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