The cam_sim structure was being deallocated unconditionally by device
[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.5 2003/11/16 12:13:39 akiyama Exp $
27  * $DragonFly: src/sys/dev/usbmisc/umct/umct.c,v 1.2 2004/03/15 01:10:45 dillon 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/interrupt.h>
48
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
52 #include <dev/usb/usbdevs.h>
53 #include <dev/usb/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         void                    *sc_swicookie;
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 *);
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         { 0, 0 }
114 };
115
116 Static device_probe_t   umct_match;
117 Static device_attach_t  umct_attach;
118 Static device_detach_t  umct_detach;
119
120 Static device_method_t umct_methods[] = {
121         DEVMETHOD(device_probe, umct_match),
122         DEVMETHOD(device_attach, umct_attach),
123         DEVMETHOD(device_detach, umct_detach),
124         { 0, 0 }
125 };
126
127 Static driver_t umct_driver = {
128         "ucom",
129         umct_methods,
130         sizeof(struct umct_softc)
131 };
132
133 DRIVER_MODULE(umct, uhub, umct_driver, ucom_devclass, usbd_driver_load, 0);
134 MODULE_DEPEND(umct, usb, 1, 1, 1);
135 MODULE_DEPEND(umct, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
136 MODULE_VERSION(umct, 1);
137
138 Static struct ithd *umct_ithd;
139
140 USB_MATCH(umct)
141 {
142         USB_MATCH_START(umct, uaa);
143         int i;
144
145         if (uaa->iface != NULL)
146                 return (UMATCH_NONE);
147
148         for (i = 0; umct_products[i].vendor != 0; i++) {
149                 if (umct_products[i].vendor == uaa->vendor &&
150                     umct_products[i].product == uaa->product) {
151                         return (UMATCH_VENDOR_PRODUCT);
152                 }
153         }
154
155         return (UMATCH_NONE);
156 }
157
158 USB_ATTACH(umct)
159 {
160         USB_ATTACH_START(umct, sc, uaa);
161         usbd_device_handle dev;
162         struct ucom_softc *ucom;
163         usb_config_descriptor_t *cdesc;
164         usb_interface_descriptor_t *id;
165         usb_endpoint_descriptor_t *ed;
166         char *devinfo;
167         const char *devname;
168         usbd_status err;
169         int i;
170
171         dev = uaa->device;
172         devinfo = malloc(1024, M_USBDEV, M_WAITOK | M_ZERO);
173         bzero(sc, sizeof(struct umct_softc));
174         ucom = &sc->sc_ucom;
175         ucom->sc_dev = self;
176         ucom->sc_udev = dev;
177         ucom->sc_iface = uaa->iface;
178
179         usbd_devinfo(dev, 0, devinfo);
180         device_set_desc_copy(self, devinfo);
181         devname = USBDEVNAME(ucom->sc_dev);
182         printf("%s: %s\n", devname, devinfo);
183
184         ucom->sc_bulkout_no = -1;
185         ucom->sc_bulkin_no = -1;
186         sc->sc_intr_number = -1;
187         sc->sc_intr_pipe = NULL;
188
189         err = usbd_set_config_index(dev, UMCT_CONFIG_INDEX, 1);
190         if (err) {
191                 printf("%s: failed to set configuration: %s\n",
192                     devname, usbd_errstr(err));
193                 ucom->sc_dying = 1;
194                 goto error;
195         }
196
197         cdesc = usbd_get_config_descriptor(ucom->sc_udev);
198         if (cdesc == NULL) {
199                 printf("%s: failed to get configuration descriptor\n", devname);
200                 ucom->sc_dying = 1;
201                 goto error;
202         }
203
204         err = usbd_device2interface_handle(dev, UMCT_IFACE_INDEX,
205             &ucom->sc_iface);
206         if (err) {
207                 printf("%s: failed to get interface: %s\n", devname,
208                     usbd_errstr(err));
209                 ucom->sc_dying = 1;
210                 goto error;
211         }
212
213         id = usbd_get_interface_descriptor(ucom->sc_iface);
214         sc->sc_iface_number = id->bInterfaceNumber;
215
216         for (i = 0; i < id->bNumEndpoints; i++) {
217                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
218                 if (ed == NULL) {
219                         printf("%s: no endpoint descriptor for %d\n",
220                             devname, i);
221                         ucom->sc_dying = 1;
222                         goto error;
223                 }
224
225                 /*
226                  * The real bulk-in endpoint is also marked as an interrupt.
227                  * The only way to differentiate it from the real interrupt
228                  * endpoint is to look at the wMaxPacketSize field.
229                  */
230                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) {
231                         if (UGETW(ed->wMaxPacketSize) == 0x2) {
232                                 sc->sc_intr_number = ed->bEndpointAddress;
233                                 sc->sc_isize = UGETW(ed->wMaxPacketSize);
234                         } else {
235                                 ucom->sc_bulkin_no = ed->bEndpointAddress;
236                                 ucom->sc_ibufsize = UGETW(ed->wMaxPacketSize);
237                         }
238                         continue;
239                 }
240
241                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT) {
242                         ucom->sc_bulkout_no = ed->bEndpointAddress;
243                         if (uaa->product == USB_PRODUCT_MCT_SITECOM_USB232)
244                                 ucom->sc_obufsize = 16; /* device is broken */
245                         else
246                                 ucom->sc_obufsize = UGETW(ed->wMaxPacketSize);
247                         continue;
248                 }
249
250                 printf("%s: warning - unsupported endpoint 0x%x\n", devname,
251                     ed->bEndpointAddress);
252         }
253
254         if (sc->sc_intr_number == -1) {
255                 printf("%s: Could not fint interrupt in\n", devname);
256                 ucom->sc_dying = 1;
257                 goto error;
258         }
259
260         sc->sc_intr_iface = ucom->sc_iface;
261
262         if (ucom->sc_bulkout_no == -1) {
263                 printf("%s: Could not find data bulk out\n", devname);
264                 ucom->sc_dying = 1;
265                 goto error;
266         }
267
268         ucom->sc_parent = sc;
269         ucom->sc_portno = UCOM_UNK_PORTNO;
270         ucom->sc_opkthdrlen = 0;
271         ucom->sc_callback = &umct_callback;
272         ucom_attach(ucom);
273         swi_add(&umct_ithd, "ucom", umct_notify, sc, SWI_TTY, 0,
274             &sc->sc_swicookie);
275
276         free(devinfo, M_USBDEV);
277         USB_ATTACH_SUCCESS_RETURN;
278
279 error:
280         free(devinfo, M_USBDEV);
281         USB_ATTACH_ERROR_RETURN;
282 }
283
284 USB_DETACH(umct)
285 {
286         USB_DETACH_START(umct, sc);
287         int rv;
288
289         if (sc->sc_intr_pipe != NULL) {
290                 usbd_abort_pipe(sc->sc_intr_pipe);
291                 usbd_close_pipe(sc->sc_intr_pipe);
292                 free(sc->sc_intr_buf, M_USBDEV);
293                 sc->sc_intr_pipe = NULL;
294         }
295
296         sc->sc_ucom.sc_dying = 1;
297         ithread_remove_handler(sc->sc_swicookie);
298         rv = ucom_detach(&sc->sc_ucom);
299         return (rv);
300 }
301
302 Static int
303 umct_request(struct umct_softc *sc, uint8_t request, int len, uint32_t value)
304 {
305         usb_device_request_t req;
306         usbd_status err;
307         uint8_t oval[4];
308
309         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
310         req.bRequest = request;
311         USETW(req.wValue, 0);
312         USETW(req.wIndex, sc->sc_iface_number);
313         USETW(req.wLength, len);
314         USETDW(oval, value);
315
316         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, oval);
317         if (err)
318                 printf("%s: ubsa_request: %s\n",
319                     USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
320         return (err);
321 }
322
323 Static void
324 umct_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
325 {
326         struct umct_softc *sc;
327         u_char *buf;
328
329         sc = (struct umct_softc *)priv;
330         buf = sc->sc_intr_buf;
331         if (sc->sc_ucom.sc_dying)
332                 return;
333
334         if (status != USBD_NORMAL_COMPLETION) {
335                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
336                         return;
337
338                 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
339                 return;
340         }
341
342         sc->sc_msr = buf[0];
343         sc->sc_lsr = buf[1];
344
345         /*
346          * Defer notifying the ucom layer as it doesn't like to be bothered
347          * from an interrupt context.
348          */
349         swi_sched(sc->sc_swicookie, 0);
350 }
351
352 Static void
353 umct_notify(void *arg)
354 {
355         struct umct_softc *sc;
356
357         sc = (struct umct_softc *)arg;
358         if (sc->sc_ucom.sc_dying == 0)
359                 ucom_status_change(&sc->sc_ucom);
360 }
361
362 Static void
363 umct_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
364 {
365         struct umct_softc *sc;
366
367         sc = addr;
368         if (lsr != NULL)
369                 *lsr = sc->sc_lsr;
370         if (msr != NULL)
371                 *msr = sc->sc_msr;
372
373         return;
374 }
375
376 Static void
377 umct_set(void *addr, int portno, int reg, int onoff)
378 {
379         struct umct_softc *sc;
380
381         sc = addr;
382         switch (reg) {
383         case UCOM_SET_BREAK:
384                 sc->sc_lcr &= ~0x40;
385                 sc->sc_lcr |= (onoff) ? 0x40 : 0;
386                 umct_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
387                 break;
388         case UCOM_SET_DTR:
389                 sc->sc_mcr &= ~0x01;
390                 sc->sc_mcr |= (onoff) ? 0x01 : 0;
391                 umct_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
392                 break;
393         case UCOM_SET_RTS:
394                 sc->sc_mcr &= ~0x2;
395                 sc->sc_mcr |= (onoff) ? 0x02 : 0;
396                 umct_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
397                 break;
398         default:
399                 break;
400         }
401 }
402
403 Static int
404 umct_calc_baud(u_int baud)
405 {
406         switch(baud) {
407         case B300: return (0x1);
408         case B600: return (0x2);
409         case B1200: return (0x3);
410         case B2400: return (0x4);
411         case B4800: return (0x6);
412         case B9600: return (0x8);
413         case B19200: return (0x9);
414         case B38400: return (0xa);
415         case B57600: return (0xb);
416         case 115200: return (0xc);
417         case B0:
418         default:
419                 break;
420         }
421
422         return (0x0);
423 }
424
425 Static int
426 umct_param(void *addr, int portno, struct termios *ti)
427 {
428         struct umct_softc *sc;
429         uint32_t value;
430
431         sc = addr;
432         value = umct_calc_baud(ti->c_ospeed);
433         umct_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
434
435         value = sc->sc_lcr & 0x40;
436
437         switch (ti->c_cflag & CSIZE) {
438         case CS5: value |= 0x0; break;
439         case CS6: value |= 0x1; break;
440         case CS7: value |= 0x2; break;
441         case CS8: value |= 0x3; break;
442         default: value |= 0x0; break;
443         }
444
445         value |= (ti->c_cflag & CSTOPB) ? 0x4 : 0;
446         if (ti->c_cflag & PARENB) {
447                 value |= 0x8;
448                 value |= (ti->c_cflag & PARODD) ? 0x0 : 0x10;
449         }
450
451         /*
452          * XXX There doesn't seem to be a way to tell the device to use flow
453          * control.
454          */
455
456         sc->sc_lcr = value;
457         umct_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
458
459         return (0);
460 }
461
462 Static int
463 umct_open(void *addr, int portno)
464 {
465         struct umct_softc *sc;
466         int err;
467
468         sc = addr;
469         if (sc->sc_ucom.sc_dying) {
470                 return (ENXIO);
471         }
472
473         if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
474                 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
475                 err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
476                     USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_intr_buf,
477                     sc->sc_isize, umct_intr, UMCT_INTR_INTERVAL);
478                 if (err) {
479                         printf("%s: cannot open interrupt pipe (addr %d)\n",
480                             USBDEVNAME(sc->sc_ucom.sc_dev),
481                             sc->sc_intr_number);
482                         free(sc->sc_intr_buf, M_USBDEV);
483                         return (EIO);
484                 }
485         }
486
487         return (0);
488 }
489
490 Static void
491 umct_close(void *addr, int portno)
492 {
493         struct umct_softc *sc;
494         int err;
495
496         sc = addr;
497         if (sc->sc_ucom.sc_dying)
498                 return;
499
500         if (sc->sc_intr_pipe != NULL) {
501                 err = usbd_abort_pipe(sc->sc_intr_pipe);
502                 if (err)
503                         printf("%s: abort interrupt pipe failed: %s\n",
504                             USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
505                 err = usbd_close_pipe(sc->sc_intr_pipe);
506                 if (err)
507                         printf("%s: close interrupt pipe failed: %s\n",
508                             USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
509                 free(sc->sc_intr_buf, M_USBDEV);
510                 sc->sc_intr_pipe = NULL;
511         }
512 }