xref: /freebsd-src/sys/dev/usb/serial/ubsa.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
102ac6454SAndrew Thompson /*-
2*eebd9d53SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
402ac6454SAndrew Thompson  * Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>.
502ac6454SAndrew Thompson  * All rights reserved.
602ac6454SAndrew Thompson  *
702ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
802ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
902ac6454SAndrew Thompson  * are met:
1002ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1202ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1302ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1402ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1502ac6454SAndrew Thompson  *
1602ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1702ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1802ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1902ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2002ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2102ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2202ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2302ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2402ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2502ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2602ac6454SAndrew Thompson  * SUCH DAMAGE.
2702ac6454SAndrew Thompson  */
2802ac6454SAndrew Thompson 
2902ac6454SAndrew Thompson #include <sys/cdefs.h>
3002ac6454SAndrew Thompson /*-
3102ac6454SAndrew Thompson  * Copyright (c) 2001 The NetBSD Foundation, Inc.
3202ac6454SAndrew Thompson  * All rights reserved.
3302ac6454SAndrew Thompson  *
3402ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
3502ac6454SAndrew Thompson  * by Ichiro FUKUHARA (ichiro@ichiro.org).
3602ac6454SAndrew Thompson  *
3702ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
3802ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
3902ac6454SAndrew Thompson  * are met:
4002ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
4102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
4202ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
4302ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
4402ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
4502ac6454SAndrew Thompson  *
4602ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
4702ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
4802ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4902ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5002ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5102ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5202ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5302ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
5402ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5502ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
5602ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
5702ac6454SAndrew Thompson  */
5802ac6454SAndrew Thompson 
59ed6d949aSAndrew Thompson #include <sys/stdint.h>
60ed6d949aSAndrew Thompson #include <sys/stddef.h>
61ed6d949aSAndrew Thompson #include <sys/param.h>
62ed6d949aSAndrew Thompson #include <sys/queue.h>
63ed6d949aSAndrew Thompson #include <sys/types.h>
64ed6d949aSAndrew Thompson #include <sys/systm.h>
65ed6d949aSAndrew Thompson #include <sys/kernel.h>
66ed6d949aSAndrew Thompson #include <sys/bus.h>
67ed6d949aSAndrew Thompson #include <sys/module.h>
68ed6d949aSAndrew Thompson #include <sys/lock.h>
69ed6d949aSAndrew Thompson #include <sys/mutex.h>
70ed6d949aSAndrew Thompson #include <sys/condvar.h>
71ed6d949aSAndrew Thompson #include <sys/sysctl.h>
72ed6d949aSAndrew Thompson #include <sys/sx.h>
73ed6d949aSAndrew Thompson #include <sys/unistd.h>
74ed6d949aSAndrew Thompson #include <sys/callout.h>
75ed6d949aSAndrew Thompson #include <sys/malloc.h>
76ed6d949aSAndrew Thompson #include <sys/priv.h>
77ed6d949aSAndrew Thompson 
7802ac6454SAndrew Thompson #include <dev/usb/usb.h>
79ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
80ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
81ed6d949aSAndrew Thompson #include "usbdevs.h"
8202ac6454SAndrew Thompson 
8302ac6454SAndrew Thompson #define	USB_DEBUG_VAR ubsa_debug
8402ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
8502ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
8602ac6454SAndrew Thompson 
8702ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
8802ac6454SAndrew Thompson 
89b850ecc1SAndrew Thompson #ifdef USB_DEBUG
9002ac6454SAndrew Thompson static int ubsa_debug = 0;
9102ac6454SAndrew Thompson 
92f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, ubsa, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
93f8d2b1f3SPawel Biernacki     "USB ubsa");
94ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_ubsa, OID_AUTO, debug, CTLFLAG_RWTUN,
9502ac6454SAndrew Thompson     &ubsa_debug, 0, "ubsa debug level");
9602ac6454SAndrew Thompson #endif
9702ac6454SAndrew Thompson 
9802ac6454SAndrew Thompson #define	UBSA_BSIZE             1024	/* bytes */
9902ac6454SAndrew Thompson 
10002ac6454SAndrew Thompson #define	UBSA_CONFIG_INDEX	0
10102ac6454SAndrew Thompson #define	UBSA_IFACE_INDEX	0
10202ac6454SAndrew Thompson 
10302ac6454SAndrew Thompson #define	UBSA_REG_BAUDRATE	0x00
10402ac6454SAndrew Thompson #define	UBSA_REG_STOP_BITS	0x01
10502ac6454SAndrew Thompson #define	UBSA_REG_DATA_BITS	0x02
10602ac6454SAndrew Thompson #define	UBSA_REG_PARITY		0x03
10702ac6454SAndrew Thompson #define	UBSA_REG_DTR		0x0A
10802ac6454SAndrew Thompson #define	UBSA_REG_RTS		0x0B
10902ac6454SAndrew Thompson #define	UBSA_REG_BREAK		0x0C
11002ac6454SAndrew Thompson #define	UBSA_REG_FLOW_CTRL	0x10
11102ac6454SAndrew Thompson 
11202ac6454SAndrew Thompson #define	UBSA_PARITY_NONE	0x00
11302ac6454SAndrew Thompson #define	UBSA_PARITY_EVEN	0x01
11402ac6454SAndrew Thompson #define	UBSA_PARITY_ODD		0x02
11502ac6454SAndrew Thompson #define	UBSA_PARITY_MARK	0x03
11602ac6454SAndrew Thompson #define	UBSA_PARITY_SPACE	0x04
11702ac6454SAndrew Thompson 
11802ac6454SAndrew Thompson #define	UBSA_FLOW_NONE		0x0000
11902ac6454SAndrew Thompson #define	UBSA_FLOW_OCTS		0x0001
12002ac6454SAndrew Thompson #define	UBSA_FLOW_ODSR		0x0002
12102ac6454SAndrew Thompson #define	UBSA_FLOW_IDSR		0x0004
12202ac6454SAndrew Thompson #define	UBSA_FLOW_IDTR		0x0008
12302ac6454SAndrew Thompson #define	UBSA_FLOW_IRTS		0x0010
12402ac6454SAndrew Thompson #define	UBSA_FLOW_ORTS		0x0020
12502ac6454SAndrew Thompson #define	UBSA_FLOW_UNKNOWN	0x0040
12602ac6454SAndrew Thompson #define	UBSA_FLOW_OXON		0x0080
12702ac6454SAndrew Thompson #define	UBSA_FLOW_IXON		0x0100
12802ac6454SAndrew Thompson 
12902ac6454SAndrew Thompson /* line status register */
13002ac6454SAndrew Thompson #define	UBSA_LSR_TSRE		0x40	/* Transmitter empty: byte sent */
13102ac6454SAndrew Thompson #define	UBSA_LSR_TXRDY		0x20	/* Transmitter buffer empty */
13202ac6454SAndrew Thompson #define	UBSA_LSR_BI		0x10	/* Break detected */
13302ac6454SAndrew Thompson #define	UBSA_LSR_FE		0x08	/* Framing error: bad stop bit */
13402ac6454SAndrew Thompson #define	UBSA_LSR_PE		0x04	/* Parity error */
13502ac6454SAndrew Thompson #define	UBSA_LSR_OE		0x02	/* Overrun, lost incoming byte */
13602ac6454SAndrew Thompson #define	UBSA_LSR_RXRDY		0x01	/* Byte ready in Receive Buffer */
13702ac6454SAndrew Thompson #define	UBSA_LSR_RCV_MASK	0x1f	/* Mask for incoming data or error */
13802ac6454SAndrew Thompson 
13902ac6454SAndrew Thompson /* modem status register */
14002ac6454SAndrew Thompson /* All deltas are from the last read of the MSR. */
14102ac6454SAndrew Thompson #define	UBSA_MSR_DCD		0x80	/* Current Data Carrier Detect */
14202ac6454SAndrew Thompson #define	UBSA_MSR_RI		0x40	/* Current Ring Indicator */
14302ac6454SAndrew Thompson #define	UBSA_MSR_DSR		0x20	/* Current Data Set Ready */
14402ac6454SAndrew Thompson #define	UBSA_MSR_CTS		0x10	/* Current Clear to Send */
14502ac6454SAndrew Thompson #define	UBSA_MSR_DDCD		0x08	/* DCD has changed state */
14602ac6454SAndrew Thompson #define	UBSA_MSR_TERI		0x04	/* RI has toggled low to high */
14702ac6454SAndrew Thompson #define	UBSA_MSR_DDSR		0x02	/* DSR has changed state */
14802ac6454SAndrew Thompson #define	UBSA_MSR_DCTS		0x01	/* CTS has changed state */
14902ac6454SAndrew Thompson 
15002ac6454SAndrew Thompson enum {
15102ac6454SAndrew Thompson 	UBSA_BULK_DT_WR,
15202ac6454SAndrew Thompson 	UBSA_BULK_DT_RD,
15302ac6454SAndrew Thompson 	UBSA_INTR_DT_RD,
15402ac6454SAndrew Thompson 	UBSA_N_TRANSFER,
15502ac6454SAndrew Thompson };
15602ac6454SAndrew Thompson 
15702ac6454SAndrew Thompson struct ubsa_softc {
158760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
159760bc48eSAndrew Thompson 	struct ucom_softc sc_ucom;
16002ac6454SAndrew Thompson 
161760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UBSA_N_TRANSFER];
162760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
163deefe583SAndrew Thompson 	struct mtx sc_mtx;
16402ac6454SAndrew Thompson 
16502ac6454SAndrew Thompson 	uint8_t	sc_iface_no;		/* interface number */
16602ac6454SAndrew Thompson 	uint8_t	sc_iface_index;		/* interface index */
16702ac6454SAndrew Thompson 	uint8_t	sc_lsr;			/* local status register */
16802ac6454SAndrew Thompson 	uint8_t	sc_msr;			/* UBSA status register */
16902ac6454SAndrew Thompson };
17002ac6454SAndrew Thompson 
17102ac6454SAndrew Thompson static device_probe_t ubsa_probe;
17202ac6454SAndrew Thompson static device_attach_t ubsa_attach;
17302ac6454SAndrew Thompson static device_detach_t ubsa_detach;
174c01fc06eSHans Petter Selasky static void ubsa_free_softc(struct ubsa_softc *);
17502ac6454SAndrew Thompson 
176e0a69b51SAndrew Thompson static usb_callback_t ubsa_write_callback;
177e0a69b51SAndrew Thompson static usb_callback_t ubsa_read_callback;
178e0a69b51SAndrew Thompson static usb_callback_t ubsa_intr_callback;
17902ac6454SAndrew Thompson 
18002ac6454SAndrew Thompson static void	ubsa_cfg_request(struct ubsa_softc *, uint8_t, uint16_t);
1815805d178SHans Petter Selasky static void	ubsa_free(struct ucom_softc *);
182760bc48eSAndrew Thompson static void	ubsa_cfg_set_dtr(struct ucom_softc *, uint8_t);
183760bc48eSAndrew Thompson static void	ubsa_cfg_set_rts(struct ucom_softc *, uint8_t);
184760bc48eSAndrew Thompson static void	ubsa_cfg_set_break(struct ucom_softc *, uint8_t);
185760bc48eSAndrew Thompson static int	ubsa_pre_param(struct ucom_softc *, struct termios *);
186760bc48eSAndrew Thompson static void	ubsa_cfg_param(struct ucom_softc *, struct termios *);
187760bc48eSAndrew Thompson static void	ubsa_start_read(struct ucom_softc *);
188760bc48eSAndrew Thompson static void	ubsa_stop_read(struct ucom_softc *);
189760bc48eSAndrew Thompson static void	ubsa_start_write(struct ucom_softc *);
190760bc48eSAndrew Thompson static void	ubsa_stop_write(struct ucom_softc *);
191760bc48eSAndrew Thompson static void	ubsa_cfg_get_status(struct ucom_softc *, uint8_t *,
19202ac6454SAndrew Thompson 		    uint8_t *);
193655dc9d0SAndrew Thompson static void	ubsa_poll(struct ucom_softc *ucom);
19402ac6454SAndrew Thompson 
195760bc48eSAndrew Thompson static const struct usb_config ubsa_config[UBSA_N_TRANSFER] = {
19602ac6454SAndrew Thompson 	[UBSA_BULK_DT_WR] = {
19702ac6454SAndrew Thompson 		.type = UE_BULK,
19802ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
19902ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
2004eae601eSAndrew Thompson 		.bufsize = UBSA_BSIZE,	/* bytes */
2014eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2024eae601eSAndrew Thompson 		.callback = &ubsa_write_callback,
20302ac6454SAndrew Thompson 	},
20402ac6454SAndrew Thompson 
20502ac6454SAndrew Thompson 	[UBSA_BULK_DT_RD] = {
20602ac6454SAndrew Thompson 		.type = UE_BULK,
20702ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
20802ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2094eae601eSAndrew Thompson 		.bufsize = UBSA_BSIZE,	/* bytes */
2104eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2114eae601eSAndrew Thompson 		.callback = &ubsa_read_callback,
21202ac6454SAndrew Thompson 	},
21302ac6454SAndrew Thompson 
21402ac6454SAndrew Thompson 	[UBSA_INTR_DT_RD] = {
21502ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
21602ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
21702ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2184eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2194eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
2204eae601eSAndrew Thompson 		.callback = &ubsa_intr_callback,
22102ac6454SAndrew Thompson 	},
22202ac6454SAndrew Thompson };
22302ac6454SAndrew Thompson 
224760bc48eSAndrew Thompson static const struct ucom_callback ubsa_callback = {
225a593f6b8SAndrew Thompson 	.ucom_cfg_get_status = &ubsa_cfg_get_status,
226a593f6b8SAndrew Thompson 	.ucom_cfg_set_dtr = &ubsa_cfg_set_dtr,
227a593f6b8SAndrew Thompson 	.ucom_cfg_set_rts = &ubsa_cfg_set_rts,
228a593f6b8SAndrew Thompson 	.ucom_cfg_set_break = &ubsa_cfg_set_break,
229a593f6b8SAndrew Thompson 	.ucom_cfg_param = &ubsa_cfg_param,
230a593f6b8SAndrew Thompson 	.ucom_pre_param = &ubsa_pre_param,
231a593f6b8SAndrew Thompson 	.ucom_start_read = &ubsa_start_read,
232a593f6b8SAndrew Thompson 	.ucom_stop_read = &ubsa_stop_read,
233a593f6b8SAndrew Thompson 	.ucom_start_write = &ubsa_start_write,
234a593f6b8SAndrew Thompson 	.ucom_stop_write = &ubsa_stop_write,
235655dc9d0SAndrew Thompson 	.ucom_poll = &ubsa_poll,
2365805d178SHans Petter Selasky 	.ucom_free = &ubsa_free,
23702ac6454SAndrew Thompson };
23802ac6454SAndrew Thompson 
239f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID ubsa_devs[] = {
24002ac6454SAndrew Thompson 	/* AnyData ADU-500A */
24102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_500A, 0)},
24202ac6454SAndrew Thompson 	/* AnyData ADU-E100A/H */
24302ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_E100X, 0)},
24402ac6454SAndrew Thompson 	/* Axesstel MV100H */
24502ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_AXESSTEL, USB_PRODUCT_AXESSTEL_DATAMODEM, 0)},
24602ac6454SAndrew Thompson 	/* BELKIN F5U103 */
24702ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103, 0)},
24802ac6454SAndrew Thompson 	/* BELKIN F5U120 */
24902ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120, 0)},
25002ac6454SAndrew Thompson 	/* GoHubs GO-COM232 */
25102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM, 0)},
25202ac6454SAndrew Thompson 	/* GoHubs GO-COM232 */
25302ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232, 0)},
25402ac6454SAndrew Thompson 	/* Peracom */
25502ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1, 0)},
25602ac6454SAndrew Thompson };
25702ac6454SAndrew Thompson 
25802ac6454SAndrew Thompson static device_method_t ubsa_methods[] = {
25902ac6454SAndrew Thompson 	DEVMETHOD(device_probe, ubsa_probe),
26002ac6454SAndrew Thompson 	DEVMETHOD(device_attach, ubsa_attach),
26102ac6454SAndrew Thompson 	DEVMETHOD(device_detach, ubsa_detach),
2625805d178SHans Petter Selasky 	DEVMETHOD_END
26302ac6454SAndrew Thompson };
26402ac6454SAndrew Thompson 
26502ac6454SAndrew Thompson static driver_t ubsa_driver = {
26602ac6454SAndrew Thompson 	.name = "ubsa",
26702ac6454SAndrew Thompson 	.methods = ubsa_methods,
26802ac6454SAndrew Thompson 	.size = sizeof(struct ubsa_softc),
26902ac6454SAndrew Thompson };
27002ac6454SAndrew Thompson 
271bc9372d7SJohn Baldwin DRIVER_MODULE(ubsa, uhub, ubsa_driver, NULL, NULL);
27202ac6454SAndrew Thompson MODULE_DEPEND(ubsa, ucom, 1, 1, 1);
27302ac6454SAndrew Thompson MODULE_DEPEND(ubsa, usb, 1, 1, 1);
274910cb8feSAndrew Thompson MODULE_VERSION(ubsa, 1);
275f809f280SWarner Losh USB_PNP_HOST_INFO(ubsa_devs);
27602ac6454SAndrew Thompson 
27702ac6454SAndrew Thompson static int
ubsa_probe(device_t dev)27802ac6454SAndrew Thompson ubsa_probe(device_t dev)
27902ac6454SAndrew Thompson {
280760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
28102ac6454SAndrew Thompson 
282f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
28302ac6454SAndrew Thompson 		return (ENXIO);
28402ac6454SAndrew Thompson 	}
28502ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != UBSA_CONFIG_INDEX) {
28602ac6454SAndrew Thompson 		return (ENXIO);
28702ac6454SAndrew Thompson 	}
28802ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != UBSA_IFACE_INDEX) {
28902ac6454SAndrew Thompson 		return (ENXIO);
29002ac6454SAndrew Thompson 	}
291a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(ubsa_devs, sizeof(ubsa_devs), uaa));
29202ac6454SAndrew Thompson }
29302ac6454SAndrew Thompson 
29402ac6454SAndrew Thompson static int
ubsa_attach(device_t dev)29502ac6454SAndrew Thompson ubsa_attach(device_t dev)
29602ac6454SAndrew Thompson {
297760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
29802ac6454SAndrew Thompson 	struct ubsa_softc *sc = device_get_softc(dev);
29902ac6454SAndrew Thompson 	int error;
30002ac6454SAndrew Thompson 
30102ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
30202ac6454SAndrew Thompson 
303a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
304deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "ubsa", NULL, MTX_DEF);
3055805d178SHans Petter Selasky 	ucom_ref(&sc->sc_super_ucom);
30602ac6454SAndrew Thompson 
30702ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
30802ac6454SAndrew Thompson 	sc->sc_iface_no = uaa->info.bIfaceNum;
30902ac6454SAndrew Thompson 	sc->sc_iface_index = UBSA_IFACE_INDEX;
31002ac6454SAndrew Thompson 
311a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
312deefe583SAndrew Thompson 	    sc->sc_xfer, ubsa_config, UBSA_N_TRANSFER, sc, &sc->sc_mtx);
31302ac6454SAndrew Thompson 
31402ac6454SAndrew Thompson 	if (error) {
31502ac6454SAndrew Thompson 		DPRINTF("could not allocate all pipes\n");
31602ac6454SAndrew Thompson 		goto detach;
31702ac6454SAndrew Thompson 	}
31802ac6454SAndrew Thompson 	/* clear stall at first run */
319deefe583SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
320ec97e9caSHans Petter Selasky 	usbd_xfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_WR]);
321ec97e9caSHans Petter Selasky 	usbd_xfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_RD]);
322deefe583SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
32302ac6454SAndrew Thompson 
324a593f6b8SAndrew Thompson 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
325deefe583SAndrew Thompson 	    &ubsa_callback, &sc->sc_mtx);
32602ac6454SAndrew Thompson 	if (error) {
327a593f6b8SAndrew Thompson 		DPRINTF("ucom_attach failed\n");
32802ac6454SAndrew Thompson 		goto detach;
32902ac6454SAndrew Thompson 	}
3306416c259SNick Hibma 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
3316416c259SNick Hibma 
33202ac6454SAndrew Thompson 	return (0);
33302ac6454SAndrew Thompson 
33402ac6454SAndrew Thompson detach:
33502ac6454SAndrew Thompson 	ubsa_detach(dev);
33602ac6454SAndrew Thompson 	return (ENXIO);
33702ac6454SAndrew Thompson }
33802ac6454SAndrew Thompson 
33902ac6454SAndrew Thompson static int
ubsa_detach(device_t dev)34002ac6454SAndrew Thompson ubsa_detach(device_t dev)
34102ac6454SAndrew Thompson {
34202ac6454SAndrew Thompson 	struct ubsa_softc *sc = device_get_softc(dev);
34302ac6454SAndrew Thompson 
34402ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
34502ac6454SAndrew Thompson 
346015bb88fSNick Hibma 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
347a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UBSA_N_TRANSFER);
34802ac6454SAndrew Thompson 
349c01fc06eSHans Petter Selasky 	device_claim_softc(dev);
350c01fc06eSHans Petter Selasky 
351c01fc06eSHans Petter Selasky 	ubsa_free_softc(sc);
352c01fc06eSHans Petter Selasky 
35302ac6454SAndrew Thompson 	return (0);
35402ac6454SAndrew Thompson }
35502ac6454SAndrew Thompson 
3565805d178SHans Petter Selasky UCOM_UNLOAD_DRAIN(ubsa);
3575805d178SHans Petter Selasky 
3585805d178SHans Petter Selasky static void
ubsa_free_softc(struct ubsa_softc * sc)359c01fc06eSHans Petter Selasky ubsa_free_softc(struct ubsa_softc *sc)
3605805d178SHans Petter Selasky {
3615805d178SHans Petter Selasky 	if (ucom_unref(&sc->sc_super_ucom)) {
3625805d178SHans Petter Selasky 		mtx_destroy(&sc->sc_mtx);
363c01fc06eSHans Petter Selasky 		device_free_softc(sc);
3645805d178SHans Petter Selasky 	}
3655805d178SHans Petter Selasky }
3665805d178SHans Petter Selasky 
3675805d178SHans Petter Selasky static void
ubsa_free(struct ucom_softc * ucom)3685805d178SHans Petter Selasky ubsa_free(struct ucom_softc *ucom)
3695805d178SHans Petter Selasky {
370c01fc06eSHans Petter Selasky 	ubsa_free_softc(ucom->sc_parent);
3715805d178SHans Petter Selasky }
3725805d178SHans Petter Selasky 
37302ac6454SAndrew Thompson static void
ubsa_cfg_request(struct ubsa_softc * sc,uint8_t index,uint16_t value)37402ac6454SAndrew Thompson ubsa_cfg_request(struct ubsa_softc *sc, uint8_t index, uint16_t value)
37502ac6454SAndrew Thompson {
376760bc48eSAndrew Thompson 	struct usb_device_request req;
377e0a69b51SAndrew Thompson 	usb_error_t err;
37802ac6454SAndrew Thompson 
37902ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
38002ac6454SAndrew Thompson 	req.bRequest = index;
38102ac6454SAndrew Thompson 	USETW(req.wValue, value);
38202ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_iface_no;
38302ac6454SAndrew Thompson 	req.wIndex[1] = 0;
38402ac6454SAndrew Thompson 	USETW(req.wLength, 0);
38502ac6454SAndrew Thompson 
386a593f6b8SAndrew Thompson 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
38702ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
38802ac6454SAndrew Thompson 	if (err) {
38902ac6454SAndrew Thompson 		DPRINTFN(0, "device request failed, err=%s "
390a593f6b8SAndrew Thompson 		    "(ignored)\n", usbd_errstr(err));
39102ac6454SAndrew Thompson 	}
39202ac6454SAndrew Thompson }
39302ac6454SAndrew Thompson 
39402ac6454SAndrew Thompson static void
ubsa_cfg_set_dtr(struct ucom_softc * ucom,uint8_t onoff)395760bc48eSAndrew Thompson ubsa_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
39602ac6454SAndrew Thompson {
39702ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
39802ac6454SAndrew Thompson 
39902ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
40002ac6454SAndrew Thompson 
40102ac6454SAndrew Thompson 	ubsa_cfg_request(sc, UBSA_REG_DTR, onoff ? 1 : 0);
40202ac6454SAndrew Thompson }
40302ac6454SAndrew Thompson 
40402ac6454SAndrew Thompson static void
ubsa_cfg_set_rts(struct ucom_softc * ucom,uint8_t onoff)405760bc48eSAndrew Thompson ubsa_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
40602ac6454SAndrew Thompson {
40702ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
40802ac6454SAndrew Thompson 
40902ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
41002ac6454SAndrew Thompson 
41102ac6454SAndrew Thompson 	ubsa_cfg_request(sc, UBSA_REG_RTS, onoff ? 1 : 0);
41202ac6454SAndrew Thompson }
41302ac6454SAndrew Thompson 
41402ac6454SAndrew Thompson static void
ubsa_cfg_set_break(struct ucom_softc * ucom,uint8_t onoff)415760bc48eSAndrew Thompson ubsa_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
41602ac6454SAndrew Thompson {
41702ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
41802ac6454SAndrew Thompson 
41902ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
42002ac6454SAndrew Thompson 
42102ac6454SAndrew Thompson 	ubsa_cfg_request(sc, UBSA_REG_BREAK, onoff ? 1 : 0);
42202ac6454SAndrew Thompson }
42302ac6454SAndrew Thompson 
42402ac6454SAndrew Thompson static int
ubsa_pre_param(struct ucom_softc * ucom,struct termios * t)425760bc48eSAndrew Thompson ubsa_pre_param(struct ucom_softc *ucom, struct termios *t)
42602ac6454SAndrew Thompson {
42702ac6454SAndrew Thompson 
428b850ecc1SAndrew Thompson 	DPRINTF("sc = %p\n", ucom->sc_parent);
42902ac6454SAndrew Thompson 
43002ac6454SAndrew Thompson 	switch (t->c_ospeed) {
43102ac6454SAndrew Thompson 	case B0:
43202ac6454SAndrew Thompson 	case B300:
43302ac6454SAndrew Thompson 	case B600:
43402ac6454SAndrew Thompson 	case B1200:
43502ac6454SAndrew Thompson 	case B2400:
43602ac6454SAndrew Thompson 	case B4800:
43702ac6454SAndrew Thompson 	case B9600:
43802ac6454SAndrew Thompson 	case B19200:
43902ac6454SAndrew Thompson 	case B38400:
44002ac6454SAndrew Thompson 	case B57600:
44102ac6454SAndrew Thompson 	case B115200:
44202ac6454SAndrew Thompson 	case B230400:
44302ac6454SAndrew Thompson 		break;
44402ac6454SAndrew Thompson 	default:
44502ac6454SAndrew Thompson 		return (EINVAL);
44602ac6454SAndrew Thompson 	}
44702ac6454SAndrew Thompson 	return (0);
44802ac6454SAndrew Thompson }
44902ac6454SAndrew Thompson 
45002ac6454SAndrew Thompson static void
ubsa_cfg_param(struct ucom_softc * ucom,struct termios * t)451760bc48eSAndrew Thompson ubsa_cfg_param(struct ucom_softc *ucom, struct termios *t)
45202ac6454SAndrew Thompson {
45302ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
45402ac6454SAndrew Thompson 	uint16_t value = 0;
45502ac6454SAndrew Thompson 
45602ac6454SAndrew Thompson 	DPRINTF("sc = %p\n", sc);
45702ac6454SAndrew Thompson 
45802ac6454SAndrew Thompson 	switch (t->c_ospeed) {
45902ac6454SAndrew Thompson 	case B0:
46002ac6454SAndrew Thompson 		ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, 0);
46102ac6454SAndrew Thompson 		ubsa_cfg_set_dtr(&sc->sc_ucom, 0);
46202ac6454SAndrew Thompson 		ubsa_cfg_set_rts(&sc->sc_ucom, 0);
46302ac6454SAndrew Thompson 		break;
46402ac6454SAndrew Thompson 	case B300:
46502ac6454SAndrew Thompson 	case B600:
46602ac6454SAndrew Thompson 	case B1200:
46702ac6454SAndrew Thompson 	case B2400:
46802ac6454SAndrew Thompson 	case B4800:
46902ac6454SAndrew Thompson 	case B9600:
47002ac6454SAndrew Thompson 	case B19200:
47102ac6454SAndrew Thompson 	case B38400:
47202ac6454SAndrew Thompson 	case B57600:
47302ac6454SAndrew Thompson 	case B115200:
47402ac6454SAndrew Thompson 	case B230400:
47502ac6454SAndrew Thompson 		value = B230400 / t->c_ospeed;
47602ac6454SAndrew Thompson 		ubsa_cfg_request(sc, UBSA_REG_BAUDRATE, value);
47702ac6454SAndrew Thompson 		break;
47802ac6454SAndrew Thompson 	default:
47902ac6454SAndrew Thompson 		return;
48002ac6454SAndrew Thompson 	}
48102ac6454SAndrew Thompson 
48202ac6454SAndrew Thompson 	if (t->c_cflag & PARENB)
48302ac6454SAndrew Thompson 		value = (t->c_cflag & PARODD) ? UBSA_PARITY_ODD : UBSA_PARITY_EVEN;
48402ac6454SAndrew Thompson 	else
48502ac6454SAndrew Thompson 		value = UBSA_PARITY_NONE;
48602ac6454SAndrew Thompson 
48702ac6454SAndrew Thompson 	ubsa_cfg_request(sc, UBSA_REG_PARITY, value);
48802ac6454SAndrew Thompson 
48902ac6454SAndrew Thompson 	switch (t->c_cflag & CSIZE) {
49002ac6454SAndrew Thompson 	case CS5:
49102ac6454SAndrew Thompson 		value = 0;
49202ac6454SAndrew Thompson 		break;
49302ac6454SAndrew Thompson 	case CS6:
49402ac6454SAndrew Thompson 		value = 1;
49502ac6454SAndrew Thompson 		break;
49602ac6454SAndrew Thompson 	case CS7:
49702ac6454SAndrew Thompson 		value = 2;
49802ac6454SAndrew Thompson 		break;
49902ac6454SAndrew Thompson 	default:
50002ac6454SAndrew Thompson 	case CS8:
50102ac6454SAndrew Thompson 		value = 3;
50202ac6454SAndrew Thompson 		break;
50302ac6454SAndrew Thompson 	}
50402ac6454SAndrew Thompson 
50502ac6454SAndrew Thompson 	ubsa_cfg_request(sc, UBSA_REG_DATA_BITS, value);
50602ac6454SAndrew Thompson 
50702ac6454SAndrew Thompson 	value = (t->c_cflag & CSTOPB) ? 1 : 0;
50802ac6454SAndrew Thompson 
50902ac6454SAndrew Thompson 	ubsa_cfg_request(sc, UBSA_REG_STOP_BITS, value);
51002ac6454SAndrew Thompson 
51102ac6454SAndrew Thompson 	value = 0;
51202ac6454SAndrew Thompson 	if (t->c_cflag & CRTSCTS)
51302ac6454SAndrew Thompson 		value |= UBSA_FLOW_OCTS | UBSA_FLOW_IRTS;
51402ac6454SAndrew Thompson 
51502ac6454SAndrew Thompson 	if (t->c_iflag & (IXON | IXOFF))
51602ac6454SAndrew Thompson 		value |= UBSA_FLOW_OXON | UBSA_FLOW_IXON;
51702ac6454SAndrew Thompson 
51802ac6454SAndrew Thompson 	ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, value);
51902ac6454SAndrew Thompson }
52002ac6454SAndrew Thompson 
52102ac6454SAndrew Thompson static void
ubsa_start_read(struct ucom_softc * ucom)522760bc48eSAndrew Thompson ubsa_start_read(struct ucom_softc *ucom)
52302ac6454SAndrew Thompson {
52402ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
52502ac6454SAndrew Thompson 
52602ac6454SAndrew Thompson 	/* start interrupt endpoint */
527a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UBSA_INTR_DT_RD]);
52802ac6454SAndrew Thompson 
52902ac6454SAndrew Thompson 	/* start read endpoint */
530a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UBSA_BULK_DT_RD]);
53102ac6454SAndrew Thompson }
53202ac6454SAndrew Thompson 
53302ac6454SAndrew Thompson static void
ubsa_stop_read(struct ucom_softc * ucom)534760bc48eSAndrew Thompson ubsa_stop_read(struct ucom_softc *ucom)
53502ac6454SAndrew Thompson {
53602ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
53702ac6454SAndrew Thompson 
53802ac6454SAndrew Thompson 	/* stop interrupt endpoint */
539a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UBSA_INTR_DT_RD]);
54002ac6454SAndrew Thompson 
54102ac6454SAndrew Thompson 	/* stop read endpoint */
542a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_RD]);
54302ac6454SAndrew Thompson }
54402ac6454SAndrew Thompson 
54502ac6454SAndrew Thompson static void
ubsa_start_write(struct ucom_softc * ucom)546760bc48eSAndrew Thompson ubsa_start_write(struct ucom_softc *ucom)
54702ac6454SAndrew Thompson {
54802ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
54902ac6454SAndrew Thompson 
550a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UBSA_BULK_DT_WR]);
55102ac6454SAndrew Thompson }
55202ac6454SAndrew Thompson 
55302ac6454SAndrew Thompson static void
ubsa_stop_write(struct ucom_softc * ucom)554760bc48eSAndrew Thompson ubsa_stop_write(struct ucom_softc *ucom)
55502ac6454SAndrew Thompson {
55602ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
55702ac6454SAndrew Thompson 
558a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_WR]);
55902ac6454SAndrew Thompson }
56002ac6454SAndrew Thompson 
56102ac6454SAndrew Thompson static void
ubsa_cfg_get_status(struct ucom_softc * ucom,uint8_t * lsr,uint8_t * msr)562760bc48eSAndrew Thompson ubsa_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
56302ac6454SAndrew Thompson {
56402ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
56502ac6454SAndrew Thompson 
56602ac6454SAndrew Thompson 	DPRINTF("\n");
56702ac6454SAndrew Thompson 
56802ac6454SAndrew Thompson 	*lsr = sc->sc_lsr;
56902ac6454SAndrew Thompson 	*msr = sc->sc_msr;
57002ac6454SAndrew Thompson }
57102ac6454SAndrew Thompson 
57202ac6454SAndrew Thompson static void
ubsa_write_callback(struct usb_xfer * xfer,usb_error_t error)573ed6d949aSAndrew Thompson ubsa_write_callback(struct usb_xfer *xfer, usb_error_t error)
57402ac6454SAndrew Thompson {
575ed6d949aSAndrew Thompson 	struct ubsa_softc *sc = usbd_xfer_softc(xfer);
576ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
57702ac6454SAndrew Thompson 	uint32_t actlen;
57802ac6454SAndrew Thompson 
57902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
58002ac6454SAndrew Thompson 	case USB_ST_SETUP:
58102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
58202ac6454SAndrew Thompson tr_setup:
583ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
584ed6d949aSAndrew Thompson 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
58502ac6454SAndrew Thompson 		    UBSA_BSIZE, &actlen)) {
586ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, actlen);
587a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
58802ac6454SAndrew Thompson 		}
589ec97e9caSHans Petter Selasky 		return;
59002ac6454SAndrew Thompson 
59102ac6454SAndrew Thompson 	default:			/* Error */
592ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
59302ac6454SAndrew Thompson 			/* try to clear stall first */
594ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
59502ac6454SAndrew Thompson 			goto tr_setup;
59602ac6454SAndrew Thompson 		}
597ec97e9caSHans Petter Selasky 		return;
59802ac6454SAndrew Thompson 	}
59902ac6454SAndrew Thompson }
60002ac6454SAndrew Thompson 
60102ac6454SAndrew Thompson static void
ubsa_read_callback(struct usb_xfer * xfer,usb_error_t error)602ed6d949aSAndrew Thompson ubsa_read_callback(struct usb_xfer *xfer, usb_error_t error)
60302ac6454SAndrew Thompson {
604ed6d949aSAndrew Thompson 	struct ubsa_softc *sc = usbd_xfer_softc(xfer);
605ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
606ed6d949aSAndrew Thompson 	int actlen;
607ed6d949aSAndrew Thompson 
608ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
60902ac6454SAndrew Thompson 
61002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
61102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
612ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
613ed6d949aSAndrew Thompson 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
61402ac6454SAndrew Thompson 
61502ac6454SAndrew Thompson 	case USB_ST_SETUP:
61602ac6454SAndrew Thompson tr_setup:
617ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
618a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
61902ac6454SAndrew Thompson 		return;
62002ac6454SAndrew Thompson 
62102ac6454SAndrew Thompson 	default:			/* Error */
622ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
62302ac6454SAndrew Thompson 			/* try to clear stall first */
624ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
62502ac6454SAndrew Thompson 			goto tr_setup;
62602ac6454SAndrew Thompson 		}
62702ac6454SAndrew Thompson 		return;
62802ac6454SAndrew Thompson 	}
62902ac6454SAndrew Thompson }
63002ac6454SAndrew Thompson 
63102ac6454SAndrew Thompson static void
ubsa_intr_callback(struct usb_xfer * xfer,usb_error_t error)632ed6d949aSAndrew Thompson ubsa_intr_callback(struct usb_xfer *xfer, usb_error_t error)
63302ac6454SAndrew Thompson {
634ed6d949aSAndrew Thompson 	struct ubsa_softc *sc = usbd_xfer_softc(xfer);
635ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
63602ac6454SAndrew Thompson 	uint8_t buf[4];
637ed6d949aSAndrew Thompson 	int actlen;
638ed6d949aSAndrew Thompson 
639ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
64002ac6454SAndrew Thompson 
64102ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
64202ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
64302ac6454SAndrew Thompson 
6446d917491SHans Petter Selasky 		if (actlen >= (int)sizeof(buf)) {
645ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
646ed6d949aSAndrew Thompson 			usbd_copy_out(pc, 0, buf, sizeof(buf));
64702ac6454SAndrew Thompson 
64802ac6454SAndrew Thompson 			/*
649d8343329SIan Lepore 			 * MSR bits need translation from ns16550 to SER_* values.
650d8343329SIan Lepore 			 * LSR bits are ns16550 in hardware and ucom.
65102ac6454SAndrew Thompson 			 */
652d8343329SIan Lepore 			sc->sc_msr = 0;
653d8343329SIan Lepore 			if (buf[3] & UBSA_MSR_CTS)
654d8343329SIan Lepore 				sc->sc_msr |= SER_CTS;
655d8343329SIan Lepore 			if (buf[3] & UBSA_MSR_DCD)
656d8343329SIan Lepore 				sc->sc_msr |= SER_DCD;
657d8343329SIan Lepore 			if (buf[3] & UBSA_MSR_RI)
658d8343329SIan Lepore 				sc->sc_msr |= SER_RI;
659d8343329SIan Lepore 			if (buf[3] & UBSA_MSR_DSR)
660d8343329SIan Lepore 				sc->sc_msr |= SER_DSR;
66102ac6454SAndrew Thompson 			sc->sc_lsr = buf[2];
66202ac6454SAndrew Thompson 
66302ac6454SAndrew Thompson 			DPRINTF("lsr = 0x%02x, msr = 0x%02x\n",
66402ac6454SAndrew Thompson 			    sc->sc_lsr, sc->sc_msr);
66502ac6454SAndrew Thompson 
666a593f6b8SAndrew Thompson 			ucom_status_change(&sc->sc_ucom);
66702ac6454SAndrew Thompson 		} else {
668ed6d949aSAndrew Thompson 			DPRINTF("ignoring short packet, %d bytes\n", actlen);
66902ac6454SAndrew Thompson 		}
670d8343329SIan Lepore 		/* FALLTHROUGH */
67102ac6454SAndrew Thompson 	case USB_ST_SETUP:
67202ac6454SAndrew Thompson tr_setup:
673ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
674a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
67502ac6454SAndrew Thompson 		return;
67602ac6454SAndrew Thompson 
67702ac6454SAndrew Thompson 	default:			/* Error */
678ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
67902ac6454SAndrew Thompson 			/* try to clear stall first */
680ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
68102ac6454SAndrew Thompson 			goto tr_setup;
68202ac6454SAndrew Thompson 		}
68302ac6454SAndrew Thompson 		return;
68402ac6454SAndrew Thompson 	}
68502ac6454SAndrew Thompson }
686655dc9d0SAndrew Thompson 
687655dc9d0SAndrew Thompson static void
ubsa_poll(struct ucom_softc * ucom)688655dc9d0SAndrew Thompson ubsa_poll(struct ucom_softc *ucom)
689655dc9d0SAndrew Thompson {
690655dc9d0SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
691655dc9d0SAndrew Thompson 	usbd_transfer_poll(sc->sc_xfer, UBSA_N_TRANSFER);
692655dc9d0SAndrew Thompson 
693655dc9d0SAndrew Thompson }
694