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