xref: /freebsd/sys/dev/usb/serial/uplcom.c (revision f8d2b1f3f72086bd169f1a0890b6faba8434a01a)
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 
116*f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
117*f8d2b1f3SPawel Biernacki     "USB uplcom");
118ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RWTUN,
11902ac6454SAndrew Thompson     &uplcom_debug, 0, "Debug level");
12002ac6454SAndrew Thompson #endif
12102ac6454SAndrew Thompson 
12202ac6454SAndrew Thompson #define	UPLCOM_MODVER			1	/* module version */
12302ac6454SAndrew Thompson 
12402ac6454SAndrew Thompson #define	UPLCOM_CONFIG_INDEX		0
12502ac6454SAndrew Thompson #define	UPLCOM_IFACE_INDEX		0
12602ac6454SAndrew Thompson #define	UPLCOM_SECOND_IFACE_INDEX	1
12702ac6454SAndrew Thompson 
12802ac6454SAndrew Thompson #ifndef UPLCOM_INTR_INTERVAL
12902ac6454SAndrew Thompson #define	UPLCOM_INTR_INTERVAL		0	/* default */
13002ac6454SAndrew Thompson #endif
13102ac6454SAndrew Thompson 
13202ac6454SAndrew Thompson #define	UPLCOM_BULK_BUF_SIZE 1024	/* bytes */
13302ac6454SAndrew Thompson 
13402ac6454SAndrew Thompson #define	UPLCOM_SET_REQUEST		0x01
13502ac6454SAndrew Thompson #define	UPLCOM_SET_CRTSCTS		0x41
13602ac6454SAndrew Thompson #define	UPLCOM_SET_CRTSCTS_PL2303X	0x61
13702ac6454SAndrew Thompson #define	RSAQ_STATUS_CTS			0x80
13827e4bd81SHans Petter Selasky #define	RSAQ_STATUS_OVERRUN_ERROR	0x40
13927e4bd81SHans Petter Selasky #define	RSAQ_STATUS_PARITY_ERROR	0x20
14027e4bd81SHans Petter Selasky #define	RSAQ_STATUS_FRAME_ERROR	0x10
14127e4bd81SHans Petter Selasky #define	RSAQ_STATUS_RING		0x08
14227e4bd81SHans Petter Selasky #define	RSAQ_STATUS_BREAK_ERROR	0x04
14302ac6454SAndrew Thompson #define	RSAQ_STATUS_DSR			0x02
14402ac6454SAndrew Thompson #define	RSAQ_STATUS_DCD			0x01
14502ac6454SAndrew Thompson 
14602ac6454SAndrew Thompson #define	TYPE_PL2303			0
147bc65068dSGavin Atkinson #define	TYPE_PL2303HX			1
14827e4bd81SHans Petter Selasky #define	TYPE_PL2303HXD			2
14927e4bd81SHans Petter Selasky 
15027e4bd81SHans Petter Selasky #define	UPLCOM_STATE_INDEX		8
15102ac6454SAndrew Thompson 
15202ac6454SAndrew Thompson enum {
15302ac6454SAndrew Thompson 	UPLCOM_BULK_DT_WR,
15402ac6454SAndrew Thompson 	UPLCOM_BULK_DT_RD,
15502ac6454SAndrew Thompson 	UPLCOM_INTR_DT_RD,
15602ac6454SAndrew Thompson 	UPLCOM_N_TRANSFER,
15702ac6454SAndrew Thompson };
15802ac6454SAndrew Thompson 
15902ac6454SAndrew Thompson struct uplcom_softc {
160760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
161760bc48eSAndrew Thompson 	struct ucom_softc sc_ucom;
16202ac6454SAndrew Thompson 
163760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER];
164760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
165deefe583SAndrew Thompson 	struct mtx sc_mtx;
16602ac6454SAndrew Thompson 
16702ac6454SAndrew Thompson 	uint16_t sc_line;
16802ac6454SAndrew Thompson 
16902ac6454SAndrew Thompson 	uint8_t	sc_lsr;			/* local status register */
17002ac6454SAndrew Thompson 	uint8_t	sc_msr;			/* uplcom status register */
17102ac6454SAndrew Thompson 	uint8_t	sc_chiptype;		/* type of chip */
17202ac6454SAndrew Thompson 	uint8_t	sc_ctrl_iface_no;
17302ac6454SAndrew Thompson 	uint8_t	sc_data_iface_no;
17402ac6454SAndrew Thompson 	uint8_t	sc_iface_index[2];
17502ac6454SAndrew Thompson };
17602ac6454SAndrew Thompson 
17702ac6454SAndrew Thompson /* prototypes */
17802ac6454SAndrew Thompson 
179e0a69b51SAndrew Thompson static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *);
18002d4a225SDimitry Andric static usb_error_t uplcom_pl2303_do(struct usb_device *, uint8_t, uint8_t,
181bc65068dSGavin Atkinson 			uint16_t, uint16_t, uint16_t);
182bc65068dSGavin Atkinson static int	uplcom_pl2303_init(struct usb_device *, uint8_t);
1835805d178SHans Petter Selasky static void	uplcom_free(struct ucom_softc *);
184760bc48eSAndrew Thompson static void	uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
185760bc48eSAndrew Thompson static void	uplcom_cfg_set_rts(struct ucom_softc *, uint8_t);
186760bc48eSAndrew Thompson static void	uplcom_cfg_set_break(struct ucom_softc *, uint8_t);
187760bc48eSAndrew Thompson static int	uplcom_pre_param(struct ucom_softc *, struct termios *);
188760bc48eSAndrew Thompson static void	uplcom_cfg_param(struct ucom_softc *, struct termios *);
189760bc48eSAndrew Thompson static void	uplcom_start_read(struct ucom_softc *);
190760bc48eSAndrew Thompson static void	uplcom_stop_read(struct ucom_softc *);
191760bc48eSAndrew Thompson static void	uplcom_start_write(struct ucom_softc *);
192760bc48eSAndrew Thompson static void	uplcom_stop_write(struct ucom_softc *);
193760bc48eSAndrew Thompson static void	uplcom_cfg_get_status(struct ucom_softc *, uint8_t *,
19402ac6454SAndrew Thompson 		    uint8_t *);
195655dc9d0SAndrew Thompson static void	uplcom_poll(struct ucom_softc *ucom);
19602ac6454SAndrew Thompson 
19702ac6454SAndrew Thompson static device_probe_t uplcom_probe;
19802ac6454SAndrew Thompson static device_attach_t uplcom_attach;
19902ac6454SAndrew Thompson static device_detach_t uplcom_detach;
200c01fc06eSHans Petter Selasky static void uplcom_free_softc(struct uplcom_softc *);
20102ac6454SAndrew Thompson 
202e0a69b51SAndrew Thompson static usb_callback_t uplcom_intr_callback;
203e0a69b51SAndrew Thompson static usb_callback_t uplcom_write_callback;
204e0a69b51SAndrew Thompson static usb_callback_t uplcom_read_callback;
20502ac6454SAndrew Thompson 
206760bc48eSAndrew Thompson static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
20702ac6454SAndrew Thompson 
20802ac6454SAndrew Thompson 	[UPLCOM_BULK_DT_WR] = {
20902ac6454SAndrew Thompson 		.type = UE_BULK,
21002ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
21102ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
2124eae601eSAndrew Thompson 		.bufsize = UPLCOM_BULK_BUF_SIZE,
2134eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2144eae601eSAndrew Thompson 		.callback = &uplcom_write_callback,
21502ac6454SAndrew Thompson 		.if_index = 0,
21602ac6454SAndrew Thompson 	},
21702ac6454SAndrew Thompson 
21802ac6454SAndrew Thompson 	[UPLCOM_BULK_DT_RD] = {
21902ac6454SAndrew Thompson 		.type = UE_BULK,
22002ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
22102ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2224eae601eSAndrew Thompson 		.bufsize = UPLCOM_BULK_BUF_SIZE,
2234eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2244eae601eSAndrew Thompson 		.callback = &uplcom_read_callback,
22502ac6454SAndrew Thompson 		.if_index = 0,
22602ac6454SAndrew Thompson 	},
22702ac6454SAndrew Thompson 
22802ac6454SAndrew Thompson 	[UPLCOM_INTR_DT_RD] = {
22902ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
23002ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
23102ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2324eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2334eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
2344eae601eSAndrew Thompson 		.callback = &uplcom_intr_callback,
23502ac6454SAndrew Thompson 		.if_index = 1,
23602ac6454SAndrew Thompson 	},
23702ac6454SAndrew Thompson };
23802ac6454SAndrew Thompson 
239a8675caeSAndrew Thompson static struct ucom_callback uplcom_callback = {
240a593f6b8SAndrew Thompson 	.ucom_cfg_get_status = &uplcom_cfg_get_status,
241a593f6b8SAndrew Thompson 	.ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
242a593f6b8SAndrew Thompson 	.ucom_cfg_set_rts = &uplcom_cfg_set_rts,
243a593f6b8SAndrew Thompson 	.ucom_cfg_set_break = &uplcom_cfg_set_break,
244a593f6b8SAndrew Thompson 	.ucom_cfg_param = &uplcom_cfg_param,
245a593f6b8SAndrew Thompson 	.ucom_pre_param = &uplcom_pre_param,
246a593f6b8SAndrew Thompson 	.ucom_start_read = &uplcom_start_read,
247a593f6b8SAndrew Thompson 	.ucom_stop_read = &uplcom_stop_read,
248a593f6b8SAndrew Thompson 	.ucom_start_write = &uplcom_start_write,
249a593f6b8SAndrew Thompson 	.ucom_stop_write = &uplcom_stop_write,
250655dc9d0SAndrew Thompson 	.ucom_poll = &uplcom_poll,
2515805d178SHans Petter Selasky 	.ucom_free = &uplcom_free,
25202ac6454SAndrew Thompson };
25302ac6454SAndrew Thompson 
254bc65068dSGavin Atkinson #define	UPLCOM_DEV(v,p)				\
255bc65068dSGavin Atkinson   { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p) }
25602ac6454SAndrew Thompson 
257f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID uplcom_devs[] = {
258f5113df9SGavin Atkinson 	UPLCOM_DEV(ACERP, S81),			/* BenQ S81 phone */
259f5113df9SGavin Atkinson 	UPLCOM_DEV(ADLINK, ND6530),		/* ADLINK ND-6530 USB-Serial */
260f5113df9SGavin Atkinson 	UPLCOM_DEV(ALCATEL, OT535),		/* Alcatel One Touch 535/735 */
261f5113df9SGavin Atkinson 	UPLCOM_DEV(ALCOR, AU9720),		/* Alcor AU9720 USB 2.0-RS232 */
262f5113df9SGavin Atkinson 	UPLCOM_DEV(ANCHOR, SERIAL),		/* Anchor Serial adapter */
263bc65068dSGavin Atkinson 	UPLCOM_DEV(ATEN, UC232A),		/* PLANEX USB-RS232 URS-03 */
26401a8f075SGavin Atkinson 	UPLCOM_DEV(BELKIN, F5U257),		/* Belkin F5U257 USB to Serial */
265bc65068dSGavin Atkinson 	UPLCOM_DEV(COREGA, CGUSBRS232R),	/* Corega CG-USBRS232R */
266f5113df9SGavin Atkinson 	UPLCOM_DEV(EPSON, CRESSI_EDY),		/* Cressi Edy diving computer */
267c6ac426dSGavin Atkinson 	UPLCOM_DEV(EPSON, N2ITION3),		/* Zeagle N2iTion3 diving computer */
26801a8f075SGavin Atkinson 	UPLCOM_DEV(ELECOM, UCSGT),		/* ELECOM UC-SGT Serial Adapter */
26901a8f075SGavin Atkinson 	UPLCOM_DEV(ELECOM, UCSGT0),		/* ELECOM UC-SGT Serial Adapter */
270bc65068dSGavin Atkinson 	UPLCOM_DEV(HAL, IMR001),		/* HAL Corporation Crossam2+USB */
271f5113df9SGavin Atkinson 	UPLCOM_DEV(HP, LD220),			/* HP LD220 POS Display */
272bc65068dSGavin Atkinson 	UPLCOM_DEV(IODATA, USBRSAQ),		/* I/O DATA USB-RSAQ */
273bc65068dSGavin Atkinson 	UPLCOM_DEV(IODATA, USBRSAQ5),		/* I/O DATA USB-RSAQ5 */
274f5113df9SGavin Atkinson 	UPLCOM_DEV(ITEGNO, WM1080A),		/* iTegno WM1080A GSM/GFPRS modem */
275f5113df9SGavin Atkinson 	UPLCOM_DEV(ITEGNO, WM2080A),		/* iTegno WM2080A CDMA modem */
276f5113df9SGavin Atkinson 	UPLCOM_DEV(LEADTEK, 9531),		/* Leadtek 9531 GPS */
277f5113df9SGavin Atkinson 	UPLCOM_DEV(MICROSOFT, 700WX),		/* Microsoft Palm 700WX */
278bc65068dSGavin Atkinson 	UPLCOM_DEV(MOBILEACTION, MA620),	/* Mobile Action MA-620 Infrared Adapter */
279f5113df9SGavin Atkinson 	UPLCOM_DEV(NETINDEX, WS002IN),		/* Willcom W-S002IN */
280f5113df9SGavin Atkinson 	UPLCOM_DEV(NOKIA2, CA42),		/* Nokia CA-42 cable */
281f5113df9SGavin Atkinson 	UPLCOM_DEV(OTI, DKU5),			/* OTI DKU-5 cable */
282f5113df9SGavin Atkinson 	UPLCOM_DEV(PANASONIC, TYTP50P6S),	/* Panasonic TY-TP50P6-S flat screen */
283f5113df9SGavin Atkinson 	UPLCOM_DEV(PLX, CA42),			/* PLX CA-42 clone cable */
284f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, ALLTRONIX_GPRS),	/* Alltronix ACM003U00 modem */
285f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, ALDIGA_AL11U),	/* AlDiga AL-11U modem */
286f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, DCU11),		/* DCU-11 Phone Cable */
287f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, HCR331),		/* HCR331 Card Reader */
288f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, MICROMAX_610U),	/* Micromax 610U modem */
289d01755edSGavin Atkinson 	UPLCOM_DEV(PROLIFIC, MOTOROLA),		/* Motorola cable */
290bc65068dSGavin Atkinson 	UPLCOM_DEV(PROLIFIC, PHAROS),		/* Prolific Pharos */
291f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, PL2303),		/* Generic adapter */
292bc65068dSGavin Atkinson 	UPLCOM_DEV(PROLIFIC, RSAQ2),		/* I/O DATA USB-RSAQ2 */
293bc65068dSGavin Atkinson 	UPLCOM_DEV(PROLIFIC, RSAQ3),		/* I/O DATA USB-RSAQ3 */
294635c9457SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, UIC_MSR206),	/* UIC MSR206 Card Reader */
295f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC2, PL2303),		/* Prolific adapter */
29601a8f075SGavin Atkinson 	UPLCOM_DEV(RADIOSHACK, USBCABLE),	/* Radio Shack USB Adapter */
297bc65068dSGavin Atkinson 	UPLCOM_DEV(RATOC, REXUSB60),		/* RATOC REX-USB60 */
298bc65068dSGavin Atkinson 	UPLCOM_DEV(SAGEM, USBSERIAL),		/* Sagem USB-Serial Controller */
299f5113df9SGavin Atkinson 	UPLCOM_DEV(SAMSUNG, I330),		/* Samsung I330 phone cradle */
300f5113df9SGavin Atkinson 	UPLCOM_DEV(SANWA, KB_USB2),		/* Sanwa KB-USB2 Multimeter cable */
30101a8f075SGavin Atkinson 	UPLCOM_DEV(SIEMENS3, EF81),		/* Siemens EF81 */
30201a8f075SGavin Atkinson 	UPLCOM_DEV(SIEMENS3, SX1),		/* Siemens SX1 */
30301a8f075SGavin Atkinson 	UPLCOM_DEV(SIEMENS3, X65),		/* Siemens X65 */
30401a8f075SGavin Atkinson 	UPLCOM_DEV(SIEMENS3, X75),		/* Siemens X75 */
305bc65068dSGavin Atkinson 	UPLCOM_DEV(SITECOM, SERIAL),		/* Sitecom USB to Serial */
30601a8f075SGavin Atkinson 	UPLCOM_DEV(SMART, PL2303),		/* SMART Technologies USB to Serial */
307f5113df9SGavin Atkinson 	UPLCOM_DEV(SONY, QN3),			/* Sony QN3 phone cable */
308f5113df9SGavin Atkinson 	UPLCOM_DEV(SONYERICSSON, DATAPILOT),	/* Sony Ericsson Datapilot */
309f5113df9SGavin Atkinson 	UPLCOM_DEV(SONYERICSSON, DCU10),	/* Sony Ericsson DCU-10 Cable */
310bc65068dSGavin Atkinson 	UPLCOM_DEV(SOURCENEXT, KEIKAI8),	/* SOURCENEXT KeikaiDenwa 8 */
311bc65068dSGavin Atkinson 	UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG),	/* SOURCENEXT KeikaiDenwa 8 with charger */
312f5113df9SGavin Atkinson 	UPLCOM_DEV(SPEEDDRAGON, MS3303H),	/* Speed Dragon USB-Serial */
313f5113df9SGavin Atkinson 	UPLCOM_DEV(SYNTECH, CPT8001C),		/* Syntech CPT-8001C Barcode scanner */
314bc65068dSGavin Atkinson 	UPLCOM_DEV(TDK, UHA6400),		/* TDK USB-PHS Adapter UHA6400 */
315f5113df9SGavin Atkinson 	UPLCOM_DEV(TDK, UPA9664),		/* TDK USB-PHS Adapter UPA9664 */
31601a8f075SGavin Atkinson 	UPLCOM_DEV(TRIPPLITE, U209),		/* Tripp-Lite U209-000-R USB to Serial */
317f5113df9SGavin Atkinson 	UPLCOM_DEV(YCCABLE, PL2303),		/* YC Cable USB-Serial */
31802ac6454SAndrew Thompson };
3199e6b5313SAndrew Thompson #undef UPLCOM_DEV
32002ac6454SAndrew Thompson 
32102ac6454SAndrew Thompson static device_method_t uplcom_methods[] = {
32202ac6454SAndrew Thompson 	DEVMETHOD(device_probe, uplcom_probe),
32302ac6454SAndrew Thompson 	DEVMETHOD(device_attach, uplcom_attach),
32402ac6454SAndrew Thompson 	DEVMETHOD(device_detach, uplcom_detach),
3255805d178SHans Petter Selasky 	DEVMETHOD_END
32602ac6454SAndrew Thompson };
32702ac6454SAndrew Thompson 
32802ac6454SAndrew Thompson static devclass_t uplcom_devclass;
32902ac6454SAndrew Thompson 
33002ac6454SAndrew Thompson static driver_t uplcom_driver = {
33102ac6454SAndrew Thompson 	.name = "uplcom",
33202ac6454SAndrew Thompson 	.methods = uplcom_methods,
33302ac6454SAndrew Thompson 	.size = sizeof(struct uplcom_softc),
33402ac6454SAndrew Thompson };
33502ac6454SAndrew Thompson 
3369aef556dSAndrew Thompson DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0);
33702ac6454SAndrew Thompson MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
33802ac6454SAndrew Thompson MODULE_DEPEND(uplcom, usb, 1, 1, 1);
33902ac6454SAndrew Thompson MODULE_VERSION(uplcom, UPLCOM_MODVER);
340f809f280SWarner Losh USB_PNP_HOST_INFO(uplcom_devs);
34102ac6454SAndrew Thompson 
34202ac6454SAndrew Thompson static int
34302ac6454SAndrew Thompson uplcom_probe(device_t dev)
34402ac6454SAndrew Thompson {
345760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
34602ac6454SAndrew Thompson 
34702ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
34802ac6454SAndrew Thompson 
349f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
35002ac6454SAndrew Thompson 		return (ENXIO);
35102ac6454SAndrew Thompson 	}
35202ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
35302ac6454SAndrew Thompson 		return (ENXIO);
35402ac6454SAndrew Thompson 	}
35502ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
35602ac6454SAndrew Thompson 		return (ENXIO);
35702ac6454SAndrew Thompson 	}
358a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
35902ac6454SAndrew Thompson }
36002ac6454SAndrew Thompson 
36102ac6454SAndrew Thompson static int
36202ac6454SAndrew Thompson uplcom_attach(device_t dev)
36302ac6454SAndrew Thompson {
364760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
36502ac6454SAndrew Thompson 	struct uplcom_softc *sc = device_get_softc(dev);
366760bc48eSAndrew Thompson 	struct usb_interface *iface;
367760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
368bc65068dSGavin Atkinson 	struct usb_device_descriptor *dd;
36902ac6454SAndrew Thompson 	int error;
37002ac6454SAndrew Thompson 
37102ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
37202ac6454SAndrew Thompson 
373a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
374deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
3755805d178SHans Petter Selasky 	ucom_ref(&sc->sc_super_ucom);
37602ac6454SAndrew Thompson 
37702ac6454SAndrew Thompson 	DPRINTF("sc = %p\n", sc);
37802ac6454SAndrew Thompson 
37902ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
38002ac6454SAndrew Thompson 
381bc65068dSGavin Atkinson 	dd = usbd_get_device_descriptor(sc->sc_udev);
38227e4bd81SHans Petter Selasky 
38327e4bd81SHans Petter Selasky 	switch (UGETW(dd->bcdDevice)) {
38427e4bd81SHans Petter Selasky 	case 0x0300:
38527e4bd81SHans Petter Selasky 		sc->sc_chiptype = TYPE_PL2303HX;
38627e4bd81SHans Petter Selasky 		/* or TA, that is HX with external crystal */
38727e4bd81SHans Petter Selasky 		break;
38827e4bd81SHans Petter Selasky 	case 0x0400:
38927e4bd81SHans Petter Selasky 		sc->sc_chiptype = TYPE_PL2303HXD;
39027e4bd81SHans Petter Selasky 		/* or EA, that is HXD with ESD protection */
39127e4bd81SHans Petter Selasky 		/* or RA, that has internal voltage level converter that works only up to 1Mbaud (!) */
39227e4bd81SHans Petter Selasky 		break;
39327e4bd81SHans Petter Selasky 	case 0x0500:
39427e4bd81SHans Petter Selasky 		sc->sc_chiptype = TYPE_PL2303HXD;
39527e4bd81SHans Petter Selasky 		/* in fact it's TB, that is HXD with external crystal */
39627e4bd81SHans Petter Selasky 		break;
39727e4bd81SHans Petter Selasky 	default:
39827e4bd81SHans Petter Selasky 		/* NOTE: I have no info about the bcdDevice for the base PL2303 (up to 1.2Mbaud,
39927e4bd81SHans Petter Selasky 		   only fixed rates) and for PL2303SA (8-pin chip, up to 115200 baud */
40027e4bd81SHans Petter Selasky 		/* Determine the chip type.  This algorithm is taken from Linux. */
401bc65068dSGavin Atkinson 		if (dd->bDeviceClass == 0x02)
402bc65068dSGavin Atkinson 			sc->sc_chiptype = TYPE_PL2303;
403bc65068dSGavin Atkinson 		else if (dd->bMaxPacketSize == 0x40)
404bc65068dSGavin Atkinson 			sc->sc_chiptype = TYPE_PL2303HX;
405bc65068dSGavin Atkinson 		else
406bc65068dSGavin Atkinson 			sc->sc_chiptype = TYPE_PL2303;
40727e4bd81SHans Petter Selasky 		break;
40827e4bd81SHans Petter Selasky 	}
409bc65068dSGavin Atkinson 
41027e4bd81SHans Petter Selasky 	switch (sc->sc_chiptype) {
41127e4bd81SHans Petter Selasky 	case TYPE_PL2303:
41227e4bd81SHans Petter Selasky 		DPRINTF("chiptype: 2303\n");
41327e4bd81SHans Petter Selasky 		break;
41427e4bd81SHans Petter Selasky 	case TYPE_PL2303HX:
41527e4bd81SHans Petter Selasky 		DPRINTF("chiptype: 2303HX/TA\n");
41627e4bd81SHans Petter Selasky 		break;
41727e4bd81SHans Petter Selasky 	case TYPE_PL2303HXD:
41827e4bd81SHans Petter Selasky 		DPRINTF("chiptype: 2303HXD/TB/RA/EA\n");
41927e4bd81SHans Petter Selasky 		break;
42027e4bd81SHans Petter Selasky 	default:
42127e4bd81SHans Petter Selasky 		DPRINTF("chiptype: unknown %d\n", sc->sc_chiptype);
42227e4bd81SHans Petter Selasky 		break;
42327e4bd81SHans Petter Selasky 	}
42402ac6454SAndrew Thompson 
42502ac6454SAndrew Thompson 	/*
42602ac6454SAndrew Thompson 	 * USB-RSAQ1 has two interface
42702ac6454SAndrew Thompson 	 *
42802ac6454SAndrew Thompson 	 *  USB-RSAQ1       | USB-RSAQ2
42902ac6454SAndrew Thompson 	 * -----------------+-----------------
43002ac6454SAndrew Thompson 	 * Interface 0      |Interface 0
43102ac6454SAndrew Thompson 	 *  Interrupt(0x81) | Interrupt(0x81)
43202ac6454SAndrew Thompson 	 * -----------------+ BulkIN(0x02)
43302ac6454SAndrew Thompson 	 * Interface 1	    | BulkOUT(0x83)
43402ac6454SAndrew Thompson 	 *   BulkIN(0x02)   |
43502ac6454SAndrew Thompson 	 *   BulkOUT(0x83)  |
43602ac6454SAndrew Thompson 	 */
43702ac6454SAndrew Thompson 
43802ac6454SAndrew Thompson 	sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
43902ac6454SAndrew Thompson 	sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
44002ac6454SAndrew Thompson 
441a593f6b8SAndrew Thompson 	iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
44202ac6454SAndrew Thompson 	if (iface) {
443a593f6b8SAndrew Thompson 		id = usbd_get_interface_descriptor(iface);
44402ac6454SAndrew Thompson 		if (id == NULL) {
445767cb2e2SAndrew Thompson 			device_printf(dev, "no interface descriptor (2)\n");
44602ac6454SAndrew Thompson 			goto detach;
44702ac6454SAndrew Thompson 		}
44802ac6454SAndrew Thompson 		sc->sc_data_iface_no = id->bInterfaceNumber;
44902ac6454SAndrew Thompson 		sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
450a593f6b8SAndrew Thompson 		usbd_set_parent_iface(uaa->device,
45102ac6454SAndrew Thompson 		    UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
45202ac6454SAndrew Thompson 	} else {
45302ac6454SAndrew Thompson 		sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
45402ac6454SAndrew Thompson 		sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
45502ac6454SAndrew Thompson 	}
45602ac6454SAndrew Thompson 
457a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device,
45802ac6454SAndrew Thompson 	    sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
459deefe583SAndrew Thompson 	    UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
46002ac6454SAndrew Thompson 	if (error) {
46102ac6454SAndrew Thompson 		DPRINTF("one or more missing USB endpoints, "
462a593f6b8SAndrew Thompson 		    "error=%s\n", usbd_errstr(error));
46302ac6454SAndrew Thompson 		goto detach;
46402ac6454SAndrew Thompson 	}
46502ac6454SAndrew Thompson 	error = uplcom_reset(sc, uaa->device);
46602ac6454SAndrew Thompson 	if (error) {
46702ac6454SAndrew Thompson 		device_printf(dev, "reset failed, error=%s\n",
468a593f6b8SAndrew Thompson 		    usbd_errstr(error));
46902ac6454SAndrew Thompson 		goto detach;
47002ac6454SAndrew Thompson 	}
4719efb7339SHans Petter Selasky 
47227e4bd81SHans Petter Selasky 	if (sc->sc_chiptype == TYPE_PL2303) {
4739efb7339SHans Petter Selasky 		/* HX variants seem to lock up after a clear stall request. */
474deefe583SAndrew Thompson 		mtx_lock(&sc->sc_mtx);
475ed6d949aSAndrew Thompson 		usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
476ed6d949aSAndrew Thompson 		usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
477deefe583SAndrew Thompson 		mtx_unlock(&sc->sc_mtx);
4789efb7339SHans Petter Selasky 	} else {
47927e4bd81SHans Petter Selasky 		/* reset upstream data pipes */
4809efb7339SHans Petter Selasky 		if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
4819efb7339SHans Petter Selasky 		    UPLCOM_SET_REQUEST, 8, 0, 0) ||
4829efb7339SHans Petter Selasky 		    uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
4839efb7339SHans Petter Selasky 		    UPLCOM_SET_REQUEST, 9, 0, 0)) {
4849efb7339SHans Petter Selasky 			goto detach;
4859efb7339SHans Petter Selasky 		}
4869efb7339SHans Petter Selasky 	}
48702ac6454SAndrew Thompson 
488a593f6b8SAndrew Thompson 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
489deefe583SAndrew Thompson 	    &uplcom_callback, &sc->sc_mtx);
49002ac6454SAndrew Thompson 	if (error) {
49102ac6454SAndrew Thompson 		goto detach;
49202ac6454SAndrew Thompson 	}
49302ac6454SAndrew Thompson 	/*
49402ac6454SAndrew Thompson 	 * do the initialization during attach so that the system does not
49502ac6454SAndrew Thompson 	 * sleep during open:
49602ac6454SAndrew Thompson 	 */
497bc65068dSGavin Atkinson 	if (uplcom_pl2303_init(uaa->device, sc->sc_chiptype)) {
498767cb2e2SAndrew Thompson 		device_printf(dev, "init failed\n");
49902ac6454SAndrew Thompson 		goto detach;
50002ac6454SAndrew Thompson 	}
5016416c259SNick Hibma 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
5026416c259SNick Hibma 
50302ac6454SAndrew Thompson 	return (0);
50402ac6454SAndrew Thompson 
50502ac6454SAndrew Thompson detach:
50602ac6454SAndrew Thompson 	uplcom_detach(dev);
50702ac6454SAndrew Thompson 	return (ENXIO);
50802ac6454SAndrew Thompson }
50902ac6454SAndrew Thompson 
51002ac6454SAndrew Thompson static int
51102ac6454SAndrew Thompson uplcom_detach(device_t dev)
51202ac6454SAndrew Thompson {
51302ac6454SAndrew Thompson 	struct uplcom_softc *sc = device_get_softc(dev);
51402ac6454SAndrew Thompson 
51502ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
51602ac6454SAndrew Thompson 
517015bb88fSNick Hibma 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
518a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
51902ac6454SAndrew Thompson 
520c01fc06eSHans Petter Selasky 	device_claim_softc(dev);
521c01fc06eSHans Petter Selasky 
522c01fc06eSHans Petter Selasky 	uplcom_free_softc(sc);
523c01fc06eSHans Petter Selasky 
52402ac6454SAndrew Thompson 	return (0);
52502ac6454SAndrew Thompson }
52602ac6454SAndrew Thompson 
5275805d178SHans Petter Selasky UCOM_UNLOAD_DRAIN(uplcom);
5285805d178SHans Petter Selasky 
5295805d178SHans Petter Selasky static void
530c01fc06eSHans Petter Selasky uplcom_free_softc(struct uplcom_softc *sc)
5315805d178SHans Petter Selasky {
5325805d178SHans Petter Selasky 	if (ucom_unref(&sc->sc_super_ucom)) {
5335805d178SHans Petter Selasky 		mtx_destroy(&sc->sc_mtx);
534c01fc06eSHans Petter Selasky 		device_free_softc(sc);
5355805d178SHans Petter Selasky 	}
5365805d178SHans Petter Selasky }
5375805d178SHans Petter Selasky 
5385805d178SHans Petter Selasky static void
5395805d178SHans Petter Selasky uplcom_free(struct ucom_softc *ucom)
5405805d178SHans Petter Selasky {
541c01fc06eSHans Petter Selasky 	uplcom_free_softc(ucom->sc_parent);
5425805d178SHans Petter Selasky }
5435805d178SHans Petter Selasky 
544e0a69b51SAndrew Thompson static usb_error_t
545760bc48eSAndrew Thompson uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
54602ac6454SAndrew Thompson {
547760bc48eSAndrew Thompson 	struct usb_device_request req;
54802ac6454SAndrew Thompson 
54902ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
55002ac6454SAndrew Thompson 	req.bRequest = UPLCOM_SET_REQUEST;
55102ac6454SAndrew Thompson 	USETW(req.wValue, 0);
55202ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
55302ac6454SAndrew Thompson 	req.wIndex[1] = 0;
55402ac6454SAndrew Thompson 	USETW(req.wLength, 0);
55502ac6454SAndrew Thompson 
556a593f6b8SAndrew Thompson 	return (usbd_do_request(udev, NULL, &req, NULL));
55702ac6454SAndrew Thompson }
55802ac6454SAndrew Thompson 
559bc65068dSGavin Atkinson static usb_error_t
56002d4a225SDimitry Andric uplcom_pl2303_do(struct usb_device *udev, uint8_t req_type, uint8_t request,
561bc65068dSGavin Atkinson     uint16_t value, uint16_t index, uint16_t length)
56202ac6454SAndrew Thompson {
563760bc48eSAndrew Thompson 	struct usb_device_request req;
564e0a69b51SAndrew Thompson 	usb_error_t err;
56502ac6454SAndrew Thompson 	uint8_t buf[4];
56602ac6454SAndrew Thompson 
567bc65068dSGavin Atkinson 	req.bmRequestType = req_type;
568bc65068dSGavin Atkinson 	req.bRequest = request;
569bc65068dSGavin Atkinson 	USETW(req.wValue, value);
570bc65068dSGavin Atkinson 	USETW(req.wIndex, index);
571bc65068dSGavin Atkinson 	USETW(req.wLength, length);
57202ac6454SAndrew Thompson 
573a593f6b8SAndrew Thompson 	err = usbd_do_request(udev, NULL, &req, buf);
57402ac6454SAndrew Thompson 	if (err) {
575a593f6b8SAndrew Thompson 		DPRINTF("error=%s\n", usbd_errstr(err));
576bc65068dSGavin Atkinson 		return (1);
577bc65068dSGavin Atkinson 	}
578bc65068dSGavin Atkinson 	return (0);
579bc65068dSGavin Atkinson }
580bc65068dSGavin Atkinson 
581bc65068dSGavin Atkinson static int
582bc65068dSGavin Atkinson uplcom_pl2303_init(struct usb_device *udev, uint8_t chiptype)
583bc65068dSGavin Atkinson {
584bc65068dSGavin Atkinson 	int err;
585bc65068dSGavin Atkinson 
586bc65068dSGavin Atkinson 	if (uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
587bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0)
588bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
589bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
590bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
591bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0)
592bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
593bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
594bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0)
595bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0))
59602ac6454SAndrew Thompson 		return (EIO);
597bc65068dSGavin Atkinson 
59827e4bd81SHans Petter Selasky 	if (chiptype != TYPE_PL2303)
599bc65068dSGavin Atkinson 		err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0);
600bc65068dSGavin Atkinson 	else
601bc65068dSGavin Atkinson 		err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x24, 0);
602bc65068dSGavin Atkinson 	if (err)
603bc65068dSGavin Atkinson 		return (EIO);
604bc65068dSGavin Atkinson 
60502ac6454SAndrew Thompson 	return (0);
60602ac6454SAndrew Thompson }
60702ac6454SAndrew Thompson 
60802ac6454SAndrew Thompson static void
609760bc48eSAndrew Thompson uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
61002ac6454SAndrew Thompson {
61102ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
612760bc48eSAndrew Thompson 	struct usb_device_request req;
61302ac6454SAndrew Thompson 
61402ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
61502ac6454SAndrew Thompson 
61602ac6454SAndrew Thompson 	if (onoff)
61702ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_DTR;
61802ac6454SAndrew Thompson 	else
61902ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_DTR;
62002ac6454SAndrew Thompson 
62102ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
62202ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
62302ac6454SAndrew Thompson 	USETW(req.wValue, sc->sc_line);
62402ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
62502ac6454SAndrew Thompson 	req.wIndex[1] = 0;
62602ac6454SAndrew Thompson 	USETW(req.wLength, 0);
62702ac6454SAndrew Thompson 
628a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
62902ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
63002ac6454SAndrew Thompson }
63102ac6454SAndrew Thompson 
63202ac6454SAndrew Thompson static void
633760bc48eSAndrew Thompson uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
63402ac6454SAndrew Thompson {
63502ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
636760bc48eSAndrew Thompson 	struct usb_device_request req;
63702ac6454SAndrew Thompson 
63802ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
63902ac6454SAndrew Thompson 
64002ac6454SAndrew Thompson 	if (onoff)
64102ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_RTS;
64202ac6454SAndrew Thompson 	else
64302ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_RTS;
64402ac6454SAndrew Thompson 
64502ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
64602ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
64702ac6454SAndrew Thompson 	USETW(req.wValue, sc->sc_line);
64802ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
64902ac6454SAndrew Thompson 	req.wIndex[1] = 0;
65002ac6454SAndrew Thompson 	USETW(req.wLength, 0);
65102ac6454SAndrew Thompson 
652a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
65302ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
65402ac6454SAndrew Thompson }
65502ac6454SAndrew Thompson 
65602ac6454SAndrew Thompson static void
657760bc48eSAndrew Thompson uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
65802ac6454SAndrew Thompson {
65902ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
660760bc48eSAndrew Thompson 	struct usb_device_request req;
66102ac6454SAndrew Thompson 	uint16_t temp;
66202ac6454SAndrew Thompson 
66302ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
66402ac6454SAndrew Thompson 
66502ac6454SAndrew Thompson 	temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
66602ac6454SAndrew Thompson 
66702ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
66802ac6454SAndrew Thompson 	req.bRequest = UCDC_SEND_BREAK;
66902ac6454SAndrew Thompson 	USETW(req.wValue, temp);
67002ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
67102ac6454SAndrew Thompson 	req.wIndex[1] = 0;
67202ac6454SAndrew Thompson 	USETW(req.wLength, 0);
67302ac6454SAndrew Thompson 
674a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
67502ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
67602ac6454SAndrew Thompson }
67702ac6454SAndrew Thompson 
67802ac6454SAndrew Thompson /*
67927e4bd81SHans Petter Selasky  * NOTE: These baud rates are officially supported, they can be written
68027e4bd81SHans Petter Selasky  * directly into dwDTERate register.
68127e4bd81SHans Petter Selasky  *
68227e4bd81SHans Petter Selasky  * Free baudrate setting is not supported by the base PL2303, and on
68327e4bd81SHans Petter Selasky  * other models it requires writing a divisor value to dwDTERate instead
68427e4bd81SHans Petter Selasky  * of the raw baudrate. The formula for divisor calculation is not published
68527e4bd81SHans Petter Selasky  * by the vendor, so it is speculative, though the official product homepage
68627e4bd81SHans Petter Selasky  * refers to the Linux module source as a reference implementation.
68702ac6454SAndrew Thompson  */
68827e4bd81SHans Petter Selasky static const uint32_t uplcom_rates[] = {
68927e4bd81SHans Petter Selasky 	/*
69027e4bd81SHans Petter Selasky 	 * Basic 'standard' speed rates, supported by all models
69127e4bd81SHans Petter Selasky 	 * NOTE: 900 and 56000 actually works as well
69227e4bd81SHans Petter Selasky 	 */
69327e4bd81SHans Petter Selasky 	75, 150, 300, 600, 900, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
69427e4bd81SHans Petter Selasky 	19200, 28800, 38400, 56000, 57600, 115200,
69527e4bd81SHans Petter Selasky 	/*
69627e4bd81SHans Petter Selasky 	 * Advanced speed rates up to 6Mbs, supported by HX/TA and HXD/TB/EA/RA
69727e4bd81SHans Petter Selasky      * NOTE: regardless of the spec, 256000 does not work
69827e4bd81SHans Petter Selasky 	 */
69927e4bd81SHans Petter Selasky 	128000, 134400, 161280, 201600, 230400, 268800, 403200, 460800, 614400,
70027e4bd81SHans Petter Selasky 	806400, 921600, 1228800, 2457600, 3000000, 6000000,
70127e4bd81SHans Petter Selasky 	/*
70227e4bd81SHans Petter Selasky 	 * Advanced speed rates up to 12, supported by HXD/TB/EA/RA
70327e4bd81SHans Petter Selasky 	 */
70427e4bd81SHans Petter Selasky 	12000000
70502ac6454SAndrew Thompson };
70602ac6454SAndrew Thompson 
707432157dcSPedro F. Giffuni #define	N_UPLCOM_RATES	nitems(uplcom_rates)
70802ac6454SAndrew Thompson 
70902ac6454SAndrew Thompson static int
71027e4bd81SHans Petter Selasky uplcom_baud_supported(unsigned int speed)
71127e4bd81SHans Petter Selasky {
71227e4bd81SHans Petter Selasky 	int i;
71327e4bd81SHans Petter Selasky 	for (i = 0; i < N_UPLCOM_RATES; i++) {
71427e4bd81SHans Petter Selasky 		if (uplcom_rates[i] == speed)
71527e4bd81SHans Petter Selasky 			return 1;
71627e4bd81SHans Petter Selasky 	}
71727e4bd81SHans Petter Selasky 	return 0;
71827e4bd81SHans Petter Selasky }
71927e4bd81SHans Petter Selasky 
72027e4bd81SHans Petter Selasky static int
721760bc48eSAndrew Thompson uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
72202ac6454SAndrew Thompson {
723fe0f6362SGavin Atkinson 	struct uplcom_softc *sc = ucom->sc_parent;
72402ac6454SAndrew Thompson 
72502ac6454SAndrew Thompson 	DPRINTF("\n");
72602ac6454SAndrew Thompson 
727fe0f6362SGavin Atkinson 	/**
728fe0f6362SGavin Atkinson 	 * Check requested baud rate.
729fe0f6362SGavin Atkinson 	 *
730fe0f6362SGavin Atkinson 	 * The PL2303 can only set specific baud rates, up to 1228800 baud.
73127e4bd81SHans Petter Selasky 	 * The PL2303HX can set any baud rate up to 6Mb.
732fe0f6362SGavin Atkinson 	 * The PL2303HX rev. D can set any baud rate up to 12Mb.
733fe0f6362SGavin Atkinson 	 *
734fe0f6362SGavin Atkinson 	 */
73527e4bd81SHans Petter Selasky 
73627e4bd81SHans Petter Selasky 	/* accept raw divisor data, if someone wants to do the math in user domain */
73727e4bd81SHans Petter Selasky 	if (t->c_ospeed & 0x80000000)
73827e4bd81SHans Petter Selasky 		return 0;
73927e4bd81SHans Petter Selasky 	switch (sc->sc_chiptype) {
74027e4bd81SHans Petter Selasky 		case TYPE_PL2303HXD:
74127e4bd81SHans Petter Selasky 			if (t->c_ospeed <= 12000000)
742fe0f6362SGavin Atkinson 				return (0);
74327e4bd81SHans Petter Selasky 			break;
74427e4bd81SHans Petter Selasky 		case TYPE_PL2303HX:
745fe0f6362SGavin Atkinson 			if (t->c_ospeed <= 6000000)
746fe0f6362SGavin Atkinson 				return (0);
74727e4bd81SHans Petter Selasky 			break;
74827e4bd81SHans Petter Selasky 		default:
74927e4bd81SHans Petter Selasky 			if (uplcom_baud_supported(t->c_ospeed))
75027e4bd81SHans Petter Selasky 				return (0);
75127e4bd81SHans Petter Selasky 			break;
75202ac6454SAndrew Thompson 	}
75302ac6454SAndrew Thompson 
754fe0f6362SGavin Atkinson 	DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed);
755fe0f6362SGavin Atkinson 	return (EIO);
75602ac6454SAndrew Thompson }
75702ac6454SAndrew Thompson 
75827e4bd81SHans Petter Selasky static unsigned int
75927e4bd81SHans Petter Selasky uplcom_encode_baud_rate_divisor(uint8_t *buf, unsigned int baud)
76027e4bd81SHans Petter Selasky {
76127e4bd81SHans Petter Selasky 	unsigned int baseline, mantissa, exponent;
76227e4bd81SHans Petter Selasky 
76327e4bd81SHans Petter Selasky 	/* Determine the baud rate divisor. This algorithm is taken from Linux. */
76427e4bd81SHans Petter Selasky 	/*
76527e4bd81SHans Petter Selasky 	 * Apparently the formula is:
76627e4bd81SHans Petter Selasky 	 *   baudrate = baseline / (mantissa * 4^exponent)
76727e4bd81SHans Petter Selasky 	 * where
76827e4bd81SHans Petter Selasky 	 *   mantissa = buf[8:0]
76927e4bd81SHans Petter Selasky 	 *   exponent = buf[11:9]
77027e4bd81SHans Petter Selasky 	 */
77127e4bd81SHans Petter Selasky 	if (baud == 0)
77227e4bd81SHans Petter Selasky 		baud = 1;
77327e4bd81SHans Petter Selasky 	baseline = 383385600;
77427e4bd81SHans Petter Selasky 	mantissa = baseline / baud;
77527e4bd81SHans Petter Selasky 	if (mantissa == 0)
77627e4bd81SHans Petter Selasky 		mantissa = 1;
77727e4bd81SHans Petter Selasky 	exponent = 0;
77827e4bd81SHans Petter Selasky 	while (mantissa >= 512) {
77927e4bd81SHans Petter Selasky 		if (exponent < 7) {
78027e4bd81SHans Petter Selasky 			mantissa >>= 2;	/* divide by 4 */
78127e4bd81SHans Petter Selasky 			exponent++;
78227e4bd81SHans Petter Selasky 		} else {
78327e4bd81SHans Petter Selasky 			/* Exponent is maxed. Trim mantissa and leave. This gives approx. 45.8 baud */
78427e4bd81SHans Petter Selasky 			mantissa = 511;
78527e4bd81SHans Petter Selasky 			break;
78627e4bd81SHans Petter Selasky 		}
78727e4bd81SHans Petter Selasky 	}
78827e4bd81SHans Petter Selasky 
78927e4bd81SHans Petter Selasky 	buf[3] = 0x80;
79027e4bd81SHans Petter Selasky 	buf[2] = 0;
79127e4bd81SHans Petter Selasky 	buf[1] = exponent << 1 | mantissa >> 8;
79227e4bd81SHans Petter Selasky 	buf[0] = mantissa & 0xff;
79327e4bd81SHans Petter Selasky 
79427e4bd81SHans Petter Selasky 	/* Calculate and return the exact baud rate. */
79527e4bd81SHans Petter Selasky 	baud = (baseline / mantissa) >> (exponent << 1);
79627e4bd81SHans Petter Selasky 	DPRINTF("real baud rate will be %u\n", baud);
79727e4bd81SHans Petter Selasky 
79827e4bd81SHans Petter Selasky 	return baud;
79927e4bd81SHans Petter Selasky }
80002ac6454SAndrew Thompson static void
801760bc48eSAndrew Thompson uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
80202ac6454SAndrew Thompson {
80302ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
804760bc48eSAndrew Thompson 	struct usb_cdc_line_state ls;
805760bc48eSAndrew Thompson 	struct usb_device_request req;
80602ac6454SAndrew Thompson 
80702ac6454SAndrew Thompson 	DPRINTF("sc = %p\n", sc);
80802ac6454SAndrew Thompson 
809271ae033SHans Petter Selasky 	memset(&ls, 0, sizeof(ls));
81002ac6454SAndrew Thompson 
81127e4bd81SHans Petter Selasky 	/*
81227e4bd81SHans Petter Selasky 	 * NOTE: If unsupported baud rates are set directly, the PL2303* uses 9600 baud.
81327e4bd81SHans Petter Selasky 	 */
81427e4bd81SHans Petter Selasky 	if ((t->c_ospeed & 0x80000000) || uplcom_baud_supported(t->c_ospeed))
81502ac6454SAndrew Thompson 		USETDW(ls.dwDTERate, t->c_ospeed);
81627e4bd81SHans Petter Selasky 	else
81727e4bd81SHans Petter Selasky 		t->c_ospeed = uplcom_encode_baud_rate_divisor((uint8_t*)&ls.dwDTERate, t->c_ospeed);
81802ac6454SAndrew Thompson 
81902ac6454SAndrew Thompson 	if (t->c_cflag & CSTOPB) {
82027e4bd81SHans Petter Selasky 		if ((t->c_cflag & CSIZE) == CS5) {
82127e4bd81SHans Petter Selasky 			/*
82227e4bd81SHans Petter Selasky 			 * NOTE: Comply with "real" UARTs / RS232:
82327e4bd81SHans Petter Selasky 			 *       use 1.5 instead of 2 stop bits with 5 data bits
82427e4bd81SHans Petter Selasky 			 */
82527e4bd81SHans Petter Selasky 			ls.bCharFormat = UCDC_STOP_BIT_1_5;
82627e4bd81SHans Petter Selasky 		} else {
82702ac6454SAndrew Thompson 			ls.bCharFormat = UCDC_STOP_BIT_2;
82827e4bd81SHans Petter Selasky 		}
82902ac6454SAndrew Thompson 	} else {
83002ac6454SAndrew Thompson 		ls.bCharFormat = UCDC_STOP_BIT_1;
83102ac6454SAndrew Thompson 	}
83202ac6454SAndrew Thompson 
83302ac6454SAndrew Thompson 	if (t->c_cflag & PARENB) {
83402ac6454SAndrew Thompson 		if (t->c_cflag & PARODD) {
83502ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_ODD;
83602ac6454SAndrew Thompson 		} else {
83702ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_EVEN;
83802ac6454SAndrew Thompson 		}
83902ac6454SAndrew Thompson 	} else {
84002ac6454SAndrew Thompson 		ls.bParityType = UCDC_PARITY_NONE;
84102ac6454SAndrew Thompson 	}
84202ac6454SAndrew Thompson 
84302ac6454SAndrew Thompson 	switch (t->c_cflag & CSIZE) {
84402ac6454SAndrew Thompson 	case CS5:
84502ac6454SAndrew Thompson 		ls.bDataBits = 5;
84602ac6454SAndrew Thompson 		break;
84702ac6454SAndrew Thompson 	case CS6:
84802ac6454SAndrew Thompson 		ls.bDataBits = 6;
84902ac6454SAndrew Thompson 		break;
85002ac6454SAndrew Thompson 	case CS7:
85102ac6454SAndrew Thompson 		ls.bDataBits = 7;
85202ac6454SAndrew Thompson 		break;
85302ac6454SAndrew Thompson 	case CS8:
85402ac6454SAndrew Thompson 		ls.bDataBits = 8;
85502ac6454SAndrew Thompson 		break;
85602ac6454SAndrew Thompson 	}
85702ac6454SAndrew Thompson 
85827e4bd81SHans Petter Selasky 	DPRINTF("rate=0x%08x fmt=%d parity=%d bits=%d\n",
85902ac6454SAndrew Thompson 	    UGETDW(ls.dwDTERate), ls.bCharFormat,
86002ac6454SAndrew Thompson 	    ls.bParityType, ls.bDataBits);
86102ac6454SAndrew Thompson 
86202ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
86302ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_LINE_CODING;
86402ac6454SAndrew Thompson 	USETW(req.wValue, 0);
86502ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
86602ac6454SAndrew Thompson 	req.wIndex[1] = 0;
86702ac6454SAndrew Thompson 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
86802ac6454SAndrew Thompson 
869a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
87002ac6454SAndrew Thompson 	    &req, &ls, 0, 1000);
87102ac6454SAndrew Thompson 
87202ac6454SAndrew Thompson 	if (t->c_cflag & CRTSCTS) {
87302ac6454SAndrew Thompson 
87402ac6454SAndrew Thompson 		DPRINTF("crtscts = on\n");
87502ac6454SAndrew Thompson 
87602ac6454SAndrew Thompson 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
87702ac6454SAndrew Thompson 		req.bRequest = UPLCOM_SET_REQUEST;
87802ac6454SAndrew Thompson 		USETW(req.wValue, 0);
87927e4bd81SHans Petter Selasky 		if (sc->sc_chiptype != TYPE_PL2303)
88002ac6454SAndrew Thompson 			USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
88102ac6454SAndrew Thompson 		else
88202ac6454SAndrew Thompson 			USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
88302ac6454SAndrew Thompson 		USETW(req.wLength, 0);
88402ac6454SAndrew Thompson 
885a593f6b8SAndrew Thompson 		ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
88602ac6454SAndrew Thompson 		    &req, NULL, 0, 1000);
88702ac6454SAndrew Thompson 	} else {
88802ac6454SAndrew Thompson 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
88902ac6454SAndrew Thompson 		req.bRequest = UPLCOM_SET_REQUEST;
89002ac6454SAndrew Thompson 		USETW(req.wValue, 0);
89102ac6454SAndrew Thompson 		USETW(req.wIndex, 0);
89202ac6454SAndrew Thompson 		USETW(req.wLength, 0);
893a593f6b8SAndrew Thompson 		ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
89402ac6454SAndrew Thompson 		    &req, NULL, 0, 1000);
89502ac6454SAndrew Thompson 	}
89602ac6454SAndrew Thompson }
89702ac6454SAndrew Thompson 
89802ac6454SAndrew Thompson static void
899760bc48eSAndrew Thompson uplcom_start_read(struct ucom_softc *ucom)
90002ac6454SAndrew Thompson {
90102ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
90202ac6454SAndrew Thompson 
90302ac6454SAndrew Thompson 	/* start interrupt endpoint */
904a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
90502ac6454SAndrew Thompson 
90602ac6454SAndrew Thompson 	/* start read endpoint */
907a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
90802ac6454SAndrew Thompson }
90902ac6454SAndrew Thompson 
91002ac6454SAndrew Thompson static void
911760bc48eSAndrew Thompson uplcom_stop_read(struct ucom_softc *ucom)
91202ac6454SAndrew Thompson {
91302ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
91402ac6454SAndrew Thompson 
91502ac6454SAndrew Thompson 	/* stop interrupt endpoint */
916a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
91702ac6454SAndrew Thompson 
91802ac6454SAndrew Thompson 	/* stop read endpoint */
919a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
92002ac6454SAndrew Thompson }
92102ac6454SAndrew Thompson 
92202ac6454SAndrew Thompson static void
923760bc48eSAndrew Thompson uplcom_start_write(struct ucom_softc *ucom)
92402ac6454SAndrew Thompson {
92502ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
92602ac6454SAndrew Thompson 
927a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
92802ac6454SAndrew Thompson }
92902ac6454SAndrew Thompson 
93002ac6454SAndrew Thompson static void
931760bc48eSAndrew Thompson uplcom_stop_write(struct ucom_softc *ucom)
93202ac6454SAndrew Thompson {
93302ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
93402ac6454SAndrew Thompson 
935a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
93602ac6454SAndrew Thompson }
93702ac6454SAndrew Thompson 
93802ac6454SAndrew Thompson static void
939760bc48eSAndrew Thompson uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
94002ac6454SAndrew Thompson {
94102ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
94202ac6454SAndrew Thompson 
94302ac6454SAndrew Thompson 	DPRINTF("\n");
94402ac6454SAndrew Thompson 
94502ac6454SAndrew Thompson 	*lsr = sc->sc_lsr;
94602ac6454SAndrew Thompson 	*msr = sc->sc_msr;
94702ac6454SAndrew Thompson }
94802ac6454SAndrew Thompson 
94902ac6454SAndrew Thompson static void
950ed6d949aSAndrew Thompson uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
95102ac6454SAndrew Thompson {
952ed6d949aSAndrew Thompson 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
953ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
95402ac6454SAndrew Thompson 	uint8_t buf[9];
955ed6d949aSAndrew Thompson 	int actlen;
956ed6d949aSAndrew Thompson 
957ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
95802ac6454SAndrew Thompson 
95902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
96002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
96102ac6454SAndrew Thompson 
962ed6d949aSAndrew Thompson 		DPRINTF("actlen = %u\n", actlen);
96302ac6454SAndrew Thompson 
964ed6d949aSAndrew Thompson 		if (actlen >= 9) {
96502ac6454SAndrew Thompson 
966ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
967ed6d949aSAndrew Thompson 			usbd_copy_out(pc, 0, buf, sizeof(buf));
96802ac6454SAndrew Thompson 
96927e4bd81SHans Petter Selasky 			DPRINTF("status = 0x%02x\n", buf[UPLCOM_STATE_INDEX]);
97002ac6454SAndrew Thompson 
97102ac6454SAndrew Thompson 			sc->sc_lsr = 0;
97202ac6454SAndrew Thompson 			sc->sc_msr = 0;
97302ac6454SAndrew Thompson 
97427e4bd81SHans Petter Selasky 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_CTS) {
97502ac6454SAndrew Thompson 				sc->sc_msr |= SER_CTS;
97602ac6454SAndrew Thompson 			}
97727e4bd81SHans Petter Selasky 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_OVERRUN_ERROR) {
97827e4bd81SHans Petter Selasky 				sc->sc_lsr |= ULSR_OE;
97927e4bd81SHans Petter Selasky 			}
98027e4bd81SHans Petter Selasky 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_PARITY_ERROR) {
98127e4bd81SHans Petter Selasky 				sc->sc_lsr |= ULSR_PE;
98227e4bd81SHans Petter Selasky 			}
98327e4bd81SHans Petter Selasky 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_FRAME_ERROR) {
98427e4bd81SHans Petter Selasky 				sc->sc_lsr |= ULSR_FE;
98527e4bd81SHans Petter Selasky 			}
98627e4bd81SHans Petter Selasky 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_RING) {
98727e4bd81SHans Petter Selasky 				sc->sc_msr |= SER_RI;
98827e4bd81SHans Petter Selasky 			}
98927e4bd81SHans Petter Selasky 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_BREAK_ERROR) {
99027e4bd81SHans Petter Selasky 				sc->sc_lsr |= ULSR_BI;
99127e4bd81SHans Petter Selasky 			}
99227e4bd81SHans Petter Selasky 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DSR) {
99302ac6454SAndrew Thompson 				sc->sc_msr |= SER_DSR;
99402ac6454SAndrew Thompson 			}
99527e4bd81SHans Petter Selasky 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DCD) {
99602ac6454SAndrew Thompson 				sc->sc_msr |= SER_DCD;
99702ac6454SAndrew Thompson 			}
998a593f6b8SAndrew Thompson 			ucom_status_change(&sc->sc_ucom);
99902ac6454SAndrew Thompson 		}
100002ac6454SAndrew Thompson 	case USB_ST_SETUP:
100102ac6454SAndrew Thompson tr_setup:
1002ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1003a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
100402ac6454SAndrew Thompson 		return;
100502ac6454SAndrew Thompson 
100602ac6454SAndrew Thompson 	default:			/* Error */
1007ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
100802ac6454SAndrew Thompson 			/* try to clear stall first */
1009ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
101002ac6454SAndrew Thompson 			goto tr_setup;
101102ac6454SAndrew Thompson 		}
101202ac6454SAndrew Thompson 		return;
101302ac6454SAndrew Thompson 	}
101402ac6454SAndrew Thompson }
101502ac6454SAndrew Thompson 
101602ac6454SAndrew Thompson static void
1017ed6d949aSAndrew Thompson uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
101802ac6454SAndrew Thompson {
1019ed6d949aSAndrew Thompson 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1020ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
102102ac6454SAndrew Thompson 	uint32_t actlen;
102202ac6454SAndrew Thompson 
102302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
102402ac6454SAndrew Thompson 	case USB_ST_SETUP:
102502ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
102602ac6454SAndrew Thompson tr_setup:
1027ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
1028ed6d949aSAndrew Thompson 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
102902ac6454SAndrew Thompson 		    UPLCOM_BULK_BUF_SIZE, &actlen)) {
103002ac6454SAndrew Thompson 
103102ac6454SAndrew Thompson 			DPRINTF("actlen = %d\n", actlen);
103202ac6454SAndrew Thompson 
1033ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, actlen);
1034a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
103502ac6454SAndrew Thompson 		}
103602ac6454SAndrew Thompson 		return;
103702ac6454SAndrew Thompson 
103802ac6454SAndrew Thompson 	default:			/* Error */
1039ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
104002ac6454SAndrew Thompson 			/* try to clear stall first */
1041ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
104202ac6454SAndrew Thompson 			goto tr_setup;
104302ac6454SAndrew Thompson 		}
104402ac6454SAndrew Thompson 		return;
104502ac6454SAndrew Thompson 	}
104602ac6454SAndrew Thompson }
104702ac6454SAndrew Thompson 
104802ac6454SAndrew Thompson static void
1049ed6d949aSAndrew Thompson uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
105002ac6454SAndrew Thompson {
1051ed6d949aSAndrew Thompson 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1052ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
1053ed6d949aSAndrew Thompson 	int actlen;
1054ed6d949aSAndrew Thompson 
1055ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
105602ac6454SAndrew Thompson 
105702ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
105802ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
1059ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
1060ed6d949aSAndrew Thompson 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
106102ac6454SAndrew Thompson 
106202ac6454SAndrew Thompson 	case USB_ST_SETUP:
106302ac6454SAndrew Thompson tr_setup:
1064ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1065a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
106602ac6454SAndrew Thompson 		return;
106702ac6454SAndrew Thompson 
106802ac6454SAndrew Thompson 	default:			/* Error */
1069ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
107002ac6454SAndrew Thompson 			/* try to clear stall first */
1071ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
107202ac6454SAndrew Thompson 			goto tr_setup;
107302ac6454SAndrew Thompson 		}
107402ac6454SAndrew Thompson 		return;
107502ac6454SAndrew Thompson 	}
107602ac6454SAndrew Thompson }
1077655dc9d0SAndrew Thompson 
1078655dc9d0SAndrew Thompson static void
1079655dc9d0SAndrew Thompson uplcom_poll(struct ucom_softc *ucom)
1080655dc9d0SAndrew Thompson {
1081655dc9d0SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
1082655dc9d0SAndrew Thompson 	usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);
1083655dc9d0SAndrew Thompson }
1084