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