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 { } /* Terminating entry */
102 MODULE_DEVICE_TABLE (usb
, bluetooth_ids
);
104 static struct usb_device_id blacklist_ids
[] = {
105 /* CSR BlueCore devices */
106 { USB_DEVICE(0x0a12, 0x0001), .driver_info
= HCI_CSR
},
108 /* Broadcom BCM2033 without firmware */
109 { USB_DEVICE(0x0a5c, 0x2033), .driver_info
= HCI_IGNORE
},
111 /* Broadcom BCM2035 */
112 { USB_DEVICE(0x0a5c, 0x200a), .driver_info
= HCI_RESET
| HCI_BROKEN_ISOC
},
113 { USB_DEVICE(0x0a5c, 0x2009), .driver_info
= HCI_BCM92035
},
115 /* IBM/Lenovo ThinkPad with Broadcom chip */
116 { USB_DEVICE(0x0a5c, 0x201e), .driver_info
= HCI_WRONG_SCO_MTU
},
118 /* Microsoft Wireless Transceiver for Bluetooth 2.0 */
119 { USB_DEVICE(0x045e, 0x009c), .driver_info
= HCI_RESET
},
121 /* Kensington Bluetooth USB adapter */
122 { USB_DEVICE(0x047d, 0x105d), .driver_info
= HCI_RESET
},
124 /* ISSC Bluetooth Adapter v3.1 */
125 { USB_DEVICE(0x1131, 0x1001), .driver_info
= HCI_RESET
},
127 /* RTX Telecom based adapter with buggy SCO support */
128 { USB_DEVICE(0x0400, 0x0807), .driver_info
= HCI_BROKEN_ISOC
},
130 /* Belkin F8T012 and F8T013 devices */
131 { USB_DEVICE(0x050d, 0x0012), .driver_info
= HCI_WRONG_SCO_MTU
},
132 { USB_DEVICE(0x050d, 0x0013), .driver_info
= HCI_WRONG_SCO_MTU
},
134 /* Digianswer devices */
135 { USB_DEVICE(0x08fd, 0x0001), .driver_info
= HCI_DIGIANSWER
},
136 { USB_DEVICE(0x08fd, 0x0002), .driver_info
= HCI_IGNORE
},
138 /* CSR BlueCore Bluetooth Sniffer */
139 { USB_DEVICE(0x0a12, 0x0002), .driver_info
= HCI_SNIFFER
},
141 /* Frontline ComProbe Bluetooth Sniffer */
142 { USB_DEVICE(0x16d3, 0x0002), .driver_info
= HCI_SNIFFER
},
144 { } /* Terminating entry */
147 static struct _urb
*_urb_alloc(int isoc
, gfp_t gfp
)
149 struct _urb
*_urb
= kmalloc(sizeof(struct _urb
) +
150 sizeof(struct usb_iso_packet_descriptor
) * isoc
, gfp
);
152 memset(_urb
, 0, sizeof(*_urb
));
153 usb_init_urb(&_urb
->urb
);
158 static struct _urb
*_urb_dequeue(struct _urb_queue
*q
)
160 struct _urb
*_urb
= NULL
;
162 spin_lock_irqsave(&q
->lock
, flags
);
164 struct list_head
*head
= &q
->head
;
165 struct list_head
*next
= head
->next
;
167 _urb
= list_entry(next
, struct _urb
, list
);
168 list_del(next
); _urb
->queue
= NULL
;
171 spin_unlock_irqrestore(&q
->lock
, flags
);
175 static void hci_usb_rx_complete(struct urb
*urb
, struct pt_regs
*regs
);
176 static void hci_usb_tx_complete(struct urb
*urb
, struct pt_regs
*regs
);
178 #define __pending_tx(husb, type) (&husb->pending_tx[type-1])
179 #define __pending_q(husb, type) (&husb->pending_q[type-1])
180 #define __completed_q(husb, type) (&husb->completed_q[type-1])
181 #define __transmit_q(husb, type) (&husb->transmit_q[type-1])
182 #define __reassembly(husb, type) (husb->reassembly[type-1])
184 static inline struct _urb
*__get_completed(struct hci_usb
*husb
, int type
)
186 return _urb_dequeue(__completed_q(husb
, type
));
189 #ifdef CONFIG_BT_HCIUSB_SCO
190 static void __fill_isoc_desc(struct urb
*urb
, int len
, int mtu
)
194 BT_DBG("len %d mtu %d", len
, mtu
);
196 for (i
=0; i
< HCI_MAX_ISOC_FRAMES
&& len
>= mtu
; i
++, offset
+= mtu
, len
-= mtu
) {
197 urb
->iso_frame_desc
[i
].offset
= offset
;
198 urb
->iso_frame_desc
[i
].length
= mtu
;
199 BT_DBG("desc %d offset %d len %d", i
, offset
, mtu
);
201 if (len
&& i
< HCI_MAX_ISOC_FRAMES
) {
202 urb
->iso_frame_desc
[i
].offset
= offset
;
203 urb
->iso_frame_desc
[i
].length
= len
;
204 BT_DBG("desc %d offset %d len %d", i
, offset
, len
);
207 urb
->number_of_packets
= i
;
211 static int hci_usb_intr_rx_submit(struct hci_usb
*husb
)
215 int err
, pipe
, interval
, size
;
218 BT_DBG("%s", husb
->hdev
->name
);
220 size
= le16_to_cpu(husb
->intr_in_ep
->desc
.wMaxPacketSize
);
222 buf
= kmalloc(size
, GFP_ATOMIC
);
226 _urb
= _urb_alloc(0, GFP_ATOMIC
);
231 _urb
->type
= HCI_EVENT_PKT
;
232 _urb_queue_tail(__pending_q(husb
, _urb
->type
), _urb
);
235 pipe
= usb_rcvintpipe(husb
->udev
, husb
->intr_in_ep
->desc
.bEndpointAddress
);
236 interval
= husb
->intr_in_ep
->desc
.bInterval
;
237 usb_fill_int_urb(urb
, husb
->udev
, pipe
, buf
, size
, hci_usb_rx_complete
, husb
, interval
);
239 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
241 BT_ERR("%s intr rx submit failed urb %p err %d",
242 husb
->hdev
->name
, urb
, err
);
250 static int hci_usb_bulk_rx_submit(struct hci_usb
*husb
)
254 int err
, pipe
, size
= HCI_MAX_FRAME_SIZE
;
257 buf
= kmalloc(size
, GFP_ATOMIC
);
261 _urb
= _urb_alloc(0, GFP_ATOMIC
);
266 _urb
->type
= HCI_ACLDATA_PKT
;
267 _urb_queue_tail(__pending_q(husb
, _urb
->type
), _urb
);
270 pipe
= usb_rcvbulkpipe(husb
->udev
, husb
->bulk_in_ep
->desc
.bEndpointAddress
);
271 usb_fill_bulk_urb(urb
, husb
->udev
, pipe
, buf
, size
, hci_usb_rx_complete
, husb
);
272 urb
->transfer_flags
= 0;
274 BT_DBG("%s urb %p", husb
->hdev
->name
, urb
);
276 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
278 BT_ERR("%s bulk rx submit failed urb %p err %d",
279 husb
->hdev
->name
, urb
, err
);
287 #ifdef CONFIG_BT_HCIUSB_SCO
288 static int hci_usb_isoc_rx_submit(struct hci_usb
*husb
)
295 mtu
= le16_to_cpu(husb
->isoc_in_ep
->desc
.wMaxPacketSize
);
296 size
= mtu
* HCI_MAX_ISOC_FRAMES
;
298 buf
= kmalloc(size
, GFP_ATOMIC
);
302 _urb
= _urb_alloc(HCI_MAX_ISOC_FRAMES
, GFP_ATOMIC
);
307 _urb
->type
= HCI_SCODATA_PKT
;
308 _urb_queue_tail(__pending_q(husb
, _urb
->type
), _urb
);
313 urb
->dev
= husb
->udev
;
314 urb
->pipe
= usb_rcvisocpipe(husb
->udev
, husb
->isoc_in_ep
->desc
.bEndpointAddress
);
315 urb
->complete
= hci_usb_rx_complete
;
317 urb
->interval
= husb
->isoc_in_ep
->desc
.bInterval
;
319 urb
->transfer_buffer_length
= size
;
320 urb
->transfer_buffer
= buf
;
321 urb
->transfer_flags
= URB_ISO_ASAP
;
323 __fill_isoc_desc(urb
, size
, mtu
);
325 BT_DBG("%s urb %p", husb
->hdev
->name
, urb
);
327 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
329 BT_ERR("%s isoc rx submit failed urb %p err %d",
330 husb
->hdev
->name
, urb
, err
);
339 /* Initialize device */
340 static int hci_usb_open(struct hci_dev
*hdev
)
342 struct hci_usb
*husb
= (struct hci_usb
*) hdev
->driver_data
;
346 BT_DBG("%s", hdev
->name
);
348 if (test_and_set_bit(HCI_RUNNING
, &hdev
->flags
))
351 write_lock_irqsave(&husb
->completion_lock
, flags
);
353 err
= hci_usb_intr_rx_submit(husb
);
355 for (i
= 0; i
< HCI_MAX_BULK_RX
; i
++)
356 hci_usb_bulk_rx_submit(husb
);
358 #ifdef CONFIG_BT_HCIUSB_SCO
359 if (husb
->isoc_iface
)
360 for (i
= 0; i
< HCI_MAX_ISOC_RX
; i
++)
361 hci_usb_isoc_rx_submit(husb
);
364 clear_bit(HCI_RUNNING
, &hdev
->flags
);
367 write_unlock_irqrestore(&husb
->completion_lock
, flags
);
372 static int hci_usb_flush(struct hci_dev
*hdev
)
374 struct hci_usb
*husb
= (struct hci_usb
*) hdev
->driver_data
;
377 BT_DBG("%s", hdev
->name
);
379 for (i
= 0; i
< 4; i
++)
380 skb_queue_purge(&husb
->transmit_q
[i
]);
384 static void hci_usb_unlink_urbs(struct hci_usb
*husb
)
388 BT_DBG("%s", husb
->hdev
->name
);
390 for (i
= 0; i
< 4; i
++) {
394 /* Kill pending requests */
395 while ((_urb
= _urb_dequeue(&husb
->pending_q
[i
]))) {
397 BT_DBG("%s unlinking _urb %p type %d urb %p",
398 husb
->hdev
->name
, _urb
, _urb
->type
, urb
);
400 _urb_queue_tail(__completed_q(husb
, _urb
->type
), _urb
);
403 /* Release completed requests */
404 while ((_urb
= _urb_dequeue(&husb
->completed_q
[i
]))) {
406 BT_DBG("%s freeing _urb %p type %d urb %p",
407 husb
->hdev
->name
, _urb
, _urb
->type
, urb
);
408 kfree(urb
->setup_packet
);
409 kfree(urb
->transfer_buffer
);
413 /* Release reassembly buffers */
414 if (husb
->reassembly
[i
]) {
415 kfree_skb(husb
->reassembly
[i
]);
416 husb
->reassembly
[i
] = NULL
;
422 static int hci_usb_close(struct hci_dev
*hdev
)
424 struct hci_usb
*husb
= (struct hci_usb
*) hdev
->driver_data
;
427 if (!test_and_clear_bit(HCI_RUNNING
, &hdev
->flags
))
430 BT_DBG("%s", hdev
->name
);
432 /* Synchronize with completion handlers */
433 write_lock_irqsave(&husb
->completion_lock
, flags
);
434 write_unlock_irqrestore(&husb
->completion_lock
, flags
);
436 hci_usb_unlink_urbs(husb
);
441 static int __tx_submit(struct hci_usb
*husb
, struct _urb
*_urb
)
443 struct urb
*urb
= &_urb
->urb
;
446 BT_DBG("%s urb %p type %d", husb
->hdev
->name
, urb
, _urb
->type
);
448 _urb_queue_tail(__pending_q(husb
, _urb
->type
), _urb
);
449 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
451 BT_ERR("%s tx submit failed urb %p type %d err %d",
452 husb
->hdev
->name
, urb
, _urb
->type
, err
);
454 _urb_queue_tail(__completed_q(husb
, _urb
->type
), _urb
);
456 atomic_inc(__pending_tx(husb
, _urb
->type
));
461 static inline int hci_usb_send_ctrl(struct hci_usb
*husb
, struct sk_buff
*skb
)
463 struct _urb
*_urb
= __get_completed(husb
, bt_cb(skb
)->pkt_type
);
464 struct usb_ctrlrequest
*dr
;
468 _urb
= _urb_alloc(0, GFP_ATOMIC
);
471 _urb
->type
= bt_cb(skb
)->pkt_type
;
473 dr
= kmalloc(sizeof(*dr
), GFP_ATOMIC
);
479 dr
= (void *) _urb
->urb
.setup_packet
;
481 dr
->bRequestType
= husb
->ctrl_req
;
485 dr
->wLength
= __cpu_to_le16(skb
->len
);
488 usb_fill_control_urb(urb
, husb
->udev
, usb_sndctrlpipe(husb
->udev
, 0),
489 (void *) dr
, skb
->data
, skb
->len
, hci_usb_tx_complete
, husb
);
491 BT_DBG("%s skb %p len %d", husb
->hdev
->name
, skb
, skb
->len
);
494 return __tx_submit(husb
, _urb
);
497 static inline int hci_usb_send_bulk(struct hci_usb
*husb
, struct sk_buff
*skb
)
499 struct _urb
*_urb
= __get_completed(husb
, bt_cb(skb
)->pkt_type
);
504 _urb
= _urb_alloc(0, GFP_ATOMIC
);
507 _urb
->type
= bt_cb(skb
)->pkt_type
;
511 pipe
= usb_sndbulkpipe(husb
->udev
, husb
->bulk_out_ep
->desc
.bEndpointAddress
);
512 usb_fill_bulk_urb(urb
, husb
->udev
, pipe
, skb
->data
, skb
->len
,
513 hci_usb_tx_complete
, husb
);
514 urb
->transfer_flags
= URB_ZERO_PACKET
;
516 BT_DBG("%s skb %p len %d", husb
->hdev
->name
, skb
, skb
->len
);
519 return __tx_submit(husb
, _urb
);
522 #ifdef CONFIG_BT_HCIUSB_SCO
523 static inline int hci_usb_send_isoc(struct hci_usb
*husb
, struct sk_buff
*skb
)
525 struct _urb
*_urb
= __get_completed(husb
, bt_cb(skb
)->pkt_type
);
529 _urb
= _urb_alloc(HCI_MAX_ISOC_FRAMES
, GFP_ATOMIC
);
532 _urb
->type
= bt_cb(skb
)->pkt_type
;
535 BT_DBG("%s skb %p len %d", husb
->hdev
->name
, skb
, skb
->len
);
540 urb
->dev
= husb
->udev
;
541 urb
->pipe
= usb_sndisocpipe(husb
->udev
, husb
->isoc_out_ep
->desc
.bEndpointAddress
);
542 urb
->complete
= hci_usb_tx_complete
;
543 urb
->transfer_flags
= URB_ISO_ASAP
;
545 urb
->interval
= husb
->isoc_out_ep
->desc
.bInterval
;
547 urb
->transfer_buffer
= skb
->data
;
548 urb
->transfer_buffer_length
= skb
->len
;
550 __fill_isoc_desc(urb
, skb
->len
, le16_to_cpu(husb
->isoc_out_ep
->desc
.wMaxPacketSize
));
553 return __tx_submit(husb
, _urb
);
557 static void hci_usb_tx_process(struct hci_usb
*husb
)
559 struct sk_buff_head
*q
;
562 BT_DBG("%s", husb
->hdev
->name
);
565 clear_bit(HCI_USB_TX_WAKEUP
, &husb
->state
);
567 /* Process command queue */
568 q
= __transmit_q(husb
, HCI_COMMAND_PKT
);
569 if (!atomic_read(__pending_tx(husb
, HCI_COMMAND_PKT
)) &&
570 (skb
= skb_dequeue(q
))) {
571 if (hci_usb_send_ctrl(husb
, skb
) < 0)
572 skb_queue_head(q
, skb
);
575 #ifdef CONFIG_BT_HCIUSB_SCO
576 /* Process SCO queue */
577 q
= __transmit_q(husb
, HCI_SCODATA_PKT
);
578 if (atomic_read(__pending_tx(husb
, HCI_SCODATA_PKT
)) < HCI_MAX_ISOC_TX
&&
579 (skb
= skb_dequeue(q
))) {
580 if (hci_usb_send_isoc(husb
, skb
) < 0)
581 skb_queue_head(q
, skb
);
585 /* Process ACL queue */
586 q
= __transmit_q(husb
, HCI_ACLDATA_PKT
);
587 while (atomic_read(__pending_tx(husb
, HCI_ACLDATA_PKT
)) < HCI_MAX_BULK_TX
&&
588 (skb
= skb_dequeue(q
))) {
589 if (hci_usb_send_bulk(husb
, skb
) < 0) {
590 skb_queue_head(q
, skb
);
594 } while(test_bit(HCI_USB_TX_WAKEUP
, &husb
->state
));
597 static inline void hci_usb_tx_wakeup(struct hci_usb
*husb
)
599 /* Serialize TX queue processing to avoid data reordering */
600 if (!test_and_set_bit(HCI_USB_TX_PROCESS
, &husb
->state
)) {
601 hci_usb_tx_process(husb
);
602 clear_bit(HCI_USB_TX_PROCESS
, &husb
->state
);
604 set_bit(HCI_USB_TX_WAKEUP
, &husb
->state
);
607 /* Send frames from HCI layer */
608 static int hci_usb_send_frame(struct sk_buff
*skb
)
610 struct hci_dev
*hdev
= (struct hci_dev
*) skb
->dev
;
611 struct hci_usb
*husb
;
614 BT_ERR("frame for uknown device (hdev=NULL)");
618 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
621 BT_DBG("%s type %d len %d", hdev
->name
, bt_cb(skb
)->pkt_type
, skb
->len
);
623 husb
= (struct hci_usb
*) hdev
->driver_data
;
625 switch (bt_cb(skb
)->pkt_type
) {
626 case HCI_COMMAND_PKT
:
630 case HCI_ACLDATA_PKT
:
634 #ifdef CONFIG_BT_HCIUSB_SCO
635 case HCI_SCODATA_PKT
:
645 read_lock(&husb
->completion_lock
);
647 skb_queue_tail(__transmit_q(husb
, bt_cb(skb
)->pkt_type
), skb
);
648 hci_usb_tx_wakeup(husb
);
650 read_unlock(&husb
->completion_lock
);
654 static inline int __recv_frame(struct hci_usb
*husb
, int type
, void *data
, int count
)
656 BT_DBG("%s type %d data %p count %d", husb
->hdev
->name
, type
, data
, count
);
658 husb
->hdev
->stat
.byte_rx
+= count
;
661 struct sk_buff
*skb
= __reassembly(husb
, type
);
662 struct { int expect
; } *scb
;
666 /* Start of the frame */
670 if (count
>= HCI_EVENT_HDR_SIZE
) {
671 struct hci_event_hdr
*h
= data
;
672 len
= HCI_EVENT_HDR_SIZE
+ h
->plen
;
677 case HCI_ACLDATA_PKT
:
678 if (count
>= HCI_ACL_HDR_SIZE
) {
679 struct hci_acl_hdr
*h
= data
;
680 len
= HCI_ACL_HDR_SIZE
+ __le16_to_cpu(h
->dlen
);
684 #ifdef CONFIG_BT_HCIUSB_SCO
685 case HCI_SCODATA_PKT
:
686 if (count
>= HCI_SCO_HDR_SIZE
) {
687 struct hci_sco_hdr
*h
= data
;
688 len
= HCI_SCO_HDR_SIZE
+ h
->dlen
;
694 BT_DBG("new packet len %d", len
);
696 skb
= bt_skb_alloc(len
, GFP_ATOMIC
);
698 BT_ERR("%s no memory for the packet", husb
->hdev
->name
);
701 skb
->dev
= (void *) husb
->hdev
;
702 bt_cb(skb
)->pkt_type
= type
;
704 __reassembly(husb
, type
) = skb
;
706 scb
= (void *) skb
->cb
;
710 scb
= (void *) skb
->cb
;
714 len
= min(len
, count
);
716 memcpy(skb_put(skb
, len
), data
, len
);
721 __reassembly(husb
, type
) = NULL
;
722 bt_cb(skb
)->pkt_type
= type
;
726 count
-= len
; data
+= len
;
731 static void hci_usb_rx_complete(struct urb
*urb
, struct pt_regs
*regs
)
733 struct _urb
*_urb
= container_of(urb
, struct _urb
, urb
);
734 struct hci_usb
*husb
= (void *) urb
->context
;
735 struct hci_dev
*hdev
= husb
->hdev
;
736 int err
, count
= urb
->actual_length
;
738 BT_DBG("%s urb %p type %d status %d count %d flags %x", hdev
->name
, urb
,
739 _urb
->type
, urb
->status
, count
, urb
->transfer_flags
);
741 read_lock(&husb
->completion_lock
);
743 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
746 if (urb
->status
|| !count
)
749 if (_urb
->type
== HCI_SCODATA_PKT
) {
750 #ifdef CONFIG_BT_HCIUSB_SCO
752 for (i
=0; i
< urb
->number_of_packets
; i
++) {
753 BT_DBG("desc %d status %d offset %d len %d", i
,
754 urb
->iso_frame_desc
[i
].status
,
755 urb
->iso_frame_desc
[i
].offset
,
756 urb
->iso_frame_desc
[i
].actual_length
);
758 if (!urb
->iso_frame_desc
[i
].status
)
759 __recv_frame(husb
, _urb
->type
,
760 urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
,
761 urb
->iso_frame_desc
[i
].actual_length
);
767 err
= __recv_frame(husb
, _urb
->type
, urb
->transfer_buffer
, count
);
769 BT_ERR("%s corrupted packet: type %d count %d",
770 husb
->hdev
->name
, _urb
->type
, count
);
776 urb
->dev
= husb
->udev
;
777 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
778 BT_DBG("%s urb %p type %d resubmit status %d", hdev
->name
, urb
,
782 read_unlock(&husb
->completion_lock
);
785 static void hci_usb_tx_complete(struct urb
*urb
, struct pt_regs
*regs
)
787 struct _urb
*_urb
= container_of(urb
, struct _urb
, urb
);
788 struct hci_usb
*husb
= (void *) urb
->context
;
789 struct hci_dev
*hdev
= husb
->hdev
;
791 BT_DBG("%s urb %p status %d flags %x", hdev
->name
, urb
,
792 urb
->status
, urb
->transfer_flags
);
794 atomic_dec(__pending_tx(husb
, _urb
->type
));
796 urb
->transfer_buffer
= NULL
;
797 kfree_skb((struct sk_buff
*) _urb
->priv
);
799 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
803 hdev
->stat
.byte_tx
+= urb
->transfer_buffer_length
;
807 read_lock(&husb
->completion_lock
);
810 _urb_queue_tail(__completed_q(husb
, _urb
->type
), _urb
);
812 hci_usb_tx_wakeup(husb
);
814 read_unlock(&husb
->completion_lock
);
817 static void hci_usb_destruct(struct hci_dev
*hdev
)
819 struct hci_usb
*husb
= (struct hci_usb
*) hdev
->driver_data
;
821 BT_DBG("%s", hdev
->name
);
826 static void hci_usb_notify(struct hci_dev
*hdev
, unsigned int evt
)
828 BT_DBG("%s evt %d", hdev
->name
, evt
);
831 static int hci_usb_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
833 struct usb_device
*udev
= interface_to_usbdev(intf
);
834 struct usb_host_endpoint
*bulk_out_ep
= NULL
;
835 struct usb_host_endpoint
*bulk_in_ep
= NULL
;
836 struct usb_host_endpoint
*intr_in_ep
= NULL
;
837 struct usb_host_endpoint
*ep
;
838 struct usb_host_interface
*uif
;
839 struct usb_interface
*isoc_iface
;
840 struct hci_usb
*husb
;
841 struct hci_dev
*hdev
;
842 int i
, e
, size
, isoc_ifnum
, isoc_alts
;
844 BT_DBG("udev %p intf %p", udev
, intf
);
846 if (!id
->driver_info
) {
847 const struct usb_device_id
*match
;
848 match
= usb_match_id(intf
, blacklist_ids
);
853 if (ignore
|| id
->driver_info
& HCI_IGNORE
)
856 if (ignore_dga
&& id
->driver_info
& HCI_DIGIANSWER
)
859 if (ignore_csr
&& id
->driver_info
& HCI_CSR
)
862 if (ignore_sniffer
&& id
->driver_info
& HCI_SNIFFER
)
865 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
> 0)
868 /* Find endpoints that we need */
869 uif
= intf
->cur_altsetting
;
870 for (e
= 0; e
< uif
->desc
.bNumEndpoints
; e
++) {
871 ep
= &uif
->endpoint
[e
];
873 switch (ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
874 case USB_ENDPOINT_XFER_INT
:
875 if (ep
->desc
.bEndpointAddress
& USB_DIR_IN
)
879 case USB_ENDPOINT_XFER_BULK
:
880 if (ep
->desc
.bEndpointAddress
& USB_DIR_IN
)
888 if (!bulk_in_ep
|| !bulk_out_ep
|| !intr_in_ep
) {
889 BT_DBG("Bulk endpoints not found");
893 if (!(husb
= kzalloc(sizeof(struct hci_usb
), GFP_KERNEL
))) {
894 BT_ERR("Can't allocate: control structure");
899 husb
->bulk_out_ep
= bulk_out_ep
;
900 husb
->bulk_in_ep
= bulk_in_ep
;
901 husb
->intr_in_ep
= intr_in_ep
;
903 if (id
->driver_info
& HCI_DIGIANSWER
)
904 husb
->ctrl_req
= USB_TYPE_VENDOR
;
906 husb
->ctrl_req
= USB_TYPE_CLASS
;
908 /* Find isochronous endpoints that we can use */
914 #ifdef CONFIG_BT_HCIUSB_SCO
915 if (isoc
&& !(id
->driver_info
& (HCI_BROKEN_ISOC
| HCI_SNIFFER
)))
916 isoc_iface
= usb_ifnum_to_if(udev
, isoc_ifnum
);
920 struct usb_host_endpoint
*isoc_out_ep
= NULL
;
921 struct usb_host_endpoint
*isoc_in_ep
= NULL
;
923 for (a
= 0; a
< isoc_iface
->num_altsetting
; a
++) {
924 uif
= &isoc_iface
->altsetting
[a
];
925 for (e
= 0; e
< uif
->desc
.bNumEndpoints
; e
++) {
926 ep
= &uif
->endpoint
[e
];
928 switch (ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
929 case USB_ENDPOINT_XFER_ISOC
:
930 if (le16_to_cpu(ep
->desc
.wMaxPacketSize
) < size
||
931 uif
->desc
.bAlternateSetting
!= isoc
)
933 size
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
935 isoc_alts
= uif
->desc
.bAlternateSetting
;
937 if (ep
->desc
.bEndpointAddress
& USB_DIR_IN
)
946 if (!isoc_in_ep
|| !isoc_out_ep
)
947 BT_DBG("Isoc endpoints not found");
949 BT_DBG("isoc ifnum %d alts %d", isoc_ifnum
, isoc_alts
);
950 if (usb_driver_claim_interface(&hci_usb_driver
, isoc_iface
, husb
) != 0)
951 BT_ERR("Can't claim isoc interface");
952 else if (usb_set_interface(udev
, isoc_ifnum
, isoc_alts
)) {
953 BT_ERR("Can't set isoc interface settings");
954 husb
->isoc_iface
= isoc_iface
;
955 usb_driver_release_interface(&hci_usb_driver
, isoc_iface
);
956 husb
->isoc_iface
= NULL
;
958 husb
->isoc_iface
= isoc_iface
;
959 husb
->isoc_in_ep
= isoc_in_ep
;
960 husb
->isoc_out_ep
= isoc_out_ep
;
966 rwlock_init(&husb
->completion_lock
);
968 for (i
= 0; i
< 4; i
++) {
969 skb_queue_head_init(&husb
->transmit_q
[i
]);
970 _urb_queue_init(&husb
->pending_q
[i
]);
971 _urb_queue_init(&husb
->completed_q
[i
]);
974 /* Initialize and register HCI device */
975 hdev
= hci_alloc_dev();
977 BT_ERR("Can't allocate HCI device");
983 hdev
->type
= HCI_USB
;
984 hdev
->driver_data
= husb
;
985 SET_HCIDEV_DEV(hdev
, &intf
->dev
);
987 hdev
->open
= hci_usb_open
;
988 hdev
->close
= hci_usb_close
;
989 hdev
->flush
= hci_usb_flush
;
990 hdev
->send
= hci_usb_send_frame
;
991 hdev
->destruct
= hci_usb_destruct
;
992 hdev
->notify
= hci_usb_notify
;
994 hdev
->owner
= THIS_MODULE
;
996 if (reset
|| id
->driver_info
& HCI_RESET
)
997 set_bit(HCI_QUIRK_RESET_ON_INIT
, &hdev
->quirks
);
999 if (force_scofix
|| id
->driver_info
& HCI_WRONG_SCO_MTU
) {
1000 if (!disable_scofix
)
1001 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE
, &hdev
->quirks
);
1004 if (id
->driver_info
& HCI_SNIFFER
) {
1005 if (le16_to_cpu(udev
->descriptor
.bcdDevice
) > 0x997)
1006 set_bit(HCI_QUIRK_RAW_DEVICE
, &hdev
->quirks
);
1009 if (id
->driver_info
& HCI_BCM92035
) {
1010 unsigned char cmd
[] = { 0x3b, 0xfc, 0x01, 0x00 };
1011 struct sk_buff
*skb
;
1013 skb
= bt_skb_alloc(sizeof(cmd
), GFP_KERNEL
);
1015 memcpy(skb_put(skb
, sizeof(cmd
)), cmd
, sizeof(cmd
));
1016 skb_queue_tail(&hdev
->driver_init
, skb
);
1020 if (hci_register_dev(hdev
) < 0) {
1021 BT_ERR("Can't register HCI device");
1026 usb_set_intfdata(intf
, husb
);
1030 if (husb
->isoc_iface
)
1031 usb_driver_release_interface(&hci_usb_driver
, husb
->isoc_iface
);
1038 static void hci_usb_disconnect(struct usb_interface
*intf
)
1040 struct hci_usb
*husb
= usb_get_intfdata(intf
);
1041 struct hci_dev
*hdev
;
1043 if (!husb
|| intf
== husb
->isoc_iface
)
1046 usb_set_intfdata(intf
, NULL
);
1049 BT_DBG("%s", hdev
->name
);
1051 hci_usb_close(hdev
);
1053 if (husb
->isoc_iface
)
1054 usb_driver_release_interface(&hci_usb_driver
, husb
->isoc_iface
);
1056 if (hci_unregister_dev(hdev
) < 0)
1057 BT_ERR("Can't unregister HCI device %s", hdev
->name
);
1062 static int hci_usb_suspend(struct usb_interface
*intf
, pm_message_t message
)
1064 struct hci_usb
*husb
= usb_get_intfdata(intf
);
1065 struct list_head killed
;
1066 unsigned long flags
;
1069 if (!husb
|| intf
== husb
->isoc_iface
)
1072 hci_suspend_dev(husb
->hdev
);
1074 INIT_LIST_HEAD(&killed
);
1076 for (i
= 0; i
< 4; i
++) {
1077 struct _urb_queue
*q
= &husb
->pending_q
[i
];
1078 struct _urb
*_urb
, *_tmp
;
1080 while ((_urb
= _urb_dequeue(q
))) {
1081 /* reset queue since _urb_dequeue sets it to NULL */
1083 usb_kill_urb(&_urb
->urb
);
1084 list_add(&_urb
->list
, &killed
);
1087 spin_lock_irqsave(&q
->lock
, flags
);
1089 list_for_each_entry_safe(_urb
, _tmp
, &killed
, list
) {
1090 list_move_tail(&_urb
->list
, &q
->head
);
1093 spin_unlock_irqrestore(&q
->lock
, flags
);
1099 static int hci_usb_resume(struct usb_interface
*intf
)
1101 struct hci_usb
*husb
= usb_get_intfdata(intf
);
1102 unsigned long flags
;
1105 if (!husb
|| intf
== husb
->isoc_iface
)
1108 for (i
= 0; i
< 4; i
++) {
1109 struct _urb_queue
*q
= &husb
->pending_q
[i
];
1112 spin_lock_irqsave(&q
->lock
, flags
);
1114 list_for_each_entry(_urb
, &q
->head
, list
) {
1115 err
= usb_submit_urb(&_urb
->urb
, GFP_ATOMIC
);
1120 spin_unlock_irqrestore(&q
->lock
, flags
);
1126 hci_resume_dev(husb
->hdev
);
1131 static struct usb_driver hci_usb_driver
= {
1133 .probe
= hci_usb_probe
,
1134 .disconnect
= hci_usb_disconnect
,
1135 .suspend
= hci_usb_suspend
,
1136 .resume
= hci_usb_resume
,
1137 .id_table
= bluetooth_ids
,
1140 static int __init
hci_usb_init(void)
1144 BT_INFO("HCI USB driver ver %s", VERSION
);
1146 if ((err
= usb_register(&hci_usb_driver
)) < 0)
1147 BT_ERR("Failed to register HCI USB driver");
1152 static void __exit
hci_usb_exit(void)
1154 usb_deregister(&hci_usb_driver
);
1157 module_init(hci_usb_init
);
1158 module_exit(hci_usb_exit
);
1160 module_param(ignore
, bool, 0644);
1161 MODULE_PARM_DESC(ignore
, "Ignore devices from the matching table");
1163 module_param(ignore_dga
, bool, 0644);
1164 MODULE_PARM_DESC(ignore_dga
, "Ignore devices with id 08fd:0001");
1166 module_param(ignore_csr
, bool, 0644);
1167 MODULE_PARM_DESC(ignore_csr
, "Ignore devices with id 0a12:0001");
1169 module_param(ignore_sniffer
, bool, 0644);
1170 MODULE_PARM_DESC(ignore_sniffer
, "Ignore devices with id 0a12:0002");
1172 module_param(disable_scofix
, bool, 0644);
1173 MODULE_PARM_DESC(disable_scofix
, "Disable fixup of wrong SCO buffer size");
1175 module_param(force_scofix
, bool, 0644);
1176 MODULE_PARM_DESC(force_scofix
, "Force fixup of wrong SCO buffers size");
1178 module_param(reset
, bool, 0644);
1179 MODULE_PARM_DESC(reset
, "Send HCI reset command on initialization");
1181 #ifdef CONFIG_BT_HCIUSB_SCO
1182 module_param(isoc
, int, 0644);
1183 MODULE_PARM_DESC(isoc
, "Set isochronous transfers for SCO over HCI support");
1186 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
1187 MODULE_DESCRIPTION("Bluetooth HCI USB driver ver " VERSION
);
1188 MODULE_VERSION(VERSION
);
1189 MODULE_LICENSE("GPL");