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