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