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