1239310Sdim/*-
2239310Sdim * Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>.
3239310Sdim * All rights reserved.
4239310Sdim *
5239310Sdim * Redistribution and use in source and binary forms, with or without
6239310Sdim * modification, are permitted provided that the following conditions
7239310Sdim * are met:
8239310Sdim * 1. Redistributions of source code must retain the above copyright
9239310Sdim *    notice, this list of conditions and the following disclaimer.
10239310Sdim * 2. Redistributions in binary form must reproduce the above copyright
11239310Sdim *    notice, this list of conditions and the following disclaimer in the
12239310Sdim *    documentation and/or other materials provided with the distribution.
13239310Sdim *
14239310Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15239310Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16239310Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17239310Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18239310Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19249423Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20239310Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21249423Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22249423Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23239310Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24249423Sdim * SUCH DAMAGE.
25239310Sdim */
26239310Sdim
27239310Sdim#include <sys/cdefs.h>
28249423Sdim__FBSDID("$FreeBSD$");
29239310Sdim/*-
30239310Sdim * Copyright (c) 2001 The NetBSD Foundation, Inc.
31249423Sdim * All rights reserved.
32239310Sdim *
33239310Sdim * This code is derived from software contributed to The NetBSD Foundation
34239310Sdim * by Ichiro FUKUHARA (ichiro@ichiro.org).
35239310Sdim *
36239310Sdim * Redistribution and use in source and binary forms, with or without
37239310Sdim * modification, are permitted provided that the following conditions
38239310Sdim * are met:
39239310Sdim * 1. Redistributions of source code must retain the above copyright
40239310Sdim *    notice, this list of conditions and the following disclaimer.
41239310Sdim * 2. Redistributions in binary form must reproduce the above copyright
42239310Sdim *    notice, this list of conditions and the following disclaimer in the
43239310Sdim *    documentation and/or other materials provided with the distribution.
44239310Sdim *
45239310Sdim * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
46249423Sdim * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
47249423Sdim * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48239310Sdim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
49239310Sdim * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50239310Sdim * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51239310Sdim * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52239310Sdim * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53239310Sdim * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54249423Sdim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55239310Sdim * POSSIBILITY OF SUCH DAMAGE.
56239310Sdim */
57239310Sdim
58239310Sdim#include <sys/stdint.h>
59239310Sdim#include <sys/stddef.h>
60239310Sdim#include <sys/param.h>
61239310Sdim#include <sys/queue.h>
62239310Sdim#include <sys/types.h>
63239310Sdim#include <sys/systm.h>
64239310Sdim#include <sys/kernel.h>
65239310Sdim#include <sys/bus.h>
66249423Sdim#include <sys/module.h>
67239310Sdim#include <sys/lock.h>
68239310Sdim#include <sys/mutex.h>
69239310Sdim#include <sys/condvar.h>
70239310Sdim#include <sys/sysctl.h>
71239310Sdim#include <sys/sx.h>
72239310Sdim#include <sys/unistd.h>
73239310Sdim#include <sys/callout.h>
74239310Sdim#include <sys/malloc.h>
75239310Sdim#include <sys/priv.h>
76239310Sdim
77239310Sdim#include <dev/usb/usb.h>
78239310Sdim#include <dev/usb/usbdi.h>
79239310Sdim#include <dev/usb/usbdi_util.h>
80239310Sdim#include "usbdevs.h"
81239310Sdim
82239310Sdim#define	USB_DEBUG_VAR ubsa_debug
83239310Sdim#include <dev/usb/usb_debug.h>
84239310Sdim#include <dev/usb/usb_process.h>
85239310Sdim
86239310Sdim#include <dev/usb/serial/usb_serial.h>
87239310Sdim
88239310Sdim#ifdef USB_DEBUG
89239310Sdimstatic int ubsa_debug = 0;
90249423Sdim
91239310Sdimstatic SYSCTL_NODE(_hw_usb, OID_AUTO, ubsa, CTLFLAG_RW, 0, "USB ubsa");
92239310SdimSYSCTL_INT(_hw_usb_ubsa, OID_AUTO, debug, CTLFLAG_RW,
93239310Sdim    &ubsa_debug, 0, "ubsa debug level");
94251662Sdim#endif
95239310Sdim
96239310Sdim#define	UBSA_BSIZE             1024	/* bytes */
97239310Sdim
98239310Sdim#define	UBSA_CONFIG_INDEX	0
99239310Sdim#define	UBSA_IFACE_INDEX	0
100239310Sdim
101239310Sdim#define	UBSA_REG_BAUDRATE	0x00
102239310Sdim#define	UBSA_REG_STOP_BITS	0x01
103249423Sdim#define	UBSA_REG_DATA_BITS	0x02
104239310Sdim#define	UBSA_REG_PARITY		0x03
105239310Sdim#define	UBSA_REG_DTR		0x0A
106239310Sdim#define	UBSA_REG_RTS		0x0B
107239310Sdim#define	UBSA_REG_BREAK		0x0C
108239310Sdim#define	UBSA_REG_FLOW_CTRL	0x10
109249423Sdim
110239310Sdim#define	UBSA_PARITY_NONE	0x00
111249423Sdim#define	UBSA_PARITY_EVEN	0x01
112249423Sdim#define	UBSA_PARITY_ODD		0x02
113249423Sdim#define	UBSA_PARITY_MARK	0x03
114239310Sdim#define	UBSA_PARITY_SPACE	0x04
115249423Sdim
116239310Sdim#define	UBSA_FLOW_NONE		0x0000
117249423Sdim#define	UBSA_FLOW_OCTS		0x0001
118239310Sdim#define	UBSA_FLOW_ODSR		0x0002
119249423Sdim#define	UBSA_FLOW_IDSR		0x0004
120239310Sdim#define	UBSA_FLOW_IDTR		0x0008
121239310Sdim#define	UBSA_FLOW_IRTS		0x0010
122239310Sdim#define	UBSA_FLOW_ORTS		0x0020
123239310Sdim#define	UBSA_FLOW_UNKNOWN	0x0040
124249423Sdim#define	UBSA_FLOW_OXON		0x0080
125249423Sdim#define	UBSA_FLOW_IXON		0x0100
126239310Sdim
127249423Sdim/* line status register */
128239310Sdim#define	UBSA_LSR_TSRE		0x40	/* Transmitter empty: byte sent */
129239310Sdim#define	UBSA_LSR_TXRDY		0x20	/* Transmitter buffer empty */
130239310Sdim#define	UBSA_LSR_BI		0x10	/* Break detected */
131251662Sdim#define	UBSA_LSR_FE		0x08	/* Framing error: bad stop bit */
132239310Sdim#define	UBSA_LSR_PE		0x04	/* Parity error */
133239310Sdim#define	UBSA_LSR_OE		0x02	/* Overrun, lost incoming byte */
134239310Sdim#define	UBSA_LSR_RXRDY		0x01	/* Byte ready in Receive Buffer */
135239310Sdim#define	UBSA_LSR_RCV_MASK	0x1f	/* Mask for incoming data or error */
136239310Sdim
137239310Sdim/* modem status register */
138239310Sdim/* All deltas are from the last read of the MSR. */
139249423Sdim#define	UBSA_MSR_DCD		0x80	/* Current Data Carrier Detect */
140239310Sdim#define	UBSA_MSR_RI		0x40	/* Current Ring Indicator */
141239310Sdim#define	UBSA_MSR_DSR		0x20	/* Current Data Set Ready */
142249423Sdim#define	UBSA_MSR_CTS		0x10	/* Current Clear to Send */
143239310Sdim#define	UBSA_MSR_DDCD		0x08	/* DCD has changed state */
144239310Sdim#define	UBSA_MSR_TERI		0x04	/* RI has toggled low to high */
145239310Sdim#define	UBSA_MSR_DDSR		0x02	/* DSR has changed state */
146239310Sdim#define	UBSA_MSR_DCTS		0x01	/* CTS has changed state */
147239310Sdim
148239310Sdimenum {
149239310Sdim	UBSA_BULK_DT_WR,
150239310Sdim	UBSA_BULK_DT_RD,
151239310Sdim	UBSA_INTR_DT_RD,
152249423Sdim	UBSA_N_TRANSFER,
153239310Sdim};
154239310Sdim
155239310Sdimstruct ubsa_softc {
156251662Sdim	struct ucom_super_softc sc_super_ucom;
157251662Sdim	struct ucom_softc sc_ucom;
158239310Sdim
159239310Sdim	struct usb_xfer *sc_xfer[UBSA_N_TRANSFER];
160251662Sdim	struct usb_device *sc_udev;
161239310Sdim	struct mtx sc_mtx;
162239310Sdim
163239310Sdim	uint8_t	sc_iface_no;		/* interface number */
164239310Sdim	uint8_t	sc_iface_index;		/* interface index */
165239310Sdim	uint8_t	sc_lsr;			/* local status register */
166249423Sdim	uint8_t	sc_msr;			/* UBSA status register */
167239310Sdim};
168239310Sdim
169249423Sdimstatic device_probe_t ubsa_probe;
170249423Sdimstatic device_attach_t ubsa_attach;
171249423Sdimstatic device_detach_t ubsa_detach;
172249423Sdimstatic void ubsa_free_softc(struct ubsa_softc *);
173239310Sdim
174239310Sdimstatic usb_callback_t ubsa_write_callback;
175239310Sdimstatic usb_callback_t ubsa_read_callback;
176239310Sdimstatic usb_callback_t ubsa_intr_callback;
177239310Sdim
178239310Sdimstatic void	ubsa_cfg_request(struct ubsa_softc *, uint8_t, uint16_t);
179239310Sdimstatic void	ubsa_free(struct ucom_softc *);
180239310Sdimstatic void	ubsa_cfg_set_dtr(struct ucom_softc *, uint8_t);
181239310Sdimstatic void	ubsa_cfg_set_rts(struct ucom_softc *, uint8_t);
182249423Sdimstatic void	ubsa_cfg_set_break(struct ucom_softc *, uint8_t);
183249423Sdimstatic int	ubsa_pre_param(struct ucom_softc *, struct termios *);
184239310Sdimstatic void	ubsa_cfg_param(struct ucom_softc *, struct termios *);
185239310Sdimstatic void	ubsa_start_read(struct ucom_softc *);
186239310Sdimstatic void	ubsa_stop_read(struct ucom_softc *);
187239310Sdimstatic void	ubsa_start_write(struct ucom_softc *);
188239310Sdimstatic void	ubsa_stop_write(struct ucom_softc *);
189239310Sdimstatic void	ubsa_cfg_get_status(struct ucom_softc *, uint8_t *,
190239310Sdim		    uint8_t *);
191239310Sdimstatic void	ubsa_poll(struct ucom_softc *ucom);
192239310Sdim
193239310Sdimstatic const struct usb_config ubsa_config[UBSA_N_TRANSFER] = {
194239310Sdim
195239310Sdim	[UBSA_BULK_DT_WR] = {
196239310Sdim		.type = UE_BULK,
197239310Sdim		.endpoint = UE_ADDR_ANY,
198249423Sdim		.direction = UE_DIR_OUT,
199239310Sdim		.bufsize = UBSA_BSIZE,	/* bytes */
200249423Sdim		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
201249423Sdim		.callback = &ubsa_write_callback,
202249423Sdim	},
203239310Sdim
204249423Sdim	[UBSA_BULK_DT_RD] = {
205239310Sdim		.type = UE_BULK,
206239310Sdim		.endpoint = UE_ADDR_ANY,
207239310Sdim		.direction = UE_DIR_IN,
208251662Sdim		.bufsize = UBSA_BSIZE,	/* bytes */
209251662Sdim		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
210239310Sdim		.callback = &ubsa_read_callback,
211239310Sdim	},
212239310Sdim
213251662Sdim	[UBSA_INTR_DT_RD] = {
214239310Sdim		.type = UE_INTERRUPT,
215249423Sdim		.endpoint = UE_ADDR_ANY,
216239310Sdim		.direction = UE_DIR_IN,
217239310Sdim		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
218239310Sdim		.bufsize = 0,	/* use wMaxPacketSize */
219239310Sdim		.callback = &ubsa_intr_callback,
220239310Sdim	},
221249423Sdim};
222239310Sdim
223239310Sdimstatic const struct ucom_callback ubsa_callback = {
224239310Sdim	.ucom_cfg_get_status = &ubsa_cfg_get_status,
225239310Sdim	.ucom_cfg_set_dtr = &ubsa_cfg_set_dtr,
226239310Sdim	.ucom_cfg_set_rts = &ubsa_cfg_set_rts,
227239310Sdim	.ucom_cfg_set_break = &ubsa_cfg_set_break,
228239310Sdim	.ucom_cfg_param = &ubsa_cfg_param,
229239310Sdim	.ucom_pre_param = &ubsa_pre_param,
230239310Sdim	.ucom_start_read = &ubsa_start_read,
231239310Sdim	.ucom_stop_read = &ubsa_stop_read,
232239310Sdim	.ucom_start_write = &ubsa_start_write,
233239310Sdim	.ucom_stop_write = &ubsa_stop_write,
234239310Sdim	.ucom_poll = &ubsa_poll,
235239310Sdim	.ucom_free = &ubsa_free,
236239310Sdim};
237239310Sdim
238239310Sdimstatic const STRUCT_USB_HOST_ID ubsa_devs[] = {
239251662Sdim	/* AnyData ADU-500A */
240251662Sdim	{USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_500A, 0)},
241239310Sdim	/* AnyData ADU-E100A/H */
242239310Sdim	{USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_E100X, 0)},
243239310Sdim	/* Axesstel MV100H */
244239310Sdim	{USB_VPI(USB_VENDOR_AXESSTEL, USB_PRODUCT_AXESSTEL_DATAMODEM, 0)},
245239310Sdim	/* BELKIN F5U103 */
246239310Sdim	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103, 0)},
247239310Sdim	/* BELKIN F5U120 */
248239310Sdim	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120, 0)},
249239310Sdim	/* GoHubs GO-COM232 */
250239310Sdim	{USB_VPI(USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM, 0)},
251239310Sdim	/* GoHubs GO-COM232 */
252239310Sdim	{USB_VPI(USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232, 0)},
253239310Sdim	/* Peracom */
254251662Sdim	{USB_VPI(USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1, 0)},
255239310Sdim};
256239310Sdim
257239310Sdimstatic device_method_t ubsa_methods[] = {
258239310Sdim	DEVMETHOD(device_probe, ubsa_probe),
259239310Sdim	DEVMETHOD(device_attach, ubsa_attach),
260239310Sdim	DEVMETHOD(device_detach, ubsa_detach),
261249423Sdim	DEVMETHOD_END
262249423Sdim};
263251662Sdim
264249423Sdimstatic devclass_t ubsa_devclass;
265251662Sdim
266251662Sdimstatic driver_t ubsa_driver = {
267239310Sdim	.name = "ubsa",
268239310Sdim	.methods = ubsa_methods,
269239310Sdim	.size = sizeof(struct ubsa_softc),
270249423Sdim};
271251662Sdim
272239310SdimDRIVER_MODULE(ubsa, uhub, ubsa_driver, ubsa_devclass, NULL, 0);
273239310SdimMODULE_DEPEND(ubsa, ucom, 1, 1, 1);
274239310SdimMODULE_DEPEND(ubsa, usb, 1, 1, 1);
275239310SdimMODULE_VERSION(ubsa, 1);
276239310Sdim
277239310Sdimstatic int
278239310Sdimubsa_probe(device_t dev)
279239310Sdim{
280249423Sdim	struct usb_attach_arg *uaa = device_get_ivars(dev);
281249423Sdim
282249423Sdim	if (uaa->usb_mode != USB_MODE_HOST) {
283239310Sdim		return (ENXIO);
284239310Sdim	}
285239310Sdim	if (uaa->info.bConfigIndex != UBSA_CONFIG_INDEX) {
286239310Sdim		return (ENXIO);
287239310Sdim	}
288239310Sdim	if (uaa->info.bIfaceIndex != UBSA_IFACE_INDEX) {
289239310Sdim		return (ENXIO);
290239310Sdim	}
291239310Sdim	return (usbd_lookup_id_by_uaa(ubsa_devs, sizeof(ubsa_devs), uaa));
292239310Sdim}
293239310Sdim
294239310Sdimstatic int
295239310Sdimubsa_attach(device_t dev)
296239310Sdim{
297239310Sdim	struct usb_attach_arg *uaa = device_get_ivars(dev);
298239310Sdim	struct ubsa_softc *sc = device_get_softc(dev);
299239310Sdim	int error;
300239310Sdim
301239310Sdim	DPRINTF("sc=%p\n", sc);
302239310Sdim
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
334detach:
335	ubsa_detach(dev);
336	return (ENXIO);
337}
338
339static int
340ubsa_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
356UCOM_UNLOAD_DRAIN(ubsa);
357
358static void
359ubsa_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
367static void
368ubsa_free(struct ucom_softc *ucom)
369{
370	ubsa_free_softc(ucom->sc_parent);
371}
372
373static void
374ubsa_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
394static void
395ubsa_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
404static void
405ubsa_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
414static void
415ubsa_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
424static int
425ubsa_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
450static void
451ubsa_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
521static void
522ubsa_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
533static void
534ubsa_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
545static void
546ubsa_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
553static void
554ubsa_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
561static void
562ubsa_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
572static void
573ubsa_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:
582tr_setup:
583		pc = usbd_xfer_get_frame(xfer, 0);
584		if (ucom_get_data(&sc->sc_ucom, pc, 0,
585		    UBSA_BSIZE, &actlen)) {
586
587			usbd_xfer_set_frame_len(xfer, 0, actlen);
588			usbd_transfer_submit(xfer);
589		}
590		return;
591
592	default:			/* Error */
593		if (error != USB_ERR_CANCELLED) {
594			/* try to clear stall first */
595			usbd_xfer_set_stall(xfer);
596			goto tr_setup;
597		}
598		return;
599
600	}
601}
602
603static void
604ubsa_read_callback(struct usb_xfer *xfer, usb_error_t error)
605{
606	struct ubsa_softc *sc = usbd_xfer_softc(xfer);
607	struct usb_page_cache *pc;
608	int actlen;
609
610	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
611
612	switch (USB_GET_STATE(xfer)) {
613	case USB_ST_TRANSFERRED:
614		pc = usbd_xfer_get_frame(xfer, 0);
615		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
616
617	case USB_ST_SETUP:
618tr_setup:
619		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
620		usbd_transfer_submit(xfer);
621		return;
622
623	default:			/* Error */
624		if (error != USB_ERR_CANCELLED) {
625			/* try to clear stall first */
626			usbd_xfer_set_stall(xfer);
627			goto tr_setup;
628		}
629		return;
630
631	}
632}
633
634static void
635ubsa_intr_callback(struct usb_xfer *xfer, usb_error_t error)
636{
637	struct ubsa_softc *sc = usbd_xfer_softc(xfer);
638	struct usb_page_cache *pc;
639	uint8_t buf[4];
640	int actlen;
641
642	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
643
644	switch (USB_GET_STATE(xfer)) {
645	case USB_ST_TRANSFERRED:
646
647		if (actlen >= (int)sizeof(buf)) {
648			pc = usbd_xfer_get_frame(xfer, 0);
649			usbd_copy_out(pc, 0, buf, sizeof(buf));
650
651			/*
652			 * incidentally, Belkin adapter status bits match
653			 * UART 16550 bits
654			 */
655			sc->sc_lsr = buf[2];
656			sc->sc_msr = buf[3];
657
658			DPRINTF("lsr = 0x%02x, msr = 0x%02x\n",
659			    sc->sc_lsr, sc->sc_msr);
660
661			ucom_status_change(&sc->sc_ucom);
662		} else {
663			DPRINTF("ignoring short packet, %d bytes\n", actlen);
664		}
665
666	case USB_ST_SETUP:
667tr_setup:
668		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
669		usbd_transfer_submit(xfer);
670		return;
671
672	default:			/* Error */
673		if (error != USB_ERR_CANCELLED) {
674			/* try to clear stall first */
675			usbd_xfer_set_stall(xfer);
676			goto tr_setup;
677		}
678		return;
679
680	}
681}
682
683static void
684ubsa_poll(struct ucom_softc *ucom)
685{
686	struct ubsa_softc *sc = ucom->sc_parent;
687	usbd_transfer_poll(sc->sc_xfer, UBSA_N_TRANSFER);
688
689}
690