1 /* $NetBSD: uplcom.c,v 1.21 2001/11/13 06:24:56 lukem Exp $ */ 2 3 #include <sys/cdefs.h> 4 __FBSDID("$FreeBSD$"); 5 6 /*- 7 * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 2001 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Ichiro FUKUHARA (ichiro@ichiro.org). 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. All advertising materials mentioning features or use of this software 48 * must display the following acknowledgement: 49 * This product includes software developed by the NetBSD 50 * Foundation, Inc. and its contributors. 51 * 4. Neither the name of The NetBSD Foundation nor the names of its 52 * contributors may be used to endorse or promote products derived 53 * from this software without specific prior written permission. 54 * 55 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 56 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 57 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 58 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 59 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 60 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 61 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 62 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 63 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 64 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 65 * POSSIBILITY OF SUCH DAMAGE. 66 */ 67 68 /* 69 * This driver supports several USB-to-RS232 serial adapters driven by 70 * Prolific PL-2303, PL-2303X and probably PL-2303HX USB-to-RS232 71 * bridge chip. The adapters are sold under many different brand 72 * names. 73 * 74 * Datasheets are available at Prolific www site at 75 * http://www.prolific.com.tw. The datasheets don't contain full 76 * programming information for the chip. 77 * 78 * PL-2303HX is probably programmed the same as PL-2303X. 79 * 80 * There are several differences between PL-2303 and PL-2303(H)X. 81 * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_ 82 * different command for controlling CRTSCTS and needs special 83 * sequence of commands for initialization which aren't also 84 * documented in the datasheet. 85 */ 86 87 #include "usbdevs.h" 88 #include <dev/usb/usb.h> 89 #include <dev/usb/usb_mfunc.h> 90 #include <dev/usb/usb_error.h> 91 #include <dev/usb/usb_cdc.h> 92 93 #define USB_DEBUG_VAR uplcom_debug 94 95 #include <dev/usb/usb_core.h> 96 #include <dev/usb/usb_debug.h> 97 #include <dev/usb/usb_process.h> 98 #include <dev/usb/usb_request.h> 99 #include <dev/usb/usb_lookup.h> 100 #include <dev/usb/usb_util.h> 101 #include <dev/usb/usb_busdma.h> 102 103 #include <dev/usb/serial/usb_serial.h> 104 105 #if USB_DEBUG 106 static int uplcom_debug = 0; 107 108 SYSCTL_NODE(_hw_usb2, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom"); 109 SYSCTL_INT(_hw_usb2_uplcom, OID_AUTO, debug, CTLFLAG_RW, 110 &uplcom_debug, 0, "Debug level"); 111 #endif 112 113 #define UPLCOM_MODVER 1 /* module version */ 114 115 #define UPLCOM_CONFIG_INDEX 0 116 #define UPLCOM_IFACE_INDEX 0 117 #define UPLCOM_SECOND_IFACE_INDEX 1 118 119 #ifndef UPLCOM_INTR_INTERVAL 120 #define UPLCOM_INTR_INTERVAL 0 /* default */ 121 #endif 122 123 #define UPLCOM_BULK_BUF_SIZE 1024 /* bytes */ 124 125 #define UPLCOM_SET_REQUEST 0x01 126 #define UPLCOM_SET_CRTSCTS 0x41 127 #define UPLCOM_SET_CRTSCTS_PL2303X 0x61 128 #define RSAQ_STATUS_CTS 0x80 129 #define RSAQ_STATUS_DSR 0x02 130 #define RSAQ_STATUS_DCD 0x01 131 132 #define TYPE_PL2303 0 133 #define TYPE_PL2303X 1 134 135 enum { 136 UPLCOM_BULK_DT_WR, 137 UPLCOM_BULK_DT_RD, 138 UPLCOM_INTR_DT_RD, 139 UPLCOM_N_TRANSFER, 140 }; 141 142 struct uplcom_softc { 143 struct usb2_com_super_softc sc_super_ucom; 144 struct usb2_com_softc sc_ucom; 145 146 struct usb2_xfer *sc_xfer[UPLCOM_N_TRANSFER]; 147 struct usb2_device *sc_udev; 148 149 uint16_t sc_line; 150 151 uint8_t sc_lsr; /* local status register */ 152 uint8_t sc_msr; /* uplcom status register */ 153 uint8_t sc_chiptype; /* type of chip */ 154 uint8_t sc_ctrl_iface_no; 155 uint8_t sc_data_iface_no; 156 uint8_t sc_iface_index[2]; 157 }; 158 159 /* prototypes */ 160 161 static usb2_error_t uplcom_reset(struct uplcom_softc *, struct usb2_device *); 162 static int uplcom_pl2303x_init(struct usb2_device *); 163 static void uplcom_cfg_set_dtr(struct usb2_com_softc *, uint8_t); 164 static void uplcom_cfg_set_rts(struct usb2_com_softc *, uint8_t); 165 static void uplcom_cfg_set_break(struct usb2_com_softc *, uint8_t); 166 static int uplcom_pre_param(struct usb2_com_softc *, struct termios *); 167 static void uplcom_cfg_param(struct usb2_com_softc *, struct termios *); 168 static void uplcom_start_read(struct usb2_com_softc *); 169 static void uplcom_stop_read(struct usb2_com_softc *); 170 static void uplcom_start_write(struct usb2_com_softc *); 171 static void uplcom_stop_write(struct usb2_com_softc *); 172 static void uplcom_cfg_get_status(struct usb2_com_softc *, uint8_t *, 173 uint8_t *); 174 175 static device_probe_t uplcom_probe; 176 static device_attach_t uplcom_attach; 177 static device_detach_t uplcom_detach; 178 179 static usb2_callback_t uplcom_intr_callback; 180 static usb2_callback_t uplcom_write_callback; 181 static usb2_callback_t uplcom_read_callback; 182 183 static const struct usb2_config uplcom_config_data[UPLCOM_N_TRANSFER] = { 184 185 [UPLCOM_BULK_DT_WR] = { 186 .type = UE_BULK, 187 .endpoint = UE_ADDR_ANY, 188 .direction = UE_DIR_OUT, 189 .mh.bufsize = UPLCOM_BULK_BUF_SIZE, 190 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 191 .mh.callback = &uplcom_write_callback, 192 .if_index = 0, 193 }, 194 195 [UPLCOM_BULK_DT_RD] = { 196 .type = UE_BULK, 197 .endpoint = UE_ADDR_ANY, 198 .direction = UE_DIR_IN, 199 .mh.bufsize = UPLCOM_BULK_BUF_SIZE, 200 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 201 .mh.callback = &uplcom_read_callback, 202 .if_index = 0, 203 }, 204 205 [UPLCOM_INTR_DT_RD] = { 206 .type = UE_INTERRUPT, 207 .endpoint = UE_ADDR_ANY, 208 .direction = UE_DIR_IN, 209 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 210 .mh.bufsize = 0, /* use wMaxPacketSize */ 211 .mh.callback = &uplcom_intr_callback, 212 .if_index = 1, 213 }, 214 }; 215 216 struct usb2_com_callback uplcom_callback = { 217 .usb2_com_cfg_get_status = &uplcom_cfg_get_status, 218 .usb2_com_cfg_set_dtr = &uplcom_cfg_set_dtr, 219 .usb2_com_cfg_set_rts = &uplcom_cfg_set_rts, 220 .usb2_com_cfg_set_break = &uplcom_cfg_set_break, 221 .usb2_com_cfg_param = &uplcom_cfg_param, 222 .usb2_com_pre_param = &uplcom_pre_param, 223 .usb2_com_start_read = &uplcom_start_read, 224 .usb2_com_stop_read = &uplcom_stop_read, 225 .usb2_com_start_write = &uplcom_start_write, 226 .usb2_com_stop_write = &uplcom_stop_write, 227 }; 228 229 #define USB_UPL(v,p,rl,rh,t) \ 230 USB_VENDOR(v), USB_PRODUCT(p), USB_DEV_BCD_GTEQ(rl), \ 231 USB_DEV_BCD_LTEQ(rh), USB_DRIVER_INFO(t) 232 233 static const struct usb2_device_id uplcom_devs[] = { 234 /* Belkin F5U257 */ 235 {USB_UPL(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U257, 0, 0xFFFF, TYPE_PL2303X)}, 236 /* I/O DATA USB-RSAQ */ 237 {USB_UPL(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ, 0, 0xFFFF, TYPE_PL2303)}, 238 /* I/O DATA USB-RSAQ2 */ 239 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2, 0, 0xFFFF, TYPE_PL2303)}, 240 /* I/O DATA USB-RSAQ3 */ 241 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3, 0, 0xFFFF, TYPE_PL2303X)}, 242 /* PLANEX USB-RS232 URS-03 */ 243 {USB_UPL(USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A, 0, 0xFFFF, TYPE_PL2303)}, 244 /* TrendNet TU-S9 */ 245 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0x0400, 0xFFFF, TYPE_PL2303X)}, 246 /* ST Lab USB-SERIAL-4 */ 247 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0x0300, 0x03FF, TYPE_PL2303X)}, 248 /* IOGEAR/ATEN UC-232A (also ST Lab USB-SERIAL-1) */ 249 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0, 0x02FF, TYPE_PL2303)}, 250 /* TDK USB-PHS Adapter UHA6400 */ 251 {USB_UPL(USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400, 0, 0xFFFF, TYPE_PL2303)}, 252 /* RATOC REX-USB60 */ 253 {USB_UPL(USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60, 0, 0xFFFF, TYPE_PL2303)}, 254 /* ELECOM UC-SGT */ 255 {USB_UPL(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT, 0, 0xFFFF, TYPE_PL2303)}, 256 {USB_UPL(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0, 0, 0xFFFF, TYPE_PL2303)}, 257 /* Sagem USB-Serial Controller */ 258 {USB_UPL(USB_VENDOR_SAGEM, USB_PRODUCT_SAGEM_USBSERIAL, 0, 0xFFFF, TYPE_PL2303X)}, 259 /* Sony Ericsson USB Cable */ 260 {USB_UPL(USB_VENDOR_SONYERICSSON, USB_PRODUCT_SONYERICSSON_DCU10, 0, 0xFFFF, TYPE_PL2303)}, 261 /* SOURCENEXT KeikaiDenwa 8 */ 262 {USB_UPL(USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8, 0, 0xFFFF, TYPE_PL2303)}, 263 /* SOURCENEXT KeikaiDenwa 8 with charger */ 264 {USB_UPL(USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG, 0, 0, TYPE_PL2303)}, 265 /* HAL Corporation Crossam2+USB */ 266 {USB_UPL(USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001, 0, 0xFFFF, TYPE_PL2303)}, 267 /* Sitecom USB to Serial */ 268 {USB_UPL(USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_SERIAL, 0, 0xFFFF, TYPE_PL2303)}, 269 /* Tripp-Lite U209-000-R */ 270 {USB_UPL(USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209, 0, 0xFFFF, TYPE_PL2303X)}, 271 {USB_UPL(USB_VENDOR_RADIOSHACK, USB_PRODUCT_RADIOSHACK_USBCABLE, 0, 0xFFFF, TYPE_PL2303)}, 272 /* Prolific Pharos */ 273 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PHAROS, 0, 0xFFFF, TYPE_PL2303)}, 274 /* Willcom W-SIM */ 275 {USB_UPL(USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_WSIM, 0, 0xFFFF, TYPE_PL2303X)}, 276 }; 277 278 static device_method_t uplcom_methods[] = { 279 DEVMETHOD(device_probe, uplcom_probe), 280 DEVMETHOD(device_attach, uplcom_attach), 281 DEVMETHOD(device_detach, uplcom_detach), 282 {0, 0} 283 }; 284 285 static devclass_t uplcom_devclass; 286 287 static driver_t uplcom_driver = { 288 .name = "uplcom", 289 .methods = uplcom_methods, 290 .size = sizeof(struct uplcom_softc), 291 }; 292 293 DRIVER_MODULE(uplcom, ushub, uplcom_driver, uplcom_devclass, NULL, 0); 294 MODULE_DEPEND(uplcom, ucom, 1, 1, 1); 295 MODULE_DEPEND(uplcom, usb, 1, 1, 1); 296 MODULE_VERSION(uplcom, UPLCOM_MODVER); 297 298 static int 299 uplcom_probe(device_t dev) 300 { 301 struct usb2_attach_arg *uaa = device_get_ivars(dev); 302 303 DPRINTFN(11, "\n"); 304 305 if (uaa->usb2_mode != USB_MODE_HOST) { 306 return (ENXIO); 307 } 308 if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) { 309 return (ENXIO); 310 } 311 if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) { 312 return (ENXIO); 313 } 314 return (usb2_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa)); 315 } 316 317 static int 318 uplcom_attach(device_t dev) 319 { 320 struct usb2_attach_arg *uaa = device_get_ivars(dev); 321 struct uplcom_softc *sc = device_get_softc(dev); 322 struct usb2_interface *iface; 323 struct usb2_interface_descriptor *id; 324 int error; 325 326 DPRINTFN(11, "\n"); 327 328 device_set_usb2_desc(dev); 329 330 DPRINTF("sc = %p\n", sc); 331 332 sc->sc_chiptype = USB_GET_DRIVER_INFO(uaa); 333 sc->sc_udev = uaa->device; 334 335 DPRINTF("chiptype: %s\n", 336 (sc->sc_chiptype == TYPE_PL2303X) ? 337 "2303X" : "2303"); 338 339 /* 340 * USB-RSAQ1 has two interface 341 * 342 * USB-RSAQ1 | USB-RSAQ2 343 * -----------------+----------------- 344 * Interface 0 |Interface 0 345 * Interrupt(0x81) | Interrupt(0x81) 346 * -----------------+ BulkIN(0x02) 347 * Interface 1 | BulkOUT(0x83) 348 * BulkIN(0x02) | 349 * BulkOUT(0x83) | 350 */ 351 352 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum; 353 sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX; 354 355 iface = usb2_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX); 356 if (iface) { 357 id = usb2_get_interface_descriptor(iface); 358 if (id == NULL) { 359 device_printf(dev, "no interface descriptor (2)!\n"); 360 goto detach; 361 } 362 sc->sc_data_iface_no = id->bInterfaceNumber; 363 sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX; 364 usb2_set_parent_iface(uaa->device, 365 UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex); 366 } else { 367 sc->sc_data_iface_no = sc->sc_ctrl_iface_no; 368 sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX; 369 } 370 371 error = usb2_transfer_setup(uaa->device, 372 sc->sc_iface_index, sc->sc_xfer, uplcom_config_data, 373 UPLCOM_N_TRANSFER, sc, &Giant); 374 if (error) { 375 DPRINTF("one or more missing USB endpoints, " 376 "error=%s\n", usb2_errstr(error)); 377 goto detach; 378 } 379 error = uplcom_reset(sc, uaa->device); 380 if (error) { 381 device_printf(dev, "reset failed, error=%s\n", 382 usb2_errstr(error)); 383 goto detach; 384 } 385 /* clear stall at first run */ 386 usb2_transfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 387 usb2_transfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 388 389 error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 390 &uplcom_callback, &Giant); 391 if (error) { 392 goto detach; 393 } 394 /* 395 * do the initialization during attach so that the system does not 396 * sleep during open: 397 */ 398 if (sc->sc_chiptype == TYPE_PL2303X) { 399 if (uplcom_pl2303x_init(uaa->device)) { 400 device_printf(dev, "init failed!\n"); 401 goto detach; 402 } 403 } 404 return (0); 405 406 detach: 407 uplcom_detach(dev); 408 return (ENXIO); 409 } 410 411 static int 412 uplcom_detach(device_t dev) 413 { 414 struct uplcom_softc *sc = device_get_softc(dev); 415 416 DPRINTF("sc=%p\n", sc); 417 418 usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1); 419 420 usb2_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER); 421 422 return (0); 423 } 424 425 static usb2_error_t 426 uplcom_reset(struct uplcom_softc *sc, struct usb2_device *udev) 427 { 428 struct usb2_device_request req; 429 430 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 431 req.bRequest = UPLCOM_SET_REQUEST; 432 USETW(req.wValue, 0); 433 req.wIndex[0] = sc->sc_data_iface_no; 434 req.wIndex[1] = 0; 435 USETW(req.wLength, 0); 436 437 return (usb2_do_request(udev, &Giant, &req, NULL)); 438 } 439 440 struct pl2303x_init { 441 uint8_t req_type; 442 uint8_t request; 443 uint16_t value; 444 uint16_t index; 445 uint16_t length; 446 }; 447 448 static const struct pl2303x_init pl2303x[] = { 449 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1}, 450 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0}, 451 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1}, 452 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1}, 453 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1}, 454 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0}, 455 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1}, 456 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1}, 457 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0}, 458 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0}, 459 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0}, 460 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 8, 0, 0}, 461 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 9, 0, 0}, 462 }; 463 464 #define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0])) 465 466 static int 467 uplcom_pl2303x_init(struct usb2_device *udev) 468 { 469 struct usb2_device_request req; 470 usb2_error_t err; 471 uint8_t buf[4]; 472 uint8_t i; 473 474 for (i = 0; i != N_PL2302X_INIT; i++) { 475 req.bmRequestType = pl2303x[i].req_type; 476 req.bRequest = pl2303x[i].request; 477 USETW(req.wValue, pl2303x[i].value); 478 USETW(req.wIndex, pl2303x[i].index); 479 USETW(req.wLength, pl2303x[i].length); 480 481 err = usb2_do_request(udev, &Giant, &req, buf); 482 if (err) { 483 DPRINTF("error=%s\n", usb2_errstr(err)); 484 return (EIO); 485 } 486 } 487 return (0); 488 } 489 490 static void 491 uplcom_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff) 492 { 493 struct uplcom_softc *sc = ucom->sc_parent; 494 struct usb2_device_request req; 495 496 DPRINTF("onoff = %d\n", onoff); 497 498 if (onoff) 499 sc->sc_line |= UCDC_LINE_DTR; 500 else 501 sc->sc_line &= ~UCDC_LINE_DTR; 502 503 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 504 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 505 USETW(req.wValue, sc->sc_line); 506 req.wIndex[0] = sc->sc_data_iface_no; 507 req.wIndex[1] = 0; 508 USETW(req.wLength, 0); 509 510 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 511 &req, NULL, 0, 1000); 512 } 513 514 static void 515 uplcom_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff) 516 { 517 struct uplcom_softc *sc = ucom->sc_parent; 518 struct usb2_device_request req; 519 520 DPRINTF("onoff = %d\n", onoff); 521 522 if (onoff) 523 sc->sc_line |= UCDC_LINE_RTS; 524 else 525 sc->sc_line &= ~UCDC_LINE_RTS; 526 527 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 528 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 529 USETW(req.wValue, sc->sc_line); 530 req.wIndex[0] = sc->sc_data_iface_no; 531 req.wIndex[1] = 0; 532 USETW(req.wLength, 0); 533 534 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 535 &req, NULL, 0, 1000); 536 } 537 538 static void 539 uplcom_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff) 540 { 541 struct uplcom_softc *sc = ucom->sc_parent; 542 struct usb2_device_request req; 543 uint16_t temp; 544 545 DPRINTF("onoff = %d\n", onoff); 546 547 temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF); 548 549 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 550 req.bRequest = UCDC_SEND_BREAK; 551 USETW(req.wValue, temp); 552 req.wIndex[0] = sc->sc_data_iface_no; 553 req.wIndex[1] = 0; 554 USETW(req.wLength, 0); 555 556 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 557 &req, NULL, 0, 1000); 558 } 559 560 static const int32_t uplcom_rates[] = { 561 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400, 562 19200, 28800, 38400, 57600, 115200, 563 /* 564 * Higher speeds are probably possible. PL2303X supports up to 565 * 6Mb and can set any rate 566 */ 567 230400, 460800, 614400, 921600, 1228800 568 }; 569 570 #define N_UPLCOM_RATES (sizeof(uplcom_rates)/sizeof(uplcom_rates[0])) 571 572 static int 573 uplcom_pre_param(struct usb2_com_softc *ucom, struct termios *t) 574 { 575 uint8_t i; 576 577 DPRINTF("\n"); 578 579 /* check requested baud rate */ 580 581 for (i = 0;; i++) { 582 583 if (i != N_UPLCOM_RATES) { 584 if (uplcom_rates[i] == t->c_ospeed) { 585 break; 586 } 587 } else { 588 DPRINTF("invalid baud rate (%d)\n", t->c_ospeed); 589 return (EIO); 590 } 591 } 592 593 return (0); 594 } 595 596 static void 597 uplcom_cfg_param(struct usb2_com_softc *ucom, struct termios *t) 598 { 599 struct uplcom_softc *sc = ucom->sc_parent; 600 struct usb2_cdc_line_state ls; 601 struct usb2_device_request req; 602 603 DPRINTF("sc = %p\n", sc); 604 605 bzero(&ls, sizeof(ls)); 606 607 USETDW(ls.dwDTERate, t->c_ospeed); 608 609 if (t->c_cflag & CSTOPB) { 610 ls.bCharFormat = UCDC_STOP_BIT_2; 611 } else { 612 ls.bCharFormat = UCDC_STOP_BIT_1; 613 } 614 615 if (t->c_cflag & PARENB) { 616 if (t->c_cflag & PARODD) { 617 ls.bParityType = UCDC_PARITY_ODD; 618 } else { 619 ls.bParityType = UCDC_PARITY_EVEN; 620 } 621 } else { 622 ls.bParityType = UCDC_PARITY_NONE; 623 } 624 625 switch (t->c_cflag & CSIZE) { 626 case CS5: 627 ls.bDataBits = 5; 628 break; 629 case CS6: 630 ls.bDataBits = 6; 631 break; 632 case CS7: 633 ls.bDataBits = 7; 634 break; 635 case CS8: 636 ls.bDataBits = 8; 637 break; 638 } 639 640 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n", 641 UGETDW(ls.dwDTERate), ls.bCharFormat, 642 ls.bParityType, ls.bDataBits); 643 644 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 645 req.bRequest = UCDC_SET_LINE_CODING; 646 USETW(req.wValue, 0); 647 req.wIndex[0] = sc->sc_data_iface_no; 648 req.wIndex[1] = 0; 649 USETW(req.wLength, UCDC_LINE_STATE_LENGTH); 650 651 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 652 &req, &ls, 0, 1000); 653 654 if (t->c_cflag & CRTSCTS) { 655 656 DPRINTF("crtscts = on\n"); 657 658 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 659 req.bRequest = UPLCOM_SET_REQUEST; 660 USETW(req.wValue, 0); 661 if (sc->sc_chiptype == TYPE_PL2303X) 662 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X); 663 else 664 USETW(req.wIndex, UPLCOM_SET_CRTSCTS); 665 USETW(req.wLength, 0); 666 667 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 668 &req, NULL, 0, 1000); 669 } else { 670 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 671 req.bRequest = UPLCOM_SET_REQUEST; 672 USETW(req.wValue, 0); 673 USETW(req.wIndex, 0); 674 USETW(req.wLength, 0); 675 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 676 &req, NULL, 0, 1000); 677 } 678 } 679 680 static void 681 uplcom_start_read(struct usb2_com_softc *ucom) 682 { 683 struct uplcom_softc *sc = ucom->sc_parent; 684 685 /* start interrupt endpoint */ 686 usb2_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]); 687 688 /* start read endpoint */ 689 usb2_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 690 } 691 692 static void 693 uplcom_stop_read(struct usb2_com_softc *ucom) 694 { 695 struct uplcom_softc *sc = ucom->sc_parent; 696 697 /* stop interrupt endpoint */ 698 usb2_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]); 699 700 /* stop read endpoint */ 701 usb2_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 702 } 703 704 static void 705 uplcom_start_write(struct usb2_com_softc *ucom) 706 { 707 struct uplcom_softc *sc = ucom->sc_parent; 708 709 usb2_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 710 } 711 712 static void 713 uplcom_stop_write(struct usb2_com_softc *ucom) 714 { 715 struct uplcom_softc *sc = ucom->sc_parent; 716 717 usb2_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 718 } 719 720 static void 721 uplcom_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr) 722 { 723 struct uplcom_softc *sc = ucom->sc_parent; 724 725 DPRINTF("\n"); 726 727 *lsr = sc->sc_lsr; 728 *msr = sc->sc_msr; 729 } 730 731 static void 732 uplcom_intr_callback(struct usb2_xfer *xfer) 733 { 734 struct uplcom_softc *sc = xfer->priv_sc; 735 uint8_t buf[9]; 736 737 switch (USB_GET_STATE(xfer)) { 738 case USB_ST_TRANSFERRED: 739 740 DPRINTF("actlen = %u\n", xfer->actlen); 741 742 if (xfer->actlen >= 9) { 743 744 usb2_copy_out(xfer->frbuffers, 0, buf, sizeof(buf)); 745 746 DPRINTF("status = 0x%02x\n", buf[8]); 747 748 sc->sc_lsr = 0; 749 sc->sc_msr = 0; 750 751 if (buf[8] & RSAQ_STATUS_CTS) { 752 sc->sc_msr |= SER_CTS; 753 } 754 if (buf[8] & RSAQ_STATUS_DSR) { 755 sc->sc_msr |= SER_DSR; 756 } 757 if (buf[8] & RSAQ_STATUS_DCD) { 758 sc->sc_msr |= SER_DCD; 759 } 760 usb2_com_status_change(&sc->sc_ucom); 761 } 762 case USB_ST_SETUP: 763 tr_setup: 764 xfer->frlengths[0] = xfer->max_data_length; 765 usb2_start_hardware(xfer); 766 return; 767 768 default: /* Error */ 769 if (xfer->error != USB_ERR_CANCELLED) { 770 /* try to clear stall first */ 771 xfer->flags.stall_pipe = 1; 772 goto tr_setup; 773 } 774 return; 775 } 776 } 777 778 static void 779 uplcom_write_callback(struct usb2_xfer *xfer) 780 { 781 struct uplcom_softc *sc = xfer->priv_sc; 782 uint32_t actlen; 783 784 switch (USB_GET_STATE(xfer)) { 785 case USB_ST_SETUP: 786 case USB_ST_TRANSFERRED: 787 tr_setup: 788 if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0, 789 UPLCOM_BULK_BUF_SIZE, &actlen)) { 790 791 DPRINTF("actlen = %d\n", actlen); 792 793 xfer->frlengths[0] = actlen; 794 usb2_start_hardware(xfer); 795 } 796 return; 797 798 default: /* Error */ 799 if (xfer->error != USB_ERR_CANCELLED) { 800 /* try to clear stall first */ 801 xfer->flags.stall_pipe = 1; 802 goto tr_setup; 803 } 804 return; 805 } 806 } 807 808 static void 809 uplcom_read_callback(struct usb2_xfer *xfer) 810 { 811 struct uplcom_softc *sc = xfer->priv_sc; 812 813 switch (USB_GET_STATE(xfer)) { 814 case USB_ST_TRANSFERRED: 815 usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers, 0, xfer->actlen); 816 817 case USB_ST_SETUP: 818 tr_setup: 819 xfer->frlengths[0] = xfer->max_data_length; 820 usb2_start_hardware(xfer); 821 return; 822 823 default: /* Error */ 824 if (xfer->error != USB_ERR_CANCELLED) { 825 /* try to clear stall first */ 826 xfer->flags.stall_pipe = 1; 827 goto tr_setup; 828 } 829 return; 830 } 831 } 832