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