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