xref: /freebsd/sys/dev/usb/serial/uplcom.c (revision 369d70ec9282cccf131ab0400b05dad13ba8116d)
1 /*	$NetBSD: uplcom.c,v 1.21 2001/11/13 06:24:56 lukem Exp $	*/
2 
3 #include <sys/cdefs.h>
4 __FBSDID("$FreeBSD$");
5 
6 /*-
7  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause-NetBSD
8  *
9  * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*-
35  * Copyright (c) 2001 The NetBSD Foundation, Inc.
36  * All rights reserved.
37  *
38  * This code is derived from software contributed to The NetBSD Foundation
39  * by Ichiro FUKUHARA (ichiro@ichiro.org).
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
51  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
54  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60  * POSSIBILITY OF SUCH DAMAGE.
61  */
62 
63 /*
64  * This driver supports several USB-to-RS232 serial adapters driven by
65  * Prolific PL-2303, PL-2303X and probably PL-2303HX USB-to-RS232
66  * bridge chip.  The adapters are sold under many different brand
67  * names.
68  *
69  * Datasheets are available at Prolific www site at
70  * http://www.prolific.com.tw.  The datasheets don't contain full
71  * programming information for the chip.
72  *
73  * PL-2303HX is probably programmed the same as PL-2303X.
74  *
75  * There are several differences between PL-2303 and PL-2303(H)X.
76  * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_
77  * different command for controlling CRTSCTS and needs special
78  * sequence of commands for initialization which aren't also
79  * documented in the datasheet.
80  */
81 
82 #include <sys/stdint.h>
83 #include <sys/stddef.h>
84 #include <sys/param.h>
85 #include <sys/queue.h>
86 #include <sys/types.h>
87 #include <sys/systm.h>
88 #include <sys/kernel.h>
89 #include <sys/bus.h>
90 #include <sys/module.h>
91 #include <sys/lock.h>
92 #include <sys/mutex.h>
93 #include <sys/condvar.h>
94 #include <sys/sysctl.h>
95 #include <sys/sx.h>
96 #include <sys/unistd.h>
97 #include <sys/callout.h>
98 #include <sys/malloc.h>
99 #include <sys/priv.h>
100 
101 #include <dev/usb/usb.h>
102 #include <dev/usb/usbdi.h>
103 #include <dev/usb/usbdi_util.h>
104 #include <dev/usb/usb_cdc.h>
105 #include "usbdevs.h"
106 
107 #define	USB_DEBUG_VAR uplcom_debug
108 #include <dev/usb/usb_debug.h>
109 #include <dev/usb/usb_process.h>
110 
111 #include <dev/usb/serial/usb_serial.h>
112 
113 #ifdef USB_DEBUG
114 static int uplcom_debug = 0;
115 
116 static SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
117     "USB uplcom");
118 SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RWTUN,
119     &uplcom_debug, 0, "Debug level");
120 #endif
121 
122 #define	UPLCOM_MODVER			1	/* module version */
123 
124 #define	UPLCOM_CONFIG_INDEX		0
125 #define	UPLCOM_IFACE_INDEX		0
126 #define	UPLCOM_SECOND_IFACE_INDEX	1
127 
128 #ifndef UPLCOM_INTR_INTERVAL
129 #define	UPLCOM_INTR_INTERVAL		0	/* default */
130 #endif
131 
132 #define	UPLCOM_BULK_BUF_SIZE 1024	/* bytes */
133 
134 #define	UPLCOM_SET_REQUEST		0x01
135 #define	UPLCOM_SET_CRTSCTS		0x41
136 #define	UPLCOM_SET_CRTSCTS_PL2303X	0x61
137 #define	RSAQ_STATUS_CTS			0x80
138 #define	RSAQ_STATUS_OVERRUN_ERROR	0x40
139 #define	RSAQ_STATUS_PARITY_ERROR	0x20
140 #define	RSAQ_STATUS_FRAME_ERROR	0x10
141 #define	RSAQ_STATUS_RING		0x08
142 #define	RSAQ_STATUS_BREAK_ERROR	0x04
143 #define	RSAQ_STATUS_DSR			0x02
144 #define	RSAQ_STATUS_DCD			0x01
145 
146 #define	TYPE_PL2303			0
147 #define	TYPE_PL2303HX			1
148 #define	TYPE_PL2303HXD			2
149 
150 #define	UPLCOM_STATE_INDEX		8
151 
152 enum {
153 	UPLCOM_BULK_DT_WR,
154 	UPLCOM_BULK_DT_RD,
155 	UPLCOM_INTR_DT_RD,
156 	UPLCOM_N_TRANSFER,
157 };
158 
159 struct uplcom_softc {
160 	struct ucom_super_softc sc_super_ucom;
161 	struct ucom_softc sc_ucom;
162 
163 	struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER];
164 	struct usb_device *sc_udev;
165 	struct mtx sc_mtx;
166 
167 	uint16_t sc_line;
168 
169 	uint8_t	sc_lsr;			/* local status register */
170 	uint8_t	sc_msr;			/* uplcom status register */
171 	uint8_t	sc_chiptype;		/* type of chip */
172 	uint8_t	sc_ctrl_iface_no;
173 	uint8_t	sc_data_iface_no;
174 	uint8_t	sc_iface_index[2];
175 };
176 
177 /* prototypes */
178 
179 static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *);
180 static usb_error_t uplcom_pl2303_do(struct usb_device *, uint8_t, uint8_t,
181 			uint16_t, uint16_t, uint16_t);
182 static int	uplcom_pl2303_init(struct usb_device *, uint8_t);
183 static void	uplcom_free(struct ucom_softc *);
184 static void	uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
185 static void	uplcom_cfg_set_rts(struct ucom_softc *, uint8_t);
186 static void	uplcom_cfg_set_break(struct ucom_softc *, uint8_t);
187 static int	uplcom_pre_param(struct ucom_softc *, struct termios *);
188 static void	uplcom_cfg_param(struct ucom_softc *, struct termios *);
189 static void	uplcom_start_read(struct ucom_softc *);
190 static void	uplcom_stop_read(struct ucom_softc *);
191 static void	uplcom_start_write(struct ucom_softc *);
192 static void	uplcom_stop_write(struct ucom_softc *);
193 static void	uplcom_cfg_get_status(struct ucom_softc *, uint8_t *,
194 		    uint8_t *);
195 static void	uplcom_poll(struct ucom_softc *ucom);
196 
197 static device_probe_t uplcom_probe;
198 static device_attach_t uplcom_attach;
199 static device_detach_t uplcom_detach;
200 static void uplcom_free_softc(struct uplcom_softc *);
201 
202 static usb_callback_t uplcom_intr_callback;
203 static usb_callback_t uplcom_write_callback;
204 static usb_callback_t uplcom_read_callback;
205 
206 static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
207 	[UPLCOM_BULK_DT_WR] = {
208 		.type = UE_BULK,
209 		.endpoint = UE_ADDR_ANY,
210 		.direction = UE_DIR_OUT,
211 		.bufsize = UPLCOM_BULK_BUF_SIZE,
212 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
213 		.callback = &uplcom_write_callback,
214 		.if_index = 0,
215 	},
216 
217 	[UPLCOM_BULK_DT_RD] = {
218 		.type = UE_BULK,
219 		.endpoint = UE_ADDR_ANY,
220 		.direction = UE_DIR_IN,
221 		.bufsize = UPLCOM_BULK_BUF_SIZE,
222 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
223 		.callback = &uplcom_read_callback,
224 		.if_index = 0,
225 	},
226 
227 	[UPLCOM_INTR_DT_RD] = {
228 		.type = UE_INTERRUPT,
229 		.endpoint = UE_ADDR_ANY,
230 		.direction = UE_DIR_IN,
231 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
232 		.bufsize = 0,	/* use wMaxPacketSize */
233 		.callback = &uplcom_intr_callback,
234 		.if_index = 1,
235 	},
236 };
237 
238 static struct ucom_callback uplcom_callback = {
239 	.ucom_cfg_get_status = &uplcom_cfg_get_status,
240 	.ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
241 	.ucom_cfg_set_rts = &uplcom_cfg_set_rts,
242 	.ucom_cfg_set_break = &uplcom_cfg_set_break,
243 	.ucom_cfg_param = &uplcom_cfg_param,
244 	.ucom_pre_param = &uplcom_pre_param,
245 	.ucom_start_read = &uplcom_start_read,
246 	.ucom_stop_read = &uplcom_stop_read,
247 	.ucom_start_write = &uplcom_start_write,
248 	.ucom_stop_write = &uplcom_stop_write,
249 	.ucom_poll = &uplcom_poll,
250 	.ucom_free = &uplcom_free,
251 };
252 
253 #define	UPLCOM_DEV(v,p)				\
254   { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p) }
255 
256 static const STRUCT_USB_HOST_ID uplcom_devs[] = {
257 	UPLCOM_DEV(ACERP, S81),			/* BenQ S81 phone */
258 	UPLCOM_DEV(ADLINK, ND6530),		/* ADLINK ND-6530 USB-Serial */
259 	UPLCOM_DEV(ALCATEL, OT535),		/* Alcatel One Touch 535/735 */
260 	UPLCOM_DEV(ALCOR, AU9720),		/* Alcor AU9720 USB 2.0-RS232 */
261 	UPLCOM_DEV(ANCHOR, SERIAL),		/* Anchor Serial adapter */
262 	UPLCOM_DEV(ATEN, UC232A),		/* PLANEX USB-RS232 URS-03 */
263 	UPLCOM_DEV(ATEN, UC232B),		/* Prolific USB-RS232 Controller D */
264 	UPLCOM_DEV(BELKIN, F5U257),		/* Belkin F5U257 USB to Serial */
265 	UPLCOM_DEV(COREGA, CGUSBRS232R),	/* Corega CG-USBRS232R */
266 	UPLCOM_DEV(EPSON, CRESSI_EDY),		/* Cressi Edy diving computer */
267 	UPLCOM_DEV(EPSON, N2ITION3),		/* Zeagle N2iTion3 diving computer */
268 	UPLCOM_DEV(ELECOM, UCSGT),		/* ELECOM UC-SGT Serial Adapter */
269 	UPLCOM_DEV(ELECOM, UCSGT0),		/* ELECOM UC-SGT Serial Adapter */
270 	UPLCOM_DEV(HAL, IMR001),		/* HAL Corporation Crossam2+USB */
271 	UPLCOM_DEV(HP, LD220),			/* HP LD220 POS Display */
272 	UPLCOM_DEV(IODATA, USBRSAQ),		/* I/O DATA USB-RSAQ */
273 	UPLCOM_DEV(IODATA, USBRSAQ5),		/* I/O DATA USB-RSAQ5 */
274 	UPLCOM_DEV(ITEGNO, WM1080A),		/* iTegno WM1080A GSM/GFPRS modem */
275 	UPLCOM_DEV(ITEGNO, WM2080A),		/* iTegno WM2080A CDMA modem */
276 	UPLCOM_DEV(LEADTEK, 9531),		/* Leadtek 9531 GPS */
277 	UPLCOM_DEV(MICROSOFT, 700WX),		/* Microsoft Palm 700WX */
278 	UPLCOM_DEV(MOBILEACTION, MA620),	/* Mobile Action MA-620 Infrared Adapter */
279 	UPLCOM_DEV(NETINDEX, WS002IN),		/* Willcom W-S002IN */
280 	UPLCOM_DEV(NOKIA2, CA42),		/* Nokia CA-42 cable */
281 	UPLCOM_DEV(OTI, DKU5),			/* OTI DKU-5 cable */
282 	UPLCOM_DEV(PANASONIC, TYTP50P6S),	/* Panasonic TY-TP50P6-S flat screen */
283 	UPLCOM_DEV(PLX, CA42),			/* PLX CA-42 clone cable */
284 	UPLCOM_DEV(PROLIFIC, ALLTRONIX_GPRS),	/* Alltronix ACM003U00 modem */
285 	UPLCOM_DEV(PROLIFIC, ALDIGA_AL11U),	/* AlDiga AL-11U modem */
286 	UPLCOM_DEV(PROLIFIC, DCU11),		/* DCU-11 Phone Cable */
287 	UPLCOM_DEV(PROLIFIC, HCR331),		/* HCR331 Card Reader */
288 	UPLCOM_DEV(PROLIFIC, MICROMAX_610U),	/* Micromax 610U modem */
289 	UPLCOM_DEV(PROLIFIC, MOTOROLA),		/* Motorola cable */
290 	UPLCOM_DEV(PROLIFIC, PHAROS),		/* Prolific Pharos */
291 	UPLCOM_DEV(PROLIFIC, PL2303),		/* Generic adapter */
292 	UPLCOM_DEV(PROLIFIC, RSAQ2),		/* I/O DATA USB-RSAQ2 */
293 	UPLCOM_DEV(PROLIFIC, RSAQ3),		/* I/O DATA USB-RSAQ3 */
294 	UPLCOM_DEV(PROLIFIC, UIC_MSR206),	/* UIC MSR206 Card Reader */
295 	UPLCOM_DEV(PROLIFIC2, PL2303),		/* Prolific adapter */
296 	UPLCOM_DEV(RADIOSHACK, USBCABLE),	/* Radio Shack USB Adapter */
297 	UPLCOM_DEV(RATOC, REXUSB60),		/* RATOC REX-USB60 */
298 	UPLCOM_DEV(SAGEM, USBSERIAL),		/* Sagem USB-Serial Controller */
299 	UPLCOM_DEV(SAMSUNG, I330),		/* Samsung I330 phone cradle */
300 	UPLCOM_DEV(SANWA, KB_USB2),		/* Sanwa KB-USB2 Multimeter cable */
301 	UPLCOM_DEV(SIEMENS3, EF81),		/* Siemens EF81 */
302 	UPLCOM_DEV(SIEMENS3, SX1),		/* Siemens SX1 */
303 	UPLCOM_DEV(SIEMENS3, X65),		/* Siemens X65 */
304 	UPLCOM_DEV(SIEMENS3, X75),		/* Siemens X75 */
305 	UPLCOM_DEV(SITECOM, SERIAL),		/* Sitecom USB to Serial */
306 	UPLCOM_DEV(SMART, PL2303),		/* SMART Technologies USB to Serial */
307 	UPLCOM_DEV(SONY, QN3),			/* Sony QN3 phone cable */
308 	UPLCOM_DEV(SONYERICSSON, DATAPILOT),	/* Sony Ericsson Datapilot */
309 	UPLCOM_DEV(SONYERICSSON, DCU10),	/* Sony Ericsson DCU-10 Cable */
310 	UPLCOM_DEV(SOURCENEXT, KEIKAI8),	/* SOURCENEXT KeikaiDenwa 8 */
311 	UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG),	/* SOURCENEXT KeikaiDenwa 8 with charger */
312 	UPLCOM_DEV(SPEEDDRAGON, MS3303H),	/* Speed Dragon USB-Serial */
313 	UPLCOM_DEV(SYNTECH, CPT8001C),		/* Syntech CPT-8001C Barcode scanner */
314 	UPLCOM_DEV(TDK, UHA6400),		/* TDK USB-PHS Adapter UHA6400 */
315 	UPLCOM_DEV(TDK, UPA9664),		/* TDK USB-PHS Adapter UPA9664 */
316 	UPLCOM_DEV(TRIPPLITE, U209),		/* Tripp-Lite U209-000-R USB to Serial */
317 	UPLCOM_DEV(YCCABLE, PL2303),		/* YC Cable USB-Serial */
318 };
319 #undef UPLCOM_DEV
320 
321 static device_method_t uplcom_methods[] = {
322 	DEVMETHOD(device_probe, uplcom_probe),
323 	DEVMETHOD(device_attach, uplcom_attach),
324 	DEVMETHOD(device_detach, uplcom_detach),
325 	DEVMETHOD_END
326 };
327 
328 static devclass_t uplcom_devclass;
329 
330 static driver_t uplcom_driver = {
331 	.name = "uplcom",
332 	.methods = uplcom_methods,
333 	.size = sizeof(struct uplcom_softc),
334 };
335 
336 DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0);
337 MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
338 MODULE_DEPEND(uplcom, usb, 1, 1, 1);
339 MODULE_VERSION(uplcom, UPLCOM_MODVER);
340 USB_PNP_HOST_INFO(uplcom_devs);
341 
342 static int
343 uplcom_probe(device_t dev)
344 {
345 	struct usb_attach_arg *uaa = device_get_ivars(dev);
346 
347 	DPRINTFN(11, "\n");
348 
349 	if (uaa->usb_mode != USB_MODE_HOST) {
350 		return (ENXIO);
351 	}
352 	if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
353 		return (ENXIO);
354 	}
355 	if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
356 		return (ENXIO);
357 	}
358 	return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
359 }
360 
361 static int
362 uplcom_attach(device_t dev)
363 {
364 	struct usb_attach_arg *uaa = device_get_ivars(dev);
365 	struct uplcom_softc *sc = device_get_softc(dev);
366 	struct usb_interface *iface;
367 	struct usb_interface_descriptor *id;
368 	struct usb_device_descriptor *dd;
369 	int error;
370 
371 	DPRINTFN(11, "\n");
372 
373 	device_set_usb_desc(dev);
374 	mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
375 	ucom_ref(&sc->sc_super_ucom);
376 
377 	DPRINTF("sc = %p\n", sc);
378 
379 	sc->sc_udev = uaa->device;
380 
381 	dd = usbd_get_device_descriptor(sc->sc_udev);
382 
383 	switch (UGETW(dd->bcdDevice)) {
384 	case 0x0300:
385 		sc->sc_chiptype = TYPE_PL2303HX;
386 		/* or TA, that is HX with external crystal */
387 		break;
388 	case 0x0400:
389 		sc->sc_chiptype = TYPE_PL2303HXD;
390 		/* or EA, that is HXD with ESD protection */
391 		/* or RA, that has internal voltage level converter that works only up to 1Mbaud (!) */
392 		break;
393 	case 0x0500:
394 		sc->sc_chiptype = TYPE_PL2303HXD;
395 		/* in fact it's TB, that is HXD with external crystal */
396 		break;
397 	default:
398 		/* NOTE: I have no info about the bcdDevice for the base PL2303 (up to 1.2Mbaud,
399 		   only fixed rates) and for PL2303SA (8-pin chip, up to 115200 baud */
400 		/* Determine the chip type.  This algorithm is taken from Linux. */
401 		if (dd->bDeviceClass == 0x02)
402 			sc->sc_chiptype = TYPE_PL2303;
403 		else if (dd->bMaxPacketSize == 0x40)
404 			sc->sc_chiptype = TYPE_PL2303HX;
405 		else
406 			sc->sc_chiptype = TYPE_PL2303;
407 		break;
408 	}
409 
410 	switch (sc->sc_chiptype) {
411 	case TYPE_PL2303:
412 		DPRINTF("chiptype: 2303\n");
413 		break;
414 	case TYPE_PL2303HX:
415 		DPRINTF("chiptype: 2303HX/TA\n");
416 		break;
417 	case TYPE_PL2303HXD:
418 		DPRINTF("chiptype: 2303HXD/TB/RA/EA\n");
419 		break;
420 	default:
421 		DPRINTF("chiptype: unknown %d\n", sc->sc_chiptype);
422 		break;
423 	}
424 
425 	/*
426 	 * USB-RSAQ1 has two interface
427 	 *
428 	 *  USB-RSAQ1       | USB-RSAQ2
429 	 * -----------------+-----------------
430 	 * Interface 0      |Interface 0
431 	 *  Interrupt(0x81) | Interrupt(0x81)
432 	 * -----------------+ BulkIN(0x02)
433 	 * Interface 1	    | BulkOUT(0x83)
434 	 *   BulkIN(0x02)   |
435 	 *   BulkOUT(0x83)  |
436 	 */
437 
438 	sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
439 	sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
440 
441 	iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
442 	if (iface) {
443 		id = usbd_get_interface_descriptor(iface);
444 		if (id == NULL) {
445 			device_printf(dev, "no interface descriptor (2)\n");
446 			goto detach;
447 		}
448 		sc->sc_data_iface_no = id->bInterfaceNumber;
449 		sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
450 		usbd_set_parent_iface(uaa->device,
451 		    UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
452 	} else {
453 		sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
454 		sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
455 	}
456 
457 	error = usbd_transfer_setup(uaa->device,
458 	    sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
459 	    UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
460 	if (error) {
461 		DPRINTF("one or more missing USB endpoints, "
462 		    "error=%s\n", usbd_errstr(error));
463 		goto detach;
464 	}
465 	error = uplcom_reset(sc, uaa->device);
466 	if (error) {
467 		device_printf(dev, "reset failed, error=%s\n",
468 		    usbd_errstr(error));
469 		goto detach;
470 	}
471 
472 	if (sc->sc_chiptype == TYPE_PL2303) {
473 		/* HX variants seem to lock up after a clear stall request. */
474 		mtx_lock(&sc->sc_mtx);
475 		usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
476 		usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
477 		mtx_unlock(&sc->sc_mtx);
478 	} else {
479 		/* reset upstream data pipes */
480 		if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
481 		    UPLCOM_SET_REQUEST, 8, 0, 0) ||
482 		    uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
483 		    UPLCOM_SET_REQUEST, 9, 0, 0)) {
484 			goto detach;
485 		}
486 	}
487 
488 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
489 	    &uplcom_callback, &sc->sc_mtx);
490 	if (error) {
491 		goto detach;
492 	}
493 	/*
494 	 * do the initialization during attach so that the system does not
495 	 * sleep during open:
496 	 */
497 	if (uplcom_pl2303_init(uaa->device, sc->sc_chiptype)) {
498 		device_printf(dev, "init failed\n");
499 		goto detach;
500 	}
501 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
502 
503 	return (0);
504 
505 detach:
506 	uplcom_detach(dev);
507 	return (ENXIO);
508 }
509 
510 static int
511 uplcom_detach(device_t dev)
512 {
513 	struct uplcom_softc *sc = device_get_softc(dev);
514 
515 	DPRINTF("sc=%p\n", sc);
516 
517 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
518 	usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
519 
520 	device_claim_softc(dev);
521 
522 	uplcom_free_softc(sc);
523 
524 	return (0);
525 }
526 
527 UCOM_UNLOAD_DRAIN(uplcom);
528 
529 static void
530 uplcom_free_softc(struct uplcom_softc *sc)
531 {
532 	if (ucom_unref(&sc->sc_super_ucom)) {
533 		mtx_destroy(&sc->sc_mtx);
534 		device_free_softc(sc);
535 	}
536 }
537 
538 static void
539 uplcom_free(struct ucom_softc *ucom)
540 {
541 	uplcom_free_softc(ucom->sc_parent);
542 }
543 
544 static usb_error_t
545 uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
546 {
547 	struct usb_device_request req;
548 
549 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
550 	req.bRequest = UPLCOM_SET_REQUEST;
551 	USETW(req.wValue, 0);
552 	req.wIndex[0] = sc->sc_data_iface_no;
553 	req.wIndex[1] = 0;
554 	USETW(req.wLength, 0);
555 
556 	return (usbd_do_request(udev, NULL, &req, NULL));
557 }
558 
559 static usb_error_t
560 uplcom_pl2303_do(struct usb_device *udev, uint8_t req_type, uint8_t request,
561     uint16_t value, uint16_t index, uint16_t length)
562 {
563 	struct usb_device_request req;
564 	usb_error_t err;
565 	uint8_t buf[4];
566 
567 	req.bmRequestType = req_type;
568 	req.bRequest = request;
569 	USETW(req.wValue, value);
570 	USETW(req.wIndex, index);
571 	USETW(req.wLength, length);
572 
573 	err = usbd_do_request(udev, NULL, &req, buf);
574 	if (err) {
575 		DPRINTF("error=%s\n", usbd_errstr(err));
576 		return (1);
577 	}
578 	return (0);
579 }
580 
581 static int
582 uplcom_pl2303_init(struct usb_device *udev, uint8_t chiptype)
583 {
584 	int err;
585 
586 	if (uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
587 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0)
588 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
589 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
590 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
591 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0)
592 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
593 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
594 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0)
595 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0))
596 		return (EIO);
597 
598 	if (chiptype != TYPE_PL2303)
599 		err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0);
600 	else
601 		err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x24, 0);
602 	if (err)
603 		return (EIO);
604 
605 	return (0);
606 }
607 
608 static void
609 uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
610 {
611 	struct uplcom_softc *sc = ucom->sc_parent;
612 	struct usb_device_request req;
613 
614 	DPRINTF("onoff = %d\n", onoff);
615 
616 	if (onoff)
617 		sc->sc_line |= UCDC_LINE_DTR;
618 	else
619 		sc->sc_line &= ~UCDC_LINE_DTR;
620 
621 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
622 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
623 	USETW(req.wValue, sc->sc_line);
624 	req.wIndex[0] = sc->sc_data_iface_no;
625 	req.wIndex[1] = 0;
626 	USETW(req.wLength, 0);
627 
628 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
629 	    &req, NULL, 0, 1000);
630 }
631 
632 static void
633 uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
634 {
635 	struct uplcom_softc *sc = ucom->sc_parent;
636 	struct usb_device_request req;
637 
638 	DPRINTF("onoff = %d\n", onoff);
639 
640 	if (onoff)
641 		sc->sc_line |= UCDC_LINE_RTS;
642 	else
643 		sc->sc_line &= ~UCDC_LINE_RTS;
644 
645 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
646 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
647 	USETW(req.wValue, sc->sc_line);
648 	req.wIndex[0] = sc->sc_data_iface_no;
649 	req.wIndex[1] = 0;
650 	USETW(req.wLength, 0);
651 
652 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
653 	    &req, NULL, 0, 1000);
654 }
655 
656 static void
657 uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
658 {
659 	struct uplcom_softc *sc = ucom->sc_parent;
660 	struct usb_device_request req;
661 	uint16_t temp;
662 
663 	DPRINTF("onoff = %d\n", onoff);
664 
665 	temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
666 
667 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
668 	req.bRequest = UCDC_SEND_BREAK;
669 	USETW(req.wValue, temp);
670 	req.wIndex[0] = sc->sc_data_iface_no;
671 	req.wIndex[1] = 0;
672 	USETW(req.wLength, 0);
673 
674 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
675 	    &req, NULL, 0, 1000);
676 }
677 
678 /*
679  * NOTE: These baud rates are officially supported, they can be written
680  * directly into dwDTERate register.
681  *
682  * Free baudrate setting is not supported by the base PL2303, and on
683  * other models it requires writing a divisor value to dwDTERate instead
684  * of the raw baudrate. The formula for divisor calculation is not published
685  * by the vendor, so it is speculative, though the official product homepage
686  * refers to the Linux module source as a reference implementation.
687  */
688 static const uint32_t uplcom_rates[] = {
689 	/*
690 	 * Basic 'standard' speed rates, supported by all models
691 	 * NOTE: 900 and 56000 actually works as well
692 	 */
693 	75, 150, 300, 600, 900, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
694 	19200, 28800, 38400, 56000, 57600, 115200,
695 	/*
696 	 * Advanced speed rates up to 6Mbs, supported by HX/TA and HXD/TB/EA/RA
697      * NOTE: regardless of the spec, 256000 does not work
698 	 */
699 	128000, 134400, 161280, 201600, 230400, 268800, 403200, 460800, 614400,
700 	806400, 921600, 1228800, 2457600, 3000000, 6000000,
701 	/*
702 	 * Advanced speed rates up to 12, supported by HXD/TB/EA/RA
703 	 */
704 	12000000
705 };
706 
707 #define	N_UPLCOM_RATES	nitems(uplcom_rates)
708 
709 static int
710 uplcom_baud_supported(unsigned int speed)
711 {
712 	int i;
713 	for (i = 0; i < N_UPLCOM_RATES; i++) {
714 		if (uplcom_rates[i] == speed)
715 			return 1;
716 	}
717 	return 0;
718 }
719 
720 static int
721 uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
722 {
723 	struct uplcom_softc *sc = ucom->sc_parent;
724 
725 	DPRINTF("\n");
726 
727 	/**
728 	 * Check requested baud rate.
729 	 *
730 	 * The PL2303 can only set specific baud rates, up to 1228800 baud.
731 	 * The PL2303HX can set any baud rate up to 6Mb.
732 	 * The PL2303HX rev. D can set any baud rate up to 12Mb.
733 	 *
734 	 */
735 
736 	/* accept raw divisor data, if someone wants to do the math in user domain */
737 	if (t->c_ospeed & 0x80000000)
738 		return 0;
739 	switch (sc->sc_chiptype) {
740 		case TYPE_PL2303HXD:
741 			if (t->c_ospeed <= 12000000)
742 				return (0);
743 			break;
744 		case TYPE_PL2303HX:
745 			if (t->c_ospeed <= 6000000)
746 				return (0);
747 			break;
748 		default:
749 			if (uplcom_baud_supported(t->c_ospeed))
750 				return (0);
751 			break;
752 	}
753 
754 	DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed);
755 	return (EIO);
756 }
757 
758 static unsigned int
759 uplcom_encode_baud_rate_divisor(uint8_t *buf, unsigned int baud)
760 {
761 	unsigned int baseline, mantissa, exponent;
762 
763 	/* Determine the baud rate divisor. This algorithm is taken from Linux. */
764 	/*
765 	 * Apparently the formula is:
766 	 *   baudrate = baseline / (mantissa * 4^exponent)
767 	 * where
768 	 *   mantissa = buf[8:0]
769 	 *   exponent = buf[11:9]
770 	 */
771 	if (baud == 0)
772 		baud = 1;
773 	baseline = 383385600;
774 	mantissa = baseline / baud;
775 	if (mantissa == 0)
776 		mantissa = 1;
777 	exponent = 0;
778 	while (mantissa >= 512) {
779 		if (exponent < 7) {
780 			mantissa >>= 2;	/* divide by 4 */
781 			exponent++;
782 		} else {
783 			/* Exponent is maxed. Trim mantissa and leave. This gives approx. 45.8 baud */
784 			mantissa = 511;
785 			break;
786 		}
787 	}
788 
789 	buf[3] = 0x80;
790 	buf[2] = 0;
791 	buf[1] = exponent << 1 | mantissa >> 8;
792 	buf[0] = mantissa & 0xff;
793 
794 	/* Calculate and return the exact baud rate. */
795 	baud = (baseline / mantissa) >> (exponent << 1);
796 	DPRINTF("real baud rate will be %u\n", baud);
797 
798 	return baud;
799 }
800 static void
801 uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
802 {
803 	struct uplcom_softc *sc = ucom->sc_parent;
804 	struct usb_cdc_line_state ls;
805 	struct usb_device_request req;
806 
807 	DPRINTF("sc = %p\n", sc);
808 
809 	memset(&ls, 0, sizeof(ls));
810 
811 	/*
812 	 * NOTE: If unsupported baud rates are set directly, the PL2303* uses 9600 baud.
813 	 */
814 	if ((t->c_ospeed & 0x80000000) || uplcom_baud_supported(t->c_ospeed))
815 		USETDW(ls.dwDTERate, t->c_ospeed);
816 	else
817 		t->c_ospeed = uplcom_encode_baud_rate_divisor((uint8_t*)&ls.dwDTERate, t->c_ospeed);
818 
819 	if (t->c_cflag & CSTOPB) {
820 		if ((t->c_cflag & CSIZE) == CS5) {
821 			/*
822 			 * NOTE: Comply with "real" UARTs / RS232:
823 			 *       use 1.5 instead of 2 stop bits with 5 data bits
824 			 */
825 			ls.bCharFormat = UCDC_STOP_BIT_1_5;
826 		} else {
827 			ls.bCharFormat = UCDC_STOP_BIT_2;
828 		}
829 	} else {
830 		ls.bCharFormat = UCDC_STOP_BIT_1;
831 	}
832 
833 	if (t->c_cflag & PARENB) {
834 		if (t->c_cflag & PARODD) {
835 			ls.bParityType = UCDC_PARITY_ODD;
836 		} else {
837 			ls.bParityType = UCDC_PARITY_EVEN;
838 		}
839 	} else {
840 		ls.bParityType = UCDC_PARITY_NONE;
841 	}
842 
843 	switch (t->c_cflag & CSIZE) {
844 	case CS5:
845 		ls.bDataBits = 5;
846 		break;
847 	case CS6:
848 		ls.bDataBits = 6;
849 		break;
850 	case CS7:
851 		ls.bDataBits = 7;
852 		break;
853 	case CS8:
854 		ls.bDataBits = 8;
855 		break;
856 	}
857 
858 	DPRINTF("rate=0x%08x fmt=%d parity=%d bits=%d\n",
859 	    UGETDW(ls.dwDTERate), ls.bCharFormat,
860 	    ls.bParityType, ls.bDataBits);
861 
862 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
863 	req.bRequest = UCDC_SET_LINE_CODING;
864 	USETW(req.wValue, 0);
865 	req.wIndex[0] = sc->sc_data_iface_no;
866 	req.wIndex[1] = 0;
867 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
868 
869 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
870 	    &req, &ls, 0, 1000);
871 
872 	if (t->c_cflag & CRTSCTS) {
873 		DPRINTF("crtscts = on\n");
874 
875 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
876 		req.bRequest = UPLCOM_SET_REQUEST;
877 		USETW(req.wValue, 0);
878 		if (sc->sc_chiptype != TYPE_PL2303)
879 			USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
880 		else
881 			USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
882 		USETW(req.wLength, 0);
883 
884 		ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
885 		    &req, NULL, 0, 1000);
886 	} else {
887 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
888 		req.bRequest = UPLCOM_SET_REQUEST;
889 		USETW(req.wValue, 0);
890 		USETW(req.wIndex, 0);
891 		USETW(req.wLength, 0);
892 		ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
893 		    &req, NULL, 0, 1000);
894 	}
895 }
896 
897 static void
898 uplcom_start_read(struct ucom_softc *ucom)
899 {
900 	struct uplcom_softc *sc = ucom->sc_parent;
901 
902 	/* start interrupt endpoint */
903 	usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
904 
905 	/* start read endpoint */
906 	usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
907 }
908 
909 static void
910 uplcom_stop_read(struct ucom_softc *ucom)
911 {
912 	struct uplcom_softc *sc = ucom->sc_parent;
913 
914 	/* stop interrupt endpoint */
915 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
916 
917 	/* stop read endpoint */
918 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
919 }
920 
921 static void
922 uplcom_start_write(struct ucom_softc *ucom)
923 {
924 	struct uplcom_softc *sc = ucom->sc_parent;
925 
926 	usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
927 }
928 
929 static void
930 uplcom_stop_write(struct ucom_softc *ucom)
931 {
932 	struct uplcom_softc *sc = ucom->sc_parent;
933 
934 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
935 }
936 
937 static void
938 uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
939 {
940 	struct uplcom_softc *sc = ucom->sc_parent;
941 
942 	DPRINTF("\n");
943 
944 	*lsr = sc->sc_lsr;
945 	*msr = sc->sc_msr;
946 }
947 
948 static void
949 uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
950 {
951 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
952 	struct usb_page_cache *pc;
953 	uint8_t buf[9];
954 	int actlen;
955 
956 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
957 
958 	switch (USB_GET_STATE(xfer)) {
959 	case USB_ST_TRANSFERRED:
960 
961 		DPRINTF("actlen = %u\n", actlen);
962 
963 		if (actlen >= 9) {
964 			pc = usbd_xfer_get_frame(xfer, 0);
965 			usbd_copy_out(pc, 0, buf, sizeof(buf));
966 
967 			DPRINTF("status = 0x%02x\n", buf[UPLCOM_STATE_INDEX]);
968 
969 			sc->sc_lsr = 0;
970 			sc->sc_msr = 0;
971 
972 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_CTS) {
973 				sc->sc_msr |= SER_CTS;
974 			}
975 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_OVERRUN_ERROR) {
976 				sc->sc_lsr |= ULSR_OE;
977 			}
978 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_PARITY_ERROR) {
979 				sc->sc_lsr |= ULSR_PE;
980 			}
981 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_FRAME_ERROR) {
982 				sc->sc_lsr |= ULSR_FE;
983 			}
984 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_RING) {
985 				sc->sc_msr |= SER_RI;
986 			}
987 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_BREAK_ERROR) {
988 				sc->sc_lsr |= ULSR_BI;
989 			}
990 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DSR) {
991 				sc->sc_msr |= SER_DSR;
992 			}
993 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DCD) {
994 				sc->sc_msr |= SER_DCD;
995 			}
996 			ucom_status_change(&sc->sc_ucom);
997 		}
998 	case USB_ST_SETUP:
999 tr_setup:
1000 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1001 		usbd_transfer_submit(xfer);
1002 		return;
1003 
1004 	default:			/* Error */
1005 		if (error != USB_ERR_CANCELLED) {
1006 			/* try to clear stall first */
1007 			usbd_xfer_set_stall(xfer);
1008 			goto tr_setup;
1009 		}
1010 		return;
1011 	}
1012 }
1013 
1014 static void
1015 uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
1016 {
1017 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1018 	struct usb_page_cache *pc;
1019 	uint32_t actlen;
1020 
1021 	switch (USB_GET_STATE(xfer)) {
1022 	case USB_ST_SETUP:
1023 	case USB_ST_TRANSFERRED:
1024 tr_setup:
1025 		pc = usbd_xfer_get_frame(xfer, 0);
1026 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
1027 		    UPLCOM_BULK_BUF_SIZE, &actlen)) {
1028 			DPRINTF("actlen = %d\n", actlen);
1029 
1030 			usbd_xfer_set_frame_len(xfer, 0, actlen);
1031 			usbd_transfer_submit(xfer);
1032 		}
1033 		return;
1034 
1035 	default:			/* Error */
1036 		if (error != USB_ERR_CANCELLED) {
1037 			/* try to clear stall first */
1038 			usbd_xfer_set_stall(xfer);
1039 			goto tr_setup;
1040 		}
1041 		return;
1042 	}
1043 }
1044 
1045 static void
1046 uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
1047 {
1048 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1049 	struct usb_page_cache *pc;
1050 	int actlen;
1051 
1052 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1053 
1054 	switch (USB_GET_STATE(xfer)) {
1055 	case USB_ST_TRANSFERRED:
1056 		pc = usbd_xfer_get_frame(xfer, 0);
1057 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
1058 
1059 	case USB_ST_SETUP:
1060 tr_setup:
1061 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1062 		usbd_transfer_submit(xfer);
1063 		return;
1064 
1065 	default:			/* Error */
1066 		if (error != USB_ERR_CANCELLED) {
1067 			/* try to clear stall first */
1068 			usbd_xfer_set_stall(xfer);
1069 			goto tr_setup;
1070 		}
1071 		return;
1072 	}
1073 }
1074 
1075 static void
1076 uplcom_poll(struct ucom_softc *ucom)
1077 {
1078 	struct uplcom_softc *sc = ucom->sc_parent;
1079 	usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);
1080 }
1081