1 /* $NetBSD: umct.c,v 1.27 2009/05/12 13:22:10 cegger 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.27 2009/05/12 13:22:10 cegger Exp $"); 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/malloc.h> 44 #include <sys/ioctl.h> 45 #include <sys/conf.h> 46 #include <sys/tty.h> 47 #include <sys/file.h> 48 #include <sys/select.h> 49 #include <sys/proc.h> 50 #include <sys/vnode.h> 51 #include <sys/device.h> 52 #include <sys/poll.h> 53 54 #include <dev/usb/usb.h> 55 #include <dev/usb/usbcdc.h> 56 57 #include <dev/usb/usbdi.h> 58 #include <dev/usb/usbdi_util.h> 59 #include <dev/usb/usbdevs.h> 60 #include <dev/usb/usb_quirks.h> 61 62 #include <dev/usb/ucomvar.h> 63 #include <dev/usb/umct.h> 64 65 #ifdef UMCT_DEBUG 66 #define DPRINTFN(n, x) if (umctdebug > (n)) logprintf x 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 USBBASEDEVICE 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 device_t 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 Static void umct_init(struct umct_softc *); 113 Static void umct_set_baudrate(struct umct_softc *, u_int); 114 Static void umct_set_lcr(struct umct_softc *, u_int); 115 Static void umct_intr(usbd_xfer_handle, usbd_private_handle, usbd_status); 116 117 Static void umct_set(void *, int, int, int); 118 Static void umct_dtr(struct umct_softc *, int); 119 Static void umct_rts(struct umct_softc *, int); 120 Static void umct_break(struct umct_softc *, int); 121 Static void umct_set_line_state(struct umct_softc *); 122 Static void umct_get_status(void *, int portno, u_char *lsr, u_char *msr); 123 Static int umct_param(void *, int, struct termios *); 124 Static int umct_open(void *, int); 125 Static 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 }; 148 #define umct_lookup(v, p) usb_lookup(umct_devs, v, p) 149 150 int umct_match(device_t, cfdata_t, void *); 151 void umct_attach(device_t, device_t, void *); 152 void umct_childdet(device_t, device_t); 153 int umct_detach(device_t, int); 154 int umct_activate(device_t, enum devact); 155 extern struct cfdriver umct_cd; 156 CFATTACH_DECL2_NEW(umct, sizeof(struct umct_softc), umct_match, 157 umct_attach, umct_detach, umct_activate, NULL, umct_childdet); 158 159 USB_MATCH(umct) 160 { 161 USB_MATCH_START(umct, uaa); 162 163 return (umct_lookup(uaa->vendor, uaa->product) != NULL ? 164 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 165 } 166 167 USB_ATTACH(umct) 168 { 169 USB_ATTACH_START(umct, sc, uaa); 170 usbd_device_handle dev = uaa->device; 171 usb_config_descriptor_t *cdesc; 172 usb_interface_descriptor_t *id; 173 usb_endpoint_descriptor_t *ed; 174 175 char *devinfop; 176 usbd_status err; 177 int i; 178 struct ucom_attach_args uca; 179 180 sc->sc_dev = self; 181 182 devinfop = usbd_devinfo_alloc(dev, 0); 183 USB_ATTACH_SETUP; 184 aprint_normal_dev(self, "%s\n", devinfop); 185 usbd_devinfo_free(devinfop); 186 187 sc->sc_udev = dev; 188 sc->sc_product = uaa->product; 189 190 DPRINTF(("\n\numct attach: sc=%p\n", sc)); 191 192 /* initialize endpoints */ 193 uca.bulkin = uca.bulkout = -1; 194 sc->sc_intr_number = -1; 195 sc->sc_intr_pipe = NULL; 196 197 /* Move the device into the configured state. */ 198 err = usbd_set_config_index(dev, UMCT_CONFIG_INDEX, 1); 199 if (err) { 200 aprint_error_dev(self, "failed to set configuration, err=%s\n", 201 usbd_errstr(err)); 202 sc->sc_dying = 1; 203 USB_ATTACH_ERROR_RETURN; 204 } 205 206 /* get the config descriptor */ 207 cdesc = usbd_get_config_descriptor(sc->sc_udev); 208 209 if (cdesc == NULL) { 210 aprint_error_dev(self, 211 "failed to get configuration descriptor\n"); 212 sc->sc_dying = 1; 213 USB_ATTACH_ERROR_RETURN; 214 } 215 216 /* get the interface */ 217 err = usbd_device2interface_handle(dev, UMCT_IFACE_INDEX, 218 &sc->sc_iface); 219 if (err) { 220 aprint_error_dev(self, "failed to get interface, err=%s\n", 221 usbd_errstr(err)); 222 sc->sc_dying = 1; 223 USB_ATTACH_ERROR_RETURN; 224 } 225 226 /* Find the bulk{in,out} and interrupt endpoints */ 227 228 id = usbd_get_interface_descriptor(sc->sc_iface); 229 sc->sc_iface_number = id->bInterfaceNumber; 230 231 for (i = 0; i < id->bNumEndpoints; i++) { 232 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 233 if (ed == NULL) { 234 aprint_error_dev(self, 235 "no endpoint descriptor for %d\n", i); 236 sc->sc_dying = 1; 237 USB_ATTACH_ERROR_RETURN; 238 } 239 240 /* 241 * The Bulkin endpoint is marked as an interrupt. Since 242 * we can't rely on the endpoint descriptor order, we'll 243 * check the wMaxPacketSize field to differentiate. 244 */ 245 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 246 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT && 247 UGETW(ed->wMaxPacketSize) != 0x2) { 248 uca.bulkin = ed->bEndpointAddress; 249 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 250 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 251 uca.bulkout = ed->bEndpointAddress; 252 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 253 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 254 sc->sc_intr_number = ed->bEndpointAddress; 255 sc->sc_isize = UGETW(ed->wMaxPacketSize); 256 } 257 } 258 259 if (uca.bulkin == -1) { 260 aprint_error_dev(self, "Could not find data bulk in\n"); 261 sc->sc_dying = 1; 262 USB_ATTACH_ERROR_RETURN; 263 } 264 265 if (uca.bulkout == -1) { 266 aprint_error_dev(self, "Could not find data bulk out\n"); 267 sc->sc_dying = 1; 268 USB_ATTACH_ERROR_RETURN; 269 } 270 271 if (sc->sc_intr_number== -1) { 272 aprint_error_dev(self, "Could not find interrupt in\n"); 273 sc->sc_dying = 1; 274 USB_ATTACH_ERROR_RETURN; 275 } 276 277 sc->sc_dtr = sc->sc_rts = 0; 278 uca.portno = UCOM_UNK_PORTNO; 279 /* bulkin, bulkout set above */ 280 uca.ibufsize = UMCTIBUFSIZE; 281 if (sc->sc_product == USB_PRODUCT_MCT_SITECOM_USB232) 282 uca.obufsize = 16; /* device is broken */ 283 else 284 uca.obufsize = UMCTOBUFSIZE; 285 uca.ibufsizepad = UMCTIBUFSIZE; 286 uca.opkthdrlen = 0; 287 uca.device = dev; 288 uca.iface = sc->sc_iface; 289 uca.methods = &umct_methods; 290 uca.arg = sc; 291 uca.info = NULL; 292 293 umct_init(sc); 294 295 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 296 USBDEV(sc->sc_dev)); 297 298 DPRINTF(("umct: in=0x%x out=0x%x intr=0x%x\n", 299 uca.bulkin, uca.bulkout, sc->sc_intr_number )); 300 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca, 301 ucomprint, ucomsubmatch); 302 303 USB_ATTACH_SUCCESS_RETURN; 304 } 305 306 void 307 umct_childdet(device_t self, device_t child) 308 { 309 struct umct_softc *sc = device_private(self); 310 311 KASSERT(sc->sc_subdev == child); 312 sc->sc_subdev = NULL; 313 } 314 315 USB_DETACH(umct) 316 { 317 USB_DETACH_START(umct, sc); 318 int rv = 0; 319 320 DPRINTF(("umct_detach: sc=%p flags=%d\n", sc, flags)); 321 322 if (sc->sc_intr_pipe != NULL) { 323 usbd_abort_pipe(sc->sc_intr_pipe); 324 usbd_close_pipe(sc->sc_intr_pipe); 325 free(sc->sc_intr_buf, M_USBDEV); 326 sc->sc_intr_pipe = NULL; 327 } 328 329 sc->sc_dying = 1; 330 if (sc->sc_subdev != NULL) 331 rv = config_detach(sc->sc_subdev, flags); 332 333 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 334 USBDEV(sc->sc_dev)); 335 336 return (rv); 337 } 338 339 int 340 umct_activate(device_t self, enum devact act) 341 { 342 struct umct_softc *sc = device_private(self); 343 int rv = 0; 344 345 switch (act) { 346 case DVACT_ACTIVATE: 347 return (EOPNOTSUPP); 348 349 case DVACT_DEACTIVATE: 350 if (sc->sc_subdev != NULL) 351 rv = config_deactivate(sc->sc_subdev); 352 sc->sc_dying = 1; 353 break; 354 } 355 return (rv); 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 = malloc(sc->sc_isize, M_USBDEV, M_WAITOK); 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 USBDEVNAME(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 USBDEVNAME(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 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 589 free(sc->sc_intr_buf, M_USBDEV); 590 sc->sc_intr_pipe = NULL; 591 } 592 } 593 594 void 595 umct_intr(usbd_xfer_handle xfer, usbd_private_handle 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", USBDEVNAME(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 USBDEVNAME(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