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