xref: /linux/drivers/bluetooth/btusb_main.c (revision fa96916cfd05ceb1c26af0b2ea73daf5481cad2d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  *  Generic Bluetooth USB driver
5  *
6  *  Copyright (C) 2005-2008  Marcel Holtmann <marcel@holtmann.org>
7  */
8 
9 #include <linux/cpufeature.h>
10 #include <linux/dmi.h>
11 #include <linux/module.h>
12 #include <linux/usb.h>
13 #include <linux/usb/quirks.h>
14 #include <linux/firmware.h>
15 #include <linux/iopoll.h>
16 #include <linux/of_device.h>
17 #include <linux/of_irq.h>
18 #include <linux/suspend.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/debugfs.h>
21 #include <linux/unaligned.h>
22 
23 #include <net/bluetooth/bluetooth.h>
24 #include <net/bluetooth/hci_core.h>
25 #include <net/bluetooth/hci_drv.h>
26 
27 #include "btintel.h"
28 #include "btbcm.h"
29 #include "btrtl.h"
30 #include "btmtk.h"
31 #include "hci_uart.h"
32 #include "btusb_qcom.h"
33 
34 #define VERSION "1.0"
35 
36 static bool disable_scofix;
37 static bool force_scofix;
38 static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
39 static bool enable_poll_sync = IS_ENABLED(CONFIG_BT_HCIBTUSB_POLL_SYNC);
40 static bool reset = true;
41 
42 static struct usb_driver btusb_driver;
43 
44 #define BTUSB_IGNORE			BIT(0)
45 #define BTUSB_DIGIANSWER		BIT(1)
46 #define BTUSB_CSR			BIT(2)
47 #define BTUSB_SNIFFER			BIT(3)
48 #define BTUSB_BCM92035			BIT(4)
49 #define BTUSB_BROKEN_ISOC		BIT(5)
50 #define BTUSB_WRONG_SCO_MTU		BIT(6)
51 #define BTUSB_ATH3012			BIT(7)
52 #define BTUSB_INTEL_COMBINED		BIT(8)
53 #define BTUSB_INTEL_BOOT		BIT(9)
54 #define BTUSB_BCM_PATCHRAM		BIT(10)
55 #define BTUSB_MARVELL			BIT(11)
56 #define BTUSB_SWAVE			BIT(12)
57 #define BTUSB_AMP			BIT(13)
58 #define BTUSB_QCA_ROME			BIT(14)
59 #define BTUSB_BCM_APPLE			BIT(15)
60 #define BTUSB_REALTEK			BIT(16)
61 #define BTUSB_BCM2045			BIT(17)
62 #define BTUSB_IFNUM_2			BIT(18)
63 #define BTUSB_CW6622			BIT(19)
64 #define BTUSB_MEDIATEK			BIT(20)
65 #define BTUSB_WIDEBAND_SPEECH		BIT(21)
66 #define BTUSB_INVALID_LE_STATES		BIT(22)
67 #define BTUSB_QCA_WCN6855		BIT(23)
68 #define BTUSB_INTEL_BROKEN_SHUTDOWN_LED	BIT(24)
69 #define BTUSB_INTEL_BROKEN_INITIAL_NCMD BIT(25)
70 #define BTUSB_INTEL_NO_WBS_SUPPORT	BIT(26)
71 #define BTUSB_ACTIONS_SEMI		BIT(27)
72 #define BTUSB_BARROT			BIT(28)
73 #define BTUSB_BROKEN_EXT_SCAN		BIT(29)
74 #define BTUSB_QUALCOMM			BIT(30)
75 
76 static const struct usb_device_id btusb_table[] = {
77 	/*
78 	 * NXP IW610 (0471:0215): the composite device reports Bluetooth
79 	 * class at the whole-device level, so the generic entry below
80 	 * would also match this WiFi vendor interface. Ignore it here
81 	 * first so mwifiex-nxp can bind it instead.
82 	 */
83 	{ USB_DEVICE_AND_INTERFACE_INFO(0x0471, 0x0215, 0xff, 0xff, 0xff),
84 	  .driver_info = BTUSB_IGNORE },
85 
86 	/* Generic Bluetooth USB device */
87 	{ USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
88 
89 	/* Generic Bluetooth AMP device */
90 	{ USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
91 
92 	/* Generic Bluetooth USB interface */
93 	{ USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
94 
95 	/* Apple-specific (Broadcom) devices */
96 	{ USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
97 	  .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
98 
99 	/* MediaTek MT76x0E */
100 	{ USB_DEVICE(0x0e8d, 0x763f) },
101 
102 	/* Broadcom SoftSailing reporting vendor specific */
103 	{ USB_DEVICE(0x0a5c, 0x21e1) },
104 
105 	/* Apple MacBookPro 7,1 */
106 	{ USB_DEVICE(0x05ac, 0x8213) },
107 
108 	/* Apple iMac11,1 */
109 	{ USB_DEVICE(0x05ac, 0x8215) },
110 
111 	/* Apple MacBookPro6,2 */
112 	{ USB_DEVICE(0x05ac, 0x8218) },
113 
114 	/* Apple MacBookAir3,1, MacBookAir3,2 */
115 	{ USB_DEVICE(0x05ac, 0x821b) },
116 
117 	/* Apple MacBookAir4,1 */
118 	{ USB_DEVICE(0x05ac, 0x821f) },
119 
120 	/* Apple MacBookPro8,2 */
121 	{ USB_DEVICE(0x05ac, 0x821a) },
122 
123 	/* Apple MacMini5,1 */
124 	{ USB_DEVICE(0x05ac, 0x8281) },
125 
126 	/* AVM BlueFRITZ! USB v2.0 */
127 	{ USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
128 
129 	/* Bluetooth Ultraport Module from IBM */
130 	{ USB_DEVICE(0x04bf, 0x030a) },
131 
132 	/* ALPS Modules with non-standard id */
133 	{ USB_DEVICE(0x044e, 0x3001) },
134 	{ USB_DEVICE(0x044e, 0x3002) },
135 
136 	/* Ericsson with non-standard id */
137 	{ USB_DEVICE(0x0bdb, 0x1002) },
138 
139 	/* Canyon CN-BTU1 with HID interfaces */
140 	{ USB_DEVICE(0x0c10, 0x0000) },
141 
142 	/* Broadcom BCM20702B0 (Dynex/Insignia) */
143 	{ USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
144 
145 	/* Broadcom BCM43142A0 (Foxconn/Lenovo) */
146 	{ USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
147 	  .driver_info = BTUSB_BCM_PATCHRAM },
148 
149 	/* Broadcom BCM920703 (HTC Vive) */
150 	{ USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
151 	  .driver_info = BTUSB_BCM_PATCHRAM },
152 
153 	/* Foxconn - Hon Hai */
154 	{ USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
155 	  .driver_info = BTUSB_BCM_PATCHRAM },
156 
157 	/* Lite-On Technology - Broadcom based */
158 	{ USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
159 	  .driver_info = BTUSB_BCM_PATCHRAM },
160 
161 	/* Broadcom devices with vendor specific id */
162 	{ USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
163 	  .driver_info = BTUSB_BCM_PATCHRAM },
164 
165 	/* ASUSTek Computer - Broadcom based */
166 	{ USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
167 	  .driver_info = BTUSB_BCM_PATCHRAM },
168 
169 	/* Belkin F8065bf - Broadcom based */
170 	{ USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
171 	  .driver_info = BTUSB_BCM_PATCHRAM },
172 
173 	/* IMC Networks - Broadcom based */
174 	{ USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
175 	  .driver_info = BTUSB_BCM_PATCHRAM },
176 
177 	/* Dell Computer - Broadcom based  */
178 	{ USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
179 	  .driver_info = BTUSB_BCM_PATCHRAM },
180 
181 	/* Toshiba Corp - Broadcom based */
182 	{ USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
183 	  .driver_info = BTUSB_BCM_PATCHRAM },
184 
185 	/* Intel Bluetooth USB Bootloader (RAM module) */
186 	{ USB_DEVICE(0x8087, 0x0a5a),
187 	  .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
188 
189 	{ }	/* Terminating entry */
190 };
191 
192 MODULE_DEVICE_TABLE(usb, btusb_table);
193 
194 static const struct usb_device_id quirks_table[] = {
195 	/* CSR BlueCore devices */
196 	{ USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
197 
198 	/* Broadcom BCM2033 without firmware */
199 	{ USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
200 
201 	/* Broadcom BCM2045 devices */
202 	{ USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
203 
204 	/* Atheros 3011 with sflash firmware */
205 	{ USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
206 	{ USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
207 	{ USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
208 	{ USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
209 	{ USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
210 	{ USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
211 	{ USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
212 
213 	/* Atheros AR9285 Malbec with sflash firmware */
214 	{ USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
215 
216 	/* Atheros 3012 with sflash firmware */
217 	{ USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
218 	{ USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
219 	{ USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
220 	{ USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
221 	{ USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
222 	{ USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
223 	{ USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
224 	{ USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
225 	{ USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
226 	{ USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
227 	{ USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
228 	{ USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
229 	{ USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
230 	{ USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
231 	{ USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
232 	{ USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
233 	{ USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
234 	{ USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
235 	{ USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
236 	{ USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
237 	{ USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
238 	{ USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
239 	{ USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
240 	{ USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
241 	{ USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
242 	{ USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
243 	{ USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
244 	{ USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
245 	{ USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
246 	{ USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
247 	{ USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
248 	{ USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
249 	{ USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
250 	{ USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
251 	{ USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
252 	{ USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
253 	{ USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
254 	{ USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
255 	{ USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
256 	{ USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
257 	{ USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
258 	{ USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
259 	{ USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
260 	{ USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
261 	{ USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
262 	{ USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
263 	{ USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
264 	{ USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
265 	{ USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
266 	{ USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
267 
268 	/* Atheros AR5BBU12 with sflash firmware */
269 	{ USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
270 
271 	/* Atheros AR5BBU12 with sflash firmware */
272 	{ USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
273 	{ USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
274 
275 	/* QCA ROME chipset */
276 	{ USB_DEVICE(0x0b05, 0x1868), .driver_info = BTUSB_QCA_ROME |
277 						     BTUSB_WIDEBAND_SPEECH },
278 	{ USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
279 						     BTUSB_WIDEBAND_SPEECH },
280 	{ USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
281 						     BTUSB_WIDEBAND_SPEECH },
282 	{ USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
283 						     BTUSB_WIDEBAND_SPEECH },
284 	{ USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
285 						     BTUSB_WIDEBAND_SPEECH },
286 	{ USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
287 						     BTUSB_WIDEBAND_SPEECH },
288 	{ USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
289 						     BTUSB_WIDEBAND_SPEECH },
290 	{ USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
291 						     BTUSB_WIDEBAND_SPEECH },
292 	{ USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
293 						     BTUSB_WIDEBAND_SPEECH },
294 	{ USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
295 						     BTUSB_WIDEBAND_SPEECH },
296 	{ USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
297 						     BTUSB_WIDEBAND_SPEECH },
298 	{ USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
299 						     BTUSB_WIDEBAND_SPEECH },
300 	{ USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
301 						     BTUSB_WIDEBAND_SPEECH },
302 	{ USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
303 						     BTUSB_WIDEBAND_SPEECH },
304 	{ USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
305 						     BTUSB_WIDEBAND_SPEECH },
306 	{ USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
307 						     BTUSB_WIDEBAND_SPEECH },
308 	{ USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
309 						     BTUSB_WIDEBAND_SPEECH },
310 	{ USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
311 						     BTUSB_WIDEBAND_SPEECH },
312 	{ USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
313 						     BTUSB_WIDEBAND_SPEECH },
314 	{ USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
315 						     BTUSB_WIDEBAND_SPEECH },
316 	{ USB_DEVICE(0x13d3, 0x3503), .driver_info = BTUSB_QCA_ROME |
317 						     BTUSB_WIDEBAND_SPEECH },
318 
319 	/* QCA WCN6855 chipset */
320 	{ USB_DEVICE(0x0489, 0xe0c7), .driver_info = BTUSB_QCA_WCN6855 |
321 						     BTUSB_WIDEBAND_SPEECH },
322 	{ USB_DEVICE(0x0489, 0xe0c9), .driver_info = BTUSB_QCA_WCN6855 |
323 						     BTUSB_WIDEBAND_SPEECH },
324 	{ USB_DEVICE(0x0489, 0xe0ca), .driver_info = BTUSB_QCA_WCN6855 |
325 						     BTUSB_WIDEBAND_SPEECH },
326 	{ USB_DEVICE(0x0489, 0xe0cb), .driver_info = BTUSB_QCA_WCN6855 |
327 						     BTUSB_WIDEBAND_SPEECH },
328 	{ USB_DEVICE(0x0489, 0xe0cc), .driver_info = BTUSB_QCA_WCN6855 |
329 						     BTUSB_WIDEBAND_SPEECH },
330 	{ USB_DEVICE(0x0489, 0xe0ce), .driver_info = BTUSB_QCA_WCN6855 |
331 						     BTUSB_WIDEBAND_SPEECH },
332 	{ USB_DEVICE(0x0489, 0xe0d0), .driver_info = BTUSB_QCA_WCN6855 |
333 						     BTUSB_WIDEBAND_SPEECH },
334 	{ USB_DEVICE(0x0489, 0xe0d6), .driver_info = BTUSB_QCA_WCN6855 |
335 						     BTUSB_WIDEBAND_SPEECH },
336 	{ USB_DEVICE(0x0489, 0xe0de), .driver_info = BTUSB_QCA_WCN6855 |
337 						     BTUSB_WIDEBAND_SPEECH },
338 	{ USB_DEVICE(0x0489, 0xe0df), .driver_info = BTUSB_QCA_WCN6855 |
339 						     BTUSB_WIDEBAND_SPEECH },
340 	{ USB_DEVICE(0x0489, 0xe0e1), .driver_info = BTUSB_QCA_WCN6855 |
341 						     BTUSB_WIDEBAND_SPEECH },
342 	{ USB_DEVICE(0x0489, 0xe0e3), .driver_info = BTUSB_QCA_WCN6855 |
343 						     BTUSB_WIDEBAND_SPEECH },
344 	{ USB_DEVICE(0x0489, 0xe0ea), .driver_info = BTUSB_QCA_WCN6855 |
345 						     BTUSB_WIDEBAND_SPEECH },
346 	{ USB_DEVICE(0x0489, 0xe0ec), .driver_info = BTUSB_QCA_WCN6855 |
347 						     BTUSB_WIDEBAND_SPEECH },
348 	{ USB_DEVICE(0x04ca, 0x3022), .driver_info = BTUSB_QCA_WCN6855 |
349 						     BTUSB_WIDEBAND_SPEECH },
350 	{ USB_DEVICE(0x04ca, 0x3023), .driver_info = BTUSB_QCA_WCN6855 |
351 						     BTUSB_WIDEBAND_SPEECH },
352 	{ USB_DEVICE(0x04ca, 0x3024), .driver_info = BTUSB_QCA_WCN6855 |
353 						     BTUSB_WIDEBAND_SPEECH },
354 	{ USB_DEVICE(0x04ca, 0x3a22), .driver_info = BTUSB_QCA_WCN6855 |
355 						     BTUSB_WIDEBAND_SPEECH },
356 	{ USB_DEVICE(0x04ca, 0x3a24), .driver_info = BTUSB_QCA_WCN6855 |
357 						     BTUSB_WIDEBAND_SPEECH },
358 	{ USB_DEVICE(0x04ca, 0x3a26), .driver_info = BTUSB_QCA_WCN6855 |
359 						     BTUSB_WIDEBAND_SPEECH },
360 	{ USB_DEVICE(0x04ca, 0x3a27), .driver_info = BTUSB_QCA_WCN6855 |
361 						     BTUSB_WIDEBAND_SPEECH },
362 	{ USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
363 						     BTUSB_WIDEBAND_SPEECH },
364 	{ USB_DEVICE(0x10ab, 0x9108), .driver_info = BTUSB_QCA_WCN6855 |
365 						     BTUSB_WIDEBAND_SPEECH },
366 	{ USB_DEVICE(0x10ab, 0x9109), .driver_info = BTUSB_QCA_WCN6855 |
367 						     BTUSB_WIDEBAND_SPEECH },
368 	{ USB_DEVICE(0x10ab, 0x9208), .driver_info = BTUSB_QCA_WCN6855 |
369 						     BTUSB_WIDEBAND_SPEECH },
370 	{ USB_DEVICE(0x10ab, 0x9209), .driver_info = BTUSB_QCA_WCN6855 |
371 						     BTUSB_WIDEBAND_SPEECH },
372 	{ USB_DEVICE(0x10ab, 0x9308), .driver_info = BTUSB_QCA_WCN6855 |
373 						     BTUSB_WIDEBAND_SPEECH },
374 	{ USB_DEVICE(0x10ab, 0x9309), .driver_info = BTUSB_QCA_WCN6855 |
375 						     BTUSB_WIDEBAND_SPEECH },
376 	{ USB_DEVICE(0x10ab, 0x9408), .driver_info = BTUSB_QCA_WCN6855 |
377 						     BTUSB_WIDEBAND_SPEECH },
378 	{ USB_DEVICE(0x10ab, 0x9409), .driver_info = BTUSB_QCA_WCN6855 |
379 						     BTUSB_WIDEBAND_SPEECH },
380 	{ USB_DEVICE(0x10ab, 0x9508), .driver_info = BTUSB_QCA_WCN6855 |
381 						     BTUSB_WIDEBAND_SPEECH },
382 	{ USB_DEVICE(0x10ab, 0x9509), .driver_info = BTUSB_QCA_WCN6855 |
383 						     BTUSB_WIDEBAND_SPEECH },
384 	{ USB_DEVICE(0x10ab, 0x9608), .driver_info = BTUSB_QCA_WCN6855 |
385 						     BTUSB_WIDEBAND_SPEECH },
386 	{ USB_DEVICE(0x10ab, 0x9609), .driver_info = BTUSB_QCA_WCN6855 |
387 						     BTUSB_WIDEBAND_SPEECH },
388 	{ USB_DEVICE(0x10ab, 0x9f09), .driver_info = BTUSB_QCA_WCN6855 |
389 						     BTUSB_WIDEBAND_SPEECH },
390 	{ USB_DEVICE(0x28de, 0x1401), .driver_info = BTUSB_QCA_WCN6855 |
391 						     BTUSB_WIDEBAND_SPEECH },
392 
393 	/* QCA WCN785x chipset */
394 	{ USB_DEVICE(0x0cf3, 0xe700), .driver_info = BTUSB_QCA_WCN6855 |
395 						     BTUSB_WIDEBAND_SPEECH },
396 	{ USB_DEVICE(0x0489, 0xe0fc), .driver_info = BTUSB_QCA_WCN6855 |
397 						     BTUSB_WIDEBAND_SPEECH },
398 	{ USB_DEVICE(0x0489, 0xe0f3), .driver_info = BTUSB_QCA_WCN6855 |
399 						     BTUSB_WIDEBAND_SPEECH },
400 	{ USB_DEVICE(0x0489, 0xe100), .driver_info = BTUSB_QCA_WCN6855 |
401 						     BTUSB_WIDEBAND_SPEECH },
402 	{ USB_DEVICE(0x0489, 0xe103), .driver_info = BTUSB_QCA_WCN6855 |
403 						     BTUSB_WIDEBAND_SPEECH },
404 	{ USB_DEVICE(0x0489, 0xe10a), .driver_info = BTUSB_QCA_WCN6855 |
405 						     BTUSB_WIDEBAND_SPEECH },
406 	{ USB_DEVICE(0x0489, 0xe10d), .driver_info = BTUSB_QCA_WCN6855 |
407 						     BTUSB_WIDEBAND_SPEECH },
408 	{ USB_DEVICE(0x0489, 0xe11b), .driver_info = BTUSB_QCA_WCN6855 |
409 						     BTUSB_WIDEBAND_SPEECH },
410 	{ USB_DEVICE(0x0489, 0xe11c), .driver_info = BTUSB_QCA_WCN6855 |
411 						     BTUSB_WIDEBAND_SPEECH },
412 	{ USB_DEVICE(0x0489, 0xe11f), .driver_info = BTUSB_QCA_WCN6855 |
413 						     BTUSB_WIDEBAND_SPEECH },
414 	{ USB_DEVICE(0x0489, 0xe141), .driver_info = BTUSB_QCA_WCN6855 |
415 						     BTUSB_WIDEBAND_SPEECH },
416 	{ USB_DEVICE(0x0489, 0xe14a), .driver_info = BTUSB_QCA_WCN6855 |
417 						     BTUSB_WIDEBAND_SPEECH },
418 	{ USB_DEVICE(0x0489, 0xe14b), .driver_info = BTUSB_QCA_WCN6855 |
419 						     BTUSB_WIDEBAND_SPEECH },
420 	{ USB_DEVICE(0x0489, 0xe14d), .driver_info = BTUSB_QCA_WCN6855 |
421 						     BTUSB_WIDEBAND_SPEECH },
422 	{ USB_DEVICE(0x13d3, 0x3623), .driver_info = BTUSB_QCA_WCN6855 |
423 						     BTUSB_WIDEBAND_SPEECH },
424 	{ USB_DEVICE(0x13d3, 0x3624), .driver_info = BTUSB_QCA_WCN6855 |
425 						     BTUSB_WIDEBAND_SPEECH },
426 	{ USB_DEVICE(0x2c7c, 0x0130), .driver_info = BTUSB_QCA_WCN6855 |
427 						     BTUSB_WIDEBAND_SPEECH },
428 	{ USB_DEVICE(0x2c7c, 0x0131), .driver_info = BTUSB_QCA_WCN6855 |
429 						     BTUSB_WIDEBAND_SPEECH },
430 	{ USB_DEVICE(0x2c7c, 0x0132), .driver_info = BTUSB_QCA_WCN6855 |
431 						     BTUSB_WIDEBAND_SPEECH },
432 
433 	/* Qualcomm multi-subsystem chipset QCC2072 */
434 	{ USB_DEVICE(0x0cf3, 0xea00), .driver_info = BTUSB_QUALCOMM |
435 						     BTUSB_WIDEBAND_SPEECH },
436 
437 	/* Broadcom BCM2035 */
438 	{ USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
439 	{ USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
440 	{ USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
441 
442 	/* Broadcom BCM2045 */
443 	{ USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
444 	{ USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
445 
446 	/* IBM/Lenovo ThinkPad with Broadcom chip */
447 	{ USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
448 	{ USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
449 
450 	/* HP laptop with Broadcom chip */
451 	{ USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
452 
453 	/* Dell laptop with Broadcom chip */
454 	{ USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
455 
456 	/* Dell Wireless 370 and 410 devices */
457 	{ USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
458 	{ USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
459 
460 	/* Belkin F8T012 and F8T013 devices */
461 	{ USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
462 	{ USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
463 
464 	/* Asus WL-BTD202 device */
465 	{ USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
466 
467 	/* Kensington Bluetooth USB adapter */
468 	{ USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
469 
470 	/* RTX Telecom based adapters with buggy SCO support */
471 	{ USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
472 	{ USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
473 
474 	/* CONWISE Technology based adapters with buggy SCO support */
475 	{ USB_DEVICE(0x0e5e, 0x6622),
476 	  .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
477 
478 	/* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
479 	{ USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
480 
481 	/* Digianswer devices */
482 	{ USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
483 	{ USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
484 
485 	/* CSR BlueCore Bluetooth Sniffer */
486 	{ USB_DEVICE(0x0a12, 0x0002),
487 	  .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
488 
489 	/* Frontline ComProbe Bluetooth Sniffer */
490 	{ USB_DEVICE(0x16d3, 0x0002),
491 	  .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
492 
493 	/* Marvell Bluetooth devices */
494 	{ USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
495 	{ USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
496 	{ USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
497 
498 	/*
499 	 * NXP IW610 BT interfaces (Marvell-lineage silicon, same quirk as
500 	 * the 0x1286 entries above). Scoped to the BT interface class,
501 	 * not just VID/PID -- see the btusb_table entry above.
502 	 */
503 	{ USB_DEVICE_AND_INTERFACE_INFO(0x0471, 0x0215, 0xe0, 0x01, 0x01),
504 	  .driver_info = BTUSB_MARVELL },
505 
506 	/* Intel Bluetooth devices */
507 	{ USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_COMBINED },
508 	{ USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_COMBINED },
509 	{ USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_COMBINED },
510 	{ USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_COMBINED },
511 	{ USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_COMBINED },
512 	{ USB_DEVICE(0x8087, 0x0035), .driver_info = BTUSB_INTEL_COMBINED },
513 	{ USB_DEVICE(0x8087, 0x0036), .driver_info = BTUSB_INTEL_COMBINED },
514 	{ USB_DEVICE(0x8087, 0x0037), .driver_info = BTUSB_INTEL_COMBINED },
515 	{ USB_DEVICE(0x8087, 0x0038), .driver_info = BTUSB_INTEL_COMBINED },
516 	{ USB_DEVICE(0x8087, 0x0039), .driver_info = BTUSB_INTEL_COMBINED },
517 	{ USB_DEVICE(0x8087, 0x0043), .driver_info = BTUSB_INTEL_COMBINED }, /* Otter Peak 2 */
518 	{ USB_DEVICE(0x8087, 0x0040), .driver_info = BTUSB_INTEL_COMBINED }, /* Lizard Peak 2 */
519 	{ USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
520 	{ USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL_COMBINED |
521 						     BTUSB_INTEL_NO_WBS_SUPPORT |
522 						     BTUSB_INTEL_BROKEN_INITIAL_NCMD |
523 						     BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
524 	{ USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL_COMBINED |
525 						     BTUSB_INTEL_NO_WBS_SUPPORT |
526 						     BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
527 	{ USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_COMBINED },
528 	{ USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL_COMBINED |
529 						     BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
530 	{ USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_COMBINED },
531 
532 	/* Other Intel Bluetooth devices */
533 	{ USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
534 	  .driver_info = BTUSB_IGNORE },
535 
536 	/* Realtek 8821CE Bluetooth devices */
537 	{ USB_DEVICE(0x13d3, 0x3529), .driver_info = BTUSB_REALTEK |
538 						     BTUSB_WIDEBAND_SPEECH },
539 	{ USB_DEVICE(0x13d3, 0x3533), .driver_info = BTUSB_REALTEK |
540 						     BTUSB_WIDEBAND_SPEECH },
541 	{ USB_DEVICE(0x13d3, 0x3556), .driver_info = BTUSB_REALTEK |
542 						     BTUSB_WIDEBAND_SPEECH },
543 	{ USB_DEVICE(0x13d3, 0x3558), .driver_info = BTUSB_REALTEK |
544 						     BTUSB_WIDEBAND_SPEECH },
545 
546 	/* Realtek 8822CE Bluetooth devices */
547 	{ USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
548 						     BTUSB_WIDEBAND_SPEECH },
549 	{ USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
550 						     BTUSB_WIDEBAND_SPEECH },
551 
552 	/* Realtek 8822CU Bluetooth devices */
553 	{ USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
554 						     BTUSB_WIDEBAND_SPEECH },
555 
556 	/* Realtek 8851BE Bluetooth devices */
557 	{ USB_DEVICE(0x0bda, 0xb850), .driver_info = BTUSB_REALTEK },
558 	{ USB_DEVICE(0x13d3, 0x3600), .driver_info = BTUSB_REALTEK },
559 	{ USB_DEVICE(0x13d3, 0x3601), .driver_info = BTUSB_REALTEK },
560 	{ USB_DEVICE(0x0489, 0xe112), .driver_info = BTUSB_REALTEK |
561 						     BTUSB_WIDEBAND_SPEECH },
562 
563 	/* Realtek 8851BU Bluetooth devices */
564 	{ USB_DEVICE(0x3625, 0x010b), .driver_info = BTUSB_REALTEK |
565 						     BTUSB_WIDEBAND_SPEECH },
566 	{ USB_DEVICE(0x2001, 0x332a), .driver_info = BTUSB_REALTEK |
567 						     BTUSB_WIDEBAND_SPEECH },
568 	{ USB_DEVICE(0x7392, 0xe611), .driver_info = BTUSB_REALTEK |
569 						     BTUSB_WIDEBAND_SPEECH },
570 	{ USB_DEVICE(0x2c4e, 0x0128), .driver_info = BTUSB_REALTEK |
571 						     BTUSB_WIDEBAND_SPEECH },
572 
573 	/* Realtek 8852AE Bluetooth devices */
574 	{ USB_DEVICE(0x0bda, 0x2852), .driver_info = BTUSB_REALTEK |
575 						     BTUSB_WIDEBAND_SPEECH },
576 	{ USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
577 						     BTUSB_WIDEBAND_SPEECH },
578 	{ USB_DEVICE(0x0bda, 0x385a), .driver_info = BTUSB_REALTEK |
579 						     BTUSB_WIDEBAND_SPEECH },
580 	{ USB_DEVICE(0x0bda, 0x4852), .driver_info = BTUSB_REALTEK |
581 						     BTUSB_WIDEBAND_SPEECH },
582 	{ USB_DEVICE(0x04c5, 0x165c), .driver_info = BTUSB_REALTEK |
583 						     BTUSB_WIDEBAND_SPEECH },
584 	{ USB_DEVICE(0x04ca, 0x4006), .driver_info = BTUSB_REALTEK |
585 						     BTUSB_WIDEBAND_SPEECH },
586 	{ USB_DEVICE(0x0cb8, 0xc549), .driver_info = BTUSB_REALTEK |
587 						     BTUSB_WIDEBAND_SPEECH },
588 
589 	/* Realtek 8852CE Bluetooth devices */
590 	{ USB_DEVICE(0x04ca, 0x4007), .driver_info = BTUSB_REALTEK |
591 						     BTUSB_WIDEBAND_SPEECH },
592 	{ USB_DEVICE(0x04c5, 0x1675), .driver_info = BTUSB_REALTEK |
593 						     BTUSB_WIDEBAND_SPEECH },
594 	{ USB_DEVICE(0x0cb8, 0xc558), .driver_info = BTUSB_REALTEK |
595 						     BTUSB_WIDEBAND_SPEECH },
596 	{ USB_DEVICE(0x13d3, 0x3587), .driver_info = BTUSB_REALTEK |
597 						     BTUSB_WIDEBAND_SPEECH },
598 	{ USB_DEVICE(0x13d3, 0x3586), .driver_info = BTUSB_REALTEK |
599 						     BTUSB_WIDEBAND_SPEECH },
600 	{ USB_DEVICE(0x13d3, 0x3592), .driver_info = BTUSB_REALTEK |
601 						     BTUSB_WIDEBAND_SPEECH },
602 	{ USB_DEVICE(0x13d3, 0x3612), .driver_info = BTUSB_REALTEK |
603 						     BTUSB_WIDEBAND_SPEECH },
604 	{ USB_DEVICE(0x0489, 0xe122), .driver_info = BTUSB_REALTEK |
605 						     BTUSB_WIDEBAND_SPEECH },
606 
607 	/* Realtek 8852BE Bluetooth devices */
608 	{ USB_DEVICE(0x0cb8, 0xc559), .driver_info = BTUSB_REALTEK |
609 						     BTUSB_WIDEBAND_SPEECH },
610 	{ USB_DEVICE(0x0bda, 0x4853), .driver_info = BTUSB_REALTEK |
611 						     BTUSB_WIDEBAND_SPEECH },
612 	{ USB_DEVICE(0x0bda, 0x887b), .driver_info = BTUSB_REALTEK |
613 						     BTUSB_WIDEBAND_SPEECH },
614 	{ USB_DEVICE(0x0bda, 0xb85b), .driver_info = BTUSB_REALTEK |
615 						     BTUSB_WIDEBAND_SPEECH },
616 	{ USB_DEVICE(0x13d3, 0x3570), .driver_info = BTUSB_REALTEK |
617 						     BTUSB_WIDEBAND_SPEECH },
618 	{ USB_DEVICE(0x13d3, 0x3571), .driver_info = BTUSB_REALTEK |
619 						     BTUSB_WIDEBAND_SPEECH },
620 	{ USB_DEVICE(0x13d3, 0x3572), .driver_info = BTUSB_REALTEK |
621 						     BTUSB_WIDEBAND_SPEECH },
622 	{ USB_DEVICE(0x13d3, 0x3591), .driver_info = BTUSB_REALTEK |
623 						     BTUSB_WIDEBAND_SPEECH },
624 	{ USB_DEVICE(0x0489, 0xe123), .driver_info = BTUSB_REALTEK |
625 						     BTUSB_WIDEBAND_SPEECH },
626 	{ USB_DEVICE(0x0489, 0xe125), .driver_info = BTUSB_REALTEK |
627 						     BTUSB_WIDEBAND_SPEECH },
628 
629 	/* Realtek 8852BT/8852BE-VT Bluetooth devices */
630 	{ USB_DEVICE(0x0bda, 0x8520), .driver_info = BTUSB_REALTEK |
631 						     BTUSB_WIDEBAND_SPEECH },
632 	{ USB_DEVICE(0x0489, 0xe12f), .driver_info = BTUSB_REALTEK |
633 						     BTUSB_WIDEBAND_SPEECH },
634 	{ USB_DEVICE(0x13d3, 0x3618), .driver_info = BTUSB_REALTEK |
635 						     BTUSB_WIDEBAND_SPEECH },
636 	{ USB_DEVICE(0x13d3, 0x3619), .driver_info = BTUSB_REALTEK |
637 						     BTUSB_WIDEBAND_SPEECH },
638 
639 	/* Realtek 8922AE Bluetooth devices */
640 	{ USB_DEVICE(0x0bda, 0x8922), .driver_info = BTUSB_REALTEK |
641 						     BTUSB_WIDEBAND_SPEECH },
642 	{ USB_DEVICE(0x0bda, 0xd922), .driver_info = BTUSB_REALTEK |
643 						     BTUSB_WIDEBAND_SPEECH },
644 	{ USB_DEVICE(0x0bda, 0xd923), .driver_info = BTUSB_REALTEK |
645 						     BTUSB_WIDEBAND_SPEECH },
646 	{ USB_DEVICE(0x13d3, 0x3617), .driver_info = BTUSB_REALTEK |
647 						     BTUSB_WIDEBAND_SPEECH },
648 	{ USB_DEVICE(0x13d3, 0x3616), .driver_info = BTUSB_REALTEK |
649 						     BTUSB_WIDEBAND_SPEECH },
650 	{ USB_DEVICE(0x0489, 0xe130), .driver_info = BTUSB_REALTEK |
651 						     BTUSB_WIDEBAND_SPEECH },
652 
653 	/* Realtek 8761BU Bluetooth devices */
654 	{ USB_DEVICE(0x0bda, 0xa728), .driver_info = BTUSB_REALTEK |
655 						     BTUSB_BROKEN_EXT_SCAN },
656 
657 	/* Realtek Bluetooth devices */
658 	{ USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
659 	  .driver_info = BTUSB_REALTEK },
660 
661 	/* MediaTek Bluetooth devices */
662 	{ USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
663 	  .driver_info = BTUSB_MEDIATEK |
664 			 BTUSB_WIDEBAND_SPEECH },
665 
666 	/* Additional MediaTek MT7615E Bluetooth devices */
667 	{ USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
668 
669 	/* Additional MediaTek MT7663 Bluetooth devices */
670 	{ USB_DEVICE(0x043e, 0x310c), .driver_info = BTUSB_MEDIATEK |
671 						     BTUSB_WIDEBAND_SPEECH },
672 	{ USB_DEVICE(0x04ca, 0x3801), .driver_info = BTUSB_MEDIATEK |
673 						     BTUSB_WIDEBAND_SPEECH },
674 
675 	/* Additional MediaTek MT7668 Bluetooth devices */
676 	{ USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
677 						     BTUSB_WIDEBAND_SPEECH },
678 
679 	/* Additional MediaTek MT7920 Bluetooth devices */
680 	{ USB_DEVICE(0x0489, 0xe134), .driver_info = BTUSB_MEDIATEK |
681 						     BTUSB_WIDEBAND_SPEECH },
682 	{ USB_DEVICE(0x0489, 0xe135), .driver_info = BTUSB_MEDIATEK |
683 						     BTUSB_WIDEBAND_SPEECH },
684 	{ USB_DEVICE(0x13d3, 0x3620), .driver_info = BTUSB_MEDIATEK |
685 						     BTUSB_WIDEBAND_SPEECH },
686 	{ USB_DEVICE(0x13d3, 0x3621), .driver_info = BTUSB_MEDIATEK |
687 						     BTUSB_WIDEBAND_SPEECH },
688 	{ USB_DEVICE(0x13d3, 0x3622), .driver_info = BTUSB_MEDIATEK |
689 						     BTUSB_WIDEBAND_SPEECH },
690 	{ USB_DEVICE(0x0489, 0xe158), .driver_info = BTUSB_MEDIATEK |
691 						     BTUSB_WIDEBAND_SPEECH },
692 
693 	/* Additional MediaTek MT7921 Bluetooth devices */
694 	{ USB_DEVICE(0x0489, 0xe0c8), .driver_info = BTUSB_MEDIATEK |
695 						     BTUSB_WIDEBAND_SPEECH },
696 	{ USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
697 						     BTUSB_WIDEBAND_SPEECH },
698 	{ USB_DEVICE(0x0489, 0xe0e0), .driver_info = BTUSB_MEDIATEK |
699 						     BTUSB_WIDEBAND_SPEECH },
700 	{ USB_DEVICE(0x0489, 0xe0f2), .driver_info = BTUSB_MEDIATEK |
701 						     BTUSB_WIDEBAND_SPEECH },
702 	{ USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
703 						     BTUSB_WIDEBAND_SPEECH },
704 	{ USB_DEVICE(0x0e8d, 0x0608), .driver_info = BTUSB_MEDIATEK |
705 						     BTUSB_WIDEBAND_SPEECH },
706 	{ USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
707 						     BTUSB_WIDEBAND_SPEECH },
708 	{ USB_DEVICE(0x13d3, 0x3564), .driver_info = BTUSB_MEDIATEK |
709 						     BTUSB_WIDEBAND_SPEECH },
710 	{ USB_DEVICE(0x13d3, 0x3567), .driver_info = BTUSB_MEDIATEK |
711 						     BTUSB_WIDEBAND_SPEECH },
712 	{ USB_DEVICE(0x13d3, 0x3576), .driver_info = BTUSB_MEDIATEK |
713 						     BTUSB_WIDEBAND_SPEECH },
714 	{ USB_DEVICE(0x13d3, 0x3578), .driver_info = BTUSB_MEDIATEK |
715 						     BTUSB_WIDEBAND_SPEECH },
716 	{ USB_DEVICE(0x13d3, 0x3583), .driver_info = BTUSB_MEDIATEK |
717 						     BTUSB_WIDEBAND_SPEECH },
718 	{ USB_DEVICE(0x13d3, 0x3606), .driver_info = BTUSB_MEDIATEK |
719 						     BTUSB_WIDEBAND_SPEECH },
720 	/* MediaTek MT7902 Bluetooth devices */
721 	{ USB_DEVICE(0x0489, 0xe156), .driver_info = BTUSB_MEDIATEK |
722 						     BTUSB_WIDEBAND_SPEECH },
723 	{ USB_DEVICE(0x0e8d, 0x1ede), .driver_info = BTUSB_MEDIATEK |
724 						     BTUSB_WIDEBAND_SPEECH },
725 	{ USB_DEVICE(0x13d3, 0x3579), .driver_info = BTUSB_MEDIATEK |
726 						     BTUSB_WIDEBAND_SPEECH },
727 	{ USB_DEVICE(0x13d3, 0x3580), .driver_info = BTUSB_MEDIATEK |
728 						     BTUSB_WIDEBAND_SPEECH },
729 	{ USB_DEVICE(0x13d3, 0x3594), .driver_info = BTUSB_MEDIATEK |
730 						     BTUSB_WIDEBAND_SPEECH },
731 	{ USB_DEVICE(0x13d3, 0x3596), .driver_info = BTUSB_MEDIATEK |
732 						     BTUSB_WIDEBAND_SPEECH },
733 	/* MediaTek MT7922 Bluetooth devices */
734 	{ USB_DEVICE(0x13d3, 0x3585), .driver_info = BTUSB_MEDIATEK |
735 						     BTUSB_WIDEBAND_SPEECH },
736 	{ USB_DEVICE(0x13d3, 0x3610), .driver_info = BTUSB_MEDIATEK |
737 						     BTUSB_WIDEBAND_SPEECH },
738 
739 	/* MediaTek MT7922A Bluetooth devices */
740 	{ USB_DEVICE(0x0489, 0xe0d8), .driver_info = BTUSB_MEDIATEK |
741 						     BTUSB_WIDEBAND_SPEECH },
742 	{ USB_DEVICE(0x0489, 0xe0d9), .driver_info = BTUSB_MEDIATEK |
743 						     BTUSB_WIDEBAND_SPEECH },
744 	{ USB_DEVICE(0x0489, 0xe0e2), .driver_info = BTUSB_MEDIATEK |
745 						     BTUSB_WIDEBAND_SPEECH },
746 	{ USB_DEVICE(0x0489, 0xe0e4), .driver_info = BTUSB_MEDIATEK |
747 						     BTUSB_WIDEBAND_SPEECH },
748 	{ USB_DEVICE(0x0489, 0xe0f1), .driver_info = BTUSB_MEDIATEK |
749 						     BTUSB_WIDEBAND_SPEECH },
750 	{ USB_DEVICE(0x0489, 0xe0f2), .driver_info = BTUSB_MEDIATEK |
751 						     BTUSB_WIDEBAND_SPEECH },
752 	{ USB_DEVICE(0x0489, 0xe0f5), .driver_info = BTUSB_MEDIATEK |
753 						     BTUSB_WIDEBAND_SPEECH },
754 	{ USB_DEVICE(0x0489, 0xe0f6), .driver_info = BTUSB_MEDIATEK |
755 						     BTUSB_WIDEBAND_SPEECH },
756 	{ USB_DEVICE(0x0489, 0xe102), .driver_info = BTUSB_MEDIATEK |
757 						     BTUSB_WIDEBAND_SPEECH },
758 	{ USB_DEVICE(0x0489, 0xe11d), .driver_info = BTUSB_MEDIATEK |
759 						     BTUSB_WIDEBAND_SPEECH },
760 	{ USB_DEVICE(0x0489, 0xe152), .driver_info = BTUSB_MEDIATEK |
761 						     BTUSB_WIDEBAND_SPEECH },
762 	{ USB_DEVICE(0x0489, 0xe153), .driver_info = BTUSB_MEDIATEK |
763 						     BTUSB_WIDEBAND_SPEECH },
764 	{ USB_DEVICE(0x0489, 0xe170), .driver_info = BTUSB_MEDIATEK |
765 						     BTUSB_WIDEBAND_SPEECH },
766 	{ USB_DEVICE(0x0489, 0xe174), .driver_info = BTUSB_MEDIATEK |
767 						     BTUSB_WIDEBAND_SPEECH },
768 	{ USB_DEVICE(0x04ca, 0x3804), .driver_info = BTUSB_MEDIATEK |
769 						     BTUSB_WIDEBAND_SPEECH },
770 	{ USB_DEVICE(0x04ca, 0x3807), .driver_info = BTUSB_MEDIATEK |
771 						     BTUSB_WIDEBAND_SPEECH },
772 	{ USB_DEVICE(0x04ca, 0x38e4), .driver_info = BTUSB_MEDIATEK |
773 						     BTUSB_WIDEBAND_SPEECH },
774 	{ USB_DEVICE(0x0e8d, 0x223c), .driver_info = BTUSB_MEDIATEK |
775 						     BTUSB_WIDEBAND_SPEECH },
776 	{ USB_DEVICE(0x13d3, 0x3568), .driver_info = BTUSB_MEDIATEK |
777 						     BTUSB_WIDEBAND_SPEECH },
778 	{ USB_DEVICE(0x13d3, 0x3584), .driver_info = BTUSB_MEDIATEK |
779 						     BTUSB_WIDEBAND_SPEECH },
780 	{ USB_DEVICE(0x13d3, 0x3605), .driver_info = BTUSB_MEDIATEK |
781 						     BTUSB_WIDEBAND_SPEECH },
782 	{ USB_DEVICE(0x13d3, 0x3607), .driver_info = BTUSB_MEDIATEK |
783 						     BTUSB_WIDEBAND_SPEECH },
784 	{ USB_DEVICE(0x13d3, 0x3614), .driver_info = BTUSB_MEDIATEK |
785 						     BTUSB_WIDEBAND_SPEECH },
786 	{ USB_DEVICE(0x13d3, 0x3615), .driver_info = BTUSB_MEDIATEK |
787 						     BTUSB_WIDEBAND_SPEECH },
788 	{ USB_DEVICE(0x13d3, 0x3633), .driver_info = BTUSB_MEDIATEK |
789 						     BTUSB_WIDEBAND_SPEECH },
790 	{ USB_DEVICE(0x35f5, 0x7922), .driver_info = BTUSB_MEDIATEK |
791 						     BTUSB_WIDEBAND_SPEECH },
792 
793 	/* Additional MediaTek MT7925 Bluetooth devices */
794 	{ USB_DEVICE(0x0489, 0xe111), .driver_info = BTUSB_MEDIATEK |
795 						     BTUSB_WIDEBAND_SPEECH },
796 	{ USB_DEVICE(0x0489, 0xe113), .driver_info = BTUSB_MEDIATEK |
797 						     BTUSB_WIDEBAND_SPEECH },
798 	{ USB_DEVICE(0x0489, 0xe118), .driver_info = BTUSB_MEDIATEK |
799 						     BTUSB_WIDEBAND_SPEECH },
800 	{ USB_DEVICE(0x0489, 0xe11e), .driver_info = BTUSB_MEDIATEK |
801 						     BTUSB_WIDEBAND_SPEECH },
802 	{ USB_DEVICE(0x0489, 0xe124), .driver_info = BTUSB_MEDIATEK |
803 						     BTUSB_WIDEBAND_SPEECH },
804 	{ USB_DEVICE(0x0489, 0xe139), .driver_info = BTUSB_MEDIATEK |
805 						     BTUSB_WIDEBAND_SPEECH },
806 	{ USB_DEVICE(0x0489, 0xe13a), .driver_info = BTUSB_MEDIATEK |
807 						     BTUSB_WIDEBAND_SPEECH },
808 	{ USB_DEVICE(0x0489, 0xe0fa), .driver_info = BTUSB_MEDIATEK |
809 						     BTUSB_WIDEBAND_SPEECH },
810 	{ USB_DEVICE(0x0489, 0xe10f), .driver_info = BTUSB_MEDIATEK |
811 						     BTUSB_WIDEBAND_SPEECH },
812 	{ USB_DEVICE(0x0489, 0xe110), .driver_info = BTUSB_MEDIATEK |
813 						     BTUSB_WIDEBAND_SPEECH },
814 	{ USB_DEVICE(0x0489, 0xe116), .driver_info = BTUSB_MEDIATEK |
815 						     BTUSB_WIDEBAND_SPEECH },
816 	{ USB_DEVICE(0x13d3, 0x3588), .driver_info = BTUSB_MEDIATEK |
817 						     BTUSB_WIDEBAND_SPEECH },
818 	{ USB_DEVICE(0x0489, 0xe14e), .driver_info = BTUSB_MEDIATEK |
819 						     BTUSB_WIDEBAND_SPEECH },
820 	{ USB_DEVICE(0x0489, 0xe14f), .driver_info = BTUSB_MEDIATEK |
821 						     BTUSB_WIDEBAND_SPEECH },
822 	{ USB_DEVICE(0x0489, 0xe150), .driver_info = BTUSB_MEDIATEK |
823 						     BTUSB_WIDEBAND_SPEECH },
824 	{ USB_DEVICE(0x0489, 0xe151), .driver_info = BTUSB_MEDIATEK |
825 						     BTUSB_WIDEBAND_SPEECH },
826 	{ USB_DEVICE(0x0e8d, 0x8c38), .driver_info = BTUSB_MEDIATEK |
827 						     BTUSB_WIDEBAND_SPEECH },
828 	{ USB_DEVICE(0x13d3, 0x3602), .driver_info = BTUSB_MEDIATEK |
829 						     BTUSB_WIDEBAND_SPEECH },
830 	{ USB_DEVICE(0x13d3, 0x3603), .driver_info = BTUSB_MEDIATEK |
831 						     BTUSB_WIDEBAND_SPEECH },
832 	{ USB_DEVICE(0x13d3, 0x3604), .driver_info = BTUSB_MEDIATEK |
833 						     BTUSB_WIDEBAND_SPEECH },
834 	{ USB_DEVICE(0x13d3, 0x3608), .driver_info = BTUSB_MEDIATEK |
835 						     BTUSB_WIDEBAND_SPEECH },
836 	{ USB_DEVICE(0x13d3, 0x3609), .driver_info = BTUSB_MEDIATEK |
837 						     BTUSB_WIDEBAND_SPEECH },
838 	{ USB_DEVICE(0x13d3, 0x3613), .driver_info = BTUSB_MEDIATEK |
839 						     BTUSB_WIDEBAND_SPEECH },
840 	{ USB_DEVICE(0x13d3, 0x3625), .driver_info = BTUSB_MEDIATEK |
841 						     BTUSB_WIDEBAND_SPEECH },
842 	{ USB_DEVICE(0x13d3, 0x3627), .driver_info = BTUSB_MEDIATEK |
843 						     BTUSB_WIDEBAND_SPEECH },
844 	{ USB_DEVICE(0x13d3, 0x3628), .driver_info = BTUSB_MEDIATEK |
845 						     BTUSB_WIDEBAND_SPEECH },
846 	{ USB_DEVICE(0x13d3, 0x3630), .driver_info = BTUSB_MEDIATEK |
847 						     BTUSB_WIDEBAND_SPEECH },
848 	{ USB_DEVICE(0x13d3, 0x3631), .driver_info = BTUSB_MEDIATEK |
849 						     BTUSB_WIDEBAND_SPEECH },
850 	{ USB_DEVICE(0x2c7c, 0x7009), .driver_info = BTUSB_MEDIATEK |
851 						     BTUSB_WIDEBAND_SPEECH },
852 
853 	/* Additional Realtek 8723AE Bluetooth devices */
854 	{ USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
855 	{ USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
856 
857 	/* Additional Realtek 8723BE Bluetooth devices */
858 	{ USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
859 	{ USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
860 	{ USB_DEVICE(0x04f2, 0xb49f), .driver_info = BTUSB_REALTEK },
861 	{ USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
862 	{ USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
863 	{ USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
864 	{ USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
865 
866 	/* Additional Realtek 8723BU Bluetooth devices */
867 	{ USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
868 	{ USB_DEVICE(0x2c0a, 0x8761), .driver_info = BTUSB_REALTEK },
869 
870 	/* Additional Realtek 8723DE Bluetooth devices */
871 	{ USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
872 	{ USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
873 
874 	/* Additional Realtek 8761BUV Bluetooth devices */
875 	{ USB_DEVICE(0x2c4e, 0x0115), .driver_info = BTUSB_REALTEK |
876 						     BTUSB_WIDEBAND_SPEECH },
877 	{ USB_DEVICE(0x2357, 0x0604), .driver_info = BTUSB_REALTEK |
878 						     BTUSB_WIDEBAND_SPEECH },
879 	{ USB_DEVICE(0x2357, 0x0607), .driver_info = BTUSB_REALTEK |
880 						     BTUSB_WIDEBAND_SPEECH },
881 	{ USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
882 						     BTUSB_WIDEBAND_SPEECH },
883 	{ USB_DEVICE(0x2550, 0x8761), .driver_info = BTUSB_REALTEK |
884 						     BTUSB_WIDEBAND_SPEECH },
885 	{ USB_DEVICE(0x0bda, 0x8771), .driver_info = BTUSB_REALTEK |
886 						     BTUSB_WIDEBAND_SPEECH },
887 	{ USB_DEVICE(0x6655, 0x8771), .driver_info = BTUSB_REALTEK |
888 						     BTUSB_WIDEBAND_SPEECH },
889 	{ USB_DEVICE(0x7392, 0xc611), .driver_info = BTUSB_REALTEK |
890 						     BTUSB_WIDEBAND_SPEECH },
891 	{ USB_DEVICE(0x2b89, 0x8761), .driver_info = BTUSB_REALTEK |
892 						     BTUSB_WIDEBAND_SPEECH },
893 	{ USB_DEVICE(0x2b89, 0x6275), .driver_info = BTUSB_REALTEK |
894 						     BTUSB_WIDEBAND_SPEECH },
895 	{ USB_DEVICE(0x37ad, 0x0600), .driver_info = BTUSB_REALTEK |
896 						     BTUSB_WIDEBAND_SPEECH },
897 	{ USB_DEVICE(0x14b0, 0x047a), .driver_info = BTUSB_REALTEK |
898 						     BTUSB_WIDEBAND_SPEECH },
899 
900 	/* Additional Realtek 8761CU Bluetooth devices */
901 	{ USB_DEVICE(0x0b05, 0x1bef), .driver_info = BTUSB_REALTEK |
902 						     BTUSB_WIDEBAND_SPEECH },
903 	{ USB_DEVICE(0x0b05, 0x1d70), .driver_info = BTUSB_REALTEK |
904 						     BTUSB_WIDEBAND_SPEECH },
905 
906 	/* Additional Realtek 8821AE Bluetooth devices */
907 	{ USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
908 	{ USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
909 	{ USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
910 	{ USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
911 	{ USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
912 
913 	/* Additional Realtek 8822BE Bluetooth devices */
914 	{ USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
915 	{ USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
916 
917 	/* Additional Realtek 8822CE Bluetooth devices */
918 	{ USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
919 						     BTUSB_WIDEBAND_SPEECH },
920 	{ USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
921 						     BTUSB_WIDEBAND_SPEECH },
922 	{ USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
923 						     BTUSB_WIDEBAND_SPEECH },
924 	{ USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
925 						     BTUSB_WIDEBAND_SPEECH },
926 	{ USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
927 						     BTUSB_WIDEBAND_SPEECH },
928 	{ USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
929 						     BTUSB_WIDEBAND_SPEECH },
930 	{ USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
931 						     BTUSB_WIDEBAND_SPEECH },
932 	{ USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
933 						     BTUSB_WIDEBAND_SPEECH },
934 	{ USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
935 						     BTUSB_WIDEBAND_SPEECH },
936 	{ USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
937 						     BTUSB_WIDEBAND_SPEECH },
938 	{ USB_DEVICE(0x1357, 0xc123), .driver_info = BTUSB_REALTEK |
939 						     BTUSB_WIDEBAND_SPEECH },
940 	{ USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
941 						     BTUSB_WIDEBAND_SPEECH },
942 
943 	/* Barrot Technology Bluetooth devices */
944 	{ USB_DEVICE(0x33fa, 0x0010), .driver_info = BTUSB_BARROT },
945 	{ USB_DEVICE(0x33fa, 0x0012), .driver_info = BTUSB_BARROT },
946 
947 	/* Actions Semiconductor ATS2851 based devices */
948 	{ USB_DEVICE(0x10d7, 0xb012), .driver_info = BTUSB_ACTIONS_SEMI },
949 
950 	/* Silicon Wave based devices */
951 	{ USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
952 
953 	{ }	/* Terminating entry */
954 };
955 
956 /* The Bluetooth USB module build into some devices needs to be reset on resume,
957  * this is a problem with the platform (likely shutting off all power) not with
958  * the module itself. So we use a DMI list to match known broken platforms.
959  */
960 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
961 	{
962 		/* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
963 		.matches = {
964 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
965 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
966 		},
967 	},
968 	{
969 		/* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
970 		.matches = {
971 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
972 			DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
973 		},
974 	},
975 	{
976 		/* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
977 		.matches = {
978 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
979 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
980 		},
981 	},
982 	{}
983 };
984 
985 struct qca_dump_info {
986 	/* fields for dump collection */
987 	u16 id_vendor;
988 	u16 id_product;
989 	u32 fw_version;
990 	u32 controller_id;
991 	u32 ram_dump_size;
992 	u16 ram_dump_seqno;
993 };
994 
995 struct btqca_data {
996 	struct qca_dump_info qca_dump;
997 };
998 
999 #define BTUSB_MAX_ISOC_FRAMES	10
1000 
1001 #define BTUSB_INTR_RUNNING	0
1002 #define BTUSB_BULK_RUNNING	1
1003 #define BTUSB_ISOC_RUNNING	2
1004 #define BTUSB_SUSPENDING	3
1005 #define BTUSB_DID_ISO_RESUME	4
1006 #define BTUSB_BOOTLOADER	5
1007 #define BTUSB_DOWNLOADING	6
1008 #define BTUSB_FIRMWARE_LOADED	7
1009 #define BTUSB_FIRMWARE_FAILED	8
1010 #define BTUSB_BOOTING		9
1011 #define BTUSB_DIAG_RUNNING	10
1012 #define BTUSB_OOB_WAKE_ENABLED	11
1013 #define BTUSB_HW_RESET_ACTIVE	12
1014 #define BTUSB_TX_WAIT_VND_EVT	13
1015 #define BTUSB_WAKEUP_AUTOSUSPEND	14
1016 #define BTUSB_USE_ALT3_FOR_WBS	15
1017 #define BTUSB_ALT6_CONTINUOUS_TX	16
1018 #define BTUSB_HW_SSR_ACTIVE	17
1019 #define BTUSB_WAKEUP_BROKEN	18
1020 #define BTUSB_RESET		19
1021 
1022 #define BTUSB_PROTO_LEGACY	0x00
1023 #define BTUSB_PROTO_H4		0x01
1024 
1025 struct btusb_data {
1026 	struct hci_dev       *hdev;
1027 	struct usb_device    *udev;
1028 	struct usb_interface *intf;
1029 	struct usb_interface *isoc;
1030 	struct usb_interface *diag;
1031 	unsigned isoc_ifnum;
1032 
1033 	unsigned long flags;
1034 
1035 	bool poll_sync;
1036 	int intr_interval;
1037 	struct work_struct  work;
1038 	struct work_struct  waker;
1039 	struct delayed_work rx_work;
1040 
1041 	struct sk_buff_head acl_q;
1042 
1043 	struct usb_anchor deferred;
1044 	struct usb_anchor tx_anchor;
1045 	int tx_in_flight;
1046 	spinlock_t txlock;
1047 
1048 	struct usb_anchor intr_anchor;
1049 	struct usb_anchor bulk_anchor;
1050 	struct usb_anchor isoc_anchor;
1051 	struct usb_anchor diag_anchor;
1052 	struct usb_anchor ctrl_anchor;
1053 	spinlock_t rxlock;
1054 
1055 	struct sk_buff *evt_skb;
1056 	struct sk_buff *acl_skb;
1057 	struct sk_buff *sco_skb;
1058 	struct sk_buff *rx_skb;
1059 
1060 	struct usb_endpoint_descriptor *intr_ep;
1061 	struct usb_endpoint_descriptor *bulk_tx_ep;
1062 	struct usb_endpoint_descriptor *bulk_rx_ep;
1063 	struct usb_endpoint_descriptor *isoc_tx_ep;
1064 	struct usb_endpoint_descriptor *isoc_rx_ep;
1065 	struct usb_endpoint_descriptor *diag_tx_ep;
1066 	struct usb_endpoint_descriptor *diag_rx_ep;
1067 
1068 	struct gpio_desc *reset_gpio;
1069 
1070 	__u8 cmdreq_type;
1071 	__u8 cmdreq;
1072 	__u8 proto;
1073 
1074 	unsigned int sco_num;
1075 	unsigned int air_mode;
1076 	bool usb_alt6_packet_flow;
1077 	int isoc_altsetting;
1078 	int suspend_count;
1079 	const struct usb_device_id *match_id;
1080 
1081 	int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
1082 	int (*recv_acl)(struct hci_dev *hdev, struct sk_buff *skb);
1083 	int (*recv_intr)(struct btusb_data *data, void *buffer, int count);
1084 	int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
1085 
1086 	int (*setup_on_usb)(struct hci_dev *hdev);
1087 
1088 	int (*suspend)(struct hci_dev *hdev);
1089 	int (*resume)(struct hci_dev *hdev);
1090 	int (*disconnect)(struct hci_dev *hdev);
1091 
1092 	int oob_wake_irq;   /* irq for out-of-band wake-on-bt */
1093 };
1094 
1095 static void btusb_reset(struct hci_dev *hdev)
1096 {
1097 	struct btusb_data *data;
1098 	int err;
1099 
1100 	data = hci_get_drvdata(hdev);
1101 	err = usb_autopm_get_interface(data->intf);
1102 	if (err) {
1103 		bt_dev_err(hdev, "Failed usb_autopm_get_interface: %d", err);
1104 		return;
1105 	}
1106 
1107 	if (test_and_set_bit(BTUSB_RESET, &data->flags))
1108 		usb_autopm_put_interface_no_suspend(data->intf);
1109 
1110 	bt_dev_err(hdev, "Resetting usb device.");
1111 	usb_queue_reset_device(data->intf);
1112 }
1113 
1114 static void btusb_intel_reset(struct hci_dev *hdev)
1115 {
1116 	struct btusb_data *data = hci_get_drvdata(hdev);
1117 	struct gpio_desc *reset_gpio = data->reset_gpio;
1118 	struct btintel_data *intel_data = hci_get_priv(hdev);
1119 
1120 	if (intel_data->acpi_reset_method) {
1121 		if (test_and_set_bit(INTEL_ACPI_RESET_ACTIVE, intel_data->flags)) {
1122 			bt_dev_err(hdev, "acpi: last reset failed ? Not resetting again");
1123 			return;
1124 		}
1125 
1126 		bt_dev_err(hdev, "Initiating acpi reset method");
1127 		/* If ACPI reset method fails, lets try with legacy GPIO
1128 		 * toggling
1129 		 */
1130 		if (!intel_data->acpi_reset_method(hdev)) {
1131 			return;
1132 		}
1133 	}
1134 
1135 	if (!reset_gpio) {
1136 		btusb_reset(hdev);
1137 		return;
1138 	}
1139 
1140 	/*
1141 	 * Toggle the hard reset line if the platform provides one. The reset
1142 	 * is going to yank the device off the USB and then replug. So doing
1143 	 * once is enough. The cleanup is handled correctly on the way out
1144 	 * (standard USB disconnect), and the new device is detected cleanly
1145 	 * and bound to the driver again like it should be.
1146 	 */
1147 	if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
1148 		bt_dev_err(hdev, "last reset failed? Not resetting again");
1149 		return;
1150 	}
1151 
1152 	bt_dev_err(hdev, "Initiating HW reset via gpio");
1153 	gpiod_set_value_cansleep(reset_gpio, 1);
1154 	msleep(100);
1155 	gpiod_set_value_cansleep(reset_gpio, 0);
1156 }
1157 
1158 #define RTK_DEVCOREDUMP_CODE_MEMDUMP		0x01
1159 #define RTK_DEVCOREDUMP_CODE_HW_ERR		0x02
1160 #define RTK_DEVCOREDUMP_CODE_CMD_TIMEOUT	0x03
1161 
1162 #define RTK_SUB_EVENT_CODE_COREDUMP		0x34
1163 
1164 struct rtk_dev_coredump_hdr {
1165 	u8 type;
1166 	u8 code;
1167 	u8 reserved[2];
1168 } __packed;
1169 
1170 static inline void btusb_rtl_alloc_devcoredump(struct hci_dev *hdev,
1171 		struct rtk_dev_coredump_hdr *hdr, u8 *buf, u32 len)
1172 {
1173 	struct sk_buff *skb;
1174 
1175 	skb = alloc_skb(len + sizeof(*hdr), GFP_ATOMIC);
1176 	if (!skb)
1177 		return;
1178 
1179 	skb_put_data(skb, hdr, sizeof(*hdr));
1180 	if (len)
1181 		skb_put_data(skb, buf, len);
1182 
1183 	if (!hci_devcd_init(hdev, skb->len)) {
1184 		hci_devcd_append(hdev, skb);
1185 		hci_devcd_complete(hdev);
1186 	} else {
1187 		bt_dev_err(hdev, "RTL: Failed to generate devcoredump");
1188 		kfree_skb(skb);
1189 	}
1190 }
1191 
1192 static void btusb_rtl_reset(struct hci_dev *hdev)
1193 {
1194 	struct btusb_data *data = hci_get_drvdata(hdev);
1195 	struct gpio_desc *reset_gpio = data->reset_gpio;
1196 	struct rtk_dev_coredump_hdr hdr = {
1197 		.type = RTK_DEVCOREDUMP_CODE_CMD_TIMEOUT,
1198 	};
1199 
1200 	btusb_rtl_alloc_devcoredump(hdev, &hdr, NULL, 0);
1201 
1202 	if (!reset_gpio) {
1203 		btusb_reset(hdev);
1204 		return;
1205 	}
1206 
1207 	/* Toggle the hard reset line. The Realtek device is going to
1208 	 * yank itself off the USB and then replug. The cleanup is handled
1209 	 * correctly on the way out (standard USB disconnect), and the new
1210 	 * device is detected cleanly and bound to the driver again like
1211 	 * it should be.
1212 	 */
1213 	if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
1214 		bt_dev_err(hdev, "last reset failed? Not resetting again");
1215 		return;
1216 	}
1217 
1218 	bt_dev_err(hdev, "Reset Realtek device via gpio");
1219 	gpiod_set_value_cansleep(reset_gpio, 1);
1220 	msleep(200);
1221 	gpiod_set_value_cansleep(reset_gpio, 0);
1222 }
1223 
1224 static void btusb_rtl_hw_error(struct hci_dev *hdev, u8 code)
1225 {
1226 	struct rtk_dev_coredump_hdr hdr = {
1227 		.type = RTK_DEVCOREDUMP_CODE_HW_ERR,
1228 		.code = code,
1229 	};
1230 
1231 	bt_dev_err(hdev, "RTL: hw err, trigger devcoredump (%d)", code);
1232 
1233 	btusb_rtl_alloc_devcoredump(hdev, &hdr, NULL, 0);
1234 }
1235 
1236 static void btusb_qca_reset(struct hci_dev *hdev)
1237 {
1238 	struct btusb_data *data = hci_get_drvdata(hdev);
1239 	struct gpio_desc *reset_gpio = data->reset_gpio;
1240 
1241 	if (test_bit(BTUSB_HW_SSR_ACTIVE, &data->flags)) {
1242 		bt_dev_info(hdev, "Ramdump in progress, defer reset");
1243 		return;
1244 	}
1245 
1246 	if (reset_gpio) {
1247 		bt_dev_err(hdev, "Reset qca device via bt_en gpio");
1248 
1249 		/* Toggle the hard reset line. The qca bt device is going to
1250 		 * yank itself off the USB and then replug. The cleanup is handled
1251 		 * correctly on the way out (standard USB disconnect), and the new
1252 		 * device is detected cleanly and bound to the driver again like
1253 		 * it should be.
1254 		 */
1255 		if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
1256 			bt_dev_err(hdev, "last reset failed? Not resetting again");
1257 			return;
1258 		}
1259 
1260 		gpiod_set_value_cansleep(reset_gpio, 0);
1261 		msleep(200);
1262 		gpiod_set_value_cansleep(reset_gpio, 1);
1263 
1264 		return;
1265 	}
1266 
1267 	btusb_reset(hdev);
1268 }
1269 
1270 static u8 btusb_classify_qca_pkt_type(struct hci_dev *hdev, struct sk_buff *skb)
1271 {
1272 	/* Some Qualcomm controllers, e.g., QCNFA765 with WCN6855 chip, send debug
1273 	 * packets as ACL frames with connection handle 0x2EDC. These are not real
1274 	 * ACL packets and should be reclassified as HCI_DIAG_PKT to prevent
1275 	 * "ACL packet for unknown connection handle 3804" errors.
1276 	 */
1277 	if (skb->len >= 2) {
1278 		u16 handle = get_unaligned_le16(skb->data);
1279 
1280 		if (handle == 0x2EDC)
1281 			return HCI_DIAG_PKT;
1282 	}
1283 
1284 	/* Use default packet type for other packets */
1285 	return hci_skb_pkt_type(skb);
1286 }
1287 
1288 static inline void btusb_free_frags(struct btusb_data *data)
1289 {
1290 	unsigned long flags;
1291 
1292 	spin_lock_irqsave(&data->rxlock, flags);
1293 
1294 	dev_kfree_skb_irq(data->evt_skb);
1295 	data->evt_skb = NULL;
1296 
1297 	dev_kfree_skb_irq(data->acl_skb);
1298 	data->acl_skb = NULL;
1299 
1300 	dev_kfree_skb_irq(data->sco_skb);
1301 	data->sco_skb = NULL;
1302 
1303 	/* rx_skb may hold an ERR_PTR from a previous h4_recv_skb() call */
1304 	if (!IS_ERR(data->rx_skb))
1305 		dev_kfree_skb_irq(data->rx_skb);
1306 	data->rx_skb = NULL;
1307 
1308 	spin_unlock_irqrestore(&data->rxlock, flags);
1309 }
1310 
1311 static int btusb_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
1312 {
1313 	struct btusb_data *data = hci_get_drvdata(hdev);
1314 	int err;
1315 
1316 	err = data->recv_event(hdev, skb);
1317 
1318 	if (data->intr_interval) {
1319 		/* Trigger dequeue immediately if an event is received */
1320 		schedule_delayed_work(&data->rx_work, 0);
1321 	}
1322 
1323 	return err;
1324 }
1325 
1326 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
1327 {
1328 	struct sk_buff *skb;
1329 	unsigned long flags;
1330 	int err = 0;
1331 
1332 	spin_lock_irqsave(&data->rxlock, flags);
1333 	skb = data->evt_skb;
1334 
1335 	while (count) {
1336 		int len;
1337 
1338 		if (!skb) {
1339 			skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
1340 			if (!skb) {
1341 				err = -ENOMEM;
1342 				break;
1343 			}
1344 
1345 			hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
1346 			hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
1347 		}
1348 
1349 		len = min_t(uint, hci_skb_expect(skb), count);
1350 		skb_put_data(skb, buffer, len);
1351 
1352 		count -= len;
1353 		buffer += len;
1354 		hci_skb_expect(skb) -= len;
1355 
1356 		if (skb->len == HCI_EVENT_HDR_SIZE) {
1357 			/* Complete event header */
1358 			hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
1359 
1360 			if (skb_tailroom(skb) < hci_skb_expect(skb)) {
1361 				kfree_skb(skb);
1362 				skb = NULL;
1363 
1364 				err = -EILSEQ;
1365 				break;
1366 			}
1367 		}
1368 
1369 		if (!hci_skb_expect(skb)) {
1370 			/* Each chunk should correspond to at least 1 or more
1371 			 * events so if there are still bytes left that doesn't
1372 			 * constitute a new event this is likely a bug in the
1373 			 * controller.
1374 			 */
1375 			if (count && count < HCI_EVENT_HDR_SIZE) {
1376 				bt_dev_warn(data->hdev,
1377 					"Unexpected continuation: %d bytes",
1378 					count);
1379 				count = 0;
1380 			}
1381 
1382 			/* Complete frame */
1383 			btusb_recv_event(data->hdev, skb);
1384 			skb = NULL;
1385 		}
1386 	}
1387 
1388 	data->evt_skb = skb;
1389 	spin_unlock_irqrestore(&data->rxlock, flags);
1390 
1391 	return err;
1392 }
1393 
1394 static int btusb_recv_acl(struct hci_dev *hdev, struct sk_buff *skb)
1395 {
1396 	struct btusb_data *data = hci_get_drvdata(hdev);
1397 
1398 	/* Only queue ACL packet if intr_interval is set as it means
1399 	 * force_poll_sync has been enabled.
1400 	 */
1401 	if (!data->intr_interval)
1402 		return data->recv_acl(hdev, skb);
1403 
1404 	skb_queue_tail(&data->acl_q, skb);
1405 	schedule_delayed_work(&data->rx_work, data->intr_interval);
1406 
1407 	return 0;
1408 }
1409 
1410 static int btusb_recv_frame(struct hci_dev *hdev, struct sk_buff *skb)
1411 {
1412 	u8 pkt_type = hci_skb_pkt_type(skb);
1413 	int ret;
1414 
1415 	switch (pkt_type) {
1416 	case HCI_EVENT_PKT:
1417 		ret = btusb_recv_event(hdev, skb);
1418 		break;
1419 	case HCI_ACLDATA_PKT:
1420 		ret = btusb_recv_acl(hdev, skb);
1421 		break;
1422 	case HCI_SCODATA_PKT:
1423 	case HCI_ISODATA_PKT:
1424 		ret = hci_recv_frame(hdev, skb);
1425 		break;
1426 	default:
1427 		dev_kfree_skb_irq(skb);
1428 		ret = -EINVAL;
1429 		break;
1430 	}
1431 
1432 	return ret;
1433 }
1434 
1435 /* Dispatch through the btusb_recv_* wrappers so that vendor specific
1436  * handling (data->recv_event, data->recv_acl) is preserved in H:4 mode.
1437  */
1438 static const struct h4_recv_pkt btusb_recv_pkts[] = {
1439 	{ H4_RECV_ACL,          .recv = btusb_recv_acl },
1440 	{ H4_RECV_SCO,          .recv = hci_recv_frame },
1441 	{ H4_RECV_EVENT,        .recv = btusb_recv_event },
1442 	{ H4_RECV_ISO,          .recv = hci_recv_frame },
1443 };
1444 
1445 static int btusb_recv_h4(struct btusb_data *data, void *buffer, int count)
1446 {
1447 	unsigned long flags;
1448 	int err = 0;
1449 
1450 	spin_lock_irqsave(&data->rxlock, flags);
1451 	data->rx_skb = h4_recv_skb(data->hdev, NULL, NULL, data->rx_skb, buffer,
1452 				   count, btusb_recv_pkts,
1453 				   ARRAY_SIZE(btusb_recv_pkts));
1454 	if (IS_ERR(data->rx_skb))
1455 		err = PTR_ERR(data->rx_skb);
1456 	spin_unlock_irqrestore(&data->rxlock, flags);
1457 
1458 	return err;
1459 }
1460 
1461 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
1462 {
1463 	struct sk_buff *skb;
1464 	unsigned long flags;
1465 	int err = 0;
1466 
1467 	if (data->proto == BTUSB_PROTO_H4)
1468 		return btusb_recv_h4(data, buffer, count);
1469 
1470 	spin_lock_irqsave(&data->rxlock, flags);
1471 	skb = data->acl_skb;
1472 
1473 	while (count) {
1474 		int len;
1475 
1476 		if (!skb) {
1477 			skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
1478 			if (!skb) {
1479 				err = -ENOMEM;
1480 				break;
1481 			}
1482 
1483 			hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
1484 			hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
1485 		}
1486 
1487 		len = min_t(uint, hci_skb_expect(skb), count);
1488 		skb_put_data(skb, buffer, len);
1489 
1490 		count -= len;
1491 		buffer += len;
1492 		hci_skb_expect(skb) -= len;
1493 
1494 		if (skb->len == HCI_ACL_HDR_SIZE) {
1495 			/* Complete ACL header */
1496 			hci_skb_expect(skb) = hci_acl_dlen(skb);
1497 
1498 			if (skb_tailroom(skb) < hci_skb_expect(skb)) {
1499 				kfree_skb(skb);
1500 				skb = NULL;
1501 
1502 				err = -EILSEQ;
1503 				break;
1504 			}
1505 		}
1506 
1507 		if (!hci_skb_expect(skb)) {
1508 			/* Complete frame */
1509 			btusb_recv_acl(data->hdev, skb);
1510 			skb = NULL;
1511 		}
1512 	}
1513 
1514 	data->acl_skb = skb;
1515 	spin_unlock_irqrestore(&data->rxlock, flags);
1516 
1517 	return err;
1518 }
1519 
1520 static bool btusb_validate_sco_handle(struct hci_dev *hdev,
1521 				      struct hci_sco_hdr *hdr)
1522 {
1523 	__u16 handle;
1524 
1525 	if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
1526 		// Can't validate, userspace controls everything.
1527 		return true;
1528 
1529 	/*
1530 	 * USB isochronous transfers are not designed to be reliable and may
1531 	 * lose fragments.  When this happens, the next first fragment
1532 	 * encountered might actually be a continuation fragment.
1533 	 * Validate the handle to detect it and drop it, or else the upper
1534 	 * layer will get garbage for a while.
1535 	 */
1536 
1537 	handle = hci_handle(__le16_to_cpu(hdr->handle));
1538 
1539 	switch (hci_conn_lookup_type(hdev, handle)) {
1540 	case SCO_LINK:
1541 	case ESCO_LINK:
1542 		return true;
1543 	default:
1544 		return false;
1545 	}
1546 }
1547 
1548 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
1549 {
1550 	struct sk_buff *skb;
1551 	unsigned long flags;
1552 	int err = 0;
1553 
1554 	spin_lock_irqsave(&data->rxlock, flags);
1555 	skb = data->sco_skb;
1556 
1557 	while (count) {
1558 		int len;
1559 
1560 		if (!skb) {
1561 			skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
1562 			if (!skb) {
1563 				err = -ENOMEM;
1564 				break;
1565 			}
1566 
1567 			hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
1568 			hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
1569 		}
1570 
1571 		len = min_t(uint, hci_skb_expect(skb), count);
1572 		skb_put_data(skb, buffer, len);
1573 
1574 		count -= len;
1575 		buffer += len;
1576 		hci_skb_expect(skb) -= len;
1577 
1578 		if (skb->len == HCI_SCO_HDR_SIZE) {
1579 			/* Complete SCO header */
1580 			struct hci_sco_hdr *hdr = hci_sco_hdr(skb);
1581 
1582 			hci_skb_expect(skb) = hdr->dlen;
1583 
1584 			if (skb_tailroom(skb) < hci_skb_expect(skb) ||
1585 			    !btusb_validate_sco_handle(data->hdev, hdr)) {
1586 				kfree_skb(skb);
1587 				skb = NULL;
1588 
1589 				err = -EILSEQ;
1590 				break;
1591 			}
1592 		}
1593 
1594 		if (!hci_skb_expect(skb)) {
1595 			/* Complete frame */
1596 			hci_recv_frame(data->hdev, skb);
1597 			skb = NULL;
1598 		}
1599 	}
1600 
1601 	data->sco_skb = skb;
1602 	spin_unlock_irqrestore(&data->rxlock, flags);
1603 
1604 	return err;
1605 }
1606 
1607 static void btusb_intr_complete(struct urb *urb)
1608 {
1609 	struct hci_dev *hdev = urb->context;
1610 	struct btusb_data *data = hci_get_drvdata(hdev);
1611 	int err;
1612 
1613 	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1614 	       urb->actual_length);
1615 
1616 	if (!test_bit(HCI_RUNNING, &hdev->flags))
1617 		return;
1618 
1619 	if (urb->status == 0) {
1620 		hdev->stat.byte_rx += urb->actual_length;
1621 
1622 		if (data->recv_intr(data, urb->transfer_buffer,
1623 				    urb->actual_length) < 0) {
1624 			bt_dev_err(hdev, "corrupted event packet");
1625 			hdev->stat.err_rx++;
1626 		}
1627 	} else if (urb->status == -ENOENT) {
1628 		/* Avoid suspend failed when usb_kill_urb */
1629 		return;
1630 	}
1631 
1632 	if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
1633 		return;
1634 
1635 	usb_mark_last_busy(data->udev);
1636 	usb_anchor_urb(urb, &data->intr_anchor);
1637 
1638 	err = usb_submit_urb(urb, GFP_ATOMIC);
1639 	if (err < 0) {
1640 		/* -EPERM: urb is being killed;
1641 		 * -ENODEV: device got disconnected
1642 		 */
1643 		if (err != -EPERM && err != -ENODEV)
1644 			bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1645 				   urb, -err);
1646 		if (err != -EPERM)
1647 			hci_cmd_sync_cancel(hdev, -err);
1648 		usb_unanchor_urb(urb);
1649 	}
1650 }
1651 
1652 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
1653 {
1654 	struct btusb_data *data = hci_get_drvdata(hdev);
1655 	struct urb *urb;
1656 	unsigned char *buf;
1657 	unsigned int pipe;
1658 	int err, size;
1659 
1660 	BT_DBG("%s", hdev->name);
1661 
1662 	if (!data->intr_ep)
1663 		return -ENODEV;
1664 
1665 	urb = usb_alloc_urb(0, mem_flags);
1666 	if (!urb)
1667 		return -ENOMEM;
1668 
1669 	if (le16_to_cpu(data->udev->descriptor.idVendor)  == 0x0a12 &&
1670 	    le16_to_cpu(data->udev->descriptor.idProduct) == 0x0001)
1671 		/* Fake CSR devices don't seem to support sort-transter */
1672 		size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
1673 	else
1674 		/* Use maximum HCI Event size so the USB stack handles
1675 		 * ZPL/short-transfer automatically.
1676 		 */
1677 		size = HCI_MAX_EVENT_SIZE;
1678 
1679 	buf = kmalloc(size, mem_flags);
1680 	if (!buf) {
1681 		usb_free_urb(urb);
1682 		return -ENOMEM;
1683 	}
1684 
1685 	pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
1686 
1687 	usb_fill_int_urb(urb, data->udev, pipe, buf, size,
1688 			 btusb_intr_complete, hdev, data->intr_ep->bInterval);
1689 
1690 	urb->transfer_flags |= URB_FREE_BUFFER;
1691 
1692 	usb_anchor_urb(urb, &data->intr_anchor);
1693 
1694 	err = usb_submit_urb(urb, mem_flags);
1695 	if (err < 0) {
1696 		if (err != -EPERM && err != -ENODEV)
1697 			bt_dev_err(hdev, "urb %p submission failed (%d)",
1698 				   urb, -err);
1699 		if (err != -EPERM)
1700 			hci_cmd_sync_cancel(hdev, -err);
1701 		usb_unanchor_urb(urb);
1702 	}
1703 
1704 	/* Only initialize intr_interval if URB poll sync is enabled */
1705 	if (!data->poll_sync)
1706 		goto done;
1707 
1708 	/* The units are frames (milliseconds) for full and low speed devices,
1709 	 * and microframes (1/8 millisecond) for highspeed and SuperSpeed
1710 	 * devices.
1711 	 *
1712 	 * This is done once on open/resume so it shouldn't change even if
1713 	 * force_poll_sync changes.
1714 	 */
1715 	switch (urb->dev->speed) {
1716 	case USB_SPEED_SUPER_PLUS:
1717 	case USB_SPEED_SUPER:	/* units are 125us */
1718 		data->intr_interval = usecs_to_jiffies(urb->interval * 125);
1719 		break;
1720 	default:
1721 		data->intr_interval = msecs_to_jiffies(urb->interval);
1722 		break;
1723 	}
1724 
1725 done:
1726 	usb_free_urb(urb);
1727 
1728 	return err;
1729 }
1730 
1731 static void btusb_bulk_complete(struct urb *urb)
1732 {
1733 	struct hci_dev *hdev = urb->context;
1734 	struct btusb_data *data = hci_get_drvdata(hdev);
1735 	int err;
1736 
1737 	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1738 	       urb->actual_length);
1739 
1740 	if (!test_bit(HCI_RUNNING, &hdev->flags))
1741 		return;
1742 
1743 	if (urb->status == 0) {
1744 		hdev->stat.byte_rx += urb->actual_length;
1745 
1746 		if (data->recv_bulk(data, urb->transfer_buffer,
1747 				    urb->actual_length) < 0) {
1748 			bt_dev_err(hdev, "corrupted ACL packet");
1749 			hdev->stat.err_rx++;
1750 		}
1751 	} else if (urb->status == -ENOENT) {
1752 		/* Avoid suspend failed when usb_kill_urb */
1753 		return;
1754 	}
1755 
1756 	if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
1757 		return;
1758 
1759 	usb_anchor_urb(urb, &data->bulk_anchor);
1760 	usb_mark_last_busy(data->udev);
1761 
1762 	err = usb_submit_urb(urb, GFP_ATOMIC);
1763 	if (err < 0) {
1764 		/* -EPERM: urb is being killed;
1765 		 * -ENODEV: device got disconnected
1766 		 */
1767 		if (err != -EPERM && err != -ENODEV)
1768 			bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1769 				   urb, -err);
1770 		usb_unanchor_urb(urb);
1771 	}
1772 }
1773 
1774 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
1775 {
1776 	struct btusb_data *data = hci_get_drvdata(hdev);
1777 	struct urb *urb;
1778 	unsigned char *buf;
1779 	unsigned int pipe;
1780 	int err, size = HCI_MAX_FRAME_SIZE;
1781 
1782 	BT_DBG("%s", hdev->name);
1783 
1784 	if (!data->bulk_rx_ep)
1785 		return -ENODEV;
1786 
1787 	urb = usb_alloc_urb(0, mem_flags);
1788 	if (!urb)
1789 		return -ENOMEM;
1790 
1791 	buf = kmalloc(size, mem_flags);
1792 	if (!buf) {
1793 		usb_free_urb(urb);
1794 		return -ENOMEM;
1795 	}
1796 
1797 	pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1798 
1799 	usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1800 			  btusb_bulk_complete, hdev);
1801 
1802 	urb->transfer_flags |= URB_FREE_BUFFER;
1803 
1804 	usb_mark_last_busy(data->udev);
1805 	usb_anchor_urb(urb, &data->bulk_anchor);
1806 
1807 	err = usb_submit_urb(urb, mem_flags);
1808 	if (err < 0) {
1809 		if (err != -EPERM && err != -ENODEV)
1810 			bt_dev_err(hdev, "urb %p submission failed (%d)",
1811 				   urb, -err);
1812 		usb_unanchor_urb(urb);
1813 	}
1814 
1815 	usb_free_urb(urb);
1816 
1817 	return err;
1818 }
1819 
1820 static void btusb_isoc_complete(struct urb *urb)
1821 {
1822 	struct hci_dev *hdev = urb->context;
1823 	struct btusb_data *data = hci_get_drvdata(hdev);
1824 	int i, err;
1825 
1826 	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1827 	       urb->actual_length);
1828 
1829 	if (!test_bit(HCI_RUNNING, &hdev->flags))
1830 		return;
1831 
1832 	if (urb->status == 0) {
1833 		for (i = 0; i < urb->number_of_packets; i++) {
1834 			unsigned int offset = urb->iso_frame_desc[i].offset;
1835 			unsigned int length = urb->iso_frame_desc[i].actual_length;
1836 
1837 			if (urb->iso_frame_desc[i].status)
1838 				continue;
1839 
1840 			hdev->stat.byte_rx += length;
1841 
1842 			if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1843 					    length) < 0) {
1844 				bt_dev_err(hdev, "corrupted SCO packet");
1845 				hdev->stat.err_rx++;
1846 			}
1847 		}
1848 	} else if (urb->status == -ENOENT) {
1849 		/* Avoid suspend failed when usb_kill_urb */
1850 		return;
1851 	}
1852 
1853 	if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1854 		return;
1855 
1856 	usb_anchor_urb(urb, &data->isoc_anchor);
1857 
1858 	err = usb_submit_urb(urb, GFP_ATOMIC);
1859 	if (err < 0) {
1860 		/* -EPERM: urb is being killed;
1861 		 * -ENODEV: device got disconnected
1862 		 */
1863 		if (err != -EPERM && err != -ENODEV)
1864 			bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1865 				   urb, -err);
1866 		usb_unanchor_urb(urb);
1867 	}
1868 }
1869 
1870 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1871 					       int mtu, struct btusb_data *data)
1872 {
1873 	int i = 0, offset = 0;
1874 	unsigned int interval;
1875 
1876 	BT_DBG("len %d mtu %d", len, mtu);
1877 
1878 	/* For mSBC ALT 6 settings some chips need to transmit the data
1879 	 * continuously without the zero length of USB packets.
1880 	 */
1881 	if (test_bit(BTUSB_ALT6_CONTINUOUS_TX, &data->flags))
1882 		goto ignore_usb_alt6_packet_flow;
1883 
1884 	/* For mSBC ALT 6 setting the host will send the packet at continuous
1885 	 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1886 	 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1887 	 * To maintain the rate we send 63bytes of usb packets alternatively for
1888 	 * 7ms and 8ms to maintain the rate as 7.5ms.
1889 	 */
1890 	if (data->usb_alt6_packet_flow) {
1891 		interval = 7;
1892 		data->usb_alt6_packet_flow = false;
1893 	} else {
1894 		interval = 6;
1895 		data->usb_alt6_packet_flow = true;
1896 	}
1897 
1898 	for (i = 0; i < interval; i++) {
1899 		urb->iso_frame_desc[i].offset = offset;
1900 		urb->iso_frame_desc[i].length = offset;
1901 	}
1902 
1903 ignore_usb_alt6_packet_flow:
1904 	if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1905 		urb->iso_frame_desc[i].offset = offset;
1906 		urb->iso_frame_desc[i].length = len;
1907 		i++;
1908 	}
1909 
1910 	urb->number_of_packets = i;
1911 }
1912 
1913 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1914 {
1915 	int i, offset = 0;
1916 
1917 	BT_DBG("len %d mtu %d", len, mtu);
1918 
1919 	for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1920 					i++, offset += mtu, len -= mtu) {
1921 		urb->iso_frame_desc[i].offset = offset;
1922 		urb->iso_frame_desc[i].length = mtu;
1923 	}
1924 
1925 	if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1926 		urb->iso_frame_desc[i].offset = offset;
1927 		urb->iso_frame_desc[i].length = len;
1928 		i++;
1929 	}
1930 
1931 	urb->number_of_packets = i;
1932 }
1933 
1934 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1935 {
1936 	struct btusb_data *data = hci_get_drvdata(hdev);
1937 	struct urb *urb;
1938 	unsigned char *buf;
1939 	unsigned int pipe;
1940 	int err, size;
1941 
1942 	BT_DBG("%s", hdev->name);
1943 
1944 	if (!data->isoc_rx_ep)
1945 		return -ENODEV;
1946 
1947 	urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1948 	if (!urb)
1949 		return -ENOMEM;
1950 
1951 	size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1952 						BTUSB_MAX_ISOC_FRAMES;
1953 
1954 	buf = kmalloc(size, mem_flags);
1955 	if (!buf) {
1956 		usb_free_urb(urb);
1957 		return -ENOMEM;
1958 	}
1959 
1960 	pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1961 
1962 	usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1963 			 hdev, data->isoc_rx_ep->bInterval);
1964 
1965 	urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1966 
1967 	__fill_isoc_descriptor(urb, size,
1968 			       le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1969 
1970 	usb_anchor_urb(urb, &data->isoc_anchor);
1971 
1972 	err = usb_submit_urb(urb, mem_flags);
1973 	if (err < 0) {
1974 		if (err != -EPERM && err != -ENODEV)
1975 			bt_dev_err(hdev, "urb %p submission failed (%d)",
1976 				   urb, -err);
1977 		usb_unanchor_urb(urb);
1978 	}
1979 
1980 	usb_free_urb(urb);
1981 
1982 	return err;
1983 }
1984 
1985 static void btusb_diag_complete(struct urb *urb)
1986 {
1987 	struct hci_dev *hdev = urb->context;
1988 	struct btusb_data *data = hci_get_drvdata(hdev);
1989 	int err;
1990 
1991 	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1992 	       urb->actual_length);
1993 
1994 	if (urb->status == 0) {
1995 		struct sk_buff *skb;
1996 
1997 		skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1998 		if (skb) {
1999 			skb_put_data(skb, urb->transfer_buffer,
2000 				     urb->actual_length);
2001 			hci_recv_diag(hdev, skb);
2002 		}
2003 	} else if (urb->status == -ENOENT) {
2004 		/* Avoid suspend failed when usb_kill_urb */
2005 		return;
2006 	}
2007 
2008 	if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
2009 		return;
2010 
2011 	usb_anchor_urb(urb, &data->diag_anchor);
2012 	usb_mark_last_busy(data->udev);
2013 
2014 	err = usb_submit_urb(urb, GFP_ATOMIC);
2015 	if (err < 0) {
2016 		/* -EPERM: urb is being killed;
2017 		 * -ENODEV: device got disconnected
2018 		 */
2019 		if (err != -EPERM && err != -ENODEV)
2020 			bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2021 				   urb, -err);
2022 		usb_unanchor_urb(urb);
2023 	}
2024 }
2025 
2026 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
2027 {
2028 	struct btusb_data *data = hci_get_drvdata(hdev);
2029 	struct urb *urb;
2030 	unsigned char *buf;
2031 	unsigned int pipe;
2032 	int err, size = HCI_MAX_FRAME_SIZE;
2033 
2034 	BT_DBG("%s", hdev->name);
2035 
2036 	if (!data->diag_rx_ep)
2037 		return -ENODEV;
2038 
2039 	urb = usb_alloc_urb(0, mem_flags);
2040 	if (!urb)
2041 		return -ENOMEM;
2042 
2043 	buf = kmalloc(size, mem_flags);
2044 	if (!buf) {
2045 		usb_free_urb(urb);
2046 		return -ENOMEM;
2047 	}
2048 
2049 	pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
2050 
2051 	usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
2052 			  btusb_diag_complete, hdev);
2053 
2054 	urb->transfer_flags |= URB_FREE_BUFFER;
2055 
2056 	usb_mark_last_busy(data->udev);
2057 	usb_anchor_urb(urb, &data->diag_anchor);
2058 
2059 	err = usb_submit_urb(urb, mem_flags);
2060 	if (err < 0) {
2061 		if (err != -EPERM && err != -ENODEV)
2062 			bt_dev_err(hdev, "urb %p submission failed (%d)",
2063 				   urb, -err);
2064 		usb_unanchor_urb(urb);
2065 	}
2066 
2067 	usb_free_urb(urb);
2068 
2069 	return err;
2070 }
2071 
2072 static void btusb_tx_complete(struct urb *urb)
2073 {
2074 	struct sk_buff *skb = urb->context;
2075 	struct hci_dev *hdev = (struct hci_dev *)skb->dev;
2076 	struct btusb_data *data = hci_get_drvdata(hdev);
2077 	unsigned long flags;
2078 
2079 	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
2080 	       urb->actual_length);
2081 
2082 	if (!test_bit(HCI_RUNNING, &hdev->flags))
2083 		goto done;
2084 
2085 	if (!urb->status) {
2086 		hdev->stat.byte_tx += urb->transfer_buffer_length;
2087 	} else {
2088 		if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)
2089 			hci_cmd_sync_cancel(hdev, -urb->status);
2090 		hdev->stat.err_tx++;
2091 	}
2092 
2093 done:
2094 	spin_lock_irqsave(&data->txlock, flags);
2095 	data->tx_in_flight--;
2096 	spin_unlock_irqrestore(&data->txlock, flags);
2097 
2098 	kfree(urb->setup_packet);
2099 
2100 	kfree_skb(skb);
2101 }
2102 
2103 static void btusb_isoc_tx_complete(struct urb *urb)
2104 {
2105 	struct sk_buff *skb = urb->context;
2106 	struct hci_dev *hdev = (struct hci_dev *)skb->dev;
2107 
2108 	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
2109 	       urb->actual_length);
2110 
2111 	if (!test_bit(HCI_RUNNING, &hdev->flags))
2112 		goto done;
2113 
2114 	if (!urb->status)
2115 		hdev->stat.byte_tx += urb->transfer_buffer_length;
2116 	else
2117 		hdev->stat.err_tx++;
2118 
2119 done:
2120 	kfree(urb->setup_packet);
2121 
2122 	kfree_skb(skb);
2123 }
2124 
2125 static int btusb_open(struct hci_dev *hdev)
2126 {
2127 	struct btusb_data *data = hci_get_drvdata(hdev);
2128 	int err;
2129 
2130 	BT_DBG("%s", hdev->name);
2131 
2132 	err = usb_autopm_get_interface(data->intf);
2133 	if (err < 0)
2134 		return err;
2135 
2136 	/* Patching USB firmware files prior to starting any URBs of HCI path
2137 	 * It is more safe to use USB bulk channel for downloading USB patch
2138 	 */
2139 	if (data->setup_on_usb) {
2140 		err = data->setup_on_usb(hdev);
2141 		if (err < 0)
2142 			goto setup_fail;
2143 	}
2144 
2145 	data->intf->needs_remote_wakeup = 1;
2146 
2147 	if (data->proto == BTUSB_PROTO_LEGACY) {
2148 		if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
2149 			goto done;
2150 
2151 		err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
2152 		if (err < 0)
2153 			goto failed;
2154 	}
2155 
2156 	err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
2157 	if (err < 0) {
2158 		usb_kill_anchored_urbs(&data->intr_anchor);
2159 		goto failed;
2160 	}
2161 
2162 	set_bit(BTUSB_BULK_RUNNING, &data->flags);
2163 	btusb_submit_bulk_urb(hdev, GFP_KERNEL);
2164 
2165 	if (data->diag) {
2166 		if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
2167 			set_bit(BTUSB_DIAG_RUNNING, &data->flags);
2168 	}
2169 
2170 done:
2171 	usb_autopm_put_interface(data->intf);
2172 	return 0;
2173 
2174 failed:
2175 	clear_bit(BTUSB_INTR_RUNNING, &data->flags);
2176 setup_fail:
2177 	usb_autopm_put_interface(data->intf);
2178 	return err;
2179 }
2180 
2181 static void btusb_stop_traffic(struct btusb_data *data)
2182 {
2183 	usb_kill_anchored_urbs(&data->intr_anchor);
2184 	usb_kill_anchored_urbs(&data->bulk_anchor);
2185 	usb_kill_anchored_urbs(&data->isoc_anchor);
2186 	usb_kill_anchored_urbs(&data->diag_anchor);
2187 	usb_kill_anchored_urbs(&data->ctrl_anchor);
2188 }
2189 
2190 static void btusb_prepare_reset(struct hci_dev *hdev)
2191 {
2192 	struct btusb_data *data = hci_get_drvdata(hdev);
2193 
2194 	btusb_stop_traffic(data);
2195 	usb_kill_anchored_urbs(&data->tx_anchor);
2196 }
2197 
2198 static int btusb_close(struct hci_dev *hdev)
2199 {
2200 	struct btusb_data *data = hci_get_drvdata(hdev);
2201 	int err;
2202 
2203 	BT_DBG("%s", hdev->name);
2204 
2205 	cancel_work_sync(&data->work);
2206 	cancel_work_sync(&data->waker);
2207 
2208 	clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2209 	clear_bit(BTUSB_BULK_RUNNING, &data->flags);
2210 	clear_bit(BTUSB_INTR_RUNNING, &data->flags);
2211 	clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
2212 
2213 	btusb_stop_traffic(data);
2214 
2215 	/* rx_work must only be canceled once the URBs that can rearm it are
2216 	 * gone, and it must be canceled synchronously since btusb_disconnect()
2217 	 * frees the btusb_data it dereferences right after hci_unregister_dev().
2218 	 */
2219 	cancel_delayed_work_sync(&data->rx_work);
2220 
2221 	skb_queue_purge(&data->acl_q);
2222 
2223 	btusb_free_frags(data);
2224 
2225 	err = usb_autopm_get_interface(data->intf);
2226 	if (err < 0)
2227 		goto failed;
2228 
2229 	data->intf->needs_remote_wakeup = 0;
2230 
2231 	/* Enable remote wake up for auto-suspend */
2232 	if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags))
2233 		data->intf->needs_remote_wakeup = 1;
2234 
2235 	usb_autopm_put_interface(data->intf);
2236 
2237 failed:
2238 	usb_scuttle_anchored_urbs(&data->deferred);
2239 	return 0;
2240 }
2241 
2242 static int btusb_flush(struct hci_dev *hdev)
2243 {
2244 	struct btusb_data *data = hci_get_drvdata(hdev);
2245 
2246 	BT_DBG("%s", hdev->name);
2247 
2248 	cancel_delayed_work_sync(&data->rx_work);
2249 
2250 	skb_queue_purge(&data->acl_q);
2251 
2252 	usb_kill_anchored_urbs(&data->tx_anchor);
2253 	btusb_free_frags(data);
2254 
2255 	return 0;
2256 }
2257 
2258 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
2259 {
2260 	struct btusb_data *data = hci_get_drvdata(hdev);
2261 	struct urb *urb;
2262 	unsigned int pipe;
2263 
2264 	if (!data->bulk_tx_ep)
2265 		return ERR_PTR(-ENODEV);
2266 
2267 	if (data->proto == BTUSB_PROTO_H4) {
2268 		/* The frame type is prepended in place, so the buffer must not
2269 		 * be shared with anyone else.
2270 		 */
2271 		if (skb_cow_head(skb, 1))
2272 			return ERR_PTR(-ENOMEM);
2273 	}
2274 
2275 	urb = usb_alloc_urb(0, GFP_KERNEL);
2276 	if (!urb)
2277 		return ERR_PTR(-ENOMEM);
2278 
2279 	pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
2280 
2281 	if (data->proto == BTUSB_PROTO_H4) {
2282 		/* Prepend skb with frame type */
2283 		memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
2284 	}
2285 
2286 	usb_fill_bulk_urb(urb, data->udev, pipe,
2287 			  skb->data, skb->len, btusb_tx_complete, skb);
2288 
2289 	skb->dev = (void *)hdev;
2290 
2291 	return urb;
2292 }
2293 
2294 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
2295 {
2296 	struct btusb_data *data = hci_get_drvdata(hdev);
2297 	struct usb_ctrlrequest *dr;
2298 	struct urb *urb;
2299 	unsigned int pipe;
2300 
2301 	if (data->proto == BTUSB_PROTO_H4)
2302 		return alloc_bulk_urb(hdev, skb);
2303 
2304 	urb = usb_alloc_urb(0, GFP_KERNEL);
2305 	if (!urb)
2306 		return ERR_PTR(-ENOMEM);
2307 
2308 	dr = kmalloc_obj(*dr);
2309 	if (!dr) {
2310 		usb_free_urb(urb);
2311 		return ERR_PTR(-ENOMEM);
2312 	}
2313 
2314 	dr->bRequestType = data->cmdreq_type;
2315 	dr->bRequest     = data->cmdreq;
2316 	dr->wIndex       = 0;
2317 	dr->wValue       = 0;
2318 	dr->wLength      = __cpu_to_le16(skb->len);
2319 
2320 	pipe = usb_sndctrlpipe(data->udev, 0x00);
2321 
2322 	usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2323 			     skb->data, skb->len, btusb_tx_complete, skb);
2324 
2325 	skb->dev = (void *)hdev;
2326 
2327 	return urb;
2328 }
2329 
2330 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
2331 {
2332 	struct btusb_data *data = hci_get_drvdata(hdev);
2333 	struct urb *urb;
2334 	unsigned int pipe;
2335 
2336 	if (data->proto == BTUSB_PROTO_H4)
2337 		return alloc_bulk_urb(hdev, skb);
2338 
2339 	if (!data->isoc_tx_ep)
2340 		return ERR_PTR(-ENODEV);
2341 
2342 	urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
2343 	if (!urb)
2344 		return ERR_PTR(-ENOMEM);
2345 
2346 	pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
2347 
2348 	usb_fill_int_urb(urb, data->udev, pipe,
2349 			 skb->data, skb->len, btusb_isoc_tx_complete,
2350 			 skb, data->isoc_tx_ep->bInterval);
2351 
2352 	urb->transfer_flags  = URB_ISO_ASAP;
2353 
2354 	if (data->isoc_altsetting == 6)
2355 		__fill_isoc_descriptor_msbc(urb, skb->len,
2356 					    le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
2357 					    data);
2358 	else
2359 		__fill_isoc_descriptor(urb, skb->len,
2360 				       le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
2361 	skb->dev = (void *)hdev;
2362 
2363 	return urb;
2364 }
2365 
2366 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
2367 {
2368 	struct btusb_data *data = hci_get_drvdata(hdev);
2369 	int err;
2370 
2371 	usb_anchor_urb(urb, &data->tx_anchor);
2372 
2373 	err = usb_submit_urb(urb, GFP_KERNEL);
2374 	if (err < 0) {
2375 		if (err != -EPERM && err != -ENODEV)
2376 			bt_dev_err(hdev, "urb %p submission failed (%d)",
2377 				   urb, -err);
2378 		kfree(urb->setup_packet);
2379 		usb_unanchor_urb(urb);
2380 	} else {
2381 		usb_mark_last_busy(data->udev);
2382 	}
2383 
2384 	usb_free_urb(urb);
2385 	return err;
2386 }
2387 
2388 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
2389 {
2390 	struct btusb_data *data = hci_get_drvdata(hdev);
2391 	unsigned long flags;
2392 	bool suspending;
2393 
2394 	spin_lock_irqsave(&data->txlock, flags);
2395 	suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
2396 	if (!suspending)
2397 		data->tx_in_flight++;
2398 	spin_unlock_irqrestore(&data->txlock, flags);
2399 
2400 	if (!suspending)
2401 		return submit_tx_urb(hdev, urb);
2402 
2403 	usb_anchor_urb(urb, &data->deferred);
2404 	schedule_work(&data->waker);
2405 
2406 	usb_free_urb(urb);
2407 	return 0;
2408 }
2409 
2410 static int submit_sco_urb(struct hci_dev *hdev, struct urb *urb)
2411 {
2412 	struct btusb_data *data = hci_get_drvdata(hdev);
2413 
2414 	/* In H:4 mode SCO frames are carried over the bulk endpoint and
2415 	 * complete via btusb_tx_complete(), which decrements tx_in_flight, so
2416 	 * they have to be accounted for like any other bulk transfer.
2417 	 * Isochronous transfers use btusb_isoc_tx_complete() instead, which
2418 	 * does not, so they must not be counted.
2419 	 */
2420 	if (data->proto == BTUSB_PROTO_H4)
2421 		return submit_or_queue_tx_urb(hdev, urb);
2422 
2423 	return submit_tx_urb(hdev, urb);
2424 }
2425 
2426 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
2427 {
2428 	struct urb *urb;
2429 
2430 	BT_DBG("%s", hdev->name);
2431 
2432 	switch (hci_skb_pkt_type(skb)) {
2433 	case HCI_COMMAND_PKT:
2434 		urb = alloc_ctrl_urb(hdev, skb);
2435 		if (IS_ERR(urb))
2436 			return PTR_ERR(urb);
2437 
2438 		hdev->stat.cmd_tx++;
2439 		return submit_or_queue_tx_urb(hdev, urb);
2440 
2441 	case HCI_ACLDATA_PKT:
2442 		urb = alloc_bulk_urb(hdev, skb);
2443 		if (IS_ERR(urb))
2444 			return PTR_ERR(urb);
2445 
2446 		hdev->stat.acl_tx++;
2447 		return submit_or_queue_tx_urb(hdev, urb);
2448 
2449 	case HCI_SCODATA_PKT:
2450 		if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
2451 		    hci_conn_num(hdev, SCO_LINK) < 1)
2452 			return -ENODEV;
2453 
2454 		urb = alloc_isoc_urb(hdev, skb);
2455 		if (IS_ERR(urb))
2456 			return PTR_ERR(urb);
2457 
2458 		hdev->stat.sco_tx++;
2459 		return submit_sco_urb(hdev, urb);
2460 
2461 	case HCI_ISODATA_PKT:
2462 		urb = alloc_bulk_urb(hdev, skb);
2463 		if (IS_ERR(urb))
2464 			return PTR_ERR(urb);
2465 
2466 		return submit_or_queue_tx_urb(hdev, urb);
2467 	}
2468 
2469 	return -EILSEQ;
2470 }
2471 
2472 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
2473 {
2474 	struct btusb_data *data = hci_get_drvdata(hdev);
2475 
2476 	BT_DBG("%s evt %d", hdev->name, evt);
2477 
2478 	if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
2479 		data->sco_num = hci_conn_num(hdev, SCO_LINK);
2480 		data->air_mode = evt;
2481 		schedule_work(&data->work);
2482 	}
2483 }
2484 
2485 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
2486 {
2487 	struct btusb_data *data = hci_get_drvdata(hdev);
2488 	struct usb_interface *intf = data->isoc;
2489 	struct usb_endpoint_descriptor *ep_desc;
2490 	int i, err;
2491 
2492 	if (!data->isoc)
2493 		return -ENODEV;
2494 
2495 	err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
2496 	if (err < 0) {
2497 		bt_dev_err(hdev, "setting interface failed (%d)", -err);
2498 		return err;
2499 	}
2500 
2501 	data->isoc_altsetting = altsetting;
2502 
2503 	data->isoc_tx_ep = NULL;
2504 	data->isoc_rx_ep = NULL;
2505 
2506 	for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2507 		ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2508 
2509 		if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
2510 			data->isoc_tx_ep = ep_desc;
2511 			continue;
2512 		}
2513 
2514 		if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
2515 			data->isoc_rx_ep = ep_desc;
2516 			continue;
2517 		}
2518 	}
2519 
2520 	if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
2521 		bt_dev_err(hdev, "invalid SCO descriptors");
2522 		return -ENODEV;
2523 	}
2524 
2525 	return 0;
2526 }
2527 
2528 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
2529 {
2530 	struct btusb_data *data = hci_get_drvdata(hdev);
2531 	int err;
2532 
2533 	if (data->isoc_altsetting != new_alts) {
2534 		unsigned long flags;
2535 
2536 		clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2537 		usb_kill_anchored_urbs(&data->isoc_anchor);
2538 
2539 		/* When isochronous alternate setting needs to be
2540 		 * changed, because SCO connection has been added
2541 		 * or removed, a packet fragment may be left in the
2542 		 * reassembling state. This could lead to wrongly
2543 		 * assembled fragments.
2544 		 *
2545 		 * Clear outstanding fragment when selecting a new
2546 		 * alternate setting.
2547 		 */
2548 		spin_lock_irqsave(&data->rxlock, flags);
2549 		dev_kfree_skb_irq(data->sco_skb);
2550 		data->sco_skb = NULL;
2551 		spin_unlock_irqrestore(&data->rxlock, flags);
2552 
2553 		err = __set_isoc_interface(hdev, new_alts);
2554 		if (err < 0)
2555 			return err;
2556 	}
2557 
2558 	if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2559 		if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
2560 			clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2561 		else
2562 			btusb_submit_isoc_urb(hdev, GFP_KERNEL);
2563 	}
2564 
2565 	return 0;
2566 }
2567 
2568 static struct usb_host_interface *
2569 btusb_find_altsetting(struct usb_interface *intf, int alt)
2570 {
2571 	int i;
2572 
2573 	BT_DBG("Looking for Alt no :%d", alt);
2574 
2575 	if (!intf)
2576 		return NULL;
2577 
2578 	for (i = 0; i < intf->num_altsetting; i++) {
2579 		if (intf->altsetting[i].desc.bAlternateSetting == alt)
2580 			return &intf->altsetting[i];
2581 	}
2582 
2583 	return NULL;
2584 }
2585 
2586 static void btusb_work(struct work_struct *work)
2587 {
2588 	struct btusb_data *data = container_of(work, struct btusb_data, work);
2589 	struct hci_dev *hdev = data->hdev;
2590 	int new_alts = 0;
2591 	int err;
2592 
2593 	/* In H:4 mode SCO/ISO data is carried over the bulk endpoints, so
2594 	 * there is no isochronous interface to resume or to switch alternate
2595 	 * settings on.
2596 	 */
2597 	if (data->proto == BTUSB_PROTO_H4)
2598 		return;
2599 
2600 	if (data->sco_num > 0) {
2601 		if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
2602 			err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
2603 			if (err < 0) {
2604 				clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2605 				usb_kill_anchored_urbs(&data->isoc_anchor);
2606 				return;
2607 			}
2608 
2609 			set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
2610 		}
2611 
2612 		if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
2613 			if (hdev->voice_setting & 0x0020) {
2614 				static const int alts[3] = { 2, 4, 5 };
2615 				unsigned int sco_idx;
2616 
2617 				sco_idx = min_t(unsigned int, data->sco_num - 1,
2618 						ARRAY_SIZE(alts) - 1);
2619 				new_alts = alts[sco_idx];
2620 			} else {
2621 				new_alts = data->sco_num;
2622 			}
2623 		} else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
2624 			/* Bluetooth USB spec recommends alt 6 (63 bytes), but
2625 			 * many adapters do not support it.  Alt 1 appears to
2626 			 * work for all adapters that do not have alt 6, and
2627 			 * which work with WBS at all.  Some devices prefer
2628 			 * alt 3 (HCI payload >= 60 Bytes let air packet
2629 			 * data satisfy 60 bytes), requiring
2630 			 * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
2631 			 * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
2632 			 */
2633 			if (btusb_find_altsetting(data->isoc, 6))
2634 				new_alts = 6;
2635 			else if (btusb_find_altsetting(data->isoc, 3) &&
2636 				 hdev->sco_mtu >= 72 &&
2637 				 test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
2638 				new_alts = 3;
2639 			else
2640 				new_alts = 1;
2641 		}
2642 
2643 		if (btusb_switch_alt_setting(hdev, new_alts) < 0)
2644 			bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
2645 	} else {
2646 		usb_kill_anchored_urbs(&data->isoc_anchor);
2647 
2648 		if (test_and_clear_bit(BTUSB_ISOC_RUNNING, &data->flags))
2649 			__set_isoc_interface(hdev, 0);
2650 
2651 		if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
2652 			usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
2653 	}
2654 }
2655 
2656 static void btusb_waker(struct work_struct *work)
2657 {
2658 	struct btusb_data *data = container_of(work, struct btusb_data, waker);
2659 	int err;
2660 
2661 	err = usb_autopm_get_interface(data->intf);
2662 	if (err < 0)
2663 		return;
2664 
2665 	usb_autopm_put_interface(data->intf);
2666 }
2667 
2668 static void btusb_rx_work(struct work_struct *work)
2669 {
2670 	struct btusb_data *data = container_of(work, struct btusb_data,
2671 					       rx_work.work);
2672 	struct sk_buff *skb;
2673 
2674 	/* Dequeue ACL data received during the interval */
2675 	while ((skb = skb_dequeue(&data->acl_q)))
2676 		data->recv_acl(data->hdev, skb);
2677 }
2678 
2679 static int btusb_setup_bcm92035(struct hci_dev *hdev)
2680 {
2681 	struct sk_buff *skb;
2682 	u8 val = 0x00;
2683 
2684 	BT_DBG("%s", hdev->name);
2685 
2686 	skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
2687 	if (IS_ERR(skb))
2688 		bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
2689 	else
2690 		kfree_skb(skb);
2691 
2692 	return 0;
2693 }
2694 
2695 static int btusb_setup_csr(struct hci_dev *hdev)
2696 {
2697 	struct btusb_data *data = hci_get_drvdata(hdev);
2698 	u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
2699 	struct hci_rp_read_local_version *rp;
2700 	struct sk_buff *skb;
2701 	bool is_fake = false;
2702 	int ret;
2703 
2704 	BT_DBG("%s", hdev->name);
2705 
2706 	skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2707 			     HCI_INIT_TIMEOUT);
2708 	if (IS_ERR(skb)) {
2709 		int err = PTR_ERR(skb);
2710 
2711 		bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
2712 		return err;
2713 	}
2714 
2715 	rp = skb_pull_data(skb, sizeof(*rp));
2716 	if (!rp) {
2717 		bt_dev_err(hdev, "CSR: Local version length mismatch");
2718 		kfree_skb(skb);
2719 		return -EIO;
2720 	}
2721 
2722 	bt_dev_info(hdev, "CSR: Setting up dongle with HCI ver=%u rev=%04x",
2723 		    rp->hci_ver, le16_to_cpu(rp->hci_rev));
2724 
2725 	bt_dev_info(hdev, "LMP ver=%u subver=%04x; manufacturer=%u",
2726 		    rp->lmp_ver, le16_to_cpu(rp->lmp_subver),
2727 		    le16_to_cpu(rp->manufacturer));
2728 
2729 	/* Detect a wide host of Chinese controllers that aren't CSR.
2730 	 *
2731 	 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
2732 	 *
2733 	 * The main thing they have in common is that these are really popular low-cost
2734 	 * options that support newer Bluetooth versions but rely on heavy VID/PID
2735 	 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
2736 	 *
2737 	 * We detect actual CSR devices by checking that the HCI manufacturer code
2738 	 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
2739 	 * HCI rev values always match. As they both store the firmware number.
2740 	 */
2741 	if (le16_to_cpu(rp->manufacturer) != 10 ||
2742 	    le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
2743 		is_fake = true;
2744 
2745 	/* Known legit CSR firmware build numbers and their supported BT versions:
2746 	 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
2747 	 * - 1.2 (0x2) ->                 0x04d9, 0x0529
2748 	 * - 2.0 (0x3) ->         0x07a6, 0x07ad, 0x0c5c
2749 	 * - 2.1 (0x4) ->         0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
2750 	 * - 4.0 (0x6) ->         0x1d86, 0x2031, 0x22bb
2751 	 *
2752 	 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
2753 	 *      support BT 1.1 only; so it's a dead giveaway when some
2754 	 *      third-party BT 4.0 dongle reuses it.
2755 	 */
2756 	else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
2757 		 rp->hci_ver > BLUETOOTH_VER_1_1)
2758 		is_fake = true;
2759 
2760 	else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
2761 		 rp->hci_ver > BLUETOOTH_VER_1_2)
2762 		is_fake = true;
2763 
2764 	else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
2765 		 rp->hci_ver > BLUETOOTH_VER_2_0)
2766 		is_fake = true;
2767 
2768 	else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
2769 		 rp->hci_ver > BLUETOOTH_VER_2_1)
2770 		is_fake = true;
2771 
2772 	else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
2773 		 rp->hci_ver > BLUETOOTH_VER_4_0)
2774 		is_fake = true;
2775 
2776 	/* Other clones which beat all the above checks */
2777 	else if (bcdDevice == 0x0134 &&
2778 		 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
2779 		 rp->hci_ver == BLUETOOTH_VER_2_0)
2780 		is_fake = true;
2781 
2782 	if (is_fake) {
2783 		bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
2784 
2785 		/* Generally these clones have big discrepancies between
2786 		 * advertised features and what's actually supported.
2787 		 * Probably will need to be expanded in the future;
2788 		 * without these the controller will lock up.
2789 		 */
2790 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_STORED_LINK_KEY);
2791 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_ERR_DATA_REPORTING);
2792 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL);
2793 		hci_set_quirk(hdev, HCI_QUIRK_NO_SUSPEND_NOTIFIER);
2794 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_READ_VOICE_SETTING);
2795 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_READ_PAGE_SCAN_TYPE);
2796 
2797 		/* Clear the reset quirk since this is not an actual
2798 		 * early Bluetooth 1.1 device from CSR.
2799 		 */
2800 		hci_clear_quirk(hdev, HCI_QUIRK_RESET_ON_CLOSE);
2801 		hci_clear_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
2802 
2803 		/*
2804 		 * Special workaround for these BT 4.0 chip clones, and potentially more:
2805 		 *
2806 		 * - 0x0134: a Barrot 8041a02                 (HCI rev: 0x0810 sub: 0x1012)
2807 		 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
2808 		 *
2809 		 * These controllers are really messed-up.
2810 		 *
2811 		 * 1. Their bulk RX endpoint will never report any data unless
2812 		 *    the device was suspended at least once (yes, really).
2813 		 * 2. They will not wakeup when autosuspended and receiving data
2814 		 *    on their bulk RX endpoint from e.g. a keyboard or mouse
2815 		 *    (IOW remote-wakeup support is broken for the bulk endpoint).
2816 		 *
2817 		 * To fix 1. enable runtime-suspend, force-suspend the
2818 		 * HCI and then wake-it up by disabling runtime-suspend.
2819 		 *
2820 		 * To fix 2. clear the HCI's can_wake flag, this way the HCI
2821 		 * will still be autosuspended when it is not open.
2822 		 *
2823 		 * --
2824 		 *
2825 		 * Because these are widespread problems we prefer generic solutions; so
2826 		 * apply this initialization quirk to every controller that gets here,
2827 		 * it should be harmless. The alternative is to not work at all.
2828 		 */
2829 		pm_runtime_allow(&data->udev->dev);
2830 
2831 		ret = pm_runtime_suspend(&data->udev->dev);
2832 		if (ret >= 0)
2833 			msleep(200);
2834 		else
2835 			bt_dev_warn(hdev, "CSR: Couldn't suspend the device for our Barrot 8041a02 receive-issue workaround");
2836 
2837 		pm_runtime_forbid(&data->udev->dev);
2838 
2839 		device_set_wakeup_capable(&data->udev->dev, false);
2840 
2841 		/* Re-enable autosuspend if this was requested */
2842 		if (enable_autosuspend)
2843 			usb_enable_autosuspend(data->udev);
2844 	}
2845 
2846 	kfree_skb(skb);
2847 
2848 	return 0;
2849 }
2850 
2851 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2852 {
2853 	struct sk_buff *skb;
2854 	struct hci_event_hdr *hdr;
2855 	struct hci_ev_cmd_complete *evt;
2856 
2857 	skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2858 	if (!skb)
2859 		return -ENOMEM;
2860 
2861 	hdr = skb_put(skb, sizeof(*hdr));
2862 	hdr->evt = HCI_EV_CMD_COMPLETE;
2863 	hdr->plen = sizeof(*evt) + 1;
2864 
2865 	evt = skb_put(skb, sizeof(*evt));
2866 	evt->ncmd = 0x01;
2867 	evt->opcode = cpu_to_le16(opcode);
2868 
2869 	skb_put_u8(skb, 0x00);
2870 
2871 	hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2872 
2873 	return hci_recv_frame(hdev, skb);
2874 }
2875 
2876 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2877 				 int count)
2878 {
2879 	struct hci_dev *hdev = data->hdev;
2880 
2881 	/* When the device is in bootloader mode, then it can send
2882 	 * events via the bulk endpoint. These events are treated the
2883 	 * same way as the ones received from the interrupt endpoint.
2884 	 *
2885 	 * In H:4 mode there is no interrupt endpoint and every frame on the
2886 	 * bulk endpoint carries an H:4 header, including the ones sent by the
2887 	 * bootloader, so the regular decoding applies.
2888 	 */
2889 	if (data->proto == BTUSB_PROTO_LEGACY &&
2890 	    btintel_test_flag(hdev, INTEL_BOOTLOADER))
2891 		return data->recv_intr(data, buffer, count);
2892 
2893 	return btusb_recv_bulk(data, buffer, count);
2894 }
2895 
2896 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2897 {
2898 	struct urb *urb;
2899 
2900 	BT_DBG("%s", hdev->name);
2901 
2902 	switch (hci_skb_pkt_type(skb)) {
2903 	case HCI_COMMAND_PKT:
2904 		if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2905 			struct hci_command_hdr *cmd = (void *)skb->data;
2906 			__u16 opcode = le16_to_cpu(cmd->opcode);
2907 
2908 			/* When in bootloader mode and the command 0xfc09
2909 			 * is received, it needs to be send down the
2910 			 * bulk endpoint. So allocate a bulk URB instead.
2911 			 */
2912 			if (opcode == 0xfc09)
2913 				urb = alloc_bulk_urb(hdev, skb);
2914 			else
2915 				urb = alloc_ctrl_urb(hdev, skb);
2916 
2917 			/* When the BTINTEL_HCI_OP_RESET command is issued to
2918 			 * boot into the operational firmware, it will actually
2919 			 * not send a command complete event. To keep the flow
2920 			 * control working inject that event here.
2921 			 */
2922 			if (opcode == BTINTEL_HCI_OP_RESET)
2923 				inject_cmd_complete(hdev, opcode);
2924 		} else {
2925 			urb = alloc_ctrl_urb(hdev, skb);
2926 		}
2927 		if (IS_ERR(urb))
2928 			return PTR_ERR(urb);
2929 
2930 		hdev->stat.cmd_tx++;
2931 		return submit_or_queue_tx_urb(hdev, urb);
2932 
2933 	case HCI_ACLDATA_PKT:
2934 		urb = alloc_bulk_urb(hdev, skb);
2935 		if (IS_ERR(urb))
2936 			return PTR_ERR(urb);
2937 
2938 		hdev->stat.acl_tx++;
2939 		return submit_or_queue_tx_urb(hdev, urb);
2940 
2941 	case HCI_SCODATA_PKT:
2942 		if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
2943 		    hci_conn_num(hdev, SCO_LINK) < 1)
2944 			return -ENODEV;
2945 
2946 		urb = alloc_isoc_urb(hdev, skb);
2947 		if (IS_ERR(urb))
2948 			return PTR_ERR(urb);
2949 
2950 		hdev->stat.sco_tx++;
2951 		return submit_sco_urb(hdev, urb);
2952 
2953 	case HCI_ISODATA_PKT:
2954 		urb = alloc_bulk_urb(hdev, skb);
2955 		if (IS_ERR(urb))
2956 			return PTR_ERR(urb);
2957 
2958 		return submit_or_queue_tx_urb(hdev, urb);
2959 	}
2960 
2961 	return -EILSEQ;
2962 }
2963 
2964 static int btusb_setup_realtek(struct hci_dev *hdev)
2965 {
2966 	struct btusb_data *data = hci_get_drvdata(hdev);
2967 	int ret;
2968 
2969 	ret = btrtl_setup_realtek(hdev);
2970 
2971 	if (btrealtek_test_flag(data->hdev, REALTEK_ALT6_CONTINUOUS_TX_CHIP))
2972 		set_bit(BTUSB_ALT6_CONTINUOUS_TX, &data->flags);
2973 
2974 	return ret;
2975 }
2976 
2977 static int btusb_recv_event_realtek(struct hci_dev *hdev, struct sk_buff *skb)
2978 {
2979 	if (skb->len >= HCI_EVENT_HDR_SIZE + 1 &&
2980 	    skb->data[0] == HCI_EV_VENDOR &&
2981 	    skb->data[2] == RTK_SUB_EVENT_CODE_COREDUMP) {
2982 		struct rtk_dev_coredump_hdr hdr = {
2983 			.code = RTK_DEVCOREDUMP_CODE_MEMDUMP,
2984 		};
2985 
2986 		bt_dev_dbg(hdev, "RTL: received coredump vendor evt, len %u",
2987 			skb->len);
2988 
2989 		btusb_rtl_alloc_devcoredump(hdev, &hdr, skb->data, skb->len);
2990 		kfree_skb(skb);
2991 
2992 		return 0;
2993 	}
2994 
2995 	return hci_recv_frame(hdev, skb);
2996 }
2997 
2998 static void btusb_mtk_claim_iso_intf(struct btusb_data *data)
2999 {
3000 	struct btmtk_data *btmtk_data;
3001 	int err;
3002 
3003 	if (!data->hdev)
3004 		return;
3005 
3006 	btmtk_data = hci_get_priv(data->hdev);
3007 	if (!btmtk_data)
3008 		return;
3009 
3010 	if (!btmtk_data->isopkt_intf) {
3011 		bt_dev_err(data->hdev, "Can't claim NULL iso interface");
3012 		return;
3013 	}
3014 
3015 	/*
3016 	 * The function usb_driver_claim_interface() is documented to need
3017 	 * locks held if it's not called from a probe routine. The code here
3018 	 * is called from the hci_power_on workqueue, so grab the lock.
3019 	 */
3020 	device_lock(&btmtk_data->isopkt_intf->dev);
3021 	err = usb_driver_claim_interface(&btusb_driver,
3022 					 btmtk_data->isopkt_intf, data);
3023 	device_unlock(&btmtk_data->isopkt_intf->dev);
3024 	if (err < 0) {
3025 		btmtk_data->isopkt_intf = NULL;
3026 		bt_dev_err(data->hdev, "Failed to claim iso interface: %d", err);
3027 		return;
3028 	}
3029 
3030 	set_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags);
3031 	init_usb_anchor(&btmtk_data->isopkt_anchor);
3032 }
3033 
3034 static void btusb_mtk_release_iso_intf(struct hci_dev *hdev)
3035 {
3036 	struct btmtk_data *btmtk_data;
3037 
3038 	if (!hdev)
3039 		return;
3040 
3041 	btmtk_data = hci_get_priv(hdev);
3042 	if (!btmtk_data)
3043 		return;
3044 
3045 	if (test_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags)) {
3046 		usb_kill_anchored_urbs(&btmtk_data->isopkt_anchor);
3047 		clear_bit(BTMTK_ISOPKT_RUNNING, &btmtk_data->flags);
3048 
3049 		if (btmtk_data->isopkt_skb) {
3050 			dev_kfree_skb_irq(btmtk_data->isopkt_skb);
3051 			btmtk_data->isopkt_skb = NULL;
3052 		}
3053 
3054 		if (btmtk_data->isopkt_intf) {
3055 			usb_set_intfdata(btmtk_data->isopkt_intf, NULL);
3056 			usb_driver_release_interface(&btusb_driver,
3057 						     btmtk_data->isopkt_intf);
3058 			btmtk_data->isopkt_intf = NULL;
3059 		}
3060 	}
3061 
3062 	clear_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags);
3063 }
3064 
3065 static int btusb_mtk_disconnect(struct hci_dev *hdev)
3066 {
3067 	/* This function describes the specific additional steps taken by MediaTek
3068 	 * when Bluetooth usb driver's resume function is called.
3069 	 */
3070 	btusb_mtk_release_iso_intf(hdev);
3071 
3072 	return 0;
3073 }
3074 
3075 static int btusb_mtk_reset(struct hci_dev *hdev, void *rst_data)
3076 {
3077 	struct btusb_data *data = hci_get_drvdata(hdev);
3078 	struct btmtk_data *btmtk_data = hci_get_priv(hdev);
3079 	int err;
3080 
3081 	/* It's MediaTek specific bluetooth reset mechanism via USB */
3082 	if (test_and_set_bit(BTMTK_HW_RESET_ACTIVE, &btmtk_data->flags)) {
3083 		bt_dev_err(hdev, "last reset failed? Not resetting again");
3084 		return -EBUSY;
3085 	}
3086 
3087 	err = usb_autopm_get_interface(data->intf);
3088 	if (err < 0) {
3089 		bt_dev_err(hdev, "Failed usb_autopm_get_interface: %d", err);
3090 		clear_bit(BTMTK_HW_RESET_ACTIVE, &btmtk_data->flags);
3091 		return err;
3092 	}
3093 
3094 	/* Release MediaTek ISO data interface */
3095 	btusb_mtk_release_iso_intf(hdev);
3096 
3097 	btusb_prepare_reset(hdev);
3098 
3099 	/* Toggle the hard reset line. The MediaTek device is going to
3100 	 * yank itself off the USB and then replug. The cleanup is handled
3101 	 * correctly on the way out (standard USB disconnect), and the new
3102 	 * device is detected cleanly and bound to the driver again like
3103 	 * it should be.
3104 	 */
3105 	if (data->reset_gpio) {
3106 		gpiod_set_value_cansleep(data->reset_gpio, 1);
3107 		msleep(200);
3108 		gpiod_set_value_cansleep(data->reset_gpio, 0);
3109 		return 0;
3110 	}
3111 
3112 	err = btmtk_usb_subsys_reset(hdev, btmtk_data->dev_id);
3113 
3114 	if (test_and_set_bit(BTUSB_RESET, &data->flags)) {
3115 		bt_dev_err(hdev, "last usb reset failed? Resetting again");
3116 		usb_autopm_put_interface_no_suspend(data->intf);
3117 	}
3118 
3119 	usb_queue_reset_device(data->intf);
3120 	clear_bit(BTMTK_HW_RESET_ACTIVE, &btmtk_data->flags);
3121 
3122 	return err;
3123 }
3124 
3125 static int btusb_send_frame_mtk(struct hci_dev *hdev, struct sk_buff *skb)
3126 {
3127 	struct urb *urb;
3128 
3129 	BT_DBG("%s", hdev->name);
3130 
3131 	if (hci_skb_pkt_type(skb) == HCI_ISODATA_PKT) {
3132 		urb = alloc_mtk_intr_urb(hdev, skb, btusb_tx_complete);
3133 		if (IS_ERR(urb))
3134 			return PTR_ERR(urb);
3135 
3136 		return submit_or_queue_tx_urb(hdev, urb);
3137 	} else {
3138 		return btusb_send_frame(hdev, skb);
3139 	}
3140 }
3141 
3142 static inline bool platform_is_ryzen(void)
3143 {
3144 #ifdef CONFIG_X86
3145 	return boot_cpu_has(X86_FEATURE_ZEN);
3146 #else
3147 	return false;
3148 #endif
3149 }
3150 
3151 static inline bool is_direct_child_of_root_hub(struct usb_device *udev)
3152 {
3153 	return udev->parent == udev->bus->root_hub;
3154 }
3155 
3156 static int btusb_mtk_setup(struct hci_dev *hdev)
3157 {
3158 	struct btusb_data *data = hci_get_drvdata(hdev);
3159 	struct btmtk_data *btmtk_data = hci_get_priv(hdev);
3160 	int err;
3161 
3162 	/* MediaTek WMT vendor cmd requiring below USB resources to
3163 	 * complete the handshake.
3164 	 */
3165 	btmtk_data->drv_name = btusb_driver.name;
3166 	btmtk_data->intf = data->intf;
3167 	btmtk_data->udev = data->udev;
3168 	btmtk_data->ctrl_anchor = &data->ctrl_anchor;
3169 	btmtk_data->reset_sync = btusb_mtk_reset;
3170 
3171 	/* Claim ISO data interface and endpoint */
3172 	if (!test_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags)) {
3173 		btmtk_data->isopkt_intf = usb_ifnum_to_if(data->udev, MTK_ISO_IFNUM);
3174 		btusb_mtk_claim_iso_intf(data);
3175 	}
3176 
3177 	err = btmtk_usb_setup(hdev);
3178 	if (err)
3179 		return err;
3180 
3181 	switch (btmtk_data->dev_id) {
3182 	case 0x7922:
3183 	case 0x7925:
3184 		/*
3185 		 * All reports seen to be relevant to Ryzen-based laptops. These
3186 		 * NICs are usually used as OEM components thanks to some sort
3187 		 * of reference designs.
3188 		 *
3189 		 * Their popularity on other platforms is unclear. While there
3190 		 * is still a chance that the quirk may exist on other
3191 		 * platforms, be cautious and only apply the quirk to direct
3192 		 * children of Ryzen platforms's root hubs for the time being.
3193 		 *
3194 		 * In most cases the root hub is on the SoC or PCH, which needs
3195 		 * the quirk. Unfortunately, this can't distinguish root hubs on
3196 		 * PCIe add-in cards. Such roughness should be acceptable, as
3197 		 * PCIe USB controller add-in cards are less commonly used
3198 		 * nowadays. On the other hand, applying the quirk doesn't hurt
3199 		 * any functionalities either, as the device can still be used
3200 		 * as a wakeup source if desired.
3201 		 *
3202 		 * Theoretically, we could retrieve the root hub's PCI vendor ID
3203 		 * with some hierarchy magic, but that's too intrusive...
3204 		 */
3205 		if (platform_is_ryzen() && is_direct_child_of_root_hub(data->udev))
3206 			set_bit(BTUSB_WAKEUP_BROKEN, &data->flags);
3207 		break;
3208 	}
3209 
3210 	return 0;
3211 }
3212 
3213 static int btusb_mtk_shutdown(struct hci_dev *hdev)
3214 {
3215 	int ret;
3216 
3217 	ret = btmtk_usb_shutdown(hdev);
3218 
3219 	/* Release MediaTek iso interface after shutdown */
3220 	btusb_mtk_release_iso_intf(hdev);
3221 
3222 	return ret;
3223 }
3224 
3225 #ifdef CONFIG_PM
3226 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3227 static int marvell_config_oob_wake(struct hci_dev *hdev)
3228 {
3229 	struct sk_buff *skb;
3230 	struct btusb_data *data = hci_get_drvdata(hdev);
3231 	struct device *dev = &data->udev->dev;
3232 	u16 pin, gap, opcode;
3233 	int ret;
3234 	u8 cmd[5];
3235 
3236 	/* Move on if no wakeup pin specified */
3237 	if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3238 	    of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3239 		return 0;
3240 
3241 	/* Vendor specific command to configure a GPIO as wake-up pin */
3242 	opcode = hci_opcode_pack(0x3F, 0x59);
3243 	cmd[0] = opcode & 0xFF;
3244 	cmd[1] = opcode >> 8;
3245 	cmd[2] = 2; /* length of parameters that follow */
3246 	cmd[3] = pin;
3247 	cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3248 
3249 	skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3250 	if (!skb) {
3251 		bt_dev_err(hdev, "%s: No memory", __func__);
3252 		return -ENOMEM;
3253 	}
3254 
3255 	skb_put_data(skb, cmd, sizeof(cmd));
3256 	hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3257 
3258 	ret = btusb_send_frame(hdev, skb);
3259 	if (ret) {
3260 		bt_dev_err(hdev, "%s: configuration failed", __func__);
3261 		kfree_skb(skb);
3262 		return ret;
3263 	}
3264 
3265 	return 0;
3266 }
3267 #else
3268 static inline int marvell_config_oob_wake(struct hci_dev *hdev)
3269 {
3270 	return 0;
3271 }
3272 #endif
3273 
3274 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3275 				    const bdaddr_t *bdaddr)
3276 {
3277 	struct sk_buff *skb;
3278 	u8 buf[8];
3279 	long ret;
3280 
3281 	buf[0] = 0xfe;
3282 	buf[1] = sizeof(bdaddr_t);
3283 	memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3284 
3285 	skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3286 	if (IS_ERR(skb)) {
3287 		ret = PTR_ERR(skb);
3288 		bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3289 			   ret);
3290 		return ret;
3291 	}
3292 	kfree_skb(skb);
3293 
3294 	return 0;
3295 }
3296 
3297 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3298 				    const bdaddr_t *bdaddr)
3299 {
3300 	struct sk_buff *skb;
3301 	u8 buf[10];
3302 	long ret;
3303 
3304 	buf[0] = 0x01;
3305 	buf[1] = 0x01;
3306 	buf[2] = 0x00;
3307 	buf[3] = sizeof(bdaddr_t);
3308 	memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3309 
3310 	skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3311 	if (IS_ERR(skb)) {
3312 		ret = PTR_ERR(skb);
3313 		bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3314 		return ret;
3315 	}
3316 	kfree_skb(skb);
3317 
3318 	return 0;
3319 }
3320 
3321 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3322 				const bdaddr_t *bdaddr)
3323 {
3324 	bdaddr_t bdaddr_swapped;
3325 	struct sk_buff *skb;
3326 	long ret;
3327 
3328 	baswap(&bdaddr_swapped, bdaddr);
3329 
3330 	skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(bdaddr_swapped),
3331 				&bdaddr_swapped, HCI_EV_CMD_COMPLETE,
3332 				HCI_INIT_TIMEOUT);
3333 	if (IS_ERR(skb)) {
3334 		ret = PTR_ERR(skb);
3335 		bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3336 		return ret;
3337 	}
3338 	kfree_skb(skb);
3339 
3340 	return 0;
3341 }
3342 
3343 #define QCA_MEMDUMP_ACL_HANDLE 0x2EDD
3344 #define QCA_MEMDUMP_SIZE_MAX  0x100000
3345 #define QCA_MEMDUMP_VSE_CLASS 0x01
3346 #define QCA_MEMDUMP_MSG_TYPE 0x08
3347 #define QCA_MEMDUMP_PKT_SIZE 248
3348 #define QCA_LAST_SEQUENCE_NUM 0xffff
3349 
3350 struct qca_dump_hdr {
3351 	u8 vse_class;
3352 	u8 msg_type;
3353 	__le16 seqno;
3354 	u8 reserved;
3355 	union {
3356 		DECLARE_FLEX_ARRAY(u8, data);
3357 		struct {
3358 			__le32 ram_dump_size;
3359 			DECLARE_FLEX_ARRAY(u8, data0);
3360 		} __packed;
3361 	};
3362 } __packed;
3363 
3364 
3365 static void btusb_dump_hdr_qca(struct hci_dev *hdev, struct sk_buff *skb)
3366 {
3367 	char buf[128];
3368 	struct btqca_data *btqca_data = hci_get_priv(hdev);
3369 	struct qca_dump_info *qca_dump_ptr = &btqca_data->qca_dump;
3370 
3371 	snprintf(buf, sizeof(buf), "Controller Name: 0x%x\n",
3372 			qca_dump_ptr->controller_id);
3373 	skb_put_data(skb, buf, strlen(buf));
3374 
3375 	snprintf(buf, sizeof(buf), "Firmware Version: 0x%x\n",
3376 			qca_dump_ptr->fw_version);
3377 	skb_put_data(skb, buf, strlen(buf));
3378 
3379 	snprintf(buf, sizeof(buf), "Driver: %s\nVendor: qca\n",
3380 			btusb_driver.name);
3381 	skb_put_data(skb, buf, strlen(buf));
3382 
3383 	snprintf(buf, sizeof(buf), "VID: 0x%x\nPID:0x%x\n",
3384 			qca_dump_ptr->id_vendor, qca_dump_ptr->id_product);
3385 	skb_put_data(skb, buf, strlen(buf));
3386 
3387 	snprintf(buf, sizeof(buf), "Lmp Subversion: 0x%x\n",
3388 			hdev->lmp_subver);
3389 	skb_put_data(skb, buf, strlen(buf));
3390 }
3391 
3392 static void btusb_coredump_qca(struct hci_dev *hdev)
3393 {
3394 	int err;
3395 	static const u8 param[] = { 0x26 };
3396 
3397 	err = __hci_cmd_send(hdev, 0xfc0c, 1, param);
3398 	if (err < 0)
3399 		bt_dev_err(hdev, "%s: triggle crash failed (%d)", __func__, err);
3400 }
3401 
3402 /* Return: 0 on success, negative errno on failure. */
3403 static int handle_dump_pkt_qca(struct hci_dev *hdev, struct sk_buff *skb)
3404 {
3405 	int ret = 0;
3406 	unsigned int skip = 0;
3407 	u8 pkt_type;
3408 	u16 seqno;
3409 	u32 dump_size;
3410 
3411 	struct qca_dump_hdr *dump_hdr;
3412 	struct btusb_data *btdata = hci_get_drvdata(hdev);
3413 	struct btqca_data *btqca_data = hci_get_priv(hdev);
3414 	struct qca_dump_info *qca_dump_ptr = &btqca_data->qca_dump;
3415 	struct usb_device *udev = btdata->udev;
3416 
3417 	pkt_type = hci_skb_pkt_type(skb);
3418 	skip = sizeof(struct hci_event_hdr);
3419 	if (pkt_type == HCI_ACLDATA_PKT)
3420 		skip += sizeof(struct hci_acl_hdr);
3421 
3422 	skb_pull(skb, skip);
3423 	dump_hdr = (struct qca_dump_hdr *)skb->data;
3424 
3425 	seqno = le16_to_cpu(dump_hdr->seqno);
3426 	if (seqno == 0) {
3427 		set_bit(BTUSB_HW_SSR_ACTIVE, &btdata->flags);
3428 		dump_size = le32_to_cpu(dump_hdr->ram_dump_size);
3429 		if (!dump_size || (dump_size > QCA_MEMDUMP_SIZE_MAX)) {
3430 			ret = -EILSEQ;
3431 			bt_dev_err(hdev, "Invalid memdump size(%u)",
3432 				   dump_size);
3433 			goto out;
3434 		}
3435 
3436 		ret = hci_devcd_init(hdev, dump_size);
3437 		if (ret < 0) {
3438 			bt_dev_err(hdev, "memdump init error(%d)", ret);
3439 			goto out;
3440 		}
3441 
3442 		qca_dump_ptr->ram_dump_size = dump_size;
3443 		qca_dump_ptr->ram_dump_seqno = 0;
3444 
3445 		skb_pull(skb, offsetof(struct qca_dump_hdr, data0));
3446 
3447 		usb_disable_autosuspend(udev);
3448 		bt_dev_info(hdev, "%s memdump size(%u)\n",
3449 			    (pkt_type == HCI_ACLDATA_PKT) ? "ACL" : "event",
3450 			    dump_size);
3451 	} else {
3452 		skb_pull(skb, offsetof(struct qca_dump_hdr, data));
3453 	}
3454 
3455 	if (!qca_dump_ptr->ram_dump_size) {
3456 		ret = -EINVAL;
3457 		bt_dev_err(hdev, "memdump is not active");
3458 		goto out;
3459 	}
3460 
3461 	if ((seqno > qca_dump_ptr->ram_dump_seqno + 1) && seqno != QCA_LAST_SEQUENCE_NUM) {
3462 		dump_size = QCA_MEMDUMP_PKT_SIZE * (seqno - qca_dump_ptr->ram_dump_seqno - 1);
3463 		hci_devcd_append_pattern(hdev, 0x0, dump_size);
3464 		bt_dev_err(hdev,
3465 			   "expected memdump seqno(%u) is not received(%u)\n",
3466 			   qca_dump_ptr->ram_dump_seqno, seqno);
3467 		qca_dump_ptr->ram_dump_seqno = seqno;
3468 		kfree_skb(skb);
3469 		return ret;
3470 	}
3471 
3472 	hci_devcd_append(hdev, skb);
3473 	qca_dump_ptr->ram_dump_seqno++;
3474 	if (seqno == QCA_LAST_SEQUENCE_NUM) {
3475 		bt_dev_info(hdev,
3476 				"memdump done: pkts(%u), total(%u)\n",
3477 				qca_dump_ptr->ram_dump_seqno, qca_dump_ptr->ram_dump_size);
3478 
3479 		hci_devcd_complete(hdev);
3480 		goto out;
3481 	}
3482 	return ret;
3483 
3484 out:
3485 	if (qca_dump_ptr->ram_dump_size)
3486 		usb_enable_autosuspend(udev);
3487 	qca_dump_ptr->ram_dump_size = 0;
3488 	qca_dump_ptr->ram_dump_seqno = 0;
3489 	clear_bit(BTUSB_HW_SSR_ACTIVE, &btdata->flags);
3490 
3491 	if (ret < 0)
3492 		kfree_skb(skb);
3493 	return ret;
3494 }
3495 
3496 /* Return: true if the ACL packet is a dump packet, false otherwise. */
3497 static bool acl_pkt_is_dump_qca(struct hci_dev *hdev, struct sk_buff *skb)
3498 {
3499 	struct hci_event_hdr *event_hdr;
3500 	struct hci_acl_hdr *acl_hdr;
3501 	struct qca_dump_hdr *dump_hdr;
3502 	struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
3503 	bool is_dump = false;
3504 
3505 	if (!clone)
3506 		return false;
3507 
3508 	acl_hdr = skb_pull_data(clone, sizeof(*acl_hdr));
3509 	if (!acl_hdr || (le16_to_cpu(acl_hdr->handle) != QCA_MEMDUMP_ACL_HANDLE))
3510 		goto out;
3511 
3512 	event_hdr = skb_pull_data(clone, sizeof(*event_hdr));
3513 	if (!event_hdr || event_hdr->evt != HCI_EV_VENDOR)
3514 		goto out;
3515 
3516 	dump_hdr = skb_pull_data(clone, sizeof(*dump_hdr));
3517 	if (!dump_hdr || (dump_hdr->vse_class != QCA_MEMDUMP_VSE_CLASS) ||
3518 	   (dump_hdr->msg_type != QCA_MEMDUMP_MSG_TYPE))
3519 		goto out;
3520 
3521 	is_dump = true;
3522 out:
3523 	consume_skb(clone);
3524 	return is_dump;
3525 }
3526 
3527 /* Return: true if the event packet is a dump packet, false otherwise. */
3528 static bool evt_pkt_is_dump_qca(struct hci_dev *hdev, struct sk_buff *skb)
3529 {
3530 	struct hci_event_hdr *event_hdr;
3531 	struct qca_dump_hdr *dump_hdr;
3532 	struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
3533 	bool is_dump = false;
3534 
3535 	if (!clone)
3536 		return false;
3537 
3538 	event_hdr = skb_pull_data(clone, sizeof(*event_hdr));
3539 	if (!event_hdr || event_hdr->evt != HCI_EV_VENDOR)
3540 		goto out;
3541 
3542 	dump_hdr = skb_pull_data(clone, sizeof(*dump_hdr));
3543 	if (!dump_hdr || (dump_hdr->vse_class != QCA_MEMDUMP_VSE_CLASS) ||
3544 	   (dump_hdr->msg_type != QCA_MEMDUMP_MSG_TYPE))
3545 		goto out;
3546 
3547 	is_dump = true;
3548 out:
3549 	consume_skb(clone);
3550 	return is_dump;
3551 }
3552 
3553 static int btusb_recv_acl_qca(struct hci_dev *hdev, struct sk_buff *skb)
3554 {
3555 	if (acl_pkt_is_dump_qca(hdev, skb))
3556 		return handle_dump_pkt_qca(hdev, skb);
3557 	return hci_recv_frame(hdev, skb);
3558 }
3559 
3560 static int btusb_recv_evt_qca(struct hci_dev *hdev, struct sk_buff *skb)
3561 {
3562 	if (evt_pkt_is_dump_qca(hdev, skb))
3563 		return handle_dump_pkt_qca(hdev, skb);
3564 	return hci_recv_frame(hdev, skb);
3565 }
3566 
3567 
3568 #define QCA_DFU_PACKET_LEN	4096
3569 
3570 #define QCA_GET_TARGET_VERSION	0x09
3571 #define QCA_CHECK_STATUS	0x05
3572 #define QCA_DFU_DOWNLOAD	0x01
3573 
3574 #define QCA_SYSCFG_UPDATED	0x40
3575 #define QCA_PATCH_UPDATED	0x80
3576 #define QCA_DFU_TIMEOUT		3000
3577 #define QCA_FLAG_MULTI_NVM      0x80
3578 #define QCA_BT_RESET_WAIT_MS    100
3579 
3580 #define WCN6855_2_0_RAM_VERSION_GF 0x400c1200
3581 #define WCN6855_2_1_RAM_VERSION_GF 0x400c1211
3582 
3583 struct qca_version {
3584 	__le32	rom_version;
3585 	__le32	patch_version;
3586 	__le32	ram_version;
3587 	__u8	chip_id;
3588 	__u8	platform_id;
3589 	__le16	flag;
3590 	__u8	reserved[4];
3591 } __packed;
3592 
3593 struct qca_rampatch_version {
3594 	__le16	rom_version_high;
3595 	__le16  rom_version_low;
3596 	__le16	patch_version;
3597 } __packed;
3598 
3599 struct qca_device_info {
3600 	u32	rom_version;
3601 	u8	rampatch_hdr;	/* length of header in rampatch */
3602 	u8	nvm_hdr;	/* length of header in NVM */
3603 	u8	ver_offset;	/* offset of version structure in rampatch */
3604 };
3605 
3606 struct qca_custom_firmware {
3607 	u32 rom_version;
3608 	u16 board_id;
3609 	const char *subdirectory;
3610 };
3611 
3612 static const struct qca_device_info qca_devices_table[] = {
3613 	{ 0x00000100, 20, 4,  8 }, /* Rome 1.0 */
3614 	{ 0x00000101, 20, 4,  8 }, /* Rome 1.1 */
3615 	{ 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3616 	{ 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3617 	{ 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3618 	{ 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3619 	{ 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3620 	{ 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3621 	{ 0x00130201, 40, 4, 16 }, /* WCN6855 2.1 */
3622 	{ 0x00190200, 40, 4, 16 }, /* WCN785x 2.0 */
3623 };
3624 
3625 static const struct qca_custom_firmware qca_custom_btfws[] = {
3626 	{ 0x00130201, 0x030A, "QCA2066" },
3627 	{ 0x00130201, 0x030B, "QCA2066" },
3628 	{ },
3629 };
3630 
3631 static u16 qca_extract_board_id(const struct qca_version *ver)
3632 {
3633 	u16 flag = le16_to_cpu(ver->flag);
3634 	u16 board_id = 0;
3635 
3636 	if (((flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
3637 		/* The board_id should be split into two bytes
3638 		 * The 1st byte is chip ID, and the 2nd byte is platform ID
3639 		 * For example, board ID 0x010A, 0x01 is platform ID. 0x0A is chip ID
3640 		 * we have several platforms, and platform IDs are continuously added
3641 		 * Platform ID:
3642 		 * 0x00 is for Mobile
3643 		 * 0x01 is for X86
3644 		 * 0x02 is for Automotive
3645 		 * 0x03 is for Consumer electronic
3646 		 */
3647 		board_id = (ver->chip_id << 8) + ver->platform_id;
3648 	}
3649 
3650 	/* Take 0xffff as invalid board ID */
3651 	if (board_id == 0xffff)
3652 		board_id = 0;
3653 
3654 	return board_id;
3655 }
3656 
3657 static const char *qca_get_fw_subdirectory(const struct qca_version *ver)
3658 {
3659 	const struct qca_custom_firmware *ptr;
3660 	u32 rom_ver;
3661 	u16 board_id;
3662 
3663 	rom_ver = le32_to_cpu(ver->rom_version);
3664 	board_id = qca_extract_board_id(ver);
3665 	if (!board_id)
3666 		return NULL;
3667 
3668 	for (ptr = qca_custom_btfws; ptr->rom_version; ptr++) {
3669 		if (ptr->rom_version == rom_ver &&
3670 		    ptr->board_id == board_id)
3671 			return ptr->subdirectory;
3672 	}
3673 
3674 	return NULL;
3675 }
3676 
3677 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3678 				     void *data, u16 size)
3679 {
3680 	int err;
3681 
3682 	/* Found some of USB hosts have IOT issues with ours so that we should
3683 	 * not wait until HCI layer is ready.
3684 	 */
3685 	err = usb_control_msg_recv(udev, 0, request, USB_TYPE_VENDOR | USB_DIR_IN,
3686 				   0, 0, data, size, USB_CTRL_GET_TIMEOUT,
3687 				   GFP_KERNEL);
3688 	if (err)
3689 		dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3690 
3691 	return err;
3692 }
3693 
3694 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3695 				       const struct firmware *firmware,
3696 				       size_t hdr_size)
3697 {
3698 	struct btusb_data *btdata = hci_get_drvdata(hdev);
3699 	struct usb_device *udev = btdata->udev;
3700 	size_t count, size, sent = 0;
3701 	int pipe, len, err;
3702 	u8 *buf;
3703 
3704 	buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3705 	if (!buf)
3706 		return -ENOMEM;
3707 
3708 	count = firmware->size;
3709 
3710 	size = min_t(size_t, count, hdr_size);
3711 	memcpy(buf, firmware->data, size);
3712 
3713 	/* USB patches should go down to controller through USB path
3714 	 * because binary format fits to go down through USB channel.
3715 	 * USB control path is for patching headers and USB bulk is for
3716 	 * patch body.
3717 	 */
3718 	pipe = usb_sndctrlpipe(udev, 0);
3719 	err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3720 			      0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3721 	if (err < 0) {
3722 		bt_dev_err(hdev, "Failed to send headers (%d)", err);
3723 		goto done;
3724 	}
3725 
3726 	sent += size;
3727 	count -= size;
3728 
3729 	/* ep2 need time to switch from function acl to function dfu,
3730 	 * so we add 20ms delay here.
3731 	 */
3732 	msleep(20);
3733 
3734 	while (count) {
3735 		size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3736 
3737 		memcpy(buf, firmware->data + sent, size);
3738 
3739 		pipe = usb_sndbulkpipe(udev, 0x02);
3740 		err = usb_bulk_msg(udev, pipe, buf, size, &len,
3741 				   QCA_DFU_TIMEOUT);
3742 		if (err < 0) {
3743 			bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3744 				   sent, firmware->size, err);
3745 			break;
3746 		}
3747 
3748 		if (size != len) {
3749 			bt_dev_err(hdev, "Failed to get bulk buffer");
3750 			err = -EILSEQ;
3751 			break;
3752 		}
3753 
3754 		sent  += size;
3755 		count -= size;
3756 	}
3757 
3758 done:
3759 	kfree(buf);
3760 	return err;
3761 }
3762 
3763 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3764 					 struct qca_version *ver,
3765 					 const struct qca_device_info *info)
3766 {
3767 	struct qca_rampatch_version *rver;
3768 	const struct firmware *fw;
3769 	const char *fw_subdir;
3770 	u32 ver_rom, ver_patch, rver_rom;
3771 	u16 rver_rom_low, rver_rom_high, rver_patch;
3772 	char fwname[80];
3773 	int err;
3774 
3775 	ver_rom = le32_to_cpu(ver->rom_version);
3776 	ver_patch = le32_to_cpu(ver->patch_version);
3777 
3778 	fw_subdir = qca_get_fw_subdirectory(ver);
3779 	if (fw_subdir)
3780 		snprintf(fwname, sizeof(fwname), "qca/%s/rampatch_usb_%08x.bin",
3781 			 fw_subdir, ver_rom);
3782 	else
3783 		snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin",
3784 			 ver_rom);
3785 
3786 	err = request_firmware(&fw, fwname, &hdev->dev);
3787 	if (err) {
3788 		bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3789 			   fwname, err);
3790 		return err;
3791 	}
3792 
3793 	bt_dev_info(hdev, "using rampatch file: %s", fwname);
3794 
3795 	rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3796 	rver_rom_low = le16_to_cpu(rver->rom_version_low);
3797 	rver_patch = le16_to_cpu(rver->patch_version);
3798 
3799 	if (ver_rom & ~0xffffU) {
3800 		rver_rom_high = le16_to_cpu(rver->rom_version_high);
3801 		rver_rom = rver_rom_high << 16 | rver_rom_low;
3802 	} else {
3803 		rver_rom = rver_rom_low;
3804 	}
3805 
3806 	bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3807 		    "firmware rome 0x%x build 0x%x",
3808 		    rver_rom, rver_patch, ver_rom, ver_patch);
3809 
3810 	/* Allow rampatch when the patch version equals the firmware version.
3811 	 * A firmware download may be aborted by a transient USB error (e.g.
3812 	 * disconnect) after the controller updates version info but before
3813 	 * completion.
3814 	 * Allowing equal versions enables re-flashing during recovery.
3815 	 */
3816 	if (rver_rom != ver_rom || rver_patch < ver_patch) {
3817 		bt_dev_err(hdev, "rampatch file version did not match with firmware");
3818 		err = -EINVAL;
3819 		goto done;
3820 	}
3821 
3822 	err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3823 
3824 done:
3825 	release_firmware(fw);
3826 
3827 	return err;
3828 }
3829 
3830 static void btusb_generate_qca_nvm_name(char *fwname, size_t max_size,
3831 					const struct qca_version *ver)
3832 {
3833 	u32 rom_version = le32_to_cpu(ver->rom_version);
3834 	const char *variant, *fw_subdir;
3835 	int len;
3836 	u16 board_id;
3837 
3838 	fw_subdir = qca_get_fw_subdirectory(ver);
3839 	board_id = qca_extract_board_id(ver);
3840 
3841 	switch (le32_to_cpu(ver->ram_version)) {
3842 	case WCN6855_2_0_RAM_VERSION_GF:
3843 	case WCN6855_2_1_RAM_VERSION_GF:
3844 		variant = "_gf";
3845 		break;
3846 	default:
3847 		variant = NULL;
3848 		break;
3849 	}
3850 
3851 	if (fw_subdir)
3852 		len = snprintf(fwname, max_size, "qca/%s/nvm_usb_%08x",
3853 			       fw_subdir, rom_version);
3854 	else
3855 		len = snprintf(fwname, max_size, "qca/nvm_usb_%08x",
3856 			       rom_version);
3857 	if (variant)
3858 		len += snprintf(fwname + len, max_size - len, "%s", variant);
3859 	if (board_id)
3860 		len += snprintf(fwname + len, max_size - len, "_%04x", board_id);
3861 	len += snprintf(fwname + len, max_size - len, ".bin");
3862 }
3863 
3864 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3865 				    struct qca_version *ver,
3866 				    const struct qca_device_info *info)
3867 {
3868 	const struct firmware *fw;
3869 	char fwname[80];
3870 	int err;
3871 
3872 	btusb_generate_qca_nvm_name(fwname, sizeof(fwname), ver);
3873 
3874 	err = request_firmware(&fw, fwname, &hdev->dev);
3875 	if (err) {
3876 		bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3877 			   fwname, err);
3878 		return err;
3879 	}
3880 
3881 	bt_dev_info(hdev, "using NVM file: %s", fwname);
3882 
3883 	err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3884 
3885 	release_firmware(fw);
3886 
3887 	return err;
3888 }
3889 
3890 /* identify the ROM version and check whether patches are needed */
3891 static bool btusb_qca_need_patch(struct usb_device *udev)
3892 {
3893 	struct qca_version ver;
3894 
3895 	if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3896 				      sizeof(ver)))
3897 		return false;
3898 	/* only low ROM versions need patches */
3899 	return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3900 }
3901 
3902 static int btusb_setup_qca(struct hci_dev *hdev)
3903 {
3904 	struct btusb_data *btdata = hci_get_drvdata(hdev);
3905 	struct usb_device *udev = btdata->udev;
3906 	const struct qca_device_info *info = NULL;
3907 	struct qca_version ver;
3908 	u32 ver_rom;
3909 	u8 status;
3910 	int i, err;
3911 
3912 	err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3913 					sizeof(ver));
3914 	if (err)
3915 		return err;
3916 
3917 	ver_rom = le32_to_cpu(ver.rom_version);
3918 
3919 	for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
3920 		if (ver_rom == qca_devices_table[i].rom_version)
3921 			info = &qca_devices_table[i];
3922 	}
3923 	if (!info) {
3924 		/* If the rom_version is not matched in the qca_devices_table
3925 		 * and the high ROM version is not zero, we assume this chip no
3926 		 * need to load the rampatch and nvm.
3927 		 */
3928 		if (ver_rom & ~0xffffU)
3929 			return 0;
3930 
3931 		bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3932 		return -ENODEV;
3933 	}
3934 
3935 	err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3936 					sizeof(status));
3937 	if (err)
3938 		return err;
3939 
3940 	if (!(status & QCA_PATCH_UPDATED)) {
3941 		err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3942 		if (err < 0)
3943 			return err;
3944 	}
3945 
3946 	err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3947 					sizeof(ver));
3948 	if (err)
3949 		return err;
3950 
3951 	if (btdata->match_id->driver_info & BTUSB_QCA_WCN6855) {
3952 		struct btqca_data *btqca_data = hci_get_priv(hdev);
3953 
3954 		btqca_data->qca_dump.fw_version = le32_to_cpu(ver.patch_version);
3955 		btqca_data->qca_dump.controller_id = le32_to_cpu(ver.rom_version);
3956 	}
3957 
3958 	if (!(status & QCA_SYSCFG_UPDATED)) {
3959 		err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3960 		if (err < 0)
3961 			return err;
3962 
3963 		/* WCN6855 2.1 and later will reset to apply firmware downloaded here, so
3964 		 * wait ~100ms for reset Done then go ahead, otherwise, it maybe
3965 		 * cause potential enable failure.
3966 		 */
3967 		if (info->rom_version >= 0x00130201)
3968 			msleep(QCA_BT_RESET_WAIT_MS);
3969 	}
3970 
3971 	/* Mark HCI_OP_ENHANCED_SETUP_SYNC_CONN as broken as it doesn't seem to
3972 	 * work with the likes of HSP/HFP mSBC.
3973 	 */
3974 	hci_set_quirk(hdev, HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN);
3975 
3976 	return 0;
3977 }
3978 
3979 static inline int __set_diag_interface(struct hci_dev *hdev)
3980 {
3981 	struct btusb_data *data = hci_get_drvdata(hdev);
3982 	struct usb_interface *intf = data->diag;
3983 	int ret;
3984 
3985 	if (!data->diag)
3986 		return -ENODEV;
3987 
3988 	ret = usb_find_common_endpoints(intf->cur_altsetting, &data->diag_rx_ep,
3989 					&data->diag_tx_ep, NULL, NULL);
3990 	if (ret) {
3991 		bt_dev_err(hdev, "invalid diagnostic descriptors");
3992 		return -ENODEV;
3993 	}
3994 
3995 	return 0;
3996 }
3997 
3998 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3999 {
4000 	struct btusb_data *data = hci_get_drvdata(hdev);
4001 	struct sk_buff *skb;
4002 	struct urb *urb;
4003 	unsigned int pipe;
4004 
4005 	if (!data->diag_tx_ep)
4006 		return ERR_PTR(-ENODEV);
4007 
4008 	urb = usb_alloc_urb(0, GFP_KERNEL);
4009 	if (!urb)
4010 		return ERR_PTR(-ENOMEM);
4011 
4012 	skb = bt_skb_alloc(2, GFP_KERNEL);
4013 	if (!skb) {
4014 		usb_free_urb(urb);
4015 		return ERR_PTR(-ENOMEM);
4016 	}
4017 
4018 	skb_put_u8(skb, 0xf0);
4019 	skb_put_u8(skb, enable);
4020 
4021 	pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
4022 
4023 	usb_fill_bulk_urb(urb, data->udev, pipe,
4024 			  skb->data, skb->len, btusb_tx_complete, skb);
4025 
4026 	skb->dev = (void *)hdev;
4027 
4028 	return urb;
4029 }
4030 
4031 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
4032 {
4033 	struct btusb_data *data = hci_get_drvdata(hdev);
4034 	struct urb *urb;
4035 
4036 	if (!data->diag)
4037 		return -ENODEV;
4038 
4039 	if (!test_bit(HCI_RUNNING, &hdev->flags))
4040 		return -ENETDOWN;
4041 
4042 	urb = alloc_diag_urb(hdev, enable);
4043 	if (IS_ERR(urb))
4044 		return PTR_ERR(urb);
4045 
4046 	return submit_or_queue_tx_urb(hdev, urb);
4047 }
4048 
4049 #ifdef CONFIG_PM
4050 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
4051 {
4052 	struct btusb_data *data = priv;
4053 
4054 	pm_wakeup_event(&data->udev->dev, 0);
4055 	pm_system_wakeup();
4056 
4057 	/* Disable only if not already disabled (keep it balanced) */
4058 	if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4059 		disable_irq_nosync(irq);
4060 		disable_irq_wake(irq);
4061 	}
4062 	return IRQ_HANDLED;
4063 }
4064 
4065 static const struct of_device_id btusb_match_table[] = {
4066 	{ .compatible = "usb1286,204e" },
4067 	{ .compatible = "usbcf3,e300" }, /* QCA6174A */
4068 	{ .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
4069 	{ }
4070 };
4071 MODULE_DEVICE_TABLE(of, btusb_match_table);
4072 
4073 /* Use an oob wakeup pin? */
4074 static int btusb_config_oob_wake(struct hci_dev *hdev)
4075 {
4076 	struct btusb_data *data = hci_get_drvdata(hdev);
4077 	struct device *dev = &data->udev->dev;
4078 	int irq, ret;
4079 
4080 	clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4081 
4082 	if (!of_match_device(btusb_match_table, dev))
4083 		return 0;
4084 
4085 	/* Move on if no IRQ specified */
4086 	irq = of_irq_get_byname(dev->of_node, "wakeup");
4087 	if (irq <= 0) {
4088 		bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
4089 		return 0;
4090 	}
4091 
4092 	irq_set_status_flags(irq, IRQ_NOAUTOEN);
4093 	ret = request_irq(irq, btusb_oob_wake_handler, 0, "OOB Wake-on-BT", data);
4094 	if (ret) {
4095 		bt_dev_err(hdev, "%s: IRQ request failed", __func__);
4096 		return ret;
4097 	}
4098 
4099 	ret = device_init_wakeup(dev, true);
4100 	if (ret) {
4101 		bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
4102 		goto err_free_irq;
4103 	}
4104 
4105 	data->oob_wake_irq = irq;
4106 	bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
4107 
4108 	return 0;
4109 
4110 err_free_irq:
4111 	free_irq(irq, data);
4112 
4113 	return ret;
4114 }
4115 #else
4116 static inline int btusb_config_oob_wake(struct hci_dev *hdev)
4117 {
4118 	return 0;
4119 }
4120 #endif
4121 
4122 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
4123 {
4124 	if (dmi_check_system(btusb_needs_reset_resume_table))
4125 		interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
4126 }
4127 
4128 static bool btusb_wakeup(struct hci_dev *hdev)
4129 {
4130 	struct btusb_data *data = hci_get_drvdata(hdev);
4131 
4132 	return device_may_wakeup(&data->udev->dev);
4133 }
4134 
4135 static int btusb_shutdown_qca(struct hci_dev *hdev)
4136 {
4137 	int err;
4138 
4139 	err = __hci_reset_sync(hdev);
4140 	if (err)
4141 		bt_dev_err(hdev, "HCI reset during shutdown failed");
4142 
4143 	return err;
4144 }
4145 
4146 static ssize_t force_poll_sync_read(struct file *file, char __user *user_buf,
4147 				    size_t count, loff_t *ppos)
4148 {
4149 	struct btusb_data *data = file->private_data;
4150 	char buf[3];
4151 
4152 	buf[0] = data->poll_sync ? 'Y' : 'N';
4153 	buf[1] = '\n';
4154 	buf[2] = '\0';
4155 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
4156 }
4157 
4158 static ssize_t force_poll_sync_write(struct file *file,
4159 				     const char __user *user_buf,
4160 				     size_t count, loff_t *ppos)
4161 {
4162 	struct btusb_data *data = file->private_data;
4163 	bool enable;
4164 	int err;
4165 
4166 	err = kstrtobool_from_user(user_buf, count, &enable);
4167 	if (err)
4168 		return err;
4169 
4170 	/* Only allow changes while the adapter is down and it is using legacy
4171 	 * protocol.
4172 	 */
4173 	if (test_bit(HCI_UP, &data->hdev->flags) ||
4174 	    data->proto != BTUSB_PROTO_LEGACY)
4175 		return -EPERM;
4176 
4177 	if (data->poll_sync == enable)
4178 		return -EALREADY;
4179 
4180 	data->poll_sync = enable;
4181 
4182 	return count;
4183 }
4184 
4185 static const struct file_operations force_poll_sync_fops = {
4186 	.owner		= THIS_MODULE,
4187 	.open		= simple_open,
4188 	.read		= force_poll_sync_read,
4189 	.write		= force_poll_sync_write,
4190 	.llseek		= default_llseek,
4191 };
4192 
4193 #define BTUSB_HCI_DRV_OP_SUPPORTED_ALTSETTINGS \
4194 		hci_opcode_pack(HCI_DRV_OGF_DRIVER_SPECIFIC, 0x0000)
4195 #define BTUSB_HCI_DRV_SUPPORTED_ALTSETTINGS_SIZE	0
4196 struct btusb_hci_drv_rp_supported_altsettings {
4197 	__u8	num;
4198 	__u8	altsettings[];
4199 } __packed;
4200 
4201 #define BTUSB_HCI_DRV_OP_SWITCH_ALTSETTING \
4202 		hci_opcode_pack(HCI_DRV_OGF_DRIVER_SPECIFIC, 0x0001)
4203 #define BTUSB_HCI_DRV_SWITCH_ALTSETTING_SIZE		1
4204 struct btusb_hci_drv_cmd_switch_altsetting {
4205 	__u8	altsetting;
4206 } __packed;
4207 
4208 static const struct {
4209 	u16 opcode;
4210 	const char *desc;
4211 } btusb_hci_drv_supported_commands[] = {
4212 	/* Common commands */
4213 	{ HCI_DRV_OP_READ_INFO, "Read Info" },
4214 
4215 	/* Driver specific commands */
4216 	{ BTUSB_HCI_DRV_OP_SUPPORTED_ALTSETTINGS, "Supported Altsettings" },
4217 	{ BTUSB_HCI_DRV_OP_SWITCH_ALTSETTING,     "Switch Altsetting" },
4218 };
4219 static int btusb_hci_drv_read_info(struct hci_dev *hdev, void *data,
4220 				   u16 data_len)
4221 {
4222 	struct hci_drv_rp_read_info *rp;
4223 	size_t rp_size;
4224 	int err, i;
4225 	u16 opcode, num_supported_commands =
4226 		ARRAY_SIZE(btusb_hci_drv_supported_commands);
4227 
4228 	rp_size = sizeof(*rp) + num_supported_commands * 2;
4229 
4230 	rp = kmalloc(rp_size, GFP_KERNEL);
4231 	if (!rp)
4232 		return -ENOMEM;
4233 
4234 	strscpy_pad(rp->driver_name, btusb_driver.name);
4235 
4236 	rp->num_supported_commands = cpu_to_le16(num_supported_commands);
4237 	for (i = 0; i < num_supported_commands; i++) {
4238 		opcode = btusb_hci_drv_supported_commands[i].opcode;
4239 		bt_dev_info(hdev,
4240 			    "Supported HCI Drv command (0x%02x|0x%04x): %s",
4241 			    hci_opcode_ogf(opcode),
4242 			    hci_opcode_ocf(opcode),
4243 			    btusb_hci_drv_supported_commands[i].desc);
4244 		rp->supported_commands[i] = cpu_to_le16(opcode);
4245 	}
4246 
4247 	err = hci_drv_cmd_complete(hdev, HCI_DRV_OP_READ_INFO,
4248 				   HCI_DRV_STATUS_SUCCESS, rp, rp_size);
4249 
4250 	kfree(rp);
4251 	return err;
4252 }
4253 
4254 static int btusb_hci_drv_supported_altsettings(struct hci_dev *hdev, void *data,
4255 					       u16 data_len)
4256 {
4257 	struct btusb_data *drvdata = hci_get_drvdata(hdev);
4258 	struct btusb_hci_drv_rp_supported_altsettings *rp;
4259 	size_t rp_size;
4260 	int err;
4261 	u8 i;
4262 
4263 	/* There are at most 7 alt (0 - 6) */
4264 	rp = kmalloc(sizeof(*rp) + 7, GFP_KERNEL);
4265 	if (!rp)
4266 		return -ENOMEM;
4267 
4268 	rp->num = 0;
4269 	if (!drvdata->isoc)
4270 		goto done;
4271 
4272 	for (i = 0; i <= 6; i++) {
4273 		if (btusb_find_altsetting(drvdata->isoc, i))
4274 			rp->altsettings[rp->num++] = i;
4275 	}
4276 
4277 done:
4278 	rp_size = sizeof(*rp) + rp->num;
4279 
4280 	err = hci_drv_cmd_complete(hdev, BTUSB_HCI_DRV_OP_SUPPORTED_ALTSETTINGS,
4281 				   HCI_DRV_STATUS_SUCCESS, rp, rp_size);
4282 	kfree(rp);
4283 	return err;
4284 }
4285 
4286 static int btusb_hci_drv_switch_altsetting(struct hci_dev *hdev, void *data,
4287 					   u16 data_len)
4288 {
4289 	struct btusb_hci_drv_cmd_switch_altsetting *cmd = data;
4290 	u8 status;
4291 
4292 	if (cmd->altsetting > 6) {
4293 		status = HCI_DRV_STATUS_INVALID_PARAMETERS;
4294 	} else {
4295 		if (btusb_switch_alt_setting(hdev, cmd->altsetting))
4296 			status = HCI_DRV_STATUS_UNSPECIFIED_ERROR;
4297 		else
4298 			status = HCI_DRV_STATUS_SUCCESS;
4299 	}
4300 
4301 	return hci_drv_cmd_status(hdev, BTUSB_HCI_DRV_OP_SWITCH_ALTSETTING,
4302 				  status);
4303 }
4304 
4305 static const struct hci_drv_handler btusb_hci_drv_common_handlers[] = {
4306 	{ btusb_hci_drv_read_info,	HCI_DRV_READ_INFO_SIZE },
4307 };
4308 
4309 static const struct hci_drv_handler btusb_hci_drv_specific_handlers[] = {
4310 	{ btusb_hci_drv_supported_altsettings,
4311 				BTUSB_HCI_DRV_SUPPORTED_ALTSETTINGS_SIZE },
4312 	{ btusb_hci_drv_switch_altsetting,
4313 				BTUSB_HCI_DRV_SWITCH_ALTSETTING_SIZE },
4314 };
4315 
4316 static struct hci_drv btusb_hci_drv = {
4317 	.common_handler_count	= ARRAY_SIZE(btusb_hci_drv_common_handlers),
4318 	.common_handlers	= btusb_hci_drv_common_handlers,
4319 	.specific_handler_count	= ARRAY_SIZE(btusb_hci_drv_specific_handlers),
4320 	.specific_handlers	= btusb_hci_drv_specific_handlers,
4321 };
4322 
4323 /*
4324  * ============================================================================
4325  * Qualcomm support
4326  * ============================================================================
4327  */
4328 
4329 static int btusb_recv_intr_qcom(struct btusb_data *data, void *buffer, int count)
4330 {
4331 	unsigned long flags;
4332 	int err = 0;
4333 
4334 	spin_lock_irqsave(&data->rxlock, flags);
4335 	data->evt_skb = btusb_qcom_recv_intr(data->hdev, data->evt_skb, buffer, count,
4336 					     &err);
4337 	spin_unlock_irqrestore(&data->rxlock, flags);
4338 
4339 	return err;
4340 }
4341 
4342 static int btusb_recv_bulk_qcom(struct btusb_data *data, void *buffer, int count)
4343 {
4344 	unsigned long flags;
4345 	int err = 0;
4346 
4347 	spin_lock_irqsave(&data->rxlock, flags);
4348 	data->acl_skb = btusb_qcom_recv_bulk(data->hdev, data->acl_skb, buffer, count,
4349 					     &err);
4350 	spin_unlock_irqrestore(&data->rxlock, flags);
4351 
4352 	return err;
4353 }
4354 
4355 static int btusb_send_vendor_frame_qcom(struct hci_dev *hdev, struct sk_buff *skb)
4356 {
4357 	u8 pkt_type = hci_skb_pkt_type(skb);
4358 	struct urb *urb;
4359 
4360 	switch (pkt_type) {
4361 	case QPERI_COMMAND_PKT:
4362 		urb = alloc_ctrl_urb(hdev, skb);
4363 		break;
4364 
4365 	case QPERI_ACLDATA_PKT:
4366 		urb = alloc_bulk_urb(hdev, skb);
4367 		break;
4368 
4369 	default:
4370 		return -EILSEQ;
4371 	}
4372 
4373 	if (IS_ERR(urb))
4374 		return PTR_ERR(urb);
4375 
4376 	return submit_or_queue_tx_urb(hdev, urb);
4377 }
4378 
4379 /*
4380  * ============================================================================
4381  * Qualcomm support end
4382  * ============================================================================
4383  */
4384 
4385 static int btusb_probe(struct usb_interface *intf,
4386 		       const struct usb_device_id *id)
4387 {
4388 	struct gpio_desc *reset_gpio;
4389 	struct usb_host_interface *alt;
4390 	struct usb_endpoint_descriptor *bulk_rx_ep, *bulk_tx_ep, *intr_ep;
4391 	struct btusb_data *data;
4392 	struct hci_dev *hdev;
4393 	unsigned ifnum_base;
4394 	int err, priv_size = 0;
4395 
4396 	BT_DBG("intf %p id %p", intf, id);
4397 
4398 	if ((id->driver_info & BTUSB_IFNUM_2) &&
4399 	    (intf->cur_altsetting->desc.bInterfaceNumber != 0) &&
4400 	    (intf->cur_altsetting->desc.bInterfaceNumber != 2))
4401 		return -ENODEV;
4402 
4403 	ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
4404 
4405 	if (!id->driver_info) {
4406 		const struct usb_device_id *match;
4407 
4408 		match = usb_match_id(intf, quirks_table);
4409 		if (match)
4410 			id = match;
4411 	}
4412 
4413 	if (id->driver_info & BTUSB_IGNORE)
4414 		return -ENODEV;
4415 
4416 	if (id->driver_info & BTUSB_ATH3012) {
4417 		struct usb_device *udev = interface_to_usbdev(intf);
4418 
4419 		/* Old firmware would otherwise let ath3k driver load
4420 		 * patch and sysconfig files
4421 		 */
4422 		if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
4423 		    !btusb_qca_need_patch(udev))
4424 			return -ENODEV;
4425 	}
4426 
4427 	data = kzalloc_obj(*data);
4428 	if (!data)
4429 		return -ENOMEM;
4430 
4431 	data->match_id = id;
4432 
4433 	/* Alternate setting 1 with a single pair of bulk endpoints and no
4434 	 * interrupt endpoint means the controller supports Bulk Serialization
4435 	 * Mode, in which every packet is prefixed with an H:4 header and
4436 	 * carried over the bulk endpoints.
4437 	 */
4438 	alt = btusb_find_altsetting(intf, 1);
4439 	if (alt && usb_find_int_in_endpoint(alt, &intr_ep) &&
4440 	    !usb_find_common_endpoints(alt, &bulk_rx_ep, &bulk_tx_ep, NULL,
4441 				       NULL)) {
4442 		err = usb_set_interface(interface_to_usbdev(intf), ifnum_base, 1);
4443 		if (!err)
4444 			data->proto = BTUSB_PROTO_H4;
4445 		else
4446 			dev_warn(&intf->dev,
4447 				 "failed to select alt setting 1 (%d), using legacy mode",
4448 				 err);
4449 	}
4450 
4451 	/* Check if all endpoints could be enumerated, legacy mode requires
4452 	 * interrupt and bulk endpoints while H4 mode only requires bulk
4453 	 * endpoints.
4454 	 */
4455 	err = usb_find_common_endpoints(intf->cur_altsetting, &data->bulk_rx_ep,
4456 					&data->bulk_tx_ep,
4457 					data->proto == BTUSB_PROTO_LEGACY ?
4458 						&data->intr_ep : NULL,
4459 					NULL);
4460 	if (err) {
4461 		dev_err(&intf->dev, "failed to enumerate endpoints\n");
4462 		goto err_free_data;
4463 	}
4464 
4465 	if (id->driver_info & BTUSB_AMP) {
4466 		data->cmdreq_type = USB_TYPE_CLASS | 0x01;
4467 		data->cmdreq = 0x2b;
4468 	} else {
4469 		data->cmdreq_type = USB_TYPE_CLASS;
4470 		data->cmdreq = 0x00;
4471 	}
4472 
4473 	data->udev = interface_to_usbdev(intf);
4474 	data->intf = intf;
4475 
4476 	INIT_WORK(&data->work, btusb_work);
4477 	INIT_WORK(&data->waker, btusb_waker);
4478 	INIT_DELAYED_WORK(&data->rx_work, btusb_rx_work);
4479 
4480 	skb_queue_head_init(&data->acl_q);
4481 
4482 	init_usb_anchor(&data->deferred);
4483 	init_usb_anchor(&data->tx_anchor);
4484 	spin_lock_init(&data->txlock);
4485 
4486 	init_usb_anchor(&data->intr_anchor);
4487 	init_usb_anchor(&data->bulk_anchor);
4488 	init_usb_anchor(&data->isoc_anchor);
4489 	init_usb_anchor(&data->diag_anchor);
4490 	init_usb_anchor(&data->ctrl_anchor);
4491 	spin_lock_init(&data->rxlock);
4492 
4493 	data->recv_event = hci_recv_frame;
4494 	data->recv_intr = btusb_recv_intr;
4495 	data->recv_bulk = btusb_recv_bulk;
4496 
4497 	if (id->driver_info & BTUSB_INTEL_COMBINED) {
4498 		/* Allocate extra space for Intel device */
4499 		priv_size += sizeof(struct btintel_data);
4500 
4501 		/* Override the rx handlers */
4502 		data->recv_event = btintel_recv_event;
4503 		data->recv_bulk = btusb_recv_bulk_intel;
4504 	} else if (id->driver_info & BTUSB_REALTEK) {
4505 		/* Allocate extra space for Realtek device */
4506 		priv_size += sizeof(struct btrealtek_data);
4507 
4508 		data->recv_event = btusb_recv_event_realtek;
4509 	} else if (id->driver_info & BTUSB_MEDIATEK) {
4510 		/* Allocate extra space for Mediatek device */
4511 		priv_size += sizeof(struct btmtk_data);
4512 	} else if (id->driver_info & BTUSB_QCA_WCN6855) {
4513 		/* Allocate extra space for QCA WCN6855 device */
4514 		priv_size += sizeof(struct btqca_data);
4515 	} else if (id->driver_info & BTUSB_QUALCOMM) {
4516 		/* Allocate extra space for Qualcomm device */
4517 		priv_size += btusb_qcom_hdev_priv_size();
4518 	}
4519 
4520 	data->recv_acl = hci_recv_frame;
4521 
4522 	hdev = hci_alloc_dev_priv(priv_size);
4523 	if (!hdev) {
4524 		err = -ENOMEM;
4525 		goto err_free_data;
4526 	}
4527 
4528 	hdev->bus = HCI_USB;
4529 	hci_set_drvdata(hdev, data);
4530 
4531 	data->hdev = hdev;
4532 
4533 	SET_HCIDEV_DEV(hdev, &intf->dev);
4534 
4535 	reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
4536 					GPIOD_OUT_LOW);
4537 	if (IS_ERR(reset_gpio)) {
4538 		err = PTR_ERR(reset_gpio);
4539 		goto err_free_hdev;
4540 	} else if (reset_gpio) {
4541 		data->reset_gpio = reset_gpio;
4542 	}
4543 
4544 	hdev->open    = btusb_open;
4545 	hdev->close   = btusb_close;
4546 	hdev->flush   = btusb_flush;
4547 	hdev->send    = btusb_send_frame;
4548 	hdev->notify  = btusb_notify;
4549 	hdev->wakeup  = btusb_wakeup;
4550 	hdev->hci_drv = &btusb_hci_drv;
4551 
4552 	err = btusb_config_oob_wake(hdev);
4553 	if (err)
4554 		goto err_put_reset;
4555 
4556 	/* Marvell devices may need a specific chip configuration */
4557 	if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
4558 		err = marvell_config_oob_wake(hdev);
4559 		if (err)
4560 			goto err_disable_wakeup;
4561 	}
4562 
4563 	if (id->driver_info & BTUSB_CW6622)
4564 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_STORED_LINK_KEY);
4565 
4566 	if (id->driver_info & BTUSB_BCM2045)
4567 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_STORED_LINK_KEY);
4568 
4569 	if (id->driver_info & BTUSB_BCM92035)
4570 		hdev->setup = btusb_setup_bcm92035;
4571 
4572 	if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4573 	    (id->driver_info & BTUSB_BCM_PATCHRAM)) {
4574 		hdev->manufacturer = 15;
4575 		hdev->setup = btbcm_setup_patchram;
4576 		hdev->set_diag = btusb_bcm_set_diag;
4577 		hdev->set_bdaddr = btbcm_set_bdaddr;
4578 
4579 		/* Broadcom LM_DIAG Interface numbers are hardcoded */
4580 		data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4581 	}
4582 
4583 	if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4584 	    (id->driver_info & BTUSB_BCM_APPLE)) {
4585 		hdev->manufacturer = 15;
4586 		hdev->setup = btbcm_setup_apple;
4587 		hdev->set_diag = btusb_bcm_set_diag;
4588 
4589 		/* Broadcom LM_DIAG Interface numbers are hardcoded */
4590 		data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4591 	}
4592 
4593 	/* Combined Intel Device setup to support multiple setup routine */
4594 	if (id->driver_info & BTUSB_INTEL_COMBINED) {
4595 		err = btintel_configure_setup(hdev, btusb_driver.name);
4596 		if (err)
4597 			goto err_kill_tx_urbs;
4598 
4599 		/* Transport specific configuration */
4600 		hdev->send = btusb_send_frame_intel;
4601 		hdev->reset = btusb_intel_reset;
4602 
4603 		if (id->driver_info & BTUSB_INTEL_NO_WBS_SUPPORT)
4604 			btintel_set_flag(hdev, INTEL_ROM_LEGACY_NO_WBS_SUPPORT);
4605 
4606 		if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
4607 			btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
4608 
4609 		if (id->driver_info & BTUSB_INTEL_BROKEN_SHUTDOWN_LED)
4610 			btintel_set_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED);
4611 	}
4612 
4613 	if (id->driver_info & BTUSB_MARVELL)
4614 		hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4615 
4616 	if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4617 	    (id->driver_info & BTUSB_MEDIATEK)) {
4618 		hdev->setup = btusb_mtk_setup;
4619 		hdev->shutdown = btusb_mtk_shutdown;
4620 		hdev->manufacturer = 70;
4621 		hdev->reset = btmtk_reset_sync;
4622 		hdev->set_bdaddr = btmtk_set_bdaddr;
4623 		hdev->send = btusb_send_frame_mtk;
4624 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN);
4625 		hci_set_quirk(hdev, HCI_QUIRK_NON_PERSISTENT_SETUP);
4626 		data->recv_acl = btmtk_usb_recv_acl;
4627 		data->recv_event = btmtk_recv_event;
4628 		data->suspend = btmtk_usb_suspend;
4629 		data->resume = btmtk_usb_resume;
4630 		data->disconnect = btusb_mtk_disconnect;
4631 	}
4632 
4633 	if (id->driver_info & BTUSB_SWAVE) {
4634 		hci_set_quirk(hdev, HCI_QUIRK_FIXUP_INQUIRY_MODE);
4635 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_LOCAL_COMMANDS);
4636 	}
4637 
4638 	if (id->driver_info & BTUSB_INTEL_BOOT) {
4639 		hdev->manufacturer = 2;
4640 		hci_set_quirk(hdev, HCI_QUIRK_RAW_DEVICE);
4641 	}
4642 
4643 	if (id->driver_info & BTUSB_ATH3012) {
4644 		data->setup_on_usb = btusb_setup_qca;
4645 		hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4646 		hci_set_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
4647 		hci_set_quirk(hdev, HCI_QUIRK_STRICT_DUPLICATE_FILTER);
4648 	}
4649 
4650 	if (id->driver_info & BTUSB_QCA_ROME) {
4651 		data->setup_on_usb = btusb_setup_qca;
4652 		hdev->shutdown = btusb_shutdown_qca;
4653 		hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4654 		hdev->reset = btusb_qca_reset;
4655 		hci_set_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
4656 		btusb_check_needs_reset_resume(intf);
4657 	}
4658 
4659 	if (id->driver_info & BTUSB_QCA_WCN6855) {
4660 		struct btqca_data *btqca_data = hci_get_priv(hdev);
4661 
4662 		btqca_data->qca_dump.id_vendor = id->idVendor;
4663 		btqca_data->qca_dump.id_product = id->idProduct;
4664 		data->recv_event = btusb_recv_evt_qca;
4665 		data->recv_acl = btusb_recv_acl_qca;
4666 		hci_devcd_register(hdev, btusb_coredump_qca, btusb_dump_hdr_qca, NULL);
4667 		data->setup_on_usb = btusb_setup_qca;
4668 		hdev->classify_pkt_type = btusb_classify_qca_pkt_type;
4669 		hdev->shutdown = btusb_shutdown_qca;
4670 		hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
4671 		hdev->reset = btusb_qca_reset;
4672 		hci_set_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
4673 		hci_set_msft_opcode(hdev, 0xFD70);
4674 	}
4675 
4676 	if (id->driver_info & BTUSB_QUALCOMM) {
4677 		struct btusb_qcom *xport_data;
4678 
4679 		if (!IS_ENABLED(CONFIG_BT_HCIBTUSB_QCOM)) {
4680 			err = -ENODEV;
4681 			bt_dev_err(hdev, "CONFIG_BT_HCIBTUSB_QCOM not enabled");
4682 			goto err_kill_tx_urbs;
4683 		}
4684 
4685 		xport_data = btusb_qcom_xport_data(hdev);
4686 		xport_data->reset_gpio = data->reset_gpio;
4687 		xport_data->prepare_reset = btusb_prepare_reset;
4688 		xport_data->recv_bt_frame = btusb_recv_frame;
4689 		xport_data->send_bt_frame = btusb_send_frame;
4690 		xport_data->send_vendor_frame = btusb_send_vendor_frame_qcom;
4691 
4692 		data->recv_intr = btusb_recv_intr_qcom;
4693 		data->recv_bulk = btusb_recv_bulk_qcom;
4694 		data->disconnect = btusb_qcom_disconnect;
4695 
4696 		hdev->manufacturer = 0x1d;
4697 		hdev->send  = btusb_qcom_send_frame;
4698 		hdev->setup = btusb_qcom_setup;
4699 	}
4700 
4701 	if (id->driver_info & BTUSB_AMP) {
4702 		/* AMP controllers do not support SCO packets */
4703 		data->isoc = NULL;
4704 	} else if (data->proto == BTUSB_PROTO_H4) {
4705 		/* In H:4 mode every packet, including SCO/ISO, is carried over
4706 		 * the bulk endpoints, so the isochronous interface must not be
4707 		 * claimed nor have its alternate settings switched.
4708 		 */
4709 		data->isoc = NULL;
4710 	} else {
4711 		/* Interface orders are hardcoded in the specification */
4712 		data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
4713 		data->isoc_ifnum = ifnum_base + 1;
4714 	}
4715 
4716 	if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4717 	    (id->driver_info & BTUSB_REALTEK)) {
4718 		btrtl_set_driver_name(hdev, btusb_driver.name);
4719 		hdev->setup = btusb_setup_realtek;
4720 		hdev->shutdown = btrtl_shutdown_realtek;
4721 		hdev->reset = btusb_rtl_reset;
4722 		hdev->hw_error = btusb_rtl_hw_error;
4723 
4724 		/* Realtek devices need to set remote wakeup on auto-suspend */
4725 		set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
4726 		set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
4727 	}
4728 
4729 	if (id->driver_info & BTUSB_ACTIONS_SEMI) {
4730 		/* Support is advertised, but not implemented */
4731 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_ERR_DATA_REPORTING);
4732 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER);
4733 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT);
4734 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_EXT_SCAN);
4735 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_READ_ENC_KEY_SIZE);
4736 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_EXT_CREATE_CONN);
4737 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_WRITE_AUTH_PAYLOAD_TIMEOUT);
4738 	}
4739 
4740 	if (!reset)
4741 		hci_set_quirk(hdev, HCI_QUIRK_RESET_ON_CLOSE);
4742 
4743 	if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
4744 		if (!disable_scofix)
4745 			hci_set_quirk(hdev, HCI_QUIRK_FIXUP_BUFFER_SIZE);
4746 	}
4747 
4748 	if (id->driver_info & BTUSB_BROKEN_ISOC)
4749 		data->isoc = NULL;
4750 
4751 	if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
4752 		hci_set_quirk(hdev, HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED);
4753 
4754 	if (id->driver_info & BTUSB_INVALID_LE_STATES)
4755 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_LE_STATES);
4756 
4757 	if (id->driver_info & BTUSB_BROKEN_EXT_SCAN)
4758 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_EXT_SCAN);
4759 
4760 	if (id->driver_info & BTUSB_DIGIANSWER) {
4761 		data->cmdreq_type = USB_TYPE_VENDOR;
4762 		hci_set_quirk(hdev, HCI_QUIRK_RESET_ON_CLOSE);
4763 	}
4764 
4765 	if (id->driver_info & BTUSB_CSR) {
4766 		struct usb_device *udev = data->udev;
4767 		u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
4768 
4769 		/* Old firmware would otherwise execute USB reset */
4770 		if (bcdDevice < 0x117)
4771 			hci_set_quirk(hdev, HCI_QUIRK_RESET_ON_CLOSE);
4772 
4773 		/* This must be set first in case we disable it for fakes */
4774 		hci_set_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
4775 
4776 		/* Fake CSR devices with broken commands */
4777 		if (le16_to_cpu(udev->descriptor.idVendor)  == 0x0a12 &&
4778 		    le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4779 			hdev->setup = btusb_setup_csr;
4780 	}
4781 
4782 	if (id->driver_info & BTUSB_SNIFFER) {
4783 		struct usb_device *udev = data->udev;
4784 
4785 		/* New sniffer firmware has crippled HCI interface */
4786 		if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4787 			hci_set_quirk(hdev, HCI_QUIRK_RAW_DEVICE);
4788 	}
4789 
4790 	if (id->driver_info & BTUSB_INTEL_BOOT) {
4791 		/* A bug in the bootloader causes that interrupt interface is
4792 		 * only enabled after receiving SetInterface(0, AltSetting=0).
4793 		 */
4794 		err = usb_set_interface(data->udev, 0, 0);
4795 		if (err < 0) {
4796 			BT_ERR("failed to set interface 0, alt 0 %d", err);
4797 			goto err_kill_tx_urbs;
4798 		}
4799 	}
4800 
4801 	if (data->isoc) {
4802 		err = usb_driver_claim_interface(&btusb_driver,
4803 						 data->isoc, data);
4804 		if (err < 0)
4805 			goto err_kill_tx_urbs;
4806 	}
4807 
4808 	if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
4809 		if (!usb_driver_claim_interface(&btusb_driver,
4810 						data->diag, data))
4811 			__set_diag_interface(hdev);
4812 		else
4813 			data->diag = NULL;
4814 	}
4815 
4816 	if (enable_autosuspend)
4817 		usb_enable_autosuspend(data->udev);
4818 
4819 	if (data->proto == BTUSB_PROTO_LEGACY)
4820 		data->poll_sync = enable_poll_sync;
4821 
4822 	err = hci_register_dev(hdev);
4823 	if (err < 0)
4824 		goto err_release_siblings;
4825 
4826 	usb_set_intfdata(intf, data);
4827 
4828 	debugfs_create_file("force_poll_sync", 0644, hdev->debugfs, data,
4829 			    &force_poll_sync_fops);
4830 
4831 	return 0;
4832 
4833 err_release_siblings:
4834 	if (data->diag) {
4835 		usb_set_intfdata(data->diag, NULL);
4836 		usb_driver_release_interface(&btusb_driver, data->diag);
4837 	}
4838 	if (data->isoc) {
4839 		usb_set_intfdata(data->isoc, NULL);
4840 		usb_driver_release_interface(&btusb_driver, data->isoc);
4841 	}
4842 err_kill_tx_urbs:
4843 	usb_kill_anchored_urbs(&data->tx_anchor);
4844 err_disable_wakeup:
4845 	if (data->oob_wake_irq) {
4846 		device_init_wakeup(&data->udev->dev, false);
4847 		free_irq(data->oob_wake_irq, data);
4848 	}
4849 err_put_reset:
4850 	if (data->reset_gpio)
4851 		gpiod_put(data->reset_gpio);
4852 err_free_hdev:
4853 	hci_free_dev(hdev);
4854 err_free_data:
4855 	kfree(data);
4856 
4857 	return err;
4858 }
4859 
4860 static void btusb_disconnect(struct usb_interface *intf)
4861 {
4862 	struct btusb_data *data = usb_get_intfdata(intf);
4863 	struct hci_dev *hdev;
4864 
4865 	BT_DBG("intf %p", intf);
4866 
4867 	if (!data)
4868 		return;
4869 
4870 	hdev = data->hdev;
4871 	usb_set_intfdata(data->intf, NULL);
4872 
4873 	if (data->isoc)
4874 		usb_set_intfdata(data->isoc, NULL);
4875 
4876 	if (data->diag)
4877 		usb_set_intfdata(data->diag, NULL);
4878 
4879 	if (data->disconnect)
4880 		data->disconnect(hdev);
4881 
4882 	hci_unregister_dev(hdev);
4883 
4884 	if (data->oob_wake_irq) {
4885 		device_init_wakeup(&data->udev->dev, false);
4886 		free_irq(data->oob_wake_irq, data);
4887 	}
4888 
4889 	if (data->reset_gpio)
4890 		gpiod_put(data->reset_gpio);
4891 
4892 	if (test_and_clear_bit(BTUSB_RESET, &data->flags))
4893 		usb_autopm_put_interface_no_suspend(data->intf);
4894 
4895 	if (intf == data->intf) {
4896 		if (data->isoc)
4897 			usb_driver_release_interface(&btusb_driver, data->isoc);
4898 		if (data->diag)
4899 			usb_driver_release_interface(&btusb_driver, data->diag);
4900 	} else if (intf == data->isoc) {
4901 		if (data->diag)
4902 			usb_driver_release_interface(&btusb_driver, data->diag);
4903 		usb_driver_release_interface(&btusb_driver, data->intf);
4904 	} else if (intf == data->diag) {
4905 		if (data->isoc)
4906 			usb_driver_release_interface(&btusb_driver, data->isoc);
4907 		usb_driver_release_interface(&btusb_driver, data->intf);
4908 	}
4909 
4910 	hci_free_dev(hdev);
4911 	kfree(data);
4912 }
4913 
4914 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4915 {
4916 	struct btusb_data *data = usb_get_intfdata(intf);
4917 
4918 	BT_DBG("intf %p", intf);
4919 
4920 	/*
4921 	 * It is reported that remote wakeup events could sometimes cause some
4922 	 * adapters completely unresponsive. Resetting the xHCI root hub doesn't
4923 	 * help at all, and recovering from such a state needs a power cycle.
4924 	 * Since disabling remote wakeup simply causes the USB core to gate
4925 	 * runtime autosuspend as well due to needs_remote_wakeup == 1, let's do
4926 	 * this ourselves to make our life easier. The interface can be safely
4927 	 * autosuspended as long as remote wakeup is disabled, i.e., after
4928 	 * closing the HCI device.
4929 	 *
4930 	 * Don't auto-suspend if there are connections or discovery in progress.
4931 	 *
4932 	 * External suspend calls shall never fail. Specifically, a device with
4933 	 * broken remote wakeup may still take the advantage of remote wakeup in
4934 	 * order to wake up the system from sleep if userspace has enabled it as
4935 	 * a wakeup source.
4936 	 */
4937 	if (PMSG_IS_AUTO(message) &&
4938 	    ((test_bit(BTUSB_WAKEUP_BROKEN, &data->flags) && data->intf->needs_remote_wakeup) ||
4939 	     hci_conn_count(data->hdev) || hci_discovery_active(data->hdev)))
4940 		return -EBUSY;
4941 
4942 	if (data->suspend_count++)
4943 		return 0;
4944 
4945 	spin_lock_irq(&data->txlock);
4946 	if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4947 		set_bit(BTUSB_SUSPENDING, &data->flags);
4948 		spin_unlock_irq(&data->txlock);
4949 	} else {
4950 		spin_unlock_irq(&data->txlock);
4951 		data->suspend_count--;
4952 		return -EBUSY;
4953 	}
4954 
4955 	cancel_work_sync(&data->work);
4956 
4957 	if (data->suspend)
4958 		data->suspend(data->hdev);
4959 
4960 	btusb_stop_traffic(data);
4961 	usb_kill_anchored_urbs(&data->tx_anchor);
4962 
4963 	if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4964 		set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4965 		enable_irq_wake(data->oob_wake_irq);
4966 		enable_irq(data->oob_wake_irq);
4967 	}
4968 
4969 	/* For global suspend, Realtek devices lose the loaded fw
4970 	 * in them. But for autosuspend, firmware should remain.
4971 	 * Actually, it depends on whether the usb host sends
4972 	 * set feature (enable wakeup) or not.
4973 	 */
4974 	if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
4975 		if (PMSG_IS_AUTO(message) &&
4976 		    device_can_wakeup(&data->udev->dev))
4977 			data->udev->do_remote_wakeup = 1;
4978 		else if (!PMSG_IS_AUTO(message) &&
4979 			 !device_may_wakeup(&data->udev->dev)) {
4980 			data->udev->do_remote_wakeup = 0;
4981 			data->udev->reset_resume = 1;
4982 		}
4983 	}
4984 
4985 	return 0;
4986 }
4987 
4988 static void play_deferred(struct btusb_data *data)
4989 {
4990 	struct urb *urb;
4991 	int err;
4992 
4993 	while ((urb = usb_get_from_anchor(&data->deferred))) {
4994 		usb_anchor_urb(urb, &data->tx_anchor);
4995 
4996 		err = usb_submit_urb(urb, GFP_ATOMIC);
4997 		if (err < 0) {
4998 			if (err != -EPERM && err != -ENODEV)
4999 				BT_ERR("%s urb %p submission failed (%d)",
5000 				       data->hdev->name, urb, -err);
5001 			kfree(urb->setup_packet);
5002 			usb_unanchor_urb(urb);
5003 			usb_free_urb(urb);
5004 			break;
5005 		}
5006 
5007 		data->tx_in_flight++;
5008 		usb_free_urb(urb);
5009 	}
5010 
5011 	/* Cleanup the rest deferred urbs. */
5012 	while ((urb = usb_get_from_anchor(&data->deferred))) {
5013 		kfree(urb->setup_packet);
5014 		usb_free_urb(urb);
5015 	}
5016 }
5017 
5018 static int btusb_resume(struct usb_interface *intf)
5019 {
5020 	struct btusb_data *data = usb_get_intfdata(intf);
5021 	struct hci_dev *hdev = data->hdev;
5022 	int err = 0;
5023 
5024 	BT_DBG("intf %p", intf);
5025 
5026 	if (--data->suspend_count)
5027 		return 0;
5028 
5029 	/* Disable only if not already disabled (keep it balanced) */
5030 	if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
5031 		disable_irq(data->oob_wake_irq);
5032 		disable_irq_wake(data->oob_wake_irq);
5033 	}
5034 
5035 	if (!test_bit(HCI_RUNNING, &hdev->flags))
5036 		goto done;
5037 
5038 	if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
5039 		err = btusb_submit_intr_urb(hdev, GFP_NOIO);
5040 		if (err < 0) {
5041 			clear_bit(BTUSB_INTR_RUNNING, &data->flags);
5042 			goto failed;
5043 		}
5044 	}
5045 
5046 	if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
5047 		err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
5048 		if (err < 0) {
5049 			clear_bit(BTUSB_BULK_RUNNING, &data->flags);
5050 			goto failed;
5051 		}
5052 
5053 		btusb_submit_bulk_urb(hdev, GFP_NOIO);
5054 	}
5055 
5056 	if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
5057 		if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
5058 			clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
5059 		else
5060 			btusb_submit_isoc_urb(hdev, GFP_NOIO);
5061 	}
5062 
5063 	if (data->resume)
5064 		data->resume(hdev);
5065 
5066 	spin_lock_irq(&data->txlock);
5067 	play_deferred(data);
5068 	clear_bit(BTUSB_SUSPENDING, &data->flags);
5069 	spin_unlock_irq(&data->txlock);
5070 	schedule_work(&data->work);
5071 
5072 	return 0;
5073 
5074 failed:
5075 	usb_scuttle_anchored_urbs(&data->deferred);
5076 done:
5077 	spin_lock_irq(&data->txlock);
5078 	clear_bit(BTUSB_SUSPENDING, &data->flags);
5079 	spin_unlock_irq(&data->txlock);
5080 
5081 	return err;
5082 }
5083 
5084 #ifdef CONFIG_DEV_COREDUMP
5085 static void btusb_coredump(struct device *dev)
5086 {
5087 	struct btusb_data *data = dev_get_drvdata(dev);
5088 	struct hci_dev *hdev = data->hdev;
5089 
5090 	if (hdev->dump.coredump)
5091 		hdev->dump.coredump(hdev);
5092 }
5093 #endif
5094 
5095 static struct usb_driver btusb_driver = {
5096 	.name		= "btusb",
5097 	.probe		= btusb_probe,
5098 	.disconnect	= btusb_disconnect,
5099 	.suspend	= pm_ptr(btusb_suspend),
5100 	.resume		= pm_ptr(btusb_resume),
5101 	.id_table	= btusb_table,
5102 	.supports_autosuspend = 1,
5103 	.disable_hub_initiated_lpm = 1,
5104 
5105 #ifdef CONFIG_DEV_COREDUMP
5106 	.driver = {
5107 		.coredump = btusb_coredump,
5108 	},
5109 #endif
5110 };
5111 
5112 module_usb_driver(btusb_driver);
5113 
5114 module_param(disable_scofix, bool, 0644);
5115 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
5116 
5117 module_param(force_scofix, bool, 0644);
5118 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
5119 
5120 module_param(enable_autosuspend, bool, 0644);
5121 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
5122 
5123 module_param(reset, bool, 0644);
5124 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
5125 
5126 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
5127 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
5128 MODULE_VERSION(VERSION);
5129 MODULE_LICENSE("GPL");
5130