kernel: Use NULL for DRIVER_MODULE()'s evh & arg (which are pointers).
[dragonfly.git] / sys / dev / usbmisc / moscom / moscom.c
1 /* $OpenBSD: src/sys/dev/usb/moscom.c,v 1.11 2007/10/11 18:33:14 deraadt Exp $ */
2
3 /*
4  * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
5  *
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.
9  *
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.
17  */
18
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/kernel.h>
22 #include <sys/conf.h>
23 #include <sys/tty.h>
24 #include <sys/device.h>
25 #include <sys/types.h>
26 #include <sys/bus.h>
27 #include <sys/module.h>
28
29 #include <bus/usb/usb.h>
30 #include <bus/usb/usbdi.h>
31 #include <bus/usb/usbdi_util.h>
32
33 #include <dev/usbmisc/ucom/ucomvar.h>
34
35 #define MOSCOMBUFSZ             256
36 #define MOSCOM_CONFIG_NO        0
37 #define MOSCOM_IFACE_NO         0
38
39 #define MOSCOM_READ             0x0d
40 #define MOSCOM_WRITE            0x0e
41 #define MOSCOM_UART_REG         0x0300
42 #define MOSCOM_VEND_REG         0x0000
43
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
61
62 #define MOSCOM_BAUDLO           0x00
63 #define MOSCOM_BAUDHI           0x01
64
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    
72
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
87
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
97
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
106
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
115
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
124
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
133
134 #define MOSCOM_BAUD_REF         115200
135
136 struct moscom_softc {
137         struct ucom_softc       sc_ucom;
138         u_char                  sc_msr;
139         u_char                  sc_lsr;
140         u_char                  sc_lcr;
141 };
142
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);    
148
149 struct ucom_callback moscom_callback = {
150         moscom_get_status,
151         moscom_set,
152         moscom_param,
153         NULL,
154         moscom_open,
155         NULL,
156         NULL,
157         NULL,
158 };
159
160 static const struct usb_devno moscom_devs[] = {
161         { USB_DEVICE(0x9710, 0x7703) }  /* MosChip MCS7703 serial port */
162 };
163
164 static device_probe_t moscom_match;
165 static device_attach_t moscom_attach;
166 static device_detach_t moscom_detach;
167
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),
172         { 0, 0 }
173 };
174
175 static driver_t moscom_driver = {
176         "ucom",
177         moscom_methods,
178         sizeof (struct moscom_softc)
179 };
180
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);
185
186 static int
187 moscom_match(device_t self)
188 {
189         struct usb_attach_arg *uaa = device_get_ivars(self);
190
191         if (uaa->iface != NULL)
192                 return UMATCH_NONE;
193
194         return (usb_lookup(moscom_devs, uaa->vendor, uaa->product) != NULL) ?
195             UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
196 }
197
198 static int
199 moscom_attach(device_t self)
200 {
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;
206         usbd_status error;
207         int i;
208
209         bzero(sc, sizeof (struct moscom_softc));
210         ucom = &sc->sc_ucom;
211         ucom->sc_dev = self;
212         ucom->sc_udev = uaa->device;
213         ucom->sc_iface = uaa->iface;
214
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");
217                 ucom->sc_dying = 1;
218                 return ENXIO;
219         }
220
221         /* get the first interface handle */
222         error = usbd_device2interface_handle(ucom->sc_udev, MOSCOM_IFACE_NO,
223             &ucom->sc_iface);
224         if (error != 0) {
225                 device_printf(ucom->sc_dev, "could not get interface handle\n");
226                 ucom->sc_dying = 1;
227                 return ENXIO;
228         }
229
230         id = usbd_get_interface_descriptor(ucom->sc_iface);
231
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);
235                 if (ed == NULL) {
236                         device_printf(ucom->sc_dev, "no endpoint descriptor "
237                                       "found for %d\n", i);
238                         ucom->sc_dying = 1;
239                         return ENXIO;
240                 }
241
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;
248         }
249
250         if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
251                 device_printf(ucom->sc_dev, "missing endpoint\n");
252                 ucom->sc_dying = 1;
253                 return ENXIO;
254         }
255
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;
263
264         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
265                            ucom->sc_dev);
266
267         return 0;
268 }
269
270 static int
271 moscom_detach(device_t self)
272 {
273         struct moscom_softc *sc = device_get_softc(self);
274         int rv = 0;
275
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,
279                            sc->sc_ucom.sc_dev);
280
281         return (rv);
282 }
283
284 #if 0 /* endif */
285 int
286 moscom_activate(struct device *self, enum devact act)
287 {
288         struct moscom_softc *sc = (struct moscom_softc *)self;
289         int rv = 0;
290
291         switch (act) {
292         case DVACT_ACTIVATE:
293                 break;
294
295         case DVACT_DEACTIVATE:
296                 if (sc->sc_subdev != NULL)
297                         rv = config_deactivate(sc->sc_subdev);
298                 sc->sc_ucom.sc_dying = 1;
299                 break;
300         }
301         return (rv);
302 }
303 #endif
304
305 static int
306 moscom_open(void *vsc, int portno)
307 {
308         struct moscom_softc *sc = vsc;
309         usb_device_request_t req;
310
311         if (sc->sc_ucom.sc_dying)
312                 return (EIO);
313
314         /* Purge FIFOs or odd things happen */
315         if (moscom_cmd(sc, MOSCOM_FIFO, 0x00) != 0)
316                 return (EIO);
317         
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) 
321                 return (EIO);
322
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)
330                 return (EIO);
331
332         return (0);
333 }
334
335 static void
336 moscom_set(void *vsc, int portno, int reg, int onoff)
337 {
338         struct moscom_softc *sc = vsc;
339         int val;
340
341         switch (reg) {
342         case UCOM_SET_DTR:
343                 val = onoff ? MOSCOM_MCR_DTR : 0;
344                 break;
345         case UCOM_SET_RTS:
346                 val = onoff ? MOSCOM_MCR_RTS : 0;
347                 break;
348         case UCOM_SET_BREAK:
349                 val = sc->sc_lcr;
350                 if (onoff)
351                         val |= MOSCOM_LCR_BREAK;
352                 moscom_cmd(sc, MOSCOM_LCR, val);
353                 return;
354         default:
355                 return;
356         }
357
358         moscom_cmd(sc, MOSCOM_MCR, val);
359 }
360
361 static int
362 moscom_param(void *vsc, int portno, struct termios *t)
363 {
364         struct moscom_softc *sc = (struct moscom_softc *)vsc;
365         int data;
366
367         if (t->c_ospeed <= 0 || t->c_ospeed > 115200)
368                 return (EINVAL);
369
370         data = MOSCOM_BAUD_REF / t->c_ospeed;
371
372         if (data == 0 || data > 0xffff)
373                 return (EINVAL);
374
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);
378
379         if (ISSET(t->c_cflag, CSTOPB))
380                 data = MOSCOM_LCR_STOP_BITS_2;
381         else
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;
386                 else
387                         data |= MOSCOM_LCR_PARITY_EVEN;
388         } else
389                 data |= MOSCOM_LCR_PARITY_NONE;
390         switch (ISSET(t->c_cflag, CSIZE)) {
391         case CS5:
392                 data |= MOSCOM_LCR_DBITS(5);
393                 break;
394         case CS6:
395                 data |= MOSCOM_LCR_DBITS(6);
396                 break;
397         case CS7:
398                 data |= MOSCOM_LCR_DBITS(7);
399                 break;
400         case CS8:
401                 data |= MOSCOM_LCR_DBITS(8);
402                 break;
403         }
404
405         sc->sc_lcr = data;
406         moscom_cmd(sc, MOSCOM_LCR, sc->sc_lcr);
407
408 #if 0
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 */
414         } else {
415                 /* disable flow ctl */
416         }
417 #endif
418
419         return (0);
420 }
421
422 static void
423 moscom_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
424 {
425         struct moscom_softc *sc = vsc;
426         
427         if (msr != NULL)
428                 *msr = sc->sc_msr;
429         if (lsr != NULL)
430                 *lsr = sc->sc_lsr;
431 }
432
433 static int
434 moscom_cmd(struct moscom_softc *sc, int reg, int val)
435 {
436         usb_device_request_t req;
437         usbd_status err;
438         
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);
445         if (err)
446                 return (EIO);
447         else
448                 return (0);
449 }