1 /*
2  * ng_ubt.c
3  */
4 
5 /*-
6  * Copyright (c) 2001-2009 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $Id: ng_ubt.c,v 1.16 2003/10/10 19:15:06 max Exp $
31  * $FreeBSD$
32  */
33 
34 /*
35  * NOTE: ng_ubt2 driver has a split personality. On one side it is
36  * a USB device driver and on the other it is a Netgraph node. This
37  * driver will *NOT* create traditional /dev/ enties, only Netgraph
38  * node.
39  *
40  * NOTE ON LOCKS USED: ng_ubt2 drives uses 2 locks (mutexes)
41  *
42  * 1) sc_if_mtx - lock for device's interface #0 and #1. This lock is used
43  *    by USB for any USB request going over device's interface #0 and #1,
44  *    i.e. interrupt, control, bulk and isoc. transfers.
45  *
46  * 2) sc_ng_mtx - this lock is used to protect shared (between USB, Netgraph
47  *    and Taskqueue) data, such as outgoing mbuf queues, task flags and hook
48  *    pointer. This lock *SHOULD NOT* be grabbed for a long time. In fact,
49  *    think of it as a spin lock.
50  *
51  * NOTE ON LOCKING STRATEGY: ng_ubt2 driver operates in 3 different contexts.
52  *
53  * 1) USB context. This is where all the USB related stuff happens. All
54  *    callbacks run in this context. All callbacks are called (by USB) with
55  *    appropriate interface lock held. It is (generally) allowed to grab
56  *    any additional locks.
57  *
58  * 2) Netgraph context. This is where all the Netgraph related stuff happens.
59  *    Since we mark node as WRITER, the Netgraph node will be "locked" (from
60  *    Netgraph point of view). Any variable that is only modified from the
61  *    Netgraph context does not require any additonal locking. It is generally
62  *    *NOT* allowed to grab *ANY* additional locks. Whatever you do, *DO NOT*
63  *    grab any lock in the Netgraph context that could cause de-scheduling of
64  *    the Netgraph thread for significant amount of time. In fact, the only
65  *    lock that is allowed in the Netgraph context is the sc_ng_mtx lock.
66  *    Also make sure that any code that is called from the Netgraph context
67  *    follows the rule above.
68  *
69  * 3) Taskqueue context. This is where ubt_task runs. Since we are generally
70  *    NOT allowed to grab any lock that could cause de-scheduling in the
71  *    Netgraph context, and, USB requires us to grab interface lock before
72  *    doing things with transfers, it is safer to transition from the Netgraph
73  *    context to the Taskqueue context before we can call into USB subsystem.
74  *
75  * So, to put everything together, the rules are as follows.
76  *	It is OK to call from the USB context or the Taskqueue context into
77  * the Netgraph context (i.e. call NG_SEND_xxx functions). In other words
78  * it is allowed to call into the Netgraph context with locks held.
79  *	Is it *NOT* OK to call from the Netgraph context into the USB context,
80  * because USB requires us to grab interface locks, and, it is safer to
81  * avoid it. So, to make things safer we set task flags to indicate which
82  * actions we want to perform and schedule ubt_task which would run in the
83  * Taskqueue context.
84  *	Is is OK to call from the Taskqueue context into the USB context,
85  * and, ubt_task does just that (i.e. grabs appropriate interface locks
86  * before calling into USB).
87  *	Access to the outgoing queues, task flags and hook pointer is
88  * controlled by the sc_ng_mtx lock. It is an unavoidable evil. Again,
89  * sc_ng_mtx should really be a spin lock (and it is very likely to an
90  * equivalent of spin lock due to adaptive nature of FreeBSD mutexes).
91  *	All USB callbacks accept softc pointer as a private data. USB ensures
92  * that this pointer is valid.
93  */
94 
95 #include <sys/stdint.h>
96 #include <sys/stddef.h>
97 #include <sys/param.h>
98 #include <sys/queue.h>
99 #include <sys/types.h>
100 #include <sys/systm.h>
101 #include <sys/kernel.h>
102 #include <sys/bus.h>
103 #include <sys/module.h>
104 #include <sys/lock.h>
105 #include <sys/mutex.h>
106 #include <sys/condvar.h>
107 #include <sys/sysctl.h>
108 #include <sys/sx.h>
109 #include <sys/unistd.h>
110 #include <sys/callout.h>
111 #include <sys/malloc.h>
112 #include <sys/priv.h>
113 
114 #include "usbdevs.h"
115 #include <dev/usb/usb.h>
116 #include <dev/usb/usbdi.h>
117 #include <dev/usb/usbdi_util.h>
118 
119 #define	USB_DEBUG_VAR usb_debug
120 #include <dev/usb/usb_debug.h>
121 #include <dev/usb/usb_busdma.h>
122 
123 #include <sys/mbuf.h>
124 #include <sys/taskqueue.h>
125 
126 #include <netgraph/ng_message.h>
127 #include <netgraph/netgraph.h>
128 #include <netgraph/ng_parse.h>
129 #include <netgraph/bluetooth/include/ng_bluetooth.h>
130 #include <netgraph/bluetooth/include/ng_hci.h>
131 #include <netgraph/bluetooth/include/ng_ubt.h>
132 #include <netgraph/bluetooth/drivers/ubt/ng_ubt_var.h>
133 
134 static int		ubt_modevent(module_t, int, void *);
135 static device_probe_t	ubt_probe;
136 static device_attach_t	ubt_attach;
137 static device_detach_t	ubt_detach;
138 
139 static void		ubt_task_schedule(ubt_softc_p, int);
140 static task_fn_t	ubt_task;
141 
142 #define	ubt_xfer_start(sc, i)	usbd_transfer_start((sc)->sc_xfer[(i)])
143 
144 /* Netgraph methods */
145 static ng_constructor_t	ng_ubt_constructor;
146 static ng_shutdown_t	ng_ubt_shutdown;
147 static ng_newhook_t	ng_ubt_newhook;
148 static ng_connect_t	ng_ubt_connect;
149 static ng_disconnect_t	ng_ubt_disconnect;
150 static ng_rcvmsg_t	ng_ubt_rcvmsg;
151 static ng_rcvdata_t	ng_ubt_rcvdata;
152 
153 /* Queue length */
154 static const struct ng_parse_struct_field	ng_ubt_node_qlen_type_fields[] =
155 {
156 	{ "queue", &ng_parse_int32_type, },
157 	{ "qlen",  &ng_parse_int32_type, },
158 	{ NULL, }
159 };
160 static const struct ng_parse_type		ng_ubt_node_qlen_type =
161 {
162 	&ng_parse_struct_type,
163 	&ng_ubt_node_qlen_type_fields
164 };
165 
166 /* Stat info */
167 static const struct ng_parse_struct_field	ng_ubt_node_stat_type_fields[] =
168 {
169 	{ "pckts_recv", &ng_parse_uint32_type, },
170 	{ "bytes_recv", &ng_parse_uint32_type, },
171 	{ "pckts_sent", &ng_parse_uint32_type, },
172 	{ "bytes_sent", &ng_parse_uint32_type, },
173 	{ "oerrors",    &ng_parse_uint32_type, },
174 	{ "ierrors",    &ng_parse_uint32_type, },
175 	{ NULL, }
176 };
177 static const struct ng_parse_type		ng_ubt_node_stat_type =
178 {
179 	&ng_parse_struct_type,
180 	&ng_ubt_node_stat_type_fields
181 };
182 
183 /* Netgraph node command list */
184 static const struct ng_cmdlist			ng_ubt_cmdlist[] =
185 {
186 	{
187 		NGM_UBT_COOKIE,
188 		NGM_UBT_NODE_SET_DEBUG,
189 		"set_debug",
190 		&ng_parse_uint16_type,
191 		NULL
192 	},
193 	{
194 		NGM_UBT_COOKIE,
195 		NGM_UBT_NODE_GET_DEBUG,
196 		"get_debug",
197 		NULL,
198 		&ng_parse_uint16_type
199 	},
200 	{
201 		NGM_UBT_COOKIE,
202 		NGM_UBT_NODE_SET_QLEN,
203 		"set_qlen",
204 		&ng_ubt_node_qlen_type,
205 		NULL
206 	},
207 	{
208 		NGM_UBT_COOKIE,
209 		NGM_UBT_NODE_GET_QLEN,
210 		"get_qlen",
211 		&ng_ubt_node_qlen_type,
212 		&ng_ubt_node_qlen_type
213 	},
214 	{
215 		NGM_UBT_COOKIE,
216 		NGM_UBT_NODE_GET_STAT,
217 		"get_stat",
218 		NULL,
219 		&ng_ubt_node_stat_type
220 	},
221 	{
222 		NGM_UBT_COOKIE,
223 		NGM_UBT_NODE_RESET_STAT,
224 		"reset_stat",
225 		NULL,
226 		NULL
227 	},
228 	{ 0, }
229 };
230 
231 /* Netgraph node type */
232 static struct ng_type	typestruct =
233 {
234 	.version = 	NG_ABI_VERSION,
235 	.name =		NG_UBT_NODE_TYPE,
236 	.constructor =	ng_ubt_constructor,
237 	.rcvmsg =	ng_ubt_rcvmsg,
238 	.shutdown =	ng_ubt_shutdown,
239 	.newhook =	ng_ubt_newhook,
240 	.connect =	ng_ubt_connect,
241 	.rcvdata =	ng_ubt_rcvdata,
242 	.disconnect =	ng_ubt_disconnect,
243 	.cmdlist =	ng_ubt_cmdlist
244 };
245 
246 /****************************************************************************
247  ****************************************************************************
248  **                              USB specific
249  ****************************************************************************
250  ****************************************************************************/
251 
252 /* USB methods */
253 static usb_callback_t	ubt_ctrl_write_callback;
254 static usb_callback_t	ubt_intr_read_callback;
255 static usb_callback_t	ubt_bulk_read_callback;
256 static usb_callback_t	ubt_bulk_write_callback;
257 static usb_callback_t	ubt_isoc_read_callback;
258 static usb_callback_t	ubt_isoc_write_callback;
259 
260 static int		ubt_fwd_mbuf_up(ubt_softc_p, struct mbuf **);
261 static int		ubt_isoc_read_one_frame(struct usb_xfer *, int);
262 
263 /*
264  * USB config
265  *
266  * The following desribes usb transfers that could be submitted on USB device.
267  *
268  * Interface 0 on the USB device must present the following endpoints
269  *	1) Interrupt endpoint to receive HCI events
270  *	2) Bulk IN endpoint to receive ACL data
271  *	3) Bulk OUT endpoint to send ACL data
272  *
273  * Interface 1 on the USB device must present the following endpoints
274  *	1) Isochronous IN endpoint to receive SCO data
275  *	2) Isochronous OUT endpoint to send SCO data
276  */
277 
278 static const struct usb_config		ubt_config[UBT_N_TRANSFER] =
279 {
280 	/*
281 	 * Interface #0
282  	 */
283 
284 	/* Outgoing bulk transfer - ACL packets */
285 	[UBT_IF_0_BULK_DT_WR] = {
286 		.type =		UE_BULK,
287 		.endpoint =	UE_ADDR_ANY,
288 		.direction =	UE_DIR_OUT,
289 		.if_index = 	0,
290 		.bufsize =	UBT_BULK_WRITE_BUFFER_SIZE,
291 		.flags =	{ .pipe_bof = 1, .force_short_xfer = 1, },
292 		.callback =	&ubt_bulk_write_callback,
293 	},
294 	/* Incoming bulk transfer - ACL packets */
295 	[UBT_IF_0_BULK_DT_RD] = {
296 		.type =		UE_BULK,
297 		.endpoint =	UE_ADDR_ANY,
298 		.direction =	UE_DIR_IN,
299 		.if_index = 	0,
300 		.bufsize =	UBT_BULK_READ_BUFFER_SIZE,
301 		.flags =	{ .pipe_bof = 1, .short_xfer_ok = 1, },
302 		.callback =	&ubt_bulk_read_callback,
303 	},
304 	/* Incoming interrupt transfer - HCI events */
305 	[UBT_IF_0_INTR_DT_RD] = {
306 		.type =		UE_INTERRUPT,
307 		.endpoint =	UE_ADDR_ANY,
308 		.direction =	UE_DIR_IN,
309 		.if_index = 	0,
310 		.flags =	{ .pipe_bof = 1, .short_xfer_ok = 1, },
311 		.bufsize =	UBT_INTR_BUFFER_SIZE,
312 		.callback =	&ubt_intr_read_callback,
313 	},
314 	/* Outgoing control transfer - HCI commands */
315 	[UBT_IF_0_CTRL_DT_WR] = {
316 		.type =		UE_CONTROL,
317 		.endpoint =	0x00,	/* control pipe */
318 		.direction =	UE_DIR_ANY,
319 		.if_index = 	0,
320 		.bufsize =	UBT_CTRL_BUFFER_SIZE,
321 		.callback =	&ubt_ctrl_write_callback,
322 		.timeout =	5000,	/* 5 seconds */
323 	},
324 
325 	/*
326 	 * Interface #1
327  	 */
328 
329 	/* Incoming isochronous transfer #1 - SCO packets */
330 	[UBT_IF_1_ISOC_DT_RD1] = {
331 		.type =		UE_ISOCHRONOUS,
332 		.endpoint =	UE_ADDR_ANY,
333 		.direction =	UE_DIR_IN,
334 		.if_index = 	1,
335 		.bufsize =	0,	/* use "wMaxPacketSize * frames" */
336 		.frames =	UBT_ISOC_NFRAMES,
337 		.flags =	{ .short_xfer_ok = 1, },
338 		.callback =	&ubt_isoc_read_callback,
339 	},
340 	/* Incoming isochronous transfer #2 - SCO packets */
341 	[UBT_IF_1_ISOC_DT_RD2] = {
342 		.type =		UE_ISOCHRONOUS,
343 		.endpoint =	UE_ADDR_ANY,
344 		.direction =	UE_DIR_IN,
345 		.if_index = 	1,
346 		.bufsize =	0,	/* use "wMaxPacketSize * frames" */
347 		.frames =	UBT_ISOC_NFRAMES,
348 		.flags =	{ .short_xfer_ok = 1, },
349 		.callback =	&ubt_isoc_read_callback,
350 	},
351 	/* Outgoing isochronous transfer #1 - SCO packets */
352 	[UBT_IF_1_ISOC_DT_WR1] = {
353 		.type =		UE_ISOCHRONOUS,
354 		.endpoint =	UE_ADDR_ANY,
355 		.direction =	UE_DIR_OUT,
356 		.if_index = 	1,
357 		.bufsize =	0,	/* use "wMaxPacketSize * frames" */
358 		.frames =	UBT_ISOC_NFRAMES,
359 		.flags =	{ .short_xfer_ok = 1, },
360 		.callback =	&ubt_isoc_write_callback,
361 	},
362 	/* Outgoing isochronous transfer #2 - SCO packets */
363 	[UBT_IF_1_ISOC_DT_WR2] = {
364 		.type =		UE_ISOCHRONOUS,
365 		.endpoint =	UE_ADDR_ANY,
366 		.direction =	UE_DIR_OUT,
367 		.if_index = 	1,
368 		.bufsize =	0,	/* use "wMaxPacketSize * frames" */
369 		.frames =	UBT_ISOC_NFRAMES,
370 		.flags =	{ .short_xfer_ok = 1, },
371 		.callback =	&ubt_isoc_write_callback,
372 	},
373 };
374 
375 /*
376  * If for some reason device should not be attached then put
377  * VendorID/ProductID pair into the list below. The format is
378  * as follows:
379  *
380  *	{ USB_VPI(VENDOR_ID, PRODUCT_ID, 0) },
381  *
382  * where VENDOR_ID and PRODUCT_ID are hex numbers.
383  */
384 
385 static const struct usb_device_id ubt_ignore_devs[] =
386 {
387 	/* AVM USB Bluetooth-Adapter BlueFritz! v1.0 */
388 	{ USB_VPI(USB_VENDOR_AVM, 0x2200, 0) },
389 };
390 
391 /* List of supported bluetooth devices */
392 static const struct usb_device_id ubt_devs[] =
393 {
394 	/* Generic Bluetooth class devices */
395 	{ USB_IFACE_CLASS(UDCLASS_WIRELESS),
396 	  USB_IFACE_SUBCLASS(UDSUBCLASS_RF),
397 	  USB_IFACE_PROTOCOL(UDPROTO_BLUETOOTH) },
398 
399 	/* AVM USB Bluetooth-Adapter BlueFritz! v2.0 */
400 	{ USB_VPI(USB_VENDOR_AVM, 0x3800, 0) },
401 };
402 
403 /*
404  * Probe for a USB Bluetooth device.
405  * USB context.
406  */
407 
408 static int
409 ubt_probe(device_t dev)
410 {
411 	struct usb_attach_arg	*uaa = device_get_ivars(dev);
412 
413 	if (uaa->usb_mode != USB_MODE_HOST)
414 		return (ENXIO);
415 
416 	if (uaa->info.bIfaceIndex != 0)
417 		return (ENXIO);
418 
419 	if (uaa->use_generic == 0)
420 		return (ENXIO);
421 
422 	if (usbd_lookup_id_by_uaa(ubt_ignore_devs,
423 			sizeof(ubt_ignore_devs), uaa) == 0)
424 		return (ENXIO);
425 
426 	return (usbd_lookup_id_by_uaa(ubt_devs, sizeof(ubt_devs), uaa));
427 } /* ubt_probe */
428 
429 /*
430  * Attach the device.
431  * USB context.
432  */
433 
434 static int
435 ubt_attach(device_t dev)
436 {
437 	struct usb_attach_arg		*uaa = device_get_ivars(dev);
438 	struct ubt_softc		*sc = device_get_softc(dev);
439 	struct usb_endpoint_descriptor	*ed;
440 	struct usb_interface_descriptor *id;
441 	uint16_t			wMaxPacketSize;
442 	uint8_t				alt_index, i, j;
443 	uint8_t				iface_index[2] = { 0, 1 };
444 
445 	device_set_usb_desc(dev);
446 
447 	sc->sc_dev = dev;
448 	sc->sc_debug = NG_UBT_WARN_LEVEL;
449 
450 	/*
451 	 * Create Netgraph node
452 	 */
453 
454 	if (ng_make_node_common(&typestruct, &sc->sc_node) != 0) {
455 		UBT_ALERT(sc, "could not create Netgraph node\n");
456 		return (ENXIO);
457 	}
458 
459 	/* Name Netgraph node */
460 	if (ng_name_node(sc->sc_node, device_get_nameunit(dev)) != 0) {
461 		UBT_ALERT(sc, "could not name Netgraph node\n");
462 		NG_NODE_UNREF(sc->sc_node);
463 		return (ENXIO);
464 	}
465 	NG_NODE_SET_PRIVATE(sc->sc_node, sc);
466 	NG_NODE_FORCE_WRITER(sc->sc_node);
467 
468 	/*
469 	 * Initialize device softc structure
470 	 */
471 
472 	/* initialize locks */
473 	mtx_init(&sc->sc_ng_mtx, "ubt ng", NULL, MTX_DEF);
474 	mtx_init(&sc->sc_if_mtx, "ubt if", NULL, MTX_DEF | MTX_RECURSE);
475 
476 	/* initialize packet queues */
477 	NG_BT_MBUFQ_INIT(&sc->sc_cmdq, UBT_DEFAULT_QLEN);
478 	NG_BT_MBUFQ_INIT(&sc->sc_aclq, UBT_DEFAULT_QLEN);
479 	NG_BT_MBUFQ_INIT(&sc->sc_scoq, UBT_DEFAULT_QLEN);
480 
481 	/* initialize glue task */
482 	TASK_INIT(&sc->sc_task, 0, ubt_task, sc);
483 
484 	/*
485 	 * Configure Bluetooth USB device. Discover all required USB
486 	 * interfaces and endpoints.
487 	 *
488 	 * USB device must present two interfaces:
489 	 * 1) Interface 0 that has 3 endpoints
490 	 *	1) Interrupt endpoint to receive HCI events
491 	 *	2) Bulk IN endpoint to receive ACL data
492 	 *	3) Bulk OUT endpoint to send ACL data
493 	 *
494 	 * 2) Interface 1 then has 2 endpoints
495 	 *	1) Isochronous IN endpoint to receive SCO data
496  	 *	2) Isochronous OUT endpoint to send SCO data
497 	 *
498 	 * Interface 1 (with isochronous endpoints) has several alternate
499 	 * configurations with different packet size.
500 	 */
501 
502 	/*
503 	 * For interface #1 search alternate settings, and find
504 	 * the descriptor with the largest wMaxPacketSize
505 	 */
506 
507 	wMaxPacketSize = 0;
508 	alt_index = 0;
509 	i = 0;
510 	j = 0;
511 	ed = NULL;
512 
513 	/*
514 	 * Search through all the descriptors looking for the largest
515 	 * packet size:
516 	 */
517 	while ((ed = (struct usb_endpoint_descriptor *)usb_desc_foreach(
518 	    usbd_get_config_descriptor(uaa->device),
519 	    (struct usb_descriptor *)ed))) {
520 
521 		if ((ed->bDescriptorType == UDESC_INTERFACE) &&
522 		    (ed->bLength >= sizeof(*id))) {
523 			id = (struct usb_interface_descriptor *)ed;
524 			i = id->bInterfaceNumber;
525 			j = id->bAlternateSetting;
526 		}
527 
528 		if ((ed->bDescriptorType == UDESC_ENDPOINT) &&
529 		    (ed->bLength >= sizeof(*ed)) &&
530 		    (i == 1)) {
531 			uint16_t temp;
532 
533 			temp = UGETW(ed->wMaxPacketSize);
534 			if (temp > wMaxPacketSize) {
535 				wMaxPacketSize = temp;
536 				alt_index = j;
537 			}
538 		}
539 	}
540 
541 	/* Set alt configuration on interface #1 only if we found it */
542 	if (wMaxPacketSize > 0 &&
543 	    usbd_set_alt_interface_index(uaa->device, 1, alt_index)) {
544 		UBT_ALERT(sc, "could not set alternate setting %d " \
545 			"for interface 1!\n", alt_index);
546 		goto detach;
547 	}
548 
549 	/* Setup transfers for both interfaces */
550 	if (usbd_transfer_setup(uaa->device, iface_index, sc->sc_xfer,
551 			ubt_config, UBT_N_TRANSFER, sc, &sc->sc_if_mtx)) {
552 		UBT_ALERT(sc, "could not allocate transfers\n");
553 		goto detach;
554 	}
555 
556 	/* Claim all interfaces on the device */
557 	for (i = 1; usbd_get_iface(uaa->device, i) != NULL; i ++)
558 		usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
559 
560 	return (0); /* success */
561 
562 detach:
563 	ubt_detach(dev);
564 
565 	return (ENXIO);
566 } /* ubt_attach */
567 
568 /*
569  * Detach the device.
570  * USB context.
571  */
572 
573 int
574 ubt_detach(device_t dev)
575 {
576 	struct ubt_softc	*sc = device_get_softc(dev);
577 	node_p			node = sc->sc_node;
578 
579 	/* Destroy Netgraph node */
580 	if (node != NULL) {
581 		sc->sc_node = NULL;
582 		NG_NODE_REALLY_DIE(node);
583 		ng_rmnode_self(node);
584 	}
585 
586 	/* Make sure ubt_task in gone */
587 	taskqueue_drain(taskqueue_swi, &sc->sc_task);
588 
589 	/* Free USB transfers, if any */
590 	usbd_transfer_unsetup(sc->sc_xfer, UBT_N_TRANSFER);
591 
592 	/* Destroy queues */
593 	UBT_NG_LOCK(sc);
594 	NG_BT_MBUFQ_DESTROY(&sc->sc_cmdq);
595 	NG_BT_MBUFQ_DESTROY(&sc->sc_aclq);
596 	NG_BT_MBUFQ_DESTROY(&sc->sc_scoq);
597 	UBT_NG_UNLOCK(sc);
598 
599 	mtx_destroy(&sc->sc_if_mtx);
600 	mtx_destroy(&sc->sc_ng_mtx);
601 
602 	return (0);
603 } /* ubt_detach */
604 
605 /*
606  * Called when outgoing control request (HCI command) has completed, i.e.
607  * HCI command was sent to the device.
608  * USB context.
609  */
610 
611 static void
612 ubt_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
613 {
614 	struct ubt_softc		*sc = usbd_xfer_softc(xfer);
615 	struct usb_device_request	req;
616 	struct mbuf			*m;
617 	struct usb_page_cache		*pc;
618 	int				actlen;
619 
620 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
621 
622 	switch (USB_GET_STATE(xfer)) {
623 	case USB_ST_TRANSFERRED:
624 		UBT_INFO(sc, "sent %d bytes to control pipe\n", actlen);
625 		UBT_STAT_BYTES_SENT(sc, actlen);
626 		UBT_STAT_PCKTS_SENT(sc);
627 		/* FALLTHROUGH */
628 
629 	case USB_ST_SETUP:
630 send_next:
631 		/* Get next command mbuf, if any */
632 		UBT_NG_LOCK(sc);
633 		NG_BT_MBUFQ_DEQUEUE(&sc->sc_cmdq, m);
634 		UBT_NG_UNLOCK(sc);
635 
636 		if (m == NULL) {
637 			UBT_INFO(sc, "HCI command queue is empty\n");
638 			break;	/* transfer complete */
639 		}
640 
641 		/* Initialize a USB control request and then schedule it */
642 		bzero(&req, sizeof(req));
643 		req.bmRequestType = UBT_HCI_REQUEST;
644 		USETW(req.wLength, m->m_pkthdr.len);
645 
646 		UBT_INFO(sc, "Sending control request, " \
647 			"bmRequestType=0x%02x, wLength=%d\n",
648 			req.bmRequestType, UGETW(req.wLength));
649 
650 		pc = usbd_xfer_get_frame(xfer, 0);
651 		usbd_copy_in(pc, 0, &req, sizeof(req));
652 		pc = usbd_xfer_get_frame(xfer, 1);
653 		usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
654 
655 		usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
656 		usbd_xfer_set_frame_len(xfer, 1, m->m_pkthdr.len);
657 		usbd_xfer_set_frames(xfer, 2);
658 
659 		NG_FREE_M(m);
660 
661 		usbd_transfer_submit(xfer);
662 		break;
663 
664 	default: /* Error */
665 		if (error != USB_ERR_CANCELLED) {
666 			UBT_WARN(sc, "control transfer failed: %s\n",
667 				usbd_errstr(error));
668 
669 			UBT_STAT_OERROR(sc);
670 			goto send_next;
671 		}
672 
673 		/* transfer cancelled */
674 		break;
675 	}
676 } /* ubt_ctrl_write_callback */
677 
678 /*
679  * Called when incoming interrupt transfer (HCI event) has completed, i.e.
680  * HCI event was received from the device.
681  * USB context.
682  */
683 
684 static void
685 ubt_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
686 {
687 	struct ubt_softc	*sc = usbd_xfer_softc(xfer);
688 	struct mbuf		*m;
689 	ng_hci_event_pkt_t	*hdr;
690 	struct usb_page_cache	*pc;
691 	int			actlen;
692 
693 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
694 
695 	m = NULL;
696 
697 	switch (USB_GET_STATE(xfer)) {
698 	case USB_ST_TRANSFERRED:
699 		/* Allocate a new mbuf */
700 		MGETHDR(m, M_DONTWAIT, MT_DATA);
701 		if (m == NULL) {
702 			UBT_STAT_IERROR(sc);
703 			goto submit_next;
704 		}
705 
706 		MCLGET(m, M_DONTWAIT);
707 		if (!(m->m_flags & M_EXT)) {
708 			UBT_STAT_IERROR(sc);
709 			goto submit_next;
710 		}
711 
712 		/* Add HCI packet type */
713 		*mtod(m, uint8_t *)= NG_HCI_EVENT_PKT;
714 		m->m_pkthdr.len = m->m_len = 1;
715 
716 		if (actlen > MCLBYTES - 1)
717 			actlen = MCLBYTES - 1;
718 
719 		pc = usbd_xfer_get_frame(xfer, 0);
720 		usbd_copy_out(pc, 0, mtod(m, uint8_t *) + 1, actlen);
721 		m->m_pkthdr.len += actlen;
722 		m->m_len += actlen;
723 
724 		UBT_INFO(sc, "got %d bytes from interrupt pipe\n",
725 			actlen);
726 
727 		/* Validate packet and send it up the stack */
728 		if (m->m_pkthdr.len < sizeof(*hdr)) {
729 			UBT_INFO(sc, "HCI event packet is too short\n");
730 
731 			UBT_STAT_IERROR(sc);
732 			goto submit_next;
733 		}
734 
735 		hdr = mtod(m, ng_hci_event_pkt_t *);
736 		if (hdr->length != (m->m_pkthdr.len - sizeof(*hdr))) {
737 			UBT_ERR(sc, "Invalid HCI event packet size, " \
738 				"length=%d, pktlen=%d\n",
739 				hdr->length, m->m_pkthdr.len);
740 
741 			UBT_STAT_IERROR(sc);
742 			goto submit_next;
743 		}
744 
745 		UBT_INFO(sc, "got complete HCI event frame, pktlen=%d, " \
746 			"length=%d\n", m->m_pkthdr.len, hdr->length);
747 
748 		UBT_STAT_PCKTS_RECV(sc);
749 		UBT_STAT_BYTES_RECV(sc, m->m_pkthdr.len);
750 
751 		ubt_fwd_mbuf_up(sc, &m);
752 		/* m == NULL at this point */
753 		/* FALLTHROUGH */
754 
755 	case USB_ST_SETUP:
756 submit_next:
757 		NG_FREE_M(m); /* checks for m != NULL */
758 
759 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
760 		usbd_transfer_submit(xfer);
761 		break;
762 
763 	default: /* Error */
764 		if (error != USB_ERR_CANCELLED) {
765 			UBT_WARN(sc, "interrupt transfer failed: %s\n",
766 				usbd_errstr(error));
767 
768 			/* Try to clear stall first */
769 			usbd_xfer_set_stall(xfer);
770 			goto submit_next;
771 		}
772 			/* transfer cancelled */
773 		break;
774 	}
775 } /* ubt_intr_read_callback */
776 
777 /*
778  * Called when incoming bulk transfer (ACL packet) has completed, i.e.
779  * ACL packet was received from the device.
780  * USB context.
781  */
782 
783 static void
784 ubt_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
785 {
786 	struct ubt_softc	*sc = usbd_xfer_softc(xfer);
787 	struct mbuf		*m;
788 	ng_hci_acldata_pkt_t	*hdr;
789 	struct usb_page_cache	*pc;
790 	uint16_t		len;
791 	int			actlen;
792 
793 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
794 
795 	m = NULL;
796 
797 	switch (USB_GET_STATE(xfer)) {
798 	case USB_ST_TRANSFERRED:
799 		/* Allocate new mbuf */
800 		MGETHDR(m, M_DONTWAIT, MT_DATA);
801 		if (m == NULL) {
802 			UBT_STAT_IERROR(sc);
803 			goto submit_next;
804 		}
805 
806 		MCLGET(m, M_DONTWAIT);
807 		if (!(m->m_flags & M_EXT)) {
808 			UBT_STAT_IERROR(sc);
809 			goto submit_next;
810 		}
811 
812 		/* Add HCI packet type */
813 		*mtod(m, uint8_t *)= NG_HCI_ACL_DATA_PKT;
814 		m->m_pkthdr.len = m->m_len = 1;
815 
816 		if (actlen > MCLBYTES - 1)
817 			actlen = MCLBYTES - 1;
818 
819 		pc = usbd_xfer_get_frame(xfer, 0);
820 		usbd_copy_out(pc, 0, mtod(m, uint8_t *) + 1, actlen);
821 		m->m_pkthdr.len += actlen;
822 		m->m_len += actlen;
823 
824 		UBT_INFO(sc, "got %d bytes from bulk-in pipe\n",
825 			actlen);
826 
827 		/* Validate packet and send it up the stack */
828 		if (m->m_pkthdr.len < sizeof(*hdr)) {
829 			UBT_INFO(sc, "HCI ACL packet is too short\n");
830 
831 			UBT_STAT_IERROR(sc);
832 			goto submit_next;
833 		}
834 
835 		hdr = mtod(m, ng_hci_acldata_pkt_t *);
836 		len = le16toh(hdr->length);
837 		if (len != (m->m_pkthdr.len - sizeof(*hdr))) {
838 			UBT_ERR(sc, "Invalid ACL packet size, length=%d, " \
839 				"pktlen=%d\n", len, m->m_pkthdr.len);
840 
841 			UBT_STAT_IERROR(sc);
842 			goto submit_next;
843 		}
844 
845 		UBT_INFO(sc, "got complete ACL data packet, pktlen=%d, " \
846 			"length=%d\n", m->m_pkthdr.len, len);
847 
848 		UBT_STAT_PCKTS_RECV(sc);
849 		UBT_STAT_BYTES_RECV(sc, m->m_pkthdr.len);
850 
851 		ubt_fwd_mbuf_up(sc, &m);
852 		/* m == NULL at this point */
853 		/* FALLTHOUGH */
854 
855 	case USB_ST_SETUP:
856 submit_next:
857 		NG_FREE_M(m); /* checks for m != NULL */
858 
859 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
860 		usbd_transfer_submit(xfer);
861 		break;
862 
863 	default: /* Error */
864 		if (error != USB_ERR_CANCELLED) {
865 			UBT_WARN(sc, "bulk-in transfer failed: %s\n",
866 				usbd_errstr(error));
867 
868 			/* Try to clear stall first */
869 			usbd_xfer_set_stall(xfer);
870 			goto submit_next;
871 		}
872 			/* transfer cancelled */
873 		break;
874 	}
875 } /* ubt_bulk_read_callback */
876 
877 /*
878  * Called when outgoing bulk transfer (ACL packet) has completed, i.e.
879  * ACL packet was sent to the device.
880  * USB context.
881  */
882 
883 static void
884 ubt_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
885 {
886 	struct ubt_softc	*sc = usbd_xfer_softc(xfer);
887 	struct mbuf		*m;
888 	struct usb_page_cache	*pc;
889 	int			actlen;
890 
891 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
892 
893 	switch (USB_GET_STATE(xfer)) {
894 	case USB_ST_TRANSFERRED:
895 		UBT_INFO(sc, "sent %d bytes to bulk-out pipe\n", actlen);
896 		UBT_STAT_BYTES_SENT(sc, actlen);
897 		UBT_STAT_PCKTS_SENT(sc);
898 		/* FALLTHROUGH */
899 
900 	case USB_ST_SETUP:
901 send_next:
902 		/* Get next mbuf, if any */
903 		UBT_NG_LOCK(sc);
904 		NG_BT_MBUFQ_DEQUEUE(&sc->sc_aclq, m);
905 		UBT_NG_UNLOCK(sc);
906 
907 		if (m == NULL) {
908 			UBT_INFO(sc, "ACL data queue is empty\n");
909 			break; /* transfer completed */
910 		}
911 
912 		/*
913 		 * Copy ACL data frame back to a linear USB transfer buffer
914 		 * and schedule transfer
915 		 */
916 
917 		pc = usbd_xfer_get_frame(xfer, 0);
918 		usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
919 		usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
920 
921 		UBT_INFO(sc, "bulk-out transfer has been started, len=%d\n",
922 			m->m_pkthdr.len);
923 
924 		NG_FREE_M(m);
925 
926 		usbd_transfer_submit(xfer);
927 		break;
928 
929 	default: /* Error */
930 		if (error != USB_ERR_CANCELLED) {
931 			UBT_WARN(sc, "bulk-out transfer failed: %s\n",
932 				usbd_errstr(error));
933 
934 			UBT_STAT_OERROR(sc);
935 
936 			/* try to clear stall first */
937 			usbd_xfer_set_stall(xfer);
938 			goto send_next;
939 		}
940 			/* transfer cancelled */
941 		break;
942 	}
943 } /* ubt_bulk_write_callback */
944 
945 /*
946  * Called when incoming isoc transfer (SCO packet) has completed, i.e.
947  * SCO packet was received from the device.
948  * USB context.
949  */
950 
951 static void
952 ubt_isoc_read_callback(struct usb_xfer *xfer, usb_error_t error)
953 {
954 	struct ubt_softc	*sc = usbd_xfer_softc(xfer);
955 	int			n;
956 	int actlen, nframes;
957 
958 	usbd_xfer_status(xfer, &actlen, NULL, NULL, &nframes);
959 
960 	switch (USB_GET_STATE(xfer)) {
961 	case USB_ST_TRANSFERRED:
962 		for (n = 0; n < nframes; n ++)
963 			if (ubt_isoc_read_one_frame(xfer, n) < 0)
964 				break;
965 		/* FALLTHROUGH */
966 
967 	case USB_ST_SETUP:
968 read_next:
969 		for (n = 0; n < nframes; n ++)
970 			usbd_xfer_set_frame_len(xfer, n,
971 			    usbd_xfer_max_framelen(xfer));
972 
973 		usbd_transfer_submit(xfer);
974 		break;
975 
976 	default: /* Error */
977                 if (error != USB_ERR_CANCELLED) {
978                         UBT_STAT_IERROR(sc);
979                         goto read_next;
980                 }
981 
982 		/* transfer cancelled */
983 		break;
984 	}
985 } /* ubt_isoc_read_callback */
986 
987 /*
988  * Helper function. Called from ubt_isoc_read_callback() to read
989  * SCO data from one frame.
990  * USB context.
991  */
992 
993 static int
994 ubt_isoc_read_one_frame(struct usb_xfer *xfer, int frame_no)
995 {
996 	struct ubt_softc	*sc = usbd_xfer_softc(xfer);
997 	struct usb_page_cache	*pc;
998 	struct mbuf		*m;
999 	int			len, want, got, total;
1000 
1001 	/* Get existing SCO reassembly buffer */
1002 	pc = usbd_xfer_get_frame(xfer, 0);
1003 	m = sc->sc_isoc_in_buffer;
1004 	total = usbd_xfer_frame_len(xfer, frame_no);
1005 
1006 	/* While we have data in the frame */
1007 	while (total > 0) {
1008 		if (m == NULL) {
1009 			/* Start new reassembly buffer */
1010 			MGETHDR(m, M_DONTWAIT, MT_DATA);
1011 			if (m == NULL) {
1012 				UBT_STAT_IERROR(sc);
1013 				return (-1);	/* XXX out of sync! */
1014 			}
1015 
1016 			MCLGET(m, M_DONTWAIT);
1017 			if (!(m->m_flags & M_EXT)) {
1018 				UBT_STAT_IERROR(sc);
1019 				NG_FREE_M(m);
1020 				return (-1);	/* XXX out of sync! */
1021 			}
1022 
1023 			/* Expect SCO header */
1024 			*mtod(m, uint8_t *) = NG_HCI_SCO_DATA_PKT;
1025 			m->m_pkthdr.len = m->m_len = got = 1;
1026 			want = sizeof(ng_hci_scodata_pkt_t);
1027 		} else {
1028 			/*
1029 			 * Check if we have SCO header and if so
1030 			 * adjust amount of data we want
1031 			 */
1032 			got = m->m_pkthdr.len;
1033 			want = sizeof(ng_hci_scodata_pkt_t);
1034 
1035 			if (got >= want)
1036 				want += mtod(m, ng_hci_scodata_pkt_t *)->length;
1037 		}
1038 
1039 		/* Append frame data to the SCO reassembly buffer */
1040 		len = total;
1041 		if (got + len > want)
1042 			len = want - got;
1043 
1044 		usbd_copy_out(pc, frame_no * usbd_xfer_max_framelen(xfer),
1045 			mtod(m, uint8_t *) + m->m_pkthdr.len, len);
1046 
1047 		m->m_pkthdr.len += len;
1048 		m->m_len += len;
1049 		total -= len;
1050 
1051 		/* Check if we got everything we wanted, if not - continue */
1052 		if (got != want)
1053 			continue;
1054 
1055 		/* If we got here then we got complete SCO frame */
1056 		UBT_INFO(sc, "got complete SCO data frame, pktlen=%d, " \
1057 			"length=%d\n", m->m_pkthdr.len,
1058 			mtod(m, ng_hci_scodata_pkt_t *)->length);
1059 
1060 		UBT_STAT_PCKTS_RECV(sc);
1061 		UBT_STAT_BYTES_RECV(sc, m->m_pkthdr.len);
1062 
1063 		ubt_fwd_mbuf_up(sc, &m);
1064 		/* m == NULL at this point */
1065 	}
1066 
1067 	/* Put SCO reassembly buffer back */
1068 	sc->sc_isoc_in_buffer = m;
1069 
1070 	return (0);
1071 } /* ubt_isoc_read_one_frame */
1072 
1073 /*
1074  * Called when outgoing isoc transfer (SCO packet) has completed, i.e.
1075  * SCO packet was sent to the device.
1076  * USB context.
1077  */
1078 
1079 static void
1080 ubt_isoc_write_callback(struct usb_xfer *xfer, usb_error_t error)
1081 {
1082 	struct ubt_softc	*sc = usbd_xfer_softc(xfer);
1083 	struct usb_page_cache	*pc;
1084 	struct mbuf		*m;
1085 	int			n, space, offset;
1086 	int			actlen, nframes;
1087 
1088 	usbd_xfer_status(xfer, &actlen, NULL, NULL, &nframes);
1089 	pc = usbd_xfer_get_frame(xfer, 0);
1090 
1091 	switch (USB_GET_STATE(xfer)) {
1092 	case USB_ST_TRANSFERRED:
1093 		UBT_INFO(sc, "sent %d bytes to isoc-out pipe\n", actlen);
1094 		UBT_STAT_BYTES_SENT(sc, actlen);
1095 		UBT_STAT_PCKTS_SENT(sc);
1096 		/* FALLTHROUGH */
1097 
1098 	case USB_ST_SETUP:
1099 send_next:
1100 		offset = 0;
1101 		space = usbd_xfer_max_framelen(xfer) * nframes;
1102 		m = NULL;
1103 
1104 		while (space > 0) {
1105 			if (m == NULL) {
1106 				UBT_NG_LOCK(sc);
1107 				NG_BT_MBUFQ_DEQUEUE(&sc->sc_scoq, m);
1108 				UBT_NG_UNLOCK(sc);
1109 
1110 				if (m == NULL)
1111 					break;
1112 			}
1113 
1114 			n = min(space, m->m_pkthdr.len);
1115 			if (n > 0) {
1116 				usbd_m_copy_in(pc, offset, m,0, n);
1117 				m_adj(m, n);
1118 
1119 				offset += n;
1120 				space -= n;
1121 			}
1122 
1123 			if (m->m_pkthdr.len == 0)
1124 				NG_FREE_M(m); /* sets m = NULL */
1125 		}
1126 
1127 		/* Put whatever is left from mbuf back on queue */
1128 		if (m != NULL) {
1129 			UBT_NG_LOCK(sc);
1130 			NG_BT_MBUFQ_PREPEND(&sc->sc_scoq, m);
1131 			UBT_NG_UNLOCK(sc);
1132 		}
1133 
1134 		/*
1135 		 * Calculate sizes for isoc frames.
1136 		 * Note that offset could be 0 at this point (i.e. we have
1137 		 * nothing to send). That is fine, as we have isoc. transfers
1138 		 * going in both directions all the time. In this case it
1139 		 * would be just empty isoc. transfer.
1140 		 */
1141 
1142 		for (n = 0; n < nframes; n ++) {
1143 			usbd_xfer_set_frame_len(xfer, n,
1144 			    min(offset, usbd_xfer_max_framelen(xfer)));
1145 			offset -= usbd_xfer_frame_len(xfer, n);
1146 		}
1147 
1148 		usbd_transfer_submit(xfer);
1149 		break;
1150 
1151 	default: /* Error */
1152 		if (error != USB_ERR_CANCELLED) {
1153 			UBT_STAT_OERROR(sc);
1154 			goto send_next;
1155 		}
1156 
1157 		/* transfer cancelled */
1158 		break;
1159 	}
1160 }
1161 
1162 /*
1163  * Utility function to forward provided mbuf upstream (i.e. up the stack).
1164  * Modifies value of the mbuf pointer (sets it to NULL).
1165  * Save to call from any context.
1166  */
1167 
1168 static int
1169 ubt_fwd_mbuf_up(ubt_softc_p sc, struct mbuf **m)
1170 {
1171 	hook_p	hook;
1172 	int	error;
1173 
1174 	/*
1175 	 * Close the race with Netgraph hook newhook/disconnect methods.
1176 	 * Save the hook pointer atomically. Two cases are possible:
1177 	 *
1178 	 * 1) The hook pointer is NULL. It means disconnect method got
1179 	 *    there first. In this case we are done.
1180 	 *
1181 	 * 2) The hook pointer is not NULL. It means that hook pointer
1182 	 *    could be either in valid or invalid (i.e. in the process
1183 	 *    of disconnect) state. In any case grab an extra reference
1184 	 *    to protect the hook pointer.
1185 	 *
1186 	 * It is ok to pass hook in invalid state to NG_SEND_DATA_ONLY() as
1187 	 * it checks for it. Drop extra reference after NG_SEND_DATA_ONLY().
1188 	 */
1189 
1190 	UBT_NG_LOCK(sc);
1191 	if ((hook = sc->sc_hook) != NULL)
1192 		NG_HOOK_REF(hook);
1193 	UBT_NG_UNLOCK(sc);
1194 
1195 	if (hook == NULL) {
1196 		NG_FREE_M(*m);
1197 		return (ENETDOWN);
1198 	}
1199 
1200 	NG_SEND_DATA_ONLY(error, hook, *m);
1201 	NG_HOOK_UNREF(hook);
1202 
1203 	if (error != 0)
1204 		UBT_STAT_IERROR(sc);
1205 
1206 	return (error);
1207 } /* ubt_fwd_mbuf_up */
1208 
1209 /****************************************************************************
1210  ****************************************************************************
1211  **                                 Glue
1212  ****************************************************************************
1213  ****************************************************************************/
1214 
1215 /*
1216  * Schedule glue task. Should be called with sc_ng_mtx held.
1217  * Netgraph context.
1218  */
1219 
1220 static void
1221 ubt_task_schedule(ubt_softc_p sc, int action)
1222 {
1223 	mtx_assert(&sc->sc_ng_mtx, MA_OWNED);
1224 
1225 	/*
1226 	 * Try to handle corner case when "start all" and "stop all"
1227 	 * actions can both be set before task is executed.
1228 	 *
1229 	 * The rules are
1230 	 *
1231 	 * sc_task_flags	action		new sc_task_flags
1232 	 * ------------------------------------------------------
1233 	 * 0			start		start
1234 	 * 0			stop		stop
1235 	 * start		start		start
1236 	 * start		stop		stop
1237 	 * stop			start		stop|start
1238 	 * stop			stop		stop
1239 	 * stop|start		start		stop|start
1240 	 * stop|start		stop		stop
1241 	 */
1242 
1243 	if (action != 0) {
1244 		if ((action & UBT_FLAG_T_STOP_ALL) != 0)
1245 			sc->sc_task_flags &= ~UBT_FLAG_T_START_ALL;
1246 
1247 		sc->sc_task_flags |= action;
1248 	}
1249 
1250 	if (sc->sc_task_flags & UBT_FLAG_T_PENDING)
1251 		return;
1252 
1253 	if (taskqueue_enqueue(taskqueue_swi, &sc->sc_task) == 0) {
1254 		sc->sc_task_flags |= UBT_FLAG_T_PENDING;
1255 		return;
1256 	}
1257 
1258 	/* XXX: i think this should never happen */
1259 } /* ubt_task_schedule */
1260 
1261 /*
1262  * Glue task. Examines sc_task_flags and does things depending on it.
1263  * Taskqueue context.
1264  */
1265 
1266 static void
1267 ubt_task(void *context, int pending)
1268 {
1269 	ubt_softc_p	sc = context;
1270 	int		task_flags, i;
1271 
1272 	UBT_NG_LOCK(sc);
1273 	task_flags = sc->sc_task_flags;
1274 	sc->sc_task_flags = 0;
1275 	UBT_NG_UNLOCK(sc);
1276 
1277 	/*
1278 	 * Stop all USB transfers synchronously.
1279 	 * Stop interface #0 and #1 transfers at the same time and in the
1280 	 * same loop. usbd_transfer_drain() will do appropriate locking.
1281 	 */
1282 
1283 	if (task_flags & UBT_FLAG_T_STOP_ALL)
1284 		for (i = 0; i < UBT_N_TRANSFER; i ++)
1285 			usbd_transfer_drain(sc->sc_xfer[i]);
1286 
1287 	/* Start incoming interrupt and bulk, and all isoc. USB transfers */
1288 	if (task_flags & UBT_FLAG_T_START_ALL) {
1289 		/*
1290 		 * Interface #0
1291 		 */
1292 
1293 		mtx_lock(&sc->sc_if_mtx);
1294 
1295 		ubt_xfer_start(sc, UBT_IF_0_INTR_DT_RD);
1296 		ubt_xfer_start(sc, UBT_IF_0_BULK_DT_RD);
1297 
1298 		/*
1299 		 * Interface #1
1300 		 * Start both read and write isoc. transfers by default.
1301 		 * Get them going all the time even if we have nothing
1302 		 * to send to avoid any delays.
1303 		 */
1304 
1305 		ubt_xfer_start(sc, UBT_IF_1_ISOC_DT_RD1);
1306 		ubt_xfer_start(sc, UBT_IF_1_ISOC_DT_RD2);
1307 		ubt_xfer_start(sc, UBT_IF_1_ISOC_DT_WR1);
1308 		ubt_xfer_start(sc, UBT_IF_1_ISOC_DT_WR2);
1309 
1310 		mtx_unlock(&sc->sc_if_mtx);
1311 	}
1312 
1313  	/* Start outgoing control transfer */
1314 	if (task_flags & UBT_FLAG_T_START_CTRL) {
1315 		mtx_lock(&sc->sc_if_mtx);
1316 		ubt_xfer_start(sc, UBT_IF_0_CTRL_DT_WR);
1317 		mtx_unlock(&sc->sc_if_mtx);
1318 	}
1319 
1320 	/* Start outgoing bulk transfer */
1321 	if (task_flags & UBT_FLAG_T_START_BULK) {
1322 		mtx_lock(&sc->sc_if_mtx);
1323 		ubt_xfer_start(sc, UBT_IF_0_BULK_DT_WR);
1324 		mtx_unlock(&sc->sc_if_mtx);
1325 	}
1326 } /* ubt_task */
1327 
1328 /****************************************************************************
1329  ****************************************************************************
1330  **                        Netgraph specific
1331  ****************************************************************************
1332  ****************************************************************************/
1333 
1334 /*
1335  * Netgraph node constructor. Do not allow to create node of this type.
1336  * Netgraph context.
1337  */
1338 
1339 static int
1340 ng_ubt_constructor(node_p node)
1341 {
1342 	return (EINVAL);
1343 } /* ng_ubt_constructor */
1344 
1345 /*
1346  * Netgraph node destructor. Destroy node only when device has been detached.
1347  * Netgraph context.
1348  */
1349 
1350 static int
1351 ng_ubt_shutdown(node_p node)
1352 {
1353 	if (node->nd_flags & NGF_REALLY_DIE) {
1354 		/*
1355                  * We came here because the USB device is being
1356 		 * detached, so stop being persistant.
1357                  */
1358 		NG_NODE_SET_PRIVATE(node, NULL);
1359 		NG_NODE_UNREF(node);
1360 	} else
1361 		NG_NODE_REVIVE(node); /* tell ng_rmnode we are persisant */
1362 
1363 	return (0);
1364 } /* ng_ubt_shutdown */
1365 
1366 /*
1367  * Create new hook. There can only be one.
1368  * Netgraph context.
1369  */
1370 
1371 static int
1372 ng_ubt_newhook(node_p node, hook_p hook, char const *name)
1373 {
1374 	struct ubt_softc	*sc = NG_NODE_PRIVATE(node);
1375 
1376 	if (strcmp(name, NG_UBT_HOOK) != 0)
1377 		return (EINVAL);
1378 
1379 	UBT_NG_LOCK(sc);
1380 	if (sc->sc_hook != NULL) {
1381 		UBT_NG_UNLOCK(sc);
1382 
1383 		return (EISCONN);
1384 	}
1385 
1386 	sc->sc_hook = hook;
1387 	UBT_NG_UNLOCK(sc);
1388 
1389 	return (0);
1390 } /* ng_ubt_newhook */
1391 
1392 /*
1393  * Connect hook. Start incoming USB transfers.
1394  * Netgraph context.
1395  */
1396 
1397 static int
1398 ng_ubt_connect(hook_p hook)
1399 {
1400 	struct ubt_softc	*sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1401 
1402 	NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
1403 
1404 	UBT_NG_LOCK(sc);
1405 	ubt_task_schedule(sc, UBT_FLAG_T_START_ALL);
1406 	UBT_NG_UNLOCK(sc);
1407 
1408 	return (0);
1409 } /* ng_ubt_connect */
1410 
1411 /*
1412  * Disconnect hook.
1413  * Netgraph context.
1414  */
1415 
1416 static int
1417 ng_ubt_disconnect(hook_p hook)
1418 {
1419 	struct ubt_softc	*sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1420 
1421 	UBT_NG_LOCK(sc);
1422 
1423 	if (hook != sc->sc_hook) {
1424 		UBT_NG_UNLOCK(sc);
1425 
1426 		return (EINVAL);
1427 	}
1428 
1429 	sc->sc_hook = NULL;
1430 
1431 	/* Kick off task to stop all USB xfers */
1432 	ubt_task_schedule(sc, UBT_FLAG_T_STOP_ALL);
1433 
1434 	/* Drain queues */
1435 	NG_BT_MBUFQ_DRAIN(&sc->sc_cmdq);
1436 	NG_BT_MBUFQ_DRAIN(&sc->sc_aclq);
1437 	NG_BT_MBUFQ_DRAIN(&sc->sc_scoq);
1438 
1439 	UBT_NG_UNLOCK(sc);
1440 
1441 	return (0);
1442 } /* ng_ubt_disconnect */
1443 
1444 /*
1445  * Process control message.
1446  * Netgraph context.
1447  */
1448 
1449 static int
1450 ng_ubt_rcvmsg(node_p node, item_p item, hook_p lasthook)
1451 {
1452 	struct ubt_softc	*sc = NG_NODE_PRIVATE(node);
1453 	struct ng_mesg		*msg, *rsp = NULL;
1454 	struct ng_bt_mbufq	*q;
1455 	int			error = 0, queue, qlen;
1456 
1457 	NGI_GET_MSG(item, msg);
1458 
1459 	switch (msg->header.typecookie) {
1460 	case NGM_GENERIC_COOKIE:
1461 		switch (msg->header.cmd) {
1462 		case NGM_TEXT_STATUS:
1463 			NG_MKRESPONSE(rsp, msg, NG_TEXTRESPONSE, M_NOWAIT);
1464 			if (rsp == NULL) {
1465 				error = ENOMEM;
1466 				break;
1467 			}
1468 
1469 			snprintf(rsp->data, NG_TEXTRESPONSE,
1470 				"Hook: %s\n" \
1471 				"Task flags: %#x\n" \
1472 				"Debug: %d\n" \
1473 				"CMD queue: [have:%d,max:%d]\n" \
1474 				"ACL queue: [have:%d,max:%d]\n" \
1475 				"SCO queue: [have:%d,max:%d]",
1476 				(sc->sc_hook != NULL) ? NG_UBT_HOOK : "",
1477 				sc->sc_task_flags,
1478 				sc->sc_debug,
1479 				sc->sc_cmdq.len,
1480 				sc->sc_cmdq.maxlen,
1481 				sc->sc_aclq.len,
1482 				sc->sc_aclq.maxlen,
1483 				sc->sc_scoq.len,
1484 				sc->sc_scoq.maxlen);
1485 			break;
1486 
1487 		default:
1488 			error = EINVAL;
1489 			break;
1490 		}
1491 		break;
1492 
1493 	case NGM_UBT_COOKIE:
1494 		switch (msg->header.cmd) {
1495 		case NGM_UBT_NODE_SET_DEBUG:
1496 			if (msg->header.arglen != sizeof(ng_ubt_node_debug_ep)){
1497 				error = EMSGSIZE;
1498 				break;
1499 			}
1500 
1501 			sc->sc_debug = *((ng_ubt_node_debug_ep *) (msg->data));
1502 			break;
1503 
1504 		case NGM_UBT_NODE_GET_DEBUG:
1505 			NG_MKRESPONSE(rsp, msg, sizeof(ng_ubt_node_debug_ep),
1506 			    M_NOWAIT);
1507 			if (rsp == NULL) {
1508 				error = ENOMEM;
1509 				break;
1510 			}
1511 
1512 			*((ng_ubt_node_debug_ep *) (rsp->data)) = sc->sc_debug;
1513 			break;
1514 
1515 		case NGM_UBT_NODE_SET_QLEN:
1516 			if (msg->header.arglen != sizeof(ng_ubt_node_qlen_ep)) {
1517 				error = EMSGSIZE;
1518 				break;
1519 			}
1520 
1521 			queue = ((ng_ubt_node_qlen_ep *) (msg->data))->queue;
1522 			qlen = ((ng_ubt_node_qlen_ep *) (msg->data))->qlen;
1523 
1524 			switch (queue) {
1525 			case NGM_UBT_NODE_QUEUE_CMD:
1526 				q = &sc->sc_cmdq;
1527 				break;
1528 
1529 			case NGM_UBT_NODE_QUEUE_ACL:
1530 				q = &sc->sc_aclq;
1531 				break;
1532 
1533 			case NGM_UBT_NODE_QUEUE_SCO:
1534 				q = &sc->sc_scoq;
1535 				break;
1536 
1537 			default:
1538 				error = EINVAL;
1539 				goto done;
1540 				/* NOT REACHED */
1541 			}
1542 
1543 			q->maxlen = qlen;
1544 			break;
1545 
1546 		case NGM_UBT_NODE_GET_QLEN:
1547 			if (msg->header.arglen != sizeof(ng_ubt_node_qlen_ep)) {
1548 				error = EMSGSIZE;
1549 				break;
1550 			}
1551 
1552 			queue = ((ng_ubt_node_qlen_ep *) (msg->data))->queue;
1553 
1554 			switch (queue) {
1555 			case NGM_UBT_NODE_QUEUE_CMD:
1556 				q = &sc->sc_cmdq;
1557 				break;
1558 
1559 			case NGM_UBT_NODE_QUEUE_ACL:
1560 				q = &sc->sc_aclq;
1561 				break;
1562 
1563 			case NGM_UBT_NODE_QUEUE_SCO:
1564 				q = &sc->sc_scoq;
1565 				break;
1566 
1567 			default:
1568 				error = EINVAL;
1569 				goto done;
1570 				/* NOT REACHED */
1571 			}
1572 
1573 			NG_MKRESPONSE(rsp, msg, sizeof(ng_ubt_node_qlen_ep),
1574 				M_NOWAIT);
1575 			if (rsp == NULL) {
1576 				error = ENOMEM;
1577 				break;
1578 			}
1579 
1580 			((ng_ubt_node_qlen_ep *) (rsp->data))->queue = queue;
1581 			((ng_ubt_node_qlen_ep *) (rsp->data))->qlen = q->maxlen;
1582 			break;
1583 
1584 		case NGM_UBT_NODE_GET_STAT:
1585 			NG_MKRESPONSE(rsp, msg, sizeof(ng_ubt_node_stat_ep),
1586 			    M_NOWAIT);
1587 			if (rsp == NULL) {
1588 				error = ENOMEM;
1589 				break;
1590 			}
1591 
1592 			bcopy(&sc->sc_stat, rsp->data,
1593 				sizeof(ng_ubt_node_stat_ep));
1594 			break;
1595 
1596 		case NGM_UBT_NODE_RESET_STAT:
1597 			UBT_STAT_RESET(sc);
1598 			break;
1599 
1600 		default:
1601 			error = EINVAL;
1602 			break;
1603 		}
1604 		break;
1605 
1606 	default:
1607 		error = EINVAL;
1608 		break;
1609 	}
1610 done:
1611 	NG_RESPOND_MSG(error, node, item, rsp);
1612 	NG_FREE_MSG(msg);
1613 
1614 	return (error);
1615 } /* ng_ubt_rcvmsg */
1616 
1617 /*
1618  * Process data.
1619  * Netgraph context.
1620  */
1621 
1622 static int
1623 ng_ubt_rcvdata(hook_p hook, item_p item)
1624 {
1625 	struct ubt_softc	*sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1626 	struct mbuf		*m;
1627 	struct ng_bt_mbufq	*q;
1628 	int			action, error = 0;
1629 
1630 	if (hook != sc->sc_hook) {
1631 		error = EINVAL;
1632 		goto done;
1633 	}
1634 
1635 	/* Deatch mbuf and get HCI frame type */
1636 	NGI_GET_M(item, m);
1637 
1638 	/*
1639 	 * Minimal size of the HCI frame is 4 bytes: 1 byte frame type,
1640 	 * 2 bytes connection handle and at least 1 byte of length.
1641 	 * Panic on data frame that has size smaller than 4 bytes (it
1642 	 * should not happen)
1643 	 */
1644 
1645 	if (m->m_pkthdr.len < 4)
1646 		panic("HCI frame size is too small! pktlen=%d\n",
1647 			m->m_pkthdr.len);
1648 
1649 	/* Process HCI frame */
1650 	switch (*mtod(m, uint8_t *)) {	/* XXX call m_pullup ? */
1651 	case NG_HCI_CMD_PKT:
1652 		if (m->m_pkthdr.len - 1 > UBT_CTRL_BUFFER_SIZE)
1653 			panic("HCI command frame size is too big! " \
1654 				"buffer size=%zd, packet len=%d\n",
1655 				UBT_CTRL_BUFFER_SIZE, m->m_pkthdr.len);
1656 
1657 		q = &sc->sc_cmdq;
1658 		action = UBT_FLAG_T_START_CTRL;
1659 		break;
1660 
1661 	case NG_HCI_ACL_DATA_PKT:
1662 		if (m->m_pkthdr.len - 1 > UBT_BULK_WRITE_BUFFER_SIZE)
1663 			panic("ACL data frame size is too big! " \
1664 				"buffer size=%d, packet len=%d\n",
1665 				UBT_BULK_WRITE_BUFFER_SIZE, m->m_pkthdr.len);
1666 
1667 		q = &sc->sc_aclq;
1668 		action = UBT_FLAG_T_START_BULK;
1669 		break;
1670 
1671 	case NG_HCI_SCO_DATA_PKT:
1672 		q = &sc->sc_scoq;
1673 		action = 0;
1674 		break;
1675 
1676 	default:
1677 		UBT_ERR(sc, "Dropping unsupported HCI frame, type=0x%02x, " \
1678 			"pktlen=%d\n", *mtod(m, uint8_t *), m->m_pkthdr.len);
1679 
1680 		NG_FREE_M(m);
1681 		error = EINVAL;
1682 		goto done;
1683 		/* NOT REACHED */
1684 	}
1685 
1686 	UBT_NG_LOCK(sc);
1687 	if (NG_BT_MBUFQ_FULL(q)) {
1688 		NG_BT_MBUFQ_DROP(q);
1689 		UBT_NG_UNLOCK(sc);
1690 
1691 		UBT_ERR(sc, "Dropping HCI frame 0x%02x, len=%d. Queue full\n",
1692 			*mtod(m, uint8_t *), m->m_pkthdr.len);
1693 
1694 		NG_FREE_M(m);
1695 	} else {
1696 		/* Loose HCI packet type, enqueue mbuf and kick off task */
1697 		m_adj(m, sizeof(uint8_t));
1698 		NG_BT_MBUFQ_ENQUEUE(q, m);
1699 		ubt_task_schedule(sc, action);
1700 		UBT_NG_UNLOCK(sc);
1701 	}
1702 done:
1703 	NG_FREE_ITEM(item);
1704 
1705 	return (error);
1706 } /* ng_ubt_rcvdata */
1707 
1708 /****************************************************************************
1709  ****************************************************************************
1710  **                              Module
1711  ****************************************************************************
1712  ****************************************************************************/
1713 
1714 /*
1715  * Load/Unload the driver module
1716  */
1717 
1718 static int
1719 ubt_modevent(module_t mod, int event, void *data)
1720 {
1721 	int	error;
1722 
1723 	switch (event) {
1724 	case MOD_LOAD:
1725 		error = ng_newtype(&typestruct);
1726 		if (error != 0)
1727 			printf("%s: Could not register Netgraph node type, " \
1728 				"error=%d\n", NG_UBT_NODE_TYPE, error);
1729 		break;
1730 
1731 	case MOD_UNLOAD:
1732 		error = ng_rmtype(&typestruct);
1733 		break;
1734 
1735 	default:
1736 		error = EOPNOTSUPP;
1737 		break;
1738 	}
1739 
1740 	return (error);
1741 } /* ubt_modevent */
1742 
1743 static devclass_t	ubt_devclass;
1744 
1745 static device_method_t	ubt_methods[] =
1746 {
1747 	DEVMETHOD(device_probe,	ubt_probe),
1748 	DEVMETHOD(device_attach, ubt_attach),
1749 	DEVMETHOD(device_detach, ubt_detach),
1750 	{ 0, 0 }
1751 };
1752 
1753 static driver_t		ubt_driver =
1754 {
1755 	.name =	   "ubt",
1756 	.methods = ubt_methods,
1757 	.size =	   sizeof(struct ubt_softc),
1758 };
1759 
1760 DRIVER_MODULE(ng_ubt, uhub, ubt_driver, ubt_devclass, ubt_modevent, 0);
1761 MODULE_VERSION(ng_ubt, NG_BLUETOOTH_VERSION);
1762 MODULE_DEPEND(ng_ubt, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
1763 MODULE_DEPEND(ng_ubt, ng_hci, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
1764 MODULE_DEPEND(ng_ubt, usb, 1, 1, 1);
1765 
1766