]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/usb/uslcom.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / usb / uslcom.c
1 /*      $FreeBSD$ */
2 /*      $OpenBSD: uslcom.c,v 1.17 2007/11/24 10:52:12 jsg 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/module.h>
24 #include <sys/conf.h>
25 #include <sys/bus.h>
26 #include <sys/tty.h>
27
28 #include <dev/usb/usb.h>
29 #include <dev/usb/usbcdc.h>
30
31 #include <dev/usb/usb.h>
32 #include <dev/usb/usbdi.h>
33 #include <dev/usb/usbdi_util.h>
34
35 #include "usbdevs.h"
36 #include <dev/usb/ucomvar.h>
37
38 #ifdef USLCOM_DEBUG
39 #define DPRINTFN(n, x)  do { if (uslcomdebug > (n)) printf x; } while (0)
40 int     uslcomdebug = 1;
41 #else
42 #define DPRINTFN(n, x)
43 #endif
44 #define DPRINTF(x) DPRINTFN(0, x)
45
46 #define USLCOMBUFSZ             256
47 #define USLCOM_CONFIG_NO        0
48 #define USLCOM_IFACE_NO         0
49
50 #define USLCOM_SET_DATA_BITS(x) (x << 8)
51
52 #define USLCOM_WRITE            0x41
53 #define USLCOM_READ             0xc1
54
55 #define USLCOM_UART             0x00
56 #define USLCOM_BAUD_RATE        0x01    
57 #define USLCOM_DATA             0x03
58 #define USLCOM_BREAK            0x05
59 #define USLCOM_CTRL             0x07
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         device_t                 sc_dev;
89         usbd_device_handle       sc_udev;
90
91         u_char                   sc_msr;
92         u_char                   sc_lsr;
93
94         u_char                   sc_dying;
95 };
96
97 void    uslcom_get_status(void *, int portno, u_char *lsr, u_char *msr);
98 void    uslcom_set(void *, int, int, int);
99 int     uslcom_param(void *, int, struct termios *);
100 int     uslcom_open(void *sc, int portno);
101 void    uslcom_close(void *, int);
102 void    uslcom_break(void *sc, int portno, int onoff);
103
104 struct ucom_callback uslcom_callback = {
105         uslcom_get_status,
106         uslcom_set,
107         uslcom_param,
108         NULL,
109         uslcom_open,
110         uslcom_close,
111         NULL,
112         NULL,
113 };
114
115 static const struct usb_devno uslcom_devs[] = {
116         { USB_VENDOR_BALTECH,           USB_PRODUCT_BALTECH_CARDREADER },
117         { USB_VENDOR_DYNASTREAM,        USB_PRODUCT_DYNASTREAM_ANTDEVBOARD },
118         { USB_VENDOR_JABLOTRON,         USB_PRODUCT_JABLOTRON_PC60B },
119         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_ARGUSISP },
120         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CRUMB128 },
121         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_DEGREE },
122         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_BURNSIDE },
123         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_HELICOM },
124         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_HARP },
125         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_JTAG },
126         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_LIN },
127         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_POLOLU },
128         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CP2102 },
129         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CP210X_2 },
130         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_SUUNTO },
131         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_TRAQMATE },
132         { USB_VENDOR_SILABS2,           USB_PRODUCT_SILABS2_DCU11CLONE },
133         { USB_VENDOR_USI,               USB_PRODUCT_USI_MC60 }
134 };
135
136 static device_probe_t uslcom_match;
137 static device_attach_t uslcom_attach;
138 static device_detach_t uslcom_detach;
139
140 static device_method_t uslcom_methods[] = {
141         /* Device interface */
142         DEVMETHOD(device_probe, uslcom_match),
143         DEVMETHOD(device_attach, uslcom_attach),
144         DEVMETHOD(device_detach, uslcom_detach),
145         { 0, 0 }
146 };
147
148 static driver_t uslcom_driver = {
149         "ucom",
150         uslcom_methods,
151         sizeof (struct uslcom_softc)
152 };
153
154 DRIVER_MODULE(uslcom, uhub, uslcom_driver, ucom_devclass, usbd_driver_load, 0);
155 MODULE_DEPEND(uslcom, usb, 1, 1, 1);
156 MODULE_DEPEND(uslcom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
157 MODULE_VERSION(uslcom, 1);
158
159 static int
160 uslcom_match(device_t self)
161 {
162         struct usb_attach_arg *uaa = device_get_ivars(self);
163
164         if (uaa->iface != NULL)
165                 return UMATCH_NONE;
166
167         return (usb_lookup(uslcom_devs, uaa->vendor, uaa->product) != NULL) ?
168             UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
169 }
170
171 static int
172 uslcom_attach(device_t self)
173 {
174         struct uslcom_softc *sc = device_get_softc(self);
175         struct usb_attach_arg *uaa = device_get_ivars(self);
176         usbd_device_handle dev = uaa->device;
177         struct ucom_softc* ucom;
178         usb_interface_descriptor_t *id;
179         usb_endpoint_descriptor_t *ed;
180         usbd_status error;
181         int i;
182
183         ucom = &sc->sc_ucom;
184         ucom->sc_dev = self;
185         ucom->sc_udev = dev;
186         ucom->sc_iface = uaa->iface;
187         
188         sc->sc_dev = self;
189         sc->sc_udev = uaa->device;
190
191         if (usbd_set_config_index(sc->sc_udev, USLCOM_CONFIG_NO, 1) != 0) {
192                 device_printf(self, "could not set configuration no\n");
193                 sc->sc_dying = 1;
194                 return ENXIO;
195         }
196
197         /* get the first interface handle */
198         error = usbd_device2interface_handle(sc->sc_udev, USLCOM_IFACE_NO,
199             &ucom->sc_iface);
200         if (error != 0) {
201                 device_printf(self, "could not get interface handle\n");
202                 sc->sc_dying = 1;
203                 return ENXIO;
204         }
205
206         id = usbd_get_interface_descriptor(ucom->sc_iface);
207
208         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
209         for (i = 0; i < id->bNumEndpoints; i++) {
210                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
211                 if (ed == NULL) {
212                         device_printf(self, "no endpoint descriptor found for %d\n",
213                             i);
214                         sc->sc_dying = 1;
215                         return ENXIO;
216                 }
217
218                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
219                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
220                         ucom->sc_bulkin_no = ed->bEndpointAddress;
221                 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
222                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
223                         ucom->sc_bulkout_no = ed->bEndpointAddress;
224         }
225
226         if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
227                 device_printf(self, "missing endpoint\n");
228                 sc->sc_dying = 1;
229                 return ENXIO;
230         }
231
232         ucom->sc_parent = sc;
233         ucom->sc_portno = UCOM_UNK_PORTNO;
234         /* bulkin, bulkout set above */
235         ucom->sc_ibufsize = USLCOMBUFSZ;
236         ucom->sc_obufsize = USLCOMBUFSZ;
237         ucom->sc_ibufsizepad = USLCOMBUFSZ;
238         ucom->sc_opkthdrlen = 0;
239         ucom->sc_callback = &uslcom_callback;
240
241         DPRINTF(("uslcom: in = 0x%x, out = 0x%x\n",
242                  ucom->sc_bulkin_no, ucom->sc_bulkout_no));
243
244         ucom_attach(&sc->sc_ucom);
245         return 0;
246 }
247
248 static int
249 uslcom_detach(device_t self)
250 {
251         struct uslcom_softc *sc = device_get_softc(self);
252
253         sc->sc_dying = 1;
254         return ucom_detach(&sc->sc_ucom);
255 }
256
257 int
258 uslcom_open(void *vsc, int portno)
259 {
260         struct uslcom_softc *sc = vsc;
261         usb_device_request_t req;
262         usbd_status err;
263
264         if (sc->sc_dying)
265                 return (EIO);
266
267         req.bmRequestType = USLCOM_WRITE;
268         req.bRequest = USLCOM_UART;
269         USETW(req.wValue, USLCOM_UART_ENABLE);
270         USETW(req.wIndex, portno);
271         USETW(req.wLength, 0);
272         err = usbd_do_request(sc->sc_udev, &req, NULL);
273         if (err)
274                 return (EIO);
275
276         return (0);
277 }
278
279 void
280 uslcom_close(void *vsc, int portno)
281 {
282         struct uslcom_softc *sc = vsc;
283         usb_device_request_t req;
284
285         if (sc->sc_dying)
286                 return;
287
288         req.bmRequestType = USLCOM_WRITE;
289         req.bRequest = USLCOM_UART;
290         USETW(req.wValue, USLCOM_UART_DISABLE);
291         USETW(req.wIndex, portno);
292         USETW(req.wLength, 0);
293         usbd_do_request(sc->sc_udev, &req, NULL);
294 }
295
296 void
297 uslcom_set(void *vsc, int portno, int reg, int onoff)
298 {
299         struct uslcom_softc *sc = vsc;
300         usb_device_request_t req;
301         int ctl;
302
303         switch (reg) {
304         case UCOM_SET_DTR:
305                 ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
306                 ctl |= USLCOM_CTRL_DTR_SET;
307                 break;
308         case UCOM_SET_RTS:
309                 ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
310                 ctl |= USLCOM_CTRL_RTS_SET;
311                 break;
312         case UCOM_SET_BREAK:
313                 uslcom_break(sc, portno, onoff);
314                 return;
315         default:
316                 return;
317         }
318         req.bmRequestType = USLCOM_WRITE;
319         req.bRequest = USLCOM_CTRL;
320         USETW(req.wValue, ctl);
321         USETW(req.wIndex, portno);
322         USETW(req.wLength, 0);
323         usbd_do_request(sc->sc_udev, &req, NULL);
324 }
325
326 int
327 uslcom_param(void *vsc, int portno, struct termios *t)
328 {
329         struct uslcom_softc *sc = (struct uslcom_softc *)vsc;
330         usbd_status err;
331         usb_device_request_t req;
332         int data;
333
334         if (t->c_ospeed <= 0 || t->c_ospeed > 921600)
335                 return (EINVAL);
336
337         req.bmRequestType = USLCOM_WRITE;
338         req.bRequest = USLCOM_BAUD_RATE;
339         USETW(req.wValue, USLCOM_BAUD_REF / t->c_ospeed);
340         USETW(req.wIndex, portno);
341         USETW(req.wLength, 0);
342         err = usbd_do_request(sc->sc_udev, &req, NULL);
343         if (err)
344                 return (EIO);
345
346         if (ISSET(t->c_cflag, CSTOPB))
347                 data = USLCOM_STOP_BITS_2;
348         else
349                 data = USLCOM_STOP_BITS_1;
350         if (ISSET(t->c_cflag, PARENB)) {
351                 if (ISSET(t->c_cflag, PARODD))
352                         data |= USLCOM_PARITY_ODD;
353                 else
354                         data |= USLCOM_PARITY_EVEN;
355         } else
356                 data |= USLCOM_PARITY_NONE;
357         switch (ISSET(t->c_cflag, CSIZE)) {
358         case CS5:
359                 data |= USLCOM_SET_DATA_BITS(5);
360                 break;
361         case CS6:
362                 data |= USLCOM_SET_DATA_BITS(6);
363                 break;
364         case CS7:
365                 data |= USLCOM_SET_DATA_BITS(7);
366                 break;
367         case CS8:
368                 data |= USLCOM_SET_DATA_BITS(8);
369                 break;
370         }
371
372         req.bmRequestType = USLCOM_WRITE;
373         req.bRequest = USLCOM_DATA;
374         USETW(req.wValue, data);
375         USETW(req.wIndex, portno);
376         USETW(req.wLength, 0);
377         err = usbd_do_request(sc->sc_udev, &req, NULL);
378         if (err)
379                 return (EIO);
380
381 #if 0
382         /* XXX flow control */
383         if (ISSET(t->c_cflag, CRTSCTS))
384                 /*  rts/cts flow ctl */
385         } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
386                 /*  xon/xoff flow ctl */
387         } else {
388                 /* disable flow ctl */
389         }
390 #endif
391
392         return (0);
393 }
394
395 void
396 uslcom_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
397 {
398         struct uslcom_softc *sc = vsc;
399         
400         if (msr != NULL)
401                 *msr = sc->sc_msr;
402         if (lsr != NULL)
403                 *lsr = sc->sc_lsr;
404 }
405
406 void
407 uslcom_break(void *vsc, int portno, int onoff)
408 {
409         struct uslcom_softc *sc = vsc;
410         usb_device_request_t req;
411         int brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;   
412
413         req.bmRequestType = USLCOM_WRITE;
414         req.bRequest = USLCOM_BREAK;
415         USETW(req.wValue, brk);
416         USETW(req.wIndex, portno);
417         USETW(req.wLength, 0);
418         usbd_do_request(sc->sc_udev, &req, NULL);
419 }