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