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