xref: /openbsd-src/sys/dev/usb/uplcom.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: uplcom.c,v 1.64 2014/07/12 21:24:33 mpi Exp $	*/
2 /*	$NetBSD: uplcom.c,v 1.29 2002/09/23 05:51:23 simonb Exp $	*/
3 /*
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Ichiro FUKUHARA (ichiro@ichiro.org).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Simple datasheet
34  * http://www.prolific.com.tw/PDF/PL-2303%20Market%20Spec.pdf
35  * http://www.hitachi-hitec.com/jyouhou/prolific/2303.pdf
36  * 	(english)
37  *
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/ioctl.h>
45 #include <sys/conf.h>
46 #include <sys/tty.h>
47 #include <sys/file.h>
48 #include <sys/selinfo.h>
49 #include <sys/device.h>
50 #include <sys/poll.h>
51 
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbcdc.h>
54 
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
57 #include <dev/usb/usbdevs.h>
58 #include <dev/usb/usb_quirks.h>
59 
60 #include <dev/usb/usbdevs.h>
61 #include <dev/usb/ucomvar.h>
62 
63 #ifdef UPLCOM_DEBUG
64 #define DPRINTFN(n, x)  do { if (uplcomdebug > (n)) printf x; } while (0)
65 int	uplcomdebug = 0;
66 #else
67 #define DPRINTFN(n, x)
68 #endif
69 #define DPRINTF(x) DPRINTFN(0, x)
70 
71 #define	UPLCOM_CONFIG_INDEX	0
72 #define	UPLCOM_IFACE_INDEX	0
73 #define	UPLCOM_SECOND_IFACE_INDEX	1
74 
75 #define	UPLCOM_SET_REQUEST	0x01
76 #define	UPLCOM_SET_CRTSCTS	0x41
77 #define	UPLCOM_HX_SET_CRTSCTS	0x61
78 #define RSAQ_STATUS_CTS		0x80
79 #define RSAQ_STATUS_DSR		0x02
80 #define RSAQ_STATUS_DCD		0x01
81 
82 struct	uplcom_softc {
83 	struct device		 sc_dev;	/* base device */
84 	struct usbd_device	*sc_udev;	/* USB device */
85 	struct usbd_interface	*sc_iface;	/* interface */
86 	int			 sc_iface_number;	/* interface number */
87 
88 	struct usbd_interface	*sc_intr_iface;	/* interrupt interface */
89 	int			 sc_intr_number;	/* interrupt number */
90 	struct usbd_pipe	*sc_intr_pipe;	/* interrupt pipe */
91 	u_char			*sc_intr_buf;	/* interrupt buffer */
92 	int			 sc_isize;
93 
94 	struct usb_cdc_line_state sc_line_state;/* current line state */
95 	int			 sc_dtr;	/* current DTR state */
96 	int			 sc_rts;	/* current RTS state */
97 
98 	struct device		*sc_subdev;	/* ucom device */
99 
100 	u_char			 sc_lsr;	/* Local status register */
101 	u_char			 sc_msr;	/* uplcom status register */
102 	int			 sc_type_hx;	/* HX variant */
103 };
104 
105 /*
106  * These are the maximum number of bytes transferred per frame.
107  * The output buffer size cannot be increased due to the size encoding.
108  */
109 #define UPLCOMIBUFSIZE 256
110 #define UPLCOMOBUFSIZE 256
111 
112 usbd_status uplcom_reset(struct uplcom_softc *);
113 usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
114     struct usb_cdc_line_state *state);
115 usbd_status uplcom_set_crtscts(struct uplcom_softc *);
116 void uplcom_intr(struct usbd_xfer *, void *, usbd_status);
117 
118 void uplcom_set(void *, int, int, int);
119 void uplcom_dtr(struct uplcom_softc *, int);
120 void uplcom_rts(struct uplcom_softc *, int);
121 void uplcom_break(struct uplcom_softc *, int);
122 void uplcom_set_line_state(struct uplcom_softc *);
123 void uplcom_get_status(void *, int portno, u_char *lsr, u_char *msr);
124 #if TODO
125 int  uplcom_ioctl(void *, int, u_long, caddr_t, int, struct proc *);
126 #endif
127 int  uplcom_param(void *, int, struct termios *);
128 int  uplcom_open(void *, int);
129 void uplcom_close(void *, int);
130 
131 struct	ucom_methods uplcom_methods = {
132 	uplcom_get_status,
133 	uplcom_set,
134 	uplcom_param,
135 	NULL, /* uplcom_ioctl, TODO */
136 	uplcom_open,
137 	uplcom_close,
138 	NULL,
139 	NULL,
140 };
141 
142 static const struct usb_devno uplcom_devs[] = {
143 	{ USB_VENDOR_ALCATEL, USB_PRODUCT_ALCATEL_OT535 },
144 	{ USB_VENDOR_ANCHOR, USB_PRODUCT_ANCHOR_SERIAL },
145 	{ USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
146 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U257 },
147 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
148 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 },
149 	{ USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 },
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_PL2303X },
161 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X2 },
162 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
163 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303BENQ },
164 	{ USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_PL2303 },
165 	{ USB_VENDOR_RADIOSHACK, USB_PRODUCT_RADIOSHACK_PL2303 },
166 	{ USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
167 	{ USB_VENDOR_SAGEM, USB_PRODUCT_SAGEM_SERIAL },
168 	{ USB_VENDOR_SAMSUNG2, USB_PRODUCT_SAMSUNG2_I330 },
169 	{ USB_VENDOR_SIEMENS3, USB_PRODUCT_SIEMENS3_SX1 },
170 	{ USB_VENDOR_SIEMENS3, USB_PRODUCT_SIEMENS3_X65 },
171 	{ USB_VENDOR_SIEMENS3, USB_PRODUCT_SIEMENS3_X75 },
172 	{ USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 },
173 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 },
174 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG },
175 	{ USB_VENDOR_SPEEDDRAGON, USB_PRODUCT_SPEEDDRAGON_MS3303H },
176 	{ USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU11 },
177 	{ USB_VENDOR_SYNTECH, USB_PRODUCT_SYNTECH_SERIAL },
178 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
179 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 },
180 	{ USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
181 	{ USB_VENDOR_SMART, USB_PRODUCT_SMART_PL2303 },
182 	{ USB_VENDOR_YCCABLE, USB_PRODUCT_YCCABLE_PL2303 }
183 };
184 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
185 
186 int uplcom_match(struct device *, void *, void *);
187 void uplcom_attach(struct device *, struct device *, void *);
188 int uplcom_detach(struct device *, int);
189 
190 struct cfdriver uplcom_cd = {
191 	NULL, "uplcom", DV_DULL
192 };
193 
194 const struct cfattach uplcom_ca = {
195 	sizeof(struct uplcom_softc), uplcom_match, uplcom_attach, uplcom_detach
196 };
197 
198 int
199 uplcom_match(struct device *parent, void *match, void *aux)
200 {
201 	struct usb_attach_arg *uaa = aux;
202 
203 	if (uaa->iface != NULL)
204 		return (UMATCH_NONE);
205 
206 	return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ?
207 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
208 }
209 
210 void
211 uplcom_attach(struct device *parent, struct device *self, void *aux)
212 {
213 	struct uplcom_softc *sc = (struct uplcom_softc *)self;
214 	struct usb_attach_arg *uaa = aux;
215 	struct usbd_device *dev = uaa->device;
216 	usb_config_descriptor_t *cdesc;
217 	usb_device_descriptor_t *ddesc;
218 	usb_interface_descriptor_t *id;
219 	usb_endpoint_descriptor_t *ed;
220 	char *devname = sc->sc_dev.dv_xname;
221 	usbd_status err;
222 	int i;
223 	struct ucom_attach_args uca;
224 
225         sc->sc_udev = dev;
226 
227 	DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
228 
229 	/* initialize endpoints */
230 	uca.bulkin = uca.bulkout = -1;
231 	sc->sc_intr_number = -1;
232 	sc->sc_intr_pipe = NULL;
233 
234 	/* Move the device into the configured state. */
235 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
236 	if (err) {
237 		printf("%s: failed to set configuration, err=%s\n",
238 			devname, usbd_errstr(err));
239 		usbd_deactivate(sc->sc_udev);
240 		return;
241 	}
242 
243 	/* get the config descriptor */
244 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
245 
246 	if (cdesc == NULL) {
247 		printf("%s: failed to get configuration descriptor\n",
248 			sc->sc_dev.dv_xname);
249 		usbd_deactivate(sc->sc_udev);
250 		return;
251 	}
252 
253 	/* get the device descriptor */
254 	ddesc = usbd_get_device_descriptor(sc->sc_udev);
255 	if (ddesc == NULL) {
256 		printf("%s: failed to get device descriptor\n",
257 		    sc->sc_dev.dv_xname);
258 		usbd_deactivate(sc->sc_udev);
259 		return;
260 	}
261 
262 	/*
263 	 * The Linux driver suggest this will only be true for the HX
264 	 * variants. The datasheets disagree.
265 	 */
266 	if (ddesc->bDeviceClass == 0x02)
267 		sc->sc_type_hx = 0;
268 	else if (ddesc->bMaxPacketSize == 0x40)
269 		sc->sc_type_hx = 1;
270 	else
271 		sc->sc_type_hx = 0;
272 
273 #ifdef USB_DEBUG
274 	/* print the chip type */
275 	if (sc->sc_type_hx) {
276 		DPRINTF(("uplcom_attach: chiptype 2303X\n"));
277 	} else {
278 		DPRINTF(("uplcom_attach: chiptype 2303\n"));
279 	}
280 #endif
281 	/* get the (first/common) interface */
282 	err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
283 							&sc->sc_iface);
284 	if (err) {
285 		printf("\n%s: failed to get interface, err=%s\n",
286 			devname, usbd_errstr(err));
287 		usbd_deactivate(sc->sc_udev);
288 		return;
289 	}
290 
291 	/* Find the interrupt endpoints */
292 
293 	id = usbd_get_interface_descriptor(sc->sc_iface);
294 	sc->sc_iface_number = id->bInterfaceNumber;
295 
296 	for (i = 0; i < id->bNumEndpoints; i++) {
297 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
298 		if (ed == NULL) {
299 			printf("%s: no endpoint descriptor for %d\n",
300 				sc->sc_dev.dv_xname, i);
301 			usbd_deactivate(sc->sc_udev);
302 			return;
303 		}
304 
305 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
306 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
307 			sc->sc_intr_number = ed->bEndpointAddress;
308 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
309 		}
310 	}
311 
312 	if (sc->sc_intr_number== -1) {
313 		printf("%s: Could not find interrupt in\n",
314 			sc->sc_dev.dv_xname);
315 		usbd_deactivate(sc->sc_udev);
316 		return;
317 	}
318 
319 	/* keep interface for interrupt */
320 	sc->sc_intr_iface = sc->sc_iface;
321 
322 	/*
323 	 * USB-RSAQ1 has two interface
324 	 *
325 	 *  USB-RSAQ1       | USB-RSAQ2
326  	 * -----------------+-----------------
327 	 * Interface 0      |Interface 0
328 	 *  Interrupt(0x81) | Interrupt(0x81)
329 	 * -----------------+ BulkIN(0x02)
330 	 * Interface 1	    | BulkOUT(0x83)
331 	 *   BulkIN(0x02)   |
332 	 *   BulkOUT(0x83)  |
333 	 */
334 	if (cdesc->bNumInterface == 2) {
335 		err = usbd_device2interface_handle(dev,
336 				UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
337 		if (err) {
338 			printf("\n%s: failed to get second interface, err=%s\n",
339 			    devname, usbd_errstr(err));
340 			usbd_deactivate(sc->sc_udev);
341 			return;
342 		}
343 	}
344 
345 	/* Find the bulk{in,out} endpoints */
346 
347 	id = usbd_get_interface_descriptor(sc->sc_iface);
348 	sc->sc_iface_number = id->bInterfaceNumber;
349 
350 	for (i = 0; i < id->bNumEndpoints; i++) {
351 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
352 		if (ed == NULL) {
353 			printf("%s: no endpoint descriptor for %d\n",
354 				sc->sc_dev.dv_xname, i);
355 			usbd_deactivate(sc->sc_udev);
356 			return;
357 		}
358 
359 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
360 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
361 			uca.bulkin = ed->bEndpointAddress;
362 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
363 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
364 			uca.bulkout = ed->bEndpointAddress;
365 		}
366 	}
367 
368 	if (uca.bulkin == -1) {
369 		printf("%s: Could not find data bulk in\n",
370 			sc->sc_dev.dv_xname);
371 		usbd_deactivate(sc->sc_udev);
372 		return;
373 	}
374 
375 	if (uca.bulkout == -1) {
376 		printf("%s: Could not find data bulk out\n",
377 			sc->sc_dev.dv_xname);
378 		usbd_deactivate(sc->sc_udev);
379 		return;
380 	}
381 
382 	sc->sc_dtr = sc->sc_rts = -1;
383 	uca.portno = UCOM_UNK_PORTNO;
384 	/* bulkin, bulkout set above */
385 	uca.ibufsize = UPLCOMIBUFSIZE;
386 	uca.obufsize = UPLCOMOBUFSIZE;
387 	uca.ibufsizepad = UPLCOMIBUFSIZE;
388 	uca.opkthdrlen = 0;
389 	uca.device = dev;
390 	uca.iface = sc->sc_iface;
391 	uca.methods = &uplcom_methods;
392 	uca.arg = sc;
393 	uca.info = NULL;
394 
395 	err = uplcom_reset(sc);
396 
397 	if (err) {
398 		printf("%s: reset failed, %s\n", sc->sc_dev.dv_xname,
399 			usbd_errstr(err));
400 		usbd_deactivate(sc->sc_udev);
401 		return;
402 	}
403 
404 	DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
405 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
406 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
407 }
408 
409 int
410 uplcom_detach(struct device *self, int flags)
411 {
412 	struct uplcom_softc *sc = (struct uplcom_softc *)self;
413 	int rv = 0;
414 
415 	DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
416 
417         if (sc->sc_intr_pipe != NULL) {
418                 usbd_abort_pipe(sc->sc_intr_pipe);
419                 usbd_close_pipe(sc->sc_intr_pipe);
420 		free(sc->sc_intr_buf, M_USBDEV, 0);
421                 sc->sc_intr_pipe = NULL;
422         }
423 
424 	if (sc->sc_subdev != NULL) {
425 		rv = config_detach(sc->sc_subdev, flags);
426 		sc->sc_subdev = NULL;
427 	}
428 
429 	return (rv);
430 }
431 
432 usbd_status
433 uplcom_reset(struct uplcom_softc *sc)
434 {
435         usb_device_request_t req;
436 	usbd_status err;
437 
438         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
439         req.bRequest = UPLCOM_SET_REQUEST;
440         USETW(req.wValue, 0);
441         USETW(req.wIndex, sc->sc_iface_number);
442         USETW(req.wLength, 0);
443 
444         err = usbd_do_request(sc->sc_udev, &req, 0);
445 	if (err)
446 		return (EIO);
447 
448 	return (0);
449 }
450 
451 void
452 uplcom_set_line_state(struct uplcom_softc *sc)
453 {
454 	usb_device_request_t req;
455 	int ls;
456 
457 	/* Make sure we have initialized state for sc_dtr and sc_rts */
458 	if (sc->sc_dtr == -1)
459 		sc->sc_dtr = 0;
460 	if (sc->sc_rts == -1)
461 		sc->sc_rts = 0;
462 
463 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
464 	    (sc->sc_rts ? UCDC_LINE_RTS : 0);
465 
466 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
467 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
468 	USETW(req.wValue, ls);
469 	USETW(req.wIndex, sc->sc_iface_number);
470 	USETW(req.wLength, 0);
471 
472 	(void)usbd_do_request(sc->sc_udev, &req, 0);
473 
474 }
475 
476 void
477 uplcom_set(void *addr, int portno, int reg, int onoff)
478 {
479 	struct uplcom_softc *sc = addr;
480 
481 	switch (reg) {
482 	case UCOM_SET_DTR:
483 		uplcom_dtr(sc, onoff);
484 		break;
485 	case UCOM_SET_RTS:
486 		uplcom_rts(sc, onoff);
487 		break;
488 	case UCOM_SET_BREAK:
489 		uplcom_break(sc, onoff);
490 		break;
491 	default:
492 		break;
493 	}
494 }
495 
496 void
497 uplcom_dtr(struct uplcom_softc *sc, int onoff)
498 {
499 
500 	DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
501 
502 	if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
503 		return;
504 
505 	sc->sc_dtr = !!onoff;
506 
507 	uplcom_set_line_state(sc);
508 }
509 
510 void
511 uplcom_rts(struct uplcom_softc *sc, int onoff)
512 {
513 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
514 
515 	if (sc->sc_rts == -1 && !sc->sc_rts == !onoff)
516 		return;
517 
518 	sc->sc_rts = !!onoff;
519 
520 	uplcom_set_line_state(sc);
521 }
522 
523 void
524 uplcom_break(struct uplcom_softc *sc, int onoff)
525 {
526 	usb_device_request_t req;
527 
528 	DPRINTF(("uplcom_break: onoff=%d\n", onoff));
529 
530 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
531 	req.bRequest = UCDC_SEND_BREAK;
532 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
533 	USETW(req.wIndex, sc->sc_iface_number);
534 	USETW(req.wLength, 0);
535 
536 	(void)usbd_do_request(sc->sc_udev, &req, 0);
537 }
538 
539 usbd_status
540 uplcom_set_crtscts(struct uplcom_softc *sc)
541 {
542 	usb_device_request_t req;
543 	usbd_status err;
544 
545 	DPRINTF(("uplcom_set_crtscts: on\n"));
546 
547 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
548 	req.bRequest = UPLCOM_SET_REQUEST;
549 	USETW(req.wValue, 0);
550 	USETW(req.wIndex,
551 	    (sc->sc_type_hx ? UPLCOM_HX_SET_CRTSCTS : UPLCOM_SET_CRTSCTS));
552 	USETW(req.wLength, 0);
553 
554 	err = usbd_do_request(sc->sc_udev, &req, 0);
555 	if (err) {
556 		DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
557 			usbd_errstr(err)));
558 		return (err);
559 	}
560 
561 	return (USBD_NORMAL_COMPLETION);
562 }
563 
564 usbd_status
565 uplcom_set_line_coding(struct uplcom_softc *sc,
566     struct usb_cdc_line_state *state)
567 {
568 	usb_device_request_t req;
569 	usbd_status err;
570 
571 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
572 		UGETDW(state->dwDTERate), state->bCharFormat,
573 		state->bParityType, state->bDataBits));
574 
575 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
576 		DPRINTF(("uplcom_set_line_coding: already set\n"));
577 		return (USBD_NORMAL_COMPLETION);
578 	}
579 
580 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
581 	req.bRequest = UCDC_SET_LINE_CODING;
582 	USETW(req.wValue, 0);
583 	USETW(req.wIndex, sc->sc_iface_number);
584 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
585 
586 	err = usbd_do_request(sc->sc_udev, &req, state);
587 	if (err) {
588 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
589 			usbd_errstr(err)));
590 		return (err);
591 	}
592 
593 	sc->sc_line_state = *state;
594 
595 	return (USBD_NORMAL_COMPLETION);
596 }
597 
598 int
599 uplcom_param(void *addr, int portno, struct termios *t)
600 {
601 	struct uplcom_softc *sc = addr;
602 	usbd_status err;
603 	struct usb_cdc_line_state ls;
604 
605 	DPRINTF(("uplcom_param: sc=%p\n", sc));
606 
607 	USETDW(ls.dwDTERate, t->c_ospeed);
608 	if (ISSET(t->c_cflag, CSTOPB))
609 		ls.bCharFormat = UCDC_STOP_BIT_2;
610 	else
611 		ls.bCharFormat = UCDC_STOP_BIT_1;
612 	if (ISSET(t->c_cflag, PARENB)) {
613 		if (ISSET(t->c_cflag, PARODD))
614 			ls.bParityType = UCDC_PARITY_ODD;
615 		else
616 			ls.bParityType = UCDC_PARITY_EVEN;
617 	} else
618 		ls.bParityType = UCDC_PARITY_NONE;
619 	switch (ISSET(t->c_cflag, CSIZE)) {
620 	case CS5:
621 		ls.bDataBits = 5;
622 		break;
623 	case CS6:
624 		ls.bDataBits = 6;
625 		break;
626 	case CS7:
627 		ls.bDataBits = 7;
628 		break;
629 	case CS8:
630 		ls.bDataBits = 8;
631 		break;
632 	}
633 
634 	err = uplcom_set_line_coding(sc, &ls);
635 	if (err) {
636 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
637 		return (EIO);
638 	}
639 
640 	if (ISSET(t->c_cflag, CRTSCTS))
641 		uplcom_set_crtscts(sc);
642 
643 	if (sc->sc_rts == -1 || sc->sc_dtr == -1)
644 		uplcom_set_line_state(sc);
645 
646 	if (err) {
647 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
648 		return (EIO);
649 	}
650 
651 	return (0);
652 }
653 
654 int
655 uplcom_open(void *addr, int portno)
656 {
657 	struct uplcom_softc *sc = addr;
658 	usb_device_request_t req;
659 	usbd_status uerr;
660 	int err;
661 
662 	if (usbd_is_dying(sc->sc_udev))
663 		return (EIO);
664 
665 	DPRINTF(("uplcom_open: sc=%p\n", sc));
666 
667 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
668 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
669 		err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
670 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
671 			sc->sc_intr_buf, sc->sc_isize,
672 			uplcom_intr, USBD_DEFAULT_INTERVAL);
673 		if (err) {
674 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
675 				sc->sc_dev.dv_xname, sc->sc_intr_number));
676 					return (EIO);
677 		}
678 	}
679 
680 	if (sc->sc_type_hx == 1) {
681 		/*
682 		 * Undocumented (vendor unresponsive) - possibly changes
683 		 * flow control semantics. It is needed for HX variant devices.
684 		 */
685 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
686 		req.bRequest = UPLCOM_SET_REQUEST;
687 		USETW(req.wValue, 2);
688 		USETW(req.wIndex, 0x44);
689 		USETW(req.wLength, 0);
690 
691 		uerr = usbd_do_request(sc->sc_udev, &req, 0);
692 		if (uerr)
693 			return (EIO);
694 
695 		/* Reset upstream data pipes */
696 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
697 		req.bRequest = UPLCOM_SET_REQUEST;
698 		USETW(req.wValue, 8);
699 		USETW(req.wIndex, 0);
700 		USETW(req.wLength, 0);
701 
702 		uerr = usbd_do_request(sc->sc_udev, &req, 0);
703 		if (uerr)
704 			return (EIO);
705 
706 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
707 		req.bRequest = UPLCOM_SET_REQUEST;
708 		USETW(req.wValue, 9);
709 		USETW(req.wIndex, 0);
710 		USETW(req.wLength, 0);
711 
712 		uerr = usbd_do_request(sc->sc_udev, &req, 0);
713 		if (uerr)
714 			return (EIO);
715 	}
716 
717 	return (0);
718 }
719 
720 void
721 uplcom_close(void *addr, int portno)
722 {
723 	struct uplcom_softc *sc = addr;
724 	int err;
725 
726 	if (usbd_is_dying(sc->sc_udev))
727 		return;
728 
729 	DPRINTF(("uplcom_close: close\n"));
730 
731 	if (sc->sc_intr_pipe != NULL) {
732 		usbd_abort_pipe(sc->sc_intr_pipe);
733 		err = usbd_close_pipe(sc->sc_intr_pipe);
734 		if (err)
735 			printf("%s: close interrupt pipe failed: %s\n",
736 				sc->sc_dev.dv_xname, usbd_errstr(err));
737 		free(sc->sc_intr_buf, M_USBDEV, 0);
738 		sc->sc_intr_pipe = NULL;
739 	}
740 }
741 
742 void
743 uplcom_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
744 {
745 	struct uplcom_softc *sc = priv;
746 	u_char *buf = sc->sc_intr_buf;
747 	u_char pstatus;
748 
749 	if (usbd_is_dying(sc->sc_udev))
750 		return;
751 
752 	if (status != USBD_NORMAL_COMPLETION) {
753 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
754 			return;
755 
756 		DPRINTF(("%s: abnormal status: %s\n", sc->sc_dev.dv_xname,
757 			usbd_errstr(status)));
758 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
759 		return;
760 	}
761 
762 	DPRINTF(("%s: uplcom status = %02x\n", sc->sc_dev.dv_xname, buf[8]));
763 
764 	sc->sc_lsr = sc->sc_msr = 0;
765 	pstatus = buf[8];
766 	if (ISSET(pstatus, RSAQ_STATUS_CTS))
767 		sc->sc_msr |= UMSR_CTS;
768 	else
769 		sc->sc_msr &= ~UMSR_CTS;
770 	if (ISSET(pstatus, RSAQ_STATUS_DSR))
771 		sc->sc_msr |= UMSR_DSR;
772 	else
773 		sc->sc_msr &= ~UMSR_DSR;
774 	if (ISSET(pstatus, RSAQ_STATUS_DCD))
775 		sc->sc_msr |= UMSR_DCD;
776 	else
777 		sc->sc_msr &= ~UMSR_DCD;
778 	ucom_status_change((struct ucom_softc *) sc->sc_subdev);
779 }
780 
781 void
782 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
783 {
784 	struct uplcom_softc *sc = addr;
785 
786 	DPRINTF(("uplcom_get_status:\n"));
787 
788 	if (lsr != NULL)
789 		*lsr = sc->sc_lsr;
790 	if (msr != NULL)
791 		*msr = sc->sc_msr;
792 }
793 
794 #if TODO
795 int
796 uplcom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
797 	     struct proc *p)
798 {
799 	struct uplcom_softc *sc = addr;
800 	int error = 0;
801 
802 	if (usbd_is_dying(sc->sc_udev))
803 		return (EIO);
804 
805 	DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
806 
807 	switch (cmd) {
808 	case TIOCNOTTY:
809 	case TIOCMGET:
810 	case TIOCMSET:
811 	case USB_GET_CM_OVER_DATA:
812 	case USB_SET_CM_OVER_DATA:
813 		break;
814 
815 	default:
816 		DPRINTF(("uplcom_ioctl: unknown\n"));
817 		error = ENOTTY;
818 		break;
819 	}
820 
821 	return (error);
822 }
823 #endif
824