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