uplcom.c revision 276701
121308Sache/* $NetBSD: uplcom.c,v 1.21 2001/11/13 06:24:56 lukem Exp $ */ 221308Sache 321308Sache#include <sys/cdefs.h> 421308Sache__FBSDID("$FreeBSD: head/sys/dev/usb/serial/uplcom.c 276701 2015-01-05 15:04:17Z hselasky $"); 521308Sache 621308Sache/*- 721308Sache * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>. 821308Sache * All rights reserved. 921308Sache * 1058310Sache * Redistribution and use in source and binary forms, with or without 1121308Sache * modification, are permitted provided that the following conditions 1221308Sache * are met: 1321308Sache * 1. Redistributions of source code must retain the above copyright 1421308Sache * notice, this list of conditions and the following disclaimer. 1521308Sache * 2. Redistributions in binary form must reproduce the above copyright 1621308Sache * notice, this list of conditions and the following disclaimer in the 1721308Sache * documentation and/or other materials provided with the distribution. 1821308Sache * 1921308Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2021308Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2158310Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2221308Sache * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2321308Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2421308Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2521308Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2621308Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2721308Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2821308Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2921308Sache * SUCH DAMAGE. 3021308Sache */ 3121308Sache 3221308Sache/*- 3321308Sache * Copyright (c) 2001 The NetBSD Foundation, Inc. 3421308Sache * All rights reserved. 3521308Sache * 3621308Sache * This code is derived from software contributed to The NetBSD Foundation 3721308Sache * by Ichiro FUKUHARA (ichiro@ichiro.org). 3821308Sache * 3921308Sache * Redistribution and use in source and binary forms, with or without 4021308Sache * modification, are permitted provided that the following conditions 4121308Sache * are met: 4221308Sache * 1. Redistributions of source code must retain the above copyright 4321308Sache * notice, this list of conditions and the following disclaimer. 4421308Sache * 2. Redistributions in binary form must reproduce the above copyright 4521308Sache * notice, this list of conditions and the following disclaimer in the 4621308Sache * documentation and/or other materials provided with the distribution. 4721308Sache * 4821308Sache * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 4958310Sache * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 5058310Sache * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 5158310Sache * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 5221308Sache * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 5321308Sache * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 5421308Sache * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 5521308Sache * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 5621308Sache * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 5721308Sache * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 5875406Sache * POSSIBILITY OF SUCH DAMAGE. 5975406Sache */ 6075406Sache 6175406Sache/* 6221308Sache * This driver supports several USB-to-RS232 serial adapters driven by 6321308Sache * Prolific PL-2303, PL-2303X and probably PL-2303HX USB-to-RS232 6421308Sache * bridge chip. The adapters are sold under many different brand 6521308Sache * names. 6621308Sache * 6721308Sache * Datasheets are available at Prolific www site at 6821308Sache * http://www.prolific.com.tw. The datasheets don't contain full 6921308Sache * programming information for the chip. 7021308Sache * 7121308Sache * PL-2303HX is probably programmed the same as PL-2303X. 7221308Sache * 7321308Sache * There are several differences between PL-2303 and PL-2303(H)X. 7421308Sache * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_ 7521308Sache * different command for controlling CRTSCTS and needs special 7621308Sache * sequence of commands for initialization which aren't also 7721308Sache * documented in the datasheet. 7821308Sache */ 7921308Sache 8021308Sache#include <sys/stdint.h> 8121308Sache#include <sys/stddef.h> 8221308Sache#include <sys/param.h> 8321308Sache#include <sys/queue.h> 8421308Sache#include <sys/types.h> 8521308Sache#include <sys/systm.h> 8621308Sache#include <sys/kernel.h> 8721308Sache#include <sys/bus.h> 8821308Sache#include <sys/module.h> 8921308Sache#include <sys/lock.h> 9021308Sache#include <sys/mutex.h> 9121308Sache#include <sys/condvar.h> 9221308Sache#include <sys/sysctl.h> 9375406Sache#include <sys/sx.h> 9421308Sache#include <sys/unistd.h> 9575406Sache#include <sys/callout.h> 9621308Sache#include <sys/malloc.h> 9721308Sache#include <sys/priv.h> 9821308Sache 9921308Sache#include <dev/usb/usb.h> 10021308Sache#include <dev/usb/usbdi.h> 10121308Sache#include <dev/usb/usbdi_util.h> 10221308Sache#include <dev/usb/usb_cdc.h> 10375406Sache#include "usbdevs.h" 10421308Sache 10521308Sache#define USB_DEBUG_VAR uplcom_debug 10675406Sache#include <dev/usb/usb_debug.h> 10721308Sache#include <dev/usb/usb_process.h> 10821308Sache 10921308Sache#include <dev/usb/serial/usb_serial.h> 11021308Sache 11121308Sache#ifdef USB_DEBUG 11275406Sachestatic int uplcom_debug = 0; 11321308Sache 11421308Sachestatic SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom"); 11521308SacheSYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RWTUN, 11621308Sache &uplcom_debug, 0, "Debug level"); 11721308Sache#endif 11821308Sache 11921308Sache#define UPLCOM_MODVER 1 /* module version */ 12021308Sache 12121308Sache#define UPLCOM_CONFIG_INDEX 0 12221308Sache#define UPLCOM_IFACE_INDEX 0 12321308Sache#define UPLCOM_SECOND_IFACE_INDEX 1 12475406Sache 12521308Sache#ifndef UPLCOM_INTR_INTERVAL 12621308Sache#define UPLCOM_INTR_INTERVAL 0 /* default */ 12721308Sache#endif 12821308Sache 12921308Sache#define UPLCOM_BULK_BUF_SIZE 1024 /* bytes */ 13021308Sache 13121308Sache#define UPLCOM_SET_REQUEST 0x01 13221308Sache#define UPLCOM_SET_CRTSCTS 0x41 13321308Sache#define UPLCOM_SET_CRTSCTS_PL2303X 0x61 13421308Sache#define RSAQ_STATUS_CTS 0x80 13521308Sache#define RSAQ_STATUS_DSR 0x02 13675406Sache#define RSAQ_STATUS_DCD 0x01 13775406Sache 13821308Sache#define TYPE_PL2303 0 13921308Sache#define TYPE_PL2303HX 1 14021308Sache 14121308Sacheenum { 14221308Sache UPLCOM_BULK_DT_WR, 14375406Sache UPLCOM_BULK_DT_RD, 14421308Sache UPLCOM_INTR_DT_RD, 14521308Sache UPLCOM_N_TRANSFER, 14621308Sache}; 14721308Sache 14875406Sachestruct uplcom_softc { 14975406Sache struct ucom_super_softc sc_super_ucom; 15075406Sache struct ucom_softc sc_ucom; 15121308Sache 15221308Sache struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER]; 15321308Sache struct usb_device *sc_udev; 15421308Sache struct mtx sc_mtx; 15521308Sache 15621308Sache uint16_t sc_line; 15721308Sache 15821308Sache uint8_t sc_lsr; /* local status register */ 15921308Sache uint8_t sc_msr; /* uplcom status register */ 16021308Sache uint8_t sc_chiptype; /* type of chip */ 161119610Sache uint8_t sc_ctrl_iface_no; 16221308Sache uint8_t sc_data_iface_no; 163119610Sache uint8_t sc_iface_index[2]; 16421308Sache}; 16521308Sache 16621308Sache/* prototypes */ 16721308Sache 16821308Sachestatic usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *); 16921308Sachestatic usb_error_t uplcom_pl2303_do(struct usb_device *, int8_t, uint8_t, 17021308Sache uint16_t, uint16_t, uint16_t); 17121308Sachestatic int uplcom_pl2303_init(struct usb_device *, uint8_t); 17221308Sachestatic void uplcom_free(struct ucom_softc *); 17321308Sachestatic void uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t); 17421308Sachestatic void uplcom_cfg_set_rts(struct ucom_softc *, uint8_t); 17521308Sachestatic void uplcom_cfg_set_break(struct ucom_softc *, uint8_t); 17621308Sachestatic int uplcom_pre_param(struct ucom_softc *, struct termios *); 17721308Sachestatic void uplcom_cfg_param(struct ucom_softc *, struct termios *); 17821308Sachestatic void uplcom_start_read(struct ucom_softc *); 17921308Sachestatic void uplcom_stop_read(struct ucom_softc *); 18075406Sachestatic void uplcom_start_write(struct ucom_softc *); 18175406Sachestatic void uplcom_stop_write(struct ucom_softc *); 18221308Sachestatic void uplcom_cfg_get_status(struct ucom_softc *, uint8_t *, 18321308Sache uint8_t *); 18475406Sachestatic void uplcom_poll(struct ucom_softc *ucom); 18521308Sache 18621308Sachestatic device_probe_t uplcom_probe; 18721308Sachestatic device_attach_t uplcom_attach; 18821308Sachestatic device_detach_t uplcom_detach; 18921308Sachestatic void uplcom_free_softc(struct uplcom_softc *); 19021308Sache 19121308Sachestatic usb_callback_t uplcom_intr_callback; 19221308Sachestatic usb_callback_t uplcom_write_callback; 19321308Sachestatic usb_callback_t uplcom_read_callback; 19421308Sache 19521308Sachestatic const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = { 19621308Sache 197119610Sache [UPLCOM_BULK_DT_WR] = { 19821308Sache .type = UE_BULK, 19921308Sache .endpoint = UE_ADDR_ANY, 20021308Sache .direction = UE_DIR_OUT, 20121308Sache .bufsize = UPLCOM_BULK_BUF_SIZE, 20221308Sache .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 20321308Sache .callback = &uplcom_write_callback, 20421308Sache .if_index = 0, 20521308Sache }, 20621308Sache 20721308Sache [UPLCOM_BULK_DT_RD] = { 20821308Sache .type = UE_BULK, 20921308Sache .endpoint = UE_ADDR_ANY, 21021308Sache .direction = UE_DIR_IN, 21175406Sache .bufsize = UPLCOM_BULK_BUF_SIZE, 21221308Sache .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 21321308Sache .callback = &uplcom_read_callback, 21421308Sache .if_index = 0, 21521308Sache }, 21621308Sache 21721308Sache [UPLCOM_INTR_DT_RD] = { 21821308Sache .type = UE_INTERRUPT, 21921308Sache .endpoint = UE_ADDR_ANY, 22021308Sache .direction = UE_DIR_IN, 22121308Sache .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 222119610Sache .bufsize = 0, /* use wMaxPacketSize */ 22321308Sache .callback = &uplcom_intr_callback, 22421308Sache .if_index = 1, 22521308Sache }, 22621308Sache}; 22721308Sache 22821308Sachestatic struct ucom_callback uplcom_callback = { 22921308Sache .ucom_cfg_get_status = &uplcom_cfg_get_status, 23021308Sache .ucom_cfg_set_dtr = &uplcom_cfg_set_dtr, 23175406Sache .ucom_cfg_set_rts = &uplcom_cfg_set_rts, 23221308Sache .ucom_cfg_set_break = &uplcom_cfg_set_break, 23321308Sache .ucom_cfg_param = &uplcom_cfg_param, 23421308Sache .ucom_pre_param = &uplcom_pre_param, 23521308Sache .ucom_start_read = &uplcom_start_read, 23621308Sache .ucom_stop_read = &uplcom_stop_read, 23721308Sache .ucom_start_write = &uplcom_start_write, 23821308Sache .ucom_stop_write = &uplcom_stop_write, 23921308Sache .ucom_poll = &uplcom_poll, 24021308Sache .ucom_free = &uplcom_free, 24121308Sache}; 24221308Sache 24321308Sache#define UPLCOM_DEV(v,p) \ 24421308Sache { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p) } 245119610Sache 24621308Sachestatic const STRUCT_USB_HOST_ID uplcom_devs[] = { 24775406Sache UPLCOM_DEV(ACERP, S81), /* BenQ S81 phone */ 24821308Sache UPLCOM_DEV(ADLINK, ND6530), /* ADLINK ND-6530 USB-Serial */ 24921308Sache UPLCOM_DEV(ALCATEL, OT535), /* Alcatel One Touch 535/735 */ 25021308Sache UPLCOM_DEV(ALCOR, AU9720), /* Alcor AU9720 USB 2.0-RS232 */ 25121308Sache UPLCOM_DEV(ANCHOR, SERIAL), /* Anchor Serial adapter */ 25221308Sache UPLCOM_DEV(ATEN, UC232A), /* PLANEX USB-RS232 URS-03 */ 25321308Sache UPLCOM_DEV(BELKIN, F5U257), /* Belkin F5U257 USB to Serial */ 25421308Sache UPLCOM_DEV(COREGA, CGUSBRS232R), /* Corega CG-USBRS232R */ 25521308Sache UPLCOM_DEV(EPSON, CRESSI_EDY), /* Cressi Edy diving computer */ 25621308Sache UPLCOM_DEV(EPSON, N2ITION3), /* Zeagle N2iTion3 diving computer */ 25721308Sache UPLCOM_DEV(ELECOM, UCSGT), /* ELECOM UC-SGT Serial Adapter */ 25821308Sache UPLCOM_DEV(ELECOM, UCSGT0), /* ELECOM UC-SGT Serial Adapter */ 25921308Sache UPLCOM_DEV(HAL, IMR001), /* HAL Corporation Crossam2+USB */ 26021308Sache UPLCOM_DEV(HP, LD220), /* HP LD220 POS Display */ 26121308Sache UPLCOM_DEV(IODATA, USBRSAQ), /* I/O DATA USB-RSAQ */ 26221308Sache UPLCOM_DEV(IODATA, USBRSAQ5), /* I/O DATA USB-RSAQ5 */ 263 UPLCOM_DEV(ITEGNO, WM1080A), /* iTegno WM1080A GSM/GFPRS modem */ 264 UPLCOM_DEV(ITEGNO, WM2080A), /* iTegno WM2080A CDMA modem */ 265 UPLCOM_DEV(LEADTEK, 9531), /* Leadtek 9531 GPS */ 266 UPLCOM_DEV(MICROSOFT, 700WX), /* Microsoft Palm 700WX */ 267 UPLCOM_DEV(MOBILEACTION, MA620), /* Mobile Action MA-620 Infrared Adapter */ 268 UPLCOM_DEV(NETINDEX, WS002IN), /* Willcom W-S002IN */ 269 UPLCOM_DEV(NOKIA2, CA42), /* Nokia CA-42 cable */ 270 UPLCOM_DEV(OTI, DKU5), /* OTI DKU-5 cable */ 271 UPLCOM_DEV(PANASONIC, TYTP50P6S), /* Panasonic TY-TP50P6-S flat screen */ 272 UPLCOM_DEV(PLX, CA42), /* PLX CA-42 clone cable */ 273 UPLCOM_DEV(PROLIFIC, ALLTRONIX_GPRS), /* Alltronix ACM003U00 modem */ 274 UPLCOM_DEV(PROLIFIC, ALDIGA_AL11U), /* AlDiga AL-11U modem */ 275 UPLCOM_DEV(PROLIFIC, DCU11), /* DCU-11 Phone Cable */ 276 UPLCOM_DEV(PROLIFIC, HCR331), /* HCR331 Card Reader */ 277 UPLCOM_DEV(PROLIFIC, MICROMAX_610U), /* Micromax 610U modem */ 278 UPLCOM_DEV(PROLIFIC, MOTOROLA), /* Motorola cable */ 279 UPLCOM_DEV(PROLIFIC, PHAROS), /* Prolific Pharos */ 280 UPLCOM_DEV(PROLIFIC, PL2303), /* Generic adapter */ 281 UPLCOM_DEV(PROLIFIC, RSAQ2), /* I/O DATA USB-RSAQ2 */ 282 UPLCOM_DEV(PROLIFIC, RSAQ3), /* I/O DATA USB-RSAQ3 */ 283 UPLCOM_DEV(PROLIFIC, UIC_MSR206), /* UIC MSR206 Card Reader */ 284 UPLCOM_DEV(PROLIFIC2, PL2303), /* Prolific adapter */ 285 UPLCOM_DEV(RADIOSHACK, USBCABLE), /* Radio Shack USB Adapter */ 286 UPLCOM_DEV(RATOC, REXUSB60), /* RATOC REX-USB60 */ 287 UPLCOM_DEV(SAGEM, USBSERIAL), /* Sagem USB-Serial Controller */ 288 UPLCOM_DEV(SAMSUNG, I330), /* Samsung I330 phone cradle */ 289 UPLCOM_DEV(SANWA, KB_USB2), /* Sanwa KB-USB2 Multimeter cable */ 290 UPLCOM_DEV(SIEMENS3, EF81), /* Siemens EF81 */ 291 UPLCOM_DEV(SIEMENS3, SX1), /* Siemens SX1 */ 292 UPLCOM_DEV(SIEMENS3, X65), /* Siemens X65 */ 293 UPLCOM_DEV(SIEMENS3, X75), /* Siemens X75 */ 294 UPLCOM_DEV(SITECOM, SERIAL), /* Sitecom USB to Serial */ 295 UPLCOM_DEV(SMART, PL2303), /* SMART Technologies USB to Serial */ 296 UPLCOM_DEV(SONY, QN3), /* Sony QN3 phone cable */ 297 UPLCOM_DEV(SONYERICSSON, DATAPILOT), /* Sony Ericsson Datapilot */ 298 UPLCOM_DEV(SONYERICSSON, DCU10), /* Sony Ericsson DCU-10 Cable */ 299 UPLCOM_DEV(SOURCENEXT, KEIKAI8), /* SOURCENEXT KeikaiDenwa 8 */ 300 UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG), /* SOURCENEXT KeikaiDenwa 8 with charger */ 301 UPLCOM_DEV(SPEEDDRAGON, MS3303H), /* Speed Dragon USB-Serial */ 302 UPLCOM_DEV(SYNTECH, CPT8001C), /* Syntech CPT-8001C Barcode scanner */ 303 UPLCOM_DEV(TDK, UHA6400), /* TDK USB-PHS Adapter UHA6400 */ 304 UPLCOM_DEV(TDK, UPA9664), /* TDK USB-PHS Adapter UPA9664 */ 305 UPLCOM_DEV(TRIPPLITE, U209), /* Tripp-Lite U209-000-R USB to Serial */ 306 UPLCOM_DEV(YCCABLE, PL2303), /* YC Cable USB-Serial */ 307}; 308#undef UPLCOM_DEV 309 310static device_method_t uplcom_methods[] = { 311 DEVMETHOD(device_probe, uplcom_probe), 312 DEVMETHOD(device_attach, uplcom_attach), 313 DEVMETHOD(device_detach, uplcom_detach), 314 DEVMETHOD_END 315}; 316 317static devclass_t uplcom_devclass; 318 319static driver_t uplcom_driver = { 320 .name = "uplcom", 321 .methods = uplcom_methods, 322 .size = sizeof(struct uplcom_softc), 323}; 324 325DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0); 326MODULE_DEPEND(uplcom, ucom, 1, 1, 1); 327MODULE_DEPEND(uplcom, usb, 1, 1, 1); 328MODULE_VERSION(uplcom, UPLCOM_MODVER); 329 330static int 331uplcom_probe(device_t dev) 332{ 333 struct usb_attach_arg *uaa = device_get_ivars(dev); 334 335 DPRINTFN(11, "\n"); 336 337 if (uaa->usb_mode != USB_MODE_HOST) { 338 return (ENXIO); 339 } 340 if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) { 341 return (ENXIO); 342 } 343 if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) { 344 return (ENXIO); 345 } 346 return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa)); 347} 348 349static int 350uplcom_attach(device_t dev) 351{ 352 struct usb_attach_arg *uaa = device_get_ivars(dev); 353 struct uplcom_softc *sc = device_get_softc(dev); 354 struct usb_interface *iface; 355 struct usb_interface_descriptor *id; 356 struct usb_device_descriptor *dd; 357 int error; 358 359 DPRINTFN(11, "\n"); 360 361 device_set_usb_desc(dev); 362 mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF); 363 ucom_ref(&sc->sc_super_ucom); 364 365 DPRINTF("sc = %p\n", sc); 366 367 sc->sc_udev = uaa->device; 368 369 /* Determine the chip type. This algorithm is taken from Linux. */ 370 dd = usbd_get_device_descriptor(sc->sc_udev); 371 if (dd->bDeviceClass == 0x02) 372 sc->sc_chiptype = TYPE_PL2303; 373 else if (dd->bMaxPacketSize == 0x40) 374 sc->sc_chiptype = TYPE_PL2303HX; 375 else 376 sc->sc_chiptype = TYPE_PL2303; 377 378 DPRINTF("chiptype: %s\n", 379 (sc->sc_chiptype == TYPE_PL2303HX) ? 380 "2303X" : "2303"); 381 382 /* 383 * USB-RSAQ1 has two interface 384 * 385 * USB-RSAQ1 | USB-RSAQ2 386 * -----------------+----------------- 387 * Interface 0 |Interface 0 388 * Interrupt(0x81) | Interrupt(0x81) 389 * -----------------+ BulkIN(0x02) 390 * Interface 1 | BulkOUT(0x83) 391 * BulkIN(0x02) | 392 * BulkOUT(0x83) | 393 */ 394 395 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum; 396 sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX; 397 398 iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX); 399 if (iface) { 400 id = usbd_get_interface_descriptor(iface); 401 if (id == NULL) { 402 device_printf(dev, "no interface descriptor (2)\n"); 403 goto detach; 404 } 405 sc->sc_data_iface_no = id->bInterfaceNumber; 406 sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX; 407 usbd_set_parent_iface(uaa->device, 408 UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex); 409 } else { 410 sc->sc_data_iface_no = sc->sc_ctrl_iface_no; 411 sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX; 412 } 413 414 error = usbd_transfer_setup(uaa->device, 415 sc->sc_iface_index, sc->sc_xfer, uplcom_config_data, 416 UPLCOM_N_TRANSFER, sc, &sc->sc_mtx); 417 if (error) { 418 DPRINTF("one or more missing USB endpoints, " 419 "error=%s\n", usbd_errstr(error)); 420 goto detach; 421 } 422 error = uplcom_reset(sc, uaa->device); 423 if (error) { 424 device_printf(dev, "reset failed, error=%s\n", 425 usbd_errstr(error)); 426 goto detach; 427 } 428 429 if (sc->sc_chiptype != TYPE_PL2303HX) { 430 /* HX variants seem to lock up after a clear stall request. */ 431 mtx_lock(&sc->sc_mtx); 432 usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 433 usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 434 mtx_unlock(&sc->sc_mtx); 435 } else { 436 if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE, 437 UPLCOM_SET_REQUEST, 8, 0, 0) || 438 uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE, 439 UPLCOM_SET_REQUEST, 9, 0, 0)) { 440 goto detach; 441 } 442 } 443 444 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 445 &uplcom_callback, &sc->sc_mtx); 446 if (error) { 447 goto detach; 448 } 449 /* 450 * do the initialization during attach so that the system does not 451 * sleep during open: 452 */ 453 if (uplcom_pl2303_init(uaa->device, sc->sc_chiptype)) { 454 device_printf(dev, "init failed\n"); 455 goto detach; 456 } 457 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev); 458 459 return (0); 460 461detach: 462 uplcom_detach(dev); 463 return (ENXIO); 464} 465 466static int 467uplcom_detach(device_t dev) 468{ 469 struct uplcom_softc *sc = device_get_softc(dev); 470 471 DPRINTF("sc=%p\n", sc); 472 473 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom); 474 usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER); 475 476 device_claim_softc(dev); 477 478 uplcom_free_softc(sc); 479 480 return (0); 481} 482 483UCOM_UNLOAD_DRAIN(uplcom); 484 485static void 486uplcom_free_softc(struct uplcom_softc *sc) 487{ 488 if (ucom_unref(&sc->sc_super_ucom)) { 489 mtx_destroy(&sc->sc_mtx); 490 device_free_softc(sc); 491 } 492} 493 494static void 495uplcom_free(struct ucom_softc *ucom) 496{ 497 uplcom_free_softc(ucom->sc_parent); 498} 499 500static usb_error_t 501uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev) 502{ 503 struct usb_device_request req; 504 505 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 506 req.bRequest = UPLCOM_SET_REQUEST; 507 USETW(req.wValue, 0); 508 req.wIndex[0] = sc->sc_data_iface_no; 509 req.wIndex[1] = 0; 510 USETW(req.wLength, 0); 511 512 return (usbd_do_request(udev, NULL, &req, NULL)); 513} 514 515static usb_error_t 516uplcom_pl2303_do(struct usb_device *udev, int8_t req_type, uint8_t request, 517 uint16_t value, uint16_t index, uint16_t length) 518{ 519 struct usb_device_request req; 520 usb_error_t err; 521 uint8_t buf[4]; 522 523 req.bmRequestType = req_type; 524 req.bRequest = request; 525 USETW(req.wValue, value); 526 USETW(req.wIndex, index); 527 USETW(req.wLength, length); 528 529 err = usbd_do_request(udev, NULL, &req, buf); 530 if (err) { 531 DPRINTF("error=%s\n", usbd_errstr(err)); 532 return (1); 533 } 534 return (0); 535} 536 537static int 538uplcom_pl2303_init(struct usb_device *udev, uint8_t chiptype) 539{ 540 int err; 541 542 if (uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1) 543 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0) 544 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1) 545 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1) 546 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1) 547 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0) 548 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1) 549 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1) 550 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0) 551 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0)) 552 return (EIO); 553 554 if (chiptype == TYPE_PL2303HX) 555 err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0); 556 else 557 err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x24, 0); 558 if (err) 559 return (EIO); 560 561 return (0); 562} 563 564static void 565uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 566{ 567 struct uplcom_softc *sc = ucom->sc_parent; 568 struct usb_device_request req; 569 570 DPRINTF("onoff = %d\n", onoff); 571 572 if (onoff) 573 sc->sc_line |= UCDC_LINE_DTR; 574 else 575 sc->sc_line &= ~UCDC_LINE_DTR; 576 577 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 578 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 579 USETW(req.wValue, sc->sc_line); 580 req.wIndex[0] = sc->sc_data_iface_no; 581 req.wIndex[1] = 0; 582 USETW(req.wLength, 0); 583 584 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 585 &req, NULL, 0, 1000); 586} 587 588static void 589uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff) 590{ 591 struct uplcom_softc *sc = ucom->sc_parent; 592 struct usb_device_request req; 593 594 DPRINTF("onoff = %d\n", onoff); 595 596 if (onoff) 597 sc->sc_line |= UCDC_LINE_RTS; 598 else 599 sc->sc_line &= ~UCDC_LINE_RTS; 600 601 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 602 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 603 USETW(req.wValue, sc->sc_line); 604 req.wIndex[0] = sc->sc_data_iface_no; 605 req.wIndex[1] = 0; 606 USETW(req.wLength, 0); 607 608 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 609 &req, NULL, 0, 1000); 610} 611 612static void 613uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff) 614{ 615 struct uplcom_softc *sc = ucom->sc_parent; 616 struct usb_device_request req; 617 uint16_t temp; 618 619 DPRINTF("onoff = %d\n", onoff); 620 621 temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF); 622 623 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 624 req.bRequest = UCDC_SEND_BREAK; 625 USETW(req.wValue, temp); 626 req.wIndex[0] = sc->sc_data_iface_no; 627 req.wIndex[1] = 0; 628 USETW(req.wLength, 0); 629 630 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 631 &req, NULL, 0, 1000); 632} 633 634static const uint32_t uplcom_rates[] = { 635 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400, 636 19200, 28800, 38400, 57600, 115200, 637 /* 638 * Higher speeds are probably possible. PL2303X supports up to 639 * 6Mb and can set any rate 640 */ 641 230400, 460800, 614400, 921600, 1228800 642}; 643 644#define N_UPLCOM_RATES (sizeof(uplcom_rates)/sizeof(uplcom_rates[0])) 645 646static int 647uplcom_pre_param(struct ucom_softc *ucom, struct termios *t) 648{ 649 struct uplcom_softc *sc = ucom->sc_parent; 650 uint8_t i; 651 652 DPRINTF("\n"); 653 654 /** 655 * Check requested baud rate. 656 * 657 * The PL2303 can only set specific baud rates, up to 1228800 baud. 658 * The PL2303X can set any baud rate up to 6Mb. 659 * The PL2303HX rev. D can set any baud rate up to 12Mb. 660 * 661 * XXX: We currently cannot identify the PL2303HX rev. D, so treat 662 * it the same as the PL2303X. 663 */ 664 if (sc->sc_chiptype != TYPE_PL2303HX) { 665 for (i = 0; i < N_UPLCOM_RATES; i++) { 666 if (uplcom_rates[i] == t->c_ospeed) 667 return (0); 668 } 669 } else { 670 if (t->c_ospeed <= 6000000) 671 return (0); 672 } 673 674 DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed); 675 return (EIO); 676} 677 678static void 679uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t) 680{ 681 struct uplcom_softc *sc = ucom->sc_parent; 682 struct usb_cdc_line_state ls; 683 struct usb_device_request req; 684 685 DPRINTF("sc = %p\n", sc); 686 687 memset(&ls, 0, sizeof(ls)); 688 689 USETDW(ls.dwDTERate, t->c_ospeed); 690 691 if (t->c_cflag & CSTOPB) { 692 ls.bCharFormat = UCDC_STOP_BIT_2; 693 } else { 694 ls.bCharFormat = UCDC_STOP_BIT_1; 695 } 696 697 if (t->c_cflag & PARENB) { 698 if (t->c_cflag & PARODD) { 699 ls.bParityType = UCDC_PARITY_ODD; 700 } else { 701 ls.bParityType = UCDC_PARITY_EVEN; 702 } 703 } else { 704 ls.bParityType = UCDC_PARITY_NONE; 705 } 706 707 switch (t->c_cflag & CSIZE) { 708 case CS5: 709 ls.bDataBits = 5; 710 break; 711 case CS6: 712 ls.bDataBits = 6; 713 break; 714 case CS7: 715 ls.bDataBits = 7; 716 break; 717 case CS8: 718 ls.bDataBits = 8; 719 break; 720 } 721 722 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n", 723 UGETDW(ls.dwDTERate), ls.bCharFormat, 724 ls.bParityType, ls.bDataBits); 725 726 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 727 req.bRequest = UCDC_SET_LINE_CODING; 728 USETW(req.wValue, 0); 729 req.wIndex[0] = sc->sc_data_iface_no; 730 req.wIndex[1] = 0; 731 USETW(req.wLength, UCDC_LINE_STATE_LENGTH); 732 733 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 734 &req, &ls, 0, 1000); 735 736 if (t->c_cflag & CRTSCTS) { 737 738 DPRINTF("crtscts = on\n"); 739 740 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 741 req.bRequest = UPLCOM_SET_REQUEST; 742 USETW(req.wValue, 0); 743 if (sc->sc_chiptype == TYPE_PL2303HX) 744 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X); 745 else 746 USETW(req.wIndex, UPLCOM_SET_CRTSCTS); 747 USETW(req.wLength, 0); 748 749 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 750 &req, NULL, 0, 1000); 751 } else { 752 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 753 req.bRequest = UPLCOM_SET_REQUEST; 754 USETW(req.wValue, 0); 755 USETW(req.wIndex, 0); 756 USETW(req.wLength, 0); 757 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 758 &req, NULL, 0, 1000); 759 } 760} 761 762static void 763uplcom_start_read(struct ucom_softc *ucom) 764{ 765 struct uplcom_softc *sc = ucom->sc_parent; 766 767 /* start interrupt endpoint */ 768 usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]); 769 770 /* start read endpoint */ 771 usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 772} 773 774static void 775uplcom_stop_read(struct ucom_softc *ucom) 776{ 777 struct uplcom_softc *sc = ucom->sc_parent; 778 779 /* stop interrupt endpoint */ 780 usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]); 781 782 /* stop read endpoint */ 783 usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 784} 785 786static void 787uplcom_start_write(struct ucom_softc *ucom) 788{ 789 struct uplcom_softc *sc = ucom->sc_parent; 790 791 usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 792} 793 794static void 795uplcom_stop_write(struct ucom_softc *ucom) 796{ 797 struct uplcom_softc *sc = ucom->sc_parent; 798 799 usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 800} 801 802static void 803uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 804{ 805 struct uplcom_softc *sc = ucom->sc_parent; 806 807 DPRINTF("\n"); 808 809 *lsr = sc->sc_lsr; 810 *msr = sc->sc_msr; 811} 812 813static void 814uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error) 815{ 816 struct uplcom_softc *sc = usbd_xfer_softc(xfer); 817 struct usb_page_cache *pc; 818 uint8_t buf[9]; 819 int actlen; 820 821 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 822 823 switch (USB_GET_STATE(xfer)) { 824 case USB_ST_TRANSFERRED: 825 826 DPRINTF("actlen = %u\n", actlen); 827 828 if (actlen >= 9) { 829 830 pc = usbd_xfer_get_frame(xfer, 0); 831 usbd_copy_out(pc, 0, buf, sizeof(buf)); 832 833 DPRINTF("status = 0x%02x\n", buf[8]); 834 835 sc->sc_lsr = 0; 836 sc->sc_msr = 0; 837 838 if (buf[8] & RSAQ_STATUS_CTS) { 839 sc->sc_msr |= SER_CTS; 840 } 841 if (buf[8] & RSAQ_STATUS_DSR) { 842 sc->sc_msr |= SER_DSR; 843 } 844 if (buf[8] & RSAQ_STATUS_DCD) { 845 sc->sc_msr |= SER_DCD; 846 } 847 ucom_status_change(&sc->sc_ucom); 848 } 849 case USB_ST_SETUP: 850tr_setup: 851 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 852 usbd_transfer_submit(xfer); 853 return; 854 855 default: /* Error */ 856 if (error != USB_ERR_CANCELLED) { 857 /* try to clear stall first */ 858 usbd_xfer_set_stall(xfer); 859 goto tr_setup; 860 } 861 return; 862 } 863} 864 865static void 866uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error) 867{ 868 struct uplcom_softc *sc = usbd_xfer_softc(xfer); 869 struct usb_page_cache *pc; 870 uint32_t actlen; 871 872 switch (USB_GET_STATE(xfer)) { 873 case USB_ST_SETUP: 874 case USB_ST_TRANSFERRED: 875tr_setup: 876 pc = usbd_xfer_get_frame(xfer, 0); 877 if (ucom_get_data(&sc->sc_ucom, pc, 0, 878 UPLCOM_BULK_BUF_SIZE, &actlen)) { 879 880 DPRINTF("actlen = %d\n", actlen); 881 882 usbd_xfer_set_frame_len(xfer, 0, actlen); 883 usbd_transfer_submit(xfer); 884 } 885 return; 886 887 default: /* Error */ 888 if (error != USB_ERR_CANCELLED) { 889 /* try to clear stall first */ 890 usbd_xfer_set_stall(xfer); 891 goto tr_setup; 892 } 893 return; 894 } 895} 896 897static void 898uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error) 899{ 900 struct uplcom_softc *sc = usbd_xfer_softc(xfer); 901 struct usb_page_cache *pc; 902 int actlen; 903 904 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 905 906 switch (USB_GET_STATE(xfer)) { 907 case USB_ST_TRANSFERRED: 908 pc = usbd_xfer_get_frame(xfer, 0); 909 ucom_put_data(&sc->sc_ucom, pc, 0, actlen); 910 911 case USB_ST_SETUP: 912tr_setup: 913 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 914 usbd_transfer_submit(xfer); 915 return; 916 917 default: /* Error */ 918 if (error != USB_ERR_CANCELLED) { 919 /* try to clear stall first */ 920 usbd_xfer_set_stall(xfer); 921 goto tr_setup; 922 } 923 return; 924 } 925} 926 927static void 928uplcom_poll(struct ucom_softc *ucom) 929{ 930 struct uplcom_softc *sc = ucom->sc_parent; 931 usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER); 932} 933