xref: /freebsd/sys/dev/usb/serial/uplcom.c (revision 27e4bd81f74296452d8a6a96af9cfde3108e372d)
102ac6454SAndrew Thompson /*	$NetBSD: uplcom.c,v 1.21 2001/11/13 06:24:56 lukem Exp $	*/
202ac6454SAndrew Thompson 
302ac6454SAndrew Thompson #include <sys/cdefs.h>
402ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
502ac6454SAndrew Thompson 
602ac6454SAndrew Thompson /*-
7718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause-NetBSD
8718cf2ccSPedro F. Giffuni  *
902ac6454SAndrew Thompson  * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
1002ac6454SAndrew Thompson  * All rights reserved.
1102ac6454SAndrew Thompson  *
1202ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
1302ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1402ac6454SAndrew Thompson  * are met:
1502ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1602ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1702ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1802ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1902ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
2002ac6454SAndrew Thompson  *
2102ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2202ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2302ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2402ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2502ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2602ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2702ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2802ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2902ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3002ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3102ac6454SAndrew Thompson  * SUCH DAMAGE.
3202ac6454SAndrew Thompson  */
3302ac6454SAndrew Thompson 
3402ac6454SAndrew Thompson /*-
3502ac6454SAndrew Thompson  * Copyright (c) 2001 The NetBSD Foundation, Inc.
3602ac6454SAndrew Thompson  * All rights reserved.
3702ac6454SAndrew Thompson  *
3802ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
3902ac6454SAndrew Thompson  * by Ichiro FUKUHARA (ichiro@ichiro.org).
4002ac6454SAndrew Thompson  *
4102ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
4202ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
4302ac6454SAndrew Thompson  * are met:
4402ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
4502ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
4602ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
4702ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
4802ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
4902ac6454SAndrew Thompson  *
5002ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
5102ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
5202ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5302ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5402ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5502ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5602ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5702ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
5802ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5902ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
6002ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
6102ac6454SAndrew Thompson  */
6202ac6454SAndrew Thompson 
6302ac6454SAndrew Thompson /*
6402ac6454SAndrew Thompson  * This driver supports several USB-to-RS232 serial adapters driven by
6502ac6454SAndrew Thompson  * Prolific PL-2303, PL-2303X and probably PL-2303HX USB-to-RS232
6602ac6454SAndrew Thompson  * bridge chip.  The adapters are sold under many different brand
6702ac6454SAndrew Thompson  * names.
6802ac6454SAndrew Thompson  *
6902ac6454SAndrew Thompson  * Datasheets are available at Prolific www site at
7002ac6454SAndrew Thompson  * http://www.prolific.com.tw.  The datasheets don't contain full
7102ac6454SAndrew Thompson  * programming information for the chip.
7202ac6454SAndrew Thompson  *
7302ac6454SAndrew Thompson  * PL-2303HX is probably programmed the same as PL-2303X.
7402ac6454SAndrew Thompson  *
7502ac6454SAndrew Thompson  * There are several differences between PL-2303 and PL-2303(H)X.
7602ac6454SAndrew Thompson  * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_
7702ac6454SAndrew Thompson  * different command for controlling CRTSCTS and needs special
7802ac6454SAndrew Thompson  * sequence of commands for initialization which aren't also
7902ac6454SAndrew Thompson  * documented in the datasheet.
8002ac6454SAndrew Thompson  */
8102ac6454SAndrew Thompson 
82ed6d949aSAndrew Thompson #include <sys/stdint.h>
83ed6d949aSAndrew Thompson #include <sys/stddef.h>
84ed6d949aSAndrew Thompson #include <sys/param.h>
85ed6d949aSAndrew Thompson #include <sys/queue.h>
86ed6d949aSAndrew Thompson #include <sys/types.h>
87ed6d949aSAndrew Thompson #include <sys/systm.h>
88ed6d949aSAndrew Thompson #include <sys/kernel.h>
89ed6d949aSAndrew Thompson #include <sys/bus.h>
90ed6d949aSAndrew Thompson #include <sys/module.h>
91ed6d949aSAndrew Thompson #include <sys/lock.h>
92ed6d949aSAndrew Thompson #include <sys/mutex.h>
93ed6d949aSAndrew Thompson #include <sys/condvar.h>
94ed6d949aSAndrew Thompson #include <sys/sysctl.h>
95ed6d949aSAndrew Thompson #include <sys/sx.h>
96ed6d949aSAndrew Thompson #include <sys/unistd.h>
97ed6d949aSAndrew Thompson #include <sys/callout.h>
98ed6d949aSAndrew Thompson #include <sys/malloc.h>
99ed6d949aSAndrew Thompson #include <sys/priv.h>
100ed6d949aSAndrew Thompson 
10102ac6454SAndrew Thompson #include <dev/usb/usb.h>
102ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
103ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
10402ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
105ed6d949aSAndrew Thompson #include "usbdevs.h"
10602ac6454SAndrew Thompson 
10702ac6454SAndrew Thompson #define	USB_DEBUG_VAR uplcom_debug
10802ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
10902ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
11002ac6454SAndrew Thompson 
11102ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
11202ac6454SAndrew Thompson 
113b850ecc1SAndrew Thompson #ifdef USB_DEBUG
11402ac6454SAndrew Thompson static int uplcom_debug = 0;
11502ac6454SAndrew Thompson 
1166472ac3dSEd Schouten static SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom");
117ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RWTUN,
11802ac6454SAndrew Thompson     &uplcom_debug, 0, "Debug level");
11902ac6454SAndrew Thompson #endif
12002ac6454SAndrew Thompson 
12102ac6454SAndrew Thompson #define	UPLCOM_MODVER			1	/* module version */
12202ac6454SAndrew Thompson 
12302ac6454SAndrew Thompson #define	UPLCOM_CONFIG_INDEX		0
12402ac6454SAndrew Thompson #define	UPLCOM_IFACE_INDEX		0
12502ac6454SAndrew Thompson #define	UPLCOM_SECOND_IFACE_INDEX	1
12602ac6454SAndrew Thompson 
12702ac6454SAndrew Thompson #ifndef UPLCOM_INTR_INTERVAL
12802ac6454SAndrew Thompson #define	UPLCOM_INTR_INTERVAL		0	/* default */
12902ac6454SAndrew Thompson #endif
13002ac6454SAndrew Thompson 
13102ac6454SAndrew Thompson #define	UPLCOM_BULK_BUF_SIZE 1024	/* bytes */
13202ac6454SAndrew Thompson 
13302ac6454SAndrew Thompson #define	UPLCOM_SET_REQUEST		0x01
13402ac6454SAndrew Thompson #define	UPLCOM_SET_CRTSCTS		0x41
13502ac6454SAndrew Thompson #define	UPLCOM_SET_CRTSCTS_PL2303X	0x61
13602ac6454SAndrew Thompson #define	RSAQ_STATUS_CTS			0x80
137*27e4bd81SHans Petter Selasky #define	RSAQ_STATUS_OVERRUN_ERROR	0x40
138*27e4bd81SHans Petter Selasky #define	RSAQ_STATUS_PARITY_ERROR	0x20
139*27e4bd81SHans Petter Selasky #define	RSAQ_STATUS_FRAME_ERROR	0x10
140*27e4bd81SHans Petter Selasky #define	RSAQ_STATUS_RING		0x08
141*27e4bd81SHans Petter Selasky #define	RSAQ_STATUS_BREAK_ERROR	0x04
14202ac6454SAndrew Thompson #define	RSAQ_STATUS_DSR			0x02
14302ac6454SAndrew Thompson #define	RSAQ_STATUS_DCD			0x01
14402ac6454SAndrew Thompson 
14502ac6454SAndrew Thompson #define	TYPE_PL2303			0
146bc65068dSGavin Atkinson #define	TYPE_PL2303HX			1
147*27e4bd81SHans Petter Selasky #define	TYPE_PL2303HXD			2
148*27e4bd81SHans Petter Selasky 
149*27e4bd81SHans Petter Selasky #define	UPLCOM_STATE_INDEX		8
15002ac6454SAndrew Thompson 
15102ac6454SAndrew Thompson enum {
15202ac6454SAndrew Thompson 	UPLCOM_BULK_DT_WR,
15302ac6454SAndrew Thompson 	UPLCOM_BULK_DT_RD,
15402ac6454SAndrew Thompson 	UPLCOM_INTR_DT_RD,
15502ac6454SAndrew Thompson 	UPLCOM_N_TRANSFER,
15602ac6454SAndrew Thompson };
15702ac6454SAndrew Thompson 
15802ac6454SAndrew Thompson struct uplcom_softc {
159760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
160760bc48eSAndrew Thompson 	struct ucom_softc sc_ucom;
16102ac6454SAndrew Thompson 
162760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER];
163760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
164deefe583SAndrew Thompson 	struct mtx sc_mtx;
16502ac6454SAndrew Thompson 
16602ac6454SAndrew Thompson 	uint16_t sc_line;
16702ac6454SAndrew Thompson 
16802ac6454SAndrew Thompson 	uint8_t	sc_lsr;			/* local status register */
16902ac6454SAndrew Thompson 	uint8_t	sc_msr;			/* uplcom status register */
17002ac6454SAndrew Thompson 	uint8_t	sc_chiptype;		/* type of chip */
17102ac6454SAndrew Thompson 	uint8_t	sc_ctrl_iface_no;
17202ac6454SAndrew Thompson 	uint8_t	sc_data_iface_no;
17302ac6454SAndrew Thompson 	uint8_t	sc_iface_index[2];
17402ac6454SAndrew Thompson };
17502ac6454SAndrew Thompson 
17602ac6454SAndrew Thompson /* prototypes */
17702ac6454SAndrew Thompson 
178e0a69b51SAndrew Thompson static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *);
17902d4a225SDimitry Andric static usb_error_t uplcom_pl2303_do(struct usb_device *, uint8_t, uint8_t,
180bc65068dSGavin Atkinson 			uint16_t, uint16_t, uint16_t);
181bc65068dSGavin Atkinson static int	uplcom_pl2303_init(struct usb_device *, uint8_t);
1825805d178SHans Petter Selasky static void	uplcom_free(struct ucom_softc *);
183760bc48eSAndrew Thompson static void	uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
184760bc48eSAndrew Thompson static void	uplcom_cfg_set_rts(struct ucom_softc *, uint8_t);
185760bc48eSAndrew Thompson static void	uplcom_cfg_set_break(struct ucom_softc *, uint8_t);
186760bc48eSAndrew Thompson static int	uplcom_pre_param(struct ucom_softc *, struct termios *);
187760bc48eSAndrew Thompson static void	uplcom_cfg_param(struct ucom_softc *, struct termios *);
188760bc48eSAndrew Thompson static void	uplcom_start_read(struct ucom_softc *);
189760bc48eSAndrew Thompson static void	uplcom_stop_read(struct ucom_softc *);
190760bc48eSAndrew Thompson static void	uplcom_start_write(struct ucom_softc *);
191760bc48eSAndrew Thompson static void	uplcom_stop_write(struct ucom_softc *);
192760bc48eSAndrew Thompson static void	uplcom_cfg_get_status(struct ucom_softc *, uint8_t *,
19302ac6454SAndrew Thompson 		    uint8_t *);
194655dc9d0SAndrew Thompson static void	uplcom_poll(struct ucom_softc *ucom);
19502ac6454SAndrew Thompson 
19602ac6454SAndrew Thompson static device_probe_t uplcom_probe;
19702ac6454SAndrew Thompson static device_attach_t uplcom_attach;
19802ac6454SAndrew Thompson static device_detach_t uplcom_detach;
199c01fc06eSHans Petter Selasky static void uplcom_free_softc(struct uplcom_softc *);
20002ac6454SAndrew Thompson 
201e0a69b51SAndrew Thompson static usb_callback_t uplcom_intr_callback;
202e0a69b51SAndrew Thompson static usb_callback_t uplcom_write_callback;
203e0a69b51SAndrew Thompson static usb_callback_t uplcom_read_callback;
20402ac6454SAndrew Thompson 
205760bc48eSAndrew Thompson static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
20602ac6454SAndrew Thompson 
20702ac6454SAndrew Thompson 	[UPLCOM_BULK_DT_WR] = {
20802ac6454SAndrew Thompson 		.type = UE_BULK,
20902ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
21002ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
2114eae601eSAndrew Thompson 		.bufsize = UPLCOM_BULK_BUF_SIZE,
2124eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2134eae601eSAndrew Thompson 		.callback = &uplcom_write_callback,
21402ac6454SAndrew Thompson 		.if_index = 0,
21502ac6454SAndrew Thompson 	},
21602ac6454SAndrew Thompson 
21702ac6454SAndrew Thompson 	[UPLCOM_BULK_DT_RD] = {
21802ac6454SAndrew Thompson 		.type = UE_BULK,
21902ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
22002ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2214eae601eSAndrew Thompson 		.bufsize = UPLCOM_BULK_BUF_SIZE,
2224eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2234eae601eSAndrew Thompson 		.callback = &uplcom_read_callback,
22402ac6454SAndrew Thompson 		.if_index = 0,
22502ac6454SAndrew Thompson 	},
22602ac6454SAndrew Thompson 
22702ac6454SAndrew Thompson 	[UPLCOM_INTR_DT_RD] = {
22802ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
22902ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
23002ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2314eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2324eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
2334eae601eSAndrew Thompson 		.callback = &uplcom_intr_callback,
23402ac6454SAndrew Thompson 		.if_index = 1,
23502ac6454SAndrew Thompson 	},
23602ac6454SAndrew Thompson };
23702ac6454SAndrew Thompson 
238a8675caeSAndrew Thompson static struct ucom_callback uplcom_callback = {
239a593f6b8SAndrew Thompson 	.ucom_cfg_get_status = &uplcom_cfg_get_status,
240a593f6b8SAndrew Thompson 	.ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
241a593f6b8SAndrew Thompson 	.ucom_cfg_set_rts = &uplcom_cfg_set_rts,
242a593f6b8SAndrew Thompson 	.ucom_cfg_set_break = &uplcom_cfg_set_break,
243a593f6b8SAndrew Thompson 	.ucom_cfg_param = &uplcom_cfg_param,
244a593f6b8SAndrew Thompson 	.ucom_pre_param = &uplcom_pre_param,
245a593f6b8SAndrew Thompson 	.ucom_start_read = &uplcom_start_read,
246a593f6b8SAndrew Thompson 	.ucom_stop_read = &uplcom_stop_read,
247a593f6b8SAndrew Thompson 	.ucom_start_write = &uplcom_start_write,
248a593f6b8SAndrew Thompson 	.ucom_stop_write = &uplcom_stop_write,
249655dc9d0SAndrew Thompson 	.ucom_poll = &uplcom_poll,
2505805d178SHans Petter Selasky 	.ucom_free = &uplcom_free,
25102ac6454SAndrew Thompson };
25202ac6454SAndrew Thompson 
253bc65068dSGavin Atkinson #define	UPLCOM_DEV(v,p)				\
254bc65068dSGavin Atkinson   { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p) }
25502ac6454SAndrew Thompson 
256f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID uplcom_devs[] = {
257f5113df9SGavin Atkinson 	UPLCOM_DEV(ACERP, S81),			/* BenQ S81 phone */
258f5113df9SGavin Atkinson 	UPLCOM_DEV(ADLINK, ND6530),		/* ADLINK ND-6530 USB-Serial */
259f5113df9SGavin Atkinson 	UPLCOM_DEV(ALCATEL, OT535),		/* Alcatel One Touch 535/735 */
260f5113df9SGavin Atkinson 	UPLCOM_DEV(ALCOR, AU9720),		/* Alcor AU9720 USB 2.0-RS232 */
261f5113df9SGavin Atkinson 	UPLCOM_DEV(ANCHOR, SERIAL),		/* Anchor Serial adapter */
262bc65068dSGavin Atkinson 	UPLCOM_DEV(ATEN, UC232A),		/* PLANEX USB-RS232 URS-03 */
26301a8f075SGavin Atkinson 	UPLCOM_DEV(BELKIN, F5U257),		/* Belkin F5U257 USB to Serial */
264bc65068dSGavin Atkinson 	UPLCOM_DEV(COREGA, CGUSBRS232R),	/* Corega CG-USBRS232R */
265f5113df9SGavin Atkinson 	UPLCOM_DEV(EPSON, CRESSI_EDY),		/* Cressi Edy diving computer */
266c6ac426dSGavin Atkinson 	UPLCOM_DEV(EPSON, N2ITION3),		/* Zeagle N2iTion3 diving computer */
26701a8f075SGavin Atkinson 	UPLCOM_DEV(ELECOM, UCSGT),		/* ELECOM UC-SGT Serial Adapter */
26801a8f075SGavin Atkinson 	UPLCOM_DEV(ELECOM, UCSGT0),		/* ELECOM UC-SGT Serial Adapter */
269bc65068dSGavin Atkinson 	UPLCOM_DEV(HAL, IMR001),		/* HAL Corporation Crossam2+USB */
270f5113df9SGavin Atkinson 	UPLCOM_DEV(HP, LD220),			/* HP LD220 POS Display */
271bc65068dSGavin Atkinson 	UPLCOM_DEV(IODATA, USBRSAQ),		/* I/O DATA USB-RSAQ */
272bc65068dSGavin Atkinson 	UPLCOM_DEV(IODATA, USBRSAQ5),		/* I/O DATA USB-RSAQ5 */
273f5113df9SGavin Atkinson 	UPLCOM_DEV(ITEGNO, WM1080A),		/* iTegno WM1080A GSM/GFPRS modem */
274f5113df9SGavin Atkinson 	UPLCOM_DEV(ITEGNO, WM2080A),		/* iTegno WM2080A CDMA modem */
275f5113df9SGavin Atkinson 	UPLCOM_DEV(LEADTEK, 9531),		/* Leadtek 9531 GPS */
276f5113df9SGavin Atkinson 	UPLCOM_DEV(MICROSOFT, 700WX),		/* Microsoft Palm 700WX */
277bc65068dSGavin Atkinson 	UPLCOM_DEV(MOBILEACTION, MA620),	/* Mobile Action MA-620 Infrared Adapter */
278f5113df9SGavin Atkinson 	UPLCOM_DEV(NETINDEX, WS002IN),		/* Willcom W-S002IN */
279f5113df9SGavin Atkinson 	UPLCOM_DEV(NOKIA2, CA42),		/* Nokia CA-42 cable */
280f5113df9SGavin Atkinson 	UPLCOM_DEV(OTI, DKU5),			/* OTI DKU-5 cable */
281f5113df9SGavin Atkinson 	UPLCOM_DEV(PANASONIC, TYTP50P6S),	/* Panasonic TY-TP50P6-S flat screen */
282f5113df9SGavin Atkinson 	UPLCOM_DEV(PLX, CA42),			/* PLX CA-42 clone cable */
283f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, ALLTRONIX_GPRS),	/* Alltronix ACM003U00 modem */
284f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, ALDIGA_AL11U),	/* AlDiga AL-11U modem */
285f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, DCU11),		/* DCU-11 Phone Cable */
286f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, HCR331),		/* HCR331 Card Reader */
287f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, MICROMAX_610U),	/* Micromax 610U modem */
288d01755edSGavin Atkinson 	UPLCOM_DEV(PROLIFIC, MOTOROLA),		/* Motorola cable */
289bc65068dSGavin Atkinson 	UPLCOM_DEV(PROLIFIC, PHAROS),		/* Prolific Pharos */
290f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, PL2303),		/* Generic adapter */
291bc65068dSGavin Atkinson 	UPLCOM_DEV(PROLIFIC, RSAQ2),		/* I/O DATA USB-RSAQ2 */
292bc65068dSGavin Atkinson 	UPLCOM_DEV(PROLIFIC, RSAQ3),		/* I/O DATA USB-RSAQ3 */
293635c9457SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, UIC_MSR206),	/* UIC MSR206 Card Reader */
294f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC2, PL2303),		/* Prolific adapter */
29501a8f075SGavin Atkinson 	UPLCOM_DEV(RADIOSHACK, USBCABLE),	/* Radio Shack USB Adapter */
296bc65068dSGavin Atkinson 	UPLCOM_DEV(RATOC, REXUSB60),		/* RATOC REX-USB60 */
297bc65068dSGavin Atkinson 	UPLCOM_DEV(SAGEM, USBSERIAL),		/* Sagem USB-Serial Controller */
298f5113df9SGavin Atkinson 	UPLCOM_DEV(SAMSUNG, I330),		/* Samsung I330 phone cradle */
299f5113df9SGavin Atkinson 	UPLCOM_DEV(SANWA, KB_USB2),		/* Sanwa KB-USB2 Multimeter cable */
30001a8f075SGavin Atkinson 	UPLCOM_DEV(SIEMENS3, EF81),		/* Siemens EF81 */
30101a8f075SGavin Atkinson 	UPLCOM_DEV(SIEMENS3, SX1),		/* Siemens SX1 */
30201a8f075SGavin Atkinson 	UPLCOM_DEV(SIEMENS3, X65),		/* Siemens X65 */
30301a8f075SGavin Atkinson 	UPLCOM_DEV(SIEMENS3, X75),		/* Siemens X75 */
304bc65068dSGavin Atkinson 	UPLCOM_DEV(SITECOM, SERIAL),		/* Sitecom USB to Serial */
30501a8f075SGavin Atkinson 	UPLCOM_DEV(SMART, PL2303),		/* SMART Technologies USB to Serial */
306f5113df9SGavin Atkinson 	UPLCOM_DEV(SONY, QN3),			/* Sony QN3 phone cable */
307f5113df9SGavin Atkinson 	UPLCOM_DEV(SONYERICSSON, DATAPILOT),	/* Sony Ericsson Datapilot */
308f5113df9SGavin Atkinson 	UPLCOM_DEV(SONYERICSSON, DCU10),	/* Sony Ericsson DCU-10 Cable */
309bc65068dSGavin Atkinson 	UPLCOM_DEV(SOURCENEXT, KEIKAI8),	/* SOURCENEXT KeikaiDenwa 8 */
310bc65068dSGavin Atkinson 	UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG),	/* SOURCENEXT KeikaiDenwa 8 with charger */
311f5113df9SGavin Atkinson 	UPLCOM_DEV(SPEEDDRAGON, MS3303H),	/* Speed Dragon USB-Serial */
312f5113df9SGavin Atkinson 	UPLCOM_DEV(SYNTECH, CPT8001C),		/* Syntech CPT-8001C Barcode scanner */
313bc65068dSGavin Atkinson 	UPLCOM_DEV(TDK, UHA6400),		/* TDK USB-PHS Adapter UHA6400 */
314f5113df9SGavin Atkinson 	UPLCOM_DEV(TDK, UPA9664),		/* TDK USB-PHS Adapter UPA9664 */
31501a8f075SGavin Atkinson 	UPLCOM_DEV(TRIPPLITE, U209),		/* Tripp-Lite U209-000-R USB to Serial */
316f5113df9SGavin Atkinson 	UPLCOM_DEV(YCCABLE, PL2303),		/* YC Cable USB-Serial */
31702ac6454SAndrew Thompson };
3189e6b5313SAndrew Thompson #undef UPLCOM_DEV
31902ac6454SAndrew Thompson 
32002ac6454SAndrew Thompson static device_method_t uplcom_methods[] = {
32102ac6454SAndrew Thompson 	DEVMETHOD(device_probe, uplcom_probe),
32202ac6454SAndrew Thompson 	DEVMETHOD(device_attach, uplcom_attach),
32302ac6454SAndrew Thompson 	DEVMETHOD(device_detach, uplcom_detach),
3245805d178SHans Petter Selasky 	DEVMETHOD_END
32502ac6454SAndrew Thompson };
32602ac6454SAndrew Thompson 
32702ac6454SAndrew Thompson static devclass_t uplcom_devclass;
32802ac6454SAndrew Thompson 
32902ac6454SAndrew Thompson static driver_t uplcom_driver = {
33002ac6454SAndrew Thompson 	.name = "uplcom",
33102ac6454SAndrew Thompson 	.methods = uplcom_methods,
33202ac6454SAndrew Thompson 	.size = sizeof(struct uplcom_softc),
33302ac6454SAndrew Thompson };
33402ac6454SAndrew Thompson 
3359aef556dSAndrew Thompson DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0);
33602ac6454SAndrew Thompson MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
33702ac6454SAndrew Thompson MODULE_DEPEND(uplcom, usb, 1, 1, 1);
33802ac6454SAndrew Thompson MODULE_VERSION(uplcom, UPLCOM_MODVER);
339f809f280SWarner Losh USB_PNP_HOST_INFO(uplcom_devs);
34002ac6454SAndrew Thompson 
34102ac6454SAndrew Thompson static int
34202ac6454SAndrew Thompson uplcom_probe(device_t dev)
34302ac6454SAndrew Thompson {
344760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
34502ac6454SAndrew Thompson 
34602ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
34702ac6454SAndrew Thompson 
348f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
34902ac6454SAndrew Thompson 		return (ENXIO);
35002ac6454SAndrew Thompson 	}
35102ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
35202ac6454SAndrew Thompson 		return (ENXIO);
35302ac6454SAndrew Thompson 	}
35402ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
35502ac6454SAndrew Thompson 		return (ENXIO);
35602ac6454SAndrew Thompson 	}
357a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
35802ac6454SAndrew Thompson }
35902ac6454SAndrew Thompson 
36002ac6454SAndrew Thompson static int
36102ac6454SAndrew Thompson uplcom_attach(device_t dev)
36202ac6454SAndrew Thompson {
363760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
36402ac6454SAndrew Thompson 	struct uplcom_softc *sc = device_get_softc(dev);
365760bc48eSAndrew Thompson 	struct usb_interface *iface;
366760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
367bc65068dSGavin Atkinson 	struct usb_device_descriptor *dd;
36802ac6454SAndrew Thompson 	int error;
36902ac6454SAndrew Thompson 
37002ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
37102ac6454SAndrew Thompson 
372a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
373deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
3745805d178SHans Petter Selasky 	ucom_ref(&sc->sc_super_ucom);
37502ac6454SAndrew Thompson 
37602ac6454SAndrew Thompson 	DPRINTF("sc = %p\n", sc);
37702ac6454SAndrew Thompson 
37802ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
37902ac6454SAndrew Thompson 
380bc65068dSGavin Atkinson 	dd = usbd_get_device_descriptor(sc->sc_udev);
381*27e4bd81SHans Petter Selasky 
382*27e4bd81SHans Petter Selasky 	switch (UGETW(dd->bcdDevice)) {
383*27e4bd81SHans Petter Selasky 	case 0x0300:
384*27e4bd81SHans Petter Selasky 		sc->sc_chiptype = TYPE_PL2303HX;
385*27e4bd81SHans Petter Selasky 		/* or TA, that is HX with external crystal */
386*27e4bd81SHans Petter Selasky 		break;
387*27e4bd81SHans Petter Selasky 	case 0x0400:
388*27e4bd81SHans Petter Selasky 		sc->sc_chiptype = TYPE_PL2303HXD;
389*27e4bd81SHans Petter Selasky 		/* or EA, that is HXD with ESD protection */
390*27e4bd81SHans Petter Selasky 		/* or RA, that has internal voltage level converter that works only up to 1Mbaud (!) */
391*27e4bd81SHans Petter Selasky 		break;
392*27e4bd81SHans Petter Selasky 	case 0x0500:
393*27e4bd81SHans Petter Selasky 		sc->sc_chiptype = TYPE_PL2303HXD;
394*27e4bd81SHans Petter Selasky 		/* in fact it's TB, that is HXD with external crystal */
395*27e4bd81SHans Petter Selasky 		break;
396*27e4bd81SHans Petter Selasky 	default:
397*27e4bd81SHans Petter Selasky 		/* NOTE: I have no info about the bcdDevice for the base PL2303 (up to 1.2Mbaud,
398*27e4bd81SHans Petter Selasky 		   only fixed rates) and for PL2303SA (8-pin chip, up to 115200 baud */
399*27e4bd81SHans Petter Selasky 		/* Determine the chip type.  This algorithm is taken from Linux. */
400bc65068dSGavin Atkinson 		if (dd->bDeviceClass == 0x02)
401bc65068dSGavin Atkinson 			sc->sc_chiptype = TYPE_PL2303;
402bc65068dSGavin Atkinson 		else if (dd->bMaxPacketSize == 0x40)
403bc65068dSGavin Atkinson 			sc->sc_chiptype = TYPE_PL2303HX;
404bc65068dSGavin Atkinson 		else
405bc65068dSGavin Atkinson 			sc->sc_chiptype = TYPE_PL2303;
406*27e4bd81SHans Petter Selasky 		break;
407*27e4bd81SHans Petter Selasky 	}
408bc65068dSGavin Atkinson 
409*27e4bd81SHans Petter Selasky 	switch (sc->sc_chiptype) {
410*27e4bd81SHans Petter Selasky 	case TYPE_PL2303:
411*27e4bd81SHans Petter Selasky 		DPRINTF("chiptype: 2303\n");
412*27e4bd81SHans Petter Selasky 		break;
413*27e4bd81SHans Petter Selasky 	case TYPE_PL2303HX:
414*27e4bd81SHans Petter Selasky 		DPRINTF("chiptype: 2303HX/TA\n");
415*27e4bd81SHans Petter Selasky 		break;
416*27e4bd81SHans Petter Selasky 	case TYPE_PL2303HXD:
417*27e4bd81SHans Petter Selasky 		DPRINTF("chiptype: 2303HXD/TB/RA/EA\n");
418*27e4bd81SHans Petter Selasky 		break;
419*27e4bd81SHans Petter Selasky 	default:
420*27e4bd81SHans Petter Selasky 		DPRINTF("chiptype: unknown %d\n", sc->sc_chiptype);
421*27e4bd81SHans Petter Selasky 		break;
422*27e4bd81SHans Petter Selasky 	}
42302ac6454SAndrew Thompson 
42402ac6454SAndrew Thompson 	/*
42502ac6454SAndrew Thompson 	 * USB-RSAQ1 has two interface
42602ac6454SAndrew Thompson 	 *
42702ac6454SAndrew Thompson 	 *  USB-RSAQ1       | USB-RSAQ2
42802ac6454SAndrew Thompson 	 * -----------------+-----------------
42902ac6454SAndrew Thompson 	 * Interface 0      |Interface 0
43002ac6454SAndrew Thompson 	 *  Interrupt(0x81) | Interrupt(0x81)
43102ac6454SAndrew Thompson 	 * -----------------+ BulkIN(0x02)
43202ac6454SAndrew Thompson 	 * Interface 1	    | BulkOUT(0x83)
43302ac6454SAndrew Thompson 	 *   BulkIN(0x02)   |
43402ac6454SAndrew Thompson 	 *   BulkOUT(0x83)  |
43502ac6454SAndrew Thompson 	 */
43602ac6454SAndrew Thompson 
43702ac6454SAndrew Thompson 	sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
43802ac6454SAndrew Thompson 	sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
43902ac6454SAndrew Thompson 
440a593f6b8SAndrew Thompson 	iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
44102ac6454SAndrew Thompson 	if (iface) {
442a593f6b8SAndrew Thompson 		id = usbd_get_interface_descriptor(iface);
44302ac6454SAndrew Thompson 		if (id == NULL) {
444767cb2e2SAndrew Thompson 			device_printf(dev, "no interface descriptor (2)\n");
44502ac6454SAndrew Thompson 			goto detach;
44602ac6454SAndrew Thompson 		}
44702ac6454SAndrew Thompson 		sc->sc_data_iface_no = id->bInterfaceNumber;
44802ac6454SAndrew Thompson 		sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
449a593f6b8SAndrew Thompson 		usbd_set_parent_iface(uaa->device,
45002ac6454SAndrew Thompson 		    UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
45102ac6454SAndrew Thompson 	} else {
45202ac6454SAndrew Thompson 		sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
45302ac6454SAndrew Thompson 		sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
45402ac6454SAndrew Thompson 	}
45502ac6454SAndrew Thompson 
456a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device,
45702ac6454SAndrew Thompson 	    sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
458deefe583SAndrew Thompson 	    UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
45902ac6454SAndrew Thompson 	if (error) {
46002ac6454SAndrew Thompson 		DPRINTF("one or more missing USB endpoints, "
461a593f6b8SAndrew Thompson 		    "error=%s\n", usbd_errstr(error));
46202ac6454SAndrew Thompson 		goto detach;
46302ac6454SAndrew Thompson 	}
46402ac6454SAndrew Thompson 	error = uplcom_reset(sc, uaa->device);
46502ac6454SAndrew Thompson 	if (error) {
46602ac6454SAndrew Thompson 		device_printf(dev, "reset failed, error=%s\n",
467a593f6b8SAndrew Thompson 		    usbd_errstr(error));
46802ac6454SAndrew Thompson 		goto detach;
46902ac6454SAndrew Thompson 	}
4709efb7339SHans Petter Selasky 
471*27e4bd81SHans Petter Selasky 	if (sc->sc_chiptype == TYPE_PL2303) {
4729efb7339SHans Petter Selasky 		/* HX variants seem to lock up after a clear stall request. */
473deefe583SAndrew Thompson 		mtx_lock(&sc->sc_mtx);
474ed6d949aSAndrew Thompson 		usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
475ed6d949aSAndrew Thompson 		usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
476deefe583SAndrew Thompson 		mtx_unlock(&sc->sc_mtx);
4779efb7339SHans Petter Selasky 	} else {
478*27e4bd81SHans Petter Selasky 		/* reset upstream data pipes */
4799efb7339SHans Petter Selasky 		if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
4809efb7339SHans Petter Selasky 		    UPLCOM_SET_REQUEST, 8, 0, 0) ||
4819efb7339SHans Petter Selasky 		    uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
4829efb7339SHans Petter Selasky 		    UPLCOM_SET_REQUEST, 9, 0, 0)) {
4839efb7339SHans Petter Selasky 			goto detach;
4849efb7339SHans Petter Selasky 		}
4859efb7339SHans Petter Selasky 	}
48602ac6454SAndrew Thompson 
487a593f6b8SAndrew Thompson 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
488deefe583SAndrew Thompson 	    &uplcom_callback, &sc->sc_mtx);
48902ac6454SAndrew Thompson 	if (error) {
49002ac6454SAndrew Thompson 		goto detach;
49102ac6454SAndrew Thompson 	}
49202ac6454SAndrew Thompson 	/*
49302ac6454SAndrew Thompson 	 * do the initialization during attach so that the system does not
49402ac6454SAndrew Thompson 	 * sleep during open:
49502ac6454SAndrew Thompson 	 */
496bc65068dSGavin Atkinson 	if (uplcom_pl2303_init(uaa->device, sc->sc_chiptype)) {
497767cb2e2SAndrew Thompson 		device_printf(dev, "init failed\n");
49802ac6454SAndrew Thompson 		goto detach;
49902ac6454SAndrew Thompson 	}
5006416c259SNick Hibma 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
5016416c259SNick Hibma 
50202ac6454SAndrew Thompson 	return (0);
50302ac6454SAndrew Thompson 
50402ac6454SAndrew Thompson detach:
50502ac6454SAndrew Thompson 	uplcom_detach(dev);
50602ac6454SAndrew Thompson 	return (ENXIO);
50702ac6454SAndrew Thompson }
50802ac6454SAndrew Thompson 
50902ac6454SAndrew Thompson static int
51002ac6454SAndrew Thompson uplcom_detach(device_t dev)
51102ac6454SAndrew Thompson {
51202ac6454SAndrew Thompson 	struct uplcom_softc *sc = device_get_softc(dev);
51302ac6454SAndrew Thompson 
51402ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
51502ac6454SAndrew Thompson 
516015bb88fSNick Hibma 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
517a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
51802ac6454SAndrew Thompson 
519c01fc06eSHans Petter Selasky 	device_claim_softc(dev);
520c01fc06eSHans Petter Selasky 
521c01fc06eSHans Petter Selasky 	uplcom_free_softc(sc);
522c01fc06eSHans Petter Selasky 
52302ac6454SAndrew Thompson 	return (0);
52402ac6454SAndrew Thompson }
52502ac6454SAndrew Thompson 
5265805d178SHans Petter Selasky UCOM_UNLOAD_DRAIN(uplcom);
5275805d178SHans Petter Selasky 
5285805d178SHans Petter Selasky static void
529c01fc06eSHans Petter Selasky uplcom_free_softc(struct uplcom_softc *sc)
5305805d178SHans Petter Selasky {
5315805d178SHans Petter Selasky 	if (ucom_unref(&sc->sc_super_ucom)) {
5325805d178SHans Petter Selasky 		mtx_destroy(&sc->sc_mtx);
533c01fc06eSHans Petter Selasky 		device_free_softc(sc);
5345805d178SHans Petter Selasky 	}
5355805d178SHans Petter Selasky }
5365805d178SHans Petter Selasky 
5375805d178SHans Petter Selasky static void
5385805d178SHans Petter Selasky uplcom_free(struct ucom_softc *ucom)
5395805d178SHans Petter Selasky {
540c01fc06eSHans Petter Selasky 	uplcom_free_softc(ucom->sc_parent);
5415805d178SHans Petter Selasky }
5425805d178SHans Petter Selasky 
543e0a69b51SAndrew Thompson static usb_error_t
544760bc48eSAndrew Thompson uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
54502ac6454SAndrew Thompson {
546760bc48eSAndrew Thompson 	struct usb_device_request req;
54702ac6454SAndrew Thompson 
54802ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
54902ac6454SAndrew Thompson 	req.bRequest = UPLCOM_SET_REQUEST;
55002ac6454SAndrew Thompson 	USETW(req.wValue, 0);
55102ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
55202ac6454SAndrew Thompson 	req.wIndex[1] = 0;
55302ac6454SAndrew Thompson 	USETW(req.wLength, 0);
55402ac6454SAndrew Thompson 
555a593f6b8SAndrew Thompson 	return (usbd_do_request(udev, NULL, &req, NULL));
55602ac6454SAndrew Thompson }
55702ac6454SAndrew Thompson 
558bc65068dSGavin Atkinson static usb_error_t
55902d4a225SDimitry Andric uplcom_pl2303_do(struct usb_device *udev, uint8_t req_type, uint8_t request,
560bc65068dSGavin Atkinson     uint16_t value, uint16_t index, uint16_t length)
56102ac6454SAndrew Thompson {
562760bc48eSAndrew Thompson 	struct usb_device_request req;
563e0a69b51SAndrew Thompson 	usb_error_t err;
56402ac6454SAndrew Thompson 	uint8_t buf[4];
56502ac6454SAndrew Thompson 
566bc65068dSGavin Atkinson 	req.bmRequestType = req_type;
567bc65068dSGavin Atkinson 	req.bRequest = request;
568bc65068dSGavin Atkinson 	USETW(req.wValue, value);
569bc65068dSGavin Atkinson 	USETW(req.wIndex, index);
570bc65068dSGavin Atkinson 	USETW(req.wLength, length);
57102ac6454SAndrew Thompson 
572a593f6b8SAndrew Thompson 	err = usbd_do_request(udev, NULL, &req, buf);
57302ac6454SAndrew Thompson 	if (err) {
574a593f6b8SAndrew Thompson 		DPRINTF("error=%s\n", usbd_errstr(err));
575bc65068dSGavin Atkinson 		return (1);
576bc65068dSGavin Atkinson 	}
577bc65068dSGavin Atkinson 	return (0);
578bc65068dSGavin Atkinson }
579bc65068dSGavin Atkinson 
580bc65068dSGavin Atkinson static int
581bc65068dSGavin Atkinson uplcom_pl2303_init(struct usb_device *udev, uint8_t chiptype)
582bc65068dSGavin Atkinson {
583bc65068dSGavin Atkinson 	int err;
584bc65068dSGavin Atkinson 
585bc65068dSGavin Atkinson 	if (uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
586bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0)
587bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
588bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
589bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
590bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0)
591bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
592bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
593bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0)
594bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0))
59502ac6454SAndrew Thompson 		return (EIO);
596bc65068dSGavin Atkinson 
597*27e4bd81SHans Petter Selasky 	if (chiptype != TYPE_PL2303)
598bc65068dSGavin Atkinson 		err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0);
599bc65068dSGavin Atkinson 	else
600bc65068dSGavin Atkinson 		err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x24, 0);
601bc65068dSGavin Atkinson 	if (err)
602bc65068dSGavin Atkinson 		return (EIO);
603bc65068dSGavin Atkinson 
60402ac6454SAndrew Thompson 	return (0);
60502ac6454SAndrew Thompson }
60602ac6454SAndrew Thompson 
60702ac6454SAndrew Thompson static void
608760bc48eSAndrew Thompson uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
60902ac6454SAndrew Thompson {
61002ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
611760bc48eSAndrew Thompson 	struct usb_device_request req;
61202ac6454SAndrew Thompson 
61302ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
61402ac6454SAndrew Thompson 
61502ac6454SAndrew Thompson 	if (onoff)
61602ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_DTR;
61702ac6454SAndrew Thompson 	else
61802ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_DTR;
61902ac6454SAndrew Thompson 
62002ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
62102ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
62202ac6454SAndrew Thompson 	USETW(req.wValue, sc->sc_line);
62302ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
62402ac6454SAndrew Thompson 	req.wIndex[1] = 0;
62502ac6454SAndrew Thompson 	USETW(req.wLength, 0);
62602ac6454SAndrew Thompson 
627a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
62802ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
62902ac6454SAndrew Thompson }
63002ac6454SAndrew Thompson 
63102ac6454SAndrew Thompson static void
632760bc48eSAndrew Thompson uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
63302ac6454SAndrew Thompson {
63402ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
635760bc48eSAndrew Thompson 	struct usb_device_request req;
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
63802ac6454SAndrew Thompson 
63902ac6454SAndrew Thompson 	if (onoff)
64002ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_RTS;
64102ac6454SAndrew Thompson 	else
64202ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_RTS;
64302ac6454SAndrew Thompson 
64402ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
64502ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
64602ac6454SAndrew Thompson 	USETW(req.wValue, sc->sc_line);
64702ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
64802ac6454SAndrew Thompson 	req.wIndex[1] = 0;
64902ac6454SAndrew Thompson 	USETW(req.wLength, 0);
65002ac6454SAndrew Thompson 
651a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
65202ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
65302ac6454SAndrew Thompson }
65402ac6454SAndrew Thompson 
65502ac6454SAndrew Thompson static void
656760bc48eSAndrew Thompson uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
65702ac6454SAndrew Thompson {
65802ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
659760bc48eSAndrew Thompson 	struct usb_device_request req;
66002ac6454SAndrew Thompson 	uint16_t temp;
66102ac6454SAndrew Thompson 
66202ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
66302ac6454SAndrew Thompson 
66402ac6454SAndrew Thompson 	temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
66502ac6454SAndrew Thompson 
66602ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
66702ac6454SAndrew Thompson 	req.bRequest = UCDC_SEND_BREAK;
66802ac6454SAndrew Thompson 	USETW(req.wValue, temp);
66902ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
67002ac6454SAndrew Thompson 	req.wIndex[1] = 0;
67102ac6454SAndrew Thompson 	USETW(req.wLength, 0);
67202ac6454SAndrew Thompson 
673a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
67402ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
67502ac6454SAndrew Thompson }
67602ac6454SAndrew Thompson 
67702ac6454SAndrew Thompson /*
678*27e4bd81SHans Petter Selasky  * NOTE: These baud rates are officially supported, they can be written
679*27e4bd81SHans Petter Selasky  * directly into dwDTERate register.
680*27e4bd81SHans Petter Selasky  *
681*27e4bd81SHans Petter Selasky  * Free baudrate setting is not supported by the base PL2303, and on
682*27e4bd81SHans Petter Selasky  * other models it requires writing a divisor value to dwDTERate instead
683*27e4bd81SHans Petter Selasky  * of the raw baudrate. The formula for divisor calculation is not published
684*27e4bd81SHans Petter Selasky  * by the vendor, so it is speculative, though the official product homepage
685*27e4bd81SHans Petter Selasky  * refers to the Linux module source as a reference implementation.
68602ac6454SAndrew Thompson  */
687*27e4bd81SHans Petter Selasky static const uint32_t uplcom_rates[] = {
688*27e4bd81SHans Petter Selasky 	/*
689*27e4bd81SHans Petter Selasky 	 * Basic 'standard' speed rates, supported by all models
690*27e4bd81SHans Petter Selasky 	 * NOTE: 900 and 56000 actually works as well
691*27e4bd81SHans Petter Selasky 	 */
692*27e4bd81SHans Petter Selasky 	75, 150, 300, 600, 900, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
693*27e4bd81SHans Petter Selasky 	19200, 28800, 38400, 56000, 57600, 115200,
694*27e4bd81SHans Petter Selasky 	/*
695*27e4bd81SHans Petter Selasky 	 * Advanced speed rates up to 6Mbs, supported by HX/TA and HXD/TB/EA/RA
696*27e4bd81SHans Petter Selasky      * NOTE: regardless of the spec, 256000 does not work
697*27e4bd81SHans Petter Selasky 	 */
698*27e4bd81SHans Petter Selasky 	128000, 134400, 161280, 201600, 230400, 268800, 403200, 460800, 614400,
699*27e4bd81SHans Petter Selasky 	806400, 921600, 1228800, 2457600, 3000000, 6000000,
700*27e4bd81SHans Petter Selasky 	/*
701*27e4bd81SHans Petter Selasky 	 * Advanced speed rates up to 12, supported by HXD/TB/EA/RA
702*27e4bd81SHans Petter Selasky 	 */
703*27e4bd81SHans Petter Selasky 	12000000
70402ac6454SAndrew Thompson };
70502ac6454SAndrew Thompson 
706432157dcSPedro F. Giffuni #define	N_UPLCOM_RATES	nitems(uplcom_rates)
70702ac6454SAndrew Thompson 
70802ac6454SAndrew Thompson static int
709*27e4bd81SHans Petter Selasky uplcom_baud_supported(unsigned int speed)
710*27e4bd81SHans Petter Selasky {
711*27e4bd81SHans Petter Selasky 	int i;
712*27e4bd81SHans Petter Selasky 	for (i = 0; i < N_UPLCOM_RATES; i++) {
713*27e4bd81SHans Petter Selasky 		if (uplcom_rates[i] == speed)
714*27e4bd81SHans Petter Selasky 			return 1;
715*27e4bd81SHans Petter Selasky 	}
716*27e4bd81SHans Petter Selasky 	return 0;
717*27e4bd81SHans Petter Selasky }
718*27e4bd81SHans Petter Selasky 
719*27e4bd81SHans Petter Selasky static int
720760bc48eSAndrew Thompson uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
72102ac6454SAndrew Thompson {
722fe0f6362SGavin Atkinson 	struct uplcom_softc *sc = ucom->sc_parent;
72302ac6454SAndrew Thompson 
72402ac6454SAndrew Thompson 	DPRINTF("\n");
72502ac6454SAndrew Thompson 
726fe0f6362SGavin Atkinson 	/**
727fe0f6362SGavin Atkinson 	 * Check requested baud rate.
728fe0f6362SGavin Atkinson 	 *
729fe0f6362SGavin Atkinson 	 * The PL2303 can only set specific baud rates, up to 1228800 baud.
730*27e4bd81SHans Petter Selasky 	 * The PL2303HX can set any baud rate up to 6Mb.
731fe0f6362SGavin Atkinson 	 * The PL2303HX rev. D can set any baud rate up to 12Mb.
732fe0f6362SGavin Atkinson 	 *
733fe0f6362SGavin Atkinson 	 */
734*27e4bd81SHans Petter Selasky 
735*27e4bd81SHans Petter Selasky 	/* accept raw divisor data, if someone wants to do the math in user domain */
736*27e4bd81SHans Petter Selasky 	if (t->c_ospeed & 0x80000000)
737*27e4bd81SHans Petter Selasky 		return 0;
738*27e4bd81SHans Petter Selasky 	switch (sc->sc_chiptype) {
739*27e4bd81SHans Petter Selasky 		case TYPE_PL2303HXD:
740*27e4bd81SHans Petter Selasky 			if (t->c_ospeed <= 12000000)
741fe0f6362SGavin Atkinson 				return (0);
742*27e4bd81SHans Petter Selasky 			break;
743*27e4bd81SHans Petter Selasky 		case TYPE_PL2303HX:
744fe0f6362SGavin Atkinson 			if (t->c_ospeed <= 6000000)
745fe0f6362SGavin Atkinson 				return (0);
746*27e4bd81SHans Petter Selasky 			break;
747*27e4bd81SHans Petter Selasky 		default:
748*27e4bd81SHans Petter Selasky 			if (uplcom_baud_supported(t->c_ospeed))
749*27e4bd81SHans Petter Selasky 				return (0);
750*27e4bd81SHans Petter Selasky 			break;
75102ac6454SAndrew Thompson 	}
75202ac6454SAndrew Thompson 
753fe0f6362SGavin Atkinson 	DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed);
754fe0f6362SGavin Atkinson 	return (EIO);
75502ac6454SAndrew Thompson }
75602ac6454SAndrew Thompson 
757*27e4bd81SHans Petter Selasky static unsigned int
758*27e4bd81SHans Petter Selasky uplcom_encode_baud_rate_divisor(uint8_t *buf, unsigned int baud)
759*27e4bd81SHans Petter Selasky {
760*27e4bd81SHans Petter Selasky 	unsigned int baseline, mantissa, exponent;
761*27e4bd81SHans Petter Selasky 
762*27e4bd81SHans Petter Selasky 	/* Determine the baud rate divisor. This algorithm is taken from Linux. */
763*27e4bd81SHans Petter Selasky 	/*
764*27e4bd81SHans Petter Selasky 	 * Apparently the formula is:
765*27e4bd81SHans Petter Selasky 	 *   baudrate = baseline / (mantissa * 4^exponent)
766*27e4bd81SHans Petter Selasky 	 * where
767*27e4bd81SHans Petter Selasky 	 *   mantissa = buf[8:0]
768*27e4bd81SHans Petter Selasky 	 *   exponent = buf[11:9]
769*27e4bd81SHans Petter Selasky 	 */
770*27e4bd81SHans Petter Selasky 	if (baud == 0)
771*27e4bd81SHans Petter Selasky 		baud = 1;
772*27e4bd81SHans Petter Selasky 	baseline = 383385600;
773*27e4bd81SHans Petter Selasky 	mantissa = baseline / baud;
774*27e4bd81SHans Petter Selasky 	if (mantissa == 0)
775*27e4bd81SHans Petter Selasky 		mantissa = 1;
776*27e4bd81SHans Petter Selasky 	exponent = 0;
777*27e4bd81SHans Petter Selasky 	while (mantissa >= 512) {
778*27e4bd81SHans Petter Selasky 		if (exponent < 7) {
779*27e4bd81SHans Petter Selasky 			mantissa >>= 2;	/* divide by 4 */
780*27e4bd81SHans Petter Selasky 			exponent++;
781*27e4bd81SHans Petter Selasky 		} else {
782*27e4bd81SHans Petter Selasky 			/* Exponent is maxed. Trim mantissa and leave. This gives approx. 45.8 baud */
783*27e4bd81SHans Petter Selasky 			mantissa = 511;
784*27e4bd81SHans Petter Selasky 			break;
785*27e4bd81SHans Petter Selasky 		}
786*27e4bd81SHans Petter Selasky 	}
787*27e4bd81SHans Petter Selasky 
788*27e4bd81SHans Petter Selasky 	buf[3] = 0x80;
789*27e4bd81SHans Petter Selasky 	buf[2] = 0;
790*27e4bd81SHans Petter Selasky 	buf[1] = exponent << 1 | mantissa >> 8;
791*27e4bd81SHans Petter Selasky 	buf[0] = mantissa & 0xff;
792*27e4bd81SHans Petter Selasky 
793*27e4bd81SHans Petter Selasky 	/* Calculate and return the exact baud rate. */
794*27e4bd81SHans Petter Selasky 	baud = (baseline / mantissa) >> (exponent << 1);
795*27e4bd81SHans Petter Selasky 	DPRINTF("real baud rate will be %u\n", baud);
796*27e4bd81SHans Petter Selasky 
797*27e4bd81SHans Petter Selasky 	return baud;
798*27e4bd81SHans Petter Selasky }
79902ac6454SAndrew Thompson static void
800760bc48eSAndrew Thompson uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
80102ac6454SAndrew Thompson {
80202ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
803760bc48eSAndrew Thompson 	struct usb_cdc_line_state ls;
804760bc48eSAndrew Thompson 	struct usb_device_request req;
80502ac6454SAndrew Thompson 
80602ac6454SAndrew Thompson 	DPRINTF("sc = %p\n", sc);
80702ac6454SAndrew Thompson 
808271ae033SHans Petter Selasky 	memset(&ls, 0, sizeof(ls));
80902ac6454SAndrew Thompson 
810*27e4bd81SHans Petter Selasky 	/*
811*27e4bd81SHans Petter Selasky 	 * NOTE: If unsupported baud rates are set directly, the PL2303* uses 9600 baud.
812*27e4bd81SHans Petter Selasky 	 */
813*27e4bd81SHans Petter Selasky 	if ((t->c_ospeed & 0x80000000) || uplcom_baud_supported(t->c_ospeed))
81402ac6454SAndrew Thompson 		USETDW(ls.dwDTERate, t->c_ospeed);
815*27e4bd81SHans Petter Selasky 	else
816*27e4bd81SHans Petter Selasky 		t->c_ospeed = uplcom_encode_baud_rate_divisor((uint8_t*)&ls.dwDTERate, t->c_ospeed);
81702ac6454SAndrew Thompson 
81802ac6454SAndrew Thompson 	if (t->c_cflag & CSTOPB) {
819*27e4bd81SHans Petter Selasky 		if ((t->c_cflag & CSIZE) == CS5) {
820*27e4bd81SHans Petter Selasky 			/*
821*27e4bd81SHans Petter Selasky 			 * NOTE: Comply with "real" UARTs / RS232:
822*27e4bd81SHans Petter Selasky 			 *       use 1.5 instead of 2 stop bits with 5 data bits
823*27e4bd81SHans Petter Selasky 			 */
824*27e4bd81SHans Petter Selasky 			ls.bCharFormat = UCDC_STOP_BIT_1_5;
825*27e4bd81SHans Petter Selasky 		} else {
82602ac6454SAndrew Thompson 			ls.bCharFormat = UCDC_STOP_BIT_2;
827*27e4bd81SHans Petter Selasky 		}
82802ac6454SAndrew Thompson 	} else {
82902ac6454SAndrew Thompson 		ls.bCharFormat = UCDC_STOP_BIT_1;
83002ac6454SAndrew Thompson 	}
83102ac6454SAndrew Thompson 
83202ac6454SAndrew Thompson 	if (t->c_cflag & PARENB) {
83302ac6454SAndrew Thompson 		if (t->c_cflag & PARODD) {
83402ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_ODD;
83502ac6454SAndrew Thompson 		} else {
83602ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_EVEN;
83702ac6454SAndrew Thompson 		}
83802ac6454SAndrew Thompson 	} else {
83902ac6454SAndrew Thompson 		ls.bParityType = UCDC_PARITY_NONE;
84002ac6454SAndrew Thompson 	}
84102ac6454SAndrew Thompson 
84202ac6454SAndrew Thompson 	switch (t->c_cflag & CSIZE) {
84302ac6454SAndrew Thompson 	case CS5:
84402ac6454SAndrew Thompson 		ls.bDataBits = 5;
84502ac6454SAndrew Thompson 		break;
84602ac6454SAndrew Thompson 	case CS6:
84702ac6454SAndrew Thompson 		ls.bDataBits = 6;
84802ac6454SAndrew Thompson 		break;
84902ac6454SAndrew Thompson 	case CS7:
85002ac6454SAndrew Thompson 		ls.bDataBits = 7;
85102ac6454SAndrew Thompson 		break;
85202ac6454SAndrew Thompson 	case CS8:
85302ac6454SAndrew Thompson 		ls.bDataBits = 8;
85402ac6454SAndrew Thompson 		break;
85502ac6454SAndrew Thompson 	}
85602ac6454SAndrew Thompson 
857*27e4bd81SHans Petter Selasky 	DPRINTF("rate=0x%08x fmt=%d parity=%d bits=%d\n",
85802ac6454SAndrew Thompson 	    UGETDW(ls.dwDTERate), ls.bCharFormat,
85902ac6454SAndrew Thompson 	    ls.bParityType, ls.bDataBits);
86002ac6454SAndrew Thompson 
86102ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
86202ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_LINE_CODING;
86302ac6454SAndrew Thompson 	USETW(req.wValue, 0);
86402ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
86502ac6454SAndrew Thompson 	req.wIndex[1] = 0;
86602ac6454SAndrew Thompson 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
86702ac6454SAndrew Thompson 
868a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
86902ac6454SAndrew Thompson 	    &req, &ls, 0, 1000);
87002ac6454SAndrew Thompson 
87102ac6454SAndrew Thompson 	if (t->c_cflag & CRTSCTS) {
87202ac6454SAndrew Thompson 
87302ac6454SAndrew Thompson 		DPRINTF("crtscts = on\n");
87402ac6454SAndrew Thompson 
87502ac6454SAndrew Thompson 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
87602ac6454SAndrew Thompson 		req.bRequest = UPLCOM_SET_REQUEST;
87702ac6454SAndrew Thompson 		USETW(req.wValue, 0);
878*27e4bd81SHans Petter Selasky 		if (sc->sc_chiptype != TYPE_PL2303)
87902ac6454SAndrew Thompson 			USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
88002ac6454SAndrew Thompson 		else
88102ac6454SAndrew Thompson 			USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
88202ac6454SAndrew Thompson 		USETW(req.wLength, 0);
88302ac6454SAndrew Thompson 
884a593f6b8SAndrew Thompson 		ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
88502ac6454SAndrew Thompson 		    &req, NULL, 0, 1000);
88602ac6454SAndrew Thompson 	} else {
88702ac6454SAndrew Thompson 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
88802ac6454SAndrew Thompson 		req.bRequest = UPLCOM_SET_REQUEST;
88902ac6454SAndrew Thompson 		USETW(req.wValue, 0);
89002ac6454SAndrew Thompson 		USETW(req.wIndex, 0);
89102ac6454SAndrew Thompson 		USETW(req.wLength, 0);
892a593f6b8SAndrew Thompson 		ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
89302ac6454SAndrew Thompson 		    &req, NULL, 0, 1000);
89402ac6454SAndrew Thompson 	}
89502ac6454SAndrew Thompson }
89602ac6454SAndrew Thompson 
89702ac6454SAndrew Thompson static void
898760bc48eSAndrew Thompson uplcom_start_read(struct ucom_softc *ucom)
89902ac6454SAndrew Thompson {
90002ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
90102ac6454SAndrew Thompson 
90202ac6454SAndrew Thompson 	/* start interrupt endpoint */
903a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
90402ac6454SAndrew Thompson 
90502ac6454SAndrew Thompson 	/* start read endpoint */
906a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
90702ac6454SAndrew Thompson }
90802ac6454SAndrew Thompson 
90902ac6454SAndrew Thompson static void
910760bc48eSAndrew Thompson uplcom_stop_read(struct ucom_softc *ucom)
91102ac6454SAndrew Thompson {
91202ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
91302ac6454SAndrew Thompson 
91402ac6454SAndrew Thompson 	/* stop interrupt endpoint */
915a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
91602ac6454SAndrew Thompson 
91702ac6454SAndrew Thompson 	/* stop read endpoint */
918a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
91902ac6454SAndrew Thompson }
92002ac6454SAndrew Thompson 
92102ac6454SAndrew Thompson static void
922760bc48eSAndrew Thompson uplcom_start_write(struct ucom_softc *ucom)
92302ac6454SAndrew Thompson {
92402ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
92502ac6454SAndrew Thompson 
926a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
92702ac6454SAndrew Thompson }
92802ac6454SAndrew Thompson 
92902ac6454SAndrew Thompson static void
930760bc48eSAndrew Thompson uplcom_stop_write(struct ucom_softc *ucom)
93102ac6454SAndrew Thompson {
93202ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
93302ac6454SAndrew Thompson 
934a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
93502ac6454SAndrew Thompson }
93602ac6454SAndrew Thompson 
93702ac6454SAndrew Thompson static void
938760bc48eSAndrew Thompson uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
93902ac6454SAndrew Thompson {
94002ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
94102ac6454SAndrew Thompson 
94202ac6454SAndrew Thompson 	DPRINTF("\n");
94302ac6454SAndrew Thompson 
94402ac6454SAndrew Thompson 	*lsr = sc->sc_lsr;
94502ac6454SAndrew Thompson 	*msr = sc->sc_msr;
94602ac6454SAndrew Thompson }
94702ac6454SAndrew Thompson 
94802ac6454SAndrew Thompson static void
949ed6d949aSAndrew Thompson uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
95002ac6454SAndrew Thompson {
951ed6d949aSAndrew Thompson 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
952ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
95302ac6454SAndrew Thompson 	uint8_t buf[9];
954ed6d949aSAndrew Thompson 	int actlen;
955ed6d949aSAndrew Thompson 
956ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
95702ac6454SAndrew Thompson 
95802ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
95902ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
96002ac6454SAndrew Thompson 
961ed6d949aSAndrew Thompson 		DPRINTF("actlen = %u\n", actlen);
96202ac6454SAndrew Thompson 
963ed6d949aSAndrew Thompson 		if (actlen >= 9) {
96402ac6454SAndrew Thompson 
965ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
966ed6d949aSAndrew Thompson 			usbd_copy_out(pc, 0, buf, sizeof(buf));
96702ac6454SAndrew Thompson 
968*27e4bd81SHans Petter Selasky 			DPRINTF("status = 0x%02x\n", buf[UPLCOM_STATE_INDEX]);
96902ac6454SAndrew Thompson 
97002ac6454SAndrew Thompson 			sc->sc_lsr = 0;
97102ac6454SAndrew Thompson 			sc->sc_msr = 0;
97202ac6454SAndrew Thompson 
973*27e4bd81SHans Petter Selasky 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_CTS) {
97402ac6454SAndrew Thompson 				sc->sc_msr |= SER_CTS;
97502ac6454SAndrew Thompson 			}
976*27e4bd81SHans Petter Selasky 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_OVERRUN_ERROR) {
977*27e4bd81SHans Petter Selasky 				sc->sc_lsr |= ULSR_OE;
978*27e4bd81SHans Petter Selasky 			}
979*27e4bd81SHans Petter Selasky 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_PARITY_ERROR) {
980*27e4bd81SHans Petter Selasky 				sc->sc_lsr |= ULSR_PE;
981*27e4bd81SHans Petter Selasky 			}
982*27e4bd81SHans Petter Selasky 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_FRAME_ERROR) {
983*27e4bd81SHans Petter Selasky 				sc->sc_lsr |= ULSR_FE;
984*27e4bd81SHans Petter Selasky 			}
985*27e4bd81SHans Petter Selasky 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_RING) {
986*27e4bd81SHans Petter Selasky 				sc->sc_msr |= SER_RI;
987*27e4bd81SHans Petter Selasky 			}
988*27e4bd81SHans Petter Selasky 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_BREAK_ERROR) {
989*27e4bd81SHans Petter Selasky 				sc->sc_lsr |= ULSR_BI;
990*27e4bd81SHans Petter Selasky 			}
991*27e4bd81SHans Petter Selasky 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DSR) {
99202ac6454SAndrew Thompson 				sc->sc_msr |= SER_DSR;
99302ac6454SAndrew Thompson 			}
994*27e4bd81SHans Petter Selasky 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DCD) {
99502ac6454SAndrew Thompson 				sc->sc_msr |= SER_DCD;
99602ac6454SAndrew Thompson 			}
997a593f6b8SAndrew Thompson 			ucom_status_change(&sc->sc_ucom);
99802ac6454SAndrew Thompson 		}
99902ac6454SAndrew Thompson 	case USB_ST_SETUP:
100002ac6454SAndrew Thompson tr_setup:
1001ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1002a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
100302ac6454SAndrew Thompson 		return;
100402ac6454SAndrew Thompson 
100502ac6454SAndrew Thompson 	default:			/* Error */
1006ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
100702ac6454SAndrew Thompson 			/* try to clear stall first */
1008ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
100902ac6454SAndrew Thompson 			goto tr_setup;
101002ac6454SAndrew Thompson 		}
101102ac6454SAndrew Thompson 		return;
101202ac6454SAndrew Thompson 	}
101302ac6454SAndrew Thompson }
101402ac6454SAndrew Thompson 
101502ac6454SAndrew Thompson static void
1016ed6d949aSAndrew Thompson uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
101702ac6454SAndrew Thompson {
1018ed6d949aSAndrew Thompson 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1019ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
102002ac6454SAndrew Thompson 	uint32_t actlen;
102102ac6454SAndrew Thompson 
102202ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
102302ac6454SAndrew Thompson 	case USB_ST_SETUP:
102402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
102502ac6454SAndrew Thompson tr_setup:
1026ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
1027ed6d949aSAndrew Thompson 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
102802ac6454SAndrew Thompson 		    UPLCOM_BULK_BUF_SIZE, &actlen)) {
102902ac6454SAndrew Thompson 
103002ac6454SAndrew Thompson 			DPRINTF("actlen = %d\n", actlen);
103102ac6454SAndrew Thompson 
1032ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, actlen);
1033a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
103402ac6454SAndrew Thompson 		}
103502ac6454SAndrew Thompson 		return;
103602ac6454SAndrew Thompson 
103702ac6454SAndrew Thompson 	default:			/* Error */
1038ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
103902ac6454SAndrew Thompson 			/* try to clear stall first */
1040ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
104102ac6454SAndrew Thompson 			goto tr_setup;
104202ac6454SAndrew Thompson 		}
104302ac6454SAndrew Thompson 		return;
104402ac6454SAndrew Thompson 	}
104502ac6454SAndrew Thompson }
104602ac6454SAndrew Thompson 
104702ac6454SAndrew Thompson static void
1048ed6d949aSAndrew Thompson uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
104902ac6454SAndrew Thompson {
1050ed6d949aSAndrew Thompson 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1051ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
1052ed6d949aSAndrew Thompson 	int actlen;
1053ed6d949aSAndrew Thompson 
1054ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
105502ac6454SAndrew Thompson 
105602ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
105702ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
1058ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
1059ed6d949aSAndrew Thompson 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
106002ac6454SAndrew Thompson 
106102ac6454SAndrew Thompson 	case USB_ST_SETUP:
106202ac6454SAndrew Thompson tr_setup:
1063ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1064a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
106502ac6454SAndrew Thompson 		return;
106602ac6454SAndrew Thompson 
106702ac6454SAndrew Thompson 	default:			/* Error */
1068ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
106902ac6454SAndrew Thompson 			/* try to clear stall first */
1070ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
107102ac6454SAndrew Thompson 			goto tr_setup;
107202ac6454SAndrew Thompson 		}
107302ac6454SAndrew Thompson 		return;
107402ac6454SAndrew Thompson 	}
107502ac6454SAndrew Thompson }
1076655dc9d0SAndrew Thompson 
1077655dc9d0SAndrew Thompson static void
1078655dc9d0SAndrew Thompson uplcom_poll(struct ucom_softc *ucom)
1079655dc9d0SAndrew Thompson {
1080655dc9d0SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
1081655dc9d0SAndrew Thompson 	usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);
1082655dc9d0SAndrew Thompson }
1083