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