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