xref: /netbsd-src/sys/dev/usb/uplcom.c (revision cd22f25e6f6d1cc1f197fe8c5468a80f51d1c4e1)
1 /*	$NetBSD: uplcom.c,v 1.64 2008/04/28 20:24:00 martin Exp $	*/
2 /*
3  * Copyright (c) 2001 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Ichiro FUKUHARA (ichiro@ichiro.org).
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*
32  * General information: http://www.prolific.com.tw/fr_pl2303.htm
33  * http://www.hitachi-hitec.com/jyouhou/prolific/2303.pdf
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: uplcom.c,v 1.64 2008/04/28 20:24:00 martin Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/ioctl.h>
44 #include <sys/conf.h>
45 #include <sys/tty.h>
46 #include <sys/file.h>
47 #include <sys/select.h>
48 #include <sys/proc.h>
49 #include <sys/device.h>
50 #include <sys/poll.h>
51 
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbcdc.h>
54 
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
57 #include <dev/usb/usbdevs.h>
58 #include <dev/usb/usb_quirks.h>
59 
60 #include <dev/usb/ucomvar.h>
61 
62 #ifdef UPLCOM_DEBUG
63 #define DPRINTFN(n, x)  if (uplcomdebug > (n)) logprintf x
64 int	uplcomdebug = 0;
65 #else
66 #define DPRINTFN(n, x)
67 #endif
68 #define DPRINTF(x) DPRINTFN(0, x)
69 
70 #define	UPLCOM_CONFIG_INDEX	0
71 #define	UPLCOM_IFACE_INDEX	0
72 #define	UPLCOM_SECOND_IFACE_INDEX	1
73 
74 #define	UPLCOM_SET_REQUEST	0x01
75 #define	UPLCOM_SET_CRTSCTS_0	0x41
76 #define	UPLCOM_SET_CRTSCTS_HX	0x61
77 #define RSAQ_STATUS_DSR		0x02
78 #define RSAQ_STATUS_DCD		0x01
79 
80 enum  pl2303_type {
81 	UPLCOM_TYPE_0,
82 	UPLCOM_TYPE_HX,
83 };
84 
85 struct	uplcom_softc {
86 	USBBASEDEVICE		sc_dev;		/* base device */
87 	usbd_device_handle	sc_udev;	/* USB device */
88 	usbd_interface_handle	sc_iface;	/* interface */
89 	int			sc_iface_number;	/* interface number */
90 
91 	usbd_interface_handle	sc_intr_iface;	/* interrupt interface */
92 	int			sc_intr_number;	/* interrupt number */
93 	usbd_pipe_handle	sc_intr_pipe;	/* interrupt pipe */
94 	u_char			*sc_intr_buf;	/* interrupt buffer */
95 	int			sc_isize;
96 
97 	usb_cdc_line_state_t	sc_line_state;	/* current line state */
98 	int			sc_dtr;		/* current DTR state */
99 	int			sc_rts;		/* current RTS state */
100 
101 	device_ptr_t		sc_subdev;	/* ucom device */
102 
103 	u_char			sc_dying;	/* disconnecting */
104 
105 	u_char			sc_lsr;		/* Local status register */
106 	u_char			sc_msr;		/* uplcom status register */
107 
108 	enum pl2303_type	sc_type;	/* PL2303 chip type */
109 };
110 
111 /*
112  * These are the maximum number of bytes transferred per frame.
113  * The output buffer size cannot be increased due to the size encoding.
114  */
115 #define UPLCOMIBUFSIZE 256
116 #define UPLCOMOBUFSIZE 256
117 
118 Static	usbd_status uplcom_reset(struct uplcom_softc *);
119 Static	usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
120 					   usb_cdc_line_state_t *state);
121 Static	usbd_status uplcom_set_crtscts(struct uplcom_softc *);
122 Static	void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
123 
124 Static	void uplcom_set(void *, int, int, int);
125 Static	void uplcom_dtr(struct uplcom_softc *, int);
126 Static	void uplcom_rts(struct uplcom_softc *, int);
127 Static	void uplcom_break(struct uplcom_softc *, int);
128 Static	void uplcom_set_line_state(struct uplcom_softc *);
129 Static	void uplcom_get_status(void *, int portno, u_char *lsr, u_char *msr);
130 #if TODO
131 Static	int  uplcom_ioctl(void *, int, u_long, void *, int, usb_proc_ptr );
132 #endif
133 Static	int  uplcom_param(void *, int, struct termios *);
134 Static	int  uplcom_open(void *, int);
135 Static	void uplcom_close(void *, int);
136 Static usbd_status uplcom_vendor_control_write(usbd_device_handle, u_int16_t, u_int16_t);
137 
138 struct	ucom_methods uplcom_methods = {
139 	uplcom_get_status,
140 	uplcom_set,
141 	uplcom_param,
142 	NULL, /* uplcom_ioctl, TODO */
143 	uplcom_open,
144 	uplcom_close,
145 	NULL,
146 	NULL,
147 };
148 
149 static const struct uplcom_type {
150       struct usb_devno uplcom_dev;
151       int32_t	  release;
152       enum pl2303_type chiptype;
153 } uplcom_devs[] = {
154 	/* I/O DATA USB-RSAQ2 */
155 	{ { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
156 		-1, UPLCOM_TYPE_0 },
157 	/* I/O DATA USB-RSAQ3 */
158 	{ { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3 },
159 		-1, UPLCOM_TYPE_HX },
160 	/* I/O DATA USB-RSAQ */
161 	{ { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
162 		-1, UPLCOM_TYPE_0 },
163 	/* I/O DATA USB-RSAQ5 */
164 	{ { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ5 },
165 		-1, UPLCOM_TYPE_HX },
166 	/* PLANEX USB-RS232 URS-03 */
167 	{ { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
168 		-1, UPLCOM_TYPE_0 },
169 	/* TrendNet TU-S9 */
170 	{ { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
171 		0x400, UPLCOM_TYPE_HX },
172 	/* ST Lab USB-SERIAL-4 */
173 	{ { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
174 		0x300, UPLCOM_TYPE_HX },
175 	/* IOGEAR/ATEN UC-232A */
176 	{ { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
177 		-1, UPLCOM_TYPE_0 },
178 	/* SMART Technologies USB to serial */
179 	{ { USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_PL2303 },
180 		-1, UPLCOM_TYPE_HX },
181 	/* IOGEAR/ATENTRIPPLITE */
182 	{ { USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
183 		0x300, UPLCOM_TYPE_HX },
184 	{ { USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
185 		-1, UPLCOM_TYPE_0 },
186 	/* ELECOM UC-SGT */
187 	{ { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
188 		-1, UPLCOM_TYPE_0 },
189 	/* ELECOM UC-SGT0 */
190 	{ { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 },
191 		-1, UPLCOM_TYPE_0 },
192 	/* Panasonic 50" Touch Panel */
193 	{ { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_TYTP50P6S },
194 		-1, UPLCOM_TYPE_0 },
195 	/* RATOC REX-USB60 */
196 	{ { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
197 		-1, UPLCOM_TYPE_0 },
198 	/* TDK USB-PHS Adapter UHA6400 */
199 	{ { USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
200 		-1, UPLCOM_TYPE_0 },
201 	/* TDK USB-PDC Adapter UPA9664 */
202 	{ { USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 },
203 		-1, UPLCOM_TYPE_0 },
204 	/* Sony Ericsson USB Cable */
205 	{ { USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU10 },
206 		-1, UPLCOM_TYPE_0 },
207 	/* SOURCENEXT KeikaiDenwa 8 */
208 	{ { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 },
209 		-1, UPLCOM_TYPE_0 },
210 	/* SOURCENEXT KeikaiDenwa 8 with charger */
211 	{ { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG },
212 		-1, UPLCOM_TYPE_0 },
213 	/* HAL Corporation Crossam2+USB */
214 	{ { USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 },
215 		-1, UPLCOM_TYPE_0 },
216 	/* Sitecom USB to serial cable */
217 	{ { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 },
218 		-1, UPLCOM_TYPE_0 },
219 	/* Pharos USB GPS - Microsoft version */
220 	{ { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X },
221 		-1, UPLCOM_TYPE_0 },
222 	/* Willcom WS002IN (DD) */
223 	{ { USB_VENDOR_NETINDEX, USB_PRODUCT_NETINDEX_WS002IN },
224 		-1, UPLCOM_TYPE_HX },
225 };
226 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
227 
228 int uplcom_match(device_t, struct cfdata *, void *);
229 void uplcom_attach(device_t, device_t, void *);
230 void uplcom_childdet(device_t, device_t);
231 int uplcom_detach(device_t, int);
232 int uplcom_activate(device_t, enum devact);
233 extern struct cfdriver uplcom_cd;
234 CFATTACH_DECL2(uplcom, sizeof(struct uplcom_softc), uplcom_match,
235     uplcom_attach, uplcom_detach, uplcom_activate, NULL, uplcom_childdet);
236 
237 USB_MATCH(uplcom)
238 {
239 	USB_MATCH_START(uplcom, uaa);
240 
241 	return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ?
242 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
243 }
244 
245 USB_ATTACH(uplcom)
246 {
247 	USB_ATTACH_START(uplcom, sc, uaa);
248 	usbd_device_handle dev = uaa->device;
249 	usb_config_descriptor_t *cdesc;
250 	usb_interface_descriptor_t *id;
251 	usb_endpoint_descriptor_t *ed;
252 	char *devinfop;
253 	const char *devname = USBDEVNAME(sc->sc_dev);
254 	usbd_status err;
255 	int i;
256 	struct ucom_attach_args uca;
257 
258 	devinfop = usbd_devinfo_alloc(dev, 0);
259 	USB_ATTACH_SETUP;
260 	printf("%s: %s\n", devname, devinfop);
261 	usbd_devinfo_free(devinfop);
262 
263         sc->sc_udev = dev;
264 
265 	DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
266 
267 	/* initialize endpoints */
268 	uca.bulkin = uca.bulkout = -1;
269 	sc->sc_intr_number = -1;
270 	sc->sc_intr_pipe = NULL;
271 
272 	/* Move the device into the configured state. */
273 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
274 	if (err) {
275 		printf("\n%s: failed to set configuration, err=%s\n",
276 			devname, usbd_errstr(err));
277 		sc->sc_dying = 1;
278 		USB_ATTACH_ERROR_RETURN;
279 	}
280 
281 	/* determine chip type */
282 	for (i = 0; uplcom_devs[i].uplcom_dev.ud_vendor != 0; i++) {
283 		if (uplcom_devs[i].uplcom_dev.ud_vendor == uaa->vendor &&
284 		    uplcom_devs[i].uplcom_dev.ud_product == uaa->product &&
285 		    (uplcom_devs[i].release == uaa->release ||
286 		    uplcom_devs[i].release == -1)) {
287 			sc->sc_type = uplcom_devs[i].chiptype;
288 			break;
289 		}
290 	}
291 
292 #ifdef USB_DEBUG
293 	/* print the chip type */
294 	if (sc->sc_type == UPLCOM_TYPE_HX) {
295 		DPRINTF(("uplcom_attach: chiptype HX\n"));
296 	} else {
297 		DPRINTF(("uplcom_attach: chiptype 0\n"));
298 	}
299 #endif
300 
301 	/* Move the device into the configured state. */
302 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
303 	if (err) {
304 		printf("%s: failed to set configuration: %s\n",
305 			devname, usbd_errstr(err));
306 		sc->sc_dying = 1;
307 		USB_ATTACH_ERROR_RETURN;
308 	}
309 
310 	/* get the config descriptor */
311 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
312 
313 	if (cdesc == NULL) {
314 		printf("%s: failed to get configuration descriptor\n",
315 			USBDEVNAME(sc->sc_dev));
316 		sc->sc_dying = 1;
317 		USB_ATTACH_ERROR_RETURN;
318 	}
319 
320 	/* get the (first/common) interface */
321 	err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
322 							&sc->sc_iface);
323 	if (err) {
324 		printf("\n%s: failed to get interface, err=%s\n",
325 			devname, usbd_errstr(err));
326 		sc->sc_dying = 1;
327 		USB_ATTACH_ERROR_RETURN;
328 	}
329 
330 	/* Find the interrupt endpoints */
331 
332 	id = usbd_get_interface_descriptor(sc->sc_iface);
333 	sc->sc_iface_number = id->bInterfaceNumber;
334 
335 	for (i = 0; i < id->bNumEndpoints; i++) {
336 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
337 		if (ed == NULL) {
338 			printf("%s: no endpoint descriptor for %d\n",
339 				USBDEVNAME(sc->sc_dev), i);
340 			sc->sc_dying = 1;
341 			USB_ATTACH_ERROR_RETURN;
342 		}
343 
344 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
345 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
346 			sc->sc_intr_number = ed->bEndpointAddress;
347 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
348 		}
349 	}
350 
351 	if (sc->sc_intr_number== -1) {
352 		printf("%s: Could not find interrupt in\n",
353 			USBDEVNAME(sc->sc_dev));
354 		sc->sc_dying = 1;
355 		USB_ATTACH_ERROR_RETURN;
356 	}
357 
358 	/* keep interface for interrupt */
359 	sc->sc_intr_iface = sc->sc_iface;
360 
361 	/*
362 	 * USB-RSAQ1 has two interface
363 	 *
364 	 *  USB-RSAQ1       | USB-RSAQ2
365  	 * -----------------+-----------------
366 	 * Interface 0      |Interface 0
367 	 *  Interrupt(0x81) | Interrupt(0x81)
368 	 * -----------------+ BulkIN(0x02)
369 	 * Interface 1	    | BulkOUT(0x83)
370 	 *   BulkIN(0x02)   |
371 	 *   BulkOUT(0x83)  |
372 	 */
373 	if (cdesc->bNumInterface == 2) {
374 		err = usbd_device2interface_handle(dev,
375 				UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
376 		if (err) {
377 			printf("\n%s: failed to get second interface, err=%s\n",
378 							devname, usbd_errstr(err));
379 			sc->sc_dying = 1;
380 			USB_ATTACH_ERROR_RETURN;
381 		}
382 	}
383 
384 	/* Find the bulk{in,out} endpoints */
385 
386 	id = usbd_get_interface_descriptor(sc->sc_iface);
387 	sc->sc_iface_number = id->bInterfaceNumber;
388 
389 	for (i = 0; i < id->bNumEndpoints; i++) {
390 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
391 		if (ed == NULL) {
392 			printf("%s: no endpoint descriptor for %d\n",
393 				USBDEVNAME(sc->sc_dev), i);
394 			sc->sc_dying = 1;
395 			USB_ATTACH_ERROR_RETURN;
396 		}
397 
398 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
399 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
400 			uca.bulkin = ed->bEndpointAddress;
401 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
402 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
403 			uca.bulkout = ed->bEndpointAddress;
404 		}
405 	}
406 
407 	if (uca.bulkin == -1) {
408 		printf("%s: Could not find data bulk in\n",
409 			USBDEVNAME(sc->sc_dev));
410 		sc->sc_dying = 1;
411 		USB_ATTACH_ERROR_RETURN;
412 	}
413 
414 	if (uca.bulkout == -1) {
415 		printf("%s: Could not find data bulk out\n",
416 			USBDEVNAME(sc->sc_dev));
417 		sc->sc_dying = 1;
418 		USB_ATTACH_ERROR_RETURN;
419 	}
420 
421 	sc->sc_dtr = sc->sc_rts = -1;
422 	uca.portno = UCOM_UNK_PORTNO;
423 	/* bulkin, bulkout set above */
424 	uca.ibufsize = UPLCOMIBUFSIZE;
425 	uca.obufsize = UPLCOMOBUFSIZE;
426 	uca.ibufsizepad = UPLCOMIBUFSIZE;
427 	uca.opkthdrlen = 0;
428 	uca.device = dev;
429 	uca.iface = sc->sc_iface;
430 	uca.methods = &uplcom_methods;
431 	uca.arg = sc;
432 	uca.info = NULL;
433 
434 	err = uplcom_reset(sc);
435 
436 	if (err) {
437 		printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev),
438 			usbd_errstr(err));
439 		sc->sc_dying = 1;
440 		USB_ATTACH_ERROR_RETURN;
441 	}
442 
443 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
444 			   USBDEV(sc->sc_dev));
445 
446 	DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
447 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
448 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
449 					    ucomprint, ucomsubmatch);
450 
451 	USB_ATTACH_SUCCESS_RETURN;
452 }
453 
454 void
455 uplcom_childdet(device_t self, device_t child)
456 {
457 	struct uplcom_softc *sc = device_private(self);
458 
459 	KASSERT(sc->sc_subdev == child);
460 	sc->sc_subdev = NULL;
461 }
462 
463 USB_DETACH(uplcom)
464 {
465 	USB_DETACH_START(uplcom, sc);
466 	int rv = 0;
467 
468 	DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
469 
470         if (sc->sc_intr_pipe != NULL) {
471                 usbd_abort_pipe(sc->sc_intr_pipe);
472                 usbd_close_pipe(sc->sc_intr_pipe);
473 		free(sc->sc_intr_buf, M_USBDEV);
474                 sc->sc_intr_pipe = NULL;
475         }
476 
477 	sc->sc_dying = 1;
478 	if (sc->sc_subdev != NULL)
479 		rv = config_detach(sc->sc_subdev, flags);
480 
481 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
482 			   USBDEV(sc->sc_dev));
483 
484 	return (rv);
485 }
486 
487 int
488 uplcom_activate(device_t self, enum devact act)
489 {
490 	struct uplcom_softc *sc = device_private(self);
491 	int rv = 0;
492 
493 	switch (act) {
494 	case DVACT_ACTIVATE:
495 		return (EOPNOTSUPP);
496 
497 	case DVACT_DEACTIVATE:
498 		if (sc->sc_subdev != NULL)
499 			rv = config_deactivate(sc->sc_subdev);
500 		sc->sc_dying = 1;
501 		break;
502 	}
503 	return (rv);
504 }
505 
506 usbd_status
507 uplcom_reset(struct uplcom_softc *sc)
508 {
509         usb_device_request_t req;
510 	usbd_status err;
511 
512         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
513         req.bRequest = UPLCOM_SET_REQUEST;
514         USETW(req.wValue, 0);
515         USETW(req.wIndex, sc->sc_iface_number);
516         USETW(req.wLength, 0);
517 
518         err = usbd_do_request(sc->sc_udev, &req, 0);
519 	if (err)
520 		return (EIO);
521 
522 	return (0);
523 }
524 
525 struct pl2303x_init {
526 	uint8_t		req_type;
527 	uint8_t		request;
528 	uint16_t	value;
529 	uint16_t	index;
530 	uint16_t	length;
531 };
532 
533 static const struct pl2303x_init pl2303x[] = {
534 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
535 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404,    0, 0 },
536 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
537 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8383,    0, 0 },
538 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
539 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404,    1, 0 },
540 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
541 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8383,    0, 0 },
542 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      0,    1, 0 },
543 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      1,    0, 0 },
544 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      2, 0x44, 0 }
545 };
546 #define N_PL2302X_INIT  (sizeof(pl2303x)/sizeof(pl2303x[0]))
547 
548 static usbd_status
549 uplcom_pl2303x_init(struct uplcom_softc *sc)
550 {
551 	usb_device_request_t req;
552 	usbd_status err;
553 	int i;
554 
555 	for (i = 0; i < N_PL2302X_INIT; i++) {
556 		req.bmRequestType = pl2303x[i].req_type;
557 		req.bRequest = pl2303x[i].request;
558 		USETW(req.wValue, pl2303x[i].value);
559 		USETW(req.wIndex, pl2303x[i].index);
560 		USETW(req.wLength, pl2303x[i].length);
561 
562 		err = usbd_do_request(sc->sc_udev, &req, 0);
563 		if (err) {
564 			printf("%s: uplcom_pl2303x_init failed: %s\n",
565 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
566 			return (EIO);
567 		}
568 	}
569 
570 	return (0);
571 }
572 
573 void
574 uplcom_set_line_state(struct uplcom_softc *sc)
575 {
576 	usb_device_request_t req;
577 	int ls;
578 
579 	/* make sure we have initialized state for sc_dtr and sc_rts */
580 	if (sc->sc_dtr == -1)
581 		sc->sc_dtr = 0;
582 	if (sc->sc_rts == -1)
583 		sc->sc_rts = 0;
584 
585 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
586 		(sc->sc_rts ? UCDC_LINE_RTS : 0);
587 
588 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
589 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
590 	USETW(req.wValue, ls);
591 	USETW(req.wIndex, sc->sc_iface_number);
592 	USETW(req.wLength, 0);
593 
594 	(void)usbd_do_request(sc->sc_udev, &req, 0);
595 }
596 
597 void
598 uplcom_set(void *addr, int portno, int reg, int onoff)
599 {
600 	struct uplcom_softc *sc = addr;
601 
602 	switch (reg) {
603 	case UCOM_SET_DTR:
604 		uplcom_dtr(sc, onoff);
605 		break;
606 	case UCOM_SET_RTS:
607 		uplcom_rts(sc, onoff);
608 		break;
609 	case UCOM_SET_BREAK:
610 		uplcom_break(sc, onoff);
611 		break;
612 	default:
613 		break;
614 	}
615 }
616 
617 void
618 uplcom_dtr(struct uplcom_softc *sc, int onoff)
619 {
620 
621 	DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
622 
623 	if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
624 		return;
625 
626 	sc->sc_dtr = !!onoff;
627 
628 	uplcom_set_line_state(sc);
629 }
630 
631 void
632 uplcom_rts(struct uplcom_softc *sc, int onoff)
633 {
634 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
635 
636 	if (sc->sc_rts != -1 && !sc->sc_rts == !onoff)
637 		return;
638 
639 	sc->sc_rts = !!onoff;
640 
641 	uplcom_set_line_state(sc);
642 }
643 
644 void
645 uplcom_break(struct uplcom_softc *sc, int onoff)
646 {
647 	usb_device_request_t req;
648 
649 	DPRINTF(("uplcom_break: onoff=%d\n", onoff));
650 
651 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
652 	req.bRequest = UCDC_SEND_BREAK;
653 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
654 	USETW(req.wIndex, sc->sc_iface_number);
655 	USETW(req.wLength, 0);
656 
657 	(void)usbd_do_request(sc->sc_udev, &req, 0);
658 }
659 
660 usbd_status
661 uplcom_set_crtscts(struct uplcom_softc *sc)
662 {
663 	usb_device_request_t req;
664 	usbd_status err;
665 
666 	DPRINTF(("uplcom_set_crtscts: on\n"));
667 
668 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
669 	req.bRequest = UPLCOM_SET_REQUEST;
670 	USETW(req.wValue, 0);
671 	if (sc->sc_type == UPLCOM_TYPE_HX)
672 		USETW(req.wIndex, UPLCOM_SET_CRTSCTS_HX);
673 	else
674 		USETW(req.wIndex, UPLCOM_SET_CRTSCTS_0);
675 	USETW(req.wLength, 0);
676 
677 	err = usbd_do_request(sc->sc_udev, &req, 0);
678 	if (err) {
679 		DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
680 			usbd_errstr(err)));
681 		return (err);
682 	}
683 
684 	return (USBD_NORMAL_COMPLETION);
685 }
686 
687 usbd_status
688 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
689 {
690 	usb_device_request_t req;
691 	usbd_status err;
692 
693 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
694 		UGETDW(state->dwDTERate), state->bCharFormat,
695 		state->bParityType, state->bDataBits));
696 
697 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
698 		DPRINTF(("uplcom_set_line_coding: already set\n"));
699 		return (USBD_NORMAL_COMPLETION);
700 	}
701 
702 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
703 	req.bRequest = UCDC_SET_LINE_CODING;
704 	USETW(req.wValue, 0);
705 	USETW(req.wIndex, sc->sc_iface_number);
706 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
707 
708 	err = usbd_do_request(sc->sc_udev, &req, state);
709 	if (err) {
710 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
711 			usbd_errstr(err)));
712 		return (err);
713 	}
714 
715 	sc->sc_line_state = *state;
716 
717 	return (USBD_NORMAL_COMPLETION);
718 }
719 
720 int
721 uplcom_param(void *addr, int portno, struct termios *t)
722 {
723 	struct uplcom_softc *sc = addr;
724 	usbd_status err;
725 	usb_cdc_line_state_t ls;
726 
727 	DPRINTF(("uplcom_param: sc=%p\n", sc));
728 
729 	USETDW(ls.dwDTERate, t->c_ospeed);
730 	if (ISSET(t->c_cflag, CSTOPB))
731 		ls.bCharFormat = UCDC_STOP_BIT_2;
732 	else
733 		ls.bCharFormat = UCDC_STOP_BIT_1;
734 	if (ISSET(t->c_cflag, PARENB)) {
735 		if (ISSET(t->c_cflag, PARODD))
736 			ls.bParityType = UCDC_PARITY_ODD;
737 		else
738 			ls.bParityType = UCDC_PARITY_EVEN;
739 	} else
740 		ls.bParityType = UCDC_PARITY_NONE;
741 	switch (ISSET(t->c_cflag, CSIZE)) {
742 	case CS5:
743 		ls.bDataBits = 5;
744 		break;
745 	case CS6:
746 		ls.bDataBits = 6;
747 		break;
748 	case CS7:
749 		ls.bDataBits = 7;
750 		break;
751 	case CS8:
752 		ls.bDataBits = 8;
753 		break;
754 	}
755 
756 	err = uplcom_set_line_coding(sc, &ls);
757 	if (err) {
758 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
759 		return (EIO);
760 	}
761 
762 	if (ISSET(t->c_cflag, CRTSCTS))
763 		uplcom_set_crtscts(sc);
764 
765 	if (sc->sc_rts == -1 || sc->sc_dtr == -1)
766 		uplcom_set_line_state(sc);
767 
768 	if (err) {
769 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
770 		return (EIO);
771 	}
772 
773 	return (0);
774 }
775 
776 Static usbd_status
777 uplcom_vendor_control_write(usbd_device_handle dev, u_int16_t value, u_int16_t index)
778 {
779 	usb_device_request_t req;
780 	usbd_status err;
781 
782 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
783 	req.bRequest = UPLCOM_SET_REQUEST;
784 	USETW(req.wValue, value);
785 	USETW(req.wIndex, index);
786 	USETW(req.wLength, 0);
787 
788 	err = usbd_do_request(dev, &req, NULL);
789 
790 	if (err) {
791 		DPRINTF(("uplcom_open: vendor write failed, err=%s (%d)\n",
792 			    usbd_errstr(err), err));
793 	}
794 
795 	return err;
796 }
797 
798 int
799 uplcom_open(void *addr, int portno)
800 {
801 	struct uplcom_softc *sc = addr;
802 	usbd_status err;
803 
804 	if (sc->sc_dying)
805 		return (EIO);
806 
807 	DPRINTF(("uplcom_open: sc=%p\n", sc));
808 
809 	/* Some unknown device frobbing. */
810 	if (sc->sc_type == UPLCOM_TYPE_HX)
811 		uplcom_vendor_control_write(sc->sc_udev, 2, 0x44);
812 	else
813 		uplcom_vendor_control_write(sc->sc_udev, 2, 0x24);
814 
815 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
816 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
817 		err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
818 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
819 			sc->sc_intr_buf, sc->sc_isize,
820 			uplcom_intr, USBD_DEFAULT_INTERVAL);
821 		if (err) {
822 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
823 				USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
824 					return (EIO);
825 		}
826 	}
827 
828 	if (sc->sc_type == UPLCOM_TYPE_HX)
829 		return (uplcom_pl2303x_init(sc));
830 
831 	return (0);
832 }
833 
834 void
835 uplcom_close(void *addr, int portno)
836 {
837 	struct uplcom_softc *sc = addr;
838 	int err;
839 
840 	if (sc->sc_dying)
841 		return;
842 
843 	DPRINTF(("uplcom_close: close\n"));
844 
845 	if (sc->sc_intr_pipe != NULL) {
846 		err = usbd_abort_pipe(sc->sc_intr_pipe);
847 		if (err)
848 			printf("%s: abort interrupt pipe failed: %s\n",
849 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
850 		err = usbd_close_pipe(sc->sc_intr_pipe);
851 		if (err)
852 			printf("%s: close interrupt pipe failed: %s\n",
853 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
854 		free(sc->sc_intr_buf, M_USBDEV);
855 		sc->sc_intr_pipe = NULL;
856 	}
857 }
858 
859 void
860 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
861     usbd_status status)
862 {
863 	struct uplcom_softc *sc = priv;
864 	u_char *buf = sc->sc_intr_buf;
865 	u_char pstatus;
866 
867 	if (sc->sc_dying)
868 		return;
869 
870 	if (status != USBD_NORMAL_COMPLETION) {
871 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
872 			return;
873 
874 		DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
875 			usbd_errstr(status)));
876 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
877 		return;
878 	}
879 
880 	DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc->sc_dev), buf[8]));
881 
882 	sc->sc_lsr = sc->sc_msr = 0;
883 	pstatus = buf[8];
884 	if (ISSET(pstatus, RSAQ_STATUS_DSR))
885 		sc->sc_msr |= UMSR_DSR;
886 	if (ISSET(pstatus, RSAQ_STATUS_DCD))
887 		sc->sc_msr |= UMSR_DCD;
888 	ucom_status_change((struct ucom_softc *) sc->sc_subdev);
889 }
890 
891 void
892 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
893 {
894 	struct uplcom_softc *sc = addr;
895 
896 	DPRINTF(("uplcom_get_status:\n"));
897 
898 	if (lsr != NULL)
899 		*lsr = sc->sc_lsr;
900 	if (msr != NULL)
901 		*msr = sc->sc_msr;
902 }
903 
904 #if TODO
905 int
906 uplcom_ioctl(void *addr, int portno, u_long cmd, void *data, int flag,
907 	     usb_proc_ptr p)
908 {
909 	struct uplcom_softc *sc = addr;
910 	int error = 0;
911 
912 	if (sc->sc_dying)
913 		return (EIO);
914 
915 	DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
916 
917 	switch (cmd) {
918 	case TIOCNOTTY:
919 	case TIOCMGET:
920 	case TIOCMSET:
921 	case USB_GET_CM_OVER_DATA:
922 	case USB_SET_CM_OVER_DATA:
923 		break;
924 
925 	default:
926 		DPRINTF(("uplcom_ioctl: unknown\n"));
927 		error = ENOTTY;
928 		break;
929 	}
930 
931 	return (error);
932 }
933 #endif
934