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 /*-
5*eebd9d53SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
6718cf2ccSPedro F. Giffuni *
702ac6454SAndrew Thompson * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
802ac6454SAndrew Thompson * All rights reserved.
902ac6454SAndrew Thompson *
1002ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without
1102ac6454SAndrew Thompson * modification, are permitted provided that the following conditions
1202ac6454SAndrew Thompson * are met:
1302ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright
1402ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer.
1502ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright
1602ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the
1702ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution.
1802ac6454SAndrew Thompson *
1902ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2002ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2102ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2202ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2302ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2402ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2502ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2602ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2702ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2802ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2902ac6454SAndrew Thompson * SUCH DAMAGE.
3002ac6454SAndrew Thompson */
3102ac6454SAndrew Thompson
3202ac6454SAndrew Thompson /*-
3302ac6454SAndrew Thompson * Copyright (c) 2001 The NetBSD Foundation, Inc.
3402ac6454SAndrew Thompson * All rights reserved.
3502ac6454SAndrew Thompson *
3602ac6454SAndrew Thompson * This code is derived from software contributed to The NetBSD Foundation
3702ac6454SAndrew Thompson * by Ichiro FUKUHARA (ichiro@ichiro.org).
3802ac6454SAndrew Thompson *
3902ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without
4002ac6454SAndrew Thompson * modification, are permitted provided that the following conditions
4102ac6454SAndrew Thompson * are met:
4202ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright
4302ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer.
4402ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright
4502ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the
4602ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution.
4702ac6454SAndrew Thompson *
4802ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
4902ac6454SAndrew Thompson * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
5002ac6454SAndrew Thompson * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5102ac6454SAndrew Thompson * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5202ac6454SAndrew Thompson * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5302ac6454SAndrew Thompson * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5402ac6454SAndrew Thompson * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5502ac6454SAndrew Thompson * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
5602ac6454SAndrew Thompson * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5702ac6454SAndrew Thompson * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
5802ac6454SAndrew Thompson * POSSIBILITY OF SUCH DAMAGE.
5902ac6454SAndrew Thompson */
6002ac6454SAndrew Thompson
6102ac6454SAndrew Thompson /*
6202ac6454SAndrew Thompson * This driver supports several USB-to-RS232 serial adapters driven by
6302ac6454SAndrew Thompson * Prolific PL-2303, PL-2303X and probably PL-2303HX USB-to-RS232
6402ac6454SAndrew Thompson * bridge chip. The adapters are sold under many different brand
6502ac6454SAndrew Thompson * names.
6602ac6454SAndrew Thompson *
6702ac6454SAndrew Thompson * Datasheets are available at Prolific www site at
6802ac6454SAndrew Thompson * http://www.prolific.com.tw. The datasheets don't contain full
6902ac6454SAndrew Thompson * programming information for the chip.
7002ac6454SAndrew Thompson *
7102ac6454SAndrew Thompson * PL-2303HX is probably programmed the same as PL-2303X.
7202ac6454SAndrew Thompson *
7302ac6454SAndrew Thompson * There are several differences between PL-2303 and PL-2303(H)X.
7402ac6454SAndrew Thompson * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_
7502ac6454SAndrew Thompson * different command for controlling CRTSCTS and needs special
7602ac6454SAndrew Thompson * sequence of commands for initialization which aren't also
7702ac6454SAndrew Thompson * documented in the datasheet.
7802ac6454SAndrew Thompson */
7902ac6454SAndrew Thompson
80ed6d949aSAndrew Thompson #include <sys/stdint.h>
81ed6d949aSAndrew Thompson #include <sys/stddef.h>
82ed6d949aSAndrew Thompson #include <sys/param.h>
83ed6d949aSAndrew Thompson #include <sys/queue.h>
84ed6d949aSAndrew Thompson #include <sys/types.h>
85ed6d949aSAndrew Thompson #include <sys/systm.h>
86ed6d949aSAndrew Thompson #include <sys/kernel.h>
87ed6d949aSAndrew Thompson #include <sys/bus.h>
88ed6d949aSAndrew Thompson #include <sys/module.h>
89ed6d949aSAndrew Thompson #include <sys/lock.h>
90ed6d949aSAndrew Thompson #include <sys/mutex.h>
91ed6d949aSAndrew Thompson #include <sys/condvar.h>
92ed6d949aSAndrew Thompson #include <sys/sysctl.h>
93ed6d949aSAndrew Thompson #include <sys/sx.h>
94ed6d949aSAndrew Thompson #include <sys/unistd.h>
95ed6d949aSAndrew Thompson #include <sys/callout.h>
96ed6d949aSAndrew Thompson #include <sys/malloc.h>
97ed6d949aSAndrew Thompson #include <sys/priv.h>
98ed6d949aSAndrew Thompson
9902ac6454SAndrew Thompson #include <dev/usb/usb.h>
100ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
101ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
10202ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
103ed6d949aSAndrew Thompson #include "usbdevs.h"
10402ac6454SAndrew Thompson
10502ac6454SAndrew Thompson #define USB_DEBUG_VAR uplcom_debug
10602ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
10702ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
10802ac6454SAndrew Thompson
10902ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
11002ac6454SAndrew Thompson
111b850ecc1SAndrew Thompson #ifdef USB_DEBUG
11202ac6454SAndrew Thompson static int uplcom_debug = 0;
11302ac6454SAndrew Thompson
114f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
115f8d2b1f3SPawel Biernacki "USB uplcom");
116ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RWTUN,
11702ac6454SAndrew Thompson &uplcom_debug, 0, "Debug level");
11802ac6454SAndrew Thompson #endif
11902ac6454SAndrew Thompson
12002ac6454SAndrew Thompson #define UPLCOM_MODVER 1 /* module version */
12102ac6454SAndrew Thompson
12202ac6454SAndrew Thompson #define UPLCOM_CONFIG_INDEX 0
12302ac6454SAndrew Thompson #define UPLCOM_IFACE_INDEX 0
12402ac6454SAndrew Thompson #define UPLCOM_SECOND_IFACE_INDEX 1
12502ac6454SAndrew Thompson
12602ac6454SAndrew Thompson #ifndef UPLCOM_INTR_INTERVAL
12702ac6454SAndrew Thompson #define UPLCOM_INTR_INTERVAL 0 /* default */
12802ac6454SAndrew Thompson #endif
12902ac6454SAndrew Thompson
13002ac6454SAndrew Thompson #define UPLCOM_BULK_BUF_SIZE 1024 /* bytes */
13102ac6454SAndrew Thompson
13202ac6454SAndrew Thompson #define UPLCOM_SET_REQUEST 0x01
133ea0efc37SHans Petter Selasky #define UPLCOM_SET_REQUEST_PL2303HXN 0x80
13402ac6454SAndrew Thompson #define UPLCOM_SET_CRTSCTS 0x41
13502ac6454SAndrew Thompson #define UPLCOM_SET_CRTSCTS_PL2303X 0x61
136ea0efc37SHans Petter Selasky #define UPLCOM_SET_CRTSCTS_PL2303HXN 0xFA
137ea0efc37SHans Petter Selasky #define UPLCOM_CLEAR_CRTSCTS_PL2303HXN 0xFF
138ea0efc37SHans Petter Selasky #define UPLCOM_CRTSCTS_REG_PL2303HXN 0x0A
139ea0efc37SHans Petter Selasky #define UPLCOM_STATUS_REG_PL2303HX 0x8080
14002ac6454SAndrew Thompson #define RSAQ_STATUS_CTS 0x80
14127e4bd81SHans Petter Selasky #define RSAQ_STATUS_OVERRUN_ERROR 0x40
14227e4bd81SHans Petter Selasky #define RSAQ_STATUS_PARITY_ERROR 0x20
14327e4bd81SHans Petter Selasky #define RSAQ_STATUS_FRAME_ERROR 0x10
14427e4bd81SHans Petter Selasky #define RSAQ_STATUS_RING 0x08
14527e4bd81SHans Petter Selasky #define RSAQ_STATUS_BREAK_ERROR 0x04
14602ac6454SAndrew Thompson #define RSAQ_STATUS_DSR 0x02
14702ac6454SAndrew Thompson #define RSAQ_STATUS_DCD 0x01
14802ac6454SAndrew Thompson
14902ac6454SAndrew Thompson #define TYPE_PL2303 0
150bc65068dSGavin Atkinson #define TYPE_PL2303HX 1
15127e4bd81SHans Petter Selasky #define TYPE_PL2303HXD 2
152ea0efc37SHans Petter Selasky #define TYPE_PL2303HXN 3
15327e4bd81SHans Petter Selasky
15427e4bd81SHans Petter Selasky #define UPLCOM_STATE_INDEX 8
15502ac6454SAndrew Thompson
15602ac6454SAndrew Thompson enum {
15702ac6454SAndrew Thompson UPLCOM_BULK_DT_WR,
15802ac6454SAndrew Thompson UPLCOM_BULK_DT_RD,
15902ac6454SAndrew Thompson UPLCOM_INTR_DT_RD,
16002ac6454SAndrew Thompson UPLCOM_N_TRANSFER,
16102ac6454SAndrew Thompson };
16202ac6454SAndrew Thompson
16302ac6454SAndrew Thompson struct uplcom_softc {
164760bc48eSAndrew Thompson struct ucom_super_softc sc_super_ucom;
165760bc48eSAndrew Thompson struct ucom_softc sc_ucom;
16602ac6454SAndrew Thompson
167760bc48eSAndrew Thompson struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER];
168760bc48eSAndrew Thompson struct usb_device *sc_udev;
169deefe583SAndrew Thompson struct mtx sc_mtx;
17002ac6454SAndrew Thompson
17102ac6454SAndrew Thompson uint16_t sc_line;
17202ac6454SAndrew Thompson
17302ac6454SAndrew Thompson uint8_t sc_lsr; /* local status register */
17402ac6454SAndrew Thompson uint8_t sc_msr; /* uplcom status register */
17502ac6454SAndrew Thompson uint8_t sc_chiptype; /* type of chip */
17602ac6454SAndrew Thompson uint8_t sc_ctrl_iface_no;
17702ac6454SAndrew Thompson uint8_t sc_data_iface_no;
17802ac6454SAndrew Thompson uint8_t sc_iface_index[2];
17902ac6454SAndrew Thompson };
18002ac6454SAndrew Thompson
18102ac6454SAndrew Thompson /* prototypes */
18202ac6454SAndrew Thompson
183e0a69b51SAndrew Thompson static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *);
18402d4a225SDimitry Andric static usb_error_t uplcom_pl2303_do(struct usb_device *, uint8_t, uint8_t,
185bc65068dSGavin Atkinson uint16_t, uint16_t, uint16_t);
186bc65068dSGavin Atkinson static int uplcom_pl2303_init(struct usb_device *, uint8_t);
1875805d178SHans Petter Selasky static void uplcom_free(struct ucom_softc *);
188760bc48eSAndrew Thompson static void uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
189760bc48eSAndrew Thompson static void uplcom_cfg_set_rts(struct ucom_softc *, uint8_t);
190760bc48eSAndrew Thompson static void uplcom_cfg_set_break(struct ucom_softc *, uint8_t);
191760bc48eSAndrew Thompson static int uplcom_pre_param(struct ucom_softc *, struct termios *);
192760bc48eSAndrew Thompson static void uplcom_cfg_param(struct ucom_softc *, struct termios *);
193760bc48eSAndrew Thompson static void uplcom_start_read(struct ucom_softc *);
194760bc48eSAndrew Thompson static void uplcom_stop_read(struct ucom_softc *);
195760bc48eSAndrew Thompson static void uplcom_start_write(struct ucom_softc *);
196760bc48eSAndrew Thompson static void uplcom_stop_write(struct ucom_softc *);
197760bc48eSAndrew Thompson static void uplcom_cfg_get_status(struct ucom_softc *, uint8_t *,
19802ac6454SAndrew Thompson uint8_t *);
199655dc9d0SAndrew Thompson static void uplcom_poll(struct ucom_softc *ucom);
20002ac6454SAndrew Thompson
20102ac6454SAndrew Thompson static device_probe_t uplcom_probe;
20202ac6454SAndrew Thompson static device_attach_t uplcom_attach;
20302ac6454SAndrew Thompson static device_detach_t uplcom_detach;
204c01fc06eSHans Petter Selasky static void uplcom_free_softc(struct uplcom_softc *);
20502ac6454SAndrew Thompson
206e0a69b51SAndrew Thompson static usb_callback_t uplcom_intr_callback;
207e0a69b51SAndrew Thompson static usb_callback_t uplcom_write_callback;
208e0a69b51SAndrew Thompson static usb_callback_t uplcom_read_callback;
20902ac6454SAndrew Thompson
210760bc48eSAndrew Thompson static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
21102ac6454SAndrew Thompson [UPLCOM_BULK_DT_WR] = {
21202ac6454SAndrew Thompson .type = UE_BULK,
21302ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY,
21402ac6454SAndrew Thompson .direction = UE_DIR_OUT,
2154eae601eSAndrew Thompson .bufsize = UPLCOM_BULK_BUF_SIZE,
2164eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2174eae601eSAndrew Thompson .callback = &uplcom_write_callback,
21802ac6454SAndrew Thompson .if_index = 0,
21902ac6454SAndrew Thompson },
22002ac6454SAndrew Thompson
22102ac6454SAndrew Thompson [UPLCOM_BULK_DT_RD] = {
22202ac6454SAndrew Thompson .type = UE_BULK,
22302ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY,
22402ac6454SAndrew Thompson .direction = UE_DIR_IN,
2254eae601eSAndrew Thompson .bufsize = UPLCOM_BULK_BUF_SIZE,
2264eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2274eae601eSAndrew Thompson .callback = &uplcom_read_callback,
22802ac6454SAndrew Thompson .if_index = 0,
22902ac6454SAndrew Thompson },
23002ac6454SAndrew Thompson
23102ac6454SAndrew Thompson [UPLCOM_INTR_DT_RD] = {
23202ac6454SAndrew Thompson .type = UE_INTERRUPT,
23302ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY,
23402ac6454SAndrew Thompson .direction = UE_DIR_IN,
2354eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2364eae601eSAndrew Thompson .bufsize = 0, /* use wMaxPacketSize */
2374eae601eSAndrew Thompson .callback = &uplcom_intr_callback,
23802ac6454SAndrew Thompson .if_index = 1,
23902ac6454SAndrew Thompson },
24002ac6454SAndrew Thompson };
24102ac6454SAndrew Thompson
242a8675caeSAndrew Thompson static struct ucom_callback uplcom_callback = {
243a593f6b8SAndrew Thompson .ucom_cfg_get_status = &uplcom_cfg_get_status,
244a593f6b8SAndrew Thompson .ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
245a593f6b8SAndrew Thompson .ucom_cfg_set_rts = &uplcom_cfg_set_rts,
246a593f6b8SAndrew Thompson .ucom_cfg_set_break = &uplcom_cfg_set_break,
247a593f6b8SAndrew Thompson .ucom_cfg_param = &uplcom_cfg_param,
248a593f6b8SAndrew Thompson .ucom_pre_param = &uplcom_pre_param,
249a593f6b8SAndrew Thompson .ucom_start_read = &uplcom_start_read,
250a593f6b8SAndrew Thompson .ucom_stop_read = &uplcom_stop_read,
251a593f6b8SAndrew Thompson .ucom_start_write = &uplcom_start_write,
252a593f6b8SAndrew Thompson .ucom_stop_write = &uplcom_stop_write,
253655dc9d0SAndrew Thompson .ucom_poll = &uplcom_poll,
2545805d178SHans Petter Selasky .ucom_free = &uplcom_free,
25502ac6454SAndrew Thompson };
25602ac6454SAndrew Thompson
257bc65068dSGavin Atkinson #define UPLCOM_DEV(v,p) \
258bc65068dSGavin Atkinson { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p) }
25902ac6454SAndrew Thompson
260f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID uplcom_devs[] = {
261f5113df9SGavin Atkinson UPLCOM_DEV(ACERP, S81), /* BenQ S81 phone */
262f5113df9SGavin Atkinson UPLCOM_DEV(ADLINK, ND6530), /* ADLINK ND-6530 USB-Serial */
263f5113df9SGavin Atkinson UPLCOM_DEV(ALCATEL, OT535), /* Alcatel One Touch 535/735 */
264f5113df9SGavin Atkinson UPLCOM_DEV(ALCOR, AU9720), /* Alcor AU9720 USB 2.0-RS232 */
265f5113df9SGavin Atkinson UPLCOM_DEV(ANCHOR, SERIAL), /* Anchor Serial adapter */
266bc65068dSGavin Atkinson UPLCOM_DEV(ATEN, UC232A), /* PLANEX USB-RS232 URS-03 */
26746a5f883SEd Maste UPLCOM_DEV(ATEN, UC232B), /* Prolific USB-RS232 Controller D */
26801a8f075SGavin Atkinson UPLCOM_DEV(BELKIN, F5U257), /* Belkin F5U257 USB to Serial */
269bc65068dSGavin Atkinson UPLCOM_DEV(COREGA, CGUSBRS232R), /* Corega CG-USBRS232R */
270f5113df9SGavin Atkinson UPLCOM_DEV(EPSON, CRESSI_EDY), /* Cressi Edy diving computer */
271c6ac426dSGavin Atkinson UPLCOM_DEV(EPSON, N2ITION3), /* Zeagle N2iTion3 diving computer */
27201a8f075SGavin Atkinson UPLCOM_DEV(ELECOM, UCSGT), /* ELECOM UC-SGT Serial Adapter */
27301a8f075SGavin Atkinson UPLCOM_DEV(ELECOM, UCSGT0), /* ELECOM UC-SGT Serial Adapter */
274bc65068dSGavin Atkinson UPLCOM_DEV(HAL, IMR001), /* HAL Corporation Crossam2+USB */
275f5113df9SGavin Atkinson UPLCOM_DEV(HP, LD220), /* HP LD220 POS Display */
276bc65068dSGavin Atkinson UPLCOM_DEV(IODATA, USBRSAQ), /* I/O DATA USB-RSAQ */
277bc65068dSGavin Atkinson UPLCOM_DEV(IODATA, USBRSAQ5), /* I/O DATA USB-RSAQ5 */
278f5113df9SGavin Atkinson UPLCOM_DEV(ITEGNO, WM1080A), /* iTegno WM1080A GSM/GFPRS modem */
279f5113df9SGavin Atkinson UPLCOM_DEV(ITEGNO, WM2080A), /* iTegno WM2080A CDMA modem */
280f5113df9SGavin Atkinson UPLCOM_DEV(LEADTEK, 9531), /* Leadtek 9531 GPS */
281f5113df9SGavin Atkinson UPLCOM_DEV(MICROSOFT, 700WX), /* Microsoft Palm 700WX */
282bc65068dSGavin Atkinson UPLCOM_DEV(MOBILEACTION, MA620), /* Mobile Action MA-620 Infrared Adapter */
283f5113df9SGavin Atkinson UPLCOM_DEV(NETINDEX, WS002IN), /* Willcom W-S002IN */
284f5113df9SGavin Atkinson UPLCOM_DEV(NOKIA2, CA42), /* Nokia CA-42 cable */
285f5113df9SGavin Atkinson UPLCOM_DEV(OTI, DKU5), /* OTI DKU-5 cable */
286f5113df9SGavin Atkinson UPLCOM_DEV(PANASONIC, TYTP50P6S), /* Panasonic TY-TP50P6-S flat screen */
287f5113df9SGavin Atkinson UPLCOM_DEV(PLX, CA42), /* PLX CA-42 clone cable */
288f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, ALLTRONIX_GPRS), /* Alltronix ACM003U00 modem */
289f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, ALDIGA_AL11U), /* AlDiga AL-11U modem */
290f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, DCU11), /* DCU-11 Phone Cable */
291f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, HCR331), /* HCR331 Card Reader */
292f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, MICROMAX_610U), /* Micromax 610U modem */
293d01755edSGavin Atkinson UPLCOM_DEV(PROLIFIC, MOTOROLA), /* Motorola cable */
294bc65068dSGavin Atkinson UPLCOM_DEV(PROLIFIC, PHAROS), /* Prolific Pharos */
295f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC, PL2303), /* Generic adapter */
296ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GC), /* Generic adapter (PL2303HXN, type GC) */
297ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GB), /* Generic adapter (PL2303HXN, type GB) */
298ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GT), /* Generic adapter (PL2303HXN, type GT) */
299ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GL), /* Generic adapter (PL2303HXN, type GL) */
300ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GE), /* Generic adapter (PL2303HXN, type GE) */
301ea0efc37SHans Petter Selasky UPLCOM_DEV(PROLIFIC, PL2303GS), /* Generic adapter (PL2303HXN, type GS) */
302bc65068dSGavin Atkinson UPLCOM_DEV(PROLIFIC, RSAQ2), /* I/O DATA USB-RSAQ2 */
303bc65068dSGavin Atkinson UPLCOM_DEV(PROLIFIC, RSAQ3), /* I/O DATA USB-RSAQ3 */
304635c9457SGavin Atkinson UPLCOM_DEV(PROLIFIC, UIC_MSR206), /* UIC MSR206 Card Reader */
305f5113df9SGavin Atkinson UPLCOM_DEV(PROLIFIC2, PL2303), /* Prolific adapter */
30601a8f075SGavin Atkinson UPLCOM_DEV(RADIOSHACK, USBCABLE), /* Radio Shack USB Adapter */
307bc65068dSGavin Atkinson UPLCOM_DEV(RATOC, REXUSB60), /* RATOC REX-USB60 */
308bc65068dSGavin Atkinson UPLCOM_DEV(SAGEM, USBSERIAL), /* Sagem USB-Serial Controller */
309f5113df9SGavin Atkinson UPLCOM_DEV(SAMSUNG, I330), /* Samsung I330 phone cradle */
310f5113df9SGavin Atkinson UPLCOM_DEV(SANWA, KB_USB2), /* Sanwa KB-USB2 Multimeter cable */
31101a8f075SGavin Atkinson UPLCOM_DEV(SIEMENS3, EF81), /* Siemens EF81 */
31201a8f075SGavin Atkinson UPLCOM_DEV(SIEMENS3, SX1), /* Siemens SX1 */
31301a8f075SGavin Atkinson UPLCOM_DEV(SIEMENS3, X65), /* Siemens X65 */
31401a8f075SGavin Atkinson UPLCOM_DEV(SIEMENS3, X75), /* Siemens X75 */
315bc65068dSGavin Atkinson UPLCOM_DEV(SITECOM, SERIAL), /* Sitecom USB to Serial */
31601a8f075SGavin Atkinson UPLCOM_DEV(SMART, PL2303), /* SMART Technologies USB to Serial */
317f5113df9SGavin Atkinson UPLCOM_DEV(SONY, QN3), /* Sony QN3 phone cable */
318f5113df9SGavin Atkinson UPLCOM_DEV(SONYERICSSON, DATAPILOT), /* Sony Ericsson Datapilot */
319f5113df9SGavin Atkinson UPLCOM_DEV(SONYERICSSON, DCU10), /* Sony Ericsson DCU-10 Cable */
320bc65068dSGavin Atkinson UPLCOM_DEV(SOURCENEXT, KEIKAI8), /* SOURCENEXT KeikaiDenwa 8 */
321bc65068dSGavin Atkinson UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG), /* SOURCENEXT KeikaiDenwa 8 with charger */
322f5113df9SGavin Atkinson UPLCOM_DEV(SPEEDDRAGON, MS3303H), /* Speed Dragon USB-Serial */
323f5113df9SGavin Atkinson UPLCOM_DEV(SYNTECH, CPT8001C), /* Syntech CPT-8001C Barcode scanner */
324bc65068dSGavin Atkinson UPLCOM_DEV(TDK, UHA6400), /* TDK USB-PHS Adapter UHA6400 */
325f5113df9SGavin Atkinson UPLCOM_DEV(TDK, UPA9664), /* TDK USB-PHS Adapter UPA9664 */
32601a8f075SGavin Atkinson UPLCOM_DEV(TRIPPLITE, U209), /* Tripp-Lite U209-000-R USB to Serial */
327f5113df9SGavin Atkinson UPLCOM_DEV(YCCABLE, PL2303), /* YC Cable USB-Serial */
32802ac6454SAndrew Thompson };
3299e6b5313SAndrew Thompson #undef UPLCOM_DEV
33002ac6454SAndrew Thompson
33102ac6454SAndrew Thompson static device_method_t uplcom_methods[] = {
33202ac6454SAndrew Thompson DEVMETHOD(device_probe, uplcom_probe),
33302ac6454SAndrew Thompson DEVMETHOD(device_attach, uplcom_attach),
33402ac6454SAndrew Thompson DEVMETHOD(device_detach, uplcom_detach),
3355805d178SHans Petter Selasky DEVMETHOD_END
33602ac6454SAndrew Thompson };
33702ac6454SAndrew Thompson
33802ac6454SAndrew Thompson static driver_t uplcom_driver = {
33902ac6454SAndrew Thompson .name = "uplcom",
34002ac6454SAndrew Thompson .methods = uplcom_methods,
34102ac6454SAndrew Thompson .size = sizeof(struct uplcom_softc),
34202ac6454SAndrew Thompson };
34302ac6454SAndrew Thompson
344bc9372d7SJohn Baldwin DRIVER_MODULE(uplcom, uhub, uplcom_driver, NULL, NULL);
34502ac6454SAndrew Thompson MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
34602ac6454SAndrew Thompson MODULE_DEPEND(uplcom, usb, 1, 1, 1);
34702ac6454SAndrew Thompson MODULE_VERSION(uplcom, UPLCOM_MODVER);
348f809f280SWarner Losh USB_PNP_HOST_INFO(uplcom_devs);
34902ac6454SAndrew Thompson
35002ac6454SAndrew Thompson static int
uplcom_probe(device_t dev)35102ac6454SAndrew Thompson uplcom_probe(device_t dev)
35202ac6454SAndrew Thompson {
353760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev);
35402ac6454SAndrew Thompson
35502ac6454SAndrew Thompson DPRINTFN(11, "\n");
35602ac6454SAndrew Thompson
357f29a0724SAndrew Thompson if (uaa->usb_mode != USB_MODE_HOST) {
35802ac6454SAndrew Thompson return (ENXIO);
35902ac6454SAndrew Thompson }
36002ac6454SAndrew Thompson if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
36102ac6454SAndrew Thompson return (ENXIO);
36202ac6454SAndrew Thompson }
36302ac6454SAndrew Thompson if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
36402ac6454SAndrew Thompson return (ENXIO);
36502ac6454SAndrew Thompson }
366a593f6b8SAndrew Thompson return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
36702ac6454SAndrew Thompson }
36802ac6454SAndrew Thompson
36902ac6454SAndrew Thompson static int
uplcom_attach(device_t dev)37002ac6454SAndrew Thompson uplcom_attach(device_t dev)
37102ac6454SAndrew Thompson {
372760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev);
37302ac6454SAndrew Thompson struct uplcom_softc *sc = device_get_softc(dev);
374760bc48eSAndrew Thompson struct usb_interface *iface;
375760bc48eSAndrew Thompson struct usb_interface_descriptor *id;
376bc65068dSGavin Atkinson struct usb_device_descriptor *dd;
37702ac6454SAndrew Thompson int error;
37802ac6454SAndrew Thompson
379ea0efc37SHans Petter Selasky struct usb_device_request req;
380ea0efc37SHans Petter Selasky usb_error_t err;
381ea0efc37SHans Petter Selasky uint8_t buf[4];
382ea0efc37SHans Petter Selasky
38302ac6454SAndrew Thompson DPRINTFN(11, "\n");
38402ac6454SAndrew Thompson
385a593f6b8SAndrew Thompson device_set_usb_desc(dev);
386deefe583SAndrew Thompson mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
3875805d178SHans Petter Selasky ucom_ref(&sc->sc_super_ucom);
38802ac6454SAndrew Thompson
38902ac6454SAndrew Thompson DPRINTF("sc = %p\n", sc);
39002ac6454SAndrew Thompson
39102ac6454SAndrew Thompson sc->sc_udev = uaa->device;
39202ac6454SAndrew Thompson
393bc65068dSGavin Atkinson dd = usbd_get_device_descriptor(sc->sc_udev);
39427e4bd81SHans Petter Selasky
39527e4bd81SHans Petter Selasky switch (UGETW(dd->bcdDevice)) {
39627e4bd81SHans Petter Selasky case 0x0300:
39727e4bd81SHans Petter Selasky sc->sc_chiptype = TYPE_PL2303HX;
39827e4bd81SHans Petter Selasky /* or TA, that is HX with external crystal */
39927e4bd81SHans Petter Selasky break;
40027e4bd81SHans Petter Selasky case 0x0400:
40127e4bd81SHans Petter Selasky sc->sc_chiptype = TYPE_PL2303HXD;
40227e4bd81SHans Petter Selasky /* or EA, that is HXD with ESD protection */
40327e4bd81SHans Petter Selasky /* or RA, that has internal voltage level converter that works only up to 1Mbaud (!) */
40427e4bd81SHans Petter Selasky break;
40527e4bd81SHans Petter Selasky case 0x0500:
40627e4bd81SHans Petter Selasky sc->sc_chiptype = TYPE_PL2303HXD;
40727e4bd81SHans Petter Selasky /* in fact it's TB, that is HXD with external crystal */
40827e4bd81SHans Petter Selasky break;
40927e4bd81SHans Petter Selasky default:
41027e4bd81SHans Petter Selasky /* NOTE: I have no info about the bcdDevice for the base PL2303 (up to 1.2Mbaud,
41127e4bd81SHans Petter Selasky only fixed rates) and for PL2303SA (8-pin chip, up to 115200 baud */
41227e4bd81SHans Petter Selasky /* Determine the chip type. This algorithm is taken from Linux. */
413bc65068dSGavin Atkinson if (dd->bDeviceClass == 0x02)
414bc65068dSGavin Atkinson sc->sc_chiptype = TYPE_PL2303;
415bc65068dSGavin Atkinson else if (dd->bMaxPacketSize == 0x40)
416bc65068dSGavin Atkinson sc->sc_chiptype = TYPE_PL2303HX;
417bc65068dSGavin Atkinson else
418bc65068dSGavin Atkinson sc->sc_chiptype = TYPE_PL2303;
41927e4bd81SHans Petter Selasky break;
42027e4bd81SHans Petter Selasky }
421bc65068dSGavin Atkinson
422ea0efc37SHans Petter Selasky /*
423ea0efc37SHans Petter Selasky * The new chip revision PL2303HXN is only compatible with the new
424ea0efc37SHans Petter Selasky * UPLCOM_SET_REQUEST_PL2303HXN command. Issuing the old command
425ea0efc37SHans Petter Selasky * UPLCOM_SET_REQUEST to the new chip raises an error. Thus, PL2303HX
426ea0efc37SHans Petter Selasky * and PL2303HXN can be distinguished by issuing an old-style request
427ea0efc37SHans Petter Selasky * (on a status register) to the new chip and checking the error.
428ea0efc37SHans Petter Selasky */
429ea0efc37SHans Petter Selasky if (sc->sc_chiptype == TYPE_PL2303HX) {
430ea0efc37SHans Petter Selasky req.bmRequestType = UT_READ_VENDOR_DEVICE;
431ea0efc37SHans Petter Selasky req.bRequest = UPLCOM_SET_REQUEST;
432ea0efc37SHans Petter Selasky USETW(req.wValue, UPLCOM_STATUS_REG_PL2303HX);
433ea0efc37SHans Petter Selasky req.wIndex[0] = sc->sc_data_iface_no;
434ea0efc37SHans Petter Selasky req.wIndex[1] = 0;
435ea0efc37SHans Petter Selasky USETW(req.wLength, 1);
436ea0efc37SHans Petter Selasky err = usbd_do_request(sc->sc_udev, NULL, &req, buf);
437ea0efc37SHans Petter Selasky if (err)
438ea0efc37SHans Petter Selasky sc->sc_chiptype = TYPE_PL2303HXN;
439ea0efc37SHans Petter Selasky }
440ea0efc37SHans Petter Selasky
44127e4bd81SHans Petter Selasky switch (sc->sc_chiptype) {
44227e4bd81SHans Petter Selasky case TYPE_PL2303:
44327e4bd81SHans Petter Selasky DPRINTF("chiptype: 2303\n");
44427e4bd81SHans Petter Selasky break;
44527e4bd81SHans Petter Selasky case TYPE_PL2303HX:
44627e4bd81SHans Petter Selasky DPRINTF("chiptype: 2303HX/TA\n");
44727e4bd81SHans Petter Selasky break;
448ea0efc37SHans Petter Selasky case TYPE_PL2303HXN:
449ea0efc37SHans Petter Selasky DPRINTF("chiptype: 2303HXN\n");
450ea0efc37SHans Petter Selasky break;
45127e4bd81SHans Petter Selasky case TYPE_PL2303HXD:
45227e4bd81SHans Petter Selasky DPRINTF("chiptype: 2303HXD/TB/RA/EA\n");
45327e4bd81SHans Petter Selasky break;
45427e4bd81SHans Petter Selasky default:
45527e4bd81SHans Petter Selasky DPRINTF("chiptype: unknown %d\n", sc->sc_chiptype);
45627e4bd81SHans Petter Selasky break;
45727e4bd81SHans Petter Selasky }
45802ac6454SAndrew Thompson
45902ac6454SAndrew Thompson /*
46002ac6454SAndrew Thompson * USB-RSAQ1 has two interface
46102ac6454SAndrew Thompson *
46202ac6454SAndrew Thompson * USB-RSAQ1 | USB-RSAQ2
46302ac6454SAndrew Thompson * -----------------+-----------------
46402ac6454SAndrew Thompson * Interface 0 |Interface 0
46502ac6454SAndrew Thompson * Interrupt(0x81) | Interrupt(0x81)
46602ac6454SAndrew Thompson * -----------------+ BulkIN(0x02)
46702ac6454SAndrew Thompson * Interface 1 | BulkOUT(0x83)
46802ac6454SAndrew Thompson * BulkIN(0x02) |
46902ac6454SAndrew Thompson * BulkOUT(0x83) |
47002ac6454SAndrew Thompson */
47102ac6454SAndrew Thompson
47202ac6454SAndrew Thompson sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
47302ac6454SAndrew Thompson sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
47402ac6454SAndrew Thompson
475a593f6b8SAndrew Thompson iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
47602ac6454SAndrew Thompson if (iface) {
477a593f6b8SAndrew Thompson id = usbd_get_interface_descriptor(iface);
47802ac6454SAndrew Thompson if (id == NULL) {
479767cb2e2SAndrew Thompson device_printf(dev, "no interface descriptor (2)\n");
48002ac6454SAndrew Thompson goto detach;
48102ac6454SAndrew Thompson }
48202ac6454SAndrew Thompson sc->sc_data_iface_no = id->bInterfaceNumber;
48302ac6454SAndrew Thompson sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
484a593f6b8SAndrew Thompson usbd_set_parent_iface(uaa->device,
48502ac6454SAndrew Thompson UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
48602ac6454SAndrew Thompson } else {
48702ac6454SAndrew Thompson sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
48802ac6454SAndrew Thompson sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
48902ac6454SAndrew Thompson }
49002ac6454SAndrew Thompson
491a593f6b8SAndrew Thompson error = usbd_transfer_setup(uaa->device,
49202ac6454SAndrew Thompson sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
493deefe583SAndrew Thompson UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
49402ac6454SAndrew Thompson if (error) {
49502ac6454SAndrew Thompson DPRINTF("one or more missing USB endpoints, "
496a593f6b8SAndrew Thompson "error=%s\n", usbd_errstr(error));
49702ac6454SAndrew Thompson goto detach;
49802ac6454SAndrew Thompson }
49902ac6454SAndrew Thompson error = uplcom_reset(sc, uaa->device);
50002ac6454SAndrew Thompson if (error) {
50102ac6454SAndrew Thompson device_printf(dev, "reset failed, error=%s\n",
502a593f6b8SAndrew Thompson usbd_errstr(error));
50302ac6454SAndrew Thompson goto detach;
50402ac6454SAndrew Thompson }
5059efb7339SHans Petter Selasky
506ec97e9caSHans Petter Selasky if (sc->sc_chiptype == TYPE_PL2303) {
507ec97e9caSHans Petter Selasky /* HX variants seem to lock up after a clear stall request. */
508deefe583SAndrew Thompson mtx_lock(&sc->sc_mtx);
509ec97e9caSHans Petter Selasky usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
510ec97e9caSHans Petter Selasky usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
511deefe583SAndrew Thompson mtx_unlock(&sc->sc_mtx);
512ec97e9caSHans Petter Selasky } else if (sc->sc_chiptype == TYPE_PL2303HX ||
513ea0efc37SHans Petter Selasky sc->sc_chiptype == TYPE_PL2303HXD) {
51427e4bd81SHans Petter Selasky /* reset upstream data pipes */
5159efb7339SHans Petter Selasky if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
5169efb7339SHans Petter Selasky UPLCOM_SET_REQUEST, 8, 0, 0) ||
5179efb7339SHans Petter Selasky uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
5189efb7339SHans Petter Selasky UPLCOM_SET_REQUEST, 9, 0, 0)) {
5199efb7339SHans Petter Selasky goto detach;
5209efb7339SHans Petter Selasky }
521ea0efc37SHans Petter Selasky } else if (sc->sc_chiptype == TYPE_PL2303HXN) {
522ea0efc37SHans Petter Selasky /* reset upstream data pipes */
523ea0efc37SHans Petter Selasky if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
524ea0efc37SHans Petter Selasky UPLCOM_SET_REQUEST_PL2303HXN, 0x07, 0x03, 0)) {
525ea0efc37SHans Petter Selasky goto detach;
526ea0efc37SHans Petter Selasky }
5279efb7339SHans Petter Selasky }
52802ac6454SAndrew Thompson
529a593f6b8SAndrew Thompson error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
530deefe583SAndrew Thompson &uplcom_callback, &sc->sc_mtx);
53102ac6454SAndrew Thompson if (error) {
53202ac6454SAndrew Thompson goto detach;
53302ac6454SAndrew Thompson }
53402ac6454SAndrew Thompson /*
53502ac6454SAndrew Thompson * do the initialization during attach so that the system does not
53602ac6454SAndrew Thompson * sleep during open:
53702ac6454SAndrew Thompson */
538bc65068dSGavin Atkinson if (uplcom_pl2303_init(uaa->device, sc->sc_chiptype)) {
539767cb2e2SAndrew Thompson device_printf(dev, "init failed\n");
54002ac6454SAndrew Thompson goto detach;
54102ac6454SAndrew Thompson }
5426416c259SNick Hibma ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
5436416c259SNick Hibma
54402ac6454SAndrew Thompson return (0);
54502ac6454SAndrew Thompson
54602ac6454SAndrew Thompson detach:
54702ac6454SAndrew Thompson uplcom_detach(dev);
54802ac6454SAndrew Thompson return (ENXIO);
54902ac6454SAndrew Thompson }
55002ac6454SAndrew Thompson
55102ac6454SAndrew Thompson static int
uplcom_detach(device_t dev)55202ac6454SAndrew Thompson uplcom_detach(device_t dev)
55302ac6454SAndrew Thompson {
55402ac6454SAndrew Thompson struct uplcom_softc *sc = device_get_softc(dev);
55502ac6454SAndrew Thompson
55602ac6454SAndrew Thompson DPRINTF("sc=%p\n", sc);
55702ac6454SAndrew Thompson
558015bb88fSNick Hibma ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
559a593f6b8SAndrew Thompson usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
56002ac6454SAndrew Thompson
561c01fc06eSHans Petter Selasky device_claim_softc(dev);
562c01fc06eSHans Petter Selasky
563c01fc06eSHans Petter Selasky uplcom_free_softc(sc);
564c01fc06eSHans Petter Selasky
56502ac6454SAndrew Thompson return (0);
56602ac6454SAndrew Thompson }
56702ac6454SAndrew Thompson
5685805d178SHans Petter Selasky UCOM_UNLOAD_DRAIN(uplcom);
5695805d178SHans Petter Selasky
5705805d178SHans Petter Selasky static void
uplcom_free_softc(struct uplcom_softc * sc)571c01fc06eSHans Petter Selasky uplcom_free_softc(struct uplcom_softc *sc)
5725805d178SHans Petter Selasky {
5735805d178SHans Petter Selasky if (ucom_unref(&sc->sc_super_ucom)) {
5745805d178SHans Petter Selasky mtx_destroy(&sc->sc_mtx);
575c01fc06eSHans Petter Selasky device_free_softc(sc);
5765805d178SHans Petter Selasky }
5775805d178SHans Petter Selasky }
5785805d178SHans Petter Selasky
5795805d178SHans Petter Selasky static void
uplcom_free(struct ucom_softc * ucom)5805805d178SHans Petter Selasky uplcom_free(struct ucom_softc *ucom)
5815805d178SHans Petter Selasky {
582c01fc06eSHans Petter Selasky uplcom_free_softc(ucom->sc_parent);
5835805d178SHans Petter Selasky }
5845805d178SHans Petter Selasky
585e0a69b51SAndrew Thompson static usb_error_t
uplcom_reset(struct uplcom_softc * sc,struct usb_device * udev)586760bc48eSAndrew Thompson uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
58702ac6454SAndrew Thompson {
588760bc48eSAndrew Thompson struct usb_device_request req;
58902ac6454SAndrew Thompson
590ea0efc37SHans Petter Selasky if (sc->sc_chiptype == TYPE_PL2303HXN) {
591ea0efc37SHans Petter Selasky /* PL2303HXN doesn't need this reset sequence */
592ea0efc37SHans Petter Selasky return (0);
593ea0efc37SHans Petter Selasky }
594ea0efc37SHans Petter Selasky
59502ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
59602ac6454SAndrew Thompson req.bRequest = UPLCOM_SET_REQUEST;
59702ac6454SAndrew Thompson USETW(req.wValue, 0);
59802ac6454SAndrew Thompson req.wIndex[0] = sc->sc_data_iface_no;
59902ac6454SAndrew Thompson req.wIndex[1] = 0;
60002ac6454SAndrew Thompson USETW(req.wLength, 0);
60102ac6454SAndrew Thompson
602a593f6b8SAndrew Thompson return (usbd_do_request(udev, NULL, &req, NULL));
60302ac6454SAndrew Thompson }
60402ac6454SAndrew Thompson
605bc65068dSGavin Atkinson static usb_error_t
uplcom_pl2303_do(struct usb_device * udev,uint8_t req_type,uint8_t request,uint16_t value,uint16_t index,uint16_t length)60602d4a225SDimitry Andric uplcom_pl2303_do(struct usb_device *udev, uint8_t req_type, uint8_t request,
607bc65068dSGavin Atkinson uint16_t value, uint16_t index, uint16_t length)
60802ac6454SAndrew Thompson {
609760bc48eSAndrew Thompson struct usb_device_request req;
610e0a69b51SAndrew Thompson usb_error_t err;
61102ac6454SAndrew Thompson uint8_t buf[4];
61202ac6454SAndrew Thompson
613bc65068dSGavin Atkinson req.bmRequestType = req_type;
614bc65068dSGavin Atkinson req.bRequest = request;
615bc65068dSGavin Atkinson USETW(req.wValue, value);
616bc65068dSGavin Atkinson USETW(req.wIndex, index);
617bc65068dSGavin Atkinson USETW(req.wLength, length);
61802ac6454SAndrew Thompson
619a593f6b8SAndrew Thompson err = usbd_do_request(udev, NULL, &req, buf);
62002ac6454SAndrew Thompson if (err) {
621a593f6b8SAndrew Thompson DPRINTF("error=%s\n", usbd_errstr(err));
622bc65068dSGavin Atkinson return (1);
623bc65068dSGavin Atkinson }
624bc65068dSGavin Atkinson return (0);
625bc65068dSGavin Atkinson }
626bc65068dSGavin Atkinson
627bc65068dSGavin Atkinson static int
uplcom_pl2303_init(struct usb_device * udev,uint8_t chiptype)628bc65068dSGavin Atkinson uplcom_pl2303_init(struct usb_device *udev, uint8_t chiptype)
629bc65068dSGavin Atkinson {
630bc65068dSGavin Atkinson int err;
631bc65068dSGavin Atkinson
632ea0efc37SHans Petter Selasky if (chiptype == TYPE_PL2303HXN) {
633ea0efc37SHans Petter Selasky /* PL2303HXN doesn't need this initialization sequence */
634ea0efc37SHans Petter Selasky return (0);
635ea0efc37SHans Petter Selasky }
636ea0efc37SHans Petter Selasky
637bc65068dSGavin Atkinson if (uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
638bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0)
639bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
640bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
641bc65068dSGavin Atkinson || 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, 1, 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_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0)
646bc65068dSGavin Atkinson || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0))
64702ac6454SAndrew Thompson return (EIO);
648bc65068dSGavin Atkinson
64927e4bd81SHans Petter Selasky if (chiptype != TYPE_PL2303)
650bc65068dSGavin Atkinson err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0);
651bc65068dSGavin Atkinson else
652bc65068dSGavin Atkinson err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x24, 0);
653bc65068dSGavin Atkinson if (err)
654bc65068dSGavin Atkinson return (EIO);
655bc65068dSGavin Atkinson
65602ac6454SAndrew Thompson return (0);
65702ac6454SAndrew Thompson }
65802ac6454SAndrew Thompson
65902ac6454SAndrew Thompson static void
uplcom_cfg_set_dtr(struct ucom_softc * ucom,uint8_t onoff)660760bc48eSAndrew Thompson uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
66102ac6454SAndrew Thompson {
66202ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
663760bc48eSAndrew Thompson struct usb_device_request req;
66402ac6454SAndrew Thompson
66502ac6454SAndrew Thompson DPRINTF("onoff = %d\n", onoff);
66602ac6454SAndrew Thompson
66702ac6454SAndrew Thompson if (onoff)
66802ac6454SAndrew Thompson sc->sc_line |= UCDC_LINE_DTR;
66902ac6454SAndrew Thompson else
67002ac6454SAndrew Thompson sc->sc_line &= ~UCDC_LINE_DTR;
67102ac6454SAndrew Thompson
67202ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
67302ac6454SAndrew Thompson req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
67402ac6454SAndrew Thompson USETW(req.wValue, sc->sc_line);
67502ac6454SAndrew Thompson req.wIndex[0] = sc->sc_data_iface_no;
67602ac6454SAndrew Thompson req.wIndex[1] = 0;
67702ac6454SAndrew Thompson USETW(req.wLength, 0);
67802ac6454SAndrew Thompson
679a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
68002ac6454SAndrew Thompson &req, NULL, 0, 1000);
68102ac6454SAndrew Thompson }
68202ac6454SAndrew Thompson
68302ac6454SAndrew Thompson static void
uplcom_cfg_set_rts(struct ucom_softc * ucom,uint8_t onoff)684760bc48eSAndrew Thompson uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
68502ac6454SAndrew Thompson {
68602ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
687760bc48eSAndrew Thompson struct usb_device_request req;
68802ac6454SAndrew Thompson
68902ac6454SAndrew Thompson DPRINTF("onoff = %d\n", onoff);
69002ac6454SAndrew Thompson
69102ac6454SAndrew Thompson if (onoff)
69202ac6454SAndrew Thompson sc->sc_line |= UCDC_LINE_RTS;
69302ac6454SAndrew Thompson else
69402ac6454SAndrew Thompson sc->sc_line &= ~UCDC_LINE_RTS;
69502ac6454SAndrew Thompson
69602ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
69702ac6454SAndrew Thompson req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
69802ac6454SAndrew Thompson USETW(req.wValue, sc->sc_line);
69902ac6454SAndrew Thompson req.wIndex[0] = sc->sc_data_iface_no;
70002ac6454SAndrew Thompson req.wIndex[1] = 0;
70102ac6454SAndrew Thompson USETW(req.wLength, 0);
70202ac6454SAndrew Thompson
703a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
70402ac6454SAndrew Thompson &req, NULL, 0, 1000);
70502ac6454SAndrew Thompson }
70602ac6454SAndrew Thompson
70702ac6454SAndrew Thompson static void
uplcom_cfg_set_break(struct ucom_softc * ucom,uint8_t onoff)708760bc48eSAndrew Thompson uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
70902ac6454SAndrew Thompson {
71002ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
711760bc48eSAndrew Thompson struct usb_device_request req;
71202ac6454SAndrew Thompson uint16_t temp;
71302ac6454SAndrew Thompson
71402ac6454SAndrew Thompson DPRINTF("onoff = %d\n", onoff);
71502ac6454SAndrew Thompson
71602ac6454SAndrew Thompson temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
71702ac6454SAndrew Thompson
71802ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
71902ac6454SAndrew Thompson req.bRequest = UCDC_SEND_BREAK;
72002ac6454SAndrew Thompson USETW(req.wValue, temp);
72102ac6454SAndrew Thompson req.wIndex[0] = sc->sc_data_iface_no;
72202ac6454SAndrew Thompson req.wIndex[1] = 0;
72302ac6454SAndrew Thompson USETW(req.wLength, 0);
72402ac6454SAndrew Thompson
725a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
72602ac6454SAndrew Thompson &req, NULL, 0, 1000);
72702ac6454SAndrew Thompson }
72802ac6454SAndrew Thompson
72902ac6454SAndrew Thompson /*
73027e4bd81SHans Petter Selasky * NOTE: These baud rates are officially supported, they can be written
73127e4bd81SHans Petter Selasky * directly into dwDTERate register.
73227e4bd81SHans Petter Selasky *
73327e4bd81SHans Petter Selasky * Free baudrate setting is not supported by the base PL2303, and on
73427e4bd81SHans Petter Selasky * other models it requires writing a divisor value to dwDTERate instead
73527e4bd81SHans Petter Selasky * of the raw baudrate. The formula for divisor calculation is not published
73627e4bd81SHans Petter Selasky * by the vendor, so it is speculative, though the official product homepage
73727e4bd81SHans Petter Selasky * refers to the Linux module source as a reference implementation.
73802ac6454SAndrew Thompson */
73927e4bd81SHans Petter Selasky static const uint32_t uplcom_rates[] = {
74027e4bd81SHans Petter Selasky /*
74127e4bd81SHans Petter Selasky * Basic 'standard' speed rates, supported by all models
74227e4bd81SHans Petter Selasky * NOTE: 900 and 56000 actually works as well
74327e4bd81SHans Petter Selasky */
74427e4bd81SHans Petter Selasky 75, 150, 300, 600, 900, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
74527e4bd81SHans Petter Selasky 19200, 28800, 38400, 56000, 57600, 115200,
74627e4bd81SHans Petter Selasky /*
74727e4bd81SHans Petter Selasky * Advanced speed rates up to 6Mbs, supported by HX/TA and HXD/TB/EA/RA
74827e4bd81SHans Petter Selasky * NOTE: regardless of the spec, 256000 does not work
74927e4bd81SHans Petter Selasky */
75027e4bd81SHans Petter Selasky 128000, 134400, 161280, 201600, 230400, 268800, 403200, 460800, 614400,
75127e4bd81SHans Petter Selasky 806400, 921600, 1228800, 2457600, 3000000, 6000000,
75227e4bd81SHans Petter Selasky /*
75327e4bd81SHans Petter Selasky * Advanced speed rates up to 12, supported by HXD/TB/EA/RA
75427e4bd81SHans Petter Selasky */
75527e4bd81SHans Petter Selasky 12000000
75602ac6454SAndrew Thompson };
75702ac6454SAndrew Thompson
758432157dcSPedro F. Giffuni #define N_UPLCOM_RATES nitems(uplcom_rates)
75902ac6454SAndrew Thompson
76002ac6454SAndrew Thompson static int
uplcom_baud_supported(unsigned speed)76162d42655SHans Petter Selasky uplcom_baud_supported(unsigned speed)
76227e4bd81SHans Petter Selasky {
76327e4bd81SHans Petter Selasky int i;
76427e4bd81SHans Petter Selasky for (i = 0; i < N_UPLCOM_RATES; i++) {
76527e4bd81SHans Petter Selasky if (uplcom_rates[i] == speed)
76627e4bd81SHans Petter Selasky return 1;
76727e4bd81SHans Petter Selasky }
76827e4bd81SHans Petter Selasky return 0;
76927e4bd81SHans Petter Selasky }
77027e4bd81SHans Petter Selasky
77127e4bd81SHans Petter Selasky static int
uplcom_pre_param(struct ucom_softc * ucom,struct termios * t)772760bc48eSAndrew Thompson uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
77302ac6454SAndrew Thompson {
774fe0f6362SGavin Atkinson struct uplcom_softc *sc = ucom->sc_parent;
77502ac6454SAndrew Thompson
77602ac6454SAndrew Thompson DPRINTF("\n");
77702ac6454SAndrew Thompson
778fe0f6362SGavin Atkinson /**
779fe0f6362SGavin Atkinson * Check requested baud rate.
780fe0f6362SGavin Atkinson *
781fe0f6362SGavin Atkinson * The PL2303 can only set specific baud rates, up to 1228800 baud.
78227e4bd81SHans Petter Selasky * The PL2303HX can set any baud rate up to 6Mb.
783ea0efc37SHans Petter Selasky * The PL2303HX rev. D and PL2303HXN can set any baud rate up to 12Mb.
784fe0f6362SGavin Atkinson *
785fe0f6362SGavin Atkinson */
78627e4bd81SHans Petter Selasky
78727e4bd81SHans Petter Selasky /* accept raw divisor data, if someone wants to do the math in user domain */
78827e4bd81SHans Petter Selasky if (t->c_ospeed & 0x80000000)
78927e4bd81SHans Petter Selasky return 0;
79027e4bd81SHans Petter Selasky switch (sc->sc_chiptype) {
791ea0efc37SHans Petter Selasky case TYPE_PL2303HXN:
792ea0efc37SHans Petter Selasky if (t->c_ospeed <= 12000000)
793ea0efc37SHans Petter Selasky return (0);
794ea0efc37SHans Petter Selasky break;
79527e4bd81SHans Petter Selasky case TYPE_PL2303HXD:
79627e4bd81SHans Petter Selasky if (t->c_ospeed <= 12000000)
797fe0f6362SGavin Atkinson return (0);
79827e4bd81SHans Petter Selasky break;
79927e4bd81SHans Petter Selasky case TYPE_PL2303HX:
800fe0f6362SGavin Atkinson if (t->c_ospeed <= 6000000)
801fe0f6362SGavin Atkinson return (0);
80227e4bd81SHans Petter Selasky break;
80327e4bd81SHans Petter Selasky default:
80427e4bd81SHans Petter Selasky if (uplcom_baud_supported(t->c_ospeed))
80527e4bd81SHans Petter Selasky return (0);
80627e4bd81SHans Petter Selasky break;
80702ac6454SAndrew Thompson }
80802ac6454SAndrew Thompson
809fe0f6362SGavin Atkinson DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed);
810fe0f6362SGavin Atkinson return (EIO);
81102ac6454SAndrew Thompson }
81202ac6454SAndrew Thompson
81362d42655SHans Petter Selasky static unsigned
uplcom_encode_baud_rate_divisor(uint8_t * buf,unsigned baud)81462d42655SHans Petter Selasky uplcom_encode_baud_rate_divisor(uint8_t *buf, unsigned baud)
81527e4bd81SHans Petter Selasky {
81662d42655SHans Petter Selasky unsigned baseline, mantissa, exponent;
81727e4bd81SHans Petter Selasky
81827e4bd81SHans Petter Selasky /* Determine the baud rate divisor. This algorithm is taken from Linux. */
81927e4bd81SHans Petter Selasky /*
82027e4bd81SHans Petter Selasky * Apparently the formula is:
82127e4bd81SHans Petter Selasky * baudrate = baseline / (mantissa * 4^exponent)
82227e4bd81SHans Petter Selasky * where
82327e4bd81SHans Petter Selasky * mantissa = buf[8:0]
82427e4bd81SHans Petter Selasky * exponent = buf[11:9]
82527e4bd81SHans Petter Selasky */
82627e4bd81SHans Petter Selasky if (baud == 0)
82727e4bd81SHans Petter Selasky baud = 1;
82827e4bd81SHans Petter Selasky baseline = 383385600;
82927e4bd81SHans Petter Selasky mantissa = baseline / baud;
83027e4bd81SHans Petter Selasky if (mantissa == 0)
83127e4bd81SHans Petter Selasky mantissa = 1;
83227e4bd81SHans Petter Selasky exponent = 0;
83327e4bd81SHans Petter Selasky while (mantissa >= 512) {
83427e4bd81SHans Petter Selasky if (exponent < 7) {
83527e4bd81SHans Petter Selasky mantissa >>= 2; /* divide by 4 */
83627e4bd81SHans Petter Selasky exponent++;
83727e4bd81SHans Petter Selasky } else {
83827e4bd81SHans Petter Selasky /* Exponent is maxed. Trim mantissa and leave. This gives approx. 45.8 baud */
83927e4bd81SHans Petter Selasky mantissa = 511;
84027e4bd81SHans Petter Selasky break;
84127e4bd81SHans Petter Selasky }
84227e4bd81SHans Petter Selasky }
84327e4bd81SHans Petter Selasky
84427e4bd81SHans Petter Selasky buf[3] = 0x80;
84527e4bd81SHans Petter Selasky buf[2] = 0;
84627e4bd81SHans Petter Selasky buf[1] = exponent << 1 | mantissa >> 8;
84727e4bd81SHans Petter Selasky buf[0] = mantissa & 0xff;
84827e4bd81SHans Petter Selasky
84927e4bd81SHans Petter Selasky /* Calculate and return the exact baud rate. */
85027e4bd81SHans Petter Selasky baud = (baseline / mantissa) >> (exponent << 1);
85127e4bd81SHans Petter Selasky DPRINTF("real baud rate will be %u\n", baud);
85227e4bd81SHans Petter Selasky
85327e4bd81SHans Petter Selasky return baud;
85427e4bd81SHans Petter Selasky }
85502ac6454SAndrew Thompson static void
uplcom_cfg_param(struct ucom_softc * ucom,struct termios * t)856760bc48eSAndrew Thompson uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
85702ac6454SAndrew Thompson {
85802ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
859760bc48eSAndrew Thompson struct usb_cdc_line_state ls;
860760bc48eSAndrew Thompson struct usb_device_request req;
86102ac6454SAndrew Thompson
86202ac6454SAndrew Thompson DPRINTF("sc = %p\n", sc);
86302ac6454SAndrew Thompson
864271ae033SHans Petter Selasky memset(&ls, 0, sizeof(ls));
86502ac6454SAndrew Thompson
86627e4bd81SHans Petter Selasky /*
86727e4bd81SHans Petter Selasky * NOTE: If unsupported baud rates are set directly, the PL2303* uses 9600 baud.
86827e4bd81SHans Petter Selasky */
86927e4bd81SHans Petter Selasky if ((t->c_ospeed & 0x80000000) || uplcom_baud_supported(t->c_ospeed))
87002ac6454SAndrew Thompson USETDW(ls.dwDTERate, t->c_ospeed);
87127e4bd81SHans Petter Selasky else
87227e4bd81SHans Petter Selasky t->c_ospeed = uplcom_encode_baud_rate_divisor((uint8_t*)&ls.dwDTERate, t->c_ospeed);
87302ac6454SAndrew Thompson
87402ac6454SAndrew Thompson if (t->c_cflag & CSTOPB) {
87527e4bd81SHans Petter Selasky if ((t->c_cflag & CSIZE) == CS5) {
87627e4bd81SHans Petter Selasky /*
87727e4bd81SHans Petter Selasky * NOTE: Comply with "real" UARTs / RS232:
87827e4bd81SHans Petter Selasky * use 1.5 instead of 2 stop bits with 5 data bits
87927e4bd81SHans Petter Selasky */
88027e4bd81SHans Petter Selasky ls.bCharFormat = UCDC_STOP_BIT_1_5;
88127e4bd81SHans Petter Selasky } else {
88202ac6454SAndrew Thompson ls.bCharFormat = UCDC_STOP_BIT_2;
88327e4bd81SHans Petter Selasky }
88402ac6454SAndrew Thompson } else {
88502ac6454SAndrew Thompson ls.bCharFormat = UCDC_STOP_BIT_1;
88602ac6454SAndrew Thompson }
88702ac6454SAndrew Thompson
88802ac6454SAndrew Thompson if (t->c_cflag & PARENB) {
88902ac6454SAndrew Thompson if (t->c_cflag & PARODD) {
89002ac6454SAndrew Thompson ls.bParityType = UCDC_PARITY_ODD;
89102ac6454SAndrew Thompson } else {
89202ac6454SAndrew Thompson ls.bParityType = UCDC_PARITY_EVEN;
89302ac6454SAndrew Thompson }
89402ac6454SAndrew Thompson } else {
89502ac6454SAndrew Thompson ls.bParityType = UCDC_PARITY_NONE;
89602ac6454SAndrew Thompson }
89702ac6454SAndrew Thompson
89802ac6454SAndrew Thompson switch (t->c_cflag & CSIZE) {
89902ac6454SAndrew Thompson case CS5:
90002ac6454SAndrew Thompson ls.bDataBits = 5;
90102ac6454SAndrew Thompson break;
90202ac6454SAndrew Thompson case CS6:
90302ac6454SAndrew Thompson ls.bDataBits = 6;
90402ac6454SAndrew Thompson break;
90502ac6454SAndrew Thompson case CS7:
90602ac6454SAndrew Thompson ls.bDataBits = 7;
90702ac6454SAndrew Thompson break;
90802ac6454SAndrew Thompson case CS8:
90902ac6454SAndrew Thompson ls.bDataBits = 8;
91002ac6454SAndrew Thompson break;
91102ac6454SAndrew Thompson }
91202ac6454SAndrew Thompson
91327e4bd81SHans Petter Selasky DPRINTF("rate=0x%08x fmt=%d parity=%d bits=%d\n",
91402ac6454SAndrew Thompson UGETDW(ls.dwDTERate), ls.bCharFormat,
91502ac6454SAndrew Thompson ls.bParityType, ls.bDataBits);
91602ac6454SAndrew Thompson
91702ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
91802ac6454SAndrew Thompson req.bRequest = UCDC_SET_LINE_CODING;
91902ac6454SAndrew Thompson USETW(req.wValue, 0);
92002ac6454SAndrew Thompson req.wIndex[0] = sc->sc_data_iface_no;
92102ac6454SAndrew Thompson req.wIndex[1] = 0;
92202ac6454SAndrew Thompson USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
92302ac6454SAndrew Thompson
924a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
92502ac6454SAndrew Thompson &req, &ls, 0, 1000);
92602ac6454SAndrew Thompson
92702ac6454SAndrew Thompson if (t->c_cflag & CRTSCTS) {
92802ac6454SAndrew Thompson DPRINTF("crtscts = on\n");
92902ac6454SAndrew Thompson
93002ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
931ea0efc37SHans Petter Selasky if (sc->sc_chiptype == TYPE_PL2303HXN) {
932ea0efc37SHans Petter Selasky req.bRequest = UPLCOM_SET_REQUEST_PL2303HXN;
933ea0efc37SHans Petter Selasky USETW(req.wValue, UPLCOM_CRTSCTS_REG_PL2303HXN);
934ea0efc37SHans Petter Selasky USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303HXN);
935ea0efc37SHans Petter Selasky } else {
93602ac6454SAndrew Thompson req.bRequest = UPLCOM_SET_REQUEST;
93702ac6454SAndrew Thompson USETW(req.wValue, 0);
93827e4bd81SHans Petter Selasky if (sc->sc_chiptype != TYPE_PL2303)
93902ac6454SAndrew Thompson USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
94002ac6454SAndrew Thompson else
94102ac6454SAndrew Thompson USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
942ea0efc37SHans Petter Selasky }
94302ac6454SAndrew Thompson USETW(req.wLength, 0);
94402ac6454SAndrew Thompson
945a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
94602ac6454SAndrew Thompson &req, NULL, 0, 1000);
94702ac6454SAndrew Thompson } else {
94802ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
949ea0efc37SHans Petter Selasky if (sc->sc_chiptype == TYPE_PL2303HXN) {
950ea0efc37SHans Petter Selasky req.bRequest = UPLCOM_SET_REQUEST_PL2303HXN;
951ea0efc37SHans Petter Selasky USETW(req.wValue, UPLCOM_CRTSCTS_REG_PL2303HXN);
952ea0efc37SHans Petter Selasky USETW(req.wIndex, UPLCOM_CLEAR_CRTSCTS_PL2303HXN);
953ea0efc37SHans Petter Selasky }
954ea0efc37SHans Petter Selasky else {
95502ac6454SAndrew Thompson req.bRequest = UPLCOM_SET_REQUEST;
95602ac6454SAndrew Thompson USETW(req.wValue, 0);
95702ac6454SAndrew Thompson USETW(req.wIndex, 0);
958ea0efc37SHans Petter Selasky }
95902ac6454SAndrew Thompson USETW(req.wLength, 0);
960a593f6b8SAndrew Thompson ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
96102ac6454SAndrew Thompson &req, NULL, 0, 1000);
96202ac6454SAndrew Thompson }
96302ac6454SAndrew Thompson }
96402ac6454SAndrew Thompson
96502ac6454SAndrew Thompson static void
uplcom_start_read(struct ucom_softc * ucom)966760bc48eSAndrew Thompson uplcom_start_read(struct ucom_softc *ucom)
96702ac6454SAndrew Thompson {
96802ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
96902ac6454SAndrew Thompson
97002ac6454SAndrew Thompson /* start interrupt endpoint */
971a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
97202ac6454SAndrew Thompson
97302ac6454SAndrew Thompson /* start read endpoint */
974a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
97502ac6454SAndrew Thompson }
97602ac6454SAndrew Thompson
97702ac6454SAndrew Thompson static void
uplcom_stop_read(struct ucom_softc * ucom)978760bc48eSAndrew Thompson uplcom_stop_read(struct ucom_softc *ucom)
97902ac6454SAndrew Thompson {
98002ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
98102ac6454SAndrew Thompson
98202ac6454SAndrew Thompson /* stop interrupt endpoint */
983a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
98402ac6454SAndrew Thompson
98502ac6454SAndrew Thompson /* stop read endpoint */
986a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
98702ac6454SAndrew Thompson }
98802ac6454SAndrew Thompson
98902ac6454SAndrew Thompson static void
uplcom_start_write(struct ucom_softc * ucom)990760bc48eSAndrew Thompson uplcom_start_write(struct ucom_softc *ucom)
99102ac6454SAndrew Thompson {
99202ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
99302ac6454SAndrew Thompson
994a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
99502ac6454SAndrew Thompson }
99602ac6454SAndrew Thompson
99702ac6454SAndrew Thompson static void
uplcom_stop_write(struct ucom_softc * ucom)998760bc48eSAndrew Thompson uplcom_stop_write(struct ucom_softc *ucom)
99902ac6454SAndrew Thompson {
100002ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
100102ac6454SAndrew Thompson
1002a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
100302ac6454SAndrew Thompson }
100402ac6454SAndrew Thompson
100502ac6454SAndrew Thompson static void
uplcom_cfg_get_status(struct ucom_softc * ucom,uint8_t * lsr,uint8_t * msr)1006760bc48eSAndrew Thompson uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
100702ac6454SAndrew Thompson {
100802ac6454SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
100902ac6454SAndrew Thompson
101002ac6454SAndrew Thompson DPRINTF("\n");
101102ac6454SAndrew Thompson
101202ac6454SAndrew Thompson *lsr = sc->sc_lsr;
101302ac6454SAndrew Thompson *msr = sc->sc_msr;
101402ac6454SAndrew Thompson }
101502ac6454SAndrew Thompson
101602ac6454SAndrew Thompson static void
uplcom_intr_callback(struct usb_xfer * xfer,usb_error_t error)1017ed6d949aSAndrew Thompson uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
101802ac6454SAndrew Thompson {
1019ed6d949aSAndrew Thompson struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1020ed6d949aSAndrew Thompson struct usb_page_cache *pc;
102102ac6454SAndrew Thompson uint8_t buf[9];
1022ed6d949aSAndrew Thompson int actlen;
1023ed6d949aSAndrew Thompson
1024ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
102502ac6454SAndrew Thompson
102602ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) {
102702ac6454SAndrew Thompson case USB_ST_TRANSFERRED:
102802ac6454SAndrew Thompson
1029ed6d949aSAndrew Thompson DPRINTF("actlen = %u\n", actlen);
103002ac6454SAndrew Thompson
1031ed6d949aSAndrew Thompson if (actlen >= 9) {
1032ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0);
1033ed6d949aSAndrew Thompson usbd_copy_out(pc, 0, buf, sizeof(buf));
103402ac6454SAndrew Thompson
103527e4bd81SHans Petter Selasky DPRINTF("status = 0x%02x\n", buf[UPLCOM_STATE_INDEX]);
103602ac6454SAndrew Thompson
103702ac6454SAndrew Thompson sc->sc_lsr = 0;
103802ac6454SAndrew Thompson sc->sc_msr = 0;
103902ac6454SAndrew Thompson
104027e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_CTS) {
104102ac6454SAndrew Thompson sc->sc_msr |= SER_CTS;
104202ac6454SAndrew Thompson }
104327e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_OVERRUN_ERROR) {
104427e4bd81SHans Petter Selasky sc->sc_lsr |= ULSR_OE;
104527e4bd81SHans Petter Selasky }
104627e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_PARITY_ERROR) {
104727e4bd81SHans Petter Selasky sc->sc_lsr |= ULSR_PE;
104827e4bd81SHans Petter Selasky }
104927e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_FRAME_ERROR) {
105027e4bd81SHans Petter Selasky sc->sc_lsr |= ULSR_FE;
105127e4bd81SHans Petter Selasky }
105227e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_RING) {
105327e4bd81SHans Petter Selasky sc->sc_msr |= SER_RI;
105427e4bd81SHans Petter Selasky }
105527e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_BREAK_ERROR) {
105627e4bd81SHans Petter Selasky sc->sc_lsr |= ULSR_BI;
105727e4bd81SHans Petter Selasky }
105827e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DSR) {
105902ac6454SAndrew Thompson sc->sc_msr |= SER_DSR;
106002ac6454SAndrew Thompson }
106127e4bd81SHans Petter Selasky if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DCD) {
106202ac6454SAndrew Thompson sc->sc_msr |= SER_DCD;
106302ac6454SAndrew Thompson }
1064a593f6b8SAndrew Thompson ucom_status_change(&sc->sc_ucom);
106502ac6454SAndrew Thompson }
106602ac6454SAndrew Thompson case USB_ST_SETUP:
106702ac6454SAndrew Thompson tr_setup:
1068ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1069a593f6b8SAndrew Thompson usbd_transfer_submit(xfer);
107002ac6454SAndrew Thompson return;
107102ac6454SAndrew Thompson
107202ac6454SAndrew Thompson default: /* Error */
1073ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) {
107402ac6454SAndrew Thompson /* try to clear stall first */
1075ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer);
107602ac6454SAndrew Thompson goto tr_setup;
107702ac6454SAndrew Thompson }
107802ac6454SAndrew Thompson return;
107902ac6454SAndrew Thompson }
108002ac6454SAndrew Thompson }
108102ac6454SAndrew Thompson
108202ac6454SAndrew Thompson static void
uplcom_write_callback(struct usb_xfer * xfer,usb_error_t error)1083ed6d949aSAndrew Thompson uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
108402ac6454SAndrew Thompson {
1085ed6d949aSAndrew Thompson struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1086ed6d949aSAndrew Thompson struct usb_page_cache *pc;
108702ac6454SAndrew Thompson uint32_t actlen;
108802ac6454SAndrew Thompson
108902ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) {
109002ac6454SAndrew Thompson case USB_ST_SETUP:
109102ac6454SAndrew Thompson case USB_ST_TRANSFERRED:
109202ac6454SAndrew Thompson tr_setup:
1093ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0);
1094ed6d949aSAndrew Thompson if (ucom_get_data(&sc->sc_ucom, pc, 0,
109502ac6454SAndrew Thompson UPLCOM_BULK_BUF_SIZE, &actlen)) {
109602ac6454SAndrew Thompson DPRINTF("actlen = %d\n", actlen);
109702ac6454SAndrew Thompson
1098ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, actlen);
1099a593f6b8SAndrew Thompson usbd_transfer_submit(xfer);
110002ac6454SAndrew Thompson }
1101ec97e9caSHans Petter Selasky return;
110202ac6454SAndrew Thompson
110302ac6454SAndrew Thompson default: /* Error */
1104ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) {
110502ac6454SAndrew Thompson /* try to clear stall first */
1106ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer);
110702ac6454SAndrew Thompson goto tr_setup;
110802ac6454SAndrew Thompson }
1109ec97e9caSHans Petter Selasky return;
111002ac6454SAndrew Thompson }
111102ac6454SAndrew Thompson }
111202ac6454SAndrew Thompson
111302ac6454SAndrew Thompson static void
uplcom_read_callback(struct usb_xfer * xfer,usb_error_t error)1114ed6d949aSAndrew Thompson uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
111502ac6454SAndrew Thompson {
1116ed6d949aSAndrew Thompson struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1117ed6d949aSAndrew Thompson struct usb_page_cache *pc;
1118ed6d949aSAndrew Thompson int actlen;
1119ed6d949aSAndrew Thompson
1120ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
112102ac6454SAndrew Thompson
112202ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) {
112302ac6454SAndrew Thompson case USB_ST_TRANSFERRED:
1124ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0);
1125ed6d949aSAndrew Thompson ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
112602ac6454SAndrew Thompson
112702ac6454SAndrew Thompson case USB_ST_SETUP:
112802ac6454SAndrew Thompson tr_setup:
1129ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1130a593f6b8SAndrew Thompson usbd_transfer_submit(xfer);
113102ac6454SAndrew Thompson return;
113202ac6454SAndrew Thompson
113302ac6454SAndrew Thompson default: /* Error */
1134ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) {
113502ac6454SAndrew Thompson /* try to clear stall first */
1136ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer);
113702ac6454SAndrew Thompson goto tr_setup;
113802ac6454SAndrew Thompson }
113902ac6454SAndrew Thompson return;
114002ac6454SAndrew Thompson }
114102ac6454SAndrew Thompson }
1142655dc9d0SAndrew Thompson
1143655dc9d0SAndrew Thompson static void
uplcom_poll(struct ucom_softc * ucom)1144655dc9d0SAndrew Thompson uplcom_poll(struct ucom_softc *ucom)
1145655dc9d0SAndrew Thompson {
1146655dc9d0SAndrew Thompson struct uplcom_softc *sc = ucom->sc_parent;
1147655dc9d0SAndrew Thompson usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);
1148655dc9d0SAndrew Thompson }
1149