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