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