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