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