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