xref: /freebsd/sys/dev/usb/misc/udbp.c (revision bc9372d784c2bd4874f221bf7cd8072cb157dc71)
102ac6454SAndrew Thompson /*-
27282444bSPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
37282444bSPedro F. Giffuni  *
402ac6454SAndrew Thompson  * Copyright (c) 1996-2000 Whistle Communications, Inc.
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  * 3. Neither the name of author nor the names of its
1602ac6454SAndrew Thompson  *    contributors may be used to endorse or promote products derived
1702ac6454SAndrew Thompson  *    from this software without specific prior written permission.
1802ac6454SAndrew Thompson  *
1902ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY NICK HIBMA AND CONTRIBUTORS
2002ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2102ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2202ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
2302ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2402ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2502ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2602ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2702ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2802ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2902ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
3002ac6454SAndrew Thompson  *
3102ac6454SAndrew Thompson  */
3202ac6454SAndrew Thompson 
3302ac6454SAndrew Thompson #include <sys/cdefs.h>
3402ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
3502ac6454SAndrew Thompson 
3602ac6454SAndrew Thompson /* Driver for arbitrary double bulk pipe devices.
3702ac6454SAndrew Thompson  * The driver assumes that there will be the same driver on the other side.
3802ac6454SAndrew Thompson  *
3902ac6454SAndrew Thompson  * XXX Some more information on what the framing of the IP packets looks like.
4002ac6454SAndrew Thompson  *
4102ac6454SAndrew Thompson  * To take full advantage of bulk transmission, packets should be chosen
4202ac6454SAndrew Thompson  * between 1k and 5k in size (1k to make sure the sending side starts
4302ac6454SAndrew Thompson  * streaming, and <5k to avoid overflowing the system with small TDs).
4402ac6454SAndrew Thompson  */
4502ac6454SAndrew Thompson 
4602ac6454SAndrew Thompson /* probe/attach/detach:
4702ac6454SAndrew Thompson  *  Connect the driver to the hardware and netgraph
4802ac6454SAndrew Thompson  *
4902ac6454SAndrew Thompson  *  The reason we submit a bulk in transfer is that USB does not know about
5002ac6454SAndrew Thompson  *  interrupts. The bulk transfer continuously polls the device for data.
5102ac6454SAndrew Thompson  *  While the device has no data available, the device NAKs the TDs. As soon
5202ac6454SAndrew Thompson  *  as there is data, the transfer happens and the data comes flowing in.
5302ac6454SAndrew Thompson  *
5402ac6454SAndrew Thompson  *  In case you were wondering, interrupt transfers happen exactly that way.
5502ac6454SAndrew Thompson  *  It therefore doesn't make sense to use the interrupt pipe to signal
5602ac6454SAndrew Thompson  *  'data ready' and then schedule a bulk transfer to fetch it. That would
5702ac6454SAndrew Thompson  *  incur a 2ms delay at least, without reducing bandwidth requirements.
5802ac6454SAndrew Thompson  *
5902ac6454SAndrew Thompson  */
6002ac6454SAndrew Thompson 
61ed6d949aSAndrew Thompson #include <sys/stdint.h>
62ed6d949aSAndrew Thompson #include <sys/stddef.h>
63ed6d949aSAndrew Thompson #include <sys/param.h>
64ed6d949aSAndrew Thompson #include <sys/queue.h>
65ed6d949aSAndrew Thompson #include <sys/types.h>
66ed6d949aSAndrew Thompson #include <sys/systm.h>
67ed6d949aSAndrew Thompson #include <sys/kernel.h>
68ed6d949aSAndrew Thompson #include <sys/bus.h>
69ed6d949aSAndrew Thompson #include <sys/module.h>
70ed6d949aSAndrew Thompson #include <sys/lock.h>
71ed6d949aSAndrew Thompson #include <sys/mutex.h>
72ed6d949aSAndrew Thompson #include <sys/condvar.h>
73ed6d949aSAndrew Thompson #include <sys/sysctl.h>
74ed6d949aSAndrew Thompson #include <sys/sx.h>
75ed6d949aSAndrew Thompson #include <sys/unistd.h>
76ed6d949aSAndrew Thompson #include <sys/callout.h>
77ed6d949aSAndrew Thompson #include <sys/malloc.h>
78ed6d949aSAndrew Thompson #include <sys/priv.h>
79ed6d949aSAndrew Thompson 
8002ac6454SAndrew Thompson #include <dev/usb/usb.h>
81ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
82ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
83ed6d949aSAndrew Thompson #include "usbdevs.h"
8402ac6454SAndrew Thompson 
8502ac6454SAndrew Thompson #define	USB_DEBUG_VAR udbp_debug
8602ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
8702ac6454SAndrew Thompson 
8802ac6454SAndrew Thompson #include <sys/mbuf.h>
8902ac6454SAndrew Thompson 
9002ac6454SAndrew Thompson #include <netgraph/ng_message.h>
9102ac6454SAndrew Thompson #include <netgraph/netgraph.h>
9202ac6454SAndrew Thompson #include <netgraph/ng_parse.h>
9302ac6454SAndrew Thompson #include <netgraph/bluetooth/include/ng_bluetooth.h>
9402ac6454SAndrew Thompson 
9502ac6454SAndrew Thompson #include <dev/usb/misc/udbp.h>
9602ac6454SAndrew Thompson 
97b850ecc1SAndrew Thompson #ifdef USB_DEBUG
9802ac6454SAndrew Thompson static int udbp_debug = 0;
9902ac6454SAndrew Thompson 
100f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, udbp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
101f8d2b1f3SPawel Biernacki     "USB udbp");
102ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_udbp, OID_AUTO, debug, CTLFLAG_RWTUN,
10302ac6454SAndrew Thompson     &udbp_debug, 0, "udbp debug level");
10402ac6454SAndrew Thompson #endif
10502ac6454SAndrew Thompson 
10602ac6454SAndrew Thompson #define	UDBP_TIMEOUT	2000		/* timeout on outbound transfers, in
10702ac6454SAndrew Thompson 					 * msecs */
10802ac6454SAndrew Thompson #define	UDBP_BUFFERSIZE	MCLBYTES	/* maximum number of bytes in one
10902ac6454SAndrew Thompson 					 * transfer */
11002ac6454SAndrew Thompson #define	UDBP_T_WR       0
11102ac6454SAndrew Thompson #define	UDBP_T_RD       1
11202ac6454SAndrew Thompson #define	UDBP_T_WR_CS    2
11302ac6454SAndrew Thompson #define	UDBP_T_RD_CS    3
11402ac6454SAndrew Thompson #define	UDBP_T_MAX      4
11502ac6454SAndrew Thompson #define	UDBP_Q_MAXLEN   50
11602ac6454SAndrew Thompson 
11702ac6454SAndrew Thompson struct udbp_softc {
11802ac6454SAndrew Thompson 	struct mtx sc_mtx;
11902ac6454SAndrew Thompson 	struct ng_bt_mbufq sc_xmitq_hipri;	/* hi-priority transmit queue */
12002ac6454SAndrew Thompson 	struct ng_bt_mbufq sc_xmitq;	/* low-priority transmit queue */
12102ac6454SAndrew Thompson 
122760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UDBP_T_MAX];
12302ac6454SAndrew Thompson 	node_p	sc_node;		/* back pointer to node */
12402ac6454SAndrew Thompson 	hook_p	sc_hook;		/* pointer to the hook */
12502ac6454SAndrew Thompson 	struct mbuf *sc_bulk_in_buffer;
12602ac6454SAndrew Thompson 
12702ac6454SAndrew Thompson 	uint32_t sc_packets_in;		/* packets in from downstream */
12802ac6454SAndrew Thompson 	uint32_t sc_packets_out;	/* packets out towards downstream */
12902ac6454SAndrew Thompson 
13002ac6454SAndrew Thompson 	uint8_t	sc_flags;
13102ac6454SAndrew Thompson #define	UDBP_FLAG_READ_STALL    0x01	/* read transfer stalled */
13202ac6454SAndrew Thompson #define	UDBP_FLAG_WRITE_STALL   0x02	/* write transfer stalled */
13302ac6454SAndrew Thompson 
13402ac6454SAndrew Thompson 	uint8_t	sc_name[16];
13502ac6454SAndrew Thompson };
13602ac6454SAndrew Thompson 
13702ac6454SAndrew Thompson /* prototypes */
13802ac6454SAndrew Thompson 
13902ac6454SAndrew Thompson static int udbp_modload(module_t mod, int event, void *data);
14002ac6454SAndrew Thompson 
14102ac6454SAndrew Thompson static device_probe_t udbp_probe;
14202ac6454SAndrew Thompson static device_attach_t udbp_attach;
14302ac6454SAndrew Thompson static device_detach_t udbp_detach;
14402ac6454SAndrew Thompson 
145e0a69b51SAndrew Thompson static usb_callback_t udbp_bulk_read_callback;
146e0a69b51SAndrew Thompson static usb_callback_t udbp_bulk_read_clear_stall_callback;
147e0a69b51SAndrew Thompson static usb_callback_t udbp_bulk_write_callback;
148e0a69b51SAndrew Thompson static usb_callback_t udbp_bulk_write_clear_stall_callback;
14902ac6454SAndrew Thompson 
15002ac6454SAndrew Thompson static void	udbp_bulk_read_complete(node_p, hook_p, void *, int);
15102ac6454SAndrew Thompson 
15202ac6454SAndrew Thompson static ng_constructor_t	ng_udbp_constructor;
15302ac6454SAndrew Thompson static ng_rcvmsg_t	ng_udbp_rcvmsg;
15402ac6454SAndrew Thompson static ng_shutdown_t	ng_udbp_rmnode;
15502ac6454SAndrew Thompson static ng_newhook_t	ng_udbp_newhook;
15602ac6454SAndrew Thompson static ng_connect_t	ng_udbp_connect;
15702ac6454SAndrew Thompson static ng_rcvdata_t	ng_udbp_rcvdata;
15802ac6454SAndrew Thompson static ng_disconnect_t	ng_udbp_disconnect;
15902ac6454SAndrew Thompson 
16002ac6454SAndrew Thompson /* Parse type for struct ngudbpstat */
16102ac6454SAndrew Thompson static const struct ng_parse_struct_field
16202ac6454SAndrew Thompson 	ng_udbp_stat_type_fields[] = NG_UDBP_STATS_TYPE_INFO;
16302ac6454SAndrew Thompson 
16402ac6454SAndrew Thompson static const struct ng_parse_type ng_udbp_stat_type = {
16502ac6454SAndrew Thompson 	&ng_parse_struct_type,
16602ac6454SAndrew Thompson 	&ng_udbp_stat_type_fields
16702ac6454SAndrew Thompson };
16802ac6454SAndrew Thompson 
16902ac6454SAndrew Thompson /* List of commands and how to convert arguments to/from ASCII */
17002ac6454SAndrew Thompson static const struct ng_cmdlist ng_udbp_cmdlist[] = {
17102ac6454SAndrew Thompson 	{
17202ac6454SAndrew Thompson 		NGM_UDBP_COOKIE,
17302ac6454SAndrew Thompson 		NGM_UDBP_GET_STATUS,
17402ac6454SAndrew Thompson 		"getstatus",
17502ac6454SAndrew Thompson 		NULL,
17602ac6454SAndrew Thompson 		&ng_udbp_stat_type,
17702ac6454SAndrew Thompson 	},
17802ac6454SAndrew Thompson 	{
17902ac6454SAndrew Thompson 		NGM_UDBP_COOKIE,
18002ac6454SAndrew Thompson 		NGM_UDBP_SET_FLAG,
18102ac6454SAndrew Thompson 		"setflag",
18202ac6454SAndrew Thompson 		&ng_parse_int32_type,
18302ac6454SAndrew Thompson 		NULL
18402ac6454SAndrew Thompson 	},
18502ac6454SAndrew Thompson 	{0}
18602ac6454SAndrew Thompson };
18702ac6454SAndrew Thompson 
18802ac6454SAndrew Thompson /* Netgraph node type descriptor */
18902ac6454SAndrew Thompson static struct ng_type ng_udbp_typestruct = {
19002ac6454SAndrew Thompson 	.version = NG_ABI_VERSION,
19102ac6454SAndrew Thompson 	.name = NG_UDBP_NODE_TYPE,
19202ac6454SAndrew Thompson 	.constructor = ng_udbp_constructor,
19302ac6454SAndrew Thompson 	.rcvmsg = ng_udbp_rcvmsg,
19402ac6454SAndrew Thompson 	.shutdown = ng_udbp_rmnode,
19502ac6454SAndrew Thompson 	.newhook = ng_udbp_newhook,
19602ac6454SAndrew Thompson 	.connect = ng_udbp_connect,
19702ac6454SAndrew Thompson 	.rcvdata = ng_udbp_rcvdata,
19802ac6454SAndrew Thompson 	.disconnect = ng_udbp_disconnect,
19902ac6454SAndrew Thompson 	.cmdlist = ng_udbp_cmdlist,
20002ac6454SAndrew Thompson };
20102ac6454SAndrew Thompson 
20202ac6454SAndrew Thompson /* USB config */
203760bc48eSAndrew Thompson static const struct usb_config udbp_config[UDBP_T_MAX] = {
20402ac6454SAndrew Thompson 	[UDBP_T_WR] = {
20502ac6454SAndrew Thompson 		.type = UE_BULK,
20602ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
20702ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
2084eae601eSAndrew Thompson 		.bufsize = UDBP_BUFFERSIZE,
2094eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2104eae601eSAndrew Thompson 		.callback = &udbp_bulk_write_callback,
2114eae601eSAndrew Thompson 		.timeout = UDBP_TIMEOUT,
21202ac6454SAndrew Thompson 	},
21302ac6454SAndrew Thompson 
21402ac6454SAndrew Thompson 	[UDBP_T_RD] = {
21502ac6454SAndrew Thompson 		.type = UE_BULK,
21602ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
21702ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2184eae601eSAndrew Thompson 		.bufsize = UDBP_BUFFERSIZE,
2194eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2204eae601eSAndrew Thompson 		.callback = &udbp_bulk_read_callback,
22102ac6454SAndrew Thompson 	},
22202ac6454SAndrew Thompson 
22302ac6454SAndrew Thompson 	[UDBP_T_WR_CS] = {
22402ac6454SAndrew Thompson 		.type = UE_CONTROL,
22502ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
22602ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
227760bc48eSAndrew Thompson 		.bufsize = sizeof(struct usb_device_request),
2284eae601eSAndrew Thompson 		.callback = &udbp_bulk_write_clear_stall_callback,
2294eae601eSAndrew Thompson 		.timeout = 1000,	/* 1 second */
2304eae601eSAndrew Thompson 		.interval = 50,	/* 50ms */
23102ac6454SAndrew Thompson 	},
23202ac6454SAndrew Thompson 
23302ac6454SAndrew Thompson 	[UDBP_T_RD_CS] = {
23402ac6454SAndrew Thompson 		.type = UE_CONTROL,
23502ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
23602ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
237760bc48eSAndrew Thompson 		.bufsize = sizeof(struct usb_device_request),
2384eae601eSAndrew Thompson 		.callback = &udbp_bulk_read_clear_stall_callback,
2394eae601eSAndrew Thompson 		.timeout = 1000,	/* 1 second */
2404eae601eSAndrew Thompson 		.interval = 50,	/* 50ms */
24102ac6454SAndrew Thompson 	},
24202ac6454SAndrew Thompson };
24302ac6454SAndrew Thompson 
24402ac6454SAndrew Thompson static device_method_t udbp_methods[] = {
24502ac6454SAndrew Thompson 	/* Device interface */
24602ac6454SAndrew Thompson 	DEVMETHOD(device_probe, udbp_probe),
24702ac6454SAndrew Thompson 	DEVMETHOD(device_attach, udbp_attach),
24802ac6454SAndrew Thompson 	DEVMETHOD(device_detach, udbp_detach),
24961bfd867SSofian Brabez 
25061bfd867SSofian Brabez 	DEVMETHOD_END
25102ac6454SAndrew Thompson };
25202ac6454SAndrew Thompson 
25302ac6454SAndrew Thompson static driver_t udbp_driver = {
25402ac6454SAndrew Thompson 	.name = "udbp",
25502ac6454SAndrew Thompson 	.methods = udbp_methods,
25602ac6454SAndrew Thompson 	.size = sizeof(struct udbp_softc),
25702ac6454SAndrew Thompson };
25802ac6454SAndrew Thompson 
259f809f280SWarner Losh static const STRUCT_USB_HOST_ID udbp_devs[] = {
260ae9fd9ccSBruce M Simpson 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U258, 0)},
261f809f280SWarner Losh 	{USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_TURBOCONNECT, 0)},
262f809f280SWarner Losh 	{USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_GADGETZERO, 0)},
263f809f280SWarner Losh 	{USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2301, 0)},
264f809f280SWarner Losh 	{USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2302, 0)},
2651d4c696aSBruce M Simpson 	{USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL27A1, 0)},
266f809f280SWarner Losh 	{USB_VPI(USB_VENDOR_ANCHOR, USB_PRODUCT_ANCHOR_EZLINK, 0)},
267f809f280SWarner Losh 	{USB_VPI(USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL620USB, 0)},
268f809f280SWarner Losh };
269f809f280SWarner Losh 
270*bc9372d7SJohn Baldwin DRIVER_MODULE(udbp, uhub, udbp_driver, udbp_modload, NULL);
27102ac6454SAndrew Thompson MODULE_DEPEND(udbp, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
27202ac6454SAndrew Thompson MODULE_DEPEND(udbp, usb, 1, 1, 1);
273910cb8feSAndrew Thompson MODULE_VERSION(udbp, 1);
274f809f280SWarner Losh USB_PNP_HOST_INFO(udbp_devs);
27502ac6454SAndrew Thompson 
27602ac6454SAndrew Thompson static int
27702ac6454SAndrew Thompson udbp_modload(module_t mod, int event, void *data)
27802ac6454SAndrew Thompson {
27902ac6454SAndrew Thompson 	int error;
28002ac6454SAndrew Thompson 
28102ac6454SAndrew Thompson 	switch (event) {
28202ac6454SAndrew Thompson 	case MOD_LOAD:
28302ac6454SAndrew Thompson 		error = ng_newtype(&ng_udbp_typestruct);
28402ac6454SAndrew Thompson 		if (error != 0) {
28502ac6454SAndrew Thompson 			printf("%s: Could not register "
28602ac6454SAndrew Thompson 			    "Netgraph node type, error=%d\n",
28702ac6454SAndrew Thompson 			    NG_UDBP_NODE_TYPE, error);
28802ac6454SAndrew Thompson 		}
28902ac6454SAndrew Thompson 		break;
29002ac6454SAndrew Thompson 
29102ac6454SAndrew Thompson 	case MOD_UNLOAD:
29202ac6454SAndrew Thompson 		error = ng_rmtype(&ng_udbp_typestruct);
29302ac6454SAndrew Thompson 		break;
29402ac6454SAndrew Thompson 
29502ac6454SAndrew Thompson 	default:
29602ac6454SAndrew Thompson 		error = EOPNOTSUPP;
29702ac6454SAndrew Thompson 		break;
29802ac6454SAndrew Thompson 	}
29902ac6454SAndrew Thompson 	return (error);
30002ac6454SAndrew Thompson }
30102ac6454SAndrew Thompson 
30202ac6454SAndrew Thompson static int
30302ac6454SAndrew Thompson udbp_probe(device_t dev)
30402ac6454SAndrew Thompson {
305760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
30602ac6454SAndrew Thompson 
3079cfbe3e7SHans Petter Selasky 	if (uaa->usb_mode != USB_MODE_HOST)
30802ac6454SAndrew Thompson 		return (ENXIO);
3099cfbe3e7SHans Petter Selasky 	if (uaa->info.bConfigIndex != 0)
31002ac6454SAndrew Thompson 		return (ENXIO);
3119cfbe3e7SHans Petter Selasky 	if (uaa->info.bIfaceIndex != 0)
3129cfbe3e7SHans Petter Selasky 		return (ENXIO);
3139cfbe3e7SHans Petter Selasky 
3149cfbe3e7SHans Petter Selasky 	return (usbd_lookup_id_by_uaa(udbp_devs, sizeof(udbp_devs), uaa));
31502ac6454SAndrew Thompson }
31602ac6454SAndrew Thompson 
31702ac6454SAndrew Thompson static int
31802ac6454SAndrew Thompson udbp_attach(device_t dev)
31902ac6454SAndrew Thompson {
320760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
32102ac6454SAndrew Thompson 	struct udbp_softc *sc = device_get_softc(dev);
32202ac6454SAndrew Thompson 	int error;
32302ac6454SAndrew Thompson 
324a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
32502ac6454SAndrew Thompson 
32602ac6454SAndrew Thompson 	snprintf(sc->sc_name, sizeof(sc->sc_name),
32702ac6454SAndrew Thompson 	    "%s", device_get_nameunit(dev));
32802ac6454SAndrew Thompson 
32902ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, "udbp lock", NULL, MTX_DEF | MTX_RECURSE);
33002ac6454SAndrew Thompson 
331a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device, &uaa->info.bIfaceIndex,
33202ac6454SAndrew Thompson 	    sc->sc_xfer, udbp_config, UDBP_T_MAX, sc, &sc->sc_mtx);
33302ac6454SAndrew Thompson 	if (error) {
334a593f6b8SAndrew Thompson 		DPRINTF("error=%s\n", usbd_errstr(error));
33502ac6454SAndrew Thompson 		goto detach;
33602ac6454SAndrew Thompson 	}
33702ac6454SAndrew Thompson 	NG_BT_MBUFQ_INIT(&sc->sc_xmitq, UDBP_Q_MAXLEN);
33802ac6454SAndrew Thompson 
33902ac6454SAndrew Thompson 	NG_BT_MBUFQ_INIT(&sc->sc_xmitq_hipri, UDBP_Q_MAXLEN);
34002ac6454SAndrew Thompson 
34102ac6454SAndrew Thompson 	/* create Netgraph node */
34202ac6454SAndrew Thompson 
34302ac6454SAndrew Thompson 	if (ng_make_node_common(&ng_udbp_typestruct, &sc->sc_node) != 0) {
34402ac6454SAndrew Thompson 		printf("%s: Could not create Netgraph node\n",
34502ac6454SAndrew Thompson 		    sc->sc_name);
34602ac6454SAndrew Thompson 		sc->sc_node = NULL;
34702ac6454SAndrew Thompson 		goto detach;
34802ac6454SAndrew Thompson 	}
34902ac6454SAndrew Thompson 	/* name node */
35002ac6454SAndrew Thompson 
35102ac6454SAndrew Thompson 	if (ng_name_node(sc->sc_node, sc->sc_name) != 0) {
35202ac6454SAndrew Thompson 		printf("%s: Could not name node\n",
35302ac6454SAndrew Thompson 		    sc->sc_name);
35402ac6454SAndrew Thompson 		NG_NODE_UNREF(sc->sc_node);
35502ac6454SAndrew Thompson 		sc->sc_node = NULL;
35602ac6454SAndrew Thompson 		goto detach;
35702ac6454SAndrew Thompson 	}
35802ac6454SAndrew Thompson 	NG_NODE_SET_PRIVATE(sc->sc_node, sc);
35902ac6454SAndrew Thompson 
36002ac6454SAndrew Thompson 	/* the device is now operational */
36102ac6454SAndrew Thompson 
36202ac6454SAndrew Thompson 	return (0);			/* success */
36302ac6454SAndrew Thompson 
36402ac6454SAndrew Thompson detach:
36502ac6454SAndrew Thompson 	udbp_detach(dev);
36602ac6454SAndrew Thompson 	return (ENOMEM);		/* failure */
36702ac6454SAndrew Thompson }
36802ac6454SAndrew Thompson 
36902ac6454SAndrew Thompson static int
37002ac6454SAndrew Thompson udbp_detach(device_t dev)
37102ac6454SAndrew Thompson {
37202ac6454SAndrew Thompson 	struct udbp_softc *sc = device_get_softc(dev);
37302ac6454SAndrew Thompson 
37402ac6454SAndrew Thompson 	/* destroy Netgraph node */
37502ac6454SAndrew Thompson 
37602ac6454SAndrew Thompson 	if (sc->sc_node != NULL) {
37702ac6454SAndrew Thompson 		NG_NODE_SET_PRIVATE(sc->sc_node, NULL);
37802ac6454SAndrew Thompson 		ng_rmnode_self(sc->sc_node);
37902ac6454SAndrew Thompson 		sc->sc_node = NULL;
38002ac6454SAndrew Thompson 	}
38102ac6454SAndrew Thompson 	/* free USB transfers, if any */
38202ac6454SAndrew Thompson 
383a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UDBP_T_MAX);
38402ac6454SAndrew Thompson 
38502ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
38602ac6454SAndrew Thompson 
38702ac6454SAndrew Thompson 	/* destroy queues */
38802ac6454SAndrew Thompson 
38902ac6454SAndrew Thompson 	NG_BT_MBUFQ_DESTROY(&sc->sc_xmitq);
39002ac6454SAndrew Thompson 	NG_BT_MBUFQ_DESTROY(&sc->sc_xmitq_hipri);
39102ac6454SAndrew Thompson 
39202ac6454SAndrew Thompson 	/* extra check */
39302ac6454SAndrew Thompson 
39402ac6454SAndrew Thompson 	if (sc->sc_bulk_in_buffer) {
39502ac6454SAndrew Thompson 		m_freem(sc->sc_bulk_in_buffer);
39602ac6454SAndrew Thompson 		sc->sc_bulk_in_buffer = NULL;
39702ac6454SAndrew Thompson 	}
39802ac6454SAndrew Thompson 	return (0);			/* success */
39902ac6454SAndrew Thompson }
40002ac6454SAndrew Thompson 
40102ac6454SAndrew Thompson static void
402ed6d949aSAndrew Thompson udbp_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
40302ac6454SAndrew Thompson {
404ed6d949aSAndrew Thompson 	struct udbp_softc *sc = usbd_xfer_softc(xfer);
405ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
40602ac6454SAndrew Thompson 	struct mbuf *m;
407ed6d949aSAndrew Thompson 	int actlen;
408ed6d949aSAndrew Thompson 
409ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
41002ac6454SAndrew Thompson 
41102ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
41202ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
41302ac6454SAndrew Thompson 
41402ac6454SAndrew Thompson 		/* allocate new mbuf */
41502ac6454SAndrew Thompson 
416c6499eccSGleb Smirnoff 		MGETHDR(m, M_NOWAIT, MT_DATA);
41702ac6454SAndrew Thompson 
41802ac6454SAndrew Thompson 		if (m == NULL) {
41902ac6454SAndrew Thompson 			goto tr_setup;
42002ac6454SAndrew Thompson 		}
42102ac6454SAndrew Thompson 
4222a8c860fSRobert Watson 		if (!(MCLGET(m, M_NOWAIT))) {
42302ac6454SAndrew Thompson 			m_freem(m);
42402ac6454SAndrew Thompson 			goto tr_setup;
42502ac6454SAndrew Thompson 		}
426ed6d949aSAndrew Thompson 		m->m_pkthdr.len = m->m_len = actlen;
42702ac6454SAndrew Thompson 
428ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
429ed6d949aSAndrew Thompson 		usbd_copy_out(pc, 0, m->m_data, actlen);
43002ac6454SAndrew Thompson 
43102ac6454SAndrew Thompson 		sc->sc_bulk_in_buffer = m;
43202ac6454SAndrew Thompson 
433ed6d949aSAndrew Thompson 		DPRINTF("received package %d bytes\n", actlen);
43402ac6454SAndrew Thompson 
43502ac6454SAndrew Thompson 	case USB_ST_SETUP:
43602ac6454SAndrew Thompson tr_setup:
43702ac6454SAndrew Thompson 		if (sc->sc_bulk_in_buffer) {
43802ac6454SAndrew Thompson 			ng_send_fn(sc->sc_node, NULL, &udbp_bulk_read_complete, NULL, 0);
43902ac6454SAndrew Thompson 			return;
44002ac6454SAndrew Thompson 		}
44102ac6454SAndrew Thompson 		if (sc->sc_flags & UDBP_FLAG_READ_STALL) {
442a593f6b8SAndrew Thompson 			usbd_transfer_start(sc->sc_xfer[UDBP_T_RD_CS]);
44302ac6454SAndrew Thompson 			return;
44402ac6454SAndrew Thompson 		}
445ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
446a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
44702ac6454SAndrew Thompson 		return;
44802ac6454SAndrew Thompson 
44902ac6454SAndrew Thompson 	default:			/* Error */
450ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
45102ac6454SAndrew Thompson 			/* try to clear stall first */
45202ac6454SAndrew Thompson 			sc->sc_flags |= UDBP_FLAG_READ_STALL;
453a593f6b8SAndrew Thompson 			usbd_transfer_start(sc->sc_xfer[UDBP_T_RD_CS]);
45402ac6454SAndrew Thompson 		}
45502ac6454SAndrew Thompson 		return;
45602ac6454SAndrew Thompson 	}
45702ac6454SAndrew Thompson }
45802ac6454SAndrew Thompson 
45902ac6454SAndrew Thompson static void
460ed6d949aSAndrew Thompson udbp_bulk_read_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
46102ac6454SAndrew Thompson {
462ed6d949aSAndrew Thompson 	struct udbp_softc *sc = usbd_xfer_softc(xfer);
463760bc48eSAndrew Thompson 	struct usb_xfer *xfer_other = sc->sc_xfer[UDBP_T_RD];
46402ac6454SAndrew Thompson 
465a593f6b8SAndrew Thompson 	if (usbd_clear_stall_callback(xfer, xfer_other)) {
46602ac6454SAndrew Thompson 		DPRINTF("stall cleared\n");
46702ac6454SAndrew Thompson 		sc->sc_flags &= ~UDBP_FLAG_READ_STALL;
468a593f6b8SAndrew Thompson 		usbd_transfer_start(xfer_other);
46902ac6454SAndrew Thompson 	}
47002ac6454SAndrew Thompson }
47102ac6454SAndrew Thompson 
47202ac6454SAndrew Thompson static void
47302ac6454SAndrew Thompson udbp_bulk_read_complete(node_p node, hook_p hook, void *arg1, int arg2)
47402ac6454SAndrew Thompson {
47502ac6454SAndrew Thompson 	struct udbp_softc *sc = NG_NODE_PRIVATE(node);
47602ac6454SAndrew Thompson 	struct mbuf *m;
47702ac6454SAndrew Thompson 	int error;
47802ac6454SAndrew Thompson 
47902ac6454SAndrew Thompson 	if (sc == NULL) {
48002ac6454SAndrew Thompson 		return;
48102ac6454SAndrew Thompson 	}
48202ac6454SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
48302ac6454SAndrew Thompson 
48402ac6454SAndrew Thompson 	m = sc->sc_bulk_in_buffer;
48502ac6454SAndrew Thompson 
48602ac6454SAndrew Thompson 	if (m) {
48702ac6454SAndrew Thompson 		sc->sc_bulk_in_buffer = NULL;
48802ac6454SAndrew Thompson 
48902ac6454SAndrew Thompson 		if ((sc->sc_hook == NULL) ||
49002ac6454SAndrew Thompson 		    NG_HOOK_NOT_VALID(sc->sc_hook)) {
49102ac6454SAndrew Thompson 			DPRINTF("No upstream hook\n");
49202ac6454SAndrew Thompson 			goto done;
49302ac6454SAndrew Thompson 		}
49402ac6454SAndrew Thompson 		sc->sc_packets_in++;
49502ac6454SAndrew Thompson 
49602ac6454SAndrew Thompson 		NG_SEND_DATA_ONLY(error, sc->sc_hook, m);
49702ac6454SAndrew Thompson 
49802ac6454SAndrew Thompson 		m = NULL;
49902ac6454SAndrew Thompson 	}
50002ac6454SAndrew Thompson done:
50102ac6454SAndrew Thompson 	if (m) {
50202ac6454SAndrew Thompson 		m_freem(m);
50302ac6454SAndrew Thompson 	}
50402ac6454SAndrew Thompson 	/* start USB bulk-in transfer, if not already started */
50502ac6454SAndrew Thompson 
506a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UDBP_T_RD]);
50702ac6454SAndrew Thompson 
50802ac6454SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
50902ac6454SAndrew Thompson }
51002ac6454SAndrew Thompson 
51102ac6454SAndrew Thompson static void
512ed6d949aSAndrew Thompson udbp_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
51302ac6454SAndrew Thompson {
514ed6d949aSAndrew Thompson 	struct udbp_softc *sc = usbd_xfer_softc(xfer);
515ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
51602ac6454SAndrew Thompson 	struct mbuf *m;
51702ac6454SAndrew Thompson 
51802ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
51902ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
52002ac6454SAndrew Thompson 
52102ac6454SAndrew Thompson 		sc->sc_packets_out++;
52202ac6454SAndrew Thompson 
52302ac6454SAndrew Thompson 	case USB_ST_SETUP:
52402ac6454SAndrew Thompson 		if (sc->sc_flags & UDBP_FLAG_WRITE_STALL) {
525a593f6b8SAndrew Thompson 			usbd_transfer_start(sc->sc_xfer[UDBP_T_WR_CS]);
52602ac6454SAndrew Thompson 			return;
52702ac6454SAndrew Thompson 		}
52802ac6454SAndrew Thompson 		/* get next mbuf, if any */
52902ac6454SAndrew Thompson 
53002ac6454SAndrew Thompson 		NG_BT_MBUFQ_DEQUEUE(&sc->sc_xmitq_hipri, m);
53102ac6454SAndrew Thompson 		if (m == NULL) {
53202ac6454SAndrew Thompson 			NG_BT_MBUFQ_DEQUEUE(&sc->sc_xmitq, m);
53302ac6454SAndrew Thompson 			if (m == NULL) {
53402ac6454SAndrew Thompson 				DPRINTF("Data queue is empty\n");
53502ac6454SAndrew Thompson 				return;
53602ac6454SAndrew Thompson 			}
53702ac6454SAndrew Thompson 		}
53802ac6454SAndrew Thompson 		if (m->m_pkthdr.len > MCLBYTES) {
53902ac6454SAndrew Thompson 			DPRINTF("truncating large packet "
54002ac6454SAndrew Thompson 			    "from %d to %d bytes\n", m->m_pkthdr.len,
54102ac6454SAndrew Thompson 			    MCLBYTES);
54202ac6454SAndrew Thompson 			m->m_pkthdr.len = MCLBYTES;
54302ac6454SAndrew Thompson 		}
544ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
545ed6d949aSAndrew Thompson 		usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
54602ac6454SAndrew Thompson 
547ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
548ed6d949aSAndrew Thompson 
549ed6d949aSAndrew Thompson 		DPRINTF("packet out: %d bytes\n", m->m_pkthdr.len);
55002ac6454SAndrew Thompson 
55102ac6454SAndrew Thompson 		m_freem(m);
55202ac6454SAndrew Thompson 
553a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
55402ac6454SAndrew Thompson 		return;
55502ac6454SAndrew Thompson 
55602ac6454SAndrew Thompson 	default:			/* Error */
557ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
55802ac6454SAndrew Thompson 			/* try to clear stall first */
55902ac6454SAndrew Thompson 			sc->sc_flags |= UDBP_FLAG_WRITE_STALL;
560a593f6b8SAndrew Thompson 			usbd_transfer_start(sc->sc_xfer[UDBP_T_WR_CS]);
56102ac6454SAndrew Thompson 		}
56202ac6454SAndrew Thompson 		return;
56302ac6454SAndrew Thompson 	}
56402ac6454SAndrew Thompson }
56502ac6454SAndrew Thompson 
56602ac6454SAndrew Thompson static void
567ed6d949aSAndrew Thompson udbp_bulk_write_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
56802ac6454SAndrew Thompson {
569ed6d949aSAndrew Thompson 	struct udbp_softc *sc = usbd_xfer_softc(xfer);
570760bc48eSAndrew Thompson 	struct usb_xfer *xfer_other = sc->sc_xfer[UDBP_T_WR];
57102ac6454SAndrew Thompson 
572a593f6b8SAndrew Thompson 	if (usbd_clear_stall_callback(xfer, xfer_other)) {
57302ac6454SAndrew Thompson 		DPRINTF("stall cleared\n");
57402ac6454SAndrew Thompson 		sc->sc_flags &= ~UDBP_FLAG_WRITE_STALL;
575a593f6b8SAndrew Thompson 		usbd_transfer_start(xfer_other);
57602ac6454SAndrew Thompson 	}
57702ac6454SAndrew Thompson }
57802ac6454SAndrew Thompson 
57902ac6454SAndrew Thompson /***********************************************************************
58002ac6454SAndrew Thompson  * Start of Netgraph methods
58102ac6454SAndrew Thompson  **********************************************************************/
58202ac6454SAndrew Thompson 
58302ac6454SAndrew Thompson /*
58402ac6454SAndrew Thompson  * If this is a device node so this work is done in the attach()
58502ac6454SAndrew Thompson  * routine and the constructor will return EINVAL as you should not be able
58602ac6454SAndrew Thompson  * to create nodes that depend on hardware (unless you can add the hardware :)
58702ac6454SAndrew Thompson  */
58802ac6454SAndrew Thompson static int
58902ac6454SAndrew Thompson ng_udbp_constructor(node_p node)
59002ac6454SAndrew Thompson {
59102ac6454SAndrew Thompson 	return (EINVAL);
59202ac6454SAndrew Thompson }
59302ac6454SAndrew Thompson 
59402ac6454SAndrew Thompson /*
59502ac6454SAndrew Thompson  * Give our ok for a hook to be added...
59602ac6454SAndrew Thompson  * If we are not running this might kick a device into life.
59702ac6454SAndrew Thompson  * Possibly decode information out of the hook name.
59802ac6454SAndrew Thompson  * Add the hook's private info to the hook structure.
59902ac6454SAndrew Thompson  * (if we had some). In this example, we assume that there is a
60002ac6454SAndrew Thompson  * an array of structs, called 'channel' in the private info,
60102ac6454SAndrew Thompson  * one for each active channel. The private
60202ac6454SAndrew Thompson  * pointer of each hook points to the appropriate UDBP_hookinfo struct
60302ac6454SAndrew Thompson  * so that the source of an input packet is easily identified.
60402ac6454SAndrew Thompson  */
60502ac6454SAndrew Thompson static int
60602ac6454SAndrew Thompson ng_udbp_newhook(node_p node, hook_p hook, const char *name)
60702ac6454SAndrew Thompson {
60802ac6454SAndrew Thompson 	struct udbp_softc *sc = NG_NODE_PRIVATE(node);
60902ac6454SAndrew Thompson 	int32_t error = 0;
61002ac6454SAndrew Thompson 
61102ac6454SAndrew Thompson 	if (strcmp(name, NG_UDBP_HOOK_NAME)) {
61202ac6454SAndrew Thompson 		return (EINVAL);
61302ac6454SAndrew Thompson 	}
61402ac6454SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
61502ac6454SAndrew Thompson 
61602ac6454SAndrew Thompson 	if (sc->sc_hook != NULL) {
61702ac6454SAndrew Thompson 		error = EISCONN;
61802ac6454SAndrew Thompson 	} else {
61902ac6454SAndrew Thompson 		sc->sc_hook = hook;
62002ac6454SAndrew Thompson 		NG_HOOK_SET_PRIVATE(hook, NULL);
62102ac6454SAndrew Thompson 	}
62202ac6454SAndrew Thompson 
62302ac6454SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
62402ac6454SAndrew Thompson 
62502ac6454SAndrew Thompson 	return (error);
62602ac6454SAndrew Thompson }
62702ac6454SAndrew Thompson 
62802ac6454SAndrew Thompson /*
62902ac6454SAndrew Thompson  * Get a netgraph control message.
63002ac6454SAndrew Thompson  * Check it is one we understand. If needed, send a response.
63102ac6454SAndrew Thompson  * We could save the address for an async action later, but don't here.
63202ac6454SAndrew Thompson  * Always free the message.
63302ac6454SAndrew Thompson  * The response should be in a malloc'd region that the caller can 'free'.
63402ac6454SAndrew Thompson  * A response is not required.
63502ac6454SAndrew Thompson  * Theoretically you could respond defferently to old message types if
63602ac6454SAndrew Thompson  * the cookie in the header didn't match what we consider to be current
63702ac6454SAndrew Thompson  * (so that old userland programs could continue to work).
63802ac6454SAndrew Thompson  */
63902ac6454SAndrew Thompson static int
64002ac6454SAndrew Thompson ng_udbp_rcvmsg(node_p node, item_p item, hook_p lasthook)
64102ac6454SAndrew Thompson {
64202ac6454SAndrew Thompson 	struct udbp_softc *sc = NG_NODE_PRIVATE(node);
64302ac6454SAndrew Thompson 	struct ng_mesg *resp = NULL;
64402ac6454SAndrew Thompson 	int error = 0;
64502ac6454SAndrew Thompson 	struct ng_mesg *msg;
64602ac6454SAndrew Thompson 
64702ac6454SAndrew Thompson 	NGI_GET_MSG(item, msg);
64802ac6454SAndrew Thompson 	/* Deal with message according to cookie and command */
64902ac6454SAndrew Thompson 	switch (msg->header.typecookie) {
65002ac6454SAndrew Thompson 	case NGM_UDBP_COOKIE:
65102ac6454SAndrew Thompson 		switch (msg->header.cmd) {
65202ac6454SAndrew Thompson 		case NGM_UDBP_GET_STATUS:
65302ac6454SAndrew Thompson 			{
65402ac6454SAndrew Thompson 				struct ngudbpstat *stats;
65502ac6454SAndrew Thompson 
65602ac6454SAndrew Thompson 				NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
65702ac6454SAndrew Thompson 				if (!resp) {
65802ac6454SAndrew Thompson 					error = ENOMEM;
65902ac6454SAndrew Thompson 					break;
66002ac6454SAndrew Thompson 				}
66102ac6454SAndrew Thompson 				stats = (struct ngudbpstat *)resp->data;
66202ac6454SAndrew Thompson 				mtx_lock(&sc->sc_mtx);
66302ac6454SAndrew Thompson 				stats->packets_in = sc->sc_packets_in;
66402ac6454SAndrew Thompson 				stats->packets_out = sc->sc_packets_out;
66502ac6454SAndrew Thompson 				mtx_unlock(&sc->sc_mtx);
66602ac6454SAndrew Thompson 				break;
66702ac6454SAndrew Thompson 			}
66802ac6454SAndrew Thompson 		case NGM_UDBP_SET_FLAG:
66902ac6454SAndrew Thompson 			if (msg->header.arglen != sizeof(uint32_t)) {
67002ac6454SAndrew Thompson 				error = EINVAL;
67102ac6454SAndrew Thompson 				break;
67202ac6454SAndrew Thompson 			}
67302ac6454SAndrew Thompson 			DPRINTF("flags = 0x%08x\n",
67402ac6454SAndrew Thompson 			    *((uint32_t *)msg->data));
67502ac6454SAndrew Thompson 			break;
67602ac6454SAndrew Thompson 		default:
67702ac6454SAndrew Thompson 			error = EINVAL;	/* unknown command */
67802ac6454SAndrew Thompson 			break;
67902ac6454SAndrew Thompson 		}
68002ac6454SAndrew Thompson 		break;
68102ac6454SAndrew Thompson 	default:
68202ac6454SAndrew Thompson 		error = EINVAL;		/* unknown cookie type */
68302ac6454SAndrew Thompson 		break;
68402ac6454SAndrew Thompson 	}
68502ac6454SAndrew Thompson 
68602ac6454SAndrew Thompson 	/* Take care of synchronous response, if any */
68702ac6454SAndrew Thompson 	NG_RESPOND_MSG(error, node, item, resp);
68802ac6454SAndrew Thompson 	NG_FREE_MSG(msg);
68902ac6454SAndrew Thompson 	return (error);
69002ac6454SAndrew Thompson }
69102ac6454SAndrew Thompson 
69202ac6454SAndrew Thompson /*
69302ac6454SAndrew Thompson  * Accept data from the hook and queue it for output.
69402ac6454SAndrew Thompson  */
69502ac6454SAndrew Thompson static int
69602ac6454SAndrew Thompson ng_udbp_rcvdata(hook_p hook, item_p item)
69702ac6454SAndrew Thompson {
69802ac6454SAndrew Thompson 	struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
69902ac6454SAndrew Thompson 	struct ng_bt_mbufq *queue_ptr;
70002ac6454SAndrew Thompson 	struct mbuf *m;
70102ac6454SAndrew Thompson 	struct ng_tag_prio *ptag;
70202ac6454SAndrew Thompson 	int error;
70302ac6454SAndrew Thompson 
70402ac6454SAndrew Thompson 	if (sc == NULL) {
70502ac6454SAndrew Thompson 		NG_FREE_ITEM(item);
70602ac6454SAndrew Thompson 		return (EHOSTDOWN);
70702ac6454SAndrew Thompson 	}
70802ac6454SAndrew Thompson 	NGI_GET_M(item, m);
70902ac6454SAndrew Thompson 	NG_FREE_ITEM(item);
71002ac6454SAndrew Thompson 
71102ac6454SAndrew Thompson 	/*
71202ac6454SAndrew Thompson 	 * Now queue the data for when it can be sent
71302ac6454SAndrew Thompson 	 */
71402ac6454SAndrew Thompson 	ptag = (void *)m_tag_locate(m, NGM_GENERIC_COOKIE,
71502ac6454SAndrew Thompson 	    NG_TAG_PRIO, NULL);
71602ac6454SAndrew Thompson 
71702ac6454SAndrew Thompson 	if (ptag && (ptag->priority > NG_PRIO_CUTOFF))
71802ac6454SAndrew Thompson 		queue_ptr = &sc->sc_xmitq_hipri;
71902ac6454SAndrew Thompson 	else
72002ac6454SAndrew Thompson 		queue_ptr = &sc->sc_xmitq;
72102ac6454SAndrew Thompson 
72202ac6454SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
72302ac6454SAndrew Thompson 
72402ac6454SAndrew Thompson 	if (NG_BT_MBUFQ_FULL(queue_ptr)) {
72502ac6454SAndrew Thompson 		NG_BT_MBUFQ_DROP(queue_ptr);
72602ac6454SAndrew Thompson 		NG_FREE_M(m);
72702ac6454SAndrew Thompson 		error = ENOBUFS;
72802ac6454SAndrew Thompson 	} else {
72902ac6454SAndrew Thompson 		NG_BT_MBUFQ_ENQUEUE(queue_ptr, m);
73002ac6454SAndrew Thompson 		/*
73102ac6454SAndrew Thompson 		 * start bulk-out transfer, if not already started:
73202ac6454SAndrew Thompson 		 */
733a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_xfer[UDBP_T_WR]);
73402ac6454SAndrew Thompson 		error = 0;
73502ac6454SAndrew Thompson 	}
73602ac6454SAndrew Thompson 
73702ac6454SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
73802ac6454SAndrew Thompson 
73902ac6454SAndrew Thompson 	return (error);
74002ac6454SAndrew Thompson }
74102ac6454SAndrew Thompson 
74202ac6454SAndrew Thompson /*
74302ac6454SAndrew Thompson  * Do local shutdown processing..
74420733245SPedro F. Giffuni  * We are a persistent device, we refuse to go away, and
74502ac6454SAndrew Thompson  * only remove our links and reset ourself.
74602ac6454SAndrew Thompson  */
74702ac6454SAndrew Thompson static int
74802ac6454SAndrew Thompson ng_udbp_rmnode(node_p node)
74902ac6454SAndrew Thompson {
75002ac6454SAndrew Thompson 	struct udbp_softc *sc = NG_NODE_PRIVATE(node);
75102ac6454SAndrew Thompson 
75202ac6454SAndrew Thompson 	/* Let old node go */
75302ac6454SAndrew Thompson 	NG_NODE_SET_PRIVATE(node, NULL);
75402ac6454SAndrew Thompson 	NG_NODE_UNREF(node);		/* forget it ever existed */
75502ac6454SAndrew Thompson 
75602ac6454SAndrew Thompson 	if (sc == NULL) {
75702ac6454SAndrew Thompson 		goto done;
75802ac6454SAndrew Thompson 	}
75902ac6454SAndrew Thompson 	/* Create Netgraph node */
76002ac6454SAndrew Thompson 	if (ng_make_node_common(&ng_udbp_typestruct, &sc->sc_node) != 0) {
76102ac6454SAndrew Thompson 		printf("%s: Could not create Netgraph node\n",
76202ac6454SAndrew Thompson 		    sc->sc_name);
76302ac6454SAndrew Thompson 		sc->sc_node = NULL;
76402ac6454SAndrew Thompson 		goto done;
76502ac6454SAndrew Thompson 	}
76602ac6454SAndrew Thompson 	/* Name node */
76702ac6454SAndrew Thompson 	if (ng_name_node(sc->sc_node, sc->sc_name) != 0) {
76802ac6454SAndrew Thompson 		printf("%s: Could not name Netgraph node\n",
76902ac6454SAndrew Thompson 		    sc->sc_name);
77002ac6454SAndrew Thompson 		NG_NODE_UNREF(sc->sc_node);
77102ac6454SAndrew Thompson 		sc->sc_node = NULL;
77202ac6454SAndrew Thompson 		goto done;
77302ac6454SAndrew Thompson 	}
77402ac6454SAndrew Thompson 	NG_NODE_SET_PRIVATE(sc->sc_node, sc);
77502ac6454SAndrew Thompson 
77602ac6454SAndrew Thompson done:
77702ac6454SAndrew Thompson 	if (sc) {
77802ac6454SAndrew Thompson 		mtx_unlock(&sc->sc_mtx);
77902ac6454SAndrew Thompson 	}
78002ac6454SAndrew Thompson 	return (0);
78102ac6454SAndrew Thompson }
78202ac6454SAndrew Thompson 
78302ac6454SAndrew Thompson /*
78402ac6454SAndrew Thompson  * This is called once we've already connected a new hook to the other node.
78502ac6454SAndrew Thompson  * It gives us a chance to balk at the last minute.
78602ac6454SAndrew Thompson  */
78702ac6454SAndrew Thompson static int
78802ac6454SAndrew Thompson ng_udbp_connect(hook_p hook)
78902ac6454SAndrew Thompson {
79002ac6454SAndrew Thompson 	struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
79102ac6454SAndrew Thompson 
79202ac6454SAndrew Thompson 	/* probably not at splnet, force outward queueing */
79302ac6454SAndrew Thompson 	NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
79402ac6454SAndrew Thompson 
79502ac6454SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
79602ac6454SAndrew Thompson 
79702ac6454SAndrew Thompson 	sc->sc_flags |= (UDBP_FLAG_READ_STALL |
79802ac6454SAndrew Thompson 	    UDBP_FLAG_WRITE_STALL);
79902ac6454SAndrew Thompson 
80002ac6454SAndrew Thompson 	/* start bulk-in transfer */
801a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UDBP_T_RD]);
80202ac6454SAndrew Thompson 
80302ac6454SAndrew Thompson 	/* start bulk-out transfer */
804a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UDBP_T_WR]);
80502ac6454SAndrew Thompson 
80602ac6454SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
80702ac6454SAndrew Thompson 
80802ac6454SAndrew Thompson 	return (0);
80902ac6454SAndrew Thompson }
81002ac6454SAndrew Thompson 
81102ac6454SAndrew Thompson /*
81202ac6454SAndrew Thompson  * Dook disconnection
81302ac6454SAndrew Thompson  *
81402ac6454SAndrew Thompson  * For this type, removal of the last link destroys the node
81502ac6454SAndrew Thompson  */
81602ac6454SAndrew Thompson static int
81702ac6454SAndrew Thompson ng_udbp_disconnect(hook_p hook)
81802ac6454SAndrew Thompson {
81902ac6454SAndrew Thompson 	struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
82002ac6454SAndrew Thompson 	int error = 0;
82102ac6454SAndrew Thompson 
82202ac6454SAndrew Thompson 	if (sc != NULL) {
82302ac6454SAndrew Thompson 		mtx_lock(&sc->sc_mtx);
82402ac6454SAndrew Thompson 
82502ac6454SAndrew Thompson 		if (hook != sc->sc_hook) {
82602ac6454SAndrew Thompson 			error = EINVAL;
82702ac6454SAndrew Thompson 		} else {
82802ac6454SAndrew Thompson 			/* stop bulk-in transfer */
829a593f6b8SAndrew Thompson 			usbd_transfer_stop(sc->sc_xfer[UDBP_T_RD_CS]);
830a593f6b8SAndrew Thompson 			usbd_transfer_stop(sc->sc_xfer[UDBP_T_RD]);
83102ac6454SAndrew Thompson 
83202ac6454SAndrew Thompson 			/* stop bulk-out transfer */
833a593f6b8SAndrew Thompson 			usbd_transfer_stop(sc->sc_xfer[UDBP_T_WR_CS]);
834a593f6b8SAndrew Thompson 			usbd_transfer_stop(sc->sc_xfer[UDBP_T_WR]);
83502ac6454SAndrew Thompson 
83602ac6454SAndrew Thompson 			/* cleanup queues */
83702ac6454SAndrew Thompson 			NG_BT_MBUFQ_DRAIN(&sc->sc_xmitq);
83802ac6454SAndrew Thompson 			NG_BT_MBUFQ_DRAIN(&sc->sc_xmitq_hipri);
83902ac6454SAndrew Thompson 
84002ac6454SAndrew Thompson 			if (sc->sc_bulk_in_buffer) {
84102ac6454SAndrew Thompson 				m_freem(sc->sc_bulk_in_buffer);
84202ac6454SAndrew Thompson 				sc->sc_bulk_in_buffer = NULL;
84302ac6454SAndrew Thompson 			}
84402ac6454SAndrew Thompson 			sc->sc_hook = NULL;
84502ac6454SAndrew Thompson 		}
84602ac6454SAndrew Thompson 
84702ac6454SAndrew Thompson 		mtx_unlock(&sc->sc_mtx);
84802ac6454SAndrew Thompson 	}
84902ac6454SAndrew Thompson 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
85002ac6454SAndrew Thompson 	    && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
85102ac6454SAndrew Thompson 		ng_rmnode_self(NG_HOOK_NODE(hook));
85202ac6454SAndrew Thompson 
85302ac6454SAndrew Thompson 	return (error);
85402ac6454SAndrew Thompson }
855