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/sched.h>
39 #include <linux/unistd.h>
40 #include <linux/types.h>
41 #include <linux/interrupt.h>
42 #include <linux/moduleparam.h>
44 #include <linux/slab.h>
45 #include <linux/errno.h>
46 #include <linux/string.h>
47 #include <linux/skbuff.h>
49 #include <linux/usb.h>
51 #include <net/bluetooth/bluetooth.h>
52 #include <net/bluetooth/hci_core.h>
56 #ifndef CONFIG_BT_HCIUSB_DEBUG
61 #ifndef CONFIG_BT_HCIUSB_ZERO_PACKET
62 #undef URB_ZERO_PACKET
63 #define URB_ZERO_PACKET 0
66 static int ignore = 0;
67 static int ignore_dga = 0;
68 static int ignore_csr = 0;
69 static int ignore_sniffer = 0;
72 #ifdef CONFIG_BT_HCIUSB_SCO
78 static struct usb_driver hci_usb_driver;
80 static struct usb_device_id bluetooth_ids[] = {
81 /* Generic Bluetooth USB device */
82 { USB_DEVICE_INFO(HCI_DEV_CLASS, HCI_DEV_SUBCLASS, HCI_DEV_PROTOCOL) },
84 /* AVM BlueFRITZ! USB v2.0 */
85 { USB_DEVICE(0x057c, 0x3800) },
87 /* Bluetooth Ultraport Module from IBM */
88 { USB_DEVICE(0x04bf, 0x030a) },
90 /* ALPS Modules with non-standard id */
91 { USB_DEVICE(0x044e, 0x3001) },
92 { USB_DEVICE(0x044e, 0x3002) },
94 /* Ericsson with non-standard id */
95 { USB_DEVICE(0x0bdb, 0x1002) },
97 { } /* Terminating entry */
100 MODULE_DEVICE_TABLE (usb, bluetooth_ids);
102 static struct usb_device_id blacklist_ids[] = {
103 /* CSR BlueCore devices */
104 { USB_DEVICE(0x0a12, 0x0001), .driver_info = HCI_CSR },
106 /* Broadcom BCM2033 without firmware */
107 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = HCI_IGNORE },
109 /* Broadcom BCM2035 */
110 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = HCI_RESET | HCI_BROKEN_ISOC },
111 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = HCI_BCM92035 },
113 /* Microsoft Wireless Transceiver for Bluetooth 2.0 */
114 { USB_DEVICE(0x045e, 0x009c), .driver_info = HCI_RESET },
116 /* Kensington Bluetooth USB adapter */
117 { USB_DEVICE(0x047d, 0x105d), .driver_info = HCI_RESET },
119 /* ISSC Bluetooth Adapter v3.1 */
120 { USB_DEVICE(0x1131, 0x1001), .driver_info = HCI_RESET },
122 /* RTX Telecom based adapter with buggy SCO support */
123 { USB_DEVICE(0x0400, 0x0807), .driver_info = HCI_BROKEN_ISOC },
126 { USB_DEVICE(0x050d, 0x0012), .driver_info = HCI_WRONG_SCO_MTU },
128 /* Digianswer devices */
129 { USB_DEVICE(0x08fd, 0x0001), .driver_info = HCI_DIGIANSWER },
130 { USB_DEVICE(0x08fd, 0x0002), .driver_info = HCI_IGNORE },
132 /* CSR BlueCore Bluetooth Sniffer */
133 { USB_DEVICE(0x0a12, 0x0002), .driver_info = HCI_SNIFFER },
135 /* Frontline ComProbe Bluetooth Sniffer */
136 { USB_DEVICE(0x16d3, 0x0002), .driver_info = HCI_SNIFFER },
138 { } /* Terminating entry */
141 static struct _urb *_urb_alloc(int isoc, gfp_t gfp)
143 struct _urb *_urb = kmalloc(sizeof(struct _urb) +
144 sizeof(struct usb_iso_packet_descriptor) * isoc, gfp);
146 memset(_urb, 0, sizeof(*_urb));
147 usb_init_urb(&_urb->urb);
152 static struct _urb *_urb_dequeue(struct _urb_queue *q)
154 struct _urb *_urb = NULL;
156 spin_lock_irqsave(&q->lock, flags);
158 struct list_head *head = &q->head;
159 struct list_head *next = head->next;
161 _urb = list_entry(next, struct _urb, list);
162 list_del(next); _urb->queue = NULL;
165 spin_unlock_irqrestore(&q->lock, flags);
169 static void hci_usb_rx_complete(struct urb *urb, struct pt_regs *regs);
170 static void hci_usb_tx_complete(struct urb *urb, struct pt_regs *regs);
172 #define __pending_tx(husb, type) (&husb->pending_tx[type-1])
173 #define __pending_q(husb, type) (&husb->pending_q[type-1])
174 #define __completed_q(husb, type) (&husb->completed_q[type-1])
175 #define __transmit_q(husb, type) (&husb->transmit_q[type-1])
176 #define __reassembly(husb, type) (husb->reassembly[type-1])
178 static inline struct _urb *__get_completed(struct hci_usb *husb, int type)
180 return _urb_dequeue(__completed_q(husb, type));
183 #ifdef CONFIG_BT_HCIUSB_SCO
184 static void __fill_isoc_desc(struct urb *urb, int len, int mtu)
188 BT_DBG("len %d mtu %d", len, mtu);
190 for (i=0; i < HCI_MAX_ISOC_FRAMES && len >= mtu; i++, offset += mtu, len -= mtu) {
191 urb->iso_frame_desc[i].offset = offset;
192 urb->iso_frame_desc[i].length = mtu;
193 BT_DBG("desc %d offset %d len %d", i, offset, mtu);
195 if (len && i < HCI_MAX_ISOC_FRAMES) {
196 urb->iso_frame_desc[i].offset = offset;
197 urb->iso_frame_desc[i].length = len;
198 BT_DBG("desc %d offset %d len %d", i, offset, len);
201 urb->number_of_packets = i;
205 static int hci_usb_intr_rx_submit(struct hci_usb *husb)
209 int err, pipe, interval, size;
212 BT_DBG("%s", husb->hdev->name);
214 size = le16_to_cpu(husb->intr_in_ep->desc.wMaxPacketSize);
216 buf = kmalloc(size, GFP_ATOMIC);
220 _urb = _urb_alloc(0, GFP_ATOMIC);
225 _urb->type = HCI_EVENT_PKT;
226 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
229 pipe = usb_rcvintpipe(husb->udev, husb->intr_in_ep->desc.bEndpointAddress);
230 interval = husb->intr_in_ep->desc.bInterval;
231 usb_fill_int_urb(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb, interval);
233 err = usb_submit_urb(urb, GFP_ATOMIC);
235 BT_ERR("%s intr rx submit failed urb %p err %d",
236 husb->hdev->name, urb, err);
244 static int hci_usb_bulk_rx_submit(struct hci_usb *husb)
248 int err, pipe, size = HCI_MAX_FRAME_SIZE;
251 buf = kmalloc(size, GFP_ATOMIC);
255 _urb = _urb_alloc(0, GFP_ATOMIC);
260 _urb->type = HCI_ACLDATA_PKT;
261 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
264 pipe = usb_rcvbulkpipe(husb->udev, husb->bulk_in_ep->desc.bEndpointAddress);
265 usb_fill_bulk_urb(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb);
266 urb->transfer_flags = 0;
268 BT_DBG("%s urb %p", husb->hdev->name, urb);
270 err = usb_submit_urb(urb, GFP_ATOMIC);
272 BT_ERR("%s bulk rx submit failed urb %p err %d",
273 husb->hdev->name, urb, err);
281 #ifdef CONFIG_BT_HCIUSB_SCO
282 static int hci_usb_isoc_rx_submit(struct hci_usb *husb)
289 mtu = le16_to_cpu(husb->isoc_in_ep->desc.wMaxPacketSize);
290 size = mtu * HCI_MAX_ISOC_FRAMES;
292 buf = kmalloc(size, GFP_ATOMIC);
296 _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
301 _urb->type = HCI_SCODATA_PKT;
302 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
307 urb->dev = husb->udev;
308 urb->pipe = usb_rcvisocpipe(husb->udev, husb->isoc_in_ep->desc.bEndpointAddress);
309 urb->complete = hci_usb_rx_complete;
311 urb->interval = husb->isoc_in_ep->desc.bInterval;
313 urb->transfer_buffer_length = size;
314 urb->transfer_buffer = buf;
315 urb->transfer_flags = URB_ISO_ASAP;
317 __fill_isoc_desc(urb, size, mtu);
319 BT_DBG("%s urb %p", husb->hdev->name, urb);
321 err = usb_submit_urb(urb, GFP_ATOMIC);
323 BT_ERR("%s isoc rx submit failed urb %p err %d",
324 husb->hdev->name, urb, err);
333 /* Initialize device */
334 static int hci_usb_open(struct hci_dev *hdev)
336 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
340 BT_DBG("%s", hdev->name);
342 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
345 write_lock_irqsave(&husb->completion_lock, flags);
347 err = hci_usb_intr_rx_submit(husb);
349 for (i = 0; i < HCI_MAX_BULK_RX; i++)
350 hci_usb_bulk_rx_submit(husb);
352 #ifdef CONFIG_BT_HCIUSB_SCO
353 if (husb->isoc_iface)
354 for (i = 0; i < HCI_MAX_ISOC_RX; i++)
355 hci_usb_isoc_rx_submit(husb);
358 clear_bit(HCI_RUNNING, &hdev->flags);
361 write_unlock_irqrestore(&husb->completion_lock, flags);
366 static int hci_usb_flush(struct hci_dev *hdev)
368 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
371 BT_DBG("%s", hdev->name);
373 for (i = 0; i < 4; i++)
374 skb_queue_purge(&husb->transmit_q[i]);
378 static void hci_usb_unlink_urbs(struct hci_usb *husb)
382 BT_DBG("%s", husb->hdev->name);
384 for (i = 0; i < 4; i++) {
388 /* Kill pending requests */
389 while ((_urb = _urb_dequeue(&husb->pending_q[i]))) {
391 BT_DBG("%s unlinking _urb %p type %d urb %p",
392 husb->hdev->name, _urb, _urb->type, urb);
394 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
397 /* Release completed requests */
398 while ((_urb = _urb_dequeue(&husb->completed_q[i]))) {
400 BT_DBG("%s freeing _urb %p type %d urb %p",
401 husb->hdev->name, _urb, _urb->type, urb);
402 kfree(urb->setup_packet);
403 kfree(urb->transfer_buffer);
407 /* Release reassembly buffers */
408 if (husb->reassembly[i]) {
409 kfree_skb(husb->reassembly[i]);
410 husb->reassembly[i] = NULL;
416 static int hci_usb_close(struct hci_dev *hdev)
418 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
421 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
424 BT_DBG("%s", hdev->name);
426 /* Synchronize with completion handlers */
427 write_lock_irqsave(&husb->completion_lock, flags);
428 write_unlock_irqrestore(&husb->completion_lock, flags);
430 hci_usb_unlink_urbs(husb);
435 static int __tx_submit(struct hci_usb *husb, struct _urb *_urb)
437 struct urb *urb = &_urb->urb;
440 BT_DBG("%s urb %p type %d", husb->hdev->name, urb, _urb->type);
442 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
443 err = usb_submit_urb(urb, GFP_ATOMIC);
445 BT_ERR("%s tx submit failed urb %p type %d err %d",
446 husb->hdev->name, urb, _urb->type, err);
448 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
450 atomic_inc(__pending_tx(husb, _urb->type));
455 static inline int hci_usb_send_ctrl(struct hci_usb *husb, struct sk_buff *skb)
457 struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
458 struct usb_ctrlrequest *dr;
462 _urb = _urb_alloc(0, GFP_ATOMIC);
465 _urb->type = bt_cb(skb)->pkt_type;
467 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
473 dr = (void *) _urb->urb.setup_packet;
475 dr->bRequestType = husb->ctrl_req;
479 dr->wLength = __cpu_to_le16(skb->len);
482 usb_fill_control_urb(urb, husb->udev, usb_sndctrlpipe(husb->udev, 0),
483 (void *) dr, skb->data, skb->len, hci_usb_tx_complete, husb);
485 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
488 return __tx_submit(husb, _urb);
491 static inline int hci_usb_send_bulk(struct hci_usb *husb, struct sk_buff *skb)
493 struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
498 _urb = _urb_alloc(0, GFP_ATOMIC);
501 _urb->type = bt_cb(skb)->pkt_type;
505 pipe = usb_sndbulkpipe(husb->udev, husb->bulk_out_ep->desc.bEndpointAddress);
506 usb_fill_bulk_urb(urb, husb->udev, pipe, skb->data, skb->len,
507 hci_usb_tx_complete, husb);
508 urb->transfer_flags = URB_ZERO_PACKET;
510 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
513 return __tx_submit(husb, _urb);
516 #ifdef CONFIG_BT_HCIUSB_SCO
517 static inline int hci_usb_send_isoc(struct hci_usb *husb, struct sk_buff *skb)
519 struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
523 _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
526 _urb->type = bt_cb(skb)->pkt_type;
529 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
534 urb->dev = husb->udev;
535 urb->pipe = usb_sndisocpipe(husb->udev, husb->isoc_out_ep->desc.bEndpointAddress);
536 urb->complete = hci_usb_tx_complete;
537 urb->transfer_flags = URB_ISO_ASAP;
539 urb->interval = husb->isoc_out_ep->desc.bInterval;
541 urb->transfer_buffer = skb->data;
542 urb->transfer_buffer_length = skb->len;
544 __fill_isoc_desc(urb, skb->len, le16_to_cpu(husb->isoc_out_ep->desc.wMaxPacketSize));
547 return __tx_submit(husb, _urb);
551 static void hci_usb_tx_process(struct hci_usb *husb)
553 struct sk_buff_head *q;
556 BT_DBG("%s", husb->hdev->name);
559 clear_bit(HCI_USB_TX_WAKEUP, &husb->state);
561 /* Process command queue */
562 q = __transmit_q(husb, HCI_COMMAND_PKT);
563 if (!atomic_read(__pending_tx(husb, HCI_COMMAND_PKT)) &&
564 (skb = skb_dequeue(q))) {
565 if (hci_usb_send_ctrl(husb, skb) < 0)
566 skb_queue_head(q, skb);
569 #ifdef CONFIG_BT_HCIUSB_SCO
570 /* Process SCO queue */
571 q = __transmit_q(husb, HCI_SCODATA_PKT);
572 if (atomic_read(__pending_tx(husb, HCI_SCODATA_PKT)) < HCI_MAX_ISOC_TX &&
573 (skb = skb_dequeue(q))) {
574 if (hci_usb_send_isoc(husb, skb) < 0)
575 skb_queue_head(q, skb);
579 /* Process ACL queue */
580 q = __transmit_q(husb, HCI_ACLDATA_PKT);
581 while (atomic_read(__pending_tx(husb, HCI_ACLDATA_PKT)) < HCI_MAX_BULK_TX &&
582 (skb = skb_dequeue(q))) {
583 if (hci_usb_send_bulk(husb, skb) < 0) {
584 skb_queue_head(q, skb);
588 } while(test_bit(HCI_USB_TX_WAKEUP, &husb->state));
591 static inline void hci_usb_tx_wakeup(struct hci_usb *husb)
593 /* Serialize TX queue processing to avoid data reordering */
594 if (!test_and_set_bit(HCI_USB_TX_PROCESS, &husb->state)) {
595 hci_usb_tx_process(husb);
596 clear_bit(HCI_USB_TX_PROCESS, &husb->state);
598 set_bit(HCI_USB_TX_WAKEUP, &husb->state);
601 /* Send frames from HCI layer */
602 static int hci_usb_send_frame(struct sk_buff *skb)
604 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
605 struct hci_usb *husb;
608 BT_ERR("frame for uknown device (hdev=NULL)");
612 if (!test_bit(HCI_RUNNING, &hdev->flags))
615 BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
617 husb = (struct hci_usb *) hdev->driver_data;
619 switch (bt_cb(skb)->pkt_type) {
620 case HCI_COMMAND_PKT:
624 case HCI_ACLDATA_PKT:
628 #ifdef CONFIG_BT_HCIUSB_SCO
629 case HCI_SCODATA_PKT:
639 read_lock(&husb->completion_lock);
641 skb_queue_tail(__transmit_q(husb, bt_cb(skb)->pkt_type), skb);
642 hci_usb_tx_wakeup(husb);
644 read_unlock(&husb->completion_lock);
648 static inline int __recv_frame(struct hci_usb *husb, int type, void *data, int count)
650 BT_DBG("%s type %d data %p count %d", husb->hdev->name, type, data, count);
652 husb->hdev->stat.byte_rx += count;
655 struct sk_buff *skb = __reassembly(husb, type);
656 struct { int expect; } *scb;
660 /* Start of the frame */
664 if (count >= HCI_EVENT_HDR_SIZE) {
665 struct hci_event_hdr *h = data;
666 len = HCI_EVENT_HDR_SIZE + h->plen;
671 case HCI_ACLDATA_PKT:
672 if (count >= HCI_ACL_HDR_SIZE) {
673 struct hci_acl_hdr *h = data;
674 len = HCI_ACL_HDR_SIZE + __le16_to_cpu(h->dlen);
678 #ifdef CONFIG_BT_HCIUSB_SCO
679 case HCI_SCODATA_PKT:
680 if (count >= HCI_SCO_HDR_SIZE) {
681 struct hci_sco_hdr *h = data;
682 len = HCI_SCO_HDR_SIZE + h->dlen;
688 BT_DBG("new packet len %d", len);
690 skb = bt_skb_alloc(len, GFP_ATOMIC);
692 BT_ERR("%s no memory for the packet", husb->hdev->name);
695 skb->dev = (void *) husb->hdev;
696 bt_cb(skb)->pkt_type = type;
698 __reassembly(husb, type) = skb;
700 scb = (void *) skb->cb;
704 scb = (void *) skb->cb;
708 len = min(len, count);
710 memcpy(skb_put(skb, len), data, len);
715 __reassembly(husb, type) = NULL;
716 bt_cb(skb)->pkt_type = type;
720 count -= len; data += len;
725 static void hci_usb_rx_complete(struct urb *urb, struct pt_regs *regs)
727 struct _urb *_urb = container_of(urb, struct _urb, urb);
728 struct hci_usb *husb = (void *) urb->context;
729 struct hci_dev *hdev = husb->hdev;
730 int err, count = urb->actual_length;
732 BT_DBG("%s urb %p type %d status %d count %d flags %x", hdev->name, urb,
733 _urb->type, urb->status, count, urb->transfer_flags);
735 read_lock(&husb->completion_lock);
737 if (!test_bit(HCI_RUNNING, &hdev->flags))
740 if (urb->status || !count)
743 if (_urb->type == HCI_SCODATA_PKT) {
744 #ifdef CONFIG_BT_HCIUSB_SCO
746 for (i=0; i < urb->number_of_packets; i++) {
747 BT_DBG("desc %d status %d offset %d len %d", i,
748 urb->iso_frame_desc[i].status,
749 urb->iso_frame_desc[i].offset,
750 urb->iso_frame_desc[i].actual_length);
752 if (!urb->iso_frame_desc[i].status)
753 __recv_frame(husb, _urb->type,
754 urb->transfer_buffer + urb->iso_frame_desc[i].offset,
755 urb->iso_frame_desc[i].actual_length);
761 err = __recv_frame(husb, _urb->type, urb->transfer_buffer, count);
763 BT_ERR("%s corrupted packet: type %d count %d",
764 husb->hdev->name, _urb->type, count);
770 urb->dev = husb->udev;
771 err = usb_submit_urb(urb, GFP_ATOMIC);
772 BT_DBG("%s urb %p type %d resubmit status %d", hdev->name, urb,
776 read_unlock(&husb->completion_lock);
779 static void hci_usb_tx_complete(struct urb *urb, struct pt_regs *regs)
781 struct _urb *_urb = container_of(urb, struct _urb, urb);
782 struct hci_usb *husb = (void *) urb->context;
783 struct hci_dev *hdev = husb->hdev;
785 BT_DBG("%s urb %p status %d flags %x", hdev->name, urb,
786 urb->status, urb->transfer_flags);
788 atomic_dec(__pending_tx(husb, _urb->type));
790 urb->transfer_buffer = NULL;
791 kfree_skb((struct sk_buff *) _urb->priv);
793 if (!test_bit(HCI_RUNNING, &hdev->flags))
797 hdev->stat.byte_tx += urb->transfer_buffer_length;
801 read_lock(&husb->completion_lock);
804 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
806 hci_usb_tx_wakeup(husb);
808 read_unlock(&husb->completion_lock);
811 static void hci_usb_destruct(struct hci_dev *hdev)
813 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
815 BT_DBG("%s", hdev->name);
820 static void hci_usb_notify(struct hci_dev *hdev, unsigned int evt)
822 BT_DBG("%s evt %d", hdev->name, evt);
825 static int hci_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
827 struct usb_device *udev = interface_to_usbdev(intf);
828 struct usb_host_endpoint *bulk_out_ep = NULL;
829 struct usb_host_endpoint *bulk_in_ep = NULL;
830 struct usb_host_endpoint *intr_in_ep = NULL;
831 struct usb_host_endpoint *ep;
832 struct usb_host_interface *uif;
833 struct usb_interface *isoc_iface;
834 struct hci_usb *husb;
835 struct hci_dev *hdev;
836 int i, e, size, isoc_ifnum, isoc_alts;
838 BT_DBG("udev %p intf %p", udev, intf);
840 if (!id->driver_info) {
841 const struct usb_device_id *match;
842 match = usb_match_id(intf, blacklist_ids);
847 if (ignore || id->driver_info & HCI_IGNORE)
850 if (ignore_dga && id->driver_info & HCI_DIGIANSWER)
853 if (ignore_csr && id->driver_info & HCI_CSR)
856 if (ignore_sniffer && id->driver_info & HCI_SNIFFER)
859 if (intf->cur_altsetting->desc.bInterfaceNumber > 0)
862 /* Find endpoints that we need */
863 uif = intf->cur_altsetting;
864 for (e = 0; e < uif->desc.bNumEndpoints; e++) {
865 ep = &uif->endpoint[e];
867 switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
868 case USB_ENDPOINT_XFER_INT:
869 if (ep->desc.bEndpointAddress & USB_DIR_IN)
873 case USB_ENDPOINT_XFER_BULK:
874 if (ep->desc.bEndpointAddress & USB_DIR_IN)
882 if (!bulk_in_ep || !bulk_out_ep || !intr_in_ep) {
883 BT_DBG("Bulk endpoints not found");
887 if (!(husb = kzalloc(sizeof(struct hci_usb), GFP_KERNEL))) {
888 BT_ERR("Can't allocate: control structure");
893 husb->bulk_out_ep = bulk_out_ep;
894 husb->bulk_in_ep = bulk_in_ep;
895 husb->intr_in_ep = intr_in_ep;
897 if (id->driver_info & HCI_DIGIANSWER)
898 husb->ctrl_req = USB_TYPE_VENDOR;
900 husb->ctrl_req = USB_TYPE_CLASS;
902 /* Find isochronous endpoints that we can use */
908 #ifdef CONFIG_BT_HCIUSB_SCO
909 if (isoc && !(id->driver_info & (HCI_BROKEN_ISOC | HCI_SNIFFER)))
910 isoc_iface = usb_ifnum_to_if(udev, isoc_ifnum);
914 struct usb_host_endpoint *isoc_out_ep = NULL;
915 struct usb_host_endpoint *isoc_in_ep = NULL;
917 for (a = 0; a < isoc_iface->num_altsetting; a++) {
918 uif = &isoc_iface->altsetting[a];
919 for (e = 0; e < uif->desc.bNumEndpoints; e++) {
920 ep = &uif->endpoint[e];
922 switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
923 case USB_ENDPOINT_XFER_ISOC:
924 if (le16_to_cpu(ep->desc.wMaxPacketSize) < size ||
925 uif->desc.bAlternateSetting != isoc)
927 size = le16_to_cpu(ep->desc.wMaxPacketSize);
929 isoc_alts = uif->desc.bAlternateSetting;
931 if (ep->desc.bEndpointAddress & USB_DIR_IN)
940 if (!isoc_in_ep || !isoc_out_ep)
941 BT_DBG("Isoc endpoints not found");
943 BT_DBG("isoc ifnum %d alts %d", isoc_ifnum, isoc_alts);
944 if (usb_driver_claim_interface(&hci_usb_driver, isoc_iface, husb) != 0)
945 BT_ERR("Can't claim isoc interface");
946 else if (usb_set_interface(udev, isoc_ifnum, isoc_alts)) {
947 BT_ERR("Can't set isoc interface settings");
948 husb->isoc_iface = isoc_iface;
949 usb_driver_release_interface(&hci_usb_driver, isoc_iface);
950 husb->isoc_iface = NULL;
952 husb->isoc_iface = isoc_iface;
953 husb->isoc_in_ep = isoc_in_ep;
954 husb->isoc_out_ep = isoc_out_ep;
960 rwlock_init(&husb->completion_lock);
962 for (i = 0; i < 4; i++) {
963 skb_queue_head_init(&husb->transmit_q[i]);
964 _urb_queue_init(&husb->pending_q[i]);
965 _urb_queue_init(&husb->completed_q[i]);
968 /* Initialize and register HCI device */
969 hdev = hci_alloc_dev();
971 BT_ERR("Can't allocate HCI device");
977 hdev->type = HCI_USB;
978 hdev->driver_data = husb;
979 SET_HCIDEV_DEV(hdev, &intf->dev);
981 hdev->open = hci_usb_open;
982 hdev->close = hci_usb_close;
983 hdev->flush = hci_usb_flush;
984 hdev->send = hci_usb_send_frame;
985 hdev->destruct = hci_usb_destruct;
986 hdev->notify = hci_usb_notify;
988 hdev->owner = THIS_MODULE;
990 if (reset || id->driver_info & HCI_RESET)
991 set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
993 if (id->driver_info & HCI_WRONG_SCO_MTU)
994 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
996 if (id->driver_info & HCI_SNIFFER) {
997 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
998 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
1001 if (id->driver_info & HCI_BCM92035) {
1002 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1003 struct sk_buff *skb;
1005 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1007 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1008 skb_queue_tail(&hdev->driver_init, skb);
1012 if (hci_register_dev(hdev) < 0) {
1013 BT_ERR("Can't register HCI device");
1018 usb_set_intfdata(intf, husb);
1022 if (husb->isoc_iface)
1023 usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
1030 static void hci_usb_disconnect(struct usb_interface *intf)
1032 struct hci_usb *husb = usb_get_intfdata(intf);
1033 struct hci_dev *hdev;
1035 if (!husb || intf == husb->isoc_iface)
1038 usb_set_intfdata(intf, NULL);
1041 BT_DBG("%s", hdev->name);
1043 hci_usb_close(hdev);
1045 if (husb->isoc_iface)
1046 usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
1048 if (hci_unregister_dev(hdev) < 0)
1049 BT_ERR("Can't unregister HCI device %s", hdev->name);
1054 static int hci_usb_suspend(struct usb_interface *intf, pm_message_t message)
1056 struct hci_usb *husb = usb_get_intfdata(intf);
1057 struct list_head killed;
1058 unsigned long flags;
1061 if (!husb || intf == husb->isoc_iface)
1064 hci_suspend_dev(husb->hdev);
1066 INIT_LIST_HEAD(&killed);
1068 for (i = 0; i < 4; i++) {
1069 struct _urb_queue *q = &husb->pending_q[i];
1070 struct _urb *_urb, *_tmp;
1072 while ((_urb = _urb_dequeue(q))) {
1073 /* reset queue since _urb_dequeue sets it to NULL */
1075 usb_kill_urb(&_urb->urb);
1076 list_add(&_urb->list, &killed);
1079 spin_lock_irqsave(&q->lock, flags);
1081 list_for_each_entry_safe(_urb, _tmp, &killed, list) {
1082 list_move_tail(&_urb->list, &q->head);
1085 spin_unlock_irqrestore(&q->lock, flags);
1091 static int hci_usb_resume(struct usb_interface *intf)
1093 struct hci_usb *husb = usb_get_intfdata(intf);
1094 unsigned long flags;
1097 if (!husb || intf == husb->isoc_iface)
1100 for (i = 0; i < 4; i++) {
1101 struct _urb_queue *q = &husb->pending_q[i];
1104 spin_lock_irqsave(&q->lock, flags);
1106 list_for_each_entry(_urb, &q->head, list) {
1107 err = usb_submit_urb(&_urb->urb, GFP_ATOMIC);
1112 spin_unlock_irqrestore(&q->lock, flags);
1118 hci_resume_dev(husb->hdev);
1123 static struct usb_driver hci_usb_driver = {
1125 .probe = hci_usb_probe,
1126 .disconnect = hci_usb_disconnect,
1127 .suspend = hci_usb_suspend,
1128 .resume = hci_usb_resume,
1129 .id_table = bluetooth_ids,
1132 static int __init hci_usb_init(void)
1136 BT_INFO("HCI USB driver ver %s", VERSION);
1138 if ((err = usb_register(&hci_usb_driver)) < 0)
1139 BT_ERR("Failed to register HCI USB driver");
1144 static void __exit hci_usb_exit(void)
1146 usb_deregister(&hci_usb_driver);
1149 module_init(hci_usb_init);
1150 module_exit(hci_usb_exit);
1152 module_param(ignore, bool, 0644);
1153 MODULE_PARM_DESC(ignore, "Ignore devices from the matching table");
1155 module_param(ignore_dga, bool, 0644);
1156 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1158 module_param(ignore_csr, bool, 0644);
1159 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1161 module_param(ignore_sniffer, bool, 0644);
1162 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1164 module_param(reset, bool, 0644);
1165 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1167 #ifdef CONFIG_BT_HCIUSB_SCO
1168 module_param(isoc, int, 0644);
1169 MODULE_PARM_DESC(isoc, "Set isochronous transfers for SCO over HCI support");
1172 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
1173 MODULE_DESCRIPTION("Bluetooth HCI USB driver ver " VERSION);
1174 MODULE_VERSION(VERSION);
1175 MODULE_LICENSE("GPL");