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 },
391 { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
392 BTUSB_WIDEBAND_SPEECH },
394 /* Realtek 8852AE Bluetooth devices */
395 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
396 BTUSB_WIDEBAND_SPEECH },
398 /* Realtek Bluetooth devices */
399 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
400 .driver_info = BTUSB_REALTEK },
402 /* MediaTek Bluetooth devices */
403 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
404 .driver_info = BTUSB_MEDIATEK |
405 BTUSB_WIDEBAND_SPEECH |
406 BTUSB_VALID_LE_STATES },
408 /* Additional MediaTek MT7615E Bluetooth devices */
409 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
411 /* Additional Realtek 8723AE Bluetooth devices */
412 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
413 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
415 /* Additional Realtek 8723BE Bluetooth devices */
416 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
417 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
418 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
419 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
420 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
421 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
423 /* Additional Realtek 8723BU Bluetooth devices */
424 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
426 /* Additional Realtek 8723DE Bluetooth devices */
427 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
428 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
430 /* Additional Realtek 8821AE Bluetooth devices */
431 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
432 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
433 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
434 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
435 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
437 /* Additional Realtek 8822BE Bluetooth devices */
438 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
439 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
441 /* Additional Realtek 8822CE Bluetooth devices */
442 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
443 BTUSB_WIDEBAND_SPEECH },
444 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
445 BTUSB_WIDEBAND_SPEECH },
446 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
447 BTUSB_WIDEBAND_SPEECH },
448 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
449 BTUSB_WIDEBAND_SPEECH },
450 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
451 BTUSB_WIDEBAND_SPEECH },
452 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
453 BTUSB_WIDEBAND_SPEECH },
454 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
455 BTUSB_WIDEBAND_SPEECH },
456 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
457 BTUSB_WIDEBAND_SPEECH },
458 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
459 BTUSB_WIDEBAND_SPEECH },
460 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
461 BTUSB_WIDEBAND_SPEECH },
462 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
463 BTUSB_WIDEBAND_SPEECH },
465 /* Silicon Wave based devices */
466 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
468 { } /* Terminating entry */
471 /* The Bluetooth USB module build into some devices needs to be reset on resume,
472 * this is a problem with the platform (likely shutting off all power) not with
473 * the module itself. So we use a DMI list to match known broken platforms.
475 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
477 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
479 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
480 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
484 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
486 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
487 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
491 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
493 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
494 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
500 #define BTUSB_MAX_ISOC_FRAMES 10
502 #define BTUSB_INTR_RUNNING 0
503 #define BTUSB_BULK_RUNNING 1
504 #define BTUSB_ISOC_RUNNING 2
505 #define BTUSB_SUSPENDING 3
506 #define BTUSB_DID_ISO_RESUME 4
507 #define BTUSB_BOOTLOADER 5
508 #define BTUSB_DOWNLOADING 6
509 #define BTUSB_FIRMWARE_LOADED 7
510 #define BTUSB_FIRMWARE_FAILED 8
511 #define BTUSB_BOOTING 9
512 #define BTUSB_DIAG_RUNNING 10
513 #define BTUSB_OOB_WAKE_ENABLED 11
514 #define BTUSB_HW_RESET_ACTIVE 12
515 #define BTUSB_TX_WAIT_VND_EVT 13
516 #define BTUSB_WAKEUP_DISABLE 14
519 struct hci_dev *hdev;
520 struct usb_device *udev;
521 struct usb_interface *intf;
522 struct usb_interface *isoc;
523 struct usb_interface *diag;
528 struct work_struct work;
529 struct work_struct waker;
531 struct usb_anchor deferred;
532 struct usb_anchor tx_anchor;
536 struct usb_anchor intr_anchor;
537 struct usb_anchor bulk_anchor;
538 struct usb_anchor isoc_anchor;
539 struct usb_anchor diag_anchor;
540 struct usb_anchor ctrl_anchor;
543 struct sk_buff *evt_skb;
544 struct sk_buff *acl_skb;
545 struct sk_buff *sco_skb;
547 struct usb_endpoint_descriptor *intr_ep;
548 struct usb_endpoint_descriptor *bulk_tx_ep;
549 struct usb_endpoint_descriptor *bulk_rx_ep;
550 struct usb_endpoint_descriptor *isoc_tx_ep;
551 struct usb_endpoint_descriptor *isoc_rx_ep;
552 struct usb_endpoint_descriptor *diag_tx_ep;
553 struct usb_endpoint_descriptor *diag_rx_ep;
555 struct gpio_desc *reset_gpio;
560 unsigned int sco_num;
561 unsigned int air_mode;
562 bool usb_alt6_packet_flow;
566 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
567 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
569 int (*setup_on_usb)(struct hci_dev *hdev);
571 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
572 unsigned cmd_timeout_cnt;
575 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
577 struct btusb_data *data = hci_get_drvdata(hdev);
578 struct gpio_desc *reset_gpio = data->reset_gpio;
580 if (++data->cmd_timeout_cnt < 5)
584 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
589 * Toggle the hard reset line if the platform provides one. The reset
590 * is going to yank the device off the USB and then replug. So doing
591 * once is enough. The cleanup is handled correctly on the way out
592 * (standard USB disconnect), and the new device is detected cleanly
593 * and bound to the driver again like it should be.
595 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
596 bt_dev_err(hdev, "last reset failed? Not resetting again");
600 bt_dev_err(hdev, "Initiating HW reset via gpio");
601 gpiod_set_value_cansleep(reset_gpio, 1);
603 gpiod_set_value_cansleep(reset_gpio, 0);
606 static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
608 struct btusb_data *data = hci_get_drvdata(hdev);
609 struct gpio_desc *reset_gpio = data->reset_gpio;
611 if (++data->cmd_timeout_cnt < 5)
615 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
619 /* Toggle the hard reset line. The Realtek device is going to
620 * yank itself off the USB and then replug. The cleanup is handled
621 * correctly on the way out (standard USB disconnect), and the new
622 * device is detected cleanly and bound to the driver again like
625 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
626 bt_dev_err(hdev, "last reset failed? Not resetting again");
630 bt_dev_err(hdev, "Reset Realtek device via gpio");
631 gpiod_set_value_cansleep(reset_gpio, 1);
633 gpiod_set_value_cansleep(reset_gpio, 0);
636 static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
638 struct btusb_data *data = hci_get_drvdata(hdev);
641 if (++data->cmd_timeout_cnt < 5)
644 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
645 /* This is not an unbalanced PM reference since the device will reset */
646 err = usb_autopm_get_interface(data->intf);
648 usb_queue_reset_device(data->intf);
650 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
653 static inline void btusb_free_frags(struct btusb_data *data)
657 spin_lock_irqsave(&data->rxlock, flags);
659 kfree_skb(data->evt_skb);
660 data->evt_skb = NULL;
662 kfree_skb(data->acl_skb);
663 data->acl_skb = NULL;
665 kfree_skb(data->sco_skb);
666 data->sco_skb = NULL;
668 spin_unlock_irqrestore(&data->rxlock, flags);
671 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
677 spin_lock_irqsave(&data->rxlock, flags);
684 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
690 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
691 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
694 len = min_t(uint, hci_skb_expect(skb), count);
695 skb_put_data(skb, buffer, len);
699 hci_skb_expect(skb) -= len;
701 if (skb->len == HCI_EVENT_HDR_SIZE) {
702 /* Complete event header */
703 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
705 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
714 if (!hci_skb_expect(skb)) {
716 data->recv_event(data->hdev, skb);
722 spin_unlock_irqrestore(&data->rxlock, flags);
727 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
733 spin_lock_irqsave(&data->rxlock, flags);
740 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
746 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
747 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
750 len = min_t(uint, hci_skb_expect(skb), count);
751 skb_put_data(skb, buffer, len);
755 hci_skb_expect(skb) -= len;
757 if (skb->len == HCI_ACL_HDR_SIZE) {
758 __le16 dlen = hci_acl_hdr(skb)->dlen;
760 /* Complete ACL header */
761 hci_skb_expect(skb) = __le16_to_cpu(dlen);
763 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
772 if (!hci_skb_expect(skb)) {
774 hci_recv_frame(data->hdev, skb);
780 spin_unlock_irqrestore(&data->rxlock, flags);
785 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
791 spin_lock_irqsave(&data->rxlock, flags);
798 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
804 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
805 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
808 len = min_t(uint, hci_skb_expect(skb), count);
809 skb_put_data(skb, buffer, len);
813 hci_skb_expect(skb) -= len;
815 if (skb->len == HCI_SCO_HDR_SIZE) {
816 /* Complete SCO header */
817 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
819 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
828 if (!hci_skb_expect(skb)) {
830 hci_recv_frame(data->hdev, skb);
836 spin_unlock_irqrestore(&data->rxlock, flags);
841 static void btusb_intr_complete(struct urb *urb)
843 struct hci_dev *hdev = urb->context;
844 struct btusb_data *data = hci_get_drvdata(hdev);
847 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
850 if (!test_bit(HCI_RUNNING, &hdev->flags))
853 if (urb->status == 0) {
854 hdev->stat.byte_rx += urb->actual_length;
856 if (btusb_recv_intr(data, urb->transfer_buffer,
857 urb->actual_length) < 0) {
858 bt_dev_err(hdev, "corrupted event packet");
861 } else if (urb->status == -ENOENT) {
862 /* Avoid suspend failed when usb_kill_urb */
866 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
869 usb_mark_last_busy(data->udev);
870 usb_anchor_urb(urb, &data->intr_anchor);
872 err = usb_submit_urb(urb, GFP_ATOMIC);
874 /* -EPERM: urb is being killed;
875 * -ENODEV: device got disconnected
877 if (err != -EPERM && err != -ENODEV)
878 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
880 usb_unanchor_urb(urb);
884 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
886 struct btusb_data *data = hci_get_drvdata(hdev);
892 BT_DBG("%s", hdev->name);
897 urb = usb_alloc_urb(0, mem_flags);
901 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
903 buf = kmalloc(size, mem_flags);
909 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
911 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
912 btusb_intr_complete, hdev, data->intr_ep->bInterval);
914 urb->transfer_flags |= URB_FREE_BUFFER;
916 usb_anchor_urb(urb, &data->intr_anchor);
918 err = usb_submit_urb(urb, mem_flags);
920 if (err != -EPERM && err != -ENODEV)
921 bt_dev_err(hdev, "urb %p submission failed (%d)",
923 usb_unanchor_urb(urb);
931 static void btusb_bulk_complete(struct urb *urb)
933 struct hci_dev *hdev = urb->context;
934 struct btusb_data *data = hci_get_drvdata(hdev);
937 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
940 if (!test_bit(HCI_RUNNING, &hdev->flags))
943 if (urb->status == 0) {
944 hdev->stat.byte_rx += urb->actual_length;
946 if (data->recv_bulk(data, urb->transfer_buffer,
947 urb->actual_length) < 0) {
948 bt_dev_err(hdev, "corrupted ACL packet");
951 } else if (urb->status == -ENOENT) {
952 /* Avoid suspend failed when usb_kill_urb */
956 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
959 usb_anchor_urb(urb, &data->bulk_anchor);
960 usb_mark_last_busy(data->udev);
962 err = usb_submit_urb(urb, GFP_ATOMIC);
964 /* -EPERM: urb is being killed;
965 * -ENODEV: device got disconnected
967 if (err != -EPERM && err != -ENODEV)
968 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
970 usb_unanchor_urb(urb);
974 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
976 struct btusb_data *data = hci_get_drvdata(hdev);
980 int err, size = HCI_MAX_FRAME_SIZE;
982 BT_DBG("%s", hdev->name);
984 if (!data->bulk_rx_ep)
987 urb = usb_alloc_urb(0, mem_flags);
991 buf = kmalloc(size, mem_flags);
997 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
999 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1000 btusb_bulk_complete, hdev);
1002 urb->transfer_flags |= URB_FREE_BUFFER;
1004 usb_mark_last_busy(data->udev);
1005 usb_anchor_urb(urb, &data->bulk_anchor);
1007 err = usb_submit_urb(urb, mem_flags);
1009 if (err != -EPERM && err != -ENODEV)
1010 bt_dev_err(hdev, "urb %p submission failed (%d)",
1012 usb_unanchor_urb(urb);
1020 static void btusb_isoc_complete(struct urb *urb)
1022 struct hci_dev *hdev = urb->context;
1023 struct btusb_data *data = hci_get_drvdata(hdev);
1026 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1027 urb->actual_length);
1029 if (!test_bit(HCI_RUNNING, &hdev->flags))
1032 if (urb->status == 0) {
1033 for (i = 0; i < urb->number_of_packets; i++) {
1034 unsigned int offset = urb->iso_frame_desc[i].offset;
1035 unsigned int length = urb->iso_frame_desc[i].actual_length;
1037 if (urb->iso_frame_desc[i].status)
1040 hdev->stat.byte_rx += length;
1042 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1044 bt_dev_err(hdev, "corrupted SCO packet");
1045 hdev->stat.err_rx++;
1048 } else if (urb->status == -ENOENT) {
1049 /* Avoid suspend failed when usb_kill_urb */
1053 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1056 usb_anchor_urb(urb, &data->isoc_anchor);
1058 err = usb_submit_urb(urb, GFP_ATOMIC);
1060 /* -EPERM: urb is being killed;
1061 * -ENODEV: device got disconnected
1063 if (err != -EPERM && err != -ENODEV)
1064 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1066 usb_unanchor_urb(urb);
1070 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1071 int mtu, struct btusb_data *data)
1074 unsigned int interval;
1076 BT_DBG("len %d mtu %d", len, mtu);
1078 /* For mSBC ALT 6 setting the host will send the packet at continuous
1079 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1080 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1081 * To maintain the rate we send 63bytes of usb packets alternatively for
1082 * 7ms and 8ms to maintain the rate as 7.5ms.
1084 if (data->usb_alt6_packet_flow) {
1086 data->usb_alt6_packet_flow = false;
1089 data->usb_alt6_packet_flow = true;
1092 for (i = 0; i < interval; i++) {
1093 urb->iso_frame_desc[i].offset = offset;
1094 urb->iso_frame_desc[i].length = offset;
1097 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1098 urb->iso_frame_desc[i].offset = offset;
1099 urb->iso_frame_desc[i].length = len;
1103 urb->number_of_packets = i;
1106 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1110 BT_DBG("len %d mtu %d", len, mtu);
1112 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1113 i++, offset += mtu, len -= mtu) {
1114 urb->iso_frame_desc[i].offset = offset;
1115 urb->iso_frame_desc[i].length = mtu;
1118 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1119 urb->iso_frame_desc[i].offset = offset;
1120 urb->iso_frame_desc[i].length = len;
1124 urb->number_of_packets = i;
1127 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1129 struct btusb_data *data = hci_get_drvdata(hdev);
1135 BT_DBG("%s", hdev->name);
1137 if (!data->isoc_rx_ep)
1140 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1144 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1145 BTUSB_MAX_ISOC_FRAMES;
1147 buf = kmalloc(size, mem_flags);
1153 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1155 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1156 hdev, data->isoc_rx_ep->bInterval);
1158 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1160 __fill_isoc_descriptor(urb, size,
1161 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1163 usb_anchor_urb(urb, &data->isoc_anchor);
1165 err = usb_submit_urb(urb, mem_flags);
1167 if (err != -EPERM && err != -ENODEV)
1168 bt_dev_err(hdev, "urb %p submission failed (%d)",
1170 usb_unanchor_urb(urb);
1178 static void btusb_diag_complete(struct urb *urb)
1180 struct hci_dev *hdev = urb->context;
1181 struct btusb_data *data = hci_get_drvdata(hdev);
1184 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1185 urb->actual_length);
1187 if (urb->status == 0) {
1188 struct sk_buff *skb;
1190 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1192 skb_put_data(skb, urb->transfer_buffer,
1193 urb->actual_length);
1194 hci_recv_diag(hdev, skb);
1196 } else if (urb->status == -ENOENT) {
1197 /* Avoid suspend failed when usb_kill_urb */
1201 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1204 usb_anchor_urb(urb, &data->diag_anchor);
1205 usb_mark_last_busy(data->udev);
1207 err = usb_submit_urb(urb, GFP_ATOMIC);
1209 /* -EPERM: urb is being killed;
1210 * -ENODEV: device got disconnected
1212 if (err != -EPERM && err != -ENODEV)
1213 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1215 usb_unanchor_urb(urb);
1219 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1221 struct btusb_data *data = hci_get_drvdata(hdev);
1225 int err, size = HCI_MAX_FRAME_SIZE;
1227 BT_DBG("%s", hdev->name);
1229 if (!data->diag_rx_ep)
1232 urb = usb_alloc_urb(0, mem_flags);
1236 buf = kmalloc(size, mem_flags);
1242 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1244 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1245 btusb_diag_complete, hdev);
1247 urb->transfer_flags |= URB_FREE_BUFFER;
1249 usb_mark_last_busy(data->udev);
1250 usb_anchor_urb(urb, &data->diag_anchor);
1252 err = usb_submit_urb(urb, mem_flags);
1254 if (err != -EPERM && err != -ENODEV)
1255 bt_dev_err(hdev, "urb %p submission failed (%d)",
1257 usb_unanchor_urb(urb);
1265 static void btusb_tx_complete(struct urb *urb)
1267 struct sk_buff *skb = urb->context;
1268 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1269 struct btusb_data *data = hci_get_drvdata(hdev);
1270 unsigned long flags;
1272 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1273 urb->actual_length);
1275 if (!test_bit(HCI_RUNNING, &hdev->flags))
1279 hdev->stat.byte_tx += urb->transfer_buffer_length;
1281 hdev->stat.err_tx++;
1284 spin_lock_irqsave(&data->txlock, flags);
1285 data->tx_in_flight--;
1286 spin_unlock_irqrestore(&data->txlock, flags);
1288 kfree(urb->setup_packet);
1293 static void btusb_isoc_tx_complete(struct urb *urb)
1295 struct sk_buff *skb = urb->context;
1296 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1298 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1299 urb->actual_length);
1301 if (!test_bit(HCI_RUNNING, &hdev->flags))
1305 hdev->stat.byte_tx += urb->transfer_buffer_length;
1307 hdev->stat.err_tx++;
1310 kfree(urb->setup_packet);
1315 static int btusb_open(struct hci_dev *hdev)
1317 struct btusb_data *data = hci_get_drvdata(hdev);
1320 BT_DBG("%s", hdev->name);
1322 err = usb_autopm_get_interface(data->intf);
1326 /* Patching USB firmware files prior to starting any URBs of HCI path
1327 * It is more safe to use USB bulk channel for downloading USB patch
1329 if (data->setup_on_usb) {
1330 err = data->setup_on_usb(hdev);
1335 data->intf->needs_remote_wakeup = 1;
1337 /* Disable device remote wakeup when host is suspended
1338 * For Realtek chips, global suspend without
1339 * SET_FEATURE (DEVICE_REMOTE_WAKEUP) can save more power in device.
1341 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1342 device_wakeup_disable(&data->udev->dev);
1344 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1347 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1351 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1353 usb_kill_anchored_urbs(&data->intr_anchor);
1357 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1358 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1361 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1362 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1366 usb_autopm_put_interface(data->intf);
1370 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1372 usb_autopm_put_interface(data->intf);
1376 static void btusb_stop_traffic(struct btusb_data *data)
1378 usb_kill_anchored_urbs(&data->intr_anchor);
1379 usb_kill_anchored_urbs(&data->bulk_anchor);
1380 usb_kill_anchored_urbs(&data->isoc_anchor);
1381 usb_kill_anchored_urbs(&data->diag_anchor);
1382 usb_kill_anchored_urbs(&data->ctrl_anchor);
1385 static int btusb_close(struct hci_dev *hdev)
1387 struct btusb_data *data = hci_get_drvdata(hdev);
1390 BT_DBG("%s", hdev->name);
1392 cancel_work_sync(&data->work);
1393 cancel_work_sync(&data->waker);
1395 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1396 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1397 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1398 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1400 btusb_stop_traffic(data);
1401 btusb_free_frags(data);
1403 err = usb_autopm_get_interface(data->intf);
1407 data->intf->needs_remote_wakeup = 0;
1409 /* Enable remote wake up for auto-suspend */
1410 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1411 data->intf->needs_remote_wakeup = 1;
1413 usb_autopm_put_interface(data->intf);
1416 usb_scuttle_anchored_urbs(&data->deferred);
1420 static int btusb_flush(struct hci_dev *hdev)
1422 struct btusb_data *data = hci_get_drvdata(hdev);
1424 BT_DBG("%s", hdev->name);
1426 usb_kill_anchored_urbs(&data->tx_anchor);
1427 btusb_free_frags(data);
1432 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1434 struct btusb_data *data = hci_get_drvdata(hdev);
1435 struct usb_ctrlrequest *dr;
1439 urb = usb_alloc_urb(0, GFP_KERNEL);
1441 return ERR_PTR(-ENOMEM);
1443 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1446 return ERR_PTR(-ENOMEM);
1449 dr->bRequestType = data->cmdreq_type;
1450 dr->bRequest = data->cmdreq;
1453 dr->wLength = __cpu_to_le16(skb->len);
1455 pipe = usb_sndctrlpipe(data->udev, 0x00);
1457 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1458 skb->data, skb->len, btusb_tx_complete, skb);
1460 skb->dev = (void *)hdev;
1465 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1467 struct btusb_data *data = hci_get_drvdata(hdev);
1471 if (!data->bulk_tx_ep)
1472 return ERR_PTR(-ENODEV);
1474 urb = usb_alloc_urb(0, GFP_KERNEL);
1476 return ERR_PTR(-ENOMEM);
1478 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1480 usb_fill_bulk_urb(urb, data->udev, pipe,
1481 skb->data, skb->len, btusb_tx_complete, skb);
1483 skb->dev = (void *)hdev;
1488 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1490 struct btusb_data *data = hci_get_drvdata(hdev);
1494 if (!data->isoc_tx_ep)
1495 return ERR_PTR(-ENODEV);
1497 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1499 return ERR_PTR(-ENOMEM);
1501 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1503 usb_fill_int_urb(urb, data->udev, pipe,
1504 skb->data, skb->len, btusb_isoc_tx_complete,
1505 skb, data->isoc_tx_ep->bInterval);
1507 urb->transfer_flags = URB_ISO_ASAP;
1509 if (data->isoc_altsetting == 6)
1510 __fill_isoc_descriptor_msbc(urb, skb->len,
1511 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1514 __fill_isoc_descriptor(urb, skb->len,
1515 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1516 skb->dev = (void *)hdev;
1521 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1523 struct btusb_data *data = hci_get_drvdata(hdev);
1526 usb_anchor_urb(urb, &data->tx_anchor);
1528 err = usb_submit_urb(urb, GFP_KERNEL);
1530 if (err != -EPERM && err != -ENODEV)
1531 bt_dev_err(hdev, "urb %p submission failed (%d)",
1533 kfree(urb->setup_packet);
1534 usb_unanchor_urb(urb);
1536 usb_mark_last_busy(data->udev);
1543 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1545 struct btusb_data *data = hci_get_drvdata(hdev);
1546 unsigned long flags;
1549 spin_lock_irqsave(&data->txlock, flags);
1550 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1552 data->tx_in_flight++;
1553 spin_unlock_irqrestore(&data->txlock, flags);
1556 return submit_tx_urb(hdev, urb);
1558 usb_anchor_urb(urb, &data->deferred);
1559 schedule_work(&data->waker);
1565 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1569 BT_DBG("%s", hdev->name);
1571 switch (hci_skb_pkt_type(skb)) {
1572 case HCI_COMMAND_PKT:
1573 urb = alloc_ctrl_urb(hdev, skb);
1575 return PTR_ERR(urb);
1577 hdev->stat.cmd_tx++;
1578 return submit_or_queue_tx_urb(hdev, urb);
1580 case HCI_ACLDATA_PKT:
1581 urb = alloc_bulk_urb(hdev, skb);
1583 return PTR_ERR(urb);
1585 hdev->stat.acl_tx++;
1586 return submit_or_queue_tx_urb(hdev, urb);
1588 case HCI_SCODATA_PKT:
1589 if (hci_conn_num(hdev, SCO_LINK) < 1)
1592 urb = alloc_isoc_urb(hdev, skb);
1594 return PTR_ERR(urb);
1596 hdev->stat.sco_tx++;
1597 return submit_tx_urb(hdev, urb);
1603 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1605 struct btusb_data *data = hci_get_drvdata(hdev);
1607 BT_DBG("%s evt %d", hdev->name, evt);
1609 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1610 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1611 data->air_mode = evt;
1612 schedule_work(&data->work);
1616 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1618 struct btusb_data *data = hci_get_drvdata(hdev);
1619 struct usb_interface *intf = data->isoc;
1620 struct usb_endpoint_descriptor *ep_desc;
1626 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
1628 bt_dev_err(hdev, "setting interface failed (%d)", -err);
1632 data->isoc_altsetting = altsetting;
1634 data->isoc_tx_ep = NULL;
1635 data->isoc_rx_ep = NULL;
1637 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1638 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1640 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1641 data->isoc_tx_ep = ep_desc;
1645 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1646 data->isoc_rx_ep = ep_desc;
1651 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1652 bt_dev_err(hdev, "invalid SCO descriptors");
1659 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1661 struct btusb_data *data = hci_get_drvdata(hdev);
1664 if (data->isoc_altsetting != new_alts) {
1665 unsigned long flags;
1667 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1668 usb_kill_anchored_urbs(&data->isoc_anchor);
1670 /* When isochronous alternate setting needs to be
1671 * changed, because SCO connection has been added
1672 * or removed, a packet fragment may be left in the
1673 * reassembling state. This could lead to wrongly
1674 * assembled fragments.
1676 * Clear outstanding fragment when selecting a new
1677 * alternate setting.
1679 spin_lock_irqsave(&data->rxlock, flags);
1680 kfree_skb(data->sco_skb);
1681 data->sco_skb = NULL;
1682 spin_unlock_irqrestore(&data->rxlock, flags);
1684 err = __set_isoc_interface(hdev, new_alts);
1689 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1690 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1691 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1693 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1699 static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1702 struct usb_interface *intf = data->isoc;
1705 BT_DBG("Looking for Alt no :%d", alt);
1710 for (i = 0; i < intf->num_altsetting; i++) {
1711 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1712 return &intf->altsetting[i];
1718 static void btusb_work(struct work_struct *work)
1720 struct btusb_data *data = container_of(work, struct btusb_data, work);
1721 struct hci_dev *hdev = data->hdev;
1725 if (data->sco_num > 0) {
1726 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1727 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1729 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1730 usb_kill_anchored_urbs(&data->isoc_anchor);
1734 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1737 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1738 if (hdev->voice_setting & 0x0020) {
1739 static const int alts[3] = { 2, 4, 5 };
1741 new_alts = alts[data->sco_num - 1];
1743 new_alts = data->sco_num;
1745 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
1746 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1747 * many adapters do not support it. Alt 1 appears to
1748 * work for all adapters that do not have alt 6, and
1749 * which work with WBS at all.
1751 new_alts = btusb_find_altsetting(data, 6) ? 6 : 1;
1754 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1755 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
1757 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1758 usb_kill_anchored_urbs(&data->isoc_anchor);
1760 __set_isoc_interface(hdev, 0);
1761 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1762 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1766 static void btusb_waker(struct work_struct *work)
1768 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1771 err = usb_autopm_get_interface(data->intf);
1775 usb_autopm_put_interface(data->intf);
1778 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1780 struct sk_buff *skb;
1783 BT_DBG("%s", hdev->name);
1785 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1787 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
1794 static int btusb_setup_csr(struct hci_dev *hdev)
1796 struct btusb_data *data = hci_get_drvdata(hdev);
1797 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
1798 struct hci_rp_read_local_version *rp;
1799 struct sk_buff *skb;
1800 bool is_fake = false;
1803 BT_DBG("%s", hdev->name);
1805 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1808 int err = PTR_ERR(skb);
1809 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
1813 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1814 bt_dev_err(hdev, "CSR: Local version length mismatch");
1819 rp = (struct hci_rp_read_local_version *)skb->data;
1821 /* Detect a wide host of Chinese controllers that aren't CSR.
1823 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1825 * The main thing they have in common is that these are really popular low-cost
1826 * options that support newer Bluetooth versions but rely on heavy VID/PID
1827 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1829 * We detect actual CSR devices by checking that the HCI manufacturer code
1830 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1831 * HCI rev values always match. As they both store the firmware number.
1833 if (le16_to_cpu(rp->manufacturer) != 10 ||
1834 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
1837 /* Known legit CSR firmware build numbers and their supported BT versions:
1838 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1839 * - 1.2 (0x2) -> 0x04d9, 0x0529
1840 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
1841 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
1842 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
1844 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
1845 * support BT 1.1 only; so it's a dead giveaway when some
1846 * third-party BT 4.0 dongle reuses it.
1848 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
1849 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
1852 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
1853 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
1856 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
1857 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
1860 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
1861 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
1864 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
1865 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
1868 /* Other clones which beat all the above checks */
1869 else if (bcdDevice == 0x0134 &&
1870 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
1871 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
1875 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds...");
1877 /* Generally these clones have big discrepancies between
1878 * advertised features and what's actually supported.
1879 * Probably will need to be expanded in the future;
1880 * without these the controller will lock up.
1882 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1883 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
1885 /* Clear the reset quirk since this is not an actual
1886 * early Bluetooth 1.1 device from CSR.
1888 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1889 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1892 * Special workaround for clones with a Barrot 8041a02 chip,
1893 * these clones are really messed-up:
1894 * 1. Their bulk rx endpoint will never report any data unless
1895 * the device was suspended at least once (yes really).
1896 * 2. They will not wakeup when autosuspended and receiving data
1897 * on their bulk rx endpoint from e.g. a keyboard or mouse
1898 * (IOW remote-wakeup support is broken for the bulk endpoint).
1900 * To fix 1. enable runtime-suspend, force-suspend the
1901 * hci and then wake-it up by disabling runtime-suspend.
1903 * To fix 2. clear the hci's can_wake flag, this way the hci
1904 * will still be autosuspended when it is not open.
1906 if (bcdDevice == 0x8891 &&
1907 le16_to_cpu(rp->lmp_subver) == 0x1012 &&
1908 le16_to_cpu(rp->hci_rev) == 0x0810 &&
1909 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_4_0) {
1910 bt_dev_warn(hdev, "CSR: detected a fake CSR dongle using a Barrot 8041a02 chip, this chip is very buggy and may have issues");
1912 pm_runtime_allow(&data->udev->dev);
1914 ret = pm_runtime_suspend(&data->udev->dev);
1918 bt_dev_err(hdev, "Failed to suspend the device for Barrot 8041a02 receive-issue workaround");
1920 pm_runtime_forbid(&data->udev->dev);
1922 device_set_wakeup_capable(&data->udev->dev, false);
1923 /* Re-enable autosuspend if this was requested */
1924 if (enable_autosuspend)
1925 usb_enable_autosuspend(data->udev);
1934 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1935 struct intel_version *ver)
1937 const struct firmware *fw;
1941 snprintf(fwname, sizeof(fwname),
1942 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1943 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1944 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1945 ver->fw_build_ww, ver->fw_build_yy);
1947 ret = request_firmware(&fw, fwname, &hdev->dev);
1949 if (ret == -EINVAL) {
1950 bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1955 bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1958 /* If the correct firmware patch file is not found, use the
1959 * default firmware patch file instead
1961 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1962 ver->hw_platform, ver->hw_variant);
1963 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1964 bt_dev_err(hdev, "failed to open default fw file: %s",
1970 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
1975 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1976 const struct firmware *fw,
1977 const u8 **fw_ptr, int *disable_patch)
1979 struct sk_buff *skb;
1980 struct hci_command_hdr *cmd;
1981 const u8 *cmd_param;
1982 struct hci_event_hdr *evt = NULL;
1983 const u8 *evt_param = NULL;
1984 int remain = fw->size - (*fw_ptr - fw->data);
1986 /* The first byte indicates the types of the patch command or event.
1987 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1988 * in the current firmware buffer doesn't start with 0x01 or
1989 * the size of remain buffer is smaller than HCI command header,
1990 * the firmware file is corrupted and it should stop the patching
1993 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1994 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
2000 cmd = (struct hci_command_hdr *)(*fw_ptr);
2001 *fw_ptr += sizeof(*cmd);
2002 remain -= sizeof(*cmd);
2004 /* Ensure that the remain firmware data is long enough than the length
2005 * of command parameter. If not, the firmware file is corrupted.
2007 if (remain < cmd->plen) {
2008 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
2012 /* If there is a command that loads a patch in the firmware
2013 * file, then enable the patch upon success, otherwise just
2014 * disable the manufacturer mode, for example patch activation
2015 * is not required when the default firmware patch file is used
2016 * because there are no patch data to load.
2018 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
2021 cmd_param = *fw_ptr;
2022 *fw_ptr += cmd->plen;
2023 remain -= cmd->plen;
2025 /* This reads the expected events when the above command is sent to the
2026 * device. Some vendor commands expects more than one events, for
2027 * example command status event followed by vendor specific event.
2028 * For this case, it only keeps the last expected event. so the command
2029 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
2030 * last expected event.
2032 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
2036 evt = (struct hci_event_hdr *)(*fw_ptr);
2037 *fw_ptr += sizeof(*evt);
2038 remain -= sizeof(*evt);
2040 if (remain < evt->plen) {
2041 bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
2045 evt_param = *fw_ptr;
2046 *fw_ptr += evt->plen;
2047 remain -= evt->plen;
2050 /* Every HCI commands in the firmware file has its correspond event.
2051 * If event is not found or remain is smaller than zero, the firmware
2052 * file is corrupted.
2054 if (!evt || !evt_param || remain < 0) {
2055 bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
2059 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
2060 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
2062 bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
2063 cmd->opcode, PTR_ERR(skb));
2064 return PTR_ERR(skb);
2067 /* It ensures that the returned event matches the event data read from
2068 * the firmware file. At fist, it checks the length and then
2069 * the contents of the event.
2071 if (skb->len != evt->plen) {
2072 bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
2073 le16_to_cpu(cmd->opcode));
2078 if (memcmp(skb->data, evt_param, evt->plen)) {
2079 bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
2080 le16_to_cpu(cmd->opcode));
2089 static int btusb_setup_intel(struct hci_dev *hdev)
2091 struct sk_buff *skb;
2092 const struct firmware *fw;
2094 int disable_patch, err;
2095 struct intel_version ver;
2097 BT_DBG("%s", hdev->name);
2099 /* The controller has a bug with the first HCI command sent to it
2100 * returning number of completed commands as zero. This would stall the
2101 * command processing in the Bluetooth core.
2103 * As a workaround, send HCI Reset command first which will reset the
2104 * number of completed commands and allow normal command processing
2107 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2109 bt_dev_err(hdev, "sending initial HCI reset command failed (%ld)",
2111 return PTR_ERR(skb);
2115 /* Read Intel specific controller version first to allow selection of
2116 * which firmware file to load.
2118 * The returned information are hardware variant and revision plus
2119 * firmware variant, revision and build number.
2121 err = btintel_read_version(hdev, &ver);
2125 bt_dev_info(hdev, "read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
2126 ver.hw_platform, ver.hw_variant, ver.hw_revision,
2127 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
2128 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
2130 /* fw_patch_num indicates the version of patch the device currently
2131 * have. If there is no patch data in the device, it is always 0x00.
2132 * So, if it is other than 0x00, no need to patch the device again.
2134 if (ver.fw_patch_num) {
2135 bt_dev_info(hdev, "Intel device is already patched. "
2136 "patch num: %02x", ver.fw_patch_num);
2140 /* Opens the firmware patch file based on the firmware version read
2141 * from the controller. If it fails to open the matching firmware
2142 * patch file, it tries to open the default firmware patch file.
2143 * If no patch file is found, allow the device to operate without
2146 fw = btusb_setup_intel_get_fw(hdev, &ver);
2151 /* Enable the manufacturer mode of the controller.
2152 * Only while this mode is enabled, the driver can download the
2153 * firmware patch data and configuration parameters.
2155 err = btintel_enter_mfg(hdev);
2157 release_firmware(fw);
2163 /* The firmware data file consists of list of Intel specific HCI
2164 * commands and its expected events. The first byte indicates the
2165 * type of the message, either HCI command or HCI event.
2167 * It reads the command and its expected event from the firmware file,
2168 * and send to the controller. Once __hci_cmd_sync_ev() returns,
2169 * the returned event is compared with the event read from the firmware
2170 * file and it will continue until all the messages are downloaded to
2173 * Once the firmware patching is completed successfully,
2174 * the manufacturer mode is disabled with reset and activating the
2177 * If the firmware patching fails, the manufacturer mode is
2178 * disabled with reset and deactivating the patch.
2180 * If the default patch file is used, no reset is done when disabling
2183 while (fw->size > fw_ptr - fw->data) {
2186 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
2189 goto exit_mfg_deactivate;
2192 release_firmware(fw);
2195 goto exit_mfg_disable;
2197 /* Patching completed successfully and disable the manufacturer mode
2198 * with reset and activate the downloaded firmware patches.
2200 err = btintel_exit_mfg(hdev, true, true);
2204 /* Need build number for downloaded fw patches in
2205 * every power-on boot
2207 err = btintel_read_version(hdev, &ver);
2210 bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
2216 /* Disable the manufacturer mode without reset */
2217 err = btintel_exit_mfg(hdev, false, false);
2221 bt_dev_info(hdev, "Intel firmware patch completed");
2225 exit_mfg_deactivate:
2226 release_firmware(fw);
2228 /* Patching failed. Disable the manufacturer mode with reset and
2229 * deactivate the downloaded firmware patches.
2231 err = btintel_exit_mfg(hdev, true, false);
2235 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
2238 /* Set the event mask for Intel specific vendor events. This enables
2239 * a few extra events that are useful during general operation.
2241 btintel_set_event_mask_mfg(hdev, false);
2243 btintel_check_bdaddr(hdev);
2247 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2249 struct sk_buff *skb;
2250 struct hci_event_hdr *hdr;
2251 struct hci_ev_cmd_complete *evt;
2253 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2257 hdr = skb_put(skb, sizeof(*hdr));
2258 hdr->evt = HCI_EV_CMD_COMPLETE;
2259 hdr->plen = sizeof(*evt) + 1;
2261 evt = skb_put(skb, sizeof(*evt));
2263 evt->opcode = cpu_to_le16(opcode);
2265 skb_put_u8(skb, 0x00);
2267 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2269 return hci_recv_frame(hdev, skb);
2272 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2275 /* When the device is in bootloader mode, then it can send
2276 * events via the bulk endpoint. These events are treated the
2277 * same way as the ones received from the interrupt endpoint.
2279 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
2280 return btusb_recv_intr(data, buffer, count);
2282 return btusb_recv_bulk(data, buffer, count);
2285 static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
2288 const struct intel_bootup *evt = ptr;
2290 if (len != sizeof(*evt))
2293 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags))
2294 wake_up_bit(&data->flags, BTUSB_BOOTING);
2297 static void btusb_intel_secure_send_result(struct btusb_data *data,
2298 const void *ptr, unsigned int len)
2300 const struct intel_secure_send_result *evt = ptr;
2302 if (len != sizeof(*evt))
2306 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
2308 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
2309 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags))
2310 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
2313 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2315 struct btusb_data *data = hci_get_drvdata(hdev);
2317 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2318 struct hci_event_hdr *hdr = (void *)skb->data;
2320 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2322 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2323 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
2325 switch (skb->data[2]) {
2327 /* When switching to the operational firmware
2328 * the device sends a vendor specific event
2329 * indicating that the bootup completed.
2331 btusb_intel_bootup(data, ptr, len);
2334 /* When the firmware loading completes the
2335 * device sends out a vendor specific event
2336 * indicating the result of the firmware
2339 btusb_intel_secure_send_result(data, ptr, len);
2345 return hci_recv_frame(hdev, skb);
2348 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2350 struct btusb_data *data = hci_get_drvdata(hdev);
2353 BT_DBG("%s", hdev->name);
2355 switch (hci_skb_pkt_type(skb)) {
2356 case HCI_COMMAND_PKT:
2357 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2358 struct hci_command_hdr *cmd = (void *)skb->data;
2359 __u16 opcode = le16_to_cpu(cmd->opcode);
2361 /* When in bootloader mode and the command 0xfc09
2362 * is received, it needs to be send down the
2363 * bulk endpoint. So allocate a bulk URB instead.
2365 if (opcode == 0xfc09)
2366 urb = alloc_bulk_urb(hdev, skb);
2368 urb = alloc_ctrl_urb(hdev, skb);
2370 /* When the 0xfc01 command is issued to boot into
2371 * the operational firmware, it will actually not
2372 * send a command complete event. To keep the flow
2373 * control working inject that event here.
2375 if (opcode == 0xfc01)
2376 inject_cmd_complete(hdev, opcode);
2378 urb = alloc_ctrl_urb(hdev, skb);
2381 return PTR_ERR(urb);
2383 hdev->stat.cmd_tx++;
2384 return submit_or_queue_tx_urb(hdev, urb);
2386 case HCI_ACLDATA_PKT:
2387 urb = alloc_bulk_urb(hdev, skb);
2389 return PTR_ERR(urb);
2391 hdev->stat.acl_tx++;
2392 return submit_or_queue_tx_urb(hdev, urb);
2394 case HCI_SCODATA_PKT:
2395 if (hci_conn_num(hdev, SCO_LINK) < 1)
2398 urb = alloc_isoc_urb(hdev, skb);
2400 return PTR_ERR(urb);
2402 hdev->stat.sco_tx++;
2403 return submit_tx_urb(hdev, urb);
2409 static int btusb_setup_intel_new_get_fw_name(struct intel_version *ver,
2410 struct intel_boot_params *params,
2411 char *fw_name, size_t len,
2414 switch (ver->hw_variant) {
2415 case 0x0b: /* SfP */
2416 case 0x0c: /* WsP */
2417 snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
2418 le16_to_cpu(ver->hw_variant),
2419 le16_to_cpu(params->dev_revid),
2422 case 0x11: /* JfP */
2423 case 0x12: /* ThP */
2424 case 0x13: /* HrP */
2425 case 0x14: /* CcP */
2426 snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
2427 le16_to_cpu(ver->hw_variant),
2428 le16_to_cpu(ver->hw_revision),
2429 le16_to_cpu(ver->fw_revision),
2439 static void btusb_setup_intel_newgen_get_fw_name(const struct intel_version_tlv *ver_tlv,
2440 char *fw_name, size_t len,
2443 /* The firmware file name for new generation controllers will be
2444 * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
2446 snprintf(fw_name, len, "intel/ibt-%04x-%04x.%s",
2447 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver_tlv->cnvi_top),
2448 INTEL_CNVX_TOP_STEP(ver_tlv->cnvi_top)),
2449 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver_tlv->cnvr_top),
2450 INTEL_CNVX_TOP_STEP(ver_tlv->cnvr_top)),
2454 static int btusb_download_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
2456 struct btusb_data *data = hci_get_drvdata(hdev);
2457 ktime_t delta, rettime;
2458 unsigned long long duration;
2461 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2463 bt_dev_info(hdev, "Waiting for firmware download to complete");
2465 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2467 msecs_to_jiffies(msec));
2468 if (err == -EINTR) {
2469 bt_dev_err(hdev, "Firmware loading interrupted");
2474 bt_dev_err(hdev, "Firmware loading timeout");
2478 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2479 bt_dev_err(hdev, "Firmware loading failed");
2483 rettime = ktime_get();
2484 delta = ktime_sub(rettime, calltime);
2485 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2487 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
2492 static int btusb_intel_download_firmware_newgen(struct hci_dev *hdev,
2493 struct intel_version_tlv *ver,
2496 const struct firmware *fw;
2499 struct btusb_data *data = hci_get_drvdata(hdev);
2502 if (!ver || !boot_param)
2505 /* The firmware variant determines if the device is in bootloader
2506 * mode or is running operational firmware. The value 0x03 identifies
2507 * the bootloader and the value 0x23 identifies the operational
2510 * When the operational firmware is already present, then only
2511 * the check for valid Bluetooth device address is needed. This
2512 * determines if the device will be added as configured or
2513 * unconfigured controller.
2515 * It is not possible to use the Secure Boot Parameters in this
2516 * case since that command is only available in bootloader mode.
2518 if (ver->img_type == 0x03) {
2519 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2520 btintel_check_bdaddr(hdev);
2523 /* If the OTP has no valid Bluetooth device address, then there will
2524 * also be no valid address for the operational firmware.
2526 if (!bacmp(&ver->otp_bd_addr, BDADDR_ANY)) {
2527 bt_dev_info(hdev, "No device address configured");
2528 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2531 btusb_setup_intel_newgen_get_fw_name(ver, fwname, sizeof(fwname), "sfi");
2532 err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
2534 if (!test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2535 /* Firmware has already been loaded */
2536 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2540 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2546 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2548 if (fw->size < 644) {
2549 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2555 calltime = ktime_get();
2557 set_bit(BTUSB_DOWNLOADING, &data->flags);
2559 /* Start firmware downloading and get boot parameter */
2560 err = btintel_download_firmware_newgen(hdev, ver, fw, boot_param,
2561 INTEL_HW_VARIANT(ver->cnvi_bt),
2564 if (err == -EALREADY) {
2565 /* Firmware has already been loaded */
2566 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2571 /* When FW download fails, send Intel Reset to retry
2574 btintel_reset_to_bootloader(hdev);
2578 /* Before switching the device into operational mode and with that
2579 * booting the loaded firmware, wait for the bootloader notification
2580 * that all fragments have been successfully received.
2582 * When the event processing receives the notification, then the
2583 * BTUSB_DOWNLOADING flag will be cleared.
2585 * The firmware loading should not take longer than 5 seconds
2586 * and thus just timeout if that happens and fail the setup
2589 err = btusb_download_wait(hdev, calltime, 5000);
2590 if (err == -ETIMEDOUT)
2591 btintel_reset_to_bootloader(hdev);
2594 release_firmware(fw);
2598 static int btusb_intel_download_firmware(struct hci_dev *hdev,
2599 struct intel_version *ver,
2600 struct intel_boot_params *params,
2603 const struct firmware *fw;
2606 struct btusb_data *data = hci_get_drvdata(hdev);
2609 if (!ver || !params)
2612 /* The firmware variant determines if the device is in bootloader
2613 * mode or is running operational firmware. The value 0x06 identifies
2614 * the bootloader and the value 0x23 identifies the operational
2617 * When the operational firmware is already present, then only
2618 * the check for valid Bluetooth device address is needed. This
2619 * determines if the device will be added as configured or
2620 * unconfigured controller.
2622 * It is not possible to use the Secure Boot Parameters in this
2623 * case since that command is only available in bootloader mode.
2625 if (ver->fw_variant == 0x23) {
2626 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2627 btintel_check_bdaddr(hdev);
2629 /* SfP and WsP don't seem to update the firmware version on file
2630 * so version checking is currently possible.
2632 switch (ver->hw_variant) {
2633 case 0x0b: /* SfP */
2634 case 0x0c: /* WsP */
2638 /* Proceed to download to check if the version matches */
2642 /* Read the secure boot parameters to identify the operating
2643 * details of the bootloader.
2645 err = btintel_read_boot_params(hdev, params);
2649 /* It is required that every single firmware fragment is acknowledged
2650 * with a command complete event. If the boot parameters indicate
2651 * that this bootloader does not send them, then abort the setup.
2653 if (params->limited_cce != 0x00) {
2654 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
2655 params->limited_cce);
2659 /* If the OTP has no valid Bluetooth device address, then there will
2660 * also be no valid address for the operational firmware.
2662 if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) {
2663 bt_dev_info(hdev, "No device address configured");
2664 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2668 /* With this Intel bootloader only the hardware variant and device
2669 * revision information are used to select the right firmware for SfP
2672 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2674 * Currently the supported hardware variants are:
2675 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2676 * 12 (0x0c) for iBT3.5 (WsP)
2678 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2679 * variant, HW revision and FW revision, as these are dependent on CNVi
2680 * and RF Combination.
2682 * 17 (0x11) for iBT3.5 (JfP)
2683 * 18 (0x12) for iBT3.5 (ThP)
2685 * The firmware file name for these will be
2686 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2689 err = btusb_setup_intel_new_get_fw_name(ver, params, fwname,
2690 sizeof(fwname), "sfi");
2692 if (!test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2693 /* Firmware has already been loaded */
2694 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2698 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2702 err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
2704 if (!test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2705 /* Firmware has already been loaded */
2706 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2710 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2715 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2717 if (fw->size < 644) {
2718 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2724 calltime = ktime_get();
2726 set_bit(BTUSB_DOWNLOADING, &data->flags);
2728 /* Start firmware downloading and get boot parameter */
2729 err = btintel_download_firmware(hdev, ver, fw, boot_param);
2731 if (err == -EALREADY) {
2732 /* Firmware has already been loaded */
2733 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2738 /* When FW download fails, send Intel Reset to retry
2741 btintel_reset_to_bootloader(hdev);
2745 /* Before switching the device into operational mode and with that
2746 * booting the loaded firmware, wait for the bootloader notification
2747 * that all fragments have been successfully received.
2749 * When the event processing receives the notification, then the
2750 * BTUSB_DOWNLOADING flag will be cleared.
2752 * The firmware loading should not take longer than 5 seconds
2753 * and thus just timeout if that happens and fail the setup
2756 err = btusb_download_wait(hdev, calltime, 5000);
2757 if (err == -ETIMEDOUT)
2758 btintel_reset_to_bootloader(hdev);
2761 release_firmware(fw);
2765 static int btusb_boot_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
2767 struct btusb_data *data = hci_get_drvdata(hdev);
2768 ktime_t delta, rettime;
2769 unsigned long long duration;
2772 bt_dev_info(hdev, "Waiting for device to boot");
2774 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2776 msecs_to_jiffies(msec));
2777 if (err == -EINTR) {
2778 bt_dev_err(hdev, "Device boot interrupted");
2783 bt_dev_err(hdev, "Device boot timeout");
2787 rettime = ktime_get();
2788 delta = ktime_sub(rettime, calltime);
2789 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2791 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
2796 static int btusb_intel_boot(struct hci_dev *hdev, u32 boot_addr)
2798 struct btusb_data *data = hci_get_drvdata(hdev);
2802 calltime = ktime_get();
2804 set_bit(BTUSB_BOOTING, &data->flags);
2806 err = btintel_send_intel_reset(hdev, boot_addr);
2808 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
2809 btintel_reset_to_bootloader(hdev);
2813 /* The bootloader will not indicate when the device is ready. This
2814 * is done by the operational firmware sending bootup notification.
2816 * Booting into operational firmware should not take longer than
2817 * 1 second. However if that happens, then just fail the setup
2818 * since something went wrong.
2820 err = btusb_boot_wait(hdev, calltime, 1000);
2821 if (err == -ETIMEDOUT)
2822 btintel_reset_to_bootloader(hdev);
2827 static int btusb_setup_intel_new(struct hci_dev *hdev)
2829 struct btusb_data *data = hci_get_drvdata(hdev);
2830 struct intel_version ver;
2831 struct intel_boot_params params;
2835 struct intel_debug_features features;
2837 BT_DBG("%s", hdev->name);
2839 /* Set the default boot parameter to 0x0 and it is updated to
2840 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2841 * command while downloading the firmware.
2843 boot_param = 0x00000000;
2845 /* Read the Intel version information to determine if the device
2846 * is in bootloader mode or if it already has operational firmware
2849 err = btintel_read_version(hdev, &ver);
2851 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2852 btintel_reset_to_bootloader(hdev);
2856 err = btintel_version_info(hdev, &ver);
2860 err = btusb_intel_download_firmware(hdev, &ver, ¶ms, &boot_param);
2864 /* controller is already having an operational firmware */
2865 if (ver.fw_variant == 0x23)
2868 err = btusb_intel_boot(hdev, boot_param);
2872 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2874 err = btusb_setup_intel_new_get_fw_name(&ver, ¶ms, ddcname,
2875 sizeof(ddcname), "ddc");
2878 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2880 /* Once the device is running in operational mode, it needs to
2881 * apply the device configuration (DDC) parameters.
2883 * The device can work without DDC parameters, so even if it
2884 * fails to load the file, no need to fail the setup.
2886 btintel_load_ddc_config(hdev, ddcname);
2889 /* Read the Intel supported features and if new exception formats
2890 * supported, need to load the additional DDC config to enable.
2892 btintel_read_debug_features(hdev, &features);
2894 /* Set DDC mask for available debug features */
2895 btintel_set_debug_features(hdev, &features);
2897 /* Read the Intel version information after loading the FW */
2898 err = btintel_read_version(hdev, &ver);
2902 btintel_version_info(hdev, &ver);
2905 /* All Intel controllers that support the Microsoft vendor
2906 * extension are using 0xFC1E for VsMsftOpCode.
2908 switch (ver.hw_variant) {
2909 case 0x11: /* JfP */
2910 case 0x12: /* ThP */
2911 case 0x13: /* HrP */
2912 case 0x14: /* CcP */
2913 hci_set_msft_opcode(hdev, 0xFC1E);
2917 /* Set the event mask for Intel specific vendor events. This enables
2918 * a few extra events that are useful during general operation. It
2919 * does not enable any debugging related events.
2921 * The device will function correctly without these events enabled
2922 * and thus no need to fail the setup.
2924 btintel_set_event_mask(hdev, false);
2929 static int btusb_setup_intel_newgen(struct hci_dev *hdev)
2931 struct btusb_data *data = hci_get_drvdata(hdev);
2935 struct intel_debug_features features;
2936 struct intel_version_tlv version;
2938 bt_dev_dbg(hdev, "");
2940 /* Set the default boot parameter to 0x0 and it is updated to
2941 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2942 * command while downloading the firmware.
2944 boot_param = 0x00000000;
2946 /* Read the Intel version information to determine if the device
2947 * is in bootloader mode or if it already has operational firmware
2950 err = btintel_read_version_tlv(hdev, &version);
2952 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2953 btintel_reset_to_bootloader(hdev);
2957 err = btintel_version_info_tlv(hdev, &version);
2961 err = btusb_intel_download_firmware_newgen(hdev, &version, &boot_param);
2965 /* check if controller is already having an operational firmware */
2966 if (version.img_type == 0x03)
2969 err = btusb_intel_boot(hdev, boot_param);
2973 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2975 btusb_setup_intel_newgen_get_fw_name(&version, ddcname, sizeof(ddcname),
2977 /* Once the device is running in operational mode, it needs to
2978 * apply the device configuration (DDC) parameters.
2980 * The device can work without DDC parameters, so even if it
2981 * fails to load the file, no need to fail the setup.
2983 btintel_load_ddc_config(hdev, ddcname);
2985 /* Read the Intel supported features and if new exception formats
2986 * supported, need to load the additional DDC config to enable.
2988 btintel_read_debug_features(hdev, &features);
2990 /* Set DDC mask for available debug features */
2991 btintel_set_debug_features(hdev, &features);
2993 /* Read the Intel version information after loading the FW */
2994 err = btintel_read_version_tlv(hdev, &version);
2998 btintel_version_info_tlv(hdev, &version);
3001 /* Set the event mask for Intel specific vendor events. This enables
3002 * a few extra events that are useful during general operation. It
3003 * does not enable any debugging related events.
3005 * The device will function correctly without these events enabled
3006 * and thus no need to fail the setup.
3008 btintel_set_event_mask(hdev, false);
3012 static int btusb_shutdown_intel(struct hci_dev *hdev)
3014 struct sk_buff *skb;
3017 /* In the shutdown sequence where Bluetooth is turned off followed
3018 * by WiFi being turned off, turning WiFi back on causes issue with
3019 * the RF calibration.
3021 * To ensure that any RF activity has been stopped, issue HCI Reset
3022 * command to clear all ongoing activity including advertising,
3025 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3028 bt_dev_err(hdev, "HCI reset during shutdown failed");
3033 /* Some platforms have an issue with BT LED when the interface is
3034 * down or BT radio is turned off, which takes 5 seconds to BT LED
3035 * goes off. This command turns off the BT LED immediately.
3037 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
3040 bt_dev_err(hdev, "turning off Intel device LED failed");
3048 static int btusb_shutdown_intel_new(struct hci_dev *hdev)
3050 struct sk_buff *skb;
3052 /* Send HCI Reset to the controller to stop any BT activity which
3053 * were triggered. This will help to save power and maintain the
3054 * sync b/w Host and controller
3056 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3058 bt_dev_err(hdev, "HCI reset during shutdown failed");
3059 return PTR_ERR(skb);
3066 #define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
3067 #define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
3069 #define HCI_WMT_MAX_EVENT_SIZE 64
3070 /* It is for mt79xx download rom patch*/
3071 #define MTK_FW_ROM_PATCH_HEADER_SIZE 32
3072 #define MTK_FW_ROM_PATCH_GD_SIZE 64
3073 #define MTK_FW_ROM_PATCH_SEC_MAP_SIZE 64
3074 #define MTK_SEC_MAP_COMMON_SIZE 12
3075 #define MTK_SEC_MAP_NEED_SEND_SIZE 52
3078 BTMTK_WMT_PATCH_DWNLD = 0x1,
3079 BTMTK_WMT_FUNC_CTRL = 0x6,
3080 BTMTK_WMT_RST = 0x7,
3081 BTMTK_WMT_SEMAPHORE = 0x17,
3086 BTMTK_WMT_PATCH_UNDONE,
3087 BTMTK_WMT_PATCH_PROGRESS,
3088 BTMTK_WMT_PATCH_DONE,
3089 BTMTK_WMT_ON_UNDONE,
3091 BTMTK_WMT_ON_PROGRESS,
3094 struct btmtk_wmt_hdr {
3101 struct btmtk_hci_wmt_cmd {
3102 struct btmtk_wmt_hdr hdr;
3106 struct btmtk_hci_wmt_evt {
3107 struct hci_event_hdr hhdr;
3108 struct btmtk_wmt_hdr whdr;
3111 struct btmtk_hci_wmt_evt_funcc {
3112 struct btmtk_hci_wmt_evt hwhdr;
3116 struct btmtk_tci_sleep {
3119 __le16 host_duration;
3121 u8 time_compensation;
3124 struct btmtk_hci_wmt_params {
3132 struct btmtk_patch_header {
3140 struct btmtk_global_desc {
3147 struct btmtk_section_map {
3152 __le32 u4SecSpec[13];
3159 __le32 dlmodecrctype;
3166 static void btusb_mtk_wmt_recv(struct urb *urb)
3168 struct hci_dev *hdev = urb->context;
3169 struct btusb_data *data = hci_get_drvdata(hdev);
3170 struct hci_event_hdr *hdr;
3171 struct sk_buff *skb;
3174 if (urb->status == 0 && urb->actual_length > 0) {
3175 hdev->stat.byte_rx += urb->actual_length;
3177 /* WMT event shouldn't be fragmented and the size should be
3178 * less than HCI_WMT_MAX_EVENT_SIZE.
3180 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
3182 hdev->stat.err_rx++;
3186 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
3187 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
3189 hdr = (void *)skb->data;
3190 /* Fix up the vendor event id with 0xff for vendor specific
3191 * instead of 0xe4 so that event send via monitoring socket can
3192 * be parsed properly.
3196 /* When someone waits for the WMT event, the skb is being cloned
3197 * and being processed the events from there then.
3199 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
3200 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
3201 if (!data->evt_skb) {
3207 err = hci_recv_frame(hdev, skb);
3209 kfree_skb(data->evt_skb);
3210 data->evt_skb = NULL;
3214 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
3216 /* Barrier to sync with other CPUs */
3217 smp_mb__after_atomic();
3218 wake_up_bit(&data->flags,
3219 BTUSB_TX_WAIT_VND_EVT);
3222 } else if (urb->status == -ENOENT) {
3223 /* Avoid suspend failed when usb_kill_urb */
3227 usb_mark_last_busy(data->udev);
3229 /* The URB complete handler is still called with urb->actual_length = 0
3230 * when the event is not available, so we should keep re-submitting
3231 * URB until WMT event returns, Also, It's necessary to wait some time
3232 * between the two consecutive control URBs to relax the target device
3233 * to generate the event. Otherwise, the WMT event cannot return from
3234 * the device successfully.
3238 usb_anchor_urb(urb, &data->ctrl_anchor);
3239 err = usb_submit_urb(urb, GFP_ATOMIC);
3241 /* -EPERM: urb is being killed;
3242 * -ENODEV: device got disconnected
3244 if (err != -EPERM && err != -ENODEV)
3245 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
3247 usb_unanchor_urb(urb);
3251 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
3253 struct btusb_data *data = hci_get_drvdata(hdev);
3254 struct usb_ctrlrequest *dr;
3260 urb = usb_alloc_urb(0, GFP_KERNEL);
3264 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
3270 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
3272 dr->wIndex = cpu_to_le16(0);
3273 dr->wValue = cpu_to_le16(48);
3274 dr->wLength = cpu_to_le16(size);
3276 buf = kmalloc(size, GFP_KERNEL);
3283 pipe = usb_rcvctrlpipe(data->udev, 0);
3285 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
3286 buf, size, btusb_mtk_wmt_recv, hdev);
3288 urb->transfer_flags |= URB_FREE_BUFFER;
3290 usb_anchor_urb(urb, &data->ctrl_anchor);
3291 err = usb_submit_urb(urb, GFP_KERNEL);
3293 if (err != -EPERM && err != -ENODEV)
3294 bt_dev_err(hdev, "urb %p submission failed (%d)",
3296 usb_unanchor_urb(urb);
3304 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
3305 struct btmtk_hci_wmt_params *wmt_params)
3307 struct btusb_data *data = hci_get_drvdata(hdev);
3308 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
3309 u32 hlen, status = BTMTK_WMT_INVALID;
3310 struct btmtk_hci_wmt_evt *wmt_evt;
3311 struct btmtk_hci_wmt_cmd *wc;
3312 struct btmtk_wmt_hdr *hdr;
3315 /* Submit control IN URB on demand to process the WMT event */
3316 err = btusb_mtk_submit_wmt_recv_urb(hdev);
3320 /* Send the WMT command and wait until the WMT event returns */
3321 hlen = sizeof(*hdr) + wmt_params->dlen;
3325 wc = kzalloc(hlen, GFP_KERNEL);
3331 hdr->op = wmt_params->op;
3332 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
3333 hdr->flag = wmt_params->flag;
3334 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
3336 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3338 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
3341 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3345 /* The vendor specific WMT commands are all answered by a vendor
3346 * specific event and will have the Command Status or Command
3347 * Complete as with usual HCI command flow control.
3349 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
3350 * state to be cleared. The driver specific event receive routine
3351 * will clear that state and with that indicate completion of the
3354 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
3355 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
3356 if (err == -EINTR) {
3357 bt_dev_err(hdev, "Execution of wmt command interrupted");
3358 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3363 bt_dev_err(hdev, "Execution of wmt command timed out");
3364 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3369 /* Parse and handle the return WMT event */
3370 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
3371 if (wmt_evt->whdr.op != hdr->op) {
3372 bt_dev_err(hdev, "Wrong op received %d expected %d",
3373 wmt_evt->whdr.op, hdr->op);
3378 switch (wmt_evt->whdr.op) {
3379 case BTMTK_WMT_SEMAPHORE:
3380 if (wmt_evt->whdr.flag == 2)
3381 status = BTMTK_WMT_PATCH_UNDONE;
3383 status = BTMTK_WMT_PATCH_DONE;
3385 case BTMTK_WMT_FUNC_CTRL:
3386 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
3387 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
3388 status = BTMTK_WMT_ON_DONE;
3389 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
3390 status = BTMTK_WMT_ON_PROGRESS;
3392 status = BTMTK_WMT_ON_UNDONE;
3394 case BTMTK_WMT_PATCH_DWNLD:
3395 if (wmt_evt->whdr.flag == 2)
3396 status = BTMTK_WMT_PATCH_DONE;
3397 else if (wmt_evt->whdr.flag == 1)
3398 status = BTMTK_WMT_PATCH_PROGRESS;
3400 status = BTMTK_WMT_PATCH_UNDONE;
3404 if (wmt_params->status)
3405 *wmt_params->status = status;
3408 kfree_skb(data->evt_skb);
3409 data->evt_skb = NULL;
3415 static int btusb_mtk_setup_firmware_79xx(struct hci_dev *hdev, const char *fwname)
3417 struct btmtk_hci_wmt_params wmt_params;
3418 struct btmtk_global_desc *globaldesc = NULL;
3419 struct btmtk_section_map *sectionmap;
3420 const struct firmware *fw;
3422 const u8 *fw_bin_ptr;
3423 int err, dlen, i, status;
3424 u8 flag, first_block, retry;
3425 u32 section_num, dl_size, section_offset;
3428 err = request_firmware(&fw, fwname, &hdev->dev);
3430 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
3435 fw_bin_ptr = fw_ptr;
3436 globaldesc = (struct btmtk_global_desc *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE);
3437 section_num = le32_to_cpu(globaldesc->section_num);
3439 for (i = 0; i < section_num; i++) {
3441 fw_ptr = fw_bin_ptr;
3442 sectionmap = (struct btmtk_section_map *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
3443 MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i);
3445 section_offset = le32_to_cpu(sectionmap->secoffset);
3446 dl_size = le32_to_cpu(sectionmap->bin_info_spec.dlsize);
3451 cmd[0] = 0; /* 0 means legacy dl mode. */
3453 fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
3454 MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i +
3455 MTK_SEC_MAP_COMMON_SIZE,
3456 MTK_SEC_MAP_NEED_SEND_SIZE + 1);
3458 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3459 wmt_params.status = &status;
3460 wmt_params.flag = 0;
3461 wmt_params.dlen = MTK_SEC_MAP_NEED_SEND_SIZE + 1;
3462 wmt_params.data = &cmd;
3464 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3466 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3468 goto err_release_fw;
3471 if (status == BTMTK_WMT_PATCH_UNDONE) {
3473 } else if (status == BTMTK_WMT_PATCH_PROGRESS) {
3476 } else if (status == BTMTK_WMT_PATCH_DONE) {
3479 bt_dev_err(hdev, "Failed wmt patch dwnld status (%d)",
3481 goto err_release_fw;
3485 fw_ptr += section_offset;
3486 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3487 wmt_params.status = NULL;
3489 while (dl_size > 0) {
3490 dlen = min_t(int, 250, dl_size);
3491 if (first_block == 1) {
3494 } else if (dl_size - dlen <= 0) {
3500 wmt_params.flag = flag;
3501 wmt_params.dlen = dlen;
3502 wmt_params.data = fw_ptr;
3504 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3506 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3508 goto err_release_fw;
3518 /* Wait a few moments for firmware activation done */
3519 usleep_range(100000, 120000);
3522 release_firmware(fw);
3527 static int btusb_mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
3529 struct btmtk_hci_wmt_params wmt_params;
3530 const struct firmware *fw;
3536 err = request_firmware(&fw, fwname, &hdev->dev);
3538 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
3542 /* Power on data RAM the firmware relies on. */
3544 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3545 wmt_params.flag = 3;
3546 wmt_params.dlen = sizeof(param);
3547 wmt_params.data = ¶m;
3548 wmt_params.status = NULL;
3550 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3552 bt_dev_err(hdev, "Failed to power on data RAM (%d)", err);
3553 goto err_release_fw;
3559 /* The size of patch header is 30 bytes, should be skip */
3562 goto err_release_fw;
3569 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3570 wmt_params.status = NULL;
3572 while (fw_size > 0) {
3573 dlen = min_t(int, 250, fw_size);
3575 /* Tell device the position in sequence */
3576 if (fw_size - dlen <= 0)
3578 else if (fw_size < fw->size - 30)
3581 wmt_params.flag = flag;
3582 wmt_params.dlen = dlen;
3583 wmt_params.data = fw_ptr;
3585 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3587 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3589 goto err_release_fw;
3596 wmt_params.op = BTMTK_WMT_RST;
3597 wmt_params.flag = 4;
3598 wmt_params.dlen = 0;
3599 wmt_params.data = NULL;
3600 wmt_params.status = NULL;
3602 /* Activate funciton the firmware providing to */
3603 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3605 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
3606 goto err_release_fw;
3609 /* Wait a few moments for firmware activation done */
3610 usleep_range(10000, 12000);
3613 release_firmware(fw);
3618 static int btusb_mtk_func_query(struct hci_dev *hdev)
3620 struct btmtk_hci_wmt_params wmt_params;
3624 /* Query whether the function is enabled */
3625 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3626 wmt_params.flag = 4;
3627 wmt_params.dlen = sizeof(param);
3628 wmt_params.data = ¶m;
3629 wmt_params.status = &status;
3631 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3633 bt_dev_err(hdev, "Failed to query function status (%d)", err);
3640 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
3642 int pipe, err, size = sizeof(u32);
3645 buf = kzalloc(size, GFP_KERNEL);
3649 pipe = usb_rcvctrlpipe(data->udev, 0);
3650 err = usb_control_msg(data->udev, pipe, 0x63,
3651 USB_TYPE_VENDOR | USB_DIR_IN,
3652 reg >> 16, reg & 0xffff,
3653 buf, size, USB_CTRL_SET_TIMEOUT);
3657 *val = get_unaligned_le32(buf);
3665 static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
3667 return btusb_mtk_reg_read(data, reg, id);
3670 static int btusb_mtk_setup(struct hci_dev *hdev)
3672 struct btusb_data *data = hci_get_drvdata(hdev);
3673 struct btmtk_hci_wmt_params wmt_params;
3674 ktime_t calltime, delta, rettime;
3675 struct btmtk_tci_sleep tci_sleep;
3676 unsigned long long duration;
3677 struct sk_buff *skb;
3681 char fw_bin_name[64];
3685 calltime = ktime_get();
3687 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
3689 bt_dev_err(hdev, "Failed to get device id (%d)", err);
3694 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
3696 bt_dev_err(hdev, "Failed to get device id (%d)", err);
3699 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
3701 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
3708 fwname = FIRMWARE_MT7663;
3711 fwname = FIRMWARE_MT7668;
3714 snprintf(fw_bin_name, sizeof(fw_bin_name),
3715 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
3716 dev_id & 0xffff, (fw_version & 0xff) + 1);
3717 err = btusb_mtk_setup_firmware_79xx(hdev, fw_bin_name);
3719 /* Enable Bluetooth protocol */
3721 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3722 wmt_params.flag = 0;
3723 wmt_params.dlen = sizeof(param);
3724 wmt_params.data = ¶m;
3725 wmt_params.status = NULL;
3727 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3729 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3734 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
3739 /* Query whether the firmware is already download */
3740 wmt_params.op = BTMTK_WMT_SEMAPHORE;
3741 wmt_params.flag = 1;
3742 wmt_params.dlen = 0;
3743 wmt_params.data = NULL;
3744 wmt_params.status = &status;
3746 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3748 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
3752 if (status == BTMTK_WMT_PATCH_DONE) {
3753 bt_dev_info(hdev, "firmware already downloaded");
3754 goto ignore_setup_fw;
3757 /* Setup a firmware which the device definitely requires */
3758 err = btusb_mtk_setup_firmware(hdev, fwname);
3763 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
3764 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
3766 /* -ETIMEDOUT happens */
3770 /* The other errors happen in btusb_mtk_func_query */
3774 if (status == BTMTK_WMT_ON_DONE) {
3775 bt_dev_info(hdev, "function already on");
3776 goto ignore_func_on;
3779 /* Enable Bluetooth protocol */
3781 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3782 wmt_params.flag = 0;
3783 wmt_params.dlen = sizeof(param);
3784 wmt_params.data = ¶m;
3785 wmt_params.status = NULL;
3787 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3789 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3794 /* Apply the low power environment setup */
3795 tci_sleep.mode = 0x5;
3796 tci_sleep.duration = cpu_to_le16(0x640);
3797 tci_sleep.host_duration = cpu_to_le16(0x640);
3798 tci_sleep.host_wakeup_pin = 0;
3799 tci_sleep.time_compensation = 0;
3801 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
3805 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
3811 rettime = ktime_get();
3812 delta = ktime_sub(rettime, calltime);
3813 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3815 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
3820 static int btusb_mtk_shutdown(struct hci_dev *hdev)
3822 struct btmtk_hci_wmt_params wmt_params;
3826 /* Disable the device */
3827 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3828 wmt_params.flag = 0;
3829 wmt_params.dlen = sizeof(param);
3830 wmt_params.data = ¶m;
3831 wmt_params.status = NULL;
3833 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3835 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3842 MODULE_FIRMWARE(FIRMWARE_MT7663);
3843 MODULE_FIRMWARE(FIRMWARE_MT7668);
3846 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3847 static int marvell_config_oob_wake(struct hci_dev *hdev)
3849 struct sk_buff *skb;
3850 struct btusb_data *data = hci_get_drvdata(hdev);
3851 struct device *dev = &data->udev->dev;
3852 u16 pin, gap, opcode;
3856 /* Move on if no wakeup pin specified */
3857 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3858 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3861 /* Vendor specific command to configure a GPIO as wake-up pin */
3862 opcode = hci_opcode_pack(0x3F, 0x59);
3863 cmd[0] = opcode & 0xFF;
3864 cmd[1] = opcode >> 8;
3865 cmd[2] = 2; /* length of parameters that follow */
3867 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3869 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3871 bt_dev_err(hdev, "%s: No memory", __func__);
3875 skb_put_data(skb, cmd, sizeof(cmd));
3876 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3878 ret = btusb_send_frame(hdev, skb);
3880 bt_dev_err(hdev, "%s: configuration failed", __func__);
3889 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3890 const bdaddr_t *bdaddr)
3892 struct sk_buff *skb;
3897 buf[1] = sizeof(bdaddr_t);
3898 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3900 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3903 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3912 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3913 const bdaddr_t *bdaddr)
3915 struct sk_buff *skb;
3922 buf[3] = sizeof(bdaddr_t);
3923 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3925 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3928 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3936 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3937 const bdaddr_t *bdaddr)
3939 struct sk_buff *skb;
3943 memcpy(buf, bdaddr, sizeof(bdaddr_t));
3945 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3946 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3949 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3957 #define QCA_DFU_PACKET_LEN 4096
3959 #define QCA_GET_TARGET_VERSION 0x09
3960 #define QCA_CHECK_STATUS 0x05
3961 #define QCA_DFU_DOWNLOAD 0x01
3963 #define QCA_SYSCFG_UPDATED 0x40
3964 #define QCA_PATCH_UPDATED 0x80
3965 #define QCA_DFU_TIMEOUT 3000
3966 #define QCA_FLAG_MULTI_NVM 0x80
3968 struct qca_version {
3970 __le32 patch_version;
3977 struct qca_rampatch_version {
3978 __le16 rom_version_high;
3979 __le16 rom_version_low;
3980 __le16 patch_version;
3983 struct qca_device_info {
3985 u8 rampatch_hdr; /* length of header in rampatch */
3986 u8 nvm_hdr; /* length of header in NVM */
3987 u8 ver_offset; /* offset of version structure in rampatch */
3990 static const struct qca_device_info qca_devices_table[] = {
3991 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3992 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3993 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3994 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3995 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3996 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3997 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3998 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
4001 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
4002 void *data, u16 size)
4007 buf = kmalloc(size, GFP_KERNEL);
4011 /* Found some of USB hosts have IOT issues with ours so that we should
4012 * not wait until HCI layer is ready.
4014 pipe = usb_rcvctrlpipe(udev, 0);
4015 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
4016 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
4018 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
4022 memcpy(data, buf, size);
4030 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
4031 const struct firmware *firmware,
4034 struct btusb_data *btdata = hci_get_drvdata(hdev);
4035 struct usb_device *udev = btdata->udev;
4036 size_t count, size, sent = 0;
4040 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
4044 count = firmware->size;
4046 size = min_t(size_t, count, hdr_size);
4047 memcpy(buf, firmware->data, size);
4049 /* USB patches should go down to controller through USB path
4050 * because binary format fits to go down through USB channel.
4051 * USB control path is for patching headers and USB bulk is for
4054 pipe = usb_sndctrlpipe(udev, 0);
4055 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
4056 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
4058 bt_dev_err(hdev, "Failed to send headers (%d)", err);
4066 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
4068 memcpy(buf, firmware->data + sent, size);
4070 pipe = usb_sndbulkpipe(udev, 0x02);
4071 err = usb_bulk_msg(udev, pipe, buf, size, &len,
4074 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
4075 sent, firmware->size, err);
4080 bt_dev_err(hdev, "Failed to get bulk buffer");
4094 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
4095 struct qca_version *ver,
4096 const struct qca_device_info *info)
4098 struct qca_rampatch_version *rver;
4099 const struct firmware *fw;
4100 u32 ver_rom, ver_patch, rver_rom;
4101 u16 rver_rom_low, rver_rom_high, rver_patch;
4105 ver_rom = le32_to_cpu(ver->rom_version);
4106 ver_patch = le32_to_cpu(ver->patch_version);
4108 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
4110 err = request_firmware(&fw, fwname, &hdev->dev);
4112 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
4117 bt_dev_info(hdev, "using rampatch file: %s", fwname);
4119 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
4120 rver_rom_low = le16_to_cpu(rver->rom_version_low);
4121 rver_patch = le16_to_cpu(rver->patch_version);
4123 if (ver_rom & ~0xffffU) {
4124 rver_rom_high = le16_to_cpu(rver->rom_version_high);
4125 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
4127 rver_rom = rver_rom_low;
4130 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
4131 "firmware rome 0x%x build 0x%x",
4132 rver_rom, rver_patch, ver_rom, ver_patch);
4134 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
4135 bt_dev_err(hdev, "rampatch file version did not match with firmware");
4140 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
4143 release_firmware(fw);
4148 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
4149 struct qca_version *ver,
4150 const struct qca_device_info *info)
4152 const struct firmware *fw;
4156 if (((ver->flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
4157 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x_%04x.bin",
4158 le32_to_cpu(ver->rom_version),
4159 le16_to_cpu(ver->board_id));
4161 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
4162 le32_to_cpu(ver->rom_version));
4165 err = request_firmware(&fw, fwname, &hdev->dev);
4167 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
4172 bt_dev_info(hdev, "using NVM file: %s", fwname);
4174 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
4176 release_firmware(fw);
4181 /* identify the ROM version and check whether patches are needed */
4182 static bool btusb_qca_need_patch(struct usb_device *udev)
4184 struct qca_version ver;
4186 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
4189 /* only low ROM versions need patches */
4190 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
4193 static int btusb_setup_qca(struct hci_dev *hdev)
4195 struct btusb_data *btdata = hci_get_drvdata(hdev);
4196 struct usb_device *udev = btdata->udev;
4197 const struct qca_device_info *info = NULL;
4198 struct qca_version ver;
4203 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
4208 ver_rom = le32_to_cpu(ver.rom_version);
4210 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
4211 if (ver_rom == qca_devices_table[i].rom_version)
4212 info = &qca_devices_table[i];
4215 /* If the rom_version is not matched in the qca_devices_table
4216 * and the high ROM version is not zero, we assume this chip no
4217 * need to load the rampatch and nvm.
4219 if (ver_rom & ~0xffffU)
4222 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
4226 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
4231 if (!(status & QCA_PATCH_UPDATED)) {
4232 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
4237 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
4242 if (!(status & QCA_SYSCFG_UPDATED)) {
4243 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
4251 static inline int __set_diag_interface(struct hci_dev *hdev)
4253 struct btusb_data *data = hci_get_drvdata(hdev);
4254 struct usb_interface *intf = data->diag;
4260 data->diag_tx_ep = NULL;
4261 data->diag_rx_ep = NULL;
4263 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4264 struct usb_endpoint_descriptor *ep_desc;
4266 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4268 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4269 data->diag_tx_ep = ep_desc;
4273 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4274 data->diag_rx_ep = ep_desc;
4279 if (!data->diag_tx_ep || !data->diag_rx_ep) {
4280 bt_dev_err(hdev, "invalid diagnostic descriptors");
4287 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
4289 struct btusb_data *data = hci_get_drvdata(hdev);
4290 struct sk_buff *skb;
4294 if (!data->diag_tx_ep)
4295 return ERR_PTR(-ENODEV);
4297 urb = usb_alloc_urb(0, GFP_KERNEL);
4299 return ERR_PTR(-ENOMEM);
4301 skb = bt_skb_alloc(2, GFP_KERNEL);
4304 return ERR_PTR(-ENOMEM);
4307 skb_put_u8(skb, 0xf0);
4308 skb_put_u8(skb, enable);
4310 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
4312 usb_fill_bulk_urb(urb, data->udev, pipe,
4313 skb->data, skb->len, btusb_tx_complete, skb);
4315 skb->dev = (void *)hdev;
4320 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
4322 struct btusb_data *data = hci_get_drvdata(hdev);
4328 if (!test_bit(HCI_RUNNING, &hdev->flags))
4331 urb = alloc_diag_urb(hdev, enable);
4333 return PTR_ERR(urb);
4335 return submit_or_queue_tx_urb(hdev, urb);
4339 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
4341 struct btusb_data *data = priv;
4343 pm_wakeup_event(&data->udev->dev, 0);
4346 /* Disable only if not already disabled (keep it balanced) */
4347 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4348 disable_irq_nosync(irq);
4349 disable_irq_wake(irq);
4354 static const struct of_device_id btusb_match_table[] = {
4355 { .compatible = "usb1286,204e" },
4356 { .compatible = "usbcf3,e300" }, /* QCA6174A */
4357 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
4360 MODULE_DEVICE_TABLE(of, btusb_match_table);
4362 /* Use an oob wakeup pin? */
4363 static int btusb_config_oob_wake(struct hci_dev *hdev)
4365 struct btusb_data *data = hci_get_drvdata(hdev);
4366 struct device *dev = &data->udev->dev;
4369 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4371 if (!of_match_device(btusb_match_table, dev))
4374 /* Move on if no IRQ specified */
4375 irq = of_irq_get_byname(dev->of_node, "wakeup");
4377 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
4381 irq_set_status_flags(irq, IRQ_NOAUTOEN);
4382 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
4383 0, "OOB Wake-on-BT", data);
4385 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
4389 ret = device_init_wakeup(dev, true);
4391 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
4395 data->oob_wake_irq = irq;
4396 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
4401 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
4403 if (dmi_check_system(btusb_needs_reset_resume_table))
4404 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
4407 static bool btusb_prevent_wake(struct hci_dev *hdev)
4409 struct btusb_data *data = hci_get_drvdata(hdev);
4411 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
4414 return !device_may_wakeup(&data->udev->dev);
4417 static int btusb_shutdown_qca(struct hci_dev *hdev)
4419 struct sk_buff *skb;
4421 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
4423 bt_dev_err(hdev, "HCI reset during shutdown failed");
4424 return PTR_ERR(skb);
4431 static int btusb_probe(struct usb_interface *intf,
4432 const struct usb_device_id *id)
4434 struct usb_endpoint_descriptor *ep_desc;
4435 struct gpio_desc *reset_gpio;
4436 struct btusb_data *data;
4437 struct hci_dev *hdev;
4438 unsigned ifnum_base;
4441 BT_DBG("intf %p id %p", intf, id);
4443 /* interface numbers are hardcoded in the spec */
4444 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
4445 if (!(id->driver_info & BTUSB_IFNUM_2))
4447 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
4451 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
4453 if (!id->driver_info) {
4454 const struct usb_device_id *match;
4456 match = usb_match_id(intf, blacklist_table);
4461 if (id->driver_info == BTUSB_IGNORE)
4464 if (id->driver_info & BTUSB_ATH3012) {
4465 struct usb_device *udev = interface_to_usbdev(intf);
4467 /* Old firmware would otherwise let ath3k driver load
4468 * patch and sysconfig files
4470 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
4471 !btusb_qca_need_patch(udev))
4475 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
4479 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4480 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4482 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
4483 data->intr_ep = ep_desc;
4487 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4488 data->bulk_tx_ep = ep_desc;
4492 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4493 data->bulk_rx_ep = ep_desc;
4498 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
4501 if (id->driver_info & BTUSB_AMP) {
4502 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
4503 data->cmdreq = 0x2b;
4505 data->cmdreq_type = USB_TYPE_CLASS;
4506 data->cmdreq = 0x00;
4509 data->udev = interface_to_usbdev(intf);
4512 INIT_WORK(&data->work, btusb_work);
4513 INIT_WORK(&data->waker, btusb_waker);
4514 init_usb_anchor(&data->deferred);
4515 init_usb_anchor(&data->tx_anchor);
4516 spin_lock_init(&data->txlock);
4518 init_usb_anchor(&data->intr_anchor);
4519 init_usb_anchor(&data->bulk_anchor);
4520 init_usb_anchor(&data->isoc_anchor);
4521 init_usb_anchor(&data->diag_anchor);
4522 init_usb_anchor(&data->ctrl_anchor);
4523 spin_lock_init(&data->rxlock);
4525 if (id->driver_info & BTUSB_INTEL_NEW) {
4526 data->recv_event = btusb_recv_event_intel;
4527 data->recv_bulk = btusb_recv_bulk_intel;
4528 set_bit(BTUSB_BOOTLOADER, &data->flags);
4530 data->recv_event = hci_recv_frame;
4531 data->recv_bulk = btusb_recv_bulk;
4534 hdev = hci_alloc_dev();
4538 hdev->bus = HCI_USB;
4539 hci_set_drvdata(hdev, data);
4541 if (id->driver_info & BTUSB_AMP)
4542 hdev->dev_type = HCI_AMP;
4544 hdev->dev_type = HCI_PRIMARY;
4548 SET_HCIDEV_DEV(hdev, &intf->dev);
4550 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
4552 if (IS_ERR(reset_gpio)) {
4553 err = PTR_ERR(reset_gpio);
4555 } else if (reset_gpio) {
4556 data->reset_gpio = reset_gpio;
4559 hdev->open = btusb_open;
4560 hdev->close = btusb_close;
4561 hdev->flush = btusb_flush;
4562 hdev->send = btusb_send_frame;
4563 hdev->notify = btusb_notify;
4564 hdev->prevent_wake = btusb_prevent_wake;
4567 err = btusb_config_oob_wake(hdev);
4571 /* Marvell devices may need a specific chip configuration */
4572 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
4573 err = marvell_config_oob_wake(hdev);
4578 if (id->driver_info & BTUSB_CW6622)
4579 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4581 if (id->driver_info & BTUSB_BCM2045)
4582 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4584 if (id->driver_info & BTUSB_BCM92035)
4585 hdev->setup = btusb_setup_bcm92035;
4587 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4588 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
4589 hdev->manufacturer = 15;
4590 hdev->setup = btbcm_setup_patchram;
4591 hdev->set_diag = btusb_bcm_set_diag;
4592 hdev->set_bdaddr = btbcm_set_bdaddr;
4594 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4595 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4598 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4599 (id->driver_info & BTUSB_BCM_APPLE)) {
4600 hdev->manufacturer = 15;
4601 hdev->setup = btbcm_setup_apple;
4602 hdev->set_diag = btusb_bcm_set_diag;
4604 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4605 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4608 if (id->driver_info & BTUSB_INTEL) {
4609 hdev->manufacturer = 2;
4610 hdev->setup = btusb_setup_intel;
4611 hdev->shutdown = btusb_shutdown_intel;
4612 hdev->set_diag = btintel_set_diag_mfg;
4613 hdev->set_bdaddr = btintel_set_bdaddr;
4614 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4615 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4616 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4617 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4620 if (id->driver_info & BTUSB_INTEL_NEW) {
4621 hdev->manufacturer = 2;
4622 hdev->send = btusb_send_frame_intel;
4623 hdev->setup = btusb_setup_intel_new;
4624 hdev->shutdown = btusb_shutdown_intel_new;
4625 hdev->hw_error = btintel_hw_error;
4626 hdev->set_diag = btintel_set_diag;
4627 hdev->set_bdaddr = btintel_set_bdaddr;
4628 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4629 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4630 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4631 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4634 if (id->driver_info & BTUSB_INTEL_NEWGEN) {
4635 hdev->manufacturer = 2;
4636 hdev->send = btusb_send_frame_intel;
4637 hdev->setup = btusb_setup_intel_newgen;
4638 hdev->shutdown = btusb_shutdown_intel_new;
4639 hdev->hw_error = btintel_hw_error;
4640 hdev->set_diag = btintel_set_diag;
4641 hdev->set_bdaddr = btintel_set_bdaddr;
4642 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4643 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4644 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4645 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4647 data->recv_event = btusb_recv_event_intel;
4648 data->recv_bulk = btusb_recv_bulk_intel;
4649 set_bit(BTUSB_BOOTLOADER, &data->flags);
4652 if (id->driver_info & BTUSB_MARVELL)
4653 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4655 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4656 (id->driver_info & BTUSB_MEDIATEK)) {
4657 hdev->setup = btusb_mtk_setup;
4658 hdev->shutdown = btusb_mtk_shutdown;
4659 hdev->manufacturer = 70;
4660 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
4663 if (id->driver_info & BTUSB_SWAVE) {
4664 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
4665 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
4668 if (id->driver_info & BTUSB_INTEL_BOOT) {
4669 hdev->manufacturer = 2;
4670 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4673 if (id->driver_info & BTUSB_ATH3012) {
4674 data->setup_on_usb = btusb_setup_qca;
4675 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4676 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4677 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4680 if (id->driver_info & BTUSB_QCA_ROME) {
4681 data->setup_on_usb = btusb_setup_qca;
4682 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4683 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4684 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4685 btusb_check_needs_reset_resume(intf);
4688 if (id->driver_info & BTUSB_QCA_WCN6855) {
4689 data->setup_on_usb = btusb_setup_qca;
4690 hdev->shutdown = btusb_shutdown_qca;
4691 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
4692 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4693 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4696 if (id->driver_info & BTUSB_AMP) {
4697 /* AMP controllers do not support SCO packets */
4700 /* Interface orders are hardcoded in the specification */
4701 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
4702 data->isoc_ifnum = ifnum_base + 1;
4705 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4706 (id->driver_info & BTUSB_REALTEK)) {
4707 hdev->setup = btrtl_setup_realtek;
4708 hdev->shutdown = btrtl_shutdown_realtek;
4709 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
4711 /* Realtek devices lose their updated firmware over global
4712 * suspend that means host doesn't send SET_FEATURE
4713 * (DEVICE_REMOTE_WAKEUP)
4715 set_bit(BTUSB_WAKEUP_DISABLE, &data->flags);
4719 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4721 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
4722 if (!disable_scofix)
4723 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
4726 if (id->driver_info & BTUSB_BROKEN_ISOC)
4729 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
4730 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
4732 if (id->driver_info & BTUSB_VALID_LE_STATES)
4733 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
4735 if (id->driver_info & BTUSB_DIGIANSWER) {
4736 data->cmdreq_type = USB_TYPE_VENDOR;
4737 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4740 if (id->driver_info & BTUSB_CSR) {
4741 struct usb_device *udev = data->udev;
4742 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
4744 /* Old firmware would otherwise execute USB reset */
4745 if (bcdDevice < 0x117)
4746 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4748 /* This must be set first in case we disable it for fakes */
4749 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4751 /* Fake CSR devices with broken commands */
4752 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
4753 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4754 hdev->setup = btusb_setup_csr;
4757 if (id->driver_info & BTUSB_SNIFFER) {
4758 struct usb_device *udev = data->udev;
4760 /* New sniffer firmware has crippled HCI interface */
4761 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4762 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4765 if (id->driver_info & BTUSB_INTEL_BOOT) {
4766 /* A bug in the bootloader causes that interrupt interface is
4767 * only enabled after receiving SetInterface(0, AltSetting=0).
4769 err = usb_set_interface(data->udev, 0, 0);
4771 BT_ERR("failed to set interface 0, alt 0 %d", err);
4777 err = usb_driver_claim_interface(&btusb_driver,
4783 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
4784 if (!usb_driver_claim_interface(&btusb_driver,
4786 __set_diag_interface(hdev);
4791 if (enable_autosuspend)
4792 usb_enable_autosuspend(data->udev);
4794 err = hci_register_dev(hdev);
4798 usb_set_intfdata(intf, data);
4803 if (data->reset_gpio)
4804 gpiod_put(data->reset_gpio);
4809 static void btusb_disconnect(struct usb_interface *intf)
4811 struct btusb_data *data = usb_get_intfdata(intf);
4812 struct hci_dev *hdev;
4814 BT_DBG("intf %p", intf);
4820 usb_set_intfdata(data->intf, NULL);
4823 usb_set_intfdata(data->isoc, NULL);
4826 usb_set_intfdata(data->diag, NULL);
4828 hci_unregister_dev(hdev);
4830 if (intf == data->intf) {
4832 usb_driver_release_interface(&btusb_driver, data->isoc);
4834 usb_driver_release_interface(&btusb_driver, data->diag);
4835 } else if (intf == data->isoc) {
4837 usb_driver_release_interface(&btusb_driver, data->diag);
4838 usb_driver_release_interface(&btusb_driver, data->intf);
4839 } else if (intf == data->diag) {
4840 usb_driver_release_interface(&btusb_driver, data->intf);
4842 usb_driver_release_interface(&btusb_driver, data->isoc);
4845 if (data->oob_wake_irq)
4846 device_init_wakeup(&data->udev->dev, false);
4848 if (data->reset_gpio)
4849 gpiod_put(data->reset_gpio);
4855 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4857 struct btusb_data *data = usb_get_intfdata(intf);
4859 BT_DBG("intf %p", intf);
4861 if (data->suspend_count++)
4864 spin_lock_irq(&data->txlock);
4865 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4866 set_bit(BTUSB_SUSPENDING, &data->flags);
4867 spin_unlock_irq(&data->txlock);
4869 spin_unlock_irq(&data->txlock);
4870 data->suspend_count--;
4874 cancel_work_sync(&data->work);
4876 btusb_stop_traffic(data);
4877 usb_kill_anchored_urbs(&data->tx_anchor);
4879 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4880 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4881 enable_irq_wake(data->oob_wake_irq);
4882 enable_irq(data->oob_wake_irq);
4885 /* For global suspend, Realtek devices lose the loaded fw
4886 * in them. But for autosuspend, firmware should remain.
4887 * Actually, it depends on whether the usb host sends
4888 * set feature (enable wakeup) or not.
4890 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags)) {
4891 if (PMSG_IS_AUTO(message) &&
4892 device_can_wakeup(&data->udev->dev))
4893 data->udev->do_remote_wakeup = 1;
4894 else if (!PMSG_IS_AUTO(message))
4895 data->udev->reset_resume = 1;
4901 static void play_deferred(struct btusb_data *data)
4906 while ((urb = usb_get_from_anchor(&data->deferred))) {
4907 usb_anchor_urb(urb, &data->tx_anchor);
4909 err = usb_submit_urb(urb, GFP_ATOMIC);
4911 if (err != -EPERM && err != -ENODEV)
4912 BT_ERR("%s urb %p submission failed (%d)",
4913 data->hdev->name, urb, -err);
4914 kfree(urb->setup_packet);
4915 usb_unanchor_urb(urb);
4920 data->tx_in_flight++;
4924 /* Cleanup the rest deferred urbs. */
4925 while ((urb = usb_get_from_anchor(&data->deferred))) {
4926 kfree(urb->setup_packet);
4931 static int btusb_resume(struct usb_interface *intf)
4933 struct btusb_data *data = usb_get_intfdata(intf);
4934 struct hci_dev *hdev = data->hdev;
4937 BT_DBG("intf %p", intf);
4939 if (--data->suspend_count)
4942 /* Disable only if not already disabled (keep it balanced) */
4943 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4944 disable_irq(data->oob_wake_irq);
4945 disable_irq_wake(data->oob_wake_irq);
4948 if (!test_bit(HCI_RUNNING, &hdev->flags))
4951 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4952 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4954 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4959 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4960 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4962 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4966 btusb_submit_bulk_urb(hdev, GFP_NOIO);
4969 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4970 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4971 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4973 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4976 spin_lock_irq(&data->txlock);
4977 play_deferred(data);
4978 clear_bit(BTUSB_SUSPENDING, &data->flags);
4979 spin_unlock_irq(&data->txlock);
4980 schedule_work(&data->work);
4985 usb_scuttle_anchored_urbs(&data->deferred);
4987 spin_lock_irq(&data->txlock);
4988 clear_bit(BTUSB_SUSPENDING, &data->flags);
4989 spin_unlock_irq(&data->txlock);
4995 static struct usb_driver btusb_driver = {
4997 .probe = btusb_probe,
4998 .disconnect = btusb_disconnect,
5000 .suspend = btusb_suspend,
5001 .resume = btusb_resume,
5003 .id_table = btusb_table,
5004 .supports_autosuspend = 1,
5005 .disable_hub_initiated_lpm = 1,
5008 module_usb_driver(btusb_driver);
5010 module_param(disable_scofix, bool, 0644);
5011 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
5013 module_param(force_scofix, bool, 0644);
5014 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
5016 module_param(enable_autosuspend, bool, 0644);
5017 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
5019 module_param(reset, bool, 0644);
5020 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
5022 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
5023 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
5024 MODULE_VERSION(VERSION);
5025 MODULE_LICENSE("GPL");