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