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