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