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