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