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