Deleted Added
sdiff udiff text old ( 189275 ) new ( 190174 )
full compact
1#include <sys/cdefs.h>
2__FBSDID("$FreeBSD: head/sys/dev/usb/serial/umct.c 189275 2009-03-02 05:37:05Z 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>
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}