Bluetooth: Add support for GC-WB300D PCIe [04ca:3006] to ath3k.
[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/kernel.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/types.h>
29 #include <linux/sched.h>
30 #include <linux/errno.h>
31 #include <linux/skbuff.h>
32
33 #include <linux/usb.h>
34
35 #include <net/bluetooth/bluetooth.h>
36 #include <net/bluetooth/hci_core.h>
37
38 #define VERSION "0.6"
39
40 static int ignore_dga;
41 static int ignore_csr;
42 static int ignore_sniffer;
43 static int disable_scofix;
44 static int force_scofix;
45
46 static int reset = 1;
47
48 static struct usb_driver btusb_driver;
49
50 #define BTUSB_IGNORE            0x01
51 #define BTUSB_DIGIANSWER        0x02
52 #define BTUSB_CSR               0x04
53 #define BTUSB_SNIFFER           0x08
54 #define BTUSB_BCM92035          0x10
55 #define BTUSB_BROKEN_ISOC       0x20
56 #define BTUSB_WRONG_SCO_MTU     0x40
57 #define BTUSB_ATH3012           0x80
58
59 static struct usb_device_id btusb_table[] = {
60         /* Generic Bluetooth USB device */
61         { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
62
63         /* Apple-specific (Broadcom) devices */
64         { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
65
66         /* Broadcom SoftSailing reporting vendor specific */
67         { USB_DEVICE(0x0a5c, 0x21e1) },
68
69         /* Apple MacBookPro 7,1 */
70         { USB_DEVICE(0x05ac, 0x8213) },
71
72         /* Apple iMac11,1 */
73         { USB_DEVICE(0x05ac, 0x8215) },
74
75         /* Apple MacBookPro6,2 */
76         { USB_DEVICE(0x05ac, 0x8218) },
77
78         /* Apple MacBookAir3,1, MacBookAir3,2 */
79         { USB_DEVICE(0x05ac, 0x821b) },
80
81         /* Apple MacBookAir4,1 */
82         { USB_DEVICE(0x05ac, 0x821f) },
83
84         /* Apple MacBookPro8,2 */
85         { USB_DEVICE(0x05ac, 0x821a) },
86
87         /* Apple MacMini5,1 */
88         { USB_DEVICE(0x05ac, 0x8281) },
89
90         /* AVM BlueFRITZ! USB v2.0 */
91         { USB_DEVICE(0x057c, 0x3800) },
92
93         /* Bluetooth Ultraport Module from IBM */
94         { USB_DEVICE(0x04bf, 0x030a) },
95
96         /* ALPS Modules with non-standard id */
97         { USB_DEVICE(0x044e, 0x3001) },
98         { USB_DEVICE(0x044e, 0x3002) },
99
100         /* Ericsson with non-standard id */
101         { USB_DEVICE(0x0bdb, 0x1002) },
102
103         /* Canyon CN-BTU1 with HID interfaces */
104         { USB_DEVICE(0x0c10, 0x0000) },
105
106         /* Broadcom BCM20702A0 */
107         { USB_DEVICE(0x0b05, 0x17b5) },
108         { USB_DEVICE(0x04ca, 0x2003) },
109         { USB_DEVICE(0x0489, 0xe042) },
110         { USB_DEVICE(0x413c, 0x8197) },
111
112         /* Foxconn - Hon Hai */
113         { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01) },
114
115         /*Broadcom devices with vendor specific id */
116         { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01) },
117
118         { }     /* Terminating entry */
119 };
120
121 MODULE_DEVICE_TABLE(usb, btusb_table);
122
123 static struct usb_device_id blacklist_table[] = {
124         /* CSR BlueCore devices */
125         { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
126
127         /* Broadcom BCM2033 without firmware */
128         { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
129
130         /* Atheros 3011 with sflash firmware */
131         { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
132         { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
133         { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
134         { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
135         { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
136         { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
137
138         /* Atheros AR9285 Malbec with sflash firmware */
139         { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
140
141         /* Atheros 3012 with sflash firmware */
142         { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
143         { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
144         { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
145         { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
146         { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
147         { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
148         { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
149         { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
150         { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
151         { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
152
153         /* Atheros AR5BBU12 with sflash firmware */
154         { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
155
156         /* Atheros AR5BBU12 with sflash firmware */
157         { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
158         { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
159
160         /* Broadcom BCM2035 */
161         { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
162         { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
163         { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
164
165         /* Broadcom BCM2045 */
166         { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
167         { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
168
169         /* IBM/Lenovo ThinkPad with Broadcom chip */
170         { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
171         { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
172
173         /* HP laptop with Broadcom chip */
174         { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
175
176         /* Dell laptop with Broadcom chip */
177         { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
178
179         /* Dell Wireless 370 and 410 devices */
180         { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
181         { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
182
183         /* Belkin F8T012 and F8T013 devices */
184         { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
185         { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
186
187         /* Asus WL-BTD202 device */
188         { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
189
190         /* Kensington Bluetooth USB adapter */
191         { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
192
193         /* RTX Telecom based adapters with buggy SCO support */
194         { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
195         { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
196
197         /* CONWISE Technology based adapters with buggy SCO support */
198         { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
199
200         /* Digianswer devices */
201         { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
202         { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
203
204         /* CSR BlueCore Bluetooth Sniffer */
205         { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
206
207         /* Frontline ComProbe Bluetooth Sniffer */
208         { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
209
210         { }     /* Terminating entry */
211 };
212
213 #define BTUSB_MAX_ISOC_FRAMES   10
214
215 #define BTUSB_INTR_RUNNING      0
216 #define BTUSB_BULK_RUNNING      1
217 #define BTUSB_ISOC_RUNNING      2
218 #define BTUSB_SUSPENDING        3
219 #define BTUSB_DID_ISO_RESUME    4
220
221 struct btusb_data {
222         struct hci_dev       *hdev;
223         struct usb_device    *udev;
224         struct usb_interface *intf;
225         struct usb_interface *isoc;
226
227         spinlock_t lock;
228
229         unsigned long flags;
230
231         struct work_struct work;
232         struct work_struct waker;
233
234         struct usb_anchor tx_anchor;
235         struct usb_anchor intr_anchor;
236         struct usb_anchor bulk_anchor;
237         struct usb_anchor isoc_anchor;
238         struct usb_anchor deferred;
239         int tx_in_flight;
240         spinlock_t txlock;
241
242         struct usb_endpoint_descriptor *intr_ep;
243         struct usb_endpoint_descriptor *bulk_tx_ep;
244         struct usb_endpoint_descriptor *bulk_rx_ep;
245         struct usb_endpoint_descriptor *isoc_tx_ep;
246         struct usb_endpoint_descriptor *isoc_rx_ep;
247
248         __u8 cmdreq_type;
249
250         unsigned int sco_num;
251         int isoc_altsetting;
252         int suspend_count;
253 };
254
255 static int inc_tx(struct btusb_data *data)
256 {
257         unsigned long flags;
258         int rv;
259
260         spin_lock_irqsave(&data->txlock, flags);
261         rv = test_bit(BTUSB_SUSPENDING, &data->flags);
262         if (!rv)
263                 data->tx_in_flight++;
264         spin_unlock_irqrestore(&data->txlock, flags);
265
266         return rv;
267 }
268
269 static void btusb_intr_complete(struct urb *urb)
270 {
271         struct hci_dev *hdev = urb->context;
272         struct btusb_data *data = hdev->driver_data;
273         int err;
274
275         BT_DBG("%s urb %p status %d count %d", hdev->name,
276                                         urb, urb->status, urb->actual_length);
277
278         if (!test_bit(HCI_RUNNING, &hdev->flags))
279                 return;
280
281         if (urb->status == 0) {
282                 hdev->stat.byte_rx += urb->actual_length;
283
284                 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
285                                                 urb->transfer_buffer,
286                                                 urb->actual_length) < 0) {
287                         BT_ERR("%s corrupted event packet", hdev->name);
288                         hdev->stat.err_rx++;
289                 }
290         }
291
292         if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
293                 return;
294
295         usb_mark_last_busy(data->udev);
296         usb_anchor_urb(urb, &data->intr_anchor);
297
298         err = usb_submit_urb(urb, GFP_ATOMIC);
299         if (err < 0) {
300                 /* -EPERM: urb is being killed;
301                  * -ENODEV: device got disconnected */
302                 if (err != -EPERM && err != -ENODEV)
303                         BT_ERR("%s urb %p failed to resubmit (%d)",
304                                                 hdev->name, urb, -err);
305                 usb_unanchor_urb(urb);
306         }
307 }
308
309 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
310 {
311         struct btusb_data *data = hdev->driver_data;
312         struct urb *urb;
313         unsigned char *buf;
314         unsigned int pipe;
315         int err, size;
316
317         BT_DBG("%s", hdev->name);
318
319         if (!data->intr_ep)
320                 return -ENODEV;
321
322         urb = usb_alloc_urb(0, mem_flags);
323         if (!urb)
324                 return -ENOMEM;
325
326         size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
327
328         buf = kmalloc(size, mem_flags);
329         if (!buf) {
330                 usb_free_urb(urb);
331                 return -ENOMEM;
332         }
333
334         pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
335
336         usb_fill_int_urb(urb, data->udev, pipe, buf, size,
337                                                 btusb_intr_complete, hdev,
338                                                 data->intr_ep->bInterval);
339
340         urb->transfer_flags |= URB_FREE_BUFFER;
341
342         usb_anchor_urb(urb, &data->intr_anchor);
343
344         err = usb_submit_urb(urb, mem_flags);
345         if (err < 0) {
346                 BT_ERR("%s urb %p submission failed (%d)",
347                                                 hdev->name, urb, -err);
348                 usb_unanchor_urb(urb);
349         }
350
351         usb_free_urb(urb);
352
353         return err;
354 }
355
356 static void btusb_bulk_complete(struct urb *urb)
357 {
358         struct hci_dev *hdev = urb->context;
359         struct btusb_data *data = hdev->driver_data;
360         int err;
361
362         BT_DBG("%s urb %p status %d count %d", hdev->name,
363                                         urb, urb->status, urb->actual_length);
364
365         if (!test_bit(HCI_RUNNING, &hdev->flags))
366                 return;
367
368         if (urb->status == 0) {
369                 hdev->stat.byte_rx += urb->actual_length;
370
371                 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
372                                                 urb->transfer_buffer,
373                                                 urb->actual_length) < 0) {
374                         BT_ERR("%s corrupted ACL packet", hdev->name);
375                         hdev->stat.err_rx++;
376                 }
377         }
378
379         if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
380                 return;
381
382         usb_anchor_urb(urb, &data->bulk_anchor);
383         usb_mark_last_busy(data->udev);
384
385         err = usb_submit_urb(urb, GFP_ATOMIC);
386         if (err < 0) {
387                 /* -EPERM: urb is being killed;
388                  * -ENODEV: device got disconnected */
389                 if (err != -EPERM && err != -ENODEV)
390                         BT_ERR("%s urb %p failed to resubmit (%d)",
391                                                 hdev->name, urb, -err);
392                 usb_unanchor_urb(urb);
393         }
394 }
395
396 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
397 {
398         struct btusb_data *data = hdev->driver_data;
399         struct urb *urb;
400         unsigned char *buf;
401         unsigned int pipe;
402         int err, size = HCI_MAX_FRAME_SIZE;
403
404         BT_DBG("%s", hdev->name);
405
406         if (!data->bulk_rx_ep)
407                 return -ENODEV;
408
409         urb = usb_alloc_urb(0, mem_flags);
410         if (!urb)
411                 return -ENOMEM;
412
413         buf = kmalloc(size, mem_flags);
414         if (!buf) {
415                 usb_free_urb(urb);
416                 return -ENOMEM;
417         }
418
419         pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
420
421         usb_fill_bulk_urb(urb, data->udev, pipe,
422                                         buf, size, btusb_bulk_complete, hdev);
423
424         urb->transfer_flags |= URB_FREE_BUFFER;
425
426         usb_mark_last_busy(data->udev);
427         usb_anchor_urb(urb, &data->bulk_anchor);
428
429         err = usb_submit_urb(urb, mem_flags);
430         if (err < 0) {
431                 BT_ERR("%s urb %p submission failed (%d)",
432                                                 hdev->name, urb, -err);
433                 usb_unanchor_urb(urb);
434         }
435
436         usb_free_urb(urb);
437
438         return err;
439 }
440
441 static void btusb_isoc_complete(struct urb *urb)
442 {
443         struct hci_dev *hdev = urb->context;
444         struct btusb_data *data = hdev->driver_data;
445         int i, err;
446
447         BT_DBG("%s urb %p status %d count %d", hdev->name,
448                                         urb, urb->status, urb->actual_length);
449
450         if (!test_bit(HCI_RUNNING, &hdev->flags))
451                 return;
452
453         if (urb->status == 0) {
454                 for (i = 0; i < urb->number_of_packets; i++) {
455                         unsigned int offset = urb->iso_frame_desc[i].offset;
456                         unsigned int length = urb->iso_frame_desc[i].actual_length;
457
458                         if (urb->iso_frame_desc[i].status)
459                                 continue;
460
461                         hdev->stat.byte_rx += length;
462
463                         if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
464                                                 urb->transfer_buffer + offset,
465                                                                 length) < 0) {
466                                 BT_ERR("%s corrupted SCO packet", hdev->name);
467                                 hdev->stat.err_rx++;
468                         }
469                 }
470         }
471
472         if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
473                 return;
474
475         usb_anchor_urb(urb, &data->isoc_anchor);
476
477         err = usb_submit_urb(urb, GFP_ATOMIC);
478         if (err < 0) {
479                 /* -EPERM: urb is being killed;
480                  * -ENODEV: device got disconnected */
481                 if (err != -EPERM && err != -ENODEV)
482                         BT_ERR("%s urb %p failed to resubmit (%d)",
483                                                 hdev->name, urb, -err);
484                 usb_unanchor_urb(urb);
485         }
486 }
487
488 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
489 {
490         int i, offset = 0;
491
492         BT_DBG("len %d mtu %d", len, mtu);
493
494         for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
495                                         i++, offset += mtu, len -= mtu) {
496                 urb->iso_frame_desc[i].offset = offset;
497                 urb->iso_frame_desc[i].length = mtu;
498         }
499
500         if (len && i < BTUSB_MAX_ISOC_FRAMES) {
501                 urb->iso_frame_desc[i].offset = offset;
502                 urb->iso_frame_desc[i].length = len;
503                 i++;
504         }
505
506         urb->number_of_packets = i;
507 }
508
509 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
510 {
511         struct btusb_data *data = hdev->driver_data;
512         struct urb *urb;
513         unsigned char *buf;
514         unsigned int pipe;
515         int err, size;
516
517         BT_DBG("%s", hdev->name);
518
519         if (!data->isoc_rx_ep)
520                 return -ENODEV;
521
522         urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
523         if (!urb)
524                 return -ENOMEM;
525
526         size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
527                                                 BTUSB_MAX_ISOC_FRAMES;
528
529         buf = kmalloc(size, mem_flags);
530         if (!buf) {
531                 usb_free_urb(urb);
532                 return -ENOMEM;
533         }
534
535         pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
536
537         usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
538                                 hdev, data->isoc_rx_ep->bInterval);
539
540         urb->transfer_flags  = URB_FREE_BUFFER | URB_ISO_ASAP;
541
542         __fill_isoc_descriptor(urb, size,
543                         le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
544
545         usb_anchor_urb(urb, &data->isoc_anchor);
546
547         err = usb_submit_urb(urb, mem_flags);
548         if (err < 0) {
549                 BT_ERR("%s urb %p submission failed (%d)",
550                                                 hdev->name, urb, -err);
551                 usb_unanchor_urb(urb);
552         }
553
554         usb_free_urb(urb);
555
556         return err;
557 }
558
559 static void btusb_tx_complete(struct urb *urb)
560 {
561         struct sk_buff *skb = urb->context;
562         struct hci_dev *hdev = (struct hci_dev *) skb->dev;
563         struct btusb_data *data = hdev->driver_data;
564
565         BT_DBG("%s urb %p status %d count %d", hdev->name,
566                                         urb, urb->status, urb->actual_length);
567
568         if (!test_bit(HCI_RUNNING, &hdev->flags))
569                 goto done;
570
571         if (!urb->status)
572                 hdev->stat.byte_tx += urb->transfer_buffer_length;
573         else
574                 hdev->stat.err_tx++;
575
576 done:
577         spin_lock(&data->txlock);
578         data->tx_in_flight--;
579         spin_unlock(&data->txlock);
580
581         kfree(urb->setup_packet);
582
583         kfree_skb(skb);
584 }
585
586 static void btusb_isoc_tx_complete(struct urb *urb)
587 {
588         struct sk_buff *skb = urb->context;
589         struct hci_dev *hdev = (struct hci_dev *) skb->dev;
590
591         BT_DBG("%s urb %p status %d count %d", hdev->name,
592                                         urb, urb->status, urb->actual_length);
593
594         if (!test_bit(HCI_RUNNING, &hdev->flags))
595                 goto done;
596
597         if (!urb->status)
598                 hdev->stat.byte_tx += urb->transfer_buffer_length;
599         else
600                 hdev->stat.err_tx++;
601
602 done:
603         kfree(urb->setup_packet);
604
605         kfree_skb(skb);
606 }
607
608 static int btusb_open(struct hci_dev *hdev)
609 {
610         struct btusb_data *data = hdev->driver_data;
611         int err;
612
613         BT_DBG("%s", hdev->name);
614
615         err = usb_autopm_get_interface(data->intf);
616         if (err < 0)
617                 return err;
618
619         data->intf->needs_remote_wakeup = 1;
620
621         if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
622                 goto done;
623
624         if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
625                 goto done;
626
627         err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
628         if (err < 0)
629                 goto failed;
630
631         err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
632         if (err < 0) {
633                 usb_kill_anchored_urbs(&data->intr_anchor);
634                 goto failed;
635         }
636
637         set_bit(BTUSB_BULK_RUNNING, &data->flags);
638         btusb_submit_bulk_urb(hdev, GFP_KERNEL);
639
640 done:
641         usb_autopm_put_interface(data->intf);
642         return 0;
643
644 failed:
645         clear_bit(BTUSB_INTR_RUNNING, &data->flags);
646         clear_bit(HCI_RUNNING, &hdev->flags);
647         usb_autopm_put_interface(data->intf);
648         return err;
649 }
650
651 static void btusb_stop_traffic(struct btusb_data *data)
652 {
653         usb_kill_anchored_urbs(&data->intr_anchor);
654         usb_kill_anchored_urbs(&data->bulk_anchor);
655         usb_kill_anchored_urbs(&data->isoc_anchor);
656 }
657
658 static int btusb_close(struct hci_dev *hdev)
659 {
660         struct btusb_data *data = hdev->driver_data;
661         int err;
662
663         BT_DBG("%s", hdev->name);
664
665         if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
666                 return 0;
667
668         cancel_work_sync(&data->work);
669         cancel_work_sync(&data->waker);
670
671         clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
672         clear_bit(BTUSB_BULK_RUNNING, &data->flags);
673         clear_bit(BTUSB_INTR_RUNNING, &data->flags);
674
675         btusb_stop_traffic(data);
676         err = usb_autopm_get_interface(data->intf);
677         if (err < 0)
678                 goto failed;
679
680         data->intf->needs_remote_wakeup = 0;
681         usb_autopm_put_interface(data->intf);
682
683 failed:
684         usb_scuttle_anchored_urbs(&data->deferred);
685         return 0;
686 }
687
688 static int btusb_flush(struct hci_dev *hdev)
689 {
690         struct btusb_data *data = hdev->driver_data;
691
692         BT_DBG("%s", hdev->name);
693
694         usb_kill_anchored_urbs(&data->tx_anchor);
695
696         return 0;
697 }
698
699 static int btusb_send_frame(struct sk_buff *skb)
700 {
701         struct hci_dev *hdev = (struct hci_dev *) skb->dev;
702         struct btusb_data *data = hdev->driver_data;
703         struct usb_ctrlrequest *dr;
704         struct urb *urb;
705         unsigned int pipe;
706         int err;
707
708         BT_DBG("%s", hdev->name);
709
710         if (!test_bit(HCI_RUNNING, &hdev->flags))
711                 return -EBUSY;
712
713         switch (bt_cb(skb)->pkt_type) {
714         case HCI_COMMAND_PKT:
715                 urb = usb_alloc_urb(0, GFP_ATOMIC);
716                 if (!urb)
717                         return -ENOMEM;
718
719                 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
720                 if (!dr) {
721                         usb_free_urb(urb);
722                         return -ENOMEM;
723                 }
724
725                 dr->bRequestType = data->cmdreq_type;
726                 dr->bRequest     = 0;
727                 dr->wIndex       = 0;
728                 dr->wValue       = 0;
729                 dr->wLength      = __cpu_to_le16(skb->len);
730
731                 pipe = usb_sndctrlpipe(data->udev, 0x00);
732
733                 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
734                                 skb->data, skb->len, btusb_tx_complete, skb);
735
736                 hdev->stat.cmd_tx++;
737                 break;
738
739         case HCI_ACLDATA_PKT:
740                 if (!data->bulk_tx_ep)
741                         return -ENODEV;
742
743                 urb = usb_alloc_urb(0, GFP_ATOMIC);
744                 if (!urb)
745                         return -ENOMEM;
746
747                 pipe = usb_sndbulkpipe(data->udev,
748                                         data->bulk_tx_ep->bEndpointAddress);
749
750                 usb_fill_bulk_urb(urb, data->udev, pipe,
751                                 skb->data, skb->len, btusb_tx_complete, skb);
752
753                 hdev->stat.acl_tx++;
754                 break;
755
756         case HCI_SCODATA_PKT:
757                 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
758                         return -ENODEV;
759
760                 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
761                 if (!urb)
762                         return -ENOMEM;
763
764                 pipe = usb_sndisocpipe(data->udev,
765                                         data->isoc_tx_ep->bEndpointAddress);
766
767                 usb_fill_int_urb(urb, data->udev, pipe,
768                                 skb->data, skb->len, btusb_isoc_tx_complete,
769                                 skb, data->isoc_tx_ep->bInterval);
770
771                 urb->transfer_flags  = URB_ISO_ASAP;
772
773                 __fill_isoc_descriptor(urb, skb->len,
774                                 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
775
776                 hdev->stat.sco_tx++;
777                 goto skip_waking;
778
779         default:
780                 return -EILSEQ;
781         }
782
783         err = inc_tx(data);
784         if (err) {
785                 usb_anchor_urb(urb, &data->deferred);
786                 schedule_work(&data->waker);
787                 err = 0;
788                 goto done;
789         }
790
791 skip_waking:
792         usb_anchor_urb(urb, &data->tx_anchor);
793
794         err = usb_submit_urb(urb, GFP_ATOMIC);
795         if (err < 0) {
796                 BT_ERR("%s urb %p submission failed", hdev->name, urb);
797                 kfree(urb->setup_packet);
798                 usb_unanchor_urb(urb);
799         } else {
800                 usb_mark_last_busy(data->udev);
801         }
802
803 done:
804         usb_free_urb(urb);
805         return err;
806 }
807
808 static void btusb_destruct(struct hci_dev *hdev)
809 {
810         struct btusb_data *data = hdev->driver_data;
811
812         BT_DBG("%s", hdev->name);
813
814         kfree(data);
815 }
816
817 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
818 {
819         struct btusb_data *data = hdev->driver_data;
820
821         BT_DBG("%s evt %d", hdev->name, evt);
822
823         if (hdev->conn_hash.sco_num != data->sco_num) {
824                 data->sco_num = hdev->conn_hash.sco_num;
825                 schedule_work(&data->work);
826         }
827 }
828
829 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
830 {
831         struct btusb_data *data = hdev->driver_data;
832         struct usb_interface *intf = data->isoc;
833         struct usb_endpoint_descriptor *ep_desc;
834         int i, err;
835
836         if (!data->isoc)
837                 return -ENODEV;
838
839         err = usb_set_interface(data->udev, 1, altsetting);
840         if (err < 0) {
841                 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
842                 return err;
843         }
844
845         data->isoc_altsetting = altsetting;
846
847         data->isoc_tx_ep = NULL;
848         data->isoc_rx_ep = NULL;
849
850         for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
851                 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
852
853                 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
854                         data->isoc_tx_ep = ep_desc;
855                         continue;
856                 }
857
858                 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
859                         data->isoc_rx_ep = ep_desc;
860                         continue;
861                 }
862         }
863
864         if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
865                 BT_ERR("%s invalid SCO descriptors", hdev->name);
866                 return -ENODEV;
867         }
868
869         return 0;
870 }
871
872 static void btusb_work(struct work_struct *work)
873 {
874         struct btusb_data *data = container_of(work, struct btusb_data, work);
875         struct hci_dev *hdev = data->hdev;
876         int err;
877
878         if (hdev->conn_hash.sco_num > 0) {
879                 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
880                         err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
881                         if (err < 0) {
882                                 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
883                                 usb_kill_anchored_urbs(&data->isoc_anchor);
884                                 return;
885                         }
886
887                         set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
888                 }
889                 if (data->isoc_altsetting != 2) {
890                         clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
891                         usb_kill_anchored_urbs(&data->isoc_anchor);
892
893                         if (__set_isoc_interface(hdev, 2) < 0)
894                                 return;
895                 }
896
897                 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
898                         if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
899                                 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
900                         else
901                                 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
902                 }
903         } else {
904                 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
905                 usb_kill_anchored_urbs(&data->isoc_anchor);
906
907                 __set_isoc_interface(hdev, 0);
908                 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
909                         usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
910         }
911 }
912
913 static void btusb_waker(struct work_struct *work)
914 {
915         struct btusb_data *data = container_of(work, struct btusb_data, waker);
916         int err;
917
918         err = usb_autopm_get_interface(data->intf);
919         if (err < 0)
920                 return;
921
922         usb_autopm_put_interface(data->intf);
923 }
924
925 static int btusb_probe(struct usb_interface *intf,
926                                 const struct usb_device_id *id)
927 {
928         struct usb_endpoint_descriptor *ep_desc;
929         struct btusb_data *data;
930         struct hci_dev *hdev;
931         int i, err;
932
933         BT_DBG("intf %p id %p", intf, id);
934
935         /* interface numbers are hardcoded in the spec */
936         if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
937                 return -ENODEV;
938
939         if (!id->driver_info) {
940                 const struct usb_device_id *match;
941                 match = usb_match_id(intf, blacklist_table);
942                 if (match)
943                         id = match;
944         }
945
946         if (id->driver_info == BTUSB_IGNORE)
947                 return -ENODEV;
948
949         if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
950                 return -ENODEV;
951
952         if (ignore_csr && id->driver_info & BTUSB_CSR)
953                 return -ENODEV;
954
955         if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
956                 return -ENODEV;
957
958         if (id->driver_info & BTUSB_ATH3012) {
959                 struct usb_device *udev = interface_to_usbdev(intf);
960
961                 /* Old firmware would otherwise let ath3k driver load
962                  * patch and sysconfig files */
963                 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
964                         return -ENODEV;
965         }
966
967         data = kzalloc(sizeof(*data), GFP_KERNEL);
968         if (!data)
969                 return -ENOMEM;
970
971         for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
972                 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
973
974                 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
975                         data->intr_ep = ep_desc;
976                         continue;
977                 }
978
979                 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
980                         data->bulk_tx_ep = ep_desc;
981                         continue;
982                 }
983
984                 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
985                         data->bulk_rx_ep = ep_desc;
986                         continue;
987                 }
988         }
989
990         if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
991                 kfree(data);
992                 return -ENODEV;
993         }
994
995         data->cmdreq_type = USB_TYPE_CLASS;
996
997         data->udev = interface_to_usbdev(intf);
998         data->intf = intf;
999
1000         spin_lock_init(&data->lock);
1001
1002         INIT_WORK(&data->work, btusb_work);
1003         INIT_WORK(&data->waker, btusb_waker);
1004         spin_lock_init(&data->txlock);
1005
1006         init_usb_anchor(&data->tx_anchor);
1007         init_usb_anchor(&data->intr_anchor);
1008         init_usb_anchor(&data->bulk_anchor);
1009         init_usb_anchor(&data->isoc_anchor);
1010         init_usb_anchor(&data->deferred);
1011
1012         hdev = hci_alloc_dev();
1013         if (!hdev) {
1014                 kfree(data);
1015                 return -ENOMEM;
1016         }
1017
1018         hdev->bus = HCI_USB;
1019         hdev->driver_data = data;
1020
1021         data->hdev = hdev;
1022
1023         SET_HCIDEV_DEV(hdev, &intf->dev);
1024
1025         hdev->open     = btusb_open;
1026         hdev->close    = btusb_close;
1027         hdev->flush    = btusb_flush;
1028         hdev->send     = btusb_send_frame;
1029         hdev->destruct = btusb_destruct;
1030         hdev->notify   = btusb_notify;
1031
1032         hdev->owner = THIS_MODULE;
1033
1034         /* Interface numbers are hardcoded in the specification */
1035         data->isoc = usb_ifnum_to_if(data->udev, 1);
1036
1037         if (!reset)
1038                 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1039
1040         if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
1041                 if (!disable_scofix)
1042                         set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1043         }
1044
1045         if (id->driver_info & BTUSB_BROKEN_ISOC)
1046                 data->isoc = NULL;
1047
1048         if (id->driver_info & BTUSB_DIGIANSWER) {
1049                 data->cmdreq_type = USB_TYPE_VENDOR;
1050                 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1051         }
1052
1053         if (id->driver_info & BTUSB_CSR) {
1054                 struct usb_device *udev = data->udev;
1055
1056                 /* Old firmware would otherwise execute USB reset */
1057                 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
1058                         set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1059         }
1060
1061         if (id->driver_info & BTUSB_SNIFFER) {
1062                 struct usb_device *udev = data->udev;
1063
1064                 /* New sniffer firmware has crippled HCI interface */
1065                 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1066                         set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
1067
1068                 data->isoc = NULL;
1069         }
1070
1071         if (id->driver_info & BTUSB_BCM92035) {
1072                 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1073                 struct sk_buff *skb;
1074
1075                 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1076                 if (skb) {
1077                         memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1078                         skb_queue_tail(&hdev->driver_init, skb);
1079                 }
1080         }
1081
1082         if (data->isoc) {
1083                 err = usb_driver_claim_interface(&btusb_driver,
1084                                                         data->isoc, data);
1085                 if (err < 0) {
1086                         hci_free_dev(hdev);
1087                         kfree(data);
1088                         return err;
1089                 }
1090         }
1091
1092         err = hci_register_dev(hdev);
1093         if (err < 0) {
1094                 hci_free_dev(hdev);
1095                 kfree(data);
1096                 return err;
1097         }
1098
1099         usb_set_intfdata(intf, data);
1100
1101         return 0;
1102 }
1103
1104 static void btusb_disconnect(struct usb_interface *intf)
1105 {
1106         struct btusb_data *data = usb_get_intfdata(intf);
1107         struct hci_dev *hdev;
1108
1109         BT_DBG("intf %p", intf);
1110
1111         if (!data)
1112                 return;
1113
1114         hdev = data->hdev;
1115
1116         __hci_dev_hold(hdev);
1117
1118         usb_set_intfdata(data->intf, NULL);
1119
1120         if (data->isoc)
1121                 usb_set_intfdata(data->isoc, NULL);
1122
1123         hci_unregister_dev(hdev);
1124
1125         if (intf == data->isoc)
1126                 usb_driver_release_interface(&btusb_driver, data->intf);
1127         else if (data->isoc)
1128                 usb_driver_release_interface(&btusb_driver, data->isoc);
1129
1130         __hci_dev_put(hdev);
1131
1132         hci_free_dev(hdev);
1133 }
1134
1135 #ifdef CONFIG_PM
1136 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1137 {
1138         struct btusb_data *data = usb_get_intfdata(intf);
1139
1140         BT_DBG("intf %p", intf);
1141
1142         if (data->suspend_count++)
1143                 return 0;
1144
1145         spin_lock_irq(&data->txlock);
1146         if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
1147                 set_bit(BTUSB_SUSPENDING, &data->flags);
1148                 spin_unlock_irq(&data->txlock);
1149         } else {
1150                 spin_unlock_irq(&data->txlock);
1151                 data->suspend_count--;
1152                 return -EBUSY;
1153         }
1154
1155         cancel_work_sync(&data->work);
1156
1157         btusb_stop_traffic(data);
1158         usb_kill_anchored_urbs(&data->tx_anchor);
1159
1160         return 0;
1161 }
1162
1163 static void play_deferred(struct btusb_data *data)
1164 {
1165         struct urb *urb;
1166         int err;
1167
1168         while ((urb = usb_get_from_anchor(&data->deferred))) {
1169                 err = usb_submit_urb(urb, GFP_ATOMIC);
1170                 if (err < 0)
1171                         break;
1172
1173                 data->tx_in_flight++;
1174         }
1175         usb_scuttle_anchored_urbs(&data->deferred);
1176 }
1177
1178 static int btusb_resume(struct usb_interface *intf)
1179 {
1180         struct btusb_data *data = usb_get_intfdata(intf);
1181         struct hci_dev *hdev = data->hdev;
1182         int err = 0;
1183
1184         BT_DBG("intf %p", intf);
1185
1186         if (--data->suspend_count)
1187                 return 0;
1188
1189         if (!test_bit(HCI_RUNNING, &hdev->flags))
1190                 goto done;
1191
1192         if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1193                 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1194                 if (err < 0) {
1195                         clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1196                         goto failed;
1197                 }
1198         }
1199
1200         if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
1201                 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1202                 if (err < 0) {
1203                         clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1204                         goto failed;
1205                 }
1206
1207                 btusb_submit_bulk_urb(hdev, GFP_NOIO);
1208         }
1209
1210         if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1211                 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1212                         clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1213                 else
1214                         btusb_submit_isoc_urb(hdev, GFP_NOIO);
1215         }
1216
1217         spin_lock_irq(&data->txlock);
1218         play_deferred(data);
1219         clear_bit(BTUSB_SUSPENDING, &data->flags);
1220         spin_unlock_irq(&data->txlock);
1221         schedule_work(&data->work);
1222
1223         return 0;
1224
1225 failed:
1226         usb_scuttle_anchored_urbs(&data->deferred);
1227 done:
1228         spin_lock_irq(&data->txlock);
1229         clear_bit(BTUSB_SUSPENDING, &data->flags);
1230         spin_unlock_irq(&data->txlock);
1231
1232         return err;
1233 }
1234 #endif
1235
1236 static struct usb_driver btusb_driver = {
1237         .name           = "btusb",
1238         .probe          = btusb_probe,
1239         .disconnect     = btusb_disconnect,
1240 #ifdef CONFIG_PM
1241         .suspend        = btusb_suspend,
1242         .resume         = btusb_resume,
1243 #endif
1244         .id_table       = btusb_table,
1245         .supports_autosuspend = 1,
1246 };
1247
1248 static int __init btusb_init(void)
1249 {
1250         BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
1251
1252         return usb_register(&btusb_driver);
1253 }
1254
1255 static void __exit btusb_exit(void)
1256 {
1257         usb_deregister(&btusb_driver);
1258 }
1259
1260 module_init(btusb_init);
1261 module_exit(btusb_exit);
1262
1263 module_param(ignore_dga, bool, 0644);
1264 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1265
1266 module_param(ignore_csr, bool, 0644);
1267 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1268
1269 module_param(ignore_sniffer, bool, 0644);
1270 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1271
1272 module_param(disable_scofix, bool, 0644);
1273 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1274
1275 module_param(force_scofix, bool, 0644);
1276 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1277
1278 module_param(reset, bool, 0644);
1279 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1280
1281 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1282 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1283 MODULE_VERSION(VERSION);
1284 MODULE_LICENSE("GPL");