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