xref: /freebsd/sys/dev/usb/misc/udbp.c (revision b3aaa0cc21c63d388230c7ef2a80abd631ff20d5)
1 /*-
2  * Copyright (c) 1996-2000 Whistle Communications, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of author nor the names of its
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY NICK HIBMA AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 /* Driver for arbitrary double bulk pipe devices.
35  * The driver assumes that there will be the same driver on the other side.
36  *
37  * XXX Some more information on what the framing of the IP packets looks like.
38  *
39  * To take full advantage of bulk transmission, packets should be chosen
40  * between 1k and 5k in size (1k to make sure the sending side starts
41  * streaming, and <5k to avoid overflowing the system with small TDs).
42  */
43 
44 
45 /* probe/attach/detach:
46  *  Connect the driver to the hardware and netgraph
47  *
48  *  The reason we submit a bulk in transfer is that USB does not know about
49  *  interrupts. The bulk transfer continuously polls the device for data.
50  *  While the device has no data available, the device NAKs the TDs. As soon
51  *  as there is data, the transfer happens and the data comes flowing in.
52  *
53  *  In case you were wondering, interrupt transfers happen exactly that way.
54  *  It therefore doesn't make sense to use the interrupt pipe to signal
55  *  'data ready' and then schedule a bulk transfer to fetch it. That would
56  *  incur a 2ms delay at least, without reducing bandwidth requirements.
57  *
58  */
59 
60 #include "usbdevs.h"
61 #include <dev/usb/usb.h>
62 #include <dev/usb/usb_mfunc.h>
63 #include <dev/usb/usb_error.h>
64 
65 #define	USB_DEBUG_VAR udbp_debug
66 
67 #include <dev/usb/usb_core.h>
68 #include <dev/usb/usb_debug.h>
69 #include <dev/usb/usb_parse.h>
70 #include <dev/usb/usb_lookup.h>
71 #include <dev/usb/usb_util.h>
72 #include <dev/usb/usb_busdma.h>
73 
74 #include <sys/mbuf.h>
75 
76 #include <netgraph/ng_message.h>
77 #include <netgraph/netgraph.h>
78 #include <netgraph/ng_parse.h>
79 #include <netgraph/bluetooth/include/ng_bluetooth.h>
80 
81 #include <dev/usb/misc/udbp.h>
82 
83 #if USB_DEBUG
84 static int udbp_debug = 0;
85 
86 SYSCTL_NODE(_hw_usb2, OID_AUTO, udbp, CTLFLAG_RW, 0, "USB udbp");
87 SYSCTL_INT(_hw_usb2_udbp, OID_AUTO, debug, CTLFLAG_RW,
88     &udbp_debug, 0, "udbp debug level");
89 #endif
90 
91 #define	UDBP_TIMEOUT	2000		/* timeout on outbound transfers, in
92 					 * msecs */
93 #define	UDBP_BUFFERSIZE	MCLBYTES	/* maximum number of bytes in one
94 					 * transfer */
95 #define	UDBP_T_WR       0
96 #define	UDBP_T_RD       1
97 #define	UDBP_T_WR_CS    2
98 #define	UDBP_T_RD_CS    3
99 #define	UDBP_T_MAX      4
100 #define	UDBP_Q_MAXLEN   50
101 
102 struct udbp_softc {
103 
104 	struct mtx sc_mtx;
105 	struct ng_bt_mbufq sc_xmitq_hipri;	/* hi-priority transmit queue */
106 	struct ng_bt_mbufq sc_xmitq;	/* low-priority transmit queue */
107 
108 	struct usb2_xfer *sc_xfer[UDBP_T_MAX];
109 	node_p	sc_node;		/* back pointer to node */
110 	hook_p	sc_hook;		/* pointer to the hook */
111 	struct mbuf *sc_bulk_in_buffer;
112 
113 	uint32_t sc_packets_in;		/* packets in from downstream */
114 	uint32_t sc_packets_out;	/* packets out towards downstream */
115 
116 	uint8_t	sc_flags;
117 #define	UDBP_FLAG_READ_STALL    0x01	/* read transfer stalled */
118 #define	UDBP_FLAG_WRITE_STALL   0x02	/* write transfer stalled */
119 
120 	uint8_t	sc_name[16];
121 };
122 
123 /* prototypes */
124 
125 static int udbp_modload(module_t mod, int event, void *data);
126 
127 static device_probe_t udbp_probe;
128 static device_attach_t udbp_attach;
129 static device_detach_t udbp_detach;
130 
131 static usb2_callback_t udbp_bulk_read_callback;
132 static usb2_callback_t udbp_bulk_read_clear_stall_callback;
133 static usb2_callback_t udbp_bulk_write_callback;
134 static usb2_callback_t udbp_bulk_write_clear_stall_callback;
135 
136 static void	udbp_bulk_read_complete(node_p, hook_p, void *, int);
137 
138 static ng_constructor_t	ng_udbp_constructor;
139 static ng_rcvmsg_t	ng_udbp_rcvmsg;
140 static ng_shutdown_t	ng_udbp_rmnode;
141 static ng_newhook_t	ng_udbp_newhook;
142 static ng_connect_t	ng_udbp_connect;
143 static ng_rcvdata_t	ng_udbp_rcvdata;
144 static ng_disconnect_t	ng_udbp_disconnect;
145 
146 /* Parse type for struct ngudbpstat */
147 static const struct ng_parse_struct_field
148 	ng_udbp_stat_type_fields[] = NG_UDBP_STATS_TYPE_INFO;
149 
150 static const struct ng_parse_type ng_udbp_stat_type = {
151 	&ng_parse_struct_type,
152 	&ng_udbp_stat_type_fields
153 };
154 
155 /* List of commands and how to convert arguments to/from ASCII */
156 static const struct ng_cmdlist ng_udbp_cmdlist[] = {
157 	{
158 		NGM_UDBP_COOKIE,
159 		NGM_UDBP_GET_STATUS,
160 		"getstatus",
161 		NULL,
162 		&ng_udbp_stat_type,
163 	},
164 	{
165 		NGM_UDBP_COOKIE,
166 		NGM_UDBP_SET_FLAG,
167 		"setflag",
168 		&ng_parse_int32_type,
169 		NULL
170 	},
171 	{0}
172 };
173 
174 /* Netgraph node type descriptor */
175 static struct ng_type ng_udbp_typestruct = {
176 	.version = NG_ABI_VERSION,
177 	.name = NG_UDBP_NODE_TYPE,
178 	.constructor = ng_udbp_constructor,
179 	.rcvmsg = ng_udbp_rcvmsg,
180 	.shutdown = ng_udbp_rmnode,
181 	.newhook = ng_udbp_newhook,
182 	.connect = ng_udbp_connect,
183 	.rcvdata = ng_udbp_rcvdata,
184 	.disconnect = ng_udbp_disconnect,
185 	.cmdlist = ng_udbp_cmdlist,
186 };
187 
188 /* USB config */
189 static const struct usb2_config udbp_config[UDBP_T_MAX] = {
190 
191 	[UDBP_T_WR] = {
192 		.type = UE_BULK,
193 		.endpoint = UE_ADDR_ANY,
194 		.direction = UE_DIR_OUT,
195 		.mh.bufsize = UDBP_BUFFERSIZE,
196 		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
197 		.mh.callback = &udbp_bulk_write_callback,
198 		.mh.timeout = UDBP_TIMEOUT,
199 	},
200 
201 	[UDBP_T_RD] = {
202 		.type = UE_BULK,
203 		.endpoint = UE_ADDR_ANY,
204 		.direction = UE_DIR_IN,
205 		.mh.bufsize = UDBP_BUFFERSIZE,
206 		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
207 		.mh.callback = &udbp_bulk_read_callback,
208 	},
209 
210 	[UDBP_T_WR_CS] = {
211 		.type = UE_CONTROL,
212 		.endpoint = 0x00,	/* Control pipe */
213 		.direction = UE_DIR_ANY,
214 		.mh.bufsize = sizeof(struct usb2_device_request),
215 		.mh.flags = {},
216 		.mh.callback = &udbp_bulk_write_clear_stall_callback,
217 		.mh.timeout = 1000,	/* 1 second */
218 		.mh.interval = 50,	/* 50ms */
219 	},
220 
221 	[UDBP_T_RD_CS] = {
222 		.type = UE_CONTROL,
223 		.endpoint = 0x00,	/* Control pipe */
224 		.direction = UE_DIR_ANY,
225 		.mh.bufsize = sizeof(struct usb2_device_request),
226 		.mh.flags = {},
227 		.mh.callback = &udbp_bulk_read_clear_stall_callback,
228 		.mh.timeout = 1000,	/* 1 second */
229 		.mh.interval = 50,	/* 50ms */
230 	},
231 };
232 
233 static devclass_t udbp_devclass;
234 
235 static device_method_t udbp_methods[] = {
236 	/* Device interface */
237 	DEVMETHOD(device_probe, udbp_probe),
238 	DEVMETHOD(device_attach, udbp_attach),
239 	DEVMETHOD(device_detach, udbp_detach),
240 	{0, 0}
241 };
242 
243 static driver_t udbp_driver = {
244 	.name = "udbp",
245 	.methods = udbp_methods,
246 	.size = sizeof(struct udbp_softc),
247 };
248 
249 DRIVER_MODULE(udbp, ushub, udbp_driver, udbp_devclass, udbp_modload, 0);
250 MODULE_DEPEND(udbp, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
251 MODULE_DEPEND(udbp, usb, 1, 1, 1);
252 
253 static int
254 udbp_modload(module_t mod, int event, void *data)
255 {
256 	int error;
257 
258 	switch (event) {
259 	case MOD_LOAD:
260 		error = ng_newtype(&ng_udbp_typestruct);
261 		if (error != 0) {
262 			printf("%s: Could not register "
263 			    "Netgraph node type, error=%d\n",
264 			    NG_UDBP_NODE_TYPE, error);
265 		}
266 		break;
267 
268 	case MOD_UNLOAD:
269 		error = ng_rmtype(&ng_udbp_typestruct);
270 		break;
271 
272 	default:
273 		error = EOPNOTSUPP;
274 		break;
275 	}
276 	return (error);
277 }
278 
279 static int
280 udbp_probe(device_t dev)
281 {
282 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
283 
284 	if (uaa->usb2_mode != USB_MODE_HOST) {
285 		return (ENXIO);
286 	}
287 	/*
288 	 * XXX Julian, add the id of the device if you have one to test
289 	 * things with. run 'usbdevs -v' and note the 3 ID's that appear.
290 	 * The Vendor Id and Product Id are in hex and the Revision Id is in
291 	 * bcd. But as usual if the revision is 0x101 then you should
292 	 * compare the revision id in the device descriptor with 0x101 Or go
293 	 * search the file usbdevs.h. Maybe the device is already in there.
294 	 */
295 	if (((uaa->info.idVendor == USB_VENDOR_NETCHIP) &&
296 	    (uaa->info.idProduct == USB_PRODUCT_NETCHIP_TURBOCONNECT)))
297 		return (0);
298 
299 	if (((uaa->info.idVendor == USB_VENDOR_PROLIFIC) &&
300 	    ((uaa->info.idProduct == USB_PRODUCT_PROLIFIC_PL2301) ||
301 	    (uaa->info.idProduct == USB_PRODUCT_PROLIFIC_PL2302))))
302 		return (0);
303 
304 	if ((uaa->info.idVendor == USB_VENDOR_ANCHOR) &&
305 	    (uaa->info.idProduct == USB_PRODUCT_ANCHOR_EZLINK))
306 		return (0);
307 
308 	if ((uaa->info.idVendor == USB_VENDOR_GENESYS) &&
309 	    (uaa->info.idProduct == USB_PRODUCT_GENESYS_GL620USB))
310 		return (0);
311 
312 	return (ENXIO);
313 }
314 
315 static int
316 udbp_attach(device_t dev)
317 {
318 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
319 	struct udbp_softc *sc = device_get_softc(dev);
320 	int error;
321 
322 	device_set_usb2_desc(dev);
323 
324 	snprintf(sc->sc_name, sizeof(sc->sc_name),
325 	    "%s", device_get_nameunit(dev));
326 
327 	mtx_init(&sc->sc_mtx, "udbp lock", NULL, MTX_DEF | MTX_RECURSE);
328 
329 	error = usb2_transfer_setup(uaa->device, &uaa->info.bIfaceIndex,
330 	    sc->sc_xfer, udbp_config, UDBP_T_MAX, sc, &sc->sc_mtx);
331 	if (error) {
332 		DPRINTF("error=%s\n", usb2_errstr(error));
333 		goto detach;
334 	}
335 	NG_BT_MBUFQ_INIT(&sc->sc_xmitq, UDBP_Q_MAXLEN);
336 
337 	NG_BT_MBUFQ_INIT(&sc->sc_xmitq_hipri, UDBP_Q_MAXLEN);
338 
339 	/* create Netgraph node */
340 
341 	if (ng_make_node_common(&ng_udbp_typestruct, &sc->sc_node) != 0) {
342 		printf("%s: Could not create Netgraph node\n",
343 		    sc->sc_name);
344 		sc->sc_node = NULL;
345 		goto detach;
346 	}
347 	/* name node */
348 
349 	if (ng_name_node(sc->sc_node, sc->sc_name) != 0) {
350 		printf("%s: Could not name node\n",
351 		    sc->sc_name);
352 		NG_NODE_UNREF(sc->sc_node);
353 		sc->sc_node = NULL;
354 		goto detach;
355 	}
356 	NG_NODE_SET_PRIVATE(sc->sc_node, sc);
357 
358 	/* the device is now operational */
359 
360 	return (0);			/* success */
361 
362 detach:
363 	udbp_detach(dev);
364 	return (ENOMEM);		/* failure */
365 }
366 
367 static int
368 udbp_detach(device_t dev)
369 {
370 	struct udbp_softc *sc = device_get_softc(dev);
371 
372 	/* destroy Netgraph node */
373 
374 	if (sc->sc_node != NULL) {
375 		NG_NODE_SET_PRIVATE(sc->sc_node, NULL);
376 		ng_rmnode_self(sc->sc_node);
377 		sc->sc_node = NULL;
378 	}
379 	/* free USB transfers, if any */
380 
381 	usb2_transfer_unsetup(sc->sc_xfer, UDBP_T_MAX);
382 
383 	mtx_destroy(&sc->sc_mtx);
384 
385 	/* destroy queues */
386 
387 	NG_BT_MBUFQ_DESTROY(&sc->sc_xmitq);
388 	NG_BT_MBUFQ_DESTROY(&sc->sc_xmitq_hipri);
389 
390 	/* extra check */
391 
392 	if (sc->sc_bulk_in_buffer) {
393 		m_freem(sc->sc_bulk_in_buffer);
394 		sc->sc_bulk_in_buffer = NULL;
395 	}
396 	return (0);			/* success */
397 }
398 
399 static void
400 udbp_bulk_read_callback(struct usb2_xfer *xfer)
401 {
402 	struct udbp_softc *sc = xfer->priv_sc;
403 	struct mbuf *m;
404 
405 	switch (USB_GET_STATE(xfer)) {
406 	case USB_ST_TRANSFERRED:
407 
408 		/* allocate new mbuf */
409 
410 		MGETHDR(m, M_DONTWAIT, MT_DATA);
411 
412 		if (m == NULL) {
413 			goto tr_setup;
414 		}
415 		MCLGET(m, M_DONTWAIT);
416 
417 		if (!(m->m_flags & M_EXT)) {
418 			m_freem(m);
419 			goto tr_setup;
420 		}
421 		m->m_pkthdr.len = m->m_len = xfer->actlen;
422 
423 		usb2_copy_out(xfer->frbuffers, 0, m->m_data, xfer->actlen);
424 
425 		sc->sc_bulk_in_buffer = m;
426 
427 		DPRINTF("received package %d "
428 		    "bytes\n", xfer->actlen);
429 
430 	case USB_ST_SETUP:
431 tr_setup:
432 		if (sc->sc_bulk_in_buffer) {
433 			ng_send_fn(sc->sc_node, NULL, &udbp_bulk_read_complete, NULL, 0);
434 			return;
435 		}
436 		if (sc->sc_flags & UDBP_FLAG_READ_STALL) {
437 			usb2_transfer_start(sc->sc_xfer[UDBP_T_RD_CS]);
438 			return;
439 		}
440 		xfer->frlengths[0] = xfer->max_data_length;
441 		usb2_start_hardware(xfer);
442 		return;
443 
444 	default:			/* Error */
445 		if (xfer->error != USB_ERR_CANCELLED) {
446 			/* try to clear stall first */
447 			sc->sc_flags |= UDBP_FLAG_READ_STALL;
448 			usb2_transfer_start(sc->sc_xfer[UDBP_T_RD_CS]);
449 		}
450 		return;
451 
452 	}
453 }
454 
455 static void
456 udbp_bulk_read_clear_stall_callback(struct usb2_xfer *xfer)
457 {
458 	struct udbp_softc *sc = xfer->priv_sc;
459 	struct usb2_xfer *xfer_other = sc->sc_xfer[UDBP_T_RD];
460 
461 	if (usb2_clear_stall_callback(xfer, xfer_other)) {
462 		DPRINTF("stall cleared\n");
463 		sc->sc_flags &= ~UDBP_FLAG_READ_STALL;
464 		usb2_transfer_start(xfer_other);
465 	}
466 }
467 
468 static void
469 udbp_bulk_read_complete(node_p node, hook_p hook, void *arg1, int arg2)
470 {
471 	struct udbp_softc *sc = NG_NODE_PRIVATE(node);
472 	struct mbuf *m;
473 	int error;
474 
475 	if (sc == NULL) {
476 		return;
477 	}
478 	mtx_lock(&sc->sc_mtx);
479 
480 	m = sc->sc_bulk_in_buffer;
481 
482 	if (m) {
483 
484 		sc->sc_bulk_in_buffer = NULL;
485 
486 		if ((sc->sc_hook == NULL) ||
487 		    NG_HOOK_NOT_VALID(sc->sc_hook)) {
488 			DPRINTF("No upstream hook\n");
489 			goto done;
490 		}
491 		sc->sc_packets_in++;
492 
493 		NG_SEND_DATA_ONLY(error, sc->sc_hook, m);
494 
495 		m = NULL;
496 	}
497 done:
498 	if (m) {
499 		m_freem(m);
500 	}
501 	/* start USB bulk-in transfer, if not already started */
502 
503 	usb2_transfer_start(sc->sc_xfer[UDBP_T_RD]);
504 
505 	mtx_unlock(&sc->sc_mtx);
506 }
507 
508 static void
509 udbp_bulk_write_callback(struct usb2_xfer *xfer)
510 {
511 	struct udbp_softc *sc = xfer->priv_sc;
512 	struct mbuf *m;
513 
514 	switch (USB_GET_STATE(xfer)) {
515 	case USB_ST_TRANSFERRED:
516 
517 		sc->sc_packets_out++;
518 
519 	case USB_ST_SETUP:
520 		if (sc->sc_flags & UDBP_FLAG_WRITE_STALL) {
521 			usb2_transfer_start(sc->sc_xfer[UDBP_T_WR_CS]);
522 			return;
523 		}
524 		/* get next mbuf, if any */
525 
526 		NG_BT_MBUFQ_DEQUEUE(&sc->sc_xmitq_hipri, m);
527 		if (m == NULL) {
528 			NG_BT_MBUFQ_DEQUEUE(&sc->sc_xmitq, m);
529 			if (m == NULL) {
530 				DPRINTF("Data queue is empty\n");
531 				return;
532 			}
533 		}
534 		if (m->m_pkthdr.len > MCLBYTES) {
535 			DPRINTF("truncating large packet "
536 			    "from %d to %d bytes\n", m->m_pkthdr.len,
537 			    MCLBYTES);
538 			m->m_pkthdr.len = MCLBYTES;
539 		}
540 		usb2_m_copy_in(xfer->frbuffers, 0, m, 0, m->m_pkthdr.len);
541 
542 		xfer->frlengths[0] = m->m_pkthdr.len;
543 
544 		m_freem(m);
545 
546 		DPRINTF("packet out: %d bytes\n",
547 		    xfer->frlengths[0]);
548 
549 		usb2_start_hardware(xfer);
550 		return;
551 
552 	default:			/* Error */
553 		if (xfer->error != USB_ERR_CANCELLED) {
554 			/* try to clear stall first */
555 			sc->sc_flags |= UDBP_FLAG_WRITE_STALL;
556 			usb2_transfer_start(sc->sc_xfer[UDBP_T_WR_CS]);
557 		}
558 		return;
559 
560 	}
561 }
562 
563 static void
564 udbp_bulk_write_clear_stall_callback(struct usb2_xfer *xfer)
565 {
566 	struct udbp_softc *sc = xfer->priv_sc;
567 	struct usb2_xfer *xfer_other = sc->sc_xfer[UDBP_T_WR];
568 
569 	if (usb2_clear_stall_callback(xfer, xfer_other)) {
570 		DPRINTF("stall cleared\n");
571 		sc->sc_flags &= ~UDBP_FLAG_WRITE_STALL;
572 		usb2_transfer_start(xfer_other);
573 	}
574 }
575 
576 /***********************************************************************
577  * Start of Netgraph methods
578  **********************************************************************/
579 
580 /*
581  * If this is a device node so this work is done in the attach()
582  * routine and the constructor will return EINVAL as you should not be able
583  * to create nodes that depend on hardware (unless you can add the hardware :)
584  */
585 static int
586 ng_udbp_constructor(node_p node)
587 {
588 	return (EINVAL);
589 }
590 
591 /*
592  * Give our ok for a hook to be added...
593  * If we are not running this might kick a device into life.
594  * Possibly decode information out of the hook name.
595  * Add the hook's private info to the hook structure.
596  * (if we had some). In this example, we assume that there is a
597  * an array of structs, called 'channel' in the private info,
598  * one for each active channel. The private
599  * pointer of each hook points to the appropriate UDBP_hookinfo struct
600  * so that the source of an input packet is easily identified.
601  */
602 static int
603 ng_udbp_newhook(node_p node, hook_p hook, const char *name)
604 {
605 	struct udbp_softc *sc = NG_NODE_PRIVATE(node);
606 	int32_t error = 0;
607 
608 	if (strcmp(name, NG_UDBP_HOOK_NAME)) {
609 		return (EINVAL);
610 	}
611 	mtx_lock(&sc->sc_mtx);
612 
613 	if (sc->sc_hook != NULL) {
614 		error = EISCONN;
615 	} else {
616 		sc->sc_hook = hook;
617 		NG_HOOK_SET_PRIVATE(hook, NULL);
618 	}
619 
620 	mtx_unlock(&sc->sc_mtx);
621 
622 	return (error);
623 }
624 
625 /*
626  * Get a netgraph control message.
627  * Check it is one we understand. If needed, send a response.
628  * We could save the address for an async action later, but don't here.
629  * Always free the message.
630  * The response should be in a malloc'd region that the caller can 'free'.
631  * A response is not required.
632  * Theoretically you could respond defferently to old message types if
633  * the cookie in the header didn't match what we consider to be current
634  * (so that old userland programs could continue to work).
635  */
636 static int
637 ng_udbp_rcvmsg(node_p node, item_p item, hook_p lasthook)
638 {
639 	struct udbp_softc *sc = NG_NODE_PRIVATE(node);
640 	struct ng_mesg *resp = NULL;
641 	int error = 0;
642 	struct ng_mesg *msg;
643 
644 	NGI_GET_MSG(item, msg);
645 	/* Deal with message according to cookie and command */
646 	switch (msg->header.typecookie) {
647 	case NGM_UDBP_COOKIE:
648 		switch (msg->header.cmd) {
649 		case NGM_UDBP_GET_STATUS:
650 			{
651 				struct ngudbpstat *stats;
652 
653 				NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
654 				if (!resp) {
655 					error = ENOMEM;
656 					break;
657 				}
658 				stats = (struct ngudbpstat *)resp->data;
659 				mtx_lock(&sc->sc_mtx);
660 				stats->packets_in = sc->sc_packets_in;
661 				stats->packets_out = sc->sc_packets_out;
662 				mtx_unlock(&sc->sc_mtx);
663 				break;
664 			}
665 		case NGM_UDBP_SET_FLAG:
666 			if (msg->header.arglen != sizeof(uint32_t)) {
667 				error = EINVAL;
668 				break;
669 			}
670 			DPRINTF("flags = 0x%08x\n",
671 			    *((uint32_t *)msg->data));
672 			break;
673 		default:
674 			error = EINVAL;	/* unknown command */
675 			break;
676 		}
677 		break;
678 	default:
679 		error = EINVAL;		/* unknown cookie type */
680 		break;
681 	}
682 
683 	/* Take care of synchronous response, if any */
684 	NG_RESPOND_MSG(error, node, item, resp);
685 	NG_FREE_MSG(msg);
686 	return (error);
687 }
688 
689 /*
690  * Accept data from the hook and queue it for output.
691  */
692 static int
693 ng_udbp_rcvdata(hook_p hook, item_p item)
694 {
695 	struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
696 	struct ng_bt_mbufq *queue_ptr;
697 	struct mbuf *m;
698 	struct ng_tag_prio *ptag;
699 	int error;
700 
701 	if (sc == NULL) {
702 		NG_FREE_ITEM(item);
703 		return (EHOSTDOWN);
704 	}
705 	NGI_GET_M(item, m);
706 	NG_FREE_ITEM(item);
707 
708 	/*
709 	 * Now queue the data for when it can be sent
710 	 */
711 	ptag = (void *)m_tag_locate(m, NGM_GENERIC_COOKIE,
712 	    NG_TAG_PRIO, NULL);
713 
714 	if (ptag && (ptag->priority > NG_PRIO_CUTOFF))
715 		queue_ptr = &sc->sc_xmitq_hipri;
716 	else
717 		queue_ptr = &sc->sc_xmitq;
718 
719 	mtx_lock(&sc->sc_mtx);
720 
721 	if (NG_BT_MBUFQ_FULL(queue_ptr)) {
722 		NG_BT_MBUFQ_DROP(queue_ptr);
723 		NG_FREE_M(m);
724 		error = ENOBUFS;
725 	} else {
726 		NG_BT_MBUFQ_ENQUEUE(queue_ptr, m);
727 		/*
728 		 * start bulk-out transfer, if not already started:
729 		 */
730 		usb2_transfer_start(sc->sc_xfer[UDBP_T_WR]);
731 		error = 0;
732 	}
733 
734 	mtx_unlock(&sc->sc_mtx);
735 
736 	return (error);
737 }
738 
739 /*
740  * Do local shutdown processing..
741  * We are a persistant device, we refuse to go away, and
742  * only remove our links and reset ourself.
743  */
744 static int
745 ng_udbp_rmnode(node_p node)
746 {
747 	struct udbp_softc *sc = NG_NODE_PRIVATE(node);
748 
749 	/* Let old node go */
750 	NG_NODE_SET_PRIVATE(node, NULL);
751 	NG_NODE_UNREF(node);		/* forget it ever existed */
752 
753 	if (sc == NULL) {
754 		goto done;
755 	}
756 	/* Create Netgraph node */
757 	if (ng_make_node_common(&ng_udbp_typestruct, &sc->sc_node) != 0) {
758 		printf("%s: Could not create Netgraph node\n",
759 		    sc->sc_name);
760 		sc->sc_node = NULL;
761 		goto done;
762 	}
763 	/* Name node */
764 	if (ng_name_node(sc->sc_node, sc->sc_name) != 0) {
765 		printf("%s: Could not name Netgraph node\n",
766 		    sc->sc_name);
767 		NG_NODE_UNREF(sc->sc_node);
768 		sc->sc_node = NULL;
769 		goto done;
770 	}
771 	NG_NODE_SET_PRIVATE(sc->sc_node, sc);
772 
773 done:
774 	if (sc) {
775 		mtx_unlock(&sc->sc_mtx);
776 	}
777 	return (0);
778 }
779 
780 /*
781  * This is called once we've already connected a new hook to the other node.
782  * It gives us a chance to balk at the last minute.
783  */
784 static int
785 ng_udbp_connect(hook_p hook)
786 {
787 	struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
788 
789 	/* probably not at splnet, force outward queueing */
790 	NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
791 
792 	mtx_lock(&sc->sc_mtx);
793 
794 	sc->sc_flags |= (UDBP_FLAG_READ_STALL |
795 	    UDBP_FLAG_WRITE_STALL);
796 
797 	/* start bulk-in transfer */
798 	usb2_transfer_start(sc->sc_xfer[UDBP_T_RD]);
799 
800 	/* start bulk-out transfer */
801 	usb2_transfer_start(sc->sc_xfer[UDBP_T_WR]);
802 
803 	mtx_unlock(&sc->sc_mtx);
804 
805 	return (0);
806 }
807 
808 /*
809  * Dook disconnection
810  *
811  * For this type, removal of the last link destroys the node
812  */
813 static int
814 ng_udbp_disconnect(hook_p hook)
815 {
816 	struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
817 	int error = 0;
818 
819 	if (sc != NULL) {
820 
821 		mtx_lock(&sc->sc_mtx);
822 
823 		if (hook != sc->sc_hook) {
824 			error = EINVAL;
825 		} else {
826 
827 			/* stop bulk-in transfer */
828 			usb2_transfer_stop(sc->sc_xfer[UDBP_T_RD_CS]);
829 			usb2_transfer_stop(sc->sc_xfer[UDBP_T_RD]);
830 
831 			/* stop bulk-out transfer */
832 			usb2_transfer_stop(sc->sc_xfer[UDBP_T_WR_CS]);
833 			usb2_transfer_stop(sc->sc_xfer[UDBP_T_WR]);
834 
835 			/* cleanup queues */
836 			NG_BT_MBUFQ_DRAIN(&sc->sc_xmitq);
837 			NG_BT_MBUFQ_DRAIN(&sc->sc_xmitq_hipri);
838 
839 			if (sc->sc_bulk_in_buffer) {
840 				m_freem(sc->sc_bulk_in_buffer);
841 				sc->sc_bulk_in_buffer = NULL;
842 			}
843 			sc->sc_hook = NULL;
844 		}
845 
846 		mtx_unlock(&sc->sc_mtx);
847 	}
848 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
849 	    && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
850 		ng_rmnode_self(NG_HOOK_NODE(hook));
851 
852 	return (error);
853 }
854