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, NULL); 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 static int 615 uplcom_pre_param(struct ucom_softc *ucom, struct termios *t) 616 { 617 struct uplcom_softc *sc = ucom->sc_parent; 618 uint8_t i; 619 620 DPRINTF("\n"); 621 622 /** 623 * Check requested baud rate. 624 * 625 * The PL2303 can only set specific baud rates, up to 1228800 baud. 626 * The PL2303X can set any baud rate up to 6Mb. 627 * The PL2303HX rev. D can set any baud rate up to 12Mb. 628 * 629 * XXX: We currently cannot identify the PL2303HX rev. D, so treat 630 * it the same as the PL2303X. 631 */ 632 if (sc->sc_chiptype != TYPE_PL2303HX) { 633 for (i = 0; i < NELEM(uplcom_rates); i++) { 634 if (uplcom_rates[i] == t->c_ospeed) 635 return (0); 636 } 637 } else { 638 if (t->c_ospeed <= 6000000) 639 return (0); 640 } 641 642 DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed); 643 return (EIO); 644 } 645 646 static void 647 uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t) 648 { 649 struct uplcom_softc *sc = ucom->sc_parent; 650 struct usb_cdc_line_state ls; 651 struct usb_device_request req; 652 653 DPRINTF("sc = %p\n", sc); 654 655 memset(&ls, 0, sizeof(ls)); 656 657 USETDW(ls.dwDTERate, t->c_ospeed); 658 659 if (t->c_cflag & CSTOPB) { 660 ls.bCharFormat = UCDC_STOP_BIT_2; 661 } else { 662 ls.bCharFormat = UCDC_STOP_BIT_1; 663 } 664 665 if (t->c_cflag & PARENB) { 666 if (t->c_cflag & PARODD) { 667 ls.bParityType = UCDC_PARITY_ODD; 668 } else { 669 ls.bParityType = UCDC_PARITY_EVEN; 670 } 671 } else { 672 ls.bParityType = UCDC_PARITY_NONE; 673 } 674 675 switch (t->c_cflag & CSIZE) { 676 case CS5: 677 ls.bDataBits = 5; 678 break; 679 case CS6: 680 ls.bDataBits = 6; 681 break; 682 case CS7: 683 ls.bDataBits = 7; 684 break; 685 case CS8: 686 ls.bDataBits = 8; 687 break; 688 } 689 690 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n", 691 UGETDW(ls.dwDTERate), ls.bCharFormat, 692 ls.bParityType, ls.bDataBits); 693 694 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 695 req.bRequest = UCDC_SET_LINE_CODING; 696 USETW(req.wValue, 0); 697 req.wIndex[0] = sc->sc_data_iface_no; 698 req.wIndex[1] = 0; 699 USETW(req.wLength, UCDC_LINE_STATE_LENGTH); 700 701 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 702 &req, &ls, 0, 1000); 703 704 if (t->c_cflag & CRTSCTS) { 705 706 DPRINTF("crtscts = on\n"); 707 708 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 709 req.bRequest = UPLCOM_SET_REQUEST; 710 USETW(req.wValue, 0); 711 if (sc->sc_chiptype == TYPE_PL2303HX) 712 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X); 713 else 714 USETW(req.wIndex, UPLCOM_SET_CRTSCTS); 715 USETW(req.wLength, 0); 716 717 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 718 &req, NULL, 0, 1000); 719 } else { 720 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 721 req.bRequest = UPLCOM_SET_REQUEST; 722 USETW(req.wValue, 0); 723 USETW(req.wIndex, 0); 724 USETW(req.wLength, 0); 725 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 726 &req, NULL, 0, 1000); 727 } 728 } 729 730 static void 731 uplcom_start_read(struct ucom_softc *ucom) 732 { 733 struct uplcom_softc *sc = ucom->sc_parent; 734 735 /* start interrupt endpoint */ 736 usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]); 737 738 /* start read endpoint */ 739 usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 740 } 741 742 static void 743 uplcom_stop_read(struct ucom_softc *ucom) 744 { 745 struct uplcom_softc *sc = ucom->sc_parent; 746 747 /* stop interrupt endpoint */ 748 usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]); 749 750 /* stop read endpoint */ 751 usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 752 } 753 754 static void 755 uplcom_start_write(struct ucom_softc *ucom) 756 { 757 struct uplcom_softc *sc = ucom->sc_parent; 758 759 usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 760 } 761 762 static void 763 uplcom_stop_write(struct ucom_softc *ucom) 764 { 765 struct uplcom_softc *sc = ucom->sc_parent; 766 767 usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 768 } 769 770 static void 771 uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 772 { 773 struct uplcom_softc *sc = ucom->sc_parent; 774 775 DPRINTF("\n"); 776 777 *lsr = sc->sc_lsr; 778 *msr = sc->sc_msr; 779 } 780 781 static void 782 uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error) 783 { 784 struct uplcom_softc *sc = usbd_xfer_softc(xfer); 785 struct usb_page_cache *pc; 786 uint8_t buf[9]; 787 int actlen; 788 789 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 790 791 switch (USB_GET_STATE(xfer)) { 792 case USB_ST_TRANSFERRED: 793 794 DPRINTF("actlen = %u\n", actlen); 795 796 if (actlen >= 9) { 797 798 pc = usbd_xfer_get_frame(xfer, 0); 799 usbd_copy_out(pc, 0, buf, sizeof(buf)); 800 801 DPRINTF("status = 0x%02x\n", buf[8]); 802 803 sc->sc_lsr = 0; 804 sc->sc_msr = 0; 805 806 if (buf[8] & RSAQ_STATUS_CTS) { 807 sc->sc_msr |= SER_CTS; 808 } 809 if (buf[8] & RSAQ_STATUS_DSR) { 810 sc->sc_msr |= SER_DSR; 811 } 812 if (buf[8] & RSAQ_STATUS_DCD) { 813 sc->sc_msr |= SER_DCD; 814 } 815 ucom_status_change(&sc->sc_ucom); 816 } 817 case USB_ST_SETUP: 818 tr_setup: 819 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 820 usbd_transfer_submit(xfer); 821 return; 822 823 default: /* Error */ 824 if (error != USB_ERR_CANCELLED) { 825 /* try to clear stall first */ 826 usbd_xfer_set_stall(xfer); 827 goto tr_setup; 828 } 829 return; 830 } 831 } 832 833 static void 834 uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error) 835 { 836 struct uplcom_softc *sc = usbd_xfer_softc(xfer); 837 struct usb_page_cache *pc; 838 uint32_t actlen; 839 840 switch (USB_GET_STATE(xfer)) { 841 case USB_ST_SETUP: 842 case USB_ST_TRANSFERRED: 843 tr_setup: 844 pc = usbd_xfer_get_frame(xfer, 0); 845 if (ucom_get_data(&sc->sc_ucom, pc, 0, 846 UPLCOM_BULK_BUF_SIZE, &actlen)) { 847 848 DPRINTF("actlen = %d\n", actlen); 849 850 usbd_xfer_set_frame_len(xfer, 0, actlen); 851 usbd_transfer_submit(xfer); 852 } 853 return; 854 855 default: /* Error */ 856 if (error != USB_ERR_CANCELLED) { 857 /* try to clear stall first */ 858 usbd_xfer_set_stall(xfer); 859 goto tr_setup; 860 } 861 return; 862 } 863 } 864 865 static void 866 uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error) 867 { 868 struct uplcom_softc *sc = usbd_xfer_softc(xfer); 869 struct usb_page_cache *pc; 870 int actlen; 871 872 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 873 874 switch (USB_GET_STATE(xfer)) { 875 case USB_ST_TRANSFERRED: 876 pc = usbd_xfer_get_frame(xfer, 0); 877 ucom_put_data(&sc->sc_ucom, pc, 0, actlen); 878 879 case USB_ST_SETUP: 880 tr_setup: 881 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 882 usbd_transfer_submit(xfer); 883 return; 884 885 default: /* Error */ 886 if (error != USB_ERR_CANCELLED) { 887 /* try to clear stall first */ 888 usbd_xfer_set_stall(xfer); 889 goto tr_setup; 890 } 891 return; 892 } 893 } 894 895 static void 896 uplcom_poll(struct ucom_softc *ucom) 897 { 898 struct uplcom_softc *sc = ucom->sc_parent; 899 usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER); 900 } 901