xref: /openbsd/sys/dev/usb/uplcom.c (revision 9b7c3dbb)
1 /*	$OpenBSD: uplcom.c,v 1.67 2016/05/24 05:35:01 mpi Exp $	*/
2 /*	$NetBSD: uplcom.c,v 1.29 2002/09/23 05:51:23 simonb Exp $	*/
3 /*
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Ichiro FUKUHARA (ichiro@ichiro.org).
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Simple datasheet
34  * http://www.prolific.com.tw/PDF/PL-2303%20Market%20Spec.pdf
35  * http://www.hitachi-hitec.com/jyouhou/prolific/2303.pdf
36  * 	(english)
37  *
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/ioctl.h>
45 #include <sys/conf.h>
46 #include <sys/tty.h>
47 #include <sys/file.h>
48 #include <sys/selinfo.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 
59 #include <dev/usb/ucomvar.h>
60 
61 #ifdef UPLCOM_DEBUG
62 #define DPRINTFN(n, x)  do { if (uplcomdebug > (n)) printf x; } while (0)
63 int	uplcomdebug = 0;
64 #else
65 #define DPRINTFN(n, x)
66 #endif
67 #define DPRINTF(x) DPRINTFN(0, x)
68 
69 #define	UPLCOM_CONFIG_INDEX	0
70 #define	UPLCOM_IFACE_INDEX	0
71 #define	UPLCOM_SECOND_IFACE_INDEX	1
72 
73 #define	UPLCOM_SET_REQUEST	0x01
74 #define	UPLCOM_SET_CRTSCTS	0x41
75 #define	UPLCOM_HX_SET_CRTSCTS	0x61
76 #define RSAQ_STATUS_CTS		0x80
77 #define RSAQ_STATUS_DSR		0x02
78 #define RSAQ_STATUS_DCD		0x01
79 
80 struct	uplcom_softc {
81 	struct device		 sc_dev;	/* base device */
82 	struct usbd_device	*sc_udev;	/* USB device */
83 	struct usbd_interface	*sc_iface;	/* interface */
84 	int			 sc_iface_number;	/* interface number */
85 
86 	struct usbd_interface	*sc_intr_iface;	/* interrupt interface */
87 	int			 sc_intr_number;	/* interrupt number */
88 	struct usbd_pipe	*sc_intr_pipe;	/* interrupt pipe */
89 	u_char			*sc_intr_buf;	/* interrupt buffer */
90 	int			 sc_isize;
91 
92 	struct usb_cdc_line_state sc_line_state;/* current line state */
93 	int			 sc_dtr;	/* current DTR state */
94 	int			 sc_rts;	/* current RTS state */
95 
96 	struct device		*sc_subdev;	/* ucom device */
97 
98 	u_char			 sc_lsr;	/* Local status register */
99 	u_char			 sc_msr;	/* uplcom status register */
100 	int			 sc_type_hx;	/* HX variant */
101 };
102 
103 /*
104  * These are the maximum number of bytes transferred per frame.
105  * The output buffer size cannot be increased due to the size encoding.
106  */
107 #define UPLCOMIBUFSIZE 256
108 #define UPLCOMOBUFSIZE 256
109 
110 usbd_status uplcom_reset(struct uplcom_softc *);
111 usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
112     struct usb_cdc_line_state *state);
113 usbd_status uplcom_set_crtscts(struct uplcom_softc *);
114 void uplcom_intr(struct usbd_xfer *, void *, usbd_status);
115 
116 void uplcom_set(void *, int, int, int);
117 void uplcom_dtr(struct uplcom_softc *, int);
118 void uplcom_rts(struct uplcom_softc *, int);
119 void uplcom_break(struct uplcom_softc *, int);
120 void uplcom_set_line_state(struct uplcom_softc *);
121 void uplcom_get_status(void *, int portno, u_char *lsr, u_char *msr);
122 int  uplcom_param(void *, int, struct termios *);
123 int  uplcom_open(void *, int);
124 void uplcom_close(void *, int);
125 
126 struct	ucom_methods uplcom_methods = {
127 	uplcom_get_status,
128 	uplcom_set,
129 	uplcom_param,
130 	NULL,
131 	uplcom_open,
132 	uplcom_close,
133 	NULL,
134 	NULL,
135 };
136 
137 static const struct usb_devno uplcom_devs[] = {
138 	{ USB_VENDOR_ALCATEL, USB_PRODUCT_ALCATEL_OT535 },
139 	{ USB_VENDOR_ANCHOR, USB_PRODUCT_ANCHOR_SERIAL },
140 	{ USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
141 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U257 },
142 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
143 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 },
144 	{ USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 },
145 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
146 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ5 },
147 	{ USB_VENDOR_LEADTEK, USB_PRODUCT_LEADTEK_9531 },
148 	{ USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_700WX },
149 	{ USB_VENDOR_MOBILEACTION, USB_PRODUCT_MOBILEACTION_MA620 },
150 	{ USB_VENDOR_NOKIA, USB_PRODUCT_NOKIA_CA42 },
151 	{ USB_VENDOR_OTI, USB_PRODUCT_OTI_DKU5 },
152 	{ USB_VENDOR_PLX, USB_PRODUCT_PLX_CA42 },
153 	{ USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_TYTP50P6S },
154 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
155 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X },
156 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X2 },
157 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
158 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303BENQ },
159 	{ USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_PL2303 },
160 	{ USB_VENDOR_RADIOSHACK, USB_PRODUCT_RADIOSHACK_PL2303 },
161 	{ USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
162 	{ USB_VENDOR_SAGEM, USB_PRODUCT_SAGEM_SERIAL },
163 	{ USB_VENDOR_SAMSUNG2, USB_PRODUCT_SAMSUNG2_I330 },
164 	{ USB_VENDOR_SIEMENS3, USB_PRODUCT_SIEMENS3_SX1 },
165 	{ USB_VENDOR_SIEMENS3, USB_PRODUCT_SIEMENS3_X65 },
166 	{ USB_VENDOR_SIEMENS3, USB_PRODUCT_SIEMENS3_X75 },
167 	{ USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 },
168 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 },
169 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG },
170 	{ USB_VENDOR_SPEEDDRAGON, USB_PRODUCT_SPEEDDRAGON_MS3303H },
171 	{ USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU11 },
172 	{ USB_VENDOR_SYNTECH, USB_PRODUCT_SYNTECH_SERIAL },
173 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
174 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 },
175 	{ USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
176 	{ USB_VENDOR_SMART, USB_PRODUCT_SMART_PL2303 },
177 	{ USB_VENDOR_YCCABLE, USB_PRODUCT_YCCABLE_PL2303 }
178 };
179 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
180 
181 int uplcom_match(struct device *, void *, void *);
182 void uplcom_attach(struct device *, struct device *, void *);
183 int uplcom_detach(struct device *, int);
184 
185 struct cfdriver uplcom_cd = {
186 	NULL, "uplcom", DV_DULL
187 };
188 
189 const struct cfattach uplcom_ca = {
190 	sizeof(struct uplcom_softc), uplcom_match, uplcom_attach, uplcom_detach
191 };
192 
193 int
194 uplcom_match(struct device *parent, void *match, void *aux)
195 {
196 	struct usb_attach_arg *uaa = aux;
197 
198 	if (uaa->iface != NULL)
199 		return (UMATCH_NONE);
200 
201 	return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ?
202 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
203 }
204 
205 void
206 uplcom_attach(struct device *parent, struct device *self, void *aux)
207 {
208 	struct uplcom_softc *sc = (struct uplcom_softc *)self;
209 	struct usb_attach_arg *uaa = aux;
210 	struct usbd_device *dev = uaa->device;
211 	usb_config_descriptor_t *cdesc;
212 	usb_device_descriptor_t *ddesc;
213 	usb_interface_descriptor_t *id;
214 	usb_endpoint_descriptor_t *ed;
215 	char *devname = sc->sc_dev.dv_xname;
216 	usbd_status err;
217 	int i;
218 	struct ucom_attach_args uca;
219 
220         sc->sc_udev = dev;
221 
222 	DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
223 
224 	/* initialize endpoints */
225 	uca.bulkin = uca.bulkout = -1;
226 	sc->sc_intr_number = -1;
227 	sc->sc_intr_pipe = NULL;
228 
229 	/* Move the device into the configured state. */
230 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
231 	if (err) {
232 		printf("%s: failed to set configuration, err=%s\n",
233 			devname, usbd_errstr(err));
234 		usbd_deactivate(sc->sc_udev);
235 		return;
236 	}
237 
238 	/* get the config descriptor */
239 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
240 
241 	if (cdesc == NULL) {
242 		printf("%s: failed to get configuration descriptor\n",
243 			sc->sc_dev.dv_xname);
244 		usbd_deactivate(sc->sc_udev);
245 		return;
246 	}
247 
248 	/* get the device descriptor */
249 	ddesc = usbd_get_device_descriptor(sc->sc_udev);
250 	if (ddesc == NULL) {
251 		printf("%s: failed to get device descriptor\n",
252 		    sc->sc_dev.dv_xname);
253 		usbd_deactivate(sc->sc_udev);
254 		return;
255 	}
256 
257 	/*
258 	 * The Linux driver suggest this will only be true for the HX
259 	 * variants. The datasheets disagree.
260 	 */
261 	if (ddesc->bDeviceClass == 0x02)
262 		sc->sc_type_hx = 0;
263 	else if (ddesc->bMaxPacketSize == 0x40)
264 		sc->sc_type_hx = 1;
265 	else
266 		sc->sc_type_hx = 0;
267 
268 #ifdef USB_DEBUG
269 	/* print the chip type */
270 	if (sc->sc_type_hx) {
271 		DPRINTF(("uplcom_attach: chiptype 2303X\n"));
272 	} else {
273 		DPRINTF(("uplcom_attach: chiptype 2303\n"));
274 	}
275 #endif
276 	/* get the (first/common) interface */
277 	err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
278 							&sc->sc_iface);
279 	if (err) {
280 		printf("\n%s: failed to get interface, err=%s\n",
281 			devname, usbd_errstr(err));
282 		usbd_deactivate(sc->sc_udev);
283 		return;
284 	}
285 
286 	/* Find the interrupt endpoints */
287 
288 	id = usbd_get_interface_descriptor(sc->sc_iface);
289 	sc->sc_iface_number = id->bInterfaceNumber;
290 
291 	for (i = 0; i < id->bNumEndpoints; i++) {
292 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
293 		if (ed == NULL) {
294 			printf("%s: no endpoint descriptor for %d\n",
295 				sc->sc_dev.dv_xname, i);
296 			usbd_deactivate(sc->sc_udev);
297 			return;
298 		}
299 
300 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
301 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
302 			sc->sc_intr_number = ed->bEndpointAddress;
303 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
304 		}
305 	}
306 
307 	if (sc->sc_intr_number== -1) {
308 		printf("%s: Could not find interrupt in\n",
309 			sc->sc_dev.dv_xname);
310 		usbd_deactivate(sc->sc_udev);
311 		return;
312 	}
313 
314 	/* keep interface for interrupt */
315 	sc->sc_intr_iface = sc->sc_iface;
316 
317 	/*
318 	 * USB-RSAQ1 has two interface
319 	 *
320 	 *  USB-RSAQ1       | USB-RSAQ2
321  	 * -----------------+-----------------
322 	 * Interface 0      |Interface 0
323 	 *  Interrupt(0x81) | Interrupt(0x81)
324 	 * -----------------+ BulkIN(0x02)
325 	 * Interface 1	    | BulkOUT(0x83)
326 	 *   BulkIN(0x02)   |
327 	 *   BulkOUT(0x83)  |
328 	 */
329 	if (cdesc->bNumInterface == 2) {
330 		err = usbd_device2interface_handle(dev,
331 				UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
332 		if (err) {
333 			printf("\n%s: failed to get second interface, err=%s\n",
334 			    devname, usbd_errstr(err));
335 			usbd_deactivate(sc->sc_udev);
336 			return;
337 		}
338 	}
339 
340 	/* Find the bulk{in,out} endpoints */
341 
342 	id = usbd_get_interface_descriptor(sc->sc_iface);
343 	sc->sc_iface_number = id->bInterfaceNumber;
344 
345 	for (i = 0; i < id->bNumEndpoints; i++) {
346 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
347 		if (ed == NULL) {
348 			printf("%s: no endpoint descriptor for %d\n",
349 				sc->sc_dev.dv_xname, i);
350 			usbd_deactivate(sc->sc_udev);
351 			return;
352 		}
353 
354 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
355 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
356 			uca.bulkin = ed->bEndpointAddress;
357 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
358 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
359 			uca.bulkout = ed->bEndpointAddress;
360 		}
361 	}
362 
363 	if (uca.bulkin == -1) {
364 		printf("%s: Could not find data bulk in\n",
365 			sc->sc_dev.dv_xname);
366 		usbd_deactivate(sc->sc_udev);
367 		return;
368 	}
369 
370 	if (uca.bulkout == -1) {
371 		printf("%s: Could not find data bulk out\n",
372 			sc->sc_dev.dv_xname);
373 		usbd_deactivate(sc->sc_udev);
374 		return;
375 	}
376 
377 	sc->sc_dtr = sc->sc_rts = -1;
378 	uca.portno = UCOM_UNK_PORTNO;
379 	/* bulkin, bulkout set above */
380 	uca.ibufsize = UPLCOMIBUFSIZE;
381 	uca.obufsize = UPLCOMOBUFSIZE;
382 	uca.ibufsizepad = UPLCOMIBUFSIZE;
383 	uca.opkthdrlen = 0;
384 	uca.device = dev;
385 	uca.iface = sc->sc_iface;
386 	uca.methods = &uplcom_methods;
387 	uca.arg = sc;
388 	uca.info = NULL;
389 
390 	err = uplcom_reset(sc);
391 
392 	if (err) {
393 		printf("%s: reset failed, %s\n", sc->sc_dev.dv_xname,
394 			usbd_errstr(err));
395 		usbd_deactivate(sc->sc_udev);
396 		return;
397 	}
398 
399 	DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
400 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
401 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
402 }
403 
404 int
405 uplcom_detach(struct device *self, int flags)
406 {
407 	struct uplcom_softc *sc = (struct uplcom_softc *)self;
408 	int rv = 0;
409 
410 	DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
411 
412         if (sc->sc_intr_pipe != NULL) {
413                 usbd_abort_pipe(sc->sc_intr_pipe);
414                 usbd_close_pipe(sc->sc_intr_pipe);
415 		free(sc->sc_intr_buf, M_USBDEV, 0);
416                 sc->sc_intr_pipe = NULL;
417         }
418 
419 	if (sc->sc_subdev != NULL) {
420 		rv = config_detach(sc->sc_subdev, flags);
421 		sc->sc_subdev = NULL;
422 	}
423 
424 	return (rv);
425 }
426 
427 usbd_status
428 uplcom_reset(struct uplcom_softc *sc)
429 {
430         usb_device_request_t req;
431 	usbd_status err;
432 
433         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
434         req.bRequest = UPLCOM_SET_REQUEST;
435         USETW(req.wValue, 0);
436         USETW(req.wIndex, sc->sc_iface_number);
437         USETW(req.wLength, 0);
438 
439         err = usbd_do_request(sc->sc_udev, &req, 0);
440 	if (err)
441 		return (EIO);
442 
443 	return (0);
444 }
445 
446 void
447 uplcom_set_line_state(struct uplcom_softc *sc)
448 {
449 	usb_device_request_t req;
450 	int ls;
451 
452 	/* Make sure we have initialized state for sc_dtr and sc_rts */
453 	if (sc->sc_dtr == -1)
454 		sc->sc_dtr = 0;
455 	if (sc->sc_rts == -1)
456 		sc->sc_rts = 0;
457 
458 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
459 	    (sc->sc_rts ? UCDC_LINE_RTS : 0);
460 
461 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
462 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
463 	USETW(req.wValue, ls);
464 	USETW(req.wIndex, sc->sc_iface_number);
465 	USETW(req.wLength, 0);
466 
467 	(void)usbd_do_request(sc->sc_udev, &req, 0);
468 
469 }
470 
471 void
472 uplcom_set(void *addr, int portno, int reg, int onoff)
473 {
474 	struct uplcom_softc *sc = addr;
475 
476 	switch (reg) {
477 	case UCOM_SET_DTR:
478 		uplcom_dtr(sc, onoff);
479 		break;
480 	case UCOM_SET_RTS:
481 		uplcom_rts(sc, onoff);
482 		break;
483 	case UCOM_SET_BREAK:
484 		uplcom_break(sc, onoff);
485 		break;
486 	default:
487 		break;
488 	}
489 }
490 
491 void
492 uplcom_dtr(struct uplcom_softc *sc, int onoff)
493 {
494 
495 	DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
496 
497 	if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
498 		return;
499 
500 	sc->sc_dtr = !!onoff;
501 
502 	uplcom_set_line_state(sc);
503 }
504 
505 void
506 uplcom_rts(struct uplcom_softc *sc, int onoff)
507 {
508 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
509 
510 	if (sc->sc_rts == -1 && !sc->sc_rts == !onoff)
511 		return;
512 
513 	sc->sc_rts = !!onoff;
514 
515 	uplcom_set_line_state(sc);
516 }
517 
518 void
519 uplcom_break(struct uplcom_softc *sc, int onoff)
520 {
521 	usb_device_request_t req;
522 
523 	DPRINTF(("uplcom_break: onoff=%d\n", onoff));
524 
525 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
526 	req.bRequest = UCDC_SEND_BREAK;
527 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
528 	USETW(req.wIndex, sc->sc_iface_number);
529 	USETW(req.wLength, 0);
530 
531 	(void)usbd_do_request(sc->sc_udev, &req, 0);
532 }
533 
534 usbd_status
535 uplcom_set_crtscts(struct uplcom_softc *sc)
536 {
537 	usb_device_request_t req;
538 	usbd_status err;
539 
540 	DPRINTF(("uplcom_set_crtscts: on\n"));
541 
542 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
543 	req.bRequest = UPLCOM_SET_REQUEST;
544 	USETW(req.wValue, 0);
545 	USETW(req.wIndex,
546 	    (sc->sc_type_hx ? UPLCOM_HX_SET_CRTSCTS : UPLCOM_SET_CRTSCTS));
547 	USETW(req.wLength, 0);
548 
549 	err = usbd_do_request(sc->sc_udev, &req, 0);
550 	if (err) {
551 		DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
552 			usbd_errstr(err)));
553 		return (err);
554 	}
555 
556 	return (USBD_NORMAL_COMPLETION);
557 }
558 
559 usbd_status
560 uplcom_set_line_coding(struct uplcom_softc *sc,
561     struct usb_cdc_line_state *state)
562 {
563 	usb_device_request_t req;
564 	usbd_status err;
565 
566 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
567 		UGETDW(state->dwDTERate), state->bCharFormat,
568 		state->bParityType, state->bDataBits));
569 
570 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
571 		DPRINTF(("uplcom_set_line_coding: already set\n"));
572 		return (USBD_NORMAL_COMPLETION);
573 	}
574 
575 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
576 	req.bRequest = UCDC_SET_LINE_CODING;
577 	USETW(req.wValue, 0);
578 	USETW(req.wIndex, sc->sc_iface_number);
579 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
580 
581 	err = usbd_do_request(sc->sc_udev, &req, state);
582 	if (err) {
583 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
584 			usbd_errstr(err)));
585 		return (err);
586 	}
587 
588 	sc->sc_line_state = *state;
589 
590 	return (USBD_NORMAL_COMPLETION);
591 }
592 
593 int
594 uplcom_param(void *addr, int portno, struct termios *t)
595 {
596 	struct uplcom_softc *sc = addr;
597 	usbd_status err;
598 	struct usb_cdc_line_state ls;
599 
600 	DPRINTF(("uplcom_param: sc=%p\n", sc));
601 
602 	USETDW(ls.dwDTERate, t->c_ospeed);
603 	if (ISSET(t->c_cflag, CSTOPB))
604 		ls.bCharFormat = UCDC_STOP_BIT_2;
605 	else
606 		ls.bCharFormat = UCDC_STOP_BIT_1;
607 	if (ISSET(t->c_cflag, PARENB)) {
608 		if (ISSET(t->c_cflag, PARODD))
609 			ls.bParityType = UCDC_PARITY_ODD;
610 		else
611 			ls.bParityType = UCDC_PARITY_EVEN;
612 	} else
613 		ls.bParityType = UCDC_PARITY_NONE;
614 	switch (ISSET(t->c_cflag, CSIZE)) {
615 	case CS5:
616 		ls.bDataBits = 5;
617 		break;
618 	case CS6:
619 		ls.bDataBits = 6;
620 		break;
621 	case CS7:
622 		ls.bDataBits = 7;
623 		break;
624 	case CS8:
625 		ls.bDataBits = 8;
626 		break;
627 	}
628 
629 	err = uplcom_set_line_coding(sc, &ls);
630 	if (err) {
631 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
632 		return (EIO);
633 	}
634 
635 	if (ISSET(t->c_cflag, CRTSCTS))
636 		uplcom_set_crtscts(sc);
637 
638 	if (sc->sc_rts == -1 || sc->sc_dtr == -1)
639 		uplcom_set_line_state(sc);
640 
641 	if (err) {
642 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
643 		return (EIO);
644 	}
645 
646 	return (0);
647 }
648 
649 int
650 uplcom_open(void *addr, int portno)
651 {
652 	struct uplcom_softc *sc = addr;
653 	usb_device_request_t req;
654 	usbd_status uerr;
655 	int err;
656 
657 	if (usbd_is_dying(sc->sc_udev))
658 		return (EIO);
659 
660 	DPRINTF(("uplcom_open: sc=%p\n", sc));
661 
662 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
663 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
664 		err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
665 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
666 			sc->sc_intr_buf, sc->sc_isize,
667 			uplcom_intr, USBD_DEFAULT_INTERVAL);
668 		if (err) {
669 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
670 				sc->sc_dev.dv_xname, sc->sc_intr_number));
671 					return (EIO);
672 		}
673 	}
674 
675 	if (sc->sc_type_hx == 1) {
676 		/*
677 		 * Undocumented (vendor unresponsive) - possibly changes
678 		 * flow control semantics. It is needed for HX variant devices.
679 		 */
680 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
681 		req.bRequest = UPLCOM_SET_REQUEST;
682 		USETW(req.wValue, 2);
683 		USETW(req.wIndex, 0x44);
684 		USETW(req.wLength, 0);
685 
686 		uerr = usbd_do_request(sc->sc_udev, &req, 0);
687 		if (uerr)
688 			return (EIO);
689 
690 		/* Reset upstream data pipes */
691 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
692 		req.bRequest = UPLCOM_SET_REQUEST;
693 		USETW(req.wValue, 8);
694 		USETW(req.wIndex, 0);
695 		USETW(req.wLength, 0);
696 
697 		uerr = usbd_do_request(sc->sc_udev, &req, 0);
698 		if (uerr)
699 			return (EIO);
700 
701 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
702 		req.bRequest = UPLCOM_SET_REQUEST;
703 		USETW(req.wValue, 9);
704 		USETW(req.wIndex, 0);
705 		USETW(req.wLength, 0);
706 
707 		uerr = usbd_do_request(sc->sc_udev, &req, 0);
708 		if (uerr)
709 			return (EIO);
710 	}
711 
712 	return (0);
713 }
714 
715 void
716 uplcom_close(void *addr, int portno)
717 {
718 	struct uplcom_softc *sc = addr;
719 	int err;
720 
721 	if (usbd_is_dying(sc->sc_udev))
722 		return;
723 
724 	DPRINTF(("uplcom_close: close\n"));
725 
726 	if (sc->sc_intr_pipe != NULL) {
727 		usbd_abort_pipe(sc->sc_intr_pipe);
728 		err = usbd_close_pipe(sc->sc_intr_pipe);
729 		if (err)
730 			printf("%s: close interrupt pipe failed: %s\n",
731 				sc->sc_dev.dv_xname, usbd_errstr(err));
732 		free(sc->sc_intr_buf, M_USBDEV, 0);
733 		sc->sc_intr_pipe = NULL;
734 	}
735 }
736 
737 void
738 uplcom_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
739 {
740 	struct uplcom_softc *sc = priv;
741 	u_char *buf = sc->sc_intr_buf;
742 	u_char pstatus;
743 
744 	if (usbd_is_dying(sc->sc_udev))
745 		return;
746 
747 	if (status != USBD_NORMAL_COMPLETION) {
748 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
749 			return;
750 
751 		DPRINTF(("%s: abnormal status: %s\n", sc->sc_dev.dv_xname,
752 			usbd_errstr(status)));
753 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
754 		return;
755 	}
756 
757 	DPRINTF(("%s: uplcom status = %02x\n", sc->sc_dev.dv_xname, buf[8]));
758 
759 	sc->sc_lsr = sc->sc_msr = 0;
760 	pstatus = buf[8];
761 	if (ISSET(pstatus, RSAQ_STATUS_CTS))
762 		sc->sc_msr |= UMSR_CTS;
763 	else
764 		sc->sc_msr &= ~UMSR_CTS;
765 	if (ISSET(pstatus, RSAQ_STATUS_DSR))
766 		sc->sc_msr |= UMSR_DSR;
767 	else
768 		sc->sc_msr &= ~UMSR_DSR;
769 	if (ISSET(pstatus, RSAQ_STATUS_DCD))
770 		sc->sc_msr |= UMSR_DCD;
771 	else
772 		sc->sc_msr &= ~UMSR_DCD;
773 	ucom_status_change((struct ucom_softc *) sc->sc_subdev);
774 }
775 
776 void
777 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
778 {
779 	struct uplcom_softc *sc = addr;
780 
781 	DPRINTF(("uplcom_get_status:\n"));
782 
783 	if (lsr != NULL)
784 		*lsr = sc->sc_lsr;
785 	if (msr != NULL)
786 		*msr = sc->sc_msr;
787 }
788