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 enable_poll_sync = IS_ENABLED(CONFIG_BT_HCIBTUSB_POLL_SYNC);
36 static bool reset = true;
38 static struct usb_driver btusb_driver;
40 #define BTUSB_IGNORE BIT(0)
41 #define BTUSB_DIGIANSWER BIT(1)
42 #define BTUSB_CSR BIT(2)
43 #define BTUSB_SNIFFER BIT(3)
44 #define BTUSB_BCM92035 BIT(4)
45 #define BTUSB_BROKEN_ISOC BIT(5)
46 #define BTUSB_WRONG_SCO_MTU BIT(6)
47 #define BTUSB_ATH3012 BIT(7)
48 #define BTUSB_INTEL_COMBINED BIT(8)
49 #define BTUSB_INTEL_BOOT BIT(9)
50 #define BTUSB_BCM_PATCHRAM BIT(10)
51 #define BTUSB_MARVELL BIT(11)
52 #define BTUSB_SWAVE BIT(12)
53 #define BTUSB_AMP BIT(13)
54 #define BTUSB_QCA_ROME BIT(14)
55 #define BTUSB_BCM_APPLE BIT(15)
56 #define BTUSB_REALTEK BIT(16)
57 #define BTUSB_BCM2045 BIT(17)
58 #define BTUSB_IFNUM_2 BIT(18)
59 #define BTUSB_CW6622 BIT(19)
60 #define BTUSB_MEDIATEK BIT(20)
61 #define BTUSB_WIDEBAND_SPEECH BIT(21)
62 #define BTUSB_VALID_LE_STATES BIT(22)
63 #define BTUSB_QCA_WCN6855 BIT(23)
64 #define BTUSB_INTEL_BROKEN_SHUTDOWN_LED BIT(24)
65 #define BTUSB_INTEL_BROKEN_INITIAL_NCMD BIT(25)
66 #define BTUSB_INTEL_NO_WBS_SUPPORT BIT(26)
67 #define BTUSB_ACTIONS_SEMI BIT(27)
69 static const struct usb_device_id btusb_table[] = {
70 /* Generic Bluetooth USB device */
71 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
73 /* Generic Bluetooth AMP device */
74 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
76 /* Generic Bluetooth USB interface */
77 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
79 /* Apple-specific (Broadcom) devices */
80 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
81 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
83 /* MediaTek MT76x0E */
84 { USB_DEVICE(0x0e8d, 0x763f) },
86 /* Broadcom SoftSailing reporting vendor specific */
87 { USB_DEVICE(0x0a5c, 0x21e1) },
89 /* Apple MacBookPro 7,1 */
90 { USB_DEVICE(0x05ac, 0x8213) },
93 { USB_DEVICE(0x05ac, 0x8215) },
95 /* Apple MacBookPro6,2 */
96 { USB_DEVICE(0x05ac, 0x8218) },
98 /* Apple MacBookAir3,1, MacBookAir3,2 */
99 { USB_DEVICE(0x05ac, 0x821b) },
101 /* Apple MacBookAir4,1 */
102 { USB_DEVICE(0x05ac, 0x821f) },
104 /* Apple MacBookPro8,2 */
105 { USB_DEVICE(0x05ac, 0x821a) },
107 /* Apple MacMini5,1 */
108 { USB_DEVICE(0x05ac, 0x8281) },
110 /* AVM BlueFRITZ! USB v2.0 */
111 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
113 /* Bluetooth Ultraport Module from IBM */
114 { USB_DEVICE(0x04bf, 0x030a) },
116 /* ALPS Modules with non-standard id */
117 { USB_DEVICE(0x044e, 0x3001) },
118 { USB_DEVICE(0x044e, 0x3002) },
120 /* Ericsson with non-standard id */
121 { USB_DEVICE(0x0bdb, 0x1002) },
123 /* Canyon CN-BTU1 with HID interfaces */
124 { USB_DEVICE(0x0c10, 0x0000) },
126 /* Broadcom BCM20702B0 (Dynex/Insignia) */
127 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
129 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
130 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
131 .driver_info = BTUSB_BCM_PATCHRAM },
133 /* Broadcom BCM920703 (HTC Vive) */
134 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
135 .driver_info = BTUSB_BCM_PATCHRAM },
137 /* Foxconn - Hon Hai */
138 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
139 .driver_info = BTUSB_BCM_PATCHRAM },
141 /* Lite-On Technology - Broadcom based */
142 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
143 .driver_info = BTUSB_BCM_PATCHRAM },
145 /* Broadcom devices with vendor specific id */
146 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
147 .driver_info = BTUSB_BCM_PATCHRAM },
149 /* ASUSTek Computer - Broadcom based */
150 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
151 .driver_info = BTUSB_BCM_PATCHRAM },
153 /* Belkin F8065bf - Broadcom based */
154 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
155 .driver_info = BTUSB_BCM_PATCHRAM },
157 /* IMC Networks - Broadcom based */
158 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
159 .driver_info = BTUSB_BCM_PATCHRAM },
161 /* Dell Computer - Broadcom based */
162 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
163 .driver_info = BTUSB_BCM_PATCHRAM },
165 /* Toshiba Corp - Broadcom based */
166 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
167 .driver_info = BTUSB_BCM_PATCHRAM },
169 /* Intel Bluetooth USB Bootloader (RAM module) */
170 { USB_DEVICE(0x8087, 0x0a5a),
171 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
173 { } /* Terminating entry */
176 MODULE_DEVICE_TABLE(usb, btusb_table);
178 static const struct usb_device_id blacklist_table[] = {
179 /* CSR BlueCore devices */
180 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
182 /* Broadcom BCM2033 without firmware */
183 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
185 /* Broadcom BCM2045 devices */
186 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
188 /* Atheros 3011 with sflash firmware */
189 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
190 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
191 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
192 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
193 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
194 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
195 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
197 /* Atheros AR9285 Malbec with sflash firmware */
198 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
200 /* Atheros 3012 with sflash firmware */
201 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
246 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
247 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
248 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
249 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
250 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
252 /* Atheros AR5BBU12 with sflash firmware */
253 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
255 /* Atheros AR5BBU12 with sflash firmware */
256 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
257 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
259 /* QCA ROME chipset */
260 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
261 BTUSB_WIDEBAND_SPEECH },
262 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
263 BTUSB_WIDEBAND_SPEECH },
264 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
265 BTUSB_WIDEBAND_SPEECH },
266 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
267 BTUSB_WIDEBAND_SPEECH },
268 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
269 BTUSB_WIDEBAND_SPEECH },
270 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
271 BTUSB_WIDEBAND_SPEECH },
272 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
273 BTUSB_WIDEBAND_SPEECH },
274 { USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
275 BTUSB_WIDEBAND_SPEECH },
276 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
277 BTUSB_WIDEBAND_SPEECH },
278 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
279 BTUSB_WIDEBAND_SPEECH },
280 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
281 BTUSB_WIDEBAND_SPEECH },
282 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
283 BTUSB_WIDEBAND_SPEECH },
284 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
285 BTUSB_WIDEBAND_SPEECH },
286 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
287 BTUSB_WIDEBAND_SPEECH },
288 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
289 BTUSB_WIDEBAND_SPEECH },
290 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
291 BTUSB_WIDEBAND_SPEECH },
292 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
293 BTUSB_WIDEBAND_SPEECH },
294 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
295 BTUSB_WIDEBAND_SPEECH },
296 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
297 BTUSB_WIDEBAND_SPEECH },
299 /* QCA WCN6855 chipset */
300 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
301 BTUSB_WIDEBAND_SPEECH |
302 BTUSB_VALID_LE_STATES },
303 { USB_DEVICE(0x0489, 0xe0cc), .driver_info = BTUSB_QCA_WCN6855 |
304 BTUSB_WIDEBAND_SPEECH |
305 BTUSB_VALID_LE_STATES },
306 { USB_DEVICE(0x0489, 0xe0d6), .driver_info = BTUSB_QCA_WCN6855 |
307 BTUSB_WIDEBAND_SPEECH |
308 BTUSB_VALID_LE_STATES },
309 { USB_DEVICE(0x0489, 0xe0e3), .driver_info = BTUSB_QCA_WCN6855 |
310 BTUSB_WIDEBAND_SPEECH |
311 BTUSB_VALID_LE_STATES },
312 { USB_DEVICE(0x10ab, 0x9309), .driver_info = BTUSB_QCA_WCN6855 |
313 BTUSB_WIDEBAND_SPEECH |
314 BTUSB_VALID_LE_STATES },
315 { USB_DEVICE(0x10ab, 0x9409), .driver_info = BTUSB_QCA_WCN6855 |
316 BTUSB_WIDEBAND_SPEECH |
317 BTUSB_VALID_LE_STATES },
318 { USB_DEVICE(0x0489, 0xe0d0), .driver_info = BTUSB_QCA_WCN6855 |
319 BTUSB_WIDEBAND_SPEECH |
320 BTUSB_VALID_LE_STATES },
321 { USB_DEVICE(0x10ab, 0x9108), .driver_info = BTUSB_QCA_WCN6855 |
322 BTUSB_WIDEBAND_SPEECH |
323 BTUSB_VALID_LE_STATES },
324 { USB_DEVICE(0x10ab, 0x9109), .driver_info = BTUSB_QCA_WCN6855 |
325 BTUSB_WIDEBAND_SPEECH |
326 BTUSB_VALID_LE_STATES },
327 { USB_DEVICE(0x10ab, 0x9208), .driver_info = BTUSB_QCA_WCN6855 |
328 BTUSB_WIDEBAND_SPEECH |
329 BTUSB_VALID_LE_STATES },
330 { USB_DEVICE(0x10ab, 0x9209), .driver_info = BTUSB_QCA_WCN6855 |
331 BTUSB_WIDEBAND_SPEECH |
332 BTUSB_VALID_LE_STATES },
333 { USB_DEVICE(0x10ab, 0x9308), .driver_info = BTUSB_QCA_WCN6855 |
334 BTUSB_WIDEBAND_SPEECH |
335 BTUSB_VALID_LE_STATES },
336 { USB_DEVICE(0x10ab, 0x9408), .driver_info = BTUSB_QCA_WCN6855 |
337 BTUSB_WIDEBAND_SPEECH |
338 BTUSB_VALID_LE_STATES },
339 { USB_DEVICE(0x10ab, 0x9508), .driver_info = BTUSB_QCA_WCN6855 |
340 BTUSB_WIDEBAND_SPEECH |
341 BTUSB_VALID_LE_STATES },
342 { USB_DEVICE(0x10ab, 0x9509), .driver_info = BTUSB_QCA_WCN6855 |
343 BTUSB_WIDEBAND_SPEECH |
344 BTUSB_VALID_LE_STATES },
345 { USB_DEVICE(0x10ab, 0x9608), .driver_info = BTUSB_QCA_WCN6855 |
346 BTUSB_WIDEBAND_SPEECH |
347 BTUSB_VALID_LE_STATES },
348 { USB_DEVICE(0x10ab, 0x9609), .driver_info = BTUSB_QCA_WCN6855 |
349 BTUSB_WIDEBAND_SPEECH |
350 BTUSB_VALID_LE_STATES },
351 { USB_DEVICE(0x10ab, 0x9f09), .driver_info = BTUSB_QCA_WCN6855 |
352 BTUSB_WIDEBAND_SPEECH |
353 BTUSB_VALID_LE_STATES },
354 { USB_DEVICE(0x04ca, 0x3022), .driver_info = BTUSB_QCA_WCN6855 |
355 BTUSB_WIDEBAND_SPEECH |
356 BTUSB_VALID_LE_STATES },
357 { USB_DEVICE(0x0489, 0xe0c7), .driver_info = BTUSB_QCA_WCN6855 |
358 BTUSB_WIDEBAND_SPEECH |
359 BTUSB_VALID_LE_STATES },
360 { USB_DEVICE(0x0489, 0xe0c9), .driver_info = BTUSB_QCA_WCN6855 |
361 BTUSB_WIDEBAND_SPEECH |
362 BTUSB_VALID_LE_STATES },
363 { USB_DEVICE(0x0489, 0xe0ca), .driver_info = BTUSB_QCA_WCN6855 |
364 BTUSB_WIDEBAND_SPEECH |
365 BTUSB_VALID_LE_STATES },
366 { USB_DEVICE(0x0489, 0xe0cb), .driver_info = BTUSB_QCA_WCN6855 |
367 BTUSB_WIDEBAND_SPEECH |
368 BTUSB_VALID_LE_STATES },
369 { USB_DEVICE(0x0489, 0xe0ce), .driver_info = BTUSB_QCA_WCN6855 |
370 BTUSB_WIDEBAND_SPEECH |
371 BTUSB_VALID_LE_STATES },
372 { USB_DEVICE(0x0489, 0xe0de), .driver_info = BTUSB_QCA_WCN6855 |
373 BTUSB_WIDEBAND_SPEECH |
374 BTUSB_VALID_LE_STATES },
375 { USB_DEVICE(0x0489, 0xe0df), .driver_info = BTUSB_QCA_WCN6855 |
376 BTUSB_WIDEBAND_SPEECH |
377 BTUSB_VALID_LE_STATES },
378 { USB_DEVICE(0x0489, 0xe0e1), .driver_info = BTUSB_QCA_WCN6855 |
379 BTUSB_WIDEBAND_SPEECH |
380 BTUSB_VALID_LE_STATES },
381 { USB_DEVICE(0x0489, 0xe0ea), .driver_info = BTUSB_QCA_WCN6855 |
382 BTUSB_WIDEBAND_SPEECH |
383 BTUSB_VALID_LE_STATES },
384 { USB_DEVICE(0x0489, 0xe0ec), .driver_info = BTUSB_QCA_WCN6855 |
385 BTUSB_WIDEBAND_SPEECH |
386 BTUSB_VALID_LE_STATES },
387 { USB_DEVICE(0x04ca, 0x3023), .driver_info = BTUSB_QCA_WCN6855 |
388 BTUSB_WIDEBAND_SPEECH |
389 BTUSB_VALID_LE_STATES },
390 { USB_DEVICE(0x04ca, 0x3024), .driver_info = BTUSB_QCA_WCN6855 |
391 BTUSB_WIDEBAND_SPEECH |
392 BTUSB_VALID_LE_STATES },
393 { USB_DEVICE(0x04ca, 0x3a22), .driver_info = BTUSB_QCA_WCN6855 |
394 BTUSB_WIDEBAND_SPEECH |
395 BTUSB_VALID_LE_STATES },
396 { USB_DEVICE(0x04ca, 0x3a24), .driver_info = BTUSB_QCA_WCN6855 |
397 BTUSB_WIDEBAND_SPEECH |
398 BTUSB_VALID_LE_STATES },
399 { USB_DEVICE(0x04ca, 0x3a26), .driver_info = BTUSB_QCA_WCN6855 |
400 BTUSB_WIDEBAND_SPEECH |
401 BTUSB_VALID_LE_STATES },
402 { USB_DEVICE(0x04ca, 0x3a27), .driver_info = BTUSB_QCA_WCN6855 |
403 BTUSB_WIDEBAND_SPEECH |
404 BTUSB_VALID_LE_STATES },
406 /* QCA WCN785x chipset */
407 { USB_DEVICE(0x0cf3, 0xe700), .driver_info = BTUSB_QCA_WCN6855 |
408 BTUSB_WIDEBAND_SPEECH |
409 BTUSB_VALID_LE_STATES },
411 /* Broadcom BCM2035 */
412 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
413 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
414 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
416 /* Broadcom BCM2045 */
417 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
418 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
420 /* IBM/Lenovo ThinkPad with Broadcom chip */
421 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
422 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
424 /* HP laptop with Broadcom chip */
425 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
427 /* Dell laptop with Broadcom chip */
428 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
430 /* Dell Wireless 370 and 410 devices */
431 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
432 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
434 /* Belkin F8T012 and F8T013 devices */
435 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
436 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
438 /* Asus WL-BTD202 device */
439 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
441 /* Kensington Bluetooth USB adapter */
442 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
444 /* RTX Telecom based adapters with buggy SCO support */
445 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
446 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
448 /* CONWISE Technology based adapters with buggy SCO support */
449 { USB_DEVICE(0x0e5e, 0x6622),
450 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
452 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
453 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
455 /* Digianswer devices */
456 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
457 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
459 /* CSR BlueCore Bluetooth Sniffer */
460 { USB_DEVICE(0x0a12, 0x0002),
461 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
463 /* Frontline ComProbe Bluetooth Sniffer */
464 { USB_DEVICE(0x16d3, 0x0002),
465 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
467 /* Marvell Bluetooth devices */
468 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
469 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
470 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
472 /* Intel Bluetooth devices */
473 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_COMBINED },
474 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_COMBINED },
475 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_COMBINED },
476 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_COMBINED },
477 { USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_COMBINED },
478 { USB_DEVICE(0x8087, 0x0035), .driver_info = BTUSB_INTEL_COMBINED },
479 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
480 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL_COMBINED |
481 BTUSB_INTEL_NO_WBS_SUPPORT |
482 BTUSB_INTEL_BROKEN_INITIAL_NCMD |
483 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
484 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL_COMBINED |
485 BTUSB_INTEL_NO_WBS_SUPPORT |
486 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
487 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_COMBINED },
488 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL_COMBINED |
489 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
490 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_COMBINED },
492 /* Other Intel Bluetooth devices */
493 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
494 .driver_info = BTUSB_IGNORE },
496 /* Realtek 8821CE Bluetooth devices */
497 { USB_DEVICE(0x13d3, 0x3529), .driver_info = BTUSB_REALTEK |
498 BTUSB_WIDEBAND_SPEECH },
500 /* Realtek 8822CE Bluetooth devices */
501 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
502 BTUSB_WIDEBAND_SPEECH },
503 { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
504 BTUSB_WIDEBAND_SPEECH },
506 /* Realtek 8822CU Bluetooth devices */
507 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
508 BTUSB_WIDEBAND_SPEECH },
510 /* Realtek 8852AE Bluetooth devices */
511 { USB_DEVICE(0x0bda, 0x2852), .driver_info = BTUSB_REALTEK |
512 BTUSB_WIDEBAND_SPEECH },
513 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
514 BTUSB_WIDEBAND_SPEECH },
515 { USB_DEVICE(0x0bda, 0x385a), .driver_info = BTUSB_REALTEK |
516 BTUSB_WIDEBAND_SPEECH },
517 { USB_DEVICE(0x0bda, 0x4852), .driver_info = BTUSB_REALTEK |
518 BTUSB_WIDEBAND_SPEECH },
519 { USB_DEVICE(0x04c5, 0x165c), .driver_info = BTUSB_REALTEK |
520 BTUSB_WIDEBAND_SPEECH },
521 { USB_DEVICE(0x04ca, 0x4006), .driver_info = BTUSB_REALTEK |
522 BTUSB_WIDEBAND_SPEECH },
523 { USB_DEVICE(0x0cb8, 0xc549), .driver_info = BTUSB_REALTEK |
524 BTUSB_WIDEBAND_SPEECH },
526 /* Realtek 8852CE Bluetooth devices */
527 { USB_DEVICE(0x04ca, 0x4007), .driver_info = BTUSB_REALTEK |
528 BTUSB_WIDEBAND_SPEECH },
529 { USB_DEVICE(0x04c5, 0x1675), .driver_info = BTUSB_REALTEK |
530 BTUSB_WIDEBAND_SPEECH },
531 { USB_DEVICE(0x0cb8, 0xc558), .driver_info = BTUSB_REALTEK |
532 BTUSB_WIDEBAND_SPEECH },
533 { USB_DEVICE(0x13d3, 0x3587), .driver_info = BTUSB_REALTEK |
534 BTUSB_WIDEBAND_SPEECH },
535 { USB_DEVICE(0x13d3, 0x3586), .driver_info = BTUSB_REALTEK |
536 BTUSB_WIDEBAND_SPEECH },
537 { USB_DEVICE(0x13d3, 0x3592), .driver_info = BTUSB_REALTEK |
538 BTUSB_WIDEBAND_SPEECH },
540 /* Realtek 8852BE Bluetooth devices */
541 { USB_DEVICE(0x0cb8, 0xc559), .driver_info = BTUSB_REALTEK |
542 BTUSB_WIDEBAND_SPEECH },
544 /* Realtek Bluetooth devices */
545 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
546 .driver_info = BTUSB_REALTEK },
548 /* MediaTek Bluetooth devices */
549 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
550 .driver_info = BTUSB_MEDIATEK |
551 BTUSB_WIDEBAND_SPEECH |
552 BTUSB_VALID_LE_STATES },
554 /* Additional MediaTek MT7615E Bluetooth devices */
555 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
557 /* Additional MediaTek MT7663 Bluetooth devices */
558 { USB_DEVICE(0x043e, 0x310c), .driver_info = BTUSB_MEDIATEK |
559 BTUSB_WIDEBAND_SPEECH |
560 BTUSB_VALID_LE_STATES },
562 /* Additional MediaTek MT7668 Bluetooth devices */
563 { USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
564 BTUSB_WIDEBAND_SPEECH |
565 BTUSB_VALID_LE_STATES },
567 /* Additional MediaTek MT7921 Bluetooth devices */
568 { USB_DEVICE(0x0489, 0xe0c8), .driver_info = BTUSB_MEDIATEK |
569 BTUSB_WIDEBAND_SPEECH |
570 BTUSB_VALID_LE_STATES },
571 { USB_DEVICE(0x0489, 0xe0e0), .driver_info = BTUSB_MEDIATEK |
572 BTUSB_WIDEBAND_SPEECH |
573 BTUSB_VALID_LE_STATES },
574 { USB_DEVICE(0x0489, 0xe0f2), .driver_info = BTUSB_MEDIATEK |
575 BTUSB_WIDEBAND_SPEECH |
576 BTUSB_VALID_LE_STATES },
577 { USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
578 BTUSB_WIDEBAND_SPEECH |
579 BTUSB_VALID_LE_STATES },
580 { USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
581 BTUSB_WIDEBAND_SPEECH |
582 BTUSB_VALID_LE_STATES },
583 { USB_DEVICE(0x13d3, 0x3564), .driver_info = BTUSB_MEDIATEK |
584 BTUSB_WIDEBAND_SPEECH |
585 BTUSB_VALID_LE_STATES },
586 { USB_DEVICE(0x13d3, 0x3567), .driver_info = BTUSB_MEDIATEK |
587 BTUSB_WIDEBAND_SPEECH |
588 BTUSB_VALID_LE_STATES },
589 { USB_DEVICE(0x13d3, 0x3578), .driver_info = BTUSB_MEDIATEK |
590 BTUSB_WIDEBAND_SPEECH |
591 BTUSB_VALID_LE_STATES },
592 { USB_DEVICE(0x13d3, 0x3583), .driver_info = BTUSB_MEDIATEK |
593 BTUSB_WIDEBAND_SPEECH |
594 BTUSB_VALID_LE_STATES },
595 { USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
596 BTUSB_WIDEBAND_SPEECH |
597 BTUSB_VALID_LE_STATES },
598 { USB_DEVICE(0x0e8d, 0x0608), .driver_info = BTUSB_MEDIATEK |
599 BTUSB_WIDEBAND_SPEECH |
600 BTUSB_VALID_LE_STATES },
602 /* MediaTek MT7922A Bluetooth devices */
603 { USB_DEVICE(0x0489, 0xe0d8), .driver_info = BTUSB_MEDIATEK |
604 BTUSB_WIDEBAND_SPEECH |
605 BTUSB_VALID_LE_STATES },
606 { USB_DEVICE(0x0489, 0xe0d9), .driver_info = BTUSB_MEDIATEK |
607 BTUSB_WIDEBAND_SPEECH |
608 BTUSB_VALID_LE_STATES },
609 { USB_DEVICE(0x13d3, 0x3568), .driver_info = BTUSB_MEDIATEK |
610 BTUSB_WIDEBAND_SPEECH |
611 BTUSB_VALID_LE_STATES },
612 { USB_DEVICE(0x0489, 0xe0e2), .driver_info = BTUSB_MEDIATEK |
613 BTUSB_WIDEBAND_SPEECH |
614 BTUSB_VALID_LE_STATES },
615 { USB_DEVICE(0x0489, 0xe0f2), .driver_info = BTUSB_MEDIATEK |
616 BTUSB_WIDEBAND_SPEECH |
617 BTUSB_VALID_LE_STATES },
619 /* Additional Realtek 8723AE Bluetooth devices */
620 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
621 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
623 /* Additional Realtek 8723BE Bluetooth devices */
624 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
625 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
626 { USB_DEVICE(0x04f2, 0xb49f), .driver_info = BTUSB_REALTEK },
627 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
628 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
629 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
630 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
632 /* Additional Realtek 8723BU Bluetooth devices */
633 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
635 /* Additional Realtek 8723DE Bluetooth devices */
636 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
637 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
639 /* Additional Realtek 8761BUV Bluetooth devices */
640 { USB_DEVICE(0x2357, 0x0604), .driver_info = BTUSB_REALTEK |
641 BTUSB_WIDEBAND_SPEECH },
642 { USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
643 BTUSB_WIDEBAND_SPEECH },
644 { USB_DEVICE(0x2550, 0x8761), .driver_info = BTUSB_REALTEK |
645 BTUSB_WIDEBAND_SPEECH },
646 { USB_DEVICE(0x0bda, 0x8771), .driver_info = BTUSB_REALTEK |
647 BTUSB_WIDEBAND_SPEECH },
648 { USB_DEVICE(0x7392, 0xc611), .driver_info = BTUSB_REALTEK |
649 BTUSB_WIDEBAND_SPEECH },
650 { USB_DEVICE(0x2b89, 0x8761), .driver_info = BTUSB_REALTEK |
651 BTUSB_WIDEBAND_SPEECH },
653 /* Additional Realtek 8821AE Bluetooth devices */
654 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
655 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
656 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
657 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
658 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
660 /* Additional Realtek 8822BE Bluetooth devices */
661 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
662 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
664 /* Additional Realtek 8822CE Bluetooth devices */
665 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
666 BTUSB_WIDEBAND_SPEECH },
667 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
668 BTUSB_WIDEBAND_SPEECH },
669 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
670 BTUSB_WIDEBAND_SPEECH },
671 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
672 BTUSB_WIDEBAND_SPEECH },
673 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
674 BTUSB_WIDEBAND_SPEECH },
675 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
676 BTUSB_WIDEBAND_SPEECH },
677 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
678 BTUSB_WIDEBAND_SPEECH },
679 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
680 BTUSB_WIDEBAND_SPEECH },
681 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
682 BTUSB_WIDEBAND_SPEECH },
683 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
684 BTUSB_WIDEBAND_SPEECH },
685 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
686 BTUSB_WIDEBAND_SPEECH },
688 /* Actions Semiconductor ATS2851 based devices */
689 { USB_DEVICE(0x10d7, 0xb012), .driver_info = BTUSB_ACTIONS_SEMI },
691 /* Silicon Wave based devices */
692 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
694 { } /* Terminating entry */
697 /* The Bluetooth USB module build into some devices needs to be reset on resume,
698 * this is a problem with the platform (likely shutting off all power) not with
699 * the module itself. So we use a DMI list to match known broken platforms.
701 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
703 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
705 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
706 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
710 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
712 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
713 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
717 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
719 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
720 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
726 #define BTUSB_MAX_ISOC_FRAMES 10
728 #define BTUSB_INTR_RUNNING 0
729 #define BTUSB_BULK_RUNNING 1
730 #define BTUSB_ISOC_RUNNING 2
731 #define BTUSB_SUSPENDING 3
732 #define BTUSB_DID_ISO_RESUME 4
733 #define BTUSB_BOOTLOADER 5
734 #define BTUSB_DOWNLOADING 6
735 #define BTUSB_FIRMWARE_LOADED 7
736 #define BTUSB_FIRMWARE_FAILED 8
737 #define BTUSB_BOOTING 9
738 #define BTUSB_DIAG_RUNNING 10
739 #define BTUSB_OOB_WAKE_ENABLED 11
740 #define BTUSB_HW_RESET_ACTIVE 12
741 #define BTUSB_TX_WAIT_VND_EVT 13
742 #define BTUSB_WAKEUP_AUTOSUSPEND 14
743 #define BTUSB_USE_ALT3_FOR_WBS 15
744 #define BTUSB_ALT6_CONTINUOUS_TX 16
747 struct hci_dev *hdev;
748 struct usb_device *udev;
749 struct usb_interface *intf;
750 struct usb_interface *isoc;
751 struct usb_interface *diag;
758 struct work_struct work;
759 struct work_struct waker;
760 struct delayed_work rx_work;
762 struct sk_buff_head acl_q;
764 struct usb_anchor deferred;
765 struct usb_anchor tx_anchor;
769 struct usb_anchor intr_anchor;
770 struct usb_anchor bulk_anchor;
771 struct usb_anchor isoc_anchor;
772 struct usb_anchor diag_anchor;
773 struct usb_anchor ctrl_anchor;
776 struct sk_buff *evt_skb;
777 struct sk_buff *acl_skb;
778 struct sk_buff *sco_skb;
780 struct usb_endpoint_descriptor *intr_ep;
781 struct usb_endpoint_descriptor *bulk_tx_ep;
782 struct usb_endpoint_descriptor *bulk_rx_ep;
783 struct usb_endpoint_descriptor *isoc_tx_ep;
784 struct usb_endpoint_descriptor *isoc_rx_ep;
785 struct usb_endpoint_descriptor *diag_tx_ep;
786 struct usb_endpoint_descriptor *diag_rx_ep;
788 struct gpio_desc *reset_gpio;
793 unsigned int sco_num;
794 unsigned int air_mode;
795 bool usb_alt6_packet_flow;
799 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
800 int (*recv_acl)(struct hci_dev *hdev, struct sk_buff *skb);
801 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
803 int (*setup_on_usb)(struct hci_dev *hdev);
805 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
806 unsigned cmd_timeout_cnt;
809 static void btusb_reset(struct hci_dev *hdev)
811 struct btusb_data *data;
819 data = hci_get_drvdata(hdev);
820 /* This is not an unbalanced PM reference since the device will reset */
821 err = usb_autopm_get_interface(data->intf);
823 bt_dev_err(hdev, "Failed usb_autopm_get_interface: %d", err);
827 bt_dev_err(hdev, "Resetting usb device.");
828 usb_queue_reset_device(data->intf);
831 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
833 struct btusb_data *data = hci_get_drvdata(hdev);
834 struct gpio_desc *reset_gpio = data->reset_gpio;
836 if (++data->cmd_timeout_cnt < 5)
845 * Toggle the hard reset line if the platform provides one. The reset
846 * is going to yank the device off the USB and then replug. So doing
847 * once is enough. The cleanup is handled correctly on the way out
848 * (standard USB disconnect), and the new device is detected cleanly
849 * and bound to the driver again like it should be.
851 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
852 bt_dev_err(hdev, "last reset failed? Not resetting again");
856 bt_dev_err(hdev, "Initiating HW reset via gpio");
857 gpiod_set_value_cansleep(reset_gpio, 1);
859 gpiod_set_value_cansleep(reset_gpio, 0);
862 static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
864 struct btusb_data *data = hci_get_drvdata(hdev);
865 struct gpio_desc *reset_gpio = data->reset_gpio;
867 if (++data->cmd_timeout_cnt < 5)
875 /* Toggle the hard reset line. The Realtek device is going to
876 * yank itself off the USB and then replug. The cleanup is handled
877 * correctly on the way out (standard USB disconnect), and the new
878 * device is detected cleanly and bound to the driver again like
881 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
882 bt_dev_err(hdev, "last reset failed? Not resetting again");
886 bt_dev_err(hdev, "Reset Realtek device via gpio");
887 gpiod_set_value_cansleep(reset_gpio, 1);
889 gpiod_set_value_cansleep(reset_gpio, 0);
892 static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
894 struct btusb_data *data = hci_get_drvdata(hdev);
895 struct gpio_desc *reset_gpio = data->reset_gpio;
897 if (++data->cmd_timeout_cnt < 5)
901 bt_dev_err(hdev, "Reset qca device via bt_en gpio");
903 /* Toggle the hard reset line. The qca bt device is going to
904 * yank itself off the USB and then replug. The cleanup is handled
905 * correctly on the way out (standard USB disconnect), and the new
906 * device is detected cleanly and bound to the driver again like
909 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
910 bt_dev_err(hdev, "last reset failed? Not resetting again");
914 gpiod_set_value_cansleep(reset_gpio, 0);
916 gpiod_set_value_cansleep(reset_gpio, 1);
924 static inline void btusb_free_frags(struct btusb_data *data)
928 spin_lock_irqsave(&data->rxlock, flags);
930 dev_kfree_skb_irq(data->evt_skb);
931 data->evt_skb = NULL;
933 dev_kfree_skb_irq(data->acl_skb);
934 data->acl_skb = NULL;
936 dev_kfree_skb_irq(data->sco_skb);
937 data->sco_skb = NULL;
939 spin_unlock_irqrestore(&data->rxlock, flags);
942 static int btusb_recv_event(struct btusb_data *data, struct sk_buff *skb)
944 if (data->intr_interval) {
945 /* Trigger dequeue immediatelly if an event is received */
946 schedule_delayed_work(&data->rx_work, 0);
949 return data->recv_event(data->hdev, skb);
952 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
958 spin_lock_irqsave(&data->rxlock, flags);
965 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
971 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
972 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
975 len = min_t(uint, hci_skb_expect(skb), count);
976 skb_put_data(skb, buffer, len);
980 hci_skb_expect(skb) -= len;
982 if (skb->len == HCI_EVENT_HDR_SIZE) {
983 /* Complete event header */
984 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
986 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
995 if (!hci_skb_expect(skb)) {
997 btusb_recv_event(data, skb);
1002 data->evt_skb = skb;
1003 spin_unlock_irqrestore(&data->rxlock, flags);
1008 static int btusb_recv_acl(struct btusb_data *data, struct sk_buff *skb)
1010 /* Only queue ACL packet if intr_interval is set as it means
1011 * force_poll_sync has been enabled.
1013 if (!data->intr_interval)
1014 return data->recv_acl(data->hdev, skb);
1016 skb_queue_tail(&data->acl_q, skb);
1017 schedule_delayed_work(&data->rx_work, data->intr_interval);
1022 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
1024 struct sk_buff *skb;
1025 unsigned long flags;
1028 spin_lock_irqsave(&data->rxlock, flags);
1029 skb = data->acl_skb;
1035 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
1041 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
1042 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
1045 len = min_t(uint, hci_skb_expect(skb), count);
1046 skb_put_data(skb, buffer, len);
1050 hci_skb_expect(skb) -= len;
1052 if (skb->len == HCI_ACL_HDR_SIZE) {
1053 __le16 dlen = hci_acl_hdr(skb)->dlen;
1055 /* Complete ACL header */
1056 hci_skb_expect(skb) = __le16_to_cpu(dlen);
1058 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
1067 if (!hci_skb_expect(skb)) {
1068 /* Complete frame */
1069 btusb_recv_acl(data, skb);
1074 data->acl_skb = skb;
1075 spin_unlock_irqrestore(&data->rxlock, flags);
1080 static bool btusb_validate_sco_handle(struct hci_dev *hdev,
1081 struct hci_sco_hdr *hdr)
1085 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
1086 // Can't validate, userspace controls everything.
1090 * USB isochronous transfers are not designed to be reliable and may
1091 * lose fragments. When this happens, the next first fragment
1092 * encountered might actually be a continuation fragment.
1093 * Validate the handle to detect it and drop it, or else the upper
1094 * layer will get garbage for a while.
1097 handle = hci_handle(__le16_to_cpu(hdr->handle));
1099 switch (hci_conn_lookup_type(hdev, handle)) {
1108 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
1110 struct sk_buff *skb;
1111 unsigned long flags;
1114 spin_lock_irqsave(&data->rxlock, flags);
1115 skb = data->sco_skb;
1121 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
1127 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
1128 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
1131 len = min_t(uint, hci_skb_expect(skb), count);
1132 skb_put_data(skb, buffer, len);
1136 hci_skb_expect(skb) -= len;
1138 if (skb->len == HCI_SCO_HDR_SIZE) {
1139 /* Complete SCO header */
1140 struct hci_sco_hdr *hdr = hci_sco_hdr(skb);
1142 hci_skb_expect(skb) = hdr->dlen;
1144 if (skb_tailroom(skb) < hci_skb_expect(skb) ||
1145 !btusb_validate_sco_handle(data->hdev, hdr)) {
1154 if (!hci_skb_expect(skb)) {
1155 /* Complete frame */
1156 hci_recv_frame(data->hdev, skb);
1161 data->sco_skb = skb;
1162 spin_unlock_irqrestore(&data->rxlock, flags);
1167 static void btusb_intr_complete(struct urb *urb)
1169 struct hci_dev *hdev = urb->context;
1170 struct btusb_data *data = hci_get_drvdata(hdev);
1173 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1174 urb->actual_length);
1176 if (!test_bit(HCI_RUNNING, &hdev->flags))
1179 if (urb->status == 0) {
1180 hdev->stat.byte_rx += urb->actual_length;
1182 if (btusb_recv_intr(data, urb->transfer_buffer,
1183 urb->actual_length) < 0) {
1184 bt_dev_err(hdev, "corrupted event packet");
1185 hdev->stat.err_rx++;
1187 } else if (urb->status == -ENOENT) {
1188 /* Avoid suspend failed when usb_kill_urb */
1192 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
1195 usb_mark_last_busy(data->udev);
1196 usb_anchor_urb(urb, &data->intr_anchor);
1198 err = usb_submit_urb(urb, GFP_ATOMIC);
1200 /* -EPERM: urb is being killed;
1201 * -ENODEV: device got disconnected
1203 if (err != -EPERM && err != -ENODEV)
1204 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1207 hci_cmd_sync_cancel(hdev, -err);
1208 usb_unanchor_urb(urb);
1212 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
1214 struct btusb_data *data = hci_get_drvdata(hdev);
1220 BT_DBG("%s", hdev->name);
1225 urb = usb_alloc_urb(0, mem_flags);
1229 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
1231 buf = kmalloc(size, mem_flags);
1237 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
1239 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
1240 btusb_intr_complete, hdev, data->intr_ep->bInterval);
1242 urb->transfer_flags |= URB_FREE_BUFFER;
1244 usb_anchor_urb(urb, &data->intr_anchor);
1246 err = usb_submit_urb(urb, mem_flags);
1248 if (err != -EPERM && err != -ENODEV)
1249 bt_dev_err(hdev, "urb %p submission failed (%d)",
1252 hci_cmd_sync_cancel(hdev, -err);
1253 usb_unanchor_urb(urb);
1256 /* Only initialize intr_interval if URB poll sync is enabled */
1257 if (!data->poll_sync)
1260 /* The units are frames (milliseconds) for full and low speed devices,
1261 * and microframes (1/8 millisecond) for highspeed and SuperSpeed
1264 * This is done once on open/resume so it shouldn't change even if
1265 * force_poll_sync changes.
1267 switch (urb->dev->speed) {
1268 case USB_SPEED_SUPER_PLUS:
1269 case USB_SPEED_SUPER: /* units are 125us */
1270 data->intr_interval = usecs_to_jiffies(urb->interval * 125);
1273 data->intr_interval = msecs_to_jiffies(urb->interval);
1283 static void btusb_bulk_complete(struct urb *urb)
1285 struct hci_dev *hdev = urb->context;
1286 struct btusb_data *data = hci_get_drvdata(hdev);
1289 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1290 urb->actual_length);
1292 if (!test_bit(HCI_RUNNING, &hdev->flags))
1295 if (urb->status == 0) {
1296 hdev->stat.byte_rx += urb->actual_length;
1298 if (data->recv_bulk(data, urb->transfer_buffer,
1299 urb->actual_length) < 0) {
1300 bt_dev_err(hdev, "corrupted ACL packet");
1301 hdev->stat.err_rx++;
1303 } else if (urb->status == -ENOENT) {
1304 /* Avoid suspend failed when usb_kill_urb */
1308 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
1311 usb_anchor_urb(urb, &data->bulk_anchor);
1312 usb_mark_last_busy(data->udev);
1314 err = usb_submit_urb(urb, GFP_ATOMIC);
1316 /* -EPERM: urb is being killed;
1317 * -ENODEV: device got disconnected
1319 if (err != -EPERM && err != -ENODEV)
1320 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1322 usb_unanchor_urb(urb);
1326 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
1328 struct btusb_data *data = hci_get_drvdata(hdev);
1332 int err, size = HCI_MAX_FRAME_SIZE;
1334 BT_DBG("%s", hdev->name);
1336 if (!data->bulk_rx_ep)
1339 urb = usb_alloc_urb(0, mem_flags);
1343 buf = kmalloc(size, mem_flags);
1349 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1351 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1352 btusb_bulk_complete, hdev);
1354 urb->transfer_flags |= URB_FREE_BUFFER;
1356 usb_mark_last_busy(data->udev);
1357 usb_anchor_urb(urb, &data->bulk_anchor);
1359 err = usb_submit_urb(urb, mem_flags);
1361 if (err != -EPERM && err != -ENODEV)
1362 bt_dev_err(hdev, "urb %p submission failed (%d)",
1364 usb_unanchor_urb(urb);
1372 static void btusb_isoc_complete(struct urb *urb)
1374 struct hci_dev *hdev = urb->context;
1375 struct btusb_data *data = hci_get_drvdata(hdev);
1378 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1379 urb->actual_length);
1381 if (!test_bit(HCI_RUNNING, &hdev->flags))
1384 if (urb->status == 0) {
1385 for (i = 0; i < urb->number_of_packets; i++) {
1386 unsigned int offset = urb->iso_frame_desc[i].offset;
1387 unsigned int length = urb->iso_frame_desc[i].actual_length;
1389 if (urb->iso_frame_desc[i].status)
1392 hdev->stat.byte_rx += length;
1394 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1396 bt_dev_err(hdev, "corrupted SCO packet");
1397 hdev->stat.err_rx++;
1400 } else if (urb->status == -ENOENT) {
1401 /* Avoid suspend failed when usb_kill_urb */
1405 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1408 usb_anchor_urb(urb, &data->isoc_anchor);
1410 err = usb_submit_urb(urb, GFP_ATOMIC);
1412 /* -EPERM: urb is being killed;
1413 * -ENODEV: device got disconnected
1415 if (err != -EPERM && err != -ENODEV)
1416 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1418 usb_unanchor_urb(urb);
1422 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1423 int mtu, struct btusb_data *data)
1425 int i = 0, offset = 0;
1426 unsigned int interval;
1428 BT_DBG("len %d mtu %d", len, mtu);
1430 /* For mSBC ALT 6 settings some chips need to transmit the data
1431 * continuously without the zero length of USB packets.
1433 if (test_bit(BTUSB_ALT6_CONTINUOUS_TX, &data->flags))
1434 goto ignore_usb_alt6_packet_flow;
1436 /* For mSBC ALT 6 setting the host will send the packet at continuous
1437 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1438 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1439 * To maintain the rate we send 63bytes of usb packets alternatively for
1440 * 7ms and 8ms to maintain the rate as 7.5ms.
1442 if (data->usb_alt6_packet_flow) {
1444 data->usb_alt6_packet_flow = false;
1447 data->usb_alt6_packet_flow = true;
1450 for (i = 0; i < interval; i++) {
1451 urb->iso_frame_desc[i].offset = offset;
1452 urb->iso_frame_desc[i].length = offset;
1455 ignore_usb_alt6_packet_flow:
1456 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1457 urb->iso_frame_desc[i].offset = offset;
1458 urb->iso_frame_desc[i].length = len;
1462 urb->number_of_packets = i;
1465 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1469 BT_DBG("len %d mtu %d", len, mtu);
1471 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1472 i++, offset += mtu, len -= mtu) {
1473 urb->iso_frame_desc[i].offset = offset;
1474 urb->iso_frame_desc[i].length = mtu;
1477 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1478 urb->iso_frame_desc[i].offset = offset;
1479 urb->iso_frame_desc[i].length = len;
1483 urb->number_of_packets = i;
1486 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1488 struct btusb_data *data = hci_get_drvdata(hdev);
1494 BT_DBG("%s", hdev->name);
1496 if (!data->isoc_rx_ep)
1499 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1503 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1504 BTUSB_MAX_ISOC_FRAMES;
1506 buf = kmalloc(size, mem_flags);
1512 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1514 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1515 hdev, data->isoc_rx_ep->bInterval);
1517 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1519 __fill_isoc_descriptor(urb, size,
1520 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1522 usb_anchor_urb(urb, &data->isoc_anchor);
1524 err = usb_submit_urb(urb, mem_flags);
1526 if (err != -EPERM && err != -ENODEV)
1527 bt_dev_err(hdev, "urb %p submission failed (%d)",
1529 usb_unanchor_urb(urb);
1537 static void btusb_diag_complete(struct urb *urb)
1539 struct hci_dev *hdev = urb->context;
1540 struct btusb_data *data = hci_get_drvdata(hdev);
1543 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1544 urb->actual_length);
1546 if (urb->status == 0) {
1547 struct sk_buff *skb;
1549 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1551 skb_put_data(skb, urb->transfer_buffer,
1552 urb->actual_length);
1553 hci_recv_diag(hdev, skb);
1555 } else if (urb->status == -ENOENT) {
1556 /* Avoid suspend failed when usb_kill_urb */
1560 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1563 usb_anchor_urb(urb, &data->diag_anchor);
1564 usb_mark_last_busy(data->udev);
1566 err = usb_submit_urb(urb, GFP_ATOMIC);
1568 /* -EPERM: urb is being killed;
1569 * -ENODEV: device got disconnected
1571 if (err != -EPERM && err != -ENODEV)
1572 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1574 usb_unanchor_urb(urb);
1578 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1580 struct btusb_data *data = hci_get_drvdata(hdev);
1584 int err, size = HCI_MAX_FRAME_SIZE;
1586 BT_DBG("%s", hdev->name);
1588 if (!data->diag_rx_ep)
1591 urb = usb_alloc_urb(0, mem_flags);
1595 buf = kmalloc(size, mem_flags);
1601 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1603 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1604 btusb_diag_complete, hdev);
1606 urb->transfer_flags |= URB_FREE_BUFFER;
1608 usb_mark_last_busy(data->udev);
1609 usb_anchor_urb(urb, &data->diag_anchor);
1611 err = usb_submit_urb(urb, mem_flags);
1613 if (err != -EPERM && err != -ENODEV)
1614 bt_dev_err(hdev, "urb %p submission failed (%d)",
1616 usb_unanchor_urb(urb);
1624 static void btusb_tx_complete(struct urb *urb)
1626 struct sk_buff *skb = urb->context;
1627 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1628 struct btusb_data *data = hci_get_drvdata(hdev);
1629 unsigned long flags;
1631 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1632 urb->actual_length);
1634 if (!test_bit(HCI_RUNNING, &hdev->flags))
1638 hdev->stat.byte_tx += urb->transfer_buffer_length;
1640 if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)
1641 hci_cmd_sync_cancel(hdev, -urb->status);
1642 hdev->stat.err_tx++;
1646 spin_lock_irqsave(&data->txlock, flags);
1647 data->tx_in_flight--;
1648 spin_unlock_irqrestore(&data->txlock, flags);
1650 kfree(urb->setup_packet);
1655 static void btusb_isoc_tx_complete(struct urb *urb)
1657 struct sk_buff *skb = urb->context;
1658 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1660 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1661 urb->actual_length);
1663 if (!test_bit(HCI_RUNNING, &hdev->flags))
1667 hdev->stat.byte_tx += urb->transfer_buffer_length;
1669 hdev->stat.err_tx++;
1672 kfree(urb->setup_packet);
1677 static int btusb_open(struct hci_dev *hdev)
1679 struct btusb_data *data = hci_get_drvdata(hdev);
1682 BT_DBG("%s", hdev->name);
1684 err = usb_autopm_get_interface(data->intf);
1688 /* Patching USB firmware files prior to starting any URBs of HCI path
1689 * It is more safe to use USB bulk channel for downloading USB patch
1691 if (data->setup_on_usb) {
1692 err = data->setup_on_usb(hdev);
1697 data->intf->needs_remote_wakeup = 1;
1699 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1702 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1706 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1708 usb_kill_anchored_urbs(&data->intr_anchor);
1712 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1713 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1716 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1717 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1721 usb_autopm_put_interface(data->intf);
1725 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1727 usb_autopm_put_interface(data->intf);
1731 static void btusb_stop_traffic(struct btusb_data *data)
1733 usb_kill_anchored_urbs(&data->intr_anchor);
1734 usb_kill_anchored_urbs(&data->bulk_anchor);
1735 usb_kill_anchored_urbs(&data->isoc_anchor);
1736 usb_kill_anchored_urbs(&data->diag_anchor);
1737 usb_kill_anchored_urbs(&data->ctrl_anchor);
1740 static int btusb_close(struct hci_dev *hdev)
1742 struct btusb_data *data = hci_get_drvdata(hdev);
1745 BT_DBG("%s", hdev->name);
1747 cancel_delayed_work(&data->rx_work);
1748 cancel_work_sync(&data->work);
1749 cancel_work_sync(&data->waker);
1751 skb_queue_purge(&data->acl_q);
1753 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1754 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1755 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1756 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1758 btusb_stop_traffic(data);
1759 btusb_free_frags(data);
1761 err = usb_autopm_get_interface(data->intf);
1765 data->intf->needs_remote_wakeup = 0;
1767 /* Enable remote wake up for auto-suspend */
1768 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags))
1769 data->intf->needs_remote_wakeup = 1;
1771 usb_autopm_put_interface(data->intf);
1774 usb_scuttle_anchored_urbs(&data->deferred);
1778 static int btusb_flush(struct hci_dev *hdev)
1780 struct btusb_data *data = hci_get_drvdata(hdev);
1782 BT_DBG("%s", hdev->name);
1784 cancel_delayed_work(&data->rx_work);
1786 skb_queue_purge(&data->acl_q);
1788 usb_kill_anchored_urbs(&data->tx_anchor);
1789 btusb_free_frags(data);
1794 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1796 struct btusb_data *data = hci_get_drvdata(hdev);
1797 struct usb_ctrlrequest *dr;
1801 urb = usb_alloc_urb(0, GFP_KERNEL);
1803 return ERR_PTR(-ENOMEM);
1805 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1808 return ERR_PTR(-ENOMEM);
1811 dr->bRequestType = data->cmdreq_type;
1812 dr->bRequest = data->cmdreq;
1815 dr->wLength = __cpu_to_le16(skb->len);
1817 pipe = usb_sndctrlpipe(data->udev, 0x00);
1819 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1820 skb->data, skb->len, btusb_tx_complete, skb);
1822 skb->dev = (void *)hdev;
1827 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1829 struct btusb_data *data = hci_get_drvdata(hdev);
1833 if (!data->bulk_tx_ep)
1834 return ERR_PTR(-ENODEV);
1836 urb = usb_alloc_urb(0, GFP_KERNEL);
1838 return ERR_PTR(-ENOMEM);
1840 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1842 usb_fill_bulk_urb(urb, data->udev, pipe,
1843 skb->data, skb->len, btusb_tx_complete, skb);
1845 skb->dev = (void *)hdev;
1850 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1852 struct btusb_data *data = hci_get_drvdata(hdev);
1856 if (!data->isoc_tx_ep)
1857 return ERR_PTR(-ENODEV);
1859 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1861 return ERR_PTR(-ENOMEM);
1863 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1865 usb_fill_int_urb(urb, data->udev, pipe,
1866 skb->data, skb->len, btusb_isoc_tx_complete,
1867 skb, data->isoc_tx_ep->bInterval);
1869 urb->transfer_flags = URB_ISO_ASAP;
1871 if (data->isoc_altsetting == 6)
1872 __fill_isoc_descriptor_msbc(urb, skb->len,
1873 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1876 __fill_isoc_descriptor(urb, skb->len,
1877 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1878 skb->dev = (void *)hdev;
1883 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1885 struct btusb_data *data = hci_get_drvdata(hdev);
1888 usb_anchor_urb(urb, &data->tx_anchor);
1890 err = usb_submit_urb(urb, GFP_KERNEL);
1892 if (err != -EPERM && err != -ENODEV)
1893 bt_dev_err(hdev, "urb %p submission failed (%d)",
1895 kfree(urb->setup_packet);
1896 usb_unanchor_urb(urb);
1898 usb_mark_last_busy(data->udev);
1905 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1907 struct btusb_data *data = hci_get_drvdata(hdev);
1908 unsigned long flags;
1911 spin_lock_irqsave(&data->txlock, flags);
1912 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1914 data->tx_in_flight++;
1915 spin_unlock_irqrestore(&data->txlock, flags);
1918 return submit_tx_urb(hdev, urb);
1920 usb_anchor_urb(urb, &data->deferred);
1921 schedule_work(&data->waker);
1927 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1931 BT_DBG("%s", hdev->name);
1933 switch (hci_skb_pkt_type(skb)) {
1934 case HCI_COMMAND_PKT:
1935 urb = alloc_ctrl_urb(hdev, skb);
1937 return PTR_ERR(urb);
1939 hdev->stat.cmd_tx++;
1940 return submit_or_queue_tx_urb(hdev, urb);
1942 case HCI_ACLDATA_PKT:
1943 urb = alloc_bulk_urb(hdev, skb);
1945 return PTR_ERR(urb);
1947 hdev->stat.acl_tx++;
1948 return submit_or_queue_tx_urb(hdev, urb);
1950 case HCI_SCODATA_PKT:
1951 if (hci_conn_num(hdev, SCO_LINK) < 1)
1954 urb = alloc_isoc_urb(hdev, skb);
1956 return PTR_ERR(urb);
1958 hdev->stat.sco_tx++;
1959 return submit_tx_urb(hdev, urb);
1961 case HCI_ISODATA_PKT:
1962 urb = alloc_bulk_urb(hdev, skb);
1964 return PTR_ERR(urb);
1966 return submit_or_queue_tx_urb(hdev, urb);
1972 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1974 struct btusb_data *data = hci_get_drvdata(hdev);
1976 BT_DBG("%s evt %d", hdev->name, evt);
1978 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1979 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1980 data->air_mode = evt;
1981 schedule_work(&data->work);
1985 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1987 struct btusb_data *data = hci_get_drvdata(hdev);
1988 struct usb_interface *intf = data->isoc;
1989 struct usb_endpoint_descriptor *ep_desc;
1995 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
1997 bt_dev_err(hdev, "setting interface failed (%d)", -err);
2001 data->isoc_altsetting = altsetting;
2003 data->isoc_tx_ep = NULL;
2004 data->isoc_rx_ep = NULL;
2006 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2007 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2009 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
2010 data->isoc_tx_ep = ep_desc;
2014 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
2015 data->isoc_rx_ep = ep_desc;
2020 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
2021 bt_dev_err(hdev, "invalid SCO descriptors");
2028 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
2030 struct btusb_data *data = hci_get_drvdata(hdev);
2033 if (data->isoc_altsetting != new_alts) {
2034 unsigned long flags;
2036 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2037 usb_kill_anchored_urbs(&data->isoc_anchor);
2039 /* When isochronous alternate setting needs to be
2040 * changed, because SCO connection has been added
2041 * or removed, a packet fragment may be left in the
2042 * reassembling state. This could lead to wrongly
2043 * assembled fragments.
2045 * Clear outstanding fragment when selecting a new
2046 * alternate setting.
2048 spin_lock_irqsave(&data->rxlock, flags);
2049 kfree_skb(data->sco_skb);
2050 data->sco_skb = NULL;
2051 spin_unlock_irqrestore(&data->rxlock, flags);
2053 err = __set_isoc_interface(hdev, new_alts);
2058 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2059 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
2060 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2062 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
2068 static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
2071 struct usb_interface *intf = data->isoc;
2074 BT_DBG("Looking for Alt no :%d", alt);
2079 for (i = 0; i < intf->num_altsetting; i++) {
2080 if (intf->altsetting[i].desc.bAlternateSetting == alt)
2081 return &intf->altsetting[i];
2087 static void btusb_work(struct work_struct *work)
2089 struct btusb_data *data = container_of(work, struct btusb_data, work);
2090 struct hci_dev *hdev = data->hdev;
2094 if (data->sco_num > 0) {
2095 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
2096 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
2098 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2099 usb_kill_anchored_urbs(&data->isoc_anchor);
2103 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
2106 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
2107 if (hdev->voice_setting & 0x0020) {
2108 static const int alts[3] = { 2, 4, 5 };
2110 new_alts = alts[data->sco_num - 1];
2112 new_alts = data->sco_num;
2114 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
2115 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
2116 * many adapters do not support it. Alt 1 appears to
2117 * work for all adapters that do not have alt 6, and
2118 * which work with WBS at all. Some devices prefer
2119 * alt 3 (HCI payload >= 60 Bytes let air packet
2120 * data satisfy 60 bytes), requiring
2121 * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
2122 * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
2124 if (btusb_find_altsetting(data, 6))
2126 else if (btusb_find_altsetting(data, 3) &&
2127 hdev->sco_mtu >= 72 &&
2128 test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
2134 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
2135 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
2137 usb_kill_anchored_urbs(&data->isoc_anchor);
2139 if (test_and_clear_bit(BTUSB_ISOC_RUNNING, &data->flags))
2140 __set_isoc_interface(hdev, 0);
2142 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
2143 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
2147 static void btusb_waker(struct work_struct *work)
2149 struct btusb_data *data = container_of(work, struct btusb_data, waker);
2152 err = usb_autopm_get_interface(data->intf);
2156 usb_autopm_put_interface(data->intf);
2159 static void btusb_rx_work(struct work_struct *work)
2161 struct btusb_data *data = container_of(work, struct btusb_data,
2163 struct sk_buff *skb;
2165 /* Dequeue ACL data received during the interval */
2166 while ((skb = skb_dequeue(&data->acl_q)))
2167 data->recv_acl(data->hdev, skb);
2170 static int btusb_setup_bcm92035(struct hci_dev *hdev)
2172 struct sk_buff *skb;
2175 BT_DBG("%s", hdev->name);
2177 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
2179 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
2186 static int btusb_setup_csr(struct hci_dev *hdev)
2188 struct btusb_data *data = hci_get_drvdata(hdev);
2189 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
2190 struct hci_rp_read_local_version *rp;
2191 struct sk_buff *skb;
2192 bool is_fake = false;
2195 BT_DBG("%s", hdev->name);
2197 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2200 int err = PTR_ERR(skb);
2201 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
2205 rp = skb_pull_data(skb, sizeof(*rp));
2207 bt_dev_err(hdev, "CSR: Local version length mismatch");
2212 bt_dev_info(hdev, "CSR: Setting up dongle with HCI ver=%u rev=%04x",
2213 rp->hci_ver, le16_to_cpu(rp->hci_rev));
2215 bt_dev_info(hdev, "LMP ver=%u subver=%04x; manufacturer=%u",
2216 rp->lmp_ver, le16_to_cpu(rp->lmp_subver),
2217 le16_to_cpu(rp->manufacturer));
2219 /* Detect a wide host of Chinese controllers that aren't CSR.
2221 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
2223 * The main thing they have in common is that these are really popular low-cost
2224 * options that support newer Bluetooth versions but rely on heavy VID/PID
2225 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
2227 * We detect actual CSR devices by checking that the HCI manufacturer code
2228 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
2229 * HCI rev values always match. As they both store the firmware number.
2231 if (le16_to_cpu(rp->manufacturer) != 10 ||
2232 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
2235 /* Known legit CSR firmware build numbers and their supported BT versions:
2236 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
2237 * - 1.2 (0x2) -> 0x04d9, 0x0529
2238 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
2239 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
2240 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
2242 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
2243 * support BT 1.1 only; so it's a dead giveaway when some
2244 * third-party BT 4.0 dongle reuses it.
2246 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
2247 rp->hci_ver > BLUETOOTH_VER_1_1)
2250 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
2251 rp->hci_ver > BLUETOOTH_VER_1_2)
2254 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
2255 rp->hci_ver > BLUETOOTH_VER_2_0)
2258 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
2259 rp->hci_ver > BLUETOOTH_VER_2_1)
2262 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
2263 rp->hci_ver > BLUETOOTH_VER_4_0)
2266 /* Other clones which beat all the above checks */
2267 else if (bcdDevice == 0x0134 &&
2268 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
2269 rp->hci_ver == BLUETOOTH_VER_2_0)
2273 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
2275 /* Generally these clones have big discrepancies between
2276 * advertised features and what's actually supported.
2277 * Probably will need to be expanded in the future;
2278 * without these the controller will lock up.
2280 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2281 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
2282 set_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks);
2283 set_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks);
2285 /* Clear the reset quirk since this is not an actual
2286 * early Bluetooth 1.1 device from CSR.
2288 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2289 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2292 * Special workaround for these BT 4.0 chip clones, and potentially more:
2294 * - 0x0134: a Barrot 8041a02 (HCI rev: 0x0810 sub: 0x1012)
2295 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
2297 * These controllers are really messed-up.
2299 * 1. Their bulk RX endpoint will never report any data unless
2300 * the device was suspended at least once (yes, really).
2301 * 2. They will not wakeup when autosuspended and receiving data
2302 * on their bulk RX endpoint from e.g. a keyboard or mouse
2303 * (IOW remote-wakeup support is broken for the bulk endpoint).
2305 * To fix 1. enable runtime-suspend, force-suspend the
2306 * HCI and then wake-it up by disabling runtime-suspend.
2308 * To fix 2. clear the HCI's can_wake flag, this way the HCI
2309 * will still be autosuspended when it is not open.
2313 * Because these are widespread problems we prefer generic solutions; so
2314 * apply this initialization quirk to every controller that gets here,
2315 * it should be harmless. The alternative is to not work at all.
2317 pm_runtime_allow(&data->udev->dev);
2319 ret = pm_runtime_suspend(&data->udev->dev);
2323 bt_dev_warn(hdev, "CSR: Couldn't suspend the device for our Barrot 8041a02 receive-issue workaround");
2325 pm_runtime_forbid(&data->udev->dev);
2327 device_set_wakeup_capable(&data->udev->dev, false);
2329 /* Re-enable autosuspend if this was requested */
2330 if (enable_autosuspend)
2331 usb_enable_autosuspend(data->udev);
2339 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2341 struct sk_buff *skb;
2342 struct hci_event_hdr *hdr;
2343 struct hci_ev_cmd_complete *evt;
2345 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2349 hdr = skb_put(skb, sizeof(*hdr));
2350 hdr->evt = HCI_EV_CMD_COMPLETE;
2351 hdr->plen = sizeof(*evt) + 1;
2353 evt = skb_put(skb, sizeof(*evt));
2355 evt->opcode = cpu_to_le16(opcode);
2357 skb_put_u8(skb, 0x00);
2359 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2361 return hci_recv_frame(hdev, skb);
2364 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2367 struct hci_dev *hdev = data->hdev;
2369 /* When the device is in bootloader mode, then it can send
2370 * events via the bulk endpoint. These events are treated the
2371 * same way as the ones received from the interrupt endpoint.
2373 if (btintel_test_flag(hdev, INTEL_BOOTLOADER))
2374 return btusb_recv_intr(data, buffer, count);
2376 return btusb_recv_bulk(data, buffer, count);
2379 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2381 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2382 struct hci_event_hdr *hdr = (void *)skb->data;
2384 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2386 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2387 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
2389 switch (skb->data[2]) {
2391 /* When switching to the operational firmware
2392 * the device sends a vendor specific event
2393 * indicating that the bootup completed.
2395 btintel_bootup(hdev, ptr, len);
2398 /* When the firmware loading completes the
2399 * device sends out a vendor specific event
2400 * indicating the result of the firmware
2403 btintel_secure_send_result(hdev, ptr, len);
2409 return hci_recv_frame(hdev, skb);
2412 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2416 BT_DBG("%s", hdev->name);
2418 switch (hci_skb_pkt_type(skb)) {
2419 case HCI_COMMAND_PKT:
2420 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2421 struct hci_command_hdr *cmd = (void *)skb->data;
2422 __u16 opcode = le16_to_cpu(cmd->opcode);
2424 /* When in bootloader mode and the command 0xfc09
2425 * is received, it needs to be send down the
2426 * bulk endpoint. So allocate a bulk URB instead.
2428 if (opcode == 0xfc09)
2429 urb = alloc_bulk_urb(hdev, skb);
2431 urb = alloc_ctrl_urb(hdev, skb);
2433 /* When the 0xfc01 command is issued to boot into
2434 * the operational firmware, it will actually not
2435 * send a command complete event. To keep the flow
2436 * control working inject that event here.
2438 if (opcode == 0xfc01)
2439 inject_cmd_complete(hdev, opcode);
2441 urb = alloc_ctrl_urb(hdev, skb);
2444 return PTR_ERR(urb);
2446 hdev->stat.cmd_tx++;
2447 return submit_or_queue_tx_urb(hdev, urb);
2449 case HCI_ACLDATA_PKT:
2450 urb = alloc_bulk_urb(hdev, skb);
2452 return PTR_ERR(urb);
2454 hdev->stat.acl_tx++;
2455 return submit_or_queue_tx_urb(hdev, urb);
2457 case HCI_SCODATA_PKT:
2458 if (hci_conn_num(hdev, SCO_LINK) < 1)
2461 urb = alloc_isoc_urb(hdev, skb);
2463 return PTR_ERR(urb);
2465 hdev->stat.sco_tx++;
2466 return submit_tx_urb(hdev, urb);
2468 case HCI_ISODATA_PKT:
2469 urb = alloc_bulk_urb(hdev, skb);
2471 return PTR_ERR(urb);
2473 return submit_or_queue_tx_urb(hdev, urb);
2479 static int btusb_setup_realtek(struct hci_dev *hdev)
2481 struct btusb_data *data = hci_get_drvdata(hdev);
2484 ret = btrtl_setup_realtek(hdev);
2486 if (btrealtek_test_flag(data->hdev, REALTEK_ALT6_CONTINUOUS_TX_CHIP))
2487 set_bit(BTUSB_ALT6_CONTINUOUS_TX, &data->flags);
2492 /* UHW CR mapping */
2493 #define MTK_BT_MISC 0x70002510
2494 #define MTK_BT_SUBSYS_RST 0x70002610
2495 #define MTK_UDMA_INT_STA_BT 0x74000024
2496 #define MTK_UDMA_INT_STA_BT1 0x74000308
2497 #define MTK_BT_WDT_STATUS 0x740003A0
2498 #define MTK_EP_RST_OPT 0x74011890
2499 #define MTK_EP_RST_IN_OUT_OPT 0x00010001
2500 #define MTK_BT_RST_DONE 0x00000100
2501 #define MTK_BT_RESET_WAIT_MS 100
2502 #define MTK_BT_RESET_NUM_TRIES 10
2504 static void btusb_mtk_wmt_recv(struct urb *urb)
2506 struct hci_dev *hdev = urb->context;
2507 struct btusb_data *data = hci_get_drvdata(hdev);
2508 struct sk_buff *skb;
2511 if (urb->status == 0 && urb->actual_length > 0) {
2512 hdev->stat.byte_rx += urb->actual_length;
2514 /* WMT event shouldn't be fragmented and the size should be
2515 * less than HCI_WMT_MAX_EVENT_SIZE.
2517 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2519 hdev->stat.err_rx++;
2520 kfree(urb->setup_packet);
2524 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2525 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2527 /* When someone waits for the WMT event, the skb is being cloned
2528 * and being processed the events from there then.
2530 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
2531 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
2532 if (!data->evt_skb) {
2534 kfree(urb->setup_packet);
2539 err = hci_recv_frame(hdev, skb);
2541 kfree_skb(data->evt_skb);
2542 data->evt_skb = NULL;
2543 kfree(urb->setup_packet);
2547 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2549 /* Barrier to sync with other CPUs */
2550 smp_mb__after_atomic();
2551 wake_up_bit(&data->flags,
2552 BTUSB_TX_WAIT_VND_EVT);
2554 kfree(urb->setup_packet);
2556 } else if (urb->status == -ENOENT) {
2557 /* Avoid suspend failed when usb_kill_urb */
2561 usb_mark_last_busy(data->udev);
2563 /* The URB complete handler is still called with urb->actual_length = 0
2564 * when the event is not available, so we should keep re-submitting
2565 * URB until WMT event returns, Also, It's necessary to wait some time
2566 * between the two consecutive control URBs to relax the target device
2567 * to generate the event. Otherwise, the WMT event cannot return from
2568 * the device successfully.
2572 usb_anchor_urb(urb, &data->ctrl_anchor);
2573 err = usb_submit_urb(urb, GFP_ATOMIC);
2575 kfree(urb->setup_packet);
2576 /* -EPERM: urb is being killed;
2577 * -ENODEV: device got disconnected
2579 if (err != -EPERM && err != -ENODEV)
2580 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2582 usb_unanchor_urb(urb);
2586 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2588 struct btusb_data *data = hci_get_drvdata(hdev);
2589 struct usb_ctrlrequest *dr;
2595 urb = usb_alloc_urb(0, GFP_KERNEL);
2599 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2605 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2607 dr->wIndex = cpu_to_le16(0);
2608 dr->wValue = cpu_to_le16(48);
2609 dr->wLength = cpu_to_le16(size);
2611 buf = kmalloc(size, GFP_KERNEL);
2618 pipe = usb_rcvctrlpipe(data->udev, 0);
2620 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2621 buf, size, btusb_mtk_wmt_recv, hdev);
2623 urb->transfer_flags |= URB_FREE_BUFFER;
2625 usb_anchor_urb(urb, &data->ctrl_anchor);
2626 err = usb_submit_urb(urb, GFP_KERNEL);
2628 if (err != -EPERM && err != -ENODEV)
2629 bt_dev_err(hdev, "urb %p submission failed (%d)",
2631 usb_unanchor_urb(urb);
2639 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
2640 struct btmtk_hci_wmt_params *wmt_params)
2642 struct btusb_data *data = hci_get_drvdata(hdev);
2643 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
2644 u32 hlen, status = BTMTK_WMT_INVALID;
2645 struct btmtk_hci_wmt_evt *wmt_evt;
2646 struct btmtk_hci_wmt_cmd *wc;
2647 struct btmtk_wmt_hdr *hdr;
2650 /* Send the WMT command and wait until the WMT event returns */
2651 hlen = sizeof(*hdr) + wmt_params->dlen;
2655 wc = kzalloc(hlen, GFP_KERNEL);
2661 hdr->op = wmt_params->op;
2662 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
2663 hdr->flag = wmt_params->flag;
2664 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
2666 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2668 /* WMT cmd/event doesn't follow up the generic HCI cmd/event handling,
2669 * it needs constantly polling control pipe until the host received the
2670 * WMT event, thus, we should require to specifically acquire PM counter
2671 * on the USB to prevent the interface from entering auto suspended
2672 * while WMT cmd/event in progress.
2674 err = usb_autopm_get_interface(data->intf);
2678 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
2681 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2682 usb_autopm_put_interface(data->intf);
2686 /* Submit control IN URB on demand to process the WMT event */
2687 err = btusb_mtk_submit_wmt_recv_urb(hdev);
2689 usb_autopm_put_interface(data->intf);
2694 /* The vendor specific WMT commands are all answered by a vendor
2695 * specific event and will have the Command Status or Command
2696 * Complete as with usual HCI command flow control.
2698 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
2699 * state to be cleared. The driver specific event receive routine
2700 * will clear that state and with that indicate completion of the
2703 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
2704 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
2705 if (err == -EINTR) {
2706 bt_dev_err(hdev, "Execution of wmt command interrupted");
2707 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2712 bt_dev_err(hdev, "Execution of wmt command timed out");
2713 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2718 /* Parse and handle the return WMT event */
2719 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
2720 if (wmt_evt->whdr.op != hdr->op) {
2721 bt_dev_err(hdev, "Wrong op received %d expected %d",
2722 wmt_evt->whdr.op, hdr->op);
2727 switch (wmt_evt->whdr.op) {
2728 case BTMTK_WMT_SEMAPHORE:
2729 if (wmt_evt->whdr.flag == 2)
2730 status = BTMTK_WMT_PATCH_UNDONE;
2732 status = BTMTK_WMT_PATCH_DONE;
2734 case BTMTK_WMT_FUNC_CTRL:
2735 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
2736 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
2737 status = BTMTK_WMT_ON_DONE;
2738 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
2739 status = BTMTK_WMT_ON_PROGRESS;
2741 status = BTMTK_WMT_ON_UNDONE;
2743 case BTMTK_WMT_PATCH_DWNLD:
2744 if (wmt_evt->whdr.flag == 2)
2745 status = BTMTK_WMT_PATCH_DONE;
2746 else if (wmt_evt->whdr.flag == 1)
2747 status = BTMTK_WMT_PATCH_PROGRESS;
2749 status = BTMTK_WMT_PATCH_UNDONE;
2753 if (wmt_params->status)
2754 *wmt_params->status = status;
2757 kfree_skb(data->evt_skb);
2758 data->evt_skb = NULL;
2764 static int btusb_mtk_func_query(struct hci_dev *hdev)
2766 struct btmtk_hci_wmt_params wmt_params;
2770 /* Query whether the function is enabled */
2771 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2772 wmt_params.flag = 4;
2773 wmt_params.dlen = sizeof(param);
2774 wmt_params.data = ¶m;
2775 wmt_params.status = &status;
2777 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2779 bt_dev_err(hdev, "Failed to query function status (%d)", err);
2786 static int btusb_mtk_uhw_reg_write(struct btusb_data *data, u32 reg, u32 val)
2788 struct hci_dev *hdev = data->hdev;
2792 buf = kzalloc(4, GFP_KERNEL);
2796 put_unaligned_le32(val, buf);
2798 pipe = usb_sndctrlpipe(data->udev, 0);
2799 err = usb_control_msg(data->udev, pipe, 0x02,
2801 reg >> 16, reg & 0xffff,
2802 buf, 4, USB_CTRL_SET_TIMEOUT);
2804 bt_dev_err(hdev, "Failed to write uhw reg(%d)", err);
2814 static int btusb_mtk_uhw_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2816 struct hci_dev *hdev = data->hdev;
2820 buf = kzalloc(4, GFP_KERNEL);
2824 pipe = usb_rcvctrlpipe(data->udev, 0);
2825 err = usb_control_msg(data->udev, pipe, 0x01,
2827 reg >> 16, reg & 0xffff,
2828 buf, 4, USB_CTRL_SET_TIMEOUT);
2830 bt_dev_err(hdev, "Failed to read uhw reg(%d)", err);
2834 *val = get_unaligned_le32(buf);
2835 bt_dev_dbg(hdev, "reg=%x, value=0x%08x", reg, *val);
2843 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2845 int pipe, err, size = sizeof(u32);
2848 buf = kzalloc(size, GFP_KERNEL);
2852 pipe = usb_rcvctrlpipe(data->udev, 0);
2853 err = usb_control_msg(data->udev, pipe, 0x63,
2854 USB_TYPE_VENDOR | USB_DIR_IN,
2855 reg >> 16, reg & 0xffff,
2856 buf, size, USB_CTRL_SET_TIMEOUT);
2860 *val = get_unaligned_le32(buf);
2868 static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
2870 return btusb_mtk_reg_read(data, reg, id);
2873 static int btusb_mtk_setup(struct hci_dev *hdev)
2875 struct btusb_data *data = hci_get_drvdata(hdev);
2876 struct btmtk_hci_wmt_params wmt_params;
2877 ktime_t calltime, delta, rettime;
2878 struct btmtk_tci_sleep tci_sleep;
2879 unsigned long long duration;
2880 struct sk_buff *skb;
2884 char fw_bin_name[64];
2888 calltime = ktime_get();
2890 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
2892 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2897 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
2899 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2902 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
2904 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
2911 fwname = FIRMWARE_MT7663;
2914 fwname = FIRMWARE_MT7668;
2918 snprintf(fw_bin_name, sizeof(fw_bin_name),
2919 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
2920 dev_id & 0xffff, (fw_version & 0xff) + 1);
2921 err = btmtk_setup_firmware_79xx(hdev, fw_bin_name,
2922 btusb_mtk_hci_wmt_sync);
2924 bt_dev_err(hdev, "Failed to set up firmware (%d)", err);
2928 /* It's Device EndPoint Reset Option Register */
2929 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2931 /* Enable Bluetooth protocol */
2933 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2934 wmt_params.flag = 0;
2935 wmt_params.dlen = sizeof(param);
2936 wmt_params.data = ¶m;
2937 wmt_params.status = NULL;
2939 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2941 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2945 hci_set_msft_opcode(hdev, 0xFD30);
2946 hci_set_aosp_capable(hdev);
2949 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
2954 /* Query whether the firmware is already download */
2955 wmt_params.op = BTMTK_WMT_SEMAPHORE;
2956 wmt_params.flag = 1;
2957 wmt_params.dlen = 0;
2958 wmt_params.data = NULL;
2959 wmt_params.status = &status;
2961 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2963 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
2967 if (status == BTMTK_WMT_PATCH_DONE) {
2968 bt_dev_info(hdev, "firmware already downloaded");
2969 goto ignore_setup_fw;
2972 /* Setup a firmware which the device definitely requires */
2973 err = btmtk_setup_firmware(hdev, fwname,
2974 btusb_mtk_hci_wmt_sync);
2979 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
2980 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
2982 /* -ETIMEDOUT happens */
2986 /* The other errors happen in btusb_mtk_func_query */
2990 if (status == BTMTK_WMT_ON_DONE) {
2991 bt_dev_info(hdev, "function already on");
2992 goto ignore_func_on;
2995 /* Enable Bluetooth protocol */
2997 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2998 wmt_params.flag = 0;
2999 wmt_params.dlen = sizeof(param);
3000 wmt_params.data = ¶m;
3001 wmt_params.status = NULL;
3003 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3005 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3010 /* Apply the low power environment setup */
3011 tci_sleep.mode = 0x5;
3012 tci_sleep.duration = cpu_to_le16(0x640);
3013 tci_sleep.host_duration = cpu_to_le16(0x640);
3014 tci_sleep.host_wakeup_pin = 0;
3015 tci_sleep.time_compensation = 0;
3017 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
3021 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
3027 rettime = ktime_get();
3028 delta = ktime_sub(rettime, calltime);
3029 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3031 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
3036 static int btusb_mtk_shutdown(struct hci_dev *hdev)
3038 struct btmtk_hci_wmt_params wmt_params;
3042 /* Disable the device */
3043 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3044 wmt_params.flag = 0;
3045 wmt_params.dlen = sizeof(param);
3046 wmt_params.data = ¶m;
3047 wmt_params.status = NULL;
3049 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3051 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3058 static void btusb_mtk_cmd_timeout(struct hci_dev *hdev)
3060 struct btusb_data *data = hci_get_drvdata(hdev);
3064 /* It's MediaTek specific bluetooth reset mechanism via USB */
3065 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
3066 bt_dev_err(hdev, "last reset failed? Not resetting again");
3070 err = usb_autopm_get_interface(data->intf);
3074 btusb_stop_traffic(data);
3075 usb_kill_anchored_urbs(&data->tx_anchor);
3077 /* It's Device EndPoint Reset Option Register */
3078 bt_dev_dbg(hdev, "Initiating reset mechanism via uhw");
3079 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
3080 btusb_mtk_uhw_reg_read(data, MTK_BT_WDT_STATUS, &val);
3082 /* Reset the bluetooth chip via USB interface. */
3083 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 1);
3084 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT, 0x000000FF);
3085 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT, &val);
3086 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT1, 0x000000FF);
3087 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT1, &val);
3088 /* MT7921 need to delay 20ms between toggle reset bit */
3090 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 0);
3091 btusb_mtk_uhw_reg_read(data, MTK_BT_SUBSYS_RST, &val);
3093 /* Poll the register until reset is completed */
3095 btusb_mtk_uhw_reg_read(data, MTK_BT_MISC, &val);
3096 if (val & MTK_BT_RST_DONE) {
3097 bt_dev_dbg(hdev, "Bluetooth Reset Successfully");
3101 bt_dev_dbg(hdev, "Polling Bluetooth Reset CR");
3103 msleep(MTK_BT_RESET_WAIT_MS);
3104 } while (retry < MTK_BT_RESET_NUM_TRIES);
3106 btusb_mtk_id_get(data, 0x70010200, &val);
3108 bt_dev_err(hdev, "Can't get device id, subsys reset fail.");
3110 usb_queue_reset_device(data->intf);
3112 clear_bit(BTUSB_HW_RESET_ACTIVE, &data->flags);
3115 static int btusb_recv_acl_mtk(struct hci_dev *hdev, struct sk_buff *skb)
3117 struct btusb_data *data = hci_get_drvdata(hdev);
3118 u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
3121 case 0xfc6f: /* Firmware dump from device */
3122 /* When the firmware hangs, the device can no longer
3123 * suspend and thus disable auto-suspend.
3125 usb_disable_autosuspend(data->udev);
3127 case 0x05ff: /* Firmware debug logging 1 */
3128 case 0x05fe: /* Firmware debug logging 2 */
3129 return hci_recv_diag(hdev, skb);
3132 return hci_recv_frame(hdev, skb);
3136 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3137 static int marvell_config_oob_wake(struct hci_dev *hdev)
3139 struct sk_buff *skb;
3140 struct btusb_data *data = hci_get_drvdata(hdev);
3141 struct device *dev = &data->udev->dev;
3142 u16 pin, gap, opcode;
3146 /* Move on if no wakeup pin specified */
3147 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3148 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3151 /* Vendor specific command to configure a GPIO as wake-up pin */
3152 opcode = hci_opcode_pack(0x3F, 0x59);
3153 cmd[0] = opcode & 0xFF;
3154 cmd[1] = opcode >> 8;
3155 cmd[2] = 2; /* length of parameters that follow */
3157 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3159 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3161 bt_dev_err(hdev, "%s: No memory", __func__);
3165 skb_put_data(skb, cmd, sizeof(cmd));
3166 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3168 ret = btusb_send_frame(hdev, skb);
3170 bt_dev_err(hdev, "%s: configuration failed", __func__);
3179 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3180 const bdaddr_t *bdaddr)
3182 struct sk_buff *skb;
3187 buf[1] = sizeof(bdaddr_t);
3188 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3190 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3193 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3202 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3203 const bdaddr_t *bdaddr)
3205 struct sk_buff *skb;
3212 buf[3] = sizeof(bdaddr_t);
3213 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3215 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3218 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3226 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3227 const bdaddr_t *bdaddr)
3229 struct sk_buff *skb;
3233 memcpy(buf, bdaddr, sizeof(bdaddr_t));
3235 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3236 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3239 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3247 #define QCA_DFU_PACKET_LEN 4096
3249 #define QCA_GET_TARGET_VERSION 0x09
3250 #define QCA_CHECK_STATUS 0x05
3251 #define QCA_DFU_DOWNLOAD 0x01
3253 #define QCA_SYSCFG_UPDATED 0x40
3254 #define QCA_PATCH_UPDATED 0x80
3255 #define QCA_DFU_TIMEOUT 3000
3256 #define QCA_FLAG_MULTI_NVM 0x80
3257 #define QCA_BT_RESET_WAIT_MS 100
3259 #define WCN6855_2_0_RAM_VERSION_GF 0x400c1200
3260 #define WCN6855_2_1_RAM_VERSION_GF 0x400c1211
3262 struct qca_version {
3264 __le32 patch_version;
3272 struct qca_rampatch_version {
3273 __le16 rom_version_high;
3274 __le16 rom_version_low;
3275 __le16 patch_version;
3278 struct qca_device_info {
3280 u8 rampatch_hdr; /* length of header in rampatch */
3281 u8 nvm_hdr; /* length of header in NVM */
3282 u8 ver_offset; /* offset of version structure in rampatch */
3285 static const struct qca_device_info qca_devices_table[] = {
3286 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3287 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3288 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3289 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3290 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3291 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3292 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3293 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3294 { 0x00130201, 40, 4, 16 }, /* WCN6855 2.1 */
3295 { 0x00190200, 40, 4, 16 }, /* WCN785x 2.0 */
3298 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3299 void *data, u16 size)
3304 buf = kmalloc(size, GFP_KERNEL);
3308 /* Found some of USB hosts have IOT issues with ours so that we should
3309 * not wait until HCI layer is ready.
3311 pipe = usb_rcvctrlpipe(udev, 0);
3312 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3313 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3315 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3319 memcpy(data, buf, size);
3327 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3328 const struct firmware *firmware,
3331 struct btusb_data *btdata = hci_get_drvdata(hdev);
3332 struct usb_device *udev = btdata->udev;
3333 size_t count, size, sent = 0;
3337 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3341 count = firmware->size;
3343 size = min_t(size_t, count, hdr_size);
3344 memcpy(buf, firmware->data, size);
3346 /* USB patches should go down to controller through USB path
3347 * because binary format fits to go down through USB channel.
3348 * USB control path is for patching headers and USB bulk is for
3351 pipe = usb_sndctrlpipe(udev, 0);
3352 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3353 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3355 bt_dev_err(hdev, "Failed to send headers (%d)", err);
3362 /* ep2 need time to switch from function acl to function dfu,
3363 * so we add 20ms delay here.
3368 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3370 memcpy(buf, firmware->data + sent, size);
3372 pipe = usb_sndbulkpipe(udev, 0x02);
3373 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3376 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3377 sent, firmware->size, err);
3382 bt_dev_err(hdev, "Failed to get bulk buffer");
3396 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3397 struct qca_version *ver,
3398 const struct qca_device_info *info)
3400 struct qca_rampatch_version *rver;
3401 const struct firmware *fw;
3402 u32 ver_rom, ver_patch, rver_rom;
3403 u16 rver_rom_low, rver_rom_high, rver_patch;
3407 ver_rom = le32_to_cpu(ver->rom_version);
3408 ver_patch = le32_to_cpu(ver->patch_version);
3410 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
3412 err = request_firmware(&fw, fwname, &hdev->dev);
3414 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3419 bt_dev_info(hdev, "using rampatch file: %s", fwname);
3421 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3422 rver_rom_low = le16_to_cpu(rver->rom_version_low);
3423 rver_patch = le16_to_cpu(rver->patch_version);
3425 if (ver_rom & ~0xffffU) {
3426 rver_rom_high = le16_to_cpu(rver->rom_version_high);
3427 rver_rom = rver_rom_high << 16 | rver_rom_low;
3429 rver_rom = rver_rom_low;
3432 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3433 "firmware rome 0x%x build 0x%x",
3434 rver_rom, rver_patch, ver_rom, ver_patch);
3436 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
3437 bt_dev_err(hdev, "rampatch file version did not match with firmware");
3442 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3445 release_firmware(fw);
3450 static void btusb_generate_qca_nvm_name(char *fwname, size_t max_size,
3451 const struct qca_version *ver)
3453 u32 rom_version = le32_to_cpu(ver->rom_version);
3454 u16 flag = le16_to_cpu(ver->flag);
3456 if (((flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
3457 /* The board_id should be split into two bytes
3458 * The 1st byte is chip ID, and the 2nd byte is platform ID
3459 * For example, board ID 0x010A, 0x01 is platform ID. 0x0A is chip ID
3460 * we have several platforms, and platform IDs are continuously added
3462 * 0x00 is for Mobile
3464 * 0x02 is for Automotive
3465 * 0x03 is for Consumer electronic
3467 u16 board_id = (ver->chip_id << 8) + ver->platform_id;
3468 const char *variant;
3470 switch (le32_to_cpu(ver->ram_version)) {
3471 case WCN6855_2_0_RAM_VERSION_GF:
3472 case WCN6855_2_1_RAM_VERSION_GF:
3480 if (board_id == 0) {
3481 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s.bin",
3482 rom_version, variant);
3484 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s_%04x.bin",
3485 rom_version, variant, board_id);
3488 snprintf(fwname, max_size, "qca/nvm_usb_%08x.bin",
3494 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3495 struct qca_version *ver,
3496 const struct qca_device_info *info)
3498 const struct firmware *fw;
3502 btusb_generate_qca_nvm_name(fwname, sizeof(fwname), ver);
3504 err = request_firmware(&fw, fwname, &hdev->dev);
3506 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3511 bt_dev_info(hdev, "using NVM file: %s", fwname);
3513 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3515 release_firmware(fw);
3520 /* identify the ROM version and check whether patches are needed */
3521 static bool btusb_qca_need_patch(struct usb_device *udev)
3523 struct qca_version ver;
3525 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3528 /* only low ROM versions need patches */
3529 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3532 static int btusb_setup_qca(struct hci_dev *hdev)
3534 struct btusb_data *btdata = hci_get_drvdata(hdev);
3535 struct usb_device *udev = btdata->udev;
3536 const struct qca_device_info *info = NULL;
3537 struct qca_version ver;
3542 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3547 ver_rom = le32_to_cpu(ver.rom_version);
3549 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
3550 if (ver_rom == qca_devices_table[i].rom_version)
3551 info = &qca_devices_table[i];
3554 /* If the rom_version is not matched in the qca_devices_table
3555 * and the high ROM version is not zero, we assume this chip no
3556 * need to load the rampatch and nvm.
3558 if (ver_rom & ~0xffffU)
3561 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3565 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3570 if (!(status & QCA_PATCH_UPDATED)) {
3571 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3576 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3581 if (!(status & QCA_SYSCFG_UPDATED)) {
3582 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3586 /* WCN6855 2.1 and later will reset to apply firmware downloaded here, so
3587 * wait ~100ms for reset Done then go ahead, otherwise, it maybe
3588 * cause potential enable failure.
3590 if (info->rom_version >= 0x00130201)
3591 msleep(QCA_BT_RESET_WAIT_MS);
3594 /* Mark HCI_OP_ENHANCED_SETUP_SYNC_CONN as broken as it doesn't seem to
3595 * work with the likes of HSP/HFP mSBC.
3597 set_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &hdev->quirks);
3602 static inline int __set_diag_interface(struct hci_dev *hdev)
3604 struct btusb_data *data = hci_get_drvdata(hdev);
3605 struct usb_interface *intf = data->diag;
3611 data->diag_tx_ep = NULL;
3612 data->diag_rx_ep = NULL;
3614 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3615 struct usb_endpoint_descriptor *ep_desc;
3617 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3619 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3620 data->diag_tx_ep = ep_desc;
3624 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3625 data->diag_rx_ep = ep_desc;
3630 if (!data->diag_tx_ep || !data->diag_rx_ep) {
3631 bt_dev_err(hdev, "invalid diagnostic descriptors");
3638 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3640 struct btusb_data *data = hci_get_drvdata(hdev);
3641 struct sk_buff *skb;
3645 if (!data->diag_tx_ep)
3646 return ERR_PTR(-ENODEV);
3648 urb = usb_alloc_urb(0, GFP_KERNEL);
3650 return ERR_PTR(-ENOMEM);
3652 skb = bt_skb_alloc(2, GFP_KERNEL);
3655 return ERR_PTR(-ENOMEM);
3658 skb_put_u8(skb, 0xf0);
3659 skb_put_u8(skb, enable);
3661 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3663 usb_fill_bulk_urb(urb, data->udev, pipe,
3664 skb->data, skb->len, btusb_tx_complete, skb);
3666 skb->dev = (void *)hdev;
3671 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3673 struct btusb_data *data = hci_get_drvdata(hdev);
3679 if (!test_bit(HCI_RUNNING, &hdev->flags))
3682 urb = alloc_diag_urb(hdev, enable);
3684 return PTR_ERR(urb);
3686 return submit_or_queue_tx_urb(hdev, urb);
3690 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3692 struct btusb_data *data = priv;
3694 pm_wakeup_event(&data->udev->dev, 0);
3697 /* Disable only if not already disabled (keep it balanced) */
3698 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3699 disable_irq_nosync(irq);
3700 disable_irq_wake(irq);
3705 static const struct of_device_id btusb_match_table[] = {
3706 { .compatible = "usb1286,204e" },
3707 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3708 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
3711 MODULE_DEVICE_TABLE(of, btusb_match_table);
3713 /* Use an oob wakeup pin? */
3714 static int btusb_config_oob_wake(struct hci_dev *hdev)
3716 struct btusb_data *data = hci_get_drvdata(hdev);
3717 struct device *dev = &data->udev->dev;
3720 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3722 if (!of_match_device(btusb_match_table, dev))
3725 /* Move on if no IRQ specified */
3726 irq = of_irq_get_byname(dev->of_node, "wakeup");
3728 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
3732 irq_set_status_flags(irq, IRQ_NOAUTOEN);
3733 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
3734 0, "OOB Wake-on-BT", data);
3736 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
3740 ret = device_init_wakeup(dev, true);
3742 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
3746 data->oob_wake_irq = irq;
3747 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
3752 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
3754 if (dmi_check_system(btusb_needs_reset_resume_table))
3755 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3758 static bool btusb_wakeup(struct hci_dev *hdev)
3760 struct btusb_data *data = hci_get_drvdata(hdev);
3762 return device_may_wakeup(&data->udev->dev);
3765 static int btusb_shutdown_qca(struct hci_dev *hdev)
3767 struct sk_buff *skb;
3769 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3771 bt_dev_err(hdev, "HCI reset during shutdown failed");
3772 return PTR_ERR(skb);
3779 static ssize_t force_poll_sync_read(struct file *file, char __user *user_buf,
3780 size_t count, loff_t *ppos)
3782 struct btusb_data *data = file->private_data;
3785 buf[0] = data->poll_sync ? 'Y' : 'N';
3788 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3791 static ssize_t force_poll_sync_write(struct file *file,
3792 const char __user *user_buf,
3793 size_t count, loff_t *ppos)
3795 struct btusb_data *data = file->private_data;
3799 err = kstrtobool_from_user(user_buf, count, &enable);
3803 /* Only allow changes while the adapter is down */
3804 if (test_bit(HCI_UP, &data->hdev->flags))
3807 if (data->poll_sync == enable)
3810 data->poll_sync = enable;
3815 static const struct file_operations force_poll_sync_fops = {
3816 .open = simple_open,
3817 .read = force_poll_sync_read,
3818 .write = force_poll_sync_write,
3819 .llseek = default_llseek,
3822 static int btusb_probe(struct usb_interface *intf,
3823 const struct usb_device_id *id)
3825 struct usb_endpoint_descriptor *ep_desc;
3826 struct gpio_desc *reset_gpio;
3827 struct btusb_data *data;
3828 struct hci_dev *hdev;
3829 unsigned ifnum_base;
3830 int i, err, priv_size;
3832 BT_DBG("intf %p id %p", intf, id);
3834 /* interface numbers are hardcoded in the spec */
3835 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
3836 if (!(id->driver_info & BTUSB_IFNUM_2))
3838 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
3842 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
3844 if (!id->driver_info) {
3845 const struct usb_device_id *match;
3847 match = usb_match_id(intf, blacklist_table);
3852 if (id->driver_info == BTUSB_IGNORE)
3855 if (id->driver_info & BTUSB_ATH3012) {
3856 struct usb_device *udev = interface_to_usbdev(intf);
3858 /* Old firmware would otherwise let ath3k driver load
3859 * patch and sysconfig files
3861 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
3862 !btusb_qca_need_patch(udev))
3866 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
3870 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3871 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3873 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3874 data->intr_ep = ep_desc;
3878 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3879 data->bulk_tx_ep = ep_desc;
3883 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3884 data->bulk_rx_ep = ep_desc;
3889 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
3892 if (id->driver_info & BTUSB_AMP) {
3893 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3894 data->cmdreq = 0x2b;
3896 data->cmdreq_type = USB_TYPE_CLASS;
3897 data->cmdreq = 0x00;
3900 data->udev = interface_to_usbdev(intf);
3903 INIT_WORK(&data->work, btusb_work);
3904 INIT_WORK(&data->waker, btusb_waker);
3905 INIT_DELAYED_WORK(&data->rx_work, btusb_rx_work);
3907 skb_queue_head_init(&data->acl_q);
3909 init_usb_anchor(&data->deferred);
3910 init_usb_anchor(&data->tx_anchor);
3911 spin_lock_init(&data->txlock);
3913 init_usb_anchor(&data->intr_anchor);
3914 init_usb_anchor(&data->bulk_anchor);
3915 init_usb_anchor(&data->isoc_anchor);
3916 init_usb_anchor(&data->diag_anchor);
3917 init_usb_anchor(&data->ctrl_anchor);
3918 spin_lock_init(&data->rxlock);
3922 data->recv_event = hci_recv_frame;
3923 data->recv_bulk = btusb_recv_bulk;
3925 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3926 /* Allocate extra space for Intel device */
3927 priv_size += sizeof(struct btintel_data);
3929 /* Override the rx handlers */
3930 data->recv_event = btusb_recv_event_intel;
3931 data->recv_bulk = btusb_recv_bulk_intel;
3932 } else if (id->driver_info & BTUSB_REALTEK) {
3933 /* Allocate extra space for Realtek device */
3934 priv_size += sizeof(struct btrealtek_data);
3937 data->recv_acl = hci_recv_frame;
3939 hdev = hci_alloc_dev_priv(priv_size);
3943 hdev->bus = HCI_USB;
3944 hci_set_drvdata(hdev, data);
3946 if (id->driver_info & BTUSB_AMP)
3947 hdev->dev_type = HCI_AMP;
3949 hdev->dev_type = HCI_PRIMARY;
3953 SET_HCIDEV_DEV(hdev, &intf->dev);
3955 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
3957 if (IS_ERR(reset_gpio)) {
3958 err = PTR_ERR(reset_gpio);
3960 } else if (reset_gpio) {
3961 data->reset_gpio = reset_gpio;
3964 hdev->open = btusb_open;
3965 hdev->close = btusb_close;
3966 hdev->flush = btusb_flush;
3967 hdev->send = btusb_send_frame;
3968 hdev->notify = btusb_notify;
3969 hdev->wakeup = btusb_wakeup;
3972 err = btusb_config_oob_wake(hdev);
3976 /* Marvell devices may need a specific chip configuration */
3977 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
3978 err = marvell_config_oob_wake(hdev);
3983 if (id->driver_info & BTUSB_CW6622)
3984 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3986 if (id->driver_info & BTUSB_BCM2045)
3987 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3989 if (id->driver_info & BTUSB_BCM92035)
3990 hdev->setup = btusb_setup_bcm92035;
3992 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3993 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
3994 hdev->manufacturer = 15;
3995 hdev->setup = btbcm_setup_patchram;
3996 hdev->set_diag = btusb_bcm_set_diag;
3997 hdev->set_bdaddr = btbcm_set_bdaddr;
3999 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4000 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4003 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4004 (id->driver_info & BTUSB_BCM_APPLE)) {
4005 hdev->manufacturer = 15;
4006 hdev->setup = btbcm_setup_apple;
4007 hdev->set_diag = btusb_bcm_set_diag;
4009 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4010 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4013 /* Combined Intel Device setup to support multiple setup routine */
4014 if (id->driver_info & BTUSB_INTEL_COMBINED) {
4015 err = btintel_configure_setup(hdev);
4019 /* Transport specific configuration */
4020 hdev->send = btusb_send_frame_intel;
4021 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4023 if (id->driver_info & BTUSB_INTEL_NO_WBS_SUPPORT)
4024 btintel_set_flag(hdev, INTEL_ROM_LEGACY_NO_WBS_SUPPORT);
4026 if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
4027 btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
4029 if (id->driver_info & BTUSB_INTEL_BROKEN_SHUTDOWN_LED)
4030 btintel_set_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED);
4033 if (id->driver_info & BTUSB_MARVELL)
4034 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4036 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4037 (id->driver_info & BTUSB_MEDIATEK)) {
4038 hdev->setup = btusb_mtk_setup;
4039 hdev->shutdown = btusb_mtk_shutdown;
4040 hdev->manufacturer = 70;
4041 hdev->cmd_timeout = btusb_mtk_cmd_timeout;
4042 hdev->set_bdaddr = btmtk_set_bdaddr;
4043 set_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &hdev->quirks);
4044 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
4045 data->recv_acl = btusb_recv_acl_mtk;
4048 if (id->driver_info & BTUSB_SWAVE) {
4049 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
4050 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
4053 if (id->driver_info & BTUSB_INTEL_BOOT) {
4054 hdev->manufacturer = 2;
4055 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4058 if (id->driver_info & BTUSB_ATH3012) {
4059 data->setup_on_usb = btusb_setup_qca;
4060 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4061 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4062 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4065 if (id->driver_info & BTUSB_QCA_ROME) {
4066 data->setup_on_usb = btusb_setup_qca;
4067 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4068 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4069 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4070 btusb_check_needs_reset_resume(intf);
4073 if (id->driver_info & BTUSB_QCA_WCN6855) {
4074 data->setup_on_usb = btusb_setup_qca;
4075 hdev->shutdown = btusb_shutdown_qca;
4076 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
4077 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4078 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4079 hci_set_msft_opcode(hdev, 0xFD70);
4082 if (id->driver_info & BTUSB_AMP) {
4083 /* AMP controllers do not support SCO packets */
4086 /* Interface orders are hardcoded in the specification */
4087 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
4088 data->isoc_ifnum = ifnum_base + 1;
4091 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4092 (id->driver_info & BTUSB_REALTEK)) {
4093 hdev->setup = btusb_setup_realtek;
4094 hdev->shutdown = btrtl_shutdown_realtek;
4095 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
4097 /* Realtek devices need to set remote wakeup on auto-suspend */
4098 set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
4099 set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
4102 if (id->driver_info & BTUSB_ACTIONS_SEMI) {
4103 /* Support is advertised, but not implemented */
4104 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
4108 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4110 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
4111 if (!disable_scofix)
4112 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
4115 if (id->driver_info & BTUSB_BROKEN_ISOC)
4118 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
4119 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
4121 if (id->driver_info & BTUSB_VALID_LE_STATES)
4122 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
4124 if (id->driver_info & BTUSB_DIGIANSWER) {
4125 data->cmdreq_type = USB_TYPE_VENDOR;
4126 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4129 if (id->driver_info & BTUSB_CSR) {
4130 struct usb_device *udev = data->udev;
4131 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
4133 /* Old firmware would otherwise execute USB reset */
4134 if (bcdDevice < 0x117)
4135 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4137 /* This must be set first in case we disable it for fakes */
4138 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4140 /* Fake CSR devices with broken commands */
4141 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
4142 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4143 hdev->setup = btusb_setup_csr;
4146 if (id->driver_info & BTUSB_SNIFFER) {
4147 struct usb_device *udev = data->udev;
4149 /* New sniffer firmware has crippled HCI interface */
4150 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4151 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4154 if (id->driver_info & BTUSB_INTEL_BOOT) {
4155 /* A bug in the bootloader causes that interrupt interface is
4156 * only enabled after receiving SetInterface(0, AltSetting=0).
4158 err = usb_set_interface(data->udev, 0, 0);
4160 BT_ERR("failed to set interface 0, alt 0 %d", err);
4166 err = usb_driver_claim_interface(&btusb_driver,
4172 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
4173 if (!usb_driver_claim_interface(&btusb_driver,
4175 __set_diag_interface(hdev);
4180 if (enable_autosuspend)
4181 usb_enable_autosuspend(data->udev);
4183 data->poll_sync = enable_poll_sync;
4185 err = hci_register_dev(hdev);
4189 usb_set_intfdata(intf, data);
4191 debugfs_create_file("force_poll_sync", 0644, hdev->debugfs, data,
4192 &force_poll_sync_fops);
4197 if (data->reset_gpio)
4198 gpiod_put(data->reset_gpio);
4203 static void btusb_disconnect(struct usb_interface *intf)
4205 struct btusb_data *data = usb_get_intfdata(intf);
4206 struct hci_dev *hdev;
4208 BT_DBG("intf %p", intf);
4214 usb_set_intfdata(data->intf, NULL);
4217 usb_set_intfdata(data->isoc, NULL);
4220 usb_set_intfdata(data->diag, NULL);
4222 hci_unregister_dev(hdev);
4224 if (intf == data->intf) {
4226 usb_driver_release_interface(&btusb_driver, data->isoc);
4228 usb_driver_release_interface(&btusb_driver, data->diag);
4229 } else if (intf == data->isoc) {
4231 usb_driver_release_interface(&btusb_driver, data->diag);
4232 usb_driver_release_interface(&btusb_driver, data->intf);
4233 } else if (intf == data->diag) {
4234 usb_driver_release_interface(&btusb_driver, data->intf);
4236 usb_driver_release_interface(&btusb_driver, data->isoc);
4239 if (data->oob_wake_irq)
4240 device_init_wakeup(&data->udev->dev, false);
4242 if (data->reset_gpio)
4243 gpiod_put(data->reset_gpio);
4249 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4251 struct btusb_data *data = usb_get_intfdata(intf);
4253 BT_DBG("intf %p", intf);
4255 if (data->suspend_count++)
4258 spin_lock_irq(&data->txlock);
4259 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4260 set_bit(BTUSB_SUSPENDING, &data->flags);
4261 spin_unlock_irq(&data->txlock);
4263 spin_unlock_irq(&data->txlock);
4264 data->suspend_count--;
4268 cancel_work_sync(&data->work);
4270 btusb_stop_traffic(data);
4271 usb_kill_anchored_urbs(&data->tx_anchor);
4273 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4274 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4275 enable_irq_wake(data->oob_wake_irq);
4276 enable_irq(data->oob_wake_irq);
4279 /* For global suspend, Realtek devices lose the loaded fw
4280 * in them. But for autosuspend, firmware should remain.
4281 * Actually, it depends on whether the usb host sends
4282 * set feature (enable wakeup) or not.
4284 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
4285 if (PMSG_IS_AUTO(message) &&
4286 device_can_wakeup(&data->udev->dev))
4287 data->udev->do_remote_wakeup = 1;
4288 else if (!PMSG_IS_AUTO(message) &&
4289 !device_may_wakeup(&data->udev->dev)) {
4290 data->udev->do_remote_wakeup = 0;
4291 data->udev->reset_resume = 1;
4298 static void play_deferred(struct btusb_data *data)
4303 while ((urb = usb_get_from_anchor(&data->deferred))) {
4304 usb_anchor_urb(urb, &data->tx_anchor);
4306 err = usb_submit_urb(urb, GFP_ATOMIC);
4308 if (err != -EPERM && err != -ENODEV)
4309 BT_ERR("%s urb %p submission failed (%d)",
4310 data->hdev->name, urb, -err);
4311 kfree(urb->setup_packet);
4312 usb_unanchor_urb(urb);
4317 data->tx_in_flight++;
4321 /* Cleanup the rest deferred urbs. */
4322 while ((urb = usb_get_from_anchor(&data->deferred))) {
4323 kfree(urb->setup_packet);
4328 static int btusb_resume(struct usb_interface *intf)
4330 struct btusb_data *data = usb_get_intfdata(intf);
4331 struct hci_dev *hdev = data->hdev;
4334 BT_DBG("intf %p", intf);
4336 if (--data->suspend_count)
4339 /* Disable only if not already disabled (keep it balanced) */
4340 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4341 disable_irq(data->oob_wake_irq);
4342 disable_irq_wake(data->oob_wake_irq);
4345 if (!test_bit(HCI_RUNNING, &hdev->flags))
4348 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4349 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4351 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4356 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4357 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4359 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4363 btusb_submit_bulk_urb(hdev, GFP_NOIO);
4366 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4367 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4368 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4370 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4373 spin_lock_irq(&data->txlock);
4374 play_deferred(data);
4375 clear_bit(BTUSB_SUSPENDING, &data->flags);
4376 spin_unlock_irq(&data->txlock);
4377 schedule_work(&data->work);
4382 usb_scuttle_anchored_urbs(&data->deferred);
4384 spin_lock_irq(&data->txlock);
4385 clear_bit(BTUSB_SUSPENDING, &data->flags);
4386 spin_unlock_irq(&data->txlock);
4392 static struct usb_driver btusb_driver = {
4394 .probe = btusb_probe,
4395 .disconnect = btusb_disconnect,
4397 .suspend = btusb_suspend,
4398 .resume = btusb_resume,
4400 .id_table = btusb_table,
4401 .supports_autosuspend = 1,
4402 .disable_hub_initiated_lpm = 1,
4405 module_usb_driver(btusb_driver);
4407 module_param(disable_scofix, bool, 0644);
4408 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4410 module_param(force_scofix, bool, 0644);
4411 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4413 module_param(enable_autosuspend, bool, 0644);
4414 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4416 module_param(reset, bool, 0644);
4417 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4419 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4420 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4421 MODULE_VERSION(VERSION);
4422 MODULE_LICENSE("GPL");