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 },
543 { USB_DEVICE(0x0bda, 0x887b), .driver_info = BTUSB_REALTEK |
544 BTUSB_WIDEBAND_SPEECH },
545 { USB_DEVICE(0x13d3, 0x3571), .driver_info = BTUSB_REALTEK |
546 BTUSB_WIDEBAND_SPEECH },
548 /* Realtek Bluetooth devices */
549 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
550 .driver_info = BTUSB_REALTEK },
552 /* MediaTek Bluetooth devices */
553 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
554 .driver_info = BTUSB_MEDIATEK |
555 BTUSB_WIDEBAND_SPEECH |
556 BTUSB_VALID_LE_STATES },
558 /* Additional MediaTek MT7615E Bluetooth devices */
559 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
561 /* Additional MediaTek MT7663 Bluetooth devices */
562 { USB_DEVICE(0x043e, 0x310c), .driver_info = BTUSB_MEDIATEK |
563 BTUSB_WIDEBAND_SPEECH |
564 BTUSB_VALID_LE_STATES },
565 { USB_DEVICE(0x04ca, 0x3801), .driver_info = BTUSB_MEDIATEK |
566 BTUSB_WIDEBAND_SPEECH |
567 BTUSB_VALID_LE_STATES },
569 /* Additional MediaTek MT7668 Bluetooth devices */
570 { USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
571 BTUSB_WIDEBAND_SPEECH |
572 BTUSB_VALID_LE_STATES },
574 /* Additional MediaTek MT7921 Bluetooth devices */
575 { USB_DEVICE(0x0489, 0xe0c8), .driver_info = BTUSB_MEDIATEK |
576 BTUSB_WIDEBAND_SPEECH |
577 BTUSB_VALID_LE_STATES },
578 { USB_DEVICE(0x0489, 0xe0e0), .driver_info = BTUSB_MEDIATEK |
579 BTUSB_WIDEBAND_SPEECH |
580 BTUSB_VALID_LE_STATES },
581 { USB_DEVICE(0x0489, 0xe0f2), .driver_info = BTUSB_MEDIATEK |
582 BTUSB_WIDEBAND_SPEECH |
583 BTUSB_VALID_LE_STATES },
584 { USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
585 BTUSB_WIDEBAND_SPEECH |
586 BTUSB_VALID_LE_STATES },
587 { USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
588 BTUSB_WIDEBAND_SPEECH |
589 BTUSB_VALID_LE_STATES },
590 { USB_DEVICE(0x13d3, 0x3564), .driver_info = BTUSB_MEDIATEK |
591 BTUSB_WIDEBAND_SPEECH |
592 BTUSB_VALID_LE_STATES },
593 { USB_DEVICE(0x13d3, 0x3567), .driver_info = BTUSB_MEDIATEK |
594 BTUSB_WIDEBAND_SPEECH |
595 BTUSB_VALID_LE_STATES },
596 { USB_DEVICE(0x13d3, 0x3578), .driver_info = BTUSB_MEDIATEK |
597 BTUSB_WIDEBAND_SPEECH |
598 BTUSB_VALID_LE_STATES },
599 { USB_DEVICE(0x13d3, 0x3583), .driver_info = BTUSB_MEDIATEK |
600 BTUSB_WIDEBAND_SPEECH |
601 BTUSB_VALID_LE_STATES },
602 { USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
603 BTUSB_WIDEBAND_SPEECH |
604 BTUSB_VALID_LE_STATES },
605 { USB_DEVICE(0x0e8d, 0x0608), .driver_info = BTUSB_MEDIATEK |
606 BTUSB_WIDEBAND_SPEECH |
607 BTUSB_VALID_LE_STATES },
609 /* MediaTek MT7922A Bluetooth devices */
610 { USB_DEVICE(0x0489, 0xe0d8), .driver_info = BTUSB_MEDIATEK |
611 BTUSB_WIDEBAND_SPEECH |
612 BTUSB_VALID_LE_STATES },
613 { USB_DEVICE(0x0489, 0xe0d9), .driver_info = BTUSB_MEDIATEK |
614 BTUSB_WIDEBAND_SPEECH |
615 BTUSB_VALID_LE_STATES },
616 { USB_DEVICE(0x13d3, 0x3568), .driver_info = BTUSB_MEDIATEK |
617 BTUSB_WIDEBAND_SPEECH |
618 BTUSB_VALID_LE_STATES },
619 { USB_DEVICE(0x0489, 0xe0e2), .driver_info = BTUSB_MEDIATEK |
620 BTUSB_WIDEBAND_SPEECH |
621 BTUSB_VALID_LE_STATES },
622 { USB_DEVICE(0x0489, 0xe0e4), .driver_info = BTUSB_MEDIATEK |
623 BTUSB_WIDEBAND_SPEECH |
624 BTUSB_VALID_LE_STATES },
625 { USB_DEVICE(0x0489, 0xe0f2), .driver_info = BTUSB_MEDIATEK |
626 BTUSB_WIDEBAND_SPEECH |
627 BTUSB_VALID_LE_STATES },
629 /* Additional Realtek 8723AE Bluetooth devices */
630 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
631 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
633 /* Additional Realtek 8723BE Bluetooth devices */
634 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
635 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
636 { USB_DEVICE(0x04f2, 0xb49f), .driver_info = BTUSB_REALTEK },
637 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
638 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
639 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
640 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
642 /* Additional Realtek 8723BU Bluetooth devices */
643 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
645 /* Additional Realtek 8723DE Bluetooth devices */
646 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
647 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
649 /* Additional Realtek 8761BUV Bluetooth devices */
650 { USB_DEVICE(0x2357, 0x0604), .driver_info = BTUSB_REALTEK |
651 BTUSB_WIDEBAND_SPEECH },
652 { USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
653 BTUSB_WIDEBAND_SPEECH },
654 { USB_DEVICE(0x2550, 0x8761), .driver_info = BTUSB_REALTEK |
655 BTUSB_WIDEBAND_SPEECH },
656 { USB_DEVICE(0x0bda, 0x8771), .driver_info = BTUSB_REALTEK |
657 BTUSB_WIDEBAND_SPEECH },
658 { USB_DEVICE(0x7392, 0xc611), .driver_info = BTUSB_REALTEK |
659 BTUSB_WIDEBAND_SPEECH },
660 { USB_DEVICE(0x2b89, 0x8761), .driver_info = BTUSB_REALTEK |
661 BTUSB_WIDEBAND_SPEECH },
663 /* Additional Realtek 8821AE Bluetooth devices */
664 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
665 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
666 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
667 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
668 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
670 /* Additional Realtek 8822BE Bluetooth devices */
671 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
672 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
674 /* Additional Realtek 8822CE Bluetooth devices */
675 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
676 BTUSB_WIDEBAND_SPEECH },
677 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
678 BTUSB_WIDEBAND_SPEECH },
679 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
680 BTUSB_WIDEBAND_SPEECH },
681 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
682 BTUSB_WIDEBAND_SPEECH },
683 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
684 BTUSB_WIDEBAND_SPEECH },
685 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
686 BTUSB_WIDEBAND_SPEECH },
687 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
688 BTUSB_WIDEBAND_SPEECH },
689 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
690 BTUSB_WIDEBAND_SPEECH },
691 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
692 BTUSB_WIDEBAND_SPEECH },
693 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
694 BTUSB_WIDEBAND_SPEECH },
695 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
696 BTUSB_WIDEBAND_SPEECH },
698 /* Actions Semiconductor ATS2851 based devices */
699 { USB_DEVICE(0x10d7, 0xb012), .driver_info = BTUSB_ACTIONS_SEMI },
701 /* Silicon Wave based devices */
702 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
704 { } /* Terminating entry */
707 /* The Bluetooth USB module build into some devices needs to be reset on resume,
708 * this is a problem with the platform (likely shutting off all power) not with
709 * the module itself. So we use a DMI list to match known broken platforms.
711 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
713 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
715 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
716 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
720 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
722 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
723 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
727 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
729 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
730 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
736 struct qca_dump_info {
737 /* fields for dump collection */
746 #define BTUSB_MAX_ISOC_FRAMES 10
748 #define BTUSB_INTR_RUNNING 0
749 #define BTUSB_BULK_RUNNING 1
750 #define BTUSB_ISOC_RUNNING 2
751 #define BTUSB_SUSPENDING 3
752 #define BTUSB_DID_ISO_RESUME 4
753 #define BTUSB_BOOTLOADER 5
754 #define BTUSB_DOWNLOADING 6
755 #define BTUSB_FIRMWARE_LOADED 7
756 #define BTUSB_FIRMWARE_FAILED 8
757 #define BTUSB_BOOTING 9
758 #define BTUSB_DIAG_RUNNING 10
759 #define BTUSB_OOB_WAKE_ENABLED 11
760 #define BTUSB_HW_RESET_ACTIVE 12
761 #define BTUSB_TX_WAIT_VND_EVT 13
762 #define BTUSB_WAKEUP_AUTOSUSPEND 14
763 #define BTUSB_USE_ALT3_FOR_WBS 15
764 #define BTUSB_ALT6_CONTINUOUS_TX 16
765 #define BTUSB_HW_SSR_ACTIVE 17
768 struct hci_dev *hdev;
769 struct usb_device *udev;
770 struct usb_interface *intf;
771 struct usb_interface *isoc;
772 struct usb_interface *diag;
779 struct work_struct work;
780 struct work_struct waker;
781 struct delayed_work rx_work;
783 struct sk_buff_head acl_q;
785 struct usb_anchor deferred;
786 struct usb_anchor tx_anchor;
790 struct usb_anchor intr_anchor;
791 struct usb_anchor bulk_anchor;
792 struct usb_anchor isoc_anchor;
793 struct usb_anchor diag_anchor;
794 struct usb_anchor ctrl_anchor;
797 struct sk_buff *evt_skb;
798 struct sk_buff *acl_skb;
799 struct sk_buff *sco_skb;
801 struct usb_endpoint_descriptor *intr_ep;
802 struct usb_endpoint_descriptor *bulk_tx_ep;
803 struct usb_endpoint_descriptor *bulk_rx_ep;
804 struct usb_endpoint_descriptor *isoc_tx_ep;
805 struct usb_endpoint_descriptor *isoc_rx_ep;
806 struct usb_endpoint_descriptor *diag_tx_ep;
807 struct usb_endpoint_descriptor *diag_rx_ep;
809 struct gpio_desc *reset_gpio;
814 unsigned int sco_num;
815 unsigned int air_mode;
816 bool usb_alt6_packet_flow;
820 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
821 int (*recv_acl)(struct hci_dev *hdev, struct sk_buff *skb);
822 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
824 int (*setup_on_usb)(struct hci_dev *hdev);
826 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
827 unsigned cmd_timeout_cnt;
829 struct qca_dump_info qca_dump;
832 static void btusb_reset(struct hci_dev *hdev)
834 struct btusb_data *data;
842 data = hci_get_drvdata(hdev);
843 /* This is not an unbalanced PM reference since the device will reset */
844 err = usb_autopm_get_interface(data->intf);
846 bt_dev_err(hdev, "Failed usb_autopm_get_interface: %d", err);
850 bt_dev_err(hdev, "Resetting usb device.");
851 usb_queue_reset_device(data->intf);
854 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
856 struct btusb_data *data = hci_get_drvdata(hdev);
857 struct gpio_desc *reset_gpio = data->reset_gpio;
859 if (++data->cmd_timeout_cnt < 5)
868 * Toggle the hard reset line if the platform provides one. The reset
869 * is going to yank the device off the USB and then replug. So doing
870 * once is enough. The cleanup is handled correctly on the way out
871 * (standard USB disconnect), and the new device is detected cleanly
872 * and bound to the driver again like it should be.
874 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
875 bt_dev_err(hdev, "last reset failed? Not resetting again");
879 bt_dev_err(hdev, "Initiating HW reset via gpio");
880 gpiod_set_value_cansleep(reset_gpio, 1);
882 gpiod_set_value_cansleep(reset_gpio, 0);
885 static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
887 struct btusb_data *data = hci_get_drvdata(hdev);
888 struct gpio_desc *reset_gpio = data->reset_gpio;
890 if (++data->cmd_timeout_cnt < 5)
898 /* Toggle the hard reset line. The Realtek device is going to
899 * yank itself off the USB and then replug. The cleanup is handled
900 * correctly on the way out (standard USB disconnect), and the new
901 * device is detected cleanly and bound to the driver again like
904 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
905 bt_dev_err(hdev, "last reset failed? Not resetting again");
909 bt_dev_err(hdev, "Reset Realtek device via gpio");
910 gpiod_set_value_cansleep(reset_gpio, 1);
912 gpiod_set_value_cansleep(reset_gpio, 0);
915 static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
917 struct btusb_data *data = hci_get_drvdata(hdev);
918 struct gpio_desc *reset_gpio = data->reset_gpio;
920 if (test_bit(BTUSB_HW_SSR_ACTIVE, &data->flags)) {
921 bt_dev_info(hdev, "Ramdump in progress, defer cmd_timeout");
925 if (++data->cmd_timeout_cnt < 5)
929 bt_dev_err(hdev, "Reset qca device via bt_en gpio");
931 /* Toggle the hard reset line. The qca bt device is going to
932 * yank itself off the USB and then replug. The cleanup is handled
933 * correctly on the way out (standard USB disconnect), and the new
934 * device is detected cleanly and bound to the driver again like
937 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
938 bt_dev_err(hdev, "last reset failed? Not resetting again");
942 gpiod_set_value_cansleep(reset_gpio, 0);
944 gpiod_set_value_cansleep(reset_gpio, 1);
952 static inline void btusb_free_frags(struct btusb_data *data)
956 spin_lock_irqsave(&data->rxlock, flags);
958 dev_kfree_skb_irq(data->evt_skb);
959 data->evt_skb = NULL;
961 dev_kfree_skb_irq(data->acl_skb);
962 data->acl_skb = NULL;
964 dev_kfree_skb_irq(data->sco_skb);
965 data->sco_skb = NULL;
967 spin_unlock_irqrestore(&data->rxlock, flags);
970 static int btusb_recv_event(struct btusb_data *data, struct sk_buff *skb)
972 if (data->intr_interval) {
973 /* Trigger dequeue immediatelly if an event is received */
974 schedule_delayed_work(&data->rx_work, 0);
977 return data->recv_event(data->hdev, skb);
980 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
986 spin_lock_irqsave(&data->rxlock, flags);
993 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
999 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
1000 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
1003 len = min_t(uint, hci_skb_expect(skb), count);
1004 skb_put_data(skb, buffer, len);
1008 hci_skb_expect(skb) -= len;
1010 if (skb->len == HCI_EVENT_HDR_SIZE) {
1011 /* Complete event header */
1012 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
1014 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
1023 if (!hci_skb_expect(skb)) {
1024 /* Complete frame */
1025 btusb_recv_event(data, skb);
1030 data->evt_skb = skb;
1031 spin_unlock_irqrestore(&data->rxlock, flags);
1036 static int btusb_recv_acl(struct btusb_data *data, struct sk_buff *skb)
1038 /* Only queue ACL packet if intr_interval is set as it means
1039 * force_poll_sync has been enabled.
1041 if (!data->intr_interval)
1042 return data->recv_acl(data->hdev, skb);
1044 skb_queue_tail(&data->acl_q, skb);
1045 schedule_delayed_work(&data->rx_work, data->intr_interval);
1050 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
1052 struct sk_buff *skb;
1053 unsigned long flags;
1056 spin_lock_irqsave(&data->rxlock, flags);
1057 skb = data->acl_skb;
1063 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
1069 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
1070 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
1073 len = min_t(uint, hci_skb_expect(skb), count);
1074 skb_put_data(skb, buffer, len);
1078 hci_skb_expect(skb) -= len;
1080 if (skb->len == HCI_ACL_HDR_SIZE) {
1081 __le16 dlen = hci_acl_hdr(skb)->dlen;
1083 /* Complete ACL header */
1084 hci_skb_expect(skb) = __le16_to_cpu(dlen);
1086 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
1095 if (!hci_skb_expect(skb)) {
1096 /* Complete frame */
1097 btusb_recv_acl(data, skb);
1102 data->acl_skb = skb;
1103 spin_unlock_irqrestore(&data->rxlock, flags);
1108 static bool btusb_validate_sco_handle(struct hci_dev *hdev,
1109 struct hci_sco_hdr *hdr)
1113 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
1114 // Can't validate, userspace controls everything.
1118 * USB isochronous transfers are not designed to be reliable and may
1119 * lose fragments. When this happens, the next first fragment
1120 * encountered might actually be a continuation fragment.
1121 * Validate the handle to detect it and drop it, or else the upper
1122 * layer will get garbage for a while.
1125 handle = hci_handle(__le16_to_cpu(hdr->handle));
1127 switch (hci_conn_lookup_type(hdev, handle)) {
1136 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
1138 struct sk_buff *skb;
1139 unsigned long flags;
1142 spin_lock_irqsave(&data->rxlock, flags);
1143 skb = data->sco_skb;
1149 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
1155 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
1156 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
1159 len = min_t(uint, hci_skb_expect(skb), count);
1160 skb_put_data(skb, buffer, len);
1164 hci_skb_expect(skb) -= len;
1166 if (skb->len == HCI_SCO_HDR_SIZE) {
1167 /* Complete SCO header */
1168 struct hci_sco_hdr *hdr = hci_sco_hdr(skb);
1170 hci_skb_expect(skb) = hdr->dlen;
1172 if (skb_tailroom(skb) < hci_skb_expect(skb) ||
1173 !btusb_validate_sco_handle(data->hdev, hdr)) {
1182 if (!hci_skb_expect(skb)) {
1183 /* Complete frame */
1184 hci_recv_frame(data->hdev, skb);
1189 data->sco_skb = skb;
1190 spin_unlock_irqrestore(&data->rxlock, flags);
1195 static void btusb_intr_complete(struct urb *urb)
1197 struct hci_dev *hdev = urb->context;
1198 struct btusb_data *data = hci_get_drvdata(hdev);
1201 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1202 urb->actual_length);
1204 if (!test_bit(HCI_RUNNING, &hdev->flags))
1207 if (urb->status == 0) {
1208 hdev->stat.byte_rx += urb->actual_length;
1210 if (btusb_recv_intr(data, urb->transfer_buffer,
1211 urb->actual_length) < 0) {
1212 bt_dev_err(hdev, "corrupted event packet");
1213 hdev->stat.err_rx++;
1215 } else if (urb->status == -ENOENT) {
1216 /* Avoid suspend failed when usb_kill_urb */
1220 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
1223 usb_mark_last_busy(data->udev);
1224 usb_anchor_urb(urb, &data->intr_anchor);
1226 err = usb_submit_urb(urb, GFP_ATOMIC);
1228 /* -EPERM: urb is being killed;
1229 * -ENODEV: device got disconnected
1231 if (err != -EPERM && err != -ENODEV)
1232 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1235 hci_cmd_sync_cancel(hdev, -err);
1236 usb_unanchor_urb(urb);
1240 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
1242 struct btusb_data *data = hci_get_drvdata(hdev);
1248 BT_DBG("%s", hdev->name);
1253 urb = usb_alloc_urb(0, mem_flags);
1257 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
1259 buf = kmalloc(size, mem_flags);
1265 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
1267 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
1268 btusb_intr_complete, hdev, data->intr_ep->bInterval);
1270 urb->transfer_flags |= URB_FREE_BUFFER;
1272 usb_anchor_urb(urb, &data->intr_anchor);
1274 err = usb_submit_urb(urb, mem_flags);
1276 if (err != -EPERM && err != -ENODEV)
1277 bt_dev_err(hdev, "urb %p submission failed (%d)",
1280 hci_cmd_sync_cancel(hdev, -err);
1281 usb_unanchor_urb(urb);
1284 /* Only initialize intr_interval if URB poll sync is enabled */
1285 if (!data->poll_sync)
1288 /* The units are frames (milliseconds) for full and low speed devices,
1289 * and microframes (1/8 millisecond) for highspeed and SuperSpeed
1292 * This is done once on open/resume so it shouldn't change even if
1293 * force_poll_sync changes.
1295 switch (urb->dev->speed) {
1296 case USB_SPEED_SUPER_PLUS:
1297 case USB_SPEED_SUPER: /* units are 125us */
1298 data->intr_interval = usecs_to_jiffies(urb->interval * 125);
1301 data->intr_interval = msecs_to_jiffies(urb->interval);
1311 static void btusb_bulk_complete(struct urb *urb)
1313 struct hci_dev *hdev = urb->context;
1314 struct btusb_data *data = hci_get_drvdata(hdev);
1317 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1318 urb->actual_length);
1320 if (!test_bit(HCI_RUNNING, &hdev->flags))
1323 if (urb->status == 0) {
1324 hdev->stat.byte_rx += urb->actual_length;
1326 if (data->recv_bulk(data, urb->transfer_buffer,
1327 urb->actual_length) < 0) {
1328 bt_dev_err(hdev, "corrupted ACL packet");
1329 hdev->stat.err_rx++;
1331 } else if (urb->status == -ENOENT) {
1332 /* Avoid suspend failed when usb_kill_urb */
1336 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
1339 usb_anchor_urb(urb, &data->bulk_anchor);
1340 usb_mark_last_busy(data->udev);
1342 err = usb_submit_urb(urb, GFP_ATOMIC);
1344 /* -EPERM: urb is being killed;
1345 * -ENODEV: device got disconnected
1347 if (err != -EPERM && err != -ENODEV)
1348 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1350 usb_unanchor_urb(urb);
1354 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
1356 struct btusb_data *data = hci_get_drvdata(hdev);
1360 int err, size = HCI_MAX_FRAME_SIZE;
1362 BT_DBG("%s", hdev->name);
1364 if (!data->bulk_rx_ep)
1367 urb = usb_alloc_urb(0, mem_flags);
1371 buf = kmalloc(size, mem_flags);
1377 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1379 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1380 btusb_bulk_complete, hdev);
1382 urb->transfer_flags |= URB_FREE_BUFFER;
1384 usb_mark_last_busy(data->udev);
1385 usb_anchor_urb(urb, &data->bulk_anchor);
1387 err = usb_submit_urb(urb, mem_flags);
1389 if (err != -EPERM && err != -ENODEV)
1390 bt_dev_err(hdev, "urb %p submission failed (%d)",
1392 usb_unanchor_urb(urb);
1400 static void btusb_isoc_complete(struct urb *urb)
1402 struct hci_dev *hdev = urb->context;
1403 struct btusb_data *data = hci_get_drvdata(hdev);
1406 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1407 urb->actual_length);
1409 if (!test_bit(HCI_RUNNING, &hdev->flags))
1412 if (urb->status == 0) {
1413 for (i = 0; i < urb->number_of_packets; i++) {
1414 unsigned int offset = urb->iso_frame_desc[i].offset;
1415 unsigned int length = urb->iso_frame_desc[i].actual_length;
1417 if (urb->iso_frame_desc[i].status)
1420 hdev->stat.byte_rx += length;
1422 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1424 bt_dev_err(hdev, "corrupted SCO packet");
1425 hdev->stat.err_rx++;
1428 } else if (urb->status == -ENOENT) {
1429 /* Avoid suspend failed when usb_kill_urb */
1433 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1436 usb_anchor_urb(urb, &data->isoc_anchor);
1438 err = usb_submit_urb(urb, GFP_ATOMIC);
1440 /* -EPERM: urb is being killed;
1441 * -ENODEV: device got disconnected
1443 if (err != -EPERM && err != -ENODEV)
1444 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1446 usb_unanchor_urb(urb);
1450 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1451 int mtu, struct btusb_data *data)
1453 int i = 0, offset = 0;
1454 unsigned int interval;
1456 BT_DBG("len %d mtu %d", len, mtu);
1458 /* For mSBC ALT 6 settings some chips need to transmit the data
1459 * continuously without the zero length of USB packets.
1461 if (test_bit(BTUSB_ALT6_CONTINUOUS_TX, &data->flags))
1462 goto ignore_usb_alt6_packet_flow;
1464 /* For mSBC ALT 6 setting the host will send the packet at continuous
1465 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1466 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1467 * To maintain the rate we send 63bytes of usb packets alternatively for
1468 * 7ms and 8ms to maintain the rate as 7.5ms.
1470 if (data->usb_alt6_packet_flow) {
1472 data->usb_alt6_packet_flow = false;
1475 data->usb_alt6_packet_flow = true;
1478 for (i = 0; i < interval; i++) {
1479 urb->iso_frame_desc[i].offset = offset;
1480 urb->iso_frame_desc[i].length = offset;
1483 ignore_usb_alt6_packet_flow:
1484 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1485 urb->iso_frame_desc[i].offset = offset;
1486 urb->iso_frame_desc[i].length = len;
1490 urb->number_of_packets = i;
1493 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1497 BT_DBG("len %d mtu %d", len, mtu);
1499 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1500 i++, offset += mtu, len -= mtu) {
1501 urb->iso_frame_desc[i].offset = offset;
1502 urb->iso_frame_desc[i].length = mtu;
1505 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1506 urb->iso_frame_desc[i].offset = offset;
1507 urb->iso_frame_desc[i].length = len;
1511 urb->number_of_packets = i;
1514 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1516 struct btusb_data *data = hci_get_drvdata(hdev);
1522 BT_DBG("%s", hdev->name);
1524 if (!data->isoc_rx_ep)
1527 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1531 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1532 BTUSB_MAX_ISOC_FRAMES;
1534 buf = kmalloc(size, mem_flags);
1540 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1542 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1543 hdev, data->isoc_rx_ep->bInterval);
1545 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1547 __fill_isoc_descriptor(urb, size,
1548 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1550 usb_anchor_urb(urb, &data->isoc_anchor);
1552 err = usb_submit_urb(urb, mem_flags);
1554 if (err != -EPERM && err != -ENODEV)
1555 bt_dev_err(hdev, "urb %p submission failed (%d)",
1557 usb_unanchor_urb(urb);
1565 static void btusb_diag_complete(struct urb *urb)
1567 struct hci_dev *hdev = urb->context;
1568 struct btusb_data *data = hci_get_drvdata(hdev);
1571 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1572 urb->actual_length);
1574 if (urb->status == 0) {
1575 struct sk_buff *skb;
1577 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1579 skb_put_data(skb, urb->transfer_buffer,
1580 urb->actual_length);
1581 hci_recv_diag(hdev, skb);
1583 } else if (urb->status == -ENOENT) {
1584 /* Avoid suspend failed when usb_kill_urb */
1588 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1591 usb_anchor_urb(urb, &data->diag_anchor);
1592 usb_mark_last_busy(data->udev);
1594 err = usb_submit_urb(urb, GFP_ATOMIC);
1596 /* -EPERM: urb is being killed;
1597 * -ENODEV: device got disconnected
1599 if (err != -EPERM && err != -ENODEV)
1600 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1602 usb_unanchor_urb(urb);
1606 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1608 struct btusb_data *data = hci_get_drvdata(hdev);
1612 int err, size = HCI_MAX_FRAME_SIZE;
1614 BT_DBG("%s", hdev->name);
1616 if (!data->diag_rx_ep)
1619 urb = usb_alloc_urb(0, mem_flags);
1623 buf = kmalloc(size, mem_flags);
1629 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1631 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1632 btusb_diag_complete, hdev);
1634 urb->transfer_flags |= URB_FREE_BUFFER;
1636 usb_mark_last_busy(data->udev);
1637 usb_anchor_urb(urb, &data->diag_anchor);
1639 err = usb_submit_urb(urb, mem_flags);
1641 if (err != -EPERM && err != -ENODEV)
1642 bt_dev_err(hdev, "urb %p submission failed (%d)",
1644 usb_unanchor_urb(urb);
1652 static void btusb_tx_complete(struct urb *urb)
1654 struct sk_buff *skb = urb->context;
1655 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1656 struct btusb_data *data = hci_get_drvdata(hdev);
1657 unsigned long flags;
1659 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1660 urb->actual_length);
1662 if (!test_bit(HCI_RUNNING, &hdev->flags))
1666 hdev->stat.byte_tx += urb->transfer_buffer_length;
1668 if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)
1669 hci_cmd_sync_cancel(hdev, -urb->status);
1670 hdev->stat.err_tx++;
1674 spin_lock_irqsave(&data->txlock, flags);
1675 data->tx_in_flight--;
1676 spin_unlock_irqrestore(&data->txlock, flags);
1678 kfree(urb->setup_packet);
1683 static void btusb_isoc_tx_complete(struct urb *urb)
1685 struct sk_buff *skb = urb->context;
1686 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1688 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1689 urb->actual_length);
1691 if (!test_bit(HCI_RUNNING, &hdev->flags))
1695 hdev->stat.byte_tx += urb->transfer_buffer_length;
1697 hdev->stat.err_tx++;
1700 kfree(urb->setup_packet);
1705 static int btusb_open(struct hci_dev *hdev)
1707 struct btusb_data *data = hci_get_drvdata(hdev);
1710 BT_DBG("%s", hdev->name);
1712 err = usb_autopm_get_interface(data->intf);
1716 /* Patching USB firmware files prior to starting any URBs of HCI path
1717 * It is more safe to use USB bulk channel for downloading USB patch
1719 if (data->setup_on_usb) {
1720 err = data->setup_on_usb(hdev);
1725 data->intf->needs_remote_wakeup = 1;
1727 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1730 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1734 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1736 usb_kill_anchored_urbs(&data->intr_anchor);
1740 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1741 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1744 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1745 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1749 usb_autopm_put_interface(data->intf);
1753 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1755 usb_autopm_put_interface(data->intf);
1759 static void btusb_stop_traffic(struct btusb_data *data)
1761 usb_kill_anchored_urbs(&data->intr_anchor);
1762 usb_kill_anchored_urbs(&data->bulk_anchor);
1763 usb_kill_anchored_urbs(&data->isoc_anchor);
1764 usb_kill_anchored_urbs(&data->diag_anchor);
1765 usb_kill_anchored_urbs(&data->ctrl_anchor);
1768 static int btusb_close(struct hci_dev *hdev)
1770 struct btusb_data *data = hci_get_drvdata(hdev);
1773 BT_DBG("%s", hdev->name);
1775 cancel_delayed_work(&data->rx_work);
1776 cancel_work_sync(&data->work);
1777 cancel_work_sync(&data->waker);
1779 skb_queue_purge(&data->acl_q);
1781 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1782 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1783 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1784 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1786 btusb_stop_traffic(data);
1787 btusb_free_frags(data);
1789 err = usb_autopm_get_interface(data->intf);
1793 data->intf->needs_remote_wakeup = 0;
1795 /* Enable remote wake up for auto-suspend */
1796 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags))
1797 data->intf->needs_remote_wakeup = 1;
1799 usb_autopm_put_interface(data->intf);
1802 usb_scuttle_anchored_urbs(&data->deferred);
1806 static int btusb_flush(struct hci_dev *hdev)
1808 struct btusb_data *data = hci_get_drvdata(hdev);
1810 BT_DBG("%s", hdev->name);
1812 cancel_delayed_work(&data->rx_work);
1814 skb_queue_purge(&data->acl_q);
1816 usb_kill_anchored_urbs(&data->tx_anchor);
1817 btusb_free_frags(data);
1822 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1824 struct btusb_data *data = hci_get_drvdata(hdev);
1825 struct usb_ctrlrequest *dr;
1829 urb = usb_alloc_urb(0, GFP_KERNEL);
1831 return ERR_PTR(-ENOMEM);
1833 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1836 return ERR_PTR(-ENOMEM);
1839 dr->bRequestType = data->cmdreq_type;
1840 dr->bRequest = data->cmdreq;
1843 dr->wLength = __cpu_to_le16(skb->len);
1845 pipe = usb_sndctrlpipe(data->udev, 0x00);
1847 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1848 skb->data, skb->len, btusb_tx_complete, skb);
1850 skb->dev = (void *)hdev;
1855 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1857 struct btusb_data *data = hci_get_drvdata(hdev);
1861 if (!data->bulk_tx_ep)
1862 return ERR_PTR(-ENODEV);
1864 urb = usb_alloc_urb(0, GFP_KERNEL);
1866 return ERR_PTR(-ENOMEM);
1868 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1870 usb_fill_bulk_urb(urb, data->udev, pipe,
1871 skb->data, skb->len, btusb_tx_complete, skb);
1873 skb->dev = (void *)hdev;
1878 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1880 struct btusb_data *data = hci_get_drvdata(hdev);
1884 if (!data->isoc_tx_ep)
1885 return ERR_PTR(-ENODEV);
1887 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1889 return ERR_PTR(-ENOMEM);
1891 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1893 usb_fill_int_urb(urb, data->udev, pipe,
1894 skb->data, skb->len, btusb_isoc_tx_complete,
1895 skb, data->isoc_tx_ep->bInterval);
1897 urb->transfer_flags = URB_ISO_ASAP;
1899 if (data->isoc_altsetting == 6)
1900 __fill_isoc_descriptor_msbc(urb, skb->len,
1901 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1904 __fill_isoc_descriptor(urb, skb->len,
1905 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1906 skb->dev = (void *)hdev;
1911 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1913 struct btusb_data *data = hci_get_drvdata(hdev);
1916 usb_anchor_urb(urb, &data->tx_anchor);
1918 err = usb_submit_urb(urb, GFP_KERNEL);
1920 if (err != -EPERM && err != -ENODEV)
1921 bt_dev_err(hdev, "urb %p submission failed (%d)",
1923 kfree(urb->setup_packet);
1924 usb_unanchor_urb(urb);
1926 usb_mark_last_busy(data->udev);
1933 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1935 struct btusb_data *data = hci_get_drvdata(hdev);
1936 unsigned long flags;
1939 spin_lock_irqsave(&data->txlock, flags);
1940 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1942 data->tx_in_flight++;
1943 spin_unlock_irqrestore(&data->txlock, flags);
1946 return submit_tx_urb(hdev, urb);
1948 usb_anchor_urb(urb, &data->deferred);
1949 schedule_work(&data->waker);
1955 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1959 BT_DBG("%s", hdev->name);
1961 switch (hci_skb_pkt_type(skb)) {
1962 case HCI_COMMAND_PKT:
1963 urb = alloc_ctrl_urb(hdev, skb);
1965 return PTR_ERR(urb);
1967 hdev->stat.cmd_tx++;
1968 return submit_or_queue_tx_urb(hdev, urb);
1970 case HCI_ACLDATA_PKT:
1971 urb = alloc_bulk_urb(hdev, skb);
1973 return PTR_ERR(urb);
1975 hdev->stat.acl_tx++;
1976 return submit_or_queue_tx_urb(hdev, urb);
1978 case HCI_SCODATA_PKT:
1979 if (hci_conn_num(hdev, SCO_LINK) < 1)
1982 urb = alloc_isoc_urb(hdev, skb);
1984 return PTR_ERR(urb);
1986 hdev->stat.sco_tx++;
1987 return submit_tx_urb(hdev, urb);
1989 case HCI_ISODATA_PKT:
1990 urb = alloc_bulk_urb(hdev, skb);
1992 return PTR_ERR(urb);
1994 return submit_or_queue_tx_urb(hdev, urb);
2000 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
2002 struct btusb_data *data = hci_get_drvdata(hdev);
2004 BT_DBG("%s evt %d", hdev->name, evt);
2006 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
2007 data->sco_num = hci_conn_num(hdev, SCO_LINK);
2008 data->air_mode = evt;
2009 schedule_work(&data->work);
2013 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
2015 struct btusb_data *data = hci_get_drvdata(hdev);
2016 struct usb_interface *intf = data->isoc;
2017 struct usb_endpoint_descriptor *ep_desc;
2023 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
2025 bt_dev_err(hdev, "setting interface failed (%d)", -err);
2029 data->isoc_altsetting = altsetting;
2031 data->isoc_tx_ep = NULL;
2032 data->isoc_rx_ep = NULL;
2034 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2035 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2037 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
2038 data->isoc_tx_ep = ep_desc;
2042 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
2043 data->isoc_rx_ep = ep_desc;
2048 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
2049 bt_dev_err(hdev, "invalid SCO descriptors");
2056 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
2058 struct btusb_data *data = hci_get_drvdata(hdev);
2061 if (data->isoc_altsetting != new_alts) {
2062 unsigned long flags;
2064 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2065 usb_kill_anchored_urbs(&data->isoc_anchor);
2067 /* When isochronous alternate setting needs to be
2068 * changed, because SCO connection has been added
2069 * or removed, a packet fragment may be left in the
2070 * reassembling state. This could lead to wrongly
2071 * assembled fragments.
2073 * Clear outstanding fragment when selecting a new
2074 * alternate setting.
2076 spin_lock_irqsave(&data->rxlock, flags);
2077 kfree_skb(data->sco_skb);
2078 data->sco_skb = NULL;
2079 spin_unlock_irqrestore(&data->rxlock, flags);
2081 err = __set_isoc_interface(hdev, new_alts);
2086 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2087 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
2088 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2090 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
2096 static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
2099 struct usb_interface *intf = data->isoc;
2102 BT_DBG("Looking for Alt no :%d", alt);
2107 for (i = 0; i < intf->num_altsetting; i++) {
2108 if (intf->altsetting[i].desc.bAlternateSetting == alt)
2109 return &intf->altsetting[i];
2115 static void btusb_work(struct work_struct *work)
2117 struct btusb_data *data = container_of(work, struct btusb_data, work);
2118 struct hci_dev *hdev = data->hdev;
2122 if (data->sco_num > 0) {
2123 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
2124 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
2126 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2127 usb_kill_anchored_urbs(&data->isoc_anchor);
2131 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
2134 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
2135 if (hdev->voice_setting & 0x0020) {
2136 static const int alts[3] = { 2, 4, 5 };
2138 new_alts = alts[data->sco_num - 1];
2140 new_alts = data->sco_num;
2142 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
2143 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
2144 * many adapters do not support it. Alt 1 appears to
2145 * work for all adapters that do not have alt 6, and
2146 * which work with WBS at all. Some devices prefer
2147 * alt 3 (HCI payload >= 60 Bytes let air packet
2148 * data satisfy 60 bytes), requiring
2149 * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
2150 * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
2152 if (btusb_find_altsetting(data, 6))
2154 else if (btusb_find_altsetting(data, 3) &&
2155 hdev->sco_mtu >= 72 &&
2156 test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
2162 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
2163 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
2165 usb_kill_anchored_urbs(&data->isoc_anchor);
2167 if (test_and_clear_bit(BTUSB_ISOC_RUNNING, &data->flags))
2168 __set_isoc_interface(hdev, 0);
2170 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
2171 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
2175 static void btusb_waker(struct work_struct *work)
2177 struct btusb_data *data = container_of(work, struct btusb_data, waker);
2180 err = usb_autopm_get_interface(data->intf);
2184 usb_autopm_put_interface(data->intf);
2187 static void btusb_rx_work(struct work_struct *work)
2189 struct btusb_data *data = container_of(work, struct btusb_data,
2191 struct sk_buff *skb;
2193 /* Dequeue ACL data received during the interval */
2194 while ((skb = skb_dequeue(&data->acl_q)))
2195 data->recv_acl(data->hdev, skb);
2198 static int btusb_setup_bcm92035(struct hci_dev *hdev)
2200 struct sk_buff *skb;
2203 BT_DBG("%s", hdev->name);
2205 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
2207 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
2214 static int btusb_setup_csr(struct hci_dev *hdev)
2216 struct btusb_data *data = hci_get_drvdata(hdev);
2217 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
2218 struct hci_rp_read_local_version *rp;
2219 struct sk_buff *skb;
2220 bool is_fake = false;
2223 BT_DBG("%s", hdev->name);
2225 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2228 int err = PTR_ERR(skb);
2229 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
2233 rp = skb_pull_data(skb, sizeof(*rp));
2235 bt_dev_err(hdev, "CSR: Local version length mismatch");
2240 bt_dev_info(hdev, "CSR: Setting up dongle with HCI ver=%u rev=%04x",
2241 rp->hci_ver, le16_to_cpu(rp->hci_rev));
2243 bt_dev_info(hdev, "LMP ver=%u subver=%04x; manufacturer=%u",
2244 rp->lmp_ver, le16_to_cpu(rp->lmp_subver),
2245 le16_to_cpu(rp->manufacturer));
2247 /* Detect a wide host of Chinese controllers that aren't CSR.
2249 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
2251 * The main thing they have in common is that these are really popular low-cost
2252 * options that support newer Bluetooth versions but rely on heavy VID/PID
2253 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
2255 * We detect actual CSR devices by checking that the HCI manufacturer code
2256 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
2257 * HCI rev values always match. As they both store the firmware number.
2259 if (le16_to_cpu(rp->manufacturer) != 10 ||
2260 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
2263 /* Known legit CSR firmware build numbers and their supported BT versions:
2264 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
2265 * - 1.2 (0x2) -> 0x04d9, 0x0529
2266 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
2267 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
2268 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
2270 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
2271 * support BT 1.1 only; so it's a dead giveaway when some
2272 * third-party BT 4.0 dongle reuses it.
2274 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
2275 rp->hci_ver > BLUETOOTH_VER_1_1)
2278 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
2279 rp->hci_ver > BLUETOOTH_VER_1_2)
2282 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
2283 rp->hci_ver > BLUETOOTH_VER_2_0)
2286 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
2287 rp->hci_ver > BLUETOOTH_VER_2_1)
2290 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
2291 rp->hci_ver > BLUETOOTH_VER_4_0)
2294 /* Other clones which beat all the above checks */
2295 else if (bcdDevice == 0x0134 &&
2296 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
2297 rp->hci_ver == BLUETOOTH_VER_2_0)
2301 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
2303 /* Generally these clones have big discrepancies between
2304 * advertised features and what's actually supported.
2305 * Probably will need to be expanded in the future;
2306 * without these the controller will lock up.
2308 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2309 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
2310 set_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks);
2311 set_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks);
2313 /* Clear the reset quirk since this is not an actual
2314 * early Bluetooth 1.1 device from CSR.
2316 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2317 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2320 * Special workaround for these BT 4.0 chip clones, and potentially more:
2322 * - 0x0134: a Barrot 8041a02 (HCI rev: 0x0810 sub: 0x1012)
2323 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
2325 * These controllers are really messed-up.
2327 * 1. Their bulk RX endpoint will never report any data unless
2328 * the device was suspended at least once (yes, really).
2329 * 2. They will not wakeup when autosuspended and receiving data
2330 * on their bulk RX endpoint from e.g. a keyboard or mouse
2331 * (IOW remote-wakeup support is broken for the bulk endpoint).
2333 * To fix 1. enable runtime-suspend, force-suspend the
2334 * HCI and then wake-it up by disabling runtime-suspend.
2336 * To fix 2. clear the HCI's can_wake flag, this way the HCI
2337 * will still be autosuspended when it is not open.
2341 * Because these are widespread problems we prefer generic solutions; so
2342 * apply this initialization quirk to every controller that gets here,
2343 * it should be harmless. The alternative is to not work at all.
2345 pm_runtime_allow(&data->udev->dev);
2347 ret = pm_runtime_suspend(&data->udev->dev);
2351 bt_dev_warn(hdev, "CSR: Couldn't suspend the device for our Barrot 8041a02 receive-issue workaround");
2353 pm_runtime_forbid(&data->udev->dev);
2355 device_set_wakeup_capable(&data->udev->dev, false);
2357 /* Re-enable autosuspend if this was requested */
2358 if (enable_autosuspend)
2359 usb_enable_autosuspend(data->udev);
2367 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2369 struct sk_buff *skb;
2370 struct hci_event_hdr *hdr;
2371 struct hci_ev_cmd_complete *evt;
2373 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2377 hdr = skb_put(skb, sizeof(*hdr));
2378 hdr->evt = HCI_EV_CMD_COMPLETE;
2379 hdr->plen = sizeof(*evt) + 1;
2381 evt = skb_put(skb, sizeof(*evt));
2383 evt->opcode = cpu_to_le16(opcode);
2385 skb_put_u8(skb, 0x00);
2387 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2389 return hci_recv_frame(hdev, skb);
2392 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2395 struct hci_dev *hdev = data->hdev;
2397 /* When the device is in bootloader mode, then it can send
2398 * events via the bulk endpoint. These events are treated the
2399 * same way as the ones received from the interrupt endpoint.
2401 if (btintel_test_flag(hdev, INTEL_BOOTLOADER))
2402 return btusb_recv_intr(data, buffer, count);
2404 return btusb_recv_bulk(data, buffer, count);
2407 static int btusb_intel_diagnostics(struct hci_dev *hdev, struct sk_buff *skb)
2409 struct intel_tlv *tlv = (void *)&skb->data[5];
2411 /* The first event is always an event type TLV */
2412 if (tlv->type != INTEL_TLV_TYPE_ID)
2415 switch (tlv->val[0]) {
2416 case INTEL_TLV_SYSTEM_EXCEPTION:
2417 case INTEL_TLV_FATAL_EXCEPTION:
2418 case INTEL_TLV_DEBUG_EXCEPTION:
2419 case INTEL_TLV_TEST_EXCEPTION:
2420 /* Generate devcoredump from exception */
2421 if (!hci_devcd_init(hdev, skb->len)) {
2422 hci_devcd_append(hdev, skb);
2423 hci_devcd_complete(hdev);
2425 bt_dev_err(hdev, "Failed to generate devcoredump");
2430 bt_dev_err(hdev, "Invalid exception type %02X", tlv->val[0]);
2434 return hci_recv_frame(hdev, skb);
2437 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2439 struct hci_event_hdr *hdr = (void *)skb->data;
2440 const char diagnostics_hdr[] = { 0x87, 0x80, 0x03 };
2442 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2444 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2445 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
2447 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2448 switch (skb->data[2]) {
2450 /* When switching to the operational firmware
2451 * the device sends a vendor specific event
2452 * indicating that the bootup completed.
2454 btintel_bootup(hdev, ptr, len);
2457 /* When the firmware loading completes the
2458 * device sends out a vendor specific event
2459 * indicating the result of the firmware
2462 btintel_secure_send_result(hdev, ptr, len);
2467 /* Handle all diagnostics events separately. May still call
2470 if (len >= sizeof(diagnostics_hdr) &&
2471 memcmp(&skb->data[2], diagnostics_hdr,
2472 sizeof(diagnostics_hdr)) == 0) {
2473 return btusb_intel_diagnostics(hdev, skb);
2477 return hci_recv_frame(hdev, skb);
2480 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2484 BT_DBG("%s", hdev->name);
2486 switch (hci_skb_pkt_type(skb)) {
2487 case HCI_COMMAND_PKT:
2488 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2489 struct hci_command_hdr *cmd = (void *)skb->data;
2490 __u16 opcode = le16_to_cpu(cmd->opcode);
2492 /* When in bootloader mode and the command 0xfc09
2493 * is received, it needs to be send down the
2494 * bulk endpoint. So allocate a bulk URB instead.
2496 if (opcode == 0xfc09)
2497 urb = alloc_bulk_urb(hdev, skb);
2499 urb = alloc_ctrl_urb(hdev, skb);
2501 /* When the 0xfc01 command is issued to boot into
2502 * the operational firmware, it will actually not
2503 * send a command complete event. To keep the flow
2504 * control working inject that event here.
2506 if (opcode == 0xfc01)
2507 inject_cmd_complete(hdev, opcode);
2509 urb = alloc_ctrl_urb(hdev, skb);
2512 return PTR_ERR(urb);
2514 hdev->stat.cmd_tx++;
2515 return submit_or_queue_tx_urb(hdev, urb);
2517 case HCI_ACLDATA_PKT:
2518 urb = alloc_bulk_urb(hdev, skb);
2520 return PTR_ERR(urb);
2522 hdev->stat.acl_tx++;
2523 return submit_or_queue_tx_urb(hdev, urb);
2525 case HCI_SCODATA_PKT:
2526 if (hci_conn_num(hdev, SCO_LINK) < 1)
2529 urb = alloc_isoc_urb(hdev, skb);
2531 return PTR_ERR(urb);
2533 hdev->stat.sco_tx++;
2534 return submit_tx_urb(hdev, urb);
2536 case HCI_ISODATA_PKT:
2537 urb = alloc_bulk_urb(hdev, skb);
2539 return PTR_ERR(urb);
2541 return submit_or_queue_tx_urb(hdev, urb);
2547 static int btusb_setup_realtek(struct hci_dev *hdev)
2549 struct btusb_data *data = hci_get_drvdata(hdev);
2552 ret = btrtl_setup_realtek(hdev);
2554 if (btrealtek_test_flag(data->hdev, REALTEK_ALT6_CONTINUOUS_TX_CHIP))
2555 set_bit(BTUSB_ALT6_CONTINUOUS_TX, &data->flags);
2560 /* UHW CR mapping */
2561 #define MTK_BT_MISC 0x70002510
2562 #define MTK_BT_SUBSYS_RST 0x70002610
2563 #define MTK_UDMA_INT_STA_BT 0x74000024
2564 #define MTK_UDMA_INT_STA_BT1 0x74000308
2565 #define MTK_BT_WDT_STATUS 0x740003A0
2566 #define MTK_EP_RST_OPT 0x74011890
2567 #define MTK_EP_RST_IN_OUT_OPT 0x00010001
2568 #define MTK_BT_RST_DONE 0x00000100
2569 #define MTK_BT_RESET_WAIT_MS 100
2570 #define MTK_BT_RESET_NUM_TRIES 10
2572 static void btusb_mtk_wmt_recv(struct urb *urb)
2574 struct hci_dev *hdev = urb->context;
2575 struct btusb_data *data = hci_get_drvdata(hdev);
2576 struct sk_buff *skb;
2579 if (urb->status == 0 && urb->actual_length > 0) {
2580 hdev->stat.byte_rx += urb->actual_length;
2582 /* WMT event shouldn't be fragmented and the size should be
2583 * less than HCI_WMT_MAX_EVENT_SIZE.
2585 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2587 hdev->stat.err_rx++;
2588 kfree(urb->setup_packet);
2592 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2593 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2595 /* When someone waits for the WMT event, the skb is being cloned
2596 * and being processed the events from there then.
2598 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
2599 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
2600 if (!data->evt_skb) {
2602 kfree(urb->setup_packet);
2607 err = hci_recv_frame(hdev, skb);
2609 kfree_skb(data->evt_skb);
2610 data->evt_skb = NULL;
2611 kfree(urb->setup_packet);
2615 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2617 /* Barrier to sync with other CPUs */
2618 smp_mb__after_atomic();
2619 wake_up_bit(&data->flags,
2620 BTUSB_TX_WAIT_VND_EVT);
2622 kfree(urb->setup_packet);
2624 } else if (urb->status == -ENOENT) {
2625 /* Avoid suspend failed when usb_kill_urb */
2629 usb_mark_last_busy(data->udev);
2631 /* The URB complete handler is still called with urb->actual_length = 0
2632 * when the event is not available, so we should keep re-submitting
2633 * URB until WMT event returns, Also, It's necessary to wait some time
2634 * between the two consecutive control URBs to relax the target device
2635 * to generate the event. Otherwise, the WMT event cannot return from
2636 * the device successfully.
2640 usb_anchor_urb(urb, &data->ctrl_anchor);
2641 err = usb_submit_urb(urb, GFP_ATOMIC);
2643 kfree(urb->setup_packet);
2644 /* -EPERM: urb is being killed;
2645 * -ENODEV: device got disconnected
2647 if (err != -EPERM && err != -ENODEV)
2648 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2650 usb_unanchor_urb(urb);
2654 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2656 struct btusb_data *data = hci_get_drvdata(hdev);
2657 struct usb_ctrlrequest *dr;
2663 urb = usb_alloc_urb(0, GFP_KERNEL);
2667 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2673 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2675 dr->wIndex = cpu_to_le16(0);
2676 dr->wValue = cpu_to_le16(48);
2677 dr->wLength = cpu_to_le16(size);
2679 buf = kmalloc(size, GFP_KERNEL);
2686 pipe = usb_rcvctrlpipe(data->udev, 0);
2688 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2689 buf, size, btusb_mtk_wmt_recv, hdev);
2691 urb->transfer_flags |= URB_FREE_BUFFER;
2693 usb_anchor_urb(urb, &data->ctrl_anchor);
2694 err = usb_submit_urb(urb, GFP_KERNEL);
2696 if (err != -EPERM && err != -ENODEV)
2697 bt_dev_err(hdev, "urb %p submission failed (%d)",
2699 usb_unanchor_urb(urb);
2707 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
2708 struct btmtk_hci_wmt_params *wmt_params)
2710 struct btusb_data *data = hci_get_drvdata(hdev);
2711 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
2712 u32 hlen, status = BTMTK_WMT_INVALID;
2713 struct btmtk_hci_wmt_evt *wmt_evt;
2714 struct btmtk_hci_wmt_cmd *wc;
2715 struct btmtk_wmt_hdr *hdr;
2718 /* Send the WMT command and wait until the WMT event returns */
2719 hlen = sizeof(*hdr) + wmt_params->dlen;
2723 wc = kzalloc(hlen, GFP_KERNEL);
2729 hdr->op = wmt_params->op;
2730 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
2731 hdr->flag = wmt_params->flag;
2732 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
2734 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2736 /* WMT cmd/event doesn't follow up the generic HCI cmd/event handling,
2737 * it needs constantly polling control pipe until the host received the
2738 * WMT event, thus, we should require to specifically acquire PM counter
2739 * on the USB to prevent the interface from entering auto suspended
2740 * while WMT cmd/event in progress.
2742 err = usb_autopm_get_interface(data->intf);
2746 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
2749 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2750 usb_autopm_put_interface(data->intf);
2754 /* Submit control IN URB on demand to process the WMT event */
2755 err = btusb_mtk_submit_wmt_recv_urb(hdev);
2757 usb_autopm_put_interface(data->intf);
2762 /* The vendor specific WMT commands are all answered by a vendor
2763 * specific event and will have the Command Status or Command
2764 * Complete as with usual HCI command flow control.
2766 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
2767 * state to be cleared. The driver specific event receive routine
2768 * will clear that state and with that indicate completion of the
2771 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
2772 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
2773 if (err == -EINTR) {
2774 bt_dev_err(hdev, "Execution of wmt command interrupted");
2775 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2780 bt_dev_err(hdev, "Execution of wmt command timed out");
2781 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2786 /* Parse and handle the return WMT event */
2787 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
2788 if (wmt_evt->whdr.op != hdr->op) {
2789 bt_dev_err(hdev, "Wrong op received %d expected %d",
2790 wmt_evt->whdr.op, hdr->op);
2795 switch (wmt_evt->whdr.op) {
2796 case BTMTK_WMT_SEMAPHORE:
2797 if (wmt_evt->whdr.flag == 2)
2798 status = BTMTK_WMT_PATCH_UNDONE;
2800 status = BTMTK_WMT_PATCH_DONE;
2802 case BTMTK_WMT_FUNC_CTRL:
2803 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
2804 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
2805 status = BTMTK_WMT_ON_DONE;
2806 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
2807 status = BTMTK_WMT_ON_PROGRESS;
2809 status = BTMTK_WMT_ON_UNDONE;
2811 case BTMTK_WMT_PATCH_DWNLD:
2812 if (wmt_evt->whdr.flag == 2)
2813 status = BTMTK_WMT_PATCH_DONE;
2814 else if (wmt_evt->whdr.flag == 1)
2815 status = BTMTK_WMT_PATCH_PROGRESS;
2817 status = BTMTK_WMT_PATCH_UNDONE;
2821 if (wmt_params->status)
2822 *wmt_params->status = status;
2825 kfree_skb(data->evt_skb);
2826 data->evt_skb = NULL;
2832 static int btusb_mtk_func_query(struct hci_dev *hdev)
2834 struct btmtk_hci_wmt_params wmt_params;
2838 /* Query whether the function is enabled */
2839 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2840 wmt_params.flag = 4;
2841 wmt_params.dlen = sizeof(param);
2842 wmt_params.data = ¶m;
2843 wmt_params.status = &status;
2845 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2847 bt_dev_err(hdev, "Failed to query function status (%d)", err);
2854 static int btusb_mtk_uhw_reg_write(struct btusb_data *data, u32 reg, u32 val)
2856 struct hci_dev *hdev = data->hdev;
2860 buf = kzalloc(4, GFP_KERNEL);
2864 put_unaligned_le32(val, buf);
2866 pipe = usb_sndctrlpipe(data->udev, 0);
2867 err = usb_control_msg(data->udev, pipe, 0x02,
2869 reg >> 16, reg & 0xffff,
2870 buf, 4, USB_CTRL_SET_TIMEOUT);
2872 bt_dev_err(hdev, "Failed to write uhw reg(%d)", err);
2882 static int btusb_mtk_uhw_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2884 struct hci_dev *hdev = data->hdev;
2888 buf = kzalloc(4, GFP_KERNEL);
2892 pipe = usb_rcvctrlpipe(data->udev, 0);
2893 err = usb_control_msg(data->udev, pipe, 0x01,
2895 reg >> 16, reg & 0xffff,
2896 buf, 4, USB_CTRL_SET_TIMEOUT);
2898 bt_dev_err(hdev, "Failed to read uhw reg(%d)", err);
2902 *val = get_unaligned_le32(buf);
2903 bt_dev_dbg(hdev, "reg=%x, value=0x%08x", reg, *val);
2911 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2913 int pipe, err, size = sizeof(u32);
2916 buf = kzalloc(size, GFP_KERNEL);
2920 pipe = usb_rcvctrlpipe(data->udev, 0);
2921 err = usb_control_msg(data->udev, pipe, 0x63,
2922 USB_TYPE_VENDOR | USB_DIR_IN,
2923 reg >> 16, reg & 0xffff,
2924 buf, size, USB_CTRL_SET_TIMEOUT);
2928 *val = get_unaligned_le32(buf);
2936 static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
2938 return btusb_mtk_reg_read(data, reg, id);
2941 static int btusb_mtk_setup(struct hci_dev *hdev)
2943 struct btusb_data *data = hci_get_drvdata(hdev);
2944 struct btmtk_hci_wmt_params wmt_params;
2945 ktime_t calltime, delta, rettime;
2946 struct btmtk_tci_sleep tci_sleep;
2947 unsigned long long duration;
2948 struct sk_buff *skb;
2952 char fw_bin_name[64];
2956 calltime = ktime_get();
2958 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
2960 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2965 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
2967 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2970 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
2972 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
2979 fwname = FIRMWARE_MT7663;
2982 fwname = FIRMWARE_MT7668;
2986 snprintf(fw_bin_name, sizeof(fw_bin_name),
2987 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
2988 dev_id & 0xffff, (fw_version & 0xff) + 1);
2989 err = btmtk_setup_firmware_79xx(hdev, fw_bin_name,
2990 btusb_mtk_hci_wmt_sync);
2992 bt_dev_err(hdev, "Failed to set up firmware (%d)", err);
2996 /* It's Device EndPoint Reset Option Register */
2997 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2999 /* Enable Bluetooth protocol */
3001 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3002 wmt_params.flag = 0;
3003 wmt_params.dlen = sizeof(param);
3004 wmt_params.data = ¶m;
3005 wmt_params.status = NULL;
3007 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3009 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3013 hci_set_msft_opcode(hdev, 0xFD30);
3014 hci_set_aosp_capable(hdev);
3017 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
3022 /* Query whether the firmware is already download */
3023 wmt_params.op = BTMTK_WMT_SEMAPHORE;
3024 wmt_params.flag = 1;
3025 wmt_params.dlen = 0;
3026 wmt_params.data = NULL;
3027 wmt_params.status = &status;
3029 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3031 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
3035 if (status == BTMTK_WMT_PATCH_DONE) {
3036 bt_dev_info(hdev, "firmware already downloaded");
3037 goto ignore_setup_fw;
3040 /* Setup a firmware which the device definitely requires */
3041 err = btmtk_setup_firmware(hdev, fwname,
3042 btusb_mtk_hci_wmt_sync);
3047 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
3048 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
3050 /* -ETIMEDOUT happens */
3054 /* The other errors happen in btusb_mtk_func_query */
3058 if (status == BTMTK_WMT_ON_DONE) {
3059 bt_dev_info(hdev, "function already on");
3060 goto ignore_func_on;
3063 /* Enable Bluetooth protocol */
3065 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3066 wmt_params.flag = 0;
3067 wmt_params.dlen = sizeof(param);
3068 wmt_params.data = ¶m;
3069 wmt_params.status = NULL;
3071 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3073 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3078 /* Apply the low power environment setup */
3079 tci_sleep.mode = 0x5;
3080 tci_sleep.duration = cpu_to_le16(0x640);
3081 tci_sleep.host_duration = cpu_to_le16(0x640);
3082 tci_sleep.host_wakeup_pin = 0;
3083 tci_sleep.time_compensation = 0;
3085 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
3089 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
3095 rettime = ktime_get();
3096 delta = ktime_sub(rettime, calltime);
3097 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3099 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
3104 static int btusb_mtk_shutdown(struct hci_dev *hdev)
3106 struct btmtk_hci_wmt_params wmt_params;
3110 /* Disable the device */
3111 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3112 wmt_params.flag = 0;
3113 wmt_params.dlen = sizeof(param);
3114 wmt_params.data = ¶m;
3115 wmt_params.status = NULL;
3117 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3119 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3126 static void btusb_mtk_cmd_timeout(struct hci_dev *hdev)
3128 struct btusb_data *data = hci_get_drvdata(hdev);
3132 /* It's MediaTek specific bluetooth reset mechanism via USB */
3133 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
3134 bt_dev_err(hdev, "last reset failed? Not resetting again");
3138 err = usb_autopm_get_interface(data->intf);
3142 btusb_stop_traffic(data);
3143 usb_kill_anchored_urbs(&data->tx_anchor);
3145 /* It's Device EndPoint Reset Option Register */
3146 bt_dev_dbg(hdev, "Initiating reset mechanism via uhw");
3147 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
3148 btusb_mtk_uhw_reg_read(data, MTK_BT_WDT_STATUS, &val);
3150 /* Reset the bluetooth chip via USB interface. */
3151 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 1);
3152 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT, 0x000000FF);
3153 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT, &val);
3154 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT1, 0x000000FF);
3155 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT1, &val);
3156 /* MT7921 need to delay 20ms between toggle reset bit */
3158 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 0);
3159 btusb_mtk_uhw_reg_read(data, MTK_BT_SUBSYS_RST, &val);
3161 /* Poll the register until reset is completed */
3163 btusb_mtk_uhw_reg_read(data, MTK_BT_MISC, &val);
3164 if (val & MTK_BT_RST_DONE) {
3165 bt_dev_dbg(hdev, "Bluetooth Reset Successfully");
3169 bt_dev_dbg(hdev, "Polling Bluetooth Reset CR");
3171 msleep(MTK_BT_RESET_WAIT_MS);
3172 } while (retry < MTK_BT_RESET_NUM_TRIES);
3174 btusb_mtk_id_get(data, 0x70010200, &val);
3176 bt_dev_err(hdev, "Can't get device id, subsys reset fail.");
3178 usb_queue_reset_device(data->intf);
3180 clear_bit(BTUSB_HW_RESET_ACTIVE, &data->flags);
3183 static int btusb_recv_acl_mtk(struct hci_dev *hdev, struct sk_buff *skb)
3185 struct btusb_data *data = hci_get_drvdata(hdev);
3186 u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
3189 case 0xfc6f: /* Firmware dump from device */
3190 /* When the firmware hangs, the device can no longer
3191 * suspend and thus disable auto-suspend.
3193 usb_disable_autosuspend(data->udev);
3195 case 0x05ff: /* Firmware debug logging 1 */
3196 case 0x05fe: /* Firmware debug logging 2 */
3197 return hci_recv_diag(hdev, skb);
3200 return hci_recv_frame(hdev, skb);
3204 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3205 static int marvell_config_oob_wake(struct hci_dev *hdev)
3207 struct sk_buff *skb;
3208 struct btusb_data *data = hci_get_drvdata(hdev);
3209 struct device *dev = &data->udev->dev;
3210 u16 pin, gap, opcode;
3214 /* Move on if no wakeup pin specified */
3215 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3216 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3219 /* Vendor specific command to configure a GPIO as wake-up pin */
3220 opcode = hci_opcode_pack(0x3F, 0x59);
3221 cmd[0] = opcode & 0xFF;
3222 cmd[1] = opcode >> 8;
3223 cmd[2] = 2; /* length of parameters that follow */
3225 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3227 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3229 bt_dev_err(hdev, "%s: No memory", __func__);
3233 skb_put_data(skb, cmd, sizeof(cmd));
3234 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3236 ret = btusb_send_frame(hdev, skb);
3238 bt_dev_err(hdev, "%s: configuration failed", __func__);
3247 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3248 const bdaddr_t *bdaddr)
3250 struct sk_buff *skb;
3255 buf[1] = sizeof(bdaddr_t);
3256 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3258 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3261 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3270 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3271 const bdaddr_t *bdaddr)
3273 struct sk_buff *skb;
3280 buf[3] = sizeof(bdaddr_t);
3281 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3283 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3286 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3294 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3295 const bdaddr_t *bdaddr)
3297 struct sk_buff *skb;
3301 memcpy(buf, bdaddr, sizeof(bdaddr_t));
3303 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3304 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3307 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3315 #define QCA_MEMDUMP_ACL_HANDLE 0x2EDD
3316 #define QCA_MEMDUMP_SIZE_MAX 0x100000
3317 #define QCA_MEMDUMP_VSE_CLASS 0x01
3318 #define QCA_MEMDUMP_MSG_TYPE 0x08
3319 #define QCA_MEMDUMP_PKT_SIZE 248
3320 #define QCA_LAST_SEQUENCE_NUM 0xffff
3322 struct qca_dump_hdr {
3330 __le32 ram_dump_size;
3337 static void btusb_dump_hdr_qca(struct hci_dev *hdev, struct sk_buff *skb)
3340 struct btusb_data *btdata = hci_get_drvdata(hdev);
3342 snprintf(buf, sizeof(buf), "Controller Name: 0x%x\n",
3343 btdata->qca_dump.controller_id);
3344 skb_put_data(skb, buf, strlen(buf));
3346 snprintf(buf, sizeof(buf), "Firmware Version: 0x%x\n",
3347 btdata->qca_dump.fw_version);
3348 skb_put_data(skb, buf, strlen(buf));
3350 snprintf(buf, sizeof(buf), "Driver: %s\nVendor: qca\n",
3352 skb_put_data(skb, buf, strlen(buf));
3354 snprintf(buf, sizeof(buf), "VID: 0x%x\nPID:0x%x\n",
3355 btdata->qca_dump.id_vendor, btdata->qca_dump.id_product);
3356 skb_put_data(skb, buf, strlen(buf));
3358 snprintf(buf, sizeof(buf), "Lmp Subversion: 0x%x\n",
3360 skb_put_data(skb, buf, strlen(buf));
3363 static void btusb_coredump_qca(struct hci_dev *hdev)
3365 static const u8 param[] = { 0x26 };
3366 struct sk_buff *skb;
3368 skb = __hci_cmd_sync(hdev, 0xfc0c, 1, param, HCI_CMD_TIMEOUT);
3370 bt_dev_err(hdev, "%s: triggle crash failed (%ld)", __func__, PTR_ERR(skb));
3375 * ==0: not a dump pkt.
3376 * < 0: fails to handle a dump pkt
3379 static int handle_dump_pkt_qca(struct hci_dev *hdev, struct sk_buff *skb)
3384 unsigned int sk_len;
3388 struct hci_event_hdr *event_hdr;
3389 struct hci_acl_hdr *acl_hdr;
3390 struct qca_dump_hdr *dump_hdr;
3391 struct btusb_data *btdata = hci_get_drvdata(hdev);
3392 struct usb_device *udev = btdata->udev;
3394 pkt_type = hci_skb_pkt_type(skb);
3398 if (pkt_type == HCI_ACLDATA_PKT) {
3399 acl_hdr = hci_acl_hdr(skb);
3400 if (le16_to_cpu(acl_hdr->handle) != QCA_MEMDUMP_ACL_HANDLE)
3402 sk_ptr += HCI_ACL_HDR_SIZE;
3403 sk_len -= HCI_ACL_HDR_SIZE;
3404 event_hdr = (struct hci_event_hdr *)sk_ptr;
3406 event_hdr = hci_event_hdr(skb);
3409 if ((event_hdr->evt != HCI_VENDOR_PKT)
3410 || (event_hdr->plen != (sk_len - HCI_EVENT_HDR_SIZE)))
3413 sk_ptr += HCI_EVENT_HDR_SIZE;
3414 sk_len -= HCI_EVENT_HDR_SIZE;
3416 dump_hdr = (struct qca_dump_hdr *)sk_ptr;
3417 if ((sk_len < offsetof(struct qca_dump_hdr, data))
3418 || (dump_hdr->vse_class != QCA_MEMDUMP_VSE_CLASS)
3419 || (dump_hdr->msg_type != QCA_MEMDUMP_MSG_TYPE))
3422 /*it is dump pkt now*/
3423 seqno = le16_to_cpu(dump_hdr->seqno);
3425 set_bit(BTUSB_HW_SSR_ACTIVE, &btdata->flags);
3426 dump_size = le32_to_cpu(dump_hdr->ram_dump_size);
3427 if (!dump_size || (dump_size > QCA_MEMDUMP_SIZE_MAX)) {
3429 bt_dev_err(hdev, "Invalid memdump size(%u)",
3434 ret = hci_devcd_init(hdev, dump_size);
3436 bt_dev_err(hdev, "memdump init error(%d)", ret);
3440 btdata->qca_dump.ram_dump_size = dump_size;
3441 btdata->qca_dump.ram_dump_seqno = 0;
3442 sk_ptr += offsetof(struct qca_dump_hdr, data0);
3443 sk_len -= offsetof(struct qca_dump_hdr, data0);
3445 usb_disable_autosuspend(udev);
3446 bt_dev_info(hdev, "%s memdump size(%u)\n",
3447 (pkt_type == HCI_ACLDATA_PKT) ? "ACL" : "event",
3450 sk_ptr += offsetof(struct qca_dump_hdr, data);
3451 sk_len -= offsetof(struct qca_dump_hdr, data);
3454 if (!btdata->qca_dump.ram_dump_size) {
3456 bt_dev_err(hdev, "memdump is not active");
3460 if ((seqno > btdata->qca_dump.ram_dump_seqno + 1) && (seqno != QCA_LAST_SEQUENCE_NUM)) {
3461 dump_size = QCA_MEMDUMP_PKT_SIZE * (seqno - btdata->qca_dump.ram_dump_seqno - 1);
3462 hci_devcd_append_pattern(hdev, 0x0, dump_size);
3464 "expected memdump seqno(%u) is not received(%u)\n",
3465 btdata->qca_dump.ram_dump_seqno, seqno);
3466 btdata->qca_dump.ram_dump_seqno = seqno;
3471 skb_pull(skb, skb->len - sk_len);
3472 hci_devcd_append(hdev, skb);
3473 btdata->qca_dump.ram_dump_seqno++;
3474 if (seqno == QCA_LAST_SEQUENCE_NUM) {
3476 "memdump done: pkts(%u), total(%u)\n",
3477 btdata->qca_dump.ram_dump_seqno, btdata->qca_dump.ram_dump_size);
3479 hci_devcd_complete(hdev);
3485 if (btdata->qca_dump.ram_dump_size)
3486 usb_enable_autosuspend(udev);
3487 btdata->qca_dump.ram_dump_size = 0;
3488 btdata->qca_dump.ram_dump_seqno = 0;
3489 clear_bit(BTUSB_HW_SSR_ACTIVE, &btdata->flags);
3496 static int btusb_recv_acl_qca(struct hci_dev *hdev, struct sk_buff *skb)
3498 if (handle_dump_pkt_qca(hdev, skb))
3500 return hci_recv_frame(hdev, skb);
3503 static int btusb_recv_evt_qca(struct hci_dev *hdev, struct sk_buff *skb)
3505 if (handle_dump_pkt_qca(hdev, skb))
3507 return hci_recv_frame(hdev, skb);
3511 #define QCA_DFU_PACKET_LEN 4096
3513 #define QCA_GET_TARGET_VERSION 0x09
3514 #define QCA_CHECK_STATUS 0x05
3515 #define QCA_DFU_DOWNLOAD 0x01
3517 #define QCA_SYSCFG_UPDATED 0x40
3518 #define QCA_PATCH_UPDATED 0x80
3519 #define QCA_DFU_TIMEOUT 3000
3520 #define QCA_FLAG_MULTI_NVM 0x80
3521 #define QCA_BT_RESET_WAIT_MS 100
3523 #define WCN6855_2_0_RAM_VERSION_GF 0x400c1200
3524 #define WCN6855_2_1_RAM_VERSION_GF 0x400c1211
3526 struct qca_version {
3528 __le32 patch_version;
3536 struct qca_rampatch_version {
3537 __le16 rom_version_high;
3538 __le16 rom_version_low;
3539 __le16 patch_version;
3542 struct qca_device_info {
3544 u8 rampatch_hdr; /* length of header in rampatch */
3545 u8 nvm_hdr; /* length of header in NVM */
3546 u8 ver_offset; /* offset of version structure in rampatch */
3549 static const struct qca_device_info qca_devices_table[] = {
3550 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3551 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3552 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3553 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3554 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3555 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3556 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3557 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3558 { 0x00130201, 40, 4, 16 }, /* WCN6855 2.1 */
3559 { 0x00190200, 40, 4, 16 }, /* WCN785x 2.0 */
3562 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3563 void *data, u16 size)
3568 buf = kmalloc(size, GFP_KERNEL);
3572 /* Found some of USB hosts have IOT issues with ours so that we should
3573 * not wait until HCI layer is ready.
3575 pipe = usb_rcvctrlpipe(udev, 0);
3576 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3577 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3579 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3583 memcpy(data, buf, size);
3591 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3592 const struct firmware *firmware,
3595 struct btusb_data *btdata = hci_get_drvdata(hdev);
3596 struct usb_device *udev = btdata->udev;
3597 size_t count, size, sent = 0;
3601 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3605 count = firmware->size;
3607 size = min_t(size_t, count, hdr_size);
3608 memcpy(buf, firmware->data, size);
3610 /* USB patches should go down to controller through USB path
3611 * because binary format fits to go down through USB channel.
3612 * USB control path is for patching headers and USB bulk is for
3615 pipe = usb_sndctrlpipe(udev, 0);
3616 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3617 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3619 bt_dev_err(hdev, "Failed to send headers (%d)", err);
3626 /* ep2 need time to switch from function acl to function dfu,
3627 * so we add 20ms delay here.
3632 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3634 memcpy(buf, firmware->data + sent, size);
3636 pipe = usb_sndbulkpipe(udev, 0x02);
3637 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3640 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3641 sent, firmware->size, err);
3646 bt_dev_err(hdev, "Failed to get bulk buffer");
3660 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3661 struct qca_version *ver,
3662 const struct qca_device_info *info)
3664 struct qca_rampatch_version *rver;
3665 const struct firmware *fw;
3666 u32 ver_rom, ver_patch, rver_rom;
3667 u16 rver_rom_low, rver_rom_high, rver_patch;
3671 ver_rom = le32_to_cpu(ver->rom_version);
3672 ver_patch = le32_to_cpu(ver->patch_version);
3674 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
3676 err = request_firmware(&fw, fwname, &hdev->dev);
3678 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3683 bt_dev_info(hdev, "using rampatch file: %s", fwname);
3685 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3686 rver_rom_low = le16_to_cpu(rver->rom_version_low);
3687 rver_patch = le16_to_cpu(rver->patch_version);
3689 if (ver_rom & ~0xffffU) {
3690 rver_rom_high = le16_to_cpu(rver->rom_version_high);
3691 rver_rom = rver_rom_high << 16 | rver_rom_low;
3693 rver_rom = rver_rom_low;
3696 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3697 "firmware rome 0x%x build 0x%x",
3698 rver_rom, rver_patch, ver_rom, ver_patch);
3700 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
3701 bt_dev_err(hdev, "rampatch file version did not match with firmware");
3706 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3709 release_firmware(fw);
3714 static void btusb_generate_qca_nvm_name(char *fwname, size_t max_size,
3715 const struct qca_version *ver)
3717 u32 rom_version = le32_to_cpu(ver->rom_version);
3718 u16 flag = le16_to_cpu(ver->flag);
3720 if (((flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
3721 /* The board_id should be split into two bytes
3722 * The 1st byte is chip ID, and the 2nd byte is platform ID
3723 * For example, board ID 0x010A, 0x01 is platform ID. 0x0A is chip ID
3724 * we have several platforms, and platform IDs are continuously added
3726 * 0x00 is for Mobile
3728 * 0x02 is for Automotive
3729 * 0x03 is for Consumer electronic
3731 u16 board_id = (ver->chip_id << 8) + ver->platform_id;
3732 const char *variant;
3734 switch (le32_to_cpu(ver->ram_version)) {
3735 case WCN6855_2_0_RAM_VERSION_GF:
3736 case WCN6855_2_1_RAM_VERSION_GF:
3744 if (board_id == 0) {
3745 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s.bin",
3746 rom_version, variant);
3748 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s_%04x.bin",
3749 rom_version, variant, board_id);
3752 snprintf(fwname, max_size, "qca/nvm_usb_%08x.bin",
3758 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3759 struct qca_version *ver,
3760 const struct qca_device_info *info)
3762 const struct firmware *fw;
3766 btusb_generate_qca_nvm_name(fwname, sizeof(fwname), ver);
3768 err = request_firmware(&fw, fwname, &hdev->dev);
3770 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3775 bt_dev_info(hdev, "using NVM file: %s", fwname);
3777 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3779 release_firmware(fw);
3784 /* identify the ROM version and check whether patches are needed */
3785 static bool btusb_qca_need_patch(struct usb_device *udev)
3787 struct qca_version ver;
3789 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3792 /* only low ROM versions need patches */
3793 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3796 static int btusb_setup_qca(struct hci_dev *hdev)
3798 struct btusb_data *btdata = hci_get_drvdata(hdev);
3799 struct usb_device *udev = btdata->udev;
3800 const struct qca_device_info *info = NULL;
3801 struct qca_version ver;
3806 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3811 ver_rom = le32_to_cpu(ver.rom_version);
3813 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
3814 if (ver_rom == qca_devices_table[i].rom_version)
3815 info = &qca_devices_table[i];
3818 /* If the rom_version is not matched in the qca_devices_table
3819 * and the high ROM version is not zero, we assume this chip no
3820 * need to load the rampatch and nvm.
3822 if (ver_rom & ~0xffffU)
3825 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3829 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3834 if (!(status & QCA_PATCH_UPDATED)) {
3835 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3840 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3845 btdata->qca_dump.fw_version = le32_to_cpu(ver.patch_version);
3846 btdata->qca_dump.controller_id = le32_to_cpu(ver.rom_version);
3848 if (!(status & QCA_SYSCFG_UPDATED)) {
3849 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3853 /* WCN6855 2.1 and later will reset to apply firmware downloaded here, so
3854 * wait ~100ms for reset Done then go ahead, otherwise, it maybe
3855 * cause potential enable failure.
3857 if (info->rom_version >= 0x00130201)
3858 msleep(QCA_BT_RESET_WAIT_MS);
3861 /* Mark HCI_OP_ENHANCED_SETUP_SYNC_CONN as broken as it doesn't seem to
3862 * work with the likes of HSP/HFP mSBC.
3864 set_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &hdev->quirks);
3869 static inline int __set_diag_interface(struct hci_dev *hdev)
3871 struct btusb_data *data = hci_get_drvdata(hdev);
3872 struct usb_interface *intf = data->diag;
3878 data->diag_tx_ep = NULL;
3879 data->diag_rx_ep = NULL;
3881 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3882 struct usb_endpoint_descriptor *ep_desc;
3884 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3886 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3887 data->diag_tx_ep = ep_desc;
3891 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3892 data->diag_rx_ep = ep_desc;
3897 if (!data->diag_tx_ep || !data->diag_rx_ep) {
3898 bt_dev_err(hdev, "invalid diagnostic descriptors");
3905 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3907 struct btusb_data *data = hci_get_drvdata(hdev);
3908 struct sk_buff *skb;
3912 if (!data->diag_tx_ep)
3913 return ERR_PTR(-ENODEV);
3915 urb = usb_alloc_urb(0, GFP_KERNEL);
3917 return ERR_PTR(-ENOMEM);
3919 skb = bt_skb_alloc(2, GFP_KERNEL);
3922 return ERR_PTR(-ENOMEM);
3925 skb_put_u8(skb, 0xf0);
3926 skb_put_u8(skb, enable);
3928 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3930 usb_fill_bulk_urb(urb, data->udev, pipe,
3931 skb->data, skb->len, btusb_tx_complete, skb);
3933 skb->dev = (void *)hdev;
3938 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3940 struct btusb_data *data = hci_get_drvdata(hdev);
3946 if (!test_bit(HCI_RUNNING, &hdev->flags))
3949 urb = alloc_diag_urb(hdev, enable);
3951 return PTR_ERR(urb);
3953 return submit_or_queue_tx_urb(hdev, urb);
3957 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3959 struct btusb_data *data = priv;
3961 pm_wakeup_event(&data->udev->dev, 0);
3964 /* Disable only if not already disabled (keep it balanced) */
3965 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3966 disable_irq_nosync(irq);
3967 disable_irq_wake(irq);
3972 static const struct of_device_id btusb_match_table[] = {
3973 { .compatible = "usb1286,204e" },
3974 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3975 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
3978 MODULE_DEVICE_TABLE(of, btusb_match_table);
3980 /* Use an oob wakeup pin? */
3981 static int btusb_config_oob_wake(struct hci_dev *hdev)
3983 struct btusb_data *data = hci_get_drvdata(hdev);
3984 struct device *dev = &data->udev->dev;
3987 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3989 if (!of_match_device(btusb_match_table, dev))
3992 /* Move on if no IRQ specified */
3993 irq = of_irq_get_byname(dev->of_node, "wakeup");
3995 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
3999 irq_set_status_flags(irq, IRQ_NOAUTOEN);
4000 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
4001 0, "OOB Wake-on-BT", data);
4003 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
4007 ret = device_init_wakeup(dev, true);
4009 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
4013 data->oob_wake_irq = irq;
4014 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
4019 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
4021 if (dmi_check_system(btusb_needs_reset_resume_table))
4022 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
4025 static bool btusb_wakeup(struct hci_dev *hdev)
4027 struct btusb_data *data = hci_get_drvdata(hdev);
4029 return device_may_wakeup(&data->udev->dev);
4032 static int btusb_shutdown_qca(struct hci_dev *hdev)
4034 struct sk_buff *skb;
4036 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
4038 bt_dev_err(hdev, "HCI reset during shutdown failed");
4039 return PTR_ERR(skb);
4046 static ssize_t force_poll_sync_read(struct file *file, char __user *user_buf,
4047 size_t count, loff_t *ppos)
4049 struct btusb_data *data = file->private_data;
4052 buf[0] = data->poll_sync ? 'Y' : 'N';
4055 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
4058 static ssize_t force_poll_sync_write(struct file *file,
4059 const char __user *user_buf,
4060 size_t count, loff_t *ppos)
4062 struct btusb_data *data = file->private_data;
4066 err = kstrtobool_from_user(user_buf, count, &enable);
4070 /* Only allow changes while the adapter is down */
4071 if (test_bit(HCI_UP, &data->hdev->flags))
4074 if (data->poll_sync == enable)
4077 data->poll_sync = enable;
4082 static const struct file_operations force_poll_sync_fops = {
4083 .open = simple_open,
4084 .read = force_poll_sync_read,
4085 .write = force_poll_sync_write,
4086 .llseek = default_llseek,
4089 static int btusb_probe(struct usb_interface *intf,
4090 const struct usb_device_id *id)
4092 struct usb_endpoint_descriptor *ep_desc;
4093 struct gpio_desc *reset_gpio;
4094 struct btusb_data *data;
4095 struct hci_dev *hdev;
4096 unsigned ifnum_base;
4097 int i, err, priv_size;
4099 BT_DBG("intf %p id %p", intf, id);
4101 if ((id->driver_info & BTUSB_IFNUM_2) &&
4102 (intf->cur_altsetting->desc.bInterfaceNumber != 2))
4105 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
4107 if (!id->driver_info) {
4108 const struct usb_device_id *match;
4110 match = usb_match_id(intf, blacklist_table);
4115 if (id->driver_info == BTUSB_IGNORE)
4118 if (id->driver_info & BTUSB_ATH3012) {
4119 struct usb_device *udev = interface_to_usbdev(intf);
4121 /* Old firmware would otherwise let ath3k driver load
4122 * patch and sysconfig files
4124 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
4125 !btusb_qca_need_patch(udev))
4129 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
4133 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4134 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4136 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
4137 data->intr_ep = ep_desc;
4141 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4142 data->bulk_tx_ep = ep_desc;
4146 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4147 data->bulk_rx_ep = ep_desc;
4152 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
4155 if (id->driver_info & BTUSB_AMP) {
4156 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
4157 data->cmdreq = 0x2b;
4159 data->cmdreq_type = USB_TYPE_CLASS;
4160 data->cmdreq = 0x00;
4163 data->udev = interface_to_usbdev(intf);
4166 INIT_WORK(&data->work, btusb_work);
4167 INIT_WORK(&data->waker, btusb_waker);
4168 INIT_DELAYED_WORK(&data->rx_work, btusb_rx_work);
4170 skb_queue_head_init(&data->acl_q);
4172 init_usb_anchor(&data->deferred);
4173 init_usb_anchor(&data->tx_anchor);
4174 spin_lock_init(&data->txlock);
4176 init_usb_anchor(&data->intr_anchor);
4177 init_usb_anchor(&data->bulk_anchor);
4178 init_usb_anchor(&data->isoc_anchor);
4179 init_usb_anchor(&data->diag_anchor);
4180 init_usb_anchor(&data->ctrl_anchor);
4181 spin_lock_init(&data->rxlock);
4185 data->recv_event = hci_recv_frame;
4186 data->recv_bulk = btusb_recv_bulk;
4188 if (id->driver_info & BTUSB_INTEL_COMBINED) {
4189 /* Allocate extra space for Intel device */
4190 priv_size += sizeof(struct btintel_data);
4192 /* Override the rx handlers */
4193 data->recv_event = btusb_recv_event_intel;
4194 data->recv_bulk = btusb_recv_bulk_intel;
4195 } else if (id->driver_info & BTUSB_REALTEK) {
4196 /* Allocate extra space for Realtek device */
4197 priv_size += sizeof(struct btrealtek_data);
4200 data->recv_acl = hci_recv_frame;
4202 hdev = hci_alloc_dev_priv(priv_size);
4206 hdev->bus = HCI_USB;
4207 hci_set_drvdata(hdev, data);
4209 if (id->driver_info & BTUSB_AMP)
4210 hdev->dev_type = HCI_AMP;
4212 hdev->dev_type = HCI_PRIMARY;
4216 SET_HCIDEV_DEV(hdev, &intf->dev);
4218 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
4220 if (IS_ERR(reset_gpio)) {
4221 err = PTR_ERR(reset_gpio);
4223 } else if (reset_gpio) {
4224 data->reset_gpio = reset_gpio;
4227 hdev->open = btusb_open;
4228 hdev->close = btusb_close;
4229 hdev->flush = btusb_flush;
4230 hdev->send = btusb_send_frame;
4231 hdev->notify = btusb_notify;
4232 hdev->wakeup = btusb_wakeup;
4235 err = btusb_config_oob_wake(hdev);
4239 /* Marvell devices may need a specific chip configuration */
4240 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
4241 err = marvell_config_oob_wake(hdev);
4246 if (id->driver_info & BTUSB_CW6622)
4247 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4249 if (id->driver_info & BTUSB_BCM2045)
4250 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4252 if (id->driver_info & BTUSB_BCM92035)
4253 hdev->setup = btusb_setup_bcm92035;
4255 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4256 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
4257 hdev->manufacturer = 15;
4258 hdev->setup = btbcm_setup_patchram;
4259 hdev->set_diag = btusb_bcm_set_diag;
4260 hdev->set_bdaddr = btbcm_set_bdaddr;
4262 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4263 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4266 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4267 (id->driver_info & BTUSB_BCM_APPLE)) {
4268 hdev->manufacturer = 15;
4269 hdev->setup = btbcm_setup_apple;
4270 hdev->set_diag = btusb_bcm_set_diag;
4272 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4273 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4276 /* Combined Intel Device setup to support multiple setup routine */
4277 if (id->driver_info & BTUSB_INTEL_COMBINED) {
4278 err = btintel_configure_setup(hdev, btusb_driver.name);
4282 /* Transport specific configuration */
4283 hdev->send = btusb_send_frame_intel;
4284 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4286 if (id->driver_info & BTUSB_INTEL_NO_WBS_SUPPORT)
4287 btintel_set_flag(hdev, INTEL_ROM_LEGACY_NO_WBS_SUPPORT);
4289 if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
4290 btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
4292 if (id->driver_info & BTUSB_INTEL_BROKEN_SHUTDOWN_LED)
4293 btintel_set_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED);
4296 if (id->driver_info & BTUSB_MARVELL)
4297 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4299 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4300 (id->driver_info & BTUSB_MEDIATEK)) {
4301 hdev->setup = btusb_mtk_setup;
4302 hdev->shutdown = btusb_mtk_shutdown;
4303 hdev->manufacturer = 70;
4304 hdev->cmd_timeout = btusb_mtk_cmd_timeout;
4305 hdev->set_bdaddr = btmtk_set_bdaddr;
4306 set_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &hdev->quirks);
4307 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
4308 data->recv_acl = btusb_recv_acl_mtk;
4311 if (id->driver_info & BTUSB_SWAVE) {
4312 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
4313 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
4316 if (id->driver_info & BTUSB_INTEL_BOOT) {
4317 hdev->manufacturer = 2;
4318 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4321 if (id->driver_info & BTUSB_ATH3012) {
4322 data->setup_on_usb = btusb_setup_qca;
4323 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4324 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4325 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4328 if (id->driver_info & BTUSB_QCA_ROME) {
4329 data->setup_on_usb = btusb_setup_qca;
4330 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4331 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4332 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4333 btusb_check_needs_reset_resume(intf);
4336 if (id->driver_info & BTUSB_QCA_WCN6855) {
4337 data->qca_dump.id_vendor = id->idVendor;
4338 data->qca_dump.id_product = id->idProduct;
4339 data->recv_event = btusb_recv_evt_qca;
4340 data->recv_acl = btusb_recv_acl_qca;
4341 hci_devcd_register(hdev, btusb_coredump_qca, btusb_dump_hdr_qca, NULL);
4342 data->setup_on_usb = btusb_setup_qca;
4343 hdev->shutdown = btusb_shutdown_qca;
4344 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
4345 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4346 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4347 hci_set_msft_opcode(hdev, 0xFD70);
4350 if (id->driver_info & BTUSB_AMP) {
4351 /* AMP controllers do not support SCO packets */
4354 /* Interface orders are hardcoded in the specification */
4355 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
4356 data->isoc_ifnum = ifnum_base + 1;
4359 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4360 (id->driver_info & BTUSB_REALTEK)) {
4361 hdev->setup = btusb_setup_realtek;
4362 hdev->shutdown = btrtl_shutdown_realtek;
4363 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
4365 /* Realtek devices need to set remote wakeup on auto-suspend */
4366 set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
4367 set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
4370 if (id->driver_info & BTUSB_ACTIONS_SEMI) {
4371 /* Support is advertised, but not implemented */
4372 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
4373 set_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks);
4374 set_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks);
4375 set_bit(HCI_QUIRK_BROKEN_EXT_SCAN, &hdev->quirks);
4379 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4381 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
4382 if (!disable_scofix)
4383 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
4386 if (id->driver_info & BTUSB_BROKEN_ISOC)
4389 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
4390 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
4392 if (id->driver_info & BTUSB_VALID_LE_STATES)
4393 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
4395 if (id->driver_info & BTUSB_DIGIANSWER) {
4396 data->cmdreq_type = USB_TYPE_VENDOR;
4397 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4400 if (id->driver_info & BTUSB_CSR) {
4401 struct usb_device *udev = data->udev;
4402 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
4404 /* Old firmware would otherwise execute USB reset */
4405 if (bcdDevice < 0x117)
4406 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4408 /* This must be set first in case we disable it for fakes */
4409 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4411 /* Fake CSR devices with broken commands */
4412 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
4413 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4414 hdev->setup = btusb_setup_csr;
4417 if (id->driver_info & BTUSB_SNIFFER) {
4418 struct usb_device *udev = data->udev;
4420 /* New sniffer firmware has crippled HCI interface */
4421 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4422 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4425 if (id->driver_info & BTUSB_INTEL_BOOT) {
4426 /* A bug in the bootloader causes that interrupt interface is
4427 * only enabled after receiving SetInterface(0, AltSetting=0).
4429 err = usb_set_interface(data->udev, 0, 0);
4431 BT_ERR("failed to set interface 0, alt 0 %d", err);
4437 err = usb_driver_claim_interface(&btusb_driver,
4443 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
4444 if (!usb_driver_claim_interface(&btusb_driver,
4446 __set_diag_interface(hdev);
4451 if (enable_autosuspend)
4452 usb_enable_autosuspend(data->udev);
4454 data->poll_sync = enable_poll_sync;
4456 err = hci_register_dev(hdev);
4460 usb_set_intfdata(intf, data);
4462 debugfs_create_file("force_poll_sync", 0644, hdev->debugfs, data,
4463 &force_poll_sync_fops);
4468 if (data->reset_gpio)
4469 gpiod_put(data->reset_gpio);
4474 static void btusb_disconnect(struct usb_interface *intf)
4476 struct btusb_data *data = usb_get_intfdata(intf);
4477 struct hci_dev *hdev;
4479 BT_DBG("intf %p", intf);
4485 usb_set_intfdata(data->intf, NULL);
4488 usb_set_intfdata(data->isoc, NULL);
4491 usb_set_intfdata(data->diag, NULL);
4493 hci_unregister_dev(hdev);
4495 if (intf == data->intf) {
4497 usb_driver_release_interface(&btusb_driver, data->isoc);
4499 usb_driver_release_interface(&btusb_driver, data->diag);
4500 } else if (intf == data->isoc) {
4502 usb_driver_release_interface(&btusb_driver, data->diag);
4503 usb_driver_release_interface(&btusb_driver, data->intf);
4504 } else if (intf == data->diag) {
4505 usb_driver_release_interface(&btusb_driver, data->intf);
4507 usb_driver_release_interface(&btusb_driver, data->isoc);
4510 if (data->oob_wake_irq)
4511 device_init_wakeup(&data->udev->dev, false);
4513 if (data->reset_gpio)
4514 gpiod_put(data->reset_gpio);
4520 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4522 struct btusb_data *data = usb_get_intfdata(intf);
4524 BT_DBG("intf %p", intf);
4526 if (data->suspend_count++)
4529 spin_lock_irq(&data->txlock);
4530 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4531 set_bit(BTUSB_SUSPENDING, &data->flags);
4532 spin_unlock_irq(&data->txlock);
4534 spin_unlock_irq(&data->txlock);
4535 data->suspend_count--;
4539 cancel_work_sync(&data->work);
4541 btusb_stop_traffic(data);
4542 usb_kill_anchored_urbs(&data->tx_anchor);
4544 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4545 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4546 enable_irq_wake(data->oob_wake_irq);
4547 enable_irq(data->oob_wake_irq);
4550 /* For global suspend, Realtek devices lose the loaded fw
4551 * in them. But for autosuspend, firmware should remain.
4552 * Actually, it depends on whether the usb host sends
4553 * set feature (enable wakeup) or not.
4555 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
4556 if (PMSG_IS_AUTO(message) &&
4557 device_can_wakeup(&data->udev->dev))
4558 data->udev->do_remote_wakeup = 1;
4559 else if (!PMSG_IS_AUTO(message) &&
4560 !device_may_wakeup(&data->udev->dev)) {
4561 data->udev->do_remote_wakeup = 0;
4562 data->udev->reset_resume = 1;
4569 static void play_deferred(struct btusb_data *data)
4574 while ((urb = usb_get_from_anchor(&data->deferred))) {
4575 usb_anchor_urb(urb, &data->tx_anchor);
4577 err = usb_submit_urb(urb, GFP_ATOMIC);
4579 if (err != -EPERM && err != -ENODEV)
4580 BT_ERR("%s urb %p submission failed (%d)",
4581 data->hdev->name, urb, -err);
4582 kfree(urb->setup_packet);
4583 usb_unanchor_urb(urb);
4588 data->tx_in_flight++;
4592 /* Cleanup the rest deferred urbs. */
4593 while ((urb = usb_get_from_anchor(&data->deferred))) {
4594 kfree(urb->setup_packet);
4599 static int btusb_resume(struct usb_interface *intf)
4601 struct btusb_data *data = usb_get_intfdata(intf);
4602 struct hci_dev *hdev = data->hdev;
4605 BT_DBG("intf %p", intf);
4607 if (--data->suspend_count)
4610 /* Disable only if not already disabled (keep it balanced) */
4611 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4612 disable_irq(data->oob_wake_irq);
4613 disable_irq_wake(data->oob_wake_irq);
4616 if (!test_bit(HCI_RUNNING, &hdev->flags))
4619 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4620 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4622 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4627 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4628 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4630 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4634 btusb_submit_bulk_urb(hdev, GFP_NOIO);
4637 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4638 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4639 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4641 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4644 spin_lock_irq(&data->txlock);
4645 play_deferred(data);
4646 clear_bit(BTUSB_SUSPENDING, &data->flags);
4647 spin_unlock_irq(&data->txlock);
4648 schedule_work(&data->work);
4653 usb_scuttle_anchored_urbs(&data->deferred);
4655 spin_lock_irq(&data->txlock);
4656 clear_bit(BTUSB_SUSPENDING, &data->flags);
4657 spin_unlock_irq(&data->txlock);
4663 #ifdef CONFIG_DEV_COREDUMP
4664 static void btusb_coredump(struct device *dev)
4666 struct btusb_data *data = dev_get_drvdata(dev);
4667 struct hci_dev *hdev = data->hdev;
4669 if (hdev->dump.coredump)
4670 hdev->dump.coredump(hdev);
4674 static struct usb_driver btusb_driver = {
4676 .probe = btusb_probe,
4677 .disconnect = btusb_disconnect,
4679 .suspend = btusb_suspend,
4680 .resume = btusb_resume,
4682 .id_table = btusb_table,
4683 .supports_autosuspend = 1,
4684 .disable_hub_initiated_lpm = 1,
4686 #ifdef CONFIG_DEV_COREDUMP
4689 .coredump = btusb_coredump,
4695 module_usb_driver(btusb_driver);
4697 module_param(disable_scofix, bool, 0644);
4698 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4700 module_param(force_scofix, bool, 0644);
4701 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4703 module_param(enable_autosuspend, bool, 0644);
4704 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4706 module_param(reset, bool, 0644);
4707 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4709 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4710 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4711 MODULE_VERSION(VERSION);
4712 MODULE_LICENSE("GPL");