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