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