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