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