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