Merge remote-tracking branch 'scott/next' into next
[pandora-kernel.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.6"
32
33 static bool ignore_dga;
34 static bool ignore_csr;
35 static bool ignore_sniffer;
36 static bool disable_scofix;
37 static bool force_scofix;
38
39 static bool reset = 1;
40
41 static struct usb_driver btusb_driver;
42
43 #define BTUSB_IGNORE            0x01
44 #define BTUSB_DIGIANSWER        0x02
45 #define BTUSB_CSR               0x04
46 #define BTUSB_SNIFFER           0x08
47 #define BTUSB_BCM92035          0x10
48 #define BTUSB_BROKEN_ISOC       0x20
49 #define BTUSB_WRONG_SCO_MTU     0x40
50 #define BTUSB_ATH3012           0x80
51 #define BTUSB_INTEL             0x100
52
53 static const struct usb_device_id btusb_table[] = {
54         /* Generic Bluetooth USB device */
55         { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
56
57         /* Apple-specific (Broadcom) devices */
58         { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
59
60         /* MediaTek MT76x0E */
61         { USB_DEVICE(0x0e8d, 0x763f) },
62
63         /* Broadcom SoftSailing reporting vendor specific */
64         { USB_DEVICE(0x0a5c, 0x21e1) },
65
66         /* Apple MacBookPro 7,1 */
67         { USB_DEVICE(0x05ac, 0x8213) },
68
69         /* Apple iMac11,1 */
70         { USB_DEVICE(0x05ac, 0x8215) },
71
72         /* Apple MacBookPro6,2 */
73         { USB_DEVICE(0x05ac, 0x8218) },
74
75         /* Apple MacBookAir3,1, MacBookAir3,2 */
76         { USB_DEVICE(0x05ac, 0x821b) },
77
78         /* Apple MacBookAir4,1 */
79         { USB_DEVICE(0x05ac, 0x821f) },
80
81         /* Apple MacBookPro8,2 */
82         { USB_DEVICE(0x05ac, 0x821a) },
83
84         /* Apple MacMini5,1 */
85         { USB_DEVICE(0x05ac, 0x8281) },
86
87         /* AVM BlueFRITZ! USB v2.0 */
88         { USB_DEVICE(0x057c, 0x3800) },
89
90         /* Bluetooth Ultraport Module from IBM */
91         { USB_DEVICE(0x04bf, 0x030a) },
92
93         /* ALPS Modules with non-standard id */
94         { USB_DEVICE(0x044e, 0x3001) },
95         { USB_DEVICE(0x044e, 0x3002) },
96
97         /* Ericsson with non-standard id */
98         { USB_DEVICE(0x0bdb, 0x1002) },
99
100         /* Canyon CN-BTU1 with HID interfaces */
101         { USB_DEVICE(0x0c10, 0x0000) },
102
103         /* Broadcom BCM20702A0 */
104         { USB_DEVICE(0x0b05, 0x17b5) },
105         { USB_DEVICE(0x0b05, 0x17cb) },
106         { USB_DEVICE(0x04ca, 0x2003) },
107         { USB_DEVICE(0x0489, 0xe042) },
108         { USB_DEVICE(0x413c, 0x8197) },
109
110         /* Foxconn - Hon Hai */
111         { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01) },
112
113         /*Broadcom devices with vendor specific id */
114         { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01) },
115
116         /* Belkin F8065bf - Broadcom based */
117         { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01) },
118
119         { }     /* Terminating entry */
120 };
121
122 MODULE_DEVICE_TABLE(usb, btusb_table);
123
124 static const struct usb_device_id blacklist_table[] = {
125         /* CSR BlueCore devices */
126         { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
127
128         /* Broadcom BCM2033 without firmware */
129         { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
130
131         /* Atheros 3011 with sflash firmware */
132         { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
133         { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
134         { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
135         { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
136         { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
137         { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
138
139         /* Atheros AR9285 Malbec with sflash firmware */
140         { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
141
142         /* Atheros 3012 with sflash firmware */
143         { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
144         { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
145         { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
146         { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
147         { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
148         { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
149         { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
150         { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
151         { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
152         { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
153         { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
154         { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
155         { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
156         { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
157         { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
158         { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
159         { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
160         { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
161         { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
162         { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
163         { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
164         { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
165         { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
166         { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
167         { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
168         { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
169
170         /* Atheros AR5BBU12 with sflash firmware */
171         { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
172
173         /* Atheros AR5BBU12 with sflash firmware */
174         { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
175         { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
176
177         /* Broadcom BCM2035 */
178         { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
179         { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
180         { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
181
182         /* Broadcom BCM2045 */
183         { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
184         { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
185
186         /* IBM/Lenovo ThinkPad with Broadcom chip */
187         { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
188         { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
189
190         /* HP laptop with Broadcom chip */
191         { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
192
193         /* Dell laptop with Broadcom chip */
194         { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
195
196         /* Dell Wireless 370 and 410 devices */
197         { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
198         { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
199
200         /* Belkin F8T012 and F8T013 devices */
201         { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
202         { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
203
204         /* Asus WL-BTD202 device */
205         { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
206
207         /* Kensington Bluetooth USB adapter */
208         { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
209
210         /* RTX Telecom based adapters with buggy SCO support */
211         { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
212         { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
213
214         /* CONWISE Technology based adapters with buggy SCO support */
215         { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
216
217         /* Digianswer devices */
218         { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
219         { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
220
221         /* CSR BlueCore Bluetooth Sniffer */
222         { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
223
224         /* Frontline ComProbe Bluetooth Sniffer */
225         { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
226
227         /* Intel Bluetooth device */
228         { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
229         { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
230
231         { }     /* Terminating entry */
232 };
233
234 #define BTUSB_MAX_ISOC_FRAMES   10
235
236 #define BTUSB_INTR_RUNNING      0
237 #define BTUSB_BULK_RUNNING      1
238 #define BTUSB_ISOC_RUNNING      2
239 #define BTUSB_SUSPENDING        3
240 #define BTUSB_DID_ISO_RESUME    4
241
242 struct btusb_data {
243         struct hci_dev       *hdev;
244         struct usb_device    *udev;
245         struct usb_interface *intf;
246         struct usb_interface *isoc;
247
248         spinlock_t lock;
249
250         unsigned long flags;
251
252         struct work_struct work;
253         struct work_struct waker;
254
255         struct usb_anchor tx_anchor;
256         struct usb_anchor intr_anchor;
257         struct usb_anchor bulk_anchor;
258         struct usb_anchor isoc_anchor;
259         struct usb_anchor deferred;
260         int tx_in_flight;
261         spinlock_t txlock;
262
263         struct usb_endpoint_descriptor *intr_ep;
264         struct usb_endpoint_descriptor *bulk_tx_ep;
265         struct usb_endpoint_descriptor *bulk_rx_ep;
266         struct usb_endpoint_descriptor *isoc_tx_ep;
267         struct usb_endpoint_descriptor *isoc_rx_ep;
268
269         __u8 cmdreq_type;
270
271         unsigned int sco_num;
272         int isoc_altsetting;
273         int suspend_count;
274 };
275
276 static int inc_tx(struct btusb_data *data)
277 {
278         unsigned long flags;
279         int rv;
280
281         spin_lock_irqsave(&data->txlock, flags);
282         rv = test_bit(BTUSB_SUSPENDING, &data->flags);
283         if (!rv)
284                 data->tx_in_flight++;
285         spin_unlock_irqrestore(&data->txlock, flags);
286
287         return rv;
288 }
289
290 static void btusb_intr_complete(struct urb *urb)
291 {
292         struct hci_dev *hdev = urb->context;
293         struct btusb_data *data = hci_get_drvdata(hdev);
294         int err;
295
296         BT_DBG("%s urb %p status %d count %d", hdev->name,
297                                         urb, urb->status, urb->actual_length);
298
299         if (!test_bit(HCI_RUNNING, &hdev->flags))
300                 return;
301
302         if (urb->status == 0) {
303                 hdev->stat.byte_rx += urb->actual_length;
304
305                 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
306                                                 urb->transfer_buffer,
307                                                 urb->actual_length) < 0) {
308                         BT_ERR("%s corrupted event packet", hdev->name);
309                         hdev->stat.err_rx++;
310                 }
311         }
312
313         if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
314                 return;
315
316         usb_mark_last_busy(data->udev);
317         usb_anchor_urb(urb, &data->intr_anchor);
318
319         err = usb_submit_urb(urb, GFP_ATOMIC);
320         if (err < 0) {
321                 /* -EPERM: urb is being killed;
322                  * -ENODEV: device got disconnected */
323                 if (err != -EPERM && err != -ENODEV)
324                         BT_ERR("%s urb %p failed to resubmit (%d)",
325                                                 hdev->name, urb, -err);
326                 usb_unanchor_urb(urb);
327         }
328 }
329
330 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
331 {
332         struct btusb_data *data = hci_get_drvdata(hdev);
333         struct urb *urb;
334         unsigned char *buf;
335         unsigned int pipe;
336         int err, size;
337
338         BT_DBG("%s", hdev->name);
339
340         if (!data->intr_ep)
341                 return -ENODEV;
342
343         urb = usb_alloc_urb(0, mem_flags);
344         if (!urb)
345                 return -ENOMEM;
346
347         size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
348
349         buf = kmalloc(size, mem_flags);
350         if (!buf) {
351                 usb_free_urb(urb);
352                 return -ENOMEM;
353         }
354
355         pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
356
357         usb_fill_int_urb(urb, data->udev, pipe, buf, size,
358                                                 btusb_intr_complete, hdev,
359                                                 data->intr_ep->bInterval);
360
361         urb->transfer_flags |= URB_FREE_BUFFER;
362
363         usb_anchor_urb(urb, &data->intr_anchor);
364
365         err = usb_submit_urb(urb, mem_flags);
366         if (err < 0) {
367                 if (err != -EPERM && err != -ENODEV)
368                         BT_ERR("%s urb %p submission failed (%d)",
369                                                 hdev->name, urb, -err);
370                 usb_unanchor_urb(urb);
371         }
372
373         usb_free_urb(urb);
374
375         return err;
376 }
377
378 static void btusb_bulk_complete(struct urb *urb)
379 {
380         struct hci_dev *hdev = urb->context;
381         struct btusb_data *data = hci_get_drvdata(hdev);
382         int err;
383
384         BT_DBG("%s urb %p status %d count %d", hdev->name,
385                                         urb, urb->status, urb->actual_length);
386
387         if (!test_bit(HCI_RUNNING, &hdev->flags))
388                 return;
389
390         if (urb->status == 0) {
391                 hdev->stat.byte_rx += urb->actual_length;
392
393                 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
394                                                 urb->transfer_buffer,
395                                                 urb->actual_length) < 0) {
396                         BT_ERR("%s corrupted ACL packet", hdev->name);
397                         hdev->stat.err_rx++;
398                 }
399         }
400
401         if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
402                 return;
403
404         usb_anchor_urb(urb, &data->bulk_anchor);
405         usb_mark_last_busy(data->udev);
406
407         err = usb_submit_urb(urb, GFP_ATOMIC);
408         if (err < 0) {
409                 /* -EPERM: urb is being killed;
410                  * -ENODEV: device got disconnected */
411                 if (err != -EPERM && err != -ENODEV)
412                         BT_ERR("%s urb %p failed to resubmit (%d)",
413                                                 hdev->name, urb, -err);
414                 usb_unanchor_urb(urb);
415         }
416 }
417
418 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
419 {
420         struct btusb_data *data = hci_get_drvdata(hdev);
421         struct urb *urb;
422         unsigned char *buf;
423         unsigned int pipe;
424         int err, size = HCI_MAX_FRAME_SIZE;
425
426         BT_DBG("%s", hdev->name);
427
428         if (!data->bulk_rx_ep)
429                 return -ENODEV;
430
431         urb = usb_alloc_urb(0, mem_flags);
432         if (!urb)
433                 return -ENOMEM;
434
435         buf = kmalloc(size, mem_flags);
436         if (!buf) {
437                 usb_free_urb(urb);
438                 return -ENOMEM;
439         }
440
441         pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
442
443         usb_fill_bulk_urb(urb, data->udev, pipe,
444                                         buf, size, btusb_bulk_complete, hdev);
445
446         urb->transfer_flags |= URB_FREE_BUFFER;
447
448         usb_mark_last_busy(data->udev);
449         usb_anchor_urb(urb, &data->bulk_anchor);
450
451         err = usb_submit_urb(urb, mem_flags);
452         if (err < 0) {
453                 if (err != -EPERM && err != -ENODEV)
454                         BT_ERR("%s urb %p submission failed (%d)",
455                                                 hdev->name, urb, -err);
456                 usb_unanchor_urb(urb);
457         }
458
459         usb_free_urb(urb);
460
461         return err;
462 }
463
464 static void btusb_isoc_complete(struct urb *urb)
465 {
466         struct hci_dev *hdev = urb->context;
467         struct btusb_data *data = hci_get_drvdata(hdev);
468         int i, err;
469
470         BT_DBG("%s urb %p status %d count %d", hdev->name,
471                                         urb, urb->status, urb->actual_length);
472
473         if (!test_bit(HCI_RUNNING, &hdev->flags))
474                 return;
475
476         if (urb->status == 0) {
477                 for (i = 0; i < urb->number_of_packets; i++) {
478                         unsigned int offset = urb->iso_frame_desc[i].offset;
479                         unsigned int length = urb->iso_frame_desc[i].actual_length;
480
481                         if (urb->iso_frame_desc[i].status)
482                                 continue;
483
484                         hdev->stat.byte_rx += length;
485
486                         if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
487                                                 urb->transfer_buffer + offset,
488                                                                 length) < 0) {
489                                 BT_ERR("%s corrupted SCO packet", hdev->name);
490                                 hdev->stat.err_rx++;
491                         }
492                 }
493         }
494
495         if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
496                 return;
497
498         usb_anchor_urb(urb, &data->isoc_anchor);
499
500         err = usb_submit_urb(urb, GFP_ATOMIC);
501         if (err < 0) {
502                 /* -EPERM: urb is being killed;
503                  * -ENODEV: device got disconnected */
504                 if (err != -EPERM && err != -ENODEV)
505                         BT_ERR("%s urb %p failed to resubmit (%d)",
506                                                 hdev->name, urb, -err);
507                 usb_unanchor_urb(urb);
508         }
509 }
510
511 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
512 {
513         int i, offset = 0;
514
515         BT_DBG("len %d mtu %d", len, mtu);
516
517         for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
518                                         i++, offset += mtu, len -= mtu) {
519                 urb->iso_frame_desc[i].offset = offset;
520                 urb->iso_frame_desc[i].length = mtu;
521         }
522
523         if (len && i < BTUSB_MAX_ISOC_FRAMES) {
524                 urb->iso_frame_desc[i].offset = offset;
525                 urb->iso_frame_desc[i].length = len;
526                 i++;
527         }
528
529         urb->number_of_packets = i;
530 }
531
532 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
533 {
534         struct btusb_data *data = hci_get_drvdata(hdev);
535         struct urb *urb;
536         unsigned char *buf;
537         unsigned int pipe;
538         int err, size;
539
540         BT_DBG("%s", hdev->name);
541
542         if (!data->isoc_rx_ep)
543                 return -ENODEV;
544
545         urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
546         if (!urb)
547                 return -ENOMEM;
548
549         size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
550                                                 BTUSB_MAX_ISOC_FRAMES;
551
552         buf = kmalloc(size, mem_flags);
553         if (!buf) {
554                 usb_free_urb(urb);
555                 return -ENOMEM;
556         }
557
558         pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
559
560         usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
561                                 hdev, data->isoc_rx_ep->bInterval);
562
563         urb->transfer_flags  = URB_FREE_BUFFER | URB_ISO_ASAP;
564
565         __fill_isoc_descriptor(urb, size,
566                         le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
567
568         usb_anchor_urb(urb, &data->isoc_anchor);
569
570         err = usb_submit_urb(urb, mem_flags);
571         if (err < 0) {
572                 if (err != -EPERM && err != -ENODEV)
573                         BT_ERR("%s urb %p submission failed (%d)",
574                                                 hdev->name, urb, -err);
575                 usb_unanchor_urb(urb);
576         }
577
578         usb_free_urb(urb);
579
580         return err;
581 }
582
583 static void btusb_tx_complete(struct urb *urb)
584 {
585         struct sk_buff *skb = urb->context;
586         struct hci_dev *hdev = (struct hci_dev *) skb->dev;
587         struct btusb_data *data = hci_get_drvdata(hdev);
588
589         BT_DBG("%s urb %p status %d count %d", hdev->name,
590                                         urb, urb->status, urb->actual_length);
591
592         if (!test_bit(HCI_RUNNING, &hdev->flags))
593                 goto done;
594
595         if (!urb->status)
596                 hdev->stat.byte_tx += urb->transfer_buffer_length;
597         else
598                 hdev->stat.err_tx++;
599
600 done:
601         spin_lock(&data->txlock);
602         data->tx_in_flight--;
603         spin_unlock(&data->txlock);
604
605         kfree(urb->setup_packet);
606
607         kfree_skb(skb);
608 }
609
610 static void btusb_isoc_tx_complete(struct urb *urb)
611 {
612         struct sk_buff *skb = urb->context;
613         struct hci_dev *hdev = (struct hci_dev *) skb->dev;
614
615         BT_DBG("%s urb %p status %d count %d", hdev->name,
616                                         urb, urb->status, urb->actual_length);
617
618         if (!test_bit(HCI_RUNNING, &hdev->flags))
619                 goto done;
620
621         if (!urb->status)
622                 hdev->stat.byte_tx += urb->transfer_buffer_length;
623         else
624                 hdev->stat.err_tx++;
625
626 done:
627         kfree(urb->setup_packet);
628
629         kfree_skb(skb);
630 }
631
632 static int btusb_open(struct hci_dev *hdev)
633 {
634         struct btusb_data *data = hci_get_drvdata(hdev);
635         int err;
636
637         BT_DBG("%s", hdev->name);
638
639         err = usb_autopm_get_interface(data->intf);
640         if (err < 0)
641                 return err;
642
643         data->intf->needs_remote_wakeup = 1;
644
645         if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
646                 goto done;
647
648         if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
649                 goto done;
650
651         err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
652         if (err < 0)
653                 goto failed;
654
655         err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
656         if (err < 0) {
657                 usb_kill_anchored_urbs(&data->intr_anchor);
658                 goto failed;
659         }
660
661         set_bit(BTUSB_BULK_RUNNING, &data->flags);
662         btusb_submit_bulk_urb(hdev, GFP_KERNEL);
663
664 done:
665         usb_autopm_put_interface(data->intf);
666         return 0;
667
668 failed:
669         clear_bit(BTUSB_INTR_RUNNING, &data->flags);
670         clear_bit(HCI_RUNNING, &hdev->flags);
671         usb_autopm_put_interface(data->intf);
672         return err;
673 }
674
675 static void btusb_stop_traffic(struct btusb_data *data)
676 {
677         usb_kill_anchored_urbs(&data->intr_anchor);
678         usb_kill_anchored_urbs(&data->bulk_anchor);
679         usb_kill_anchored_urbs(&data->isoc_anchor);
680 }
681
682 static int btusb_close(struct hci_dev *hdev)
683 {
684         struct btusb_data *data = hci_get_drvdata(hdev);
685         int err;
686
687         BT_DBG("%s", hdev->name);
688
689         if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
690                 return 0;
691
692         cancel_work_sync(&data->work);
693         cancel_work_sync(&data->waker);
694
695         clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
696         clear_bit(BTUSB_BULK_RUNNING, &data->flags);
697         clear_bit(BTUSB_INTR_RUNNING, &data->flags);
698
699         btusb_stop_traffic(data);
700         err = usb_autopm_get_interface(data->intf);
701         if (err < 0)
702                 goto failed;
703
704         data->intf->needs_remote_wakeup = 0;
705         usb_autopm_put_interface(data->intf);
706
707 failed:
708         usb_scuttle_anchored_urbs(&data->deferred);
709         return 0;
710 }
711
712 static int btusb_flush(struct hci_dev *hdev)
713 {
714         struct btusb_data *data = hci_get_drvdata(hdev);
715
716         BT_DBG("%s", hdev->name);
717
718         usb_kill_anchored_urbs(&data->tx_anchor);
719
720         return 0;
721 }
722
723 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
724 {
725         struct btusb_data *data = hci_get_drvdata(hdev);
726         struct usb_ctrlrequest *dr;
727         struct urb *urb;
728         unsigned int pipe;
729         int err;
730
731         BT_DBG("%s", hdev->name);
732
733         if (!test_bit(HCI_RUNNING, &hdev->flags))
734                 return -EBUSY;
735
736         skb->dev = (void *) hdev;
737
738         switch (bt_cb(skb)->pkt_type) {
739         case HCI_COMMAND_PKT:
740                 urb = usb_alloc_urb(0, GFP_ATOMIC);
741                 if (!urb)
742                         return -ENOMEM;
743
744                 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
745                 if (!dr) {
746                         usb_free_urb(urb);
747                         return -ENOMEM;
748                 }
749
750                 dr->bRequestType = data->cmdreq_type;
751                 dr->bRequest     = 0;
752                 dr->wIndex       = 0;
753                 dr->wValue       = 0;
754                 dr->wLength      = __cpu_to_le16(skb->len);
755
756                 pipe = usb_sndctrlpipe(data->udev, 0x00);
757
758                 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
759                                 skb->data, skb->len, btusb_tx_complete, skb);
760
761                 hdev->stat.cmd_tx++;
762                 break;
763
764         case HCI_ACLDATA_PKT:
765                 if (!data->bulk_tx_ep)
766                         return -ENODEV;
767
768                 urb = usb_alloc_urb(0, GFP_ATOMIC);
769                 if (!urb)
770                         return -ENOMEM;
771
772                 pipe = usb_sndbulkpipe(data->udev,
773                                         data->bulk_tx_ep->bEndpointAddress);
774
775                 usb_fill_bulk_urb(urb, data->udev, pipe,
776                                 skb->data, skb->len, btusb_tx_complete, skb);
777
778                 hdev->stat.acl_tx++;
779                 break;
780
781         case HCI_SCODATA_PKT:
782                 if (!data->isoc_tx_ep || hci_conn_num(hdev, SCO_LINK) < 1)
783                         return -ENODEV;
784
785                 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
786                 if (!urb)
787                         return -ENOMEM;
788
789                 pipe = usb_sndisocpipe(data->udev,
790                                         data->isoc_tx_ep->bEndpointAddress);
791
792                 usb_fill_int_urb(urb, data->udev, pipe,
793                                 skb->data, skb->len, btusb_isoc_tx_complete,
794                                 skb, data->isoc_tx_ep->bInterval);
795
796                 urb->transfer_flags  = URB_ISO_ASAP;
797
798                 __fill_isoc_descriptor(urb, skb->len,
799                                 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
800
801                 hdev->stat.sco_tx++;
802                 goto skip_waking;
803
804         default:
805                 return -EILSEQ;
806         }
807
808         err = inc_tx(data);
809         if (err) {
810                 usb_anchor_urb(urb, &data->deferred);
811                 schedule_work(&data->waker);
812                 err = 0;
813                 goto done;
814         }
815
816 skip_waking:
817         usb_anchor_urb(urb, &data->tx_anchor);
818
819         err = usb_submit_urb(urb, GFP_ATOMIC);
820         if (err < 0) {
821                 if (err != -EPERM && err != -ENODEV)
822                         BT_ERR("%s urb %p submission failed (%d)",
823                                                 hdev->name, urb, -err);
824                 kfree(urb->setup_packet);
825                 usb_unanchor_urb(urb);
826         } else {
827                 usb_mark_last_busy(data->udev);
828         }
829
830 done:
831         usb_free_urb(urb);
832         return err;
833 }
834
835 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
836 {
837         struct btusb_data *data = hci_get_drvdata(hdev);
838
839         BT_DBG("%s evt %d", hdev->name, evt);
840
841         if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
842                 data->sco_num = hci_conn_num(hdev, SCO_LINK);
843                 schedule_work(&data->work);
844         }
845 }
846
847 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
848 {
849         struct btusb_data *data = hci_get_drvdata(hdev);
850         struct usb_interface *intf = data->isoc;
851         struct usb_endpoint_descriptor *ep_desc;
852         int i, err;
853
854         if (!data->isoc)
855                 return -ENODEV;
856
857         err = usb_set_interface(data->udev, 1, altsetting);
858         if (err < 0) {
859                 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
860                 return err;
861         }
862
863         data->isoc_altsetting = altsetting;
864
865         data->isoc_tx_ep = NULL;
866         data->isoc_rx_ep = NULL;
867
868         for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
869                 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
870
871                 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
872                         data->isoc_tx_ep = ep_desc;
873                         continue;
874                 }
875
876                 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
877                         data->isoc_rx_ep = ep_desc;
878                         continue;
879                 }
880         }
881
882         if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
883                 BT_ERR("%s invalid SCO descriptors", hdev->name);
884                 return -ENODEV;
885         }
886
887         return 0;
888 }
889
890 static void btusb_work(struct work_struct *work)
891 {
892         struct btusb_data *data = container_of(work, struct btusb_data, work);
893         struct hci_dev *hdev = data->hdev;
894         int new_alts;
895         int err;
896
897         if (data->sco_num > 0) {
898                 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
899                         err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
900                         if (err < 0) {
901                                 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
902                                 usb_kill_anchored_urbs(&data->isoc_anchor);
903                                 return;
904                         }
905
906                         set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
907                 }
908
909                 if (hdev->voice_setting & 0x0020) {
910                         static const int alts[3] = { 2, 4, 5 };
911                         new_alts = alts[data->sco_num - 1];
912                 } else {
913                         new_alts = data->sco_num;
914                 }
915
916                 if (data->isoc_altsetting != new_alts) {
917                         clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
918                         usb_kill_anchored_urbs(&data->isoc_anchor);
919
920                         if (__set_isoc_interface(hdev, new_alts) < 0)
921                                 return;
922                 }
923
924                 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
925                         if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
926                                 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
927                         else
928                                 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
929                 }
930         } else {
931                 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
932                 usb_kill_anchored_urbs(&data->isoc_anchor);
933
934                 __set_isoc_interface(hdev, 0);
935                 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
936                         usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
937         }
938 }
939
940 static void btusb_waker(struct work_struct *work)
941 {
942         struct btusb_data *data = container_of(work, struct btusb_data, waker);
943         int err;
944
945         err = usb_autopm_get_interface(data->intf);
946         if (err < 0)
947                 return;
948
949         usb_autopm_put_interface(data->intf);
950 }
951
952 static int btusb_setup_bcm92035(struct hci_dev *hdev)
953 {
954         struct sk_buff *skb;
955         u8 val = 0x00;
956
957         BT_DBG("%s", hdev->name);
958
959         skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
960         if (IS_ERR(skb))
961                 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
962         else
963                 kfree_skb(skb);
964
965         return 0;
966 }
967
968 static int btusb_setup_csr(struct hci_dev *hdev)
969 {
970         struct hci_rp_read_local_version *rp;
971         struct sk_buff *skb;
972         int ret;
973
974         BT_DBG("%s", hdev->name);
975
976         skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
977                              HCI_INIT_TIMEOUT);
978         if (IS_ERR(skb)) {
979                 BT_ERR("Reading local version failed (%ld)", -PTR_ERR(skb));
980                 return -PTR_ERR(skb);
981         }
982
983         rp = (struct hci_rp_read_local_version *) skb->data;
984
985         if (!rp->status) {
986                 if (le16_to_cpu(rp->manufacturer) != 10) {
987                         /* Clear the reset quirk since this is not an actual
988                          * early Bluetooth 1.1 device from CSR.
989                          */
990                         clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
991
992                         /* These fake CSR controllers have all a broken
993                          * stored link key handling and so just disable it.
994                          */
995                         set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
996                                 &hdev->quirks);
997                 }
998         }
999
1000         ret = -bt_to_errno(rp->status);
1001
1002         kfree_skb(skb);
1003
1004         return ret;
1005 }
1006
1007 struct intel_version {
1008         u8 status;
1009         u8 hw_platform;
1010         u8 hw_variant;
1011         u8 hw_revision;
1012         u8 fw_variant;
1013         u8 fw_revision;
1014         u8 fw_build_num;
1015         u8 fw_build_ww;
1016         u8 fw_build_yy;
1017         u8 fw_patch_num;
1018 } __packed;
1019
1020 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1021                                                 struct intel_version *ver)
1022 {
1023         const struct firmware *fw;
1024         char fwname[64];
1025         int ret;
1026
1027         snprintf(fwname, sizeof(fwname),
1028                  "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1029                  ver->hw_platform, ver->hw_variant, ver->hw_revision,
1030                  ver->fw_variant,  ver->fw_revision, ver->fw_build_num,
1031                  ver->fw_build_ww, ver->fw_build_yy);
1032
1033         ret = request_firmware(&fw, fwname, &hdev->dev);
1034         if (ret < 0) {
1035                 if (ret == -EINVAL) {
1036                         BT_ERR("%s Intel firmware file request failed (%d)",
1037                                hdev->name, ret);
1038                         return NULL;
1039                 }
1040
1041                 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1042                        hdev->name, fwname, ret);
1043
1044                 /* If the correct firmware patch file is not found, use the
1045                  * default firmware patch file instead
1046                  */
1047                 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1048                          ver->hw_platform, ver->hw_variant);
1049                 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1050                         BT_ERR("%s failed to open default Intel fw file: %s",
1051                                hdev->name, fwname);
1052                         return NULL;
1053                 }
1054         }
1055
1056         BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1057
1058         return fw;
1059 }
1060
1061 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1062                                       const struct firmware *fw,
1063                                       const u8 **fw_ptr, int *disable_patch)
1064 {
1065         struct sk_buff *skb;
1066         struct hci_command_hdr *cmd;
1067         const u8 *cmd_param;
1068         struct hci_event_hdr *evt = NULL;
1069         const u8 *evt_param = NULL;
1070         int remain = fw->size - (*fw_ptr - fw->data);
1071
1072         /* The first byte indicates the types of the patch command or event.
1073          * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1074          * in the current firmware buffer doesn't start with 0x01 or
1075          * the size of remain buffer is smaller than HCI command header,
1076          * the firmware file is corrupted and it should stop the patching
1077          * process.
1078          */
1079         if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1080                 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1081                 return -EINVAL;
1082         }
1083         (*fw_ptr)++;
1084         remain--;
1085
1086         cmd = (struct hci_command_hdr *)(*fw_ptr);
1087         *fw_ptr += sizeof(*cmd);
1088         remain -= sizeof(*cmd);
1089
1090         /* Ensure that the remain firmware data is long enough than the length
1091          * of command parameter. If not, the firmware file is corrupted.
1092          */
1093         if (remain < cmd->plen) {
1094                 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1095                 return -EFAULT;
1096         }
1097
1098         /* If there is a command that loads a patch in the firmware
1099          * file, then enable the patch upon success, otherwise just
1100          * disable the manufacturer mode, for example patch activation
1101          * is not required when the default firmware patch file is used
1102          * because there are no patch data to load.
1103          */
1104         if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1105                 *disable_patch = 0;
1106
1107         cmd_param = *fw_ptr;
1108         *fw_ptr += cmd->plen;
1109         remain -= cmd->plen;
1110
1111         /* This reads the expected events when the above command is sent to the
1112          * device. Some vendor commands expects more than one events, for
1113          * example command status event followed by vendor specific event.
1114          * For this case, it only keeps the last expected event. so the command
1115          * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1116          * last expected event.
1117          */
1118         while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1119                 (*fw_ptr)++;
1120                 remain--;
1121
1122                 evt = (struct hci_event_hdr *)(*fw_ptr);
1123                 *fw_ptr += sizeof(*evt);
1124                 remain -= sizeof(*evt);
1125
1126                 if (remain < evt->plen) {
1127                         BT_ERR("%s Intel fw corrupted: invalid evt len",
1128                                hdev->name);
1129                         return -EFAULT;
1130                 }
1131
1132                 evt_param = *fw_ptr;
1133                 *fw_ptr += evt->plen;
1134                 remain -= evt->plen;
1135         }
1136
1137         /* Every HCI commands in the firmware file has its correspond event.
1138          * If event is not found or remain is smaller than zero, the firmware
1139          * file is corrupted.
1140          */
1141         if (!evt || !evt_param || remain < 0) {
1142                 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1143                 return -EFAULT;
1144         }
1145
1146         skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1147                                 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1148         if (IS_ERR(skb)) {
1149                 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1150                        hdev->name, cmd->opcode, PTR_ERR(skb));
1151                 return PTR_ERR(skb);
1152         }
1153
1154         /* It ensures that the returned event matches the event data read from
1155          * the firmware file. At fist, it checks the length and then
1156          * the contents of the event.
1157          */
1158         if (skb->len != evt->plen) {
1159                 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1160                        le16_to_cpu(cmd->opcode));
1161                 kfree_skb(skb);
1162                 return -EFAULT;
1163         }
1164
1165         if (memcmp(skb->data, evt_param, evt->plen)) {
1166                 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1167                        hdev->name, le16_to_cpu(cmd->opcode));
1168                 kfree_skb(skb);
1169                 return -EFAULT;
1170         }
1171         kfree_skb(skb);
1172
1173         return 0;
1174 }
1175
1176 static int btusb_setup_intel(struct hci_dev *hdev)
1177 {
1178         struct sk_buff *skb;
1179         const struct firmware *fw;
1180         const u8 *fw_ptr;
1181         int disable_patch;
1182         struct intel_version *ver;
1183
1184         const u8 mfg_enable[] = { 0x01, 0x00 };
1185         const u8 mfg_disable[] = { 0x00, 0x00 };
1186         const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1187         const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1188
1189         BT_DBG("%s", hdev->name);
1190
1191         /* The controller has a bug with the first HCI command sent to it
1192          * returning number of completed commands as zero. This would stall the
1193          * command processing in the Bluetooth core.
1194          *
1195          * As a workaround, send HCI Reset command first which will reset the
1196          * number of completed commands and allow normal command processing
1197          * from now on.
1198          */
1199         skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1200         if (IS_ERR(skb)) {
1201                 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1202                        hdev->name, PTR_ERR(skb));
1203                 return PTR_ERR(skb);
1204         }
1205         kfree_skb(skb);
1206
1207         /* Read Intel specific controller version first to allow selection of
1208          * which firmware file to load.
1209          *
1210          * The returned information are hardware variant and revision plus
1211          * firmware variant, revision and build number.
1212          */
1213         skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1214         if (IS_ERR(skb)) {
1215                 BT_ERR("%s reading Intel fw version command failed (%ld)",
1216                        hdev->name, PTR_ERR(skb));
1217                 return PTR_ERR(skb);
1218         }
1219
1220         if (skb->len != sizeof(*ver)) {
1221                 BT_ERR("%s Intel version event length mismatch", hdev->name);
1222                 kfree_skb(skb);
1223                 return -EIO;
1224         }
1225
1226         ver = (struct intel_version *)skb->data;
1227         if (ver->status) {
1228                 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1229                        ver->status);
1230                 kfree_skb(skb);
1231                 return -bt_to_errno(ver->status);
1232         }
1233
1234         BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1235                 hdev->name, ver->hw_platform, ver->hw_variant,
1236                 ver->hw_revision, ver->fw_variant,  ver->fw_revision,
1237                 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1238                 ver->fw_patch_num);
1239
1240         /* fw_patch_num indicates the version of patch the device currently
1241          * have. If there is no patch data in the device, it is always 0x00.
1242          * So, if it is other than 0x00, no need to patch the deivce again.
1243          */
1244         if (ver->fw_patch_num) {
1245                 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1246                         hdev->name, ver->fw_patch_num);
1247                 kfree_skb(skb);
1248                 return 0;
1249         }
1250
1251         /* Opens the firmware patch file based on the firmware version read
1252          * from the controller. If it fails to open the matching firmware
1253          * patch file, it tries to open the default firmware patch file.
1254          * If no patch file is found, allow the device to operate without
1255          * a patch.
1256          */
1257         fw = btusb_setup_intel_get_fw(hdev, ver);
1258         if (!fw) {
1259                 kfree_skb(skb);
1260                 return 0;
1261         }
1262         fw_ptr = fw->data;
1263
1264         /* This Intel specific command enables the manufacturer mode of the
1265          * controller.
1266          *
1267          * Only while this mode is enabled, the driver can download the
1268          * firmware patch data and configuration parameters.
1269          */
1270         skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1271         if (IS_ERR(skb)) {
1272                 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1273                        hdev->name, PTR_ERR(skb));
1274                 release_firmware(fw);
1275                 return PTR_ERR(skb);
1276         }
1277
1278         if (skb->data[0]) {
1279                 u8 evt_status = skb->data[0];
1280                 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1281                        hdev->name, evt_status);
1282                 kfree_skb(skb);
1283                 release_firmware(fw);
1284                 return -bt_to_errno(evt_status);
1285         }
1286         kfree_skb(skb);
1287
1288         disable_patch = 1;
1289
1290         /* The firmware data file consists of list of Intel specific HCI
1291          * commands and its expected events. The first byte indicates the
1292          * type of the message, either HCI command or HCI event.
1293          *
1294          * It reads the command and its expected event from the firmware file,
1295          * and send to the controller. Once __hci_cmd_sync_ev() returns,
1296          * the returned event is compared with the event read from the firmware
1297          * file and it will continue until all the messages are downloaded to
1298          * the controller.
1299          *
1300          * Once the firmware patching is completed successfully,
1301          * the manufacturer mode is disabled with reset and activating the
1302          * downloaded patch.
1303          *
1304          * If the firmware patching fails, the manufacturer mode is
1305          * disabled with reset and deactivating the patch.
1306          *
1307          * If the default patch file is used, no reset is done when disabling
1308          * the manufacturer.
1309          */
1310         while (fw->size > fw_ptr - fw->data) {
1311                 int ret;
1312
1313                 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1314                                                  &disable_patch);
1315                 if (ret < 0)
1316                         goto exit_mfg_deactivate;
1317         }
1318
1319         release_firmware(fw);
1320
1321         if (disable_patch)
1322                 goto exit_mfg_disable;
1323
1324         /* Patching completed successfully and disable the manufacturer mode
1325          * with reset and activate the downloaded firmware patches.
1326          */
1327         skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1328                              mfg_reset_activate, HCI_INIT_TIMEOUT);
1329         if (IS_ERR(skb)) {
1330                 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1331                        hdev->name, PTR_ERR(skb));
1332                 return PTR_ERR(skb);
1333         }
1334         kfree_skb(skb);
1335
1336         BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1337                 hdev->name);
1338
1339         return 0;
1340
1341 exit_mfg_disable:
1342         /* Disable the manufacturer mode without reset */
1343         skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1344                              HCI_INIT_TIMEOUT);
1345         if (IS_ERR(skb)) {
1346                 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1347                        hdev->name, PTR_ERR(skb));
1348                 return PTR_ERR(skb);
1349         }
1350         kfree_skb(skb);
1351
1352         BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
1353         return 0;
1354
1355 exit_mfg_deactivate:
1356         release_firmware(fw);
1357
1358         /* Patching failed. Disable the manufacturer mode with reset and
1359          * deactivate the downloaded firmware patches.
1360          */
1361         skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1362                              mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1363         if (IS_ERR(skb)) {
1364                 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1365                        hdev->name, PTR_ERR(skb));
1366                 return PTR_ERR(skb);
1367         }
1368         kfree_skb(skb);
1369
1370         BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1371                 hdev->name);
1372
1373         return 0;
1374 }
1375
1376 static int btusb_probe(struct usb_interface *intf,
1377                                 const struct usb_device_id *id)
1378 {
1379         struct usb_endpoint_descriptor *ep_desc;
1380         struct btusb_data *data;
1381         struct hci_dev *hdev;
1382         int i, err;
1383
1384         BT_DBG("intf %p id %p", intf, id);
1385
1386         /* interface numbers are hardcoded in the spec */
1387         if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
1388                 return -ENODEV;
1389
1390         if (!id->driver_info) {
1391                 const struct usb_device_id *match;
1392                 match = usb_match_id(intf, blacklist_table);
1393                 if (match)
1394                         id = match;
1395         }
1396
1397         if (id->driver_info == BTUSB_IGNORE)
1398                 return -ENODEV;
1399
1400         if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
1401                 return -ENODEV;
1402
1403         if (ignore_csr && id->driver_info & BTUSB_CSR)
1404                 return -ENODEV;
1405
1406         if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
1407                 return -ENODEV;
1408
1409         if (id->driver_info & BTUSB_ATH3012) {
1410                 struct usb_device *udev = interface_to_usbdev(intf);
1411
1412                 /* Old firmware would otherwise let ath3k driver load
1413                  * patch and sysconfig files */
1414                 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
1415                         return -ENODEV;
1416         }
1417
1418         data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
1419         if (!data)
1420                 return -ENOMEM;
1421
1422         for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1423                 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1424
1425                 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
1426                         data->intr_ep = ep_desc;
1427                         continue;
1428                 }
1429
1430                 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
1431                         data->bulk_tx_ep = ep_desc;
1432                         continue;
1433                 }
1434
1435                 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
1436                         data->bulk_rx_ep = ep_desc;
1437                         continue;
1438                 }
1439         }
1440
1441         if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
1442                 return -ENODEV;
1443
1444         data->cmdreq_type = USB_TYPE_CLASS;
1445
1446         data->udev = interface_to_usbdev(intf);
1447         data->intf = intf;
1448
1449         spin_lock_init(&data->lock);
1450
1451         INIT_WORK(&data->work, btusb_work);
1452         INIT_WORK(&data->waker, btusb_waker);
1453         spin_lock_init(&data->txlock);
1454
1455         init_usb_anchor(&data->tx_anchor);
1456         init_usb_anchor(&data->intr_anchor);
1457         init_usb_anchor(&data->bulk_anchor);
1458         init_usb_anchor(&data->isoc_anchor);
1459         init_usb_anchor(&data->deferred);
1460
1461         hdev = hci_alloc_dev();
1462         if (!hdev)
1463                 return -ENOMEM;
1464
1465         hdev->bus = HCI_USB;
1466         hci_set_drvdata(hdev, data);
1467
1468         data->hdev = hdev;
1469
1470         SET_HCIDEV_DEV(hdev, &intf->dev);
1471
1472         hdev->open   = btusb_open;
1473         hdev->close  = btusb_close;
1474         hdev->flush  = btusb_flush;
1475         hdev->send   = btusb_send_frame;
1476         hdev->notify = btusb_notify;
1477
1478         if (id->driver_info & BTUSB_BCM92035)
1479                 hdev->setup = btusb_setup_bcm92035;
1480
1481         if (id->driver_info & BTUSB_INTEL) {
1482                 usb_enable_autosuspend(data->udev);
1483                 hdev->setup = btusb_setup_intel;
1484         }
1485
1486         /* Interface numbers are hardcoded in the specification */
1487         data->isoc = usb_ifnum_to_if(data->udev, 1);
1488
1489         if (!reset)
1490                 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1491
1492         if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
1493                 if (!disable_scofix)
1494                         set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1495         }
1496
1497         if (id->driver_info & BTUSB_BROKEN_ISOC)
1498                 data->isoc = NULL;
1499
1500         if (id->driver_info & BTUSB_DIGIANSWER) {
1501                 data->cmdreq_type = USB_TYPE_VENDOR;
1502                 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1503         }
1504
1505         if (id->driver_info & BTUSB_CSR) {
1506                 struct usb_device *udev = data->udev;
1507                 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
1508
1509                 /* Old firmware would otherwise execute USB reset */
1510                 if (bcdDevice < 0x117)
1511                         set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1512
1513                 /* Fake CSR devices with broken commands */
1514                 if (bcdDevice <= 0x100)
1515                         hdev->setup = btusb_setup_csr;
1516         }
1517
1518         if (id->driver_info & BTUSB_SNIFFER) {
1519                 struct usb_device *udev = data->udev;
1520
1521                 /* New sniffer firmware has crippled HCI interface */
1522                 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1523                         set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
1524
1525                 data->isoc = NULL;
1526         }
1527
1528         if (data->isoc) {
1529                 err = usb_driver_claim_interface(&btusb_driver,
1530                                                         data->isoc, data);
1531                 if (err < 0) {
1532                         hci_free_dev(hdev);
1533                         return err;
1534                 }
1535         }
1536
1537         err = hci_register_dev(hdev);
1538         if (err < 0) {
1539                 hci_free_dev(hdev);
1540                 return err;
1541         }
1542
1543         usb_set_intfdata(intf, data);
1544
1545         return 0;
1546 }
1547
1548 static void btusb_disconnect(struct usb_interface *intf)
1549 {
1550         struct btusb_data *data = usb_get_intfdata(intf);
1551         struct hci_dev *hdev;
1552
1553         BT_DBG("intf %p", intf);
1554
1555         if (!data)
1556                 return;
1557
1558         hdev = data->hdev;
1559         usb_set_intfdata(data->intf, NULL);
1560
1561         if (data->isoc)
1562                 usb_set_intfdata(data->isoc, NULL);
1563
1564         hci_unregister_dev(hdev);
1565
1566         if (intf == data->isoc)
1567                 usb_driver_release_interface(&btusb_driver, data->intf);
1568         else if (data->isoc)
1569                 usb_driver_release_interface(&btusb_driver, data->isoc);
1570
1571         hci_free_dev(hdev);
1572 }
1573
1574 #ifdef CONFIG_PM
1575 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1576 {
1577         struct btusb_data *data = usb_get_intfdata(intf);
1578
1579         BT_DBG("intf %p", intf);
1580
1581         if (data->suspend_count++)
1582                 return 0;
1583
1584         spin_lock_irq(&data->txlock);
1585         if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
1586                 set_bit(BTUSB_SUSPENDING, &data->flags);
1587                 spin_unlock_irq(&data->txlock);
1588         } else {
1589                 spin_unlock_irq(&data->txlock);
1590                 data->suspend_count--;
1591                 return -EBUSY;
1592         }
1593
1594         cancel_work_sync(&data->work);
1595
1596         btusb_stop_traffic(data);
1597         usb_kill_anchored_urbs(&data->tx_anchor);
1598
1599         return 0;
1600 }
1601
1602 static void play_deferred(struct btusb_data *data)
1603 {
1604         struct urb *urb;
1605         int err;
1606
1607         while ((urb = usb_get_from_anchor(&data->deferred))) {
1608                 err = usb_submit_urb(urb, GFP_ATOMIC);
1609                 if (err < 0)
1610                         break;
1611
1612                 data->tx_in_flight++;
1613         }
1614         usb_scuttle_anchored_urbs(&data->deferred);
1615 }
1616
1617 static int btusb_resume(struct usb_interface *intf)
1618 {
1619         struct btusb_data *data = usb_get_intfdata(intf);
1620         struct hci_dev *hdev = data->hdev;
1621         int err = 0;
1622
1623         BT_DBG("intf %p", intf);
1624
1625         if (--data->suspend_count)
1626                 return 0;
1627
1628         if (!test_bit(HCI_RUNNING, &hdev->flags))
1629                 goto done;
1630
1631         if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1632                 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1633                 if (err < 0) {
1634                         clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1635                         goto failed;
1636                 }
1637         }
1638
1639         if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
1640                 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1641                 if (err < 0) {
1642                         clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1643                         goto failed;
1644                 }
1645
1646                 btusb_submit_bulk_urb(hdev, GFP_NOIO);
1647         }
1648
1649         if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1650                 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1651                         clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1652                 else
1653                         btusb_submit_isoc_urb(hdev, GFP_NOIO);
1654         }
1655
1656         spin_lock_irq(&data->txlock);
1657         play_deferred(data);
1658         clear_bit(BTUSB_SUSPENDING, &data->flags);
1659         spin_unlock_irq(&data->txlock);
1660         schedule_work(&data->work);
1661
1662         return 0;
1663
1664 failed:
1665         usb_scuttle_anchored_urbs(&data->deferred);
1666 done:
1667         spin_lock_irq(&data->txlock);
1668         clear_bit(BTUSB_SUSPENDING, &data->flags);
1669         spin_unlock_irq(&data->txlock);
1670
1671         return err;
1672 }
1673 #endif
1674
1675 static struct usb_driver btusb_driver = {
1676         .name           = "btusb",
1677         .probe          = btusb_probe,
1678         .disconnect     = btusb_disconnect,
1679 #ifdef CONFIG_PM
1680         .suspend        = btusb_suspend,
1681         .resume         = btusb_resume,
1682 #endif
1683         .id_table       = btusb_table,
1684         .supports_autosuspend = 1,
1685         .disable_hub_initiated_lpm = 1,
1686 };
1687
1688 module_usb_driver(btusb_driver);
1689
1690 module_param(ignore_dga, bool, 0644);
1691 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1692
1693 module_param(ignore_csr, bool, 0644);
1694 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1695
1696 module_param(ignore_sniffer, bool, 0644);
1697 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1698
1699 module_param(disable_scofix, bool, 0644);
1700 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1701
1702 module_param(force_scofix, bool, 0644);
1703 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1704
1705 module_param(reset, bool, 0644);
1706 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1707
1708 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1709 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1710 MODULE_VERSION(VERSION);
1711 MODULE_LICENSE("GPL");