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