1 /* $OpenBSD: umct.c,v 1.28 2008/06/26 05:42:19 ray Exp $ */ 2 /* $NetBSD: umct.c,v 1.10 2003/02/23 04:20:07 simonb Exp $ */ 3 /* 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Ichiro FUKUHARA (ichiro@ichiro.org). 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * MCT USB-RS232 Interface Controller 34 * http://www.mct.com.tw/prod/rs232.html 35 * http://www.dlink.com/products/usb/dsbs25 36 */ 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/malloc.h> 42 #include <sys/ioctl.h> 43 #include <sys/conf.h> 44 #include <sys/tty.h> 45 #include <sys/file.h> 46 #include <sys/selinfo.h> 47 #include <sys/proc.h> 48 #include <sys/vnode.h> 49 #include <sys/device.h> 50 #include <sys/poll.h> 51 52 #include <dev/usb/usb.h> 53 #include <dev/usb/usbcdc.h> 54 55 #include <dev/usb/usbdi.h> 56 #include <dev/usb/usbdi_util.h> 57 #include <dev/usb/usbdevs.h> 58 #include <dev/usb/usb_quirks.h> 59 60 #include <dev/usb/usbdevs.h> 61 #include <dev/usb/ucomvar.h> 62 63 #include <dev/usb/umct.h> 64 65 #ifdef UMCT_DEBUG 66 #define DPRINTFN(n, x) do { if (umctdebug > (n)) printf x; } while (0) 67 int umctdebug = 0; 68 #else 69 #define DPRINTFN(n, x) 70 #endif 71 #define DPRINTF(x) DPRINTFN(0, x) 72 73 #define UMCT_CONFIG_INDEX 0 74 #define UMCT_IFACE_INDEX 0 75 76 struct umct_softc { 77 struct device sc_dev; /* base device */ 78 usbd_device_handle sc_udev; /* USB device */ 79 usbd_interface_handle sc_iface; /* interface */ 80 int sc_iface_number; /* interface number */ 81 u_int16_t sc_product; 82 83 int sc_intr_number; /* interrupt number */ 84 usbd_pipe_handle sc_intr_pipe; /* interrupt pipe */ 85 u_char *sc_intr_buf; /* interrupt buffer */ 86 int sc_isize; 87 88 usb_cdc_line_state_t sc_line_state; /* current line state */ 89 u_char sc_dtr; /* current DTR state */ 90 u_char sc_rts; /* current RTS state */ 91 u_char sc_break; /* set break */ 92 93 u_char sc_status; 94 95 struct device *sc_subdev; /* ucom device */ 96 97 u_char sc_dying; /* disconnecting */ 98 99 u_char sc_lsr; /* Local status register */ 100 u_char sc_msr; /* umct status register */ 101 102 u_int last_lcr; /* keep lcr register */ 103 }; 104 105 /* 106 * These are the maximum number of bytes transferred per frame. 107 * The output buffer size cannot be increased due to the size encoding. 108 */ 109 #define UMCTIBUFSIZE 256 110 #define UMCTOBUFSIZE 256 111 112 void umct_init(struct umct_softc *); 113 void umct_set_baudrate(struct umct_softc *, u_int); 114 void umct_set_lcr(struct umct_softc *, u_int); 115 void umct_intr(usbd_xfer_handle, usbd_private_handle, usbd_status); 116 117 void umct_set(void *, int, int, int); 118 void umct_dtr(struct umct_softc *, int); 119 void umct_rts(struct umct_softc *, int); 120 void umct_break(struct umct_softc *, int); 121 void umct_set_line_state(struct umct_softc *); 122 void umct_get_status(void *, int portno, u_char *lsr, u_char *msr); 123 int umct_param(void *, int, struct termios *); 124 int umct_open(void *, int); 125 void umct_close(void *, int); 126 127 struct ucom_methods umct_methods = { 128 umct_get_status, 129 umct_set, 130 umct_param, 131 NULL, 132 umct_open, 133 umct_close, 134 NULL, 135 NULL, 136 }; 137 138 static const struct usb_devno umct_devs[] = { 139 /* MCT USB-232 Interface Products */ 140 { USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232 }, 141 /* Sitecom USB-232 Products */ 142 { USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232 }, 143 /* D-Link DU-H3SP USB BAY Hub Products */ 144 { USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232 }, 145 /* BELKIN F5U109 */ 146 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109 }, 147 /* BELKIN F5U409 */ 148 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U409 }, 149 }; 150 #define umct_lookup(v, p) usb_lookup(umct_devs, v, p) 151 152 int umct_match(struct device *, void *, void *); 153 void umct_attach(struct device *, struct device *, void *); 154 int umct_detach(struct device *, int); 155 int umct_activate(struct device *, enum devact); 156 157 struct cfdriver umct_cd = { 158 NULL, "umct", DV_DULL 159 }; 160 161 const struct cfattach umct_ca = { 162 sizeof(struct umct_softc), 163 umct_match, 164 umct_attach, 165 umct_detach, 166 umct_activate, 167 }; 168 169 int 170 umct_match(struct device *parent, void *match, void *aux) 171 { 172 struct usb_attach_arg *uaa = aux; 173 174 if (uaa->iface != NULL) 175 return (UMATCH_NONE); 176 177 return (umct_lookup(uaa->vendor, uaa->product) != NULL ? 178 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 179 } 180 181 void 182 umct_attach(struct device *parent, struct device *self, void *aux) 183 { 184 struct umct_softc *sc = (struct umct_softc *)self; 185 struct usb_attach_arg *uaa = aux; 186 usbd_device_handle dev = uaa->device; 187 usb_config_descriptor_t *cdesc; 188 usb_interface_descriptor_t *id; 189 usb_endpoint_descriptor_t *ed; 190 191 char *devname = sc->sc_dev.dv_xname; 192 usbd_status err; 193 int i; 194 struct ucom_attach_args uca; 195 196 sc->sc_udev = dev; 197 sc->sc_product = uaa->product; 198 199 DPRINTF(("\n\numct attach: sc=%p\n", sc)); 200 201 /* initialize endpoints */ 202 uca.bulkin = uca.bulkout = -1; 203 sc->sc_intr_number = -1; 204 sc->sc_intr_pipe = NULL; 205 206 /* Move the device into the configured state. */ 207 err = usbd_set_config_index(dev, UMCT_CONFIG_INDEX, 1); 208 if (err) { 209 printf("\n%s: failed to set configuration, err=%s\n", 210 devname, usbd_errstr(err)); 211 sc->sc_dying = 1; 212 return; 213 } 214 215 /* get the config descriptor */ 216 cdesc = usbd_get_config_descriptor(sc->sc_udev); 217 218 if (cdesc == NULL) { 219 printf("%s: failed to get configuration descriptor\n", 220 sc->sc_dev.dv_xname); 221 sc->sc_dying = 1; 222 return; 223 } 224 225 /* get the interface */ 226 err = usbd_device2interface_handle(dev, UMCT_IFACE_INDEX, 227 &sc->sc_iface); 228 if (err) { 229 printf("\n%s: failed to get interface, err=%s\n", 230 devname, usbd_errstr(err)); 231 sc->sc_dying = 1; 232 return; 233 } 234 235 /* Find the bulk{in,out} and interrupt endpoints */ 236 237 id = usbd_get_interface_descriptor(sc->sc_iface); 238 sc->sc_iface_number = id->bInterfaceNumber; 239 240 for (i = 0; i < id->bNumEndpoints; i++) { 241 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 242 if (ed == NULL) { 243 printf("%s: no endpoint descriptor for %d\n", 244 sc->sc_dev.dv_xname, i); 245 sc->sc_dying = 1; 246 return; 247 } 248 249 /* 250 * The Bulkin endpoint is marked as an interrupt. Since 251 * we can't rely on the endpoint descriptor order, we'll 252 * check the wMaxPacketSize field to differentiate. 253 */ 254 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 255 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT && 256 UGETW(ed->wMaxPacketSize) != 0x2) { 257 uca.bulkin = ed->bEndpointAddress; 258 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 259 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 260 uca.bulkout = ed->bEndpointAddress; 261 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 262 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 263 sc->sc_intr_number = ed->bEndpointAddress; 264 sc->sc_isize = UGETW(ed->wMaxPacketSize); 265 } 266 } 267 268 if (uca.bulkin == -1) { 269 printf("%s: Could not find data bulk in\n", 270 sc->sc_dev.dv_xname); 271 sc->sc_dying = 1; 272 return; 273 } 274 275 if (uca.bulkout == -1) { 276 printf("%s: Could not find data bulk out\n", 277 sc->sc_dev.dv_xname); 278 sc->sc_dying = 1; 279 return; 280 } 281 282 if (sc->sc_intr_number== -1) { 283 printf("%s: Could not find interrupt in\n", 284 sc->sc_dev.dv_xname); 285 sc->sc_dying = 1; 286 return; 287 } 288 289 sc->sc_dtr = sc->sc_rts = 0; 290 uca.portno = UCOM_UNK_PORTNO; 291 /* bulkin, bulkout set above */ 292 uca.ibufsize = UMCTIBUFSIZE; 293 if (sc->sc_product == USB_PRODUCT_MCT_SITECOM_USB232) 294 uca.obufsize = 16; /* device is broken */ 295 else 296 uca.obufsize = UMCTOBUFSIZE; 297 uca.ibufsizepad = UMCTIBUFSIZE; 298 uca.opkthdrlen = 0; 299 uca.device = dev; 300 uca.iface = sc->sc_iface; 301 uca.methods = &umct_methods; 302 uca.arg = sc; 303 uca.info = NULL; 304 305 umct_init(sc); 306 307 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 308 &sc->sc_dev); 309 310 DPRINTF(("umct: in=0x%x out=0x%x intr=0x%x\n", 311 uca.bulkin, uca.bulkout, sc->sc_intr_number )); 312 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch); 313 } 314 315 int 316 umct_detach(struct device *self, int flags) 317 { 318 struct umct_softc *sc = (struct umct_softc *)self; 319 int rv = 0; 320 321 DPRINTF(("umct_detach: sc=%p flags=%d\n", sc, flags)); 322 323 if (sc->sc_intr_pipe != NULL) { 324 usbd_abort_pipe(sc->sc_intr_pipe); 325 usbd_close_pipe(sc->sc_intr_pipe); 326 free(sc->sc_intr_buf, M_USBDEV); 327 sc->sc_intr_pipe = NULL; 328 } 329 330 sc->sc_dying = 1; 331 if (sc->sc_subdev != NULL) { 332 rv = config_detach(sc->sc_subdev, flags); 333 sc->sc_subdev = NULL; 334 } 335 336 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 337 &sc->sc_dev); 338 339 return (rv); 340 } 341 342 int 343 umct_activate(struct device *self, enum devact act) 344 { 345 struct umct_softc *sc = (struct umct_softc *)self; 346 int rv = 0; 347 348 switch (act) { 349 case DVACT_ACTIVATE: 350 break; 351 352 case DVACT_DEACTIVATE: 353 if (sc->sc_subdev != NULL) 354 rv = config_deactivate(sc->sc_subdev); 355 sc->sc_dying = 1; 356 break; 357 } 358 return (rv); 359 } 360 361 void 362 umct_set_line_state(struct umct_softc *sc) 363 { 364 usb_device_request_t req; 365 uByte ls; 366 367 ls = (sc->sc_dtr ? MCR_DTR : 0) | 368 (sc->sc_rts ? MCR_RTS : 0); 369 370 DPRINTF(("umct_set_line_state: DTR=%d,RTS=%d,ls=%02x\n", 371 sc->sc_dtr, sc->sc_rts, ls)); 372 373 req.bmRequestType = UMCT_SET_REQUEST; 374 req.bRequest = REQ_SET_MCR; 375 USETW(req.wValue, 0); 376 USETW(req.wIndex, sc->sc_iface_number); 377 USETW(req.wLength, LENGTH_SET_MCR); 378 379 (void)usbd_do_request(sc->sc_udev, &req, &ls); 380 } 381 382 void 383 umct_set(void *addr, int portno, int reg, int onoff) 384 { 385 struct umct_softc *sc = addr; 386 387 switch (reg) { 388 case UCOM_SET_DTR: 389 umct_dtr(sc, onoff); 390 break; 391 case UCOM_SET_RTS: 392 umct_rts(sc, onoff); 393 break; 394 case UCOM_SET_BREAK: 395 umct_break(sc, onoff); 396 break; 397 default: 398 break; 399 } 400 } 401 402 void 403 umct_dtr(struct umct_softc *sc, int onoff) 404 { 405 406 DPRINTF(("umct_dtr: onoff=%d\n", onoff)); 407 408 if (sc->sc_dtr == onoff) 409 return; 410 sc->sc_dtr = onoff; 411 412 umct_set_line_state(sc); 413 } 414 415 void 416 umct_rts(struct umct_softc *sc, int onoff) 417 { 418 DPRINTF(("umct_rts: onoff=%d\n", onoff)); 419 420 if (sc->sc_rts == onoff) 421 return; 422 sc->sc_rts = onoff; 423 424 umct_set_line_state(sc); 425 } 426 427 void 428 umct_break(struct umct_softc *sc, int onoff) 429 { 430 DPRINTF(("umct_break: onoff=%d\n", onoff)); 431 432 umct_set_lcr(sc, onoff ? sc->last_lcr | LCR_SET_BREAK : 433 sc->last_lcr); 434 } 435 436 void 437 umct_set_lcr(struct umct_softc *sc, u_int data) 438 { 439 usb_device_request_t req; 440 uByte adata; 441 442 adata = data; 443 req.bmRequestType = UMCT_SET_REQUEST; 444 req.bRequest = REQ_SET_LCR; 445 USETW(req.wValue, 0); 446 USETW(req.wIndex, sc->sc_iface_number); 447 USETW(req.wLength, LENGTH_SET_LCR); 448 449 /* XXX should check */ 450 (void)usbd_do_request(sc->sc_udev, &req, &adata); 451 } 452 453 void 454 umct_set_baudrate(struct umct_softc *sc, u_int rate) 455 { 456 usb_device_request_t req; 457 uDWord arate; 458 u_int val; 459 460 if (sc->sc_product == USB_PRODUCT_MCT_SITECOM_USB232 || 461 sc->sc_product == USB_PRODUCT_BELKIN_F5U109) { 462 switch (rate) { 463 case 300: val = 0x01; break; 464 case 600: val = 0x02; break; 465 case 1200: val = 0x03; break; 466 case 2400: val = 0x04; break; 467 case 4800: val = 0x06; break; 468 case 9600: val = 0x08; break; 469 case 19200: val = 0x09; break; 470 case 38400: val = 0x0a; break; 471 case 57600: val = 0x0b; break; 472 case 115200: val = 0x0c; break; 473 default: val = -1; break; 474 } 475 } else { 476 val = UMCT_BAUD_RATE(rate); 477 } 478 USETDW(arate, val); 479 480 req.bmRequestType = UMCT_SET_REQUEST; 481 req.bRequest = REQ_SET_BAUD_RATE; 482 USETW(req.wValue, 0); 483 USETW(req.wIndex, sc->sc_iface_number); 484 USETW(req.wLength, LENGTH_BAUD_RATE); 485 486 /* XXX should check */ 487 (void)usbd_do_request(sc->sc_udev, &req, arate); 488 } 489 490 void 491 umct_init(struct umct_softc *sc) 492 { 493 umct_set_baudrate(sc, 9600); 494 umct_set_lcr(sc, LCR_DATA_BITS_8 | LCR_PARITY_NONE | LCR_STOP_BITS_1); 495 } 496 497 int 498 umct_param(void *addr, int portno, struct termios *t) 499 { 500 struct umct_softc *sc = addr; 501 u_int data = 0; 502 503 DPRINTF(("umct_param: sc=%p\n", sc)); 504 505 DPRINTF(("umct_param: BAUDRATE=%d\n", t->c_ospeed)); 506 507 if (ISSET(t->c_cflag, CSTOPB)) 508 data |= LCR_STOP_BITS_2; 509 else 510 data |= LCR_STOP_BITS_1; 511 if (ISSET(t->c_cflag, PARENB)) { 512 if (ISSET(t->c_cflag, PARODD)) 513 data |= LCR_PARITY_ODD; 514 else 515 data |= LCR_PARITY_EVEN; 516 } else 517 data |= LCR_PARITY_NONE; 518 switch (ISSET(t->c_cflag, CSIZE)) { 519 case CS5: 520 data |= LCR_DATA_BITS_5; 521 break; 522 case CS6: 523 data |= LCR_DATA_BITS_6; 524 break; 525 case CS7: 526 data |= LCR_DATA_BITS_7; 527 break; 528 case CS8: 529 data |= LCR_DATA_BITS_8; 530 break; 531 } 532 533 umct_set_baudrate(sc, t->c_ospeed); 534 535 sc->last_lcr = data; 536 umct_set_lcr(sc, data); 537 538 return (0); 539 } 540 541 int 542 umct_open(void *addr, int portno) 543 { 544 struct umct_softc *sc = addr; 545 int err, lcr_data; 546 547 if (sc->sc_dying) 548 return (EIO); 549 550 DPRINTF(("umct_open: sc=%p\n", sc)); 551 552 /* initialize LCR */ 553 lcr_data = LCR_DATA_BITS_8 | LCR_PARITY_NONE | 554 LCR_STOP_BITS_1; 555 umct_set_lcr(sc, lcr_data); 556 557 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) { 558 sc->sc_status = 0; /* clear status bit */ 559 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK); 560 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number, 561 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, 562 sc->sc_intr_buf, sc->sc_isize, 563 umct_intr, USBD_DEFAULT_INTERVAL); 564 if (err) { 565 DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n", 566 sc->sc_dev.dv_xname, sc->sc_intr_number)); 567 return (EIO); 568 } 569 } 570 571 return (0); 572 } 573 574 void 575 umct_close(void *addr, int portno) 576 { 577 struct umct_softc *sc = addr; 578 int err; 579 580 if (sc->sc_dying) 581 return; 582 583 DPRINTF(("umct_close: close\n")); 584 585 if (sc->sc_intr_pipe != NULL) { 586 err = usbd_abort_pipe(sc->sc_intr_pipe); 587 if (err) 588 printf("%s: abort interrupt pipe failed: %s\n", 589 sc->sc_dev.dv_xname, usbd_errstr(err)); 590 err = usbd_close_pipe(sc->sc_intr_pipe); 591 if (err) 592 printf("%s: close interrupt pipe failed: %s\n", 593 sc->sc_dev.dv_xname, usbd_errstr(err)); 594 free(sc->sc_intr_buf, M_USBDEV); 595 sc->sc_intr_pipe = NULL; 596 } 597 } 598 599 void 600 umct_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 601 { 602 struct umct_softc *sc = priv; 603 u_char *buf = sc->sc_intr_buf; 604 u_char mstatus; 605 606 if (sc->sc_dying) 607 return; 608 609 if (status != USBD_NORMAL_COMPLETION) { 610 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 611 return; 612 613 DPRINTF(("%s: abnormal status: %s\n", sc->sc_dev.dv_xname, 614 usbd_errstr(status))); 615 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe); 616 return; 617 } 618 619 DPRINTF(("%s: umct status = MSR:%02x, LSR:%02x\n", 620 sc->sc_dev.dv_xname, buf[0],buf[1])); 621 622 sc->sc_lsr = sc->sc_msr = 0; 623 mstatus = buf[0]; 624 if (ISSET(mstatus, MSR_DSR)) 625 sc->sc_msr |= UMSR_DSR; 626 if (ISSET(mstatus, MSR_DCD)) 627 sc->sc_msr |= UMSR_DCD; 628 ucom_status_change((struct ucom_softc *)sc->sc_subdev); 629 } 630 631 void 632 umct_get_status(void *addr, int portno, u_char *lsr, u_char *msr) 633 { 634 struct umct_softc *sc = addr; 635 636 DPRINTF(("umct_get_status:\n")); 637 638 if (lsr != NULL) 639 *lsr = sc->sc_lsr; 640 if (msr != NULL) 641 *msr = sc->sc_msr; 642 } 643