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