xref: /freebsd/sys/dev/usb/serial/uplcom.c (revision f29a072444d6e31d57f7edd94f952395ae7bb22e)
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 
8702ac6454SAndrew Thompson #include "usbdevs.h"
8802ac6454SAndrew Thompson #include <dev/usb/usb.h>
8902ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
9002ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
9102ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
9202ac6454SAndrew Thompson 
9302ac6454SAndrew Thompson #define	USB_DEBUG_VAR uplcom_debug
9402ac6454SAndrew Thompson 
9502ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
9602ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
9702ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
9802ac6454SAndrew Thompson #include <dev/usb/usb_request.h>
9902ac6454SAndrew Thompson #include <dev/usb/usb_lookup.h>
10002ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
10102ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
10202ac6454SAndrew Thompson 
10302ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
10402ac6454SAndrew Thompson 
10502ac6454SAndrew Thompson #if USB_DEBUG
10602ac6454SAndrew Thompson static int uplcom_debug = 0;
10702ac6454SAndrew Thompson 
10802ac6454SAndrew Thompson SYSCTL_NODE(_hw_usb2, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom");
10902ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2_uplcom, OID_AUTO, debug, CTLFLAG_RW,
11002ac6454SAndrew Thompson     &uplcom_debug, 0, "Debug level");
11102ac6454SAndrew Thompson #endif
11202ac6454SAndrew Thompson 
11302ac6454SAndrew Thompson #define	UPLCOM_MODVER			1	/* module version */
11402ac6454SAndrew Thompson 
11502ac6454SAndrew Thompson #define	UPLCOM_CONFIG_INDEX		0
11602ac6454SAndrew Thompson #define	UPLCOM_IFACE_INDEX		0
11702ac6454SAndrew Thompson #define	UPLCOM_SECOND_IFACE_INDEX	1
11802ac6454SAndrew Thompson 
11902ac6454SAndrew Thompson #ifndef UPLCOM_INTR_INTERVAL
12002ac6454SAndrew Thompson #define	UPLCOM_INTR_INTERVAL		0	/* default */
12102ac6454SAndrew Thompson #endif
12202ac6454SAndrew Thompson 
12302ac6454SAndrew Thompson #define	UPLCOM_BULK_BUF_SIZE 1024	/* bytes */
12402ac6454SAndrew Thompson 
12502ac6454SAndrew Thompson #define	UPLCOM_SET_REQUEST		0x01
12602ac6454SAndrew Thompson #define	UPLCOM_SET_CRTSCTS		0x41
12702ac6454SAndrew Thompson #define	UPLCOM_SET_CRTSCTS_PL2303X	0x61
12802ac6454SAndrew Thompson #define	RSAQ_STATUS_CTS			0x80
12902ac6454SAndrew Thompson #define	RSAQ_STATUS_DSR			0x02
13002ac6454SAndrew Thompson #define	RSAQ_STATUS_DCD			0x01
13102ac6454SAndrew Thompson 
13202ac6454SAndrew Thompson #define	TYPE_PL2303			0
13302ac6454SAndrew Thompson #define	TYPE_PL2303X			1
13402ac6454SAndrew Thompson 
13502ac6454SAndrew Thompson enum {
13602ac6454SAndrew Thompson 	UPLCOM_BULK_DT_WR,
13702ac6454SAndrew Thompson 	UPLCOM_BULK_DT_RD,
13802ac6454SAndrew Thompson 	UPLCOM_INTR_DT_RD,
13902ac6454SAndrew Thompson 	UPLCOM_N_TRANSFER,
14002ac6454SAndrew Thompson };
14102ac6454SAndrew Thompson 
14202ac6454SAndrew Thompson struct uplcom_softc {
14302ac6454SAndrew Thompson 	struct usb2_com_super_softc sc_super_ucom;
14402ac6454SAndrew Thompson 	struct usb2_com_softc sc_ucom;
14502ac6454SAndrew Thompson 
14602ac6454SAndrew Thompson 	struct usb2_xfer *sc_xfer[UPLCOM_N_TRANSFER];
14702ac6454SAndrew Thompson 	struct usb2_device *sc_udev;
148deefe583SAndrew Thompson 	struct mtx sc_mtx;
14902ac6454SAndrew Thompson 
15002ac6454SAndrew Thompson 	uint16_t sc_line;
15102ac6454SAndrew Thompson 
15202ac6454SAndrew Thompson 	uint8_t	sc_lsr;			/* local status register */
15302ac6454SAndrew Thompson 	uint8_t	sc_msr;			/* uplcom status register */
15402ac6454SAndrew Thompson 	uint8_t	sc_chiptype;		/* type of chip */
15502ac6454SAndrew Thompson 	uint8_t	sc_ctrl_iface_no;
15602ac6454SAndrew Thompson 	uint8_t	sc_data_iface_no;
15702ac6454SAndrew Thompson 	uint8_t	sc_iface_index[2];
15802ac6454SAndrew Thompson };
15902ac6454SAndrew Thompson 
16002ac6454SAndrew Thompson /* prototypes */
16102ac6454SAndrew Thompson 
16202ac6454SAndrew Thompson static usb2_error_t uplcom_reset(struct uplcom_softc *, struct usb2_device *);
16302ac6454SAndrew Thompson static int	uplcom_pl2303x_init(struct usb2_device *);
16402ac6454SAndrew Thompson static void	uplcom_cfg_set_dtr(struct usb2_com_softc *, uint8_t);
16502ac6454SAndrew Thompson static void	uplcom_cfg_set_rts(struct usb2_com_softc *, uint8_t);
16602ac6454SAndrew Thompson static void	uplcom_cfg_set_break(struct usb2_com_softc *, uint8_t);
16702ac6454SAndrew Thompson static int	uplcom_pre_param(struct usb2_com_softc *, struct termios *);
16802ac6454SAndrew Thompson static void	uplcom_cfg_param(struct usb2_com_softc *, struct termios *);
16902ac6454SAndrew Thompson static void	uplcom_start_read(struct usb2_com_softc *);
17002ac6454SAndrew Thompson static void	uplcom_stop_read(struct usb2_com_softc *);
17102ac6454SAndrew Thompson static void	uplcom_start_write(struct usb2_com_softc *);
17202ac6454SAndrew Thompson static void	uplcom_stop_write(struct usb2_com_softc *);
17302ac6454SAndrew Thompson static void	uplcom_cfg_get_status(struct usb2_com_softc *, uint8_t *,
17402ac6454SAndrew Thompson 		    uint8_t *);
17502ac6454SAndrew Thompson 
17602ac6454SAndrew Thompson static device_probe_t uplcom_probe;
17702ac6454SAndrew Thompson static device_attach_t uplcom_attach;
17802ac6454SAndrew Thompson static device_detach_t uplcom_detach;
17902ac6454SAndrew Thompson 
18002ac6454SAndrew Thompson static usb2_callback_t uplcom_intr_callback;
18102ac6454SAndrew Thompson static usb2_callback_t uplcom_write_callback;
18202ac6454SAndrew Thompson static usb2_callback_t uplcom_read_callback;
18302ac6454SAndrew Thompson 
18402ac6454SAndrew Thompson static const struct usb2_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
18502ac6454SAndrew Thompson 
18602ac6454SAndrew Thompson 	[UPLCOM_BULK_DT_WR] = {
18702ac6454SAndrew Thompson 		.type = UE_BULK,
18802ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
18902ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
1904eae601eSAndrew Thompson 		.bufsize = UPLCOM_BULK_BUF_SIZE,
1914eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
1924eae601eSAndrew Thompson 		.callback = &uplcom_write_callback,
19302ac6454SAndrew Thompson 		.if_index = 0,
19402ac6454SAndrew Thompson 	},
19502ac6454SAndrew Thompson 
19602ac6454SAndrew Thompson 	[UPLCOM_BULK_DT_RD] = {
19702ac6454SAndrew Thompson 		.type = UE_BULK,
19802ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
19902ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2004eae601eSAndrew Thompson 		.bufsize = UPLCOM_BULK_BUF_SIZE,
2014eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2024eae601eSAndrew Thompson 		.callback = &uplcom_read_callback,
20302ac6454SAndrew Thompson 		.if_index = 0,
20402ac6454SAndrew Thompson 	},
20502ac6454SAndrew Thompson 
20602ac6454SAndrew Thompson 	[UPLCOM_INTR_DT_RD] = {
20702ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
20802ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
20902ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2104eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2114eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
2124eae601eSAndrew Thompson 		.callback = &uplcom_intr_callback,
21302ac6454SAndrew Thompson 		.if_index = 1,
21402ac6454SAndrew Thompson 	},
21502ac6454SAndrew Thompson };
21602ac6454SAndrew Thompson 
21702ac6454SAndrew Thompson struct usb2_com_callback uplcom_callback = {
21802ac6454SAndrew Thompson 	.usb2_com_cfg_get_status = &uplcom_cfg_get_status,
21902ac6454SAndrew Thompson 	.usb2_com_cfg_set_dtr = &uplcom_cfg_set_dtr,
22002ac6454SAndrew Thompson 	.usb2_com_cfg_set_rts = &uplcom_cfg_set_rts,
22102ac6454SAndrew Thompson 	.usb2_com_cfg_set_break = &uplcom_cfg_set_break,
22202ac6454SAndrew Thompson 	.usb2_com_cfg_param = &uplcom_cfg_param,
22302ac6454SAndrew Thompson 	.usb2_com_pre_param = &uplcom_pre_param,
22402ac6454SAndrew Thompson 	.usb2_com_start_read = &uplcom_start_read,
22502ac6454SAndrew Thompson 	.usb2_com_stop_read = &uplcom_stop_read,
22602ac6454SAndrew Thompson 	.usb2_com_start_write = &uplcom_start_write,
22702ac6454SAndrew Thompson 	.usb2_com_stop_write = &uplcom_stop_write,
22802ac6454SAndrew Thompson };
22902ac6454SAndrew Thompson 
23002ac6454SAndrew Thompson #define	USB_UPL(v,p,rl,rh,t)				\
23102ac6454SAndrew Thompson   USB_VENDOR(v), USB_PRODUCT(p), USB_DEV_BCD_GTEQ(rl),	\
23202ac6454SAndrew Thompson   USB_DEV_BCD_LTEQ(rh), USB_DRIVER_INFO(t)
23302ac6454SAndrew Thompson 
23402ac6454SAndrew Thompson static const struct usb2_device_id uplcom_devs[] = {
23502ac6454SAndrew Thompson 	/* Belkin F5U257 */
23602ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U257, 0, 0xFFFF, TYPE_PL2303X)},
23702ac6454SAndrew Thompson 	/* I/O DATA USB-RSAQ */
23802ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ, 0, 0xFFFF, TYPE_PL2303)},
23902ac6454SAndrew Thompson 	/* I/O DATA USB-RSAQ2 */
24002ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2, 0, 0xFFFF, TYPE_PL2303)},
24102ac6454SAndrew Thompson 	/* I/O DATA USB-RSAQ3 */
24202ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3, 0, 0xFFFF, TYPE_PL2303X)},
24302ac6454SAndrew Thompson 	/* PLANEX USB-RS232 URS-03 */
24402ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A, 0, 0xFFFF, TYPE_PL2303)},
24502ac6454SAndrew Thompson 	/* TrendNet TU-S9 */
24602ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0x0400, 0xFFFF, TYPE_PL2303X)},
24702ac6454SAndrew Thompson 	/* ST Lab USB-SERIAL-4 */
24802ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0x0300, 0x03FF, TYPE_PL2303X)},
24902ac6454SAndrew Thompson 	/* IOGEAR/ATEN UC-232A (also ST Lab USB-SERIAL-1) */
25002ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0, 0x02FF, TYPE_PL2303)},
25102ac6454SAndrew Thompson 	/* TDK USB-PHS Adapter UHA6400 */
25202ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400, 0, 0xFFFF, TYPE_PL2303)},
25302ac6454SAndrew Thompson 	/* RATOC REX-USB60 */
25402ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60, 0, 0xFFFF, TYPE_PL2303)},
25502ac6454SAndrew Thompson 	/* ELECOM UC-SGT */
25602ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT, 0, 0xFFFF, TYPE_PL2303)},
25702ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0, 0, 0xFFFF, TYPE_PL2303)},
25802ac6454SAndrew Thompson 	/* Sagem USB-Serial Controller */
25902ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_SAGEM, USB_PRODUCT_SAGEM_USBSERIAL, 0, 0xFFFF, TYPE_PL2303X)},
26002ac6454SAndrew Thompson 	/* Sony Ericsson USB Cable */
26102ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_SONYERICSSON, USB_PRODUCT_SONYERICSSON_DCU10, 0, 0xFFFF, TYPE_PL2303)},
26202ac6454SAndrew Thompson 	/* SOURCENEXT KeikaiDenwa 8 */
26302ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8, 0, 0xFFFF, TYPE_PL2303)},
26402ac6454SAndrew Thompson 	/* SOURCENEXT KeikaiDenwa 8 with charger */
26502ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG, 0, 0, TYPE_PL2303)},
26602ac6454SAndrew Thompson 	/* HAL Corporation Crossam2+USB */
26702ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001, 0, 0xFFFF, TYPE_PL2303)},
26802ac6454SAndrew Thompson 	/* Sitecom USB to Serial */
26902ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_SERIAL, 0, 0xFFFF, TYPE_PL2303)},
27002ac6454SAndrew Thompson 	/* Tripp-Lite U209-000-R */
27102ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209, 0, 0xFFFF, TYPE_PL2303X)},
27202ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_RADIOSHACK, USB_PRODUCT_RADIOSHACK_USBCABLE, 0, 0xFFFF, TYPE_PL2303)},
27302ac6454SAndrew Thompson 	/* Prolific Pharos */
27402ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PHAROS, 0, 0xFFFF, TYPE_PL2303)},
27502ac6454SAndrew Thompson 	/* Willcom W-SIM */
27602ac6454SAndrew Thompson 	{USB_UPL(USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_WSIM, 0, 0xFFFF, TYPE_PL2303X)},
277dc4b1d16SAndrew Thompson 	/* Mobile Action MA-620 Infrared Adapter */
278dc4b1d16SAndrew Thompson 	{USB_UPL(USB_VENDOR_MOBILEACTION, USB_PRODUCT_MOBILEACTION_MA620, 0, 0xFFFF, TYPE_PL2303X)},
279dc4b1d16SAndrew Thompson 
28002ac6454SAndrew Thompson };
28102ac6454SAndrew Thompson 
28202ac6454SAndrew Thompson static device_method_t uplcom_methods[] = {
28302ac6454SAndrew Thompson 	DEVMETHOD(device_probe, uplcom_probe),
28402ac6454SAndrew Thompson 	DEVMETHOD(device_attach, uplcom_attach),
28502ac6454SAndrew Thompson 	DEVMETHOD(device_detach, uplcom_detach),
28602ac6454SAndrew Thompson 	{0, 0}
28702ac6454SAndrew Thompson };
28802ac6454SAndrew Thompson 
28902ac6454SAndrew Thompson static devclass_t uplcom_devclass;
29002ac6454SAndrew Thompson 
29102ac6454SAndrew Thompson static driver_t uplcom_driver = {
29202ac6454SAndrew Thompson 	.name = "uplcom",
29302ac6454SAndrew Thompson 	.methods = uplcom_methods,
29402ac6454SAndrew Thompson 	.size = sizeof(struct uplcom_softc),
29502ac6454SAndrew Thompson };
29602ac6454SAndrew Thompson 
2979aef556dSAndrew Thompson DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0);
29802ac6454SAndrew Thompson MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
29902ac6454SAndrew Thompson MODULE_DEPEND(uplcom, usb, 1, 1, 1);
30002ac6454SAndrew Thompson MODULE_VERSION(uplcom, UPLCOM_MODVER);
30102ac6454SAndrew Thompson 
30202ac6454SAndrew Thompson static int
30302ac6454SAndrew Thompson uplcom_probe(device_t dev)
30402ac6454SAndrew Thompson {
30502ac6454SAndrew Thompson 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
30602ac6454SAndrew Thompson 
30702ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
30802ac6454SAndrew Thompson 
309f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
31002ac6454SAndrew Thompson 		return (ENXIO);
31102ac6454SAndrew Thompson 	}
31202ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
31302ac6454SAndrew Thompson 		return (ENXIO);
31402ac6454SAndrew Thompson 	}
31502ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
31602ac6454SAndrew Thompson 		return (ENXIO);
31702ac6454SAndrew Thompson 	}
31802ac6454SAndrew Thompson 	return (usb2_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
31902ac6454SAndrew Thompson }
32002ac6454SAndrew Thompson 
32102ac6454SAndrew Thompson static int
32202ac6454SAndrew Thompson uplcom_attach(device_t dev)
32302ac6454SAndrew Thompson {
32402ac6454SAndrew Thompson 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
32502ac6454SAndrew Thompson 	struct uplcom_softc *sc = device_get_softc(dev);
32602ac6454SAndrew Thompson 	struct usb2_interface *iface;
32702ac6454SAndrew Thompson 	struct usb2_interface_descriptor *id;
32802ac6454SAndrew Thompson 	int error;
32902ac6454SAndrew Thompson 
33002ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
33102ac6454SAndrew Thompson 
33202ac6454SAndrew Thompson 	device_set_usb2_desc(dev);
333deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
33402ac6454SAndrew Thompson 
33502ac6454SAndrew Thompson 	DPRINTF("sc = %p\n", sc);
33602ac6454SAndrew Thompson 
33702ac6454SAndrew Thompson 	sc->sc_chiptype = USB_GET_DRIVER_INFO(uaa);
33802ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
33902ac6454SAndrew Thompson 
34002ac6454SAndrew Thompson 	DPRINTF("chiptype: %s\n",
34102ac6454SAndrew Thompson 	    (sc->sc_chiptype == TYPE_PL2303X) ?
34202ac6454SAndrew Thompson 	    "2303X" : "2303");
34302ac6454SAndrew Thompson 
34402ac6454SAndrew Thompson 	/*
34502ac6454SAndrew Thompson 	 * USB-RSAQ1 has two interface
34602ac6454SAndrew Thompson 	 *
34702ac6454SAndrew Thompson 	 *  USB-RSAQ1       | USB-RSAQ2
34802ac6454SAndrew Thompson 	 * -----------------+-----------------
34902ac6454SAndrew Thompson 	 * Interface 0      |Interface 0
35002ac6454SAndrew Thompson 	 *  Interrupt(0x81) | Interrupt(0x81)
35102ac6454SAndrew Thompson 	 * -----------------+ BulkIN(0x02)
35202ac6454SAndrew Thompson 	 * Interface 1	    | BulkOUT(0x83)
35302ac6454SAndrew Thompson 	 *   BulkIN(0x02)   |
35402ac6454SAndrew Thompson 	 *   BulkOUT(0x83)  |
35502ac6454SAndrew Thompson 	 */
35602ac6454SAndrew Thompson 
35702ac6454SAndrew Thompson 	sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
35802ac6454SAndrew Thompson 	sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
35902ac6454SAndrew Thompson 
36002ac6454SAndrew Thompson 	iface = usb2_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
36102ac6454SAndrew Thompson 	if (iface) {
36202ac6454SAndrew Thompson 		id = usb2_get_interface_descriptor(iface);
36302ac6454SAndrew Thompson 		if (id == NULL) {
36402ac6454SAndrew Thompson 			device_printf(dev, "no interface descriptor (2)!\n");
36502ac6454SAndrew Thompson 			goto detach;
36602ac6454SAndrew Thompson 		}
36702ac6454SAndrew Thompson 		sc->sc_data_iface_no = id->bInterfaceNumber;
36802ac6454SAndrew Thompson 		sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
36902ac6454SAndrew Thompson 		usb2_set_parent_iface(uaa->device,
37002ac6454SAndrew Thompson 		    UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
37102ac6454SAndrew Thompson 	} else {
37202ac6454SAndrew Thompson 		sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
37302ac6454SAndrew Thompson 		sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
37402ac6454SAndrew Thompson 	}
37502ac6454SAndrew Thompson 
37602ac6454SAndrew Thompson 	error = usb2_transfer_setup(uaa->device,
37702ac6454SAndrew Thompson 	    sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
378deefe583SAndrew Thompson 	    UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
37902ac6454SAndrew Thompson 	if (error) {
38002ac6454SAndrew Thompson 		DPRINTF("one or more missing USB endpoints, "
38102ac6454SAndrew Thompson 		    "error=%s\n", usb2_errstr(error));
38202ac6454SAndrew Thompson 		goto detach;
38302ac6454SAndrew Thompson 	}
38402ac6454SAndrew Thompson 	error = uplcom_reset(sc, uaa->device);
38502ac6454SAndrew Thompson 	if (error) {
38602ac6454SAndrew Thompson 		device_printf(dev, "reset failed, error=%s\n",
38702ac6454SAndrew Thompson 		    usb2_errstr(error));
38802ac6454SAndrew Thompson 		goto detach;
38902ac6454SAndrew Thompson 	}
39002ac6454SAndrew Thompson 	/* clear stall at first run */
391deefe583SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
39202ac6454SAndrew Thompson 	usb2_transfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
39302ac6454SAndrew Thompson 	usb2_transfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
394deefe583SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
39502ac6454SAndrew Thompson 
39602ac6454SAndrew Thompson 	error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
397deefe583SAndrew Thompson 	    &uplcom_callback, &sc->sc_mtx);
39802ac6454SAndrew Thompson 	if (error) {
39902ac6454SAndrew Thompson 		goto detach;
40002ac6454SAndrew Thompson 	}
40102ac6454SAndrew Thompson 	/*
40202ac6454SAndrew Thompson 	 * do the initialization during attach so that the system does not
40302ac6454SAndrew Thompson 	 * sleep during open:
40402ac6454SAndrew Thompson 	 */
40502ac6454SAndrew Thompson 	if (sc->sc_chiptype == TYPE_PL2303X) {
40602ac6454SAndrew Thompson 		if (uplcom_pl2303x_init(uaa->device)) {
40702ac6454SAndrew Thompson 			device_printf(dev, "init failed!\n");
40802ac6454SAndrew Thompson 			goto detach;
40902ac6454SAndrew Thompson 		}
41002ac6454SAndrew Thompson 	}
41102ac6454SAndrew Thompson 	return (0);
41202ac6454SAndrew Thompson 
41302ac6454SAndrew Thompson detach:
41402ac6454SAndrew Thompson 	uplcom_detach(dev);
41502ac6454SAndrew Thompson 	return (ENXIO);
41602ac6454SAndrew Thompson }
41702ac6454SAndrew Thompson 
41802ac6454SAndrew Thompson static int
41902ac6454SAndrew Thompson uplcom_detach(device_t dev)
42002ac6454SAndrew Thompson {
42102ac6454SAndrew Thompson 	struct uplcom_softc *sc = device_get_softc(dev);
42202ac6454SAndrew Thompson 
42302ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
42402ac6454SAndrew Thompson 
42502ac6454SAndrew Thompson 	usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
42602ac6454SAndrew Thompson 	usb2_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
427deefe583SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
42802ac6454SAndrew Thompson 
42902ac6454SAndrew Thompson 	return (0);
43002ac6454SAndrew Thompson }
43102ac6454SAndrew Thompson 
43202ac6454SAndrew Thompson static usb2_error_t
43302ac6454SAndrew Thompson uplcom_reset(struct uplcom_softc *sc, struct usb2_device *udev)
43402ac6454SAndrew Thompson {
43502ac6454SAndrew Thompson 	struct usb2_device_request req;
43602ac6454SAndrew Thompson 
43702ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
43802ac6454SAndrew Thompson 	req.bRequest = UPLCOM_SET_REQUEST;
43902ac6454SAndrew Thompson 	USETW(req.wValue, 0);
44002ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
44102ac6454SAndrew Thompson 	req.wIndex[1] = 0;
44202ac6454SAndrew Thompson 	USETW(req.wLength, 0);
44302ac6454SAndrew Thompson 
444296ade60SAndrew Thompson 	return (usb2_do_request(udev, NULL, &req, NULL));
44502ac6454SAndrew Thompson }
44602ac6454SAndrew Thompson 
44702ac6454SAndrew Thompson struct pl2303x_init {
44802ac6454SAndrew Thompson 	uint8_t	req_type;
44902ac6454SAndrew Thompson 	uint8_t	request;
45002ac6454SAndrew Thompson 	uint16_t value;
45102ac6454SAndrew Thompson 	uint16_t index;
45202ac6454SAndrew Thompson 	uint16_t length;
45302ac6454SAndrew Thompson };
45402ac6454SAndrew Thompson 
45502ac6454SAndrew Thompson static const struct pl2303x_init pl2303x[] = {
45602ac6454SAndrew Thompson 	{UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
45702ac6454SAndrew Thompson 	{UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0},
45802ac6454SAndrew Thompson 	{UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
45902ac6454SAndrew Thompson 	{UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1},
46002ac6454SAndrew Thompson 	{UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
46102ac6454SAndrew Thompson 	{UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0},
46202ac6454SAndrew Thompson 	{UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
46302ac6454SAndrew Thompson 	{UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1},
46402ac6454SAndrew Thompson 	{UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0},
46502ac6454SAndrew Thompson 	{UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0},
46602ac6454SAndrew Thompson 	{UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0},
46702ac6454SAndrew Thompson 	{UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 8, 0, 0},
46802ac6454SAndrew Thompson 	{UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 9, 0, 0},
46902ac6454SAndrew Thompson };
47002ac6454SAndrew Thompson 
47102ac6454SAndrew Thompson #define	N_PL2302X_INIT	(sizeof(pl2303x)/sizeof(pl2303x[0]))
47202ac6454SAndrew Thompson 
47302ac6454SAndrew Thompson static int
47402ac6454SAndrew Thompson uplcom_pl2303x_init(struct usb2_device *udev)
47502ac6454SAndrew Thompson {
47602ac6454SAndrew Thompson 	struct usb2_device_request req;
47702ac6454SAndrew Thompson 	usb2_error_t err;
47802ac6454SAndrew Thompson 	uint8_t buf[4];
47902ac6454SAndrew Thompson 	uint8_t i;
48002ac6454SAndrew Thompson 
48102ac6454SAndrew Thompson 	for (i = 0; i != N_PL2302X_INIT; i++) {
48202ac6454SAndrew Thompson 		req.bmRequestType = pl2303x[i].req_type;
48302ac6454SAndrew Thompson 		req.bRequest = pl2303x[i].request;
48402ac6454SAndrew Thompson 		USETW(req.wValue, pl2303x[i].value);
48502ac6454SAndrew Thompson 		USETW(req.wIndex, pl2303x[i].index);
48602ac6454SAndrew Thompson 		USETW(req.wLength, pl2303x[i].length);
48702ac6454SAndrew Thompson 
488296ade60SAndrew Thompson 		err = usb2_do_request(udev, NULL, &req, buf);
48902ac6454SAndrew Thompson 		if (err) {
49002ac6454SAndrew Thompson 			DPRINTF("error=%s\n", usb2_errstr(err));
49102ac6454SAndrew Thompson 			return (EIO);
49202ac6454SAndrew Thompson 		}
49302ac6454SAndrew Thompson 	}
49402ac6454SAndrew Thompson 	return (0);
49502ac6454SAndrew Thompson }
49602ac6454SAndrew Thompson 
49702ac6454SAndrew Thompson static void
49802ac6454SAndrew Thompson uplcom_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff)
49902ac6454SAndrew Thompson {
50002ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
50102ac6454SAndrew Thompson 	struct usb2_device_request req;
50202ac6454SAndrew Thompson 
50302ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
50402ac6454SAndrew Thompson 
50502ac6454SAndrew Thompson 	if (onoff)
50602ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_DTR;
50702ac6454SAndrew Thompson 	else
50802ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_DTR;
50902ac6454SAndrew Thompson 
51002ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
51102ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
51202ac6454SAndrew Thompson 	USETW(req.wValue, sc->sc_line);
51302ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
51402ac6454SAndrew Thompson 	req.wIndex[1] = 0;
51502ac6454SAndrew Thompson 	USETW(req.wLength, 0);
51602ac6454SAndrew Thompson 
51702ac6454SAndrew Thompson 	usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
51802ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
51902ac6454SAndrew Thompson }
52002ac6454SAndrew Thompson 
52102ac6454SAndrew Thompson static void
52202ac6454SAndrew Thompson uplcom_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff)
52302ac6454SAndrew Thompson {
52402ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
52502ac6454SAndrew Thompson 	struct usb2_device_request req;
52602ac6454SAndrew Thompson 
52702ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
52802ac6454SAndrew Thompson 
52902ac6454SAndrew Thompson 	if (onoff)
53002ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_RTS;
53102ac6454SAndrew Thompson 	else
53202ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_RTS;
53302ac6454SAndrew Thompson 
53402ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
53502ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
53602ac6454SAndrew Thompson 	USETW(req.wValue, sc->sc_line);
53702ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
53802ac6454SAndrew Thompson 	req.wIndex[1] = 0;
53902ac6454SAndrew Thompson 	USETW(req.wLength, 0);
54002ac6454SAndrew Thompson 
54102ac6454SAndrew Thompson 	usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
54202ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
54302ac6454SAndrew Thompson }
54402ac6454SAndrew Thompson 
54502ac6454SAndrew Thompson static void
54602ac6454SAndrew Thompson uplcom_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
54702ac6454SAndrew Thompson {
54802ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
54902ac6454SAndrew Thompson 	struct usb2_device_request req;
55002ac6454SAndrew Thompson 	uint16_t temp;
55102ac6454SAndrew Thompson 
55202ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
55302ac6454SAndrew Thompson 
55402ac6454SAndrew Thompson 	temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
55502ac6454SAndrew Thompson 
55602ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
55702ac6454SAndrew Thompson 	req.bRequest = UCDC_SEND_BREAK;
55802ac6454SAndrew Thompson 	USETW(req.wValue, temp);
55902ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
56002ac6454SAndrew Thompson 	req.wIndex[1] = 0;
56102ac6454SAndrew Thompson 	USETW(req.wLength, 0);
56202ac6454SAndrew Thompson 
56302ac6454SAndrew Thompson 	usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
56402ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
56502ac6454SAndrew Thompson }
56602ac6454SAndrew Thompson 
56702ac6454SAndrew Thompson static const int32_t uplcom_rates[] = {
56802ac6454SAndrew Thompson 	75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
56902ac6454SAndrew Thompson 	19200, 28800, 38400, 57600, 115200,
57002ac6454SAndrew Thompson 	/*
57102ac6454SAndrew Thompson 	 * Higher speeds are probably possible. PL2303X supports up to
57202ac6454SAndrew Thompson 	 * 6Mb and can set any rate
57302ac6454SAndrew Thompson 	 */
57402ac6454SAndrew Thompson 	230400, 460800, 614400, 921600, 1228800
57502ac6454SAndrew Thompson };
57602ac6454SAndrew Thompson 
57702ac6454SAndrew Thompson #define	N_UPLCOM_RATES	(sizeof(uplcom_rates)/sizeof(uplcom_rates[0]))
57802ac6454SAndrew Thompson 
57902ac6454SAndrew Thompson static int
58002ac6454SAndrew Thompson uplcom_pre_param(struct usb2_com_softc *ucom, struct termios *t)
58102ac6454SAndrew Thompson {
58202ac6454SAndrew Thompson 	uint8_t i;
58302ac6454SAndrew Thompson 
58402ac6454SAndrew Thompson 	DPRINTF("\n");
58502ac6454SAndrew Thompson 
58602ac6454SAndrew Thompson 	/* check requested baud rate */
58702ac6454SAndrew Thompson 
58802ac6454SAndrew Thompson 	for (i = 0;; i++) {
58902ac6454SAndrew Thompson 
59002ac6454SAndrew Thompson 		if (i != N_UPLCOM_RATES) {
59102ac6454SAndrew Thompson 			if (uplcom_rates[i] == t->c_ospeed) {
59202ac6454SAndrew Thompson 				break;
59302ac6454SAndrew Thompson 			}
59402ac6454SAndrew Thompson 		} else {
59502ac6454SAndrew Thompson 			DPRINTF("invalid baud rate (%d)\n", t->c_ospeed);
59602ac6454SAndrew Thompson 			return (EIO);
59702ac6454SAndrew Thompson 		}
59802ac6454SAndrew Thompson 	}
59902ac6454SAndrew Thompson 
60002ac6454SAndrew Thompson 	return (0);
60102ac6454SAndrew Thompson }
60202ac6454SAndrew Thompson 
60302ac6454SAndrew Thompson static void
60402ac6454SAndrew Thompson uplcom_cfg_param(struct usb2_com_softc *ucom, struct termios *t)
60502ac6454SAndrew Thompson {
60602ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
60702ac6454SAndrew Thompson 	struct usb2_cdc_line_state ls;
60802ac6454SAndrew Thompson 	struct usb2_device_request req;
60902ac6454SAndrew Thompson 
61002ac6454SAndrew Thompson 	DPRINTF("sc = %p\n", sc);
61102ac6454SAndrew Thompson 
61202ac6454SAndrew Thompson 	bzero(&ls, sizeof(ls));
61302ac6454SAndrew Thompson 
61402ac6454SAndrew Thompson 	USETDW(ls.dwDTERate, t->c_ospeed);
61502ac6454SAndrew Thompson 
61602ac6454SAndrew Thompson 	if (t->c_cflag & CSTOPB) {
61702ac6454SAndrew Thompson 		ls.bCharFormat = UCDC_STOP_BIT_2;
61802ac6454SAndrew Thompson 	} else {
61902ac6454SAndrew Thompson 		ls.bCharFormat = UCDC_STOP_BIT_1;
62002ac6454SAndrew Thompson 	}
62102ac6454SAndrew Thompson 
62202ac6454SAndrew Thompson 	if (t->c_cflag & PARENB) {
62302ac6454SAndrew Thompson 		if (t->c_cflag & PARODD) {
62402ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_ODD;
62502ac6454SAndrew Thompson 		} else {
62602ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_EVEN;
62702ac6454SAndrew Thompson 		}
62802ac6454SAndrew Thompson 	} else {
62902ac6454SAndrew Thompson 		ls.bParityType = UCDC_PARITY_NONE;
63002ac6454SAndrew Thompson 	}
63102ac6454SAndrew Thompson 
63202ac6454SAndrew Thompson 	switch (t->c_cflag & CSIZE) {
63302ac6454SAndrew Thompson 	case CS5:
63402ac6454SAndrew Thompson 		ls.bDataBits = 5;
63502ac6454SAndrew Thompson 		break;
63602ac6454SAndrew Thompson 	case CS6:
63702ac6454SAndrew Thompson 		ls.bDataBits = 6;
63802ac6454SAndrew Thompson 		break;
63902ac6454SAndrew Thompson 	case CS7:
64002ac6454SAndrew Thompson 		ls.bDataBits = 7;
64102ac6454SAndrew Thompson 		break;
64202ac6454SAndrew Thompson 	case CS8:
64302ac6454SAndrew Thompson 		ls.bDataBits = 8;
64402ac6454SAndrew Thompson 		break;
64502ac6454SAndrew Thompson 	}
64602ac6454SAndrew Thompson 
64702ac6454SAndrew Thompson 	DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
64802ac6454SAndrew Thompson 	    UGETDW(ls.dwDTERate), ls.bCharFormat,
64902ac6454SAndrew Thompson 	    ls.bParityType, ls.bDataBits);
65002ac6454SAndrew Thompson 
65102ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
65202ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_LINE_CODING;
65302ac6454SAndrew Thompson 	USETW(req.wValue, 0);
65402ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
65502ac6454SAndrew Thompson 	req.wIndex[1] = 0;
65602ac6454SAndrew Thompson 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
65702ac6454SAndrew Thompson 
65802ac6454SAndrew Thompson 	usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
65902ac6454SAndrew Thompson 	    &req, &ls, 0, 1000);
66002ac6454SAndrew Thompson 
66102ac6454SAndrew Thompson 	if (t->c_cflag & CRTSCTS) {
66202ac6454SAndrew Thompson 
66302ac6454SAndrew Thompson 		DPRINTF("crtscts = on\n");
66402ac6454SAndrew Thompson 
66502ac6454SAndrew Thompson 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
66602ac6454SAndrew Thompson 		req.bRequest = UPLCOM_SET_REQUEST;
66702ac6454SAndrew Thompson 		USETW(req.wValue, 0);
66802ac6454SAndrew Thompson 		if (sc->sc_chiptype == TYPE_PL2303X)
66902ac6454SAndrew Thompson 			USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
67002ac6454SAndrew Thompson 		else
67102ac6454SAndrew Thompson 			USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
67202ac6454SAndrew Thompson 		USETW(req.wLength, 0);
67302ac6454SAndrew Thompson 
67402ac6454SAndrew Thompson 		usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
67502ac6454SAndrew Thompson 		    &req, NULL, 0, 1000);
67602ac6454SAndrew Thompson 	} else {
67702ac6454SAndrew Thompson 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
67802ac6454SAndrew Thompson 		req.bRequest = UPLCOM_SET_REQUEST;
67902ac6454SAndrew Thompson 		USETW(req.wValue, 0);
68002ac6454SAndrew Thompson 		USETW(req.wIndex, 0);
68102ac6454SAndrew Thompson 		USETW(req.wLength, 0);
68202ac6454SAndrew Thompson 		usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
68302ac6454SAndrew Thompson 		    &req, NULL, 0, 1000);
68402ac6454SAndrew Thompson 	}
68502ac6454SAndrew Thompson }
68602ac6454SAndrew Thompson 
68702ac6454SAndrew Thompson static void
68802ac6454SAndrew Thompson uplcom_start_read(struct usb2_com_softc *ucom)
68902ac6454SAndrew Thompson {
69002ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
69102ac6454SAndrew Thompson 
69202ac6454SAndrew Thompson 	/* start interrupt endpoint */
69302ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
69402ac6454SAndrew Thompson 
69502ac6454SAndrew Thompson 	/* start read endpoint */
69602ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
69702ac6454SAndrew Thompson }
69802ac6454SAndrew Thompson 
69902ac6454SAndrew Thompson static void
70002ac6454SAndrew Thompson uplcom_stop_read(struct usb2_com_softc *ucom)
70102ac6454SAndrew Thompson {
70202ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
70302ac6454SAndrew Thompson 
70402ac6454SAndrew Thompson 	/* stop interrupt endpoint */
70502ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
70602ac6454SAndrew Thompson 
70702ac6454SAndrew Thompson 	/* stop read endpoint */
70802ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
70902ac6454SAndrew Thompson }
71002ac6454SAndrew Thompson 
71102ac6454SAndrew Thompson static void
71202ac6454SAndrew Thompson uplcom_start_write(struct usb2_com_softc *ucom)
71302ac6454SAndrew Thompson {
71402ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
71502ac6454SAndrew Thompson 
71602ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
71702ac6454SAndrew Thompson }
71802ac6454SAndrew Thompson 
71902ac6454SAndrew Thompson static void
72002ac6454SAndrew Thompson uplcom_stop_write(struct usb2_com_softc *ucom)
72102ac6454SAndrew Thompson {
72202ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
72302ac6454SAndrew Thompson 
72402ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
72502ac6454SAndrew Thompson }
72602ac6454SAndrew Thompson 
72702ac6454SAndrew Thompson static void
72802ac6454SAndrew Thompson uplcom_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr)
72902ac6454SAndrew Thompson {
73002ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
73102ac6454SAndrew Thompson 
73202ac6454SAndrew Thompson 	DPRINTF("\n");
73302ac6454SAndrew Thompson 
73402ac6454SAndrew Thompson 	*lsr = sc->sc_lsr;
73502ac6454SAndrew Thompson 	*msr = sc->sc_msr;
73602ac6454SAndrew Thompson }
73702ac6454SAndrew Thompson 
73802ac6454SAndrew Thompson static void
73902ac6454SAndrew Thompson uplcom_intr_callback(struct usb2_xfer *xfer)
74002ac6454SAndrew Thompson {
74102ac6454SAndrew Thompson 	struct uplcom_softc *sc = xfer->priv_sc;
74202ac6454SAndrew Thompson 	uint8_t buf[9];
74302ac6454SAndrew Thompson 
74402ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
74502ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
74602ac6454SAndrew Thompson 
74702ac6454SAndrew Thompson 		DPRINTF("actlen = %u\n", xfer->actlen);
74802ac6454SAndrew Thompson 
74902ac6454SAndrew Thompson 		if (xfer->actlen >= 9) {
75002ac6454SAndrew Thompson 
75102ac6454SAndrew Thompson 			usb2_copy_out(xfer->frbuffers, 0, buf, sizeof(buf));
75202ac6454SAndrew Thompson 
75302ac6454SAndrew Thompson 			DPRINTF("status = 0x%02x\n", buf[8]);
75402ac6454SAndrew Thompson 
75502ac6454SAndrew Thompson 			sc->sc_lsr = 0;
75602ac6454SAndrew Thompson 			sc->sc_msr = 0;
75702ac6454SAndrew Thompson 
75802ac6454SAndrew Thompson 			if (buf[8] & RSAQ_STATUS_CTS) {
75902ac6454SAndrew Thompson 				sc->sc_msr |= SER_CTS;
76002ac6454SAndrew Thompson 			}
76102ac6454SAndrew Thompson 			if (buf[8] & RSAQ_STATUS_DSR) {
76202ac6454SAndrew Thompson 				sc->sc_msr |= SER_DSR;
76302ac6454SAndrew Thompson 			}
76402ac6454SAndrew Thompson 			if (buf[8] & RSAQ_STATUS_DCD) {
76502ac6454SAndrew Thompson 				sc->sc_msr |= SER_DCD;
76602ac6454SAndrew Thompson 			}
76702ac6454SAndrew Thompson 			usb2_com_status_change(&sc->sc_ucom);
76802ac6454SAndrew Thompson 		}
76902ac6454SAndrew Thompson 	case USB_ST_SETUP:
77002ac6454SAndrew Thompson tr_setup:
77102ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
77202ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
77302ac6454SAndrew Thompson 		return;
77402ac6454SAndrew Thompson 
77502ac6454SAndrew Thompson 	default:			/* Error */
77602ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
77702ac6454SAndrew Thompson 			/* try to clear stall first */
77802ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
77902ac6454SAndrew Thompson 			goto tr_setup;
78002ac6454SAndrew Thompson 		}
78102ac6454SAndrew Thompson 		return;
78202ac6454SAndrew Thompson 	}
78302ac6454SAndrew Thompson }
78402ac6454SAndrew Thompson 
78502ac6454SAndrew Thompson static void
78602ac6454SAndrew Thompson uplcom_write_callback(struct usb2_xfer *xfer)
78702ac6454SAndrew Thompson {
78802ac6454SAndrew Thompson 	struct uplcom_softc *sc = xfer->priv_sc;
78902ac6454SAndrew Thompson 	uint32_t actlen;
79002ac6454SAndrew Thompson 
79102ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
79202ac6454SAndrew Thompson 	case USB_ST_SETUP:
79302ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
79402ac6454SAndrew Thompson tr_setup:
79502ac6454SAndrew Thompson 		if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
79602ac6454SAndrew Thompson 		    UPLCOM_BULK_BUF_SIZE, &actlen)) {
79702ac6454SAndrew Thompson 
79802ac6454SAndrew Thompson 			DPRINTF("actlen = %d\n", actlen);
79902ac6454SAndrew Thompson 
80002ac6454SAndrew Thompson 			xfer->frlengths[0] = actlen;
80102ac6454SAndrew Thompson 			usb2_start_hardware(xfer);
80202ac6454SAndrew Thompson 		}
80302ac6454SAndrew Thompson 		return;
80402ac6454SAndrew Thompson 
80502ac6454SAndrew Thompson 	default:			/* Error */
80602ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
80702ac6454SAndrew Thompson 			/* try to clear stall first */
80802ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
80902ac6454SAndrew Thompson 			goto tr_setup;
81002ac6454SAndrew Thompson 		}
81102ac6454SAndrew Thompson 		return;
81202ac6454SAndrew Thompson 	}
81302ac6454SAndrew Thompson }
81402ac6454SAndrew Thompson 
81502ac6454SAndrew Thompson static void
81602ac6454SAndrew Thompson uplcom_read_callback(struct usb2_xfer *xfer)
81702ac6454SAndrew Thompson {
81802ac6454SAndrew Thompson 	struct uplcom_softc *sc = xfer->priv_sc;
81902ac6454SAndrew Thompson 
82002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
82102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
82202ac6454SAndrew Thompson 		usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers, 0, xfer->actlen);
82302ac6454SAndrew Thompson 
82402ac6454SAndrew Thompson 	case USB_ST_SETUP:
82502ac6454SAndrew Thompson tr_setup:
82602ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
82702ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
82802ac6454SAndrew Thompson 		return;
82902ac6454SAndrew Thompson 
83002ac6454SAndrew Thompson 	default:			/* Error */
83102ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
83202ac6454SAndrew Thompson 			/* try to clear stall first */
83302ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
83402ac6454SAndrew Thompson 			goto tr_setup;
83502ac6454SAndrew Thompson 		}
83602ac6454SAndrew Thompson 		return;
83702ac6454SAndrew Thompson 	}
83802ac6454SAndrew Thompson }
839