102ac6454SAndrew Thompson /* $NetBSD: uplcom.c,v 1.21 2001/11/13 06:24:56 lukem Exp $ */ 202ac6454SAndrew Thompson 302ac6454SAndrew Thompson #include <sys/cdefs.h> 402ac6454SAndrew Thompson __FBSDID("$FreeBSD$"); 502ac6454SAndrew Thompson 602ac6454SAndrew Thompson /*- 7718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause-NetBSD 8718cf2ccSPedro F. Giffuni * 902ac6454SAndrew Thompson * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>. 1002ac6454SAndrew Thompson * All rights reserved. 1102ac6454SAndrew Thompson * 1202ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 1302ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 1402ac6454SAndrew Thompson * are met: 1502ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 1602ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1702ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1802ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1902ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 2002ac6454SAndrew Thompson * 2102ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2202ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2302ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2402ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2502ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2602ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2702ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2802ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2902ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3002ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3102ac6454SAndrew Thompson * SUCH DAMAGE. 3202ac6454SAndrew Thompson */ 3302ac6454SAndrew Thompson 3402ac6454SAndrew Thompson /*- 3502ac6454SAndrew Thompson * Copyright (c) 2001 The NetBSD Foundation, Inc. 3602ac6454SAndrew Thompson * All rights reserved. 3702ac6454SAndrew Thompson * 3802ac6454SAndrew Thompson * This code is derived from software contributed to The NetBSD Foundation 3902ac6454SAndrew Thompson * by Ichiro FUKUHARA (ichiro@ichiro.org). 4002ac6454SAndrew Thompson * 4102ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 4202ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 4302ac6454SAndrew Thompson * are met: 4402ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 4502ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 4602ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 4702ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 4802ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 4902ac6454SAndrew Thompson * 5002ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 5102ac6454SAndrew Thompson * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 5202ac6454SAndrew Thompson * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 5302ac6454SAndrew Thompson * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 5402ac6454SAndrew Thompson * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 5502ac6454SAndrew Thompson * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 5602ac6454SAndrew Thompson * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 5702ac6454SAndrew Thompson * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 5802ac6454SAndrew Thompson * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 5902ac6454SAndrew Thompson * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 6002ac6454SAndrew Thompson * POSSIBILITY OF SUCH DAMAGE. 6102ac6454SAndrew Thompson */ 6202ac6454SAndrew Thompson 6302ac6454SAndrew Thompson /* 6402ac6454SAndrew Thompson * This driver supports several USB-to-RS232 serial adapters driven by 6502ac6454SAndrew Thompson * Prolific PL-2303, PL-2303X and probably PL-2303HX USB-to-RS232 6602ac6454SAndrew Thompson * bridge chip. The adapters are sold under many different brand 6702ac6454SAndrew Thompson * names. 6802ac6454SAndrew Thompson * 6902ac6454SAndrew Thompson * Datasheets are available at Prolific www site at 7002ac6454SAndrew Thompson * http://www.prolific.com.tw. The datasheets don't contain full 7102ac6454SAndrew Thompson * programming information for the chip. 7202ac6454SAndrew Thompson * 7302ac6454SAndrew Thompson * PL-2303HX is probably programmed the same as PL-2303X. 7402ac6454SAndrew Thompson * 7502ac6454SAndrew Thompson * There are several differences between PL-2303 and PL-2303(H)X. 7602ac6454SAndrew Thompson * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_ 7702ac6454SAndrew Thompson * different command for controlling CRTSCTS and needs special 7802ac6454SAndrew Thompson * sequence of commands for initialization which aren't also 7902ac6454SAndrew Thompson * documented in the datasheet. 8002ac6454SAndrew Thompson */ 8102ac6454SAndrew Thompson 82ed6d949aSAndrew Thompson #include <sys/stdint.h> 83ed6d949aSAndrew Thompson #include <sys/stddef.h> 84ed6d949aSAndrew Thompson #include <sys/param.h> 85ed6d949aSAndrew Thompson #include <sys/queue.h> 86ed6d949aSAndrew Thompson #include <sys/types.h> 87ed6d949aSAndrew Thompson #include <sys/systm.h> 88ed6d949aSAndrew Thompson #include <sys/kernel.h> 89ed6d949aSAndrew Thompson #include <sys/bus.h> 90ed6d949aSAndrew Thompson #include <sys/module.h> 91ed6d949aSAndrew Thompson #include <sys/lock.h> 92ed6d949aSAndrew Thompson #include <sys/mutex.h> 93ed6d949aSAndrew Thompson #include <sys/condvar.h> 94ed6d949aSAndrew Thompson #include <sys/sysctl.h> 95ed6d949aSAndrew Thompson #include <sys/sx.h> 96ed6d949aSAndrew Thompson #include <sys/unistd.h> 97ed6d949aSAndrew Thompson #include <sys/callout.h> 98ed6d949aSAndrew Thompson #include <sys/malloc.h> 99ed6d949aSAndrew Thompson #include <sys/priv.h> 100ed6d949aSAndrew Thompson 10102ac6454SAndrew Thompson #include <dev/usb/usb.h> 102ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 103ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h> 10402ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h> 105ed6d949aSAndrew Thompson #include "usbdevs.h" 10602ac6454SAndrew Thompson 10702ac6454SAndrew Thompson #define USB_DEBUG_VAR uplcom_debug 10802ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 10902ac6454SAndrew Thompson #include <dev/usb/usb_process.h> 11002ac6454SAndrew Thompson 11102ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h> 11202ac6454SAndrew Thompson 113b850ecc1SAndrew Thompson #ifdef USB_DEBUG 11402ac6454SAndrew Thompson static int uplcom_debug = 0; 11502ac6454SAndrew Thompson 116f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 117f8d2b1f3SPawel Biernacki "USB uplcom"); 118ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RWTUN, 11902ac6454SAndrew Thompson &uplcom_debug, 0, "Debug level"); 12002ac6454SAndrew Thompson #endif 12102ac6454SAndrew Thompson 12202ac6454SAndrew Thompson #define UPLCOM_MODVER 1 /* module version */ 12302ac6454SAndrew Thompson 12402ac6454SAndrew Thompson #define UPLCOM_CONFIG_INDEX 0 12502ac6454SAndrew Thompson #define UPLCOM_IFACE_INDEX 0 12602ac6454SAndrew Thompson #define UPLCOM_SECOND_IFACE_INDEX 1 12702ac6454SAndrew Thompson 12802ac6454SAndrew Thompson #ifndef UPLCOM_INTR_INTERVAL 12902ac6454SAndrew Thompson #define UPLCOM_INTR_INTERVAL 0 /* default */ 13002ac6454SAndrew Thompson #endif 13102ac6454SAndrew Thompson 13202ac6454SAndrew Thompson #define UPLCOM_BULK_BUF_SIZE 1024 /* bytes */ 13302ac6454SAndrew Thompson 13402ac6454SAndrew Thompson #define UPLCOM_SET_REQUEST 0x01 135ea0efc37SHans Petter Selasky #define UPLCOM_SET_REQUEST_PL2303HXN 0x80 13602ac6454SAndrew Thompson #define UPLCOM_SET_CRTSCTS 0x41 13702ac6454SAndrew Thompson #define UPLCOM_SET_CRTSCTS_PL2303X 0x61 138ea0efc37SHans Petter Selasky #define UPLCOM_SET_CRTSCTS_PL2303HXN 0xFA 139ea0efc37SHans Petter Selasky #define UPLCOM_CLEAR_CRTSCTS_PL2303HXN 0xFF 140ea0efc37SHans Petter Selasky #define UPLCOM_CRTSCTS_REG_PL2303HXN 0x0A 141ea0efc37SHans Petter Selasky #define UPLCOM_STATUS_REG_PL2303HX 0x8080 14202ac6454SAndrew Thompson #define RSAQ_STATUS_CTS 0x80 14327e4bd81SHans Petter Selasky #define RSAQ_STATUS_OVERRUN_ERROR 0x40 14427e4bd81SHans Petter Selasky #define RSAQ_STATUS_PARITY_ERROR 0x20 14527e4bd81SHans Petter Selasky #define RSAQ_STATUS_FRAME_ERROR 0x10 14627e4bd81SHans Petter Selasky #define RSAQ_STATUS_RING 0x08 14727e4bd81SHans Petter Selasky #define RSAQ_STATUS_BREAK_ERROR 0x04 14802ac6454SAndrew Thompson #define RSAQ_STATUS_DSR 0x02 14902ac6454SAndrew Thompson #define RSAQ_STATUS_DCD 0x01 15002ac6454SAndrew Thompson 15102ac6454SAndrew Thompson #define TYPE_PL2303 0 152bc65068dSGavin Atkinson #define TYPE_PL2303HX 1 15327e4bd81SHans Petter Selasky #define TYPE_PL2303HXD 2 154ea0efc37SHans Petter Selasky #define TYPE_PL2303HXN 3 15527e4bd81SHans Petter Selasky 15627e4bd81SHans Petter Selasky #define UPLCOM_STATE_INDEX 8 15702ac6454SAndrew Thompson 15802ac6454SAndrew Thompson enum { 15902ac6454SAndrew Thompson UPLCOM_BULK_DT_WR, 16002ac6454SAndrew Thompson UPLCOM_BULK_DT_RD, 16102ac6454SAndrew Thompson UPLCOM_INTR_DT_RD, 16202ac6454SAndrew Thompson UPLCOM_N_TRANSFER, 16302ac6454SAndrew Thompson }; 16402ac6454SAndrew Thompson 16502ac6454SAndrew Thompson struct uplcom_softc { 166760bc48eSAndrew Thompson struct ucom_super_softc sc_super_ucom; 167760bc48eSAndrew Thompson struct ucom_softc sc_ucom; 16802ac6454SAndrew Thompson 169760bc48eSAndrew Thompson struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER]; 170760bc48eSAndrew Thompson struct usb_device *sc_udev; 171deefe583SAndrew Thompson struct mtx sc_mtx; 17202ac6454SAndrew Thompson 17302ac6454SAndrew Thompson uint16_t sc_line; 17402ac6454SAndrew Thompson 17502ac6454SAndrew Thompson uint8_t sc_lsr; /* local status register */ 17602ac6454SAndrew Thompson uint8_t sc_msr; /* uplcom status register */ 17702ac6454SAndrew Thompson uint8_t sc_chiptype; /* type of chip */ 17802ac6454SAndrew Thompson uint8_t sc_ctrl_iface_no; 17902ac6454SAndrew Thompson uint8_t sc_data_iface_no; 18002ac6454SAndrew Thompson uint8_t sc_iface_index[2]; 18102ac6454SAndrew Thompson }; 18202ac6454SAndrew Thompson 18302ac6454SAndrew Thompson /* prototypes */ 18402ac6454SAndrew Thompson 185e0a69b51SAndrew Thompson static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *); 18602d4a225SDimitry Andric static usb_error_t uplcom_pl2303_do(struct usb_device *, uint8_t, uint8_t, 187bc65068dSGavin Atkinson uint16_t, uint16_t, uint16_t); 188bc65068dSGavin Atkinson static int uplcom_pl2303_init(struct usb_device *, uint8_t); 1895805d178SHans Petter Selasky static void uplcom_free(struct ucom_softc *); 190760bc48eSAndrew Thompson static void uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t); 191760bc48eSAndrew Thompson static void uplcom_cfg_set_rts(struct ucom_softc *, uint8_t); 192760bc48eSAndrew Thompson static void uplcom_cfg_set_break(struct ucom_softc *, uint8_t); 193760bc48eSAndrew Thompson static int uplcom_pre_param(struct ucom_softc *, struct termios *); 194760bc48eSAndrew Thompson static void uplcom_cfg_param(struct ucom_softc *, struct termios *); 195760bc48eSAndrew Thompson static void uplcom_start_read(struct ucom_softc *); 196760bc48eSAndrew Thompson static void uplcom_stop_read(struct ucom_softc *); 197760bc48eSAndrew Thompson static void uplcom_start_write(struct ucom_softc *); 198760bc48eSAndrew Thompson static void uplcom_stop_write(struct ucom_softc *); 199760bc48eSAndrew Thompson static void uplcom_cfg_get_status(struct ucom_softc *, uint8_t *, 20002ac6454SAndrew Thompson uint8_t *); 201655dc9d0SAndrew Thompson static void uplcom_poll(struct ucom_softc *ucom); 20202ac6454SAndrew Thompson 20302ac6454SAndrew Thompson static device_probe_t uplcom_probe; 20402ac6454SAndrew Thompson static device_attach_t uplcom_attach; 20502ac6454SAndrew Thompson static device_detach_t uplcom_detach; 206c01fc06eSHans Petter Selasky static void uplcom_free_softc(struct uplcom_softc *); 20702ac6454SAndrew Thompson 208e0a69b51SAndrew Thompson static usb_callback_t uplcom_intr_callback; 209e0a69b51SAndrew Thompson static usb_callback_t uplcom_write_callback; 210e0a69b51SAndrew Thompson static usb_callback_t uplcom_read_callback; 21102ac6454SAndrew Thompson 212760bc48eSAndrew Thompson static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = { 21302ac6454SAndrew Thompson [UPLCOM_BULK_DT_WR] = { 21402ac6454SAndrew Thompson .type = UE_BULK, 21502ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 21602ac6454SAndrew Thompson .direction = UE_DIR_OUT, 2174eae601eSAndrew Thompson .bufsize = UPLCOM_BULK_BUF_SIZE, 2184eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 2194eae601eSAndrew Thompson .callback = &uplcom_write_callback, 22002ac6454SAndrew Thompson .if_index = 0, 22102ac6454SAndrew Thompson }, 22202ac6454SAndrew Thompson 22302ac6454SAndrew Thompson [UPLCOM_BULK_DT_RD] = { 22402ac6454SAndrew Thompson .type = UE_BULK, 22502ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 22602ac6454SAndrew Thompson .direction = UE_DIR_IN, 2274eae601eSAndrew Thompson .bufsize = UPLCOM_BULK_BUF_SIZE, 2284eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 2294eae601eSAndrew Thompson .callback = &uplcom_read_callback, 23002ac6454SAndrew Thompson .if_index = 0, 23102ac6454SAndrew Thompson }, 23202ac6454SAndrew Thompson 23302ac6454SAndrew Thompson [UPLCOM_INTR_DT_RD] = { 23402ac6454SAndrew Thompson .type = UE_INTERRUPT, 23502ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 23602ac6454SAndrew Thompson .direction = UE_DIR_IN, 2374eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 2384eae601eSAndrew Thompson .bufsize = 0, /* use wMaxPacketSize */ 2394eae601eSAndrew Thompson .callback = &uplcom_intr_callback, 24002ac6454SAndrew Thompson .if_index = 1, 24102ac6454SAndrew Thompson }, 24202ac6454SAndrew Thompson }; 24302ac6454SAndrew Thompson 244a8675caeSAndrew Thompson static struct ucom_callback uplcom_callback = { 245a593f6b8SAndrew Thompson .ucom_cfg_get_status = &uplcom_cfg_get_status, 246a593f6b8SAndrew Thompson .ucom_cfg_set_dtr = &uplcom_cfg_set_dtr, 247a593f6b8SAndrew Thompson .ucom_cfg_set_rts = &uplcom_cfg_set_rts, 248a593f6b8SAndrew Thompson .ucom_cfg_set_break = &uplcom_cfg_set_break, 249a593f6b8SAndrew Thompson .ucom_cfg_param = &uplcom_cfg_param, 250a593f6b8SAndrew Thompson .ucom_pre_param = &uplcom_pre_param, 251a593f6b8SAndrew Thompson .ucom_start_read = &uplcom_start_read, 252a593f6b8SAndrew Thompson .ucom_stop_read = &uplcom_stop_read, 253a593f6b8SAndrew Thompson .ucom_start_write = &uplcom_start_write, 254a593f6b8SAndrew Thompson .ucom_stop_write = &uplcom_stop_write, 255655dc9d0SAndrew Thompson .ucom_poll = &uplcom_poll, 2565805d178SHans Petter Selasky .ucom_free = &uplcom_free, 25702ac6454SAndrew Thompson }; 25802ac6454SAndrew Thompson 259bc65068dSGavin Atkinson #define UPLCOM_DEV(v,p) \ 260bc65068dSGavin Atkinson { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p) } 26102ac6454SAndrew Thompson 262f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID uplcom_devs[] = { 263f5113df9SGavin Atkinson UPLCOM_DEV(ACERP, S81), /* BenQ S81 phone */ 264f5113df9SGavin Atkinson UPLCOM_DEV(ADLINK, ND6530), /* ADLINK ND-6530 USB-Serial */ 265f5113df9SGavin Atkinson UPLCOM_DEV(ALCATEL, OT535), /* Alcatel One Touch 535/735 */ 266f5113df9SGavin Atkinson UPLCOM_DEV(ALCOR, AU9720), /* Alcor AU9720 USB 2.0-RS232 */ 267f5113df9SGavin Atkinson UPLCOM_DEV(ANCHOR, SERIAL), /* Anchor Serial adapter */ 268bc65068dSGavin Atkinson UPLCOM_DEV(ATEN, UC232A), /* PLANEX USB-RS232 URS-03 */ 26946a5f883SEd Maste UPLCOM_DEV(ATEN, UC232B), /* Prolific USB-RS232 Controller D */ 27001a8f075SGavin Atkinson UPLCOM_DEV(BELKIN, F5U257), /* Belkin F5U257 USB to Serial */ 271bc65068dSGavin Atkinson UPLCOM_DEV(COREGA, CGUSBRS232R), /* Corega CG-USBRS232R */ 272f5113df9SGavin Atkinson UPLCOM_DEV(EPSON, CRESSI_EDY), /* Cressi Edy diving computer */ 273c6ac426dSGavin Atkinson UPLCOM_DEV(EPSON, N2ITION3), /* Zeagle N2iTion3 diving computer */ 27401a8f075SGavin Atkinson UPLCOM_DEV(ELECOM, UCSGT), /* ELECOM UC-SGT Serial Adapter */ 27501a8f075SGavin Atkinson UPLCOM_DEV(ELECOM, UCSGT0), /* ELECOM UC-SGT Serial Adapter */ 276bc65068dSGavin Atkinson UPLCOM_DEV(HAL, IMR001), /* HAL Corporation Crossam2+USB */ 277f5113df9SGavin Atkinson UPLCOM_DEV(HP, LD220), /* HP LD220 POS Display */ 278bc65068dSGavin Atkinson UPLCOM_DEV(IODATA, USBRSAQ), /* I/O DATA USB-RSAQ */ 279bc65068dSGavin Atkinson UPLCOM_DEV(IODATA, USBRSAQ5), /* I/O DATA USB-RSAQ5 */ 280f5113df9SGavin Atkinson UPLCOM_DEV(ITEGNO, WM1080A), /* iTegno WM1080A GSM/GFPRS modem */ 281f5113df9SGavin Atkinson UPLCOM_DEV(ITEGNO, WM2080A), /* iTegno WM2080A CDMA modem */ 282f5113df9SGavin Atkinson UPLCOM_DEV(LEADTEK, 9531), /* Leadtek 9531 GPS */ 283f5113df9SGavin Atkinson UPLCOM_DEV(MICROSOFT, 700WX), /* Microsoft Palm 700WX */ 284bc65068dSGavin Atkinson UPLCOM_DEV(MOBILEACTION, MA620), /* Mobile Action MA-620 Infrared Adapter */ 285f5113df9SGavin Atkinson UPLCOM_DEV(NETINDEX, WS002IN), /* Willcom W-S002IN */ 286f5113df9SGavin Atkinson UPLCOM_DEV(NOKIA2, CA42), /* Nokia CA-42 cable */ 287f5113df9SGavin Atkinson UPLCOM_DEV(OTI, DKU5), /* OTI DKU-5 cable */ 288f5113df9SGavin Atkinson UPLCOM_DEV(PANASONIC, TYTP50P6S), /* Panasonic TY-TP50P6-S flat screen */ 289f5113df9SGavin Atkinson UPLCOM_DEV(PLX, CA42), /* PLX CA-42 clone cable */ 290f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, ALLTRONIX_GPRS), /* Alltronix ACM003U00 modem */ 291f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, ALDIGA_AL11U), /* AlDiga AL-11U modem */ 292f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, DCU11), /* DCU-11 Phone Cable */ 293f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, HCR331), /* HCR331 Card Reader */ 294f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, MICROMAX_610U), /* Micromax 610U modem */ 295d01755edSGavin Atkinson UPLCOM_DEV(PROLIFIC, MOTOROLA), /* Motorola cable */ 296bc65068dSGavin Atkinson UPLCOM_DEV(PROLIFIC, PHAROS), /* Prolific Pharos */ 297f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, PL2303), /* Generic adapter */ 298ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GC), /* Generic adapter (PL2303HXN, type GC) */ 299ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GB), /* Generic adapter (PL2303HXN, type GB) */ 300ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GT), /* Generic adapter (PL2303HXN, type GT) */ 301ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GL), /* Generic adapter (PL2303HXN, type GL) */ 302ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GE), /* Generic adapter (PL2303HXN, type GE) */ 303ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GS), /* Generic adapter (PL2303HXN, type GS) */ 304bc65068dSGavin Atkinson UPLCOM_DEV(PROLIFIC, RSAQ2), /* I/O DATA USB-RSAQ2 */ 305bc65068dSGavin Atkinson UPLCOM_DEV(PROLIFIC, RSAQ3), /* I/O DATA USB-RSAQ3 */ 306635c9457SGavin Atkinson UPLCOM_DEV(PROLIFIC, UIC_MSR206), /* UIC MSR206 Card Reader */ 307f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC2, PL2303), /* Prolific adapter */ 30801a8f075SGavin Atkinson UPLCOM_DEV(RADIOSHACK, USBCABLE), /* Radio Shack USB Adapter */ 309bc65068dSGavin Atkinson UPLCOM_DEV(RATOC, REXUSB60), /* RATOC REX-USB60 */ 310bc65068dSGavin Atkinson UPLCOM_DEV(SAGEM, USBSERIAL), /* Sagem USB-Serial Controller */ 311f5113df9SGavin Atkinson UPLCOM_DEV(SAMSUNG, I330), /* Samsung I330 phone cradle */ 312f5113df9SGavin Atkinson UPLCOM_DEV(SANWA, KB_USB2), /* Sanwa KB-USB2 Multimeter cable */ 31301a8f075SGavin Atkinson UPLCOM_DEV(SIEMENS3, EF81), /* Siemens EF81 */ 31401a8f075SGavin Atkinson UPLCOM_DEV(SIEMENS3, SX1), /* Siemens SX1 */ 31501a8f075SGavin Atkinson UPLCOM_DEV(SIEMENS3, X65), /* Siemens X65 */ 31601a8f075SGavin Atkinson UPLCOM_DEV(SIEMENS3, X75), /* Siemens X75 */ 317bc65068dSGavin Atkinson UPLCOM_DEV(SITECOM, SERIAL), /* Sitecom USB to Serial */ 31801a8f075SGavin Atkinson UPLCOM_DEV(SMART, PL2303), /* SMART Technologies USB to Serial */ 319f5113df9SGavin Atkinson UPLCOM_DEV(SONY, QN3), /* Sony QN3 phone cable */ 320f5113df9SGavin Atkinson UPLCOM_DEV(SONYERICSSON, DATAPILOT), /* Sony Ericsson Datapilot */ 321f5113df9SGavin Atkinson UPLCOM_DEV(SONYERICSSON, DCU10), /* Sony Ericsson DCU-10 Cable */ 322bc65068dSGavin Atkinson UPLCOM_DEV(SOURCENEXT, KEIKAI8), /* SOURCENEXT KeikaiDenwa 8 */ 323bc65068dSGavin Atkinson UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG), /* SOURCENEXT KeikaiDenwa 8 with charger */ 324f5113df9SGavin Atkinson UPLCOM_DEV(SPEEDDRAGON, MS3303H), /* Speed Dragon USB-Serial */ 325f5113df9SGavin Atkinson UPLCOM_DEV(SYNTECH, CPT8001C), /* Syntech CPT-8001C Barcode scanner */ 326bc65068dSGavin Atkinson UPLCOM_DEV(TDK, UHA6400), /* TDK USB-PHS Adapter UHA6400 */ 327f5113df9SGavin Atkinson UPLCOM_DEV(TDK, UPA9664), /* TDK USB-PHS Adapter UPA9664 */ 32801a8f075SGavin Atkinson UPLCOM_DEV(TRIPPLITE, U209), /* Tripp-Lite U209-000-R USB to Serial */ 329f5113df9SGavin Atkinson UPLCOM_DEV(YCCABLE, PL2303), /* YC Cable USB-Serial */ 33002ac6454SAndrew Thompson }; 3319e6b5313SAndrew Thompson #undef UPLCOM_DEV 33202ac6454SAndrew Thompson 33302ac6454SAndrew Thompson static device_method_t uplcom_methods[] = { 33402ac6454SAndrew Thompson DEVMETHOD(device_probe, uplcom_probe), 33502ac6454SAndrew Thompson DEVMETHOD(device_attach, uplcom_attach), 33602ac6454SAndrew Thompson DEVMETHOD(device_detach, uplcom_detach), 3375805d178SHans Petter Selasky DEVMETHOD_END 33802ac6454SAndrew Thompson }; 33902ac6454SAndrew Thompson 34002ac6454SAndrew Thompson static devclass_t uplcom_devclass; 34102ac6454SAndrew Thompson 34202ac6454SAndrew Thompson static driver_t uplcom_driver = { 34302ac6454SAndrew Thompson .name = "uplcom", 34402ac6454SAndrew Thompson .methods = uplcom_methods, 34502ac6454SAndrew Thompson .size = sizeof(struct uplcom_softc), 34602ac6454SAndrew Thompson }; 34702ac6454SAndrew Thompson 3489aef556dSAndrew Thompson DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0); 34902ac6454SAndrew Thompson MODULE_DEPEND(uplcom, ucom, 1, 1, 1); 35002ac6454SAndrew Thompson MODULE_DEPEND(uplcom, usb, 1, 1, 1); 35102ac6454SAndrew Thompson MODULE_VERSION(uplcom, UPLCOM_MODVER); 352f809f280SWarner Losh USB_PNP_HOST_INFO(uplcom_devs); 35302ac6454SAndrew Thompson 35402ac6454SAndrew Thompson static int 35502ac6454SAndrew Thompson uplcom_probe(device_t dev) 35602ac6454SAndrew Thompson { 357760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 35802ac6454SAndrew Thompson 35902ac6454SAndrew Thompson DPRINTFN(11, "\n"); 36002ac6454SAndrew Thompson 361f29a0724SAndrew Thompson if (uaa->usb_mode != USB_MODE_HOST) { 36202ac6454SAndrew Thompson return (ENXIO); 36302ac6454SAndrew Thompson } 36402ac6454SAndrew Thompson if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) { 36502ac6454SAndrew Thompson return (ENXIO); 36602ac6454SAndrew Thompson } 36702ac6454SAndrew Thompson if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) { 36802ac6454SAndrew Thompson return (ENXIO); 36902ac6454SAndrew Thompson } 370a593f6b8SAndrew Thompson return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa)); 37102ac6454SAndrew Thompson } 37202ac6454SAndrew Thompson 37302ac6454SAndrew Thompson static int 37402ac6454SAndrew Thompson uplcom_attach(device_t dev) 37502ac6454SAndrew Thompson { 376760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 37702ac6454SAndrew Thompson struct uplcom_softc *sc = device_get_softc(dev); 378760bc48eSAndrew Thompson struct usb_interface *iface; 379760bc48eSAndrew Thompson struct usb_interface_descriptor *id; 380bc65068dSGavin Atkinson struct usb_device_descriptor *dd; 38102ac6454SAndrew Thompson int error; 38202ac6454SAndrew Thompson 383ea0efc37SHans Petter Selasky struct usb_device_request req; 384ea0efc37SHans Petter Selasky usb_error_t err; 385ea0efc37SHans Petter Selasky uint8_t buf[4]; 386ea0efc37SHans Petter Selasky 38702ac6454SAndrew Thompson DPRINTFN(11, "\n"); 38802ac6454SAndrew Thompson 389a593f6b8SAndrew Thompson device_set_usb_desc(dev); 390deefe583SAndrew Thompson mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF); 3915805d178SHans Petter Selasky ucom_ref(&sc->sc_super_ucom); 39202ac6454SAndrew Thompson 39302ac6454SAndrew Thompson DPRINTF("sc = %p\n", sc); 39402ac6454SAndrew Thompson 39502ac6454SAndrew Thompson sc->sc_udev = uaa->device; 39602ac6454SAndrew Thompson 397bc65068dSGavin Atkinson dd = usbd_get_device_descriptor(sc->sc_udev); 39827e4bd81SHans Petter Selasky 39927e4bd81SHans Petter Selasky switch (UGETW(dd->bcdDevice)) { 40027e4bd81SHans Petter Selasky case 0x0300: 40127e4bd81SHans Petter Selasky sc->sc_chiptype = TYPE_PL2303HX; 40227e4bd81SHans Petter Selasky /* or TA, that is HX with external crystal */ 40327e4bd81SHans Petter Selasky break; 40427e4bd81SHans Petter Selasky case 0x0400: 40527e4bd81SHans Petter Selasky sc->sc_chiptype = TYPE_PL2303HXD; 40627e4bd81SHans Petter Selasky /* or EA, that is HXD with ESD protection */ 40727e4bd81SHans Petter Selasky /* or RA, that has internal voltage level converter that works only up to 1Mbaud (!) */ 40827e4bd81SHans Petter Selasky break; 40927e4bd81SHans Petter Selasky case 0x0500: 41027e4bd81SHans Petter Selasky sc->sc_chiptype = TYPE_PL2303HXD; 41127e4bd81SHans Petter Selasky /* in fact it's TB, that is HXD with external crystal */ 41227e4bd81SHans Petter Selasky break; 41327e4bd81SHans Petter Selasky default: 41427e4bd81SHans Petter Selasky /* NOTE: I have no info about the bcdDevice for the base PL2303 (up to 1.2Mbaud, 41527e4bd81SHans Petter Selasky only fixed rates) and for PL2303SA (8-pin chip, up to 115200 baud */ 41627e4bd81SHans Petter Selasky /* Determine the chip type. This algorithm is taken from Linux. */ 417bc65068dSGavin Atkinson if (dd->bDeviceClass == 0x02) 418bc65068dSGavin Atkinson sc->sc_chiptype = TYPE_PL2303; 419bc65068dSGavin Atkinson else if (dd->bMaxPacketSize == 0x40) 420bc65068dSGavin Atkinson sc->sc_chiptype = TYPE_PL2303HX; 421bc65068dSGavin Atkinson else 422bc65068dSGavin Atkinson sc->sc_chiptype = TYPE_PL2303; 42327e4bd81SHans Petter Selasky break; 42427e4bd81SHans Petter Selasky } 425bc65068dSGavin Atkinson 426ea0efc37SHans Petter Selasky /* 427ea0efc37SHans Petter Selasky * The new chip revision PL2303HXN is only compatible with the new 428ea0efc37SHans Petter Selasky * UPLCOM_SET_REQUEST_PL2303HXN command. Issuing the old command 429ea0efc37SHans Petter Selasky * UPLCOM_SET_REQUEST to the new chip raises an error. Thus, PL2303HX 430ea0efc37SHans Petter Selasky * and PL2303HXN can be distinguished by issuing an old-style request 431ea0efc37SHans Petter Selasky * (on a status register) to the new chip and checking the error. 432ea0efc37SHans Petter Selasky */ 433ea0efc37SHans Petter Selasky if (sc->sc_chiptype == TYPE_PL2303HX) { 434ea0efc37SHans Petter Selasky req.bmRequestType = UT_READ_VENDOR_DEVICE; 435ea0efc37SHans Petter Selasky req.bRequest = UPLCOM_SET_REQUEST; 436ea0efc37SHans Petter Selasky USETW(req.wValue, UPLCOM_STATUS_REG_PL2303HX); 437ea0efc37SHans Petter Selasky req.wIndex[0] = sc->sc_data_iface_no; 438ea0efc37SHans Petter Selasky req.wIndex[1] = 0; 439ea0efc37SHans Petter Selasky USETW(req.wLength, 1); 440ea0efc37SHans Petter Selasky err = usbd_do_request(sc->sc_udev, NULL, &req, buf); 441ea0efc37SHans Petter Selasky if (err) 442ea0efc37SHans Petter Selasky sc->sc_chiptype = TYPE_PL2303HXN; 443ea0efc37SHans Petter Selasky } 444ea0efc37SHans Petter Selasky 44527e4bd81SHans Petter Selasky switch (sc->sc_chiptype) { 44627e4bd81SHans Petter Selasky case TYPE_PL2303: 44727e4bd81SHans Petter Selasky DPRINTF("chiptype: 2303\n"); 44827e4bd81SHans Petter Selasky break; 44927e4bd81SHans Petter Selasky case TYPE_PL2303HX: 45027e4bd81SHans Petter Selasky DPRINTF("chiptype: 2303HX/TA\n"); 45127e4bd81SHans Petter Selasky break; 452ea0efc37SHans Petter Selasky case TYPE_PL2303HXN: 453ea0efc37SHans Petter Selasky DPRINTF("chiptype: 2303HXN\n"); 454ea0efc37SHans Petter Selasky break; 45527e4bd81SHans Petter Selasky case TYPE_PL2303HXD: 45627e4bd81SHans Petter Selasky DPRINTF("chiptype: 2303HXD/TB/RA/EA\n"); 45727e4bd81SHans Petter Selasky break; 45827e4bd81SHans Petter Selasky default: 45927e4bd81SHans Petter Selasky DPRINTF("chiptype: unknown %d\n", sc->sc_chiptype); 46027e4bd81SHans Petter Selasky break; 46127e4bd81SHans Petter Selasky } 46202ac6454SAndrew Thompson 46302ac6454SAndrew Thompson /* 46402ac6454SAndrew Thompson * USB-RSAQ1 has two interface 46502ac6454SAndrew Thompson * 46602ac6454SAndrew Thompson * USB-RSAQ1 | USB-RSAQ2 46702ac6454SAndrew Thompson * -----------------+----------------- 46802ac6454SAndrew Thompson * Interface 0 |Interface 0 46902ac6454SAndrew Thompson * Interrupt(0x81) | Interrupt(0x81) 47002ac6454SAndrew Thompson * -----------------+ BulkIN(0x02) 47102ac6454SAndrew Thompson * Interface 1 | BulkOUT(0x83) 47202ac6454SAndrew Thompson * BulkIN(0x02) | 47302ac6454SAndrew Thompson * BulkOUT(0x83) | 47402ac6454SAndrew Thompson */ 47502ac6454SAndrew Thompson 47602ac6454SAndrew Thompson sc->sc_ctrl_iface_no = uaa->info.bIfaceNum; 47702ac6454SAndrew Thompson sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX; 47802ac6454SAndrew Thompson 479a593f6b8SAndrew Thompson iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX); 48002ac6454SAndrew Thompson if (iface) { 481a593f6b8SAndrew Thompson id = usbd_get_interface_descriptor(iface); 48202ac6454SAndrew Thompson if (id == NULL) { 483767cb2e2SAndrew Thompson device_printf(dev, "no interface descriptor (2)\n"); 48402ac6454SAndrew Thompson goto detach; 48502ac6454SAndrew Thompson } 48602ac6454SAndrew Thompson sc->sc_data_iface_no = id->bInterfaceNumber; 48702ac6454SAndrew Thompson sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX; 488a593f6b8SAndrew Thompson usbd_set_parent_iface(uaa->device, 48902ac6454SAndrew Thompson UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex); 49002ac6454SAndrew Thompson } else { 49102ac6454SAndrew Thompson sc->sc_data_iface_no = sc->sc_ctrl_iface_no; 49202ac6454SAndrew Thompson sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX; 49302ac6454SAndrew Thompson } 49402ac6454SAndrew Thompson 495a593f6b8SAndrew Thompson error = usbd_transfer_setup(uaa->device, 49602ac6454SAndrew Thompson sc->sc_iface_index, sc->sc_xfer, uplcom_config_data, 497deefe583SAndrew Thompson UPLCOM_N_TRANSFER, sc, &sc->sc_mtx); 49802ac6454SAndrew Thompson if (error) { 49902ac6454SAndrew Thompson DPRINTF("one or more missing USB endpoints, " 500a593f6b8SAndrew Thompson "error=%s\n", usbd_errstr(error)); 50102ac6454SAndrew Thompson goto detach; 50202ac6454SAndrew Thompson } 50302ac6454SAndrew Thompson error = uplcom_reset(sc, uaa->device); 50402ac6454SAndrew Thompson if (error) { 50502ac6454SAndrew Thompson device_printf(dev, "reset failed, error=%s\n", 506a593f6b8SAndrew Thompson usbd_errstr(error)); 50702ac6454SAndrew Thompson goto detach; 50802ac6454SAndrew Thompson } 5099efb7339SHans Petter Selasky 510*ec97e9caSHans Petter Selasky if (sc->sc_chiptype == TYPE_PL2303) { 511*ec97e9caSHans Petter Selasky /* HX variants seem to lock up after a clear stall request. */ 512deefe583SAndrew Thompson mtx_lock(&sc->sc_mtx); 513*ec97e9caSHans Petter Selasky usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 514*ec97e9caSHans Petter Selasky usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 515deefe583SAndrew Thompson mtx_unlock(&sc->sc_mtx); 516*ec97e9caSHans Petter Selasky } else if (sc->sc_chiptype == TYPE_PL2303HX || 517ea0efc37SHans Petter Selasky sc->sc_chiptype == TYPE_PL2303HXD) { 51827e4bd81SHans Petter Selasky /* reset upstream data pipes */ 5199efb7339SHans Petter Selasky if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE, 5209efb7339SHans Petter Selasky UPLCOM_SET_REQUEST, 8, 0, 0) || 5219efb7339SHans Petter Selasky uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE, 5229efb7339SHans Petter Selasky UPLCOM_SET_REQUEST, 9, 0, 0)) { 5239efb7339SHans Petter Selasky goto detach; 5249efb7339SHans Petter Selasky } 525ea0efc37SHans Petter Selasky } else if (sc->sc_chiptype == TYPE_PL2303HXN) { 526ea0efc37SHans Petter Selasky /* reset upstream data pipes */ 527ea0efc37SHans Petter Selasky if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE, 528ea0efc37SHans Petter Selasky UPLCOM_SET_REQUEST_PL2303HXN, 0x07, 0x03, 0)) { 529ea0efc37SHans Petter Selasky goto detach; 530ea0efc37SHans Petter Selasky } 5319efb7339SHans Petter Selasky } 53202ac6454SAndrew Thompson 533a593f6b8SAndrew Thompson error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 534deefe583SAndrew Thompson &uplcom_callback, &sc->sc_mtx); 53502ac6454SAndrew Thompson if (error) { 53602ac6454SAndrew Thompson goto detach; 53702ac6454SAndrew Thompson } 53802ac6454SAndrew Thompson /* 53902ac6454SAndrew Thompson * do the initialization during attach so that the system does not 54002ac6454SAndrew Thompson * sleep during open: 54102ac6454SAndrew Thompson */ 542bc65068dSGavin Atkinson if (uplcom_pl2303_init(uaa->device, sc->sc_chiptype)) { 543767cb2e2SAndrew Thompson device_printf(dev, "init failed\n"); 54402ac6454SAndrew Thompson goto detach; 54502ac6454SAndrew Thompson } 5466416c259SNick Hibma ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev); 5476416c259SNick Hibma 54802ac6454SAndrew Thompson return (0); 54902ac6454SAndrew Thompson 55002ac6454SAndrew Thompson detach: 55102ac6454SAndrew Thompson uplcom_detach(dev); 55202ac6454SAndrew Thompson return (ENXIO); 55302ac6454SAndrew Thompson } 55402ac6454SAndrew Thompson 55502ac6454SAndrew Thompson static int 55602ac6454SAndrew Thompson uplcom_detach(device_t dev) 55702ac6454SAndrew Thompson { 55802ac6454SAndrew Thompson struct uplcom_softc *sc = device_get_softc(dev); 55902ac6454SAndrew Thompson 56002ac6454SAndrew Thompson DPRINTF("sc=%p\n", sc); 56102ac6454SAndrew Thompson 562015bb88fSNick Hibma ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom); 563a593f6b8SAndrew Thompson usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER); 56402ac6454SAndrew Thompson 565c01fc06eSHans Petter Selasky device_claim_softc(dev); 566c01fc06eSHans Petter Selasky 567c01fc06eSHans Petter Selasky uplcom_free_softc(sc); 568c01fc06eSHans Petter Selasky 56902ac6454SAndrew Thompson return (0); 57002ac6454SAndrew Thompson } 57102ac6454SAndrew Thompson 5725805d178SHans Petter Selasky UCOM_UNLOAD_DRAIN(uplcom); 5735805d178SHans Petter Selasky 5745805d178SHans Petter Selasky static void 575c01fc06eSHans Petter Selasky uplcom_free_softc(struct uplcom_softc *sc) 5765805d178SHans Petter Selasky { 5775805d178SHans Petter Selasky if (ucom_unref(&sc->sc_super_ucom)) { 5785805d178SHans Petter Selasky mtx_destroy(&sc->sc_mtx); 579c01fc06eSHans Petter Selasky device_free_softc(sc); 5805805d178SHans Petter Selasky } 5815805d178SHans Petter Selasky } 5825805d178SHans Petter Selasky 5835805d178SHans Petter Selasky static void 5845805d178SHans Petter Selasky uplcom_free(struct ucom_softc *ucom) 5855805d178SHans Petter Selasky { 586c01fc06eSHans Petter Selasky uplcom_free_softc(ucom->sc_parent); 5875805d178SHans Petter Selasky } 5885805d178SHans Petter Selasky 589e0a69b51SAndrew Thompson static usb_error_t 590760bc48eSAndrew Thompson uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev) 59102ac6454SAndrew Thompson { 592760bc48eSAndrew Thompson struct usb_device_request req; 59302ac6454SAndrew Thompson 594ea0efc37SHans Petter Selasky if (sc->sc_chiptype == TYPE_PL2303HXN) { 595ea0efc37SHans Petter Selasky /* PL2303HXN doesn't need this reset sequence */ 596ea0efc37SHans Petter Selasky return (0); 597ea0efc37SHans Petter Selasky } 598ea0efc37SHans Petter Selasky 59902ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 60002ac6454SAndrew Thompson req.bRequest = UPLCOM_SET_REQUEST; 60102ac6454SAndrew Thompson USETW(req.wValue, 0); 60202ac6454SAndrew Thompson req.wIndex[0] = sc->sc_data_iface_no; 60302ac6454SAndrew Thompson req.wIndex[1] = 0; 60402ac6454SAndrew Thompson USETW(req.wLength, 0); 60502ac6454SAndrew Thompson 606a593f6b8SAndrew Thompson return (usbd_do_request(udev, NULL, &req, NULL)); 60702ac6454SAndrew Thompson } 60802ac6454SAndrew Thompson 609bc65068dSGavin Atkinson static usb_error_t 61002d4a225SDimitry Andric uplcom_pl2303_do(struct usb_device *udev, uint8_t req_type, uint8_t request, 611bc65068dSGavin Atkinson uint16_t value, uint16_t index, uint16_t length) 61202ac6454SAndrew Thompson { 613760bc48eSAndrew Thompson struct usb_device_request req; 614e0a69b51SAndrew Thompson usb_error_t err; 61502ac6454SAndrew Thompson uint8_t buf[4]; 61602ac6454SAndrew Thompson 617bc65068dSGavin Atkinson req.bmRequestType = req_type; 618bc65068dSGavin Atkinson req.bRequest = request; 619bc65068dSGavin Atkinson USETW(req.wValue, value); 620bc65068dSGavin Atkinson USETW(req.wIndex, index); 621bc65068dSGavin Atkinson USETW(req.wLength, length); 62202ac6454SAndrew Thompson 623a593f6b8SAndrew Thompson err = usbd_do_request(udev, NULL, &req, buf); 62402ac6454SAndrew Thompson if (err) { 625a593f6b8SAndrew Thompson DPRINTF("error=%s\n", usbd_errstr(err)); 626bc65068dSGavin Atkinson return (1); 627bc65068dSGavin Atkinson } 628bc65068dSGavin Atkinson return (0); 629bc65068dSGavin Atkinson } 630bc65068dSGavin Atkinson 631bc65068dSGavin Atkinson static int 632bc65068dSGavin Atkinson uplcom_pl2303_init(struct usb_device *udev, uint8_t chiptype) 633bc65068dSGavin Atkinson { 634bc65068dSGavin Atkinson int err; 635bc65068dSGavin Atkinson 636ea0efc37SHans Petter Selasky if (chiptype == TYPE_PL2303HXN) { 637ea0efc37SHans Petter Selasky /* PL2303HXN doesn't need this initialization sequence */ 638ea0efc37SHans Petter Selasky return (0); 639ea0efc37SHans Petter Selasky } 640ea0efc37SHans Petter Selasky 641bc65068dSGavin Atkinson if (uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1) 642bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0) 643bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1) 644bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1) 645bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1) 646bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0) 647bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1) 648bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1) 649bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0) 650bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0)) 65102ac6454SAndrew Thompson return (EIO); 652bc65068dSGavin Atkinson 65327e4bd81SHans Petter Selasky if (chiptype != TYPE_PL2303) 654bc65068dSGavin Atkinson err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0); 655bc65068dSGavin Atkinson else 656bc65068dSGavin Atkinson err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x24, 0); 657bc65068dSGavin Atkinson if (err) 658bc65068dSGavin Atkinson return (EIO); 659bc65068dSGavin Atkinson 66002ac6454SAndrew Thompson return (0); 66102ac6454SAndrew Thompson } 66202ac6454SAndrew Thompson 66302ac6454SAndrew Thompson static void 664760bc48eSAndrew Thompson uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 66502ac6454SAndrew Thompson { 66602ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent; 667760bc48eSAndrew Thompson struct usb_device_request req; 66802ac6454SAndrew Thompson 66902ac6454SAndrew Thompson DPRINTF("onoff = %d\n", onoff); 67002ac6454SAndrew Thompson 67102ac6454SAndrew Thompson if (onoff) 67202ac6454SAndrew Thompson sc->sc_line |= UCDC_LINE_DTR; 67302ac6454SAndrew Thompson else 67402ac6454SAndrew Thompson sc->sc_line &= ~UCDC_LINE_DTR; 67502ac6454SAndrew Thompson 67602ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 67702ac6454SAndrew Thompson req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 67802ac6454SAndrew Thompson USETW(req.wValue, sc->sc_line); 67902ac6454SAndrew Thompson req.wIndex[0] = sc->sc_data_iface_no; 68002ac6454SAndrew Thompson req.wIndex[1] = 0; 68102ac6454SAndrew Thompson USETW(req.wLength, 0); 68202ac6454SAndrew Thompson 683a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 68402ac6454SAndrew Thompson &req, NULL, 0, 1000); 68502ac6454SAndrew Thompson } 68602ac6454SAndrew Thompson 68702ac6454SAndrew Thompson static void 688760bc48eSAndrew Thompson uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff) 68902ac6454SAndrew Thompson { 69002ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent; 691760bc48eSAndrew Thompson struct usb_device_request req; 69202ac6454SAndrew Thompson 69302ac6454SAndrew Thompson DPRINTF("onoff = %d\n", onoff); 69402ac6454SAndrew Thompson 69502ac6454SAndrew Thompson if (onoff) 69602ac6454SAndrew Thompson sc->sc_line |= UCDC_LINE_RTS; 69702ac6454SAndrew Thompson else 69802ac6454SAndrew Thompson sc->sc_line &= ~UCDC_LINE_RTS; 69902ac6454SAndrew Thompson 70002ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 70102ac6454SAndrew Thompson req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 70202ac6454SAndrew Thompson USETW(req.wValue, sc->sc_line); 70302ac6454SAndrew Thompson req.wIndex[0] = sc->sc_data_iface_no; 70402ac6454SAndrew Thompson req.wIndex[1] = 0; 70502ac6454SAndrew Thompson USETW(req.wLength, 0); 70602ac6454SAndrew Thompson 707a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 70802ac6454SAndrew Thompson &req, NULL, 0, 1000); 70902ac6454SAndrew Thompson } 71002ac6454SAndrew Thompson 71102ac6454SAndrew Thompson static void 712760bc48eSAndrew Thompson uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff) 71302ac6454SAndrew Thompson { 71402ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent; 715760bc48eSAndrew Thompson struct usb_device_request req; 71602ac6454SAndrew Thompson uint16_t temp; 71702ac6454SAndrew Thompson 71802ac6454SAndrew Thompson DPRINTF("onoff = %d\n", onoff); 71902ac6454SAndrew Thompson 72002ac6454SAndrew Thompson temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF); 72102ac6454SAndrew Thompson 72202ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 72302ac6454SAndrew Thompson req.bRequest = UCDC_SEND_BREAK; 72402ac6454SAndrew Thompson USETW(req.wValue, temp); 72502ac6454SAndrew Thompson req.wIndex[0] = sc->sc_data_iface_no; 72602ac6454SAndrew Thompson req.wIndex[1] = 0; 72702ac6454SAndrew Thompson USETW(req.wLength, 0); 72802ac6454SAndrew Thompson 729a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 73002ac6454SAndrew Thompson &req, NULL, 0, 1000); 73102ac6454SAndrew Thompson } 73202ac6454SAndrew Thompson 73302ac6454SAndrew Thompson /* 73427e4bd81SHans Petter Selasky * NOTE: These baud rates are officially supported, they can be written 73527e4bd81SHans Petter Selasky * directly into dwDTERate register. 73627e4bd81SHans Petter Selasky * 73727e4bd81SHans Petter Selasky * Free baudrate setting is not supported by the base PL2303, and on 73827e4bd81SHans Petter Selasky * other models it requires writing a divisor value to dwDTERate instead 73927e4bd81SHans Petter Selasky * of the raw baudrate. The formula for divisor calculation is not published 74027e4bd81SHans Petter Selasky * by the vendor, so it is speculative, though the official product homepage 74127e4bd81SHans Petter Selasky * refers to the Linux module source as a reference implementation. 74202ac6454SAndrew Thompson */ 74327e4bd81SHans Petter Selasky static const uint32_t uplcom_rates[] = { 74427e4bd81SHans Petter Selasky /* 74527e4bd81SHans Petter Selasky * Basic 'standard' speed rates, supported by all models 74627e4bd81SHans Petter Selasky * NOTE: 900 and 56000 actually works as well 74727e4bd81SHans Petter Selasky */ 74827e4bd81SHans Petter Selasky 75, 150, 300, 600, 900, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400, 74927e4bd81SHans Petter Selasky 19200, 28800, 38400, 56000, 57600, 115200, 75027e4bd81SHans Petter Selasky /* 75127e4bd81SHans Petter Selasky * Advanced speed rates up to 6Mbs, supported by HX/TA and HXD/TB/EA/RA 75227e4bd81SHans Petter Selasky * NOTE: regardless of the spec, 256000 does not work 75327e4bd81SHans Petter Selasky */ 75427e4bd81SHans Petter Selasky 128000, 134400, 161280, 201600, 230400, 268800, 403200, 460800, 614400, 75527e4bd81SHans Petter Selasky 806400, 921600, 1228800, 2457600, 3000000, 6000000, 75627e4bd81SHans Petter Selasky /* 75727e4bd81SHans Petter Selasky * Advanced speed rates up to 12, supported by HXD/TB/EA/RA 75827e4bd81SHans Petter Selasky */ 75927e4bd81SHans Petter Selasky 12000000 76002ac6454SAndrew Thompson }; 76102ac6454SAndrew Thompson 762432157dcSPedro F. Giffuni #define N_UPLCOM_RATES nitems(uplcom_rates) 76302ac6454SAndrew Thompson 76402ac6454SAndrew Thompson static int 76527e4bd81SHans Petter Selasky uplcom_baud_supported(unsigned int speed) 76627e4bd81SHans Petter Selasky { 76727e4bd81SHans Petter Selasky int i; 76827e4bd81SHans Petter Selasky for (i = 0; i < N_UPLCOM_RATES; i++) { 76927e4bd81SHans Petter Selasky if (uplcom_rates[i] == speed) 77027e4bd81SHans Petter Selasky return 1; 77127e4bd81SHans Petter Selasky } 77227e4bd81SHans Petter Selasky return 0; 77327e4bd81SHans Petter Selasky } 77427e4bd81SHans Petter Selasky 77527e4bd81SHans Petter Selasky static int 776760bc48eSAndrew Thompson uplcom_pre_param(struct ucom_softc *ucom, struct termios *t) 77702ac6454SAndrew Thompson { 778fe0f6362SGavin Atkinson struct uplcom_softc *sc = ucom->sc_parent; 77902ac6454SAndrew Thompson 78002ac6454SAndrew Thompson DPRINTF("\n"); 78102ac6454SAndrew Thompson 782fe0f6362SGavin Atkinson /** 783fe0f6362SGavin Atkinson * Check requested baud rate. 784fe0f6362SGavin Atkinson * 785fe0f6362SGavin Atkinson * The PL2303 can only set specific baud rates, up to 1228800 baud. 78627e4bd81SHans Petter Selasky * The PL2303HX can set any baud rate up to 6Mb. 787ea0efc37SHans Petter Selasky * The PL2303HX rev. D and PL2303HXN can set any baud rate up to 12Mb. 788fe0f6362SGavin Atkinson * 789fe0f6362SGavin Atkinson */ 79027e4bd81SHans Petter Selasky 79127e4bd81SHans Petter Selasky /* accept raw divisor data, if someone wants to do the math in user domain */ 79227e4bd81SHans Petter Selasky if (t->c_ospeed & 0x80000000) 79327e4bd81SHans Petter Selasky return 0; 79427e4bd81SHans Petter Selasky switch (sc->sc_chiptype) { 795ea0efc37SHans Petter Selasky case TYPE_PL2303HXN: 796ea0efc37SHans Petter Selasky if (t->c_ospeed <= 12000000) 797ea0efc37SHans Petter Selasky return (0); 798ea0efc37SHans Petter Selasky break; 79927e4bd81SHans Petter Selasky case TYPE_PL2303HXD: 80027e4bd81SHans Petter Selasky if (t->c_ospeed <= 12000000) 801fe0f6362SGavin Atkinson return (0); 80227e4bd81SHans Petter Selasky break; 80327e4bd81SHans Petter Selasky case TYPE_PL2303HX: 804fe0f6362SGavin Atkinson if (t->c_ospeed <= 6000000) 805fe0f6362SGavin Atkinson return (0); 80627e4bd81SHans Petter Selasky break; 80727e4bd81SHans Petter Selasky default: 80827e4bd81SHans Petter Selasky if (uplcom_baud_supported(t->c_ospeed)) 80927e4bd81SHans Petter Selasky return (0); 81027e4bd81SHans Petter Selasky break; 81102ac6454SAndrew Thompson } 81202ac6454SAndrew Thompson 813fe0f6362SGavin Atkinson DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed); 814fe0f6362SGavin Atkinson return (EIO); 81502ac6454SAndrew Thompson } 81602ac6454SAndrew Thompson 81727e4bd81SHans Petter Selasky static unsigned int 81827e4bd81SHans Petter Selasky uplcom_encode_baud_rate_divisor(uint8_t *buf, unsigned int baud) 81927e4bd81SHans Petter Selasky { 82027e4bd81SHans Petter Selasky unsigned int baseline, mantissa, exponent; 82127e4bd81SHans Petter Selasky 82227e4bd81SHans Petter Selasky /* Determine the baud rate divisor. This algorithm is taken from Linux. */ 82327e4bd81SHans Petter Selasky /* 82427e4bd81SHans Petter Selasky * Apparently the formula is: 82527e4bd81SHans Petter Selasky * baudrate = baseline / (mantissa * 4^exponent) 82627e4bd81SHans Petter Selasky * where 82727e4bd81SHans Petter Selasky * mantissa = buf[8:0] 82827e4bd81SHans Petter Selasky * exponent = buf[11:9] 82927e4bd81SHans Petter Selasky */ 83027e4bd81SHans Petter Selasky if (baud == 0) 83127e4bd81SHans Petter Selasky baud = 1; 83227e4bd81SHans Petter Selasky baseline = 383385600; 83327e4bd81SHans Petter Selasky mantissa = baseline / baud; 83427e4bd81SHans Petter Selasky if (mantissa == 0) 83527e4bd81SHans Petter Selasky mantissa = 1; 83627e4bd81SHans Petter Selasky exponent = 0; 83727e4bd81SHans Petter Selasky while (mantissa >= 512) { 83827e4bd81SHans Petter Selasky if (exponent < 7) { 83927e4bd81SHans Petter Selasky mantissa >>= 2; /* divide by 4 */ 84027e4bd81SHans Petter Selasky exponent++; 84127e4bd81SHans Petter Selasky } else { 84227e4bd81SHans Petter Selasky /* Exponent is maxed. Trim mantissa and leave. This gives approx. 45.8 baud */ 84327e4bd81SHans Petter Selasky mantissa = 511; 84427e4bd81SHans Petter Selasky break; 84527e4bd81SHans Petter Selasky } 84627e4bd81SHans Petter Selasky } 84727e4bd81SHans Petter Selasky 84827e4bd81SHans Petter Selasky buf[3] = 0x80; 84927e4bd81SHans Petter Selasky buf[2] = 0; 85027e4bd81SHans Petter Selasky buf[1] = exponent << 1 | mantissa >> 8; 85127e4bd81SHans Petter Selasky buf[0] = mantissa & 0xff; 85227e4bd81SHans Petter Selasky 85327e4bd81SHans Petter Selasky /* Calculate and return the exact baud rate. */ 85427e4bd81SHans Petter Selasky baud = (baseline / mantissa) >> (exponent << 1); 85527e4bd81SHans Petter Selasky DPRINTF("real baud rate will be %u\n", baud); 85627e4bd81SHans Petter Selasky 85727e4bd81SHans Petter Selasky return baud; 85827e4bd81SHans Petter Selasky } 85902ac6454SAndrew Thompson static void 860760bc48eSAndrew Thompson uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t) 86102ac6454SAndrew Thompson { 86202ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent; 863760bc48eSAndrew Thompson struct usb_cdc_line_state ls; 864760bc48eSAndrew Thompson struct usb_device_request req; 86502ac6454SAndrew Thompson 86602ac6454SAndrew Thompson DPRINTF("sc = %p\n", sc); 86702ac6454SAndrew Thompson 868271ae033SHans Petter Selasky memset(&ls, 0, sizeof(ls)); 86902ac6454SAndrew Thompson 87027e4bd81SHans Petter Selasky /* 87127e4bd81SHans Petter Selasky * NOTE: If unsupported baud rates are set directly, the PL2303* uses 9600 baud. 87227e4bd81SHans Petter Selasky */ 87327e4bd81SHans Petter Selasky if ((t->c_ospeed & 0x80000000) || uplcom_baud_supported(t->c_ospeed)) 87402ac6454SAndrew Thompson USETDW(ls.dwDTERate, t->c_ospeed); 87527e4bd81SHans Petter Selasky else 87627e4bd81SHans Petter Selasky t->c_ospeed = uplcom_encode_baud_rate_divisor((uint8_t*)&ls.dwDTERate, t->c_ospeed); 87702ac6454SAndrew Thompson 87802ac6454SAndrew Thompson if (t->c_cflag & CSTOPB) { 87927e4bd81SHans Petter Selasky if ((t->c_cflag & CSIZE) == CS5) { 88027e4bd81SHans Petter Selasky /* 88127e4bd81SHans Petter Selasky * NOTE: Comply with "real" UARTs / RS232: 88227e4bd81SHans Petter Selasky * use 1.5 instead of 2 stop bits with 5 data bits 88327e4bd81SHans Petter Selasky */ 88427e4bd81SHans Petter Selasky ls.bCharFormat = UCDC_STOP_BIT_1_5; 88527e4bd81SHans Petter Selasky } else { 88602ac6454SAndrew Thompson ls.bCharFormat = UCDC_STOP_BIT_2; 88727e4bd81SHans Petter Selasky } 88802ac6454SAndrew Thompson } else { 88902ac6454SAndrew Thompson ls.bCharFormat = UCDC_STOP_BIT_1; 89002ac6454SAndrew Thompson } 89102ac6454SAndrew Thompson 89202ac6454SAndrew Thompson if (t->c_cflag & PARENB) { 89302ac6454SAndrew Thompson if (t->c_cflag & PARODD) { 89402ac6454SAndrew Thompson ls.bParityType = UCDC_PARITY_ODD; 89502ac6454SAndrew Thompson } else { 89602ac6454SAndrew Thompson ls.bParityType = UCDC_PARITY_EVEN; 89702ac6454SAndrew Thompson } 89802ac6454SAndrew Thompson } else { 89902ac6454SAndrew Thompson ls.bParityType = UCDC_PARITY_NONE; 90002ac6454SAndrew Thompson } 90102ac6454SAndrew Thompson 90202ac6454SAndrew Thompson switch (t->c_cflag & CSIZE) { 90302ac6454SAndrew Thompson case CS5: 90402ac6454SAndrew Thompson ls.bDataBits = 5; 90502ac6454SAndrew Thompson break; 90602ac6454SAndrew Thompson case CS6: 90702ac6454SAndrew Thompson ls.bDataBits = 6; 90802ac6454SAndrew Thompson break; 90902ac6454SAndrew Thompson case CS7: 91002ac6454SAndrew Thompson ls.bDataBits = 7; 91102ac6454SAndrew Thompson break; 91202ac6454SAndrew Thompson case CS8: 91302ac6454SAndrew Thompson ls.bDataBits = 8; 91402ac6454SAndrew Thompson break; 91502ac6454SAndrew Thompson } 91602ac6454SAndrew Thompson 91727e4bd81SHans Petter Selasky DPRINTF("rate=0x%08x fmt=%d parity=%d bits=%d\n", 91802ac6454SAndrew Thompson UGETDW(ls.dwDTERate), ls.bCharFormat, 91902ac6454SAndrew Thompson ls.bParityType, ls.bDataBits); 92002ac6454SAndrew Thompson 92102ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 92202ac6454SAndrew Thompson req.bRequest = UCDC_SET_LINE_CODING; 92302ac6454SAndrew Thompson USETW(req.wValue, 0); 92402ac6454SAndrew Thompson req.wIndex[0] = sc->sc_data_iface_no; 92502ac6454SAndrew Thompson req.wIndex[1] = 0; 92602ac6454SAndrew Thompson USETW(req.wLength, UCDC_LINE_STATE_LENGTH); 92702ac6454SAndrew Thompson 928a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 92902ac6454SAndrew Thompson &req, &ls, 0, 1000); 93002ac6454SAndrew Thompson 93102ac6454SAndrew Thompson if (t->c_cflag & CRTSCTS) { 93202ac6454SAndrew Thompson DPRINTF("crtscts = on\n"); 93302ac6454SAndrew Thompson 93402ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 935ea0efc37SHans Petter Selasky if (sc->sc_chiptype == TYPE_PL2303HXN) { 936ea0efc37SHans Petter Selasky req.bRequest = UPLCOM_SET_REQUEST_PL2303HXN; 937ea0efc37SHans Petter Selasky USETW(req.wValue, UPLCOM_CRTSCTS_REG_PL2303HXN); 938ea0efc37SHans Petter Selasky USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303HXN); 939ea0efc37SHans Petter Selasky } else { 94002ac6454SAndrew Thompson req.bRequest = UPLCOM_SET_REQUEST; 94102ac6454SAndrew Thompson USETW(req.wValue, 0); 94227e4bd81SHans Petter Selasky if (sc->sc_chiptype != TYPE_PL2303) 94302ac6454SAndrew Thompson USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X); 94402ac6454SAndrew Thompson else 94502ac6454SAndrew Thompson USETW(req.wIndex, UPLCOM_SET_CRTSCTS); 946ea0efc37SHans Petter Selasky } 94702ac6454SAndrew Thompson USETW(req.wLength, 0); 94802ac6454SAndrew Thompson 949a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 95002ac6454SAndrew Thompson &req, NULL, 0, 1000); 95102ac6454SAndrew Thompson } else { 95202ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 953ea0efc37SHans Petter Selasky if (sc->sc_chiptype == TYPE_PL2303HXN) { 954ea0efc37SHans Petter Selasky req.bRequest = UPLCOM_SET_REQUEST_PL2303HXN; 955ea0efc37SHans Petter Selasky USETW(req.wValue, UPLCOM_CRTSCTS_REG_PL2303HXN); 956ea0efc37SHans Petter Selasky USETW(req.wIndex, UPLCOM_CLEAR_CRTSCTS_PL2303HXN); 957ea0efc37SHans Petter Selasky } 958ea0efc37SHans Petter Selasky else { 95902ac6454SAndrew Thompson req.bRequest = UPLCOM_SET_REQUEST; 96002ac6454SAndrew Thompson USETW(req.wValue, 0); 96102ac6454SAndrew Thompson USETW(req.wIndex, 0); 962ea0efc37SHans Petter Selasky } 96302ac6454SAndrew Thompson USETW(req.wLength, 0); 964a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 96502ac6454SAndrew Thompson &req, NULL, 0, 1000); 96602ac6454SAndrew Thompson } 96702ac6454SAndrew Thompson } 96802ac6454SAndrew Thompson 96902ac6454SAndrew Thompson static void 970760bc48eSAndrew Thompson uplcom_start_read(struct ucom_softc *ucom) 97102ac6454SAndrew Thompson { 97202ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent; 97302ac6454SAndrew Thompson 97402ac6454SAndrew Thompson /* start interrupt endpoint */ 975a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]); 97602ac6454SAndrew Thompson 97702ac6454SAndrew Thompson /* start read endpoint */ 978a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 97902ac6454SAndrew Thompson } 98002ac6454SAndrew Thompson 98102ac6454SAndrew Thompson static void 982760bc48eSAndrew Thompson uplcom_stop_read(struct ucom_softc *ucom) 98302ac6454SAndrew Thompson { 98402ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent; 98502ac6454SAndrew Thompson 98602ac6454SAndrew Thompson /* stop interrupt endpoint */ 987a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]); 98802ac6454SAndrew Thompson 98902ac6454SAndrew Thompson /* stop read endpoint */ 990a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]); 99102ac6454SAndrew Thompson } 99202ac6454SAndrew Thompson 99302ac6454SAndrew Thompson static void 994760bc48eSAndrew Thompson uplcom_start_write(struct ucom_softc *ucom) 99502ac6454SAndrew Thompson { 99602ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent; 99702ac6454SAndrew Thompson 998a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 99902ac6454SAndrew Thompson } 100002ac6454SAndrew Thompson 100102ac6454SAndrew Thompson static void 1002760bc48eSAndrew Thompson uplcom_stop_write(struct ucom_softc *ucom) 100302ac6454SAndrew Thompson { 100402ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent; 100502ac6454SAndrew Thompson 1006a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]); 100702ac6454SAndrew Thompson } 100802ac6454SAndrew Thompson 100902ac6454SAndrew Thompson static void 1010760bc48eSAndrew Thompson uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 101102ac6454SAndrew Thompson { 101202ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent; 101302ac6454SAndrew Thompson 101402ac6454SAndrew Thompson DPRINTF("\n"); 101502ac6454SAndrew Thompson 101602ac6454SAndrew Thompson *lsr = sc->sc_lsr; 101702ac6454SAndrew Thompson *msr = sc->sc_msr; 101802ac6454SAndrew Thompson } 101902ac6454SAndrew Thompson 102002ac6454SAndrew Thompson static void 1021ed6d949aSAndrew Thompson uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error) 102202ac6454SAndrew Thompson { 1023ed6d949aSAndrew Thompson struct uplcom_softc *sc = usbd_xfer_softc(xfer); 1024ed6d949aSAndrew Thompson struct usb_page_cache *pc; 102502ac6454SAndrew Thompson uint8_t buf[9]; 1026ed6d949aSAndrew Thompson int actlen; 1027ed6d949aSAndrew Thompson 1028ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 102902ac6454SAndrew Thompson 103002ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 103102ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 103202ac6454SAndrew Thompson 1033ed6d949aSAndrew Thompson DPRINTF("actlen = %u\n", actlen); 103402ac6454SAndrew Thompson 1035ed6d949aSAndrew Thompson if (actlen >= 9) { 1036ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 1037ed6d949aSAndrew Thompson usbd_copy_out(pc, 0, buf, sizeof(buf)); 103802ac6454SAndrew Thompson 103927e4bd81SHans Petter Selasky DPRINTF("status = 0x%02x\n", buf[UPLCOM_STATE_INDEX]); 104002ac6454SAndrew Thompson 104102ac6454SAndrew Thompson sc->sc_lsr = 0; 104202ac6454SAndrew Thompson sc->sc_msr = 0; 104302ac6454SAndrew Thompson 104427e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_CTS) { 104502ac6454SAndrew Thompson sc->sc_msr |= SER_CTS; 104602ac6454SAndrew Thompson } 104727e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_OVERRUN_ERROR) { 104827e4bd81SHans Petter Selasky sc->sc_lsr |= ULSR_OE; 104927e4bd81SHans Petter Selasky } 105027e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_PARITY_ERROR) { 105127e4bd81SHans Petter Selasky sc->sc_lsr |= ULSR_PE; 105227e4bd81SHans Petter Selasky } 105327e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_FRAME_ERROR) { 105427e4bd81SHans Petter Selasky sc->sc_lsr |= ULSR_FE; 105527e4bd81SHans Petter Selasky } 105627e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_RING) { 105727e4bd81SHans Petter Selasky sc->sc_msr |= SER_RI; 105827e4bd81SHans Petter Selasky } 105927e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_BREAK_ERROR) { 106027e4bd81SHans Petter Selasky sc->sc_lsr |= ULSR_BI; 106127e4bd81SHans Petter Selasky } 106227e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DSR) { 106302ac6454SAndrew Thompson sc->sc_msr |= SER_DSR; 106402ac6454SAndrew Thompson } 106527e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DCD) { 106602ac6454SAndrew Thompson sc->sc_msr |= SER_DCD; 106702ac6454SAndrew Thompson } 1068a593f6b8SAndrew Thompson ucom_status_change(&sc->sc_ucom); 106902ac6454SAndrew Thompson } 107002ac6454SAndrew Thompson case USB_ST_SETUP: 107102ac6454SAndrew Thompson tr_setup: 1072ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1073a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 107402ac6454SAndrew Thompson return; 107502ac6454SAndrew Thompson 107602ac6454SAndrew Thompson default: /* Error */ 1077ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 107802ac6454SAndrew Thompson /* try to clear stall first */ 1079ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 108002ac6454SAndrew Thompson goto tr_setup; 108102ac6454SAndrew Thompson } 108202ac6454SAndrew Thompson return; 108302ac6454SAndrew Thompson } 108402ac6454SAndrew Thompson } 108502ac6454SAndrew Thompson 108602ac6454SAndrew Thompson static void 1087ed6d949aSAndrew Thompson uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error) 108802ac6454SAndrew Thompson { 1089ed6d949aSAndrew Thompson struct uplcom_softc *sc = usbd_xfer_softc(xfer); 1090ed6d949aSAndrew Thompson struct usb_page_cache *pc; 109102ac6454SAndrew Thompson uint32_t actlen; 109202ac6454SAndrew Thompson 109302ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 109402ac6454SAndrew Thompson case USB_ST_SETUP: 109502ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 109602ac6454SAndrew Thompson tr_setup: 1097ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 1098ed6d949aSAndrew Thompson if (ucom_get_data(&sc->sc_ucom, pc, 0, 109902ac6454SAndrew Thompson UPLCOM_BULK_BUF_SIZE, &actlen)) { 110002ac6454SAndrew Thompson DPRINTF("actlen = %d\n", actlen); 110102ac6454SAndrew Thompson 1102ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, actlen); 1103a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 110402ac6454SAndrew Thompson } 1105*ec97e9caSHans Petter Selasky return; 110602ac6454SAndrew Thompson 110702ac6454SAndrew Thompson default: /* Error */ 1108ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 110902ac6454SAndrew Thompson /* try to clear stall first */ 1110ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 111102ac6454SAndrew Thompson goto tr_setup; 111202ac6454SAndrew Thompson } 1113*ec97e9caSHans Petter Selasky return; 111402ac6454SAndrew Thompson } 111502ac6454SAndrew Thompson } 111602ac6454SAndrew Thompson 111702ac6454SAndrew Thompson static void 1118ed6d949aSAndrew Thompson uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error) 111902ac6454SAndrew Thompson { 1120ed6d949aSAndrew Thompson struct uplcom_softc *sc = usbd_xfer_softc(xfer); 1121ed6d949aSAndrew Thompson struct usb_page_cache *pc; 1122ed6d949aSAndrew Thompson int actlen; 1123ed6d949aSAndrew Thompson 1124ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 112502ac6454SAndrew Thompson 112602ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 112702ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 1128ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 1129ed6d949aSAndrew Thompson ucom_put_data(&sc->sc_ucom, pc, 0, actlen); 113002ac6454SAndrew Thompson 113102ac6454SAndrew Thompson case USB_ST_SETUP: 113202ac6454SAndrew Thompson tr_setup: 1133ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1134a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 113502ac6454SAndrew Thompson return; 113602ac6454SAndrew Thompson 113702ac6454SAndrew Thompson default: /* Error */ 1138ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 113902ac6454SAndrew Thompson /* try to clear stall first */ 1140ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 114102ac6454SAndrew Thompson goto tr_setup; 114202ac6454SAndrew Thompson } 114302ac6454SAndrew Thompson return; 114402ac6454SAndrew Thompson } 114502ac6454SAndrew Thompson } 1146655dc9d0SAndrew Thompson 1147655dc9d0SAndrew Thompson static void 1148655dc9d0SAndrew Thompson uplcom_poll(struct ucom_softc *ucom) 1149655dc9d0SAndrew Thompson { 1150655dc9d0SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent; 1151655dc9d0SAndrew Thompson usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER); 1152655dc9d0SAndrew Thompson } 1153