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