xref: /freebsd/sys/dev/usb/serial/uplcom.c (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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_REQUEST_PL2303HXN	0x80
136 #define	UPLCOM_SET_CRTSCTS		0x41
137 #define	UPLCOM_SET_CRTSCTS_PL2303X	0x61
138 #define	UPLCOM_SET_CRTSCTS_PL2303HXN	0xFA
139 #define	UPLCOM_CLEAR_CRTSCTS_PL2303HXN	0xFF
140 #define	UPLCOM_CRTSCTS_REG_PL2303HXN	0x0A
141 #define	UPLCOM_STATUS_REG_PL2303HX	0x8080
142 #define	RSAQ_STATUS_CTS			0x80
143 #define	RSAQ_STATUS_OVERRUN_ERROR	0x40
144 #define	RSAQ_STATUS_PARITY_ERROR	0x20
145 #define	RSAQ_STATUS_FRAME_ERROR	0x10
146 #define	RSAQ_STATUS_RING		0x08
147 #define	RSAQ_STATUS_BREAK_ERROR	0x04
148 #define	RSAQ_STATUS_DSR			0x02
149 #define	RSAQ_STATUS_DCD			0x01
150 
151 #define	TYPE_PL2303			0
152 #define	TYPE_PL2303HX			1
153 #define	TYPE_PL2303HXD			2
154 #define	TYPE_PL2303HXN			3
155 
156 #define	UPLCOM_STATE_INDEX		8
157 
158 enum {
159 	UPLCOM_BULK_DT_WR,
160 	UPLCOM_BULK_DT_RD,
161 	UPLCOM_INTR_DT_RD,
162 	UPLCOM_N_TRANSFER,
163 };
164 
165 struct uplcom_softc {
166 	struct ucom_super_softc sc_super_ucom;
167 	struct ucom_softc sc_ucom;
168 
169 	struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER];
170 	struct usb_device *sc_udev;
171 	struct mtx sc_mtx;
172 
173 	uint16_t sc_line;
174 
175 	uint8_t	sc_lsr;			/* local status register */
176 	uint8_t	sc_msr;			/* uplcom status register */
177 	uint8_t	sc_chiptype;		/* type of chip */
178 	uint8_t	sc_ctrl_iface_no;
179 	uint8_t	sc_data_iface_no;
180 	uint8_t	sc_iface_index[2];
181 };
182 
183 /* prototypes */
184 
185 static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *);
186 static usb_error_t uplcom_pl2303_do(struct usb_device *, uint8_t, uint8_t,
187 			uint16_t, uint16_t, uint16_t);
188 static int	uplcom_pl2303_init(struct usb_device *, uint8_t);
189 static void	uplcom_free(struct ucom_softc *);
190 static void	uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
191 static void	uplcom_cfg_set_rts(struct ucom_softc *, uint8_t);
192 static void	uplcom_cfg_set_break(struct ucom_softc *, uint8_t);
193 static int	uplcom_pre_param(struct ucom_softc *, struct termios *);
194 static void	uplcom_cfg_param(struct ucom_softc *, struct termios *);
195 static void	uplcom_start_read(struct ucom_softc *);
196 static void	uplcom_stop_read(struct ucom_softc *);
197 static void	uplcom_start_write(struct ucom_softc *);
198 static void	uplcom_stop_write(struct ucom_softc *);
199 static void	uplcom_cfg_get_status(struct ucom_softc *, uint8_t *,
200 		    uint8_t *);
201 static void	uplcom_poll(struct ucom_softc *ucom);
202 
203 static device_probe_t uplcom_probe;
204 static device_attach_t uplcom_attach;
205 static device_detach_t uplcom_detach;
206 static void uplcom_free_softc(struct uplcom_softc *);
207 
208 static usb_callback_t uplcom_intr_callback;
209 static usb_callback_t uplcom_write_callback;
210 static usb_callback_t uplcom_read_callback;
211 
212 static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
213 	[UPLCOM_BULK_DT_WR] = {
214 		.type = UE_BULK,
215 		.endpoint = UE_ADDR_ANY,
216 		.direction = UE_DIR_OUT,
217 		.bufsize = UPLCOM_BULK_BUF_SIZE,
218 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
219 		.callback = &uplcom_write_callback,
220 		.if_index = 0,
221 	},
222 
223 	[UPLCOM_BULK_DT_RD] = {
224 		.type = UE_BULK,
225 		.endpoint = UE_ADDR_ANY,
226 		.direction = UE_DIR_IN,
227 		.bufsize = UPLCOM_BULK_BUF_SIZE,
228 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
229 		.callback = &uplcom_read_callback,
230 		.if_index = 0,
231 	},
232 
233 	[UPLCOM_INTR_DT_RD] = {
234 		.type = UE_INTERRUPT,
235 		.endpoint = UE_ADDR_ANY,
236 		.direction = UE_DIR_IN,
237 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
238 		.bufsize = 0,	/* use wMaxPacketSize */
239 		.callback = &uplcom_intr_callback,
240 		.if_index = 1,
241 	},
242 };
243 
244 static struct ucom_callback uplcom_callback = {
245 	.ucom_cfg_get_status = &uplcom_cfg_get_status,
246 	.ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
247 	.ucom_cfg_set_rts = &uplcom_cfg_set_rts,
248 	.ucom_cfg_set_break = &uplcom_cfg_set_break,
249 	.ucom_cfg_param = &uplcom_cfg_param,
250 	.ucom_pre_param = &uplcom_pre_param,
251 	.ucom_start_read = &uplcom_start_read,
252 	.ucom_stop_read = &uplcom_stop_read,
253 	.ucom_start_write = &uplcom_start_write,
254 	.ucom_stop_write = &uplcom_stop_write,
255 	.ucom_poll = &uplcom_poll,
256 	.ucom_free = &uplcom_free,
257 };
258 
259 #define	UPLCOM_DEV(v,p)				\
260   { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p) }
261 
262 static const STRUCT_USB_HOST_ID uplcom_devs[] = {
263 	UPLCOM_DEV(ACERP, S81),			/* BenQ S81 phone */
264 	UPLCOM_DEV(ADLINK, ND6530),		/* ADLINK ND-6530 USB-Serial */
265 	UPLCOM_DEV(ALCATEL, OT535),		/* Alcatel One Touch 535/735 */
266 	UPLCOM_DEV(ALCOR, AU9720),		/* Alcor AU9720 USB 2.0-RS232 */
267 	UPLCOM_DEV(ANCHOR, SERIAL),		/* Anchor Serial adapter */
268 	UPLCOM_DEV(ATEN, UC232A),		/* PLANEX USB-RS232 URS-03 */
269 	UPLCOM_DEV(ATEN, UC232B),		/* Prolific USB-RS232 Controller D */
270 	UPLCOM_DEV(BELKIN, F5U257),		/* Belkin F5U257 USB to Serial */
271 	UPLCOM_DEV(COREGA, CGUSBRS232R),	/* Corega CG-USBRS232R */
272 	UPLCOM_DEV(EPSON, CRESSI_EDY),		/* Cressi Edy diving computer */
273 	UPLCOM_DEV(EPSON, N2ITION3),		/* Zeagle N2iTion3 diving computer */
274 	UPLCOM_DEV(ELECOM, UCSGT),		/* ELECOM UC-SGT Serial Adapter */
275 	UPLCOM_DEV(ELECOM, UCSGT0),		/* ELECOM UC-SGT Serial Adapter */
276 	UPLCOM_DEV(HAL, IMR001),		/* HAL Corporation Crossam2+USB */
277 	UPLCOM_DEV(HP, LD220),			/* HP LD220 POS Display */
278 	UPLCOM_DEV(IODATA, USBRSAQ),		/* I/O DATA USB-RSAQ */
279 	UPLCOM_DEV(IODATA, USBRSAQ5),		/* I/O DATA USB-RSAQ5 */
280 	UPLCOM_DEV(ITEGNO, WM1080A),		/* iTegno WM1080A GSM/GFPRS modem */
281 	UPLCOM_DEV(ITEGNO, WM2080A),		/* iTegno WM2080A CDMA modem */
282 	UPLCOM_DEV(LEADTEK, 9531),		/* Leadtek 9531 GPS */
283 	UPLCOM_DEV(MICROSOFT, 700WX),		/* Microsoft Palm 700WX */
284 	UPLCOM_DEV(MOBILEACTION, MA620),	/* Mobile Action MA-620 Infrared Adapter */
285 	UPLCOM_DEV(NETINDEX, WS002IN),		/* Willcom W-S002IN */
286 	UPLCOM_DEV(NOKIA2, CA42),		/* Nokia CA-42 cable */
287 	UPLCOM_DEV(OTI, DKU5),			/* OTI DKU-5 cable */
288 	UPLCOM_DEV(PANASONIC, TYTP50P6S),	/* Panasonic TY-TP50P6-S flat screen */
289 	UPLCOM_DEV(PLX, CA42),			/* PLX CA-42 clone cable */
290 	UPLCOM_DEV(PROLIFIC, ALLTRONIX_GPRS),	/* Alltronix ACM003U00 modem */
291 	UPLCOM_DEV(PROLIFIC, ALDIGA_AL11U),	/* AlDiga AL-11U modem */
292 	UPLCOM_DEV(PROLIFIC, DCU11),		/* DCU-11 Phone Cable */
293 	UPLCOM_DEV(PROLIFIC, HCR331),		/* HCR331 Card Reader */
294 	UPLCOM_DEV(PROLIFIC, MICROMAX_610U),	/* Micromax 610U modem */
295 	UPLCOM_DEV(PROLIFIC, MOTOROLA),		/* Motorola cable */
296 	UPLCOM_DEV(PROLIFIC, PHAROS),		/* Prolific Pharos */
297 	UPLCOM_DEV(PROLIFIC, PL2303),		/* Generic adapter */
298 	UPLCOM_DEV(PROLIFIC, PL2303GC),		/* Generic adapter (PL2303HXN, type GC) */
299 	UPLCOM_DEV(PROLIFIC, PL2303GB),		/* Generic adapter (PL2303HXN, type GB) */
300 	UPLCOM_DEV(PROLIFIC, PL2303GT),		/* Generic adapter (PL2303HXN, type GT) */
301 	UPLCOM_DEV(PROLIFIC, PL2303GL),		/* Generic adapter (PL2303HXN, type GL) */
302 	UPLCOM_DEV(PROLIFIC, PL2303GE),		/* Generic adapter (PL2303HXN, type GE) */
303 	UPLCOM_DEV(PROLIFIC, PL2303GS),		/* Generic adapter (PL2303HXN, type GS) */
304 	UPLCOM_DEV(PROLIFIC, RSAQ2),		/* I/O DATA USB-RSAQ2 */
305 	UPLCOM_DEV(PROLIFIC, RSAQ3),		/* I/O DATA USB-RSAQ3 */
306 	UPLCOM_DEV(PROLIFIC, UIC_MSR206),	/* UIC MSR206 Card Reader */
307 	UPLCOM_DEV(PROLIFIC2, PL2303),		/* Prolific adapter */
308 	UPLCOM_DEV(RADIOSHACK, USBCABLE),	/* Radio Shack USB Adapter */
309 	UPLCOM_DEV(RATOC, REXUSB60),		/* RATOC REX-USB60 */
310 	UPLCOM_DEV(SAGEM, USBSERIAL),		/* Sagem USB-Serial Controller */
311 	UPLCOM_DEV(SAMSUNG, I330),		/* Samsung I330 phone cradle */
312 	UPLCOM_DEV(SANWA, KB_USB2),		/* Sanwa KB-USB2 Multimeter cable */
313 	UPLCOM_DEV(SIEMENS3, EF81),		/* Siemens EF81 */
314 	UPLCOM_DEV(SIEMENS3, SX1),		/* Siemens SX1 */
315 	UPLCOM_DEV(SIEMENS3, X65),		/* Siemens X65 */
316 	UPLCOM_DEV(SIEMENS3, X75),		/* Siemens X75 */
317 	UPLCOM_DEV(SITECOM, SERIAL),		/* Sitecom USB to Serial */
318 	UPLCOM_DEV(SMART, PL2303),		/* SMART Technologies USB to Serial */
319 	UPLCOM_DEV(SONY, QN3),			/* Sony QN3 phone cable */
320 	UPLCOM_DEV(SONYERICSSON, DATAPILOT),	/* Sony Ericsson Datapilot */
321 	UPLCOM_DEV(SONYERICSSON, DCU10),	/* Sony Ericsson DCU-10 Cable */
322 	UPLCOM_DEV(SOURCENEXT, KEIKAI8),	/* SOURCENEXT KeikaiDenwa 8 */
323 	UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG),	/* SOURCENEXT KeikaiDenwa 8 with charger */
324 	UPLCOM_DEV(SPEEDDRAGON, MS3303H),	/* Speed Dragon USB-Serial */
325 	UPLCOM_DEV(SYNTECH, CPT8001C),		/* Syntech CPT-8001C Barcode scanner */
326 	UPLCOM_DEV(TDK, UHA6400),		/* TDK USB-PHS Adapter UHA6400 */
327 	UPLCOM_DEV(TDK, UPA9664),		/* TDK USB-PHS Adapter UPA9664 */
328 	UPLCOM_DEV(TRIPPLITE, U209),		/* Tripp-Lite U209-000-R USB to Serial */
329 	UPLCOM_DEV(YCCABLE, PL2303),		/* YC Cable USB-Serial */
330 };
331 #undef UPLCOM_DEV
332 
333 static device_method_t uplcom_methods[] = {
334 	DEVMETHOD(device_probe, uplcom_probe),
335 	DEVMETHOD(device_attach, uplcom_attach),
336 	DEVMETHOD(device_detach, uplcom_detach),
337 	DEVMETHOD_END
338 };
339 
340 static devclass_t uplcom_devclass;
341 
342 static driver_t uplcom_driver = {
343 	.name = "uplcom",
344 	.methods = uplcom_methods,
345 	.size = sizeof(struct uplcom_softc),
346 };
347 
348 DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0);
349 MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
350 MODULE_DEPEND(uplcom, usb, 1, 1, 1);
351 MODULE_VERSION(uplcom, UPLCOM_MODVER);
352 USB_PNP_HOST_INFO(uplcom_devs);
353 
354 static int
355 uplcom_probe(device_t dev)
356 {
357 	struct usb_attach_arg *uaa = device_get_ivars(dev);
358 
359 	DPRINTFN(11, "\n");
360 
361 	if (uaa->usb_mode != USB_MODE_HOST) {
362 		return (ENXIO);
363 	}
364 	if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
365 		return (ENXIO);
366 	}
367 	if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
368 		return (ENXIO);
369 	}
370 	return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
371 }
372 
373 static int
374 uplcom_attach(device_t dev)
375 {
376 	struct usb_attach_arg *uaa = device_get_ivars(dev);
377 	struct uplcom_softc *sc = device_get_softc(dev);
378 	struct usb_interface *iface;
379 	struct usb_interface_descriptor *id;
380 	struct usb_device_descriptor *dd;
381 	int error;
382 
383 	struct usb_device_request req;
384 	usb_error_t err;
385 	uint8_t buf[4];
386 
387 	DPRINTFN(11, "\n");
388 
389 	device_set_usb_desc(dev);
390 	mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
391 	ucom_ref(&sc->sc_super_ucom);
392 
393 	DPRINTF("sc = %p\n", sc);
394 
395 	sc->sc_udev = uaa->device;
396 
397 	dd = usbd_get_device_descriptor(sc->sc_udev);
398 
399 	switch (UGETW(dd->bcdDevice)) {
400 	case 0x0300:
401 		sc->sc_chiptype = TYPE_PL2303HX;
402 		/* or TA, that is HX with external crystal */
403 		break;
404 	case 0x0400:
405 		sc->sc_chiptype = TYPE_PL2303HXD;
406 		/* or EA, that is HXD with ESD protection */
407 		/* or RA, that has internal voltage level converter that works only up to 1Mbaud (!) */
408 		break;
409 	case 0x0500:
410 		sc->sc_chiptype = TYPE_PL2303HXD;
411 		/* in fact it's TB, that is HXD with external crystal */
412 		break;
413 	default:
414 		/* NOTE: I have no info about the bcdDevice for the base PL2303 (up to 1.2Mbaud,
415 		   only fixed rates) and for PL2303SA (8-pin chip, up to 115200 baud */
416 		/* Determine the chip type.  This algorithm is taken from Linux. */
417 		if (dd->bDeviceClass == 0x02)
418 			sc->sc_chiptype = TYPE_PL2303;
419 		else if (dd->bMaxPacketSize == 0x40)
420 			sc->sc_chiptype = TYPE_PL2303HX;
421 		else
422 			sc->sc_chiptype = TYPE_PL2303;
423 		break;
424 	}
425 
426 	/*
427 	 * The new chip revision PL2303HXN is only compatible with the new
428 	 * UPLCOM_SET_REQUEST_PL2303HXN command. Issuing the old command
429 	 * UPLCOM_SET_REQUEST to the new chip raises an error. Thus, PL2303HX
430 	 * and PL2303HXN can be distinguished by issuing an old-style request
431 	 * (on a status register) to the new chip and checking the error.
432 	 */
433 	if (sc->sc_chiptype == TYPE_PL2303HX) {
434 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
435 		req.bRequest = UPLCOM_SET_REQUEST;
436 		USETW(req.wValue, UPLCOM_STATUS_REG_PL2303HX);
437 		req.wIndex[0] = sc->sc_data_iface_no;
438 		req.wIndex[1] = 0;
439 		USETW(req.wLength, 1);
440 		err = usbd_do_request(sc->sc_udev, NULL, &req, buf);
441 		if (err)
442 			sc->sc_chiptype = TYPE_PL2303HXN;
443 	}
444 
445 	switch (sc->sc_chiptype) {
446 	case TYPE_PL2303:
447 		DPRINTF("chiptype: 2303\n");
448 		break;
449 	case TYPE_PL2303HX:
450 		DPRINTF("chiptype: 2303HX/TA\n");
451 		break;
452 	case TYPE_PL2303HXN:
453 		DPRINTF("chiptype: 2303HXN\n");
454 		break;
455 	case TYPE_PL2303HXD:
456 		DPRINTF("chiptype: 2303HXD/TB/RA/EA\n");
457 		break;
458 	default:
459 		DPRINTF("chiptype: unknown %d\n", sc->sc_chiptype);
460 		break;
461 	}
462 
463 	/*
464 	 * USB-RSAQ1 has two interface
465 	 *
466 	 *  USB-RSAQ1       | USB-RSAQ2
467 	 * -----------------+-----------------
468 	 * Interface 0      |Interface 0
469 	 *  Interrupt(0x81) | Interrupt(0x81)
470 	 * -----------------+ BulkIN(0x02)
471 	 * Interface 1	    | BulkOUT(0x83)
472 	 *   BulkIN(0x02)   |
473 	 *   BulkOUT(0x83)  |
474 	 */
475 
476 	sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
477 	sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
478 
479 	iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
480 	if (iface) {
481 		id = usbd_get_interface_descriptor(iface);
482 		if (id == NULL) {
483 			device_printf(dev, "no interface descriptor (2)\n");
484 			goto detach;
485 		}
486 		sc->sc_data_iface_no = id->bInterfaceNumber;
487 		sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
488 		usbd_set_parent_iface(uaa->device,
489 		    UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
490 	} else {
491 		sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
492 		sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
493 	}
494 
495 	error = usbd_transfer_setup(uaa->device,
496 	    sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
497 	    UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
498 	if (error) {
499 		DPRINTF("one or more missing USB endpoints, "
500 		    "error=%s\n", usbd_errstr(error));
501 		goto detach;
502 	}
503 	error = uplcom_reset(sc, uaa->device);
504 	if (error) {
505 		device_printf(dev, "reset failed, error=%s\n",
506 		    usbd_errstr(error));
507 		goto detach;
508 	}
509 
510 	if (sc->sc_chiptype == TYPE_PL2303) {
511 		/* HX variants seem to lock up after a clear stall request. */
512 		mtx_lock(&sc->sc_mtx);
513 		usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
514 		usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
515 		mtx_unlock(&sc->sc_mtx);
516 	} else if (sc->sc_chiptype == TYPE_PL2303HX ||
517 		   sc->sc_chiptype == TYPE_PL2303HXD) {
518 		/* reset upstream data pipes */
519 		if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
520 		    UPLCOM_SET_REQUEST, 8, 0, 0) ||
521 		    uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
522 		    UPLCOM_SET_REQUEST, 9, 0, 0)) {
523 			goto detach;
524 		}
525 	} else if (sc->sc_chiptype == TYPE_PL2303HXN) {
526 		/* reset upstream data pipes */
527 		if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
528 		    UPLCOM_SET_REQUEST_PL2303HXN, 0x07, 0x03, 0)) {
529 			goto detach;
530 		}
531 	}
532 
533 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
534 	    &uplcom_callback, &sc->sc_mtx);
535 	if (error) {
536 		goto detach;
537 	}
538 	/*
539 	 * do the initialization during attach so that the system does not
540 	 * sleep during open:
541 	 */
542 	if (uplcom_pl2303_init(uaa->device, sc->sc_chiptype)) {
543 		device_printf(dev, "init failed\n");
544 		goto detach;
545 	}
546 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
547 
548 	return (0);
549 
550 detach:
551 	uplcom_detach(dev);
552 	return (ENXIO);
553 }
554 
555 static int
556 uplcom_detach(device_t dev)
557 {
558 	struct uplcom_softc *sc = device_get_softc(dev);
559 
560 	DPRINTF("sc=%p\n", sc);
561 
562 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
563 	usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
564 
565 	device_claim_softc(dev);
566 
567 	uplcom_free_softc(sc);
568 
569 	return (0);
570 }
571 
572 UCOM_UNLOAD_DRAIN(uplcom);
573 
574 static void
575 uplcom_free_softc(struct uplcom_softc *sc)
576 {
577 	if (ucom_unref(&sc->sc_super_ucom)) {
578 		mtx_destroy(&sc->sc_mtx);
579 		device_free_softc(sc);
580 	}
581 }
582 
583 static void
584 uplcom_free(struct ucom_softc *ucom)
585 {
586 	uplcom_free_softc(ucom->sc_parent);
587 }
588 
589 static usb_error_t
590 uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
591 {
592 	struct usb_device_request req;
593 
594 	if (sc->sc_chiptype == TYPE_PL2303HXN) {
595 		/* PL2303HXN doesn't need this reset sequence */
596 		return (0);
597 	}
598 
599 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
600 	req.bRequest = UPLCOM_SET_REQUEST;
601 	USETW(req.wValue, 0);
602 	req.wIndex[0] = sc->sc_data_iface_no;
603 	req.wIndex[1] = 0;
604 	USETW(req.wLength, 0);
605 
606 	return (usbd_do_request(udev, NULL, &req, NULL));
607 }
608 
609 static usb_error_t
610 uplcom_pl2303_do(struct usb_device *udev, uint8_t req_type, uint8_t request,
611     uint16_t value, uint16_t index, uint16_t length)
612 {
613 	struct usb_device_request req;
614 	usb_error_t err;
615 	uint8_t buf[4];
616 
617 	req.bmRequestType = req_type;
618 	req.bRequest = request;
619 	USETW(req.wValue, value);
620 	USETW(req.wIndex, index);
621 	USETW(req.wLength, length);
622 
623 	err = usbd_do_request(udev, NULL, &req, buf);
624 	if (err) {
625 		DPRINTF("error=%s\n", usbd_errstr(err));
626 		return (1);
627 	}
628 	return (0);
629 }
630 
631 static int
632 uplcom_pl2303_init(struct usb_device *udev, uint8_t chiptype)
633 {
634 	int err;
635 
636 	if (chiptype == TYPE_PL2303HXN) {
637 		/* PL2303HXN doesn't need this initialization sequence */
638 		return (0);
639 	}
640 
641 	if (uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
642 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0)
643 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
644 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
645 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
646 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0)
647 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
648 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
649 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0)
650 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0))
651 		return (EIO);
652 
653 	if (chiptype != TYPE_PL2303)
654 		err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0);
655 	else
656 		err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x24, 0);
657 	if (err)
658 		return (EIO);
659 
660 	return (0);
661 }
662 
663 static void
664 uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
665 {
666 	struct uplcom_softc *sc = ucom->sc_parent;
667 	struct usb_device_request req;
668 
669 	DPRINTF("onoff = %d\n", onoff);
670 
671 	if (onoff)
672 		sc->sc_line |= UCDC_LINE_DTR;
673 	else
674 		sc->sc_line &= ~UCDC_LINE_DTR;
675 
676 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
677 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
678 	USETW(req.wValue, sc->sc_line);
679 	req.wIndex[0] = sc->sc_data_iface_no;
680 	req.wIndex[1] = 0;
681 	USETW(req.wLength, 0);
682 
683 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
684 	    &req, NULL, 0, 1000);
685 }
686 
687 static void
688 uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
689 {
690 	struct uplcom_softc *sc = ucom->sc_parent;
691 	struct usb_device_request req;
692 
693 	DPRINTF("onoff = %d\n", onoff);
694 
695 	if (onoff)
696 		sc->sc_line |= UCDC_LINE_RTS;
697 	else
698 		sc->sc_line &= ~UCDC_LINE_RTS;
699 
700 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
701 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
702 	USETW(req.wValue, sc->sc_line);
703 	req.wIndex[0] = sc->sc_data_iface_no;
704 	req.wIndex[1] = 0;
705 	USETW(req.wLength, 0);
706 
707 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
708 	    &req, NULL, 0, 1000);
709 }
710 
711 static void
712 uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
713 {
714 	struct uplcom_softc *sc = ucom->sc_parent;
715 	struct usb_device_request req;
716 	uint16_t temp;
717 
718 	DPRINTF("onoff = %d\n", onoff);
719 
720 	temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
721 
722 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
723 	req.bRequest = UCDC_SEND_BREAK;
724 	USETW(req.wValue, temp);
725 	req.wIndex[0] = sc->sc_data_iface_no;
726 	req.wIndex[1] = 0;
727 	USETW(req.wLength, 0);
728 
729 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
730 	    &req, NULL, 0, 1000);
731 }
732 
733 /*
734  * NOTE: These baud rates are officially supported, they can be written
735  * directly into dwDTERate register.
736  *
737  * Free baudrate setting is not supported by the base PL2303, and on
738  * other models it requires writing a divisor value to dwDTERate instead
739  * of the raw baudrate. The formula for divisor calculation is not published
740  * by the vendor, so it is speculative, though the official product homepage
741  * refers to the Linux module source as a reference implementation.
742  */
743 static const uint32_t uplcom_rates[] = {
744 	/*
745 	 * Basic 'standard' speed rates, supported by all models
746 	 * NOTE: 900 and 56000 actually works as well
747 	 */
748 	75, 150, 300, 600, 900, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
749 	19200, 28800, 38400, 56000, 57600, 115200,
750 	/*
751 	 * Advanced speed rates up to 6Mbs, supported by HX/TA and HXD/TB/EA/RA
752      * NOTE: regardless of the spec, 256000 does not work
753 	 */
754 	128000, 134400, 161280, 201600, 230400, 268800, 403200, 460800, 614400,
755 	806400, 921600, 1228800, 2457600, 3000000, 6000000,
756 	/*
757 	 * Advanced speed rates up to 12, supported by HXD/TB/EA/RA
758 	 */
759 	12000000
760 };
761 
762 #define	N_UPLCOM_RATES	nitems(uplcom_rates)
763 
764 static int
765 uplcom_baud_supported(unsigned int speed)
766 {
767 	int i;
768 	for (i = 0; i < N_UPLCOM_RATES; i++) {
769 		if (uplcom_rates[i] == speed)
770 			return 1;
771 	}
772 	return 0;
773 }
774 
775 static int
776 uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
777 {
778 	struct uplcom_softc *sc = ucom->sc_parent;
779 
780 	DPRINTF("\n");
781 
782 	/**
783 	 * Check requested baud rate.
784 	 *
785 	 * The PL2303 can only set specific baud rates, up to 1228800 baud.
786 	 * The PL2303HX can set any baud rate up to 6Mb.
787 	 * The PL2303HX rev. D and PL2303HXN can set any baud rate up to 12Mb.
788 	 *
789 	 */
790 
791 	/* accept raw divisor data, if someone wants to do the math in user domain */
792 	if (t->c_ospeed & 0x80000000)
793 		return 0;
794 	switch (sc->sc_chiptype) {
795 		case TYPE_PL2303HXN:
796 			if (t->c_ospeed <= 12000000)
797 				return (0);
798 			break;
799 		case TYPE_PL2303HXD:
800 			if (t->c_ospeed <= 12000000)
801 				return (0);
802 			break;
803 		case TYPE_PL2303HX:
804 			if (t->c_ospeed <= 6000000)
805 				return (0);
806 			break;
807 		default:
808 			if (uplcom_baud_supported(t->c_ospeed))
809 				return (0);
810 			break;
811 	}
812 
813 	DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed);
814 	return (EIO);
815 }
816 
817 static unsigned int
818 uplcom_encode_baud_rate_divisor(uint8_t *buf, unsigned int baud)
819 {
820 	unsigned int baseline, mantissa, exponent;
821 
822 	/* Determine the baud rate divisor. This algorithm is taken from Linux. */
823 	/*
824 	 * Apparently the formula is:
825 	 *   baudrate = baseline / (mantissa * 4^exponent)
826 	 * where
827 	 *   mantissa = buf[8:0]
828 	 *   exponent = buf[11:9]
829 	 */
830 	if (baud == 0)
831 		baud = 1;
832 	baseline = 383385600;
833 	mantissa = baseline / baud;
834 	if (mantissa == 0)
835 		mantissa = 1;
836 	exponent = 0;
837 	while (mantissa >= 512) {
838 		if (exponent < 7) {
839 			mantissa >>= 2;	/* divide by 4 */
840 			exponent++;
841 		} else {
842 			/* Exponent is maxed. Trim mantissa and leave. This gives approx. 45.8 baud */
843 			mantissa = 511;
844 			break;
845 		}
846 	}
847 
848 	buf[3] = 0x80;
849 	buf[2] = 0;
850 	buf[1] = exponent << 1 | mantissa >> 8;
851 	buf[0] = mantissa & 0xff;
852 
853 	/* Calculate and return the exact baud rate. */
854 	baud = (baseline / mantissa) >> (exponent << 1);
855 	DPRINTF("real baud rate will be %u\n", baud);
856 
857 	return baud;
858 }
859 static void
860 uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
861 {
862 	struct uplcom_softc *sc = ucom->sc_parent;
863 	struct usb_cdc_line_state ls;
864 	struct usb_device_request req;
865 
866 	DPRINTF("sc = %p\n", sc);
867 
868 	memset(&ls, 0, sizeof(ls));
869 
870 	/*
871 	 * NOTE: If unsupported baud rates are set directly, the PL2303* uses 9600 baud.
872 	 */
873 	if ((t->c_ospeed & 0x80000000) || uplcom_baud_supported(t->c_ospeed))
874 		USETDW(ls.dwDTERate, t->c_ospeed);
875 	else
876 		t->c_ospeed = uplcom_encode_baud_rate_divisor((uint8_t*)&ls.dwDTERate, t->c_ospeed);
877 
878 	if (t->c_cflag & CSTOPB) {
879 		if ((t->c_cflag & CSIZE) == CS5) {
880 			/*
881 			 * NOTE: Comply with "real" UARTs / RS232:
882 			 *       use 1.5 instead of 2 stop bits with 5 data bits
883 			 */
884 			ls.bCharFormat = UCDC_STOP_BIT_1_5;
885 		} else {
886 			ls.bCharFormat = UCDC_STOP_BIT_2;
887 		}
888 	} else {
889 		ls.bCharFormat = UCDC_STOP_BIT_1;
890 	}
891 
892 	if (t->c_cflag & PARENB) {
893 		if (t->c_cflag & PARODD) {
894 			ls.bParityType = UCDC_PARITY_ODD;
895 		} else {
896 			ls.bParityType = UCDC_PARITY_EVEN;
897 		}
898 	} else {
899 		ls.bParityType = UCDC_PARITY_NONE;
900 	}
901 
902 	switch (t->c_cflag & CSIZE) {
903 	case CS5:
904 		ls.bDataBits = 5;
905 		break;
906 	case CS6:
907 		ls.bDataBits = 6;
908 		break;
909 	case CS7:
910 		ls.bDataBits = 7;
911 		break;
912 	case CS8:
913 		ls.bDataBits = 8;
914 		break;
915 	}
916 
917 	DPRINTF("rate=0x%08x fmt=%d parity=%d bits=%d\n",
918 	    UGETDW(ls.dwDTERate), ls.bCharFormat,
919 	    ls.bParityType, ls.bDataBits);
920 
921 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
922 	req.bRequest = UCDC_SET_LINE_CODING;
923 	USETW(req.wValue, 0);
924 	req.wIndex[0] = sc->sc_data_iface_no;
925 	req.wIndex[1] = 0;
926 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
927 
928 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
929 	    &req, &ls, 0, 1000);
930 
931 	if (t->c_cflag & CRTSCTS) {
932 		DPRINTF("crtscts = on\n");
933 
934 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
935 		if (sc->sc_chiptype == TYPE_PL2303HXN) {
936 			req.bRequest = UPLCOM_SET_REQUEST_PL2303HXN;
937 			USETW(req.wValue, UPLCOM_CRTSCTS_REG_PL2303HXN);
938 			USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303HXN);
939 		} else {
940 			req.bRequest = UPLCOM_SET_REQUEST;
941 			USETW(req.wValue, 0);
942 			if (sc->sc_chiptype != TYPE_PL2303)
943 				USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
944 			else
945 				USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
946 		}
947 		USETW(req.wLength, 0);
948 
949 		ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
950 		    &req, NULL, 0, 1000);
951 	} else {
952 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
953 		if (sc->sc_chiptype == TYPE_PL2303HXN) {
954 			req.bRequest = UPLCOM_SET_REQUEST_PL2303HXN;
955 			USETW(req.wValue, UPLCOM_CRTSCTS_REG_PL2303HXN);
956 			USETW(req.wIndex, UPLCOM_CLEAR_CRTSCTS_PL2303HXN);
957 		}
958 		else {
959 			req.bRequest = UPLCOM_SET_REQUEST;
960 			USETW(req.wValue, 0);
961 			USETW(req.wIndex, 0);
962 		}
963 		USETW(req.wLength, 0);
964 		ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
965 		    &req, NULL, 0, 1000);
966 	}
967 }
968 
969 static void
970 uplcom_start_read(struct ucom_softc *ucom)
971 {
972 	struct uplcom_softc *sc = ucom->sc_parent;
973 
974 	/* start interrupt endpoint */
975 	usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
976 
977 	/* start read endpoint */
978 	usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
979 }
980 
981 static void
982 uplcom_stop_read(struct ucom_softc *ucom)
983 {
984 	struct uplcom_softc *sc = ucom->sc_parent;
985 
986 	/* stop interrupt endpoint */
987 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
988 
989 	/* stop read endpoint */
990 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
991 }
992 
993 static void
994 uplcom_start_write(struct ucom_softc *ucom)
995 {
996 	struct uplcom_softc *sc = ucom->sc_parent;
997 
998 	usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
999 }
1000 
1001 static void
1002 uplcom_stop_write(struct ucom_softc *ucom)
1003 {
1004 	struct uplcom_softc *sc = ucom->sc_parent;
1005 
1006 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
1007 }
1008 
1009 static void
1010 uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
1011 {
1012 	struct uplcom_softc *sc = ucom->sc_parent;
1013 
1014 	DPRINTF("\n");
1015 
1016 	*lsr = sc->sc_lsr;
1017 	*msr = sc->sc_msr;
1018 }
1019 
1020 static void
1021 uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1022 {
1023 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1024 	struct usb_page_cache *pc;
1025 	uint8_t buf[9];
1026 	int actlen;
1027 
1028 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1029 
1030 	switch (USB_GET_STATE(xfer)) {
1031 	case USB_ST_TRANSFERRED:
1032 
1033 		DPRINTF("actlen = %u\n", actlen);
1034 
1035 		if (actlen >= 9) {
1036 			pc = usbd_xfer_get_frame(xfer, 0);
1037 			usbd_copy_out(pc, 0, buf, sizeof(buf));
1038 
1039 			DPRINTF("status = 0x%02x\n", buf[UPLCOM_STATE_INDEX]);
1040 
1041 			sc->sc_lsr = 0;
1042 			sc->sc_msr = 0;
1043 
1044 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_CTS) {
1045 				sc->sc_msr |= SER_CTS;
1046 			}
1047 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_OVERRUN_ERROR) {
1048 				sc->sc_lsr |= ULSR_OE;
1049 			}
1050 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_PARITY_ERROR) {
1051 				sc->sc_lsr |= ULSR_PE;
1052 			}
1053 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_FRAME_ERROR) {
1054 				sc->sc_lsr |= ULSR_FE;
1055 			}
1056 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_RING) {
1057 				sc->sc_msr |= SER_RI;
1058 			}
1059 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_BREAK_ERROR) {
1060 				sc->sc_lsr |= ULSR_BI;
1061 			}
1062 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DSR) {
1063 				sc->sc_msr |= SER_DSR;
1064 			}
1065 			if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DCD) {
1066 				sc->sc_msr |= SER_DCD;
1067 			}
1068 			ucom_status_change(&sc->sc_ucom);
1069 		}
1070 	case USB_ST_SETUP:
1071 tr_setup:
1072 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1073 		usbd_transfer_submit(xfer);
1074 		return;
1075 
1076 	default:			/* Error */
1077 		if (error != USB_ERR_CANCELLED) {
1078 			/* try to clear stall first */
1079 			usbd_xfer_set_stall(xfer);
1080 			goto tr_setup;
1081 		}
1082 		return;
1083 	}
1084 }
1085 
1086 static void
1087 uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
1088 {
1089 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1090 	struct usb_page_cache *pc;
1091 	uint32_t actlen;
1092 
1093 	switch (USB_GET_STATE(xfer)) {
1094 	case USB_ST_SETUP:
1095 	case USB_ST_TRANSFERRED:
1096 tr_setup:
1097 		pc = usbd_xfer_get_frame(xfer, 0);
1098 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
1099 		    UPLCOM_BULK_BUF_SIZE, &actlen)) {
1100 			DPRINTF("actlen = %d\n", actlen);
1101 
1102 			usbd_xfer_set_frame_len(xfer, 0, actlen);
1103 			usbd_transfer_submit(xfer);
1104 		}
1105 		return;
1106 
1107 	default:			/* Error */
1108 		if (error != USB_ERR_CANCELLED) {
1109 			/* try to clear stall first */
1110 			usbd_xfer_set_stall(xfer);
1111 			goto tr_setup;
1112 		}
1113 		return;
1114 	}
1115 }
1116 
1117 static void
1118 uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
1119 {
1120 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1121 	struct usb_page_cache *pc;
1122 	int actlen;
1123 
1124 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1125 
1126 	switch (USB_GET_STATE(xfer)) {
1127 	case USB_ST_TRANSFERRED:
1128 		pc = usbd_xfer_get_frame(xfer, 0);
1129 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
1130 
1131 	case USB_ST_SETUP:
1132 tr_setup:
1133 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1134 		usbd_transfer_submit(xfer);
1135 		return;
1136 
1137 	default:			/* Error */
1138 		if (error != USB_ERR_CANCELLED) {
1139 			/* try to clear stall first */
1140 			usbd_xfer_set_stall(xfer);
1141 			goto tr_setup;
1142 		}
1143 		return;
1144 	}
1145 }
1146 
1147 static void
1148 uplcom_poll(struct ucom_softc *ucom)
1149 {
1150 	struct uplcom_softc *sc = ucom->sc_parent;
1151 	usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);
1152 }
1153