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