Make functions static.
[dragonfly.git] / sys / dev / usbmisc / uslcom / uslcom.c
1 /*      $DragonFly: src/sys/dev/usbmisc/uslcom/uslcom.c,v 1.4 2007/08/17 06:40:19 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         const char *devname;
184         int i;
185
186         devinfo = kmalloc(1024, M_USBDEV, M_INTWAIT);
187         ucom = &sc->sc_ucom;
188
189         bzero(sc, sizeof (struct uslcom_softc));
190
191         usbd_devinfo(uaa->device, 0, devinfo);
192         ucom->sc_dev = self;
193         device_set_desc_copy(self, devinfo);
194
195         ucom->sc_udev = uaa->device;
196         ucom->sc_iface = uaa->iface;
197
198         devname = device_get_nameunit(ucom->sc_dev);
199         kprintf("%s: %s\n", devname, devinfo);
200         kfree(devinfo, M_USBDEV);
201
202         if (usbd_set_config_index(ucom->sc_udev, USLCOM_CONFIG_NO, 1) != 0) {
203                 kprintf("%s: could not set configuration no\n", devname);
204                 goto error;
205         }
206
207         /* get the first interface handle */
208         error = usbd_device2interface_handle(ucom->sc_udev, USLCOM_IFACE_NO,
209             &ucom->sc_iface);
210         if (error != 0) {
211                 kprintf("%s: could not get interface handle\n", devname);
212                 goto error;
213         }
214
215         id = usbd_get_interface_descriptor(ucom->sc_iface);
216
217         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
218         for (i = 0; i < id->bNumEndpoints; i++) {
219                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
220                 if (ed == NULL) {
221                         kprintf("%s: no endpoint descriptor found for %d\n",
222                             devname, i);
223                         goto error;
224                 }
225
226                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
227                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
228                         ucom->sc_bulkin_no = ed->bEndpointAddress;
229                 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
230                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
231                         ucom->sc_bulkout_no = ed->bEndpointAddress;
232         }
233
234         if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
235                 kprintf("%s: missing endpoint\n", devname);
236                 goto error;
237         }
238
239         ucom->sc_parent = sc;
240         ucom->sc_portno = UCOM_UNK_PORTNO;
241         ucom->sc_ibufsize = USLCOMBUFSZ;
242         ucom->sc_obufsize = USLCOMBUFSZ;
243         ucom->sc_ibufsizepad = USLCOMBUFSZ;
244         ucom->sc_opkthdrlen = 0;
245         ucom->sc_callback = &uslcom_callback;
246
247         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
248             ucom->sc_dev);
249
250         DPRINTF(("uslcom: in = 0x%x, out = 0x%x, intr = 0x%x\n",
251                  ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
252
253         ucom_attach(&sc->sc_ucom);
254         return 0;
255
256 error:
257          ucom->sc_dying = 1;
258          return ENXIO;
259 }
260
261 static int
262 uslcom_detach(device_t self)
263 {
264         struct uslcom_softc *sc = device_get_softc(self);
265         int rv = 0;
266
267         DPRINTF(("uslcom_detach: sc=%p\n", sc));
268         sc->sc_ucom.sc_dying = 1;
269         rv = ucom_detach(&sc->sc_ucom);
270         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ucom.sc_udev,
271                            sc->sc_ucom.sc_dev);
272
273         return (rv);
274 }
275
276 #if 0 /* not yet */
277 int
278 uslcom_activate(struct device *self, enum devact act)
279 {
280         struct uslcom_softc *sc = (struct uslcom_softc *)self;
281         int rv = 0;
282
283         switch (act) {
284         case DVACT_ACTIVATE:
285                 break;
286
287         case DVACT_DEACTIVATE:
288                 if (sc->sc_subdev != NULL)
289                         rv = config_deactivate(sc->sc_subdev);
290                 sc->sc_dying = 1;
291                 break;
292         }
293         return (rv);
294 }
295 #endif
296
297 static int
298 uslcom_open(void *vsc, int portno)
299 {
300         struct uslcom_softc *sc = vsc;
301         usb_device_request_t req;
302         usbd_status err;
303
304         if (sc->sc_ucom.sc_dying)
305                 return (EIO);
306
307         req.bmRequestType = USLCOM_WRITE;
308         req.bRequest = USLCOM_UART;
309         USETW(req.wValue, USLCOM_UART_ENABLE);
310         USETW(req.wIndex, portno);
311         USETW(req.wLength, 0);
312         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
313         if (err)
314                 return (EIO);
315
316         return (0);
317 }
318
319 static void
320 uslcom_close(void *vsc, int portno)
321 {
322         struct uslcom_softc *sc = vsc;
323         usb_device_request_t req;
324
325         if (sc->sc_ucom.sc_dying)
326                 return;
327
328         req.bmRequestType = USLCOM_WRITE;
329         req.bRequest = USLCOM_UART;
330         USETW(req.wValue, USLCOM_UART_DISABLE);
331         USETW(req.wIndex, portno);
332         USETW(req.wLength, 0);
333         usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
334 }
335
336 static void
337 uslcom_set(void *vsc, int portno, int reg, int onoff)
338 {
339         struct uslcom_softc *sc = vsc;
340         usb_device_request_t req;
341         int ctl;
342
343         switch (reg) {
344         case UCOM_SET_DTR:
345                 ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
346                 ctl |= USLCOM_CTRL_DTR_SET;
347                 break;
348         case UCOM_SET_RTS:
349                 ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
350                 ctl |= USLCOM_CTRL_RTS_SET;
351                 break;
352         case UCOM_SET_BREAK:
353                 uslcom_break(sc, portno, onoff);
354                 return;
355         default:
356                 return;
357         }
358         req.bmRequestType = USLCOM_WRITE;
359         req.bRequest = USLCOM_CTRL;
360         USETW(req.wValue, ctl);
361         USETW(req.wIndex, portno);
362         USETW(req.wLength, 0);
363         usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
364 }
365
366 static int
367 uslcom_param(void *vsc, int portno, struct termios *t)
368 {
369         struct uslcom_softc *sc = (struct uslcom_softc *)vsc;
370         usbd_status err;
371         usb_device_request_t req;
372         int data;
373
374         switch (t->c_ospeed) {
375         case 600:
376         case 1200:
377         case 1800:
378         case 2400:
379         case 4800:
380         case 9600:
381         case 19200:
382         case 38400:
383         case 57600:
384         case 115200:
385         case 460800:
386         case 921600:
387                 req.bmRequestType = USLCOM_WRITE;
388                 req.bRequest = USLCOM_BAUD_RATE;
389                 USETW(req.wValue, USLCOM_BAUD_REF / t->c_ospeed);
390                 USETW(req.wIndex, portno);
391                 USETW(req.wLength, 0);
392                 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
393                 if (err)
394                         return (EIO);
395                 break;
396         default:
397                 return (EINVAL);
398         }
399
400         if (ISSET(t->c_cflag, CSTOPB))
401                 data = USLCOM_STOP_BITS_2;
402         else
403                 data = USLCOM_STOP_BITS_1;
404         if (ISSET(t->c_cflag, PARENB)) {
405                 if (ISSET(t->c_cflag, PARODD))
406                         data |= USLCOM_PARITY_ODD;
407                 else
408                         data |= USLCOM_PARITY_EVEN;
409         } else
410                 data |= USLCOM_PARITY_NONE;
411         switch (ISSET(t->c_cflag, CSIZE)) {
412         case CS5:
413                 data |= USLCOM_SET_DATA_BITS(5);
414                 break;
415         case CS6:
416                 data |= USLCOM_SET_DATA_BITS(6);
417                 break;
418         case CS7:
419                 data |= USLCOM_SET_DATA_BITS(7);
420                 break;
421         case CS8:
422                 data |= USLCOM_SET_DATA_BITS(8);
423                 break;
424         }
425
426         req.bmRequestType = USLCOM_WRITE;
427         req.bRequest = USLCOM_DATA;
428         USETW(req.wValue, data);
429         USETW(req.wIndex, portno);
430         USETW(req.wLength, 0);
431         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
432         if (err)
433                 return (EIO);
434
435         uslcom_set_flow_ctrl(sc, t->c_cflag, t->c_iflag);
436
437         return (0);
438 }
439
440 static void
441 uslcom_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
442 {
443         struct uslcom_softc *sc = vsc;
444         
445         if (msr != NULL)
446                 *msr = sc->sc_msr;
447         if (lsr != NULL)
448                 *lsr = sc->sc_lsr;
449 }
450
451 static void
452 uslcom_break(void *vsc, int portno, int onoff)
453 {
454         struct uslcom_softc *sc = vsc;
455         usb_device_request_t req;
456         int brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;   
457
458         req.bmRequestType = USLCOM_WRITE;
459         req.bRequest = USLCOM_BREAK;
460         USETW(req.wValue, brk);
461         USETW(req.wIndex, portno);
462         USETW(req.wLength, 0);
463         usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
464 }
465
466 static void
467 uslcom_set_flow_ctrl(struct uslcom_softc *sc, tcflag_t cflag, tcflag_t iflag)
468 {
469         uint8_t modemdata[16];
470         usb_device_request_t req;
471         usbd_status err;
472
473         req.bmRequestType = USLCOM_READ;
474         req.bRequest = USLCOM_MODEM;
475         USETW(req.wValue, 0);
476         USETW(req.wIndex, 0);
477         USETW(req.wLength, 16);
478
479         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, modemdata);
480         if (err)
481                 kprintf("uslcom_set_flow: %s\n", usbd_errstr(err));
482
483         if (ISSET(cflag, CRTSCTS)) {
484                 modemdata[0] &= ~0x7b;
485                 modemdata[0] |= 0x09;
486                 modemdata[4] = 0x80;
487         } else {
488                 modemdata[0] &= ~0x7b;
489                 modemdata[0] |= 0x01;
490                 modemdata[4] = 0x40;
491         }
492
493         req.bmRequestType = USLCOM_WRITE;
494         req.bRequest = USLCOM_MODEM;
495         USETW(req.wValue, 0);
496         USETW(req.wIndex, 0);
497         USETW(req.wLength, 16);
498
499         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, modemdata);
500         if (err)
501                 kprintf("uslcom_set_flow: %s\n", usbd_errstr(err));
502 }
503