xref: /netbsd/sys/dev/usb/uplcom.c (revision 6550d01e)
1 /*	$NetBSD: uplcom.c,v 1.71 2010/11/03 22:34:24 dyoung 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.71 2010/11/03 22:34:24 dyoung 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)) printf 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 
78 #define	UPLCOM_N_SERIAL_CTS	0x80
79 
80 enum  pl2303_type {
81 	UPLCOM_TYPE_0,	/* we use this for all non-HX variants */
82 	UPLCOM_TYPE_HX,
83 };
84 
85 struct	uplcom_softc {
86 	device_t		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_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, proc_t *);
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 usb_devno uplcom_devs[] = {
150 	/* I/O DATA USB-RSAQ2 */
151 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
152 	/* I/O DATA USB-RSAQ3 */
153 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3 },
154 	/* I/O DATA USB-RSAQ */
155 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
156 	/* I/O DATA USB-RSAQ5 */
157 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ5 },
158 	/* PLANEX USB-RS232 URS-03 */
159 	{ USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
160 	/* various */
161 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
162 	/* SMART Technologies USB to serial */
163 	{ USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_PL2303 },
164 	/* IOGEAR/ATENTRIPPLITE */
165 	{ USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
166 	/* ELECOM UC-SGT */
167 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
168 	/* ELECOM UC-SGT0 */
169 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 },
170 	/* Panasonic 50" Touch Panel */
171 	{ USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_TYTP50P6S },
172 	/* RATOC REX-USB60 */
173 	{ USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
174 	/* TDK USB-PHS Adapter UHA6400 */
175 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
176 	/* TDK USB-PDC Adapter UPA9664 */
177 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 },
178 	/* Sony Ericsson USB Cable */
179 	{ USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU10 },
180 	/* SOURCENEXT KeikaiDenwa 8 */
181 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 },
182 	/* SOURCENEXT KeikaiDenwa 8 with charger */
183 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG },
184 	/* HAL Corporation Crossam2+USB */
185 	{ USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 },
186 	/* Sitecom USB to serial cable */
187 	{ USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 },
188 	/* Pharos USB GPS - Microsoft version */
189 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X },
190 	/* Willcom WS002IN (DD) */
191 	{ USB_VENDOR_NETINDEX, USB_PRODUCT_NETINDEX_WS002IN },
192 	/* COREGA CG-USBRS232R */
193 	{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGUSBRS232R },
194 };
195 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
196 
197 int uplcom_match(device_t, cfdata_t, void *);
198 void uplcom_attach(device_t, device_t, void *);
199 void uplcom_childdet(device_t, device_t);
200 int uplcom_detach(device_t, int);
201 int uplcom_activate(device_t, enum devact);
202 extern struct cfdriver uplcom_cd;
203 CFATTACH_DECL2_NEW(uplcom, sizeof(struct uplcom_softc), uplcom_match,
204     uplcom_attach, uplcom_detach, uplcom_activate, NULL, uplcom_childdet);
205 
206 int
207 uplcom_match(device_t parent, cfdata_t match, void *aux)
208 {
209 	struct usb_attach_arg *uaa = aux;
210 
211 	return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ?
212 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
213 }
214 
215 void
216 uplcom_attach(device_t parent, device_t self, void *aux)
217 {
218 	struct uplcom_softc *sc = device_private(self);
219 	struct usb_attach_arg *uaa = aux;
220 	usbd_device_handle dev = uaa->device;
221 	usb_device_descriptor_t *ddesc;
222 	usb_config_descriptor_t *cdesc;
223 	usb_interface_descriptor_t *id;
224 	usb_endpoint_descriptor_t *ed;
225 	char *devinfop;
226 	const char *devname = device_xname(self);
227 	usbd_status err;
228 	int i;
229 	struct ucom_attach_args uca;
230 
231 	sc->sc_dev = self;
232 
233 	aprint_naive("\n");
234 	aprint_normal("\n");
235 
236 	devinfop = usbd_devinfo_alloc(dev, 0);
237 	aprint_normal_dev(self, "%s\n", devinfop);
238 	usbd_devinfo_free(devinfop);
239 
240         sc->sc_udev = dev;
241 
242 	DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
243 
244 	/* initialize endpoints */
245 	uca.bulkin = uca.bulkout = -1;
246 	sc->sc_intr_number = -1;
247 	sc->sc_intr_pipe = NULL;
248 
249 	/* Move the device into the configured state. */
250 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
251 	if (err) {
252 		aprint_error("\n%s: failed to set configuration, err=%s\n",
253 			devname, usbd_errstr(err));
254 		sc->sc_dying = 1;
255 		return;
256 	}
257 
258 	/* determine chip type */
259 	ddesc = usbd_get_device_descriptor(dev);
260 	if (ddesc->bDeviceClass != UDCLASS_COMM &&
261 	    ddesc->bMaxPacketSize == 0x40)
262 		sc->sc_type = UPLCOM_TYPE_HX;
263 
264 #ifdef UPLCOM_DEBUG
265 	/* print the chip type */
266 	if (sc->sc_type == UPLCOM_TYPE_HX) {
267 		DPRINTF(("uplcom_attach: chiptype HX\n"));
268 	} else {
269 		DPRINTF(("uplcom_attach: chiptype 0\n"));
270 	}
271 #endif
272 
273 	/* Move the device into the configured state. */
274 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
275 	if (err) {
276 		aprint_error_dev(self, "failed to set configuration: %s\n",
277 		    usbd_errstr(err));
278 		sc->sc_dying = 1;
279 		return;
280 	}
281 
282 	/* get the config descriptor */
283 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
284 
285 	if (cdesc == NULL) {
286 		aprint_error_dev(self,
287 		    "failed to get configuration descriptor\n");
288 		sc->sc_dying = 1;
289 		return;
290 	}
291 
292 	/* get the (first/common) interface */
293 	err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
294 							&sc->sc_iface);
295 	if (err) {
296 		aprint_error("\n%s: failed to get interface, err=%s\n",
297 			devname, usbd_errstr(err));
298 		sc->sc_dying = 1;
299 		return;
300 	}
301 
302 	/* Find the interrupt endpoints */
303 
304 	id = usbd_get_interface_descriptor(sc->sc_iface);
305 	sc->sc_iface_number = id->bInterfaceNumber;
306 
307 	for (i = 0; i < id->bNumEndpoints; i++) {
308 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
309 		if (ed == NULL) {
310 			aprint_error_dev(self,
311 			    "no endpoint descriptor for %d\n", i);
312 			sc->sc_dying = 1;
313 			return;
314 		}
315 
316 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
317 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
318 			sc->sc_intr_number = ed->bEndpointAddress;
319 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
320 		}
321 	}
322 
323 	if (sc->sc_intr_number== -1) {
324 		aprint_error_dev(self, "Could not find interrupt in\n");
325 		sc->sc_dying = 1;
326 		return;
327 	}
328 
329 	/* keep interface for interrupt */
330 	sc->sc_intr_iface = sc->sc_iface;
331 
332 	/*
333 	 * USB-RSAQ1 has two interface
334 	 *
335 	 *  USB-RSAQ1       | USB-RSAQ2
336  	 * -----------------+-----------------
337 	 * Interface 0      |Interface 0
338 	 *  Interrupt(0x81) | Interrupt(0x81)
339 	 * -----------------+ BulkIN(0x02)
340 	 * Interface 1	    | BulkOUT(0x83)
341 	 *   BulkIN(0x02)   |
342 	 *   BulkOUT(0x83)  |
343 	 */
344 	if (cdesc->bNumInterface == 2) {
345 		err = usbd_device2interface_handle(dev,
346 				UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
347 		if (err) {
348 			aprint_error("\n%s: failed to get second interface, err=%s\n",
349 							devname, usbd_errstr(err));
350 			sc->sc_dying = 1;
351 			return;
352 		}
353 	}
354 
355 	/* Find the bulk{in,out} endpoints */
356 
357 	id = usbd_get_interface_descriptor(sc->sc_iface);
358 	sc->sc_iface_number = id->bInterfaceNumber;
359 
360 	for (i = 0; i < id->bNumEndpoints; i++) {
361 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
362 		if (ed == NULL) {
363 			aprint_error_dev(self,
364 			    "no endpoint descriptor for %d\n", i);
365 			sc->sc_dying = 1;
366 			return;
367 		}
368 
369 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
370 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
371 			uca.bulkin = ed->bEndpointAddress;
372 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
373 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
374 			uca.bulkout = ed->bEndpointAddress;
375 		}
376 	}
377 
378 	if (uca.bulkin == -1) {
379 		aprint_error_dev(self, "Could not find data bulk in\n");
380 		sc->sc_dying = 1;
381 		return;
382 	}
383 
384 	if (uca.bulkout == -1) {
385 		aprint_error_dev(self, "Could not find data bulk out\n");
386 		sc->sc_dying = 1;
387 		return;
388 	}
389 
390 	sc->sc_dtr = sc->sc_rts = -1;
391 	uca.portno = UCOM_UNK_PORTNO;
392 	/* bulkin, bulkout set above */
393 	uca.ibufsize = UPLCOMIBUFSIZE;
394 	uca.obufsize = UPLCOMOBUFSIZE;
395 	uca.ibufsizepad = UPLCOMIBUFSIZE;
396 	uca.opkthdrlen = 0;
397 	uca.device = dev;
398 	uca.iface = sc->sc_iface;
399 	uca.methods = &uplcom_methods;
400 	uca.arg = sc;
401 	uca.info = NULL;
402 
403 	err = uplcom_reset(sc);
404 
405 	if (err) {
406 		aprint_error_dev(self, "reset failed, %s\n", usbd_errstr(err));
407 		sc->sc_dying = 1;
408 		return;
409 	}
410 
411 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
412 			   sc->sc_dev);
413 
414 	DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
415 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
416 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
417 					    ucomprint, ucomsubmatch);
418 
419 	return;
420 }
421 
422 void
423 uplcom_childdet(device_t self, device_t child)
424 {
425 	struct uplcom_softc *sc = device_private(self);
426 
427 	KASSERT(sc->sc_subdev == child);
428 	sc->sc_subdev = NULL;
429 }
430 
431 int
432 uplcom_detach(device_t self, int flags)
433 {
434 	struct uplcom_softc *sc = device_private(self);
435 	int rv = 0;
436 
437 	DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
438 
439         if (sc->sc_intr_pipe != NULL) {
440                 usbd_abort_pipe(sc->sc_intr_pipe);
441                 usbd_close_pipe(sc->sc_intr_pipe);
442 		free(sc->sc_intr_buf, M_USBDEV);
443                 sc->sc_intr_pipe = NULL;
444         }
445 
446 	sc->sc_dying = 1;
447 	if (sc->sc_subdev != NULL)
448 		rv = config_detach(sc->sc_subdev, flags);
449 
450 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
451 			   sc->sc_dev);
452 
453 	return (rv);
454 }
455 
456 int
457 uplcom_activate(device_t self, enum devact act)
458 {
459 	struct uplcom_softc *sc = device_private(self);
460 
461 	switch (act) {
462 	case DVACT_DEACTIVATE:
463 		sc->sc_dying = 1;
464 		return 0;
465 	default:
466 		return EOPNOTSUPP;
467 	}
468 }
469 
470 usbd_status
471 uplcom_reset(struct uplcom_softc *sc)
472 {
473 	usb_device_request_t req;
474 	usbd_status err;
475 
476         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
477         req.bRequest = UPLCOM_SET_REQUEST;
478         USETW(req.wValue, 0);
479         USETW(req.wIndex, sc->sc_iface_number);
480         USETW(req.wLength, 0);
481 
482         err = usbd_do_request(sc->sc_udev, &req, 0);
483 	if (err)
484 		return (EIO);
485 
486 	return (0);
487 }
488 
489 struct pl2303x_init {
490 	uint8_t		req_type;
491 	uint8_t		request;
492 	uint16_t	value;
493 	uint16_t	index;
494 	uint16_t	length;
495 };
496 
497 static const struct pl2303x_init pl2303x[] = {
498 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
499 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404,    0, 0 },
500 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
501 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8383,    0, 0 },
502 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
503 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404,    1, 0 },
504 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
505 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8383,    0, 0 },
506 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      0,    1, 0 },
507 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      1,    0, 0 },
508 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      2, 0x44, 0 }
509 };
510 #define N_PL2302X_INIT  (sizeof(pl2303x)/sizeof(pl2303x[0]))
511 
512 static usbd_status
513 uplcom_pl2303x_init(struct uplcom_softc *sc)
514 {
515 	usb_device_request_t req;
516 	usbd_status err;
517 	int i;
518 
519 	for (i = 0; i < N_PL2302X_INIT; i++) {
520 		req.bmRequestType = pl2303x[i].req_type;
521 		req.bRequest = pl2303x[i].request;
522 		USETW(req.wValue, pl2303x[i].value);
523 		USETW(req.wIndex, pl2303x[i].index);
524 		USETW(req.wLength, pl2303x[i].length);
525 
526 		err = usbd_do_request(sc->sc_udev, &req, 0);
527 		if (err) {
528 			aprint_error_dev(sc->sc_dev,
529 			    "uplcom_pl2303x_init failed: %s\n",
530 			    usbd_errstr(err));
531 			return (EIO);
532 		}
533 	}
534 
535 	return (0);
536 }
537 
538 void
539 uplcom_set_line_state(struct uplcom_softc *sc)
540 {
541 	usb_device_request_t req;
542 	int ls;
543 
544 	/* make sure we have initialized state for sc_dtr and sc_rts */
545 	if (sc->sc_dtr == -1)
546 		sc->sc_dtr = 0;
547 	if (sc->sc_rts == -1)
548 		sc->sc_rts = 0;
549 
550 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
551 		(sc->sc_rts ? UCDC_LINE_RTS : 0);
552 
553 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
554 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
555 	USETW(req.wValue, ls);
556 	USETW(req.wIndex, sc->sc_iface_number);
557 	USETW(req.wLength, 0);
558 
559 	(void)usbd_do_request(sc->sc_udev, &req, 0);
560 }
561 
562 void
563 uplcom_set(void *addr, int portno, int reg, int onoff)
564 {
565 	struct uplcom_softc *sc = addr;
566 
567 	switch (reg) {
568 	case UCOM_SET_DTR:
569 		uplcom_dtr(sc, onoff);
570 		break;
571 	case UCOM_SET_RTS:
572 		uplcom_rts(sc, onoff);
573 		break;
574 	case UCOM_SET_BREAK:
575 		uplcom_break(sc, onoff);
576 		break;
577 	default:
578 		break;
579 	}
580 }
581 
582 void
583 uplcom_dtr(struct uplcom_softc *sc, int onoff)
584 {
585 
586 	DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
587 
588 	if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
589 		return;
590 
591 	sc->sc_dtr = !!onoff;
592 
593 	uplcom_set_line_state(sc);
594 }
595 
596 void
597 uplcom_rts(struct uplcom_softc *sc, int onoff)
598 {
599 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
600 
601 	if (sc->sc_rts != -1 && !sc->sc_rts == !onoff)
602 		return;
603 
604 	sc->sc_rts = !!onoff;
605 
606 	uplcom_set_line_state(sc);
607 }
608 
609 void
610 uplcom_break(struct uplcom_softc *sc, int onoff)
611 {
612 	usb_device_request_t req;
613 
614 	DPRINTF(("uplcom_break: onoff=%d\n", onoff));
615 
616 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
617 	req.bRequest = UCDC_SEND_BREAK;
618 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
619 	USETW(req.wIndex, sc->sc_iface_number);
620 	USETW(req.wLength, 0);
621 
622 	(void)usbd_do_request(sc->sc_udev, &req, 0);
623 }
624 
625 usbd_status
626 uplcom_set_crtscts(struct uplcom_softc *sc)
627 {
628 	usb_device_request_t req;
629 	usbd_status err;
630 
631 	DPRINTF(("uplcom_set_crtscts: on\n"));
632 
633 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
634 	req.bRequest = UPLCOM_SET_REQUEST;
635 	USETW(req.wValue, 0);
636 	if (sc->sc_type == UPLCOM_TYPE_HX)
637 		USETW(req.wIndex, UPLCOM_SET_CRTSCTS_HX);
638 	else
639 		USETW(req.wIndex, UPLCOM_SET_CRTSCTS_0);
640 	USETW(req.wLength, 0);
641 
642 	err = usbd_do_request(sc->sc_udev, &req, 0);
643 	if (err) {
644 		DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
645 			usbd_errstr(err)));
646 		return (err);
647 	}
648 
649 	return (USBD_NORMAL_COMPLETION);
650 }
651 
652 usbd_status
653 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
654 {
655 	usb_device_request_t req;
656 	usbd_status err;
657 
658 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
659 		UGETDW(state->dwDTERate), state->bCharFormat,
660 		state->bParityType, state->bDataBits));
661 
662 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
663 		DPRINTF(("uplcom_set_line_coding: already set\n"));
664 		return (USBD_NORMAL_COMPLETION);
665 	}
666 
667 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
668 	req.bRequest = UCDC_SET_LINE_CODING;
669 	USETW(req.wValue, 0);
670 	USETW(req.wIndex, sc->sc_iface_number);
671 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
672 
673 	err = usbd_do_request(sc->sc_udev, &req, state);
674 	if (err) {
675 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
676 			usbd_errstr(err)));
677 		return (err);
678 	}
679 
680 	sc->sc_line_state = *state;
681 
682 	return (USBD_NORMAL_COMPLETION);
683 }
684 
685 int
686 uplcom_param(void *addr, int portno, struct termios *t)
687 {
688 	struct uplcom_softc *sc = addr;
689 	usbd_status err;
690 	usb_cdc_line_state_t ls;
691 
692 	DPRINTF(("uplcom_param: sc=%p\n", sc));
693 
694 	USETDW(ls.dwDTERate, t->c_ospeed);
695 	if (ISSET(t->c_cflag, CSTOPB))
696 		ls.bCharFormat = UCDC_STOP_BIT_2;
697 	else
698 		ls.bCharFormat = UCDC_STOP_BIT_1;
699 	if (ISSET(t->c_cflag, PARENB)) {
700 		if (ISSET(t->c_cflag, PARODD))
701 			ls.bParityType = UCDC_PARITY_ODD;
702 		else
703 			ls.bParityType = UCDC_PARITY_EVEN;
704 	} else
705 		ls.bParityType = UCDC_PARITY_NONE;
706 	switch (ISSET(t->c_cflag, CSIZE)) {
707 	case CS5:
708 		ls.bDataBits = 5;
709 		break;
710 	case CS6:
711 		ls.bDataBits = 6;
712 		break;
713 	case CS7:
714 		ls.bDataBits = 7;
715 		break;
716 	case CS8:
717 		ls.bDataBits = 8;
718 		break;
719 	}
720 
721 	err = uplcom_set_line_coding(sc, &ls);
722 	if (err) {
723 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
724 		return (EIO);
725 	}
726 
727 	if (ISSET(t->c_cflag, CRTSCTS))
728 		uplcom_set_crtscts(sc);
729 
730 	if (sc->sc_rts == -1 || sc->sc_dtr == -1)
731 		uplcom_set_line_state(sc);
732 
733 	if (err) {
734 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
735 		return (EIO);
736 	}
737 
738 	return (0);
739 }
740 
741 Static usbd_status
742 uplcom_vendor_control_write(usbd_device_handle dev, u_int16_t value, u_int16_t index)
743 {
744 	usb_device_request_t req;
745 	usbd_status err;
746 
747 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
748 	req.bRequest = UPLCOM_SET_REQUEST;
749 	USETW(req.wValue, value);
750 	USETW(req.wIndex, index);
751 	USETW(req.wLength, 0);
752 
753 	err = usbd_do_request(dev, &req, NULL);
754 
755 	if (err) {
756 		DPRINTF(("uplcom_open: vendor write failed, err=%s (%d)\n",
757 			    usbd_errstr(err), err));
758 	}
759 
760 	return err;
761 }
762 
763 int
764 uplcom_open(void *addr, int portno)
765 {
766 	struct uplcom_softc *sc = addr;
767 	usbd_status err;
768 
769 	if (sc->sc_dying)
770 		return (EIO);
771 
772 	DPRINTF(("uplcom_open: sc=%p\n", sc));
773 
774 	/* Some unknown device frobbing. */
775 	if (sc->sc_type == UPLCOM_TYPE_HX)
776 		uplcom_vendor_control_write(sc->sc_udev, 2, 0x44);
777 	else
778 		uplcom_vendor_control_write(sc->sc_udev, 2, 0x24);
779 
780 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
781 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
782 		err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
783 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
784 			sc->sc_intr_buf, sc->sc_isize,
785 			uplcom_intr, USBD_DEFAULT_INTERVAL);
786 		if (err) {
787 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
788 				device_xname(sc->sc_dev), sc->sc_intr_number));
789 					return (EIO);
790 		}
791 	}
792 
793 	if (sc->sc_type == UPLCOM_TYPE_HX)
794 		return (uplcom_pl2303x_init(sc));
795 
796 	return (0);
797 }
798 
799 void
800 uplcom_close(void *addr, int portno)
801 {
802 	struct uplcom_softc *sc = addr;
803 	int err;
804 
805 	if (sc->sc_dying)
806 		return;
807 
808 	DPRINTF(("uplcom_close: close\n"));
809 
810 	if (sc->sc_intr_pipe != NULL) {
811 		err = usbd_abort_pipe(sc->sc_intr_pipe);
812 		if (err)
813 			printf("%s: abort interrupt pipe failed: %s\n",
814 				device_xname(sc->sc_dev), usbd_errstr(err));
815 		err = usbd_close_pipe(sc->sc_intr_pipe);
816 		if (err)
817 			printf("%s: close interrupt pipe failed: %s\n",
818 				device_xname(sc->sc_dev), usbd_errstr(err));
819 		free(sc->sc_intr_buf, M_USBDEV);
820 		sc->sc_intr_pipe = NULL;
821 	}
822 }
823 
824 void
825 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
826     usbd_status status)
827 {
828 	struct uplcom_softc *sc = priv;
829 	u_char *buf = sc->sc_intr_buf;
830 	u_char pstatus;
831 
832 	if (sc->sc_dying)
833 		return;
834 
835 	if (status != USBD_NORMAL_COMPLETION) {
836 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
837 			return;
838 
839 		DPRINTF(("%s: abnormal status: %s\n", device_xname(sc->sc_dev),
840 			usbd_errstr(status)));
841 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
842 		return;
843 	}
844 
845 	DPRINTF(("%s: uplcom status = %02x\n", device_xname(sc->sc_dev), buf[8]));
846 
847 	sc->sc_lsr = sc->sc_msr = 0;
848 	pstatus = buf[8];
849 	if (ISSET(pstatus, UPLCOM_N_SERIAL_CTS))
850 		sc->sc_msr |= UMSR_CTS;
851 	if (ISSET(pstatus, UCDC_N_SERIAL_RI))
852 		sc->sc_msr |= UMSR_RI;
853 	if (ISSET(pstatus, UCDC_N_SERIAL_DSR))
854 		sc->sc_msr |= UMSR_DSR;
855 	if (ISSET(pstatus, UCDC_N_SERIAL_DCD))
856 		sc->sc_msr |= UMSR_DCD;
857 	ucom_status_change(device_private(sc->sc_subdev));
858 }
859 
860 void
861 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
862 {
863 	struct uplcom_softc *sc = addr;
864 
865 	DPRINTF(("uplcom_get_status:\n"));
866 
867 	if (lsr != NULL)
868 		*lsr = sc->sc_lsr;
869 	if (msr != NULL)
870 		*msr = sc->sc_msr;
871 }
872 
873 #if TODO
874 int
875 uplcom_ioctl(void *addr, int portno, u_long cmd, void *data, int flag,
876 	     proc_t *p)
877 {
878 	struct uplcom_softc *sc = addr;
879 	int error = 0;
880 
881 	if (sc->sc_dying)
882 		return (EIO);
883 
884 	DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
885 
886 	switch (cmd) {
887 	case TIOCNOTTY:
888 	case TIOCMGET:
889 	case TIOCMSET:
890 	case USB_GET_CM_OVER_DATA:
891 	case USB_SET_CM_OVER_DATA:
892 		break;
893 
894 	default:
895 		DPRINTF(("uplcom_ioctl: unknown\n"));
896 		error = ENOTTY;
897 		break;
898 	}
899 
900 	return (error);
901 }
902 #endif
903