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