]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/usb/umct.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / usb / umct.c
1 /*-
2  * Copyright (c) 2003 Scott Long
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 /*
32  * Driver for the MCT (Magic Control Technology) USB-RS232 Converter.
33  * Based on the superb documentation from the linux mct_u232 driver by
34  * Wolfgang Grandeggar <wolfgang@cec.ch>.
35  * This device smells a lot like the Belkin F5U103, except that it has
36  * suffered some mild brain-damage.  This driver is based off of the ubsa.c
37  * driver from Alexander Kabaev <kan@freebsd.org>.  Merging the two together
38  * might be useful, though the subtle differences might lead to lots of
39  * #ifdef's.
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/bus.h>
48 #include <sys/tty.h>
49 #include <sys/taskqueue.h>
50
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
53 #include <dev/usb/usbdi_util.h>
54 #include "usbdevs.h"
55 #include <dev/usb/ucomvar.h>
56
57 /* The UMCT advertises the standard 8250 UART registers */
58 #define UMCT_GET_MSR            2       /* Get Modem Status Register */
59 #define UMCT_GET_MSR_SIZE       1
60 #define UMCT_GET_LCR            6       /* Get Line Control Register */
61 #define UMCT_GET_LCR_SIZE       1
62 #define UMCT_SET_BAUD           5       /* Set the Baud Rate Divisor */
63 #define UMCT_SET_BAUD_SIZE      4
64 #define UMCT_SET_LCR            7       /* Set Line Control Register */
65 #define UMCT_SET_LCR_SIZE       1
66 #define UMCT_SET_MCR            10      /* Set Modem Control Register */
67 #define UMCT_SET_MCR_SIZE       1
68
69 #define UMCT_INTR_INTERVAL      100
70 #define UMCT_IFACE_INDEX        0
71 #define UMCT_CONFIG_INDEX       1
72
73 struct umct_softc {
74         struct ucom_softc       sc_ucom;
75         int                     sc_iface_number;
76         usbd_interface_handle   sc_intr_iface;
77         int                     sc_intr_number;
78         usbd_pipe_handle        sc_intr_pipe;
79         u_char                  *sc_intr_buf;
80         int                     sc_isize;
81         uint8_t                 sc_lsr;
82         uint8_t                 sc_msr;
83         uint8_t                 sc_lcr;
84         uint8_t                 sc_mcr;
85         struct task             sc_task;
86 };
87
88 static void umct_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
89 static void umct_get_status(void *, int, u_char *, u_char *);
90 static void umct_set(void *, int, int, int);
91 static int  umct_param(void *, int, struct termios *);
92 static int  umct_open(void *, int);
93 static void umct_close(void *, int);
94 static void umct_notify(void *, int count);
95
96 static struct ucom_callback umct_callback = {
97         umct_get_status,        /* ucom_get_status */
98         umct_set,               /* ucom_set */
99         umct_param,             /* ucom_param */
100         NULL,                   /* ucom_ioctl */
101         umct_open,              /* ucom_open */
102         umct_close,             /* ucom_close */
103         NULL,                   /* ucom_read */
104         NULL                    /* ucom_write */
105 };
106
107 static const struct umct_product {
108         uint16_t        vendor;
109         uint16_t        product;
110 } umct_products[] = {
111         { USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232 },
112         { USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232 },
113         { USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232 },
114         { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109 },
115         { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U409 },
116         { 0, 0 }
117 };
118
119 static device_probe_t   umct_match;
120 static device_attach_t  umct_attach;
121 static device_detach_t  umct_detach;
122
123 static device_method_t umct_methods[] = {
124         DEVMETHOD(device_probe, umct_match),
125         DEVMETHOD(device_attach, umct_attach),
126         DEVMETHOD(device_detach, umct_detach),
127         { 0, 0 }
128 };
129
130 static driver_t umct_driver = {
131         "ucom",
132         umct_methods,
133         sizeof(struct umct_softc)
134 };
135
136 DRIVER_MODULE(umct, uhub, umct_driver, ucom_devclass, usbd_driver_load, 0);
137 MODULE_DEPEND(umct, usb, 1, 1, 1);
138 MODULE_DEPEND(umct, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
139 MODULE_VERSION(umct, 1);
140
141 static int
142 umct_match(device_t self)
143 {
144         struct usb_attach_arg *uaa = device_get_ivars(self);
145         int i;
146
147         if (uaa->iface != NULL)
148                 return (UMATCH_NONE);
149
150         for (i = 0; umct_products[i].vendor != 0; i++) {
151                 if (umct_products[i].vendor == uaa->vendor &&
152                     umct_products[i].product == uaa->product) {
153                         return (UMATCH_VENDOR_PRODUCT);
154                 }
155         }
156
157         return (UMATCH_NONE);
158 }
159
160 static int
161 umct_attach(device_t self)
162 {
163         struct umct_softc *sc = device_get_softc(self);
164         struct usb_attach_arg *uaa = device_get_ivars(self);
165         usbd_device_handle dev;
166         struct ucom_softc *ucom;
167         usb_config_descriptor_t *cdesc;
168         usb_interface_descriptor_t *id;
169         usb_endpoint_descriptor_t *ed;
170         const char *devname;
171         usbd_status err;
172         int i;
173
174         dev = uaa->device;
175         bzero(sc, sizeof(struct umct_softc));
176         ucom = &sc->sc_ucom;
177         ucom->sc_dev = self;
178         ucom->sc_udev = dev;
179         ucom->sc_iface = uaa->iface;
180
181         ucom->sc_bulkout_no = -1;
182         ucom->sc_bulkin_no = -1;
183         sc->sc_intr_number = -1;
184         sc->sc_intr_pipe = NULL;
185
186         devname = device_get_nameunit(ucom->sc_dev);
187
188         err = usbd_set_config_index(dev, UMCT_CONFIG_INDEX, 1);
189         if (err) {
190                 printf("%s: failed to set configuration: %s\n",
191                     devname, usbd_errstr(err));
192                 ucom->sc_dying = 1;
193                 goto error;
194         }
195
196         cdesc = usbd_get_config_descriptor(ucom->sc_udev);
197         if (cdesc == NULL) {
198                 printf("%s: failed to get configuration descriptor\n", devname);
199                 ucom->sc_dying = 1;
200                 goto error;
201         }
202
203         err = usbd_device2interface_handle(dev, UMCT_IFACE_INDEX,
204             &ucom->sc_iface);
205         if (err) {
206                 printf("%s: failed to get interface: %s\n", devname,
207                     usbd_errstr(err));
208                 ucom->sc_dying = 1;
209                 goto error;
210         }
211
212         id = usbd_get_interface_descriptor(ucom->sc_iface);
213         sc->sc_iface_number = id->bInterfaceNumber;
214
215         for (i = 0; i < id->bNumEndpoints; i++) {
216                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
217                 if (ed == NULL) {
218                         printf("%s: no endpoint descriptor for %d\n",
219                             devname, i);
220                         ucom->sc_dying = 1;
221                         goto error;
222                 }
223
224                 /*
225                  * The real bulk-in endpoint is also marked as an interrupt.
226                  * The only way to differentiate it from the real interrupt
227                  * endpoint is to look at the wMaxPacketSize field.
228                  */
229                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) {
230                         if (UGETW(ed->wMaxPacketSize) == 0x2) {
231                                 sc->sc_intr_number = ed->bEndpointAddress;
232                                 sc->sc_isize = UGETW(ed->wMaxPacketSize);
233                         } else {
234                                 ucom->sc_bulkin_no = ed->bEndpointAddress;
235                                 ucom->sc_ibufsize = UGETW(ed->wMaxPacketSize);
236                         }
237                         continue;
238                 }
239
240                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT) {
241                         ucom->sc_bulkout_no = ed->bEndpointAddress;
242                         if (uaa->product == USB_PRODUCT_MCT_SITECOM_USB232)
243                                 ucom->sc_obufsize = 16; /* device is broken */
244                         else
245                                 ucom->sc_obufsize = UGETW(ed->wMaxPacketSize);
246                         continue;
247                 }
248
249                 printf("%s: warning - unsupported endpoint 0x%x\n", devname,
250                     ed->bEndpointAddress);
251         }
252
253         if (sc->sc_intr_number == -1) {
254                 printf("%s: Could not find interrupt in\n", devname);
255                 ucom->sc_dying = 1;
256                 goto error;
257         }
258
259         sc->sc_intr_iface = ucom->sc_iface;
260
261         if (ucom->sc_bulkout_no == -1) {
262                 printf("%s: Could not find data bulk out\n", devname);
263                 ucom->sc_dying = 1;
264                 goto error;
265         }
266
267         ucom->sc_parent = sc;
268         ucom->sc_portno = UCOM_UNK_PORTNO;
269         ucom->sc_opkthdrlen = 0;
270         ucom->sc_callback = &umct_callback;
271         ucom_attach(ucom);
272         TASK_INIT(&sc->sc_task, 0, umct_notify, sc);
273         return 0;
274
275 error:
276         return ENXIO;
277 }
278
279 static int
280 umct_detach(device_t self)
281 {
282         struct umct_softc *sc = device_get_softc(self);
283
284         int rv;
285
286         if (sc->sc_intr_pipe != NULL) {
287                 usbd_abort_pipe(sc->sc_intr_pipe);
288                 usbd_close_pipe(sc->sc_intr_pipe);
289                 free(sc->sc_intr_buf, M_USBDEV);
290                 sc->sc_intr_pipe = NULL;
291         }
292
293         sc->sc_ucom.sc_dying = 1;
294 #if 0
295         taskqueue_drain(taskqueue_swi_giant);
296 #endif
297         rv = ucom_detach(&sc->sc_ucom);
298         return (rv);
299 }
300
301 static int
302 umct_request(struct umct_softc *sc, uint8_t request, int len, uint32_t value)
303 {
304         usb_device_request_t req;
305         usbd_status err;
306         uint8_t oval[4];
307
308         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
309         req.bRequest = request;
310         USETW(req.wValue, 0);
311         USETW(req.wIndex, sc->sc_iface_number);
312         USETW(req.wLength, len);
313         USETDW(oval, value);
314
315         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, oval);
316         if (err)
317                 printf("%s: ubsa_request: %s\n",
318                     device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
319         return (err);
320 }
321
322 static void
323 umct_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
324 {
325         struct umct_softc *sc;
326         u_char *buf;
327
328         sc = (struct umct_softc *)priv;
329         buf = sc->sc_intr_buf;
330         if (sc->sc_ucom.sc_dying)
331                 return;
332
333         if (status != USBD_NORMAL_COMPLETION) {
334                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
335                         return;
336
337                 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
338                 return;
339         }
340
341         sc->sc_msr = buf[0];
342         sc->sc_lsr = buf[1];
343
344         /*
345          * Defer notifying the ucom layer as it doesn't like to be bothered
346          * from an interrupt context.
347          */
348         taskqueue_enqueue(taskqueue_swi_giant, &sc->sc_task);
349 }
350
351 static void
352 umct_notify(void *arg, int count)
353 {
354         struct umct_softc *sc;
355
356         sc = (struct umct_softc *)arg;
357         if (sc->sc_ucom.sc_dying == 0)
358                 ucom_status_change(&sc->sc_ucom);
359 }
360
361 static void
362 umct_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
363 {
364         struct umct_softc *sc;
365
366         sc = addr;
367         if (lsr != NULL)
368                 *lsr = sc->sc_lsr;
369         if (msr != NULL)
370                 *msr = sc->sc_msr;
371
372         return;
373 }
374
375 static void
376 umct_set(void *addr, int portno, int reg, int onoff)
377 {
378         struct umct_softc *sc;
379
380         sc = addr;
381         switch (reg) {
382         case UCOM_SET_BREAK:
383                 sc->sc_lcr &= ~0x40;
384                 sc->sc_lcr |= (onoff) ? 0x40 : 0;
385                 umct_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
386                 break;
387         case UCOM_SET_DTR:
388                 sc->sc_mcr &= ~0x01;
389                 sc->sc_mcr |= (onoff) ? 0x01 : 0;
390                 umct_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
391                 break;
392         case UCOM_SET_RTS:
393                 sc->sc_mcr &= ~0x2;
394                 sc->sc_mcr |= (onoff) ? 0x02 : 0;
395                 umct_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
396                 break;
397         default:
398                 break;
399         }
400 }
401
402 static int
403 umct_calc_baud(u_int baud)
404 {
405         switch(baud) {
406         case B300: return (0x1);
407         case B600: return (0x2);
408         case B1200: return (0x3);
409         case B2400: return (0x4);
410         case B4800: return (0x6);
411         case B9600: return (0x8);
412         case B19200: return (0x9);
413         case B38400: return (0xa);
414         case B57600: return (0xb);
415         case 115200: return (0xc);
416         case B0:
417         default:
418                 break;
419         }
420
421         return (0x0);
422 }
423
424 static int
425 umct_param(void *addr, int portno, struct termios *ti)
426 {
427         struct umct_softc *sc;
428         uint32_t value;
429
430         sc = addr;
431         value = umct_calc_baud(ti->c_ospeed);
432         umct_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
433
434         value = sc->sc_lcr & 0x40;
435
436         switch (ti->c_cflag & CSIZE) {
437         case CS5: value |= 0x0; break;
438         case CS6: value |= 0x1; break;
439         case CS7: value |= 0x2; break;
440         case CS8: value |= 0x3; break;
441         default: value |= 0x0; break;
442         }
443
444         value |= (ti->c_cflag & CSTOPB) ? 0x4 : 0;
445         if (ti->c_cflag & PARENB) {
446                 value |= 0x8;
447                 value |= (ti->c_cflag & PARODD) ? 0x0 : 0x10;
448         }
449
450         /*
451          * XXX There doesn't seem to be a way to tell the device to use flow
452          * control.
453          */
454
455         sc->sc_lcr = value;
456         umct_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
457
458         return (0);
459 }
460
461 static int
462 umct_open(void *addr, int portno)
463 {
464         struct umct_softc *sc;
465         int err;
466
467         sc = addr;
468         if (sc->sc_ucom.sc_dying) {
469                 return (ENXIO);
470         }
471
472         if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
473                 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
474                 err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
475                     USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_intr_buf,
476                     sc->sc_isize, umct_intr, UMCT_INTR_INTERVAL);
477                 if (err) {
478                         printf("%s: cannot open interrupt pipe (addr %d)\n",
479                             device_get_nameunit(sc->sc_ucom.sc_dev),
480                             sc->sc_intr_number);
481                         free(sc->sc_intr_buf, M_USBDEV);
482                         return (EIO);
483                 }
484         }
485
486         return (0);
487 }
488
489 static void
490 umct_close(void *addr, int portno)
491 {
492         struct umct_softc *sc;
493         int err;
494
495         sc = addr;
496         if (sc->sc_ucom.sc_dying)
497                 return;
498
499         if (sc->sc_intr_pipe != NULL) {
500                 err = usbd_abort_pipe(sc->sc_intr_pipe);
501                 if (err)
502                         printf("%s: abort interrupt pipe failed: %s\n",
503                             device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
504                 err = usbd_close_pipe(sc->sc_intr_pipe);
505                 if (err)
506                         printf("%s: close interrupt pipe failed: %s\n",
507                             device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
508                 free(sc->sc_intr_buf, M_USBDEV);
509                 sc->sc_intr_pipe = NULL;
510         }
511 }