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