3 * Generic Bluetooth USB driver
5 * Copyright (C) 2005-2007 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>
38 //#define CONFIG_BT_HCIBTUSB_DEBUG
39 #ifndef CONFIG_BT_HCIBTUSB_DEBUG
46 static int ignore_dga;
47 static int ignore_csr;
48 static int ignore_sniffer;
49 static int disable_scofix;
50 static int force_scofix;
53 static struct usb_driver btusb_driver;
55 #define BTUSB_IGNORE 0x01
56 #define BTUSB_RESET 0x02
57 #define BTUSB_DIGIANSWER 0x04
58 #define BTUSB_CSR 0x08
59 #define BTUSB_SNIFFER 0x10
60 #define BTUSB_BCM92035 0x20
61 #define BTUSB_BROKEN_ISOC 0x40
62 #define BTUSB_WRONG_SCO_MTU 0x80
64 static struct usb_device_id btusb_table[] = {
65 /* Generic Bluetooth USB device */
66 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
68 /* AVM BlueFRITZ! USB v2.0 */
69 { USB_DEVICE(0x057c, 0x3800) },
71 /* Bluetooth Ultraport Module from IBM */
72 { USB_DEVICE(0x04bf, 0x030a) },
74 /* ALPS Modules with non-standard id */
75 { USB_DEVICE(0x044e, 0x3001) },
76 { USB_DEVICE(0x044e, 0x3002) },
78 /* Ericsson with non-standard id */
79 { USB_DEVICE(0x0bdb, 0x1002) },
81 /* Canyon CN-BTU1 with HID interfaces */
82 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_RESET },
84 { } /* Terminating entry */
87 MODULE_DEVICE_TABLE(usb, btusb_table);
89 static struct usb_device_id blacklist_table[] = {
90 /* CSR BlueCore devices */
91 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
93 /* Broadcom BCM2033 without firmware */
94 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
96 /* Broadcom BCM2035 */
97 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
98 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
100 /* Broadcom BCM2045 */
101 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
102 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
104 /* Broadcom BCM2046 */
105 { USB_DEVICE(0x0a5c, 0x2151), .driver_info = BTUSB_RESET },
107 /* IBM/Lenovo ThinkPad with Broadcom chip */
108 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
109 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
112 { USB_DEVICE(0x0a5c, 0x2100), .driver_info = BTUSB_RESET },
114 /* ANYCOM Bluetooth USB-200 and USB-250 */
115 { USB_DEVICE(0x0a5c, 0x2111), .driver_info = BTUSB_RESET },
117 /* HP laptop with Broadcom chip */
118 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
120 /* Dell laptop with Broadcom chip */
121 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
123 /* Dell Wireless 370 */
124 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
126 /* Dell Wireless 410 */
127 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
129 /* Microsoft Wireless Transceiver for Bluetooth 2.0 */
130 { USB_DEVICE(0x045e, 0x009c), .driver_info = BTUSB_RESET },
132 /* Kensington Bluetooth USB adapter */
133 { USB_DEVICE(0x047d, 0x105d), .driver_info = BTUSB_RESET },
134 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
136 /* ISSC Bluetooth Adapter v3.1 */
137 { USB_DEVICE(0x1131, 0x1001), .driver_info = BTUSB_RESET },
139 /* RTX Telecom based adapters with buggy SCO support */
140 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
141 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
143 /* CONWISE Technology based adapters with buggy SCO support */
144 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
146 /* Belkin F8T012 and F8T013 devices */
147 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
148 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
150 /* Digianswer devices */
151 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
152 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
154 /* CSR BlueCore Bluetooth Sniffer */
155 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
157 /* Frontline ComProbe Bluetooth Sniffer */
158 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
160 { } /* Terminating entry */
163 #define BTUSB_INTR_RUNNING 0
164 #define BTUSB_BULK_RUNNING 1
167 struct hci_dev *hdev;
168 struct usb_device *udev;
174 struct work_struct work;
176 struct usb_anchor tx_anchor;
177 struct usb_anchor intr_anchor;
178 struct usb_anchor bulk_anchor;
180 struct usb_endpoint_descriptor *intr_ep;
181 struct usb_endpoint_descriptor *bulk_tx_ep;
182 struct usb_endpoint_descriptor *bulk_rx_ep;
185 static void btusb_intr_complete(struct urb *urb)
187 struct hci_dev *hdev = urb->context;
188 struct btusb_data *data = hdev->driver_data;
191 BT_DBG("%s urb %p status %d count %d", hdev->name,
192 urb, urb->status, urb->actual_length);
194 if (!test_bit(HCI_RUNNING, &hdev->flags))
197 if (urb->status == 0) {
198 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
199 urb->transfer_buffer,
200 urb->actual_length) < 0) {
201 BT_ERR("%s corrupted event packet", hdev->name);
206 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
209 usb_anchor_urb(urb, &data->intr_anchor);
211 err = usb_submit_urb(urb, GFP_ATOMIC);
213 BT_ERR("%s urb %p failed to resubmit (%d)",
214 hdev->name, urb, -err);
215 usb_unanchor_urb(urb);
219 static inline int btusb_submit_intr_urb(struct hci_dev *hdev)
221 struct btusb_data *data = hdev->driver_data;
227 BT_DBG("%s", hdev->name);
229 urb = usb_alloc_urb(0, GFP_ATOMIC);
233 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
235 buf = kmalloc(size, GFP_ATOMIC);
241 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
243 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
244 btusb_intr_complete, hdev,
245 data->intr_ep->bInterval);
247 urb->transfer_flags |= URB_FREE_BUFFER;
249 usb_anchor_urb(urb, &data->intr_anchor);
251 err = usb_submit_urb(urb, GFP_ATOMIC);
253 BT_ERR("%s urb %p submission failed (%d)",
254 hdev->name, urb, -err);
255 usb_unanchor_urb(urb);
264 static void btusb_bulk_complete(struct urb *urb)
266 struct hci_dev *hdev = urb->context;
267 struct btusb_data *data = hdev->driver_data;
270 BT_DBG("%s urb %p status %d count %d", hdev->name,
271 urb, urb->status, urb->actual_length);
273 if (!test_bit(HCI_RUNNING, &hdev->flags))
276 if (urb->status == 0) {
277 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
278 urb->transfer_buffer,
279 urb->actual_length) < 0) {
280 BT_ERR("%s corrupted ACL packet", hdev->name);
285 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
288 usb_anchor_urb(urb, &data->bulk_anchor);
290 err = usb_submit_urb(urb, GFP_ATOMIC);
292 BT_ERR("%s urb %p failed to resubmit (%d)",
293 hdev->name, urb, -err);
294 usb_unanchor_urb(urb);
298 static inline int btusb_submit_bulk_urb(struct hci_dev *hdev)
300 struct btusb_data *data = hdev->driver_data;
306 BT_DBG("%s", hdev->name);
308 urb = usb_alloc_urb(0, GFP_KERNEL);
312 size = le16_to_cpu(data->bulk_rx_ep->wMaxPacketSize);
314 buf = kmalloc(size, GFP_KERNEL);
320 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
322 usb_fill_bulk_urb(urb, data->udev, pipe,
323 buf, size, btusb_bulk_complete, hdev);
325 urb->transfer_flags |= URB_FREE_BUFFER;
327 usb_anchor_urb(urb, &data->bulk_anchor);
329 err = usb_submit_urb(urb, GFP_KERNEL);
331 BT_ERR("%s urb %p submission failed (%d)",
332 hdev->name, urb, -err);
333 usb_unanchor_urb(urb);
342 static void btusb_tx_complete(struct urb *urb)
344 struct sk_buff *skb = urb->context;
345 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
347 BT_DBG("%s urb %p status %d count %d", hdev->name,
348 urb, urb->status, urb->actual_length);
350 if (!test_bit(HCI_RUNNING, &hdev->flags))
354 hdev->stat.byte_tx += urb->transfer_buffer_length;
359 kfree(urb->setup_packet);
364 static int btusb_open(struct hci_dev *hdev)
366 struct btusb_data *data = hdev->driver_data;
369 BT_DBG("%s", hdev->name);
371 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
374 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
377 err = btusb_submit_intr_urb(hdev);
379 clear_bit(BTUSB_INTR_RUNNING, &hdev->flags);
380 clear_bit(HCI_RUNNING, &hdev->flags);
386 static int btusb_close(struct hci_dev *hdev)
388 struct btusb_data *data = hdev->driver_data;
390 BT_DBG("%s", hdev->name);
392 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
395 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
396 usb_kill_anchored_urbs(&data->bulk_anchor);
398 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
399 usb_kill_anchored_urbs(&data->intr_anchor);
404 static int btusb_flush(struct hci_dev *hdev)
406 struct btusb_data *data = hdev->driver_data;
408 BT_DBG("%s", hdev->name);
410 usb_kill_anchored_urbs(&data->tx_anchor);
415 static int btusb_send_frame(struct sk_buff *skb)
417 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
418 struct btusb_data *data = hdev->driver_data;
419 struct usb_ctrlrequest *dr;
424 BT_DBG("%s", hdev->name);
426 if (!test_bit(HCI_RUNNING, &hdev->flags))
429 switch (bt_cb(skb)->pkt_type) {
430 case HCI_COMMAND_PKT:
431 urb = usb_alloc_urb(0, GFP_ATOMIC);
435 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
441 dr->bRequestType = USB_TYPE_CLASS;
445 dr->wLength = __cpu_to_le16(skb->len);
447 pipe = usb_sndctrlpipe(data->udev, 0x00);
449 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
450 skb->data, skb->len, btusb_tx_complete, skb);
455 case HCI_ACLDATA_PKT:
456 urb = usb_alloc_urb(0, GFP_ATOMIC);
460 pipe = usb_sndbulkpipe(data->udev,
461 data->bulk_tx_ep->bEndpointAddress);
463 usb_fill_bulk_urb(urb, data->udev, pipe,
464 skb->data, skb->len, btusb_tx_complete, skb);
469 case HCI_SCODATA_PKT:
478 usb_anchor_urb(urb, &data->tx_anchor);
480 err = usb_submit_urb(urb, GFP_ATOMIC);
482 BT_ERR("%s urb %p submission failed", hdev->name, urb);
483 kfree(urb->setup_packet);
484 usb_unanchor_urb(urb);
492 static void btusb_destruct(struct hci_dev *hdev)
494 struct btusb_data *data = hdev->driver_data;
496 BT_DBG("%s", hdev->name);
501 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
503 struct btusb_data *data = hdev->driver_data;
505 BT_DBG("%s evt %d", hdev->name, evt);
507 if (evt == HCI_NOTIFY_CONN_ADD || evt == HCI_NOTIFY_CONN_DEL)
508 schedule_work(&data->work);
511 static void btusb_work(struct work_struct *work)
513 struct btusb_data *data = container_of(work, struct btusb_data, work);
514 struct hci_dev *hdev = data->hdev;
516 if (hdev->conn_hash.acl_num == 0) {
517 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
518 usb_kill_anchored_urbs(&data->bulk_anchor);
522 if (!test_and_set_bit(BTUSB_BULK_RUNNING, &data->flags)) {
523 if (btusb_submit_bulk_urb(hdev) < 0)
524 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
526 btusb_submit_bulk_urb(hdev);
530 static int btusb_probe(struct usb_interface *intf,
531 const struct usb_device_id *id)
533 struct usb_endpoint_descriptor *ep_desc;
534 struct btusb_data *data;
535 struct hci_dev *hdev;
538 BT_DBG("intf %p id %p", intf, id);
540 /* interface numbers are hardcoded in the spec */
541 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
544 if (!id->driver_info) {
545 const struct usb_device_id *match;
546 match = usb_match_id(intf, blacklist_table);
551 if (id->driver_info == BTUSB_IGNORE)
554 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
557 if (ignore_csr && id->driver_info & BTUSB_CSR)
560 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
563 data = kzalloc(sizeof(*data), GFP_KERNEL);
567 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
568 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
570 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
571 data->intr_ep = ep_desc;
575 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
576 data->bulk_tx_ep = ep_desc;
580 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
581 data->bulk_rx_ep = ep_desc;
586 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
591 data->udev = interface_to_usbdev(intf);
593 spin_lock_init(&data->lock);
595 INIT_WORK(&data->work, btusb_work);
597 init_usb_anchor(&data->tx_anchor);
598 init_usb_anchor(&data->intr_anchor);
599 init_usb_anchor(&data->bulk_anchor);
601 hdev = hci_alloc_dev();
607 hdev->type = HCI_USB;
608 hdev->driver_data = data;
612 SET_HCIDEV_DEV(hdev, &intf->dev);
614 hdev->open = btusb_open;
615 hdev->close = btusb_close;
616 hdev->flush = btusb_flush;
617 hdev->send = btusb_send_frame;
618 hdev->destruct = btusb_destruct;
619 hdev->notify = btusb_notify;
621 hdev->owner = THIS_MODULE;
623 if (reset || id->driver_info & BTUSB_RESET)
624 set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
626 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
628 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
631 if (id->driver_info & BTUSB_SNIFFER) {
632 struct usb_device *udev = interface_to_usbdev(intf);
634 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
635 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
638 if (id->driver_info & BTUSB_BCM92035) {
639 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
642 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
644 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
645 skb_queue_tail(&hdev->driver_init, skb);
649 err = hci_register_dev(hdev);
656 usb_set_intfdata(intf, data);
661 static void btusb_disconnect(struct usb_interface *intf)
663 struct btusb_data *data = usb_get_intfdata(intf);
664 struct hci_dev *hdev;
666 BT_DBG("intf %p", intf);
673 usb_set_intfdata(intf, NULL);
675 hci_unregister_dev(hdev);
680 static struct usb_driver btusb_driver = {
682 .probe = btusb_probe,
683 .disconnect = btusb_disconnect,
684 .id_table = btusb_table,
687 static int __init btusb_init(void)
689 BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
691 return usb_register(&btusb_driver);
694 static void __exit btusb_exit(void)
696 usb_deregister(&btusb_driver);
699 module_init(btusb_init);
700 module_exit(btusb_exit);
702 module_param(ignore_dga, bool, 0644);
703 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
705 module_param(ignore_csr, bool, 0644);
706 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
708 module_param(ignore_sniffer, bool, 0644);
709 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
711 module_param(disable_scofix, bool, 0644);
712 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
714 module_param(force_scofix, bool, 0644);
715 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
717 module_param(reset, bool, 0644);
718 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
720 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
721 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
722 MODULE_VERSION(VERSION);
723 MODULE_LICENSE("GPL");