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