xref: /openbmc/linux/drivers/bluetooth/btusb.c (revision 70151949)
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 	if (data->evt_skb == NULL)
2822 		goto err_free_wc;
2823 
2824 	/* Parse and handle the return WMT event */
2825 	wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
2826 	if (wmt_evt->whdr.op != hdr->op) {
2827 		bt_dev_err(hdev, "Wrong op received %d expected %d",
2828 			   wmt_evt->whdr.op, hdr->op);
2829 		err = -EIO;
2830 		goto err_free_skb;
2831 	}
2832 
2833 	switch (wmt_evt->whdr.op) {
2834 	case BTMTK_WMT_SEMAPHORE:
2835 		if (wmt_evt->whdr.flag == 2)
2836 			status = BTMTK_WMT_PATCH_UNDONE;
2837 		else
2838 			status = BTMTK_WMT_PATCH_DONE;
2839 		break;
2840 	case BTMTK_WMT_FUNC_CTRL:
2841 		wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
2842 		if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
2843 			status = BTMTK_WMT_ON_DONE;
2844 		else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
2845 			status = BTMTK_WMT_ON_PROGRESS;
2846 		else
2847 			status = BTMTK_WMT_ON_UNDONE;
2848 		break;
2849 	case BTMTK_WMT_PATCH_DWNLD:
2850 		if (wmt_evt->whdr.flag == 2)
2851 			status = BTMTK_WMT_PATCH_DONE;
2852 		else if (wmt_evt->whdr.flag == 1)
2853 			status = BTMTK_WMT_PATCH_PROGRESS;
2854 		else
2855 			status = BTMTK_WMT_PATCH_UNDONE;
2856 		break;
2857 	}
2858 
2859 	if (wmt_params->status)
2860 		*wmt_params->status = status;
2861 
2862 err_free_skb:
2863 	kfree_skb(data->evt_skb);
2864 	data->evt_skb = NULL;
2865 err_free_wc:
2866 	kfree(wc);
2867 	return err;
2868 }
2869 
2870 static int btusb_mtk_func_query(struct hci_dev *hdev)
2871 {
2872 	struct btmtk_hci_wmt_params wmt_params;
2873 	int status, err;
2874 	u8 param = 0;
2875 
2876 	/* Query whether the function is enabled */
2877 	wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2878 	wmt_params.flag = 4;
2879 	wmt_params.dlen = sizeof(param);
2880 	wmt_params.data = &param;
2881 	wmt_params.status = &status;
2882 
2883 	err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2884 	if (err < 0) {
2885 		bt_dev_err(hdev, "Failed to query function status (%d)", err);
2886 		return err;
2887 	}
2888 
2889 	return status;
2890 }
2891 
2892 static int btusb_mtk_uhw_reg_write(struct btusb_data *data, u32 reg, u32 val)
2893 {
2894 	struct hci_dev *hdev = data->hdev;
2895 	int pipe, err;
2896 	void *buf;
2897 
2898 	buf = kzalloc(4, GFP_KERNEL);
2899 	if (!buf)
2900 		return -ENOMEM;
2901 
2902 	put_unaligned_le32(val, buf);
2903 
2904 	pipe = usb_sndctrlpipe(data->udev, 0);
2905 	err = usb_control_msg(data->udev, pipe, 0x02,
2906 			      0x5E,
2907 			      reg >> 16, reg & 0xffff,
2908 			      buf, 4, USB_CTRL_SET_TIMEOUT);
2909 	if (err < 0) {
2910 		bt_dev_err(hdev, "Failed to write uhw reg(%d)", err);
2911 		goto err_free_buf;
2912 	}
2913 
2914 err_free_buf:
2915 	kfree(buf);
2916 
2917 	return err;
2918 }
2919 
2920 static int btusb_mtk_uhw_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2921 {
2922 	struct hci_dev *hdev = data->hdev;
2923 	int pipe, err;
2924 	void *buf;
2925 
2926 	buf = kzalloc(4, GFP_KERNEL);
2927 	if (!buf)
2928 		return -ENOMEM;
2929 
2930 	pipe = usb_rcvctrlpipe(data->udev, 0);
2931 	err = usb_control_msg(data->udev, pipe, 0x01,
2932 			      0xDE,
2933 			      reg >> 16, reg & 0xffff,
2934 			      buf, 4, USB_CTRL_SET_TIMEOUT);
2935 	if (err < 0) {
2936 		bt_dev_err(hdev, "Failed to read uhw reg(%d)", err);
2937 		goto err_free_buf;
2938 	}
2939 
2940 	*val = get_unaligned_le32(buf);
2941 	bt_dev_dbg(hdev, "reg=%x, value=0x%08x", reg, *val);
2942 
2943 err_free_buf:
2944 	kfree(buf);
2945 
2946 	return err;
2947 }
2948 
2949 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2950 {
2951 	int pipe, err, size = sizeof(u32);
2952 	void *buf;
2953 
2954 	buf = kzalloc(size, GFP_KERNEL);
2955 	if (!buf)
2956 		return -ENOMEM;
2957 
2958 	pipe = usb_rcvctrlpipe(data->udev, 0);
2959 	err = usb_control_msg(data->udev, pipe, 0x63,
2960 			      USB_TYPE_VENDOR | USB_DIR_IN,
2961 			      reg >> 16, reg & 0xffff,
2962 			      buf, size, USB_CTRL_SET_TIMEOUT);
2963 	if (err < 0)
2964 		goto err_free_buf;
2965 
2966 	*val = get_unaligned_le32(buf);
2967 
2968 err_free_buf:
2969 	kfree(buf);
2970 
2971 	return err;
2972 }
2973 
2974 static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
2975 {
2976 	return btusb_mtk_reg_read(data, reg, id);
2977 }
2978 
2979 static u32 btusb_mtk_reset_done(struct hci_dev *hdev)
2980 {
2981 	struct btusb_data *data = hci_get_drvdata(hdev);
2982 	u32 val = 0;
2983 
2984 	btusb_mtk_uhw_reg_read(data, MTK_BT_MISC, &val);
2985 
2986 	return val & MTK_BT_RST_DONE;
2987 }
2988 
2989 static int btusb_mtk_reset(struct hci_dev *hdev, void *rst_data)
2990 {
2991 	struct btusb_data *data = hci_get_drvdata(hdev);
2992 	struct btmediatek_data *mediatek;
2993 	u32 val;
2994 	int err;
2995 
2996 	/* It's MediaTek specific bluetooth reset mechanism via USB */
2997 	if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
2998 		bt_dev_err(hdev, "last reset failed? Not resetting again");
2999 		return -EBUSY;
3000 	}
3001 
3002 	err = usb_autopm_get_interface(data->intf);
3003 	if (err < 0)
3004 		return err;
3005 
3006 	btusb_stop_traffic(data);
3007 	usb_kill_anchored_urbs(&data->tx_anchor);
3008 	mediatek = hci_get_priv(hdev);
3009 
3010 	if (mediatek->dev_id == 0x7925) {
3011 		btusb_mtk_uhw_reg_read(data, MTK_BT_RESET_REG_CONNV3, &val);
3012 		val |= (1 << 5);
3013 		btusb_mtk_uhw_reg_write(data, MTK_BT_RESET_REG_CONNV3, val);
3014 		btusb_mtk_uhw_reg_read(data, MTK_BT_RESET_REG_CONNV3, &val);
3015 		val &= 0xFFFF00FF;
3016 		val |= (1 << 13);
3017 		btusb_mtk_uhw_reg_write(data, MTK_BT_RESET_REG_CONNV3, val);
3018 		btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, 0x00010001);
3019 		btusb_mtk_uhw_reg_read(data, MTK_BT_RESET_REG_CONNV3, &val);
3020 		val |= (1 << 0);
3021 		btusb_mtk_uhw_reg_write(data, MTK_BT_RESET_REG_CONNV3, val);
3022 		btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT, 0x000000FF);
3023 		btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT, &val);
3024 		btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT1, 0x000000FF);
3025 		btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT1, &val);
3026 		msleep(100);
3027 	} else {
3028 		/* It's Device EndPoint Reset Option Register */
3029 		bt_dev_dbg(hdev, "Initiating reset mechanism via uhw");
3030 		btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
3031 		btusb_mtk_uhw_reg_read(data, MTK_BT_WDT_STATUS, &val);
3032 
3033 		/* Reset the bluetooth chip via USB interface. */
3034 		btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 1);
3035 		btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT, 0x000000FF);
3036 		btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT, &val);
3037 		btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT1, 0x000000FF);
3038 		btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT1, &val);
3039 		/* MT7921 need to delay 20ms between toggle reset bit */
3040 		msleep(20);
3041 		btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 0);
3042 		btusb_mtk_uhw_reg_read(data, MTK_BT_SUBSYS_RST, &val);
3043 	}
3044 
3045 	err = readx_poll_timeout(btusb_mtk_reset_done, hdev, val,
3046 				 val & MTK_BT_RST_DONE, 20000, 1000000);
3047 	if (err < 0)
3048 		bt_dev_err(hdev, "Reset timeout");
3049 
3050 	btusb_mtk_id_get(data, 0x70010200, &val);
3051 	if (!val)
3052 		bt_dev_err(hdev, "Can't get device id, subsys reset fail.");
3053 
3054 	usb_queue_reset_device(data->intf);
3055 
3056 	clear_bit(BTUSB_HW_RESET_ACTIVE, &data->flags);
3057 
3058 	return err;
3059 }
3060 
3061 static int btusb_mtk_setup(struct hci_dev *hdev)
3062 {
3063 	struct btusb_data *data = hci_get_drvdata(hdev);
3064 	struct btmtk_hci_wmt_params wmt_params;
3065 	ktime_t calltime, delta, rettime;
3066 	struct btmtk_tci_sleep tci_sleep;
3067 	unsigned long long duration;
3068 	struct sk_buff *skb;
3069 	const char *fwname;
3070 	int err, status;
3071 	u32 dev_id = 0;
3072 	char fw_bin_name[64];
3073 	u32 fw_version = 0;
3074 	u8 param;
3075 	struct btmediatek_data *mediatek;
3076 
3077 	calltime = ktime_get();
3078 
3079 	err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
3080 	if (err < 0) {
3081 		bt_dev_err(hdev, "Failed to get device id (%d)", err);
3082 		return err;
3083 	}
3084 
3085 	if (!dev_id || dev_id != 0x7663) {
3086 		err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
3087 		if (err < 0) {
3088 			bt_dev_err(hdev, "Failed to get device id (%d)", err);
3089 			return err;
3090 		}
3091 		err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
3092 		if (err < 0) {
3093 			bt_dev_err(hdev, "Failed to get fw version (%d)", err);
3094 			return err;
3095 		}
3096 	}
3097 
3098 	mediatek = hci_get_priv(hdev);
3099 	mediatek->dev_id = dev_id;
3100 	mediatek->reset_sync = btusb_mtk_reset;
3101 
3102 	err = btmtk_register_coredump(hdev, btusb_driver.name, fw_version);
3103 	if (err < 0)
3104 		bt_dev_err(hdev, "Failed to register coredump (%d)", err);
3105 
3106 	switch (dev_id) {
3107 	case 0x7663:
3108 		fwname = FIRMWARE_MT7663;
3109 		break;
3110 	case 0x7668:
3111 		fwname = FIRMWARE_MT7668;
3112 		break;
3113 	case 0x7922:
3114 	case 0x7961:
3115 	case 0x7925:
3116 		if (dev_id == 0x7925)
3117 			snprintf(fw_bin_name, sizeof(fw_bin_name),
3118 				 "mediatek/mt%04x/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
3119 				 dev_id & 0xffff, dev_id & 0xffff, (fw_version & 0xff) + 1);
3120 		else
3121 			snprintf(fw_bin_name, sizeof(fw_bin_name),
3122 				 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
3123 				 dev_id & 0xffff, (fw_version & 0xff) + 1);
3124 
3125 		err = btmtk_setup_firmware_79xx(hdev, fw_bin_name,
3126 						btusb_mtk_hci_wmt_sync);
3127 		if (err < 0) {
3128 			bt_dev_err(hdev, "Failed to set up firmware (%d)", err);
3129 			return err;
3130 		}
3131 
3132 		/* It's Device EndPoint Reset Option Register */
3133 		btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
3134 
3135 		/* Enable Bluetooth protocol */
3136 		param = 1;
3137 		wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3138 		wmt_params.flag = 0;
3139 		wmt_params.dlen = sizeof(param);
3140 		wmt_params.data = &param;
3141 		wmt_params.status = NULL;
3142 
3143 		err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3144 		if (err < 0) {
3145 			bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3146 			return err;
3147 		}
3148 
3149 		hci_set_msft_opcode(hdev, 0xFD30);
3150 		hci_set_aosp_capable(hdev);
3151 		goto done;
3152 	default:
3153 		bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
3154 			   dev_id);
3155 		return -ENODEV;
3156 	}
3157 
3158 	/* Query whether the firmware is already download */
3159 	wmt_params.op = BTMTK_WMT_SEMAPHORE;
3160 	wmt_params.flag = 1;
3161 	wmt_params.dlen = 0;
3162 	wmt_params.data = NULL;
3163 	wmt_params.status = &status;
3164 
3165 	err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3166 	if (err < 0) {
3167 		bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
3168 		return err;
3169 	}
3170 
3171 	if (status == BTMTK_WMT_PATCH_DONE) {
3172 		bt_dev_info(hdev, "firmware already downloaded");
3173 		goto ignore_setup_fw;
3174 	}
3175 
3176 	/* Setup a firmware which the device definitely requires */
3177 	err = btmtk_setup_firmware(hdev, fwname,
3178 				   btusb_mtk_hci_wmt_sync);
3179 	if (err < 0)
3180 		return err;
3181 
3182 ignore_setup_fw:
3183 	err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
3184 				 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
3185 				 2000, 5000000);
3186 	/* -ETIMEDOUT happens */
3187 	if (err < 0)
3188 		return err;
3189 
3190 	/* The other errors happen in btusb_mtk_func_query */
3191 	if (status < 0)
3192 		return status;
3193 
3194 	if (status == BTMTK_WMT_ON_DONE) {
3195 		bt_dev_info(hdev, "function already on");
3196 		goto ignore_func_on;
3197 	}
3198 
3199 	/* Enable Bluetooth protocol */
3200 	param = 1;
3201 	wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3202 	wmt_params.flag = 0;
3203 	wmt_params.dlen = sizeof(param);
3204 	wmt_params.data = &param;
3205 	wmt_params.status = NULL;
3206 
3207 	err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3208 	if (err < 0) {
3209 		bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3210 		return err;
3211 	}
3212 
3213 ignore_func_on:
3214 	/* Apply the low power environment setup */
3215 	tci_sleep.mode = 0x5;
3216 	tci_sleep.duration = cpu_to_le16(0x640);
3217 	tci_sleep.host_duration = cpu_to_le16(0x640);
3218 	tci_sleep.host_wakeup_pin = 0;
3219 	tci_sleep.time_compensation = 0;
3220 
3221 	skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
3222 			     HCI_INIT_TIMEOUT);
3223 	if (IS_ERR(skb)) {
3224 		err = PTR_ERR(skb);
3225 		bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
3226 		return err;
3227 	}
3228 	kfree_skb(skb);
3229 
3230 done:
3231 	rettime = ktime_get();
3232 	delta = ktime_sub(rettime, calltime);
3233 	duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3234 
3235 	bt_dev_info(hdev, "Device setup in %llu usecs", duration);
3236 
3237 	return 0;
3238 }
3239 
3240 static int btusb_mtk_shutdown(struct hci_dev *hdev)
3241 {
3242 	struct btmtk_hci_wmt_params wmt_params;
3243 	u8 param = 0;
3244 	int err;
3245 
3246 	/* Disable the device */
3247 	wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3248 	wmt_params.flag = 0;
3249 	wmt_params.dlen = sizeof(param);
3250 	wmt_params.data = &param;
3251 	wmt_params.status = NULL;
3252 
3253 	err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3254 	if (err < 0) {
3255 		bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3256 		return err;
3257 	}
3258 
3259 	return 0;
3260 }
3261 
3262 static int btusb_recv_acl_mtk(struct hci_dev *hdev, struct sk_buff *skb)
3263 {
3264 	struct btusb_data *data = hci_get_drvdata(hdev);
3265 	u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
3266 	struct sk_buff *skb_cd;
3267 
3268 	switch (handle) {
3269 	case 0xfc6f:		/* Firmware dump from device */
3270 		/* When the firmware hangs, the device can no longer
3271 		 * suspend and thus disable auto-suspend.
3272 		 */
3273 		usb_disable_autosuspend(data->udev);
3274 
3275 		/* We need to forward the diagnostic packet to userspace daemon
3276 		 * for backward compatibility, so we have to clone the packet
3277 		 * extraly for the in-kernel coredump support.
3278 		 */
3279 		skb_cd = skb_clone(skb, GFP_ATOMIC);
3280 		if (skb_cd)
3281 			btmtk_process_coredump(hdev, skb_cd);
3282 
3283 		fallthrough;
3284 	case 0x05ff:		/* Firmware debug logging 1 */
3285 	case 0x05fe:		/* Firmware debug logging 2 */
3286 		return hci_recv_diag(hdev, skb);
3287 	}
3288 
3289 	return hci_recv_frame(hdev, skb);
3290 }
3291 
3292 #ifdef CONFIG_PM
3293 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3294 static int marvell_config_oob_wake(struct hci_dev *hdev)
3295 {
3296 	struct sk_buff *skb;
3297 	struct btusb_data *data = hci_get_drvdata(hdev);
3298 	struct device *dev = &data->udev->dev;
3299 	u16 pin, gap, opcode;
3300 	int ret;
3301 	u8 cmd[5];
3302 
3303 	/* Move on if no wakeup pin specified */
3304 	if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3305 	    of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3306 		return 0;
3307 
3308 	/* Vendor specific command to configure a GPIO as wake-up pin */
3309 	opcode = hci_opcode_pack(0x3F, 0x59);
3310 	cmd[0] = opcode & 0xFF;
3311 	cmd[1] = opcode >> 8;
3312 	cmd[2] = 2; /* length of parameters that follow */
3313 	cmd[3] = pin;
3314 	cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3315 
3316 	skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3317 	if (!skb) {
3318 		bt_dev_err(hdev, "%s: No memory", __func__);
3319 		return -ENOMEM;
3320 	}
3321 
3322 	skb_put_data(skb, cmd, sizeof(cmd));
3323 	hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3324 
3325 	ret = btusb_send_frame(hdev, skb);
3326 	if (ret) {
3327 		bt_dev_err(hdev, "%s: configuration failed", __func__);
3328 		kfree_skb(skb);
3329 		return ret;
3330 	}
3331 
3332 	return 0;
3333 }
3334 #endif
3335 
3336 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3337 				    const bdaddr_t *bdaddr)
3338 {
3339 	struct sk_buff *skb;
3340 	u8 buf[8];
3341 	long ret;
3342 
3343 	buf[0] = 0xfe;
3344 	buf[1] = sizeof(bdaddr_t);
3345 	memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3346 
3347 	skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3348 	if (IS_ERR(skb)) {
3349 		ret = PTR_ERR(skb);
3350 		bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3351 			   ret);
3352 		return ret;
3353 	}
3354 	kfree_skb(skb);
3355 
3356 	return 0;
3357 }
3358 
3359 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3360 				    const bdaddr_t *bdaddr)
3361 {
3362 	struct sk_buff *skb;
3363 	u8 buf[10];
3364 	long ret;
3365 
3366 	buf[0] = 0x01;
3367 	buf[1] = 0x01;
3368 	buf[2] = 0x00;
3369 	buf[3] = sizeof(bdaddr_t);
3370 	memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3371 
3372 	skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3373 	if (IS_ERR(skb)) {
3374 		ret = PTR_ERR(skb);
3375 		bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3376 		return ret;
3377 	}
3378 	kfree_skb(skb);
3379 
3380 	return 0;
3381 }
3382 
3383 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3384 				const bdaddr_t *bdaddr)
3385 {
3386 	struct sk_buff *skb;
3387 	u8 buf[6];
3388 	long ret;
3389 
3390 	memcpy(buf, bdaddr, sizeof(bdaddr_t));
3391 
3392 	skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3393 				HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3394 	if (IS_ERR(skb)) {
3395 		ret = PTR_ERR(skb);
3396 		bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3397 		return ret;
3398 	}
3399 	kfree_skb(skb);
3400 
3401 	return 0;
3402 }
3403 
3404 #define QCA_MEMDUMP_ACL_HANDLE 0x2EDD
3405 #define QCA_MEMDUMP_SIZE_MAX  0x100000
3406 #define QCA_MEMDUMP_VSE_CLASS 0x01
3407 #define QCA_MEMDUMP_MSG_TYPE 0x08
3408 #define QCA_MEMDUMP_PKT_SIZE 248
3409 #define QCA_LAST_SEQUENCE_NUM 0xffff
3410 
3411 struct qca_dump_hdr {
3412 	u8 vse_class;
3413 	u8 msg_type;
3414 	__le16 seqno;
3415 	u8 reserved;
3416 	union {
3417 		u8 data[0];
3418 		struct {
3419 			__le32 ram_dump_size;
3420 			u8 data0[0];
3421 		} __packed;
3422 	};
3423 } __packed;
3424 
3425 
3426 static void btusb_dump_hdr_qca(struct hci_dev *hdev, struct sk_buff *skb)
3427 {
3428 	char buf[128];
3429 	struct btusb_data *btdata = hci_get_drvdata(hdev);
3430 
3431 	snprintf(buf, sizeof(buf), "Controller Name: 0x%x\n",
3432 			btdata->qca_dump.controller_id);
3433 	skb_put_data(skb, buf, strlen(buf));
3434 
3435 	snprintf(buf, sizeof(buf), "Firmware Version: 0x%x\n",
3436 			btdata->qca_dump.fw_version);
3437 	skb_put_data(skb, buf, strlen(buf));
3438 
3439 	snprintf(buf, sizeof(buf), "Driver: %s\nVendor: qca\n",
3440 			btusb_driver.name);
3441 	skb_put_data(skb, buf, strlen(buf));
3442 
3443 	snprintf(buf, sizeof(buf), "VID: 0x%x\nPID:0x%x\n",
3444 			btdata->qca_dump.id_vendor, btdata->qca_dump.id_product);
3445 	skb_put_data(skb, buf, strlen(buf));
3446 
3447 	snprintf(buf, sizeof(buf), "Lmp Subversion: 0x%x\n",
3448 			hdev->lmp_subver);
3449 	skb_put_data(skb, buf, strlen(buf));
3450 }
3451 
3452 static void btusb_coredump_qca(struct hci_dev *hdev)
3453 {
3454 	static const u8 param[] = { 0x26 };
3455 	struct sk_buff *skb;
3456 
3457 	skb = __hci_cmd_sync(hdev, 0xfc0c, 1, param, HCI_CMD_TIMEOUT);
3458 	if (IS_ERR(skb))
3459 		bt_dev_err(hdev, "%s: triggle crash failed (%ld)", __func__, PTR_ERR(skb));
3460 	kfree_skb(skb);
3461 }
3462 
3463 /*
3464  * ==0: not a dump pkt.
3465  * < 0: fails to handle a dump pkt
3466  * > 0: otherwise.
3467  */
3468 static int handle_dump_pkt_qca(struct hci_dev *hdev, struct sk_buff *skb)
3469 {
3470 	int ret = 1;
3471 	u8 pkt_type;
3472 	u8 *sk_ptr;
3473 	unsigned int sk_len;
3474 	u16 seqno;
3475 	u32 dump_size;
3476 
3477 	struct hci_event_hdr *event_hdr;
3478 	struct hci_acl_hdr *acl_hdr;
3479 	struct qca_dump_hdr *dump_hdr;
3480 	struct btusb_data *btdata = hci_get_drvdata(hdev);
3481 	struct usb_device *udev = btdata->udev;
3482 
3483 	pkt_type = hci_skb_pkt_type(skb);
3484 	sk_ptr = skb->data;
3485 	sk_len = skb->len;
3486 
3487 	if (pkt_type == HCI_ACLDATA_PKT) {
3488 		acl_hdr = hci_acl_hdr(skb);
3489 		if (le16_to_cpu(acl_hdr->handle) != QCA_MEMDUMP_ACL_HANDLE)
3490 			return 0;
3491 		sk_ptr += HCI_ACL_HDR_SIZE;
3492 		sk_len -= HCI_ACL_HDR_SIZE;
3493 		event_hdr = (struct hci_event_hdr *)sk_ptr;
3494 	} else {
3495 		event_hdr = hci_event_hdr(skb);
3496 	}
3497 
3498 	if ((event_hdr->evt != HCI_VENDOR_PKT)
3499 		|| (event_hdr->plen != (sk_len - HCI_EVENT_HDR_SIZE)))
3500 		return 0;
3501 
3502 	sk_ptr += HCI_EVENT_HDR_SIZE;
3503 	sk_len -= HCI_EVENT_HDR_SIZE;
3504 
3505 	dump_hdr = (struct qca_dump_hdr *)sk_ptr;
3506 	if ((sk_len < offsetof(struct qca_dump_hdr, data))
3507 		|| (dump_hdr->vse_class != QCA_MEMDUMP_VSE_CLASS)
3508 	    || (dump_hdr->msg_type != QCA_MEMDUMP_MSG_TYPE))
3509 		return 0;
3510 
3511 	/*it is dump pkt now*/
3512 	seqno = le16_to_cpu(dump_hdr->seqno);
3513 	if (seqno == 0) {
3514 		set_bit(BTUSB_HW_SSR_ACTIVE, &btdata->flags);
3515 		dump_size = le32_to_cpu(dump_hdr->ram_dump_size);
3516 		if (!dump_size || (dump_size > QCA_MEMDUMP_SIZE_MAX)) {
3517 			ret = -EILSEQ;
3518 			bt_dev_err(hdev, "Invalid memdump size(%u)",
3519 				   dump_size);
3520 			goto out;
3521 		}
3522 
3523 		ret = hci_devcd_init(hdev, dump_size);
3524 		if (ret < 0) {
3525 			bt_dev_err(hdev, "memdump init error(%d)", ret);
3526 			goto out;
3527 		}
3528 
3529 		btdata->qca_dump.ram_dump_size = dump_size;
3530 		btdata->qca_dump.ram_dump_seqno = 0;
3531 		sk_ptr += offsetof(struct qca_dump_hdr, data0);
3532 		sk_len -= offsetof(struct qca_dump_hdr, data0);
3533 
3534 		usb_disable_autosuspend(udev);
3535 		bt_dev_info(hdev, "%s memdump size(%u)\n",
3536 			    (pkt_type == HCI_ACLDATA_PKT) ? "ACL" : "event",
3537 			    dump_size);
3538 	} else {
3539 		sk_ptr += offsetof(struct qca_dump_hdr, data);
3540 		sk_len -= offsetof(struct qca_dump_hdr, data);
3541 	}
3542 
3543 	if (!btdata->qca_dump.ram_dump_size) {
3544 		ret = -EINVAL;
3545 		bt_dev_err(hdev, "memdump is not active");
3546 		goto out;
3547 	}
3548 
3549 	if ((seqno > btdata->qca_dump.ram_dump_seqno + 1) && (seqno != QCA_LAST_SEQUENCE_NUM)) {
3550 		dump_size = QCA_MEMDUMP_PKT_SIZE * (seqno - btdata->qca_dump.ram_dump_seqno - 1);
3551 		hci_devcd_append_pattern(hdev, 0x0, dump_size);
3552 		bt_dev_err(hdev,
3553 			   "expected memdump seqno(%u) is not received(%u)\n",
3554 			   btdata->qca_dump.ram_dump_seqno, seqno);
3555 		btdata->qca_dump.ram_dump_seqno = seqno;
3556 		kfree_skb(skb);
3557 		return ret;
3558 	}
3559 
3560 	skb_pull(skb, skb->len - sk_len);
3561 	hci_devcd_append(hdev, skb);
3562 	btdata->qca_dump.ram_dump_seqno++;
3563 	if (seqno == QCA_LAST_SEQUENCE_NUM) {
3564 		bt_dev_info(hdev,
3565 				"memdump done: pkts(%u), total(%u)\n",
3566 				btdata->qca_dump.ram_dump_seqno, btdata->qca_dump.ram_dump_size);
3567 
3568 		hci_devcd_complete(hdev);
3569 		goto out;
3570 	}
3571 	return ret;
3572 
3573 out:
3574 	if (btdata->qca_dump.ram_dump_size)
3575 		usb_enable_autosuspend(udev);
3576 	btdata->qca_dump.ram_dump_size = 0;
3577 	btdata->qca_dump.ram_dump_seqno = 0;
3578 	clear_bit(BTUSB_HW_SSR_ACTIVE, &btdata->flags);
3579 
3580 	if (ret < 0)
3581 		kfree_skb(skb);
3582 	return ret;
3583 }
3584 
3585 static int btusb_recv_acl_qca(struct hci_dev *hdev, struct sk_buff *skb)
3586 {
3587 	if (handle_dump_pkt_qca(hdev, skb))
3588 		return 0;
3589 	return hci_recv_frame(hdev, skb);
3590 }
3591 
3592 static int btusb_recv_evt_qca(struct hci_dev *hdev, struct sk_buff *skb)
3593 {
3594 	if (handle_dump_pkt_qca(hdev, skb))
3595 		return 0;
3596 	return hci_recv_frame(hdev, skb);
3597 }
3598 
3599 
3600 #define QCA_DFU_PACKET_LEN	4096
3601 
3602 #define QCA_GET_TARGET_VERSION	0x09
3603 #define QCA_CHECK_STATUS	0x05
3604 #define QCA_DFU_DOWNLOAD	0x01
3605 
3606 #define QCA_SYSCFG_UPDATED	0x40
3607 #define QCA_PATCH_UPDATED	0x80
3608 #define QCA_DFU_TIMEOUT		3000
3609 #define QCA_FLAG_MULTI_NVM      0x80
3610 #define QCA_BT_RESET_WAIT_MS    100
3611 
3612 #define WCN6855_2_0_RAM_VERSION_GF 0x400c1200
3613 #define WCN6855_2_1_RAM_VERSION_GF 0x400c1211
3614 
3615 struct qca_version {
3616 	__le32	rom_version;
3617 	__le32	patch_version;
3618 	__le32	ram_version;
3619 	__u8	chip_id;
3620 	__u8	platform_id;
3621 	__le16	flag;
3622 	__u8	reserved[4];
3623 } __packed;
3624 
3625 struct qca_rampatch_version {
3626 	__le16	rom_version_high;
3627 	__le16  rom_version_low;
3628 	__le16	patch_version;
3629 } __packed;
3630 
3631 struct qca_device_info {
3632 	u32	rom_version;
3633 	u8	rampatch_hdr;	/* length of header in rampatch */
3634 	u8	nvm_hdr;	/* length of header in NVM */
3635 	u8	ver_offset;	/* offset of version structure in rampatch */
3636 };
3637 
3638 static const struct qca_device_info qca_devices_table[] = {
3639 	{ 0x00000100, 20, 4,  8 }, /* Rome 1.0 */
3640 	{ 0x00000101, 20, 4,  8 }, /* Rome 1.1 */
3641 	{ 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3642 	{ 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3643 	{ 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3644 	{ 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3645 	{ 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3646 	{ 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3647 	{ 0x00130201, 40, 4, 16 }, /* WCN6855 2.1 */
3648 	{ 0x00190200, 40, 4, 16 }, /* WCN785x 2.0 */
3649 };
3650 
3651 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3652 				     void *data, u16 size)
3653 {
3654 	int pipe, err;
3655 	u8 *buf;
3656 
3657 	buf = kmalloc(size, GFP_KERNEL);
3658 	if (!buf)
3659 		return -ENOMEM;
3660 
3661 	/* Found some of USB hosts have IOT issues with ours so that we should
3662 	 * not wait until HCI layer is ready.
3663 	 */
3664 	pipe = usb_rcvctrlpipe(udev, 0);
3665 	err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3666 			      0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3667 	if (err < 0) {
3668 		dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3669 		goto done;
3670 	}
3671 
3672 	memcpy(data, buf, size);
3673 
3674 done:
3675 	kfree(buf);
3676 
3677 	return err;
3678 }
3679 
3680 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3681 				       const struct firmware *firmware,
3682 				       size_t hdr_size)
3683 {
3684 	struct btusb_data *btdata = hci_get_drvdata(hdev);
3685 	struct usb_device *udev = btdata->udev;
3686 	size_t count, size, sent = 0;
3687 	int pipe, len, err;
3688 	u8 *buf;
3689 
3690 	buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3691 	if (!buf)
3692 		return -ENOMEM;
3693 
3694 	count = firmware->size;
3695 
3696 	size = min_t(size_t, count, hdr_size);
3697 	memcpy(buf, firmware->data, size);
3698 
3699 	/* USB patches should go down to controller through USB path
3700 	 * because binary format fits to go down through USB channel.
3701 	 * USB control path is for patching headers and USB bulk is for
3702 	 * patch body.
3703 	 */
3704 	pipe = usb_sndctrlpipe(udev, 0);
3705 	err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3706 			      0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3707 	if (err < 0) {
3708 		bt_dev_err(hdev, "Failed to send headers (%d)", err);
3709 		goto done;
3710 	}
3711 
3712 	sent += size;
3713 	count -= size;
3714 
3715 	/* ep2 need time to switch from function acl to function dfu,
3716 	 * so we add 20ms delay here.
3717 	 */
3718 	msleep(20);
3719 
3720 	while (count) {
3721 		size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3722 
3723 		memcpy(buf, firmware->data + sent, size);
3724 
3725 		pipe = usb_sndbulkpipe(udev, 0x02);
3726 		err = usb_bulk_msg(udev, pipe, buf, size, &len,
3727 				   QCA_DFU_TIMEOUT);
3728 		if (err < 0) {
3729 			bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3730 				   sent, firmware->size, err);
3731 			break;
3732 		}
3733 
3734 		if (size != len) {
3735 			bt_dev_err(hdev, "Failed to get bulk buffer");
3736 			err = -EILSEQ;
3737 			break;
3738 		}
3739 
3740 		sent  += size;
3741 		count -= size;
3742 	}
3743 
3744 done:
3745 	kfree(buf);
3746 	return err;
3747 }
3748 
3749 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3750 					 struct qca_version *ver,
3751 					 const struct qca_device_info *info)
3752 {
3753 	struct qca_rampatch_version *rver;
3754 	const struct firmware *fw;
3755 	u32 ver_rom, ver_patch, rver_rom;
3756 	u16 rver_rom_low, rver_rom_high, rver_patch;
3757 	char fwname[64];
3758 	int err;
3759 
3760 	ver_rom = le32_to_cpu(ver->rom_version);
3761 	ver_patch = le32_to_cpu(ver->patch_version);
3762 
3763 	snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
3764 
3765 	err = request_firmware(&fw, fwname, &hdev->dev);
3766 	if (err) {
3767 		bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3768 			   fwname, err);
3769 		return err;
3770 	}
3771 
3772 	bt_dev_info(hdev, "using rampatch file: %s", fwname);
3773 
3774 	rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3775 	rver_rom_low = le16_to_cpu(rver->rom_version_low);
3776 	rver_patch = le16_to_cpu(rver->patch_version);
3777 
3778 	if (ver_rom & ~0xffffU) {
3779 		rver_rom_high = le16_to_cpu(rver->rom_version_high);
3780 		rver_rom = rver_rom_high << 16 | rver_rom_low;
3781 	} else {
3782 		rver_rom = rver_rom_low;
3783 	}
3784 
3785 	bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3786 		    "firmware rome 0x%x build 0x%x",
3787 		    rver_rom, rver_patch, ver_rom, ver_patch);
3788 
3789 	if (rver_rom != ver_rom || rver_patch <= ver_patch) {
3790 		bt_dev_err(hdev, "rampatch file version did not match with firmware");
3791 		err = -EINVAL;
3792 		goto done;
3793 	}
3794 
3795 	err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3796 
3797 done:
3798 	release_firmware(fw);
3799 
3800 	return err;
3801 }
3802 
3803 static void btusb_generate_qca_nvm_name(char *fwname, size_t max_size,
3804 					const struct qca_version *ver)
3805 {
3806 	u32 rom_version = le32_to_cpu(ver->rom_version);
3807 	u16 flag = le16_to_cpu(ver->flag);
3808 
3809 	if (((flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
3810 		/* The board_id should be split into two bytes
3811 		 * The 1st byte is chip ID, and the 2nd byte is platform ID
3812 		 * For example, board ID 0x010A, 0x01 is platform ID. 0x0A is chip ID
3813 		 * we have several platforms, and platform IDs are continuously added
3814 		 * Platform ID:
3815 		 * 0x00 is for Mobile
3816 		 * 0x01 is for X86
3817 		 * 0x02 is for Automotive
3818 		 * 0x03 is for Consumer electronic
3819 		 */
3820 		u16 board_id = (ver->chip_id << 8) + ver->platform_id;
3821 		const char *variant;
3822 
3823 		switch (le32_to_cpu(ver->ram_version)) {
3824 		case WCN6855_2_0_RAM_VERSION_GF:
3825 		case WCN6855_2_1_RAM_VERSION_GF:
3826 			variant = "_gf";
3827 			break;
3828 		default:
3829 			variant = "";
3830 			break;
3831 		}
3832 
3833 		if (board_id == 0) {
3834 			snprintf(fwname, max_size, "qca/nvm_usb_%08x%s.bin",
3835 				rom_version, variant);
3836 		} else {
3837 			snprintf(fwname, max_size, "qca/nvm_usb_%08x%s_%04x.bin",
3838 				rom_version, variant, board_id);
3839 		}
3840 	} else {
3841 		snprintf(fwname, max_size, "qca/nvm_usb_%08x.bin",
3842 			rom_version);
3843 	}
3844 
3845 }
3846 
3847 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3848 				    struct qca_version *ver,
3849 				    const struct qca_device_info *info)
3850 {
3851 	const struct firmware *fw;
3852 	char fwname[64];
3853 	int err;
3854 
3855 	btusb_generate_qca_nvm_name(fwname, sizeof(fwname), ver);
3856 
3857 	err = request_firmware(&fw, fwname, &hdev->dev);
3858 	if (err) {
3859 		bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3860 			   fwname, err);
3861 		return err;
3862 	}
3863 
3864 	bt_dev_info(hdev, "using NVM file: %s", fwname);
3865 
3866 	err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3867 
3868 	release_firmware(fw);
3869 
3870 	return err;
3871 }
3872 
3873 /* identify the ROM version and check whether patches are needed */
3874 static bool btusb_qca_need_patch(struct usb_device *udev)
3875 {
3876 	struct qca_version ver;
3877 
3878 	if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3879 				      sizeof(ver)) < 0)
3880 		return false;
3881 	/* only low ROM versions need patches */
3882 	return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3883 }
3884 
3885 static int btusb_setup_qca(struct hci_dev *hdev)
3886 {
3887 	struct btusb_data *btdata = hci_get_drvdata(hdev);
3888 	struct usb_device *udev = btdata->udev;
3889 	const struct qca_device_info *info = NULL;
3890 	struct qca_version ver;
3891 	u32 ver_rom;
3892 	u8 status;
3893 	int i, err;
3894 
3895 	err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3896 					sizeof(ver));
3897 	if (err < 0)
3898 		return err;
3899 
3900 	ver_rom = le32_to_cpu(ver.rom_version);
3901 
3902 	for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
3903 		if (ver_rom == qca_devices_table[i].rom_version)
3904 			info = &qca_devices_table[i];
3905 	}
3906 	if (!info) {
3907 		/* If the rom_version is not matched in the qca_devices_table
3908 		 * and the high ROM version is not zero, we assume this chip no
3909 		 * need to load the rampatch and nvm.
3910 		 */
3911 		if (ver_rom & ~0xffffU)
3912 			return 0;
3913 
3914 		bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3915 		return -ENODEV;
3916 	}
3917 
3918 	err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3919 					sizeof(status));
3920 	if (err < 0)
3921 		return err;
3922 
3923 	if (!(status & QCA_PATCH_UPDATED)) {
3924 		err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3925 		if (err < 0)
3926 			return err;
3927 	}
3928 
3929 	err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3930 					sizeof(ver));
3931 	if (err < 0)
3932 		return err;
3933 
3934 	btdata->qca_dump.fw_version = le32_to_cpu(ver.patch_version);
3935 	btdata->qca_dump.controller_id = le32_to_cpu(ver.rom_version);
3936 
3937 	if (!(status & QCA_SYSCFG_UPDATED)) {
3938 		err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3939 		if (err < 0)
3940 			return err;
3941 
3942 		/* WCN6855 2.1 and later will reset to apply firmware downloaded here, so
3943 		 * wait ~100ms for reset Done then go ahead, otherwise, it maybe
3944 		 * cause potential enable failure.
3945 		 */
3946 		if (info->rom_version >= 0x00130201)
3947 			msleep(QCA_BT_RESET_WAIT_MS);
3948 	}
3949 
3950 	/* Mark HCI_OP_ENHANCED_SETUP_SYNC_CONN as broken as it doesn't seem to
3951 	 * work with the likes of HSP/HFP mSBC.
3952 	 */
3953 	set_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &hdev->quirks);
3954 
3955 	return 0;
3956 }
3957 
3958 static inline int __set_diag_interface(struct hci_dev *hdev)
3959 {
3960 	struct btusb_data *data = hci_get_drvdata(hdev);
3961 	struct usb_interface *intf = data->diag;
3962 	int i;
3963 
3964 	if (!data->diag)
3965 		return -ENODEV;
3966 
3967 	data->diag_tx_ep = NULL;
3968 	data->diag_rx_ep = NULL;
3969 
3970 	for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3971 		struct usb_endpoint_descriptor *ep_desc;
3972 
3973 		ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3974 
3975 		if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3976 			data->diag_tx_ep = ep_desc;
3977 			continue;
3978 		}
3979 
3980 		if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3981 			data->diag_rx_ep = ep_desc;
3982 			continue;
3983 		}
3984 	}
3985 
3986 	if (!data->diag_tx_ep || !data->diag_rx_ep) {
3987 		bt_dev_err(hdev, "invalid diagnostic descriptors");
3988 		return -ENODEV;
3989 	}
3990 
3991 	return 0;
3992 }
3993 
3994 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3995 {
3996 	struct btusb_data *data = hci_get_drvdata(hdev);
3997 	struct sk_buff *skb;
3998 	struct urb *urb;
3999 	unsigned int pipe;
4000 
4001 	if (!data->diag_tx_ep)
4002 		return ERR_PTR(-ENODEV);
4003 
4004 	urb = usb_alloc_urb(0, GFP_KERNEL);
4005 	if (!urb)
4006 		return ERR_PTR(-ENOMEM);
4007 
4008 	skb = bt_skb_alloc(2, GFP_KERNEL);
4009 	if (!skb) {
4010 		usb_free_urb(urb);
4011 		return ERR_PTR(-ENOMEM);
4012 	}
4013 
4014 	skb_put_u8(skb, 0xf0);
4015 	skb_put_u8(skb, enable);
4016 
4017 	pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
4018 
4019 	usb_fill_bulk_urb(urb, data->udev, pipe,
4020 			  skb->data, skb->len, btusb_tx_complete, skb);
4021 
4022 	skb->dev = (void *)hdev;
4023 
4024 	return urb;
4025 }
4026 
4027 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
4028 {
4029 	struct btusb_data *data = hci_get_drvdata(hdev);
4030 	struct urb *urb;
4031 
4032 	if (!data->diag)
4033 		return -ENODEV;
4034 
4035 	if (!test_bit(HCI_RUNNING, &hdev->flags))
4036 		return -ENETDOWN;
4037 
4038 	urb = alloc_diag_urb(hdev, enable);
4039 	if (IS_ERR(urb))
4040 		return PTR_ERR(urb);
4041 
4042 	return submit_or_queue_tx_urb(hdev, urb);
4043 }
4044 
4045 #ifdef CONFIG_PM
4046 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
4047 {
4048 	struct btusb_data *data = priv;
4049 
4050 	pm_wakeup_event(&data->udev->dev, 0);
4051 	pm_system_wakeup();
4052 
4053 	/* Disable only if not already disabled (keep it balanced) */
4054 	if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4055 		disable_irq_nosync(irq);
4056 		disable_irq_wake(irq);
4057 	}
4058 	return IRQ_HANDLED;
4059 }
4060 
4061 static const struct of_device_id btusb_match_table[] = {
4062 	{ .compatible = "usb1286,204e" },
4063 	{ .compatible = "usbcf3,e300" }, /* QCA6174A */
4064 	{ .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
4065 	{ }
4066 };
4067 MODULE_DEVICE_TABLE(of, btusb_match_table);
4068 
4069 /* Use an oob wakeup pin? */
4070 static int btusb_config_oob_wake(struct hci_dev *hdev)
4071 {
4072 	struct btusb_data *data = hci_get_drvdata(hdev);
4073 	struct device *dev = &data->udev->dev;
4074 	int irq, ret;
4075 
4076 	clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4077 
4078 	if (!of_match_device(btusb_match_table, dev))
4079 		return 0;
4080 
4081 	/* Move on if no IRQ specified */
4082 	irq = of_irq_get_byname(dev->of_node, "wakeup");
4083 	if (irq <= 0) {
4084 		bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
4085 		return 0;
4086 	}
4087 
4088 	irq_set_status_flags(irq, IRQ_NOAUTOEN);
4089 	ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
4090 			       0, "OOB Wake-on-BT", data);
4091 	if (ret) {
4092 		bt_dev_err(hdev, "%s: IRQ request failed", __func__);
4093 		return ret;
4094 	}
4095 
4096 	ret = device_init_wakeup(dev, true);
4097 	if (ret) {
4098 		bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
4099 		return ret;
4100 	}
4101 
4102 	data->oob_wake_irq = irq;
4103 	bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
4104 	return 0;
4105 }
4106 #endif
4107 
4108 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
4109 {
4110 	if (dmi_check_system(btusb_needs_reset_resume_table))
4111 		interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
4112 }
4113 
4114 static bool btusb_wakeup(struct hci_dev *hdev)
4115 {
4116 	struct btusb_data *data = hci_get_drvdata(hdev);
4117 
4118 	return device_may_wakeup(&data->udev->dev);
4119 }
4120 
4121 static int btusb_shutdown_qca(struct hci_dev *hdev)
4122 {
4123 	struct sk_buff *skb;
4124 
4125 	skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
4126 	if (IS_ERR(skb)) {
4127 		bt_dev_err(hdev, "HCI reset during shutdown failed");
4128 		return PTR_ERR(skb);
4129 	}
4130 	kfree_skb(skb);
4131 
4132 	return 0;
4133 }
4134 
4135 static ssize_t force_poll_sync_read(struct file *file, char __user *user_buf,
4136 				    size_t count, loff_t *ppos)
4137 {
4138 	struct btusb_data *data = file->private_data;
4139 	char buf[3];
4140 
4141 	buf[0] = data->poll_sync ? 'Y' : 'N';
4142 	buf[1] = '\n';
4143 	buf[2] = '\0';
4144 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
4145 }
4146 
4147 static ssize_t force_poll_sync_write(struct file *file,
4148 				     const char __user *user_buf,
4149 				     size_t count, loff_t *ppos)
4150 {
4151 	struct btusb_data *data = file->private_data;
4152 	bool enable;
4153 	int err;
4154 
4155 	err = kstrtobool_from_user(user_buf, count, &enable);
4156 	if (err)
4157 		return err;
4158 
4159 	/* Only allow changes while the adapter is down */
4160 	if (test_bit(HCI_UP, &data->hdev->flags))
4161 		return -EPERM;
4162 
4163 	if (data->poll_sync == enable)
4164 		return -EALREADY;
4165 
4166 	data->poll_sync = enable;
4167 
4168 	return count;
4169 }
4170 
4171 static const struct file_operations force_poll_sync_fops = {
4172 	.open		= simple_open,
4173 	.read		= force_poll_sync_read,
4174 	.write		= force_poll_sync_write,
4175 	.llseek		= default_llseek,
4176 };
4177 
4178 static int btusb_probe(struct usb_interface *intf,
4179 		       const struct usb_device_id *id)
4180 {
4181 	struct usb_endpoint_descriptor *ep_desc;
4182 	struct gpio_desc *reset_gpio;
4183 	struct btusb_data *data;
4184 	struct hci_dev *hdev;
4185 	unsigned ifnum_base;
4186 	int i, err, priv_size;
4187 
4188 	BT_DBG("intf %p id %p", intf, id);
4189 
4190 	if ((id->driver_info & BTUSB_IFNUM_2) &&
4191 	    (intf->cur_altsetting->desc.bInterfaceNumber != 0) &&
4192 	    (intf->cur_altsetting->desc.bInterfaceNumber != 2))
4193 		return -ENODEV;
4194 
4195 	ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
4196 
4197 	if (!id->driver_info) {
4198 		const struct usb_device_id *match;
4199 
4200 		match = usb_match_id(intf, quirks_table);
4201 		if (match)
4202 			id = match;
4203 	}
4204 
4205 	if (id->driver_info == BTUSB_IGNORE)
4206 		return -ENODEV;
4207 
4208 	if (id->driver_info & BTUSB_ATH3012) {
4209 		struct usb_device *udev = interface_to_usbdev(intf);
4210 
4211 		/* Old firmware would otherwise let ath3k driver load
4212 		 * patch and sysconfig files
4213 		 */
4214 		if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
4215 		    !btusb_qca_need_patch(udev))
4216 			return -ENODEV;
4217 	}
4218 
4219 	data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
4220 	if (!data)
4221 		return -ENOMEM;
4222 
4223 	for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4224 		ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4225 
4226 		if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
4227 			data->intr_ep = ep_desc;
4228 			continue;
4229 		}
4230 
4231 		if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4232 			data->bulk_tx_ep = ep_desc;
4233 			continue;
4234 		}
4235 
4236 		if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4237 			data->bulk_rx_ep = ep_desc;
4238 			continue;
4239 		}
4240 	}
4241 
4242 	if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
4243 		return -ENODEV;
4244 
4245 	if (id->driver_info & BTUSB_AMP) {
4246 		data->cmdreq_type = USB_TYPE_CLASS | 0x01;
4247 		data->cmdreq = 0x2b;
4248 	} else {
4249 		data->cmdreq_type = USB_TYPE_CLASS;
4250 		data->cmdreq = 0x00;
4251 	}
4252 
4253 	data->udev = interface_to_usbdev(intf);
4254 	data->intf = intf;
4255 
4256 	INIT_WORK(&data->work, btusb_work);
4257 	INIT_WORK(&data->waker, btusb_waker);
4258 	INIT_DELAYED_WORK(&data->rx_work, btusb_rx_work);
4259 
4260 	skb_queue_head_init(&data->acl_q);
4261 
4262 	init_usb_anchor(&data->deferred);
4263 	init_usb_anchor(&data->tx_anchor);
4264 	spin_lock_init(&data->txlock);
4265 
4266 	init_usb_anchor(&data->intr_anchor);
4267 	init_usb_anchor(&data->bulk_anchor);
4268 	init_usb_anchor(&data->isoc_anchor);
4269 	init_usb_anchor(&data->diag_anchor);
4270 	init_usb_anchor(&data->ctrl_anchor);
4271 	spin_lock_init(&data->rxlock);
4272 
4273 	priv_size = 0;
4274 
4275 	data->recv_event = hci_recv_frame;
4276 	data->recv_bulk = btusb_recv_bulk;
4277 
4278 	if (id->driver_info & BTUSB_INTEL_COMBINED) {
4279 		/* Allocate extra space for Intel device */
4280 		priv_size += sizeof(struct btintel_data);
4281 
4282 		/* Override the rx handlers */
4283 		data->recv_event = btintel_recv_event;
4284 		data->recv_bulk = btusb_recv_bulk_intel;
4285 	} else if (id->driver_info & BTUSB_REALTEK) {
4286 		/* Allocate extra space for Realtek device */
4287 		priv_size += sizeof(struct btrealtek_data);
4288 
4289 		data->recv_event = btusb_recv_event_realtek;
4290 	} else if (id->driver_info & BTUSB_MEDIATEK) {
4291 		/* Allocate extra space for Mediatek device */
4292 		priv_size += sizeof(struct btmediatek_data);
4293 	}
4294 
4295 	data->recv_acl = hci_recv_frame;
4296 
4297 	hdev = hci_alloc_dev_priv(priv_size);
4298 	if (!hdev)
4299 		return -ENOMEM;
4300 
4301 	hdev->bus = HCI_USB;
4302 	hci_set_drvdata(hdev, data);
4303 
4304 	if (id->driver_info & BTUSB_AMP)
4305 		hdev->dev_type = HCI_AMP;
4306 	else
4307 		hdev->dev_type = HCI_PRIMARY;
4308 
4309 	data->hdev = hdev;
4310 
4311 	SET_HCIDEV_DEV(hdev, &intf->dev);
4312 
4313 	reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
4314 					GPIOD_OUT_LOW);
4315 	if (IS_ERR(reset_gpio)) {
4316 		err = PTR_ERR(reset_gpio);
4317 		goto out_free_dev;
4318 	} else if (reset_gpio) {
4319 		data->reset_gpio = reset_gpio;
4320 	}
4321 
4322 	hdev->open   = btusb_open;
4323 	hdev->close  = btusb_close;
4324 	hdev->flush  = btusb_flush;
4325 	hdev->send   = btusb_send_frame;
4326 	hdev->notify = btusb_notify;
4327 	hdev->wakeup = btusb_wakeup;
4328 
4329 #ifdef CONFIG_PM
4330 	err = btusb_config_oob_wake(hdev);
4331 	if (err)
4332 		goto out_free_dev;
4333 
4334 	/* Marvell devices may need a specific chip configuration */
4335 	if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
4336 		err = marvell_config_oob_wake(hdev);
4337 		if (err)
4338 			goto out_free_dev;
4339 	}
4340 #endif
4341 	if (id->driver_info & BTUSB_CW6622)
4342 		set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4343 
4344 	if (id->driver_info & BTUSB_BCM2045)
4345 		set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4346 
4347 	if (id->driver_info & BTUSB_BCM92035)
4348 		hdev->setup = btusb_setup_bcm92035;
4349 
4350 	if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4351 	    (id->driver_info & BTUSB_BCM_PATCHRAM)) {
4352 		hdev->manufacturer = 15;
4353 		hdev->setup = btbcm_setup_patchram;
4354 		hdev->set_diag = btusb_bcm_set_diag;
4355 		hdev->set_bdaddr = btbcm_set_bdaddr;
4356 
4357 		/* Broadcom LM_DIAG Interface numbers are hardcoded */
4358 		data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4359 	}
4360 
4361 	if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4362 	    (id->driver_info & BTUSB_BCM_APPLE)) {
4363 		hdev->manufacturer = 15;
4364 		hdev->setup = btbcm_setup_apple;
4365 		hdev->set_diag = btusb_bcm_set_diag;
4366 
4367 		/* Broadcom LM_DIAG Interface numbers are hardcoded */
4368 		data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4369 	}
4370 
4371 	/* Combined Intel Device setup to support multiple setup routine */
4372 	if (id->driver_info & BTUSB_INTEL_COMBINED) {
4373 		err = btintel_configure_setup(hdev, btusb_driver.name);
4374 		if (err)
4375 			goto out_free_dev;
4376 
4377 		/* Transport specific configuration */
4378 		hdev->send = btusb_send_frame_intel;
4379 		hdev->cmd_timeout = btusb_intel_cmd_timeout;
4380 
4381 		if (id->driver_info & BTUSB_INTEL_NO_WBS_SUPPORT)
4382 			btintel_set_flag(hdev, INTEL_ROM_LEGACY_NO_WBS_SUPPORT);
4383 
4384 		if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
4385 			btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
4386 
4387 		if (id->driver_info & BTUSB_INTEL_BROKEN_SHUTDOWN_LED)
4388 			btintel_set_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED);
4389 	}
4390 
4391 	if (id->driver_info & BTUSB_MARVELL)
4392 		hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4393 
4394 	if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4395 	    (id->driver_info & BTUSB_MEDIATEK)) {
4396 		hdev->setup = btusb_mtk_setup;
4397 		hdev->shutdown = btusb_mtk_shutdown;
4398 		hdev->manufacturer = 70;
4399 		hdev->cmd_timeout = btmtk_reset_sync;
4400 		hdev->set_bdaddr = btmtk_set_bdaddr;
4401 		set_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &hdev->quirks);
4402 		set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
4403 		data->recv_acl = btusb_recv_acl_mtk;
4404 	}
4405 
4406 	if (id->driver_info & BTUSB_SWAVE) {
4407 		set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
4408 		set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
4409 	}
4410 
4411 	if (id->driver_info & BTUSB_INTEL_BOOT) {
4412 		hdev->manufacturer = 2;
4413 		set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4414 	}
4415 
4416 	if (id->driver_info & BTUSB_ATH3012) {
4417 		data->setup_on_usb = btusb_setup_qca;
4418 		hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4419 		set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4420 		set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4421 	}
4422 
4423 	if (id->driver_info & BTUSB_QCA_ROME) {
4424 		data->setup_on_usb = btusb_setup_qca;
4425 		hdev->shutdown = btusb_shutdown_qca;
4426 		hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4427 		hdev->cmd_timeout = btusb_qca_cmd_timeout;
4428 		set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4429 		btusb_check_needs_reset_resume(intf);
4430 	}
4431 
4432 	if (id->driver_info & BTUSB_QCA_WCN6855) {
4433 		data->qca_dump.id_vendor = id->idVendor;
4434 		data->qca_dump.id_product = id->idProduct;
4435 		data->recv_event = btusb_recv_evt_qca;
4436 		data->recv_acl = btusb_recv_acl_qca;
4437 		hci_devcd_register(hdev, btusb_coredump_qca, btusb_dump_hdr_qca, NULL);
4438 		data->setup_on_usb = btusb_setup_qca;
4439 		hdev->shutdown = btusb_shutdown_qca;
4440 		hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
4441 		hdev->cmd_timeout = btusb_qca_cmd_timeout;
4442 		set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4443 		hci_set_msft_opcode(hdev, 0xFD70);
4444 	}
4445 
4446 	if (id->driver_info & BTUSB_AMP) {
4447 		/* AMP controllers do not support SCO packets */
4448 		data->isoc = NULL;
4449 	} else {
4450 		/* Interface orders are hardcoded in the specification */
4451 		data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
4452 		data->isoc_ifnum = ifnum_base + 1;
4453 	}
4454 
4455 	if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4456 	    (id->driver_info & BTUSB_REALTEK)) {
4457 		btrtl_set_driver_name(hdev, btusb_driver.name);
4458 		hdev->setup = btusb_setup_realtek;
4459 		hdev->shutdown = btrtl_shutdown_realtek;
4460 		hdev->cmd_timeout = btusb_rtl_cmd_timeout;
4461 		hdev->hw_error = btusb_rtl_hw_error;
4462 
4463 		/* Realtek devices need to set remote wakeup on auto-suspend */
4464 		set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
4465 		set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
4466 	}
4467 
4468 	if (id->driver_info & BTUSB_ACTIONS_SEMI) {
4469 		/* Support is advertised, but not implemented */
4470 		set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
4471 		set_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks);
4472 		set_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks);
4473 		set_bit(HCI_QUIRK_BROKEN_EXT_SCAN, &hdev->quirks);
4474 	}
4475 
4476 	if (!reset)
4477 		set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4478 
4479 	if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
4480 		if (!disable_scofix)
4481 			set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
4482 	}
4483 
4484 	if (id->driver_info & BTUSB_BROKEN_ISOC)
4485 		data->isoc = NULL;
4486 
4487 	if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
4488 		set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
4489 
4490 	if (id->driver_info & BTUSB_VALID_LE_STATES)
4491 		set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
4492 
4493 	if (id->driver_info & BTUSB_DIGIANSWER) {
4494 		data->cmdreq_type = USB_TYPE_VENDOR;
4495 		set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4496 	}
4497 
4498 	if (id->driver_info & BTUSB_CSR) {
4499 		struct usb_device *udev = data->udev;
4500 		u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
4501 
4502 		/* Old firmware would otherwise execute USB reset */
4503 		if (bcdDevice < 0x117)
4504 			set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4505 
4506 		/* This must be set first in case we disable it for fakes */
4507 		set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4508 
4509 		/* Fake CSR devices with broken commands */
4510 		if (le16_to_cpu(udev->descriptor.idVendor)  == 0x0a12 &&
4511 		    le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4512 			hdev->setup = btusb_setup_csr;
4513 	}
4514 
4515 	if (id->driver_info & BTUSB_SNIFFER) {
4516 		struct usb_device *udev = data->udev;
4517 
4518 		/* New sniffer firmware has crippled HCI interface */
4519 		if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4520 			set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4521 	}
4522 
4523 	if (id->driver_info & BTUSB_INTEL_BOOT) {
4524 		/* A bug in the bootloader causes that interrupt interface is
4525 		 * only enabled after receiving SetInterface(0, AltSetting=0).
4526 		 */
4527 		err = usb_set_interface(data->udev, 0, 0);
4528 		if (err < 0) {
4529 			BT_ERR("failed to set interface 0, alt 0 %d", err);
4530 			goto out_free_dev;
4531 		}
4532 	}
4533 
4534 	if (data->isoc) {
4535 		err = usb_driver_claim_interface(&btusb_driver,
4536 						 data->isoc, data);
4537 		if (err < 0)
4538 			goto out_free_dev;
4539 	}
4540 
4541 	if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
4542 		if (!usb_driver_claim_interface(&btusb_driver,
4543 						data->diag, data))
4544 			__set_diag_interface(hdev);
4545 		else
4546 			data->diag = NULL;
4547 	}
4548 
4549 	if (enable_autosuspend)
4550 		usb_enable_autosuspend(data->udev);
4551 
4552 	data->poll_sync = enable_poll_sync;
4553 
4554 	err = hci_register_dev(hdev);
4555 	if (err < 0)
4556 		goto out_free_dev;
4557 
4558 	usb_set_intfdata(intf, data);
4559 
4560 	debugfs_create_file("force_poll_sync", 0644, hdev->debugfs, data,
4561 			    &force_poll_sync_fops);
4562 
4563 	return 0;
4564 
4565 out_free_dev:
4566 	if (data->reset_gpio)
4567 		gpiod_put(data->reset_gpio);
4568 	hci_free_dev(hdev);
4569 	return err;
4570 }
4571 
4572 static void btusb_disconnect(struct usb_interface *intf)
4573 {
4574 	struct btusb_data *data = usb_get_intfdata(intf);
4575 	struct hci_dev *hdev;
4576 
4577 	BT_DBG("intf %p", intf);
4578 
4579 	if (!data)
4580 		return;
4581 
4582 	hdev = data->hdev;
4583 	usb_set_intfdata(data->intf, NULL);
4584 
4585 	if (data->isoc)
4586 		usb_set_intfdata(data->isoc, NULL);
4587 
4588 	if (data->diag)
4589 		usb_set_intfdata(data->diag, NULL);
4590 
4591 	hci_unregister_dev(hdev);
4592 
4593 	if (intf == data->intf) {
4594 		if (data->isoc)
4595 			usb_driver_release_interface(&btusb_driver, data->isoc);
4596 		if (data->diag)
4597 			usb_driver_release_interface(&btusb_driver, data->diag);
4598 	} else if (intf == data->isoc) {
4599 		if (data->diag)
4600 			usb_driver_release_interface(&btusb_driver, data->diag);
4601 		usb_driver_release_interface(&btusb_driver, data->intf);
4602 	} else if (intf == data->diag) {
4603 		usb_driver_release_interface(&btusb_driver, data->intf);
4604 		if (data->isoc)
4605 			usb_driver_release_interface(&btusb_driver, data->isoc);
4606 	}
4607 
4608 	if (data->oob_wake_irq)
4609 		device_init_wakeup(&data->udev->dev, false);
4610 
4611 	if (data->reset_gpio)
4612 		gpiod_put(data->reset_gpio);
4613 
4614 	hci_free_dev(hdev);
4615 }
4616 
4617 #ifdef CONFIG_PM
4618 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4619 {
4620 	struct btusb_data *data = usb_get_intfdata(intf);
4621 
4622 	BT_DBG("intf %p", intf);
4623 
4624 	if (data->suspend_count++)
4625 		return 0;
4626 
4627 	spin_lock_irq(&data->txlock);
4628 	if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4629 		set_bit(BTUSB_SUSPENDING, &data->flags);
4630 		spin_unlock_irq(&data->txlock);
4631 	} else {
4632 		spin_unlock_irq(&data->txlock);
4633 		data->suspend_count--;
4634 		return -EBUSY;
4635 	}
4636 
4637 	cancel_work_sync(&data->work);
4638 
4639 	btusb_stop_traffic(data);
4640 	usb_kill_anchored_urbs(&data->tx_anchor);
4641 
4642 	if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4643 		set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4644 		enable_irq_wake(data->oob_wake_irq);
4645 		enable_irq(data->oob_wake_irq);
4646 	}
4647 
4648 	/* For global suspend, Realtek devices lose the loaded fw
4649 	 * in them. But for autosuspend, firmware should remain.
4650 	 * Actually, it depends on whether the usb host sends
4651 	 * set feature (enable wakeup) or not.
4652 	 */
4653 	if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
4654 		if (PMSG_IS_AUTO(message) &&
4655 		    device_can_wakeup(&data->udev->dev))
4656 			data->udev->do_remote_wakeup = 1;
4657 		else if (!PMSG_IS_AUTO(message) &&
4658 			 !device_may_wakeup(&data->udev->dev)) {
4659 			data->udev->do_remote_wakeup = 0;
4660 			data->udev->reset_resume = 1;
4661 		}
4662 	}
4663 
4664 	return 0;
4665 }
4666 
4667 static void play_deferred(struct btusb_data *data)
4668 {
4669 	struct urb *urb;
4670 	int err;
4671 
4672 	while ((urb = usb_get_from_anchor(&data->deferred))) {
4673 		usb_anchor_urb(urb, &data->tx_anchor);
4674 
4675 		err = usb_submit_urb(urb, GFP_ATOMIC);
4676 		if (err < 0) {
4677 			if (err != -EPERM && err != -ENODEV)
4678 				BT_ERR("%s urb %p submission failed (%d)",
4679 				       data->hdev->name, urb, -err);
4680 			kfree(urb->setup_packet);
4681 			usb_unanchor_urb(urb);
4682 			usb_free_urb(urb);
4683 			break;
4684 		}
4685 
4686 		data->tx_in_flight++;
4687 		usb_free_urb(urb);
4688 	}
4689 
4690 	/* Cleanup the rest deferred urbs. */
4691 	while ((urb = usb_get_from_anchor(&data->deferred))) {
4692 		kfree(urb->setup_packet);
4693 		usb_free_urb(urb);
4694 	}
4695 }
4696 
4697 static int btusb_resume(struct usb_interface *intf)
4698 {
4699 	struct btusb_data *data = usb_get_intfdata(intf);
4700 	struct hci_dev *hdev = data->hdev;
4701 	int err = 0;
4702 
4703 	BT_DBG("intf %p", intf);
4704 
4705 	if (--data->suspend_count)
4706 		return 0;
4707 
4708 	/* Disable only if not already disabled (keep it balanced) */
4709 	if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4710 		disable_irq(data->oob_wake_irq);
4711 		disable_irq_wake(data->oob_wake_irq);
4712 	}
4713 
4714 	if (!test_bit(HCI_RUNNING, &hdev->flags))
4715 		goto done;
4716 
4717 	if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4718 		err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4719 		if (err < 0) {
4720 			clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4721 			goto failed;
4722 		}
4723 	}
4724 
4725 	if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4726 		err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4727 		if (err < 0) {
4728 			clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4729 			goto failed;
4730 		}
4731 
4732 		btusb_submit_bulk_urb(hdev, GFP_NOIO);
4733 	}
4734 
4735 	if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4736 		if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4737 			clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4738 		else
4739 			btusb_submit_isoc_urb(hdev, GFP_NOIO);
4740 	}
4741 
4742 	spin_lock_irq(&data->txlock);
4743 	play_deferred(data);
4744 	clear_bit(BTUSB_SUSPENDING, &data->flags);
4745 	spin_unlock_irq(&data->txlock);
4746 	schedule_work(&data->work);
4747 
4748 	return 0;
4749 
4750 failed:
4751 	usb_scuttle_anchored_urbs(&data->deferred);
4752 done:
4753 	spin_lock_irq(&data->txlock);
4754 	clear_bit(BTUSB_SUSPENDING, &data->flags);
4755 	spin_unlock_irq(&data->txlock);
4756 
4757 	return err;
4758 }
4759 #endif
4760 
4761 #ifdef CONFIG_DEV_COREDUMP
4762 static void btusb_coredump(struct device *dev)
4763 {
4764 	struct btusb_data *data = dev_get_drvdata(dev);
4765 	struct hci_dev *hdev = data->hdev;
4766 
4767 	if (hdev->dump.coredump)
4768 		hdev->dump.coredump(hdev);
4769 }
4770 #endif
4771 
4772 static struct usb_driver btusb_driver = {
4773 	.name		= "btusb",
4774 	.probe		= btusb_probe,
4775 	.disconnect	= btusb_disconnect,
4776 #ifdef CONFIG_PM
4777 	.suspend	= btusb_suspend,
4778 	.resume		= btusb_resume,
4779 #endif
4780 	.id_table	= btusb_table,
4781 	.supports_autosuspend = 1,
4782 	.disable_hub_initiated_lpm = 1,
4783 
4784 #ifdef CONFIG_DEV_COREDUMP
4785 	.drvwrap = {
4786 		.driver = {
4787 			.coredump = btusb_coredump,
4788 		},
4789 	},
4790 #endif
4791 };
4792 
4793 module_usb_driver(btusb_driver);
4794 
4795 module_param(disable_scofix, bool, 0644);
4796 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4797 
4798 module_param(force_scofix, bool, 0644);
4799 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4800 
4801 module_param(enable_autosuspend, bool, 0644);
4802 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4803 
4804 module_param(reset, bool, 0644);
4805 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4806 
4807 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4808 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4809 MODULE_VERSION(VERSION);
4810 MODULE_LICENSE("GPL");
4811