Merge from vendor branch OPENSSH:
[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.7 2006/12/22 23:26:26 swildner 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
67 #define UMCT_INTR_INTERVAL      100
68 #define UMCT_IFACE_INDEX        0
69 #define UMCT_CONFIG_INDEX       1
70
71 struct umct_softc {
72         struct ucom_softc       sc_ucom;
73         int                     sc_iface_number;
74         usbd_interface_handle   sc_intr_iface;
75         int                     sc_intr_number;
76         usbd_pipe_handle        sc_intr_pipe;
77         u_char                  *sc_intr_buf;
78         int                     sc_isize;
79         uint8_t                 sc_lsr;
80         uint8_t                 sc_msr;
81         uint8_t                 sc_lcr;
82         uint8_t                 sc_mcr;
83         struct task             sc_task;
84 };
85
86 Static void umct_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
87 Static void umct_get_status(void *, int, u_char *, u_char *);
88 Static void umct_set(void *, int, int, int);
89 Static int  umct_param(void *, int, struct termios *);
90 Static int  umct_open(void *, int);
91 Static void umct_close(void *, int);
92 Static void umct_notify(void *, int);
93
94 Static struct ucom_callback umct_callback = {
95         umct_get_status,        /* ucom_get_status */
96         umct_set,               /* ucom_set */
97         umct_param,             /* ucom_param */
98         NULL,                   /* ucom_ioctl */
99         umct_open,              /* ucom_open */
100         umct_close,             /* ucom_close */
101         NULL,                   /* ucom_read */
102         NULL                    /* ucom_write */
103 };
104
105 Static const struct umct_product {
106         uint16_t        vendor;
107         uint16_t        product;
108 } umct_products[] = {
109         { USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232 },
110         { USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232 },
111         { USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232 },
112         { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109 },
113         { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U409 },
114         { 0, 0 }
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 USB_MATCH(umct)
140 {
141         USB_MATCH_START(umct, uaa);
142         int i;
143
144         if (uaa->iface != NULL)
145                 return (UMATCH_NONE);
146
147         for (i = 0; umct_products[i].vendor != 0; i++) {
148                 if (umct_products[i].vendor == uaa->vendor &&
149                     umct_products[i].product == uaa->product) {
150                         return (UMATCH_VENDOR_PRODUCT);
151                 }
152         }
153
154         return (UMATCH_NONE);
155 }
156
157 USB_ATTACH(umct)
158 {
159         USB_ATTACH_START(umct, sc, uaa);
160         usbd_device_handle dev;
161         struct ucom_softc *ucom;
162         usb_config_descriptor_t *cdesc;
163         usb_interface_descriptor_t *id;
164         usb_endpoint_descriptor_t *ed;
165         char *devinfo;
166         const char *devname;
167         usbd_status err;
168         int i;
169
170         dev = uaa->device;
171         devinfo = kmalloc(1024, M_USBDEV, M_INTWAIT | M_ZERO);
172         bzero(sc, sizeof(struct umct_softc));
173         ucom = &sc->sc_ucom;
174         ucom->sc_dev = self;
175         ucom->sc_udev = dev;
176         ucom->sc_iface = uaa->iface;
177
178         usbd_devinfo(dev, 0, devinfo);
179         device_set_desc_copy(self, devinfo);
180         devname = USBDEVNAME(ucom->sc_dev);
181         kprintf("%s: %s\n", devname, devinfo);
182
183         ucom->sc_bulkout_no = -1;
184         ucom->sc_bulkin_no = -1;
185         sc->sc_intr_number = -1;
186         sc->sc_intr_pipe = NULL;
187
188         err = usbd_set_config_index(dev, UMCT_CONFIG_INDEX, 1);
189         if (err) {
190                 kprintf("%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                 kprintf("%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                 kprintf("%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                         kprintf("%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                 kprintf("%s: warning - unsupported endpoint 0x%x\n", devname,
250                     ed->bEndpointAddress);
251         }
252
253         if (sc->sc_intr_number == -1) {
254                 kprintf("%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                 kprintf("%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         TASK_INIT(&sc->sc_task, 0, umct_notify, sc);
272         ucom_attach(ucom);
273
274         kfree(devinfo, M_USBDEV);
275         USB_ATTACH_SUCCESS_RETURN;
276
277 error:
278         kfree(devinfo, M_USBDEV);
279         USB_ATTACH_ERROR_RETURN;
280 }
281
282 USB_DETACH(umct)
283 {
284         USB_DETACH_START(umct, sc);
285         int rv;
286
287         if (sc->sc_intr_pipe != NULL) {
288                 usbd_abort_pipe(sc->sc_intr_pipe);
289                 usbd_close_pipe(sc->sc_intr_pipe);
290                 kfree(sc->sc_intr_buf, M_USBDEV);
291                 sc->sc_intr_pipe = NULL;
292         }
293
294         sc->sc_ucom.sc_dying = 1;
295         rv = ucom_detach(&sc->sc_ucom);
296         return (rv);
297 }
298
299 Static int
300 umct_request(struct umct_softc *sc, uint8_t request, int len, uint32_t value)
301 {
302         usb_device_request_t req;
303         usbd_status err;
304         uint8_t oval[4];
305
306         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
307         req.bRequest = request;
308         USETW(req.wValue, 0);
309         USETW(req.wIndex, sc->sc_iface_number);
310         USETW(req.wLength, len);
311         USETDW(oval, value);
312
313         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, oval);
314         if (err)
315                 kprintf("%s: ubsa_request: %s\n",
316                     USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
317         return (err);
318 }
319
320 Static void
321 umct_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
322 {
323         struct umct_softc *sc;
324         u_char *buf;
325
326         sc = (struct umct_softc *)priv;
327         buf = sc->sc_intr_buf;
328         if (sc->sc_ucom.sc_dying)
329                 return;
330
331         if (status != USBD_NORMAL_COMPLETION) {
332                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
333                         return;
334
335                 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
336                 return;
337         }
338
339         sc->sc_msr = buf[0];
340         sc->sc_lsr = buf[1];
341
342         /*
343          * Defer notifying the ucom layer as it doesn't like to be bothered
344          * from an interrupt context.
345          */
346         taskqueue_enqueue(taskqueue_swi, &sc->sc_task);
347 }
348
349 Static void
350 umct_notify(void *arg, int count)
351 {
352         struct umct_softc *sc;
353
354         sc = (struct umct_softc *)arg;
355         if (sc->sc_ucom.sc_dying == 0)
356                 ucom_status_change(&sc->sc_ucom);
357 }
358
359 Static void
360 umct_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
361 {
362         struct umct_softc *sc;
363
364         sc = addr;
365         if (lsr != NULL)
366                 *lsr = sc->sc_lsr;
367         if (msr != NULL)
368                 *msr = sc->sc_msr;
369
370         return;
371 }
372
373 Static void
374 umct_set(void *addr, int portno, int reg, int onoff)
375 {
376         struct umct_softc *sc;
377
378         sc = addr;
379         switch (reg) {
380         case UCOM_SET_BREAK:
381                 sc->sc_lcr &= ~0x40;
382                 sc->sc_lcr |= (onoff) ? 0x40 : 0;
383                 umct_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
384                 break;
385         case UCOM_SET_DTR:
386                 sc->sc_mcr &= ~0x01;
387                 sc->sc_mcr |= (onoff) ? 0x01 : 0;
388                 umct_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
389                 break;
390         case UCOM_SET_RTS:
391                 sc->sc_mcr &= ~0x2;
392                 sc->sc_mcr |= (onoff) ? 0x02 : 0;
393                 umct_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
394                 break;
395         default:
396                 break;
397         }
398 }
399
400 Static int
401 umct_calc_baud(u_int baud)
402 {
403         switch(baud) {
404         case B300: return (0x1);
405         case B600: return (0x2);
406         case B1200: return (0x3);
407         case B2400: return (0x4);
408         case B4800: return (0x6);
409         case B9600: return (0x8);
410         case B19200: return (0x9);
411         case B38400: return (0xa);
412         case B57600: return (0xb);
413         case 115200: return (0xc);
414         case B0:
415         default:
416                 break;
417         }
418
419         return (0x0);
420 }
421
422 Static int
423 umct_param(void *addr, int portno, struct termios *ti)
424 {
425         struct umct_softc *sc;
426         uint32_t value;
427
428         sc = addr;
429         value = umct_calc_baud(ti->c_ospeed);
430         umct_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
431
432         value = sc->sc_lcr & 0x40;
433
434         switch (ti->c_cflag & CSIZE) {
435         case CS5: value |= 0x0; break;
436         case CS6: value |= 0x1; break;
437         case CS7: value |= 0x2; break;
438         case CS8: value |= 0x3; break;
439         default: value |= 0x0; break;
440         }
441
442         value |= (ti->c_cflag & CSTOPB) ? 0x4 : 0;
443         if (ti->c_cflag & PARENB) {
444                 value |= 0x8;
445                 value |= (ti->c_cflag & PARODD) ? 0x0 : 0x10;
446         }
447
448         /*
449          * XXX There doesn't seem to be a way to tell the device to use flow
450          * control.
451          */
452
453         sc->sc_lcr = value;
454         umct_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
455
456         return (0);
457 }
458
459 Static int
460 umct_open(void *addr, int portno)
461 {
462         struct umct_softc *sc;
463         int err;
464
465         sc = addr;
466         if (sc->sc_ucom.sc_dying) {
467                 return (ENXIO);
468         }
469
470         if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
471                 sc->sc_intr_buf = kmalloc(sc->sc_isize, M_USBDEV, M_WAITOK);
472                 err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
473                     USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_intr_buf,
474                     sc->sc_isize, umct_intr, UMCT_INTR_INTERVAL);
475                 if (err) {
476                         kprintf("%s: cannot open interrupt pipe (addr %d)\n",
477                             USBDEVNAME(sc->sc_ucom.sc_dev),
478                             sc->sc_intr_number);
479                         kfree(sc->sc_intr_buf, M_USBDEV);
480                         return (EIO);
481                 }
482         }
483
484         return (0);
485 }
486
487 Static void
488 umct_close(void *addr, int portno)
489 {
490         struct umct_softc *sc;
491         int err;
492
493         sc = addr;
494         if (sc->sc_ucom.sc_dying)
495                 return;
496
497         if (sc->sc_intr_pipe != NULL) {
498                 err = usbd_abort_pipe(sc->sc_intr_pipe);
499                 if (err)
500                         kprintf("%s: abort interrupt pipe failed: %s\n",
501                             USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
502                 err = usbd_close_pipe(sc->sc_intr_pipe);
503                 if (err)
504                         kprintf("%s: close interrupt pipe failed: %s\n",
505                             USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
506                 kfree(sc->sc_intr_buf, M_USBDEV);
507                 sc->sc_intr_pipe = NULL;
508         }
509 }