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