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 <linux/debugfs.h>
20 #include <asm/unaligned.h>
22 #include <net/bluetooth/bluetooth.h>
23 #include <net/bluetooth/hci_core.h>
32 static bool disable_scofix;
33 static bool force_scofix;
34 static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
35 static bool reset = true;
37 static struct usb_driver btusb_driver;
39 #define BTUSB_IGNORE BIT(0)
40 #define BTUSB_DIGIANSWER BIT(1)
41 #define BTUSB_CSR BIT(2)
42 #define BTUSB_SNIFFER BIT(3)
43 #define BTUSB_BCM92035 BIT(4)
44 #define BTUSB_BROKEN_ISOC BIT(5)
45 #define BTUSB_WRONG_SCO_MTU BIT(6)
46 #define BTUSB_ATH3012 BIT(7)
47 #define BTUSB_INTEL_COMBINED BIT(8)
48 #define BTUSB_INTEL_BOOT BIT(9)
49 #define BTUSB_BCM_PATCHRAM BIT(10)
50 #define BTUSB_MARVELL BIT(11)
51 #define BTUSB_SWAVE BIT(12)
52 #define BTUSB_AMP BIT(13)
53 #define BTUSB_QCA_ROME BIT(14)
54 #define BTUSB_BCM_APPLE BIT(15)
55 #define BTUSB_REALTEK BIT(16)
56 #define BTUSB_BCM2045 BIT(17)
57 #define BTUSB_IFNUM_2 BIT(18)
58 #define BTUSB_CW6622 BIT(19)
59 #define BTUSB_MEDIATEK BIT(20)
60 #define BTUSB_WIDEBAND_SPEECH BIT(21)
61 #define BTUSB_VALID_LE_STATES BIT(22)
62 #define BTUSB_QCA_WCN6855 BIT(23)
63 #define BTUSB_INTEL_BROKEN_SHUTDOWN_LED BIT(24)
64 #define BTUSB_INTEL_BROKEN_INITIAL_NCMD BIT(25)
65 #define BTUSB_INTEL_NO_WBS_SUPPORT BIT(26)
67 static const struct usb_device_id btusb_table[] = {
68 /* Generic Bluetooth USB device */
69 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
71 /* Generic Bluetooth AMP device */
72 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
74 /* Generic Bluetooth USB interface */
75 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
77 /* Apple-specific (Broadcom) devices */
78 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
79 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
81 /* MediaTek MT76x0E */
82 { USB_DEVICE(0x0e8d, 0x763f) },
84 /* Broadcom SoftSailing reporting vendor specific */
85 { USB_DEVICE(0x0a5c, 0x21e1) },
87 /* Apple MacBookPro 7,1 */
88 { USB_DEVICE(0x05ac, 0x8213) },
91 { USB_DEVICE(0x05ac, 0x8215) },
93 /* Apple MacBookPro6,2 */
94 { USB_DEVICE(0x05ac, 0x8218) },
96 /* Apple MacBookAir3,1, MacBookAir3,2 */
97 { USB_DEVICE(0x05ac, 0x821b) },
99 /* Apple MacBookAir4,1 */
100 { USB_DEVICE(0x05ac, 0x821f) },
102 /* Apple MacBookPro8,2 */
103 { USB_DEVICE(0x05ac, 0x821a) },
105 /* Apple MacMini5,1 */
106 { USB_DEVICE(0x05ac, 0x8281) },
108 /* AVM BlueFRITZ! USB v2.0 */
109 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
111 /* Bluetooth Ultraport Module from IBM */
112 { USB_DEVICE(0x04bf, 0x030a) },
114 /* ALPS Modules with non-standard id */
115 { USB_DEVICE(0x044e, 0x3001) },
116 { USB_DEVICE(0x044e, 0x3002) },
118 /* Ericsson with non-standard id */
119 { USB_DEVICE(0x0bdb, 0x1002) },
121 /* Canyon CN-BTU1 with HID interfaces */
122 { USB_DEVICE(0x0c10, 0x0000) },
124 /* Broadcom BCM20702B0 (Dynex/Insignia) */
125 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
127 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
128 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
129 .driver_info = BTUSB_BCM_PATCHRAM },
131 /* Broadcom BCM920703 (HTC Vive) */
132 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
133 .driver_info = BTUSB_BCM_PATCHRAM },
135 /* Foxconn - Hon Hai */
136 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
137 .driver_info = BTUSB_BCM_PATCHRAM },
139 /* Lite-On Technology - Broadcom based */
140 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
141 .driver_info = BTUSB_BCM_PATCHRAM },
143 /* Broadcom devices with vendor specific id */
144 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
145 .driver_info = BTUSB_BCM_PATCHRAM },
147 /* ASUSTek Computer - Broadcom based */
148 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
149 .driver_info = BTUSB_BCM_PATCHRAM },
151 /* Belkin F8065bf - Broadcom based */
152 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
153 .driver_info = BTUSB_BCM_PATCHRAM },
155 /* IMC Networks - Broadcom based */
156 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
157 .driver_info = BTUSB_BCM_PATCHRAM },
159 /* Dell Computer - Broadcom based */
160 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
161 .driver_info = BTUSB_BCM_PATCHRAM },
163 /* Toshiba Corp - Broadcom based */
164 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
165 .driver_info = BTUSB_BCM_PATCHRAM },
167 /* Intel Bluetooth USB Bootloader (RAM module) */
168 { USB_DEVICE(0x8087, 0x0a5a),
169 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
171 { } /* Terminating entry */
174 MODULE_DEVICE_TABLE(usb, btusb_table);
176 static const struct usb_device_id blacklist_table[] = {
177 /* CSR BlueCore devices */
178 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
180 /* Broadcom BCM2033 without firmware */
181 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
183 /* Broadcom BCM2045 devices */
184 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
186 /* Atheros 3011 with sflash firmware */
187 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
188 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
189 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
190 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
191 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
192 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
193 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
195 /* Atheros AR9285 Malbec with sflash firmware */
196 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
198 /* Atheros 3012 with sflash firmware */
199 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
200 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
246 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
247 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
248 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
250 /* Atheros AR5BBU12 with sflash firmware */
251 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
253 /* Atheros AR5BBU12 with sflash firmware */
254 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
255 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
257 /* QCA ROME chipset */
258 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
259 BTUSB_WIDEBAND_SPEECH },
260 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
261 BTUSB_WIDEBAND_SPEECH },
262 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
263 BTUSB_WIDEBAND_SPEECH },
264 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
265 BTUSB_WIDEBAND_SPEECH },
266 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
267 BTUSB_WIDEBAND_SPEECH },
268 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
269 BTUSB_WIDEBAND_SPEECH },
270 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
271 BTUSB_WIDEBAND_SPEECH },
272 { USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
273 BTUSB_WIDEBAND_SPEECH },
274 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
275 BTUSB_WIDEBAND_SPEECH },
276 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
277 BTUSB_WIDEBAND_SPEECH },
278 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
279 BTUSB_WIDEBAND_SPEECH },
280 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
281 BTUSB_WIDEBAND_SPEECH },
282 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
283 BTUSB_WIDEBAND_SPEECH },
284 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
285 BTUSB_WIDEBAND_SPEECH },
286 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
287 BTUSB_WIDEBAND_SPEECH },
288 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
289 BTUSB_WIDEBAND_SPEECH },
290 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
291 BTUSB_WIDEBAND_SPEECH },
292 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
293 BTUSB_WIDEBAND_SPEECH },
294 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
295 BTUSB_WIDEBAND_SPEECH },
297 /* QCA WCN6855 chipset */
298 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
299 BTUSB_WIDEBAND_SPEECH |
300 BTUSB_VALID_LE_STATES },
301 { USB_DEVICE(0x0489, 0xe0cc), .driver_info = BTUSB_QCA_WCN6855 |
302 BTUSB_WIDEBAND_SPEECH |
303 BTUSB_VALID_LE_STATES },
304 { USB_DEVICE(0x0489, 0xe0d6), .driver_info = BTUSB_QCA_WCN6855 |
305 BTUSB_WIDEBAND_SPEECH |
306 BTUSB_VALID_LE_STATES },
307 { USB_DEVICE(0x0489, 0xe0e3), .driver_info = BTUSB_QCA_WCN6855 |
308 BTUSB_WIDEBAND_SPEECH |
309 BTUSB_VALID_LE_STATES },
310 { USB_DEVICE(0x10ab, 0x9309), .driver_info = BTUSB_QCA_WCN6855 |
311 BTUSB_WIDEBAND_SPEECH |
312 BTUSB_VALID_LE_STATES },
313 { USB_DEVICE(0x10ab, 0x9409), .driver_info = BTUSB_QCA_WCN6855 |
314 BTUSB_WIDEBAND_SPEECH |
315 BTUSB_VALID_LE_STATES },
316 { USB_DEVICE(0x0489, 0xe0d0), .driver_info = BTUSB_QCA_WCN6855 |
317 BTUSB_WIDEBAND_SPEECH |
318 BTUSB_VALID_LE_STATES },
320 /* Broadcom BCM2035 */
321 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
322 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
323 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
325 /* Broadcom BCM2045 */
326 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
327 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
329 /* IBM/Lenovo ThinkPad with Broadcom chip */
330 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
331 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
333 /* HP laptop with Broadcom chip */
334 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
336 /* Dell laptop with Broadcom chip */
337 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
339 /* Dell Wireless 370 and 410 devices */
340 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
341 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
343 /* Belkin F8T012 and F8T013 devices */
344 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
345 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
347 /* Asus WL-BTD202 device */
348 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
350 /* Kensington Bluetooth USB adapter */
351 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
353 /* RTX Telecom based adapters with buggy SCO support */
354 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
355 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
357 /* CONWISE Technology based adapters with buggy SCO support */
358 { USB_DEVICE(0x0e5e, 0x6622),
359 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
361 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
362 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
364 /* Digianswer devices */
365 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
366 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
368 /* CSR BlueCore Bluetooth Sniffer */
369 { USB_DEVICE(0x0a12, 0x0002),
370 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
372 /* Frontline ComProbe Bluetooth Sniffer */
373 { USB_DEVICE(0x16d3, 0x0002),
374 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
376 /* Marvell Bluetooth devices */
377 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
378 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
379 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
381 /* Intel Bluetooth devices */
382 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_COMBINED },
383 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_COMBINED },
384 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_COMBINED },
385 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_COMBINED },
386 { USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_COMBINED },
387 { USB_DEVICE(0x8087, 0x0035), .driver_info = BTUSB_INTEL_COMBINED },
388 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
389 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL_COMBINED |
390 BTUSB_INTEL_NO_WBS_SUPPORT |
391 BTUSB_INTEL_BROKEN_INITIAL_NCMD |
392 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
393 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL_COMBINED |
394 BTUSB_INTEL_NO_WBS_SUPPORT |
395 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
396 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_COMBINED },
397 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL_COMBINED |
398 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
399 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_COMBINED },
401 /* Other Intel Bluetooth devices */
402 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
403 .driver_info = BTUSB_IGNORE },
405 /* Realtek 8822CE Bluetooth devices */
406 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
407 BTUSB_WIDEBAND_SPEECH },
408 { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
409 BTUSB_WIDEBAND_SPEECH },
411 /* Realtek 8852AE Bluetooth devices */
412 { USB_DEVICE(0x0bda, 0x2852), .driver_info = BTUSB_REALTEK |
413 BTUSB_WIDEBAND_SPEECH },
414 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
415 BTUSB_WIDEBAND_SPEECH },
416 { USB_DEVICE(0x0bda, 0x385a), .driver_info = BTUSB_REALTEK |
417 BTUSB_WIDEBAND_SPEECH },
418 { USB_DEVICE(0x0bda, 0x4852), .driver_info = BTUSB_REALTEK |
419 BTUSB_WIDEBAND_SPEECH },
420 { USB_DEVICE(0x04c5, 0x165c), .driver_info = BTUSB_REALTEK |
421 BTUSB_WIDEBAND_SPEECH },
422 { USB_DEVICE(0x04ca, 0x4006), .driver_info = BTUSB_REALTEK |
423 BTUSB_WIDEBAND_SPEECH },
425 /* Realtek Bluetooth devices */
426 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
427 .driver_info = BTUSB_REALTEK },
429 /* MediaTek Bluetooth devices */
430 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
431 .driver_info = BTUSB_MEDIATEK |
432 BTUSB_WIDEBAND_SPEECH |
433 BTUSB_VALID_LE_STATES },
435 /* Additional MediaTek MT7615E Bluetooth devices */
436 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
438 /* Additional MediaTek MT7663 Bluetooth devices */
439 { USB_DEVICE(0x043e, 0x310c), .driver_info = BTUSB_MEDIATEK |
440 BTUSB_WIDEBAND_SPEECH |
441 BTUSB_VALID_LE_STATES },
443 /* Additional MediaTek MT7668 Bluetooth devices */
444 { USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
445 BTUSB_WIDEBAND_SPEECH |
446 BTUSB_VALID_LE_STATES },
448 /* Additional MediaTek MT7921 Bluetooth devices */
449 { USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
450 BTUSB_WIDEBAND_SPEECH |
451 BTUSB_VALID_LE_STATES },
452 { USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
453 BTUSB_WIDEBAND_SPEECH |
454 BTUSB_VALID_LE_STATES },
455 { USB_DEVICE(0x13d3, 0x3564), .driver_info = BTUSB_MEDIATEK |
456 BTUSB_WIDEBAND_SPEECH |
457 BTUSB_VALID_LE_STATES },
458 { USB_DEVICE(0x13d3, 0x3567), .driver_info = BTUSB_MEDIATEK |
459 BTUSB_WIDEBAND_SPEECH |
460 BTUSB_VALID_LE_STATES },
461 { USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
462 BTUSB_WIDEBAND_SPEECH |
463 BTUSB_VALID_LE_STATES },
465 /* MediaTek MT7922A Bluetooth devices */
466 { USB_DEVICE(0x0489, 0xe0d8), .driver_info = BTUSB_MEDIATEK |
467 BTUSB_WIDEBAND_SPEECH |
468 BTUSB_VALID_LE_STATES },
469 { USB_DEVICE(0x0489, 0xe0d9), .driver_info = BTUSB_MEDIATEK |
470 BTUSB_WIDEBAND_SPEECH |
471 BTUSB_VALID_LE_STATES },
473 /* Additional Realtek 8723AE Bluetooth devices */
474 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
475 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
477 /* Additional Realtek 8723BE Bluetooth devices */
478 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
479 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
480 { USB_DEVICE(0x04f2, 0xb49f), .driver_info = BTUSB_REALTEK },
481 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
482 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
483 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
484 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
486 /* Additional Realtek 8723BU Bluetooth devices */
487 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
489 /* Additional Realtek 8723DE Bluetooth devices */
490 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
491 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
493 /* Additional Realtek 8761B Bluetooth devices */
494 { USB_DEVICE(0x2357, 0x0604), .driver_info = BTUSB_REALTEK |
495 BTUSB_WIDEBAND_SPEECH },
497 /* Additional Realtek 8761BU Bluetooth devices */
498 { USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
499 BTUSB_WIDEBAND_SPEECH },
500 { USB_DEVICE(0x2550, 0x8761), .driver_info = BTUSB_REALTEK |
501 BTUSB_WIDEBAND_SPEECH },
503 /* Additional Realtek 8821AE Bluetooth devices */
504 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
505 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
506 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
507 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
508 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
510 /* Additional Realtek 8822BE Bluetooth devices */
511 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
512 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
514 /* Additional Realtek 8822CE Bluetooth devices */
515 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
516 BTUSB_WIDEBAND_SPEECH },
517 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
518 BTUSB_WIDEBAND_SPEECH },
519 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
520 BTUSB_WIDEBAND_SPEECH },
521 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
522 BTUSB_WIDEBAND_SPEECH },
523 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
524 BTUSB_WIDEBAND_SPEECH },
525 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
526 BTUSB_WIDEBAND_SPEECH },
527 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
528 BTUSB_WIDEBAND_SPEECH },
529 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
530 BTUSB_WIDEBAND_SPEECH },
531 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
532 BTUSB_WIDEBAND_SPEECH },
533 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
534 BTUSB_WIDEBAND_SPEECH },
535 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
536 BTUSB_WIDEBAND_SPEECH },
538 /* Silicon Wave based devices */
539 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
541 { } /* Terminating entry */
544 /* The Bluetooth USB module build into some devices needs to be reset on resume,
545 * this is a problem with the platform (likely shutting off all power) not with
546 * the module itself. So we use a DMI list to match known broken platforms.
548 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
550 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
552 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
553 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
557 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
559 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
560 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
564 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
566 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
567 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
573 #define BTUSB_MAX_ISOC_FRAMES 10
575 #define BTUSB_INTR_RUNNING 0
576 #define BTUSB_BULK_RUNNING 1
577 #define BTUSB_ISOC_RUNNING 2
578 #define BTUSB_SUSPENDING 3
579 #define BTUSB_DID_ISO_RESUME 4
580 #define BTUSB_BOOTLOADER 5
581 #define BTUSB_DOWNLOADING 6
582 #define BTUSB_FIRMWARE_LOADED 7
583 #define BTUSB_FIRMWARE_FAILED 8
584 #define BTUSB_BOOTING 9
585 #define BTUSB_DIAG_RUNNING 10
586 #define BTUSB_OOB_WAKE_ENABLED 11
587 #define BTUSB_HW_RESET_ACTIVE 12
588 #define BTUSB_TX_WAIT_VND_EVT 13
589 #define BTUSB_WAKEUP_AUTOSUSPEND 14
590 #define BTUSB_USE_ALT3_FOR_WBS 15
593 struct hci_dev *hdev;
594 struct usb_device *udev;
595 struct usb_interface *intf;
596 struct usb_interface *isoc;
597 struct usb_interface *diag;
604 struct work_struct work;
605 struct work_struct waker;
606 struct delayed_work rx_work;
608 struct sk_buff_head acl_q;
610 struct usb_anchor deferred;
611 struct usb_anchor tx_anchor;
615 struct usb_anchor intr_anchor;
616 struct usb_anchor bulk_anchor;
617 struct usb_anchor isoc_anchor;
618 struct usb_anchor diag_anchor;
619 struct usb_anchor ctrl_anchor;
622 struct sk_buff *evt_skb;
623 struct sk_buff *acl_skb;
624 struct sk_buff *sco_skb;
626 struct usb_endpoint_descriptor *intr_ep;
627 struct usb_endpoint_descriptor *bulk_tx_ep;
628 struct usb_endpoint_descriptor *bulk_rx_ep;
629 struct usb_endpoint_descriptor *isoc_tx_ep;
630 struct usb_endpoint_descriptor *isoc_rx_ep;
631 struct usb_endpoint_descriptor *diag_tx_ep;
632 struct usb_endpoint_descriptor *diag_rx_ep;
634 struct gpio_desc *reset_gpio;
639 unsigned int sco_num;
640 unsigned int air_mode;
641 bool usb_alt6_packet_flow;
645 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
646 int (*recv_acl)(struct hci_dev *hdev, struct sk_buff *skb);
647 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
649 int (*setup_on_usb)(struct hci_dev *hdev);
651 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
652 unsigned cmd_timeout_cnt;
655 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
657 struct btusb_data *data = hci_get_drvdata(hdev);
658 struct gpio_desc *reset_gpio = data->reset_gpio;
660 if (++data->cmd_timeout_cnt < 5)
664 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
669 * Toggle the hard reset line if the platform provides one. The reset
670 * is going to yank the device off the USB and then replug. So doing
671 * once is enough. The cleanup is handled correctly on the way out
672 * (standard USB disconnect), and the new device is detected cleanly
673 * and bound to the driver again like it should be.
675 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
676 bt_dev_err(hdev, "last reset failed? Not resetting again");
680 bt_dev_err(hdev, "Initiating HW reset via gpio");
681 gpiod_set_value_cansleep(reset_gpio, 1);
683 gpiod_set_value_cansleep(reset_gpio, 0);
686 static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
688 struct btusb_data *data = hci_get_drvdata(hdev);
689 struct gpio_desc *reset_gpio = data->reset_gpio;
691 if (++data->cmd_timeout_cnt < 5)
695 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
699 /* Toggle the hard reset line. The Realtek device is going to
700 * yank itself off the USB and then replug. The cleanup is handled
701 * correctly on the way out (standard USB disconnect), and the new
702 * device is detected cleanly and bound to the driver again like
705 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
706 bt_dev_err(hdev, "last reset failed? Not resetting again");
710 bt_dev_err(hdev, "Reset Realtek device via gpio");
711 gpiod_set_value_cansleep(reset_gpio, 1);
713 gpiod_set_value_cansleep(reset_gpio, 0);
716 static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
718 struct btusb_data *data = hci_get_drvdata(hdev);
719 struct gpio_desc *reset_gpio = data->reset_gpio;
722 if (++data->cmd_timeout_cnt < 5)
726 bt_dev_err(hdev, "Reset qca device via bt_en gpio");
728 /* Toggle the hard reset line. The qca bt device is going to
729 * yank itself off the USB and then replug. The cleanup is handled
730 * correctly on the way out (standard USB disconnect), and the new
731 * device is detected cleanly and bound to the driver again like
734 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
735 bt_dev_err(hdev, "last reset failed? Not resetting again");
739 gpiod_set_value_cansleep(reset_gpio, 0);
741 gpiod_set_value_cansleep(reset_gpio, 1);
746 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
747 /* This is not an unbalanced PM reference since the device will reset */
748 err = usb_autopm_get_interface(data->intf);
750 usb_queue_reset_device(data->intf);
752 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
755 static inline void btusb_free_frags(struct btusb_data *data)
759 spin_lock_irqsave(&data->rxlock, flags);
761 kfree_skb(data->evt_skb);
762 data->evt_skb = NULL;
764 kfree_skb(data->acl_skb);
765 data->acl_skb = NULL;
767 kfree_skb(data->sco_skb);
768 data->sco_skb = NULL;
770 spin_unlock_irqrestore(&data->rxlock, flags);
773 static int btusb_recv_event(struct btusb_data *data, struct sk_buff *skb)
775 if (data->intr_interval) {
776 /* Trigger dequeue immediatelly if an event is received */
777 schedule_delayed_work(&data->rx_work, 0);
780 return data->recv_event(data->hdev, skb);
783 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
789 spin_lock_irqsave(&data->rxlock, flags);
796 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
802 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
803 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
806 len = min_t(uint, hci_skb_expect(skb), count);
807 skb_put_data(skb, buffer, len);
811 hci_skb_expect(skb) -= len;
813 if (skb->len == HCI_EVENT_HDR_SIZE) {
814 /* Complete event header */
815 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
817 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
826 if (!hci_skb_expect(skb)) {
828 btusb_recv_event(data, skb);
834 spin_unlock_irqrestore(&data->rxlock, flags);
839 static int btusb_recv_acl(struct btusb_data *data, struct sk_buff *skb)
841 /* Only queue ACL packet if intr_interval is set as it means
842 * force_poll_sync has been enabled.
844 if (!data->intr_interval)
845 return data->recv_acl(data->hdev, skb);
847 skb_queue_tail(&data->acl_q, skb);
848 schedule_delayed_work(&data->rx_work, data->intr_interval);
853 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
859 spin_lock_irqsave(&data->rxlock, flags);
866 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
872 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
873 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
876 len = min_t(uint, hci_skb_expect(skb), count);
877 skb_put_data(skb, buffer, len);
881 hci_skb_expect(skb) -= len;
883 if (skb->len == HCI_ACL_HDR_SIZE) {
884 __le16 dlen = hci_acl_hdr(skb)->dlen;
886 /* Complete ACL header */
887 hci_skb_expect(skb) = __le16_to_cpu(dlen);
889 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
898 if (!hci_skb_expect(skb)) {
900 btusb_recv_acl(data, skb);
906 spin_unlock_irqrestore(&data->rxlock, flags);
911 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
917 spin_lock_irqsave(&data->rxlock, flags);
924 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
930 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
931 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
934 len = min_t(uint, hci_skb_expect(skb), count);
935 skb_put_data(skb, buffer, len);
939 hci_skb_expect(skb) -= len;
941 if (skb->len == HCI_SCO_HDR_SIZE) {
942 /* Complete SCO header */
943 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
945 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
954 if (!hci_skb_expect(skb)) {
956 hci_recv_frame(data->hdev, skb);
962 spin_unlock_irqrestore(&data->rxlock, flags);
967 static void btusb_intr_complete(struct urb *urb)
969 struct hci_dev *hdev = urb->context;
970 struct btusb_data *data = hci_get_drvdata(hdev);
973 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
976 if (!test_bit(HCI_RUNNING, &hdev->flags))
979 if (urb->status == 0) {
980 hdev->stat.byte_rx += urb->actual_length;
982 if (btusb_recv_intr(data, urb->transfer_buffer,
983 urb->actual_length) < 0) {
984 bt_dev_err(hdev, "corrupted event packet");
987 } else if (urb->status == -ENOENT) {
988 /* Avoid suspend failed when usb_kill_urb */
992 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
995 usb_mark_last_busy(data->udev);
996 usb_anchor_urb(urb, &data->intr_anchor);
998 err = usb_submit_urb(urb, GFP_ATOMIC);
1000 /* -EPERM: urb is being killed;
1001 * -ENODEV: device got disconnected
1003 if (err != -EPERM && err != -ENODEV)
1004 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1007 hci_cmd_sync_cancel(hdev, -err);
1008 usb_unanchor_urb(urb);
1012 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
1014 struct btusb_data *data = hci_get_drvdata(hdev);
1020 BT_DBG("%s", hdev->name);
1025 urb = usb_alloc_urb(0, mem_flags);
1029 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
1031 buf = kmalloc(size, mem_flags);
1037 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
1039 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
1040 btusb_intr_complete, hdev, data->intr_ep->bInterval);
1042 urb->transfer_flags |= URB_FREE_BUFFER;
1044 usb_anchor_urb(urb, &data->intr_anchor);
1046 err = usb_submit_urb(urb, mem_flags);
1048 if (err != -EPERM && err != -ENODEV)
1049 bt_dev_err(hdev, "urb %p submission failed (%d)",
1052 hci_cmd_sync_cancel(hdev, -err);
1053 usb_unanchor_urb(urb);
1056 /* Only initialize intr_interval if URB poll sync is enabled */
1057 if (!data->poll_sync)
1060 /* The units are frames (milliseconds) for full and low speed devices,
1061 * and microframes (1/8 millisecond) for highspeed and SuperSpeed
1064 * This is done once on open/resume so it shouldn't change even if
1065 * force_poll_sync changes.
1067 switch (urb->dev->speed) {
1068 case USB_SPEED_SUPER_PLUS:
1069 case USB_SPEED_SUPER: /* units are 125us */
1070 data->intr_interval = usecs_to_jiffies(urb->interval * 125);
1073 data->intr_interval = msecs_to_jiffies(urb->interval);
1083 static void btusb_bulk_complete(struct urb *urb)
1085 struct hci_dev *hdev = urb->context;
1086 struct btusb_data *data = hci_get_drvdata(hdev);
1089 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1090 urb->actual_length);
1092 if (!test_bit(HCI_RUNNING, &hdev->flags))
1095 if (urb->status == 0) {
1096 hdev->stat.byte_rx += urb->actual_length;
1098 if (data->recv_bulk(data, urb->transfer_buffer,
1099 urb->actual_length) < 0) {
1100 bt_dev_err(hdev, "corrupted ACL packet");
1101 hdev->stat.err_rx++;
1103 } else if (urb->status == -ENOENT) {
1104 /* Avoid suspend failed when usb_kill_urb */
1108 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
1111 usb_anchor_urb(urb, &data->bulk_anchor);
1112 usb_mark_last_busy(data->udev);
1114 err = usb_submit_urb(urb, GFP_ATOMIC);
1116 /* -EPERM: urb is being killed;
1117 * -ENODEV: device got disconnected
1119 if (err != -EPERM && err != -ENODEV)
1120 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1122 usb_unanchor_urb(urb);
1126 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
1128 struct btusb_data *data = hci_get_drvdata(hdev);
1132 int err, size = HCI_MAX_FRAME_SIZE;
1134 BT_DBG("%s", hdev->name);
1136 if (!data->bulk_rx_ep)
1139 urb = usb_alloc_urb(0, mem_flags);
1143 buf = kmalloc(size, mem_flags);
1149 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1151 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1152 btusb_bulk_complete, hdev);
1154 urb->transfer_flags |= URB_FREE_BUFFER;
1156 usb_mark_last_busy(data->udev);
1157 usb_anchor_urb(urb, &data->bulk_anchor);
1159 err = usb_submit_urb(urb, mem_flags);
1161 if (err != -EPERM && err != -ENODEV)
1162 bt_dev_err(hdev, "urb %p submission failed (%d)",
1164 usb_unanchor_urb(urb);
1172 static void btusb_isoc_complete(struct urb *urb)
1174 struct hci_dev *hdev = urb->context;
1175 struct btusb_data *data = hci_get_drvdata(hdev);
1178 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1179 urb->actual_length);
1181 if (!test_bit(HCI_RUNNING, &hdev->flags))
1184 if (urb->status == 0) {
1185 for (i = 0; i < urb->number_of_packets; i++) {
1186 unsigned int offset = urb->iso_frame_desc[i].offset;
1187 unsigned int length = urb->iso_frame_desc[i].actual_length;
1189 if (urb->iso_frame_desc[i].status)
1192 hdev->stat.byte_rx += length;
1194 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1196 bt_dev_err(hdev, "corrupted SCO packet");
1197 hdev->stat.err_rx++;
1200 } else if (urb->status == -ENOENT) {
1201 /* Avoid suspend failed when usb_kill_urb */
1205 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1208 usb_anchor_urb(urb, &data->isoc_anchor);
1210 err = usb_submit_urb(urb, GFP_ATOMIC);
1212 /* -EPERM: urb is being killed;
1213 * -ENODEV: device got disconnected
1215 if (err != -EPERM && err != -ENODEV)
1216 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1218 usb_unanchor_urb(urb);
1222 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1223 int mtu, struct btusb_data *data)
1226 unsigned int interval;
1228 BT_DBG("len %d mtu %d", len, mtu);
1230 /* For mSBC ALT 6 setting the host will send the packet at continuous
1231 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1232 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1233 * To maintain the rate we send 63bytes of usb packets alternatively for
1234 * 7ms and 8ms to maintain the rate as 7.5ms.
1236 if (data->usb_alt6_packet_flow) {
1238 data->usb_alt6_packet_flow = false;
1241 data->usb_alt6_packet_flow = true;
1244 for (i = 0; i < interval; i++) {
1245 urb->iso_frame_desc[i].offset = offset;
1246 urb->iso_frame_desc[i].length = offset;
1249 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1250 urb->iso_frame_desc[i].offset = offset;
1251 urb->iso_frame_desc[i].length = len;
1255 urb->number_of_packets = i;
1258 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1262 BT_DBG("len %d mtu %d", len, mtu);
1264 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1265 i++, offset += mtu, len -= mtu) {
1266 urb->iso_frame_desc[i].offset = offset;
1267 urb->iso_frame_desc[i].length = mtu;
1270 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1271 urb->iso_frame_desc[i].offset = offset;
1272 urb->iso_frame_desc[i].length = len;
1276 urb->number_of_packets = i;
1279 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1281 struct btusb_data *data = hci_get_drvdata(hdev);
1287 BT_DBG("%s", hdev->name);
1289 if (!data->isoc_rx_ep)
1292 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1296 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1297 BTUSB_MAX_ISOC_FRAMES;
1299 buf = kmalloc(size, mem_flags);
1305 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1307 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1308 hdev, data->isoc_rx_ep->bInterval);
1310 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1312 __fill_isoc_descriptor(urb, size,
1313 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1315 usb_anchor_urb(urb, &data->isoc_anchor);
1317 err = usb_submit_urb(urb, mem_flags);
1319 if (err != -EPERM && err != -ENODEV)
1320 bt_dev_err(hdev, "urb %p submission failed (%d)",
1322 usb_unanchor_urb(urb);
1330 static void btusb_diag_complete(struct urb *urb)
1332 struct hci_dev *hdev = urb->context;
1333 struct btusb_data *data = hci_get_drvdata(hdev);
1336 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1337 urb->actual_length);
1339 if (urb->status == 0) {
1340 struct sk_buff *skb;
1342 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1344 skb_put_data(skb, urb->transfer_buffer,
1345 urb->actual_length);
1346 hci_recv_diag(hdev, skb);
1348 } else if (urb->status == -ENOENT) {
1349 /* Avoid suspend failed when usb_kill_urb */
1353 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1356 usb_anchor_urb(urb, &data->diag_anchor);
1357 usb_mark_last_busy(data->udev);
1359 err = usb_submit_urb(urb, GFP_ATOMIC);
1361 /* -EPERM: urb is being killed;
1362 * -ENODEV: device got disconnected
1364 if (err != -EPERM && err != -ENODEV)
1365 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1367 usb_unanchor_urb(urb);
1371 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1373 struct btusb_data *data = hci_get_drvdata(hdev);
1377 int err, size = HCI_MAX_FRAME_SIZE;
1379 BT_DBG("%s", hdev->name);
1381 if (!data->diag_rx_ep)
1384 urb = usb_alloc_urb(0, mem_flags);
1388 buf = kmalloc(size, mem_flags);
1394 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1396 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1397 btusb_diag_complete, hdev);
1399 urb->transfer_flags |= URB_FREE_BUFFER;
1401 usb_mark_last_busy(data->udev);
1402 usb_anchor_urb(urb, &data->diag_anchor);
1404 err = usb_submit_urb(urb, mem_flags);
1406 if (err != -EPERM && err != -ENODEV)
1407 bt_dev_err(hdev, "urb %p submission failed (%d)",
1409 usb_unanchor_urb(urb);
1417 static void btusb_tx_complete(struct urb *urb)
1419 struct sk_buff *skb = urb->context;
1420 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1421 struct btusb_data *data = hci_get_drvdata(hdev);
1422 unsigned long flags;
1424 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1425 urb->actual_length);
1427 if (!test_bit(HCI_RUNNING, &hdev->flags))
1431 hdev->stat.byte_tx += urb->transfer_buffer_length;
1433 if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)
1434 hci_cmd_sync_cancel(hdev, -urb->status);
1435 hdev->stat.err_tx++;
1439 spin_lock_irqsave(&data->txlock, flags);
1440 data->tx_in_flight--;
1441 spin_unlock_irqrestore(&data->txlock, flags);
1443 kfree(urb->setup_packet);
1448 static void btusb_isoc_tx_complete(struct urb *urb)
1450 struct sk_buff *skb = urb->context;
1451 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1453 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1454 urb->actual_length);
1456 if (!test_bit(HCI_RUNNING, &hdev->flags))
1460 hdev->stat.byte_tx += urb->transfer_buffer_length;
1462 hdev->stat.err_tx++;
1465 kfree(urb->setup_packet);
1470 static int btusb_open(struct hci_dev *hdev)
1472 struct btusb_data *data = hci_get_drvdata(hdev);
1475 BT_DBG("%s", hdev->name);
1477 err = usb_autopm_get_interface(data->intf);
1481 /* Patching USB firmware files prior to starting any URBs of HCI path
1482 * It is more safe to use USB bulk channel for downloading USB patch
1484 if (data->setup_on_usb) {
1485 err = data->setup_on_usb(hdev);
1490 data->intf->needs_remote_wakeup = 1;
1492 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1495 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1499 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1501 usb_kill_anchored_urbs(&data->intr_anchor);
1505 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1506 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1509 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1510 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1514 usb_autopm_put_interface(data->intf);
1518 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1520 usb_autopm_put_interface(data->intf);
1524 static void btusb_stop_traffic(struct btusb_data *data)
1526 usb_kill_anchored_urbs(&data->intr_anchor);
1527 usb_kill_anchored_urbs(&data->bulk_anchor);
1528 usb_kill_anchored_urbs(&data->isoc_anchor);
1529 usb_kill_anchored_urbs(&data->diag_anchor);
1530 usb_kill_anchored_urbs(&data->ctrl_anchor);
1533 static int btusb_close(struct hci_dev *hdev)
1535 struct btusb_data *data = hci_get_drvdata(hdev);
1538 BT_DBG("%s", hdev->name);
1540 cancel_delayed_work(&data->rx_work);
1541 cancel_work_sync(&data->work);
1542 cancel_work_sync(&data->waker);
1544 skb_queue_purge(&data->acl_q);
1546 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1547 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1548 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1549 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1551 btusb_stop_traffic(data);
1552 btusb_free_frags(data);
1554 err = usb_autopm_get_interface(data->intf);
1558 data->intf->needs_remote_wakeup = 0;
1560 /* Enable remote wake up for auto-suspend */
1561 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags))
1562 data->intf->needs_remote_wakeup = 1;
1564 usb_autopm_put_interface(data->intf);
1567 usb_scuttle_anchored_urbs(&data->deferred);
1571 static int btusb_flush(struct hci_dev *hdev)
1573 struct btusb_data *data = hci_get_drvdata(hdev);
1575 BT_DBG("%s", hdev->name);
1577 cancel_delayed_work(&data->rx_work);
1579 skb_queue_purge(&data->acl_q);
1581 usb_kill_anchored_urbs(&data->tx_anchor);
1582 btusb_free_frags(data);
1587 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1589 struct btusb_data *data = hci_get_drvdata(hdev);
1590 struct usb_ctrlrequest *dr;
1594 urb = usb_alloc_urb(0, GFP_KERNEL);
1596 return ERR_PTR(-ENOMEM);
1598 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1601 return ERR_PTR(-ENOMEM);
1604 dr->bRequestType = data->cmdreq_type;
1605 dr->bRequest = data->cmdreq;
1608 dr->wLength = __cpu_to_le16(skb->len);
1610 pipe = usb_sndctrlpipe(data->udev, 0x00);
1612 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1613 skb->data, skb->len, btusb_tx_complete, skb);
1615 skb->dev = (void *)hdev;
1620 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1622 struct btusb_data *data = hci_get_drvdata(hdev);
1626 if (!data->bulk_tx_ep)
1627 return ERR_PTR(-ENODEV);
1629 urb = usb_alloc_urb(0, GFP_KERNEL);
1631 return ERR_PTR(-ENOMEM);
1633 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1635 usb_fill_bulk_urb(urb, data->udev, pipe,
1636 skb->data, skb->len, btusb_tx_complete, skb);
1638 skb->dev = (void *)hdev;
1643 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1645 struct btusb_data *data = hci_get_drvdata(hdev);
1649 if (!data->isoc_tx_ep)
1650 return ERR_PTR(-ENODEV);
1652 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1654 return ERR_PTR(-ENOMEM);
1656 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1658 usb_fill_int_urb(urb, data->udev, pipe,
1659 skb->data, skb->len, btusb_isoc_tx_complete,
1660 skb, data->isoc_tx_ep->bInterval);
1662 urb->transfer_flags = URB_ISO_ASAP;
1664 if (data->isoc_altsetting == 6)
1665 __fill_isoc_descriptor_msbc(urb, skb->len,
1666 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1669 __fill_isoc_descriptor(urb, skb->len,
1670 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1671 skb->dev = (void *)hdev;
1676 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1678 struct btusb_data *data = hci_get_drvdata(hdev);
1681 usb_anchor_urb(urb, &data->tx_anchor);
1683 err = usb_submit_urb(urb, GFP_KERNEL);
1685 if (err != -EPERM && err != -ENODEV)
1686 bt_dev_err(hdev, "urb %p submission failed (%d)",
1688 kfree(urb->setup_packet);
1689 usb_unanchor_urb(urb);
1691 usb_mark_last_busy(data->udev);
1698 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1700 struct btusb_data *data = hci_get_drvdata(hdev);
1701 unsigned long flags;
1704 spin_lock_irqsave(&data->txlock, flags);
1705 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1707 data->tx_in_flight++;
1708 spin_unlock_irqrestore(&data->txlock, flags);
1711 return submit_tx_urb(hdev, urb);
1713 usb_anchor_urb(urb, &data->deferred);
1714 schedule_work(&data->waker);
1720 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1724 BT_DBG("%s", hdev->name);
1726 switch (hci_skb_pkt_type(skb)) {
1727 case HCI_COMMAND_PKT:
1728 urb = alloc_ctrl_urb(hdev, skb);
1730 return PTR_ERR(urb);
1732 hdev->stat.cmd_tx++;
1733 return submit_or_queue_tx_urb(hdev, urb);
1735 case HCI_ACLDATA_PKT:
1736 urb = alloc_bulk_urb(hdev, skb);
1738 return PTR_ERR(urb);
1740 hdev->stat.acl_tx++;
1741 return submit_or_queue_tx_urb(hdev, urb);
1743 case HCI_SCODATA_PKT:
1744 if (hci_conn_num(hdev, SCO_LINK) < 1)
1747 urb = alloc_isoc_urb(hdev, skb);
1749 return PTR_ERR(urb);
1751 hdev->stat.sco_tx++;
1752 return submit_tx_urb(hdev, urb);
1758 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1760 struct btusb_data *data = hci_get_drvdata(hdev);
1762 BT_DBG("%s evt %d", hdev->name, evt);
1764 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1765 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1766 data->air_mode = evt;
1767 schedule_work(&data->work);
1771 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1773 struct btusb_data *data = hci_get_drvdata(hdev);
1774 struct usb_interface *intf = data->isoc;
1775 struct usb_endpoint_descriptor *ep_desc;
1781 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
1783 bt_dev_err(hdev, "setting interface failed (%d)", -err);
1787 data->isoc_altsetting = altsetting;
1789 data->isoc_tx_ep = NULL;
1790 data->isoc_rx_ep = NULL;
1792 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1793 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1795 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1796 data->isoc_tx_ep = ep_desc;
1800 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1801 data->isoc_rx_ep = ep_desc;
1806 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1807 bt_dev_err(hdev, "invalid SCO descriptors");
1814 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1816 struct btusb_data *data = hci_get_drvdata(hdev);
1819 if (data->isoc_altsetting != new_alts) {
1820 unsigned long flags;
1822 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1823 usb_kill_anchored_urbs(&data->isoc_anchor);
1825 /* When isochronous alternate setting needs to be
1826 * changed, because SCO connection has been added
1827 * or removed, a packet fragment may be left in the
1828 * reassembling state. This could lead to wrongly
1829 * assembled fragments.
1831 * Clear outstanding fragment when selecting a new
1832 * alternate setting.
1834 spin_lock_irqsave(&data->rxlock, flags);
1835 kfree_skb(data->sco_skb);
1836 data->sco_skb = NULL;
1837 spin_unlock_irqrestore(&data->rxlock, flags);
1839 err = __set_isoc_interface(hdev, new_alts);
1844 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1845 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1846 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1848 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1854 static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1857 struct usb_interface *intf = data->isoc;
1860 BT_DBG("Looking for Alt no :%d", alt);
1865 for (i = 0; i < intf->num_altsetting; i++) {
1866 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1867 return &intf->altsetting[i];
1873 static void btusb_work(struct work_struct *work)
1875 struct btusb_data *data = container_of(work, struct btusb_data, work);
1876 struct hci_dev *hdev = data->hdev;
1880 if (data->sco_num > 0) {
1881 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1882 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1884 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1885 usb_kill_anchored_urbs(&data->isoc_anchor);
1889 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1892 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1893 if (hdev->voice_setting & 0x0020) {
1894 static const int alts[3] = { 2, 4, 5 };
1896 new_alts = alts[data->sco_num - 1];
1898 new_alts = data->sco_num;
1900 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
1901 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1902 * many adapters do not support it. Alt 1 appears to
1903 * work for all adapters that do not have alt 6, and
1904 * which work with WBS at all. Some devices prefer
1905 * alt 3 (HCI payload >= 60 Bytes let air packet
1906 * data satisfy 60 bytes), requiring
1907 * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
1908 * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
1910 if (btusb_find_altsetting(data, 6))
1912 else if (btusb_find_altsetting(data, 3) &&
1913 hdev->sco_mtu >= 72 &&
1914 test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
1920 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1921 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
1923 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1924 usb_kill_anchored_urbs(&data->isoc_anchor);
1926 __set_isoc_interface(hdev, 0);
1927 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1928 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1932 static void btusb_waker(struct work_struct *work)
1934 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1937 err = usb_autopm_get_interface(data->intf);
1941 usb_autopm_put_interface(data->intf);
1944 static void btusb_rx_work(struct work_struct *work)
1946 struct btusb_data *data = container_of(work, struct btusb_data,
1948 struct sk_buff *skb;
1950 /* Dequeue ACL data received during the interval */
1951 while ((skb = skb_dequeue(&data->acl_q)))
1952 data->recv_acl(data->hdev, skb);
1955 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1957 struct sk_buff *skb;
1960 BT_DBG("%s", hdev->name);
1962 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1964 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
1971 static int btusb_setup_csr(struct hci_dev *hdev)
1973 struct btusb_data *data = hci_get_drvdata(hdev);
1974 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
1975 struct hci_rp_read_local_version *rp;
1976 struct sk_buff *skb;
1977 bool is_fake = false;
1980 BT_DBG("%s", hdev->name);
1982 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1985 int err = PTR_ERR(skb);
1986 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
1990 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1991 bt_dev_err(hdev, "CSR: Local version length mismatch");
1996 rp = (struct hci_rp_read_local_version *)skb->data;
1998 /* Detect a wide host of Chinese controllers that aren't CSR.
2000 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
2002 * The main thing they have in common is that these are really popular low-cost
2003 * options that support newer Bluetooth versions but rely on heavy VID/PID
2004 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
2006 * We detect actual CSR devices by checking that the HCI manufacturer code
2007 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
2008 * HCI rev values always match. As they both store the firmware number.
2010 if (le16_to_cpu(rp->manufacturer) != 10 ||
2011 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
2014 /* Known legit CSR firmware build numbers and their supported BT versions:
2015 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
2016 * - 1.2 (0x2) -> 0x04d9, 0x0529
2017 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
2018 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
2019 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
2021 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
2022 * support BT 1.1 only; so it's a dead giveaway when some
2023 * third-party BT 4.0 dongle reuses it.
2025 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
2026 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
2029 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
2030 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
2033 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
2034 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
2037 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
2038 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
2041 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
2042 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
2045 /* Other clones which beat all the above checks */
2046 else if (bcdDevice == 0x0134 &&
2047 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
2048 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
2052 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
2054 /* Generally these clones have big discrepancies between
2055 * advertised features and what's actually supported.
2056 * Probably will need to be expanded in the future;
2057 * without these the controller will lock up.
2059 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2060 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
2061 set_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks);
2062 set_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks);
2064 /* Clear the reset quirk since this is not an actual
2065 * early Bluetooth 1.1 device from CSR.
2067 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2068 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2071 * Special workaround for these BT 4.0 chip clones, and potentially more:
2073 * - 0x0134: a Barrot 8041a02 (HCI rev: 0x0810 sub: 0x1012)
2074 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
2076 * These controllers are really messed-up.
2078 * 1. Their bulk RX endpoint will never report any data unless
2079 * the device was suspended at least once (yes, really).
2080 * 2. They will not wakeup when autosuspended and receiving data
2081 * on their bulk RX endpoint from e.g. a keyboard or mouse
2082 * (IOW remote-wakeup support is broken for the bulk endpoint).
2084 * To fix 1. enable runtime-suspend, force-suspend the
2085 * HCI and then wake-it up by disabling runtime-suspend.
2087 * To fix 2. clear the HCI's can_wake flag, this way the HCI
2088 * will still be autosuspended when it is not open.
2092 * Because these are widespread problems we prefer generic solutions; so
2093 * apply this initialization quirk to every controller that gets here,
2094 * it should be harmless. The alternative is to not work at all.
2096 pm_runtime_allow(&data->udev->dev);
2098 ret = pm_runtime_suspend(&data->udev->dev);
2102 bt_dev_warn(hdev, "CSR: Couldn't suspend the device for our Barrot 8041a02 receive-issue workaround");
2104 pm_runtime_forbid(&data->udev->dev);
2106 device_set_wakeup_capable(&data->udev->dev, false);
2108 /* Re-enable autosuspend if this was requested */
2109 if (enable_autosuspend)
2110 usb_enable_autosuspend(data->udev);
2118 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2120 struct sk_buff *skb;
2121 struct hci_event_hdr *hdr;
2122 struct hci_ev_cmd_complete *evt;
2124 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2128 hdr = skb_put(skb, sizeof(*hdr));
2129 hdr->evt = HCI_EV_CMD_COMPLETE;
2130 hdr->plen = sizeof(*evt) + 1;
2132 evt = skb_put(skb, sizeof(*evt));
2134 evt->opcode = cpu_to_le16(opcode);
2136 skb_put_u8(skb, 0x00);
2138 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2140 return hci_recv_frame(hdev, skb);
2143 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2146 struct hci_dev *hdev = data->hdev;
2148 /* When the device is in bootloader mode, then it can send
2149 * events via the bulk endpoint. These events are treated the
2150 * same way as the ones received from the interrupt endpoint.
2152 if (btintel_test_flag(hdev, INTEL_BOOTLOADER))
2153 return btusb_recv_intr(data, buffer, count);
2155 return btusb_recv_bulk(data, buffer, count);
2158 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2160 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2161 struct hci_event_hdr *hdr = (void *)skb->data;
2163 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2165 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2166 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
2168 switch (skb->data[2]) {
2170 /* When switching to the operational firmware
2171 * the device sends a vendor specific event
2172 * indicating that the bootup completed.
2174 btintel_bootup(hdev, ptr, len);
2177 /* When the firmware loading completes the
2178 * device sends out a vendor specific event
2179 * indicating the result of the firmware
2182 btintel_secure_send_result(hdev, ptr, len);
2188 return hci_recv_frame(hdev, skb);
2191 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2195 BT_DBG("%s", hdev->name);
2197 switch (hci_skb_pkt_type(skb)) {
2198 case HCI_COMMAND_PKT:
2199 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2200 struct hci_command_hdr *cmd = (void *)skb->data;
2201 __u16 opcode = le16_to_cpu(cmd->opcode);
2203 /* When in bootloader mode and the command 0xfc09
2204 * is received, it needs to be send down the
2205 * bulk endpoint. So allocate a bulk URB instead.
2207 if (opcode == 0xfc09)
2208 urb = alloc_bulk_urb(hdev, skb);
2210 urb = alloc_ctrl_urb(hdev, skb);
2212 /* When the 0xfc01 command is issued to boot into
2213 * the operational firmware, it will actually not
2214 * send a command complete event. To keep the flow
2215 * control working inject that event here.
2217 if (opcode == 0xfc01)
2218 inject_cmd_complete(hdev, opcode);
2220 urb = alloc_ctrl_urb(hdev, skb);
2223 return PTR_ERR(urb);
2225 hdev->stat.cmd_tx++;
2226 return submit_or_queue_tx_urb(hdev, urb);
2228 case HCI_ACLDATA_PKT:
2229 urb = alloc_bulk_urb(hdev, skb);
2231 return PTR_ERR(urb);
2233 hdev->stat.acl_tx++;
2234 return submit_or_queue_tx_urb(hdev, urb);
2236 case HCI_SCODATA_PKT:
2237 if (hci_conn_num(hdev, SCO_LINK) < 1)
2240 urb = alloc_isoc_urb(hdev, skb);
2242 return PTR_ERR(urb);
2244 hdev->stat.sco_tx++;
2245 return submit_tx_urb(hdev, urb);
2251 /* UHW CR mapping */
2252 #define MTK_BT_MISC 0x70002510
2253 #define MTK_BT_SUBSYS_RST 0x70002610
2254 #define MTK_UDMA_INT_STA_BT 0x74000024
2255 #define MTK_UDMA_INT_STA_BT1 0x74000308
2256 #define MTK_BT_WDT_STATUS 0x740003A0
2257 #define MTK_EP_RST_OPT 0x74011890
2258 #define MTK_EP_RST_IN_OUT_OPT 0x00010001
2259 #define MTK_BT_RST_DONE 0x00000100
2260 #define MTK_BT_RESET_WAIT_MS 100
2261 #define MTK_BT_RESET_NUM_TRIES 10
2263 static void btusb_mtk_wmt_recv(struct urb *urb)
2265 struct hci_dev *hdev = urb->context;
2266 struct btusb_data *data = hci_get_drvdata(hdev);
2267 struct sk_buff *skb;
2270 if (urb->status == 0 && urb->actual_length > 0) {
2271 hdev->stat.byte_rx += urb->actual_length;
2273 /* WMT event shouldn't be fragmented and the size should be
2274 * less than HCI_WMT_MAX_EVENT_SIZE.
2276 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2278 hdev->stat.err_rx++;
2279 kfree(urb->setup_packet);
2283 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2284 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2286 /* When someone waits for the WMT event, the skb is being cloned
2287 * and being processed the events from there then.
2289 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
2290 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
2291 if (!data->evt_skb) {
2293 kfree(urb->setup_packet);
2298 err = hci_recv_frame(hdev, skb);
2300 kfree_skb(data->evt_skb);
2301 data->evt_skb = NULL;
2302 kfree(urb->setup_packet);
2306 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2308 /* Barrier to sync with other CPUs */
2309 smp_mb__after_atomic();
2310 wake_up_bit(&data->flags,
2311 BTUSB_TX_WAIT_VND_EVT);
2313 kfree(urb->setup_packet);
2315 } else if (urb->status == -ENOENT) {
2316 /* Avoid suspend failed when usb_kill_urb */
2320 usb_mark_last_busy(data->udev);
2322 /* The URB complete handler is still called with urb->actual_length = 0
2323 * when the event is not available, so we should keep re-submitting
2324 * URB until WMT event returns, Also, It's necessary to wait some time
2325 * between the two consecutive control URBs to relax the target device
2326 * to generate the event. Otherwise, the WMT event cannot return from
2327 * the device successfully.
2331 usb_anchor_urb(urb, &data->ctrl_anchor);
2332 err = usb_submit_urb(urb, GFP_ATOMIC);
2334 kfree(urb->setup_packet);
2335 /* -EPERM: urb is being killed;
2336 * -ENODEV: device got disconnected
2338 if (err != -EPERM && err != -ENODEV)
2339 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2341 usb_unanchor_urb(urb);
2345 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2347 struct btusb_data *data = hci_get_drvdata(hdev);
2348 struct usb_ctrlrequest *dr;
2354 urb = usb_alloc_urb(0, GFP_KERNEL);
2358 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2364 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2366 dr->wIndex = cpu_to_le16(0);
2367 dr->wValue = cpu_to_le16(48);
2368 dr->wLength = cpu_to_le16(size);
2370 buf = kmalloc(size, GFP_KERNEL);
2377 pipe = usb_rcvctrlpipe(data->udev, 0);
2379 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2380 buf, size, btusb_mtk_wmt_recv, hdev);
2382 urb->transfer_flags |= URB_FREE_BUFFER;
2384 usb_anchor_urb(urb, &data->ctrl_anchor);
2385 err = usb_submit_urb(urb, GFP_KERNEL);
2387 if (err != -EPERM && err != -ENODEV)
2388 bt_dev_err(hdev, "urb %p submission failed (%d)",
2390 usb_unanchor_urb(urb);
2398 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
2399 struct btmtk_hci_wmt_params *wmt_params)
2401 struct btusb_data *data = hci_get_drvdata(hdev);
2402 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
2403 u32 hlen, status = BTMTK_WMT_INVALID;
2404 struct btmtk_hci_wmt_evt *wmt_evt;
2405 struct btmtk_hci_wmt_cmd *wc;
2406 struct btmtk_wmt_hdr *hdr;
2409 /* Send the WMT command and wait until the WMT event returns */
2410 hlen = sizeof(*hdr) + wmt_params->dlen;
2414 wc = kzalloc(hlen, GFP_KERNEL);
2420 hdr->op = wmt_params->op;
2421 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
2422 hdr->flag = wmt_params->flag;
2423 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
2425 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2427 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
2430 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2434 /* Submit control IN URB on demand to process the WMT event */
2435 err = btusb_mtk_submit_wmt_recv_urb(hdev);
2439 /* The vendor specific WMT commands are all answered by a vendor
2440 * specific event and will have the Command Status or Command
2441 * Complete as with usual HCI command flow control.
2443 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
2444 * state to be cleared. The driver specific event receive routine
2445 * will clear that state and with that indicate completion of the
2448 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
2449 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
2450 if (err == -EINTR) {
2451 bt_dev_err(hdev, "Execution of wmt command interrupted");
2452 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2457 bt_dev_err(hdev, "Execution of wmt command timed out");
2458 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2463 /* Parse and handle the return WMT event */
2464 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
2465 if (wmt_evt->whdr.op != hdr->op) {
2466 bt_dev_err(hdev, "Wrong op received %d expected %d",
2467 wmt_evt->whdr.op, hdr->op);
2472 switch (wmt_evt->whdr.op) {
2473 case BTMTK_WMT_SEMAPHORE:
2474 if (wmt_evt->whdr.flag == 2)
2475 status = BTMTK_WMT_PATCH_UNDONE;
2477 status = BTMTK_WMT_PATCH_DONE;
2479 case BTMTK_WMT_FUNC_CTRL:
2480 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
2481 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
2482 status = BTMTK_WMT_ON_DONE;
2483 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
2484 status = BTMTK_WMT_ON_PROGRESS;
2486 status = BTMTK_WMT_ON_UNDONE;
2488 case BTMTK_WMT_PATCH_DWNLD:
2489 if (wmt_evt->whdr.flag == 2)
2490 status = BTMTK_WMT_PATCH_DONE;
2491 else if (wmt_evt->whdr.flag == 1)
2492 status = BTMTK_WMT_PATCH_PROGRESS;
2494 status = BTMTK_WMT_PATCH_UNDONE;
2498 if (wmt_params->status)
2499 *wmt_params->status = status;
2502 kfree_skb(data->evt_skb);
2503 data->evt_skb = NULL;
2509 static int btusb_mtk_func_query(struct hci_dev *hdev)
2511 struct btmtk_hci_wmt_params wmt_params;
2515 /* Query whether the function is enabled */
2516 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2517 wmt_params.flag = 4;
2518 wmt_params.dlen = sizeof(param);
2519 wmt_params.data = ¶m;
2520 wmt_params.status = &status;
2522 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2524 bt_dev_err(hdev, "Failed to query function status (%d)", err);
2531 static int btusb_mtk_uhw_reg_write(struct btusb_data *data, u32 reg, u32 val)
2533 struct hci_dev *hdev = data->hdev;
2537 buf = kzalloc(4, GFP_KERNEL);
2541 put_unaligned_le32(val, buf);
2543 pipe = usb_sndctrlpipe(data->udev, 0);
2544 err = usb_control_msg(data->udev, pipe, 0x02,
2546 reg >> 16, reg & 0xffff,
2547 buf, 4, USB_CTRL_SET_TIMEOUT);
2549 bt_dev_err(hdev, "Failed to write uhw reg(%d)", err);
2559 static int btusb_mtk_uhw_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2561 struct hci_dev *hdev = data->hdev;
2565 buf = kzalloc(4, GFP_KERNEL);
2569 pipe = usb_rcvctrlpipe(data->udev, 0);
2570 err = usb_control_msg(data->udev, pipe, 0x01,
2572 reg >> 16, reg & 0xffff,
2573 buf, 4, USB_CTRL_SET_TIMEOUT);
2575 bt_dev_err(hdev, "Failed to read uhw reg(%d)", err);
2579 *val = get_unaligned_le32(buf);
2580 bt_dev_dbg(hdev, "reg=%x, value=0x%08x", reg, *val);
2588 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2590 int pipe, err, size = sizeof(u32);
2593 buf = kzalloc(size, GFP_KERNEL);
2597 pipe = usb_rcvctrlpipe(data->udev, 0);
2598 err = usb_control_msg(data->udev, pipe, 0x63,
2599 USB_TYPE_VENDOR | USB_DIR_IN,
2600 reg >> 16, reg & 0xffff,
2601 buf, size, USB_CTRL_SET_TIMEOUT);
2605 *val = get_unaligned_le32(buf);
2613 static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
2615 return btusb_mtk_reg_read(data, reg, id);
2618 static int btusb_mtk_setup(struct hci_dev *hdev)
2620 struct btusb_data *data = hci_get_drvdata(hdev);
2621 struct btmtk_hci_wmt_params wmt_params;
2622 ktime_t calltime, delta, rettime;
2623 struct btmtk_tci_sleep tci_sleep;
2624 unsigned long long duration;
2625 struct sk_buff *skb;
2629 char fw_bin_name[64];
2633 calltime = ktime_get();
2635 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
2637 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2642 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
2644 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2647 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
2649 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
2656 fwname = FIRMWARE_MT7663;
2659 fwname = FIRMWARE_MT7668;
2663 snprintf(fw_bin_name, sizeof(fw_bin_name),
2664 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
2665 dev_id & 0xffff, (fw_version & 0xff) + 1);
2666 err = btmtk_setup_firmware_79xx(hdev, fw_bin_name,
2667 btusb_mtk_hci_wmt_sync);
2669 bt_dev_err(hdev, "Failed to set up firmware (%d)", err);
2673 /* It's Device EndPoint Reset Option Register */
2674 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2676 /* Enable Bluetooth protocol */
2678 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2679 wmt_params.flag = 0;
2680 wmt_params.dlen = sizeof(param);
2681 wmt_params.data = ¶m;
2682 wmt_params.status = NULL;
2684 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2686 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2690 hci_set_msft_opcode(hdev, 0xFD30);
2691 hci_set_aosp_capable(hdev);
2694 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
2699 /* Query whether the firmware is already download */
2700 wmt_params.op = BTMTK_WMT_SEMAPHORE;
2701 wmt_params.flag = 1;
2702 wmt_params.dlen = 0;
2703 wmt_params.data = NULL;
2704 wmt_params.status = &status;
2706 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2708 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
2712 if (status == BTMTK_WMT_PATCH_DONE) {
2713 bt_dev_info(hdev, "firmware already downloaded");
2714 goto ignore_setup_fw;
2717 /* Setup a firmware which the device definitely requires */
2718 err = btmtk_setup_firmware(hdev, fwname,
2719 btusb_mtk_hci_wmt_sync);
2724 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
2725 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
2727 /* -ETIMEDOUT happens */
2731 /* The other errors happen in btusb_mtk_func_query */
2735 if (status == BTMTK_WMT_ON_DONE) {
2736 bt_dev_info(hdev, "function already on");
2737 goto ignore_func_on;
2740 /* Enable Bluetooth protocol */
2742 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2743 wmt_params.flag = 0;
2744 wmt_params.dlen = sizeof(param);
2745 wmt_params.data = ¶m;
2746 wmt_params.status = NULL;
2748 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2750 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2755 /* Apply the low power environment setup */
2756 tci_sleep.mode = 0x5;
2757 tci_sleep.duration = cpu_to_le16(0x640);
2758 tci_sleep.host_duration = cpu_to_le16(0x640);
2759 tci_sleep.host_wakeup_pin = 0;
2760 tci_sleep.time_compensation = 0;
2762 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
2766 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
2772 rettime = ktime_get();
2773 delta = ktime_sub(rettime, calltime);
2774 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2776 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
2781 static int btusb_mtk_shutdown(struct hci_dev *hdev)
2783 struct btmtk_hci_wmt_params wmt_params;
2787 /* Disable the device */
2788 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2789 wmt_params.flag = 0;
2790 wmt_params.dlen = sizeof(param);
2791 wmt_params.data = ¶m;
2792 wmt_params.status = NULL;
2794 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2796 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2803 static void btusb_mtk_cmd_timeout(struct hci_dev *hdev)
2805 struct btusb_data *data = hci_get_drvdata(hdev);
2809 /* It's MediaTek specific bluetooth reset mechanism via USB */
2810 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
2811 bt_dev_err(hdev, "last reset failed? Not resetting again");
2815 err = usb_autopm_get_interface(data->intf);
2819 btusb_stop_traffic(data);
2820 usb_kill_anchored_urbs(&data->tx_anchor);
2822 /* It's Device EndPoint Reset Option Register */
2823 bt_dev_dbg(hdev, "Initiating reset mechanism via uhw");
2824 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2825 btusb_mtk_uhw_reg_read(data, MTK_BT_WDT_STATUS, &val);
2827 /* Reset the bluetooth chip via USB interface. */
2828 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 1);
2829 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT, 0x000000FF);
2830 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT, &val);
2831 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT1, 0x000000FF);
2832 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT1, &val);
2833 /* MT7921 need to delay 20ms between toggle reset bit */
2835 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 0);
2836 btusb_mtk_uhw_reg_read(data, MTK_BT_SUBSYS_RST, &val);
2838 /* Poll the register until reset is completed */
2840 btusb_mtk_uhw_reg_read(data, MTK_BT_MISC, &val);
2841 if (val & MTK_BT_RST_DONE) {
2842 bt_dev_dbg(hdev, "Bluetooth Reset Successfully");
2846 bt_dev_dbg(hdev, "Polling Bluetooth Reset CR");
2848 msleep(MTK_BT_RESET_WAIT_MS);
2849 } while (retry < MTK_BT_RESET_NUM_TRIES);
2851 btusb_mtk_id_get(data, 0x70010200, &val);
2853 bt_dev_err(hdev, "Can't get device id, subsys reset fail.");
2855 usb_queue_reset_device(data->intf);
2857 clear_bit(BTUSB_HW_RESET_ACTIVE, &data->flags);
2860 static int btusb_recv_acl_mtk(struct hci_dev *hdev, struct sk_buff *skb)
2862 struct btusb_data *data = hci_get_drvdata(hdev);
2863 u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
2866 case 0xfc6f: /* Firmware dump from device */
2867 /* When the firmware hangs, the device can no longer
2868 * suspend and thus disable auto-suspend.
2870 usb_disable_autosuspend(data->udev);
2872 case 0x05ff: /* Firmware debug logging 1 */
2873 case 0x05fe: /* Firmware debug logging 2 */
2874 return hci_recv_diag(hdev, skb);
2877 return hci_recv_frame(hdev, skb);
2881 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
2882 static int marvell_config_oob_wake(struct hci_dev *hdev)
2884 struct sk_buff *skb;
2885 struct btusb_data *data = hci_get_drvdata(hdev);
2886 struct device *dev = &data->udev->dev;
2887 u16 pin, gap, opcode;
2891 /* Move on if no wakeup pin specified */
2892 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
2893 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
2896 /* Vendor specific command to configure a GPIO as wake-up pin */
2897 opcode = hci_opcode_pack(0x3F, 0x59);
2898 cmd[0] = opcode & 0xFF;
2899 cmd[1] = opcode >> 8;
2900 cmd[2] = 2; /* length of parameters that follow */
2902 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
2904 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
2906 bt_dev_err(hdev, "%s: No memory", __func__);
2910 skb_put_data(skb, cmd, sizeof(cmd));
2911 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
2913 ret = btusb_send_frame(hdev, skb);
2915 bt_dev_err(hdev, "%s: configuration failed", __func__);
2924 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2925 const bdaddr_t *bdaddr)
2927 struct sk_buff *skb;
2932 buf[1] = sizeof(bdaddr_t);
2933 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2935 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2938 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
2947 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2948 const bdaddr_t *bdaddr)
2950 struct sk_buff *skb;
2957 buf[3] = sizeof(bdaddr_t);
2958 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2960 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2963 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
2971 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
2972 const bdaddr_t *bdaddr)
2974 struct sk_buff *skb;
2978 memcpy(buf, bdaddr, sizeof(bdaddr_t));
2980 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
2981 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
2984 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
2992 #define QCA_DFU_PACKET_LEN 4096
2994 #define QCA_GET_TARGET_VERSION 0x09
2995 #define QCA_CHECK_STATUS 0x05
2996 #define QCA_DFU_DOWNLOAD 0x01
2998 #define QCA_SYSCFG_UPDATED 0x40
2999 #define QCA_PATCH_UPDATED 0x80
3000 #define QCA_DFU_TIMEOUT 3000
3001 #define QCA_FLAG_MULTI_NVM 0x80
3002 #define QCA_BT_RESET_WAIT_MS 100
3004 #define WCN6855_2_0_RAM_VERSION_GF 0x400c1200
3005 #define WCN6855_2_1_RAM_VERSION_GF 0x400c1211
3007 struct qca_version {
3009 __le32 patch_version;
3017 struct qca_rampatch_version {
3018 __le16 rom_version_high;
3019 __le16 rom_version_low;
3020 __le16 patch_version;
3023 struct qca_device_info {
3025 u8 rampatch_hdr; /* length of header in rampatch */
3026 u8 nvm_hdr; /* length of header in NVM */
3027 u8 ver_offset; /* offset of version structure in rampatch */
3030 static const struct qca_device_info qca_devices_table[] = {
3031 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3032 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3033 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3034 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3035 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3036 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3037 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3038 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3039 { 0x00130201, 40, 4, 16 }, /* WCN6855 2.1 */
3042 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3043 void *data, u16 size)
3048 buf = kmalloc(size, GFP_KERNEL);
3052 /* Found some of USB hosts have IOT issues with ours so that we should
3053 * not wait until HCI layer is ready.
3055 pipe = usb_rcvctrlpipe(udev, 0);
3056 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3057 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3059 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3063 memcpy(data, buf, size);
3071 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3072 const struct firmware *firmware,
3075 struct btusb_data *btdata = hci_get_drvdata(hdev);
3076 struct usb_device *udev = btdata->udev;
3077 size_t count, size, sent = 0;
3081 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3085 count = firmware->size;
3087 size = min_t(size_t, count, hdr_size);
3088 memcpy(buf, firmware->data, size);
3090 /* USB patches should go down to controller through USB path
3091 * because binary format fits to go down through USB channel.
3092 * USB control path is for patching headers and USB bulk is for
3095 pipe = usb_sndctrlpipe(udev, 0);
3096 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3097 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3099 bt_dev_err(hdev, "Failed to send headers (%d)", err);
3106 /* ep2 need time to switch from function acl to function dfu,
3107 * so we add 20ms delay here.
3112 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3114 memcpy(buf, firmware->data + sent, size);
3116 pipe = usb_sndbulkpipe(udev, 0x02);
3117 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3120 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3121 sent, firmware->size, err);
3126 bt_dev_err(hdev, "Failed to get bulk buffer");
3140 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3141 struct qca_version *ver,
3142 const struct qca_device_info *info)
3144 struct qca_rampatch_version *rver;
3145 const struct firmware *fw;
3146 u32 ver_rom, ver_patch, rver_rom;
3147 u16 rver_rom_low, rver_rom_high, rver_patch;
3151 ver_rom = le32_to_cpu(ver->rom_version);
3152 ver_patch = le32_to_cpu(ver->patch_version);
3154 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
3156 err = request_firmware(&fw, fwname, &hdev->dev);
3158 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3163 bt_dev_info(hdev, "using rampatch file: %s", fwname);
3165 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3166 rver_rom_low = le16_to_cpu(rver->rom_version_low);
3167 rver_patch = le16_to_cpu(rver->patch_version);
3169 if (ver_rom & ~0xffffU) {
3170 rver_rom_high = le16_to_cpu(rver->rom_version_high);
3171 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
3173 rver_rom = rver_rom_low;
3176 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3177 "firmware rome 0x%x build 0x%x",
3178 rver_rom, rver_patch, ver_rom, ver_patch);
3180 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
3181 bt_dev_err(hdev, "rampatch file version did not match with firmware");
3186 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3189 release_firmware(fw);
3194 static void btusb_generate_qca_nvm_name(char *fwname, size_t max_size,
3195 const struct qca_version *ver)
3197 u32 rom_version = le32_to_cpu(ver->rom_version);
3198 u16 flag = le16_to_cpu(ver->flag);
3200 if (((flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
3201 /* The board_id should be split into two bytes
3202 * The 1st byte is chip ID, and the 2nd byte is platform ID
3203 * For example, board ID 0x010A, 0x01 is platform ID. 0x0A is chip ID
3204 * we have several platforms, and platform IDs are continuously added
3206 * 0x00 is for Mobile
3208 * 0x02 is for Automotive
3209 * 0x03 is for Consumer electronic
3211 u16 board_id = (ver->chip_id << 8) + ver->platform_id;
3212 const char *variant;
3214 switch (le32_to_cpu(ver->ram_version)) {
3215 case WCN6855_2_0_RAM_VERSION_GF:
3216 case WCN6855_2_1_RAM_VERSION_GF:
3224 if (board_id == 0) {
3225 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s.bin",
3226 rom_version, variant);
3228 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s_%04x.bin",
3229 rom_version, variant, board_id);
3232 snprintf(fwname, max_size, "qca/nvm_usb_%08x.bin",
3238 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3239 struct qca_version *ver,
3240 const struct qca_device_info *info)
3242 const struct firmware *fw;
3246 btusb_generate_qca_nvm_name(fwname, sizeof(fwname), ver);
3248 err = request_firmware(&fw, fwname, &hdev->dev);
3250 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3255 bt_dev_info(hdev, "using NVM file: %s", fwname);
3257 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3259 release_firmware(fw);
3264 /* identify the ROM version and check whether patches are needed */
3265 static bool btusb_qca_need_patch(struct usb_device *udev)
3267 struct qca_version ver;
3269 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3272 /* only low ROM versions need patches */
3273 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3276 static int btusb_setup_qca(struct hci_dev *hdev)
3278 struct btusb_data *btdata = hci_get_drvdata(hdev);
3279 struct usb_device *udev = btdata->udev;
3280 const struct qca_device_info *info = NULL;
3281 struct qca_version ver;
3286 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3291 ver_rom = le32_to_cpu(ver.rom_version);
3293 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
3294 if (ver_rom == qca_devices_table[i].rom_version)
3295 info = &qca_devices_table[i];
3298 /* If the rom_version is not matched in the qca_devices_table
3299 * and the high ROM version is not zero, we assume this chip no
3300 * need to load the rampatch and nvm.
3302 if (ver_rom & ~0xffffU)
3305 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3309 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3314 if (!(status & QCA_PATCH_UPDATED)) {
3315 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3320 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3325 if (!(status & QCA_SYSCFG_UPDATED)) {
3326 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3330 /* WCN6855 2.1 will reset to apply firmware downloaded here, so
3331 * wait ~100ms for reset Done then go ahead, otherwise, it maybe
3332 * cause potential enable failure.
3334 if (info->rom_version == 0x00130201)
3335 msleep(QCA_BT_RESET_WAIT_MS);
3341 static inline int __set_diag_interface(struct hci_dev *hdev)
3343 struct btusb_data *data = hci_get_drvdata(hdev);
3344 struct usb_interface *intf = data->diag;
3350 data->diag_tx_ep = NULL;
3351 data->diag_rx_ep = NULL;
3353 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3354 struct usb_endpoint_descriptor *ep_desc;
3356 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3358 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3359 data->diag_tx_ep = ep_desc;
3363 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3364 data->diag_rx_ep = ep_desc;
3369 if (!data->diag_tx_ep || !data->diag_rx_ep) {
3370 bt_dev_err(hdev, "invalid diagnostic descriptors");
3377 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3379 struct btusb_data *data = hci_get_drvdata(hdev);
3380 struct sk_buff *skb;
3384 if (!data->diag_tx_ep)
3385 return ERR_PTR(-ENODEV);
3387 urb = usb_alloc_urb(0, GFP_KERNEL);
3389 return ERR_PTR(-ENOMEM);
3391 skb = bt_skb_alloc(2, GFP_KERNEL);
3394 return ERR_PTR(-ENOMEM);
3397 skb_put_u8(skb, 0xf0);
3398 skb_put_u8(skb, enable);
3400 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3402 usb_fill_bulk_urb(urb, data->udev, pipe,
3403 skb->data, skb->len, btusb_tx_complete, skb);
3405 skb->dev = (void *)hdev;
3410 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3412 struct btusb_data *data = hci_get_drvdata(hdev);
3418 if (!test_bit(HCI_RUNNING, &hdev->flags))
3421 urb = alloc_diag_urb(hdev, enable);
3423 return PTR_ERR(urb);
3425 return submit_or_queue_tx_urb(hdev, urb);
3429 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3431 struct btusb_data *data = priv;
3433 pm_wakeup_event(&data->udev->dev, 0);
3436 /* Disable only if not already disabled (keep it balanced) */
3437 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3438 disable_irq_nosync(irq);
3439 disable_irq_wake(irq);
3444 static const struct of_device_id btusb_match_table[] = {
3445 { .compatible = "usb1286,204e" },
3446 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3447 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
3450 MODULE_DEVICE_TABLE(of, btusb_match_table);
3452 /* Use an oob wakeup pin? */
3453 static int btusb_config_oob_wake(struct hci_dev *hdev)
3455 struct btusb_data *data = hci_get_drvdata(hdev);
3456 struct device *dev = &data->udev->dev;
3459 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3461 if (!of_match_device(btusb_match_table, dev))
3464 /* Move on if no IRQ specified */
3465 irq = of_irq_get_byname(dev->of_node, "wakeup");
3467 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
3471 irq_set_status_flags(irq, IRQ_NOAUTOEN);
3472 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
3473 0, "OOB Wake-on-BT", data);
3475 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
3479 ret = device_init_wakeup(dev, true);
3481 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
3485 data->oob_wake_irq = irq;
3486 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
3491 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
3493 if (dmi_check_system(btusb_needs_reset_resume_table))
3494 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3497 static bool btusb_wakeup(struct hci_dev *hdev)
3499 struct btusb_data *data = hci_get_drvdata(hdev);
3501 return device_may_wakeup(&data->udev->dev);
3504 static int btusb_shutdown_qca(struct hci_dev *hdev)
3506 struct sk_buff *skb;
3508 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3510 bt_dev_err(hdev, "HCI reset during shutdown failed");
3511 return PTR_ERR(skb);
3518 static ssize_t force_poll_sync_read(struct file *file, char __user *user_buf,
3519 size_t count, loff_t *ppos)
3521 struct btusb_data *data = file->private_data;
3524 buf[0] = data->poll_sync ? 'Y' : 'N';
3527 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3530 static ssize_t force_poll_sync_write(struct file *file,
3531 const char __user *user_buf,
3532 size_t count, loff_t *ppos)
3534 struct btusb_data *data = file->private_data;
3538 err = kstrtobool_from_user(user_buf, count, &enable);
3542 /* Only allow changes while the adapter is down */
3543 if (test_bit(HCI_UP, &data->hdev->flags))
3546 if (data->poll_sync == enable)
3549 data->poll_sync = enable;
3554 static const struct file_operations force_poll_sync_fops = {
3555 .open = simple_open,
3556 .read = force_poll_sync_read,
3557 .write = force_poll_sync_write,
3558 .llseek = default_llseek,
3561 static int btusb_probe(struct usb_interface *intf,
3562 const struct usb_device_id *id)
3564 struct usb_endpoint_descriptor *ep_desc;
3565 struct gpio_desc *reset_gpio;
3566 struct btusb_data *data;
3567 struct hci_dev *hdev;
3568 unsigned ifnum_base;
3569 int i, err, priv_size;
3571 BT_DBG("intf %p id %p", intf, id);
3573 /* interface numbers are hardcoded in the spec */
3574 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
3575 if (!(id->driver_info & BTUSB_IFNUM_2))
3577 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
3581 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
3583 if (!id->driver_info) {
3584 const struct usb_device_id *match;
3586 match = usb_match_id(intf, blacklist_table);
3591 if (id->driver_info == BTUSB_IGNORE)
3594 if (id->driver_info & BTUSB_ATH3012) {
3595 struct usb_device *udev = interface_to_usbdev(intf);
3597 /* Old firmware would otherwise let ath3k driver load
3598 * patch and sysconfig files
3600 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
3601 !btusb_qca_need_patch(udev))
3605 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
3609 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3610 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3612 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3613 data->intr_ep = ep_desc;
3617 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3618 data->bulk_tx_ep = ep_desc;
3622 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3623 data->bulk_rx_ep = ep_desc;
3628 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
3631 if (id->driver_info & BTUSB_AMP) {
3632 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3633 data->cmdreq = 0x2b;
3635 data->cmdreq_type = USB_TYPE_CLASS;
3636 data->cmdreq = 0x00;
3639 data->udev = interface_to_usbdev(intf);
3642 INIT_WORK(&data->work, btusb_work);
3643 INIT_WORK(&data->waker, btusb_waker);
3644 INIT_DELAYED_WORK(&data->rx_work, btusb_rx_work);
3646 skb_queue_head_init(&data->acl_q);
3648 init_usb_anchor(&data->deferred);
3649 init_usb_anchor(&data->tx_anchor);
3650 spin_lock_init(&data->txlock);
3652 init_usb_anchor(&data->intr_anchor);
3653 init_usb_anchor(&data->bulk_anchor);
3654 init_usb_anchor(&data->isoc_anchor);
3655 init_usb_anchor(&data->diag_anchor);
3656 init_usb_anchor(&data->ctrl_anchor);
3657 spin_lock_init(&data->rxlock);
3661 data->recv_event = hci_recv_frame;
3662 data->recv_bulk = btusb_recv_bulk;
3664 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3665 /* Allocate extra space for Intel device */
3666 priv_size += sizeof(struct btintel_data);
3668 /* Override the rx handlers */
3669 data->recv_event = btusb_recv_event_intel;
3670 data->recv_bulk = btusb_recv_bulk_intel;
3673 data->recv_acl = hci_recv_frame;
3675 hdev = hci_alloc_dev_priv(priv_size);
3679 hdev->bus = HCI_USB;
3680 hci_set_drvdata(hdev, data);
3682 if (id->driver_info & BTUSB_AMP)
3683 hdev->dev_type = HCI_AMP;
3685 hdev->dev_type = HCI_PRIMARY;
3689 SET_HCIDEV_DEV(hdev, &intf->dev);
3691 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
3693 if (IS_ERR(reset_gpio)) {
3694 err = PTR_ERR(reset_gpio);
3696 } else if (reset_gpio) {
3697 data->reset_gpio = reset_gpio;
3700 hdev->open = btusb_open;
3701 hdev->close = btusb_close;
3702 hdev->flush = btusb_flush;
3703 hdev->send = btusb_send_frame;
3704 hdev->notify = btusb_notify;
3705 hdev->wakeup = btusb_wakeup;
3708 err = btusb_config_oob_wake(hdev);
3712 /* Marvell devices may need a specific chip configuration */
3713 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
3714 err = marvell_config_oob_wake(hdev);
3719 if (id->driver_info & BTUSB_CW6622)
3720 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3722 if (id->driver_info & BTUSB_BCM2045)
3723 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3725 if (id->driver_info & BTUSB_BCM92035)
3726 hdev->setup = btusb_setup_bcm92035;
3728 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3729 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
3730 hdev->manufacturer = 15;
3731 hdev->setup = btbcm_setup_patchram;
3732 hdev->set_diag = btusb_bcm_set_diag;
3733 hdev->set_bdaddr = btbcm_set_bdaddr;
3735 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3736 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3739 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3740 (id->driver_info & BTUSB_BCM_APPLE)) {
3741 hdev->manufacturer = 15;
3742 hdev->setup = btbcm_setup_apple;
3743 hdev->set_diag = btusb_bcm_set_diag;
3745 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3746 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3749 /* Combined Intel Device setup to support multiple setup routine */
3750 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3751 err = btintel_configure_setup(hdev);
3755 /* Transport specific configuration */
3756 hdev->send = btusb_send_frame_intel;
3757 hdev->cmd_timeout = btusb_intel_cmd_timeout;
3759 if (id->driver_info & BTUSB_INTEL_NO_WBS_SUPPORT)
3760 btintel_set_flag(hdev, INTEL_ROM_LEGACY_NO_WBS_SUPPORT);
3762 if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
3763 btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
3765 if (id->driver_info & BTUSB_INTEL_BROKEN_SHUTDOWN_LED)
3766 btintel_set_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED);
3769 if (id->driver_info & BTUSB_MARVELL)
3770 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3772 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
3773 (id->driver_info & BTUSB_MEDIATEK)) {
3774 hdev->setup = btusb_mtk_setup;
3775 hdev->shutdown = btusb_mtk_shutdown;
3776 hdev->manufacturer = 70;
3777 hdev->cmd_timeout = btusb_mtk_cmd_timeout;
3778 hdev->set_bdaddr = btmtk_set_bdaddr;
3779 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
3780 data->recv_acl = btusb_recv_acl_mtk;
3783 if (id->driver_info & BTUSB_SWAVE) {
3784 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
3785 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
3788 if (id->driver_info & BTUSB_INTEL_BOOT) {
3789 hdev->manufacturer = 2;
3790 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3793 if (id->driver_info & BTUSB_ATH3012) {
3794 data->setup_on_usb = btusb_setup_qca;
3795 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3796 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3797 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3800 if (id->driver_info & BTUSB_QCA_ROME) {
3801 data->setup_on_usb = btusb_setup_qca;
3802 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3803 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3804 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3805 btusb_check_needs_reset_resume(intf);
3808 if (id->driver_info & BTUSB_QCA_WCN6855) {
3809 data->setup_on_usb = btusb_setup_qca;
3810 hdev->shutdown = btusb_shutdown_qca;
3811 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
3812 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3813 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3814 hci_set_msft_opcode(hdev, 0xFD70);
3817 if (id->driver_info & BTUSB_AMP) {
3818 /* AMP controllers do not support SCO packets */
3821 /* Interface orders are hardcoded in the specification */
3822 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
3823 data->isoc_ifnum = ifnum_base + 1;
3826 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
3827 (id->driver_info & BTUSB_REALTEK)) {
3828 hdev->setup = btrtl_setup_realtek;
3829 hdev->shutdown = btrtl_shutdown_realtek;
3830 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
3832 /* Realtek devices need to set remote wakeup on auto-suspend */
3833 set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
3834 set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
3838 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3840 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3841 if (!disable_scofix)
3842 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3845 if (id->driver_info & BTUSB_BROKEN_ISOC)
3848 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
3849 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
3851 if (id->driver_info & BTUSB_VALID_LE_STATES)
3852 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
3854 if (id->driver_info & BTUSB_DIGIANSWER) {
3855 data->cmdreq_type = USB_TYPE_VENDOR;
3856 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3859 if (id->driver_info & BTUSB_CSR) {
3860 struct usb_device *udev = data->udev;
3861 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
3863 /* Old firmware would otherwise execute USB reset */
3864 if (bcdDevice < 0x117)
3865 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3867 /* This must be set first in case we disable it for fakes */
3868 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3870 /* Fake CSR devices with broken commands */
3871 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
3872 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
3873 hdev->setup = btusb_setup_csr;
3876 if (id->driver_info & BTUSB_SNIFFER) {
3877 struct usb_device *udev = data->udev;
3879 /* New sniffer firmware has crippled HCI interface */
3880 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3881 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3884 if (id->driver_info & BTUSB_INTEL_BOOT) {
3885 /* A bug in the bootloader causes that interrupt interface is
3886 * only enabled after receiving SetInterface(0, AltSetting=0).
3888 err = usb_set_interface(data->udev, 0, 0);
3890 BT_ERR("failed to set interface 0, alt 0 %d", err);
3896 err = usb_driver_claim_interface(&btusb_driver,
3902 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
3903 if (!usb_driver_claim_interface(&btusb_driver,
3905 __set_diag_interface(hdev);
3910 if (enable_autosuspend)
3911 usb_enable_autosuspend(data->udev);
3913 err = hci_register_dev(hdev);
3917 usb_set_intfdata(intf, data);
3919 debugfs_create_file("force_poll_sync", 0644, hdev->debugfs, data,
3920 &force_poll_sync_fops);
3925 if (data->reset_gpio)
3926 gpiod_put(data->reset_gpio);
3931 static void btusb_disconnect(struct usb_interface *intf)
3933 struct btusb_data *data = usb_get_intfdata(intf);
3934 struct hci_dev *hdev;
3936 BT_DBG("intf %p", intf);
3942 usb_set_intfdata(data->intf, NULL);
3945 usb_set_intfdata(data->isoc, NULL);
3948 usb_set_intfdata(data->diag, NULL);
3950 hci_unregister_dev(hdev);
3952 if (intf == data->intf) {
3954 usb_driver_release_interface(&btusb_driver, data->isoc);
3956 usb_driver_release_interface(&btusb_driver, data->diag);
3957 } else if (intf == data->isoc) {
3959 usb_driver_release_interface(&btusb_driver, data->diag);
3960 usb_driver_release_interface(&btusb_driver, data->intf);
3961 } else if (intf == data->diag) {
3962 usb_driver_release_interface(&btusb_driver, data->intf);
3964 usb_driver_release_interface(&btusb_driver, data->isoc);
3967 if (data->oob_wake_irq)
3968 device_init_wakeup(&data->udev->dev, false);
3970 if (data->reset_gpio)
3971 gpiod_put(data->reset_gpio);
3977 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3979 struct btusb_data *data = usb_get_intfdata(intf);
3981 BT_DBG("intf %p", intf);
3983 if (data->suspend_count++)
3986 spin_lock_irq(&data->txlock);
3987 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
3988 set_bit(BTUSB_SUSPENDING, &data->flags);
3989 spin_unlock_irq(&data->txlock);
3991 spin_unlock_irq(&data->txlock);
3992 data->suspend_count--;
3996 cancel_work_sync(&data->work);
3998 btusb_stop_traffic(data);
3999 usb_kill_anchored_urbs(&data->tx_anchor);
4001 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4002 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4003 enable_irq_wake(data->oob_wake_irq);
4004 enable_irq(data->oob_wake_irq);
4007 /* For global suspend, Realtek devices lose the loaded fw
4008 * in them. But for autosuspend, firmware should remain.
4009 * Actually, it depends on whether the usb host sends
4010 * set feature (enable wakeup) or not.
4012 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
4013 if (PMSG_IS_AUTO(message) &&
4014 device_can_wakeup(&data->udev->dev))
4015 data->udev->do_remote_wakeup = 1;
4016 else if (!PMSG_IS_AUTO(message) &&
4017 !device_may_wakeup(&data->udev->dev)) {
4018 data->udev->do_remote_wakeup = 0;
4019 data->udev->reset_resume = 1;
4026 static void play_deferred(struct btusb_data *data)
4031 while ((urb = usb_get_from_anchor(&data->deferred))) {
4032 usb_anchor_urb(urb, &data->tx_anchor);
4034 err = usb_submit_urb(urb, GFP_ATOMIC);
4036 if (err != -EPERM && err != -ENODEV)
4037 BT_ERR("%s urb %p submission failed (%d)",
4038 data->hdev->name, urb, -err);
4039 kfree(urb->setup_packet);
4040 usb_unanchor_urb(urb);
4045 data->tx_in_flight++;
4049 /* Cleanup the rest deferred urbs. */
4050 while ((urb = usb_get_from_anchor(&data->deferred))) {
4051 kfree(urb->setup_packet);
4056 static int btusb_resume(struct usb_interface *intf)
4058 struct btusb_data *data = usb_get_intfdata(intf);
4059 struct hci_dev *hdev = data->hdev;
4062 BT_DBG("intf %p", intf);
4064 if (--data->suspend_count)
4067 /* Disable only if not already disabled (keep it balanced) */
4068 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4069 disable_irq(data->oob_wake_irq);
4070 disable_irq_wake(data->oob_wake_irq);
4073 if (!test_bit(HCI_RUNNING, &hdev->flags))
4076 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4077 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4079 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4084 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4085 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4087 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4091 btusb_submit_bulk_urb(hdev, GFP_NOIO);
4094 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4095 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4096 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4098 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4101 spin_lock_irq(&data->txlock);
4102 play_deferred(data);
4103 clear_bit(BTUSB_SUSPENDING, &data->flags);
4104 spin_unlock_irq(&data->txlock);
4105 schedule_work(&data->work);
4110 usb_scuttle_anchored_urbs(&data->deferred);
4112 spin_lock_irq(&data->txlock);
4113 clear_bit(BTUSB_SUSPENDING, &data->flags);
4114 spin_unlock_irq(&data->txlock);
4120 static struct usb_driver btusb_driver = {
4122 .probe = btusb_probe,
4123 .disconnect = btusb_disconnect,
4125 .suspend = btusb_suspend,
4126 .resume = btusb_resume,
4128 .id_table = btusb_table,
4129 .supports_autosuspend = 1,
4130 .disable_hub_initiated_lpm = 1,
4133 module_usb_driver(btusb_driver);
4135 module_param(disable_scofix, bool, 0644);
4136 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4138 module_param(force_scofix, bool, 0644);
4139 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4141 module_param(enable_autosuspend, bool, 0644);
4142 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4144 module_param(reset, bool, 0644);
4145 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4147 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4148 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4149 MODULE_VERSION(VERSION);
4150 MODULE_LICENSE("GPL");