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