xref: /openbsd-src/sys/dev/usb/ubsa.c (revision 850e275390052b330d93020bf619a739a3c277ac)
1 /*	$OpenBSD: ubsa.c,v 1.36 2008/07/20 14:24:49 yuo 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 	/* AnyDATA ADU-E100H */
207 	{ USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_E100H },
208 	/* Axesstel MV100H */
209 	{ USB_VENDOR_AXESSTEL, USB_PRODUCT_AXESSTEL_DATAMODEM },
210 	/* BELKIN F5U103 */
211 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103 },
212 	/* BELKIN F5U120 */
213 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120 },
214 	/* GoHubs GO-COM232 , Belkin F5U003 */
215 	{ USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM },
216 	/* GoHubs GO-COM232 */
217 	{ USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232 },
218 	/* Novatel Wireless U740 */
219 	{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_MERLINU740 },
220 	/* Option Vodafone Mobile Connect 3G */
221 	{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_VODAFONEMC3G },
222 	/* Option GlobeTrotter 3G FUSION */
223 	{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3GFUSION },
224 	/* Option GlobeTrotter 3G QUAD */
225 	{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3GQUAD },
226 	/* Option GlobeTrotter 3G QUAD PLUS */
227 	{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3GQUADPLUS },
228 	/* Peracom */
229 	{ USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1 },
230 	/* Qualcomm Inc. ZTE CMDMA MSM modem */
231 	{ USB_VENDOR_QUALCOMM3, USB_PRODUCT_QUALCOMM3_CDMA_MSM },
232 };
233 #define ubsa_lookup(v, p) usb_lookup(ubsa_devs, v, p)
234 
235 int ubsa_match(struct device *, void *, void *);
236 void ubsa_attach(struct device *, struct device *, void *);
237 int ubsa_detach(struct device *, int);
238 int ubsa_activate(struct device *, enum devact);
239 
240 struct cfdriver ubsa_cd = {
241 	NULL, "ubsa", DV_DULL
242 };
243 
244 const struct cfattach ubsa_ca = {
245 	sizeof(struct ubsa_softc),
246 	ubsa_match,
247 	ubsa_attach,
248 	ubsa_detach,
249 	ubsa_activate,
250 };
251 
252 int
253 ubsa_match(struct device *parent, void *match, void *aux)
254 {
255 	struct usb_attach_arg *uaa = aux;
256 
257 	if (uaa->iface != NULL)
258 		return (UMATCH_NONE);
259 
260 	return (ubsa_lookup(uaa->vendor, uaa->product) != NULL ?
261 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
262 }
263 
264 void
265 ubsa_attach(struct device *parent, struct device *self, void *aux)
266 {
267 	struct ubsa_softc *sc = (struct ubsa_softc *)self;
268 	struct usb_attach_arg *uaa = aux;
269 	usbd_device_handle dev = uaa->device;
270 	usb_config_descriptor_t *cdesc;
271 	usb_interface_descriptor_t *id;
272 	usb_endpoint_descriptor_t *ed;
273 	const char *devname = sc->sc_dev.dv_xname;
274 	usbd_status err;
275 	struct ucom_attach_args uca;
276 	int i;
277 
278 	sc->sc_udev = dev;
279 
280 	/*
281 	 * initialize rts, dtr variables to something
282 	 * different from boolean 0, 1
283 	 */
284 	sc->sc_dtr = -1;
285 	sc->sc_rts = -1;
286 
287 	DPRINTF(("ubsa attach: sc = %p\n", sc));
288 
289 	/* initialize endpoints */
290 	uca.bulkin = uca.bulkout = -1;
291 	sc->sc_intr_number = -1;
292 	sc->sc_intr_pipe = NULL;
293 
294 	/* Move the device into the configured state. */
295 	err = usbd_set_config_index(dev, UBSA_CONFIG_INDEX, 1);
296 	if (err) {
297 		printf("%s: failed to set configuration: %s\n",
298 		    devname, usbd_errstr(err));
299 		sc->sc_dying = 1;
300 		goto error;
301 	}
302 
303 	/* get the config descriptor */
304 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
305 
306 	if (cdesc == NULL) {
307 		printf("%s: failed to get configuration descriptor\n",
308 		    devname);
309 		sc->sc_dying = 1;
310 		goto error;
311 	}
312 
313 	/* get the first interface */
314 	err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX,
315 	    &sc->sc_iface);
316 	if (err) {
317 		printf("%s: failed to get interface: %s\n",
318 			devname, usbd_errstr(err));
319 		sc->sc_dying = 1;
320 		goto error;
321 	}
322 
323 	/* Find the endpoints */
324 
325 	id = usbd_get_interface_descriptor(sc->sc_iface);
326 	sc->sc_iface_number = id->bInterfaceNumber;
327 
328 	for (i = 0; i < id->bNumEndpoints; i++) {
329 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
330 		if (ed == NULL) {
331 			printf("%s: no endpoint descriptor for %d\n",
332 			    sc->sc_dev.dv_xname, i);
333 			sc->sc_dying = 1;
334 			goto error;
335 		}
336 
337 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
338 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
339 			sc->sc_intr_number = ed->bEndpointAddress;
340 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
341 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
342 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
343 			uca.bulkin = ed->bEndpointAddress;
344 			uca.ibufsize = UGETW(ed->wMaxPacketSize);
345 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
346 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
347 			uca.bulkout = ed->bEndpointAddress;
348 			uca.obufsize = UGETW(ed->wMaxPacketSize);
349 		}
350 	}
351 
352 	if (sc->sc_intr_number == -1) {
353 		printf("%s: Could not find interrupt in\n", devname);
354 		sc->sc_dying = 1;
355 		goto error;
356 	}
357 
358 	if (uca.bulkin == -1) {
359 		printf("%s: Could not find data bulk in\n", devname);
360 		sc->sc_dying = 1;
361 		goto error;
362 	}
363 
364 	if (uca.bulkout == -1) {
365 		printf("%s: Could not find data bulk out\n", devname);
366 		sc->sc_dying = 1;
367 		goto error;
368 	}
369 
370 	uca.portno = UCOM_UNK_PORTNO;
371 	/* bulkin, bulkout set above */
372 	uca.ibufsizepad = uca.ibufsize;
373 	uca.opkthdrlen = 0;
374 	uca.device = dev;
375 	uca.iface = sc->sc_iface;
376 	uca.methods = &ubsa_methods;
377 	uca.arg = sc;
378 	uca.info = NULL;
379 
380 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
381 			   &sc->sc_dev);
382 
383 	DPRINTF(("ubsa: in = 0x%x, out = 0x%x, intr = 0x%x\n",
384 	    uca.bulkin, uca.bulkout, sc->sc_intr_number));
385 
386 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
387 
388 error:
389 	return;
390 }
391 
392 int
393 ubsa_detach(struct device *self, int flags)
394 {
395 	struct ubsa_softc *sc = (struct ubsa_softc *)self;
396 	int rv = 0;
397 
398 
399 	DPRINTF(("ubsa_detach: sc = %p\n", sc));
400 
401 	if (sc->sc_intr_pipe != NULL) {
402 		usbd_abort_pipe(sc->sc_intr_pipe);
403 		usbd_close_pipe(sc->sc_intr_pipe);
404 		free(sc->sc_intr_buf, M_USBDEV);
405 		sc->sc_intr_pipe = NULL;
406 	}
407 
408 	sc->sc_dying = 1;
409 	if (sc->sc_subdev != NULL) {
410 		rv = config_detach(sc->sc_subdev, flags);
411 		sc->sc_subdev = NULL;
412 	}
413 
414 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
415 			   &sc->sc_dev);
416 
417 	return (rv);
418 }
419 
420 int
421 ubsa_activate(struct device *self, enum devact act)
422 {
423 	struct ubsa_softc *sc = (struct ubsa_softc *)self;
424 	int rv = 0;
425 
426 	switch (act) {
427 	case DVACT_ACTIVATE:
428 		break;
429 
430 	case DVACT_DEACTIVATE:
431 		if (sc->sc_subdev != NULL)
432 			rv = config_deactivate(sc->sc_subdev);
433 		sc->sc_dying = 1;
434 		break;
435 	}
436 	return (rv);
437 }
438 
439 int
440 ubsa_request(struct ubsa_softc *sc, u_int8_t request, u_int16_t value)
441 {
442 	usb_device_request_t req;
443 	usbd_status err;
444 
445 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
446 	req.bRequest = request;
447 	USETW(req.wValue, value);
448 	USETW(req.wIndex, sc->sc_iface_number);
449 	USETW(req.wLength, 0);
450 
451 	err = usbd_do_request(sc->sc_udev, &req, 0);
452 	if (err && err != USBD_STALLED)
453 		printf("%s: ubsa_request: %s\n",
454 		    sc->sc_dev.dv_xname, usbd_errstr(err));
455 	return (err);
456 }
457 
458 void
459 ubsa_dtr(struct ubsa_softc *sc, int onoff)
460 {
461 
462 	DPRINTF(("ubsa_dtr: onoff = %d\n", onoff));
463 
464 	if (sc->sc_dtr == onoff)
465 		return;
466 	sc->sc_dtr = onoff;
467 
468 	ubsa_request(sc, UBSA_SET_DTR, onoff ? 1 : 0);
469 }
470 
471 void
472 ubsa_rts(struct ubsa_softc *sc, int onoff)
473 {
474 
475 	DPRINTF(("ubsa_rts: onoff = %d\n", onoff));
476 
477 	if (sc->sc_rts == onoff)
478 		return;
479 	sc->sc_rts = onoff;
480 
481 	ubsa_request(sc, UBSA_SET_RTS, onoff ? 1 : 0);
482 }
483 
484 void
485 ubsa_break(struct ubsa_softc *sc, int onoff)
486 {
487 
488 	DPRINTF(("ubsa_rts: onoff = %d\n", onoff));
489 
490 	ubsa_request(sc, UBSA_SET_BREAK, onoff ? 1 : 0);
491 }
492 
493 void
494 ubsa_set(void *addr, int portno, int reg, int onoff)
495 {
496 	struct ubsa_softc *sc;
497 
498 	sc = addr;
499 	switch (reg) {
500 	case UCOM_SET_DTR:
501 		ubsa_dtr(sc, onoff);
502 		break;
503 	case UCOM_SET_RTS:
504 		ubsa_rts(sc, onoff);
505 		break;
506 	case UCOM_SET_BREAK:
507 		ubsa_break(sc, onoff);
508 		break;
509 	default:
510 		break;
511 	}
512 }
513 
514 void
515 ubsa_baudrate(struct ubsa_softc *sc, speed_t speed)
516 {
517 	u_int16_t value = 0;
518 
519 	DPRINTF(("ubsa_baudrate: speed = %d\n", speed));
520 
521 	switch(speed) {
522 	case B0:
523 		break;
524 	case B300:
525 	case B600:
526 	case B1200:
527 	case B2400:
528 	case B4800:
529 	case B9600:
530 	case B19200:
531 	case B38400:
532 	case B57600:
533 	case B115200:
534 	case B230400:
535 		value = B230400 / speed;
536 		break;
537 	default:
538 		DPRINTF(("%s: ubsa_param: unsupported baudrate, "
539 		    "forcing default of 9600\n",
540 		    sc->sc_dev.dv_xname));
541 		value = B230400 / B9600;
542 		break;
543 	};
544 
545 	if (speed == B0) {
546 		ubsa_flow(sc, 0, 0);
547 		ubsa_dtr(sc, 0);
548 		ubsa_rts(sc, 0);
549 	} else
550 		ubsa_request(sc, UBSA_SET_BAUDRATE, value);
551 }
552 
553 void
554 ubsa_parity(struct ubsa_softc *sc, tcflag_t cflag)
555 {
556 	int value;
557 
558 	DPRINTF(("ubsa_parity: cflag = 0x%x\n", cflag));
559 
560 	if (cflag & PARENB)
561 		value = (cflag & PARODD) ? UBSA_PARITY_ODD : UBSA_PARITY_EVEN;
562 	else
563 		value = UBSA_PARITY_NONE;
564 
565 	ubsa_request(sc, UBSA_SET_PARITY, value);
566 }
567 
568 void
569 ubsa_databits(struct ubsa_softc *sc, tcflag_t cflag)
570 {
571 	int value;
572 
573 	DPRINTF(("ubsa_databits: cflag = 0x%x\n", cflag));
574 
575 	switch (cflag & CSIZE) {
576 	case CS5: value = 0; break;
577 	case CS6: value = 1; break;
578 	case CS7: value = 2; break;
579 	case CS8: value = 3; break;
580 	default:
581 		DPRINTF(("%s: ubsa_param: unsupported databits requested, "
582 		    "forcing default of 8\n",
583 		    sc->sc_dev.dv_xname));
584 		value = 3;
585 	}
586 
587 	ubsa_request(sc, UBSA_SET_DATA_BITS, value);
588 }
589 
590 void
591 ubsa_stopbits(struct ubsa_softc *sc, tcflag_t cflag)
592 {
593 	int value;
594 
595 	DPRINTF(("ubsa_stopbits: cflag = 0x%x\n", cflag));
596 
597 	value = (cflag & CSTOPB) ? 1 : 0;
598 
599 	ubsa_request(sc, UBSA_SET_STOP_BITS, value);
600 }
601 
602 void
603 ubsa_flow(struct ubsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
604 {
605 	int value;
606 
607 	DPRINTF(("ubsa_flow: cflag = 0x%x, iflag = 0x%x\n", cflag, iflag));
608 
609 	value = 0;
610 	if (cflag & CRTSCTS)
611 		value |= UBSA_FLOW_OCTS | UBSA_FLOW_IRTS;
612 	if (iflag & (IXON|IXOFF))
613 		value |= UBSA_FLOW_OXON | UBSA_FLOW_IXON;
614 
615 	ubsa_request(sc, UBSA_SET_FLOW_CTRL, value);
616 }
617 
618 int
619 ubsa_param(void *addr, int portno, struct termios *ti)
620 {
621 	struct ubsa_softc *sc = addr;
622 
623 	DPRINTF(("ubsa_param: sc = %p\n", sc));
624 
625 	ubsa_baudrate(sc, ti->c_ospeed);
626 	ubsa_parity(sc, ti->c_cflag);
627 	ubsa_databits(sc, ti->c_cflag);
628 	ubsa_stopbits(sc, ti->c_cflag);
629 	ubsa_flow(sc, ti->c_cflag, ti->c_iflag);
630 
631 	return (0);
632 }
633 
634 int
635 ubsa_open(void *addr, int portno)
636 {
637 	struct ubsa_softc *sc = addr;
638 	int err;
639 
640 	if (sc->sc_dying)
641 		return (ENXIO);
642 
643 	DPRINTF(("ubsa_open: sc = %p\n", sc));
644 
645 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
646 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
647 		err = usbd_open_pipe_intr(sc->sc_iface,
648 		    sc->sc_intr_number,
649 		    USBD_SHORT_XFER_OK,
650 		    &sc->sc_intr_pipe,
651 		    sc,
652 		    sc->sc_intr_buf,
653 		    sc->sc_isize,
654 		    ubsa_intr,
655 		    UBSA_INTR_INTERVAL);
656 		if (err) {
657 			printf("%s: cannot open interrupt pipe (addr %d)\n",
658 			    sc->sc_dev.dv_xname,
659 			    sc->sc_intr_number);
660 			return (EIO);
661 		}
662 	}
663 
664 	return (0);
665 }
666 
667 void
668 ubsa_close(void *addr, int portno)
669 {
670 	struct ubsa_softc *sc = addr;
671 	int err;
672 
673 	if (sc->sc_dying)
674 		return;
675 
676 	DPRINTF(("ubsa_close: close\n"));
677 
678 	if (sc->sc_intr_pipe != NULL) {
679 		err = usbd_abort_pipe(sc->sc_intr_pipe);
680 		if (err)
681 			printf("%s: abort interrupt pipe failed: %s\n",
682 			    sc->sc_dev.dv_xname,
683 			    usbd_errstr(err));
684 		err = usbd_close_pipe(sc->sc_intr_pipe);
685 		if (err)
686 			printf("%s: close interrupt pipe failed: %s\n",
687 			    sc->sc_dev.dv_xname,
688 			    usbd_errstr(err));
689 		free(sc->sc_intr_buf, M_USBDEV);
690 		sc->sc_intr_pipe = NULL;
691 	}
692 }
693 
694 void
695 ubsa_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
696 {
697 	struct ubsa_softc *sc = priv;
698 	u_char *buf;
699 	usb_cdc_notification_t *cdcbuf;
700 
701 	buf = sc->sc_intr_buf;
702 	cdcbuf = (usb_cdc_notification_t *)sc->sc_intr_buf;
703 	if (sc->sc_dying)
704 		return;
705 
706 	if (status != USBD_NORMAL_COMPLETION) {
707 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
708 			return;
709 
710 		DPRINTF(("%s: ubsa_intr: abnormal status: %s\n",
711 		    sc->sc_dev.dv_xname, usbd_errstr(status)));
712 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
713 		return;
714 	}
715 
716 #if 1 /* test */
717 	if (cdcbuf->bmRequestType == UCDC_NOTIFICATION) {
718 		printf("%s: this device is using CDC notify message in"
719 		    " intr pipe.\n"
720 		    "Please send your dmesg to <bugs@openbsd.org>, thanks.\n",
721 		    sc->sc_dev.dv_xname);
722 		printf("%s: intr buffer 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
723 		    sc->sc_dev.dv_xname,
724 		    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
725 
726 		/* check the buffer data */
727 		if (cdcbuf->bNotification == UCDC_N_SERIAL_STATE)
728 			printf("%s:notify serial state, len=%d, data=0x%02x\n",
729 			    sc->sc_dev.dv_xname,
730 			    cdcbuf->wLength, cdcbuf->data[0]);
731 	}
732 #endif
733 
734 	/* incidentally, Belkin adapter status bits match UART 16550 bits */
735 	sc->sc_lsr = buf[2];
736 	sc->sc_msr = buf[3];
737 
738 	DPRINTF(("%s: ubsa lsr = 0x%02x, msr = 0x%02x\n",
739 	    sc->sc_dev.dv_xname, sc->sc_lsr, sc->sc_msr));
740 
741 	ucom_status_change((struct ucom_softc *)sc->sc_subdev);
742 }
743 
744 void
745 ubsa_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
746 {
747 	struct ubsa_softc *sc = addr;
748 
749 	DPRINTF(("ubsa_get_status\n"));
750 
751 	if (lsr != NULL)
752 		*lsr = sc->sc_lsr;
753 	if (msr != NULL)
754 		*msr = sc->sc_msr;
755 }
756