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