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;
70 static int disable_scofix = 0;
71 static int force_scofix = 0;
74 #ifdef CONFIG_BT_HCIUSB_SCO
80 static struct usb_driver hci_usb_driver;
82 static struct usb_device_id bluetooth_ids[] = {
83 /* Generic Bluetooth USB device */
84 { USB_DEVICE_INFO(HCI_DEV_CLASS, HCI_DEV_SUBCLASS, HCI_DEV_PROTOCOL) },
86 /* AVM BlueFRITZ! USB v2.0 */
87 { USB_DEVICE(0x057c, 0x3800) },
89 /* Bluetooth Ultraport Module from IBM */
90 { USB_DEVICE(0x04bf, 0x030a) },
92 /* ALPS Modules with non-standard id */
93 { USB_DEVICE(0x044e, 0x3001) },
94 { USB_DEVICE(0x044e, 0x3002) },
96 /* Ericsson with non-standard id */
97 { USB_DEVICE(0x0bdb, 0x1002) },
99 /* Canyon CN-BTU1 with HID interfaces */
100 { USB_DEVICE(0x0c10, 0x0000), .driver_info = HCI_RESET },
102 { } /* Terminating entry */
105 MODULE_DEVICE_TABLE (usb, bluetooth_ids);
107 static struct usb_device_id blacklist_ids[] = {
108 /* CSR BlueCore devices */
109 { USB_DEVICE(0x0a12, 0x0001), .driver_info = HCI_CSR },
111 /* Broadcom BCM2033 without firmware */
112 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = HCI_IGNORE },
114 /* Broadcom BCM2035 */
115 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
116 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = HCI_BCM92035 },
118 /* IBM/Lenovo ThinkPad with Broadcom chip */
119 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = HCI_WRONG_SCO_MTU },
121 /* ANYCOM Bluetooth USB-200 and USB-250 */
122 { USB_DEVICE(0x0a5c, 0x2111), .driver_info = HCI_RESET },
124 /* Microsoft Wireless Transceiver for Bluetooth 2.0 */
125 { USB_DEVICE(0x045e, 0x009c), .driver_info = HCI_RESET },
127 /* Kensington Bluetooth USB adapter */
128 { USB_DEVICE(0x047d, 0x105d), .driver_info = HCI_RESET },
130 /* ISSC Bluetooth Adapter v3.1 */
131 { USB_DEVICE(0x1131, 0x1001), .driver_info = HCI_RESET },
133 /* RTX Telecom based adapters with buggy SCO support */
134 { USB_DEVICE(0x0400, 0x0807), .driver_info = HCI_BROKEN_ISOC },
135 { USB_DEVICE(0x0400, 0x080a), .driver_info = HCI_BROKEN_ISOC },
137 /* Belkin F8T012 and F8T013 devices */
138 { USB_DEVICE(0x050d, 0x0012), .driver_info = HCI_WRONG_SCO_MTU },
139 { USB_DEVICE(0x050d, 0x0013), .driver_info = HCI_WRONG_SCO_MTU },
141 /* Digianswer devices */
142 { USB_DEVICE(0x08fd, 0x0001), .driver_info = HCI_DIGIANSWER },
143 { USB_DEVICE(0x08fd, 0x0002), .driver_info = HCI_IGNORE },
145 /* CSR BlueCore Bluetooth Sniffer */
146 { USB_DEVICE(0x0a12, 0x0002), .driver_info = HCI_SNIFFER },
148 /* Frontline ComProbe Bluetooth Sniffer */
149 { USB_DEVICE(0x16d3, 0x0002), .driver_info = HCI_SNIFFER },
151 { } /* Terminating entry */
154 static struct _urb *_urb_alloc(int isoc, gfp_t gfp)
156 struct _urb *_urb = kmalloc(sizeof(struct _urb) +
157 sizeof(struct usb_iso_packet_descriptor) * isoc, gfp);
159 memset(_urb, 0, sizeof(*_urb));
160 usb_init_urb(&_urb->urb);
165 static struct _urb *_urb_dequeue(struct _urb_queue *q)
167 struct _urb *_urb = NULL;
169 spin_lock_irqsave(&q->lock, flags);
171 struct list_head *head = &q->head;
172 struct list_head *next = head->next;
174 _urb = list_entry(next, struct _urb, list);
175 list_del(next); _urb->queue = NULL;
178 spin_unlock_irqrestore(&q->lock, flags);
182 static void hci_usb_rx_complete(struct urb *urb);
183 static void hci_usb_tx_complete(struct urb *urb);
185 #define __pending_tx(husb, type) (&husb->pending_tx[type-1])
186 #define __pending_q(husb, type) (&husb->pending_q[type-1])
187 #define __completed_q(husb, type) (&husb->completed_q[type-1])
188 #define __transmit_q(husb, type) (&husb->transmit_q[type-1])
189 #define __reassembly(husb, type) (husb->reassembly[type-1])
191 static inline struct _urb *__get_completed(struct hci_usb *husb, int type)
193 return _urb_dequeue(__completed_q(husb, type));
196 #ifdef CONFIG_BT_HCIUSB_SCO
197 static void __fill_isoc_desc(struct urb *urb, int len, int mtu)
201 BT_DBG("len %d mtu %d", len, mtu);
203 for (i=0; i < HCI_MAX_ISOC_FRAMES && len >= mtu; i++, offset += mtu, len -= mtu) {
204 urb->iso_frame_desc[i].offset = offset;
205 urb->iso_frame_desc[i].length = mtu;
206 BT_DBG("desc %d offset %d len %d", i, offset, mtu);
208 if (len && i < HCI_MAX_ISOC_FRAMES) {
209 urb->iso_frame_desc[i].offset = offset;
210 urb->iso_frame_desc[i].length = len;
211 BT_DBG("desc %d offset %d len %d", i, offset, len);
214 urb->number_of_packets = i;
218 static int hci_usb_intr_rx_submit(struct hci_usb *husb)
222 int err, pipe, interval, size;
225 BT_DBG("%s", husb->hdev->name);
227 size = le16_to_cpu(husb->intr_in_ep->desc.wMaxPacketSize);
229 buf = kmalloc(size, GFP_ATOMIC);
233 _urb = _urb_alloc(0, GFP_ATOMIC);
238 _urb->type = HCI_EVENT_PKT;
239 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
242 pipe = usb_rcvintpipe(husb->udev, husb->intr_in_ep->desc.bEndpointAddress);
243 interval = husb->intr_in_ep->desc.bInterval;
244 usb_fill_int_urb(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb, interval);
246 err = usb_submit_urb(urb, GFP_ATOMIC);
248 BT_ERR("%s intr rx submit failed urb %p err %d",
249 husb->hdev->name, urb, err);
257 static int hci_usb_bulk_rx_submit(struct hci_usb *husb)
261 int err, pipe, size = HCI_MAX_FRAME_SIZE;
264 buf = kmalloc(size, GFP_ATOMIC);
268 _urb = _urb_alloc(0, GFP_ATOMIC);
273 _urb->type = HCI_ACLDATA_PKT;
274 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
277 pipe = usb_rcvbulkpipe(husb->udev, husb->bulk_in_ep->desc.bEndpointAddress);
278 usb_fill_bulk_urb(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb);
279 urb->transfer_flags = 0;
281 BT_DBG("%s urb %p", husb->hdev->name, urb);
283 err = usb_submit_urb(urb, GFP_ATOMIC);
285 BT_ERR("%s bulk rx submit failed urb %p err %d",
286 husb->hdev->name, urb, err);
294 #ifdef CONFIG_BT_HCIUSB_SCO
295 static int hci_usb_isoc_rx_submit(struct hci_usb *husb)
302 mtu = le16_to_cpu(husb->isoc_in_ep->desc.wMaxPacketSize);
303 size = mtu * HCI_MAX_ISOC_FRAMES;
305 buf = kmalloc(size, GFP_ATOMIC);
309 _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
314 _urb->type = HCI_SCODATA_PKT;
315 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
320 urb->dev = husb->udev;
321 urb->pipe = usb_rcvisocpipe(husb->udev, husb->isoc_in_ep->desc.bEndpointAddress);
322 urb->complete = hci_usb_rx_complete;
324 urb->interval = husb->isoc_in_ep->desc.bInterval;
326 urb->transfer_buffer_length = size;
327 urb->transfer_buffer = buf;
328 urb->transfer_flags = URB_ISO_ASAP;
330 __fill_isoc_desc(urb, size, mtu);
332 BT_DBG("%s urb %p", husb->hdev->name, urb);
334 err = usb_submit_urb(urb, GFP_ATOMIC);
336 BT_ERR("%s isoc rx submit failed urb %p err %d",
337 husb->hdev->name, urb, err);
346 /* Initialize device */
347 static int hci_usb_open(struct hci_dev *hdev)
349 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
353 BT_DBG("%s", hdev->name);
355 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
358 write_lock_irqsave(&husb->completion_lock, flags);
360 err = hci_usb_intr_rx_submit(husb);
362 for (i = 0; i < HCI_MAX_BULK_RX; i++)
363 hci_usb_bulk_rx_submit(husb);
365 #ifdef CONFIG_BT_HCIUSB_SCO
366 if (husb->isoc_iface)
367 for (i = 0; i < HCI_MAX_ISOC_RX; i++)
368 hci_usb_isoc_rx_submit(husb);
371 clear_bit(HCI_RUNNING, &hdev->flags);
374 write_unlock_irqrestore(&husb->completion_lock, flags);
379 static int hci_usb_flush(struct hci_dev *hdev)
381 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
384 BT_DBG("%s", hdev->name);
386 for (i = 0; i < 4; i++)
387 skb_queue_purge(&husb->transmit_q[i]);
391 static void hci_usb_unlink_urbs(struct hci_usb *husb)
395 BT_DBG("%s", husb->hdev->name);
397 for (i = 0; i < 4; i++) {
401 /* Kill pending requests */
402 while ((_urb = _urb_dequeue(&husb->pending_q[i]))) {
404 BT_DBG("%s unlinking _urb %p type %d urb %p",
405 husb->hdev->name, _urb, _urb->type, urb);
407 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
410 /* Release completed requests */
411 while ((_urb = _urb_dequeue(&husb->completed_q[i]))) {
413 BT_DBG("%s freeing _urb %p type %d urb %p",
414 husb->hdev->name, _urb, _urb->type, urb);
415 kfree(urb->setup_packet);
416 kfree(urb->transfer_buffer);
420 /* Release reassembly buffers */
421 if (husb->reassembly[i]) {
422 kfree_skb(husb->reassembly[i]);
423 husb->reassembly[i] = NULL;
429 static int hci_usb_close(struct hci_dev *hdev)
431 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
434 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
437 BT_DBG("%s", hdev->name);
439 /* Synchronize with completion handlers */
440 write_lock_irqsave(&husb->completion_lock, flags);
441 write_unlock_irqrestore(&husb->completion_lock, flags);
443 hci_usb_unlink_urbs(husb);
448 static int __tx_submit(struct hci_usb *husb, struct _urb *_urb)
450 struct urb *urb = &_urb->urb;
453 BT_DBG("%s urb %p type %d", husb->hdev->name, urb, _urb->type);
455 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
456 err = usb_submit_urb(urb, GFP_ATOMIC);
458 BT_ERR("%s tx submit failed urb %p type %d err %d",
459 husb->hdev->name, urb, _urb->type, err);
461 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
463 atomic_inc(__pending_tx(husb, _urb->type));
468 static inline int hci_usb_send_ctrl(struct hci_usb *husb, struct sk_buff *skb)
470 struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
471 struct usb_ctrlrequest *dr;
475 _urb = _urb_alloc(0, GFP_ATOMIC);
478 _urb->type = bt_cb(skb)->pkt_type;
480 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
486 dr = (void *) _urb->urb.setup_packet;
488 dr->bRequestType = husb->ctrl_req;
492 dr->wLength = __cpu_to_le16(skb->len);
495 usb_fill_control_urb(urb, husb->udev, usb_sndctrlpipe(husb->udev, 0),
496 (void *) dr, skb->data, skb->len, hci_usb_tx_complete, husb);
498 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
501 return __tx_submit(husb, _urb);
504 static inline int hci_usb_send_bulk(struct hci_usb *husb, struct sk_buff *skb)
506 struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
511 _urb = _urb_alloc(0, GFP_ATOMIC);
514 _urb->type = bt_cb(skb)->pkt_type;
518 pipe = usb_sndbulkpipe(husb->udev, husb->bulk_out_ep->desc.bEndpointAddress);
519 usb_fill_bulk_urb(urb, husb->udev, pipe, skb->data, skb->len,
520 hci_usb_tx_complete, husb);
521 urb->transfer_flags = URB_ZERO_PACKET;
523 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
526 return __tx_submit(husb, _urb);
529 #ifdef CONFIG_BT_HCIUSB_SCO
530 static inline int hci_usb_send_isoc(struct hci_usb *husb, struct sk_buff *skb)
532 struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
536 _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
539 _urb->type = bt_cb(skb)->pkt_type;
542 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
547 urb->dev = husb->udev;
548 urb->pipe = usb_sndisocpipe(husb->udev, husb->isoc_out_ep->desc.bEndpointAddress);
549 urb->complete = hci_usb_tx_complete;
550 urb->transfer_flags = URB_ISO_ASAP;
552 urb->interval = husb->isoc_out_ep->desc.bInterval;
554 urb->transfer_buffer = skb->data;
555 urb->transfer_buffer_length = skb->len;
557 __fill_isoc_desc(urb, skb->len, le16_to_cpu(husb->isoc_out_ep->desc.wMaxPacketSize));
560 return __tx_submit(husb, _urb);
564 static void hci_usb_tx_process(struct hci_usb *husb)
566 struct sk_buff_head *q;
569 BT_DBG("%s", husb->hdev->name);
572 clear_bit(HCI_USB_TX_WAKEUP, &husb->state);
574 /* Process command queue */
575 q = __transmit_q(husb, HCI_COMMAND_PKT);
576 if (!atomic_read(__pending_tx(husb, HCI_COMMAND_PKT)) &&
577 (skb = skb_dequeue(q))) {
578 if (hci_usb_send_ctrl(husb, skb) < 0)
579 skb_queue_head(q, skb);
582 #ifdef CONFIG_BT_HCIUSB_SCO
583 /* Process SCO queue */
584 q = __transmit_q(husb, HCI_SCODATA_PKT);
585 if (atomic_read(__pending_tx(husb, HCI_SCODATA_PKT)) < HCI_MAX_ISOC_TX &&
586 (skb = skb_dequeue(q))) {
587 if (hci_usb_send_isoc(husb, skb) < 0)
588 skb_queue_head(q, skb);
592 /* Process ACL queue */
593 q = __transmit_q(husb, HCI_ACLDATA_PKT);
594 while (atomic_read(__pending_tx(husb, HCI_ACLDATA_PKT)) < HCI_MAX_BULK_TX &&
595 (skb = skb_dequeue(q))) {
596 if (hci_usb_send_bulk(husb, skb) < 0) {
597 skb_queue_head(q, skb);
601 } while(test_bit(HCI_USB_TX_WAKEUP, &husb->state));
604 static inline void hci_usb_tx_wakeup(struct hci_usb *husb)
606 /* Serialize TX queue processing to avoid data reordering */
607 if (!test_and_set_bit(HCI_USB_TX_PROCESS, &husb->state)) {
608 hci_usb_tx_process(husb);
609 clear_bit(HCI_USB_TX_PROCESS, &husb->state);
611 set_bit(HCI_USB_TX_WAKEUP, &husb->state);
614 /* Send frames from HCI layer */
615 static int hci_usb_send_frame(struct sk_buff *skb)
617 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
618 struct hci_usb *husb;
621 BT_ERR("frame for uknown device (hdev=NULL)");
625 if (!test_bit(HCI_RUNNING, &hdev->flags))
628 BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
630 husb = (struct hci_usb *) hdev->driver_data;
632 switch (bt_cb(skb)->pkt_type) {
633 case HCI_COMMAND_PKT:
637 case HCI_ACLDATA_PKT:
641 #ifdef CONFIG_BT_HCIUSB_SCO
642 case HCI_SCODATA_PKT:
652 read_lock(&husb->completion_lock);
654 skb_queue_tail(__transmit_q(husb, bt_cb(skb)->pkt_type), skb);
655 hci_usb_tx_wakeup(husb);
657 read_unlock(&husb->completion_lock);
661 static inline int __recv_frame(struct hci_usb *husb, int type, void *data, int count)
663 BT_DBG("%s type %d data %p count %d", husb->hdev->name, type, data, count);
665 husb->hdev->stat.byte_rx += count;
668 struct sk_buff *skb = __reassembly(husb, type);
669 struct { int expect; } *scb;
673 /* Start of the frame */
677 if (count >= HCI_EVENT_HDR_SIZE) {
678 struct hci_event_hdr *h = data;
679 len = HCI_EVENT_HDR_SIZE + h->plen;
684 case HCI_ACLDATA_PKT:
685 if (count >= HCI_ACL_HDR_SIZE) {
686 struct hci_acl_hdr *h = data;
687 len = HCI_ACL_HDR_SIZE + __le16_to_cpu(h->dlen);
691 #ifdef CONFIG_BT_HCIUSB_SCO
692 case HCI_SCODATA_PKT:
693 if (count >= HCI_SCO_HDR_SIZE) {
694 struct hci_sco_hdr *h = data;
695 len = HCI_SCO_HDR_SIZE + h->dlen;
701 BT_DBG("new packet len %d", len);
703 skb = bt_skb_alloc(len, GFP_ATOMIC);
705 BT_ERR("%s no memory for the packet", husb->hdev->name);
708 skb->dev = (void *) husb->hdev;
709 bt_cb(skb)->pkt_type = type;
711 __reassembly(husb, type) = skb;
713 scb = (void *) skb->cb;
717 scb = (void *) skb->cb;
721 len = min(len, count);
723 memcpy(skb_put(skb, len), data, len);
728 __reassembly(husb, type) = NULL;
729 bt_cb(skb)->pkt_type = type;
733 count -= len; data += len;
738 static void hci_usb_rx_complete(struct urb *urb)
740 struct _urb *_urb = container_of(urb, struct _urb, urb);
741 struct hci_usb *husb = (void *) urb->context;
742 struct hci_dev *hdev = husb->hdev;
743 int err, count = urb->actual_length;
745 BT_DBG("%s urb %p type %d status %d count %d flags %x", hdev->name, urb,
746 _urb->type, urb->status, count, urb->transfer_flags);
748 read_lock(&husb->completion_lock);
750 if (!test_bit(HCI_RUNNING, &hdev->flags))
753 if (urb->status || !count)
756 if (_urb->type == HCI_SCODATA_PKT) {
757 #ifdef CONFIG_BT_HCIUSB_SCO
759 for (i=0; i < urb->number_of_packets; i++) {
760 BT_DBG("desc %d status %d offset %d len %d", i,
761 urb->iso_frame_desc[i].status,
762 urb->iso_frame_desc[i].offset,
763 urb->iso_frame_desc[i].actual_length);
765 if (!urb->iso_frame_desc[i].status)
766 __recv_frame(husb, _urb->type,
767 urb->transfer_buffer + urb->iso_frame_desc[i].offset,
768 urb->iso_frame_desc[i].actual_length);
774 err = __recv_frame(husb, _urb->type, urb->transfer_buffer, count);
776 BT_ERR("%s corrupted packet: type %d count %d",
777 husb->hdev->name, _urb->type, count);
783 urb->dev = husb->udev;
784 err = usb_submit_urb(urb, GFP_ATOMIC);
785 BT_DBG("%s urb %p type %d resubmit status %d", hdev->name, urb,
789 read_unlock(&husb->completion_lock);
792 static void hci_usb_tx_complete(struct urb *urb)
794 struct _urb *_urb = container_of(urb, struct _urb, urb);
795 struct hci_usb *husb = (void *) urb->context;
796 struct hci_dev *hdev = husb->hdev;
798 BT_DBG("%s urb %p status %d flags %x", hdev->name, urb,
799 urb->status, urb->transfer_flags);
801 atomic_dec(__pending_tx(husb, _urb->type));
803 urb->transfer_buffer = NULL;
804 kfree_skb((struct sk_buff *) _urb->priv);
806 if (!test_bit(HCI_RUNNING, &hdev->flags))
810 hdev->stat.byte_tx += urb->transfer_buffer_length;
814 read_lock(&husb->completion_lock);
817 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
819 hci_usb_tx_wakeup(husb);
821 read_unlock(&husb->completion_lock);
824 static void hci_usb_destruct(struct hci_dev *hdev)
826 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
828 BT_DBG("%s", hdev->name);
833 static void hci_usb_notify(struct hci_dev *hdev, unsigned int evt)
835 BT_DBG("%s evt %d", hdev->name, evt);
838 static int hci_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
840 struct usb_device *udev = interface_to_usbdev(intf);
841 struct usb_host_endpoint *bulk_out_ep = NULL;
842 struct usb_host_endpoint *bulk_in_ep = NULL;
843 struct usb_host_endpoint *intr_in_ep = NULL;
844 struct usb_host_endpoint *ep;
845 struct usb_host_interface *uif;
846 struct usb_interface *isoc_iface;
847 struct hci_usb *husb;
848 struct hci_dev *hdev;
849 int i, e, size, isoc_ifnum, isoc_alts;
851 BT_DBG("udev %p intf %p", udev, intf);
853 if (!id->driver_info) {
854 const struct usb_device_id *match;
855 match = usb_match_id(intf, blacklist_ids);
860 if (ignore || id->driver_info & HCI_IGNORE)
863 if (ignore_dga && id->driver_info & HCI_DIGIANSWER)
866 if (ignore_csr && id->driver_info & HCI_CSR)
869 if (ignore_sniffer && id->driver_info & HCI_SNIFFER)
872 if (intf->cur_altsetting->desc.bInterfaceNumber > 0)
875 /* Find endpoints that we need */
876 uif = intf->cur_altsetting;
877 for (e = 0; e < uif->desc.bNumEndpoints; e++) {
878 ep = &uif->endpoint[e];
880 switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
881 case USB_ENDPOINT_XFER_INT:
882 if (ep->desc.bEndpointAddress & USB_DIR_IN)
886 case USB_ENDPOINT_XFER_BULK:
887 if (ep->desc.bEndpointAddress & USB_DIR_IN)
895 if (!bulk_in_ep || !bulk_out_ep || !intr_in_ep) {
896 BT_DBG("Bulk endpoints not found");
900 if (!(husb = kzalloc(sizeof(struct hci_usb), GFP_KERNEL))) {
901 BT_ERR("Can't allocate: control structure");
906 husb->bulk_out_ep = bulk_out_ep;
907 husb->bulk_in_ep = bulk_in_ep;
908 husb->intr_in_ep = intr_in_ep;
910 if (id->driver_info & HCI_DIGIANSWER)
911 husb->ctrl_req = USB_TYPE_VENDOR;
913 husb->ctrl_req = USB_TYPE_CLASS;
915 /* Find isochronous endpoints that we can use */
921 #ifdef CONFIG_BT_HCIUSB_SCO
922 if (isoc && !(id->driver_info & (HCI_BROKEN_ISOC | HCI_SNIFFER)))
923 isoc_iface = usb_ifnum_to_if(udev, isoc_ifnum);
927 struct usb_host_endpoint *isoc_out_ep = NULL;
928 struct usb_host_endpoint *isoc_in_ep = NULL;
930 for (a = 0; a < isoc_iface->num_altsetting; a++) {
931 uif = &isoc_iface->altsetting[a];
932 for (e = 0; e < uif->desc.bNumEndpoints; e++) {
933 ep = &uif->endpoint[e];
935 switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
936 case USB_ENDPOINT_XFER_ISOC:
937 if (le16_to_cpu(ep->desc.wMaxPacketSize) < size ||
938 uif->desc.bAlternateSetting != isoc)
940 size = le16_to_cpu(ep->desc.wMaxPacketSize);
942 isoc_alts = uif->desc.bAlternateSetting;
944 if (ep->desc.bEndpointAddress & USB_DIR_IN)
953 if (!isoc_in_ep || !isoc_out_ep)
954 BT_DBG("Isoc endpoints not found");
956 BT_DBG("isoc ifnum %d alts %d", isoc_ifnum, isoc_alts);
957 if (usb_driver_claim_interface(&hci_usb_driver, isoc_iface, husb) != 0)
958 BT_ERR("Can't claim isoc interface");
959 else if (usb_set_interface(udev, isoc_ifnum, isoc_alts)) {
960 BT_ERR("Can't set isoc interface settings");
961 husb->isoc_iface = isoc_iface;
962 usb_driver_release_interface(&hci_usb_driver, isoc_iface);
963 husb->isoc_iface = NULL;
965 husb->isoc_iface = isoc_iface;
966 husb->isoc_in_ep = isoc_in_ep;
967 husb->isoc_out_ep = isoc_out_ep;
973 rwlock_init(&husb->completion_lock);
975 for (i = 0; i < 4; i++) {
976 skb_queue_head_init(&husb->transmit_q[i]);
977 _urb_queue_init(&husb->pending_q[i]);
978 _urb_queue_init(&husb->completed_q[i]);
981 /* Initialize and register HCI device */
982 hdev = hci_alloc_dev();
984 BT_ERR("Can't allocate HCI device");
990 hdev->type = HCI_USB;
991 hdev->driver_data = husb;
992 SET_HCIDEV_DEV(hdev, &intf->dev);
994 hdev->open = hci_usb_open;
995 hdev->close = hci_usb_close;
996 hdev->flush = hci_usb_flush;
997 hdev->send = hci_usb_send_frame;
998 hdev->destruct = hci_usb_destruct;
999 hdev->notify = hci_usb_notify;
1001 hdev->owner = THIS_MODULE;
1003 if (reset || id->driver_info & HCI_RESET)
1004 set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
1006 if (force_scofix || id->driver_info & HCI_WRONG_SCO_MTU) {
1007 if (!disable_scofix)
1008 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1011 if (id->driver_info & HCI_SNIFFER) {
1012 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1013 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
1016 if (id->driver_info & HCI_BCM92035) {
1017 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1018 struct sk_buff *skb;
1020 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1022 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1023 skb_queue_tail(&hdev->driver_init, skb);
1027 if (hci_register_dev(hdev) < 0) {
1028 BT_ERR("Can't register HCI device");
1033 usb_set_intfdata(intf, husb);
1037 if (husb->isoc_iface)
1038 usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
1045 static void hci_usb_disconnect(struct usb_interface *intf)
1047 struct hci_usb *husb = usb_get_intfdata(intf);
1048 struct hci_dev *hdev;
1050 if (!husb || intf == husb->isoc_iface)
1053 usb_set_intfdata(intf, NULL);
1056 BT_DBG("%s", hdev->name);
1058 hci_usb_close(hdev);
1060 if (husb->isoc_iface)
1061 usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
1063 if (hci_unregister_dev(hdev) < 0)
1064 BT_ERR("Can't unregister HCI device %s", hdev->name);
1069 static int hci_usb_suspend(struct usb_interface *intf, pm_message_t message)
1071 struct hci_usb *husb = usb_get_intfdata(intf);
1072 struct list_head killed;
1073 unsigned long flags;
1076 if (!husb || intf == husb->isoc_iface)
1079 hci_suspend_dev(husb->hdev);
1081 INIT_LIST_HEAD(&killed);
1083 for (i = 0; i < 4; i++) {
1084 struct _urb_queue *q = &husb->pending_q[i];
1085 struct _urb *_urb, *_tmp;
1087 while ((_urb = _urb_dequeue(q))) {
1088 /* reset queue since _urb_dequeue sets it to NULL */
1090 usb_kill_urb(&_urb->urb);
1091 list_add(&_urb->list, &killed);
1094 spin_lock_irqsave(&q->lock, flags);
1096 list_for_each_entry_safe(_urb, _tmp, &killed, list) {
1097 list_move_tail(&_urb->list, &q->head);
1100 spin_unlock_irqrestore(&q->lock, flags);
1106 static int hci_usb_resume(struct usb_interface *intf)
1108 struct hci_usb *husb = usb_get_intfdata(intf);
1109 unsigned long flags;
1112 if (!husb || intf == husb->isoc_iface)
1115 for (i = 0; i < 4; i++) {
1116 struct _urb_queue *q = &husb->pending_q[i];
1119 spin_lock_irqsave(&q->lock, flags);
1121 list_for_each_entry(_urb, &q->head, list) {
1122 err = usb_submit_urb(&_urb->urb, GFP_ATOMIC);
1127 spin_unlock_irqrestore(&q->lock, flags);
1133 hci_resume_dev(husb->hdev);
1138 static struct usb_driver hci_usb_driver = {
1140 .probe = hci_usb_probe,
1141 .disconnect = hci_usb_disconnect,
1142 .suspend = hci_usb_suspend,
1143 .resume = hci_usb_resume,
1144 .id_table = bluetooth_ids,
1147 static int __init hci_usb_init(void)
1151 BT_INFO("HCI USB driver ver %s", VERSION);
1153 if ((err = usb_register(&hci_usb_driver)) < 0)
1154 BT_ERR("Failed to register HCI USB driver");
1159 static void __exit hci_usb_exit(void)
1161 usb_deregister(&hci_usb_driver);
1164 module_init(hci_usb_init);
1165 module_exit(hci_usb_exit);
1167 module_param(ignore, bool, 0644);
1168 MODULE_PARM_DESC(ignore, "Ignore devices from the matching table");
1170 module_param(ignore_dga, bool, 0644);
1171 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1173 module_param(ignore_csr, bool, 0644);
1174 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1176 module_param(ignore_sniffer, bool, 0644);
1177 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1179 module_param(disable_scofix, bool, 0644);
1180 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1182 module_param(force_scofix, bool, 0644);
1183 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1185 module_param(reset, bool, 0644);
1186 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1188 #ifdef CONFIG_BT_HCIUSB_SCO
1189 module_param(isoc, int, 0644);
1190 MODULE_PARM_DESC(isoc, "Set isochronous transfers for SCO over HCI support");
1193 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
1194 MODULE_DESCRIPTION("Bluetooth HCI USB driver ver " VERSION);
1195 MODULE_VERSION(VERSION);
1196 MODULE_LICENSE("GPL");