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