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