Nuke usbdevs and references to it.
[dragonfly.git] / sys / dev / usbmisc / uslcom / uslcom.c
1 /*      $DragonFly: src/sys/dev/usbmisc/uslcom/uslcom.c,v 1.7 2007/11/05 19:09:44 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
34 #include <dev/usbmisc/ucom/ucomvar.h>
35
36 #ifdef USLCOM_DEBUG
37 #define DPRINTFN(n, x)  do { if (uslcomdebug > (n)) kprintf x; } while (0)
38 int     uslcomdebug = 0;
39 #else
40 #define DPRINTFN(n, x)
41 #endif
42 #define DPRINTF(x) DPRINTFN(0, x)
43
44 #define USLCOMBUFSZ             256
45 #define USLCOM_CONFIG_NO        0
46 #define USLCOM_IFACE_NO         0
47
48 #define USLCOM_SET_DATA_BITS(x) (x << 8)
49
50 #define USLCOM_WRITE            0x41
51 #define USLCOM_READ             0xc1
52
53 #define USLCOM_UART             0x00
54 #define USLCOM_BAUD_RATE        0x01    
55 #define USLCOM_DATA             0x03
56 #define USLCOM_BREAK            0x05
57 #define USLCOM_CTRL             0x07
58 #define USLCOM_MODEM            0x13
59
60 #define USLCOM_UART_DISABLE     0x00
61 #define USLCOM_UART_ENABLE      0x01
62
63 #define USLCOM_CTRL_DTR_ON      0x0001  
64 #define USLCOM_CTRL_DTR_SET     0x0100
65 #define USLCOM_CTRL_RTS_ON      0x0002
66 #define USLCOM_CTRL_RTS_SET     0x0200
67 #define USLCOM_CTRL_CTS         0x0010
68 #define USLCOM_CTRL_DSR         0x0020
69 #define USLCOM_CTRL_DCD         0x0080
70
71
72 #define USLCOM_BAUD_REF         0x384000
73
74 #define USLCOM_STOP_BITS_1      0x00
75 #define USLCOM_STOP_BITS_2      0x02
76
77 #define USLCOM_PARITY_NONE      0x00
78 #define USLCOM_PARITY_ODD       0x10
79 #define USLCOM_PARITY_EVEN      0x20
80
81 #define USLCOM_BREAK_OFF        0x00
82 #define USLCOM_BREAK_ON         0x01
83
84
85 struct uslcom_softc {
86         struct ucom_softc       sc_ucom;
87         u_char                  sc_msr;
88         u_char                  sc_lsr;
89 };
90
91 static void     uslcom_get_status(void *, int portno, u_char *lsr,
92                                   u_char *msr);
93 static void     uslcom_set(void *, int, int, int);
94 static int      uslcom_param(void *, int, struct termios *);
95 static int      uslcom_open(void *sc, int portno);
96 static void     uslcom_close(void *, int);
97 static void     uslcom_break(void *sc, int portno, int onoff);
98 static 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_DEVICE(0x0fcf, 0x1003) }, /* ANT development board */
114         { USB_DEVICE(0x10a6, 0xaa26) }, /* Noname DCU-11 clone */
115         { USB_DEVICE(0x10ab, 0x10c5) }, /* USI MC60 */
116         { USB_DEVICE(0x10b5, 0xac70) }, /* PLX CA-42 */
117         { USB_DEVICE(0x10c4, 0x803b) }, /* Pololu Serial */
118         { USB_DEVICE(0x10c4, 0x8053) }, /* Enfora EDG1228 */
119         { USB_DEVICE(0x10c4, 0x8066) }, /* Argussoft In-System Programmer */
120         { USB_DEVICE(0x10c4, 0x807a) }, /* Crumb128 board */
121         { USB_DEVICE(0x10c4, 0x80ca) }, /* Degree Controls */
122         { USB_DEVICE(0x10c4, 0x80dd) }, /* Tracient RFID */
123         { USB_DEVICE(0x10c4, 0x80f6) }, /* Suunto sports */
124         { USB_DEVICE(0x10c4, 0x813d) }, /* Burnside Desktop mobile */
125         { USB_DEVICE(0x10c4, 0x814a) }, /* West Mountain Radio RIGblaster */
126         { USB_DEVICE(0x10c4, 0x814b) }, /* West Mountain Radio RIGtalk */
127         { USB_DEVICE(0x10c4, 0x815e) }, /* IP-Link 1220 */
128         { USB_DEVICE(0x10c4, 0x81c8) }, /* Lipowsky Baby-JTAG */
129         { USB_DEVICE(0x10c4, 0x81e2) }, /* Lipowsky Baby-LIN */
130         { USB_DEVICE(0x10c4, 0x8218) }, /* Lipowsky HARP-1 */
131         { USB_DEVICE(0x10c4, 0xea60) }, /* Silicon Labs CP210x */
132         { USB_DEVICE(0x10c4, 0xea61) }, /* Silicon Labs CP210x */
133         { USB_DEVICE(0x13ad, 0x9999) }, /* Baltech card reader */
134         { USB_DEVICE(0x16d6, 0x0001) }, /* Jablotron PC-60B */
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         int i;
183
184         devinfo = kmalloc(1024, M_USBDEV, M_INTWAIT);
185         ucom = &sc->sc_ucom;
186
187         bzero(sc, sizeof (struct uslcom_softc));
188
189         usbd_devinfo(uaa->device, 0, devinfo);
190         ucom->sc_dev = self;
191         device_set_desc_copy(self, devinfo);
192
193         ucom->sc_udev = uaa->device;
194         ucom->sc_iface = uaa->iface;
195
196         device_printf(ucom->sc_dev, "%s\n", devinfo);
197         kfree(devinfo, M_USBDEV);
198
199         if (usbd_set_config_index(ucom->sc_udev, USLCOM_CONFIG_NO, 1) != 0) {
200                 device_printf(ucom->sc_dev, "could not set configuration no\n");
201                 goto error;
202         }
203
204         /* get the first interface handle */
205         error = usbd_device2interface_handle(ucom->sc_udev, USLCOM_IFACE_NO,
206             &ucom->sc_iface);
207         if (error != 0) {
208                 device_printf(ucom->sc_dev, "could not get interface handle\n");
209                 goto error;
210         }
211
212         id = usbd_get_interface_descriptor(ucom->sc_iface);
213
214         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
215         for (i = 0; i < id->bNumEndpoints; i++) {
216                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
217                 if (ed == NULL) {
218                         device_printf(ucom->sc_dev, "no endpoint descriptor "
219                                       "found for %d\n", i);
220                         goto error;
221                 }
222
223                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
224                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
225                         ucom->sc_bulkin_no = ed->bEndpointAddress;
226                 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
227                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
228                         ucom->sc_bulkout_no = ed->bEndpointAddress;
229         }
230
231         if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
232                 device_printf(ucom->sc_dev, "missing endpoint\n");
233                 goto error;
234         }
235
236         ucom->sc_parent = sc;
237         ucom->sc_portno = UCOM_UNK_PORTNO;
238         ucom->sc_ibufsize = USLCOMBUFSZ;
239         ucom->sc_obufsize = USLCOMBUFSZ;
240         ucom->sc_ibufsizepad = USLCOMBUFSZ;
241         ucom->sc_opkthdrlen = 0;
242         ucom->sc_callback = &uslcom_callback;
243
244         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
245             ucom->sc_dev);
246
247         DPRINTF(("uslcom: in = 0x%x, out = 0x%x, intr = 0x%x\n",
248                  ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
249
250         ucom_attach(&sc->sc_ucom);
251         return 0;
252
253 error:
254          ucom->sc_dying = 1;
255          return ENXIO;
256 }
257
258 static int
259 uslcom_detach(device_t self)
260 {
261         struct uslcom_softc *sc = device_get_softc(self);
262         int rv = 0;
263
264         DPRINTF(("uslcom_detach: sc=%p\n", sc));
265         sc->sc_ucom.sc_dying = 1;
266         rv = ucom_detach(&sc->sc_ucom);
267         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ucom.sc_udev,
268                            sc->sc_ucom.sc_dev);
269
270         return (rv);
271 }
272
273 #if 0 /* not yet */
274 int
275 uslcom_activate(struct device *self, enum devact act)
276 {
277         struct uslcom_softc *sc = (struct uslcom_softc *)self;
278         int rv = 0;
279
280         switch (act) {
281         case DVACT_ACTIVATE:
282                 break;
283
284         case DVACT_DEACTIVATE:
285                 if (sc->sc_subdev != NULL)
286                         rv = config_deactivate(sc->sc_subdev);
287                 sc->sc_dying = 1;
288                 break;
289         }
290         return (rv);
291 }
292 #endif
293
294 static int
295 uslcom_open(void *vsc, int portno)
296 {
297         struct uslcom_softc *sc = vsc;
298         usb_device_request_t req;
299         usbd_status err;
300
301         if (sc->sc_ucom.sc_dying)
302                 return (EIO);
303
304         req.bmRequestType = USLCOM_WRITE;
305         req.bRequest = USLCOM_UART;
306         USETW(req.wValue, USLCOM_UART_ENABLE);
307         USETW(req.wIndex, portno);
308         USETW(req.wLength, 0);
309         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
310         if (err)
311                 return (EIO);
312
313         return (0);
314 }
315
316 static void
317 uslcom_close(void *vsc, int portno)
318 {
319         struct uslcom_softc *sc = vsc;
320         usb_device_request_t req;
321
322         if (sc->sc_ucom.sc_dying)
323                 return;
324
325         req.bmRequestType = USLCOM_WRITE;
326         req.bRequest = USLCOM_UART;
327         USETW(req.wValue, USLCOM_UART_DISABLE);
328         USETW(req.wIndex, portno);
329         USETW(req.wLength, 0);
330         usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
331 }
332
333 static void
334 uslcom_set(void *vsc, int portno, int reg, int onoff)
335 {
336         struct uslcom_softc *sc = vsc;
337         usb_device_request_t req;
338         int ctl;
339
340         switch (reg) {
341         case UCOM_SET_DTR:
342                 ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
343                 ctl |= USLCOM_CTRL_DTR_SET;
344                 break;
345         case UCOM_SET_RTS:
346                 ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
347                 ctl |= USLCOM_CTRL_RTS_SET;
348                 break;
349         case UCOM_SET_BREAK:
350                 uslcom_break(sc, portno, onoff);
351                 return;
352         default:
353                 return;
354         }
355         req.bmRequestType = USLCOM_WRITE;
356         req.bRequest = USLCOM_CTRL;
357         USETW(req.wValue, ctl);
358         USETW(req.wIndex, portno);
359         USETW(req.wLength, 0);
360         usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
361 }
362
363 static int
364 uslcom_param(void *vsc, int portno, struct termios *t)
365 {
366         struct uslcom_softc *sc = (struct uslcom_softc *)vsc;
367         usbd_status err;
368         usb_device_request_t req;
369         int data;
370
371         switch (t->c_ospeed) {
372         case 600:
373         case 1200:
374         case 1800:
375         case 2400:
376         case 4800:
377         case 9600:
378         case 19200:
379         case 38400:
380         case 57600:
381         case 115200:
382         case 460800:
383         case 921600:
384                 req.bmRequestType = USLCOM_WRITE;
385                 req.bRequest = USLCOM_BAUD_RATE;
386                 USETW(req.wValue, USLCOM_BAUD_REF / t->c_ospeed);
387                 USETW(req.wIndex, portno);
388                 USETW(req.wLength, 0);
389                 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
390                 if (err)
391                         return (EIO);
392                 break;
393         default:
394                 return (EINVAL);
395         }
396
397         if (ISSET(t->c_cflag, CSTOPB))
398                 data = USLCOM_STOP_BITS_2;
399         else
400                 data = USLCOM_STOP_BITS_1;
401         if (ISSET(t->c_cflag, PARENB)) {
402                 if (ISSET(t->c_cflag, PARODD))
403                         data |= USLCOM_PARITY_ODD;
404                 else
405                         data |= USLCOM_PARITY_EVEN;
406         } else
407                 data |= USLCOM_PARITY_NONE;
408         switch (ISSET(t->c_cflag, CSIZE)) {
409         case CS5:
410                 data |= USLCOM_SET_DATA_BITS(5);
411                 break;
412         case CS6:
413                 data |= USLCOM_SET_DATA_BITS(6);
414                 break;
415         case CS7:
416                 data |= USLCOM_SET_DATA_BITS(7);
417                 break;
418         case CS8:
419                 data |= USLCOM_SET_DATA_BITS(8);
420                 break;
421         }
422
423         req.bmRequestType = USLCOM_WRITE;
424         req.bRequest = USLCOM_DATA;
425         USETW(req.wValue, data);
426         USETW(req.wIndex, portno);
427         USETW(req.wLength, 0);
428         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
429         if (err)
430                 return (EIO);
431
432         uslcom_set_flow_ctrl(sc, t->c_cflag, t->c_iflag);
433
434         return (0);
435 }
436
437 static void
438 uslcom_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
439 {
440         struct uslcom_softc *sc = vsc;
441         
442         if (msr != NULL)
443                 *msr = sc->sc_msr;
444         if (lsr != NULL)
445                 *lsr = sc->sc_lsr;
446 }
447
448 static void
449 uslcom_break(void *vsc, int portno, int onoff)
450 {
451         struct uslcom_softc *sc = vsc;
452         usb_device_request_t req;
453         int brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;   
454
455         req.bmRequestType = USLCOM_WRITE;
456         req.bRequest = USLCOM_BREAK;
457         USETW(req.wValue, brk);
458         USETW(req.wIndex, portno);
459         USETW(req.wLength, 0);
460         usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
461 }
462
463 static void
464 uslcom_set_flow_ctrl(struct uslcom_softc *sc, tcflag_t cflag, tcflag_t iflag)
465 {
466         uint8_t modemdata[16];
467         usb_device_request_t req;
468         usbd_status err;
469
470         req.bmRequestType = USLCOM_READ;
471         req.bRequest = USLCOM_MODEM;
472         USETW(req.wValue, 0);
473         USETW(req.wIndex, 0);
474         USETW(req.wLength, 16);
475
476         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, modemdata);
477         if (err)
478                 device_printf(sc->sc_ucom.sc_dev, "uslcom_set_flow: %s\n",
479                               usbd_errstr(err));
480
481         if (ISSET(cflag, CRTSCTS)) {
482                 modemdata[0] &= ~0x7b;
483                 modemdata[0] |= 0x09;
484                 modemdata[4] = 0x80;
485         } else {
486                 modemdata[0] &= ~0x7b;
487                 modemdata[0] |= 0x01;
488                 modemdata[4] = 0x40;
489         }
490
491         req.bmRequestType = USLCOM_WRITE;
492         req.bRequest = USLCOM_MODEM;
493         USETW(req.wValue, 0);
494         USETW(req.wIndex, 0);
495         USETW(req.wLength, 16);
496
497         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, modemdata);
498         if (err)
499                 device_printf(sc->sc_ucom.sc_dev, "uslcom_set_flow: %s\n",
500                               usbd_errstr(err));
501 }
502