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(0x0489, 0xe042) },
111 { USB_DEVICE(0x04ca, 0x2003) },
112 { USB_DEVICE(0x0b05, 0x17b5) },
113 { USB_DEVICE(0x0b05, 0x17cb) },
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 /* IMC Networks - Broadcom based */
126 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01) },
128 { } /* Terminating entry */
131 MODULE_DEVICE_TABLE(usb, btusb_table);
133 static struct usb_device_id blacklist_table[] = {
134 /* CSR BlueCore devices */
135 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
137 /* Broadcom BCM2033 without firmware */
138 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
140 /* Atheros 3011 with sflash firmware */
141 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
142 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
143 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
144 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
145 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
146 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
148 /* Atheros AR9285 Malbec with sflash firmware */
149 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
151 /* Atheros 3012 with sflash firmware */
152 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
153 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
154 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
155 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
156 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
157 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
158 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
159 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
160 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
161 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
162 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
163 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
164 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
165 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
166 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
167 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
168 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
169 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
170 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
171 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
172 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
173 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
174 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
175 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
176 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
177 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
178 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
179 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
180 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
182 /* Atheros AR5BBU12 with sflash firmware */
183 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
185 /* Atheros AR5BBU12 with sflash firmware */
186 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
187 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
189 /* Broadcom BCM2035 */
190 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
191 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
192 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
194 /* Broadcom BCM2045 */
195 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
196 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
198 /* IBM/Lenovo ThinkPad with Broadcom chip */
199 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
200 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
202 /* HP laptop with Broadcom chip */
203 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
205 /* Dell laptop with Broadcom chip */
206 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
208 /* Dell Wireless 370 and 410 devices */
209 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
210 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
212 /* Belkin F8T012 and F8T013 devices */
213 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
214 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
216 /* Asus WL-BTD202 device */
217 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
219 /* Kensington Bluetooth USB adapter */
220 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
222 /* RTX Telecom based adapters with buggy SCO support */
223 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
224 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
226 /* CONWISE Technology based adapters with buggy SCO support */
227 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
229 /* Digianswer devices */
230 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
231 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
233 /* CSR BlueCore Bluetooth Sniffer */
234 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
236 /* Frontline ComProbe Bluetooth Sniffer */
237 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
239 { } /* Terminating entry */
242 #define BTUSB_MAX_ISOC_FRAMES 10
244 #define BTUSB_INTR_RUNNING 0
245 #define BTUSB_BULK_RUNNING 1
246 #define BTUSB_ISOC_RUNNING 2
247 #define BTUSB_SUSPENDING 3
248 #define BTUSB_DID_ISO_RESUME 4
251 struct hci_dev *hdev;
252 struct usb_device *udev;
253 struct usb_interface *intf;
254 struct usb_interface *isoc;
260 struct work_struct work;
261 struct work_struct waker;
263 struct usb_anchor tx_anchor;
264 struct usb_anchor intr_anchor;
265 struct usb_anchor bulk_anchor;
266 struct usb_anchor isoc_anchor;
267 struct usb_anchor deferred;
271 struct usb_endpoint_descriptor *intr_ep;
272 struct usb_endpoint_descriptor *bulk_tx_ep;
273 struct usb_endpoint_descriptor *bulk_rx_ep;
274 struct usb_endpoint_descriptor *isoc_tx_ep;
275 struct usb_endpoint_descriptor *isoc_rx_ep;
279 unsigned int sco_num;
284 static int inc_tx(struct btusb_data *data)
289 spin_lock_irqsave(&data->txlock, flags);
290 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
292 data->tx_in_flight++;
293 spin_unlock_irqrestore(&data->txlock, flags);
298 static void btusb_intr_complete(struct urb *urb)
300 struct hci_dev *hdev = urb->context;
301 struct btusb_data *data = hdev->driver_data;
304 BT_DBG("%s urb %p status %d count %d", hdev->name,
305 urb, urb->status, urb->actual_length);
307 if (!test_bit(HCI_RUNNING, &hdev->flags))
310 if (urb->status == 0) {
311 hdev->stat.byte_rx += urb->actual_length;
313 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
314 urb->transfer_buffer,
315 urb->actual_length) < 0) {
316 BT_ERR("%s corrupted event packet", hdev->name);
319 } else if (urb->status == -ENOENT) {
320 /* Avoid suspend failed when usb_kill_urb */
324 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
327 usb_mark_last_busy(data->udev);
328 usb_anchor_urb(urb, &data->intr_anchor);
330 err = usb_submit_urb(urb, GFP_ATOMIC);
332 /* -EPERM: urb is being killed;
333 * -ENODEV: device got disconnected */
334 if (err != -EPERM && err != -ENODEV)
335 BT_ERR("%s urb %p failed to resubmit (%d)",
336 hdev->name, urb, -err);
337 usb_unanchor_urb(urb);
341 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
343 struct btusb_data *data = hdev->driver_data;
349 BT_DBG("%s", hdev->name);
354 urb = usb_alloc_urb(0, mem_flags);
358 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
360 buf = kmalloc(size, mem_flags);
366 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
368 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
369 btusb_intr_complete, hdev,
370 data->intr_ep->bInterval);
372 urb->transfer_flags |= URB_FREE_BUFFER;
374 usb_anchor_urb(urb, &data->intr_anchor);
376 err = usb_submit_urb(urb, mem_flags);
378 BT_ERR("%s urb %p submission failed (%d)",
379 hdev->name, urb, -err);
380 usb_unanchor_urb(urb);
388 static void btusb_bulk_complete(struct urb *urb)
390 struct hci_dev *hdev = urb->context;
391 struct btusb_data *data = hdev->driver_data;
394 BT_DBG("%s urb %p status %d count %d", hdev->name,
395 urb, urb->status, urb->actual_length);
397 if (!test_bit(HCI_RUNNING, &hdev->flags))
400 if (urb->status == 0) {
401 hdev->stat.byte_rx += urb->actual_length;
403 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
404 urb->transfer_buffer,
405 urb->actual_length) < 0) {
406 BT_ERR("%s corrupted ACL packet", hdev->name);
409 } else if (urb->status == -ENOENT) {
410 /* Avoid suspend failed when usb_kill_urb */
414 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
417 usb_anchor_urb(urb, &data->bulk_anchor);
418 usb_mark_last_busy(data->udev);
420 err = usb_submit_urb(urb, GFP_ATOMIC);
422 /* -EPERM: urb is being killed;
423 * -ENODEV: device got disconnected */
424 if (err != -EPERM && err != -ENODEV)
425 BT_ERR("%s urb %p failed to resubmit (%d)",
426 hdev->name, urb, -err);
427 usb_unanchor_urb(urb);
431 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
433 struct btusb_data *data = hdev->driver_data;
437 int err, size = HCI_MAX_FRAME_SIZE;
439 BT_DBG("%s", hdev->name);
441 if (!data->bulk_rx_ep)
444 urb = usb_alloc_urb(0, mem_flags);
448 buf = kmalloc(size, mem_flags);
454 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
456 usb_fill_bulk_urb(urb, data->udev, pipe,
457 buf, size, btusb_bulk_complete, hdev);
459 urb->transfer_flags |= URB_FREE_BUFFER;
461 usb_mark_last_busy(data->udev);
462 usb_anchor_urb(urb, &data->bulk_anchor);
464 err = usb_submit_urb(urb, mem_flags);
466 BT_ERR("%s urb %p submission failed (%d)",
467 hdev->name, urb, -err);
468 usb_unanchor_urb(urb);
476 static void btusb_isoc_complete(struct urb *urb)
478 struct hci_dev *hdev = urb->context;
479 struct btusb_data *data = hdev->driver_data;
482 BT_DBG("%s urb %p status %d count %d", hdev->name,
483 urb, urb->status, urb->actual_length);
485 if (!test_bit(HCI_RUNNING, &hdev->flags))
488 if (urb->status == 0) {
489 for (i = 0; i < urb->number_of_packets; i++) {
490 unsigned int offset = urb->iso_frame_desc[i].offset;
491 unsigned int length = urb->iso_frame_desc[i].actual_length;
493 if (urb->iso_frame_desc[i].status)
496 hdev->stat.byte_rx += length;
498 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
499 urb->transfer_buffer + offset,
501 BT_ERR("%s corrupted SCO packet", hdev->name);
505 } else if (urb->status == -ENOENT) {
506 /* Avoid suspend failed when usb_kill_urb */
510 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
513 usb_anchor_urb(urb, &data->isoc_anchor);
515 err = usb_submit_urb(urb, GFP_ATOMIC);
517 /* -EPERM: urb is being killed;
518 * -ENODEV: device got disconnected */
519 if (err != -EPERM && err != -ENODEV)
520 BT_ERR("%s urb %p failed to resubmit (%d)",
521 hdev->name, urb, -err);
522 usb_unanchor_urb(urb);
526 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
530 BT_DBG("len %d mtu %d", len, mtu);
532 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
533 i++, offset += mtu, len -= mtu) {
534 urb->iso_frame_desc[i].offset = offset;
535 urb->iso_frame_desc[i].length = mtu;
538 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
539 urb->iso_frame_desc[i].offset = offset;
540 urb->iso_frame_desc[i].length = len;
544 urb->number_of_packets = i;
547 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
549 struct btusb_data *data = hdev->driver_data;
555 BT_DBG("%s", hdev->name);
557 if (!data->isoc_rx_ep)
560 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
564 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
565 BTUSB_MAX_ISOC_FRAMES;
567 buf = kmalloc(size, mem_flags);
573 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
575 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
576 hdev, data->isoc_rx_ep->bInterval);
578 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
580 __fill_isoc_descriptor(urb, size,
581 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
583 usb_anchor_urb(urb, &data->isoc_anchor);
585 err = usb_submit_urb(urb, mem_flags);
587 BT_ERR("%s urb %p submission failed (%d)",
588 hdev->name, urb, -err);
589 usb_unanchor_urb(urb);
597 static void btusb_tx_complete(struct urb *urb)
599 struct sk_buff *skb = urb->context;
600 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
601 struct btusb_data *data = hdev->driver_data;
603 BT_DBG("%s urb %p status %d count %d", hdev->name,
604 urb, urb->status, urb->actual_length);
606 if (!test_bit(HCI_RUNNING, &hdev->flags))
610 hdev->stat.byte_tx += urb->transfer_buffer_length;
615 spin_lock(&data->txlock);
616 data->tx_in_flight--;
617 spin_unlock(&data->txlock);
619 kfree(urb->setup_packet);
624 static void btusb_isoc_tx_complete(struct urb *urb)
626 struct sk_buff *skb = urb->context;
627 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
629 BT_DBG("%s urb %p status %d count %d", hdev->name,
630 urb, urb->status, urb->actual_length);
632 if (!test_bit(HCI_RUNNING, &hdev->flags))
636 hdev->stat.byte_tx += urb->transfer_buffer_length;
641 kfree(urb->setup_packet);
646 static int btusb_open(struct hci_dev *hdev)
648 struct btusb_data *data = hdev->driver_data;
651 BT_DBG("%s", hdev->name);
653 err = usb_autopm_get_interface(data->intf);
657 data->intf->needs_remote_wakeup = 1;
659 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
662 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
665 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
669 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
671 usb_kill_anchored_urbs(&data->intr_anchor);
675 set_bit(BTUSB_BULK_RUNNING, &data->flags);
676 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
679 usb_autopm_put_interface(data->intf);
683 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
684 clear_bit(HCI_RUNNING, &hdev->flags);
685 usb_autopm_put_interface(data->intf);
689 static void btusb_stop_traffic(struct btusb_data *data)
691 usb_kill_anchored_urbs(&data->intr_anchor);
692 usb_kill_anchored_urbs(&data->bulk_anchor);
693 usb_kill_anchored_urbs(&data->isoc_anchor);
696 static int btusb_close(struct hci_dev *hdev)
698 struct btusb_data *data = hdev->driver_data;
701 BT_DBG("%s", hdev->name);
703 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
706 cancel_work_sync(&data->work);
707 cancel_work_sync(&data->waker);
709 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
710 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
711 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
713 btusb_stop_traffic(data);
714 err = usb_autopm_get_interface(data->intf);
718 data->intf->needs_remote_wakeup = 0;
719 usb_autopm_put_interface(data->intf);
722 usb_scuttle_anchored_urbs(&data->deferred);
726 static int btusb_flush(struct hci_dev *hdev)
728 struct btusb_data *data = hdev->driver_data;
730 BT_DBG("%s", hdev->name);
732 usb_kill_anchored_urbs(&data->tx_anchor);
737 static int btusb_send_frame(struct sk_buff *skb)
739 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
740 struct btusb_data *data = hdev->driver_data;
741 struct usb_ctrlrequest *dr;
746 BT_DBG("%s", hdev->name);
748 if (!test_bit(HCI_RUNNING, &hdev->flags))
751 switch (bt_cb(skb)->pkt_type) {
752 case HCI_COMMAND_PKT:
753 urb = usb_alloc_urb(0, GFP_ATOMIC);
757 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
763 dr->bRequestType = data->cmdreq_type;
767 dr->wLength = __cpu_to_le16(skb->len);
769 pipe = usb_sndctrlpipe(data->udev, 0x00);
771 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
772 skb->data, skb->len, btusb_tx_complete, skb);
777 case HCI_ACLDATA_PKT:
778 if (!data->bulk_tx_ep)
781 urb = usb_alloc_urb(0, GFP_ATOMIC);
785 pipe = usb_sndbulkpipe(data->udev,
786 data->bulk_tx_ep->bEndpointAddress);
788 usb_fill_bulk_urb(urb, data->udev, pipe,
789 skb->data, skb->len, btusb_tx_complete, skb);
794 case HCI_SCODATA_PKT:
795 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
798 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
802 pipe = usb_sndisocpipe(data->udev,
803 data->isoc_tx_ep->bEndpointAddress);
805 usb_fill_int_urb(urb, data->udev, pipe,
806 skb->data, skb->len, btusb_isoc_tx_complete,
807 skb, data->isoc_tx_ep->bInterval);
809 urb->transfer_flags = URB_ISO_ASAP;
811 __fill_isoc_descriptor(urb, skb->len,
812 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
823 usb_anchor_urb(urb, &data->deferred);
824 schedule_work(&data->waker);
830 usb_anchor_urb(urb, &data->tx_anchor);
832 err = usb_submit_urb(urb, GFP_ATOMIC);
834 BT_ERR("%s urb %p submission failed", hdev->name, urb);
835 kfree(urb->setup_packet);
836 usb_unanchor_urb(urb);
838 usb_mark_last_busy(data->udev);
846 static void btusb_destruct(struct hci_dev *hdev)
848 struct btusb_data *data = hdev->driver_data;
850 BT_DBG("%s", hdev->name);
855 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
857 struct btusb_data *data = hdev->driver_data;
859 BT_DBG("%s evt %d", hdev->name, evt);
861 if (hdev->conn_hash.sco_num != data->sco_num) {
862 data->sco_num = hdev->conn_hash.sco_num;
863 schedule_work(&data->work);
867 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
869 struct btusb_data *data = hdev->driver_data;
870 struct usb_interface *intf = data->isoc;
871 struct usb_endpoint_descriptor *ep_desc;
877 err = usb_set_interface(data->udev, 1, altsetting);
879 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
883 data->isoc_altsetting = altsetting;
885 data->isoc_tx_ep = NULL;
886 data->isoc_rx_ep = NULL;
888 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
889 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
891 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
892 data->isoc_tx_ep = ep_desc;
896 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
897 data->isoc_rx_ep = ep_desc;
902 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
903 BT_ERR("%s invalid SCO descriptors", hdev->name);
910 static void btusb_work(struct work_struct *work)
912 struct btusb_data *data = container_of(work, struct btusb_data, work);
913 struct hci_dev *hdev = data->hdev;
916 if (hdev->conn_hash.sco_num > 0) {
917 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
918 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
920 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
921 usb_kill_anchored_urbs(&data->isoc_anchor);
925 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
927 if (data->isoc_altsetting != 2) {
928 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
929 usb_kill_anchored_urbs(&data->isoc_anchor);
931 if (__set_isoc_interface(hdev, 2) < 0)
935 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
936 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
937 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
939 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
942 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
943 usb_kill_anchored_urbs(&data->isoc_anchor);
945 __set_isoc_interface(hdev, 0);
946 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
947 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
951 static void btusb_waker(struct work_struct *work)
953 struct btusb_data *data = container_of(work, struct btusb_data, waker);
956 err = usb_autopm_get_interface(data->intf);
960 usb_autopm_put_interface(data->intf);
963 static int btusb_probe(struct usb_interface *intf,
964 const struct usb_device_id *id)
966 struct usb_endpoint_descriptor *ep_desc;
967 struct btusb_data *data;
968 struct hci_dev *hdev;
971 BT_DBG("intf %p id %p", intf, id);
973 /* interface numbers are hardcoded in the spec */
974 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
977 if (!id->driver_info) {
978 const struct usb_device_id *match;
979 match = usb_match_id(intf, blacklist_table);
984 if (id->driver_info == BTUSB_IGNORE)
987 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
990 if (ignore_csr && id->driver_info & BTUSB_CSR)
993 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
996 if (id->driver_info & BTUSB_ATH3012) {
997 struct usb_device *udev = interface_to_usbdev(intf);
999 /* Old firmware would otherwise let ath3k driver load
1000 * patch and sysconfig files */
1001 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
1005 data = kzalloc(sizeof(*data), GFP_KERNEL);
1009 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1010 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1012 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
1013 data->intr_ep = ep_desc;
1017 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
1018 data->bulk_tx_ep = ep_desc;
1022 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
1023 data->bulk_rx_ep = ep_desc;
1028 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
1033 data->cmdreq_type = USB_TYPE_CLASS;
1035 data->udev = interface_to_usbdev(intf);
1038 spin_lock_init(&data->lock);
1040 INIT_WORK(&data->work, btusb_work);
1041 INIT_WORK(&data->waker, btusb_waker);
1042 spin_lock_init(&data->txlock);
1044 init_usb_anchor(&data->tx_anchor);
1045 init_usb_anchor(&data->intr_anchor);
1046 init_usb_anchor(&data->bulk_anchor);
1047 init_usb_anchor(&data->isoc_anchor);
1048 init_usb_anchor(&data->deferred);
1050 hdev = hci_alloc_dev();
1056 hdev->bus = HCI_USB;
1057 hdev->driver_data = data;
1061 SET_HCIDEV_DEV(hdev, &intf->dev);
1063 hdev->open = btusb_open;
1064 hdev->close = btusb_close;
1065 hdev->flush = btusb_flush;
1066 hdev->send = btusb_send_frame;
1067 hdev->destruct = btusb_destruct;
1068 hdev->notify = btusb_notify;
1070 hdev->owner = THIS_MODULE;
1072 /* Interface numbers are hardcoded in the specification */
1073 data->isoc = usb_ifnum_to_if(data->udev, 1);
1076 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1078 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
1079 if (!disable_scofix)
1080 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1083 if (id->driver_info & BTUSB_BROKEN_ISOC)
1086 if (id->driver_info & BTUSB_DIGIANSWER) {
1087 data->cmdreq_type = USB_TYPE_VENDOR;
1088 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1091 if (id->driver_info & BTUSB_CSR) {
1092 struct usb_device *udev = data->udev;
1094 /* Old firmware would otherwise execute USB reset */
1095 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
1096 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1099 if (id->driver_info & BTUSB_SNIFFER) {
1100 struct usb_device *udev = data->udev;
1102 /* New sniffer firmware has crippled HCI interface */
1103 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1104 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
1109 if (id->driver_info & BTUSB_BCM92035) {
1110 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1111 struct sk_buff *skb;
1113 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1115 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1116 skb_queue_tail(&hdev->driver_init, skb);
1121 err = usb_driver_claim_interface(&btusb_driver,
1130 err = hci_register_dev(hdev);
1137 usb_set_intfdata(intf, data);
1142 static void btusb_disconnect(struct usb_interface *intf)
1144 struct btusb_data *data = usb_get_intfdata(intf);
1145 struct hci_dev *hdev;
1147 BT_DBG("intf %p", intf);
1154 __hci_dev_hold(hdev);
1156 usb_set_intfdata(data->intf, NULL);
1159 usb_set_intfdata(data->isoc, NULL);
1161 hci_unregister_dev(hdev);
1163 if (intf == data->isoc)
1164 usb_driver_release_interface(&btusb_driver, data->intf);
1165 else if (data->isoc)
1166 usb_driver_release_interface(&btusb_driver, data->isoc);
1168 __hci_dev_put(hdev);
1174 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1176 struct btusb_data *data = usb_get_intfdata(intf);
1178 BT_DBG("intf %p", intf);
1180 if (data->suspend_count++)
1183 spin_lock_irq(&data->txlock);
1184 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
1185 set_bit(BTUSB_SUSPENDING, &data->flags);
1186 spin_unlock_irq(&data->txlock);
1188 spin_unlock_irq(&data->txlock);
1189 data->suspend_count--;
1193 cancel_work_sync(&data->work);
1195 btusb_stop_traffic(data);
1196 usb_kill_anchored_urbs(&data->tx_anchor);
1201 static void play_deferred(struct btusb_data *data)
1206 while ((urb = usb_get_from_anchor(&data->deferred))) {
1207 err = usb_submit_urb(urb, GFP_ATOMIC);
1211 data->tx_in_flight++;
1213 usb_scuttle_anchored_urbs(&data->deferred);
1216 static int btusb_resume(struct usb_interface *intf)
1218 struct btusb_data *data = usb_get_intfdata(intf);
1219 struct hci_dev *hdev = data->hdev;
1222 BT_DBG("intf %p", intf);
1224 if (--data->suspend_count)
1227 if (!test_bit(HCI_RUNNING, &hdev->flags))
1230 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1231 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1233 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1238 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
1239 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1241 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1245 btusb_submit_bulk_urb(hdev, GFP_NOIO);
1248 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1249 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1250 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1252 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1255 spin_lock_irq(&data->txlock);
1256 play_deferred(data);
1257 clear_bit(BTUSB_SUSPENDING, &data->flags);
1258 spin_unlock_irq(&data->txlock);
1259 schedule_work(&data->work);
1264 usb_scuttle_anchored_urbs(&data->deferred);
1266 spin_lock_irq(&data->txlock);
1267 clear_bit(BTUSB_SUSPENDING, &data->flags);
1268 spin_unlock_irq(&data->txlock);
1274 static struct usb_driver btusb_driver = {
1276 .probe = btusb_probe,
1277 .disconnect = btusb_disconnect,
1279 .suspend = btusb_suspend,
1280 .resume = btusb_resume,
1282 .id_table = btusb_table,
1283 .supports_autosuspend = 1,
1286 static int __init btusb_init(void)
1288 BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
1290 return usb_register(&btusb_driver);
1293 static void __exit btusb_exit(void)
1295 usb_deregister(&btusb_driver);
1298 module_init(btusb_init);
1299 module_exit(btusb_exit);
1301 module_param(ignore_dga, bool, 0644);
1302 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1304 module_param(ignore_csr, bool, 0644);
1305 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1307 module_param(ignore_sniffer, bool, 0644);
1308 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1310 module_param(disable_scofix, bool, 0644);
1311 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1313 module_param(force_scofix, bool, 0644);
1314 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1316 module_param(reset, bool, 0644);
1317 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1319 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1320 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1321 MODULE_VERSION(VERSION);
1322 MODULE_LICENSE("GPL");