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
62 #ifndef CONFIG_BT_HCIUSB_ZERO_PACKET
63 #undef URB_ZERO_PACKET
64 #define URB_ZERO_PACKET 0
67 static int ignore
= 0;
70 #ifdef CONFIG_BT_HCIUSB_SCO
76 static struct usb_driver hci_usb_driver
;
78 static struct usb_device_id bluetooth_ids
[] = {
79 /* Generic Bluetooth USB device */
80 { USB_DEVICE_INFO(HCI_DEV_CLASS
, HCI_DEV_SUBCLASS
, HCI_DEV_PROTOCOL
) },
82 /* AVM BlueFRITZ! USB v2.0 */
83 { USB_DEVICE(0x057c, 0x3800) },
85 /* Bluetooth Ultraport Module from IBM */
86 { USB_DEVICE(0x04bf, 0x030a) },
88 /* ALPS Modules with non-standard id */
89 { USB_DEVICE(0x044e, 0x3001) },
90 { USB_DEVICE(0x044e, 0x3002) },
92 /* Ericsson with non-standard id */
93 { USB_DEVICE(0x0bdb, 0x1002) },
95 { } /* Terminating entry */
98 MODULE_DEVICE_TABLE (usb
, bluetooth_ids
);
100 static struct usb_device_id blacklist_ids
[] = {
101 /* Broadcom BCM2033 without firmware */
102 { USB_DEVICE(0x0a5c, 0x2033), .driver_info
= HCI_IGNORE
},
104 /* Broadcom BCM2035 */
105 { USB_DEVICE(0x0a5c, 0x200a), .driver_info
= HCI_RESET
| HCI_BROKEN_ISOC
},
106 { USB_DEVICE(0x0a5c, 0x2009), .driver_info
= HCI_BCM92035
},
108 /* Microsoft Wireless Transceiver for Bluetooth 2.0 */
109 { USB_DEVICE(0x045e, 0x009c), .driver_info
= HCI_RESET
},
111 /* Kensington Bluetooth USB adapter */
112 { USB_DEVICE(0x047d, 0x105d), .driver_info
= HCI_RESET
},
114 /* ISSC Bluetooth Adapter v3.1 */
115 { USB_DEVICE(0x1131, 0x1001), .driver_info
= HCI_RESET
},
117 /* RTX Telecom based adapter with buggy SCO support */
118 { USB_DEVICE(0x0400, 0x0807), .driver_info
= HCI_BROKEN_ISOC
},
120 /* Digianswer devices */
121 { USB_DEVICE(0x08fd, 0x0001), .driver_info
= HCI_DIGIANSWER
},
122 { USB_DEVICE(0x08fd, 0x0002), .driver_info
= HCI_IGNORE
},
124 /* CSR BlueCore Bluetooth Sniffer */
125 { USB_DEVICE(0x0a12, 0x0002), .driver_info
= HCI_SNIFFER
},
127 { } /* Terminating entry */
130 static struct _urb
*_urb_alloc(int isoc
, unsigned int __nocast gfp
)
132 struct _urb
*_urb
= kmalloc(sizeof(struct _urb
) +
133 sizeof(struct usb_iso_packet_descriptor
) * isoc
, gfp
);
135 memset(_urb
, 0, sizeof(*_urb
));
136 usb_init_urb(&_urb
->urb
);
141 static struct _urb
*_urb_dequeue(struct _urb_queue
*q
)
143 struct _urb
*_urb
= NULL
;
145 spin_lock_irqsave(&q
->lock
, flags
);
147 struct list_head
*head
= &q
->head
;
148 struct list_head
*next
= head
->next
;
150 _urb
= list_entry(next
, struct _urb
, list
);
151 list_del(next
); _urb
->queue
= NULL
;
154 spin_unlock_irqrestore(&q
->lock
, flags
);
158 static void hci_usb_rx_complete(struct urb
*urb
, struct pt_regs
*regs
);
159 static void hci_usb_tx_complete(struct urb
*urb
, struct pt_regs
*regs
);
161 #define __pending_tx(husb, type) (&husb->pending_tx[type-1])
162 #define __pending_q(husb, type) (&husb->pending_q[type-1])
163 #define __completed_q(husb, type) (&husb->completed_q[type-1])
164 #define __transmit_q(husb, type) (&husb->transmit_q[type-1])
165 #define __reassembly(husb, type) (husb->reassembly[type-1])
167 static inline struct _urb
*__get_completed(struct hci_usb
*husb
, int type
)
169 return _urb_dequeue(__completed_q(husb
, type
));
172 #ifdef CONFIG_BT_HCIUSB_SCO
173 static void __fill_isoc_desc(struct urb
*urb
, int len
, int mtu
)
177 BT_DBG("len %d mtu %d", len
, mtu
);
179 for (i
=0; i
< HCI_MAX_ISOC_FRAMES
&& len
>= mtu
; i
++, offset
+= mtu
, len
-= mtu
) {
180 urb
->iso_frame_desc
[i
].offset
= offset
;
181 urb
->iso_frame_desc
[i
].length
= mtu
;
182 BT_DBG("desc %d offset %d len %d", i
, offset
, mtu
);
184 if (len
&& i
< HCI_MAX_ISOC_FRAMES
) {
185 urb
->iso_frame_desc
[i
].offset
= offset
;
186 urb
->iso_frame_desc
[i
].length
= len
;
187 BT_DBG("desc %d offset %d len %d", i
, offset
, len
);
190 urb
->number_of_packets
= i
;
194 static int hci_usb_intr_rx_submit(struct hci_usb
*husb
)
198 int err
, pipe
, interval
, size
;
201 BT_DBG("%s", husb
->hdev
->name
);
203 size
= le16_to_cpu(husb
->intr_in_ep
->desc
.wMaxPacketSize
);
205 buf
= kmalloc(size
, GFP_ATOMIC
);
209 _urb
= _urb_alloc(0, GFP_ATOMIC
);
214 _urb
->type
= HCI_EVENT_PKT
;
215 _urb_queue_tail(__pending_q(husb
, _urb
->type
), _urb
);
218 pipe
= usb_rcvintpipe(husb
->udev
, husb
->intr_in_ep
->desc
.bEndpointAddress
);
219 interval
= husb
->intr_in_ep
->desc
.bInterval
;
220 usb_fill_int_urb(urb
, husb
->udev
, pipe
, buf
, size
, hci_usb_rx_complete
, husb
, interval
);
222 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
224 BT_ERR("%s intr rx submit failed urb %p err %d",
225 husb
->hdev
->name
, urb
, err
);
233 static int hci_usb_bulk_rx_submit(struct hci_usb
*husb
)
237 int err
, pipe
, size
= HCI_MAX_FRAME_SIZE
;
240 buf
= kmalloc(size
, GFP_ATOMIC
);
244 _urb
= _urb_alloc(0, GFP_ATOMIC
);
249 _urb
->type
= HCI_ACLDATA_PKT
;
250 _urb_queue_tail(__pending_q(husb
, _urb
->type
), _urb
);
253 pipe
= usb_rcvbulkpipe(husb
->udev
, husb
->bulk_in_ep
->desc
.bEndpointAddress
);
254 usb_fill_bulk_urb(urb
, husb
->udev
, pipe
, buf
, size
, hci_usb_rx_complete
, husb
);
255 urb
->transfer_flags
= 0;
257 BT_DBG("%s urb %p", husb
->hdev
->name
, urb
);
259 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
261 BT_ERR("%s bulk rx submit failed urb %p err %d",
262 husb
->hdev
->name
, urb
, err
);
270 #ifdef CONFIG_BT_HCIUSB_SCO
271 static int hci_usb_isoc_rx_submit(struct hci_usb
*husb
)
278 mtu
= le16_to_cpu(husb
->isoc_in_ep
->desc
.wMaxPacketSize
);
279 size
= mtu
* HCI_MAX_ISOC_FRAMES
;
281 buf
= kmalloc(size
, GFP_ATOMIC
);
285 _urb
= _urb_alloc(HCI_MAX_ISOC_FRAMES
, GFP_ATOMIC
);
290 _urb
->type
= HCI_SCODATA_PKT
;
291 _urb_queue_tail(__pending_q(husb
, _urb
->type
), _urb
);
296 urb
->dev
= husb
->udev
;
297 urb
->pipe
= usb_rcvisocpipe(husb
->udev
, husb
->isoc_in_ep
->desc
.bEndpointAddress
);
298 urb
->complete
= hci_usb_rx_complete
;
300 urb
->interval
= husb
->isoc_in_ep
->desc
.bInterval
;
302 urb
->transfer_buffer_length
= size
;
303 urb
->transfer_buffer
= buf
;
304 urb
->transfer_flags
= URB_ISO_ASAP
;
306 __fill_isoc_desc(urb
, size
, mtu
);
308 BT_DBG("%s urb %p", husb
->hdev
->name
, urb
);
310 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
312 BT_ERR("%s isoc rx submit failed urb %p err %d",
313 husb
->hdev
->name
, urb
, err
);
322 /* Initialize device */
323 static int hci_usb_open(struct hci_dev
*hdev
)
325 struct hci_usb
*husb
= (struct hci_usb
*) hdev
->driver_data
;
329 BT_DBG("%s", hdev
->name
);
331 if (test_and_set_bit(HCI_RUNNING
, &hdev
->flags
))
334 write_lock_irqsave(&husb
->completion_lock
, flags
);
336 err
= hci_usb_intr_rx_submit(husb
);
338 for (i
= 0; i
< HCI_MAX_BULK_RX
; i
++)
339 hci_usb_bulk_rx_submit(husb
);
341 #ifdef CONFIG_BT_HCIUSB_SCO
342 if (husb
->isoc_iface
)
343 for (i
= 0; i
< HCI_MAX_ISOC_RX
; i
++)
344 hci_usb_isoc_rx_submit(husb
);
347 clear_bit(HCI_RUNNING
, &hdev
->flags
);
350 write_unlock_irqrestore(&husb
->completion_lock
, flags
);
355 static int hci_usb_flush(struct hci_dev
*hdev
)
357 struct hci_usb
*husb
= (struct hci_usb
*) hdev
->driver_data
;
360 BT_DBG("%s", hdev
->name
);
362 for (i
= 0; i
< 4; i
++)
363 skb_queue_purge(&husb
->transmit_q
[i
]);
367 static void hci_usb_unlink_urbs(struct hci_usb
*husb
)
371 BT_DBG("%s", husb
->hdev
->name
);
373 for (i
= 0; i
< 4; i
++) {
377 /* Kill pending requests */
378 while ((_urb
= _urb_dequeue(&husb
->pending_q
[i
]))) {
380 BT_DBG("%s unlinking _urb %p type %d urb %p",
381 husb
->hdev
->name
, _urb
, _urb
->type
, urb
);
383 _urb_queue_tail(__completed_q(husb
, _urb
->type
), _urb
);
386 /* Release completed requests */
387 while ((_urb
= _urb_dequeue(&husb
->completed_q
[i
]))) {
389 BT_DBG("%s freeing _urb %p type %d urb %p",
390 husb
->hdev
->name
, _urb
, _urb
->type
, urb
);
391 kfree(urb
->setup_packet
);
392 kfree(urb
->transfer_buffer
);
396 /* Release reassembly buffers */
397 if (husb
->reassembly
[i
]) {
398 kfree_skb(husb
->reassembly
[i
]);
399 husb
->reassembly
[i
] = NULL
;
405 static int hci_usb_close(struct hci_dev
*hdev
)
407 struct hci_usb
*husb
= (struct hci_usb
*) hdev
->driver_data
;
410 if (!test_and_clear_bit(HCI_RUNNING
, &hdev
->flags
))
413 BT_DBG("%s", hdev
->name
);
415 /* Synchronize with completion handlers */
416 write_lock_irqsave(&husb
->completion_lock
, flags
);
417 write_unlock_irqrestore(&husb
->completion_lock
, flags
);
419 hci_usb_unlink_urbs(husb
);
424 static int __tx_submit(struct hci_usb
*husb
, struct _urb
*_urb
)
426 struct urb
*urb
= &_urb
->urb
;
429 BT_DBG("%s urb %p type %d", husb
->hdev
->name
, urb
, _urb
->type
);
431 _urb_queue_tail(__pending_q(husb
, _urb
->type
), _urb
);
432 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
434 BT_ERR("%s tx submit failed urb %p type %d err %d",
435 husb
->hdev
->name
, urb
, _urb
->type
, err
);
437 _urb_queue_tail(__completed_q(husb
, _urb
->type
), _urb
);
439 atomic_inc(__pending_tx(husb
, _urb
->type
));
444 static inline int hci_usb_send_ctrl(struct hci_usb
*husb
, struct sk_buff
*skb
)
446 struct _urb
*_urb
= __get_completed(husb
, bt_cb(skb
)->pkt_type
);
447 struct usb_ctrlrequest
*dr
;
451 _urb
= _urb_alloc(0, GFP_ATOMIC
);
454 _urb
->type
= bt_cb(skb
)->pkt_type
;
456 dr
= kmalloc(sizeof(*dr
), GFP_ATOMIC
);
462 dr
= (void *) _urb
->urb
.setup_packet
;
464 dr
->bRequestType
= husb
->ctrl_req
;
468 dr
->wLength
= __cpu_to_le16(skb
->len
);
471 usb_fill_control_urb(urb
, husb
->udev
, usb_sndctrlpipe(husb
->udev
, 0),
472 (void *) dr
, skb
->data
, skb
->len
, hci_usb_tx_complete
, husb
);
474 BT_DBG("%s skb %p len %d", husb
->hdev
->name
, skb
, skb
->len
);
477 return __tx_submit(husb
, _urb
);
480 static inline int hci_usb_send_bulk(struct hci_usb
*husb
, struct sk_buff
*skb
)
482 struct _urb
*_urb
= __get_completed(husb
, bt_cb(skb
)->pkt_type
);
487 _urb
= _urb_alloc(0, GFP_ATOMIC
);
490 _urb
->type
= bt_cb(skb
)->pkt_type
;
494 pipe
= usb_sndbulkpipe(husb
->udev
, husb
->bulk_out_ep
->desc
.bEndpointAddress
);
495 usb_fill_bulk_urb(urb
, husb
->udev
, pipe
, skb
->data
, skb
->len
,
496 hci_usb_tx_complete
, husb
);
497 urb
->transfer_flags
= URB_ZERO_PACKET
;
499 BT_DBG("%s skb %p len %d", husb
->hdev
->name
, skb
, skb
->len
);
502 return __tx_submit(husb
, _urb
);
505 #ifdef CONFIG_BT_HCIUSB_SCO
506 static inline int hci_usb_send_isoc(struct hci_usb
*husb
, struct sk_buff
*skb
)
508 struct _urb
*_urb
= __get_completed(husb
, bt_cb(skb
)->pkt_type
);
512 _urb
= _urb_alloc(HCI_MAX_ISOC_FRAMES
, GFP_ATOMIC
);
515 _urb
->type
= bt_cb(skb
)->pkt_type
;
518 BT_DBG("%s skb %p len %d", husb
->hdev
->name
, skb
, skb
->len
);
523 urb
->dev
= husb
->udev
;
524 urb
->pipe
= usb_sndisocpipe(husb
->udev
, husb
->isoc_out_ep
->desc
.bEndpointAddress
);
525 urb
->complete
= hci_usb_tx_complete
;
526 urb
->transfer_flags
= URB_ISO_ASAP
;
528 urb
->interval
= husb
->isoc_out_ep
->desc
.bInterval
;
530 urb
->transfer_buffer
= skb
->data
;
531 urb
->transfer_buffer_length
= skb
->len
;
533 __fill_isoc_desc(urb
, skb
->len
, le16_to_cpu(husb
->isoc_out_ep
->desc
.wMaxPacketSize
));
536 return __tx_submit(husb
, _urb
);
540 static void hci_usb_tx_process(struct hci_usb
*husb
)
542 struct sk_buff_head
*q
;
545 BT_DBG("%s", husb
->hdev
->name
);
548 clear_bit(HCI_USB_TX_WAKEUP
, &husb
->state
);
550 /* Process command queue */
551 q
= __transmit_q(husb
, HCI_COMMAND_PKT
);
552 if (!atomic_read(__pending_tx(husb
, HCI_COMMAND_PKT
)) &&
553 (skb
= skb_dequeue(q
))) {
554 if (hci_usb_send_ctrl(husb
, skb
) < 0)
555 skb_queue_head(q
, skb
);
558 #ifdef CONFIG_BT_HCIUSB_SCO
559 /* Process SCO queue */
560 q
= __transmit_q(husb
, HCI_SCODATA_PKT
);
561 if (atomic_read(__pending_tx(husb
, HCI_SCODATA_PKT
)) < HCI_MAX_ISOC_TX
&&
562 (skb
= skb_dequeue(q
))) {
563 if (hci_usb_send_isoc(husb
, skb
) < 0)
564 skb_queue_head(q
, skb
);
568 /* Process ACL queue */
569 q
= __transmit_q(husb
, HCI_ACLDATA_PKT
);
570 while (atomic_read(__pending_tx(husb
, HCI_ACLDATA_PKT
)) < HCI_MAX_BULK_TX
&&
571 (skb
= skb_dequeue(q
))) {
572 if (hci_usb_send_bulk(husb
, skb
) < 0) {
573 skb_queue_head(q
, skb
);
577 } while(test_bit(HCI_USB_TX_WAKEUP
, &husb
->state
));
580 static inline void hci_usb_tx_wakeup(struct hci_usb
*husb
)
582 /* Serialize TX queue processing to avoid data reordering */
583 if (!test_and_set_bit(HCI_USB_TX_PROCESS
, &husb
->state
)) {
584 hci_usb_tx_process(husb
);
585 clear_bit(HCI_USB_TX_PROCESS
, &husb
->state
);
587 set_bit(HCI_USB_TX_WAKEUP
, &husb
->state
);
590 /* Send frames from HCI layer */
591 static int hci_usb_send_frame(struct sk_buff
*skb
)
593 struct hci_dev
*hdev
= (struct hci_dev
*) skb
->dev
;
594 struct hci_usb
*husb
;
597 BT_ERR("frame for uknown device (hdev=NULL)");
601 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
604 BT_DBG("%s type %d len %d", hdev
->name
, bt_cb(skb
)->pkt_type
, skb
->len
);
606 husb
= (struct hci_usb
*) hdev
->driver_data
;
608 switch (bt_cb(skb
)->pkt_type
) {
609 case HCI_COMMAND_PKT
:
613 case HCI_ACLDATA_PKT
:
617 #ifdef CONFIG_BT_HCIUSB_SCO
618 case HCI_SCODATA_PKT
:
628 read_lock(&husb
->completion_lock
);
630 skb_queue_tail(__transmit_q(husb
, bt_cb(skb
)->pkt_type
), skb
);
631 hci_usb_tx_wakeup(husb
);
633 read_unlock(&husb
->completion_lock
);
637 static inline int __recv_frame(struct hci_usb
*husb
, int type
, void *data
, int count
)
639 BT_DBG("%s type %d data %p count %d", husb
->hdev
->name
, type
, data
, count
);
641 husb
->hdev
->stat
.byte_rx
+= count
;
644 struct sk_buff
*skb
= __reassembly(husb
, type
);
645 struct { int expect
; } *scb
;
649 /* Start of the frame */
653 if (count
>= HCI_EVENT_HDR_SIZE
) {
654 struct hci_event_hdr
*h
= data
;
655 len
= HCI_EVENT_HDR_SIZE
+ h
->plen
;
660 case HCI_ACLDATA_PKT
:
661 if (count
>= HCI_ACL_HDR_SIZE
) {
662 struct hci_acl_hdr
*h
= data
;
663 len
= HCI_ACL_HDR_SIZE
+ __le16_to_cpu(h
->dlen
);
667 #ifdef CONFIG_BT_HCIUSB_SCO
668 case HCI_SCODATA_PKT
:
669 if (count
>= HCI_SCO_HDR_SIZE
) {
670 struct hci_sco_hdr
*h
= data
;
671 len
= HCI_SCO_HDR_SIZE
+ h
->dlen
;
677 BT_DBG("new packet len %d", len
);
679 skb
= bt_skb_alloc(len
, GFP_ATOMIC
);
681 BT_ERR("%s no memory for the packet", husb
->hdev
->name
);
684 skb
->dev
= (void *) husb
->hdev
;
685 bt_cb(skb
)->pkt_type
= type
;
687 __reassembly(husb
, type
) = skb
;
689 scb
= (void *) skb
->cb
;
693 scb
= (void *) skb
->cb
;
697 len
= min(len
, count
);
699 memcpy(skb_put(skb
, len
), data
, len
);
704 __reassembly(husb
, type
) = NULL
;
705 bt_cb(skb
)->pkt_type
= type
;
709 count
-= len
; data
+= len
;
714 static void hci_usb_rx_complete(struct urb
*urb
, struct pt_regs
*regs
)
716 struct _urb
*_urb
= container_of(urb
, struct _urb
, urb
);
717 struct hci_usb
*husb
= (void *) urb
->context
;
718 struct hci_dev
*hdev
= husb
->hdev
;
719 int err
, count
= urb
->actual_length
;
721 BT_DBG("%s urb %p type %d status %d count %d flags %x", hdev
->name
, urb
,
722 _urb
->type
, urb
->status
, count
, urb
->transfer_flags
);
724 read_lock(&husb
->completion_lock
);
726 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
729 if (urb
->status
|| !count
)
732 if (_urb
->type
== HCI_SCODATA_PKT
) {
733 #ifdef CONFIG_BT_HCIUSB_SCO
735 for (i
=0; i
< urb
->number_of_packets
; i
++) {
736 BT_DBG("desc %d status %d offset %d len %d", i
,
737 urb
->iso_frame_desc
[i
].status
,
738 urb
->iso_frame_desc
[i
].offset
,
739 urb
->iso_frame_desc
[i
].actual_length
);
741 if (!urb
->iso_frame_desc
[i
].status
)
742 __recv_frame(husb
, _urb
->type
,
743 urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
,
744 urb
->iso_frame_desc
[i
].actual_length
);
750 err
= __recv_frame(husb
, _urb
->type
, urb
->transfer_buffer
, count
);
752 BT_ERR("%s corrupted packet: type %d count %d",
753 husb
->hdev
->name
, _urb
->type
, count
);
759 urb
->dev
= husb
->udev
;
760 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
761 BT_DBG("%s urb %p type %d resubmit status %d", hdev
->name
, urb
,
765 read_unlock(&husb
->completion_lock
);
768 static void hci_usb_tx_complete(struct urb
*urb
, struct pt_regs
*regs
)
770 struct _urb
*_urb
= container_of(urb
, struct _urb
, urb
);
771 struct hci_usb
*husb
= (void *) urb
->context
;
772 struct hci_dev
*hdev
= husb
->hdev
;
774 BT_DBG("%s urb %p status %d flags %x", hdev
->name
, urb
,
775 urb
->status
, urb
->transfer_flags
);
777 atomic_dec(__pending_tx(husb
, _urb
->type
));
779 urb
->transfer_buffer
= NULL
;
780 kfree_skb((struct sk_buff
*) _urb
->priv
);
782 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
786 hdev
->stat
.byte_tx
+= urb
->transfer_buffer_length
;
790 read_lock(&husb
->completion_lock
);
793 _urb_queue_tail(__completed_q(husb
, _urb
->type
), _urb
);
795 hci_usb_tx_wakeup(husb
);
797 read_unlock(&husb
->completion_lock
);
800 static void hci_usb_destruct(struct hci_dev
*hdev
)
802 struct hci_usb
*husb
= (struct hci_usb
*) hdev
->driver_data
;
804 BT_DBG("%s", hdev
->name
);
809 static void hci_usb_notify(struct hci_dev
*hdev
, unsigned int evt
)
811 BT_DBG("%s evt %d", hdev
->name
, evt
);
814 static int hci_usb_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
816 struct usb_device
*udev
= interface_to_usbdev(intf
);
817 struct usb_host_endpoint
*bulk_out_ep
= NULL
;
818 struct usb_host_endpoint
*bulk_in_ep
= NULL
;
819 struct usb_host_endpoint
*intr_in_ep
= NULL
;
820 struct usb_host_endpoint
*ep
;
821 struct usb_host_interface
*uif
;
822 struct usb_interface
*isoc_iface
;
823 struct hci_usb
*husb
;
824 struct hci_dev
*hdev
;
825 int i
, e
, size
, isoc_ifnum
, isoc_alts
;
827 BT_DBG("udev %p intf %p", udev
, intf
);
829 if (!id
->driver_info
) {
830 const struct usb_device_id
*match
;
831 match
= usb_match_id(intf
, blacklist_ids
);
836 if (ignore
|| id
->driver_info
& HCI_IGNORE
)
839 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
> 0)
842 /* Find endpoints that we need */
843 uif
= intf
->cur_altsetting
;
844 for (e
= 0; e
< uif
->desc
.bNumEndpoints
; e
++) {
845 ep
= &uif
->endpoint
[e
];
847 switch (ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
848 case USB_ENDPOINT_XFER_INT
:
849 if (ep
->desc
.bEndpointAddress
& USB_DIR_IN
)
853 case USB_ENDPOINT_XFER_BULK
:
854 if (ep
->desc
.bEndpointAddress
& USB_DIR_IN
)
862 if (!bulk_in_ep
|| !bulk_out_ep
|| !intr_in_ep
) {
863 BT_DBG("Bulk endpoints not found");
867 if (!(husb
= kmalloc(sizeof(struct hci_usb
), GFP_KERNEL
))) {
868 BT_ERR("Can't allocate: control structure");
872 memset(husb
, 0, sizeof(struct hci_usb
));
875 husb
->bulk_out_ep
= bulk_out_ep
;
876 husb
->bulk_in_ep
= bulk_in_ep
;
877 husb
->intr_in_ep
= intr_in_ep
;
879 if (id
->driver_info
& HCI_DIGIANSWER
)
880 husb
->ctrl_req
= USB_TYPE_VENDOR
;
882 husb
->ctrl_req
= USB_TYPE_CLASS
;
884 /* Find isochronous endpoints that we can use */
890 #ifdef CONFIG_BT_HCIUSB_SCO
891 if (isoc
&& !(id
->driver_info
& (HCI_BROKEN_ISOC
| HCI_SNIFFER
)))
892 isoc_iface
= usb_ifnum_to_if(udev
, isoc_ifnum
);
896 struct usb_host_endpoint
*isoc_out_ep
= NULL
;
897 struct usb_host_endpoint
*isoc_in_ep
= NULL
;
899 for (a
= 0; a
< isoc_iface
->num_altsetting
; a
++) {
900 uif
= &isoc_iface
->altsetting
[a
];
901 for (e
= 0; e
< uif
->desc
.bNumEndpoints
; e
++) {
902 ep
= &uif
->endpoint
[e
];
904 switch (ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
905 case USB_ENDPOINT_XFER_ISOC
:
906 if (le16_to_cpu(ep
->desc
.wMaxPacketSize
) < size
||
907 uif
->desc
.bAlternateSetting
!= isoc
)
909 size
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
911 isoc_alts
= uif
->desc
.bAlternateSetting
;
913 if (ep
->desc
.bEndpointAddress
& USB_DIR_IN
)
922 if (!isoc_in_ep
|| !isoc_out_ep
)
923 BT_DBG("Isoc endpoints not found");
925 BT_DBG("isoc ifnum %d alts %d", isoc_ifnum
, isoc_alts
);
926 if (usb_driver_claim_interface(&hci_usb_driver
, isoc_iface
, husb
) != 0)
927 BT_ERR("Can't claim isoc interface");
928 else if (usb_set_interface(udev
, isoc_ifnum
, isoc_alts
)) {
929 BT_ERR("Can't set isoc interface settings");
930 husb
->isoc_iface
= isoc_iface
;
931 usb_driver_release_interface(&hci_usb_driver
, isoc_iface
);
932 husb
->isoc_iface
= NULL
;
934 husb
->isoc_iface
= isoc_iface
;
935 husb
->isoc_in_ep
= isoc_in_ep
;
936 husb
->isoc_out_ep
= isoc_out_ep
;
942 rwlock_init(&husb
->completion_lock
);
944 for (i
= 0; i
< 4; i
++) {
945 skb_queue_head_init(&husb
->transmit_q
[i
]);
946 _urb_queue_init(&husb
->pending_q
[i
]);
947 _urb_queue_init(&husb
->completed_q
[i
]);
950 /* Initialize and register HCI device */
951 hdev
= hci_alloc_dev();
953 BT_ERR("Can't allocate HCI device");
959 hdev
->type
= HCI_USB
;
960 hdev
->driver_data
= husb
;
961 SET_HCIDEV_DEV(hdev
, &intf
->dev
);
963 hdev
->open
= hci_usb_open
;
964 hdev
->close
= hci_usb_close
;
965 hdev
->flush
= hci_usb_flush
;
966 hdev
->send
= hci_usb_send_frame
;
967 hdev
->destruct
= hci_usb_destruct
;
968 hdev
->notify
= hci_usb_notify
;
970 hdev
->owner
= THIS_MODULE
;
972 if (reset
|| id
->driver_info
& HCI_RESET
)
973 set_bit(HCI_QUIRK_RESET_ON_INIT
, &hdev
->quirks
);
975 if (id
->driver_info
& HCI_SNIFFER
) {
976 if (le16_to_cpu(udev
->descriptor
.bcdDevice
) > 0x997)
977 set_bit(HCI_QUIRK_RAW_DEVICE
, &hdev
->quirks
);
980 if (id
->driver_info
& HCI_BCM92035
) {
981 unsigned char cmd
[] = { 0x3b, 0xfc, 0x01, 0x00 };
984 skb
= bt_skb_alloc(sizeof(cmd
), GFP_KERNEL
);
986 memcpy(skb_put(skb
, sizeof(cmd
)), cmd
, sizeof(cmd
));
987 skb_queue_tail(&hdev
->driver_init
, skb
);
991 if (hci_register_dev(hdev
) < 0) {
992 BT_ERR("Can't register HCI device");
997 usb_set_intfdata(intf
, husb
);
1001 if (husb
->isoc_iface
)
1002 usb_driver_release_interface(&hci_usb_driver
, husb
->isoc_iface
);
1009 static void hci_usb_disconnect(struct usb_interface
*intf
)
1011 struct hci_usb
*husb
= usb_get_intfdata(intf
);
1012 struct hci_dev
*hdev
;
1014 if (!husb
|| intf
== husb
->isoc_iface
)
1017 usb_set_intfdata(intf
, NULL
);
1020 BT_DBG("%s", hdev
->name
);
1022 hci_usb_close(hdev
);
1024 if (husb
->isoc_iface
)
1025 usb_driver_release_interface(&hci_usb_driver
, husb
->isoc_iface
);
1027 if (hci_unregister_dev(hdev
) < 0)
1028 BT_ERR("Can't unregister HCI device %s", hdev
->name
);
1033 static struct usb_driver hci_usb_driver
= {
1034 .owner
= THIS_MODULE
,
1036 .probe
= hci_usb_probe
,
1037 .disconnect
= hci_usb_disconnect
,
1038 .id_table
= bluetooth_ids
,
1041 static int __init
hci_usb_init(void)
1045 BT_INFO("HCI USB driver ver %s", VERSION
);
1047 if ((err
= usb_register(&hci_usb_driver
)) < 0)
1048 BT_ERR("Failed to register HCI USB driver");
1053 static void __exit
hci_usb_exit(void)
1055 usb_deregister(&hci_usb_driver
);
1058 module_init(hci_usb_init
);
1059 module_exit(hci_usb_exit
);
1061 module_param(ignore
, bool, 0644);
1062 MODULE_PARM_DESC(ignore
, "Ignore devices from the matching table");
1064 module_param(reset
, bool, 0644);
1065 MODULE_PARM_DESC(reset
, "Send HCI reset command on initialization");
1067 #ifdef CONFIG_BT_HCIUSB_SCO
1068 module_param(isoc
, int, 0644);
1069 MODULE_PARM_DESC(isoc
, "Set isochronous transfers for SCO over HCI support");
1072 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
1073 MODULE_DESCRIPTION("Bluetooth HCI USB driver ver " VERSION
);
1074 MODULE_VERSION(VERSION
);
1075 MODULE_LICENSE("GPL");