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