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