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