1 /*
2  * ng_ubt_intel.c
3  */
4 
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause
7  *
8  * Copyright (c) 2019, 2021 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 
32 /*
33  * Attempt to initialize FreeBSD bluetooth stack while Intel Wireless 8260/8265
34  * device is in bootloader mode locks the adapter hardly so it requires power
35  * on/off cycle to restore. This driver blocks ng_ubt attachment until
36  * operational firmware is loaded by iwmbtfw utility thus avoiding the lock up.
37  */
38 
39 #include <sys/types.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/mbuf.h>
43 #include <sys/module.h>
44 #include <sys/systm.h>
45 #include <sys/taskqueue.h>
46 
47 #include "usbdevs.h"
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50 
51 #include <netgraph/ng_message.h>
52 #include <netgraph/netgraph.h>
53 #include <netgraph/ng_parse.h>
54 #include <netgraph/bluetooth/include/ng_bluetooth.h>
55 #include <netgraph/bluetooth/include/ng_hci.h>
56 #include <netgraph/bluetooth/include/ng_ubt.h>
57 #include <netgraph/bluetooth/drivers/ubt/ng_ubt_var.h>
58 
59 enum {
60 	UBT_INTEL_DEVICE_7260,
61 	UBT_INTEL_DEVICE_8260,
62 };
63 
64 struct ubt_intel_version_rp {
65 	uint8_t status;
66 	uint8_t hw_platform;
67 	uint8_t hw_variant;
68 	uint8_t hw_revision;
69 	uint8_t fw_variant;
70 	uint8_t fw_revision;
71 	uint8_t fw_build_num;
72 	uint8_t fw_build_ww;
73 	uint8_t fw_build_yy;
74 	uint8_t fw_patch_num;
75 } __attribute__ ((packed));
76 
77 static device_probe_t	ubt_intel_probe;
78 
79 /*
80  * List of supported bluetooth devices. If you add a new device PID here ensure
81  * that it is blacklisted in ng_ubt.c and is supported by iwmbtfw utility.
82  */
83 
84 static const STRUCT_USB_HOST_ID ubt_intel_devs[] =
85 {
86 	/* Intel Wireless 7260/7265 and successors */
87 	{ USB_VPI(USB_VENDOR_INTEL2, 0x07dc, UBT_INTEL_DEVICE_7260) },
88 	{ USB_VPI(USB_VENDOR_INTEL2, 0x0a2a, UBT_INTEL_DEVICE_7260) },
89 	{ USB_VPI(USB_VENDOR_INTEL2, 0x0aa7, UBT_INTEL_DEVICE_7260) },
90 	/* Intel Wireless 8260/8265 and successors */
91 	{ USB_VPI(USB_VENDOR_INTEL2, 0x0a2b, UBT_INTEL_DEVICE_8260) },
92 	{ USB_VPI(USB_VENDOR_INTEL2, 0x0aaa, UBT_INTEL_DEVICE_8260) },
93 	{ USB_VPI(USB_VENDOR_INTEL2, 0x0025, UBT_INTEL_DEVICE_8260) },
94 	{ USB_VPI(USB_VENDOR_INTEL2, 0x0026, UBT_INTEL_DEVICE_8260) },
95 	{ USB_VPI(USB_VENDOR_INTEL2, 0x0029, UBT_INTEL_DEVICE_8260) },
96 };
97 
98 /*
99  * Execute generic HCI command and return response in provided buffer.
100  */
101 
102 static usb_error_t
103 ubt_intel_do_hci_request(struct usb_device *udev, uint16_t opcode,
104     void *resp, uint8_t resp_len)
105 {
106 #define	UBT_INTEL_HCICMD_TIMEOUT	2000	/* ms */
107 	struct ubt_hci_event_command_compl *evt;
108 	struct ubt_hci_cmd cmd;
109 	usb_error_t error;
110 
111 	memset(&cmd, 0, sizeof(cmd));
112 	cmd.opcode = htole16(opcode);
113 	evt = malloc(offsetof(struct ubt_hci_event_command_compl, data) +
114 	    resp_len, M_TEMP, M_ZERO | M_WAITOK);
115 	evt->header.length = resp_len + UBT_HCI_EVENT_COMPL_HEAD_SIZE;
116 
117 	error = ubt_do_hci_request(udev, &cmd, evt, UBT_INTEL_HCICMD_TIMEOUT);
118 	if (error != USB_ERR_NORMAL_COMPLETION)
119 		goto exit;
120 
121 	if (evt->header.event == NG_HCI_EVENT_COMMAND_COMPL &&
122 	    evt->header.length == resp_len + UBT_HCI_EVENT_COMPL_HEAD_SIZE)
123 		memcpy(resp, evt->data, resp_len);
124 	else
125 		error = USB_ERR_INVAL;
126 exit:
127 	free(evt, M_TEMP);
128 	return (error);
129 }
130 
131 /*
132  * Probe for a Intel Wireless Bluetooth device.
133  */
134 
135 static int
136 ubt_intel_probe(device_t dev)
137 {
138 	struct usb_attach_arg	*uaa = device_get_ivars(dev);
139 	struct ubt_intel_version_rp version;
140 	ng_hci_reset_rp reset;
141 	int error;
142 
143 	if (uaa->usb_mode != USB_MODE_HOST)
144 		return (ENXIO);
145 
146 	if (uaa->info.bIfaceIndex != 0)
147 		return (ENXIO);
148 
149 	error = usbd_lookup_id_by_uaa(ubt_intel_devs, sizeof(ubt_intel_devs),
150 	    uaa);
151 	if (error != 0)
152 		return (error);
153 
154 	switch (USB_GET_DRIVER_INFO(uaa)) {
155 	case UBT_INTEL_DEVICE_7260:
156 		/*
157 		 * Send HCI Reset command to workaround controller bug with the
158 		 * first HCI command sent to it returning number of completed
159 		 * commands as zero.  This will reset the number of completed
160 		 * commands and allow further normal command processing.
161 		 */
162 		if (ubt_intel_do_hci_request(uaa->device,
163 		    NG_HCI_OPCODE(NG_HCI_OGF_HC_BASEBAND, NG_HCI_OCF_RESET),
164 		    &reset, sizeof(reset)) != USB_ERR_NORMAL_COMPLETION)
165 			return (ENXIO);
166 		if (reset.status != 0)
167 			return (ENXIO);
168 		/*
169 		 * fw_patch_num indicates the version of patch the device
170 		 * currently have.  If there is no patch data in the device,
171 		 * it is always 0x00 and we need to patch the device again.
172 		 */
173 		if (ubt_intel_do_hci_request(uaa->device,
174 		    NG_HCI_OPCODE(NG_HCI_OGF_VENDOR, 0x05),
175 		    &version, sizeof(version)) != USB_ERR_NORMAL_COMPLETION)
176 			return (ENXIO);
177 		if (version.fw_patch_num == 0)
178 			return (ENXIO);
179 		break;
180 
181 	case UBT_INTEL_DEVICE_8260:
182 		/*
183 		 * Find if the Intel Wireless 8260/8265 device is in bootloader
184 		 * mode or is running operational firmware with checking of
185 		 * variant byte of "Intel version" HCI command response.
186 		 * The value 0x23 identifies the operational firmware.
187 		 */
188 		if (ubt_intel_do_hci_request(uaa->device,
189 		    NG_HCI_OPCODE(NG_HCI_OGF_VENDOR, 0x05),
190 		    &version, sizeof(version)) != USB_ERR_NORMAL_COMPLETION)
191 			return (ENXIO);
192 		if (version.fw_variant != 0x23)
193 			return (ENXIO);
194 		break;
195 
196 	default:
197 		KASSERT(0 == 1, ("Unknown DRIVER_INFO"));
198 	}
199 
200 	return (BUS_PROBE_DEFAULT);
201 }
202 
203 /*
204  * Module interface. Attach and detach methods, netgraph node type
205  * registration and PNP string are inherited from ng_ubt.c driver.
206  */
207 
208 static device_method_t	ubt_intel_methods[] =
209 {
210 	DEVMETHOD(device_probe,	ubt_intel_probe),
211 	DEVMETHOD_END
212 };
213 
214 DEFINE_CLASS_1(ubt, ubt_intel_driver, ubt_intel_methods,
215     sizeof(struct ubt_softc), ubt_driver);
216 DRIVER_MODULE(ng_ubt_intel, uhub, ubt_intel_driver, 0, 0);
217 MODULE_VERSION(ng_ubt_intel, NG_BLUETOOTH_VERSION);
218 MODULE_DEPEND(ng_ubt_intel, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
219 MODULE_DEPEND(ng_ubt_intel, ng_hci, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
220 MODULE_DEPEND(ng_ubt_intel, usb, 1, 1, 1);
221