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