1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Generic Bluetooth USB driver
6 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
10 #include <linux/module.h>
11 #include <linux/usb.h>
12 #include <linux/usb/quirks.h>
13 #include <linux/firmware.h>
14 #include <linux/iopoll.h>
15 #include <linux/of_device.h>
16 #include <linux/of_irq.h>
17 #include <linux/suspend.h>
18 #include <linux/gpio/consumer.h>
19 #include <asm/unaligned.h>
21 #include <net/bluetooth/bluetooth.h>
22 #include <net/bluetooth/hci_core.h>
30 static bool disable_scofix;
31 static bool force_scofix;
32 static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
34 static bool reset = true;
36 static struct usb_driver btusb_driver;
38 #define BTUSB_IGNORE 0x01
39 #define BTUSB_DIGIANSWER 0x02
40 #define BTUSB_CSR 0x04
41 #define BTUSB_SNIFFER 0x08
42 #define BTUSB_BCM92035 0x10
43 #define BTUSB_BROKEN_ISOC 0x20
44 #define BTUSB_WRONG_SCO_MTU 0x40
45 #define BTUSB_ATH3012 0x80
46 #define BTUSB_INTEL 0x100
47 #define BTUSB_INTEL_BOOT 0x200
48 #define BTUSB_BCM_PATCHRAM 0x400
49 #define BTUSB_MARVELL 0x800
50 #define BTUSB_SWAVE 0x1000
51 #define BTUSB_INTEL_NEW 0x2000
52 #define BTUSB_AMP 0x4000
53 #define BTUSB_QCA_ROME 0x8000
54 #define BTUSB_BCM_APPLE 0x10000
55 #define BTUSB_REALTEK 0x20000
56 #define BTUSB_BCM2045 0x40000
57 #define BTUSB_IFNUM_2 0x80000
58 #define BTUSB_CW6622 0x100000
59 #define BTUSB_MEDIATEK 0x200000
60 #define BTUSB_WIDEBAND_SPEECH 0x400000
61 #define BTUSB_VALID_LE_STATES 0x800000
62 #define BTUSB_QCA_WCN6855 0x1000000
63 #define BTUSB_INTEL_NEWGEN 0x2000000
65 static const struct usb_device_id btusb_table[] = {
66 /* Generic Bluetooth USB device */
67 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
69 /* Generic Bluetooth AMP device */
70 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
72 /* Generic Bluetooth USB interface */
73 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
75 /* Apple-specific (Broadcom) devices */
76 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
77 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
79 /* MediaTek MT76x0E */
80 { USB_DEVICE(0x0e8d, 0x763f) },
82 /* Broadcom SoftSailing reporting vendor specific */
83 { USB_DEVICE(0x0a5c, 0x21e1) },
85 /* Apple MacBookPro 7,1 */
86 { USB_DEVICE(0x05ac, 0x8213) },
89 { USB_DEVICE(0x05ac, 0x8215) },
91 /* Apple MacBookPro6,2 */
92 { USB_DEVICE(0x05ac, 0x8218) },
94 /* Apple MacBookAir3,1, MacBookAir3,2 */
95 { USB_DEVICE(0x05ac, 0x821b) },
97 /* Apple MacBookAir4,1 */
98 { USB_DEVICE(0x05ac, 0x821f) },
100 /* Apple MacBookPro8,2 */
101 { USB_DEVICE(0x05ac, 0x821a) },
103 /* Apple MacMini5,1 */
104 { USB_DEVICE(0x05ac, 0x8281) },
106 /* AVM BlueFRITZ! USB v2.0 */
107 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
109 /* Bluetooth Ultraport Module from IBM */
110 { USB_DEVICE(0x04bf, 0x030a) },
112 /* ALPS Modules with non-standard id */
113 { USB_DEVICE(0x044e, 0x3001) },
114 { USB_DEVICE(0x044e, 0x3002) },
116 /* Ericsson with non-standard id */
117 { USB_DEVICE(0x0bdb, 0x1002) },
119 /* Canyon CN-BTU1 with HID interfaces */
120 { USB_DEVICE(0x0c10, 0x0000) },
122 /* Broadcom BCM20702A0 */
123 { USB_DEVICE(0x413c, 0x8197) },
125 /* Broadcom BCM20702B0 (Dynex/Insignia) */
126 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
128 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
129 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
130 .driver_info = BTUSB_BCM_PATCHRAM },
132 /* Broadcom BCM920703 (HTC Vive) */
133 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
134 .driver_info = BTUSB_BCM_PATCHRAM },
136 /* Foxconn - Hon Hai */
137 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
138 .driver_info = BTUSB_BCM_PATCHRAM },
140 /* Lite-On Technology - Broadcom based */
141 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
142 .driver_info = BTUSB_BCM_PATCHRAM },
144 /* Broadcom devices with vendor specific id */
145 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
146 .driver_info = BTUSB_BCM_PATCHRAM },
148 /* ASUSTek Computer - Broadcom based */
149 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
150 .driver_info = BTUSB_BCM_PATCHRAM },
152 /* Belkin F8065bf - Broadcom based */
153 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
154 .driver_info = BTUSB_BCM_PATCHRAM },
156 /* IMC Networks - Broadcom based */
157 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
158 .driver_info = BTUSB_BCM_PATCHRAM },
160 /* Dell Computer - Broadcom based */
161 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
162 .driver_info = BTUSB_BCM_PATCHRAM },
164 /* Toshiba Corp - Broadcom based */
165 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
166 .driver_info = BTUSB_BCM_PATCHRAM },
168 /* Intel Bluetooth USB Bootloader (RAM module) */
169 { USB_DEVICE(0x8087, 0x0a5a),
170 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
172 { } /* Terminating entry */
175 MODULE_DEVICE_TABLE(usb, btusb_table);
177 static const struct usb_device_id blacklist_table[] = {
178 /* CSR BlueCore devices */
179 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
181 /* Broadcom BCM2033 without firmware */
182 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
184 /* Broadcom BCM2045 devices */
185 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
187 /* Atheros 3011 with sflash firmware */
188 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
189 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
190 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
191 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
192 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
193 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
194 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
196 /* Atheros AR9285 Malbec with sflash firmware */
197 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
199 /* Atheros 3012 with sflash firmware */
200 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
246 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
247 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
248 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
249 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
251 /* Atheros AR5BBU12 with sflash firmware */
252 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
254 /* Atheros AR5BBU12 with sflash firmware */
255 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
256 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
258 /* QCA ROME chipset */
259 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
260 BTUSB_WIDEBAND_SPEECH },
261 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
262 BTUSB_WIDEBAND_SPEECH },
263 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
264 BTUSB_WIDEBAND_SPEECH },
265 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
266 BTUSB_WIDEBAND_SPEECH },
267 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
268 BTUSB_WIDEBAND_SPEECH },
269 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
270 BTUSB_WIDEBAND_SPEECH },
271 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
272 BTUSB_WIDEBAND_SPEECH },
273 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
274 BTUSB_WIDEBAND_SPEECH },
275 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
276 BTUSB_WIDEBAND_SPEECH },
277 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
278 BTUSB_WIDEBAND_SPEECH },
279 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
280 BTUSB_WIDEBAND_SPEECH },
281 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
282 BTUSB_WIDEBAND_SPEECH },
283 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
284 BTUSB_WIDEBAND_SPEECH },
285 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
286 BTUSB_WIDEBAND_SPEECH },
287 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
288 BTUSB_WIDEBAND_SPEECH },
289 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
290 BTUSB_WIDEBAND_SPEECH },
291 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
292 BTUSB_WIDEBAND_SPEECH },
293 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
294 BTUSB_WIDEBAND_SPEECH },
296 /* QCA WCN6855 chipset */
297 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
298 BTUSB_WIDEBAND_SPEECH },
300 /* Broadcom BCM2035 */
301 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
302 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
303 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
305 /* Broadcom BCM2045 */
306 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
307 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
309 /* IBM/Lenovo ThinkPad with Broadcom chip */
310 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
311 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
313 /* HP laptop with Broadcom chip */
314 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
316 /* Dell laptop with Broadcom chip */
317 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
319 /* Dell Wireless 370 and 410 devices */
320 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
321 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
323 /* Belkin F8T012 and F8T013 devices */
324 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
325 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
327 /* Asus WL-BTD202 device */
328 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
330 /* Kensington Bluetooth USB adapter */
331 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
333 /* RTX Telecom based adapters with buggy SCO support */
334 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
335 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
337 /* CONWISE Technology based adapters with buggy SCO support */
338 { USB_DEVICE(0x0e5e, 0x6622),
339 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
341 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
342 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
344 /* Digianswer devices */
345 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
346 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
348 /* CSR BlueCore Bluetooth Sniffer */
349 { USB_DEVICE(0x0a12, 0x0002),
350 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
352 /* Frontline ComProbe Bluetooth Sniffer */
353 { USB_DEVICE(0x16d3, 0x0002),
354 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
356 /* Marvell Bluetooth devices */
357 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
358 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
359 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
361 /* Intel Bluetooth devices */
362 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW |
363 BTUSB_WIDEBAND_SPEECH |
364 BTUSB_VALID_LE_STATES },
365 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_NEW |
366 BTUSB_WIDEBAND_SPEECH },
367 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_NEW |
368 BTUSB_WIDEBAND_SPEECH },
369 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_NEWGEN |
370 BTUSB_WIDEBAND_SPEECH},
371 { USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_NEWGEN |
372 BTUSB_WIDEBAND_SPEECH},
373 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
374 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
375 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
376 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW |
377 BTUSB_WIDEBAND_SPEECH },
378 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL |
379 BTUSB_WIDEBAND_SPEECH },
380 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW |
381 BTUSB_WIDEBAND_SPEECH |
382 BTUSB_VALID_LE_STATES },
384 /* Other Intel Bluetooth devices */
385 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
386 .driver_info = BTUSB_IGNORE },
388 /* Realtek 8822CE Bluetooth devices */
389 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
390 BTUSB_WIDEBAND_SPEECH },
392 /* Realtek 8852AE Bluetooth devices */
393 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
394 BTUSB_WIDEBAND_SPEECH },
396 /* Realtek Bluetooth devices */
397 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
398 .driver_info = BTUSB_REALTEK },
400 /* MediaTek Bluetooth devices */
401 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
402 .driver_info = BTUSB_MEDIATEK },
404 /* Additional MediaTek MT7615E Bluetooth devices */
405 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
407 /* Additional Realtek 8723AE Bluetooth devices */
408 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
409 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
411 /* Additional Realtek 8723BE Bluetooth devices */
412 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
413 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
414 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
415 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
416 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
417 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
419 /* Additional Realtek 8723BU Bluetooth devices */
420 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
422 /* Additional Realtek 8723DE Bluetooth devices */
423 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
424 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
426 /* Additional Realtek 8821AE Bluetooth devices */
427 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
428 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
429 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
430 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
431 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
433 /* Additional Realtek 8822BE Bluetooth devices */
434 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
435 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
437 /* Additional Realtek 8822CE Bluetooth devices */
438 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
439 BTUSB_WIDEBAND_SPEECH },
440 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
441 BTUSB_WIDEBAND_SPEECH },
442 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
443 BTUSB_WIDEBAND_SPEECH },
444 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
445 BTUSB_WIDEBAND_SPEECH },
446 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
447 BTUSB_WIDEBAND_SPEECH },
448 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
449 BTUSB_WIDEBAND_SPEECH },
450 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
451 BTUSB_WIDEBAND_SPEECH },
452 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
453 BTUSB_WIDEBAND_SPEECH },
454 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
455 BTUSB_WIDEBAND_SPEECH },
456 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
457 BTUSB_WIDEBAND_SPEECH },
458 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
459 BTUSB_WIDEBAND_SPEECH },
461 /* Silicon Wave based devices */
462 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
464 { } /* Terminating entry */
467 /* The Bluetooth USB module build into some devices needs to be reset on resume,
468 * this is a problem with the platform (likely shutting off all power) not with
469 * the module itself. So we use a DMI list to match known broken platforms.
471 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
473 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
475 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
476 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
480 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
482 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
483 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
487 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
489 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
490 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
496 #define BTUSB_MAX_ISOC_FRAMES 10
498 #define BTUSB_INTR_RUNNING 0
499 #define BTUSB_BULK_RUNNING 1
500 #define BTUSB_ISOC_RUNNING 2
501 #define BTUSB_SUSPENDING 3
502 #define BTUSB_DID_ISO_RESUME 4
503 #define BTUSB_BOOTLOADER 5
504 #define BTUSB_DOWNLOADING 6
505 #define BTUSB_FIRMWARE_LOADED 7
506 #define BTUSB_FIRMWARE_FAILED 8
507 #define BTUSB_BOOTING 9
508 #define BTUSB_DIAG_RUNNING 10
509 #define BTUSB_OOB_WAKE_ENABLED 11
510 #define BTUSB_HW_RESET_ACTIVE 12
511 #define BTUSB_TX_WAIT_VND_EVT 13
512 #define BTUSB_WAKEUP_DISABLE 14
515 struct hci_dev *hdev;
516 struct usb_device *udev;
517 struct usb_interface *intf;
518 struct usb_interface *isoc;
519 struct usb_interface *diag;
524 struct work_struct work;
525 struct work_struct waker;
527 struct usb_anchor deferred;
528 struct usb_anchor tx_anchor;
532 struct usb_anchor intr_anchor;
533 struct usb_anchor bulk_anchor;
534 struct usb_anchor isoc_anchor;
535 struct usb_anchor diag_anchor;
536 struct usb_anchor ctrl_anchor;
539 struct sk_buff *evt_skb;
540 struct sk_buff *acl_skb;
541 struct sk_buff *sco_skb;
543 struct usb_endpoint_descriptor *intr_ep;
544 struct usb_endpoint_descriptor *bulk_tx_ep;
545 struct usb_endpoint_descriptor *bulk_rx_ep;
546 struct usb_endpoint_descriptor *isoc_tx_ep;
547 struct usb_endpoint_descriptor *isoc_rx_ep;
548 struct usb_endpoint_descriptor *diag_tx_ep;
549 struct usb_endpoint_descriptor *diag_rx_ep;
551 struct gpio_desc *reset_gpio;
556 unsigned int sco_num;
557 unsigned int air_mode;
558 bool usb_alt6_packet_flow;
562 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
563 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
565 int (*setup_on_usb)(struct hci_dev *hdev);
567 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
568 unsigned cmd_timeout_cnt;
571 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
573 struct btusb_data *data = hci_get_drvdata(hdev);
574 struct gpio_desc *reset_gpio = data->reset_gpio;
576 if (++data->cmd_timeout_cnt < 5)
580 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
585 * Toggle the hard reset line if the platform provides one. The reset
586 * is going to yank the device off the USB and then replug. So doing
587 * once is enough. The cleanup is handled correctly on the way out
588 * (standard USB disconnect), and the new device is detected cleanly
589 * and bound to the driver again like it should be.
591 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
592 bt_dev_err(hdev, "last reset failed? Not resetting again");
596 bt_dev_err(hdev, "Initiating HW reset via gpio");
597 gpiod_set_value_cansleep(reset_gpio, 1);
599 gpiod_set_value_cansleep(reset_gpio, 0);
602 static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
604 struct btusb_data *data = hci_get_drvdata(hdev);
605 struct gpio_desc *reset_gpio = data->reset_gpio;
607 if (++data->cmd_timeout_cnt < 5)
611 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
615 /* Toggle the hard reset line. The Realtek device is going to
616 * yank itself off the USB and then replug. The cleanup is handled
617 * correctly on the way out (standard USB disconnect), and the new
618 * device is detected cleanly and bound to the driver again like
621 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
622 bt_dev_err(hdev, "last reset failed? Not resetting again");
626 bt_dev_err(hdev, "Reset Realtek device via gpio");
627 gpiod_set_value_cansleep(reset_gpio, 1);
629 gpiod_set_value_cansleep(reset_gpio, 0);
632 static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
634 struct btusb_data *data = hci_get_drvdata(hdev);
637 if (++data->cmd_timeout_cnt < 5)
640 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
641 /* This is not an unbalanced PM reference since the device will reset */
642 err = usb_autopm_get_interface(data->intf);
644 usb_queue_reset_device(data->intf);
646 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
649 static inline void btusb_free_frags(struct btusb_data *data)
653 spin_lock_irqsave(&data->rxlock, flags);
655 kfree_skb(data->evt_skb);
656 data->evt_skb = NULL;
658 kfree_skb(data->acl_skb);
659 data->acl_skb = NULL;
661 kfree_skb(data->sco_skb);
662 data->sco_skb = NULL;
664 spin_unlock_irqrestore(&data->rxlock, flags);
667 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
673 spin_lock_irqsave(&data->rxlock, flags);
680 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
686 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
687 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
690 len = min_t(uint, hci_skb_expect(skb), count);
691 skb_put_data(skb, buffer, len);
695 hci_skb_expect(skb) -= len;
697 if (skb->len == HCI_EVENT_HDR_SIZE) {
698 /* Complete event header */
699 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
701 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
710 if (!hci_skb_expect(skb)) {
712 data->recv_event(data->hdev, skb);
718 spin_unlock_irqrestore(&data->rxlock, flags);
723 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
729 spin_lock_irqsave(&data->rxlock, flags);
736 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
742 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
743 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
746 len = min_t(uint, hci_skb_expect(skb), count);
747 skb_put_data(skb, buffer, len);
751 hci_skb_expect(skb) -= len;
753 if (skb->len == HCI_ACL_HDR_SIZE) {
754 __le16 dlen = hci_acl_hdr(skb)->dlen;
756 /* Complete ACL header */
757 hci_skb_expect(skb) = __le16_to_cpu(dlen);
759 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
768 if (!hci_skb_expect(skb)) {
770 hci_recv_frame(data->hdev, skb);
776 spin_unlock_irqrestore(&data->rxlock, flags);
781 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
787 spin_lock_irqsave(&data->rxlock, flags);
794 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
800 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
801 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
804 len = min_t(uint, hci_skb_expect(skb), count);
805 skb_put_data(skb, buffer, len);
809 hci_skb_expect(skb) -= len;
811 if (skb->len == HCI_SCO_HDR_SIZE) {
812 /* Complete SCO header */
813 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
815 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
824 if (!hci_skb_expect(skb)) {
826 hci_recv_frame(data->hdev, skb);
832 spin_unlock_irqrestore(&data->rxlock, flags);
837 static void btusb_intr_complete(struct urb *urb)
839 struct hci_dev *hdev = urb->context;
840 struct btusb_data *data = hci_get_drvdata(hdev);
843 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
846 if (!test_bit(HCI_RUNNING, &hdev->flags))
849 if (urb->status == 0) {
850 hdev->stat.byte_rx += urb->actual_length;
852 if (btusb_recv_intr(data, urb->transfer_buffer,
853 urb->actual_length) < 0) {
854 bt_dev_err(hdev, "corrupted event packet");
857 } else if (urb->status == -ENOENT) {
858 /* Avoid suspend failed when usb_kill_urb */
862 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
865 usb_mark_last_busy(data->udev);
866 usb_anchor_urb(urb, &data->intr_anchor);
868 err = usb_submit_urb(urb, GFP_ATOMIC);
870 /* -EPERM: urb is being killed;
871 * -ENODEV: device got disconnected
873 if (err != -EPERM && err != -ENODEV)
874 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
876 usb_unanchor_urb(urb);
880 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
882 struct btusb_data *data = hci_get_drvdata(hdev);
888 BT_DBG("%s", hdev->name);
893 urb = usb_alloc_urb(0, mem_flags);
897 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
899 buf = kmalloc(size, mem_flags);
905 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
907 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
908 btusb_intr_complete, hdev, data->intr_ep->bInterval);
910 urb->transfer_flags |= URB_FREE_BUFFER;
912 usb_anchor_urb(urb, &data->intr_anchor);
914 err = usb_submit_urb(urb, mem_flags);
916 if (err != -EPERM && err != -ENODEV)
917 bt_dev_err(hdev, "urb %p submission failed (%d)",
919 usb_unanchor_urb(urb);
927 static void btusb_bulk_complete(struct urb *urb)
929 struct hci_dev *hdev = urb->context;
930 struct btusb_data *data = hci_get_drvdata(hdev);
933 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
936 if (!test_bit(HCI_RUNNING, &hdev->flags))
939 if (urb->status == 0) {
940 hdev->stat.byte_rx += urb->actual_length;
942 if (data->recv_bulk(data, urb->transfer_buffer,
943 urb->actual_length) < 0) {
944 bt_dev_err(hdev, "corrupted ACL packet");
947 } else if (urb->status == -ENOENT) {
948 /* Avoid suspend failed when usb_kill_urb */
952 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
955 usb_anchor_urb(urb, &data->bulk_anchor);
956 usb_mark_last_busy(data->udev);
958 err = usb_submit_urb(urb, GFP_ATOMIC);
960 /* -EPERM: urb is being killed;
961 * -ENODEV: device got disconnected
963 if (err != -EPERM && err != -ENODEV)
964 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
966 usb_unanchor_urb(urb);
970 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
972 struct btusb_data *data = hci_get_drvdata(hdev);
976 int err, size = HCI_MAX_FRAME_SIZE;
978 BT_DBG("%s", hdev->name);
980 if (!data->bulk_rx_ep)
983 urb = usb_alloc_urb(0, mem_flags);
987 buf = kmalloc(size, mem_flags);
993 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
995 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
996 btusb_bulk_complete, hdev);
998 urb->transfer_flags |= URB_FREE_BUFFER;
1000 usb_mark_last_busy(data->udev);
1001 usb_anchor_urb(urb, &data->bulk_anchor);
1003 err = usb_submit_urb(urb, mem_flags);
1005 if (err != -EPERM && err != -ENODEV)
1006 bt_dev_err(hdev, "urb %p submission failed (%d)",
1008 usb_unanchor_urb(urb);
1016 static void btusb_isoc_complete(struct urb *urb)
1018 struct hci_dev *hdev = urb->context;
1019 struct btusb_data *data = hci_get_drvdata(hdev);
1022 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1023 urb->actual_length);
1025 if (!test_bit(HCI_RUNNING, &hdev->flags))
1028 if (urb->status == 0) {
1029 for (i = 0; i < urb->number_of_packets; i++) {
1030 unsigned int offset = urb->iso_frame_desc[i].offset;
1031 unsigned int length = urb->iso_frame_desc[i].actual_length;
1033 if (urb->iso_frame_desc[i].status)
1036 hdev->stat.byte_rx += length;
1038 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1040 bt_dev_err(hdev, "corrupted SCO packet");
1041 hdev->stat.err_rx++;
1044 } else if (urb->status == -ENOENT) {
1045 /* Avoid suspend failed when usb_kill_urb */
1049 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1052 usb_anchor_urb(urb, &data->isoc_anchor);
1054 err = usb_submit_urb(urb, GFP_ATOMIC);
1056 /* -EPERM: urb is being killed;
1057 * -ENODEV: device got disconnected
1059 if (err != -EPERM && err != -ENODEV)
1060 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1062 usb_unanchor_urb(urb);
1066 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1067 int mtu, struct btusb_data *data)
1070 unsigned int interval;
1072 BT_DBG("len %d mtu %d", len, mtu);
1074 /* For mSBC ALT 6 setting the host will send the packet at continuous
1075 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1076 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1077 * To maintain the rate we send 63bytes of usb packets alternatively for
1078 * 7ms and 8ms to maintain the rate as 7.5ms.
1080 if (data->usb_alt6_packet_flow) {
1082 data->usb_alt6_packet_flow = false;
1085 data->usb_alt6_packet_flow = true;
1088 for (i = 0; i < interval; i++) {
1089 urb->iso_frame_desc[i].offset = offset;
1090 urb->iso_frame_desc[i].length = offset;
1093 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1094 urb->iso_frame_desc[i].offset = offset;
1095 urb->iso_frame_desc[i].length = len;
1099 urb->number_of_packets = i;
1102 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1106 BT_DBG("len %d mtu %d", len, mtu);
1108 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1109 i++, offset += mtu, len -= mtu) {
1110 urb->iso_frame_desc[i].offset = offset;
1111 urb->iso_frame_desc[i].length = mtu;
1114 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1115 urb->iso_frame_desc[i].offset = offset;
1116 urb->iso_frame_desc[i].length = len;
1120 urb->number_of_packets = i;
1123 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1125 struct btusb_data *data = hci_get_drvdata(hdev);
1131 BT_DBG("%s", hdev->name);
1133 if (!data->isoc_rx_ep)
1136 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1140 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1141 BTUSB_MAX_ISOC_FRAMES;
1143 buf = kmalloc(size, mem_flags);
1149 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1151 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1152 hdev, data->isoc_rx_ep->bInterval);
1154 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1156 __fill_isoc_descriptor(urb, size,
1157 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1159 usb_anchor_urb(urb, &data->isoc_anchor);
1161 err = usb_submit_urb(urb, mem_flags);
1163 if (err != -EPERM && err != -ENODEV)
1164 bt_dev_err(hdev, "urb %p submission failed (%d)",
1166 usb_unanchor_urb(urb);
1174 static void btusb_diag_complete(struct urb *urb)
1176 struct hci_dev *hdev = urb->context;
1177 struct btusb_data *data = hci_get_drvdata(hdev);
1180 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1181 urb->actual_length);
1183 if (urb->status == 0) {
1184 struct sk_buff *skb;
1186 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1188 skb_put_data(skb, urb->transfer_buffer,
1189 urb->actual_length);
1190 hci_recv_diag(hdev, skb);
1192 } else if (urb->status == -ENOENT) {
1193 /* Avoid suspend failed when usb_kill_urb */
1197 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1200 usb_anchor_urb(urb, &data->diag_anchor);
1201 usb_mark_last_busy(data->udev);
1203 err = usb_submit_urb(urb, GFP_ATOMIC);
1205 /* -EPERM: urb is being killed;
1206 * -ENODEV: device got disconnected
1208 if (err != -EPERM && err != -ENODEV)
1209 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1211 usb_unanchor_urb(urb);
1215 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1217 struct btusb_data *data = hci_get_drvdata(hdev);
1221 int err, size = HCI_MAX_FRAME_SIZE;
1223 BT_DBG("%s", hdev->name);
1225 if (!data->diag_rx_ep)
1228 urb = usb_alloc_urb(0, mem_flags);
1232 buf = kmalloc(size, mem_flags);
1238 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1240 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1241 btusb_diag_complete, hdev);
1243 urb->transfer_flags |= URB_FREE_BUFFER;
1245 usb_mark_last_busy(data->udev);
1246 usb_anchor_urb(urb, &data->diag_anchor);
1248 err = usb_submit_urb(urb, mem_flags);
1250 if (err != -EPERM && err != -ENODEV)
1251 bt_dev_err(hdev, "urb %p submission failed (%d)",
1253 usb_unanchor_urb(urb);
1261 static void btusb_tx_complete(struct urb *urb)
1263 struct sk_buff *skb = urb->context;
1264 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1265 struct btusb_data *data = hci_get_drvdata(hdev);
1266 unsigned long flags;
1268 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1269 urb->actual_length);
1271 if (!test_bit(HCI_RUNNING, &hdev->flags))
1275 hdev->stat.byte_tx += urb->transfer_buffer_length;
1277 hdev->stat.err_tx++;
1280 spin_lock_irqsave(&data->txlock, flags);
1281 data->tx_in_flight--;
1282 spin_unlock_irqrestore(&data->txlock, flags);
1284 kfree(urb->setup_packet);
1289 static void btusb_isoc_tx_complete(struct urb *urb)
1291 struct sk_buff *skb = urb->context;
1292 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1294 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1295 urb->actual_length);
1297 if (!test_bit(HCI_RUNNING, &hdev->flags))
1301 hdev->stat.byte_tx += urb->transfer_buffer_length;
1303 hdev->stat.err_tx++;
1306 kfree(urb->setup_packet);
1311 static int btusb_open(struct hci_dev *hdev)
1313 struct btusb_data *data = hci_get_drvdata(hdev);
1316 BT_DBG("%s", hdev->name);
1318 err = usb_autopm_get_interface(data->intf);
1322 /* Patching USB firmware files prior to starting any URBs of HCI path
1323 * It is more safe to use USB bulk channel for downloading USB patch
1325 if (data->setup_on_usb) {
1326 err = data->setup_on_usb(hdev);
1331 data->intf->needs_remote_wakeup = 1;
1333 /* Disable device remote wakeup when host is suspended
1334 * For Realtek chips, global suspend without
1335 * SET_FEATURE (DEVICE_REMOTE_WAKEUP) can save more power in device.
1337 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1338 device_wakeup_disable(&data->udev->dev);
1340 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1343 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1347 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1349 usb_kill_anchored_urbs(&data->intr_anchor);
1353 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1354 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1357 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1358 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1362 usb_autopm_put_interface(data->intf);
1366 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1368 usb_autopm_put_interface(data->intf);
1372 static void btusb_stop_traffic(struct btusb_data *data)
1374 usb_kill_anchored_urbs(&data->intr_anchor);
1375 usb_kill_anchored_urbs(&data->bulk_anchor);
1376 usb_kill_anchored_urbs(&data->isoc_anchor);
1377 usb_kill_anchored_urbs(&data->diag_anchor);
1378 usb_kill_anchored_urbs(&data->ctrl_anchor);
1381 static int btusb_close(struct hci_dev *hdev)
1383 struct btusb_data *data = hci_get_drvdata(hdev);
1386 BT_DBG("%s", hdev->name);
1388 cancel_work_sync(&data->work);
1389 cancel_work_sync(&data->waker);
1391 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1392 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1393 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1394 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1396 btusb_stop_traffic(data);
1397 btusb_free_frags(data);
1399 err = usb_autopm_get_interface(data->intf);
1403 data->intf->needs_remote_wakeup = 0;
1405 /* Enable remote wake up for auto-suspend */
1406 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1407 data->intf->needs_remote_wakeup = 1;
1409 usb_autopm_put_interface(data->intf);
1412 usb_scuttle_anchored_urbs(&data->deferred);
1416 static int btusb_flush(struct hci_dev *hdev)
1418 struct btusb_data *data = hci_get_drvdata(hdev);
1420 BT_DBG("%s", hdev->name);
1422 usb_kill_anchored_urbs(&data->tx_anchor);
1423 btusb_free_frags(data);
1428 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1430 struct btusb_data *data = hci_get_drvdata(hdev);
1431 struct usb_ctrlrequest *dr;
1435 urb = usb_alloc_urb(0, GFP_KERNEL);
1437 return ERR_PTR(-ENOMEM);
1439 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1442 return ERR_PTR(-ENOMEM);
1445 dr->bRequestType = data->cmdreq_type;
1446 dr->bRequest = data->cmdreq;
1449 dr->wLength = __cpu_to_le16(skb->len);
1451 pipe = usb_sndctrlpipe(data->udev, 0x00);
1453 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1454 skb->data, skb->len, btusb_tx_complete, skb);
1456 skb->dev = (void *)hdev;
1461 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1463 struct btusb_data *data = hci_get_drvdata(hdev);
1467 if (!data->bulk_tx_ep)
1468 return ERR_PTR(-ENODEV);
1470 urb = usb_alloc_urb(0, GFP_KERNEL);
1472 return ERR_PTR(-ENOMEM);
1474 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1476 usb_fill_bulk_urb(urb, data->udev, pipe,
1477 skb->data, skb->len, btusb_tx_complete, skb);
1479 skb->dev = (void *)hdev;
1484 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1486 struct btusb_data *data = hci_get_drvdata(hdev);
1490 if (!data->isoc_tx_ep)
1491 return ERR_PTR(-ENODEV);
1493 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1495 return ERR_PTR(-ENOMEM);
1497 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1499 usb_fill_int_urb(urb, data->udev, pipe,
1500 skb->data, skb->len, btusb_isoc_tx_complete,
1501 skb, data->isoc_tx_ep->bInterval);
1503 urb->transfer_flags = URB_ISO_ASAP;
1505 if (data->isoc_altsetting == 6)
1506 __fill_isoc_descriptor_msbc(urb, skb->len,
1507 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1510 __fill_isoc_descriptor(urb, skb->len,
1511 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1512 skb->dev = (void *)hdev;
1517 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1519 struct btusb_data *data = hci_get_drvdata(hdev);
1522 usb_anchor_urb(urb, &data->tx_anchor);
1524 err = usb_submit_urb(urb, GFP_KERNEL);
1526 if (err != -EPERM && err != -ENODEV)
1527 bt_dev_err(hdev, "urb %p submission failed (%d)",
1529 kfree(urb->setup_packet);
1530 usb_unanchor_urb(urb);
1532 usb_mark_last_busy(data->udev);
1539 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1541 struct btusb_data *data = hci_get_drvdata(hdev);
1542 unsigned long flags;
1545 spin_lock_irqsave(&data->txlock, flags);
1546 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1548 data->tx_in_flight++;
1549 spin_unlock_irqrestore(&data->txlock, flags);
1552 return submit_tx_urb(hdev, urb);
1554 usb_anchor_urb(urb, &data->deferred);
1555 schedule_work(&data->waker);
1561 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1565 BT_DBG("%s", hdev->name);
1567 switch (hci_skb_pkt_type(skb)) {
1568 case HCI_COMMAND_PKT:
1569 urb = alloc_ctrl_urb(hdev, skb);
1571 return PTR_ERR(urb);
1573 hdev->stat.cmd_tx++;
1574 return submit_or_queue_tx_urb(hdev, urb);
1576 case HCI_ACLDATA_PKT:
1577 urb = alloc_bulk_urb(hdev, skb);
1579 return PTR_ERR(urb);
1581 hdev->stat.acl_tx++;
1582 return submit_or_queue_tx_urb(hdev, urb);
1584 case HCI_SCODATA_PKT:
1585 if (hci_conn_num(hdev, SCO_LINK) < 1)
1588 urb = alloc_isoc_urb(hdev, skb);
1590 return PTR_ERR(urb);
1592 hdev->stat.sco_tx++;
1593 return submit_tx_urb(hdev, urb);
1599 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1601 struct btusb_data *data = hci_get_drvdata(hdev);
1603 BT_DBG("%s evt %d", hdev->name, evt);
1605 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1606 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1607 data->air_mode = evt;
1608 schedule_work(&data->work);
1612 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1614 struct btusb_data *data = hci_get_drvdata(hdev);
1615 struct usb_interface *intf = data->isoc;
1616 struct usb_endpoint_descriptor *ep_desc;
1622 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
1624 bt_dev_err(hdev, "setting interface failed (%d)", -err);
1628 data->isoc_altsetting = altsetting;
1630 data->isoc_tx_ep = NULL;
1631 data->isoc_rx_ep = NULL;
1633 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1634 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1636 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1637 data->isoc_tx_ep = ep_desc;
1641 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1642 data->isoc_rx_ep = ep_desc;
1647 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1648 bt_dev_err(hdev, "invalid SCO descriptors");
1655 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1657 struct btusb_data *data = hci_get_drvdata(hdev);
1660 if (data->isoc_altsetting != new_alts) {
1661 unsigned long flags;
1663 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1664 usb_kill_anchored_urbs(&data->isoc_anchor);
1666 /* When isochronous alternate setting needs to be
1667 * changed, because SCO connection has been added
1668 * or removed, a packet fragment may be left in the
1669 * reassembling state. This could lead to wrongly
1670 * assembled fragments.
1672 * Clear outstanding fragment when selecting a new
1673 * alternate setting.
1675 spin_lock_irqsave(&data->rxlock, flags);
1676 kfree_skb(data->sco_skb);
1677 data->sco_skb = NULL;
1678 spin_unlock_irqrestore(&data->rxlock, flags);
1680 err = __set_isoc_interface(hdev, new_alts);
1685 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1686 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1687 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1689 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1695 static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1698 struct usb_interface *intf = data->isoc;
1701 BT_DBG("Looking for Alt no :%d", alt);
1706 for (i = 0; i < intf->num_altsetting; i++) {
1707 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1708 return &intf->altsetting[i];
1714 static void btusb_work(struct work_struct *work)
1716 struct btusb_data *data = container_of(work, struct btusb_data, work);
1717 struct hci_dev *hdev = data->hdev;
1721 if (data->sco_num > 0) {
1722 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1723 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1725 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1726 usb_kill_anchored_urbs(&data->isoc_anchor);
1730 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1733 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1734 if (hdev->voice_setting & 0x0020) {
1735 static const int alts[3] = { 2, 4, 5 };
1737 new_alts = alts[data->sco_num - 1];
1739 new_alts = data->sco_num;
1741 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
1742 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1743 * many adapters do not support it. Alt 1 appears to
1744 * work for all adapters that do not have alt 6, and
1745 * which work with WBS at all.
1747 new_alts = btusb_find_altsetting(data, 6) ? 6 : 1;
1750 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1751 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
1753 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1754 usb_kill_anchored_urbs(&data->isoc_anchor);
1756 __set_isoc_interface(hdev, 0);
1757 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1758 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1762 static void btusb_waker(struct work_struct *work)
1764 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1767 err = usb_autopm_get_interface(data->intf);
1771 usb_autopm_put_interface(data->intf);
1774 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1776 struct sk_buff *skb;
1779 BT_DBG("%s", hdev->name);
1781 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1783 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
1790 static int btusb_setup_csr(struct hci_dev *hdev)
1792 struct btusb_data *data = hci_get_drvdata(hdev);
1793 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
1794 struct hci_rp_read_local_version *rp;
1795 struct sk_buff *skb;
1796 bool is_fake = false;
1799 BT_DBG("%s", hdev->name);
1801 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1804 int err = PTR_ERR(skb);
1805 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
1809 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1810 bt_dev_err(hdev, "CSR: Local version length mismatch");
1815 rp = (struct hci_rp_read_local_version *)skb->data;
1817 /* Detect a wide host of Chinese controllers that aren't CSR.
1819 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1821 * The main thing they have in common is that these are really popular low-cost
1822 * options that support newer Bluetooth versions but rely on heavy VID/PID
1823 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1825 * We detect actual CSR devices by checking that the HCI manufacturer code
1826 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1827 * HCI rev values always match. As they both store the firmware number.
1829 if (le16_to_cpu(rp->manufacturer) != 10 ||
1830 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
1833 /* Known legit CSR firmware build numbers and their supported BT versions:
1834 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1835 * - 1.2 (0x2) -> 0x04d9, 0x0529
1836 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
1837 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
1838 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
1840 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
1841 * support BT 1.1 only; so it's a dead giveaway when some
1842 * third-party BT 4.0 dongle reuses it.
1844 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
1845 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
1848 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
1849 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
1852 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
1853 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
1856 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
1857 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
1860 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
1861 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
1864 /* Other clones which beat all the above checks */
1865 else if (bcdDevice == 0x0134 &&
1866 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
1867 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
1871 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds...");
1873 /* Generally these clones have big discrepancies between
1874 * advertised features and what's actually supported.
1875 * Probably will need to be expanded in the future;
1876 * without these the controller will lock up.
1878 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1879 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
1881 /* Clear the reset quirk since this is not an actual
1882 * early Bluetooth 1.1 device from CSR.
1884 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1885 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1888 * Special workaround for clones with a Barrot 8041a02 chip,
1889 * these clones are really messed-up:
1890 * 1. Their bulk rx endpoint will never report any data unless
1891 * the device was suspended at least once (yes really).
1892 * 2. They will not wakeup when autosuspended and receiving data
1893 * on their bulk rx endpoint from e.g. a keyboard or mouse
1894 * (IOW remote-wakeup support is broken for the bulk endpoint).
1896 * To fix 1. enable runtime-suspend, force-suspend the
1897 * hci and then wake-it up by disabling runtime-suspend.
1899 * To fix 2. clear the hci's can_wake flag, this way the hci
1900 * will still be autosuspended when it is not open.
1902 if (bcdDevice == 0x8891 &&
1903 le16_to_cpu(rp->lmp_subver) == 0x1012 &&
1904 le16_to_cpu(rp->hci_rev) == 0x0810 &&
1905 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_4_0) {
1906 bt_dev_warn(hdev, "CSR: detected a fake CSR dongle using a Barrot 8041a02 chip, this chip is very buggy and may have issues");
1908 pm_runtime_allow(&data->udev->dev);
1910 ret = pm_runtime_suspend(&data->udev->dev);
1914 bt_dev_err(hdev, "Failed to suspend the device for Barrot 8041a02 receive-issue workaround");
1916 pm_runtime_forbid(&data->udev->dev);
1918 device_set_wakeup_capable(&data->udev->dev, false);
1919 /* Re-enable autosuspend if this was requested */
1920 if (enable_autosuspend)
1921 usb_enable_autosuspend(data->udev);
1930 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1931 struct intel_version *ver)
1933 const struct firmware *fw;
1937 snprintf(fwname, sizeof(fwname),
1938 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1939 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1940 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1941 ver->fw_build_ww, ver->fw_build_yy);
1943 ret = request_firmware(&fw, fwname, &hdev->dev);
1945 if (ret == -EINVAL) {
1946 bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1951 bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1954 /* If the correct firmware patch file is not found, use the
1955 * default firmware patch file instead
1957 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1958 ver->hw_platform, ver->hw_variant);
1959 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1960 bt_dev_err(hdev, "failed to open default fw file: %s",
1966 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
1971 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1972 const struct firmware *fw,
1973 const u8 **fw_ptr, int *disable_patch)
1975 struct sk_buff *skb;
1976 struct hci_command_hdr *cmd;
1977 const u8 *cmd_param;
1978 struct hci_event_hdr *evt = NULL;
1979 const u8 *evt_param = NULL;
1980 int remain = fw->size - (*fw_ptr - fw->data);
1982 /* The first byte indicates the types of the patch command or event.
1983 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1984 * in the current firmware buffer doesn't start with 0x01 or
1985 * the size of remain buffer is smaller than HCI command header,
1986 * the firmware file is corrupted and it should stop the patching
1989 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1990 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
1996 cmd = (struct hci_command_hdr *)(*fw_ptr);
1997 *fw_ptr += sizeof(*cmd);
1998 remain -= sizeof(*cmd);
2000 /* Ensure that the remain firmware data is long enough than the length
2001 * of command parameter. If not, the firmware file is corrupted.
2003 if (remain < cmd->plen) {
2004 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
2008 /* If there is a command that loads a patch in the firmware
2009 * file, then enable the patch upon success, otherwise just
2010 * disable the manufacturer mode, for example patch activation
2011 * is not required when the default firmware patch file is used
2012 * because there are no patch data to load.
2014 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
2017 cmd_param = *fw_ptr;
2018 *fw_ptr += cmd->plen;
2019 remain -= cmd->plen;
2021 /* This reads the expected events when the above command is sent to the
2022 * device. Some vendor commands expects more than one events, for
2023 * example command status event followed by vendor specific event.
2024 * For this case, it only keeps the last expected event. so the command
2025 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
2026 * last expected event.
2028 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
2032 evt = (struct hci_event_hdr *)(*fw_ptr);
2033 *fw_ptr += sizeof(*evt);
2034 remain -= sizeof(*evt);
2036 if (remain < evt->plen) {
2037 bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
2041 evt_param = *fw_ptr;
2042 *fw_ptr += evt->plen;
2043 remain -= evt->plen;
2046 /* Every HCI commands in the firmware file has its correspond event.
2047 * If event is not found or remain is smaller than zero, the firmware
2048 * file is corrupted.
2050 if (!evt || !evt_param || remain < 0) {
2051 bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
2055 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
2056 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
2058 bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
2059 cmd->opcode, PTR_ERR(skb));
2060 return PTR_ERR(skb);
2063 /* It ensures that the returned event matches the event data read from
2064 * the firmware file. At fist, it checks the length and then
2065 * the contents of the event.
2067 if (skb->len != evt->plen) {
2068 bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
2069 le16_to_cpu(cmd->opcode));
2074 if (memcmp(skb->data, evt_param, evt->plen)) {
2075 bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
2076 le16_to_cpu(cmd->opcode));
2085 static int btusb_setup_intel(struct hci_dev *hdev)
2087 struct sk_buff *skb;
2088 const struct firmware *fw;
2090 int disable_patch, err;
2091 struct intel_version ver;
2093 BT_DBG("%s", hdev->name);
2095 /* The controller has a bug with the first HCI command sent to it
2096 * returning number of completed commands as zero. This would stall the
2097 * command processing in the Bluetooth core.
2099 * As a workaround, send HCI Reset command first which will reset the
2100 * number of completed commands and allow normal command processing
2103 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2105 bt_dev_err(hdev, "sending initial HCI reset command failed (%ld)",
2107 return PTR_ERR(skb);
2111 /* Read Intel specific controller version first to allow selection of
2112 * which firmware file to load.
2114 * The returned information are hardware variant and revision plus
2115 * firmware variant, revision and build number.
2117 err = btintel_read_version(hdev, &ver);
2121 bt_dev_info(hdev, "read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
2122 ver.hw_platform, ver.hw_variant, ver.hw_revision,
2123 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
2124 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
2126 /* fw_patch_num indicates the version of patch the device currently
2127 * have. If there is no patch data in the device, it is always 0x00.
2128 * So, if it is other than 0x00, no need to patch the device again.
2130 if (ver.fw_patch_num) {
2131 bt_dev_info(hdev, "Intel device is already patched. "
2132 "patch num: %02x", ver.fw_patch_num);
2136 /* Opens the firmware patch file based on the firmware version read
2137 * from the controller. If it fails to open the matching firmware
2138 * patch file, it tries to open the default firmware patch file.
2139 * If no patch file is found, allow the device to operate without
2142 fw = btusb_setup_intel_get_fw(hdev, &ver);
2147 /* Enable the manufacturer mode of the controller.
2148 * Only while this mode is enabled, the driver can download the
2149 * firmware patch data and configuration parameters.
2151 err = btintel_enter_mfg(hdev);
2153 release_firmware(fw);
2159 /* The firmware data file consists of list of Intel specific HCI
2160 * commands and its expected events. The first byte indicates the
2161 * type of the message, either HCI command or HCI event.
2163 * It reads the command and its expected event from the firmware file,
2164 * and send to the controller. Once __hci_cmd_sync_ev() returns,
2165 * the returned event is compared with the event read from the firmware
2166 * file and it will continue until all the messages are downloaded to
2169 * Once the firmware patching is completed successfully,
2170 * the manufacturer mode is disabled with reset and activating the
2173 * If the firmware patching fails, the manufacturer mode is
2174 * disabled with reset and deactivating the patch.
2176 * If the default patch file is used, no reset is done when disabling
2179 while (fw->size > fw_ptr - fw->data) {
2182 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
2185 goto exit_mfg_deactivate;
2188 release_firmware(fw);
2191 goto exit_mfg_disable;
2193 /* Patching completed successfully and disable the manufacturer mode
2194 * with reset and activate the downloaded firmware patches.
2196 err = btintel_exit_mfg(hdev, true, true);
2200 /* Need build number for downloaded fw patches in
2201 * every power-on boot
2203 err = btintel_read_version(hdev, &ver);
2206 bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
2212 /* Disable the manufacturer mode without reset */
2213 err = btintel_exit_mfg(hdev, false, false);
2217 bt_dev_info(hdev, "Intel firmware patch completed");
2221 exit_mfg_deactivate:
2222 release_firmware(fw);
2224 /* Patching failed. Disable the manufacturer mode with reset and
2225 * deactivate the downloaded firmware patches.
2227 err = btintel_exit_mfg(hdev, true, false);
2231 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
2234 /* Set the event mask for Intel specific vendor events. This enables
2235 * a few extra events that are useful during general operation.
2237 btintel_set_event_mask_mfg(hdev, false);
2239 btintel_check_bdaddr(hdev);
2243 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2245 struct sk_buff *skb;
2246 struct hci_event_hdr *hdr;
2247 struct hci_ev_cmd_complete *evt;
2249 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2253 hdr = skb_put(skb, sizeof(*hdr));
2254 hdr->evt = HCI_EV_CMD_COMPLETE;
2255 hdr->plen = sizeof(*evt) + 1;
2257 evt = skb_put(skb, sizeof(*evt));
2259 evt->opcode = cpu_to_le16(opcode);
2261 skb_put_u8(skb, 0x00);
2263 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2265 return hci_recv_frame(hdev, skb);
2268 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2271 /* When the device is in bootloader mode, then it can send
2272 * events via the bulk endpoint. These events are treated the
2273 * same way as the ones received from the interrupt endpoint.
2275 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
2276 return btusb_recv_intr(data, buffer, count);
2278 return btusb_recv_bulk(data, buffer, count);
2281 static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
2284 const struct intel_bootup *evt = ptr;
2286 if (len != sizeof(*evt))
2289 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags))
2290 wake_up_bit(&data->flags, BTUSB_BOOTING);
2293 static void btusb_intel_secure_send_result(struct btusb_data *data,
2294 const void *ptr, unsigned int len)
2296 const struct intel_secure_send_result *evt = ptr;
2298 if (len != sizeof(*evt))
2302 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
2304 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
2305 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags))
2306 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
2309 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2311 struct btusb_data *data = hci_get_drvdata(hdev);
2313 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2314 struct hci_event_hdr *hdr = (void *)skb->data;
2316 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2318 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2319 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
2321 switch (skb->data[2]) {
2323 /* When switching to the operational firmware
2324 * the device sends a vendor specific event
2325 * indicating that the bootup completed.
2327 btusb_intel_bootup(data, ptr, len);
2330 /* When the firmware loading completes the
2331 * device sends out a vendor specific event
2332 * indicating the result of the firmware
2335 btusb_intel_secure_send_result(data, ptr, len);
2341 return hci_recv_frame(hdev, skb);
2344 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2346 struct btusb_data *data = hci_get_drvdata(hdev);
2349 BT_DBG("%s", hdev->name);
2351 switch (hci_skb_pkt_type(skb)) {
2352 case HCI_COMMAND_PKT:
2353 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2354 struct hci_command_hdr *cmd = (void *)skb->data;
2355 __u16 opcode = le16_to_cpu(cmd->opcode);
2357 /* When in bootloader mode and the command 0xfc09
2358 * is received, it needs to be send down the
2359 * bulk endpoint. So allocate a bulk URB instead.
2361 if (opcode == 0xfc09)
2362 urb = alloc_bulk_urb(hdev, skb);
2364 urb = alloc_ctrl_urb(hdev, skb);
2366 /* When the 0xfc01 command is issued to boot into
2367 * the operational firmware, it will actually not
2368 * send a command complete event. To keep the flow
2369 * control working inject that event here.
2371 if (opcode == 0xfc01)
2372 inject_cmd_complete(hdev, opcode);
2374 urb = alloc_ctrl_urb(hdev, skb);
2377 return PTR_ERR(urb);
2379 hdev->stat.cmd_tx++;
2380 return submit_or_queue_tx_urb(hdev, urb);
2382 case HCI_ACLDATA_PKT:
2383 urb = alloc_bulk_urb(hdev, skb);
2385 return PTR_ERR(urb);
2387 hdev->stat.acl_tx++;
2388 return submit_or_queue_tx_urb(hdev, urb);
2390 case HCI_SCODATA_PKT:
2391 if (hci_conn_num(hdev, SCO_LINK) < 1)
2394 urb = alloc_isoc_urb(hdev, skb);
2396 return PTR_ERR(urb);
2398 hdev->stat.sco_tx++;
2399 return submit_tx_urb(hdev, urb);
2405 static bool btusb_setup_intel_new_get_fw_name(struct intel_version *ver,
2406 struct intel_boot_params *params,
2407 char *fw_name, size_t len,
2410 switch (ver->hw_variant) {
2411 case 0x0b: /* SfP */
2412 case 0x0c: /* WsP */
2413 snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
2414 le16_to_cpu(ver->hw_variant),
2415 le16_to_cpu(params->dev_revid),
2418 case 0x11: /* JfP */
2419 case 0x12: /* ThP */
2420 case 0x13: /* HrP */
2421 case 0x14: /* CcP */
2422 snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
2423 le16_to_cpu(ver->hw_variant),
2424 le16_to_cpu(ver->hw_revision),
2425 le16_to_cpu(ver->fw_revision),
2434 static void btusb_setup_intel_newgen_get_fw_name(const struct intel_version_tlv *ver_tlv,
2435 char *fw_name, size_t len,
2438 /* The firmware file name for new generation controllers will be
2439 * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
2441 snprintf(fw_name, len, "intel/ibt-%04x-%04x.%s",
2442 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver_tlv->cnvi_top),
2443 INTEL_CNVX_TOP_STEP(ver_tlv->cnvi_top)),
2444 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver_tlv->cnvr_top),
2445 INTEL_CNVX_TOP_STEP(ver_tlv->cnvr_top)),
2449 static int btusb_intel_download_firmware_newgen(struct hci_dev *hdev,
2450 struct intel_version_tlv *ver,
2453 const struct firmware *fw;
2456 struct btusb_data *data = hci_get_drvdata(hdev);
2458 if (!ver || !boot_param)
2461 /* The hardware platform number has a fixed value of 0x37 and
2462 * for now only accept this single value.
2464 if (INTEL_HW_PLATFORM(ver->cnvi_bt) != 0x37) {
2465 bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
2466 INTEL_HW_PLATFORM(ver->cnvi_bt));
2470 /* The firmware variant determines if the device is in bootloader
2471 * mode or is running operational firmware. The value 0x03 identifies
2472 * the bootloader and the value 0x23 identifies the operational
2475 * When the operational firmware is already present, then only
2476 * the check for valid Bluetooth device address is needed. This
2477 * determines if the device will be added as configured or
2478 * unconfigured controller.
2480 * It is not possible to use the Secure Boot Parameters in this
2481 * case since that command is only available in bootloader mode.
2483 if (ver->img_type == 0x03) {
2484 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2485 btintel_check_bdaddr(hdev);
2489 /* Check for supported iBT hardware variants of this firmware
2492 * This check has been put in place to ensure correct forward
2493 * compatibility options when newer hardware variants come along.
2495 switch (INTEL_HW_VARIANT(ver->cnvi_bt)) {
2496 case 0x17: /* TyP */
2497 case 0x18: /* Slr */
2498 case 0x19: /* Slr-F */
2501 bt_dev_err(hdev, "Unsupported Intel hardware variant (0x%x)",
2502 INTEL_HW_VARIANT(ver->cnvi_bt));
2506 /* If the device is not in bootloader mode, then the only possible
2507 * choice is to return an error and abort the device initialization.
2509 if (ver->img_type != 0x01) {
2510 bt_dev_err(hdev, "Unsupported Intel firmware variant (0x%x)",
2515 /* It is required that every single firmware fragment is acknowledged
2516 * with a command complete event. If the boot parameters indicate
2517 * that this bootloader does not send them, then abort the setup.
2519 if (ver->limited_cce != 0x00) {
2520 bt_dev_err(hdev, "Unsupported Intel firmware loading method (0x%x)",
2525 /* Secure boot engine type should be either 1 (ECDSA) or 0 (RSA) */
2526 if (ver->sbe_type > 0x01) {
2527 bt_dev_err(hdev, "Unsupported Intel secure boot engine type (0x%x)",
2532 /* If the OTP has no valid Bluetooth device address, then there will
2533 * also be no valid address for the operational firmware.
2535 if (!bacmp(&ver->otp_bd_addr, BDADDR_ANY)) {
2536 bt_dev_info(hdev, "No device address configured");
2537 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2540 btusb_setup_intel_newgen_get_fw_name(ver, fwname, sizeof(fwname), "sfi");
2541 err = request_firmware(&fw, fwname, &hdev->dev);
2543 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2548 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2550 if (fw->size < 644) {
2551 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2557 set_bit(BTUSB_DOWNLOADING, &data->flags);
2559 /* Start firmware downloading and get boot parameter */
2560 err = btintel_download_firmware_newgen(hdev, fw, boot_param,
2561 INTEL_HW_VARIANT(ver->cnvi_bt),
2564 /* When FW download fails, send Intel Reset to retry
2567 btintel_reset_to_bootloader(hdev);
2570 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2572 bt_dev_info(hdev, "Waiting for firmware download to complete");
2574 /* Before switching the device into operational mode and with that
2575 * booting the loaded firmware, wait for the bootloader notification
2576 * that all fragments have been successfully received.
2578 * When the event processing receives the notification, then the
2579 * BTUSB_DOWNLOADING flag will be cleared.
2581 * The firmware loading should not take longer than 5 seconds
2582 * and thus just timeout if that happens and fail the setup
2585 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2587 msecs_to_jiffies(5000));
2588 if (err == -EINTR) {
2589 bt_dev_err(hdev, "Firmware loading interrupted");
2594 bt_dev_err(hdev, "Firmware loading timeout");
2596 btintel_reset_to_bootloader(hdev);
2600 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2601 bt_dev_err(hdev, "Firmware loading failed");
2607 release_firmware(fw);
2611 static int btusb_intel_download_firmware(struct hci_dev *hdev,
2612 struct intel_version *ver,
2613 struct intel_boot_params *params,
2616 const struct firmware *fw;
2619 struct btusb_data *data = hci_get_drvdata(hdev);
2621 if (!ver || !params)
2624 /* The hardware platform number has a fixed value of 0x37 and
2625 * for now only accept this single value.
2627 if (ver->hw_platform != 0x37) {
2628 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
2633 /* Check for supported iBT hardware variants of this firmware
2636 * This check has been put in place to ensure correct forward
2637 * compatibility options when newer hardware variants come along.
2639 switch (ver->hw_variant) {
2640 case 0x0b: /* SfP */
2641 case 0x0c: /* WsP */
2642 case 0x11: /* JfP */
2643 case 0x12: /* ThP */
2644 case 0x13: /* HrP */
2645 case 0x14: /* CcP */
2648 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
2653 btintel_version_info(hdev, ver);
2655 /* The firmware variant determines if the device is in bootloader
2656 * mode or is running operational firmware. The value 0x06 identifies
2657 * the bootloader and the value 0x23 identifies the operational
2660 * When the operational firmware is already present, then only
2661 * the check for valid Bluetooth device address is needed. This
2662 * determines if the device will be added as configured or
2663 * unconfigured controller.
2665 * It is not possible to use the Secure Boot Parameters in this
2666 * case since that command is only available in bootloader mode.
2668 if (ver->fw_variant == 0x23) {
2669 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2670 btintel_check_bdaddr(hdev);
2674 /* If the device is not in bootloader mode, then the only possible
2675 * choice is to return an error and abort the device initialization.
2677 if (ver->fw_variant != 0x06) {
2678 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
2683 /* Read the secure boot parameters to identify the operating
2684 * details of the bootloader.
2686 err = btintel_read_boot_params(hdev, params);
2690 /* It is required that every single firmware fragment is acknowledged
2691 * with a command complete event. If the boot parameters indicate
2692 * that this bootloader does not send them, then abort the setup.
2694 if (params->limited_cce != 0x00) {
2695 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
2696 params->limited_cce);
2700 /* If the OTP has no valid Bluetooth device address, then there will
2701 * also be no valid address for the operational firmware.
2703 if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) {
2704 bt_dev_info(hdev, "No device address configured");
2705 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2708 /* With this Intel bootloader only the hardware variant and device
2709 * revision information are used to select the right firmware for SfP
2712 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2714 * Currently the supported hardware variants are:
2715 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2716 * 12 (0x0c) for iBT3.5 (WsP)
2718 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2719 * variant, HW revision and FW revision, as these are dependent on CNVi
2720 * and RF Combination.
2722 * 17 (0x11) for iBT3.5 (JfP)
2723 * 18 (0x12) for iBT3.5 (ThP)
2725 * The firmware file name for these will be
2726 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2729 err = btusb_setup_intel_new_get_fw_name(ver, params, fwname,
2730 sizeof(fwname), "sfi");
2732 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2736 err = request_firmware(&fw, fwname, &hdev->dev);
2738 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2743 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2745 if (fw->size < 644) {
2746 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2752 set_bit(BTUSB_DOWNLOADING, &data->flags);
2754 /* Start firmware downloading and get boot parameter */
2755 err = btintel_download_firmware(hdev, fw, boot_param);
2757 /* When FW download fails, send Intel Reset to retry
2760 btintel_reset_to_bootloader(hdev);
2763 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2765 bt_dev_info(hdev, "Waiting for firmware download to complete");
2767 /* Before switching the device into operational mode and with that
2768 * booting the loaded firmware, wait for the bootloader notification
2769 * that all fragments have been successfully received.
2771 * When the event processing receives the notification, then the
2772 * BTUSB_DOWNLOADING flag will be cleared.
2774 * The firmware loading should not take longer than 5 seconds
2775 * and thus just timeout if that happens and fail the setup
2778 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2780 msecs_to_jiffies(5000));
2781 if (err == -EINTR) {
2782 bt_dev_err(hdev, "Firmware loading interrupted");
2787 bt_dev_err(hdev, "Firmware loading timeout");
2789 btintel_reset_to_bootloader(hdev);
2793 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2794 bt_dev_err(hdev, "Firmware loading failed");
2800 release_firmware(fw);
2804 static int btusb_setup_intel_new(struct hci_dev *hdev)
2806 struct btusb_data *data = hci_get_drvdata(hdev);
2807 struct intel_version ver;
2808 struct intel_boot_params params;
2811 ktime_t calltime, delta, rettime;
2812 unsigned long long duration;
2814 struct intel_debug_features features;
2816 BT_DBG("%s", hdev->name);
2818 /* Set the default boot parameter to 0x0 and it is updated to
2819 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2820 * command while downloading the firmware.
2822 boot_param = 0x00000000;
2824 calltime = ktime_get();
2826 /* Read the Intel version information to determine if the device
2827 * is in bootloader mode or if it already has operational firmware
2830 err = btintel_read_version(hdev, &ver);
2832 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2833 btintel_reset_to_bootloader(hdev);
2837 err = btusb_intel_download_firmware(hdev, &ver, ¶ms, &boot_param);
2841 /* controller is already having an operational firmware */
2842 if (ver.fw_variant == 0x23)
2845 rettime = ktime_get();
2846 delta = ktime_sub(rettime, calltime);
2847 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2849 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
2851 calltime = ktime_get();
2853 set_bit(BTUSB_BOOTING, &data->flags);
2855 err = btintel_send_intel_reset(hdev, boot_param);
2857 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
2858 btintel_reset_to_bootloader(hdev);
2862 /* The bootloader will not indicate when the device is ready. This
2863 * is done by the operational firmware sending bootup notification.
2865 * Booting into operational firmware should not take longer than
2866 * 1 second. However if that happens, then just fail the setup
2867 * since something went wrong.
2869 bt_dev_info(hdev, "Waiting for device to boot");
2871 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2873 msecs_to_jiffies(1000));
2875 if (err == -EINTR) {
2876 bt_dev_err(hdev, "Device boot interrupted");
2881 bt_dev_err(hdev, "Device boot timeout");
2882 btintel_reset_to_bootloader(hdev);
2886 rettime = ktime_get();
2887 delta = ktime_sub(rettime, calltime);
2888 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2890 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
2892 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2894 err = btusb_setup_intel_new_get_fw_name(&ver, ¶ms, ddcname,
2895 sizeof(ddcname), "ddc");
2898 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2900 /* Once the device is running in operational mode, it needs to
2901 * apply the device configuration (DDC) parameters.
2903 * The device can work without DDC parameters, so even if it
2904 * fails to load the file, no need to fail the setup.
2906 btintel_load_ddc_config(hdev, ddcname);
2909 /* Read the Intel supported features and if new exception formats
2910 * supported, need to load the additional DDC config to enable.
2912 btintel_read_debug_features(hdev, &features);
2914 /* Set DDC mask for available debug features */
2915 btintel_set_debug_features(hdev, &features);
2917 /* Read the Intel version information after loading the FW */
2918 err = btintel_read_version(hdev, &ver);
2922 btintel_version_info(hdev, &ver);
2925 /* All Intel controllers that support the Microsoft vendor
2926 * extension are using 0xFC1E for VsMsftOpCode.
2928 switch (ver.hw_variant) {
2929 case 0x11: /* JfP */
2930 case 0x12: /* ThP */
2931 case 0x13: /* HrP */
2932 case 0x14: /* CcP */
2933 hci_set_msft_opcode(hdev, 0xFC1E);
2937 /* Set the event mask for Intel specific vendor events. This enables
2938 * a few extra events that are useful during general operation. It
2939 * does not enable any debugging related events.
2941 * The device will function correctly without these events enabled
2942 * and thus no need to fail the setup.
2944 btintel_set_event_mask(hdev, false);
2949 static int btusb_setup_intel_newgen(struct hci_dev *hdev)
2951 struct btusb_data *data = hci_get_drvdata(hdev);
2954 ktime_t calltime, delta, rettime;
2955 unsigned long long duration;
2957 struct intel_debug_features features;
2958 struct intel_version_tlv version;
2960 bt_dev_dbg(hdev, "");
2962 /* Set the default boot parameter to 0x0 and it is updated to
2963 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2964 * command while downloading the firmware.
2966 boot_param = 0x00000000;
2968 calltime = ktime_get();
2970 /* Read the Intel version information to determine if the device
2971 * is in bootloader mode or if it already has operational firmware
2974 err = btintel_read_version_tlv(hdev, &version);
2976 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2977 btintel_reset_to_bootloader(hdev);
2981 btintel_version_info_tlv(hdev, &version);
2983 err = btusb_intel_download_firmware_newgen(hdev, &version, &boot_param);
2987 /* check if controller is already having an operational firmware */
2988 if (version.img_type == 0x03)
2991 rettime = ktime_get();
2992 delta = ktime_sub(rettime, calltime);
2993 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2995 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
2997 calltime = ktime_get();
2999 set_bit(BTUSB_BOOTING, &data->flags);
3001 err = btintel_send_intel_reset(hdev, boot_param);
3003 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
3004 btintel_reset_to_bootloader(hdev);
3008 /* The bootloader will not indicate when the device is ready. This
3009 * is done by the operational firmware sending bootup notification.
3011 * Booting into operational firmware should not take longer than
3012 * 1 second. However if that happens, then just fail the setup
3013 * since something went wrong.
3015 bt_dev_info(hdev, "Waiting for device to boot");
3017 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
3019 msecs_to_jiffies(1000));
3021 if (err == -EINTR) {
3022 bt_dev_err(hdev, "Device boot interrupted");
3027 bt_dev_err(hdev, "Device boot timeout");
3028 btintel_reset_to_bootloader(hdev);
3032 rettime = ktime_get();
3033 delta = ktime_sub(rettime, calltime);
3034 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3036 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
3038 clear_bit(BTUSB_BOOTLOADER, &data->flags);
3040 btusb_setup_intel_newgen_get_fw_name(&version, ddcname, sizeof(ddcname),
3042 /* Once the device is running in operational mode, it needs to
3043 * apply the device configuration (DDC) parameters.
3045 * The device can work without DDC parameters, so even if it
3046 * fails to load the file, no need to fail the setup.
3048 btintel_load_ddc_config(hdev, ddcname);
3050 /* Read the Intel supported features and if new exception formats
3051 * supported, need to load the additional DDC config to enable.
3053 btintel_read_debug_features(hdev, &features);
3055 /* Set DDC mask for available debug features */
3056 btintel_set_debug_features(hdev, &features);
3058 /* Read the Intel version information after loading the FW */
3059 err = btintel_read_version_tlv(hdev, &version);
3063 btintel_version_info_tlv(hdev, &version);
3066 /* Set the event mask for Intel specific vendor events. This enables
3067 * a few extra events that are useful during general operation. It
3068 * does not enable any debugging related events.
3070 * The device will function correctly without these events enabled
3071 * and thus no need to fail the setup.
3073 btintel_set_event_mask(hdev, false);
3077 static int btusb_shutdown_intel(struct hci_dev *hdev)
3079 struct sk_buff *skb;
3082 /* In the shutdown sequence where Bluetooth is turned off followed
3083 * by WiFi being turned off, turning WiFi back on causes issue with
3084 * the RF calibration.
3086 * To ensure that any RF activity has been stopped, issue HCI Reset
3087 * command to clear all ongoing activity including advertising,
3090 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3093 bt_dev_err(hdev, "HCI reset during shutdown failed");
3098 /* Some platforms have an issue with BT LED when the interface is
3099 * down or BT radio is turned off, which takes 5 seconds to BT LED
3100 * goes off. This command turns off the BT LED immediately.
3102 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
3105 bt_dev_err(hdev, "turning off Intel device LED failed");
3113 static int btusb_shutdown_intel_new(struct hci_dev *hdev)
3115 struct sk_buff *skb;
3117 /* Send HCI Reset to the controller to stop any BT activity which
3118 * were triggered. This will help to save power and maintain the
3119 * sync b/w Host and controller
3121 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3123 bt_dev_err(hdev, "HCI reset during shutdown failed");
3124 return PTR_ERR(skb);
3131 #define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
3132 #define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
3134 #define HCI_WMT_MAX_EVENT_SIZE 64
3135 /* It is for mt79xx download rom patch*/
3136 #define MTK_FW_ROM_PATCH_HEADER_SIZE 32
3137 #define MTK_FW_ROM_PATCH_GD_SIZE 64
3138 #define MTK_FW_ROM_PATCH_SEC_MAP_SIZE 64
3139 #define MTK_SEC_MAP_COMMON_SIZE 12
3140 #define MTK_SEC_MAP_NEED_SEND_SIZE 52
3143 BTMTK_WMT_PATCH_DWNLD = 0x1,
3144 BTMTK_WMT_FUNC_CTRL = 0x6,
3145 BTMTK_WMT_RST = 0x7,
3146 BTMTK_WMT_SEMAPHORE = 0x17,
3151 BTMTK_WMT_PATCH_UNDONE,
3152 BTMTK_WMT_PATCH_PROGRESS,
3153 BTMTK_WMT_PATCH_DONE,
3154 BTMTK_WMT_ON_UNDONE,
3156 BTMTK_WMT_ON_PROGRESS,
3159 struct btmtk_wmt_hdr {
3166 struct btmtk_hci_wmt_cmd {
3167 struct btmtk_wmt_hdr hdr;
3171 struct btmtk_hci_wmt_evt {
3172 struct hci_event_hdr hhdr;
3173 struct btmtk_wmt_hdr whdr;
3176 struct btmtk_hci_wmt_evt_funcc {
3177 struct btmtk_hci_wmt_evt hwhdr;
3181 struct btmtk_tci_sleep {
3184 __le16 host_duration;
3186 u8 time_compensation;
3189 struct btmtk_hci_wmt_params {
3197 struct btmtk_patch_header {
3205 struct btmtk_global_desc {
3212 struct btmtk_section_map {
3217 __le32 u4SecSpec[13];
3224 __le32 dlmodecrctype;
3231 static void btusb_mtk_wmt_recv(struct urb *urb)
3233 struct hci_dev *hdev = urb->context;
3234 struct btusb_data *data = hci_get_drvdata(hdev);
3235 struct hci_event_hdr *hdr;
3236 struct sk_buff *skb;
3239 if (urb->status == 0 && urb->actual_length > 0) {
3240 hdev->stat.byte_rx += urb->actual_length;
3242 /* WMT event shouldn't be fragmented and the size should be
3243 * less than HCI_WMT_MAX_EVENT_SIZE.
3245 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
3247 hdev->stat.err_rx++;
3251 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
3252 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
3254 hdr = (void *)skb->data;
3255 /* Fix up the vendor event id with 0xff for vendor specific
3256 * instead of 0xe4 so that event send via monitoring socket can
3257 * be parsed properly.
3261 /* When someone waits for the WMT event, the skb is being cloned
3262 * and being processed the events from there then.
3264 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
3265 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
3266 if (!data->evt_skb) {
3272 err = hci_recv_frame(hdev, skb);
3274 kfree_skb(data->evt_skb);
3275 data->evt_skb = NULL;
3279 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
3281 /* Barrier to sync with other CPUs */
3282 smp_mb__after_atomic();
3283 wake_up_bit(&data->flags,
3284 BTUSB_TX_WAIT_VND_EVT);
3287 } else if (urb->status == -ENOENT) {
3288 /* Avoid suspend failed when usb_kill_urb */
3292 usb_mark_last_busy(data->udev);
3294 /* The URB complete handler is still called with urb->actual_length = 0
3295 * when the event is not available, so we should keep re-submitting
3296 * URB until WMT event returns, Also, It's necessary to wait some time
3297 * between the two consecutive control URBs to relax the target device
3298 * to generate the event. Otherwise, the WMT event cannot return from
3299 * the device successfully.
3303 usb_anchor_urb(urb, &data->ctrl_anchor);
3304 err = usb_submit_urb(urb, GFP_ATOMIC);
3306 /* -EPERM: urb is being killed;
3307 * -ENODEV: device got disconnected
3309 if (err != -EPERM && err != -ENODEV)
3310 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
3312 usb_unanchor_urb(urb);
3316 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
3318 struct btusb_data *data = hci_get_drvdata(hdev);
3319 struct usb_ctrlrequest *dr;
3325 urb = usb_alloc_urb(0, GFP_KERNEL);
3329 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
3335 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
3337 dr->wIndex = cpu_to_le16(0);
3338 dr->wValue = cpu_to_le16(48);
3339 dr->wLength = cpu_to_le16(size);
3341 buf = kmalloc(size, GFP_KERNEL);
3348 pipe = usb_rcvctrlpipe(data->udev, 0);
3350 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
3351 buf, size, btusb_mtk_wmt_recv, hdev);
3353 urb->transfer_flags |= URB_FREE_BUFFER;
3355 usb_anchor_urb(urb, &data->ctrl_anchor);
3356 err = usb_submit_urb(urb, GFP_KERNEL);
3358 if (err != -EPERM && err != -ENODEV)
3359 bt_dev_err(hdev, "urb %p submission failed (%d)",
3361 usb_unanchor_urb(urb);
3369 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
3370 struct btmtk_hci_wmt_params *wmt_params)
3372 struct btusb_data *data = hci_get_drvdata(hdev);
3373 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
3374 u32 hlen, status = BTMTK_WMT_INVALID;
3375 struct btmtk_hci_wmt_evt *wmt_evt;
3376 struct btmtk_hci_wmt_cmd *wc;
3377 struct btmtk_wmt_hdr *hdr;
3380 /* Submit control IN URB on demand to process the WMT event */
3381 err = btusb_mtk_submit_wmt_recv_urb(hdev);
3385 /* Send the WMT command and wait until the WMT event returns */
3386 hlen = sizeof(*hdr) + wmt_params->dlen;
3390 wc = kzalloc(hlen, GFP_KERNEL);
3396 hdr->op = wmt_params->op;
3397 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
3398 hdr->flag = wmt_params->flag;
3399 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
3401 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3403 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
3406 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3410 /* The vendor specific WMT commands are all answered by a vendor
3411 * specific event and will have the Command Status or Command
3412 * Complete as with usual HCI command flow control.
3414 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
3415 * state to be cleared. The driver specific event receive routine
3416 * will clear that state and with that indicate completion of the
3419 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
3420 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
3421 if (err == -EINTR) {
3422 bt_dev_err(hdev, "Execution of wmt command interrupted");
3423 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3428 bt_dev_err(hdev, "Execution of wmt command timed out");
3429 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3434 /* Parse and handle the return WMT event */
3435 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
3436 if (wmt_evt->whdr.op != hdr->op) {
3437 bt_dev_err(hdev, "Wrong op received %d expected %d",
3438 wmt_evt->whdr.op, hdr->op);
3443 switch (wmt_evt->whdr.op) {
3444 case BTMTK_WMT_SEMAPHORE:
3445 if (wmt_evt->whdr.flag == 2)
3446 status = BTMTK_WMT_PATCH_UNDONE;
3448 status = BTMTK_WMT_PATCH_DONE;
3450 case BTMTK_WMT_FUNC_CTRL:
3451 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
3452 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
3453 status = BTMTK_WMT_ON_DONE;
3454 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
3455 status = BTMTK_WMT_ON_PROGRESS;
3457 status = BTMTK_WMT_ON_UNDONE;
3459 case BTMTK_WMT_PATCH_DWNLD:
3460 if (wmt_evt->whdr.flag == 2)
3461 status = BTMTK_WMT_PATCH_DONE;
3462 else if (wmt_evt->whdr.flag == 1)
3463 status = BTMTK_WMT_PATCH_PROGRESS;
3465 status = BTMTK_WMT_PATCH_UNDONE;
3469 if (wmt_params->status)
3470 *wmt_params->status = status;
3473 kfree_skb(data->evt_skb);
3474 data->evt_skb = NULL;
3480 static int btusb_mtk_setup_firmware_79xx(struct hci_dev *hdev, const char *fwname)
3482 struct btmtk_hci_wmt_params wmt_params;
3483 struct btmtk_global_desc *globaldesc = NULL;
3484 struct btmtk_section_map *sectionmap;
3485 const struct firmware *fw;
3487 const u8 *fw_bin_ptr;
3488 int err, dlen, i, status;
3489 u8 flag, first_block, retry;
3490 u32 section_num, dl_size, section_offset;
3493 err = request_firmware(&fw, fwname, &hdev->dev);
3495 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
3500 fw_bin_ptr = fw_ptr;
3501 globaldesc = (struct btmtk_global_desc *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE);
3502 section_num = le32_to_cpu(globaldesc->section_num);
3504 for (i = 0; i < section_num; i++) {
3506 fw_ptr = fw_bin_ptr;
3507 sectionmap = (struct btmtk_section_map *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
3508 MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i);
3510 section_offset = le32_to_cpu(sectionmap->secoffset);
3511 dl_size = le32_to_cpu(sectionmap->bin_info_spec.dlsize);
3516 cmd[0] = 0; /* 0 means legacy dl mode. */
3518 fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
3519 MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i +
3520 MTK_SEC_MAP_COMMON_SIZE,
3521 MTK_SEC_MAP_NEED_SEND_SIZE + 1);
3523 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3524 wmt_params.status = &status;
3525 wmt_params.flag = 0;
3526 wmt_params.dlen = MTK_SEC_MAP_NEED_SEND_SIZE + 1;
3527 wmt_params.data = &cmd;
3529 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3531 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3533 goto err_release_fw;
3536 if (status == BTMTK_WMT_PATCH_UNDONE) {
3538 } else if (status == BTMTK_WMT_PATCH_PROGRESS) {
3541 } else if (status == BTMTK_WMT_PATCH_DONE) {
3544 bt_dev_err(hdev, "Failed wmt patch dwnld status (%d)",
3546 goto err_release_fw;
3550 fw_ptr += section_offset;
3551 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3552 wmt_params.status = NULL;
3554 while (dl_size > 0) {
3555 dlen = min_t(int, 250, dl_size);
3556 if (first_block == 1) {
3559 } else if (dl_size - dlen <= 0) {
3565 wmt_params.flag = flag;
3566 wmt_params.dlen = dlen;
3567 wmt_params.data = fw_ptr;
3569 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3571 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3573 goto err_release_fw;
3583 /* Wait a few moments for firmware activation done */
3584 usleep_range(100000, 120000);
3587 release_firmware(fw);
3592 static int btusb_mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
3594 struct btmtk_hci_wmt_params wmt_params;
3595 const struct firmware *fw;
3601 err = request_firmware(&fw, fwname, &hdev->dev);
3603 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
3607 /* Power on data RAM the firmware relies on. */
3609 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3610 wmt_params.flag = 3;
3611 wmt_params.dlen = sizeof(param);
3612 wmt_params.data = ¶m;
3613 wmt_params.status = NULL;
3615 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3617 bt_dev_err(hdev, "Failed to power on data RAM (%d)", err);
3618 goto err_release_fw;
3624 /* The size of patch header is 30 bytes, should be skip */
3627 goto err_release_fw;
3634 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3635 wmt_params.status = NULL;
3637 while (fw_size > 0) {
3638 dlen = min_t(int, 250, fw_size);
3640 /* Tell device the position in sequence */
3641 if (fw_size - dlen <= 0)
3643 else if (fw_size < fw->size - 30)
3646 wmt_params.flag = flag;
3647 wmt_params.dlen = dlen;
3648 wmt_params.data = fw_ptr;
3650 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3652 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3654 goto err_release_fw;
3661 wmt_params.op = BTMTK_WMT_RST;
3662 wmt_params.flag = 4;
3663 wmt_params.dlen = 0;
3664 wmt_params.data = NULL;
3665 wmt_params.status = NULL;
3667 /* Activate funciton the firmware providing to */
3668 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3670 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
3671 goto err_release_fw;
3674 /* Wait a few moments for firmware activation done */
3675 usleep_range(10000, 12000);
3678 release_firmware(fw);
3683 static int btusb_mtk_func_query(struct hci_dev *hdev)
3685 struct btmtk_hci_wmt_params wmt_params;
3689 /* Query whether the function is enabled */
3690 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3691 wmt_params.flag = 4;
3692 wmt_params.dlen = sizeof(param);
3693 wmt_params.data = ¶m;
3694 wmt_params.status = &status;
3696 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3698 bt_dev_err(hdev, "Failed to query function status (%d)", err);
3705 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
3707 int pipe, err, size = sizeof(u32);
3710 buf = kzalloc(size, GFP_KERNEL);
3714 pipe = usb_rcvctrlpipe(data->udev, 0);
3715 err = usb_control_msg(data->udev, pipe, 0x63,
3716 USB_TYPE_VENDOR | USB_DIR_IN,
3717 reg >> 16, reg & 0xffff,
3718 buf, size, USB_CTRL_SET_TIMEOUT);
3722 *val = get_unaligned_le32(buf);
3730 static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
3732 return btusb_mtk_reg_read(data, reg, id);
3735 static int btusb_mtk_setup(struct hci_dev *hdev)
3737 struct btusb_data *data = hci_get_drvdata(hdev);
3738 struct btmtk_hci_wmt_params wmt_params;
3739 ktime_t calltime, delta, rettime;
3740 struct btmtk_tci_sleep tci_sleep;
3741 unsigned long long duration;
3742 struct sk_buff *skb;
3746 char fw_bin_name[64];
3750 calltime = ktime_get();
3752 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
3754 bt_dev_err(hdev, "Failed to get device id (%d)", err);
3759 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
3761 bt_dev_err(hdev, "Failed to get device id (%d)", err);
3764 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
3766 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
3773 fwname = FIRMWARE_MT7663;
3776 fwname = FIRMWARE_MT7668;
3779 snprintf(fw_bin_name, sizeof(fw_bin_name),
3780 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
3781 dev_id & 0xffff, (fw_version & 0xff) + 1);
3782 err = btusb_mtk_setup_firmware_79xx(hdev, fw_bin_name);
3784 /* Enable Bluetooth protocol */
3786 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3787 wmt_params.flag = 0;
3788 wmt_params.dlen = sizeof(param);
3789 wmt_params.data = ¶m;
3790 wmt_params.status = NULL;
3792 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3794 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3799 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
3804 /* Query whether the firmware is already download */
3805 wmt_params.op = BTMTK_WMT_SEMAPHORE;
3806 wmt_params.flag = 1;
3807 wmt_params.dlen = 0;
3808 wmt_params.data = NULL;
3809 wmt_params.status = &status;
3811 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3813 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
3817 if (status == BTMTK_WMT_PATCH_DONE) {
3818 bt_dev_info(hdev, "firmware already downloaded");
3819 goto ignore_setup_fw;
3822 /* Setup a firmware which the device definitely requires */
3823 err = btusb_mtk_setup_firmware(hdev, fwname);
3828 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
3829 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
3831 /* -ETIMEDOUT happens */
3835 /* The other errors happen in btusb_mtk_func_query */
3839 if (status == BTMTK_WMT_ON_DONE) {
3840 bt_dev_info(hdev, "function already on");
3841 goto ignore_func_on;
3844 /* Enable Bluetooth protocol */
3846 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3847 wmt_params.flag = 0;
3848 wmt_params.dlen = sizeof(param);
3849 wmt_params.data = ¶m;
3850 wmt_params.status = NULL;
3852 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3854 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3859 /* Apply the low power environment setup */
3860 tci_sleep.mode = 0x5;
3861 tci_sleep.duration = cpu_to_le16(0x640);
3862 tci_sleep.host_duration = cpu_to_le16(0x640);
3863 tci_sleep.host_wakeup_pin = 0;
3864 tci_sleep.time_compensation = 0;
3866 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
3870 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
3876 rettime = ktime_get();
3877 delta = ktime_sub(rettime, calltime);
3878 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3880 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
3885 static int btusb_mtk_shutdown(struct hci_dev *hdev)
3887 struct btmtk_hci_wmt_params wmt_params;
3891 /* Disable the device */
3892 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3893 wmt_params.flag = 0;
3894 wmt_params.dlen = sizeof(param);
3895 wmt_params.data = ¶m;
3896 wmt_params.status = NULL;
3898 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3900 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3907 MODULE_FIRMWARE(FIRMWARE_MT7663);
3908 MODULE_FIRMWARE(FIRMWARE_MT7668);
3911 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3912 static int marvell_config_oob_wake(struct hci_dev *hdev)
3914 struct sk_buff *skb;
3915 struct btusb_data *data = hci_get_drvdata(hdev);
3916 struct device *dev = &data->udev->dev;
3917 u16 pin, gap, opcode;
3921 /* Move on if no wakeup pin specified */
3922 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3923 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3926 /* Vendor specific command to configure a GPIO as wake-up pin */
3927 opcode = hci_opcode_pack(0x3F, 0x59);
3928 cmd[0] = opcode & 0xFF;
3929 cmd[1] = opcode >> 8;
3930 cmd[2] = 2; /* length of parameters that follow */
3932 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3934 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3936 bt_dev_err(hdev, "%s: No memory", __func__);
3940 skb_put_data(skb, cmd, sizeof(cmd));
3941 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3943 ret = btusb_send_frame(hdev, skb);
3945 bt_dev_err(hdev, "%s: configuration failed", __func__);
3954 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3955 const bdaddr_t *bdaddr)
3957 struct sk_buff *skb;
3962 buf[1] = sizeof(bdaddr_t);
3963 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3965 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3968 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3977 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3978 const bdaddr_t *bdaddr)
3980 struct sk_buff *skb;
3987 buf[3] = sizeof(bdaddr_t);
3988 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3990 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3993 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
4001 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
4002 const bdaddr_t *bdaddr)
4004 struct sk_buff *skb;
4008 memcpy(buf, bdaddr, sizeof(bdaddr_t));
4010 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
4011 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
4014 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
4022 #define QCA_DFU_PACKET_LEN 4096
4024 #define QCA_GET_TARGET_VERSION 0x09
4025 #define QCA_CHECK_STATUS 0x05
4026 #define QCA_DFU_DOWNLOAD 0x01
4028 #define QCA_SYSCFG_UPDATED 0x40
4029 #define QCA_PATCH_UPDATED 0x80
4030 #define QCA_DFU_TIMEOUT 3000
4031 #define QCA_FLAG_MULTI_NVM 0x80
4033 struct qca_version {
4035 __le32 patch_version;
4042 struct qca_rampatch_version {
4043 __le16 rom_version_high;
4044 __le16 rom_version_low;
4045 __le16 patch_version;
4048 struct qca_device_info {
4050 u8 rampatch_hdr; /* length of header in rampatch */
4051 u8 nvm_hdr; /* length of header in NVM */
4052 u8 ver_offset; /* offset of version structure in rampatch */
4055 static const struct qca_device_info qca_devices_table[] = {
4056 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
4057 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
4058 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
4059 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
4060 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
4061 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
4062 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
4063 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
4066 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
4067 void *data, u16 size)
4072 buf = kmalloc(size, GFP_KERNEL);
4076 /* Found some of USB hosts have IOT issues with ours so that we should
4077 * not wait until HCI layer is ready.
4079 pipe = usb_rcvctrlpipe(udev, 0);
4080 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
4081 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
4083 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
4087 memcpy(data, buf, size);
4095 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
4096 const struct firmware *firmware,
4099 struct btusb_data *btdata = hci_get_drvdata(hdev);
4100 struct usb_device *udev = btdata->udev;
4101 size_t count, size, sent = 0;
4105 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
4109 count = firmware->size;
4111 size = min_t(size_t, count, hdr_size);
4112 memcpy(buf, firmware->data, size);
4114 /* USB patches should go down to controller through USB path
4115 * because binary format fits to go down through USB channel.
4116 * USB control path is for patching headers and USB bulk is for
4119 pipe = usb_sndctrlpipe(udev, 0);
4120 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
4121 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
4123 bt_dev_err(hdev, "Failed to send headers (%d)", err);
4131 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
4133 memcpy(buf, firmware->data + sent, size);
4135 pipe = usb_sndbulkpipe(udev, 0x02);
4136 err = usb_bulk_msg(udev, pipe, buf, size, &len,
4139 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
4140 sent, firmware->size, err);
4145 bt_dev_err(hdev, "Failed to get bulk buffer");
4159 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
4160 struct qca_version *ver,
4161 const struct qca_device_info *info)
4163 struct qca_rampatch_version *rver;
4164 const struct firmware *fw;
4165 u32 ver_rom, ver_patch, rver_rom;
4166 u16 rver_rom_low, rver_rom_high, rver_patch;
4170 ver_rom = le32_to_cpu(ver->rom_version);
4171 ver_patch = le32_to_cpu(ver->patch_version);
4173 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
4175 err = request_firmware(&fw, fwname, &hdev->dev);
4177 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
4182 bt_dev_info(hdev, "using rampatch file: %s", fwname);
4184 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
4185 rver_rom_low = le16_to_cpu(rver->rom_version_low);
4186 rver_patch = le16_to_cpu(rver->patch_version);
4188 if (ver_rom & ~0xffffU) {
4189 rver_rom_high = le16_to_cpu(rver->rom_version_high);
4190 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
4192 rver_rom = rver_rom_low;
4195 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
4196 "firmware rome 0x%x build 0x%x",
4197 rver_rom, rver_patch, ver_rom, ver_patch);
4199 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
4200 bt_dev_err(hdev, "rampatch file version did not match with firmware");
4205 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
4208 release_firmware(fw);
4213 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
4214 struct qca_version *ver,
4215 const struct qca_device_info *info)
4217 const struct firmware *fw;
4221 if (((ver->flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
4222 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x_%04x.bin",
4223 le32_to_cpu(ver->rom_version),
4224 le16_to_cpu(ver->board_id));
4226 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
4227 le32_to_cpu(ver->rom_version));
4230 err = request_firmware(&fw, fwname, &hdev->dev);
4232 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
4237 bt_dev_info(hdev, "using NVM file: %s", fwname);
4239 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
4241 release_firmware(fw);
4246 /* identify the ROM version and check whether patches are needed */
4247 static bool btusb_qca_need_patch(struct usb_device *udev)
4249 struct qca_version ver;
4251 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
4254 /* only low ROM versions need patches */
4255 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
4258 static int btusb_setup_qca(struct hci_dev *hdev)
4260 struct btusb_data *btdata = hci_get_drvdata(hdev);
4261 struct usb_device *udev = btdata->udev;
4262 const struct qca_device_info *info = NULL;
4263 struct qca_version ver;
4268 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
4273 ver_rom = le32_to_cpu(ver.rom_version);
4275 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
4276 if (ver_rom == qca_devices_table[i].rom_version)
4277 info = &qca_devices_table[i];
4280 /* If the rom_version is not matched in the qca_devices_table
4281 * and the high ROM version is not zero, we assume this chip no
4282 * need to load the rampatch and nvm.
4284 if (ver_rom & ~0xffffU)
4287 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
4291 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
4296 if (!(status & QCA_PATCH_UPDATED)) {
4297 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
4302 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
4307 if (!(status & QCA_SYSCFG_UPDATED)) {
4308 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
4316 static inline int __set_diag_interface(struct hci_dev *hdev)
4318 struct btusb_data *data = hci_get_drvdata(hdev);
4319 struct usb_interface *intf = data->diag;
4325 data->diag_tx_ep = NULL;
4326 data->diag_rx_ep = NULL;
4328 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4329 struct usb_endpoint_descriptor *ep_desc;
4331 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4333 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4334 data->diag_tx_ep = ep_desc;
4338 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4339 data->diag_rx_ep = ep_desc;
4344 if (!data->diag_tx_ep || !data->diag_rx_ep) {
4345 bt_dev_err(hdev, "invalid diagnostic descriptors");
4352 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
4354 struct btusb_data *data = hci_get_drvdata(hdev);
4355 struct sk_buff *skb;
4359 if (!data->diag_tx_ep)
4360 return ERR_PTR(-ENODEV);
4362 urb = usb_alloc_urb(0, GFP_KERNEL);
4364 return ERR_PTR(-ENOMEM);
4366 skb = bt_skb_alloc(2, GFP_KERNEL);
4369 return ERR_PTR(-ENOMEM);
4372 skb_put_u8(skb, 0xf0);
4373 skb_put_u8(skb, enable);
4375 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
4377 usb_fill_bulk_urb(urb, data->udev, pipe,
4378 skb->data, skb->len, btusb_tx_complete, skb);
4380 skb->dev = (void *)hdev;
4385 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
4387 struct btusb_data *data = hci_get_drvdata(hdev);
4393 if (!test_bit(HCI_RUNNING, &hdev->flags))
4396 urb = alloc_diag_urb(hdev, enable);
4398 return PTR_ERR(urb);
4400 return submit_or_queue_tx_urb(hdev, urb);
4404 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
4406 struct btusb_data *data = priv;
4408 pm_wakeup_event(&data->udev->dev, 0);
4411 /* Disable only if not already disabled (keep it balanced) */
4412 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4413 disable_irq_nosync(irq);
4414 disable_irq_wake(irq);
4419 static const struct of_device_id btusb_match_table[] = {
4420 { .compatible = "usb1286,204e" },
4421 { .compatible = "usbcf3,e300" }, /* QCA6174A */
4422 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
4425 MODULE_DEVICE_TABLE(of, btusb_match_table);
4427 /* Use an oob wakeup pin? */
4428 static int btusb_config_oob_wake(struct hci_dev *hdev)
4430 struct btusb_data *data = hci_get_drvdata(hdev);
4431 struct device *dev = &data->udev->dev;
4434 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4436 if (!of_match_device(btusb_match_table, dev))
4439 /* Move on if no IRQ specified */
4440 irq = of_irq_get_byname(dev->of_node, "wakeup");
4442 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
4446 irq_set_status_flags(irq, IRQ_NOAUTOEN);
4447 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
4448 0, "OOB Wake-on-BT", data);
4450 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
4454 ret = device_init_wakeup(dev, true);
4456 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
4460 data->oob_wake_irq = irq;
4461 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
4466 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
4468 if (dmi_check_system(btusb_needs_reset_resume_table))
4469 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
4472 static bool btusb_prevent_wake(struct hci_dev *hdev)
4474 struct btusb_data *data = hci_get_drvdata(hdev);
4476 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
4479 return !device_may_wakeup(&data->udev->dev);
4482 static int btusb_shutdown_qca(struct hci_dev *hdev)
4484 struct sk_buff *skb;
4486 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
4488 bt_dev_err(hdev, "HCI reset during shutdown failed");
4489 return PTR_ERR(skb);
4496 static int btusb_probe(struct usb_interface *intf,
4497 const struct usb_device_id *id)
4499 struct usb_endpoint_descriptor *ep_desc;
4500 struct gpio_desc *reset_gpio;
4501 struct btusb_data *data;
4502 struct hci_dev *hdev;
4503 unsigned ifnum_base;
4506 BT_DBG("intf %p id %p", intf, id);
4508 /* interface numbers are hardcoded in the spec */
4509 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
4510 if (!(id->driver_info & BTUSB_IFNUM_2))
4512 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
4516 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
4518 if (!id->driver_info) {
4519 const struct usb_device_id *match;
4521 match = usb_match_id(intf, blacklist_table);
4526 if (id->driver_info == BTUSB_IGNORE)
4529 if (id->driver_info & BTUSB_ATH3012) {
4530 struct usb_device *udev = interface_to_usbdev(intf);
4532 /* Old firmware would otherwise let ath3k driver load
4533 * patch and sysconfig files
4535 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
4536 !btusb_qca_need_patch(udev))
4540 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
4544 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4545 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4547 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
4548 data->intr_ep = ep_desc;
4552 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4553 data->bulk_tx_ep = ep_desc;
4557 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4558 data->bulk_rx_ep = ep_desc;
4563 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
4566 if (id->driver_info & BTUSB_AMP) {
4567 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
4568 data->cmdreq = 0x2b;
4570 data->cmdreq_type = USB_TYPE_CLASS;
4571 data->cmdreq = 0x00;
4574 data->udev = interface_to_usbdev(intf);
4577 INIT_WORK(&data->work, btusb_work);
4578 INIT_WORK(&data->waker, btusb_waker);
4579 init_usb_anchor(&data->deferred);
4580 init_usb_anchor(&data->tx_anchor);
4581 spin_lock_init(&data->txlock);
4583 init_usb_anchor(&data->intr_anchor);
4584 init_usb_anchor(&data->bulk_anchor);
4585 init_usb_anchor(&data->isoc_anchor);
4586 init_usb_anchor(&data->diag_anchor);
4587 init_usb_anchor(&data->ctrl_anchor);
4588 spin_lock_init(&data->rxlock);
4590 if (id->driver_info & BTUSB_INTEL_NEW) {
4591 data->recv_event = btusb_recv_event_intel;
4592 data->recv_bulk = btusb_recv_bulk_intel;
4593 set_bit(BTUSB_BOOTLOADER, &data->flags);
4595 data->recv_event = hci_recv_frame;
4596 data->recv_bulk = btusb_recv_bulk;
4599 hdev = hci_alloc_dev();
4603 hdev->bus = HCI_USB;
4604 hci_set_drvdata(hdev, data);
4606 if (id->driver_info & BTUSB_AMP)
4607 hdev->dev_type = HCI_AMP;
4609 hdev->dev_type = HCI_PRIMARY;
4613 SET_HCIDEV_DEV(hdev, &intf->dev);
4615 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
4617 if (IS_ERR(reset_gpio)) {
4618 err = PTR_ERR(reset_gpio);
4620 } else if (reset_gpio) {
4621 data->reset_gpio = reset_gpio;
4624 hdev->open = btusb_open;
4625 hdev->close = btusb_close;
4626 hdev->flush = btusb_flush;
4627 hdev->send = btusb_send_frame;
4628 hdev->notify = btusb_notify;
4629 hdev->prevent_wake = btusb_prevent_wake;
4632 err = btusb_config_oob_wake(hdev);
4636 /* Marvell devices may need a specific chip configuration */
4637 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
4638 err = marvell_config_oob_wake(hdev);
4643 if (id->driver_info & BTUSB_CW6622)
4644 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4646 if (id->driver_info & BTUSB_BCM2045)
4647 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4649 if (id->driver_info & BTUSB_BCM92035)
4650 hdev->setup = btusb_setup_bcm92035;
4652 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4653 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
4654 hdev->manufacturer = 15;
4655 hdev->setup = btbcm_setup_patchram;
4656 hdev->set_diag = btusb_bcm_set_diag;
4657 hdev->set_bdaddr = btbcm_set_bdaddr;
4659 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4660 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4663 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4664 (id->driver_info & BTUSB_BCM_APPLE)) {
4665 hdev->manufacturer = 15;
4666 hdev->setup = btbcm_setup_apple;
4667 hdev->set_diag = btusb_bcm_set_diag;
4669 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4670 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4673 if (id->driver_info & BTUSB_INTEL) {
4674 hdev->manufacturer = 2;
4675 hdev->setup = btusb_setup_intel;
4676 hdev->shutdown = btusb_shutdown_intel;
4677 hdev->set_diag = btintel_set_diag_mfg;
4678 hdev->set_bdaddr = btintel_set_bdaddr;
4679 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4680 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4681 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4682 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4685 if (id->driver_info & BTUSB_INTEL_NEW) {
4686 hdev->manufacturer = 2;
4687 hdev->send = btusb_send_frame_intel;
4688 hdev->setup = btusb_setup_intel_new;
4689 hdev->shutdown = btusb_shutdown_intel_new;
4690 hdev->hw_error = btintel_hw_error;
4691 hdev->set_diag = btintel_set_diag;
4692 hdev->set_bdaddr = btintel_set_bdaddr;
4693 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4694 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4695 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4696 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4699 if (id->driver_info & BTUSB_INTEL_NEWGEN) {
4700 hdev->manufacturer = 2;
4701 hdev->send = btusb_send_frame_intel;
4702 hdev->setup = btusb_setup_intel_newgen;
4703 hdev->shutdown = btusb_shutdown_intel_new;
4704 hdev->hw_error = btintel_hw_error;
4705 hdev->set_diag = btintel_set_diag;
4706 hdev->set_bdaddr = btintel_set_bdaddr;
4707 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4708 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4709 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4710 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4712 data->recv_event = btusb_recv_event_intel;
4713 data->recv_bulk = btusb_recv_bulk_intel;
4714 set_bit(BTUSB_BOOTLOADER, &data->flags);
4717 if (id->driver_info & BTUSB_MARVELL)
4718 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4720 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4721 (id->driver_info & BTUSB_MEDIATEK)) {
4722 hdev->setup = btusb_mtk_setup;
4723 hdev->shutdown = btusb_mtk_shutdown;
4724 hdev->manufacturer = 70;
4725 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
4728 if (id->driver_info & BTUSB_SWAVE) {
4729 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
4730 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
4733 if (id->driver_info & BTUSB_INTEL_BOOT) {
4734 hdev->manufacturer = 2;
4735 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4738 if (id->driver_info & BTUSB_ATH3012) {
4739 data->setup_on_usb = btusb_setup_qca;
4740 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4741 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4742 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4745 if (id->driver_info & BTUSB_QCA_ROME) {
4746 data->setup_on_usb = btusb_setup_qca;
4747 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4748 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4749 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4750 btusb_check_needs_reset_resume(intf);
4753 if (id->driver_info & BTUSB_QCA_WCN6855) {
4754 data->setup_on_usb = btusb_setup_qca;
4755 hdev->shutdown = btusb_shutdown_qca;
4756 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
4757 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4758 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4761 if (id->driver_info & BTUSB_AMP) {
4762 /* AMP controllers do not support SCO packets */
4765 /* Interface orders are hardcoded in the specification */
4766 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
4767 data->isoc_ifnum = ifnum_base + 1;
4770 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4771 (id->driver_info & BTUSB_REALTEK)) {
4772 hdev->setup = btrtl_setup_realtek;
4773 hdev->shutdown = btrtl_shutdown_realtek;
4774 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
4776 /* Realtek devices lose their updated firmware over global
4777 * suspend that means host doesn't send SET_FEATURE
4778 * (DEVICE_REMOTE_WAKEUP)
4780 set_bit(BTUSB_WAKEUP_DISABLE, &data->flags);
4784 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4786 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
4787 if (!disable_scofix)
4788 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
4791 if (id->driver_info & BTUSB_BROKEN_ISOC)
4794 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
4795 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
4797 if (id->driver_info & BTUSB_VALID_LE_STATES)
4798 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
4800 if (id->driver_info & BTUSB_DIGIANSWER) {
4801 data->cmdreq_type = USB_TYPE_VENDOR;
4802 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4805 if (id->driver_info & BTUSB_CSR) {
4806 struct usb_device *udev = data->udev;
4807 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
4809 /* Old firmware would otherwise execute USB reset */
4810 if (bcdDevice < 0x117)
4811 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4813 /* This must be set first in case we disable it for fakes */
4814 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4816 /* Fake CSR devices with broken commands */
4817 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
4818 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4819 hdev->setup = btusb_setup_csr;
4822 if (id->driver_info & BTUSB_SNIFFER) {
4823 struct usb_device *udev = data->udev;
4825 /* New sniffer firmware has crippled HCI interface */
4826 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4827 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4830 if (id->driver_info & BTUSB_INTEL_BOOT) {
4831 /* A bug in the bootloader causes that interrupt interface is
4832 * only enabled after receiving SetInterface(0, AltSetting=0).
4834 err = usb_set_interface(data->udev, 0, 0);
4836 BT_ERR("failed to set interface 0, alt 0 %d", err);
4842 err = usb_driver_claim_interface(&btusb_driver,
4848 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
4849 if (!usb_driver_claim_interface(&btusb_driver,
4851 __set_diag_interface(hdev);
4856 if (!enable_autosuspend)
4857 usb_disable_autosuspend(data->udev);
4859 err = hci_register_dev(hdev);
4863 usb_set_intfdata(intf, data);
4868 if (data->reset_gpio)
4869 gpiod_put(data->reset_gpio);
4874 static void btusb_disconnect(struct usb_interface *intf)
4876 struct btusb_data *data = usb_get_intfdata(intf);
4877 struct hci_dev *hdev;
4879 BT_DBG("intf %p", intf);
4885 usb_set_intfdata(data->intf, NULL);
4888 usb_set_intfdata(data->isoc, NULL);
4891 usb_set_intfdata(data->diag, NULL);
4893 hci_unregister_dev(hdev);
4895 if (intf == data->intf) {
4897 usb_driver_release_interface(&btusb_driver, data->isoc);
4899 usb_driver_release_interface(&btusb_driver, data->diag);
4900 } else if (intf == data->isoc) {
4902 usb_driver_release_interface(&btusb_driver, data->diag);
4903 usb_driver_release_interface(&btusb_driver, data->intf);
4904 } else if (intf == data->diag) {
4905 usb_driver_release_interface(&btusb_driver, data->intf);
4907 usb_driver_release_interface(&btusb_driver, data->isoc);
4910 if (data->oob_wake_irq)
4911 device_init_wakeup(&data->udev->dev, false);
4913 if (data->reset_gpio)
4914 gpiod_put(data->reset_gpio);
4918 if (!enable_autosuspend)
4919 usb_enable_autosuspend(data->udev);
4923 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4925 struct btusb_data *data = usb_get_intfdata(intf);
4927 BT_DBG("intf %p", intf);
4929 if (data->suspend_count++)
4932 spin_lock_irq(&data->txlock);
4933 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4934 set_bit(BTUSB_SUSPENDING, &data->flags);
4935 spin_unlock_irq(&data->txlock);
4937 spin_unlock_irq(&data->txlock);
4938 data->suspend_count--;
4942 cancel_work_sync(&data->work);
4944 btusb_stop_traffic(data);
4945 usb_kill_anchored_urbs(&data->tx_anchor);
4947 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4948 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4949 enable_irq_wake(data->oob_wake_irq);
4950 enable_irq(data->oob_wake_irq);
4953 /* For global suspend, Realtek devices lose the loaded fw
4954 * in them. But for autosuspend, firmware should remain.
4955 * Actually, it depends on whether the usb host sends
4956 * set feature (enable wakeup) or not.
4958 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags)) {
4959 if (PMSG_IS_AUTO(message) &&
4960 device_can_wakeup(&data->udev->dev))
4961 data->udev->do_remote_wakeup = 1;
4962 else if (!PMSG_IS_AUTO(message))
4963 data->udev->reset_resume = 1;
4969 static void play_deferred(struct btusb_data *data)
4974 while ((urb = usb_get_from_anchor(&data->deferred))) {
4975 usb_anchor_urb(urb, &data->tx_anchor);
4977 err = usb_submit_urb(urb, GFP_ATOMIC);
4979 if (err != -EPERM && err != -ENODEV)
4980 BT_ERR("%s urb %p submission failed (%d)",
4981 data->hdev->name, urb, -err);
4982 kfree(urb->setup_packet);
4983 usb_unanchor_urb(urb);
4988 data->tx_in_flight++;
4992 /* Cleanup the rest deferred urbs. */
4993 while ((urb = usb_get_from_anchor(&data->deferred))) {
4994 kfree(urb->setup_packet);
4999 static int btusb_resume(struct usb_interface *intf)
5001 struct btusb_data *data = usb_get_intfdata(intf);
5002 struct hci_dev *hdev = data->hdev;
5005 BT_DBG("intf %p", intf);
5007 if (--data->suspend_count)
5010 /* Disable only if not already disabled (keep it balanced) */
5011 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
5012 disable_irq(data->oob_wake_irq);
5013 disable_irq_wake(data->oob_wake_irq);
5016 if (!test_bit(HCI_RUNNING, &hdev->flags))
5019 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
5020 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
5022 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
5027 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
5028 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
5030 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
5034 btusb_submit_bulk_urb(hdev, GFP_NOIO);
5037 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
5038 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
5039 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
5041 btusb_submit_isoc_urb(hdev, GFP_NOIO);
5044 spin_lock_irq(&data->txlock);
5045 play_deferred(data);
5046 clear_bit(BTUSB_SUSPENDING, &data->flags);
5047 spin_unlock_irq(&data->txlock);
5048 schedule_work(&data->work);
5053 usb_scuttle_anchored_urbs(&data->deferred);
5055 spin_lock_irq(&data->txlock);
5056 clear_bit(BTUSB_SUSPENDING, &data->flags);
5057 spin_unlock_irq(&data->txlock);
5063 static struct usb_driver btusb_driver = {
5065 .probe = btusb_probe,
5066 .disconnect = btusb_disconnect,
5068 .suspend = btusb_suspend,
5069 .resume = btusb_resume,
5071 .id_table = btusb_table,
5072 .supports_autosuspend = 1,
5073 .disable_hub_initiated_lpm = 1,
5076 module_usb_driver(btusb_driver);
5078 module_param(disable_scofix, bool, 0644);
5079 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
5081 module_param(force_scofix, bool, 0644);
5082 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
5084 module_param(enable_autosuspend, bool, 0644);
5085 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
5087 module_param(reset, bool, 0644);
5088 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
5090 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
5091 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
5092 MODULE_VERSION(VERSION);
5093 MODULE_LICENSE("GPL");