1 /*
2  * ng_ubt_intel.c
3  */
4 
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
7  *
8  * Copyright (c) 2019 Vladimir Kondratyev <wulf@FreeBSD.org>
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33 
34 /*
35  * Attempt to initialize FreeBSD bluetooth stack while Intel Wireless 8260/8265
36  * device is in bootloader mode locks the adapter hardly so it requires power
37  * on/off cycle to restore. This driver blocks ng_ubt attachment until
38  * operational firmware is loaded by iwmbtfw utility thus avoiding the lock up.
39  */
40 
41 #include <sys/types.h>
42 #include <sys/bus.h>
43 #include <sys/kernel.h>
44 #include <sys/mbuf.h>
45 #include <sys/module.h>
46 #include <sys/systm.h>
47 #include <sys/taskqueue.h>
48 
49 #include "usbdevs.h"
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
52 
53 #include <netgraph/ng_message.h>
54 #include <netgraph/netgraph.h>
55 #include <netgraph/ng_parse.h>
56 #include <netgraph/bluetooth/include/ng_bluetooth.h>
57 #include <netgraph/bluetooth/include/ng_hci.h>
58 #include <netgraph/bluetooth/include/ng_ubt.h>
59 #include <netgraph/bluetooth/drivers/ubt/ng_ubt_var.h>
60 
61 static device_probe_t	ubt_intel_probe;
62 
63 /*
64  * List of supported bluetooth devices. If you add a new device PID here ensure
65  * that it is blacklisted in ng_ubt.c and is supported by iwmbtfw utility.
66  */
67 
68 static const STRUCT_USB_HOST_ID ubt_intel_devs[] =
69 {
70 	/* Intel Wireless 8260/8265 and successors */
71 	{ USB_VPI(USB_VENDOR_INTEL2, 0x0a2b, 0) },
72 	{ USB_VPI(USB_VENDOR_INTEL2, 0x0aaa, 0) },
73 	{ USB_VPI(USB_VENDOR_INTEL2, 0x0025, 0) },
74 	{ USB_VPI(USB_VENDOR_INTEL2, 0x0026, 0) },
75 	{ USB_VPI(USB_VENDOR_INTEL2, 0x0029, 0) },
76 };
77 
78 /*
79  * Find if the Intel Wireless 8260/8265 device is in bootloader mode or is
80  * running operational firmware with checking of 4-th byte "Intel version"
81  * HCI command response. The value 0x23 identifies the operational firmware.
82  */
83 
84 static bool
85 ubt_intel_check_firmware_state(struct usb_device *udev)
86 {
87 #define	UBT_INTEL_VER_LEN		13
88 #define	UBT_INTEL_HCICMD_TIMEOUT	2000	/* ms */
89 	struct ubt_hci_event_command_compl *evt;
90 	uint8_t buf[offsetof(struct ubt_hci_event, data) + UBT_INTEL_VER_LEN];
91 	static struct ubt_hci_cmd cmd = {
92 		.opcode = htole16(NG_HCI_OPCODE(NG_HCI_OGF_VENDOR, 0x05)),
93 		.length = 0,
94 	};
95 	usb_error_t error;
96 
97 	bzero(buf, sizeof(buf));
98 	evt = (struct ubt_hci_event_command_compl *)buf;
99 	evt->header.length = UBT_INTEL_VER_LEN;
100 
101 	error = ubt_do_hci_request(udev, &cmd, evt, UBT_INTEL_HCICMD_TIMEOUT);
102 	if (error != USB_ERR_NORMAL_COMPLETION)
103 		return false;
104 
105 	return (evt->header.event == NG_HCI_EVENT_COMMAND_COMPL &&
106 		evt->header.length == UBT_INTEL_VER_LEN &&
107 		evt->data[4] == 0x23);
108 }
109 
110 /*
111  * Probe for a Intel Wireless Bluetooth device.
112  */
113 
114 static int
115 ubt_intel_probe(device_t dev)
116 {
117 	struct usb_attach_arg	*uaa = device_get_ivars(dev);
118 	int error;
119 
120 	if (uaa->usb_mode != USB_MODE_HOST)
121 		return (ENXIO);
122 
123 	if (uaa->info.bIfaceIndex != 0)
124 		return (ENXIO);
125 
126 	error = usbd_lookup_id_by_uaa(ubt_intel_devs, sizeof(ubt_intel_devs),
127 	    uaa);
128 	if (error != 0)
129 		return (error);
130 
131 	if (!ubt_intel_check_firmware_state(uaa->device))
132 		return (ENXIO);
133 
134 	return (BUS_PROBE_DEFAULT);
135 }
136 
137 /*
138  * Module interface. Attach and detach methods, netgraph node type
139  * registration and PNP string are inherited from ng_ubt.c driver.
140  */
141 
142 static device_method_t	ubt_intel_methods[] =
143 {
144 	DEVMETHOD(device_probe,	ubt_intel_probe),
145 	DEVMETHOD_END
146 };
147 
148 static kobj_class_t ubt_baseclasses[] = { &ubt_driver, NULL };
149 static driver_t		ubt_intel_driver =
150 {
151 	.name =	   "ubt",
152 	.methods = ubt_intel_methods,
153 	.size =	   sizeof(struct ubt_softc),
154 	.baseclasses = ubt_baseclasses,
155 };
156 
157 DRIVER_MODULE(ng_ubt_intel, uhub, ubt_intel_driver, ubt_devclass, 0, 0);
158 MODULE_VERSION(ng_ubt_intel, NG_BLUETOOTH_VERSION);
159 MODULE_DEPEND(ng_ubt_intel, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
160 MODULE_DEPEND(ng_ubt_intel, ng_hci, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
161 MODULE_DEPEND(ng_ubt_intel, usb, 1, 1, 1);
162