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