xref: /freebsd/sys/dev/usb/serial/uplcom.c (revision 718cf2ccb9956613756ab15d7a0e28f2c8e91cab)
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 /*-
7*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause-NetBSD
8*718cf2ccSPedro 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
13702ac6454SAndrew Thompson #define	RSAQ_STATUS_DSR			0x02
13802ac6454SAndrew Thompson #define	RSAQ_STATUS_DCD			0x01
13902ac6454SAndrew Thompson 
14002ac6454SAndrew Thompson #define	TYPE_PL2303			0
141bc65068dSGavin Atkinson #define	TYPE_PL2303HX			1
14202ac6454SAndrew Thompson 
14302ac6454SAndrew Thompson enum {
14402ac6454SAndrew Thompson 	UPLCOM_BULK_DT_WR,
14502ac6454SAndrew Thompson 	UPLCOM_BULK_DT_RD,
14602ac6454SAndrew Thompson 	UPLCOM_INTR_DT_RD,
14702ac6454SAndrew Thompson 	UPLCOM_N_TRANSFER,
14802ac6454SAndrew Thompson };
14902ac6454SAndrew Thompson 
15002ac6454SAndrew Thompson struct uplcom_softc {
151760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
152760bc48eSAndrew Thompson 	struct ucom_softc sc_ucom;
15302ac6454SAndrew Thompson 
154760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER];
155760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
156deefe583SAndrew Thompson 	struct mtx sc_mtx;
15702ac6454SAndrew Thompson 
15802ac6454SAndrew Thompson 	uint16_t sc_line;
15902ac6454SAndrew Thompson 
16002ac6454SAndrew Thompson 	uint8_t	sc_lsr;			/* local status register */
16102ac6454SAndrew Thompson 	uint8_t	sc_msr;			/* uplcom status register */
16202ac6454SAndrew Thompson 	uint8_t	sc_chiptype;		/* type of chip */
16302ac6454SAndrew Thompson 	uint8_t	sc_ctrl_iface_no;
16402ac6454SAndrew Thompson 	uint8_t	sc_data_iface_no;
16502ac6454SAndrew Thompson 	uint8_t	sc_iface_index[2];
16602ac6454SAndrew Thompson };
16702ac6454SAndrew Thompson 
16802ac6454SAndrew Thompson /* prototypes */
16902ac6454SAndrew Thompson 
170e0a69b51SAndrew Thompson static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *);
17102d4a225SDimitry Andric static usb_error_t uplcom_pl2303_do(struct usb_device *, uint8_t, uint8_t,
172bc65068dSGavin Atkinson 			uint16_t, uint16_t, uint16_t);
173bc65068dSGavin Atkinson static int	uplcom_pl2303_init(struct usb_device *, uint8_t);
1745805d178SHans Petter Selasky static void	uplcom_free(struct ucom_softc *);
175760bc48eSAndrew Thompson static void	uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
176760bc48eSAndrew Thompson static void	uplcom_cfg_set_rts(struct ucom_softc *, uint8_t);
177760bc48eSAndrew Thompson static void	uplcom_cfg_set_break(struct ucom_softc *, uint8_t);
178760bc48eSAndrew Thompson static int	uplcom_pre_param(struct ucom_softc *, struct termios *);
179760bc48eSAndrew Thompson static void	uplcom_cfg_param(struct ucom_softc *, struct termios *);
180760bc48eSAndrew Thompson static void	uplcom_start_read(struct ucom_softc *);
181760bc48eSAndrew Thompson static void	uplcom_stop_read(struct ucom_softc *);
182760bc48eSAndrew Thompson static void	uplcom_start_write(struct ucom_softc *);
183760bc48eSAndrew Thompson static void	uplcom_stop_write(struct ucom_softc *);
184760bc48eSAndrew Thompson static void	uplcom_cfg_get_status(struct ucom_softc *, uint8_t *,
18502ac6454SAndrew Thompson 		    uint8_t *);
186655dc9d0SAndrew Thompson static void	uplcom_poll(struct ucom_softc *ucom);
18702ac6454SAndrew Thompson 
18802ac6454SAndrew Thompson static device_probe_t uplcom_probe;
18902ac6454SAndrew Thompson static device_attach_t uplcom_attach;
19002ac6454SAndrew Thompson static device_detach_t uplcom_detach;
191c01fc06eSHans Petter Selasky static void uplcom_free_softc(struct uplcom_softc *);
19202ac6454SAndrew Thompson 
193e0a69b51SAndrew Thompson static usb_callback_t uplcom_intr_callback;
194e0a69b51SAndrew Thompson static usb_callback_t uplcom_write_callback;
195e0a69b51SAndrew Thompson static usb_callback_t uplcom_read_callback;
19602ac6454SAndrew Thompson 
197760bc48eSAndrew Thompson static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
19802ac6454SAndrew Thompson 
19902ac6454SAndrew Thompson 	[UPLCOM_BULK_DT_WR] = {
20002ac6454SAndrew Thompson 		.type = UE_BULK,
20102ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
20202ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
2034eae601eSAndrew Thompson 		.bufsize = UPLCOM_BULK_BUF_SIZE,
2044eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2054eae601eSAndrew Thompson 		.callback = &uplcom_write_callback,
20602ac6454SAndrew Thompson 		.if_index = 0,
20702ac6454SAndrew Thompson 	},
20802ac6454SAndrew Thompson 
20902ac6454SAndrew Thompson 	[UPLCOM_BULK_DT_RD] = {
21002ac6454SAndrew Thompson 		.type = UE_BULK,
21102ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
21202ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2134eae601eSAndrew Thompson 		.bufsize = UPLCOM_BULK_BUF_SIZE,
2144eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2154eae601eSAndrew Thompson 		.callback = &uplcom_read_callback,
21602ac6454SAndrew Thompson 		.if_index = 0,
21702ac6454SAndrew Thompson 	},
21802ac6454SAndrew Thompson 
21902ac6454SAndrew Thompson 	[UPLCOM_INTR_DT_RD] = {
22002ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
22102ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
22202ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2234eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2244eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
2254eae601eSAndrew Thompson 		.callback = &uplcom_intr_callback,
22602ac6454SAndrew Thompson 		.if_index = 1,
22702ac6454SAndrew Thompson 	},
22802ac6454SAndrew Thompson };
22902ac6454SAndrew Thompson 
230a8675caeSAndrew Thompson static struct ucom_callback uplcom_callback = {
231a593f6b8SAndrew Thompson 	.ucom_cfg_get_status = &uplcom_cfg_get_status,
232a593f6b8SAndrew Thompson 	.ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
233a593f6b8SAndrew Thompson 	.ucom_cfg_set_rts = &uplcom_cfg_set_rts,
234a593f6b8SAndrew Thompson 	.ucom_cfg_set_break = &uplcom_cfg_set_break,
235a593f6b8SAndrew Thompson 	.ucom_cfg_param = &uplcom_cfg_param,
236a593f6b8SAndrew Thompson 	.ucom_pre_param = &uplcom_pre_param,
237a593f6b8SAndrew Thompson 	.ucom_start_read = &uplcom_start_read,
238a593f6b8SAndrew Thompson 	.ucom_stop_read = &uplcom_stop_read,
239a593f6b8SAndrew Thompson 	.ucom_start_write = &uplcom_start_write,
240a593f6b8SAndrew Thompson 	.ucom_stop_write = &uplcom_stop_write,
241655dc9d0SAndrew Thompson 	.ucom_poll = &uplcom_poll,
2425805d178SHans Petter Selasky 	.ucom_free = &uplcom_free,
24302ac6454SAndrew Thompson };
24402ac6454SAndrew Thompson 
245bc65068dSGavin Atkinson #define	UPLCOM_DEV(v,p)				\
246bc65068dSGavin Atkinson   { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p) }
24702ac6454SAndrew Thompson 
248f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID uplcom_devs[] = {
249f5113df9SGavin Atkinson 	UPLCOM_DEV(ACERP, S81),			/* BenQ S81 phone */
250f5113df9SGavin Atkinson 	UPLCOM_DEV(ADLINK, ND6530),		/* ADLINK ND-6530 USB-Serial */
251f5113df9SGavin Atkinson 	UPLCOM_DEV(ALCATEL, OT535),		/* Alcatel One Touch 535/735 */
252f5113df9SGavin Atkinson 	UPLCOM_DEV(ALCOR, AU9720),		/* Alcor AU9720 USB 2.0-RS232 */
253f5113df9SGavin Atkinson 	UPLCOM_DEV(ANCHOR, SERIAL),		/* Anchor Serial adapter */
254bc65068dSGavin Atkinson 	UPLCOM_DEV(ATEN, UC232A),		/* PLANEX USB-RS232 URS-03 */
25501a8f075SGavin Atkinson 	UPLCOM_DEV(BELKIN, F5U257),		/* Belkin F5U257 USB to Serial */
256bc65068dSGavin Atkinson 	UPLCOM_DEV(COREGA, CGUSBRS232R),	/* Corega CG-USBRS232R */
257f5113df9SGavin Atkinson 	UPLCOM_DEV(EPSON, CRESSI_EDY),		/* Cressi Edy diving computer */
258c6ac426dSGavin Atkinson 	UPLCOM_DEV(EPSON, N2ITION3),		/* Zeagle N2iTion3 diving computer */
25901a8f075SGavin Atkinson 	UPLCOM_DEV(ELECOM, UCSGT),		/* ELECOM UC-SGT Serial Adapter */
26001a8f075SGavin Atkinson 	UPLCOM_DEV(ELECOM, UCSGT0),		/* ELECOM UC-SGT Serial Adapter */
261bc65068dSGavin Atkinson 	UPLCOM_DEV(HAL, IMR001),		/* HAL Corporation Crossam2+USB */
262f5113df9SGavin Atkinson 	UPLCOM_DEV(HP, LD220),			/* HP LD220 POS Display */
263bc65068dSGavin Atkinson 	UPLCOM_DEV(IODATA, USBRSAQ),		/* I/O DATA USB-RSAQ */
264bc65068dSGavin Atkinson 	UPLCOM_DEV(IODATA, USBRSAQ5),		/* I/O DATA USB-RSAQ5 */
265f5113df9SGavin Atkinson 	UPLCOM_DEV(ITEGNO, WM1080A),		/* iTegno WM1080A GSM/GFPRS modem */
266f5113df9SGavin Atkinson 	UPLCOM_DEV(ITEGNO, WM2080A),		/* iTegno WM2080A CDMA modem */
267f5113df9SGavin Atkinson 	UPLCOM_DEV(LEADTEK, 9531),		/* Leadtek 9531 GPS */
268f5113df9SGavin Atkinson 	UPLCOM_DEV(MICROSOFT, 700WX),		/* Microsoft Palm 700WX */
269bc65068dSGavin Atkinson 	UPLCOM_DEV(MOBILEACTION, MA620),	/* Mobile Action MA-620 Infrared Adapter */
270f5113df9SGavin Atkinson 	UPLCOM_DEV(NETINDEX, WS002IN),		/* Willcom W-S002IN */
271f5113df9SGavin Atkinson 	UPLCOM_DEV(NOKIA2, CA42),		/* Nokia CA-42 cable */
272f5113df9SGavin Atkinson 	UPLCOM_DEV(OTI, DKU5),			/* OTI DKU-5 cable */
273f5113df9SGavin Atkinson 	UPLCOM_DEV(PANASONIC, TYTP50P6S),	/* Panasonic TY-TP50P6-S flat screen */
274f5113df9SGavin Atkinson 	UPLCOM_DEV(PLX, CA42),			/* PLX CA-42 clone cable */
275f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, ALLTRONIX_GPRS),	/* Alltronix ACM003U00 modem */
276f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, ALDIGA_AL11U),	/* AlDiga AL-11U modem */
277f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, DCU11),		/* DCU-11 Phone Cable */
278f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, HCR331),		/* HCR331 Card Reader */
279f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, MICROMAX_610U),	/* Micromax 610U modem */
280d01755edSGavin Atkinson 	UPLCOM_DEV(PROLIFIC, MOTOROLA),		/* Motorola cable */
281bc65068dSGavin Atkinson 	UPLCOM_DEV(PROLIFIC, PHAROS),		/* Prolific Pharos */
282f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, PL2303),		/* Generic adapter */
283bc65068dSGavin Atkinson 	UPLCOM_DEV(PROLIFIC, RSAQ2),		/* I/O DATA USB-RSAQ2 */
284bc65068dSGavin Atkinson 	UPLCOM_DEV(PROLIFIC, RSAQ3),		/* I/O DATA USB-RSAQ3 */
285635c9457SGavin Atkinson 	UPLCOM_DEV(PROLIFIC, UIC_MSR206),	/* UIC MSR206 Card Reader */
286f5113df9SGavin Atkinson 	UPLCOM_DEV(PROLIFIC2, PL2303),		/* Prolific adapter */
28701a8f075SGavin Atkinson 	UPLCOM_DEV(RADIOSHACK, USBCABLE),	/* Radio Shack USB Adapter */
288bc65068dSGavin Atkinson 	UPLCOM_DEV(RATOC, REXUSB60),		/* RATOC REX-USB60 */
289bc65068dSGavin Atkinson 	UPLCOM_DEV(SAGEM, USBSERIAL),		/* Sagem USB-Serial Controller */
290f5113df9SGavin Atkinson 	UPLCOM_DEV(SAMSUNG, I330),		/* Samsung I330 phone cradle */
291f5113df9SGavin Atkinson 	UPLCOM_DEV(SANWA, KB_USB2),		/* Sanwa KB-USB2 Multimeter cable */
29201a8f075SGavin Atkinson 	UPLCOM_DEV(SIEMENS3, EF81),		/* Siemens EF81 */
29301a8f075SGavin Atkinson 	UPLCOM_DEV(SIEMENS3, SX1),		/* Siemens SX1 */
29401a8f075SGavin Atkinson 	UPLCOM_DEV(SIEMENS3, X65),		/* Siemens X65 */
29501a8f075SGavin Atkinson 	UPLCOM_DEV(SIEMENS3, X75),		/* Siemens X75 */
296bc65068dSGavin Atkinson 	UPLCOM_DEV(SITECOM, SERIAL),		/* Sitecom USB to Serial */
29701a8f075SGavin Atkinson 	UPLCOM_DEV(SMART, PL2303),		/* SMART Technologies USB to Serial */
298f5113df9SGavin Atkinson 	UPLCOM_DEV(SONY, QN3),			/* Sony QN3 phone cable */
299f5113df9SGavin Atkinson 	UPLCOM_DEV(SONYERICSSON, DATAPILOT),	/* Sony Ericsson Datapilot */
300f5113df9SGavin Atkinson 	UPLCOM_DEV(SONYERICSSON, DCU10),	/* Sony Ericsson DCU-10 Cable */
301bc65068dSGavin Atkinson 	UPLCOM_DEV(SOURCENEXT, KEIKAI8),	/* SOURCENEXT KeikaiDenwa 8 */
302bc65068dSGavin Atkinson 	UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG),	/* SOURCENEXT KeikaiDenwa 8 with charger */
303f5113df9SGavin Atkinson 	UPLCOM_DEV(SPEEDDRAGON, MS3303H),	/* Speed Dragon USB-Serial */
304f5113df9SGavin Atkinson 	UPLCOM_DEV(SYNTECH, CPT8001C),		/* Syntech CPT-8001C Barcode scanner */
305bc65068dSGavin Atkinson 	UPLCOM_DEV(TDK, UHA6400),		/* TDK USB-PHS Adapter UHA6400 */
306f5113df9SGavin Atkinson 	UPLCOM_DEV(TDK, UPA9664),		/* TDK USB-PHS Adapter UPA9664 */
30701a8f075SGavin Atkinson 	UPLCOM_DEV(TRIPPLITE, U209),		/* Tripp-Lite U209-000-R USB to Serial */
308f5113df9SGavin Atkinson 	UPLCOM_DEV(YCCABLE, PL2303),		/* YC Cable USB-Serial */
30902ac6454SAndrew Thompson };
3109e6b5313SAndrew Thompson #undef UPLCOM_DEV
31102ac6454SAndrew Thompson 
31202ac6454SAndrew Thompson static device_method_t uplcom_methods[] = {
31302ac6454SAndrew Thompson 	DEVMETHOD(device_probe, uplcom_probe),
31402ac6454SAndrew Thompson 	DEVMETHOD(device_attach, uplcom_attach),
31502ac6454SAndrew Thompson 	DEVMETHOD(device_detach, uplcom_detach),
3165805d178SHans Petter Selasky 	DEVMETHOD_END
31702ac6454SAndrew Thompson };
31802ac6454SAndrew Thompson 
31902ac6454SAndrew Thompson static devclass_t uplcom_devclass;
32002ac6454SAndrew Thompson 
32102ac6454SAndrew Thompson static driver_t uplcom_driver = {
32202ac6454SAndrew Thompson 	.name = "uplcom",
32302ac6454SAndrew Thompson 	.methods = uplcom_methods,
32402ac6454SAndrew Thompson 	.size = sizeof(struct uplcom_softc),
32502ac6454SAndrew Thompson };
32602ac6454SAndrew Thompson 
3279aef556dSAndrew Thompson DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0);
32802ac6454SAndrew Thompson MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
32902ac6454SAndrew Thompson MODULE_DEPEND(uplcom, usb, 1, 1, 1);
33002ac6454SAndrew Thompson MODULE_VERSION(uplcom, UPLCOM_MODVER);
331f809f280SWarner Losh USB_PNP_HOST_INFO(uplcom_devs);
33202ac6454SAndrew Thompson 
33302ac6454SAndrew Thompson static int
33402ac6454SAndrew Thompson uplcom_probe(device_t dev)
33502ac6454SAndrew Thompson {
336760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
33702ac6454SAndrew Thompson 
33802ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
33902ac6454SAndrew Thompson 
340f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
34102ac6454SAndrew Thompson 		return (ENXIO);
34202ac6454SAndrew Thompson 	}
34302ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
34402ac6454SAndrew Thompson 		return (ENXIO);
34502ac6454SAndrew Thompson 	}
34602ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
34702ac6454SAndrew Thompson 		return (ENXIO);
34802ac6454SAndrew Thompson 	}
349a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
35002ac6454SAndrew Thompson }
35102ac6454SAndrew Thompson 
35202ac6454SAndrew Thompson static int
35302ac6454SAndrew Thompson uplcom_attach(device_t dev)
35402ac6454SAndrew Thompson {
355760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
35602ac6454SAndrew Thompson 	struct uplcom_softc *sc = device_get_softc(dev);
357760bc48eSAndrew Thompson 	struct usb_interface *iface;
358760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
359bc65068dSGavin Atkinson 	struct usb_device_descriptor *dd;
36002ac6454SAndrew Thompson 	int error;
36102ac6454SAndrew Thompson 
36202ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
36302ac6454SAndrew Thompson 
364a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
365deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
3665805d178SHans Petter Selasky 	ucom_ref(&sc->sc_super_ucom);
36702ac6454SAndrew Thompson 
36802ac6454SAndrew Thompson 	DPRINTF("sc = %p\n", sc);
36902ac6454SAndrew Thompson 
37002ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
37102ac6454SAndrew Thompson 
372bc65068dSGavin Atkinson 	/* Determine the chip type.  This algorithm is taken from Linux. */
373bc65068dSGavin Atkinson 	dd = usbd_get_device_descriptor(sc->sc_udev);
374bc65068dSGavin Atkinson 	if (dd->bDeviceClass == 0x02)
375bc65068dSGavin Atkinson 		sc->sc_chiptype = TYPE_PL2303;
376bc65068dSGavin Atkinson 	else if (dd->bMaxPacketSize == 0x40)
377bc65068dSGavin Atkinson 		sc->sc_chiptype = TYPE_PL2303HX;
378bc65068dSGavin Atkinson 	else
379bc65068dSGavin Atkinson 		sc->sc_chiptype = TYPE_PL2303;
380bc65068dSGavin Atkinson 
38102ac6454SAndrew Thompson 	DPRINTF("chiptype: %s\n",
382bc65068dSGavin Atkinson 	    (sc->sc_chiptype == TYPE_PL2303HX) ?
38302ac6454SAndrew Thompson 	    "2303X" : "2303");
38402ac6454SAndrew Thompson 
38502ac6454SAndrew Thompson 	/*
38602ac6454SAndrew Thompson 	 * USB-RSAQ1 has two interface
38702ac6454SAndrew Thompson 	 *
38802ac6454SAndrew Thompson 	 *  USB-RSAQ1       | USB-RSAQ2
38902ac6454SAndrew Thompson 	 * -----------------+-----------------
39002ac6454SAndrew Thompson 	 * Interface 0      |Interface 0
39102ac6454SAndrew Thompson 	 *  Interrupt(0x81) | Interrupt(0x81)
39202ac6454SAndrew Thompson 	 * -----------------+ BulkIN(0x02)
39302ac6454SAndrew Thompson 	 * Interface 1	    | BulkOUT(0x83)
39402ac6454SAndrew Thompson 	 *   BulkIN(0x02)   |
39502ac6454SAndrew Thompson 	 *   BulkOUT(0x83)  |
39602ac6454SAndrew Thompson 	 */
39702ac6454SAndrew Thompson 
39802ac6454SAndrew Thompson 	sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
39902ac6454SAndrew Thompson 	sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
40002ac6454SAndrew Thompson 
401a593f6b8SAndrew Thompson 	iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
40202ac6454SAndrew Thompson 	if (iface) {
403a593f6b8SAndrew Thompson 		id = usbd_get_interface_descriptor(iface);
40402ac6454SAndrew Thompson 		if (id == NULL) {
405767cb2e2SAndrew Thompson 			device_printf(dev, "no interface descriptor (2)\n");
40602ac6454SAndrew Thompson 			goto detach;
40702ac6454SAndrew Thompson 		}
40802ac6454SAndrew Thompson 		sc->sc_data_iface_no = id->bInterfaceNumber;
40902ac6454SAndrew Thompson 		sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
410a593f6b8SAndrew Thompson 		usbd_set_parent_iface(uaa->device,
41102ac6454SAndrew Thompson 		    UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
41202ac6454SAndrew Thompson 	} else {
41302ac6454SAndrew Thompson 		sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
41402ac6454SAndrew Thompson 		sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
41502ac6454SAndrew Thompson 	}
41602ac6454SAndrew Thompson 
417a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device,
41802ac6454SAndrew Thompson 	    sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
419deefe583SAndrew Thompson 	    UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
42002ac6454SAndrew Thompson 	if (error) {
42102ac6454SAndrew Thompson 		DPRINTF("one or more missing USB endpoints, "
422a593f6b8SAndrew Thompson 		    "error=%s\n", usbd_errstr(error));
42302ac6454SAndrew Thompson 		goto detach;
42402ac6454SAndrew Thompson 	}
42502ac6454SAndrew Thompson 	error = uplcom_reset(sc, uaa->device);
42602ac6454SAndrew Thompson 	if (error) {
42702ac6454SAndrew Thompson 		device_printf(dev, "reset failed, error=%s\n",
428a593f6b8SAndrew Thompson 		    usbd_errstr(error));
42902ac6454SAndrew Thompson 		goto detach;
43002ac6454SAndrew Thompson 	}
4319efb7339SHans Petter Selasky 
4329efb7339SHans Petter Selasky 	if (sc->sc_chiptype != TYPE_PL2303HX) {
4339efb7339SHans Petter Selasky 		/* HX variants seem to lock up after a clear stall request. */
434deefe583SAndrew Thompson 		mtx_lock(&sc->sc_mtx);
435ed6d949aSAndrew Thompson 		usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
436ed6d949aSAndrew Thompson 		usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
437deefe583SAndrew Thompson 		mtx_unlock(&sc->sc_mtx);
4389efb7339SHans Petter Selasky 	} else {
4399efb7339SHans Petter Selasky 		if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
4409efb7339SHans Petter Selasky 		    UPLCOM_SET_REQUEST, 8, 0, 0) ||
4419efb7339SHans Petter Selasky 		    uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
4429efb7339SHans Petter Selasky 		    UPLCOM_SET_REQUEST, 9, 0, 0)) {
4439efb7339SHans Petter Selasky 			goto detach;
4449efb7339SHans Petter Selasky 		}
4459efb7339SHans Petter Selasky 	}
44602ac6454SAndrew Thompson 
447a593f6b8SAndrew Thompson 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
448deefe583SAndrew Thompson 	    &uplcom_callback, &sc->sc_mtx);
44902ac6454SAndrew Thompson 	if (error) {
45002ac6454SAndrew Thompson 		goto detach;
45102ac6454SAndrew Thompson 	}
45202ac6454SAndrew Thompson 	/*
45302ac6454SAndrew Thompson 	 * do the initialization during attach so that the system does not
45402ac6454SAndrew Thompson 	 * sleep during open:
45502ac6454SAndrew Thompson 	 */
456bc65068dSGavin Atkinson 	if (uplcom_pl2303_init(uaa->device, sc->sc_chiptype)) {
457767cb2e2SAndrew Thompson 		device_printf(dev, "init failed\n");
45802ac6454SAndrew Thompson 		goto detach;
45902ac6454SAndrew Thompson 	}
4606416c259SNick Hibma 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
4616416c259SNick Hibma 
46202ac6454SAndrew Thompson 	return (0);
46302ac6454SAndrew Thompson 
46402ac6454SAndrew Thompson detach:
46502ac6454SAndrew Thompson 	uplcom_detach(dev);
46602ac6454SAndrew Thompson 	return (ENXIO);
46702ac6454SAndrew Thompson }
46802ac6454SAndrew Thompson 
46902ac6454SAndrew Thompson static int
47002ac6454SAndrew Thompson uplcom_detach(device_t dev)
47102ac6454SAndrew Thompson {
47202ac6454SAndrew Thompson 	struct uplcom_softc *sc = device_get_softc(dev);
47302ac6454SAndrew Thompson 
47402ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
47502ac6454SAndrew Thompson 
476015bb88fSNick Hibma 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
477a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
47802ac6454SAndrew Thompson 
479c01fc06eSHans Petter Selasky 	device_claim_softc(dev);
480c01fc06eSHans Petter Selasky 
481c01fc06eSHans Petter Selasky 	uplcom_free_softc(sc);
482c01fc06eSHans Petter Selasky 
48302ac6454SAndrew Thompson 	return (0);
48402ac6454SAndrew Thompson }
48502ac6454SAndrew Thompson 
4865805d178SHans Petter Selasky UCOM_UNLOAD_DRAIN(uplcom);
4875805d178SHans Petter Selasky 
4885805d178SHans Petter Selasky static void
489c01fc06eSHans Petter Selasky uplcom_free_softc(struct uplcom_softc *sc)
4905805d178SHans Petter Selasky {
4915805d178SHans Petter Selasky 	if (ucom_unref(&sc->sc_super_ucom)) {
4925805d178SHans Petter Selasky 		mtx_destroy(&sc->sc_mtx);
493c01fc06eSHans Petter Selasky 		device_free_softc(sc);
4945805d178SHans Petter Selasky 	}
4955805d178SHans Petter Selasky }
4965805d178SHans Petter Selasky 
4975805d178SHans Petter Selasky static void
4985805d178SHans Petter Selasky uplcom_free(struct ucom_softc *ucom)
4995805d178SHans Petter Selasky {
500c01fc06eSHans Petter Selasky 	uplcom_free_softc(ucom->sc_parent);
5015805d178SHans Petter Selasky }
5025805d178SHans Petter Selasky 
503e0a69b51SAndrew Thompson static usb_error_t
504760bc48eSAndrew Thompson uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
50502ac6454SAndrew Thompson {
506760bc48eSAndrew Thompson 	struct usb_device_request req;
50702ac6454SAndrew Thompson 
50802ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
50902ac6454SAndrew Thompson 	req.bRequest = UPLCOM_SET_REQUEST;
51002ac6454SAndrew Thompson 	USETW(req.wValue, 0);
51102ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
51202ac6454SAndrew Thompson 	req.wIndex[1] = 0;
51302ac6454SAndrew Thompson 	USETW(req.wLength, 0);
51402ac6454SAndrew Thompson 
515a593f6b8SAndrew Thompson 	return (usbd_do_request(udev, NULL, &req, NULL));
51602ac6454SAndrew Thompson }
51702ac6454SAndrew Thompson 
518bc65068dSGavin Atkinson static usb_error_t
51902d4a225SDimitry Andric uplcom_pl2303_do(struct usb_device *udev, uint8_t req_type, uint8_t request,
520bc65068dSGavin Atkinson     uint16_t value, uint16_t index, uint16_t length)
52102ac6454SAndrew Thompson {
522760bc48eSAndrew Thompson 	struct usb_device_request req;
523e0a69b51SAndrew Thompson 	usb_error_t err;
52402ac6454SAndrew Thompson 	uint8_t buf[4];
52502ac6454SAndrew Thompson 
526bc65068dSGavin Atkinson 	req.bmRequestType = req_type;
527bc65068dSGavin Atkinson 	req.bRequest = request;
528bc65068dSGavin Atkinson 	USETW(req.wValue, value);
529bc65068dSGavin Atkinson 	USETW(req.wIndex, index);
530bc65068dSGavin Atkinson 	USETW(req.wLength, length);
53102ac6454SAndrew Thompson 
532a593f6b8SAndrew Thompson 	err = usbd_do_request(udev, NULL, &req, buf);
53302ac6454SAndrew Thompson 	if (err) {
534a593f6b8SAndrew Thompson 		DPRINTF("error=%s\n", usbd_errstr(err));
535bc65068dSGavin Atkinson 		return (1);
536bc65068dSGavin Atkinson 	}
537bc65068dSGavin Atkinson 	return (0);
538bc65068dSGavin Atkinson }
539bc65068dSGavin Atkinson 
540bc65068dSGavin Atkinson static int
541bc65068dSGavin Atkinson uplcom_pl2303_init(struct usb_device *udev, uint8_t chiptype)
542bc65068dSGavin Atkinson {
543bc65068dSGavin Atkinson 	int err;
544bc65068dSGavin Atkinson 
545bc65068dSGavin Atkinson 	if (uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
546bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0)
547bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
548bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
549bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
550bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0)
551bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
552bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
553bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0)
554bc65068dSGavin Atkinson 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0))
55502ac6454SAndrew Thompson 		return (EIO);
556bc65068dSGavin Atkinson 
557bc65068dSGavin Atkinson 	if (chiptype == TYPE_PL2303HX)
558bc65068dSGavin Atkinson 		err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0);
559bc65068dSGavin Atkinson 	else
560bc65068dSGavin Atkinson 		err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x24, 0);
561bc65068dSGavin Atkinson 	if (err)
562bc65068dSGavin Atkinson 		return (EIO);
563bc65068dSGavin Atkinson 
56402ac6454SAndrew Thompson 	return (0);
56502ac6454SAndrew Thompson }
56602ac6454SAndrew Thompson 
56702ac6454SAndrew Thompson static void
568760bc48eSAndrew Thompson uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
56902ac6454SAndrew Thompson {
57002ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
571760bc48eSAndrew Thompson 	struct usb_device_request req;
57202ac6454SAndrew Thompson 
57302ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
57402ac6454SAndrew Thompson 
57502ac6454SAndrew Thompson 	if (onoff)
57602ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_DTR;
57702ac6454SAndrew Thompson 	else
57802ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_DTR;
57902ac6454SAndrew Thompson 
58002ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
58102ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
58202ac6454SAndrew Thompson 	USETW(req.wValue, sc->sc_line);
58302ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
58402ac6454SAndrew Thompson 	req.wIndex[1] = 0;
58502ac6454SAndrew Thompson 	USETW(req.wLength, 0);
58602ac6454SAndrew Thompson 
587a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
58802ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
58902ac6454SAndrew Thompson }
59002ac6454SAndrew Thompson 
59102ac6454SAndrew Thompson static void
592760bc48eSAndrew Thompson uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
59302ac6454SAndrew Thompson {
59402ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
595760bc48eSAndrew Thompson 	struct usb_device_request req;
59602ac6454SAndrew Thompson 
59702ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
59802ac6454SAndrew Thompson 
59902ac6454SAndrew Thompson 	if (onoff)
60002ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_RTS;
60102ac6454SAndrew Thompson 	else
60202ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_RTS;
60302ac6454SAndrew Thompson 
60402ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
60502ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
60602ac6454SAndrew Thompson 	USETW(req.wValue, sc->sc_line);
60702ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
60802ac6454SAndrew Thompson 	req.wIndex[1] = 0;
60902ac6454SAndrew Thompson 	USETW(req.wLength, 0);
61002ac6454SAndrew Thompson 
611a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
61202ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
61302ac6454SAndrew Thompson }
61402ac6454SAndrew Thompson 
61502ac6454SAndrew Thompson static void
616760bc48eSAndrew Thompson uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
61702ac6454SAndrew Thompson {
61802ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
619760bc48eSAndrew Thompson 	struct usb_device_request req;
62002ac6454SAndrew Thompson 	uint16_t temp;
62102ac6454SAndrew Thompson 
62202ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
62302ac6454SAndrew Thompson 
62402ac6454SAndrew Thompson 	temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
62502ac6454SAndrew Thompson 
62602ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
62702ac6454SAndrew Thompson 	req.bRequest = UCDC_SEND_BREAK;
62802ac6454SAndrew Thompson 	USETW(req.wValue, temp);
62902ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
63002ac6454SAndrew Thompson 	req.wIndex[1] = 0;
63102ac6454SAndrew Thompson 	USETW(req.wLength, 0);
63202ac6454SAndrew Thompson 
633a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
63402ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
63502ac6454SAndrew Thompson }
63602ac6454SAndrew Thompson 
6376d917491SHans Petter Selasky static const uint32_t uplcom_rates[] = {
63802ac6454SAndrew Thompson 	75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
63902ac6454SAndrew Thompson 	19200, 28800, 38400, 57600, 115200,
64002ac6454SAndrew Thompson 	/*
64102ac6454SAndrew Thompson 	 * Higher speeds are probably possible. PL2303X supports up to
64202ac6454SAndrew Thompson 	 * 6Mb and can set any rate
64302ac6454SAndrew Thompson 	 */
64402ac6454SAndrew Thompson 	230400, 460800, 614400, 921600, 1228800
64502ac6454SAndrew Thompson };
64602ac6454SAndrew Thompson 
647432157dcSPedro F. Giffuni #define	N_UPLCOM_RATES	nitems(uplcom_rates)
64802ac6454SAndrew Thompson 
64902ac6454SAndrew Thompson static int
650760bc48eSAndrew Thompson uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
65102ac6454SAndrew Thompson {
652fe0f6362SGavin Atkinson 	struct uplcom_softc *sc = ucom->sc_parent;
65302ac6454SAndrew Thompson 	uint8_t i;
65402ac6454SAndrew Thompson 
65502ac6454SAndrew Thompson 	DPRINTF("\n");
65602ac6454SAndrew Thompson 
657fe0f6362SGavin Atkinson 	/**
658fe0f6362SGavin Atkinson 	 * Check requested baud rate.
659fe0f6362SGavin Atkinson 	 *
660fe0f6362SGavin Atkinson 	 * The PL2303 can only set specific baud rates, up to 1228800 baud.
661fe0f6362SGavin Atkinson 	 * The PL2303X can set any baud rate up to 6Mb.
662fe0f6362SGavin Atkinson 	 * The PL2303HX rev. D can set any baud rate up to 12Mb.
663fe0f6362SGavin Atkinson 	 *
664fe0f6362SGavin Atkinson 	 * XXX: We currently cannot identify the PL2303HX rev. D, so treat
665fe0f6362SGavin Atkinson 	 *      it the same as the PL2303X.
666fe0f6362SGavin Atkinson 	 */
667bc65068dSGavin Atkinson 	if (sc->sc_chiptype != TYPE_PL2303HX) {
668fe0f6362SGavin Atkinson 		for (i = 0; i < N_UPLCOM_RATES; i++) {
669fe0f6362SGavin Atkinson 			if (uplcom_rates[i] == t->c_ospeed)
670fe0f6362SGavin Atkinson 				return (0);
67102ac6454SAndrew Thompson 		}
67202ac6454SAndrew Thompson  	} else {
673fe0f6362SGavin Atkinson 		if (t->c_ospeed <= 6000000)
674fe0f6362SGavin Atkinson 			return (0);
67502ac6454SAndrew Thompson 	}
67602ac6454SAndrew Thompson 
677fe0f6362SGavin Atkinson 	DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed);
678fe0f6362SGavin Atkinson 	return (EIO);
67902ac6454SAndrew Thompson }
68002ac6454SAndrew Thompson 
68102ac6454SAndrew Thompson static void
682760bc48eSAndrew Thompson uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
68302ac6454SAndrew Thompson {
68402ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
685760bc48eSAndrew Thompson 	struct usb_cdc_line_state ls;
686760bc48eSAndrew Thompson 	struct usb_device_request req;
68702ac6454SAndrew Thompson 
68802ac6454SAndrew Thompson 	DPRINTF("sc = %p\n", sc);
68902ac6454SAndrew Thompson 
690271ae033SHans Petter Selasky 	memset(&ls, 0, sizeof(ls));
69102ac6454SAndrew Thompson 
69202ac6454SAndrew Thompson 	USETDW(ls.dwDTERate, t->c_ospeed);
69302ac6454SAndrew Thompson 
69402ac6454SAndrew Thompson 	if (t->c_cflag & CSTOPB) {
69502ac6454SAndrew Thompson 		ls.bCharFormat = UCDC_STOP_BIT_2;
69602ac6454SAndrew Thompson 	} else {
69702ac6454SAndrew Thompson 		ls.bCharFormat = UCDC_STOP_BIT_1;
69802ac6454SAndrew Thompson 	}
69902ac6454SAndrew Thompson 
70002ac6454SAndrew Thompson 	if (t->c_cflag & PARENB) {
70102ac6454SAndrew Thompson 		if (t->c_cflag & PARODD) {
70202ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_ODD;
70302ac6454SAndrew Thompson 		} else {
70402ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_EVEN;
70502ac6454SAndrew Thompson 		}
70602ac6454SAndrew Thompson 	} else {
70702ac6454SAndrew Thompson 		ls.bParityType = UCDC_PARITY_NONE;
70802ac6454SAndrew Thompson 	}
70902ac6454SAndrew Thompson 
71002ac6454SAndrew Thompson 	switch (t->c_cflag & CSIZE) {
71102ac6454SAndrew Thompson 	case CS5:
71202ac6454SAndrew Thompson 		ls.bDataBits = 5;
71302ac6454SAndrew Thompson 		break;
71402ac6454SAndrew Thompson 	case CS6:
71502ac6454SAndrew Thompson 		ls.bDataBits = 6;
71602ac6454SAndrew Thompson 		break;
71702ac6454SAndrew Thompson 	case CS7:
71802ac6454SAndrew Thompson 		ls.bDataBits = 7;
71902ac6454SAndrew Thompson 		break;
72002ac6454SAndrew Thompson 	case CS8:
72102ac6454SAndrew Thompson 		ls.bDataBits = 8;
72202ac6454SAndrew Thompson 		break;
72302ac6454SAndrew Thompson 	}
72402ac6454SAndrew Thompson 
72502ac6454SAndrew Thompson 	DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
72602ac6454SAndrew Thompson 	    UGETDW(ls.dwDTERate), ls.bCharFormat,
72702ac6454SAndrew Thompson 	    ls.bParityType, ls.bDataBits);
72802ac6454SAndrew Thompson 
72902ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
73002ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_LINE_CODING;
73102ac6454SAndrew Thompson 	USETW(req.wValue, 0);
73202ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_data_iface_no;
73302ac6454SAndrew Thompson 	req.wIndex[1] = 0;
73402ac6454SAndrew Thompson 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
73502ac6454SAndrew Thompson 
736a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
73702ac6454SAndrew Thompson 	    &req, &ls, 0, 1000);
73802ac6454SAndrew Thompson 
73902ac6454SAndrew Thompson 	if (t->c_cflag & CRTSCTS) {
74002ac6454SAndrew Thompson 
74102ac6454SAndrew Thompson 		DPRINTF("crtscts = on\n");
74202ac6454SAndrew Thompson 
74302ac6454SAndrew Thompson 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
74402ac6454SAndrew Thompson 		req.bRequest = UPLCOM_SET_REQUEST;
74502ac6454SAndrew Thompson 		USETW(req.wValue, 0);
746bc65068dSGavin Atkinson 		if (sc->sc_chiptype == TYPE_PL2303HX)
74702ac6454SAndrew Thompson 			USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
74802ac6454SAndrew Thompson 		else
74902ac6454SAndrew Thompson 			USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
75002ac6454SAndrew Thompson 		USETW(req.wLength, 0);
75102ac6454SAndrew Thompson 
752a593f6b8SAndrew Thompson 		ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
75302ac6454SAndrew Thompson 		    &req, NULL, 0, 1000);
75402ac6454SAndrew Thompson 	} else {
75502ac6454SAndrew Thompson 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
75602ac6454SAndrew Thompson 		req.bRequest = UPLCOM_SET_REQUEST;
75702ac6454SAndrew Thompson 		USETW(req.wValue, 0);
75802ac6454SAndrew Thompson 		USETW(req.wIndex, 0);
75902ac6454SAndrew Thompson 		USETW(req.wLength, 0);
760a593f6b8SAndrew Thompson 		ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
76102ac6454SAndrew Thompson 		    &req, NULL, 0, 1000);
76202ac6454SAndrew Thompson 	}
76302ac6454SAndrew Thompson }
76402ac6454SAndrew Thompson 
76502ac6454SAndrew Thompson static void
766760bc48eSAndrew Thompson uplcom_start_read(struct ucom_softc *ucom)
76702ac6454SAndrew Thompson {
76802ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
76902ac6454SAndrew Thompson 
77002ac6454SAndrew Thompson 	/* start interrupt endpoint */
771a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
77202ac6454SAndrew Thompson 
77302ac6454SAndrew Thompson 	/* start read endpoint */
774a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
77502ac6454SAndrew Thompson }
77602ac6454SAndrew Thompson 
77702ac6454SAndrew Thompson static void
778760bc48eSAndrew Thompson uplcom_stop_read(struct ucom_softc *ucom)
77902ac6454SAndrew Thompson {
78002ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
78102ac6454SAndrew Thompson 
78202ac6454SAndrew Thompson 	/* stop interrupt endpoint */
783a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
78402ac6454SAndrew Thompson 
78502ac6454SAndrew Thompson 	/* stop read endpoint */
786a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
78702ac6454SAndrew Thompson }
78802ac6454SAndrew Thompson 
78902ac6454SAndrew Thompson static void
790760bc48eSAndrew Thompson uplcom_start_write(struct ucom_softc *ucom)
79102ac6454SAndrew Thompson {
79202ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
79302ac6454SAndrew Thompson 
794a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
79502ac6454SAndrew Thompson }
79602ac6454SAndrew Thompson 
79702ac6454SAndrew Thompson static void
798760bc48eSAndrew Thompson uplcom_stop_write(struct ucom_softc *ucom)
79902ac6454SAndrew Thompson {
80002ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
80102ac6454SAndrew Thompson 
802a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
80302ac6454SAndrew Thompson }
80402ac6454SAndrew Thompson 
80502ac6454SAndrew Thompson static void
806760bc48eSAndrew Thompson uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
80702ac6454SAndrew Thompson {
80802ac6454SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
80902ac6454SAndrew Thompson 
81002ac6454SAndrew Thompson 	DPRINTF("\n");
81102ac6454SAndrew Thompson 
812fe67c846SIan Lepore 	/* XXX Note: sc_lsr is always zero */
81302ac6454SAndrew Thompson 	*lsr = sc->sc_lsr;
81402ac6454SAndrew Thompson 	*msr = sc->sc_msr;
81502ac6454SAndrew Thompson }
81602ac6454SAndrew Thompson 
81702ac6454SAndrew Thompson static void
818ed6d949aSAndrew Thompson uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
81902ac6454SAndrew Thompson {
820ed6d949aSAndrew Thompson 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
821ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
82202ac6454SAndrew Thompson 	uint8_t buf[9];
823ed6d949aSAndrew Thompson 	int actlen;
824ed6d949aSAndrew Thompson 
825ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
82602ac6454SAndrew Thompson 
82702ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
82802ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
82902ac6454SAndrew Thompson 
830ed6d949aSAndrew Thompson 		DPRINTF("actlen = %u\n", actlen);
83102ac6454SAndrew Thompson 
832ed6d949aSAndrew Thompson 		if (actlen >= 9) {
83302ac6454SAndrew Thompson 
834ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
835ed6d949aSAndrew Thompson 			usbd_copy_out(pc, 0, buf, sizeof(buf));
83602ac6454SAndrew Thompson 
83702ac6454SAndrew Thompson 			DPRINTF("status = 0x%02x\n", buf[8]);
83802ac6454SAndrew Thompson 
83902ac6454SAndrew Thompson 			sc->sc_lsr = 0;
84002ac6454SAndrew Thompson 			sc->sc_msr = 0;
84102ac6454SAndrew Thompson 
84202ac6454SAndrew Thompson 			if (buf[8] & RSAQ_STATUS_CTS) {
84302ac6454SAndrew Thompson 				sc->sc_msr |= SER_CTS;
84402ac6454SAndrew Thompson 			}
84502ac6454SAndrew Thompson 			if (buf[8] & RSAQ_STATUS_DSR) {
84602ac6454SAndrew Thompson 				sc->sc_msr |= SER_DSR;
84702ac6454SAndrew Thompson 			}
84802ac6454SAndrew Thompson 			if (buf[8] & RSAQ_STATUS_DCD) {
84902ac6454SAndrew Thompson 				sc->sc_msr |= SER_DCD;
85002ac6454SAndrew Thompson 			}
851a593f6b8SAndrew Thompson 			ucom_status_change(&sc->sc_ucom);
85202ac6454SAndrew Thompson 		}
85302ac6454SAndrew Thompson 	case USB_ST_SETUP:
85402ac6454SAndrew Thompson tr_setup:
855ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
856a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
85702ac6454SAndrew Thompson 		return;
85802ac6454SAndrew Thompson 
85902ac6454SAndrew Thompson 	default:			/* Error */
860ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
86102ac6454SAndrew Thompson 			/* try to clear stall first */
862ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
86302ac6454SAndrew Thompson 			goto tr_setup;
86402ac6454SAndrew Thompson 		}
86502ac6454SAndrew Thompson 		return;
86602ac6454SAndrew Thompson 	}
86702ac6454SAndrew Thompson }
86802ac6454SAndrew Thompson 
86902ac6454SAndrew Thompson static void
870ed6d949aSAndrew Thompson uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
87102ac6454SAndrew Thompson {
872ed6d949aSAndrew Thompson 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
873ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
87402ac6454SAndrew Thompson 	uint32_t actlen;
87502ac6454SAndrew Thompson 
87602ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
87702ac6454SAndrew Thompson 	case USB_ST_SETUP:
87802ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
87902ac6454SAndrew Thompson tr_setup:
880ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
881ed6d949aSAndrew Thompson 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
88202ac6454SAndrew Thompson 		    UPLCOM_BULK_BUF_SIZE, &actlen)) {
88302ac6454SAndrew Thompson 
88402ac6454SAndrew Thompson 			DPRINTF("actlen = %d\n", actlen);
88502ac6454SAndrew Thompson 
886ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, actlen);
887a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
88802ac6454SAndrew Thompson 		}
88902ac6454SAndrew Thompson 		return;
89002ac6454SAndrew Thompson 
89102ac6454SAndrew Thompson 	default:			/* Error */
892ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
89302ac6454SAndrew Thompson 			/* try to clear stall first */
894ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
89502ac6454SAndrew Thompson 			goto tr_setup;
89602ac6454SAndrew Thompson 		}
89702ac6454SAndrew Thompson 		return;
89802ac6454SAndrew Thompson 	}
89902ac6454SAndrew Thompson }
90002ac6454SAndrew Thompson 
90102ac6454SAndrew Thompson static void
902ed6d949aSAndrew Thompson uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
90302ac6454SAndrew Thompson {
904ed6d949aSAndrew Thompson 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
905ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
906ed6d949aSAndrew Thompson 	int actlen;
907ed6d949aSAndrew Thompson 
908ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
90902ac6454SAndrew Thompson 
91002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
91102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
912ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
913ed6d949aSAndrew Thompson 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
91402ac6454SAndrew Thompson 
91502ac6454SAndrew Thompson 	case USB_ST_SETUP:
91602ac6454SAndrew Thompson tr_setup:
917ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
918a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
91902ac6454SAndrew Thompson 		return;
92002ac6454SAndrew Thompson 
92102ac6454SAndrew Thompson 	default:			/* Error */
922ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
92302ac6454SAndrew Thompson 			/* try to clear stall first */
924ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
92502ac6454SAndrew Thompson 			goto tr_setup;
92602ac6454SAndrew Thompson 		}
92702ac6454SAndrew Thompson 		return;
92802ac6454SAndrew Thompson 	}
92902ac6454SAndrew Thompson }
930655dc9d0SAndrew Thompson 
931655dc9d0SAndrew Thompson static void
932655dc9d0SAndrew Thompson uplcom_poll(struct ucom_softc *ucom)
933655dc9d0SAndrew Thompson {
934655dc9d0SAndrew Thompson 	struct uplcom_softc *sc = ucom->sc_parent;
935655dc9d0SAndrew Thompson 	usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);
936655dc9d0SAndrew Thompson }
937