xref: /openbsd-src/sys/dev/usb/ubsa.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: ubsa.c,v 1.66 2018/04/27 08:08:06 guenther 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_abort_pipe(sc->sc_intr_pipe);
371 		usbd_close_pipe(sc->sc_intr_pipe);
372 		free(sc->sc_intr_buf, M_USBDEV, sc->sc_isize);
373 		sc->sc_intr_pipe = NULL;
374 	}
375 
376 	if (sc->sc_subdev != NULL) {
377 		rv = config_detach(sc->sc_subdev, flags);
378 		sc->sc_subdev = NULL;
379 	}
380 
381 	return (rv);
382 }
383 
384 int
385 ubsa_request(struct ubsa_softc *sc, u_int8_t request, u_int16_t value)
386 {
387 	usb_device_request_t req;
388 	usbd_status err;
389 
390 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
391 	req.bRequest = request;
392 	USETW(req.wValue, value);
393 	USETW(req.wIndex, sc->sc_iface_number);
394 	USETW(req.wLength, 0);
395 
396 	err = usbd_do_request(sc->sc_udev, &req, 0);
397 	if (err && err != USBD_STALLED)
398 		printf("%s: ubsa_request: %s\n",
399 		    sc->sc_dev.dv_xname, usbd_errstr(err));
400 	return (err);
401 }
402 
403 void
404 ubsa_dtr(struct ubsa_softc *sc, int onoff)
405 {
406 
407 	DPRINTF(("ubsa_dtr: onoff = %d\n", onoff));
408 
409 	if (sc->sc_dtr == onoff)
410 		return;
411 	sc->sc_dtr = onoff;
412 
413 	ubsa_request(sc, UBSA_SET_DTR, onoff ? 1 : 0);
414 }
415 
416 void
417 ubsa_rts(struct ubsa_softc *sc, int onoff)
418 {
419 
420 	DPRINTF(("ubsa_rts: onoff = %d\n", onoff));
421 
422 	if (sc->sc_rts == onoff)
423 		return;
424 	sc->sc_rts = onoff;
425 
426 	ubsa_request(sc, UBSA_SET_RTS, onoff ? 1 : 0);
427 }
428 
429 void
430 ubsa_break(struct ubsa_softc *sc, int onoff)
431 {
432 
433 	DPRINTF(("ubsa_rts: onoff = %d\n", onoff));
434 
435 	ubsa_request(sc, UBSA_SET_BREAK, onoff ? 1 : 0);
436 }
437 
438 void
439 ubsa_set(void *addr, int portno, int reg, int onoff)
440 {
441 	struct ubsa_softc *sc;
442 
443 	sc = addr;
444 	switch (reg) {
445 	case UCOM_SET_DTR:
446 		ubsa_dtr(sc, onoff);
447 		break;
448 	case UCOM_SET_RTS:
449 		ubsa_rts(sc, onoff);
450 		break;
451 	case UCOM_SET_BREAK:
452 		ubsa_break(sc, onoff);
453 		break;
454 	default:
455 		break;
456 	}
457 }
458 
459 void
460 ubsa_baudrate(struct ubsa_softc *sc, speed_t speed)
461 {
462 	u_int16_t value = 0;
463 
464 	DPRINTF(("ubsa_baudrate: speed = %d\n", speed));
465 
466 	switch(speed) {
467 	case B0:
468 		break;
469 	case B300:
470 	case B600:
471 	case B1200:
472 	case B2400:
473 	case B4800:
474 	case B9600:
475 	case B19200:
476 	case B38400:
477 	case B57600:
478 	case B115200:
479 	case B230400:
480 		value = B230400 / speed;
481 		break;
482 	default:
483 		DPRINTF(("%s: ubsa_param: unsupported baudrate, "
484 		    "forcing default of 9600\n",
485 		    sc->sc_dev.dv_xname));
486 		value = B230400 / B9600;
487 		break;
488 	};
489 
490 	if (speed == B0) {
491 		ubsa_flow(sc, 0, 0);
492 		ubsa_dtr(sc, 0);
493 		ubsa_rts(sc, 0);
494 	} else
495 		ubsa_request(sc, UBSA_SET_BAUDRATE, value);
496 }
497 
498 void
499 ubsa_parity(struct ubsa_softc *sc, tcflag_t cflag)
500 {
501 	int value;
502 
503 	DPRINTF(("ubsa_parity: cflag = 0x%x\n", cflag));
504 
505 	if (cflag & PARENB)
506 		value = (cflag & PARODD) ? UBSA_PARITY_ODD : UBSA_PARITY_EVEN;
507 	else
508 		value = UBSA_PARITY_NONE;
509 
510 	ubsa_request(sc, UBSA_SET_PARITY, value);
511 }
512 
513 void
514 ubsa_databits(struct ubsa_softc *sc, tcflag_t cflag)
515 {
516 	int value;
517 
518 	DPRINTF(("ubsa_databits: cflag = 0x%x\n", cflag));
519 
520 	switch (cflag & CSIZE) {
521 	case CS5: value = 0; break;
522 	case CS6: value = 1; break;
523 	case CS7: value = 2; break;
524 	case CS8: value = 3; break;
525 	default:
526 		DPRINTF(("%s: ubsa_param: unsupported databits requested, "
527 		    "forcing default of 8\n",
528 		    sc->sc_dev.dv_xname));
529 		value = 3;
530 	}
531 
532 	ubsa_request(sc, UBSA_SET_DATA_BITS, value);
533 }
534 
535 void
536 ubsa_stopbits(struct ubsa_softc *sc, tcflag_t cflag)
537 {
538 	int value;
539 
540 	DPRINTF(("ubsa_stopbits: cflag = 0x%x\n", cflag));
541 
542 	value = (cflag & CSTOPB) ? 1 : 0;
543 
544 	ubsa_request(sc, UBSA_SET_STOP_BITS, value);
545 }
546 
547 void
548 ubsa_flow(struct ubsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
549 {
550 	int value;
551 
552 	DPRINTF(("ubsa_flow: cflag = 0x%x, iflag = 0x%x\n", cflag, iflag));
553 
554 	value = 0;
555 	if (cflag & CRTSCTS)
556 		value |= UBSA_FLOW_OCTS | UBSA_FLOW_IRTS;
557 	if (iflag & (IXON|IXOFF))
558 		value |= UBSA_FLOW_OXON | UBSA_FLOW_IXON;
559 
560 	ubsa_request(sc, UBSA_SET_FLOW_CTRL, value);
561 }
562 
563 int
564 ubsa_param(void *addr, int portno, struct termios *ti)
565 {
566 	struct ubsa_softc *sc = addr;
567 
568 	DPRINTF(("ubsa_param: sc = %p\n", sc));
569 
570 	ubsa_baudrate(sc, ti->c_ospeed);
571 	ubsa_parity(sc, ti->c_cflag);
572 	ubsa_databits(sc, ti->c_cflag);
573 	ubsa_stopbits(sc, ti->c_cflag);
574 	ubsa_flow(sc, ti->c_cflag, ti->c_iflag);
575 
576 	return (0);
577 }
578 
579 int
580 ubsa_open(void *addr, int portno)
581 {
582 	struct ubsa_softc *sc = addr;
583 	int err;
584 
585 	if (usbd_is_dying(sc->sc_udev))
586 		return (ENXIO);
587 
588 	DPRINTF(("ubsa_open: sc = %p\n", sc));
589 
590 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
591 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
592 		err = usbd_open_pipe_intr(sc->sc_iface,
593 		    sc->sc_intr_number,
594 		    USBD_SHORT_XFER_OK,
595 		    &sc->sc_intr_pipe,
596 		    sc,
597 		    sc->sc_intr_buf,
598 		    sc->sc_isize,
599 		    ubsa_intr,
600 		    UBSA_INTR_INTERVAL);
601 		if (err) {
602 			printf("%s: cannot open interrupt pipe (addr %d)\n",
603 			    sc->sc_dev.dv_xname,
604 			    sc->sc_intr_number);
605 			return (EIO);
606 		}
607 	}
608 
609 	return (0);
610 }
611 
612 void
613 ubsa_close(void *addr, int portno)
614 {
615 	struct ubsa_softc *sc = addr;
616 	int err;
617 
618 	if (usbd_is_dying(sc->sc_udev))
619 		return;
620 
621 	DPRINTF(("ubsa_close: close\n"));
622 
623 	if (sc->sc_intr_pipe != NULL) {
624 		usbd_abort_pipe(sc->sc_intr_pipe);
625 		err = usbd_close_pipe(sc->sc_intr_pipe);
626 		if (err)
627 			printf("%s: close interrupt pipe failed: %s\n",
628 			    sc->sc_dev.dv_xname,
629 			    usbd_errstr(err));
630 		free(sc->sc_intr_buf, M_USBDEV, sc->sc_isize);
631 		sc->sc_intr_pipe = NULL;
632 	}
633 }
634 
635 void
636 ubsa_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
637 {
638 	struct ubsa_softc *sc = priv;
639 	u_char *buf;
640 	struct usb_cdc_notification *cdcbuf;
641 
642 	buf = sc->sc_intr_buf;
643 	cdcbuf = (struct usb_cdc_notification *)sc->sc_intr_buf;
644 	if (usbd_is_dying(sc->sc_udev))
645 		return;
646 
647 	if (status != USBD_NORMAL_COMPLETION) {
648 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
649 			return;
650 
651 		DPRINTF(("%s: ubsa_intr: abnormal status: %s\n",
652 		    sc->sc_dev.dv_xname, usbd_errstr(status)));
653 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
654 		return;
655 	}
656 
657 #if 1 /* test */
658 	if (cdcbuf->bmRequestType == UCDC_NOTIFICATION) {
659 		printf("%s: this device is using CDC notify message in"
660 		    " intr pipe.\n"
661 		    "Please send your dmesg to <bugs@openbsd.org>, thanks.\n",
662 		    sc->sc_dev.dv_xname);
663 		printf("%s: intr buffer 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
664 		    sc->sc_dev.dv_xname,
665 		    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
666 
667 		/* check the buffer data */
668 		if (cdcbuf->bNotification == UCDC_N_SERIAL_STATE)
669 			printf("%s:notify serial state, len=%d, data=0x%02x\n",
670 			    sc->sc_dev.dv_xname,
671 			    UGETW(cdcbuf->wLength), cdcbuf->data[0]);
672 	}
673 #endif
674 
675 	/* incidentally, Belkin adapter status bits match UART 16550 bits */
676 	sc->sc_lsr = buf[2];
677 	sc->sc_msr = buf[3];
678 
679 	DPRINTF(("%s: ubsa lsr = 0x%02x, msr = 0x%02x\n",
680 	    sc->sc_dev.dv_xname, sc->sc_lsr, sc->sc_msr));
681 
682 	ucom_status_change((struct ucom_softc *)sc->sc_subdev);
683 }
684 
685 void
686 ubsa_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
687 {
688 	struct ubsa_softc *sc = addr;
689 
690 	DPRINTF(("ubsa_get_status\n"));
691 
692 	if (lsr != NULL)
693 		*lsr = sc->sc_lsr;
694 	if (msr != NULL)
695 		*msr = sc->sc_msr;
696 }
697