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