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(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
158 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
159 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
160 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
161 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
162 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
163 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
164 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
165 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
166 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
167 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
169 /* Atheros AR5BBU12 with sflash firmware */
170 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
172 /* Atheros AR5BBU12 with sflash firmware */
173 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
174 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
176 /* Broadcom BCM2035 */
177 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
178 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
179 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
181 /* Broadcom BCM2045 */
182 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
183 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
185 /* IBM/Lenovo ThinkPad with Broadcom chip */
186 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
187 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
189 /* HP laptop with Broadcom chip */
190 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
192 /* Dell laptop with Broadcom chip */
193 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
195 /* Dell Wireless 370 and 410 devices */
196 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
197 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
199 /* Belkin F8T012 and F8T013 devices */
200 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
201 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
203 /* Asus WL-BTD202 device */
204 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
206 /* Kensington Bluetooth USB adapter */
207 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
209 /* RTX Telecom based adapters with buggy SCO support */
210 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
211 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
213 /* CONWISE Technology based adapters with buggy SCO support */
214 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
216 /* Digianswer devices */
217 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
218 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
220 /* CSR BlueCore Bluetooth Sniffer */
221 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
223 /* Frontline ComProbe Bluetooth Sniffer */
224 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
226 { } /* Terminating entry */
229 #define BTUSB_MAX_ISOC_FRAMES 10
231 #define BTUSB_INTR_RUNNING 0
232 #define BTUSB_BULK_RUNNING 1
233 #define BTUSB_ISOC_RUNNING 2
234 #define BTUSB_SUSPENDING 3
235 #define BTUSB_DID_ISO_RESUME 4
238 struct hci_dev *hdev;
239 struct usb_device *udev;
240 struct usb_interface *intf;
241 struct usb_interface *isoc;
247 struct work_struct work;
248 struct work_struct waker;
250 struct usb_anchor tx_anchor;
251 struct usb_anchor intr_anchor;
252 struct usb_anchor bulk_anchor;
253 struct usb_anchor isoc_anchor;
254 struct usb_anchor deferred;
258 struct usb_endpoint_descriptor *intr_ep;
259 struct usb_endpoint_descriptor *bulk_tx_ep;
260 struct usb_endpoint_descriptor *bulk_rx_ep;
261 struct usb_endpoint_descriptor *isoc_tx_ep;
262 struct usb_endpoint_descriptor *isoc_rx_ep;
266 unsigned int sco_num;
271 static int inc_tx(struct btusb_data *data)
276 spin_lock_irqsave(&data->txlock, flags);
277 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
279 data->tx_in_flight++;
280 spin_unlock_irqrestore(&data->txlock, flags);
285 static void btusb_intr_complete(struct urb *urb)
287 struct hci_dev *hdev = urb->context;
288 struct btusb_data *data = hdev->driver_data;
291 BT_DBG("%s urb %p status %d count %d", hdev->name,
292 urb, urb->status, urb->actual_length);
294 if (!test_bit(HCI_RUNNING, &hdev->flags))
297 if (urb->status == 0) {
298 hdev->stat.byte_rx += urb->actual_length;
300 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
301 urb->transfer_buffer,
302 urb->actual_length) < 0) {
303 BT_ERR("%s corrupted event packet", hdev->name);
308 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
311 usb_mark_last_busy(data->udev);
312 usb_anchor_urb(urb, &data->intr_anchor);
314 err = usb_submit_urb(urb, GFP_ATOMIC);
316 /* -EPERM: urb is being killed;
317 * -ENODEV: device got disconnected */
318 if (err != -EPERM && err != -ENODEV)
319 BT_ERR("%s urb %p failed to resubmit (%d)",
320 hdev->name, urb, -err);
321 usb_unanchor_urb(urb);
325 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
327 struct btusb_data *data = hdev->driver_data;
333 BT_DBG("%s", hdev->name);
338 urb = usb_alloc_urb(0, mem_flags);
342 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
344 buf = kmalloc(size, mem_flags);
350 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
352 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
353 btusb_intr_complete, hdev,
354 data->intr_ep->bInterval);
356 urb->transfer_flags |= URB_FREE_BUFFER;
358 usb_anchor_urb(urb, &data->intr_anchor);
360 err = usb_submit_urb(urb, mem_flags);
362 BT_ERR("%s urb %p submission failed (%d)",
363 hdev->name, urb, -err);
364 usb_unanchor_urb(urb);
372 static void btusb_bulk_complete(struct urb *urb)
374 struct hci_dev *hdev = urb->context;
375 struct btusb_data *data = hdev->driver_data;
378 BT_DBG("%s urb %p status %d count %d", hdev->name,
379 urb, urb->status, urb->actual_length);
381 if (!test_bit(HCI_RUNNING, &hdev->flags))
384 if (urb->status == 0) {
385 hdev->stat.byte_rx += urb->actual_length;
387 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
388 urb->transfer_buffer,
389 urb->actual_length) < 0) {
390 BT_ERR("%s corrupted ACL packet", hdev->name);
395 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
398 usb_anchor_urb(urb, &data->bulk_anchor);
399 usb_mark_last_busy(data->udev);
401 err = usb_submit_urb(urb, GFP_ATOMIC);
403 /* -EPERM: urb is being killed;
404 * -ENODEV: device got disconnected */
405 if (err != -EPERM && err != -ENODEV)
406 BT_ERR("%s urb %p failed to resubmit (%d)",
407 hdev->name, urb, -err);
408 usb_unanchor_urb(urb);
412 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
414 struct btusb_data *data = hdev->driver_data;
418 int err, size = HCI_MAX_FRAME_SIZE;
420 BT_DBG("%s", hdev->name);
422 if (!data->bulk_rx_ep)
425 urb = usb_alloc_urb(0, mem_flags);
429 buf = kmalloc(size, mem_flags);
435 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
437 usb_fill_bulk_urb(urb, data->udev, pipe,
438 buf, size, btusb_bulk_complete, hdev);
440 urb->transfer_flags |= URB_FREE_BUFFER;
442 usb_mark_last_busy(data->udev);
443 usb_anchor_urb(urb, &data->bulk_anchor);
445 err = usb_submit_urb(urb, mem_flags);
447 BT_ERR("%s urb %p submission failed (%d)",
448 hdev->name, urb, -err);
449 usb_unanchor_urb(urb);
457 static void btusb_isoc_complete(struct urb *urb)
459 struct hci_dev *hdev = urb->context;
460 struct btusb_data *data = hdev->driver_data;
463 BT_DBG("%s urb %p status %d count %d", hdev->name,
464 urb, urb->status, urb->actual_length);
466 if (!test_bit(HCI_RUNNING, &hdev->flags))
469 if (urb->status == 0) {
470 for (i = 0; i < urb->number_of_packets; i++) {
471 unsigned int offset = urb->iso_frame_desc[i].offset;
472 unsigned int length = urb->iso_frame_desc[i].actual_length;
474 if (urb->iso_frame_desc[i].status)
477 hdev->stat.byte_rx += length;
479 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
480 urb->transfer_buffer + offset,
482 BT_ERR("%s corrupted SCO packet", hdev->name);
488 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
491 usb_anchor_urb(urb, &data->isoc_anchor);
493 err = usb_submit_urb(urb, GFP_ATOMIC);
495 /* -EPERM: urb is being killed;
496 * -ENODEV: device got disconnected */
497 if (err != -EPERM && err != -ENODEV)
498 BT_ERR("%s urb %p failed to resubmit (%d)",
499 hdev->name, urb, -err);
500 usb_unanchor_urb(urb);
504 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
508 BT_DBG("len %d mtu %d", len, mtu);
510 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
511 i++, offset += mtu, len -= mtu) {
512 urb->iso_frame_desc[i].offset = offset;
513 urb->iso_frame_desc[i].length = mtu;
516 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
517 urb->iso_frame_desc[i].offset = offset;
518 urb->iso_frame_desc[i].length = len;
522 urb->number_of_packets = i;
525 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
527 struct btusb_data *data = hdev->driver_data;
533 BT_DBG("%s", hdev->name);
535 if (!data->isoc_rx_ep)
538 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
542 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
543 BTUSB_MAX_ISOC_FRAMES;
545 buf = kmalloc(size, mem_flags);
551 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
553 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
554 hdev, data->isoc_rx_ep->bInterval);
556 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
558 __fill_isoc_descriptor(urb, size,
559 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
561 usb_anchor_urb(urb, &data->isoc_anchor);
563 err = usb_submit_urb(urb, mem_flags);
565 BT_ERR("%s urb %p submission failed (%d)",
566 hdev->name, urb, -err);
567 usb_unanchor_urb(urb);
575 static void btusb_tx_complete(struct urb *urb)
577 struct sk_buff *skb = urb->context;
578 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
579 struct btusb_data *data = hdev->driver_data;
581 BT_DBG("%s urb %p status %d count %d", hdev->name,
582 urb, urb->status, urb->actual_length);
584 if (!test_bit(HCI_RUNNING, &hdev->flags))
588 hdev->stat.byte_tx += urb->transfer_buffer_length;
593 spin_lock(&data->txlock);
594 data->tx_in_flight--;
595 spin_unlock(&data->txlock);
597 kfree(urb->setup_packet);
602 static void btusb_isoc_tx_complete(struct urb *urb)
604 struct sk_buff *skb = urb->context;
605 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
607 BT_DBG("%s urb %p status %d count %d", hdev->name,
608 urb, urb->status, urb->actual_length);
610 if (!test_bit(HCI_RUNNING, &hdev->flags))
614 hdev->stat.byte_tx += urb->transfer_buffer_length;
619 kfree(urb->setup_packet);
624 static int btusb_open(struct hci_dev *hdev)
626 struct btusb_data *data = hdev->driver_data;
629 BT_DBG("%s", hdev->name);
631 err = usb_autopm_get_interface(data->intf);
635 data->intf->needs_remote_wakeup = 1;
637 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
640 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
643 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
647 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
649 usb_kill_anchored_urbs(&data->intr_anchor);
653 set_bit(BTUSB_BULK_RUNNING, &data->flags);
654 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
657 usb_autopm_put_interface(data->intf);
661 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
662 clear_bit(HCI_RUNNING, &hdev->flags);
663 usb_autopm_put_interface(data->intf);
667 static void btusb_stop_traffic(struct btusb_data *data)
669 usb_kill_anchored_urbs(&data->intr_anchor);
670 usb_kill_anchored_urbs(&data->bulk_anchor);
671 usb_kill_anchored_urbs(&data->isoc_anchor);
674 static int btusb_close(struct hci_dev *hdev)
676 struct btusb_data *data = hdev->driver_data;
679 BT_DBG("%s", hdev->name);
681 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
684 cancel_work_sync(&data->work);
685 cancel_work_sync(&data->waker);
687 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
688 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
689 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
691 btusb_stop_traffic(data);
692 err = usb_autopm_get_interface(data->intf);
696 data->intf->needs_remote_wakeup = 0;
697 usb_autopm_put_interface(data->intf);
700 usb_scuttle_anchored_urbs(&data->deferred);
704 static int btusb_flush(struct hci_dev *hdev)
706 struct btusb_data *data = hdev->driver_data;
708 BT_DBG("%s", hdev->name);
710 usb_kill_anchored_urbs(&data->tx_anchor);
715 static int btusb_send_frame(struct sk_buff *skb)
717 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
718 struct btusb_data *data = hdev->driver_data;
719 struct usb_ctrlrequest *dr;
724 BT_DBG("%s", hdev->name);
726 if (!test_bit(HCI_RUNNING, &hdev->flags))
729 switch (bt_cb(skb)->pkt_type) {
730 case HCI_COMMAND_PKT:
731 urb = usb_alloc_urb(0, GFP_ATOMIC);
735 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
741 dr->bRequestType = data->cmdreq_type;
745 dr->wLength = __cpu_to_le16(skb->len);
747 pipe = usb_sndctrlpipe(data->udev, 0x00);
749 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
750 skb->data, skb->len, btusb_tx_complete, skb);
755 case HCI_ACLDATA_PKT:
756 if (!data->bulk_tx_ep)
759 urb = usb_alloc_urb(0, GFP_ATOMIC);
763 pipe = usb_sndbulkpipe(data->udev,
764 data->bulk_tx_ep->bEndpointAddress);
766 usb_fill_bulk_urb(urb, data->udev, pipe,
767 skb->data, skb->len, btusb_tx_complete, skb);
772 case HCI_SCODATA_PKT:
773 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
776 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
780 pipe = usb_sndisocpipe(data->udev,
781 data->isoc_tx_ep->bEndpointAddress);
783 usb_fill_int_urb(urb, data->udev, pipe,
784 skb->data, skb->len, btusb_isoc_tx_complete,
785 skb, data->isoc_tx_ep->bInterval);
787 urb->transfer_flags = URB_ISO_ASAP;
789 __fill_isoc_descriptor(urb, skb->len,
790 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
801 usb_anchor_urb(urb, &data->deferred);
802 schedule_work(&data->waker);
808 usb_anchor_urb(urb, &data->tx_anchor);
810 err = usb_submit_urb(urb, GFP_ATOMIC);
812 BT_ERR("%s urb %p submission failed", hdev->name, urb);
813 kfree(urb->setup_packet);
814 usb_unanchor_urb(urb);
816 usb_mark_last_busy(data->udev);
824 static void btusb_destruct(struct hci_dev *hdev)
826 struct btusb_data *data = hdev->driver_data;
828 BT_DBG("%s", hdev->name);
833 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
835 struct btusb_data *data = hdev->driver_data;
837 BT_DBG("%s evt %d", hdev->name, evt);
839 if (hdev->conn_hash.sco_num != data->sco_num) {
840 data->sco_num = hdev->conn_hash.sco_num;
841 schedule_work(&data->work);
845 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
847 struct btusb_data *data = hdev->driver_data;
848 struct usb_interface *intf = data->isoc;
849 struct usb_endpoint_descriptor *ep_desc;
855 err = usb_set_interface(data->udev, 1, altsetting);
857 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
861 data->isoc_altsetting = altsetting;
863 data->isoc_tx_ep = NULL;
864 data->isoc_rx_ep = NULL;
866 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
867 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
869 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
870 data->isoc_tx_ep = ep_desc;
874 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
875 data->isoc_rx_ep = ep_desc;
880 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
881 BT_ERR("%s invalid SCO descriptors", hdev->name);
888 static void btusb_work(struct work_struct *work)
890 struct btusb_data *data = container_of(work, struct btusb_data, work);
891 struct hci_dev *hdev = data->hdev;
894 if (hdev->conn_hash.sco_num > 0) {
895 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
896 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
898 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
899 usb_kill_anchored_urbs(&data->isoc_anchor);
903 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
905 if (data->isoc_altsetting != 2) {
906 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
907 usb_kill_anchored_urbs(&data->isoc_anchor);
909 if (__set_isoc_interface(hdev, 2) < 0)
913 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
914 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
915 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
917 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
920 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
921 usb_kill_anchored_urbs(&data->isoc_anchor);
923 __set_isoc_interface(hdev, 0);
924 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
925 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
929 static void btusb_waker(struct work_struct *work)
931 struct btusb_data *data = container_of(work, struct btusb_data, waker);
934 err = usb_autopm_get_interface(data->intf);
938 usb_autopm_put_interface(data->intf);
941 static int btusb_probe(struct usb_interface *intf,
942 const struct usb_device_id *id)
944 struct usb_endpoint_descriptor *ep_desc;
945 struct btusb_data *data;
946 struct hci_dev *hdev;
949 BT_DBG("intf %p id %p", intf, id);
951 /* interface numbers are hardcoded in the spec */
952 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
955 if (!id->driver_info) {
956 const struct usb_device_id *match;
957 match = usb_match_id(intf, blacklist_table);
962 if (id->driver_info == BTUSB_IGNORE)
965 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
968 if (ignore_csr && id->driver_info & BTUSB_CSR)
971 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
974 if (id->driver_info & BTUSB_ATH3012) {
975 struct usb_device *udev = interface_to_usbdev(intf);
977 /* Old firmware would otherwise let ath3k driver load
978 * patch and sysconfig files */
979 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
983 data = kzalloc(sizeof(*data), GFP_KERNEL);
987 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
988 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
990 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
991 data->intr_ep = ep_desc;
995 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
996 data->bulk_tx_ep = ep_desc;
1000 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
1001 data->bulk_rx_ep = ep_desc;
1006 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
1011 data->cmdreq_type = USB_TYPE_CLASS;
1013 data->udev = interface_to_usbdev(intf);
1016 spin_lock_init(&data->lock);
1018 INIT_WORK(&data->work, btusb_work);
1019 INIT_WORK(&data->waker, btusb_waker);
1020 spin_lock_init(&data->txlock);
1022 init_usb_anchor(&data->tx_anchor);
1023 init_usb_anchor(&data->intr_anchor);
1024 init_usb_anchor(&data->bulk_anchor);
1025 init_usb_anchor(&data->isoc_anchor);
1026 init_usb_anchor(&data->deferred);
1028 hdev = hci_alloc_dev();
1034 hdev->bus = HCI_USB;
1035 hdev->driver_data = data;
1039 SET_HCIDEV_DEV(hdev, &intf->dev);
1041 hdev->open = btusb_open;
1042 hdev->close = btusb_close;
1043 hdev->flush = btusb_flush;
1044 hdev->send = btusb_send_frame;
1045 hdev->destruct = btusb_destruct;
1046 hdev->notify = btusb_notify;
1048 hdev->owner = THIS_MODULE;
1050 /* Interface numbers are hardcoded in the specification */
1051 data->isoc = usb_ifnum_to_if(data->udev, 1);
1054 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1056 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
1057 if (!disable_scofix)
1058 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1061 if (id->driver_info & BTUSB_BROKEN_ISOC)
1064 if (id->driver_info & BTUSB_DIGIANSWER) {
1065 data->cmdreq_type = USB_TYPE_VENDOR;
1066 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1069 if (id->driver_info & BTUSB_CSR) {
1070 struct usb_device *udev = data->udev;
1072 /* Old firmware would otherwise execute USB reset */
1073 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
1074 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1077 if (id->driver_info & BTUSB_SNIFFER) {
1078 struct usb_device *udev = data->udev;
1080 /* New sniffer firmware has crippled HCI interface */
1081 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1082 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
1087 if (id->driver_info & BTUSB_BCM92035) {
1088 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1089 struct sk_buff *skb;
1091 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1093 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1094 skb_queue_tail(&hdev->driver_init, skb);
1099 err = usb_driver_claim_interface(&btusb_driver,
1108 err = hci_register_dev(hdev);
1115 usb_set_intfdata(intf, data);
1120 static void btusb_disconnect(struct usb_interface *intf)
1122 struct btusb_data *data = usb_get_intfdata(intf);
1123 struct hci_dev *hdev;
1125 BT_DBG("intf %p", intf);
1132 __hci_dev_hold(hdev);
1134 usb_set_intfdata(data->intf, NULL);
1137 usb_set_intfdata(data->isoc, NULL);
1139 hci_unregister_dev(hdev);
1141 if (intf == data->isoc)
1142 usb_driver_release_interface(&btusb_driver, data->intf);
1143 else if (data->isoc)
1144 usb_driver_release_interface(&btusb_driver, data->isoc);
1146 __hci_dev_put(hdev);
1152 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1154 struct btusb_data *data = usb_get_intfdata(intf);
1156 BT_DBG("intf %p", intf);
1158 if (data->suspend_count++)
1161 spin_lock_irq(&data->txlock);
1162 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
1163 set_bit(BTUSB_SUSPENDING, &data->flags);
1164 spin_unlock_irq(&data->txlock);
1166 spin_unlock_irq(&data->txlock);
1167 data->suspend_count--;
1171 cancel_work_sync(&data->work);
1173 btusb_stop_traffic(data);
1174 usb_kill_anchored_urbs(&data->tx_anchor);
1179 static void play_deferred(struct btusb_data *data)
1184 while ((urb = usb_get_from_anchor(&data->deferred))) {
1185 err = usb_submit_urb(urb, GFP_ATOMIC);
1189 data->tx_in_flight++;
1191 usb_scuttle_anchored_urbs(&data->deferred);
1194 static int btusb_resume(struct usb_interface *intf)
1196 struct btusb_data *data = usb_get_intfdata(intf);
1197 struct hci_dev *hdev = data->hdev;
1200 BT_DBG("intf %p", intf);
1202 if (--data->suspend_count)
1205 if (!test_bit(HCI_RUNNING, &hdev->flags))
1208 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1209 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1211 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1216 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
1217 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1219 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1223 btusb_submit_bulk_urb(hdev, GFP_NOIO);
1226 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1227 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1228 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1230 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1233 spin_lock_irq(&data->txlock);
1234 play_deferred(data);
1235 clear_bit(BTUSB_SUSPENDING, &data->flags);
1236 spin_unlock_irq(&data->txlock);
1237 schedule_work(&data->work);
1242 usb_scuttle_anchored_urbs(&data->deferred);
1244 spin_lock_irq(&data->txlock);
1245 clear_bit(BTUSB_SUSPENDING, &data->flags);
1246 spin_unlock_irq(&data->txlock);
1252 static struct usb_driver btusb_driver = {
1254 .probe = btusb_probe,
1255 .disconnect = btusb_disconnect,
1257 .suspend = btusb_suspend,
1258 .resume = btusb_resume,
1260 .id_table = btusb_table,
1261 .supports_autosuspend = 1,
1264 static int __init btusb_init(void)
1266 BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
1268 return usb_register(&btusb_driver);
1271 static void __exit btusb_exit(void)
1273 usb_deregister(&btusb_driver);
1276 module_init(btusb_init);
1277 module_exit(btusb_exit);
1279 module_param(ignore_dga, bool, 0644);
1280 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1282 module_param(ignore_csr, bool, 0644);
1283 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1285 module_param(ignore_sniffer, bool, 0644);
1286 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1288 module_param(disable_scofix, bool, 0644);
1289 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1291 module_param(force_scofix, bool, 0644);
1292 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1294 module_param(reset, bool, 0644);
1295 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1297 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1298 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1299 MODULE_VERSION(VERSION);
1300 MODULE_LICENSE("GPL");