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 { } /* Terminating entry */
125 MODULE_DEVICE_TABLE(usb, btusb_table);
127 static struct usb_device_id blacklist_table[] = {
128 /* CSR BlueCore devices */
129 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
131 /* Broadcom BCM2033 without firmware */
132 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
134 /* Atheros 3011 with sflash firmware */
135 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
136 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
137 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
138 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
139 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
140 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
142 /* Atheros AR9285 Malbec with sflash firmware */
143 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
145 /* Atheros 3012 with sflash firmware */
146 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
147 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
148 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
149 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
150 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
151 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
152 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
153 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
154 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
155 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
156 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
157 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
158 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
159 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
160 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
161 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
162 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
163 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
164 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
165 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
166 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
167 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
168 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
170 /* Atheros AR5BBU12 with sflash firmware */
171 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
173 /* Atheros AR5BBU12 with sflash firmware */
174 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
175 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
177 /* Broadcom BCM2035 */
178 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
179 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
180 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
182 /* Broadcom BCM2045 */
183 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
184 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
186 /* IBM/Lenovo ThinkPad with Broadcom chip */
187 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
188 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
190 /* HP laptop with Broadcom chip */
191 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
193 /* Dell laptop with Broadcom chip */
194 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
196 /* Dell Wireless 370 and 410 devices */
197 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
198 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
200 /* Belkin F8T012 and F8T013 devices */
201 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
202 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
204 /* Asus WL-BTD202 device */
205 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
207 /* Kensington Bluetooth USB adapter */
208 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
210 /* RTX Telecom based adapters with buggy SCO support */
211 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
212 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
214 /* CONWISE Technology based adapters with buggy SCO support */
215 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
217 /* Digianswer devices */
218 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
219 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
221 /* CSR BlueCore Bluetooth Sniffer */
222 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
224 /* Frontline ComProbe Bluetooth Sniffer */
225 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
227 { } /* Terminating entry */
230 #define BTUSB_MAX_ISOC_FRAMES 10
232 #define BTUSB_INTR_RUNNING 0
233 #define BTUSB_BULK_RUNNING 1
234 #define BTUSB_ISOC_RUNNING 2
235 #define BTUSB_SUSPENDING 3
236 #define BTUSB_DID_ISO_RESUME 4
239 struct hci_dev *hdev;
240 struct usb_device *udev;
241 struct usb_interface *intf;
242 struct usb_interface *isoc;
248 struct work_struct work;
249 struct work_struct waker;
251 struct usb_anchor tx_anchor;
252 struct usb_anchor intr_anchor;
253 struct usb_anchor bulk_anchor;
254 struct usb_anchor isoc_anchor;
255 struct usb_anchor deferred;
259 struct usb_endpoint_descriptor *intr_ep;
260 struct usb_endpoint_descriptor *bulk_tx_ep;
261 struct usb_endpoint_descriptor *bulk_rx_ep;
262 struct usb_endpoint_descriptor *isoc_tx_ep;
263 struct usb_endpoint_descriptor *isoc_rx_ep;
267 unsigned int sco_num;
272 static int inc_tx(struct btusb_data *data)
277 spin_lock_irqsave(&data->txlock, flags);
278 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
280 data->tx_in_flight++;
281 spin_unlock_irqrestore(&data->txlock, flags);
286 static void btusb_intr_complete(struct urb *urb)
288 struct hci_dev *hdev = urb->context;
289 struct btusb_data *data = hdev->driver_data;
292 BT_DBG("%s urb %p status %d count %d", hdev->name,
293 urb, urb->status, urb->actual_length);
295 if (!test_bit(HCI_RUNNING, &hdev->flags))
298 if (urb->status == 0) {
299 hdev->stat.byte_rx += urb->actual_length;
301 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
302 urb->transfer_buffer,
303 urb->actual_length) < 0) {
304 BT_ERR("%s corrupted event packet", hdev->name);
309 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
312 usb_mark_last_busy(data->udev);
313 usb_anchor_urb(urb, &data->intr_anchor);
315 err = usb_submit_urb(urb, GFP_ATOMIC);
317 /* -EPERM: urb is being killed;
318 * -ENODEV: device got disconnected */
319 if (err != -EPERM && err != -ENODEV)
320 BT_ERR("%s urb %p failed to resubmit (%d)",
321 hdev->name, urb, -err);
322 usb_unanchor_urb(urb);
326 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
328 struct btusb_data *data = hdev->driver_data;
334 BT_DBG("%s", hdev->name);
339 urb = usb_alloc_urb(0, mem_flags);
343 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
345 buf = kmalloc(size, mem_flags);
351 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
353 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
354 btusb_intr_complete, hdev,
355 data->intr_ep->bInterval);
357 urb->transfer_flags |= URB_FREE_BUFFER;
359 usb_anchor_urb(urb, &data->intr_anchor);
361 err = usb_submit_urb(urb, mem_flags);
363 BT_ERR("%s urb %p submission failed (%d)",
364 hdev->name, urb, -err);
365 usb_unanchor_urb(urb);
373 static void btusb_bulk_complete(struct urb *urb)
375 struct hci_dev *hdev = urb->context;
376 struct btusb_data *data = hdev->driver_data;
379 BT_DBG("%s urb %p status %d count %d", hdev->name,
380 urb, urb->status, urb->actual_length);
382 if (!test_bit(HCI_RUNNING, &hdev->flags))
385 if (urb->status == 0) {
386 hdev->stat.byte_rx += urb->actual_length;
388 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
389 urb->transfer_buffer,
390 urb->actual_length) < 0) {
391 BT_ERR("%s corrupted ACL packet", hdev->name);
396 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
399 usb_anchor_urb(urb, &data->bulk_anchor);
400 usb_mark_last_busy(data->udev);
402 err = usb_submit_urb(urb, GFP_ATOMIC);
404 /* -EPERM: urb is being killed;
405 * -ENODEV: device got disconnected */
406 if (err != -EPERM && err != -ENODEV)
407 BT_ERR("%s urb %p failed to resubmit (%d)",
408 hdev->name, urb, -err);
409 usb_unanchor_urb(urb);
413 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
415 struct btusb_data *data = hdev->driver_data;
419 int err, size = HCI_MAX_FRAME_SIZE;
421 BT_DBG("%s", hdev->name);
423 if (!data->bulk_rx_ep)
426 urb = usb_alloc_urb(0, mem_flags);
430 buf = kmalloc(size, mem_flags);
436 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
438 usb_fill_bulk_urb(urb, data->udev, pipe,
439 buf, size, btusb_bulk_complete, hdev);
441 urb->transfer_flags |= URB_FREE_BUFFER;
443 usb_mark_last_busy(data->udev);
444 usb_anchor_urb(urb, &data->bulk_anchor);
446 err = usb_submit_urb(urb, mem_flags);
448 BT_ERR("%s urb %p submission failed (%d)",
449 hdev->name, urb, -err);
450 usb_unanchor_urb(urb);
458 static void btusb_isoc_complete(struct urb *urb)
460 struct hci_dev *hdev = urb->context;
461 struct btusb_data *data = hdev->driver_data;
464 BT_DBG("%s urb %p status %d count %d", hdev->name,
465 urb, urb->status, urb->actual_length);
467 if (!test_bit(HCI_RUNNING, &hdev->flags))
470 if (urb->status == 0) {
471 for (i = 0; i < urb->number_of_packets; i++) {
472 unsigned int offset = urb->iso_frame_desc[i].offset;
473 unsigned int length = urb->iso_frame_desc[i].actual_length;
475 if (urb->iso_frame_desc[i].status)
478 hdev->stat.byte_rx += length;
480 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
481 urb->transfer_buffer + offset,
483 BT_ERR("%s corrupted SCO packet", hdev->name);
489 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
492 usb_anchor_urb(urb, &data->isoc_anchor);
494 err = usb_submit_urb(urb, GFP_ATOMIC);
496 /* -EPERM: urb is being killed;
497 * -ENODEV: device got disconnected */
498 if (err != -EPERM && err != -ENODEV)
499 BT_ERR("%s urb %p failed to resubmit (%d)",
500 hdev->name, urb, -err);
501 usb_unanchor_urb(urb);
505 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
509 BT_DBG("len %d mtu %d", len, mtu);
511 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
512 i++, offset += mtu, len -= mtu) {
513 urb->iso_frame_desc[i].offset = offset;
514 urb->iso_frame_desc[i].length = mtu;
517 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
518 urb->iso_frame_desc[i].offset = offset;
519 urb->iso_frame_desc[i].length = len;
523 urb->number_of_packets = i;
526 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
528 struct btusb_data *data = hdev->driver_data;
534 BT_DBG("%s", hdev->name);
536 if (!data->isoc_rx_ep)
539 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
543 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
544 BTUSB_MAX_ISOC_FRAMES;
546 buf = kmalloc(size, mem_flags);
552 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
554 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
555 hdev, data->isoc_rx_ep->bInterval);
557 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
559 __fill_isoc_descriptor(urb, size,
560 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
562 usb_anchor_urb(urb, &data->isoc_anchor);
564 err = usb_submit_urb(urb, mem_flags);
566 BT_ERR("%s urb %p submission failed (%d)",
567 hdev->name, urb, -err);
568 usb_unanchor_urb(urb);
576 static void btusb_tx_complete(struct urb *urb)
578 struct sk_buff *skb = urb->context;
579 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
580 struct btusb_data *data = hdev->driver_data;
582 BT_DBG("%s urb %p status %d count %d", hdev->name,
583 urb, urb->status, urb->actual_length);
585 if (!test_bit(HCI_RUNNING, &hdev->flags))
589 hdev->stat.byte_tx += urb->transfer_buffer_length;
594 spin_lock(&data->txlock);
595 data->tx_in_flight--;
596 spin_unlock(&data->txlock);
598 kfree(urb->setup_packet);
603 static void btusb_isoc_tx_complete(struct urb *urb)
605 struct sk_buff *skb = urb->context;
606 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
608 BT_DBG("%s urb %p status %d count %d", hdev->name,
609 urb, urb->status, urb->actual_length);
611 if (!test_bit(HCI_RUNNING, &hdev->flags))
615 hdev->stat.byte_tx += urb->transfer_buffer_length;
620 kfree(urb->setup_packet);
625 static int btusb_open(struct hci_dev *hdev)
627 struct btusb_data *data = hdev->driver_data;
630 BT_DBG("%s", hdev->name);
632 err = usb_autopm_get_interface(data->intf);
636 data->intf->needs_remote_wakeup = 1;
638 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
641 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
644 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
648 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
650 usb_kill_anchored_urbs(&data->intr_anchor);
654 set_bit(BTUSB_BULK_RUNNING, &data->flags);
655 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
658 usb_autopm_put_interface(data->intf);
662 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
663 clear_bit(HCI_RUNNING, &hdev->flags);
664 usb_autopm_put_interface(data->intf);
668 static void btusb_stop_traffic(struct btusb_data *data)
670 usb_kill_anchored_urbs(&data->intr_anchor);
671 usb_kill_anchored_urbs(&data->bulk_anchor);
672 usb_kill_anchored_urbs(&data->isoc_anchor);
675 static int btusb_close(struct hci_dev *hdev)
677 struct btusb_data *data = hdev->driver_data;
680 BT_DBG("%s", hdev->name);
682 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
685 cancel_work_sync(&data->work);
686 cancel_work_sync(&data->waker);
688 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
689 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
690 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
692 btusb_stop_traffic(data);
693 err = usb_autopm_get_interface(data->intf);
697 data->intf->needs_remote_wakeup = 0;
698 usb_autopm_put_interface(data->intf);
701 usb_scuttle_anchored_urbs(&data->deferred);
705 static int btusb_flush(struct hci_dev *hdev)
707 struct btusb_data *data = hdev->driver_data;
709 BT_DBG("%s", hdev->name);
711 usb_kill_anchored_urbs(&data->tx_anchor);
716 static int btusb_send_frame(struct sk_buff *skb)
718 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
719 struct btusb_data *data = hdev->driver_data;
720 struct usb_ctrlrequest *dr;
725 BT_DBG("%s", hdev->name);
727 if (!test_bit(HCI_RUNNING, &hdev->flags))
730 switch (bt_cb(skb)->pkt_type) {
731 case HCI_COMMAND_PKT:
732 urb = usb_alloc_urb(0, GFP_ATOMIC);
736 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
742 dr->bRequestType = data->cmdreq_type;
746 dr->wLength = __cpu_to_le16(skb->len);
748 pipe = usb_sndctrlpipe(data->udev, 0x00);
750 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
751 skb->data, skb->len, btusb_tx_complete, skb);
756 case HCI_ACLDATA_PKT:
757 if (!data->bulk_tx_ep)
760 urb = usb_alloc_urb(0, GFP_ATOMIC);
764 pipe = usb_sndbulkpipe(data->udev,
765 data->bulk_tx_ep->bEndpointAddress);
767 usb_fill_bulk_urb(urb, data->udev, pipe,
768 skb->data, skb->len, btusb_tx_complete, skb);
773 case HCI_SCODATA_PKT:
774 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
777 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
781 pipe = usb_sndisocpipe(data->udev,
782 data->isoc_tx_ep->bEndpointAddress);
784 usb_fill_int_urb(urb, data->udev, pipe,
785 skb->data, skb->len, btusb_isoc_tx_complete,
786 skb, data->isoc_tx_ep->bInterval);
788 urb->transfer_flags = URB_ISO_ASAP;
790 __fill_isoc_descriptor(urb, skb->len,
791 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
802 usb_anchor_urb(urb, &data->deferred);
803 schedule_work(&data->waker);
809 usb_anchor_urb(urb, &data->tx_anchor);
811 err = usb_submit_urb(urb, GFP_ATOMIC);
813 BT_ERR("%s urb %p submission failed", hdev->name, urb);
814 kfree(urb->setup_packet);
815 usb_unanchor_urb(urb);
817 usb_mark_last_busy(data->udev);
825 static void btusb_destruct(struct hci_dev *hdev)
827 struct btusb_data *data = hdev->driver_data;
829 BT_DBG("%s", hdev->name);
834 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
836 struct btusb_data *data = hdev->driver_data;
838 BT_DBG("%s evt %d", hdev->name, evt);
840 if (hdev->conn_hash.sco_num != data->sco_num) {
841 data->sco_num = hdev->conn_hash.sco_num;
842 schedule_work(&data->work);
846 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
848 struct btusb_data *data = hdev->driver_data;
849 struct usb_interface *intf = data->isoc;
850 struct usb_endpoint_descriptor *ep_desc;
856 err = usb_set_interface(data->udev, 1, altsetting);
858 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
862 data->isoc_altsetting = altsetting;
864 data->isoc_tx_ep = NULL;
865 data->isoc_rx_ep = NULL;
867 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
868 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
870 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
871 data->isoc_tx_ep = ep_desc;
875 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
876 data->isoc_rx_ep = ep_desc;
881 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
882 BT_ERR("%s invalid SCO descriptors", hdev->name);
889 static void btusb_work(struct work_struct *work)
891 struct btusb_data *data = container_of(work, struct btusb_data, work);
892 struct hci_dev *hdev = data->hdev;
895 if (hdev->conn_hash.sco_num > 0) {
896 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
897 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
899 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
900 usb_kill_anchored_urbs(&data->isoc_anchor);
904 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
906 if (data->isoc_altsetting != 2) {
907 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
908 usb_kill_anchored_urbs(&data->isoc_anchor);
910 if (__set_isoc_interface(hdev, 2) < 0)
914 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
915 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
916 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
918 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
921 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
922 usb_kill_anchored_urbs(&data->isoc_anchor);
924 __set_isoc_interface(hdev, 0);
925 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
926 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
930 static void btusb_waker(struct work_struct *work)
932 struct btusb_data *data = container_of(work, struct btusb_data, waker);
935 err = usb_autopm_get_interface(data->intf);
939 usb_autopm_put_interface(data->intf);
942 static int btusb_probe(struct usb_interface *intf,
943 const struct usb_device_id *id)
945 struct usb_endpoint_descriptor *ep_desc;
946 struct btusb_data *data;
947 struct hci_dev *hdev;
950 BT_DBG("intf %p id %p", intf, id);
952 /* interface numbers are hardcoded in the spec */
953 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
956 if (!id->driver_info) {
957 const struct usb_device_id *match;
958 match = usb_match_id(intf, blacklist_table);
963 if (id->driver_info == BTUSB_IGNORE)
966 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
969 if (ignore_csr && id->driver_info & BTUSB_CSR)
972 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
975 if (id->driver_info & BTUSB_ATH3012) {
976 struct usb_device *udev = interface_to_usbdev(intf);
978 /* Old firmware would otherwise let ath3k driver load
979 * patch and sysconfig files */
980 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
984 data = kzalloc(sizeof(*data), GFP_KERNEL);
988 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
989 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
991 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
992 data->intr_ep = ep_desc;
996 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
997 data->bulk_tx_ep = ep_desc;
1001 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
1002 data->bulk_rx_ep = ep_desc;
1007 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
1012 data->cmdreq_type = USB_TYPE_CLASS;
1014 data->udev = interface_to_usbdev(intf);
1017 spin_lock_init(&data->lock);
1019 INIT_WORK(&data->work, btusb_work);
1020 INIT_WORK(&data->waker, btusb_waker);
1021 spin_lock_init(&data->txlock);
1023 init_usb_anchor(&data->tx_anchor);
1024 init_usb_anchor(&data->intr_anchor);
1025 init_usb_anchor(&data->bulk_anchor);
1026 init_usb_anchor(&data->isoc_anchor);
1027 init_usb_anchor(&data->deferred);
1029 hdev = hci_alloc_dev();
1035 hdev->bus = HCI_USB;
1036 hdev->driver_data = data;
1040 SET_HCIDEV_DEV(hdev, &intf->dev);
1042 hdev->open = btusb_open;
1043 hdev->close = btusb_close;
1044 hdev->flush = btusb_flush;
1045 hdev->send = btusb_send_frame;
1046 hdev->destruct = btusb_destruct;
1047 hdev->notify = btusb_notify;
1049 hdev->owner = THIS_MODULE;
1051 /* Interface numbers are hardcoded in the specification */
1052 data->isoc = usb_ifnum_to_if(data->udev, 1);
1055 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1057 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
1058 if (!disable_scofix)
1059 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1062 if (id->driver_info & BTUSB_BROKEN_ISOC)
1065 if (id->driver_info & BTUSB_DIGIANSWER) {
1066 data->cmdreq_type = USB_TYPE_VENDOR;
1067 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1070 if (id->driver_info & BTUSB_CSR) {
1071 struct usb_device *udev = data->udev;
1073 /* Old firmware would otherwise execute USB reset */
1074 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
1075 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1078 if (id->driver_info & BTUSB_SNIFFER) {
1079 struct usb_device *udev = data->udev;
1081 /* New sniffer firmware has crippled HCI interface */
1082 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1083 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
1088 if (id->driver_info & BTUSB_BCM92035) {
1089 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1090 struct sk_buff *skb;
1092 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1094 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1095 skb_queue_tail(&hdev->driver_init, skb);
1100 err = usb_driver_claim_interface(&btusb_driver,
1109 err = hci_register_dev(hdev);
1116 usb_set_intfdata(intf, data);
1121 static void btusb_disconnect(struct usb_interface *intf)
1123 struct btusb_data *data = usb_get_intfdata(intf);
1124 struct hci_dev *hdev;
1126 BT_DBG("intf %p", intf);
1133 __hci_dev_hold(hdev);
1135 usb_set_intfdata(data->intf, NULL);
1138 usb_set_intfdata(data->isoc, NULL);
1140 hci_unregister_dev(hdev);
1142 if (intf == data->isoc)
1143 usb_driver_release_interface(&btusb_driver, data->intf);
1144 else if (data->isoc)
1145 usb_driver_release_interface(&btusb_driver, data->isoc);
1147 __hci_dev_put(hdev);
1153 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1155 struct btusb_data *data = usb_get_intfdata(intf);
1157 BT_DBG("intf %p", intf);
1159 if (data->suspend_count++)
1162 spin_lock_irq(&data->txlock);
1163 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
1164 set_bit(BTUSB_SUSPENDING, &data->flags);
1165 spin_unlock_irq(&data->txlock);
1167 spin_unlock_irq(&data->txlock);
1168 data->suspend_count--;
1172 cancel_work_sync(&data->work);
1174 btusb_stop_traffic(data);
1175 usb_kill_anchored_urbs(&data->tx_anchor);
1180 static void play_deferred(struct btusb_data *data)
1185 while ((urb = usb_get_from_anchor(&data->deferred))) {
1186 err = usb_submit_urb(urb, GFP_ATOMIC);
1190 data->tx_in_flight++;
1192 usb_scuttle_anchored_urbs(&data->deferred);
1195 static int btusb_resume(struct usb_interface *intf)
1197 struct btusb_data *data = usb_get_intfdata(intf);
1198 struct hci_dev *hdev = data->hdev;
1201 BT_DBG("intf %p", intf);
1203 if (--data->suspend_count)
1206 if (!test_bit(HCI_RUNNING, &hdev->flags))
1209 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1210 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1212 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1217 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
1218 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1220 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1224 btusb_submit_bulk_urb(hdev, GFP_NOIO);
1227 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1228 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1229 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1231 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1234 spin_lock_irq(&data->txlock);
1235 play_deferred(data);
1236 clear_bit(BTUSB_SUSPENDING, &data->flags);
1237 spin_unlock_irq(&data->txlock);
1238 schedule_work(&data->work);
1243 usb_scuttle_anchored_urbs(&data->deferred);
1245 spin_lock_irq(&data->txlock);
1246 clear_bit(BTUSB_SUSPENDING, &data->flags);
1247 spin_unlock_irq(&data->txlock);
1253 static struct usb_driver btusb_driver = {
1255 .probe = btusb_probe,
1256 .disconnect = btusb_disconnect,
1258 .suspend = btusb_suspend,
1259 .resume = btusb_resume,
1261 .id_table = btusb_table,
1262 .supports_autosuspend = 1,
1265 static int __init btusb_init(void)
1267 BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
1269 return usb_register(&btusb_driver);
1272 static void __exit btusb_exit(void)
1274 usb_deregister(&btusb_driver);
1277 module_init(btusb_init);
1278 module_exit(btusb_exit);
1280 module_param(ignore_dga, bool, 0644);
1281 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1283 module_param(ignore_csr, bool, 0644);
1284 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1286 module_param(ignore_sniffer, bool, 0644);
1287 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1289 module_param(disable_scofix, bool, 0644);
1290 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1292 module_param(force_scofix, bool, 0644);
1293 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1295 module_param(reset, bool, 0644);
1296 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1298 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1299 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1300 MODULE_VERSION(VERSION);
1301 MODULE_LICENSE("GPL");