5 * Copyright (c) 2003 Scott Long
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
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
43 * NOTE: all function names beginning like "umct_cfg_" can only
44 * be called from within the config thread function !
47 #include <sys/stdint.h>
48 #include <sys/stddef.h>
49 #include <sys/param.h>
50 #include <sys/queue.h>
51 #include <sys/types.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
55 #include <sys/linker_set.h>
56 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/sysctl.h>
62 #include <sys/unistd.h>
63 #include <sys/callout.h>
64 #include <sys/malloc.h>
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
69 #include <dev/usb/usbdi_util.h>
72 #define USB_DEBUG_VAR usb_debug
73 #include <dev/usb/usb_debug.h>
74 #include <dev/usb/usb_process.h>
76 #include <dev/usb/serial/usb_serial.h>
78 /* The UMCT advertises the standard 8250 UART registers */
79 #define UMCT_GET_MSR 2 /* Get Modem Status Register */
80 #define UMCT_GET_MSR_SIZE 1
81 #define UMCT_GET_LCR 6 /* Get Line Control Register */
82 #define UMCT_GET_LCR_SIZE 1
83 #define UMCT_SET_BAUD 5 /* Set the Baud Rate Divisor */
84 #define UMCT_SET_BAUD_SIZE 4
85 #define UMCT_SET_LCR 7 /* Set Line Control Register */
86 #define UMCT_SET_LCR_SIZE 1
87 #define UMCT_SET_MCR 10 /* Set Modem Control Register */
88 #define UMCT_SET_MCR_SIZE 1
90 #define UMCT_INTR_INTERVAL 100
91 #define UMCT_IFACE_INDEX 0
92 #define UMCT_CONFIG_INDEX 0
102 struct ucom_super_softc sc_super_ucom;
103 struct ucom_softc sc_ucom;
105 struct usb_device *sc_udev;
106 struct usb_xfer *sc_xfer[UMCT_N_TRANSFER];
111 uint16_t sc_obufsize;
123 static device_probe_t umct_probe;
124 static device_attach_t umct_attach;
125 static device_detach_t umct_detach;
127 static usb_callback_t umct_intr_callback;
128 static usb_callback_t umct_write_callback;
129 static usb_callback_t umct_read_callback;
131 static void umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
132 uint16_t len, uint32_t value);
133 static void umct_cfg_get_status(struct ucom_softc *, uint8_t *,
135 static void umct_cfg_set_break(struct ucom_softc *, uint8_t);
136 static void umct_cfg_set_dtr(struct ucom_softc *, uint8_t);
137 static void umct_cfg_set_rts(struct ucom_softc *, uint8_t);
138 static uint8_t umct_calc_baud(uint32_t);
139 static int umct_pre_param(struct ucom_softc *, struct termios *);
140 static void umct_cfg_param(struct ucom_softc *, struct termios *);
141 static void umct_start_read(struct ucom_softc *);
142 static void umct_stop_read(struct ucom_softc *);
143 static void umct_start_write(struct ucom_softc *);
144 static void umct_stop_write(struct ucom_softc *);
146 static const struct usb_config umct_config[UMCT_N_TRANSFER] = {
148 [UMCT_BULK_DT_WR] = {
150 .endpoint = UE_ADDR_ANY,
151 .direction = UE_DIR_OUT,
152 .bufsize = 0, /* use wMaxPacketSize */
153 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
154 .callback = &umct_write_callback,
157 [UMCT_BULK_DT_RD] = {
158 .type = UE_INTERRUPT,
159 .endpoint = UE_ADDR_ANY,
160 .direction = UE_DIR_IN,
161 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
162 .bufsize = 0, /* use wMaxPacketSize */
163 .callback = &umct_read_callback,
164 .ep_index = 0, /* first interrupt endpoint */
167 [UMCT_INTR_DT_RD] = {
168 .type = UE_INTERRUPT,
169 .endpoint = UE_ADDR_ANY,
170 .direction = UE_DIR_IN,
171 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
172 .bufsize = 0, /* use wMaxPacketSize */
173 .callback = &umct_intr_callback,
174 .ep_index = 1, /* second interrupt endpoint */
178 static const struct ucom_callback umct_callback = {
179 .ucom_cfg_get_status = &umct_cfg_get_status,
180 .ucom_cfg_set_dtr = &umct_cfg_set_dtr,
181 .ucom_cfg_set_rts = &umct_cfg_set_rts,
182 .ucom_cfg_set_break = &umct_cfg_set_break,
183 .ucom_cfg_param = &umct_cfg_param,
184 .ucom_pre_param = &umct_pre_param,
185 .ucom_start_read = &umct_start_read,
186 .ucom_stop_read = &umct_stop_read,
187 .ucom_start_write = &umct_start_write,
188 .ucom_stop_write = &umct_stop_write,
191 static const struct usb_device_id umct_devs[] = {
192 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232, 0)},
193 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232, 0)},
194 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232, 0)},
195 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109, 0)},
196 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U409, 0)},
199 static device_method_t umct_methods[] = {
200 DEVMETHOD(device_probe, umct_probe),
201 DEVMETHOD(device_attach, umct_attach),
202 DEVMETHOD(device_detach, umct_detach),
206 static devclass_t umct_devclass;
208 static driver_t umct_driver = {
210 .methods = umct_methods,
211 .size = sizeof(struct umct_softc),
214 DRIVER_MODULE(umct, uhub, umct_driver, umct_devclass, NULL, 0);
215 MODULE_DEPEND(umct, ucom, 1, 1, 1);
216 MODULE_DEPEND(umct, usb, 1, 1, 1);
219 umct_probe(device_t dev)
221 struct usb_attach_arg *uaa = device_get_ivars(dev);
223 if (uaa->usb_mode != USB_MODE_HOST) {
226 if (uaa->info.bConfigIndex != UMCT_CONFIG_INDEX) {
229 if (uaa->info.bIfaceIndex != UMCT_IFACE_INDEX) {
232 return (usbd_lookup_id_by_uaa(umct_devs, sizeof(umct_devs), uaa));
236 umct_attach(device_t dev)
238 struct usb_attach_arg *uaa = device_get_ivars(dev);
239 struct umct_softc *sc = device_get_softc(dev);
244 sc->sc_udev = uaa->device;
245 sc->sc_unit = device_get_unit(dev);
247 device_set_usb_desc(dev);
248 mtx_init(&sc->sc_mtx, "umct", NULL, MTX_DEF);
250 snprintf(sc->sc_name, sizeof(sc->sc_name),
251 "%s", device_get_nameunit(dev));
253 sc->sc_iface_no = uaa->info.bIfaceNum;
255 iface_index = UMCT_IFACE_INDEX;
256 error = usbd_transfer_setup(uaa->device, &iface_index,
257 sc->sc_xfer, umct_config, UMCT_N_TRANSFER, sc, &sc->sc_mtx);
260 device_printf(dev, "allocating USB "
261 "transfers failed!\n");
265 * The real bulk-in endpoint is also marked as an interrupt.
266 * The only way to differentiate it from the real interrupt
267 * endpoint is to look at the wMaxPacketSize field.
270 maxp = UGETW(sc->sc_xfer[UMCT_BULK_DT_RD]->endpoint->edesc->wMaxPacketSize);
273 /* guessed wrong - switch around endpoints */
275 struct usb_xfer *temp = sc->sc_xfer[UMCT_INTR_DT_RD];
277 sc->sc_xfer[UMCT_INTR_DT_RD] = sc->sc_xfer[UMCT_BULK_DT_RD];
278 sc->sc_xfer[UMCT_BULK_DT_RD] = temp;
280 sc->sc_xfer[UMCT_BULK_DT_RD]->callback = &umct_read_callback;
281 sc->sc_xfer[UMCT_INTR_DT_RD]->callback = &umct_intr_callback;
284 sc->sc_obufsize = usbd_xfer_max_len(sc->sc_xfer[UMCT_BULK_DT_WR]);
286 if (uaa->info.idProduct == USB_PRODUCT_MCT_SITECOM_USB232) {
287 if (sc->sc_obufsize > 16) {
288 sc->sc_obufsize = 16;
291 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
292 &umct_callback, &sc->sc_mtx);
296 return (0); /* success */
300 return (ENXIO); /* failure */
304 umct_detach(device_t dev)
306 struct umct_softc *sc = device_get_softc(dev);
308 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
309 usbd_transfer_unsetup(sc->sc_xfer, UMCT_N_TRANSFER);
310 mtx_destroy(&sc->sc_mtx);
316 umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
317 uint16_t len, uint32_t value)
319 struct usb_device_request req;
325 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
326 req.bRequest = request;
327 USETW(req.wValue, 0);
328 req.wIndex[0] = sc->sc_iface_no;
330 USETW(req.wLength, len);
333 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
334 &req, temp, 0, 1000);
336 DPRINTFN(0, "device request failed, err=%s "
337 "(ignored)\n", usbd_errstr(err));
343 umct_intr_callback(struct usb_xfer *xfer, usb_error_t error)
345 struct umct_softc *sc = usbd_xfer_softc(xfer);
346 struct usb_page_cache *pc;
350 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
352 switch (USB_GET_STATE(xfer)) {
353 case USB_ST_TRANSFERRED:
355 DPRINTF("too short message\n");
358 pc = usbd_xfer_get_frame(xfer, 0);
359 usbd_copy_out(pc, 0, buf, sizeof(buf));
364 ucom_status_change(&sc->sc_ucom);
368 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
369 usbd_transfer_submit(xfer);
373 if (error != USB_ERR_CANCELLED) {
374 /* try to clear stall first */
375 usbd_xfer_set_stall(xfer);
383 umct_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
385 struct umct_softc *sc = ucom->sc_parent;
392 umct_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
394 struct umct_softc *sc = ucom->sc_parent;
401 umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
405 umct_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
407 struct umct_softc *sc = ucom->sc_parent;
414 umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
418 umct_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
420 struct umct_softc *sc = ucom->sc_parent;
427 umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
431 umct_calc_baud(uint32_t baud)
434 case B300:return (0x1);
461 umct_pre_param(struct ucom_softc *ucom, struct termios *t)
463 return (0); /* we accept anything */
467 umct_cfg_param(struct ucom_softc *ucom, struct termios *t)
469 struct umct_softc *sc = ucom->sc_parent;
472 value = umct_calc_baud(t->c_ospeed);
473 umct_cfg_do_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
475 value = (sc->sc_lcr & 0x40);
477 switch (t->c_cflag & CSIZE) {
493 value |= (t->c_cflag & CSTOPB) ? 0x4 : 0;
494 if (t->c_cflag & PARENB) {
496 value |= (t->c_cflag & PARODD) ? 0x0 : 0x10;
499 * XXX There doesn't seem to be a way to tell the device
500 * to use flow control.
504 umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
508 umct_start_read(struct ucom_softc *ucom)
510 struct umct_softc *sc = ucom->sc_parent;
512 /* start interrupt endpoint */
513 usbd_transfer_start(sc->sc_xfer[UMCT_INTR_DT_RD]);
515 /* start read endpoint */
516 usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_RD]);
520 umct_stop_read(struct ucom_softc *ucom)
522 struct umct_softc *sc = ucom->sc_parent;
524 /* stop interrupt endpoint */
525 usbd_transfer_stop(sc->sc_xfer[UMCT_INTR_DT_RD]);
527 /* stop read endpoint */
528 usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_RD]);
532 umct_start_write(struct ucom_softc *ucom)
534 struct umct_softc *sc = ucom->sc_parent;
536 usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_WR]);
540 umct_stop_write(struct ucom_softc *ucom)
542 struct umct_softc *sc = ucom->sc_parent;
544 usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_WR]);
548 umct_write_callback(struct usb_xfer *xfer, usb_error_t error)
550 struct umct_softc *sc = usbd_xfer_softc(xfer);
551 struct usb_page_cache *pc;
554 switch (USB_GET_STATE(xfer)) {
556 case USB_ST_TRANSFERRED:
558 pc = usbd_xfer_get_frame(xfer, 0);
559 if (ucom_get_data(&sc->sc_ucom, pc, 0,
560 sc->sc_obufsize, &actlen)) {
562 usbd_xfer_set_frame_len(xfer, 0, actlen);
563 usbd_transfer_submit(xfer);
568 if (error != USB_ERR_CANCELLED) {
569 /* try to clear stall first */
570 usbd_xfer_set_stall(xfer);
578 umct_read_callback(struct usb_xfer *xfer, usb_error_t error)
580 struct umct_softc *sc = usbd_xfer_softc(xfer);
581 struct usb_page_cache *pc;
584 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
586 switch (USB_GET_STATE(xfer)) {
587 case USB_ST_TRANSFERRED:
588 pc = usbd_xfer_get_frame(xfer, 0);
589 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
593 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
594 usbd_transfer_submit(xfer);
598 if (error != USB_ERR_CANCELLED) {
599 /* try to clear stall first */
600 usbd_xfer_set_stall(xfer);