xref: /netbsd-src/sys/dev/usb/uvscom.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: uvscom.c,v 1.21 2008/02/18 05:24:24 dyoung 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.21 2008/02/18 05:24:24 dyoung 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 int uvscom_match(device_t, struct cfdata *, void *);
236 void uvscom_attach(device_t, device_t, void *);
237 void uvscom_childdet(device_t, device_t);
238 int uvscom_detach(device_t, int);
239 int uvscom_activate(device_t, enum devact);
240 extern struct cfdriver uvscom_cd;
241 CFATTACH_DECL2(uvscom, sizeof(struct uvscom_softc), uvscom_match,
242     uvscom_attach, uvscom_detach, uvscom_activate, NULL, uvscom_childdet);
243 
244 USB_MATCH(uvscom)
245 {
246 	USB_MATCH_START(uvscom, uaa);
247 
248 	return (uvscom_lookup(uaa->vendor, uaa->product) != NULL ?
249 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
250 }
251 
252 USB_ATTACH(uvscom)
253 {
254 	USB_ATTACH_START(uvscom, sc, uaa);
255 	usbd_device_handle dev = uaa->device;
256 	usb_config_descriptor_t *cdesc;
257 	usb_interface_descriptor_t *id;
258 	usb_endpoint_descriptor_t *ed;
259 	char *devinfop;
260 	const char *devname = USBDEVNAME(sc->sc_dev);
261 	usbd_status err;
262 	int i;
263 	struct ucom_attach_args uca;
264 
265 	devinfop = usbd_devinfo_alloc(dev, 0);
266         USB_ATTACH_SETUP;
267         printf("%s: %s\n", devname, devinfop);
268 	usbd_devinfo_free(devinfop);
269 
270         sc->sc_udev = dev;
271 
272 	DPRINTF(("uvscom attach: sc = %p\n", sc));
273 
274 	/* initialize endpoints */
275 	uca.bulkin = uca.bulkout = -1;
276 	sc->sc_intr_number = -1;
277 	sc->sc_intr_pipe = NULL;
278 
279 	/* Move the device into the configured state. */
280 	err = usbd_set_config_index(dev, UVSCOM_CONFIG_INDEX, 1);
281 	if (err) {
282 		printf("%s: failed to set configuration, err=%s\n",
283 			devname, usbd_errstr(err));
284 		sc->sc_dying = 1;
285 		USB_ATTACH_ERROR_RETURN;
286 	}
287 
288 	/* get the config descriptor */
289 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
290 
291 	if (cdesc == NULL) {
292 		printf("%s: failed to get configuration descriptor\n",
293 			USBDEVNAME(sc->sc_dev));
294 		sc->sc_dying = 1;
295 		USB_ATTACH_ERROR_RETURN;
296 	}
297 
298 	/* get the common interface */
299 	err = usbd_device2interface_handle(dev, UVSCOM_IFACE_INDEX,
300 					   &sc->sc_iface);
301 	if (err) {
302 		printf("%s: failed to get interface, err=%s\n",
303 			devname, usbd_errstr(err));
304 		sc->sc_dying = 1;
305 		USB_ATTACH_ERROR_RETURN;
306 	}
307 
308 	id = usbd_get_interface_descriptor(sc->sc_iface);
309 	sc->sc_iface_number = id->bInterfaceNumber;
310 
311 	/* Find endpoints */
312 	for (i = 0; i < id->bNumEndpoints; i++) {
313 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
314 		if (ed == NULL) {
315 			printf("%s: no endpoint descriptor for %d\n",
316 				USBDEVNAME(sc->sc_dev), i);
317 			sc->sc_dying = 1;
318 			USB_ATTACH_ERROR_RETURN;
319 		}
320 
321 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
322 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
323 			uca.bulkin = ed->bEndpointAddress;
324 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
325 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
326 			uca.bulkout = ed->bEndpointAddress;
327 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
328 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
329 			sc->sc_intr_number = ed->bEndpointAddress;
330 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
331 		}
332 	}
333 
334 	if (uca.bulkin == -1) {
335 		printf("%s: Could not find data bulk in\n",
336 			USBDEVNAME(sc->sc_dev));
337 		sc->sc_dying = 1;
338 		USB_ATTACH_ERROR_RETURN;
339 	}
340 	if (uca.bulkout == -1) {
341 		printf("%s: Could not find data bulk out\n",
342 			USBDEVNAME(sc->sc_dev));
343 		sc->sc_dying = 1;
344 		USB_ATTACH_ERROR_RETURN;
345 	}
346 	if (sc->sc_intr_number == -1) {
347 		printf("%s: Could not find interrupt in\n",
348 			USBDEVNAME(sc->sc_dev));
349 		sc->sc_dying = 1;
350 		USB_ATTACH_ERROR_RETURN;
351 	}
352 
353 	sc->sc_dtr = sc->sc_rts = 0;
354 	sc->sc_lcr = UVSCOM_LINE_INIT;
355 
356 	uca.portno = UCOM_UNK_PORTNO;
357 	/* bulkin, bulkout set above */
358 	uca.ibufsize = UVSCOMIBUFSIZE;
359 	uca.obufsize = UVSCOMOBUFSIZE;
360 	uca.ibufsizepad = UVSCOMIBUFSIZE;
361 	uca.opkthdrlen = 0;
362 	uca.device = dev;
363 	uca.iface = sc->sc_iface;
364 	uca.methods = &uvscom_methods;
365 	uca.arg = sc;
366 	uca.info = NULL;
367 
368 	err = uvscom_reset(sc);
369 
370 	if (err) {
371 		printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev),
372 			usbd_errstr(err));
373 		sc->sc_dying = 1;
374 		USB_ATTACH_ERROR_RETURN;
375 	}
376 
377 	DPRINTF(("uvscom: in = 0x%x out = 0x%x intr = 0x%x\n",
378 		 uca.bulkin, uca.bulkout, sc->sc_intr_number));
379 
380 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
381 			   USBDEV(sc->sc_dev));
382 
383 	DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
384 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
385 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
386 					    ucomprint, ucomsubmatch);
387 
388 	USB_ATTACH_SUCCESS_RETURN;
389 }
390 
391 void
392 uvscom_childdet(device_t self, device_t child)
393 {
394 	struct uvscom_softc *sc = device_private(self);
395 
396 	KASSERT(sc->sc_subdev == child);
397 	sc->sc_subdev = NULL;
398 }
399 
400 USB_DETACH(uvscom)
401 {
402 	USB_DETACH_START(uvscom, sc);
403 	int rv = 0;
404 
405 	DPRINTF(("uvscom_detach: sc = %p\n", sc));
406 
407 	sc->sc_dying = 1;
408 
409 	if (sc->sc_intr_pipe != NULL) {
410 		usbd_abort_pipe(sc->sc_intr_pipe);
411 		usbd_close_pipe(sc->sc_intr_pipe);
412 		free(sc->sc_intr_buf, M_USBDEV);
413 		sc->sc_intr_pipe = NULL;
414 	}
415 
416 	sc->sc_dying = 1;
417 	if (sc->sc_subdev != NULL)
418 		rv = config_detach(sc->sc_subdev, flags);
419 
420 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
421 			   USBDEV(sc->sc_dev));
422 
423 	return (rv);
424 }
425 
426 int
427 uvscom_activate(device_t self, enum devact act)
428 {
429 	struct uvscom_softc *sc = device_private(self);
430 	int rv = 0;
431 
432 	switch (act) {
433 	case DVACT_ACTIVATE:
434 		return (EOPNOTSUPP);
435 
436 	case DVACT_DEACTIVATE:
437 		if (sc->sc_subdev != NULL)
438 			rv = config_deactivate(sc->sc_subdev);
439 		sc->sc_dying = 1;
440 		break;
441 	}
442 	return (rv);
443 }
444 
445 Static usbd_status
446 uvscom_readstat(struct uvscom_softc *sc)
447 {
448 	usb_device_request_t req;
449 	usbd_status err;
450 	uint16_t r;
451 
452 	DPRINTF(("%s: send readstat\n", USBDEVNAME(sc->sc_dev)));
453 
454 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
455 	req.bRequest = UVSCOM_READ_STATUS;
456 	USETW(req.wValue, 0);
457 	USETW(req.wIndex, 0);
458 	USETW(req.wLength, 2);
459 
460 	err = usbd_do_request(sc->sc_udev, &req, &r);
461 	if (err) {
462 		printf("%s: uvscom_readstat: %s\n",
463 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
464 		return (err);
465 	}
466 
467 	DPRINTF(("%s: uvscom_readstat: r = %d\n",
468 		 USBDEVNAME(sc->sc_dev), r));
469 
470 	return (USBD_NORMAL_COMPLETION);
471 }
472 
473 Static usbd_status
474 uvscom_shutdown(struct uvscom_softc *sc)
475 {
476 	usb_device_request_t req;
477 	usbd_status err;
478 
479 	DPRINTF(("%s: send shutdown\n", USBDEVNAME(sc->sc_dev)));
480 
481 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
482 	req.bRequest = UVSCOM_SHUTDOWN;
483 	USETW(req.wValue, 0);
484 	USETW(req.wIndex, 0);
485 	USETW(req.wLength, 0);
486 
487 	err = usbd_do_request(sc->sc_udev, &req, NULL);
488 	if (err) {
489 		printf("%s: uvscom_shutdown: %s\n",
490 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
491 		return (err);
492 	}
493 
494 	return (USBD_NORMAL_COMPLETION);
495 }
496 
497 Static usbd_status
498 uvscom_reset(struct uvscom_softc *sc)
499 {
500 	DPRINTF(("%s: uvscom_reset\n", USBDEVNAME(sc->sc_dev)));
501 
502 	return (USBD_NORMAL_COMPLETION);
503 }
504 
505 Static usbd_status
506 uvscom_set_crtscts(struct uvscom_softc *sc)
507 {
508 	DPRINTF(("%s: uvscom_set_crtscts\n", USBDEVNAME(sc->sc_dev)));
509 
510 	return (USBD_NORMAL_COMPLETION);
511 }
512 
513 Static usbd_status
514 uvscom_set_line(struct uvscom_softc *sc, uint16_t line)
515 {
516 	usb_device_request_t req;
517 	usbd_status err;
518 
519 	DPRINTF(("%s: uvscom_set_line: %04x\n",
520 		 USBDEVNAME(sc->sc_dev), line));
521 
522 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
523 	req.bRequest = UVSCOM_LINE_CTL;
524 	USETW(req.wValue, line);
525 	USETW(req.wIndex, 0);
526 	USETW(req.wLength, 0);
527 
528 	err = usbd_do_request(sc->sc_udev, &req, NULL);
529 	if (err) {
530 		printf("%s: uvscom_set_line: %s\n",
531 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
532 		return (err);
533 	}
534 
535 	return (USBD_NORMAL_COMPLETION);
536 }
537 
538 Static usbd_status
539 uvscom_set_line_coding(struct uvscom_softc *sc, uint16_t lsp, uint16_t ls)
540 {
541 	usb_device_request_t req;
542 	usbd_status err;
543 
544 	DPRINTF(("%s: uvscom_set_line_coding: %02x %02x\n",
545 		 USBDEVNAME(sc->sc_dev), lsp, ls));
546 
547 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
548 	req.bRequest = UVSCOM_SET_SPEED;
549 	USETW(req.wValue, lsp);
550 	USETW(req.wIndex, 0);
551 	USETW(req.wLength, 0);
552 
553 	err = usbd_do_request(sc->sc_udev, &req, NULL);
554 	if (err) {
555 		printf("%s: uvscom_set_line_coding: %s\n",
556 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
557 		return (err);
558 	}
559 
560 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
561 	req.bRequest = UVSCOM_SET_PARAM;
562 	USETW(req.wValue, ls);
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 	return (USBD_NORMAL_COMPLETION);
574 }
575 
576 Static void
577 uvscom_dtr(struct uvscom_softc *sc, int onoff)
578 {
579 	DPRINTF(("%s: uvscom_dtr: onoff = %d\n",
580 		 USBDEVNAME(sc->sc_dev), onoff));
581 
582 	if (sc->sc_dtr == onoff)
583 		return;			/* no change */
584 
585 	sc->sc_dtr = onoff;
586 
587 	if (onoff)
588 		SET(sc->sc_lcr, UVSCOM_DTR);
589 	else
590 		CLR(sc->sc_lcr, UVSCOM_DTR);
591 
592 	uvscom_set_line(sc, sc->sc_lcr);
593 }
594 
595 Static void
596 uvscom_rts(struct uvscom_softc *sc, int onoff)
597 {
598 	DPRINTF(("%s: uvscom_rts: onoff = %d\n",
599 		 USBDEVNAME(sc->sc_dev), onoff));
600 
601 	if (sc->sc_rts == onoff)
602 		return;			/* no change */
603 
604 	sc->sc_rts = onoff;
605 
606 	if (onoff)
607 		SET(sc->sc_lcr, UVSCOM_RTS);
608 	else
609 		CLR(sc->sc_lcr, UVSCOM_RTS);
610 
611 	uvscom_set_line(sc, sc->sc_lcr);
612 }
613 
614 Static void
615 uvscom_break(struct uvscom_softc *sc, int onoff)
616 {
617 	DPRINTF(("%s: uvscom_break: onoff = %d\n",
618 		 USBDEVNAME(sc->sc_dev), onoff));
619 
620 	if (onoff)
621 		uvscom_set_line(sc, SET(sc->sc_lcr, UVSCOM_BREAK));
622 }
623 
624 Static void
625 uvscom_set(void *addr, int portno, int reg, int onoff)
626 {
627 	struct uvscom_softc *sc = addr;
628 
629 	switch (reg) {
630 	case UCOM_SET_DTR:
631 		uvscom_dtr(sc, onoff);
632 		break;
633 	case UCOM_SET_RTS:
634 		uvscom_rts(sc, onoff);
635 		break;
636 	case UCOM_SET_BREAK:
637 		uvscom_break(sc, onoff);
638 		break;
639 	default:
640 		break;
641 	}
642 }
643 
644 Static int
645 uvscom_param(void *addr, int portno, struct termios *t)
646 {
647 	struct uvscom_softc *sc = addr;
648 	usbd_status err;
649 	uint16_t lsp;
650 	uint16_t ls;
651 
652 	DPRINTF(("%s: uvscom_param: sc = %p\n",
653 		 USBDEVNAME(sc->sc_dev), sc));
654 
655 	ls = 0;
656 
657 	switch (t->c_ospeed) {
658 	case B150:
659 		lsp = UVSCOM_SPEED_150BPS;
660 		break;
661 	case B300:
662 		lsp = UVSCOM_SPEED_300BPS;
663 		break;
664 	case B600:
665 		lsp = UVSCOM_SPEED_600BPS;
666 		break;
667 	case B1200:
668 		lsp = UVSCOM_SPEED_1200BPS;
669 		break;
670 	case B2400:
671 		lsp = UVSCOM_SPEED_2400BPS;
672 		break;
673 	case B4800:
674 		lsp = UVSCOM_SPEED_4800BPS;
675 		break;
676 	case B9600:
677 		lsp = UVSCOM_SPEED_9600BPS;
678 		break;
679 	case B19200:
680 		lsp = UVSCOM_SPEED_19200BPS;
681 		break;
682 	case B38400:
683 		lsp = UVSCOM_SPEED_38400BPS;
684 		break;
685 	case B57600:
686 		lsp = UVSCOM_SPEED_57600BPS;
687 		break;
688 	case B115200:
689 		lsp = UVSCOM_SPEED_115200BPS;
690 		break;
691 	default:
692 		return (EIO);
693 	}
694 
695 	if (ISSET(t->c_cflag, CSTOPB))
696 		SET(ls, UVSCOM_STOP_BIT_2);
697 	else
698 		SET(ls, UVSCOM_STOP_BIT_1);
699 
700 	if (ISSET(t->c_cflag, PARENB)) {
701 		if (ISSET(t->c_cflag, PARODD))
702 			SET(ls, UVSCOM_PARITY_ODD);
703 		else
704 			SET(ls, UVSCOM_PARITY_EVEN);
705 	} else
706 		SET(ls, UVSCOM_PARITY_NONE);
707 
708 	switch (ISSET(t->c_cflag, CSIZE)) {
709 	case CS5:
710 		SET(ls, UVSCOM_DATA_BIT_5);
711 		break;
712 	case CS6:
713 		SET(ls, UVSCOM_DATA_BIT_6);
714 		break;
715 	case CS7:
716 		SET(ls, UVSCOM_DATA_BIT_7);
717 		break;
718 	case CS8:
719 		SET(ls, UVSCOM_DATA_BIT_8);
720 		break;
721 	default:
722 		return (EIO);
723 	}
724 
725 	err = uvscom_set_line_coding(sc, lsp, ls);
726 	if (err)
727 		return (EIO);
728 
729 	if (ISSET(t->c_cflag, CRTSCTS)) {
730 		err = uvscom_set_crtscts(sc);
731 		if (err)
732 			return (EIO);
733 	}
734 
735 	return (0);
736 }
737 
738 Static int
739 uvscom_open(void *addr, int portno)
740 {
741 	struct uvscom_softc *sc = addr;
742 	int err;
743 	int i;
744 
745 	if (sc->sc_dying)
746 		return (EIO);
747 
748 	DPRINTF(("uvscom_open: sc = %p\n", sc));
749 
750 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
751 		DPRINTF(("uvscom_open: open interrupt pipe.\n"));
752 
753 		sc->sc_usr = 0;		/* clear unit status */
754 
755 		err = uvscom_readstat(sc);
756 		if (err) {
757 			DPRINTF(("%s: uvscom_open: readstat faild\n",
758 				 USBDEVNAME(sc->sc_dev)));
759 			return (EIO);
760 		}
761 
762 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
763 		err = usbd_open_pipe_intr(sc->sc_iface,
764 					  sc->sc_intr_number,
765 					  USBD_SHORT_XFER_OK,
766 					  &sc->sc_intr_pipe,
767 					  sc,
768 					  sc->sc_intr_buf,
769 					  sc->sc_isize,
770 					  uvscom_intr,
771 					  UVSCOM_INTR_INTERVAL);
772 		if (err) {
773 			printf("%s: cannot open interrupt pipe (addr %d)\n",
774 				 USBDEVNAME(sc->sc_dev),
775 				 sc->sc_intr_number);
776 			return (EIO);
777 		}
778 	} else {
779 		DPRINTF(("uvscom_open: did not open interrupt pipe.\n"));
780 	}
781 
782 	if ((sc->sc_usr & UVSCOM_USTAT_MASK) == 0) {
783 		/* unit is not ready */
784 
785 		for (i = UVSCOM_UNIT_WAIT; i > 0; --i) {
786 			tsleep(&err, TTIPRI, "uvsop", hz);	/* XXX */
787 			if (ISSET(sc->sc_usr, UVSCOM_USTAT_MASK))
788 				break;
789 		}
790 		if (i == 0) {
791 			DPRINTF(("%s: unit is not ready\n",
792 				 USBDEVNAME(sc->sc_dev)));
793 			return (EIO);
794 		}
795 
796 		/* check PC card was inserted */
797 		if (ISSET(sc->sc_usr, UVSCOM_NOCARD)) {
798 			DPRINTF(("%s: no card\n",
799 				 USBDEVNAME(sc->sc_dev)));
800 			return (EIO);
801 		}
802 	}
803 
804 	return (0);
805 }
806 
807 Static void
808 uvscom_close(void *addr, int portno)
809 {
810 	struct uvscom_softc *sc = addr;
811 	int err;
812 
813 	if (sc->sc_dying)
814 		return;
815 
816 	DPRINTF(("uvscom_close: close\n"));
817 
818 	uvscom_shutdown(sc);
819 
820 	if (sc->sc_intr_pipe != NULL) {
821 		err = usbd_abort_pipe(sc->sc_intr_pipe);
822 		if (err)
823 			printf("%s: abort interrupt pipe failed: %s\n",
824 				USBDEVNAME(sc->sc_dev),
825 					   usbd_errstr(err));
826 		err = usbd_close_pipe(sc->sc_intr_pipe);
827 		if (err)
828 			printf("%s: close interrupt pipe failed: %s\n",
829 				USBDEVNAME(sc->sc_dev),
830 					   usbd_errstr(err));
831 		free(sc->sc_intr_buf, M_USBDEV);
832 		sc->sc_intr_pipe = NULL;
833 	}
834 }
835 
836 Static void
837 uvscom_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
838     usbd_status status)
839 {
840 	struct uvscom_softc *sc = priv;
841 	u_char *buf = sc->sc_intr_buf;
842 	u_char pstatus;
843 
844 	if (sc->sc_dying)
845 		return;
846 
847 	if (status != USBD_NORMAL_COMPLETION) {
848 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
849 			return;
850 
851 		printf("%s: uvscom_intr: abnormal status: %s\n",
852 			USBDEVNAME(sc->sc_dev),
853 			usbd_errstr(status));
854 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
855 		return;
856 	}
857 
858 	DPRINTFN(2, ("%s: uvscom status = %02x %02x\n",
859 		 USBDEVNAME(sc->sc_dev), buf[0], buf[1]));
860 
861 	sc->sc_lsr = sc->sc_msr = 0;
862 	sc->sc_usr = buf[1];
863 
864 	pstatus = buf[0];
865 	if (ISSET(pstatus, UVSCOM_TXRDY))
866 		SET(sc->sc_lsr, ULSR_TXRDY);
867 	if (ISSET(pstatus, UVSCOM_RXRDY))
868 		SET(sc->sc_lsr, ULSR_RXRDY);
869 
870 	pstatus = buf[1];
871 	if (ISSET(pstatus, UVSCOM_CTS))
872 		SET(sc->sc_msr, UMSR_CTS);
873 	if (ISSET(pstatus, UVSCOM_DSR))
874 		SET(sc->sc_msr, UMSR_DSR);
875 	if (ISSET(pstatus, UVSCOM_DCD))
876 		SET(sc->sc_msr, UMSR_DCD);
877 
878 	ucom_status_change((struct ucom_softc *) sc->sc_subdev);
879 }
880 
881 Static void
882 uvscom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
883 {
884 	struct uvscom_softc *sc = addr;
885 
886 	if (lsr != NULL)
887 		*lsr = sc->sc_lsr;
888 	if (msr != NULL)
889 		*msr = sc->sc_msr;
890 }
891 
892