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