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