xref: /netbsd-src/sys/dev/usb/uplcom.c (revision 0dd5877adce57db949b16ae963e5a6831cccdfb6)
1 /*	$NetBSD: uplcom.c,v 1.25 2002/01/22 03:13:28 ichiro 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  * Simple datasheet
40  * http://www.prolific.com.tw/download/DataSheet/pl2303_ds11.PDF
41  * http://www.nisseisg.co.jp/jyouhou/_cp/@gif/2303.pdf
42  * 	(english)
43  *
44  */
45 
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: uplcom.c,v 1.25 2002/01/22 03:13:28 ichiro Exp $");
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/ioctl.h>
54 #include <sys/conf.h>
55 #include <sys/tty.h>
56 #include <sys/file.h>
57 #include <sys/select.h>
58 #include <sys/proc.h>
59 #include <sys/vnode.h>
60 #include <sys/device.h>
61 #include <sys/poll.h>
62 
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbcdc.h>
65 
66 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usbdi_util.h>
68 #include <dev/usb/usbdevs.h>
69 #include <dev/usb/usb_quirks.h>
70 
71 #include <dev/usb/usbdevs.h>
72 #include <dev/usb/ucomvar.h>
73 
74 #ifdef UPLCOM_DEBUG
75 #define DPRINTFN(n, x)  if (uplcomdebug > (n)) logprintf x
76 int	uplcomdebug = 0;
77 #else
78 #define DPRINTFN(n, x)
79 #endif
80 #define DPRINTF(x) DPRINTFN(0, x)
81 
82 #define	UPLCOM_CONFIG_INDEX	0
83 #define	UPLCOM_IFACE_INDEX	0
84 #define	UPLCOM_SECOND_IFACE_INDEX	1
85 
86 #define	UPLCOM_SET_REQUEST	0x01
87 #define	UPLCOM_SET_CRTSCTS	0x41
88 #define RSAQ_STATUS_DSR		0x02
89 #define RSAQ_STATUS_DCD		0x01
90 
91 struct	uplcom_softc {
92 	USBBASEDEVICE		sc_dev;		/* base device */
93 	usbd_device_handle	sc_udev;	/* USB device */
94 	usbd_interface_handle	sc_iface;	/* interface */
95 	int			sc_iface_number;	/* interface number */
96 
97 	usbd_interface_handle	sc_intr_iface;	/* interrupt interface */
98 	int			sc_intr_number;	/* interrupt number */
99 	usbd_pipe_handle	sc_intr_pipe;	/* interrupt pipe */
100 	u_char			*sc_intr_buf;	/* interrupt buffer */
101 	int			sc_isize;
102 
103 	usb_cdc_line_state_t	sc_line_state;	/* current line state */
104 	u_char			sc_dtr;		/* current DTR state */
105 	u_char			sc_rts;		/* current RTS state */
106 	u_char			sc_status;
107 
108 	device_ptr_t		sc_subdev;	/* ucom device */
109 
110 	u_char			sc_dying;	/* disconnecting */
111 
112 	u_char			sc_lsr;		/* Local status register */
113 	u_char			sc_msr;		/* uplcom status register */
114 };
115 
116 /*
117  * These are the maximum number of bytes transferred per frame.
118  * The output buffer size cannot be increased due to the size encoding.
119  */
120 #define UPLCOMIBUFSIZE 256
121 #define UPLCOMOBUFSIZE 256
122 
123 Static	usbd_status uplcom_reset(struct uplcom_softc *);
124 Static	usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
125 					   usb_cdc_line_state_t *state);
126 Static	usbd_status uplcom_set_crtscts(struct uplcom_softc *);
127 Static	void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
128 
129 Static	void uplcom_set(void *, int, int, int);
130 Static	void uplcom_dtr(struct uplcom_softc *, int);
131 Static	void uplcom_rts(struct uplcom_softc *, int);
132 Static	void uplcom_break(struct uplcom_softc *, int);
133 Static	void uplcom_set_line_state(struct uplcom_softc *);
134 Static	void uplcom_get_status(void *, int portno, u_char *lsr, u_char *msr);
135 #if TODO
136 Static	int  uplcom_ioctl(void *, int, u_long, caddr_t, int, usb_proc_ptr );
137 #endif
138 Static	int  uplcom_param(void *, int, struct termios *);
139 Static	int  uplcom_open(void *, int);
140 Static	void uplcom_close(void *, int);
141 
142 struct	ucom_methods uplcom_methods = {
143 	uplcom_get_status,
144 	uplcom_set,
145 	uplcom_param,
146 	NULL, /* uplcom_ioctl, TODO */
147 	uplcom_open,
148 	uplcom_close,
149 	NULL,
150 	NULL,
151 };
152 
153 static const struct usb_devno uplcom_devs[] = {
154 	/* I/O DATA USB-RSAQ2 */
155 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
156 	/* I/O DATA USB-RSAQ */
157 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
158 	/* PLANEX USB-RS232 URS-03 */
159 	{ USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
160 	/* IOGEAR/ATEN UC-232A */
161 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
162 	/* ELECOM UC-SGT */
163 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
164 	/* RATOC REX-USB60 */
165 	{ USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
166 };
167 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
168 
169 USB_DECLARE_DRIVER(uplcom);
170 
171 USB_MATCH(uplcom)
172 {
173 	USB_MATCH_START(uplcom, uaa);
174 
175 	if (uaa->iface != NULL)
176 		return (UMATCH_NONE);
177 
178 	return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ?
179 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
180 }
181 
182 USB_ATTACH(uplcom)
183 {
184 	USB_ATTACH_START(uplcom, sc, uaa);
185 	usbd_device_handle dev = uaa->device;
186 	usb_config_descriptor_t *cdesc;
187 	usb_interface_descriptor_t *id;
188 	usb_endpoint_descriptor_t *ed;
189 
190 	char devinfo[1024];
191 	char *devname = USBDEVNAME(sc->sc_dev);
192 	usbd_status err;
193 	int i;
194 	struct ucom_attach_args uca;
195 
196         usbd_devinfo(dev, 0, devinfo);
197         USB_ATTACH_SETUP;
198         printf("%s: %s\n", devname, devinfo);
199 
200         sc->sc_udev = dev;
201 
202 	DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
203 
204 	/* initialize endpoints */
205 	uca.bulkin = uca.bulkout = -1;
206 	sc->sc_intr_number = -1;
207 	sc->sc_intr_pipe = NULL;
208 
209 	/* Move the device into the configured state. */
210 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
211 	if (err) {
212 		printf("\n%s: failed to set configuration, err=%s\n",
213 			devname, usbd_errstr(err));
214 		sc->sc_dying = 1;
215 		USB_ATTACH_ERROR_RETURN;
216 	}
217 
218 	/* get the config descriptor */
219 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
220 
221 	if (cdesc == NULL) {
222 		printf("%s: failed to get configuration descriptor\n",
223 			USBDEVNAME(sc->sc_dev));
224 		sc->sc_dying = 1;
225 		USB_ATTACH_ERROR_RETURN;
226 	}
227 
228 	/* get the (first/common) interface */
229 	err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
230 							&sc->sc_iface);
231 	if (err) {
232 		printf("\n%s: failed to get interface, err=%s\n",
233 			devname, usbd_errstr(err));
234 		sc->sc_dying = 1;
235 		USB_ATTACH_ERROR_RETURN;
236 	}
237 
238 	/* Find the interrupt endpoints */
239 
240 	id = usbd_get_interface_descriptor(sc->sc_iface);
241 	sc->sc_iface_number = id->bInterfaceNumber;
242 
243 	for (i = 0; i < id->bNumEndpoints; i++) {
244 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
245 		if (ed == NULL) {
246 			printf("%s: no endpoint descriptor for %d\n",
247 				USBDEVNAME(sc->sc_dev), i);
248 			sc->sc_dying = 1;
249 			USB_ATTACH_ERROR_RETURN;
250 		}
251 
252 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
253 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
254 			sc->sc_intr_number = ed->bEndpointAddress;
255 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
256 		}
257 	}
258 
259 	if (sc->sc_intr_number== -1) {
260 		printf("%s: Could not find interrupt in\n",
261 			USBDEVNAME(sc->sc_dev));
262 		sc->sc_dying = 1;
263 		USB_ATTACH_ERROR_RETURN;
264 	}
265 
266 	/* keep interface for interrupt */
267 	sc->sc_intr_iface = sc->sc_iface;
268 
269 	/*
270 	 * USB-RSAQ1 has two interface
271 	 *
272 	 *  USB-RSAQ1       | USB-RSAQ2
273  	 * -----------------+-----------------
274 	 * Interface 0      |Interface 0
275 	 *  Interrupt(0x81) | Interrupt(0x81)
276 	 * -----------------+ BulkIN(0x02)
277 	 * Interface 1	    | BulkOUT(0x83)
278 	 *   BulkIN(0x02)   |
279 	 *   BulkOUT(0x83)  |
280 	 */
281 	if (cdesc->bNumInterface == 2) {
282 		err = usbd_device2interface_handle(dev,
283 				UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
284 		if (err) {
285 			printf("\n%s: failed to get second interface, err=%s\n",
286 							devname, usbd_errstr(err));
287 			sc->sc_dying = 1;
288 			USB_ATTACH_ERROR_RETURN;
289 		}
290 	}
291 
292 	/* Find the bulk{in,out} endpoints */
293 
294 	id = usbd_get_interface_descriptor(sc->sc_iface);
295 	sc->sc_iface_number = id->bInterfaceNumber;
296 
297 	for (i = 0; i < id->bNumEndpoints; i++) {
298 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
299 		if (ed == NULL) {
300 			printf("%s: no endpoint descriptor for %d\n",
301 				USBDEVNAME(sc->sc_dev), i);
302 			sc->sc_dying = 1;
303 			USB_ATTACH_ERROR_RETURN;
304 		}
305 
306 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
307 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
308 			uca.bulkin = ed->bEndpointAddress;
309 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
310 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
311 			uca.bulkout = ed->bEndpointAddress;
312 		}
313 	}
314 
315 	if (uca.bulkin == -1) {
316 		printf("%s: Could not find data bulk in\n",
317 			USBDEVNAME(sc->sc_dev));
318 		sc->sc_dying = 1;
319 		USB_ATTACH_ERROR_RETURN;
320 	}
321 
322 	if (uca.bulkout == -1) {
323 		printf("%s: Could not find data bulk out\n",
324 			USBDEVNAME(sc->sc_dev));
325 		sc->sc_dying = 1;
326 		USB_ATTACH_ERROR_RETURN;
327 	}
328 
329 	sc->sc_dtr = sc->sc_rts = -1;
330 	uca.portno = UCOM_UNK_PORTNO;
331 	/* bulkin, bulkout set above */
332 	uca.ibufsize = UPLCOMIBUFSIZE;
333 	uca.obufsize = UPLCOMOBUFSIZE;
334 	uca.ibufsizepad = UPLCOMIBUFSIZE;
335 	uca.opkthdrlen = 0;
336 	uca.device = dev;
337 	uca.iface = sc->sc_iface;
338 	uca.methods = &uplcom_methods;
339 	uca.arg = sc;
340 	uca.info = NULL;
341 
342 	err = uplcom_reset(sc);
343 
344 	if (err) {
345 		printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev),
346 			usbd_errstr(err));
347 		sc->sc_dying = 1;
348 		USB_ATTACH_ERROR_RETURN;
349 	}
350 
351 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
352 			   USBDEV(sc->sc_dev));
353 
354 	DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
355 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
356 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
357 
358 	USB_ATTACH_SUCCESS_RETURN;
359 }
360 
361 USB_DETACH(uplcom)
362 {
363 	USB_DETACH_START(uplcom, sc);
364 	int rv = 0;
365 
366 	DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
367 
368         if (sc->sc_intr_pipe != NULL) {
369                 usbd_abort_pipe(sc->sc_intr_pipe);
370                 usbd_close_pipe(sc->sc_intr_pipe);
371 		free(sc->sc_intr_buf, M_USBDEV);
372                 sc->sc_intr_pipe = NULL;
373         }
374 
375 	sc->sc_dying = 1;
376 	if (sc->sc_subdev != NULL) {
377 		rv = config_detach(sc->sc_subdev, flags);
378 		sc->sc_subdev = NULL;
379 	}
380 
381 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
382 			   USBDEV(sc->sc_dev));
383 
384 	return (rv);
385 }
386 
387 int
388 uplcom_activate(device_ptr_t self, enum devact act)
389 {
390 	struct uplcom_softc *sc = (struct uplcom_softc *)self;
391 	int rv = 0;
392 
393 	switch (act) {
394 	case DVACT_ACTIVATE:
395 		return (EOPNOTSUPP);
396 		break;
397 
398 	case DVACT_DEACTIVATE:
399 		if (sc->sc_subdev != NULL)
400 			rv = config_deactivate(sc->sc_subdev);
401 		sc->sc_dying = 1;
402 		break;
403 	}
404 	return (rv);
405 }
406 
407 usbd_status
408 uplcom_reset(struct uplcom_softc *sc)
409 {
410         usb_device_request_t req;
411 	usbd_status err;
412 
413         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
414         req.bRequest = UPLCOM_SET_REQUEST;
415         USETW(req.wValue, 0);
416         USETW(req.wIndex, sc->sc_iface_number);
417         USETW(req.wLength, 0);
418 
419         err = usbd_do_request(sc->sc_udev, &req, 0);
420 	if (err)
421 		return (EIO);
422 
423 	return (0);
424 }
425 
426 void
427 uplcom_set_line_state(struct uplcom_softc *sc)
428 {
429 	usb_device_request_t req;
430 	int ls;
431 
432 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
433 		(sc->sc_rts ? UCDC_LINE_RTS : 0);
434 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
435 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
436 	USETW(req.wValue, ls);
437 	USETW(req.wIndex, sc->sc_iface_number);
438 	USETW(req.wLength, 0);
439 
440 	(void)usbd_do_request(sc->sc_udev, &req, 0);
441 
442 }
443 
444 void
445 uplcom_set(void *addr, int portno, int reg, int onoff)
446 {
447 	struct uplcom_softc *sc = addr;
448 
449 	switch (reg) {
450 	case UCOM_SET_DTR:
451 		uplcom_dtr(sc, onoff);
452 		break;
453 	case UCOM_SET_RTS:
454 		uplcom_rts(sc, onoff);
455 		break;
456 	case UCOM_SET_BREAK:
457 		uplcom_break(sc, onoff);
458 		break;
459 	default:
460 		break;
461 	}
462 }
463 
464 void
465 uplcom_dtr(struct uplcom_softc *sc, int onoff)
466 {
467 
468 	DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
469 
470 	if (sc->sc_dtr == onoff)
471 		return;
472 	sc->sc_dtr = onoff;
473 
474 	uplcom_set_line_state(sc);
475 }
476 
477 void
478 uplcom_rts(struct uplcom_softc *sc, int onoff)
479 {
480 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
481 
482 	if (sc->sc_rts == onoff)
483 		return;
484 	sc->sc_rts = onoff;
485 
486 	uplcom_set_line_state(sc);
487 }
488 
489 void
490 uplcom_break(struct uplcom_softc *sc, int onoff)
491 {
492 	usb_device_request_t req;
493 
494 	DPRINTF(("uplcom_break: onoff=%d\n", onoff));
495 
496 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
497 	req.bRequest = UCDC_SEND_BREAK;
498 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
499 	USETW(req.wIndex, sc->sc_iface_number);
500 	USETW(req.wLength, 0);
501 
502 	(void)usbd_do_request(sc->sc_udev, &req, 0);
503 }
504 
505 usbd_status
506 uplcom_set_crtscts(struct uplcom_softc *sc)
507 {
508 	usb_device_request_t req;
509 	usbd_status err;
510 
511 	DPRINTF(("uplcom_set_crtscts: on\n"));
512 
513 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
514 	req.bRequest = UPLCOM_SET_REQUEST;
515 	USETW(req.wValue, 0);
516 	USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
517 	USETW(req.wLength, 0);
518 
519 	err = usbd_do_request(sc->sc_udev, &req, 0);
520 	if (err) {
521 		DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
522 			usbd_errstr(err)));
523 		return (err);
524 	}
525 
526 	return (USBD_NORMAL_COMPLETION);
527 }
528 
529 usbd_status
530 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
531 {
532 	usb_device_request_t req;
533 	usbd_status err;
534 
535 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
536 		UGETDW(state->dwDTERate), state->bCharFormat,
537 		state->bParityType, state->bDataBits));
538 
539 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
540 		DPRINTF(("uplcom_set_line_coding: already set\n"));
541 		return (USBD_NORMAL_COMPLETION);
542 	}
543 
544 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
545 	req.bRequest = UCDC_SET_LINE_CODING;
546 	USETW(req.wValue, 0);
547 	USETW(req.wIndex, sc->sc_iface_number);
548 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
549 
550 	err = usbd_do_request(sc->sc_udev, &req, state);
551 	if (err) {
552 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
553 			usbd_errstr(err)));
554 		return (err);
555 	}
556 
557 	sc->sc_line_state = *state;
558 
559 	return (USBD_NORMAL_COMPLETION);
560 }
561 
562 int
563 uplcom_param(void *addr, int portno, struct termios *t)
564 {
565 	struct uplcom_softc *sc = addr;
566 	usbd_status err;
567 	usb_cdc_line_state_t ls;
568 
569 	DPRINTF(("uplcom_param: sc=%p\n", sc));
570 
571 	USETDW(ls.dwDTERate, t->c_ospeed);
572 	if (ISSET(t->c_cflag, CSTOPB))
573 		ls.bCharFormat = UCDC_STOP_BIT_2;
574 	else
575 		ls.bCharFormat = UCDC_STOP_BIT_1;
576 	if (ISSET(t->c_cflag, PARENB)) {
577 		if (ISSET(t->c_cflag, PARODD))
578 			ls.bParityType = UCDC_PARITY_ODD;
579 		else
580 			ls.bParityType = UCDC_PARITY_EVEN;
581 	} else
582 		ls.bParityType = UCDC_PARITY_NONE;
583 	switch (ISSET(t->c_cflag, CSIZE)) {
584 	case CS5:
585 		ls.bDataBits = 5;
586 		break;
587 	case CS6:
588 		ls.bDataBits = 6;
589 		break;
590 	case CS7:
591 		ls.bDataBits = 7;
592 		break;
593 	case CS8:
594 		ls.bDataBits = 8;
595 		break;
596 	}
597 
598 	err = uplcom_set_line_coding(sc, &ls);
599 	if (err) {
600 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
601 		return (EIO);
602 	}
603 
604 	if (ISSET(t->c_cflag, CRTSCTS))
605 		uplcom_set_crtscts(sc);
606 
607 	if (err) {
608 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
609 		return (EIO);
610 	}
611 
612 	return (0);
613 }
614 
615 int
616 uplcom_open(void *addr, int portno)
617 {
618 	struct uplcom_softc *sc = addr;
619 	int err;
620 
621 	if (sc->sc_dying)
622 		return (EIO);
623 
624 	DPRINTF(("uplcom_open: sc=%p\n", sc));
625 
626 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
627 		sc->sc_status = 0; /* clear status bit */
628 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
629 		err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
630 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
631 			sc->sc_intr_buf, sc->sc_isize,
632 			uplcom_intr, USBD_DEFAULT_INTERVAL);
633 		if (err) {
634 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
635 				USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
636 					return (EIO);
637 		}
638 	}
639 
640 	return (0);
641 }
642 
643 void
644 uplcom_close(void *addr, int portno)
645 {
646 	struct uplcom_softc *sc = addr;
647 	int err;
648 
649 	if (sc->sc_dying)
650 		return;
651 
652 	DPRINTF(("uplcom_close: close\n"));
653 
654 	if (sc->sc_intr_pipe != NULL) {
655 		err = usbd_abort_pipe(sc->sc_intr_pipe);
656 		if (err)
657 			printf("%s: abort interrupt pipe failed: %s\n",
658 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
659 		err = usbd_close_pipe(sc->sc_intr_pipe);
660 		if (err)
661 			printf("%s: close interrupt pipe failed: %s\n",
662 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
663 		free(sc->sc_intr_buf, M_USBDEV);
664 		sc->sc_intr_pipe = NULL;
665 	}
666 }
667 
668 void
669 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
670 {
671 	struct uplcom_softc *sc = priv;
672 	u_char *buf = sc->sc_intr_buf;
673 	u_char pstatus;
674 
675 	if (sc->sc_dying)
676 		return;
677 
678 	if (status != USBD_NORMAL_COMPLETION) {
679 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
680 			return;
681 
682 		DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
683 			usbd_errstr(status)));
684 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
685 		return;
686 	}
687 
688 	DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc->sc_dev), buf[8]));
689 
690 	sc->sc_lsr = sc->sc_msr = 0;
691 	pstatus = buf[8];
692 	if (ISSET(pstatus, RSAQ_STATUS_DSR))
693 		sc->sc_msr |= UMSR_DSR;
694 	if (ISSET(pstatus, RSAQ_STATUS_DCD))
695 		sc->sc_msr |= UMSR_DCD;
696 	ucom_status_change((struct ucom_softc *) sc->sc_subdev);
697 }
698 
699 void
700 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
701 {
702 	struct uplcom_softc *sc = addr;
703 
704 	DPRINTF(("uplcom_get_status:\n"));
705 
706 	if (lsr != NULL)
707 		*lsr = sc->sc_lsr;
708 	if (msr != NULL)
709 		*msr = sc->sc_msr;
710 }
711 
712 #if TODO
713 int
714 uplcom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
715 	     usb_proc_ptr p)
716 {
717 	struct uplcom_softc *sc = addr;
718 	int error = 0;
719 
720 	if (sc->sc_dying)
721 		return (EIO);
722 
723 	DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
724 
725 	switch (cmd) {
726 	case TIOCNOTTY:
727 	case TIOCMGET:
728 	case TIOCMSET:
729 	case USB_GET_CM_OVER_DATA:
730 	case USB_SET_CM_OVER_DATA:
731 		break;
732 
733 	default:
734 		DPRINTF(("uplcom_ioctl: unknown\n"));
735 		error = ENOTTY;
736 		break;
737 	}
738 
739 	return (error);
740 }
741 #endif
742