Merge from vendor branch OPENSSL:
[dragonfly.git] / sys / dev / usbmisc / uslcom / uslcom.c
1 /*      $DragonFly: src/sys/dev/usbmisc/uslcom/uslcom.c,v 1.5 2007/08/19 17:16:43 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 static void     uslcom_get_status(void *, int portno, u_char *lsr,
93                                   u_char *msr);
94 static void     uslcom_set(void *, int, int, int);
95 static int      uslcom_param(void *, int, struct termios *);
96 static int      uslcom_open(void *sc, int portno);
97 static void     uslcom_close(void *, int);
98 static void     uslcom_break(void *sc, int portno, int onoff);
99 static void     uslcom_set_flow_ctrl(struct uslcom_softc *sc, tcflag_t cflag,
100                                      tcflag_t iflag);
101
102 struct ucom_callback uslcom_callback = {
103         uslcom_get_status,
104         uslcom_set,
105         uslcom_param,
106         NULL,
107         uslcom_open,
108         uslcom_close,
109         NULL,
110         NULL,
111 };
112
113 static const struct usb_devno uslcom_devs[] = {
114         { USB_VENDOR_BALTECH,           USB_PRODUCT_BALTECH_CARDREADER },
115         { USB_VENDOR_DYNASTREAM,        USB_PRODUCT_DYNASTREAM_ANTDEVBOARD },
116         { USB_VENDOR_JABLOTRON,         USB_PRODUCT_JABLOTRON_PC60B },
117         { USB_VENDOR_PLX,               USB_PRODUCT_PLX_CA42 },
118         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_ARGUSISP },
119         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CP210X_1 },
120         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CP210X_2 },
121         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CRUMB128 },
122         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_DEGREECONT },
123         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_DESKTOPMOBILE },
124         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_EDG1228 },
125         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_IPLINK1220 },
126         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_HARP },
127         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_JTAG },
128         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_LIN },
129         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_POLOLU },
130         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_RIGBLASTER },
131         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_RIGTALK },
132         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_SUNNTO },
133         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_TRACIENT_RFID },
134         { USB_VENDOR_SILABS2,           USB_PRODUCT_SILABS2_DCU11CLONE },
135         { USB_VENDOR_USI,               USB_PRODUCT_USI_MC60 }
136 };
137
138 static device_probe_t uslcom_match;
139 static device_attach_t uslcom_attach;
140 static device_detach_t uslcom_detach;
141
142 static device_method_t uslcom_methods[] = {
143         /* Device interface */
144         DEVMETHOD(device_probe, uslcom_match),
145         DEVMETHOD(device_attach, uslcom_attach),
146         DEVMETHOD(device_detach, uslcom_detach),
147         { 0, 0 }
148 };
149
150 static driver_t uslcom_driver = {
151         "ucom",
152         uslcom_methods,
153         sizeof (struct uslcom_softc)
154 };
155
156 DRIVER_MODULE(uslcom, uhub, uslcom_driver, ucom_devclass, usbd_driver_load, 0);
157 MODULE_DEPEND(uslcom, usb, 1, 1, 1);
158 MODULE_DEPEND(uslcom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
159 MODULE_VERSION(uslcom, 1);
160
161 static int
162 uslcom_match(device_t self)
163 {
164         struct usb_attach_arg *uaa = device_get_ivars(self);
165
166         if (uaa->iface != NULL)
167                 return UMATCH_NONE;
168
169         return (usb_lookup(uslcom_devs, uaa->vendor, uaa->product) != NULL) ?
170             UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
171 }
172
173 static int
174 uslcom_attach(device_t self)
175 {
176         struct uslcom_softc *sc = device_get_softc(self);
177         struct usb_attach_arg *uaa = device_get_ivars(self);
178         struct ucom_softc *ucom;
179         usb_interface_descriptor_t *id;
180         usb_endpoint_descriptor_t *ed;
181         usbd_status error;
182         char *devinfo;
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         device_printf(ucom->sc_dev, "%s\n", devinfo);
198         kfree(devinfo, M_USBDEV);
199
200         if (usbd_set_config_index(ucom->sc_udev, USLCOM_CONFIG_NO, 1) != 0) {
201                 device_printf(ucom->sc_dev, "could not set configuration no\n");
202                 goto error;
203         }
204
205         /* get the first interface handle */
206         error = usbd_device2interface_handle(ucom->sc_udev, USLCOM_IFACE_NO,
207             &ucom->sc_iface);
208         if (error != 0) {
209                 device_printf(ucom->sc_dev, "could not get interface handle\n");
210                 goto error;
211         }
212
213         id = usbd_get_interface_descriptor(ucom->sc_iface);
214
215         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
216         for (i = 0; i < id->bNumEndpoints; i++) {
217                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
218                 if (ed == NULL) {
219                         device_printf(ucom->sc_dev, "no endpoint descriptor "
220                                       "found for %d\n", i);
221                         goto error;
222                 }
223
224                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
225                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
226                         ucom->sc_bulkin_no = ed->bEndpointAddress;
227                 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
228                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
229                         ucom->sc_bulkout_no = ed->bEndpointAddress;
230         }
231
232         if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
233                 device_printf(ucom->sc_dev, "missing endpoint\n");
234                 goto error;
235         }
236
237         ucom->sc_parent = sc;
238         ucom->sc_portno = UCOM_UNK_PORTNO;
239         ucom->sc_ibufsize = USLCOMBUFSZ;
240         ucom->sc_obufsize = USLCOMBUFSZ;
241         ucom->sc_ibufsizepad = USLCOMBUFSZ;
242         ucom->sc_opkthdrlen = 0;
243         ucom->sc_callback = &uslcom_callback;
244
245         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
246             ucom->sc_dev);
247
248         DPRINTF(("uslcom: in = 0x%x, out = 0x%x, intr = 0x%x\n",
249                  ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
250
251         ucom_attach(&sc->sc_ucom);
252         return 0;
253
254 error:
255          ucom->sc_dying = 1;
256          return ENXIO;
257 }
258
259 static int
260 uslcom_detach(device_t self)
261 {
262         struct uslcom_softc *sc = device_get_softc(self);
263         int rv = 0;
264
265         DPRINTF(("uslcom_detach: sc=%p\n", sc));
266         sc->sc_ucom.sc_dying = 1;
267         rv = ucom_detach(&sc->sc_ucom);
268         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ucom.sc_udev,
269                            sc->sc_ucom.sc_dev);
270
271         return (rv);
272 }
273
274 #if 0 /* not yet */
275 int
276 uslcom_activate(struct device *self, enum devact act)
277 {
278         struct uslcom_softc *sc = (struct uslcom_softc *)self;
279         int rv = 0;
280
281         switch (act) {
282         case DVACT_ACTIVATE:
283                 break;
284
285         case DVACT_DEACTIVATE:
286                 if (sc->sc_subdev != NULL)
287                         rv = config_deactivate(sc->sc_subdev);
288                 sc->sc_dying = 1;
289                 break;
290         }
291         return (rv);
292 }
293 #endif
294
295 static int
296 uslcom_open(void *vsc, int portno)
297 {
298         struct uslcom_softc *sc = vsc;
299         usb_device_request_t req;
300         usbd_status err;
301
302         if (sc->sc_ucom.sc_dying)
303                 return (EIO);
304
305         req.bmRequestType = USLCOM_WRITE;
306         req.bRequest = USLCOM_UART;
307         USETW(req.wValue, USLCOM_UART_ENABLE);
308         USETW(req.wIndex, portno);
309         USETW(req.wLength, 0);
310         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
311         if (err)
312                 return (EIO);
313
314         return (0);
315 }
316
317 static void
318 uslcom_close(void *vsc, int portno)
319 {
320         struct uslcom_softc *sc = vsc;
321         usb_device_request_t req;
322
323         if (sc->sc_ucom.sc_dying)
324                 return;
325
326         req.bmRequestType = USLCOM_WRITE;
327         req.bRequest = USLCOM_UART;
328         USETW(req.wValue, USLCOM_UART_DISABLE);
329         USETW(req.wIndex, portno);
330         USETW(req.wLength, 0);
331         usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
332 }
333
334 static void
335 uslcom_set(void *vsc, int portno, int reg, int onoff)
336 {
337         struct uslcom_softc *sc = vsc;
338         usb_device_request_t req;
339         int ctl;
340
341         switch (reg) {
342         case UCOM_SET_DTR:
343                 ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
344                 ctl |= USLCOM_CTRL_DTR_SET;
345                 break;
346         case UCOM_SET_RTS:
347                 ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
348                 ctl |= USLCOM_CTRL_RTS_SET;
349                 break;
350         case UCOM_SET_BREAK:
351                 uslcom_break(sc, portno, onoff);
352                 return;
353         default:
354                 return;
355         }
356         req.bmRequestType = USLCOM_WRITE;
357         req.bRequest = USLCOM_CTRL;
358         USETW(req.wValue, ctl);
359         USETW(req.wIndex, portno);
360         USETW(req.wLength, 0);
361         usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
362 }
363
364 static int
365 uslcom_param(void *vsc, int portno, struct termios *t)
366 {
367         struct uslcom_softc *sc = (struct uslcom_softc *)vsc;
368         usbd_status err;
369         usb_device_request_t req;
370         int data;
371
372         switch (t->c_ospeed) {
373         case 600:
374         case 1200:
375         case 1800:
376         case 2400:
377         case 4800:
378         case 9600:
379         case 19200:
380         case 38400:
381         case 57600:
382         case 115200:
383         case 460800:
384         case 921600:
385                 req.bmRequestType = USLCOM_WRITE;
386                 req.bRequest = USLCOM_BAUD_RATE;
387                 USETW(req.wValue, USLCOM_BAUD_REF / t->c_ospeed);
388                 USETW(req.wIndex, portno);
389                 USETW(req.wLength, 0);
390                 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
391                 if (err)
392                         return (EIO);
393                 break;
394         default:
395                 return (EINVAL);
396         }
397
398         if (ISSET(t->c_cflag, CSTOPB))
399                 data = USLCOM_STOP_BITS_2;
400         else
401                 data = USLCOM_STOP_BITS_1;
402         if (ISSET(t->c_cflag, PARENB)) {
403                 if (ISSET(t->c_cflag, PARODD))
404                         data |= USLCOM_PARITY_ODD;
405                 else
406                         data |= USLCOM_PARITY_EVEN;
407         } else
408                 data |= USLCOM_PARITY_NONE;
409         switch (ISSET(t->c_cflag, CSIZE)) {
410         case CS5:
411                 data |= USLCOM_SET_DATA_BITS(5);
412                 break;
413         case CS6:
414                 data |= USLCOM_SET_DATA_BITS(6);
415                 break;
416         case CS7:
417                 data |= USLCOM_SET_DATA_BITS(7);
418                 break;
419         case CS8:
420                 data |= USLCOM_SET_DATA_BITS(8);
421                 break;
422         }
423
424         req.bmRequestType = USLCOM_WRITE;
425         req.bRequest = USLCOM_DATA;
426         USETW(req.wValue, data);
427         USETW(req.wIndex, portno);
428         USETW(req.wLength, 0);
429         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
430         if (err)
431                 return (EIO);
432
433         uslcom_set_flow_ctrl(sc, t->c_cflag, t->c_iflag);
434
435         return (0);
436 }
437
438 static void
439 uslcom_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
440 {
441         struct uslcom_softc *sc = vsc;
442         
443         if (msr != NULL)
444                 *msr = sc->sc_msr;
445         if (lsr != NULL)
446                 *lsr = sc->sc_lsr;
447 }
448
449 static void
450 uslcom_break(void *vsc, int portno, int onoff)
451 {
452         struct uslcom_softc *sc = vsc;
453         usb_device_request_t req;
454         int brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;   
455
456         req.bmRequestType = USLCOM_WRITE;
457         req.bRequest = USLCOM_BREAK;
458         USETW(req.wValue, brk);
459         USETW(req.wIndex, portno);
460         USETW(req.wLength, 0);
461         usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
462 }
463
464 static void
465 uslcom_set_flow_ctrl(struct uslcom_softc *sc, tcflag_t cflag, tcflag_t iflag)
466 {
467         uint8_t modemdata[16];
468         usb_device_request_t req;
469         usbd_status err;
470
471         req.bmRequestType = USLCOM_READ;
472         req.bRequest = USLCOM_MODEM;
473         USETW(req.wValue, 0);
474         USETW(req.wIndex, 0);
475         USETW(req.wLength, 16);
476
477         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, modemdata);
478         if (err)
479                 device_printf(sc->sc_ucom.sc_dev, "uslcom_set_flow: %s\n",
480                               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                 device_printf(sc->sc_ucom.sc_dev, "uslcom_set_flow: %s\n",
501                               usbd_errstr(err));
502 }
503