xref: /netbsd-src/sys/dev/usb/ubsa.c (revision 4b896b232495b7a9b8b94a1cf1e21873296d53b8)
1 /*	$NetBSD: ubsa.c,v 1.9 2004/04/23 17:25:25 itojun Exp $	*/
2 /*-
3  * Copyright (c) 2002, Alexander Kabaev <kan.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 /*
28  * Copyright (c) 2001 The NetBSD Foundation, Inc.
29  * All rights reserved.
30  *
31  * This code is derived from software contributed to The NetBSD Foundation
32  * by Ichiro FUKUHARA (ichiro@ichiro.org).
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  * 3. All advertising materials mentioning features or use of this software
43  *    must display the following acknowledgement:
44  *        This product includes software developed by the NetBSD
45  *        Foundation, Inc. and its contributors.
46  * 4. Neither the name of The NetBSD Foundation nor the names of its
47  *    contributors may be used to endorse or promote products derived
48  *    from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
51  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
54  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60  * POSSIBILITY OF SUCH DAMAGE.
61  */
62 
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: ubsa.c,v 1.9 2004/04/23 17:25:25 itojun Exp $");
65 
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/malloc.h>
70 #ifdef __FreeBSD__
71 #include <sys/bus.h>
72 #endif
73 #include <sys/ioccom.h>
74 #include <sys/fcntl.h>
75 #include <sys/conf.h>
76 #include <sys/tty.h>
77 #include <sys/file.h>
78 #if __FreeBSD_version >= 500014
79 #include <sys/selinfo.h>
80 #else
81 #include <sys/select.h>
82 #endif
83 #include <sys/proc.h>
84 #include <sys/device.h>
85 #include <sys/poll.h>
86 #include <sys/sysctl.h>
87 
88 #include <dev/usb/usb.h>
89 #include <dev/usb/usbcdc.h>
90 
91 #include <dev/usb/usbdi.h>
92 #include <dev/usb/usbdi_util.h>
93 #include <dev/usb/usbdevs.h>
94 #include <dev/usb/usb_quirks.h>
95 
96 #include <dev/usb/ucomvar.h>
97 
98 #ifdef UBSA_DEBUG
99 Static int	ubsadebug = 0;
100 #ifdef __FreeBSD__
101 SYSCTL_NODE(_hw_usb, OID_AUTO, ubsa, CTLFLAG_RW, 0, "USB ubsa");
102 SYSCTL_INT(_hw_usb_ubsa, OID_AUTO, debug, CTLFLAG_RW,
103 	   &ubsadebug, 0, "ubsa debug level");
104 #endif
105 
106 #define	DPRINTFN(n, x)	do { \
107 				if (ubsadebug > (n)) \
108 					logprintf x; \
109 			} while (0)
110 #else
111 #define	DPRINTFN(n, x)
112 #endif
113 #define	DPRINTF(x) DPRINTFN(0, x)
114 
115 #define	UBSA_MODVER		1	/* module version */
116 
117 #define	UBSA_CONFIG_INDEX	1
118 #define	UBSA_IFACE_INDEX	0
119 
120 #define	UBSA_INTR_INTERVAL	100	/* ms */
121 
122 #define	UBSA_SET_BAUDRATE  	0x00
123 #define	UBSA_SET_STOP_BITS	0x01
124 #define	UBSA_SET_DATA_BITS	0x02
125 #define	UBSA_SET_PARITY		0x03
126 #define	UBSA_SET_DTR		0x0A
127 #define	UBSA_SET_RTS		0x0B
128 #define	UBSA_SET_BREAK		0x0C
129 #define	UBSA_SET_FLOW_CTRL	0x10
130 
131 #define	UBSA_PARITY_NONE	0x00
132 #define	UBSA_PARITY_EVEN	0x01
133 #define	UBSA_PARITY_ODD		0x02
134 #define	UBSA_PARITY_MARK	0x03
135 #define	UBSA_PARITY_SPACE	0x04
136 
137 #define	UBSA_FLOW_NONE		0x0000
138 #define	UBSA_FLOW_OCTS		0x0001
139 #define	UBSA_FLOW_ODSR		0x0002
140 #define	UBSA_FLOW_IDSR		0x0004
141 #define	UBSA_FLOW_IDTR		0x0008
142 #define	UBSA_FLOW_IRTS		0x0010
143 #define	UBSA_FLOW_ORTS		0x0020
144 #define	UBSA_FLOW_UNKNOWN	0x0040
145 #define	UBSA_FLOW_OXON		0x0080
146 #define	UBSA_FLOW_IXON		0x0100
147 
148 /* line status register */
149 #define	UBSA_LSR_TSRE		0x40	/* Transmitter empty: byte sent */
150 #define	UBSA_LSR_TXRDY		0x20	/* Transmitter buffer empty */
151 #define	UBSA_LSR_BI		0x10	/* Break detected */
152 #define	UBSA_LSR_FE		0x08	/* Framing error: bad stop bit */
153 #define	UBSA_LSR_PE		0x04	/* Parity error */
154 #define	UBSA_LSR_OE		0x02	/* Overrun, lost incoming byte */
155 #define	UBSA_LSR_RXRDY		0x01	/* Byte ready in Receive Buffer */
156 #define	UBSA_LSR_RCV_MASK	0x1f	/* Mask for incoming data or error */
157 
158 /* modem status register */
159 /* All deltas are from the last read of the MSR. */
160 #define	UBSA_MSR_DCD		0x80	/* Current Data Carrier Detect */
161 #define	UBSA_MSR_RI		0x40	/* Current Ring Indicator */
162 #define	UBSA_MSR_DSR		0x20	/* Current Data Set Ready */
163 #define	UBSA_MSR_CTS		0x10	/* Current Clear to Send */
164 #define	UBSA_MSR_DDCD		0x08	/* DCD has changed state */
165 #define	UBSA_MSR_TERI		0x04	/* RI has toggled low to high */
166 #define	UBSA_MSR_DDSR		0x02	/* DSR has changed state */
167 #define	UBSA_MSR_DCTS		0x01	/* CTS has changed state */
168 
169 struct	ubsa_softc {
170 	USBBASEDEVICE		sc_dev;		/* base device */
171 	usbd_device_handle	sc_udev;	/* USB device */
172 	usbd_interface_handle	sc_iface;	/* interface */
173 
174 	int			sc_iface_number;	/* interface number */
175 
176 	usbd_interface_handle	sc_intr_iface;	/* interrupt interface */
177 	int			sc_intr_number;	/* interrupt number */
178 	usbd_pipe_handle	sc_intr_pipe;	/* interrupt pipe */
179 	u_char			*sc_intr_buf;	/* interrupt buffer */
180 	int			sc_isize;
181 
182 	u_char			sc_dtr;		/* current DTR state */
183 	u_char			sc_rts;		/* current RTS state */
184 
185 	u_char			sc_lsr;		/* Local status register */
186 	u_char			sc_msr;		/* ubsa status register */
187 
188 	device_ptr_t		sc_subdev;	/* ucom device */
189 
190 	u_char			sc_dying;	/* disconnecting */
191 
192 };
193 
194 Static	void ubsa_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
195 
196 Static	void ubsa_get_status(void *, int, u_char *, u_char *);
197 Static	void ubsa_set(void *, int, int, int);
198 Static	int  ubsa_param(void *, int, struct termios *);
199 Static	int  ubsa_open(void *, int);
200 Static	void ubsa_close(void *, int);
201 
202 Static  void ubsa_break(struct ubsa_softc *sc, int onoff);
203 Static	int  ubsa_request(struct ubsa_softc *, u_int8_t, u_int16_t);
204 Static	void ubsa_dtr(struct ubsa_softc *, int);
205 Static	void ubsa_rts(struct ubsa_softc *, int);
206 Static	void ubsa_baudrate(struct ubsa_softc *, speed_t);
207 Static	void ubsa_parity(struct ubsa_softc *, tcflag_t);
208 Static	void ubsa_databits(struct ubsa_softc *, tcflag_t);
209 Static	void ubsa_stopbits(struct ubsa_softc *, tcflag_t);
210 Static	void ubsa_flow(struct ubsa_softc *, tcflag_t, tcflag_t);
211 
212 struct	ucom_methods ubsa_methods = {
213 	ubsa_get_status,
214 	ubsa_set,
215 	ubsa_param,
216 	NULL,
217 	ubsa_open,
218 	ubsa_close,
219 	NULL,
220 	NULL
221 };
222 
223 Static const struct usb_devno ubsa_devs[] = {
224 	/* BELKIN F5U103 */
225 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103 },
226 	/* BELKIN F5U120 */
227 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120 },
228 	/* GoHubs GO-COM232 */
229 	{ USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM },
230 	/* GoHubs GO-COM232 */
231 	{ USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232 },
232 	/* Peracom */
233 	{ USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1 },
234 };
235 #define ubsa_lookup(v, p) usb_lookup(ubsa_devs, v, p)
236 
237 USB_DECLARE_DRIVER(ubsa);
238 
239 USB_MATCH(ubsa)
240 {
241 	USB_MATCH_START(ubsa, uaa);
242 
243 	if (uaa->iface != NULL)
244 		return (UMATCH_NONE);
245 
246 	return (ubsa_lookup(uaa->vendor, uaa->product) != NULL ?
247 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
248 }
249 
250 USB_ATTACH(ubsa)
251 {
252 	USB_ATTACH_START(ubsa, sc, uaa);
253 	usbd_device_handle dev = uaa->device;
254 	usb_config_descriptor_t *cdesc;
255 	usb_interface_descriptor_t *id;
256 	usb_endpoint_descriptor_t *ed;
257 	char devinfo[1024];
258 	const char *devname = USBDEVNAME(sc->sc_dev);
259 	usbd_status err;
260 	struct ucom_attach_args uca;
261 	int i;
262 
263         usbd_devinfo(dev, 0, devinfo, sizeof(devinfo));
264         USB_ATTACH_SETUP;
265         printf("%s: %s\n", devname, devinfo);
266 
267         sc->sc_udev = dev;
268 
269 	/*
270 	 * initialize rts, dtr variables to something
271 	 * different from boolean 0, 1
272 	 */
273 	sc->sc_dtr = -1;
274 	sc->sc_rts = -1;
275 
276 	printf("%s: %s\n", devname, devinfo);
277 
278 	DPRINTF(("ubsa attach: sc = %p\n", sc));
279 
280 	/* initialize endpoints */
281 	uca.bulkin = uca.bulkout = -1;
282 	sc->sc_intr_number = -1;
283 	sc->sc_intr_pipe = NULL;
284 
285 	/* Move the device into the configured state. */
286 	err = usbd_set_config_index(dev, UBSA_CONFIG_INDEX, 1);
287 	if (err) {
288 		printf("%s: failed to set configuration: %s\n",
289 		    devname, usbd_errstr(err));
290 		sc->sc_dying = 1;
291 		goto error;
292 	}
293 
294 	/* get the config descriptor */
295 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
296 
297 	if (cdesc == NULL) {
298 		printf("%s: failed to get configuration descriptor\n",
299 		    devname);
300 		sc->sc_dying = 1;
301 		goto error;
302 	}
303 
304 	/* get the first interface */
305 	err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX,
306 	    &sc->sc_iface);
307 	if (err) {
308 		printf("%s: failed to get interface: %s\n",
309 			devname, usbd_errstr(err));
310 		sc->sc_dying = 1;
311 		goto error;
312 	}
313 
314 	/* Find the endpoints */
315 
316 	id = usbd_get_interface_descriptor(sc->sc_iface);
317 	sc->sc_iface_number = id->bInterfaceNumber;
318 
319 	for (i = 0; i < id->bNumEndpoints; i++) {
320 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
321 		if (ed == NULL) {
322 			printf("%s: no endpoint descriptor for %d\n",
323 			    USBDEVNAME(sc->sc_dev), i);
324 			sc->sc_dying = 1;
325 			goto error;
326 		}
327 
328 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
329 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
330 			sc->sc_intr_number = ed->bEndpointAddress;
331 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
332 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
333 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
334 			uca.bulkin = ed->bEndpointAddress;
335 			uca.ibufsize = UGETW(ed->wMaxPacketSize);
336 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
337 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
338 			uca.bulkout = ed->bEndpointAddress;
339 			uca.obufsize = UGETW(ed->wMaxPacketSize);
340 		}
341 	}
342 
343 	if (sc->sc_intr_number == -1) {
344 		printf("%s: Could not find interrupt in\n", devname);
345 		sc->sc_dying = 1;
346 		goto error;
347 	}
348 
349 	if (uca.bulkin == -1) {
350 		printf("%s: Could not find data bulk in\n", devname);
351 		sc->sc_dying = 1;
352 		goto error;
353 	}
354 
355 	if (uca.bulkout == -1) {
356 		printf("%s: Could not find data bulk out\n", devname);
357 		sc->sc_dying = 1;
358 		goto error;
359 	}
360 
361 	uca.portno = UCOM_UNK_PORTNO;
362 	/* bulkin, bulkout set above */
363 	uca.ibufsizepad = uca.ibufsize;
364 	uca.opkthdrlen = 0;
365 	uca.device = dev;
366 	uca.iface = sc->sc_iface;
367 	uca.methods = &ubsa_methods;
368 	uca.arg = sc;
369 	uca.info = NULL;
370 
371 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
372 			   USBDEV(sc->sc_dev));
373 
374 	DPRINTF(("ubsa: in = 0x%x, out = 0x%x, intr = 0x%x\n",
375 	    uca.bulkin_no, uca.bulkout_no, sc->sc_intr_number));
376 
377 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
378 
379 	USB_ATTACH_SUCCESS_RETURN;
380 
381 error:
382 	USB_ATTACH_ERROR_RETURN;
383 }
384 
385 USB_DETACH(ubsa)
386 {
387 	USB_DETACH_START(ubsa, sc);
388 	int rv = 0;
389 
390 
391 	DPRINTF(("ubsa_detach: sc = %p\n", sc));
392 
393 	if (sc->sc_intr_pipe != NULL) {
394 		usbd_abort_pipe(sc->sc_intr_pipe);
395 		usbd_close_pipe(sc->sc_intr_pipe);
396 		free(sc->sc_intr_buf, M_USBDEV);
397 		sc->sc_intr_pipe = NULL;
398 	}
399 
400 	sc->sc_dying = 1;
401 	if (sc->sc_subdev != NULL) {
402 		rv = config_detach(sc->sc_subdev, flags);
403 		sc->sc_subdev = NULL;
404 	}
405 
406 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
407 			   USBDEV(sc->sc_dev));
408 
409 	return (rv);
410 }
411 
412 int
413 ubsa_activate(device_ptr_t self, enum devact act)
414 {
415 	struct ubsa_softc *sc = (struct ubsa_softc *)self;
416 	int rv = 0;
417 
418 	switch (act) {
419 	case DVACT_ACTIVATE:
420 		return (EOPNOTSUPP);
421 
422 	case DVACT_DEACTIVATE:
423 		if (sc->sc_subdev != NULL)
424 			rv = config_deactivate(sc->sc_subdev);
425 		sc->sc_dying = 1;
426 		break;
427 	}
428 	return (rv);
429 }
430 
431 Static int
432 ubsa_request(struct ubsa_softc *sc, u_int8_t request, u_int16_t value)
433 {
434 	usb_device_request_t req;
435 	usbd_status err;
436 
437 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
438 	req.bRequest = request;
439 	USETW(req.wValue, value);
440 	USETW(req.wIndex, sc->sc_iface_number);
441 	USETW(req.wLength, 0);
442 
443 	err = usbd_do_request(sc->sc_udev, &req, 0);
444 	if (err)
445 		printf("%s: ubsa_request: %s\n",
446 		    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
447 	return (err);
448 }
449 
450 Static void
451 ubsa_dtr(struct ubsa_softc *sc, int onoff)
452 {
453 
454 	DPRINTF(("ubsa_dtr: onoff = %d\n", onoff));
455 
456 	if (sc->sc_dtr == onoff)
457 		return;
458 	sc->sc_dtr = onoff;
459 
460 	ubsa_request(sc, UBSA_SET_DTR, onoff ? 1 : 0);
461 }
462 
463 Static void
464 ubsa_rts(struct ubsa_softc *sc, int onoff)
465 {
466 
467 	DPRINTF(("ubsa_rts: onoff = %d\n", onoff));
468 
469 	if (sc->sc_rts == onoff)
470 		return;
471 	sc->sc_rts = onoff;
472 
473 	ubsa_request(sc, UBSA_SET_RTS, onoff ? 1 : 0);
474 }
475 
476 Static void
477 ubsa_break(struct ubsa_softc *sc, int onoff)
478 {
479 
480 	DPRINTF(("ubsa_rts: onoff = %d\n", onoff));
481 
482 	ubsa_request(sc, UBSA_SET_BREAK, onoff ? 1 : 0);
483 }
484 
485 Static void
486 ubsa_set(void *addr, int portno, int reg, int onoff)
487 {
488 	struct ubsa_softc *sc;
489 
490 	sc = addr;
491 	switch (reg) {
492 	case UCOM_SET_DTR:
493 		ubsa_dtr(sc, onoff);
494 		break;
495 	case UCOM_SET_RTS:
496 		ubsa_rts(sc, onoff);
497 		break;
498 	case UCOM_SET_BREAK:
499 		ubsa_break(sc, onoff);
500 		break;
501 	default:
502 		break;
503 	}
504 }
505 
506 Static void
507 ubsa_baudrate(struct ubsa_softc *sc, speed_t speed)
508 {
509 	u_int16_t value = 0;
510 
511 	DPRINTF(("ubsa_baudrate: speed = %d\n", speed));
512 
513 	switch(speed) {
514 	case B0:
515 		break;
516 	case B300:
517 	case B600:
518 	case B1200:
519 	case B2400:
520 	case B4800:
521 	case B9600:
522 	case B19200:
523 	case B38400:
524 	case B57600:
525 	case B115200:
526 	case B230400:
527 		value = B230400 / speed;
528 		break;
529 	default:
530 		printf("%s: ubsa_param: unsupported baudrate, "
531 		    "forcing default of 9600\n",
532 		    USBDEVNAME(sc->sc_dev));
533 		value = B230400 / B9600;
534 		break;
535 	};
536 
537 	if (speed == B0) {
538 		ubsa_flow(sc, 0, 0);
539 		ubsa_dtr(sc, 0);
540 		ubsa_rts(sc, 0);
541 	} else
542 		ubsa_request(sc, UBSA_SET_BAUDRATE, value);
543 }
544 
545 Static void
546 ubsa_parity(struct ubsa_softc *sc, tcflag_t cflag)
547 {
548 	int value;
549 
550 	DPRINTF(("ubsa_parity: cflag = 0x%x\n", cflag));
551 
552 	if (cflag & PARENB)
553 		value = (cflag & PARODD) ? UBSA_PARITY_ODD : UBSA_PARITY_EVEN;
554 	else
555 		value = UBSA_PARITY_NONE;
556 
557 	ubsa_request(sc, UBSA_SET_PARITY, value);
558 }
559 
560 Static void
561 ubsa_databits(struct ubsa_softc *sc, tcflag_t cflag)
562 {
563 	int value;
564 
565 	DPRINTF(("ubsa_databits: cflag = 0x%x\n", cflag));
566 
567 	switch (cflag & CSIZE) {
568 	case CS5: value = 0; break;
569 	case CS6: value = 1; break;
570 	case CS7: value = 2; break;
571 	case CS8: value = 3; break;
572 	default:
573 		printf("%s: ubsa_param: unsupported databits requested, "
574 		    "forcing default of 8\n",
575 		    USBDEVNAME(sc->sc_dev));
576 		value = 3;
577 	}
578 
579 	ubsa_request(sc, UBSA_SET_DATA_BITS, value);
580 }
581 
582 Static void
583 ubsa_stopbits(struct ubsa_softc *sc, tcflag_t cflag)
584 {
585 	int value;
586 
587 	DPRINTF(("ubsa_stopbits: cflag = 0x%x\n", cflag));
588 
589 	value = (cflag & CSTOPB) ? 1 : 0;
590 
591 	ubsa_request(sc, UBSA_SET_STOP_BITS, value);
592 }
593 
594 Static void
595 ubsa_flow(struct ubsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
596 {
597 	int value;
598 
599 	DPRINTF(("ubsa_flow: cflag = 0x%x, iflag = 0x%x\n", cflag, iflag));
600 
601 	value = 0;
602 	if (cflag & CRTSCTS)
603 		value |= UBSA_FLOW_OCTS | UBSA_FLOW_IRTS;
604 	if (iflag & (IXON|IXOFF))
605 		value |= UBSA_FLOW_OXON | UBSA_FLOW_IXON;
606 
607 	ubsa_request(sc, UBSA_SET_FLOW_CTRL, value);
608 }
609 
610 Static int
611 ubsa_param(void *addr, int portno, struct termios *ti)
612 {
613 	struct ubsa_softc *sc = addr;
614 
615 	DPRINTF(("ubsa_param: sc = %p\n", sc));
616 
617 	ubsa_baudrate(sc, ti->c_ospeed);
618 	ubsa_parity(sc, ti->c_cflag);
619 	ubsa_databits(sc, ti->c_cflag);
620 	ubsa_stopbits(sc, ti->c_cflag);
621 	ubsa_flow(sc, ti->c_cflag, ti->c_iflag);
622 
623 	return (0);
624 }
625 
626 Static int
627 ubsa_open(void *addr, int portno)
628 {
629 	struct ubsa_softc *sc = addr;
630 	int err;
631 
632 	if (sc->sc_dying)
633 		return (ENXIO);
634 
635 	DPRINTF(("ubsa_open: sc = %p\n", sc));
636 
637 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
638 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
639 		err = usbd_open_pipe_intr(sc->sc_intr_iface,
640 		    sc->sc_intr_number,
641 		    USBD_SHORT_XFER_OK,
642 		    &sc->sc_intr_pipe,
643 		    sc,
644 		    sc->sc_intr_buf,
645 		    sc->sc_isize,
646 		    ubsa_intr,
647 		    UBSA_INTR_INTERVAL);
648 		if (err) {
649 			printf("%s: cannot open interrupt pipe (addr %d)\n",
650 			    USBDEVNAME(sc->sc_dev),
651 			    sc->sc_intr_number);
652 			return (EIO);
653 		}
654 	}
655 
656 	return (0);
657 }
658 
659 Static void
660 ubsa_close(void *addr, int portno)
661 {
662 	struct ubsa_softc *sc = addr;
663 	int err;
664 
665 	if (sc->sc_dying)
666 		return;
667 
668 	DPRINTF(("ubsa_close: close\n"));
669 
670 	if (sc->sc_intr_pipe != NULL) {
671 		err = usbd_abort_pipe(sc->sc_intr_pipe);
672 		if (err)
673 			printf("%s: abort interrupt pipe failed: %s\n",
674 			    USBDEVNAME(sc->sc_dev),
675 			    usbd_errstr(err));
676 		err = usbd_close_pipe(sc->sc_intr_pipe);
677 		if (err)
678 			printf("%s: close interrupt pipe failed: %s\n",
679 			    USBDEVNAME(sc->sc_dev),
680 			    usbd_errstr(err));
681 		free(sc->sc_intr_buf, M_USBDEV);
682 		sc->sc_intr_pipe = NULL;
683 	}
684 }
685 
686 Static void
687 ubsa_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
688 {
689 	struct ubsa_softc *sc = priv;
690 	u_char *buf;
691 
692 	buf = sc->sc_intr_buf;
693 	if (sc->sc_dying)
694 		return;
695 
696 	if (status != USBD_NORMAL_COMPLETION) {
697 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
698 			return;
699 
700 		DPRINTF(("%s: ubsa_intr: abnormal status: %s\n",
701 		    USBDEVNAME(sc->sc_ucom.sc_dev),
702 		    usbd_errstr(status)));
703 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
704 		return;
705 	}
706 
707 	/* incidentally, Belkin adapter status bits match UART 16550 bits */
708 	sc->sc_lsr = buf[2];
709 	sc->sc_msr = buf[3];
710 
711 	DPRINTF(("%s: ubsa lsr = 0x%02x, msr = 0x%02x\n",
712 	    USBDEVNAME(sc->sc_ucom.sc_dev), sc->sc_lsr, sc->sc_msr));
713 
714 	ucom_status_change((struct ucom_softc *)sc->sc_subdev);
715 }
716 
717 Static void
718 ubsa_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
719 {
720 	struct ubsa_softc *sc = addr;
721 
722 	DPRINTF(("ubsa_get_status\n"));
723 
724 	if (lsr != NULL)
725 		*lsr = sc->sc_lsr;
726 	if (msr != NULL)
727 		*msr = sc->sc_msr;
728 }
729