a70443c27e1b4e62703d278171f72779a3deabd9
[dragonfly.git] / sys / dev / usbmisc / uslcom / uslcom.c
1 /*      $DragonFly: src/sys/dev/usbmisc/uslcom/uslcom.c,v 1.3 2007/08/15 06:47:26 hasso Exp $   */
2 /*      $OpenBSD: uslcom.c,v 1.12 2007/06/13 06:25:03 mbalmer Exp $     */
3
4 /*
5  * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
6  *
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.
10  *
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.
18  */
19
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>
29
30 #include <bus/usb/usb.h>
31 #include <bus/usb/usbdi.h>
32 #include <bus/usb/usbdi_util.h>
33 #include <bus/usb/usbdevs.h>
34
35 #include <dev/usbmisc/ucom/ucomvar.h>
36
37 #ifdef USLCOM_DEBUG
38 #define DPRINTFN(n, x)  do { if (uslcomdebug > (n)) kprintf x; } while (0)
39 int     uslcomdebug = 0;
40 #else
41 #define DPRINTFN(n, x)
42 #endif
43 #define DPRINTF(x) DPRINTFN(0, x)
44
45 #define USLCOMBUFSZ             256
46 #define USLCOM_CONFIG_NO        0
47 #define USLCOM_IFACE_NO         0
48
49 #define USLCOM_SET_DATA_BITS(x) (x << 8)
50
51 #define USLCOM_WRITE            0x41
52 #define USLCOM_READ             0xc1
53
54 #define USLCOM_UART             0x00
55 #define USLCOM_BAUD_RATE        0x01    
56 #define USLCOM_DATA             0x03
57 #define USLCOM_BREAK            0x05
58 #define USLCOM_CTRL             0x07
59 #define USLCOM_MODEM            0x13
60
61 #define USLCOM_UART_DISABLE     0x00
62 #define USLCOM_UART_ENABLE      0x01
63
64 #define USLCOM_CTRL_DTR_ON      0x0001  
65 #define USLCOM_CTRL_DTR_SET     0x0100
66 #define USLCOM_CTRL_RTS_ON      0x0002
67 #define USLCOM_CTRL_RTS_SET     0x0200
68 #define USLCOM_CTRL_CTS         0x0010
69 #define USLCOM_CTRL_DSR         0x0020
70 #define USLCOM_CTRL_DCD         0x0080
71
72
73 #define USLCOM_BAUD_REF         0x384000
74
75 #define USLCOM_STOP_BITS_1      0x00
76 #define USLCOM_STOP_BITS_2      0x02
77
78 #define USLCOM_PARITY_NONE      0x00
79 #define USLCOM_PARITY_ODD       0x10
80 #define USLCOM_PARITY_EVEN      0x20
81
82 #define USLCOM_BREAK_OFF        0x00
83 #define USLCOM_BREAK_ON         0x01
84
85
86 struct uslcom_softc {
87         struct ucom_softc       sc_ucom;
88         u_char                  sc_msr;
89         u_char                  sc_lsr;
90 };
91
92 void    uslcom_get_status(void *, int portno, u_char *lsr, u_char *msr);
93 void    uslcom_set(void *, int, int, int);
94 int     uslcom_param(void *, int, struct termios *);
95 int     uslcom_open(void *sc, int portno);
96 void    uslcom_close(void *, int);
97 void    uslcom_break(void *sc, int portno, int onoff);
98 void    uslcom_set_flow_ctrl(struct uslcom_softc *sc, tcflag_t cflag,
99                              tcflag_t iflag);
100
101 struct ucom_callback uslcom_callback = {
102         uslcom_get_status,
103         uslcom_set,
104         uslcom_param,
105         NULL,
106         uslcom_open,
107         uslcom_close,
108         NULL,
109         NULL,
110 };
111
112 static const struct usb_devno uslcom_devs[] = {
113         { USB_VENDOR_BALTECH,           USB_PRODUCT_BALTECH_CARDREADER },
114         { USB_VENDOR_DYNASTREAM,        USB_PRODUCT_DYNASTREAM_ANTDEVBOARD },
115         { USB_VENDOR_JABLOTRON,         USB_PRODUCT_JABLOTRON_PC60B },
116         { USB_VENDOR_PLX,               USB_PRODUCT_PLX_CA42 },
117         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_ARGUSISP },
118         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CP210X_1 },
119         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CP210X_2 },
120         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CRUMB128 },
121         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_DEGREECONT },
122         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_DESKTOPMOBILE },
123         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_EDG1228 },
124         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_IPLINK1220 },
125         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_HARP },
126         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_JTAG },
127         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_LIN },
128         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_POLOLU },
129         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_RIGBLASTER },
130         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_RIGTALK },
131         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_SUNNTO },
132         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_TRACIENT_RFID },
133         { USB_VENDOR_SILABS2,           USB_PRODUCT_SILABS2_DCU11CLONE },
134         { USB_VENDOR_USI,               USB_PRODUCT_USI_MC60 }
135 };
136
137 static device_probe_t uslcom_match;
138 static device_attach_t uslcom_attach;
139 static device_detach_t uslcom_detach;
140
141 static device_method_t uslcom_methods[] = {
142         /* Device interface */
143         DEVMETHOD(device_probe, uslcom_match),
144         DEVMETHOD(device_attach, uslcom_attach),
145         DEVMETHOD(device_detach, uslcom_detach),
146         { 0, 0 }
147 };
148
149 static driver_t uslcom_driver = {
150         "ucom",
151         uslcom_methods,
152         sizeof (struct uslcom_softc)
153 };
154
155 DRIVER_MODULE(uslcom, uhub, uslcom_driver, ucom_devclass, usbd_driver_load, 0);
156 MODULE_DEPEND(uslcom, usb, 1, 1, 1);
157 MODULE_DEPEND(uslcom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
158 MODULE_VERSION(uslcom, 1);
159
160 static int
161 uslcom_match(device_t self)
162 {
163         struct usb_attach_arg *uaa = device_get_ivars(self);
164
165         if (uaa->iface != NULL)
166                 return UMATCH_NONE;
167
168         return (usb_lookup(uslcom_devs, uaa->vendor, uaa->product) != NULL) ?
169             UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
170 }
171
172 static int
173 uslcom_attach(device_t self)
174 {
175         struct uslcom_softc *sc = device_get_softc(self);
176         struct usb_attach_arg *uaa = device_get_ivars(self);
177         struct ucom_softc *ucom;
178         usb_interface_descriptor_t *id;
179         usb_endpoint_descriptor_t *ed;
180         usbd_status error;
181         char *devinfo;
182         const char *devname;
183         int i;
184
185         devinfo = kmalloc(1024, M_USBDEV, M_INTWAIT);
186         ucom = &sc->sc_ucom;
187
188         bzero(sc, sizeof (struct uslcom_softc));
189
190         usbd_devinfo(uaa->device, 0, devinfo);
191         ucom->sc_dev = self;
192         device_set_desc_copy(self, devinfo);
193
194         ucom->sc_udev = uaa->device;
195         ucom->sc_iface = uaa->iface;
196
197         devname = device_get_nameunit(ucom->sc_dev);
198         kprintf("%s: %s\n", devname, devinfo);
199         kfree(devinfo, M_USBDEV);
200
201         if (usbd_set_config_index(ucom->sc_udev, USLCOM_CONFIG_NO, 1) != 0) {
202                 kprintf("%s: could not set configuration no\n", devname);
203                 goto error;
204         }
205
206         /* get the first interface handle */
207         error = usbd_device2interface_handle(ucom->sc_udev, USLCOM_IFACE_NO,
208             &ucom->sc_iface);
209         if (error != 0) {
210                 kprintf("%s: could not get interface handle\n", devname);
211                 goto error;
212         }
213
214         id = usbd_get_interface_descriptor(ucom->sc_iface);
215
216         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
217         for (i = 0; i < id->bNumEndpoints; i++) {
218                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
219                 if (ed == NULL) {
220                         kprintf("%s: no endpoint descriptor found for %d\n",
221                             devname, i);
222                         goto error;
223                 }
224
225                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
226                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
227                         ucom->sc_bulkin_no = ed->bEndpointAddress;
228                 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
229                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
230                         ucom->sc_bulkout_no = ed->bEndpointAddress;
231         }
232
233         if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
234                 kprintf("%s: missing endpoint\n", devname);
235                 goto error;
236         }
237
238         ucom->sc_parent = sc;
239         ucom->sc_portno = UCOM_UNK_PORTNO;
240         ucom->sc_ibufsize = USLCOMBUFSZ;
241         ucom->sc_obufsize = USLCOMBUFSZ;
242         ucom->sc_ibufsizepad = USLCOMBUFSZ;
243         ucom->sc_opkthdrlen = 0;
244         ucom->sc_callback = &uslcom_callback;
245
246         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
247             ucom->sc_dev);
248
249         DPRINTF(("uslcom: in = 0x%x, out = 0x%x, intr = 0x%x\n",
250                  ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
251
252         ucom_attach(&sc->sc_ucom);
253         return 0;
254
255 error:
256          ucom->sc_dying = 1;
257          return ENXIO;
258 }
259
260 static int
261 uslcom_detach(device_t self)
262 {
263         struct uslcom_softc *sc = device_get_softc(self);
264         int rv = 0;
265
266         DPRINTF(("uslcom_detach: sc=%p\n", sc));
267         sc->sc_ucom.sc_dying = 1;
268         rv = ucom_detach(&sc->sc_ucom);
269         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ucom.sc_udev,
270                            sc->sc_ucom.sc_dev);
271
272         return (rv);
273 }
274
275 #if 0 /* not yet */
276 int
277 uslcom_activate(struct device *self, enum devact act)
278 {
279         struct uslcom_softc *sc = (struct uslcom_softc *)self;
280         int rv = 0;
281
282         switch (act) {
283         case DVACT_ACTIVATE:
284                 break;
285
286         case DVACT_DEACTIVATE:
287                 if (sc->sc_subdev != NULL)
288                         rv = config_deactivate(sc->sc_subdev);
289                 sc->sc_dying = 1;
290                 break;
291         }
292         return (rv);
293 }
294 #endif
295
296 int
297 uslcom_open(void *vsc, int portno)
298 {
299         struct uslcom_softc *sc = vsc;
300         usb_device_request_t req;
301         usbd_status err;
302
303         if (sc->sc_ucom.sc_dying)
304                 return (EIO);
305
306         req.bmRequestType = USLCOM_WRITE;
307         req.bRequest = USLCOM_UART;
308         USETW(req.wValue, USLCOM_UART_ENABLE);
309         USETW(req.wIndex, portno);
310         USETW(req.wLength, 0);
311         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
312         if (err)
313                 return (EIO);
314
315         return (0);
316 }
317
318 void
319 uslcom_close(void *vsc, int portno)
320 {
321         struct uslcom_softc *sc = vsc;
322         usb_device_request_t req;
323
324         if (sc->sc_ucom.sc_dying)
325                 return;
326
327         req.bmRequestType = USLCOM_WRITE;
328         req.bRequest = USLCOM_UART;
329         USETW(req.wValue, USLCOM_UART_DISABLE);
330         USETW(req.wIndex, portno);
331         USETW(req.wLength, 0);
332         usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
333 }
334
335 void
336 uslcom_set(void *vsc, int portno, int reg, int onoff)
337 {
338         struct uslcom_softc *sc = vsc;
339         usb_device_request_t req;
340         int ctl;
341
342         switch (reg) {
343         case UCOM_SET_DTR:
344                 ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
345                 ctl |= USLCOM_CTRL_DTR_SET;
346                 break;
347         case UCOM_SET_RTS:
348                 ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
349                 ctl |= USLCOM_CTRL_RTS_SET;
350                 break;
351         case UCOM_SET_BREAK:
352                 uslcom_break(sc, portno, onoff);
353                 return;
354         default:
355                 return;
356         }
357         req.bmRequestType = USLCOM_WRITE;
358         req.bRequest = USLCOM_CTRL;
359         USETW(req.wValue, ctl);
360         USETW(req.wIndex, portno);
361         USETW(req.wLength, 0);
362         usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
363 }
364
365 int
366 uslcom_param(void *vsc, int portno, struct termios *t)
367 {
368         struct uslcom_softc *sc = (struct uslcom_softc *)vsc;
369         usbd_status err;
370         usb_device_request_t req;
371         int data;
372
373         switch (t->c_ospeed) {
374         case 600:
375         case 1200:
376         case 1800:
377         case 2400:
378         case 4800:
379         case 9600:
380         case 19200:
381         case 38400:
382         case 57600:
383         case 115200:
384         case 460800:
385         case 921600:
386                 req.bmRequestType = USLCOM_WRITE;
387                 req.bRequest = USLCOM_BAUD_RATE;
388                 USETW(req.wValue, USLCOM_BAUD_REF / t->c_ospeed);
389                 USETW(req.wIndex, portno);
390                 USETW(req.wLength, 0);
391                 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
392                 if (err)
393                         return (EIO);
394                 break;
395         default:
396                 return (EINVAL);
397         }
398
399         if (ISSET(t->c_cflag, CSTOPB))
400                 data = USLCOM_STOP_BITS_2;
401         else
402                 data = USLCOM_STOP_BITS_1;
403         if (ISSET(t->c_cflag, PARENB)) {
404                 if (ISSET(t->c_cflag, PARODD))
405                         data |= USLCOM_PARITY_ODD;
406                 else
407                         data |= USLCOM_PARITY_EVEN;
408         } else
409                 data |= USLCOM_PARITY_NONE;
410         switch (ISSET(t->c_cflag, CSIZE)) {
411         case CS5:
412                 data |= USLCOM_SET_DATA_BITS(5);
413                 break;
414         case CS6:
415                 data |= USLCOM_SET_DATA_BITS(6);
416                 break;
417         case CS7:
418                 data |= USLCOM_SET_DATA_BITS(7);
419                 break;
420         case CS8:
421                 data |= USLCOM_SET_DATA_BITS(8);
422                 break;
423         }
424
425         req.bmRequestType = USLCOM_WRITE;
426         req.bRequest = USLCOM_DATA;
427         USETW(req.wValue, data);
428         USETW(req.wIndex, portno);
429         USETW(req.wLength, 0);
430         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
431         if (err)
432                 return (EIO);
433
434         uslcom_set_flow_ctrl(sc, t->c_cflag, t->c_iflag);
435
436         return (0);
437 }
438
439 void
440 uslcom_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
441 {
442         struct uslcom_softc *sc = vsc;
443         
444         if (msr != NULL)
445                 *msr = sc->sc_msr;
446         if (lsr != NULL)
447                 *lsr = sc->sc_lsr;
448 }
449
450 void
451 uslcom_break(void *vsc, int portno, int onoff)
452 {
453         struct uslcom_softc *sc = vsc;
454         usb_device_request_t req;
455         int brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;   
456
457         req.bmRequestType = USLCOM_WRITE;
458         req.bRequest = USLCOM_BREAK;
459         USETW(req.wValue, brk);
460         USETW(req.wIndex, portno);
461         USETW(req.wLength, 0);
462         usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
463 }
464
465 void
466 uslcom_set_flow_ctrl(struct uslcom_softc *sc, tcflag_t cflag, tcflag_t iflag)
467 {
468         uint8_t modemdata[16];
469         usb_device_request_t req;
470         usbd_status err;
471
472         req.bmRequestType = USLCOM_READ;
473         req.bRequest = USLCOM_MODEM;
474         USETW(req.wValue, 0);
475         USETW(req.wIndex, 0);
476         USETW(req.wLength, 16);
477
478         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, modemdata);
479         if (err)
480                 kprintf("uslcom_set_flow: %s\n", usbd_errstr(err));
481
482         if (ISSET(cflag, CRTSCTS)) {
483                 modemdata[0] &= ~0x7b;
484                 modemdata[0] |= 0x09;
485                 modemdata[4] = 0x80;
486         } else {
487                 modemdata[0] &= ~0x7b;
488                 modemdata[0] |= 0x01;
489                 modemdata[4] = 0x40;
490         }
491
492         req.bmRequestType = USLCOM_WRITE;
493         req.bRequest = USLCOM_MODEM;
494         USETW(req.wValue, 0);
495         USETW(req.wIndex, 0);
496         USETW(req.wLength, 16);
497
498         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, modemdata);
499         if (err)
500                 kprintf("uslcom_set_flow: %s\n", usbd_errstr(err));
501 }
502