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 /* MediaTek MT76x0E */
67 { USB_DEVICE(0x0e8d, 0x763f) },
69 /* Broadcom SoftSailing reporting vendor specific */
70 { USB_DEVICE(0x0a5c, 0x21e1) },
72 /* Apple MacBookPro 7,1 */
73 { USB_DEVICE(0x05ac, 0x8213) },
76 { USB_DEVICE(0x05ac, 0x8215) },
78 /* Apple MacBookPro6,2 */
79 { USB_DEVICE(0x05ac, 0x8218) },
81 /* Apple MacBookAir3,1, MacBookAir3,2 */
82 { USB_DEVICE(0x05ac, 0x821b) },
84 /* Apple MacBookAir4,1 */
85 { USB_DEVICE(0x05ac, 0x821f) },
87 /* Apple MacBookPro8,2 */
88 { USB_DEVICE(0x05ac, 0x821a) },
90 /* Apple MacMini5,1 */
91 { USB_DEVICE(0x05ac, 0x8281) },
93 /* AVM BlueFRITZ! USB v2.0 */
94 { USB_DEVICE(0x057c, 0x3800) },
96 /* Bluetooth Ultraport Module from IBM */
97 { USB_DEVICE(0x04bf, 0x030a) },
99 /* ALPS Modules with non-standard id */
100 { USB_DEVICE(0x044e, 0x3001) },
101 { USB_DEVICE(0x044e, 0x3002) },
103 /* Ericsson with non-standard id */
104 { USB_DEVICE(0x0bdb, 0x1002) },
106 /* Canyon CN-BTU1 with HID interfaces */
107 { USB_DEVICE(0x0c10, 0x0000) },
109 /* Broadcom BCM20702A0 */
110 { USB_DEVICE(0x0b05, 0x17b5) },
111 { USB_DEVICE(0x04ca, 0x2003) },
112 { USB_DEVICE(0x0489, 0xe042) },
113 { USB_DEVICE(0x413c, 0x8197) },
115 /* Foxconn - Hon Hai */
116 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01) },
118 /*Broadcom devices with vendor specific id */
119 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01) },
121 { } /* Terminating entry */
124 MODULE_DEVICE_TABLE(usb, btusb_table);
126 static struct usb_device_id blacklist_table[] = {
127 /* CSR BlueCore devices */
128 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
130 /* Broadcom BCM2033 without firmware */
131 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
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 },
141 /* Atheros AR9285 Malbec with sflash firmware */
142 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
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 },
164 /* Atheros AR5BBU12 with sflash firmware */
165 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
167 /* Atheros AR5BBU12 with sflash firmware */
168 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
169 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
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 },
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 },
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 },
184 /* HP laptop with Broadcom chip */
185 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
187 /* Dell laptop with Broadcom chip */
188 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
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 },
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 },
198 /* Asus WL-BTD202 device */
199 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
201 /* Kensington Bluetooth USB adapter */
202 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
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 },
208 /* CONWISE Technology based adapters with buggy SCO support */
209 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
211 /* Digianswer devices */
212 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
213 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
215 /* CSR BlueCore Bluetooth Sniffer */
216 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
218 /* Frontline ComProbe Bluetooth Sniffer */
219 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
221 { } /* Terminating entry */
224 #define BTUSB_MAX_ISOC_FRAMES 10
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
233 struct hci_dev *hdev;
234 struct usb_device *udev;
235 struct usb_interface *intf;
236 struct usb_interface *isoc;
242 struct work_struct work;
243 struct work_struct waker;
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;
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;
261 unsigned int sco_num;
266 static int inc_tx(struct btusb_data *data)
271 spin_lock_irqsave(&data->txlock, flags);
272 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
274 data->tx_in_flight++;
275 spin_unlock_irqrestore(&data->txlock, flags);
280 static void btusb_intr_complete(struct urb *urb)
282 struct hci_dev *hdev = urb->context;
283 struct btusb_data *data = hdev->driver_data;
286 BT_DBG("%s urb %p status %d count %d", hdev->name,
287 urb, urb->status, urb->actual_length);
289 if (!test_bit(HCI_RUNNING, &hdev->flags))
292 if (urb->status == 0) {
293 hdev->stat.byte_rx += urb->actual_length;
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);
303 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
306 usb_mark_last_busy(data->udev);
307 usb_anchor_urb(urb, &data->intr_anchor);
309 err = usb_submit_urb(urb, GFP_ATOMIC);
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);
320 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
322 struct btusb_data *data = hdev->driver_data;
328 BT_DBG("%s", hdev->name);
333 urb = usb_alloc_urb(0, mem_flags);
337 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
339 buf = kmalloc(size, mem_flags);
345 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
347 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
348 btusb_intr_complete, hdev,
349 data->intr_ep->bInterval);
351 urb->transfer_flags |= URB_FREE_BUFFER;
353 usb_anchor_urb(urb, &data->intr_anchor);
355 err = usb_submit_urb(urb, mem_flags);
357 BT_ERR("%s urb %p submission failed (%d)",
358 hdev->name, urb, -err);
359 usb_unanchor_urb(urb);
367 static void btusb_bulk_complete(struct urb *urb)
369 struct hci_dev *hdev = urb->context;
370 struct btusb_data *data = hdev->driver_data;
373 BT_DBG("%s urb %p status %d count %d", hdev->name,
374 urb, urb->status, urb->actual_length);
376 if (!test_bit(HCI_RUNNING, &hdev->flags))
379 if (urb->status == 0) {
380 hdev->stat.byte_rx += urb->actual_length;
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);
390 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
393 usb_anchor_urb(urb, &data->bulk_anchor);
394 usb_mark_last_busy(data->udev);
396 err = usb_submit_urb(urb, GFP_ATOMIC);
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);
407 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
409 struct btusb_data *data = hdev->driver_data;
413 int err, size = HCI_MAX_FRAME_SIZE;
415 BT_DBG("%s", hdev->name);
417 if (!data->bulk_rx_ep)
420 urb = usb_alloc_urb(0, mem_flags);
424 buf = kmalloc(size, mem_flags);
430 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
432 usb_fill_bulk_urb(urb, data->udev, pipe,
433 buf, size, btusb_bulk_complete, hdev);
435 urb->transfer_flags |= URB_FREE_BUFFER;
437 usb_mark_last_busy(data->udev);
438 usb_anchor_urb(urb, &data->bulk_anchor);
440 err = usb_submit_urb(urb, mem_flags);
442 BT_ERR("%s urb %p submission failed (%d)",
443 hdev->name, urb, -err);
444 usb_unanchor_urb(urb);
452 static void btusb_isoc_complete(struct urb *urb)
454 struct hci_dev *hdev = urb->context;
455 struct btusb_data *data = hdev->driver_data;
458 BT_DBG("%s urb %p status %d count %d", hdev->name,
459 urb, urb->status, urb->actual_length);
461 if (!test_bit(HCI_RUNNING, &hdev->flags))
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;
469 if (urb->iso_frame_desc[i].status)
472 hdev->stat.byte_rx += length;
474 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
475 urb->transfer_buffer + offset,
477 BT_ERR("%s corrupted SCO packet", hdev->name);
483 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
486 usb_anchor_urb(urb, &data->isoc_anchor);
488 err = usb_submit_urb(urb, GFP_ATOMIC);
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);
499 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
503 BT_DBG("len %d mtu %d", len, mtu);
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;
511 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
512 urb->iso_frame_desc[i].offset = offset;
513 urb->iso_frame_desc[i].length = len;
517 urb->number_of_packets = i;
520 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
522 struct btusb_data *data = hdev->driver_data;
528 BT_DBG("%s", hdev->name);
530 if (!data->isoc_rx_ep)
533 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
537 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
538 BTUSB_MAX_ISOC_FRAMES;
540 buf = kmalloc(size, mem_flags);
546 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
548 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
549 hdev, data->isoc_rx_ep->bInterval);
551 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
553 __fill_isoc_descriptor(urb, size,
554 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
556 usb_anchor_urb(urb, &data->isoc_anchor);
558 err = usb_submit_urb(urb, mem_flags);
560 BT_ERR("%s urb %p submission failed (%d)",
561 hdev->name, urb, -err);
562 usb_unanchor_urb(urb);
570 static void btusb_tx_complete(struct urb *urb)
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;
576 BT_DBG("%s urb %p status %d count %d", hdev->name,
577 urb, urb->status, urb->actual_length);
579 if (!test_bit(HCI_RUNNING, &hdev->flags))
583 hdev->stat.byte_tx += urb->transfer_buffer_length;
588 spin_lock(&data->txlock);
589 data->tx_in_flight--;
590 spin_unlock(&data->txlock);
592 kfree(urb->setup_packet);
597 static void btusb_isoc_tx_complete(struct urb *urb)
599 struct sk_buff *skb = urb->context;
600 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
602 BT_DBG("%s urb %p status %d count %d", hdev->name,
603 urb, urb->status, urb->actual_length);
605 if (!test_bit(HCI_RUNNING, &hdev->flags))
609 hdev->stat.byte_tx += urb->transfer_buffer_length;
614 kfree(urb->setup_packet);
619 static int btusb_open(struct hci_dev *hdev)
621 struct btusb_data *data = hdev->driver_data;
624 BT_DBG("%s", hdev->name);
626 err = usb_autopm_get_interface(data->intf);
630 data->intf->needs_remote_wakeup = 1;
632 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
635 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
638 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
642 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
644 usb_kill_anchored_urbs(&data->intr_anchor);
648 set_bit(BTUSB_BULK_RUNNING, &data->flags);
649 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
652 usb_autopm_put_interface(data->intf);
656 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
657 clear_bit(HCI_RUNNING, &hdev->flags);
658 usb_autopm_put_interface(data->intf);
662 static void btusb_stop_traffic(struct btusb_data *data)
664 usb_kill_anchored_urbs(&data->intr_anchor);
665 usb_kill_anchored_urbs(&data->bulk_anchor);
666 usb_kill_anchored_urbs(&data->isoc_anchor);
669 static int btusb_close(struct hci_dev *hdev)
671 struct btusb_data *data = hdev->driver_data;
674 BT_DBG("%s", hdev->name);
676 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
679 cancel_work_sync(&data->work);
680 cancel_work_sync(&data->waker);
682 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
683 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
684 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
686 btusb_stop_traffic(data);
687 err = usb_autopm_get_interface(data->intf);
691 data->intf->needs_remote_wakeup = 0;
692 usb_autopm_put_interface(data->intf);
695 usb_scuttle_anchored_urbs(&data->deferred);
699 static int btusb_flush(struct hci_dev *hdev)
701 struct btusb_data *data = hdev->driver_data;
703 BT_DBG("%s", hdev->name);
705 usb_kill_anchored_urbs(&data->tx_anchor);
710 static int btusb_send_frame(struct sk_buff *skb)
712 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
713 struct btusb_data *data = hdev->driver_data;
714 struct usb_ctrlrequest *dr;
719 BT_DBG("%s", hdev->name);
721 if (!test_bit(HCI_RUNNING, &hdev->flags))
724 switch (bt_cb(skb)->pkt_type) {
725 case HCI_COMMAND_PKT:
726 urb = usb_alloc_urb(0, GFP_ATOMIC);
730 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
736 dr->bRequestType = data->cmdreq_type;
740 dr->wLength = __cpu_to_le16(skb->len);
742 pipe = usb_sndctrlpipe(data->udev, 0x00);
744 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
745 skb->data, skb->len, btusb_tx_complete, skb);
750 case HCI_ACLDATA_PKT:
751 if (!data->bulk_tx_ep)
754 urb = usb_alloc_urb(0, GFP_ATOMIC);
758 pipe = usb_sndbulkpipe(data->udev,
759 data->bulk_tx_ep->bEndpointAddress);
761 usb_fill_bulk_urb(urb, data->udev, pipe,
762 skb->data, skb->len, btusb_tx_complete, skb);
767 case HCI_SCODATA_PKT:
768 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
771 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
775 pipe = usb_sndisocpipe(data->udev,
776 data->isoc_tx_ep->bEndpointAddress);
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);
782 urb->transfer_flags = URB_ISO_ASAP;
784 __fill_isoc_descriptor(urb, skb->len,
785 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
796 usb_anchor_urb(urb, &data->deferred);
797 schedule_work(&data->waker);
803 usb_anchor_urb(urb, &data->tx_anchor);
805 err = usb_submit_urb(urb, GFP_ATOMIC);
807 BT_ERR("%s urb %p submission failed", hdev->name, urb);
808 kfree(urb->setup_packet);
809 usb_unanchor_urb(urb);
811 usb_mark_last_busy(data->udev);
819 static void btusb_destruct(struct hci_dev *hdev)
821 struct btusb_data *data = hdev->driver_data;
823 BT_DBG("%s", hdev->name);
828 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
830 struct btusb_data *data = hdev->driver_data;
832 BT_DBG("%s evt %d", hdev->name, evt);
834 if (hdev->conn_hash.sco_num != data->sco_num) {
835 data->sco_num = hdev->conn_hash.sco_num;
836 schedule_work(&data->work);
840 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
842 struct btusb_data *data = hdev->driver_data;
843 struct usb_interface *intf = data->isoc;
844 struct usb_endpoint_descriptor *ep_desc;
850 err = usb_set_interface(data->udev, 1, altsetting);
852 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
856 data->isoc_altsetting = altsetting;
858 data->isoc_tx_ep = NULL;
859 data->isoc_rx_ep = NULL;
861 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
862 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
864 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
865 data->isoc_tx_ep = ep_desc;
869 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
870 data->isoc_rx_ep = ep_desc;
875 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
876 BT_ERR("%s invalid SCO descriptors", hdev->name);
883 static void btusb_work(struct work_struct *work)
885 struct btusb_data *data = container_of(work, struct btusb_data, work);
886 struct hci_dev *hdev = data->hdev;
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);
893 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
894 usb_kill_anchored_urbs(&data->isoc_anchor);
898 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
900 if (data->isoc_altsetting != 2) {
901 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
902 usb_kill_anchored_urbs(&data->isoc_anchor);
904 if (__set_isoc_interface(hdev, 2) < 0)
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);
912 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
915 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
916 usb_kill_anchored_urbs(&data->isoc_anchor);
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);
924 static void btusb_waker(struct work_struct *work)
926 struct btusb_data *data = container_of(work, struct btusb_data, waker);
929 err = usb_autopm_get_interface(data->intf);
933 usb_autopm_put_interface(data->intf);
936 static int btusb_probe(struct usb_interface *intf,
937 const struct usb_device_id *id)
939 struct usb_endpoint_descriptor *ep_desc;
940 struct btusb_data *data;
941 struct hci_dev *hdev;
944 BT_DBG("intf %p id %p", intf, id);
946 /* interface numbers are hardcoded in the spec */
947 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
950 if (!id->driver_info) {
951 const struct usb_device_id *match;
952 match = usb_match_id(intf, blacklist_table);
957 if (id->driver_info == BTUSB_IGNORE)
960 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
963 if (ignore_csr && id->driver_info & BTUSB_CSR)
966 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
969 if (id->driver_info & BTUSB_ATH3012) {
970 struct usb_device *udev = interface_to_usbdev(intf);
972 /* Old firmware would otherwise let ath3k driver load
973 * patch and sysconfig files */
974 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
978 data = kzalloc(sizeof(*data), GFP_KERNEL);
982 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
983 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
985 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
986 data->intr_ep = ep_desc;
990 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
991 data->bulk_tx_ep = ep_desc;
995 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
996 data->bulk_rx_ep = ep_desc;
1001 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
1006 data->cmdreq_type = USB_TYPE_CLASS;
1008 data->udev = interface_to_usbdev(intf);
1011 spin_lock_init(&data->lock);
1013 INIT_WORK(&data->work, btusb_work);
1014 INIT_WORK(&data->waker, btusb_waker);
1015 spin_lock_init(&data->txlock);
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);
1023 hdev = hci_alloc_dev();
1029 hdev->bus = HCI_USB;
1030 hdev->driver_data = data;
1034 SET_HCIDEV_DEV(hdev, &intf->dev);
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;
1043 hdev->owner = THIS_MODULE;
1045 /* Interface numbers are hardcoded in the specification */
1046 data->isoc = usb_ifnum_to_if(data->udev, 1);
1049 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
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);
1056 if (id->driver_info & BTUSB_BROKEN_ISOC)
1059 if (id->driver_info & BTUSB_DIGIANSWER) {
1060 data->cmdreq_type = USB_TYPE_VENDOR;
1061 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1064 if (id->driver_info & BTUSB_CSR) {
1065 struct usb_device *udev = data->udev;
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);
1072 if (id->driver_info & BTUSB_SNIFFER) {
1073 struct usb_device *udev = data->udev;
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);
1082 if (id->driver_info & BTUSB_BCM92035) {
1083 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1084 struct sk_buff *skb;
1086 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1088 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1089 skb_queue_tail(&hdev->driver_init, skb);
1094 err = usb_driver_claim_interface(&btusb_driver,
1103 err = hci_register_dev(hdev);
1110 usb_set_intfdata(intf, data);
1115 static void btusb_disconnect(struct usb_interface *intf)
1117 struct btusb_data *data = usb_get_intfdata(intf);
1118 struct hci_dev *hdev;
1120 BT_DBG("intf %p", intf);
1127 __hci_dev_hold(hdev);
1129 usb_set_intfdata(data->intf, NULL);
1132 usb_set_intfdata(data->isoc, NULL);
1134 hci_unregister_dev(hdev);
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);
1141 __hci_dev_put(hdev);
1147 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1149 struct btusb_data *data = usb_get_intfdata(intf);
1151 BT_DBG("intf %p", intf);
1153 if (data->suspend_count++)
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);
1161 spin_unlock_irq(&data->txlock);
1162 data->suspend_count--;
1166 cancel_work_sync(&data->work);
1168 btusb_stop_traffic(data);
1169 usb_kill_anchored_urbs(&data->tx_anchor);
1174 static void play_deferred(struct btusb_data *data)
1179 while ((urb = usb_get_from_anchor(&data->deferred))) {
1180 err = usb_submit_urb(urb, GFP_ATOMIC);
1184 data->tx_in_flight++;
1186 usb_scuttle_anchored_urbs(&data->deferred);
1189 static int btusb_resume(struct usb_interface *intf)
1191 struct btusb_data *data = usb_get_intfdata(intf);
1192 struct hci_dev *hdev = data->hdev;
1195 BT_DBG("intf %p", intf);
1197 if (--data->suspend_count)
1200 if (!test_bit(HCI_RUNNING, &hdev->flags))
1203 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1204 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1206 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1211 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
1212 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1214 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1218 btusb_submit_bulk_urb(hdev, GFP_NOIO);
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);
1225 btusb_submit_isoc_urb(hdev, GFP_NOIO);
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);
1237 usb_scuttle_anchored_urbs(&data->deferred);
1239 spin_lock_irq(&data->txlock);
1240 clear_bit(BTUSB_SUSPENDING, &data->flags);
1241 spin_unlock_irq(&data->txlock);
1247 static struct usb_driver btusb_driver = {
1249 .probe = btusb_probe,
1250 .disconnect = btusb_disconnect,
1252 .suspend = btusb_suspend,
1253 .resume = btusb_resume,
1255 .id_table = btusb_table,
1256 .supports_autosuspend = 1,
1259 static int __init btusb_init(void)
1261 BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
1263 return usb_register(&btusb_driver);
1266 static void __exit btusb_exit(void)
1268 usb_deregister(&btusb_driver);
1271 module_init(btusb_init);
1272 module_exit(btusb_exit);
1274 module_param(ignore_dga, bool, 0644);
1275 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1277 module_param(ignore_csr, bool, 0644);
1278 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1280 module_param(ignore_sniffer, bool, 0644);
1281 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1283 module_param(disable_scofix, bool, 0644);
1284 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1286 module_param(force_scofix, bool, 0644);
1287 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1289 module_param(reset, bool, 0644);
1290 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
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");