Merge branch 'vendor/GDTOA'
[dragonfly.git] / sys / dev / usbmisc / umct / 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  * $FreeBSD: src/sys/dev/usb/umct.c,v 1.12 2006/09/07 00:06:42 imp Exp $
27  * $DragonFly: src/sys/dev/usbmisc/umct/umct.c,v 1.15 2007/11/06 07:37:01 hasso Exp $
28  */
29
30 /*
31  * Driver for the MCT (Magic Control Technology) USB-RS232 Converter.
32  * Based on the superb documentation from the linux mct_u232 driver by
33  * Wolfgang Grandeggar <wolfgang@cec.ch>.
34  * This device smells a lot like the Belkin F5U103, except that it has
35  * suffered some mild brain-damage.  This driver is based off of the ubsa.c
36  * driver from Alexander Kabaev <kan@freebsd.org>.  Merging the two together
37  * might be useful, though the subtle differences might lead to lots of
38  * #ifdef's.
39  */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/bus.h>
46 #include <sys/tty.h>
47 #include <sys/taskqueue.h>
48
49 #include <bus/usb/usb.h>
50 #include <bus/usb/usbdi.h>
51 #include <bus/usb/usbdi_util.h>
52 #include "../ucom/ucomvar.h"
53
54 /* The UMCT advertises the standard 8250 UART registers */
55 #define UMCT_GET_MSR            2       /* Get Modem Status Register */
56 #define UMCT_GET_MSR_SIZE       1
57 #define UMCT_GET_LCR            6       /* Get Line Control Register */
58 #define UMCT_GET_LCR_SIZE       1
59 #define UMCT_SET_BAUD           5       /* Set the Baud Rate Divisor */
60 #define UMCT_SET_BAUD_SIZE      4
61 #define UMCT_SET_LCR            7       /* Set Line Control Register */
62 #define UMCT_SET_LCR_SIZE       1
63 #define UMCT_SET_MCR            10      /* Set Modem Control Register */
64 #define UMCT_SET_MCR_SIZE       1
65 #define UMCT_SET_UNKNOWN1       11
66 #define UMCT_SET_UNKNOWN1_SIZE  1
67 #define UMCT_SET_UNKNOWN2       12
68 #define UMCT_SET_UNKNOWN2_SIZE  1
69
70
71 #define UMCT_INTR_INTERVAL      100
72 #define UMCT_IFACE_INDEX        0
73 #define UMCT_CONFIG_INDEX       1
74
75 struct umct_softc {
76         struct ucom_softc       sc_ucom;
77         int                     sc_iface_number;
78         usbd_interface_handle   sc_intr_iface;
79         int                     sc_intr_number;
80         usbd_pipe_handle        sc_intr_pipe;
81         u_char                  *sc_intr_buf;
82         int                     sc_isize;
83         uint8_t                 sc_lsr;
84         uint8_t                 sc_msr;
85         uint8_t                 sc_lcr;
86         uint8_t                 sc_mcr;
87         struct task             sc_task;
88 };
89
90 static void umct_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
91 static void umct_get_status(void *, int, u_char *, u_char *);
92 static void umct_set(void *, int, int, int);
93 static int  umct_param(void *, int, struct termios *);
94 static int  umct_open(void *, int);
95 static void umct_close(void *, int);
96 static void umct_notify(void *, int);
97
98 static struct ucom_callback umct_callback = {
99         umct_get_status,        /* ucom_get_status */
100         umct_set,               /* ucom_set */
101         umct_param,             /* ucom_param */
102         NULL,                   /* ucom_ioctl */
103         umct_open,              /* ucom_open */
104         umct_close,             /* ucom_close */
105         NULL,                   /* ucom_read */
106         NULL                    /* ucom_write */
107 };
108
109 static const struct usb_devno umct_products[] = {
110         { USB_DEVICE(0x050d, 0x0109) }, /* Belkin F5U109 */
111         { USB_DEVICE(0x050d, 0x0409) }, /* Belkin F5U409 */
112         { USB_DEVICE(0x0711, 0x0200) }, /* D-Link DU-H3SP USB BAY Hub */
113         { USB_DEVICE(0x0711, 0x0210) }, /* MCT USB-232 */
114         { USB_DEVICE(0x0711, 0x0230) }, /* Sitecom USB-232 */
115 };
116
117 static device_probe_t   umct_match;
118 static device_attach_t  umct_attach;
119 static device_detach_t  umct_detach;
120
121 static device_method_t umct_methods[] = {
122         DEVMETHOD(device_probe, umct_match),
123         DEVMETHOD(device_attach, umct_attach),
124         DEVMETHOD(device_detach, umct_detach),
125         { 0, 0 }
126 };
127
128 static driver_t umct_driver = {
129         "ucom",
130         umct_methods,
131         sizeof(struct umct_softc)
132 };
133
134 DRIVER_MODULE(umct, uhub, umct_driver, ucom_devclass, usbd_driver_load, 0);
135 MODULE_DEPEND(umct, usb, 1, 1, 1);
136 MODULE_DEPEND(umct, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
137 MODULE_VERSION(umct, 1);
138
139 static int
140 umct_match(device_t self)
141 {
142         struct usb_attach_arg *uaa = device_get_ivars(self);
143
144         if (uaa->iface != NULL)
145                 return (UMATCH_NONE);
146
147         return (usb_lookup(umct_products, uaa->vendor, uaa->product) != NULL) ?
148             UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
149 }
150
151 static int
152 umct_attach(device_t self)
153 {
154         struct umct_softc *sc = device_get_softc(self);
155         struct usb_attach_arg *uaa = device_get_ivars(self);
156         usbd_device_handle dev;
157         struct ucom_softc *ucom;
158         usb_config_descriptor_t *cdesc;
159         usb_interface_descriptor_t *id;
160         usb_endpoint_descriptor_t *ed;
161         usbd_status err;
162         int i;
163
164         dev = uaa->device;
165         bzero(sc, sizeof(struct umct_softc));
166         ucom = &sc->sc_ucom;
167         ucom->sc_dev = self;
168         ucom->sc_udev = dev;
169         ucom->sc_iface = uaa->iface;
170
171         ucom->sc_bulkout_no = -1;
172         ucom->sc_bulkin_no = -1;
173         sc->sc_intr_number = -1;
174         sc->sc_intr_pipe = NULL;
175
176         err = usbd_set_config_index(dev, UMCT_CONFIG_INDEX, 1);
177         if (err) {
178                 device_printf(ucom->sc_dev, "failed to set configuration: %s\n",
179                               usbd_errstr(err));
180                 ucom->sc_dying = 1;
181                 goto error;
182         }
183
184         cdesc = usbd_get_config_descriptor(ucom->sc_udev);
185         if (cdesc == NULL) {
186                 device_printf(ucom->sc_dev, "failed to get configuration "
187                               "descriptor\n");
188                 ucom->sc_dying = 1;
189                 goto error;
190         }
191
192         err = usbd_device2interface_handle(dev, UMCT_IFACE_INDEX,
193             &ucom->sc_iface);
194         if (err) {
195                 device_printf(ucom->sc_dev, "failed to get interface: %s\n",
196                               usbd_errstr(err));
197                 ucom->sc_dying = 1;
198                 goto error;
199         }
200
201         id = usbd_get_interface_descriptor(ucom->sc_iface);
202         sc->sc_iface_number = id->bInterfaceNumber;
203
204         for (i = 0; i < id->bNumEndpoints; i++) {
205                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
206                 if (ed == NULL) {
207                         device_printf(ucom->sc_dev, "no endpoint descriptor "
208                                       "for %d\n", i);
209                         ucom->sc_dying = 1;
210                         goto error;
211                 }
212
213                 /*
214                  * The real bulk-in endpoint is also marked as an interrupt.
215                  * The only way to differentiate it from the real interrupt
216                  * endpoint is to look at the wMaxPacketSize field.
217                  */
218                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) {
219                         if (UGETW(ed->wMaxPacketSize) == 0x2) {
220                                 sc->sc_intr_number = ed->bEndpointAddress;
221                                 sc->sc_isize = UGETW(ed->wMaxPacketSize);
222                         } else {
223                                 ucom->sc_bulkin_no = ed->bEndpointAddress;
224                                 ucom->sc_ibufsize = UGETW(ed->wMaxPacketSize);
225                         }
226                         continue;
227                 }
228
229                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT) {
230                         ucom->sc_bulkout_no = ed->bEndpointAddress;
231                         /* MCT Sitecom USB-232 device is broken. The bulk out
232                          * endpoint descriptor reports 32 bytes, but data will
233                          * get dropped if this value is used. */
234                         if (uaa->vendor == 0x0711 && uaa->product == 0x0230)
235                                 ucom->sc_obufsize = 16;
236                         else
237                                 ucom->sc_obufsize = UGETW(ed->wMaxPacketSize);
238                         continue;
239                 }
240
241                 device_printf(ucom->sc_dev, "warning - unsupported endpoint "
242                               "0x%x\n", ed->bEndpointAddress);
243         }
244
245         if (sc->sc_intr_number == -1) {
246                 device_printf(ucom->sc_dev, "could not find interrupt in\n");
247                 ucom->sc_dying = 1;
248                 goto error;
249         }
250
251         sc->sc_intr_iface = ucom->sc_iface;
252
253         if (ucom->sc_bulkout_no == -1) {
254                 device_printf(ucom->sc_dev, "rould not find data bulk out\n");
255                 ucom->sc_dying = 1;
256                 goto error;
257         }
258
259         ucom->sc_parent = sc;
260         ucom->sc_portno = UCOM_UNK_PORTNO;
261         ucom->sc_opkthdrlen = 0;
262         ucom->sc_callback = &umct_callback;
263         TASK_INIT(&sc->sc_task, 0, umct_notify, sc);
264         ucom_attach(ucom);
265
266         return 0;
267
268 error:
269         return ENXIO;
270 }
271
272 static int
273 umct_detach(device_t self)
274 {
275         struct umct_softc *sc = device_get_softc(self);
276         int rv;
277
278         if (sc->sc_intr_pipe != NULL) {
279                 usbd_abort_pipe(sc->sc_intr_pipe);
280                 usbd_close_pipe(sc->sc_intr_pipe);
281                 kfree(sc->sc_intr_buf, M_USBDEV);
282                 sc->sc_intr_pipe = NULL;
283         }
284
285         sc->sc_ucom.sc_dying = 1;
286         rv = ucom_detach(&sc->sc_ucom);
287         return (rv);
288 }
289
290 static int
291 umct_request(struct umct_softc *sc, uint8_t request, int len, uint32_t value)
292 {
293         usb_device_request_t req;
294         usbd_status err;
295         uint8_t oval[4];
296
297         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
298         req.bRequest = request;
299         USETW(req.wValue, 0);
300         USETW(req.wIndex, sc->sc_iface_number);
301         USETW(req.wLength, len);
302         USETDW(oval, value);
303
304         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, oval);
305         if (err)
306                 device_printf(sc->sc_ucom.sc_dev, "ubsa_request: %s\n",
307                               usbd_errstr(err));
308         return (err);
309 }
310
311 static void
312 umct_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
313 {
314         struct umct_softc *sc;
315         u_char *buf;
316
317         sc = (struct umct_softc *)priv;
318         buf = sc->sc_intr_buf;
319         if (sc->sc_ucom.sc_dying)
320                 return;
321
322         if (status != USBD_NORMAL_COMPLETION) {
323                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
324                         return;
325
326                 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
327                 return;
328         }
329
330         sc->sc_msr = buf[0];
331         sc->sc_lsr = buf[1];
332
333         /*
334          * Defer notifying the ucom layer as it doesn't like to be bothered
335          * from an interrupt context.
336          */
337         taskqueue_enqueue(taskqueue_swi, &sc->sc_task);
338 }
339
340 static void
341 umct_notify(void *arg, int count)
342 {
343         struct umct_softc *sc;
344
345         sc = (struct umct_softc *)arg;
346         if (sc->sc_ucom.sc_dying == 0)
347                 ucom_status_change(&sc->sc_ucom);
348 }
349
350 static void
351 umct_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
352 {
353         struct umct_softc *sc;
354
355         sc = addr;
356         if (lsr != NULL)
357                 *lsr = sc->sc_lsr;
358         if (msr != NULL)
359                 *msr = sc->sc_msr;
360
361         return;
362 }
363
364 static void
365 umct_set(void *addr, int portno, int reg, int onoff)
366 {
367         struct umct_softc *sc;
368
369         sc = addr;
370         switch (reg) {
371         case UCOM_SET_BREAK:
372                 sc->sc_lcr &= ~0x40;
373                 sc->sc_lcr |= (onoff) ? 0x40 : 0;
374                 umct_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
375                 break;
376         case UCOM_SET_DTR:
377                 sc->sc_mcr &= ~0x01;
378                 sc->sc_mcr |= (onoff) ? 0x01 : 0;
379                 umct_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
380                 break;
381         case UCOM_SET_RTS:
382                 sc->sc_mcr &= ~0x2;
383                 sc->sc_mcr |= (onoff) ? 0x02 : 0;
384                 umct_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
385                 break;
386         default:
387                 break;
388         }
389 }
390
391 static int
392 umct_calc_baud(u_int baud)
393 {
394         switch(baud) {
395         case B300: return (0x1);
396         case B600: return (0x2);
397         case B1200: return (0x3);
398         case B2400: return (0x4);
399         case B4800: return (0x6);
400         case B9600: return (0x8);
401         case B19200: return (0x9);
402         case B38400: return (0xa);
403         case B57600: return (0xb);
404         case B115200: return (0xc);
405         case B0:
406         default:
407                 break;
408         }
409
410         return (0x0);
411 }
412
413 static int
414 umct_param(void *addr, int portno, struct termios *ti)
415 {
416         struct umct_softc *sc;
417         uint32_t value;
418
419         sc = addr;
420         value = umct_calc_baud(ti->c_ospeed);
421         umct_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
422         umct_request(sc, UMCT_SET_UNKNOWN1, UMCT_SET_UNKNOWN1_SIZE, 0);
423         umct_request(sc, UMCT_SET_UNKNOWN2, UMCT_SET_UNKNOWN2_SIZE, 0);
424
425         value = sc->sc_lcr & 0x40;
426
427         switch (ti->c_cflag & CSIZE) {
428         case CS5: value |= 0x0; break;
429         case CS6: value |= 0x1; break;
430         case CS7: value |= 0x2; break;
431         case CS8: value |= 0x3; break;
432         default: value |= 0x0; break;
433         }
434
435         value |= (ti->c_cflag & CSTOPB) ? 0x4 : 0;
436         if (ti->c_cflag & PARENB) {
437                 value |= 0x8;
438                 value |= (ti->c_cflag & PARODD) ? 0x0 : 0x10;
439         }
440
441         /*
442          * XXX There doesn't seem to be a way to tell the device to use flow
443          * control.
444          */
445
446         sc->sc_lcr = value;
447         umct_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
448
449         return (0);
450 }
451
452 static int
453 umct_open(void *addr, int portno)
454 {
455         struct umct_softc *sc;
456         int err;
457
458         sc = addr;
459         if (sc->sc_ucom.sc_dying) {
460                 return (ENXIO);
461         }
462
463         if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
464                 sc->sc_intr_buf = kmalloc(sc->sc_isize, M_USBDEV, M_WAITOK);
465                 err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
466                     USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_intr_buf,
467                     sc->sc_isize, umct_intr, UMCT_INTR_INTERVAL);
468                 if (err) {
469                         device_printf(sc->sc_ucom.sc_dev, "cannot open "
470                                       "interrupt pipe (addr %d)\n",
471                                       sc->sc_intr_number);
472                         kfree(sc->sc_intr_buf, M_USBDEV);
473                         return (EIO);
474                 }
475         }
476
477         return (0);
478 }
479
480 static void
481 umct_close(void *addr, int portno)
482 {
483         struct umct_softc *sc;
484         int err;
485
486         sc = addr;
487         if (sc->sc_ucom.sc_dying)
488                 return;
489
490         if (sc->sc_intr_pipe != NULL) {
491                 err = usbd_abort_pipe(sc->sc_intr_pipe);
492                 if (err)
493                         device_printf(sc->sc_ucom.sc_dev, "abort interrupt "
494                                       "pipe failed: %s\n", usbd_errstr(err));
495                 err = usbd_close_pipe(sc->sc_intr_pipe);
496                 if (err)
497                         device_printf(sc->sc_ucom.sc_dev, "close interrupt "
498                                       "pipe failed: %s\n", usbd_errstr(err));
499                 kfree(sc->sc_intr_buf, M_USBDEV);
500                 sc->sc_intr_pipe = NULL;
501         }
502 }