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