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
64 static const struct usb_device_id btusb_table[] = {
65 /* Generic Bluetooth USB device */
66 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
68 /* Generic Bluetooth AMP device */
69 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
71 /* Generic Bluetooth USB interface */
72 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
74 /* Apple-specific (Broadcom) devices */
75 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
76 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
78 /* MediaTek MT76x0E */
79 { USB_DEVICE(0x0e8d, 0x763f) },
81 /* Broadcom SoftSailing reporting vendor specific */
82 { USB_DEVICE(0x0a5c, 0x21e1) },
84 /* Apple MacBookPro 7,1 */
85 { USB_DEVICE(0x05ac, 0x8213) },
88 { USB_DEVICE(0x05ac, 0x8215) },
90 /* Apple MacBookPro6,2 */
91 { USB_DEVICE(0x05ac, 0x8218) },
93 /* Apple MacBookAir3,1, MacBookAir3,2 */
94 { USB_DEVICE(0x05ac, 0x821b) },
96 /* Apple MacBookAir4,1 */
97 { USB_DEVICE(0x05ac, 0x821f) },
99 /* Apple MacBookPro8,2 */
100 { USB_DEVICE(0x05ac, 0x821a) },
102 /* Apple MacMini5,1 */
103 { USB_DEVICE(0x05ac, 0x8281) },
105 /* AVM BlueFRITZ! USB v2.0 */
106 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
108 /* Bluetooth Ultraport Module from IBM */
109 { USB_DEVICE(0x04bf, 0x030a) },
111 /* ALPS Modules with non-standard id */
112 { USB_DEVICE(0x044e, 0x3001) },
113 { USB_DEVICE(0x044e, 0x3002) },
115 /* Ericsson with non-standard id */
116 { USB_DEVICE(0x0bdb, 0x1002) },
118 /* Canyon CN-BTU1 with HID interfaces */
119 { USB_DEVICE(0x0c10, 0x0000) },
121 /* Broadcom BCM20702A0 */
122 { USB_DEVICE(0x413c, 0x8197) },
124 /* Broadcom BCM20702B0 (Dynex/Insignia) */
125 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
127 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
128 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
129 .driver_info = BTUSB_BCM_PATCHRAM },
131 /* Broadcom BCM920703 (HTC Vive) */
132 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
133 .driver_info = BTUSB_BCM_PATCHRAM },
135 /* Foxconn - Hon Hai */
136 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
137 .driver_info = BTUSB_BCM_PATCHRAM },
139 /* Lite-On Technology - Broadcom based */
140 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
141 .driver_info = BTUSB_BCM_PATCHRAM },
143 /* Broadcom devices with vendor specific id */
144 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
145 .driver_info = BTUSB_BCM_PATCHRAM },
147 /* ASUSTek Computer - Broadcom based */
148 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
149 .driver_info = BTUSB_BCM_PATCHRAM },
151 /* Belkin F8065bf - Broadcom based */
152 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
153 .driver_info = BTUSB_BCM_PATCHRAM },
155 /* IMC Networks - Broadcom based */
156 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
157 .driver_info = BTUSB_BCM_PATCHRAM },
159 /* Dell Computer - Broadcom based */
160 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
161 .driver_info = BTUSB_BCM_PATCHRAM },
163 /* Toshiba Corp - Broadcom based */
164 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
165 .driver_info = BTUSB_BCM_PATCHRAM },
167 /* Intel Bluetooth USB Bootloader (RAM module) */
168 { USB_DEVICE(0x8087, 0x0a5a),
169 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
171 { } /* Terminating entry */
174 MODULE_DEVICE_TABLE(usb, btusb_table);
176 static const struct usb_device_id blacklist_table[] = {
177 /* CSR BlueCore devices */
178 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
180 /* Broadcom BCM2033 without firmware */
181 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
183 /* Broadcom BCM2045 devices */
184 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
186 /* Atheros 3011 with sflash firmware */
187 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
188 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
189 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
190 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
191 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
192 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
193 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
195 /* Atheros AR9285 Malbec with sflash firmware */
196 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
198 /* Atheros 3012 with sflash firmware */
199 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
200 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
246 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
247 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
248 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
250 /* Atheros AR5BBU12 with sflash firmware */
251 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
253 /* Atheros AR5BBU12 with sflash firmware */
254 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
255 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
257 /* QCA ROME chipset */
258 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
259 BTUSB_WIDEBAND_SPEECH },
260 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
261 BTUSB_WIDEBAND_SPEECH },
262 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
263 BTUSB_WIDEBAND_SPEECH },
264 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
265 BTUSB_WIDEBAND_SPEECH },
266 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
267 BTUSB_WIDEBAND_SPEECH },
268 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
269 BTUSB_WIDEBAND_SPEECH },
270 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
271 BTUSB_WIDEBAND_SPEECH },
272 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
273 BTUSB_WIDEBAND_SPEECH },
274 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
275 BTUSB_WIDEBAND_SPEECH },
276 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
277 BTUSB_WIDEBAND_SPEECH },
278 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
279 BTUSB_WIDEBAND_SPEECH },
280 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
281 BTUSB_WIDEBAND_SPEECH },
282 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
283 BTUSB_WIDEBAND_SPEECH },
284 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
285 BTUSB_WIDEBAND_SPEECH },
286 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
287 BTUSB_WIDEBAND_SPEECH },
288 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
289 BTUSB_WIDEBAND_SPEECH },
290 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
291 BTUSB_WIDEBAND_SPEECH },
292 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
293 BTUSB_WIDEBAND_SPEECH },
295 /* QCA WCN6855 chipset */
296 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
297 BTUSB_WIDEBAND_SPEECH },
299 /* Broadcom BCM2035 */
300 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
301 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
302 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
304 /* Broadcom BCM2045 */
305 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
306 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
308 /* IBM/Lenovo ThinkPad with Broadcom chip */
309 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
310 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
312 /* HP laptop with Broadcom chip */
313 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
315 /* Dell laptop with Broadcom chip */
316 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
318 /* Dell Wireless 370 and 410 devices */
319 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
320 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
322 /* Belkin F8T012 and F8T013 devices */
323 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
324 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
326 /* Asus WL-BTD202 device */
327 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
329 /* Kensington Bluetooth USB adapter */
330 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
332 /* RTX Telecom based adapters with buggy SCO support */
333 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
334 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
336 /* CONWISE Technology based adapters with buggy SCO support */
337 { USB_DEVICE(0x0e5e, 0x6622),
338 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
340 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
341 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
343 /* Digianswer devices */
344 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
345 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
347 /* CSR BlueCore Bluetooth Sniffer */
348 { USB_DEVICE(0x0a12, 0x0002),
349 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
351 /* Frontline ComProbe Bluetooth Sniffer */
352 { USB_DEVICE(0x16d3, 0x0002),
353 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
355 /* Marvell Bluetooth devices */
356 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
357 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
358 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
360 /* Intel Bluetooth devices */
361 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW |
362 BTUSB_WIDEBAND_SPEECH |
363 BTUSB_VALID_LE_STATES },
364 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_NEW |
365 BTUSB_WIDEBAND_SPEECH },
366 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_NEW |
367 BTUSB_WIDEBAND_SPEECH },
368 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_NEW |
369 BTUSB_WIDEBAND_SPEECH},
370 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
371 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
372 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
373 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW |
374 BTUSB_WIDEBAND_SPEECH },
375 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL |
376 BTUSB_WIDEBAND_SPEECH },
377 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW |
378 BTUSB_WIDEBAND_SPEECH |
379 BTUSB_VALID_LE_STATES },
381 /* Other Intel Bluetooth devices */
382 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
383 .driver_info = BTUSB_IGNORE },
385 /* Realtek 8822CE Bluetooth devices */
386 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
387 BTUSB_WIDEBAND_SPEECH },
389 /* Realtek Bluetooth devices */
390 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
391 .driver_info = BTUSB_REALTEK },
393 /* MediaTek Bluetooth devices */
394 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
395 .driver_info = BTUSB_MEDIATEK },
397 /* Additional Realtek 8723AE Bluetooth devices */
398 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
399 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
401 /* Additional Realtek 8723BE Bluetooth devices */
402 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
403 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
404 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
405 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
406 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
407 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
409 /* Additional Realtek 8723BU Bluetooth devices */
410 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
412 /* Additional Realtek 8723DE Bluetooth devices */
413 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
414 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
416 /* Additional Realtek 8821AE Bluetooth devices */
417 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
418 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
419 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
420 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
421 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
423 /* Additional Realtek 8822BE Bluetooth devices */
424 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
425 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
427 /* Additional Realtek 8822CE Bluetooth devices */
428 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK },
429 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK },
431 /* Silicon Wave based devices */
432 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
434 { } /* Terminating entry */
437 /* The Bluetooth USB module build into some devices needs to be reset on resume,
438 * this is a problem with the platform (likely shutting off all power) not with
439 * the module itself. So we use a DMI list to match known broken platforms.
441 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
443 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
445 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
446 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
450 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
452 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
453 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
457 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
459 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
460 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
466 #define BTUSB_MAX_ISOC_FRAMES 10
468 #define BTUSB_INTR_RUNNING 0
469 #define BTUSB_BULK_RUNNING 1
470 #define BTUSB_ISOC_RUNNING 2
471 #define BTUSB_SUSPENDING 3
472 #define BTUSB_DID_ISO_RESUME 4
473 #define BTUSB_BOOTLOADER 5
474 #define BTUSB_DOWNLOADING 6
475 #define BTUSB_FIRMWARE_LOADED 7
476 #define BTUSB_FIRMWARE_FAILED 8
477 #define BTUSB_BOOTING 9
478 #define BTUSB_DIAG_RUNNING 10
479 #define BTUSB_OOB_WAKE_ENABLED 11
480 #define BTUSB_HW_RESET_ACTIVE 12
481 #define BTUSB_TX_WAIT_VND_EVT 13
482 #define BTUSB_WAKEUP_DISABLE 14
483 #define BTUSB_USE_ALT1_FOR_WBS 15
486 struct hci_dev *hdev;
487 struct usb_device *udev;
488 struct usb_interface *intf;
489 struct usb_interface *isoc;
490 struct usb_interface *diag;
495 struct work_struct work;
496 struct work_struct waker;
498 struct usb_anchor deferred;
499 struct usb_anchor tx_anchor;
503 struct usb_anchor intr_anchor;
504 struct usb_anchor bulk_anchor;
505 struct usb_anchor isoc_anchor;
506 struct usb_anchor diag_anchor;
507 struct usb_anchor ctrl_anchor;
510 struct sk_buff *evt_skb;
511 struct sk_buff *acl_skb;
512 struct sk_buff *sco_skb;
514 struct usb_endpoint_descriptor *intr_ep;
515 struct usb_endpoint_descriptor *bulk_tx_ep;
516 struct usb_endpoint_descriptor *bulk_rx_ep;
517 struct usb_endpoint_descriptor *isoc_tx_ep;
518 struct usb_endpoint_descriptor *isoc_rx_ep;
519 struct usb_endpoint_descriptor *diag_tx_ep;
520 struct usb_endpoint_descriptor *diag_rx_ep;
522 struct gpio_desc *reset_gpio;
527 unsigned int sco_num;
528 unsigned int air_mode;
529 bool usb_alt6_packet_flow;
533 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
534 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
536 int (*setup_on_usb)(struct hci_dev *hdev);
538 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
539 unsigned cmd_timeout_cnt;
542 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
544 struct btusb_data *data = hci_get_drvdata(hdev);
545 struct gpio_desc *reset_gpio = data->reset_gpio;
547 if (++data->cmd_timeout_cnt < 5)
551 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
556 * Toggle the hard reset line if the platform provides one. The reset
557 * is going to yank the device off the USB and then replug. So doing
558 * once is enough. The cleanup is handled correctly on the way out
559 * (standard USB disconnect), and the new device is detected cleanly
560 * and bound to the driver again like it should be.
562 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
563 bt_dev_err(hdev, "last reset failed? Not resetting again");
567 bt_dev_err(hdev, "Initiating HW reset via gpio");
568 gpiod_set_value_cansleep(reset_gpio, 1);
570 gpiod_set_value_cansleep(reset_gpio, 0);
573 static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
575 struct btusb_data *data = hci_get_drvdata(hdev);
576 struct gpio_desc *reset_gpio = data->reset_gpio;
578 if (++data->cmd_timeout_cnt < 5)
582 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
586 /* Toggle the hard reset line. The Realtek device is going to
587 * yank itself off the USB and then replug. The cleanup is handled
588 * correctly on the way out (standard USB disconnect), and the new
589 * device is detected cleanly and bound to the driver again like
592 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
593 bt_dev_err(hdev, "last reset failed? Not resetting again");
597 bt_dev_err(hdev, "Reset Realtek device via gpio");
598 gpiod_set_value_cansleep(reset_gpio, 1);
600 gpiod_set_value_cansleep(reset_gpio, 0);
603 static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
605 struct btusb_data *data = hci_get_drvdata(hdev);
608 if (++data->cmd_timeout_cnt < 5)
611 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
612 /* This is not an unbalanced PM reference since the device will reset */
613 err = usb_autopm_get_interface(data->intf);
615 usb_queue_reset_device(data->intf);
617 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
620 static inline void btusb_free_frags(struct btusb_data *data)
624 spin_lock_irqsave(&data->rxlock, flags);
626 kfree_skb(data->evt_skb);
627 data->evt_skb = NULL;
629 kfree_skb(data->acl_skb);
630 data->acl_skb = NULL;
632 kfree_skb(data->sco_skb);
633 data->sco_skb = NULL;
635 spin_unlock_irqrestore(&data->rxlock, flags);
638 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
644 spin_lock_irqsave(&data->rxlock, flags);
651 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
657 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
658 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
661 len = min_t(uint, hci_skb_expect(skb), count);
662 skb_put_data(skb, buffer, len);
666 hci_skb_expect(skb) -= len;
668 if (skb->len == HCI_EVENT_HDR_SIZE) {
669 /* Complete event header */
670 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
672 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
681 if (!hci_skb_expect(skb)) {
683 data->recv_event(data->hdev, skb);
689 spin_unlock_irqrestore(&data->rxlock, flags);
694 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
700 spin_lock_irqsave(&data->rxlock, flags);
707 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
713 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
714 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
717 len = min_t(uint, hci_skb_expect(skb), count);
718 skb_put_data(skb, buffer, len);
722 hci_skb_expect(skb) -= len;
724 if (skb->len == HCI_ACL_HDR_SIZE) {
725 __le16 dlen = hci_acl_hdr(skb)->dlen;
727 /* Complete ACL header */
728 hci_skb_expect(skb) = __le16_to_cpu(dlen);
730 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
739 if (!hci_skb_expect(skb)) {
741 hci_recv_frame(data->hdev, skb);
747 spin_unlock_irqrestore(&data->rxlock, flags);
752 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
758 spin_lock_irqsave(&data->rxlock, flags);
765 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
771 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
772 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
775 len = min_t(uint, hci_skb_expect(skb), count);
776 skb_put_data(skb, buffer, len);
780 hci_skb_expect(skb) -= len;
782 if (skb->len == HCI_SCO_HDR_SIZE) {
783 /* Complete SCO header */
784 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
786 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
795 if (!hci_skb_expect(skb)) {
797 hci_recv_frame(data->hdev, skb);
803 spin_unlock_irqrestore(&data->rxlock, flags);
808 static void btusb_intr_complete(struct urb *urb)
810 struct hci_dev *hdev = urb->context;
811 struct btusb_data *data = hci_get_drvdata(hdev);
814 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
817 if (!test_bit(HCI_RUNNING, &hdev->flags))
820 if (urb->status == 0) {
821 hdev->stat.byte_rx += urb->actual_length;
823 if (btusb_recv_intr(data, urb->transfer_buffer,
824 urb->actual_length) < 0) {
825 bt_dev_err(hdev, "corrupted event packet");
828 } else if (urb->status == -ENOENT) {
829 /* Avoid suspend failed when usb_kill_urb */
833 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
836 usb_mark_last_busy(data->udev);
837 usb_anchor_urb(urb, &data->intr_anchor);
839 err = usb_submit_urb(urb, GFP_ATOMIC);
841 /* -EPERM: urb is being killed;
842 * -ENODEV: device got disconnected
844 if (err != -EPERM && err != -ENODEV)
845 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
847 usb_unanchor_urb(urb);
851 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
853 struct btusb_data *data = hci_get_drvdata(hdev);
859 BT_DBG("%s", hdev->name);
864 urb = usb_alloc_urb(0, mem_flags);
868 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
870 buf = kmalloc(size, mem_flags);
876 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
878 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
879 btusb_intr_complete, hdev, data->intr_ep->bInterval);
881 urb->transfer_flags |= URB_FREE_BUFFER;
883 usb_anchor_urb(urb, &data->intr_anchor);
885 err = usb_submit_urb(urb, mem_flags);
887 if (err != -EPERM && err != -ENODEV)
888 bt_dev_err(hdev, "urb %p submission failed (%d)",
890 usb_unanchor_urb(urb);
898 static void btusb_bulk_complete(struct urb *urb)
900 struct hci_dev *hdev = urb->context;
901 struct btusb_data *data = hci_get_drvdata(hdev);
904 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
907 if (!test_bit(HCI_RUNNING, &hdev->flags))
910 if (urb->status == 0) {
911 hdev->stat.byte_rx += urb->actual_length;
913 if (data->recv_bulk(data, urb->transfer_buffer,
914 urb->actual_length) < 0) {
915 bt_dev_err(hdev, "corrupted ACL packet");
918 } else if (urb->status == -ENOENT) {
919 /* Avoid suspend failed when usb_kill_urb */
923 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
926 usb_anchor_urb(urb, &data->bulk_anchor);
927 usb_mark_last_busy(data->udev);
929 err = usb_submit_urb(urb, GFP_ATOMIC);
931 /* -EPERM: urb is being killed;
932 * -ENODEV: device got disconnected
934 if (err != -EPERM && err != -ENODEV)
935 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
937 usb_unanchor_urb(urb);
941 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
943 struct btusb_data *data = hci_get_drvdata(hdev);
947 int err, size = HCI_MAX_FRAME_SIZE;
949 BT_DBG("%s", hdev->name);
951 if (!data->bulk_rx_ep)
954 urb = usb_alloc_urb(0, mem_flags);
958 buf = kmalloc(size, mem_flags);
964 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
966 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
967 btusb_bulk_complete, hdev);
969 urb->transfer_flags |= URB_FREE_BUFFER;
971 usb_mark_last_busy(data->udev);
972 usb_anchor_urb(urb, &data->bulk_anchor);
974 err = usb_submit_urb(urb, mem_flags);
976 if (err != -EPERM && err != -ENODEV)
977 bt_dev_err(hdev, "urb %p submission failed (%d)",
979 usb_unanchor_urb(urb);
987 static void btusb_isoc_complete(struct urb *urb)
989 struct hci_dev *hdev = urb->context;
990 struct btusb_data *data = hci_get_drvdata(hdev);
993 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
996 if (!test_bit(HCI_RUNNING, &hdev->flags))
999 if (urb->status == 0) {
1000 for (i = 0; i < urb->number_of_packets; i++) {
1001 unsigned int offset = urb->iso_frame_desc[i].offset;
1002 unsigned int length = urb->iso_frame_desc[i].actual_length;
1004 if (urb->iso_frame_desc[i].status)
1007 hdev->stat.byte_rx += length;
1009 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1011 bt_dev_err(hdev, "corrupted SCO packet");
1012 hdev->stat.err_rx++;
1015 } else if (urb->status == -ENOENT) {
1016 /* Avoid suspend failed when usb_kill_urb */
1020 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1023 usb_anchor_urb(urb, &data->isoc_anchor);
1025 err = usb_submit_urb(urb, GFP_ATOMIC);
1027 /* -EPERM: urb is being killed;
1028 * -ENODEV: device got disconnected
1030 if (err != -EPERM && err != -ENODEV)
1031 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1033 usb_unanchor_urb(urb);
1037 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1038 int mtu, struct btusb_data *data)
1041 unsigned int interval;
1043 BT_DBG("len %d mtu %d", len, mtu);
1045 /* For mSBC ALT 6 setting the host will send the packet at continuous
1046 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1047 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1048 * To maintain the rate we send 63bytes of usb packets alternatively for
1049 * 7ms and 8ms to maintain the rate as 7.5ms.
1051 if (data->usb_alt6_packet_flow) {
1053 data->usb_alt6_packet_flow = false;
1056 data->usb_alt6_packet_flow = true;
1059 for (i = 0; i < interval; i++) {
1060 urb->iso_frame_desc[i].offset = offset;
1061 urb->iso_frame_desc[i].length = offset;
1064 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1065 urb->iso_frame_desc[i].offset = offset;
1066 urb->iso_frame_desc[i].length = len;
1070 urb->number_of_packets = i;
1073 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1077 BT_DBG("len %d mtu %d", len, mtu);
1079 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1080 i++, offset += mtu, len -= mtu) {
1081 urb->iso_frame_desc[i].offset = offset;
1082 urb->iso_frame_desc[i].length = mtu;
1085 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1086 urb->iso_frame_desc[i].offset = offset;
1087 urb->iso_frame_desc[i].length = len;
1091 urb->number_of_packets = i;
1094 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1096 struct btusb_data *data = hci_get_drvdata(hdev);
1102 BT_DBG("%s", hdev->name);
1104 if (!data->isoc_rx_ep)
1107 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1111 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1112 BTUSB_MAX_ISOC_FRAMES;
1114 buf = kmalloc(size, mem_flags);
1120 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1122 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1123 hdev, data->isoc_rx_ep->bInterval);
1125 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1127 __fill_isoc_descriptor(urb, size,
1128 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1130 usb_anchor_urb(urb, &data->isoc_anchor);
1132 err = usb_submit_urb(urb, mem_flags);
1134 if (err != -EPERM && err != -ENODEV)
1135 bt_dev_err(hdev, "urb %p submission failed (%d)",
1137 usb_unanchor_urb(urb);
1145 static void btusb_diag_complete(struct urb *urb)
1147 struct hci_dev *hdev = urb->context;
1148 struct btusb_data *data = hci_get_drvdata(hdev);
1151 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1152 urb->actual_length);
1154 if (urb->status == 0) {
1155 struct sk_buff *skb;
1157 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1159 skb_put_data(skb, urb->transfer_buffer,
1160 urb->actual_length);
1161 hci_recv_diag(hdev, skb);
1163 } else if (urb->status == -ENOENT) {
1164 /* Avoid suspend failed when usb_kill_urb */
1168 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1171 usb_anchor_urb(urb, &data->diag_anchor);
1172 usb_mark_last_busy(data->udev);
1174 err = usb_submit_urb(urb, GFP_ATOMIC);
1176 /* -EPERM: urb is being killed;
1177 * -ENODEV: device got disconnected
1179 if (err != -EPERM && err != -ENODEV)
1180 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1182 usb_unanchor_urb(urb);
1186 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1188 struct btusb_data *data = hci_get_drvdata(hdev);
1192 int err, size = HCI_MAX_FRAME_SIZE;
1194 BT_DBG("%s", hdev->name);
1196 if (!data->diag_rx_ep)
1199 urb = usb_alloc_urb(0, mem_flags);
1203 buf = kmalloc(size, mem_flags);
1209 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1211 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1212 btusb_diag_complete, hdev);
1214 urb->transfer_flags |= URB_FREE_BUFFER;
1216 usb_mark_last_busy(data->udev);
1217 usb_anchor_urb(urb, &data->diag_anchor);
1219 err = usb_submit_urb(urb, mem_flags);
1221 if (err != -EPERM && err != -ENODEV)
1222 bt_dev_err(hdev, "urb %p submission failed (%d)",
1224 usb_unanchor_urb(urb);
1232 static void btusb_tx_complete(struct urb *urb)
1234 struct sk_buff *skb = urb->context;
1235 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1236 struct btusb_data *data = hci_get_drvdata(hdev);
1237 unsigned long flags;
1239 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1240 urb->actual_length);
1242 if (!test_bit(HCI_RUNNING, &hdev->flags))
1246 hdev->stat.byte_tx += urb->transfer_buffer_length;
1248 hdev->stat.err_tx++;
1251 spin_lock_irqsave(&data->txlock, flags);
1252 data->tx_in_flight--;
1253 spin_unlock_irqrestore(&data->txlock, flags);
1255 kfree(urb->setup_packet);
1260 static void btusb_isoc_tx_complete(struct urb *urb)
1262 struct sk_buff *skb = urb->context;
1263 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1265 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1266 urb->actual_length);
1268 if (!test_bit(HCI_RUNNING, &hdev->flags))
1272 hdev->stat.byte_tx += urb->transfer_buffer_length;
1274 hdev->stat.err_tx++;
1277 kfree(urb->setup_packet);
1282 static int btusb_open(struct hci_dev *hdev)
1284 struct btusb_data *data = hci_get_drvdata(hdev);
1287 BT_DBG("%s", hdev->name);
1289 err = usb_autopm_get_interface(data->intf);
1293 /* Patching USB firmware files prior to starting any URBs of HCI path
1294 * It is more safe to use USB bulk channel for downloading USB patch
1296 if (data->setup_on_usb) {
1297 err = data->setup_on_usb(hdev);
1302 data->intf->needs_remote_wakeup = 1;
1304 /* Disable device remote wakeup when host is suspended
1305 * For Realtek chips, global suspend without
1306 * SET_FEATURE (DEVICE_REMOTE_WAKEUP) can save more power in device.
1308 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1309 device_wakeup_disable(&data->udev->dev);
1311 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1314 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1318 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1320 usb_kill_anchored_urbs(&data->intr_anchor);
1324 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1325 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1328 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1329 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1333 usb_autopm_put_interface(data->intf);
1337 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1339 usb_autopm_put_interface(data->intf);
1343 static void btusb_stop_traffic(struct btusb_data *data)
1345 usb_kill_anchored_urbs(&data->intr_anchor);
1346 usb_kill_anchored_urbs(&data->bulk_anchor);
1347 usb_kill_anchored_urbs(&data->isoc_anchor);
1348 usb_kill_anchored_urbs(&data->diag_anchor);
1349 usb_kill_anchored_urbs(&data->ctrl_anchor);
1352 static int btusb_close(struct hci_dev *hdev)
1354 struct btusb_data *data = hci_get_drvdata(hdev);
1357 BT_DBG("%s", hdev->name);
1359 cancel_work_sync(&data->work);
1360 cancel_work_sync(&data->waker);
1362 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1363 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1364 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1365 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1367 btusb_stop_traffic(data);
1368 btusb_free_frags(data);
1370 err = usb_autopm_get_interface(data->intf);
1374 data->intf->needs_remote_wakeup = 0;
1376 /* Enable remote wake up for auto-suspend */
1377 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1378 data->intf->needs_remote_wakeup = 1;
1380 usb_autopm_put_interface(data->intf);
1383 usb_scuttle_anchored_urbs(&data->deferred);
1387 static int btusb_flush(struct hci_dev *hdev)
1389 struct btusb_data *data = hci_get_drvdata(hdev);
1391 BT_DBG("%s", hdev->name);
1393 usb_kill_anchored_urbs(&data->tx_anchor);
1394 btusb_free_frags(data);
1399 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1401 struct btusb_data *data = hci_get_drvdata(hdev);
1402 struct usb_ctrlrequest *dr;
1406 urb = usb_alloc_urb(0, GFP_KERNEL);
1408 return ERR_PTR(-ENOMEM);
1410 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1413 return ERR_PTR(-ENOMEM);
1416 dr->bRequestType = data->cmdreq_type;
1417 dr->bRequest = data->cmdreq;
1420 dr->wLength = __cpu_to_le16(skb->len);
1422 pipe = usb_sndctrlpipe(data->udev, 0x00);
1424 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1425 skb->data, skb->len, btusb_tx_complete, skb);
1427 skb->dev = (void *)hdev;
1432 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1434 struct btusb_data *data = hci_get_drvdata(hdev);
1438 if (!data->bulk_tx_ep)
1439 return ERR_PTR(-ENODEV);
1441 urb = usb_alloc_urb(0, GFP_KERNEL);
1443 return ERR_PTR(-ENOMEM);
1445 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1447 usb_fill_bulk_urb(urb, data->udev, pipe,
1448 skb->data, skb->len, btusb_tx_complete, skb);
1450 skb->dev = (void *)hdev;
1455 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1457 struct btusb_data *data = hci_get_drvdata(hdev);
1461 if (!data->isoc_tx_ep)
1462 return ERR_PTR(-ENODEV);
1464 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1466 return ERR_PTR(-ENOMEM);
1468 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1470 usb_fill_int_urb(urb, data->udev, pipe,
1471 skb->data, skb->len, btusb_isoc_tx_complete,
1472 skb, data->isoc_tx_ep->bInterval);
1474 urb->transfer_flags = URB_ISO_ASAP;
1476 if (data->isoc_altsetting == 6)
1477 __fill_isoc_descriptor_msbc(urb, skb->len,
1478 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1481 __fill_isoc_descriptor(urb, skb->len,
1482 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1483 skb->dev = (void *)hdev;
1488 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1490 struct btusb_data *data = hci_get_drvdata(hdev);
1493 usb_anchor_urb(urb, &data->tx_anchor);
1495 err = usb_submit_urb(urb, GFP_KERNEL);
1497 if (err != -EPERM && err != -ENODEV)
1498 bt_dev_err(hdev, "urb %p submission failed (%d)",
1500 kfree(urb->setup_packet);
1501 usb_unanchor_urb(urb);
1503 usb_mark_last_busy(data->udev);
1510 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1512 struct btusb_data *data = hci_get_drvdata(hdev);
1513 unsigned long flags;
1516 spin_lock_irqsave(&data->txlock, flags);
1517 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1519 data->tx_in_flight++;
1520 spin_unlock_irqrestore(&data->txlock, flags);
1523 return submit_tx_urb(hdev, urb);
1525 usb_anchor_urb(urb, &data->deferred);
1526 schedule_work(&data->waker);
1532 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1536 BT_DBG("%s", hdev->name);
1538 switch (hci_skb_pkt_type(skb)) {
1539 case HCI_COMMAND_PKT:
1540 urb = alloc_ctrl_urb(hdev, skb);
1542 return PTR_ERR(urb);
1544 hdev->stat.cmd_tx++;
1545 return submit_or_queue_tx_urb(hdev, urb);
1547 case HCI_ACLDATA_PKT:
1548 urb = alloc_bulk_urb(hdev, skb);
1550 return PTR_ERR(urb);
1552 hdev->stat.acl_tx++;
1553 return submit_or_queue_tx_urb(hdev, urb);
1555 case HCI_SCODATA_PKT:
1556 if (hci_conn_num(hdev, SCO_LINK) < 1)
1559 urb = alloc_isoc_urb(hdev, skb);
1561 return PTR_ERR(urb);
1563 hdev->stat.sco_tx++;
1564 return submit_tx_urb(hdev, urb);
1570 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1572 struct btusb_data *data = hci_get_drvdata(hdev);
1574 BT_DBG("%s evt %d", hdev->name, evt);
1576 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1577 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1578 data->air_mode = evt;
1579 schedule_work(&data->work);
1583 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1585 struct btusb_data *data = hci_get_drvdata(hdev);
1586 struct usb_interface *intf = data->isoc;
1587 struct usb_endpoint_descriptor *ep_desc;
1593 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
1595 bt_dev_err(hdev, "setting interface failed (%d)", -err);
1599 data->isoc_altsetting = altsetting;
1601 data->isoc_tx_ep = NULL;
1602 data->isoc_rx_ep = NULL;
1604 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1605 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1607 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1608 data->isoc_tx_ep = ep_desc;
1612 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1613 data->isoc_rx_ep = ep_desc;
1618 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1619 bt_dev_err(hdev, "invalid SCO descriptors");
1626 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1628 struct btusb_data *data = hci_get_drvdata(hdev);
1631 if (data->isoc_altsetting != new_alts) {
1632 unsigned long flags;
1634 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1635 usb_kill_anchored_urbs(&data->isoc_anchor);
1637 /* When isochronous alternate setting needs to be
1638 * changed, because SCO connection has been added
1639 * or removed, a packet fragment may be left in the
1640 * reassembling state. This could lead to wrongly
1641 * assembled fragments.
1643 * Clear outstanding fragment when selecting a new
1644 * alternate setting.
1646 spin_lock_irqsave(&data->rxlock, flags);
1647 kfree_skb(data->sco_skb);
1648 data->sco_skb = NULL;
1649 spin_unlock_irqrestore(&data->rxlock, flags);
1651 err = __set_isoc_interface(hdev, new_alts);
1656 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1657 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1658 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1660 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1666 static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1669 struct usb_interface *intf = data->isoc;
1672 BT_DBG("Looking for Alt no :%d", alt);
1677 for (i = 0; i < intf->num_altsetting; i++) {
1678 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1679 return &intf->altsetting[i];
1685 static void btusb_work(struct work_struct *work)
1687 struct btusb_data *data = container_of(work, struct btusb_data, work);
1688 struct hci_dev *hdev = data->hdev;
1692 if (data->sco_num > 0) {
1693 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1694 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1696 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1697 usb_kill_anchored_urbs(&data->isoc_anchor);
1701 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1704 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1705 if (hdev->voice_setting & 0x0020) {
1706 static const int alts[3] = { 2, 4, 5 };
1708 new_alts = alts[data->sco_num - 1];
1710 new_alts = data->sco_num;
1712 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
1713 /* Check if Alt 6 is supported for Transparent audio */
1714 if (btusb_find_altsetting(data, 6)) {
1715 data->usb_alt6_packet_flow = true;
1717 } else if (test_bit(BTUSB_USE_ALT1_FOR_WBS, &data->flags)) {
1720 bt_dev_err(hdev, "Device does not support ALT setting 6");
1724 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1725 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
1727 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1728 usb_kill_anchored_urbs(&data->isoc_anchor);
1730 __set_isoc_interface(hdev, 0);
1731 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1732 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1736 static void btusb_waker(struct work_struct *work)
1738 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1741 err = usb_autopm_get_interface(data->intf);
1745 usb_autopm_put_interface(data->intf);
1748 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1750 struct sk_buff *skb;
1753 BT_DBG("%s", hdev->name);
1755 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1757 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
1764 static int btusb_setup_csr(struct hci_dev *hdev)
1766 struct hci_rp_read_local_version *rp;
1767 struct sk_buff *skb;
1768 bool is_fake = false;
1770 BT_DBG("%s", hdev->name);
1772 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1775 int err = PTR_ERR(skb);
1776 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
1780 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1781 bt_dev_err(hdev, "CSR: Local version length mismatch");
1786 rp = (struct hci_rp_read_local_version *)skb->data;
1788 /* Detect a wide host of Chinese controllers that aren't CSR.
1790 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1792 * The main thing they have in common is that these are really popular low-cost
1793 * options that support newer Bluetooth versions but rely on heavy VID/PID
1794 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1796 * We detect actual CSR devices by checking that the HCI manufacturer code
1797 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1798 * HCI rev values always match. As they both store the firmware number.
1800 if (le16_to_cpu(rp->manufacturer) != 10 ||
1801 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
1804 /* Known legit CSR firmware build numbers and their supported BT versions:
1805 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1806 * - 1.2 (0x2) -> 0x04d9, 0x0529
1807 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
1808 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
1809 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
1811 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
1812 * support BT 1.1 only; so it's a dead giveaway when some
1813 * third-party BT 4.0 dongle reuses it.
1815 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
1816 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
1819 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
1820 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
1823 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
1824 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
1827 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
1828 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
1831 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
1832 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
1836 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds...");
1838 /* Generally these clones have big discrepancies between
1839 * advertised features and what's actually supported.
1840 * Probably will need to be expanded in the future;
1841 * without these the controller will lock up.
1843 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1844 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
1846 /* Clear the reset quirk since this is not an actual
1847 * early Bluetooth 1.1 device from CSR.
1849 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1850 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1858 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1859 struct intel_version *ver)
1861 const struct firmware *fw;
1865 snprintf(fwname, sizeof(fwname),
1866 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1867 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1868 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1869 ver->fw_build_ww, ver->fw_build_yy);
1871 ret = request_firmware(&fw, fwname, &hdev->dev);
1873 if (ret == -EINVAL) {
1874 bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1879 bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1882 /* If the correct firmware patch file is not found, use the
1883 * default firmware patch file instead
1885 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1886 ver->hw_platform, ver->hw_variant);
1887 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1888 bt_dev_err(hdev, "failed to open default fw file: %s",
1894 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
1899 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1900 const struct firmware *fw,
1901 const u8 **fw_ptr, int *disable_patch)
1903 struct sk_buff *skb;
1904 struct hci_command_hdr *cmd;
1905 const u8 *cmd_param;
1906 struct hci_event_hdr *evt = NULL;
1907 const u8 *evt_param = NULL;
1908 int remain = fw->size - (*fw_ptr - fw->data);
1910 /* The first byte indicates the types of the patch command or event.
1911 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1912 * in the current firmware buffer doesn't start with 0x01 or
1913 * the size of remain buffer is smaller than HCI command header,
1914 * the firmware file is corrupted and it should stop the patching
1917 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1918 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
1924 cmd = (struct hci_command_hdr *)(*fw_ptr);
1925 *fw_ptr += sizeof(*cmd);
1926 remain -= sizeof(*cmd);
1928 /* Ensure that the remain firmware data is long enough than the length
1929 * of command parameter. If not, the firmware file is corrupted.
1931 if (remain < cmd->plen) {
1932 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
1936 /* If there is a command that loads a patch in the firmware
1937 * file, then enable the patch upon success, otherwise just
1938 * disable the manufacturer mode, for example patch activation
1939 * is not required when the default firmware patch file is used
1940 * because there are no patch data to load.
1942 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1945 cmd_param = *fw_ptr;
1946 *fw_ptr += cmd->plen;
1947 remain -= cmd->plen;
1949 /* This reads the expected events when the above command is sent to the
1950 * device. Some vendor commands expects more than one events, for
1951 * example command status event followed by vendor specific event.
1952 * For this case, it only keeps the last expected event. so the command
1953 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1954 * last expected event.
1956 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1960 evt = (struct hci_event_hdr *)(*fw_ptr);
1961 *fw_ptr += sizeof(*evt);
1962 remain -= sizeof(*evt);
1964 if (remain < evt->plen) {
1965 bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
1969 evt_param = *fw_ptr;
1970 *fw_ptr += evt->plen;
1971 remain -= evt->plen;
1974 /* Every HCI commands in the firmware file has its correspond event.
1975 * If event is not found or remain is smaller than zero, the firmware
1976 * file is corrupted.
1978 if (!evt || !evt_param || remain < 0) {
1979 bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
1983 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1984 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1986 bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
1987 cmd->opcode, PTR_ERR(skb));
1988 return PTR_ERR(skb);
1991 /* It ensures that the returned event matches the event data read from
1992 * the firmware file. At fist, it checks the length and then
1993 * the contents of the event.
1995 if (skb->len != evt->plen) {
1996 bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
1997 le16_to_cpu(cmd->opcode));
2002 if (memcmp(skb->data, evt_param, evt->plen)) {
2003 bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
2004 le16_to_cpu(cmd->opcode));
2013 static int btusb_setup_intel(struct hci_dev *hdev)
2015 struct sk_buff *skb;
2016 const struct firmware *fw;
2018 int disable_patch, err;
2019 struct intel_version ver;
2021 BT_DBG("%s", hdev->name);
2023 /* The controller has a bug with the first HCI command sent to it
2024 * returning number of completed commands as zero. This would stall the
2025 * command processing in the Bluetooth core.
2027 * As a workaround, send HCI Reset command first which will reset the
2028 * number of completed commands and allow normal command processing
2031 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2033 bt_dev_err(hdev, "sending initial HCI reset command failed (%ld)",
2035 return PTR_ERR(skb);
2039 /* Read Intel specific controller version first to allow selection of
2040 * which firmware file to load.
2042 * The returned information are hardware variant and revision plus
2043 * firmware variant, revision and build number.
2045 err = btintel_read_version(hdev, &ver);
2049 bt_dev_info(hdev, "read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
2050 ver.hw_platform, ver.hw_variant, ver.hw_revision,
2051 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
2052 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
2054 /* fw_patch_num indicates the version of patch the device currently
2055 * have. If there is no patch data in the device, it is always 0x00.
2056 * So, if it is other than 0x00, no need to patch the device again.
2058 if (ver.fw_patch_num) {
2059 bt_dev_info(hdev, "Intel device is already patched. "
2060 "patch num: %02x", ver.fw_patch_num);
2064 /* Opens the firmware patch file based on the firmware version read
2065 * from the controller. If it fails to open the matching firmware
2066 * patch file, it tries to open the default firmware patch file.
2067 * If no patch file is found, allow the device to operate without
2070 fw = btusb_setup_intel_get_fw(hdev, &ver);
2075 /* Enable the manufacturer mode of the controller.
2076 * Only while this mode is enabled, the driver can download the
2077 * firmware patch data and configuration parameters.
2079 err = btintel_enter_mfg(hdev);
2081 release_firmware(fw);
2087 /* The firmware data file consists of list of Intel specific HCI
2088 * commands and its expected events. The first byte indicates the
2089 * type of the message, either HCI command or HCI event.
2091 * It reads the command and its expected event from the firmware file,
2092 * and send to the controller. Once __hci_cmd_sync_ev() returns,
2093 * the returned event is compared with the event read from the firmware
2094 * file and it will continue until all the messages are downloaded to
2097 * Once the firmware patching is completed successfully,
2098 * the manufacturer mode is disabled with reset and activating the
2101 * If the firmware patching fails, the manufacturer mode is
2102 * disabled with reset and deactivating the patch.
2104 * If the default patch file is used, no reset is done when disabling
2107 while (fw->size > fw_ptr - fw->data) {
2110 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
2113 goto exit_mfg_deactivate;
2116 release_firmware(fw);
2119 goto exit_mfg_disable;
2121 /* Patching completed successfully and disable the manufacturer mode
2122 * with reset and activate the downloaded firmware patches.
2124 err = btintel_exit_mfg(hdev, true, true);
2128 /* Need build number for downloaded fw patches in
2129 * every power-on boot
2131 err = btintel_read_version(hdev, &ver);
2134 bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
2140 /* Disable the manufacturer mode without reset */
2141 err = btintel_exit_mfg(hdev, false, false);
2145 bt_dev_info(hdev, "Intel firmware patch completed");
2149 exit_mfg_deactivate:
2150 release_firmware(fw);
2152 /* Patching failed. Disable the manufacturer mode with reset and
2153 * deactivate the downloaded firmware patches.
2155 err = btintel_exit_mfg(hdev, true, false);
2159 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
2162 /* Set the event mask for Intel specific vendor events. This enables
2163 * a few extra events that are useful during general operation.
2165 btintel_set_event_mask_mfg(hdev, false);
2167 btintel_check_bdaddr(hdev);
2171 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2173 struct sk_buff *skb;
2174 struct hci_event_hdr *hdr;
2175 struct hci_ev_cmd_complete *evt;
2177 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2181 hdr = skb_put(skb, sizeof(*hdr));
2182 hdr->evt = HCI_EV_CMD_COMPLETE;
2183 hdr->plen = sizeof(*evt) + 1;
2185 evt = skb_put(skb, sizeof(*evt));
2187 evt->opcode = cpu_to_le16(opcode);
2189 skb_put_u8(skb, 0x00);
2191 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2193 return hci_recv_frame(hdev, skb);
2196 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2199 /* When the device is in bootloader mode, then it can send
2200 * events via the bulk endpoint. These events are treated the
2201 * same way as the ones received from the interrupt endpoint.
2203 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
2204 return btusb_recv_intr(data, buffer, count);
2206 return btusb_recv_bulk(data, buffer, count);
2209 static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
2212 const struct intel_bootup *evt = ptr;
2214 if (len != sizeof(*evt))
2217 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags))
2218 wake_up_bit(&data->flags, BTUSB_BOOTING);
2221 static void btusb_intel_secure_send_result(struct btusb_data *data,
2222 const void *ptr, unsigned int len)
2224 const struct intel_secure_send_result *evt = ptr;
2226 if (len != sizeof(*evt))
2230 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
2232 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
2233 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags))
2234 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
2237 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2239 struct btusb_data *data = hci_get_drvdata(hdev);
2241 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2242 struct hci_event_hdr *hdr = (void *)skb->data;
2244 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2246 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2247 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
2249 switch (skb->data[2]) {
2251 /* When switching to the operational firmware
2252 * the device sends a vendor specific event
2253 * indicating that the bootup completed.
2255 btusb_intel_bootup(data, ptr, len);
2258 /* When the firmware loading completes the
2259 * device sends out a vendor specific event
2260 * indicating the result of the firmware
2263 btusb_intel_secure_send_result(data, ptr, len);
2269 return hci_recv_frame(hdev, skb);
2272 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2274 struct btusb_data *data = hci_get_drvdata(hdev);
2277 BT_DBG("%s", hdev->name);
2279 switch (hci_skb_pkt_type(skb)) {
2280 case HCI_COMMAND_PKT:
2281 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2282 struct hci_command_hdr *cmd = (void *)skb->data;
2283 __u16 opcode = le16_to_cpu(cmd->opcode);
2285 /* When in bootloader mode and the command 0xfc09
2286 * is received, it needs to be send down the
2287 * bulk endpoint. So allocate a bulk URB instead.
2289 if (opcode == 0xfc09)
2290 urb = alloc_bulk_urb(hdev, skb);
2292 urb = alloc_ctrl_urb(hdev, skb);
2294 /* When the 0xfc01 command is issued to boot into
2295 * the operational firmware, it will actually not
2296 * send a command complete event. To keep the flow
2297 * control working inject that event here.
2299 if (opcode == 0xfc01)
2300 inject_cmd_complete(hdev, opcode);
2302 urb = alloc_ctrl_urb(hdev, skb);
2305 return PTR_ERR(urb);
2307 hdev->stat.cmd_tx++;
2308 return submit_or_queue_tx_urb(hdev, urb);
2310 case HCI_ACLDATA_PKT:
2311 urb = alloc_bulk_urb(hdev, skb);
2313 return PTR_ERR(urb);
2315 hdev->stat.acl_tx++;
2316 return submit_or_queue_tx_urb(hdev, urb);
2318 case HCI_SCODATA_PKT:
2319 if (hci_conn_num(hdev, SCO_LINK) < 1)
2322 urb = alloc_isoc_urb(hdev, skb);
2324 return PTR_ERR(urb);
2326 hdev->stat.sco_tx++;
2327 return submit_tx_urb(hdev, urb);
2333 static bool btusb_setup_intel_new_get_fw_name(struct intel_version *ver,
2334 struct intel_boot_params *params,
2335 char *fw_name, size_t len,
2338 switch (ver->hw_variant) {
2339 case 0x0b: /* SfP */
2340 case 0x0c: /* WsP */
2341 snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
2342 le16_to_cpu(ver->hw_variant),
2343 le16_to_cpu(params->dev_revid),
2346 case 0x11: /* JfP */
2347 case 0x12: /* ThP */
2348 case 0x13: /* HrP */
2349 case 0x14: /* CcP */
2350 snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
2351 le16_to_cpu(ver->hw_variant),
2352 le16_to_cpu(ver->hw_revision),
2353 le16_to_cpu(ver->fw_revision),
2362 static int btusb_intel_download_firmware(struct hci_dev *hdev,
2363 struct intel_version *ver,
2364 struct intel_boot_params *params,
2367 const struct firmware *fw;
2370 struct btusb_data *data = hci_get_drvdata(hdev);
2372 if (!ver || !params)
2375 /* The hardware platform number has a fixed value of 0x37 and
2376 * for now only accept this single value.
2378 if (ver->hw_platform != 0x37) {
2379 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
2384 /* Check for supported iBT hardware variants of this firmware
2387 * This check has been put in place to ensure correct forward
2388 * compatibility options when newer hardware variants come along.
2390 switch (ver->hw_variant) {
2391 case 0x0b: /* SfP */
2392 case 0x0c: /* WsP */
2393 case 0x11: /* JfP */
2394 case 0x12: /* ThP */
2395 case 0x13: /* HrP */
2396 case 0x14: /* CcP */
2399 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
2404 btintel_version_info(hdev, ver);
2406 /* The firmware variant determines if the device is in bootloader
2407 * mode or is running operational firmware. The value 0x06 identifies
2408 * the bootloader and the value 0x23 identifies the operational
2411 * When the operational firmware is already present, then only
2412 * the check for valid Bluetooth device address is needed. This
2413 * determines if the device will be added as configured or
2414 * unconfigured controller.
2416 * It is not possible to use the Secure Boot Parameters in this
2417 * case since that command is only available in bootloader mode.
2419 if (ver->fw_variant == 0x23) {
2420 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2421 btintel_check_bdaddr(hdev);
2425 /* If the device is not in bootloader mode, then the only possible
2426 * choice is to return an error and abort the device initialization.
2428 if (ver->fw_variant != 0x06) {
2429 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
2434 /* Read the secure boot parameters to identify the operating
2435 * details of the bootloader.
2437 err = btintel_read_boot_params(hdev, params);
2441 /* It is required that every single firmware fragment is acknowledged
2442 * with a command complete event. If the boot parameters indicate
2443 * that this bootloader does not send them, then abort the setup.
2445 if (params->limited_cce != 0x00) {
2446 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
2447 params->limited_cce);
2451 /* If the OTP has no valid Bluetooth device address, then there will
2452 * also be no valid address for the operational firmware.
2454 if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) {
2455 bt_dev_info(hdev, "No device address configured");
2456 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2459 /* With this Intel bootloader only the hardware variant and device
2460 * revision information are used to select the right firmware for SfP
2463 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2465 * Currently the supported hardware variants are:
2466 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2467 * 12 (0x0c) for iBT3.5 (WsP)
2469 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2470 * variant, HW revision and FW revision, as these are dependent on CNVi
2471 * and RF Combination.
2473 * 17 (0x11) for iBT3.5 (JfP)
2474 * 18 (0x12) for iBT3.5 (ThP)
2476 * The firmware file name for these will be
2477 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2480 err = btusb_setup_intel_new_get_fw_name(ver, params, fwname,
2481 sizeof(fwname), "sfi");
2483 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2487 err = request_firmware(&fw, fwname, &hdev->dev);
2489 bt_dev_err(hdev, "Failed to load Intel firmware file (%d)", err);
2493 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2495 if (fw->size < 644) {
2496 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2502 set_bit(BTUSB_DOWNLOADING, &data->flags);
2504 /* Start firmware downloading and get boot parameter */
2505 err = btintel_download_firmware(hdev, fw, boot_param);
2507 /* When FW download fails, send Intel Reset to retry
2510 btintel_reset_to_bootloader(hdev);
2513 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2515 bt_dev_info(hdev, "Waiting for firmware download to complete");
2517 /* Before switching the device into operational mode and with that
2518 * booting the loaded firmware, wait for the bootloader notification
2519 * that all fragments have been successfully received.
2521 * When the event processing receives the notification, then the
2522 * BTUSB_DOWNLOADING flag will be cleared.
2524 * The firmware loading should not take longer than 5 seconds
2525 * and thus just timeout if that happens and fail the setup
2528 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2530 msecs_to_jiffies(5000));
2531 if (err == -EINTR) {
2532 bt_dev_err(hdev, "Firmware loading interrupted");
2537 bt_dev_err(hdev, "Firmware loading timeout");
2539 btintel_reset_to_bootloader(hdev);
2543 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2544 bt_dev_err(hdev, "Firmware loading failed");
2550 release_firmware(fw);
2554 static int btusb_setup_intel_new(struct hci_dev *hdev)
2556 struct btusb_data *data = hci_get_drvdata(hdev);
2557 struct intel_version ver;
2558 struct intel_boot_params params;
2561 ktime_t calltime, delta, rettime;
2562 unsigned long long duration;
2564 struct intel_debug_features features;
2566 BT_DBG("%s", hdev->name);
2568 /* Set the default boot parameter to 0x0 and it is updated to
2569 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2570 * command while downloading the firmware.
2572 boot_param = 0x00000000;
2574 calltime = ktime_get();
2576 /* Read the Intel version information to determine if the device
2577 * is in bootloader mode or if it already has operational firmware
2580 err = btintel_read_version(hdev, &ver);
2582 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2583 btintel_reset_to_bootloader(hdev);
2587 err = btusb_intel_download_firmware(hdev, &ver, ¶ms, &boot_param);
2591 /* controller is already having an operational firmware */
2592 if (ver.fw_variant == 0x23)
2595 rettime = ktime_get();
2596 delta = ktime_sub(rettime, calltime);
2597 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2599 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
2601 calltime = ktime_get();
2603 set_bit(BTUSB_BOOTING, &data->flags);
2605 err = btintel_send_intel_reset(hdev, boot_param);
2607 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
2608 btintel_reset_to_bootloader(hdev);
2612 /* The bootloader will not indicate when the device is ready. This
2613 * is done by the operational firmware sending bootup notification.
2615 * Booting into operational firmware should not take longer than
2616 * 1 second. However if that happens, then just fail the setup
2617 * since something went wrong.
2619 bt_dev_info(hdev, "Waiting for device to boot");
2621 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2623 msecs_to_jiffies(1000));
2625 if (err == -EINTR) {
2626 bt_dev_err(hdev, "Device boot interrupted");
2631 bt_dev_err(hdev, "Device boot timeout");
2632 btintel_reset_to_bootloader(hdev);
2636 rettime = ktime_get();
2637 delta = ktime_sub(rettime, calltime);
2638 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2640 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
2642 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2644 err = btusb_setup_intel_new_get_fw_name(&ver, ¶ms, ddcname,
2645 sizeof(ddcname), "ddc");
2648 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2650 /* Once the device is running in operational mode, it needs to
2651 * apply the device configuration (DDC) parameters.
2653 * The device can work without DDC parameters, so even if it
2654 * fails to load the file, no need to fail the setup.
2656 btintel_load_ddc_config(hdev, ddcname);
2659 /* Read the Intel supported features and if new exception formats
2660 * supported, need to load the additional DDC config to enable.
2662 btintel_read_debug_features(hdev, &features);
2664 /* Set DDC mask for available debug features */
2665 btintel_set_debug_features(hdev, &features);
2667 /* Read the Intel version information after loading the FW */
2668 err = btintel_read_version(hdev, &ver);
2672 btintel_version_info(hdev, &ver);
2675 /* All Intel controllers that support the Microsoft vendor
2676 * extension are using 0xFC1E for VsMsftOpCode.
2678 switch (ver.hw_variant) {
2679 case 0x12: /* ThP */
2680 hci_set_msft_opcode(hdev, 0xFC1E);
2684 /* Set the event mask for Intel specific vendor events. This enables
2685 * a few extra events that are useful during general operation. It
2686 * does not enable any debugging related events.
2688 * The device will function correctly without these events enabled
2689 * and thus no need to fail the setup.
2691 btintel_set_event_mask(hdev, false);
2696 static int btusb_shutdown_intel(struct hci_dev *hdev)
2698 struct sk_buff *skb;
2701 /* In the shutdown sequence where Bluetooth is turned off followed
2702 * by WiFi being turned off, turning WiFi back on causes issue with
2703 * the RF calibration.
2705 * To ensure that any RF activity has been stopped, issue HCI Reset
2706 * command to clear all ongoing activity including advertising,
2709 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2712 bt_dev_err(hdev, "HCI reset during shutdown failed");
2717 /* Some platforms have an issue with BT LED when the interface is
2718 * down or BT radio is turned off, which takes 5 seconds to BT LED
2719 * goes off. This command turns off the BT LED immediately.
2721 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2724 bt_dev_err(hdev, "turning off Intel device LED failed");
2732 static int btusb_shutdown_intel_new(struct hci_dev *hdev)
2734 struct sk_buff *skb;
2736 /* Send HCI Reset to the controller to stop any BT activity which
2737 * were triggered. This will help to save power and maintain the
2738 * sync b/w Host and controller
2740 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2742 bt_dev_err(hdev, "HCI reset during shutdown failed");
2743 return PTR_ERR(skb);
2750 #define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
2751 #define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
2753 #define HCI_WMT_MAX_EVENT_SIZE 64
2756 BTMTK_WMT_PATCH_DWNLD = 0x1,
2757 BTMTK_WMT_FUNC_CTRL = 0x6,
2758 BTMTK_WMT_RST = 0x7,
2759 BTMTK_WMT_SEMAPHORE = 0x17,
2764 BTMTK_WMT_PATCH_UNDONE,
2765 BTMTK_WMT_PATCH_DONE,
2766 BTMTK_WMT_ON_UNDONE,
2768 BTMTK_WMT_ON_PROGRESS,
2771 struct btmtk_wmt_hdr {
2778 struct btmtk_hci_wmt_cmd {
2779 struct btmtk_wmt_hdr hdr;
2783 struct btmtk_hci_wmt_evt {
2784 struct hci_event_hdr hhdr;
2785 struct btmtk_wmt_hdr whdr;
2788 struct btmtk_hci_wmt_evt_funcc {
2789 struct btmtk_hci_wmt_evt hwhdr;
2793 struct btmtk_tci_sleep {
2796 __le16 host_duration;
2798 u8 time_compensation;
2801 struct btmtk_hci_wmt_params {
2809 static void btusb_mtk_wmt_recv(struct urb *urb)
2811 struct hci_dev *hdev = urb->context;
2812 struct btusb_data *data = hci_get_drvdata(hdev);
2813 struct hci_event_hdr *hdr;
2814 struct sk_buff *skb;
2817 if (urb->status == 0 && urb->actual_length > 0) {
2818 hdev->stat.byte_rx += urb->actual_length;
2820 /* WMT event shouldn't be fragmented and the size should be
2821 * less than HCI_WMT_MAX_EVENT_SIZE.
2823 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2825 hdev->stat.err_rx++;
2829 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2830 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2832 hdr = (void *)skb->data;
2833 /* Fix up the vendor event id with 0xff for vendor specific
2834 * instead of 0xe4 so that event send via monitoring socket can
2835 * be parsed properly.
2839 /* When someone waits for the WMT event, the skb is being cloned
2840 * and being processed the events from there then.
2842 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
2843 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
2848 err = hci_recv_frame(hdev, skb);
2852 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2854 /* Barrier to sync with other CPUs */
2855 smp_mb__after_atomic();
2856 wake_up_bit(&data->flags,
2857 BTUSB_TX_WAIT_VND_EVT);
2862 kfree_skb(data->evt_skb);
2863 data->evt_skb = NULL;
2865 } else if (urb->status == -ENOENT) {
2866 /* Avoid suspend failed when usb_kill_urb */
2870 usb_mark_last_busy(data->udev);
2872 /* The URB complete handler is still called with urb->actual_length = 0
2873 * when the event is not available, so we should keep re-submitting
2874 * URB until WMT event returns, Also, It's necessary to wait some time
2875 * between the two consecutive control URBs to relax the target device
2876 * to generate the event. Otherwise, the WMT event cannot return from
2877 * the device successfully.
2881 usb_anchor_urb(urb, &data->ctrl_anchor);
2882 err = usb_submit_urb(urb, GFP_ATOMIC);
2884 /* -EPERM: urb is being killed;
2885 * -ENODEV: device got disconnected
2887 if (err != -EPERM && err != -ENODEV)
2888 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2890 usb_unanchor_urb(urb);
2894 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2896 struct btusb_data *data = hci_get_drvdata(hdev);
2897 struct usb_ctrlrequest *dr;
2903 urb = usb_alloc_urb(0, GFP_KERNEL);
2907 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2913 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2915 dr->wIndex = cpu_to_le16(0);
2916 dr->wValue = cpu_to_le16(48);
2917 dr->wLength = cpu_to_le16(size);
2919 buf = kmalloc(size, GFP_KERNEL);
2926 pipe = usb_rcvctrlpipe(data->udev, 0);
2928 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2929 buf, size, btusb_mtk_wmt_recv, hdev);
2931 urb->transfer_flags |= URB_FREE_BUFFER;
2933 usb_anchor_urb(urb, &data->ctrl_anchor);
2934 err = usb_submit_urb(urb, GFP_KERNEL);
2936 if (err != -EPERM && err != -ENODEV)
2937 bt_dev_err(hdev, "urb %p submission failed (%d)",
2939 usb_unanchor_urb(urb);
2947 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
2948 struct btmtk_hci_wmt_params *wmt_params)
2950 struct btusb_data *data = hci_get_drvdata(hdev);
2951 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
2952 u32 hlen, status = BTMTK_WMT_INVALID;
2953 struct btmtk_hci_wmt_evt *wmt_evt;
2954 struct btmtk_hci_wmt_cmd wc;
2955 struct btmtk_wmt_hdr *hdr;
2958 /* Submit control IN URB on demand to process the WMT event */
2959 err = btusb_mtk_submit_wmt_recv_urb(hdev);
2963 /* Send the WMT command and wait until the WMT event returns */
2964 hlen = sizeof(*hdr) + wmt_params->dlen;
2968 hdr = (struct btmtk_wmt_hdr *)&wc;
2970 hdr->op = wmt_params->op;
2971 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
2972 hdr->flag = wmt_params->flag;
2973 memcpy(wc.data, wmt_params->data, wmt_params->dlen);
2975 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2977 err = __hci_cmd_send(hdev, 0xfc6f, hlen, &wc);
2980 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2984 /* The vendor specific WMT commands are all answered by a vendor
2985 * specific event and will have the Command Status or Command
2986 * Complete as with usual HCI command flow control.
2988 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
2989 * state to be cleared. The driver specific event receive routine
2990 * will clear that state and with that indicate completion of the
2993 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
2994 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
2995 if (err == -EINTR) {
2996 bt_dev_err(hdev, "Execution of wmt command interrupted");
2997 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3002 bt_dev_err(hdev, "Execution of wmt command timed out");
3003 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3007 /* Parse and handle the return WMT event */
3008 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
3009 if (wmt_evt->whdr.op != hdr->op) {
3010 bt_dev_err(hdev, "Wrong op received %d expected %d",
3011 wmt_evt->whdr.op, hdr->op);
3016 switch (wmt_evt->whdr.op) {
3017 case BTMTK_WMT_SEMAPHORE:
3018 if (wmt_evt->whdr.flag == 2)
3019 status = BTMTK_WMT_PATCH_UNDONE;
3021 status = BTMTK_WMT_PATCH_DONE;
3023 case BTMTK_WMT_FUNC_CTRL:
3024 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
3025 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
3026 status = BTMTK_WMT_ON_DONE;
3027 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
3028 status = BTMTK_WMT_ON_PROGRESS;
3030 status = BTMTK_WMT_ON_UNDONE;
3034 if (wmt_params->status)
3035 *wmt_params->status = status;
3038 kfree_skb(data->evt_skb);
3039 data->evt_skb = NULL;
3044 static int btusb_mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
3046 struct btmtk_hci_wmt_params wmt_params;
3047 const struct firmware *fw;
3053 err = request_firmware(&fw, fwname, &hdev->dev);
3055 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
3059 /* Power on data RAM the firmware relies on. */
3061 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3062 wmt_params.flag = 3;
3063 wmt_params.dlen = sizeof(param);
3064 wmt_params.data = ¶m;
3065 wmt_params.status = NULL;
3067 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3069 bt_dev_err(hdev, "Failed to power on data RAM (%d)", err);
3076 /* The size of patch header is 30 bytes, should be skip */
3079 goto err_release_fw;
3086 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3087 wmt_params.status = NULL;
3089 while (fw_size > 0) {
3090 dlen = min_t(int, 250, fw_size);
3092 /* Tell deivice the position in sequence */
3093 if (fw_size - dlen <= 0)
3095 else if (fw_size < fw->size - 30)
3098 wmt_params.flag = flag;
3099 wmt_params.dlen = dlen;
3100 wmt_params.data = fw_ptr;
3102 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3104 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3106 goto err_release_fw;
3113 wmt_params.op = BTMTK_WMT_RST;
3114 wmt_params.flag = 4;
3115 wmt_params.dlen = 0;
3116 wmt_params.data = NULL;
3117 wmt_params.status = NULL;
3119 /* Activate funciton the firmware providing to */
3120 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3122 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
3123 goto err_release_fw;
3126 /* Wait a few moments for firmware activation done */
3127 usleep_range(10000, 12000);
3130 release_firmware(fw);
3135 static int btusb_mtk_func_query(struct hci_dev *hdev)
3137 struct btmtk_hci_wmt_params wmt_params;
3141 /* Query whether the function is enabled */
3142 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3143 wmt_params.flag = 4;
3144 wmt_params.dlen = sizeof(param);
3145 wmt_params.data = ¶m;
3146 wmt_params.status = &status;
3148 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3150 bt_dev_err(hdev, "Failed to query function status (%d)", err);
3157 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
3159 int pipe, err, size = sizeof(u32);
3162 buf = kzalloc(size, GFP_KERNEL);
3166 pipe = usb_rcvctrlpipe(data->udev, 0);
3167 err = usb_control_msg(data->udev, pipe, 0x63,
3168 USB_TYPE_VENDOR | USB_DIR_IN,
3169 reg >> 16, reg & 0xffff,
3170 buf, size, USB_CTRL_SET_TIMEOUT);
3174 *val = get_unaligned_le32(buf);
3182 static int btusb_mtk_id_get(struct btusb_data *data, u32 *id)
3184 return btusb_mtk_reg_read(data, 0x80000008, id);
3187 static int btusb_mtk_setup(struct hci_dev *hdev)
3189 struct btusb_data *data = hci_get_drvdata(hdev);
3190 struct btmtk_hci_wmt_params wmt_params;
3191 ktime_t calltime, delta, rettime;
3192 struct btmtk_tci_sleep tci_sleep;
3193 unsigned long long duration;
3194 struct sk_buff *skb;
3200 calltime = ktime_get();
3202 err = btusb_mtk_id_get(data, &dev_id);
3204 bt_dev_err(hdev, "Failed to get device id (%d)", err);
3210 fwname = FIRMWARE_MT7663;
3213 fwname = FIRMWARE_MT7668;
3216 bt_dev_err(hdev, "Unsupported support hardware variant (%08x)",
3221 /* Query whether the firmware is already download */
3222 wmt_params.op = BTMTK_WMT_SEMAPHORE;
3223 wmt_params.flag = 1;
3224 wmt_params.dlen = 0;
3225 wmt_params.data = NULL;
3226 wmt_params.status = &status;
3228 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3230 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
3234 if (status == BTMTK_WMT_PATCH_DONE) {
3235 bt_dev_info(hdev, "firmware already downloaded");
3236 goto ignore_setup_fw;
3239 /* Setup a firmware which the device definitely requires */
3240 err = btusb_mtk_setup_firmware(hdev, fwname);
3245 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
3246 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
3248 /* -ETIMEDOUT happens */
3252 /* The other errors happen in btusb_mtk_func_query */
3256 if (status == BTMTK_WMT_ON_DONE) {
3257 bt_dev_info(hdev, "function already on");
3258 goto ignore_func_on;
3261 /* Enable Bluetooth protocol */
3263 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3264 wmt_params.flag = 0;
3265 wmt_params.dlen = sizeof(param);
3266 wmt_params.data = ¶m;
3267 wmt_params.status = NULL;
3269 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3271 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3276 /* Apply the low power environment setup */
3277 tci_sleep.mode = 0x5;
3278 tci_sleep.duration = cpu_to_le16(0x640);
3279 tci_sleep.host_duration = cpu_to_le16(0x640);
3280 tci_sleep.host_wakeup_pin = 0;
3281 tci_sleep.time_compensation = 0;
3283 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
3287 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
3292 rettime = ktime_get();
3293 delta = ktime_sub(rettime, calltime);
3294 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3296 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
3301 static int btusb_mtk_shutdown(struct hci_dev *hdev)
3303 struct btmtk_hci_wmt_params wmt_params;
3307 /* Disable the device */
3308 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3309 wmt_params.flag = 0;
3310 wmt_params.dlen = sizeof(param);
3311 wmt_params.data = ¶m;
3312 wmt_params.status = NULL;
3314 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3316 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3323 MODULE_FIRMWARE(FIRMWARE_MT7663);
3324 MODULE_FIRMWARE(FIRMWARE_MT7668);
3327 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3328 static int marvell_config_oob_wake(struct hci_dev *hdev)
3330 struct sk_buff *skb;
3331 struct btusb_data *data = hci_get_drvdata(hdev);
3332 struct device *dev = &data->udev->dev;
3333 u16 pin, gap, opcode;
3337 /* Move on if no wakeup pin specified */
3338 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3339 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3342 /* Vendor specific command to configure a GPIO as wake-up pin */
3343 opcode = hci_opcode_pack(0x3F, 0x59);
3344 cmd[0] = opcode & 0xFF;
3345 cmd[1] = opcode >> 8;
3346 cmd[2] = 2; /* length of parameters that follow */
3348 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3350 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3352 bt_dev_err(hdev, "%s: No memory\n", __func__);
3356 skb_put_data(skb, cmd, sizeof(cmd));
3357 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3359 ret = btusb_send_frame(hdev, skb);
3361 bt_dev_err(hdev, "%s: configuration failed\n", __func__);
3370 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3371 const bdaddr_t *bdaddr)
3373 struct sk_buff *skb;
3378 buf[1] = sizeof(bdaddr_t);
3379 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3381 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3384 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3393 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3394 const bdaddr_t *bdaddr)
3396 struct sk_buff *skb;
3403 buf[3] = sizeof(bdaddr_t);
3404 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3406 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3409 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3417 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3418 const bdaddr_t *bdaddr)
3420 struct sk_buff *skb;
3424 memcpy(buf, bdaddr, sizeof(bdaddr_t));
3426 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3427 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3430 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3438 #define QCA_DFU_PACKET_LEN 4096
3440 #define QCA_GET_TARGET_VERSION 0x09
3441 #define QCA_CHECK_STATUS 0x05
3442 #define QCA_DFU_DOWNLOAD 0x01
3444 #define QCA_SYSCFG_UPDATED 0x40
3445 #define QCA_PATCH_UPDATED 0x80
3446 #define QCA_DFU_TIMEOUT 3000
3448 struct qca_version {
3450 __le32 patch_version;
3456 struct qca_rampatch_version {
3457 __le16 rom_version_high;
3458 __le16 rom_version_low;
3459 __le16 patch_version;
3462 struct qca_device_info {
3464 u8 rampatch_hdr; /* length of header in rampatch */
3465 u8 nvm_hdr; /* length of header in NVM */
3466 u8 ver_offset; /* offset of version structure in rampatch */
3469 static const struct qca_device_info qca_devices_table[] = {
3470 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3471 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3472 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3473 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3474 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3475 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3476 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3477 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3480 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3481 void *data, u16 size)
3486 buf = kmalloc(size, GFP_KERNEL);
3490 /* Found some of USB hosts have IOT issues with ours so that we should
3491 * not wait until HCI layer is ready.
3493 pipe = usb_rcvctrlpipe(udev, 0);
3494 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3495 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3497 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3501 memcpy(data, buf, size);
3509 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3510 const struct firmware *firmware,
3513 struct btusb_data *btdata = hci_get_drvdata(hdev);
3514 struct usb_device *udev = btdata->udev;
3515 size_t count, size, sent = 0;
3519 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3523 count = firmware->size;
3525 size = min_t(size_t, count, hdr_size);
3526 memcpy(buf, firmware->data, size);
3528 /* USB patches should go down to controller through USB path
3529 * because binary format fits to go down through USB channel.
3530 * USB control path is for patching headers and USB bulk is for
3533 pipe = usb_sndctrlpipe(udev, 0);
3534 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3535 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3537 bt_dev_err(hdev, "Failed to send headers (%d)", err);
3545 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3547 memcpy(buf, firmware->data + sent, size);
3549 pipe = usb_sndbulkpipe(udev, 0x02);
3550 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3553 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3554 sent, firmware->size, err);
3559 bt_dev_err(hdev, "Failed to get bulk buffer");
3573 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3574 struct qca_version *ver,
3575 const struct qca_device_info *info)
3577 struct qca_rampatch_version *rver;
3578 const struct firmware *fw;
3579 u32 ver_rom, ver_patch, rver_rom;
3580 u16 rver_rom_low, rver_rom_high, rver_patch;
3584 ver_rom = le32_to_cpu(ver->rom_version);
3585 ver_patch = le32_to_cpu(ver->patch_version);
3587 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
3589 err = request_firmware(&fw, fwname, &hdev->dev);
3591 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3596 bt_dev_info(hdev, "using rampatch file: %s", fwname);
3598 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3599 rver_rom_low = le16_to_cpu(rver->rom_version_low);
3600 rver_patch = le16_to_cpu(rver->patch_version);
3602 if (ver_rom & ~0xffffU) {
3603 rver_rom_high = le16_to_cpu(rver->rom_version_high);
3604 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
3606 rver_rom = rver_rom_low;
3609 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3610 "firmware rome 0x%x build 0x%x",
3611 rver_rom, rver_patch, ver_rom, ver_patch);
3613 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
3614 bt_dev_err(hdev, "rampatch file version did not match with firmware");
3619 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3622 release_firmware(fw);
3627 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3628 struct qca_version *ver,
3629 const struct qca_device_info *info)
3631 const struct firmware *fw;
3635 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
3636 le32_to_cpu(ver->rom_version));
3638 err = request_firmware(&fw, fwname, &hdev->dev);
3640 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3645 bt_dev_info(hdev, "using NVM file: %s", fwname);
3647 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3649 release_firmware(fw);
3654 /* identify the ROM version and check whether patches are needed */
3655 static bool btusb_qca_need_patch(struct usb_device *udev)
3657 struct qca_version ver;
3659 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3662 /* only low ROM versions need patches */
3663 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3666 static int btusb_setup_qca(struct hci_dev *hdev)
3668 struct btusb_data *btdata = hci_get_drvdata(hdev);
3669 struct usb_device *udev = btdata->udev;
3670 const struct qca_device_info *info = NULL;
3671 struct qca_version ver;
3676 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3681 ver_rom = le32_to_cpu(ver.rom_version);
3683 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
3684 if (ver_rom == qca_devices_table[i].rom_version)
3685 info = &qca_devices_table[i];
3688 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3692 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3697 if (!(status & QCA_PATCH_UPDATED)) {
3698 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3703 if (!(status & QCA_SYSCFG_UPDATED)) {
3704 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3712 static inline int __set_diag_interface(struct hci_dev *hdev)
3714 struct btusb_data *data = hci_get_drvdata(hdev);
3715 struct usb_interface *intf = data->diag;
3721 data->diag_tx_ep = NULL;
3722 data->diag_rx_ep = NULL;
3724 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3725 struct usb_endpoint_descriptor *ep_desc;
3727 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3729 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3730 data->diag_tx_ep = ep_desc;
3734 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3735 data->diag_rx_ep = ep_desc;
3740 if (!data->diag_tx_ep || !data->diag_rx_ep) {
3741 bt_dev_err(hdev, "invalid diagnostic descriptors");
3748 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3750 struct btusb_data *data = hci_get_drvdata(hdev);
3751 struct sk_buff *skb;
3755 if (!data->diag_tx_ep)
3756 return ERR_PTR(-ENODEV);
3758 urb = usb_alloc_urb(0, GFP_KERNEL);
3760 return ERR_PTR(-ENOMEM);
3762 skb = bt_skb_alloc(2, GFP_KERNEL);
3765 return ERR_PTR(-ENOMEM);
3768 skb_put_u8(skb, 0xf0);
3769 skb_put_u8(skb, enable);
3771 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3773 usb_fill_bulk_urb(urb, data->udev, pipe,
3774 skb->data, skb->len, btusb_tx_complete, skb);
3776 skb->dev = (void *)hdev;
3781 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3783 struct btusb_data *data = hci_get_drvdata(hdev);
3789 if (!test_bit(HCI_RUNNING, &hdev->flags))
3792 urb = alloc_diag_urb(hdev, enable);
3794 return PTR_ERR(urb);
3796 return submit_or_queue_tx_urb(hdev, urb);
3800 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3802 struct btusb_data *data = priv;
3804 pm_wakeup_event(&data->udev->dev, 0);
3807 /* Disable only if not already disabled (keep it balanced) */
3808 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3809 disable_irq_nosync(irq);
3810 disable_irq_wake(irq);
3815 static const struct of_device_id btusb_match_table[] = {
3816 { .compatible = "usb1286,204e" },
3817 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3818 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
3821 MODULE_DEVICE_TABLE(of, btusb_match_table);
3823 /* Use an oob wakeup pin? */
3824 static int btusb_config_oob_wake(struct hci_dev *hdev)
3826 struct btusb_data *data = hci_get_drvdata(hdev);
3827 struct device *dev = &data->udev->dev;
3830 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3832 if (!of_match_device(btusb_match_table, dev))
3835 /* Move on if no IRQ specified */
3836 irq = of_irq_get_byname(dev->of_node, "wakeup");
3838 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
3842 irq_set_status_flags(irq, IRQ_NOAUTOEN);
3843 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
3844 0, "OOB Wake-on-BT", data);
3846 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
3850 ret = device_init_wakeup(dev, true);
3852 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
3856 data->oob_wake_irq = irq;
3857 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
3862 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
3864 if (dmi_check_system(btusb_needs_reset_resume_table))
3865 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3868 static bool btusb_prevent_wake(struct hci_dev *hdev)
3870 struct btusb_data *data = hci_get_drvdata(hdev);
3872 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
3875 return !device_may_wakeup(&data->udev->dev);
3878 static int btusb_probe(struct usb_interface *intf,
3879 const struct usb_device_id *id)
3881 struct usb_endpoint_descriptor *ep_desc;
3882 struct gpio_desc *reset_gpio;
3883 struct btusb_data *data;
3884 struct hci_dev *hdev;
3885 unsigned ifnum_base;
3888 BT_DBG("intf %p id %p", intf, id);
3890 /* interface numbers are hardcoded in the spec */
3891 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
3892 if (!(id->driver_info & BTUSB_IFNUM_2))
3894 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
3898 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
3900 if (!id->driver_info) {
3901 const struct usb_device_id *match;
3903 match = usb_match_id(intf, blacklist_table);
3908 if (id->driver_info == BTUSB_IGNORE)
3911 if (id->driver_info & BTUSB_ATH3012) {
3912 struct usb_device *udev = interface_to_usbdev(intf);
3914 /* Old firmware would otherwise let ath3k driver load
3915 * patch and sysconfig files
3917 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
3918 !btusb_qca_need_patch(udev))
3922 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
3926 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3927 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3929 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3930 data->intr_ep = ep_desc;
3934 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3935 data->bulk_tx_ep = ep_desc;
3939 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3940 data->bulk_rx_ep = ep_desc;
3945 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
3948 if (id->driver_info & BTUSB_AMP) {
3949 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3950 data->cmdreq = 0x2b;
3952 data->cmdreq_type = USB_TYPE_CLASS;
3953 data->cmdreq = 0x00;
3956 data->udev = interface_to_usbdev(intf);
3959 INIT_WORK(&data->work, btusb_work);
3960 INIT_WORK(&data->waker, btusb_waker);
3961 init_usb_anchor(&data->deferred);
3962 init_usb_anchor(&data->tx_anchor);
3963 spin_lock_init(&data->txlock);
3965 init_usb_anchor(&data->intr_anchor);
3966 init_usb_anchor(&data->bulk_anchor);
3967 init_usb_anchor(&data->isoc_anchor);
3968 init_usb_anchor(&data->diag_anchor);
3969 init_usb_anchor(&data->ctrl_anchor);
3970 spin_lock_init(&data->rxlock);
3972 if (id->driver_info & BTUSB_INTEL_NEW) {
3973 data->recv_event = btusb_recv_event_intel;
3974 data->recv_bulk = btusb_recv_bulk_intel;
3975 set_bit(BTUSB_BOOTLOADER, &data->flags);
3977 data->recv_event = hci_recv_frame;
3978 data->recv_bulk = btusb_recv_bulk;
3981 hdev = hci_alloc_dev();
3985 hdev->bus = HCI_USB;
3986 hci_set_drvdata(hdev, data);
3988 if (id->driver_info & BTUSB_AMP)
3989 hdev->dev_type = HCI_AMP;
3991 hdev->dev_type = HCI_PRIMARY;
3995 SET_HCIDEV_DEV(hdev, &intf->dev);
3997 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
3999 if (IS_ERR(reset_gpio)) {
4000 err = PTR_ERR(reset_gpio);
4002 } else if (reset_gpio) {
4003 data->reset_gpio = reset_gpio;
4006 hdev->open = btusb_open;
4007 hdev->close = btusb_close;
4008 hdev->flush = btusb_flush;
4009 hdev->send = btusb_send_frame;
4010 hdev->notify = btusb_notify;
4011 hdev->prevent_wake = btusb_prevent_wake;
4014 err = btusb_config_oob_wake(hdev);
4018 /* Marvell devices may need a specific chip configuration */
4019 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
4020 err = marvell_config_oob_wake(hdev);
4025 if (id->driver_info & BTUSB_CW6622)
4026 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4028 if (id->driver_info & BTUSB_BCM2045)
4029 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4031 if (id->driver_info & BTUSB_BCM92035)
4032 hdev->setup = btusb_setup_bcm92035;
4034 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4035 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
4036 hdev->manufacturer = 15;
4037 hdev->setup = btbcm_setup_patchram;
4038 hdev->set_diag = btusb_bcm_set_diag;
4039 hdev->set_bdaddr = btbcm_set_bdaddr;
4041 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4042 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4045 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4046 (id->driver_info & BTUSB_BCM_APPLE)) {
4047 hdev->manufacturer = 15;
4048 hdev->setup = btbcm_setup_apple;
4049 hdev->set_diag = btusb_bcm_set_diag;
4051 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4052 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4055 if (id->driver_info & BTUSB_INTEL) {
4056 hdev->manufacturer = 2;
4057 hdev->setup = btusb_setup_intel;
4058 hdev->shutdown = btusb_shutdown_intel;
4059 hdev->set_diag = btintel_set_diag_mfg;
4060 hdev->set_bdaddr = btintel_set_bdaddr;
4061 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4062 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4063 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4064 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4067 if (id->driver_info & BTUSB_INTEL_NEW) {
4068 hdev->manufacturer = 2;
4069 hdev->send = btusb_send_frame_intel;
4070 hdev->setup = btusb_setup_intel_new;
4071 hdev->shutdown = btusb_shutdown_intel_new;
4072 hdev->hw_error = btintel_hw_error;
4073 hdev->set_diag = btintel_set_diag;
4074 hdev->set_bdaddr = btintel_set_bdaddr;
4075 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4076 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4077 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4078 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4081 if (id->driver_info & BTUSB_MARVELL)
4082 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4084 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4085 (id->driver_info & BTUSB_MEDIATEK)) {
4086 hdev->setup = btusb_mtk_setup;
4087 hdev->shutdown = btusb_mtk_shutdown;
4088 hdev->manufacturer = 70;
4089 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
4092 if (id->driver_info & BTUSB_SWAVE) {
4093 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
4094 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
4097 if (id->driver_info & BTUSB_INTEL_BOOT) {
4098 hdev->manufacturer = 2;
4099 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4102 if (id->driver_info & BTUSB_ATH3012) {
4103 data->setup_on_usb = btusb_setup_qca;
4104 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4105 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4106 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4109 if (id->driver_info & BTUSB_QCA_ROME) {
4110 data->setup_on_usb = btusb_setup_qca;
4111 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4112 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4113 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4114 btusb_check_needs_reset_resume(intf);
4117 if (id->driver_info & BTUSB_QCA_WCN6855) {
4118 data->setup_on_usb = btusb_setup_qca;
4119 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
4120 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4121 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4124 if (id->driver_info & BTUSB_AMP) {
4125 /* AMP controllers do not support SCO packets */
4128 /* Interface orders are hardcoded in the specification */
4129 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
4130 data->isoc_ifnum = ifnum_base + 1;
4133 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4134 (id->driver_info & BTUSB_REALTEK)) {
4135 hdev->setup = btrtl_setup_realtek;
4136 hdev->shutdown = btrtl_shutdown_realtek;
4137 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
4139 /* Realtek devices lose their updated firmware over global
4140 * suspend that means host doesn't send SET_FEATURE
4141 * (DEVICE_REMOTE_WAKEUP)
4143 set_bit(BTUSB_WAKEUP_DISABLE, &data->flags);
4144 if (btusb_find_altsetting(data, 1))
4145 set_bit(BTUSB_USE_ALT1_FOR_WBS, &data->flags);
4147 bt_dev_err(hdev, "Device does not support ALT setting 1");
4151 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4153 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
4154 if (!disable_scofix)
4155 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
4158 if (id->driver_info & BTUSB_BROKEN_ISOC)
4161 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
4162 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
4164 if (id->driver_info & BTUSB_VALID_LE_STATES)
4165 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
4167 if (id->driver_info & BTUSB_DIGIANSWER) {
4168 data->cmdreq_type = USB_TYPE_VENDOR;
4169 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4172 if (id->driver_info & BTUSB_CSR) {
4173 struct usb_device *udev = data->udev;
4174 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
4176 /* Old firmware would otherwise execute USB reset */
4177 if (bcdDevice < 0x117)
4178 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4180 /* This must be set first in case we disable it for fakes */
4181 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4183 /* Fake CSR devices with broken commands */
4184 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
4185 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4186 hdev->setup = btusb_setup_csr;
4189 if (id->driver_info & BTUSB_SNIFFER) {
4190 struct usb_device *udev = data->udev;
4192 /* New sniffer firmware has crippled HCI interface */
4193 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4194 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4197 if (id->driver_info & BTUSB_INTEL_BOOT) {
4198 /* A bug in the bootloader causes that interrupt interface is
4199 * only enabled after receiving SetInterface(0, AltSetting=0).
4201 err = usb_set_interface(data->udev, 0, 0);
4203 BT_ERR("failed to set interface 0, alt 0 %d", err);
4209 err = usb_driver_claim_interface(&btusb_driver,
4215 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
4216 if (!usb_driver_claim_interface(&btusb_driver,
4218 __set_diag_interface(hdev);
4223 if (enable_autosuspend)
4224 usb_enable_autosuspend(data->udev);
4226 err = hci_register_dev(hdev);
4230 usb_set_intfdata(intf, data);
4235 if (data->reset_gpio)
4236 gpiod_put(data->reset_gpio);
4241 static void btusb_disconnect(struct usb_interface *intf)
4243 struct btusb_data *data = usb_get_intfdata(intf);
4244 struct hci_dev *hdev;
4246 BT_DBG("intf %p", intf);
4252 usb_set_intfdata(data->intf, NULL);
4255 usb_set_intfdata(data->isoc, NULL);
4258 usb_set_intfdata(data->diag, NULL);
4260 hci_unregister_dev(hdev);
4262 if (intf == data->intf) {
4264 usb_driver_release_interface(&btusb_driver, data->isoc);
4266 usb_driver_release_interface(&btusb_driver, data->diag);
4267 } else if (intf == data->isoc) {
4269 usb_driver_release_interface(&btusb_driver, data->diag);
4270 usb_driver_release_interface(&btusb_driver, data->intf);
4271 } else if (intf == data->diag) {
4272 usb_driver_release_interface(&btusb_driver, data->intf);
4274 usb_driver_release_interface(&btusb_driver, data->isoc);
4277 if (data->oob_wake_irq)
4278 device_init_wakeup(&data->udev->dev, false);
4280 if (data->reset_gpio)
4281 gpiod_put(data->reset_gpio);
4287 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4289 struct btusb_data *data = usb_get_intfdata(intf);
4291 BT_DBG("intf %p", intf);
4293 if (data->suspend_count++)
4296 spin_lock_irq(&data->txlock);
4297 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4298 set_bit(BTUSB_SUSPENDING, &data->flags);
4299 spin_unlock_irq(&data->txlock);
4301 spin_unlock_irq(&data->txlock);
4302 data->suspend_count--;
4306 cancel_work_sync(&data->work);
4308 btusb_stop_traffic(data);
4309 usb_kill_anchored_urbs(&data->tx_anchor);
4311 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4312 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4313 enable_irq_wake(data->oob_wake_irq);
4314 enable_irq(data->oob_wake_irq);
4317 /* For global suspend, Realtek devices lose the loaded fw
4318 * in them. But for autosuspend, firmware should remain.
4319 * Actually, it depends on whether the usb host sends
4320 * set feature (enable wakeup) or not.
4322 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags)) {
4323 if (PMSG_IS_AUTO(message) &&
4324 device_can_wakeup(&data->udev->dev))
4325 data->udev->do_remote_wakeup = 1;
4326 else if (!PMSG_IS_AUTO(message))
4327 data->udev->reset_resume = 1;
4333 static void play_deferred(struct btusb_data *data)
4338 while ((urb = usb_get_from_anchor(&data->deferred))) {
4339 usb_anchor_urb(urb, &data->tx_anchor);
4341 err = usb_submit_urb(urb, GFP_ATOMIC);
4343 if (err != -EPERM && err != -ENODEV)
4344 BT_ERR("%s urb %p submission failed (%d)",
4345 data->hdev->name, urb, -err);
4346 kfree(urb->setup_packet);
4347 usb_unanchor_urb(urb);
4352 data->tx_in_flight++;
4356 /* Cleanup the rest deferred urbs. */
4357 while ((urb = usb_get_from_anchor(&data->deferred))) {
4358 kfree(urb->setup_packet);
4363 static int btusb_resume(struct usb_interface *intf)
4365 struct btusb_data *data = usb_get_intfdata(intf);
4366 struct hci_dev *hdev = data->hdev;
4369 BT_DBG("intf %p", intf);
4371 if (--data->suspend_count)
4374 /* Disable only if not already disabled (keep it balanced) */
4375 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4376 disable_irq(data->oob_wake_irq);
4377 disable_irq_wake(data->oob_wake_irq);
4380 if (!test_bit(HCI_RUNNING, &hdev->flags))
4383 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4384 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4386 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4391 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4392 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4394 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4398 btusb_submit_bulk_urb(hdev, GFP_NOIO);
4401 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4402 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4403 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4405 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4408 spin_lock_irq(&data->txlock);
4409 play_deferred(data);
4410 clear_bit(BTUSB_SUSPENDING, &data->flags);
4411 spin_unlock_irq(&data->txlock);
4412 schedule_work(&data->work);
4417 usb_scuttle_anchored_urbs(&data->deferred);
4419 spin_lock_irq(&data->txlock);
4420 clear_bit(BTUSB_SUSPENDING, &data->flags);
4421 spin_unlock_irq(&data->txlock);
4427 static struct usb_driver btusb_driver = {
4429 .probe = btusb_probe,
4430 .disconnect = btusb_disconnect,
4432 .suspend = btusb_suspend,
4433 .resume = btusb_resume,
4435 .id_table = btusb_table,
4436 .supports_autosuspend = 1,
4437 .disable_hub_initiated_lpm = 1,
4440 module_usb_driver(btusb_driver);
4442 module_param(disable_scofix, bool, 0644);
4443 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4445 module_param(force_scofix, bool, 0644);
4446 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4448 module_param(enable_autosuspend, bool, 0644);
4449 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4451 module_param(reset, bool, 0644);
4452 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4455 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4456 MODULE_VERSION(VERSION);
4457 MODULE_LICENSE("GPL");