xref: /freebsd/sys/dev/usb/serial/ubsa.c (revision a593f6b8de8b956bd0dd22f74805ede942e3d6a9)
102ac6454SAndrew Thompson /*-
202ac6454SAndrew Thompson  * Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>.
302ac6454SAndrew Thompson  * All rights reserved.
402ac6454SAndrew Thompson  *
502ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
602ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
702ac6454SAndrew Thompson  * are met:
802ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
902ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1002ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1202ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1302ac6454SAndrew Thompson  *
1402ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1502ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1602ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1702ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1802ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1902ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2002ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2102ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2202ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2302ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2402ac6454SAndrew Thompson  * SUCH DAMAGE.
2502ac6454SAndrew Thompson  */
2602ac6454SAndrew Thompson 
2702ac6454SAndrew Thompson #include <sys/cdefs.h>
2802ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
2902ac6454SAndrew Thompson /*-
3002ac6454SAndrew Thompson  * Copyright (c) 2001 The NetBSD Foundation, Inc.
3102ac6454SAndrew Thompson  * All rights reserved.
3202ac6454SAndrew Thompson  *
3302ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
3402ac6454SAndrew Thompson  * by Ichiro FUKUHARA (ichiro@ichiro.org).
3502ac6454SAndrew Thompson  *
3602ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
3702ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
3802ac6454SAndrew Thompson  * are met:
3902ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
4002ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
4102ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
4202ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
4302ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
4402ac6454SAndrew Thompson  * 3. All advertising materials mentioning features or use of this software
4502ac6454SAndrew Thompson  *    must display the following acknowledgement:
4602ac6454SAndrew Thompson  *        This product includes software developed by the NetBSD
4702ac6454SAndrew Thompson  *        Foundation, Inc. and its contributors.
4802ac6454SAndrew Thompson  * 4. Neither the name of The NetBSD Foundation nor the names of its
4902ac6454SAndrew Thompson  *    contributors may be used to endorse or promote products derived
5002ac6454SAndrew Thompson  *    from this software without specific prior written permission.
5102ac6454SAndrew Thompson  *
5202ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
5302ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
5402ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5502ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5602ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5702ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5802ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5902ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
6002ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6102ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
6202ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
6302ac6454SAndrew Thompson  */
6402ac6454SAndrew Thompson 
6502ac6454SAndrew Thompson #include "usbdevs.h"
6602ac6454SAndrew Thompson #include <dev/usb/usb.h>
6702ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
6802ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
6902ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
7002ac6454SAndrew Thompson 
7102ac6454SAndrew Thompson #define	USB_DEBUG_VAR ubsa_debug
7202ac6454SAndrew Thompson 
7302ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
7402ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
7502ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
7602ac6454SAndrew Thompson #include <dev/usb/usb_request.h>
7702ac6454SAndrew Thompson #include <dev/usb/usb_lookup.h>
7802ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
7902ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
8002ac6454SAndrew Thompson 
8102ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
8202ac6454SAndrew Thompson 
8302ac6454SAndrew Thompson #if USB_DEBUG
8402ac6454SAndrew Thompson static int ubsa_debug = 0;
8502ac6454SAndrew Thompson 
869360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, ubsa, CTLFLAG_RW, 0, "USB ubsa");
879360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_ubsa, OID_AUTO, debug, CTLFLAG_RW,
8802ac6454SAndrew Thompson     &ubsa_debug, 0, "ubsa debug level");
8902ac6454SAndrew Thompson #endif
9002ac6454SAndrew Thompson 
9102ac6454SAndrew Thompson #define	UBSA_BSIZE             1024	/* bytes */
9202ac6454SAndrew Thompson 
9302ac6454SAndrew Thompson #define	UBSA_CONFIG_INDEX	0
9402ac6454SAndrew Thompson #define	UBSA_IFACE_INDEX	0
9502ac6454SAndrew Thompson 
9602ac6454SAndrew Thompson #define	UBSA_REG_BAUDRATE	0x00
9702ac6454SAndrew Thompson #define	UBSA_REG_STOP_BITS	0x01
9802ac6454SAndrew Thompson #define	UBSA_REG_DATA_BITS	0x02
9902ac6454SAndrew Thompson #define	UBSA_REG_PARITY		0x03
10002ac6454SAndrew Thompson #define	UBSA_REG_DTR		0x0A
10102ac6454SAndrew Thompson #define	UBSA_REG_RTS		0x0B
10202ac6454SAndrew Thompson #define	UBSA_REG_BREAK		0x0C
10302ac6454SAndrew Thompson #define	UBSA_REG_FLOW_CTRL	0x10
10402ac6454SAndrew Thompson 
10502ac6454SAndrew Thompson #define	UBSA_PARITY_NONE	0x00
10602ac6454SAndrew Thompson #define	UBSA_PARITY_EVEN	0x01
10702ac6454SAndrew Thompson #define	UBSA_PARITY_ODD		0x02
10802ac6454SAndrew Thompson #define	UBSA_PARITY_MARK	0x03
10902ac6454SAndrew Thompson #define	UBSA_PARITY_SPACE	0x04
11002ac6454SAndrew Thompson 
11102ac6454SAndrew Thompson #define	UBSA_FLOW_NONE		0x0000
11202ac6454SAndrew Thompson #define	UBSA_FLOW_OCTS		0x0001
11302ac6454SAndrew Thompson #define	UBSA_FLOW_ODSR		0x0002
11402ac6454SAndrew Thompson #define	UBSA_FLOW_IDSR		0x0004
11502ac6454SAndrew Thompson #define	UBSA_FLOW_IDTR		0x0008
11602ac6454SAndrew Thompson #define	UBSA_FLOW_IRTS		0x0010
11702ac6454SAndrew Thompson #define	UBSA_FLOW_ORTS		0x0020
11802ac6454SAndrew Thompson #define	UBSA_FLOW_UNKNOWN	0x0040
11902ac6454SAndrew Thompson #define	UBSA_FLOW_OXON		0x0080
12002ac6454SAndrew Thompson #define	UBSA_FLOW_IXON		0x0100
12102ac6454SAndrew Thompson 
12202ac6454SAndrew Thompson /* line status register */
12302ac6454SAndrew Thompson #define	UBSA_LSR_TSRE		0x40	/* Transmitter empty: byte sent */
12402ac6454SAndrew Thompson #define	UBSA_LSR_TXRDY		0x20	/* Transmitter buffer empty */
12502ac6454SAndrew Thompson #define	UBSA_LSR_BI		0x10	/* Break detected */
12602ac6454SAndrew Thompson #define	UBSA_LSR_FE		0x08	/* Framing error: bad stop bit */
12702ac6454SAndrew Thompson #define	UBSA_LSR_PE		0x04	/* Parity error */
12802ac6454SAndrew Thompson #define	UBSA_LSR_OE		0x02	/* Overrun, lost incoming byte */
12902ac6454SAndrew Thompson #define	UBSA_LSR_RXRDY		0x01	/* Byte ready in Receive Buffer */
13002ac6454SAndrew Thompson #define	UBSA_LSR_RCV_MASK	0x1f	/* Mask for incoming data or error */
13102ac6454SAndrew Thompson 
13202ac6454SAndrew Thompson /* modem status register */
13302ac6454SAndrew Thompson /* All deltas are from the last read of the MSR. */
13402ac6454SAndrew Thompson #define	UBSA_MSR_DCD		0x80	/* Current Data Carrier Detect */
13502ac6454SAndrew Thompson #define	UBSA_MSR_RI		0x40	/* Current Ring Indicator */
13602ac6454SAndrew Thompson #define	UBSA_MSR_DSR		0x20	/* Current Data Set Ready */
13702ac6454SAndrew Thompson #define	UBSA_MSR_CTS		0x10	/* Current Clear to Send */
13802ac6454SAndrew Thompson #define	UBSA_MSR_DDCD		0x08	/* DCD has changed state */
13902ac6454SAndrew Thompson #define	UBSA_MSR_TERI		0x04	/* RI has toggled low to high */
14002ac6454SAndrew Thompson #define	UBSA_MSR_DDSR		0x02	/* DSR has changed state */
14102ac6454SAndrew Thompson #define	UBSA_MSR_DCTS		0x01	/* CTS has changed state */
14202ac6454SAndrew Thompson 
14302ac6454SAndrew Thompson enum {
14402ac6454SAndrew Thompson 	UBSA_BULK_DT_WR,
14502ac6454SAndrew Thompson 	UBSA_BULK_DT_RD,
14602ac6454SAndrew Thompson 	UBSA_INTR_DT_RD,
14702ac6454SAndrew Thompson 	UBSA_N_TRANSFER,
14802ac6454SAndrew Thompson };
14902ac6454SAndrew Thompson 
15002ac6454SAndrew Thompson struct ubsa_softc {
151760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
152760bc48eSAndrew Thompson 	struct ucom_softc sc_ucom;
15302ac6454SAndrew Thompson 
154760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UBSA_N_TRANSFER];
155760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
156deefe583SAndrew Thompson 	struct mtx sc_mtx;
15702ac6454SAndrew Thompson 
15802ac6454SAndrew Thompson 	uint8_t	sc_iface_no;		/* interface number */
15902ac6454SAndrew Thompson 	uint8_t	sc_iface_index;		/* interface index */
16002ac6454SAndrew Thompson 	uint8_t	sc_lsr;			/* local status register */
16102ac6454SAndrew Thompson 	uint8_t	sc_msr;			/* UBSA status register */
16202ac6454SAndrew Thompson };
16302ac6454SAndrew Thompson 
16402ac6454SAndrew Thompson static device_probe_t ubsa_probe;
16502ac6454SAndrew Thompson static device_attach_t ubsa_attach;
16602ac6454SAndrew Thompson static device_detach_t ubsa_detach;
16702ac6454SAndrew Thompson 
168e0a69b51SAndrew Thompson static usb_callback_t ubsa_write_callback;
169e0a69b51SAndrew Thompson static usb_callback_t ubsa_read_callback;
170e0a69b51SAndrew Thompson static usb_callback_t ubsa_intr_callback;
17102ac6454SAndrew Thompson 
17202ac6454SAndrew Thompson static void	ubsa_cfg_request(struct ubsa_softc *, uint8_t, uint16_t);
173760bc48eSAndrew Thompson static void	ubsa_cfg_set_dtr(struct ucom_softc *, uint8_t);
174760bc48eSAndrew Thompson static void	ubsa_cfg_set_rts(struct ucom_softc *, uint8_t);
175760bc48eSAndrew Thompson static void	ubsa_cfg_set_break(struct ucom_softc *, uint8_t);
176760bc48eSAndrew Thompson static int	ubsa_pre_param(struct ucom_softc *, struct termios *);
177760bc48eSAndrew Thompson static void	ubsa_cfg_param(struct ucom_softc *, struct termios *);
178760bc48eSAndrew Thompson static void	ubsa_start_read(struct ucom_softc *);
179760bc48eSAndrew Thompson static void	ubsa_stop_read(struct ucom_softc *);
180760bc48eSAndrew Thompson static void	ubsa_start_write(struct ucom_softc *);
181760bc48eSAndrew Thompson static void	ubsa_stop_write(struct ucom_softc *);
182760bc48eSAndrew Thompson static void	ubsa_cfg_get_status(struct ucom_softc *, uint8_t *,
18302ac6454SAndrew Thompson 		    uint8_t *);
18402ac6454SAndrew Thompson 
185760bc48eSAndrew Thompson static const struct usb_config ubsa_config[UBSA_N_TRANSFER] = {
18602ac6454SAndrew Thompson 
18702ac6454SAndrew Thompson 	[UBSA_BULK_DT_WR] = {
18802ac6454SAndrew Thompson 		.type = UE_BULK,
18902ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
19002ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
1914eae601eSAndrew Thompson 		.bufsize = UBSA_BSIZE,	/* bytes */
1924eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
1934eae601eSAndrew Thompson 		.callback = &ubsa_write_callback,
19402ac6454SAndrew Thompson 	},
19502ac6454SAndrew Thompson 
19602ac6454SAndrew Thompson 	[UBSA_BULK_DT_RD] = {
19702ac6454SAndrew Thompson 		.type = UE_BULK,
19802ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
19902ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2004eae601eSAndrew Thompson 		.bufsize = UBSA_BSIZE,	/* bytes */
2014eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2024eae601eSAndrew Thompson 		.callback = &ubsa_read_callback,
20302ac6454SAndrew Thompson 	},
20402ac6454SAndrew Thompson 
20502ac6454SAndrew Thompson 	[UBSA_INTR_DT_RD] = {
20602ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
20702ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
20802ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2094eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2104eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
2114eae601eSAndrew Thompson 		.callback = &ubsa_intr_callback,
21202ac6454SAndrew Thompson 	},
21302ac6454SAndrew Thompson };
21402ac6454SAndrew Thompson 
215760bc48eSAndrew Thompson static const struct ucom_callback ubsa_callback = {
216a593f6b8SAndrew Thompson 	.ucom_cfg_get_status = &ubsa_cfg_get_status,
217a593f6b8SAndrew Thompson 	.ucom_cfg_set_dtr = &ubsa_cfg_set_dtr,
218a593f6b8SAndrew Thompson 	.ucom_cfg_set_rts = &ubsa_cfg_set_rts,
219a593f6b8SAndrew Thompson 	.ucom_cfg_set_break = &ubsa_cfg_set_break,
220a593f6b8SAndrew Thompson 	.ucom_cfg_param = &ubsa_cfg_param,
221a593f6b8SAndrew Thompson 	.ucom_pre_param = &ubsa_pre_param,
222a593f6b8SAndrew Thompson 	.ucom_start_read = &ubsa_start_read,
223a593f6b8SAndrew Thompson 	.ucom_stop_read = &ubsa_stop_read,
224a593f6b8SAndrew Thompson 	.ucom_start_write = &ubsa_start_write,
225a593f6b8SAndrew Thompson 	.ucom_stop_write = &ubsa_stop_write,
22602ac6454SAndrew Thompson };
22702ac6454SAndrew Thompson 
228760bc48eSAndrew Thompson static const struct usb_device_id ubsa_devs[] = {
22902ac6454SAndrew Thompson 	/* AnyData ADU-500A */
23002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_500A, 0)},
23102ac6454SAndrew Thompson 	/* AnyData ADU-E100A/H */
23202ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_E100X, 0)},
23302ac6454SAndrew Thompson 	/* Axesstel MV100H */
23402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_AXESSTEL, USB_PRODUCT_AXESSTEL_DATAMODEM, 0)},
23502ac6454SAndrew Thompson 	/* BELKIN F5U103 */
23602ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103, 0)},
23702ac6454SAndrew Thompson 	/* BELKIN F5U120 */
23802ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120, 0)},
23902ac6454SAndrew Thompson 	/* GoHubs GO-COM232 */
24002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM, 0)},
24102ac6454SAndrew Thompson 	/* GoHubs GO-COM232 */
24202ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232, 0)},
24302ac6454SAndrew Thompson 	/* Peracom */
24402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1, 0)},
24502ac6454SAndrew Thompson };
24602ac6454SAndrew Thompson 
24702ac6454SAndrew Thompson static device_method_t ubsa_methods[] = {
24802ac6454SAndrew Thompson 	DEVMETHOD(device_probe, ubsa_probe),
24902ac6454SAndrew Thompson 	DEVMETHOD(device_attach, ubsa_attach),
25002ac6454SAndrew Thompson 	DEVMETHOD(device_detach, ubsa_detach),
25102ac6454SAndrew Thompson 	{0, 0}
25202ac6454SAndrew Thompson };
25302ac6454SAndrew Thompson 
25402ac6454SAndrew Thompson static devclass_t ubsa_devclass;
25502ac6454SAndrew Thompson 
25602ac6454SAndrew Thompson static driver_t ubsa_driver = {
25702ac6454SAndrew Thompson 	.name = "ubsa",
25802ac6454SAndrew Thompson 	.methods = ubsa_methods,
25902ac6454SAndrew Thompson 	.size = sizeof(struct ubsa_softc),
26002ac6454SAndrew Thompson };
26102ac6454SAndrew Thompson 
2629aef556dSAndrew Thompson DRIVER_MODULE(ubsa, uhub, ubsa_driver, ubsa_devclass, NULL, 0);
26302ac6454SAndrew Thompson MODULE_DEPEND(ubsa, ucom, 1, 1, 1);
26402ac6454SAndrew Thompson MODULE_DEPEND(ubsa, usb, 1, 1, 1);
26502ac6454SAndrew Thompson 
26602ac6454SAndrew Thompson static int
26702ac6454SAndrew Thompson ubsa_probe(device_t dev)
26802ac6454SAndrew Thompson {
269760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
27002ac6454SAndrew Thompson 
271f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
27202ac6454SAndrew Thompson 		return (ENXIO);
27302ac6454SAndrew Thompson 	}
27402ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != UBSA_CONFIG_INDEX) {
27502ac6454SAndrew Thompson 		return (ENXIO);
27602ac6454SAndrew Thompson 	}
27702ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != UBSA_IFACE_INDEX) {
27802ac6454SAndrew Thompson 		return (ENXIO);
27902ac6454SAndrew Thompson 	}
280a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(ubsa_devs, sizeof(ubsa_devs), uaa));
28102ac6454SAndrew Thompson }
28202ac6454SAndrew Thompson 
28302ac6454SAndrew Thompson static int
28402ac6454SAndrew Thompson ubsa_attach(device_t dev)
28502ac6454SAndrew Thompson {
286760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
28702ac6454SAndrew Thompson 	struct ubsa_softc *sc = device_get_softc(dev);
28802ac6454SAndrew Thompson 	int error;
28902ac6454SAndrew Thompson 
29002ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
29102ac6454SAndrew Thompson 
292a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
293deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "ubsa", NULL, MTX_DEF);
29402ac6454SAndrew Thompson 
29502ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
29602ac6454SAndrew Thompson 	sc->sc_iface_no = uaa->info.bIfaceNum;
29702ac6454SAndrew Thompson 	sc->sc_iface_index = UBSA_IFACE_INDEX;
29802ac6454SAndrew Thompson 
299a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
300deefe583SAndrew Thompson 	    sc->sc_xfer, ubsa_config, UBSA_N_TRANSFER, sc, &sc->sc_mtx);
30102ac6454SAndrew Thompson 
30202ac6454SAndrew Thompson 	if (error) {
30302ac6454SAndrew Thompson 		DPRINTF("could not allocate all pipes\n");
30402ac6454SAndrew Thompson 		goto detach;
30502ac6454SAndrew Thompson 	}
30602ac6454SAndrew Thompson 	/* clear stall at first run */
307deefe583SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
308a593f6b8SAndrew Thompson 	usbd_transfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_WR]);
309a593f6b8SAndrew Thompson 	usbd_transfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_RD]);
310deefe583SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
31102ac6454SAndrew Thompson 
312a593f6b8SAndrew Thompson 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
313deefe583SAndrew Thompson 	    &ubsa_callback, &sc->sc_mtx);
31402ac6454SAndrew Thompson 	if (error) {
315a593f6b8SAndrew Thompson 		DPRINTF("ucom_attach failed\n");
31602ac6454SAndrew Thompson 		goto detach;
31702ac6454SAndrew Thompson 	}
31802ac6454SAndrew Thompson 	return (0);
31902ac6454SAndrew Thompson 
32002ac6454SAndrew Thompson detach:
32102ac6454SAndrew Thompson 	ubsa_detach(dev);
32202ac6454SAndrew Thompson 	return (ENXIO);
32302ac6454SAndrew Thompson }
32402ac6454SAndrew Thompson 
32502ac6454SAndrew Thompson static int
32602ac6454SAndrew Thompson ubsa_detach(device_t dev)
32702ac6454SAndrew Thompson {
32802ac6454SAndrew Thompson 	struct ubsa_softc *sc = device_get_softc(dev);
32902ac6454SAndrew Thompson 
33002ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
33102ac6454SAndrew Thompson 
332a593f6b8SAndrew Thompson 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
333a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UBSA_N_TRANSFER);
334deefe583SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
33502ac6454SAndrew Thompson 
33602ac6454SAndrew Thompson 	return (0);
33702ac6454SAndrew Thompson }
33802ac6454SAndrew Thompson 
33902ac6454SAndrew Thompson static void
34002ac6454SAndrew Thompson ubsa_cfg_request(struct ubsa_softc *sc, uint8_t index, uint16_t value)
34102ac6454SAndrew Thompson {
342760bc48eSAndrew Thompson 	struct usb_device_request req;
343e0a69b51SAndrew Thompson 	usb_error_t err;
34402ac6454SAndrew Thompson 
34502ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
34602ac6454SAndrew Thompson 	req.bRequest = index;
34702ac6454SAndrew Thompson 	USETW(req.wValue, value);
34802ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_iface_no;
34902ac6454SAndrew Thompson 	req.wIndex[1] = 0;
35002ac6454SAndrew Thompson 	USETW(req.wLength, 0);
35102ac6454SAndrew Thompson 
352a593f6b8SAndrew Thompson 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
35302ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
35402ac6454SAndrew Thompson 	if (err) {
35502ac6454SAndrew Thompson 		DPRINTFN(0, "device request failed, err=%s "
356a593f6b8SAndrew Thompson 		    "(ignored)\n", usbd_errstr(err));
35702ac6454SAndrew Thompson 	}
35802ac6454SAndrew Thompson }
35902ac6454SAndrew Thompson 
36002ac6454SAndrew Thompson static void
361760bc48eSAndrew Thompson ubsa_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
36202ac6454SAndrew Thompson {
36302ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
36402ac6454SAndrew Thompson 
36502ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
36602ac6454SAndrew Thompson 
36702ac6454SAndrew Thompson 	ubsa_cfg_request(sc, UBSA_REG_DTR, onoff ? 1 : 0);
36802ac6454SAndrew Thompson }
36902ac6454SAndrew Thompson 
37002ac6454SAndrew Thompson static void
371760bc48eSAndrew Thompson ubsa_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
37202ac6454SAndrew Thompson {
37302ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
37402ac6454SAndrew Thompson 
37502ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
37602ac6454SAndrew Thompson 
37702ac6454SAndrew Thompson 	ubsa_cfg_request(sc, UBSA_REG_RTS, onoff ? 1 : 0);
37802ac6454SAndrew Thompson }
37902ac6454SAndrew Thompson 
38002ac6454SAndrew Thompson static void
381760bc48eSAndrew Thompson ubsa_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
38202ac6454SAndrew Thompson {
38302ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
38402ac6454SAndrew Thompson 
38502ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
38602ac6454SAndrew Thompson 
38702ac6454SAndrew Thompson 	ubsa_cfg_request(sc, UBSA_REG_BREAK, onoff ? 1 : 0);
38802ac6454SAndrew Thompson }
38902ac6454SAndrew Thompson 
39002ac6454SAndrew Thompson static int
391760bc48eSAndrew Thompson ubsa_pre_param(struct ucom_softc *ucom, struct termios *t)
39202ac6454SAndrew Thompson {
39302ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
39402ac6454SAndrew Thompson 
39502ac6454SAndrew Thompson 	DPRINTF("sc = %p\n", sc);
39602ac6454SAndrew Thompson 
39702ac6454SAndrew Thompson 	switch (t->c_ospeed) {
39802ac6454SAndrew Thompson 	case B0:
39902ac6454SAndrew Thompson 	case B300:
40002ac6454SAndrew Thompson 	case B600:
40102ac6454SAndrew Thompson 	case B1200:
40202ac6454SAndrew Thompson 	case B2400:
40302ac6454SAndrew Thompson 	case B4800:
40402ac6454SAndrew Thompson 	case B9600:
40502ac6454SAndrew Thompson 	case B19200:
40602ac6454SAndrew Thompson 	case B38400:
40702ac6454SAndrew Thompson 	case B57600:
40802ac6454SAndrew Thompson 	case B115200:
40902ac6454SAndrew Thompson 	case B230400:
41002ac6454SAndrew Thompson 		break;
41102ac6454SAndrew Thompson 	default:
41202ac6454SAndrew Thompson 		return (EINVAL);
41302ac6454SAndrew Thompson 	}
41402ac6454SAndrew Thompson 	return (0);
41502ac6454SAndrew Thompson }
41602ac6454SAndrew Thompson 
41702ac6454SAndrew Thompson static void
418760bc48eSAndrew Thompson ubsa_cfg_param(struct ucom_softc *ucom, struct termios *t)
41902ac6454SAndrew Thompson {
42002ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
42102ac6454SAndrew Thompson 	uint16_t value = 0;
42202ac6454SAndrew Thompson 
42302ac6454SAndrew Thompson 	DPRINTF("sc = %p\n", sc);
42402ac6454SAndrew Thompson 
42502ac6454SAndrew Thompson 	switch (t->c_ospeed) {
42602ac6454SAndrew Thompson 	case B0:
42702ac6454SAndrew Thompson 		ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, 0);
42802ac6454SAndrew Thompson 		ubsa_cfg_set_dtr(&sc->sc_ucom, 0);
42902ac6454SAndrew Thompson 		ubsa_cfg_set_rts(&sc->sc_ucom, 0);
43002ac6454SAndrew Thompson 		break;
43102ac6454SAndrew Thompson 	case B300:
43202ac6454SAndrew Thompson 	case B600:
43302ac6454SAndrew Thompson 	case B1200:
43402ac6454SAndrew Thompson 	case B2400:
43502ac6454SAndrew Thompson 	case B4800:
43602ac6454SAndrew Thompson 	case B9600:
43702ac6454SAndrew Thompson 	case B19200:
43802ac6454SAndrew Thompson 	case B38400:
43902ac6454SAndrew Thompson 	case B57600:
44002ac6454SAndrew Thompson 	case B115200:
44102ac6454SAndrew Thompson 	case B230400:
44202ac6454SAndrew Thompson 		value = B230400 / t->c_ospeed;
44302ac6454SAndrew Thompson 		ubsa_cfg_request(sc, UBSA_REG_BAUDRATE, value);
44402ac6454SAndrew Thompson 		break;
44502ac6454SAndrew Thompson 	default:
44602ac6454SAndrew Thompson 		return;
44702ac6454SAndrew Thompson 	}
44802ac6454SAndrew Thompson 
44902ac6454SAndrew Thompson 	if (t->c_cflag & PARENB)
45002ac6454SAndrew Thompson 		value = (t->c_cflag & PARODD) ? UBSA_PARITY_ODD : UBSA_PARITY_EVEN;
45102ac6454SAndrew Thompson 	else
45202ac6454SAndrew Thompson 		value = UBSA_PARITY_NONE;
45302ac6454SAndrew Thompson 
45402ac6454SAndrew Thompson 	ubsa_cfg_request(sc, UBSA_REG_PARITY, value);
45502ac6454SAndrew Thompson 
45602ac6454SAndrew Thompson 	switch (t->c_cflag & CSIZE) {
45702ac6454SAndrew Thompson 	case CS5:
45802ac6454SAndrew Thompson 		value = 0;
45902ac6454SAndrew Thompson 		break;
46002ac6454SAndrew Thompson 	case CS6:
46102ac6454SAndrew Thompson 		value = 1;
46202ac6454SAndrew Thompson 		break;
46302ac6454SAndrew Thompson 	case CS7:
46402ac6454SAndrew Thompson 		value = 2;
46502ac6454SAndrew Thompson 		break;
46602ac6454SAndrew Thompson 	default:
46702ac6454SAndrew Thompson 	case CS8:
46802ac6454SAndrew Thompson 		value = 3;
46902ac6454SAndrew Thompson 		break;
47002ac6454SAndrew Thompson 	}
47102ac6454SAndrew Thompson 
47202ac6454SAndrew Thompson 	ubsa_cfg_request(sc, UBSA_REG_DATA_BITS, value);
47302ac6454SAndrew Thompson 
47402ac6454SAndrew Thompson 	value = (t->c_cflag & CSTOPB) ? 1 : 0;
47502ac6454SAndrew Thompson 
47602ac6454SAndrew Thompson 	ubsa_cfg_request(sc, UBSA_REG_STOP_BITS, value);
47702ac6454SAndrew Thompson 
47802ac6454SAndrew Thompson 	value = 0;
47902ac6454SAndrew Thompson 	if (t->c_cflag & CRTSCTS)
48002ac6454SAndrew Thompson 		value |= UBSA_FLOW_OCTS | UBSA_FLOW_IRTS;
48102ac6454SAndrew Thompson 
48202ac6454SAndrew Thompson 	if (t->c_iflag & (IXON | IXOFF))
48302ac6454SAndrew Thompson 		value |= UBSA_FLOW_OXON | UBSA_FLOW_IXON;
48402ac6454SAndrew Thompson 
48502ac6454SAndrew Thompson 	ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, value);
48602ac6454SAndrew Thompson }
48702ac6454SAndrew Thompson 
48802ac6454SAndrew Thompson static void
489760bc48eSAndrew Thompson ubsa_start_read(struct ucom_softc *ucom)
49002ac6454SAndrew Thompson {
49102ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
49202ac6454SAndrew Thompson 
49302ac6454SAndrew Thompson 	/* start interrupt endpoint */
494a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UBSA_INTR_DT_RD]);
49502ac6454SAndrew Thompson 
49602ac6454SAndrew Thompson 	/* start read endpoint */
497a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UBSA_BULK_DT_RD]);
49802ac6454SAndrew Thompson }
49902ac6454SAndrew Thompson 
50002ac6454SAndrew Thompson static void
501760bc48eSAndrew Thompson ubsa_stop_read(struct ucom_softc *ucom)
50202ac6454SAndrew Thompson {
50302ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
50402ac6454SAndrew Thompson 
50502ac6454SAndrew Thompson 	/* stop interrupt endpoint */
506a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UBSA_INTR_DT_RD]);
50702ac6454SAndrew Thompson 
50802ac6454SAndrew Thompson 	/* stop read endpoint */
509a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_RD]);
51002ac6454SAndrew Thompson }
51102ac6454SAndrew Thompson 
51202ac6454SAndrew Thompson static void
513760bc48eSAndrew Thompson ubsa_start_write(struct ucom_softc *ucom)
51402ac6454SAndrew Thompson {
51502ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
51602ac6454SAndrew Thompson 
517a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UBSA_BULK_DT_WR]);
51802ac6454SAndrew Thompson }
51902ac6454SAndrew Thompson 
52002ac6454SAndrew Thompson static void
521760bc48eSAndrew Thompson ubsa_stop_write(struct ucom_softc *ucom)
52202ac6454SAndrew Thompson {
52302ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
52402ac6454SAndrew Thompson 
525a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_WR]);
52602ac6454SAndrew Thompson }
52702ac6454SAndrew Thompson 
52802ac6454SAndrew Thompson static void
529760bc48eSAndrew Thompson ubsa_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
53002ac6454SAndrew Thompson {
53102ac6454SAndrew Thompson 	struct ubsa_softc *sc = ucom->sc_parent;
53202ac6454SAndrew Thompson 
53302ac6454SAndrew Thompson 	DPRINTF("\n");
53402ac6454SAndrew Thompson 
53502ac6454SAndrew Thompson 	*lsr = sc->sc_lsr;
53602ac6454SAndrew Thompson 	*msr = sc->sc_msr;
53702ac6454SAndrew Thompson }
53802ac6454SAndrew Thompson 
53902ac6454SAndrew Thompson static void
540760bc48eSAndrew Thompson ubsa_write_callback(struct usb_xfer *xfer)
54102ac6454SAndrew Thompson {
54202ac6454SAndrew Thompson 	struct ubsa_softc *sc = xfer->priv_sc;
54302ac6454SAndrew Thompson 	uint32_t actlen;
54402ac6454SAndrew Thompson 
54502ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
54602ac6454SAndrew Thompson 	case USB_ST_SETUP:
54702ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
54802ac6454SAndrew Thompson tr_setup:
549a593f6b8SAndrew Thompson 		if (ucom_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
55002ac6454SAndrew Thompson 		    UBSA_BSIZE, &actlen)) {
55102ac6454SAndrew Thompson 
55202ac6454SAndrew Thompson 			xfer->frlengths[0] = actlen;
553a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
55402ac6454SAndrew Thompson 		}
55502ac6454SAndrew Thompson 		return;
55602ac6454SAndrew Thompson 
55702ac6454SAndrew Thompson 	default:			/* Error */
55802ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
55902ac6454SAndrew Thompson 			/* try to clear stall first */
56002ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
56102ac6454SAndrew Thompson 			goto tr_setup;
56202ac6454SAndrew Thompson 		}
56302ac6454SAndrew Thompson 		return;
56402ac6454SAndrew Thompson 
56502ac6454SAndrew Thompson 	}
56602ac6454SAndrew Thompson }
56702ac6454SAndrew Thompson 
56802ac6454SAndrew Thompson static void
569760bc48eSAndrew Thompson ubsa_read_callback(struct usb_xfer *xfer)
57002ac6454SAndrew Thompson {
57102ac6454SAndrew Thompson 	struct ubsa_softc *sc = xfer->priv_sc;
57202ac6454SAndrew Thompson 
57302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
57402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
575a593f6b8SAndrew Thompson 		ucom_put_data(&sc->sc_ucom, xfer->frbuffers, 0, xfer->actlen);
57602ac6454SAndrew Thompson 
57702ac6454SAndrew Thompson 	case USB_ST_SETUP:
57802ac6454SAndrew Thompson tr_setup:
57902ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
580a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
58102ac6454SAndrew Thompson 		return;
58202ac6454SAndrew Thompson 
58302ac6454SAndrew Thompson 	default:			/* Error */
58402ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
58502ac6454SAndrew Thompson 			/* try to clear stall first */
58602ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
58702ac6454SAndrew Thompson 			goto tr_setup;
58802ac6454SAndrew Thompson 		}
58902ac6454SAndrew Thompson 		return;
59002ac6454SAndrew Thompson 
59102ac6454SAndrew Thompson 	}
59202ac6454SAndrew Thompson }
59302ac6454SAndrew Thompson 
59402ac6454SAndrew Thompson static void
595760bc48eSAndrew Thompson ubsa_intr_callback(struct usb_xfer *xfer)
59602ac6454SAndrew Thompson {
59702ac6454SAndrew Thompson 	struct ubsa_softc *sc = xfer->priv_sc;
59802ac6454SAndrew Thompson 	uint8_t buf[4];
59902ac6454SAndrew Thompson 
60002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
60102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
60202ac6454SAndrew Thompson 
60302ac6454SAndrew Thompson 		if (xfer->actlen >= sizeof(buf)) {
60402ac6454SAndrew Thompson 
605a593f6b8SAndrew Thompson 			usbd_copy_out(xfer->frbuffers, 0, buf, sizeof(buf));
60602ac6454SAndrew Thompson 
60702ac6454SAndrew Thompson 			/*
60802ac6454SAndrew Thompson 			 * incidentally, Belkin adapter status bits match
60902ac6454SAndrew Thompson 			 * UART 16550 bits
61002ac6454SAndrew Thompson 			 */
61102ac6454SAndrew Thompson 			sc->sc_lsr = buf[2];
61202ac6454SAndrew Thompson 			sc->sc_msr = buf[3];
61302ac6454SAndrew Thompson 
61402ac6454SAndrew Thompson 			DPRINTF("lsr = 0x%02x, msr = 0x%02x\n",
61502ac6454SAndrew Thompson 			    sc->sc_lsr, sc->sc_msr);
61602ac6454SAndrew Thompson 
617a593f6b8SAndrew Thompson 			ucom_status_change(&sc->sc_ucom);
61802ac6454SAndrew Thompson 		} else {
61902ac6454SAndrew Thompson 			DPRINTF("ignoring short packet, %d bytes\n",
62002ac6454SAndrew Thompson 			    xfer->actlen);
62102ac6454SAndrew Thompson 		}
62202ac6454SAndrew Thompson 
62302ac6454SAndrew Thompson 	case USB_ST_SETUP:
62402ac6454SAndrew Thompson tr_setup:
62502ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
626a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
62702ac6454SAndrew Thompson 		return;
62802ac6454SAndrew Thompson 
62902ac6454SAndrew Thompson 	default:			/* Error */
63002ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
63102ac6454SAndrew Thompson 			/* try to clear stall first */
63202ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
63302ac6454SAndrew Thompson 			goto tr_setup;
63402ac6454SAndrew Thompson 		}
63502ac6454SAndrew Thompson 		return;
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson 	}
63802ac6454SAndrew Thompson }
639