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