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