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 225 static int 226 umct_probe(device_t dev) 227 { 228 struct usb_attach_arg *uaa = device_get_ivars(dev); 229 230 if (uaa->usb_mode != USB_MODE_HOST) { 231 return (ENXIO); 232 } 233 if (uaa->info.bConfigIndex != UMCT_CONFIG_INDEX) { 234 return (ENXIO); 235 } 236 if (uaa->info.bIfaceIndex != UMCT_IFACE_INDEX) { 237 return (ENXIO); 238 } 239 return (usbd_lookup_id_by_uaa(umct_devs, sizeof(umct_devs), uaa)); 240 } 241 242 static int 243 umct_attach(device_t dev) 244 { 245 struct usb_attach_arg *uaa = device_get_ivars(dev); 246 struct umct_softc *sc = device_get_softc(dev); 247 int32_t error; 248 uint16_t maxp; 249 uint8_t iface_index; 250 251 sc->sc_udev = uaa->device; 252 sc->sc_unit = device_get_unit(dev); 253 254 device_set_usb_desc(dev); 255 mtx_init(&sc->sc_mtx, "umct", NULL, MTX_DEF); 256 ucom_ref(&sc->sc_super_ucom); 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 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev); 300 301 return (0); /* success */ 302 303 detach: 304 umct_detach(dev); 305 return (ENXIO); /* failure */ 306 } 307 308 static int 309 umct_detach(device_t dev) 310 { 311 struct umct_softc *sc = device_get_softc(dev); 312 313 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom); 314 usbd_transfer_unsetup(sc->sc_xfer, UMCT_N_TRANSFER); 315 316 device_claim_softc(dev); 317 318 umct_free_softc(sc); 319 320 return (0); 321 } 322 323 UCOM_UNLOAD_DRAIN(umct); 324 325 static void 326 umct_free_softc(struct umct_softc *sc) 327 { 328 if (ucom_unref(&sc->sc_super_ucom)) { 329 mtx_destroy(&sc->sc_mtx); 330 device_free_softc(sc); 331 } 332 } 333 334 static void 335 umct_free(struct ucom_softc *ucom) 336 { 337 umct_free_softc(ucom->sc_parent); 338 } 339 340 static void 341 umct_cfg_do_request(struct umct_softc *sc, uint8_t request, 342 uint16_t len, uint32_t value) 343 { 344 struct usb_device_request req; 345 usb_error_t err; 346 uint8_t temp[4]; 347 348 if (len > 4) 349 len = 4; 350 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 351 req.bRequest = request; 352 USETW(req.wValue, 0); 353 req.wIndex[0] = sc->sc_iface_no; 354 req.wIndex[1] = 0; 355 USETW(req.wLength, len); 356 USETDW(temp, value); 357 358 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 359 &req, temp, 0, 1000); 360 if (err) { 361 DPRINTFN(0, "device request failed, err=%s " 362 "(ignored)\n", usbd_errstr(err)); 363 } 364 return; 365 } 366 367 static void 368 umct_intr_callback_sub(struct usb_xfer *xfer, usb_error_t error) 369 { 370 struct umct_softc *sc = usbd_xfer_softc(xfer); 371 struct usb_page_cache *pc; 372 uint8_t buf[2]; 373 int actlen; 374 375 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 376 377 switch (USB_GET_STATE(xfer)) { 378 case USB_ST_TRANSFERRED: 379 if (actlen < 2) { 380 DPRINTF("too short message\n"); 381 goto tr_setup; 382 } 383 pc = usbd_xfer_get_frame(xfer, 0); 384 usbd_copy_out(pc, 0, buf, sizeof(buf)); 385 386 sc->sc_msr = buf[0]; 387 sc->sc_lsr = buf[1]; 388 389 ucom_status_change(&sc->sc_ucom); 390 391 case USB_ST_SETUP: 392 tr_setup: 393 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 394 usbd_transfer_submit(xfer); 395 return; 396 397 default: /* Error */ 398 if (error != USB_ERR_CANCELLED) { 399 /* try to clear stall first */ 400 usbd_xfer_set_stall(xfer); 401 goto tr_setup; 402 } 403 return; 404 } 405 } 406 407 static void 408 umct_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 409 { 410 struct umct_softc *sc = ucom->sc_parent; 411 412 *lsr = sc->sc_lsr; 413 *msr = sc->sc_msr; 414 } 415 416 static void 417 umct_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff) 418 { 419 struct umct_softc *sc = ucom->sc_parent; 420 421 if (onoff) 422 sc->sc_lcr |= 0x40; 423 else 424 sc->sc_lcr &= ~0x40; 425 426 umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr); 427 } 428 429 static void 430 umct_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 431 { 432 struct umct_softc *sc = ucom->sc_parent; 433 434 if (onoff) 435 sc->sc_mcr |= 0x01; 436 else 437 sc->sc_mcr &= ~0x01; 438 439 umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr); 440 } 441 442 static void 443 umct_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff) 444 { 445 struct umct_softc *sc = ucom->sc_parent; 446 447 if (onoff) 448 sc->sc_mcr |= 0x02; 449 else 450 sc->sc_mcr &= ~0x02; 451 452 umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr); 453 } 454 455 static uint8_t 456 umct_calc_baud(uint32_t baud) 457 { 458 switch (baud) { 459 case B300:return (0x1); 460 case B600: 461 return (0x2); 462 case B1200: 463 return (0x3); 464 case B2400: 465 return (0x4); 466 case B4800: 467 return (0x6); 468 case B9600: 469 return (0x8); 470 case B19200: 471 return (0x9); 472 case B38400: 473 return (0xa); 474 case B57600: 475 return (0xb); 476 case 115200: 477 return (0xc); 478 case B0: 479 default: 480 break; 481 } 482 return (0x0); 483 } 484 485 static int 486 umct_pre_param(struct ucom_softc *ucom, struct termios *t) 487 { 488 return (0); /* we accept anything */ 489 } 490 491 static void 492 umct_cfg_param(struct ucom_softc *ucom, struct termios *t) 493 { 494 struct umct_softc *sc = ucom->sc_parent; 495 uint32_t value; 496 497 value = umct_calc_baud(t->c_ospeed); 498 umct_cfg_do_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value); 499 500 value = (sc->sc_lcr & 0x40); 501 502 switch (t->c_cflag & CSIZE) { 503 case CS5: 504 value |= 0x0; 505 break; 506 case CS6: 507 value |= 0x1; 508 break; 509 case CS7: 510 value |= 0x2; 511 break; 512 default: 513 case CS8: 514 value |= 0x3; 515 break; 516 } 517 518 value |= (t->c_cflag & CSTOPB) ? 0x4 : 0; 519 if (t->c_cflag & PARENB) { 520 value |= 0x8; 521 value |= (t->c_cflag & PARODD) ? 0x0 : 0x10; 522 } 523 /* 524 * XXX There doesn't seem to be a way to tell the device 525 * to use flow control. 526 */ 527 528 sc->sc_lcr = value; 529 umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value); 530 } 531 532 static void 533 umct_start_read(struct ucom_softc *ucom) 534 { 535 struct umct_softc *sc = ucom->sc_parent; 536 537 /* start interrupt endpoint */ 538 usbd_transfer_start(sc->sc_xfer[UMCT_INTR_DT_RD]); 539 540 /* start read endpoint */ 541 usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_RD]); 542 } 543 544 static void 545 umct_stop_read(struct ucom_softc *ucom) 546 { 547 struct umct_softc *sc = ucom->sc_parent; 548 549 /* stop interrupt endpoint */ 550 usbd_transfer_stop(sc->sc_xfer[UMCT_INTR_DT_RD]); 551 552 /* stop read endpoint */ 553 usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_RD]); 554 } 555 556 static void 557 umct_start_write(struct ucom_softc *ucom) 558 { 559 struct umct_softc *sc = ucom->sc_parent; 560 561 usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_WR]); 562 } 563 564 static void 565 umct_stop_write(struct ucom_softc *ucom) 566 { 567 struct umct_softc *sc = ucom->sc_parent; 568 569 usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_WR]); 570 } 571 572 static void 573 umct_read_callback(struct usb_xfer *xfer, usb_error_t error) 574 { 575 struct umct_softc *sc = usbd_xfer_softc(xfer); 576 577 if (sc->sc_swap_cb) 578 umct_intr_callback_sub(xfer, error); 579 else 580 umct_read_callback_sub(xfer, error); 581 } 582 583 static void 584 umct_intr_callback(struct usb_xfer *xfer, usb_error_t error) 585 { 586 struct umct_softc *sc = usbd_xfer_softc(xfer); 587 588 if (sc->sc_swap_cb) 589 umct_read_callback_sub(xfer, error); 590 else 591 umct_intr_callback_sub(xfer, error); 592 } 593 594 static void 595 umct_write_callback(struct usb_xfer *xfer, usb_error_t error) 596 { 597 struct umct_softc *sc = usbd_xfer_softc(xfer); 598 struct usb_page_cache *pc; 599 uint32_t actlen; 600 601 switch (USB_GET_STATE(xfer)) { 602 case USB_ST_SETUP: 603 case USB_ST_TRANSFERRED: 604 tr_setup: 605 pc = usbd_xfer_get_frame(xfer, 0); 606 if (ucom_get_data(&sc->sc_ucom, pc, 0, 607 sc->sc_obufsize, &actlen)) { 608 609 usbd_xfer_set_frame_len(xfer, 0, actlen); 610 usbd_transfer_submit(xfer); 611 } 612 return; 613 614 default: /* Error */ 615 if (error != USB_ERR_CANCELLED) { 616 /* try to clear stall first */ 617 usbd_xfer_set_stall(xfer); 618 goto tr_setup; 619 } 620 return; 621 } 622 } 623 624 static void 625 umct_read_callback_sub(struct usb_xfer *xfer, usb_error_t error) 626 { 627 struct umct_softc *sc = usbd_xfer_softc(xfer); 628 struct usb_page_cache *pc; 629 int actlen; 630 631 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 632 633 switch (USB_GET_STATE(xfer)) { 634 case USB_ST_TRANSFERRED: 635 pc = usbd_xfer_get_frame(xfer, 0); 636 ucom_put_data(&sc->sc_ucom, pc, 0, actlen); 637 638 case USB_ST_SETUP: 639 tr_setup: 640 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 641 usbd_transfer_submit(xfer); 642 return; 643 644 default: /* Error */ 645 if (error != USB_ERR_CANCELLED) { 646 /* try to clear stall first */ 647 usbd_xfer_set_stall(xfer); 648 goto tr_setup; 649 } 650 return; 651 } 652 } 653 654 static void 655 umct_poll(struct ucom_softc *ucom) 656 { 657 struct umct_softc *sc = ucom->sc_parent; 658 usbd_transfer_poll(sc->sc_xfer, UMCT_N_TRANSFER); 659 } 660