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