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