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