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