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