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