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