1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Generic Bluetooth USB driver
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(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
274 BTUSB_WIDEBAND_SPEECH },
275 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
276 BTUSB_WIDEBAND_SPEECH },
277 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
278 BTUSB_WIDEBAND_SPEECH },
279 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
280 BTUSB_WIDEBAND_SPEECH },
281 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
282 BTUSB_WIDEBAND_SPEECH },
283 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
284 BTUSB_WIDEBAND_SPEECH },
285 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
286 BTUSB_WIDEBAND_SPEECH },
287 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
288 BTUSB_WIDEBAND_SPEECH },
289 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
290 BTUSB_WIDEBAND_SPEECH },
291 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
292 BTUSB_WIDEBAND_SPEECH },
293 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
294 BTUSB_WIDEBAND_SPEECH },
295 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
296 BTUSB_WIDEBAND_SPEECH },
298 /* QCA WCN6855 chipset */
299 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
300 BTUSB_WIDEBAND_SPEECH },
302 /* Broadcom BCM2035 */
303 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
304 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
305 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
307 /* Broadcom BCM2045 */
308 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
309 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
311 /* IBM/Lenovo ThinkPad with Broadcom chip */
312 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
313 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
315 /* HP laptop with Broadcom chip */
316 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
318 /* Dell laptop with Broadcom chip */
319 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
321 /* Dell Wireless 370 and 410 devices */
322 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
323 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
325 /* Belkin F8T012 and F8T013 devices */
326 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
327 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
329 /* Asus WL-BTD202 device */
330 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
332 /* Kensington Bluetooth USB adapter */
333 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
335 /* RTX Telecom based adapters with buggy SCO support */
336 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
337 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
339 /* CONWISE Technology based adapters with buggy SCO support */
340 { USB_DEVICE(0x0e5e, 0x6622),
341 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
343 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
344 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
346 /* Digianswer devices */
347 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
348 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
350 /* CSR BlueCore Bluetooth Sniffer */
351 { USB_DEVICE(0x0a12, 0x0002),
352 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
354 /* Frontline ComProbe Bluetooth Sniffer */
355 { USB_DEVICE(0x16d3, 0x0002),
356 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
358 /* Marvell Bluetooth devices */
359 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
360 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
361 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
363 /* Intel Bluetooth devices */
364 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW |
365 BTUSB_WIDEBAND_SPEECH |
366 BTUSB_VALID_LE_STATES },
367 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_NEW |
368 BTUSB_WIDEBAND_SPEECH },
369 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_NEW |
370 BTUSB_WIDEBAND_SPEECH },
371 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_NEWGEN |
372 BTUSB_WIDEBAND_SPEECH},
373 { USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_NEWGEN |
374 BTUSB_WIDEBAND_SPEECH},
375 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
376 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
377 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
378 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW |
379 BTUSB_WIDEBAND_SPEECH },
380 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL |
381 BTUSB_WIDEBAND_SPEECH },
382 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW |
383 BTUSB_WIDEBAND_SPEECH |
384 BTUSB_VALID_LE_STATES },
386 /* Other Intel Bluetooth devices */
387 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
388 .driver_info = BTUSB_IGNORE },
390 /* Realtek 8822CE Bluetooth devices */
391 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
392 BTUSB_WIDEBAND_SPEECH },
393 { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
394 BTUSB_WIDEBAND_SPEECH },
396 /* Realtek 8852AE Bluetooth devices */
397 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
398 BTUSB_WIDEBAND_SPEECH },
400 /* Realtek Bluetooth devices */
401 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
402 .driver_info = BTUSB_REALTEK },
404 /* MediaTek Bluetooth devices */
405 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
406 .driver_info = BTUSB_MEDIATEK |
407 BTUSB_WIDEBAND_SPEECH |
408 BTUSB_VALID_LE_STATES },
410 /* Additional MediaTek MT7615E Bluetooth devices */
411 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
413 /* Additional MediaTek MT7921 Bluetooth devices */
414 { USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
415 BTUSB_WIDEBAND_SPEECH |
416 BTUSB_VALID_LE_STATES },
418 /* Additional Realtek 8723AE Bluetooth devices */
419 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
420 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
422 /* Additional Realtek 8723BE Bluetooth devices */
423 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
424 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
425 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
426 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
427 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
428 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
430 /* Additional Realtek 8723BU Bluetooth devices */
431 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
433 /* Additional Realtek 8723DE Bluetooth devices */
434 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
435 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
437 /* Additional Realtek 8761BU Bluetooth devices */
438 { USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
439 BTUSB_WIDEBAND_SPEECH },
441 /* Additional Realtek 8821AE Bluetooth devices */
442 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
443 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
444 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
445 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
446 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
448 /* Additional Realtek 8822BE Bluetooth devices */
449 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
450 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
452 /* Additional Realtek 8822CE Bluetooth devices */
453 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
454 BTUSB_WIDEBAND_SPEECH },
455 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
456 BTUSB_WIDEBAND_SPEECH },
457 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
458 BTUSB_WIDEBAND_SPEECH },
459 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
460 BTUSB_WIDEBAND_SPEECH },
461 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
462 BTUSB_WIDEBAND_SPEECH },
463 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
464 BTUSB_WIDEBAND_SPEECH },
465 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
466 BTUSB_WIDEBAND_SPEECH },
467 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
468 BTUSB_WIDEBAND_SPEECH },
469 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
470 BTUSB_WIDEBAND_SPEECH },
471 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
472 BTUSB_WIDEBAND_SPEECH },
473 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
474 BTUSB_WIDEBAND_SPEECH },
476 /* Silicon Wave based devices */
477 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
479 { } /* Terminating entry */
482 /* The Bluetooth USB module build into some devices needs to be reset on resume,
483 * this is a problem with the platform (likely shutting off all power) not with
484 * the module itself. So we use a DMI list to match known broken platforms.
486 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
488 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
490 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
491 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
495 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
497 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
498 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
502 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
504 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
505 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
511 #define BTUSB_MAX_ISOC_FRAMES 10
513 #define BTUSB_INTR_RUNNING 0
514 #define BTUSB_BULK_RUNNING 1
515 #define BTUSB_ISOC_RUNNING 2
516 #define BTUSB_SUSPENDING 3
517 #define BTUSB_DID_ISO_RESUME 4
518 #define BTUSB_BOOTLOADER 5
519 #define BTUSB_DOWNLOADING 6
520 #define BTUSB_FIRMWARE_LOADED 7
521 #define BTUSB_FIRMWARE_FAILED 8
522 #define BTUSB_BOOTING 9
523 #define BTUSB_DIAG_RUNNING 10
524 #define BTUSB_OOB_WAKE_ENABLED 11
525 #define BTUSB_HW_RESET_ACTIVE 12
526 #define BTUSB_TX_WAIT_VND_EVT 13
527 #define BTUSB_WAKEUP_DISABLE 14
530 struct hci_dev *hdev;
531 struct usb_device *udev;
532 struct usb_interface *intf;
533 struct usb_interface *isoc;
534 struct usb_interface *diag;
539 struct work_struct work;
540 struct work_struct waker;
542 struct usb_anchor deferred;
543 struct usb_anchor tx_anchor;
547 struct usb_anchor intr_anchor;
548 struct usb_anchor bulk_anchor;
549 struct usb_anchor isoc_anchor;
550 struct usb_anchor diag_anchor;
551 struct usb_anchor ctrl_anchor;
554 struct sk_buff *evt_skb;
555 struct sk_buff *acl_skb;
556 struct sk_buff *sco_skb;
558 struct usb_endpoint_descriptor *intr_ep;
559 struct usb_endpoint_descriptor *bulk_tx_ep;
560 struct usb_endpoint_descriptor *bulk_rx_ep;
561 struct usb_endpoint_descriptor *isoc_tx_ep;
562 struct usb_endpoint_descriptor *isoc_rx_ep;
563 struct usb_endpoint_descriptor *diag_tx_ep;
564 struct usb_endpoint_descriptor *diag_rx_ep;
566 struct gpio_desc *reset_gpio;
571 unsigned int sco_num;
572 unsigned int air_mode;
573 bool usb_alt6_packet_flow;
577 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
578 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
580 int (*setup_on_usb)(struct hci_dev *hdev);
582 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
583 unsigned cmd_timeout_cnt;
586 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
588 struct btusb_data *data = hci_get_drvdata(hdev);
589 struct gpio_desc *reset_gpio = data->reset_gpio;
591 if (++data->cmd_timeout_cnt < 5)
595 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
600 * Toggle the hard reset line if the platform provides one. The reset
601 * is going to yank the device off the USB and then replug. So doing
602 * once is enough. The cleanup is handled correctly on the way out
603 * (standard USB disconnect), and the new device is detected cleanly
604 * and bound to the driver again like it should be.
606 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
607 bt_dev_err(hdev, "last reset failed? Not resetting again");
611 bt_dev_err(hdev, "Initiating HW reset via gpio");
612 gpiod_set_value_cansleep(reset_gpio, 1);
614 gpiod_set_value_cansleep(reset_gpio, 0);
617 static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
619 struct btusb_data *data = hci_get_drvdata(hdev);
620 struct gpio_desc *reset_gpio = data->reset_gpio;
622 if (++data->cmd_timeout_cnt < 5)
626 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
630 /* Toggle the hard reset line. The Realtek device is going to
631 * yank itself off the USB and then replug. The cleanup is handled
632 * correctly on the way out (standard USB disconnect), and the new
633 * device is detected cleanly and bound to the driver again like
636 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
637 bt_dev_err(hdev, "last reset failed? Not resetting again");
641 bt_dev_err(hdev, "Reset Realtek device via gpio");
642 gpiod_set_value_cansleep(reset_gpio, 1);
644 gpiod_set_value_cansleep(reset_gpio, 0);
647 static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
649 struct btusb_data *data = hci_get_drvdata(hdev);
652 if (++data->cmd_timeout_cnt < 5)
655 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
656 /* This is not an unbalanced PM reference since the device will reset */
657 err = usb_autopm_get_interface(data->intf);
659 usb_queue_reset_device(data->intf);
661 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
664 static inline void btusb_free_frags(struct btusb_data *data)
668 spin_lock_irqsave(&data->rxlock, flags);
670 kfree_skb(data->evt_skb);
671 data->evt_skb = NULL;
673 kfree_skb(data->acl_skb);
674 data->acl_skb = NULL;
676 kfree_skb(data->sco_skb);
677 data->sco_skb = NULL;
679 spin_unlock_irqrestore(&data->rxlock, flags);
682 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
688 spin_lock_irqsave(&data->rxlock, flags);
695 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
701 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
702 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
705 len = min_t(uint, hci_skb_expect(skb), count);
706 skb_put_data(skb, buffer, len);
710 hci_skb_expect(skb) -= len;
712 if (skb->len == HCI_EVENT_HDR_SIZE) {
713 /* Complete event header */
714 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
716 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
725 if (!hci_skb_expect(skb)) {
727 data->recv_event(data->hdev, skb);
733 spin_unlock_irqrestore(&data->rxlock, flags);
738 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
744 spin_lock_irqsave(&data->rxlock, flags);
751 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
757 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
758 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
761 len = min_t(uint, hci_skb_expect(skb), count);
762 skb_put_data(skb, buffer, len);
766 hci_skb_expect(skb) -= len;
768 if (skb->len == HCI_ACL_HDR_SIZE) {
769 __le16 dlen = hci_acl_hdr(skb)->dlen;
771 /* Complete ACL header */
772 hci_skb_expect(skb) = __le16_to_cpu(dlen);
774 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
783 if (!hci_skb_expect(skb)) {
785 hci_recv_frame(data->hdev, skb);
791 spin_unlock_irqrestore(&data->rxlock, flags);
796 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
802 spin_lock_irqsave(&data->rxlock, flags);
809 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
815 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
816 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
819 len = min_t(uint, hci_skb_expect(skb), count);
820 skb_put_data(skb, buffer, len);
824 hci_skb_expect(skb) -= len;
826 if (skb->len == HCI_SCO_HDR_SIZE) {
827 /* Complete SCO header */
828 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
830 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
839 if (!hci_skb_expect(skb)) {
841 hci_recv_frame(data->hdev, skb);
847 spin_unlock_irqrestore(&data->rxlock, flags);
852 static void btusb_intr_complete(struct urb *urb)
854 struct hci_dev *hdev = urb->context;
855 struct btusb_data *data = hci_get_drvdata(hdev);
858 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
861 if (!test_bit(HCI_RUNNING, &hdev->flags))
864 if (urb->status == 0) {
865 hdev->stat.byte_rx += urb->actual_length;
867 if (btusb_recv_intr(data, urb->transfer_buffer,
868 urb->actual_length) < 0) {
869 bt_dev_err(hdev, "corrupted event packet");
872 } else if (urb->status == -ENOENT) {
873 /* Avoid suspend failed when usb_kill_urb */
877 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
880 usb_mark_last_busy(data->udev);
881 usb_anchor_urb(urb, &data->intr_anchor);
883 err = usb_submit_urb(urb, GFP_ATOMIC);
885 /* -EPERM: urb is being killed;
886 * -ENODEV: device got disconnected
888 if (err != -EPERM && err != -ENODEV)
889 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
891 usb_unanchor_urb(urb);
895 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
897 struct btusb_data *data = hci_get_drvdata(hdev);
903 BT_DBG("%s", hdev->name);
908 urb = usb_alloc_urb(0, mem_flags);
912 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
914 buf = kmalloc(size, mem_flags);
920 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
922 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
923 btusb_intr_complete, hdev, data->intr_ep->bInterval);
925 urb->transfer_flags |= URB_FREE_BUFFER;
927 usb_anchor_urb(urb, &data->intr_anchor);
929 err = usb_submit_urb(urb, mem_flags);
931 if (err != -EPERM && err != -ENODEV)
932 bt_dev_err(hdev, "urb %p submission failed (%d)",
934 usb_unanchor_urb(urb);
942 static void btusb_bulk_complete(struct urb *urb)
944 struct hci_dev *hdev = urb->context;
945 struct btusb_data *data = hci_get_drvdata(hdev);
948 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
951 if (!test_bit(HCI_RUNNING, &hdev->flags))
954 if (urb->status == 0) {
955 hdev->stat.byte_rx += urb->actual_length;
957 if (data->recv_bulk(data, urb->transfer_buffer,
958 urb->actual_length) < 0) {
959 bt_dev_err(hdev, "corrupted ACL packet");
962 } else if (urb->status == -ENOENT) {
963 /* Avoid suspend failed when usb_kill_urb */
967 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
970 usb_anchor_urb(urb, &data->bulk_anchor);
971 usb_mark_last_busy(data->udev);
973 err = usb_submit_urb(urb, GFP_ATOMIC);
975 /* -EPERM: urb is being killed;
976 * -ENODEV: device got disconnected
978 if (err != -EPERM && err != -ENODEV)
979 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
981 usb_unanchor_urb(urb);
985 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
987 struct btusb_data *data = hci_get_drvdata(hdev);
991 int err, size = HCI_MAX_FRAME_SIZE;
993 BT_DBG("%s", hdev->name);
995 if (!data->bulk_rx_ep)
998 urb = usb_alloc_urb(0, mem_flags);
1002 buf = kmalloc(size, mem_flags);
1008 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1010 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1011 btusb_bulk_complete, hdev);
1013 urb->transfer_flags |= URB_FREE_BUFFER;
1015 usb_mark_last_busy(data->udev);
1016 usb_anchor_urb(urb, &data->bulk_anchor);
1018 err = usb_submit_urb(urb, mem_flags);
1020 if (err != -EPERM && err != -ENODEV)
1021 bt_dev_err(hdev, "urb %p submission failed (%d)",
1023 usb_unanchor_urb(urb);
1031 static void btusb_isoc_complete(struct urb *urb)
1033 struct hci_dev *hdev = urb->context;
1034 struct btusb_data *data = hci_get_drvdata(hdev);
1037 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1038 urb->actual_length);
1040 if (!test_bit(HCI_RUNNING, &hdev->flags))
1043 if (urb->status == 0) {
1044 for (i = 0; i < urb->number_of_packets; i++) {
1045 unsigned int offset = urb->iso_frame_desc[i].offset;
1046 unsigned int length = urb->iso_frame_desc[i].actual_length;
1048 if (urb->iso_frame_desc[i].status)
1051 hdev->stat.byte_rx += length;
1053 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1055 bt_dev_err(hdev, "corrupted SCO packet");
1056 hdev->stat.err_rx++;
1059 } else if (urb->status == -ENOENT) {
1060 /* Avoid suspend failed when usb_kill_urb */
1064 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1067 usb_anchor_urb(urb, &data->isoc_anchor);
1069 err = usb_submit_urb(urb, GFP_ATOMIC);
1071 /* -EPERM: urb is being killed;
1072 * -ENODEV: device got disconnected
1074 if (err != -EPERM && err != -ENODEV)
1075 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1077 usb_unanchor_urb(urb);
1081 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1082 int mtu, struct btusb_data *data)
1085 unsigned int interval;
1087 BT_DBG("len %d mtu %d", len, mtu);
1089 /* For mSBC ALT 6 setting the host will send the packet at continuous
1090 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1091 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1092 * To maintain the rate we send 63bytes of usb packets alternatively for
1093 * 7ms and 8ms to maintain the rate as 7.5ms.
1095 if (data->usb_alt6_packet_flow) {
1097 data->usb_alt6_packet_flow = false;
1100 data->usb_alt6_packet_flow = true;
1103 for (i = 0; i < interval; i++) {
1104 urb->iso_frame_desc[i].offset = offset;
1105 urb->iso_frame_desc[i].length = offset;
1108 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1109 urb->iso_frame_desc[i].offset = offset;
1110 urb->iso_frame_desc[i].length = len;
1114 urb->number_of_packets = i;
1117 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1121 BT_DBG("len %d mtu %d", len, mtu);
1123 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1124 i++, offset += mtu, len -= mtu) {
1125 urb->iso_frame_desc[i].offset = offset;
1126 urb->iso_frame_desc[i].length = mtu;
1129 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1130 urb->iso_frame_desc[i].offset = offset;
1131 urb->iso_frame_desc[i].length = len;
1135 urb->number_of_packets = i;
1138 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1140 struct btusb_data *data = hci_get_drvdata(hdev);
1146 BT_DBG("%s", hdev->name);
1148 if (!data->isoc_rx_ep)
1151 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1155 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1156 BTUSB_MAX_ISOC_FRAMES;
1158 buf = kmalloc(size, mem_flags);
1164 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1166 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1167 hdev, data->isoc_rx_ep->bInterval);
1169 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1171 __fill_isoc_descriptor(urb, size,
1172 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1174 usb_anchor_urb(urb, &data->isoc_anchor);
1176 err = usb_submit_urb(urb, mem_flags);
1178 if (err != -EPERM && err != -ENODEV)
1179 bt_dev_err(hdev, "urb %p submission failed (%d)",
1181 usb_unanchor_urb(urb);
1189 static void btusb_diag_complete(struct urb *urb)
1191 struct hci_dev *hdev = urb->context;
1192 struct btusb_data *data = hci_get_drvdata(hdev);
1195 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1196 urb->actual_length);
1198 if (urb->status == 0) {
1199 struct sk_buff *skb;
1201 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1203 skb_put_data(skb, urb->transfer_buffer,
1204 urb->actual_length);
1205 hci_recv_diag(hdev, skb);
1207 } else if (urb->status == -ENOENT) {
1208 /* Avoid suspend failed when usb_kill_urb */
1212 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1215 usb_anchor_urb(urb, &data->diag_anchor);
1216 usb_mark_last_busy(data->udev);
1218 err = usb_submit_urb(urb, GFP_ATOMIC);
1220 /* -EPERM: urb is being killed;
1221 * -ENODEV: device got disconnected
1223 if (err != -EPERM && err != -ENODEV)
1224 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1226 usb_unanchor_urb(urb);
1230 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1232 struct btusb_data *data = hci_get_drvdata(hdev);
1236 int err, size = HCI_MAX_FRAME_SIZE;
1238 BT_DBG("%s", hdev->name);
1240 if (!data->diag_rx_ep)
1243 urb = usb_alloc_urb(0, mem_flags);
1247 buf = kmalloc(size, mem_flags);
1253 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1255 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1256 btusb_diag_complete, hdev);
1258 urb->transfer_flags |= URB_FREE_BUFFER;
1260 usb_mark_last_busy(data->udev);
1261 usb_anchor_urb(urb, &data->diag_anchor);
1263 err = usb_submit_urb(urb, mem_flags);
1265 if (err != -EPERM && err != -ENODEV)
1266 bt_dev_err(hdev, "urb %p submission failed (%d)",
1268 usb_unanchor_urb(urb);
1276 static void btusb_tx_complete(struct urb *urb)
1278 struct sk_buff *skb = urb->context;
1279 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1280 struct btusb_data *data = hci_get_drvdata(hdev);
1281 unsigned long flags;
1283 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1284 urb->actual_length);
1286 if (!test_bit(HCI_RUNNING, &hdev->flags))
1290 hdev->stat.byte_tx += urb->transfer_buffer_length;
1292 hdev->stat.err_tx++;
1295 spin_lock_irqsave(&data->txlock, flags);
1296 data->tx_in_flight--;
1297 spin_unlock_irqrestore(&data->txlock, flags);
1299 kfree(urb->setup_packet);
1304 static void btusb_isoc_tx_complete(struct urb *urb)
1306 struct sk_buff *skb = urb->context;
1307 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1309 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1310 urb->actual_length);
1312 if (!test_bit(HCI_RUNNING, &hdev->flags))
1316 hdev->stat.byte_tx += urb->transfer_buffer_length;
1318 hdev->stat.err_tx++;
1321 kfree(urb->setup_packet);
1326 static int btusb_open(struct hci_dev *hdev)
1328 struct btusb_data *data = hci_get_drvdata(hdev);
1331 BT_DBG("%s", hdev->name);
1333 err = usb_autopm_get_interface(data->intf);
1337 /* Patching USB firmware files prior to starting any URBs of HCI path
1338 * It is more safe to use USB bulk channel for downloading USB patch
1340 if (data->setup_on_usb) {
1341 err = data->setup_on_usb(hdev);
1346 data->intf->needs_remote_wakeup = 1;
1348 /* Disable device remote wakeup when host is suspended
1349 * For Realtek chips, global suspend without
1350 * SET_FEATURE (DEVICE_REMOTE_WAKEUP) can save more power in device.
1352 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1353 device_wakeup_disable(&data->udev->dev);
1355 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1358 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1362 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1364 usb_kill_anchored_urbs(&data->intr_anchor);
1368 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1369 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1372 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1373 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1377 usb_autopm_put_interface(data->intf);
1381 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1383 usb_autopm_put_interface(data->intf);
1387 static void btusb_stop_traffic(struct btusb_data *data)
1389 usb_kill_anchored_urbs(&data->intr_anchor);
1390 usb_kill_anchored_urbs(&data->bulk_anchor);
1391 usb_kill_anchored_urbs(&data->isoc_anchor);
1392 usb_kill_anchored_urbs(&data->diag_anchor);
1393 usb_kill_anchored_urbs(&data->ctrl_anchor);
1396 static int btusb_close(struct hci_dev *hdev)
1398 struct btusb_data *data = hci_get_drvdata(hdev);
1401 BT_DBG("%s", hdev->name);
1403 cancel_work_sync(&data->work);
1404 cancel_work_sync(&data->waker);
1406 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1407 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1408 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1409 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1411 btusb_stop_traffic(data);
1412 btusb_free_frags(data);
1414 err = usb_autopm_get_interface(data->intf);
1418 data->intf->needs_remote_wakeup = 0;
1420 /* Enable remote wake up for auto-suspend */
1421 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1422 data->intf->needs_remote_wakeup = 1;
1424 usb_autopm_put_interface(data->intf);
1427 usb_scuttle_anchored_urbs(&data->deferred);
1431 static int btusb_flush(struct hci_dev *hdev)
1433 struct btusb_data *data = hci_get_drvdata(hdev);
1435 BT_DBG("%s", hdev->name);
1437 usb_kill_anchored_urbs(&data->tx_anchor);
1438 btusb_free_frags(data);
1443 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1445 struct btusb_data *data = hci_get_drvdata(hdev);
1446 struct usb_ctrlrequest *dr;
1450 urb = usb_alloc_urb(0, GFP_KERNEL);
1452 return ERR_PTR(-ENOMEM);
1454 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1457 return ERR_PTR(-ENOMEM);
1460 dr->bRequestType = data->cmdreq_type;
1461 dr->bRequest = data->cmdreq;
1464 dr->wLength = __cpu_to_le16(skb->len);
1466 pipe = usb_sndctrlpipe(data->udev, 0x00);
1468 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1469 skb->data, skb->len, btusb_tx_complete, skb);
1471 skb->dev = (void *)hdev;
1476 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1478 struct btusb_data *data = hci_get_drvdata(hdev);
1482 if (!data->bulk_tx_ep)
1483 return ERR_PTR(-ENODEV);
1485 urb = usb_alloc_urb(0, GFP_KERNEL);
1487 return ERR_PTR(-ENOMEM);
1489 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1491 usb_fill_bulk_urb(urb, data->udev, pipe,
1492 skb->data, skb->len, btusb_tx_complete, skb);
1494 skb->dev = (void *)hdev;
1499 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1501 struct btusb_data *data = hci_get_drvdata(hdev);
1505 if (!data->isoc_tx_ep)
1506 return ERR_PTR(-ENODEV);
1508 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1510 return ERR_PTR(-ENOMEM);
1512 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1514 usb_fill_int_urb(urb, data->udev, pipe,
1515 skb->data, skb->len, btusb_isoc_tx_complete,
1516 skb, data->isoc_tx_ep->bInterval);
1518 urb->transfer_flags = URB_ISO_ASAP;
1520 if (data->isoc_altsetting == 6)
1521 __fill_isoc_descriptor_msbc(urb, skb->len,
1522 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1525 __fill_isoc_descriptor(urb, skb->len,
1526 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1527 skb->dev = (void *)hdev;
1532 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1534 struct btusb_data *data = hci_get_drvdata(hdev);
1537 usb_anchor_urb(urb, &data->tx_anchor);
1539 err = usb_submit_urb(urb, GFP_KERNEL);
1541 if (err != -EPERM && err != -ENODEV)
1542 bt_dev_err(hdev, "urb %p submission failed (%d)",
1544 kfree(urb->setup_packet);
1545 usb_unanchor_urb(urb);
1547 usb_mark_last_busy(data->udev);
1554 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1556 struct btusb_data *data = hci_get_drvdata(hdev);
1557 unsigned long flags;
1560 spin_lock_irqsave(&data->txlock, flags);
1561 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1563 data->tx_in_flight++;
1564 spin_unlock_irqrestore(&data->txlock, flags);
1567 return submit_tx_urb(hdev, urb);
1569 usb_anchor_urb(urb, &data->deferred);
1570 schedule_work(&data->waker);
1576 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1580 BT_DBG("%s", hdev->name);
1582 switch (hci_skb_pkt_type(skb)) {
1583 case HCI_COMMAND_PKT:
1584 urb = alloc_ctrl_urb(hdev, skb);
1586 return PTR_ERR(urb);
1588 hdev->stat.cmd_tx++;
1589 return submit_or_queue_tx_urb(hdev, urb);
1591 case HCI_ACLDATA_PKT:
1592 urb = alloc_bulk_urb(hdev, skb);
1594 return PTR_ERR(urb);
1596 hdev->stat.acl_tx++;
1597 return submit_or_queue_tx_urb(hdev, urb);
1599 case HCI_SCODATA_PKT:
1600 if (hci_conn_num(hdev, SCO_LINK) < 1)
1603 urb = alloc_isoc_urb(hdev, skb);
1605 return PTR_ERR(urb);
1607 hdev->stat.sco_tx++;
1608 return submit_tx_urb(hdev, urb);
1614 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1616 struct btusb_data *data = hci_get_drvdata(hdev);
1618 BT_DBG("%s evt %d", hdev->name, evt);
1620 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1621 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1622 data->air_mode = evt;
1623 schedule_work(&data->work);
1627 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1629 struct btusb_data *data = hci_get_drvdata(hdev);
1630 struct usb_interface *intf = data->isoc;
1631 struct usb_endpoint_descriptor *ep_desc;
1637 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
1639 bt_dev_err(hdev, "setting interface failed (%d)", -err);
1643 data->isoc_altsetting = altsetting;
1645 data->isoc_tx_ep = NULL;
1646 data->isoc_rx_ep = NULL;
1648 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1649 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1651 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1652 data->isoc_tx_ep = ep_desc;
1656 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1657 data->isoc_rx_ep = ep_desc;
1662 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1663 bt_dev_err(hdev, "invalid SCO descriptors");
1670 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1672 struct btusb_data *data = hci_get_drvdata(hdev);
1675 if (data->isoc_altsetting != new_alts) {
1676 unsigned long flags;
1678 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1679 usb_kill_anchored_urbs(&data->isoc_anchor);
1681 /* When isochronous alternate setting needs to be
1682 * changed, because SCO connection has been added
1683 * or removed, a packet fragment may be left in the
1684 * reassembling state. This could lead to wrongly
1685 * assembled fragments.
1687 * Clear outstanding fragment when selecting a new
1688 * alternate setting.
1690 spin_lock_irqsave(&data->rxlock, flags);
1691 kfree_skb(data->sco_skb);
1692 data->sco_skb = NULL;
1693 spin_unlock_irqrestore(&data->rxlock, flags);
1695 err = __set_isoc_interface(hdev, new_alts);
1700 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1701 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1702 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1704 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1710 static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1713 struct usb_interface *intf = data->isoc;
1716 BT_DBG("Looking for Alt no :%d", alt);
1721 for (i = 0; i < intf->num_altsetting; i++) {
1722 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1723 return &intf->altsetting[i];
1729 static void btusb_work(struct work_struct *work)
1731 struct btusb_data *data = container_of(work, struct btusb_data, work);
1732 struct hci_dev *hdev = data->hdev;
1736 if (data->sco_num > 0) {
1737 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1738 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1740 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1741 usb_kill_anchored_urbs(&data->isoc_anchor);
1745 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1748 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1749 if (hdev->voice_setting & 0x0020) {
1750 static const int alts[3] = { 2, 4, 5 };
1752 new_alts = alts[data->sco_num - 1];
1754 new_alts = data->sco_num;
1756 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
1757 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1758 * many adapters do not support it. Alt 1 appears to
1759 * work for all adapters that do not have alt 6, and
1760 * which work with WBS at all.
1762 new_alts = btusb_find_altsetting(data, 6) ? 6 : 1;
1763 /* Because mSBC frames do not need to be aligned to the
1764 * SCO packet boundary. If support the Alt 3, use the
1765 * Alt 3 for HCI payload >= 60 Bytes let air packet
1766 * data satisfy 60 bytes.
1768 if (new_alts == 1 && btusb_find_altsetting(data, 3))
1772 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1773 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
1775 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1776 usb_kill_anchored_urbs(&data->isoc_anchor);
1778 __set_isoc_interface(hdev, 0);
1779 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1780 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1784 static void btusb_waker(struct work_struct *work)
1786 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1789 err = usb_autopm_get_interface(data->intf);
1793 usb_autopm_put_interface(data->intf);
1796 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1798 struct sk_buff *skb;
1801 BT_DBG("%s", hdev->name);
1803 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1805 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
1812 static int btusb_setup_csr(struct hci_dev *hdev)
1814 struct btusb_data *data = hci_get_drvdata(hdev);
1815 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
1816 struct hci_rp_read_local_version *rp;
1817 struct sk_buff *skb;
1818 bool is_fake = false;
1821 BT_DBG("%s", hdev->name);
1823 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1826 int err = PTR_ERR(skb);
1827 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
1831 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1832 bt_dev_err(hdev, "CSR: Local version length mismatch");
1837 rp = (struct hci_rp_read_local_version *)skb->data;
1839 /* Detect a wide host of Chinese controllers that aren't CSR.
1841 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1843 * The main thing they have in common is that these are really popular low-cost
1844 * options that support newer Bluetooth versions but rely on heavy VID/PID
1845 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1847 * We detect actual CSR devices by checking that the HCI manufacturer code
1848 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1849 * HCI rev values always match. As they both store the firmware number.
1851 if (le16_to_cpu(rp->manufacturer) != 10 ||
1852 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
1855 /* Known legit CSR firmware build numbers and their supported BT versions:
1856 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1857 * - 1.2 (0x2) -> 0x04d9, 0x0529
1858 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
1859 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
1860 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
1862 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
1863 * support BT 1.1 only; so it's a dead giveaway when some
1864 * third-party BT 4.0 dongle reuses it.
1866 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
1867 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
1870 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
1871 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
1874 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
1875 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
1878 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
1879 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
1882 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
1883 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
1886 /* Other clones which beat all the above checks */
1887 else if (bcdDevice == 0x0134 &&
1888 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
1889 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
1893 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds...");
1895 /* Generally these clones have big discrepancies between
1896 * advertised features and what's actually supported.
1897 * Probably will need to be expanded in the future;
1898 * without these the controller will lock up.
1900 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1901 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
1903 /* Clear the reset quirk since this is not an actual
1904 * early Bluetooth 1.1 device from CSR.
1906 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1907 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1910 * Special workaround for clones with a Barrot 8041a02 chip,
1911 * these clones are really messed-up:
1912 * 1. Their bulk rx endpoint will never report any data unless
1913 * the device was suspended at least once (yes really).
1914 * 2. They will not wakeup when autosuspended and receiving data
1915 * on their bulk rx endpoint from e.g. a keyboard or mouse
1916 * (IOW remote-wakeup support is broken for the bulk endpoint).
1918 * To fix 1. enable runtime-suspend, force-suspend the
1919 * hci and then wake-it up by disabling runtime-suspend.
1921 * To fix 2. clear the hci's can_wake flag, this way the hci
1922 * will still be autosuspended when it is not open.
1924 if (bcdDevice == 0x8891 &&
1925 le16_to_cpu(rp->lmp_subver) == 0x1012 &&
1926 le16_to_cpu(rp->hci_rev) == 0x0810 &&
1927 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_4_0) {
1928 bt_dev_warn(hdev, "CSR: detected a fake CSR dongle using a Barrot 8041a02 chip, this chip is very buggy and may have issues");
1930 pm_runtime_allow(&data->udev->dev);
1932 ret = pm_runtime_suspend(&data->udev->dev);
1936 bt_dev_err(hdev, "Failed to suspend the device for Barrot 8041a02 receive-issue workaround");
1938 pm_runtime_forbid(&data->udev->dev);
1940 device_set_wakeup_capable(&data->udev->dev, false);
1941 /* Re-enable autosuspend if this was requested */
1942 if (enable_autosuspend)
1943 usb_enable_autosuspend(data->udev);
1952 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1953 struct intel_version *ver)
1955 const struct firmware *fw;
1959 snprintf(fwname, sizeof(fwname),
1960 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1961 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1962 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1963 ver->fw_build_ww, ver->fw_build_yy);
1965 ret = request_firmware(&fw, fwname, &hdev->dev);
1967 if (ret == -EINVAL) {
1968 bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1973 bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1976 /* If the correct firmware patch file is not found, use the
1977 * default firmware patch file instead
1979 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1980 ver->hw_platform, ver->hw_variant);
1981 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1982 bt_dev_err(hdev, "failed to open default fw file: %s",
1988 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
1993 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1994 const struct firmware *fw,
1995 const u8 **fw_ptr, int *disable_patch)
1997 struct sk_buff *skb;
1998 struct hci_command_hdr *cmd;
1999 const u8 *cmd_param;
2000 struct hci_event_hdr *evt = NULL;
2001 const u8 *evt_param = NULL;
2002 int remain = fw->size - (*fw_ptr - fw->data);
2004 /* The first byte indicates the types of the patch command or event.
2005 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
2006 * in the current firmware buffer doesn't start with 0x01 or
2007 * the size of remain buffer is smaller than HCI command header,
2008 * the firmware file is corrupted and it should stop the patching
2011 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
2012 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
2018 cmd = (struct hci_command_hdr *)(*fw_ptr);
2019 *fw_ptr += sizeof(*cmd);
2020 remain -= sizeof(*cmd);
2022 /* Ensure that the remain firmware data is long enough than the length
2023 * of command parameter. If not, the firmware file is corrupted.
2025 if (remain < cmd->plen) {
2026 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
2030 /* If there is a command that loads a patch in the firmware
2031 * file, then enable the patch upon success, otherwise just
2032 * disable the manufacturer mode, for example patch activation
2033 * is not required when the default firmware patch file is used
2034 * because there are no patch data to load.
2036 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
2039 cmd_param = *fw_ptr;
2040 *fw_ptr += cmd->plen;
2041 remain -= cmd->plen;
2043 /* This reads the expected events when the above command is sent to the
2044 * device. Some vendor commands expects more than one events, for
2045 * example command status event followed by vendor specific event.
2046 * For this case, it only keeps the last expected event. so the command
2047 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
2048 * last expected event.
2050 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
2054 evt = (struct hci_event_hdr *)(*fw_ptr);
2055 *fw_ptr += sizeof(*evt);
2056 remain -= sizeof(*evt);
2058 if (remain < evt->plen) {
2059 bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
2063 evt_param = *fw_ptr;
2064 *fw_ptr += evt->plen;
2065 remain -= evt->plen;
2068 /* Every HCI commands in the firmware file has its correspond event.
2069 * If event is not found or remain is smaller than zero, the firmware
2070 * file is corrupted.
2072 if (!evt || !evt_param || remain < 0) {
2073 bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
2077 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
2078 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
2080 bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
2081 cmd->opcode, PTR_ERR(skb));
2082 return PTR_ERR(skb);
2085 /* It ensures that the returned event matches the event data read from
2086 * the firmware file. At fist, it checks the length and then
2087 * the contents of the event.
2089 if (skb->len != evt->plen) {
2090 bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
2091 le16_to_cpu(cmd->opcode));
2096 if (memcmp(skb->data, evt_param, evt->plen)) {
2097 bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
2098 le16_to_cpu(cmd->opcode));
2107 static int btusb_setup_intel(struct hci_dev *hdev)
2109 struct sk_buff *skb;
2110 const struct firmware *fw;
2112 int disable_patch, err;
2113 struct intel_version ver;
2115 BT_DBG("%s", hdev->name);
2117 /* The controller has a bug with the first HCI command sent to it
2118 * returning number of completed commands as zero. This would stall the
2119 * command processing in the Bluetooth core.
2121 * As a workaround, send HCI Reset command first which will reset the
2122 * number of completed commands and allow normal command processing
2125 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2127 bt_dev_err(hdev, "sending initial HCI reset command failed (%ld)",
2129 return PTR_ERR(skb);
2133 /* Read Intel specific controller version first to allow selection of
2134 * which firmware file to load.
2136 * The returned information are hardware variant and revision plus
2137 * firmware variant, revision and build number.
2139 err = btintel_read_version(hdev, &ver);
2143 bt_dev_info(hdev, "read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
2144 ver.hw_platform, ver.hw_variant, ver.hw_revision,
2145 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
2146 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
2148 /* fw_patch_num indicates the version of patch the device currently
2149 * have. If there is no patch data in the device, it is always 0x00.
2150 * So, if it is other than 0x00, no need to patch the device again.
2152 if (ver.fw_patch_num) {
2153 bt_dev_info(hdev, "Intel device is already patched. "
2154 "patch num: %02x", ver.fw_patch_num);
2158 /* Opens the firmware patch file based on the firmware version read
2159 * from the controller. If it fails to open the matching firmware
2160 * patch file, it tries to open the default firmware patch file.
2161 * If no patch file is found, allow the device to operate without
2164 fw = btusb_setup_intel_get_fw(hdev, &ver);
2169 /* Enable the manufacturer mode of the controller.
2170 * Only while this mode is enabled, the driver can download the
2171 * firmware patch data and configuration parameters.
2173 err = btintel_enter_mfg(hdev);
2175 release_firmware(fw);
2181 /* The firmware data file consists of list of Intel specific HCI
2182 * commands and its expected events. The first byte indicates the
2183 * type of the message, either HCI command or HCI event.
2185 * It reads the command and its expected event from the firmware file,
2186 * and send to the controller. Once __hci_cmd_sync_ev() returns,
2187 * the returned event is compared with the event read from the firmware
2188 * file and it will continue until all the messages are downloaded to
2191 * Once the firmware patching is completed successfully,
2192 * the manufacturer mode is disabled with reset and activating the
2195 * If the firmware patching fails, the manufacturer mode is
2196 * disabled with reset and deactivating the patch.
2198 * If the default patch file is used, no reset is done when disabling
2201 while (fw->size > fw_ptr - fw->data) {
2204 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
2207 goto exit_mfg_deactivate;
2210 release_firmware(fw);
2213 goto exit_mfg_disable;
2215 /* Patching completed successfully and disable the manufacturer mode
2216 * with reset and activate the downloaded firmware patches.
2218 err = btintel_exit_mfg(hdev, true, true);
2222 /* Need build number for downloaded fw patches in
2223 * every power-on boot
2225 err = btintel_read_version(hdev, &ver);
2228 bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
2234 /* Disable the manufacturer mode without reset */
2235 err = btintel_exit_mfg(hdev, false, false);
2239 bt_dev_info(hdev, "Intel firmware patch completed");
2243 exit_mfg_deactivate:
2244 release_firmware(fw);
2246 /* Patching failed. Disable the manufacturer mode with reset and
2247 * deactivate the downloaded firmware patches.
2249 err = btintel_exit_mfg(hdev, true, false);
2253 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
2256 /* Set the event mask for Intel specific vendor events. This enables
2257 * a few extra events that are useful during general operation.
2259 btintel_set_event_mask_mfg(hdev, false);
2261 btintel_check_bdaddr(hdev);
2265 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2267 struct sk_buff *skb;
2268 struct hci_event_hdr *hdr;
2269 struct hci_ev_cmd_complete *evt;
2271 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2275 hdr = skb_put(skb, sizeof(*hdr));
2276 hdr->evt = HCI_EV_CMD_COMPLETE;
2277 hdr->plen = sizeof(*evt) + 1;
2279 evt = skb_put(skb, sizeof(*evt));
2281 evt->opcode = cpu_to_le16(opcode);
2283 skb_put_u8(skb, 0x00);
2285 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2287 return hci_recv_frame(hdev, skb);
2290 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2293 /* When the device is in bootloader mode, then it can send
2294 * events via the bulk endpoint. These events are treated the
2295 * same way as the ones received from the interrupt endpoint.
2297 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
2298 return btusb_recv_intr(data, buffer, count);
2300 return btusb_recv_bulk(data, buffer, count);
2303 static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
2306 const struct intel_bootup *evt = ptr;
2308 if (len != sizeof(*evt))
2311 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags))
2312 wake_up_bit(&data->flags, BTUSB_BOOTING);
2315 static void btusb_intel_secure_send_result(struct btusb_data *data,
2316 const void *ptr, unsigned int len)
2318 const struct intel_secure_send_result *evt = ptr;
2320 if (len != sizeof(*evt))
2324 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
2326 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
2327 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags))
2328 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
2331 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2333 struct btusb_data *data = hci_get_drvdata(hdev);
2335 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2336 struct hci_event_hdr *hdr = (void *)skb->data;
2338 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2340 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2341 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
2343 switch (skb->data[2]) {
2345 /* When switching to the operational firmware
2346 * the device sends a vendor specific event
2347 * indicating that the bootup completed.
2349 btusb_intel_bootup(data, ptr, len);
2352 /* When the firmware loading completes the
2353 * device sends out a vendor specific event
2354 * indicating the result of the firmware
2357 btusb_intel_secure_send_result(data, ptr, len);
2363 return hci_recv_frame(hdev, skb);
2366 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2368 struct btusb_data *data = hci_get_drvdata(hdev);
2371 BT_DBG("%s", hdev->name);
2373 switch (hci_skb_pkt_type(skb)) {
2374 case HCI_COMMAND_PKT:
2375 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2376 struct hci_command_hdr *cmd = (void *)skb->data;
2377 __u16 opcode = le16_to_cpu(cmd->opcode);
2379 /* When in bootloader mode and the command 0xfc09
2380 * is received, it needs to be send down the
2381 * bulk endpoint. So allocate a bulk URB instead.
2383 if (opcode == 0xfc09)
2384 urb = alloc_bulk_urb(hdev, skb);
2386 urb = alloc_ctrl_urb(hdev, skb);
2388 /* When the 0xfc01 command is issued to boot into
2389 * the operational firmware, it will actually not
2390 * send a command complete event. To keep the flow
2391 * control working inject that event here.
2393 if (opcode == 0xfc01)
2394 inject_cmd_complete(hdev, opcode);
2396 urb = alloc_ctrl_urb(hdev, skb);
2399 return PTR_ERR(urb);
2401 hdev->stat.cmd_tx++;
2402 return submit_or_queue_tx_urb(hdev, urb);
2404 case HCI_ACLDATA_PKT:
2405 urb = alloc_bulk_urb(hdev, skb);
2407 return PTR_ERR(urb);
2409 hdev->stat.acl_tx++;
2410 return submit_or_queue_tx_urb(hdev, urb);
2412 case HCI_SCODATA_PKT:
2413 if (hci_conn_num(hdev, SCO_LINK) < 1)
2416 urb = alloc_isoc_urb(hdev, skb);
2418 return PTR_ERR(urb);
2420 hdev->stat.sco_tx++;
2421 return submit_tx_urb(hdev, urb);
2427 static int btusb_setup_intel_new_get_fw_name(struct intel_version *ver,
2428 struct intel_boot_params *params,
2429 char *fw_name, size_t len,
2432 switch (ver->hw_variant) {
2433 case 0x0b: /* SfP */
2434 case 0x0c: /* WsP */
2435 snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
2436 le16_to_cpu(ver->hw_variant),
2437 le16_to_cpu(params->dev_revid),
2440 case 0x11: /* JfP */
2441 case 0x12: /* ThP */
2442 case 0x13: /* HrP */
2443 case 0x14: /* CcP */
2444 snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
2445 le16_to_cpu(ver->hw_variant),
2446 le16_to_cpu(ver->hw_revision),
2447 le16_to_cpu(ver->fw_revision),
2457 static void btusb_setup_intel_newgen_get_fw_name(const struct intel_version_tlv *ver_tlv,
2458 char *fw_name, size_t len,
2461 /* The firmware file name for new generation controllers will be
2462 * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
2464 snprintf(fw_name, len, "intel/ibt-%04x-%04x.%s",
2465 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver_tlv->cnvi_top),
2466 INTEL_CNVX_TOP_STEP(ver_tlv->cnvi_top)),
2467 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver_tlv->cnvr_top),
2468 INTEL_CNVX_TOP_STEP(ver_tlv->cnvr_top)),
2472 static int btusb_download_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
2474 struct btusb_data *data = hci_get_drvdata(hdev);
2475 ktime_t delta, rettime;
2476 unsigned long long duration;
2479 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2481 bt_dev_info(hdev, "Waiting for firmware download to complete");
2483 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2485 msecs_to_jiffies(msec));
2486 if (err == -EINTR) {
2487 bt_dev_err(hdev, "Firmware loading interrupted");
2492 bt_dev_err(hdev, "Firmware loading timeout");
2496 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2497 bt_dev_err(hdev, "Firmware loading failed");
2501 rettime = ktime_get();
2502 delta = ktime_sub(rettime, calltime);
2503 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2505 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
2510 static int btusb_intel_download_firmware_newgen(struct hci_dev *hdev,
2511 struct intel_version_tlv *ver,
2514 const struct firmware *fw;
2517 struct btusb_data *data = hci_get_drvdata(hdev);
2520 if (!ver || !boot_param)
2523 /* The firmware variant determines if the device is in bootloader
2524 * mode or is running operational firmware. The value 0x03 identifies
2525 * the bootloader and the value 0x23 identifies the operational
2528 * When the operational firmware is already present, then only
2529 * the check for valid Bluetooth device address is needed. This
2530 * determines if the device will be added as configured or
2531 * unconfigured controller.
2533 * It is not possible to use the Secure Boot Parameters in this
2534 * case since that command is only available in bootloader mode.
2536 if (ver->img_type == 0x03) {
2537 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2538 btintel_check_bdaddr(hdev);
2541 /* If the OTP has no valid Bluetooth device address, then there will
2542 * also be no valid address for the operational firmware.
2544 if (!bacmp(&ver->otp_bd_addr, BDADDR_ANY)) {
2545 bt_dev_info(hdev, "No device address configured");
2546 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2549 btusb_setup_intel_newgen_get_fw_name(ver, fwname, sizeof(fwname), "sfi");
2550 err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
2552 if (!test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2553 /* Firmware has already been loaded */
2554 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2558 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2564 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2566 if (fw->size < 644) {
2567 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2573 calltime = ktime_get();
2575 set_bit(BTUSB_DOWNLOADING, &data->flags);
2577 /* Start firmware downloading and get boot parameter */
2578 err = btintel_download_firmware_newgen(hdev, ver, fw, boot_param,
2579 INTEL_HW_VARIANT(ver->cnvi_bt),
2582 if (err == -EALREADY) {
2583 /* Firmware has already been loaded */
2584 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2589 /* When FW download fails, send Intel Reset to retry
2592 btintel_reset_to_bootloader(hdev);
2596 /* Before switching the device into operational mode and with that
2597 * booting the loaded firmware, wait for the bootloader notification
2598 * that all fragments have been successfully received.
2600 * When the event processing receives the notification, then the
2601 * BTUSB_DOWNLOADING flag will be cleared.
2603 * The firmware loading should not take longer than 5 seconds
2604 * and thus just timeout if that happens and fail the setup
2607 err = btusb_download_wait(hdev, calltime, 5000);
2608 if (err == -ETIMEDOUT)
2609 btintel_reset_to_bootloader(hdev);
2612 release_firmware(fw);
2616 static int btusb_intel_download_firmware(struct hci_dev *hdev,
2617 struct intel_version *ver,
2618 struct intel_boot_params *params,
2621 const struct firmware *fw;
2624 struct btusb_data *data = hci_get_drvdata(hdev);
2627 if (!ver || !params)
2630 /* The firmware variant determines if the device is in bootloader
2631 * mode or is running operational firmware. The value 0x06 identifies
2632 * the bootloader and the value 0x23 identifies the operational
2635 * When the operational firmware is already present, then only
2636 * the check for valid Bluetooth device address is needed. This
2637 * determines if the device will be added as configured or
2638 * unconfigured controller.
2640 * It is not possible to use the Secure Boot Parameters in this
2641 * case since that command is only available in bootloader mode.
2643 if (ver->fw_variant == 0x23) {
2644 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2645 btintel_check_bdaddr(hdev);
2647 /* SfP and WsP don't seem to update the firmware version on file
2648 * so version checking is currently possible.
2650 switch (ver->hw_variant) {
2651 case 0x0b: /* SfP */
2652 case 0x0c: /* WsP */
2656 /* Proceed to download to check if the version matches */
2660 /* Read the secure boot parameters to identify the operating
2661 * details of the bootloader.
2663 err = btintel_read_boot_params(hdev, params);
2667 /* It is required that every single firmware fragment is acknowledged
2668 * with a command complete event. If the boot parameters indicate
2669 * that this bootloader does not send them, then abort the setup.
2671 if (params->limited_cce != 0x00) {
2672 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
2673 params->limited_cce);
2677 /* If the OTP has no valid Bluetooth device address, then there will
2678 * also be no valid address for the operational firmware.
2680 if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) {
2681 bt_dev_info(hdev, "No device address configured");
2682 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2686 /* With this Intel bootloader only the hardware variant and device
2687 * revision information are used to select the right firmware for SfP
2690 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2692 * Currently the supported hardware variants are:
2693 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2694 * 12 (0x0c) for iBT3.5 (WsP)
2696 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2697 * variant, HW revision and FW revision, as these are dependent on CNVi
2698 * and RF Combination.
2700 * 17 (0x11) for iBT3.5 (JfP)
2701 * 18 (0x12) for iBT3.5 (ThP)
2703 * The firmware file name for these will be
2704 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2707 err = btusb_setup_intel_new_get_fw_name(ver, params, fwname,
2708 sizeof(fwname), "sfi");
2710 if (!test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2711 /* Firmware has already been loaded */
2712 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2716 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2720 err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
2722 if (!test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2723 /* Firmware has already been loaded */
2724 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2728 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2733 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2735 if (fw->size < 644) {
2736 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2742 calltime = ktime_get();
2744 set_bit(BTUSB_DOWNLOADING, &data->flags);
2746 /* Start firmware downloading and get boot parameter */
2747 err = btintel_download_firmware(hdev, ver, fw, boot_param);
2749 if (err == -EALREADY) {
2750 /* Firmware has already been loaded */
2751 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2756 /* When FW download fails, send Intel Reset to retry
2759 btintel_reset_to_bootloader(hdev);
2763 /* Before switching the device into operational mode and with that
2764 * booting the loaded firmware, wait for the bootloader notification
2765 * that all fragments have been successfully received.
2767 * When the event processing receives the notification, then the
2768 * BTUSB_DOWNLOADING flag will be cleared.
2770 * The firmware loading should not take longer than 5 seconds
2771 * and thus just timeout if that happens and fail the setup
2774 err = btusb_download_wait(hdev, calltime, 5000);
2775 if (err == -ETIMEDOUT)
2776 btintel_reset_to_bootloader(hdev);
2779 release_firmware(fw);
2783 static int btusb_boot_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
2785 struct btusb_data *data = hci_get_drvdata(hdev);
2786 ktime_t delta, rettime;
2787 unsigned long long duration;
2790 bt_dev_info(hdev, "Waiting for device to boot");
2792 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2794 msecs_to_jiffies(msec));
2795 if (err == -EINTR) {
2796 bt_dev_err(hdev, "Device boot interrupted");
2801 bt_dev_err(hdev, "Device boot timeout");
2805 rettime = ktime_get();
2806 delta = ktime_sub(rettime, calltime);
2807 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2809 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
2814 static int btusb_intel_boot(struct hci_dev *hdev, u32 boot_addr)
2816 struct btusb_data *data = hci_get_drvdata(hdev);
2820 calltime = ktime_get();
2822 set_bit(BTUSB_BOOTING, &data->flags);
2824 err = btintel_send_intel_reset(hdev, boot_addr);
2826 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
2827 btintel_reset_to_bootloader(hdev);
2831 /* The bootloader will not indicate when the device is ready. This
2832 * is done by the operational firmware sending bootup notification.
2834 * Booting into operational firmware should not take longer than
2835 * 1 second. However if that happens, then just fail the setup
2836 * since something went wrong.
2838 err = btusb_boot_wait(hdev, calltime, 1000);
2839 if (err == -ETIMEDOUT)
2840 btintel_reset_to_bootloader(hdev);
2845 static int btusb_setup_intel_new(struct hci_dev *hdev)
2847 struct btusb_data *data = hci_get_drvdata(hdev);
2848 struct intel_version ver;
2849 struct intel_boot_params params;
2853 struct intel_debug_features features;
2855 BT_DBG("%s", hdev->name);
2857 /* Set the default boot parameter to 0x0 and it is updated to
2858 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2859 * command while downloading the firmware.
2861 boot_param = 0x00000000;
2863 /* Read the Intel version information to determine if the device
2864 * is in bootloader mode or if it already has operational firmware
2867 err = btintel_read_version(hdev, &ver);
2869 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2870 btintel_reset_to_bootloader(hdev);
2874 err = btintel_version_info(hdev, &ver);
2878 err = btusb_intel_download_firmware(hdev, &ver, ¶ms, &boot_param);
2882 /* controller is already having an operational firmware */
2883 if (ver.fw_variant == 0x23)
2886 err = btusb_intel_boot(hdev, boot_param);
2890 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2892 err = btusb_setup_intel_new_get_fw_name(&ver, ¶ms, ddcname,
2893 sizeof(ddcname), "ddc");
2896 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2898 /* Once the device is running in operational mode, it needs to
2899 * apply the device configuration (DDC) parameters.
2901 * The device can work without DDC parameters, so even if it
2902 * fails to load the file, no need to fail the setup.
2904 btintel_load_ddc_config(hdev, ddcname);
2907 /* Read the Intel supported features and if new exception formats
2908 * supported, need to load the additional DDC config to enable.
2910 btintel_read_debug_features(hdev, &features);
2912 /* Set DDC mask for available debug features */
2913 btintel_set_debug_features(hdev, &features);
2915 /* Read the Intel version information after loading the FW */
2916 err = btintel_read_version(hdev, &ver);
2920 btintel_version_info(hdev, &ver);
2923 /* All Intel controllers that support the Microsoft vendor
2924 * extension are using 0xFC1E for VsMsftOpCode.
2926 switch (ver.hw_variant) {
2927 case 0x11: /* JfP */
2928 case 0x12: /* ThP */
2929 case 0x13: /* HrP */
2930 case 0x14: /* CcP */
2931 hci_set_msft_opcode(hdev, 0xFC1E);
2935 /* Set the event mask for Intel specific vendor events. This enables
2936 * a few extra events that are useful during general operation. It
2937 * does not enable any debugging related events.
2939 * The device will function correctly without these events enabled
2940 * and thus no need to fail the setup.
2942 btintel_set_event_mask(hdev, false);
2947 static int btusb_setup_intel_newgen(struct hci_dev *hdev)
2949 struct btusb_data *data = hci_get_drvdata(hdev);
2953 struct intel_debug_features features;
2954 struct intel_version_tlv version;
2956 bt_dev_dbg(hdev, "");
2958 /* Set the default boot parameter to 0x0 and it is updated to
2959 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2960 * command while downloading the firmware.
2962 boot_param = 0x00000000;
2964 /* Read the Intel version information to determine if the device
2965 * is in bootloader mode or if it already has operational firmware
2968 err = btintel_read_version_tlv(hdev, &version);
2970 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2971 btintel_reset_to_bootloader(hdev);
2975 err = btintel_version_info_tlv(hdev, &version);
2979 err = btusb_intel_download_firmware_newgen(hdev, &version, &boot_param);
2983 /* check if controller is already having an operational firmware */
2984 if (version.img_type == 0x03)
2987 err = btusb_intel_boot(hdev, boot_param);
2991 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2993 btusb_setup_intel_newgen_get_fw_name(&version, ddcname, sizeof(ddcname),
2995 /* Once the device is running in operational mode, it needs to
2996 * apply the device configuration (DDC) parameters.
2998 * The device can work without DDC parameters, so even if it
2999 * fails to load the file, no need to fail the setup.
3001 btintel_load_ddc_config(hdev, ddcname);
3003 /* Read the Intel supported features and if new exception formats
3004 * supported, need to load the additional DDC config to enable.
3006 btintel_read_debug_features(hdev, &features);
3008 /* Set DDC mask for available debug features */
3009 btintel_set_debug_features(hdev, &features);
3011 /* Read the Intel version information after loading the FW */
3012 err = btintel_read_version_tlv(hdev, &version);
3016 btintel_version_info_tlv(hdev, &version);
3019 /* Set the event mask for Intel specific vendor events. This enables
3020 * a few extra events that are useful during general operation. It
3021 * does not enable any debugging related events.
3023 * The device will function correctly without these events enabled
3024 * and thus no need to fail the setup.
3026 btintel_set_event_mask(hdev, false);
3030 static int btusb_shutdown_intel(struct hci_dev *hdev)
3032 struct sk_buff *skb;
3035 /* In the shutdown sequence where Bluetooth is turned off followed
3036 * by WiFi being turned off, turning WiFi back on causes issue with
3037 * the RF calibration.
3039 * To ensure that any RF activity has been stopped, issue HCI Reset
3040 * command to clear all ongoing activity including advertising,
3043 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3046 bt_dev_err(hdev, "HCI reset during shutdown failed");
3051 /* Some platforms have an issue with BT LED when the interface is
3052 * down or BT radio is turned off, which takes 5 seconds to BT LED
3053 * goes off. This command turns off the BT LED immediately.
3055 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
3058 bt_dev_err(hdev, "turning off Intel device LED failed");
3066 static int btusb_shutdown_intel_new(struct hci_dev *hdev)
3068 struct sk_buff *skb;
3070 /* Send HCI Reset to the controller to stop any BT activity which
3071 * were triggered. This will help to save power and maintain the
3072 * sync b/w Host and controller
3074 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3076 bt_dev_err(hdev, "HCI reset during shutdown failed");
3077 return PTR_ERR(skb);
3084 #define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
3085 #define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
3087 #define HCI_WMT_MAX_EVENT_SIZE 64
3088 /* It is for mt79xx download rom patch*/
3089 #define MTK_FW_ROM_PATCH_HEADER_SIZE 32
3090 #define MTK_FW_ROM_PATCH_GD_SIZE 64
3091 #define MTK_FW_ROM_PATCH_SEC_MAP_SIZE 64
3092 #define MTK_SEC_MAP_COMMON_SIZE 12
3093 #define MTK_SEC_MAP_NEED_SEND_SIZE 52
3096 BTMTK_WMT_PATCH_DWNLD = 0x1,
3097 BTMTK_WMT_FUNC_CTRL = 0x6,
3098 BTMTK_WMT_RST = 0x7,
3099 BTMTK_WMT_SEMAPHORE = 0x17,
3104 BTMTK_WMT_PATCH_UNDONE,
3105 BTMTK_WMT_PATCH_PROGRESS,
3106 BTMTK_WMT_PATCH_DONE,
3107 BTMTK_WMT_ON_UNDONE,
3109 BTMTK_WMT_ON_PROGRESS,
3112 struct btmtk_wmt_hdr {
3119 struct btmtk_hci_wmt_cmd {
3120 struct btmtk_wmt_hdr hdr;
3124 struct btmtk_hci_wmt_evt {
3125 struct hci_event_hdr hhdr;
3126 struct btmtk_wmt_hdr whdr;
3129 struct btmtk_hci_wmt_evt_funcc {
3130 struct btmtk_hci_wmt_evt hwhdr;
3134 struct btmtk_tci_sleep {
3137 __le16 host_duration;
3139 u8 time_compensation;
3142 struct btmtk_hci_wmt_params {
3150 struct btmtk_patch_header {
3158 struct btmtk_global_desc {
3165 struct btmtk_section_map {
3170 __le32 u4SecSpec[13];
3177 __le32 dlmodecrctype;
3184 static void btusb_mtk_wmt_recv(struct urb *urb)
3186 struct hci_dev *hdev = urb->context;
3187 struct btusb_data *data = hci_get_drvdata(hdev);
3188 struct hci_event_hdr *hdr;
3189 struct sk_buff *skb;
3192 if (urb->status == 0 && urb->actual_length > 0) {
3193 hdev->stat.byte_rx += urb->actual_length;
3195 /* WMT event shouldn't be fragmented and the size should be
3196 * less than HCI_WMT_MAX_EVENT_SIZE.
3198 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
3200 hdev->stat.err_rx++;
3204 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
3205 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
3207 hdr = (void *)skb->data;
3208 /* Fix up the vendor event id with 0xff for vendor specific
3209 * instead of 0xe4 so that event send via monitoring socket can
3210 * be parsed properly.
3214 /* When someone waits for the WMT event, the skb is being cloned
3215 * and being processed the events from there then.
3217 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
3218 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
3219 if (!data->evt_skb) {
3225 err = hci_recv_frame(hdev, skb);
3227 kfree_skb(data->evt_skb);
3228 data->evt_skb = NULL;
3232 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
3234 /* Barrier to sync with other CPUs */
3235 smp_mb__after_atomic();
3236 wake_up_bit(&data->flags,
3237 BTUSB_TX_WAIT_VND_EVT);
3240 } else if (urb->status == -ENOENT) {
3241 /* Avoid suspend failed when usb_kill_urb */
3245 usb_mark_last_busy(data->udev);
3247 /* The URB complete handler is still called with urb->actual_length = 0
3248 * when the event is not available, so we should keep re-submitting
3249 * URB until WMT event returns, Also, It's necessary to wait some time
3250 * between the two consecutive control URBs to relax the target device
3251 * to generate the event. Otherwise, the WMT event cannot return from
3252 * the device successfully.
3256 usb_anchor_urb(urb, &data->ctrl_anchor);
3257 err = usb_submit_urb(urb, GFP_ATOMIC);
3259 /* -EPERM: urb is being killed;
3260 * -ENODEV: device got disconnected
3262 if (err != -EPERM && err != -ENODEV)
3263 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
3265 usb_unanchor_urb(urb);
3269 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
3271 struct btusb_data *data = hci_get_drvdata(hdev);
3272 struct usb_ctrlrequest *dr;
3278 urb = usb_alloc_urb(0, GFP_KERNEL);
3282 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
3288 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
3290 dr->wIndex = cpu_to_le16(0);
3291 dr->wValue = cpu_to_le16(48);
3292 dr->wLength = cpu_to_le16(size);
3294 buf = kmalloc(size, GFP_KERNEL);
3301 pipe = usb_rcvctrlpipe(data->udev, 0);
3303 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
3304 buf, size, btusb_mtk_wmt_recv, hdev);
3306 urb->transfer_flags |= URB_FREE_BUFFER;
3308 usb_anchor_urb(urb, &data->ctrl_anchor);
3309 err = usb_submit_urb(urb, GFP_KERNEL);
3311 if (err != -EPERM && err != -ENODEV)
3312 bt_dev_err(hdev, "urb %p submission failed (%d)",
3314 usb_unanchor_urb(urb);
3322 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
3323 struct btmtk_hci_wmt_params *wmt_params)
3325 struct btusb_data *data = hci_get_drvdata(hdev);
3326 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
3327 u32 hlen, status = BTMTK_WMT_INVALID;
3328 struct btmtk_hci_wmt_evt *wmt_evt;
3329 struct btmtk_hci_wmt_cmd *wc;
3330 struct btmtk_wmt_hdr *hdr;
3333 /* Send the WMT command and wait until the WMT event returns */
3334 hlen = sizeof(*hdr) + wmt_params->dlen;
3338 wc = kzalloc(hlen, GFP_KERNEL);
3344 hdr->op = wmt_params->op;
3345 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
3346 hdr->flag = wmt_params->flag;
3347 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
3349 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3351 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
3354 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3358 /* Submit control IN URB on demand to process the WMT event */
3359 err = btusb_mtk_submit_wmt_recv_urb(hdev);
3363 /* The vendor specific WMT commands are all answered by a vendor
3364 * specific event and will have the Command Status or Command
3365 * Complete as with usual HCI command flow control.
3367 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
3368 * state to be cleared. The driver specific event receive routine
3369 * will clear that state and with that indicate completion of the
3372 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
3373 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
3374 if (err == -EINTR) {
3375 bt_dev_err(hdev, "Execution of wmt command interrupted");
3376 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3381 bt_dev_err(hdev, "Execution of wmt command timed out");
3382 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3387 /* Parse and handle the return WMT event */
3388 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
3389 if (wmt_evt->whdr.op != hdr->op) {
3390 bt_dev_err(hdev, "Wrong op received %d expected %d",
3391 wmt_evt->whdr.op, hdr->op);
3396 switch (wmt_evt->whdr.op) {
3397 case BTMTK_WMT_SEMAPHORE:
3398 if (wmt_evt->whdr.flag == 2)
3399 status = BTMTK_WMT_PATCH_UNDONE;
3401 status = BTMTK_WMT_PATCH_DONE;
3403 case BTMTK_WMT_FUNC_CTRL:
3404 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
3405 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
3406 status = BTMTK_WMT_ON_DONE;
3407 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
3408 status = BTMTK_WMT_ON_PROGRESS;
3410 status = BTMTK_WMT_ON_UNDONE;
3412 case BTMTK_WMT_PATCH_DWNLD:
3413 if (wmt_evt->whdr.flag == 2)
3414 status = BTMTK_WMT_PATCH_DONE;
3415 else if (wmt_evt->whdr.flag == 1)
3416 status = BTMTK_WMT_PATCH_PROGRESS;
3418 status = BTMTK_WMT_PATCH_UNDONE;
3422 if (wmt_params->status)
3423 *wmt_params->status = status;
3426 kfree_skb(data->evt_skb);
3427 data->evt_skb = NULL;
3433 static int btusb_mtk_setup_firmware_79xx(struct hci_dev *hdev, const char *fwname)
3435 struct btmtk_hci_wmt_params wmt_params;
3436 struct btmtk_global_desc *globaldesc = NULL;
3437 struct btmtk_section_map *sectionmap;
3438 const struct firmware *fw;
3440 const u8 *fw_bin_ptr;
3441 int err, dlen, i, status;
3442 u8 flag, first_block, retry;
3443 u32 section_num, dl_size, section_offset;
3446 err = request_firmware(&fw, fwname, &hdev->dev);
3448 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
3453 fw_bin_ptr = fw_ptr;
3454 globaldesc = (struct btmtk_global_desc *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE);
3455 section_num = le32_to_cpu(globaldesc->section_num);
3457 for (i = 0; i < section_num; i++) {
3459 fw_ptr = fw_bin_ptr;
3460 sectionmap = (struct btmtk_section_map *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
3461 MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i);
3463 section_offset = le32_to_cpu(sectionmap->secoffset);
3464 dl_size = le32_to_cpu(sectionmap->bin_info_spec.dlsize);
3469 cmd[0] = 0; /* 0 means legacy dl mode. */
3471 fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
3472 MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i +
3473 MTK_SEC_MAP_COMMON_SIZE,
3474 MTK_SEC_MAP_NEED_SEND_SIZE + 1);
3476 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3477 wmt_params.status = &status;
3478 wmt_params.flag = 0;
3479 wmt_params.dlen = MTK_SEC_MAP_NEED_SEND_SIZE + 1;
3480 wmt_params.data = &cmd;
3482 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3484 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3486 goto err_release_fw;
3489 if (status == BTMTK_WMT_PATCH_UNDONE) {
3491 } else if (status == BTMTK_WMT_PATCH_PROGRESS) {
3494 } else if (status == BTMTK_WMT_PATCH_DONE) {
3497 bt_dev_err(hdev, "Failed wmt patch dwnld status (%d)",
3499 goto err_release_fw;
3503 fw_ptr += section_offset;
3504 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3505 wmt_params.status = NULL;
3507 while (dl_size > 0) {
3508 dlen = min_t(int, 250, dl_size);
3509 if (first_block == 1) {
3512 } else if (dl_size - dlen <= 0) {
3518 wmt_params.flag = flag;
3519 wmt_params.dlen = dlen;
3520 wmt_params.data = fw_ptr;
3522 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3524 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3526 goto err_release_fw;
3536 /* Wait a few moments for firmware activation done */
3537 usleep_range(100000, 120000);
3540 release_firmware(fw);
3545 static int btusb_mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
3547 struct btmtk_hci_wmt_params wmt_params;
3548 const struct firmware *fw;
3554 err = request_firmware(&fw, fwname, &hdev->dev);
3556 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
3560 /* Power on data RAM the firmware relies on. */
3562 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3563 wmt_params.flag = 3;
3564 wmt_params.dlen = sizeof(param);
3565 wmt_params.data = ¶m;
3566 wmt_params.status = NULL;
3568 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3570 bt_dev_err(hdev, "Failed to power on data RAM (%d)", err);
3571 goto err_release_fw;
3577 /* The size of patch header is 30 bytes, should be skip */
3580 goto err_release_fw;
3587 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3588 wmt_params.status = NULL;
3590 while (fw_size > 0) {
3591 dlen = min_t(int, 250, fw_size);
3593 /* Tell device the position in sequence */
3594 if (fw_size - dlen <= 0)
3596 else if (fw_size < fw->size - 30)
3599 wmt_params.flag = flag;
3600 wmt_params.dlen = dlen;
3601 wmt_params.data = fw_ptr;
3603 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3605 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3607 goto err_release_fw;
3614 wmt_params.op = BTMTK_WMT_RST;
3615 wmt_params.flag = 4;
3616 wmt_params.dlen = 0;
3617 wmt_params.data = NULL;
3618 wmt_params.status = NULL;
3620 /* Activate funciton the firmware providing to */
3621 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3623 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
3624 goto err_release_fw;
3627 /* Wait a few moments for firmware activation done */
3628 usleep_range(10000, 12000);
3631 release_firmware(fw);
3636 static int btusb_mtk_func_query(struct hci_dev *hdev)
3638 struct btmtk_hci_wmt_params wmt_params;
3642 /* Query whether the function is enabled */
3643 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3644 wmt_params.flag = 4;
3645 wmt_params.dlen = sizeof(param);
3646 wmt_params.data = ¶m;
3647 wmt_params.status = &status;
3649 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3651 bt_dev_err(hdev, "Failed to query function status (%d)", err);
3658 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
3660 int pipe, err, size = sizeof(u32);
3663 buf = kzalloc(size, GFP_KERNEL);
3667 pipe = usb_rcvctrlpipe(data->udev, 0);
3668 err = usb_control_msg(data->udev, pipe, 0x63,
3669 USB_TYPE_VENDOR | USB_DIR_IN,
3670 reg >> 16, reg & 0xffff,
3671 buf, size, USB_CTRL_SET_TIMEOUT);
3675 *val = get_unaligned_le32(buf);
3683 static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
3685 return btusb_mtk_reg_read(data, reg, id);
3688 static int btusb_mtk_setup(struct hci_dev *hdev)
3690 struct btusb_data *data = hci_get_drvdata(hdev);
3691 struct btmtk_hci_wmt_params wmt_params;
3692 ktime_t calltime, delta, rettime;
3693 struct btmtk_tci_sleep tci_sleep;
3694 unsigned long long duration;
3695 struct sk_buff *skb;
3699 char fw_bin_name[64];
3703 calltime = ktime_get();
3705 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
3707 bt_dev_err(hdev, "Failed to get device id (%d)", err);
3712 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
3714 bt_dev_err(hdev, "Failed to get device id (%d)", err);
3717 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
3719 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
3726 fwname = FIRMWARE_MT7663;
3729 fwname = FIRMWARE_MT7668;
3732 snprintf(fw_bin_name, sizeof(fw_bin_name),
3733 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
3734 dev_id & 0xffff, (fw_version & 0xff) + 1);
3735 err = btusb_mtk_setup_firmware_79xx(hdev, fw_bin_name);
3737 /* Enable Bluetooth protocol */
3739 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3740 wmt_params.flag = 0;
3741 wmt_params.dlen = sizeof(param);
3742 wmt_params.data = ¶m;
3743 wmt_params.status = NULL;
3745 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3747 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3752 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
3757 /* Query whether the firmware is already download */
3758 wmt_params.op = BTMTK_WMT_SEMAPHORE;
3759 wmt_params.flag = 1;
3760 wmt_params.dlen = 0;
3761 wmt_params.data = NULL;
3762 wmt_params.status = &status;
3764 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3766 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
3770 if (status == BTMTK_WMT_PATCH_DONE) {
3771 bt_dev_info(hdev, "firmware already downloaded");
3772 goto ignore_setup_fw;
3775 /* Setup a firmware which the device definitely requires */
3776 err = btusb_mtk_setup_firmware(hdev, fwname);
3781 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
3782 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
3784 /* -ETIMEDOUT happens */
3788 /* The other errors happen in btusb_mtk_func_query */
3792 if (status == BTMTK_WMT_ON_DONE) {
3793 bt_dev_info(hdev, "function already on");
3794 goto ignore_func_on;
3797 /* Enable Bluetooth protocol */
3799 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3800 wmt_params.flag = 0;
3801 wmt_params.dlen = sizeof(param);
3802 wmt_params.data = ¶m;
3803 wmt_params.status = NULL;
3805 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3807 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3812 /* Apply the low power environment setup */
3813 tci_sleep.mode = 0x5;
3814 tci_sleep.duration = cpu_to_le16(0x640);
3815 tci_sleep.host_duration = cpu_to_le16(0x640);
3816 tci_sleep.host_wakeup_pin = 0;
3817 tci_sleep.time_compensation = 0;
3819 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
3823 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
3829 rettime = ktime_get();
3830 delta = ktime_sub(rettime, calltime);
3831 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3833 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
3838 static int btusb_mtk_shutdown(struct hci_dev *hdev)
3840 struct btmtk_hci_wmt_params wmt_params;
3844 /* Disable the device */
3845 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3846 wmt_params.flag = 0;
3847 wmt_params.dlen = sizeof(param);
3848 wmt_params.data = ¶m;
3849 wmt_params.status = NULL;
3851 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3853 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3860 MODULE_FIRMWARE(FIRMWARE_MT7663);
3861 MODULE_FIRMWARE(FIRMWARE_MT7668);
3864 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3865 static int marvell_config_oob_wake(struct hci_dev *hdev)
3867 struct sk_buff *skb;
3868 struct btusb_data *data = hci_get_drvdata(hdev);
3869 struct device *dev = &data->udev->dev;
3870 u16 pin, gap, opcode;
3874 /* Move on if no wakeup pin specified */
3875 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3876 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3879 /* Vendor specific command to configure a GPIO as wake-up pin */
3880 opcode = hci_opcode_pack(0x3F, 0x59);
3881 cmd[0] = opcode & 0xFF;
3882 cmd[1] = opcode >> 8;
3883 cmd[2] = 2; /* length of parameters that follow */
3885 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3887 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3889 bt_dev_err(hdev, "%s: No memory", __func__);
3893 skb_put_data(skb, cmd, sizeof(cmd));
3894 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3896 ret = btusb_send_frame(hdev, skb);
3898 bt_dev_err(hdev, "%s: configuration failed", __func__);
3907 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3908 const bdaddr_t *bdaddr)
3910 struct sk_buff *skb;
3915 buf[1] = sizeof(bdaddr_t);
3916 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3918 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3921 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3930 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3931 const bdaddr_t *bdaddr)
3933 struct sk_buff *skb;
3940 buf[3] = sizeof(bdaddr_t);
3941 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3943 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3946 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3954 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3955 const bdaddr_t *bdaddr)
3957 struct sk_buff *skb;
3961 memcpy(buf, bdaddr, sizeof(bdaddr_t));
3963 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3964 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3967 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3975 #define QCA_DFU_PACKET_LEN 4096
3977 #define QCA_GET_TARGET_VERSION 0x09
3978 #define QCA_CHECK_STATUS 0x05
3979 #define QCA_DFU_DOWNLOAD 0x01
3981 #define QCA_SYSCFG_UPDATED 0x40
3982 #define QCA_PATCH_UPDATED 0x80
3983 #define QCA_DFU_TIMEOUT 3000
3984 #define QCA_FLAG_MULTI_NVM 0x80
3986 struct qca_version {
3988 __le32 patch_version;
3995 struct qca_rampatch_version {
3996 __le16 rom_version_high;
3997 __le16 rom_version_low;
3998 __le16 patch_version;
4001 struct qca_device_info {
4003 u8 rampatch_hdr; /* length of header in rampatch */
4004 u8 nvm_hdr; /* length of header in NVM */
4005 u8 ver_offset; /* offset of version structure in rampatch */
4008 static const struct qca_device_info qca_devices_table[] = {
4009 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
4010 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
4011 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
4012 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
4013 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
4014 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
4015 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
4016 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
4019 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
4020 void *data, u16 size)
4025 buf = kmalloc(size, GFP_KERNEL);
4029 /* Found some of USB hosts have IOT issues with ours so that we should
4030 * not wait until HCI layer is ready.
4032 pipe = usb_rcvctrlpipe(udev, 0);
4033 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
4034 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
4036 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
4040 memcpy(data, buf, size);
4048 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
4049 const struct firmware *firmware,
4052 struct btusb_data *btdata = hci_get_drvdata(hdev);
4053 struct usb_device *udev = btdata->udev;
4054 size_t count, size, sent = 0;
4058 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
4062 count = firmware->size;
4064 size = min_t(size_t, count, hdr_size);
4065 memcpy(buf, firmware->data, size);
4067 /* USB patches should go down to controller through USB path
4068 * because binary format fits to go down through USB channel.
4069 * USB control path is for patching headers and USB bulk is for
4072 pipe = usb_sndctrlpipe(udev, 0);
4073 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
4074 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
4076 bt_dev_err(hdev, "Failed to send headers (%d)", err);
4083 /* ep2 need time to switch from function acl to function dfu,
4084 * so we add 20ms delay here.
4089 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
4091 memcpy(buf, firmware->data + sent, size);
4093 pipe = usb_sndbulkpipe(udev, 0x02);
4094 err = usb_bulk_msg(udev, pipe, buf, size, &len,
4097 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
4098 sent, firmware->size, err);
4103 bt_dev_err(hdev, "Failed to get bulk buffer");
4117 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
4118 struct qca_version *ver,
4119 const struct qca_device_info *info)
4121 struct qca_rampatch_version *rver;
4122 const struct firmware *fw;
4123 u32 ver_rom, ver_patch, rver_rom;
4124 u16 rver_rom_low, rver_rom_high, rver_patch;
4128 ver_rom = le32_to_cpu(ver->rom_version);
4129 ver_patch = le32_to_cpu(ver->patch_version);
4131 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
4133 err = request_firmware(&fw, fwname, &hdev->dev);
4135 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
4140 bt_dev_info(hdev, "using rampatch file: %s", fwname);
4142 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
4143 rver_rom_low = le16_to_cpu(rver->rom_version_low);
4144 rver_patch = le16_to_cpu(rver->patch_version);
4146 if (ver_rom & ~0xffffU) {
4147 rver_rom_high = le16_to_cpu(rver->rom_version_high);
4148 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
4150 rver_rom = rver_rom_low;
4153 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
4154 "firmware rome 0x%x build 0x%x",
4155 rver_rom, rver_patch, ver_rom, ver_patch);
4157 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
4158 bt_dev_err(hdev, "rampatch file version did not match with firmware");
4163 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
4166 release_firmware(fw);
4171 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
4172 struct qca_version *ver,
4173 const struct qca_device_info *info)
4175 const struct firmware *fw;
4179 if (((ver->flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
4180 /* if boardid equal 0, use default nvm without surfix */
4181 if (le16_to_cpu(ver->board_id) == 0x0) {
4182 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
4183 le32_to_cpu(ver->rom_version));
4185 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x_%04x.bin",
4186 le32_to_cpu(ver->rom_version),
4187 le16_to_cpu(ver->board_id));
4190 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
4191 le32_to_cpu(ver->rom_version));
4194 err = request_firmware(&fw, fwname, &hdev->dev);
4196 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
4201 bt_dev_info(hdev, "using NVM file: %s", fwname);
4203 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
4205 release_firmware(fw);
4210 /* identify the ROM version and check whether patches are needed */
4211 static bool btusb_qca_need_patch(struct usb_device *udev)
4213 struct qca_version ver;
4215 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
4218 /* only low ROM versions need patches */
4219 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
4222 static int btusb_setup_qca(struct hci_dev *hdev)
4224 struct btusb_data *btdata = hci_get_drvdata(hdev);
4225 struct usb_device *udev = btdata->udev;
4226 const struct qca_device_info *info = NULL;
4227 struct qca_version ver;
4232 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
4237 ver_rom = le32_to_cpu(ver.rom_version);
4239 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
4240 if (ver_rom == qca_devices_table[i].rom_version)
4241 info = &qca_devices_table[i];
4244 /* If the rom_version is not matched in the qca_devices_table
4245 * and the high ROM version is not zero, we assume this chip no
4246 * need to load the rampatch and nvm.
4248 if (ver_rom & ~0xffffU)
4251 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
4255 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
4260 if (!(status & QCA_PATCH_UPDATED)) {
4261 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
4266 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
4271 if (!(status & QCA_SYSCFG_UPDATED)) {
4272 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
4280 static inline int __set_diag_interface(struct hci_dev *hdev)
4282 struct btusb_data *data = hci_get_drvdata(hdev);
4283 struct usb_interface *intf = data->diag;
4289 data->diag_tx_ep = NULL;
4290 data->diag_rx_ep = NULL;
4292 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4293 struct usb_endpoint_descriptor *ep_desc;
4295 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4297 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4298 data->diag_tx_ep = ep_desc;
4302 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4303 data->diag_rx_ep = ep_desc;
4308 if (!data->diag_tx_ep || !data->diag_rx_ep) {
4309 bt_dev_err(hdev, "invalid diagnostic descriptors");
4316 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
4318 struct btusb_data *data = hci_get_drvdata(hdev);
4319 struct sk_buff *skb;
4323 if (!data->diag_tx_ep)
4324 return ERR_PTR(-ENODEV);
4326 urb = usb_alloc_urb(0, GFP_KERNEL);
4328 return ERR_PTR(-ENOMEM);
4330 skb = bt_skb_alloc(2, GFP_KERNEL);
4333 return ERR_PTR(-ENOMEM);
4336 skb_put_u8(skb, 0xf0);
4337 skb_put_u8(skb, enable);
4339 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
4341 usb_fill_bulk_urb(urb, data->udev, pipe,
4342 skb->data, skb->len, btusb_tx_complete, skb);
4344 skb->dev = (void *)hdev;
4349 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
4351 struct btusb_data *data = hci_get_drvdata(hdev);
4357 if (!test_bit(HCI_RUNNING, &hdev->flags))
4360 urb = alloc_diag_urb(hdev, enable);
4362 return PTR_ERR(urb);
4364 return submit_or_queue_tx_urb(hdev, urb);
4368 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
4370 struct btusb_data *data = priv;
4372 pm_wakeup_event(&data->udev->dev, 0);
4375 /* Disable only if not already disabled (keep it balanced) */
4376 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4377 disable_irq_nosync(irq);
4378 disable_irq_wake(irq);
4383 static const struct of_device_id btusb_match_table[] = {
4384 { .compatible = "usb1286,204e" },
4385 { .compatible = "usbcf3,e300" }, /* QCA6174A */
4386 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
4389 MODULE_DEVICE_TABLE(of, btusb_match_table);
4391 /* Use an oob wakeup pin? */
4392 static int btusb_config_oob_wake(struct hci_dev *hdev)
4394 struct btusb_data *data = hci_get_drvdata(hdev);
4395 struct device *dev = &data->udev->dev;
4398 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4400 if (!of_match_device(btusb_match_table, dev))
4403 /* Move on if no IRQ specified */
4404 irq = of_irq_get_byname(dev->of_node, "wakeup");
4406 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
4410 irq_set_status_flags(irq, IRQ_NOAUTOEN);
4411 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
4412 0, "OOB Wake-on-BT", data);
4414 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
4418 ret = device_init_wakeup(dev, true);
4420 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
4424 data->oob_wake_irq = irq;
4425 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
4430 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
4432 if (dmi_check_system(btusb_needs_reset_resume_table))
4433 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
4436 static bool btusb_prevent_wake(struct hci_dev *hdev)
4438 struct btusb_data *data = hci_get_drvdata(hdev);
4440 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
4443 return !device_may_wakeup(&data->udev->dev);
4446 static int btusb_shutdown_qca(struct hci_dev *hdev)
4448 struct sk_buff *skb;
4450 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
4452 bt_dev_err(hdev, "HCI reset during shutdown failed");
4453 return PTR_ERR(skb);
4460 static int btusb_probe(struct usb_interface *intf,
4461 const struct usb_device_id *id)
4463 struct usb_endpoint_descriptor *ep_desc;
4464 struct gpio_desc *reset_gpio;
4465 struct btusb_data *data;
4466 struct hci_dev *hdev;
4467 unsigned ifnum_base;
4470 BT_DBG("intf %p id %p", intf, id);
4472 /* interface numbers are hardcoded in the spec */
4473 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
4474 if (!(id->driver_info & BTUSB_IFNUM_2))
4476 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
4480 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
4482 if (!id->driver_info) {
4483 const struct usb_device_id *match;
4485 match = usb_match_id(intf, blacklist_table);
4490 if (id->driver_info == BTUSB_IGNORE)
4493 if (id->driver_info & BTUSB_ATH3012) {
4494 struct usb_device *udev = interface_to_usbdev(intf);
4496 /* Old firmware would otherwise let ath3k driver load
4497 * patch and sysconfig files
4499 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
4500 !btusb_qca_need_patch(udev))
4504 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
4508 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4509 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4511 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
4512 data->intr_ep = ep_desc;
4516 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4517 data->bulk_tx_ep = ep_desc;
4521 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4522 data->bulk_rx_ep = ep_desc;
4527 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
4530 if (id->driver_info & BTUSB_AMP) {
4531 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
4532 data->cmdreq = 0x2b;
4534 data->cmdreq_type = USB_TYPE_CLASS;
4535 data->cmdreq = 0x00;
4538 data->udev = interface_to_usbdev(intf);
4541 INIT_WORK(&data->work, btusb_work);
4542 INIT_WORK(&data->waker, btusb_waker);
4543 init_usb_anchor(&data->deferred);
4544 init_usb_anchor(&data->tx_anchor);
4545 spin_lock_init(&data->txlock);
4547 init_usb_anchor(&data->intr_anchor);
4548 init_usb_anchor(&data->bulk_anchor);
4549 init_usb_anchor(&data->isoc_anchor);
4550 init_usb_anchor(&data->diag_anchor);
4551 init_usb_anchor(&data->ctrl_anchor);
4552 spin_lock_init(&data->rxlock);
4554 if (id->driver_info & BTUSB_INTEL_NEW) {
4555 data->recv_event = btusb_recv_event_intel;
4556 data->recv_bulk = btusb_recv_bulk_intel;
4557 set_bit(BTUSB_BOOTLOADER, &data->flags);
4559 data->recv_event = hci_recv_frame;
4560 data->recv_bulk = btusb_recv_bulk;
4563 hdev = hci_alloc_dev();
4567 hdev->bus = HCI_USB;
4568 hci_set_drvdata(hdev, data);
4570 if (id->driver_info & BTUSB_AMP)
4571 hdev->dev_type = HCI_AMP;
4573 hdev->dev_type = HCI_PRIMARY;
4577 SET_HCIDEV_DEV(hdev, &intf->dev);
4579 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
4581 if (IS_ERR(reset_gpio)) {
4582 err = PTR_ERR(reset_gpio);
4584 } else if (reset_gpio) {
4585 data->reset_gpio = reset_gpio;
4588 hdev->open = btusb_open;
4589 hdev->close = btusb_close;
4590 hdev->flush = btusb_flush;
4591 hdev->send = btusb_send_frame;
4592 hdev->notify = btusb_notify;
4593 hdev->prevent_wake = btusb_prevent_wake;
4596 err = btusb_config_oob_wake(hdev);
4600 /* Marvell devices may need a specific chip configuration */
4601 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
4602 err = marvell_config_oob_wake(hdev);
4607 if (id->driver_info & BTUSB_CW6622)
4608 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4610 if (id->driver_info & BTUSB_BCM2045)
4611 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4613 if (id->driver_info & BTUSB_BCM92035)
4614 hdev->setup = btusb_setup_bcm92035;
4616 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4617 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
4618 hdev->manufacturer = 15;
4619 hdev->setup = btbcm_setup_patchram;
4620 hdev->set_diag = btusb_bcm_set_diag;
4621 hdev->set_bdaddr = btbcm_set_bdaddr;
4623 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4624 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4627 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4628 (id->driver_info & BTUSB_BCM_APPLE)) {
4629 hdev->manufacturer = 15;
4630 hdev->setup = btbcm_setup_apple;
4631 hdev->set_diag = btusb_bcm_set_diag;
4633 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4634 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4637 if (id->driver_info & BTUSB_INTEL) {
4638 hdev->manufacturer = 2;
4639 hdev->setup = btusb_setup_intel;
4640 hdev->shutdown = btusb_shutdown_intel;
4641 hdev->set_diag = btintel_set_diag_mfg;
4642 hdev->set_bdaddr = btintel_set_bdaddr;
4643 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4644 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4645 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4646 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4649 if (id->driver_info & BTUSB_INTEL_NEW) {
4650 hdev->manufacturer = 2;
4651 hdev->send = btusb_send_frame_intel;
4652 hdev->setup = btusb_setup_intel_new;
4653 hdev->shutdown = btusb_shutdown_intel_new;
4654 hdev->hw_error = btintel_hw_error;
4655 hdev->set_diag = btintel_set_diag;
4656 hdev->set_bdaddr = btintel_set_bdaddr;
4657 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4658 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4659 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4660 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4663 if (id->driver_info & BTUSB_INTEL_NEWGEN) {
4664 hdev->manufacturer = 2;
4665 hdev->send = btusb_send_frame_intel;
4666 hdev->setup = btusb_setup_intel_newgen;
4667 hdev->shutdown = btusb_shutdown_intel_new;
4668 hdev->hw_error = btintel_hw_error;
4669 hdev->set_diag = btintel_set_diag;
4670 hdev->set_bdaddr = btintel_set_bdaddr;
4671 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4672 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4673 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4674 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4676 data->recv_event = btusb_recv_event_intel;
4677 data->recv_bulk = btusb_recv_bulk_intel;
4678 set_bit(BTUSB_BOOTLOADER, &data->flags);
4681 if (id->driver_info & BTUSB_MARVELL)
4682 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4684 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4685 (id->driver_info & BTUSB_MEDIATEK)) {
4686 hdev->setup = btusb_mtk_setup;
4687 hdev->shutdown = btusb_mtk_shutdown;
4688 hdev->manufacturer = 70;
4689 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
4692 if (id->driver_info & BTUSB_SWAVE) {
4693 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
4694 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
4697 if (id->driver_info & BTUSB_INTEL_BOOT) {
4698 hdev->manufacturer = 2;
4699 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4702 if (id->driver_info & BTUSB_ATH3012) {
4703 data->setup_on_usb = btusb_setup_qca;
4704 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4705 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4706 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4709 if (id->driver_info & BTUSB_QCA_ROME) {
4710 data->setup_on_usb = btusb_setup_qca;
4711 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4712 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4713 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4714 btusb_check_needs_reset_resume(intf);
4717 if (id->driver_info & BTUSB_QCA_WCN6855) {
4718 data->setup_on_usb = btusb_setup_qca;
4719 hdev->shutdown = btusb_shutdown_qca;
4720 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
4721 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4722 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4725 if (id->driver_info & BTUSB_AMP) {
4726 /* AMP controllers do not support SCO packets */
4729 /* Interface orders are hardcoded in the specification */
4730 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
4731 data->isoc_ifnum = ifnum_base + 1;
4734 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4735 (id->driver_info & BTUSB_REALTEK)) {
4736 hdev->setup = btrtl_setup_realtek;
4737 hdev->shutdown = btrtl_shutdown_realtek;
4738 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
4740 /* Realtek devices lose their updated firmware over global
4741 * suspend that means host doesn't send SET_FEATURE
4742 * (DEVICE_REMOTE_WAKEUP)
4744 set_bit(BTUSB_WAKEUP_DISABLE, &data->flags);
4748 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4750 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
4751 if (!disable_scofix)
4752 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
4755 if (id->driver_info & BTUSB_BROKEN_ISOC)
4758 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
4759 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
4761 if (id->driver_info & BTUSB_VALID_LE_STATES)
4762 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
4764 if (id->driver_info & BTUSB_DIGIANSWER) {
4765 data->cmdreq_type = USB_TYPE_VENDOR;
4766 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4769 if (id->driver_info & BTUSB_CSR) {
4770 struct usb_device *udev = data->udev;
4771 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
4773 /* Old firmware would otherwise execute USB reset */
4774 if (bcdDevice < 0x117)
4775 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4777 /* This must be set first in case we disable it for fakes */
4778 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4780 /* Fake CSR devices with broken commands */
4781 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
4782 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4783 hdev->setup = btusb_setup_csr;
4786 if (id->driver_info & BTUSB_SNIFFER) {
4787 struct usb_device *udev = data->udev;
4789 /* New sniffer firmware has crippled HCI interface */
4790 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4791 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4794 if (id->driver_info & BTUSB_INTEL_BOOT) {
4795 /* A bug in the bootloader causes that interrupt interface is
4796 * only enabled after receiving SetInterface(0, AltSetting=0).
4798 err = usb_set_interface(data->udev, 0, 0);
4800 BT_ERR("failed to set interface 0, alt 0 %d", err);
4806 err = usb_driver_claim_interface(&btusb_driver,
4812 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
4813 if (!usb_driver_claim_interface(&btusb_driver,
4815 __set_diag_interface(hdev);
4820 if (enable_autosuspend)
4821 usb_enable_autosuspend(data->udev);
4823 err = hci_register_dev(hdev);
4827 usb_set_intfdata(intf, data);
4832 if (data->reset_gpio)
4833 gpiod_put(data->reset_gpio);
4838 static void btusb_disconnect(struct usb_interface *intf)
4840 struct btusb_data *data = usb_get_intfdata(intf);
4841 struct hci_dev *hdev;
4843 BT_DBG("intf %p", intf);
4849 usb_set_intfdata(data->intf, NULL);
4852 usb_set_intfdata(data->isoc, NULL);
4855 usb_set_intfdata(data->diag, NULL);
4857 hci_unregister_dev(hdev);
4859 if (intf == data->intf) {
4861 usb_driver_release_interface(&btusb_driver, data->isoc);
4863 usb_driver_release_interface(&btusb_driver, data->diag);
4864 } else if (intf == data->isoc) {
4866 usb_driver_release_interface(&btusb_driver, data->diag);
4867 usb_driver_release_interface(&btusb_driver, data->intf);
4868 } else if (intf == data->diag) {
4869 usb_driver_release_interface(&btusb_driver, data->intf);
4871 usb_driver_release_interface(&btusb_driver, data->isoc);
4874 if (data->oob_wake_irq)
4875 device_init_wakeup(&data->udev->dev, false);
4877 if (data->reset_gpio)
4878 gpiod_put(data->reset_gpio);
4884 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4886 struct btusb_data *data = usb_get_intfdata(intf);
4888 BT_DBG("intf %p", intf);
4890 if (data->suspend_count++)
4893 spin_lock_irq(&data->txlock);
4894 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4895 set_bit(BTUSB_SUSPENDING, &data->flags);
4896 spin_unlock_irq(&data->txlock);
4898 spin_unlock_irq(&data->txlock);
4899 data->suspend_count--;
4903 cancel_work_sync(&data->work);
4905 btusb_stop_traffic(data);
4906 usb_kill_anchored_urbs(&data->tx_anchor);
4908 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4909 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4910 enable_irq_wake(data->oob_wake_irq);
4911 enable_irq(data->oob_wake_irq);
4914 /* For global suspend, Realtek devices lose the loaded fw
4915 * in them. But for autosuspend, firmware should remain.
4916 * Actually, it depends on whether the usb host sends
4917 * set feature (enable wakeup) or not.
4919 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags)) {
4920 if (PMSG_IS_AUTO(message) &&
4921 device_can_wakeup(&data->udev->dev))
4922 data->udev->do_remote_wakeup = 1;
4923 else if (!PMSG_IS_AUTO(message))
4924 data->udev->reset_resume = 1;
4930 static void play_deferred(struct btusb_data *data)
4935 while ((urb = usb_get_from_anchor(&data->deferred))) {
4936 usb_anchor_urb(urb, &data->tx_anchor);
4938 err = usb_submit_urb(urb, GFP_ATOMIC);
4940 if (err != -EPERM && err != -ENODEV)
4941 BT_ERR("%s urb %p submission failed (%d)",
4942 data->hdev->name, urb, -err);
4943 kfree(urb->setup_packet);
4944 usb_unanchor_urb(urb);
4949 data->tx_in_flight++;
4953 /* Cleanup the rest deferred urbs. */
4954 while ((urb = usb_get_from_anchor(&data->deferred))) {
4955 kfree(urb->setup_packet);
4960 static int btusb_resume(struct usb_interface *intf)
4962 struct btusb_data *data = usb_get_intfdata(intf);
4963 struct hci_dev *hdev = data->hdev;
4966 BT_DBG("intf %p", intf);
4968 if (--data->suspend_count)
4971 /* Disable only if not already disabled (keep it balanced) */
4972 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4973 disable_irq(data->oob_wake_irq);
4974 disable_irq_wake(data->oob_wake_irq);
4977 if (!test_bit(HCI_RUNNING, &hdev->flags))
4980 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4981 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4983 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4988 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4989 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4991 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4995 btusb_submit_bulk_urb(hdev, GFP_NOIO);
4998 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4999 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
5000 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
5002 btusb_submit_isoc_urb(hdev, GFP_NOIO);
5005 spin_lock_irq(&data->txlock);
5006 play_deferred(data);
5007 clear_bit(BTUSB_SUSPENDING, &data->flags);
5008 spin_unlock_irq(&data->txlock);
5009 schedule_work(&data->work);
5014 usb_scuttle_anchored_urbs(&data->deferred);
5016 spin_lock_irq(&data->txlock);
5017 clear_bit(BTUSB_SUSPENDING, &data->flags);
5018 spin_unlock_irq(&data->txlock);
5024 static struct usb_driver btusb_driver = {
5026 .probe = btusb_probe,
5027 .disconnect = btusb_disconnect,
5029 .suspend = btusb_suspend,
5030 .resume = btusb_resume,
5032 .id_table = btusb_table,
5033 .supports_autosuspend = 1,
5034 .disable_hub_initiated_lpm = 1,
5037 module_usb_driver(btusb_driver);
5039 module_param(disable_scofix, bool, 0644);
5040 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
5042 module_param(force_scofix, bool, 0644);
5043 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
5045 module_param(enable_autosuspend, bool, 0644);
5046 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
5048 module_param(reset, bool, 0644);
5049 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
5052 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
5053 MODULE_VERSION(VERSION);
5054 MODULE_LICENSE("GPL");