2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22 SOFTWARE IS DISCLAIMED.
26 * BlueZ HCI USB driver.
27 * Based on original USB Bluetooth driver for Linux kernel
28 * Copyright (c) 2000 Greg Kroah-Hartman <greg@kroah.com>
29 * Copyright (c) 2000 Mark Douglas Corner <mcorner@umich.edu>
31 * $Id: hci_usb.c,v 1.1.1.1 2005/04/11 02:50:17 jack Exp $
35 #include <linux/config.h>
36 #include <linux/module.h>
38 #define __KERNEL_SYSCALLS__
40 #include <linux/version.h>
41 #include <linux/kernel.h>
42 #include <linux/init.h>
43 #include <linux/sched.h>
44 #include <linux/unistd.h>
45 #include <linux/types.h>
46 #include <linux/interrupt.h>
48 #include <linux/slab.h>
49 #include <linux/errno.h>
50 #include <linux/string.h>
51 #include <linux/skbuff.h>
52 #include <linux/kmod.h>
54 #include <linux/usb.h>
56 #include <net/bluetooth/bluetooth.h>
57 #include <net/bluetooth/hci_core.h>
60 #define HCI_MAX_PENDING (HCI_MAX_BULK_RX + HCI_MAX_BULK_TX + 1)
64 #define BT_DBG( A... )
66 #define BT_DMP( A... )
69 #ifndef CONFIG_BLUEZ_USB_ZERO_PACKET
70 #undef USB_ZERO_PACKET
71 #define USB_ZERO_PACKET 0
74 static struct usb_driver hci_usb_driver;
76 static struct usb_device_id bluetooth_ids[] = {
77 /* Generic Bluetooth USB device */
78 { USB_DEVICE_INFO(HCI_DEV_CLASS, HCI_DEV_SUBCLASS, HCI_DEV_PROTOCOL) },
80 /* Ericsson with non-standard id */
81 { USB_DEVICE(0x0bdb, 0x1002) },
83 { } /* Terminating entry */
86 MODULE_DEVICE_TABLE (usb, bluetooth_ids);
88 static struct usb_device_id ignore_ids[] = {
89 /* Broadcom BCM2033 without firmware */
90 { USB_DEVICE(0x0a5c, 0x2033) },
92 { } /* Terminating entry */
95 static void hci_usb_interrupt(struct urb *urb);
96 static void hci_usb_rx_complete(struct urb *urb);
97 static void hci_usb_tx_complete(struct urb *urb);
99 static struct urb *hci_usb_get_completed(struct hci_usb *husb)
102 struct urb *urb = NULL;
104 skb = skb_dequeue(&husb->completed_q);
106 urb = ((struct hci_usb_scb *) skb->cb)->urb;
110 BT_DBG("%s urb %p", husb->hdev.name, urb);
114 static int hci_usb_enable_intr(struct hci_usb *husb)
120 BT_DBG("%s", husb->hdev.name);
122 if (!(urb = usb_alloc_urb(0)))
125 if (!(buf = kmalloc(HCI_MAX_EVENT_SIZE, GFP_KERNEL))) {
130 husb->intr_urb = urb;
132 pipe = usb_rcvintpipe(husb->udev, husb->intr_ep);
133 size = usb_maxpacket(husb->udev, pipe, usb_pipeout(pipe));
134 FILL_INT_URB(urb, husb->udev, pipe, buf, size,
135 hci_usb_interrupt, husb, husb->intr_interval);
137 return usb_submit_urb(urb);
140 static int hci_usb_disable_intr(struct hci_usb *husb)
142 struct urb *urb = husb->intr_urb;
145 BT_DBG("%s", husb->hdev.name);
147 usb_unlink_urb(urb); usb_free_urb(urb);
148 husb->intr_urb = NULL;
150 skb = husb->intr_skb;
152 husb->intr_skb = NULL;
159 static int hci_usb_rx_submit(struct hci_usb *husb, struct urb *urb)
161 struct hci_usb_scb *scb;
165 if (!urb && !(urb = usb_alloc_urb(0)))
168 size = HCI_MAX_FRAME_SIZE;
170 if (!(skb = bluez_skb_alloc(size, GFP_ATOMIC))) {
175 BT_DBG("%s urb %p", husb->hdev.name, urb);
177 skb->dev = (void *) &husb->hdev;
178 skb->pkt_type = HCI_ACLDATA_PKT;
180 scb = (struct hci_usb_scb *) skb->cb;
183 pipe = usb_rcvbulkpipe(husb->udev, husb->bulk_in_ep);
185 FILL_BULK_URB(urb, husb->udev, pipe, skb->data, size, hci_usb_rx_complete, skb);
186 urb->transfer_flags = USB_QUEUE_BULK;
188 skb_queue_tail(&husb->pending_q, skb);
189 err = usb_submit_urb(urb);
191 BT_ERR("%s bulk rx submit failed urb %p err %d",
192 husb->hdev.name, urb, err);
199 /* Initialize device */
200 static int hci_usb_open(struct hci_dev *hdev)
202 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
206 BT_DBG("%s", hdev->name);
208 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
213 write_lock_irqsave(&husb->completion_lock, flags);
215 err = hci_usb_enable_intr(husb);
217 for (i = 0; i < HCI_MAX_BULK_RX; i++)
218 hci_usb_rx_submit(husb, NULL);
220 clear_bit(HCI_RUNNING, &hdev->flags);
224 write_unlock_irqrestore(&husb->completion_lock, flags);
229 static int hci_usb_flush(struct hci_dev *hdev)
231 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
233 BT_DBG("%s", hdev->name);
235 skb_queue_purge(&husb->cmd_q);
236 skb_queue_purge(&husb->acl_q);
240 static inline void hci_usb_unlink_urbs(struct hci_usb *husb)
245 BT_DBG("%s", husb->hdev.name);
247 while ((skb = skb_dequeue(&husb->pending_q))) {
248 urb = ((struct hci_usb_scb *) skb->cb)->urb;
253 while ((urb = hci_usb_get_completed(husb)))
258 static int hci_usb_close(struct hci_dev *hdev)
260 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
263 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
266 BT_DBG("%s", hdev->name);
268 write_lock_irqsave(&husb->completion_lock, flags);
270 hci_usb_disable_intr(husb);
271 hci_usb_unlink_urbs(husb);
274 write_unlock_irqrestore(&husb->completion_lock, flags);
280 static inline int hci_usb_send_ctrl(struct hci_usb *husb, struct sk_buff *skb)
282 struct hci_usb_scb *scb = (void *) skb->cb;
283 struct urb *urb = hci_usb_get_completed(husb);
284 struct usb_ctrlrequest *dr;
287 if (!urb && !(urb = usb_alloc_urb(0)))
290 if (!(dr = kmalloc(sizeof(*dr), GFP_ATOMIC))) {
295 pipe = usb_sndctrlpipe(husb->udev, 0);
297 dr->bRequestType = HCI_CTRL_REQ;
301 dr->wLength = __cpu_to_le16(skb->len);
303 FILL_CONTROL_URB(urb, husb->udev, pipe, (void *) dr,
304 skb->data, skb->len, hci_usb_tx_complete, skb);
306 BT_DBG("%s urb %p len %d", husb->hdev.name, urb, skb->len);
310 skb_queue_tail(&husb->pending_q, skb);
311 err = usb_submit_urb(urb);
313 BT_ERR("%s ctrl tx submit failed urb %p err %d",
314 husb->hdev.name, urb, err);
316 usb_free_urb(urb); kfree(dr);
321 static inline int hci_usb_send_bulk(struct hci_usb *husb, struct sk_buff *skb)
323 struct hci_usb_scb *scb = (void *) skb->cb;
324 struct urb *urb = hci_usb_get_completed(husb);
327 if (!urb && !(urb = usb_alloc_urb(0)))
330 pipe = usb_sndbulkpipe(husb->udev, husb->bulk_out_ep);
332 FILL_BULK_URB(urb, husb->udev, pipe, skb->data, skb->len,
333 hci_usb_tx_complete, skb);
334 urb->transfer_flags = USB_QUEUE_BULK | USB_ZERO_PACKET;
336 BT_DBG("%s urb %p len %d", husb->hdev.name, urb, skb->len);
340 skb_queue_tail(&husb->pending_q, skb);
341 err = usb_submit_urb(urb);
343 BT_ERR("%s bulk tx submit failed urb %p err %d",
344 husb->hdev.name, urb, err);
351 static void hci_usb_tx_process(struct hci_usb *husb)
355 BT_DBG("%s", husb->hdev.name);
358 clear_bit(HCI_USB_TX_WAKEUP, &husb->state);
360 /* Process ACL queue */
361 while (skb_queue_len(&husb->pending_q) < HCI_MAX_PENDING &&
362 (skb = skb_dequeue(&husb->acl_q))) {
363 if (hci_usb_send_bulk(husb, skb) < 0) {
364 skb_queue_head(&husb->acl_q, skb);
369 /* Process command queue */
370 if (!test_bit(HCI_USB_CTRL_TX, &husb->state) &&
371 (skb = skb_dequeue(&husb->cmd_q)) != NULL) {
372 set_bit(HCI_USB_CTRL_TX, &husb->state);
373 if (hci_usb_send_ctrl(husb, skb) < 0) {
374 skb_queue_head(&husb->cmd_q, skb);
375 clear_bit(HCI_USB_CTRL_TX, &husb->state);
378 } while(test_bit(HCI_USB_TX_WAKEUP, &husb->state));
381 static inline void hci_usb_tx_wakeup(struct hci_usb *husb)
383 /* Serialize TX queue processing to avoid data reordering */
384 if (!test_and_set_bit(HCI_USB_TX_PROCESS, &husb->state)) {
385 hci_usb_tx_process(husb);
386 clear_bit(HCI_USB_TX_PROCESS, &husb->state);
388 set_bit(HCI_USB_TX_WAKEUP, &husb->state);
391 /* Send frames from HCI layer */
392 static int hci_usb_send_frame(struct sk_buff *skb)
394 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
395 struct hci_usb *husb;
398 BT_ERR("frame for uknown device (hdev=NULL)");
402 if (!test_bit(HCI_RUNNING, &hdev->flags))
405 husb = (struct hci_usb *) hdev->driver_data;
407 BT_DBG("%s type %d len %d", hdev->name, skb->pkt_type, skb->len);
409 read_lock(&husb->completion_lock);
411 switch (skb->pkt_type) {
412 case HCI_COMMAND_PKT:
413 skb_queue_tail(&husb->cmd_q, skb);
417 case HCI_ACLDATA_PKT:
418 skb_queue_tail(&husb->acl_q, skb);
422 case HCI_SCODATA_PKT:
427 hci_usb_tx_wakeup(husb);
429 read_unlock(&husb->completion_lock);
433 static void hci_usb_interrupt(struct urb *urb)
435 struct hci_usb *husb = (void *) urb->context;
436 struct hci_usb_scb *scb;
439 __u8 *data = urb->transfer_buffer;
440 int count = urb->actual_length;
441 int len = HCI_EVENT_HDR_SIZE;
443 BT_DBG("%s urb %p count %d", husb->hdev.name, urb, count);
445 if (!test_bit(HCI_RUNNING, &husb->hdev.flags))
448 if (urb->status || !count) {
449 BT_DBG("%s intr status %d, count %d",
450 husb->hdev.name, urb->status, count);
454 read_lock(&husb->completion_lock);
456 husb->hdev.stat.byte_rx += count;
458 if (!(skb = husb->intr_skb)) {
459 /* Start of the frame */
460 if (count < HCI_EVENT_HDR_SIZE)
463 eh = (hci_event_hdr *) data;
464 len = eh->plen + HCI_EVENT_HDR_SIZE;
469 skb = bluez_skb_alloc(len, GFP_ATOMIC);
471 BT_ERR("%s no memory for event packet", husb->hdev.name);
474 scb = (void *) skb->cb;
476 skb->dev = (void *) &husb->hdev;
477 skb->pkt_type = HCI_EVENT_PKT;
479 husb->intr_skb = skb;
483 scb = (void *) skb->cb;
486 husb->intr_skb = NULL;
492 memcpy(skb_put(skb, count), data, count);
493 scb->intr_len -= count;
495 if (!scb->intr_len) {
497 husb->intr_skb = NULL;
502 read_unlock(&husb->completion_lock);
506 BT_ERR("%s bad frame len %d expected %d", husb->hdev.name, count, len);
507 husb->hdev.stat.err_rx++;
508 read_unlock(&husb->completion_lock);
511 static void hci_usb_tx_complete(struct urb *urb)
513 struct sk_buff *skb = (struct sk_buff *) urb->context;
514 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
515 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
517 BT_DBG("%s urb %p status %d flags %x", husb->hdev.name, urb,
518 urb->status, urb->transfer_flags);
520 if (urb->pipe == usb_sndctrlpipe(husb->udev, 0)) {
521 kfree(urb->setup_packet);
522 clear_bit(HCI_USB_CTRL_TX, &husb->state);
525 if (!test_bit(HCI_RUNNING, &hdev->flags))
528 read_lock(&husb->completion_lock);
531 husb->hdev.stat.byte_tx += skb->len;
533 husb->hdev.stat.err_tx++;
536 skb_queue_tail(&husb->completed_q, skb);
537 hci_usb_tx_wakeup(husb);
539 read_unlock(&husb->completion_lock);
543 static void hci_usb_rx_complete(struct urb *urb)
545 struct sk_buff *skb = (struct sk_buff *) urb->context;
546 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
547 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
548 int status, count = urb->actual_length;
552 BT_DBG("%s urb %p status %d count %d flags %x", husb->hdev.name, urb,
553 urb->status, count, urb->transfer_flags);
555 if (!test_bit(HCI_RUNNING, &hdev->flags))
558 read_lock(&husb->completion_lock);
560 if (urb->status || !count)
563 husb->hdev.stat.byte_rx += count;
565 ah = (hci_acl_hdr *) skb->data;
566 dlen = __le16_to_cpu(ah->dlen);
567 size = HCI_ACL_HDR_SIZE + dlen;
569 /* Verify frame len and completeness */
571 BT_ERR("%s corrupted ACL packet: count %d, dlen %d",
572 husb->hdev.name, count, dlen);
573 bluez_dump("hci_usb", skb->data, count);
574 husb->hdev.stat.err_rx++;
582 hci_usb_rx_submit(husb, urb);
584 read_unlock(&husb->completion_lock);
588 urb->dev = husb->udev;
589 status = usb_submit_urb(urb);
590 BT_DBG("%s URB resubmit status %d", husb->hdev.name, status);
591 read_unlock(&husb->completion_lock);
594 static void hci_usb_destruct(struct hci_dev *hdev)
596 struct hci_usb *husb;
600 BT_DBG("%s", hdev->name);
602 husb = (struct hci_usb *) hdev->driver_data;
606 static void *hci_usb_probe(struct usb_device *udev, unsigned int ifnum, const struct usb_device_id *id)
608 struct usb_endpoint_descriptor *bulk_out_ep[HCI_MAX_IFACE_NUM];
609 struct usb_endpoint_descriptor *isoc_out_ep[HCI_MAX_IFACE_NUM];
610 struct usb_endpoint_descriptor *bulk_in_ep[HCI_MAX_IFACE_NUM];
611 struct usb_endpoint_descriptor *isoc_in_ep[HCI_MAX_IFACE_NUM];
612 struct usb_endpoint_descriptor *intr_in_ep[HCI_MAX_IFACE_NUM];
613 struct usb_interface_descriptor *uif;
614 struct usb_endpoint_descriptor *ep;
615 struct usb_interface *iface, *isoc_iface;
616 struct hci_usb *husb;
617 struct hci_dev *hdev;
618 int i, a, e, size, ifn, isoc_ifnum, isoc_alts;
620 BT_DBG("udev %p ifnum %d", udev, ifnum);
622 iface = &udev->actconfig->interface[0];
624 /* Check our black list */
625 if (usb_match_id(udev, iface, ignore_ids))
628 /* Check number of endpoints */
629 if (udev->actconfig->interface[ifnum].altsetting[0].bNumEndpoints < 3)
632 memset(bulk_out_ep, 0, sizeof(bulk_out_ep));
633 memset(isoc_out_ep, 0, sizeof(isoc_out_ep));
634 memset(bulk_in_ep, 0, sizeof(bulk_in_ep));
635 memset(isoc_in_ep, 0, sizeof(isoc_in_ep));
636 memset(intr_in_ep, 0, sizeof(intr_in_ep));
640 isoc_alts = isoc_ifnum = 0;
642 /* Find endpoints that we need */
644 ifn = MIN(udev->actconfig->bNumInterfaces, HCI_MAX_IFACE_NUM);
645 for (i = 0; i < ifn; i++) {
646 iface = &udev->actconfig->interface[i];
647 for (a = 0; a < iface->num_altsetting; a++) {
648 uif = &iface->altsetting[a];
649 for (e = 0; e < uif->bNumEndpoints; e++) {
650 ep = &uif->endpoint[e];
652 switch (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
653 case USB_ENDPOINT_XFER_INT:
654 if (ep->bEndpointAddress & USB_DIR_IN)
658 case USB_ENDPOINT_XFER_BULK:
659 if (ep->bEndpointAddress & USB_DIR_IN)
665 case USB_ENDPOINT_XFER_ISOC:
666 if (ep->wMaxPacketSize < size)
668 size = ep->wMaxPacketSize;
674 if (ep->bEndpointAddress & USB_DIR_IN)
684 if (!bulk_in_ep[0] || !bulk_out_ep[0] || !intr_in_ep[0]) {
685 BT_DBG("Bulk endpoints not found");
689 if (!isoc_in_ep[1] || !isoc_out_ep[1]) {
690 BT_DBG("Isoc endpoints not found");
694 if (!(husb = kmalloc(sizeof(struct hci_usb), GFP_KERNEL))) {
695 BT_ERR("Can't allocate: control structure");
699 memset(husb, 0, sizeof(struct hci_usb));
702 husb->bulk_out_ep = bulk_out_ep[0]->bEndpointAddress;
703 husb->bulk_in_ep = bulk_in_ep[0]->bEndpointAddress;
705 husb->intr_ep = intr_in_ep[0]->bEndpointAddress;
706 husb->intr_interval = intr_in_ep[0]->bInterval;
709 if (usb_set_interface(udev, isoc_ifnum, isoc_alts)) {
710 BT_ERR("Can't set isoc interface settings");
713 usb_driver_claim_interface(&hci_usb_driver, isoc_iface, husb);
714 husb->isoc_iface = isoc_iface;
716 husb->isoc_in_ep = isoc_in_ep[1]->bEndpointAddress;
717 husb->isoc_out_ep = isoc_in_ep[1]->bEndpointAddress;
720 husb->completion_lock = RW_LOCK_UNLOCKED;
722 skb_queue_head_init(&husb->acl_q);
723 skb_queue_head_init(&husb->cmd_q);
724 skb_queue_head_init(&husb->pending_q);
725 skb_queue_head_init(&husb->completed_q);
727 /* Initialize and register HCI device */
730 hdev->type = HCI_USB;
731 hdev->driver_data = husb;
733 hdev->open = hci_usb_open;
734 hdev->close = hci_usb_close;
735 hdev->flush = hci_usb_flush;
736 hdev->send = hci_usb_send_frame;
737 hdev->destruct = hci_usb_destruct;
739 if (hci_register_dev(hdev) < 0) {
740 BT_ERR("Can't register HCI device");
753 static void hci_usb_disconnect(struct usb_device *udev, void *ptr)
755 struct hci_usb *husb = (struct hci_usb *) ptr;
756 struct hci_dev *hdev = &husb->hdev;
761 BT_DBG("%s", hdev->name);
765 if (husb->isoc_iface)
766 usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
768 if (hci_unregister_dev(hdev) < 0)
769 BT_ERR("Can't unregister HCI device %s", hdev->name);
772 static struct usb_driver hci_usb_driver = {
774 probe: hci_usb_probe,
775 disconnect: hci_usb_disconnect,
776 id_table: bluetooth_ids,
779 int hci_usb_init(void)
783 BT_INFO("BlueZ HCI USB driver ver %s Copyright (C) 2000,2001 Qualcomm Inc",
785 BT_INFO("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
787 if ((err = usb_register(&hci_usb_driver)) < 0)
788 BT_ERR("Failed to register HCI USB driver");
793 void hci_usb_cleanup(void)
795 usb_deregister(&hci_usb_driver);
798 module_init(hci_usb_init);
799 module_exit(hci_usb_cleanup);
801 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
802 MODULE_DESCRIPTION("BlueZ HCI USB driver ver " VERSION);
803 MODULE_LICENSE("GPL");