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