2 * Copyright (c) 2003 Scott Long
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
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
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>
49 #include <sys/taskqueue.h>
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
53 #include <dev/usb/usbdi_util.h>
55 #include <dev/usb/ucomvar.h>
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
69 #define UMCT_INTR_INTERVAL 100
70 #define UMCT_IFACE_INDEX 0
71 #define UMCT_CONFIG_INDEX 1
74 struct ucom_softc sc_ucom;
76 usbd_interface_handle sc_intr_iface;
78 usbd_pipe_handle sc_intr_pipe;
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);
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 */
107 Static const struct umct_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 },
119 Static device_probe_t umct_match;
120 Static device_attach_t umct_attach;
121 Static device_detach_t umct_detach;
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),
130 Static driver_t umct_driver = {
133 sizeof(struct umct_softc)
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);
143 USB_MATCH_START(umct, uaa);
146 if (uaa->iface != NULL)
147 return (UMATCH_NONE);
149 for (i = 0; umct_products[i].vendor != 0; i++) {
150 if (umct_products[i].vendor == uaa->vendor &&
151 umct_products[i].product == uaa->product) {
152 return (UMATCH_VENDOR_PRODUCT);
156 return (UMATCH_NONE);
161 USB_ATTACH_START(umct, sc, uaa);
162 usbd_device_handle dev;
163 struct ucom_softc *ucom;
164 usb_config_descriptor_t *cdesc;
165 usb_interface_descriptor_t *id;
166 usb_endpoint_descriptor_t *ed;
173 devinfo = malloc(1024, M_USBDEV, M_NOWAIT | M_ZERO);
176 bzero(sc, sizeof(struct umct_softc));
180 ucom->sc_iface = uaa->iface;
182 usbd_devinfo(dev, 0, devinfo);
183 device_set_desc_copy(self, devinfo);
184 devname = USBDEVNAME(ucom->sc_dev);
185 printf("%s: %s\n", devname, devinfo);
187 ucom->sc_bulkout_no = -1;
188 ucom->sc_bulkin_no = -1;
189 sc->sc_intr_number = -1;
190 sc->sc_intr_pipe = NULL;
192 err = usbd_set_config_index(dev, UMCT_CONFIG_INDEX, 1);
194 printf("%s: failed to set configuration: %s\n",
195 devname, usbd_errstr(err));
200 cdesc = usbd_get_config_descriptor(ucom->sc_udev);
202 printf("%s: failed to get configuration descriptor\n", devname);
207 err = usbd_device2interface_handle(dev, UMCT_IFACE_INDEX,
210 printf("%s: failed to get interface: %s\n", devname,
216 id = usbd_get_interface_descriptor(ucom->sc_iface);
217 sc->sc_iface_number = id->bInterfaceNumber;
219 for (i = 0; i < id->bNumEndpoints; i++) {
220 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
222 printf("%s: no endpoint descriptor for %d\n",
229 * The real bulk-in endpoint is also marked as an interrupt.
230 * The only way to differentiate it from the real interrupt
231 * endpoint is to look at the wMaxPacketSize field.
233 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) {
234 if (UGETW(ed->wMaxPacketSize) == 0x2) {
235 sc->sc_intr_number = ed->bEndpointAddress;
236 sc->sc_isize = UGETW(ed->wMaxPacketSize);
238 ucom->sc_bulkin_no = ed->bEndpointAddress;
239 ucom->sc_ibufsize = UGETW(ed->wMaxPacketSize);
244 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT) {
245 ucom->sc_bulkout_no = ed->bEndpointAddress;
246 if (uaa->product == USB_PRODUCT_MCT_SITECOM_USB232)
247 ucom->sc_obufsize = 16; /* device is broken */
249 ucom->sc_obufsize = UGETW(ed->wMaxPacketSize);
253 printf("%s: warning - unsupported endpoint 0x%x\n", devname,
254 ed->bEndpointAddress);
257 if (sc->sc_intr_number == -1) {
258 printf("%s: Could not fint interrupt in\n", devname);
263 sc->sc_intr_iface = ucom->sc_iface;
265 if (ucom->sc_bulkout_no == -1) {
266 printf("%s: Could not find data bulk out\n", devname);
271 ucom->sc_parent = sc;
272 ucom->sc_portno = UCOM_UNK_PORTNO;
273 ucom->sc_opkthdrlen = 0;
274 ucom->sc_callback = &umct_callback;
276 TASK_INIT(&sc->sc_task, 0, umct_notify, sc);
278 free(devinfo, M_USBDEV);
279 USB_ATTACH_SUCCESS_RETURN;
282 free(devinfo, M_USBDEV);
283 USB_ATTACH_ERROR_RETURN;
288 USB_DETACH_START(umct, sc);
291 if (sc->sc_intr_pipe != NULL) {
292 usbd_abort_pipe(sc->sc_intr_pipe);
293 usbd_close_pipe(sc->sc_intr_pipe);
294 free(sc->sc_intr_buf, M_USBDEV);
295 sc->sc_intr_pipe = NULL;
298 sc->sc_ucom.sc_dying = 1;
300 taskqueue_drain(taskqueue_swi_giant);
302 rv = ucom_detach(&sc->sc_ucom);
307 umct_request(struct umct_softc *sc, uint8_t request, int len, uint32_t value)
309 usb_device_request_t req;
313 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
314 req.bRequest = request;
315 USETW(req.wValue, 0);
316 USETW(req.wIndex, sc->sc_iface_number);
317 USETW(req.wLength, len);
320 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, oval);
322 printf("%s: ubsa_request: %s\n",
323 USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
328 umct_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
330 struct umct_softc *sc;
333 sc = (struct umct_softc *)priv;
334 buf = sc->sc_intr_buf;
335 if (sc->sc_ucom.sc_dying)
338 if (status != USBD_NORMAL_COMPLETION) {
339 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
342 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
350 * Defer notifying the ucom layer as it doesn't like to be bothered
351 * from an interrupt context.
353 taskqueue_enqueue(taskqueue_swi_giant, &sc->sc_task);
357 umct_notify(void *arg, int count)
359 struct umct_softc *sc;
361 sc = (struct umct_softc *)arg;
362 if (sc->sc_ucom.sc_dying == 0)
363 ucom_status_change(&sc->sc_ucom);
367 umct_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
369 struct umct_softc *sc;
381 umct_set(void *addr, int portno, int reg, int onoff)
383 struct umct_softc *sc;
389 sc->sc_lcr |= (onoff) ? 0x40 : 0;
390 umct_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
394 sc->sc_mcr |= (onoff) ? 0x01 : 0;
395 umct_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
399 sc->sc_mcr |= (onoff) ? 0x02 : 0;
400 umct_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
408 umct_calc_baud(u_int baud)
411 case B300: return (0x1);
412 case B600: return (0x2);
413 case B1200: return (0x3);
414 case B2400: return (0x4);
415 case B4800: return (0x6);
416 case B9600: return (0x8);
417 case B19200: return (0x9);
418 case B38400: return (0xa);
419 case B57600: return (0xb);
420 case 115200: return (0xc);
430 umct_param(void *addr, int portno, struct termios *ti)
432 struct umct_softc *sc;
436 value = umct_calc_baud(ti->c_ospeed);
437 umct_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
439 value = sc->sc_lcr & 0x40;
441 switch (ti->c_cflag & CSIZE) {
442 case CS5: value |= 0x0; break;
443 case CS6: value |= 0x1; break;
444 case CS7: value |= 0x2; break;
445 case CS8: value |= 0x3; break;
446 default: value |= 0x0; break;
449 value |= (ti->c_cflag & CSTOPB) ? 0x4 : 0;
450 if (ti->c_cflag & PARENB) {
452 value |= (ti->c_cflag & PARODD) ? 0x0 : 0x10;
456 * XXX There doesn't seem to be a way to tell the device to use flow
461 umct_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
467 umct_open(void *addr, int portno)
469 struct umct_softc *sc;
473 if (sc->sc_ucom.sc_dying) {
477 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
478 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
479 err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
480 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_intr_buf,
481 sc->sc_isize, umct_intr, UMCT_INTR_INTERVAL);
483 printf("%s: cannot open interrupt pipe (addr %d)\n",
484 USBDEVNAME(sc->sc_ucom.sc_dev),
486 free(sc->sc_intr_buf, M_USBDEV);
495 umct_close(void *addr, int portno)
497 struct umct_softc *sc;
501 if (sc->sc_ucom.sc_dying)
504 if (sc->sc_intr_pipe != NULL) {
505 err = usbd_abort_pipe(sc->sc_intr_pipe);
507 printf("%s: abort interrupt pipe failed: %s\n",
508 USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
509 err = usbd_close_pipe(sc->sc_intr_pipe);
511 printf("%s: close interrupt pipe failed: %s\n",
512 USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
513 free(sc->sc_intr_buf, M_USBDEV);
514 sc->sc_intr_pipe = NULL;