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