1 /* $NetBSD: uplcom.c,v 1.74 2014/07/14 12:04:48 ryoon 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.74 2014/07/14 12:04:48 ryoon 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 return; 422 } 423 424 void 425 uplcom_childdet(device_t self, device_t child) 426 { 427 struct uplcom_softc *sc = device_private(self); 428 429 KASSERT(sc->sc_subdev == child); 430 sc->sc_subdev = NULL; 431 } 432 433 int 434 uplcom_detach(device_t self, int flags) 435 { 436 struct uplcom_softc *sc = device_private(self); 437 int rv = 0; 438 439 DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags)); 440 441 if (sc->sc_intr_pipe != NULL) { 442 usbd_abort_pipe(sc->sc_intr_pipe); 443 usbd_close_pipe(sc->sc_intr_pipe); 444 free(sc->sc_intr_buf, M_USBDEV); 445 sc->sc_intr_pipe = NULL; 446 } 447 448 sc->sc_dying = 1; 449 if (sc->sc_subdev != NULL) 450 rv = config_detach(sc->sc_subdev, flags); 451 452 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 453 sc->sc_dev); 454 455 return (rv); 456 } 457 458 int 459 uplcom_activate(device_t self, enum devact act) 460 { 461 struct uplcom_softc *sc = device_private(self); 462 463 switch (act) { 464 case DVACT_DEACTIVATE: 465 sc->sc_dying = 1; 466 return 0; 467 default: 468 return EOPNOTSUPP; 469 } 470 } 471 472 usbd_status 473 uplcom_reset(struct uplcom_softc *sc) 474 { 475 usb_device_request_t req; 476 usbd_status err; 477 478 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 479 req.bRequest = UPLCOM_SET_REQUEST; 480 USETW(req.wValue, 0); 481 USETW(req.wIndex, sc->sc_iface_number); 482 USETW(req.wLength, 0); 483 484 err = usbd_do_request(sc->sc_udev, &req, 0); 485 if (err) 486 return (EIO); 487 488 return (0); 489 } 490 491 struct pl2303x_init { 492 uint8_t req_type; 493 uint8_t request; 494 uint16_t value; 495 uint16_t index; 496 uint16_t length; 497 }; 498 499 static const struct pl2303x_init pl2303x[] = { 500 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 }, 501 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0 }, 502 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 }, 503 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 }, 504 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 }, 505 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0 }, 506 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 }, 507 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 }, 508 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0 }, 509 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0 }, 510 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0 } 511 }; 512 #define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0])) 513 514 static usbd_status 515 uplcom_pl2303x_init(struct uplcom_softc *sc) 516 { 517 usb_device_request_t req; 518 usbd_status err; 519 int i; 520 521 for (i = 0; i < N_PL2302X_INIT; i++) { 522 req.bmRequestType = pl2303x[i].req_type; 523 req.bRequest = pl2303x[i].request; 524 USETW(req.wValue, pl2303x[i].value); 525 USETW(req.wIndex, pl2303x[i].index); 526 USETW(req.wLength, pl2303x[i].length); 527 528 err = usbd_do_request(sc->sc_udev, &req, 0); 529 if (err) { 530 aprint_error_dev(sc->sc_dev, 531 "uplcom_pl2303x_init failed: %s\n", 532 usbd_errstr(err)); 533 return (EIO); 534 } 535 } 536 537 return (0); 538 } 539 540 void 541 uplcom_set_line_state(struct uplcom_softc *sc) 542 { 543 usb_device_request_t req; 544 int ls; 545 546 /* make sure we have initialized state for sc_dtr and sc_rts */ 547 if (sc->sc_dtr == -1) 548 sc->sc_dtr = 0; 549 if (sc->sc_rts == -1) 550 sc->sc_rts = 0; 551 552 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) | 553 (sc->sc_rts ? UCDC_LINE_RTS : 0); 554 555 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 556 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 557 USETW(req.wValue, ls); 558 USETW(req.wIndex, sc->sc_iface_number); 559 USETW(req.wLength, 0); 560 561 (void)usbd_do_request(sc->sc_udev, &req, 0); 562 } 563 564 void 565 uplcom_set(void *addr, int portno, int reg, int onoff) 566 { 567 struct uplcom_softc *sc = addr; 568 569 switch (reg) { 570 case UCOM_SET_DTR: 571 uplcom_dtr(sc, onoff); 572 break; 573 case UCOM_SET_RTS: 574 uplcom_rts(sc, onoff); 575 break; 576 case UCOM_SET_BREAK: 577 uplcom_break(sc, onoff); 578 break; 579 default: 580 break; 581 } 582 } 583 584 void 585 uplcom_dtr(struct uplcom_softc *sc, int onoff) 586 { 587 588 DPRINTF(("uplcom_dtr: onoff=%d\n", onoff)); 589 590 if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff) 591 return; 592 593 sc->sc_dtr = !!onoff; 594 595 uplcom_set_line_state(sc); 596 } 597 598 void 599 uplcom_rts(struct uplcom_softc *sc, int onoff) 600 { 601 DPRINTF(("uplcom_rts: onoff=%d\n", onoff)); 602 603 if (sc->sc_rts != -1 && !sc->sc_rts == !onoff) 604 return; 605 606 sc->sc_rts = !!onoff; 607 608 uplcom_set_line_state(sc); 609 } 610 611 void 612 uplcom_break(struct uplcom_softc *sc, int onoff) 613 { 614 usb_device_request_t req; 615 616 DPRINTF(("uplcom_break: onoff=%d\n", onoff)); 617 618 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 619 req.bRequest = UCDC_SEND_BREAK; 620 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF); 621 USETW(req.wIndex, sc->sc_iface_number); 622 USETW(req.wLength, 0); 623 624 (void)usbd_do_request(sc->sc_udev, &req, 0); 625 } 626 627 usbd_status 628 uplcom_set_crtscts(struct uplcom_softc *sc) 629 { 630 usb_device_request_t req; 631 usbd_status err; 632 633 DPRINTF(("uplcom_set_crtscts: on\n")); 634 635 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 636 req.bRequest = UPLCOM_SET_REQUEST; 637 USETW(req.wValue, 0); 638 if (sc->sc_type == UPLCOM_TYPE_HX) 639 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_HX); 640 else 641 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_0); 642 USETW(req.wLength, 0); 643 644 err = usbd_do_request(sc->sc_udev, &req, 0); 645 if (err) { 646 DPRINTF(("uplcom_set_crtscts: failed, err=%s\n", 647 usbd_errstr(err))); 648 return (err); 649 } 650 651 return (USBD_NORMAL_COMPLETION); 652 } 653 654 usbd_status 655 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state) 656 { 657 usb_device_request_t req; 658 usbd_status err; 659 660 DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n", 661 UGETDW(state->dwDTERate), state->bCharFormat, 662 state->bParityType, state->bDataBits)); 663 664 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) { 665 DPRINTF(("uplcom_set_line_coding: already set\n")); 666 return (USBD_NORMAL_COMPLETION); 667 } 668 669 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 670 req.bRequest = UCDC_SET_LINE_CODING; 671 USETW(req.wValue, 0); 672 USETW(req.wIndex, sc->sc_iface_number); 673 USETW(req.wLength, UCDC_LINE_STATE_LENGTH); 674 675 err = usbd_do_request(sc->sc_udev, &req, state); 676 if (err) { 677 DPRINTF(("uplcom_set_line_coding: failed, err=%s\n", 678 usbd_errstr(err))); 679 return (err); 680 } 681 682 sc->sc_line_state = *state; 683 684 return (USBD_NORMAL_COMPLETION); 685 } 686 687 int 688 uplcom_param(void *addr, int portno, struct termios *t) 689 { 690 struct uplcom_softc *sc = addr; 691 usbd_status err; 692 usb_cdc_line_state_t ls; 693 694 DPRINTF(("uplcom_param: sc=%p\n", sc)); 695 696 USETDW(ls.dwDTERate, t->c_ospeed); 697 if (ISSET(t->c_cflag, CSTOPB)) 698 ls.bCharFormat = UCDC_STOP_BIT_2; 699 else 700 ls.bCharFormat = UCDC_STOP_BIT_1; 701 if (ISSET(t->c_cflag, PARENB)) { 702 if (ISSET(t->c_cflag, PARODD)) 703 ls.bParityType = UCDC_PARITY_ODD; 704 else 705 ls.bParityType = UCDC_PARITY_EVEN; 706 } else 707 ls.bParityType = UCDC_PARITY_NONE; 708 switch (ISSET(t->c_cflag, CSIZE)) { 709 case CS5: 710 ls.bDataBits = 5; 711 break; 712 case CS6: 713 ls.bDataBits = 6; 714 break; 715 case CS7: 716 ls.bDataBits = 7; 717 break; 718 case CS8: 719 ls.bDataBits = 8; 720 break; 721 } 722 723 err = uplcom_set_line_coding(sc, &ls); 724 if (err) { 725 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err))); 726 return (EIO); 727 } 728 729 if (ISSET(t->c_cflag, CRTSCTS)) 730 uplcom_set_crtscts(sc); 731 732 if (sc->sc_rts == -1 || sc->sc_dtr == -1) 733 uplcom_set_line_state(sc); 734 735 if (err) { 736 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err))); 737 return (EIO); 738 } 739 740 return (0); 741 } 742 743 Static usbd_status 744 uplcom_vendor_control_write(usbd_device_handle dev, u_int16_t value, u_int16_t index) 745 { 746 usb_device_request_t req; 747 usbd_status err; 748 749 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 750 req.bRequest = UPLCOM_SET_REQUEST; 751 USETW(req.wValue, value); 752 USETW(req.wIndex, index); 753 USETW(req.wLength, 0); 754 755 err = usbd_do_request(dev, &req, NULL); 756 757 if (err) { 758 DPRINTF(("uplcom_open: vendor write failed, err=%s (%d)\n", 759 usbd_errstr(err), err)); 760 } 761 762 return err; 763 } 764 765 int 766 uplcom_open(void *addr, int portno) 767 { 768 struct uplcom_softc *sc = addr; 769 usbd_status err; 770 771 if (sc->sc_dying) 772 return (EIO); 773 774 DPRINTF(("uplcom_open: sc=%p\n", sc)); 775 776 /* Some unknown device frobbing. */ 777 if (sc->sc_type == UPLCOM_TYPE_HX) 778 uplcom_vendor_control_write(sc->sc_udev, 2, 0x44); 779 else 780 uplcom_vendor_control_write(sc->sc_udev, 2, 0x24); 781 782 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) { 783 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK); 784 err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number, 785 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, 786 sc->sc_intr_buf, sc->sc_isize, 787 uplcom_intr, USBD_DEFAULT_INTERVAL); 788 if (err) { 789 DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n", 790 device_xname(sc->sc_dev), sc->sc_intr_number)); 791 return (EIO); 792 } 793 } 794 795 if (sc->sc_type == UPLCOM_TYPE_HX) 796 return (uplcom_pl2303x_init(sc)); 797 798 return (0); 799 } 800 801 void 802 uplcom_close(void *addr, int portno) 803 { 804 struct uplcom_softc *sc = addr; 805 int err; 806 807 if (sc->sc_dying) 808 return; 809 810 DPRINTF(("uplcom_close: close\n")); 811 812 if (sc->sc_intr_pipe != NULL) { 813 err = usbd_abort_pipe(sc->sc_intr_pipe); 814 if (err) 815 printf("%s: abort interrupt pipe failed: %s\n", 816 device_xname(sc->sc_dev), usbd_errstr(err)); 817 err = usbd_close_pipe(sc->sc_intr_pipe); 818 if (err) 819 printf("%s: close interrupt pipe failed: %s\n", 820 device_xname(sc->sc_dev), usbd_errstr(err)); 821 free(sc->sc_intr_buf, M_USBDEV); 822 sc->sc_intr_pipe = NULL; 823 } 824 } 825 826 void 827 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, 828 usbd_status status) 829 { 830 struct uplcom_softc *sc = priv; 831 u_char *buf = sc->sc_intr_buf; 832 u_char pstatus; 833 834 if (sc->sc_dying) 835 return; 836 837 if (status != USBD_NORMAL_COMPLETION) { 838 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 839 return; 840 841 DPRINTF(("%s: abnormal status: %s\n", device_xname(sc->sc_dev), 842 usbd_errstr(status))); 843 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe); 844 return; 845 } 846 847 DPRINTF(("%s: uplcom status = %02x\n", device_xname(sc->sc_dev), buf[8])); 848 849 sc->sc_lsr = sc->sc_msr = 0; 850 pstatus = buf[8]; 851 if (ISSET(pstatus, UPLCOM_N_SERIAL_CTS)) 852 sc->sc_msr |= UMSR_CTS; 853 if (ISSET(pstatus, UCDC_N_SERIAL_RI)) 854 sc->sc_msr |= UMSR_RI; 855 if (ISSET(pstatus, UCDC_N_SERIAL_DSR)) 856 sc->sc_msr |= UMSR_DSR; 857 if (ISSET(pstatus, UCDC_N_SERIAL_DCD)) 858 sc->sc_msr |= UMSR_DCD; 859 ucom_status_change(device_private(sc->sc_subdev)); 860 } 861 862 void 863 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr) 864 { 865 struct uplcom_softc *sc = addr; 866 867 DPRINTF(("uplcom_get_status:\n")); 868 869 if (lsr != NULL) 870 *lsr = sc->sc_lsr; 871 if (msr != NULL) 872 *msr = sc->sc_msr; 873 } 874 875 #if TODO 876 int 877 uplcom_ioctl(void *addr, int portno, u_long cmd, void *data, int flag, 878 proc_t *p) 879 { 880 struct uplcom_softc *sc = addr; 881 int error = 0; 882 883 if (sc->sc_dying) 884 return (EIO); 885 886 DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd)); 887 888 switch (cmd) { 889 case TIOCNOTTY: 890 case TIOCMGET: 891 case TIOCMSET: 892 case USB_GET_CM_OVER_DATA: 893 case USB_SET_CM_OVER_DATA: 894 break; 895 896 default: 897 DPRINTF(("uplcom_ioctl: unknown\n")); 898 error = ENOTTY; 899 break; 900 } 901 902 return (error); 903 } 904 #endif 905