1 /* $NetBSD: uplcom.c,v 1.21 2001/11/13 06:24:56 lukem Exp $ */ 2 3 #include <sys/cdefs.h> 4 __FBSDID("$FreeBSD$"); 5 6 /*- 7 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause-NetBSD 8 * 9 * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>. 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 /*- 35 * Copyright (c) 2001 The NetBSD Foundation, Inc. 36 * All rights reserved. 37 * 38 * This code is derived from software contributed to The NetBSD Foundation 39 * by Ichiro FUKUHARA (ichiro@ichiro.org). 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 51 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 52 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 53 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 54 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 57 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 58 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 60 * POSSIBILITY OF SUCH DAMAGE. 61 */ 62 63 /* 64 * This driver supports several USB-to-RS232 serial adapters driven by 65 * Prolific PL-2303, PL-2303X and probably PL-2303HX USB-to-RS232 66 * bridge chip. The adapters are sold under many different brand 67 * names. 68 * 69 * Datasheets are available at Prolific www site at 70 * http://www.prolific.com.tw. The datasheets don't contain full 71 * programming information for the chip. 72 * 73 * PL-2303HX is probably programmed the same as PL-2303X. 74 * 75 * There are several differences between PL-2303 and PL-2303(H)X. 76 * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_ 77 * different command for controlling CRTSCTS and needs special 78 * sequence of commands for initialization which aren't also 79 * documented in the datasheet. 80 */ 81 82 #include <sys/stdint.h> 83 #include <sys/stddef.h> 84 #include <sys/param.h> 85 #include <sys/queue.h> 86 #include <sys/types.h> 87 #include <sys/systm.h> 88 #include <sys/kernel.h> 89 #include <sys/bus.h> 90 #include <sys/module.h> 91 #include <sys/lock.h> 92 #include <sys/mutex.h> 93 #include <sys/condvar.h> 94 #include <sys/sysctl.h> 95 #include <sys/sx.h> 96 #include <sys/unistd.h> 97 #include <sys/callout.h> 98 #include <sys/malloc.h> 99 #include <sys/priv.h> 100 101 #include <dev/usb/usb.h> 102 #include <dev/usb/usbdi.h> 103 #include <dev/usb/usbdi_util.h> 104 #include <dev/usb/usb_cdc.h> 105 #include "usbdevs.h" 106 107 #define USB_DEBUG_VAR uplcom_debug 108 #include <dev/usb/usb_debug.h> 109 #include <dev/usb/usb_process.h> 110 111 #include <dev/usb/serial/usb_serial.h> 112 113 #ifdef USB_DEBUG 114 static int uplcom_debug = 0; 115 116 static SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom"); 117 SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RWTUN, 118 &uplcom_debug, 0, "Debug level"); 119 #endif 120 121 #define UPLCOM_MODVER 1 /* module version */ 122 123 #define UPLCOM_CONFIG_INDEX 0 124 #define UPLCOM_IFACE_INDEX 0 125 #define UPLCOM_SECOND_IFACE_INDEX 1 126 127 #ifndef UPLCOM_INTR_INTERVAL 128 #define UPLCOM_INTR_INTERVAL 0 /* default */ 129 #endif 130 131 #define UPLCOM_BULK_BUF_SIZE 1024 /* bytes */ 132 133 #define UPLCOM_SET_REQUEST 0x01 134 #define UPLCOM_SET_CRTSCTS 0x41 135 #define UPLCOM_SET_CRTSCTS_PL2303X 0x61 136 #define RSAQ_STATUS_CTS 0x80 137 #define RSAQ_STATUS_OVERRUN_ERROR 0x40 138 #define RSAQ_STATUS_PARITY_ERROR 0x20 139 #define RSAQ_STATUS_FRAME_ERROR 0x10 140 #define RSAQ_STATUS_RING 0x08 141 #define RSAQ_STATUS_BREAK_ERROR 0x04 142 #define RSAQ_STATUS_DSR 0x02 143 #define RSAQ_STATUS_DCD 0x01 144 145 #define TYPE_PL2303 0 146 #define TYPE_PL2303HX 1 147 #define TYPE_PL2303HXD 2 148 149 #define UPLCOM_STATE_INDEX 8 150 151 enum { 152 UPLCOM_BULK_DT_WR, 153 UPLCOM_BULK_DT_RD, 154 UPLCOM_INTR_DT_RD, 155 UPLCOM_N_TRANSFER, 156 }; 157 158 struct uplcom_softc { 159 struct ucom_super_softc sc_super_ucom; 160 struct ucom_softc sc_ucom; 161 162 struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER]; 163 struct usb_device *sc_udev; 164 struct mtx sc_mtx; 165 166 uint16_t sc_line; 167 168 uint8_t sc_lsr; /* local status register */ 169 uint8_t sc_msr; /* uplcom status register */ 170 uint8_t sc_chiptype; /* type of chip */ 171 uint8_t sc_ctrl_iface_no; 172 uint8_t sc_data_iface_no; 173 uint8_t sc_iface_index[2]; 174 }; 175 176 /* prototypes */ 177 178 static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *); 179 static usb_error_t uplcom_pl2303_do(struct usb_device *, uint8_t, uint8_t, 180 uint16_t, uint16_t, uint16_t); 181 static int uplcom_pl2303_init(struct usb_device *, uint8_t); 182 static void uplcom_free(struct ucom_softc *); 183 static void uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t); 184 static void uplcom_cfg_set_rts(struct ucom_softc *, uint8_t); 185 static void uplcom_cfg_set_break(struct ucom_softc *, uint8_t); 186 static int uplcom_pre_param(struct ucom_softc *, struct termios *); 187 static void uplcom_cfg_param(struct ucom_softc *, struct termios *); 188 static void uplcom_start_read(struct ucom_softc *); 189 static void uplcom_stop_read(struct ucom_softc *); 190 static void uplcom_start_write(struct ucom_softc *); 191 static void uplcom_stop_write(struct ucom_softc *); 192 static void uplcom_cfg_get_status(struct ucom_softc *, uint8_t *, 193 uint8_t *); 194 static void uplcom_poll(struct ucom_softc *ucom); 195 196 static device_probe_t uplcom_probe; 197 static device_attach_t uplcom_attach; 198 static device_detach_t uplcom_detach; 199 static void uplcom_free_softc(struct uplcom_softc *); 200 201 static usb_callback_t uplcom_intr_callback; 202 static usb_callback_t uplcom_write_callback; 203 static usb_callback_t uplcom_read_callback; 204 205 static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = { 206 207 [UPLCOM_BULK_DT_WR] = { 208 .type = UE_BULK, 209 .endpoint = UE_ADDR_ANY, 210 .direction = UE_DIR_OUT, 211 .bufsize = UPLCOM_BULK_BUF_SIZE, 212 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 213 .callback = &uplcom_write_callback, 214 .if_index = 0, 215 }, 216 217 [UPLCOM_BULK_DT_RD] = { 218 .type = UE_BULK, 219 .endpoint = UE_ADDR_ANY, 220 .direction = UE_DIR_IN, 221 .bufsize = UPLCOM_BULK_BUF_SIZE, 222 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 223 .callback = &uplcom_read_callback, 224 .if_index = 0, 225 }, 226 227 [UPLCOM_INTR_DT_RD] = { 228 .type = UE_INTERRUPT, 229 .endpoint = UE_ADDR_ANY, 230 .direction = UE_DIR_IN, 231 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 232 .bufsize = 0, /* use wMaxPacketSize */ 233 .callback = &uplcom_intr_callback, 234 .if_index = 1, 235 }, 236 }; 237 238 static struct ucom_callback uplcom_callback = { 239 .ucom_cfg_get_status = &uplcom_cfg_get_status, 240 .ucom_cfg_set_dtr = &uplcom_cfg_set_dtr, 241 .ucom_cfg_set_rts = &uplcom_cfg_set_rts, 242 .ucom_cfg_set_break = &uplcom_cfg_set_break, 243 .ucom_cfg_param = &uplcom_cfg_param, 244 .ucom_pre_param = &uplcom_pre_param, 245 .ucom_start_read = &uplcom_start_read, 246 .ucom_stop_read = &uplcom_stop_read, 247 .ucom_start_write = &uplcom_start_write, 248 .ucom_stop_write = &uplcom_stop_write, 249 .ucom_poll = &uplcom_poll, 250 .ucom_free = &uplcom_free, 251 }; 252 253 #define UPLCOM_DEV(v,p) \ 254 { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p) } 255 256 static const STRUCT_USB_HOST_ID uplcom_devs[] = { 257 UPLCOM_DEV(ACERP, S81), /* BenQ S81 phone */ 258 UPLCOM_DEV(ADLINK, ND6530), /* ADLINK ND-6530 USB-Serial */ 259 UPLCOM_DEV(ALCATEL, OT535), /* Alcatel One Touch 535/735 */ 260 UPLCOM_DEV(ALCOR, AU9720), /* Alcor AU9720 USB 2.0-RS232 */ 261 UPLCOM_DEV(ANCHOR, SERIAL), /* Anchor Serial adapter */ 262 UPLCOM_DEV(ATEN, UC232A), /* PLANEX USB-RS232 URS-03 */ 263 UPLCOM_DEV(BELKIN, F5U257), /* Belkin F5U257 USB to Serial */ 264 UPLCOM_DEV(COREGA, CGUSBRS232R), /* Corega CG-USBRS232R */ 265 UPLCOM_DEV(EPSON, CRESSI_EDY), /* Cressi Edy diving computer */ 266 UPLCOM_DEV(EPSON, N2ITION3), /* Zeagle N2iTion3 diving computer */ 267 UPLCOM_DEV(ELECOM, UCSGT), /* ELECOM UC-SGT Serial Adapter */ 268 UPLCOM_DEV(ELECOM, UCSGT0), /* ELECOM UC-SGT Serial Adapter */ 269 UPLCOM_DEV(HAL, IMR001), /* HAL Corporation Crossam2+USB */ 270 UPLCOM_DEV(HP, LD220), /* HP LD220 POS Display */ 271 UPLCOM_DEV(IODATA, USBRSAQ), /* I/O DATA USB-RSAQ */ 272 UPLCOM_DEV(IODATA, USBRSAQ5), /* I/O DATA USB-RSAQ5 */ 273 UPLCOM_DEV(ITEGNO, WM1080A), /* iTegno WM1080A GSM/GFPRS modem */ 274 UPLCOM_DEV(ITEGNO, WM2080A), /* iTegno WM2080A CDMA modem */ 275 UPLCOM_DEV(LEADTEK, 9531), /* Leadtek 9531 GPS */ 276 UPLCOM_DEV(MICROSOFT, 700WX), /* Microsoft Palm 700WX */ 277 UPLCOM_DEV(MOBILEACTION, MA620), /* Mobile Action MA-620 Infrared Adapter */ 278 UPLCOM_DEV(NETINDEX, WS002IN), /* Willcom W-S002IN */ 279 UPLCOM_DEV(NOKIA2, CA42), /* Nokia CA-42 cable */ 280 UPLCOM_DEV(OTI, DKU5), /* OTI DKU-5 cable */ 281 UPLCOM_DEV(PANASONIC, TYTP50P6S), /* Panasonic TY-TP50P6-S flat screen */ 282 UPLCOM_DEV(PLX, CA42), /* PLX CA-42 clone cable */ 283 UPLCOM_DEV(PROLIFIC, ALLTRONIX_GPRS), /* Alltronix ACM003U00 modem */ 284 UPLCOM_DEV(PROLIFIC, ALDIGA_AL11U), /* AlDiga AL-11U modem */ 285 UPLCOM_DEV(PROLIFIC, DCU11), /* DCU-11 Phone Cable */ 286 UPLCOM_DEV(PROLIFIC, HCR331), /* HCR331 Card Reader */ 287 UPLCOM_DEV(PROLIFIC, MICROMAX_610U), /* Micromax 610U modem */ 288 UPLCOM_DEV(PROLIFIC, MOTOROLA), /* Motorola cable */ 289 UPLCOM_DEV(PROLIFIC, PHAROS), /* Prolific Pharos */ 290 UPLCOM_DEV(PROLIFIC, PL2303), /* Generic adapter */ 291 UPLCOM_DEV(PROLIFIC, RSAQ2), /* I/O DATA USB-RSAQ2 */ 292 UPLCOM_DEV(PROLIFIC, RSAQ3), /* I/O DATA USB-RSAQ3 */ 293 UPLCOM_DEV(PROLIFIC, UIC_MSR206), /* UIC MSR206 Card Reader */ 294 UPLCOM_DEV(PROLIFIC2, PL2303), /* Prolific adapter */ 295 UPLCOM_DEV(RADIOSHACK, USBCABLE), /* Radio Shack USB Adapter */ 296 UPLCOM_DEV(RATOC, REXUSB60), /* RATOC REX-USB60 */ 297 UPLCOM_DEV(SAGEM, USBSERIAL), /* Sagem USB-Serial Controller */ 298 UPLCOM_DEV(SAMSUNG, I330), /* Samsung I330 phone cradle */ 299 UPLCOM_DEV(SANWA, KB_USB2), /* Sanwa KB-USB2 Multimeter cable */ 300 UPLCOM_DEV(SIEMENS3, EF81), /* Siemens EF81 */ 301 UPLCOM_DEV(SIEMENS3, SX1), /* Siemens SX1 */ 302 UPLCOM_DEV(SIEMENS3, X65), /* Siemens X65 */ 303 UPLCOM_DEV(SIEMENS3, X75), /* Siemens X75 */ 304 UPLCOM_DEV(SITECOM, SERIAL), /* Sitecom USB to Serial */ 305 UPLCOM_DEV(SMART, PL2303), /* SMART Technologies USB to Serial */ 306 UPLCOM_DEV(SONY, QN3), /* Sony QN3 phone cable */ 307 UPLCOM_DEV(SONYERICSSON, DATAPILOT), /* Sony Ericsson Datapilot */ 308 UPLCOM_DEV(SONYERICSSON, DCU10), /* Sony Ericsson DCU-10 Cable */ 309 UPLCOM_DEV(SOURCENEXT, KEIKAI8), /* SOURCENEXT KeikaiDenwa 8 */ 310 UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG), /* SOURCENEXT KeikaiDenwa 8 with charger */ 311 UPLCOM_DEV(SPEEDDRAGON, MS3303H), /* Speed Dragon USB-Serial */ 312 UPLCOM_DEV(SYNTECH, CPT8001C), /* Syntech CPT-8001C Barcode scanner */ 313 UPLCOM_DEV(TDK, UHA6400), /* TDK USB-PHS Adapter UHA6400 */ 314 UPLCOM_DEV(TDK, UPA9664), /* TDK USB-PHS Adapter UPA9664 */ 315 UPLCOM_DEV(TRIPPLITE, U209), /* Tripp-Lite U209-000-R USB to Serial */ 316 UPLCOM_DEV(YCCABLE, PL2303), /* YC Cable USB-Serial */ 317 }; 318 #undef UPLCOM_DEV 319 320 static device_method_t uplcom_methods[] = { 321 DEVMETHOD(device_probe, uplcom_probe), 322 DEVMETHOD(device_attach, uplcom_attach), 323 DEVMETHOD(device_detach, uplcom_detach), 324 DEVMETHOD_END 325 }; 326 327 static devclass_t uplcom_devclass; 328 329 static driver_t uplcom_driver = { 330 .name = "uplcom", 331 .methods = uplcom_methods, 332 .size = sizeof(struct uplcom_softc), 333 }; 334 335 DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0); 336 MODULE_DEPEND(uplcom, ucom, 1, 1, 1); 337 MODULE_DEPEND(uplcom, usb, 1, 1, 1); 338 MODULE_VERSION(uplcom, UPLCOM_MODVER); 339 USB_PNP_HOST_INFO(uplcom_devs); 340 341 static int 342 uplcom_probe(device_t dev) 343 { 344 struct usb_attach_arg *uaa = device_get_ivars(dev); 345 346 DPRINTFN(11, "\n"); 347 348 if (uaa->usb_mode != USB_MODE_HOST) { 349 return (ENXIO); 350 } 351 if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) { 352 return (ENXIO); 353 } 354 if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) { 355 return (ENXIO); 356 } 357 return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa)); 358 } 359 360 static int 361 uplcom_attach(device_t dev) 362 { 363 struct usb_attach_arg *uaa = device_get_ivars(dev); 364 struct uplcom_softc *sc = device_get_softc(dev); 365 struct usb_interface *iface; 366 struct usb_interface_descriptor *id; 367 struct usb_device_descriptor *dd; 368 int error; 369 370 DPRINTFN(11, "\n"); 371 372 device_set_usb_desc(dev); 373 mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF); 374 ucom_ref(&sc->sc_super_ucom); 375 376 DPRINTF("sc = %p\n", sc); 377 378 sc->sc_udev = uaa->device; 379 380 dd = usbd_get_device_descriptor(sc->sc_udev); 381 382 switch (UGETW(dd->bcdDevice)) { 383 case 0x0300: 384 sc->sc_chiptype = TYPE_PL2303HX; 385 /* or TA, that is HX with external crystal */ 386 break; 387 case 0x0400: 388 sc->sc_chiptype = TYPE_PL2303HXD; 389 /* or EA, that is HXD with ESD protection */ 390 /* or RA, that has internal voltage level converter that works only up to 1Mbaud (!) */ 391 break; 392 case 0x0500: 393 sc->sc_chiptype = TYPE_PL2303HXD; 394 /* in fact it's TB, that is HXD with external crystal */ 395 break; 396 default: 397 /* NOTE: I have no info about the bcdDevice for the base PL2303 (up to 1.2Mbaud, 398 only fixed rates) and for PL2303SA (8-pin chip, up to 115200 baud */ 399 /* Determine the chip type. This algorithm is taken from Linux. */ 400 if (dd->bDeviceClass == 0x02) 401 sc->sc_chiptype = TYPE_PL2303; 402 else if (dd->bMaxPacketSize == 0x40) 403 sc->sc_chiptype = TYPE_PL2303HX; 404 else 405 sc->sc_chiptype = TYPE_PL2303; 406 break; 407 } 408 409 switch (sc->sc_chiptype) { 410 case TYPE_PL2303: 411 DPRINTF("chiptype: 2303\n"); 412 break; 413 case TYPE_PL2303HX: 414 DPRINTF("chiptype: 2303HX/TA\n"); 415 break; 416 case TYPE_PL2303HXD: 417 DPRINTF("chiptype: 2303HXD/TB/RA/EA\n"); 418 break; 419 default: 420 DPRINTF("chiptype: unknown %d\n", sc->sc_chiptype); 421 break; 422 } 423 424 /* 425 * USB-RSAQ1 has two interface 426 * 427 * USB-RSAQ1 | USB-RSAQ2 428 * -----------------+----------------- 429 * Interface 0 |Interface 0 430 * Interrupt(0x81) | Interrupt(0x81) 431 * -----------------+ BulkIN(0x02) 432 * Interface 1 | BulkOUT(0x83) 433 * BulkIN(0x02) | 434 * BulkOUT(0x83) | 435 */ 436 437 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum; 438 sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX; 439 440 iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX); 441 if (iface) { 442 id = usbd_get_interface_descriptor(iface); 443 if (id == NULL) { 444 device_printf(dev, "no interface descriptor (2)\n"); 445 goto detach; 446 } 447 sc->sc_data_iface_no = id->bInterfaceNumber; 448 sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX; 449 usbd_set_parent_iface(uaa->device, 450 UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex); 451 } else { 452 sc->sc_data_iface_no = sc->sc_ctrl_iface_no; 453 sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX; 454 } 455 456 error = usbd_transfer_setup(uaa->device, 457 sc->sc_iface_index, sc->sc_xfer, uplcom_config_data, 458 UPLCOM_N_TRANSFER, sc, &sc->sc_mtx); 459 if (error) { 460 DPRINTF("one or more missing USB endpoints, " 461 "error=%s\n", usbd_errstr(error)); 462 goto detach; 463 } 464 error = uplcom_reset(sc, uaa->device); 465 if (error) { 466 device_printf(dev, "reset failed, error=%s\n", 467 usbd_errstr(error)); 468 goto detach; 469 } 470 471 if (sc->sc_chiptype == TYPE_PL2303) { 472 /* HX variants seem to lock up after a clear stall request. */ 473 mtx_lock(&sc->sc_mtx); 474 usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 475 usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 476 mtx_unlock(&sc->sc_mtx); 477 } else { 478 /* reset upstream data pipes */ 479 if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE, 480 UPLCOM_SET_REQUEST, 8, 0, 0) || 481 uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE, 482 UPLCOM_SET_REQUEST, 9, 0, 0)) { 483 goto detach; 484 } 485 } 486 487 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 488 &uplcom_callback, &sc->sc_mtx); 489 if (error) { 490 goto detach; 491 } 492 /* 493 * do the initialization during attach so that the system does not 494 * sleep during open: 495 */ 496 if (uplcom_pl2303_init(uaa->device, sc->sc_chiptype)) { 497 device_printf(dev, "init failed\n"); 498 goto detach; 499 } 500 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev); 501 502 return (0); 503 504 detach: 505 uplcom_detach(dev); 506 return (ENXIO); 507 } 508 509 static int 510 uplcom_detach(device_t dev) 511 { 512 struct uplcom_softc *sc = device_get_softc(dev); 513 514 DPRINTF("sc=%p\n", sc); 515 516 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom); 517 usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER); 518 519 device_claim_softc(dev); 520 521 uplcom_free_softc(sc); 522 523 return (0); 524 } 525 526 UCOM_UNLOAD_DRAIN(uplcom); 527 528 static void 529 uplcom_free_softc(struct uplcom_softc *sc) 530 { 531 if (ucom_unref(&sc->sc_super_ucom)) { 532 mtx_destroy(&sc->sc_mtx); 533 device_free_softc(sc); 534 } 535 } 536 537 static void 538 uplcom_free(struct ucom_softc *ucom) 539 { 540 uplcom_free_softc(ucom->sc_parent); 541 } 542 543 static usb_error_t 544 uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev) 545 { 546 struct usb_device_request req; 547 548 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 549 req.bRequest = UPLCOM_SET_REQUEST; 550 USETW(req.wValue, 0); 551 req.wIndex[0] = sc->sc_data_iface_no; 552 req.wIndex[1] = 0; 553 USETW(req.wLength, 0); 554 555 return (usbd_do_request(udev, NULL, &req, NULL)); 556 } 557 558 static usb_error_t 559 uplcom_pl2303_do(struct usb_device *udev, uint8_t req_type, uint8_t request, 560 uint16_t value, uint16_t index, uint16_t length) 561 { 562 struct usb_device_request req; 563 usb_error_t err; 564 uint8_t buf[4]; 565 566 req.bmRequestType = req_type; 567 req.bRequest = request; 568 USETW(req.wValue, value); 569 USETW(req.wIndex, index); 570 USETW(req.wLength, length); 571 572 err = usbd_do_request(udev, NULL, &req, buf); 573 if (err) { 574 DPRINTF("error=%s\n", usbd_errstr(err)); 575 return (1); 576 } 577 return (0); 578 } 579 580 static int 581 uplcom_pl2303_init(struct usb_device *udev, uint8_t chiptype) 582 { 583 int err; 584 585 if (uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1) 586 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0) 587 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1) 588 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1) 589 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1) 590 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0) 591 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1) 592 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1) 593 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0) 594 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0)) 595 return (EIO); 596 597 if (chiptype != TYPE_PL2303) 598 err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0); 599 else 600 err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x24, 0); 601 if (err) 602 return (EIO); 603 604 return (0); 605 } 606 607 static void 608 uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 609 { 610 struct uplcom_softc *sc = ucom->sc_parent; 611 struct usb_device_request req; 612 613 DPRINTF("onoff = %d\n", onoff); 614 615 if (onoff) 616 sc->sc_line |= UCDC_LINE_DTR; 617 else 618 sc->sc_line &= ~UCDC_LINE_DTR; 619 620 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 621 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 622 USETW(req.wValue, sc->sc_line); 623 req.wIndex[0] = sc->sc_data_iface_no; 624 req.wIndex[1] = 0; 625 USETW(req.wLength, 0); 626 627 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 628 &req, NULL, 0, 1000); 629 } 630 631 static void 632 uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff) 633 { 634 struct uplcom_softc *sc = ucom->sc_parent; 635 struct usb_device_request req; 636 637 DPRINTF("onoff = %d\n", onoff); 638 639 if (onoff) 640 sc->sc_line |= UCDC_LINE_RTS; 641 else 642 sc->sc_line &= ~UCDC_LINE_RTS; 643 644 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 645 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 646 USETW(req.wValue, sc->sc_line); 647 req.wIndex[0] = sc->sc_data_iface_no; 648 req.wIndex[1] = 0; 649 USETW(req.wLength, 0); 650 651 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 652 &req, NULL, 0, 1000); 653 } 654 655 static void 656 uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff) 657 { 658 struct uplcom_softc *sc = ucom->sc_parent; 659 struct usb_device_request req; 660 uint16_t temp; 661 662 DPRINTF("onoff = %d\n", onoff); 663 664 temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF); 665 666 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 667 req.bRequest = UCDC_SEND_BREAK; 668 USETW(req.wValue, temp); 669 req.wIndex[0] = sc->sc_data_iface_no; 670 req.wIndex[1] = 0; 671 USETW(req.wLength, 0); 672 673 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 674 &req, NULL, 0, 1000); 675 } 676 677 /* 678 * NOTE: These baud rates are officially supported, they can be written 679 * directly into dwDTERate register. 680 * 681 * Free baudrate setting is not supported by the base PL2303, and on 682 * other models it requires writing a divisor value to dwDTERate instead 683 * of the raw baudrate. The formula for divisor calculation is not published 684 * by the vendor, so it is speculative, though the official product homepage 685 * refers to the Linux module source as a reference implementation. 686 */ 687 static const uint32_t uplcom_rates[] = { 688 /* 689 * Basic 'standard' speed rates, supported by all models 690 * NOTE: 900 and 56000 actually works as well 691 */ 692 75, 150, 300, 600, 900, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400, 693 19200, 28800, 38400, 56000, 57600, 115200, 694 /* 695 * Advanced speed rates up to 6Mbs, supported by HX/TA and HXD/TB/EA/RA 696 * NOTE: regardless of the spec, 256000 does not work 697 */ 698 128000, 134400, 161280, 201600, 230400, 268800, 403200, 460800, 614400, 699 806400, 921600, 1228800, 2457600, 3000000, 6000000, 700 /* 701 * Advanced speed rates up to 12, supported by HXD/TB/EA/RA 702 */ 703 12000000 704 }; 705 706 #define N_UPLCOM_RATES nitems(uplcom_rates) 707 708 static int 709 uplcom_baud_supported(unsigned int speed) 710 { 711 int i; 712 for (i = 0; i < N_UPLCOM_RATES; i++) { 713 if (uplcom_rates[i] == speed) 714 return 1; 715 } 716 return 0; 717 } 718 719 static int 720 uplcom_pre_param(struct ucom_softc *ucom, struct termios *t) 721 { 722 struct uplcom_softc *sc = ucom->sc_parent; 723 724 DPRINTF("\n"); 725 726 /** 727 * Check requested baud rate. 728 * 729 * The PL2303 can only set specific baud rates, up to 1228800 baud. 730 * The PL2303HX can set any baud rate up to 6Mb. 731 * The PL2303HX rev. D can set any baud rate up to 12Mb. 732 * 733 */ 734 735 /* accept raw divisor data, if someone wants to do the math in user domain */ 736 if (t->c_ospeed & 0x80000000) 737 return 0; 738 switch (sc->sc_chiptype) { 739 case TYPE_PL2303HXD: 740 if (t->c_ospeed <= 12000000) 741 return (0); 742 break; 743 case TYPE_PL2303HX: 744 if (t->c_ospeed <= 6000000) 745 return (0); 746 break; 747 default: 748 if (uplcom_baud_supported(t->c_ospeed)) 749 return (0); 750 break; 751 } 752 753 DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed); 754 return (EIO); 755 } 756 757 static unsigned int 758 uplcom_encode_baud_rate_divisor(uint8_t *buf, unsigned int baud) 759 { 760 unsigned int baseline, mantissa, exponent; 761 762 /* Determine the baud rate divisor. This algorithm is taken from Linux. */ 763 /* 764 * Apparently the formula is: 765 * baudrate = baseline / (mantissa * 4^exponent) 766 * where 767 * mantissa = buf[8:0] 768 * exponent = buf[11:9] 769 */ 770 if (baud == 0) 771 baud = 1; 772 baseline = 383385600; 773 mantissa = baseline / baud; 774 if (mantissa == 0) 775 mantissa = 1; 776 exponent = 0; 777 while (mantissa >= 512) { 778 if (exponent < 7) { 779 mantissa >>= 2; /* divide by 4 */ 780 exponent++; 781 } else { 782 /* Exponent is maxed. Trim mantissa and leave. This gives approx. 45.8 baud */ 783 mantissa = 511; 784 break; 785 } 786 } 787 788 buf[3] = 0x80; 789 buf[2] = 0; 790 buf[1] = exponent << 1 | mantissa >> 8; 791 buf[0] = mantissa & 0xff; 792 793 /* Calculate and return the exact baud rate. */ 794 baud = (baseline / mantissa) >> (exponent << 1); 795 DPRINTF("real baud rate will be %u\n", baud); 796 797 return baud; 798 } 799 static void 800 uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t) 801 { 802 struct uplcom_softc *sc = ucom->sc_parent; 803 struct usb_cdc_line_state ls; 804 struct usb_device_request req; 805 806 DPRINTF("sc = %p\n", sc); 807 808 memset(&ls, 0, sizeof(ls)); 809 810 /* 811 * NOTE: If unsupported baud rates are set directly, the PL2303* uses 9600 baud. 812 */ 813 if ((t->c_ospeed & 0x80000000) || uplcom_baud_supported(t->c_ospeed)) 814 USETDW(ls.dwDTERate, t->c_ospeed); 815 else 816 t->c_ospeed = uplcom_encode_baud_rate_divisor((uint8_t*)&ls.dwDTERate, t->c_ospeed); 817 818 if (t->c_cflag & CSTOPB) { 819 if ((t->c_cflag & CSIZE) == CS5) { 820 /* 821 * NOTE: Comply with "real" UARTs / RS232: 822 * use 1.5 instead of 2 stop bits with 5 data bits 823 */ 824 ls.bCharFormat = UCDC_STOP_BIT_1_5; 825 } else { 826 ls.bCharFormat = UCDC_STOP_BIT_2; 827 } 828 } else { 829 ls.bCharFormat = UCDC_STOP_BIT_1; 830 } 831 832 if (t->c_cflag & PARENB) { 833 if (t->c_cflag & PARODD) { 834 ls.bParityType = UCDC_PARITY_ODD; 835 } else { 836 ls.bParityType = UCDC_PARITY_EVEN; 837 } 838 } else { 839 ls.bParityType = UCDC_PARITY_NONE; 840 } 841 842 switch (t->c_cflag & CSIZE) { 843 case CS5: 844 ls.bDataBits = 5; 845 break; 846 case CS6: 847 ls.bDataBits = 6; 848 break; 849 case CS7: 850 ls.bDataBits = 7; 851 break; 852 case CS8: 853 ls.bDataBits = 8; 854 break; 855 } 856 857 DPRINTF("rate=0x%08x fmt=%d parity=%d bits=%d\n", 858 UGETDW(ls.dwDTERate), ls.bCharFormat, 859 ls.bParityType, ls.bDataBits); 860 861 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 862 req.bRequest = UCDC_SET_LINE_CODING; 863 USETW(req.wValue, 0); 864 req.wIndex[0] = sc->sc_data_iface_no; 865 req.wIndex[1] = 0; 866 USETW(req.wLength, UCDC_LINE_STATE_LENGTH); 867 868 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 869 &req, &ls, 0, 1000); 870 871 if (t->c_cflag & CRTSCTS) { 872 873 DPRINTF("crtscts = on\n"); 874 875 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 876 req.bRequest = UPLCOM_SET_REQUEST; 877 USETW(req.wValue, 0); 878 if (sc->sc_chiptype != TYPE_PL2303) 879 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X); 880 else 881 USETW(req.wIndex, UPLCOM_SET_CRTSCTS); 882 USETW(req.wLength, 0); 883 884 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 885 &req, NULL, 0, 1000); 886 } else { 887 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 888 req.bRequest = UPLCOM_SET_REQUEST; 889 USETW(req.wValue, 0); 890 USETW(req.wIndex, 0); 891 USETW(req.wLength, 0); 892 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 893 &req, NULL, 0, 1000); 894 } 895 } 896 897 static void 898 uplcom_start_read(struct ucom_softc *ucom) 899 { 900 struct uplcom_softc *sc = ucom->sc_parent; 901 902 /* start interrupt endpoint */ 903 usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]); 904 905 /* start read endpoint */ 906 usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 907 } 908 909 static void 910 uplcom_stop_read(struct ucom_softc *ucom) 911 { 912 struct uplcom_softc *sc = ucom->sc_parent; 913 914 /* stop interrupt endpoint */ 915 usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]); 916 917 /* stop read endpoint */ 918 usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 919 } 920 921 static void 922 uplcom_start_write(struct ucom_softc *ucom) 923 { 924 struct uplcom_softc *sc = ucom->sc_parent; 925 926 usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 927 } 928 929 static void 930 uplcom_stop_write(struct ucom_softc *ucom) 931 { 932 struct uplcom_softc *sc = ucom->sc_parent; 933 934 usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 935 } 936 937 static void 938 uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 939 { 940 struct uplcom_softc *sc = ucom->sc_parent; 941 942 DPRINTF("\n"); 943 944 *lsr = sc->sc_lsr; 945 *msr = sc->sc_msr; 946 } 947 948 static void 949 uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error) 950 { 951 struct uplcom_softc *sc = usbd_xfer_softc(xfer); 952 struct usb_page_cache *pc; 953 uint8_t buf[9]; 954 int actlen; 955 956 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 957 958 switch (USB_GET_STATE(xfer)) { 959 case USB_ST_TRANSFERRED: 960 961 DPRINTF("actlen = %u\n", actlen); 962 963 if (actlen >= 9) { 964 965 pc = usbd_xfer_get_frame(xfer, 0); 966 usbd_copy_out(pc, 0, buf, sizeof(buf)); 967 968 DPRINTF("status = 0x%02x\n", buf[UPLCOM_STATE_INDEX]); 969 970 sc->sc_lsr = 0; 971 sc->sc_msr = 0; 972 973 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_CTS) { 974 sc->sc_msr |= SER_CTS; 975 } 976 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_OVERRUN_ERROR) { 977 sc->sc_lsr |= ULSR_OE; 978 } 979 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_PARITY_ERROR) { 980 sc->sc_lsr |= ULSR_PE; 981 } 982 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_FRAME_ERROR) { 983 sc->sc_lsr |= ULSR_FE; 984 } 985 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_RING) { 986 sc->sc_msr |= SER_RI; 987 } 988 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_BREAK_ERROR) { 989 sc->sc_lsr |= ULSR_BI; 990 } 991 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DSR) { 992 sc->sc_msr |= SER_DSR; 993 } 994 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DCD) { 995 sc->sc_msr |= SER_DCD; 996 } 997 ucom_status_change(&sc->sc_ucom); 998 } 999 case USB_ST_SETUP: 1000 tr_setup: 1001 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1002 usbd_transfer_submit(xfer); 1003 return; 1004 1005 default: /* Error */ 1006 if (error != USB_ERR_CANCELLED) { 1007 /* try to clear stall first */ 1008 usbd_xfer_set_stall(xfer); 1009 goto tr_setup; 1010 } 1011 return; 1012 } 1013 } 1014 1015 static void 1016 uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error) 1017 { 1018 struct uplcom_softc *sc = usbd_xfer_softc(xfer); 1019 struct usb_page_cache *pc; 1020 uint32_t actlen; 1021 1022 switch (USB_GET_STATE(xfer)) { 1023 case USB_ST_SETUP: 1024 case USB_ST_TRANSFERRED: 1025 tr_setup: 1026 pc = usbd_xfer_get_frame(xfer, 0); 1027 if (ucom_get_data(&sc->sc_ucom, pc, 0, 1028 UPLCOM_BULK_BUF_SIZE, &actlen)) { 1029 1030 DPRINTF("actlen = %d\n", actlen); 1031 1032 usbd_xfer_set_frame_len(xfer, 0, actlen); 1033 usbd_transfer_submit(xfer); 1034 } 1035 return; 1036 1037 default: /* Error */ 1038 if (error != USB_ERR_CANCELLED) { 1039 /* try to clear stall first */ 1040 usbd_xfer_set_stall(xfer); 1041 goto tr_setup; 1042 } 1043 return; 1044 } 1045 } 1046 1047 static void 1048 uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error) 1049 { 1050 struct uplcom_softc *sc = usbd_xfer_softc(xfer); 1051 struct usb_page_cache *pc; 1052 int actlen; 1053 1054 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1055 1056 switch (USB_GET_STATE(xfer)) { 1057 case USB_ST_TRANSFERRED: 1058 pc = usbd_xfer_get_frame(xfer, 0); 1059 ucom_put_data(&sc->sc_ucom, pc, 0, actlen); 1060 1061 case USB_ST_SETUP: 1062 tr_setup: 1063 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1064 usbd_transfer_submit(xfer); 1065 return; 1066 1067 default: /* Error */ 1068 if (error != USB_ERR_CANCELLED) { 1069 /* try to clear stall first */ 1070 usbd_xfer_set_stall(xfer); 1071 goto tr_setup; 1072 } 1073 return; 1074 } 1075 } 1076 1077 static void 1078 uplcom_poll(struct ucom_softc *ucom) 1079 { 1080 struct uplcom_softc *sc = ucom->sc_parent; 1081 usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER); 1082 } 1083