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(0x0b05, 0x17cb) },
112 { USB_DEVICE(0x04ca, 0x2003) },
113 { USB_DEVICE(0x0489, 0xe042) },
114 { USB_DEVICE(0x413c, 0x8197) },
116 /* Foxconn - Hon Hai */
117 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01) },
119 /*Broadcom devices with vendor specific id */
120 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01) },
122 /* Belkin F8065bf - Broadcom based */
123 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01) },
125 { } /* Terminating entry */
128 MODULE_DEVICE_TABLE(usb, btusb_table);
130 static struct usb_device_id blacklist_table[] = {
131 /* CSR BlueCore devices */
132 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
134 /* Broadcom BCM2033 without firmware */
135 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
137 /* Atheros 3011 with sflash firmware */
138 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
139 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
140 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
141 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
142 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
143 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
145 /* Atheros AR9285 Malbec with sflash firmware */
146 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
148 /* Atheros 3012 with sflash firmware */
149 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
150 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
151 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
152 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
153 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
154 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
155 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
156 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
157 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
158 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
159 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
160 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
161 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
162 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
163 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
164 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
165 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
166 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
167 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
168 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
169 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
170 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
171 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
172 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
173 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
174 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
175 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
176 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
178 /* Atheros AR5BBU12 with sflash firmware */
179 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
181 /* Atheros AR5BBU12 with sflash firmware */
182 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
183 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
185 /* Broadcom BCM2035 */
186 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
187 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
188 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
190 /* Broadcom BCM2045 */
191 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
192 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
194 /* IBM/Lenovo ThinkPad with Broadcom chip */
195 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
196 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
198 /* HP laptop with Broadcom chip */
199 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
201 /* Dell laptop with Broadcom chip */
202 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
204 /* Dell Wireless 370 and 410 devices */
205 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
206 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
208 /* Belkin F8T012 and F8T013 devices */
209 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
210 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
212 /* Asus WL-BTD202 device */
213 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
215 /* Kensington Bluetooth USB adapter */
216 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
218 /* RTX Telecom based adapters with buggy SCO support */
219 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
220 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
222 /* CONWISE Technology based adapters with buggy SCO support */
223 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
225 /* Digianswer devices */
226 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
227 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
229 /* CSR BlueCore Bluetooth Sniffer */
230 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
232 /* Frontline ComProbe Bluetooth Sniffer */
233 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
235 { } /* Terminating entry */
238 #define BTUSB_MAX_ISOC_FRAMES 10
240 #define BTUSB_INTR_RUNNING 0
241 #define BTUSB_BULK_RUNNING 1
242 #define BTUSB_ISOC_RUNNING 2
243 #define BTUSB_SUSPENDING 3
244 #define BTUSB_DID_ISO_RESUME 4
247 struct hci_dev *hdev;
248 struct usb_device *udev;
249 struct usb_interface *intf;
250 struct usb_interface *isoc;
256 struct work_struct work;
257 struct work_struct waker;
259 struct usb_anchor tx_anchor;
260 struct usb_anchor intr_anchor;
261 struct usb_anchor bulk_anchor;
262 struct usb_anchor isoc_anchor;
263 struct usb_anchor deferred;
267 struct usb_endpoint_descriptor *intr_ep;
268 struct usb_endpoint_descriptor *bulk_tx_ep;
269 struct usb_endpoint_descriptor *bulk_rx_ep;
270 struct usb_endpoint_descriptor *isoc_tx_ep;
271 struct usb_endpoint_descriptor *isoc_rx_ep;
275 unsigned int sco_num;
280 static int inc_tx(struct btusb_data *data)
285 spin_lock_irqsave(&data->txlock, flags);
286 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
288 data->tx_in_flight++;
289 spin_unlock_irqrestore(&data->txlock, flags);
294 static void btusb_intr_complete(struct urb *urb)
296 struct hci_dev *hdev = urb->context;
297 struct btusb_data *data = hdev->driver_data;
300 BT_DBG("%s urb %p status %d count %d", hdev->name,
301 urb, urb->status, urb->actual_length);
303 if (!test_bit(HCI_RUNNING, &hdev->flags))
306 if (urb->status == 0) {
307 hdev->stat.byte_rx += urb->actual_length;
309 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
310 urb->transfer_buffer,
311 urb->actual_length) < 0) {
312 BT_ERR("%s corrupted event packet", hdev->name);
315 } else if (urb->status == -ENOENT) {
316 /* Avoid suspend failed when usb_kill_urb */
320 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
323 usb_mark_last_busy(data->udev);
324 usb_anchor_urb(urb, &data->intr_anchor);
326 err = usb_submit_urb(urb, GFP_ATOMIC);
328 /* -EPERM: urb is being killed;
329 * -ENODEV: device got disconnected */
330 if (err != -EPERM && err != -ENODEV)
331 BT_ERR("%s urb %p failed to resubmit (%d)",
332 hdev->name, urb, -err);
333 usb_unanchor_urb(urb);
337 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
339 struct btusb_data *data = hdev->driver_data;
345 BT_DBG("%s", hdev->name);
350 urb = usb_alloc_urb(0, mem_flags);
354 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
356 buf = kmalloc(size, mem_flags);
362 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
364 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
365 btusb_intr_complete, hdev,
366 data->intr_ep->bInterval);
368 urb->transfer_flags |= URB_FREE_BUFFER;
370 usb_anchor_urb(urb, &data->intr_anchor);
372 err = usb_submit_urb(urb, mem_flags);
374 BT_ERR("%s urb %p submission failed (%d)",
375 hdev->name, urb, -err);
376 usb_unanchor_urb(urb);
384 static void btusb_bulk_complete(struct urb *urb)
386 struct hci_dev *hdev = urb->context;
387 struct btusb_data *data = hdev->driver_data;
390 BT_DBG("%s urb %p status %d count %d", hdev->name,
391 urb, urb->status, urb->actual_length);
393 if (!test_bit(HCI_RUNNING, &hdev->flags))
396 if (urb->status == 0) {
397 hdev->stat.byte_rx += urb->actual_length;
399 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
400 urb->transfer_buffer,
401 urb->actual_length) < 0) {
402 BT_ERR("%s corrupted ACL packet", hdev->name);
405 } else if (urb->status == -ENOENT) {
406 /* Avoid suspend failed when usb_kill_urb */
410 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
413 usb_anchor_urb(urb, &data->bulk_anchor);
414 usb_mark_last_busy(data->udev);
416 err = usb_submit_urb(urb, GFP_ATOMIC);
418 /* -EPERM: urb is being killed;
419 * -ENODEV: device got disconnected */
420 if (err != -EPERM && err != -ENODEV)
421 BT_ERR("%s urb %p failed to resubmit (%d)",
422 hdev->name, urb, -err);
423 usb_unanchor_urb(urb);
427 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
429 struct btusb_data *data = hdev->driver_data;
433 int err, size = HCI_MAX_FRAME_SIZE;
435 BT_DBG("%s", hdev->name);
437 if (!data->bulk_rx_ep)
440 urb = usb_alloc_urb(0, mem_flags);
444 buf = kmalloc(size, mem_flags);
450 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
452 usb_fill_bulk_urb(urb, data->udev, pipe,
453 buf, size, btusb_bulk_complete, hdev);
455 urb->transfer_flags |= URB_FREE_BUFFER;
457 usb_mark_last_busy(data->udev);
458 usb_anchor_urb(urb, &data->bulk_anchor);
460 err = usb_submit_urb(urb, mem_flags);
462 BT_ERR("%s urb %p submission failed (%d)",
463 hdev->name, urb, -err);
464 usb_unanchor_urb(urb);
472 static void btusb_isoc_complete(struct urb *urb)
474 struct hci_dev *hdev = urb->context;
475 struct btusb_data *data = hdev->driver_data;
478 BT_DBG("%s urb %p status %d count %d", hdev->name,
479 urb, urb->status, urb->actual_length);
481 if (!test_bit(HCI_RUNNING, &hdev->flags))
484 if (urb->status == 0) {
485 for (i = 0; i < urb->number_of_packets; i++) {
486 unsigned int offset = urb->iso_frame_desc[i].offset;
487 unsigned int length = urb->iso_frame_desc[i].actual_length;
489 if (urb->iso_frame_desc[i].status)
492 hdev->stat.byte_rx += length;
494 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
495 urb->transfer_buffer + offset,
497 BT_ERR("%s corrupted SCO packet", hdev->name);
501 } else if (urb->status == -ENOENT) {
502 /* Avoid suspend failed when usb_kill_urb */
506 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
509 usb_anchor_urb(urb, &data->isoc_anchor);
511 err = usb_submit_urb(urb, GFP_ATOMIC);
513 /* -EPERM: urb is being killed;
514 * -ENODEV: device got disconnected */
515 if (err != -EPERM && err != -ENODEV)
516 BT_ERR("%s urb %p failed to resubmit (%d)",
517 hdev->name, urb, -err);
518 usb_unanchor_urb(urb);
522 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
526 BT_DBG("len %d mtu %d", len, mtu);
528 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
529 i++, offset += mtu, len -= mtu) {
530 urb->iso_frame_desc[i].offset = offset;
531 urb->iso_frame_desc[i].length = mtu;
534 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
535 urb->iso_frame_desc[i].offset = offset;
536 urb->iso_frame_desc[i].length = len;
540 urb->number_of_packets = i;
543 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
545 struct btusb_data *data = hdev->driver_data;
551 BT_DBG("%s", hdev->name);
553 if (!data->isoc_rx_ep)
556 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
560 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
561 BTUSB_MAX_ISOC_FRAMES;
563 buf = kmalloc(size, mem_flags);
569 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
571 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
572 hdev, data->isoc_rx_ep->bInterval);
574 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
576 __fill_isoc_descriptor(urb, size,
577 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
579 usb_anchor_urb(urb, &data->isoc_anchor);
581 err = usb_submit_urb(urb, mem_flags);
583 BT_ERR("%s urb %p submission failed (%d)",
584 hdev->name, urb, -err);
585 usb_unanchor_urb(urb);
593 static void btusb_tx_complete(struct urb *urb)
595 struct sk_buff *skb = urb->context;
596 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
597 struct btusb_data *data = hdev->driver_data;
599 BT_DBG("%s urb %p status %d count %d", hdev->name,
600 urb, urb->status, urb->actual_length);
602 if (!test_bit(HCI_RUNNING, &hdev->flags))
606 hdev->stat.byte_tx += urb->transfer_buffer_length;
611 spin_lock(&data->txlock);
612 data->tx_in_flight--;
613 spin_unlock(&data->txlock);
615 kfree(urb->setup_packet);
620 static void btusb_isoc_tx_complete(struct urb *urb)
622 struct sk_buff *skb = urb->context;
623 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
625 BT_DBG("%s urb %p status %d count %d", hdev->name,
626 urb, urb->status, urb->actual_length);
628 if (!test_bit(HCI_RUNNING, &hdev->flags))
632 hdev->stat.byte_tx += urb->transfer_buffer_length;
637 kfree(urb->setup_packet);
642 static int btusb_open(struct hci_dev *hdev)
644 struct btusb_data *data = hdev->driver_data;
647 BT_DBG("%s", hdev->name);
649 err = usb_autopm_get_interface(data->intf);
653 data->intf->needs_remote_wakeup = 1;
655 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
658 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
661 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
665 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
667 usb_kill_anchored_urbs(&data->intr_anchor);
671 set_bit(BTUSB_BULK_RUNNING, &data->flags);
672 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
675 usb_autopm_put_interface(data->intf);
679 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
680 clear_bit(HCI_RUNNING, &hdev->flags);
681 usb_autopm_put_interface(data->intf);
685 static void btusb_stop_traffic(struct btusb_data *data)
687 usb_kill_anchored_urbs(&data->intr_anchor);
688 usb_kill_anchored_urbs(&data->bulk_anchor);
689 usb_kill_anchored_urbs(&data->isoc_anchor);
692 static int btusb_close(struct hci_dev *hdev)
694 struct btusb_data *data = hdev->driver_data;
697 BT_DBG("%s", hdev->name);
699 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
702 cancel_work_sync(&data->work);
703 cancel_work_sync(&data->waker);
705 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
706 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
707 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
709 btusb_stop_traffic(data);
710 err = usb_autopm_get_interface(data->intf);
714 data->intf->needs_remote_wakeup = 0;
715 usb_autopm_put_interface(data->intf);
718 usb_scuttle_anchored_urbs(&data->deferred);
722 static int btusb_flush(struct hci_dev *hdev)
724 struct btusb_data *data = hdev->driver_data;
726 BT_DBG("%s", hdev->name);
728 usb_kill_anchored_urbs(&data->tx_anchor);
733 static int btusb_send_frame(struct sk_buff *skb)
735 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
736 struct btusb_data *data = hdev->driver_data;
737 struct usb_ctrlrequest *dr;
742 BT_DBG("%s", hdev->name);
744 if (!test_bit(HCI_RUNNING, &hdev->flags))
747 switch (bt_cb(skb)->pkt_type) {
748 case HCI_COMMAND_PKT:
749 urb = usb_alloc_urb(0, GFP_ATOMIC);
753 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
759 dr->bRequestType = data->cmdreq_type;
763 dr->wLength = __cpu_to_le16(skb->len);
765 pipe = usb_sndctrlpipe(data->udev, 0x00);
767 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
768 skb->data, skb->len, btusb_tx_complete, skb);
773 case HCI_ACLDATA_PKT:
774 if (!data->bulk_tx_ep)
777 urb = usb_alloc_urb(0, GFP_ATOMIC);
781 pipe = usb_sndbulkpipe(data->udev,
782 data->bulk_tx_ep->bEndpointAddress);
784 usb_fill_bulk_urb(urb, data->udev, pipe,
785 skb->data, skb->len, btusb_tx_complete, skb);
790 case HCI_SCODATA_PKT:
791 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
794 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
798 pipe = usb_sndisocpipe(data->udev,
799 data->isoc_tx_ep->bEndpointAddress);
801 usb_fill_int_urb(urb, data->udev, pipe,
802 skb->data, skb->len, btusb_isoc_tx_complete,
803 skb, data->isoc_tx_ep->bInterval);
805 urb->transfer_flags = URB_ISO_ASAP;
807 __fill_isoc_descriptor(urb, skb->len,
808 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
819 usb_anchor_urb(urb, &data->deferred);
820 schedule_work(&data->waker);
826 usb_anchor_urb(urb, &data->tx_anchor);
828 err = usb_submit_urb(urb, GFP_ATOMIC);
830 BT_ERR("%s urb %p submission failed", hdev->name, urb);
831 kfree(urb->setup_packet);
832 usb_unanchor_urb(urb);
834 usb_mark_last_busy(data->udev);
842 static void btusb_destruct(struct hci_dev *hdev)
844 struct btusb_data *data = hdev->driver_data;
846 BT_DBG("%s", hdev->name);
851 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
853 struct btusb_data *data = hdev->driver_data;
855 BT_DBG("%s evt %d", hdev->name, evt);
857 if (hdev->conn_hash.sco_num != data->sco_num) {
858 data->sco_num = hdev->conn_hash.sco_num;
859 schedule_work(&data->work);
863 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
865 struct btusb_data *data = hdev->driver_data;
866 struct usb_interface *intf = data->isoc;
867 struct usb_endpoint_descriptor *ep_desc;
873 err = usb_set_interface(data->udev, 1, altsetting);
875 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
879 data->isoc_altsetting = altsetting;
881 data->isoc_tx_ep = NULL;
882 data->isoc_rx_ep = NULL;
884 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
885 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
887 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
888 data->isoc_tx_ep = ep_desc;
892 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
893 data->isoc_rx_ep = ep_desc;
898 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
899 BT_ERR("%s invalid SCO descriptors", hdev->name);
906 static void btusb_work(struct work_struct *work)
908 struct btusb_data *data = container_of(work, struct btusb_data, work);
909 struct hci_dev *hdev = data->hdev;
912 if (hdev->conn_hash.sco_num > 0) {
913 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
914 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
916 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
917 usb_kill_anchored_urbs(&data->isoc_anchor);
921 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
923 if (data->isoc_altsetting != 2) {
924 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
925 usb_kill_anchored_urbs(&data->isoc_anchor);
927 if (__set_isoc_interface(hdev, 2) < 0)
931 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
932 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
933 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
935 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
938 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
939 usb_kill_anchored_urbs(&data->isoc_anchor);
941 __set_isoc_interface(hdev, 0);
942 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
943 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
947 static void btusb_waker(struct work_struct *work)
949 struct btusb_data *data = container_of(work, struct btusb_data, waker);
952 err = usb_autopm_get_interface(data->intf);
956 usb_autopm_put_interface(data->intf);
959 static int btusb_probe(struct usb_interface *intf,
960 const struct usb_device_id *id)
962 struct usb_endpoint_descriptor *ep_desc;
963 struct btusb_data *data;
964 struct hci_dev *hdev;
967 BT_DBG("intf %p id %p", intf, id);
969 /* interface numbers are hardcoded in the spec */
970 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
973 if (!id->driver_info) {
974 const struct usb_device_id *match;
975 match = usb_match_id(intf, blacklist_table);
980 if (id->driver_info == BTUSB_IGNORE)
983 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
986 if (ignore_csr && id->driver_info & BTUSB_CSR)
989 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
992 if (id->driver_info & BTUSB_ATH3012) {
993 struct usb_device *udev = interface_to_usbdev(intf);
995 /* Old firmware would otherwise let ath3k driver load
996 * patch and sysconfig files */
997 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
1001 data = kzalloc(sizeof(*data), GFP_KERNEL);
1005 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1006 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1008 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
1009 data->intr_ep = ep_desc;
1013 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
1014 data->bulk_tx_ep = ep_desc;
1018 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
1019 data->bulk_rx_ep = ep_desc;
1024 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
1029 data->cmdreq_type = USB_TYPE_CLASS;
1031 data->udev = interface_to_usbdev(intf);
1034 spin_lock_init(&data->lock);
1036 INIT_WORK(&data->work, btusb_work);
1037 INIT_WORK(&data->waker, btusb_waker);
1038 spin_lock_init(&data->txlock);
1040 init_usb_anchor(&data->tx_anchor);
1041 init_usb_anchor(&data->intr_anchor);
1042 init_usb_anchor(&data->bulk_anchor);
1043 init_usb_anchor(&data->isoc_anchor);
1044 init_usb_anchor(&data->deferred);
1046 hdev = hci_alloc_dev();
1052 hdev->bus = HCI_USB;
1053 hdev->driver_data = data;
1057 SET_HCIDEV_DEV(hdev, &intf->dev);
1059 hdev->open = btusb_open;
1060 hdev->close = btusb_close;
1061 hdev->flush = btusb_flush;
1062 hdev->send = btusb_send_frame;
1063 hdev->destruct = btusb_destruct;
1064 hdev->notify = btusb_notify;
1066 hdev->owner = THIS_MODULE;
1068 /* Interface numbers are hardcoded in the specification */
1069 data->isoc = usb_ifnum_to_if(data->udev, 1);
1072 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1074 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
1075 if (!disable_scofix)
1076 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1079 if (id->driver_info & BTUSB_BROKEN_ISOC)
1082 if (id->driver_info & BTUSB_DIGIANSWER) {
1083 data->cmdreq_type = USB_TYPE_VENDOR;
1084 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1087 if (id->driver_info & BTUSB_CSR) {
1088 struct usb_device *udev = data->udev;
1090 /* Old firmware would otherwise execute USB reset */
1091 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
1092 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1095 if (id->driver_info & BTUSB_SNIFFER) {
1096 struct usb_device *udev = data->udev;
1098 /* New sniffer firmware has crippled HCI interface */
1099 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1100 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
1105 if (id->driver_info & BTUSB_BCM92035) {
1106 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1107 struct sk_buff *skb;
1109 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1111 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1112 skb_queue_tail(&hdev->driver_init, skb);
1117 err = usb_driver_claim_interface(&btusb_driver,
1126 err = hci_register_dev(hdev);
1133 usb_set_intfdata(intf, data);
1138 static void btusb_disconnect(struct usb_interface *intf)
1140 struct btusb_data *data = usb_get_intfdata(intf);
1141 struct hci_dev *hdev;
1143 BT_DBG("intf %p", intf);
1150 __hci_dev_hold(hdev);
1152 usb_set_intfdata(data->intf, NULL);
1155 usb_set_intfdata(data->isoc, NULL);
1157 hci_unregister_dev(hdev);
1159 if (intf == data->isoc)
1160 usb_driver_release_interface(&btusb_driver, data->intf);
1161 else if (data->isoc)
1162 usb_driver_release_interface(&btusb_driver, data->isoc);
1164 __hci_dev_put(hdev);
1170 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1172 struct btusb_data *data = usb_get_intfdata(intf);
1174 BT_DBG("intf %p", intf);
1176 if (data->suspend_count++)
1179 spin_lock_irq(&data->txlock);
1180 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
1181 set_bit(BTUSB_SUSPENDING, &data->flags);
1182 spin_unlock_irq(&data->txlock);
1184 spin_unlock_irq(&data->txlock);
1185 data->suspend_count--;
1189 cancel_work_sync(&data->work);
1191 btusb_stop_traffic(data);
1192 usb_kill_anchored_urbs(&data->tx_anchor);
1197 static void play_deferred(struct btusb_data *data)
1202 while ((urb = usb_get_from_anchor(&data->deferred))) {
1203 err = usb_submit_urb(urb, GFP_ATOMIC);
1207 data->tx_in_flight++;
1209 usb_scuttle_anchored_urbs(&data->deferred);
1212 static int btusb_resume(struct usb_interface *intf)
1214 struct btusb_data *data = usb_get_intfdata(intf);
1215 struct hci_dev *hdev = data->hdev;
1218 BT_DBG("intf %p", intf);
1220 if (--data->suspend_count)
1223 if (!test_bit(HCI_RUNNING, &hdev->flags))
1226 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1227 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1229 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1234 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
1235 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1237 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1241 btusb_submit_bulk_urb(hdev, GFP_NOIO);
1244 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1245 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1246 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1248 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1251 spin_lock_irq(&data->txlock);
1252 play_deferred(data);
1253 clear_bit(BTUSB_SUSPENDING, &data->flags);
1254 spin_unlock_irq(&data->txlock);
1255 schedule_work(&data->work);
1260 usb_scuttle_anchored_urbs(&data->deferred);
1262 spin_lock_irq(&data->txlock);
1263 clear_bit(BTUSB_SUSPENDING, &data->flags);
1264 spin_unlock_irq(&data->txlock);
1270 static struct usb_driver btusb_driver = {
1272 .probe = btusb_probe,
1273 .disconnect = btusb_disconnect,
1275 .suspend = btusb_suspend,
1276 .resume = btusb_resume,
1278 .id_table = btusb_table,
1279 .supports_autosuspend = 1,
1282 static int __init btusb_init(void)
1284 BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
1286 return usb_register(&btusb_driver);
1289 static void __exit btusb_exit(void)
1291 usb_deregister(&btusb_driver);
1294 module_init(btusb_init);
1295 module_exit(btusb_exit);
1297 module_param(ignore_dga, bool, 0644);
1298 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1300 module_param(ignore_csr, bool, 0644);
1301 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1303 module_param(ignore_sniffer, bool, 0644);
1304 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1306 module_param(disable_scofix, bool, 0644);
1307 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1309 module_param(force_scofix, bool, 0644);
1310 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1312 module_param(reset, bool, 0644);
1313 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1315 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1316 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1317 MODULE_VERSION(VERSION);
1318 MODULE_LICENSE("GPL");