1 /* $NetBSD: umct.c,v 1.4 2001/12/03 01:47:12 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/cdefs.h> 46 __KERNEL_RCSID(0, "$NetBSD: umct.c,v 1.4 2001/12/03 01:47:12 augustss Exp $"); 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/kernel.h> 51 #include <sys/malloc.h> 52 #include <sys/ioctl.h> 53 #include <sys/conf.h> 54 #include <sys/tty.h> 55 #include <sys/file.h> 56 #include <sys/select.h> 57 #include <sys/proc.h> 58 #include <sys/vnode.h> 59 #include <sys/device.h> 60 #include <sys/poll.h> 61 62 #include <dev/usb/usb.h> 63 #include <dev/usb/usbcdc.h> 64 65 #include <dev/usb/usbdi.h> 66 #include <dev/usb/usbdi_util.h> 67 #include <dev/usb/usbdevs.h> 68 #include <dev/usb/usb_quirks.h> 69 70 #include <dev/usb/usbdevs.h> 71 #include <dev/usb/ucomvar.h> 72 73 #include <dev/usb/umct.h> 74 75 #ifdef UMCT_DEBUG 76 #define DPRINTFN(n, x) if (umctdebug > (n)) logprintf x 77 int umctdebug = 0; 78 #else 79 #define DPRINTFN(n, x) 80 #endif 81 #define DPRINTF(x) DPRINTFN(0, x) 82 83 #define UMCT_CONFIG_INDEX 0 84 #define UMCT_IFACE_INDEX 0 85 86 struct umct_softc { 87 USBBASEDEVICE sc_dev; /* base device */ 88 usbd_device_handle sc_udev; /* USB device */ 89 usbd_interface_handle sc_iface; /* interface */ 90 int sc_iface_number; /* interface number */ 91 92 int sc_intr_number; /* interrupt number */ 93 usbd_pipe_handle sc_intr_pipe; /* interrupt pipe */ 94 u_char *sc_intr_buf; /* interrupt buffer */ 95 int sc_isize; 96 97 usb_cdc_line_state_t sc_line_state; /* current line state */ 98 u_char sc_dtr; /* current DTR state */ 99 u_char sc_rts; /* current RTS state */ 100 u_char sc_break; /* set break */ 101 102 u_char sc_status; 103 104 device_ptr_t sc_subdev; /* ucom device */ 105 106 u_char sc_dying; /* disconnecting */ 107 108 u_char sc_lsr; /* Local status register */ 109 u_char sc_msr; /* umct status 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 *, void *); 121 Static void umct_set_lcr(struct umct_softc *, void *); 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); 183 USB_ATTACH_SETUP; 184 printf("%s: %s\n", devname, devinfo); 185 186 sc->sc_udev = dev; 187 188 DPRINTF(("\n\numct attach: sc=%p\n", sc)); 189 190 /* initialize endpoints */ 191 uca.bulkin = uca.bulkout = -1; 192 sc->sc_intr_number = -1; 193 sc->sc_intr_pipe = NULL; 194 195 /* Move the device into the configured state. */ 196 err = usbd_set_config_index(dev, UMCT_CONFIG_INDEX, 1); 197 if (err) { 198 printf("\n%s: failed to set configuration, err=%s\n", 199 devname, usbd_errstr(err)); 200 sc->sc_dying = 1; 201 USB_ATTACH_ERROR_RETURN; 202 } 203 204 /* get the config descriptor */ 205 cdesc = usbd_get_config_descriptor(sc->sc_udev); 206 207 if (cdesc == NULL) { 208 printf("%s: failed to get configuration descriptor\n", 209 USBDEVNAME(sc->sc_dev)); 210 sc->sc_dying = 1; 211 USB_ATTACH_ERROR_RETURN; 212 } 213 214 /* get the interface */ 215 err = usbd_device2interface_handle(dev, UMCT_IFACE_INDEX, 216 &sc->sc_iface); 217 if (err) { 218 printf("\n%s: failed to get interface, err=%s\n", 219 devname, usbd_errstr(err)); 220 sc->sc_dying = 1; 221 USB_ATTACH_ERROR_RETURN; 222 } 223 224 /* Find the bulk{in,out} and interrupt endpoints */ 225 226 id = usbd_get_interface_descriptor(sc->sc_iface); 227 sc->sc_iface_number = id->bInterfaceNumber; 228 found = 0; 229 230 for (i = 0; i < id->bNumEndpoints; i++) { 231 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 232 if (ed == NULL) { 233 printf("%s: no endpoint descriptor for %d\n", 234 USBDEVNAME(sc->sc_dev), i); 235 sc->sc_dying = 1; 236 USB_ATTACH_ERROR_RETURN; 237 } 238 239 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 240 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT && 241 found == 0) { 242 uca.bulkin = ed->bEndpointAddress; 243 found = 1; 244 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 245 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 246 uca.bulkout = ed->bEndpointAddress; 247 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 248 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 249 sc->sc_intr_number = ed->bEndpointAddress; 250 sc->sc_isize = UGETW(ed->wMaxPacketSize); 251 } 252 } 253 254 if (uca.bulkin == -1) { 255 printf("%s: Could not find data bulk in\n", 256 USBDEVNAME(sc->sc_dev)); 257 sc->sc_dying = 1; 258 USB_ATTACH_ERROR_RETURN; 259 } 260 261 if (uca.bulkout == -1) { 262 printf("%s: Could not find data bulk out\n", 263 USBDEVNAME(sc->sc_dev)); 264 sc->sc_dying = 1; 265 USB_ATTACH_ERROR_RETURN; 266 } 267 268 if (sc->sc_intr_number== -1) { 269 printf("%s: Could not find interrupt in\n", 270 USBDEVNAME(sc->sc_dev)); 271 sc->sc_dying = 1; 272 USB_ATTACH_ERROR_RETURN; 273 } 274 275 sc->sc_dtr = sc->sc_rts = 0; 276 uca.portno = UCOM_UNK_PORTNO; 277 /* bulkin, bulkout set above */ 278 uca.ibufsize = UMCTIBUFSIZE; 279 uca.obufsize = UMCTOBUFSIZE; 280 uca.ibufsizepad = UMCTIBUFSIZE; 281 uca.opkthdrlen = 0; 282 uca.device = dev; 283 uca.iface = sc->sc_iface; 284 uca.methods = &umct_methods; 285 uca.arg = sc; 286 uca.info = NULL; 287 288 umct_init(sc); 289 290 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 291 USBDEV(sc->sc_dev)); 292 293 DPRINTF(("umct: in=0x%x out=0x%x intr=0x%x\n", 294 uca.bulkin, uca.bulkout, sc->sc_intr_number )); 295 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch); 296 297 USB_ATTACH_SUCCESS_RETURN; 298 } 299 300 USB_DETACH(umct) 301 { 302 USB_DETACH_START(umct, sc); 303 int rv = 0; 304 305 DPRINTF(("umct_detach: sc=%p flags=%d\n", sc, flags)); 306 307 if (sc->sc_intr_pipe != NULL) { 308 usbd_abort_pipe(sc->sc_intr_pipe); 309 usbd_close_pipe(sc->sc_intr_pipe); 310 free(sc->sc_intr_buf, M_USBDEV); 311 sc->sc_intr_pipe = NULL; 312 } 313 314 sc->sc_dying = 1; 315 if (sc->sc_subdev != NULL) { 316 rv = config_detach(sc->sc_subdev, flags); 317 sc->sc_subdev = NULL; 318 } 319 320 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 321 USBDEV(sc->sc_dev)); 322 323 return (rv); 324 } 325 326 int 327 umct_activate(device_ptr_t self, enum devact act) 328 { 329 struct umct_softc *sc = (struct umct_softc *)self; 330 int rv = 0; 331 332 switch (act) { 333 case DVACT_ACTIVATE: 334 return (EOPNOTSUPP); 335 break; 336 337 case DVACT_DEACTIVATE: 338 if (sc->sc_subdev != NULL) 339 rv = config_deactivate(sc->sc_subdev); 340 sc->sc_dying = 1; 341 break; 342 } 343 return (rv); 344 } 345 346 void 347 umct_set_line_state(struct umct_softc *sc) 348 { 349 usb_device_request_t req; 350 int ls = MCR_NONE; 351 352 ls = (sc->sc_dtr ? MCR_DTR : 0) | 353 (sc->sc_rts ? MCR_RTS : 0); 354 355 DPRINTF(("umct_set_line_state: DTR=%d,RTS=%d,ls=%02x\n", 356 sc->sc_dtr, sc->sc_rts, ls)); 357 358 req.bmRequestType = UMCT_SET_REQUEST; 359 req.bRequest = REQ_SET_MCR; 360 USETW(req.wValue, 0); 361 USETW(req.wIndex, sc->sc_iface_number); 362 USETW(req.wLength, LENGTH_SET_MCR); 363 364 (void)usbd_do_request(sc->sc_udev, &req, &ls); 365 } 366 367 void 368 umct_set(void *addr, int portno, int reg, int onoff) 369 { 370 struct umct_softc *sc = addr; 371 372 switch (reg) { 373 case UCOM_SET_DTR: 374 umct_dtr(sc, onoff); 375 break; 376 case UCOM_SET_RTS: 377 umct_rts(sc, onoff); 378 break; 379 case UCOM_SET_BREAK: 380 umct_break(sc, onoff); 381 break; 382 default: 383 break; 384 } 385 } 386 387 void 388 umct_dtr(struct umct_softc *sc, int onoff) 389 { 390 391 DPRINTF(("umct_dtr: onoff=%d\n", onoff)); 392 393 if (sc->sc_dtr == onoff) 394 return; 395 sc->sc_dtr = onoff; 396 397 umct_set_line_state(sc); 398 } 399 400 void 401 umct_rts(struct umct_softc *sc, int onoff) 402 { 403 DPRINTF(("umct_rts: onoff=%d\n", onoff)); 404 405 if (sc->sc_rts == onoff) 406 return; 407 sc->sc_rts = onoff; 408 409 umct_set_line_state(sc); 410 } 411 412 void 413 umct_break(struct umct_softc *sc, int onoff) 414 { 415 u_char data; 416 417 DPRINTF(("umct_break: onoff=%d\n", onoff)); 418 419 data = onoff ? LCR_SET_BREAK : 0; 420 421 umct_set_lcr(sc, &data); 422 } 423 424 void 425 umct_set_lcr(struct umct_softc *sc, void *data) 426 { 427 usb_device_request_t req; 428 429 req.bmRequestType = UMCT_SET_REQUEST; 430 req.bRequest = REQ_SET_LCR; 431 USETW(req.wValue, 0); 432 USETW(req.wIndex, sc->sc_iface_number); 433 USETW(req.wLength, LENGTH_SET_LCR); 434 435 (void)usbd_do_request(sc->sc_udev, &req, data); 436 } 437 438 void 439 umct_set_baudrate(struct umct_softc *sc, void *rate) 440 { 441 usb_device_request_t req; 442 443 req.bmRequestType = UMCT_SET_REQUEST; 444 req.bRequest = REQ_SET_BAUD_RATE; 445 USETW(req.wValue, 0); 446 USETW(req.wIndex, sc->sc_iface_number); 447 USETW(req.wLength, LENGTH_BAUD_RATE); 448 449 (void)usbd_do_request(sc->sc_udev, &req, rate); 450 } 451 452 void 453 umct_init(struct umct_softc *sc) 454 { 455 int brate, data; 456 457 brate = UMCT_BAUD_RATE(9600); 458 data |= LCR_DATA_BITS_8 | LCR_PARITY_NONE | 459 LCR_STOP_BITS_1; 460 461 umct_set_baudrate(sc, &brate); 462 umct_set_lcr(sc, &data); 463 } 464 465 int 466 umct_param(void *addr, int portno, struct termios *t) 467 { 468 struct umct_softc *sc = addr; 469 int divisor = 0; 470 u_char data = NULL; 471 472 DPRINTF(("umct_param: sc=%p\n", sc)); 473 474 divisor = UMCT_BAUD_RATE(t->c_ospeed); 475 DPRINTF(("umct_param: BAUDRATE=%d\n", t->c_ospeed)); 476 477 if (ISSET(t->c_cflag, CSTOPB)) 478 data |= LCR_STOP_BITS_2; 479 else 480 data |= LCR_STOP_BITS_1; 481 if (ISSET(t->c_cflag, PARENB)) { 482 if (ISSET(t->c_cflag, PARODD)) 483 data |= LCR_PARITY_ODD; 484 else 485 data |= LCR_PARITY_EVEN; 486 } else 487 data |= LCR_PARITY_NONE; 488 switch (ISSET(t->c_cflag, CSIZE)) { 489 case CS5: 490 data |= LCR_DATA_BITS_5; 491 break; 492 case CS6: 493 data |= LCR_DATA_BITS_6; 494 break; 495 case CS7: 496 data |= LCR_DATA_BITS_7; 497 break; 498 case CS8: 499 data |= LCR_DATA_BITS_8; 500 break; 501 } 502 503 umct_set_baudrate(sc, &divisor); 504 505 umct_set_lcr(sc, &data); 506 507 return (0); 508 } 509 510 int 511 umct_open(void *addr, int portno) 512 { 513 struct umct_softc *sc = addr; 514 int err, lcr_data; 515 516 if (sc->sc_dying) 517 return (EIO); 518 519 DPRINTF(("umct_open: sc=%p\n", sc)); 520 521 /* initialize LCR */ 522 lcr_data |= LCR_DATA_BITS_8 | LCR_PARITY_NONE | 523 LCR_STOP_BITS_1; 524 umct_set_lcr(sc, &lcr_data); 525 526 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) { 527 sc->sc_status = 0; /* clear status bit */ 528 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK); 529 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number, 530 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, 531 sc->sc_intr_buf, sc->sc_isize, 532 umct_intr, USBD_DEFAULT_INTERVAL); 533 if (err) { 534 DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n", 535 USBDEVNAME(sc->sc_dev), sc->sc_intr_number)); 536 return (EIO); 537 } 538 } 539 540 return (0); 541 } 542 543 void 544 umct_close(void *addr, int portno) 545 { 546 struct umct_softc *sc = addr; 547 int err; 548 549 if (sc->sc_dying) 550 return; 551 552 DPRINTF(("umct_close: close\n")); 553 554 if (sc->sc_intr_pipe != NULL) { 555 err = usbd_abort_pipe(sc->sc_intr_pipe); 556 if (err) 557 printf("%s: abort interrupt pipe failed: %s\n", 558 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 559 err = usbd_close_pipe(sc->sc_intr_pipe); 560 if (err) 561 printf("%s: close interrupt pipe failed: %s\n", 562 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 563 free(sc->sc_intr_buf, M_USBDEV); 564 sc->sc_intr_pipe = NULL; 565 } 566 } 567 568 void 569 umct_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 570 { 571 struct umct_softc *sc = priv; 572 u_char *buf = sc->sc_intr_buf; 573 u_char mstatus, lstatus; 574 575 if (sc->sc_dying) 576 return; 577 578 if (status != USBD_NORMAL_COMPLETION) { 579 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 580 return; 581 582 DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev), 583 usbd_errstr(status))); 584 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe); 585 return; 586 } 587 588 DPRINTF(("%s: umct status = MSR:%02x, LSR:%02x\n", 589 USBDEVNAME(sc->sc_dev), buf[0],buf[1])); 590 591 sc->sc_lsr = sc->sc_msr = 0; 592 mstatus = buf[0]; 593 lstatus = buf[1]; 594 if (ISSET(mstatus, MSR_DSR)) 595 sc->sc_msr |= UMSR_DSR; 596 if (ISSET(mstatus, MSR_DCD)) 597 sc->sc_msr |= UMSR_DCD; 598 ucom_status_change((struct ucom_softc *)sc->sc_subdev); 599 } 600 601 void 602 umct_get_status(void *addr, int portno, u_char *lsr, u_char *msr) 603 { 604 struct umct_softc *sc = addr; 605 606 DPRINTF(("umct_get_status:\n")); 607 608 if (lsr != NULL) 609 *lsr = sc->sc_lsr; 610 if (msr != NULL) 611 *msr = sc->sc_msr; 612 } 613