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