3 * Generic Bluetooth USB driver
5 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
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.
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.
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
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>
33 #include <linux/usb.h>
35 #include <net/bluetooth/bluetooth.h>
36 #include <net/bluetooth/hci_core.h>
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;
48 static struct usb_driver btusb_driver;
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
59 static struct usb_device_id btusb_table[] = {
60 /* Generic Bluetooth USB device */
61 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
63 /* Apple-specific (Broadcom) devices */
64 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
66 /* Broadcom SoftSailing reporting vendor specific */
67 { USB_DEVICE(0x0a5c, 0x21e1) },
69 /* Apple MacBookPro 7,1 */
70 { USB_DEVICE(0x05ac, 0x8213) },
73 { USB_DEVICE(0x05ac, 0x8215) },
75 /* Apple MacBookPro6,2 */
76 { USB_DEVICE(0x05ac, 0x8218) },
78 /* Apple MacBookAir3,1, MacBookAir3,2 */
79 { USB_DEVICE(0x05ac, 0x821b) },
81 /* Apple MacBookAir4,1 */
82 { USB_DEVICE(0x05ac, 0x821f) },
84 /* Apple MacBookPro8,2 */
85 { USB_DEVICE(0x05ac, 0x821a) },
87 /* Apple MacMini5,1 */
88 { USB_DEVICE(0x05ac, 0x8281) },
90 /* AVM BlueFRITZ! USB v2.0 */
91 { USB_DEVICE(0x057c, 0x3800) },
93 /* Bluetooth Ultraport Module from IBM */
94 { USB_DEVICE(0x04bf, 0x030a) },
96 /* ALPS Modules with non-standard id */
97 { USB_DEVICE(0x044e, 0x3001) },
98 { USB_DEVICE(0x044e, 0x3002) },
100 /* Ericsson with non-standard id */
101 { USB_DEVICE(0x0bdb, 0x1002) },
103 /* Canyon CN-BTU1 with HID interfaces */
104 { USB_DEVICE(0x0c10, 0x0000) },
106 /* Broadcom BCM20702A0 */
107 { USB_DEVICE(0x0489, 0xe042) },
108 { USB_DEVICE(0x413c, 0x8197) },
110 /* Foxconn - Hon Hai */
111 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01) },
113 /*Broadcom devices with vendor specific id */
114 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01) },
116 { } /* Terminating entry */
119 MODULE_DEVICE_TABLE(usb, btusb_table);
121 static struct usb_device_id blacklist_table[] = {
122 /* CSR BlueCore devices */
123 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
125 /* Broadcom BCM2033 without firmware */
126 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
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 },
135 /* Atheros AR9285 Malbec with sflash firmware */
136 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
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 },
148 /* Atheros AR5BBU12 with sflash firmware */
149 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
151 /* Atheros AR5BBU12 with sflash firmware */
152 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
153 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
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 },
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 },
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 },
168 /* HP laptop with Broadcom chip */
169 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
171 /* Dell laptop with Broadcom chip */
172 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
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 },
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 },
182 /* Asus WL-BTD202 device */
183 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
185 /* Kensington Bluetooth USB adapter */
186 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
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 },
192 /* CONWISE Technology based adapters with buggy SCO support */
193 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
195 /* Digianswer devices */
196 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
197 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
199 /* CSR BlueCore Bluetooth Sniffer */
200 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
202 /* Frontline ComProbe Bluetooth Sniffer */
203 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
205 { } /* Terminating entry */
208 #define BTUSB_MAX_ISOC_FRAMES 10
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
217 struct hci_dev *hdev;
218 struct usb_device *udev;
219 struct usb_interface *intf;
220 struct usb_interface *isoc;
226 struct work_struct work;
227 struct work_struct waker;
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;
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;
245 unsigned int sco_num;
250 static int inc_tx(struct btusb_data *data)
255 spin_lock_irqsave(&data->txlock, flags);
256 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
258 data->tx_in_flight++;
259 spin_unlock_irqrestore(&data->txlock, flags);
264 static void btusb_intr_complete(struct urb *urb)
266 struct hci_dev *hdev = urb->context;
267 struct btusb_data *data = hdev->driver_data;
270 BT_DBG("%s urb %p status %d count %d", hdev->name,
271 urb, urb->status, urb->actual_length);
273 if (!test_bit(HCI_RUNNING, &hdev->flags))
276 if (urb->status == 0) {
277 hdev->stat.byte_rx += urb->actual_length;
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);
287 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
290 usb_mark_last_busy(data->udev);
291 usb_anchor_urb(urb, &data->intr_anchor);
293 err = usb_submit_urb(urb, GFP_ATOMIC);
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);
304 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
306 struct btusb_data *data = hdev->driver_data;
312 BT_DBG("%s", hdev->name);
317 urb = usb_alloc_urb(0, mem_flags);
321 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
323 buf = kmalloc(size, mem_flags);
329 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
331 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
332 btusb_intr_complete, hdev,
333 data->intr_ep->bInterval);
335 urb->transfer_flags |= URB_FREE_BUFFER;
337 usb_anchor_urb(urb, &data->intr_anchor);
339 err = usb_submit_urb(urb, mem_flags);
341 BT_ERR("%s urb %p submission failed (%d)",
342 hdev->name, urb, -err);
343 usb_unanchor_urb(urb);
351 static void btusb_bulk_complete(struct urb *urb)
353 struct hci_dev *hdev = urb->context;
354 struct btusb_data *data = hdev->driver_data;
357 BT_DBG("%s urb %p status %d count %d", hdev->name,
358 urb, urb->status, urb->actual_length);
360 if (!test_bit(HCI_RUNNING, &hdev->flags))
363 if (urb->status == 0) {
364 hdev->stat.byte_rx += urb->actual_length;
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);
374 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
377 usb_anchor_urb(urb, &data->bulk_anchor);
378 usb_mark_last_busy(data->udev);
380 err = usb_submit_urb(urb, GFP_ATOMIC);
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);
391 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
393 struct btusb_data *data = hdev->driver_data;
397 int err, size = HCI_MAX_FRAME_SIZE;
399 BT_DBG("%s", hdev->name);
401 if (!data->bulk_rx_ep)
404 urb = usb_alloc_urb(0, mem_flags);
408 buf = kmalloc(size, mem_flags);
414 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
416 usb_fill_bulk_urb(urb, data->udev, pipe,
417 buf, size, btusb_bulk_complete, hdev);
419 urb->transfer_flags |= URB_FREE_BUFFER;
421 usb_mark_last_busy(data->udev);
422 usb_anchor_urb(urb, &data->bulk_anchor);
424 err = usb_submit_urb(urb, mem_flags);
426 BT_ERR("%s urb %p submission failed (%d)",
427 hdev->name, urb, -err);
428 usb_unanchor_urb(urb);
436 static void btusb_isoc_complete(struct urb *urb)
438 struct hci_dev *hdev = urb->context;
439 struct btusb_data *data = hdev->driver_data;
442 BT_DBG("%s urb %p status %d count %d", hdev->name,
443 urb, urb->status, urb->actual_length);
445 if (!test_bit(HCI_RUNNING, &hdev->flags))
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;
453 if (urb->iso_frame_desc[i].status)
456 hdev->stat.byte_rx += length;
458 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
459 urb->transfer_buffer + offset,
461 BT_ERR("%s corrupted SCO packet", hdev->name);
467 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
470 usb_anchor_urb(urb, &data->isoc_anchor);
472 err = usb_submit_urb(urb, GFP_ATOMIC);
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);
483 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
487 BT_DBG("len %d mtu %d", len, mtu);
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;
495 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
496 urb->iso_frame_desc[i].offset = offset;
497 urb->iso_frame_desc[i].length = len;
501 urb->number_of_packets = i;
504 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
506 struct btusb_data *data = hdev->driver_data;
512 BT_DBG("%s", hdev->name);
514 if (!data->isoc_rx_ep)
517 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
521 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
522 BTUSB_MAX_ISOC_FRAMES;
524 buf = kmalloc(size, mem_flags);
530 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
532 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
533 hdev, data->isoc_rx_ep->bInterval);
535 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
537 __fill_isoc_descriptor(urb, size,
538 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
540 usb_anchor_urb(urb, &data->isoc_anchor);
542 err = usb_submit_urb(urb, mem_flags);
544 BT_ERR("%s urb %p submission failed (%d)",
545 hdev->name, urb, -err);
546 usb_unanchor_urb(urb);
554 static void btusb_tx_complete(struct urb *urb)
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;
560 BT_DBG("%s urb %p status %d count %d", hdev->name,
561 urb, urb->status, urb->actual_length);
563 if (!test_bit(HCI_RUNNING, &hdev->flags))
567 hdev->stat.byte_tx += urb->transfer_buffer_length;
572 spin_lock(&data->txlock);
573 data->tx_in_flight--;
574 spin_unlock(&data->txlock);
576 kfree(urb->setup_packet);
581 static void btusb_isoc_tx_complete(struct urb *urb)
583 struct sk_buff *skb = urb->context;
584 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
586 BT_DBG("%s urb %p status %d count %d", hdev->name,
587 urb, urb->status, urb->actual_length);
589 if (!test_bit(HCI_RUNNING, &hdev->flags))
593 hdev->stat.byte_tx += urb->transfer_buffer_length;
598 kfree(urb->setup_packet);
603 static int btusb_open(struct hci_dev *hdev)
605 struct btusb_data *data = hdev->driver_data;
608 BT_DBG("%s", hdev->name);
610 err = usb_autopm_get_interface(data->intf);
614 data->intf->needs_remote_wakeup = 1;
616 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
619 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
622 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
626 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
628 usb_kill_anchored_urbs(&data->intr_anchor);
632 set_bit(BTUSB_BULK_RUNNING, &data->flags);
633 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
636 usb_autopm_put_interface(data->intf);
640 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
641 clear_bit(HCI_RUNNING, &hdev->flags);
642 usb_autopm_put_interface(data->intf);
646 static void btusb_stop_traffic(struct btusb_data *data)
648 usb_kill_anchored_urbs(&data->intr_anchor);
649 usb_kill_anchored_urbs(&data->bulk_anchor);
650 usb_kill_anchored_urbs(&data->isoc_anchor);
653 static int btusb_close(struct hci_dev *hdev)
655 struct btusb_data *data = hdev->driver_data;
658 BT_DBG("%s", hdev->name);
660 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
663 cancel_work_sync(&data->work);
664 cancel_work_sync(&data->waker);
666 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
667 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
668 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
670 btusb_stop_traffic(data);
671 err = usb_autopm_get_interface(data->intf);
675 data->intf->needs_remote_wakeup = 0;
676 usb_autopm_put_interface(data->intf);
679 usb_scuttle_anchored_urbs(&data->deferred);
683 static int btusb_flush(struct hci_dev *hdev)
685 struct btusb_data *data = hdev->driver_data;
687 BT_DBG("%s", hdev->name);
689 usb_kill_anchored_urbs(&data->tx_anchor);
694 static int btusb_send_frame(struct sk_buff *skb)
696 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
697 struct btusb_data *data = hdev->driver_data;
698 struct usb_ctrlrequest *dr;
703 BT_DBG("%s", hdev->name);
705 if (!test_bit(HCI_RUNNING, &hdev->flags))
708 switch (bt_cb(skb)->pkt_type) {
709 case HCI_COMMAND_PKT:
710 urb = usb_alloc_urb(0, GFP_ATOMIC);
714 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
720 dr->bRequestType = data->cmdreq_type;
724 dr->wLength = __cpu_to_le16(skb->len);
726 pipe = usb_sndctrlpipe(data->udev, 0x00);
728 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
729 skb->data, skb->len, btusb_tx_complete, skb);
734 case HCI_ACLDATA_PKT:
735 if (!data->bulk_tx_ep)
738 urb = usb_alloc_urb(0, GFP_ATOMIC);
742 pipe = usb_sndbulkpipe(data->udev,
743 data->bulk_tx_ep->bEndpointAddress);
745 usb_fill_bulk_urb(urb, data->udev, pipe,
746 skb->data, skb->len, btusb_tx_complete, skb);
751 case HCI_SCODATA_PKT:
752 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
755 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
759 pipe = usb_sndisocpipe(data->udev,
760 data->isoc_tx_ep->bEndpointAddress);
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);
766 urb->transfer_flags = URB_ISO_ASAP;
768 __fill_isoc_descriptor(urb, skb->len,
769 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
780 usb_anchor_urb(urb, &data->deferred);
781 schedule_work(&data->waker);
787 usb_anchor_urb(urb, &data->tx_anchor);
789 err = usb_submit_urb(urb, GFP_ATOMIC);
791 BT_ERR("%s urb %p submission failed", hdev->name, urb);
792 kfree(urb->setup_packet);
793 usb_unanchor_urb(urb);
795 usb_mark_last_busy(data->udev);
803 static void btusb_destruct(struct hci_dev *hdev)
805 struct btusb_data *data = hdev->driver_data;
807 BT_DBG("%s", hdev->name);
812 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
814 struct btusb_data *data = hdev->driver_data;
816 BT_DBG("%s evt %d", hdev->name, evt);
818 if (hdev->conn_hash.sco_num != data->sco_num) {
819 data->sco_num = hdev->conn_hash.sco_num;
820 schedule_work(&data->work);
824 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
826 struct btusb_data *data = hdev->driver_data;
827 struct usb_interface *intf = data->isoc;
828 struct usb_endpoint_descriptor *ep_desc;
834 err = usb_set_interface(data->udev, 1, altsetting);
836 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
840 data->isoc_altsetting = altsetting;
842 data->isoc_tx_ep = NULL;
843 data->isoc_rx_ep = NULL;
845 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
846 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
848 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
849 data->isoc_tx_ep = ep_desc;
853 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
854 data->isoc_rx_ep = ep_desc;
859 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
860 BT_ERR("%s invalid SCO descriptors", hdev->name);
867 static void btusb_work(struct work_struct *work)
869 struct btusb_data *data = container_of(work, struct btusb_data, work);
870 struct hci_dev *hdev = data->hdev;
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);
877 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
878 usb_kill_anchored_urbs(&data->isoc_anchor);
882 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
884 if (data->isoc_altsetting != 2) {
885 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
886 usb_kill_anchored_urbs(&data->isoc_anchor);
888 if (__set_isoc_interface(hdev, 2) < 0)
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);
896 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
899 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
900 usb_kill_anchored_urbs(&data->isoc_anchor);
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);
908 static void btusb_waker(struct work_struct *work)
910 struct btusb_data *data = container_of(work, struct btusb_data, waker);
913 err = usb_autopm_get_interface(data->intf);
917 usb_autopm_put_interface(data->intf);
920 static int btusb_probe(struct usb_interface *intf,
921 const struct usb_device_id *id)
923 struct usb_endpoint_descriptor *ep_desc;
924 struct btusb_data *data;
925 struct hci_dev *hdev;
928 BT_DBG("intf %p id %p", intf, id);
930 /* interface numbers are hardcoded in the spec */
931 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
934 if (!id->driver_info) {
935 const struct usb_device_id *match;
936 match = usb_match_id(intf, blacklist_table);
941 if (id->driver_info == BTUSB_IGNORE)
944 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
947 if (ignore_csr && id->driver_info & BTUSB_CSR)
950 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
953 if (id->driver_info & BTUSB_ATH3012) {
954 struct usb_device *udev = interface_to_usbdev(intf);
956 /* Old firmware would otherwise let ath3k driver load
957 * patch and sysconfig files */
958 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
962 data = kzalloc(sizeof(*data), GFP_KERNEL);
966 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
967 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
969 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
970 data->intr_ep = ep_desc;
974 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
975 data->bulk_tx_ep = ep_desc;
979 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
980 data->bulk_rx_ep = ep_desc;
985 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
990 data->cmdreq_type = USB_TYPE_CLASS;
992 data->udev = interface_to_usbdev(intf);
995 spin_lock_init(&data->lock);
997 INIT_WORK(&data->work, btusb_work);
998 INIT_WORK(&data->waker, btusb_waker);
999 spin_lock_init(&data->txlock);
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);
1007 hdev = hci_alloc_dev();
1013 hdev->bus = HCI_USB;
1014 hdev->driver_data = data;
1018 SET_HCIDEV_DEV(hdev, &intf->dev);
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;
1027 hdev->owner = THIS_MODULE;
1029 /* Interface numbers are hardcoded in the specification */
1030 data->isoc = usb_ifnum_to_if(data->udev, 1);
1033 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
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);
1040 if (id->driver_info & BTUSB_BROKEN_ISOC)
1043 if (id->driver_info & BTUSB_DIGIANSWER) {
1044 data->cmdreq_type = USB_TYPE_VENDOR;
1045 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1048 if (id->driver_info & BTUSB_CSR) {
1049 struct usb_device *udev = data->udev;
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);
1056 if (id->driver_info & BTUSB_SNIFFER) {
1057 struct usb_device *udev = data->udev;
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);
1066 if (id->driver_info & BTUSB_BCM92035) {
1067 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1068 struct sk_buff *skb;
1070 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1072 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1073 skb_queue_tail(&hdev->driver_init, skb);
1078 err = usb_driver_claim_interface(&btusb_driver,
1087 err = hci_register_dev(hdev);
1094 usb_set_intfdata(intf, data);
1099 static void btusb_disconnect(struct usb_interface *intf)
1101 struct btusb_data *data = usb_get_intfdata(intf);
1102 struct hci_dev *hdev;
1104 BT_DBG("intf %p", intf);
1111 __hci_dev_hold(hdev);
1113 usb_set_intfdata(data->intf, NULL);
1116 usb_set_intfdata(data->isoc, NULL);
1118 hci_unregister_dev(hdev);
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);
1125 __hci_dev_put(hdev);
1131 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1133 struct btusb_data *data = usb_get_intfdata(intf);
1135 BT_DBG("intf %p", intf);
1137 if (data->suspend_count++)
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);
1145 spin_unlock_irq(&data->txlock);
1146 data->suspend_count--;
1150 cancel_work_sync(&data->work);
1152 btusb_stop_traffic(data);
1153 usb_kill_anchored_urbs(&data->tx_anchor);
1158 static void play_deferred(struct btusb_data *data)
1163 while ((urb = usb_get_from_anchor(&data->deferred))) {
1164 err = usb_submit_urb(urb, GFP_ATOMIC);
1168 data->tx_in_flight++;
1170 usb_scuttle_anchored_urbs(&data->deferred);
1173 static int btusb_resume(struct usb_interface *intf)
1175 struct btusb_data *data = usb_get_intfdata(intf);
1176 struct hci_dev *hdev = data->hdev;
1179 BT_DBG("intf %p", intf);
1181 if (--data->suspend_count)
1184 if (!test_bit(HCI_RUNNING, &hdev->flags))
1187 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1188 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1190 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1195 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
1196 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1198 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1202 btusb_submit_bulk_urb(hdev, GFP_NOIO);
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);
1209 btusb_submit_isoc_urb(hdev, GFP_NOIO);
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);
1221 usb_scuttle_anchored_urbs(&data->deferred);
1223 spin_lock_irq(&data->txlock);
1224 clear_bit(BTUSB_SUSPENDING, &data->flags);
1225 spin_unlock_irq(&data->txlock);
1231 static struct usb_driver btusb_driver = {
1233 .probe = btusb_probe,
1234 .disconnect = btusb_disconnect,
1236 .suspend = btusb_suspend,
1237 .resume = btusb_resume,
1239 .id_table = btusb_table,
1240 .supports_autosuspend = 1,
1243 static int __init btusb_init(void)
1245 BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
1247 return usb_register(&btusb_driver);
1250 static void __exit btusb_exit(void)
1252 usb_deregister(&btusb_driver);
1255 module_init(btusb_init);
1256 module_exit(btusb_exit);
1258 module_param(ignore_dga, bool, 0644);
1259 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1261 module_param(ignore_csr, bool, 0644);
1262 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1264 module_param(ignore_sniffer, bool, 0644);
1265 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1267 module_param(disable_scofix, bool, 0644);
1268 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1270 module_param(force_scofix, bool, 0644);
1271 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1273 module_param(reset, bool, 0644);
1274 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
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");