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)
66 #define BTUSB_ACTIONS_SEMI BIT(27)
68 static const struct usb_device_id btusb_table[] = {
69 /* Generic Bluetooth USB device */
70 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
72 /* Generic Bluetooth AMP device */
73 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
75 /* Generic Bluetooth USB interface */
76 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
78 /* Apple-specific (Broadcom) devices */
79 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
80 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
82 /* MediaTek MT76x0E */
83 { USB_DEVICE(0x0e8d, 0x763f) },
85 /* Broadcom SoftSailing reporting vendor specific */
86 { USB_DEVICE(0x0a5c, 0x21e1) },
88 /* Apple MacBookPro 7,1 */
89 { USB_DEVICE(0x05ac, 0x8213) },
92 { USB_DEVICE(0x05ac, 0x8215) },
94 /* Apple MacBookPro6,2 */
95 { USB_DEVICE(0x05ac, 0x8218) },
97 /* Apple MacBookAir3,1, MacBookAir3,2 */
98 { USB_DEVICE(0x05ac, 0x821b) },
100 /* Apple MacBookAir4,1 */
101 { USB_DEVICE(0x05ac, 0x821f) },
103 /* Apple MacBookPro8,2 */
104 { USB_DEVICE(0x05ac, 0x821a) },
106 /* Apple MacMini5,1 */
107 { USB_DEVICE(0x05ac, 0x8281) },
109 /* AVM BlueFRITZ! USB v2.0 */
110 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
112 /* Bluetooth Ultraport Module from IBM */
113 { USB_DEVICE(0x04bf, 0x030a) },
115 /* ALPS Modules with non-standard id */
116 { USB_DEVICE(0x044e, 0x3001) },
117 { USB_DEVICE(0x044e, 0x3002) },
119 /* Ericsson with non-standard id */
120 { USB_DEVICE(0x0bdb, 0x1002) },
122 /* Canyon CN-BTU1 with HID interfaces */
123 { USB_DEVICE(0x0c10, 0x0000) },
125 /* Broadcom BCM20702B0 (Dynex/Insignia) */
126 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
128 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
129 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
130 .driver_info = BTUSB_BCM_PATCHRAM },
132 /* Broadcom BCM920703 (HTC Vive) */
133 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
134 .driver_info = BTUSB_BCM_PATCHRAM },
136 /* Foxconn - Hon Hai */
137 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
138 .driver_info = BTUSB_BCM_PATCHRAM },
140 /* Lite-On Technology - Broadcom based */
141 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
142 .driver_info = BTUSB_BCM_PATCHRAM },
144 /* Broadcom devices with vendor specific id */
145 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
146 .driver_info = BTUSB_BCM_PATCHRAM },
148 /* ASUSTek Computer - Broadcom based */
149 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
150 .driver_info = BTUSB_BCM_PATCHRAM },
152 /* Belkin F8065bf - Broadcom based */
153 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
154 .driver_info = BTUSB_BCM_PATCHRAM },
156 /* IMC Networks - Broadcom based */
157 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
158 .driver_info = BTUSB_BCM_PATCHRAM },
160 /* Dell Computer - Broadcom based */
161 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
162 .driver_info = BTUSB_BCM_PATCHRAM },
164 /* Toshiba Corp - Broadcom based */
165 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
166 .driver_info = BTUSB_BCM_PATCHRAM },
168 /* Intel Bluetooth USB Bootloader (RAM module) */
169 { USB_DEVICE(0x8087, 0x0a5a),
170 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
172 { } /* Terminating entry */
175 MODULE_DEVICE_TABLE(usb, btusb_table);
177 static const struct usb_device_id blacklist_table[] = {
178 /* CSR BlueCore devices */
179 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
181 /* Broadcom BCM2033 without firmware */
182 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
184 /* Broadcom BCM2045 devices */
185 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
187 /* Atheros 3011 with sflash firmware */
188 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
189 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
190 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
191 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
192 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
193 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
194 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
196 /* Atheros AR9285 Malbec with sflash firmware */
197 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
199 /* Atheros 3012 with sflash firmware */
200 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
246 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
247 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
248 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
249 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
251 /* Atheros AR5BBU12 with sflash firmware */
252 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
254 /* Atheros AR5BBU12 with sflash firmware */
255 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
256 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
258 /* QCA ROME chipset */
259 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
260 BTUSB_WIDEBAND_SPEECH },
261 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
262 BTUSB_WIDEBAND_SPEECH },
263 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
264 BTUSB_WIDEBAND_SPEECH },
265 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
266 BTUSB_WIDEBAND_SPEECH },
267 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
268 BTUSB_WIDEBAND_SPEECH },
269 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
270 BTUSB_WIDEBAND_SPEECH },
271 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
272 BTUSB_WIDEBAND_SPEECH },
273 { USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
274 BTUSB_WIDEBAND_SPEECH },
275 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
276 BTUSB_WIDEBAND_SPEECH },
277 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
278 BTUSB_WIDEBAND_SPEECH },
279 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
280 BTUSB_WIDEBAND_SPEECH },
281 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
282 BTUSB_WIDEBAND_SPEECH },
283 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
284 BTUSB_WIDEBAND_SPEECH },
285 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
286 BTUSB_WIDEBAND_SPEECH },
287 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
288 BTUSB_WIDEBAND_SPEECH },
289 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
290 BTUSB_WIDEBAND_SPEECH },
291 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
292 BTUSB_WIDEBAND_SPEECH },
293 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
294 BTUSB_WIDEBAND_SPEECH },
295 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
296 BTUSB_WIDEBAND_SPEECH },
298 /* QCA WCN6855 chipset */
299 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
300 BTUSB_WIDEBAND_SPEECH |
301 BTUSB_VALID_LE_STATES },
302 { USB_DEVICE(0x0489, 0xe0cc), .driver_info = BTUSB_QCA_WCN6855 |
303 BTUSB_WIDEBAND_SPEECH |
304 BTUSB_VALID_LE_STATES },
305 { USB_DEVICE(0x0489, 0xe0d6), .driver_info = BTUSB_QCA_WCN6855 |
306 BTUSB_WIDEBAND_SPEECH |
307 BTUSB_VALID_LE_STATES },
308 { USB_DEVICE(0x0489, 0xe0e3), .driver_info = BTUSB_QCA_WCN6855 |
309 BTUSB_WIDEBAND_SPEECH |
310 BTUSB_VALID_LE_STATES },
311 { USB_DEVICE(0x10ab, 0x9309), .driver_info = BTUSB_QCA_WCN6855 |
312 BTUSB_WIDEBAND_SPEECH |
313 BTUSB_VALID_LE_STATES },
314 { USB_DEVICE(0x10ab, 0x9409), .driver_info = BTUSB_QCA_WCN6855 |
315 BTUSB_WIDEBAND_SPEECH |
316 BTUSB_VALID_LE_STATES },
317 { USB_DEVICE(0x0489, 0xe0d0), .driver_info = BTUSB_QCA_WCN6855 |
318 BTUSB_WIDEBAND_SPEECH |
319 BTUSB_VALID_LE_STATES },
320 { USB_DEVICE(0x10ab, 0x9108), .driver_info = BTUSB_QCA_WCN6855 |
321 BTUSB_WIDEBAND_SPEECH |
322 BTUSB_VALID_LE_STATES },
323 { USB_DEVICE(0x10ab, 0x9109), .driver_info = BTUSB_QCA_WCN6855 |
324 BTUSB_WIDEBAND_SPEECH |
325 BTUSB_VALID_LE_STATES },
326 { USB_DEVICE(0x10ab, 0x9208), .driver_info = BTUSB_QCA_WCN6855 |
327 BTUSB_WIDEBAND_SPEECH |
328 BTUSB_VALID_LE_STATES },
329 { USB_DEVICE(0x10ab, 0x9209), .driver_info = BTUSB_QCA_WCN6855 |
330 BTUSB_WIDEBAND_SPEECH |
331 BTUSB_VALID_LE_STATES },
332 { USB_DEVICE(0x10ab, 0x9308), .driver_info = BTUSB_QCA_WCN6855 |
333 BTUSB_WIDEBAND_SPEECH |
334 BTUSB_VALID_LE_STATES },
335 { USB_DEVICE(0x10ab, 0x9408), .driver_info = BTUSB_QCA_WCN6855 |
336 BTUSB_WIDEBAND_SPEECH |
337 BTUSB_VALID_LE_STATES },
338 { USB_DEVICE(0x10ab, 0x9508), .driver_info = BTUSB_QCA_WCN6855 |
339 BTUSB_WIDEBAND_SPEECH |
340 BTUSB_VALID_LE_STATES },
341 { USB_DEVICE(0x10ab, 0x9509), .driver_info = BTUSB_QCA_WCN6855 |
342 BTUSB_WIDEBAND_SPEECH |
343 BTUSB_VALID_LE_STATES },
344 { USB_DEVICE(0x10ab, 0x9608), .driver_info = BTUSB_QCA_WCN6855 |
345 BTUSB_WIDEBAND_SPEECH |
346 BTUSB_VALID_LE_STATES },
347 { USB_DEVICE(0x10ab, 0x9609), .driver_info = BTUSB_QCA_WCN6855 |
348 BTUSB_WIDEBAND_SPEECH |
349 BTUSB_VALID_LE_STATES },
350 { USB_DEVICE(0x10ab, 0x9f09), .driver_info = BTUSB_QCA_WCN6855 |
351 BTUSB_WIDEBAND_SPEECH |
352 BTUSB_VALID_LE_STATES },
353 { USB_DEVICE(0x04ca, 0x3022), .driver_info = BTUSB_QCA_WCN6855 |
354 BTUSB_WIDEBAND_SPEECH |
355 BTUSB_VALID_LE_STATES },
356 { USB_DEVICE(0x0489, 0xe0c7), .driver_info = BTUSB_QCA_WCN6855 |
357 BTUSB_WIDEBAND_SPEECH |
358 BTUSB_VALID_LE_STATES },
359 { USB_DEVICE(0x0489, 0xe0c9), .driver_info = BTUSB_QCA_WCN6855 |
360 BTUSB_WIDEBAND_SPEECH |
361 BTUSB_VALID_LE_STATES },
362 { USB_DEVICE(0x0489, 0xe0ca), .driver_info = BTUSB_QCA_WCN6855 |
363 BTUSB_WIDEBAND_SPEECH |
364 BTUSB_VALID_LE_STATES },
365 { USB_DEVICE(0x0489, 0xe0cb), .driver_info = BTUSB_QCA_WCN6855 |
366 BTUSB_WIDEBAND_SPEECH |
367 BTUSB_VALID_LE_STATES },
368 { USB_DEVICE(0x0489, 0xe0ce), .driver_info = BTUSB_QCA_WCN6855 |
369 BTUSB_WIDEBAND_SPEECH |
370 BTUSB_VALID_LE_STATES },
371 { USB_DEVICE(0x0489, 0xe0de), .driver_info = BTUSB_QCA_WCN6855 |
372 BTUSB_WIDEBAND_SPEECH |
373 BTUSB_VALID_LE_STATES },
374 { USB_DEVICE(0x0489, 0xe0df), .driver_info = BTUSB_QCA_WCN6855 |
375 BTUSB_WIDEBAND_SPEECH |
376 BTUSB_VALID_LE_STATES },
377 { USB_DEVICE(0x0489, 0xe0e1), .driver_info = BTUSB_QCA_WCN6855 |
378 BTUSB_WIDEBAND_SPEECH |
379 BTUSB_VALID_LE_STATES },
380 { USB_DEVICE(0x0489, 0xe0ea), .driver_info = BTUSB_QCA_WCN6855 |
381 BTUSB_WIDEBAND_SPEECH |
382 BTUSB_VALID_LE_STATES },
383 { USB_DEVICE(0x0489, 0xe0ec), .driver_info = BTUSB_QCA_WCN6855 |
384 BTUSB_WIDEBAND_SPEECH |
385 BTUSB_VALID_LE_STATES },
386 { USB_DEVICE(0x04ca, 0x3023), .driver_info = BTUSB_QCA_WCN6855 |
387 BTUSB_WIDEBAND_SPEECH |
388 BTUSB_VALID_LE_STATES },
389 { USB_DEVICE(0x04ca, 0x3024), .driver_info = BTUSB_QCA_WCN6855 |
390 BTUSB_WIDEBAND_SPEECH |
391 BTUSB_VALID_LE_STATES },
392 { USB_DEVICE(0x04ca, 0x3a22), .driver_info = BTUSB_QCA_WCN6855 |
393 BTUSB_WIDEBAND_SPEECH |
394 BTUSB_VALID_LE_STATES },
395 { USB_DEVICE(0x04ca, 0x3a24), .driver_info = BTUSB_QCA_WCN6855 |
396 BTUSB_WIDEBAND_SPEECH |
397 BTUSB_VALID_LE_STATES },
398 { USB_DEVICE(0x04ca, 0x3a26), .driver_info = BTUSB_QCA_WCN6855 |
399 BTUSB_WIDEBAND_SPEECH |
400 BTUSB_VALID_LE_STATES },
401 { USB_DEVICE(0x04ca, 0x3a27), .driver_info = BTUSB_QCA_WCN6855 |
402 BTUSB_WIDEBAND_SPEECH |
403 BTUSB_VALID_LE_STATES },
405 /* QCA WCN785x chipset */
406 { USB_DEVICE(0x0cf3, 0xe700), .driver_info = BTUSB_QCA_WCN6855 |
407 BTUSB_WIDEBAND_SPEECH |
408 BTUSB_VALID_LE_STATES },
410 /* Broadcom BCM2035 */
411 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
412 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
413 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
415 /* Broadcom BCM2045 */
416 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
417 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
419 /* IBM/Lenovo ThinkPad with Broadcom chip */
420 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
421 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
423 /* HP laptop with Broadcom chip */
424 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
426 /* Dell laptop with Broadcom chip */
427 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
429 /* Dell Wireless 370 and 410 devices */
430 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
431 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
433 /* Belkin F8T012 and F8T013 devices */
434 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
435 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
437 /* Asus WL-BTD202 device */
438 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
440 /* Kensington Bluetooth USB adapter */
441 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
443 /* RTX Telecom based adapters with buggy SCO support */
444 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
445 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
447 /* CONWISE Technology based adapters with buggy SCO support */
448 { USB_DEVICE(0x0e5e, 0x6622),
449 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
451 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
452 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
454 /* Digianswer devices */
455 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
456 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
458 /* CSR BlueCore Bluetooth Sniffer */
459 { USB_DEVICE(0x0a12, 0x0002),
460 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
462 /* Frontline ComProbe Bluetooth Sniffer */
463 { USB_DEVICE(0x16d3, 0x0002),
464 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
466 /* Marvell Bluetooth devices */
467 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
468 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
469 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
471 /* Intel Bluetooth devices */
472 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_COMBINED },
473 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_COMBINED },
474 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_COMBINED },
475 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_COMBINED },
476 { USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_COMBINED },
477 { USB_DEVICE(0x8087, 0x0035), .driver_info = BTUSB_INTEL_COMBINED },
478 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
479 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL_COMBINED |
480 BTUSB_INTEL_NO_WBS_SUPPORT |
481 BTUSB_INTEL_BROKEN_INITIAL_NCMD |
482 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
483 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL_COMBINED |
484 BTUSB_INTEL_NO_WBS_SUPPORT |
485 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
486 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_COMBINED },
487 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL_COMBINED |
488 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
489 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_COMBINED },
491 /* Other Intel Bluetooth devices */
492 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
493 .driver_info = BTUSB_IGNORE },
495 /* Realtek 8821CE Bluetooth devices */
496 { USB_DEVICE(0x13d3, 0x3529), .driver_info = BTUSB_REALTEK |
497 BTUSB_WIDEBAND_SPEECH },
499 /* Realtek 8822CE Bluetooth devices */
500 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
501 BTUSB_WIDEBAND_SPEECH },
502 { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
503 BTUSB_WIDEBAND_SPEECH },
505 /* Realtek 8852AE Bluetooth devices */
506 { USB_DEVICE(0x0bda, 0x2852), .driver_info = BTUSB_REALTEK |
507 BTUSB_WIDEBAND_SPEECH },
508 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
509 BTUSB_WIDEBAND_SPEECH },
510 { USB_DEVICE(0x0bda, 0x385a), .driver_info = BTUSB_REALTEK |
511 BTUSB_WIDEBAND_SPEECH },
512 { USB_DEVICE(0x0bda, 0x4852), .driver_info = BTUSB_REALTEK |
513 BTUSB_WIDEBAND_SPEECH },
514 { USB_DEVICE(0x04c5, 0x165c), .driver_info = BTUSB_REALTEK |
515 BTUSB_WIDEBAND_SPEECH },
516 { USB_DEVICE(0x04ca, 0x4006), .driver_info = BTUSB_REALTEK |
517 BTUSB_WIDEBAND_SPEECH },
518 { USB_DEVICE(0x0cb8, 0xc549), .driver_info = BTUSB_REALTEK |
519 BTUSB_WIDEBAND_SPEECH },
521 /* Realtek 8852CE Bluetooth devices */
522 { USB_DEVICE(0x04ca, 0x4007), .driver_info = BTUSB_REALTEK |
523 BTUSB_WIDEBAND_SPEECH },
524 { USB_DEVICE(0x04c5, 0x1675), .driver_info = BTUSB_REALTEK |
525 BTUSB_WIDEBAND_SPEECH },
526 { USB_DEVICE(0x0cb8, 0xc558), .driver_info = BTUSB_REALTEK |
527 BTUSB_WIDEBAND_SPEECH },
528 { USB_DEVICE(0x13d3, 0x3587), .driver_info = BTUSB_REALTEK |
529 BTUSB_WIDEBAND_SPEECH },
530 { USB_DEVICE(0x13d3, 0x3586), .driver_info = BTUSB_REALTEK |
531 BTUSB_WIDEBAND_SPEECH },
532 { USB_DEVICE(0x13d3, 0x3592), .driver_info = BTUSB_REALTEK |
533 BTUSB_WIDEBAND_SPEECH },
535 /* Realtek Bluetooth devices */
536 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
537 .driver_info = BTUSB_REALTEK },
539 /* MediaTek Bluetooth devices */
540 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
541 .driver_info = BTUSB_MEDIATEK |
542 BTUSB_WIDEBAND_SPEECH |
543 BTUSB_VALID_LE_STATES },
545 /* Additional MediaTek MT7615E Bluetooth devices */
546 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
548 /* Additional MediaTek MT7663 Bluetooth devices */
549 { USB_DEVICE(0x043e, 0x310c), .driver_info = BTUSB_MEDIATEK |
550 BTUSB_WIDEBAND_SPEECH |
551 BTUSB_VALID_LE_STATES },
553 /* Additional MediaTek MT7668 Bluetooth devices */
554 { USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
555 BTUSB_WIDEBAND_SPEECH |
556 BTUSB_VALID_LE_STATES },
558 /* Additional MediaTek MT7921 Bluetooth devices */
559 { USB_DEVICE(0x0489, 0xe0c8), .driver_info = BTUSB_MEDIATEK |
560 BTUSB_WIDEBAND_SPEECH |
561 BTUSB_VALID_LE_STATES },
562 { USB_DEVICE(0x0489, 0xe0e0), .driver_info = BTUSB_MEDIATEK |
563 BTUSB_WIDEBAND_SPEECH |
564 BTUSB_VALID_LE_STATES },
565 { USB_DEVICE(0x0489, 0xe0f2), .driver_info = BTUSB_MEDIATEK |
566 BTUSB_WIDEBAND_SPEECH |
567 BTUSB_VALID_LE_STATES },
568 { USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
569 BTUSB_WIDEBAND_SPEECH |
570 BTUSB_VALID_LE_STATES },
571 { USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
572 BTUSB_WIDEBAND_SPEECH |
573 BTUSB_VALID_LE_STATES },
574 { USB_DEVICE(0x13d3, 0x3564), .driver_info = BTUSB_MEDIATEK |
575 BTUSB_WIDEBAND_SPEECH |
576 BTUSB_VALID_LE_STATES },
577 { USB_DEVICE(0x13d3, 0x3567), .driver_info = BTUSB_MEDIATEK |
578 BTUSB_WIDEBAND_SPEECH |
579 BTUSB_VALID_LE_STATES },
580 { USB_DEVICE(0x13d3, 0x3578), .driver_info = BTUSB_MEDIATEK |
581 BTUSB_WIDEBAND_SPEECH |
582 BTUSB_VALID_LE_STATES },
583 { USB_DEVICE(0x13d3, 0x3583), .driver_info = BTUSB_MEDIATEK |
584 BTUSB_WIDEBAND_SPEECH |
585 BTUSB_VALID_LE_STATES },
586 { USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
587 BTUSB_WIDEBAND_SPEECH |
588 BTUSB_VALID_LE_STATES },
589 { USB_DEVICE(0x0e8d, 0x0608), .driver_info = BTUSB_MEDIATEK |
590 BTUSB_WIDEBAND_SPEECH |
591 BTUSB_VALID_LE_STATES },
593 /* MediaTek MT7922A Bluetooth devices */
594 { USB_DEVICE(0x0489, 0xe0d8), .driver_info = BTUSB_MEDIATEK |
595 BTUSB_WIDEBAND_SPEECH |
596 BTUSB_VALID_LE_STATES },
597 { USB_DEVICE(0x0489, 0xe0d9), .driver_info = BTUSB_MEDIATEK |
598 BTUSB_WIDEBAND_SPEECH |
599 BTUSB_VALID_LE_STATES },
600 { USB_DEVICE(0x13d3, 0x3568), .driver_info = BTUSB_MEDIATEK |
601 BTUSB_WIDEBAND_SPEECH |
602 BTUSB_VALID_LE_STATES },
603 { USB_DEVICE(0x0489, 0xe0e2), .driver_info = BTUSB_MEDIATEK |
604 BTUSB_WIDEBAND_SPEECH |
605 BTUSB_VALID_LE_STATES },
607 /* Additional Realtek 8723AE Bluetooth devices */
608 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
609 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
611 /* Additional Realtek 8723BE Bluetooth devices */
612 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
613 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
614 { USB_DEVICE(0x04f2, 0xb49f), .driver_info = BTUSB_REALTEK },
615 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
616 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
617 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
618 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
620 /* Additional Realtek 8723BU Bluetooth devices */
621 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
623 /* Additional Realtek 8723DE Bluetooth devices */
624 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
625 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
627 /* Additional Realtek 8761BUV Bluetooth devices */
628 { USB_DEVICE(0x2357, 0x0604), .driver_info = BTUSB_REALTEK |
629 BTUSB_WIDEBAND_SPEECH },
630 { USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
631 BTUSB_WIDEBAND_SPEECH },
632 { USB_DEVICE(0x2550, 0x8761), .driver_info = BTUSB_REALTEK |
633 BTUSB_WIDEBAND_SPEECH },
634 { USB_DEVICE(0x0bda, 0x8771), .driver_info = BTUSB_REALTEK |
635 BTUSB_WIDEBAND_SPEECH },
636 { USB_DEVICE(0x7392, 0xc611), .driver_info = BTUSB_REALTEK |
637 BTUSB_WIDEBAND_SPEECH },
639 /* Additional Realtek 8821AE Bluetooth devices */
640 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
641 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
642 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
643 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
644 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
646 /* Additional Realtek 8822BE Bluetooth devices */
647 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
648 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
650 /* Additional Realtek 8822CE Bluetooth devices */
651 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
652 BTUSB_WIDEBAND_SPEECH },
653 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
654 BTUSB_WIDEBAND_SPEECH },
655 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
656 BTUSB_WIDEBAND_SPEECH },
657 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
658 BTUSB_WIDEBAND_SPEECH },
659 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
660 BTUSB_WIDEBAND_SPEECH },
661 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
662 BTUSB_WIDEBAND_SPEECH },
663 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
664 BTUSB_WIDEBAND_SPEECH },
665 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
666 BTUSB_WIDEBAND_SPEECH },
667 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
668 BTUSB_WIDEBAND_SPEECH },
669 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
670 BTUSB_WIDEBAND_SPEECH },
671 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
672 BTUSB_WIDEBAND_SPEECH },
674 /* Actions Semiconductor ATS2851 based devices */
675 { USB_DEVICE(0x10d7, 0xb012), .driver_info = BTUSB_ACTIONS_SEMI },
677 /* Silicon Wave based devices */
678 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
680 { } /* Terminating entry */
683 /* The Bluetooth USB module build into some devices needs to be reset on resume,
684 * this is a problem with the platform (likely shutting off all power) not with
685 * the module itself. So we use a DMI list to match known broken platforms.
687 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
689 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
691 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
692 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
696 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
698 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
699 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
703 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
705 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
706 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
712 #define BTUSB_MAX_ISOC_FRAMES 10
714 #define BTUSB_INTR_RUNNING 0
715 #define BTUSB_BULK_RUNNING 1
716 #define BTUSB_ISOC_RUNNING 2
717 #define BTUSB_SUSPENDING 3
718 #define BTUSB_DID_ISO_RESUME 4
719 #define BTUSB_BOOTLOADER 5
720 #define BTUSB_DOWNLOADING 6
721 #define BTUSB_FIRMWARE_LOADED 7
722 #define BTUSB_FIRMWARE_FAILED 8
723 #define BTUSB_BOOTING 9
724 #define BTUSB_DIAG_RUNNING 10
725 #define BTUSB_OOB_WAKE_ENABLED 11
726 #define BTUSB_HW_RESET_ACTIVE 12
727 #define BTUSB_TX_WAIT_VND_EVT 13
728 #define BTUSB_WAKEUP_AUTOSUSPEND 14
729 #define BTUSB_USE_ALT3_FOR_WBS 15
732 struct hci_dev *hdev;
733 struct usb_device *udev;
734 struct usb_interface *intf;
735 struct usb_interface *isoc;
736 struct usb_interface *diag;
743 struct work_struct work;
744 struct work_struct waker;
745 struct delayed_work rx_work;
747 struct sk_buff_head acl_q;
749 struct usb_anchor deferred;
750 struct usb_anchor tx_anchor;
754 struct usb_anchor intr_anchor;
755 struct usb_anchor bulk_anchor;
756 struct usb_anchor isoc_anchor;
757 struct usb_anchor diag_anchor;
758 struct usb_anchor ctrl_anchor;
761 struct sk_buff *evt_skb;
762 struct sk_buff *acl_skb;
763 struct sk_buff *sco_skb;
765 struct usb_endpoint_descriptor *intr_ep;
766 struct usb_endpoint_descriptor *bulk_tx_ep;
767 struct usb_endpoint_descriptor *bulk_rx_ep;
768 struct usb_endpoint_descriptor *isoc_tx_ep;
769 struct usb_endpoint_descriptor *isoc_rx_ep;
770 struct usb_endpoint_descriptor *diag_tx_ep;
771 struct usb_endpoint_descriptor *diag_rx_ep;
773 struct gpio_desc *reset_gpio;
778 unsigned int sco_num;
779 unsigned int air_mode;
780 bool usb_alt6_packet_flow;
784 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
785 int (*recv_acl)(struct hci_dev *hdev, struct sk_buff *skb);
786 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
788 int (*setup_on_usb)(struct hci_dev *hdev);
790 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
791 unsigned cmd_timeout_cnt;
794 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
796 struct btusb_data *data = hci_get_drvdata(hdev);
797 struct gpio_desc *reset_gpio = data->reset_gpio;
799 if (++data->cmd_timeout_cnt < 5)
803 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
808 * Toggle the hard reset line if the platform provides one. The reset
809 * is going to yank the device off the USB and then replug. So doing
810 * once is enough. The cleanup is handled correctly on the way out
811 * (standard USB disconnect), and the new device is detected cleanly
812 * and bound to the driver again like it should be.
814 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
815 bt_dev_err(hdev, "last reset failed? Not resetting again");
819 bt_dev_err(hdev, "Initiating HW reset via gpio");
820 gpiod_set_value_cansleep(reset_gpio, 1);
822 gpiod_set_value_cansleep(reset_gpio, 0);
825 static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
827 struct btusb_data *data = hci_get_drvdata(hdev);
828 struct gpio_desc *reset_gpio = data->reset_gpio;
830 if (++data->cmd_timeout_cnt < 5)
834 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
838 /* Toggle the hard reset line. The Realtek device is going to
839 * yank itself off the USB and then replug. The cleanup is handled
840 * correctly on the way out (standard USB disconnect), and the new
841 * device is detected cleanly and bound to the driver again like
844 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
845 bt_dev_err(hdev, "last reset failed? Not resetting again");
849 bt_dev_err(hdev, "Reset Realtek device via gpio");
850 gpiod_set_value_cansleep(reset_gpio, 1);
852 gpiod_set_value_cansleep(reset_gpio, 0);
855 static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
857 struct btusb_data *data = hci_get_drvdata(hdev);
858 struct gpio_desc *reset_gpio = data->reset_gpio;
861 if (++data->cmd_timeout_cnt < 5)
865 bt_dev_err(hdev, "Reset qca device via bt_en gpio");
867 /* Toggle the hard reset line. The qca bt device is going to
868 * yank itself off the USB and then replug. The cleanup is handled
869 * correctly on the way out (standard USB disconnect), and the new
870 * device is detected cleanly and bound to the driver again like
873 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
874 bt_dev_err(hdev, "last reset failed? Not resetting again");
878 gpiod_set_value_cansleep(reset_gpio, 0);
880 gpiod_set_value_cansleep(reset_gpio, 1);
885 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
886 /* This is not an unbalanced PM reference since the device will reset */
887 err = usb_autopm_get_interface(data->intf);
889 usb_queue_reset_device(data->intf);
891 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
894 static inline void btusb_free_frags(struct btusb_data *data)
898 spin_lock_irqsave(&data->rxlock, flags);
900 dev_kfree_skb_irq(data->evt_skb);
901 data->evt_skb = NULL;
903 dev_kfree_skb_irq(data->acl_skb);
904 data->acl_skb = NULL;
906 dev_kfree_skb_irq(data->sco_skb);
907 data->sco_skb = NULL;
909 spin_unlock_irqrestore(&data->rxlock, flags);
912 static int btusb_recv_event(struct btusb_data *data, struct sk_buff *skb)
914 if (data->intr_interval) {
915 /* Trigger dequeue immediatelly if an event is received */
916 schedule_delayed_work(&data->rx_work, 0);
919 return data->recv_event(data->hdev, skb);
922 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
928 spin_lock_irqsave(&data->rxlock, flags);
935 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
941 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
942 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
945 len = min_t(uint, hci_skb_expect(skb), count);
946 skb_put_data(skb, buffer, len);
950 hci_skb_expect(skb) -= len;
952 if (skb->len == HCI_EVENT_HDR_SIZE) {
953 /* Complete event header */
954 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
956 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
965 if (!hci_skb_expect(skb)) {
967 btusb_recv_event(data, skb);
973 spin_unlock_irqrestore(&data->rxlock, flags);
978 static int btusb_recv_acl(struct btusb_data *data, struct sk_buff *skb)
980 /* Only queue ACL packet if intr_interval is set as it means
981 * force_poll_sync has been enabled.
983 if (!data->intr_interval)
984 return data->recv_acl(data->hdev, skb);
986 skb_queue_tail(&data->acl_q, skb);
987 schedule_delayed_work(&data->rx_work, data->intr_interval);
992 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
998 spin_lock_irqsave(&data->rxlock, flags);
1005 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
1011 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
1012 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
1015 len = min_t(uint, hci_skb_expect(skb), count);
1016 skb_put_data(skb, buffer, len);
1020 hci_skb_expect(skb) -= len;
1022 if (skb->len == HCI_ACL_HDR_SIZE) {
1023 __le16 dlen = hci_acl_hdr(skb)->dlen;
1025 /* Complete ACL header */
1026 hci_skb_expect(skb) = __le16_to_cpu(dlen);
1028 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
1037 if (!hci_skb_expect(skb)) {
1038 /* Complete frame */
1039 btusb_recv_acl(data, skb);
1044 data->acl_skb = skb;
1045 spin_unlock_irqrestore(&data->rxlock, flags);
1050 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
1052 struct sk_buff *skb;
1053 unsigned long flags;
1056 spin_lock_irqsave(&data->rxlock, flags);
1057 skb = data->sco_skb;
1063 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
1069 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
1070 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
1073 len = min_t(uint, hci_skb_expect(skb), count);
1074 skb_put_data(skb, buffer, len);
1078 hci_skb_expect(skb) -= len;
1080 if (skb->len == HCI_SCO_HDR_SIZE) {
1081 /* Complete SCO header */
1082 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
1084 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
1093 if (!hci_skb_expect(skb)) {
1094 /* Complete frame */
1095 hci_recv_frame(data->hdev, skb);
1100 data->sco_skb = skb;
1101 spin_unlock_irqrestore(&data->rxlock, flags);
1106 static void btusb_intr_complete(struct urb *urb)
1108 struct hci_dev *hdev = urb->context;
1109 struct btusb_data *data = hci_get_drvdata(hdev);
1112 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1113 urb->actual_length);
1115 if (!test_bit(HCI_RUNNING, &hdev->flags))
1118 if (urb->status == 0) {
1119 hdev->stat.byte_rx += urb->actual_length;
1121 if (btusb_recv_intr(data, urb->transfer_buffer,
1122 urb->actual_length) < 0) {
1123 bt_dev_err(hdev, "corrupted event packet");
1124 hdev->stat.err_rx++;
1126 } else if (urb->status == -ENOENT) {
1127 /* Avoid suspend failed when usb_kill_urb */
1131 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
1134 usb_mark_last_busy(data->udev);
1135 usb_anchor_urb(urb, &data->intr_anchor);
1137 err = usb_submit_urb(urb, GFP_ATOMIC);
1139 /* -EPERM: urb is being killed;
1140 * -ENODEV: device got disconnected
1142 if (err != -EPERM && err != -ENODEV)
1143 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1146 hci_cmd_sync_cancel(hdev, -err);
1147 usb_unanchor_urb(urb);
1151 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
1153 struct btusb_data *data = hci_get_drvdata(hdev);
1159 BT_DBG("%s", hdev->name);
1164 urb = usb_alloc_urb(0, mem_flags);
1168 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
1170 buf = kmalloc(size, mem_flags);
1176 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
1178 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
1179 btusb_intr_complete, hdev, data->intr_ep->bInterval);
1181 urb->transfer_flags |= URB_FREE_BUFFER;
1183 usb_anchor_urb(urb, &data->intr_anchor);
1185 err = usb_submit_urb(urb, mem_flags);
1187 if (err != -EPERM && err != -ENODEV)
1188 bt_dev_err(hdev, "urb %p submission failed (%d)",
1191 hci_cmd_sync_cancel(hdev, -err);
1192 usb_unanchor_urb(urb);
1195 /* Only initialize intr_interval if URB poll sync is enabled */
1196 if (!data->poll_sync)
1199 /* The units are frames (milliseconds) for full and low speed devices,
1200 * and microframes (1/8 millisecond) for highspeed and SuperSpeed
1203 * This is done once on open/resume so it shouldn't change even if
1204 * force_poll_sync changes.
1206 switch (urb->dev->speed) {
1207 case USB_SPEED_SUPER_PLUS:
1208 case USB_SPEED_SUPER: /* units are 125us */
1209 data->intr_interval = usecs_to_jiffies(urb->interval * 125);
1212 data->intr_interval = msecs_to_jiffies(urb->interval);
1222 static void btusb_bulk_complete(struct urb *urb)
1224 struct hci_dev *hdev = urb->context;
1225 struct btusb_data *data = hci_get_drvdata(hdev);
1228 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1229 urb->actual_length);
1231 if (!test_bit(HCI_RUNNING, &hdev->flags))
1234 if (urb->status == 0) {
1235 hdev->stat.byte_rx += urb->actual_length;
1237 if (data->recv_bulk(data, urb->transfer_buffer,
1238 urb->actual_length) < 0) {
1239 bt_dev_err(hdev, "corrupted ACL packet");
1240 hdev->stat.err_rx++;
1242 } else if (urb->status == -ENOENT) {
1243 /* Avoid suspend failed when usb_kill_urb */
1247 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
1250 usb_anchor_urb(urb, &data->bulk_anchor);
1251 usb_mark_last_busy(data->udev);
1253 err = usb_submit_urb(urb, GFP_ATOMIC);
1255 /* -EPERM: urb is being killed;
1256 * -ENODEV: device got disconnected
1258 if (err != -EPERM && err != -ENODEV)
1259 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1261 usb_unanchor_urb(urb);
1265 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
1267 struct btusb_data *data = hci_get_drvdata(hdev);
1271 int err, size = HCI_MAX_FRAME_SIZE;
1273 BT_DBG("%s", hdev->name);
1275 if (!data->bulk_rx_ep)
1278 urb = usb_alloc_urb(0, mem_flags);
1282 buf = kmalloc(size, mem_flags);
1288 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1290 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1291 btusb_bulk_complete, hdev);
1293 urb->transfer_flags |= URB_FREE_BUFFER;
1295 usb_mark_last_busy(data->udev);
1296 usb_anchor_urb(urb, &data->bulk_anchor);
1298 err = usb_submit_urb(urb, mem_flags);
1300 if (err != -EPERM && err != -ENODEV)
1301 bt_dev_err(hdev, "urb %p submission failed (%d)",
1303 usb_unanchor_urb(urb);
1311 static void btusb_isoc_complete(struct urb *urb)
1313 struct hci_dev *hdev = urb->context;
1314 struct btusb_data *data = hci_get_drvdata(hdev);
1317 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1318 urb->actual_length);
1320 if (!test_bit(HCI_RUNNING, &hdev->flags))
1323 if (urb->status == 0) {
1324 for (i = 0; i < urb->number_of_packets; i++) {
1325 unsigned int offset = urb->iso_frame_desc[i].offset;
1326 unsigned int length = urb->iso_frame_desc[i].actual_length;
1328 if (urb->iso_frame_desc[i].status)
1331 hdev->stat.byte_rx += length;
1333 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1335 bt_dev_err(hdev, "corrupted SCO packet");
1336 hdev->stat.err_rx++;
1339 } else if (urb->status == -ENOENT) {
1340 /* Avoid suspend failed when usb_kill_urb */
1344 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1347 usb_anchor_urb(urb, &data->isoc_anchor);
1349 err = usb_submit_urb(urb, GFP_ATOMIC);
1351 /* -EPERM: urb is being killed;
1352 * -ENODEV: device got disconnected
1354 if (err != -EPERM && err != -ENODEV)
1355 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1357 usb_unanchor_urb(urb);
1361 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1362 int mtu, struct btusb_data *data)
1365 unsigned int interval;
1367 BT_DBG("len %d mtu %d", len, mtu);
1369 /* For mSBC ALT 6 setting the host will send the packet at continuous
1370 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1371 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1372 * To maintain the rate we send 63bytes of usb packets alternatively for
1373 * 7ms and 8ms to maintain the rate as 7.5ms.
1375 if (data->usb_alt6_packet_flow) {
1377 data->usb_alt6_packet_flow = false;
1380 data->usb_alt6_packet_flow = true;
1383 for (i = 0; i < interval; i++) {
1384 urb->iso_frame_desc[i].offset = offset;
1385 urb->iso_frame_desc[i].length = offset;
1388 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1389 urb->iso_frame_desc[i].offset = offset;
1390 urb->iso_frame_desc[i].length = len;
1394 urb->number_of_packets = i;
1397 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1401 BT_DBG("len %d mtu %d", len, mtu);
1403 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1404 i++, offset += mtu, len -= mtu) {
1405 urb->iso_frame_desc[i].offset = offset;
1406 urb->iso_frame_desc[i].length = mtu;
1409 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1410 urb->iso_frame_desc[i].offset = offset;
1411 urb->iso_frame_desc[i].length = len;
1415 urb->number_of_packets = i;
1418 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1420 struct btusb_data *data = hci_get_drvdata(hdev);
1426 BT_DBG("%s", hdev->name);
1428 if (!data->isoc_rx_ep)
1431 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1435 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1436 BTUSB_MAX_ISOC_FRAMES;
1438 buf = kmalloc(size, mem_flags);
1444 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1446 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1447 hdev, data->isoc_rx_ep->bInterval);
1449 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1451 __fill_isoc_descriptor(urb, size,
1452 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1454 usb_anchor_urb(urb, &data->isoc_anchor);
1456 err = usb_submit_urb(urb, mem_flags);
1458 if (err != -EPERM && err != -ENODEV)
1459 bt_dev_err(hdev, "urb %p submission failed (%d)",
1461 usb_unanchor_urb(urb);
1469 static void btusb_diag_complete(struct urb *urb)
1471 struct hci_dev *hdev = urb->context;
1472 struct btusb_data *data = hci_get_drvdata(hdev);
1475 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1476 urb->actual_length);
1478 if (urb->status == 0) {
1479 struct sk_buff *skb;
1481 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1483 skb_put_data(skb, urb->transfer_buffer,
1484 urb->actual_length);
1485 hci_recv_diag(hdev, skb);
1487 } else if (urb->status == -ENOENT) {
1488 /* Avoid suspend failed when usb_kill_urb */
1492 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1495 usb_anchor_urb(urb, &data->diag_anchor);
1496 usb_mark_last_busy(data->udev);
1498 err = usb_submit_urb(urb, GFP_ATOMIC);
1500 /* -EPERM: urb is being killed;
1501 * -ENODEV: device got disconnected
1503 if (err != -EPERM && err != -ENODEV)
1504 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1506 usb_unanchor_urb(urb);
1510 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1512 struct btusb_data *data = hci_get_drvdata(hdev);
1516 int err, size = HCI_MAX_FRAME_SIZE;
1518 BT_DBG("%s", hdev->name);
1520 if (!data->diag_rx_ep)
1523 urb = usb_alloc_urb(0, mem_flags);
1527 buf = kmalloc(size, mem_flags);
1533 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1535 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1536 btusb_diag_complete, hdev);
1538 urb->transfer_flags |= URB_FREE_BUFFER;
1540 usb_mark_last_busy(data->udev);
1541 usb_anchor_urb(urb, &data->diag_anchor);
1543 err = usb_submit_urb(urb, mem_flags);
1545 if (err != -EPERM && err != -ENODEV)
1546 bt_dev_err(hdev, "urb %p submission failed (%d)",
1548 usb_unanchor_urb(urb);
1556 static void btusb_tx_complete(struct urb *urb)
1558 struct sk_buff *skb = urb->context;
1559 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1560 struct btusb_data *data = hci_get_drvdata(hdev);
1561 unsigned long flags;
1563 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1564 urb->actual_length);
1566 if (!test_bit(HCI_RUNNING, &hdev->flags))
1570 hdev->stat.byte_tx += urb->transfer_buffer_length;
1572 if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)
1573 hci_cmd_sync_cancel(hdev, -urb->status);
1574 hdev->stat.err_tx++;
1578 spin_lock_irqsave(&data->txlock, flags);
1579 data->tx_in_flight--;
1580 spin_unlock_irqrestore(&data->txlock, flags);
1582 kfree(urb->setup_packet);
1587 static void btusb_isoc_tx_complete(struct urb *urb)
1589 struct sk_buff *skb = urb->context;
1590 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1592 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1593 urb->actual_length);
1595 if (!test_bit(HCI_RUNNING, &hdev->flags))
1599 hdev->stat.byte_tx += urb->transfer_buffer_length;
1601 hdev->stat.err_tx++;
1604 kfree(urb->setup_packet);
1609 static int btusb_open(struct hci_dev *hdev)
1611 struct btusb_data *data = hci_get_drvdata(hdev);
1614 BT_DBG("%s", hdev->name);
1616 err = usb_autopm_get_interface(data->intf);
1620 /* Patching USB firmware files prior to starting any URBs of HCI path
1621 * It is more safe to use USB bulk channel for downloading USB patch
1623 if (data->setup_on_usb) {
1624 err = data->setup_on_usb(hdev);
1629 data->intf->needs_remote_wakeup = 1;
1631 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1634 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1638 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1640 usb_kill_anchored_urbs(&data->intr_anchor);
1644 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1645 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1648 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1649 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1653 usb_autopm_put_interface(data->intf);
1657 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1659 usb_autopm_put_interface(data->intf);
1663 static void btusb_stop_traffic(struct btusb_data *data)
1665 usb_kill_anchored_urbs(&data->intr_anchor);
1666 usb_kill_anchored_urbs(&data->bulk_anchor);
1667 usb_kill_anchored_urbs(&data->isoc_anchor);
1668 usb_kill_anchored_urbs(&data->diag_anchor);
1669 usb_kill_anchored_urbs(&data->ctrl_anchor);
1672 static int btusb_close(struct hci_dev *hdev)
1674 struct btusb_data *data = hci_get_drvdata(hdev);
1677 BT_DBG("%s", hdev->name);
1679 cancel_delayed_work(&data->rx_work);
1680 cancel_work_sync(&data->work);
1681 cancel_work_sync(&data->waker);
1683 skb_queue_purge(&data->acl_q);
1685 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1686 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1687 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1688 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1690 btusb_stop_traffic(data);
1691 btusb_free_frags(data);
1693 err = usb_autopm_get_interface(data->intf);
1697 data->intf->needs_remote_wakeup = 0;
1699 /* Enable remote wake up for auto-suspend */
1700 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags))
1701 data->intf->needs_remote_wakeup = 1;
1703 usb_autopm_put_interface(data->intf);
1706 usb_scuttle_anchored_urbs(&data->deferred);
1710 static int btusb_flush(struct hci_dev *hdev)
1712 struct btusb_data *data = hci_get_drvdata(hdev);
1714 BT_DBG("%s", hdev->name);
1716 cancel_delayed_work(&data->rx_work);
1718 skb_queue_purge(&data->acl_q);
1720 usb_kill_anchored_urbs(&data->tx_anchor);
1721 btusb_free_frags(data);
1726 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1728 struct btusb_data *data = hci_get_drvdata(hdev);
1729 struct usb_ctrlrequest *dr;
1733 urb = usb_alloc_urb(0, GFP_KERNEL);
1735 return ERR_PTR(-ENOMEM);
1737 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1740 return ERR_PTR(-ENOMEM);
1743 dr->bRequestType = data->cmdreq_type;
1744 dr->bRequest = data->cmdreq;
1747 dr->wLength = __cpu_to_le16(skb->len);
1749 pipe = usb_sndctrlpipe(data->udev, 0x00);
1751 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1752 skb->data, skb->len, btusb_tx_complete, skb);
1754 skb->dev = (void *)hdev;
1759 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1761 struct btusb_data *data = hci_get_drvdata(hdev);
1765 if (!data->bulk_tx_ep)
1766 return ERR_PTR(-ENODEV);
1768 urb = usb_alloc_urb(0, GFP_KERNEL);
1770 return ERR_PTR(-ENOMEM);
1772 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1774 usb_fill_bulk_urb(urb, data->udev, pipe,
1775 skb->data, skb->len, btusb_tx_complete, skb);
1777 skb->dev = (void *)hdev;
1782 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1784 struct btusb_data *data = hci_get_drvdata(hdev);
1788 if (!data->isoc_tx_ep)
1789 return ERR_PTR(-ENODEV);
1791 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1793 return ERR_PTR(-ENOMEM);
1795 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1797 usb_fill_int_urb(urb, data->udev, pipe,
1798 skb->data, skb->len, btusb_isoc_tx_complete,
1799 skb, data->isoc_tx_ep->bInterval);
1801 urb->transfer_flags = URB_ISO_ASAP;
1803 if (data->isoc_altsetting == 6)
1804 __fill_isoc_descriptor_msbc(urb, skb->len,
1805 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1808 __fill_isoc_descriptor(urb, skb->len,
1809 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1810 skb->dev = (void *)hdev;
1815 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1817 struct btusb_data *data = hci_get_drvdata(hdev);
1820 usb_anchor_urb(urb, &data->tx_anchor);
1822 err = usb_submit_urb(urb, GFP_KERNEL);
1824 if (err != -EPERM && err != -ENODEV)
1825 bt_dev_err(hdev, "urb %p submission failed (%d)",
1827 kfree(urb->setup_packet);
1828 usb_unanchor_urb(urb);
1830 usb_mark_last_busy(data->udev);
1837 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1839 struct btusb_data *data = hci_get_drvdata(hdev);
1840 unsigned long flags;
1843 spin_lock_irqsave(&data->txlock, flags);
1844 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1846 data->tx_in_flight++;
1847 spin_unlock_irqrestore(&data->txlock, flags);
1850 return submit_tx_urb(hdev, urb);
1852 usb_anchor_urb(urb, &data->deferred);
1853 schedule_work(&data->waker);
1859 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1863 BT_DBG("%s", hdev->name);
1865 switch (hci_skb_pkt_type(skb)) {
1866 case HCI_COMMAND_PKT:
1867 urb = alloc_ctrl_urb(hdev, skb);
1869 return PTR_ERR(urb);
1871 hdev->stat.cmd_tx++;
1872 return submit_or_queue_tx_urb(hdev, urb);
1874 case HCI_ACLDATA_PKT:
1875 urb = alloc_bulk_urb(hdev, skb);
1877 return PTR_ERR(urb);
1879 hdev->stat.acl_tx++;
1880 return submit_or_queue_tx_urb(hdev, urb);
1882 case HCI_SCODATA_PKT:
1883 if (hci_conn_num(hdev, SCO_LINK) < 1)
1886 urb = alloc_isoc_urb(hdev, skb);
1888 return PTR_ERR(urb);
1890 hdev->stat.sco_tx++;
1891 return submit_tx_urb(hdev, urb);
1893 case HCI_ISODATA_PKT:
1894 urb = alloc_bulk_urb(hdev, skb);
1896 return PTR_ERR(urb);
1898 return submit_or_queue_tx_urb(hdev, urb);
1904 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1906 struct btusb_data *data = hci_get_drvdata(hdev);
1908 BT_DBG("%s evt %d", hdev->name, evt);
1910 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1911 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1912 data->air_mode = evt;
1913 schedule_work(&data->work);
1917 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1919 struct btusb_data *data = hci_get_drvdata(hdev);
1920 struct usb_interface *intf = data->isoc;
1921 struct usb_endpoint_descriptor *ep_desc;
1927 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
1929 bt_dev_err(hdev, "setting interface failed (%d)", -err);
1933 data->isoc_altsetting = altsetting;
1935 data->isoc_tx_ep = NULL;
1936 data->isoc_rx_ep = NULL;
1938 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1939 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1941 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1942 data->isoc_tx_ep = ep_desc;
1946 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1947 data->isoc_rx_ep = ep_desc;
1952 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1953 bt_dev_err(hdev, "invalid SCO descriptors");
1960 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1962 struct btusb_data *data = hci_get_drvdata(hdev);
1965 if (data->isoc_altsetting != new_alts) {
1966 unsigned long flags;
1968 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1969 usb_kill_anchored_urbs(&data->isoc_anchor);
1971 /* When isochronous alternate setting needs to be
1972 * changed, because SCO connection has been added
1973 * or removed, a packet fragment may be left in the
1974 * reassembling state. This could lead to wrongly
1975 * assembled fragments.
1977 * Clear outstanding fragment when selecting a new
1978 * alternate setting.
1980 spin_lock_irqsave(&data->rxlock, flags);
1981 kfree_skb(data->sco_skb);
1982 data->sco_skb = NULL;
1983 spin_unlock_irqrestore(&data->rxlock, flags);
1985 err = __set_isoc_interface(hdev, new_alts);
1990 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1991 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1992 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1994 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
2000 static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
2003 struct usb_interface *intf = data->isoc;
2006 BT_DBG("Looking for Alt no :%d", alt);
2011 for (i = 0; i < intf->num_altsetting; i++) {
2012 if (intf->altsetting[i].desc.bAlternateSetting == alt)
2013 return &intf->altsetting[i];
2019 static void btusb_work(struct work_struct *work)
2021 struct btusb_data *data = container_of(work, struct btusb_data, work);
2022 struct hci_dev *hdev = data->hdev;
2026 if (data->sco_num > 0) {
2027 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
2028 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
2030 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2031 usb_kill_anchored_urbs(&data->isoc_anchor);
2035 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
2038 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
2039 if (hdev->voice_setting & 0x0020) {
2040 static const int alts[3] = { 2, 4, 5 };
2042 new_alts = alts[data->sco_num - 1];
2044 new_alts = data->sco_num;
2046 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
2047 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
2048 * many adapters do not support it. Alt 1 appears to
2049 * work for all adapters that do not have alt 6, and
2050 * which work with WBS at all. Some devices prefer
2051 * alt 3 (HCI payload >= 60 Bytes let air packet
2052 * data satisfy 60 bytes), requiring
2053 * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
2054 * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
2056 if (btusb_find_altsetting(data, 6))
2058 else if (btusb_find_altsetting(data, 3) &&
2059 hdev->sco_mtu >= 72 &&
2060 test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
2066 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
2067 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
2069 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2070 usb_kill_anchored_urbs(&data->isoc_anchor);
2072 __set_isoc_interface(hdev, 0);
2073 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
2074 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
2078 static void btusb_waker(struct work_struct *work)
2080 struct btusb_data *data = container_of(work, struct btusb_data, waker);
2083 err = usb_autopm_get_interface(data->intf);
2087 usb_autopm_put_interface(data->intf);
2090 static void btusb_rx_work(struct work_struct *work)
2092 struct btusb_data *data = container_of(work, struct btusb_data,
2094 struct sk_buff *skb;
2096 /* Dequeue ACL data received during the interval */
2097 while ((skb = skb_dequeue(&data->acl_q)))
2098 data->recv_acl(data->hdev, skb);
2101 static int btusb_setup_bcm92035(struct hci_dev *hdev)
2103 struct sk_buff *skb;
2106 BT_DBG("%s", hdev->name);
2108 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
2110 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
2117 static int btusb_setup_csr(struct hci_dev *hdev)
2119 struct btusb_data *data = hci_get_drvdata(hdev);
2120 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
2121 struct hci_rp_read_local_version *rp;
2122 struct sk_buff *skb;
2123 bool is_fake = false;
2126 BT_DBG("%s", hdev->name);
2128 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2131 int err = PTR_ERR(skb);
2132 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
2136 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
2137 bt_dev_err(hdev, "CSR: Local version length mismatch");
2142 rp = (struct hci_rp_read_local_version *)skb->data;
2144 bt_dev_info(hdev, "CSR: Setting up dongle with HCI ver=%u rev=%04x; LMP ver=%u subver=%04x; manufacturer=%u",
2145 le16_to_cpu(rp->hci_ver), le16_to_cpu(rp->hci_rev),
2146 le16_to_cpu(rp->lmp_ver), le16_to_cpu(rp->lmp_subver),
2147 le16_to_cpu(rp->manufacturer));
2149 /* Detect a wide host of Chinese controllers that aren't CSR.
2151 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
2153 * The main thing they have in common is that these are really popular low-cost
2154 * options that support newer Bluetooth versions but rely on heavy VID/PID
2155 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
2157 * We detect actual CSR devices by checking that the HCI manufacturer code
2158 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
2159 * HCI rev values always match. As they both store the firmware number.
2161 if (le16_to_cpu(rp->manufacturer) != 10 ||
2162 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
2165 /* Known legit CSR firmware build numbers and their supported BT versions:
2166 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
2167 * - 1.2 (0x2) -> 0x04d9, 0x0529
2168 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
2169 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
2170 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
2172 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
2173 * support BT 1.1 only; so it's a dead giveaway when some
2174 * third-party BT 4.0 dongle reuses it.
2176 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
2177 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
2180 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
2181 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
2184 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
2185 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
2188 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
2189 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
2192 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
2193 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
2196 /* Other clones which beat all the above checks */
2197 else if (bcdDevice == 0x0134 &&
2198 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
2199 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
2203 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
2205 /* Generally these clones have big discrepancies between
2206 * advertised features and what's actually supported.
2207 * Probably will need to be expanded in the future;
2208 * without these the controller will lock up.
2210 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2211 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
2212 set_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks);
2213 set_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks);
2215 /* Clear the reset quirk since this is not an actual
2216 * early Bluetooth 1.1 device from CSR.
2218 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2219 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2222 * Special workaround for these BT 4.0 chip clones, and potentially more:
2224 * - 0x0134: a Barrot 8041a02 (HCI rev: 0x0810 sub: 0x1012)
2225 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
2227 * These controllers are really messed-up.
2229 * 1. Their bulk RX endpoint will never report any data unless
2230 * the device was suspended at least once (yes, really).
2231 * 2. They will not wakeup when autosuspended and receiving data
2232 * on their bulk RX endpoint from e.g. a keyboard or mouse
2233 * (IOW remote-wakeup support is broken for the bulk endpoint).
2235 * To fix 1. enable runtime-suspend, force-suspend the
2236 * HCI and then wake-it up by disabling runtime-suspend.
2238 * To fix 2. clear the HCI's can_wake flag, this way the HCI
2239 * will still be autosuspended when it is not open.
2243 * Because these are widespread problems we prefer generic solutions; so
2244 * apply this initialization quirk to every controller that gets here,
2245 * it should be harmless. The alternative is to not work at all.
2247 pm_runtime_allow(&data->udev->dev);
2249 ret = pm_runtime_suspend(&data->udev->dev);
2253 bt_dev_warn(hdev, "CSR: Couldn't suspend the device for our Barrot 8041a02 receive-issue workaround");
2255 pm_runtime_forbid(&data->udev->dev);
2257 device_set_wakeup_capable(&data->udev->dev, false);
2259 /* Re-enable autosuspend if this was requested */
2260 if (enable_autosuspend)
2261 usb_enable_autosuspend(data->udev);
2269 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2271 struct sk_buff *skb;
2272 struct hci_event_hdr *hdr;
2273 struct hci_ev_cmd_complete *evt;
2275 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2279 hdr = skb_put(skb, sizeof(*hdr));
2280 hdr->evt = HCI_EV_CMD_COMPLETE;
2281 hdr->plen = sizeof(*evt) + 1;
2283 evt = skb_put(skb, sizeof(*evt));
2285 evt->opcode = cpu_to_le16(opcode);
2287 skb_put_u8(skb, 0x00);
2289 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2291 return hci_recv_frame(hdev, skb);
2294 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2297 struct hci_dev *hdev = data->hdev;
2299 /* When the device is in bootloader mode, then it can send
2300 * events via the bulk endpoint. These events are treated the
2301 * same way as the ones received from the interrupt endpoint.
2303 if (btintel_test_flag(hdev, INTEL_BOOTLOADER))
2304 return btusb_recv_intr(data, buffer, count);
2306 return btusb_recv_bulk(data, buffer, count);
2309 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2311 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2312 struct hci_event_hdr *hdr = (void *)skb->data;
2314 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2316 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2317 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
2319 switch (skb->data[2]) {
2321 /* When switching to the operational firmware
2322 * the device sends a vendor specific event
2323 * indicating that the bootup completed.
2325 btintel_bootup(hdev, ptr, len);
2328 /* When the firmware loading completes the
2329 * device sends out a vendor specific event
2330 * indicating the result of the firmware
2333 btintel_secure_send_result(hdev, ptr, len);
2339 return hci_recv_frame(hdev, skb);
2342 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2346 BT_DBG("%s", hdev->name);
2348 switch (hci_skb_pkt_type(skb)) {
2349 case HCI_COMMAND_PKT:
2350 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2351 struct hci_command_hdr *cmd = (void *)skb->data;
2352 __u16 opcode = le16_to_cpu(cmd->opcode);
2354 /* When in bootloader mode and the command 0xfc09
2355 * is received, it needs to be send down the
2356 * bulk endpoint. So allocate a bulk URB instead.
2358 if (opcode == 0xfc09)
2359 urb = alloc_bulk_urb(hdev, skb);
2361 urb = alloc_ctrl_urb(hdev, skb);
2363 /* When the 0xfc01 command is issued to boot into
2364 * the operational firmware, it will actually not
2365 * send a command complete event. To keep the flow
2366 * control working inject that event here.
2368 if (opcode == 0xfc01)
2369 inject_cmd_complete(hdev, opcode);
2371 urb = alloc_ctrl_urb(hdev, skb);
2374 return PTR_ERR(urb);
2376 hdev->stat.cmd_tx++;
2377 return submit_or_queue_tx_urb(hdev, urb);
2379 case HCI_ACLDATA_PKT:
2380 urb = alloc_bulk_urb(hdev, skb);
2382 return PTR_ERR(urb);
2384 hdev->stat.acl_tx++;
2385 return submit_or_queue_tx_urb(hdev, urb);
2387 case HCI_SCODATA_PKT:
2388 if (hci_conn_num(hdev, SCO_LINK) < 1)
2391 urb = alloc_isoc_urb(hdev, skb);
2393 return PTR_ERR(urb);
2395 hdev->stat.sco_tx++;
2396 return submit_tx_urb(hdev, urb);
2398 case HCI_ISODATA_PKT:
2399 urb = alloc_bulk_urb(hdev, skb);
2401 return PTR_ERR(urb);
2403 return submit_or_queue_tx_urb(hdev, urb);
2409 /* UHW CR mapping */
2410 #define MTK_BT_MISC 0x70002510
2411 #define MTK_BT_SUBSYS_RST 0x70002610
2412 #define MTK_UDMA_INT_STA_BT 0x74000024
2413 #define MTK_UDMA_INT_STA_BT1 0x74000308
2414 #define MTK_BT_WDT_STATUS 0x740003A0
2415 #define MTK_EP_RST_OPT 0x74011890
2416 #define MTK_EP_RST_IN_OUT_OPT 0x00010001
2417 #define MTK_BT_RST_DONE 0x00000100
2418 #define MTK_BT_RESET_WAIT_MS 100
2419 #define MTK_BT_RESET_NUM_TRIES 10
2421 static void btusb_mtk_wmt_recv(struct urb *urb)
2423 struct hci_dev *hdev = urb->context;
2424 struct btusb_data *data = hci_get_drvdata(hdev);
2425 struct sk_buff *skb;
2428 if (urb->status == 0 && urb->actual_length > 0) {
2429 hdev->stat.byte_rx += urb->actual_length;
2431 /* WMT event shouldn't be fragmented and the size should be
2432 * less than HCI_WMT_MAX_EVENT_SIZE.
2434 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2436 hdev->stat.err_rx++;
2437 kfree(urb->setup_packet);
2441 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2442 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2444 /* When someone waits for the WMT event, the skb is being cloned
2445 * and being processed the events from there then.
2447 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
2448 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
2449 if (!data->evt_skb) {
2451 kfree(urb->setup_packet);
2456 err = hci_recv_frame(hdev, skb);
2458 kfree_skb(data->evt_skb);
2459 data->evt_skb = NULL;
2460 kfree(urb->setup_packet);
2464 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2466 /* Barrier to sync with other CPUs */
2467 smp_mb__after_atomic();
2468 wake_up_bit(&data->flags,
2469 BTUSB_TX_WAIT_VND_EVT);
2471 kfree(urb->setup_packet);
2473 } else if (urb->status == -ENOENT) {
2474 /* Avoid suspend failed when usb_kill_urb */
2478 usb_mark_last_busy(data->udev);
2480 /* The URB complete handler is still called with urb->actual_length = 0
2481 * when the event is not available, so we should keep re-submitting
2482 * URB until WMT event returns, Also, It's necessary to wait some time
2483 * between the two consecutive control URBs to relax the target device
2484 * to generate the event. Otherwise, the WMT event cannot return from
2485 * the device successfully.
2489 usb_anchor_urb(urb, &data->ctrl_anchor);
2490 err = usb_submit_urb(urb, GFP_ATOMIC);
2492 kfree(urb->setup_packet);
2493 /* -EPERM: urb is being killed;
2494 * -ENODEV: device got disconnected
2496 if (err != -EPERM && err != -ENODEV)
2497 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2499 usb_unanchor_urb(urb);
2503 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2505 struct btusb_data *data = hci_get_drvdata(hdev);
2506 struct usb_ctrlrequest *dr;
2512 urb = usb_alloc_urb(0, GFP_KERNEL);
2516 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2522 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2524 dr->wIndex = cpu_to_le16(0);
2525 dr->wValue = cpu_to_le16(48);
2526 dr->wLength = cpu_to_le16(size);
2528 buf = kmalloc(size, GFP_KERNEL);
2535 pipe = usb_rcvctrlpipe(data->udev, 0);
2537 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2538 buf, size, btusb_mtk_wmt_recv, hdev);
2540 urb->transfer_flags |= URB_FREE_BUFFER;
2542 usb_anchor_urb(urb, &data->ctrl_anchor);
2543 err = usb_submit_urb(urb, GFP_KERNEL);
2545 if (err != -EPERM && err != -ENODEV)
2546 bt_dev_err(hdev, "urb %p submission failed (%d)",
2548 usb_unanchor_urb(urb);
2556 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
2557 struct btmtk_hci_wmt_params *wmt_params)
2559 struct btusb_data *data = hci_get_drvdata(hdev);
2560 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
2561 u32 hlen, status = BTMTK_WMT_INVALID;
2562 struct btmtk_hci_wmt_evt *wmt_evt;
2563 struct btmtk_hci_wmt_cmd *wc;
2564 struct btmtk_wmt_hdr *hdr;
2567 /* Send the WMT command and wait until the WMT event returns */
2568 hlen = sizeof(*hdr) + wmt_params->dlen;
2572 wc = kzalloc(hlen, GFP_KERNEL);
2578 hdr->op = wmt_params->op;
2579 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
2580 hdr->flag = wmt_params->flag;
2581 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
2583 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2585 /* WMT cmd/event doesn't follow up the generic HCI cmd/event handling,
2586 * it needs constantly polling control pipe until the host received the
2587 * WMT event, thus, we should require to specifically acquire PM counter
2588 * on the USB to prevent the interface from entering auto suspended
2589 * while WMT cmd/event in progress.
2591 err = usb_autopm_get_interface(data->intf);
2595 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
2598 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2599 usb_autopm_put_interface(data->intf);
2603 /* Submit control IN URB on demand to process the WMT event */
2604 err = btusb_mtk_submit_wmt_recv_urb(hdev);
2606 usb_autopm_put_interface(data->intf);
2611 /* The vendor specific WMT commands are all answered by a vendor
2612 * specific event and will have the Command Status or Command
2613 * Complete as with usual HCI command flow control.
2615 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
2616 * state to be cleared. The driver specific event receive routine
2617 * will clear that state and with that indicate completion of the
2620 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
2621 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
2622 if (err == -EINTR) {
2623 bt_dev_err(hdev, "Execution of wmt command interrupted");
2624 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2629 bt_dev_err(hdev, "Execution of wmt command timed out");
2630 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2635 /* Parse and handle the return WMT event */
2636 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
2637 if (wmt_evt->whdr.op != hdr->op) {
2638 bt_dev_err(hdev, "Wrong op received %d expected %d",
2639 wmt_evt->whdr.op, hdr->op);
2644 switch (wmt_evt->whdr.op) {
2645 case BTMTK_WMT_SEMAPHORE:
2646 if (wmt_evt->whdr.flag == 2)
2647 status = BTMTK_WMT_PATCH_UNDONE;
2649 status = BTMTK_WMT_PATCH_DONE;
2651 case BTMTK_WMT_FUNC_CTRL:
2652 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
2653 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
2654 status = BTMTK_WMT_ON_DONE;
2655 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
2656 status = BTMTK_WMT_ON_PROGRESS;
2658 status = BTMTK_WMT_ON_UNDONE;
2660 case BTMTK_WMT_PATCH_DWNLD:
2661 if (wmt_evt->whdr.flag == 2)
2662 status = BTMTK_WMT_PATCH_DONE;
2663 else if (wmt_evt->whdr.flag == 1)
2664 status = BTMTK_WMT_PATCH_PROGRESS;
2666 status = BTMTK_WMT_PATCH_UNDONE;
2670 if (wmt_params->status)
2671 *wmt_params->status = status;
2674 kfree_skb(data->evt_skb);
2675 data->evt_skb = NULL;
2681 static int btusb_mtk_func_query(struct hci_dev *hdev)
2683 struct btmtk_hci_wmt_params wmt_params;
2687 /* Query whether the function is enabled */
2688 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2689 wmt_params.flag = 4;
2690 wmt_params.dlen = sizeof(param);
2691 wmt_params.data = ¶m;
2692 wmt_params.status = &status;
2694 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2696 bt_dev_err(hdev, "Failed to query function status (%d)", err);
2703 static int btusb_mtk_uhw_reg_write(struct btusb_data *data, u32 reg, u32 val)
2705 struct hci_dev *hdev = data->hdev;
2709 buf = kzalloc(4, GFP_KERNEL);
2713 put_unaligned_le32(val, buf);
2715 pipe = usb_sndctrlpipe(data->udev, 0);
2716 err = usb_control_msg(data->udev, pipe, 0x02,
2718 reg >> 16, reg & 0xffff,
2719 buf, 4, USB_CTRL_SET_TIMEOUT);
2721 bt_dev_err(hdev, "Failed to write uhw reg(%d)", err);
2731 static int btusb_mtk_uhw_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2733 struct hci_dev *hdev = data->hdev;
2737 buf = kzalloc(4, GFP_KERNEL);
2741 pipe = usb_rcvctrlpipe(data->udev, 0);
2742 err = usb_control_msg(data->udev, pipe, 0x01,
2744 reg >> 16, reg & 0xffff,
2745 buf, 4, USB_CTRL_SET_TIMEOUT);
2747 bt_dev_err(hdev, "Failed to read uhw reg(%d)", err);
2751 *val = get_unaligned_le32(buf);
2752 bt_dev_dbg(hdev, "reg=%x, value=0x%08x", reg, *val);
2760 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2762 int pipe, err, size = sizeof(u32);
2765 buf = kzalloc(size, GFP_KERNEL);
2769 pipe = usb_rcvctrlpipe(data->udev, 0);
2770 err = usb_control_msg(data->udev, pipe, 0x63,
2771 USB_TYPE_VENDOR | USB_DIR_IN,
2772 reg >> 16, reg & 0xffff,
2773 buf, size, USB_CTRL_SET_TIMEOUT);
2777 *val = get_unaligned_le32(buf);
2785 static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
2787 return btusb_mtk_reg_read(data, reg, id);
2790 static int btusb_mtk_setup(struct hci_dev *hdev)
2792 struct btusb_data *data = hci_get_drvdata(hdev);
2793 struct btmtk_hci_wmt_params wmt_params;
2794 ktime_t calltime, delta, rettime;
2795 struct btmtk_tci_sleep tci_sleep;
2796 unsigned long long duration;
2797 struct sk_buff *skb;
2801 char fw_bin_name[64];
2805 calltime = ktime_get();
2807 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
2809 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2814 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
2816 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2819 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
2821 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
2828 fwname = FIRMWARE_MT7663;
2831 fwname = FIRMWARE_MT7668;
2835 snprintf(fw_bin_name, sizeof(fw_bin_name),
2837 dev_id & 0xffff, (fw_version & 0xff) + 1);
2838 err = btmtk_setup_firmware_79xx(hdev, fw_bin_name,
2839 btusb_mtk_hci_wmt_sync);
2841 bt_dev_err(hdev, "Failed to set up firmware (%d)", err);
2845 /* It's Device EndPoint Reset Option Register */
2846 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2848 /* Enable Bluetooth protocol */
2850 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2851 wmt_params.flag = 0;
2852 wmt_params.dlen = sizeof(param);
2853 wmt_params.data = ¶m;
2854 wmt_params.status = NULL;
2856 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2858 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2862 hci_set_msft_opcode(hdev, 0xFD30);
2863 hci_set_aosp_capable(hdev);
2866 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
2871 /* Query whether the firmware is already download */
2872 wmt_params.op = BTMTK_WMT_SEMAPHORE;
2873 wmt_params.flag = 1;
2874 wmt_params.dlen = 0;
2875 wmt_params.data = NULL;
2876 wmt_params.status = &status;
2878 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2880 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
2884 if (status == BTMTK_WMT_PATCH_DONE) {
2885 bt_dev_info(hdev, "firmware already downloaded");
2886 goto ignore_setup_fw;
2889 /* Setup a firmware which the device definitely requires */
2890 err = btmtk_setup_firmware(hdev, fwname,
2891 btusb_mtk_hci_wmt_sync);
2896 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
2897 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
2899 /* -ETIMEDOUT happens */
2903 /* The other errors happen in btusb_mtk_func_query */
2907 if (status == BTMTK_WMT_ON_DONE) {
2908 bt_dev_info(hdev, "function already on");
2909 goto ignore_func_on;
2912 /* Enable Bluetooth protocol */
2914 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2915 wmt_params.flag = 0;
2916 wmt_params.dlen = sizeof(param);
2917 wmt_params.data = ¶m;
2918 wmt_params.status = NULL;
2920 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2922 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2927 /* Apply the low power environment setup */
2928 tci_sleep.mode = 0x5;
2929 tci_sleep.duration = cpu_to_le16(0x640);
2930 tci_sleep.host_duration = cpu_to_le16(0x640);
2931 tci_sleep.host_wakeup_pin = 0;
2932 tci_sleep.time_compensation = 0;
2934 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
2938 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
2944 rettime = ktime_get();
2945 delta = ktime_sub(rettime, calltime);
2946 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2948 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
2953 static int btusb_mtk_shutdown(struct hci_dev *hdev)
2955 struct btmtk_hci_wmt_params wmt_params;
2959 /* Disable the device */
2960 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2961 wmt_params.flag = 0;
2962 wmt_params.dlen = sizeof(param);
2963 wmt_params.data = ¶m;
2964 wmt_params.status = NULL;
2966 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2968 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2975 static void btusb_mtk_cmd_timeout(struct hci_dev *hdev)
2977 struct btusb_data *data = hci_get_drvdata(hdev);
2981 /* It's MediaTek specific bluetooth reset mechanism via USB */
2982 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
2983 bt_dev_err(hdev, "last reset failed? Not resetting again");
2987 err = usb_autopm_get_interface(data->intf);
2991 btusb_stop_traffic(data);
2992 usb_kill_anchored_urbs(&data->tx_anchor);
2994 /* It's Device EndPoint Reset Option Register */
2995 bt_dev_dbg(hdev, "Initiating reset mechanism via uhw");
2996 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2997 btusb_mtk_uhw_reg_read(data, MTK_BT_WDT_STATUS, &val);
2999 /* Reset the bluetooth chip via USB interface. */
3000 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 1);
3001 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT, 0x000000FF);
3002 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT, &val);
3003 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT1, 0x000000FF);
3004 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT1, &val);
3005 /* MT7921 need to delay 20ms between toggle reset bit */
3007 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 0);
3008 btusb_mtk_uhw_reg_read(data, MTK_BT_SUBSYS_RST, &val);
3010 /* Poll the register until reset is completed */
3012 btusb_mtk_uhw_reg_read(data, MTK_BT_MISC, &val);
3013 if (val & MTK_BT_RST_DONE) {
3014 bt_dev_dbg(hdev, "Bluetooth Reset Successfully");
3018 bt_dev_dbg(hdev, "Polling Bluetooth Reset CR");
3020 msleep(MTK_BT_RESET_WAIT_MS);
3021 } while (retry < MTK_BT_RESET_NUM_TRIES);
3023 btusb_mtk_id_get(data, 0x70010200, &val);
3025 bt_dev_err(hdev, "Can't get device id, subsys reset fail.");
3027 usb_queue_reset_device(data->intf);
3029 clear_bit(BTUSB_HW_RESET_ACTIVE, &data->flags);
3032 static int btusb_recv_acl_mtk(struct hci_dev *hdev, struct sk_buff *skb)
3034 struct btusb_data *data = hci_get_drvdata(hdev);
3035 u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
3038 case 0xfc6f: /* Firmware dump from device */
3039 /* When the firmware hangs, the device can no longer
3040 * suspend and thus disable auto-suspend.
3042 usb_disable_autosuspend(data->udev);
3044 case 0x05ff: /* Firmware debug logging 1 */
3045 case 0x05fe: /* Firmware debug logging 2 */
3046 return hci_recv_diag(hdev, skb);
3049 return hci_recv_frame(hdev, skb);
3053 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3054 static int marvell_config_oob_wake(struct hci_dev *hdev)
3056 struct sk_buff *skb;
3057 struct btusb_data *data = hci_get_drvdata(hdev);
3058 struct device *dev = &data->udev->dev;
3059 u16 pin, gap, opcode;
3063 /* Move on if no wakeup pin specified */
3064 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3065 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3068 /* Vendor specific command to configure a GPIO as wake-up pin */
3069 opcode = hci_opcode_pack(0x3F, 0x59);
3070 cmd[0] = opcode & 0xFF;
3071 cmd[1] = opcode >> 8;
3072 cmd[2] = 2; /* length of parameters that follow */
3074 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3076 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3078 bt_dev_err(hdev, "%s: No memory", __func__);
3082 skb_put_data(skb, cmd, sizeof(cmd));
3083 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3085 ret = btusb_send_frame(hdev, skb);
3087 bt_dev_err(hdev, "%s: configuration failed", __func__);
3096 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3097 const bdaddr_t *bdaddr)
3099 struct sk_buff *skb;
3104 buf[1] = sizeof(bdaddr_t);
3105 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3107 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3110 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3119 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3120 const bdaddr_t *bdaddr)
3122 struct sk_buff *skb;
3129 buf[3] = sizeof(bdaddr_t);
3130 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3132 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3135 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3143 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3144 const bdaddr_t *bdaddr)
3146 struct sk_buff *skb;
3150 memcpy(buf, bdaddr, sizeof(bdaddr_t));
3152 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3153 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3156 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3164 #define QCA_DFU_PACKET_LEN 4096
3166 #define QCA_GET_TARGET_VERSION 0x09
3167 #define QCA_CHECK_STATUS 0x05
3168 #define QCA_DFU_DOWNLOAD 0x01
3170 #define QCA_SYSCFG_UPDATED 0x40
3171 #define QCA_PATCH_UPDATED 0x80
3172 #define QCA_DFU_TIMEOUT 3000
3173 #define QCA_FLAG_MULTI_NVM 0x80
3174 #define QCA_BT_RESET_WAIT_MS 100
3176 #define WCN6855_2_0_RAM_VERSION_GF 0x400c1200
3177 #define WCN6855_2_1_RAM_VERSION_GF 0x400c1211
3179 struct qca_version {
3181 __le32 patch_version;
3189 struct qca_rampatch_version {
3190 __le16 rom_version_high;
3191 __le16 rom_version_low;
3192 __le16 patch_version;
3195 struct qca_device_info {
3197 u8 rampatch_hdr; /* length of header in rampatch */
3198 u8 nvm_hdr; /* length of header in NVM */
3199 u8 ver_offset; /* offset of version structure in rampatch */
3202 static const struct qca_device_info qca_devices_table[] = {
3203 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3204 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3205 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3206 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3207 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3208 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3209 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3210 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3211 { 0x00130201, 40, 4, 16 }, /* WCN6855 2.1 */
3212 { 0x00190200, 40, 4, 16 }, /* WCN785x 2.0 */
3215 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3216 void *data, u16 size)
3221 buf = kmalloc(size, GFP_KERNEL);
3225 /* Found some of USB hosts have IOT issues with ours so that we should
3226 * not wait until HCI layer is ready.
3228 pipe = usb_rcvctrlpipe(udev, 0);
3229 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3230 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3232 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3236 memcpy(data, buf, size);
3244 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3245 const struct firmware *firmware,
3248 struct btusb_data *btdata = hci_get_drvdata(hdev);
3249 struct usb_device *udev = btdata->udev;
3250 size_t count, size, sent = 0;
3254 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3258 count = firmware->size;
3260 size = min_t(size_t, count, hdr_size);
3261 memcpy(buf, firmware->data, size);
3263 /* USB patches should go down to controller through USB path
3264 * because binary format fits to go down through USB channel.
3265 * USB control path is for patching headers and USB bulk is for
3268 pipe = usb_sndctrlpipe(udev, 0);
3269 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3270 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3272 bt_dev_err(hdev, "Failed to send headers (%d)", err);
3279 /* ep2 need time to switch from function acl to function dfu,
3280 * so we add 20ms delay here.
3285 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3287 memcpy(buf, firmware->data + sent, size);
3289 pipe = usb_sndbulkpipe(udev, 0x02);
3290 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3293 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3294 sent, firmware->size, err);
3299 bt_dev_err(hdev, "Failed to get bulk buffer");
3313 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3314 struct qca_version *ver,
3315 const struct qca_device_info *info)
3317 struct qca_rampatch_version *rver;
3318 const struct firmware *fw;
3319 u32 ver_rom, ver_patch, rver_rom;
3320 u16 rver_rom_low, rver_rom_high, rver_patch;
3324 ver_rom = le32_to_cpu(ver->rom_version);
3325 ver_patch = le32_to_cpu(ver->patch_version);
3327 snprintf(fwname, sizeof(fwname), "/*(DEBLOBBED)*/", ver_rom);
3329 err = reject_firmware(&fw, fwname, &hdev->dev);
3331 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3336 bt_dev_info(hdev, "using rampatch file: %s", fwname);
3338 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3339 rver_rom_low = le16_to_cpu(rver->rom_version_low);
3340 rver_patch = le16_to_cpu(rver->patch_version);
3342 if (ver_rom & ~0xffffU) {
3343 rver_rom_high = le16_to_cpu(rver->rom_version_high);
3344 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
3346 rver_rom = rver_rom_low;
3349 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3350 "firmware rome 0x%x build 0x%x",
3351 rver_rom, rver_patch, ver_rom, ver_patch);
3353 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
3354 bt_dev_err(hdev, "rampatch file version did not match with firmware");
3359 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3362 release_firmware(fw);
3367 static void btusb_generate_qca_nvm_name(char *fwname, size_t max_size,
3368 const struct qca_version *ver)
3370 u32 rom_version = le32_to_cpu(ver->rom_version);
3371 u16 flag = le16_to_cpu(ver->flag);
3373 if (((flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
3374 /* The board_id should be split into two bytes
3375 * The 1st byte is chip ID, and the 2nd byte is platform ID
3376 * For example, board ID 0x010A, 0x01 is platform ID. 0x0A is chip ID
3377 * we have several platforms, and platform IDs are continuously added
3379 * 0x00 is for Mobile
3381 * 0x02 is for Automotive
3382 * 0x03 is for Consumer electronic
3384 u16 board_id = (ver->chip_id << 8) + ver->platform_id;
3385 const char *variant;
3387 switch (le32_to_cpu(ver->ram_version)) {
3388 case WCN6855_2_0_RAM_VERSION_GF:
3389 case WCN6855_2_1_RAM_VERSION_GF:
3397 if (board_id == 0) {
3398 snprintf(fwname, max_size, "/*(DEBLOBBED)*/",
3399 rom_version, variant);
3401 snprintf(fwname, max_size, "/*(DEBLOBBED)*/",
3402 rom_version, variant, board_id);
3405 snprintf(fwname, max_size, "/*(DEBLOBBED)*/",
3411 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3412 struct qca_version *ver,
3413 const struct qca_device_info *info)
3415 const struct firmware *fw;
3419 btusb_generate_qca_nvm_name(fwname, sizeof(fwname), ver);
3421 err = reject_firmware(&fw, fwname, &hdev->dev);
3423 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3428 bt_dev_info(hdev, "using NVM file: %s", fwname);
3430 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3432 release_firmware(fw);
3437 /* identify the ROM version and check whether patches are needed */
3438 static bool btusb_qca_need_patch(struct usb_device *udev)
3440 struct qca_version ver;
3442 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3445 /* only low ROM versions need patches */
3446 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3449 static int btusb_setup_qca(struct hci_dev *hdev)
3451 struct btusb_data *btdata = hci_get_drvdata(hdev);
3452 struct usb_device *udev = btdata->udev;
3453 const struct qca_device_info *info = NULL;
3454 struct qca_version ver;
3459 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3464 ver_rom = le32_to_cpu(ver.rom_version);
3466 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
3467 if (ver_rom == qca_devices_table[i].rom_version)
3468 info = &qca_devices_table[i];
3471 /* If the rom_version is not matched in the qca_devices_table
3472 * and the high ROM version is not zero, we assume this chip no
3473 * need to load the rampatch and nvm.
3475 if (ver_rom & ~0xffffU)
3478 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3482 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3487 if (!(status & QCA_PATCH_UPDATED)) {
3488 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3493 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3498 if (!(status & QCA_SYSCFG_UPDATED)) {
3499 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3503 /* WCN6855 2.1 and later will reset to apply firmware downloaded here, so
3504 * wait ~100ms for reset Done then go ahead, otherwise, it maybe
3505 * cause potential enable failure.
3507 if (info->rom_version >= 0x00130201)
3508 msleep(QCA_BT_RESET_WAIT_MS);
3511 /* Mark HCI_OP_ENHANCED_SETUP_SYNC_CONN as broken as it doesn't seem to
3512 * work with the likes of HSP/HFP mSBC.
3514 set_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &hdev->quirks);
3519 static inline int __set_diag_interface(struct hci_dev *hdev)
3521 struct btusb_data *data = hci_get_drvdata(hdev);
3522 struct usb_interface *intf = data->diag;
3528 data->diag_tx_ep = NULL;
3529 data->diag_rx_ep = NULL;
3531 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3532 struct usb_endpoint_descriptor *ep_desc;
3534 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3536 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3537 data->diag_tx_ep = ep_desc;
3541 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3542 data->diag_rx_ep = ep_desc;
3547 if (!data->diag_tx_ep || !data->diag_rx_ep) {
3548 bt_dev_err(hdev, "invalid diagnostic descriptors");
3555 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3557 struct btusb_data *data = hci_get_drvdata(hdev);
3558 struct sk_buff *skb;
3562 if (!data->diag_tx_ep)
3563 return ERR_PTR(-ENODEV);
3565 urb = usb_alloc_urb(0, GFP_KERNEL);
3567 return ERR_PTR(-ENOMEM);
3569 skb = bt_skb_alloc(2, GFP_KERNEL);
3572 return ERR_PTR(-ENOMEM);
3575 skb_put_u8(skb, 0xf0);
3576 skb_put_u8(skb, enable);
3578 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3580 usb_fill_bulk_urb(urb, data->udev, pipe,
3581 skb->data, skb->len, btusb_tx_complete, skb);
3583 skb->dev = (void *)hdev;
3588 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3590 struct btusb_data *data = hci_get_drvdata(hdev);
3596 if (!test_bit(HCI_RUNNING, &hdev->flags))
3599 urb = alloc_diag_urb(hdev, enable);
3601 return PTR_ERR(urb);
3603 return submit_or_queue_tx_urb(hdev, urb);
3607 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3609 struct btusb_data *data = priv;
3611 pm_wakeup_event(&data->udev->dev, 0);
3614 /* Disable only if not already disabled (keep it balanced) */
3615 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3616 disable_irq_nosync(irq);
3617 disable_irq_wake(irq);
3622 static const struct of_device_id btusb_match_table[] = {
3623 { .compatible = "usb1286,204e" },
3624 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3625 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
3628 MODULE_DEVICE_TABLE(of, btusb_match_table);
3630 /* Use an oob wakeup pin? */
3631 static int btusb_config_oob_wake(struct hci_dev *hdev)
3633 struct btusb_data *data = hci_get_drvdata(hdev);
3634 struct device *dev = &data->udev->dev;
3637 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3639 if (!of_match_device(btusb_match_table, dev))
3642 /* Move on if no IRQ specified */
3643 irq = of_irq_get_byname(dev->of_node, "wakeup");
3645 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
3649 irq_set_status_flags(irq, IRQ_NOAUTOEN);
3650 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
3651 0, "OOB Wake-on-BT", data);
3653 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
3657 ret = device_init_wakeup(dev, true);
3659 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
3663 data->oob_wake_irq = irq;
3664 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
3669 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
3671 if (dmi_check_system(btusb_needs_reset_resume_table))
3672 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3675 static bool btusb_wakeup(struct hci_dev *hdev)
3677 struct btusb_data *data = hci_get_drvdata(hdev);
3679 return device_may_wakeup(&data->udev->dev);
3682 static int btusb_shutdown_qca(struct hci_dev *hdev)
3684 struct sk_buff *skb;
3686 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3688 bt_dev_err(hdev, "HCI reset during shutdown failed");
3689 return PTR_ERR(skb);
3696 static ssize_t force_poll_sync_read(struct file *file, char __user *user_buf,
3697 size_t count, loff_t *ppos)
3699 struct btusb_data *data = file->private_data;
3702 buf[0] = data->poll_sync ? 'Y' : 'N';
3705 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3708 static ssize_t force_poll_sync_write(struct file *file,
3709 const char __user *user_buf,
3710 size_t count, loff_t *ppos)
3712 struct btusb_data *data = file->private_data;
3716 err = kstrtobool_from_user(user_buf, count, &enable);
3720 /* Only allow changes while the adapter is down */
3721 if (test_bit(HCI_UP, &data->hdev->flags))
3724 if (data->poll_sync == enable)
3727 data->poll_sync = enable;
3732 static const struct file_operations force_poll_sync_fops = {
3733 .open = simple_open,
3734 .read = force_poll_sync_read,
3735 .write = force_poll_sync_write,
3736 .llseek = default_llseek,
3739 static int btusb_probe(struct usb_interface *intf,
3740 const struct usb_device_id *id)
3742 struct usb_endpoint_descriptor *ep_desc;
3743 struct gpio_desc *reset_gpio;
3744 struct btusb_data *data;
3745 struct hci_dev *hdev;
3746 unsigned ifnum_base;
3747 int i, err, priv_size;
3749 BT_DBG("intf %p id %p", intf, id);
3751 /* interface numbers are hardcoded in the spec */
3752 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
3753 if (!(id->driver_info & BTUSB_IFNUM_2))
3755 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
3759 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
3761 if (!id->driver_info) {
3762 const struct usb_device_id *match;
3764 match = usb_match_id(intf, blacklist_table);
3769 if (id->driver_info == BTUSB_IGNORE)
3772 if (id->driver_info & BTUSB_ATH3012) {
3773 struct usb_device *udev = interface_to_usbdev(intf);
3775 /* Old firmware would otherwise let ath3k driver load
3776 * patch and sysconfig files
3778 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
3779 !btusb_qca_need_patch(udev))
3783 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
3787 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3788 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3790 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3791 data->intr_ep = ep_desc;
3795 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3796 data->bulk_tx_ep = ep_desc;
3800 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3801 data->bulk_rx_ep = ep_desc;
3806 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
3809 if (id->driver_info & BTUSB_AMP) {
3810 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3811 data->cmdreq = 0x2b;
3813 data->cmdreq_type = USB_TYPE_CLASS;
3814 data->cmdreq = 0x00;
3817 data->udev = interface_to_usbdev(intf);
3820 INIT_WORK(&data->work, btusb_work);
3821 INIT_WORK(&data->waker, btusb_waker);
3822 INIT_DELAYED_WORK(&data->rx_work, btusb_rx_work);
3824 skb_queue_head_init(&data->acl_q);
3826 init_usb_anchor(&data->deferred);
3827 init_usb_anchor(&data->tx_anchor);
3828 spin_lock_init(&data->txlock);
3830 init_usb_anchor(&data->intr_anchor);
3831 init_usb_anchor(&data->bulk_anchor);
3832 init_usb_anchor(&data->isoc_anchor);
3833 init_usb_anchor(&data->diag_anchor);
3834 init_usb_anchor(&data->ctrl_anchor);
3835 spin_lock_init(&data->rxlock);
3839 data->recv_event = hci_recv_frame;
3840 data->recv_bulk = btusb_recv_bulk;
3842 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3843 /* Allocate extra space for Intel device */
3844 priv_size += sizeof(struct btintel_data);
3846 /* Override the rx handlers */
3847 data->recv_event = btusb_recv_event_intel;
3848 data->recv_bulk = btusb_recv_bulk_intel;
3851 data->recv_acl = hci_recv_frame;
3853 hdev = hci_alloc_dev_priv(priv_size);
3857 hdev->bus = HCI_USB;
3858 hci_set_drvdata(hdev, data);
3860 if (id->driver_info & BTUSB_AMP)
3861 hdev->dev_type = HCI_AMP;
3863 hdev->dev_type = HCI_PRIMARY;
3867 SET_HCIDEV_DEV(hdev, &intf->dev);
3869 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
3871 if (IS_ERR(reset_gpio)) {
3872 err = PTR_ERR(reset_gpio);
3874 } else if (reset_gpio) {
3875 data->reset_gpio = reset_gpio;
3878 hdev->open = btusb_open;
3879 hdev->close = btusb_close;
3880 hdev->flush = btusb_flush;
3881 hdev->send = btusb_send_frame;
3882 hdev->notify = btusb_notify;
3883 hdev->wakeup = btusb_wakeup;
3886 err = btusb_config_oob_wake(hdev);
3890 /* Marvell devices may need a specific chip configuration */
3891 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
3892 err = marvell_config_oob_wake(hdev);
3897 if (id->driver_info & BTUSB_CW6622)
3898 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3900 if (id->driver_info & BTUSB_BCM2045)
3901 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3903 if (id->driver_info & BTUSB_BCM92035)
3904 hdev->setup = btusb_setup_bcm92035;
3906 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3907 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
3908 hdev->manufacturer = 15;
3909 hdev->setup = btbcm_setup_patchram;
3910 hdev->set_diag = btusb_bcm_set_diag;
3911 hdev->set_bdaddr = btbcm_set_bdaddr;
3913 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3914 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3917 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3918 (id->driver_info & BTUSB_BCM_APPLE)) {
3919 hdev->manufacturer = 15;
3920 hdev->setup = btbcm_setup_apple;
3921 hdev->set_diag = btusb_bcm_set_diag;
3923 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3924 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3927 /* Combined Intel Device setup to support multiple setup routine */
3928 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3929 err = btintel_configure_setup(hdev);
3933 /* Transport specific configuration */
3934 hdev->send = btusb_send_frame_intel;
3935 hdev->cmd_timeout = btusb_intel_cmd_timeout;
3937 if (id->driver_info & BTUSB_INTEL_NO_WBS_SUPPORT)
3938 btintel_set_flag(hdev, INTEL_ROM_LEGACY_NO_WBS_SUPPORT);
3940 if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
3941 btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
3943 if (id->driver_info & BTUSB_INTEL_BROKEN_SHUTDOWN_LED)
3944 btintel_set_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED);
3947 if (id->driver_info & BTUSB_MARVELL)
3948 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3950 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
3951 (id->driver_info & BTUSB_MEDIATEK)) {
3952 hdev->setup = btusb_mtk_setup;
3953 hdev->shutdown = btusb_mtk_shutdown;
3954 hdev->manufacturer = 70;
3955 hdev->cmd_timeout = btusb_mtk_cmd_timeout;
3956 hdev->set_bdaddr = btmtk_set_bdaddr;
3957 set_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &hdev->quirks);
3958 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
3959 data->recv_acl = btusb_recv_acl_mtk;
3962 if (id->driver_info & BTUSB_SWAVE) {
3963 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
3964 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
3967 if (id->driver_info & BTUSB_INTEL_BOOT) {
3968 hdev->manufacturer = 2;
3969 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3972 if (id->driver_info & BTUSB_ATH3012) {
3973 data->setup_on_usb = btusb_setup_qca;
3974 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3975 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3976 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3979 if (id->driver_info & BTUSB_QCA_ROME) {
3980 data->setup_on_usb = btusb_setup_qca;
3981 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3982 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3983 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3984 btusb_check_needs_reset_resume(intf);
3987 if (id->driver_info & BTUSB_QCA_WCN6855) {
3988 data->setup_on_usb = btusb_setup_qca;
3989 hdev->shutdown = btusb_shutdown_qca;
3990 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
3991 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3992 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3993 hci_set_msft_opcode(hdev, 0xFD70);
3996 if (id->driver_info & BTUSB_AMP) {
3997 /* AMP controllers do not support SCO packets */
4000 /* Interface orders are hardcoded in the specification */
4001 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
4002 data->isoc_ifnum = ifnum_base + 1;
4005 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4006 (id->driver_info & BTUSB_REALTEK)) {
4007 hdev->setup = btrtl_setup_realtek;
4008 hdev->shutdown = btrtl_shutdown_realtek;
4009 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
4011 /* Realtek devices need to set remote wakeup on auto-suspend */
4012 set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
4013 set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
4016 if (id->driver_info & BTUSB_ACTIONS_SEMI) {
4017 /* Support is advertised, but not implemented */
4018 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
4022 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4024 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
4025 if (!disable_scofix)
4026 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
4029 if (id->driver_info & BTUSB_BROKEN_ISOC)
4032 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
4033 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
4035 if (id->driver_info & BTUSB_VALID_LE_STATES)
4036 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
4038 if (id->driver_info & BTUSB_DIGIANSWER) {
4039 data->cmdreq_type = USB_TYPE_VENDOR;
4040 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4043 if (id->driver_info & BTUSB_CSR) {
4044 struct usb_device *udev = data->udev;
4045 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
4047 /* Old firmware would otherwise execute USB reset */
4048 if (bcdDevice < 0x117)
4049 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4051 /* This must be set first in case we disable it for fakes */
4052 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4054 /* Fake CSR devices with broken commands */
4055 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
4056 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4057 hdev->setup = btusb_setup_csr;
4060 if (id->driver_info & BTUSB_SNIFFER) {
4061 struct usb_device *udev = data->udev;
4063 /* New sniffer firmware has crippled HCI interface */
4064 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4065 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4068 if (id->driver_info & BTUSB_INTEL_BOOT) {
4069 /* A bug in the bootloader causes that interrupt interface is
4070 * only enabled after receiving SetInterface(0, AltSetting=0).
4072 err = usb_set_interface(data->udev, 0, 0);
4074 BT_ERR("failed to set interface 0, alt 0 %d", err);
4080 err = usb_driver_claim_interface(&btusb_driver,
4086 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
4087 if (!usb_driver_claim_interface(&btusb_driver,
4089 __set_diag_interface(hdev);
4094 if (enable_autosuspend)
4095 usb_enable_autosuspend(data->udev);
4097 err = hci_register_dev(hdev);
4101 usb_set_intfdata(intf, data);
4103 debugfs_create_file("force_poll_sync", 0644, hdev->debugfs, data,
4104 &force_poll_sync_fops);
4109 if (data->reset_gpio)
4110 gpiod_put(data->reset_gpio);
4115 static void btusb_disconnect(struct usb_interface *intf)
4117 struct btusb_data *data = usb_get_intfdata(intf);
4118 struct hci_dev *hdev;
4120 BT_DBG("intf %p", intf);
4126 usb_set_intfdata(data->intf, NULL);
4129 usb_set_intfdata(data->isoc, NULL);
4132 usb_set_intfdata(data->diag, NULL);
4134 hci_unregister_dev(hdev);
4136 if (intf == data->intf) {
4138 usb_driver_release_interface(&btusb_driver, data->isoc);
4140 usb_driver_release_interface(&btusb_driver, data->diag);
4141 } else if (intf == data->isoc) {
4143 usb_driver_release_interface(&btusb_driver, data->diag);
4144 usb_driver_release_interface(&btusb_driver, data->intf);
4145 } else if (intf == data->diag) {
4146 usb_driver_release_interface(&btusb_driver, data->intf);
4148 usb_driver_release_interface(&btusb_driver, data->isoc);
4151 if (data->oob_wake_irq)
4152 device_init_wakeup(&data->udev->dev, false);
4154 if (data->reset_gpio)
4155 gpiod_put(data->reset_gpio);
4161 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4163 struct btusb_data *data = usb_get_intfdata(intf);
4165 BT_DBG("intf %p", intf);
4167 if (data->suspend_count++)
4170 spin_lock_irq(&data->txlock);
4171 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4172 set_bit(BTUSB_SUSPENDING, &data->flags);
4173 spin_unlock_irq(&data->txlock);
4175 spin_unlock_irq(&data->txlock);
4176 data->suspend_count--;
4180 cancel_work_sync(&data->work);
4182 btusb_stop_traffic(data);
4183 usb_kill_anchored_urbs(&data->tx_anchor);
4185 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4186 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4187 enable_irq_wake(data->oob_wake_irq);
4188 enable_irq(data->oob_wake_irq);
4191 /* For global suspend, Realtek devices lose the loaded fw
4192 * in them. But for autosuspend, firmware should remain.
4193 * Actually, it depends on whether the usb host sends
4194 * set feature (enable wakeup) or not.
4196 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
4197 if (PMSG_IS_AUTO(message) &&
4198 device_can_wakeup(&data->udev->dev))
4199 data->udev->do_remote_wakeup = 1;
4200 else if (!PMSG_IS_AUTO(message) &&
4201 !device_may_wakeup(&data->udev->dev)) {
4202 data->udev->do_remote_wakeup = 0;
4203 data->udev->reset_resume = 1;
4210 static void play_deferred(struct btusb_data *data)
4215 while ((urb = usb_get_from_anchor(&data->deferred))) {
4216 usb_anchor_urb(urb, &data->tx_anchor);
4218 err = usb_submit_urb(urb, GFP_ATOMIC);
4220 if (err != -EPERM && err != -ENODEV)
4221 BT_ERR("%s urb %p submission failed (%d)",
4222 data->hdev->name, urb, -err);
4223 kfree(urb->setup_packet);
4224 usb_unanchor_urb(urb);
4229 data->tx_in_flight++;
4233 /* Cleanup the rest deferred urbs. */
4234 while ((urb = usb_get_from_anchor(&data->deferred))) {
4235 kfree(urb->setup_packet);
4240 static int btusb_resume(struct usb_interface *intf)
4242 struct btusb_data *data = usb_get_intfdata(intf);
4243 struct hci_dev *hdev = data->hdev;
4246 BT_DBG("intf %p", intf);
4248 if (--data->suspend_count)
4251 /* Disable only if not already disabled (keep it balanced) */
4252 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4253 disable_irq(data->oob_wake_irq);
4254 disable_irq_wake(data->oob_wake_irq);
4257 if (!test_bit(HCI_RUNNING, &hdev->flags))
4260 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4261 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4263 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4268 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4269 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4271 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4275 btusb_submit_bulk_urb(hdev, GFP_NOIO);
4278 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4279 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4280 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4282 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4285 spin_lock_irq(&data->txlock);
4286 play_deferred(data);
4287 clear_bit(BTUSB_SUSPENDING, &data->flags);
4288 spin_unlock_irq(&data->txlock);
4289 schedule_work(&data->work);
4294 usb_scuttle_anchored_urbs(&data->deferred);
4296 spin_lock_irq(&data->txlock);
4297 clear_bit(BTUSB_SUSPENDING, &data->flags);
4298 spin_unlock_irq(&data->txlock);
4304 static struct usb_driver btusb_driver = {
4306 .probe = btusb_probe,
4307 .disconnect = btusb_disconnect,
4309 .suspend = btusb_suspend,
4310 .resume = btusb_resume,
4312 .id_table = btusb_table,
4313 .supports_autosuspend = 1,
4314 .disable_hub_initiated_lpm = 1,
4317 module_usb_driver(btusb_driver);
4319 module_param(disable_scofix, bool, 0644);
4320 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4322 module_param(force_scofix, bool, 0644);
4323 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4325 module_param(enable_autosuspend, bool, 0644);
4326 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4328 module_param(reset, bool, 0644);
4329 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4331 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4332 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4333 MODULE_VERSION(VERSION);
4334 MODULE_LICENSE("GPL");