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