1 /* $OpenBSD: uplcom.c,v 1.78 2021/10/29 14:25:05 jsg Exp $ */ 2 /* $NetBSD: uplcom.c,v 1.29 2002/09/23 05:51:23 simonb Exp $ */ 3 /* 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Ichiro FUKUHARA (ichiro@ichiro.org). 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Simple datasheet 34 * http://www.prolific.com.tw/PDF/PL-2303%20Market%20Spec.pdf 35 * http://www.hitachi-hitec.com/jyouhou/prolific/2303.pdf 36 * (english) 37 * 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/malloc.h> 44 #include <sys/ioctl.h> 45 #include <sys/conf.h> 46 #include <sys/tty.h> 47 #include <sys/selinfo.h> 48 #include <sys/device.h> 49 #include <sys/poll.h> 50 51 #include <dev/usb/usb.h> 52 #include <dev/usb/usbcdc.h> 53 54 #include <dev/usb/usbdi.h> 55 #include <dev/usb/usbdi_util.h> 56 #include <dev/usb/usbdevs.h> 57 58 #include <dev/usb/ucomvar.h> 59 60 #ifdef UPLCOM_DEBUG 61 #define DPRINTFN(n, x) do { if (uplcomdebug > (n)) printf x; } while (0) 62 int uplcomdebug = 0; 63 #else 64 #define DPRINTFN(n, x) 65 #endif 66 #define DPRINTF(x) DPRINTFN(0, x) 67 68 #define UPLCOM_IFACE_INDEX 0 69 #define UPLCOM_SECOND_IFACE_INDEX 1 70 71 #define UPLCOM_SET_REQUEST 0x01 72 #define UPLCOM_HXN_SET_REQUEST 0x80 73 #define UPLCOM_HXN_SET_CRTSCTS_REG 0x0A 74 #define UPLCOM_SET_CRTSCTS 0x41 75 #define UPLCOM_HX_SET_CRTSCTS 0x61 76 #define UPLCOM_HXN_SET_CRTSCTS 0xFA 77 #define UPLCOM_HX_STATUS_REG 0x8080 78 #define RSAQ_STATUS_CTS 0x80 79 #define RSAQ_STATUS_DSR 0x02 80 #define RSAQ_STATUS_DCD 0x01 81 82 #define UPLCOM_TYPE_01 0 83 #define UPLCOM_TYPE_HX 1 84 #define UPLCOM_TYPE_HXN 2 85 86 struct uplcom_softc { 87 struct device sc_dev; /* base device */ 88 struct usbd_device *sc_udev; /* USB device */ 89 struct usbd_interface *sc_iface; /* interface */ 90 int sc_iface_number; /* interface number */ 91 92 struct usbd_interface *sc_intr_iface; /* interrupt interface */ 93 int sc_intr_number; /* interrupt number */ 94 struct usbd_pipe *sc_intr_pipe; /* interrupt pipe */ 95 u_char *sc_intr_buf; /* interrupt buffer */ 96 int sc_isize; 97 98 struct usb_cdc_line_state sc_line_state;/* current line state */ 99 int sc_dtr; /* current DTR state */ 100 int sc_rts; /* current RTS state */ 101 102 struct device *sc_subdev; /* ucom device */ 103 104 u_char sc_lsr; /* Local status register */ 105 u_char sc_msr; /* uplcom status register */ 106 int sc_type; /* variant */ 107 }; 108 109 /* 110 * These are the maximum number of bytes transferred per frame. 111 * The output buffer size cannot be increased due to the size encoding. 112 */ 113 #define UPLCOMIBUFSIZE 256 114 #define UPLCOMOBUFSIZE 256 115 116 usbd_status uplcom_reset(struct uplcom_softc *); 117 usbd_status uplcom_set_line_coding(struct uplcom_softc *sc, 118 struct usb_cdc_line_state *state); 119 usbd_status uplcom_set_crtscts(struct uplcom_softc *); 120 void uplcom_intr(struct usbd_xfer *, void *, usbd_status); 121 122 void uplcom_set(void *, int, int, int); 123 void uplcom_dtr(struct uplcom_softc *, int); 124 void uplcom_rts(struct uplcom_softc *, int); 125 void uplcom_break(struct uplcom_softc *, int); 126 void uplcom_set_line_state(struct uplcom_softc *); 127 void uplcom_get_status(void *, int portno, u_char *lsr, u_char *msr); 128 int uplcom_param(void *, int, struct termios *); 129 int uplcom_open(void *, int); 130 void uplcom_close(void *, int); 131 132 struct ucom_methods uplcom_methods = { 133 uplcom_get_status, 134 uplcom_set, 135 uplcom_param, 136 NULL, 137 uplcom_open, 138 uplcom_close, 139 NULL, 140 NULL, 141 }; 142 143 static const struct usb_devno uplcom_devs[] = { 144 { USB_VENDOR_ALCATEL, USB_PRODUCT_ALCATEL_OT535 }, 145 { USB_VENDOR_ANCHOR, USB_PRODUCT_ANCHOR_SERIAL }, 146 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A }, 147 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U257 }, 148 { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT }, 149 { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 }, 150 { USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 }, 151 { USB_VENDOR_HP, USB_PRODUCT_HP_LD220 }, 152 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ }, 153 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ5 }, 154 { USB_VENDOR_LEADTEK, USB_PRODUCT_LEADTEK_9531 }, 155 { USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_700WX }, 156 { USB_VENDOR_MOBILEACTION, USB_PRODUCT_MOBILEACTION_MA620 }, 157 { USB_VENDOR_NOKIA, USB_PRODUCT_NOKIA_CA42 }, 158 { USB_VENDOR_OTI, USB_PRODUCT_OTI_DKU5 }, 159 { USB_VENDOR_PLX, USB_PRODUCT_PLX_CA42 }, 160 { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_TYTP50P6S }, 161 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 }, 162 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GB }, 163 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GC }, 164 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GE }, 165 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GL }, 166 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GS }, 167 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GT }, 168 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X }, 169 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X2 }, 170 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 }, 171 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303BENQ }, 172 { USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_PL2303 }, 173 { USB_VENDOR_RADIOSHACK, USB_PRODUCT_RADIOSHACK_PL2303 }, 174 { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 }, 175 { USB_VENDOR_SAGEM, USB_PRODUCT_SAGEM_SERIAL }, 176 { USB_VENDOR_SIEMENS3, USB_PRODUCT_SIEMENS3_SX1 }, 177 { USB_VENDOR_SIEMENS3, USB_PRODUCT_SIEMENS3_X65 }, 178 { USB_VENDOR_SIEMENS3, USB_PRODUCT_SIEMENS3_X75 }, 179 { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 }, 180 { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 }, 181 { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG }, 182 { USB_VENDOR_SPEEDDRAGON, USB_PRODUCT_SPEEDDRAGON_MS3303H }, 183 { USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU11 }, 184 { USB_VENDOR_SYNTECH, USB_PRODUCT_SYNTECH_SERIAL }, 185 { USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 }, 186 { USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 }, 187 { USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 }, 188 { USB_VENDOR_SMART, USB_PRODUCT_SMART_PL2303 }, 189 { USB_VENDOR_YCCABLE, USB_PRODUCT_YCCABLE_PL2303 } 190 }; 191 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p) 192 193 int uplcom_match(struct device *, void *, void *); 194 void uplcom_attach(struct device *, struct device *, void *); 195 int uplcom_detach(struct device *, int); 196 197 struct cfdriver uplcom_cd = { 198 NULL, "uplcom", DV_DULL 199 }; 200 201 const struct cfattach uplcom_ca = { 202 sizeof(struct uplcom_softc), uplcom_match, uplcom_attach, uplcom_detach 203 }; 204 205 int 206 uplcom_match(struct device *parent, void *match, void *aux) 207 { 208 struct usb_attach_arg *uaa = aux; 209 210 if (uaa->iface == NULL) 211 return (UMATCH_NONE); 212 213 return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ? 214 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 215 } 216 217 void 218 uplcom_attach(struct device *parent, struct device *self, void *aux) 219 { 220 struct uplcom_softc *sc = (struct uplcom_softc *)self; 221 struct usb_attach_arg *uaa = aux; 222 struct usbd_device *dev = uaa->device; 223 usb_config_descriptor_t *cdesc; 224 usb_device_descriptor_t *ddesc; 225 usb_interface_descriptor_t *id; 226 usb_endpoint_descriptor_t *ed; 227 char *devname = sc->sc_dev.dv_xname; 228 usb_device_request_t req; 229 usbd_status err; 230 uByte val; 231 int i; 232 struct ucom_attach_args uca; 233 234 sc->sc_udev = dev; 235 236 DPRINTF(("\n\nuplcom attach: sc=%p\n", sc)); 237 238 /* initialize endpoints */ 239 uca.bulkin = uca.bulkout = -1; 240 sc->sc_intr_number = -1; 241 sc->sc_intr_pipe = NULL; 242 243 /* get the config descriptor */ 244 cdesc = usbd_get_config_descriptor(sc->sc_udev); 245 246 if (cdesc == NULL) { 247 printf("%s: failed to get configuration descriptor\n", 248 sc->sc_dev.dv_xname); 249 usbd_deactivate(sc->sc_udev); 250 return; 251 } 252 253 /* get the device descriptor */ 254 ddesc = usbd_get_device_descriptor(sc->sc_udev); 255 if (ddesc == NULL) { 256 printf("%s: failed to get device descriptor\n", 257 sc->sc_dev.dv_xname); 258 usbd_deactivate(sc->sc_udev); 259 return; 260 } 261 262 /* 263 * The Linux driver suggest this will only be true for the HX 264 * variants. The datasheets disagree. 265 */ 266 if (ddesc->bDeviceClass == 0x02) 267 sc->sc_type = UPLCOM_TYPE_01; 268 else if (ddesc->bMaxPacketSize == 0x40) 269 sc->sc_type = UPLCOM_TYPE_HX; 270 else 271 sc->sc_type = UPLCOM_TYPE_01; 272 273 if (sc->sc_type == UPLCOM_TYPE_HX) { 274 req.bmRequestType = UT_READ_VENDOR_DEVICE; 275 req.bRequest = UPLCOM_SET_REQUEST; 276 USETW(req.wValue, UPLCOM_HX_STATUS_REG); 277 USETW(req.wIndex, sc->sc_iface_number); 278 USETW(req.wLength, 1); 279 err = usbd_do_request(sc->sc_udev, &req, &val); 280 if (err) 281 sc->sc_type = UPLCOM_TYPE_HXN; 282 } 283 284 #ifdef UPLCOM_DEBUG 285 /* print the chip type */ 286 if (sc->sc_type == UPLCOM_TYPE_HXN) { 287 DPRINTF(("uplcom_attach: chiptype 2303XN\n")); 288 } else if (sc->sc_type == UPLCOM_TYPE_HX) { 289 DPRINTF(("uplcom_attach: chiptype 2303X\n")); 290 } else { 291 DPRINTF(("uplcom_attach: chiptype 2303\n")); 292 } 293 #endif 294 /* get the (first/common) interface */ 295 err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX, 296 &sc->sc_iface); 297 if (err) { 298 printf("\n%s: failed to get interface, err=%s\n", 299 devname, usbd_errstr(err)); 300 usbd_deactivate(sc->sc_udev); 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 printf("%s: no endpoint descriptor for %d\n", 313 sc->sc_dev.dv_xname, i); 314 usbd_deactivate(sc->sc_udev); 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 printf("%s: Could not find interrupt in\n", 327 sc->sc_dev.dv_xname); 328 usbd_deactivate(sc->sc_udev); 329 return; 330 } 331 332 /* keep interface for interrupt */ 333 sc->sc_intr_iface = sc->sc_iface; 334 335 /* 336 * USB-RSAQ1 has two interface 337 * 338 * USB-RSAQ1 | USB-RSAQ2 339 * -----------------+----------------- 340 * Interface 0 |Interface 0 341 * Interrupt(0x81) | Interrupt(0x81) 342 * -----------------+ BulkIN(0x02) 343 * Interface 1 | BulkOUT(0x83) 344 * BulkIN(0x02) | 345 * BulkOUT(0x83) | 346 */ 347 if (cdesc->bNumInterfaces == 2) { 348 err = usbd_device2interface_handle(dev, 349 UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface); 350 if (err) { 351 printf("\n%s: failed to get second interface, err=%s\n", 352 devname, usbd_errstr(err)); 353 usbd_deactivate(sc->sc_udev); 354 return; 355 } 356 } 357 358 /* Find the bulk{in,out} endpoints */ 359 360 id = usbd_get_interface_descriptor(sc->sc_iface); 361 sc->sc_iface_number = id->bInterfaceNumber; 362 363 for (i = 0; i < id->bNumEndpoints; i++) { 364 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 365 if (ed == NULL) { 366 printf("%s: no endpoint descriptor for %d\n", 367 sc->sc_dev.dv_xname, i); 368 usbd_deactivate(sc->sc_udev); 369 return; 370 } 371 372 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 373 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 374 uca.bulkin = ed->bEndpointAddress; 375 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 376 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 377 uca.bulkout = ed->bEndpointAddress; 378 } 379 } 380 381 if (uca.bulkin == -1) { 382 printf("%s: Could not find data bulk in\n", 383 sc->sc_dev.dv_xname); 384 usbd_deactivate(sc->sc_udev); 385 return; 386 } 387 388 if (uca.bulkout == -1) { 389 printf("%s: Could not find data bulk out\n", 390 sc->sc_dev.dv_xname); 391 usbd_deactivate(sc->sc_udev); 392 return; 393 } 394 395 sc->sc_dtr = sc->sc_rts = -1; 396 uca.portno = UCOM_UNK_PORTNO; 397 /* bulkin, bulkout set above */ 398 uca.ibufsize = UPLCOMIBUFSIZE; 399 uca.obufsize = UPLCOMOBUFSIZE; 400 uca.ibufsizepad = UPLCOMIBUFSIZE; 401 uca.opkthdrlen = 0; 402 uca.device = dev; 403 uca.iface = sc->sc_iface; 404 uca.methods = &uplcom_methods; 405 uca.arg = sc; 406 uca.info = NULL; 407 408 if (sc->sc_type != UPLCOM_TYPE_HXN) { 409 err = uplcom_reset(sc); 410 if (err) { 411 printf("%s: reset failed, %s\n", sc->sc_dev.dv_xname, 412 usbd_errstr(err)); 413 usbd_deactivate(sc->sc_udev); 414 return; 415 } 416 } 417 418 DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n", 419 uca.bulkin, uca.bulkout, sc->sc_intr_number )); 420 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch); 421 } 422 423 int 424 uplcom_detach(struct device *self, int flags) 425 { 426 struct uplcom_softc *sc = (struct uplcom_softc *)self; 427 int rv = 0; 428 429 DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags)); 430 431 if (sc->sc_intr_pipe != NULL) { 432 usbd_close_pipe(sc->sc_intr_pipe); 433 free(sc->sc_intr_buf, M_USBDEV, sc->sc_isize); 434 sc->sc_intr_pipe = NULL; 435 } 436 437 if (sc->sc_subdev != NULL) { 438 rv = config_detach(sc->sc_subdev, flags); 439 sc->sc_subdev = NULL; 440 } 441 442 return (rv); 443 } 444 445 usbd_status 446 uplcom_reset(struct uplcom_softc *sc) 447 { 448 usb_device_request_t req; 449 usbd_status err; 450 451 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 452 req.bRequest = UPLCOM_SET_REQUEST; 453 USETW(req.wValue, 0); 454 USETW(req.wIndex, sc->sc_iface_number); 455 USETW(req.wLength, 0); 456 457 err = usbd_do_request(sc->sc_udev, &req, 0); 458 if (err) 459 return (EIO); 460 461 return (0); 462 } 463 464 void 465 uplcom_set_line_state(struct uplcom_softc *sc) 466 { 467 usb_device_request_t req; 468 int ls; 469 470 /* Make sure we have initialized state for sc_dtr and sc_rts */ 471 if (sc->sc_dtr == -1) 472 sc->sc_dtr = 0; 473 if (sc->sc_rts == -1) 474 sc->sc_rts = 0; 475 476 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) | 477 (sc->sc_rts ? UCDC_LINE_RTS : 0); 478 479 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 480 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 481 USETW(req.wValue, ls); 482 USETW(req.wIndex, sc->sc_iface_number); 483 USETW(req.wLength, 0); 484 485 (void)usbd_do_request(sc->sc_udev, &req, 0); 486 487 } 488 489 void 490 uplcom_set(void *addr, int portno, int reg, int onoff) 491 { 492 struct uplcom_softc *sc = addr; 493 494 switch (reg) { 495 case UCOM_SET_DTR: 496 uplcom_dtr(sc, onoff); 497 break; 498 case UCOM_SET_RTS: 499 uplcom_rts(sc, onoff); 500 break; 501 case UCOM_SET_BREAK: 502 uplcom_break(sc, onoff); 503 break; 504 default: 505 break; 506 } 507 } 508 509 void 510 uplcom_dtr(struct uplcom_softc *sc, int onoff) 511 { 512 513 DPRINTF(("uplcom_dtr: onoff=%d\n", onoff)); 514 515 if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff) 516 return; 517 518 sc->sc_dtr = !!onoff; 519 520 uplcom_set_line_state(sc); 521 } 522 523 void 524 uplcom_rts(struct uplcom_softc *sc, int onoff) 525 { 526 DPRINTF(("uplcom_rts: onoff=%d\n", onoff)); 527 528 if (sc->sc_rts == -1 && !sc->sc_rts == !onoff) 529 return; 530 531 sc->sc_rts = !!onoff; 532 533 uplcom_set_line_state(sc); 534 } 535 536 void 537 uplcom_break(struct uplcom_softc *sc, int onoff) 538 { 539 usb_device_request_t req; 540 541 DPRINTF(("uplcom_break: onoff=%d\n", onoff)); 542 543 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 544 req.bRequest = UCDC_SEND_BREAK; 545 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF); 546 USETW(req.wIndex, sc->sc_iface_number); 547 USETW(req.wLength, 0); 548 549 (void)usbd_do_request(sc->sc_udev, &req, 0); 550 } 551 552 usbd_status 553 uplcom_set_crtscts(struct uplcom_softc *sc) 554 { 555 usb_device_request_t req; 556 usbd_status err; 557 558 DPRINTF(("uplcom_set_crtscts: on\n")); 559 560 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 561 if (sc->sc_type == UPLCOM_TYPE_HXN) { 562 req.bRequest = UPLCOM_HXN_SET_REQUEST; 563 USETW(req.wValue, UPLCOM_HXN_SET_CRTSCTS_REG); 564 USETW(req.wIndex, UPLCOM_HXN_SET_CRTSCTS); 565 } else { 566 req.bRequest = UPLCOM_SET_REQUEST; 567 USETW(req.wValue, 0); 568 USETW(req.wIndex, (sc->sc_type == UPLCOM_TYPE_HX ? 569 UPLCOM_HX_SET_CRTSCTS : UPLCOM_SET_CRTSCTS)); 570 } 571 USETW(req.wLength, 0); 572 573 err = usbd_do_request(sc->sc_udev, &req, 0); 574 if (err) { 575 DPRINTF(("uplcom_set_crtscts: failed, err=%s\n", 576 usbd_errstr(err))); 577 return (err); 578 } 579 580 return (USBD_NORMAL_COMPLETION); 581 } 582 583 usbd_status 584 uplcom_set_line_coding(struct uplcom_softc *sc, 585 struct usb_cdc_line_state *state) 586 { 587 usb_device_request_t req; 588 usbd_status err; 589 590 DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n", 591 UGETDW(state->dwDTERate), state->bCharFormat, 592 state->bParityType, state->bDataBits)); 593 594 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) { 595 DPRINTF(("uplcom_set_line_coding: already set\n")); 596 return (USBD_NORMAL_COMPLETION); 597 } 598 599 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 600 req.bRequest = UCDC_SET_LINE_CODING; 601 USETW(req.wValue, 0); 602 USETW(req.wIndex, sc->sc_iface_number); 603 USETW(req.wLength, UCDC_LINE_STATE_LENGTH); 604 605 err = usbd_do_request(sc->sc_udev, &req, state); 606 if (err) { 607 DPRINTF(("uplcom_set_line_coding: failed, err=%s\n", 608 usbd_errstr(err))); 609 return (err); 610 } 611 612 sc->sc_line_state = *state; 613 614 return (USBD_NORMAL_COMPLETION); 615 } 616 617 int 618 uplcom_param(void *addr, int portno, struct termios *t) 619 { 620 struct uplcom_softc *sc = addr; 621 usbd_status err; 622 struct usb_cdc_line_state ls; 623 624 DPRINTF(("uplcom_param: sc=%p\n", sc)); 625 626 USETDW(ls.dwDTERate, t->c_ospeed); 627 if (ISSET(t->c_cflag, CSTOPB)) 628 ls.bCharFormat = UCDC_STOP_BIT_2; 629 else 630 ls.bCharFormat = UCDC_STOP_BIT_1; 631 if (ISSET(t->c_cflag, PARENB)) { 632 if (ISSET(t->c_cflag, PARODD)) 633 ls.bParityType = UCDC_PARITY_ODD; 634 else 635 ls.bParityType = UCDC_PARITY_EVEN; 636 } else 637 ls.bParityType = UCDC_PARITY_NONE; 638 switch (ISSET(t->c_cflag, CSIZE)) { 639 case CS5: 640 ls.bDataBits = 5; 641 break; 642 case CS6: 643 ls.bDataBits = 6; 644 break; 645 case CS7: 646 ls.bDataBits = 7; 647 break; 648 case CS8: 649 ls.bDataBits = 8; 650 break; 651 } 652 653 err = uplcom_set_line_coding(sc, &ls); 654 if (err) { 655 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err))); 656 return (EIO); 657 } 658 659 if (ISSET(t->c_cflag, CRTSCTS)) 660 uplcom_set_crtscts(sc); 661 662 if (sc->sc_rts == -1 || sc->sc_dtr == -1) 663 uplcom_set_line_state(sc); 664 665 return (0); 666 } 667 668 int 669 uplcom_open(void *addr, int portno) 670 { 671 struct uplcom_softc *sc = addr; 672 usb_device_request_t req; 673 usbd_status uerr; 674 int err; 675 676 if (usbd_is_dying(sc->sc_udev)) 677 return (EIO); 678 679 DPRINTF(("uplcom_open: sc=%p\n", sc)); 680 681 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) { 682 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK); 683 err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number, 684 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, 685 sc->sc_intr_buf, sc->sc_isize, 686 uplcom_intr, USBD_DEFAULT_INTERVAL); 687 if (err) { 688 DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n", 689 sc->sc_dev.dv_xname, sc->sc_intr_number)); 690 return (EIO); 691 } 692 } 693 694 if (sc->sc_type == UPLCOM_TYPE_HX) { 695 /* 696 * Undocumented (vendor unresponsive) - possibly changes 697 * flow control semantics. It is needed for HX variant devices. 698 */ 699 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 700 req.bRequest = UPLCOM_SET_REQUEST; 701 USETW(req.wValue, 2); 702 USETW(req.wIndex, 0x44); 703 USETW(req.wLength, 0); 704 705 uerr = usbd_do_request(sc->sc_udev, &req, 0); 706 if (uerr) 707 return (EIO); 708 709 /* Reset upstream data pipes */ 710 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 711 req.bRequest = UPLCOM_SET_REQUEST; 712 USETW(req.wValue, 8); 713 USETW(req.wIndex, 0); 714 USETW(req.wLength, 0); 715 716 uerr = usbd_do_request(sc->sc_udev, &req, 0); 717 if (uerr) 718 return (EIO); 719 720 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 721 req.bRequest = UPLCOM_SET_REQUEST; 722 USETW(req.wValue, 9); 723 USETW(req.wIndex, 0); 724 USETW(req.wLength, 0); 725 726 uerr = usbd_do_request(sc->sc_udev, &req, 0); 727 if (uerr) 728 return (EIO); 729 } 730 731 if (sc->sc_type == UPLCOM_TYPE_HXN) { 732 /* Reset upstream data pipes */ 733 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 734 req.bRequest = UPLCOM_HXN_SET_REQUEST; 735 USETW(req.wValue, 0x07); 736 USETW(req.wIndex, 0x03); 737 USETW(req.wLength, 0); 738 739 uerr = usbd_do_request(sc->sc_udev, &req, 0); 740 if (uerr) 741 return (EIO); 742 } 743 744 return (0); 745 } 746 747 void 748 uplcom_close(void *addr, int portno) 749 { 750 struct uplcom_softc *sc = addr; 751 int err; 752 753 if (usbd_is_dying(sc->sc_udev)) 754 return; 755 756 DPRINTF(("uplcom_close: close\n")); 757 758 if (sc->sc_intr_pipe != NULL) { 759 err = usbd_close_pipe(sc->sc_intr_pipe); 760 if (err) 761 printf("%s: close interrupt pipe failed: %s\n", 762 sc->sc_dev.dv_xname, usbd_errstr(err)); 763 free(sc->sc_intr_buf, M_USBDEV, sc->sc_isize); 764 sc->sc_intr_pipe = NULL; 765 } 766 } 767 768 void 769 uplcom_intr(struct usbd_xfer *xfer, void *priv, usbd_status status) 770 { 771 struct uplcom_softc *sc = priv; 772 u_char *buf = sc->sc_intr_buf; 773 u_char pstatus; 774 775 if (usbd_is_dying(sc->sc_udev)) 776 return; 777 778 if (status != USBD_NORMAL_COMPLETION) { 779 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 780 return; 781 782 DPRINTF(("%s: abnormal status: %s\n", sc->sc_dev.dv_xname, 783 usbd_errstr(status))); 784 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe); 785 return; 786 } 787 788 DPRINTF(("%s: uplcom status = %02x\n", sc->sc_dev.dv_xname, buf[8])); 789 790 sc->sc_lsr = sc->sc_msr = 0; 791 pstatus = buf[8]; 792 if (ISSET(pstatus, RSAQ_STATUS_CTS)) 793 sc->sc_msr |= UMSR_CTS; 794 else 795 sc->sc_msr &= ~UMSR_CTS; 796 if (ISSET(pstatus, RSAQ_STATUS_DSR)) 797 sc->sc_msr |= UMSR_DSR; 798 else 799 sc->sc_msr &= ~UMSR_DSR; 800 if (ISSET(pstatus, RSAQ_STATUS_DCD)) 801 sc->sc_msr |= UMSR_DCD; 802 else 803 sc->sc_msr &= ~UMSR_DCD; 804 ucom_status_change((struct ucom_softc *) sc->sc_subdev); 805 } 806 807 void 808 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr) 809 { 810 struct uplcom_softc *sc = addr; 811 812 DPRINTF(("uplcom_get_status:\n")); 813 814 if (lsr != NULL) 815 *lsr = sc->sc_lsr; 816 if (msr != NULL) 817 *msr = sc->sc_msr; 818 } 819