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