Deleted Added
full compact
umct.c (189275) umct.c (190174)
1#include <sys/cdefs.h>
1#include <sys/cdefs.h>
2__FBSDID("$FreeBSD: head/sys/dev/usb/serial/umct.c 189275 2009-03-02 05:37:05Z thompsa $");
2__FBSDID("$FreeBSD: head/sys/dev/usb/serial/umct.c 190174 2009-03-20 19:04:31Z 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 "usbdevs.h"
48#include <dev/usb/usb.h>
49#include <dev/usb/usb_mfunc.h>
50#include <dev/usb/usb_error.h>
51#include <dev/usb/usb_cdc.h>
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 "usbdevs.h"
48#include <dev/usb/usb.h>
49#include <dev/usb/usb_mfunc.h>
50#include <dev/usb/usb_error.h>
51#include <dev/usb/usb_cdc.h>
52#include <dev/usb/usb_defs.h>
53
54#define USB_DEBUG_VAR usb2_debug
55
56#include <dev/usb/usb_core.h>
57#include <dev/usb/usb_debug.h>
58#include <dev/usb/usb_process.h>
59#include <dev/usb/usb_request.h>
60#include <dev/usb/usb_lookup.h>
61#include <dev/usb/usb_util.h>
62#include <dev/usb/usb_busdma.h>
63#include <dev/usb/usb_device.h>
64
65#include <dev/usb/serial/usb_serial.h>
66
67/* The UMCT advertises the standard 8250 UART registers */
68#define UMCT_GET_MSR 2 /* Get Modem Status Register */
69#define UMCT_GET_MSR_SIZE 1
70#define UMCT_GET_LCR 6 /* Get Line Control Register */
71#define UMCT_GET_LCR_SIZE 1
72#define UMCT_SET_BAUD 5 /* Set the Baud Rate Divisor */
73#define UMCT_SET_BAUD_SIZE 4
74#define UMCT_SET_LCR 7 /* Set Line Control Register */
75#define UMCT_SET_LCR_SIZE 1
76#define UMCT_SET_MCR 10 /* Set Modem Control Register */
77#define UMCT_SET_MCR_SIZE 1
78
79#define UMCT_INTR_INTERVAL 100
80#define UMCT_IFACE_INDEX 0
81#define UMCT_CONFIG_INDEX 1
82
83enum {
84 UMCT_BULK_DT_WR,
85 UMCT_BULK_DT_RD,
86 UMCT_INTR_DT_RD,
87 UMCT_N_TRANSFER,
88};
89
90struct umct_softc {
91 struct usb2_com_super_softc sc_super_ucom;
92 struct usb2_com_softc sc_ucom;
93
94 struct usb2_device *sc_udev;
95 struct usb2_xfer *sc_xfer[UMCT_N_TRANSFER];
96 struct mtx sc_mtx;
97
98 uint32_t sc_unit;
99
100 uint16_t sc_obufsize;
101
102 uint8_t sc_lsr;
103 uint8_t sc_msr;
104 uint8_t sc_lcr;
105 uint8_t sc_mcr;
106 uint8_t sc_iface_no;
107 uint8_t sc_name[16];
108};
109
110/* prototypes */
111
112static device_probe_t umct_probe;
113static device_attach_t umct_attach;
114static device_detach_t umct_detach;
115
116static usb2_callback_t umct_intr_callback;
117static usb2_callback_t umct_write_callback;
118static usb2_callback_t umct_read_callback;
119
120static void umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
121 uint16_t len, uint32_t value);
122static void umct_cfg_get_status(struct usb2_com_softc *, uint8_t *,
123 uint8_t *);
124static void umct_cfg_set_break(struct usb2_com_softc *, uint8_t);
125static void umct_cfg_set_dtr(struct usb2_com_softc *, uint8_t);
126static void umct_cfg_set_rts(struct usb2_com_softc *, uint8_t);
127static uint8_t umct_calc_baud(uint32_t);
128static int umct_pre_param(struct usb2_com_softc *, struct termios *);
129static void umct_cfg_param(struct usb2_com_softc *, struct termios *);
130static void umct_start_read(struct usb2_com_softc *);
131static void umct_stop_read(struct usb2_com_softc *);
132static void umct_start_write(struct usb2_com_softc *);
133static void umct_stop_write(struct usb2_com_softc *);
134
135static const struct usb2_config umct_config[UMCT_N_TRANSFER] = {
136
137 [UMCT_BULK_DT_WR] = {
138 .type = UE_BULK,
139 .endpoint = UE_ADDR_ANY,
140 .direction = UE_DIR_OUT,
141 .mh.bufsize = 0, /* use wMaxPacketSize */
142 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
143 .mh.callback = &umct_write_callback,
144 },
145
146 [UMCT_BULK_DT_RD] = {
147 .type = UE_INTERRUPT,
148 .endpoint = UE_ADDR_ANY,
149 .direction = UE_DIR_IN,
150 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
151 .mh.bufsize = 0, /* use wMaxPacketSize */
152 .mh.callback = &umct_read_callback,
153 .ep_index = 0, /* first interrupt endpoint */
154 },
155
156 [UMCT_INTR_DT_RD] = {
157 .type = UE_INTERRUPT,
158 .endpoint = UE_ADDR_ANY,
159 .direction = UE_DIR_IN,
160 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
161 .mh.bufsize = 0, /* use wMaxPacketSize */
162 .mh.callback = &umct_intr_callback,
163 .ep_index = 1, /* second interrupt endpoint */
164 },
165};
166
167static const struct usb2_com_callback umct_callback = {
168 .usb2_com_cfg_get_status = &umct_cfg_get_status,
169 .usb2_com_cfg_set_dtr = &umct_cfg_set_dtr,
170 .usb2_com_cfg_set_rts = &umct_cfg_set_rts,
171 .usb2_com_cfg_set_break = &umct_cfg_set_break,
172 .usb2_com_cfg_param = &umct_cfg_param,
173 .usb2_com_pre_param = &umct_pre_param,
174 .usb2_com_start_read = &umct_start_read,
175 .usb2_com_stop_read = &umct_stop_read,
176 .usb2_com_start_write = &umct_start_write,
177 .usb2_com_stop_write = &umct_stop_write,
178};
179
180static const struct usb2_device_id umct_devs[] = {
181 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232, 0)},
182 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232, 0)},
183 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232, 0)},
184 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109, 0)},
185 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U409, 0)},
186};
187
188static device_method_t umct_methods[] = {
189 DEVMETHOD(device_probe, umct_probe),
190 DEVMETHOD(device_attach, umct_attach),
191 DEVMETHOD(device_detach, umct_detach),
192 {0, 0}
193};
194
195static devclass_t umct_devclass;
196
197static driver_t umct_driver = {
198 .name = "umct",
199 .methods = umct_methods,
200 .size = sizeof(struct umct_softc),
201};
202
203DRIVER_MODULE(umct, uhub, umct_driver, umct_devclass, NULL, 0);
204MODULE_DEPEND(umct, ucom, 1, 1, 1);
205MODULE_DEPEND(umct, usb, 1, 1, 1);
206
207static int
208umct_probe(device_t dev)
209{
210 struct usb2_attach_arg *uaa = device_get_ivars(dev);
211
212 if (uaa->usb2_mode != USB_MODE_HOST) {
213 return (ENXIO);
214 }
215 if (uaa->info.bConfigIndex != UMCT_CONFIG_INDEX) {
216 return (ENXIO);
217 }
218 if (uaa->info.bIfaceIndex != UMCT_IFACE_INDEX) {
219 return (ENXIO);
220 }
221 return (usb2_lookup_id_by_uaa(umct_devs, sizeof(umct_devs), uaa));
222}
223
224static int
225umct_attach(device_t dev)
226{
227 struct usb2_attach_arg *uaa = device_get_ivars(dev);
228 struct umct_softc *sc = device_get_softc(dev);
229 int32_t error;
230 uint16_t maxp;
231 uint8_t iface_index;
232
233 sc->sc_udev = uaa->device;
234 sc->sc_unit = device_get_unit(dev);
235
236 device_set_usb2_desc(dev);
237 mtx_init(&sc->sc_mtx, "umct", NULL, MTX_DEF);
238
239 snprintf(sc->sc_name, sizeof(sc->sc_name),
240 "%s", device_get_nameunit(dev));
241
242 sc->sc_iface_no = uaa->info.bIfaceNum;
243
244 iface_index = UMCT_IFACE_INDEX;
245 error = usb2_transfer_setup(uaa->device, &iface_index,
246 sc->sc_xfer, umct_config, UMCT_N_TRANSFER, sc, &sc->sc_mtx);
247
248 if (error) {
249 device_printf(dev, "allocating USB "
250 "transfers failed!\n");
251 goto detach;
252 }
253 /*
254 * The real bulk-in endpoint is also marked as an interrupt.
255 * The only way to differentiate it from the real interrupt
256 * endpoint is to look at the wMaxPacketSize field.
257 */
258 maxp = UGETW(sc->sc_xfer[UMCT_BULK_DT_RD]->pipe->edesc->wMaxPacketSize);
259 if (maxp == 0x2) {
260
261 /* guessed wrong - switch around endpoints */
262
263 struct usb2_xfer *temp = sc->sc_xfer[UMCT_INTR_DT_RD];
264
265 sc->sc_xfer[UMCT_INTR_DT_RD] = sc->sc_xfer[UMCT_BULK_DT_RD];
266 sc->sc_xfer[UMCT_BULK_DT_RD] = temp;
267
268 sc->sc_xfer[UMCT_BULK_DT_RD]->callback = &umct_read_callback;
269 sc->sc_xfer[UMCT_INTR_DT_RD]->callback = &umct_intr_callback;
270 }
271 sc->sc_obufsize = sc->sc_xfer[UMCT_BULK_DT_WR]->max_data_length;
272
273 if (uaa->info.idProduct == USB_PRODUCT_MCT_SITECOM_USB232) {
274 if (sc->sc_obufsize > 16) {
275 sc->sc_obufsize = 16;
276 }
277 }
278 error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
279 &umct_callback, &sc->sc_mtx);
280 if (error) {
281 goto detach;
282 }
283 return (0); /* success */
284
285detach:
286 umct_detach(dev);
287 return (ENXIO); /* failure */
288}
289
290static int
291umct_detach(device_t dev)
292{
293 struct umct_softc *sc = device_get_softc(dev);
294
295 usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
296 usb2_transfer_unsetup(sc->sc_xfer, UMCT_N_TRANSFER);
297 mtx_destroy(&sc->sc_mtx);
298
299 return (0);
300}
301
302static void
303umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
304 uint16_t len, uint32_t value)
305{
306 struct usb2_device_request req;
307 usb2_error_t err;
308 uint8_t temp[4];
309
310 if (len > 4)
311 len = 4;
312 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
313 req.bRequest = request;
314 USETW(req.wValue, 0);
315 req.wIndex[0] = sc->sc_iface_no;
316 req.wIndex[1] = 0;
317 USETW(req.wLength, len);
318 USETDW(temp, value);
319
320 err = usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
321 &req, temp, 0, 1000);
322 if (err) {
323 DPRINTFN(0, "device request failed, err=%s "
324 "(ignored)\n", usb2_errstr(err));
325 }
326 return;
327}
328
329static void
330umct_intr_callback(struct usb2_xfer *xfer)
331{
332 struct umct_softc *sc = xfer->priv_sc;
333 uint8_t buf[2];
334
335 switch (USB_GET_STATE(xfer)) {
336 case USB_ST_TRANSFERRED:
337 if (xfer->actlen < 2) {
338 DPRINTF("too short message\n");
339 goto tr_setup;
340 }
341 usb2_copy_out(xfer->frbuffers, 0, buf, sizeof(buf));
342
343 sc->sc_msr = buf[0];
344 sc->sc_lsr = buf[1];
345
346 usb2_com_status_change(&sc->sc_ucom);
347
348 case USB_ST_SETUP:
349tr_setup:
350 xfer->frlengths[0] = xfer->max_data_length;
351 usb2_start_hardware(xfer);
352 return;
353
354 default: /* Error */
355 if (xfer->error != USB_ERR_CANCELLED) {
356 /* try to clear stall first */
357 xfer->flags.stall_pipe = 1;
358 goto tr_setup;
359 }
360 return;
361 }
362}
363
364static void
365umct_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr)
366{
367 struct umct_softc *sc = ucom->sc_parent;
368
369 *lsr = sc->sc_lsr;
370 *msr = sc->sc_msr;
371}
372
373static void
374umct_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
375{
376 struct umct_softc *sc = ucom->sc_parent;
377
378 if (onoff)
379 sc->sc_lcr |= 0x40;
380 else
381 sc->sc_lcr &= ~0x40;
382
383 umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
384}
385
386static void
387umct_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff)
388{
389 struct umct_softc *sc = ucom->sc_parent;
390
391 if (onoff)
392 sc->sc_mcr |= 0x01;
393 else
394 sc->sc_mcr &= ~0x01;
395
396 umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
397}
398
399static void
400umct_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff)
401{
402 struct umct_softc *sc = ucom->sc_parent;
403
404 if (onoff)
405 sc->sc_mcr |= 0x02;
406 else
407 sc->sc_mcr &= ~0x02;
408
409 umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
410}
411
412static uint8_t
413umct_calc_baud(uint32_t baud)
414{
415 switch (baud) {
416 case B300:return (0x1);
417 case B600:
418 return (0x2);
419 case B1200:
420 return (0x3);
421 case B2400:
422 return (0x4);
423 case B4800:
424 return (0x6);
425 case B9600:
426 return (0x8);
427 case B19200:
428 return (0x9);
429 case B38400:
430 return (0xa);
431 case B57600:
432 return (0xb);
433 case 115200:
434 return (0xc);
435 case B0:
436 default:
437 break;
438 }
439 return (0x0);
440}
441
442static int
443umct_pre_param(struct usb2_com_softc *ucom, struct termios *t)
444{
445 return (0); /* we accept anything */
446}
447
448static void
449umct_cfg_param(struct usb2_com_softc *ucom, struct termios *t)
450{
451 struct umct_softc *sc = ucom->sc_parent;
452 uint32_t value;
453
454 value = umct_calc_baud(t->c_ospeed);
455 umct_cfg_do_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
456
457 value = (sc->sc_lcr & 0x40);
458
459 switch (t->c_cflag & CSIZE) {
460 case CS5:
461 value |= 0x0;
462 break;
463 case CS6:
464 value |= 0x1;
465 break;
466 case CS7:
467 value |= 0x2;
468 break;
469 default:
470 case CS8:
471 value |= 0x3;
472 break;
473 }
474
475 value |= (t->c_cflag & CSTOPB) ? 0x4 : 0;
476 if (t->c_cflag & PARENB) {
477 value |= 0x8;
478 value |= (t->c_cflag & PARODD) ? 0x0 : 0x10;
479 }
480 /*
481 * XXX There doesn't seem to be a way to tell the device
482 * to use flow control.
483 */
484
485 sc->sc_lcr = value;
486 umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
487}
488
489static void
490umct_start_read(struct usb2_com_softc *ucom)
491{
492 struct umct_softc *sc = ucom->sc_parent;
493
494 /* start interrupt endpoint */
495 usb2_transfer_start(sc->sc_xfer[UMCT_INTR_DT_RD]);
496
497 /* start read endpoint */
498 usb2_transfer_start(sc->sc_xfer[UMCT_BULK_DT_RD]);
499}
500
501static void
502umct_stop_read(struct usb2_com_softc *ucom)
503{
504 struct umct_softc *sc = ucom->sc_parent;
505
506 /* stop interrupt endpoint */
507 usb2_transfer_stop(sc->sc_xfer[UMCT_INTR_DT_RD]);
508
509 /* stop read endpoint */
510 usb2_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_RD]);
511}
512
513static void
514umct_start_write(struct usb2_com_softc *ucom)
515{
516 struct umct_softc *sc = ucom->sc_parent;
517
518 usb2_transfer_start(sc->sc_xfer[UMCT_BULK_DT_WR]);
519}
520
521static void
522umct_stop_write(struct usb2_com_softc *ucom)
523{
524 struct umct_softc *sc = ucom->sc_parent;
525
526 usb2_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_WR]);
527}
528
529static void
530umct_write_callback(struct usb2_xfer *xfer)
531{
532 struct umct_softc *sc = xfer->priv_sc;
533 uint32_t actlen;
534
535 switch (USB_GET_STATE(xfer)) {
536 case USB_ST_SETUP:
537 case USB_ST_TRANSFERRED:
538tr_setup:
539 if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
540 sc->sc_obufsize, &actlen)) {
541
542 xfer->frlengths[0] = actlen;
543 usb2_start_hardware(xfer);
544 }
545 return;
546
547 default: /* Error */
548 if (xfer->error != USB_ERR_CANCELLED) {
549 /* try to clear stall first */
550 xfer->flags.stall_pipe = 1;
551 goto tr_setup;
552 }
553 return;
554 }
555}
556
557static void
558umct_read_callback(struct usb2_xfer *xfer)
559{
560 struct umct_softc *sc = xfer->priv_sc;
561
562 switch (USB_GET_STATE(xfer)) {
563 case USB_ST_TRANSFERRED:
564 usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers,
565 0, xfer->actlen);
566
567 case USB_ST_SETUP:
568tr_setup:
569 xfer->frlengths[0] = xfer->max_data_length;
570 usb2_start_hardware(xfer);
571 return;
572
573 default: /* Error */
574 if (xfer->error != USB_ERR_CANCELLED) {
575 /* try to clear stall first */
576 xfer->flags.stall_pipe = 1;
577 goto tr_setup;
578 }
579 return;
580 }
581}
52
53#define USB_DEBUG_VAR usb2_debug
54
55#include <dev/usb/usb_core.h>
56#include <dev/usb/usb_debug.h>
57#include <dev/usb/usb_process.h>
58#include <dev/usb/usb_request.h>
59#include <dev/usb/usb_lookup.h>
60#include <dev/usb/usb_util.h>
61#include <dev/usb/usb_busdma.h>
62#include <dev/usb/usb_device.h>
63
64#include <dev/usb/serial/usb_serial.h>
65
66/* The UMCT advertises the standard 8250 UART registers */
67#define UMCT_GET_MSR 2 /* Get Modem Status Register */
68#define UMCT_GET_MSR_SIZE 1
69#define UMCT_GET_LCR 6 /* Get Line Control Register */
70#define UMCT_GET_LCR_SIZE 1
71#define UMCT_SET_BAUD 5 /* Set the Baud Rate Divisor */
72#define UMCT_SET_BAUD_SIZE 4
73#define UMCT_SET_LCR 7 /* Set Line Control Register */
74#define UMCT_SET_LCR_SIZE 1
75#define UMCT_SET_MCR 10 /* Set Modem Control Register */
76#define UMCT_SET_MCR_SIZE 1
77
78#define UMCT_INTR_INTERVAL 100
79#define UMCT_IFACE_INDEX 0
80#define UMCT_CONFIG_INDEX 1
81
82enum {
83 UMCT_BULK_DT_WR,
84 UMCT_BULK_DT_RD,
85 UMCT_INTR_DT_RD,
86 UMCT_N_TRANSFER,
87};
88
89struct umct_softc {
90 struct usb2_com_super_softc sc_super_ucom;
91 struct usb2_com_softc sc_ucom;
92
93 struct usb2_device *sc_udev;
94 struct usb2_xfer *sc_xfer[UMCT_N_TRANSFER];
95 struct mtx sc_mtx;
96
97 uint32_t sc_unit;
98
99 uint16_t sc_obufsize;
100
101 uint8_t sc_lsr;
102 uint8_t sc_msr;
103 uint8_t sc_lcr;
104 uint8_t sc_mcr;
105 uint8_t sc_iface_no;
106 uint8_t sc_name[16];
107};
108
109/* prototypes */
110
111static device_probe_t umct_probe;
112static device_attach_t umct_attach;
113static device_detach_t umct_detach;
114
115static usb2_callback_t umct_intr_callback;
116static usb2_callback_t umct_write_callback;
117static usb2_callback_t umct_read_callback;
118
119static void umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
120 uint16_t len, uint32_t value);
121static void umct_cfg_get_status(struct usb2_com_softc *, uint8_t *,
122 uint8_t *);
123static void umct_cfg_set_break(struct usb2_com_softc *, uint8_t);
124static void umct_cfg_set_dtr(struct usb2_com_softc *, uint8_t);
125static void umct_cfg_set_rts(struct usb2_com_softc *, uint8_t);
126static uint8_t umct_calc_baud(uint32_t);
127static int umct_pre_param(struct usb2_com_softc *, struct termios *);
128static void umct_cfg_param(struct usb2_com_softc *, struct termios *);
129static void umct_start_read(struct usb2_com_softc *);
130static void umct_stop_read(struct usb2_com_softc *);
131static void umct_start_write(struct usb2_com_softc *);
132static void umct_stop_write(struct usb2_com_softc *);
133
134static const struct usb2_config umct_config[UMCT_N_TRANSFER] = {
135
136 [UMCT_BULK_DT_WR] = {
137 .type = UE_BULK,
138 .endpoint = UE_ADDR_ANY,
139 .direction = UE_DIR_OUT,
140 .mh.bufsize = 0, /* use wMaxPacketSize */
141 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
142 .mh.callback = &umct_write_callback,
143 },
144
145 [UMCT_BULK_DT_RD] = {
146 .type = UE_INTERRUPT,
147 .endpoint = UE_ADDR_ANY,
148 .direction = UE_DIR_IN,
149 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
150 .mh.bufsize = 0, /* use wMaxPacketSize */
151 .mh.callback = &umct_read_callback,
152 .ep_index = 0, /* first interrupt endpoint */
153 },
154
155 [UMCT_INTR_DT_RD] = {
156 .type = UE_INTERRUPT,
157 .endpoint = UE_ADDR_ANY,
158 .direction = UE_DIR_IN,
159 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
160 .mh.bufsize = 0, /* use wMaxPacketSize */
161 .mh.callback = &umct_intr_callback,
162 .ep_index = 1, /* second interrupt endpoint */
163 },
164};
165
166static const struct usb2_com_callback umct_callback = {
167 .usb2_com_cfg_get_status = &umct_cfg_get_status,
168 .usb2_com_cfg_set_dtr = &umct_cfg_set_dtr,
169 .usb2_com_cfg_set_rts = &umct_cfg_set_rts,
170 .usb2_com_cfg_set_break = &umct_cfg_set_break,
171 .usb2_com_cfg_param = &umct_cfg_param,
172 .usb2_com_pre_param = &umct_pre_param,
173 .usb2_com_start_read = &umct_start_read,
174 .usb2_com_stop_read = &umct_stop_read,
175 .usb2_com_start_write = &umct_start_write,
176 .usb2_com_stop_write = &umct_stop_write,
177};
178
179static const struct usb2_device_id umct_devs[] = {
180 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232, 0)},
181 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232, 0)},
182 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232, 0)},
183 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109, 0)},
184 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U409, 0)},
185};
186
187static device_method_t umct_methods[] = {
188 DEVMETHOD(device_probe, umct_probe),
189 DEVMETHOD(device_attach, umct_attach),
190 DEVMETHOD(device_detach, umct_detach),
191 {0, 0}
192};
193
194static devclass_t umct_devclass;
195
196static driver_t umct_driver = {
197 .name = "umct",
198 .methods = umct_methods,
199 .size = sizeof(struct umct_softc),
200};
201
202DRIVER_MODULE(umct, uhub, umct_driver, umct_devclass, NULL, 0);
203MODULE_DEPEND(umct, ucom, 1, 1, 1);
204MODULE_DEPEND(umct, usb, 1, 1, 1);
205
206static int
207umct_probe(device_t dev)
208{
209 struct usb2_attach_arg *uaa = device_get_ivars(dev);
210
211 if (uaa->usb2_mode != USB_MODE_HOST) {
212 return (ENXIO);
213 }
214 if (uaa->info.bConfigIndex != UMCT_CONFIG_INDEX) {
215 return (ENXIO);
216 }
217 if (uaa->info.bIfaceIndex != UMCT_IFACE_INDEX) {
218 return (ENXIO);
219 }
220 return (usb2_lookup_id_by_uaa(umct_devs, sizeof(umct_devs), uaa));
221}
222
223static int
224umct_attach(device_t dev)
225{
226 struct usb2_attach_arg *uaa = device_get_ivars(dev);
227 struct umct_softc *sc = device_get_softc(dev);
228 int32_t error;
229 uint16_t maxp;
230 uint8_t iface_index;
231
232 sc->sc_udev = uaa->device;
233 sc->sc_unit = device_get_unit(dev);
234
235 device_set_usb2_desc(dev);
236 mtx_init(&sc->sc_mtx, "umct", NULL, MTX_DEF);
237
238 snprintf(sc->sc_name, sizeof(sc->sc_name),
239 "%s", device_get_nameunit(dev));
240
241 sc->sc_iface_no = uaa->info.bIfaceNum;
242
243 iface_index = UMCT_IFACE_INDEX;
244 error = usb2_transfer_setup(uaa->device, &iface_index,
245 sc->sc_xfer, umct_config, UMCT_N_TRANSFER, sc, &sc->sc_mtx);
246
247 if (error) {
248 device_printf(dev, "allocating USB "
249 "transfers failed!\n");
250 goto detach;
251 }
252 /*
253 * The real bulk-in endpoint is also marked as an interrupt.
254 * The only way to differentiate it from the real interrupt
255 * endpoint is to look at the wMaxPacketSize field.
256 */
257 maxp = UGETW(sc->sc_xfer[UMCT_BULK_DT_RD]->pipe->edesc->wMaxPacketSize);
258 if (maxp == 0x2) {
259
260 /* guessed wrong - switch around endpoints */
261
262 struct usb2_xfer *temp = sc->sc_xfer[UMCT_INTR_DT_RD];
263
264 sc->sc_xfer[UMCT_INTR_DT_RD] = sc->sc_xfer[UMCT_BULK_DT_RD];
265 sc->sc_xfer[UMCT_BULK_DT_RD] = temp;
266
267 sc->sc_xfer[UMCT_BULK_DT_RD]->callback = &umct_read_callback;
268 sc->sc_xfer[UMCT_INTR_DT_RD]->callback = &umct_intr_callback;
269 }
270 sc->sc_obufsize = sc->sc_xfer[UMCT_BULK_DT_WR]->max_data_length;
271
272 if (uaa->info.idProduct == USB_PRODUCT_MCT_SITECOM_USB232) {
273 if (sc->sc_obufsize > 16) {
274 sc->sc_obufsize = 16;
275 }
276 }
277 error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
278 &umct_callback, &sc->sc_mtx);
279 if (error) {
280 goto detach;
281 }
282 return (0); /* success */
283
284detach:
285 umct_detach(dev);
286 return (ENXIO); /* failure */
287}
288
289static int
290umct_detach(device_t dev)
291{
292 struct umct_softc *sc = device_get_softc(dev);
293
294 usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
295 usb2_transfer_unsetup(sc->sc_xfer, UMCT_N_TRANSFER);
296 mtx_destroy(&sc->sc_mtx);
297
298 return (0);
299}
300
301static void
302umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
303 uint16_t len, uint32_t value)
304{
305 struct usb2_device_request req;
306 usb2_error_t err;
307 uint8_t temp[4];
308
309 if (len > 4)
310 len = 4;
311 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
312 req.bRequest = request;
313 USETW(req.wValue, 0);
314 req.wIndex[0] = sc->sc_iface_no;
315 req.wIndex[1] = 0;
316 USETW(req.wLength, len);
317 USETDW(temp, value);
318
319 err = usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
320 &req, temp, 0, 1000);
321 if (err) {
322 DPRINTFN(0, "device request failed, err=%s "
323 "(ignored)\n", usb2_errstr(err));
324 }
325 return;
326}
327
328static void
329umct_intr_callback(struct usb2_xfer *xfer)
330{
331 struct umct_softc *sc = xfer->priv_sc;
332 uint8_t buf[2];
333
334 switch (USB_GET_STATE(xfer)) {
335 case USB_ST_TRANSFERRED:
336 if (xfer->actlen < 2) {
337 DPRINTF("too short message\n");
338 goto tr_setup;
339 }
340 usb2_copy_out(xfer->frbuffers, 0, buf, sizeof(buf));
341
342 sc->sc_msr = buf[0];
343 sc->sc_lsr = buf[1];
344
345 usb2_com_status_change(&sc->sc_ucom);
346
347 case USB_ST_SETUP:
348tr_setup:
349 xfer->frlengths[0] = xfer->max_data_length;
350 usb2_start_hardware(xfer);
351 return;
352
353 default: /* Error */
354 if (xfer->error != USB_ERR_CANCELLED) {
355 /* try to clear stall first */
356 xfer->flags.stall_pipe = 1;
357 goto tr_setup;
358 }
359 return;
360 }
361}
362
363static void
364umct_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr)
365{
366 struct umct_softc *sc = ucom->sc_parent;
367
368 *lsr = sc->sc_lsr;
369 *msr = sc->sc_msr;
370}
371
372static void
373umct_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
374{
375 struct umct_softc *sc = ucom->sc_parent;
376
377 if (onoff)
378 sc->sc_lcr |= 0x40;
379 else
380 sc->sc_lcr &= ~0x40;
381
382 umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
383}
384
385static void
386umct_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff)
387{
388 struct umct_softc *sc = ucom->sc_parent;
389
390 if (onoff)
391 sc->sc_mcr |= 0x01;
392 else
393 sc->sc_mcr &= ~0x01;
394
395 umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
396}
397
398static void
399umct_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff)
400{
401 struct umct_softc *sc = ucom->sc_parent;
402
403 if (onoff)
404 sc->sc_mcr |= 0x02;
405 else
406 sc->sc_mcr &= ~0x02;
407
408 umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
409}
410
411static uint8_t
412umct_calc_baud(uint32_t baud)
413{
414 switch (baud) {
415 case B300:return (0x1);
416 case B600:
417 return (0x2);
418 case B1200:
419 return (0x3);
420 case B2400:
421 return (0x4);
422 case B4800:
423 return (0x6);
424 case B9600:
425 return (0x8);
426 case B19200:
427 return (0x9);
428 case B38400:
429 return (0xa);
430 case B57600:
431 return (0xb);
432 case 115200:
433 return (0xc);
434 case B0:
435 default:
436 break;
437 }
438 return (0x0);
439}
440
441static int
442umct_pre_param(struct usb2_com_softc *ucom, struct termios *t)
443{
444 return (0); /* we accept anything */
445}
446
447static void
448umct_cfg_param(struct usb2_com_softc *ucom, struct termios *t)
449{
450 struct umct_softc *sc = ucom->sc_parent;
451 uint32_t value;
452
453 value = umct_calc_baud(t->c_ospeed);
454 umct_cfg_do_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
455
456 value = (sc->sc_lcr & 0x40);
457
458 switch (t->c_cflag & CSIZE) {
459 case CS5:
460 value |= 0x0;
461 break;
462 case CS6:
463 value |= 0x1;
464 break;
465 case CS7:
466 value |= 0x2;
467 break;
468 default:
469 case CS8:
470 value |= 0x3;
471 break;
472 }
473
474 value |= (t->c_cflag & CSTOPB) ? 0x4 : 0;
475 if (t->c_cflag & PARENB) {
476 value |= 0x8;
477 value |= (t->c_cflag & PARODD) ? 0x0 : 0x10;
478 }
479 /*
480 * XXX There doesn't seem to be a way to tell the device
481 * to use flow control.
482 */
483
484 sc->sc_lcr = value;
485 umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
486}
487
488static void
489umct_start_read(struct usb2_com_softc *ucom)
490{
491 struct umct_softc *sc = ucom->sc_parent;
492
493 /* start interrupt endpoint */
494 usb2_transfer_start(sc->sc_xfer[UMCT_INTR_DT_RD]);
495
496 /* start read endpoint */
497 usb2_transfer_start(sc->sc_xfer[UMCT_BULK_DT_RD]);
498}
499
500static void
501umct_stop_read(struct usb2_com_softc *ucom)
502{
503 struct umct_softc *sc = ucom->sc_parent;
504
505 /* stop interrupt endpoint */
506 usb2_transfer_stop(sc->sc_xfer[UMCT_INTR_DT_RD]);
507
508 /* stop read endpoint */
509 usb2_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_RD]);
510}
511
512static void
513umct_start_write(struct usb2_com_softc *ucom)
514{
515 struct umct_softc *sc = ucom->sc_parent;
516
517 usb2_transfer_start(sc->sc_xfer[UMCT_BULK_DT_WR]);
518}
519
520static void
521umct_stop_write(struct usb2_com_softc *ucom)
522{
523 struct umct_softc *sc = ucom->sc_parent;
524
525 usb2_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_WR]);
526}
527
528static void
529umct_write_callback(struct usb2_xfer *xfer)
530{
531 struct umct_softc *sc = xfer->priv_sc;
532 uint32_t actlen;
533
534 switch (USB_GET_STATE(xfer)) {
535 case USB_ST_SETUP:
536 case USB_ST_TRANSFERRED:
537tr_setup:
538 if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
539 sc->sc_obufsize, &actlen)) {
540
541 xfer->frlengths[0] = actlen;
542 usb2_start_hardware(xfer);
543 }
544 return;
545
546 default: /* Error */
547 if (xfer->error != USB_ERR_CANCELLED) {
548 /* try to clear stall first */
549 xfer->flags.stall_pipe = 1;
550 goto tr_setup;
551 }
552 return;
553 }
554}
555
556static void
557umct_read_callback(struct usb2_xfer *xfer)
558{
559 struct umct_softc *sc = xfer->priv_sc;
560
561 switch (USB_GET_STATE(xfer)) {
562 case USB_ST_TRANSFERRED:
563 usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers,
564 0, xfer->actlen);
565
566 case USB_ST_SETUP:
567tr_setup:
568 xfer->frlengths[0] = xfer->max_data_length;
569 usb2_start_hardware(xfer);
570 return;
571
572 default: /* Error */
573 if (xfer->error != USB_ERR_CANCELLED) {
574 /* try to clear stall first */
575 xfer->flags.stall_pipe = 1;
576 goto tr_setup;
577 }
578 return;
579 }
580}