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