1 /*- 2 * Copyright (c) 2003 Scott Long 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 */ 27 28 /* 29 * Driver for the MCT (Magic Control Technology) USB-RS232 Converter. 30 * Based on the superb documentation from the linux mct_u232 driver by 31 * Wolfgang Grandeggar <wolfgang@cec.ch>. 32 * This device smells a lot like the Belkin F5U103, except that it has 33 * suffered some mild brain-damage. This driver is based off of the ubsa.c 34 * driver from Alexander Kabaev <kan@FreeBSD.org>. Merging the two together 35 * might be useful, though the subtle differences might lead to lots of 36 * #ifdef's. 37 */ 38 39 /* 40 * NOTE: all function names beginning like "umct_cfg_" can only 41 * be called from within the config thread function ! 42 */ 43 44 #include <sys/stdint.h> 45 #include <sys/param.h> 46 #include <sys/queue.h> 47 #include <sys/types.h> 48 #include <sys/systm.h> 49 #include <sys/kernel.h> 50 #include <sys/bus.h> 51 #include <sys/module.h> 52 #include <sys/lock.h> 53 #include <sys/condvar.h> 54 #include <sys/sysctl.h> 55 #include <sys/unistd.h> 56 #include <sys/callout.h> 57 #include <sys/malloc.h> 58 #include <sys/priv.h> 59 60 #include <bus/u4b/usb.h> 61 #include <bus/u4b/usbdi.h> 62 #include <bus/u4b/usbdi_util.h> 63 #include "usbdevs.h" 64 65 #define USB_DEBUG_VAR usb_debug 66 #include <bus/u4b/usb_debug.h> 67 #include <bus/u4b/usb_process.h> 68 69 #include <bus/u4b/serial/usb_serial.h> 70 71 /* The UMCT advertises the standard 8250 UART registers */ 72 #define UMCT_GET_MSR 2 /* Get Modem Status Register */ 73 #define UMCT_GET_MSR_SIZE 1 74 #define UMCT_GET_LCR 6 /* Get Line Control Register */ 75 #define UMCT_GET_LCR_SIZE 1 76 #define UMCT_SET_BAUD 5 /* Set the Baud Rate Divisor */ 77 #define UMCT_SET_BAUD_SIZE 4 78 #define UMCT_SET_LCR 7 /* Set Line Control Register */ 79 #define UMCT_SET_LCR_SIZE 1 80 #define UMCT_SET_MCR 10 /* Set Modem Control Register */ 81 #define UMCT_SET_MCR_SIZE 1 82 83 #define UMCT_INTR_INTERVAL 100 84 #define UMCT_IFACE_INDEX 0 85 #define UMCT_CONFIG_INDEX 0 86 87 enum { 88 UMCT_BULK_DT_WR, 89 UMCT_BULK_DT_RD, 90 UMCT_INTR_DT_RD, 91 UMCT_N_TRANSFER, 92 }; 93 94 struct umct_softc { 95 struct ucom_super_softc sc_super_ucom; 96 struct ucom_softc sc_ucom; 97 98 struct usb_device *sc_udev; 99 struct usb_xfer *sc_xfer[UMCT_N_TRANSFER]; 100 struct lock sc_lock; 101 102 uint32_t sc_unit; 103 104 uint16_t sc_obufsize; 105 106 uint8_t sc_lsr; 107 uint8_t sc_msr; 108 uint8_t sc_lcr; 109 uint8_t sc_mcr; 110 uint8_t sc_iface_no; 111 uint8_t sc_swap_cb; 112 uint8_t sc_name[16]; 113 }; 114 115 /* prototypes */ 116 117 static device_probe_t umct_probe; 118 static device_attach_t umct_attach; 119 static device_detach_t umct_detach; 120 121 static usb_callback_t umct_intr_callback; 122 static usb_callback_t umct_intr_callback_sub; 123 static usb_callback_t umct_read_callback; 124 static usb_callback_t umct_read_callback_sub; 125 static usb_callback_t umct_write_callback; 126 127 static void umct_cfg_do_request(struct umct_softc *sc, uint8_t request, 128 uint16_t len, uint32_t value); 129 static void umct_cfg_get_status(struct ucom_softc *, uint8_t *, 130 uint8_t *); 131 static void umct_cfg_set_break(struct ucom_softc *, uint8_t); 132 static void umct_cfg_set_dtr(struct ucom_softc *, uint8_t); 133 static void umct_cfg_set_rts(struct ucom_softc *, uint8_t); 134 static uint8_t umct_calc_baud(uint32_t); 135 static int umct_pre_param(struct ucom_softc *, struct termios *); 136 static void umct_cfg_param(struct ucom_softc *, struct termios *); 137 static void umct_start_read(struct ucom_softc *); 138 static void umct_stop_read(struct ucom_softc *); 139 static void umct_start_write(struct ucom_softc *); 140 static void umct_stop_write(struct ucom_softc *); 141 static void umct_poll(struct ucom_softc *ucom); 142 143 static const struct usb_config umct_config[UMCT_N_TRANSFER] = { 144 145 [UMCT_BULK_DT_WR] = { 146 .type = UE_BULK, 147 .endpoint = UE_ADDR_ANY, 148 .direction = UE_DIR_OUT, 149 .bufsize = 0, /* use wMaxPacketSize */ 150 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 151 .callback = &umct_write_callback, 152 }, 153 154 [UMCT_BULK_DT_RD] = { 155 .type = UE_INTERRUPT, 156 .endpoint = UE_ADDR_ANY, 157 .direction = UE_DIR_IN, 158 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 159 .bufsize = 0, /* use wMaxPacketSize */ 160 .callback = &umct_read_callback, 161 .ep_index = 0, /* first interrupt endpoint */ 162 }, 163 164 [UMCT_INTR_DT_RD] = { 165 .type = UE_INTERRUPT, 166 .endpoint = UE_ADDR_ANY, 167 .direction = UE_DIR_IN, 168 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 169 .bufsize = 0, /* use wMaxPacketSize */ 170 .callback = &umct_intr_callback, 171 .ep_index = 1, /* second interrupt endpoint */ 172 }, 173 }; 174 175 static const struct ucom_callback umct_callback = { 176 .ucom_cfg_get_status = &umct_cfg_get_status, 177 .ucom_cfg_set_dtr = &umct_cfg_set_dtr, 178 .ucom_cfg_set_rts = &umct_cfg_set_rts, 179 .ucom_cfg_set_break = &umct_cfg_set_break, 180 .ucom_cfg_param = &umct_cfg_param, 181 .ucom_pre_param = &umct_pre_param, 182 .ucom_start_read = &umct_start_read, 183 .ucom_stop_read = &umct_stop_read, 184 .ucom_start_write = &umct_start_write, 185 .ucom_stop_write = &umct_stop_write, 186 .ucom_poll = &umct_poll, 187 }; 188 189 static const STRUCT_USB_HOST_ID umct_devs[] = { 190 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232, 0)}, 191 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232, 0)}, 192 {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232, 0)}, 193 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109, 0)}, 194 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U409, 0)}, 195 }; 196 197 static device_method_t umct_methods[] = { 198 DEVMETHOD(device_probe, umct_probe), 199 DEVMETHOD(device_attach, umct_attach), 200 DEVMETHOD(device_detach, umct_detach), 201 DEVMETHOD_END 202 }; 203 204 static devclass_t umct_devclass; 205 206 static driver_t umct_driver = { 207 .name = "umct", 208 .methods = umct_methods, 209 .size = sizeof(struct umct_softc), 210 }; 211 212 DRIVER_MODULE(umct, uhub, umct_driver, umct_devclass, NULL, NULL); 213 MODULE_DEPEND(umct, ucom, 1, 1, 1); 214 MODULE_DEPEND(umct, usb, 1, 1, 1); 215 MODULE_VERSION(umct, 1); 216 217 static int 218 umct_probe(device_t dev) 219 { 220 struct usb_attach_arg *uaa = device_get_ivars(dev); 221 222 if (uaa->usb_mode != USB_MODE_HOST) { 223 return (ENXIO); 224 } 225 if (uaa->info.bConfigIndex != UMCT_CONFIG_INDEX) { 226 return (ENXIO); 227 } 228 if (uaa->info.bIfaceIndex != UMCT_IFACE_INDEX) { 229 return (ENXIO); 230 } 231 return (usbd_lookup_id_by_uaa(umct_devs, sizeof(umct_devs), uaa)); 232 } 233 234 static int 235 umct_attach(device_t dev) 236 { 237 struct usb_attach_arg *uaa = device_get_ivars(dev); 238 struct umct_softc *sc = device_get_softc(dev); 239 int32_t error; 240 uint16_t maxp; 241 uint8_t iface_index; 242 243 sc->sc_udev = uaa->device; 244 sc->sc_unit = device_get_unit(dev); 245 246 device_set_usb_desc(dev); 247 lockinit(&sc->sc_lock, "umct", 0, LK_CANRECURSE); 248 249 ksnprintf(sc->sc_name, sizeof(sc->sc_name), 250 "%s", device_get_nameunit(dev)); 251 252 sc->sc_iface_no = uaa->info.bIfaceNum; 253 254 iface_index = UMCT_IFACE_INDEX; 255 error = usbd_transfer_setup(uaa->device, &iface_index, 256 sc->sc_xfer, umct_config, UMCT_N_TRANSFER, sc, &sc->sc_lock); 257 258 if (error) { 259 device_printf(dev, "allocating USB " 260 "transfers failed\n"); 261 goto detach; 262 } 263 264 /* 265 * The real bulk-in endpoint is also marked as an interrupt. 266 * The only way to differentiate it from the real interrupt 267 * endpoint is to look at the wMaxPacketSize field. 268 */ 269 maxp = usbd_xfer_max_framelen(sc->sc_xfer[UMCT_BULK_DT_RD]); 270 if (maxp == 0x2) { 271 272 /* guessed wrong - switch around endpoints */ 273 274 struct usb_xfer *temp = sc->sc_xfer[UMCT_INTR_DT_RD]; 275 276 sc->sc_xfer[UMCT_INTR_DT_RD] = sc->sc_xfer[UMCT_BULK_DT_RD]; 277 sc->sc_xfer[UMCT_BULK_DT_RD] = temp; 278 sc->sc_swap_cb = 1; 279 } 280 281 sc->sc_obufsize = usbd_xfer_max_len(sc->sc_xfer[UMCT_BULK_DT_WR]); 282 283 if (uaa->info.idProduct == USB_PRODUCT_MCT_SITECOM_USB232) { 284 if (sc->sc_obufsize > 16) { 285 sc->sc_obufsize = 16; 286 } 287 } 288 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 289 &umct_callback, &sc->sc_lock); 290 if (error) { 291 goto detach; 292 } 293 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev); 294 295 return (0); /* success */ 296 297 detach: 298 umct_detach(dev); 299 return (ENXIO); /* failure */ 300 } 301 302 static int 303 umct_detach(device_t dev) 304 { 305 struct umct_softc *sc = device_get_softc(dev); 306 307 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom); 308 usbd_transfer_unsetup(sc->sc_xfer, UMCT_N_TRANSFER); 309 lockuninit(&sc->sc_lock); 310 311 return (0); 312 } 313 314 static void 315 umct_cfg_do_request(struct umct_softc *sc, uint8_t request, 316 uint16_t len, uint32_t value) 317 { 318 struct usb_device_request req; 319 usb_error_t err; 320 uint8_t temp[4]; 321 322 if (len > 4) 323 len = 4; 324 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 325 req.bRequest = request; 326 USETW(req.wValue, 0); 327 req.wIndex[0] = sc->sc_iface_no; 328 req.wIndex[1] = 0; 329 USETW(req.wLength, len); 330 USETDW(temp, value); 331 332 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 333 &req, temp, 0, 1000); 334 if (err) { 335 DPRINTFN(0, "device request failed, err=%s " 336 "(ignored)\n", usbd_errstr(err)); 337 } 338 return; 339 } 340 341 static void 342 umct_intr_callback_sub(struct usb_xfer *xfer, usb_error_t error) 343 { 344 struct umct_softc *sc = usbd_xfer_softc(xfer); 345 struct usb_page_cache *pc; 346 uint8_t buf[2]; 347 int actlen; 348 349 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 350 351 switch (USB_GET_STATE(xfer)) { 352 case USB_ST_TRANSFERRED: 353 if (actlen < 2) { 354 DPRINTF("too short message\n"); 355 goto tr_setup; 356 } 357 pc = usbd_xfer_get_frame(xfer, 0); 358 usbd_copy_out(pc, 0, buf, sizeof(buf)); 359 360 sc->sc_msr = buf[0]; 361 sc->sc_lsr = buf[1]; 362 363 ucom_status_change(&sc->sc_ucom); 364 365 case USB_ST_SETUP: 366 tr_setup: 367 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 368 usbd_transfer_submit(xfer); 369 return; 370 371 default: /* Error */ 372 if (error != USB_ERR_CANCELLED) { 373 /* try to clear stall first */ 374 usbd_xfer_set_stall(xfer); 375 goto tr_setup; 376 } 377 return; 378 } 379 } 380 381 static void 382 umct_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 383 { 384 struct umct_softc *sc = ucom->sc_parent; 385 386 *lsr = sc->sc_lsr; 387 *msr = sc->sc_msr; 388 } 389 390 static void 391 umct_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff) 392 { 393 struct umct_softc *sc = ucom->sc_parent; 394 395 if (onoff) 396 sc->sc_lcr |= 0x40; 397 else 398 sc->sc_lcr &= ~0x40; 399 400 umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr); 401 } 402 403 static void 404 umct_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 405 { 406 struct umct_softc *sc = ucom->sc_parent; 407 408 if (onoff) 409 sc->sc_mcr |= 0x01; 410 else 411 sc->sc_mcr &= ~0x01; 412 413 umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr); 414 } 415 416 static void 417 umct_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff) 418 { 419 struct umct_softc *sc = ucom->sc_parent; 420 421 if (onoff) 422 sc->sc_mcr |= 0x02; 423 else 424 sc->sc_mcr &= ~0x02; 425 426 umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr); 427 } 428 429 static uint8_t 430 umct_calc_baud(uint32_t baud) 431 { 432 switch (baud) { 433 case B300:return (0x1); 434 case B600: 435 return (0x2); 436 case B1200: 437 return (0x3); 438 case B2400: 439 return (0x4); 440 case B4800: 441 return (0x6); 442 case B9600: 443 return (0x8); 444 case B19200: 445 return (0x9); 446 case B38400: 447 return (0xa); 448 case B57600: 449 return (0xb); 450 case 115200: 451 return (0xc); 452 case B0: 453 default: 454 break; 455 } 456 return (0x0); 457 } 458 459 static int 460 umct_pre_param(struct ucom_softc *ucom, struct termios *t) 461 { 462 return (0); /* we accept anything */ 463 } 464 465 static void 466 umct_cfg_param(struct ucom_softc *ucom, struct termios *t) 467 { 468 struct umct_softc *sc = ucom->sc_parent; 469 uint32_t value; 470 471 value = umct_calc_baud(t->c_ospeed); 472 umct_cfg_do_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value); 473 474 value = (sc->sc_lcr & 0x40); 475 476 switch (t->c_cflag & CSIZE) { 477 case CS5: 478 value |= 0x0; 479 break; 480 case CS6: 481 value |= 0x1; 482 break; 483 case CS7: 484 value |= 0x2; 485 break; 486 default: 487 case CS8: 488 value |= 0x3; 489 break; 490 } 491 492 value |= (t->c_cflag & CSTOPB) ? 0x4 : 0; 493 if (t->c_cflag & PARENB) { 494 value |= 0x8; 495 value |= (t->c_cflag & PARODD) ? 0x0 : 0x10; 496 } 497 /* 498 * XXX There doesn't seem to be a way to tell the device 499 * to use flow control. 500 */ 501 502 sc->sc_lcr = value; 503 umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value); 504 } 505 506 static void 507 umct_start_read(struct ucom_softc *ucom) 508 { 509 struct umct_softc *sc = ucom->sc_parent; 510 511 /* start interrupt endpoint */ 512 usbd_transfer_start(sc->sc_xfer[UMCT_INTR_DT_RD]); 513 514 /* start read endpoint */ 515 usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_RD]); 516 } 517 518 static void 519 umct_stop_read(struct ucom_softc *ucom) 520 { 521 struct umct_softc *sc = ucom->sc_parent; 522 523 /* stop interrupt endpoint */ 524 usbd_transfer_stop(sc->sc_xfer[UMCT_INTR_DT_RD]); 525 526 /* stop read endpoint */ 527 usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_RD]); 528 } 529 530 static void 531 umct_start_write(struct ucom_softc *ucom) 532 { 533 struct umct_softc *sc = ucom->sc_parent; 534 535 usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_WR]); 536 } 537 538 static void 539 umct_stop_write(struct ucom_softc *ucom) 540 { 541 struct umct_softc *sc = ucom->sc_parent; 542 543 usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_WR]); 544 } 545 546 static void 547 umct_read_callback(struct usb_xfer *xfer, usb_error_t error) 548 { 549 struct umct_softc *sc = usbd_xfer_softc(xfer); 550 551 if (sc->sc_swap_cb) 552 umct_intr_callback_sub(xfer, error); 553 else 554 umct_read_callback_sub(xfer, error); 555 } 556 557 static void 558 umct_intr_callback(struct usb_xfer *xfer, usb_error_t error) 559 { 560 struct umct_softc *sc = usbd_xfer_softc(xfer); 561 562 if (sc->sc_swap_cb) 563 umct_read_callback_sub(xfer, error); 564 else 565 umct_intr_callback_sub(xfer, error); 566 } 567 568 static void 569 umct_write_callback(struct usb_xfer *xfer, usb_error_t error) 570 { 571 struct umct_softc *sc = usbd_xfer_softc(xfer); 572 struct usb_page_cache *pc; 573 uint32_t actlen; 574 575 switch (USB_GET_STATE(xfer)) { 576 case USB_ST_SETUP: 577 case USB_ST_TRANSFERRED: 578 tr_setup: 579 pc = usbd_xfer_get_frame(xfer, 0); 580 if (ucom_get_data(&sc->sc_ucom, pc, 0, 581 sc->sc_obufsize, &actlen)) { 582 583 usbd_xfer_set_frame_len(xfer, 0, actlen); 584 usbd_transfer_submit(xfer); 585 } 586 return; 587 588 default: /* Error */ 589 if (error != USB_ERR_CANCELLED) { 590 /* try to clear stall first */ 591 usbd_xfer_set_stall(xfer); 592 goto tr_setup; 593 } 594 return; 595 } 596 } 597 598 static void 599 umct_read_callback_sub(struct usb_xfer *xfer, usb_error_t error) 600 { 601 struct umct_softc *sc = usbd_xfer_softc(xfer); 602 struct usb_page_cache *pc; 603 int actlen; 604 605 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 606 607 switch (USB_GET_STATE(xfer)) { 608 case USB_ST_TRANSFERRED: 609 pc = usbd_xfer_get_frame(xfer, 0); 610 ucom_put_data(&sc->sc_ucom, pc, 0, actlen); 611 612 case USB_ST_SETUP: 613 tr_setup: 614 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 615 usbd_transfer_submit(xfer); 616 return; 617 618 default: /* Error */ 619 if (error != USB_ERR_CANCELLED) { 620 /* try to clear stall first */ 621 usbd_xfer_set_stall(xfer); 622 goto tr_setup; 623 } 624 return; 625 } 626 } 627 628 static void 629 umct_poll(struct ucom_softc *ucom) 630 { 631 struct umct_softc *sc = ucom->sc_parent; 632 usbd_transfer_poll(sc->sc_xfer, UMCT_N_TRANSFER); 633 } 634