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