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