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