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