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