xref: /netbsd/sys/dev/usb/uplcom.c (revision c4a72b64)
1 /*	$NetBSD: uplcom.c,v 1.29 2002/09/23 05:51:23 simonb 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.29 2002/09/23 05:51:23 simonb 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 
401 	case DVACT_DEACTIVATE:
402 		if (sc->sc_subdev != NULL)
403 			rv = config_deactivate(sc->sc_subdev);
404 		sc->sc_dying = 1;
405 		break;
406 	}
407 	return (rv);
408 }
409 
410 usbd_status
411 uplcom_reset(struct uplcom_softc *sc)
412 {
413         usb_device_request_t req;
414 	usbd_status err;
415 
416         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
417         req.bRequest = UPLCOM_SET_REQUEST;
418         USETW(req.wValue, 0);
419         USETW(req.wIndex, sc->sc_iface_number);
420         USETW(req.wLength, 0);
421 
422         err = usbd_do_request(sc->sc_udev, &req, 0);
423 	if (err)
424 		return (EIO);
425 
426 	return (0);
427 }
428 
429 void
430 uplcom_set_line_state(struct uplcom_softc *sc)
431 {
432 	usb_device_request_t req;
433 	int ls;
434 
435 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
436 		(sc->sc_rts ? UCDC_LINE_RTS : 0);
437 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
438 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
439 	USETW(req.wValue, ls);
440 	USETW(req.wIndex, sc->sc_iface_number);
441 	USETW(req.wLength, 0);
442 
443 	(void)usbd_do_request(sc->sc_udev, &req, 0);
444 
445 }
446 
447 void
448 uplcom_set(void *addr, int portno, int reg, int onoff)
449 {
450 	struct uplcom_softc *sc = addr;
451 
452 	switch (reg) {
453 	case UCOM_SET_DTR:
454 		uplcom_dtr(sc, onoff);
455 		break;
456 	case UCOM_SET_RTS:
457 		uplcom_rts(sc, onoff);
458 		break;
459 	case UCOM_SET_BREAK:
460 		uplcom_break(sc, onoff);
461 		break;
462 	default:
463 		break;
464 	}
465 }
466 
467 void
468 uplcom_dtr(struct uplcom_softc *sc, int onoff)
469 {
470 
471 	DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
472 
473 	if (sc->sc_dtr == onoff)
474 		return;
475 	sc->sc_dtr = onoff;
476 
477 	uplcom_set_line_state(sc);
478 }
479 
480 void
481 uplcom_rts(struct uplcom_softc *sc, int onoff)
482 {
483 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
484 
485 	if (sc->sc_rts == onoff)
486 		return;
487 	sc->sc_rts = onoff;
488 
489 	uplcom_set_line_state(sc);
490 }
491 
492 void
493 uplcom_break(struct uplcom_softc *sc, int onoff)
494 {
495 	usb_device_request_t req;
496 
497 	DPRINTF(("uplcom_break: onoff=%d\n", onoff));
498 
499 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
500 	req.bRequest = UCDC_SEND_BREAK;
501 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
502 	USETW(req.wIndex, sc->sc_iface_number);
503 	USETW(req.wLength, 0);
504 
505 	(void)usbd_do_request(sc->sc_udev, &req, 0);
506 }
507 
508 usbd_status
509 uplcom_set_crtscts(struct uplcom_softc *sc)
510 {
511 	usb_device_request_t req;
512 	usbd_status err;
513 
514 	DPRINTF(("uplcom_set_crtscts: on\n"));
515 
516 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
517 	req.bRequest = UPLCOM_SET_REQUEST;
518 	USETW(req.wValue, 0);
519 	USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
520 	USETW(req.wLength, 0);
521 
522 	err = usbd_do_request(sc->sc_udev, &req, 0);
523 	if (err) {
524 		DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
525 			usbd_errstr(err)));
526 		return (err);
527 	}
528 
529 	return (USBD_NORMAL_COMPLETION);
530 }
531 
532 usbd_status
533 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
534 {
535 	usb_device_request_t req;
536 	usbd_status err;
537 
538 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
539 		UGETDW(state->dwDTERate), state->bCharFormat,
540 		state->bParityType, state->bDataBits));
541 
542 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
543 		DPRINTF(("uplcom_set_line_coding: already set\n"));
544 		return (USBD_NORMAL_COMPLETION);
545 	}
546 
547 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
548 	req.bRequest = UCDC_SET_LINE_CODING;
549 	USETW(req.wValue, 0);
550 	USETW(req.wIndex, sc->sc_iface_number);
551 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
552 
553 	err = usbd_do_request(sc->sc_udev, &req, state);
554 	if (err) {
555 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
556 			usbd_errstr(err)));
557 		return (err);
558 	}
559 
560 	sc->sc_line_state = *state;
561 
562 	return (USBD_NORMAL_COMPLETION);
563 }
564 
565 int
566 uplcom_param(void *addr, int portno, struct termios *t)
567 {
568 	struct uplcom_softc *sc = addr;
569 	usbd_status err;
570 	usb_cdc_line_state_t ls;
571 
572 	DPRINTF(("uplcom_param: sc=%p\n", sc));
573 
574 	USETDW(ls.dwDTERate, t->c_ospeed);
575 	if (ISSET(t->c_cflag, CSTOPB))
576 		ls.bCharFormat = UCDC_STOP_BIT_2;
577 	else
578 		ls.bCharFormat = UCDC_STOP_BIT_1;
579 	if (ISSET(t->c_cflag, PARENB)) {
580 		if (ISSET(t->c_cflag, PARODD))
581 			ls.bParityType = UCDC_PARITY_ODD;
582 		else
583 			ls.bParityType = UCDC_PARITY_EVEN;
584 	} else
585 		ls.bParityType = UCDC_PARITY_NONE;
586 	switch (ISSET(t->c_cflag, CSIZE)) {
587 	case CS5:
588 		ls.bDataBits = 5;
589 		break;
590 	case CS6:
591 		ls.bDataBits = 6;
592 		break;
593 	case CS7:
594 		ls.bDataBits = 7;
595 		break;
596 	case CS8:
597 		ls.bDataBits = 8;
598 		break;
599 	}
600 
601 	err = uplcom_set_line_coding(sc, &ls);
602 	if (err) {
603 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
604 		return (EIO);
605 	}
606 
607 	if (ISSET(t->c_cflag, CRTSCTS))
608 		uplcom_set_crtscts(sc);
609 
610 	if (err) {
611 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
612 		return (EIO);
613 	}
614 
615 	return (0);
616 }
617 
618 int
619 uplcom_open(void *addr, int portno)
620 {
621 	struct uplcom_softc *sc = addr;
622 	int err;
623 
624 	if (sc->sc_dying)
625 		return (EIO);
626 
627 	DPRINTF(("uplcom_open: sc=%p\n", sc));
628 
629 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
630 		sc->sc_status = 0; /* clear status bit */
631 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
632 		err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
633 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
634 			sc->sc_intr_buf, sc->sc_isize,
635 			uplcom_intr, USBD_DEFAULT_INTERVAL);
636 		if (err) {
637 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
638 				USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
639 					return (EIO);
640 		}
641 	}
642 
643 	return (0);
644 }
645 
646 void
647 uplcom_close(void *addr, int portno)
648 {
649 	struct uplcom_softc *sc = addr;
650 	int err;
651 
652 	if (sc->sc_dying)
653 		return;
654 
655 	DPRINTF(("uplcom_close: close\n"));
656 
657 	if (sc->sc_intr_pipe != NULL) {
658 		err = usbd_abort_pipe(sc->sc_intr_pipe);
659 		if (err)
660 			printf("%s: abort interrupt pipe failed: %s\n",
661 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
662 		err = usbd_close_pipe(sc->sc_intr_pipe);
663 		if (err)
664 			printf("%s: close interrupt pipe failed: %s\n",
665 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
666 		free(sc->sc_intr_buf, M_USBDEV);
667 		sc->sc_intr_pipe = NULL;
668 	}
669 }
670 
671 void
672 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
673 {
674 	struct uplcom_softc *sc = priv;
675 	u_char *buf = sc->sc_intr_buf;
676 	u_char pstatus;
677 
678 	if (sc->sc_dying)
679 		return;
680 
681 	if (status != USBD_NORMAL_COMPLETION) {
682 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
683 			return;
684 
685 		DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
686 			usbd_errstr(status)));
687 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
688 		return;
689 	}
690 
691 	DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc->sc_dev), buf[8]));
692 
693 	sc->sc_lsr = sc->sc_msr = 0;
694 	pstatus = buf[8];
695 	if (ISSET(pstatus, RSAQ_STATUS_DSR))
696 		sc->sc_msr |= UMSR_DSR;
697 	if (ISSET(pstatus, RSAQ_STATUS_DCD))
698 		sc->sc_msr |= UMSR_DCD;
699 	ucom_status_change((struct ucom_softc *) sc->sc_subdev);
700 }
701 
702 void
703 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
704 {
705 	struct uplcom_softc *sc = addr;
706 
707 	DPRINTF(("uplcom_get_status:\n"));
708 
709 	if (lsr != NULL)
710 		*lsr = sc->sc_lsr;
711 	if (msr != NULL)
712 		*msr = sc->sc_msr;
713 }
714 
715 #if TODO
716 int
717 uplcom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
718 	     usb_proc_ptr p)
719 {
720 	struct uplcom_softc *sc = addr;
721 	int error = 0;
722 
723 	if (sc->sc_dying)
724 		return (EIO);
725 
726 	DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
727 
728 	switch (cmd) {
729 	case TIOCNOTTY:
730 	case TIOCMGET:
731 	case TIOCMSET:
732 	case USB_GET_CM_OVER_DATA:
733 	case USB_SET_CM_OVER_DATA:
734 		break;
735 
736 	default:
737 		DPRINTF(("uplcom_ioctl: unknown\n"));
738 		error = ENOTTY;
739 		break;
740 	}
741 
742 	return (error);
743 }
744 #endif
745