uplcom.c revision 189360
1/* $NetBSD: uplcom.c,v 1.21 2001/11/13 06:24:56 lukem Exp $ */ 2 3#include <sys/cdefs.h> 4__FBSDID("$FreeBSD: head/sys/dev/usb/serial/uplcom.c 189360 2009-03-04 03:47:57Z thompsa $"); 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 106static int uplcom_debug = 0; 107 108SYSCTL_NODE(_hw_usb2, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom"); 109SYSCTL_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 135enum { 136 UPLCOM_BULK_DT_WR, 137 UPLCOM_BULK_DT_RD, 138 UPLCOM_INTR_DT_RD, 139 UPLCOM_N_TRANSFER, 140}; 141 142struct 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 struct mtx sc_mtx; 149 150 uint16_t sc_line; 151 152 uint8_t sc_lsr; /* local status register */ 153 uint8_t sc_msr; /* uplcom status register */ 154 uint8_t sc_chiptype; /* type of chip */ 155 uint8_t sc_ctrl_iface_no; 156 uint8_t sc_data_iface_no; 157 uint8_t sc_iface_index[2]; 158}; 159 160/* prototypes */ 161 162static usb2_error_t uplcom_reset(struct uplcom_softc *, struct usb2_device *); 163static int uplcom_pl2303x_init(struct usb2_device *); 164static void uplcom_cfg_set_dtr(struct usb2_com_softc *, uint8_t); 165static void uplcom_cfg_set_rts(struct usb2_com_softc *, uint8_t); 166static void uplcom_cfg_set_break(struct usb2_com_softc *, uint8_t); 167static int uplcom_pre_param(struct usb2_com_softc *, struct termios *); 168static void uplcom_cfg_param(struct usb2_com_softc *, struct termios *); 169static void uplcom_start_read(struct usb2_com_softc *); 170static void uplcom_stop_read(struct usb2_com_softc *); 171static void uplcom_start_write(struct usb2_com_softc *); 172static void uplcom_stop_write(struct usb2_com_softc *); 173static void uplcom_cfg_get_status(struct usb2_com_softc *, uint8_t *, 174 uint8_t *); 175 176static device_probe_t uplcom_probe; 177static device_attach_t uplcom_attach; 178static device_detach_t uplcom_detach; 179 180static usb2_callback_t uplcom_intr_callback; 181static usb2_callback_t uplcom_write_callback; 182static usb2_callback_t uplcom_read_callback; 183 184static const struct usb2_config uplcom_config_data[UPLCOM_N_TRANSFER] = { 185 186 [UPLCOM_BULK_DT_WR] = { 187 .type = UE_BULK, 188 .endpoint = UE_ADDR_ANY, 189 .direction = UE_DIR_OUT, 190 .mh.bufsize = UPLCOM_BULK_BUF_SIZE, 191 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 192 .mh.callback = &uplcom_write_callback, 193 .if_index = 0, 194 }, 195 196 [UPLCOM_BULK_DT_RD] = { 197 .type = UE_BULK, 198 .endpoint = UE_ADDR_ANY, 199 .direction = UE_DIR_IN, 200 .mh.bufsize = UPLCOM_BULK_BUF_SIZE, 201 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 202 .mh.callback = &uplcom_read_callback, 203 .if_index = 0, 204 }, 205 206 [UPLCOM_INTR_DT_RD] = { 207 .type = UE_INTERRUPT, 208 .endpoint = UE_ADDR_ANY, 209 .direction = UE_DIR_IN, 210 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 211 .mh.bufsize = 0, /* use wMaxPacketSize */ 212 .mh.callback = &uplcom_intr_callback, 213 .if_index = 1, 214 }, 215}; 216 217struct usb2_com_callback uplcom_callback = { 218 .usb2_com_cfg_get_status = &uplcom_cfg_get_status, 219 .usb2_com_cfg_set_dtr = &uplcom_cfg_set_dtr, 220 .usb2_com_cfg_set_rts = &uplcom_cfg_set_rts, 221 .usb2_com_cfg_set_break = &uplcom_cfg_set_break, 222 .usb2_com_cfg_param = &uplcom_cfg_param, 223 .usb2_com_pre_param = &uplcom_pre_param, 224 .usb2_com_start_read = &uplcom_start_read, 225 .usb2_com_stop_read = &uplcom_stop_read, 226 .usb2_com_start_write = &uplcom_start_write, 227 .usb2_com_stop_write = &uplcom_stop_write, 228}; 229 230#define USB_UPL(v,p,rl,rh,t) \ 231 USB_VENDOR(v), USB_PRODUCT(p), USB_DEV_BCD_GTEQ(rl), \ 232 USB_DEV_BCD_LTEQ(rh), USB_DRIVER_INFO(t) 233 234static const struct usb2_device_id uplcom_devs[] = { 235 /* Belkin F5U257 */ 236 {USB_UPL(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U257, 0, 0xFFFF, TYPE_PL2303X)}, 237 /* I/O DATA USB-RSAQ */ 238 {USB_UPL(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ, 0, 0xFFFF, TYPE_PL2303)}, 239 /* I/O DATA USB-RSAQ2 */ 240 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2, 0, 0xFFFF, TYPE_PL2303)}, 241 /* I/O DATA USB-RSAQ3 */ 242 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3, 0, 0xFFFF, TYPE_PL2303X)}, 243 /* PLANEX USB-RS232 URS-03 */ 244 {USB_UPL(USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A, 0, 0xFFFF, TYPE_PL2303)}, 245 /* TrendNet TU-S9 */ 246 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0x0400, 0xFFFF, TYPE_PL2303X)}, 247 /* ST Lab USB-SERIAL-4 */ 248 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0x0300, 0x03FF, TYPE_PL2303X)}, 249 /* IOGEAR/ATEN UC-232A (also ST Lab USB-SERIAL-1) */ 250 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0, 0x02FF, TYPE_PL2303)}, 251 /* TDK USB-PHS Adapter UHA6400 */ 252 {USB_UPL(USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400, 0, 0xFFFF, TYPE_PL2303)}, 253 /* RATOC REX-USB60 */ 254 {USB_UPL(USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60, 0, 0xFFFF, TYPE_PL2303)}, 255 /* ELECOM UC-SGT */ 256 {USB_UPL(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT, 0, 0xFFFF, TYPE_PL2303)}, 257 {USB_UPL(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0, 0, 0xFFFF, TYPE_PL2303)}, 258 /* Sagem USB-Serial Controller */ 259 {USB_UPL(USB_VENDOR_SAGEM, USB_PRODUCT_SAGEM_USBSERIAL, 0, 0xFFFF, TYPE_PL2303X)}, 260 /* Sony Ericsson USB Cable */ 261 {USB_UPL(USB_VENDOR_SONYERICSSON, USB_PRODUCT_SONYERICSSON_DCU10, 0, 0xFFFF, TYPE_PL2303)}, 262 /* SOURCENEXT KeikaiDenwa 8 */ 263 {USB_UPL(USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8, 0, 0xFFFF, TYPE_PL2303)}, 264 /* SOURCENEXT KeikaiDenwa 8 with charger */ 265 {USB_UPL(USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG, 0, 0, TYPE_PL2303)}, 266 /* HAL Corporation Crossam2+USB */ 267 {USB_UPL(USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001, 0, 0xFFFF, TYPE_PL2303)}, 268 /* Sitecom USB to Serial */ 269 {USB_UPL(USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_SERIAL, 0, 0xFFFF, TYPE_PL2303)}, 270 /* Tripp-Lite U209-000-R */ 271 {USB_UPL(USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209, 0, 0xFFFF, TYPE_PL2303X)}, 272 {USB_UPL(USB_VENDOR_RADIOSHACK, USB_PRODUCT_RADIOSHACK_USBCABLE, 0, 0xFFFF, TYPE_PL2303)}, 273 /* Prolific Pharos */ 274 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PHAROS, 0, 0xFFFF, TYPE_PL2303)}, 275 /* Willcom W-SIM */ 276 {USB_UPL(USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_WSIM, 0, 0xFFFF, TYPE_PL2303X)}, 277 /* Mobile Action MA-620 Infrared Adapter */ 278 {USB_UPL(USB_VENDOR_MOBILEACTION, USB_PRODUCT_MOBILEACTION_MA620, 0, 0xFFFF, TYPE_PL2303X)}, 279 280}; 281 282static device_method_t uplcom_methods[] = { 283 DEVMETHOD(device_probe, uplcom_probe), 284 DEVMETHOD(device_attach, uplcom_attach), 285 DEVMETHOD(device_detach, uplcom_detach), 286 {0, 0} 287}; 288 289static devclass_t uplcom_devclass; 290 291static driver_t uplcom_driver = { 292 .name = "uplcom", 293 .methods = uplcom_methods, 294 .size = sizeof(struct uplcom_softc), 295}; 296 297DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0); 298MODULE_DEPEND(uplcom, ucom, 1, 1, 1); 299MODULE_DEPEND(uplcom, usb, 1, 1, 1); 300MODULE_VERSION(uplcom, UPLCOM_MODVER); 301 302static int 303uplcom_probe(device_t dev) 304{ 305 struct usb2_attach_arg *uaa = device_get_ivars(dev); 306 307 DPRINTFN(11, "\n"); 308 309 if (uaa->usb2_mode != USB_MODE_HOST) { 310 return (ENXIO); 311 } 312 if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) { 313 return (ENXIO); 314 } 315 if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) { 316 return (ENXIO); 317 } 318 return (usb2_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa)); 319} 320 321static int 322uplcom_attach(device_t dev) 323{ 324 struct usb2_attach_arg *uaa = device_get_ivars(dev); 325 struct uplcom_softc *sc = device_get_softc(dev); 326 struct usb2_interface *iface; 327 struct usb2_interface_descriptor *id; 328 int error; 329 330 DPRINTFN(11, "\n"); 331 332 device_set_usb2_desc(dev); 333 mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF); 334 335 DPRINTF("sc = %p\n", sc); 336 337 sc->sc_chiptype = USB_GET_DRIVER_INFO(uaa); 338 sc->sc_udev = uaa->device; 339 340 DPRINTF("chiptype: %s\n", 341 (sc->sc_chiptype == TYPE_PL2303X) ? 342 "2303X" : "2303"); 343 344 /* 345 * USB-RSAQ1 has two interface 346 * 347 * USB-RSAQ1 | USB-RSAQ2 348 * -----------------+----------------- 349 * Interface 0 |Interface 0 350 * Interrupt(0x81) | Interrupt(0x81) 351 * -----------------+ BulkIN(0x02) 352 * Interface 1 | BulkOUT(0x83) 353 * BulkIN(0x02) | 354 * BulkOUT(0x83) | 355 */ 356 357 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum; 358 sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX; 359 360 iface = usb2_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX); 361 if (iface) { 362 id = usb2_get_interface_descriptor(iface); 363 if (id == NULL) { 364 device_printf(dev, "no interface descriptor (2)!\n"); 365 goto detach; 366 } 367 sc->sc_data_iface_no = id->bInterfaceNumber; 368 sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX; 369 usb2_set_parent_iface(uaa->device, 370 UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex); 371 } else { 372 sc->sc_data_iface_no = sc->sc_ctrl_iface_no; 373 sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX; 374 } 375 376 error = usb2_transfer_setup(uaa->device, 377 sc->sc_iface_index, sc->sc_xfer, uplcom_config_data, 378 UPLCOM_N_TRANSFER, sc, &sc->sc_mtx); 379 if (error) { 380 DPRINTF("one or more missing USB endpoints, " 381 "error=%s\n", usb2_errstr(error)); 382 goto detach; 383 } 384 error = uplcom_reset(sc, uaa->device); 385 if (error) { 386 device_printf(dev, "reset failed, error=%s\n", 387 usb2_errstr(error)); 388 goto detach; 389 } 390 /* clear stall at first run */ 391 mtx_lock(&sc->sc_mtx); 392 usb2_transfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 393 usb2_transfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 394 mtx_unlock(&sc->sc_mtx); 395 396 error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 397 &uplcom_callback, &sc->sc_mtx); 398 if (error) { 399 goto detach; 400 } 401 /* 402 * do the initialization during attach so that the system does not 403 * sleep during open: 404 */ 405 if (sc->sc_chiptype == TYPE_PL2303X) { 406 if (uplcom_pl2303x_init(uaa->device)) { 407 device_printf(dev, "init failed!\n"); 408 goto detach; 409 } 410 } 411 return (0); 412 413detach: 414 uplcom_detach(dev); 415 return (ENXIO); 416} 417 418static int 419uplcom_detach(device_t dev) 420{ 421 struct uplcom_softc *sc = device_get_softc(dev); 422 423 DPRINTF("sc=%p\n", sc); 424 425 usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1); 426 usb2_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER); 427 mtx_destroy(&sc->sc_mtx); 428 429 return (0); 430} 431 432static usb2_error_t 433uplcom_reset(struct uplcom_softc *sc, struct usb2_device *udev) 434{ 435 struct usb2_device_request req; 436 437 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 438 req.bRequest = UPLCOM_SET_REQUEST; 439 USETW(req.wValue, 0); 440 req.wIndex[0] = sc->sc_data_iface_no; 441 req.wIndex[1] = 0; 442 USETW(req.wLength, 0); 443 444 return (usb2_do_request(udev, &Giant, &req, NULL)); 445} 446 447struct pl2303x_init { 448 uint8_t req_type; 449 uint8_t request; 450 uint16_t value; 451 uint16_t index; 452 uint16_t length; 453}; 454 455static const struct pl2303x_init pl2303x[] = { 456 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1}, 457 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0}, 458 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1}, 459 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1}, 460 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1}, 461 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0}, 462 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1}, 463 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1}, 464 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0}, 465 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0}, 466 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0}, 467 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 8, 0, 0}, 468 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 9, 0, 0}, 469}; 470 471#define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0])) 472 473static int 474uplcom_pl2303x_init(struct usb2_device *udev) 475{ 476 struct usb2_device_request req; 477 usb2_error_t err; 478 uint8_t buf[4]; 479 uint8_t i; 480 481 for (i = 0; i != N_PL2302X_INIT; i++) { 482 req.bmRequestType = pl2303x[i].req_type; 483 req.bRequest = pl2303x[i].request; 484 USETW(req.wValue, pl2303x[i].value); 485 USETW(req.wIndex, pl2303x[i].index); 486 USETW(req.wLength, pl2303x[i].length); 487 488 err = usb2_do_request(udev, &Giant, &req, buf); 489 if (err) { 490 DPRINTF("error=%s\n", usb2_errstr(err)); 491 return (EIO); 492 } 493 } 494 return (0); 495} 496 497static void 498uplcom_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff) 499{ 500 struct uplcom_softc *sc = ucom->sc_parent; 501 struct usb2_device_request req; 502 503 DPRINTF("onoff = %d\n", onoff); 504 505 if (onoff) 506 sc->sc_line |= UCDC_LINE_DTR; 507 else 508 sc->sc_line &= ~UCDC_LINE_DTR; 509 510 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 511 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 512 USETW(req.wValue, sc->sc_line); 513 req.wIndex[0] = sc->sc_data_iface_no; 514 req.wIndex[1] = 0; 515 USETW(req.wLength, 0); 516 517 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 518 &req, NULL, 0, 1000); 519} 520 521static void 522uplcom_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff) 523{ 524 struct uplcom_softc *sc = ucom->sc_parent; 525 struct usb2_device_request req; 526 527 DPRINTF("onoff = %d\n", onoff); 528 529 if (onoff) 530 sc->sc_line |= UCDC_LINE_RTS; 531 else 532 sc->sc_line &= ~UCDC_LINE_RTS; 533 534 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 535 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 536 USETW(req.wValue, sc->sc_line); 537 req.wIndex[0] = sc->sc_data_iface_no; 538 req.wIndex[1] = 0; 539 USETW(req.wLength, 0); 540 541 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 542 &req, NULL, 0, 1000); 543} 544 545static void 546uplcom_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff) 547{ 548 struct uplcom_softc *sc = ucom->sc_parent; 549 struct usb2_device_request req; 550 uint16_t temp; 551 552 DPRINTF("onoff = %d\n", onoff); 553 554 temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF); 555 556 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 557 req.bRequest = UCDC_SEND_BREAK; 558 USETW(req.wValue, temp); 559 req.wIndex[0] = sc->sc_data_iface_no; 560 req.wIndex[1] = 0; 561 USETW(req.wLength, 0); 562 563 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 564 &req, NULL, 0, 1000); 565} 566 567static const int32_t uplcom_rates[] = { 568 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400, 569 19200, 28800, 38400, 57600, 115200, 570 /* 571 * Higher speeds are probably possible. PL2303X supports up to 572 * 6Mb and can set any rate 573 */ 574 230400, 460800, 614400, 921600, 1228800 575}; 576 577#define N_UPLCOM_RATES (sizeof(uplcom_rates)/sizeof(uplcom_rates[0])) 578 579static int 580uplcom_pre_param(struct usb2_com_softc *ucom, struct termios *t) 581{ 582 uint8_t i; 583 584 DPRINTF("\n"); 585 586 /* check requested baud rate */ 587 588 for (i = 0;; i++) { 589 590 if (i != N_UPLCOM_RATES) { 591 if (uplcom_rates[i] == t->c_ospeed) { 592 break; 593 } 594 } else { 595 DPRINTF("invalid baud rate (%d)\n", t->c_ospeed); 596 return (EIO); 597 } 598 } 599 600 return (0); 601} 602 603static void 604uplcom_cfg_param(struct usb2_com_softc *ucom, struct termios *t) 605{ 606 struct uplcom_softc *sc = ucom->sc_parent; 607 struct usb2_cdc_line_state ls; 608 struct usb2_device_request req; 609 610 DPRINTF("sc = %p\n", sc); 611 612 bzero(&ls, sizeof(ls)); 613 614 USETDW(ls.dwDTERate, t->c_ospeed); 615 616 if (t->c_cflag & CSTOPB) { 617 ls.bCharFormat = UCDC_STOP_BIT_2; 618 } else { 619 ls.bCharFormat = UCDC_STOP_BIT_1; 620 } 621 622 if (t->c_cflag & PARENB) { 623 if (t->c_cflag & PARODD) { 624 ls.bParityType = UCDC_PARITY_ODD; 625 } else { 626 ls.bParityType = UCDC_PARITY_EVEN; 627 } 628 } else { 629 ls.bParityType = UCDC_PARITY_NONE; 630 } 631 632 switch (t->c_cflag & CSIZE) { 633 case CS5: 634 ls.bDataBits = 5; 635 break; 636 case CS6: 637 ls.bDataBits = 6; 638 break; 639 case CS7: 640 ls.bDataBits = 7; 641 break; 642 case CS8: 643 ls.bDataBits = 8; 644 break; 645 } 646 647 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n", 648 UGETDW(ls.dwDTERate), ls.bCharFormat, 649 ls.bParityType, ls.bDataBits); 650 651 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 652 req.bRequest = UCDC_SET_LINE_CODING; 653 USETW(req.wValue, 0); 654 req.wIndex[0] = sc->sc_data_iface_no; 655 req.wIndex[1] = 0; 656 USETW(req.wLength, UCDC_LINE_STATE_LENGTH); 657 658 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 659 &req, &ls, 0, 1000); 660 661 if (t->c_cflag & CRTSCTS) { 662 663 DPRINTF("crtscts = on\n"); 664 665 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 666 req.bRequest = UPLCOM_SET_REQUEST; 667 USETW(req.wValue, 0); 668 if (sc->sc_chiptype == TYPE_PL2303X) 669 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X); 670 else 671 USETW(req.wIndex, UPLCOM_SET_CRTSCTS); 672 USETW(req.wLength, 0); 673 674 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 675 &req, NULL, 0, 1000); 676 } else { 677 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 678 req.bRequest = UPLCOM_SET_REQUEST; 679 USETW(req.wValue, 0); 680 USETW(req.wIndex, 0); 681 USETW(req.wLength, 0); 682 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 683 &req, NULL, 0, 1000); 684 } 685} 686 687static void 688uplcom_start_read(struct usb2_com_softc *ucom) 689{ 690 struct uplcom_softc *sc = ucom->sc_parent; 691 692 /* start interrupt endpoint */ 693 usb2_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]); 694 695 /* start read endpoint */ 696 usb2_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 697} 698 699static void 700uplcom_stop_read(struct usb2_com_softc *ucom) 701{ 702 struct uplcom_softc *sc = ucom->sc_parent; 703 704 /* stop interrupt endpoint */ 705 usb2_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]); 706 707 /* stop read endpoint */ 708 usb2_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 709} 710 711static void 712uplcom_start_write(struct usb2_com_softc *ucom) 713{ 714 struct uplcom_softc *sc = ucom->sc_parent; 715 716 usb2_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 717} 718 719static void 720uplcom_stop_write(struct usb2_com_softc *ucom) 721{ 722 struct uplcom_softc *sc = ucom->sc_parent; 723 724 usb2_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 725} 726 727static void 728uplcom_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr) 729{ 730 struct uplcom_softc *sc = ucom->sc_parent; 731 732 DPRINTF("\n"); 733 734 *lsr = sc->sc_lsr; 735 *msr = sc->sc_msr; 736} 737 738static void 739uplcom_intr_callback(struct usb2_xfer *xfer) 740{ 741 struct uplcom_softc *sc = xfer->priv_sc; 742 uint8_t buf[9]; 743 744 switch (USB_GET_STATE(xfer)) { 745 case USB_ST_TRANSFERRED: 746 747 DPRINTF("actlen = %u\n", xfer->actlen); 748 749 if (xfer->actlen >= 9) { 750 751 usb2_copy_out(xfer->frbuffers, 0, buf, sizeof(buf)); 752 753 DPRINTF("status = 0x%02x\n", buf[8]); 754 755 sc->sc_lsr = 0; 756 sc->sc_msr = 0; 757 758 if (buf[8] & RSAQ_STATUS_CTS) { 759 sc->sc_msr |= SER_CTS; 760 } 761 if (buf[8] & RSAQ_STATUS_DSR) { 762 sc->sc_msr |= SER_DSR; 763 } 764 if (buf[8] & RSAQ_STATUS_DCD) { 765 sc->sc_msr |= SER_DCD; 766 } 767 usb2_com_status_change(&sc->sc_ucom); 768 } 769 case USB_ST_SETUP: 770tr_setup: 771 xfer->frlengths[0] = xfer->max_data_length; 772 usb2_start_hardware(xfer); 773 return; 774 775 default: /* Error */ 776 if (xfer->error != USB_ERR_CANCELLED) { 777 /* try to clear stall first */ 778 xfer->flags.stall_pipe = 1; 779 goto tr_setup; 780 } 781 return; 782 } 783} 784 785static void 786uplcom_write_callback(struct usb2_xfer *xfer) 787{ 788 struct uplcom_softc *sc = xfer->priv_sc; 789 uint32_t actlen; 790 791 switch (USB_GET_STATE(xfer)) { 792 case USB_ST_SETUP: 793 case USB_ST_TRANSFERRED: 794tr_setup: 795 if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0, 796 UPLCOM_BULK_BUF_SIZE, &actlen)) { 797 798 DPRINTF("actlen = %d\n", actlen); 799 800 xfer->frlengths[0] = actlen; 801 usb2_start_hardware(xfer); 802 } 803 return; 804 805 default: /* Error */ 806 if (xfer->error != USB_ERR_CANCELLED) { 807 /* try to clear stall first */ 808 xfer->flags.stall_pipe = 1; 809 goto tr_setup; 810 } 811 return; 812 } 813} 814 815static void 816uplcom_read_callback(struct usb2_xfer *xfer) 817{ 818 struct uplcom_softc *sc = xfer->priv_sc; 819 820 switch (USB_GET_STATE(xfer)) { 821 case USB_ST_TRANSFERRED: 822 usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers, 0, xfer->actlen); 823 824 case USB_ST_SETUP: 825tr_setup: 826 xfer->frlengths[0] = xfer->max_data_length; 827 usb2_start_hardware(xfer); 828 return; 829 830 default: /* Error */ 831 if (xfer->error != USB_ERR_CANCELLED) { 832 /* try to clear stall first */ 833 xfer->flags.stall_pipe = 1; 834 goto tr_setup; 835 } 836 return; 837 } 838} 839