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