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, 0x817a), .driver_info = BTUSB_ATH3012 },
154 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
155 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
156 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
157 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
158 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
159 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
160 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
161 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
162 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
163 { USB_DEVICE(0x0cf3, 0xe005), .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(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
167 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
168 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
169 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
170 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
171 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
172 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
173 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
174 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
175 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
177 /* Atheros AR5BBU12 with sflash firmware */
178 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
180 /* Atheros AR5BBU12 with sflash firmware */
181 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
182 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
184 /* Broadcom BCM2035 */
185 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
186 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
187 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
189 /* Broadcom BCM2045 */
190 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
191 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
193 /* IBM/Lenovo ThinkPad with Broadcom chip */
194 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
195 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
197 /* HP laptop with Broadcom chip */
198 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
200 /* Dell laptop with Broadcom chip */
201 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
203 /* Dell Wireless 370 and 410 devices */
204 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
205 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
207 /* Belkin F8T012 and F8T013 devices */
208 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
209 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
211 /* Asus WL-BTD202 device */
212 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
214 /* Kensington Bluetooth USB adapter */
215 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
217 /* RTX Telecom based adapters with buggy SCO support */
218 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
219 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
221 /* CONWISE Technology based adapters with buggy SCO support */
222 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
224 /* Digianswer devices */
225 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
226 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
228 /* CSR BlueCore Bluetooth Sniffer */
229 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
231 /* Frontline ComProbe Bluetooth Sniffer */
232 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
234 { } /* Terminating entry */
237 #define BTUSB_MAX_ISOC_FRAMES 10
239 #define BTUSB_INTR_RUNNING 0
240 #define BTUSB_BULK_RUNNING 1
241 #define BTUSB_ISOC_RUNNING 2
242 #define BTUSB_SUSPENDING 3
243 #define BTUSB_DID_ISO_RESUME 4
246 struct hci_dev *hdev;
247 struct usb_device *udev;
248 struct usb_interface *intf;
249 struct usb_interface *isoc;
255 struct work_struct work;
256 struct work_struct waker;
258 struct usb_anchor tx_anchor;
259 struct usb_anchor intr_anchor;
260 struct usb_anchor bulk_anchor;
261 struct usb_anchor isoc_anchor;
262 struct usb_anchor deferred;
266 struct usb_endpoint_descriptor *intr_ep;
267 struct usb_endpoint_descriptor *bulk_tx_ep;
268 struct usb_endpoint_descriptor *bulk_rx_ep;
269 struct usb_endpoint_descriptor *isoc_tx_ep;
270 struct usb_endpoint_descriptor *isoc_rx_ep;
274 unsigned int sco_num;
279 static int inc_tx(struct btusb_data *data)
284 spin_lock_irqsave(&data->txlock, flags);
285 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
287 data->tx_in_flight++;
288 spin_unlock_irqrestore(&data->txlock, flags);
293 static void btusb_intr_complete(struct urb *urb)
295 struct hci_dev *hdev = urb->context;
296 struct btusb_data *data = hdev->driver_data;
299 BT_DBG("%s urb %p status %d count %d", hdev->name,
300 urb, urb->status, urb->actual_length);
302 if (!test_bit(HCI_RUNNING, &hdev->flags))
305 if (urb->status == 0) {
306 hdev->stat.byte_rx += urb->actual_length;
308 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
309 urb->transfer_buffer,
310 urb->actual_length) < 0) {
311 BT_ERR("%s corrupted event packet", hdev->name);
314 } else if (urb->status == -ENOENT) {
315 /* Avoid suspend failed when usb_kill_urb */
319 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
322 usb_mark_last_busy(data->udev);
323 usb_anchor_urb(urb, &data->intr_anchor);
325 err = usb_submit_urb(urb, GFP_ATOMIC);
327 /* -EPERM: urb is being killed;
328 * -ENODEV: device got disconnected */
329 if (err != -EPERM && err != -ENODEV)
330 BT_ERR("%s urb %p failed to resubmit (%d)",
331 hdev->name, urb, -err);
332 usb_unanchor_urb(urb);
336 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
338 struct btusb_data *data = hdev->driver_data;
344 BT_DBG("%s", hdev->name);
349 urb = usb_alloc_urb(0, mem_flags);
353 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
355 buf = kmalloc(size, mem_flags);
361 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
363 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
364 btusb_intr_complete, hdev,
365 data->intr_ep->bInterval);
367 urb->transfer_flags |= URB_FREE_BUFFER;
369 usb_anchor_urb(urb, &data->intr_anchor);
371 err = usb_submit_urb(urb, mem_flags);
373 BT_ERR("%s urb %p submission failed (%d)",
374 hdev->name, urb, -err);
375 usb_unanchor_urb(urb);
383 static void btusb_bulk_complete(struct urb *urb)
385 struct hci_dev *hdev = urb->context;
386 struct btusb_data *data = hdev->driver_data;
389 BT_DBG("%s urb %p status %d count %d", hdev->name,
390 urb, urb->status, urb->actual_length);
392 if (!test_bit(HCI_RUNNING, &hdev->flags))
395 if (urb->status == 0) {
396 hdev->stat.byte_rx += urb->actual_length;
398 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
399 urb->transfer_buffer,
400 urb->actual_length) < 0) {
401 BT_ERR("%s corrupted ACL packet", hdev->name);
404 } else if (urb->status == -ENOENT) {
405 /* Avoid suspend failed when usb_kill_urb */
409 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
412 usb_anchor_urb(urb, &data->bulk_anchor);
413 usb_mark_last_busy(data->udev);
415 err = usb_submit_urb(urb, GFP_ATOMIC);
417 /* -EPERM: urb is being killed;
418 * -ENODEV: device got disconnected */
419 if (err != -EPERM && err != -ENODEV)
420 BT_ERR("%s urb %p failed to resubmit (%d)",
421 hdev->name, urb, -err);
422 usb_unanchor_urb(urb);
426 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
428 struct btusb_data *data = hdev->driver_data;
432 int err, size = HCI_MAX_FRAME_SIZE;
434 BT_DBG("%s", hdev->name);
436 if (!data->bulk_rx_ep)
439 urb = usb_alloc_urb(0, mem_flags);
443 buf = kmalloc(size, mem_flags);
449 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
451 usb_fill_bulk_urb(urb, data->udev, pipe,
452 buf, size, btusb_bulk_complete, hdev);
454 urb->transfer_flags |= URB_FREE_BUFFER;
456 usb_mark_last_busy(data->udev);
457 usb_anchor_urb(urb, &data->bulk_anchor);
459 err = usb_submit_urb(urb, mem_flags);
461 BT_ERR("%s urb %p submission failed (%d)",
462 hdev->name, urb, -err);
463 usb_unanchor_urb(urb);
471 static void btusb_isoc_complete(struct urb *urb)
473 struct hci_dev *hdev = urb->context;
474 struct btusb_data *data = hdev->driver_data;
477 BT_DBG("%s urb %p status %d count %d", hdev->name,
478 urb, urb->status, urb->actual_length);
480 if (!test_bit(HCI_RUNNING, &hdev->flags))
483 if (urb->status == 0) {
484 for (i = 0; i < urb->number_of_packets; i++) {
485 unsigned int offset = urb->iso_frame_desc[i].offset;
486 unsigned int length = urb->iso_frame_desc[i].actual_length;
488 if (urb->iso_frame_desc[i].status)
491 hdev->stat.byte_rx += length;
493 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
494 urb->transfer_buffer + offset,
496 BT_ERR("%s corrupted SCO packet", hdev->name);
500 } else if (urb->status == -ENOENT) {
501 /* Avoid suspend failed when usb_kill_urb */
505 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
508 usb_anchor_urb(urb, &data->isoc_anchor);
510 err = usb_submit_urb(urb, GFP_ATOMIC);
512 /* -EPERM: urb is being killed;
513 * -ENODEV: device got disconnected */
514 if (err != -EPERM && err != -ENODEV)
515 BT_ERR("%s urb %p failed to resubmit (%d)",
516 hdev->name, urb, -err);
517 usb_unanchor_urb(urb);
521 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
525 BT_DBG("len %d mtu %d", len, mtu);
527 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
528 i++, offset += mtu, len -= mtu) {
529 urb->iso_frame_desc[i].offset = offset;
530 urb->iso_frame_desc[i].length = mtu;
533 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
534 urb->iso_frame_desc[i].offset = offset;
535 urb->iso_frame_desc[i].length = len;
539 urb->number_of_packets = i;
542 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
544 struct btusb_data *data = hdev->driver_data;
550 BT_DBG("%s", hdev->name);
552 if (!data->isoc_rx_ep)
555 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
559 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
560 BTUSB_MAX_ISOC_FRAMES;
562 buf = kmalloc(size, mem_flags);
568 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
570 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
571 hdev, data->isoc_rx_ep->bInterval);
573 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
575 __fill_isoc_descriptor(urb, size,
576 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
578 usb_anchor_urb(urb, &data->isoc_anchor);
580 err = usb_submit_urb(urb, mem_flags);
582 BT_ERR("%s urb %p submission failed (%d)",
583 hdev->name, urb, -err);
584 usb_unanchor_urb(urb);
592 static void btusb_tx_complete(struct urb *urb)
594 struct sk_buff *skb = urb->context;
595 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
596 struct btusb_data *data = hdev->driver_data;
598 BT_DBG("%s urb %p status %d count %d", hdev->name,
599 urb, urb->status, urb->actual_length);
601 if (!test_bit(HCI_RUNNING, &hdev->flags))
605 hdev->stat.byte_tx += urb->transfer_buffer_length;
610 spin_lock(&data->txlock);
611 data->tx_in_flight--;
612 spin_unlock(&data->txlock);
614 kfree(urb->setup_packet);
619 static void btusb_isoc_tx_complete(struct urb *urb)
621 struct sk_buff *skb = urb->context;
622 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
624 BT_DBG("%s urb %p status %d count %d", hdev->name,
625 urb, urb->status, urb->actual_length);
627 if (!test_bit(HCI_RUNNING, &hdev->flags))
631 hdev->stat.byte_tx += urb->transfer_buffer_length;
636 kfree(urb->setup_packet);
641 static int btusb_open(struct hci_dev *hdev)
643 struct btusb_data *data = hdev->driver_data;
646 BT_DBG("%s", hdev->name);
648 err = usb_autopm_get_interface(data->intf);
652 data->intf->needs_remote_wakeup = 1;
654 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
657 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
660 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
664 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
666 usb_kill_anchored_urbs(&data->intr_anchor);
670 set_bit(BTUSB_BULK_RUNNING, &data->flags);
671 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
674 usb_autopm_put_interface(data->intf);
678 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
679 clear_bit(HCI_RUNNING, &hdev->flags);
680 usb_autopm_put_interface(data->intf);
684 static void btusb_stop_traffic(struct btusb_data *data)
686 usb_kill_anchored_urbs(&data->intr_anchor);
687 usb_kill_anchored_urbs(&data->bulk_anchor);
688 usb_kill_anchored_urbs(&data->isoc_anchor);
691 static int btusb_close(struct hci_dev *hdev)
693 struct btusb_data *data = hdev->driver_data;
696 BT_DBG("%s", hdev->name);
698 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
701 cancel_work_sync(&data->work);
702 cancel_work_sync(&data->waker);
704 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
705 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
706 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
708 btusb_stop_traffic(data);
709 err = usb_autopm_get_interface(data->intf);
713 data->intf->needs_remote_wakeup = 0;
714 usb_autopm_put_interface(data->intf);
717 usb_scuttle_anchored_urbs(&data->deferred);
721 static int btusb_flush(struct hci_dev *hdev)
723 struct btusb_data *data = hdev->driver_data;
725 BT_DBG("%s", hdev->name);
727 usb_kill_anchored_urbs(&data->tx_anchor);
732 static int btusb_send_frame(struct sk_buff *skb)
734 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
735 struct btusb_data *data = hdev->driver_data;
736 struct usb_ctrlrequest *dr;
741 BT_DBG("%s", hdev->name);
743 if (!test_bit(HCI_RUNNING, &hdev->flags))
746 switch (bt_cb(skb)->pkt_type) {
747 case HCI_COMMAND_PKT:
748 urb = usb_alloc_urb(0, GFP_ATOMIC);
752 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
758 dr->bRequestType = data->cmdreq_type;
762 dr->wLength = __cpu_to_le16(skb->len);
764 pipe = usb_sndctrlpipe(data->udev, 0x00);
766 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
767 skb->data, skb->len, btusb_tx_complete, skb);
772 case HCI_ACLDATA_PKT:
773 if (!data->bulk_tx_ep)
776 urb = usb_alloc_urb(0, GFP_ATOMIC);
780 pipe = usb_sndbulkpipe(data->udev,
781 data->bulk_tx_ep->bEndpointAddress);
783 usb_fill_bulk_urb(urb, data->udev, pipe,
784 skb->data, skb->len, btusb_tx_complete, skb);
789 case HCI_SCODATA_PKT:
790 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
793 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
797 pipe = usb_sndisocpipe(data->udev,
798 data->isoc_tx_ep->bEndpointAddress);
800 usb_fill_int_urb(urb, data->udev, pipe,
801 skb->data, skb->len, btusb_isoc_tx_complete,
802 skb, data->isoc_tx_ep->bInterval);
804 urb->transfer_flags = URB_ISO_ASAP;
806 __fill_isoc_descriptor(urb, skb->len,
807 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
818 usb_anchor_urb(urb, &data->deferred);
819 schedule_work(&data->waker);
825 usb_anchor_urb(urb, &data->tx_anchor);
827 err = usb_submit_urb(urb, GFP_ATOMIC);
829 BT_ERR("%s urb %p submission failed", hdev->name, urb);
830 kfree(urb->setup_packet);
831 usb_unanchor_urb(urb);
833 usb_mark_last_busy(data->udev);
841 static void btusb_destruct(struct hci_dev *hdev)
843 struct btusb_data *data = hdev->driver_data;
845 BT_DBG("%s", hdev->name);
850 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
852 struct btusb_data *data = hdev->driver_data;
854 BT_DBG("%s evt %d", hdev->name, evt);
856 if (hdev->conn_hash.sco_num != data->sco_num) {
857 data->sco_num = hdev->conn_hash.sco_num;
858 schedule_work(&data->work);
862 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
864 struct btusb_data *data = hdev->driver_data;
865 struct usb_interface *intf = data->isoc;
866 struct usb_endpoint_descriptor *ep_desc;
872 err = usb_set_interface(data->udev, 1, altsetting);
874 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
878 data->isoc_altsetting = altsetting;
880 data->isoc_tx_ep = NULL;
881 data->isoc_rx_ep = NULL;
883 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
884 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
886 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
887 data->isoc_tx_ep = ep_desc;
891 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
892 data->isoc_rx_ep = ep_desc;
897 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
898 BT_ERR("%s invalid SCO descriptors", hdev->name);
905 static void btusb_work(struct work_struct *work)
907 struct btusb_data *data = container_of(work, struct btusb_data, work);
908 struct hci_dev *hdev = data->hdev;
911 if (hdev->conn_hash.sco_num > 0) {
912 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
913 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
915 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
916 usb_kill_anchored_urbs(&data->isoc_anchor);
920 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
922 if (data->isoc_altsetting != 2) {
923 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
924 usb_kill_anchored_urbs(&data->isoc_anchor);
926 if (__set_isoc_interface(hdev, 2) < 0)
930 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
931 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
932 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
934 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
937 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
938 usb_kill_anchored_urbs(&data->isoc_anchor);
940 __set_isoc_interface(hdev, 0);
941 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
942 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
946 static void btusb_waker(struct work_struct *work)
948 struct btusb_data *data = container_of(work, struct btusb_data, waker);
951 err = usb_autopm_get_interface(data->intf);
955 usb_autopm_put_interface(data->intf);
958 static int btusb_probe(struct usb_interface *intf,
959 const struct usb_device_id *id)
961 struct usb_endpoint_descriptor *ep_desc;
962 struct btusb_data *data;
963 struct hci_dev *hdev;
966 BT_DBG("intf %p id %p", intf, id);
968 /* interface numbers are hardcoded in the spec */
969 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
972 if (!id->driver_info) {
973 const struct usb_device_id *match;
974 match = usb_match_id(intf, blacklist_table);
979 if (id->driver_info == BTUSB_IGNORE)
982 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
985 if (ignore_csr && id->driver_info & BTUSB_CSR)
988 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
991 if (id->driver_info & BTUSB_ATH3012) {
992 struct usb_device *udev = interface_to_usbdev(intf);
994 /* Old firmware would otherwise let ath3k driver load
995 * patch and sysconfig files */
996 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
1000 data = kzalloc(sizeof(*data), GFP_KERNEL);
1004 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1005 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1007 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
1008 data->intr_ep = ep_desc;
1012 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
1013 data->bulk_tx_ep = ep_desc;
1017 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
1018 data->bulk_rx_ep = ep_desc;
1023 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
1028 data->cmdreq_type = USB_TYPE_CLASS;
1030 data->udev = interface_to_usbdev(intf);
1033 spin_lock_init(&data->lock);
1035 INIT_WORK(&data->work, btusb_work);
1036 INIT_WORK(&data->waker, btusb_waker);
1037 spin_lock_init(&data->txlock);
1039 init_usb_anchor(&data->tx_anchor);
1040 init_usb_anchor(&data->intr_anchor);
1041 init_usb_anchor(&data->bulk_anchor);
1042 init_usb_anchor(&data->isoc_anchor);
1043 init_usb_anchor(&data->deferred);
1045 hdev = hci_alloc_dev();
1051 hdev->bus = HCI_USB;
1052 hdev->driver_data = data;
1056 SET_HCIDEV_DEV(hdev, &intf->dev);
1058 hdev->open = btusb_open;
1059 hdev->close = btusb_close;
1060 hdev->flush = btusb_flush;
1061 hdev->send = btusb_send_frame;
1062 hdev->destruct = btusb_destruct;
1063 hdev->notify = btusb_notify;
1065 hdev->owner = THIS_MODULE;
1067 /* Interface numbers are hardcoded in the specification */
1068 data->isoc = usb_ifnum_to_if(data->udev, 1);
1071 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1073 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
1074 if (!disable_scofix)
1075 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1078 if (id->driver_info & BTUSB_BROKEN_ISOC)
1081 if (id->driver_info & BTUSB_DIGIANSWER) {
1082 data->cmdreq_type = USB_TYPE_VENDOR;
1083 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1086 if (id->driver_info & BTUSB_CSR) {
1087 struct usb_device *udev = data->udev;
1089 /* Old firmware would otherwise execute USB reset */
1090 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
1091 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1094 if (id->driver_info & BTUSB_SNIFFER) {
1095 struct usb_device *udev = data->udev;
1097 /* New sniffer firmware has crippled HCI interface */
1098 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1099 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
1104 if (id->driver_info & BTUSB_BCM92035) {
1105 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1106 struct sk_buff *skb;
1108 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1110 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1111 skb_queue_tail(&hdev->driver_init, skb);
1116 err = usb_driver_claim_interface(&btusb_driver,
1125 err = hci_register_dev(hdev);
1132 usb_set_intfdata(intf, data);
1137 static void btusb_disconnect(struct usb_interface *intf)
1139 struct btusb_data *data = usb_get_intfdata(intf);
1140 struct hci_dev *hdev;
1142 BT_DBG("intf %p", intf);
1149 __hci_dev_hold(hdev);
1151 usb_set_intfdata(data->intf, NULL);
1154 usb_set_intfdata(data->isoc, NULL);
1156 hci_unregister_dev(hdev);
1158 if (intf == data->isoc)
1159 usb_driver_release_interface(&btusb_driver, data->intf);
1160 else if (data->isoc)
1161 usb_driver_release_interface(&btusb_driver, data->isoc);
1163 __hci_dev_put(hdev);
1169 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1171 struct btusb_data *data = usb_get_intfdata(intf);
1173 BT_DBG("intf %p", intf);
1175 if (data->suspend_count++)
1178 spin_lock_irq(&data->txlock);
1179 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
1180 set_bit(BTUSB_SUSPENDING, &data->flags);
1181 spin_unlock_irq(&data->txlock);
1183 spin_unlock_irq(&data->txlock);
1184 data->suspend_count--;
1188 cancel_work_sync(&data->work);
1190 btusb_stop_traffic(data);
1191 usb_kill_anchored_urbs(&data->tx_anchor);
1196 static void play_deferred(struct btusb_data *data)
1201 while ((urb = usb_get_from_anchor(&data->deferred))) {
1202 err = usb_submit_urb(urb, GFP_ATOMIC);
1206 data->tx_in_flight++;
1208 usb_scuttle_anchored_urbs(&data->deferred);
1211 static int btusb_resume(struct usb_interface *intf)
1213 struct btusb_data *data = usb_get_intfdata(intf);
1214 struct hci_dev *hdev = data->hdev;
1217 BT_DBG("intf %p", intf);
1219 if (--data->suspend_count)
1222 if (!test_bit(HCI_RUNNING, &hdev->flags))
1225 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1226 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1228 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1233 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
1234 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1236 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1240 btusb_submit_bulk_urb(hdev, GFP_NOIO);
1243 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1244 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1245 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1247 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1250 spin_lock_irq(&data->txlock);
1251 play_deferred(data);
1252 clear_bit(BTUSB_SUSPENDING, &data->flags);
1253 spin_unlock_irq(&data->txlock);
1254 schedule_work(&data->work);
1259 usb_scuttle_anchored_urbs(&data->deferred);
1261 spin_lock_irq(&data->txlock);
1262 clear_bit(BTUSB_SUSPENDING, &data->flags);
1263 spin_unlock_irq(&data->txlock);
1269 static struct usb_driver btusb_driver = {
1271 .probe = btusb_probe,
1272 .disconnect = btusb_disconnect,
1274 .suspend = btusb_suspend,
1275 .resume = btusb_resume,
1277 .id_table = btusb_table,
1278 .supports_autosuspend = 1,
1281 static int __init btusb_init(void)
1283 BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
1285 return usb_register(&btusb_driver);
1288 static void __exit btusb_exit(void)
1290 usb_deregister(&btusb_driver);
1293 module_init(btusb_init);
1294 module_exit(btusb_exit);
1296 module_param(ignore_dga, bool, 0644);
1297 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1299 module_param(ignore_csr, bool, 0644);
1300 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1302 module_param(ignore_sniffer, bool, 0644);
1303 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1305 module_param(disable_scofix, bool, 0644);
1306 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1308 module_param(force_scofix, bool, 0644);
1309 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1311 module_param(reset, bool, 0644);
1312 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1314 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1315 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1316 MODULE_VERSION(VERSION);
1317 MODULE_LICENSE("GPL");