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