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_COMBINED 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_AMP 0x4000
52 #define BTUSB_QCA_ROME 0x8000
53 #define BTUSB_BCM_APPLE 0x10000
54 #define BTUSB_REALTEK 0x20000
55 #define BTUSB_BCM2045 0x40000
56 #define BTUSB_IFNUM_2 0x80000
57 #define BTUSB_CW6622 0x100000
58 #define BTUSB_MEDIATEK 0x200000
59 #define BTUSB_WIDEBAND_SPEECH 0x400000
60 #define BTUSB_VALID_LE_STATES 0x800000
61 #define BTUSB_QCA_WCN6855 0x1000000
62 #define BTUSB_INTEL_BROKEN_INITIAL_NCMD 0x4000000
64 static const struct usb_device_id btusb_table[] = {
65 /* Generic Bluetooth USB device */
66 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
68 /* Generic Bluetooth AMP device */
69 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
71 /* Generic Bluetooth USB interface */
72 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
74 /* Apple-specific (Broadcom) devices */
75 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
76 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
78 /* MediaTek MT76x0E */
79 { USB_DEVICE(0x0e8d, 0x763f) },
81 /* Broadcom SoftSailing reporting vendor specific */
82 { USB_DEVICE(0x0a5c, 0x21e1) },
84 /* Apple MacBookPro 7,1 */
85 { USB_DEVICE(0x05ac, 0x8213) },
88 { USB_DEVICE(0x05ac, 0x8215) },
90 /* Apple MacBookPro6,2 */
91 { USB_DEVICE(0x05ac, 0x8218) },
93 /* Apple MacBookAir3,1, MacBookAir3,2 */
94 { USB_DEVICE(0x05ac, 0x821b) },
96 /* Apple MacBookAir4,1 */
97 { USB_DEVICE(0x05ac, 0x821f) },
99 /* Apple MacBookPro8,2 */
100 { USB_DEVICE(0x05ac, 0x821a) },
102 /* Apple MacMini5,1 */
103 { USB_DEVICE(0x05ac, 0x8281) },
105 /* AVM BlueFRITZ! USB v2.0 */
106 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
108 /* Bluetooth Ultraport Module from IBM */
109 { USB_DEVICE(0x04bf, 0x030a) },
111 /* ALPS Modules with non-standard id */
112 { USB_DEVICE(0x044e, 0x3001) },
113 { USB_DEVICE(0x044e, 0x3002) },
115 /* Ericsson with non-standard id */
116 { USB_DEVICE(0x0bdb, 0x1002) },
118 /* Canyon CN-BTU1 with HID interfaces */
119 { USB_DEVICE(0x0c10, 0x0000) },
121 /* Broadcom BCM20702B0 (Dynex/Insignia) */
122 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
124 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
125 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
126 .driver_info = BTUSB_BCM_PATCHRAM },
128 /* Broadcom BCM920703 (HTC Vive) */
129 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
130 .driver_info = BTUSB_BCM_PATCHRAM },
132 /* Foxconn - Hon Hai */
133 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
134 .driver_info = BTUSB_BCM_PATCHRAM },
136 /* Lite-On Technology - Broadcom based */
137 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
138 .driver_info = BTUSB_BCM_PATCHRAM },
140 /* Broadcom devices with vendor specific id */
141 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
142 .driver_info = BTUSB_BCM_PATCHRAM },
144 /* ASUSTek Computer - Broadcom based */
145 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
146 .driver_info = BTUSB_BCM_PATCHRAM },
148 /* Belkin F8065bf - Broadcom based */
149 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
150 .driver_info = BTUSB_BCM_PATCHRAM },
152 /* IMC Networks - Broadcom based */
153 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
154 .driver_info = BTUSB_BCM_PATCHRAM },
156 /* Dell Computer - Broadcom based */
157 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
158 .driver_info = BTUSB_BCM_PATCHRAM },
160 /* Toshiba Corp - Broadcom based */
161 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
162 .driver_info = BTUSB_BCM_PATCHRAM },
164 /* Intel Bluetooth USB Bootloader (RAM module) */
165 { USB_DEVICE(0x8087, 0x0a5a),
166 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
168 { } /* Terminating entry */
171 MODULE_DEVICE_TABLE(usb, btusb_table);
173 static const struct usb_device_id blacklist_table[] = {
174 /* CSR BlueCore devices */
175 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
177 /* Broadcom BCM2033 without firmware */
178 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
180 /* Broadcom BCM2045 devices */
181 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
183 /* Atheros 3011 with sflash firmware */
184 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
185 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
186 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
187 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
188 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
189 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
190 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
192 /* Atheros AR9285 Malbec with sflash firmware */
193 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
195 /* Atheros 3012 with sflash firmware */
196 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
197 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
198 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
199 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
200 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
247 /* Atheros AR5BBU12 with sflash firmware */
248 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
250 /* Atheros AR5BBU12 with sflash firmware */
251 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
252 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
254 /* QCA ROME chipset */
255 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
256 BTUSB_WIDEBAND_SPEECH },
257 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
258 BTUSB_WIDEBAND_SPEECH },
259 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
260 BTUSB_WIDEBAND_SPEECH },
261 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
262 BTUSB_WIDEBAND_SPEECH },
263 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
264 BTUSB_WIDEBAND_SPEECH },
265 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
266 BTUSB_WIDEBAND_SPEECH },
267 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
268 BTUSB_WIDEBAND_SPEECH },
269 { USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
270 BTUSB_WIDEBAND_SPEECH },
271 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
272 BTUSB_WIDEBAND_SPEECH },
273 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
274 BTUSB_WIDEBAND_SPEECH },
275 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
276 BTUSB_WIDEBAND_SPEECH },
277 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
278 BTUSB_WIDEBAND_SPEECH },
279 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
280 BTUSB_WIDEBAND_SPEECH },
281 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
282 BTUSB_WIDEBAND_SPEECH },
283 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
284 BTUSB_WIDEBAND_SPEECH },
285 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
286 BTUSB_WIDEBAND_SPEECH },
287 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
288 BTUSB_WIDEBAND_SPEECH },
289 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
290 BTUSB_WIDEBAND_SPEECH },
291 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
292 BTUSB_WIDEBAND_SPEECH },
294 /* QCA WCN6855 chipset */
295 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
296 BTUSB_WIDEBAND_SPEECH |
297 BTUSB_VALID_LE_STATES },
299 /* Broadcom BCM2035 */
300 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
301 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
302 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
304 /* Broadcom BCM2045 */
305 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
306 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
308 /* IBM/Lenovo ThinkPad with Broadcom chip */
309 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
310 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
312 /* HP laptop with Broadcom chip */
313 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
315 /* Dell laptop with Broadcom chip */
316 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
318 /* Dell Wireless 370 and 410 devices */
319 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
320 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
322 /* Belkin F8T012 and F8T013 devices */
323 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
324 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
326 /* Asus WL-BTD202 device */
327 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
329 /* Kensington Bluetooth USB adapter */
330 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
332 /* RTX Telecom based adapters with buggy SCO support */
333 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
334 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
336 /* CONWISE Technology based adapters with buggy SCO support */
337 { USB_DEVICE(0x0e5e, 0x6622),
338 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
340 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
341 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
343 /* Digianswer devices */
344 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
345 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
347 /* CSR BlueCore Bluetooth Sniffer */
348 { USB_DEVICE(0x0a12, 0x0002),
349 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
351 /* Frontline ComProbe Bluetooth Sniffer */
352 { USB_DEVICE(0x16d3, 0x0002),
353 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
355 /* Marvell Bluetooth devices */
356 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
357 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
358 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
360 /* Intel Bluetooth devices */
361 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_COMBINED },
362 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_COMBINED },
363 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_COMBINED },
364 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_COMBINED },
365 { USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_COMBINED },
366 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
367 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL_COMBINED |
368 BTUSB_INTEL_BROKEN_INITIAL_NCMD },
369 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL_COMBINED },
370 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_COMBINED },
371 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL_COMBINED },
372 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_COMBINED },
374 /* Other Intel Bluetooth devices */
375 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
376 .driver_info = BTUSB_IGNORE },
378 /* Realtek 8822CE Bluetooth devices */
379 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
380 BTUSB_WIDEBAND_SPEECH },
381 { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
382 BTUSB_WIDEBAND_SPEECH },
384 /* Realtek 8852AE Bluetooth devices */
385 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
386 BTUSB_WIDEBAND_SPEECH },
387 { USB_DEVICE(0x0bda, 0x4852), .driver_info = BTUSB_REALTEK |
388 BTUSB_WIDEBAND_SPEECH },
389 { USB_DEVICE(0x04c5, 0x165c), .driver_info = BTUSB_REALTEK |
390 BTUSB_WIDEBAND_SPEECH },
391 { USB_DEVICE(0x04ca, 0x4006), .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 |
401 BTUSB_WIDEBAND_SPEECH |
402 BTUSB_VALID_LE_STATES },
404 /* Additional MediaTek MT7615E Bluetooth devices */
405 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
407 /* Additional MediaTek MT7668 Bluetooth devices */
408 { USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
409 BTUSB_WIDEBAND_SPEECH |
410 BTUSB_VALID_LE_STATES },
412 /* Additional MediaTek MT7921 Bluetooth devices */
413 { USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
414 BTUSB_WIDEBAND_SPEECH |
415 BTUSB_VALID_LE_STATES },
416 { USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
417 BTUSB_WIDEBAND_SPEECH |
418 BTUSB_VALID_LE_STATES },
419 { USB_DEVICE(0x13d3, 0x3564), .driver_info = BTUSB_MEDIATEK |
420 BTUSB_WIDEBAND_SPEECH |
421 BTUSB_VALID_LE_STATES },
422 { USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
423 BTUSB_WIDEBAND_SPEECH |
424 BTUSB_VALID_LE_STATES },
426 /* Additional Realtek 8723AE Bluetooth devices */
427 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
428 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
430 /* Additional Realtek 8723BE Bluetooth devices */
431 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
432 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
433 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
434 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
435 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
436 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
438 /* Additional Realtek 8723BU Bluetooth devices */
439 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
441 /* Additional Realtek 8723DE Bluetooth devices */
442 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
443 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
445 /* Additional Realtek 8761B Bluetooth devices */
446 { USB_DEVICE(0x2357, 0x0604), .driver_info = BTUSB_REALTEK |
447 BTUSB_WIDEBAND_SPEECH },
449 /* Additional Realtek 8761BU Bluetooth devices */
450 { USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
451 BTUSB_WIDEBAND_SPEECH },
453 /* Additional Realtek 8821AE Bluetooth devices */
454 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
455 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
456 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
457 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
458 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
460 /* Additional Realtek 8822BE Bluetooth devices */
461 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
462 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
464 /* Additional Realtek 8822CE Bluetooth devices */
465 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
466 BTUSB_WIDEBAND_SPEECH },
467 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
468 BTUSB_WIDEBAND_SPEECH },
469 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
470 BTUSB_WIDEBAND_SPEECH },
471 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
472 BTUSB_WIDEBAND_SPEECH },
473 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
474 BTUSB_WIDEBAND_SPEECH },
475 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
476 BTUSB_WIDEBAND_SPEECH },
477 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
478 BTUSB_WIDEBAND_SPEECH },
479 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
480 BTUSB_WIDEBAND_SPEECH },
481 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
482 BTUSB_WIDEBAND_SPEECH },
483 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
484 BTUSB_WIDEBAND_SPEECH },
485 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
486 BTUSB_WIDEBAND_SPEECH },
488 /* Silicon Wave based devices */
489 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
491 { } /* Terminating entry */
494 /* The Bluetooth USB module build into some devices needs to be reset on resume,
495 * this is a problem with the platform (likely shutting off all power) not with
496 * the module itself. So we use a DMI list to match known broken platforms.
498 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
500 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
502 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
503 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
507 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
509 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
510 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
514 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
516 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
517 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
523 #define BTUSB_MAX_ISOC_FRAMES 10
525 #define BTUSB_INTR_RUNNING 0
526 #define BTUSB_BULK_RUNNING 1
527 #define BTUSB_ISOC_RUNNING 2
528 #define BTUSB_SUSPENDING 3
529 #define BTUSB_DID_ISO_RESUME 4
530 #define BTUSB_BOOTLOADER 5
531 #define BTUSB_DOWNLOADING 6
532 #define BTUSB_FIRMWARE_LOADED 7
533 #define BTUSB_FIRMWARE_FAILED 8
534 #define BTUSB_BOOTING 9
535 #define BTUSB_DIAG_RUNNING 10
536 #define BTUSB_OOB_WAKE_ENABLED 11
537 #define BTUSB_HW_RESET_ACTIVE 12
538 #define BTUSB_TX_WAIT_VND_EVT 13
539 #define BTUSB_WAKEUP_AUTOSUSPEND 14
540 #define BTUSB_USE_ALT3_FOR_WBS 15
543 struct hci_dev *hdev;
544 struct usb_device *udev;
545 struct usb_interface *intf;
546 struct usb_interface *isoc;
547 struct usb_interface *diag;
552 struct work_struct work;
553 struct work_struct waker;
555 struct usb_anchor deferred;
556 struct usb_anchor tx_anchor;
560 struct usb_anchor intr_anchor;
561 struct usb_anchor bulk_anchor;
562 struct usb_anchor isoc_anchor;
563 struct usb_anchor diag_anchor;
564 struct usb_anchor ctrl_anchor;
567 struct sk_buff *evt_skb;
568 struct sk_buff *acl_skb;
569 struct sk_buff *sco_skb;
571 struct usb_endpoint_descriptor *intr_ep;
572 struct usb_endpoint_descriptor *bulk_tx_ep;
573 struct usb_endpoint_descriptor *bulk_rx_ep;
574 struct usb_endpoint_descriptor *isoc_tx_ep;
575 struct usb_endpoint_descriptor *isoc_rx_ep;
576 struct usb_endpoint_descriptor *diag_tx_ep;
577 struct usb_endpoint_descriptor *diag_rx_ep;
579 struct gpio_desc *reset_gpio;
584 unsigned int sco_num;
585 unsigned int air_mode;
586 bool usb_alt6_packet_flow;
590 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
591 int (*recv_acl)(struct hci_dev *hdev, struct sk_buff *skb);
592 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
594 int (*setup_on_usb)(struct hci_dev *hdev);
596 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
597 unsigned cmd_timeout_cnt;
600 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
602 struct btusb_data *data = hci_get_drvdata(hdev);
603 struct gpio_desc *reset_gpio = data->reset_gpio;
605 if (++data->cmd_timeout_cnt < 5)
609 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
614 * Toggle the hard reset line if the platform provides one. The reset
615 * is going to yank the device off the USB and then replug. So doing
616 * once is enough. The cleanup is handled correctly on the way out
617 * (standard USB disconnect), and the new device is detected cleanly
618 * and bound to the driver again like it should be.
620 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
621 bt_dev_err(hdev, "last reset failed? Not resetting again");
625 bt_dev_err(hdev, "Initiating HW reset via gpio");
626 gpiod_set_value_cansleep(reset_gpio, 1);
628 gpiod_set_value_cansleep(reset_gpio, 0);
631 static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
633 struct btusb_data *data = hci_get_drvdata(hdev);
634 struct gpio_desc *reset_gpio = data->reset_gpio;
636 if (++data->cmd_timeout_cnt < 5)
640 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
644 /* Toggle the hard reset line. The Realtek device is going to
645 * yank itself off the USB and then replug. The cleanup is handled
646 * correctly on the way out (standard USB disconnect), and the new
647 * device is detected cleanly and bound to the driver again like
650 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
651 bt_dev_err(hdev, "last reset failed? Not resetting again");
655 bt_dev_err(hdev, "Reset Realtek device via gpio");
656 gpiod_set_value_cansleep(reset_gpio, 1);
658 gpiod_set_value_cansleep(reset_gpio, 0);
661 static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
663 struct btusb_data *data = hci_get_drvdata(hdev);
664 struct gpio_desc *reset_gpio = data->reset_gpio;
667 if (++data->cmd_timeout_cnt < 5)
671 bt_dev_err(hdev, "Reset qca device via bt_en gpio");
673 /* Toggle the hard reset line. The qca bt device is going to
674 * yank itself off the USB and then replug. The cleanup is handled
675 * correctly on the way out (standard USB disconnect), and the new
676 * device is detected cleanly and bound to the driver again like
679 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
680 bt_dev_err(hdev, "last reset failed? Not resetting again");
684 gpiod_set_value_cansleep(reset_gpio, 0);
686 gpiod_set_value_cansleep(reset_gpio, 1);
691 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
692 /* This is not an unbalanced PM reference since the device will reset */
693 err = usb_autopm_get_interface(data->intf);
695 usb_queue_reset_device(data->intf);
697 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
700 static inline void btusb_free_frags(struct btusb_data *data)
704 spin_lock_irqsave(&data->rxlock, flags);
706 kfree_skb(data->evt_skb);
707 data->evt_skb = NULL;
709 kfree_skb(data->acl_skb);
710 data->acl_skb = NULL;
712 kfree_skb(data->sco_skb);
713 data->sco_skb = NULL;
715 spin_unlock_irqrestore(&data->rxlock, flags);
718 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
724 spin_lock_irqsave(&data->rxlock, flags);
731 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
737 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
738 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
741 len = min_t(uint, hci_skb_expect(skb), count);
742 skb_put_data(skb, buffer, len);
746 hci_skb_expect(skb) -= len;
748 if (skb->len == HCI_EVENT_HDR_SIZE) {
749 /* Complete event header */
750 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
752 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
761 if (!hci_skb_expect(skb)) {
763 data->recv_event(data->hdev, skb);
769 spin_unlock_irqrestore(&data->rxlock, flags);
774 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
780 spin_lock_irqsave(&data->rxlock, flags);
787 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
793 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
794 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
797 len = min_t(uint, hci_skb_expect(skb), count);
798 skb_put_data(skb, buffer, len);
802 hci_skb_expect(skb) -= len;
804 if (skb->len == HCI_ACL_HDR_SIZE) {
805 __le16 dlen = hci_acl_hdr(skb)->dlen;
807 /* Complete ACL header */
808 hci_skb_expect(skb) = __le16_to_cpu(dlen);
810 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
819 if (!hci_skb_expect(skb)) {
821 data->recv_acl(data->hdev, skb);
827 spin_unlock_irqrestore(&data->rxlock, flags);
832 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
838 spin_lock_irqsave(&data->rxlock, flags);
845 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
851 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
852 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
855 len = min_t(uint, hci_skb_expect(skb), count);
856 skb_put_data(skb, buffer, len);
860 hci_skb_expect(skb) -= len;
862 if (skb->len == HCI_SCO_HDR_SIZE) {
863 /* Complete SCO header */
864 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
866 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
875 if (!hci_skb_expect(skb)) {
877 hci_recv_frame(data->hdev, skb);
883 spin_unlock_irqrestore(&data->rxlock, flags);
888 static void btusb_intr_complete(struct urb *urb)
890 struct hci_dev *hdev = urb->context;
891 struct btusb_data *data = hci_get_drvdata(hdev);
894 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
897 if (!test_bit(HCI_RUNNING, &hdev->flags))
900 if (urb->status == 0) {
901 hdev->stat.byte_rx += urb->actual_length;
903 if (btusb_recv_intr(data, urb->transfer_buffer,
904 urb->actual_length) < 0) {
905 bt_dev_err(hdev, "corrupted event packet");
908 } else if (urb->status == -ENOENT) {
909 /* Avoid suspend failed when usb_kill_urb */
913 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
916 usb_mark_last_busy(data->udev);
917 usb_anchor_urb(urb, &data->intr_anchor);
919 err = usb_submit_urb(urb, GFP_ATOMIC);
921 /* -EPERM: urb is being killed;
922 * -ENODEV: device got disconnected
924 if (err != -EPERM && err != -ENODEV)
925 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
927 usb_unanchor_urb(urb);
931 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
933 struct btusb_data *data = hci_get_drvdata(hdev);
939 BT_DBG("%s", hdev->name);
944 urb = usb_alloc_urb(0, mem_flags);
948 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
950 buf = kmalloc(size, mem_flags);
956 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
958 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
959 btusb_intr_complete, hdev, data->intr_ep->bInterval);
961 urb->transfer_flags |= URB_FREE_BUFFER;
963 usb_anchor_urb(urb, &data->intr_anchor);
965 err = usb_submit_urb(urb, mem_flags);
967 if (err != -EPERM && err != -ENODEV)
968 bt_dev_err(hdev, "urb %p submission failed (%d)",
970 usb_unanchor_urb(urb);
978 static void btusb_bulk_complete(struct urb *urb)
980 struct hci_dev *hdev = urb->context;
981 struct btusb_data *data = hci_get_drvdata(hdev);
984 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
987 if (!test_bit(HCI_RUNNING, &hdev->flags))
990 if (urb->status == 0) {
991 hdev->stat.byte_rx += urb->actual_length;
993 if (data->recv_bulk(data, urb->transfer_buffer,
994 urb->actual_length) < 0) {
995 bt_dev_err(hdev, "corrupted ACL packet");
998 } else if (urb->status == -ENOENT) {
999 /* Avoid suspend failed when usb_kill_urb */
1003 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
1006 usb_anchor_urb(urb, &data->bulk_anchor);
1007 usb_mark_last_busy(data->udev);
1009 err = usb_submit_urb(urb, GFP_ATOMIC);
1011 /* -EPERM: urb is being killed;
1012 * -ENODEV: device got disconnected
1014 if (err != -EPERM && err != -ENODEV)
1015 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1017 usb_unanchor_urb(urb);
1021 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
1023 struct btusb_data *data = hci_get_drvdata(hdev);
1027 int err, size = HCI_MAX_FRAME_SIZE;
1029 BT_DBG("%s", hdev->name);
1031 if (!data->bulk_rx_ep)
1034 urb = usb_alloc_urb(0, mem_flags);
1038 buf = kmalloc(size, mem_flags);
1044 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1046 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1047 btusb_bulk_complete, hdev);
1049 urb->transfer_flags |= URB_FREE_BUFFER;
1051 usb_mark_last_busy(data->udev);
1052 usb_anchor_urb(urb, &data->bulk_anchor);
1054 err = usb_submit_urb(urb, mem_flags);
1056 if (err != -EPERM && err != -ENODEV)
1057 bt_dev_err(hdev, "urb %p submission failed (%d)",
1059 usb_unanchor_urb(urb);
1067 static void btusb_isoc_complete(struct urb *urb)
1069 struct hci_dev *hdev = urb->context;
1070 struct btusb_data *data = hci_get_drvdata(hdev);
1073 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1074 urb->actual_length);
1076 if (!test_bit(HCI_RUNNING, &hdev->flags))
1079 if (urb->status == 0) {
1080 for (i = 0; i < urb->number_of_packets; i++) {
1081 unsigned int offset = urb->iso_frame_desc[i].offset;
1082 unsigned int length = urb->iso_frame_desc[i].actual_length;
1084 if (urb->iso_frame_desc[i].status)
1087 hdev->stat.byte_rx += length;
1089 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1091 bt_dev_err(hdev, "corrupted SCO packet");
1092 hdev->stat.err_rx++;
1095 } else if (urb->status == -ENOENT) {
1096 /* Avoid suspend failed when usb_kill_urb */
1100 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1103 usb_anchor_urb(urb, &data->isoc_anchor);
1105 err = usb_submit_urb(urb, GFP_ATOMIC);
1107 /* -EPERM: urb is being killed;
1108 * -ENODEV: device got disconnected
1110 if (err != -EPERM && err != -ENODEV)
1111 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1113 usb_unanchor_urb(urb);
1117 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1118 int mtu, struct btusb_data *data)
1121 unsigned int interval;
1123 BT_DBG("len %d mtu %d", len, mtu);
1125 /* For mSBC ALT 6 setting the host will send the packet at continuous
1126 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1127 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1128 * To maintain the rate we send 63bytes of usb packets alternatively for
1129 * 7ms and 8ms to maintain the rate as 7.5ms.
1131 if (data->usb_alt6_packet_flow) {
1133 data->usb_alt6_packet_flow = false;
1136 data->usb_alt6_packet_flow = true;
1139 for (i = 0; i < interval; i++) {
1140 urb->iso_frame_desc[i].offset = offset;
1141 urb->iso_frame_desc[i].length = offset;
1144 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1145 urb->iso_frame_desc[i].offset = offset;
1146 urb->iso_frame_desc[i].length = len;
1150 urb->number_of_packets = i;
1153 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1157 BT_DBG("len %d mtu %d", len, mtu);
1159 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1160 i++, offset += mtu, len -= mtu) {
1161 urb->iso_frame_desc[i].offset = offset;
1162 urb->iso_frame_desc[i].length = mtu;
1165 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1166 urb->iso_frame_desc[i].offset = offset;
1167 urb->iso_frame_desc[i].length = len;
1171 urb->number_of_packets = i;
1174 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1176 struct btusb_data *data = hci_get_drvdata(hdev);
1182 BT_DBG("%s", hdev->name);
1184 if (!data->isoc_rx_ep)
1187 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1191 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1192 BTUSB_MAX_ISOC_FRAMES;
1194 buf = kmalloc(size, mem_flags);
1200 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1202 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1203 hdev, data->isoc_rx_ep->bInterval);
1205 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1207 __fill_isoc_descriptor(urb, size,
1208 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1210 usb_anchor_urb(urb, &data->isoc_anchor);
1212 err = usb_submit_urb(urb, mem_flags);
1214 if (err != -EPERM && err != -ENODEV)
1215 bt_dev_err(hdev, "urb %p submission failed (%d)",
1217 usb_unanchor_urb(urb);
1225 static void btusb_diag_complete(struct urb *urb)
1227 struct hci_dev *hdev = urb->context;
1228 struct btusb_data *data = hci_get_drvdata(hdev);
1231 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1232 urb->actual_length);
1234 if (urb->status == 0) {
1235 struct sk_buff *skb;
1237 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1239 skb_put_data(skb, urb->transfer_buffer,
1240 urb->actual_length);
1241 hci_recv_diag(hdev, skb);
1243 } else if (urb->status == -ENOENT) {
1244 /* Avoid suspend failed when usb_kill_urb */
1248 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1251 usb_anchor_urb(urb, &data->diag_anchor);
1252 usb_mark_last_busy(data->udev);
1254 err = usb_submit_urb(urb, GFP_ATOMIC);
1256 /* -EPERM: urb is being killed;
1257 * -ENODEV: device got disconnected
1259 if (err != -EPERM && err != -ENODEV)
1260 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1262 usb_unanchor_urb(urb);
1266 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1268 struct btusb_data *data = hci_get_drvdata(hdev);
1272 int err, size = HCI_MAX_FRAME_SIZE;
1274 BT_DBG("%s", hdev->name);
1276 if (!data->diag_rx_ep)
1279 urb = usb_alloc_urb(0, mem_flags);
1283 buf = kmalloc(size, mem_flags);
1289 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1291 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1292 btusb_diag_complete, hdev);
1294 urb->transfer_flags |= URB_FREE_BUFFER;
1296 usb_mark_last_busy(data->udev);
1297 usb_anchor_urb(urb, &data->diag_anchor);
1299 err = usb_submit_urb(urb, mem_flags);
1301 if (err != -EPERM && err != -ENODEV)
1302 bt_dev_err(hdev, "urb %p submission failed (%d)",
1304 usb_unanchor_urb(urb);
1312 static void btusb_tx_complete(struct urb *urb)
1314 struct sk_buff *skb = urb->context;
1315 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1316 struct btusb_data *data = hci_get_drvdata(hdev);
1317 unsigned long flags;
1319 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1320 urb->actual_length);
1322 if (!test_bit(HCI_RUNNING, &hdev->flags))
1326 hdev->stat.byte_tx += urb->transfer_buffer_length;
1328 hdev->stat.err_tx++;
1331 spin_lock_irqsave(&data->txlock, flags);
1332 data->tx_in_flight--;
1333 spin_unlock_irqrestore(&data->txlock, flags);
1335 kfree(urb->setup_packet);
1340 static void btusb_isoc_tx_complete(struct urb *urb)
1342 struct sk_buff *skb = urb->context;
1343 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1345 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1346 urb->actual_length);
1348 if (!test_bit(HCI_RUNNING, &hdev->flags))
1352 hdev->stat.byte_tx += urb->transfer_buffer_length;
1354 hdev->stat.err_tx++;
1357 kfree(urb->setup_packet);
1362 static int btusb_open(struct hci_dev *hdev)
1364 struct btusb_data *data = hci_get_drvdata(hdev);
1367 BT_DBG("%s", hdev->name);
1369 err = usb_autopm_get_interface(data->intf);
1373 /* Patching USB firmware files prior to starting any URBs of HCI path
1374 * It is more safe to use USB bulk channel for downloading USB patch
1376 if (data->setup_on_usb) {
1377 err = data->setup_on_usb(hdev);
1382 data->intf->needs_remote_wakeup = 1;
1384 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1387 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1391 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1393 usb_kill_anchored_urbs(&data->intr_anchor);
1397 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1398 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1401 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1402 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1406 usb_autopm_put_interface(data->intf);
1410 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1412 usb_autopm_put_interface(data->intf);
1416 static void btusb_stop_traffic(struct btusb_data *data)
1418 usb_kill_anchored_urbs(&data->intr_anchor);
1419 usb_kill_anchored_urbs(&data->bulk_anchor);
1420 usb_kill_anchored_urbs(&data->isoc_anchor);
1421 usb_kill_anchored_urbs(&data->diag_anchor);
1422 usb_kill_anchored_urbs(&data->ctrl_anchor);
1425 static int btusb_close(struct hci_dev *hdev)
1427 struct btusb_data *data = hci_get_drvdata(hdev);
1430 BT_DBG("%s", hdev->name);
1432 cancel_work_sync(&data->work);
1433 cancel_work_sync(&data->waker);
1435 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1436 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1437 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1438 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1440 btusb_stop_traffic(data);
1441 btusb_free_frags(data);
1443 err = usb_autopm_get_interface(data->intf);
1447 data->intf->needs_remote_wakeup = 0;
1449 /* Enable remote wake up for auto-suspend */
1450 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags))
1451 data->intf->needs_remote_wakeup = 1;
1453 usb_autopm_put_interface(data->intf);
1456 usb_scuttle_anchored_urbs(&data->deferred);
1460 static int btusb_flush(struct hci_dev *hdev)
1462 struct btusb_data *data = hci_get_drvdata(hdev);
1464 BT_DBG("%s", hdev->name);
1466 usb_kill_anchored_urbs(&data->tx_anchor);
1467 btusb_free_frags(data);
1472 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1474 struct btusb_data *data = hci_get_drvdata(hdev);
1475 struct usb_ctrlrequest *dr;
1479 urb = usb_alloc_urb(0, GFP_KERNEL);
1481 return ERR_PTR(-ENOMEM);
1483 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1486 return ERR_PTR(-ENOMEM);
1489 dr->bRequestType = data->cmdreq_type;
1490 dr->bRequest = data->cmdreq;
1493 dr->wLength = __cpu_to_le16(skb->len);
1495 pipe = usb_sndctrlpipe(data->udev, 0x00);
1497 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1498 skb->data, skb->len, btusb_tx_complete, skb);
1500 skb->dev = (void *)hdev;
1505 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1507 struct btusb_data *data = hci_get_drvdata(hdev);
1511 if (!data->bulk_tx_ep)
1512 return ERR_PTR(-ENODEV);
1514 urb = usb_alloc_urb(0, GFP_KERNEL);
1516 return ERR_PTR(-ENOMEM);
1518 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1520 usb_fill_bulk_urb(urb, data->udev, pipe,
1521 skb->data, skb->len, btusb_tx_complete, skb);
1523 skb->dev = (void *)hdev;
1528 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1530 struct btusb_data *data = hci_get_drvdata(hdev);
1534 if (!data->isoc_tx_ep)
1535 return ERR_PTR(-ENODEV);
1537 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1539 return ERR_PTR(-ENOMEM);
1541 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1543 usb_fill_int_urb(urb, data->udev, pipe,
1544 skb->data, skb->len, btusb_isoc_tx_complete,
1545 skb, data->isoc_tx_ep->bInterval);
1547 urb->transfer_flags = URB_ISO_ASAP;
1549 if (data->isoc_altsetting == 6)
1550 __fill_isoc_descriptor_msbc(urb, skb->len,
1551 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1554 __fill_isoc_descriptor(urb, skb->len,
1555 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1556 skb->dev = (void *)hdev;
1561 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1563 struct btusb_data *data = hci_get_drvdata(hdev);
1566 usb_anchor_urb(urb, &data->tx_anchor);
1568 err = usb_submit_urb(urb, GFP_KERNEL);
1570 if (err != -EPERM && err != -ENODEV)
1571 bt_dev_err(hdev, "urb %p submission failed (%d)",
1573 kfree(urb->setup_packet);
1574 usb_unanchor_urb(urb);
1576 usb_mark_last_busy(data->udev);
1583 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1585 struct btusb_data *data = hci_get_drvdata(hdev);
1586 unsigned long flags;
1589 spin_lock_irqsave(&data->txlock, flags);
1590 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1592 data->tx_in_flight++;
1593 spin_unlock_irqrestore(&data->txlock, flags);
1596 return submit_tx_urb(hdev, urb);
1598 usb_anchor_urb(urb, &data->deferred);
1599 schedule_work(&data->waker);
1605 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1609 BT_DBG("%s", hdev->name);
1611 switch (hci_skb_pkt_type(skb)) {
1612 case HCI_COMMAND_PKT:
1613 urb = alloc_ctrl_urb(hdev, skb);
1615 return PTR_ERR(urb);
1617 hdev->stat.cmd_tx++;
1618 return submit_or_queue_tx_urb(hdev, urb);
1620 case HCI_ACLDATA_PKT:
1621 urb = alloc_bulk_urb(hdev, skb);
1623 return PTR_ERR(urb);
1625 hdev->stat.acl_tx++;
1626 return submit_or_queue_tx_urb(hdev, urb);
1628 case HCI_SCODATA_PKT:
1629 if (hci_conn_num(hdev, SCO_LINK) < 1)
1632 urb = alloc_isoc_urb(hdev, skb);
1634 return PTR_ERR(urb);
1636 hdev->stat.sco_tx++;
1637 return submit_tx_urb(hdev, urb);
1643 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1645 struct btusb_data *data = hci_get_drvdata(hdev);
1647 BT_DBG("%s evt %d", hdev->name, evt);
1649 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1650 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1651 data->air_mode = evt;
1652 schedule_work(&data->work);
1656 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1658 struct btusb_data *data = hci_get_drvdata(hdev);
1659 struct usb_interface *intf = data->isoc;
1660 struct usb_endpoint_descriptor *ep_desc;
1666 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
1668 bt_dev_err(hdev, "setting interface failed (%d)", -err);
1672 data->isoc_altsetting = altsetting;
1674 data->isoc_tx_ep = NULL;
1675 data->isoc_rx_ep = NULL;
1677 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1678 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1680 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1681 data->isoc_tx_ep = ep_desc;
1685 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1686 data->isoc_rx_ep = ep_desc;
1691 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1692 bt_dev_err(hdev, "invalid SCO descriptors");
1699 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1701 struct btusb_data *data = hci_get_drvdata(hdev);
1704 if (data->isoc_altsetting != new_alts) {
1705 unsigned long flags;
1707 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1708 usb_kill_anchored_urbs(&data->isoc_anchor);
1710 /* When isochronous alternate setting needs to be
1711 * changed, because SCO connection has been added
1712 * or removed, a packet fragment may be left in the
1713 * reassembling state. This could lead to wrongly
1714 * assembled fragments.
1716 * Clear outstanding fragment when selecting a new
1717 * alternate setting.
1719 spin_lock_irqsave(&data->rxlock, flags);
1720 kfree_skb(data->sco_skb);
1721 data->sco_skb = NULL;
1722 spin_unlock_irqrestore(&data->rxlock, flags);
1724 err = __set_isoc_interface(hdev, new_alts);
1729 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1730 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1731 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1733 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1739 static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1742 struct usb_interface *intf = data->isoc;
1745 BT_DBG("Looking for Alt no :%d", alt);
1750 for (i = 0; i < intf->num_altsetting; i++) {
1751 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1752 return &intf->altsetting[i];
1758 static void btusb_work(struct work_struct *work)
1760 struct btusb_data *data = container_of(work, struct btusb_data, work);
1761 struct hci_dev *hdev = data->hdev;
1765 if (data->sco_num > 0) {
1766 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1767 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1769 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1770 usb_kill_anchored_urbs(&data->isoc_anchor);
1774 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1777 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1778 if (hdev->voice_setting & 0x0020) {
1779 static const int alts[3] = { 2, 4, 5 };
1781 new_alts = alts[data->sco_num - 1];
1783 new_alts = data->sco_num;
1785 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
1786 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1787 * many adapters do not support it. Alt 1 appears to
1788 * work for all adapters that do not have alt 6, and
1789 * which work with WBS at all. Some devices prefer
1790 * alt 3 (HCI payload >= 60 Bytes let air packet
1791 * data satisfy 60 bytes), requiring
1792 * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
1793 * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
1795 if (btusb_find_altsetting(data, 6))
1797 else if (btusb_find_altsetting(data, 3) &&
1798 hdev->sco_mtu >= 72 &&
1799 test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
1805 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1806 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
1808 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1809 usb_kill_anchored_urbs(&data->isoc_anchor);
1811 __set_isoc_interface(hdev, 0);
1812 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1813 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1817 static void btusb_waker(struct work_struct *work)
1819 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1822 err = usb_autopm_get_interface(data->intf);
1826 usb_autopm_put_interface(data->intf);
1829 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1831 struct sk_buff *skb;
1834 BT_DBG("%s", hdev->name);
1836 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1838 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
1845 static int btusb_setup_csr(struct hci_dev *hdev)
1847 struct btusb_data *data = hci_get_drvdata(hdev);
1848 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
1849 struct hci_rp_read_local_version *rp;
1850 struct sk_buff *skb;
1851 bool is_fake = false;
1854 BT_DBG("%s", hdev->name);
1856 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1859 int err = PTR_ERR(skb);
1860 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
1864 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1865 bt_dev_err(hdev, "CSR: Local version length mismatch");
1870 rp = (struct hci_rp_read_local_version *)skb->data;
1872 /* Detect a wide host of Chinese controllers that aren't CSR.
1874 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1876 * The main thing they have in common is that these are really popular low-cost
1877 * options that support newer Bluetooth versions but rely on heavy VID/PID
1878 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1880 * We detect actual CSR devices by checking that the HCI manufacturer code
1881 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1882 * HCI rev values always match. As they both store the firmware number.
1884 if (le16_to_cpu(rp->manufacturer) != 10 ||
1885 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
1888 /* Known legit CSR firmware build numbers and their supported BT versions:
1889 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1890 * - 1.2 (0x2) -> 0x04d9, 0x0529
1891 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
1892 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
1893 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
1895 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
1896 * support BT 1.1 only; so it's a dead giveaway when some
1897 * third-party BT 4.0 dongle reuses it.
1899 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
1900 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
1903 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
1904 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
1907 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
1908 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
1911 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
1912 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
1915 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
1916 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
1919 /* Other clones which beat all the above checks */
1920 else if (bcdDevice == 0x0134 &&
1921 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
1922 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
1926 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
1928 /* Generally these clones have big discrepancies between
1929 * advertised features and what's actually supported.
1930 * Probably will need to be expanded in the future;
1931 * without these the controller will lock up.
1933 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1934 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
1936 /* Clear the reset quirk since this is not an actual
1937 * early Bluetooth 1.1 device from CSR.
1939 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1940 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1943 * Special workaround for these BT 4.0 chip clones, and potentially more:
1945 * - 0x0134: a Barrot 8041a02 (HCI rev: 0x1012 sub: 0x0810)
1946 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
1948 * These controllers are really messed-up.
1950 * 1. Their bulk RX endpoint will never report any data unless
1951 * the device was suspended at least once (yes, really).
1952 * 2. They will not wakeup when autosuspended and receiving data
1953 * on their bulk RX endpoint from e.g. a keyboard or mouse
1954 * (IOW remote-wakeup support is broken for the bulk endpoint).
1956 * To fix 1. enable runtime-suspend, force-suspend the
1957 * HCI and then wake-it up by disabling runtime-suspend.
1959 * To fix 2. clear the HCI's can_wake flag, this way the HCI
1960 * will still be autosuspended when it is not open.
1964 * Because these are widespread problems we prefer generic solutions; so
1965 * apply this initialization quirk to every controller that gets here,
1966 * it should be harmless. The alternative is to not work at all.
1968 pm_runtime_allow(&data->udev->dev);
1970 ret = pm_runtime_suspend(&data->udev->dev);
1974 bt_dev_err(hdev, "CSR: Failed to suspend the device for our Barrot 8041a02 receive-issue workaround");
1976 pm_runtime_forbid(&data->udev->dev);
1978 device_set_wakeup_capable(&data->udev->dev, false);
1980 /* Re-enable autosuspend if this was requested */
1981 if (enable_autosuspend)
1982 usb_enable_autosuspend(data->udev);
1990 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1992 struct sk_buff *skb;
1993 struct hci_event_hdr *hdr;
1994 struct hci_ev_cmd_complete *evt;
1996 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2000 hdr = skb_put(skb, sizeof(*hdr));
2001 hdr->evt = HCI_EV_CMD_COMPLETE;
2002 hdr->plen = sizeof(*evt) + 1;
2004 evt = skb_put(skb, sizeof(*evt));
2006 evt->opcode = cpu_to_le16(opcode);
2008 skb_put_u8(skb, 0x00);
2010 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2012 return hci_recv_frame(hdev, skb);
2015 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2018 struct hci_dev *hdev = data->hdev;
2020 /* When the device is in bootloader mode, then it can send
2021 * events via the bulk endpoint. These events are treated the
2022 * same way as the ones received from the interrupt endpoint.
2024 if (btintel_test_flag(hdev, INTEL_BOOTLOADER))
2025 return btusb_recv_intr(data, buffer, count);
2027 return btusb_recv_bulk(data, buffer, count);
2030 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2032 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2033 struct hci_event_hdr *hdr = (void *)skb->data;
2035 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2037 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2038 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
2040 switch (skb->data[2]) {
2042 /* When switching to the operational firmware
2043 * the device sends a vendor specific event
2044 * indicating that the bootup completed.
2046 btintel_bootup(hdev, ptr, len);
2049 /* When the firmware loading completes the
2050 * device sends out a vendor specific event
2051 * indicating the result of the firmware
2054 btintel_secure_send_result(hdev, ptr, len);
2060 return hci_recv_frame(hdev, skb);
2063 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2067 BT_DBG("%s", hdev->name);
2069 switch (hci_skb_pkt_type(skb)) {
2070 case HCI_COMMAND_PKT:
2071 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2072 struct hci_command_hdr *cmd = (void *)skb->data;
2073 __u16 opcode = le16_to_cpu(cmd->opcode);
2075 /* When in bootloader mode and the command 0xfc09
2076 * is received, it needs to be send down the
2077 * bulk endpoint. So allocate a bulk URB instead.
2079 if (opcode == 0xfc09)
2080 urb = alloc_bulk_urb(hdev, skb);
2082 urb = alloc_ctrl_urb(hdev, skb);
2084 /* When the 0xfc01 command is issued to boot into
2085 * the operational firmware, it will actually not
2086 * send a command complete event. To keep the flow
2087 * control working inject that event here.
2089 if (opcode == 0xfc01)
2090 inject_cmd_complete(hdev, opcode);
2092 urb = alloc_ctrl_urb(hdev, skb);
2095 return PTR_ERR(urb);
2097 hdev->stat.cmd_tx++;
2098 return submit_or_queue_tx_urb(hdev, urb);
2100 case HCI_ACLDATA_PKT:
2101 urb = alloc_bulk_urb(hdev, skb);
2103 return PTR_ERR(urb);
2105 hdev->stat.acl_tx++;
2106 return submit_or_queue_tx_urb(hdev, urb);
2108 case HCI_SCODATA_PKT:
2109 if (hci_conn_num(hdev, SCO_LINK) < 1)
2112 urb = alloc_isoc_urb(hdev, skb);
2114 return PTR_ERR(urb);
2116 hdev->stat.sco_tx++;
2117 return submit_tx_urb(hdev, urb);
2123 /* UHW CR mapping */
2124 #define MTK_BT_MISC 0x70002510
2125 #define MTK_BT_SUBSYS_RST 0x70002610
2126 #define MTK_UDMA_INT_STA_BT 0x74000024
2127 #define MTK_UDMA_INT_STA_BT1 0x74000308
2128 #define MTK_BT_WDT_STATUS 0x740003A0
2129 #define MTK_EP_RST_OPT 0x74011890
2130 #define MTK_EP_RST_IN_OUT_OPT 0x00010001
2131 #define MTK_BT_RST_DONE 0x00000100
2132 #define MTK_BT_RESET_WAIT_MS 100
2133 #define MTK_BT_RESET_NUM_TRIES 10
2134 #define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
2135 #define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
2137 #define HCI_WMT_MAX_EVENT_SIZE 64
2138 /* It is for mt79xx download rom patch*/
2139 #define MTK_FW_ROM_PATCH_HEADER_SIZE 32
2140 #define MTK_FW_ROM_PATCH_GD_SIZE 64
2141 #define MTK_FW_ROM_PATCH_SEC_MAP_SIZE 64
2142 #define MTK_SEC_MAP_COMMON_SIZE 12
2143 #define MTK_SEC_MAP_NEED_SEND_SIZE 52
2146 BTMTK_WMT_PATCH_DWNLD = 0x1,
2147 BTMTK_WMT_FUNC_CTRL = 0x6,
2148 BTMTK_WMT_RST = 0x7,
2149 BTMTK_WMT_SEMAPHORE = 0x17,
2154 BTMTK_WMT_PATCH_UNDONE,
2155 BTMTK_WMT_PATCH_PROGRESS,
2156 BTMTK_WMT_PATCH_DONE,
2157 BTMTK_WMT_ON_UNDONE,
2159 BTMTK_WMT_ON_PROGRESS,
2162 struct btmtk_wmt_hdr {
2169 struct btmtk_hci_wmt_cmd {
2170 struct btmtk_wmt_hdr hdr;
2174 struct btmtk_hci_wmt_evt {
2175 struct hci_event_hdr hhdr;
2176 struct btmtk_wmt_hdr whdr;
2179 struct btmtk_hci_wmt_evt_funcc {
2180 struct btmtk_hci_wmt_evt hwhdr;
2184 struct btmtk_tci_sleep {
2187 __le16 host_duration;
2189 u8 time_compensation;
2192 struct btmtk_hci_wmt_params {
2200 struct btmtk_patch_header {
2208 struct btmtk_global_desc {
2215 struct btmtk_section_map {
2220 __le32 u4SecSpec[13];
2227 __le32 dlmodecrctype;
2234 static int btusb_set_bdaddr_mtk(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2236 struct sk_buff *skb;
2239 skb = __hci_cmd_sync(hdev, 0xfc1a, sizeof(bdaddr), bdaddr, HCI_INIT_TIMEOUT);
2242 bt_dev_err(hdev, "changing Mediatek device address failed (%ld)",
2251 static void btusb_mtk_wmt_recv(struct urb *urb)
2253 struct hci_dev *hdev = urb->context;
2254 struct btusb_data *data = hci_get_drvdata(hdev);
2255 struct hci_event_hdr *hdr;
2256 struct sk_buff *skb;
2259 if (urb->status == 0 && urb->actual_length > 0) {
2260 hdev->stat.byte_rx += urb->actual_length;
2262 /* WMT event shouldn't be fragmented and the size should be
2263 * less than HCI_WMT_MAX_EVENT_SIZE.
2265 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2267 hdev->stat.err_rx++;
2271 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2272 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2274 hdr = (void *)skb->data;
2275 /* Fix up the vendor event id with 0xff for vendor specific
2276 * instead of 0xe4 so that event send via monitoring socket can
2277 * be parsed properly.
2281 /* When someone waits for the WMT event, the skb is being cloned
2282 * and being processed the events from there then.
2284 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
2285 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
2286 if (!data->evt_skb) {
2292 err = hci_recv_frame(hdev, skb);
2294 kfree_skb(data->evt_skb);
2295 data->evt_skb = NULL;
2299 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2301 /* Barrier to sync with other CPUs */
2302 smp_mb__after_atomic();
2303 wake_up_bit(&data->flags,
2304 BTUSB_TX_WAIT_VND_EVT);
2307 } else if (urb->status == -ENOENT) {
2308 /* Avoid suspend failed when usb_kill_urb */
2312 usb_mark_last_busy(data->udev);
2314 /* The URB complete handler is still called with urb->actual_length = 0
2315 * when the event is not available, so we should keep re-submitting
2316 * URB until WMT event returns, Also, It's necessary to wait some time
2317 * between the two consecutive control URBs to relax the target device
2318 * to generate the event. Otherwise, the WMT event cannot return from
2319 * the device successfully.
2323 usb_anchor_urb(urb, &data->ctrl_anchor);
2324 err = usb_submit_urb(urb, GFP_ATOMIC);
2326 /* -EPERM: urb is being killed;
2327 * -ENODEV: device got disconnected
2329 if (err != -EPERM && err != -ENODEV)
2330 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2332 usb_unanchor_urb(urb);
2336 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2338 struct btusb_data *data = hci_get_drvdata(hdev);
2339 struct usb_ctrlrequest *dr;
2345 urb = usb_alloc_urb(0, GFP_KERNEL);
2349 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2355 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2357 dr->wIndex = cpu_to_le16(0);
2358 dr->wValue = cpu_to_le16(48);
2359 dr->wLength = cpu_to_le16(size);
2361 buf = kmalloc(size, GFP_KERNEL);
2368 pipe = usb_rcvctrlpipe(data->udev, 0);
2370 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2371 buf, size, btusb_mtk_wmt_recv, hdev);
2373 urb->transfer_flags |= URB_FREE_BUFFER;
2375 usb_anchor_urb(urb, &data->ctrl_anchor);
2376 err = usb_submit_urb(urb, GFP_KERNEL);
2378 if (err != -EPERM && err != -ENODEV)
2379 bt_dev_err(hdev, "urb %p submission failed (%d)",
2381 usb_unanchor_urb(urb);
2389 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
2390 struct btmtk_hci_wmt_params *wmt_params)
2392 struct btusb_data *data = hci_get_drvdata(hdev);
2393 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
2394 u32 hlen, status = BTMTK_WMT_INVALID;
2395 struct btmtk_hci_wmt_evt *wmt_evt;
2396 struct btmtk_hci_wmt_cmd *wc;
2397 struct btmtk_wmt_hdr *hdr;
2400 /* Send the WMT command and wait until the WMT event returns */
2401 hlen = sizeof(*hdr) + wmt_params->dlen;
2405 wc = kzalloc(hlen, GFP_KERNEL);
2411 hdr->op = wmt_params->op;
2412 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
2413 hdr->flag = wmt_params->flag;
2414 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
2416 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2418 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
2421 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2425 /* Submit control IN URB on demand to process the WMT event */
2426 err = btusb_mtk_submit_wmt_recv_urb(hdev);
2430 /* The vendor specific WMT commands are all answered by a vendor
2431 * specific event and will have the Command Status or Command
2432 * Complete as with usual HCI command flow control.
2434 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
2435 * state to be cleared. The driver specific event receive routine
2436 * will clear that state and with that indicate completion of the
2439 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
2440 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
2441 if (err == -EINTR) {
2442 bt_dev_err(hdev, "Execution of wmt command interrupted");
2443 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2448 bt_dev_err(hdev, "Execution of wmt command timed out");
2449 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2454 /* Parse and handle the return WMT event */
2455 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
2456 if (wmt_evt->whdr.op != hdr->op) {
2457 bt_dev_err(hdev, "Wrong op received %d expected %d",
2458 wmt_evt->whdr.op, hdr->op);
2463 switch (wmt_evt->whdr.op) {
2464 case BTMTK_WMT_SEMAPHORE:
2465 if (wmt_evt->whdr.flag == 2)
2466 status = BTMTK_WMT_PATCH_UNDONE;
2468 status = BTMTK_WMT_PATCH_DONE;
2470 case BTMTK_WMT_FUNC_CTRL:
2471 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
2472 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
2473 status = BTMTK_WMT_ON_DONE;
2474 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
2475 status = BTMTK_WMT_ON_PROGRESS;
2477 status = BTMTK_WMT_ON_UNDONE;
2479 case BTMTK_WMT_PATCH_DWNLD:
2480 if (wmt_evt->whdr.flag == 2)
2481 status = BTMTK_WMT_PATCH_DONE;
2482 else if (wmt_evt->whdr.flag == 1)
2483 status = BTMTK_WMT_PATCH_PROGRESS;
2485 status = BTMTK_WMT_PATCH_UNDONE;
2489 if (wmt_params->status)
2490 *wmt_params->status = status;
2493 kfree_skb(data->evt_skb);
2494 data->evt_skb = NULL;
2500 static int btusb_mtk_setup_firmware_79xx(struct hci_dev *hdev, const char *fwname)
2502 struct btmtk_hci_wmt_params wmt_params;
2503 struct btmtk_global_desc *globaldesc = NULL;
2504 struct btmtk_section_map *sectionmap;
2505 const struct firmware *fw;
2507 const u8 *fw_bin_ptr;
2508 int err, dlen, i, status;
2509 u8 flag, first_block, retry;
2510 u32 section_num, dl_size, section_offset;
2513 err = request_firmware(&fw, fwname, &hdev->dev);
2515 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
2520 fw_bin_ptr = fw_ptr;
2521 globaldesc = (struct btmtk_global_desc *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE);
2522 section_num = le32_to_cpu(globaldesc->section_num);
2524 for (i = 0; i < section_num; i++) {
2526 fw_ptr = fw_bin_ptr;
2527 sectionmap = (struct btmtk_section_map *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
2528 MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i);
2530 section_offset = le32_to_cpu(sectionmap->secoffset);
2531 dl_size = le32_to_cpu(sectionmap->bin_info_spec.dlsize);
2536 cmd[0] = 0; /* 0 means legacy dl mode. */
2538 fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
2539 MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i +
2540 MTK_SEC_MAP_COMMON_SIZE,
2541 MTK_SEC_MAP_NEED_SEND_SIZE + 1);
2543 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
2544 wmt_params.status = &status;
2545 wmt_params.flag = 0;
2546 wmt_params.dlen = MTK_SEC_MAP_NEED_SEND_SIZE + 1;
2547 wmt_params.data = &cmd;
2549 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2551 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
2553 goto err_release_fw;
2556 if (status == BTMTK_WMT_PATCH_UNDONE) {
2558 } else if (status == BTMTK_WMT_PATCH_PROGRESS) {
2561 } else if (status == BTMTK_WMT_PATCH_DONE) {
2564 bt_dev_err(hdev, "Failed wmt patch dwnld status (%d)",
2566 goto err_release_fw;
2570 fw_ptr += section_offset;
2571 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
2572 wmt_params.status = NULL;
2574 while (dl_size > 0) {
2575 dlen = min_t(int, 250, dl_size);
2576 if (first_block == 1) {
2579 } else if (dl_size - dlen <= 0) {
2585 wmt_params.flag = flag;
2586 wmt_params.dlen = dlen;
2587 wmt_params.data = fw_ptr;
2589 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2591 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
2593 goto err_release_fw;
2603 /* Wait a few moments for firmware activation done */
2604 usleep_range(100000, 120000);
2607 release_firmware(fw);
2612 static int btusb_mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
2614 struct btmtk_hci_wmt_params wmt_params;
2615 const struct firmware *fw;
2621 err = request_firmware(&fw, fwname, &hdev->dev);
2623 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
2627 /* Power on data RAM the firmware relies on. */
2629 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2630 wmt_params.flag = 3;
2631 wmt_params.dlen = sizeof(param);
2632 wmt_params.data = ¶m;
2633 wmt_params.status = NULL;
2635 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2637 bt_dev_err(hdev, "Failed to power on data RAM (%d)", err);
2638 goto err_release_fw;
2644 /* The size of patch header is 30 bytes, should be skip */
2647 goto err_release_fw;
2654 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
2655 wmt_params.status = NULL;
2657 while (fw_size > 0) {
2658 dlen = min_t(int, 250, fw_size);
2660 /* Tell device the position in sequence */
2661 if (fw_size - dlen <= 0)
2663 else if (fw_size < fw->size - 30)
2666 wmt_params.flag = flag;
2667 wmt_params.dlen = dlen;
2668 wmt_params.data = fw_ptr;
2670 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2672 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
2674 goto err_release_fw;
2681 wmt_params.op = BTMTK_WMT_RST;
2682 wmt_params.flag = 4;
2683 wmt_params.dlen = 0;
2684 wmt_params.data = NULL;
2685 wmt_params.status = NULL;
2687 /* Activate funciton the firmware providing to */
2688 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2690 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
2691 goto err_release_fw;
2694 /* Wait a few moments for firmware activation done */
2695 usleep_range(10000, 12000);
2698 release_firmware(fw);
2703 static int btusb_mtk_func_query(struct hci_dev *hdev)
2705 struct btmtk_hci_wmt_params wmt_params;
2709 /* Query whether the function is enabled */
2710 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2711 wmt_params.flag = 4;
2712 wmt_params.dlen = sizeof(param);
2713 wmt_params.data = ¶m;
2714 wmt_params.status = &status;
2716 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2718 bt_dev_err(hdev, "Failed to query function status (%d)", err);
2725 static int btusb_mtk_uhw_reg_write(struct btusb_data *data, u32 reg, u32 val)
2727 struct hci_dev *hdev = data->hdev;
2731 buf = kzalloc(4, GFP_KERNEL);
2735 put_unaligned_le32(val, buf);
2737 pipe = usb_sndctrlpipe(data->udev, 0);
2738 err = usb_control_msg(data->udev, pipe, 0x02,
2740 reg >> 16, reg & 0xffff,
2741 buf, 4, USB_CTRL_SET_TIMEOUT);
2743 bt_dev_err(hdev, "Failed to write uhw reg(%d)", err);
2753 static int btusb_mtk_uhw_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2755 struct hci_dev *hdev = data->hdev;
2759 buf = kzalloc(4, GFP_KERNEL);
2763 pipe = usb_rcvctrlpipe(data->udev, 0);
2764 err = usb_control_msg(data->udev, pipe, 0x01,
2766 reg >> 16, reg & 0xffff,
2767 buf, 4, USB_CTRL_SET_TIMEOUT);
2769 bt_dev_err(hdev, "Failed to read uhw reg(%d)", err);
2773 *val = get_unaligned_le32(buf);
2774 bt_dev_dbg(hdev, "reg=%x, value=0x%08x", reg, *val);
2782 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2784 int pipe, err, size = sizeof(u32);
2787 buf = kzalloc(size, GFP_KERNEL);
2791 pipe = usb_rcvctrlpipe(data->udev, 0);
2792 err = usb_control_msg(data->udev, pipe, 0x63,
2793 USB_TYPE_VENDOR | USB_DIR_IN,
2794 reg >> 16, reg & 0xffff,
2795 buf, size, USB_CTRL_SET_TIMEOUT);
2799 *val = get_unaligned_le32(buf);
2807 static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
2809 return btusb_mtk_reg_read(data, reg, id);
2812 static int btusb_mtk_setup(struct hci_dev *hdev)
2814 struct btusb_data *data = hci_get_drvdata(hdev);
2815 struct btmtk_hci_wmt_params wmt_params;
2816 ktime_t calltime, delta, rettime;
2817 struct btmtk_tci_sleep tci_sleep;
2818 unsigned long long duration;
2819 struct sk_buff *skb;
2823 char fw_bin_name[64];
2827 calltime = ktime_get();
2829 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
2831 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2836 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
2838 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2841 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
2843 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
2850 fwname = FIRMWARE_MT7663;
2853 fwname = FIRMWARE_MT7668;
2857 snprintf(fw_bin_name, sizeof(fw_bin_name),
2858 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
2859 dev_id & 0xffff, (fw_version & 0xff) + 1);
2860 err = btusb_mtk_setup_firmware_79xx(hdev, fw_bin_name);
2862 /* It's Device EndPoint Reset Option Register */
2863 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2865 /* Enable Bluetooth protocol */
2867 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2868 wmt_params.flag = 0;
2869 wmt_params.dlen = sizeof(param);
2870 wmt_params.data = ¶m;
2871 wmt_params.status = NULL;
2873 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2875 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2879 hci_set_msft_opcode(hdev, 0xFD30);
2882 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
2887 /* Query whether the firmware is already download */
2888 wmt_params.op = BTMTK_WMT_SEMAPHORE;
2889 wmt_params.flag = 1;
2890 wmt_params.dlen = 0;
2891 wmt_params.data = NULL;
2892 wmt_params.status = &status;
2894 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2896 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
2900 if (status == BTMTK_WMT_PATCH_DONE) {
2901 bt_dev_info(hdev, "firmware already downloaded");
2902 goto ignore_setup_fw;
2905 /* Setup a firmware which the device definitely requires */
2906 err = btusb_mtk_setup_firmware(hdev, fwname);
2911 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
2912 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
2914 /* -ETIMEDOUT happens */
2918 /* The other errors happen in btusb_mtk_func_query */
2922 if (status == BTMTK_WMT_ON_DONE) {
2923 bt_dev_info(hdev, "function already on");
2924 goto ignore_func_on;
2927 /* Enable Bluetooth protocol */
2929 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2930 wmt_params.flag = 0;
2931 wmt_params.dlen = sizeof(param);
2932 wmt_params.data = ¶m;
2933 wmt_params.status = NULL;
2935 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2937 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2942 /* Apply the low power environment setup */
2943 tci_sleep.mode = 0x5;
2944 tci_sleep.duration = cpu_to_le16(0x640);
2945 tci_sleep.host_duration = cpu_to_le16(0x640);
2946 tci_sleep.host_wakeup_pin = 0;
2947 tci_sleep.time_compensation = 0;
2949 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
2953 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
2959 rettime = ktime_get();
2960 delta = ktime_sub(rettime, calltime);
2961 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2963 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
2968 static int btusb_mtk_shutdown(struct hci_dev *hdev)
2970 struct btmtk_hci_wmt_params wmt_params;
2974 /* Disable the device */
2975 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2976 wmt_params.flag = 0;
2977 wmt_params.dlen = sizeof(param);
2978 wmt_params.data = ¶m;
2979 wmt_params.status = NULL;
2981 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2983 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2990 static void btusb_mtk_cmd_timeout(struct hci_dev *hdev)
2992 struct btusb_data *data = hci_get_drvdata(hdev);
2996 /* It's MediaTek specific bluetooth reset mechanism via USB */
2997 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
2998 bt_dev_err(hdev, "last reset failed? Not resetting again");
3002 err = usb_autopm_get_interface(data->intf);
3006 btusb_stop_traffic(data);
3007 usb_kill_anchored_urbs(&data->tx_anchor);
3009 /* It's Device EndPoint Reset Option Register */
3010 bt_dev_dbg(hdev, "Initiating reset mechanism via uhw");
3011 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
3012 btusb_mtk_uhw_reg_read(data, MTK_BT_WDT_STATUS, &val);
3014 /* Reset the bluetooth chip via USB interface. */
3015 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 1);
3016 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT, 0x000000FF);
3017 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT, &val);
3018 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT1, 0x000000FF);
3019 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT1, &val);
3020 /* MT7921 need to delay 20ms between toggle reset bit */
3022 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 0);
3023 btusb_mtk_uhw_reg_read(data, MTK_BT_SUBSYS_RST, &val);
3025 /* Poll the register until reset is completed */
3027 btusb_mtk_uhw_reg_read(data, MTK_BT_MISC, &val);
3028 if (val & MTK_BT_RST_DONE) {
3029 bt_dev_dbg(hdev, "Bluetooth Reset Successfully");
3033 bt_dev_dbg(hdev, "Polling Bluetooth Reset CR");
3035 msleep(MTK_BT_RESET_WAIT_MS);
3036 } while (retry < MTK_BT_RESET_NUM_TRIES);
3038 btusb_mtk_id_get(data, 0x70010200, &val);
3040 bt_dev_err(hdev, "Can't get device id, subsys reset fail.");
3042 usb_queue_reset_device(data->intf);
3044 clear_bit(BTUSB_HW_RESET_ACTIVE, &data->flags);
3047 static int btusb_recv_acl_mtk(struct hci_dev *hdev, struct sk_buff *skb)
3049 struct btusb_data *data = hci_get_drvdata(hdev);
3050 u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
3053 case 0xfc6f: /* Firmware dump from device */
3054 /* When the firmware hangs, the device can no longer
3055 * suspend and thus disable auto-suspend.
3057 usb_disable_autosuspend(data->udev);
3059 case 0x05ff: /* Firmware debug logging 1 */
3060 case 0x05fe: /* Firmware debug logging 2 */
3061 return hci_recv_diag(hdev, skb);
3064 return hci_recv_frame(hdev, skb);
3067 MODULE_FIRMWARE(FIRMWARE_MT7663);
3068 MODULE_FIRMWARE(FIRMWARE_MT7668);
3071 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3072 static int marvell_config_oob_wake(struct hci_dev *hdev)
3074 struct sk_buff *skb;
3075 struct btusb_data *data = hci_get_drvdata(hdev);
3076 struct device *dev = &data->udev->dev;
3077 u16 pin, gap, opcode;
3081 /* Move on if no wakeup pin specified */
3082 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3083 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3086 /* Vendor specific command to configure a GPIO as wake-up pin */
3087 opcode = hci_opcode_pack(0x3F, 0x59);
3088 cmd[0] = opcode & 0xFF;
3089 cmd[1] = opcode >> 8;
3090 cmd[2] = 2; /* length of parameters that follow */
3092 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3094 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3096 bt_dev_err(hdev, "%s: No memory", __func__);
3100 skb_put_data(skb, cmd, sizeof(cmd));
3101 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3103 ret = btusb_send_frame(hdev, skb);
3105 bt_dev_err(hdev, "%s: configuration failed", __func__);
3114 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3115 const bdaddr_t *bdaddr)
3117 struct sk_buff *skb;
3122 buf[1] = sizeof(bdaddr_t);
3123 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3125 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3128 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3137 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3138 const bdaddr_t *bdaddr)
3140 struct sk_buff *skb;
3147 buf[3] = sizeof(bdaddr_t);
3148 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3150 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3153 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3161 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3162 const bdaddr_t *bdaddr)
3164 struct sk_buff *skb;
3168 memcpy(buf, bdaddr, sizeof(bdaddr_t));
3170 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3171 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3174 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3182 #define QCA_DFU_PACKET_LEN 4096
3184 #define QCA_GET_TARGET_VERSION 0x09
3185 #define QCA_CHECK_STATUS 0x05
3186 #define QCA_DFU_DOWNLOAD 0x01
3188 #define QCA_SYSCFG_UPDATED 0x40
3189 #define QCA_PATCH_UPDATED 0x80
3190 #define QCA_DFU_TIMEOUT 3000
3191 #define QCA_FLAG_MULTI_NVM 0x80
3193 struct qca_version {
3195 __le32 patch_version;
3202 struct qca_rampatch_version {
3203 __le16 rom_version_high;
3204 __le16 rom_version_low;
3205 __le16 patch_version;
3208 struct qca_device_info {
3210 u8 rampatch_hdr; /* length of header in rampatch */
3211 u8 nvm_hdr; /* length of header in NVM */
3212 u8 ver_offset; /* offset of version structure in rampatch */
3215 static const struct qca_device_info qca_devices_table[] = {
3216 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3217 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3218 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3219 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3220 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3221 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3222 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3223 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3226 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3227 void *data, u16 size)
3232 buf = kmalloc(size, GFP_KERNEL);
3236 /* Found some of USB hosts have IOT issues with ours so that we should
3237 * not wait until HCI layer is ready.
3239 pipe = usb_rcvctrlpipe(udev, 0);
3240 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3241 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3243 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3247 memcpy(data, buf, size);
3255 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3256 const struct firmware *firmware,
3259 struct btusb_data *btdata = hci_get_drvdata(hdev);
3260 struct usb_device *udev = btdata->udev;
3261 size_t count, size, sent = 0;
3265 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3269 count = firmware->size;
3271 size = min_t(size_t, count, hdr_size);
3272 memcpy(buf, firmware->data, size);
3274 /* USB patches should go down to controller through USB path
3275 * because binary format fits to go down through USB channel.
3276 * USB control path is for patching headers and USB bulk is for
3279 pipe = usb_sndctrlpipe(udev, 0);
3280 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3281 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3283 bt_dev_err(hdev, "Failed to send headers (%d)", err);
3290 /* ep2 need time to switch from function acl to function dfu,
3291 * so we add 20ms delay here.
3296 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3298 memcpy(buf, firmware->data + sent, size);
3300 pipe = usb_sndbulkpipe(udev, 0x02);
3301 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3304 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3305 sent, firmware->size, err);
3310 bt_dev_err(hdev, "Failed to get bulk buffer");
3324 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3325 struct qca_version *ver,
3326 const struct qca_device_info *info)
3328 struct qca_rampatch_version *rver;
3329 const struct firmware *fw;
3330 u32 ver_rom, ver_patch, rver_rom;
3331 u16 rver_rom_low, rver_rom_high, rver_patch;
3335 ver_rom = le32_to_cpu(ver->rom_version);
3336 ver_patch = le32_to_cpu(ver->patch_version);
3338 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
3340 err = request_firmware(&fw, fwname, &hdev->dev);
3342 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3347 bt_dev_info(hdev, "using rampatch file: %s", fwname);
3349 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3350 rver_rom_low = le16_to_cpu(rver->rom_version_low);
3351 rver_patch = le16_to_cpu(rver->patch_version);
3353 if (ver_rom & ~0xffffU) {
3354 rver_rom_high = le16_to_cpu(rver->rom_version_high);
3355 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
3357 rver_rom = rver_rom_low;
3360 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3361 "firmware rome 0x%x build 0x%x",
3362 rver_rom, rver_patch, ver_rom, ver_patch);
3364 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
3365 bt_dev_err(hdev, "rampatch file version did not match with firmware");
3370 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3373 release_firmware(fw);
3378 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3379 struct qca_version *ver,
3380 const struct qca_device_info *info)
3382 const struct firmware *fw;
3386 if (((ver->flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
3387 /* if boardid equal 0, use default nvm without surfix */
3388 if (le16_to_cpu(ver->board_id) == 0x0) {
3389 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
3390 le32_to_cpu(ver->rom_version));
3392 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x_%04x.bin",
3393 le32_to_cpu(ver->rom_version),
3394 le16_to_cpu(ver->board_id));
3397 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
3398 le32_to_cpu(ver->rom_version));
3401 err = request_firmware(&fw, fwname, &hdev->dev);
3403 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3408 bt_dev_info(hdev, "using NVM file: %s", fwname);
3410 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3412 release_firmware(fw);
3417 /* identify the ROM version and check whether patches are needed */
3418 static bool btusb_qca_need_patch(struct usb_device *udev)
3420 struct qca_version ver;
3422 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3425 /* only low ROM versions need patches */
3426 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3429 static int btusb_setup_qca(struct hci_dev *hdev)
3431 struct btusb_data *btdata = hci_get_drvdata(hdev);
3432 struct usb_device *udev = btdata->udev;
3433 const struct qca_device_info *info = NULL;
3434 struct qca_version ver;
3439 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3444 ver_rom = le32_to_cpu(ver.rom_version);
3446 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
3447 if (ver_rom == qca_devices_table[i].rom_version)
3448 info = &qca_devices_table[i];
3451 /* If the rom_version is not matched in the qca_devices_table
3452 * and the high ROM version is not zero, we assume this chip no
3453 * need to load the rampatch and nvm.
3455 if (ver_rom & ~0xffffU)
3458 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3462 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3467 if (!(status & QCA_PATCH_UPDATED)) {
3468 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3473 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3478 if (!(status & QCA_SYSCFG_UPDATED)) {
3479 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3487 static inline int __set_diag_interface(struct hci_dev *hdev)
3489 struct btusb_data *data = hci_get_drvdata(hdev);
3490 struct usb_interface *intf = data->diag;
3496 data->diag_tx_ep = NULL;
3497 data->diag_rx_ep = NULL;
3499 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3500 struct usb_endpoint_descriptor *ep_desc;
3502 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3504 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3505 data->diag_tx_ep = ep_desc;
3509 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3510 data->diag_rx_ep = ep_desc;
3515 if (!data->diag_tx_ep || !data->diag_rx_ep) {
3516 bt_dev_err(hdev, "invalid diagnostic descriptors");
3523 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3525 struct btusb_data *data = hci_get_drvdata(hdev);
3526 struct sk_buff *skb;
3530 if (!data->diag_tx_ep)
3531 return ERR_PTR(-ENODEV);
3533 urb = usb_alloc_urb(0, GFP_KERNEL);
3535 return ERR_PTR(-ENOMEM);
3537 skb = bt_skb_alloc(2, GFP_KERNEL);
3540 return ERR_PTR(-ENOMEM);
3543 skb_put_u8(skb, 0xf0);
3544 skb_put_u8(skb, enable);
3546 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3548 usb_fill_bulk_urb(urb, data->udev, pipe,
3549 skb->data, skb->len, btusb_tx_complete, skb);
3551 skb->dev = (void *)hdev;
3556 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3558 struct btusb_data *data = hci_get_drvdata(hdev);
3564 if (!test_bit(HCI_RUNNING, &hdev->flags))
3567 urb = alloc_diag_urb(hdev, enable);
3569 return PTR_ERR(urb);
3571 return submit_or_queue_tx_urb(hdev, urb);
3575 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3577 struct btusb_data *data = priv;
3579 pm_wakeup_event(&data->udev->dev, 0);
3582 /* Disable only if not already disabled (keep it balanced) */
3583 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3584 disable_irq_nosync(irq);
3585 disable_irq_wake(irq);
3590 static const struct of_device_id btusb_match_table[] = {
3591 { .compatible = "usb1286,204e" },
3592 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3593 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
3596 MODULE_DEVICE_TABLE(of, btusb_match_table);
3598 /* Use an oob wakeup pin? */
3599 static int btusb_config_oob_wake(struct hci_dev *hdev)
3601 struct btusb_data *data = hci_get_drvdata(hdev);
3602 struct device *dev = &data->udev->dev;
3605 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3607 if (!of_match_device(btusb_match_table, dev))
3610 /* Move on if no IRQ specified */
3611 irq = of_irq_get_byname(dev->of_node, "wakeup");
3613 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
3617 irq_set_status_flags(irq, IRQ_NOAUTOEN);
3618 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
3619 0, "OOB Wake-on-BT", data);
3621 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
3625 ret = device_init_wakeup(dev, true);
3627 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
3631 data->oob_wake_irq = irq;
3632 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
3637 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
3639 if (dmi_check_system(btusb_needs_reset_resume_table))
3640 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3643 static bool btusb_wakeup(struct hci_dev *hdev)
3645 struct btusb_data *data = hci_get_drvdata(hdev);
3647 return device_may_wakeup(&data->udev->dev);
3650 static int btusb_shutdown_qca(struct hci_dev *hdev)
3652 struct sk_buff *skb;
3654 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3656 bt_dev_err(hdev, "HCI reset during shutdown failed");
3657 return PTR_ERR(skb);
3664 static int btusb_probe(struct usb_interface *intf,
3665 const struct usb_device_id *id)
3667 struct usb_endpoint_descriptor *ep_desc;
3668 struct gpio_desc *reset_gpio;
3669 struct btusb_data *data;
3670 struct hci_dev *hdev;
3671 unsigned ifnum_base;
3672 int i, err, priv_size;
3674 BT_DBG("intf %p id %p", intf, id);
3676 /* interface numbers are hardcoded in the spec */
3677 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
3678 if (!(id->driver_info & BTUSB_IFNUM_2))
3680 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
3684 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
3686 if (!id->driver_info) {
3687 const struct usb_device_id *match;
3689 match = usb_match_id(intf, blacklist_table);
3694 if (id->driver_info == BTUSB_IGNORE)
3697 if (id->driver_info & BTUSB_ATH3012) {
3698 struct usb_device *udev = interface_to_usbdev(intf);
3700 /* Old firmware would otherwise let ath3k driver load
3701 * patch and sysconfig files
3703 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
3704 !btusb_qca_need_patch(udev))
3708 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
3712 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3713 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3715 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3716 data->intr_ep = ep_desc;
3720 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3721 data->bulk_tx_ep = ep_desc;
3725 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3726 data->bulk_rx_ep = ep_desc;
3731 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
3734 if (id->driver_info & BTUSB_AMP) {
3735 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3736 data->cmdreq = 0x2b;
3738 data->cmdreq_type = USB_TYPE_CLASS;
3739 data->cmdreq = 0x00;
3742 data->udev = interface_to_usbdev(intf);
3745 INIT_WORK(&data->work, btusb_work);
3746 INIT_WORK(&data->waker, btusb_waker);
3747 init_usb_anchor(&data->deferred);
3748 init_usb_anchor(&data->tx_anchor);
3749 spin_lock_init(&data->txlock);
3751 init_usb_anchor(&data->intr_anchor);
3752 init_usb_anchor(&data->bulk_anchor);
3753 init_usb_anchor(&data->isoc_anchor);
3754 init_usb_anchor(&data->diag_anchor);
3755 init_usb_anchor(&data->ctrl_anchor);
3756 spin_lock_init(&data->rxlock);
3760 data->recv_event = hci_recv_frame;
3761 data->recv_bulk = btusb_recv_bulk;
3763 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3764 /* Allocate extra space for Intel device */
3765 priv_size += sizeof(struct btintel_data);
3767 /* Override the rx handlers */
3768 data->recv_event = btusb_recv_event_intel;
3769 data->recv_bulk = btusb_recv_bulk_intel;
3772 data->recv_acl = hci_recv_frame;
3774 hdev = hci_alloc_dev_priv(priv_size);
3778 hdev->bus = HCI_USB;
3779 hci_set_drvdata(hdev, data);
3781 if (id->driver_info & BTUSB_AMP)
3782 hdev->dev_type = HCI_AMP;
3784 hdev->dev_type = HCI_PRIMARY;
3788 SET_HCIDEV_DEV(hdev, &intf->dev);
3790 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
3792 if (IS_ERR(reset_gpio)) {
3793 err = PTR_ERR(reset_gpio);
3795 } else if (reset_gpio) {
3796 data->reset_gpio = reset_gpio;
3799 hdev->open = btusb_open;
3800 hdev->close = btusb_close;
3801 hdev->flush = btusb_flush;
3802 hdev->send = btusb_send_frame;
3803 hdev->notify = btusb_notify;
3804 hdev->wakeup = btusb_wakeup;
3807 err = btusb_config_oob_wake(hdev);
3811 /* Marvell devices may need a specific chip configuration */
3812 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
3813 err = marvell_config_oob_wake(hdev);
3818 if (id->driver_info & BTUSB_CW6622)
3819 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3821 if (id->driver_info & BTUSB_BCM2045)
3822 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3824 if (id->driver_info & BTUSB_BCM92035)
3825 hdev->setup = btusb_setup_bcm92035;
3827 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3828 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
3829 hdev->manufacturer = 15;
3830 hdev->setup = btbcm_setup_patchram;
3831 hdev->set_diag = btusb_bcm_set_diag;
3832 hdev->set_bdaddr = btbcm_set_bdaddr;
3834 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3835 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3838 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3839 (id->driver_info & BTUSB_BCM_APPLE)) {
3840 hdev->manufacturer = 15;
3841 hdev->setup = btbcm_setup_apple;
3842 hdev->set_diag = btusb_bcm_set_diag;
3844 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3845 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3848 /* Combined Intel Device setup to support multiple setup routine */
3849 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3850 err = btintel_configure_setup(hdev);
3854 /* Transport specific configuration */
3855 hdev->send = btusb_send_frame_intel;
3856 hdev->cmd_timeout = btusb_intel_cmd_timeout;
3858 if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
3859 btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
3862 if (id->driver_info & BTUSB_MARVELL)
3863 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3865 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
3866 (id->driver_info & BTUSB_MEDIATEK)) {
3867 hdev->setup = btusb_mtk_setup;
3868 hdev->shutdown = btusb_mtk_shutdown;
3869 hdev->manufacturer = 70;
3870 hdev->cmd_timeout = btusb_mtk_cmd_timeout;
3871 hdev->set_bdaddr = btusb_set_bdaddr_mtk;
3872 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
3873 data->recv_acl = btusb_recv_acl_mtk;
3876 if (id->driver_info & BTUSB_SWAVE) {
3877 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
3878 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
3881 if (id->driver_info & BTUSB_INTEL_BOOT) {
3882 hdev->manufacturer = 2;
3883 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3886 if (id->driver_info & BTUSB_ATH3012) {
3887 data->setup_on_usb = btusb_setup_qca;
3888 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3889 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3890 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3893 if (id->driver_info & BTUSB_QCA_ROME) {
3894 data->setup_on_usb = btusb_setup_qca;
3895 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3896 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3897 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3898 btusb_check_needs_reset_resume(intf);
3901 if (id->driver_info & BTUSB_QCA_WCN6855) {
3902 data->setup_on_usb = btusb_setup_qca;
3903 hdev->shutdown = btusb_shutdown_qca;
3904 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
3905 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3906 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3907 hci_set_msft_opcode(hdev, 0xFD70);
3910 if (id->driver_info & BTUSB_AMP) {
3911 /* AMP controllers do not support SCO packets */
3914 /* Interface orders are hardcoded in the specification */
3915 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
3916 data->isoc_ifnum = ifnum_base + 1;
3919 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
3920 (id->driver_info & BTUSB_REALTEK)) {
3921 hdev->setup = btrtl_setup_realtek;
3922 hdev->shutdown = btrtl_shutdown_realtek;
3923 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
3925 /* Realtek devices need to set remote wakeup on auto-suspend */
3926 set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
3927 set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
3931 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3933 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3934 if (!disable_scofix)
3935 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3938 if (id->driver_info & BTUSB_BROKEN_ISOC)
3941 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
3942 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
3944 if (id->driver_info & BTUSB_VALID_LE_STATES)
3945 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
3947 if (id->driver_info & BTUSB_DIGIANSWER) {
3948 data->cmdreq_type = USB_TYPE_VENDOR;
3949 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3952 if (id->driver_info & BTUSB_CSR) {
3953 struct usb_device *udev = data->udev;
3954 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
3956 /* Old firmware would otherwise execute USB reset */
3957 if (bcdDevice < 0x117)
3958 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3960 /* This must be set first in case we disable it for fakes */
3961 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3963 /* Fake CSR devices with broken commands */
3964 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
3965 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
3966 hdev->setup = btusb_setup_csr;
3969 if (id->driver_info & BTUSB_SNIFFER) {
3970 struct usb_device *udev = data->udev;
3972 /* New sniffer firmware has crippled HCI interface */
3973 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3974 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3977 if (id->driver_info & BTUSB_INTEL_BOOT) {
3978 /* A bug in the bootloader causes that interrupt interface is
3979 * only enabled after receiving SetInterface(0, AltSetting=0).
3981 err = usb_set_interface(data->udev, 0, 0);
3983 BT_ERR("failed to set interface 0, alt 0 %d", err);
3989 err = usb_driver_claim_interface(&btusb_driver,
3995 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
3996 if (!usb_driver_claim_interface(&btusb_driver,
3998 __set_diag_interface(hdev);
4003 if (enable_autosuspend)
4004 usb_enable_autosuspend(data->udev);
4006 err = hci_register_dev(hdev);
4010 usb_set_intfdata(intf, data);
4015 if (data->reset_gpio)
4016 gpiod_put(data->reset_gpio);
4021 static void btusb_disconnect(struct usb_interface *intf)
4023 struct btusb_data *data = usb_get_intfdata(intf);
4024 struct hci_dev *hdev;
4026 BT_DBG("intf %p", intf);
4032 usb_set_intfdata(data->intf, NULL);
4035 usb_set_intfdata(data->isoc, NULL);
4038 usb_set_intfdata(data->diag, NULL);
4040 hci_unregister_dev(hdev);
4042 if (intf == data->intf) {
4044 usb_driver_release_interface(&btusb_driver, data->isoc);
4046 usb_driver_release_interface(&btusb_driver, data->diag);
4047 } else if (intf == data->isoc) {
4049 usb_driver_release_interface(&btusb_driver, data->diag);
4050 usb_driver_release_interface(&btusb_driver, data->intf);
4051 } else if (intf == data->diag) {
4052 usb_driver_release_interface(&btusb_driver, data->intf);
4054 usb_driver_release_interface(&btusb_driver, data->isoc);
4057 if (data->oob_wake_irq)
4058 device_init_wakeup(&data->udev->dev, false);
4060 if (data->reset_gpio)
4061 gpiod_put(data->reset_gpio);
4067 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4069 struct btusb_data *data = usb_get_intfdata(intf);
4071 BT_DBG("intf %p", intf);
4073 if (data->suspend_count++)
4076 spin_lock_irq(&data->txlock);
4077 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4078 set_bit(BTUSB_SUSPENDING, &data->flags);
4079 spin_unlock_irq(&data->txlock);
4081 spin_unlock_irq(&data->txlock);
4082 data->suspend_count--;
4086 cancel_work_sync(&data->work);
4088 btusb_stop_traffic(data);
4089 usb_kill_anchored_urbs(&data->tx_anchor);
4091 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4092 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4093 enable_irq_wake(data->oob_wake_irq);
4094 enable_irq(data->oob_wake_irq);
4097 /* For global suspend, Realtek devices lose the loaded fw
4098 * in them. But for autosuspend, firmware should remain.
4099 * Actually, it depends on whether the usb host sends
4100 * set feature (enable wakeup) or not.
4102 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
4103 if (PMSG_IS_AUTO(message) &&
4104 device_can_wakeup(&data->udev->dev))
4105 data->udev->do_remote_wakeup = 1;
4106 else if (!PMSG_IS_AUTO(message) &&
4107 !device_may_wakeup(&data->udev->dev)) {
4108 data->udev->do_remote_wakeup = 0;
4109 data->udev->reset_resume = 1;
4116 static void play_deferred(struct btusb_data *data)
4121 while ((urb = usb_get_from_anchor(&data->deferred))) {
4122 usb_anchor_urb(urb, &data->tx_anchor);
4124 err = usb_submit_urb(urb, GFP_ATOMIC);
4126 if (err != -EPERM && err != -ENODEV)
4127 BT_ERR("%s urb %p submission failed (%d)",
4128 data->hdev->name, urb, -err);
4129 kfree(urb->setup_packet);
4130 usb_unanchor_urb(urb);
4135 data->tx_in_flight++;
4139 /* Cleanup the rest deferred urbs. */
4140 while ((urb = usb_get_from_anchor(&data->deferred))) {
4141 kfree(urb->setup_packet);
4146 static int btusb_resume(struct usb_interface *intf)
4148 struct btusb_data *data = usb_get_intfdata(intf);
4149 struct hci_dev *hdev = data->hdev;
4152 BT_DBG("intf %p", intf);
4154 if (--data->suspend_count)
4157 /* Disable only if not already disabled (keep it balanced) */
4158 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4159 disable_irq(data->oob_wake_irq);
4160 disable_irq_wake(data->oob_wake_irq);
4163 if (!test_bit(HCI_RUNNING, &hdev->flags))
4166 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4167 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4169 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4174 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4175 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4177 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4181 btusb_submit_bulk_urb(hdev, GFP_NOIO);
4184 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4185 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4186 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4188 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4191 spin_lock_irq(&data->txlock);
4192 play_deferred(data);
4193 clear_bit(BTUSB_SUSPENDING, &data->flags);
4194 spin_unlock_irq(&data->txlock);
4195 schedule_work(&data->work);
4200 usb_scuttle_anchored_urbs(&data->deferred);
4202 spin_lock_irq(&data->txlock);
4203 clear_bit(BTUSB_SUSPENDING, &data->flags);
4204 spin_unlock_irq(&data->txlock);
4210 static struct usb_driver btusb_driver = {
4212 .probe = btusb_probe,
4213 .disconnect = btusb_disconnect,
4215 .suspend = btusb_suspend,
4216 .resume = btusb_resume,
4218 .id_table = btusb_table,
4219 .supports_autosuspend = 1,
4220 .disable_hub_initiated_lpm = 1,
4223 module_usb_driver(btusb_driver);
4225 module_param(disable_scofix, bool, 0644);
4226 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4228 module_param(force_scofix, bool, 0644);
4229 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4231 module_param(enable_autosuspend, bool, 0644);
4232 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4234 module_param(reset, bool, 0644);
4235 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4237 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4238 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4239 MODULE_VERSION(VERSION);
4240 MODULE_LICENSE("GPL");