5 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
7 * Copyright (c) 2003 Scott Long
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Driver for the MCT (Magic Control Technology) USB-RS232 Converter.
35 * Based on the superb documentation from the linux mct_u232 driver by
36 * Wolfgang Grandeggar <wolfgang@cec.ch>.
37 * This device smells a lot like the Belkin F5U103, except that it has
38 * suffered some mild brain-damage. This driver is based off of the ubsa.c
39 * driver from Alexander Kabaev <kan@FreeBSD.org>. Merging the two together
40 * might be useful, though the subtle differences might lead to lots of
45 * NOTE: all function names beginning like "umct_cfg_" can only
46 * be called from within the config thread function !
49 #include <sys/stdint.h>
50 #include <sys/stddef.h>
51 #include <sys/param.h>
52 #include <sys/queue.h>
53 #include <sys/types.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
57 #include <sys/module.h>
59 #include <sys/mutex.h>
60 #include <sys/condvar.h>
61 #include <sys/sysctl.h>
63 #include <sys/unistd.h>
64 #include <sys/callout.h>
65 #include <sys/malloc.h>
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
73 #define USB_DEBUG_VAR usb_debug
74 #include <dev/usb/usb_debug.h>
75 #include <dev/usb/usb_process.h>
77 #include <dev/usb/serial/usb_serial.h>
79 /* The UMCT advertises the standard 8250 UART registers */
80 #define UMCT_GET_MSR 2 /* Get Modem Status Register */
81 #define UMCT_GET_MSR_SIZE 1
82 #define UMCT_GET_LCR 6 /* Get Line Control Register */
83 #define UMCT_GET_LCR_SIZE 1
84 #define UMCT_SET_BAUD 5 /* Set the Baud Rate Divisor */
85 #define UMCT_SET_BAUD_SIZE 4
86 #define UMCT_SET_LCR 7 /* Set Line Control Register */
87 #define UMCT_SET_LCR_SIZE 1
88 #define UMCT_SET_MCR 10 /* Set Modem Control Register */
89 #define UMCT_SET_MCR_SIZE 1
91 #define UMCT_MSR_CTS_CHG 0x01
92 #define UMCT_MSR_DSR_CHG 0x02
93 #define UMCT_MSR_RI_CHG 0x04
94 #define UMCT_MSR_CD_CHG 0x08
95 #define UMCT_MSR_CTS 0x10
96 #define UMCT_MSR_RTS 0x20
97 #define UMCT_MSR_RI 0x40
98 #define UMCT_MSR_CD 0x80
100 #define UMCT_INTR_INTERVAL 100
101 #define UMCT_IFACE_INDEX 0
102 #define UMCT_CONFIG_INDEX 0
112 struct ucom_super_softc sc_super_ucom;
113 struct ucom_softc sc_ucom;
115 struct usb_device *sc_udev;
116 struct usb_xfer *sc_xfer[UMCT_N_TRANSFER];
121 uint16_t sc_obufsize;
133 static device_probe_t umct_probe;
134 static device_attach_t umct_attach;
135 static device_detach_t umct_detach;
136 static void umct_free_softc(struct umct_softc *);
138 static usb_callback_t umct_intr_callback;
139 static usb_callback_t umct_intr_callback_sub;
140 static usb_callback_t umct_read_callback;
141 static usb_callback_t umct_read_callback_sub;
142 static usb_callback_t umct_write_callback;
144 static void umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
145 uint16_t len, uint32_t value);
146 static void umct_free(struct ucom_softc *);
147 static void umct_cfg_get_status(struct ucom_softc *, uint8_t *,
149 static void umct_cfg_set_break(struct ucom_softc *, uint8_t);
150 static void umct_cfg_set_dtr(struct ucom_softc *, uint8_t);
151 static void umct_cfg_set_rts(struct ucom_softc *, uint8_t);
152 static uint8_t umct_calc_baud(uint32_t);
153 static int umct_pre_param(struct ucom_softc *, struct termios *);
154 static void umct_cfg_param(struct ucom_softc *, struct termios *);
155 static void umct_start_read(struct ucom_softc *);
156 static void umct_stop_read(struct ucom_softc *);
157 static void umct_start_write(struct ucom_softc *);
158 static void umct_stop_write(struct ucom_softc *);
159 static void umct_poll(struct ucom_softc *ucom);
161 static const struct usb_config umct_config[UMCT_N_TRANSFER] = {
162 [UMCT_BULK_DT_WR] = {
164 .endpoint = UE_ADDR_ANY,
165 .direction = UE_DIR_OUT,
166 .bufsize = 0, /* use wMaxPacketSize */
167 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
168 .callback = &umct_write_callback,
171 [UMCT_BULK_DT_RD] = {
172 .type = UE_INTERRUPT,
173 .endpoint = UE_ADDR_ANY,
174 .direction = UE_DIR_IN,
175 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
176 .bufsize = 0, /* use wMaxPacketSize */
177 .callback = &umct_read_callback,
178 .ep_index = 0, /* first interrupt endpoint */
181 [UMCT_INTR_DT_RD] = {
182 .type = UE_INTERRUPT,
183 .endpoint = UE_ADDR_ANY,
184 .direction = UE_DIR_IN,
185 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
186 .bufsize = 0, /* use wMaxPacketSize */
187 .callback = &umct_intr_callback,
188 .ep_index = 1, /* second interrupt endpoint */
192 static const struct ucom_callback umct_callback = {
193 .ucom_cfg_get_status = &umct_cfg_get_status,
194 .ucom_cfg_set_dtr = &umct_cfg_set_dtr,
195 .ucom_cfg_set_rts = &umct_cfg_set_rts,
196 .ucom_cfg_set_break = &umct_cfg_set_break,
197 .ucom_cfg_param = &umct_cfg_param,
198 .ucom_pre_param = &umct_pre_param,
199 .ucom_start_read = &umct_start_read,
200 .ucom_stop_read = &umct_stop_read,
201 .ucom_start_write = &umct_start_write,
202 .ucom_stop_write = &umct_stop_write,
203 .ucom_poll = &umct_poll,
204 .ucom_free = &umct_free,
207 static const STRUCT_USB_HOST_ID umct_devs[] = {
208 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232, 0)},
209 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232, 0)},
210 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232, 0)},
211 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109, 0)},
212 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U409, 0)},
215 static device_method_t umct_methods[] = {
216 DEVMETHOD(device_probe, umct_probe),
217 DEVMETHOD(device_attach, umct_attach),
218 DEVMETHOD(device_detach, umct_detach),
222 static devclass_t umct_devclass;
224 static driver_t umct_driver = {
226 .methods = umct_methods,
227 .size = sizeof(struct umct_softc),
230 DRIVER_MODULE(umct, uhub, umct_driver, umct_devclass, NULL, 0);
231 MODULE_DEPEND(umct, ucom, 1, 1, 1);
232 MODULE_DEPEND(umct, usb, 1, 1, 1);
233 MODULE_VERSION(umct, 1);
234 USB_PNP_HOST_INFO(umct_devs);
237 umct_probe(device_t dev)
239 struct usb_attach_arg *uaa = device_get_ivars(dev);
241 if (uaa->usb_mode != USB_MODE_HOST) {
244 if (uaa->info.bConfigIndex != UMCT_CONFIG_INDEX) {
247 if (uaa->info.bIfaceIndex != UMCT_IFACE_INDEX) {
250 return (usbd_lookup_id_by_uaa(umct_devs, sizeof(umct_devs), uaa));
254 umct_attach(device_t dev)
256 struct usb_attach_arg *uaa = device_get_ivars(dev);
257 struct umct_softc *sc = device_get_softc(dev);
262 sc->sc_udev = uaa->device;
263 sc->sc_unit = device_get_unit(dev);
265 device_set_usb_desc(dev);
266 mtx_init(&sc->sc_mtx, "umct", NULL, MTX_DEF);
267 ucom_ref(&sc->sc_super_ucom);
269 sc->sc_iface_no = uaa->info.bIfaceNum;
271 iface_index = UMCT_IFACE_INDEX;
272 error = usbd_transfer_setup(uaa->device, &iface_index,
273 sc->sc_xfer, umct_config, UMCT_N_TRANSFER, sc, &sc->sc_mtx);
276 device_printf(dev, "allocating USB "
277 "transfers failed\n");
282 * The real bulk-in endpoint is also marked as an interrupt.
283 * The only way to differentiate it from the real interrupt
284 * endpoint is to look at the wMaxPacketSize field.
286 maxp = usbd_xfer_max_framelen(sc->sc_xfer[UMCT_BULK_DT_RD]);
288 /* guessed wrong - switch around endpoints */
290 struct usb_xfer *temp = sc->sc_xfer[UMCT_INTR_DT_RD];
292 sc->sc_xfer[UMCT_INTR_DT_RD] = sc->sc_xfer[UMCT_BULK_DT_RD];
293 sc->sc_xfer[UMCT_BULK_DT_RD] = temp;
297 sc->sc_obufsize = usbd_xfer_max_len(sc->sc_xfer[UMCT_BULK_DT_WR]);
299 if (uaa->info.idProduct == USB_PRODUCT_MCT_SITECOM_USB232) {
300 if (sc->sc_obufsize > 16) {
301 sc->sc_obufsize = 16;
304 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
305 &umct_callback, &sc->sc_mtx);
309 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
311 return (0); /* success */
315 return (ENXIO); /* failure */
319 umct_detach(device_t dev)
321 struct umct_softc *sc = device_get_softc(dev);
323 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
324 usbd_transfer_unsetup(sc->sc_xfer, UMCT_N_TRANSFER);
326 device_claim_softc(dev);
333 UCOM_UNLOAD_DRAIN(umct);
336 umct_free_softc(struct umct_softc *sc)
338 if (ucom_unref(&sc->sc_super_ucom)) {
339 mtx_destroy(&sc->sc_mtx);
340 device_free_softc(sc);
345 umct_free(struct ucom_softc *ucom)
347 umct_free_softc(ucom->sc_parent);
351 umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
352 uint16_t len, uint32_t value)
354 struct usb_device_request req;
360 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
361 req.bRequest = request;
362 USETW(req.wValue, 0);
363 req.wIndex[0] = sc->sc_iface_no;
365 USETW(req.wLength, len);
368 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
369 &req, temp, 0, 1000);
371 DPRINTFN(0, "device request failed, err=%s "
372 "(ignored)\n", usbd_errstr(err));
378 umct_intr_callback_sub(struct usb_xfer *xfer, usb_error_t error)
380 struct umct_softc *sc = usbd_xfer_softc(xfer);
381 struct usb_page_cache *pc;
385 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
387 switch (USB_GET_STATE(xfer)) {
388 case USB_ST_TRANSFERRED:
390 DPRINTF("too short message\n");
393 pc = usbd_xfer_get_frame(xfer, 0);
394 usbd_copy_out(pc, 0, buf, sizeof(buf));
397 * MSR bits need translation from ns16550 to SER_* values.
398 * LSR bits are ns16550 in hardware and ucom.
401 if (buf[0] & UMCT_MSR_CTS)
402 sc->sc_msr |= SER_CTS;
403 if (buf[0] & UMCT_MSR_CD)
404 sc->sc_msr |= SER_DCD;
405 if (buf[0] & UMCT_MSR_RI)
406 sc->sc_msr |= SER_RI;
407 if (buf[0] & UMCT_MSR_RTS)
408 sc->sc_msr |= SER_DSR;
411 ucom_status_change(&sc->sc_ucom);
415 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
416 usbd_transfer_submit(xfer);
420 if (error != USB_ERR_CANCELLED) {
421 /* try to clear stall first */
422 usbd_xfer_set_stall(xfer);
430 umct_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
432 struct umct_softc *sc = ucom->sc_parent;
439 umct_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
441 struct umct_softc *sc = ucom->sc_parent;
448 umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
452 umct_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
454 struct umct_softc *sc = ucom->sc_parent;
461 umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
465 umct_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
467 struct umct_softc *sc = ucom->sc_parent;
474 umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
478 umct_calc_baud(uint32_t baud)
509 umct_pre_param(struct ucom_softc *ucom, struct termios *t)
511 return (0); /* we accept anything */
515 umct_cfg_param(struct ucom_softc *ucom, struct termios *t)
517 struct umct_softc *sc = ucom->sc_parent;
520 value = umct_calc_baud(t->c_ospeed);
521 umct_cfg_do_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
523 value = (sc->sc_lcr & 0x40);
525 switch (t->c_cflag & CSIZE) {
541 value |= (t->c_cflag & CSTOPB) ? 0x4 : 0;
542 if (t->c_cflag & PARENB) {
544 value |= (t->c_cflag & PARODD) ? 0x0 : 0x10;
547 * XXX There doesn't seem to be a way to tell the device
548 * to use flow control.
552 umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
556 umct_start_read(struct ucom_softc *ucom)
558 struct umct_softc *sc = ucom->sc_parent;
560 /* start interrupt endpoint */
561 usbd_transfer_start(sc->sc_xfer[UMCT_INTR_DT_RD]);
563 /* start read endpoint */
564 usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_RD]);
568 umct_stop_read(struct ucom_softc *ucom)
570 struct umct_softc *sc = ucom->sc_parent;
572 /* stop interrupt endpoint */
573 usbd_transfer_stop(sc->sc_xfer[UMCT_INTR_DT_RD]);
575 /* stop read endpoint */
576 usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_RD]);
580 umct_start_write(struct ucom_softc *ucom)
582 struct umct_softc *sc = ucom->sc_parent;
584 usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_WR]);
588 umct_stop_write(struct ucom_softc *ucom)
590 struct umct_softc *sc = ucom->sc_parent;
592 usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_WR]);
596 umct_read_callback(struct usb_xfer *xfer, usb_error_t error)
598 struct umct_softc *sc = usbd_xfer_softc(xfer);
601 umct_intr_callback_sub(xfer, error);
603 umct_read_callback_sub(xfer, error);
607 umct_intr_callback(struct usb_xfer *xfer, usb_error_t error)
609 struct umct_softc *sc = usbd_xfer_softc(xfer);
612 umct_read_callback_sub(xfer, error);
614 umct_intr_callback_sub(xfer, error);
618 umct_write_callback(struct usb_xfer *xfer, usb_error_t error)
620 struct umct_softc *sc = usbd_xfer_softc(xfer);
621 struct usb_page_cache *pc;
624 switch (USB_GET_STATE(xfer)) {
626 case USB_ST_TRANSFERRED:
628 pc = usbd_xfer_get_frame(xfer, 0);
629 if (ucom_get_data(&sc->sc_ucom, pc, 0,
630 sc->sc_obufsize, &actlen)) {
631 usbd_xfer_set_frame_len(xfer, 0, actlen);
632 usbd_transfer_submit(xfer);
637 if (error != USB_ERR_CANCELLED) {
638 /* try to clear stall first */
639 usbd_xfer_set_stall(xfer);
647 umct_read_callback_sub(struct usb_xfer *xfer, usb_error_t error)
649 struct umct_softc *sc = usbd_xfer_softc(xfer);
650 struct usb_page_cache *pc;
653 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
655 switch (USB_GET_STATE(xfer)) {
656 case USB_ST_TRANSFERRED:
657 pc = usbd_xfer_get_frame(xfer, 0);
658 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
662 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
663 usbd_transfer_submit(xfer);
667 if (error != USB_ERR_CANCELLED) {
668 /* try to clear stall first */
669 usbd_xfer_set_stall(xfer);
677 umct_poll(struct ucom_softc *ucom)
679 struct umct_softc *sc = ucom->sc_parent;
680 usbd_transfer_poll(sc->sc_xfer, UMCT_N_TRANSFER);