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