xref: /freebsd/sys/dev/usb/serial/uplcom.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
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  * 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, 0, "USB uplcom");
115 SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RWTUN,
116     &uplcom_debug, 0, "Debug level");
117 #endif
118 
119 #define	UPLCOM_MODVER			1	/* module version */
120 
121 #define	UPLCOM_CONFIG_INDEX		0
122 #define	UPLCOM_IFACE_INDEX		0
123 #define	UPLCOM_SECOND_IFACE_INDEX	1
124 
125 #ifndef UPLCOM_INTR_INTERVAL
126 #define	UPLCOM_INTR_INTERVAL		0	/* default */
127 #endif
128 
129 #define	UPLCOM_BULK_BUF_SIZE 1024	/* bytes */
130 
131 #define	UPLCOM_SET_REQUEST		0x01
132 #define	UPLCOM_SET_CRTSCTS		0x41
133 #define	UPLCOM_SET_CRTSCTS_PL2303X	0x61
134 #define	RSAQ_STATUS_CTS			0x80
135 #define	RSAQ_STATUS_DSR			0x02
136 #define	RSAQ_STATUS_DCD			0x01
137 
138 #define	TYPE_PL2303			0
139 #define	TYPE_PL2303HX			1
140 
141 enum {
142 	UPLCOM_BULK_DT_WR,
143 	UPLCOM_BULK_DT_RD,
144 	UPLCOM_INTR_DT_RD,
145 	UPLCOM_N_TRANSFER,
146 };
147 
148 struct uplcom_softc {
149 	struct ucom_super_softc sc_super_ucom;
150 	struct ucom_softc sc_ucom;
151 
152 	struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER];
153 	struct usb_device *sc_udev;
154 	struct mtx sc_mtx;
155 
156 	uint16_t sc_line;
157 
158 	uint8_t	sc_lsr;			/* local status register */
159 	uint8_t	sc_msr;			/* uplcom status register */
160 	uint8_t	sc_chiptype;		/* type of chip */
161 	uint8_t	sc_ctrl_iface_no;
162 	uint8_t	sc_data_iface_no;
163 	uint8_t	sc_iface_index[2];
164 };
165 
166 /* prototypes */
167 
168 static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *);
169 static usb_error_t uplcom_pl2303_do(struct usb_device *, int8_t, uint8_t,
170 			uint16_t, uint16_t, uint16_t);
171 static int	uplcom_pl2303_init(struct usb_device *, uint8_t);
172 static void	uplcom_free(struct ucom_softc *);
173 static void	uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
174 static void	uplcom_cfg_set_rts(struct ucom_softc *, uint8_t);
175 static void	uplcom_cfg_set_break(struct ucom_softc *, uint8_t);
176 static int	uplcom_pre_param(struct ucom_softc *, struct termios *);
177 static void	uplcom_cfg_param(struct ucom_softc *, struct termios *);
178 static void	uplcom_start_read(struct ucom_softc *);
179 static void	uplcom_stop_read(struct ucom_softc *);
180 static void	uplcom_start_write(struct ucom_softc *);
181 static void	uplcom_stop_write(struct ucom_softc *);
182 static void	uplcom_cfg_get_status(struct ucom_softc *, uint8_t *,
183 		    uint8_t *);
184 static void	uplcom_poll(struct ucom_softc *ucom);
185 
186 static device_probe_t uplcom_probe;
187 static device_attach_t uplcom_attach;
188 static device_detach_t uplcom_detach;
189 static void uplcom_free_softc(struct uplcom_softc *);
190 
191 static usb_callback_t uplcom_intr_callback;
192 static usb_callback_t uplcom_write_callback;
193 static usb_callback_t uplcom_read_callback;
194 
195 static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
196 
197 	[UPLCOM_BULK_DT_WR] = {
198 		.type = UE_BULK,
199 		.endpoint = UE_ADDR_ANY,
200 		.direction = UE_DIR_OUT,
201 		.bufsize = UPLCOM_BULK_BUF_SIZE,
202 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
203 		.callback = &uplcom_write_callback,
204 		.if_index = 0,
205 	},
206 
207 	[UPLCOM_BULK_DT_RD] = {
208 		.type = UE_BULK,
209 		.endpoint = UE_ADDR_ANY,
210 		.direction = UE_DIR_IN,
211 		.bufsize = UPLCOM_BULK_BUF_SIZE,
212 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
213 		.callback = &uplcom_read_callback,
214 		.if_index = 0,
215 	},
216 
217 	[UPLCOM_INTR_DT_RD] = {
218 		.type = UE_INTERRUPT,
219 		.endpoint = UE_ADDR_ANY,
220 		.direction = UE_DIR_IN,
221 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
222 		.bufsize = 0,	/* use wMaxPacketSize */
223 		.callback = &uplcom_intr_callback,
224 		.if_index = 1,
225 	},
226 };
227 
228 static struct ucom_callback uplcom_callback = {
229 	.ucom_cfg_get_status = &uplcom_cfg_get_status,
230 	.ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
231 	.ucom_cfg_set_rts = &uplcom_cfg_set_rts,
232 	.ucom_cfg_set_break = &uplcom_cfg_set_break,
233 	.ucom_cfg_param = &uplcom_cfg_param,
234 	.ucom_pre_param = &uplcom_pre_param,
235 	.ucom_start_read = &uplcom_start_read,
236 	.ucom_stop_read = &uplcom_stop_read,
237 	.ucom_start_write = &uplcom_start_write,
238 	.ucom_stop_write = &uplcom_stop_write,
239 	.ucom_poll = &uplcom_poll,
240 	.ucom_free = &uplcom_free,
241 };
242 
243 #define	UPLCOM_DEV(v,p)				\
244   { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p) }
245 
246 static const STRUCT_USB_HOST_ID uplcom_devs[] = {
247 	UPLCOM_DEV(ACERP, S81),			/* BenQ S81 phone */
248 	UPLCOM_DEV(ADLINK, ND6530),		/* ADLINK ND-6530 USB-Serial */
249 	UPLCOM_DEV(ALCATEL, OT535),		/* Alcatel One Touch 535/735 */
250 	UPLCOM_DEV(ALCOR, AU9720),		/* Alcor AU9720 USB 2.0-RS232 */
251 	UPLCOM_DEV(ANCHOR, SERIAL),		/* Anchor Serial adapter */
252 	UPLCOM_DEV(ATEN, UC232A),		/* PLANEX USB-RS232 URS-03 */
253 	UPLCOM_DEV(BELKIN, F5U257),		/* Belkin F5U257 USB to Serial */
254 	UPLCOM_DEV(COREGA, CGUSBRS232R),	/* Corega CG-USBRS232R */
255 	UPLCOM_DEV(EPSON, CRESSI_EDY),		/* Cressi Edy diving computer */
256 	UPLCOM_DEV(EPSON, N2ITION3),		/* Zeagle N2iTion3 diving computer */
257 	UPLCOM_DEV(ELECOM, UCSGT),		/* ELECOM UC-SGT Serial Adapter */
258 	UPLCOM_DEV(ELECOM, UCSGT0),		/* ELECOM UC-SGT Serial Adapter */
259 	UPLCOM_DEV(HAL, IMR001),		/* HAL Corporation Crossam2+USB */
260 	UPLCOM_DEV(HP, LD220),			/* HP LD220 POS Display */
261 	UPLCOM_DEV(IODATA, USBRSAQ),		/* I/O DATA USB-RSAQ */
262 	UPLCOM_DEV(IODATA, USBRSAQ5),		/* I/O DATA USB-RSAQ5 */
263 	UPLCOM_DEV(ITEGNO, WM1080A),		/* iTegno WM1080A GSM/GFPRS modem */
264 	UPLCOM_DEV(ITEGNO, WM2080A),		/* iTegno WM2080A CDMA modem */
265 	UPLCOM_DEV(LEADTEK, 9531),		/* Leadtek 9531 GPS */
266 	UPLCOM_DEV(MICROSOFT, 700WX),		/* Microsoft Palm 700WX */
267 	UPLCOM_DEV(MOBILEACTION, MA620),	/* Mobile Action MA-620 Infrared Adapter */
268 	UPLCOM_DEV(NETINDEX, WS002IN),		/* Willcom W-S002IN */
269 	UPLCOM_DEV(NOKIA2, CA42),		/* Nokia CA-42 cable */
270 	UPLCOM_DEV(OTI, DKU5),			/* OTI DKU-5 cable */
271 	UPLCOM_DEV(PANASONIC, TYTP50P6S),	/* Panasonic TY-TP50P6-S flat screen */
272 	UPLCOM_DEV(PLX, CA42),			/* PLX CA-42 clone cable */
273 	UPLCOM_DEV(PROLIFIC, ALLTRONIX_GPRS),	/* Alltronix ACM003U00 modem */
274 	UPLCOM_DEV(PROLIFIC, ALDIGA_AL11U),	/* AlDiga AL-11U modem */
275 	UPLCOM_DEV(PROLIFIC, DCU11),		/* DCU-11 Phone Cable */
276 	UPLCOM_DEV(PROLIFIC, HCR331),		/* HCR331 Card Reader */
277 	UPLCOM_DEV(PROLIFIC, MICROMAX_610U),	/* Micromax 610U modem */
278 	UPLCOM_DEV(PROLIFIC, MOTOROLA),		/* Motorola cable */
279 	UPLCOM_DEV(PROLIFIC, PHAROS),		/* Prolific Pharos */
280 	UPLCOM_DEV(PROLIFIC, PL2303),		/* Generic adapter */
281 	UPLCOM_DEV(PROLIFIC, RSAQ2),		/* I/O DATA USB-RSAQ2 */
282 	UPLCOM_DEV(PROLIFIC, RSAQ3),		/* I/O DATA USB-RSAQ3 */
283 	UPLCOM_DEV(PROLIFIC, UIC_MSR206),	/* UIC MSR206 Card Reader */
284 	UPLCOM_DEV(PROLIFIC2, PL2303),		/* Prolific adapter */
285 	UPLCOM_DEV(RADIOSHACK, USBCABLE),	/* Radio Shack USB Adapter */
286 	UPLCOM_DEV(RATOC, REXUSB60),		/* RATOC REX-USB60 */
287 	UPLCOM_DEV(SAGEM, USBSERIAL),		/* Sagem USB-Serial Controller */
288 	UPLCOM_DEV(SAMSUNG, I330),		/* Samsung I330 phone cradle */
289 	UPLCOM_DEV(SANWA, KB_USB2),		/* Sanwa KB-USB2 Multimeter cable */
290 	UPLCOM_DEV(SIEMENS3, EF81),		/* Siemens EF81 */
291 	UPLCOM_DEV(SIEMENS3, SX1),		/* Siemens SX1 */
292 	UPLCOM_DEV(SIEMENS3, X65),		/* Siemens X65 */
293 	UPLCOM_DEV(SIEMENS3, X75),		/* Siemens X75 */
294 	UPLCOM_DEV(SITECOM, SERIAL),		/* Sitecom USB to Serial */
295 	UPLCOM_DEV(SMART, PL2303),		/* SMART Technologies USB to Serial */
296 	UPLCOM_DEV(SONY, QN3),			/* Sony QN3 phone cable */
297 	UPLCOM_DEV(SONYERICSSON, DATAPILOT),	/* Sony Ericsson Datapilot */
298 	UPLCOM_DEV(SONYERICSSON, DCU10),	/* Sony Ericsson DCU-10 Cable */
299 	UPLCOM_DEV(SOURCENEXT, KEIKAI8),	/* SOURCENEXT KeikaiDenwa 8 */
300 	UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG),	/* SOURCENEXT KeikaiDenwa 8 with charger */
301 	UPLCOM_DEV(SPEEDDRAGON, MS3303H),	/* Speed Dragon USB-Serial */
302 	UPLCOM_DEV(SYNTECH, CPT8001C),		/* Syntech CPT-8001C Barcode scanner */
303 	UPLCOM_DEV(TDK, UHA6400),		/* TDK USB-PHS Adapter UHA6400 */
304 	UPLCOM_DEV(TDK, UPA9664),		/* TDK USB-PHS Adapter UPA9664 */
305 	UPLCOM_DEV(TRIPPLITE, U209),		/* Tripp-Lite U209-000-R USB to Serial */
306 	UPLCOM_DEV(YCCABLE, PL2303),		/* YC Cable USB-Serial */
307 };
308 #undef UPLCOM_DEV
309 
310 static device_method_t uplcom_methods[] = {
311 	DEVMETHOD(device_probe, uplcom_probe),
312 	DEVMETHOD(device_attach, uplcom_attach),
313 	DEVMETHOD(device_detach, uplcom_detach),
314 	DEVMETHOD_END
315 };
316 
317 static devclass_t uplcom_devclass;
318 
319 static driver_t uplcom_driver = {
320 	.name = "uplcom",
321 	.methods = uplcom_methods,
322 	.size = sizeof(struct uplcom_softc),
323 };
324 
325 DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0);
326 MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
327 MODULE_DEPEND(uplcom, usb, 1, 1, 1);
328 MODULE_VERSION(uplcom, UPLCOM_MODVER);
329 USB_PNP_HOST_INFO(uplcom_devs);
330 
331 static int
332 uplcom_probe(device_t dev)
333 {
334 	struct usb_attach_arg *uaa = device_get_ivars(dev);
335 
336 	DPRINTFN(11, "\n");
337 
338 	if (uaa->usb_mode != USB_MODE_HOST) {
339 		return (ENXIO);
340 	}
341 	if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
342 		return (ENXIO);
343 	}
344 	if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
345 		return (ENXIO);
346 	}
347 	return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
348 }
349 
350 static int
351 uplcom_attach(device_t dev)
352 {
353 	struct usb_attach_arg *uaa = device_get_ivars(dev);
354 	struct uplcom_softc *sc = device_get_softc(dev);
355 	struct usb_interface *iface;
356 	struct usb_interface_descriptor *id;
357 	struct usb_device_descriptor *dd;
358 	int error;
359 
360 	DPRINTFN(11, "\n");
361 
362 	device_set_usb_desc(dev);
363 	mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
364 	ucom_ref(&sc->sc_super_ucom);
365 
366 	DPRINTF("sc = %p\n", sc);
367 
368 	sc->sc_udev = uaa->device;
369 
370 	/* Determine the chip type.  This algorithm is taken from Linux. */
371 	dd = usbd_get_device_descriptor(sc->sc_udev);
372 	if (dd->bDeviceClass == 0x02)
373 		sc->sc_chiptype = TYPE_PL2303;
374 	else if (dd->bMaxPacketSize == 0x40)
375 		sc->sc_chiptype = TYPE_PL2303HX;
376 	else
377 		sc->sc_chiptype = TYPE_PL2303;
378 
379 	DPRINTF("chiptype: %s\n",
380 	    (sc->sc_chiptype == TYPE_PL2303HX) ?
381 	    "2303X" : "2303");
382 
383 	/*
384 	 * USB-RSAQ1 has two interface
385 	 *
386 	 *  USB-RSAQ1       | USB-RSAQ2
387 	 * -----------------+-----------------
388 	 * Interface 0      |Interface 0
389 	 *  Interrupt(0x81) | Interrupt(0x81)
390 	 * -----------------+ BulkIN(0x02)
391 	 * Interface 1	    | BulkOUT(0x83)
392 	 *   BulkIN(0x02)   |
393 	 *   BulkOUT(0x83)  |
394 	 */
395 
396 	sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
397 	sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
398 
399 	iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
400 	if (iface) {
401 		id = usbd_get_interface_descriptor(iface);
402 		if (id == NULL) {
403 			device_printf(dev, "no interface descriptor (2)\n");
404 			goto detach;
405 		}
406 		sc->sc_data_iface_no = id->bInterfaceNumber;
407 		sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
408 		usbd_set_parent_iface(uaa->device,
409 		    UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
410 	} else {
411 		sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
412 		sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
413 	}
414 
415 	error = usbd_transfer_setup(uaa->device,
416 	    sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
417 	    UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
418 	if (error) {
419 		DPRINTF("one or more missing USB endpoints, "
420 		    "error=%s\n", usbd_errstr(error));
421 		goto detach;
422 	}
423 	error = uplcom_reset(sc, uaa->device);
424 	if (error) {
425 		device_printf(dev, "reset failed, error=%s\n",
426 		    usbd_errstr(error));
427 		goto detach;
428 	}
429 
430 	if (sc->sc_chiptype != TYPE_PL2303HX) {
431 		/* HX variants seem to lock up after a clear stall request. */
432 		mtx_lock(&sc->sc_mtx);
433 		usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
434 		usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
435 		mtx_unlock(&sc->sc_mtx);
436 	} else {
437 		if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
438 		    UPLCOM_SET_REQUEST, 8, 0, 0) ||
439 		    uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
440 		    UPLCOM_SET_REQUEST, 9, 0, 0)) {
441 			goto detach;
442 		}
443 	}
444 
445 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
446 	    &uplcom_callback, &sc->sc_mtx);
447 	if (error) {
448 		goto detach;
449 	}
450 	/*
451 	 * do the initialization during attach so that the system does not
452 	 * sleep during open:
453 	 */
454 	if (uplcom_pl2303_init(uaa->device, sc->sc_chiptype)) {
455 		device_printf(dev, "init failed\n");
456 		goto detach;
457 	}
458 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
459 
460 	return (0);
461 
462 detach:
463 	uplcom_detach(dev);
464 	return (ENXIO);
465 }
466 
467 static int
468 uplcom_detach(device_t dev)
469 {
470 	struct uplcom_softc *sc = device_get_softc(dev);
471 
472 	DPRINTF("sc=%p\n", sc);
473 
474 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
475 	usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
476 
477 	device_claim_softc(dev);
478 
479 	uplcom_free_softc(sc);
480 
481 	return (0);
482 }
483 
484 UCOM_UNLOAD_DRAIN(uplcom);
485 
486 static void
487 uplcom_free_softc(struct uplcom_softc *sc)
488 {
489 	if (ucom_unref(&sc->sc_super_ucom)) {
490 		mtx_destroy(&sc->sc_mtx);
491 		device_free_softc(sc);
492 	}
493 }
494 
495 static void
496 uplcom_free(struct ucom_softc *ucom)
497 {
498 	uplcom_free_softc(ucom->sc_parent);
499 }
500 
501 static usb_error_t
502 uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
503 {
504 	struct usb_device_request req;
505 
506 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
507 	req.bRequest = UPLCOM_SET_REQUEST;
508 	USETW(req.wValue, 0);
509 	req.wIndex[0] = sc->sc_data_iface_no;
510 	req.wIndex[1] = 0;
511 	USETW(req.wLength, 0);
512 
513 	return (usbd_do_request(udev, NULL, &req, NULL));
514 }
515 
516 static usb_error_t
517 uplcom_pl2303_do(struct usb_device *udev, int8_t req_type, uint8_t request,
518     uint16_t value, uint16_t index, uint16_t length)
519 {
520 	struct usb_device_request req;
521 	usb_error_t err;
522 	uint8_t buf[4];
523 
524 	req.bmRequestType = req_type;
525 	req.bRequest = request;
526 	USETW(req.wValue, value);
527 	USETW(req.wIndex, index);
528 	USETW(req.wLength, length);
529 
530 	err = usbd_do_request(udev, NULL, &req, buf);
531 	if (err) {
532 		DPRINTF("error=%s\n", usbd_errstr(err));
533 		return (1);
534 	}
535 	return (0);
536 }
537 
538 static int
539 uplcom_pl2303_init(struct usb_device *udev, uint8_t chiptype)
540 {
541 	int err;
542 
543 	if (uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
544 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0)
545 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
546 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
547 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
548 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0)
549 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
550 	    || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
551 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0)
552 	    || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0))
553 		return (EIO);
554 
555 	if (chiptype == TYPE_PL2303HX)
556 		err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0);
557 	else
558 		err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x24, 0);
559 	if (err)
560 		return (EIO);
561 
562 	return (0);
563 }
564 
565 static void
566 uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
567 {
568 	struct uplcom_softc *sc = ucom->sc_parent;
569 	struct usb_device_request req;
570 
571 	DPRINTF("onoff = %d\n", onoff);
572 
573 	if (onoff)
574 		sc->sc_line |= UCDC_LINE_DTR;
575 	else
576 		sc->sc_line &= ~UCDC_LINE_DTR;
577 
578 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
579 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
580 	USETW(req.wValue, sc->sc_line);
581 	req.wIndex[0] = sc->sc_data_iface_no;
582 	req.wIndex[1] = 0;
583 	USETW(req.wLength, 0);
584 
585 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
586 	    &req, NULL, 0, 1000);
587 }
588 
589 static void
590 uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
591 {
592 	struct uplcom_softc *sc = ucom->sc_parent;
593 	struct usb_device_request req;
594 
595 	DPRINTF("onoff = %d\n", onoff);
596 
597 	if (onoff)
598 		sc->sc_line |= UCDC_LINE_RTS;
599 	else
600 		sc->sc_line &= ~UCDC_LINE_RTS;
601 
602 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
603 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
604 	USETW(req.wValue, sc->sc_line);
605 	req.wIndex[0] = sc->sc_data_iface_no;
606 	req.wIndex[1] = 0;
607 	USETW(req.wLength, 0);
608 
609 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
610 	    &req, NULL, 0, 1000);
611 }
612 
613 static void
614 uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
615 {
616 	struct uplcom_softc *sc = ucom->sc_parent;
617 	struct usb_device_request req;
618 	uint16_t temp;
619 
620 	DPRINTF("onoff = %d\n", onoff);
621 
622 	temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
623 
624 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
625 	req.bRequest = UCDC_SEND_BREAK;
626 	USETW(req.wValue, temp);
627 	req.wIndex[0] = sc->sc_data_iface_no;
628 	req.wIndex[1] = 0;
629 	USETW(req.wLength, 0);
630 
631 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
632 	    &req, NULL, 0, 1000);
633 }
634 
635 static const uint32_t uplcom_rates[] = {
636 	75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
637 	19200, 28800, 38400, 57600, 115200,
638 	/*
639 	 * Higher speeds are probably possible. PL2303X supports up to
640 	 * 6Mb and can set any rate
641 	 */
642 	230400, 460800, 614400, 921600, 1228800
643 };
644 
645 #define	N_UPLCOM_RATES	(sizeof(uplcom_rates)/sizeof(uplcom_rates[0]))
646 
647 static int
648 uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
649 {
650 	struct uplcom_softc *sc = ucom->sc_parent;
651 	uint8_t i;
652 
653 	DPRINTF("\n");
654 
655 	/**
656 	 * Check requested baud rate.
657 	 *
658 	 * The PL2303 can only set specific baud rates, up to 1228800 baud.
659 	 * The PL2303X can set any baud rate up to 6Mb.
660 	 * The PL2303HX rev. D can set any baud rate up to 12Mb.
661 	 *
662 	 * XXX: We currently cannot identify the PL2303HX rev. D, so treat
663 	 *      it the same as the PL2303X.
664 	 */
665 	if (sc->sc_chiptype != TYPE_PL2303HX) {
666 		for (i = 0; i < N_UPLCOM_RATES; i++) {
667 			if (uplcom_rates[i] == t->c_ospeed)
668 				return (0);
669 		}
670  	} else {
671 		if (t->c_ospeed <= 6000000)
672 			return (0);
673 	}
674 
675 	DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed);
676 	return (EIO);
677 }
678 
679 static void
680 uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
681 {
682 	struct uplcom_softc *sc = ucom->sc_parent;
683 	struct usb_cdc_line_state ls;
684 	struct usb_device_request req;
685 
686 	DPRINTF("sc = %p\n", sc);
687 
688 	memset(&ls, 0, sizeof(ls));
689 
690 	USETDW(ls.dwDTERate, t->c_ospeed);
691 
692 	if (t->c_cflag & CSTOPB) {
693 		ls.bCharFormat = UCDC_STOP_BIT_2;
694 	} else {
695 		ls.bCharFormat = UCDC_STOP_BIT_1;
696 	}
697 
698 	if (t->c_cflag & PARENB) {
699 		if (t->c_cflag & PARODD) {
700 			ls.bParityType = UCDC_PARITY_ODD;
701 		} else {
702 			ls.bParityType = UCDC_PARITY_EVEN;
703 		}
704 	} else {
705 		ls.bParityType = UCDC_PARITY_NONE;
706 	}
707 
708 	switch (t->c_cflag & CSIZE) {
709 	case CS5:
710 		ls.bDataBits = 5;
711 		break;
712 	case CS6:
713 		ls.bDataBits = 6;
714 		break;
715 	case CS7:
716 		ls.bDataBits = 7;
717 		break;
718 	case CS8:
719 		ls.bDataBits = 8;
720 		break;
721 	}
722 
723 	DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
724 	    UGETDW(ls.dwDTERate), ls.bCharFormat,
725 	    ls.bParityType, ls.bDataBits);
726 
727 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
728 	req.bRequest = UCDC_SET_LINE_CODING;
729 	USETW(req.wValue, 0);
730 	req.wIndex[0] = sc->sc_data_iface_no;
731 	req.wIndex[1] = 0;
732 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
733 
734 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
735 	    &req, &ls, 0, 1000);
736 
737 	if (t->c_cflag & CRTSCTS) {
738 
739 		DPRINTF("crtscts = on\n");
740 
741 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
742 		req.bRequest = UPLCOM_SET_REQUEST;
743 		USETW(req.wValue, 0);
744 		if (sc->sc_chiptype == TYPE_PL2303HX)
745 			USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
746 		else
747 			USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
748 		USETW(req.wLength, 0);
749 
750 		ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
751 		    &req, NULL, 0, 1000);
752 	} else {
753 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
754 		req.bRequest = UPLCOM_SET_REQUEST;
755 		USETW(req.wValue, 0);
756 		USETW(req.wIndex, 0);
757 		USETW(req.wLength, 0);
758 		ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
759 		    &req, NULL, 0, 1000);
760 	}
761 }
762 
763 static void
764 uplcom_start_read(struct ucom_softc *ucom)
765 {
766 	struct uplcom_softc *sc = ucom->sc_parent;
767 
768 	/* start interrupt endpoint */
769 	usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
770 
771 	/* start read endpoint */
772 	usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
773 }
774 
775 static void
776 uplcom_stop_read(struct ucom_softc *ucom)
777 {
778 	struct uplcom_softc *sc = ucom->sc_parent;
779 
780 	/* stop interrupt endpoint */
781 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
782 
783 	/* stop read endpoint */
784 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
785 }
786 
787 static void
788 uplcom_start_write(struct ucom_softc *ucom)
789 {
790 	struct uplcom_softc *sc = ucom->sc_parent;
791 
792 	usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
793 }
794 
795 static void
796 uplcom_stop_write(struct ucom_softc *ucom)
797 {
798 	struct uplcom_softc *sc = ucom->sc_parent;
799 
800 	usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
801 }
802 
803 static void
804 uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
805 {
806 	struct uplcom_softc *sc = ucom->sc_parent;
807 
808 	DPRINTF("\n");
809 
810 	*lsr = sc->sc_lsr;
811 	*msr = sc->sc_msr;
812 }
813 
814 static void
815 uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
816 {
817 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
818 	struct usb_page_cache *pc;
819 	uint8_t buf[9];
820 	int actlen;
821 
822 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
823 
824 	switch (USB_GET_STATE(xfer)) {
825 	case USB_ST_TRANSFERRED:
826 
827 		DPRINTF("actlen = %u\n", actlen);
828 
829 		if (actlen >= 9) {
830 
831 			pc = usbd_xfer_get_frame(xfer, 0);
832 			usbd_copy_out(pc, 0, buf, sizeof(buf));
833 
834 			DPRINTF("status = 0x%02x\n", buf[8]);
835 
836 			sc->sc_lsr = 0;
837 			sc->sc_msr = 0;
838 
839 			if (buf[8] & RSAQ_STATUS_CTS) {
840 				sc->sc_msr |= SER_CTS;
841 			}
842 			if (buf[8] & RSAQ_STATUS_DSR) {
843 				sc->sc_msr |= SER_DSR;
844 			}
845 			if (buf[8] & RSAQ_STATUS_DCD) {
846 				sc->sc_msr |= SER_DCD;
847 			}
848 			ucom_status_change(&sc->sc_ucom);
849 		}
850 	case USB_ST_SETUP:
851 tr_setup:
852 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
853 		usbd_transfer_submit(xfer);
854 		return;
855 
856 	default:			/* Error */
857 		if (error != USB_ERR_CANCELLED) {
858 			/* try to clear stall first */
859 			usbd_xfer_set_stall(xfer);
860 			goto tr_setup;
861 		}
862 		return;
863 	}
864 }
865 
866 static void
867 uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
868 {
869 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
870 	struct usb_page_cache *pc;
871 	uint32_t actlen;
872 
873 	switch (USB_GET_STATE(xfer)) {
874 	case USB_ST_SETUP:
875 	case USB_ST_TRANSFERRED:
876 tr_setup:
877 		pc = usbd_xfer_get_frame(xfer, 0);
878 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
879 		    UPLCOM_BULK_BUF_SIZE, &actlen)) {
880 
881 			DPRINTF("actlen = %d\n", actlen);
882 
883 			usbd_xfer_set_frame_len(xfer, 0, actlen);
884 			usbd_transfer_submit(xfer);
885 		}
886 		return;
887 
888 	default:			/* Error */
889 		if (error != USB_ERR_CANCELLED) {
890 			/* try to clear stall first */
891 			usbd_xfer_set_stall(xfer);
892 			goto tr_setup;
893 		}
894 		return;
895 	}
896 }
897 
898 static void
899 uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
900 {
901 	struct uplcom_softc *sc = usbd_xfer_softc(xfer);
902 	struct usb_page_cache *pc;
903 	int actlen;
904 
905 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
906 
907 	switch (USB_GET_STATE(xfer)) {
908 	case USB_ST_TRANSFERRED:
909 		pc = usbd_xfer_get_frame(xfer, 0);
910 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
911 
912 	case USB_ST_SETUP:
913 tr_setup:
914 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
915 		usbd_transfer_submit(xfer);
916 		return;
917 
918 	default:			/* Error */
919 		if (error != USB_ERR_CANCELLED) {
920 			/* try to clear stall first */
921 			usbd_xfer_set_stall(xfer);
922 			goto tr_setup;
923 		}
924 		return;
925 	}
926 }
927 
928 static void
929 uplcom_poll(struct ucom_softc *ucom)
930 {
931 	struct uplcom_softc *sc = ucom->sc_parent;
932 	usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);
933 }
934