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