xref: /freebsd/sys/dev/usb/serial/ubsa.c (revision d2ce15bd43b3a1dcce08eecbff8d5d359946d972)
1 /*-
2  * Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 /*-
30  * Copyright (c) 2001 The NetBSD Foundation, Inc.
31  * All rights reserved.
32  *
33  * This code is derived from software contributed to The NetBSD Foundation
34  * by Ichiro FUKUHARA (ichiro@ichiro.org).
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgement:
46  *        This product includes software developed by the NetBSD
47  *        Foundation, Inc. and its contributors.
48  * 4. Neither the name of The NetBSD Foundation nor the names of its
49  *    contributors may be used to endorse or promote products derived
50  *    from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
53  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
54  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
56  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62  * POSSIBILITY OF SUCH DAMAGE.
63  */
64 
65 #include <sys/stdint.h>
66 #include <sys/stddef.h>
67 #include <sys/param.h>
68 #include <sys/queue.h>
69 #include <sys/types.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/bus.h>
73 #include <sys/module.h>
74 #include <sys/lock.h>
75 #include <sys/mutex.h>
76 #include <sys/condvar.h>
77 #include <sys/sysctl.h>
78 #include <sys/sx.h>
79 #include <sys/unistd.h>
80 #include <sys/callout.h>
81 #include <sys/malloc.h>
82 #include <sys/priv.h>
83 
84 #include <dev/usb/usb.h>
85 #include <dev/usb/usbdi.h>
86 #include <dev/usb/usbdi_util.h>
87 #include "usbdevs.h"
88 
89 #define	USB_DEBUG_VAR ubsa_debug
90 #include <dev/usb/usb_debug.h>
91 #include <dev/usb/usb_process.h>
92 
93 #include <dev/usb/serial/usb_serial.h>
94 
95 #ifdef USB_DEBUG
96 static int ubsa_debug = 0;
97 
98 static SYSCTL_NODE(_hw_usb, OID_AUTO, ubsa, CTLFLAG_RW, 0, "USB ubsa");
99 SYSCTL_INT(_hw_usb_ubsa, OID_AUTO, debug, CTLFLAG_RW,
100     &ubsa_debug, 0, "ubsa debug level");
101 #endif
102 
103 #define	UBSA_BSIZE             1024	/* bytes */
104 
105 #define	UBSA_CONFIG_INDEX	0
106 #define	UBSA_IFACE_INDEX	0
107 
108 #define	UBSA_REG_BAUDRATE	0x00
109 #define	UBSA_REG_STOP_BITS	0x01
110 #define	UBSA_REG_DATA_BITS	0x02
111 #define	UBSA_REG_PARITY		0x03
112 #define	UBSA_REG_DTR		0x0A
113 #define	UBSA_REG_RTS		0x0B
114 #define	UBSA_REG_BREAK		0x0C
115 #define	UBSA_REG_FLOW_CTRL	0x10
116 
117 #define	UBSA_PARITY_NONE	0x00
118 #define	UBSA_PARITY_EVEN	0x01
119 #define	UBSA_PARITY_ODD		0x02
120 #define	UBSA_PARITY_MARK	0x03
121 #define	UBSA_PARITY_SPACE	0x04
122 
123 #define	UBSA_FLOW_NONE		0x0000
124 #define	UBSA_FLOW_OCTS		0x0001
125 #define	UBSA_FLOW_ODSR		0x0002
126 #define	UBSA_FLOW_IDSR		0x0004
127 #define	UBSA_FLOW_IDTR		0x0008
128 #define	UBSA_FLOW_IRTS		0x0010
129 #define	UBSA_FLOW_ORTS		0x0020
130 #define	UBSA_FLOW_UNKNOWN	0x0040
131 #define	UBSA_FLOW_OXON		0x0080
132 #define	UBSA_FLOW_IXON		0x0100
133 
134 /* line status register */
135 #define	UBSA_LSR_TSRE		0x40	/* Transmitter empty: byte sent */
136 #define	UBSA_LSR_TXRDY		0x20	/* Transmitter buffer empty */
137 #define	UBSA_LSR_BI		0x10	/* Break detected */
138 #define	UBSA_LSR_FE		0x08	/* Framing error: bad stop bit */
139 #define	UBSA_LSR_PE		0x04	/* Parity error */
140 #define	UBSA_LSR_OE		0x02	/* Overrun, lost incoming byte */
141 #define	UBSA_LSR_RXRDY		0x01	/* Byte ready in Receive Buffer */
142 #define	UBSA_LSR_RCV_MASK	0x1f	/* Mask for incoming data or error */
143 
144 /* modem status register */
145 /* All deltas are from the last read of the MSR. */
146 #define	UBSA_MSR_DCD		0x80	/* Current Data Carrier Detect */
147 #define	UBSA_MSR_RI		0x40	/* Current Ring Indicator */
148 #define	UBSA_MSR_DSR		0x20	/* Current Data Set Ready */
149 #define	UBSA_MSR_CTS		0x10	/* Current Clear to Send */
150 #define	UBSA_MSR_DDCD		0x08	/* DCD has changed state */
151 #define	UBSA_MSR_TERI		0x04	/* RI has toggled low to high */
152 #define	UBSA_MSR_DDSR		0x02	/* DSR has changed state */
153 #define	UBSA_MSR_DCTS		0x01	/* CTS has changed state */
154 
155 enum {
156 	UBSA_BULK_DT_WR,
157 	UBSA_BULK_DT_RD,
158 	UBSA_INTR_DT_RD,
159 	UBSA_N_TRANSFER,
160 };
161 
162 struct ubsa_softc {
163 	struct ucom_super_softc sc_super_ucom;
164 	struct ucom_softc sc_ucom;
165 
166 	struct usb_xfer *sc_xfer[UBSA_N_TRANSFER];
167 	struct usb_device *sc_udev;
168 	struct mtx sc_mtx;
169 
170 	uint8_t	sc_iface_no;		/* interface number */
171 	uint8_t	sc_iface_index;		/* interface index */
172 	uint8_t	sc_lsr;			/* local status register */
173 	uint8_t	sc_msr;			/* UBSA status register */
174 };
175 
176 static device_probe_t ubsa_probe;
177 static device_attach_t ubsa_attach;
178 static device_detach_t ubsa_detach;
179 static void ubsa_free_softc(struct ubsa_softc *);
180 
181 static usb_callback_t ubsa_write_callback;
182 static usb_callback_t ubsa_read_callback;
183 static usb_callback_t ubsa_intr_callback;
184 
185 static void	ubsa_cfg_request(struct ubsa_softc *, uint8_t, uint16_t);
186 static void	ubsa_free(struct ucom_softc *);
187 static void	ubsa_cfg_set_dtr(struct ucom_softc *, uint8_t);
188 static void	ubsa_cfg_set_rts(struct ucom_softc *, uint8_t);
189 static void	ubsa_cfg_set_break(struct ucom_softc *, uint8_t);
190 static int	ubsa_pre_param(struct ucom_softc *, struct termios *);
191 static void	ubsa_cfg_param(struct ucom_softc *, struct termios *);
192 static void	ubsa_start_read(struct ucom_softc *);
193 static void	ubsa_stop_read(struct ucom_softc *);
194 static void	ubsa_start_write(struct ucom_softc *);
195 static void	ubsa_stop_write(struct ucom_softc *);
196 static void	ubsa_cfg_get_status(struct ucom_softc *, uint8_t *,
197 		    uint8_t *);
198 static void	ubsa_poll(struct ucom_softc *ucom);
199 
200 static const struct usb_config ubsa_config[UBSA_N_TRANSFER] = {
201 
202 	[UBSA_BULK_DT_WR] = {
203 		.type = UE_BULK,
204 		.endpoint = UE_ADDR_ANY,
205 		.direction = UE_DIR_OUT,
206 		.bufsize = UBSA_BSIZE,	/* bytes */
207 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
208 		.callback = &ubsa_write_callback,
209 	},
210 
211 	[UBSA_BULK_DT_RD] = {
212 		.type = UE_BULK,
213 		.endpoint = UE_ADDR_ANY,
214 		.direction = UE_DIR_IN,
215 		.bufsize = UBSA_BSIZE,	/* bytes */
216 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
217 		.callback = &ubsa_read_callback,
218 	},
219 
220 	[UBSA_INTR_DT_RD] = {
221 		.type = UE_INTERRUPT,
222 		.endpoint = UE_ADDR_ANY,
223 		.direction = UE_DIR_IN,
224 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
225 		.bufsize = 0,	/* use wMaxPacketSize */
226 		.callback = &ubsa_intr_callback,
227 	},
228 };
229 
230 static const struct ucom_callback ubsa_callback = {
231 	.ucom_cfg_get_status = &ubsa_cfg_get_status,
232 	.ucom_cfg_set_dtr = &ubsa_cfg_set_dtr,
233 	.ucom_cfg_set_rts = &ubsa_cfg_set_rts,
234 	.ucom_cfg_set_break = &ubsa_cfg_set_break,
235 	.ucom_cfg_param = &ubsa_cfg_param,
236 	.ucom_pre_param = &ubsa_pre_param,
237 	.ucom_start_read = &ubsa_start_read,
238 	.ucom_stop_read = &ubsa_stop_read,
239 	.ucom_start_write = &ubsa_start_write,
240 	.ucom_stop_write = &ubsa_stop_write,
241 	.ucom_poll = &ubsa_poll,
242 	.ucom_free = &ubsa_free,
243 };
244 
245 static const STRUCT_USB_HOST_ID ubsa_devs[] = {
246 	/* AnyData ADU-500A */
247 	{USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_500A, 0)},
248 	/* AnyData ADU-E100A/H */
249 	{USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_E100X, 0)},
250 	/* Axesstel MV100H */
251 	{USB_VPI(USB_VENDOR_AXESSTEL, USB_PRODUCT_AXESSTEL_DATAMODEM, 0)},
252 	/* BELKIN F5U103 */
253 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103, 0)},
254 	/* BELKIN F5U120 */
255 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120, 0)},
256 	/* GoHubs GO-COM232 */
257 	{USB_VPI(USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM, 0)},
258 	/* GoHubs GO-COM232 */
259 	{USB_VPI(USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232, 0)},
260 	/* Peracom */
261 	{USB_VPI(USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1, 0)},
262 };
263 
264 static device_method_t ubsa_methods[] = {
265 	DEVMETHOD(device_probe, ubsa_probe),
266 	DEVMETHOD(device_attach, ubsa_attach),
267 	DEVMETHOD(device_detach, ubsa_detach),
268 	DEVMETHOD_END
269 };
270 
271 static devclass_t ubsa_devclass;
272 
273 static driver_t ubsa_driver = {
274 	.name = "ubsa",
275 	.methods = ubsa_methods,
276 	.size = sizeof(struct ubsa_softc),
277 };
278 
279 DRIVER_MODULE(ubsa, uhub, ubsa_driver, ubsa_devclass, NULL, 0);
280 MODULE_DEPEND(ubsa, ucom, 1, 1, 1);
281 MODULE_DEPEND(ubsa, usb, 1, 1, 1);
282 MODULE_VERSION(ubsa, 1);
283 
284 static int
285 ubsa_probe(device_t dev)
286 {
287 	struct usb_attach_arg *uaa = device_get_ivars(dev);
288 
289 	if (uaa->usb_mode != USB_MODE_HOST) {
290 		return (ENXIO);
291 	}
292 	if (uaa->info.bConfigIndex != UBSA_CONFIG_INDEX) {
293 		return (ENXIO);
294 	}
295 	if (uaa->info.bIfaceIndex != UBSA_IFACE_INDEX) {
296 		return (ENXIO);
297 	}
298 	return (usbd_lookup_id_by_uaa(ubsa_devs, sizeof(ubsa_devs), uaa));
299 }
300 
301 static int
302 ubsa_attach(device_t dev)
303 {
304 	struct usb_attach_arg *uaa = device_get_ivars(dev);
305 	struct ubsa_softc *sc = device_get_softc(dev);
306 	int error;
307 
308 	DPRINTF("sc=%p\n", sc);
309 
310 	device_set_usb_desc(dev);
311 	mtx_init(&sc->sc_mtx, "ubsa", NULL, MTX_DEF);
312 	ucom_ref(&sc->sc_super_ucom);
313 
314 	sc->sc_udev = uaa->device;
315 	sc->sc_iface_no = uaa->info.bIfaceNum;
316 	sc->sc_iface_index = UBSA_IFACE_INDEX;
317 
318 	error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
319 	    sc->sc_xfer, ubsa_config, UBSA_N_TRANSFER, sc, &sc->sc_mtx);
320 
321 	if (error) {
322 		DPRINTF("could not allocate all pipes\n");
323 		goto detach;
324 	}
325 	/* clear stall at first run */
326 	mtx_lock(&sc->sc_mtx);
327 	usbd_xfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_WR]);
328 	usbd_xfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_RD]);
329 	mtx_unlock(&sc->sc_mtx);
330 
331 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
332 	    &ubsa_callback, &sc->sc_mtx);
333 	if (error) {
334 		DPRINTF("ucom_attach failed\n");
335 		goto detach;
336 	}
337 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
338 
339 	return (0);
340 
341 detach:
342 	ubsa_detach(dev);
343 	return (ENXIO);
344 }
345 
346 static int
347 ubsa_detach(device_t dev)
348 {
349 	struct ubsa_softc *sc = device_get_softc(dev);
350 
351 	DPRINTF("sc=%p\n", sc);
352 
353 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
354 	usbd_transfer_unsetup(sc->sc_xfer, UBSA_N_TRANSFER);
355 
356 	device_claim_softc(dev);
357 
358 	ubsa_free_softc(sc);
359 
360 	return (0);
361 }
362 
363 UCOM_UNLOAD_DRAIN(ubsa);
364 
365 static void
366 ubsa_free_softc(struct ubsa_softc *sc)
367 {
368 	if (ucom_unref(&sc->sc_super_ucom)) {
369 		mtx_destroy(&sc->sc_mtx);
370 		device_free_softc(sc);
371 	}
372 }
373 
374 static void
375 ubsa_free(struct ucom_softc *ucom)
376 {
377 	ubsa_free_softc(ucom->sc_parent);
378 }
379 
380 static void
381 ubsa_cfg_request(struct ubsa_softc *sc, uint8_t index, uint16_t value)
382 {
383 	struct usb_device_request req;
384 	usb_error_t err;
385 
386 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
387 	req.bRequest = index;
388 	USETW(req.wValue, value);
389 	req.wIndex[0] = sc->sc_iface_no;
390 	req.wIndex[1] = 0;
391 	USETW(req.wLength, 0);
392 
393 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
394 	    &req, NULL, 0, 1000);
395 	if (err) {
396 		DPRINTFN(0, "device request failed, err=%s "
397 		    "(ignored)\n", usbd_errstr(err));
398 	}
399 }
400 
401 static void
402 ubsa_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
403 {
404 	struct ubsa_softc *sc = ucom->sc_parent;
405 
406 	DPRINTF("onoff = %d\n", onoff);
407 
408 	ubsa_cfg_request(sc, UBSA_REG_DTR, onoff ? 1 : 0);
409 }
410 
411 static void
412 ubsa_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
413 {
414 	struct ubsa_softc *sc = ucom->sc_parent;
415 
416 	DPRINTF("onoff = %d\n", onoff);
417 
418 	ubsa_cfg_request(sc, UBSA_REG_RTS, onoff ? 1 : 0);
419 }
420 
421 static void
422 ubsa_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
423 {
424 	struct ubsa_softc *sc = ucom->sc_parent;
425 
426 	DPRINTF("onoff = %d\n", onoff);
427 
428 	ubsa_cfg_request(sc, UBSA_REG_BREAK, onoff ? 1 : 0);
429 }
430 
431 static int
432 ubsa_pre_param(struct ucom_softc *ucom, struct termios *t)
433 {
434 
435 	DPRINTF("sc = %p\n", ucom->sc_parent);
436 
437 	switch (t->c_ospeed) {
438 	case B0:
439 	case B300:
440 	case B600:
441 	case B1200:
442 	case B2400:
443 	case B4800:
444 	case B9600:
445 	case B19200:
446 	case B38400:
447 	case B57600:
448 	case B115200:
449 	case B230400:
450 		break;
451 	default:
452 		return (EINVAL);
453 	}
454 	return (0);
455 }
456 
457 static void
458 ubsa_cfg_param(struct ucom_softc *ucom, struct termios *t)
459 {
460 	struct ubsa_softc *sc = ucom->sc_parent;
461 	uint16_t value = 0;
462 
463 	DPRINTF("sc = %p\n", sc);
464 
465 	switch (t->c_ospeed) {
466 	case B0:
467 		ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, 0);
468 		ubsa_cfg_set_dtr(&sc->sc_ucom, 0);
469 		ubsa_cfg_set_rts(&sc->sc_ucom, 0);
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 / t->c_ospeed;
483 		ubsa_cfg_request(sc, UBSA_REG_BAUDRATE, value);
484 		break;
485 	default:
486 		return;
487 	}
488 
489 	if (t->c_cflag & PARENB)
490 		value = (t->c_cflag & PARODD) ? UBSA_PARITY_ODD : UBSA_PARITY_EVEN;
491 	else
492 		value = UBSA_PARITY_NONE;
493 
494 	ubsa_cfg_request(sc, UBSA_REG_PARITY, value);
495 
496 	switch (t->c_cflag & CSIZE) {
497 	case CS5:
498 		value = 0;
499 		break;
500 	case CS6:
501 		value = 1;
502 		break;
503 	case CS7:
504 		value = 2;
505 		break;
506 	default:
507 	case CS8:
508 		value = 3;
509 		break;
510 	}
511 
512 	ubsa_cfg_request(sc, UBSA_REG_DATA_BITS, value);
513 
514 	value = (t->c_cflag & CSTOPB) ? 1 : 0;
515 
516 	ubsa_cfg_request(sc, UBSA_REG_STOP_BITS, value);
517 
518 	value = 0;
519 	if (t->c_cflag & CRTSCTS)
520 		value |= UBSA_FLOW_OCTS | UBSA_FLOW_IRTS;
521 
522 	if (t->c_iflag & (IXON | IXOFF))
523 		value |= UBSA_FLOW_OXON | UBSA_FLOW_IXON;
524 
525 	ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, value);
526 }
527 
528 static void
529 ubsa_start_read(struct ucom_softc *ucom)
530 {
531 	struct ubsa_softc *sc = ucom->sc_parent;
532 
533 	/* start interrupt endpoint */
534 	usbd_transfer_start(sc->sc_xfer[UBSA_INTR_DT_RD]);
535 
536 	/* start read endpoint */
537 	usbd_transfer_start(sc->sc_xfer[UBSA_BULK_DT_RD]);
538 }
539 
540 static void
541 ubsa_stop_read(struct ucom_softc *ucom)
542 {
543 	struct ubsa_softc *sc = ucom->sc_parent;
544 
545 	/* stop interrupt endpoint */
546 	usbd_transfer_stop(sc->sc_xfer[UBSA_INTR_DT_RD]);
547 
548 	/* stop read endpoint */
549 	usbd_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_RD]);
550 }
551 
552 static void
553 ubsa_start_write(struct ucom_softc *ucom)
554 {
555 	struct ubsa_softc *sc = ucom->sc_parent;
556 
557 	usbd_transfer_start(sc->sc_xfer[UBSA_BULK_DT_WR]);
558 }
559 
560 static void
561 ubsa_stop_write(struct ucom_softc *ucom)
562 {
563 	struct ubsa_softc *sc = ucom->sc_parent;
564 
565 	usbd_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_WR]);
566 }
567 
568 static void
569 ubsa_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
570 {
571 	struct ubsa_softc *sc = ucom->sc_parent;
572 
573 	DPRINTF("\n");
574 
575 	*lsr = sc->sc_lsr;
576 	*msr = sc->sc_msr;
577 }
578 
579 static void
580 ubsa_write_callback(struct usb_xfer *xfer, usb_error_t error)
581 {
582 	struct ubsa_softc *sc = usbd_xfer_softc(xfer);
583 	struct usb_page_cache *pc;
584 	uint32_t actlen;
585 
586 	switch (USB_GET_STATE(xfer)) {
587 	case USB_ST_SETUP:
588 	case USB_ST_TRANSFERRED:
589 tr_setup:
590 		pc = usbd_xfer_get_frame(xfer, 0);
591 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
592 		    UBSA_BSIZE, &actlen)) {
593 
594 			usbd_xfer_set_frame_len(xfer, 0, actlen);
595 			usbd_transfer_submit(xfer);
596 		}
597 		return;
598 
599 	default:			/* Error */
600 		if (error != USB_ERR_CANCELLED) {
601 			/* try to clear stall first */
602 			usbd_xfer_set_stall(xfer);
603 			goto tr_setup;
604 		}
605 		return;
606 
607 	}
608 }
609 
610 static void
611 ubsa_read_callback(struct usb_xfer *xfer, usb_error_t error)
612 {
613 	struct ubsa_softc *sc = usbd_xfer_softc(xfer);
614 	struct usb_page_cache *pc;
615 	int actlen;
616 
617 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
618 
619 	switch (USB_GET_STATE(xfer)) {
620 	case USB_ST_TRANSFERRED:
621 		pc = usbd_xfer_get_frame(xfer, 0);
622 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
623 
624 	case USB_ST_SETUP:
625 tr_setup:
626 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
627 		usbd_transfer_submit(xfer);
628 		return;
629 
630 	default:			/* Error */
631 		if (error != USB_ERR_CANCELLED) {
632 			/* try to clear stall first */
633 			usbd_xfer_set_stall(xfer);
634 			goto tr_setup;
635 		}
636 		return;
637 
638 	}
639 }
640 
641 static void
642 ubsa_intr_callback(struct usb_xfer *xfer, usb_error_t error)
643 {
644 	struct ubsa_softc *sc = usbd_xfer_softc(xfer);
645 	struct usb_page_cache *pc;
646 	uint8_t buf[4];
647 	int actlen;
648 
649 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
650 
651 	switch (USB_GET_STATE(xfer)) {
652 	case USB_ST_TRANSFERRED:
653 
654 		if (actlen >= (int)sizeof(buf)) {
655 			pc = usbd_xfer_get_frame(xfer, 0);
656 			usbd_copy_out(pc, 0, buf, sizeof(buf));
657 
658 			/*
659 			 * incidentally, Belkin adapter status bits match
660 			 * UART 16550 bits
661 			 */
662 			sc->sc_lsr = buf[2];
663 			sc->sc_msr = buf[3];
664 
665 			DPRINTF("lsr = 0x%02x, msr = 0x%02x\n",
666 			    sc->sc_lsr, sc->sc_msr);
667 
668 			ucom_status_change(&sc->sc_ucom);
669 		} else {
670 			DPRINTF("ignoring short packet, %d bytes\n", actlen);
671 		}
672 
673 	case USB_ST_SETUP:
674 tr_setup:
675 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
676 		usbd_transfer_submit(xfer);
677 		return;
678 
679 	default:			/* Error */
680 		if (error != USB_ERR_CANCELLED) {
681 			/* try to clear stall first */
682 			usbd_xfer_set_stall(xfer);
683 			goto tr_setup;
684 		}
685 		return;
686 
687 	}
688 }
689 
690 static void
691 ubsa_poll(struct ucom_softc *ucom)
692 {
693 	struct ubsa_softc *sc = ucom->sc_parent;
694 	usbd_transfer_poll(sc->sc_xfer, UBSA_N_TRANSFER);
695 
696 }
697