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