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