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