1 /* $OpenBSD: umct.c,v 1.33 2011/07/03 15:47:17 matthew 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 151 int umct_match(struct device *, void *, void *); 152 void umct_attach(struct device *, struct device *, void *); 153 int umct_detach(struct device *, int); 154 int umct_activate(struct device *, int); 155 156 struct cfdriver umct_cd = { 157 NULL, "umct", DV_DULL 158 }; 159 160 const struct cfattach umct_ca = { 161 sizeof(struct umct_softc), 162 umct_match, 163 umct_attach, 164 umct_detach, 165 umct_activate, 166 }; 167 168 int 169 umct_match(struct device *parent, void *match, void *aux) 170 { 171 struct usb_attach_arg *uaa = aux; 172 173 if (uaa->iface != NULL) 174 return (UMATCH_NONE); 175 176 return (usb_lookup(umct_devs, uaa->vendor, uaa->product) != NULL ? 177 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 178 } 179 180 void 181 umct_attach(struct device *parent, struct device *self, void *aux) 182 { 183 struct umct_softc *sc = (struct umct_softc *)self; 184 struct usb_attach_arg *uaa = aux; 185 usbd_device_handle dev = uaa->device; 186 usb_config_descriptor_t *cdesc; 187 usb_interface_descriptor_t *id; 188 usb_endpoint_descriptor_t *ed; 189 190 char *devname = sc->sc_dev.dv_xname; 191 usbd_status err; 192 int i; 193 struct ucom_attach_args uca; 194 195 sc->sc_udev = dev; 196 sc->sc_product = uaa->product; 197 198 DPRINTF(("\n\numct attach: sc=%p\n", sc)); 199 200 /* initialize endpoints */ 201 uca.bulkin = uca.bulkout = -1; 202 sc->sc_intr_number = -1; 203 sc->sc_intr_pipe = NULL; 204 205 /* Move the device into the configured state. */ 206 err = usbd_set_config_index(dev, UMCT_CONFIG_INDEX, 1); 207 if (err) { 208 printf("\n%s: failed to set configuration, err=%s\n", 209 devname, usbd_errstr(err)); 210 sc->sc_dying = 1; 211 return; 212 } 213 214 /* get the config descriptor */ 215 cdesc = usbd_get_config_descriptor(sc->sc_udev); 216 217 if (cdesc == NULL) { 218 printf("%s: failed to get configuration descriptor\n", 219 sc->sc_dev.dv_xname); 220 sc->sc_dying = 1; 221 return; 222 } 223 224 /* get the interface */ 225 err = usbd_device2interface_handle(dev, UMCT_IFACE_INDEX, 226 &sc->sc_iface); 227 if (err) { 228 printf("\n%s: failed to get interface, err=%s\n", 229 devname, usbd_errstr(err)); 230 sc->sc_dying = 1; 231 return; 232 } 233 234 /* Find the bulk{in,out} and interrupt endpoints */ 235 236 id = usbd_get_interface_descriptor(sc->sc_iface); 237 sc->sc_iface_number = id->bInterfaceNumber; 238 239 for (i = 0; i < id->bNumEndpoints; i++) { 240 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 241 if (ed == NULL) { 242 printf("%s: no endpoint descriptor for %d\n", 243 sc->sc_dev.dv_xname, i); 244 sc->sc_dying = 1; 245 return; 246 } 247 248 /* 249 * The Bulkin endpoint is marked as an interrupt. Since 250 * we can't rely on the endpoint descriptor order, we'll 251 * check the wMaxPacketSize field to differentiate. 252 */ 253 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 254 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT && 255 UGETW(ed->wMaxPacketSize) != 0x2) { 256 uca.bulkin = ed->bEndpointAddress; 257 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 258 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 259 uca.bulkout = ed->bEndpointAddress; 260 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 261 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 262 sc->sc_intr_number = ed->bEndpointAddress; 263 sc->sc_isize = UGETW(ed->wMaxPacketSize); 264 } 265 } 266 267 if (uca.bulkin == -1) { 268 printf("%s: Could not find data bulk in\n", 269 sc->sc_dev.dv_xname); 270 sc->sc_dying = 1; 271 return; 272 } 273 274 if (uca.bulkout == -1) { 275 printf("%s: Could not find data bulk out\n", 276 sc->sc_dev.dv_xname); 277 sc->sc_dying = 1; 278 return; 279 } 280 281 if (sc->sc_intr_number== -1) { 282 printf("%s: Could not find interrupt in\n", 283 sc->sc_dev.dv_xname); 284 sc->sc_dying = 1; 285 return; 286 } 287 288 sc->sc_dtr = sc->sc_rts = 0; 289 uca.portno = UCOM_UNK_PORTNO; 290 /* bulkin, bulkout set above */ 291 uca.ibufsize = UMCTIBUFSIZE; 292 if (sc->sc_product == USB_PRODUCT_MCT_SITECOM_USB232) 293 uca.obufsize = 16; /* device is broken */ 294 else 295 uca.obufsize = UMCTOBUFSIZE; 296 uca.ibufsizepad = UMCTIBUFSIZE; 297 uca.opkthdrlen = 0; 298 uca.device = dev; 299 uca.iface = sc->sc_iface; 300 uca.methods = &umct_methods; 301 uca.arg = sc; 302 uca.info = NULL; 303 304 umct_init(sc); 305 306 DPRINTF(("umct: in=0x%x out=0x%x intr=0x%x\n", 307 uca.bulkin, uca.bulkout, sc->sc_intr_number )); 308 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch); 309 } 310 311 int 312 umct_detach(struct device *self, int flags) 313 { 314 struct umct_softc *sc = (struct umct_softc *)self; 315 int rv = 0; 316 317 DPRINTF(("umct_detach: sc=%p flags=%d\n", sc, flags)); 318 319 if (sc->sc_intr_pipe != NULL) { 320 usbd_abort_pipe(sc->sc_intr_pipe); 321 usbd_close_pipe(sc->sc_intr_pipe); 322 free(sc->sc_intr_buf, M_USBDEV); 323 sc->sc_intr_pipe = NULL; 324 } 325 326 if (sc->sc_subdev != NULL) { 327 rv = config_detach(sc->sc_subdev, flags); 328 sc->sc_subdev = NULL; 329 } 330 331 return (rv); 332 } 333 334 int 335 umct_activate(struct device *self, int act) 336 { 337 struct umct_softc *sc = (struct umct_softc *)self; 338 int rv = 0; 339 340 switch (act) { 341 case DVACT_DEACTIVATE: 342 if (sc->sc_subdev != NULL) 343 rv = config_deactivate(sc->sc_subdev); 344 sc->sc_dying = 1; 345 break; 346 } 347 return (rv); 348 } 349 350 void 351 umct_set_line_state(struct umct_softc *sc) 352 { 353 usb_device_request_t req; 354 uByte ls; 355 356 ls = (sc->sc_dtr ? MCR_DTR : 0) | 357 (sc->sc_rts ? MCR_RTS : 0); 358 359 DPRINTF(("umct_set_line_state: DTR=%d,RTS=%d,ls=%02x\n", 360 sc->sc_dtr, sc->sc_rts, ls)); 361 362 req.bmRequestType = UMCT_SET_REQUEST; 363 req.bRequest = REQ_SET_MCR; 364 USETW(req.wValue, 0); 365 USETW(req.wIndex, sc->sc_iface_number); 366 USETW(req.wLength, LENGTH_SET_MCR); 367 368 (void)usbd_do_request(sc->sc_udev, &req, &ls); 369 } 370 371 void 372 umct_set(void *addr, int portno, int reg, int onoff) 373 { 374 struct umct_softc *sc = addr; 375 376 switch (reg) { 377 case UCOM_SET_DTR: 378 umct_dtr(sc, onoff); 379 break; 380 case UCOM_SET_RTS: 381 umct_rts(sc, onoff); 382 break; 383 case UCOM_SET_BREAK: 384 umct_break(sc, onoff); 385 break; 386 default: 387 break; 388 } 389 } 390 391 void 392 umct_dtr(struct umct_softc *sc, int onoff) 393 { 394 395 DPRINTF(("umct_dtr: onoff=%d\n", onoff)); 396 397 if (sc->sc_dtr == onoff) 398 return; 399 sc->sc_dtr = onoff; 400 401 umct_set_line_state(sc); 402 } 403 404 void 405 umct_rts(struct umct_softc *sc, int onoff) 406 { 407 DPRINTF(("umct_rts: onoff=%d\n", onoff)); 408 409 if (sc->sc_rts == onoff) 410 return; 411 sc->sc_rts = onoff; 412 413 umct_set_line_state(sc); 414 } 415 416 void 417 umct_break(struct umct_softc *sc, int onoff) 418 { 419 DPRINTF(("umct_break: onoff=%d\n", onoff)); 420 421 umct_set_lcr(sc, onoff ? sc->last_lcr | LCR_SET_BREAK : 422 sc->last_lcr); 423 } 424 425 void 426 umct_set_lcr(struct umct_softc *sc, u_int data) 427 { 428 usb_device_request_t req; 429 uByte adata; 430 431 adata = data; 432 req.bmRequestType = UMCT_SET_REQUEST; 433 req.bRequest = REQ_SET_LCR; 434 USETW(req.wValue, 0); 435 USETW(req.wIndex, sc->sc_iface_number); 436 USETW(req.wLength, LENGTH_SET_LCR); 437 438 /* XXX should check */ 439 (void)usbd_do_request(sc->sc_udev, &req, &adata); 440 } 441 442 void 443 umct_set_baudrate(struct umct_softc *sc, u_int rate) 444 { 445 usb_device_request_t req; 446 uDWord arate; 447 u_int val; 448 449 if (sc->sc_product == USB_PRODUCT_MCT_SITECOM_USB232 || 450 sc->sc_product == USB_PRODUCT_BELKIN_F5U109) { 451 switch (rate) { 452 case 300: val = 0x01; break; 453 case 600: val = 0x02; break; 454 case 1200: val = 0x03; break; 455 case 2400: val = 0x04; break; 456 case 4800: val = 0x06; break; 457 case 9600: val = 0x08; break; 458 case 19200: val = 0x09; break; 459 case 38400: val = 0x0a; break; 460 case 57600: val = 0x0b; break; 461 case 115200: val = 0x0c; break; 462 default: val = -1; break; 463 } 464 } else { 465 val = UMCT_BAUD_RATE(rate); 466 } 467 USETDW(arate, val); 468 469 req.bmRequestType = UMCT_SET_REQUEST; 470 req.bRequest = REQ_SET_BAUD_RATE; 471 USETW(req.wValue, 0); 472 USETW(req.wIndex, sc->sc_iface_number); 473 USETW(req.wLength, LENGTH_BAUD_RATE); 474 475 /* XXX should check */ 476 (void)usbd_do_request(sc->sc_udev, &req, arate); 477 } 478 479 void 480 umct_init(struct umct_softc *sc) 481 { 482 umct_set_baudrate(sc, 9600); 483 umct_set_lcr(sc, LCR_DATA_BITS_8 | LCR_PARITY_NONE | LCR_STOP_BITS_1); 484 } 485 486 int 487 umct_param(void *addr, int portno, struct termios *t) 488 { 489 struct umct_softc *sc = addr; 490 u_int data = 0; 491 492 DPRINTF(("umct_param: sc=%p\n", sc)); 493 494 DPRINTF(("umct_param: BAUDRATE=%d\n", t->c_ospeed)); 495 496 if (ISSET(t->c_cflag, CSTOPB)) 497 data |= LCR_STOP_BITS_2; 498 else 499 data |= LCR_STOP_BITS_1; 500 if (ISSET(t->c_cflag, PARENB)) { 501 if (ISSET(t->c_cflag, PARODD)) 502 data |= LCR_PARITY_ODD; 503 else 504 data |= LCR_PARITY_EVEN; 505 } else 506 data |= LCR_PARITY_NONE; 507 switch (ISSET(t->c_cflag, CSIZE)) { 508 case CS5: 509 data |= LCR_DATA_BITS_5; 510 break; 511 case CS6: 512 data |= LCR_DATA_BITS_6; 513 break; 514 case CS7: 515 data |= LCR_DATA_BITS_7; 516 break; 517 case CS8: 518 data |= LCR_DATA_BITS_8; 519 break; 520 } 521 522 umct_set_baudrate(sc, t->c_ospeed); 523 524 sc->last_lcr = data; 525 umct_set_lcr(sc, data); 526 527 return (0); 528 } 529 530 int 531 umct_open(void *addr, int portno) 532 { 533 struct umct_softc *sc = addr; 534 int err, lcr_data; 535 536 if (sc->sc_dying) 537 return (EIO); 538 539 DPRINTF(("umct_open: sc=%p\n", sc)); 540 541 /* initialize LCR */ 542 lcr_data = LCR_DATA_BITS_8 | LCR_PARITY_NONE | 543 LCR_STOP_BITS_1; 544 umct_set_lcr(sc, lcr_data); 545 546 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) { 547 sc->sc_status = 0; /* clear status bit */ 548 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK); 549 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number, 550 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, 551 sc->sc_intr_buf, sc->sc_isize, 552 umct_intr, USBD_DEFAULT_INTERVAL); 553 if (err) { 554 DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n", 555 sc->sc_dev.dv_xname, sc->sc_intr_number)); 556 return (EIO); 557 } 558 } 559 560 return (0); 561 } 562 563 void 564 umct_close(void *addr, int portno) 565 { 566 struct umct_softc *sc = addr; 567 int err; 568 569 if (sc->sc_dying) 570 return; 571 572 DPRINTF(("umct_close: close\n")); 573 574 if (sc->sc_intr_pipe != NULL) { 575 err = usbd_abort_pipe(sc->sc_intr_pipe); 576 if (err) 577 printf("%s: abort interrupt pipe failed: %s\n", 578 sc->sc_dev.dv_xname, usbd_errstr(err)); 579 err = usbd_close_pipe(sc->sc_intr_pipe); 580 if (err) 581 printf("%s: close interrupt pipe failed: %s\n", 582 sc->sc_dev.dv_xname, usbd_errstr(err)); 583 free(sc->sc_intr_buf, M_USBDEV); 584 sc->sc_intr_pipe = NULL; 585 } 586 } 587 588 void 589 umct_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 590 { 591 struct umct_softc *sc = priv; 592 u_char *buf = sc->sc_intr_buf; 593 u_char mstatus; 594 595 if (sc->sc_dying) 596 return; 597 598 if (status != USBD_NORMAL_COMPLETION) { 599 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 600 return; 601 602 DPRINTF(("%s: abnormal status: %s\n", sc->sc_dev.dv_xname, 603 usbd_errstr(status))); 604 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe); 605 return; 606 } 607 608 DPRINTF(("%s: umct status = MSR:%02x, LSR:%02x\n", 609 sc->sc_dev.dv_xname, buf[0],buf[1])); 610 611 sc->sc_lsr = sc->sc_msr = 0; 612 mstatus = buf[0]; 613 if (ISSET(mstatus, MSR_DSR)) 614 sc->sc_msr |= UMSR_DSR; 615 if (ISSET(mstatus, MSR_DCD)) 616 sc->sc_msr |= UMSR_DCD; 617 ucom_status_change((struct ucom_softc *)sc->sc_subdev); 618 } 619 620 void 621 umct_get_status(void *addr, int portno, u_char *lsr, u_char *msr) 622 { 623 struct umct_softc *sc = addr; 624 625 DPRINTF(("umct_get_status:\n")); 626 627 if (lsr != NULL) 628 *lsr = sc->sc_lsr; 629 if (msr != NULL) 630 *msr = sc->sc_msr; 631 } 632