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