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