1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Generic Bluetooth USB driver
6 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
10 #include <linux/module.h>
11 #include <linux/usb.h>
12 #include <linux/usb/quirks.h>
13 #include <linux/firmware.h>
14 #include <linux/iopoll.h>
15 #include <linux/of_device.h>
16 #include <linux/of_irq.h>
17 #include <linux/suspend.h>
18 #include <linux/gpio/consumer.h>
19 #include <asm/unaligned.h>
21 #include <net/bluetooth/bluetooth.h>
22 #include <net/bluetooth/hci_core.h>
30 static bool disable_scofix;
31 static bool force_scofix;
32 static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
34 static bool reset = true;
36 static struct usb_driver btusb_driver;
38 #define BTUSB_IGNORE 0x01
39 #define BTUSB_DIGIANSWER 0x02
40 #define BTUSB_CSR 0x04
41 #define BTUSB_SNIFFER 0x08
42 #define BTUSB_BCM92035 0x10
43 #define BTUSB_BROKEN_ISOC 0x20
44 #define BTUSB_WRONG_SCO_MTU 0x40
45 #define BTUSB_ATH3012 0x80
46 #define BTUSB_INTEL 0x100
47 #define BTUSB_INTEL_BOOT 0x200
48 #define BTUSB_BCM_PATCHRAM 0x400
49 #define BTUSB_MARVELL 0x800
50 #define BTUSB_SWAVE 0x1000
51 #define BTUSB_INTEL_NEW 0x2000
52 #define BTUSB_AMP 0x4000
53 #define BTUSB_QCA_ROME 0x8000
54 #define BTUSB_BCM_APPLE 0x10000
55 #define BTUSB_REALTEK 0x20000
56 #define BTUSB_BCM2045 0x40000
57 #define BTUSB_IFNUM_2 0x80000
58 #define BTUSB_CW6622 0x100000
59 #define BTUSB_MEDIATEK 0x200000
60 #define BTUSB_WIDEBAND_SPEECH 0x400000
61 #define BTUSB_VALID_LE_STATES 0x800000
62 #define BTUSB_QCA_WCN6855 0x1000000
63 #define BTUSB_INTEL_NEWGEN 0x2000000
65 static const struct usb_device_id btusb_table[] = {
66 /* Generic Bluetooth USB device */
67 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
69 /* Generic Bluetooth AMP device */
70 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
72 /* Generic Bluetooth USB interface */
73 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
75 /* Apple-specific (Broadcom) devices */
76 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
77 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
79 /* MediaTek MT76x0E */
80 { USB_DEVICE(0x0e8d, 0x763f) },
82 /* Broadcom SoftSailing reporting vendor specific */
83 { USB_DEVICE(0x0a5c, 0x21e1) },
85 /* Apple MacBookPro 7,1 */
86 { USB_DEVICE(0x05ac, 0x8213) },
89 { USB_DEVICE(0x05ac, 0x8215) },
91 /* Apple MacBookPro6,2 */
92 { USB_DEVICE(0x05ac, 0x8218) },
94 /* Apple MacBookAir3,1, MacBookAir3,2 */
95 { USB_DEVICE(0x05ac, 0x821b) },
97 /* Apple MacBookAir4,1 */
98 { USB_DEVICE(0x05ac, 0x821f) },
100 /* Apple MacBookPro8,2 */
101 { USB_DEVICE(0x05ac, 0x821a) },
103 /* Apple MacMini5,1 */
104 { USB_DEVICE(0x05ac, 0x8281) },
106 /* AVM BlueFRITZ! USB v2.0 */
107 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
109 /* Bluetooth Ultraport Module from IBM */
110 { USB_DEVICE(0x04bf, 0x030a) },
112 /* ALPS Modules with non-standard id */
113 { USB_DEVICE(0x044e, 0x3001) },
114 { USB_DEVICE(0x044e, 0x3002) },
116 /* Ericsson with non-standard id */
117 { USB_DEVICE(0x0bdb, 0x1002) },
119 /* Canyon CN-BTU1 with HID interfaces */
120 { USB_DEVICE(0x0c10, 0x0000) },
122 /* Broadcom BCM20702A0 */
123 { USB_DEVICE(0x413c, 0x8197) },
125 /* Broadcom BCM20702B0 (Dynex/Insignia) */
126 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
128 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
129 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
130 .driver_info = BTUSB_BCM_PATCHRAM },
132 /* Broadcom BCM920703 (HTC Vive) */
133 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
134 .driver_info = BTUSB_BCM_PATCHRAM },
136 /* Foxconn - Hon Hai */
137 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
138 .driver_info = BTUSB_BCM_PATCHRAM },
140 /* Lite-On Technology - Broadcom based */
141 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
142 .driver_info = BTUSB_BCM_PATCHRAM },
144 /* Broadcom devices with vendor specific id */
145 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
146 .driver_info = BTUSB_BCM_PATCHRAM },
148 /* ASUSTek Computer - Broadcom based */
149 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
150 .driver_info = BTUSB_BCM_PATCHRAM },
152 /* Belkin F8065bf - Broadcom based */
153 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
154 .driver_info = BTUSB_BCM_PATCHRAM },
156 /* IMC Networks - Broadcom based */
157 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
158 .driver_info = BTUSB_BCM_PATCHRAM },
160 /* Dell Computer - Broadcom based */
161 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
162 .driver_info = BTUSB_BCM_PATCHRAM },
164 /* Toshiba Corp - Broadcom based */
165 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
166 .driver_info = BTUSB_BCM_PATCHRAM },
168 /* Intel Bluetooth USB Bootloader (RAM module) */
169 { USB_DEVICE(0x8087, 0x0a5a),
170 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
172 { } /* Terminating entry */
175 MODULE_DEVICE_TABLE(usb, btusb_table);
177 static const struct usb_device_id blacklist_table[] = {
178 /* CSR BlueCore devices */
179 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
181 /* Broadcom BCM2033 without firmware */
182 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
184 /* Broadcom BCM2045 devices */
185 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
187 /* Atheros 3011 with sflash firmware */
188 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
189 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
190 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
191 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
192 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
193 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
194 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
196 /* Atheros AR9285 Malbec with sflash firmware */
197 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
199 /* Atheros 3012 with sflash firmware */
200 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
246 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
247 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
248 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
249 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
251 /* Atheros AR5BBU12 with sflash firmware */
252 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
254 /* Atheros AR5BBU12 with sflash firmware */
255 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
256 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
258 /* QCA ROME chipset */
259 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
260 BTUSB_WIDEBAND_SPEECH },
261 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
262 BTUSB_WIDEBAND_SPEECH },
263 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
264 BTUSB_WIDEBAND_SPEECH },
265 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
266 BTUSB_WIDEBAND_SPEECH },
267 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
268 BTUSB_WIDEBAND_SPEECH },
269 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
270 BTUSB_WIDEBAND_SPEECH },
271 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
272 BTUSB_WIDEBAND_SPEECH },
273 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
274 BTUSB_WIDEBAND_SPEECH },
275 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
276 BTUSB_WIDEBAND_SPEECH },
277 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
278 BTUSB_WIDEBAND_SPEECH },
279 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
280 BTUSB_WIDEBAND_SPEECH },
281 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
282 BTUSB_WIDEBAND_SPEECH },
283 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
284 BTUSB_WIDEBAND_SPEECH },
285 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
286 BTUSB_WIDEBAND_SPEECH },
287 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
288 BTUSB_WIDEBAND_SPEECH },
289 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
290 BTUSB_WIDEBAND_SPEECH },
291 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
292 BTUSB_WIDEBAND_SPEECH },
293 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
294 BTUSB_WIDEBAND_SPEECH },
296 /* QCA WCN6855 chipset */
297 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
298 BTUSB_WIDEBAND_SPEECH },
300 /* Broadcom BCM2035 */
301 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
302 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
303 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
305 /* Broadcom BCM2045 */
306 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
307 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
309 /* IBM/Lenovo ThinkPad with Broadcom chip */
310 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
311 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
313 /* HP laptop with Broadcom chip */
314 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
316 /* Dell laptop with Broadcom chip */
317 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
319 /* Dell Wireless 370 and 410 devices */
320 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
321 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
323 /* Belkin F8T012 and F8T013 devices */
324 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
325 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
327 /* Asus WL-BTD202 device */
328 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
330 /* Kensington Bluetooth USB adapter */
331 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
333 /* RTX Telecom based adapters with buggy SCO support */
334 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
335 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
337 /* CONWISE Technology based adapters with buggy SCO support */
338 { USB_DEVICE(0x0e5e, 0x6622),
339 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
341 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
342 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
344 /* Digianswer devices */
345 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
346 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
348 /* CSR BlueCore Bluetooth Sniffer */
349 { USB_DEVICE(0x0a12, 0x0002),
350 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
352 /* Frontline ComProbe Bluetooth Sniffer */
353 { USB_DEVICE(0x16d3, 0x0002),
354 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
356 /* Marvell Bluetooth devices */
357 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
358 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
359 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
361 /* Intel Bluetooth devices */
362 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW |
363 BTUSB_WIDEBAND_SPEECH |
364 BTUSB_VALID_LE_STATES },
365 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_NEW |
366 BTUSB_WIDEBAND_SPEECH },
367 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_NEW |
368 BTUSB_WIDEBAND_SPEECH },
369 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_NEW |
370 BTUSB_WIDEBAND_SPEECH},
371 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
372 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
373 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
374 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW |
375 BTUSB_WIDEBAND_SPEECH },
376 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL |
377 BTUSB_WIDEBAND_SPEECH },
378 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW |
379 BTUSB_WIDEBAND_SPEECH |
380 BTUSB_VALID_LE_STATES },
382 /* Other Intel Bluetooth devices */
383 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
384 .driver_info = BTUSB_IGNORE },
386 /* Realtek 8822CE Bluetooth devices */
387 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
388 BTUSB_WIDEBAND_SPEECH },
390 /* Realtek 8852AE Bluetooth devices */
391 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
392 BTUSB_WIDEBAND_SPEECH },
394 /* Realtek Bluetooth devices */
395 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
396 .driver_info = BTUSB_REALTEK },
398 /* MediaTek Bluetooth devices */
399 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
400 .driver_info = BTUSB_MEDIATEK },
402 /* Additional MediaTek MT7615E Bluetooth devices */
403 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
405 /* Additional Realtek 8723AE Bluetooth devices */
406 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
407 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
409 /* Additional Realtek 8723BE Bluetooth devices */
410 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
411 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
412 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
413 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
414 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
415 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
417 /* Additional Realtek 8723BU Bluetooth devices */
418 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
420 /* Additional Realtek 8723DE Bluetooth devices */
421 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
422 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
424 /* Additional Realtek 8821AE Bluetooth devices */
425 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
426 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
427 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
428 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
429 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
431 /* Additional Realtek 8822BE Bluetooth devices */
432 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
433 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
435 /* Additional Realtek 8822CE Bluetooth devices */
436 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
437 BTUSB_WIDEBAND_SPEECH },
438 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
439 BTUSB_WIDEBAND_SPEECH },
440 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
441 BTUSB_WIDEBAND_SPEECH },
442 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
443 BTUSB_WIDEBAND_SPEECH },
444 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
445 BTUSB_WIDEBAND_SPEECH },
446 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
447 BTUSB_WIDEBAND_SPEECH },
448 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
449 BTUSB_WIDEBAND_SPEECH },
450 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
451 BTUSB_WIDEBAND_SPEECH },
452 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
453 BTUSB_WIDEBAND_SPEECH },
454 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
455 BTUSB_WIDEBAND_SPEECH },
457 /* Silicon Wave based devices */
458 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
460 { } /* Terminating entry */
463 /* The Bluetooth USB module build into some devices needs to be reset on resume,
464 * this is a problem with the platform (likely shutting off all power) not with
465 * the module itself. So we use a DMI list to match known broken platforms.
467 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
469 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
471 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
472 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
476 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
478 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
479 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
483 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
485 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
486 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
492 #define BTUSB_MAX_ISOC_FRAMES 10
494 #define BTUSB_INTR_RUNNING 0
495 #define BTUSB_BULK_RUNNING 1
496 #define BTUSB_ISOC_RUNNING 2
497 #define BTUSB_SUSPENDING 3
498 #define BTUSB_DID_ISO_RESUME 4
499 #define BTUSB_BOOTLOADER 5
500 #define BTUSB_DOWNLOADING 6
501 #define BTUSB_FIRMWARE_LOADED 7
502 #define BTUSB_FIRMWARE_FAILED 8
503 #define BTUSB_BOOTING 9
504 #define BTUSB_DIAG_RUNNING 10
505 #define BTUSB_OOB_WAKE_ENABLED 11
506 #define BTUSB_HW_RESET_ACTIVE 12
507 #define BTUSB_TX_WAIT_VND_EVT 13
508 #define BTUSB_WAKEUP_DISABLE 14
509 #define BTUSB_USE_ALT1_FOR_WBS 15
512 struct hci_dev *hdev;
513 struct usb_device *udev;
514 struct usb_interface *intf;
515 struct usb_interface *isoc;
516 struct usb_interface *diag;
521 struct work_struct work;
522 struct work_struct waker;
524 struct usb_anchor deferred;
525 struct usb_anchor tx_anchor;
529 struct usb_anchor intr_anchor;
530 struct usb_anchor bulk_anchor;
531 struct usb_anchor isoc_anchor;
532 struct usb_anchor diag_anchor;
533 struct usb_anchor ctrl_anchor;
536 struct sk_buff *evt_skb;
537 struct sk_buff *acl_skb;
538 struct sk_buff *sco_skb;
540 struct usb_endpoint_descriptor *intr_ep;
541 struct usb_endpoint_descriptor *bulk_tx_ep;
542 struct usb_endpoint_descriptor *bulk_rx_ep;
543 struct usb_endpoint_descriptor *isoc_tx_ep;
544 struct usb_endpoint_descriptor *isoc_rx_ep;
545 struct usb_endpoint_descriptor *diag_tx_ep;
546 struct usb_endpoint_descriptor *diag_rx_ep;
548 struct gpio_desc *reset_gpio;
553 unsigned int sco_num;
554 unsigned int air_mode;
555 bool usb_alt6_packet_flow;
559 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
560 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
562 int (*setup_on_usb)(struct hci_dev *hdev);
564 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
565 unsigned cmd_timeout_cnt;
568 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
570 struct btusb_data *data = hci_get_drvdata(hdev);
571 struct gpio_desc *reset_gpio = data->reset_gpio;
573 if (++data->cmd_timeout_cnt < 5)
577 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
582 * Toggle the hard reset line if the platform provides one. The reset
583 * is going to yank the device off the USB and then replug. So doing
584 * once is enough. The cleanup is handled correctly on the way out
585 * (standard USB disconnect), and the new device is detected cleanly
586 * and bound to the driver again like it should be.
588 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
589 bt_dev_err(hdev, "last reset failed? Not resetting again");
593 bt_dev_err(hdev, "Initiating HW reset via gpio");
594 gpiod_set_value_cansleep(reset_gpio, 1);
596 gpiod_set_value_cansleep(reset_gpio, 0);
599 static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
601 struct btusb_data *data = hci_get_drvdata(hdev);
602 struct gpio_desc *reset_gpio = data->reset_gpio;
604 if (++data->cmd_timeout_cnt < 5)
608 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
612 /* Toggle the hard reset line. The Realtek device is going to
613 * yank itself off the USB and then replug. The cleanup is handled
614 * correctly on the way out (standard USB disconnect), and the new
615 * device is detected cleanly and bound to the driver again like
618 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
619 bt_dev_err(hdev, "last reset failed? Not resetting again");
623 bt_dev_err(hdev, "Reset Realtek device via gpio");
624 gpiod_set_value_cansleep(reset_gpio, 1);
626 gpiod_set_value_cansleep(reset_gpio, 0);
629 static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
631 struct btusb_data *data = hci_get_drvdata(hdev);
634 if (++data->cmd_timeout_cnt < 5)
637 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
638 /* This is not an unbalanced PM reference since the device will reset */
639 err = usb_autopm_get_interface(data->intf);
641 usb_queue_reset_device(data->intf);
643 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
646 static inline void btusb_free_frags(struct btusb_data *data)
650 spin_lock_irqsave(&data->rxlock, flags);
652 kfree_skb(data->evt_skb);
653 data->evt_skb = NULL;
655 kfree_skb(data->acl_skb);
656 data->acl_skb = NULL;
658 kfree_skb(data->sco_skb);
659 data->sco_skb = NULL;
661 spin_unlock_irqrestore(&data->rxlock, flags);
664 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
670 spin_lock_irqsave(&data->rxlock, flags);
677 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
683 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
684 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
687 len = min_t(uint, hci_skb_expect(skb), count);
688 skb_put_data(skb, buffer, len);
692 hci_skb_expect(skb) -= len;
694 if (skb->len == HCI_EVENT_HDR_SIZE) {
695 /* Complete event header */
696 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
698 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
707 if (!hci_skb_expect(skb)) {
709 data->recv_event(data->hdev, skb);
715 spin_unlock_irqrestore(&data->rxlock, flags);
720 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
726 spin_lock_irqsave(&data->rxlock, flags);
733 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
739 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
740 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
743 len = min_t(uint, hci_skb_expect(skb), count);
744 skb_put_data(skb, buffer, len);
748 hci_skb_expect(skb) -= len;
750 if (skb->len == HCI_ACL_HDR_SIZE) {
751 __le16 dlen = hci_acl_hdr(skb)->dlen;
753 /* Complete ACL header */
754 hci_skb_expect(skb) = __le16_to_cpu(dlen);
756 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
765 if (!hci_skb_expect(skb)) {
767 hci_recv_frame(data->hdev, skb);
773 spin_unlock_irqrestore(&data->rxlock, flags);
778 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
784 spin_lock_irqsave(&data->rxlock, flags);
791 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
797 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
798 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
801 len = min_t(uint, hci_skb_expect(skb), count);
802 skb_put_data(skb, buffer, len);
806 hci_skb_expect(skb) -= len;
808 if (skb->len == HCI_SCO_HDR_SIZE) {
809 /* Complete SCO header */
810 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
812 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
821 if (!hci_skb_expect(skb)) {
823 hci_recv_frame(data->hdev, skb);
829 spin_unlock_irqrestore(&data->rxlock, flags);
834 static void btusb_intr_complete(struct urb *urb)
836 struct hci_dev *hdev = urb->context;
837 struct btusb_data *data = hci_get_drvdata(hdev);
840 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
843 if (!test_bit(HCI_RUNNING, &hdev->flags))
846 if (urb->status == 0) {
847 hdev->stat.byte_rx += urb->actual_length;
849 if (btusb_recv_intr(data, urb->transfer_buffer,
850 urb->actual_length) < 0) {
851 bt_dev_err(hdev, "corrupted event packet");
854 } else if (urb->status == -ENOENT) {
855 /* Avoid suspend failed when usb_kill_urb */
859 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
862 usb_mark_last_busy(data->udev);
863 usb_anchor_urb(urb, &data->intr_anchor);
865 err = usb_submit_urb(urb, GFP_ATOMIC);
867 /* -EPERM: urb is being killed;
868 * -ENODEV: device got disconnected
870 if (err != -EPERM && err != -ENODEV)
871 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
873 usb_unanchor_urb(urb);
877 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
879 struct btusb_data *data = hci_get_drvdata(hdev);
885 BT_DBG("%s", hdev->name);
890 urb = usb_alloc_urb(0, mem_flags);
894 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
896 buf = kmalloc(size, mem_flags);
902 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
904 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
905 btusb_intr_complete, hdev, data->intr_ep->bInterval);
907 urb->transfer_flags |= URB_FREE_BUFFER;
909 usb_anchor_urb(urb, &data->intr_anchor);
911 err = usb_submit_urb(urb, mem_flags);
913 if (err != -EPERM && err != -ENODEV)
914 bt_dev_err(hdev, "urb %p submission failed (%d)",
916 usb_unanchor_urb(urb);
924 static void btusb_bulk_complete(struct urb *urb)
926 struct hci_dev *hdev = urb->context;
927 struct btusb_data *data = hci_get_drvdata(hdev);
930 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
933 if (!test_bit(HCI_RUNNING, &hdev->flags))
936 if (urb->status == 0) {
937 hdev->stat.byte_rx += urb->actual_length;
939 if (data->recv_bulk(data, urb->transfer_buffer,
940 urb->actual_length) < 0) {
941 bt_dev_err(hdev, "corrupted ACL packet");
944 } else if (urb->status == -ENOENT) {
945 /* Avoid suspend failed when usb_kill_urb */
949 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
952 usb_anchor_urb(urb, &data->bulk_anchor);
953 usb_mark_last_busy(data->udev);
955 err = usb_submit_urb(urb, GFP_ATOMIC);
957 /* -EPERM: urb is being killed;
958 * -ENODEV: device got disconnected
960 if (err != -EPERM && err != -ENODEV)
961 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
963 usb_unanchor_urb(urb);
967 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
969 struct btusb_data *data = hci_get_drvdata(hdev);
973 int err, size = HCI_MAX_FRAME_SIZE;
975 BT_DBG("%s", hdev->name);
977 if (!data->bulk_rx_ep)
980 urb = usb_alloc_urb(0, mem_flags);
984 buf = kmalloc(size, mem_flags);
990 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
992 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
993 btusb_bulk_complete, hdev);
995 urb->transfer_flags |= URB_FREE_BUFFER;
997 usb_mark_last_busy(data->udev);
998 usb_anchor_urb(urb, &data->bulk_anchor);
1000 err = usb_submit_urb(urb, mem_flags);
1002 if (err != -EPERM && err != -ENODEV)
1003 bt_dev_err(hdev, "urb %p submission failed (%d)",
1005 usb_unanchor_urb(urb);
1013 static void btusb_isoc_complete(struct urb *urb)
1015 struct hci_dev *hdev = urb->context;
1016 struct btusb_data *data = hci_get_drvdata(hdev);
1019 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1020 urb->actual_length);
1022 if (!test_bit(HCI_RUNNING, &hdev->flags))
1025 if (urb->status == 0) {
1026 for (i = 0; i < urb->number_of_packets; i++) {
1027 unsigned int offset = urb->iso_frame_desc[i].offset;
1028 unsigned int length = urb->iso_frame_desc[i].actual_length;
1030 if (urb->iso_frame_desc[i].status)
1033 hdev->stat.byte_rx += length;
1035 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1037 bt_dev_err(hdev, "corrupted SCO packet");
1038 hdev->stat.err_rx++;
1041 } else if (urb->status == -ENOENT) {
1042 /* Avoid suspend failed when usb_kill_urb */
1046 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1049 usb_anchor_urb(urb, &data->isoc_anchor);
1051 err = usb_submit_urb(urb, GFP_ATOMIC);
1053 /* -EPERM: urb is being killed;
1054 * -ENODEV: device got disconnected
1056 if (err != -EPERM && err != -ENODEV)
1057 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1059 usb_unanchor_urb(urb);
1063 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1064 int mtu, struct btusb_data *data)
1067 unsigned int interval;
1069 BT_DBG("len %d mtu %d", len, mtu);
1071 /* For mSBC ALT 6 setting the host will send the packet at continuous
1072 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1073 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1074 * To maintain the rate we send 63bytes of usb packets alternatively for
1075 * 7ms and 8ms to maintain the rate as 7.5ms.
1077 if (data->usb_alt6_packet_flow) {
1079 data->usb_alt6_packet_flow = false;
1082 data->usb_alt6_packet_flow = true;
1085 for (i = 0; i < interval; i++) {
1086 urb->iso_frame_desc[i].offset = offset;
1087 urb->iso_frame_desc[i].length = offset;
1090 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1091 urb->iso_frame_desc[i].offset = offset;
1092 urb->iso_frame_desc[i].length = len;
1096 urb->number_of_packets = i;
1099 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1103 BT_DBG("len %d mtu %d", len, mtu);
1105 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1106 i++, offset += mtu, len -= mtu) {
1107 urb->iso_frame_desc[i].offset = offset;
1108 urb->iso_frame_desc[i].length = mtu;
1111 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1112 urb->iso_frame_desc[i].offset = offset;
1113 urb->iso_frame_desc[i].length = len;
1117 urb->number_of_packets = i;
1120 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1122 struct btusb_data *data = hci_get_drvdata(hdev);
1128 BT_DBG("%s", hdev->name);
1130 if (!data->isoc_rx_ep)
1133 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1137 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1138 BTUSB_MAX_ISOC_FRAMES;
1140 buf = kmalloc(size, mem_flags);
1146 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1148 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1149 hdev, data->isoc_rx_ep->bInterval);
1151 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1153 __fill_isoc_descriptor(urb, size,
1154 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1156 usb_anchor_urb(urb, &data->isoc_anchor);
1158 err = usb_submit_urb(urb, mem_flags);
1160 if (err != -EPERM && err != -ENODEV)
1161 bt_dev_err(hdev, "urb %p submission failed (%d)",
1163 usb_unanchor_urb(urb);
1171 static void btusb_diag_complete(struct urb *urb)
1173 struct hci_dev *hdev = urb->context;
1174 struct btusb_data *data = hci_get_drvdata(hdev);
1177 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1178 urb->actual_length);
1180 if (urb->status == 0) {
1181 struct sk_buff *skb;
1183 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1185 skb_put_data(skb, urb->transfer_buffer,
1186 urb->actual_length);
1187 hci_recv_diag(hdev, skb);
1189 } else if (urb->status == -ENOENT) {
1190 /* Avoid suspend failed when usb_kill_urb */
1194 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1197 usb_anchor_urb(urb, &data->diag_anchor);
1198 usb_mark_last_busy(data->udev);
1200 err = usb_submit_urb(urb, GFP_ATOMIC);
1202 /* -EPERM: urb is being killed;
1203 * -ENODEV: device got disconnected
1205 if (err != -EPERM && err != -ENODEV)
1206 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1208 usb_unanchor_urb(urb);
1212 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1214 struct btusb_data *data = hci_get_drvdata(hdev);
1218 int err, size = HCI_MAX_FRAME_SIZE;
1220 BT_DBG("%s", hdev->name);
1222 if (!data->diag_rx_ep)
1225 urb = usb_alloc_urb(0, mem_flags);
1229 buf = kmalloc(size, mem_flags);
1235 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1237 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1238 btusb_diag_complete, hdev);
1240 urb->transfer_flags |= URB_FREE_BUFFER;
1242 usb_mark_last_busy(data->udev);
1243 usb_anchor_urb(urb, &data->diag_anchor);
1245 err = usb_submit_urb(urb, mem_flags);
1247 if (err != -EPERM && err != -ENODEV)
1248 bt_dev_err(hdev, "urb %p submission failed (%d)",
1250 usb_unanchor_urb(urb);
1258 static void btusb_tx_complete(struct urb *urb)
1260 struct sk_buff *skb = urb->context;
1261 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1262 struct btusb_data *data = hci_get_drvdata(hdev);
1263 unsigned long flags;
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 spin_lock_irqsave(&data->txlock, flags);
1278 data->tx_in_flight--;
1279 spin_unlock_irqrestore(&data->txlock, flags);
1281 kfree(urb->setup_packet);
1286 static void btusb_isoc_tx_complete(struct urb *urb)
1288 struct sk_buff *skb = urb->context;
1289 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1291 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1292 urb->actual_length);
1294 if (!test_bit(HCI_RUNNING, &hdev->flags))
1298 hdev->stat.byte_tx += urb->transfer_buffer_length;
1300 hdev->stat.err_tx++;
1303 kfree(urb->setup_packet);
1308 static int btusb_open(struct hci_dev *hdev)
1310 struct btusb_data *data = hci_get_drvdata(hdev);
1313 BT_DBG("%s", hdev->name);
1315 err = usb_autopm_get_interface(data->intf);
1319 /* Patching USB firmware files prior to starting any URBs of HCI path
1320 * It is more safe to use USB bulk channel for downloading USB patch
1322 if (data->setup_on_usb) {
1323 err = data->setup_on_usb(hdev);
1328 data->intf->needs_remote_wakeup = 1;
1330 /* Disable device remote wakeup when host is suspended
1331 * For Realtek chips, global suspend without
1332 * SET_FEATURE (DEVICE_REMOTE_WAKEUP) can save more power in device.
1334 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1335 device_wakeup_disable(&data->udev->dev);
1337 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1340 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1344 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1346 usb_kill_anchored_urbs(&data->intr_anchor);
1350 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1351 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1354 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1355 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1359 usb_autopm_put_interface(data->intf);
1363 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1365 usb_autopm_put_interface(data->intf);
1369 static void btusb_stop_traffic(struct btusb_data *data)
1371 usb_kill_anchored_urbs(&data->intr_anchor);
1372 usb_kill_anchored_urbs(&data->bulk_anchor);
1373 usb_kill_anchored_urbs(&data->isoc_anchor);
1374 usb_kill_anchored_urbs(&data->diag_anchor);
1375 usb_kill_anchored_urbs(&data->ctrl_anchor);
1378 static int btusb_close(struct hci_dev *hdev)
1380 struct btusb_data *data = hci_get_drvdata(hdev);
1383 BT_DBG("%s", hdev->name);
1385 cancel_work_sync(&data->work);
1386 cancel_work_sync(&data->waker);
1388 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1389 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1390 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1391 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1393 btusb_stop_traffic(data);
1394 btusb_free_frags(data);
1396 err = usb_autopm_get_interface(data->intf);
1400 data->intf->needs_remote_wakeup = 0;
1402 /* Enable remote wake up for auto-suspend */
1403 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1404 data->intf->needs_remote_wakeup = 1;
1406 usb_autopm_put_interface(data->intf);
1409 usb_scuttle_anchored_urbs(&data->deferred);
1413 static int btusb_flush(struct hci_dev *hdev)
1415 struct btusb_data *data = hci_get_drvdata(hdev);
1417 BT_DBG("%s", hdev->name);
1419 usb_kill_anchored_urbs(&data->tx_anchor);
1420 btusb_free_frags(data);
1425 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1427 struct btusb_data *data = hci_get_drvdata(hdev);
1428 struct usb_ctrlrequest *dr;
1432 urb = usb_alloc_urb(0, GFP_KERNEL);
1434 return ERR_PTR(-ENOMEM);
1436 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1439 return ERR_PTR(-ENOMEM);
1442 dr->bRequestType = data->cmdreq_type;
1443 dr->bRequest = data->cmdreq;
1446 dr->wLength = __cpu_to_le16(skb->len);
1448 pipe = usb_sndctrlpipe(data->udev, 0x00);
1450 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1451 skb->data, skb->len, btusb_tx_complete, skb);
1453 skb->dev = (void *)hdev;
1458 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1460 struct btusb_data *data = hci_get_drvdata(hdev);
1464 if (!data->bulk_tx_ep)
1465 return ERR_PTR(-ENODEV);
1467 urb = usb_alloc_urb(0, GFP_KERNEL);
1469 return ERR_PTR(-ENOMEM);
1471 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1473 usb_fill_bulk_urb(urb, data->udev, pipe,
1474 skb->data, skb->len, btusb_tx_complete, skb);
1476 skb->dev = (void *)hdev;
1481 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1483 struct btusb_data *data = hci_get_drvdata(hdev);
1487 if (!data->isoc_tx_ep)
1488 return ERR_PTR(-ENODEV);
1490 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1492 return ERR_PTR(-ENOMEM);
1494 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1496 usb_fill_int_urb(urb, data->udev, pipe,
1497 skb->data, skb->len, btusb_isoc_tx_complete,
1498 skb, data->isoc_tx_ep->bInterval);
1500 urb->transfer_flags = URB_ISO_ASAP;
1502 if (data->isoc_altsetting == 6)
1503 __fill_isoc_descriptor_msbc(urb, skb->len,
1504 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1507 __fill_isoc_descriptor(urb, skb->len,
1508 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1509 skb->dev = (void *)hdev;
1514 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1516 struct btusb_data *data = hci_get_drvdata(hdev);
1519 usb_anchor_urb(urb, &data->tx_anchor);
1521 err = usb_submit_urb(urb, GFP_KERNEL);
1523 if (err != -EPERM && err != -ENODEV)
1524 bt_dev_err(hdev, "urb %p submission failed (%d)",
1526 kfree(urb->setup_packet);
1527 usb_unanchor_urb(urb);
1529 usb_mark_last_busy(data->udev);
1536 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1538 struct btusb_data *data = hci_get_drvdata(hdev);
1539 unsigned long flags;
1542 spin_lock_irqsave(&data->txlock, flags);
1543 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1545 data->tx_in_flight++;
1546 spin_unlock_irqrestore(&data->txlock, flags);
1549 return submit_tx_urb(hdev, urb);
1551 usb_anchor_urb(urb, &data->deferred);
1552 schedule_work(&data->waker);
1558 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1562 BT_DBG("%s", hdev->name);
1564 switch (hci_skb_pkt_type(skb)) {
1565 case HCI_COMMAND_PKT:
1566 urb = alloc_ctrl_urb(hdev, skb);
1568 return PTR_ERR(urb);
1570 hdev->stat.cmd_tx++;
1571 return submit_or_queue_tx_urb(hdev, urb);
1573 case HCI_ACLDATA_PKT:
1574 urb = alloc_bulk_urb(hdev, skb);
1576 return PTR_ERR(urb);
1578 hdev->stat.acl_tx++;
1579 return submit_or_queue_tx_urb(hdev, urb);
1581 case HCI_SCODATA_PKT:
1582 if (hci_conn_num(hdev, SCO_LINK) < 1)
1585 urb = alloc_isoc_urb(hdev, skb);
1587 return PTR_ERR(urb);
1589 hdev->stat.sco_tx++;
1590 return submit_tx_urb(hdev, urb);
1596 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1598 struct btusb_data *data = hci_get_drvdata(hdev);
1600 BT_DBG("%s evt %d", hdev->name, evt);
1602 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1603 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1604 data->air_mode = evt;
1605 schedule_work(&data->work);
1609 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1611 struct btusb_data *data = hci_get_drvdata(hdev);
1612 struct usb_interface *intf = data->isoc;
1613 struct usb_endpoint_descriptor *ep_desc;
1619 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
1621 bt_dev_err(hdev, "setting interface failed (%d)", -err);
1625 data->isoc_altsetting = altsetting;
1627 data->isoc_tx_ep = NULL;
1628 data->isoc_rx_ep = NULL;
1630 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1631 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1633 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1634 data->isoc_tx_ep = ep_desc;
1638 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1639 data->isoc_rx_ep = ep_desc;
1644 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1645 bt_dev_err(hdev, "invalid SCO descriptors");
1652 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1654 struct btusb_data *data = hci_get_drvdata(hdev);
1657 if (data->isoc_altsetting != new_alts) {
1658 unsigned long flags;
1660 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1661 usb_kill_anchored_urbs(&data->isoc_anchor);
1663 /* When isochronous alternate setting needs to be
1664 * changed, because SCO connection has been added
1665 * or removed, a packet fragment may be left in the
1666 * reassembling state. This could lead to wrongly
1667 * assembled fragments.
1669 * Clear outstanding fragment when selecting a new
1670 * alternate setting.
1672 spin_lock_irqsave(&data->rxlock, flags);
1673 kfree_skb(data->sco_skb);
1674 data->sco_skb = NULL;
1675 spin_unlock_irqrestore(&data->rxlock, flags);
1677 err = __set_isoc_interface(hdev, new_alts);
1682 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1683 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1684 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1686 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1692 static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1695 struct usb_interface *intf = data->isoc;
1698 BT_DBG("Looking for Alt no :%d", alt);
1703 for (i = 0; i < intf->num_altsetting; i++) {
1704 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1705 return &intf->altsetting[i];
1711 static void btusb_work(struct work_struct *work)
1713 struct btusb_data *data = container_of(work, struct btusb_data, work);
1714 struct hci_dev *hdev = data->hdev;
1718 if (data->sco_num > 0) {
1719 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1720 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1722 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1723 usb_kill_anchored_urbs(&data->isoc_anchor);
1727 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1730 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1731 if (hdev->voice_setting & 0x0020) {
1732 static const int alts[3] = { 2, 4, 5 };
1734 new_alts = alts[data->sco_num - 1];
1736 new_alts = data->sco_num;
1738 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
1739 /* Check if Alt 6 is supported for Transparent audio */
1740 if (btusb_find_altsetting(data, 6)) {
1741 data->usb_alt6_packet_flow = true;
1743 } else if (test_bit(BTUSB_USE_ALT1_FOR_WBS, &data->flags)) {
1746 bt_dev_err(hdev, "Device does not support ALT setting 6");
1750 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1751 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
1753 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1754 usb_kill_anchored_urbs(&data->isoc_anchor);
1756 __set_isoc_interface(hdev, 0);
1757 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1758 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1762 static void btusb_waker(struct work_struct *work)
1764 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1767 err = usb_autopm_get_interface(data->intf);
1771 usb_autopm_put_interface(data->intf);
1774 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1776 struct sk_buff *skb;
1779 BT_DBG("%s", hdev->name);
1781 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1783 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
1790 static int btusb_setup_csr(struct hci_dev *hdev)
1792 struct hci_rp_read_local_version *rp;
1793 struct sk_buff *skb;
1794 bool is_fake = false;
1796 BT_DBG("%s", hdev->name);
1798 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1801 int err = PTR_ERR(skb);
1802 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
1806 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1807 bt_dev_err(hdev, "CSR: Local version length mismatch");
1812 rp = (struct hci_rp_read_local_version *)skb->data;
1814 /* Detect a wide host of Chinese controllers that aren't CSR.
1816 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1818 * The main thing they have in common is that these are really popular low-cost
1819 * options that support newer Bluetooth versions but rely on heavy VID/PID
1820 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1822 * We detect actual CSR devices by checking that the HCI manufacturer code
1823 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1824 * HCI rev values always match. As they both store the firmware number.
1826 if (le16_to_cpu(rp->manufacturer) != 10 ||
1827 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
1830 /* Known legit CSR firmware build numbers and their supported BT versions:
1831 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1832 * - 1.2 (0x2) -> 0x04d9, 0x0529
1833 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
1834 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
1835 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
1837 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
1838 * support BT 1.1 only; so it's a dead giveaway when some
1839 * third-party BT 4.0 dongle reuses it.
1841 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
1842 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
1845 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
1846 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
1849 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
1850 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
1853 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
1854 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
1857 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
1858 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
1862 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds...");
1864 /* Generally these clones have big discrepancies between
1865 * advertised features and what's actually supported.
1866 * Probably will need to be expanded in the future;
1867 * without these the controller will lock up.
1869 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1870 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
1872 /* Clear the reset quirk since this is not an actual
1873 * early Bluetooth 1.1 device from CSR.
1875 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1876 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1884 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1885 struct intel_version *ver)
1887 const struct firmware *fw;
1891 snprintf(fwname, sizeof(fwname),
1892 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1893 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1894 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1895 ver->fw_build_ww, ver->fw_build_yy);
1897 ret = request_firmware(&fw, fwname, &hdev->dev);
1899 if (ret == -EINVAL) {
1900 bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1905 bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1908 /* If the correct firmware patch file is not found, use the
1909 * default firmware patch file instead
1911 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1912 ver->hw_platform, ver->hw_variant);
1913 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1914 bt_dev_err(hdev, "failed to open default fw file: %s",
1920 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
1925 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1926 const struct firmware *fw,
1927 const u8 **fw_ptr, int *disable_patch)
1929 struct sk_buff *skb;
1930 struct hci_command_hdr *cmd;
1931 const u8 *cmd_param;
1932 struct hci_event_hdr *evt = NULL;
1933 const u8 *evt_param = NULL;
1934 int remain = fw->size - (*fw_ptr - fw->data);
1936 /* The first byte indicates the types of the patch command or event.
1937 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1938 * in the current firmware buffer doesn't start with 0x01 or
1939 * the size of remain buffer is smaller than HCI command header,
1940 * the firmware file is corrupted and it should stop the patching
1943 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1944 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
1950 cmd = (struct hci_command_hdr *)(*fw_ptr);
1951 *fw_ptr += sizeof(*cmd);
1952 remain -= sizeof(*cmd);
1954 /* Ensure that the remain firmware data is long enough than the length
1955 * of command parameter. If not, the firmware file is corrupted.
1957 if (remain < cmd->plen) {
1958 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
1962 /* If there is a command that loads a patch in the firmware
1963 * file, then enable the patch upon success, otherwise just
1964 * disable the manufacturer mode, for example patch activation
1965 * is not required when the default firmware patch file is used
1966 * because there are no patch data to load.
1968 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1971 cmd_param = *fw_ptr;
1972 *fw_ptr += cmd->plen;
1973 remain -= cmd->plen;
1975 /* This reads the expected events when the above command is sent to the
1976 * device. Some vendor commands expects more than one events, for
1977 * example command status event followed by vendor specific event.
1978 * For this case, it only keeps the last expected event. so the command
1979 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1980 * last expected event.
1982 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1986 evt = (struct hci_event_hdr *)(*fw_ptr);
1987 *fw_ptr += sizeof(*evt);
1988 remain -= sizeof(*evt);
1990 if (remain < evt->plen) {
1991 bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
1995 evt_param = *fw_ptr;
1996 *fw_ptr += evt->plen;
1997 remain -= evt->plen;
2000 /* Every HCI commands in the firmware file has its correspond event.
2001 * If event is not found or remain is smaller than zero, the firmware
2002 * file is corrupted.
2004 if (!evt || !evt_param || remain < 0) {
2005 bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
2009 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
2010 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
2012 bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
2013 cmd->opcode, PTR_ERR(skb));
2014 return PTR_ERR(skb);
2017 /* It ensures that the returned event matches the event data read from
2018 * the firmware file. At fist, it checks the length and then
2019 * the contents of the event.
2021 if (skb->len != evt->plen) {
2022 bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
2023 le16_to_cpu(cmd->opcode));
2028 if (memcmp(skb->data, evt_param, evt->plen)) {
2029 bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
2030 le16_to_cpu(cmd->opcode));
2039 static int btusb_setup_intel(struct hci_dev *hdev)
2041 struct sk_buff *skb;
2042 const struct firmware *fw;
2044 int disable_patch, err;
2045 struct intel_version ver;
2047 BT_DBG("%s", hdev->name);
2049 /* The controller has a bug with the first HCI command sent to it
2050 * returning number of completed commands as zero. This would stall the
2051 * command processing in the Bluetooth core.
2053 * As a workaround, send HCI Reset command first which will reset the
2054 * number of completed commands and allow normal command processing
2057 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2059 bt_dev_err(hdev, "sending initial HCI reset command failed (%ld)",
2061 return PTR_ERR(skb);
2065 /* Read Intel specific controller version first to allow selection of
2066 * which firmware file to load.
2068 * The returned information are hardware variant and revision plus
2069 * firmware variant, revision and build number.
2071 err = btintel_read_version(hdev, &ver);
2075 bt_dev_info(hdev, "read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
2076 ver.hw_platform, ver.hw_variant, ver.hw_revision,
2077 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
2078 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
2080 /* fw_patch_num indicates the version of patch the device currently
2081 * have. If there is no patch data in the device, it is always 0x00.
2082 * So, if it is other than 0x00, no need to patch the device again.
2084 if (ver.fw_patch_num) {
2085 bt_dev_info(hdev, "Intel device is already patched. "
2086 "patch num: %02x", ver.fw_patch_num);
2090 /* Opens the firmware patch file based on the firmware version read
2091 * from the controller. If it fails to open the matching firmware
2092 * patch file, it tries to open the default firmware patch file.
2093 * If no patch file is found, allow the device to operate without
2096 fw = btusb_setup_intel_get_fw(hdev, &ver);
2101 /* Enable the manufacturer mode of the controller.
2102 * Only while this mode is enabled, the driver can download the
2103 * firmware patch data and configuration parameters.
2105 err = btintel_enter_mfg(hdev);
2107 release_firmware(fw);
2113 /* The firmware data file consists of list of Intel specific HCI
2114 * commands and its expected events. The first byte indicates the
2115 * type of the message, either HCI command or HCI event.
2117 * It reads the command and its expected event from the firmware file,
2118 * and send to the controller. Once __hci_cmd_sync_ev() returns,
2119 * the returned event is compared with the event read from the firmware
2120 * file and it will continue until all the messages are downloaded to
2123 * Once the firmware patching is completed successfully,
2124 * the manufacturer mode is disabled with reset and activating the
2127 * If the firmware patching fails, the manufacturer mode is
2128 * disabled with reset and deactivating the patch.
2130 * If the default patch file is used, no reset is done when disabling
2133 while (fw->size > fw_ptr - fw->data) {
2136 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
2139 goto exit_mfg_deactivate;
2142 release_firmware(fw);
2145 goto exit_mfg_disable;
2147 /* Patching completed successfully and disable the manufacturer mode
2148 * with reset and activate the downloaded firmware patches.
2150 err = btintel_exit_mfg(hdev, true, true);
2154 /* Need build number for downloaded fw patches in
2155 * every power-on boot
2157 err = btintel_read_version(hdev, &ver);
2160 bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
2166 /* Disable the manufacturer mode without reset */
2167 err = btintel_exit_mfg(hdev, false, false);
2171 bt_dev_info(hdev, "Intel firmware patch completed");
2175 exit_mfg_deactivate:
2176 release_firmware(fw);
2178 /* Patching failed. Disable the manufacturer mode with reset and
2179 * deactivate the downloaded firmware patches.
2181 err = btintel_exit_mfg(hdev, true, false);
2185 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
2188 /* Set the event mask for Intel specific vendor events. This enables
2189 * a few extra events that are useful during general operation.
2191 btintel_set_event_mask_mfg(hdev, false);
2193 btintel_check_bdaddr(hdev);
2197 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2199 struct sk_buff *skb;
2200 struct hci_event_hdr *hdr;
2201 struct hci_ev_cmd_complete *evt;
2203 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2207 hdr = skb_put(skb, sizeof(*hdr));
2208 hdr->evt = HCI_EV_CMD_COMPLETE;
2209 hdr->plen = sizeof(*evt) + 1;
2211 evt = skb_put(skb, sizeof(*evt));
2213 evt->opcode = cpu_to_le16(opcode);
2215 skb_put_u8(skb, 0x00);
2217 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2219 return hci_recv_frame(hdev, skb);
2222 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2225 /* When the device is in bootloader mode, then it can send
2226 * events via the bulk endpoint. These events are treated the
2227 * same way as the ones received from the interrupt endpoint.
2229 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
2230 return btusb_recv_intr(data, buffer, count);
2232 return btusb_recv_bulk(data, buffer, count);
2235 static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
2238 const struct intel_bootup *evt = ptr;
2240 if (len != sizeof(*evt))
2243 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags))
2244 wake_up_bit(&data->flags, BTUSB_BOOTING);
2247 static void btusb_intel_secure_send_result(struct btusb_data *data,
2248 const void *ptr, unsigned int len)
2250 const struct intel_secure_send_result *evt = ptr;
2252 if (len != sizeof(*evt))
2256 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
2258 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
2259 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags))
2260 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
2263 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2265 struct btusb_data *data = hci_get_drvdata(hdev);
2267 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2268 struct hci_event_hdr *hdr = (void *)skb->data;
2270 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2272 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2273 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
2275 switch (skb->data[2]) {
2277 /* When switching to the operational firmware
2278 * the device sends a vendor specific event
2279 * indicating that the bootup completed.
2281 btusb_intel_bootup(data, ptr, len);
2284 /* When the firmware loading completes the
2285 * device sends out a vendor specific event
2286 * indicating the result of the firmware
2289 btusb_intel_secure_send_result(data, ptr, len);
2295 return hci_recv_frame(hdev, skb);
2298 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2300 struct btusb_data *data = hci_get_drvdata(hdev);
2303 BT_DBG("%s", hdev->name);
2305 switch (hci_skb_pkt_type(skb)) {
2306 case HCI_COMMAND_PKT:
2307 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2308 struct hci_command_hdr *cmd = (void *)skb->data;
2309 __u16 opcode = le16_to_cpu(cmd->opcode);
2311 /* When in bootloader mode and the command 0xfc09
2312 * is received, it needs to be send down the
2313 * bulk endpoint. So allocate a bulk URB instead.
2315 if (opcode == 0xfc09)
2316 urb = alloc_bulk_urb(hdev, skb);
2318 urb = alloc_ctrl_urb(hdev, skb);
2320 /* When the 0xfc01 command is issued to boot into
2321 * the operational firmware, it will actually not
2322 * send a command complete event. To keep the flow
2323 * control working inject that event here.
2325 if (opcode == 0xfc01)
2326 inject_cmd_complete(hdev, opcode);
2328 urb = alloc_ctrl_urb(hdev, skb);
2331 return PTR_ERR(urb);
2333 hdev->stat.cmd_tx++;
2334 return submit_or_queue_tx_urb(hdev, urb);
2336 case HCI_ACLDATA_PKT:
2337 urb = alloc_bulk_urb(hdev, skb);
2339 return PTR_ERR(urb);
2341 hdev->stat.acl_tx++;
2342 return submit_or_queue_tx_urb(hdev, urb);
2344 case HCI_SCODATA_PKT:
2345 if (hci_conn_num(hdev, SCO_LINK) < 1)
2348 urb = alloc_isoc_urb(hdev, skb);
2350 return PTR_ERR(urb);
2352 hdev->stat.sco_tx++;
2353 return submit_tx_urb(hdev, urb);
2359 static bool btusb_setup_intel_new_get_fw_name(struct intel_version *ver,
2360 struct intel_boot_params *params,
2361 char *fw_name, size_t len,
2364 switch (ver->hw_variant) {
2365 case 0x0b: /* SfP */
2366 case 0x0c: /* WsP */
2367 snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
2368 le16_to_cpu(ver->hw_variant),
2369 le16_to_cpu(params->dev_revid),
2372 case 0x11: /* JfP */
2373 case 0x12: /* ThP */
2374 case 0x13: /* HrP */
2375 case 0x14: /* CcP */
2376 snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
2377 le16_to_cpu(ver->hw_variant),
2378 le16_to_cpu(ver->hw_revision),
2379 le16_to_cpu(ver->fw_revision),
2388 static void btusb_setup_intel_newgen_get_fw_name(const struct intel_version_tlv *ver_tlv,
2389 char *fw_name, size_t len,
2392 /* The firmware file name for new generation controllers will be
2393 * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
2395 snprintf(fw_name, len, "intel/ibt-%04x-%04x.%s",
2396 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver_tlv->cnvi_top),
2397 INTEL_CNVX_TOP_STEP(ver_tlv->cnvi_top)),
2398 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver_tlv->cnvr_top),
2399 INTEL_CNVX_TOP_STEP(ver_tlv->cnvr_top)),
2403 static int btusb_intel_download_firmware(struct hci_dev *hdev,
2404 struct intel_version *ver,
2405 struct intel_boot_params *params,
2408 const struct firmware *fw;
2411 struct btusb_data *data = hci_get_drvdata(hdev);
2413 if (!ver || !params)
2416 /* The hardware platform number has a fixed value of 0x37 and
2417 * for now only accept this single value.
2419 if (ver->hw_platform != 0x37) {
2420 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
2425 /* Check for supported iBT hardware variants of this firmware
2428 * This check has been put in place to ensure correct forward
2429 * compatibility options when newer hardware variants come along.
2431 switch (ver->hw_variant) {
2432 case 0x0b: /* SfP */
2433 case 0x0c: /* WsP */
2434 case 0x11: /* JfP */
2435 case 0x12: /* ThP */
2436 case 0x13: /* HrP */
2437 case 0x14: /* CcP */
2440 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
2445 btintel_version_info(hdev, ver);
2447 /* The firmware variant determines if the device is in bootloader
2448 * mode or is running operational firmware. The value 0x06 identifies
2449 * the bootloader and the value 0x23 identifies the operational
2452 * When the operational firmware is already present, then only
2453 * the check for valid Bluetooth device address is needed. This
2454 * determines if the device will be added as configured or
2455 * unconfigured controller.
2457 * It is not possible to use the Secure Boot Parameters in this
2458 * case since that command is only available in bootloader mode.
2460 if (ver->fw_variant == 0x23) {
2461 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2462 btintel_check_bdaddr(hdev);
2466 /* If the device is not in bootloader mode, then the only possible
2467 * choice is to return an error and abort the device initialization.
2469 if (ver->fw_variant != 0x06) {
2470 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
2475 /* Read the secure boot parameters to identify the operating
2476 * details of the bootloader.
2478 err = btintel_read_boot_params(hdev, params);
2482 /* It is required that every single firmware fragment is acknowledged
2483 * with a command complete event. If the boot parameters indicate
2484 * that this bootloader does not send them, then abort the setup.
2486 if (params->limited_cce != 0x00) {
2487 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
2488 params->limited_cce);
2492 /* If the OTP has no valid Bluetooth device address, then there will
2493 * also be no valid address for the operational firmware.
2495 if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) {
2496 bt_dev_info(hdev, "No device address configured");
2497 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2500 /* With this Intel bootloader only the hardware variant and device
2501 * revision information are used to select the right firmware for SfP
2504 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2506 * Currently the supported hardware variants are:
2507 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2508 * 12 (0x0c) for iBT3.5 (WsP)
2510 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2511 * variant, HW revision and FW revision, as these are dependent on CNVi
2512 * and RF Combination.
2514 * 17 (0x11) for iBT3.5 (JfP)
2515 * 18 (0x12) for iBT3.5 (ThP)
2517 * The firmware file name for these will be
2518 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2521 err = btusb_setup_intel_new_get_fw_name(ver, params, fwname,
2522 sizeof(fwname), "sfi");
2524 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2528 err = request_firmware(&fw, fwname, &hdev->dev);
2530 bt_dev_err(hdev, "Failed to load Intel firmware file (%d)", err);
2534 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2536 if (fw->size < 644) {
2537 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2543 set_bit(BTUSB_DOWNLOADING, &data->flags);
2545 /* Start firmware downloading and get boot parameter */
2546 err = btintel_download_firmware(hdev, fw, boot_param);
2548 /* When FW download fails, send Intel Reset to retry
2551 btintel_reset_to_bootloader(hdev);
2554 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2556 bt_dev_info(hdev, "Waiting for firmware download to complete");
2558 /* Before switching the device into operational mode and with that
2559 * booting the loaded firmware, wait for the bootloader notification
2560 * that all fragments have been successfully received.
2562 * When the event processing receives the notification, then the
2563 * BTUSB_DOWNLOADING flag will be cleared.
2565 * The firmware loading should not take longer than 5 seconds
2566 * and thus just timeout if that happens and fail the setup
2569 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2571 msecs_to_jiffies(5000));
2572 if (err == -EINTR) {
2573 bt_dev_err(hdev, "Firmware loading interrupted");
2578 bt_dev_err(hdev, "Firmware loading timeout");
2580 btintel_reset_to_bootloader(hdev);
2584 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2585 bt_dev_err(hdev, "Firmware loading failed");
2591 release_firmware(fw);
2595 static int btusb_setup_intel_new(struct hci_dev *hdev)
2597 struct btusb_data *data = hci_get_drvdata(hdev);
2598 struct intel_version ver;
2599 struct intel_boot_params params;
2602 ktime_t calltime, delta, rettime;
2603 unsigned long long duration;
2605 struct intel_debug_features features;
2607 BT_DBG("%s", hdev->name);
2609 /* Set the default boot parameter to 0x0 and it is updated to
2610 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2611 * command while downloading the firmware.
2613 boot_param = 0x00000000;
2615 calltime = ktime_get();
2617 /* Read the Intel version information to determine if the device
2618 * is in bootloader mode or if it already has operational firmware
2621 err = btintel_read_version(hdev, &ver);
2623 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2624 btintel_reset_to_bootloader(hdev);
2628 err = btusb_intel_download_firmware(hdev, &ver, ¶ms, &boot_param);
2632 /* controller is already having an operational firmware */
2633 if (ver.fw_variant == 0x23)
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, "Firmware loaded in %llu usecs", duration);
2642 calltime = ktime_get();
2644 set_bit(BTUSB_BOOTING, &data->flags);
2646 err = btintel_send_intel_reset(hdev, boot_param);
2648 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
2649 btintel_reset_to_bootloader(hdev);
2653 /* The bootloader will not indicate when the device is ready. This
2654 * is done by the operational firmware sending bootup notification.
2656 * Booting into operational firmware should not take longer than
2657 * 1 second. However if that happens, then just fail the setup
2658 * since something went wrong.
2660 bt_dev_info(hdev, "Waiting for device to boot");
2662 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2664 msecs_to_jiffies(1000));
2666 if (err == -EINTR) {
2667 bt_dev_err(hdev, "Device boot interrupted");
2672 bt_dev_err(hdev, "Device boot timeout");
2673 btintel_reset_to_bootloader(hdev);
2677 rettime = ktime_get();
2678 delta = ktime_sub(rettime, calltime);
2679 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2681 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
2683 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2685 err = btusb_setup_intel_new_get_fw_name(&ver, ¶ms, ddcname,
2686 sizeof(ddcname), "ddc");
2689 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2691 /* Once the device is running in operational mode, it needs to
2692 * apply the device configuration (DDC) parameters.
2694 * The device can work without DDC parameters, so even if it
2695 * fails to load the file, no need to fail the setup.
2697 btintel_load_ddc_config(hdev, ddcname);
2700 /* Read the Intel supported features and if new exception formats
2701 * supported, need to load the additional DDC config to enable.
2703 btintel_read_debug_features(hdev, &features);
2705 /* Set DDC mask for available debug features */
2706 btintel_set_debug_features(hdev, &features);
2708 /* Read the Intel version information after loading the FW */
2709 err = btintel_read_version(hdev, &ver);
2713 btintel_version_info(hdev, &ver);
2716 /* All Intel controllers that support the Microsoft vendor
2717 * extension are using 0xFC1E for VsMsftOpCode.
2719 switch (ver.hw_variant) {
2720 case 0x12: /* ThP */
2721 hci_set_msft_opcode(hdev, 0xFC1E);
2725 /* Set the event mask for Intel specific vendor events. This enables
2726 * a few extra events that are useful during general operation. It
2727 * does not enable any debugging related events.
2729 * The device will function correctly without these events enabled
2730 * and thus no need to fail the setup.
2732 btintel_set_event_mask(hdev, false);
2737 static int btusb_setup_intel_newgen(struct hci_dev *hdev)
2739 struct btusb_data *data = hci_get_drvdata(hdev);
2742 ktime_t calltime, delta, rettime;
2743 unsigned long long duration;
2745 struct intel_debug_features features;
2746 struct intel_version_tlv version;
2748 bt_dev_dbg(hdev, "");
2750 /* Set the default boot parameter to 0x0 and it is updated to
2751 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2752 * command while downloading the firmware.
2754 boot_param = 0x00000000;
2756 calltime = ktime_get();
2758 /* Read the Intel version information to determine if the device
2759 * is in bootloader mode or if it already has operational firmware
2762 err = btintel_read_version_tlv(hdev, &version);
2764 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2765 btintel_reset_to_bootloader(hdev);
2769 btintel_version_info_tlv(hdev, &version);
2771 /* check if controller is already having an operational firmware */
2772 if (version.img_type == 0x03)
2775 rettime = ktime_get();
2776 delta = ktime_sub(rettime, calltime);
2777 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2779 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
2781 calltime = ktime_get();
2783 set_bit(BTUSB_BOOTING, &data->flags);
2785 err = btintel_send_intel_reset(hdev, boot_param);
2787 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
2788 btintel_reset_to_bootloader(hdev);
2792 /* The bootloader will not indicate when the device is ready. This
2793 * is done by the operational firmware sending bootup notification.
2795 * Booting into operational firmware should not take longer than
2796 * 1 second. However if that happens, then just fail the setup
2797 * since something went wrong.
2799 bt_dev_info(hdev, "Waiting for device to boot");
2801 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2803 msecs_to_jiffies(1000));
2805 if (err == -EINTR) {
2806 bt_dev_err(hdev, "Device boot interrupted");
2811 bt_dev_err(hdev, "Device boot timeout");
2812 btintel_reset_to_bootloader(hdev);
2816 rettime = ktime_get();
2817 delta = ktime_sub(rettime, calltime);
2818 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2820 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
2822 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2824 btusb_setup_intel_newgen_get_fw_name(&version, ddcname, sizeof(ddcname),
2826 /* Once the device is running in operational mode, it needs to
2827 * apply the device configuration (DDC) parameters.
2829 * The device can work without DDC parameters, so even if it
2830 * fails to load the file, no need to fail the setup.
2832 btintel_load_ddc_config(hdev, ddcname);
2834 /* Read the Intel supported features and if new exception formats
2835 * supported, need to load the additional DDC config to enable.
2837 btintel_read_debug_features(hdev, &features);
2839 /* Set DDC mask for available debug features */
2840 btintel_set_debug_features(hdev, &features);
2842 /* Read the Intel version information after loading the FW */
2843 err = btintel_read_version_tlv(hdev, &version);
2847 btintel_version_info_tlv(hdev, &version);
2850 /* Set the event mask for Intel specific vendor events. This enables
2851 * a few extra events that are useful during general operation. It
2852 * does not enable any debugging related events.
2854 * The device will function correctly without these events enabled
2855 * and thus no need to fail the setup.
2857 btintel_set_event_mask(hdev, false);
2861 static int btusb_shutdown_intel(struct hci_dev *hdev)
2863 struct sk_buff *skb;
2866 /* In the shutdown sequence where Bluetooth is turned off followed
2867 * by WiFi being turned off, turning WiFi back on causes issue with
2868 * the RF calibration.
2870 * To ensure that any RF activity has been stopped, issue HCI Reset
2871 * command to clear all ongoing activity including advertising,
2874 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2877 bt_dev_err(hdev, "HCI reset during shutdown failed");
2882 /* Some platforms have an issue with BT LED when the interface is
2883 * down or BT radio is turned off, which takes 5 seconds to BT LED
2884 * goes off. This command turns off the BT LED immediately.
2886 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2889 bt_dev_err(hdev, "turning off Intel device LED failed");
2897 static int btusb_shutdown_intel_new(struct hci_dev *hdev)
2899 struct sk_buff *skb;
2901 /* Send HCI Reset to the controller to stop any BT activity which
2902 * were triggered. This will help to save power and maintain the
2903 * sync b/w Host and controller
2905 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2907 bt_dev_err(hdev, "HCI reset during shutdown failed");
2908 return PTR_ERR(skb);
2915 #define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
2916 #define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
2918 #define HCI_WMT_MAX_EVENT_SIZE 64
2921 BTMTK_WMT_PATCH_DWNLD = 0x1,
2922 BTMTK_WMT_FUNC_CTRL = 0x6,
2923 BTMTK_WMT_RST = 0x7,
2924 BTMTK_WMT_SEMAPHORE = 0x17,
2929 BTMTK_WMT_PATCH_UNDONE,
2930 BTMTK_WMT_PATCH_DONE,
2931 BTMTK_WMT_ON_UNDONE,
2933 BTMTK_WMT_ON_PROGRESS,
2936 struct btmtk_wmt_hdr {
2943 struct btmtk_hci_wmt_cmd {
2944 struct btmtk_wmt_hdr hdr;
2948 struct btmtk_hci_wmt_evt {
2949 struct hci_event_hdr hhdr;
2950 struct btmtk_wmt_hdr whdr;
2953 struct btmtk_hci_wmt_evt_funcc {
2954 struct btmtk_hci_wmt_evt hwhdr;
2958 struct btmtk_tci_sleep {
2961 __le16 host_duration;
2963 u8 time_compensation;
2966 struct btmtk_hci_wmt_params {
2974 static void btusb_mtk_wmt_recv(struct urb *urb)
2976 struct hci_dev *hdev = urb->context;
2977 struct btusb_data *data = hci_get_drvdata(hdev);
2978 struct hci_event_hdr *hdr;
2979 struct sk_buff *skb;
2982 if (urb->status == 0 && urb->actual_length > 0) {
2983 hdev->stat.byte_rx += urb->actual_length;
2985 /* WMT event shouldn't be fragmented and the size should be
2986 * less than HCI_WMT_MAX_EVENT_SIZE.
2988 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2990 hdev->stat.err_rx++;
2994 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2995 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2997 hdr = (void *)skb->data;
2998 /* Fix up the vendor event id with 0xff for vendor specific
2999 * instead of 0xe4 so that event send via monitoring socket can
3000 * be parsed properly.
3004 /* When someone waits for the WMT event, the skb is being cloned
3005 * and being processed the events from there then.
3007 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
3008 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
3013 err = hci_recv_frame(hdev, skb);
3017 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
3019 /* Barrier to sync with other CPUs */
3020 smp_mb__after_atomic();
3021 wake_up_bit(&data->flags,
3022 BTUSB_TX_WAIT_VND_EVT);
3027 kfree_skb(data->evt_skb);
3028 data->evt_skb = NULL;
3030 } else if (urb->status == -ENOENT) {
3031 /* Avoid suspend failed when usb_kill_urb */
3035 usb_mark_last_busy(data->udev);
3037 /* The URB complete handler is still called with urb->actual_length = 0
3038 * when the event is not available, so we should keep re-submitting
3039 * URB until WMT event returns, Also, It's necessary to wait some time
3040 * between the two consecutive control URBs to relax the target device
3041 * to generate the event. Otherwise, the WMT event cannot return from
3042 * the device successfully.
3046 usb_anchor_urb(urb, &data->ctrl_anchor);
3047 err = usb_submit_urb(urb, GFP_ATOMIC);
3049 /* -EPERM: urb is being killed;
3050 * -ENODEV: device got disconnected
3052 if (err != -EPERM && err != -ENODEV)
3053 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
3055 usb_unanchor_urb(urb);
3059 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
3061 struct btusb_data *data = hci_get_drvdata(hdev);
3062 struct usb_ctrlrequest *dr;
3068 urb = usb_alloc_urb(0, GFP_KERNEL);
3072 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
3078 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
3080 dr->wIndex = cpu_to_le16(0);
3081 dr->wValue = cpu_to_le16(48);
3082 dr->wLength = cpu_to_le16(size);
3084 buf = kmalloc(size, GFP_KERNEL);
3091 pipe = usb_rcvctrlpipe(data->udev, 0);
3093 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
3094 buf, size, btusb_mtk_wmt_recv, hdev);
3096 urb->transfer_flags |= URB_FREE_BUFFER;
3098 usb_anchor_urb(urb, &data->ctrl_anchor);
3099 err = usb_submit_urb(urb, GFP_KERNEL);
3101 if (err != -EPERM && err != -ENODEV)
3102 bt_dev_err(hdev, "urb %p submission failed (%d)",
3104 usb_unanchor_urb(urb);
3112 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
3113 struct btmtk_hci_wmt_params *wmt_params)
3115 struct btusb_data *data = hci_get_drvdata(hdev);
3116 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
3117 u32 hlen, status = BTMTK_WMT_INVALID;
3118 struct btmtk_hci_wmt_evt *wmt_evt;
3119 struct btmtk_hci_wmt_cmd wc;
3120 struct btmtk_wmt_hdr *hdr;
3123 /* Submit control IN URB on demand to process the WMT event */
3124 err = btusb_mtk_submit_wmt_recv_urb(hdev);
3128 /* Send the WMT command and wait until the WMT event returns */
3129 hlen = sizeof(*hdr) + wmt_params->dlen;
3133 hdr = (struct btmtk_wmt_hdr *)&wc;
3135 hdr->op = wmt_params->op;
3136 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
3137 hdr->flag = wmt_params->flag;
3138 memcpy(wc.data, wmt_params->data, wmt_params->dlen);
3140 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3142 err = __hci_cmd_send(hdev, 0xfc6f, hlen, &wc);
3145 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3149 /* The vendor specific WMT commands are all answered by a vendor
3150 * specific event and will have the Command Status or Command
3151 * Complete as with usual HCI command flow control.
3153 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
3154 * state to be cleared. The driver specific event receive routine
3155 * will clear that state and with that indicate completion of the
3158 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
3159 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
3160 if (err == -EINTR) {
3161 bt_dev_err(hdev, "Execution of wmt command interrupted");
3162 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3167 bt_dev_err(hdev, "Execution of wmt command timed out");
3168 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3172 /* Parse and handle the return WMT event */
3173 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
3174 if (wmt_evt->whdr.op != hdr->op) {
3175 bt_dev_err(hdev, "Wrong op received %d expected %d",
3176 wmt_evt->whdr.op, hdr->op);
3181 switch (wmt_evt->whdr.op) {
3182 case BTMTK_WMT_SEMAPHORE:
3183 if (wmt_evt->whdr.flag == 2)
3184 status = BTMTK_WMT_PATCH_UNDONE;
3186 status = BTMTK_WMT_PATCH_DONE;
3188 case BTMTK_WMT_FUNC_CTRL:
3189 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
3190 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
3191 status = BTMTK_WMT_ON_DONE;
3192 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
3193 status = BTMTK_WMT_ON_PROGRESS;
3195 status = BTMTK_WMT_ON_UNDONE;
3199 if (wmt_params->status)
3200 *wmt_params->status = status;
3203 kfree_skb(data->evt_skb);
3204 data->evt_skb = NULL;
3209 static int btusb_mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
3211 struct btmtk_hci_wmt_params wmt_params;
3212 const struct firmware *fw;
3218 err = request_firmware(&fw, fwname, &hdev->dev);
3220 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
3224 /* Power on data RAM the firmware relies on. */
3226 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3227 wmt_params.flag = 3;
3228 wmt_params.dlen = sizeof(param);
3229 wmt_params.data = ¶m;
3230 wmt_params.status = NULL;
3232 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3234 bt_dev_err(hdev, "Failed to power on data RAM (%d)", err);
3235 goto err_release_fw;
3241 /* The size of patch header is 30 bytes, should be skip */
3244 goto err_release_fw;
3251 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3252 wmt_params.status = NULL;
3254 while (fw_size > 0) {
3255 dlen = min_t(int, 250, fw_size);
3257 /* Tell deivice the position in sequence */
3258 if (fw_size - dlen <= 0)
3260 else if (fw_size < fw->size - 30)
3263 wmt_params.flag = flag;
3264 wmt_params.dlen = dlen;
3265 wmt_params.data = fw_ptr;
3267 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3269 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3271 goto err_release_fw;
3278 wmt_params.op = BTMTK_WMT_RST;
3279 wmt_params.flag = 4;
3280 wmt_params.dlen = 0;
3281 wmt_params.data = NULL;
3282 wmt_params.status = NULL;
3284 /* Activate funciton the firmware providing to */
3285 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3287 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
3288 goto err_release_fw;
3291 /* Wait a few moments for firmware activation done */
3292 usleep_range(10000, 12000);
3295 release_firmware(fw);
3300 static int btusb_mtk_func_query(struct hci_dev *hdev)
3302 struct btmtk_hci_wmt_params wmt_params;
3306 /* Query whether the function is enabled */
3307 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3308 wmt_params.flag = 4;
3309 wmt_params.dlen = sizeof(param);
3310 wmt_params.data = ¶m;
3311 wmt_params.status = &status;
3313 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3315 bt_dev_err(hdev, "Failed to query function status (%d)", err);
3322 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
3324 int pipe, err, size = sizeof(u32);
3327 buf = kzalloc(size, GFP_KERNEL);
3331 pipe = usb_rcvctrlpipe(data->udev, 0);
3332 err = usb_control_msg(data->udev, pipe, 0x63,
3333 USB_TYPE_VENDOR | USB_DIR_IN,
3334 reg >> 16, reg & 0xffff,
3335 buf, size, USB_CTRL_SET_TIMEOUT);
3339 *val = get_unaligned_le32(buf);
3347 static int btusb_mtk_id_get(struct btusb_data *data, u32 *id)
3349 return btusb_mtk_reg_read(data, 0x80000008, id);
3352 static int btusb_mtk_setup(struct hci_dev *hdev)
3354 struct btusb_data *data = hci_get_drvdata(hdev);
3355 struct btmtk_hci_wmt_params wmt_params;
3356 ktime_t calltime, delta, rettime;
3357 struct btmtk_tci_sleep tci_sleep;
3358 unsigned long long duration;
3359 struct sk_buff *skb;
3365 calltime = ktime_get();
3367 err = btusb_mtk_id_get(data, &dev_id);
3369 bt_dev_err(hdev, "Failed to get device id (%d)", err);
3375 fwname = FIRMWARE_MT7663;
3378 fwname = FIRMWARE_MT7668;
3381 bt_dev_err(hdev, "Unsupported support hardware variant (%08x)",
3386 /* Query whether the firmware is already download */
3387 wmt_params.op = BTMTK_WMT_SEMAPHORE;
3388 wmt_params.flag = 1;
3389 wmt_params.dlen = 0;
3390 wmt_params.data = NULL;
3391 wmt_params.status = &status;
3393 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3395 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
3399 if (status == BTMTK_WMT_PATCH_DONE) {
3400 bt_dev_info(hdev, "firmware already downloaded");
3401 goto ignore_setup_fw;
3404 /* Setup a firmware which the device definitely requires */
3405 err = btusb_mtk_setup_firmware(hdev, fwname);
3410 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
3411 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
3413 /* -ETIMEDOUT happens */
3417 /* The other errors happen in btusb_mtk_func_query */
3421 if (status == BTMTK_WMT_ON_DONE) {
3422 bt_dev_info(hdev, "function already on");
3423 goto ignore_func_on;
3426 /* Enable Bluetooth protocol */
3428 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3429 wmt_params.flag = 0;
3430 wmt_params.dlen = sizeof(param);
3431 wmt_params.data = ¶m;
3432 wmt_params.status = NULL;
3434 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3436 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3441 /* Apply the low power environment setup */
3442 tci_sleep.mode = 0x5;
3443 tci_sleep.duration = cpu_to_le16(0x640);
3444 tci_sleep.host_duration = cpu_to_le16(0x640);
3445 tci_sleep.host_wakeup_pin = 0;
3446 tci_sleep.time_compensation = 0;
3448 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
3452 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
3457 rettime = ktime_get();
3458 delta = ktime_sub(rettime, calltime);
3459 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3461 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
3466 static int btusb_mtk_shutdown(struct hci_dev *hdev)
3468 struct btmtk_hci_wmt_params wmt_params;
3472 /* Disable the device */
3473 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3474 wmt_params.flag = 0;
3475 wmt_params.dlen = sizeof(param);
3476 wmt_params.data = ¶m;
3477 wmt_params.status = NULL;
3479 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3481 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3488 MODULE_FIRMWARE(FIRMWARE_MT7663);
3489 MODULE_FIRMWARE(FIRMWARE_MT7668);
3492 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3493 static int marvell_config_oob_wake(struct hci_dev *hdev)
3495 struct sk_buff *skb;
3496 struct btusb_data *data = hci_get_drvdata(hdev);
3497 struct device *dev = &data->udev->dev;
3498 u16 pin, gap, opcode;
3502 /* Move on if no wakeup pin specified */
3503 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3504 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3507 /* Vendor specific command to configure a GPIO as wake-up pin */
3508 opcode = hci_opcode_pack(0x3F, 0x59);
3509 cmd[0] = opcode & 0xFF;
3510 cmd[1] = opcode >> 8;
3511 cmd[2] = 2; /* length of parameters that follow */
3513 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3515 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3517 bt_dev_err(hdev, "%s: No memory\n", __func__);
3521 skb_put_data(skb, cmd, sizeof(cmd));
3522 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3524 ret = btusb_send_frame(hdev, skb);
3526 bt_dev_err(hdev, "%s: configuration failed\n", __func__);
3535 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3536 const bdaddr_t *bdaddr)
3538 struct sk_buff *skb;
3543 buf[1] = sizeof(bdaddr_t);
3544 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3546 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3549 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3558 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3559 const bdaddr_t *bdaddr)
3561 struct sk_buff *skb;
3568 buf[3] = sizeof(bdaddr_t);
3569 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3571 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3574 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3582 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3583 const bdaddr_t *bdaddr)
3585 struct sk_buff *skb;
3589 memcpy(buf, bdaddr, sizeof(bdaddr_t));
3591 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3592 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3595 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3603 #define QCA_DFU_PACKET_LEN 4096
3605 #define QCA_GET_TARGET_VERSION 0x09
3606 #define QCA_CHECK_STATUS 0x05
3607 #define QCA_DFU_DOWNLOAD 0x01
3609 #define QCA_SYSCFG_UPDATED 0x40
3610 #define QCA_PATCH_UPDATED 0x80
3611 #define QCA_DFU_TIMEOUT 3000
3613 struct qca_version {
3615 __le32 patch_version;
3621 struct qca_rampatch_version {
3622 __le16 rom_version_high;
3623 __le16 rom_version_low;
3624 __le16 patch_version;
3627 struct qca_device_info {
3629 u8 rampatch_hdr; /* length of header in rampatch */
3630 u8 nvm_hdr; /* length of header in NVM */
3631 u8 ver_offset; /* offset of version structure in rampatch */
3634 static const struct qca_device_info qca_devices_table[] = {
3635 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3636 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3637 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3638 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3639 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3640 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3641 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3642 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3645 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3646 void *data, u16 size)
3651 buf = kmalloc(size, GFP_KERNEL);
3655 /* Found some of USB hosts have IOT issues with ours so that we should
3656 * not wait until HCI layer is ready.
3658 pipe = usb_rcvctrlpipe(udev, 0);
3659 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3660 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3662 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3666 memcpy(data, buf, size);
3674 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3675 const struct firmware *firmware,
3678 struct btusb_data *btdata = hci_get_drvdata(hdev);
3679 struct usb_device *udev = btdata->udev;
3680 size_t count, size, sent = 0;
3684 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3688 count = firmware->size;
3690 size = min_t(size_t, count, hdr_size);
3691 memcpy(buf, firmware->data, size);
3693 /* USB patches should go down to controller through USB path
3694 * because binary format fits to go down through USB channel.
3695 * USB control path is for patching headers and USB bulk is for
3698 pipe = usb_sndctrlpipe(udev, 0);
3699 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3700 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3702 bt_dev_err(hdev, "Failed to send headers (%d)", err);
3710 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3712 memcpy(buf, firmware->data + sent, size);
3714 pipe = usb_sndbulkpipe(udev, 0x02);
3715 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3718 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3719 sent, firmware->size, err);
3724 bt_dev_err(hdev, "Failed to get bulk buffer");
3738 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3739 struct qca_version *ver,
3740 const struct qca_device_info *info)
3742 struct qca_rampatch_version *rver;
3743 const struct firmware *fw;
3744 u32 ver_rom, ver_patch, rver_rom;
3745 u16 rver_rom_low, rver_rom_high, rver_patch;
3749 ver_rom = le32_to_cpu(ver->rom_version);
3750 ver_patch = le32_to_cpu(ver->patch_version);
3752 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
3754 err = request_firmware(&fw, fwname, &hdev->dev);
3756 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3761 bt_dev_info(hdev, "using rampatch file: %s", fwname);
3763 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3764 rver_rom_low = le16_to_cpu(rver->rom_version_low);
3765 rver_patch = le16_to_cpu(rver->patch_version);
3767 if (ver_rom & ~0xffffU) {
3768 rver_rom_high = le16_to_cpu(rver->rom_version_high);
3769 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
3771 rver_rom = rver_rom_low;
3774 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3775 "firmware rome 0x%x build 0x%x",
3776 rver_rom, rver_patch, ver_rom, ver_patch);
3778 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
3779 bt_dev_err(hdev, "rampatch file version did not match with firmware");
3784 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3787 release_firmware(fw);
3792 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3793 struct qca_version *ver,
3794 const struct qca_device_info *info)
3796 const struct firmware *fw;
3800 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
3801 le32_to_cpu(ver->rom_version));
3803 err = request_firmware(&fw, fwname, &hdev->dev);
3805 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3810 bt_dev_info(hdev, "using NVM file: %s", fwname);
3812 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3814 release_firmware(fw);
3819 /* identify the ROM version and check whether patches are needed */
3820 static bool btusb_qca_need_patch(struct usb_device *udev)
3822 struct qca_version ver;
3824 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3827 /* only low ROM versions need patches */
3828 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3831 static int btusb_setup_qca(struct hci_dev *hdev)
3833 struct btusb_data *btdata = hci_get_drvdata(hdev);
3834 struct usb_device *udev = btdata->udev;
3835 const struct qca_device_info *info = NULL;
3836 struct qca_version ver;
3841 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3846 ver_rom = le32_to_cpu(ver.rom_version);
3848 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
3849 if (ver_rom == qca_devices_table[i].rom_version)
3850 info = &qca_devices_table[i];
3853 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3857 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3862 if (!(status & QCA_PATCH_UPDATED)) {
3863 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3868 if (!(status & QCA_SYSCFG_UPDATED)) {
3869 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3877 static inline int __set_diag_interface(struct hci_dev *hdev)
3879 struct btusb_data *data = hci_get_drvdata(hdev);
3880 struct usb_interface *intf = data->diag;
3886 data->diag_tx_ep = NULL;
3887 data->diag_rx_ep = NULL;
3889 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3890 struct usb_endpoint_descriptor *ep_desc;
3892 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3894 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3895 data->diag_tx_ep = ep_desc;
3899 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3900 data->diag_rx_ep = ep_desc;
3905 if (!data->diag_tx_ep || !data->diag_rx_ep) {
3906 bt_dev_err(hdev, "invalid diagnostic descriptors");
3913 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3915 struct btusb_data *data = hci_get_drvdata(hdev);
3916 struct sk_buff *skb;
3920 if (!data->diag_tx_ep)
3921 return ERR_PTR(-ENODEV);
3923 urb = usb_alloc_urb(0, GFP_KERNEL);
3925 return ERR_PTR(-ENOMEM);
3927 skb = bt_skb_alloc(2, GFP_KERNEL);
3930 return ERR_PTR(-ENOMEM);
3933 skb_put_u8(skb, 0xf0);
3934 skb_put_u8(skb, enable);
3936 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3938 usb_fill_bulk_urb(urb, data->udev, pipe,
3939 skb->data, skb->len, btusb_tx_complete, skb);
3941 skb->dev = (void *)hdev;
3946 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3948 struct btusb_data *data = hci_get_drvdata(hdev);
3954 if (!test_bit(HCI_RUNNING, &hdev->flags))
3957 urb = alloc_diag_urb(hdev, enable);
3959 return PTR_ERR(urb);
3961 return submit_or_queue_tx_urb(hdev, urb);
3965 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3967 struct btusb_data *data = priv;
3969 pm_wakeup_event(&data->udev->dev, 0);
3972 /* Disable only if not already disabled (keep it balanced) */
3973 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3974 disable_irq_nosync(irq);
3975 disable_irq_wake(irq);
3980 static const struct of_device_id btusb_match_table[] = {
3981 { .compatible = "usb1286,204e" },
3982 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3983 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
3986 MODULE_DEVICE_TABLE(of, btusb_match_table);
3988 /* Use an oob wakeup pin? */
3989 static int btusb_config_oob_wake(struct hci_dev *hdev)
3991 struct btusb_data *data = hci_get_drvdata(hdev);
3992 struct device *dev = &data->udev->dev;
3995 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3997 if (!of_match_device(btusb_match_table, dev))
4000 /* Move on if no IRQ specified */
4001 irq = of_irq_get_byname(dev->of_node, "wakeup");
4003 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
4007 irq_set_status_flags(irq, IRQ_NOAUTOEN);
4008 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
4009 0, "OOB Wake-on-BT", data);
4011 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
4015 ret = device_init_wakeup(dev, true);
4017 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
4021 data->oob_wake_irq = irq;
4022 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
4027 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
4029 if (dmi_check_system(btusb_needs_reset_resume_table))
4030 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
4033 static bool btusb_prevent_wake(struct hci_dev *hdev)
4035 struct btusb_data *data = hci_get_drvdata(hdev);
4037 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
4040 return !device_may_wakeup(&data->udev->dev);
4043 static int btusb_probe(struct usb_interface *intf,
4044 const struct usb_device_id *id)
4046 struct usb_endpoint_descriptor *ep_desc;
4047 struct gpio_desc *reset_gpio;
4048 struct btusb_data *data;
4049 struct hci_dev *hdev;
4050 unsigned ifnum_base;
4053 BT_DBG("intf %p id %p", intf, id);
4055 /* interface numbers are hardcoded in the spec */
4056 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
4057 if (!(id->driver_info & BTUSB_IFNUM_2))
4059 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
4063 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
4065 if (!id->driver_info) {
4066 const struct usb_device_id *match;
4068 match = usb_match_id(intf, blacklist_table);
4073 if (id->driver_info == BTUSB_IGNORE)
4076 if (id->driver_info & BTUSB_ATH3012) {
4077 struct usb_device *udev = interface_to_usbdev(intf);
4079 /* Old firmware would otherwise let ath3k driver load
4080 * patch and sysconfig files
4082 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
4083 !btusb_qca_need_patch(udev))
4087 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
4091 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4092 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4094 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
4095 data->intr_ep = ep_desc;
4099 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4100 data->bulk_tx_ep = ep_desc;
4104 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4105 data->bulk_rx_ep = ep_desc;
4110 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
4113 if (id->driver_info & BTUSB_AMP) {
4114 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
4115 data->cmdreq = 0x2b;
4117 data->cmdreq_type = USB_TYPE_CLASS;
4118 data->cmdreq = 0x00;
4121 data->udev = interface_to_usbdev(intf);
4124 INIT_WORK(&data->work, btusb_work);
4125 INIT_WORK(&data->waker, btusb_waker);
4126 init_usb_anchor(&data->deferred);
4127 init_usb_anchor(&data->tx_anchor);
4128 spin_lock_init(&data->txlock);
4130 init_usb_anchor(&data->intr_anchor);
4131 init_usb_anchor(&data->bulk_anchor);
4132 init_usb_anchor(&data->isoc_anchor);
4133 init_usb_anchor(&data->diag_anchor);
4134 init_usb_anchor(&data->ctrl_anchor);
4135 spin_lock_init(&data->rxlock);
4137 if (id->driver_info & BTUSB_INTEL_NEW) {
4138 data->recv_event = btusb_recv_event_intel;
4139 data->recv_bulk = btusb_recv_bulk_intel;
4140 set_bit(BTUSB_BOOTLOADER, &data->flags);
4142 data->recv_event = hci_recv_frame;
4143 data->recv_bulk = btusb_recv_bulk;
4146 hdev = hci_alloc_dev();
4150 hdev->bus = HCI_USB;
4151 hci_set_drvdata(hdev, data);
4153 if (id->driver_info & BTUSB_AMP)
4154 hdev->dev_type = HCI_AMP;
4156 hdev->dev_type = HCI_PRIMARY;
4160 SET_HCIDEV_DEV(hdev, &intf->dev);
4162 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
4164 if (IS_ERR(reset_gpio)) {
4165 err = PTR_ERR(reset_gpio);
4167 } else if (reset_gpio) {
4168 data->reset_gpio = reset_gpio;
4171 hdev->open = btusb_open;
4172 hdev->close = btusb_close;
4173 hdev->flush = btusb_flush;
4174 hdev->send = btusb_send_frame;
4175 hdev->notify = btusb_notify;
4176 hdev->prevent_wake = btusb_prevent_wake;
4179 err = btusb_config_oob_wake(hdev);
4183 /* Marvell devices may need a specific chip configuration */
4184 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
4185 err = marvell_config_oob_wake(hdev);
4190 if (id->driver_info & BTUSB_CW6622)
4191 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4193 if (id->driver_info & BTUSB_BCM2045)
4194 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4196 if (id->driver_info & BTUSB_BCM92035)
4197 hdev->setup = btusb_setup_bcm92035;
4199 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4200 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
4201 hdev->manufacturer = 15;
4202 hdev->setup = btbcm_setup_patchram;
4203 hdev->set_diag = btusb_bcm_set_diag;
4204 hdev->set_bdaddr = btbcm_set_bdaddr;
4206 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4207 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4210 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4211 (id->driver_info & BTUSB_BCM_APPLE)) {
4212 hdev->manufacturer = 15;
4213 hdev->setup = btbcm_setup_apple;
4214 hdev->set_diag = btusb_bcm_set_diag;
4216 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4217 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4220 if (id->driver_info & BTUSB_INTEL) {
4221 hdev->manufacturer = 2;
4222 hdev->setup = btusb_setup_intel;
4223 hdev->shutdown = btusb_shutdown_intel;
4224 hdev->set_diag = btintel_set_diag_mfg;
4225 hdev->set_bdaddr = btintel_set_bdaddr;
4226 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4227 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4228 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4229 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4232 if (id->driver_info & BTUSB_INTEL_NEW) {
4233 hdev->manufacturer = 2;
4234 hdev->send = btusb_send_frame_intel;
4235 hdev->setup = btusb_setup_intel_new;
4236 hdev->shutdown = btusb_shutdown_intel_new;
4237 hdev->hw_error = btintel_hw_error;
4238 hdev->set_diag = btintel_set_diag;
4239 hdev->set_bdaddr = btintel_set_bdaddr;
4240 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4241 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4242 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4243 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4246 if (id->driver_info & BTUSB_INTEL_NEWGEN) {
4247 hdev->manufacturer = 2;
4248 hdev->send = btusb_send_frame_intel;
4249 hdev->setup = btusb_setup_intel_newgen;
4250 hdev->shutdown = btusb_shutdown_intel_new;
4251 hdev->hw_error = btintel_hw_error;
4252 hdev->set_diag = btintel_set_diag;
4253 hdev->set_bdaddr = btintel_set_bdaddr;
4254 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4255 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4256 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4257 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4259 data->recv_event = btusb_recv_event_intel;
4260 data->recv_bulk = btusb_recv_bulk_intel;
4261 set_bit(BTUSB_BOOTLOADER, &data->flags);
4264 if (id->driver_info & BTUSB_MARVELL)
4265 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4267 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4268 (id->driver_info & BTUSB_MEDIATEK)) {
4269 hdev->setup = btusb_mtk_setup;
4270 hdev->shutdown = btusb_mtk_shutdown;
4271 hdev->manufacturer = 70;
4272 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
4275 if (id->driver_info & BTUSB_SWAVE) {
4276 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
4277 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
4280 if (id->driver_info & BTUSB_INTEL_BOOT) {
4281 hdev->manufacturer = 2;
4282 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4285 if (id->driver_info & BTUSB_ATH3012) {
4286 data->setup_on_usb = btusb_setup_qca;
4287 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4288 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4289 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4292 if (id->driver_info & BTUSB_QCA_ROME) {
4293 data->setup_on_usb = btusb_setup_qca;
4294 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4295 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4296 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4297 btusb_check_needs_reset_resume(intf);
4300 if (id->driver_info & BTUSB_QCA_WCN6855) {
4301 data->setup_on_usb = btusb_setup_qca;
4302 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
4303 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4304 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4307 if (id->driver_info & BTUSB_AMP) {
4308 /* AMP controllers do not support SCO packets */
4311 /* Interface orders are hardcoded in the specification */
4312 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
4313 data->isoc_ifnum = ifnum_base + 1;
4316 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4317 (id->driver_info & BTUSB_REALTEK)) {
4318 hdev->setup = btrtl_setup_realtek;
4319 hdev->shutdown = btrtl_shutdown_realtek;
4320 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
4322 /* Realtek devices lose their updated firmware over global
4323 * suspend that means host doesn't send SET_FEATURE
4324 * (DEVICE_REMOTE_WAKEUP)
4326 set_bit(BTUSB_WAKEUP_DISABLE, &data->flags);
4327 if (btusb_find_altsetting(data, 1))
4328 set_bit(BTUSB_USE_ALT1_FOR_WBS, &data->flags);
4330 bt_dev_err(hdev, "Device does not support ALT setting 1");
4334 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4336 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
4337 if (!disable_scofix)
4338 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
4341 if (id->driver_info & BTUSB_BROKEN_ISOC)
4344 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
4345 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
4347 if (id->driver_info & BTUSB_VALID_LE_STATES)
4348 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
4350 if (id->driver_info & BTUSB_DIGIANSWER) {
4351 data->cmdreq_type = USB_TYPE_VENDOR;
4352 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4355 if (id->driver_info & BTUSB_CSR) {
4356 struct usb_device *udev = data->udev;
4357 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
4359 /* Old firmware would otherwise execute USB reset */
4360 if (bcdDevice < 0x117)
4361 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4363 /* This must be set first in case we disable it for fakes */
4364 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4366 /* Fake CSR devices with broken commands */
4367 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
4368 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4369 hdev->setup = btusb_setup_csr;
4372 if (id->driver_info & BTUSB_SNIFFER) {
4373 struct usb_device *udev = data->udev;
4375 /* New sniffer firmware has crippled HCI interface */
4376 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4377 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4380 if (id->driver_info & BTUSB_INTEL_BOOT) {
4381 /* A bug in the bootloader causes that interrupt interface is
4382 * only enabled after receiving SetInterface(0, AltSetting=0).
4384 err = usb_set_interface(data->udev, 0, 0);
4386 BT_ERR("failed to set interface 0, alt 0 %d", err);
4392 err = usb_driver_claim_interface(&btusb_driver,
4398 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
4399 if (!usb_driver_claim_interface(&btusb_driver,
4401 __set_diag_interface(hdev);
4406 if (enable_autosuspend)
4407 usb_enable_autosuspend(data->udev);
4409 err = hci_register_dev(hdev);
4413 usb_set_intfdata(intf, data);
4418 if (data->reset_gpio)
4419 gpiod_put(data->reset_gpio);
4424 static void btusb_disconnect(struct usb_interface *intf)
4426 struct btusb_data *data = usb_get_intfdata(intf);
4427 struct hci_dev *hdev;
4429 BT_DBG("intf %p", intf);
4435 usb_set_intfdata(data->intf, NULL);
4438 usb_set_intfdata(data->isoc, NULL);
4441 usb_set_intfdata(data->diag, NULL);
4443 hci_unregister_dev(hdev);
4445 if (intf == data->intf) {
4447 usb_driver_release_interface(&btusb_driver, data->isoc);
4449 usb_driver_release_interface(&btusb_driver, data->diag);
4450 } else if (intf == data->isoc) {
4452 usb_driver_release_interface(&btusb_driver, data->diag);
4453 usb_driver_release_interface(&btusb_driver, data->intf);
4454 } else if (intf == data->diag) {
4455 usb_driver_release_interface(&btusb_driver, data->intf);
4457 usb_driver_release_interface(&btusb_driver, data->isoc);
4460 if (data->oob_wake_irq)
4461 device_init_wakeup(&data->udev->dev, false);
4463 if (data->reset_gpio)
4464 gpiod_put(data->reset_gpio);
4470 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4472 struct btusb_data *data = usb_get_intfdata(intf);
4474 BT_DBG("intf %p", intf);
4476 if (data->suspend_count++)
4479 spin_lock_irq(&data->txlock);
4480 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4481 set_bit(BTUSB_SUSPENDING, &data->flags);
4482 spin_unlock_irq(&data->txlock);
4484 spin_unlock_irq(&data->txlock);
4485 data->suspend_count--;
4489 cancel_work_sync(&data->work);
4491 btusb_stop_traffic(data);
4492 usb_kill_anchored_urbs(&data->tx_anchor);
4494 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4495 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4496 enable_irq_wake(data->oob_wake_irq);
4497 enable_irq(data->oob_wake_irq);
4500 /* For global suspend, Realtek devices lose the loaded fw
4501 * in them. But for autosuspend, firmware should remain.
4502 * Actually, it depends on whether the usb host sends
4503 * set feature (enable wakeup) or not.
4505 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags)) {
4506 if (PMSG_IS_AUTO(message) &&
4507 device_can_wakeup(&data->udev->dev))
4508 data->udev->do_remote_wakeup = 1;
4509 else if (!PMSG_IS_AUTO(message))
4510 data->udev->reset_resume = 1;
4516 static void play_deferred(struct btusb_data *data)
4521 while ((urb = usb_get_from_anchor(&data->deferred))) {
4522 usb_anchor_urb(urb, &data->tx_anchor);
4524 err = usb_submit_urb(urb, GFP_ATOMIC);
4526 if (err != -EPERM && err != -ENODEV)
4527 BT_ERR("%s urb %p submission failed (%d)",
4528 data->hdev->name, urb, -err);
4529 kfree(urb->setup_packet);
4530 usb_unanchor_urb(urb);
4535 data->tx_in_flight++;
4539 /* Cleanup the rest deferred urbs. */
4540 while ((urb = usb_get_from_anchor(&data->deferred))) {
4541 kfree(urb->setup_packet);
4546 static int btusb_resume(struct usb_interface *intf)
4548 struct btusb_data *data = usb_get_intfdata(intf);
4549 struct hci_dev *hdev = data->hdev;
4552 BT_DBG("intf %p", intf);
4554 if (--data->suspend_count)
4557 /* Disable only if not already disabled (keep it balanced) */
4558 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4559 disable_irq(data->oob_wake_irq);
4560 disable_irq_wake(data->oob_wake_irq);
4563 if (!test_bit(HCI_RUNNING, &hdev->flags))
4566 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4567 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4569 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4574 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4575 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4577 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4581 btusb_submit_bulk_urb(hdev, GFP_NOIO);
4584 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4585 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4586 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4588 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4591 spin_lock_irq(&data->txlock);
4592 play_deferred(data);
4593 clear_bit(BTUSB_SUSPENDING, &data->flags);
4594 spin_unlock_irq(&data->txlock);
4595 schedule_work(&data->work);
4600 usb_scuttle_anchored_urbs(&data->deferred);
4602 spin_lock_irq(&data->txlock);
4603 clear_bit(BTUSB_SUSPENDING, &data->flags);
4604 spin_unlock_irq(&data->txlock);
4610 static struct usb_driver btusb_driver = {
4612 .probe = btusb_probe,
4613 .disconnect = btusb_disconnect,
4615 .suspend = btusb_suspend,
4616 .resume = btusb_resume,
4618 .id_table = btusb_table,
4619 .supports_autosuspend = 1,
4620 .disable_hub_initiated_lpm = 1,
4623 module_usb_driver(btusb_driver);
4625 module_param(disable_scofix, bool, 0644);
4626 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4628 module_param(force_scofix, bool, 0644);
4629 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4631 module_param(enable_autosuspend, bool, 0644);
4632 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4634 module_param(reset, bool, 0644);
4635 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4637 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4638 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4639 MODULE_VERSION(VERSION);
4640 MODULE_LICENSE("GPL");