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