2 HCI USB driver for Linux Bluetooth protocol stack (BlueZ)
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6 Copyright (C) 2003 Maxim Krasnyansky <maxk@qualcomm.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation;
12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
13 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
15 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
16 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
17 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
22 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
23 SOFTWARE IS DISCLAIMED.
27 * Bluetooth HCI USB driver.
28 * Based on original USB Bluetooth driver for Linux kernel
29 * Copyright (c) 2000 Greg Kroah-Hartman <greg@kroah.com>
30 * Copyright (c) 2000 Mark Douglas Corner <mcorner@umich.edu>
34 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/init.h>
38 #include <linux/unistd.h>
39 #include <linux/types.h>
40 #include <linux/interrupt.h>
41 #include <linux/moduleparam.h>
43 #include <linux/slab.h>
44 #include <linux/errno.h>
45 #include <linux/string.h>
46 #include <linux/skbuff.h>
48 #include <linux/usb.h>
50 #include <net/bluetooth/bluetooth.h>
51 #include <net/bluetooth/hci_core.h>
55 #ifndef CONFIG_BT_HCIUSB_DEBUG
60 #ifndef CONFIG_BT_HCIUSB_ZERO_PACKET
61 #undef URB_ZERO_PACKET
62 #define URB_ZERO_PACKET 0
65 static int ignore = 0;
66 static int ignore_dga = 0;
67 static int ignore_csr = 0;
68 static int ignore_sniffer = 0;
69 static int disable_scofix = 0;
70 static int force_scofix = 0;
73 #ifdef CONFIG_BT_HCIUSB_SCO
79 static struct usb_driver hci_usb_driver;
81 static struct usb_device_id bluetooth_ids[] = {
82 /* Generic Bluetooth USB device */
83 { USB_DEVICE_INFO(HCI_DEV_CLASS, HCI_DEV_SUBCLASS, HCI_DEV_PROTOCOL) },
85 /* AVM BlueFRITZ! USB v2.0 */
86 { USB_DEVICE(0x057c, 0x3800) },
88 /* Bluetooth Ultraport Module from IBM */
89 { USB_DEVICE(0x04bf, 0x030a) },
91 /* ALPS Modules with non-standard id */
92 { USB_DEVICE(0x044e, 0x3001) },
93 { USB_DEVICE(0x044e, 0x3002) },
95 /* Ericsson with non-standard id */
96 { USB_DEVICE(0x0bdb, 0x1002) },
98 /* Canyon CN-BTU1 with HID interfaces */
99 { USB_DEVICE(0x0c10, 0x0000), .driver_info = HCI_RESET },
101 { } /* Terminating entry */
104 MODULE_DEVICE_TABLE (usb, bluetooth_ids);
106 static struct usb_device_id blacklist_ids[] = {
107 /* CSR BlueCore devices */
108 { USB_DEVICE(0x0a12, 0x0001), .driver_info = HCI_CSR },
110 /* Broadcom BCM2033 without firmware */
111 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = HCI_IGNORE },
113 /* Broadcom BCM2035 */
114 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
115 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
116 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = HCI_BCM92035 },
118 /* Broadcom BCM2045 */
119 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
120 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
122 /* IBM/Lenovo ThinkPad with Broadcom chip */
123 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
124 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
127 { USB_DEVICE(0x0a5c, 0x2100), .driver_info = HCI_RESET },
129 /* ANYCOM Bluetooth USB-200 and USB-250 */
130 { USB_DEVICE(0x0a5c, 0x2111), .driver_info = HCI_RESET },
132 /* HP laptop with Broadcom chip */
133 { USB_DEVICE(0x03f0, 0x171d), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
135 /* Dell laptop with Broadcom chip */
136 { USB_DEVICE(0x413c, 0x8126), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
138 /* Microsoft Wireless Transceiver for Bluetooth 2.0 */
139 { USB_DEVICE(0x045e, 0x009c), .driver_info = HCI_RESET },
141 /* Kensington Bluetooth USB adapter */
142 { USB_DEVICE(0x047d, 0x105d), .driver_info = HCI_RESET },
143 { USB_DEVICE(0x047d, 0x105e), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
145 /* ISSC Bluetooth Adapter v3.1 */
146 { USB_DEVICE(0x1131, 0x1001), .driver_info = HCI_RESET },
148 /* RTX Telecom based adapters with buggy SCO support */
149 { USB_DEVICE(0x0400, 0x0807), .driver_info = HCI_BROKEN_ISOC },
150 { USB_DEVICE(0x0400, 0x080a), .driver_info = HCI_BROKEN_ISOC },
152 /* Belkin F8T012 and F8T013 devices */
153 { USB_DEVICE(0x050d, 0x0012), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
154 { USB_DEVICE(0x050d, 0x0013), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
156 /* Digianswer devices */
157 { USB_DEVICE(0x08fd, 0x0001), .driver_info = HCI_DIGIANSWER },
158 { USB_DEVICE(0x08fd, 0x0002), .driver_info = HCI_IGNORE },
160 /* CSR BlueCore Bluetooth Sniffer */
161 { USB_DEVICE(0x0a12, 0x0002), .driver_info = HCI_SNIFFER },
163 /* Frontline ComProbe Bluetooth Sniffer */
164 { USB_DEVICE(0x16d3, 0x0002), .driver_info = HCI_SNIFFER },
166 { } /* Terminating entry */
169 static struct _urb *_urb_alloc(int isoc, gfp_t gfp)
171 struct _urb *_urb = kmalloc(sizeof(struct _urb) +
172 sizeof(struct usb_iso_packet_descriptor) * isoc, gfp);
174 memset(_urb, 0, sizeof(*_urb));
175 usb_init_urb(&_urb->urb);
180 static struct _urb *_urb_dequeue(struct _urb_queue *q)
182 struct _urb *_urb = NULL;
184 spin_lock_irqsave(&q->lock, flags);
186 struct list_head *head = &q->head;
187 struct list_head *next = head->next;
189 _urb = list_entry(next, struct _urb, list);
190 list_del(next); _urb->queue = NULL;
193 spin_unlock_irqrestore(&q->lock, flags);
197 static void hci_usb_rx_complete(struct urb *urb);
198 static void hci_usb_tx_complete(struct urb *urb);
200 #define __pending_tx(husb, type) (&husb->pending_tx[type-1])
201 #define __pending_q(husb, type) (&husb->pending_q[type-1])
202 #define __completed_q(husb, type) (&husb->completed_q[type-1])
203 #define __transmit_q(husb, type) (&husb->transmit_q[type-1])
205 static inline struct _urb *__get_completed(struct hci_usb *husb, int type)
207 return _urb_dequeue(__completed_q(husb, type));
210 #ifdef CONFIG_BT_HCIUSB_SCO
211 static void __fill_isoc_desc(struct urb *urb, int len, int mtu)
215 BT_DBG("len %d mtu %d", len, mtu);
217 for (i=0; i < HCI_MAX_ISOC_FRAMES && len >= mtu; i++, offset += mtu, len -= mtu) {
218 urb->iso_frame_desc[i].offset = offset;
219 urb->iso_frame_desc[i].length = mtu;
220 BT_DBG("desc %d offset %d len %d", i, offset, mtu);
222 if (len && i < HCI_MAX_ISOC_FRAMES) {
223 urb->iso_frame_desc[i].offset = offset;
224 urb->iso_frame_desc[i].length = len;
225 BT_DBG("desc %d offset %d len %d", i, offset, len);
228 urb->number_of_packets = i;
232 static int hci_usb_intr_rx_submit(struct hci_usb *husb)
236 int err, pipe, interval, size;
239 BT_DBG("%s", husb->hdev->name);
241 size = le16_to_cpu(husb->intr_in_ep->desc.wMaxPacketSize);
243 buf = kmalloc(size, GFP_ATOMIC);
247 _urb = _urb_alloc(0, GFP_ATOMIC);
252 _urb->type = HCI_EVENT_PKT;
253 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
256 pipe = usb_rcvintpipe(husb->udev, husb->intr_in_ep->desc.bEndpointAddress);
257 interval = husb->intr_in_ep->desc.bInterval;
258 usb_fill_int_urb(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb, interval);
260 err = usb_submit_urb(urb, GFP_ATOMIC);
262 BT_ERR("%s intr rx submit failed urb %p err %d",
263 husb->hdev->name, urb, err);
271 static int hci_usb_bulk_rx_submit(struct hci_usb *husb)
275 int err, pipe, size = HCI_MAX_FRAME_SIZE;
278 buf = kmalloc(size, GFP_ATOMIC);
282 _urb = _urb_alloc(0, GFP_ATOMIC);
287 _urb->type = HCI_ACLDATA_PKT;
288 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
291 pipe = usb_rcvbulkpipe(husb->udev, husb->bulk_in_ep->desc.bEndpointAddress);
292 usb_fill_bulk_urb(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb);
293 urb->transfer_flags = 0;
295 BT_DBG("%s urb %p", husb->hdev->name, urb);
297 err = usb_submit_urb(urb, GFP_ATOMIC);
299 BT_ERR("%s bulk rx submit failed urb %p err %d",
300 husb->hdev->name, urb, err);
308 #ifdef CONFIG_BT_HCIUSB_SCO
309 static int hci_usb_isoc_rx_submit(struct hci_usb *husb)
316 mtu = le16_to_cpu(husb->isoc_in_ep->desc.wMaxPacketSize);
317 size = mtu * HCI_MAX_ISOC_FRAMES;
319 buf = kmalloc(size, GFP_ATOMIC);
323 _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
328 _urb->type = HCI_SCODATA_PKT;
329 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
334 urb->dev = husb->udev;
335 urb->pipe = usb_rcvisocpipe(husb->udev, husb->isoc_in_ep->desc.bEndpointAddress);
336 urb->complete = hci_usb_rx_complete;
338 urb->interval = husb->isoc_in_ep->desc.bInterval;
340 urb->transfer_buffer_length = size;
341 urb->transfer_buffer = buf;
342 urb->transfer_flags = URB_ISO_ASAP;
344 __fill_isoc_desc(urb, size, mtu);
346 BT_DBG("%s urb %p", husb->hdev->name, urb);
348 err = usb_submit_urb(urb, GFP_ATOMIC);
350 BT_ERR("%s isoc rx submit failed urb %p err %d",
351 husb->hdev->name, urb, err);
360 /* Initialize device */
361 static int hci_usb_open(struct hci_dev *hdev)
363 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
367 BT_DBG("%s", hdev->name);
369 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
372 write_lock_irqsave(&husb->completion_lock, flags);
374 err = hci_usb_intr_rx_submit(husb);
376 for (i = 0; i < HCI_MAX_BULK_RX; i++)
377 hci_usb_bulk_rx_submit(husb);
379 #ifdef CONFIG_BT_HCIUSB_SCO
380 if (husb->isoc_iface)
381 for (i = 0; i < HCI_MAX_ISOC_RX; i++)
382 hci_usb_isoc_rx_submit(husb);
385 clear_bit(HCI_RUNNING, &hdev->flags);
388 write_unlock_irqrestore(&husb->completion_lock, flags);
393 static int hci_usb_flush(struct hci_dev *hdev)
395 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
398 BT_DBG("%s", hdev->name);
400 for (i = 0; i < 4; i++)
401 skb_queue_purge(&husb->transmit_q[i]);
405 static void hci_usb_unlink_urbs(struct hci_usb *husb)
409 BT_DBG("%s", husb->hdev->name);
411 for (i = 0; i < 4; i++) {
415 /* Kill pending requests */
416 while ((_urb = _urb_dequeue(&husb->pending_q[i]))) {
418 BT_DBG("%s unlinking _urb %p type %d urb %p",
419 husb->hdev->name, _urb, _urb->type, urb);
421 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
424 /* Release completed requests */
425 while ((_urb = _urb_dequeue(&husb->completed_q[i]))) {
427 BT_DBG("%s freeing _urb %p type %d urb %p",
428 husb->hdev->name, _urb, _urb->type, urb);
429 kfree(urb->setup_packet);
430 kfree(urb->transfer_buffer);
437 static int hci_usb_close(struct hci_dev *hdev)
439 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
442 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
445 BT_DBG("%s", hdev->name);
447 /* Synchronize with completion handlers */
448 write_lock_irqsave(&husb->completion_lock, flags);
449 write_unlock_irqrestore(&husb->completion_lock, flags);
451 hci_usb_unlink_urbs(husb);
456 static int __tx_submit(struct hci_usb *husb, struct _urb *_urb)
458 struct urb *urb = &_urb->urb;
461 BT_DBG("%s urb %p type %d", husb->hdev->name, urb, _urb->type);
463 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
464 err = usb_submit_urb(urb, GFP_ATOMIC);
466 BT_ERR("%s tx submit failed urb %p type %d err %d",
467 husb->hdev->name, urb, _urb->type, err);
469 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
471 atomic_inc(__pending_tx(husb, _urb->type));
476 static inline int hci_usb_send_ctrl(struct hci_usb *husb, struct sk_buff *skb)
478 struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
479 struct usb_ctrlrequest *dr;
483 _urb = _urb_alloc(0, GFP_ATOMIC);
486 _urb->type = bt_cb(skb)->pkt_type;
488 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
494 dr = (void *) _urb->urb.setup_packet;
496 dr->bRequestType = husb->ctrl_req;
500 dr->wLength = __cpu_to_le16(skb->len);
503 usb_fill_control_urb(urb, husb->udev, usb_sndctrlpipe(husb->udev, 0),
504 (void *) dr, skb->data, skb->len, hci_usb_tx_complete, husb);
506 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
509 return __tx_submit(husb, _urb);
512 static inline int hci_usb_send_bulk(struct hci_usb *husb, struct sk_buff *skb)
514 struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
519 _urb = _urb_alloc(0, GFP_ATOMIC);
522 _urb->type = bt_cb(skb)->pkt_type;
526 pipe = usb_sndbulkpipe(husb->udev, husb->bulk_out_ep->desc.bEndpointAddress);
527 usb_fill_bulk_urb(urb, husb->udev, pipe, skb->data, skb->len,
528 hci_usb_tx_complete, husb);
529 urb->transfer_flags = URB_ZERO_PACKET;
531 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
534 return __tx_submit(husb, _urb);
537 #ifdef CONFIG_BT_HCIUSB_SCO
538 static inline int hci_usb_send_isoc(struct hci_usb *husb, struct sk_buff *skb)
540 struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
544 _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
547 _urb->type = bt_cb(skb)->pkt_type;
550 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
555 urb->dev = husb->udev;
556 urb->pipe = usb_sndisocpipe(husb->udev, husb->isoc_out_ep->desc.bEndpointAddress);
557 urb->complete = hci_usb_tx_complete;
558 urb->transfer_flags = URB_ISO_ASAP;
560 urb->interval = husb->isoc_out_ep->desc.bInterval;
562 urb->transfer_buffer = skb->data;
563 urb->transfer_buffer_length = skb->len;
565 __fill_isoc_desc(urb, skb->len, le16_to_cpu(husb->isoc_out_ep->desc.wMaxPacketSize));
568 return __tx_submit(husb, _urb);
572 static void hci_usb_tx_process(struct hci_usb *husb)
574 struct sk_buff_head *q;
577 BT_DBG("%s", husb->hdev->name);
580 clear_bit(HCI_USB_TX_WAKEUP, &husb->state);
582 /* Process command queue */
583 q = __transmit_q(husb, HCI_COMMAND_PKT);
584 if (!atomic_read(__pending_tx(husb, HCI_COMMAND_PKT)) &&
585 (skb = skb_dequeue(q))) {
586 if (hci_usb_send_ctrl(husb, skb) < 0)
587 skb_queue_head(q, skb);
590 #ifdef CONFIG_BT_HCIUSB_SCO
591 /* Process SCO queue */
592 q = __transmit_q(husb, HCI_SCODATA_PKT);
593 if (atomic_read(__pending_tx(husb, HCI_SCODATA_PKT)) < HCI_MAX_ISOC_TX &&
594 (skb = skb_dequeue(q))) {
595 if (hci_usb_send_isoc(husb, skb) < 0)
596 skb_queue_head(q, skb);
600 /* Process ACL queue */
601 q = __transmit_q(husb, HCI_ACLDATA_PKT);
602 while (atomic_read(__pending_tx(husb, HCI_ACLDATA_PKT)) < HCI_MAX_BULK_TX &&
603 (skb = skb_dequeue(q))) {
604 if (hci_usb_send_bulk(husb, skb) < 0) {
605 skb_queue_head(q, skb);
609 } while(test_bit(HCI_USB_TX_WAKEUP, &husb->state));
612 static inline void hci_usb_tx_wakeup(struct hci_usb *husb)
614 /* Serialize TX queue processing to avoid data reordering */
615 if (!test_and_set_bit(HCI_USB_TX_PROCESS, &husb->state)) {
616 hci_usb_tx_process(husb);
617 clear_bit(HCI_USB_TX_PROCESS, &husb->state);
619 set_bit(HCI_USB_TX_WAKEUP, &husb->state);
622 /* Send frames from HCI layer */
623 static int hci_usb_send_frame(struct sk_buff *skb)
625 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
626 struct hci_usb *husb;
629 BT_ERR("frame for uknown device (hdev=NULL)");
633 if (!test_bit(HCI_RUNNING, &hdev->flags))
636 BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
638 husb = (struct hci_usb *) hdev->driver_data;
640 switch (bt_cb(skb)->pkt_type) {
641 case HCI_COMMAND_PKT:
645 case HCI_ACLDATA_PKT:
649 #ifdef CONFIG_BT_HCIUSB_SCO
650 case HCI_SCODATA_PKT:
660 read_lock(&husb->completion_lock);
662 skb_queue_tail(__transmit_q(husb, bt_cb(skb)->pkt_type), skb);
663 hci_usb_tx_wakeup(husb);
665 read_unlock(&husb->completion_lock);
669 static void hci_usb_rx_complete(struct urb *urb)
671 struct _urb *_urb = container_of(urb, struct _urb, urb);
672 struct hci_usb *husb = (void *) urb->context;
673 struct hci_dev *hdev = husb->hdev;
674 int err, count = urb->actual_length;
676 BT_DBG("%s urb %p type %d status %d count %d flags %x", hdev->name, urb,
677 _urb->type, urb->status, count, urb->transfer_flags);
679 read_lock(&husb->completion_lock);
681 if (!test_bit(HCI_RUNNING, &hdev->flags))
684 if (urb->status || !count)
687 if (_urb->type == HCI_SCODATA_PKT) {
688 #ifdef CONFIG_BT_HCIUSB_SCO
690 for (i=0; i < urb->number_of_packets; i++) {
691 BT_DBG("desc %d status %d offset %d len %d", i,
692 urb->iso_frame_desc[i].status,
693 urb->iso_frame_desc[i].offset,
694 urb->iso_frame_desc[i].actual_length);
696 if (!urb->iso_frame_desc[i].status) {
697 husb->hdev->stat.byte_rx += urb->iso_frame_desc[i].actual_length;
698 hci_recv_fragment(husb->hdev, _urb->type,
699 urb->transfer_buffer + urb->iso_frame_desc[i].offset,
700 urb->iso_frame_desc[i].actual_length);
707 husb->hdev->stat.byte_rx += count;
708 err = hci_recv_fragment(husb->hdev, _urb->type, urb->transfer_buffer, count);
710 BT_ERR("%s corrupted packet: type %d count %d",
711 husb->hdev->name, _urb->type, count);
717 urb->dev = husb->udev;
718 err = usb_submit_urb(urb, GFP_ATOMIC);
719 BT_DBG("%s urb %p type %d resubmit status %d", hdev->name, urb,
723 read_unlock(&husb->completion_lock);
726 static void hci_usb_tx_complete(struct urb *urb)
728 struct _urb *_urb = container_of(urb, struct _urb, urb);
729 struct hci_usb *husb = (void *) urb->context;
730 struct hci_dev *hdev = husb->hdev;
732 BT_DBG("%s urb %p status %d flags %x", hdev->name, urb,
733 urb->status, urb->transfer_flags);
735 atomic_dec(__pending_tx(husb, _urb->type));
737 urb->transfer_buffer = NULL;
738 kfree_skb((struct sk_buff *) _urb->priv);
740 if (!test_bit(HCI_RUNNING, &hdev->flags))
744 hdev->stat.byte_tx += urb->transfer_buffer_length;
748 read_lock(&husb->completion_lock);
751 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
753 hci_usb_tx_wakeup(husb);
755 read_unlock(&husb->completion_lock);
758 static void hci_usb_destruct(struct hci_dev *hdev)
760 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
762 BT_DBG("%s", hdev->name);
767 static void hci_usb_notify(struct hci_dev *hdev, unsigned int evt)
769 BT_DBG("%s evt %d", hdev->name, evt);
772 static int hci_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
774 struct usb_device *udev = interface_to_usbdev(intf);
775 struct usb_host_endpoint *bulk_out_ep = NULL;
776 struct usb_host_endpoint *bulk_in_ep = NULL;
777 struct usb_host_endpoint *intr_in_ep = NULL;
778 struct usb_host_endpoint *ep;
779 struct usb_host_interface *uif;
780 struct usb_interface *isoc_iface;
781 struct hci_usb *husb;
782 struct hci_dev *hdev;
783 int i, e, size, isoc_ifnum, isoc_alts;
785 BT_DBG("udev %p intf %p", udev, intf);
787 if (!id->driver_info) {
788 const struct usb_device_id *match;
789 match = usb_match_id(intf, blacklist_ids);
794 if (ignore || id->driver_info & HCI_IGNORE)
797 if (ignore_dga && id->driver_info & HCI_DIGIANSWER)
800 if (ignore_csr && id->driver_info & HCI_CSR)
803 if (ignore_sniffer && id->driver_info & HCI_SNIFFER)
806 if (intf->cur_altsetting->desc.bInterfaceNumber > 0)
809 /* Find endpoints that we need */
810 uif = intf->cur_altsetting;
811 for (e = 0; e < uif->desc.bNumEndpoints; e++) {
812 ep = &uif->endpoint[e];
814 switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
815 case USB_ENDPOINT_XFER_INT:
816 if (ep->desc.bEndpointAddress & USB_DIR_IN)
820 case USB_ENDPOINT_XFER_BULK:
821 if (ep->desc.bEndpointAddress & USB_DIR_IN)
829 if (!bulk_in_ep || !bulk_out_ep || !intr_in_ep) {
830 BT_DBG("Bulk endpoints not found");
834 if (!(husb = kzalloc(sizeof(struct hci_usb), GFP_KERNEL))) {
835 BT_ERR("Can't allocate: control structure");
840 husb->bulk_out_ep = bulk_out_ep;
841 husb->bulk_in_ep = bulk_in_ep;
842 husb->intr_in_ep = intr_in_ep;
844 if (id->driver_info & HCI_DIGIANSWER)
845 husb->ctrl_req = USB_TYPE_VENDOR;
847 husb->ctrl_req = USB_TYPE_CLASS;
849 /* Find isochronous endpoints that we can use */
855 #ifdef CONFIG_BT_HCIUSB_SCO
856 if (isoc && !(id->driver_info & (HCI_BROKEN_ISOC | HCI_SNIFFER)))
857 isoc_iface = usb_ifnum_to_if(udev, isoc_ifnum);
861 struct usb_host_endpoint *isoc_out_ep = NULL;
862 struct usb_host_endpoint *isoc_in_ep = NULL;
864 for (a = 0; a < isoc_iface->num_altsetting; a++) {
865 uif = &isoc_iface->altsetting[a];
866 for (e = 0; e < uif->desc.bNumEndpoints; e++) {
867 ep = &uif->endpoint[e];
869 switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
870 case USB_ENDPOINT_XFER_ISOC:
871 if (le16_to_cpu(ep->desc.wMaxPacketSize) < size ||
872 uif->desc.bAlternateSetting != isoc)
874 size = le16_to_cpu(ep->desc.wMaxPacketSize);
876 isoc_alts = uif->desc.bAlternateSetting;
878 if (ep->desc.bEndpointAddress & USB_DIR_IN)
887 if (!isoc_in_ep || !isoc_out_ep)
888 BT_DBG("Isoc endpoints not found");
890 BT_DBG("isoc ifnum %d alts %d", isoc_ifnum, isoc_alts);
891 if (usb_driver_claim_interface(&hci_usb_driver, isoc_iface, husb) != 0)
892 BT_ERR("Can't claim isoc interface");
893 else if (usb_set_interface(udev, isoc_ifnum, isoc_alts)) {
894 BT_ERR("Can't set isoc interface settings");
895 husb->isoc_iface = isoc_iface;
896 usb_driver_release_interface(&hci_usb_driver, isoc_iface);
897 husb->isoc_iface = NULL;
899 husb->isoc_iface = isoc_iface;
900 husb->isoc_in_ep = isoc_in_ep;
901 husb->isoc_out_ep = isoc_out_ep;
907 rwlock_init(&husb->completion_lock);
909 for (i = 0; i < 4; i++) {
910 skb_queue_head_init(&husb->transmit_q[i]);
911 _urb_queue_init(&husb->pending_q[i]);
912 _urb_queue_init(&husb->completed_q[i]);
915 /* Initialize and register HCI device */
916 hdev = hci_alloc_dev();
918 BT_ERR("Can't allocate HCI device");
924 hdev->type = HCI_USB;
925 hdev->driver_data = husb;
926 SET_HCIDEV_DEV(hdev, &intf->dev);
928 hdev->open = hci_usb_open;
929 hdev->close = hci_usb_close;
930 hdev->flush = hci_usb_flush;
931 hdev->send = hci_usb_send_frame;
932 hdev->destruct = hci_usb_destruct;
933 hdev->notify = hci_usb_notify;
935 hdev->owner = THIS_MODULE;
937 if (reset || id->driver_info & HCI_RESET)
938 set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
940 if (force_scofix || id->driver_info & HCI_WRONG_SCO_MTU) {
942 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
945 if (id->driver_info & HCI_SNIFFER) {
946 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
947 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
950 if (id->driver_info & HCI_BCM92035) {
951 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
954 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
956 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
957 skb_queue_tail(&hdev->driver_init, skb);
961 if (hci_register_dev(hdev) < 0) {
962 BT_ERR("Can't register HCI device");
967 usb_set_intfdata(intf, husb);
971 if (husb->isoc_iface)
972 usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
979 static void hci_usb_disconnect(struct usb_interface *intf)
981 struct hci_usb *husb = usb_get_intfdata(intf);
982 struct hci_dev *hdev;
984 if (!husb || intf == husb->isoc_iface)
987 usb_set_intfdata(intf, NULL);
990 BT_DBG("%s", hdev->name);
994 if (husb->isoc_iface)
995 usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
997 if (hci_unregister_dev(hdev) < 0)
998 BT_ERR("Can't unregister HCI device %s", hdev->name);
1003 static int hci_usb_suspend(struct usb_interface *intf, pm_message_t message)
1005 struct hci_usb *husb = usb_get_intfdata(intf);
1006 struct list_head killed;
1007 unsigned long flags;
1010 if (!husb || intf == husb->isoc_iface)
1013 hci_suspend_dev(husb->hdev);
1015 INIT_LIST_HEAD(&killed);
1017 for (i = 0; i < 4; i++) {
1018 struct _urb_queue *q = &husb->pending_q[i];
1019 struct _urb *_urb, *_tmp;
1021 while ((_urb = _urb_dequeue(q))) {
1022 /* reset queue since _urb_dequeue sets it to NULL */
1024 usb_kill_urb(&_urb->urb);
1025 list_add(&_urb->list, &killed);
1028 spin_lock_irqsave(&q->lock, flags);
1030 list_for_each_entry_safe(_urb, _tmp, &killed, list) {
1031 list_move_tail(&_urb->list, &q->head);
1034 spin_unlock_irqrestore(&q->lock, flags);
1040 static int hci_usb_resume(struct usb_interface *intf)
1042 struct hci_usb *husb = usb_get_intfdata(intf);
1043 unsigned long flags;
1046 if (!husb || intf == husb->isoc_iface)
1049 for (i = 0; i < 4; i++) {
1050 struct _urb_queue *q = &husb->pending_q[i];
1053 spin_lock_irqsave(&q->lock, flags);
1055 list_for_each_entry(_urb, &q->head, list) {
1056 err = usb_submit_urb(&_urb->urb, GFP_ATOMIC);
1061 spin_unlock_irqrestore(&q->lock, flags);
1067 hci_resume_dev(husb->hdev);
1072 static struct usb_driver hci_usb_driver = {
1074 .probe = hci_usb_probe,
1075 .disconnect = hci_usb_disconnect,
1076 .suspend = hci_usb_suspend,
1077 .resume = hci_usb_resume,
1078 .id_table = bluetooth_ids,
1081 static int __init hci_usb_init(void)
1085 BT_INFO("HCI USB driver ver %s", VERSION);
1087 if ((err = usb_register(&hci_usb_driver)) < 0)
1088 BT_ERR("Failed to register HCI USB driver");
1093 static void __exit hci_usb_exit(void)
1095 usb_deregister(&hci_usb_driver);
1098 module_init(hci_usb_init);
1099 module_exit(hci_usb_exit);
1101 module_param(ignore, bool, 0644);
1102 MODULE_PARM_DESC(ignore, "Ignore devices from the matching table");
1104 module_param(ignore_dga, bool, 0644);
1105 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1107 module_param(ignore_csr, bool, 0644);
1108 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1110 module_param(ignore_sniffer, bool, 0644);
1111 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1113 module_param(disable_scofix, bool, 0644);
1114 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1116 module_param(force_scofix, bool, 0644);
1117 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1119 module_param(reset, bool, 0644);
1120 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1122 #ifdef CONFIG_BT_HCIUSB_SCO
1123 module_param(isoc, int, 0644);
1124 MODULE_PARM_DESC(isoc, "Set isochronous transfers for SCO over HCI support");
1127 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
1128 MODULE_DESCRIPTION("Bluetooth HCI USB driver ver " VERSION);
1129 MODULE_VERSION(VERSION);
1130 MODULE_LICENSE("GPL");