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