xref: /openbsd-src/sys/dev/usb/uvscom.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /*	$OpenBSD: uvscom.c,v 1.33 2015/03/14 03:38:50 jsg Exp $ */
2 /*	$NetBSD: uvscom.c,v 1.9 2003/02/12 15:36:20 ichiro Exp $	*/
3 /*-
4  * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/dev/usb/uvscom.c,v 1.1 2002/03/18 18:23:39 joe Exp $
29  */
30 
31 /*
32  * uvscom: SUNTAC Slipper U VS-10U driver.
33  * Slipper U is a PC card to USB converter for data communication card
34  * adapter.  It supports DDI Pocket's Air H" C@rd, C@rd H" 64, NTT's P-in,
35  * P-in m@ater and various data communication card adapters.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/fcntl.h>
43 #include <sys/conf.h>
44 #include <sys/tty.h>
45 #include <sys/file.h>
46 #include <sys/ioctl.h>
47 #include <sys/device.h>
48 #include <sys/poll.h>
49 
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbcdc.h>
52 
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbdi_util.h>
55 #include <dev/usb/usbdevs.h>
56 
57 #include <dev/usb/ucomvar.h>
58 
59 #ifdef UVSCOM_DEBUG
60 static int	uvscomdebug = 1;
61 
62 #define DPRINTFN(n, x)  do { if (uvscomdebug > (n)) printf x; } while (0)
63 #else
64 #define DPRINTFN(n, x)
65 #endif
66 #define DPRINTF(x) DPRINTFN(0, x)
67 
68 #define	UVSCOM_CONFIG_INDEX	0
69 #define	UVSCOM_IFACE_INDEX	0
70 
71 #define UVSCOM_INTR_INTERVAL	100	/* mS */
72 
73 #define UVSCOM_UNIT_WAIT	5
74 
75 /* Request */
76 #define UVSCOM_SET_SPEED	0x10
77 #define UVSCOM_LINE_CTL		0x11
78 #define UVSCOM_SET_PARAM	0x12
79 #define UVSCOM_READ_STATUS	0xd0
80 #define UVSCOM_SHUTDOWN		0xe0
81 
82 /* UVSCOM_SET_SPEED parameters */
83 #define UVSCOM_SPEED_150BPS	0x00
84 #define UVSCOM_SPEED_300BPS	0x01
85 #define UVSCOM_SPEED_600BPS	0x02
86 #define UVSCOM_SPEED_1200BPS	0x03
87 #define UVSCOM_SPEED_2400BPS	0x04
88 #define UVSCOM_SPEED_4800BPS	0x05
89 #define UVSCOM_SPEED_9600BPS	0x06
90 #define UVSCOM_SPEED_19200BPS	0x07
91 #define UVSCOM_SPEED_38400BPS	0x08
92 #define UVSCOM_SPEED_57600BPS	0x09
93 #define UVSCOM_SPEED_115200BPS	0x0a
94 
95 /* UVSCOM_LINE_CTL parameters */
96 #define UVSCOM_BREAK		0x40
97 #define UVSCOM_RTS		0x02
98 #define UVSCOM_DTR		0x01
99 #define UVSCOM_LINE_INIT	0x08
100 
101 /* UVSCOM_SET_PARAM parameters */
102 #define UVSCOM_DATA_MASK	0x03
103 #define UVSCOM_DATA_BIT_8	0x03
104 #define UVSCOM_DATA_BIT_7	0x02
105 #define UVSCOM_DATA_BIT_6	0x01
106 #define UVSCOM_DATA_BIT_5	0x00
107 
108 #define UVSCOM_STOP_MASK	0x04
109 #define UVSCOM_STOP_BIT_2	0x04
110 #define UVSCOM_STOP_BIT_1	0x00
111 
112 #define UVSCOM_PARITY_MASK	0x18
113 #define UVSCOM_PARITY_EVEN	0x18
114 #if 0
115 #define UVSCOM_PARITY_UNK	0x10
116 #endif
117 #define UVSCOM_PARITY_ODD	0x08
118 #define UVSCOM_PARITY_NONE	0x00
119 
120 /* Status bits */
121 #define UVSCOM_TXRDY		0x04
122 #define UVSCOM_RXRDY		0x01
123 
124 #define UVSCOM_DCD		0x08
125 #define UVSCOM_NOCARD		0x04
126 #define UVSCOM_DSR		0x02
127 #define UVSCOM_CTS		0x01
128 #define UVSCOM_USTAT_MASK	(UVSCOM_NOCARD | UVSCOM_DSR | UVSCOM_CTS)
129 
130 struct	uvscom_softc {
131 	struct device		sc_dev;		/* base device */
132 	struct usbd_device	*sc_udev;	/* USB device */
133 	struct usbd_interface	*sc_iface;	/* interface */
134 	int			sc_iface_number;/* interface number */
135 
136 	struct usbd_interface	*sc_intr_iface;	/* interrupt interface */
137 	int			sc_intr_number;	/* interrupt number */
138 	struct usbd_pipe	*sc_intr_pipe;	/* interrupt pipe */
139 	u_char			*sc_intr_buf;	/* interrupt buffer */
140 	int			sc_isize;
141 
142 	u_char			sc_dtr;		/* current DTR state */
143 	u_char			sc_rts;		/* current RTS state */
144 
145 	u_char			sc_lsr;		/* Local status register */
146 	u_char			sc_msr;		/* uvscom status register */
147 
148 	uint16_t		sc_lcr;		/* Line control */
149 	u_char			sc_usr;		/* unit status */
150 
151 	struct device		*sc_subdev;	/* ucom device */
152 };
153 
154 /*
155  * These are the maximum number of bytes transferred per frame.
156  * The output buffer size cannot be increased due to the size encoding.
157  */
158 #define UVSCOMIBUFSIZE 512
159 #define UVSCOMOBUFSIZE 64
160 
161 usbd_status uvscom_readstat(struct uvscom_softc *);
162 usbd_status uvscom_shutdown(struct uvscom_softc *);
163 usbd_status uvscom_reset(struct uvscom_softc *);
164 usbd_status uvscom_set_line_coding(struct uvscom_softc *,
165 					   uint16_t, uint16_t);
166 usbd_status uvscom_set_line(struct uvscom_softc *, uint16_t);
167 usbd_status uvscom_set_crtscts(struct uvscom_softc *);
168 void uvscom_get_status(void *, int, u_char *, u_char *);
169 void uvscom_dtr(struct uvscom_softc *, int);
170 void uvscom_rts(struct uvscom_softc *, int);
171 void uvscom_break(struct uvscom_softc *, int);
172 
173 void uvscom_set(void *, int, int, int);
174 void uvscom_intr(struct usbd_xfer *, void *, usbd_status);
175 int  uvscom_param(void *, int, struct termios *);
176 int  uvscom_open(void *, int);
177 void uvscom_close(void *, int);
178 
179 struct ucom_methods uvscom_methods = {
180 	uvscom_get_status,
181 	uvscom_set,
182 	uvscom_param,
183 	NULL, /* uvscom_ioctl, TODO */
184 	uvscom_open,
185 	uvscom_close,
186 	NULL,
187 	NULL
188 };
189 
190 static const struct usb_devno uvscom_devs [] = {
191 	/* SUNTAC U-Cable type A3 */
192 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_AS64LX },
193 	/* SUNTAC U-Cable type A4 */
194 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_AS144L4 },
195 	/* SUNTAC U-Cable type D2 */
196 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_DS96L },
197 	/* SUNTAC U-Cable type P1 */
198 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_PS64P1 },
199 	/* SUNTAC Slipper U  */
200 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_VS10U },
201 	/* SUNTAC Ir-Trinity */
202 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_IS96U },
203 };
204 
205 int uvscom_match(struct device *, void *, void *);
206 void uvscom_attach(struct device *, struct device *, void *);
207 int uvscom_detach(struct device *, int);
208 
209 struct cfdriver uvscom_cd = {
210 	NULL, "uvscom", DV_DULL
211 };
212 
213 const struct cfattach uvscom_ca = {
214 	sizeof(struct uvscom_softc), uvscom_match, uvscom_attach, uvscom_detach
215 };
216 
217 int
218 uvscom_match(struct device *parent, void *match, void *aux)
219 {
220 	struct usb_attach_arg *uaa = aux;
221 
222 	if (uaa->iface != NULL)
223 		return (UMATCH_NONE);
224 
225 	return (usb_lookup(uvscom_devs, uaa->vendor, uaa->product) != NULL ?
226 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
227 }
228 
229 void
230 uvscom_attach(struct device *parent, struct device *self, void *aux)
231 {
232 	struct uvscom_softc *sc = (struct uvscom_softc *)self;
233 	struct usb_attach_arg *uaa = aux;
234 	struct usbd_device *dev = uaa->device;
235 	usb_config_descriptor_t *cdesc;
236 	usb_interface_descriptor_t *id;
237 	usb_endpoint_descriptor_t *ed;
238 	const char *devname = sc->sc_dev.dv_xname;
239 	usbd_status err;
240 	int i;
241 	struct ucom_attach_args uca;
242 
243         sc->sc_udev = dev;
244 
245 	DPRINTF(("uvscom attach: sc = %p\n", sc));
246 
247 	/* initialize endpoints */
248 	uca.bulkin = uca.bulkout = -1;
249 	sc->sc_intr_number = -1;
250 	sc->sc_intr_pipe = NULL;
251 
252 	/* Move the device into the configured state. */
253 	err = usbd_set_config_index(dev, UVSCOM_CONFIG_INDEX, 1);
254 	if (err) {
255 		printf("%s: failed to set configuration, err=%s\n",
256 			devname, usbd_errstr(err));
257 		usbd_deactivate(sc->sc_udev);
258 		return;
259 	}
260 
261 	/* get the config descriptor */
262 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
263 
264 	if (cdesc == NULL) {
265 		printf("%s: failed to get configuration descriptor\n",
266 			sc->sc_dev.dv_xname);
267 		usbd_deactivate(sc->sc_udev);
268 		return;
269 	}
270 
271 	/* get the common interface */
272 	err = usbd_device2interface_handle(dev, UVSCOM_IFACE_INDEX,
273 					   &sc->sc_iface);
274 	if (err) {
275 		printf("%s: failed to get interface, err=%s\n",
276 			devname, usbd_errstr(err));
277 		usbd_deactivate(sc->sc_udev);
278 		return;
279 	}
280 
281 	id = usbd_get_interface_descriptor(sc->sc_iface);
282 	sc->sc_iface_number = id->bInterfaceNumber;
283 
284 	/* Find endpoints */
285 	for (i = 0; i < id->bNumEndpoints; i++) {
286 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
287 		if (ed == NULL) {
288 			printf("%s: no endpoint descriptor for %d\n",
289 				sc->sc_dev.dv_xname, i);
290 			usbd_deactivate(sc->sc_udev);
291 			return;
292 		}
293 
294 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
295 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
296 			uca.bulkin = ed->bEndpointAddress;
297 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
298 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
299 			uca.bulkout = ed->bEndpointAddress;
300 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
301 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
302 			sc->sc_intr_number = ed->bEndpointAddress;
303 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
304 		}
305 	}
306 
307 	if (uca.bulkin == -1) {
308 		printf("%s: Could not find data bulk in\n",
309 			sc->sc_dev.dv_xname);
310 		usbd_deactivate(sc->sc_udev);
311 		return;
312 	}
313 	if (uca.bulkout == -1) {
314 		printf("%s: Could not find data bulk out\n",
315 			sc->sc_dev.dv_xname);
316 		usbd_deactivate(sc->sc_udev);
317 		return;
318 	}
319 	if (sc->sc_intr_number == -1) {
320 		printf("%s: Could not find interrupt in\n",
321 			sc->sc_dev.dv_xname);
322 		usbd_deactivate(sc->sc_udev);
323 		return;
324 	}
325 
326 	sc->sc_dtr = sc->sc_rts = 0;
327 	sc->sc_lcr = UVSCOM_LINE_INIT;
328 
329 	uca.portno = UCOM_UNK_PORTNO;
330 	/* bulkin, bulkout set above */
331 	uca.ibufsize = UVSCOMIBUFSIZE;
332 	uca.obufsize = UVSCOMOBUFSIZE;
333 	uca.ibufsizepad = UVSCOMIBUFSIZE;
334 	uca.opkthdrlen = 0;
335 	uca.device = dev;
336 	uca.iface = sc->sc_iface;
337 	uca.methods = &uvscom_methods;
338 	uca.arg = sc;
339 	uca.info = NULL;
340 
341 	err = uvscom_reset(sc);
342 
343 	if (err) {
344 		printf("%s: reset failed, %s\n", sc->sc_dev.dv_xname,
345 			usbd_errstr(err));
346 		usbd_deactivate(sc->sc_udev);
347 		return;
348 	}
349 
350 	DPRINTF(("uvscom: in = 0x%x out = 0x%x intr = 0x%x\n",
351 		 ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
352 
353 	DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
354 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
355 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
356 }
357 
358 int
359 uvscom_detach(struct device *self, int flags)
360 {
361 	struct uvscom_softc *sc = (struct uvscom_softc *)self;
362 	int rv = 0;
363 
364 	DPRINTF(("uvscom_detach: sc = %p\n", sc));
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, 0);
370 		sc->sc_intr_pipe = NULL;
371 	}
372 
373 	if (sc->sc_subdev != NULL) {
374 		rv = config_detach(sc->sc_subdev, flags);
375 		sc->sc_subdev = NULL;
376 	}
377 
378 	return (rv);
379 }
380 
381 usbd_status
382 uvscom_readstat(struct uvscom_softc *sc)
383 {
384 	usb_device_request_t req;
385 	usbd_status err;
386 	uint16_t r;
387 
388 	DPRINTF(("%s: send readstat\n", sc->sc_dev.dv_xname));
389 
390 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
391 	req.bRequest = UVSCOM_READ_STATUS;
392 	USETW(req.wValue, 0);
393 	USETW(req.wIndex, 0);
394 	USETW(req.wLength, 2);
395 
396 	err = usbd_do_request(sc->sc_udev, &req, &r);
397 	if (err) {
398 		printf("%s: uvscom_readstat: %s\n",
399 		       sc->sc_dev.dv_xname, usbd_errstr(err));
400 		return (err);
401 	}
402 
403 	DPRINTF(("%s: uvscom_readstat: r = %d\n",
404 		 sc->sc_dev.dv_xname, r));
405 
406 	return (USBD_NORMAL_COMPLETION);
407 }
408 
409 usbd_status
410 uvscom_shutdown(struct uvscom_softc *sc)
411 {
412 	usb_device_request_t req;
413 	usbd_status err;
414 
415 	DPRINTF(("%s: send shutdown\n", sc->sc_dev.dv_xname));
416 
417 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
418 	req.bRequest = UVSCOM_SHUTDOWN;
419 	USETW(req.wValue, 0);
420 	USETW(req.wIndex, 0);
421 	USETW(req.wLength, 0);
422 
423 	err = usbd_do_request(sc->sc_udev, &req, NULL);
424 	if (err) {
425 		printf("%s: uvscom_shutdown: %s\n",
426 		       sc->sc_dev.dv_xname, usbd_errstr(err));
427 		return (err);
428 	}
429 
430 	return (USBD_NORMAL_COMPLETION);
431 }
432 
433 usbd_status
434 uvscom_reset(struct uvscom_softc *sc)
435 {
436 	DPRINTF(("%s: uvscom_reset\n", sc->sc_dev.dv_xname));
437 
438 	return (USBD_NORMAL_COMPLETION);
439 }
440 
441 usbd_status
442 uvscom_set_crtscts(struct uvscom_softc *sc)
443 {
444 	DPRINTF(("%s: uvscom_set_crtscts\n", sc->sc_dev.dv_xname));
445 
446 	return (USBD_NORMAL_COMPLETION);
447 }
448 
449 usbd_status
450 uvscom_set_line(struct uvscom_softc *sc, uint16_t line)
451 {
452 	usb_device_request_t req;
453 	usbd_status err;
454 
455 	DPRINTF(("%s: uvscom_set_line: %04x\n",
456 		 sc->sc_dev.dv_xname, line));
457 
458 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
459 	req.bRequest = UVSCOM_LINE_CTL;
460 	USETW(req.wValue, line);
461 	USETW(req.wIndex, 0);
462 	USETW(req.wLength, 0);
463 
464 	err = usbd_do_request(sc->sc_udev, &req, NULL);
465 	if (err) {
466 		printf("%s: uvscom_set_line: %s\n",
467 		       sc->sc_dev.dv_xname, usbd_errstr(err));
468 		return (err);
469 	}
470 
471 	return (USBD_NORMAL_COMPLETION);
472 }
473 
474 usbd_status
475 uvscom_set_line_coding(struct uvscom_softc *sc, uint16_t lsp, uint16_t ls)
476 {
477 	usb_device_request_t req;
478 	usbd_status err;
479 
480 	DPRINTF(("%s: uvscom_set_line_coding: %02x %02x\n",
481 		 sc->sc_dev.dv_xname, lsp, ls));
482 
483 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
484 	req.bRequest = UVSCOM_SET_SPEED;
485 	USETW(req.wValue, lsp);
486 	USETW(req.wIndex, 0);
487 	USETW(req.wLength, 0);
488 
489 	err = usbd_do_request(sc->sc_udev, &req, NULL);
490 	if (err) {
491 		printf("%s: uvscom_set_line_coding: %s\n",
492 		       sc->sc_dev.dv_xname, usbd_errstr(err));
493 		return (err);
494 	}
495 
496 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
497 	req.bRequest = UVSCOM_SET_PARAM;
498 	USETW(req.wValue, ls);
499 	USETW(req.wIndex, 0);
500 	USETW(req.wLength, 0);
501 
502 	err = usbd_do_request(sc->sc_udev, &req, NULL);
503 	if (err) {
504 		printf("%s: uvscom_set_line_coding: %s\n",
505 		       sc->sc_dev.dv_xname, usbd_errstr(err));
506 		return (err);
507 	}
508 
509 	return (USBD_NORMAL_COMPLETION);
510 }
511 
512 void
513 uvscom_dtr(struct uvscom_softc *sc, int onoff)
514 {
515 	DPRINTF(("%s: uvscom_dtr: onoff = %d\n",
516 		 sc->sc_dev.dv_xname, onoff));
517 
518 	if (sc->sc_dtr == onoff)
519 		return;			/* no change */
520 
521 	sc->sc_dtr = onoff;
522 
523 	if (onoff)
524 		SET(sc->sc_lcr, UVSCOM_DTR);
525 	else
526 		CLR(sc->sc_lcr, UVSCOM_DTR);
527 
528 	uvscom_set_line(sc, sc->sc_lcr);
529 }
530 
531 void
532 uvscom_rts(struct uvscom_softc *sc, int onoff)
533 {
534 	DPRINTF(("%s: uvscom_rts: onoff = %d\n",
535 		 sc->sc_dev.dv_xname, onoff));
536 
537 	if (sc->sc_rts == onoff)
538 		return;			/* no change */
539 
540 	sc->sc_rts = onoff;
541 
542 	if (onoff)
543 		SET(sc->sc_lcr, UVSCOM_RTS);
544 	else
545 		CLR(sc->sc_lcr, UVSCOM_RTS);
546 
547 	uvscom_set_line(sc, sc->sc_lcr);
548 }
549 
550 void
551 uvscom_break(struct uvscom_softc *sc, int onoff)
552 {
553 	DPRINTF(("%s: uvscom_break: onoff = %d\n",
554 		 sc->sc_dev.dv_xname, onoff));
555 
556 	if (onoff)
557 		uvscom_set_line(sc, SET(sc->sc_lcr, UVSCOM_BREAK));
558 }
559 
560 void
561 uvscom_set(void *addr, int portno, int reg, int onoff)
562 {
563 	struct uvscom_softc *sc = addr;
564 
565 	switch (reg) {
566 	case UCOM_SET_DTR:
567 		uvscom_dtr(sc, onoff);
568 		break;
569 	case UCOM_SET_RTS:
570 		uvscom_rts(sc, onoff);
571 		break;
572 	case UCOM_SET_BREAK:
573 		uvscom_break(sc, onoff);
574 		break;
575 	default:
576 		break;
577 	}
578 }
579 
580 int
581 uvscom_param(void *addr, int portno, struct termios *t)
582 {
583 	struct uvscom_softc *sc = addr;
584 	usbd_status err;
585 	uint16_t lsp;
586 	uint16_t ls;
587 
588 	DPRINTF(("%s: uvscom_param: sc = %p\n",
589 		 sc->sc_dev.dv_xname, sc));
590 
591 	ls = 0;
592 
593 	switch (t->c_ospeed) {
594 	case B150:
595 		lsp = UVSCOM_SPEED_150BPS;
596 		break;
597 	case B300:
598 		lsp = UVSCOM_SPEED_300BPS;
599 		break;
600 	case B600:
601 		lsp = UVSCOM_SPEED_600BPS;
602 		break;
603 	case B1200:
604 		lsp = UVSCOM_SPEED_1200BPS;
605 		break;
606 	case B2400:
607 		lsp = UVSCOM_SPEED_2400BPS;
608 		break;
609 	case B4800:
610 		lsp = UVSCOM_SPEED_4800BPS;
611 		break;
612 	case B9600:
613 		lsp = UVSCOM_SPEED_9600BPS;
614 		break;
615 	case B19200:
616 		lsp = UVSCOM_SPEED_19200BPS;
617 		break;
618 	case B38400:
619 		lsp = UVSCOM_SPEED_38400BPS;
620 		break;
621 	case B57600:
622 		lsp = UVSCOM_SPEED_57600BPS;
623 		break;
624 	case B115200:
625 		lsp = UVSCOM_SPEED_115200BPS;
626 		break;
627 	default:
628 		return (EIO);
629 	}
630 
631 	if (ISSET(t->c_cflag, CSTOPB))
632 		SET(ls, UVSCOM_STOP_BIT_2);
633 	else
634 		SET(ls, UVSCOM_STOP_BIT_1);
635 
636 	if (ISSET(t->c_cflag, PARENB)) {
637 		if (ISSET(t->c_cflag, PARODD))
638 			SET(ls, UVSCOM_PARITY_ODD);
639 		else
640 			SET(ls, UVSCOM_PARITY_EVEN);
641 	} else
642 		SET(ls, UVSCOM_PARITY_NONE);
643 
644 	switch (ISSET(t->c_cflag, CSIZE)) {
645 	case CS5:
646 		SET(ls, UVSCOM_DATA_BIT_5);
647 		break;
648 	case CS6:
649 		SET(ls, UVSCOM_DATA_BIT_6);
650 		break;
651 	case CS7:
652 		SET(ls, UVSCOM_DATA_BIT_7);
653 		break;
654 	case CS8:
655 		SET(ls, UVSCOM_DATA_BIT_8);
656 		break;
657 	default:
658 		return (EIO);
659 	}
660 
661 	err = uvscom_set_line_coding(sc, lsp, ls);
662 	if (err)
663 		return (EIO);
664 
665 	if (ISSET(t->c_cflag, CRTSCTS)) {
666 		err = uvscom_set_crtscts(sc);
667 		if (err)
668 			return (EIO);
669 	}
670 
671 	return (0);
672 }
673 
674 int
675 uvscom_open(void *addr, int portno)
676 {
677 	struct uvscom_softc *sc = addr;
678 	int err;
679 	int i;
680 
681 	if (usbd_is_dying(sc->sc_udev))
682 		return (EIO);
683 
684 	DPRINTF(("uvscom_open: sc = %p\n", sc));
685 
686 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
687 		DPRINTF(("uvscom_open: open interrupt pipe.\n"));
688 
689 		sc->sc_usr = 0;		/* clear unit status */
690 
691 		err = uvscom_readstat(sc);
692 		if (err) {
693 			DPRINTF(("%s: uvscom_open: readstat faild\n",
694 				 sc->sc_dev.dv_xname));
695 			return (EIO);
696 		}
697 
698 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
699 		err = usbd_open_pipe_intr(sc->sc_iface,
700 					  sc->sc_intr_number,
701 					  USBD_SHORT_XFER_OK,
702 					  &sc->sc_intr_pipe,
703 					  sc,
704 					  sc->sc_intr_buf,
705 					  sc->sc_isize,
706 					  uvscom_intr,
707 					  UVSCOM_INTR_INTERVAL);
708 		if (err) {
709 			printf("%s: cannot open interrupt pipe (addr %d)\n",
710 				 sc->sc_dev.dv_xname,
711 				 sc->sc_intr_number);
712 			return (EIO);
713 		}
714 	} else {
715 		DPRINTF(("uvscom_open: did not open interrupt pipe.\n"));
716 	}
717 
718 	if ((sc->sc_usr & UVSCOM_USTAT_MASK) == 0) {
719 		/* unit is not ready */
720 
721 		for (i = UVSCOM_UNIT_WAIT; i > 0; --i) {
722 			tsleep(&err, TTIPRI, "uvsop", hz);	/* XXX */
723 			if (ISSET(sc->sc_usr, UVSCOM_USTAT_MASK))
724 				break;
725 		}
726 		if (i == 0) {
727 			DPRINTF(("%s: unit is not ready\n",
728 				 sc->sc_dev.dv_xname));
729 			return (EIO);
730 		}
731 
732 		/* check PC card was inserted */
733 		if (ISSET(sc->sc_usr, UVSCOM_NOCARD)) {
734 			DPRINTF(("%s: no card\n",
735 				 sc->sc_dev.dv_xname));
736 			return (EIO);
737 		}
738 	}
739 
740 	return (0);
741 }
742 
743 void
744 uvscom_close(void *addr, int portno)
745 {
746 	struct uvscom_softc *sc = addr;
747 	int err;
748 
749 	if (usbd_is_dying(sc->sc_udev))
750 		return;
751 
752 	DPRINTF(("uvscom_close: close\n"));
753 
754 	uvscom_shutdown(sc);
755 
756 	if (sc->sc_intr_pipe != NULL) {
757 		usbd_abort_pipe(sc->sc_intr_pipe);
758 		err = usbd_close_pipe(sc->sc_intr_pipe);
759 		if (err)
760 			printf("%s: close interrupt pipe failed: %s\n",
761 				sc->sc_dev.dv_xname,
762 					   usbd_errstr(err));
763 		free(sc->sc_intr_buf, M_USBDEV, 0);
764 		sc->sc_intr_pipe = NULL;
765 	}
766 }
767 
768 void
769 uvscom_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
770 {
771 	struct uvscom_softc *sc = priv;
772 	u_char *buf = sc->sc_intr_buf;
773 	u_char pstatus;
774 
775 	if (usbd_is_dying(sc->sc_udev))
776 		return;
777 
778 	if (status != USBD_NORMAL_COMPLETION) {
779 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
780 			return;
781 
782 		printf("%s: uvscom_intr: abnormal status: %s\n",
783 			sc->sc_dev.dv_xname,
784 			usbd_errstr(status));
785 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
786 		return;
787 	}
788 
789 	DPRINTFN(2, ("%s: uvscom status = %02x %02x\n",
790 		 sc->sc_dev.dv_xname, buf[0], buf[1]));
791 
792 	sc->sc_lsr = sc->sc_msr = 0;
793 	sc->sc_usr = buf[1];
794 
795 	pstatus = buf[0];
796 	if (ISSET(pstatus, UVSCOM_TXRDY))
797 		SET(sc->sc_lsr, ULSR_TXRDY);
798 	if (ISSET(pstatus, UVSCOM_RXRDY))
799 		SET(sc->sc_lsr, ULSR_RXRDY);
800 
801 	pstatus = buf[1];
802 	if (ISSET(pstatus, UVSCOM_CTS))
803 		SET(sc->sc_msr, UMSR_CTS);
804 	if (ISSET(pstatus, UVSCOM_DSR))
805 		SET(sc->sc_msr, UMSR_DSR);
806 	if (ISSET(pstatus, UVSCOM_DCD))
807 		SET(sc->sc_msr, UMSR_DCD);
808 
809 	ucom_status_change((struct ucom_softc *) sc->sc_subdev);
810 }
811 
812 void
813 uvscom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
814 {
815 	struct uvscom_softc *sc = addr;
816 
817 	if (lsr != NULL)
818 		*lsr = sc->sc_lsr;
819 	if (msr != NULL)
820 		*msr = sc->sc_msr;
821 }
822