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/config.h>
35 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/init.h>
39 #include <linux/sched.h>
40 #include <linux/unistd.h>
41 #include <linux/types.h>
42 #include <linux/interrupt.h>
43 #include <linux/moduleparam.h>
45 #include <linux/slab.h>
46 #include <linux/errno.h>
47 #include <linux/string.h>
48 #include <linux/skbuff.h>
50 #include <linux/usb.h>
52 #include <net/bluetooth/bluetooth.h>
53 #include <net/bluetooth/hci_core.h>
57 #ifndef CONFIG_BT_HCIUSB_DEBUG
64 #ifndef CONFIG_BT_HCIUSB_ZERO_PACKET
65 #undef URB_ZERO_PACKET
66 #define URB_ZERO_PACKET 0
69 static int ignore = 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 /* Broadcom BCM2033 without firmware */
104 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = HCI_IGNORE },
106 /* Broadcom BCM2035 */
107 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = HCI_RESET | HCI_BROKEN_ISOC },
108 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = HCI_BCM92035 },
110 /* Microsoft Wireless Transceiver for Bluetooth 2.0 */
111 { USB_DEVICE(0x045e, 0x009c), .driver_info = HCI_RESET },
113 /* Kensington Bluetooth USB adapter */
114 { USB_DEVICE(0x047d, 0x105d), .driver_info = HCI_RESET },
116 /* ISSC Bluetooth Adapter v3.1 */
117 { USB_DEVICE(0x1131, 0x1001), .driver_info = HCI_RESET },
119 /* RTX Telecom based adapter with buggy SCO support */
120 { USB_DEVICE(0x0400, 0x0807), .driver_info = HCI_BROKEN_ISOC },
122 /* Digianswer devices */
123 { USB_DEVICE(0x08fd, 0x0001), .driver_info = HCI_DIGIANSWER },
124 { USB_DEVICE(0x08fd, 0x0002), .driver_info = HCI_IGNORE },
126 /* CSR BlueCore Bluetooth Sniffer */
127 { USB_DEVICE(0x0a12, 0x0002), .driver_info = HCI_SNIFFER },
129 { } /* Terminating entry */
132 static struct _urb *_urb_alloc(int isoc, int gfp)
134 struct _urb *_urb = kmalloc(sizeof(struct _urb) +
135 sizeof(struct usb_iso_packet_descriptor) * isoc, gfp);
137 memset(_urb, 0, sizeof(*_urb));
138 usb_init_urb(&_urb->urb);
143 static struct _urb *_urb_dequeue(struct _urb_queue *q)
145 struct _urb *_urb = NULL;
147 spin_lock_irqsave(&q->lock, flags);
149 struct list_head *head = &q->head;
150 struct list_head *next = head->next;
152 _urb = list_entry(next, struct _urb, list);
153 list_del(next); _urb->queue = NULL;
156 spin_unlock_irqrestore(&q->lock, flags);
160 static void hci_usb_rx_complete(struct urb *urb, struct pt_regs *regs);
161 static void hci_usb_tx_complete(struct urb *urb, struct pt_regs *regs);
163 #define __pending_tx(husb, type) (&husb->pending_tx[type-1])
164 #define __pending_q(husb, type) (&husb->pending_q[type-1])
165 #define __completed_q(husb, type) (&husb->completed_q[type-1])
166 #define __transmit_q(husb, type) (&husb->transmit_q[type-1])
167 #define __reassembly(husb, type) (husb->reassembly[type-1])
169 static inline struct _urb *__get_completed(struct hci_usb *husb, int type)
171 return _urb_dequeue(__completed_q(husb, type));
174 #ifdef CONFIG_BT_HCIUSB_SCO
175 static void __fill_isoc_desc(struct urb *urb, int len, int mtu)
179 BT_DBG("len %d mtu %d", len, mtu);
181 for (i=0; i < HCI_MAX_ISOC_FRAMES && len >= mtu; i++, offset += mtu, len -= mtu) {
182 urb->iso_frame_desc[i].offset = offset;
183 urb->iso_frame_desc[i].length = mtu;
184 BT_DBG("desc %d offset %d len %d", i, offset, mtu);
186 if (len && i < HCI_MAX_ISOC_FRAMES) {
187 urb->iso_frame_desc[i].offset = offset;
188 urb->iso_frame_desc[i].length = len;
189 BT_DBG("desc %d offset %d len %d", i, offset, len);
192 urb->number_of_packets = i;
196 static int hci_usb_intr_rx_submit(struct hci_usb *husb)
200 int err, pipe, interval, size;
203 BT_DBG("%s", husb->hdev->name);
205 size = le16_to_cpu(husb->intr_in_ep->desc.wMaxPacketSize);
207 buf = kmalloc(size, GFP_ATOMIC);
211 _urb = _urb_alloc(0, GFP_ATOMIC);
216 _urb->type = HCI_EVENT_PKT;
217 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
220 pipe = usb_rcvintpipe(husb->udev, husb->intr_in_ep->desc.bEndpointAddress);
221 interval = husb->intr_in_ep->desc.bInterval;
222 usb_fill_int_urb(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb, interval);
224 err = usb_submit_urb(urb, GFP_ATOMIC);
226 BT_ERR("%s intr rx submit failed urb %p err %d",
227 husb->hdev->name, urb, err);
235 static int hci_usb_bulk_rx_submit(struct hci_usb *husb)
239 int err, pipe, size = HCI_MAX_FRAME_SIZE;
242 buf = kmalloc(size, GFP_ATOMIC);
246 _urb = _urb_alloc(0, GFP_ATOMIC);
251 _urb->type = HCI_ACLDATA_PKT;
252 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
255 pipe = usb_rcvbulkpipe(husb->udev, husb->bulk_in_ep->desc.bEndpointAddress);
256 usb_fill_bulk_urb(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb);
257 urb->transfer_flags = 0;
259 BT_DBG("%s urb %p", husb->hdev->name, urb);
261 err = usb_submit_urb(urb, GFP_ATOMIC);
263 BT_ERR("%s bulk rx submit failed urb %p err %d",
264 husb->hdev->name, urb, err);
272 #ifdef CONFIG_BT_HCIUSB_SCO
273 static int hci_usb_isoc_rx_submit(struct hci_usb *husb)
280 mtu = le16_to_cpu(husb->isoc_in_ep->desc.wMaxPacketSize);
281 size = mtu * HCI_MAX_ISOC_FRAMES;
283 buf = kmalloc(size, GFP_ATOMIC);
287 _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
292 _urb->type = HCI_SCODATA_PKT;
293 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
298 urb->dev = husb->udev;
299 urb->pipe = usb_rcvisocpipe(husb->udev, husb->isoc_in_ep->desc.bEndpointAddress);
300 urb->complete = hci_usb_rx_complete;
302 urb->interval = husb->isoc_in_ep->desc.bInterval;
304 urb->transfer_buffer_length = size;
305 urb->transfer_buffer = buf;
306 urb->transfer_flags = URB_ISO_ASAP;
308 __fill_isoc_desc(urb, size, mtu);
310 BT_DBG("%s urb %p", husb->hdev->name, urb);
312 err = usb_submit_urb(urb, GFP_ATOMIC);
314 BT_ERR("%s isoc rx submit failed urb %p err %d",
315 husb->hdev->name, urb, err);
324 /* Initialize device */
325 static int hci_usb_open(struct hci_dev *hdev)
327 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
331 BT_DBG("%s", hdev->name);
333 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
336 write_lock_irqsave(&husb->completion_lock, flags);
338 err = hci_usb_intr_rx_submit(husb);
340 for (i = 0; i < HCI_MAX_BULK_RX; i++)
341 hci_usb_bulk_rx_submit(husb);
343 #ifdef CONFIG_BT_HCIUSB_SCO
344 if (husb->isoc_iface)
345 for (i = 0; i < HCI_MAX_ISOC_RX; i++)
346 hci_usb_isoc_rx_submit(husb);
349 clear_bit(HCI_RUNNING, &hdev->flags);
352 write_unlock_irqrestore(&husb->completion_lock, flags);
357 static int hci_usb_flush(struct hci_dev *hdev)
359 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
362 BT_DBG("%s", hdev->name);
364 for (i = 0; i < 4; i++)
365 skb_queue_purge(&husb->transmit_q[i]);
369 static void hci_usb_unlink_urbs(struct hci_usb *husb)
373 BT_DBG("%s", husb->hdev->name);
375 for (i = 0; i < 4; i++) {
379 /* Kill pending requests */
380 while ((_urb = _urb_dequeue(&husb->pending_q[i]))) {
382 BT_DBG("%s unlinking _urb %p type %d urb %p",
383 husb->hdev->name, _urb, _urb->type, urb);
385 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
388 /* Release completed requests */
389 while ((_urb = _urb_dequeue(&husb->completed_q[i]))) {
391 BT_DBG("%s freeing _urb %p type %d urb %p",
392 husb->hdev->name, _urb, _urb->type, urb);
393 if (urb->setup_packet)
394 kfree(urb->setup_packet);
395 if (urb->transfer_buffer)
396 kfree(urb->transfer_buffer);
400 /* Release reassembly buffers */
401 if (husb->reassembly[i]) {
402 kfree_skb(husb->reassembly[i]);
403 husb->reassembly[i] = NULL;
409 static int hci_usb_close(struct hci_dev *hdev)
411 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
414 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
417 BT_DBG("%s", hdev->name);
419 /* Synchronize with completion handlers */
420 write_lock_irqsave(&husb->completion_lock, flags);
421 write_unlock_irqrestore(&husb->completion_lock, flags);
423 hci_usb_unlink_urbs(husb);
428 static int __tx_submit(struct hci_usb *husb, struct _urb *_urb)
430 struct urb *urb = &_urb->urb;
433 BT_DBG("%s urb %p type %d", husb->hdev->name, urb, _urb->type);
435 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
436 err = usb_submit_urb(urb, GFP_ATOMIC);
438 BT_ERR("%s tx submit failed urb %p type %d err %d",
439 husb->hdev->name, urb, _urb->type, err);
441 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
443 atomic_inc(__pending_tx(husb, _urb->type));
448 static inline int hci_usb_send_ctrl(struct hci_usb *husb, struct sk_buff *skb)
450 struct _urb *_urb = __get_completed(husb, skb->pkt_type);
451 struct usb_ctrlrequest *dr;
455 _urb = _urb_alloc(0, GFP_ATOMIC);
458 _urb->type = skb->pkt_type;
460 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
466 dr = (void *) _urb->urb.setup_packet;
468 dr->bRequestType = husb->ctrl_req;
472 dr->wLength = __cpu_to_le16(skb->len);
475 usb_fill_control_urb(urb, husb->udev, usb_sndctrlpipe(husb->udev, 0),
476 (void *) dr, skb->data, skb->len, hci_usb_tx_complete, husb);
478 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
481 return __tx_submit(husb, _urb);
484 static inline int hci_usb_send_bulk(struct hci_usb *husb, struct sk_buff *skb)
486 struct _urb *_urb = __get_completed(husb, skb->pkt_type);
491 _urb = _urb_alloc(0, GFP_ATOMIC);
494 _urb->type = skb->pkt_type;
498 pipe = usb_sndbulkpipe(husb->udev, husb->bulk_out_ep->desc.bEndpointAddress);
499 usb_fill_bulk_urb(urb, husb->udev, pipe, skb->data, skb->len,
500 hci_usb_tx_complete, husb);
501 urb->transfer_flags = URB_ZERO_PACKET;
503 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
506 return __tx_submit(husb, _urb);
509 #ifdef CONFIG_BT_HCIUSB_SCO
510 static inline int hci_usb_send_isoc(struct hci_usb *husb, struct sk_buff *skb)
512 struct _urb *_urb = __get_completed(husb, skb->pkt_type);
516 _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
519 _urb->type = skb->pkt_type;
522 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
527 urb->dev = husb->udev;
528 urb->pipe = usb_sndisocpipe(husb->udev, husb->isoc_out_ep->desc.bEndpointAddress);
529 urb->complete = hci_usb_tx_complete;
530 urb->transfer_flags = URB_ISO_ASAP;
532 urb->interval = husb->isoc_out_ep->desc.bInterval;
534 urb->transfer_buffer = skb->data;
535 urb->transfer_buffer_length = skb->len;
537 __fill_isoc_desc(urb, skb->len, le16_to_cpu(husb->isoc_out_ep->desc.wMaxPacketSize));
540 return __tx_submit(husb, _urb);
544 static void hci_usb_tx_process(struct hci_usb *husb)
546 struct sk_buff_head *q;
549 BT_DBG("%s", husb->hdev->name);
552 clear_bit(HCI_USB_TX_WAKEUP, &husb->state);
554 /* Process command queue */
555 q = __transmit_q(husb, HCI_COMMAND_PKT);
556 if (!atomic_read(__pending_tx(husb, HCI_COMMAND_PKT)) &&
557 (skb = skb_dequeue(q))) {
558 if (hci_usb_send_ctrl(husb, skb) < 0)
559 skb_queue_head(q, skb);
562 #ifdef CONFIG_BT_HCIUSB_SCO
563 /* Process SCO queue */
564 q = __transmit_q(husb, HCI_SCODATA_PKT);
565 if (atomic_read(__pending_tx(husb, HCI_SCODATA_PKT)) < HCI_MAX_ISOC_TX &&
566 (skb = skb_dequeue(q))) {
567 if (hci_usb_send_isoc(husb, skb) < 0)
568 skb_queue_head(q, skb);
572 /* Process ACL queue */
573 q = __transmit_q(husb, HCI_ACLDATA_PKT);
574 while (atomic_read(__pending_tx(husb, HCI_ACLDATA_PKT)) < HCI_MAX_BULK_TX &&
575 (skb = skb_dequeue(q))) {
576 if (hci_usb_send_bulk(husb, skb) < 0) {
577 skb_queue_head(q, skb);
581 } while(test_bit(HCI_USB_TX_WAKEUP, &husb->state));
584 static inline void hci_usb_tx_wakeup(struct hci_usb *husb)
586 /* Serialize TX queue processing to avoid data reordering */
587 if (!test_and_set_bit(HCI_USB_TX_PROCESS, &husb->state)) {
588 hci_usb_tx_process(husb);
589 clear_bit(HCI_USB_TX_PROCESS, &husb->state);
591 set_bit(HCI_USB_TX_WAKEUP, &husb->state);
594 /* Send frames from HCI layer */
595 static int hci_usb_send_frame(struct sk_buff *skb)
597 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
598 struct hci_usb *husb;
601 BT_ERR("frame for uknown device (hdev=NULL)");
605 if (!test_bit(HCI_RUNNING, &hdev->flags))
608 BT_DBG("%s type %d len %d", hdev->name, skb->pkt_type, skb->len);
610 husb = (struct hci_usb *) hdev->driver_data;
612 switch (skb->pkt_type) {
613 case HCI_COMMAND_PKT:
617 case HCI_ACLDATA_PKT:
621 #ifdef CONFIG_BT_HCIUSB_SCO
622 case HCI_SCODATA_PKT:
632 read_lock(&husb->completion_lock);
634 skb_queue_tail(__transmit_q(husb, skb->pkt_type), skb);
635 hci_usb_tx_wakeup(husb);
637 read_unlock(&husb->completion_lock);
641 static inline int __recv_frame(struct hci_usb *husb, int type, void *data, int count)
643 BT_DBG("%s type %d data %p count %d", husb->hdev->name, type, data, count);
645 husb->hdev->stat.byte_rx += count;
648 struct sk_buff *skb = __reassembly(husb, type);
649 struct { int expect; } *scb;
653 /* Start of the frame */
657 if (count >= HCI_EVENT_HDR_SIZE) {
658 struct hci_event_hdr *h = data;
659 len = HCI_EVENT_HDR_SIZE + h->plen;
664 case HCI_ACLDATA_PKT:
665 if (count >= HCI_ACL_HDR_SIZE) {
666 struct hci_acl_hdr *h = data;
667 len = HCI_ACL_HDR_SIZE + __le16_to_cpu(h->dlen);
671 #ifdef CONFIG_BT_HCIUSB_SCO
672 case HCI_SCODATA_PKT:
673 if (count >= HCI_SCO_HDR_SIZE) {
674 struct hci_sco_hdr *h = data;
675 len = HCI_SCO_HDR_SIZE + h->dlen;
681 BT_DBG("new packet len %d", len);
683 skb = bt_skb_alloc(len, GFP_ATOMIC);
685 BT_ERR("%s no memory for the packet", husb->hdev->name);
688 skb->dev = (void *) husb->hdev;
689 skb->pkt_type = type;
691 __reassembly(husb, type) = skb;
693 scb = (void *) skb->cb;
697 scb = (void *) skb->cb;
701 len = min(len, count);
703 memcpy(skb_put(skb, len), data, len);
708 __reassembly(husb, type) = NULL;
712 count -= len; data += len;
717 static void hci_usb_rx_complete(struct urb *urb, struct pt_regs *regs)
719 struct _urb *_urb = container_of(urb, struct _urb, urb);
720 struct hci_usb *husb = (void *) urb->context;
721 struct hci_dev *hdev = husb->hdev;
722 int err, count = urb->actual_length;
724 BT_DBG("%s urb %p type %d status %d count %d flags %x", hdev->name, urb,
725 _urb->type, urb->status, count, urb->transfer_flags);
727 read_lock(&husb->completion_lock);
729 if (!test_bit(HCI_RUNNING, &hdev->flags))
732 if (urb->status || !count)
735 if (_urb->type == HCI_SCODATA_PKT) {
736 #ifdef CONFIG_BT_HCIUSB_SCO
738 for (i=0; i < urb->number_of_packets; i++) {
739 BT_DBG("desc %d status %d offset %d len %d", i,
740 urb->iso_frame_desc[i].status,
741 urb->iso_frame_desc[i].offset,
742 urb->iso_frame_desc[i].actual_length);
744 if (!urb->iso_frame_desc[i].status)
745 __recv_frame(husb, _urb->type,
746 urb->transfer_buffer + urb->iso_frame_desc[i].offset,
747 urb->iso_frame_desc[i].actual_length);
753 err = __recv_frame(husb, _urb->type, urb->transfer_buffer, count);
755 BT_ERR("%s corrupted packet: type %d count %d",
756 husb->hdev->name, _urb->type, count);
762 urb->dev = husb->udev;
763 err = usb_submit_urb(urb, GFP_ATOMIC);
764 BT_DBG("%s urb %p type %d resubmit status %d", hdev->name, urb,
768 read_unlock(&husb->completion_lock);
771 static void hci_usb_tx_complete(struct urb *urb, struct pt_regs *regs)
773 struct _urb *_urb = container_of(urb, struct _urb, urb);
774 struct hci_usb *husb = (void *) urb->context;
775 struct hci_dev *hdev = husb->hdev;
777 BT_DBG("%s urb %p status %d flags %x", hdev->name, urb,
778 urb->status, urb->transfer_flags);
780 atomic_dec(__pending_tx(husb, _urb->type));
782 urb->transfer_buffer = NULL;
783 kfree_skb((struct sk_buff *) _urb->priv);
785 if (!test_bit(HCI_RUNNING, &hdev->flags))
789 hdev->stat.byte_tx += urb->transfer_buffer_length;
793 read_lock(&husb->completion_lock);
796 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
798 hci_usb_tx_wakeup(husb);
800 read_unlock(&husb->completion_lock);
803 static void hci_usb_destruct(struct hci_dev *hdev)
805 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
807 BT_DBG("%s", hdev->name);
812 static void hci_usb_notify(struct hci_dev *hdev, unsigned int evt)
814 BT_DBG("%s evt %d", hdev->name, evt);
817 static int hci_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
819 struct usb_device *udev = interface_to_usbdev(intf);
820 struct usb_host_endpoint *bulk_out_ep = NULL;
821 struct usb_host_endpoint *bulk_in_ep = NULL;
822 struct usb_host_endpoint *intr_in_ep = NULL;
823 struct usb_host_endpoint *ep;
824 struct usb_host_interface *uif;
825 struct usb_interface *isoc_iface;
826 struct hci_usb *husb;
827 struct hci_dev *hdev;
828 int i, e, size, isoc_ifnum, isoc_alts;
830 BT_DBG("udev %p intf %p", udev, intf);
832 if (!id->driver_info) {
833 const struct usb_device_id *match;
834 match = usb_match_id(intf, blacklist_ids);
839 if (ignore || id->driver_info & HCI_IGNORE)
842 if (intf->cur_altsetting->desc.bInterfaceNumber > 0)
845 /* Find endpoints that we need */
846 uif = intf->cur_altsetting;
847 for (e = 0; e < uif->desc.bNumEndpoints; e++) {
848 ep = &uif->endpoint[e];
850 switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
851 case USB_ENDPOINT_XFER_INT:
852 if (ep->desc.bEndpointAddress & USB_DIR_IN)
856 case USB_ENDPOINT_XFER_BULK:
857 if (ep->desc.bEndpointAddress & USB_DIR_IN)
865 if (!bulk_in_ep || !bulk_out_ep || !intr_in_ep) {
866 BT_DBG("Bulk endpoints not found");
870 if (!(husb = kmalloc(sizeof(struct hci_usb), GFP_KERNEL))) {
871 BT_ERR("Can't allocate: control structure");
875 memset(husb, 0, sizeof(struct hci_usb));
878 husb->bulk_out_ep = bulk_out_ep;
879 husb->bulk_in_ep = bulk_in_ep;
880 husb->intr_in_ep = intr_in_ep;
882 if (id->driver_info & HCI_DIGIANSWER)
883 husb->ctrl_req = USB_TYPE_VENDOR;
885 husb->ctrl_req = USB_TYPE_CLASS;
887 /* Find isochronous endpoints that we can use */
893 #ifdef CONFIG_BT_HCIUSB_SCO
894 if (isoc && !(id->driver_info & (HCI_BROKEN_ISOC | HCI_SNIFFER)))
895 isoc_iface = usb_ifnum_to_if(udev, isoc_ifnum);
899 struct usb_host_endpoint *isoc_out_ep = NULL;
900 struct usb_host_endpoint *isoc_in_ep = NULL;
902 for (a = 0; a < isoc_iface->num_altsetting; a++) {
903 uif = &isoc_iface->altsetting[a];
904 for (e = 0; e < uif->desc.bNumEndpoints; e++) {
905 ep = &uif->endpoint[e];
907 switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
908 case USB_ENDPOINT_XFER_ISOC:
909 if (le16_to_cpu(ep->desc.wMaxPacketSize) < size ||
910 uif->desc.bAlternateSetting != isoc)
912 size = le16_to_cpu(ep->desc.wMaxPacketSize);
914 isoc_alts = uif->desc.bAlternateSetting;
916 if (ep->desc.bEndpointAddress & USB_DIR_IN)
925 if (!isoc_in_ep || !isoc_out_ep)
926 BT_DBG("Isoc endpoints not found");
928 BT_DBG("isoc ifnum %d alts %d", isoc_ifnum, isoc_alts);
929 if (usb_driver_claim_interface(&hci_usb_driver, isoc_iface, husb) != 0)
930 BT_ERR("Can't claim isoc interface");
931 else if (usb_set_interface(udev, isoc_ifnum, isoc_alts)) {
932 BT_ERR("Can't set isoc interface settings");
933 husb->isoc_iface = isoc_iface;
934 usb_driver_release_interface(&hci_usb_driver, isoc_iface);
935 husb->isoc_iface = NULL;
937 husb->isoc_iface = isoc_iface;
938 husb->isoc_in_ep = isoc_in_ep;
939 husb->isoc_out_ep = isoc_out_ep;
945 rwlock_init(&husb->completion_lock);
947 for (i = 0; i < 4; i++) {
948 skb_queue_head_init(&husb->transmit_q[i]);
949 _urb_queue_init(&husb->pending_q[i]);
950 _urb_queue_init(&husb->completed_q[i]);
953 /* Initialize and register HCI device */
954 hdev = hci_alloc_dev();
956 BT_ERR("Can't allocate HCI device");
962 hdev->type = HCI_USB;
963 hdev->driver_data = husb;
964 SET_HCIDEV_DEV(hdev, &intf->dev);
966 hdev->open = hci_usb_open;
967 hdev->close = hci_usb_close;
968 hdev->flush = hci_usb_flush;
969 hdev->send = hci_usb_send_frame;
970 hdev->destruct = hci_usb_destruct;
971 hdev->notify = hci_usb_notify;
973 hdev->owner = THIS_MODULE;
975 if (reset || id->driver_info & HCI_RESET)
976 set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
978 if (id->driver_info & HCI_SNIFFER) {
979 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
980 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
983 if (id->driver_info & HCI_BCM92035) {
984 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
987 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
989 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
990 skb_queue_tail(&hdev->driver_init, skb);
994 if (hci_register_dev(hdev) < 0) {
995 BT_ERR("Can't register HCI device");
1000 usb_set_intfdata(intf, husb);
1004 if (husb->isoc_iface)
1005 usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
1012 static void hci_usb_disconnect(struct usb_interface *intf)
1014 struct hci_usb *husb = usb_get_intfdata(intf);
1015 struct hci_dev *hdev;
1017 if (!husb || intf == husb->isoc_iface)
1020 usb_set_intfdata(intf, NULL);
1023 BT_DBG("%s", hdev->name);
1025 hci_usb_close(hdev);
1027 if (husb->isoc_iface)
1028 usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
1030 if (hci_unregister_dev(hdev) < 0)
1031 BT_ERR("Can't unregister HCI device %s", hdev->name);
1036 static struct usb_driver hci_usb_driver = {
1037 .owner = THIS_MODULE,
1039 .probe = hci_usb_probe,
1040 .disconnect = hci_usb_disconnect,
1041 .id_table = bluetooth_ids,
1044 static int __init hci_usb_init(void)
1048 BT_INFO("HCI USB driver ver %s", VERSION);
1050 if ((err = usb_register(&hci_usb_driver)) < 0)
1051 BT_ERR("Failed to register HCI USB driver");
1056 static void __exit hci_usb_exit(void)
1058 usb_deregister(&hci_usb_driver);
1061 module_init(hci_usb_init);
1062 module_exit(hci_usb_exit);
1064 module_param(ignore, bool, 0644);
1065 MODULE_PARM_DESC(ignore, "Ignore devices from the matching table");
1067 module_param(reset, bool, 0644);
1068 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1070 #ifdef CONFIG_BT_HCIUSB_SCO
1071 module_param(isoc, int, 0644);
1072 MODULE_PARM_DESC(isoc, "Set isochronous transfers for SCO over HCI support");
1075 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
1076 MODULE_DESCRIPTION("Bluetooth HCI USB driver ver " VERSION);
1077 MODULE_VERSION(VERSION);
1078 MODULE_LICENSE("GPL");