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