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