uplcom.c revision 189275
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 189275 2009-03-02 05:37:05Z 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}; 278 279static device_method_t uplcom_methods[] = { 280 DEVMETHOD(device_probe, uplcom_probe), 281 DEVMETHOD(device_attach, uplcom_attach), 282 DEVMETHOD(device_detach, uplcom_detach), 283 {0, 0} 284}; 285 286static devclass_t uplcom_devclass; 287 288static driver_t uplcom_driver = { 289 .name = "uplcom", 290 .methods = uplcom_methods, 291 .size = sizeof(struct uplcom_softc), 292}; 293 294DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0); 295MODULE_DEPEND(uplcom, ucom, 1, 1, 1); 296MODULE_DEPEND(uplcom, usb, 1, 1, 1); 297MODULE_VERSION(uplcom, UPLCOM_MODVER); 298 299static int 300uplcom_probe(device_t dev) 301{ 302 struct usb2_attach_arg *uaa = device_get_ivars(dev); 303 304 DPRINTFN(11, "\n"); 305 306 if (uaa->usb2_mode != USB_MODE_HOST) { 307 return (ENXIO); 308 } 309 if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) { 310 return (ENXIO); 311 } 312 if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) { 313 return (ENXIO); 314 } 315 return (usb2_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa)); 316} 317 318static int 319uplcom_attach(device_t dev) 320{ 321 struct usb2_attach_arg *uaa = device_get_ivars(dev); 322 struct uplcom_softc *sc = device_get_softc(dev); 323 struct usb2_interface *iface; 324 struct usb2_interface_descriptor *id; 325 int error; 326 327 DPRINTFN(11, "\n"); 328 329 device_set_usb2_desc(dev); 330 mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF); 331 332 DPRINTF("sc = %p\n", sc); 333 334 sc->sc_chiptype = USB_GET_DRIVER_INFO(uaa); 335 sc->sc_udev = uaa->device; 336 337 DPRINTF("chiptype: %s\n", 338 (sc->sc_chiptype == TYPE_PL2303X) ? 339 "2303X" : "2303"); 340 341 /* 342 * USB-RSAQ1 has two interface 343 * 344 * USB-RSAQ1 | USB-RSAQ2 345 * -----------------+----------------- 346 * Interface 0 |Interface 0 347 * Interrupt(0x81) | Interrupt(0x81) 348 * -----------------+ BulkIN(0x02) 349 * Interface 1 | BulkOUT(0x83) 350 * BulkIN(0x02) | 351 * BulkOUT(0x83) | 352 */ 353 354 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum; 355 sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX; 356 357 iface = usb2_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX); 358 if (iface) { 359 id = usb2_get_interface_descriptor(iface); 360 if (id == NULL) { 361 device_printf(dev, "no interface descriptor (2)!\n"); 362 goto detach; 363 } 364 sc->sc_data_iface_no = id->bInterfaceNumber; 365 sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX; 366 usb2_set_parent_iface(uaa->device, 367 UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex); 368 } else { 369 sc->sc_data_iface_no = sc->sc_ctrl_iface_no; 370 sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX; 371 } 372 373 error = usb2_transfer_setup(uaa->device, 374 sc->sc_iface_index, sc->sc_xfer, uplcom_config_data, 375 UPLCOM_N_TRANSFER, sc, &sc->sc_mtx); 376 if (error) { 377 DPRINTF("one or more missing USB endpoints, " 378 "error=%s\n", usb2_errstr(error)); 379 goto detach; 380 } 381 error = uplcom_reset(sc, uaa->device); 382 if (error) { 383 device_printf(dev, "reset failed, error=%s\n", 384 usb2_errstr(error)); 385 goto detach; 386 } 387 /* clear stall at first run */ 388 mtx_lock(&sc->sc_mtx); 389 usb2_transfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 390 usb2_transfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 391 mtx_unlock(&sc->sc_mtx); 392 393 error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 394 &uplcom_callback, &sc->sc_mtx); 395 if (error) { 396 goto detach; 397 } 398 /* 399 * do the initialization during attach so that the system does not 400 * sleep during open: 401 */ 402 if (sc->sc_chiptype == TYPE_PL2303X) { 403 if (uplcom_pl2303x_init(uaa->device)) { 404 device_printf(dev, "init failed!\n"); 405 goto detach; 406 } 407 } 408 return (0); 409 410detach: 411 uplcom_detach(dev); 412 return (ENXIO); 413} 414 415static int 416uplcom_detach(device_t dev) 417{ 418 struct uplcom_softc *sc = device_get_softc(dev); 419 420 DPRINTF("sc=%p\n", sc); 421 422 usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1); 423 usb2_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER); 424 mtx_destroy(&sc->sc_mtx); 425 426 return (0); 427} 428 429static usb2_error_t 430uplcom_reset(struct uplcom_softc *sc, struct usb2_device *udev) 431{ 432 struct usb2_device_request req; 433 434 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 435 req.bRequest = UPLCOM_SET_REQUEST; 436 USETW(req.wValue, 0); 437 req.wIndex[0] = sc->sc_data_iface_no; 438 req.wIndex[1] = 0; 439 USETW(req.wLength, 0); 440 441 return (usb2_do_request(udev, &Giant, &req, NULL)); 442} 443 444struct pl2303x_init { 445 uint8_t req_type; 446 uint8_t request; 447 uint16_t value; 448 uint16_t index; 449 uint16_t length; 450}; 451 452static const struct pl2303x_init pl2303x[] = { 453 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1}, 454 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 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_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1}, 458 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0}, 459 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1}, 460 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1}, 461 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0}, 462 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0}, 463 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0}, 464 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 8, 0, 0}, 465 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 9, 0, 0}, 466}; 467 468#define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0])) 469 470static int 471uplcom_pl2303x_init(struct usb2_device *udev) 472{ 473 struct usb2_device_request req; 474 usb2_error_t err; 475 uint8_t buf[4]; 476 uint8_t i; 477 478 for (i = 0; i != N_PL2302X_INIT; i++) { 479 req.bmRequestType = pl2303x[i].req_type; 480 req.bRequest = pl2303x[i].request; 481 USETW(req.wValue, pl2303x[i].value); 482 USETW(req.wIndex, pl2303x[i].index); 483 USETW(req.wLength, pl2303x[i].length); 484 485 err = usb2_do_request(udev, &Giant, &req, buf); 486 if (err) { 487 DPRINTF("error=%s\n", usb2_errstr(err)); 488 return (EIO); 489 } 490 } 491 return (0); 492} 493 494static void 495uplcom_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff) 496{ 497 struct uplcom_softc *sc = ucom->sc_parent; 498 struct usb2_device_request req; 499 500 DPRINTF("onoff = %d\n", onoff); 501 502 if (onoff) 503 sc->sc_line |= UCDC_LINE_DTR; 504 else 505 sc->sc_line &= ~UCDC_LINE_DTR; 506 507 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 508 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 509 USETW(req.wValue, sc->sc_line); 510 req.wIndex[0] = sc->sc_data_iface_no; 511 req.wIndex[1] = 0; 512 USETW(req.wLength, 0); 513 514 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 515 &req, NULL, 0, 1000); 516} 517 518static void 519uplcom_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff) 520{ 521 struct uplcom_softc *sc = ucom->sc_parent; 522 struct usb2_device_request req; 523 524 DPRINTF("onoff = %d\n", onoff); 525 526 if (onoff) 527 sc->sc_line |= UCDC_LINE_RTS; 528 else 529 sc->sc_line &= ~UCDC_LINE_RTS; 530 531 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 532 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 533 USETW(req.wValue, sc->sc_line); 534 req.wIndex[0] = sc->sc_data_iface_no; 535 req.wIndex[1] = 0; 536 USETW(req.wLength, 0); 537 538 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 539 &req, NULL, 0, 1000); 540} 541 542static void 543uplcom_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff) 544{ 545 struct uplcom_softc *sc = ucom->sc_parent; 546 struct usb2_device_request req; 547 uint16_t temp; 548 549 DPRINTF("onoff = %d\n", onoff); 550 551 temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF); 552 553 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 554 req.bRequest = UCDC_SEND_BREAK; 555 USETW(req.wValue, temp); 556 req.wIndex[0] = sc->sc_data_iface_no; 557 req.wIndex[1] = 0; 558 USETW(req.wLength, 0); 559 560 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 561 &req, NULL, 0, 1000); 562} 563 564static const int32_t uplcom_rates[] = { 565 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400, 566 19200, 28800, 38400, 57600, 115200, 567 /* 568 * Higher speeds are probably possible. PL2303X supports up to 569 * 6Mb and can set any rate 570 */ 571 230400, 460800, 614400, 921600, 1228800 572}; 573 574#define N_UPLCOM_RATES (sizeof(uplcom_rates)/sizeof(uplcom_rates[0])) 575 576static int 577uplcom_pre_param(struct usb2_com_softc *ucom, struct termios *t) 578{ 579 uint8_t i; 580 581 DPRINTF("\n"); 582 583 /* check requested baud rate */ 584 585 for (i = 0;; i++) { 586 587 if (i != N_UPLCOM_RATES) { 588 if (uplcom_rates[i] == t->c_ospeed) { 589 break; 590 } 591 } else { 592 DPRINTF("invalid baud rate (%d)\n", t->c_ospeed); 593 return (EIO); 594 } 595 } 596 597 return (0); 598} 599 600static void 601uplcom_cfg_param(struct usb2_com_softc *ucom, struct termios *t) 602{ 603 struct uplcom_softc *sc = ucom->sc_parent; 604 struct usb2_cdc_line_state ls; 605 struct usb2_device_request req; 606 607 DPRINTF("sc = %p\n", sc); 608 609 bzero(&ls, sizeof(ls)); 610 611 USETDW(ls.dwDTERate, t->c_ospeed); 612 613 if (t->c_cflag & CSTOPB) { 614 ls.bCharFormat = UCDC_STOP_BIT_2; 615 } else { 616 ls.bCharFormat = UCDC_STOP_BIT_1; 617 } 618 619 if (t->c_cflag & PARENB) { 620 if (t->c_cflag & PARODD) { 621 ls.bParityType = UCDC_PARITY_ODD; 622 } else { 623 ls.bParityType = UCDC_PARITY_EVEN; 624 } 625 } else { 626 ls.bParityType = UCDC_PARITY_NONE; 627 } 628 629 switch (t->c_cflag & CSIZE) { 630 case CS5: 631 ls.bDataBits = 5; 632 break; 633 case CS6: 634 ls.bDataBits = 6; 635 break; 636 case CS7: 637 ls.bDataBits = 7; 638 break; 639 case CS8: 640 ls.bDataBits = 8; 641 break; 642 } 643 644 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n", 645 UGETDW(ls.dwDTERate), ls.bCharFormat, 646 ls.bParityType, ls.bDataBits); 647 648 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 649 req.bRequest = UCDC_SET_LINE_CODING; 650 USETW(req.wValue, 0); 651 req.wIndex[0] = sc->sc_data_iface_no; 652 req.wIndex[1] = 0; 653 USETW(req.wLength, UCDC_LINE_STATE_LENGTH); 654 655 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 656 &req, &ls, 0, 1000); 657 658 if (t->c_cflag & CRTSCTS) { 659 660 DPRINTF("crtscts = on\n"); 661 662 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 663 req.bRequest = UPLCOM_SET_REQUEST; 664 USETW(req.wValue, 0); 665 if (sc->sc_chiptype == TYPE_PL2303X) 666 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X); 667 else 668 USETW(req.wIndex, UPLCOM_SET_CRTSCTS); 669 USETW(req.wLength, 0); 670 671 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 672 &req, NULL, 0, 1000); 673 } else { 674 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 675 req.bRequest = UPLCOM_SET_REQUEST; 676 USETW(req.wValue, 0); 677 USETW(req.wIndex, 0); 678 USETW(req.wLength, 0); 679 usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 680 &req, NULL, 0, 1000); 681 } 682} 683 684static void 685uplcom_start_read(struct usb2_com_softc *ucom) 686{ 687 struct uplcom_softc *sc = ucom->sc_parent; 688 689 /* start interrupt endpoint */ 690 usb2_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]); 691 692 /* start read endpoint */ 693 usb2_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 694} 695 696static void 697uplcom_stop_read(struct usb2_com_softc *ucom) 698{ 699 struct uplcom_softc *sc = ucom->sc_parent; 700 701 /* stop interrupt endpoint */ 702 usb2_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]); 703 704 /* stop read endpoint */ 705 usb2_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 706} 707 708static void 709uplcom_start_write(struct usb2_com_softc *ucom) 710{ 711 struct uplcom_softc *sc = ucom->sc_parent; 712 713 usb2_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 714} 715 716static void 717uplcom_stop_write(struct usb2_com_softc *ucom) 718{ 719 struct uplcom_softc *sc = ucom->sc_parent; 720 721 usb2_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 722} 723 724static void 725uplcom_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr) 726{ 727 struct uplcom_softc *sc = ucom->sc_parent; 728 729 DPRINTF("\n"); 730 731 *lsr = sc->sc_lsr; 732 *msr = sc->sc_msr; 733} 734 735static void 736uplcom_intr_callback(struct usb2_xfer *xfer) 737{ 738 struct uplcom_softc *sc = xfer->priv_sc; 739 uint8_t buf[9]; 740 741 switch (USB_GET_STATE(xfer)) { 742 case USB_ST_TRANSFERRED: 743 744 DPRINTF("actlen = %u\n", xfer->actlen); 745 746 if (xfer->actlen >= 9) { 747 748 usb2_copy_out(xfer->frbuffers, 0, buf, sizeof(buf)); 749 750 DPRINTF("status = 0x%02x\n", buf[8]); 751 752 sc->sc_lsr = 0; 753 sc->sc_msr = 0; 754 755 if (buf[8] & RSAQ_STATUS_CTS) { 756 sc->sc_msr |= SER_CTS; 757 } 758 if (buf[8] & RSAQ_STATUS_DSR) { 759 sc->sc_msr |= SER_DSR; 760 } 761 if (buf[8] & RSAQ_STATUS_DCD) { 762 sc->sc_msr |= SER_DCD; 763 } 764 usb2_com_status_change(&sc->sc_ucom); 765 } 766 case USB_ST_SETUP: 767tr_setup: 768 xfer->frlengths[0] = xfer->max_data_length; 769 usb2_start_hardware(xfer); 770 return; 771 772 default: /* Error */ 773 if (xfer->error != USB_ERR_CANCELLED) { 774 /* try to clear stall first */ 775 xfer->flags.stall_pipe = 1; 776 goto tr_setup; 777 } 778 return; 779 } 780} 781 782static void 783uplcom_write_callback(struct usb2_xfer *xfer) 784{ 785 struct uplcom_softc *sc = xfer->priv_sc; 786 uint32_t actlen; 787 788 switch (USB_GET_STATE(xfer)) { 789 case USB_ST_SETUP: 790 case USB_ST_TRANSFERRED: 791tr_setup: 792 if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0, 793 UPLCOM_BULK_BUF_SIZE, &actlen)) { 794 795 DPRINTF("actlen = %d\n", actlen); 796 797 xfer->frlengths[0] = actlen; 798 usb2_start_hardware(xfer); 799 } 800 return; 801 802 default: /* Error */ 803 if (xfer->error != USB_ERR_CANCELLED) { 804 /* try to clear stall first */ 805 xfer->flags.stall_pipe = 1; 806 goto tr_setup; 807 } 808 return; 809 } 810} 811 812static void 813uplcom_read_callback(struct usb2_xfer *xfer) 814{ 815 struct uplcom_softc *sc = xfer->priv_sc; 816 817 switch (USB_GET_STATE(xfer)) { 818 case USB_ST_TRANSFERRED: 819 usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers, 0, xfer->actlen); 820 821 case USB_ST_SETUP: 822tr_setup: 823 xfer->frlengths[0] = xfer->max_data_length; 824 usb2_start_hardware(xfer); 825 return; 826 827 default: /* Error */ 828 if (xfer->error != USB_ERR_CANCELLED) { 829 /* try to clear stall first */ 830 xfer->flags.stall_pipe = 1; 831 goto tr_setup; 832 } 833 return; 834 } 835} 836