1 /* $OpenBSD: src/sys/dev/usb/moscom.c,v 1.11 2007/10/11 18:33:14 deraadt Exp $ */
4 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/kernel.h>
24 #include <sys/device.h>
25 #include <sys/types.h>
27 #include <sys/module.h>
29 #include <bus/usb/usb.h>
30 #include <bus/usb/usbdi.h>
31 #include <bus/usb/usbdi_util.h>
33 #include <dev/usbmisc/ucom/ucomvar.h>
35 #define MOSCOMBUFSZ 256
36 #define MOSCOM_CONFIG_NO 0
37 #define MOSCOM_IFACE_NO 0
39 #define MOSCOM_READ 0x0d
40 #define MOSCOM_WRITE 0x0e
41 #define MOSCOM_UART_REG 0x0300
42 #define MOSCOM_VEND_REG 0x0000
44 #define MOSCOM_TXBUF 0x00 /* Write */
45 #define MOSCOM_RXBUF 0x00 /* Read */
46 #define MOSCOM_INT 0x01
47 #define MOSCOM_FIFO 0x02 /* Write */
48 #define MOSCOM_ISR 0x02 /* Read */
49 #define MOSCOM_LCR 0x03
50 #define MOSCOM_MCR 0x04
51 #define MOSCOM_LSR 0x05
52 #define MOSCOM_MSR 0x06
53 #define MOSCOM_SCRATCH 0x07
54 #define MOSCOM_DIV_LO 0x08
55 #define MOSCOM_DIV_HI 0x09
56 #define MOSCOM_EFR 0x0a
57 #define MOSCOM_XON1 0x0b
58 #define MOSCOM_XON2 0x0c
59 #define MOSCOM_XOFF1 0x0d
60 #define MOSCOM_XOFF2 0x0e
62 #define MOSCOM_BAUDLO 0x00
63 #define MOSCOM_BAUDHI 0x01
65 #define MOSCOM_INT_RXEN 0x01
66 #define MOSCOM_INT_TXEN 0x02
67 #define MOSCOM_INT_RSEN 0x04
68 #define MOSCOM_INT_MDMEM 0x08
69 #define MOSCOM_INT_SLEEP 0x10
70 #define MOSCOM_INT_XOFF 0x20
71 #define MOSCOM_INT_RTS 0x40
73 #define MOSCOM_FIFO_EN 0x01
74 #define MOSCOM_FIFO_RXCLR 0x02
75 #define MOSCOM_FIFO_TXCLR 0x04
76 #define MOSCOM_FIFO_DMA_BLK 0x08
77 #define MOSCOM_FIFO_TXLVL_MASK 0x30
78 #define MOSCOM_FIFO_TXLVL_8 0x00
79 #define MOSCOM_FIFO_TXLVL_16 0x10
80 #define MOSCOM_FIFO_TXLVL_32 0x20
81 #define MOSCOM_FIFO_TXLVL_56 0x30
82 #define MOSCOM_FIFO_RXLVL_MASK 0xc0
83 #define MOSCOM_FIFO_RXLVL_8 0x00
84 #define MOSCOM_FIFO_RXLVL_16 0x40
85 #define MOSCOM_FIFO_RXLVL_56 0x80
86 #define MOSCOM_FIFO_RXLVL_80 0xc0
88 #define MOSCOM_ISR_MDM 0x00
89 #define MOSCOM_ISR_NONE 0x01
90 #define MOSCOM_ISR_TX 0x02
91 #define MOSCOM_ISR_RX 0x04
92 #define MOSCOM_ISR_LINE 0x06
93 #define MOSCOM_ISR_RXTIMEOUT 0x0c
94 #define MOSCOM_ISR_RX_XOFF 0x10
95 #define MOSCOM_ISR_RTSCTS 0x20
96 #define MOSCOM_ISR_FIFOEN 0xc0
98 #define MOSCOM_LCR_DBITS(x) (x - 5)
99 #define MOSCOM_LCR_STOP_BITS_1 0x00
100 #define MOSCOM_LCR_STOP_BITS_2 0x04 /* 2 if 6-8 bits/char or 1.5 if 5 */
101 #define MOSCOM_LCR_PARITY_NONE 0x00
102 #define MOSCOM_LCR_PARITY_ODD 0x08
103 #define MOSCOM_LCR_PARITY_EVEN 0x18
104 #define MOSCOM_LCR_BREAK 0x40
105 #define MOSCOM_LCR_DIVLATCH_EN 0x80
107 #define MOSCOM_MCR_DTR 0x01
108 #define MOSCOM_MCR_RTS 0x02
109 #define MOSCOM_MCR_LOOP 0x04
110 #define MOSCOM_MCR_INTEN 0x08
111 #define MOSCOM_MCR_LOOPBACK 0x10
112 #define MOSCOM_MCR_XONANY 0x20
113 #define MOSCOM_MCR_IRDA_EN 0x40
114 #define MOSCOM_MCR_BAUD_DIV4 0x80
116 #define MOSCOM_LSR_RXDATA 0x01
117 #define MOSCOM_LSR_RXOVER 0x02
118 #define MOSCOM_LSR_RXPAR_ERR 0x04
119 #define MOSCOM_LSR_RXFRM_ERR 0x08
120 #define MOSCOM_LSR_RXBREAK 0x10
121 #define MOSCOM_LSR_TXEMPTY 0x20
122 #define MOSCOM_LSR_TXALLEMPTY 0x40
123 #define MOSCOM_LSR_TXFIFO_ERR 0x80
125 #define MOSCOM_MSR_CTS_CHG 0x01
126 #define MOSCOM_MSR_DSR_CHG 0x02
127 #define MOSCOM_MSR_RI_CHG 0x04
128 #define MOSCOM_MSR_CD_CHG 0x08
129 #define MOSCOM_MSR_CTS 0x10
130 #define MOSCOM_MSR_RTS 0x20
131 #define MOSCOM_MSR_RI 0x40
132 #define MOSCOM_MSR_CD 0x80
134 #define MOSCOM_BAUD_REF 115200
136 struct moscom_softc {
137 struct ucom_softc sc_ucom;
143 static void moscom_get_status(void *, int, u_char *, u_char *);
144 static void moscom_set(void *, int, int, int);
145 static int moscom_param(void *, int, struct termios *);
146 static int moscom_open(void *, int);
147 static int moscom_cmd(struct moscom_softc *, int, int);
149 struct ucom_callback moscom_callback = {
160 static const struct usb_devno moscom_devs[] = {
161 { USB_DEVICE(0x9710, 0x7703) } /* MosChip MCS7703 serial port */
164 static device_probe_t moscom_match;
165 static device_attach_t moscom_attach;
166 static device_detach_t moscom_detach;
168 static device_method_t moscom_methods[] = {
169 DEVMETHOD(device_probe, moscom_match),
170 DEVMETHOD(device_attach, moscom_attach),
171 DEVMETHOD(device_detach, moscom_detach),
175 static driver_t moscom_driver = {
178 sizeof (struct moscom_softc)
181 DRIVER_MODULE(moscom, uhub, moscom_driver, ucom_devclass, usbd_driver_load, NULL);
182 MODULE_DEPEND(moscom, usb, 1, 1, 1);
183 MODULE_DEPEND(moscom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
184 MODULE_VERSION(moscom, 1);
187 moscom_match(device_t self)
189 struct usb_attach_arg *uaa = device_get_ivars(self);
191 if (uaa->iface != NULL)
194 return (usb_lookup(moscom_devs, uaa->vendor, uaa->product) != NULL) ?
195 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
199 moscom_attach(device_t self)
201 struct moscom_softc *sc = device_get_softc(self);
202 struct usb_attach_arg *uaa = device_get_ivars(self);
203 struct ucom_softc *ucom;
204 usb_interface_descriptor_t *id;
205 usb_endpoint_descriptor_t *ed;
209 bzero(sc, sizeof (struct moscom_softc));
212 ucom->sc_udev = uaa->device;
213 ucom->sc_iface = uaa->iface;
215 if (usbd_set_config_index(ucom->sc_udev, MOSCOM_CONFIG_NO, 1) != 0) {
216 device_printf(ucom->sc_dev, "could not set configuration no\n");
221 /* get the first interface handle */
222 error = usbd_device2interface_handle(ucom->sc_udev, MOSCOM_IFACE_NO,
225 device_printf(ucom->sc_dev, "could not get interface handle\n");
230 id = usbd_get_interface_descriptor(ucom->sc_iface);
232 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
233 for (i = 0; i < id->bNumEndpoints; i++) {
234 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
236 device_printf(ucom->sc_dev, "no endpoint descriptor "
237 "found for %d\n", i);
242 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
243 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
244 ucom->sc_bulkin_no = ed->bEndpointAddress;
245 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
246 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
247 ucom->sc_bulkout_no = ed->bEndpointAddress;
250 if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
251 device_printf(ucom->sc_dev, "missing endpoint\n");
256 ucom->sc_parent = sc;
257 ucom->sc_portno = UCOM_UNK_PORTNO;
258 ucom->sc_ibufsize = MOSCOMBUFSZ;
259 ucom->sc_obufsize = MOSCOMBUFSZ;
260 ucom->sc_ibufsizepad = MOSCOMBUFSZ;
261 ucom->sc_opkthdrlen = 0;
262 ucom->sc_callback = &moscom_callback;
264 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
271 moscom_detach(device_t self)
273 struct moscom_softc *sc = device_get_softc(self);
276 sc->sc_ucom.sc_dying = 1;
277 rv = ucom_detach(&sc->sc_ucom);
278 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ucom.sc_udev,
286 moscom_activate(struct device *self, enum devact act)
288 struct moscom_softc *sc = (struct moscom_softc *)self;
295 case DVACT_DEACTIVATE:
296 if (sc->sc_subdev != NULL)
297 rv = config_deactivate(sc->sc_subdev);
298 sc->sc_ucom.sc_dying = 1;
306 moscom_open(void *vsc, int portno)
308 struct moscom_softc *sc = vsc;
309 usb_device_request_t req;
311 if (sc->sc_ucom.sc_dying)
314 /* Purge FIFOs or odd things happen */
315 if (moscom_cmd(sc, MOSCOM_FIFO, 0x00) != 0)
318 if (moscom_cmd(sc, MOSCOM_FIFO, MOSCOM_FIFO_EN |
319 MOSCOM_FIFO_RXCLR | MOSCOM_FIFO_TXCLR |
320 MOSCOM_FIFO_DMA_BLK | MOSCOM_FIFO_RXLVL_MASK) != 0)
323 /* Magic tell device we're ready for data command */
324 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
325 req.bRequest = MOSCOM_WRITE;
326 USETW(req.wValue, 0x08);
327 USETW(req.wIndex, MOSCOM_INT);
328 USETW(req.wLength, 0);
329 if (usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL) != 0)
336 moscom_set(void *vsc, int portno, int reg, int onoff)
338 struct moscom_softc *sc = vsc;
343 val = onoff ? MOSCOM_MCR_DTR : 0;
346 val = onoff ? MOSCOM_MCR_RTS : 0;
351 val |= MOSCOM_LCR_BREAK;
352 moscom_cmd(sc, MOSCOM_LCR, val);
358 moscom_cmd(sc, MOSCOM_MCR, val);
362 moscom_param(void *vsc, int portno, struct termios *t)
364 struct moscom_softc *sc = (struct moscom_softc *)vsc;
367 if (t->c_ospeed <= 0 || t->c_ospeed > 115200)
370 data = MOSCOM_BAUD_REF / t->c_ospeed;
372 if (data == 0 || data > 0xffff)
375 moscom_cmd(sc, MOSCOM_LCR, MOSCOM_LCR_DIVLATCH_EN);
376 moscom_cmd(sc, MOSCOM_BAUDLO, data & 0xFF);
377 moscom_cmd(sc, MOSCOM_BAUDHI, (data >> 8) & 0xFF);
379 if (ISSET(t->c_cflag, CSTOPB))
380 data = MOSCOM_LCR_STOP_BITS_2;
382 data = MOSCOM_LCR_STOP_BITS_1;
383 if (ISSET(t->c_cflag, PARENB)) {
384 if (ISSET(t->c_cflag, PARODD))
385 data |= MOSCOM_LCR_PARITY_ODD;
387 data |= MOSCOM_LCR_PARITY_EVEN;
389 data |= MOSCOM_LCR_PARITY_NONE;
390 switch (ISSET(t->c_cflag, CSIZE)) {
392 data |= MOSCOM_LCR_DBITS(5);
395 data |= MOSCOM_LCR_DBITS(6);
398 data |= MOSCOM_LCR_DBITS(7);
401 data |= MOSCOM_LCR_DBITS(8);
406 moscom_cmd(sc, MOSCOM_LCR, sc->sc_lcr);
409 /* XXX flow control */
410 if (ISSET(t->c_cflag, CRTSCTS))
411 /* rts/cts flow ctl */
412 } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
413 /* xon/xoff flow ctl */
415 /* disable flow ctl */
423 moscom_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
425 struct moscom_softc *sc = vsc;
434 moscom_cmd(struct moscom_softc *sc, int reg, int val)
436 usb_device_request_t req;
439 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
440 req.bRequest = MOSCOM_WRITE;
441 USETW(req.wValue, val + MOSCOM_UART_REG);
442 USETW(req.wIndex, reg);
443 USETW(req.wLength, 0);
444 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);