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