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