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