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