Add moscom(4) - the driver for MosChip Semiconductor MCS7703 USB to
[dragonfly/netmp.git] / sys / dev / usbmisc / moscom / moscom.c
blob29e63899a12e5631dcd5cbbf02176242557830c0
1 /* $OpenBSD: src/sys/dev/usb/moscom.c,v 1.11 2007/10/11 18:33:14 deraadt Exp $ */
2 /* $DragonFly: src/sys/dev/usbmisc/moscom/moscom.c,v 1.1 2007/11/07 08:57:18 hasso Exp $ */
4 /*
5 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/param.h>
21 #include <sys/systm.h>
22 #include <sys/kernel.h>
23 #include <sys/conf.h>
24 #include <sys/tty.h>
25 #include <sys/device.h>
26 #include <sys/types.h>
27 #include <sys/bus.h>
28 #include <sys/module.h>
30 #include <bus/usb/usb.h>
31 #include <bus/usb/usbdi.h>
32 #include <bus/usb/usbdi_util.h>
34 #include <dev/usbmisc/ucom/ucomvar.h>
36 #define MOSCOMBUFSZ 256
37 #define MOSCOM_CONFIG_NO 0
38 #define MOSCOM_IFACE_NO 0
40 #define MOSCOM_READ 0x0d
41 #define MOSCOM_WRITE 0x0e
42 #define MOSCOM_UART_REG 0x0300
43 #define MOSCOM_VEND_REG 0x0000
45 #define MOSCOM_TXBUF 0x00 /* Write */
46 #define MOSCOM_RXBUF 0x00 /* Read */
47 #define MOSCOM_INT 0x01
48 #define MOSCOM_FIFO 0x02 /* Write */
49 #define MOSCOM_ISR 0x02 /* Read */
50 #define MOSCOM_LCR 0x03
51 #define MOSCOM_MCR 0x04
52 #define MOSCOM_LSR 0x05
53 #define MOSCOM_MSR 0x06
54 #define MOSCOM_SCRATCH 0x07
55 #define MOSCOM_DIV_LO 0x08
56 #define MOSCOM_DIV_HI 0x09
57 #define MOSCOM_EFR 0x0a
58 #define MOSCOM_XON1 0x0b
59 #define MOSCOM_XON2 0x0c
60 #define MOSCOM_XOFF1 0x0d
61 #define MOSCOM_XOFF2 0x0e
63 #define MOSCOM_BAUDLO 0x00
64 #define MOSCOM_BAUDHI 0x01
66 #define MOSCOM_INT_RXEN 0x01
67 #define MOSCOM_INT_TXEN 0x02
68 #define MOSCOM_INT_RSEN 0x04
69 #define MOSCOM_INT_MDMEM 0x08
70 #define MOSCOM_INT_SLEEP 0x10
71 #define MOSCOM_INT_XOFF 0x20
72 #define MOSCOM_INT_RTS 0x40
74 #define MOSCOM_FIFO_EN 0x01
75 #define MOSCOM_FIFO_RXCLR 0x02
76 #define MOSCOM_FIFO_TXCLR 0x04
77 #define MOSCOM_FIFO_DMA_BLK 0x08
78 #define MOSCOM_FIFO_TXLVL_MASK 0x30
79 #define MOSCOM_FIFO_TXLVL_8 0x00
80 #define MOSCOM_FIFO_TXLVL_16 0x10
81 #define MOSCOM_FIFO_TXLVL_32 0x20
82 #define MOSCOM_FIFO_TXLVL_56 0x30
83 #define MOSCOM_FIFO_RXLVL_MASK 0xc0
84 #define MOSCOM_FIFO_RXLVL_8 0x00
85 #define MOSCOM_FIFO_RXLVL_16 0x40
86 #define MOSCOM_FIFO_RXLVL_56 0x80
87 #define MOSCOM_FIFO_RXLVL_80 0xc0
89 #define MOSCOM_ISR_MDM 0x00
90 #define MOSCOM_ISR_NONE 0x01
91 #define MOSCOM_ISR_TX 0x02
92 #define MOSCOM_ISR_RX 0x04
93 #define MOSCOM_ISR_LINE 0x06
94 #define MOSCOM_ISR_RXTIMEOUT 0x0c
95 #define MOSCOM_ISR_RX_XOFF 0x10
96 #define MOSCOM_ISR_RTSCTS 0x20
97 #define MOSCOM_ISR_FIFOEN 0xc0
99 #define MOSCOM_LCR_DBITS(x) (x - 5)
100 #define MOSCOM_LCR_STOP_BITS_1 0x00
101 #define MOSCOM_LCR_STOP_BITS_2 0x04 /* 2 if 6-8 bits/char or 1.5 if 5 */
102 #define MOSCOM_LCR_PARITY_NONE 0x00
103 #define MOSCOM_LCR_PARITY_ODD 0x08
104 #define MOSCOM_LCR_PARITY_EVEN 0x18
105 #define MOSCOM_LCR_BREAK 0x40
106 #define MOSCOM_LCR_DIVLATCH_EN 0x80
108 #define MOSCOM_MCR_DTR 0x01
109 #define MOSCOM_MCR_RTS 0x02
110 #define MOSCOM_MCR_LOOP 0x04
111 #define MOSCOM_MCR_INTEN 0x08
112 #define MOSCOM_MCR_LOOPBACK 0x10
113 #define MOSCOM_MCR_XONANY 0x20
114 #define MOSCOM_MCR_IRDA_EN 0x40
115 #define MOSCOM_MCR_BAUD_DIV4 0x80
117 #define MOSCOM_LSR_RXDATA 0x01
118 #define MOSCOM_LSR_RXOVER 0x02
119 #define MOSCOM_LSR_RXPAR_ERR 0x04
120 #define MOSCOM_LSR_RXFRM_ERR 0x08
121 #define MOSCOM_LSR_RXBREAK 0x10
122 #define MOSCOM_LSR_TXEMPTY 0x20
123 #define MOSCOM_LSR_TXALLEMPTY 0x40
124 #define MOSCOM_LSR_TXFIFO_ERR 0x80
126 #define MOSCOM_MSR_CTS_CHG 0x01
127 #define MOSCOM_MSR_DSR_CHG 0x02
128 #define MOSCOM_MSR_RI_CHG 0x04
129 #define MOSCOM_MSR_CD_CHG 0x08
130 #define MOSCOM_MSR_CTS 0x10
131 #define MOSCOM_MSR_RTS 0x20
132 #define MOSCOM_MSR_RI 0x40
133 #define MOSCOM_MSR_CD 0x80
135 #define MOSCOM_BAUD_REF 115200
137 struct moscom_softc {
138 struct ucom_softc sc_ucom;
139 u_char sc_msr;
140 u_char sc_lsr;
141 u_char sc_lcr;
144 static void moscom_get_status(void *, int, u_char *, u_char *);
145 static void moscom_set(void *, int, int, int);
146 static int moscom_param(void *, int, struct termios *);
147 static int moscom_open(void *, int);
148 static int moscom_cmd(struct moscom_softc *, int, int);
150 struct ucom_callback moscom_callback = {
151 moscom_get_status,
152 moscom_set,
153 moscom_param,
154 NULL,
155 moscom_open,
156 NULL,
157 NULL,
158 NULL,
161 static const struct usb_devno moscom_devs[] = {
162 { USB_DEVICE(0x9710, 0x7703) } /* MosChip MCS7703 serial port */
165 static device_probe_t moscom_match;
166 static device_attach_t moscom_attach;
167 static device_detach_t moscom_detach;
169 static device_method_t moscom_methods[] = {
170 DEVMETHOD(device_probe, moscom_match),
171 DEVMETHOD(device_attach, moscom_attach),
172 DEVMETHOD(device_detach, moscom_detach),
173 { 0, 0 }
176 static driver_t moscom_driver = {
177 "ucom",
178 moscom_methods,
179 sizeof (struct moscom_softc)
182 DRIVER_MODULE(moscom, uhub, moscom_driver, ucom_devclass, usbd_driver_load, 0);
183 MODULE_DEPEND(moscom, usb, 1, 1, 1);
184 MODULE_DEPEND(moscom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
185 MODULE_VERSION(moscom, 1);
187 static int
188 moscom_match(device_t self)
190 struct usb_attach_arg *uaa = device_get_ivars(self);
192 if (uaa->iface != NULL)
193 return UMATCH_NONE;
195 return (usb_lookup(moscom_devs, uaa->vendor, uaa->product) != NULL) ?
196 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
199 static int
200 moscom_attach(device_t self)
202 struct moscom_softc *sc = device_get_softc(self);
203 struct usb_attach_arg *uaa = device_get_ivars(self);
204 struct ucom_softc *ucom;
205 usb_interface_descriptor_t *id;
206 usb_endpoint_descriptor_t *ed;
207 usbd_status error;
208 int i;
210 bzero(sc, sizeof (struct moscom_softc));
211 ucom = &sc->sc_ucom;
212 ucom->sc_dev = self;
213 ucom->sc_udev = uaa->device;
214 ucom->sc_iface = uaa->iface;
216 if (usbd_set_config_index(ucom->sc_udev, MOSCOM_CONFIG_NO, 1) != 0) {
217 device_printf(ucom->sc_dev, "could not set configuration no\n");
218 ucom->sc_dying = 1;
219 return ENXIO;
222 /* get the first interface handle */
223 error = usbd_device2interface_handle(ucom->sc_udev, MOSCOM_IFACE_NO,
224 &ucom->sc_iface);
225 if (error != 0) {
226 device_printf(ucom->sc_dev, "could not get interface handle\n");
227 ucom->sc_dying = 1;
228 return ENXIO;
231 id = usbd_get_interface_descriptor(ucom->sc_iface);
233 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
234 for (i = 0; i < id->bNumEndpoints; i++) {
235 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
236 if (ed == NULL) {
237 device_printf(ucom->sc_dev, "no endpoint descriptor "
238 "found for %d\n", i);
239 ucom->sc_dying = 1;
240 return ENXIO;
243 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
244 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
245 ucom->sc_bulkin_no = ed->bEndpointAddress;
246 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
247 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
248 ucom->sc_bulkout_no = ed->bEndpointAddress;
251 if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
252 device_printf(ucom->sc_dev, "missing endpoint\n");
253 ucom->sc_dying = 1;
254 return ENXIO;
257 ucom->sc_parent = sc;
258 ucom->sc_portno = UCOM_UNK_PORTNO;
259 ucom->sc_ibufsize = MOSCOMBUFSZ;
260 ucom->sc_obufsize = MOSCOMBUFSZ;
261 ucom->sc_ibufsizepad = MOSCOMBUFSZ;
262 ucom->sc_opkthdrlen = 0;
263 ucom->sc_callback = &moscom_callback;
265 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
266 ucom->sc_dev);
268 return 0;
271 static int
272 moscom_detach(device_t self)
274 struct moscom_softc *sc = device_get_softc(self);
275 int rv = 0;
277 sc->sc_ucom.sc_dying = 1;
278 rv = ucom_detach(&sc->sc_ucom);
279 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ucom.sc_udev,
280 sc->sc_ucom.sc_dev);
282 return (rv);
285 #if 0 /* endif */
287 moscom_activate(struct device *self, enum devact act)
289 struct moscom_softc *sc = (struct moscom_softc *)self;
290 int rv = 0;
292 switch (act) {
293 case DVACT_ACTIVATE:
294 break;
296 case DVACT_DEACTIVATE:
297 if (sc->sc_subdev != NULL)
298 rv = config_deactivate(sc->sc_subdev);
299 sc->sc_ucom.sc_dying = 1;
300 break;
302 return (rv);
304 #endif
306 static int
307 moscom_open(void *vsc, int portno)
309 struct moscom_softc *sc = vsc;
310 usb_device_request_t req;
312 if (sc->sc_ucom.sc_dying)
313 return (EIO);
315 /* Purge FIFOs or odd things happen */
316 if (moscom_cmd(sc, MOSCOM_FIFO, 0x00) != 0)
317 return (EIO);
319 if (moscom_cmd(sc, MOSCOM_FIFO, MOSCOM_FIFO_EN |
320 MOSCOM_FIFO_RXCLR | MOSCOM_FIFO_TXCLR |
321 MOSCOM_FIFO_DMA_BLK | MOSCOM_FIFO_RXLVL_MASK) != 0)
322 return (EIO);
324 /* Magic tell device we're ready for data command */
325 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
326 req.bRequest = MOSCOM_WRITE;
327 USETW(req.wValue, 0x08);
328 USETW(req.wIndex, MOSCOM_INT);
329 USETW(req.wLength, 0);
330 if (usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL) != 0)
331 return (EIO);
333 return (0);
336 static void
337 moscom_set(void *vsc, int portno, int reg, int onoff)
339 struct moscom_softc *sc = vsc;
340 int val;
342 switch (reg) {
343 case UCOM_SET_DTR:
344 val = onoff ? MOSCOM_MCR_DTR : 0;
345 break;
346 case UCOM_SET_RTS:
347 val = onoff ? MOSCOM_MCR_RTS : 0;
348 break;
349 case UCOM_SET_BREAK:
350 val = sc->sc_lcr;
351 if (onoff)
352 val |= MOSCOM_LCR_BREAK;
353 moscom_cmd(sc, MOSCOM_LCR, val);
354 return;
355 default:
356 return;
359 moscom_cmd(sc, MOSCOM_MCR, val);
362 static int
363 moscom_param(void *vsc, int portno, struct termios *t)
365 struct moscom_softc *sc = (struct moscom_softc *)vsc;
366 int data;
368 if (t->c_ospeed <= 0 || t->c_ospeed > 115200)
369 return (EINVAL);
371 data = MOSCOM_BAUD_REF / t->c_ospeed;
373 if (data == 0 || data > 0xffff)
374 return (EINVAL);
376 moscom_cmd(sc, MOSCOM_LCR, MOSCOM_LCR_DIVLATCH_EN);
377 moscom_cmd(sc, MOSCOM_BAUDLO, data & 0xFF);
378 moscom_cmd(sc, MOSCOM_BAUDHI, (data >> 8) & 0xFF);
380 if (ISSET(t->c_cflag, CSTOPB))
381 data = MOSCOM_LCR_STOP_BITS_2;
382 else
383 data = MOSCOM_LCR_STOP_BITS_1;
384 if (ISSET(t->c_cflag, PARENB)) {
385 if (ISSET(t->c_cflag, PARODD))
386 data |= MOSCOM_LCR_PARITY_ODD;
387 else
388 data |= MOSCOM_LCR_PARITY_EVEN;
389 } else
390 data |= MOSCOM_LCR_PARITY_NONE;
391 switch (ISSET(t->c_cflag, CSIZE)) {
392 case CS5:
393 data |= MOSCOM_LCR_DBITS(5);
394 break;
395 case CS6:
396 data |= MOSCOM_LCR_DBITS(6);
397 break;
398 case CS7:
399 data |= MOSCOM_LCR_DBITS(7);
400 break;
401 case CS8:
402 data |= MOSCOM_LCR_DBITS(8);
403 break;
406 sc->sc_lcr = data;
407 moscom_cmd(sc, MOSCOM_LCR, sc->sc_lcr);
409 #if 0
410 /* XXX flow control */
411 if (ISSET(t->c_cflag, CRTSCTS))
412 /* rts/cts flow ctl */
413 } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
414 /* xon/xoff flow ctl */
415 } else {
416 /* disable flow ctl */
418 #endif
420 return (0);
423 static void
424 moscom_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
426 struct moscom_softc *sc = vsc;
428 if (msr != NULL)
429 *msr = sc->sc_msr;
430 if (lsr != NULL)
431 *lsr = sc->sc_lsr;
434 static int
435 moscom_cmd(struct moscom_softc *sc, int reg, int val)
437 usb_device_request_t req;
438 usbd_status err;
440 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
441 req.bRequest = MOSCOM_WRITE;
442 USETW(req.wValue, val + MOSCOM_UART_REG);
443 USETW(req.wIndex, reg);
444 USETW(req.wLength, 0);
445 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
446 if (err)
447 return (EIO);
448 else
449 return (0);