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