1 /* $NetBSD: uplcom.c,v 1.76 2015/11/01 21:31:40 skrll 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 * General information: http://www.prolific.com.tw/fr_pl2303.htm 33 * http://www.hitachi-hitec.com/jyouhou/prolific/2303.pdf 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: uplcom.c,v 1.76 2015/11/01 21:31:40 skrll Exp $"); 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/malloc.h> 43 #include <sys/ioctl.h> 44 #include <sys/conf.h> 45 #include <sys/tty.h> 46 #include <sys/file.h> 47 #include <sys/select.h> 48 #include <sys/proc.h> 49 #include <sys/device.h> 50 #include <sys/poll.h> 51 52 #include <dev/usb/usb.h> 53 #include <dev/usb/usbcdc.h> 54 55 #include <dev/usb/usbdi.h> 56 #include <dev/usb/usbdi_util.h> 57 #include <dev/usb/usbdevs.h> 58 #include <dev/usb/usb_quirks.h> 59 60 #include <dev/usb/ucomvar.h> 61 62 #ifdef UPLCOM_DEBUG 63 #define DPRINTFN(n, x) if (uplcomdebug > (n)) printf x 64 int uplcomdebug = 0; 65 #else 66 #define DPRINTFN(n, x) 67 #endif 68 #define DPRINTF(x) DPRINTFN(0, x) 69 70 #define UPLCOM_CONFIG_INDEX 0 71 #define UPLCOM_IFACE_INDEX 0 72 #define UPLCOM_SECOND_IFACE_INDEX 1 73 74 #define UPLCOM_SET_REQUEST 0x01 75 #define UPLCOM_SET_CRTSCTS_0 0x41 76 #define UPLCOM_SET_CRTSCTS_HX 0x61 77 78 #define UPLCOM_N_SERIAL_CTS 0x80 79 80 enum pl2303_type { 81 UPLCOM_TYPE_0, /* we use this for all non-HX variants */ 82 UPLCOM_TYPE_HX, 83 }; 84 85 struct uplcom_softc { 86 device_t sc_dev; /* base device */ 87 usbd_device_handle sc_udev; /* USB device */ 88 usbd_interface_handle sc_iface; /* interface */ 89 int sc_iface_number; /* interface number */ 90 91 usbd_interface_handle sc_intr_iface; /* interrupt interface */ 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 int sc_dtr; /* current DTR state */ 99 int sc_rts; /* current RTS state */ 100 101 device_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; /* uplcom status register */ 107 108 enum pl2303_type sc_type; /* PL2303 chip type */ 109 }; 110 111 /* 112 * These are the maximum number of bytes transferred per frame. 113 * The output buffer size cannot be increased due to the size encoding. 114 */ 115 #define UPLCOMIBUFSIZE 256 116 #define UPLCOMOBUFSIZE 256 117 118 Static usbd_status uplcom_reset(struct uplcom_softc *); 119 Static usbd_status uplcom_set_line_coding(struct uplcom_softc *sc, 120 usb_cdc_line_state_t *state); 121 Static usbd_status uplcom_set_crtscts(struct uplcom_softc *); 122 Static void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status); 123 124 Static void uplcom_set(void *, int, int, int); 125 Static void uplcom_dtr(struct uplcom_softc *, int); 126 Static void uplcom_rts(struct uplcom_softc *, int); 127 Static void uplcom_break(struct uplcom_softc *, int); 128 Static void uplcom_set_line_state(struct uplcom_softc *); 129 Static void uplcom_get_status(void *, int portno, u_char *lsr, u_char *msr); 130 #if TODO 131 Static int uplcom_ioctl(void *, int, u_long, void *, int, proc_t *); 132 #endif 133 Static int uplcom_param(void *, int, struct termios *); 134 Static int uplcom_open(void *, int); 135 Static void uplcom_close(void *, int); 136 Static usbd_status uplcom_vendor_control_write(usbd_device_handle, u_int16_t, u_int16_t); 137 138 struct ucom_methods uplcom_methods = { 139 uplcom_get_status, 140 uplcom_set, 141 uplcom_param, 142 NULL, /* uplcom_ioctl, TODO */ 143 uplcom_open, 144 uplcom_close, 145 NULL, 146 NULL, 147 }; 148 149 static const struct usb_devno uplcom_devs[] = { 150 /* I/O DATA USB-RSAQ2 */ 151 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 }, 152 /* I/O DATA USB-RSAQ3 */ 153 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3 }, 154 /* I/O DATA USB-RSAQ */ 155 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ }, 156 /* I/O DATA USB-RSAQ5 */ 157 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ5 }, 158 /* PLANEX USB-RS232 URS-03 */ 159 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A }, 160 /* various */ 161 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 }, 162 /* SMART Technologies USB to serial */ 163 { USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_PL2303 }, 164 /* IOGEAR/ATENTRIPPLITE */ 165 { USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 }, 166 /* ELECOM UC-SGT */ 167 { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT }, 168 /* ELECOM UC-SGT0 */ 169 { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 }, 170 /* Panasonic 50" Touch Panel */ 171 { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_TYTP50P6S }, 172 /* RATOC REX-USB60 */ 173 { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 }, 174 /* TDK USB-PHS Adapter UHA6400 */ 175 { USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 }, 176 /* TDK USB-PDC Adapter UPA9664 */ 177 { USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 }, 178 /* Sony Ericsson USB Cable */ 179 { USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU10 }, 180 /* SOURCENEXT KeikaiDenwa 8 */ 181 { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 }, 182 /* SOURCENEXT KeikaiDenwa 8 with charger */ 183 { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG }, 184 /* HAL Corporation Crossam2+USB */ 185 { USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 }, 186 /* Sitecom USB to serial cable */ 187 { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 }, 188 /* Pharos USB GPS - Microsoft version */ 189 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X }, 190 /* Willcom WS002IN (DD) */ 191 { USB_VENDOR_NETINDEX, USB_PRODUCT_NETINDEX_WS002IN }, 192 /* COREGA CG-USBRS232R */ 193 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGUSBRS232R }, 194 /* Sharp CE-175TU (USB to Zaurus option port 15 adapter) */ 195 { USB_VENDOR_SHARP, USB_PRODUCT_SHARP_CE175TU }, 196 }; 197 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p) 198 199 int uplcom_match(device_t, cfdata_t, void *); 200 void uplcom_attach(device_t, device_t, void *); 201 void uplcom_childdet(device_t, device_t); 202 int uplcom_detach(device_t, int); 203 int uplcom_activate(device_t, enum devact); 204 extern struct cfdriver uplcom_cd; 205 CFATTACH_DECL2_NEW(uplcom, sizeof(struct uplcom_softc), uplcom_match, 206 uplcom_attach, uplcom_detach, uplcom_activate, NULL, uplcom_childdet); 207 208 int 209 uplcom_match(device_t parent, cfdata_t match, void *aux) 210 { 211 struct usb_attach_arg *uaa = aux; 212 213 return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ? 214 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 215 } 216 217 void 218 uplcom_attach(device_t parent, device_t self, void *aux) 219 { 220 struct uplcom_softc *sc = device_private(self); 221 struct usb_attach_arg *uaa = aux; 222 usbd_device_handle dev = uaa->device; 223 usb_device_descriptor_t *ddesc; 224 usb_config_descriptor_t *cdesc; 225 usb_interface_descriptor_t *id; 226 usb_endpoint_descriptor_t *ed; 227 char *devinfop; 228 const char *devname = device_xname(self); 229 usbd_status err; 230 int i; 231 struct ucom_attach_args uca; 232 233 sc->sc_dev = self; 234 235 aprint_naive("\n"); 236 aprint_normal("\n"); 237 238 devinfop = usbd_devinfo_alloc(dev, 0); 239 aprint_normal_dev(self, "%s\n", devinfop); 240 usbd_devinfo_free(devinfop); 241 242 sc->sc_udev = dev; 243 244 DPRINTF(("\n\nuplcom attach: sc=%p\n", sc)); 245 246 /* initialize endpoints */ 247 uca.bulkin = uca.bulkout = -1; 248 sc->sc_intr_number = -1; 249 sc->sc_intr_pipe = NULL; 250 251 /* Move the device into the configured state. */ 252 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1); 253 if (err) { 254 aprint_error("\n%s: failed to set configuration, err=%s\n", 255 devname, usbd_errstr(err)); 256 sc->sc_dying = 1; 257 return; 258 } 259 260 /* determine chip type */ 261 ddesc = usbd_get_device_descriptor(dev); 262 if (ddesc->bDeviceClass != UDCLASS_COMM && 263 ddesc->bMaxPacketSize == 0x40) 264 sc->sc_type = UPLCOM_TYPE_HX; 265 266 #ifdef UPLCOM_DEBUG 267 /* print the chip type */ 268 if (sc->sc_type == UPLCOM_TYPE_HX) { 269 DPRINTF(("uplcom_attach: chiptype HX\n")); 270 } else { 271 DPRINTF(("uplcom_attach: chiptype 0\n")); 272 } 273 #endif 274 275 /* Move the device into the configured state. */ 276 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1); 277 if (err) { 278 aprint_error_dev(self, "failed to set configuration: %s\n", 279 usbd_errstr(err)); 280 sc->sc_dying = 1; 281 return; 282 } 283 284 /* get the config descriptor */ 285 cdesc = usbd_get_config_descriptor(sc->sc_udev); 286 287 if (cdesc == NULL) { 288 aprint_error_dev(self, 289 "failed to get configuration descriptor\n"); 290 sc->sc_dying = 1; 291 return; 292 } 293 294 /* get the (first/common) interface */ 295 err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX, 296 &sc->sc_iface); 297 if (err) { 298 aprint_error("\n%s: failed to get interface, err=%s\n", 299 devname, usbd_errstr(err)); 300 sc->sc_dying = 1; 301 return; 302 } 303 304 /* Find the interrupt endpoints */ 305 306 id = usbd_get_interface_descriptor(sc->sc_iface); 307 sc->sc_iface_number = id->bInterfaceNumber; 308 309 for (i = 0; i < id->bNumEndpoints; i++) { 310 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 311 if (ed == NULL) { 312 aprint_error_dev(self, 313 "no endpoint descriptor for %d\n", i); 314 sc->sc_dying = 1; 315 return; 316 } 317 318 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 319 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 320 sc->sc_intr_number = ed->bEndpointAddress; 321 sc->sc_isize = UGETW(ed->wMaxPacketSize); 322 } 323 } 324 325 if (sc->sc_intr_number== -1) { 326 aprint_error_dev(self, "Could not find interrupt in\n"); 327 sc->sc_dying = 1; 328 return; 329 } 330 331 /* keep interface for interrupt */ 332 sc->sc_intr_iface = sc->sc_iface; 333 334 /* 335 * USB-RSAQ1 has two interface 336 * 337 * USB-RSAQ1 | USB-RSAQ2 338 * -----------------+----------------- 339 * Interface 0 |Interface 0 340 * Interrupt(0x81) | Interrupt(0x81) 341 * -----------------+ BulkIN(0x02) 342 * Interface 1 | BulkOUT(0x83) 343 * BulkIN(0x02) | 344 * BulkOUT(0x83) | 345 */ 346 if (cdesc->bNumInterface == 2) { 347 err = usbd_device2interface_handle(dev, 348 UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface); 349 if (err) { 350 aprint_error("\n%s: failed to get second interface, err=%s\n", 351 devname, usbd_errstr(err)); 352 sc->sc_dying = 1; 353 return; 354 } 355 } 356 357 /* Find the bulk{in,out} endpoints */ 358 359 id = usbd_get_interface_descriptor(sc->sc_iface); 360 sc->sc_iface_number = id->bInterfaceNumber; 361 362 for (i = 0; i < id->bNumEndpoints; i++) { 363 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 364 if (ed == NULL) { 365 aprint_error_dev(self, 366 "no endpoint descriptor for %d\n", i); 367 sc->sc_dying = 1; 368 return; 369 } 370 371 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 372 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 373 uca.bulkin = ed->bEndpointAddress; 374 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 375 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 376 uca.bulkout = ed->bEndpointAddress; 377 } 378 } 379 380 if (uca.bulkin == -1) { 381 aprint_error_dev(self, "Could not find data bulk in\n"); 382 sc->sc_dying = 1; 383 return; 384 } 385 386 if (uca.bulkout == -1) { 387 aprint_error_dev(self, "Could not find data bulk out\n"); 388 sc->sc_dying = 1; 389 return; 390 } 391 392 sc->sc_dtr = sc->sc_rts = -1; 393 uca.portno = UCOM_UNK_PORTNO; 394 /* bulkin, bulkout set above */ 395 uca.ibufsize = UPLCOMIBUFSIZE; 396 uca.obufsize = UPLCOMOBUFSIZE; 397 uca.ibufsizepad = UPLCOMIBUFSIZE; 398 uca.opkthdrlen = 0; 399 uca.device = dev; 400 uca.iface = sc->sc_iface; 401 uca.methods = &uplcom_methods; 402 uca.arg = sc; 403 uca.info = NULL; 404 405 err = uplcom_reset(sc); 406 407 if (err) { 408 aprint_error_dev(self, "reset failed, %s\n", usbd_errstr(err)); 409 sc->sc_dying = 1; 410 return; 411 } 412 413 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 414 sc->sc_dev); 415 416 DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n", 417 uca.bulkin, uca.bulkout, sc->sc_intr_number )); 418 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca, 419 ucomprint, ucomsubmatch); 420 421 if (!pmf_device_register(self, NULL, NULL)) 422 aprint_error_dev(self, "couldn't establish power handler\n"); 423 424 return; 425 } 426 427 void 428 uplcom_childdet(device_t self, device_t child) 429 { 430 struct uplcom_softc *sc = device_private(self); 431 432 KASSERT(sc->sc_subdev == child); 433 sc->sc_subdev = NULL; 434 } 435 436 int 437 uplcom_detach(device_t self, int flags) 438 { 439 struct uplcom_softc *sc = device_private(self); 440 int rv = 0; 441 442 DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags)); 443 444 if (sc->sc_intr_pipe != NULL) { 445 usbd_abort_pipe(sc->sc_intr_pipe); 446 usbd_close_pipe(sc->sc_intr_pipe); 447 free(sc->sc_intr_buf, M_USBDEV); 448 sc->sc_intr_pipe = NULL; 449 } 450 451 sc->sc_dying = 1; 452 if (sc->sc_subdev != NULL) 453 rv = config_detach(sc->sc_subdev, flags); 454 455 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 456 sc->sc_dev); 457 458 if (rv == 0) 459 pmf_device_deregister(self); 460 461 return (rv); 462 } 463 464 int 465 uplcom_activate(device_t self, enum devact act) 466 { 467 struct uplcom_softc *sc = device_private(self); 468 469 switch (act) { 470 case DVACT_DEACTIVATE: 471 sc->sc_dying = 1; 472 return 0; 473 default: 474 return EOPNOTSUPP; 475 } 476 } 477 478 usbd_status 479 uplcom_reset(struct uplcom_softc *sc) 480 { 481 usb_device_request_t req; 482 usbd_status err; 483 484 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 485 req.bRequest = UPLCOM_SET_REQUEST; 486 USETW(req.wValue, 0); 487 USETW(req.wIndex, sc->sc_iface_number); 488 USETW(req.wLength, 0); 489 490 err = usbd_do_request(sc->sc_udev, &req, 0); 491 if (err) 492 return (EIO); 493 494 return (0); 495 } 496 497 struct pl2303x_init { 498 uint8_t req_type; 499 uint8_t request; 500 uint16_t value; 501 uint16_t index; 502 }; 503 504 static const struct pl2303x_init pl2303x[] = { 505 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0 }, 506 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0 }, 507 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0 }, 508 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0 }, 509 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0 }, 510 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1 }, 511 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0 }, 512 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0 }, 513 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1 }, 514 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0 }, 515 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44 } 516 }; 517 #define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0])) 518 519 static usbd_status 520 uplcom_pl2303x_init(struct uplcom_softc *sc) 521 { 522 usb_device_request_t req; 523 usbd_status err; 524 int i; 525 526 for (i = 0; i < N_PL2302X_INIT; i++) { 527 char buf[1]; 528 void *b; 529 530 req.bmRequestType = pl2303x[i].req_type; 531 req.bRequest = pl2303x[i].request; 532 USETW(req.wValue, pl2303x[i].value); 533 USETW(req.wIndex, pl2303x[i].index); 534 if (UT_GET_DIR(req.bmRequestType) == UT_READ) { 535 b = buf; 536 USETW(req.wLength, sizeof(buf)); 537 } else { 538 b = NULL; 539 USETW(req.wLength, 0); 540 } 541 542 err = usbd_do_request(sc->sc_udev, &req, b); 543 if (err) { 544 aprint_error_dev(sc->sc_dev, 545 "uplcom_pl2303x_init failed: %s\n", 546 usbd_errstr(err)); 547 return (EIO); 548 } 549 } 550 551 return (0); 552 } 553 554 void 555 uplcom_set_line_state(struct uplcom_softc *sc) 556 { 557 usb_device_request_t req; 558 int ls; 559 560 /* make sure we have initialized state for sc_dtr and sc_rts */ 561 if (sc->sc_dtr == -1) 562 sc->sc_dtr = 0; 563 if (sc->sc_rts == -1) 564 sc->sc_rts = 0; 565 566 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) | 567 (sc->sc_rts ? UCDC_LINE_RTS : 0); 568 569 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 570 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 571 USETW(req.wValue, ls); 572 USETW(req.wIndex, sc->sc_iface_number); 573 USETW(req.wLength, 0); 574 575 (void)usbd_do_request(sc->sc_udev, &req, 0); 576 } 577 578 void 579 uplcom_set(void *addr, int portno, int reg, int onoff) 580 { 581 struct uplcom_softc *sc = addr; 582 583 switch (reg) { 584 case UCOM_SET_DTR: 585 uplcom_dtr(sc, onoff); 586 break; 587 case UCOM_SET_RTS: 588 uplcom_rts(sc, onoff); 589 break; 590 case UCOM_SET_BREAK: 591 uplcom_break(sc, onoff); 592 break; 593 default: 594 break; 595 } 596 } 597 598 void 599 uplcom_dtr(struct uplcom_softc *sc, int onoff) 600 { 601 602 DPRINTF(("uplcom_dtr: onoff=%d\n", onoff)); 603 604 if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff) 605 return; 606 607 sc->sc_dtr = !!onoff; 608 609 uplcom_set_line_state(sc); 610 } 611 612 void 613 uplcom_rts(struct uplcom_softc *sc, int onoff) 614 { 615 DPRINTF(("uplcom_rts: onoff=%d\n", onoff)); 616 617 if (sc->sc_rts != -1 && !sc->sc_rts == !onoff) 618 return; 619 620 sc->sc_rts = !!onoff; 621 622 uplcom_set_line_state(sc); 623 } 624 625 void 626 uplcom_break(struct uplcom_softc *sc, int onoff) 627 { 628 usb_device_request_t req; 629 630 DPRINTF(("uplcom_break: onoff=%d\n", onoff)); 631 632 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 633 req.bRequest = UCDC_SEND_BREAK; 634 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF); 635 USETW(req.wIndex, sc->sc_iface_number); 636 USETW(req.wLength, 0); 637 638 (void)usbd_do_request(sc->sc_udev, &req, 0); 639 } 640 641 usbd_status 642 uplcom_set_crtscts(struct uplcom_softc *sc) 643 { 644 usb_device_request_t req; 645 usbd_status err; 646 647 DPRINTF(("uplcom_set_crtscts: on\n")); 648 649 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 650 req.bRequest = UPLCOM_SET_REQUEST; 651 USETW(req.wValue, 0); 652 if (sc->sc_type == UPLCOM_TYPE_HX) 653 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_HX); 654 else 655 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_0); 656 USETW(req.wLength, 0); 657 658 err = usbd_do_request(sc->sc_udev, &req, 0); 659 if (err) { 660 DPRINTF(("uplcom_set_crtscts: failed, err=%s\n", 661 usbd_errstr(err))); 662 return (err); 663 } 664 665 return (USBD_NORMAL_COMPLETION); 666 } 667 668 usbd_status 669 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state) 670 { 671 usb_device_request_t req; 672 usbd_status err; 673 674 DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n", 675 UGETDW(state->dwDTERate), state->bCharFormat, 676 state->bParityType, state->bDataBits)); 677 678 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) { 679 DPRINTF(("uplcom_set_line_coding: already set\n")); 680 return (USBD_NORMAL_COMPLETION); 681 } 682 683 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 684 req.bRequest = UCDC_SET_LINE_CODING; 685 USETW(req.wValue, 0); 686 USETW(req.wIndex, sc->sc_iface_number); 687 USETW(req.wLength, UCDC_LINE_STATE_LENGTH); 688 689 err = usbd_do_request(sc->sc_udev, &req, state); 690 if (err) { 691 DPRINTF(("uplcom_set_line_coding: failed, err=%s\n", 692 usbd_errstr(err))); 693 return (err); 694 } 695 696 sc->sc_line_state = *state; 697 698 return (USBD_NORMAL_COMPLETION); 699 } 700 701 int 702 uplcom_param(void *addr, int portno, struct termios *t) 703 { 704 struct uplcom_softc *sc = addr; 705 usbd_status err; 706 usb_cdc_line_state_t ls; 707 708 DPRINTF(("uplcom_param: sc=%p\n", sc)); 709 710 USETDW(ls.dwDTERate, t->c_ospeed); 711 if (ISSET(t->c_cflag, CSTOPB)) 712 ls.bCharFormat = UCDC_STOP_BIT_2; 713 else 714 ls.bCharFormat = UCDC_STOP_BIT_1; 715 if (ISSET(t->c_cflag, PARENB)) { 716 if (ISSET(t->c_cflag, PARODD)) 717 ls.bParityType = UCDC_PARITY_ODD; 718 else 719 ls.bParityType = UCDC_PARITY_EVEN; 720 } else 721 ls.bParityType = UCDC_PARITY_NONE; 722 switch (ISSET(t->c_cflag, CSIZE)) { 723 case CS5: 724 ls.bDataBits = 5; 725 break; 726 case CS6: 727 ls.bDataBits = 6; 728 break; 729 case CS7: 730 ls.bDataBits = 7; 731 break; 732 case CS8: 733 ls.bDataBits = 8; 734 break; 735 } 736 737 err = uplcom_set_line_coding(sc, &ls); 738 if (err) { 739 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err))); 740 return (EIO); 741 } 742 743 if (ISSET(t->c_cflag, CRTSCTS)) 744 uplcom_set_crtscts(sc); 745 746 if (sc->sc_rts == -1 || sc->sc_dtr == -1) 747 uplcom_set_line_state(sc); 748 749 if (err) { 750 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err))); 751 return (EIO); 752 } 753 754 return (0); 755 } 756 757 Static usbd_status 758 uplcom_vendor_control_write(usbd_device_handle dev, u_int16_t value, u_int16_t index) 759 { 760 usb_device_request_t req; 761 usbd_status err; 762 763 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 764 req.bRequest = UPLCOM_SET_REQUEST; 765 USETW(req.wValue, value); 766 USETW(req.wIndex, index); 767 USETW(req.wLength, 0); 768 769 err = usbd_do_request(dev, &req, NULL); 770 771 if (err) { 772 DPRINTF(("uplcom_open: vendor write failed, err=%s (%d)\n", 773 usbd_errstr(err), err)); 774 } 775 776 return err; 777 } 778 779 int 780 uplcom_open(void *addr, int portno) 781 { 782 struct uplcom_softc *sc = addr; 783 usbd_status err; 784 785 if (sc->sc_dying) 786 return (EIO); 787 788 DPRINTF(("uplcom_open: sc=%p\n", sc)); 789 790 /* Some unknown device frobbing. */ 791 if (sc->sc_type == UPLCOM_TYPE_HX) 792 uplcom_vendor_control_write(sc->sc_udev, 2, 0x44); 793 else 794 uplcom_vendor_control_write(sc->sc_udev, 2, 0x24); 795 796 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) { 797 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK); 798 err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number, 799 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, 800 sc->sc_intr_buf, sc->sc_isize, 801 uplcom_intr, USBD_DEFAULT_INTERVAL); 802 if (err) { 803 DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n", 804 device_xname(sc->sc_dev), sc->sc_intr_number)); 805 return (EIO); 806 } 807 } 808 809 if (sc->sc_type == UPLCOM_TYPE_HX) 810 return (uplcom_pl2303x_init(sc)); 811 812 return (0); 813 } 814 815 void 816 uplcom_close(void *addr, int portno) 817 { 818 struct uplcom_softc *sc = addr; 819 int err; 820 821 if (sc->sc_dying) 822 return; 823 824 DPRINTF(("uplcom_close: close\n")); 825 826 if (sc->sc_intr_pipe != NULL) { 827 err = usbd_abort_pipe(sc->sc_intr_pipe); 828 if (err) 829 printf("%s: abort interrupt pipe failed: %s\n", 830 device_xname(sc->sc_dev), usbd_errstr(err)); 831 err = usbd_close_pipe(sc->sc_intr_pipe); 832 if (err) 833 printf("%s: close interrupt pipe failed: %s\n", 834 device_xname(sc->sc_dev), usbd_errstr(err)); 835 free(sc->sc_intr_buf, M_USBDEV); 836 sc->sc_intr_pipe = NULL; 837 } 838 } 839 840 void 841 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, 842 usbd_status status) 843 { 844 struct uplcom_softc *sc = priv; 845 u_char *buf = sc->sc_intr_buf; 846 u_char pstatus; 847 848 if (sc->sc_dying) 849 return; 850 851 if (status != USBD_NORMAL_COMPLETION) { 852 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 853 return; 854 855 DPRINTF(("%s: abnormal status: %s\n", device_xname(sc->sc_dev), 856 usbd_errstr(status))); 857 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe); 858 return; 859 } 860 861 DPRINTF(("%s: uplcom status = %02x\n", device_xname(sc->sc_dev), buf[8])); 862 863 sc->sc_lsr = sc->sc_msr = 0; 864 pstatus = buf[8]; 865 if (ISSET(pstatus, UPLCOM_N_SERIAL_CTS)) 866 sc->sc_msr |= UMSR_CTS; 867 if (ISSET(pstatus, UCDC_N_SERIAL_RI)) 868 sc->sc_msr |= UMSR_RI; 869 if (ISSET(pstatus, UCDC_N_SERIAL_DSR)) 870 sc->sc_msr |= UMSR_DSR; 871 if (ISSET(pstatus, UCDC_N_SERIAL_DCD)) 872 sc->sc_msr |= UMSR_DCD; 873 ucom_status_change(device_private(sc->sc_subdev)); 874 } 875 876 void 877 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr) 878 { 879 struct uplcom_softc *sc = addr; 880 881 DPRINTF(("uplcom_get_status:\n")); 882 883 if (lsr != NULL) 884 *lsr = sc->sc_lsr; 885 if (msr != NULL) 886 *msr = sc->sc_msr; 887 } 888 889 #if TODO 890 int 891 uplcom_ioctl(void *addr, int portno, u_long cmd, void *data, int flag, 892 proc_t *p) 893 { 894 struct uplcom_softc *sc = addr; 895 int error = 0; 896 897 if (sc->sc_dying) 898 return (EIO); 899 900 DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd)); 901 902 switch (cmd) { 903 case TIOCNOTTY: 904 case TIOCMGET: 905 case TIOCMSET: 906 case USB_GET_CM_OVER_DATA: 907 case USB_SET_CM_OVER_DATA: 908 break; 909 910 default: 911 DPRINTF(("uplcom_ioctl: unknown\n")); 912 error = ENOTTY; 913 break; 914 } 915 916 return (error); 917 } 918 #endif 919