1#include <sys/cdefs.h>
2__FBSDID("$FreeBSD: head/sys/dev/usb/serial/umct.c 199816 2009-11-26 00:43:17Z thompsa $");
3
4/*-
5 * Copyright (c) 2003 Scott Long
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 *
29 */
30
31/*
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
39 * #ifdef's.
40 */
41
42/*
43 * NOTE: all function names beginning like "umct_cfg_" can only
44 * be called from within the config thread function !
45 */
46
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>
54#include <sys/bus.h>
55#include <sys/linker_set.h>
56#include <sys/module.h>
57#include <sys/lock.h>
58#include <sys/mutex.h>
59#include <sys/condvar.h>
60#include <sys/sysctl.h>
61#include <sys/sx.h>
62#include <sys/unistd.h>
63#include <sys/callout.h>
64#include <sys/malloc.h>
65#include <sys/priv.h>
66
67#include <dev/usb/usb.h>
68#include <dev/usb/usbdi.h>
69#include <dev/usb/usbdi_util.h>
70#include "usbdevs.h"
71
72#define USB_DEBUG_VAR usb_debug
73#include <dev/usb/usb_debug.h>
74#include <dev/usb/usb_process.h>
75
76#include <dev/usb/serial/usb_serial.h>
77
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
89
90#define UMCT_INTR_INTERVAL 100
91#define UMCT_IFACE_INDEX 0
92#define UMCT_CONFIG_INDEX 0
93
94enum {
95 UMCT_BULK_DT_WR,
96 UMCT_BULK_DT_RD,
97 UMCT_INTR_DT_RD,
98 UMCT_N_TRANSFER,
99};
100
101struct umct_softc {
102 struct ucom_super_softc sc_super_ucom;
103 struct ucom_softc sc_ucom;
104
105 struct usb_device *sc_udev;
106 struct usb_xfer *sc_xfer[UMCT_N_TRANSFER];
107 struct mtx sc_mtx;
108
109 uint32_t sc_unit;
110
111 uint16_t sc_obufsize;
112
113 uint8_t sc_lsr;
114 uint8_t sc_msr;
115 uint8_t sc_lcr;
116 uint8_t sc_mcr;
117 uint8_t sc_iface_no;
118 uint8_t sc_swap_cb;
119 uint8_t sc_name[16];
120};
121
122/* prototypes */
123
124static device_probe_t umct_probe;
125static device_attach_t umct_attach;
126static device_detach_t umct_detach;
127
128static usb_callback_t umct_intr_callback;
129static usb_callback_t umct_intr_callback_sub;
130static usb_callback_t umct_read_callback;
131static usb_callback_t umct_read_callback_sub;
132static usb_callback_t umct_write_callback;
133
134static void umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
135 uint16_t len, uint32_t value);
136static void umct_cfg_get_status(struct ucom_softc *, uint8_t *,
137 uint8_t *);
138static void umct_cfg_set_break(struct ucom_softc *, uint8_t);
139static void umct_cfg_set_dtr(struct ucom_softc *, uint8_t);
140static void umct_cfg_set_rts(struct ucom_softc *, uint8_t);
141static uint8_t umct_calc_baud(uint32_t);
142static int umct_pre_param(struct ucom_softc *, struct termios *);
143static void umct_cfg_param(struct ucom_softc *, struct termios *);
144static void umct_start_read(struct ucom_softc *);
145static void umct_stop_read(struct ucom_softc *);
146static void umct_start_write(struct ucom_softc *);
147static void umct_stop_write(struct ucom_softc *);
148static void umct_poll(struct ucom_softc *ucom);
149
150static const struct usb_config umct_config[UMCT_N_TRANSFER] = {
151
152 [UMCT_BULK_DT_WR] = {
153 .type = UE_BULK,
154 .endpoint = UE_ADDR_ANY,
155 .direction = UE_DIR_OUT,
156 .bufsize = 0, /* use wMaxPacketSize */
157 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
158 .callback = &umct_write_callback,
159 },
160
161 [UMCT_BULK_DT_RD] = {
162 .type = UE_INTERRUPT,
163 .endpoint = UE_ADDR_ANY,
164 .direction = UE_DIR_IN,
165 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
166 .bufsize = 0, /* use wMaxPacketSize */
167 .callback = &umct_read_callback,
168 .ep_index = 0, /* first interrupt endpoint */
169 },
170
171 [UMCT_INTR_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_intr_callback,
178 .ep_index = 1, /* second interrupt endpoint */
179 },
180};
181
182static const struct ucom_callback umct_callback = {
183 .ucom_cfg_get_status = &umct_cfg_get_status,
184 .ucom_cfg_set_dtr = &umct_cfg_set_dtr,
185 .ucom_cfg_set_rts = &umct_cfg_set_rts,
186 .ucom_cfg_set_break = &umct_cfg_set_break,
187 .ucom_cfg_param = &umct_cfg_param,
188 .ucom_pre_param = &umct_pre_param,
189 .ucom_start_read = &umct_start_read,
190 .ucom_stop_read = &umct_stop_read,
191 .ucom_start_write = &umct_start_write,
192 .ucom_stop_write = &umct_stop_write,
193 .ucom_poll = &umct_poll,
194};
195
196static const struct usb_device_id umct_devs[] = {
197 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232, 0)},
198 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232, 0)},
199 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232, 0)},
200 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109, 0)},
201 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U409, 0)},
202};
203
204static device_method_t umct_methods[] = {
205 DEVMETHOD(device_probe, umct_probe),
206 DEVMETHOD(device_attach, umct_attach),
207 DEVMETHOD(device_detach, umct_detach),
208 {0, 0}
209};
210
211static devclass_t umct_devclass;
212
213static driver_t umct_driver = {
214 .name = "umct",
215 .methods = umct_methods,
216 .size = sizeof(struct umct_softc),
217};
218
219DRIVER_MODULE(umct, uhub, umct_driver, umct_devclass, NULL, 0);
220MODULE_DEPEND(umct, ucom, 1, 1, 1);
221MODULE_DEPEND(umct, usb, 1, 1, 1);
222
223static int
224umct_probe(device_t dev)
225{
226 struct usb_attach_arg *uaa = device_get_ivars(dev);
227
228 if (uaa->usb_mode != USB_MODE_HOST) {
229 return (ENXIO);
230 }
231 if (uaa->info.bConfigIndex != UMCT_CONFIG_INDEX) {
232 return (ENXIO);
233 }
234 if (uaa->info.bIfaceIndex != UMCT_IFACE_INDEX) {
235 return (ENXIO);
236 }
237 return (usbd_lookup_id_by_uaa(umct_devs, sizeof(umct_devs), uaa));
238}
239
240static int
241umct_attach(device_t dev)
242{
243 struct usb_attach_arg *uaa = device_get_ivars(dev);
244 struct umct_softc *sc = device_get_softc(dev);
245 int32_t error;
246 uint16_t maxp;
247 uint8_t iface_index;
248
249 sc->sc_udev = uaa->device;
250 sc->sc_unit = device_get_unit(dev);
251
252 device_set_usb_desc(dev);
253 mtx_init(&sc->sc_mtx, "umct", NULL, MTX_DEF);
254
255 snprintf(sc->sc_name, sizeof(sc->sc_name),
256 "%s", device_get_nameunit(dev));
257
258 sc->sc_iface_no = uaa->info.bIfaceNum;
259
260 iface_index = UMCT_IFACE_INDEX;
261 error = usbd_transfer_setup(uaa->device, &iface_index,
262 sc->sc_xfer, umct_config, UMCT_N_TRANSFER, sc, &sc->sc_mtx);
263
264 if (error) {
265 device_printf(dev, "allocating USB "
266 "transfers failed\n");
267 goto detach;
268 }
269
270 /*
271 * The real bulk-in endpoint is also marked as an interrupt.
272 * The only way to differentiate it from the real interrupt
273 * endpoint is to look at the wMaxPacketSize field.
274 */
275 maxp = usbd_xfer_max_framelen(sc->sc_xfer[UMCT_BULK_DT_RD]);
276 if (maxp == 0x2) {
277
278 /* guessed wrong - switch around endpoints */
279
280 struct usb_xfer *temp = sc->sc_xfer[UMCT_INTR_DT_RD];
281
282 sc->sc_xfer[UMCT_INTR_DT_RD] = sc->sc_xfer[UMCT_BULK_DT_RD];
283 sc->sc_xfer[UMCT_BULK_DT_RD] = temp;
284 sc->sc_swap_cb = 1;
285 }
286
287 sc->sc_obufsize = usbd_xfer_max_len(sc->sc_xfer[UMCT_BULK_DT_WR]);
288
289 if (uaa->info.idProduct == USB_PRODUCT_MCT_SITECOM_USB232) {
290 if (sc->sc_obufsize > 16) {
291 sc->sc_obufsize = 16;
292 }
293 }
294 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
295 &umct_callback, &sc->sc_mtx);
296 if (error) {
297 goto detach;
298 }
299 return (0); /* success */
300
301detach:
302 umct_detach(dev);
303 return (ENXIO); /* failure */
304}
305
306static int
307umct_detach(device_t dev)
308{
309 struct umct_softc *sc = device_get_softc(dev);
310
311 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
312 usbd_transfer_unsetup(sc->sc_xfer, UMCT_N_TRANSFER);
313 mtx_destroy(&sc->sc_mtx);
314
315 return (0);
316}
317
318static void
319umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
320 uint16_t len, uint32_t value)
321{
322 struct usb_device_request req;
323 usb_error_t err;
324 uint8_t temp[4];
325
326 if (len > 4)
327 len = 4;
328 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
329 req.bRequest = request;
330 USETW(req.wValue, 0);
331 req.wIndex[0] = sc->sc_iface_no;
332 req.wIndex[1] = 0;
333 USETW(req.wLength, len);
334 USETDW(temp, value);
335
336 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
337 &req, temp, 0, 1000);
338 if (err) {
339 DPRINTFN(0, "device request failed, err=%s "
340 "(ignored)\n", usbd_errstr(err));
341 }
342 return;
343}
344
345static void
346umct_intr_callback_sub(struct usb_xfer *xfer, usb_error_t error)
347{
348 struct umct_softc *sc = usbd_xfer_softc(xfer);
349 struct usb_page_cache *pc;
350 uint8_t buf[2];
351 int actlen;
352
353 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
354
355 switch (USB_GET_STATE(xfer)) {
356 case USB_ST_TRANSFERRED:
357 if (actlen < 2) {
358 DPRINTF("too short message\n");
359 goto tr_setup;
360 }
361 pc = usbd_xfer_get_frame(xfer, 0);
362 usbd_copy_out(pc, 0, buf, sizeof(buf));
363
364 sc->sc_msr = buf[0];
365 sc->sc_lsr = buf[1];
366
367 ucom_status_change(&sc->sc_ucom);
368
369 case USB_ST_SETUP:
370tr_setup:
371 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
372 usbd_transfer_submit(xfer);
373 return;
374
375 default: /* Error */
376 if (error != USB_ERR_CANCELLED) {
377 /* try to clear stall first */
378 usbd_xfer_set_stall(xfer);
379 goto tr_setup;
380 }
381 return;
382 }
383}
384
385static void
386umct_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
387{
388 struct umct_softc *sc = ucom->sc_parent;
389
390 *lsr = sc->sc_lsr;
391 *msr = sc->sc_msr;
392}
393
394static void
395umct_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
396{
397 struct umct_softc *sc = ucom->sc_parent;
398
399 if (onoff)
400 sc->sc_lcr |= 0x40;
401 else
402 sc->sc_lcr &= ~0x40;
403
404 umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
405}
406
407static void
408umct_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
409{
410 struct umct_softc *sc = ucom->sc_parent;
411
412 if (onoff)
413 sc->sc_mcr |= 0x01;
414 else
415 sc->sc_mcr &= ~0x01;
416
417 umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
418}
419
420static void
421umct_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
422{
423 struct umct_softc *sc = ucom->sc_parent;
424
425 if (onoff)
426 sc->sc_mcr |= 0x02;
427 else
428 sc->sc_mcr &= ~0x02;
429
430 umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
431}
432
433static uint8_t
434umct_calc_baud(uint32_t baud)
435{
436 switch (baud) {
437 case B300:return (0x1);
438 case B600:
439 return (0x2);
440 case B1200:
441 return (0x3);
442 case B2400:
443 return (0x4);
444 case B4800:
445 return (0x6);
446 case B9600:
447 return (0x8);
448 case B19200:
449 return (0x9);
450 case B38400:
451 return (0xa);
452 case B57600:
453 return (0xb);
454 case 115200:
455 return (0xc);
456 case B0:
457 default:
458 break;
459 }
460 return (0x0);
461}
462
463static int
464umct_pre_param(struct ucom_softc *ucom, struct termios *t)
465{
466 return (0); /* we accept anything */
467}
468
469static void
470umct_cfg_param(struct ucom_softc *ucom, struct termios *t)
471{
472 struct umct_softc *sc = ucom->sc_parent;
473 uint32_t value;
474
475 value = umct_calc_baud(t->c_ospeed);
476 umct_cfg_do_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
477
478 value = (sc->sc_lcr & 0x40);
479
480 switch (t->c_cflag & CSIZE) {
481 case CS5:
482 value |= 0x0;
483 break;
484 case CS6:
485 value |= 0x1;
486 break;
487 case CS7:
488 value |= 0x2;
489 break;
490 default:
491 case CS8:
492 value |= 0x3;
493 break;
494 }
495
496 value |= (t->c_cflag & CSTOPB) ? 0x4 : 0;
497 if (t->c_cflag & PARENB) {
498 value |= 0x8;
499 value |= (t->c_cflag & PARODD) ? 0x0 : 0x10;
500 }
501 /*
502 * XXX There doesn't seem to be a way to tell the device
503 * to use flow control.
504 */
505
506 sc->sc_lcr = value;
507 umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
508}
509
510static void
511umct_start_read(struct ucom_softc *ucom)
512{
513 struct umct_softc *sc = ucom->sc_parent;
514
515 /* start interrupt endpoint */
516 usbd_transfer_start(sc->sc_xfer[UMCT_INTR_DT_RD]);
517
518 /* start read endpoint */
519 usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_RD]);
520}
521
522static void
523umct_stop_read(struct ucom_softc *ucom)
524{
525 struct umct_softc *sc = ucom->sc_parent;
526
527 /* stop interrupt endpoint */
528 usbd_transfer_stop(sc->sc_xfer[UMCT_INTR_DT_RD]);
529
530 /* stop read endpoint */
531 usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_RD]);
532}
533
534static void
535umct_start_write(struct ucom_softc *ucom)
536{
537 struct umct_softc *sc = ucom->sc_parent;
538
539 usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_WR]);
540}
541
542static void
543umct_stop_write(struct ucom_softc *ucom)
544{
545 struct umct_softc *sc = ucom->sc_parent;
546
547 usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_WR]);
548}
549
550static void
551umct_read_callback(struct usb_xfer *xfer, usb_error_t error)
552{
553 struct umct_softc *sc = usbd_xfer_softc(xfer);
554
555 if (sc->sc_swap_cb)
556 umct_intr_callback_sub(xfer, error);
557 else
558 umct_read_callback_sub(xfer, error);
559}
560
561static void
562umct_intr_callback(struct usb_xfer *xfer, usb_error_t error)
563{
564 struct umct_softc *sc = usbd_xfer_softc(xfer);
565
566 if (sc->sc_swap_cb)
567 umct_read_callback_sub(xfer, error);
568 else
569 umct_intr_callback_sub(xfer, error);
570}
571
572static void
573umct_write_callback(struct usb_xfer *xfer, usb_error_t error)
574{
575 struct umct_softc *sc = usbd_xfer_softc(xfer);
576 struct usb_page_cache *pc;
577 uint32_t actlen;
578
579 switch (USB_GET_STATE(xfer)) {
580 case USB_ST_SETUP:
581 case USB_ST_TRANSFERRED:
582tr_setup:
583 pc = usbd_xfer_get_frame(xfer, 0);
584 if (ucom_get_data(&sc->sc_ucom, pc, 0,
585 sc->sc_obufsize, &actlen)) {
586
587 usbd_xfer_set_frame_len(xfer, 0, actlen);
588 usbd_transfer_submit(xfer);
589 }
590 return;
591
592 default: /* Error */
593 if (error != USB_ERR_CANCELLED) {
594 /* try to clear stall first */
595 usbd_xfer_set_stall(xfer);
596 goto tr_setup;
597 }
598 return;
599 }
600}
601
602static void
603umct_read_callback_sub(struct usb_xfer *xfer, usb_error_t error)
604{
605 struct umct_softc *sc = usbd_xfer_softc(xfer);
606 struct usb_page_cache *pc;
607 int actlen;
608
609 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
610
611 switch (USB_GET_STATE(xfer)) {
612 case USB_ST_TRANSFERRED:
613 pc = usbd_xfer_get_frame(xfer, 0);
614 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
615
616 case USB_ST_SETUP:
617tr_setup:
618 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
619 usbd_transfer_submit(xfer);
620 return;
621
622 default: /* Error */
623 if (error != USB_ERR_CANCELLED) {
624 /* try to clear stall first */
625 usbd_xfer_set_stall(xfer);
626 goto tr_setup;
627 }
628 return;
629 }
630}
631
632static void
633umct_poll(struct ucom_softc *ucom)
634{
635 struct umct_softc *sc = ucom->sc_parent;
636 usbd_transfer_poll(sc->sc_xfer, UMCT_N_TRANSFER);
637}
2__FBSDID("$FreeBSD: head/sys/dev/usb/serial/umct.c 199816 2009-11-26 00:43:17Z thompsa $");
3
4/*-
5 * Copyright (c) 2003 Scott Long
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 *
29 */
30
31/*
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
39 * #ifdef's.
40 */
41
42/*
43 * NOTE: all function names beginning like "umct_cfg_" can only
44 * be called from within the config thread function !
45 */
46
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>
54#include <sys/bus.h>
55#include <sys/linker_set.h>
56#include <sys/module.h>
57#include <sys/lock.h>
58#include <sys/mutex.h>
59#include <sys/condvar.h>
60#include <sys/sysctl.h>
61#include <sys/sx.h>
62#include <sys/unistd.h>
63#include <sys/callout.h>
64#include <sys/malloc.h>
65#include <sys/priv.h>
66
67#include <dev/usb/usb.h>
68#include <dev/usb/usbdi.h>
69#include <dev/usb/usbdi_util.h>
70#include "usbdevs.h"
71
72#define USB_DEBUG_VAR usb_debug
73#include <dev/usb/usb_debug.h>
74#include <dev/usb/usb_process.h>
75
76#include <dev/usb/serial/usb_serial.h>
77
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
89
90#define UMCT_INTR_INTERVAL 100
91#define UMCT_IFACE_INDEX 0
92#define UMCT_CONFIG_INDEX 0
93
94enum {
95 UMCT_BULK_DT_WR,
96 UMCT_BULK_DT_RD,
97 UMCT_INTR_DT_RD,
98 UMCT_N_TRANSFER,
99};
100
101struct umct_softc {
102 struct ucom_super_softc sc_super_ucom;
103 struct ucom_softc sc_ucom;
104
105 struct usb_device *sc_udev;
106 struct usb_xfer *sc_xfer[UMCT_N_TRANSFER];
107 struct mtx sc_mtx;
108
109 uint32_t sc_unit;
110
111 uint16_t sc_obufsize;
112
113 uint8_t sc_lsr;
114 uint8_t sc_msr;
115 uint8_t sc_lcr;
116 uint8_t sc_mcr;
117 uint8_t sc_iface_no;
118 uint8_t sc_swap_cb;
119 uint8_t sc_name[16];
120};
121
122/* prototypes */
123
124static device_probe_t umct_probe;
125static device_attach_t umct_attach;
126static device_detach_t umct_detach;
127
128static usb_callback_t umct_intr_callback;
129static usb_callback_t umct_intr_callback_sub;
130static usb_callback_t umct_read_callback;
131static usb_callback_t umct_read_callback_sub;
132static usb_callback_t umct_write_callback;
133
134static void umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
135 uint16_t len, uint32_t value);
136static void umct_cfg_get_status(struct ucom_softc *, uint8_t *,
137 uint8_t *);
138static void umct_cfg_set_break(struct ucom_softc *, uint8_t);
139static void umct_cfg_set_dtr(struct ucom_softc *, uint8_t);
140static void umct_cfg_set_rts(struct ucom_softc *, uint8_t);
141static uint8_t umct_calc_baud(uint32_t);
142static int umct_pre_param(struct ucom_softc *, struct termios *);
143static void umct_cfg_param(struct ucom_softc *, struct termios *);
144static void umct_start_read(struct ucom_softc *);
145static void umct_stop_read(struct ucom_softc *);
146static void umct_start_write(struct ucom_softc *);
147static void umct_stop_write(struct ucom_softc *);
148static void umct_poll(struct ucom_softc *ucom);
149
150static const struct usb_config umct_config[UMCT_N_TRANSFER] = {
151
152 [UMCT_BULK_DT_WR] = {
153 .type = UE_BULK,
154 .endpoint = UE_ADDR_ANY,
155 .direction = UE_DIR_OUT,
156 .bufsize = 0, /* use wMaxPacketSize */
157 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
158 .callback = &umct_write_callback,
159 },
160
161 [UMCT_BULK_DT_RD] = {
162 .type = UE_INTERRUPT,
163 .endpoint = UE_ADDR_ANY,
164 .direction = UE_DIR_IN,
165 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
166 .bufsize = 0, /* use wMaxPacketSize */
167 .callback = &umct_read_callback,
168 .ep_index = 0, /* first interrupt endpoint */
169 },
170
171 [UMCT_INTR_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_intr_callback,
178 .ep_index = 1, /* second interrupt endpoint */
179 },
180};
181
182static const struct ucom_callback umct_callback = {
183 .ucom_cfg_get_status = &umct_cfg_get_status,
184 .ucom_cfg_set_dtr = &umct_cfg_set_dtr,
185 .ucom_cfg_set_rts = &umct_cfg_set_rts,
186 .ucom_cfg_set_break = &umct_cfg_set_break,
187 .ucom_cfg_param = &umct_cfg_param,
188 .ucom_pre_param = &umct_pre_param,
189 .ucom_start_read = &umct_start_read,
190 .ucom_stop_read = &umct_stop_read,
191 .ucom_start_write = &umct_start_write,
192 .ucom_stop_write = &umct_stop_write,
193 .ucom_poll = &umct_poll,
194};
195
196static const struct usb_device_id umct_devs[] = {
197 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232, 0)},
198 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232, 0)},
199 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232, 0)},
200 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109, 0)},
201 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U409, 0)},
202};
203
204static device_method_t umct_methods[] = {
205 DEVMETHOD(device_probe, umct_probe),
206 DEVMETHOD(device_attach, umct_attach),
207 DEVMETHOD(device_detach, umct_detach),
208 {0, 0}
209};
210
211static devclass_t umct_devclass;
212
213static driver_t umct_driver = {
214 .name = "umct",
215 .methods = umct_methods,
216 .size = sizeof(struct umct_softc),
217};
218
219DRIVER_MODULE(umct, uhub, umct_driver, umct_devclass, NULL, 0);
220MODULE_DEPEND(umct, ucom, 1, 1, 1);
221MODULE_DEPEND(umct, usb, 1, 1, 1);
222
223static int
224umct_probe(device_t dev)
225{
226 struct usb_attach_arg *uaa = device_get_ivars(dev);
227
228 if (uaa->usb_mode != USB_MODE_HOST) {
229 return (ENXIO);
230 }
231 if (uaa->info.bConfigIndex != UMCT_CONFIG_INDEX) {
232 return (ENXIO);
233 }
234 if (uaa->info.bIfaceIndex != UMCT_IFACE_INDEX) {
235 return (ENXIO);
236 }
237 return (usbd_lookup_id_by_uaa(umct_devs, sizeof(umct_devs), uaa));
238}
239
240static int
241umct_attach(device_t dev)
242{
243 struct usb_attach_arg *uaa = device_get_ivars(dev);
244 struct umct_softc *sc = device_get_softc(dev);
245 int32_t error;
246 uint16_t maxp;
247 uint8_t iface_index;
248
249 sc->sc_udev = uaa->device;
250 sc->sc_unit = device_get_unit(dev);
251
252 device_set_usb_desc(dev);
253 mtx_init(&sc->sc_mtx, "umct", NULL, MTX_DEF);
254
255 snprintf(sc->sc_name, sizeof(sc->sc_name),
256 "%s", device_get_nameunit(dev));
257
258 sc->sc_iface_no = uaa->info.bIfaceNum;
259
260 iface_index = UMCT_IFACE_INDEX;
261 error = usbd_transfer_setup(uaa->device, &iface_index,
262 sc->sc_xfer, umct_config, UMCT_N_TRANSFER, sc, &sc->sc_mtx);
263
264 if (error) {
265 device_printf(dev, "allocating USB "
266 "transfers failed\n");
267 goto detach;
268 }
269
270 /*
271 * The real bulk-in endpoint is also marked as an interrupt.
272 * The only way to differentiate it from the real interrupt
273 * endpoint is to look at the wMaxPacketSize field.
274 */
275 maxp = usbd_xfer_max_framelen(sc->sc_xfer[UMCT_BULK_DT_RD]);
276 if (maxp == 0x2) {
277
278 /* guessed wrong - switch around endpoints */
279
280 struct usb_xfer *temp = sc->sc_xfer[UMCT_INTR_DT_RD];
281
282 sc->sc_xfer[UMCT_INTR_DT_RD] = sc->sc_xfer[UMCT_BULK_DT_RD];
283 sc->sc_xfer[UMCT_BULK_DT_RD] = temp;
284 sc->sc_swap_cb = 1;
285 }
286
287 sc->sc_obufsize = usbd_xfer_max_len(sc->sc_xfer[UMCT_BULK_DT_WR]);
288
289 if (uaa->info.idProduct == USB_PRODUCT_MCT_SITECOM_USB232) {
290 if (sc->sc_obufsize > 16) {
291 sc->sc_obufsize = 16;
292 }
293 }
294 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
295 &umct_callback, &sc->sc_mtx);
296 if (error) {
297 goto detach;
298 }
299 return (0); /* success */
300
301detach:
302 umct_detach(dev);
303 return (ENXIO); /* failure */
304}
305
306static int
307umct_detach(device_t dev)
308{
309 struct umct_softc *sc = device_get_softc(dev);
310
311 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
312 usbd_transfer_unsetup(sc->sc_xfer, UMCT_N_TRANSFER);
313 mtx_destroy(&sc->sc_mtx);
314
315 return (0);
316}
317
318static void
319umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
320 uint16_t len, uint32_t value)
321{
322 struct usb_device_request req;
323 usb_error_t err;
324 uint8_t temp[4];
325
326 if (len > 4)
327 len = 4;
328 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
329 req.bRequest = request;
330 USETW(req.wValue, 0);
331 req.wIndex[0] = sc->sc_iface_no;
332 req.wIndex[1] = 0;
333 USETW(req.wLength, len);
334 USETDW(temp, value);
335
336 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
337 &req, temp, 0, 1000);
338 if (err) {
339 DPRINTFN(0, "device request failed, err=%s "
340 "(ignored)\n", usbd_errstr(err));
341 }
342 return;
343}
344
345static void
346umct_intr_callback_sub(struct usb_xfer *xfer, usb_error_t error)
347{
348 struct umct_softc *sc = usbd_xfer_softc(xfer);
349 struct usb_page_cache *pc;
350 uint8_t buf[2];
351 int actlen;
352
353 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
354
355 switch (USB_GET_STATE(xfer)) {
356 case USB_ST_TRANSFERRED:
357 if (actlen < 2) {
358 DPRINTF("too short message\n");
359 goto tr_setup;
360 }
361 pc = usbd_xfer_get_frame(xfer, 0);
362 usbd_copy_out(pc, 0, buf, sizeof(buf));
363
364 sc->sc_msr = buf[0];
365 sc->sc_lsr = buf[1];
366
367 ucom_status_change(&sc->sc_ucom);
368
369 case USB_ST_SETUP:
370tr_setup:
371 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
372 usbd_transfer_submit(xfer);
373 return;
374
375 default: /* Error */
376 if (error != USB_ERR_CANCELLED) {
377 /* try to clear stall first */
378 usbd_xfer_set_stall(xfer);
379 goto tr_setup;
380 }
381 return;
382 }
383}
384
385static void
386umct_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
387{
388 struct umct_softc *sc = ucom->sc_parent;
389
390 *lsr = sc->sc_lsr;
391 *msr = sc->sc_msr;
392}
393
394static void
395umct_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
396{
397 struct umct_softc *sc = ucom->sc_parent;
398
399 if (onoff)
400 sc->sc_lcr |= 0x40;
401 else
402 sc->sc_lcr &= ~0x40;
403
404 umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
405}
406
407static void
408umct_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
409{
410 struct umct_softc *sc = ucom->sc_parent;
411
412 if (onoff)
413 sc->sc_mcr |= 0x01;
414 else
415 sc->sc_mcr &= ~0x01;
416
417 umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
418}
419
420static void
421umct_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
422{
423 struct umct_softc *sc = ucom->sc_parent;
424
425 if (onoff)
426 sc->sc_mcr |= 0x02;
427 else
428 sc->sc_mcr &= ~0x02;
429
430 umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
431}
432
433static uint8_t
434umct_calc_baud(uint32_t baud)
435{
436 switch (baud) {
437 case B300:return (0x1);
438 case B600:
439 return (0x2);
440 case B1200:
441 return (0x3);
442 case B2400:
443 return (0x4);
444 case B4800:
445 return (0x6);
446 case B9600:
447 return (0x8);
448 case B19200:
449 return (0x9);
450 case B38400:
451 return (0xa);
452 case B57600:
453 return (0xb);
454 case 115200:
455 return (0xc);
456 case B0:
457 default:
458 break;
459 }
460 return (0x0);
461}
462
463static int
464umct_pre_param(struct ucom_softc *ucom, struct termios *t)
465{
466 return (0); /* we accept anything */
467}
468
469static void
470umct_cfg_param(struct ucom_softc *ucom, struct termios *t)
471{
472 struct umct_softc *sc = ucom->sc_parent;
473 uint32_t value;
474
475 value = umct_calc_baud(t->c_ospeed);
476 umct_cfg_do_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
477
478 value = (sc->sc_lcr & 0x40);
479
480 switch (t->c_cflag & CSIZE) {
481 case CS5:
482 value |= 0x0;
483 break;
484 case CS6:
485 value |= 0x1;
486 break;
487 case CS7:
488 value |= 0x2;
489 break;
490 default:
491 case CS8:
492 value |= 0x3;
493 break;
494 }
495
496 value |= (t->c_cflag & CSTOPB) ? 0x4 : 0;
497 if (t->c_cflag & PARENB) {
498 value |= 0x8;
499 value |= (t->c_cflag & PARODD) ? 0x0 : 0x10;
500 }
501 /*
502 * XXX There doesn't seem to be a way to tell the device
503 * to use flow control.
504 */
505
506 sc->sc_lcr = value;
507 umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
508}
509
510static void
511umct_start_read(struct ucom_softc *ucom)
512{
513 struct umct_softc *sc = ucom->sc_parent;
514
515 /* start interrupt endpoint */
516 usbd_transfer_start(sc->sc_xfer[UMCT_INTR_DT_RD]);
517
518 /* start read endpoint */
519 usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_RD]);
520}
521
522static void
523umct_stop_read(struct ucom_softc *ucom)
524{
525 struct umct_softc *sc = ucom->sc_parent;
526
527 /* stop interrupt endpoint */
528 usbd_transfer_stop(sc->sc_xfer[UMCT_INTR_DT_RD]);
529
530 /* stop read endpoint */
531 usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_RD]);
532}
533
534static void
535umct_start_write(struct ucom_softc *ucom)
536{
537 struct umct_softc *sc = ucom->sc_parent;
538
539 usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_WR]);
540}
541
542static void
543umct_stop_write(struct ucom_softc *ucom)
544{
545 struct umct_softc *sc = ucom->sc_parent;
546
547 usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_WR]);
548}
549
550static void
551umct_read_callback(struct usb_xfer *xfer, usb_error_t error)
552{
553 struct umct_softc *sc = usbd_xfer_softc(xfer);
554
555 if (sc->sc_swap_cb)
556 umct_intr_callback_sub(xfer, error);
557 else
558 umct_read_callback_sub(xfer, error);
559}
560
561static void
562umct_intr_callback(struct usb_xfer *xfer, usb_error_t error)
563{
564 struct umct_softc *sc = usbd_xfer_softc(xfer);
565
566 if (sc->sc_swap_cb)
567 umct_read_callback_sub(xfer, error);
568 else
569 umct_intr_callback_sub(xfer, error);
570}
571
572static void
573umct_write_callback(struct usb_xfer *xfer, usb_error_t error)
574{
575 struct umct_softc *sc = usbd_xfer_softc(xfer);
576 struct usb_page_cache *pc;
577 uint32_t actlen;
578
579 switch (USB_GET_STATE(xfer)) {
580 case USB_ST_SETUP:
581 case USB_ST_TRANSFERRED:
582tr_setup:
583 pc = usbd_xfer_get_frame(xfer, 0);
584 if (ucom_get_data(&sc->sc_ucom, pc, 0,
585 sc->sc_obufsize, &actlen)) {
586
587 usbd_xfer_set_frame_len(xfer, 0, actlen);
588 usbd_transfer_submit(xfer);
589 }
590 return;
591
592 default: /* Error */
593 if (error != USB_ERR_CANCELLED) {
594 /* try to clear stall first */
595 usbd_xfer_set_stall(xfer);
596 goto tr_setup;
597 }
598 return;
599 }
600}
601
602static void
603umct_read_callback_sub(struct usb_xfer *xfer, usb_error_t error)
604{
605 struct umct_softc *sc = usbd_xfer_softc(xfer);
606 struct usb_page_cache *pc;
607 int actlen;
608
609 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
610
611 switch (USB_GET_STATE(xfer)) {
612 case USB_ST_TRANSFERRED:
613 pc = usbd_xfer_get_frame(xfer, 0);
614 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
615
616 case USB_ST_SETUP:
617tr_setup:
618 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
619 usbd_transfer_submit(xfer);
620 return;
621
622 default: /* Error */
623 if (error != USB_ERR_CANCELLED) {
624 /* try to clear stall first */
625 usbd_xfer_set_stall(xfer);
626 goto tr_setup;
627 }
628 return;
629 }
630}
631
632static void
633umct_poll(struct ucom_softc *ucom)
634{
635 struct umct_softc *sc = ucom->sc_parent;
636 usbd_transfer_poll(sc->sc_xfer, UMCT_N_TRANSFER);
637}