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