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