GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / bluetooth / btusb.c
1 /*
2  *
3  *  Generic Bluetooth USB driver
4  *
5  *  Copyright (C) 2005-2008  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include <linux/dmi.h>
25 #include <linux/module.h>
26 #include <linux/usb.h>
27 #include <linux/usb/quirks.h>
28 #include <linux/firmware.h>
29 #include <linux/of_device.h>
30 #include <linux/of_irq.h>
31 #include <linux/suspend.h>
32 #include <asm/unaligned.h>
33
34 #include <net/bluetooth/bluetooth.h>
35 #include <net/bluetooth/hci_core.h>
36
37 #include "btintel.h"
38 #include "btbcm.h"
39 #include "btrtl.h"
40
41 #define VERSION "0.8"
42
43 static bool disable_scofix;
44 static bool force_scofix;
45
46 static bool reset = true;
47
48 static struct usb_driver btusb_driver;
49
50 #define BTUSB_IGNORE            0x01
51 #define BTUSB_DIGIANSWER        0x02
52 #define BTUSB_CSR               0x04
53 #define BTUSB_SNIFFER           0x08
54 #define BTUSB_BCM92035          0x10
55 #define BTUSB_BROKEN_ISOC       0x20
56 #define BTUSB_WRONG_SCO_MTU     0x40
57 #define BTUSB_ATH3012           0x80
58 #define BTUSB_INTEL             0x100
59 #define BTUSB_INTEL_BOOT        0x200
60 #define BTUSB_BCM_PATCHRAM      0x400
61 #define BTUSB_MARVELL           0x800
62 #define BTUSB_SWAVE             0x1000
63 #define BTUSB_INTEL_NEW         0x2000
64 #define BTUSB_AMP               0x4000
65 #define BTUSB_QCA_ROME          0x8000
66 #define BTUSB_BCM_APPLE         0x10000
67 #define BTUSB_REALTEK           0x20000
68 #define BTUSB_BCM2045           0x40000
69 #define BTUSB_IFNUM_2           0x80000
70 #define BTUSB_CW6622            0x100000
71 #define BTUSB_BCM_NO_PRODID     0x200000
72
73 static const struct usb_device_id btusb_table[] = {
74         /* Generic Bluetooth USB device */
75         { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
76
77         /* Generic Bluetooth AMP device */
78         { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
79
80         /* Generic Bluetooth USB interface */
81         { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
82
83         /* Apple-specific (Broadcom) devices */
84         { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
85           .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
86
87         /* MediaTek MT76x0E */
88         { USB_DEVICE(0x0e8d, 0x763f) },
89
90         /* Broadcom SoftSailing reporting vendor specific */
91         { USB_DEVICE(0x0a5c, 0x21e1) },
92
93         /* Apple MacBookPro 7,1 */
94         { USB_DEVICE(0x05ac, 0x8213) },
95
96         /* Apple iMac11,1 */
97         { USB_DEVICE(0x05ac, 0x8215) },
98
99         /* Apple MacBookPro6,2 */
100         { USB_DEVICE(0x05ac, 0x8218) },
101
102         /* Apple MacBookAir3,1, MacBookAir3,2 */
103         { USB_DEVICE(0x05ac, 0x821b) },
104
105         /* Apple MacBookAir4,1 */
106         { USB_DEVICE(0x05ac, 0x821f) },
107
108         /* Apple MacBookPro8,2 */
109         { USB_DEVICE(0x05ac, 0x821a) },
110
111         /* Apple MacMini5,1 */
112         { USB_DEVICE(0x05ac, 0x8281) },
113
114         /* AVM BlueFRITZ! USB v2.0 */
115         { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
116
117         /* Bluetooth Ultraport Module from IBM */
118         { USB_DEVICE(0x04bf, 0x030a) },
119
120         /* ALPS Modules with non-standard id */
121         { USB_DEVICE(0x044e, 0x3001) },
122         { USB_DEVICE(0x044e, 0x3002) },
123
124         /* Ericsson with non-standard id */
125         { USB_DEVICE(0x0bdb, 0x1002) },
126
127         /* Canyon CN-BTU1 with HID interfaces */
128         { USB_DEVICE(0x0c10, 0x0000) },
129
130         /* Broadcom BCM20702A0 */
131         { USB_DEVICE(0x413c, 0x8197) },
132
133         /* Broadcom BCM20702B0 (Dynex/Insignia) */
134         { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
135
136         /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
137         { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
138           .driver_info = BTUSB_BCM_PATCHRAM },
139
140         /* Broadcom BCM920703 (HTC Vive) */
141         { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
142           .driver_info = BTUSB_BCM_PATCHRAM },
143
144         /* Foxconn - Hon Hai */
145         { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
146           .driver_info = BTUSB_BCM_PATCHRAM },
147
148         /* Lite-On Technology - Broadcom based */
149         { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
150           .driver_info = BTUSB_BCM_PATCHRAM },
151
152         /* Broadcom devices with vendor specific id */
153         { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
154           .driver_info = BTUSB_BCM_PATCHRAM },
155
156         /* ASUSTek Computer - Broadcom based */
157         { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
158           .driver_info = BTUSB_BCM_PATCHRAM },
159
160         /* Belkin F8065bf - Broadcom based */
161         { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
162           .driver_info = BTUSB_BCM_PATCHRAM },
163
164         /* IMC Networks - Broadcom based */
165         { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
166           .driver_info = BTUSB_BCM_PATCHRAM },
167
168         /* Dell Computer - Broadcom based  */
169         { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
170           .driver_info = BTUSB_BCM_PATCHRAM },
171
172         /* Toshiba Corp - Broadcom based */
173         { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
174           .driver_info = BTUSB_BCM_PATCHRAM },
175
176         /* Broadcom devices with missing product id */
177         { USB_DEVICE_AND_INTERFACE_INFO(0x0000, 0x0000, 0xff, 0x01, 0x01),
178           .driver_info = BTUSB_BCM_PATCHRAM | BTUSB_BCM_NO_PRODID },
179
180         /* Intel Bluetooth USB Bootloader (RAM module) */
181         { USB_DEVICE(0x8087, 0x0a5a),
182           .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
183
184         { }     /* Terminating entry */
185 };
186
187 MODULE_DEVICE_TABLE(usb, btusb_table);
188
189 static const struct usb_device_id blacklist_table[] = {
190         /* CSR BlueCore devices */
191         { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
192
193         /* Broadcom BCM2033 without firmware */
194         { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
195
196         /* Broadcom BCM2045 devices */
197         { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
198
199         /* Atheros 3011 with sflash firmware */
200         { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
201         { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
202         { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
203         { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
204         { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
205         { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
206         { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
207
208         /* Atheros AR9285 Malbec with sflash firmware */
209         { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
210
211         /* Atheros 3012 with sflash firmware */
212         { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
213         { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
214         { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
215         { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
216         { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
217         { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
218         { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
219         { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
220         { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
221         { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
222         { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
223         { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
224         { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
225         { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
226         { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
227         { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
228         { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
229         { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
230         { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
231         { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
232         { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
233         { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
234         { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
235         { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
236         { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
237         { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
238         { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
239         { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
240         { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
241         { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
242         { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
243         { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
244         { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
245         { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
246         { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
247         { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
248         { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
249         { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
250         { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
251         { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
252         { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
253         { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
254         { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
255         { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
256         { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
257         { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
258         { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
259         { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
260         { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
261         { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
262
263         /* Atheros AR5BBU12 with sflash firmware */
264         { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
265
266         /* Atheros AR5BBU12 with sflash firmware */
267         { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
268         { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
269
270         /* QCA ROME chipset */
271         { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
272         { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME },
273         { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
274         { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME },
275         { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
276         { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME },
277         { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME },
278         { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME },
279         { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME },
280         { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME },
281         { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME },
282         { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME },
283
284         /* Broadcom BCM2035 */
285         { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
286         { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
287         { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
288
289         /* Broadcom BCM2045 */
290         { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
291         { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
292
293         /* IBM/Lenovo ThinkPad with Broadcom chip */
294         { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
295         { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
296
297         /* HP laptop with Broadcom chip */
298         { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
299
300         /* Dell laptop with Broadcom chip */
301         { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
302
303         /* Dell Wireless 370 and 410 devices */
304         { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
305         { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
306
307         /* Belkin F8T012 and F8T013 devices */
308         { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
309         { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
310
311         /* Asus WL-BTD202 device */
312         { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
313
314         /* Kensington Bluetooth USB adapter */
315         { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
316
317         /* RTX Telecom based adapters with buggy SCO support */
318         { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
319         { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
320
321         /* CONWISE Technology based adapters with buggy SCO support */
322         { USB_DEVICE(0x0e5e, 0x6622),
323           .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
324
325         /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
326         { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
327
328         /* Digianswer devices */
329         { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
330         { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
331
332         /* CSR BlueCore Bluetooth Sniffer */
333         { USB_DEVICE(0x0a12, 0x0002),
334           .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
335
336         /* Frontline ComProbe Bluetooth Sniffer */
337         { USB_DEVICE(0x16d3, 0x0002),
338           .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
339
340         /* Marvell Bluetooth devices */
341         { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
342         { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
343         { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
344
345         /* Intel Bluetooth devices */
346         { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW },
347         { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
348         { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
349         { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
350         { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
351         { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL },
352         { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW },
353
354         /* Other Intel Bluetooth devices */
355         { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
356           .driver_info = BTUSB_IGNORE },
357
358         /* Realtek Bluetooth devices */
359         { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
360           .driver_info = BTUSB_REALTEK },
361
362         /* Additional Realtek 8723AE Bluetooth devices */
363         { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
364         { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
365
366         /* Additional Realtek 8723BE Bluetooth devices */
367         { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
368         { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
369         { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
370         { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
371         { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
372         { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
373
374         /* Additional Realtek 8723BU Bluetooth devices */
375         { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
376
377         /* Additional Realtek 8723DE Bluetooth devices */
378         { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
379         { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
380
381         /* Additional Realtek 8821AE Bluetooth devices */
382         { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
383         { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
384         { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
385         { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
386         { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
387
388         /* Additional Realtek 8822BE Bluetooth devices */
389         { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
390
391         /* Additional Realtek 8822CE Bluetooth devices */
392         { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK },
393
394         /* Silicon Wave based devices */
395         { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
396
397         { }     /* Terminating entry */
398 };
399
400 /* The Bluetooth USB module build into some devices needs to be reset on resume,
401  * this is a problem with the platform (likely shutting off all power) not with
402  * the module itself. So we use a DMI list to match known broken platforms.
403  */
404 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
405         {
406                 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
407                 .matches = {
408                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
409                         DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
410                 },
411         },
412         {
413                 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
414                 .matches = {
415                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
416                         DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
417                 },
418         },
419         {}
420 };
421
422 #define BTUSB_MAX_ISOC_FRAMES   10
423
424 #define BTUSB_INTR_RUNNING      0
425 #define BTUSB_BULK_RUNNING      1
426 #define BTUSB_ISOC_RUNNING      2
427 #define BTUSB_SUSPENDING        3
428 #define BTUSB_DID_ISO_RESUME    4
429 #define BTUSB_BOOTLOADER        5
430 #define BTUSB_DOWNLOADING       6
431 #define BTUSB_FIRMWARE_LOADED   7
432 #define BTUSB_FIRMWARE_FAILED   8
433 #define BTUSB_BOOTING           9
434 #define BTUSB_DIAG_RUNNING      10
435 #define BTUSB_OOB_WAKE_ENABLED  11
436
437 struct btusb_data {
438         struct hci_dev       *hdev;
439         struct usb_device    *udev;
440         struct usb_interface *intf;
441         struct usb_interface *isoc;
442         struct usb_interface *diag;
443
444         unsigned long flags;
445
446         struct work_struct work;
447         struct work_struct waker;
448
449         struct usb_anchor deferred;
450         struct usb_anchor tx_anchor;
451         int tx_in_flight;
452         spinlock_t txlock;
453
454         struct usb_anchor intr_anchor;
455         struct usb_anchor bulk_anchor;
456         struct usb_anchor isoc_anchor;
457         struct usb_anchor diag_anchor;
458         spinlock_t rxlock;
459
460         struct sk_buff *evt_skb;
461         struct sk_buff *acl_skb;
462         struct sk_buff *sco_skb;
463
464         struct usb_endpoint_descriptor *intr_ep;
465         struct usb_endpoint_descriptor *bulk_tx_ep;
466         struct usb_endpoint_descriptor *bulk_rx_ep;
467         struct usb_endpoint_descriptor *isoc_tx_ep;
468         struct usb_endpoint_descriptor *isoc_rx_ep;
469         struct usb_endpoint_descriptor *diag_tx_ep;
470         struct usb_endpoint_descriptor *diag_rx_ep;
471
472         __u8 cmdreq_type;
473         __u8 cmdreq;
474
475         unsigned int sco_num;
476         int isoc_altsetting;
477         int suspend_count;
478
479         int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
480         int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
481
482         int (*setup_on_usb)(struct hci_dev *hdev);
483
484         int oob_wake_irq;   /* irq for out-of-band wake-on-bt */
485 };
486
487 static inline void btusb_free_frags(struct btusb_data *data)
488 {
489         unsigned long flags;
490
491         spin_lock_irqsave(&data->rxlock, flags);
492
493         kfree_skb(data->evt_skb);
494         data->evt_skb = NULL;
495
496         kfree_skb(data->acl_skb);
497         data->acl_skb = NULL;
498
499         kfree_skb(data->sco_skb);
500         data->sco_skb = NULL;
501
502         spin_unlock_irqrestore(&data->rxlock, flags);
503 }
504
505 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
506 {
507         struct sk_buff *skb;
508         int err = 0;
509
510         spin_lock(&data->rxlock);
511         skb = data->evt_skb;
512
513         while (count) {
514                 int len;
515
516                 if (!skb) {
517                         skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
518                         if (!skb) {
519                                 err = -ENOMEM;
520                                 break;
521                         }
522
523                         hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
524                         hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
525                 }
526
527                 len = min_t(uint, hci_skb_expect(skb), count);
528                 skb_put_data(skb, buffer, len);
529
530                 count -= len;
531                 buffer += len;
532                 hci_skb_expect(skb) -= len;
533
534                 if (skb->len == HCI_EVENT_HDR_SIZE) {
535                         /* Complete event header */
536                         hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
537
538                         if (skb_tailroom(skb) < hci_skb_expect(skb)) {
539                                 kfree_skb(skb);
540                                 skb = NULL;
541
542                                 err = -EILSEQ;
543                                 break;
544                         }
545                 }
546
547                 if (!hci_skb_expect(skb)) {
548                         /* Complete frame */
549                         data->recv_event(data->hdev, skb);
550                         skb = NULL;
551                 }
552         }
553
554         data->evt_skb = skb;
555         spin_unlock(&data->rxlock);
556
557         return err;
558 }
559
560 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
561 {
562         struct sk_buff *skb;
563         int err = 0;
564
565         spin_lock(&data->rxlock);
566         skb = data->acl_skb;
567
568         while (count) {
569                 int len;
570
571                 if (!skb) {
572                         skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
573                         if (!skb) {
574                                 err = -ENOMEM;
575                                 break;
576                         }
577
578                         hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
579                         hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
580                 }
581
582                 len = min_t(uint, hci_skb_expect(skb), count);
583                 skb_put_data(skb, buffer, len);
584
585                 count -= len;
586                 buffer += len;
587                 hci_skb_expect(skb) -= len;
588
589                 if (skb->len == HCI_ACL_HDR_SIZE) {
590                         __le16 dlen = hci_acl_hdr(skb)->dlen;
591
592                         /* Complete ACL header */
593                         hci_skb_expect(skb) = __le16_to_cpu(dlen);
594
595                         if (skb_tailroom(skb) < hci_skb_expect(skb)) {
596                                 kfree_skb(skb);
597                                 skb = NULL;
598
599                                 err = -EILSEQ;
600                                 break;
601                         }
602                 }
603
604                 if (!hci_skb_expect(skb)) {
605                         /* Complete frame */
606                         hci_recv_frame(data->hdev, skb);
607                         skb = NULL;
608                 }
609         }
610
611         data->acl_skb = skb;
612         spin_unlock(&data->rxlock);
613
614         return err;
615 }
616
617 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
618 {
619         struct sk_buff *skb;
620         int err = 0;
621
622         spin_lock(&data->rxlock);
623         skb = data->sco_skb;
624
625         while (count) {
626                 int len;
627
628                 if (!skb) {
629                         skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
630                         if (!skb) {
631                                 err = -ENOMEM;
632                                 break;
633                         }
634
635                         hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
636                         hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
637                 }
638
639                 len = min_t(uint, hci_skb_expect(skb), count);
640                 skb_put_data(skb, buffer, len);
641
642                 count -= len;
643                 buffer += len;
644                 hci_skb_expect(skb) -= len;
645
646                 if (skb->len == HCI_SCO_HDR_SIZE) {
647                         /* Complete SCO header */
648                         hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
649
650                         if (skb_tailroom(skb) < hci_skb_expect(skb)) {
651                                 kfree_skb(skb);
652                                 skb = NULL;
653
654                                 err = -EILSEQ;
655                                 break;
656                         }
657                 }
658
659                 if (!hci_skb_expect(skb)) {
660                         /* Complete frame */
661                         hci_recv_frame(data->hdev, skb);
662                         skb = NULL;
663                 }
664         }
665
666         data->sco_skb = skb;
667         spin_unlock(&data->rxlock);
668
669         return err;
670 }
671
672 static void btusb_intr_complete(struct urb *urb)
673 {
674         struct hci_dev *hdev = urb->context;
675         struct btusb_data *data = hci_get_drvdata(hdev);
676         int err;
677
678         BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
679                urb->actual_length);
680
681         if (!test_bit(HCI_RUNNING, &hdev->flags))
682                 return;
683
684         if (urb->status == 0) {
685                 hdev->stat.byte_rx += urb->actual_length;
686
687                 if (btusb_recv_intr(data, urb->transfer_buffer,
688                                     urb->actual_length) < 0) {
689                         BT_ERR("%s corrupted event packet", hdev->name);
690                         hdev->stat.err_rx++;
691                 }
692         } else if (urb->status == -ENOENT) {
693                 /* Avoid suspend failed when usb_kill_urb */
694                 return;
695         }
696
697         if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
698                 return;
699
700         usb_mark_last_busy(data->udev);
701         usb_anchor_urb(urb, &data->intr_anchor);
702
703         err = usb_submit_urb(urb, GFP_ATOMIC);
704         if (err < 0) {
705                 /* -EPERM: urb is being killed;
706                  * -ENODEV: device got disconnected
707                  */
708                 if (err != -EPERM && err != -ENODEV)
709                         BT_ERR("%s urb %p failed to resubmit (%d)",
710                                hdev->name, urb, -err);
711                 usb_unanchor_urb(urb);
712         }
713 }
714
715 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
716 {
717         struct btusb_data *data = hci_get_drvdata(hdev);
718         struct urb *urb;
719         unsigned char *buf;
720         unsigned int pipe;
721         int err, size;
722
723         BT_DBG("%s", hdev->name);
724
725         if (!data->intr_ep)
726                 return -ENODEV;
727
728         urb = usb_alloc_urb(0, mem_flags);
729         if (!urb)
730                 return -ENOMEM;
731
732         size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
733
734         buf = kmalloc(size, mem_flags);
735         if (!buf) {
736                 usb_free_urb(urb);
737                 return -ENOMEM;
738         }
739
740         pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
741
742         usb_fill_int_urb(urb, data->udev, pipe, buf, size,
743                          btusb_intr_complete, hdev, data->intr_ep->bInterval);
744
745         urb->transfer_flags |= URB_FREE_BUFFER;
746
747         usb_anchor_urb(urb, &data->intr_anchor);
748
749         err = usb_submit_urb(urb, mem_flags);
750         if (err < 0) {
751                 if (err != -EPERM && err != -ENODEV)
752                         BT_ERR("%s urb %p submission failed (%d)",
753                                hdev->name, urb, -err);
754                 usb_unanchor_urb(urb);
755         }
756
757         usb_free_urb(urb);
758
759         return err;
760 }
761
762 static void btusb_bulk_complete(struct urb *urb)
763 {
764         struct hci_dev *hdev = urb->context;
765         struct btusb_data *data = hci_get_drvdata(hdev);
766         int err;
767
768         BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
769                urb->actual_length);
770
771         if (!test_bit(HCI_RUNNING, &hdev->flags))
772                 return;
773
774         if (urb->status == 0) {
775                 hdev->stat.byte_rx += urb->actual_length;
776
777                 if (data->recv_bulk(data, urb->transfer_buffer,
778                                     urb->actual_length) < 0) {
779                         BT_ERR("%s corrupted ACL packet", hdev->name);
780                         hdev->stat.err_rx++;
781                 }
782         } else if (urb->status == -ENOENT) {
783                 /* Avoid suspend failed when usb_kill_urb */
784                 return;
785         }
786
787         if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
788                 return;
789
790         usb_anchor_urb(urb, &data->bulk_anchor);
791         usb_mark_last_busy(data->udev);
792
793         err = usb_submit_urb(urb, GFP_ATOMIC);
794         if (err < 0) {
795                 /* -EPERM: urb is being killed;
796                  * -ENODEV: device got disconnected
797                  */
798                 if (err != -EPERM && err != -ENODEV)
799                         BT_ERR("%s urb %p failed to resubmit (%d)",
800                                hdev->name, urb, -err);
801                 usb_unanchor_urb(urb);
802         }
803 }
804
805 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
806 {
807         struct btusb_data *data = hci_get_drvdata(hdev);
808         struct urb *urb;
809         unsigned char *buf;
810         unsigned int pipe;
811         int err, size = HCI_MAX_FRAME_SIZE;
812
813         BT_DBG("%s", hdev->name);
814
815         if (!data->bulk_rx_ep)
816                 return -ENODEV;
817
818         urb = usb_alloc_urb(0, mem_flags);
819         if (!urb)
820                 return -ENOMEM;
821
822         buf = kmalloc(size, mem_flags);
823         if (!buf) {
824                 usb_free_urb(urb);
825                 return -ENOMEM;
826         }
827
828         pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
829
830         usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
831                           btusb_bulk_complete, hdev);
832
833         urb->transfer_flags |= URB_FREE_BUFFER;
834
835         usb_mark_last_busy(data->udev);
836         usb_anchor_urb(urb, &data->bulk_anchor);
837
838         err = usb_submit_urb(urb, mem_flags);
839         if (err < 0) {
840                 if (err != -EPERM && err != -ENODEV)
841                         BT_ERR("%s urb %p submission failed (%d)",
842                                hdev->name, urb, -err);
843                 usb_unanchor_urb(urb);
844         }
845
846         usb_free_urb(urb);
847
848         return err;
849 }
850
851 static void btusb_isoc_complete(struct urb *urb)
852 {
853         struct hci_dev *hdev = urb->context;
854         struct btusb_data *data = hci_get_drvdata(hdev);
855         int i, err;
856
857         BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
858                urb->actual_length);
859
860         if (!test_bit(HCI_RUNNING, &hdev->flags))
861                 return;
862
863         if (urb->status == 0) {
864                 for (i = 0; i < urb->number_of_packets; i++) {
865                         unsigned int offset = urb->iso_frame_desc[i].offset;
866                         unsigned int length = urb->iso_frame_desc[i].actual_length;
867
868                         if (urb->iso_frame_desc[i].status)
869                                 continue;
870
871                         hdev->stat.byte_rx += length;
872
873                         if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
874                                             length) < 0) {
875                                 BT_ERR("%s corrupted SCO packet", hdev->name);
876                                 hdev->stat.err_rx++;
877                         }
878                 }
879         } else if (urb->status == -ENOENT) {
880                 /* Avoid suspend failed when usb_kill_urb */
881                 return;
882         }
883
884         if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
885                 return;
886
887         usb_anchor_urb(urb, &data->isoc_anchor);
888
889         err = usb_submit_urb(urb, GFP_ATOMIC);
890         if (err < 0) {
891                 /* -EPERM: urb is being killed;
892                  * -ENODEV: device got disconnected
893                  */
894                 if (err != -EPERM && err != -ENODEV)
895                         BT_ERR("%s urb %p failed to resubmit (%d)",
896                                hdev->name, urb, -err);
897                 usb_unanchor_urb(urb);
898         }
899 }
900
901 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
902 {
903         int i, offset = 0;
904
905         BT_DBG("len %d mtu %d", len, mtu);
906
907         for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
908                                         i++, offset += mtu, len -= mtu) {
909                 urb->iso_frame_desc[i].offset = offset;
910                 urb->iso_frame_desc[i].length = mtu;
911         }
912
913         if (len && i < BTUSB_MAX_ISOC_FRAMES) {
914                 urb->iso_frame_desc[i].offset = offset;
915                 urb->iso_frame_desc[i].length = len;
916                 i++;
917         }
918
919         urb->number_of_packets = i;
920 }
921
922 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
923 {
924         struct btusb_data *data = hci_get_drvdata(hdev);
925         struct urb *urb;
926         unsigned char *buf;
927         unsigned int pipe;
928         int err, size;
929
930         BT_DBG("%s", hdev->name);
931
932         if (!data->isoc_rx_ep)
933                 return -ENODEV;
934
935         urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
936         if (!urb)
937                 return -ENOMEM;
938
939         size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
940                                                 BTUSB_MAX_ISOC_FRAMES;
941
942         buf = kmalloc(size, mem_flags);
943         if (!buf) {
944                 usb_free_urb(urb);
945                 return -ENOMEM;
946         }
947
948         pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
949
950         usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
951                          hdev, data->isoc_rx_ep->bInterval);
952
953         urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
954
955         __fill_isoc_descriptor(urb, size,
956                                le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
957
958         usb_anchor_urb(urb, &data->isoc_anchor);
959
960         err = usb_submit_urb(urb, mem_flags);
961         if (err < 0) {
962                 if (err != -EPERM && err != -ENODEV)
963                         BT_ERR("%s urb %p submission failed (%d)",
964                                hdev->name, urb, -err);
965                 usb_unanchor_urb(urb);
966         }
967
968         usb_free_urb(urb);
969
970         return err;
971 }
972
973 static void btusb_diag_complete(struct urb *urb)
974 {
975         struct hci_dev *hdev = urb->context;
976         struct btusb_data *data = hci_get_drvdata(hdev);
977         int err;
978
979         BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
980                urb->actual_length);
981
982         if (urb->status == 0) {
983                 struct sk_buff *skb;
984
985                 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
986                 if (skb) {
987                         skb_put_data(skb, urb->transfer_buffer,
988                                      urb->actual_length);
989                         hci_recv_diag(hdev, skb);
990                 }
991         } else if (urb->status == -ENOENT) {
992                 /* Avoid suspend failed when usb_kill_urb */
993                 return;
994         }
995
996         if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
997                 return;
998
999         usb_anchor_urb(urb, &data->diag_anchor);
1000         usb_mark_last_busy(data->udev);
1001
1002         err = usb_submit_urb(urb, GFP_ATOMIC);
1003         if (err < 0) {
1004                 /* -EPERM: urb is being killed;
1005                  * -ENODEV: device got disconnected
1006                  */
1007                 if (err != -EPERM && err != -ENODEV)
1008                         BT_ERR("%s urb %p failed to resubmit (%d)",
1009                                hdev->name, urb, -err);
1010                 usb_unanchor_urb(urb);
1011         }
1012 }
1013
1014 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1015 {
1016         struct btusb_data *data = hci_get_drvdata(hdev);
1017         struct urb *urb;
1018         unsigned char *buf;
1019         unsigned int pipe;
1020         int err, size = HCI_MAX_FRAME_SIZE;
1021
1022         BT_DBG("%s", hdev->name);
1023
1024         if (!data->diag_rx_ep)
1025                 return -ENODEV;
1026
1027         urb = usb_alloc_urb(0, mem_flags);
1028         if (!urb)
1029                 return -ENOMEM;
1030
1031         buf = kmalloc(size, mem_flags);
1032         if (!buf) {
1033                 usb_free_urb(urb);
1034                 return -ENOMEM;
1035         }
1036
1037         pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1038
1039         usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1040                           btusb_diag_complete, hdev);
1041
1042         urb->transfer_flags |= URB_FREE_BUFFER;
1043
1044         usb_mark_last_busy(data->udev);
1045         usb_anchor_urb(urb, &data->diag_anchor);
1046
1047         err = usb_submit_urb(urb, mem_flags);
1048         if (err < 0) {
1049                 if (err != -EPERM && err != -ENODEV)
1050                         BT_ERR("%s urb %p submission failed (%d)",
1051                                hdev->name, urb, -err);
1052                 usb_unanchor_urb(urb);
1053         }
1054
1055         usb_free_urb(urb);
1056
1057         return err;
1058 }
1059
1060 static void btusb_tx_complete(struct urb *urb)
1061 {
1062         struct sk_buff *skb = urb->context;
1063         struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1064         struct btusb_data *data = hci_get_drvdata(hdev);
1065
1066         BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1067                urb->actual_length);
1068
1069         if (!test_bit(HCI_RUNNING, &hdev->flags))
1070                 goto done;
1071
1072         if (!urb->status)
1073                 hdev->stat.byte_tx += urb->transfer_buffer_length;
1074         else
1075                 hdev->stat.err_tx++;
1076
1077 done:
1078         spin_lock(&data->txlock);
1079         data->tx_in_flight--;
1080         spin_unlock(&data->txlock);
1081
1082         kfree(urb->setup_packet);
1083
1084         kfree_skb(skb);
1085 }
1086
1087 static void btusb_isoc_tx_complete(struct urb *urb)
1088 {
1089         struct sk_buff *skb = urb->context;
1090         struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1091
1092         BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1093                urb->actual_length);
1094
1095         if (!test_bit(HCI_RUNNING, &hdev->flags))
1096                 goto done;
1097
1098         if (!urb->status)
1099                 hdev->stat.byte_tx += urb->transfer_buffer_length;
1100         else
1101                 hdev->stat.err_tx++;
1102
1103 done:
1104         kfree(urb->setup_packet);
1105
1106         kfree_skb(skb);
1107 }
1108
1109 static int btusb_open(struct hci_dev *hdev)
1110 {
1111         struct btusb_data *data = hci_get_drvdata(hdev);
1112         int err;
1113
1114         BT_DBG("%s", hdev->name);
1115
1116         err = usb_autopm_get_interface(data->intf);
1117         if (err < 0)
1118                 return err;
1119
1120         /* Patching USB firmware files prior to starting any URBs of HCI path
1121          * It is more safe to use USB bulk channel for downloading USB patch
1122          */
1123         if (data->setup_on_usb) {
1124                 err = data->setup_on_usb(hdev);
1125                 if (err < 0)
1126                         goto setup_fail;
1127         }
1128
1129         data->intf->needs_remote_wakeup = 1;
1130
1131         if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1132                 goto done;
1133
1134         err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1135         if (err < 0)
1136                 goto failed;
1137
1138         err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1139         if (err < 0) {
1140                 usb_kill_anchored_urbs(&data->intr_anchor);
1141                 goto failed;
1142         }
1143
1144         set_bit(BTUSB_BULK_RUNNING, &data->flags);
1145         btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1146
1147         if (data->diag) {
1148                 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1149                         set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1150         }
1151
1152 done:
1153         usb_autopm_put_interface(data->intf);
1154         return 0;
1155
1156 failed:
1157         clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1158 setup_fail:
1159         usb_autopm_put_interface(data->intf);
1160         return err;
1161 }
1162
1163 static void btusb_stop_traffic(struct btusb_data *data)
1164 {
1165         usb_kill_anchored_urbs(&data->intr_anchor);
1166         usb_kill_anchored_urbs(&data->bulk_anchor);
1167         usb_kill_anchored_urbs(&data->isoc_anchor);
1168         usb_kill_anchored_urbs(&data->diag_anchor);
1169 }
1170
1171 static int btusb_close(struct hci_dev *hdev)
1172 {
1173         struct btusb_data *data = hci_get_drvdata(hdev);
1174         int err;
1175
1176         BT_DBG("%s", hdev->name);
1177
1178         cancel_work_sync(&data->work);
1179         cancel_work_sync(&data->waker);
1180
1181         clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1182         clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1183         clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1184         clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1185
1186         btusb_stop_traffic(data);
1187         btusb_free_frags(data);
1188
1189         err = usb_autopm_get_interface(data->intf);
1190         if (err < 0)
1191                 goto failed;
1192
1193         data->intf->needs_remote_wakeup = 0;
1194         usb_autopm_put_interface(data->intf);
1195
1196 failed:
1197         usb_scuttle_anchored_urbs(&data->deferred);
1198         return 0;
1199 }
1200
1201 static int btusb_flush(struct hci_dev *hdev)
1202 {
1203         struct btusb_data *data = hci_get_drvdata(hdev);
1204
1205         BT_DBG("%s", hdev->name);
1206
1207         usb_kill_anchored_urbs(&data->tx_anchor);
1208         btusb_free_frags(data);
1209
1210         return 0;
1211 }
1212
1213 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1214 {
1215         struct btusb_data *data = hci_get_drvdata(hdev);
1216         struct usb_ctrlrequest *dr;
1217         struct urb *urb;
1218         unsigned int pipe;
1219
1220         urb = usb_alloc_urb(0, GFP_KERNEL);
1221         if (!urb)
1222                 return ERR_PTR(-ENOMEM);
1223
1224         dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1225         if (!dr) {
1226                 usb_free_urb(urb);
1227                 return ERR_PTR(-ENOMEM);
1228         }
1229
1230         dr->bRequestType = data->cmdreq_type;
1231         dr->bRequest     = data->cmdreq;
1232         dr->wIndex       = 0;
1233         dr->wValue       = 0;
1234         dr->wLength      = __cpu_to_le16(skb->len);
1235
1236         pipe = usb_sndctrlpipe(data->udev, 0x00);
1237
1238         usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1239                              skb->data, skb->len, btusb_tx_complete, skb);
1240
1241         skb->dev = (void *)hdev;
1242
1243         return urb;
1244 }
1245
1246 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1247 {
1248         struct btusb_data *data = hci_get_drvdata(hdev);
1249         struct urb *urb;
1250         unsigned int pipe;
1251
1252         if (!data->bulk_tx_ep)
1253                 return ERR_PTR(-ENODEV);
1254
1255         urb = usb_alloc_urb(0, GFP_KERNEL);
1256         if (!urb)
1257                 return ERR_PTR(-ENOMEM);
1258
1259         pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1260
1261         usb_fill_bulk_urb(urb, data->udev, pipe,
1262                           skb->data, skb->len, btusb_tx_complete, skb);
1263
1264         skb->dev = (void *)hdev;
1265
1266         return urb;
1267 }
1268
1269 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1270 {
1271         struct btusb_data *data = hci_get_drvdata(hdev);
1272         struct urb *urb;
1273         unsigned int pipe;
1274
1275         if (!data->isoc_tx_ep)
1276                 return ERR_PTR(-ENODEV);
1277
1278         urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1279         if (!urb)
1280                 return ERR_PTR(-ENOMEM);
1281
1282         pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1283
1284         usb_fill_int_urb(urb, data->udev, pipe,
1285                          skb->data, skb->len, btusb_isoc_tx_complete,
1286                          skb, data->isoc_tx_ep->bInterval);
1287
1288         urb->transfer_flags  = URB_ISO_ASAP;
1289
1290         __fill_isoc_descriptor(urb, skb->len,
1291                                le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1292
1293         skb->dev = (void *)hdev;
1294
1295         return urb;
1296 }
1297
1298 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1299 {
1300         struct btusb_data *data = hci_get_drvdata(hdev);
1301         int err;
1302
1303         usb_anchor_urb(urb, &data->tx_anchor);
1304
1305         err = usb_submit_urb(urb, GFP_KERNEL);
1306         if (err < 0) {
1307                 if (err != -EPERM && err != -ENODEV)
1308                         BT_ERR("%s urb %p submission failed (%d)",
1309                                hdev->name, urb, -err);
1310                 kfree(urb->setup_packet);
1311                 usb_unanchor_urb(urb);
1312         } else {
1313                 usb_mark_last_busy(data->udev);
1314         }
1315
1316         usb_free_urb(urb);
1317         return err;
1318 }
1319
1320 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1321 {
1322         struct btusb_data *data = hci_get_drvdata(hdev);
1323         unsigned long flags;
1324         bool suspending;
1325
1326         spin_lock_irqsave(&data->txlock, flags);
1327         suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1328         if (!suspending)
1329                 data->tx_in_flight++;
1330         spin_unlock_irqrestore(&data->txlock, flags);
1331
1332         if (!suspending)
1333                 return submit_tx_urb(hdev, urb);
1334
1335         usb_anchor_urb(urb, &data->deferred);
1336         schedule_work(&data->waker);
1337
1338         usb_free_urb(urb);
1339         return 0;
1340 }
1341
1342 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1343 {
1344         struct urb *urb;
1345
1346         BT_DBG("%s", hdev->name);
1347
1348         switch (hci_skb_pkt_type(skb)) {
1349         case HCI_COMMAND_PKT:
1350                 urb = alloc_ctrl_urb(hdev, skb);
1351                 if (IS_ERR(urb))
1352                         return PTR_ERR(urb);
1353
1354                 hdev->stat.cmd_tx++;
1355                 return submit_or_queue_tx_urb(hdev, urb);
1356
1357         case HCI_ACLDATA_PKT:
1358                 urb = alloc_bulk_urb(hdev, skb);
1359                 if (IS_ERR(urb))
1360                         return PTR_ERR(urb);
1361
1362                 hdev->stat.acl_tx++;
1363                 return submit_or_queue_tx_urb(hdev, urb);
1364
1365         case HCI_SCODATA_PKT:
1366                 if (hci_conn_num(hdev, SCO_LINK) < 1)
1367                         return -ENODEV;
1368
1369                 urb = alloc_isoc_urb(hdev, skb);
1370                 if (IS_ERR(urb))
1371                         return PTR_ERR(urb);
1372
1373                 hdev->stat.sco_tx++;
1374                 return submit_tx_urb(hdev, urb);
1375         }
1376
1377         return -EILSEQ;
1378 }
1379
1380 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1381 {
1382         struct btusb_data *data = hci_get_drvdata(hdev);
1383
1384         BT_DBG("%s evt %d", hdev->name, evt);
1385
1386         if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1387                 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1388                 schedule_work(&data->work);
1389         }
1390 }
1391
1392 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1393 {
1394         struct btusb_data *data = hci_get_drvdata(hdev);
1395         struct usb_interface *intf = data->isoc;
1396         struct usb_endpoint_descriptor *ep_desc;
1397         int i, err;
1398
1399         if (!data->isoc)
1400                 return -ENODEV;
1401
1402         err = usb_set_interface(data->udev, 1, altsetting);
1403         if (err < 0) {
1404                 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1405                 return err;
1406         }
1407
1408         data->isoc_altsetting = altsetting;
1409
1410         data->isoc_tx_ep = NULL;
1411         data->isoc_rx_ep = NULL;
1412
1413         for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1414                 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1415
1416                 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1417                         data->isoc_tx_ep = ep_desc;
1418                         continue;
1419                 }
1420
1421                 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1422                         data->isoc_rx_ep = ep_desc;
1423                         continue;
1424                 }
1425         }
1426
1427         if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1428                 BT_ERR("%s invalid SCO descriptors", hdev->name);
1429                 return -ENODEV;
1430         }
1431
1432         return 0;
1433 }
1434
1435 static void btusb_work(struct work_struct *work)
1436 {
1437         struct btusb_data *data = container_of(work, struct btusb_data, work);
1438         struct hci_dev *hdev = data->hdev;
1439         int new_alts;
1440         int err;
1441
1442         if (data->sco_num > 0) {
1443                 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1444                         err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1445                         if (err < 0) {
1446                                 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1447                                 usb_kill_anchored_urbs(&data->isoc_anchor);
1448                                 return;
1449                         }
1450
1451                         set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1452                 }
1453
1454                 if (hdev->voice_setting & 0x0020) {
1455                         static const int alts[3] = { 2, 4, 5 };
1456
1457                         new_alts = alts[data->sco_num - 1];
1458                 } else {
1459                         new_alts = data->sco_num;
1460                 }
1461
1462                 if (data->isoc_altsetting != new_alts) {
1463                         unsigned long flags;
1464
1465                         clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1466                         usb_kill_anchored_urbs(&data->isoc_anchor);
1467
1468                         /* When isochronous alternate setting needs to be
1469                          * changed, because SCO connection has been added
1470                          * or removed, a packet fragment may be left in the
1471                          * reassembling state. This could lead to wrongly
1472                          * assembled fragments.
1473                          *
1474                          * Clear outstanding fragment when selecting a new
1475                          * alternate setting.
1476                          */
1477                         spin_lock_irqsave(&data->rxlock, flags);
1478                         kfree_skb(data->sco_skb);
1479                         data->sco_skb = NULL;
1480                         spin_unlock_irqrestore(&data->rxlock, flags);
1481
1482                         if (__set_isoc_interface(hdev, new_alts) < 0)
1483                                 return;
1484                 }
1485
1486                 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1487                         if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1488                                 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1489                         else
1490                                 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1491                 }
1492         } else {
1493                 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1494                 usb_kill_anchored_urbs(&data->isoc_anchor);
1495
1496                 __set_isoc_interface(hdev, 0);
1497                 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1498                         usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1499         }
1500 }
1501
1502 static void btusb_waker(struct work_struct *work)
1503 {
1504         struct btusb_data *data = container_of(work, struct btusb_data, waker);
1505         int err;
1506
1507         err = usb_autopm_get_interface(data->intf);
1508         if (err < 0)
1509                 return;
1510
1511         usb_autopm_put_interface(data->intf);
1512 }
1513
1514 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1515 {
1516         struct sk_buff *skb;
1517         u8 val = 0x00;
1518
1519         BT_DBG("%s", hdev->name);
1520
1521         skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1522         if (IS_ERR(skb))
1523                 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1524         else
1525                 kfree_skb(skb);
1526
1527         return 0;
1528 }
1529
1530 static int btusb_setup_csr(struct hci_dev *hdev)
1531 {
1532         struct hci_rp_read_local_version *rp;
1533         struct sk_buff *skb;
1534
1535         BT_DBG("%s", hdev->name);
1536
1537         skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1538                              HCI_INIT_TIMEOUT);
1539         if (IS_ERR(skb)) {
1540                 int err = PTR_ERR(skb);
1541                 BT_ERR("%s: CSR: Local version failed (%d)", hdev->name, err);
1542                 return err;
1543         }
1544
1545         if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1546                 BT_ERR("%s: CSR: Local version length mismatch", hdev->name);
1547                 kfree_skb(skb);
1548                 return -EIO;
1549         }
1550
1551         rp = (struct hci_rp_read_local_version *)skb->data;
1552
1553         /* Detect controllers which aren't real CSR ones. */
1554         if (le16_to_cpu(rp->manufacturer) != 10 ||
1555             le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
1556                 /* Clear the reset quirk since this is not an actual
1557                  * early Bluetooth 1.1 device from CSR.
1558                  */
1559                 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1560
1561                 /* These fake CSR controllers have all a broken
1562                  * stored link key handling and so just disable it.
1563                  */
1564                 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1565         }
1566
1567         kfree_skb(skb);
1568
1569         return 0;
1570 }
1571
1572 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1573                                                        struct intel_version *ver)
1574 {
1575         const struct firmware *fw;
1576         char fwname[64];
1577         int ret;
1578
1579         snprintf(fwname, sizeof(fwname),
1580                  "/*(DEBLOBBED)*/",
1581                  ver->hw_platform, ver->hw_variant, ver->hw_revision,
1582                  ver->fw_variant,  ver->fw_revision, ver->fw_build_num,
1583                  ver->fw_build_ww, ver->fw_build_yy);
1584
1585         ret = reject_firmware(&fw, fwname, &hdev->dev);
1586         if (ret < 0) {
1587                 if (ret == -EINVAL) {
1588                         BT_ERR("%s Intel firmware file request failed (%d)",
1589                                hdev->name, ret);
1590                         return NULL;
1591                 }
1592
1593                 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1594                        hdev->name, fwname, ret);
1595
1596                 /* If the correct firmware patch file is not found, use the
1597                  * default firmware patch file instead
1598                  */
1599                 snprintf(fwname, sizeof(fwname), "/*(DEBLOBBED)*/",
1600                          ver->hw_platform, ver->hw_variant);
1601                 if (reject_firmware(&fw, fwname, &hdev->dev) < 0) {
1602                         BT_ERR("%s failed to open default Intel fw file: %s",
1603                                hdev->name, fwname);
1604                         return NULL;
1605                 }
1606         }
1607
1608         BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1609
1610         return fw;
1611 }
1612
1613 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1614                                       const struct firmware *fw,
1615                                       const u8 **fw_ptr, int *disable_patch)
1616 {
1617         struct sk_buff *skb;
1618         struct hci_command_hdr *cmd;
1619         const u8 *cmd_param;
1620         struct hci_event_hdr *evt = NULL;
1621         const u8 *evt_param = NULL;
1622         int remain = fw->size - (*fw_ptr - fw->data);
1623
1624         /* The first byte indicates the types of the patch command or event.
1625          * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1626          * in the current firmware buffer doesn't start with 0x01 or
1627          * the size of remain buffer is smaller than HCI command header,
1628          * the firmware file is corrupted and it should stop the patching
1629          * process.
1630          */
1631         if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1632                 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1633                 return -EINVAL;
1634         }
1635         (*fw_ptr)++;
1636         remain--;
1637
1638         cmd = (struct hci_command_hdr *)(*fw_ptr);
1639         *fw_ptr += sizeof(*cmd);
1640         remain -= sizeof(*cmd);
1641
1642         /* Ensure that the remain firmware data is long enough than the length
1643          * of command parameter. If not, the firmware file is corrupted.
1644          */
1645         if (remain < cmd->plen) {
1646                 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1647                 return -EFAULT;
1648         }
1649
1650         /* If there is a command that loads a patch in the firmware
1651          * file, then enable the patch upon success, otherwise just
1652          * disable the manufacturer mode, for example patch activation
1653          * is not required when the default firmware patch file is used
1654          * because there are no patch data to load.
1655          */
1656         if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1657                 *disable_patch = 0;
1658
1659         cmd_param = *fw_ptr;
1660         *fw_ptr += cmd->plen;
1661         remain -= cmd->plen;
1662
1663         /* This reads the expected events when the above command is sent to the
1664          * device. Some vendor commands expects more than one events, for
1665          * example command status event followed by vendor specific event.
1666          * For this case, it only keeps the last expected event. so the command
1667          * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1668          * last expected event.
1669          */
1670         while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1671                 (*fw_ptr)++;
1672                 remain--;
1673
1674                 evt = (struct hci_event_hdr *)(*fw_ptr);
1675                 *fw_ptr += sizeof(*evt);
1676                 remain -= sizeof(*evt);
1677
1678                 if (remain < evt->plen) {
1679                         BT_ERR("%s Intel fw corrupted: invalid evt len",
1680                                hdev->name);
1681                         return -EFAULT;
1682                 }
1683
1684                 evt_param = *fw_ptr;
1685                 *fw_ptr += evt->plen;
1686                 remain -= evt->plen;
1687         }
1688
1689         /* Every HCI commands in the firmware file has its correspond event.
1690          * If event is not found or remain is smaller than zero, the firmware
1691          * file is corrupted.
1692          */
1693         if (!evt || !evt_param || remain < 0) {
1694                 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1695                 return -EFAULT;
1696         }
1697
1698         skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1699                                 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1700         if (IS_ERR(skb)) {
1701                 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1702                        hdev->name, cmd->opcode, PTR_ERR(skb));
1703                 return PTR_ERR(skb);
1704         }
1705
1706         /* It ensures that the returned event matches the event data read from
1707          * the firmware file. At fist, it checks the length and then
1708          * the contents of the event.
1709          */
1710         if (skb->len != evt->plen) {
1711                 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1712                        le16_to_cpu(cmd->opcode));
1713                 kfree_skb(skb);
1714                 return -EFAULT;
1715         }
1716
1717         if (memcmp(skb->data, evt_param, evt->plen)) {
1718                 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1719                        hdev->name, le16_to_cpu(cmd->opcode));
1720                 kfree_skb(skb);
1721                 return -EFAULT;
1722         }
1723         kfree_skb(skb);
1724
1725         return 0;
1726 }
1727
1728 static int btusb_setup_intel(struct hci_dev *hdev)
1729 {
1730         struct sk_buff *skb;
1731         const struct firmware *fw;
1732         const u8 *fw_ptr;
1733         int disable_patch, err;
1734         struct intel_version ver;
1735
1736         BT_DBG("%s", hdev->name);
1737
1738         /* The controller has a bug with the first HCI command sent to it
1739          * returning number of completed commands as zero. This would stall the
1740          * command processing in the Bluetooth core.
1741          *
1742          * As a workaround, send HCI Reset command first which will reset the
1743          * number of completed commands and allow normal command processing
1744          * from now on.
1745          */
1746         skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1747         if (IS_ERR(skb)) {
1748                 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1749                        hdev->name, PTR_ERR(skb));
1750                 return PTR_ERR(skb);
1751         }
1752         kfree_skb(skb);
1753
1754         /* Read Intel specific controller version first to allow selection of
1755          * which firmware file to load.
1756          *
1757          * The returned information are hardware variant and revision plus
1758          * firmware variant, revision and build number.
1759          */
1760         err = btintel_read_version(hdev, &ver);
1761         if (err)
1762                 return err;
1763
1764         BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1765                 hdev->name, ver.hw_platform, ver.hw_variant, ver.hw_revision,
1766                 ver.fw_variant,  ver.fw_revision, ver.fw_build_num,
1767                 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
1768
1769         /* fw_patch_num indicates the version of patch the device currently
1770          * have. If there is no patch data in the device, it is always 0x00.
1771          * So, if it is other than 0x00, no need to patch the device again.
1772          */
1773         if (ver.fw_patch_num) {
1774                 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1775                         hdev->name, ver.fw_patch_num);
1776                 goto complete;
1777         }
1778
1779         /* Opens the firmware patch file based on the firmware version read
1780          * from the controller. If it fails to open the matching firmware
1781          * patch file, it tries to open the default firmware patch file.
1782          * If no patch file is found, allow the device to operate without
1783          * a patch.
1784          */
1785         fw = btusb_setup_intel_get_fw(hdev, &ver);
1786         if (!fw)
1787                 goto complete;
1788         fw_ptr = fw->data;
1789
1790         /* Enable the manufacturer mode of the controller.
1791          * Only while this mode is enabled, the driver can download the
1792          * firmware patch data and configuration parameters.
1793          */
1794         err = btintel_enter_mfg(hdev);
1795         if (err) {
1796                 release_firmware(fw);
1797                 return err;
1798         }
1799
1800         disable_patch = 1;
1801
1802         /* The firmware data file consists of list of Intel specific HCI
1803          * commands and its expected events. The first byte indicates the
1804          * type of the message, either HCI command or HCI event.
1805          *
1806          * It reads the command and its expected event from the firmware file,
1807          * and send to the controller. Once __hci_cmd_sync_ev() returns,
1808          * the returned event is compared with the event read from the firmware
1809          * file and it will continue until all the messages are downloaded to
1810          * the controller.
1811          *
1812          * Once the firmware patching is completed successfully,
1813          * the manufacturer mode is disabled with reset and activating the
1814          * downloaded patch.
1815          *
1816          * If the firmware patching fails, the manufacturer mode is
1817          * disabled with reset and deactivating the patch.
1818          *
1819          * If the default patch file is used, no reset is done when disabling
1820          * the manufacturer.
1821          */
1822         while (fw->size > fw_ptr - fw->data) {
1823                 int ret;
1824
1825                 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1826                                                  &disable_patch);
1827                 if (ret < 0)
1828                         goto exit_mfg_deactivate;
1829         }
1830
1831         release_firmware(fw);
1832
1833         if (disable_patch)
1834                 goto exit_mfg_disable;
1835
1836         /* Patching completed successfully and disable the manufacturer mode
1837          * with reset and activate the downloaded firmware patches.
1838          */
1839         err = btintel_exit_mfg(hdev, true, true);
1840         if (err)
1841                 return err;
1842
1843         BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1844                 hdev->name);
1845
1846         goto complete;
1847
1848 exit_mfg_disable:
1849         /* Disable the manufacturer mode without reset */
1850         err = btintel_exit_mfg(hdev, false, false);
1851         if (err)
1852                 return err;
1853
1854         BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
1855
1856         goto complete;
1857
1858 exit_mfg_deactivate:
1859         release_firmware(fw);
1860
1861         /* Patching failed. Disable the manufacturer mode with reset and
1862          * deactivate the downloaded firmware patches.
1863          */
1864         err = btintel_exit_mfg(hdev, true, false);
1865         if (err)
1866                 return err;
1867
1868         BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1869                 hdev->name);
1870
1871 complete:
1872         /* Set the event mask for Intel specific vendor events. This enables
1873          * a few extra events that are useful during general operation.
1874          */
1875         btintel_set_event_mask_mfg(hdev, false);
1876
1877         btintel_check_bdaddr(hdev);
1878         return 0;
1879 }
1880
1881 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1882 {
1883         struct sk_buff *skb;
1884         struct hci_event_hdr *hdr;
1885         struct hci_ev_cmd_complete *evt;
1886
1887         skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1888         if (!skb)
1889                 return -ENOMEM;
1890
1891         hdr = skb_put(skb, sizeof(*hdr));
1892         hdr->evt = HCI_EV_CMD_COMPLETE;
1893         hdr->plen = sizeof(*evt) + 1;
1894
1895         evt = skb_put(skb, sizeof(*evt));
1896         evt->ncmd = 0x01;
1897         evt->opcode = cpu_to_le16(opcode);
1898
1899         skb_put_u8(skb, 0x00);
1900
1901         hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
1902
1903         return hci_recv_frame(hdev, skb);
1904 }
1905
1906 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1907                                  int count)
1908 {
1909         /* When the device is in bootloader mode, then it can send
1910          * events via the bulk endpoint. These events are treated the
1911          * same way as the ones received from the interrupt endpoint.
1912          */
1913         if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1914                 return btusb_recv_intr(data, buffer, count);
1915
1916         return btusb_recv_bulk(data, buffer, count);
1917 }
1918
1919 static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1920                                unsigned int len)
1921 {
1922         const struct intel_bootup *evt = ptr;
1923
1924         if (len != sizeof(*evt))
1925                 return;
1926
1927         if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1928                 smp_mb__after_atomic();
1929                 wake_up_bit(&data->flags, BTUSB_BOOTING);
1930         }
1931 }
1932
1933 static void btusb_intel_secure_send_result(struct btusb_data *data,
1934                                            const void *ptr, unsigned int len)
1935 {
1936         const struct intel_secure_send_result *evt = ptr;
1937
1938         if (len != sizeof(*evt))
1939                 return;
1940
1941         if (evt->result)
1942                 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1943
1944         if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1945             test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1946                 smp_mb__after_atomic();
1947                 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1948         }
1949 }
1950
1951 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1952 {
1953         struct btusb_data *data = hci_get_drvdata(hdev);
1954
1955         if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1956                 struct hci_event_hdr *hdr = (void *)skb->data;
1957
1958                 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1959                     hdr->plen > 0) {
1960                         const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1961                         unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
1962
1963                         switch (skb->data[2]) {
1964                         case 0x02:
1965                                 /* When switching to the operational firmware
1966                                  * the device sends a vendor specific event
1967                                  * indicating that the bootup completed.
1968                                  */
1969                                 btusb_intel_bootup(data, ptr, len);
1970                                 break;
1971                         case 0x06:
1972                                 /* When the firmware loading completes the
1973                                  * device sends out a vendor specific event
1974                                  * indicating the result of the firmware
1975                                  * loading.
1976                                  */
1977                                 btusb_intel_secure_send_result(data, ptr, len);
1978                                 break;
1979                         }
1980                 }
1981         }
1982
1983         return hci_recv_frame(hdev, skb);
1984 }
1985
1986 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1987 {
1988         struct btusb_data *data = hci_get_drvdata(hdev);
1989         struct urb *urb;
1990
1991         BT_DBG("%s", hdev->name);
1992
1993         switch (hci_skb_pkt_type(skb)) {
1994         case HCI_COMMAND_PKT:
1995                 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1996                         struct hci_command_hdr *cmd = (void *)skb->data;
1997                         __u16 opcode = le16_to_cpu(cmd->opcode);
1998
1999                         /* When in bootloader mode and the command 0xfc09
2000                          * is received, it needs to be send down the
2001                          * bulk endpoint. So allocate a bulk URB instead.
2002                          */
2003                         if (opcode == 0xfc09)
2004                                 urb = alloc_bulk_urb(hdev, skb);
2005                         else
2006                                 urb = alloc_ctrl_urb(hdev, skb);
2007
2008                         /* When the 0xfc01 command is issued to boot into
2009                          * the operational firmware, it will actually not
2010                          * send a command complete event. To keep the flow
2011                          * control working inject that event here.
2012                          */
2013                         if (opcode == 0xfc01)
2014                                 inject_cmd_complete(hdev, opcode);
2015                 } else {
2016                         urb = alloc_ctrl_urb(hdev, skb);
2017                 }
2018                 if (IS_ERR(urb))
2019                         return PTR_ERR(urb);
2020
2021                 hdev->stat.cmd_tx++;
2022                 return submit_or_queue_tx_urb(hdev, urb);
2023
2024         case HCI_ACLDATA_PKT:
2025                 urb = alloc_bulk_urb(hdev, skb);
2026                 if (IS_ERR(urb))
2027                         return PTR_ERR(urb);
2028
2029                 hdev->stat.acl_tx++;
2030                 return submit_or_queue_tx_urb(hdev, urb);
2031
2032         case HCI_SCODATA_PKT:
2033                 if (hci_conn_num(hdev, SCO_LINK) < 1)
2034                         return -ENODEV;
2035
2036                 urb = alloc_isoc_urb(hdev, skb);
2037                 if (IS_ERR(urb))
2038                         return PTR_ERR(urb);
2039
2040                 hdev->stat.sco_tx++;
2041                 return submit_tx_urb(hdev, urb);
2042         }
2043
2044         return -EILSEQ;
2045 }
2046
2047 static int btusb_setup_intel_new(struct hci_dev *hdev)
2048 {
2049         static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
2050                                           0x00, 0x08, 0x04, 0x00 };
2051         struct btusb_data *data = hci_get_drvdata(hdev);
2052         struct sk_buff *skb;
2053         struct intel_version ver;
2054         struct intel_boot_params *params;
2055         const struct firmware *fw;
2056         const u8 *fw_ptr;
2057         u32 frag_len;
2058         char fwname[64];
2059         ktime_t calltime, delta, rettime;
2060         unsigned long long duration;
2061         int err;
2062
2063         BT_DBG("%s", hdev->name);
2064
2065         calltime = ktime_get();
2066
2067         /* Read the Intel version information to determine if the device
2068          * is in bootloader mode or if it already has operational firmware
2069          * loaded.
2070          */
2071         err = btintel_read_version(hdev, &ver);
2072         if (err)
2073                 return err;
2074
2075         /* The hardware platform number has a fixed value of 0x37 and
2076          * for now only accept this single value.
2077          */
2078         if (ver.hw_platform != 0x37) {
2079                 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2080                        hdev->name, ver.hw_platform);
2081                 return -EINVAL;
2082         }
2083
2084         /* Check for supported iBT hardware variants of this firmware
2085          * loading method.
2086          *
2087          * This check has been put in place to ensure correct forward
2088          * compatibility options when newer hardware variants come along.
2089          */
2090         switch (ver.hw_variant) {
2091         case 0x0b:      /* SfP */
2092         case 0x0c:      /* WsP */
2093         case 0x11:      /* JfP */
2094         case 0x12:      /* ThP */
2095                 break;
2096         default:
2097                 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2098                        hdev->name, ver.hw_variant);
2099                 return -EINVAL;
2100         }
2101
2102         btintel_version_info(hdev, &ver);
2103
2104         /* The firmware variant determines if the device is in bootloader
2105          * mode or is running operational firmware. The value 0x06 identifies
2106          * the bootloader and the value 0x23 identifies the operational
2107          * firmware.
2108          *
2109          * When the operational firmware is already present, then only
2110          * the check for valid Bluetooth device address is needed. This
2111          * determines if the device will be added as configured or
2112          * unconfigured controller.
2113          *
2114          * It is not possible to use the Secure Boot Parameters in this
2115          * case since that command is only available in bootloader mode.
2116          */
2117         if (ver.fw_variant == 0x23) {
2118                 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2119                 btintel_check_bdaddr(hdev);
2120                 return 0;
2121         }
2122
2123         /* If the device is not in bootloader mode, then the only possible
2124          * choice is to return an error and abort the device initialization.
2125          */
2126         if (ver.fw_variant != 0x06) {
2127                 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2128                        hdev->name, ver.fw_variant);
2129                 return -ENODEV;
2130         }
2131
2132         /* Read the secure boot parameters to identify the operating
2133          * details of the bootloader.
2134          */
2135         skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2136         if (IS_ERR(skb)) {
2137                 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2138                        hdev->name, PTR_ERR(skb));
2139                 return PTR_ERR(skb);
2140         }
2141
2142         if (skb->len != sizeof(*params)) {
2143                 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2144                 kfree_skb(skb);
2145                 return -EILSEQ;
2146         }
2147
2148         params = (struct intel_boot_params *)skb->data;
2149
2150         BT_INFO("%s: Device revision is %u", hdev->name,
2151                 le16_to_cpu(params->dev_revid));
2152
2153         BT_INFO("%s: Secure boot is %s", hdev->name,
2154                 params->secure_boot ? "enabled" : "disabled");
2155
2156         BT_INFO("%s: OTP lock is %s", hdev->name,
2157                 params->otp_lock ? "enabled" : "disabled");
2158
2159         BT_INFO("%s: API lock is %s", hdev->name,
2160                 params->api_lock ? "enabled" : "disabled");
2161
2162         BT_INFO("%s: Debug lock is %s", hdev->name,
2163                 params->debug_lock ? "enabled" : "disabled");
2164
2165         BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2166                 params->min_fw_build_nn, params->min_fw_build_cw,
2167                 2000 + params->min_fw_build_yy);
2168
2169         /* It is required that every single firmware fragment is acknowledged
2170          * with a command complete event. If the boot parameters indicate
2171          * that this bootloader does not send them, then abort the setup.
2172          */
2173         if (params->limited_cce != 0x00) {
2174                 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2175                        hdev->name, params->limited_cce);
2176                 kfree_skb(skb);
2177                 return -EINVAL;
2178         }
2179
2180         /* If the OTP has no valid Bluetooth device address, then there will
2181          * also be no valid address for the operational firmware.
2182          */
2183         if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2184                 BT_INFO("%s: No device address configured", hdev->name);
2185                 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2186         }
2187
2188         /* With this Intel bootloader only the hardware variant and device
2189          * revision information are used to select the right firmware.
2190          *
2191          * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2192          *
2193          * Currently the supported hardware variants are:
2194          *   11 (0x0b) for iBT3.0 (LnP/SfP)
2195          *   12 (0x0c) for iBT3.5 (WsP)
2196          *   17 (0x11) for iBT3.5 (JfP)
2197          *   18 (0x12) for iBT3.5 (ThP)
2198          */
2199         snprintf(fwname, sizeof(fwname), "/*(DEBLOBBED)*/",
2200                  le16_to_cpu(ver.hw_variant),
2201                  le16_to_cpu(params->dev_revid));
2202
2203         err = reject_firmware(&fw, fwname, &hdev->dev);
2204         if (err < 0) {
2205                 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2206                        hdev->name, err);
2207                 kfree_skb(skb);
2208                 return err;
2209         }
2210
2211         BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2212
2213         /* Save the DDC file name for later use to apply once the firmware
2214          * downloading is done.
2215          */
2216         snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
2217                  le16_to_cpu(ver.hw_variant),
2218                  le16_to_cpu(params->dev_revid));
2219
2220         kfree_skb(skb);
2221
2222         if (fw->size < 644) {
2223                 BT_ERR("%s: Invalid size of firmware file (%zu)",
2224                        hdev->name, fw->size);
2225                 err = -EBADF;
2226                 goto done;
2227         }
2228
2229         set_bit(BTUSB_DOWNLOADING, &data->flags);
2230
2231         /* Start the firmware download transaction with the Init fragment
2232          * represented by the 128 bytes of CSS header.
2233          */
2234         err = btintel_secure_send(hdev, 0x00, 128, fw->data);
2235         if (err < 0) {
2236                 BT_ERR("%s: Failed to send firmware header (%d)",
2237                        hdev->name, err);
2238                 goto done;
2239         }
2240
2241         /* Send the 256 bytes of public key information from the firmware
2242          * as the PKey fragment.
2243          */
2244         err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
2245         if (err < 0) {
2246                 BT_ERR("%s: Failed to send firmware public key (%d)",
2247                        hdev->name, err);
2248                 goto done;
2249         }
2250
2251         /* Send the 256 bytes of signature information from the firmware
2252          * as the Sign fragment.
2253          */
2254         err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
2255         if (err < 0) {
2256                 BT_ERR("%s: Failed to send firmware signature (%d)",
2257                        hdev->name, err);
2258                 goto done;
2259         }
2260
2261         fw_ptr = fw->data + 644;
2262         frag_len = 0;
2263
2264         while (fw_ptr - fw->data < fw->size) {
2265                 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
2266
2267                 frag_len += sizeof(*cmd) + cmd->plen;
2268
2269                 /* The parameter length of the secure send command requires
2270                  * a 4 byte alignment. It happens so that the firmware file
2271                  * contains proper Intel_NOP commands to align the fragments
2272                  * as needed.
2273                  *
2274                  * Send set of commands with 4 byte alignment from the
2275                  * firmware data buffer as a single Data fragement.
2276                  */
2277                 if (!(frag_len % 4)) {
2278                         err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
2279                         if (err < 0) {
2280                                 BT_ERR("%s: Failed to send firmware data (%d)",
2281                                        hdev->name, err);
2282                                 goto done;
2283                         }
2284
2285                         fw_ptr += frag_len;
2286                         frag_len = 0;
2287                 }
2288         }
2289
2290         set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2291
2292         BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2293
2294         /* Before switching the device into operational mode and with that
2295          * booting the loaded firmware, wait for the bootloader notification
2296          * that all fragments have been successfully received.
2297          *
2298          * When the event processing receives the notification, then the
2299          * BTUSB_DOWNLOADING flag will be cleared.
2300          *
2301          * The firmware loading should not take longer than 5 seconds
2302          * and thus just timeout if that happens and fail the setup
2303          * of this device.
2304          */
2305         err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2306                                   TASK_INTERRUPTIBLE,
2307                                   msecs_to_jiffies(5000));
2308         if (err == -EINTR) {
2309                 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2310                 goto done;
2311         }
2312
2313         if (err) {
2314                 BT_ERR("%s: Firmware loading timeout", hdev->name);
2315                 err = -ETIMEDOUT;
2316                 goto done;
2317         }
2318
2319         if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2320                 BT_ERR("%s: Firmware loading failed", hdev->name);
2321                 err = -ENOEXEC;
2322                 goto done;
2323         }
2324
2325         rettime = ktime_get();
2326         delta = ktime_sub(rettime, calltime);
2327         duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2328
2329         BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2330
2331 done:
2332         release_firmware(fw);
2333
2334         if (err < 0)
2335                 return err;
2336
2337         calltime = ktime_get();
2338
2339         set_bit(BTUSB_BOOTING, &data->flags);
2340
2341         skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2342                              HCI_INIT_TIMEOUT);
2343         if (IS_ERR(skb))
2344                 return PTR_ERR(skb);
2345
2346         kfree_skb(skb);
2347
2348         /* The bootloader will not indicate when the device is ready. This
2349          * is done by the operational firmware sending bootup notification.
2350          *
2351          * Booting into operational firmware should not take longer than
2352          * 1 second. However if that happens, then just fail the setup
2353          * since something went wrong.
2354          */
2355         BT_INFO("%s: Waiting for device to boot", hdev->name);
2356
2357         err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2358                                   TASK_INTERRUPTIBLE,
2359                                   msecs_to_jiffies(1000));
2360
2361         if (err == -EINTR) {
2362                 BT_ERR("%s: Device boot interrupted", hdev->name);
2363                 return -EINTR;
2364         }
2365
2366         if (err) {
2367                 BT_ERR("%s: Device boot timeout", hdev->name);
2368                 return -ETIMEDOUT;
2369         }
2370
2371         rettime = ktime_get();
2372         delta = ktime_sub(rettime, calltime);
2373         duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2374
2375         BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2376
2377         clear_bit(BTUSB_BOOTLOADER, &data->flags);
2378
2379         /* Once the device is running in operational mode, it needs to apply
2380          * the device configuration (DDC) parameters.
2381          *
2382          * The device can work without DDC parameters, so even if it fails
2383          * to load the file, no need to fail the setup.
2384          */
2385         btintel_load_ddc_config(hdev, fwname);
2386
2387         /* Set the event mask for Intel specific vendor events. This enables
2388          * a few extra events that are useful during general operation. It
2389          * does not enable any debugging related events.
2390          *
2391          * The device will function correctly without these events enabled
2392          * and thus no need to fail the setup.
2393          */
2394         btintel_set_event_mask(hdev, false);
2395
2396         return 0;
2397 }
2398
2399 static int btusb_shutdown_intel(struct hci_dev *hdev)
2400 {
2401         struct sk_buff *skb;
2402         long ret;
2403
2404         /* Some platforms have an issue with BT LED when the interface is
2405          * down or BT radio is turned off, which takes 5 seconds to BT LED
2406          * goes off. This command turns off the BT LED immediately.
2407          */
2408         skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2409         if (IS_ERR(skb)) {
2410                 ret = PTR_ERR(skb);
2411                 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2412                        hdev->name, ret);
2413                 return ret;
2414         }
2415         kfree_skb(skb);
2416
2417         return 0;
2418 }
2419
2420 #ifdef CONFIG_PM
2421 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
2422 static int marvell_config_oob_wake(struct hci_dev *hdev)
2423 {
2424         struct sk_buff *skb;
2425         struct btusb_data *data = hci_get_drvdata(hdev);
2426         struct device *dev = &data->udev->dev;
2427         u16 pin, gap, opcode;
2428         int ret;
2429         u8 cmd[5];
2430
2431         /* Move on if no wakeup pin specified */
2432         if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
2433             of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
2434                 return 0;
2435
2436         /* Vendor specific command to configure a GPIO as wake-up pin */
2437         opcode = hci_opcode_pack(0x3F, 0x59);
2438         cmd[0] = opcode & 0xFF;
2439         cmd[1] = opcode >> 8;
2440         cmd[2] = 2; /* length of parameters that follow */
2441         cmd[3] = pin;
2442         cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
2443
2444         skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
2445         if (!skb) {
2446                 bt_dev_err(hdev, "%s: No memory\n", __func__);
2447                 return -ENOMEM;
2448         }
2449
2450         skb_put_data(skb, cmd, sizeof(cmd));
2451         hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
2452
2453         ret = btusb_send_frame(hdev, skb);
2454         if (ret) {
2455                 bt_dev_err(hdev, "%s: configuration failed\n", __func__);
2456                 kfree_skb(skb);
2457                 return ret;
2458         }
2459
2460         return 0;
2461 }
2462 #endif
2463
2464 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2465                                     const bdaddr_t *bdaddr)
2466 {
2467         struct sk_buff *skb;
2468         u8 buf[8];
2469         long ret;
2470
2471         buf[0] = 0xfe;
2472         buf[1] = sizeof(bdaddr_t);
2473         memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2474
2475         skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2476         if (IS_ERR(skb)) {
2477                 ret = PTR_ERR(skb);
2478                 BT_ERR("%s: changing Marvell device address failed (%ld)",
2479                        hdev->name, ret);
2480                 return ret;
2481         }
2482         kfree_skb(skb);
2483
2484         return 0;
2485 }
2486
2487 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2488                                     const bdaddr_t *bdaddr)
2489 {
2490         struct sk_buff *skb;
2491         u8 buf[10];
2492         long ret;
2493
2494         buf[0] = 0x01;
2495         buf[1] = 0x01;
2496         buf[2] = 0x00;
2497         buf[3] = sizeof(bdaddr_t);
2498         memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2499
2500         skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2501         if (IS_ERR(skb)) {
2502                 ret = PTR_ERR(skb);
2503                 BT_ERR("%s: Change address command failed (%ld)",
2504                        hdev->name, ret);
2505                 return ret;
2506         }
2507         kfree_skb(skb);
2508
2509         return 0;
2510 }
2511
2512 #define QCA_DFU_PACKET_LEN      4096
2513
2514 #define QCA_GET_TARGET_VERSION  0x09
2515 #define QCA_CHECK_STATUS        0x05
2516 #define QCA_DFU_DOWNLOAD        0x01
2517
2518 #define QCA_SYSCFG_UPDATED      0x40
2519 #define QCA_PATCH_UPDATED       0x80
2520 #define QCA_DFU_TIMEOUT         3000
2521
2522 struct qca_version {
2523         __le32  rom_version;
2524         __le32  patch_version;
2525         __le32  ram_version;
2526         __le32  ref_clock;
2527         __u8    reserved[4];
2528 } __packed;
2529
2530 struct qca_rampatch_version {
2531         __le16  rom_version;
2532         __le16  patch_version;
2533 } __packed;
2534
2535 struct qca_device_info {
2536         u32     rom_version;
2537         u8      rampatch_hdr;   /* length of header in rampatch */
2538         u8      nvm_hdr;        /* length of header in NVM */
2539         u8      ver_offset;     /* offset of version structure in rampatch */
2540 };
2541
2542 static const struct qca_device_info qca_devices_table[] = {
2543         { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2544         { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2545         { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
2546         { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2547         { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2548         { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2549 };
2550
2551 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
2552                                      void *data, u16 size)
2553 {
2554         int pipe, err;
2555         u8 *buf;
2556
2557         buf = kmalloc(size, GFP_KERNEL);
2558         if (!buf)
2559                 return -ENOMEM;
2560
2561         /* Found some of USB hosts have IOT issues with ours so that we should
2562          * not wait until HCI layer is ready.
2563          */
2564         pipe = usb_rcvctrlpipe(udev, 0);
2565         err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2566                               0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2567         if (err < 0) {
2568                 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
2569                 goto done;
2570         }
2571
2572         memcpy(data, buf, size);
2573
2574 done:
2575         kfree(buf);
2576
2577         return err;
2578 }
2579
2580 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2581                                        const struct firmware *firmware,
2582                                        size_t hdr_size)
2583 {
2584         struct btusb_data *btdata = hci_get_drvdata(hdev);
2585         struct usb_device *udev = btdata->udev;
2586         size_t count, size, sent = 0;
2587         int pipe, len, err;
2588         u8 *buf;
2589
2590         buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2591         if (!buf)
2592                 return -ENOMEM;
2593
2594         count = firmware->size;
2595
2596         size = min_t(size_t, count, hdr_size);
2597         memcpy(buf, firmware->data, size);
2598
2599         /* USB patches should go down to controller through USB path
2600          * because binary format fits to go down through USB channel.
2601          * USB control path is for patching headers and USB bulk is for
2602          * patch body.
2603          */
2604         pipe = usb_sndctrlpipe(udev, 0);
2605         err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2606                               0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2607         if (err < 0) {
2608                 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2609                 goto done;
2610         }
2611
2612         sent += size;
2613         count -= size;
2614
2615         /* ep2 need time to switch from function acl to function dfu,
2616          * so we add 20ms delay here.
2617          */
2618         msleep(20);
2619
2620         while (count) {
2621                 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2622
2623                 memcpy(buf, firmware->data + sent, size);
2624
2625                 pipe = usb_sndbulkpipe(udev, 0x02);
2626                 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2627                                    QCA_DFU_TIMEOUT);
2628                 if (err < 0) {
2629                         BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2630                                hdev->name, sent, firmware->size, err);
2631                         break;
2632                 }
2633
2634                 if (size != len) {
2635                         BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2636                         err = -EILSEQ;
2637                         break;
2638                 }
2639
2640                 sent  += size;
2641                 count -= size;
2642         }
2643
2644 done:
2645         kfree(buf);
2646         return err;
2647 }
2648
2649 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2650                                          struct qca_version *ver,
2651                                          const struct qca_device_info *info)
2652 {
2653         struct qca_rampatch_version *rver;
2654         const struct firmware *fw;
2655         u32 ver_rom, ver_patch;
2656         u16 rver_rom, rver_patch;
2657         char fwname[64];
2658         int err;
2659
2660         ver_rom = le32_to_cpu(ver->rom_version);
2661         ver_patch = le32_to_cpu(ver->patch_version);
2662
2663         snprintf(fwname, sizeof(fwname), "/*(DEBLOBBED)*/", ver_rom);
2664
2665         err = reject_firmware(&fw, fwname, &hdev->dev);
2666         if (err) {
2667                 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2668                        hdev->name, fwname, err);
2669                 return err;
2670         }
2671
2672         BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
2673
2674         rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2675         rver_rom = le16_to_cpu(rver->rom_version);
2676         rver_patch = le16_to_cpu(rver->patch_version);
2677
2678         BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2679                 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2680                 ver_patch);
2681
2682         if (rver_rom != ver_rom || rver_patch <= ver_patch) {
2683                 BT_ERR("%s: rampatch file version did not match with firmware",
2684                        hdev->name);
2685                 err = -EINVAL;
2686                 goto done;
2687         }
2688
2689         err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2690
2691 done:
2692         release_firmware(fw);
2693
2694         return err;
2695 }
2696
2697 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2698                                     struct qca_version *ver,
2699                                     const struct qca_device_info *info)
2700 {
2701         const struct firmware *fw;
2702         char fwname[64];
2703         int err;
2704
2705         snprintf(fwname, sizeof(fwname), "/*(DEBLOBBED)*/",
2706                  le32_to_cpu(ver->rom_version));
2707
2708         err = reject_firmware(&fw, fwname, &hdev->dev);
2709         if (err) {
2710                 BT_ERR("%s: failed to request NVM file: %s (%d)",
2711                        hdev->name, fwname, err);
2712                 return err;
2713         }
2714
2715         BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2716
2717         err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2718
2719         release_firmware(fw);
2720
2721         return err;
2722 }
2723
2724 /* identify the ROM version and check whether patches are needed */
2725 static bool btusb_qca_need_patch(struct usb_device *udev)
2726 {
2727         struct qca_version ver;
2728
2729         if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
2730                                       sizeof(ver)) < 0)
2731                 return false;
2732         /* only low ROM versions need patches */
2733         return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
2734 }
2735
2736 static int btusb_setup_qca(struct hci_dev *hdev)
2737 {
2738         struct btusb_data *btdata = hci_get_drvdata(hdev);
2739         struct usb_device *udev = btdata->udev;
2740         const struct qca_device_info *info = NULL;
2741         struct qca_version ver;
2742         u32 ver_rom;
2743         u8 status;
2744         int i, err;
2745
2746         err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
2747                                         sizeof(ver));
2748         if (err < 0)
2749                 return err;
2750
2751         ver_rom = le32_to_cpu(ver.rom_version);
2752         /* Don't care about high ROM versions */
2753         if (ver_rom & ~0xffffU)
2754                 return 0;
2755
2756         for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
2757                 if (ver_rom == qca_devices_table[i].rom_version)
2758                         info = &qca_devices_table[i];
2759         }
2760         if (!info) {
2761                 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
2762                        ver_rom);
2763                 return -ENODEV;
2764         }
2765
2766         err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
2767                                         sizeof(status));
2768         if (err < 0)
2769                 return err;
2770
2771         if (!(status & QCA_PATCH_UPDATED)) {
2772                 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2773                 if (err < 0)
2774                         return err;
2775         }
2776
2777         if (!(status & QCA_SYSCFG_UPDATED)) {
2778                 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2779                 if (err < 0)
2780                         return err;
2781         }
2782
2783         return 0;
2784 }
2785
2786 #ifdef CONFIG_BT_HCIBTUSB_BCM
2787 static inline int __set_diag_interface(struct hci_dev *hdev)
2788 {
2789         struct btusb_data *data = hci_get_drvdata(hdev);
2790         struct usb_interface *intf = data->diag;
2791         int i;
2792
2793         if (!data->diag)
2794                 return -ENODEV;
2795
2796         data->diag_tx_ep = NULL;
2797         data->diag_rx_ep = NULL;
2798
2799         for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2800                 struct usb_endpoint_descriptor *ep_desc;
2801
2802                 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2803
2804                 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2805                         data->diag_tx_ep = ep_desc;
2806                         continue;
2807                 }
2808
2809                 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2810                         data->diag_rx_ep = ep_desc;
2811                         continue;
2812                 }
2813         }
2814
2815         if (!data->diag_tx_ep || !data->diag_rx_ep) {
2816                 BT_ERR("%s invalid diagnostic descriptors", hdev->name);
2817                 return -ENODEV;
2818         }
2819
2820         return 0;
2821 }
2822
2823 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
2824 {
2825         struct btusb_data *data = hci_get_drvdata(hdev);
2826         struct sk_buff *skb;
2827         struct urb *urb;
2828         unsigned int pipe;
2829
2830         if (!data->diag_tx_ep)
2831                 return ERR_PTR(-ENODEV);
2832
2833         urb = usb_alloc_urb(0, GFP_KERNEL);
2834         if (!urb)
2835                 return ERR_PTR(-ENOMEM);
2836
2837         skb = bt_skb_alloc(2, GFP_KERNEL);
2838         if (!skb) {
2839                 usb_free_urb(urb);
2840                 return ERR_PTR(-ENOMEM);
2841         }
2842
2843         skb_put_u8(skb, 0xf0);
2844         skb_put_u8(skb, enable);
2845
2846         pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
2847
2848         usb_fill_bulk_urb(urb, data->udev, pipe,
2849                           skb->data, skb->len, btusb_tx_complete, skb);
2850
2851         skb->dev = (void *)hdev;
2852
2853         return urb;
2854 }
2855
2856 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2857 {
2858         struct btusb_data *data = hci_get_drvdata(hdev);
2859         struct urb *urb;
2860
2861         if (!data->diag)
2862                 return -ENODEV;
2863
2864         if (!test_bit(HCI_RUNNING, &hdev->flags))
2865                 return -ENETDOWN;
2866
2867         urb = alloc_diag_urb(hdev, enable);
2868         if (IS_ERR(urb))
2869                 return PTR_ERR(urb);
2870
2871         return submit_or_queue_tx_urb(hdev, urb);
2872 }
2873 #endif
2874
2875 #ifdef CONFIG_PM
2876 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
2877 {
2878         struct btusb_data *data = priv;
2879
2880         pm_wakeup_event(&data->udev->dev, 0);
2881         pm_system_wakeup();
2882
2883         /* Disable only if not already disabled (keep it balanced) */
2884         if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
2885                 disable_irq_nosync(irq);
2886                 disable_irq_wake(irq);
2887         }
2888         return IRQ_HANDLED;
2889 }
2890
2891 static const struct of_device_id btusb_match_table[] = {
2892         { .compatible = "usb1286,204e" },
2893         { }
2894 };
2895 MODULE_DEVICE_TABLE(of, btusb_match_table);
2896
2897 /* Use an oob wakeup pin? */
2898 static int btusb_config_oob_wake(struct hci_dev *hdev)
2899 {
2900         struct btusb_data *data = hci_get_drvdata(hdev);
2901         struct device *dev = &data->udev->dev;
2902         int irq, ret;
2903
2904         clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
2905
2906         if (!of_match_device(btusb_match_table, dev))
2907                 return 0;
2908
2909         /* Move on if no IRQ specified */
2910         irq = of_irq_get_byname(dev->of_node, "wakeup");
2911         if (irq <= 0) {
2912                 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
2913                 return 0;
2914         }
2915
2916         irq_set_status_flags(irq, IRQ_NOAUTOEN);
2917         ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
2918                                0, "OOB Wake-on-BT", data);
2919         if (ret) {
2920                 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
2921                 return ret;
2922         }
2923
2924         ret = device_init_wakeup(dev, true);
2925         if (ret) {
2926                 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
2927                 return ret;
2928         }
2929
2930         data->oob_wake_irq = irq;
2931         bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
2932         return 0;
2933 }
2934 #endif
2935
2936 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
2937 {
2938         if (dmi_check_system(btusb_needs_reset_resume_table))
2939                 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
2940 }
2941
2942 static int btusb_probe(struct usb_interface *intf,
2943                        const struct usb_device_id *id)
2944 {
2945         struct usb_endpoint_descriptor *ep_desc;
2946         struct btusb_data *data;
2947         struct hci_dev *hdev;
2948         unsigned ifnum_base;
2949         int i, err;
2950
2951         BT_DBG("intf %p id %p", intf, id);
2952
2953         /* interface numbers are hardcoded in the spec */
2954         if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
2955                 if (!(id->driver_info & BTUSB_IFNUM_2))
2956                         return -ENODEV;
2957                 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
2958                         return -ENODEV;
2959         }
2960
2961         ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
2962
2963         if (!id->driver_info) {
2964                 const struct usb_device_id *match;
2965
2966                 match = usb_match_id(intf, blacklist_table);
2967                 if (match)
2968                         id = match;
2969         }
2970
2971         if (id->driver_info == BTUSB_IGNORE)
2972                 return -ENODEV;
2973
2974         if (id->driver_info & BTUSB_BCM_NO_PRODID) {
2975                 struct usb_device *udev = interface_to_usbdev(intf);
2976
2977                 /* For the broken Broadcom devices that show 0000:0000
2978                  * as USB vendor and product information, check that the
2979                  * manufacturer string identifies them as Broadcom based
2980                  * devices.
2981                  */
2982                 if (!udev->manufacturer ||
2983                     strcmp(udev->manufacturer, "Broadcom Corp"))
2984                         return -ENODEV;
2985         }
2986
2987         if (id->driver_info & BTUSB_ATH3012) {
2988                 struct usb_device *udev = interface_to_usbdev(intf);
2989
2990                 /* Old firmware would otherwise let ath3k driver load
2991                  * patch and sysconfig files
2992                  */
2993                 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
2994                     !btusb_qca_need_patch(udev))
2995                         return -ENODEV;
2996         }
2997
2998         data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
2999         if (!data)
3000                 return -ENOMEM;
3001
3002         for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3003                 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3004
3005                 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3006                         data->intr_ep = ep_desc;
3007                         continue;
3008                 }
3009
3010                 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3011                         data->bulk_tx_ep = ep_desc;
3012                         continue;
3013                 }
3014
3015                 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3016                         data->bulk_rx_ep = ep_desc;
3017                         continue;
3018                 }
3019         }
3020
3021         if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
3022                 return -ENODEV;
3023
3024         if (id->driver_info & BTUSB_AMP) {
3025                 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3026                 data->cmdreq = 0x2b;
3027         } else {
3028                 data->cmdreq_type = USB_TYPE_CLASS;
3029                 data->cmdreq = 0x00;
3030         }
3031
3032         data->udev = interface_to_usbdev(intf);
3033         data->intf = intf;
3034
3035         INIT_WORK(&data->work, btusb_work);
3036         INIT_WORK(&data->waker, btusb_waker);
3037         init_usb_anchor(&data->deferred);
3038         init_usb_anchor(&data->tx_anchor);
3039         spin_lock_init(&data->txlock);
3040
3041         init_usb_anchor(&data->intr_anchor);
3042         init_usb_anchor(&data->bulk_anchor);
3043         init_usb_anchor(&data->isoc_anchor);
3044         init_usb_anchor(&data->diag_anchor);
3045         spin_lock_init(&data->rxlock);
3046
3047         if (id->driver_info & BTUSB_INTEL_NEW) {
3048                 data->recv_event = btusb_recv_event_intel;
3049                 data->recv_bulk = btusb_recv_bulk_intel;
3050                 set_bit(BTUSB_BOOTLOADER, &data->flags);
3051         } else {
3052                 data->recv_event = hci_recv_frame;
3053                 data->recv_bulk = btusb_recv_bulk;
3054         }
3055
3056         hdev = hci_alloc_dev();
3057         if (!hdev)
3058                 return -ENOMEM;
3059
3060         hdev->bus = HCI_USB;
3061         hci_set_drvdata(hdev, data);
3062
3063         if (id->driver_info & BTUSB_AMP)
3064                 hdev->dev_type = HCI_AMP;
3065         else
3066                 hdev->dev_type = HCI_PRIMARY;
3067
3068         data->hdev = hdev;
3069
3070         SET_HCIDEV_DEV(hdev, &intf->dev);
3071
3072         hdev->open   = btusb_open;
3073         hdev->close  = btusb_close;
3074         hdev->flush  = btusb_flush;
3075         hdev->send   = btusb_send_frame;
3076         hdev->notify = btusb_notify;
3077
3078 #ifdef CONFIG_PM
3079         err = btusb_config_oob_wake(hdev);
3080         if (err)
3081                 goto out_free_dev;
3082
3083         /* Marvell devices may need a specific chip configuration */
3084         if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
3085                 err = marvell_config_oob_wake(hdev);
3086                 if (err)
3087                         goto out_free_dev;
3088         }
3089 #endif
3090         if (id->driver_info & BTUSB_CW6622)
3091                 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3092
3093         if (id->driver_info & BTUSB_BCM2045)
3094                 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3095
3096         if (id->driver_info & BTUSB_BCM92035)
3097                 hdev->setup = btusb_setup_bcm92035;
3098
3099 #ifdef CONFIG_BT_HCIBTUSB_BCM
3100         if (id->driver_info & BTUSB_BCM_PATCHRAM) {
3101                 hdev->manufacturer = 15;
3102                 hdev->setup = btbcm_setup_patchram;
3103                 hdev->set_diag = btusb_bcm_set_diag;
3104                 hdev->set_bdaddr = btbcm_set_bdaddr;
3105
3106                 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3107                 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3108         }
3109
3110         if (id->driver_info & BTUSB_BCM_APPLE) {
3111                 hdev->manufacturer = 15;
3112                 hdev->setup = btbcm_setup_apple;
3113                 hdev->set_diag = btusb_bcm_set_diag;
3114
3115                 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3116                 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3117         }
3118 #endif
3119
3120         if (id->driver_info & BTUSB_INTEL) {
3121                 hdev->manufacturer = 2;
3122                 hdev->setup = btusb_setup_intel;
3123                 hdev->shutdown = btusb_shutdown_intel;
3124                 hdev->set_diag = btintel_set_diag_mfg;
3125                 hdev->set_bdaddr = btintel_set_bdaddr;
3126                 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3127                 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3128                 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
3129         }
3130
3131         if (id->driver_info & BTUSB_INTEL_NEW) {
3132                 hdev->manufacturer = 2;
3133                 hdev->send = btusb_send_frame_intel;
3134                 hdev->setup = btusb_setup_intel_new;
3135                 hdev->hw_error = btintel_hw_error;
3136                 hdev->set_diag = btintel_set_diag;
3137                 hdev->set_bdaddr = btintel_set_bdaddr;
3138                 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3139                 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
3140         }
3141
3142         if (id->driver_info & BTUSB_MARVELL)
3143                 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3144
3145         if (id->driver_info & BTUSB_SWAVE) {
3146                 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
3147                 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
3148         }
3149
3150         if (id->driver_info & BTUSB_INTEL_BOOT) {
3151                 hdev->manufacturer = 2;
3152                 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3153         }
3154
3155         if (id->driver_info & BTUSB_ATH3012) {
3156                 data->setup_on_usb = btusb_setup_qca;
3157                 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3158                 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3159                 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3160         }
3161
3162         if (id->driver_info & BTUSB_QCA_ROME) {
3163                 data->setup_on_usb = btusb_setup_qca;
3164                 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3165                 btusb_check_needs_reset_resume(intf);
3166         }
3167
3168 #ifdef CONFIG_BT_HCIBTUSB_RTL
3169         if (id->driver_info & BTUSB_REALTEK) {
3170                 hdev->setup = btrtl_setup_realtek;
3171
3172                 /* Realtek devices lose their updated firmware over suspend,
3173                  * but the USB hub doesn't notice any status change.
3174                  * Explicitly request a device reset on resume.
3175                  */
3176                 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3177         }
3178 #endif
3179
3180         if (id->driver_info & BTUSB_AMP) {
3181                 /* AMP controllers do not support SCO packets */
3182                 data->isoc = NULL;
3183         } else {
3184                 /* Interface orders are hardcoded in the specification */
3185                 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
3186         }
3187
3188         if (!reset)
3189                 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3190
3191         if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3192                 if (!disable_scofix)
3193                         set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3194         }
3195
3196         if (id->driver_info & BTUSB_BROKEN_ISOC)
3197                 data->isoc = NULL;
3198
3199         if (id->driver_info & BTUSB_DIGIANSWER) {
3200                 data->cmdreq_type = USB_TYPE_VENDOR;
3201                 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3202         }
3203
3204         if (id->driver_info & BTUSB_CSR) {
3205                 struct usb_device *udev = data->udev;
3206                 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
3207
3208                 /* Old firmware would otherwise execute USB reset */
3209                 if (bcdDevice < 0x117)
3210                         set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3211
3212                 /* Fake CSR devices with broken commands */
3213                 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
3214                         hdev->setup = btusb_setup_csr;
3215
3216                 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3217         }
3218
3219         if (id->driver_info & BTUSB_SNIFFER) {
3220                 struct usb_device *udev = data->udev;
3221
3222                 /* New sniffer firmware has crippled HCI interface */
3223                 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3224                         set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3225         }
3226
3227         if (id->driver_info & BTUSB_INTEL_BOOT) {
3228                 /* A bug in the bootloader causes that interrupt interface is
3229                  * only enabled after receiving SetInterface(0, AltSetting=0).
3230                  */
3231                 err = usb_set_interface(data->udev, 0, 0);
3232                 if (err < 0) {
3233                         BT_ERR("failed to set interface 0, alt 0 %d", err);
3234                         goto out_free_dev;
3235                 }
3236         }
3237
3238         if (data->isoc) {
3239                 err = usb_driver_claim_interface(&btusb_driver,
3240                                                  data->isoc, data);
3241                 if (err < 0)
3242                         goto out_free_dev;
3243         }
3244
3245 #ifdef CONFIG_BT_HCIBTUSB_BCM
3246         if (data->diag) {
3247                 if (!usb_driver_claim_interface(&btusb_driver,
3248                                                 data->diag, data))
3249                         __set_diag_interface(hdev);
3250                 else
3251                         data->diag = NULL;
3252         }
3253 #endif
3254
3255         err = hci_register_dev(hdev);
3256         if (err < 0)
3257                 goto out_free_dev;
3258
3259         usb_set_intfdata(intf, data);
3260
3261         return 0;
3262
3263 out_free_dev:
3264         hci_free_dev(hdev);
3265         return err;
3266 }
3267
3268 static void btusb_disconnect(struct usb_interface *intf)
3269 {
3270         struct btusb_data *data = usb_get_intfdata(intf);
3271         struct hci_dev *hdev;
3272
3273         BT_DBG("intf %p", intf);
3274
3275         if (!data)
3276                 return;
3277
3278         hdev = data->hdev;
3279         usb_set_intfdata(data->intf, NULL);
3280
3281         if (data->isoc)
3282                 usb_set_intfdata(data->isoc, NULL);
3283
3284         if (data->diag)
3285                 usb_set_intfdata(data->diag, NULL);
3286
3287         hci_unregister_dev(hdev);
3288
3289         if (intf == data->intf) {
3290                 if (data->isoc)
3291                         usb_driver_release_interface(&btusb_driver, data->isoc);
3292                 if (data->diag)
3293                         usb_driver_release_interface(&btusb_driver, data->diag);
3294         } else if (intf == data->isoc) {
3295                 if (data->diag)
3296                         usb_driver_release_interface(&btusb_driver, data->diag);
3297                 usb_driver_release_interface(&btusb_driver, data->intf);
3298         } else if (intf == data->diag) {
3299                 usb_driver_release_interface(&btusb_driver, data->intf);
3300                 if (data->isoc)
3301                         usb_driver_release_interface(&btusb_driver, data->isoc);
3302         }
3303
3304         if (data->oob_wake_irq)
3305                 device_init_wakeup(&data->udev->dev, false);
3306
3307         hci_free_dev(hdev);
3308 }
3309
3310 #ifdef CONFIG_PM
3311 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3312 {
3313         struct btusb_data *data = usb_get_intfdata(intf);
3314
3315         BT_DBG("intf %p", intf);
3316
3317         if (data->suspend_count++)
3318                 return 0;
3319
3320         spin_lock_irq(&data->txlock);
3321         if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
3322                 set_bit(BTUSB_SUSPENDING, &data->flags);
3323                 spin_unlock_irq(&data->txlock);
3324         } else {
3325                 spin_unlock_irq(&data->txlock);
3326                 data->suspend_count--;
3327                 return -EBUSY;
3328         }
3329
3330         cancel_work_sync(&data->work);
3331
3332         btusb_stop_traffic(data);
3333         usb_kill_anchored_urbs(&data->tx_anchor);
3334
3335         if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
3336                 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3337                 enable_irq_wake(data->oob_wake_irq);
3338                 enable_irq(data->oob_wake_irq);
3339         }
3340
3341         return 0;
3342 }
3343
3344 static void play_deferred(struct btusb_data *data)
3345 {
3346         struct urb *urb;
3347         int err;
3348
3349         while ((urb = usb_get_from_anchor(&data->deferred))) {
3350                 usb_anchor_urb(urb, &data->tx_anchor);
3351
3352                 err = usb_submit_urb(urb, GFP_ATOMIC);
3353                 if (err < 0) {
3354                         if (err != -EPERM && err != -ENODEV)
3355                                 BT_ERR("%s urb %p submission failed (%d)",
3356                                        data->hdev->name, urb, -err);
3357                         kfree(urb->setup_packet);
3358                         usb_unanchor_urb(urb);
3359                         usb_free_urb(urb);
3360                         break;
3361                 }
3362
3363                 data->tx_in_flight++;
3364                 usb_free_urb(urb);
3365         }
3366
3367         /* Cleanup the rest deferred urbs. */
3368         while ((urb = usb_get_from_anchor(&data->deferred))) {
3369                 kfree(urb->setup_packet);
3370                 usb_free_urb(urb);
3371         }
3372 }
3373
3374 static int btusb_resume(struct usb_interface *intf)
3375 {
3376         struct btusb_data *data = usb_get_intfdata(intf);
3377         struct hci_dev *hdev = data->hdev;
3378         int err = 0;
3379
3380         BT_DBG("intf %p", intf);
3381
3382         if (--data->suspend_count)
3383                 return 0;
3384
3385         /* Disable only if not already disabled (keep it balanced) */
3386         if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3387                 disable_irq(data->oob_wake_irq);
3388                 disable_irq_wake(data->oob_wake_irq);
3389         }
3390
3391         if (!test_bit(HCI_RUNNING, &hdev->flags))
3392                 goto done;
3393
3394         if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3395                 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3396                 if (err < 0) {
3397                         clear_bit(BTUSB_INTR_RUNNING, &data->flags);
3398                         goto failed;
3399                 }
3400         }
3401
3402         if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
3403                 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3404                 if (err < 0) {
3405                         clear_bit(BTUSB_BULK_RUNNING, &data->flags);
3406                         goto failed;
3407                 }
3408
3409                 btusb_submit_bulk_urb(hdev, GFP_NOIO);
3410         }
3411
3412         if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3413                 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3414                         clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3415                 else
3416                         btusb_submit_isoc_urb(hdev, GFP_NOIO);
3417         }
3418
3419         spin_lock_irq(&data->txlock);
3420         play_deferred(data);
3421         clear_bit(BTUSB_SUSPENDING, &data->flags);
3422         spin_unlock_irq(&data->txlock);
3423         schedule_work(&data->work);
3424
3425         return 0;
3426
3427 failed:
3428         usb_scuttle_anchored_urbs(&data->deferred);
3429 done:
3430         spin_lock_irq(&data->txlock);
3431         clear_bit(BTUSB_SUSPENDING, &data->flags);
3432         spin_unlock_irq(&data->txlock);
3433
3434         return err;
3435 }
3436 #endif
3437
3438 static struct usb_driver btusb_driver = {
3439         .name           = "btusb",
3440         .probe          = btusb_probe,
3441         .disconnect     = btusb_disconnect,
3442 #ifdef CONFIG_PM
3443         .suspend        = btusb_suspend,
3444         .resume         = btusb_resume,
3445 #endif
3446         .id_table       = btusb_table,
3447         .supports_autosuspend = 1,
3448         .disable_hub_initiated_lpm = 1,
3449 };
3450
3451 module_usb_driver(btusb_driver);
3452
3453 module_param(disable_scofix, bool, 0644);
3454 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3455
3456 module_param(force_scofix, bool, 0644);
3457 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3458
3459 module_param(reset, bool, 0644);
3460 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3461
3462 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3463 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3464 MODULE_VERSION(VERSION);
3465 MODULE_LICENSE("GPL");