1 /* $NetBSD: uplcom.c,v 1.47 2005/12/11 12:24:01 christos Exp $ */ 2 /* 3 * Copyright (c) 2001 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to The NetBSD Foundation 7 * by Ichiro FUKUHARA (ichiro@ichiro.org). 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the NetBSD 20 * Foundation, Inc. and its contributors. 21 * 4. Neither the name of The NetBSD Foundation nor the names of its 22 * contributors may be used to endorse or promote products derived 23 * from this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 /* 39 * General information: http://www.prolific.com.tw/fr_pl2303.htm 40 * http://www.hitachi-hitec.com/jyouhou/prolific/2303.pdf 41 */ 42 43 #include <sys/cdefs.h> 44 __KERNEL_RCSID(0, "$NetBSD: uplcom.c,v 1.47 2005/12/11 12:24:01 christos Exp $"); 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/malloc.h> 50 #include <sys/ioctl.h> 51 #include <sys/conf.h> 52 #include <sys/tty.h> 53 #include <sys/file.h> 54 #include <sys/select.h> 55 #include <sys/proc.h> 56 #include <sys/device.h> 57 #include <sys/poll.h> 58 59 #include <dev/usb/usb.h> 60 #include <dev/usb/usbcdc.h> 61 62 #include <dev/usb/usbdi.h> 63 #include <dev/usb/usbdi_util.h> 64 #include <dev/usb/usbdevs.h> 65 #include <dev/usb/usb_quirks.h> 66 67 #include <dev/usb/usbdevs.h> 68 #include <dev/usb/ucomvar.h> 69 70 #ifdef UPLCOM_DEBUG 71 #define DPRINTFN(n, x) if (uplcomdebug > (n)) logprintf x 72 int uplcomdebug = 0; 73 #else 74 #define DPRINTFN(n, x) 75 #endif 76 #define DPRINTF(x) DPRINTFN(0, x) 77 78 #define UPLCOM_CONFIG_INDEX 0 79 #define UPLCOM_IFACE_INDEX 0 80 #define UPLCOM_SECOND_IFACE_INDEX 1 81 82 #define UPLCOM_SET_REQUEST 0x01 83 #define UPLCOM_SET_CRTSCTS_0 0x41 84 #define UPLCOM_SET_CRTSCTS_HX 0x61 85 #define RSAQ_STATUS_DSR 0x02 86 #define RSAQ_STATUS_DCD 0x01 87 88 enum pl2303_type { 89 UPLCOM_TYPE_0, 90 UPLCOM_TYPE_HX, 91 }; 92 93 struct uplcom_softc { 94 USBBASEDEVICE sc_dev; /* base device */ 95 usbd_device_handle sc_udev; /* USB device */ 96 usbd_interface_handle sc_iface; /* interface */ 97 int sc_iface_number; /* interface number */ 98 99 usbd_interface_handle sc_intr_iface; /* interrupt interface */ 100 int sc_intr_number; /* interrupt number */ 101 usbd_pipe_handle sc_intr_pipe; /* interrupt pipe */ 102 u_char *sc_intr_buf; /* interrupt buffer */ 103 int sc_isize; 104 105 usb_cdc_line_state_t sc_line_state; /* current line state */ 106 int sc_dtr; /* current DTR state */ 107 int sc_rts; /* current RTS state */ 108 109 device_ptr_t sc_subdev; /* ucom device */ 110 111 u_char sc_dying; /* disconnecting */ 112 113 u_char sc_lsr; /* Local status register */ 114 u_char sc_msr; /* uplcom status register */ 115 116 enum pl2303_type sc_type; /* PL2303 chip type */ 117 }; 118 119 /* 120 * These are the maximum number of bytes transferred per frame. 121 * The output buffer size cannot be increased due to the size encoding. 122 */ 123 #define UPLCOMIBUFSIZE 256 124 #define UPLCOMOBUFSIZE 256 125 126 Static usbd_status uplcom_reset(struct uplcom_softc *); 127 Static usbd_status uplcom_set_line_coding(struct uplcom_softc *sc, 128 usb_cdc_line_state_t *state); 129 Static usbd_status uplcom_set_crtscts(struct uplcom_softc *); 130 Static void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status); 131 132 Static void uplcom_set(void *, int, int, int); 133 Static void uplcom_dtr(struct uplcom_softc *, int); 134 Static void uplcom_rts(struct uplcom_softc *, int); 135 Static void uplcom_break(struct uplcom_softc *, int); 136 Static void uplcom_set_line_state(struct uplcom_softc *); 137 Static void uplcom_get_status(void *, int portno, u_char *lsr, u_char *msr); 138 #if TODO 139 Static int uplcom_ioctl(void *, int, u_long, caddr_t, int, usb_proc_ptr ); 140 #endif 141 Static int uplcom_param(void *, int, struct termios *); 142 Static int uplcom_open(void *, int); 143 Static void uplcom_close(void *, int); 144 Static usbd_status uplcom_vendor_control_write(usbd_device_handle, u_int16_t, u_int16_t); 145 146 struct ucom_methods uplcom_methods = { 147 uplcom_get_status, 148 uplcom_set, 149 uplcom_param, 150 NULL, /* uplcom_ioctl, TODO */ 151 uplcom_open, 152 uplcom_close, 153 NULL, 154 NULL, 155 }; 156 157 static const struct usb_devno uplcom_devs[] = { 158 /* I/O DATA USB-RSAQ2 */ 159 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 }, 160 /* I/O DATA USB-RSAQ */ 161 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ }, 162 /* PLANEX USB-RS232 URS-03 */ 163 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A }, 164 /* IOGEAR/ATEN UC-232A */ 165 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 }, 166 /* IOGEAR/ATENTRIPPLITE */ 167 { USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 }, 168 /* ELECOM UC-SGT */ 169 { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT }, 170 /* ELECOM UC-SGT0 */ 171 { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 }, 172 /* Panasonic 50" Touch Panel */ 173 { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_TYTP50P6S }, 174 /* RATOC REX-USB60 */ 175 { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 }, 176 /* TDK USB-PHS Adapter UHA6400 */ 177 { USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 }, 178 /* TDK USB-PDC Adapter UPA9664 */ 179 { USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 }, 180 /* Sony Ericsson USB Cable */ 181 { USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU10 }, 182 /* SOURCENEXT KeikaiDenwa 8 */ 183 { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 }, 184 /* SOURCENEXT KeikaiDenwa 8 with charger */ 185 { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG }, 186 /* HAL Corporation Crossam2+USB */ 187 { USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 }, 188 /* Sitecom USB to serial cable */ 189 { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 }, 190 /* Pharos USB GPS - Microsoft version */ 191 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X }, 192 }; 193 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p) 194 195 196 USB_DECLARE_DRIVER(uplcom); 197 198 USB_MATCH(uplcom) 199 { 200 USB_MATCH_START(uplcom, uaa); 201 202 if (uaa->iface != NULL) 203 return (UMATCH_NONE); 204 205 return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ? 206 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 207 } 208 209 USB_ATTACH(uplcom) 210 { 211 USB_ATTACH_START(uplcom, sc, uaa); 212 usbd_device_handle dev = uaa->device; 213 usb_device_descriptor_t *ddesc; 214 usb_config_descriptor_t *cdesc; 215 usb_interface_descriptor_t *id; 216 usb_endpoint_descriptor_t *ed; 217 char *devinfop; 218 char *devname = USBDEVNAME(sc->sc_dev); 219 usbd_status err; 220 int i; 221 struct ucom_attach_args uca; 222 223 devinfop = usbd_devinfo_alloc(dev, 0); 224 USB_ATTACH_SETUP; 225 printf("%s: %s\n", devname, devinfop); 226 usbd_devinfo_free(devinfop); 227 228 sc->sc_udev = dev; 229 230 DPRINTF(("\n\nuplcom attach: sc=%p\n", sc)); 231 232 /* initialize endpoints */ 233 uca.bulkin = uca.bulkout = -1; 234 sc->sc_intr_number = -1; 235 sc->sc_intr_pipe = NULL; 236 237 /* Move the device into the configured state. */ 238 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1); 239 if (err) { 240 printf("\n%s: failed to set configuration, err=%s\n", 241 devname, usbd_errstr(err)); 242 sc->sc_dying = 1; 243 USB_ATTACH_ERROR_RETURN; 244 } 245 246 /* get the device descriptor */ 247 ddesc = usbd_get_device_descriptor(sc->sc_udev); 248 if (ddesc == NULL) { 249 printf("%s: failed to get device descriptor\n", 250 USBDEVNAME(sc->sc_dev)); 251 sc->sc_dying = 1; 252 USB_ATTACH_ERROR_RETURN; 253 } 254 255 /* 256 * NOTE: The Linux driver distinguishes between UPLCOM_TYPE_0 257 * and UPLCOM_TYPE_1 type chips by testing other fields in the 258 * device descriptor. As far as the uplcom driver is 259 * concerned, both types are identical. 260 * The bcdDevice field should also distinguish these versions, 261 * but who knows. 262 */ 263 if (UGETW(ddesc->bcdDevice) == 0x0300) 264 sc->sc_type = UPLCOM_TYPE_HX; 265 else 266 sc->sc_type = UPLCOM_TYPE_0; 267 268 /* get the config descriptor */ 269 cdesc = usbd_get_config_descriptor(sc->sc_udev); 270 271 if (cdesc == NULL) { 272 printf("%s: failed to get configuration descriptor\n", 273 USBDEVNAME(sc->sc_dev)); 274 sc->sc_dying = 1; 275 USB_ATTACH_ERROR_RETURN; 276 } 277 278 /* get the (first/common) interface */ 279 err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX, 280 &sc->sc_iface); 281 if (err) { 282 printf("\n%s: failed to get interface, err=%s\n", 283 devname, usbd_errstr(err)); 284 sc->sc_dying = 1; 285 USB_ATTACH_ERROR_RETURN; 286 } 287 288 /* Find the interrupt endpoints */ 289 290 id = usbd_get_interface_descriptor(sc->sc_iface); 291 sc->sc_iface_number = id->bInterfaceNumber; 292 293 for (i = 0; i < id->bNumEndpoints; i++) { 294 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 295 if (ed == NULL) { 296 printf("%s: no endpoint descriptor for %d\n", 297 USBDEVNAME(sc->sc_dev), i); 298 sc->sc_dying = 1; 299 USB_ATTACH_ERROR_RETURN; 300 } 301 302 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 303 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 304 sc->sc_intr_number = ed->bEndpointAddress; 305 sc->sc_isize = UGETW(ed->wMaxPacketSize); 306 } 307 } 308 309 if (sc->sc_intr_number== -1) { 310 printf("%s: Could not find interrupt in\n", 311 USBDEVNAME(sc->sc_dev)); 312 sc->sc_dying = 1; 313 USB_ATTACH_ERROR_RETURN; 314 } 315 316 /* keep interface for interrupt */ 317 sc->sc_intr_iface = sc->sc_iface; 318 319 /* 320 * USB-RSAQ1 has two interface 321 * 322 * USB-RSAQ1 | USB-RSAQ2 323 * -----------------+----------------- 324 * Interface 0 |Interface 0 325 * Interrupt(0x81) | Interrupt(0x81) 326 * -----------------+ BulkIN(0x02) 327 * Interface 1 | BulkOUT(0x83) 328 * BulkIN(0x02) | 329 * BulkOUT(0x83) | 330 */ 331 if (cdesc->bNumInterface == 2) { 332 err = usbd_device2interface_handle(dev, 333 UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface); 334 if (err) { 335 printf("\n%s: failed to get second interface, err=%s\n", 336 devname, usbd_errstr(err)); 337 sc->sc_dying = 1; 338 USB_ATTACH_ERROR_RETURN; 339 } 340 } 341 342 /* Find the bulk{in,out} endpoints */ 343 344 id = usbd_get_interface_descriptor(sc->sc_iface); 345 sc->sc_iface_number = id->bInterfaceNumber; 346 347 for (i = 0; i < id->bNumEndpoints; i++) { 348 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 349 if (ed == NULL) { 350 printf("%s: no endpoint descriptor for %d\n", 351 USBDEVNAME(sc->sc_dev), i); 352 sc->sc_dying = 1; 353 USB_ATTACH_ERROR_RETURN; 354 } 355 356 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 357 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 358 uca.bulkin = ed->bEndpointAddress; 359 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 360 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 361 uca.bulkout = ed->bEndpointAddress; 362 } 363 } 364 365 if (uca.bulkin == -1) { 366 printf("%s: Could not find data bulk in\n", 367 USBDEVNAME(sc->sc_dev)); 368 sc->sc_dying = 1; 369 USB_ATTACH_ERROR_RETURN; 370 } 371 372 if (uca.bulkout == -1) { 373 printf("%s: Could not find data bulk out\n", 374 USBDEVNAME(sc->sc_dev)); 375 sc->sc_dying = 1; 376 USB_ATTACH_ERROR_RETURN; 377 } 378 379 sc->sc_dtr = sc->sc_rts = -1; 380 uca.portno = UCOM_UNK_PORTNO; 381 /* bulkin, bulkout set above */ 382 uca.ibufsize = UPLCOMIBUFSIZE; 383 uca.obufsize = UPLCOMOBUFSIZE; 384 uca.ibufsizepad = UPLCOMIBUFSIZE; 385 uca.opkthdrlen = 0; 386 uca.device = dev; 387 uca.iface = sc->sc_iface; 388 uca.methods = &uplcom_methods; 389 uca.arg = sc; 390 uca.info = NULL; 391 392 err = uplcom_reset(sc); 393 394 if (err) { 395 printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev), 396 usbd_errstr(err)); 397 sc->sc_dying = 1; 398 USB_ATTACH_ERROR_RETURN; 399 } 400 401 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 402 USBDEV(sc->sc_dev)); 403 404 DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n", 405 uca.bulkin, uca.bulkout, sc->sc_intr_number )); 406 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca, 407 ucomprint, ucomsubmatch); 408 409 USB_ATTACH_SUCCESS_RETURN; 410 } 411 412 USB_DETACH(uplcom) 413 { 414 USB_DETACH_START(uplcom, sc); 415 int rv = 0; 416 417 DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags)); 418 419 if (sc->sc_intr_pipe != NULL) { 420 usbd_abort_pipe(sc->sc_intr_pipe); 421 usbd_close_pipe(sc->sc_intr_pipe); 422 free(sc->sc_intr_buf, M_USBDEV); 423 sc->sc_intr_pipe = NULL; 424 } 425 426 sc->sc_dying = 1; 427 if (sc->sc_subdev != NULL) { 428 rv = config_detach(sc->sc_subdev, flags); 429 sc->sc_subdev = NULL; 430 } 431 432 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 433 USBDEV(sc->sc_dev)); 434 435 return (rv); 436 } 437 438 int 439 uplcom_activate(device_ptr_t self, enum devact act) 440 { 441 struct uplcom_softc *sc = (struct uplcom_softc *)self; 442 int rv = 0; 443 444 switch (act) { 445 case DVACT_ACTIVATE: 446 return (EOPNOTSUPP); 447 448 case DVACT_DEACTIVATE: 449 if (sc->sc_subdev != NULL) 450 rv = config_deactivate(sc->sc_subdev); 451 sc->sc_dying = 1; 452 break; 453 } 454 return (rv); 455 } 456 457 usbd_status 458 uplcom_reset(struct uplcom_softc *sc) 459 { 460 usb_device_request_t req; 461 usbd_status err; 462 463 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 464 req.bRequest = UPLCOM_SET_REQUEST; 465 USETW(req.wValue, 0); 466 USETW(req.wIndex, sc->sc_iface_number); 467 USETW(req.wLength, 0); 468 469 err = usbd_do_request(sc->sc_udev, &req, 0); 470 if (err) 471 return (EIO); 472 473 return (0); 474 } 475 476 void 477 uplcom_set_line_state(struct uplcom_softc *sc) 478 { 479 usb_device_request_t req; 480 int ls; 481 482 /* make sure we have initialized state for sc_dtr and sc_rts */ 483 if (sc->sc_dtr == -1) 484 sc->sc_dtr = 0; 485 if (sc->sc_rts == -1) 486 sc->sc_rts = 0; 487 488 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) | 489 (sc->sc_rts ? UCDC_LINE_RTS : 0); 490 491 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 492 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 493 USETW(req.wValue, ls); 494 USETW(req.wIndex, sc->sc_iface_number); 495 USETW(req.wLength, 0); 496 497 (void)usbd_do_request(sc->sc_udev, &req, 0); 498 } 499 500 void 501 uplcom_set(void *addr, int portno, int reg, int onoff) 502 { 503 struct uplcom_softc *sc = addr; 504 505 switch (reg) { 506 case UCOM_SET_DTR: 507 uplcom_dtr(sc, onoff); 508 break; 509 case UCOM_SET_RTS: 510 uplcom_rts(sc, onoff); 511 break; 512 case UCOM_SET_BREAK: 513 uplcom_break(sc, onoff); 514 break; 515 default: 516 break; 517 } 518 } 519 520 void 521 uplcom_dtr(struct uplcom_softc *sc, int onoff) 522 { 523 524 DPRINTF(("uplcom_dtr: onoff=%d\n", onoff)); 525 526 if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff) 527 return; 528 529 sc->sc_dtr = !!onoff; 530 531 uplcom_set_line_state(sc); 532 } 533 534 void 535 uplcom_rts(struct uplcom_softc *sc, int onoff) 536 { 537 DPRINTF(("uplcom_rts: onoff=%d\n", onoff)); 538 539 if (sc->sc_rts != -1 && !sc->sc_rts == !onoff) 540 return; 541 542 sc->sc_rts = !!onoff; 543 544 uplcom_set_line_state(sc); 545 } 546 547 void 548 uplcom_break(struct uplcom_softc *sc, int onoff) 549 { 550 usb_device_request_t req; 551 552 DPRINTF(("uplcom_break: onoff=%d\n", onoff)); 553 554 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 555 req.bRequest = UCDC_SEND_BREAK; 556 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF); 557 USETW(req.wIndex, sc->sc_iface_number); 558 USETW(req.wLength, 0); 559 560 (void)usbd_do_request(sc->sc_udev, &req, 0); 561 } 562 563 usbd_status 564 uplcom_set_crtscts(struct uplcom_softc *sc) 565 { 566 usb_device_request_t req; 567 usbd_status err; 568 569 DPRINTF(("uplcom_set_crtscts: on\n")); 570 571 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 572 req.bRequest = UPLCOM_SET_REQUEST; 573 USETW(req.wValue, 0); 574 if (sc->sc_type == UPLCOM_TYPE_HX) 575 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_HX); 576 else 577 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_0); 578 USETW(req.wLength, 0); 579 580 err = usbd_do_request(sc->sc_udev, &req, 0); 581 if (err) { 582 DPRINTF(("uplcom_set_crtscts: failed, err=%s\n", 583 usbd_errstr(err))); 584 return (err); 585 } 586 587 return (USBD_NORMAL_COMPLETION); 588 } 589 590 usbd_status 591 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state) 592 { 593 usb_device_request_t req; 594 usbd_status err; 595 596 DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n", 597 UGETDW(state->dwDTERate), state->bCharFormat, 598 state->bParityType, state->bDataBits)); 599 600 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) { 601 DPRINTF(("uplcom_set_line_coding: already set\n")); 602 return (USBD_NORMAL_COMPLETION); 603 } 604 605 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 606 req.bRequest = UCDC_SET_LINE_CODING; 607 USETW(req.wValue, 0); 608 USETW(req.wIndex, sc->sc_iface_number); 609 USETW(req.wLength, UCDC_LINE_STATE_LENGTH); 610 611 err = usbd_do_request(sc->sc_udev, &req, state); 612 if (err) { 613 DPRINTF(("uplcom_set_line_coding: failed, err=%s\n", 614 usbd_errstr(err))); 615 return (err); 616 } 617 618 sc->sc_line_state = *state; 619 620 return (USBD_NORMAL_COMPLETION); 621 } 622 623 int 624 uplcom_param(void *addr, int portno, struct termios *t) 625 { 626 struct uplcom_softc *sc = addr; 627 usbd_status err; 628 usb_cdc_line_state_t ls; 629 630 DPRINTF(("uplcom_param: sc=%p\n", sc)); 631 632 USETDW(ls.dwDTERate, t->c_ospeed); 633 if (ISSET(t->c_cflag, CSTOPB)) 634 ls.bCharFormat = UCDC_STOP_BIT_2; 635 else 636 ls.bCharFormat = UCDC_STOP_BIT_1; 637 if (ISSET(t->c_cflag, PARENB)) { 638 if (ISSET(t->c_cflag, PARODD)) 639 ls.bParityType = UCDC_PARITY_ODD; 640 else 641 ls.bParityType = UCDC_PARITY_EVEN; 642 } else 643 ls.bParityType = UCDC_PARITY_NONE; 644 switch (ISSET(t->c_cflag, CSIZE)) { 645 case CS5: 646 ls.bDataBits = 5; 647 break; 648 case CS6: 649 ls.bDataBits = 6; 650 break; 651 case CS7: 652 ls.bDataBits = 7; 653 break; 654 case CS8: 655 ls.bDataBits = 8; 656 break; 657 } 658 659 err = uplcom_set_line_coding(sc, &ls); 660 if (err) { 661 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err))); 662 return (EIO); 663 } 664 665 if (ISSET(t->c_cflag, CRTSCTS)) 666 uplcom_set_crtscts(sc); 667 668 if (sc->sc_rts == -1 || sc->sc_dtr == -1) 669 uplcom_set_line_state(sc); 670 671 if (err) { 672 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err))); 673 return (EIO); 674 } 675 676 return (0); 677 } 678 679 Static usbd_status 680 uplcom_vendor_control_write(usbd_device_handle dev, u_int16_t value, u_int16_t index) 681 { 682 usb_device_request_t req; 683 usbd_status err; 684 685 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 686 req.bRequest = UPLCOM_SET_REQUEST; 687 USETW(req.wValue, value); 688 USETW(req.wIndex, index); 689 USETW(req.wLength, 0); 690 691 err = usbd_do_request(dev, &req, NULL); 692 693 if (err) { 694 DPRINTF(("uplcom_open: vendor write failed, err=%s (%d)\n", 695 usbd_errstr(err), err)); 696 } 697 698 return err; 699 } 700 701 int 702 uplcom_open(void *addr, int portno) 703 { 704 struct uplcom_softc *sc = addr; 705 usbd_status err; 706 707 if (sc->sc_dying) 708 return (EIO); 709 710 DPRINTF(("uplcom_open: sc=%p\n", sc)); 711 712 /* Some unknown device frobbing. */ 713 if (sc->sc_type == UPLCOM_TYPE_HX) 714 uplcom_vendor_control_write(sc->sc_udev, 2, 0x44); 715 else 716 uplcom_vendor_control_write(sc->sc_udev, 2, 0x24); 717 718 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) { 719 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK); 720 err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number, 721 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, 722 sc->sc_intr_buf, sc->sc_isize, 723 uplcom_intr, USBD_DEFAULT_INTERVAL); 724 if (err) { 725 DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n", 726 USBDEVNAME(sc->sc_dev), sc->sc_intr_number)); 727 return (EIO); 728 } 729 } 730 731 return (0); 732 } 733 734 void 735 uplcom_close(void *addr, int portno) 736 { 737 struct uplcom_softc *sc = addr; 738 int err; 739 740 if (sc->sc_dying) 741 return; 742 743 DPRINTF(("uplcom_close: close\n")); 744 745 if (sc->sc_intr_pipe != NULL) { 746 err = usbd_abort_pipe(sc->sc_intr_pipe); 747 if (err) 748 printf("%s: abort interrupt pipe failed: %s\n", 749 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 750 err = usbd_close_pipe(sc->sc_intr_pipe); 751 if (err) 752 printf("%s: close interrupt pipe failed: %s\n", 753 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 754 free(sc->sc_intr_buf, M_USBDEV); 755 sc->sc_intr_pipe = NULL; 756 } 757 } 758 759 void 760 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 761 { 762 struct uplcom_softc *sc = priv; 763 u_char *buf = sc->sc_intr_buf; 764 u_char pstatus; 765 766 if (sc->sc_dying) 767 return; 768 769 if (status != USBD_NORMAL_COMPLETION) { 770 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 771 return; 772 773 DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev), 774 usbd_errstr(status))); 775 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe); 776 return; 777 } 778 779 DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc->sc_dev), buf[8])); 780 781 sc->sc_lsr = sc->sc_msr = 0; 782 pstatus = buf[8]; 783 if (ISSET(pstatus, RSAQ_STATUS_DSR)) 784 sc->sc_msr |= UMSR_DSR; 785 if (ISSET(pstatus, RSAQ_STATUS_DCD)) 786 sc->sc_msr |= UMSR_DCD; 787 ucom_status_change((struct ucom_softc *) sc->sc_subdev); 788 } 789 790 void 791 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr) 792 { 793 struct uplcom_softc *sc = addr; 794 795 DPRINTF(("uplcom_get_status:\n")); 796 797 if (lsr != NULL) 798 *lsr = sc->sc_lsr; 799 if (msr != NULL) 800 *msr = sc->sc_msr; 801 } 802 803 #if TODO 804 int 805 uplcom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag, 806 usb_proc_ptr p) 807 { 808 struct uplcom_softc *sc = addr; 809 int error = 0; 810 811 if (sc->sc_dying) 812 return (EIO); 813 814 DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd)); 815 816 switch (cmd) { 817 case TIOCNOTTY: 818 case TIOCMGET: 819 case TIOCMSET: 820 case USB_GET_CM_OVER_DATA: 821 case USB_SET_CM_OVER_DATA: 822 break; 823 824 default: 825 DPRINTF(("uplcom_ioctl: unknown\n")); 826 error = ENOTTY; 827 break; 828 } 829 830 return (error); 831 } 832 #endif 833