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