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