xref: /freebsd/sys/dev/usb/serial/uplcom.c (revision fe0f636233d6b4468136d40041623f11af9d65c0)
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 /*-
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  * 3. All advertising materials mentioning features or use of this software
4802ac6454SAndrew Thompson  *    must display the following acknowledgement:
4902ac6454SAndrew Thompson  *        This product includes software developed by the NetBSD
5002ac6454SAndrew Thompson  *        Foundation, Inc. and its contributors.
5102ac6454SAndrew Thompson  * 4. Neither the name of The NetBSD Foundation nor the names of its
5202ac6454SAndrew Thompson  *    contributors may be used to endorse or promote products derived
5302ac6454SAndrew Thompson  *    from this software without specific prior written permission.
5402ac6454SAndrew Thompson  *
5502ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
5602ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
5702ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5802ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5902ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
6002ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
6102ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
6202ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
6302ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6402ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
6502ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
6602ac6454SAndrew Thompson  */
6702ac6454SAndrew Thompson 
6802ac6454SAndrew Thompson /*
6902ac6454SAndrew Thompson  * This driver supports several USB-to-RS232 serial adapters driven by
7002ac6454SAndrew Thompson  * Prolific PL-2303, PL-2303X and probably PL-2303HX USB-to-RS232
7102ac6454SAndrew Thompson  * bridge chip.  The adapters are sold under many different brand
7202ac6454SAndrew Thompson  * names.
7302ac6454SAndrew Thompson  *
7402ac6454SAndrew Thompson  * Datasheets are available at Prolific www site at
7502ac6454SAndrew Thompson  * http://www.prolific.com.tw.  The datasheets don't contain full
7602ac6454SAndrew Thompson  * programming information for the chip.
7702ac6454SAndrew Thompson  *
7802ac6454SAndrew Thompson  * PL-2303HX is probably programmed the same as PL-2303X.
7902ac6454SAndrew Thompson  *
8002ac6454SAndrew Thompson  * There are several differences between PL-2303 and PL-2303(H)X.
8102ac6454SAndrew Thompson  * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_
8202ac6454SAndrew Thompson  * different command for controlling CRTSCTS and needs special
8302ac6454SAndrew Thompson  * sequence of commands for initialization which aren't also
8402ac6454SAndrew Thompson  * documented in the datasheet.
8502ac6454SAndrew Thompson  */
8602ac6454SAndrew Thompson 
87ed6d949aSAndrew Thompson #include <sys/stdint.h>
88ed6d949aSAndrew Thompson #include <sys/stddef.h>
89ed6d949aSAndrew Thompson #include <sys/param.h>
90ed6d949aSAndrew Thompson #include <sys/queue.h>
91ed6d949aSAndrew Thompson #include <sys/types.h>
92ed6d949aSAndrew Thompson #include <sys/systm.h>
93ed6d949aSAndrew Thompson #include <sys/kernel.h>
94ed6d949aSAndrew Thompson #include <sys/bus.h>
95ed6d949aSAndrew Thompson #include <sys/linker_set.h>
96ed6d949aSAndrew Thompson #include <sys/module.h>
97ed6d949aSAndrew Thompson #include <sys/lock.h>
98ed6d949aSAndrew Thompson #include <sys/mutex.h>
99ed6d949aSAndrew Thompson #include <sys/condvar.h>
100ed6d949aSAndrew Thompson #include <sys/sysctl.h>
101ed6d949aSAndrew Thompson #include <sys/sx.h>
102ed6d949aSAndrew Thompson #include <sys/unistd.h>
103ed6d949aSAndrew Thompson #include <sys/callout.h>
104ed6d949aSAndrew Thompson #include <sys/malloc.h>
105ed6d949aSAndrew Thompson #include <sys/priv.h>
106ed6d949aSAndrew Thompson 
10702ac6454SAndrew Thompson #include <dev/usb/usb.h>
108ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
109ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
11002ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
111ed6d949aSAndrew Thompson #include "usbdevs.h"
11202ac6454SAndrew Thompson 
11302ac6454SAndrew Thompson #define	USB_DEBUG_VAR uplcom_debug
11402ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
11502ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
11602ac6454SAndrew Thompson 
11702ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
11802ac6454SAndrew Thompson 
119b850ecc1SAndrew Thompson #ifdef USB_DEBUG
12002ac6454SAndrew Thompson static int uplcom_debug = 0;
12102ac6454SAndrew Thompson 
1229360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom");
1239360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RW,
12402ac6454SAndrew Thompson     &uplcom_debug, 0, "Debug level");
12502ac6454SAndrew Thompson #endif
12602ac6454SAndrew Thompson 
12702ac6454SAndrew Thompson #define	UPLCOM_MODVER			1	/* module version */
12802ac6454SAndrew Thompson 
12902ac6454SAndrew Thompson #define	UPLCOM_CONFIG_INDEX		0
13002ac6454SAndrew Thompson #define	UPLCOM_IFACE_INDEX		0
13102ac6454SAndrew Thompson #define	UPLCOM_SECOND_IFACE_INDEX	1
13202ac6454SAndrew Thompson 
13302ac6454SAndrew Thompson #ifndef UPLCOM_INTR_INTERVAL
13402ac6454SAndrew Thompson #define	UPLCOM_INTR_INTERVAL		0	/* default */
13502ac6454SAndrew Thompson #endif
13602ac6454SAndrew Thompson 
13702ac6454SAndrew Thompson #define	UPLCOM_BULK_BUF_SIZE 1024	/* bytes */
13802ac6454SAndrew Thompson 
13902ac6454SAndrew Thompson #define	UPLCOM_SET_REQUEST		0x01
14002ac6454SAndrew Thompson #define	UPLCOM_SET_CRTSCTS		0x41
14102ac6454SAndrew Thompson #define	UPLCOM_SET_CRTSCTS_PL2303X	0x61
14202ac6454SAndrew Thompson #define	RSAQ_STATUS_CTS			0x80
14302ac6454SAndrew Thompson #define	RSAQ_STATUS_DSR			0x02
14402ac6454SAndrew Thompson #define	RSAQ_STATUS_DCD			0x01
14502ac6454SAndrew Thompson 
14602ac6454SAndrew Thompson #define	TYPE_PL2303			0
14702ac6454SAndrew Thompson #define	TYPE_PL2303X			1
14802ac6454SAndrew Thompson 
14902ac6454SAndrew Thompson enum {
15002ac6454SAndrew Thompson 	UPLCOM_BULK_DT_WR,
15102ac6454SAndrew Thompson 	UPLCOM_BULK_DT_RD,
15202ac6454SAndrew Thompson 	UPLCOM_INTR_DT_RD,
15302ac6454SAndrew Thompson 	UPLCOM_N_TRANSFER,
15402ac6454SAndrew Thompson };
15502ac6454SAndrew Thompson 
15602ac6454SAndrew Thompson struct uplcom_softc {
157760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
158760bc48eSAndrew Thompson 	struct ucom_softc sc_ucom;
15902ac6454SAndrew Thompson 
160760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER];
161760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
162deefe583SAndrew Thompson 	struct mtx sc_mtx;
16302ac6454SAndrew Thompson 
16402ac6454SAndrew Thompson 	uint16_t sc_line;
16502ac6454SAndrew Thompson 
16602ac6454SAndrew Thompson 	uint8_t	sc_lsr;			/* local status register */
16702ac6454SAndrew Thompson 	uint8_t	sc_msr;			/* uplcom status register */
16802ac6454SAndrew Thompson 	uint8_t	sc_chiptype;		/* type of chip */
16902ac6454SAndrew Thompson 	uint8_t	sc_ctrl_iface_no;
17002ac6454SAndrew Thompson 	uint8_t	sc_data_iface_no;
17102ac6454SAndrew Thompson 	uint8_t	sc_iface_index[2];
17202ac6454SAndrew Thompson };
17302ac6454SAndrew Thompson 
17402ac6454SAndrew Thompson /* prototypes */
17502ac6454SAndrew Thompson 
176e0a69b51SAndrew Thompson static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *);
177760bc48eSAndrew Thompson static int	uplcom_pl2303x_init(struct usb_device *);
178760bc48eSAndrew Thompson static void	uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
179760bc48eSAndrew Thompson static void	uplcom_cfg_set_rts(struct ucom_softc *, uint8_t);
180760bc48eSAndrew Thompson static void	uplcom_cfg_set_break(struct ucom_softc *, uint8_t);
181760bc48eSAndrew Thompson static int	uplcom_pre_param(struct ucom_softc *, struct termios *);
182760bc48eSAndrew Thompson static void	uplcom_cfg_param(struct ucom_softc *, struct termios *);
183760bc48eSAndrew Thompson static void	uplcom_start_read(struct ucom_softc *);
184760bc48eSAndrew Thompson static void	uplcom_stop_read(struct ucom_softc *);
185760bc48eSAndrew Thompson static void	uplcom_start_write(struct ucom_softc *);
186760bc48eSAndrew Thompson static void	uplcom_stop_write(struct ucom_softc *);
187760bc48eSAndrew Thompson static void	uplcom_cfg_get_status(struct ucom_softc *, uint8_t *,
18802ac6454SAndrew Thompson 		    uint8_t *);
189655dc9d0SAndrew Thompson static void	uplcom_poll(struct ucom_softc *ucom);
19002ac6454SAndrew Thompson 
19102ac6454SAndrew Thompson static device_probe_t uplcom_probe;
19202ac6454SAndrew Thompson static device_attach_t uplcom_attach;
19302ac6454SAndrew Thompson static device_detach_t uplcom_detach;
19402ac6454SAndrew Thompson 
195e0a69b51SAndrew Thompson static usb_callback_t uplcom_intr_callback;
196e0a69b51SAndrew Thompson static usb_callback_t uplcom_write_callback;
197e0a69b51SAndrew Thompson static usb_callback_t uplcom_read_callback;
19802ac6454SAndrew Thompson 
199760bc48eSAndrew Thompson static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
20002ac6454SAndrew Thompson 
20102ac6454SAndrew Thompson 	[UPLCOM_BULK_DT_WR] = {
20202ac6454SAndrew Thompson 		.type = UE_BULK,
20302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
20402ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
2054eae601eSAndrew Thompson 		.bufsize = UPLCOM_BULK_BUF_SIZE,
2064eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2074eae601eSAndrew Thompson 		.callback = &uplcom_write_callback,
20802ac6454SAndrew Thompson 		.if_index = 0,
20902ac6454SAndrew Thompson 	},
21002ac6454SAndrew Thompson 
21102ac6454SAndrew Thompson 	[UPLCOM_BULK_DT_RD] = {
21202ac6454SAndrew Thompson 		.type = UE_BULK,
21302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
21402ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2154eae601eSAndrew Thompson 		.bufsize = UPLCOM_BULK_BUF_SIZE,
2164eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2174eae601eSAndrew Thompson 		.callback = &uplcom_read_callback,
21802ac6454SAndrew Thompson 		.if_index = 0,
21902ac6454SAndrew Thompson 	},
22002ac6454SAndrew Thompson 
22102ac6454SAndrew Thompson 	[UPLCOM_INTR_DT_RD] = {
22202ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
22302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
22402ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2254eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2264eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
2274eae601eSAndrew Thompson 		.callback = &uplcom_intr_callback,
22802ac6454SAndrew Thompson 		.if_index = 1,
22902ac6454SAndrew Thompson 	},
23002ac6454SAndrew Thompson };
23102ac6454SAndrew Thompson 
232a8675caeSAndrew Thompson static struct ucom_callback uplcom_callback = {
233a593f6b8SAndrew Thompson 	.ucom_cfg_get_status = &uplcom_cfg_get_status,
234a593f6b8SAndrew Thompson 	.ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
235a593f6b8SAndrew Thompson 	.ucom_cfg_set_rts = &uplcom_cfg_set_rts,
236a593f6b8SAndrew Thompson 	.ucom_cfg_set_break = &uplcom_cfg_set_break,
237a593f6b8SAndrew Thompson 	.ucom_cfg_param = &uplcom_cfg_param,
238a593f6b8SAndrew Thompson 	.ucom_pre_param = &uplcom_pre_param,
239a593f6b8SAndrew Thompson 	.ucom_start_read = &uplcom_start_read,
240a593f6b8SAndrew Thompson 	.ucom_stop_read = &uplcom_stop_read,
241a593f6b8SAndrew Thompson 	.ucom_start_write = &uplcom_start_write,
242a593f6b8SAndrew Thompson 	.ucom_stop_write = &uplcom_stop_write,
243655dc9d0SAndrew Thompson 	.ucom_poll = &uplcom_poll,
24402ac6454SAndrew Thompson };
24502ac6454SAndrew Thompson 
2469e6b5313SAndrew Thompson #define	UPLCOM_DEV(v,p,rl,rh,t)				\
2479e6b5313SAndrew Thompson   { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p), \
2489e6b5313SAndrew Thompson     USB_DEV_BCD_GTEQ(rl), USB_DEV_BCD_LTEQ(rh), USB_DRIVER_INFO(TYPE_##t) }
24902ac6454SAndrew Thompson 
250760bc48eSAndrew Thompson static const struct usb_device_id uplcom_devs[] = {
25102ac6454SAndrew Thompson 	/* Belkin F5U257 */
2529e6b5313SAndrew Thompson 	UPLCOM_DEV(BELKIN, F5U257, 0, 0xFFFF, PL2303X),
25302ac6454SAndrew Thompson 	/* I/O DATA USB-RSAQ */
2549e6b5313SAndrew Thompson 	UPLCOM_DEV(IODATA, USBRSAQ, 0, 0xFFFF, PL2303),
25502ac6454SAndrew Thompson 	/* I/O DATA USB-RSAQ2 */
2569e6b5313SAndrew Thompson 	UPLCOM_DEV(PROLIFIC, RSAQ2, 0, 0xFFFF, PL2303),
25702ac6454SAndrew Thompson 	/* I/O DATA USB-RSAQ3 */
2589e6b5313SAndrew Thompson 	UPLCOM_DEV(PROLIFIC, RSAQ3, 0, 0xFFFF, PL2303X),
2591b0f43beSTakanori Watanabe 	/* I/O DATA USB-RSAQ5 */
26055fdde37STakanori Watanabe 	UPLCOM_DEV(IODATA, USBRSAQ5, 0, 0xFFFF, PL2303X),
26102ac6454SAndrew Thompson 	/* PLANEX USB-RS232 URS-03 */
2629e6b5313SAndrew Thompson 	UPLCOM_DEV(ATEN, UC232A, 0, 0xFFFF, PL2303),
26302ac6454SAndrew Thompson 	/* TrendNet TU-S9 */
2649e6b5313SAndrew Thompson 	UPLCOM_DEV(PROLIFIC, PL2303, 0x0400, 0xFFFF, PL2303X),
26502ac6454SAndrew Thompson 	/* ST Lab USB-SERIAL-4 */
2669e6b5313SAndrew Thompson 	UPLCOM_DEV(PROLIFIC, PL2303, 0x0300, 0x03FF, PL2303X),
26702ac6454SAndrew Thompson 	/* IOGEAR/ATEN UC-232A (also ST Lab USB-SERIAL-1) */
2689e6b5313SAndrew Thompson 	UPLCOM_DEV(PROLIFIC, PL2303, 0, 0x02FF, PL2303),
26902ac6454SAndrew Thompson 	/* TDK USB-PHS Adapter UHA6400 */
2709e6b5313SAndrew Thompson 	UPLCOM_DEV(TDK, UHA6400, 0, 0xFFFF, PL2303),
27102ac6454SAndrew Thompson 	/* RATOC REX-USB60 */
2729e6b5313SAndrew Thompson 	UPLCOM_DEV(RATOC, REXUSB60, 0, 0xFFFF, PL2303),
27302ac6454SAndrew Thompson 	/* ELECOM UC-SGT */
2749e6b5313SAndrew Thompson 	UPLCOM_DEV(ELECOM, UCSGT, 0, 0xFFFF, PL2303),
2759e6b5313SAndrew Thompson 	UPLCOM_DEV(ELECOM, UCSGT0, 0, 0xFFFF, PL2303),
27602ac6454SAndrew Thompson 	/* Sagem USB-Serial Controller */
2779e6b5313SAndrew Thompson 	UPLCOM_DEV(SAGEM, USBSERIAL, 0, 0xFFFF, PL2303X),
27802ac6454SAndrew Thompson 	/* Sony Ericsson USB Cable */
2799e6b5313SAndrew Thompson 	UPLCOM_DEV(SONYERICSSON, DCU10, 0, 0xFFFF, PL2303),
28002ac6454SAndrew Thompson 	/* SOURCENEXT KeikaiDenwa 8 */
2819e6b5313SAndrew Thompson 	UPLCOM_DEV(SOURCENEXT, KEIKAI8, 0, 0xFFFF, PL2303),
28202ac6454SAndrew Thompson 	/* SOURCENEXT KeikaiDenwa 8 with charger */
2839e6b5313SAndrew Thompson 	UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG, 0, 0, PL2303),
28402ac6454SAndrew Thompson 	/* HAL Corporation Crossam2+USB */
2859e6b5313SAndrew Thompson 	UPLCOM_DEV(HAL, IMR001, 0, 0xFFFF, PL2303),
28602ac6454SAndrew Thompson 	/* Sitecom USB to Serial */
2879e6b5313SAndrew Thompson 	UPLCOM_DEV(SITECOM, SERIAL, 0, 0xFFFF, PL2303),
28802ac6454SAndrew Thompson 	/* Tripp-Lite U209-000-R */
2899e6b5313SAndrew Thompson 	UPLCOM_DEV(TRIPPLITE, U209, 0, 0xFFFF, PL2303X),
2909e6b5313SAndrew Thompson 	UPLCOM_DEV(RADIOSHACK, USBCABLE, 0, 0xFFFF, PL2303),
29102ac6454SAndrew Thompson 	/* Prolific Pharos */
2929e6b5313SAndrew Thompson 	UPLCOM_DEV(PROLIFIC, PHAROS, 0, 0xFFFF, PL2303),
29302ac6454SAndrew Thompson 	/* Willcom W-SIM */
2949e6b5313SAndrew Thompson 	UPLCOM_DEV(PROLIFIC2, WSIM, 0, 0xFFFF, PL2303X),
295dc4b1d16SAndrew Thompson 	/* Mobile Action MA-620 Infrared Adapter */
2969e6b5313SAndrew Thompson 	UPLCOM_DEV(MOBILEACTION, MA620, 0, 0xFFFF, PL2303X),
297082dbb0dSGavin Atkinson 	/* Corega CG-USBRS232R */
298082dbb0dSGavin Atkinson 	UPLCOM_DEV(COREGA, CGUSBRS232R, 0, 0xFFFF, PL2303X),
29902ac6454SAndrew Thompson };
3009e6b5313SAndrew Thompson #undef UPLCOM_DEV
30102ac6454SAndrew Thompson 
30202ac6454SAndrew Thompson static device_method_t uplcom_methods[] = {
30302ac6454SAndrew Thompson 	DEVMETHOD(device_probe, uplcom_probe),
30402ac6454SAndrew Thompson 	DEVMETHOD(device_attach, uplcom_attach),
30502ac6454SAndrew Thompson 	DEVMETHOD(device_detach, uplcom_detach),
30602ac6454SAndrew Thompson 	{0, 0}
30702ac6454SAndrew Thompson };
30802ac6454SAndrew Thompson 
30902ac6454SAndrew Thompson static devclass_t uplcom_devclass;
31002ac6454SAndrew Thompson 
31102ac6454SAndrew Thompson static driver_t uplcom_driver = {
31202ac6454SAndrew Thompson 	.name = "uplcom",
31302ac6454SAndrew Thompson 	.methods = uplcom_methods,
31402ac6454SAndrew Thompson 	.size = sizeof(struct uplcom_softc),
31502ac6454SAndrew Thompson };
31602ac6454SAndrew Thompson 
3179aef556dSAndrew Thompson DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0);
31802ac6454SAndrew Thompson MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
31902ac6454SAndrew Thompson MODULE_DEPEND(uplcom, usb, 1, 1, 1);
32002ac6454SAndrew Thompson MODULE_VERSION(uplcom, UPLCOM_MODVER);
32102ac6454SAndrew Thompson 
32202ac6454SAndrew Thompson static int
32302ac6454SAndrew Thompson uplcom_probe(device_t dev)
32402ac6454SAndrew Thompson {
325760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
32602ac6454SAndrew Thompson 
32702ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
32802ac6454SAndrew Thompson 
329f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
33002ac6454SAndrew Thompson 		return (ENXIO);
33102ac6454SAndrew Thompson 	}
33202ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
33302ac6454SAndrew Thompson 		return (ENXIO);
33402ac6454SAndrew Thompson 	}
33502ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
33602ac6454SAndrew Thompson 		return (ENXIO);
33702ac6454SAndrew Thompson 	}
338a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
33902ac6454SAndrew Thompson }
34002ac6454SAndrew Thompson 
34102ac6454SAndrew Thompson static int
34202ac6454SAndrew Thompson uplcom_attach(device_t dev)
34302ac6454SAndrew Thompson {
344760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
34502ac6454SAndrew Thompson 	struct uplcom_softc *sc = device_get_softc(dev);
346760bc48eSAndrew Thompson 	struct usb_interface *iface;
347760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
34802ac6454SAndrew Thompson 	int error;
34902ac6454SAndrew Thompson 
35002ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
35102ac6454SAndrew Thompson 
352a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
353deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
35402ac6454SAndrew Thompson 
35502ac6454SAndrew Thompson 	DPRINTF("sc = %p\n", sc);
35602ac6454SAndrew Thompson 
35702ac6454SAndrew Thompson 	sc->sc_chiptype = USB_GET_DRIVER_INFO(uaa);
35802ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
35902ac6454SAndrew Thompson 
36002ac6454SAndrew Thompson 	DPRINTF("chiptype: %s\n",
36102ac6454SAndrew Thompson 	    (sc->sc_chiptype == TYPE_PL2303X) ?
36202ac6454SAndrew Thompson 	    "2303X" : "2303");
36302ac6454SAndrew Thompson 
36402ac6454SAndrew Thompson 	/*
36502ac6454SAndrew Thompson 	 * USB-RSAQ1 has two interface
36602ac6454SAndrew Thompson 	 *
36702ac6454SAndrew Thompson 	 *  USB-RSAQ1       | USB-RSAQ2
36802ac6454SAndrew Thompson 	 * -----------------+-----------------
36902ac6454SAndrew Thompson 	 * Interface 0      |Interface 0
37002ac6454SAndrew Thompson 	 *  Interrupt(0x81) | Interrupt(0x81)
37102ac6454SAndrew Thompson 	 * -----------------+ BulkIN(0x02)
37202ac6454SAndrew Thompson 	 * Interface 1	    | BulkOUT(0x83)
37302ac6454SAndrew Thompson 	 *   BulkIN(0x02)   |
37402ac6454SAndrew Thompson 	 *   BulkOUT(0x83)  |
37502ac6454SAndrew Thompson 	 */
37602ac6454SAndrew Thompson 
37702ac6454SAndrew Thompson 	sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
37802ac6454SAndrew Thompson 	sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
37902ac6454SAndrew Thompson 
380a593f6b8SAndrew Thompson 	iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
38102ac6454SAndrew Thompson 	if (iface) {
382a593f6b8SAndrew Thompson 		id = usbd_get_interface_descriptor(iface);
38302ac6454SAndrew Thompson 		if (id == NULL) {
384767cb2e2SAndrew Thompson 			device_printf(dev, "no interface descriptor (2)\n");
38502ac6454SAndrew Thompson 			goto detach;
38602ac6454SAndrew Thompson 		}
38702ac6454SAndrew Thompson 		sc->sc_data_iface_no = id->bInterfaceNumber;
38802ac6454SAndrew Thompson 		sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
389a593f6b8SAndrew Thompson 		usbd_set_parent_iface(uaa->device,
39002ac6454SAndrew Thompson 		    UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
39102ac6454SAndrew Thompson 	} else {
39202ac6454SAndrew Thompson 		sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
39302ac6454SAndrew Thompson 		sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
39402ac6454SAndrew Thompson 	}
39502ac6454SAndrew Thompson 
396a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device,
39702ac6454SAndrew Thompson 	    sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
398deefe583SAndrew Thompson 	    UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
39902ac6454SAndrew Thompson 	if (error) {
40002ac6454SAndrew Thompson 		DPRINTF("one or more missing USB endpoints, "
401a593f6b8SAndrew Thompson 		    "error=%s\n", usbd_errstr(error));
40202ac6454SAndrew Thompson 		goto detach;
40302ac6454SAndrew Thompson 	}
40402ac6454SAndrew Thompson 	error = uplcom_reset(sc, uaa->device);
40502ac6454SAndrew Thompson 	if (error) {
40602ac6454SAndrew Thompson 		device_printf(dev, "reset failed, error=%s\n",
407a593f6b8SAndrew Thompson 		    usbd_errstr(error));
40802ac6454SAndrew Thompson 		goto detach;
40902ac6454SAndrew Thompson 	}
41002ac6454SAndrew Thompson 	/* clear stall at first run */
411deefe583SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
412ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
413ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
414deefe583SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
41502ac6454SAndrew Thompson 
416a593f6b8SAndrew Thompson 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
417deefe583SAndrew Thompson 	    &uplcom_callback, &sc->sc_mtx);
41802ac6454SAndrew Thompson 	if (error) {
41902ac6454SAndrew Thompson 		goto detach;
42002ac6454SAndrew Thompson 	}
42102ac6454SAndrew Thompson 	/*
42202ac6454SAndrew Thompson 	 * do the initialization during attach so that the system does not
42302ac6454SAndrew Thompson 	 * sleep during open:
42402ac6454SAndrew Thompson 	 */
42502ac6454SAndrew Thompson 	if (sc->sc_chiptype == TYPE_PL2303X) {
42602ac6454SAndrew Thompson 		if (uplcom_pl2303x_init(uaa->device)) {
427767cb2e2SAndrew Thompson 			device_printf(dev, "init failed\n");
42802ac6454SAndrew Thompson 			goto detach;
42902ac6454SAndrew Thompson 		}
43002ac6454SAndrew Thompson 	}
43102ac6454SAndrew Thompson 	return (0);
43202ac6454SAndrew Thompson 
43302ac6454SAndrew Thompson detach:
43402ac6454SAndrew Thompson 	uplcom_detach(dev);
43502ac6454SAndrew Thompson 	return (ENXIO);
43602ac6454SAndrew Thompson }
43702ac6454SAndrew Thompson 
43802ac6454SAndrew Thompson static int
43902ac6454SAndrew Thompson uplcom_detach(device_t dev)
44002ac6454SAndrew Thompson {
44102ac6454SAndrew Thompson 	struct uplcom_softc *sc = device_get_softc(dev);
44202ac6454SAndrew Thompson 
44302ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
44402ac6454SAndrew Thompson 
445a593f6b8SAndrew Thompson 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
446a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
447deefe583SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
44802ac6454SAndrew Thompson 
44902ac6454SAndrew Thompson 	return (0);
45002ac6454SAndrew Thompson }
45102ac6454SAndrew Thompson 
452e0a69b51SAndrew Thompson static usb_error_t
453760bc48eSAndrew Thompson uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
45402ac6454SAndrew Thompson {
455760bc48eSAndrew Thompson 	struct usb_device_request req;
45602ac6454SAndrew Thompson 
45702ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
45802ac6454SAndrew Thompson 	req.bRequest = UPLCOM_SET_REQUEST;
45902ac6454SAndrew Thompson 	USETW(req.wValue, 0);
46002ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
46102ac6454SAndrew Thompson 	req.wIndex[1] = 0;
46202ac6454SAndrew Thompson 	USETW(req.wLength, 0);
46302ac6454SAndrew Thompson 
464a593f6b8SAndrew Thompson 	return (usbd_do_request(udev, NULL, &req, NULL));
46502ac6454SAndrew Thompson }
46602ac6454SAndrew Thompson 
46702ac6454SAndrew Thompson struct pl2303x_init {
46802ac6454SAndrew Thompson 	uint8_t	req_type;
46902ac6454SAndrew Thompson 	uint8_t	request;
47002ac6454SAndrew Thompson 	uint16_t value;
47102ac6454SAndrew Thompson 	uint16_t index;
47202ac6454SAndrew Thompson 	uint16_t length;
47302ac6454SAndrew Thompson };
47402ac6454SAndrew Thompson 
47502ac6454SAndrew Thompson static const struct pl2303x_init pl2303x[] = {
47602ac6454SAndrew Thompson 	{UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
47702ac6454SAndrew Thompson 	{UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0},
47802ac6454SAndrew Thompson 	{UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
47902ac6454SAndrew Thompson 	{UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1},
48002ac6454SAndrew Thompson 	{UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
48102ac6454SAndrew Thompson 	{UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0},
48202ac6454SAndrew Thompson 	{UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
48302ac6454SAndrew Thompson 	{UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1},
48402ac6454SAndrew Thompson 	{UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0},
48502ac6454SAndrew Thompson 	{UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0},
48602ac6454SAndrew Thompson 	{UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0},
48702ac6454SAndrew Thompson 	{UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 8, 0, 0},
48802ac6454SAndrew Thompson 	{UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 9, 0, 0},
48902ac6454SAndrew Thompson };
49002ac6454SAndrew Thompson 
49102ac6454SAndrew Thompson #define	N_PL2302X_INIT	(sizeof(pl2303x)/sizeof(pl2303x[0]))
49202ac6454SAndrew Thompson 
49302ac6454SAndrew Thompson static int
494760bc48eSAndrew Thompson uplcom_pl2303x_init(struct usb_device *udev)
49502ac6454SAndrew Thompson {
496760bc48eSAndrew Thompson 	struct usb_device_request req;
497e0a69b51SAndrew Thompson 	usb_error_t err;
49802ac6454SAndrew Thompson 	uint8_t buf[4];
49902ac6454SAndrew Thompson 	uint8_t i;
50002ac6454SAndrew Thompson 
50102ac6454SAndrew Thompson 	for (i = 0; i != N_PL2302X_INIT; i++) {
50202ac6454SAndrew Thompson 		req.bmRequestType = pl2303x[i].req_type;
50302ac6454SAndrew Thompson 		req.bRequest = pl2303x[i].request;
50402ac6454SAndrew Thompson 		USETW(req.wValue, pl2303x[i].value);
50502ac6454SAndrew Thompson 		USETW(req.wIndex, pl2303x[i].index);
50602ac6454SAndrew Thompson 		USETW(req.wLength, pl2303x[i].length);
50702ac6454SAndrew Thompson 
508a593f6b8SAndrew Thompson 		err = usbd_do_request(udev, NULL, &req, buf);
50902ac6454SAndrew Thompson 		if (err) {
510a593f6b8SAndrew Thompson 			DPRINTF("error=%s\n", usbd_errstr(err));
51102ac6454SAndrew Thompson 			return (EIO);
51202ac6454SAndrew Thompson 		}
51302ac6454SAndrew Thompson 	}
51402ac6454SAndrew Thompson 	return (0);
51502ac6454SAndrew Thompson }
51602ac6454SAndrew Thompson 
51702ac6454SAndrew Thompson static void
518760bc48eSAndrew Thompson uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
51902ac6454SAndrew Thompson {
52002ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
521760bc48eSAndrew Thompson 	struct usb_device_request req;
52202ac6454SAndrew Thompson 
52302ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
52402ac6454SAndrew Thompson 
52502ac6454SAndrew Thompson 	if (onoff)
52602ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_DTR;
52702ac6454SAndrew Thompson 	else
52802ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_DTR;
52902ac6454SAndrew Thompson 
53002ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
53102ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
53202ac6454SAndrew Thompson 	USETW(req.wValue, sc->sc_line);
53302ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
53402ac6454SAndrew Thompson 	req.wIndex[1] = 0;
53502ac6454SAndrew Thompson 	USETW(req.wLength, 0);
53602ac6454SAndrew Thompson 
537a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
53802ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
53902ac6454SAndrew Thompson }
54002ac6454SAndrew Thompson 
54102ac6454SAndrew Thompson static void
542760bc48eSAndrew Thompson uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
54302ac6454SAndrew Thompson {
54402ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
545760bc48eSAndrew Thompson 	struct usb_device_request req;
54602ac6454SAndrew Thompson 
54702ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
54802ac6454SAndrew Thompson 
54902ac6454SAndrew Thompson 	if (onoff)
55002ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_RTS;
55102ac6454SAndrew Thompson 	else
55202ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_RTS;
55302ac6454SAndrew Thompson 
55402ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
55502ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
55602ac6454SAndrew Thompson 	USETW(req.wValue, sc->sc_line);
55702ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
55802ac6454SAndrew Thompson 	req.wIndex[1] = 0;
55902ac6454SAndrew Thompson 	USETW(req.wLength, 0);
56002ac6454SAndrew Thompson 
561a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
56202ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
56302ac6454SAndrew Thompson }
56402ac6454SAndrew Thompson 
56502ac6454SAndrew Thompson static void
566760bc48eSAndrew Thompson uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
56702ac6454SAndrew Thompson {
56802ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
569760bc48eSAndrew Thompson 	struct usb_device_request req;
57002ac6454SAndrew Thompson 	uint16_t temp;
57102ac6454SAndrew Thompson 
57202ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
57302ac6454SAndrew Thompson 
57402ac6454SAndrew Thompson 	temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
57502ac6454SAndrew Thompson 
57602ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
57702ac6454SAndrew Thompson 	req.bRequest = UCDC_SEND_BREAK;
57802ac6454SAndrew Thompson 	USETW(req.wValue, temp);
57902ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
58002ac6454SAndrew Thompson 	req.wIndex[1] = 0;
58102ac6454SAndrew Thompson 	USETW(req.wLength, 0);
58202ac6454SAndrew Thompson 
583a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
58402ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
58502ac6454SAndrew Thompson }
58602ac6454SAndrew Thompson 
58702ac6454SAndrew Thompson static const int32_t uplcom_rates[] = {
58802ac6454SAndrew Thompson 	75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
58902ac6454SAndrew Thompson 	19200, 28800, 38400, 57600, 115200,
59002ac6454SAndrew Thompson 	/*
59102ac6454SAndrew Thompson 	 * Higher speeds are probably possible. PL2303X supports up to
59202ac6454SAndrew Thompson 	 * 6Mb and can set any rate
59302ac6454SAndrew Thompson 	 */
59402ac6454SAndrew Thompson 	230400, 460800, 614400, 921600, 1228800
59502ac6454SAndrew Thompson };
59602ac6454SAndrew Thompson 
59702ac6454SAndrew Thompson #define	N_UPLCOM_RATES	(sizeof(uplcom_rates)/sizeof(uplcom_rates[0]))
59802ac6454SAndrew Thompson 
59902ac6454SAndrew Thompson static int
600760bc48eSAndrew Thompson uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
60102ac6454SAndrew Thompson {
602*fe0f6362SGavin Atkinson 	struct uplcom_softc *sc = ucom->sc_parent;
60302ac6454SAndrew Thompson 	uint8_t i;
60402ac6454SAndrew Thompson 
60502ac6454SAndrew Thompson 	DPRINTF("\n");
60602ac6454SAndrew Thompson 
607*fe0f6362SGavin Atkinson 	/**
608*fe0f6362SGavin Atkinson 	 * Check requested baud rate.
609*fe0f6362SGavin Atkinson 	 *
610*fe0f6362SGavin Atkinson 	 * The PL2303 can only set specific baud rates, up to 1228800 baud.
611*fe0f6362SGavin Atkinson 	 * The PL2303X can set any baud rate up to 6Mb.
612*fe0f6362SGavin Atkinson 	 * The PL2303HX rev. D can set any baud rate up to 12Mb.
613*fe0f6362SGavin Atkinson 	 *
614*fe0f6362SGavin Atkinson 	 * XXX: We currently cannot identify the PL2303HX rev. D, so treat
615*fe0f6362SGavin Atkinson 	 *      it the same as the PL2303X.
616*fe0f6362SGavin Atkinson 	 */
617*fe0f6362SGavin Atkinson 	if (sc->sc_chiptype == TYPE_PL2303) {
618*fe0f6362SGavin Atkinson 		for (i = 0; i < N_UPLCOM_RATES; i++) {
619*fe0f6362SGavin Atkinson 			if (uplcom_rates[i] == t->c_ospeed)
620*fe0f6362SGavin Atkinson 				return (0);
62102ac6454SAndrew Thompson 		}
62202ac6454SAndrew Thompson  	} else {
623*fe0f6362SGavin Atkinson 		if (t->c_ospeed <= 6000000)
624*fe0f6362SGavin Atkinson 			return (0);
62502ac6454SAndrew Thompson 	}
62602ac6454SAndrew Thompson 
627*fe0f6362SGavin Atkinson 	DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed);
628*fe0f6362SGavin Atkinson 	return (EIO);
62902ac6454SAndrew Thompson }
63002ac6454SAndrew Thompson 
63102ac6454SAndrew Thompson static void
632760bc48eSAndrew Thompson uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
63302ac6454SAndrew Thompson {
63402ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
635760bc48eSAndrew Thompson 	struct usb_cdc_line_state ls;
636760bc48eSAndrew Thompson 	struct usb_device_request req;
63702ac6454SAndrew Thompson 
63802ac6454SAndrew Thompson 	DPRINTF("sc = %p\n", sc);
63902ac6454SAndrew Thompson 
64002ac6454SAndrew Thompson 	bzero(&ls, sizeof(ls));
64102ac6454SAndrew Thompson 
64202ac6454SAndrew Thompson 	USETDW(ls.dwDTERate, t->c_ospeed);
64302ac6454SAndrew Thompson 
64402ac6454SAndrew Thompson 	if (t->c_cflag & CSTOPB) {
64502ac6454SAndrew Thompson 		ls.bCharFormat = UCDC_STOP_BIT_2;
64602ac6454SAndrew Thompson 	} else {
64702ac6454SAndrew Thompson 		ls.bCharFormat = UCDC_STOP_BIT_1;
64802ac6454SAndrew Thompson 	}
64902ac6454SAndrew Thompson 
65002ac6454SAndrew Thompson 	if (t->c_cflag & PARENB) {
65102ac6454SAndrew Thompson 		if (t->c_cflag & PARODD) {
65202ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_ODD;
65302ac6454SAndrew Thompson 		} else {
65402ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_EVEN;
65502ac6454SAndrew Thompson 		}
65602ac6454SAndrew Thompson 	} else {
65702ac6454SAndrew Thompson 		ls.bParityType = UCDC_PARITY_NONE;
65802ac6454SAndrew Thompson 	}
65902ac6454SAndrew Thompson 
66002ac6454SAndrew Thompson 	switch (t->c_cflag & CSIZE) {
66102ac6454SAndrew Thompson 	case CS5:
66202ac6454SAndrew Thompson 		ls.bDataBits = 5;
66302ac6454SAndrew Thompson 		break;
66402ac6454SAndrew Thompson 	case CS6:
66502ac6454SAndrew Thompson 		ls.bDataBits = 6;
66602ac6454SAndrew Thompson 		break;
66702ac6454SAndrew Thompson 	case CS7:
66802ac6454SAndrew Thompson 		ls.bDataBits = 7;
66902ac6454SAndrew Thompson 		break;
67002ac6454SAndrew Thompson 	case CS8:
67102ac6454SAndrew Thompson 		ls.bDataBits = 8;
67202ac6454SAndrew Thompson 		break;
67302ac6454SAndrew Thompson 	}
67402ac6454SAndrew Thompson 
67502ac6454SAndrew Thompson 	DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
67602ac6454SAndrew Thompson 	    UGETDW(ls.dwDTERate), ls.bCharFormat,
67702ac6454SAndrew Thompson 	    ls.bParityType, ls.bDataBits);
67802ac6454SAndrew Thompson 
67902ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
68002ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_LINE_CODING;
68102ac6454SAndrew Thompson 	USETW(req.wValue, 0);
68202ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
68302ac6454SAndrew Thompson 	req.wIndex[1] = 0;
68402ac6454SAndrew Thompson 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
68502ac6454SAndrew Thompson 
686a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
68702ac6454SAndrew Thompson 	    &req, &ls, 0, 1000);
68802ac6454SAndrew Thompson 
68902ac6454SAndrew Thompson 	if (t->c_cflag & CRTSCTS) {
69002ac6454SAndrew Thompson 
69102ac6454SAndrew Thompson 		DPRINTF("crtscts = on\n");
69202ac6454SAndrew Thompson 
69302ac6454SAndrew Thompson 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
69402ac6454SAndrew Thompson 		req.bRequest = UPLCOM_SET_REQUEST;
69502ac6454SAndrew Thompson 		USETW(req.wValue, 0);
69602ac6454SAndrew Thompson 		if (sc->sc_chiptype == TYPE_PL2303X)
69702ac6454SAndrew Thompson 			USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
69802ac6454SAndrew Thompson 		else
69902ac6454SAndrew Thompson 			USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
70002ac6454SAndrew Thompson 		USETW(req.wLength, 0);
70102ac6454SAndrew Thompson 
702a593f6b8SAndrew Thompson 		ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
70302ac6454SAndrew Thompson 		    &req, NULL, 0, 1000);
70402ac6454SAndrew Thompson 	} else {
70502ac6454SAndrew Thompson 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
70602ac6454SAndrew Thompson 		req.bRequest = UPLCOM_SET_REQUEST;
70702ac6454SAndrew Thompson 		USETW(req.wValue, 0);
70802ac6454SAndrew Thompson 		USETW(req.wIndex, 0);
70902ac6454SAndrew Thompson 		USETW(req.wLength, 0);
710a593f6b8SAndrew Thompson 		ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
71102ac6454SAndrew Thompson 		    &req, NULL, 0, 1000);
71202ac6454SAndrew Thompson 	}
71302ac6454SAndrew Thompson }
71402ac6454SAndrew Thompson 
71502ac6454SAndrew Thompson static void
716760bc48eSAndrew Thompson uplcom_start_read(struct ucom_softc *ucom)
71702ac6454SAndrew Thompson {
71802ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
71902ac6454SAndrew Thompson 
72002ac6454SAndrew Thompson 	/* start interrupt endpoint */
721a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
72202ac6454SAndrew Thompson 
72302ac6454SAndrew Thompson 	/* start read endpoint */
724a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
72502ac6454SAndrew Thompson }
72602ac6454SAndrew Thompson 
72702ac6454SAndrew Thompson static void
728760bc48eSAndrew Thompson uplcom_stop_read(struct ucom_softc *ucom)
72902ac6454SAndrew Thompson {
73002ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
73102ac6454SAndrew Thompson 
73202ac6454SAndrew Thompson 	/* stop interrupt endpoint */
733a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
73402ac6454SAndrew Thompson 
73502ac6454SAndrew Thompson 	/* stop read endpoint */
736a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
73702ac6454SAndrew Thompson }
73802ac6454SAndrew Thompson 
73902ac6454SAndrew Thompson static void
740760bc48eSAndrew Thompson uplcom_start_write(struct ucom_softc *ucom)
74102ac6454SAndrew Thompson {
74202ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
74302ac6454SAndrew Thompson 
744a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
74502ac6454SAndrew Thompson }
74602ac6454SAndrew Thompson 
74702ac6454SAndrew Thompson static void
748760bc48eSAndrew Thompson uplcom_stop_write(struct ucom_softc *ucom)
74902ac6454SAndrew Thompson {
75002ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
75102ac6454SAndrew Thompson 
752a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
75302ac6454SAndrew Thompson }
75402ac6454SAndrew Thompson 
75502ac6454SAndrew Thompson static void
756760bc48eSAndrew Thompson uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
75702ac6454SAndrew Thompson {
75802ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
75902ac6454SAndrew Thompson 
76002ac6454SAndrew Thompson 	DPRINTF("\n");
76102ac6454SAndrew Thompson 
76202ac6454SAndrew Thompson 	*lsr = sc->sc_lsr;
76302ac6454SAndrew Thompson 	*msr = sc->sc_msr;
76402ac6454SAndrew Thompson }
76502ac6454SAndrew Thompson 
76602ac6454SAndrew Thompson static void
767ed6d949aSAndrew Thompson uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
76802ac6454SAndrew Thompson {
769ed6d949aSAndrew Thompson 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
770ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
77102ac6454SAndrew Thompson 	uint8_t buf[9];
772ed6d949aSAndrew Thompson 	int actlen;
773ed6d949aSAndrew Thompson 
774ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
77502ac6454SAndrew Thompson 
77602ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
77702ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
77802ac6454SAndrew Thompson 
779ed6d949aSAndrew Thompson 		DPRINTF("actlen = %u\n", actlen);
78002ac6454SAndrew Thompson 
781ed6d949aSAndrew Thompson 		if (actlen >= 9) {
78202ac6454SAndrew Thompson 
783ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
784ed6d949aSAndrew Thompson 			usbd_copy_out(pc, 0, buf, sizeof(buf));
78502ac6454SAndrew Thompson 
78602ac6454SAndrew Thompson 			DPRINTF("status = 0x%02x\n", buf[8]);
78702ac6454SAndrew Thompson 
78802ac6454SAndrew Thompson 			sc->sc_lsr = 0;
78902ac6454SAndrew Thompson 			sc->sc_msr = 0;
79002ac6454SAndrew Thompson 
79102ac6454SAndrew Thompson 			if (buf[8] & RSAQ_STATUS_CTS) {
79202ac6454SAndrew Thompson 				sc->sc_msr |= SER_CTS;
79302ac6454SAndrew Thompson 			}
79402ac6454SAndrew Thompson 			if (buf[8] & RSAQ_STATUS_DSR) {
79502ac6454SAndrew Thompson 				sc->sc_msr |= SER_DSR;
79602ac6454SAndrew Thompson 			}
79702ac6454SAndrew Thompson 			if (buf[8] & RSAQ_STATUS_DCD) {
79802ac6454SAndrew Thompson 				sc->sc_msr |= SER_DCD;
79902ac6454SAndrew Thompson 			}
800a593f6b8SAndrew Thompson 			ucom_status_change(&sc->sc_ucom);
80102ac6454SAndrew Thompson 		}
80202ac6454SAndrew Thompson 	case USB_ST_SETUP:
80302ac6454SAndrew Thompson tr_setup:
804ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
805a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
80602ac6454SAndrew Thompson 		return;
80702ac6454SAndrew Thompson 
80802ac6454SAndrew Thompson 	default:			/* Error */
809ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
81002ac6454SAndrew Thompson 			/* try to clear stall first */
811ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
81202ac6454SAndrew Thompson 			goto tr_setup;
81302ac6454SAndrew Thompson 		}
81402ac6454SAndrew Thompson 		return;
81502ac6454SAndrew Thompson 	}
81602ac6454SAndrew Thompson }
81702ac6454SAndrew Thompson 
81802ac6454SAndrew Thompson static void
819ed6d949aSAndrew Thompson uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
82002ac6454SAndrew Thompson {
821ed6d949aSAndrew Thompson 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
822ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
82302ac6454SAndrew Thompson 	uint32_t actlen;
82402ac6454SAndrew Thompson 
82502ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
82602ac6454SAndrew Thompson 	case USB_ST_SETUP:
82702ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
82802ac6454SAndrew Thompson tr_setup:
829ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
830ed6d949aSAndrew Thompson 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
83102ac6454SAndrew Thompson 		    UPLCOM_BULK_BUF_SIZE, &actlen)) {
83202ac6454SAndrew Thompson 
83302ac6454SAndrew Thompson 			DPRINTF("actlen = %d\n", actlen);
83402ac6454SAndrew Thompson 
835ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, actlen);
836a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
83702ac6454SAndrew Thompson 		}
83802ac6454SAndrew Thompson 		return;
83902ac6454SAndrew Thompson 
84002ac6454SAndrew Thompson 	default:			/* Error */
841ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
84202ac6454SAndrew Thompson 			/* try to clear stall first */
843ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
84402ac6454SAndrew Thompson 			goto tr_setup;
84502ac6454SAndrew Thompson 		}
84602ac6454SAndrew Thompson 		return;
84702ac6454SAndrew Thompson 	}
84802ac6454SAndrew Thompson }
84902ac6454SAndrew Thompson 
85002ac6454SAndrew Thompson static void
851ed6d949aSAndrew Thompson uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
85202ac6454SAndrew Thompson {
853ed6d949aSAndrew Thompson 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
854ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
855ed6d949aSAndrew Thompson 	int actlen;
856ed6d949aSAndrew Thompson 
857ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
85802ac6454SAndrew Thompson 
85902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
86002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
861ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
862ed6d949aSAndrew Thompson 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
86302ac6454SAndrew Thompson 
86402ac6454SAndrew Thompson 	case USB_ST_SETUP:
86502ac6454SAndrew Thompson tr_setup:
866ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
867a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
86802ac6454SAndrew Thompson 		return;
86902ac6454SAndrew Thompson 
87002ac6454SAndrew Thompson 	default:			/* Error */
871ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
87202ac6454SAndrew Thompson 			/* try to clear stall first */
873ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
87402ac6454SAndrew Thompson 			goto tr_setup;
87502ac6454SAndrew Thompson 		}
87602ac6454SAndrew Thompson 		return;
87702ac6454SAndrew Thompson 	}
87802ac6454SAndrew Thompson }
879655dc9d0SAndrew Thompson 
880655dc9d0SAndrew Thompson static void
881655dc9d0SAndrew Thompson uplcom_poll(struct ucom_softc *ucom)
882655dc9d0SAndrew Thompson {
883655dc9d0SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
884655dc9d0SAndrew Thompson 	usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);
885655dc9d0SAndrew Thompson }
886