]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/commitdiff
[Bluetooth] Add UART driver for Texas Instruments' BRF63xx chips
authorOhad Ben-Cohen <ohad@bencohen.org>
Sat, 20 Oct 2007 11:42:36 +0000 (13:42 +0200)
committerDavid S. Miller <davem@sunset.davemloft.net>
Mon, 22 Oct 2007 09:59:44 +0000 (02:59 -0700)
Add support for Texas Instruments' HCI Low Level (HCILL) Bluetooth
protocol, which is a power management extension to H4. The HCILL is
widely used by TI's BRF63xx Bluetooth chips.

Signed-off-by: Ohad Ben-Cohen <ohad@bencohen.org>
Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
drivers/bluetooth/Kconfig
drivers/bluetooth/Makefile
drivers/bluetooth/hci_ldisc.c
drivers/bluetooth/hci_ll.c [new file with mode: 0644]
drivers/bluetooth/hci_uart.h

index b9fbe6e7f9ae6e6fd9da5e78990efd5e87229bc4..62b89b95ace28e5a5e70842d7a68a694f258c8fa 100644 (file)
@@ -55,6 +55,17 @@ config BT_HCIUART_BCSP
 
          Say Y here to compile support for HCI BCSP protocol.
 
+config BT_HCIUART_LL
+       bool "HCILL protocol support"
+       depends on BT_HCIUART
+       help
+         HCILL (HCI Low Level) is a serial protocol for communication
+         between Bluetooth device and host. This protocol is required for
+         serial Bluetooth devices that are based on Texas Instruments'
+         BRF chips.
+
+         Say Y here to compile support for HCILL protocol.
+
 config BT_HCIBCM203X
        tristate "HCI BCM203x USB driver"
        depends on USB
index 08c10e178e02400fd3aae0027943a94b6c91daea..a543dfc2d6f5e81087a205d6faace9e954e64e15 100644 (file)
@@ -16,4 +16,5 @@ obj-$(CONFIG_BT_HCIBTUART)    += btuart_cs.o
 hci_uart-y                             := hci_ldisc.o
 hci_uart-$(CONFIG_BT_HCIUART_H4)       += hci_h4.o
 hci_uart-$(CONFIG_BT_HCIUART_BCSP)     += hci_bcsp.o
+hci_uart-$(CONFIG_BT_HCIUART_LL)       += hci_ll.o
 hci_uart-objs                          := $(hci_uart-y)
index 6055b9c0ac0f0b2f96380bebbb227b1fcaad055f..e68821d074b0ad90a24f1e686f1b5da9875e808a 100644 (file)
@@ -549,7 +549,10 @@ static int __init hci_uart_init(void)
 #ifdef CONFIG_BT_HCIUART_BCSP
        bcsp_init();
 #endif
-       
+#ifdef CONFIG_BT_HCIUART_LL
+       ll_init();
+#endif
+
        return 0;
 }
 
@@ -563,6 +566,9 @@ static void __exit hci_uart_exit(void)
 #ifdef CONFIG_BT_HCIUART_BCSP
        bcsp_deinit();
 #endif
+#ifdef CONFIG_BT_HCIUART_LL
+       ll_deinit();
+#endif
 
        /* Release tty registration of line discipline */
        if ((err = tty_unregister_ldisc(N_HCI)))
diff --git a/drivers/bluetooth/hci_ll.c b/drivers/bluetooth/hci_ll.c
new file mode 100644 (file)
index 0000000..8c3e62a
--- /dev/null
@@ -0,0 +1,531 @@
+/*
+ *  Texas Instruments' Bluetooth HCILL UART protocol
+ *
+ *  HCILL (HCI Low Level) is a Texas Instruments' power management
+ *  protocol extension to H4.
+ *
+ *  Copyright (C) 2007 Texas Instruments, Inc.
+ *
+ *  Written by Ohad Ben-Cohen <ohad@bencohen.org>
+ *
+ *  Acknowledgements:
+ *  This file is based on hci_h4.c, which was written
+ *  by Maxim Krasnyansky and Marcel Holtmann.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2
+ *  as published by the Free Software Foundation
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+
+#include <linux/init.h>
+#include <linux/sched.h>
+#include <linux/types.h>
+#include <linux/fcntl.h>
+#include <linux/interrupt.h>
+#include <linux/ptrace.h>
+#include <linux/poll.h>
+
+#include <linux/slab.h>
+#include <linux/tty.h>
+#include <linux/errno.h>
+#include <linux/string.h>
+#include <linux/signal.h>
+#include <linux/ioctl.h>
+#include <linux/skbuff.h>
+
+#include <net/bluetooth/bluetooth.h>
+#include <net/bluetooth/hci_core.h>
+
+#include "hci_uart.h"
+
+/* HCILL commands */
+#define HCILL_GO_TO_SLEEP_IND  0x30
+#define HCILL_GO_TO_SLEEP_ACK  0x31
+#define HCILL_WAKE_UP_IND      0x32
+#define HCILL_WAKE_UP_ACK      0x33
+
+/* HCILL receiver States */
+#define HCILL_W4_PACKET_TYPE   0
+#define HCILL_W4_EVENT_HDR     1
+#define HCILL_W4_ACL_HDR       2
+#define HCILL_W4_SCO_HDR       3
+#define HCILL_W4_DATA          4
+
+/* HCILL states */
+enum hcill_states_e {
+       HCILL_ASLEEP,
+       HCILL_ASLEEP_TO_AWAKE,
+       HCILL_AWAKE,
+       HCILL_AWAKE_TO_ASLEEP
+};
+
+struct hcill_cmd {
+       u8 cmd;
+} __attribute__((packed));
+
+struct ll_struct {
+       unsigned long rx_state;
+       unsigned long rx_count;
+       struct sk_buff *rx_skb;
+       struct sk_buff_head txq;
+       spinlock_t hcill_lock;          /* HCILL state lock     */
+       unsigned long hcill_state;      /* HCILL power state    */
+       struct sk_buff_head tx_wait_q;  /* HCILL wait queue     */
+};
+
+/*
+ * Builds and sends an HCILL command packet.
+ * These are very simple packets with only 1 cmd byte
+ */
+static int send_hcill_cmd(u8 cmd, struct hci_uart *hu)
+{
+       int err = 0;
+       struct sk_buff *skb = NULL;
+       struct ll_struct *ll = hu->priv;
+       struct hcill_cmd *hcill_packet;
+
+       BT_DBG("hu %p cmd 0x%x", hu, cmd);
+
+       /* allocate packet */
+       skb = bt_skb_alloc(1, GFP_ATOMIC);
+       if (!skb) {
+               BT_ERR("cannot allocate memory for HCILL packet");
+               err = -ENOMEM;
+               goto out;
+       }
+
+       /* prepare packet */
+       hcill_packet = (struct hcill_cmd *) skb_put(skb, 1);
+       hcill_packet->cmd = cmd;
+       skb->dev = (void *) hu->hdev;
+
+       /* send packet */
+       skb_queue_tail(&ll->txq, skb);
+out:
+       return err;
+}
+
+/* Initialize protocol */
+static int ll_open(struct hci_uart *hu)
+{
+       struct ll_struct *ll;
+
+       BT_DBG("hu %p", hu);
+
+       ll = kzalloc(sizeof(*ll), GFP_ATOMIC);
+       if (!ll)
+               return -ENOMEM;
+
+       skb_queue_head_init(&ll->txq);
+       skb_queue_head_init(&ll->tx_wait_q);
+       spin_lock_init(&ll->hcill_lock);
+
+       ll->hcill_state = HCILL_AWAKE;
+
+       hu->priv = ll;
+
+       return 0;
+}
+
+/* Flush protocol data */
+static int ll_flush(struct hci_uart *hu)
+{
+       struct ll_struct *ll = hu->priv;
+
+       BT_DBG("hu %p", hu);
+
+       skb_queue_purge(&ll->tx_wait_q);
+       skb_queue_purge(&ll->txq);
+
+       return 0;
+}
+
+/* Close protocol */
+static int ll_close(struct hci_uart *hu)
+{
+       struct ll_struct *ll = hu->priv;
+
+       BT_DBG("hu %p", hu);
+
+       skb_queue_purge(&ll->tx_wait_q);
+       skb_queue_purge(&ll->txq);
+
+       if (ll->rx_skb)
+               kfree_skb(ll->rx_skb);
+
+       hu->priv = NULL;
+
+       kfree(ll);
+
+       return 0;
+}
+
+/*
+ * internal function, which does common work of the device wake up process:
+ * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
+ * 2. changes internal state to HCILL_AWAKE.
+ * Note: assumes that hcill_lock spinlock is taken,
+ * shouldn't be called otherwise!
+ */
+static void __ll_do_awake(struct ll_struct *ll)
+{
+       struct sk_buff *skb = NULL;
+
+       while ((skb = skb_dequeue(&ll->tx_wait_q)))
+               skb_queue_tail(&ll->txq, skb);
+
+       ll->hcill_state = HCILL_AWAKE;
+}
+
+/*
+ * Called upon a wake-up-indication from the device
+ */
+static void ll_device_want_to_wakeup(struct hci_uart *hu)
+{
+       unsigned long flags;
+       struct ll_struct *ll = hu->priv;
+
+       BT_DBG("hu %p", hu);
+
+       /* lock hcill state */
+       spin_lock_irqsave(&ll->hcill_lock, flags);
+
+       switch (ll->hcill_state) {
+       case HCILL_ASLEEP:
+               /* acknowledge device wake up */
+               if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) {
+                       BT_ERR("cannot acknowledge device wake up");
+                       goto out;
+               }
+               break;
+       case HCILL_ASLEEP_TO_AWAKE:
+               /*
+                * this state means that a wake-up-indication
+                * is already on its way to the device,
+                * and will serve as the required wake-up-ack
+                */
+               BT_DBG("dual wake-up-indication");
+               break;
+       default:
+               /* any other state are illegal */
+               BT_ERR("received HCILL_WAKE_UP_IND in state %ld", ll->hcill_state);
+               break;
+       }
+
+       /* send pending packets and change state to HCILL_AWAKE */
+       __ll_do_awake(ll);
+
+out:
+       spin_unlock_irqrestore(&ll->hcill_lock, flags);
+
+       /* actually send the packets */
+       hci_uart_tx_wakeup(hu);
+}
+
+/*
+ * Called upon a sleep-indication from the device
+ */
+static void ll_device_want_to_sleep(struct hci_uart *hu)
+{
+       unsigned long flags;
+       struct ll_struct *ll = hu->priv;
+
+       BT_DBG("hu %p", hu);
+
+       /* lock hcill state */
+       spin_lock_irqsave(&ll->hcill_lock, flags);
+
+       /* sanity check */
+       if (ll->hcill_state != HCILL_AWAKE)
+               BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld", ll->hcill_state);
+
+       /* acknowledge device sleep */
+       if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) {
+               BT_ERR("cannot acknowledge device sleep");
+               goto out;
+       }
+
+       /* update state */
+       ll->hcill_state = HCILL_ASLEEP;
+
+out:
+       spin_unlock_irqrestore(&ll->hcill_lock, flags);
+
+       /* actually send the sleep ack packet */
+       hci_uart_tx_wakeup(hu);
+}
+
+/*
+ * Called upon wake-up-acknowledgement from the device
+ */
+static void ll_device_woke_up(struct hci_uart *hu)
+{
+       unsigned long flags;
+       struct ll_struct *ll = hu->priv;
+
+       BT_DBG("hu %p", hu);
+
+       /* lock hcill state */
+       spin_lock_irqsave(&ll->hcill_lock, flags);
+
+       /* sanity check */
+       if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE)
+               BT_ERR("received HCILL_WAKE_UP_ACK in state %ld", ll->hcill_state);
+
+       /* send pending packets and change state to HCILL_AWAKE */
+       __ll_do_awake(ll);
+
+       spin_unlock_irqrestore(&ll->hcill_lock, flags);
+
+       /* actually send the packets */
+       hci_uart_tx_wakeup(hu);
+}
+
+/* Enqueue frame for transmittion (padding, crc, etc) */
+/* may be called from two simultaneous tasklets */
+static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb)
+{
+       unsigned long flags = 0;
+       struct ll_struct *ll = hu->priv;
+
+       BT_DBG("hu %p skb %p", hu, skb);
+
+       /* Prepend skb with frame type */
+       memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
+
+       /* lock hcill state */
+       spin_lock_irqsave(&ll->hcill_lock, flags);
+
+       /* act according to current state */
+       switch (ll->hcill_state) {
+       case HCILL_AWAKE:
+               BT_DBG("device awake, sending normally");
+               skb_queue_tail(&ll->txq, skb);
+               break;
+       case HCILL_ASLEEP:
+               BT_DBG("device asleep, waking up and queueing packet");
+               /* save packet for later */
+               skb_queue_tail(&ll->tx_wait_q, skb);
+               /* awake device */
+               if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) {
+                       BT_ERR("cannot wake up device");
+                       break;
+               }
+               ll->hcill_state = HCILL_ASLEEP_TO_AWAKE;
+               break;
+       case HCILL_ASLEEP_TO_AWAKE:
+               BT_DBG("device waking up, queueing packet");
+               /* transient state; just keep packet for later */
+               skb_queue_tail(&ll->tx_wait_q, skb);
+               break;
+       default:
+               BT_ERR("illegal hcill state: %ld (losing packet)", ll->hcill_state);
+               kfree_skb(skb);
+               break;
+       }
+
+       spin_unlock_irqrestore(&ll->hcill_lock, flags);
+
+       return 0;
+}
+
+static inline int ll_check_data_len(struct ll_struct *ll, int len)
+{
+       register int room = skb_tailroom(ll->rx_skb);
+
+       BT_DBG("len %d room %d", len, room);
+
+       if (!len) {
+               hci_recv_frame(ll->rx_skb);
+       } else if (len > room) {
+               BT_ERR("Data length is too large");
+               kfree_skb(ll->rx_skb);
+       } else {
+               ll->rx_state = HCILL_W4_DATA;
+               ll->rx_count = len;
+               return len;
+       }
+
+       ll->rx_state = HCILL_W4_PACKET_TYPE;
+       ll->rx_skb   = NULL;
+       ll->rx_count = 0;
+
+       return 0;
+}
+
+/* Recv data */
+static int ll_recv(struct hci_uart *hu, void *data, int count)
+{
+       struct ll_struct *ll = hu->priv;
+       register char *ptr;
+       struct hci_event_hdr *eh;
+       struct hci_acl_hdr   *ah;
+       struct hci_sco_hdr   *sh;
+       register int len, type, dlen;
+
+       BT_DBG("hu %p count %d rx_state %ld rx_count %ld", hu, count, ll->rx_state, ll->rx_count);
+
+       ptr = data;
+       while (count) {
+               if (ll->rx_count) {
+                       len = min_t(unsigned int, ll->rx_count, count);
+                       memcpy(skb_put(ll->rx_skb, len), ptr, len);
+                       ll->rx_count -= len; count -= len; ptr += len;
+
+                       if (ll->rx_count)
+                               continue;
+
+                       switch (ll->rx_state) {
+                       case HCILL_W4_DATA:
+                               BT_DBG("Complete data");
+                               hci_recv_frame(ll->rx_skb);
+
+                               ll->rx_state = HCILL_W4_PACKET_TYPE;
+                               ll->rx_skb = NULL;
+                               continue;
+
+                       case HCILL_W4_EVENT_HDR:
+                               eh = (struct hci_event_hdr *) ll->rx_skb->data;
+
+                               BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
+
+                               ll_check_data_len(ll, eh->plen);
+                               continue;
+
+                       case HCILL_W4_ACL_HDR:
+                               ah = (struct hci_acl_hdr *) ll->rx_skb->data;
+                               dlen = __le16_to_cpu(ah->dlen);
+
+                               BT_DBG("ACL header: dlen %d", dlen);
+
+                               ll_check_data_len(ll, dlen);
+                               continue;
+
+                       case HCILL_W4_SCO_HDR:
+                               sh = (struct hci_sco_hdr *) ll->rx_skb->data;
+
+                               BT_DBG("SCO header: dlen %d", sh->dlen);
+
+                               ll_check_data_len(ll, sh->dlen);
+                               continue;
+                       }
+               }
+
+               /* HCILL_W4_PACKET_TYPE */
+               switch (*ptr) {
+               case HCI_EVENT_PKT:
+                       BT_DBG("Event packet");
+                       ll->rx_state = HCILL_W4_EVENT_HDR;
+                       ll->rx_count = HCI_EVENT_HDR_SIZE;
+                       type = HCI_EVENT_PKT;
+                       break;
+
+               case HCI_ACLDATA_PKT:
+                       BT_DBG("ACL packet");
+                       ll->rx_state = HCILL_W4_ACL_HDR;
+                       ll->rx_count = HCI_ACL_HDR_SIZE;
+                       type = HCI_ACLDATA_PKT;
+                       break;
+
+               case HCI_SCODATA_PKT:
+                       BT_DBG("SCO packet");
+                       ll->rx_state = HCILL_W4_SCO_HDR;
+                       ll->rx_count = HCI_SCO_HDR_SIZE;
+                       type = HCI_SCODATA_PKT;
+                       break;
+
+               /* HCILL signals */
+               case HCILL_GO_TO_SLEEP_IND:
+                       BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
+                       ll_device_want_to_sleep(hu);
+                       ptr++; count--;
+                       continue;
+
+               case HCILL_GO_TO_SLEEP_ACK:
+                       /* shouldn't happen */
+                       BT_ERR("received HCILL_GO_TO_SLEEP_ACK (in state %ld)", ll->hcill_state);
+                       ptr++; count--;
+                       continue;
+
+               case HCILL_WAKE_UP_IND:
+                       BT_DBG("HCILL_WAKE_UP_IND packet");
+                       ll_device_want_to_wakeup(hu);
+                       ptr++; count--;
+                       continue;
+
+               case HCILL_WAKE_UP_ACK:
+                       BT_DBG("HCILL_WAKE_UP_ACK packet");
+                       ll_device_woke_up(hu);
+                       ptr++; count--;
+                       continue;
+
+               default:
+                       BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
+                       hu->hdev->stat.err_rx++;
+                       ptr++; count--;
+                       continue;
+               };
+
+               ptr++; count--;
+
+               /* Allocate packet */
+               ll->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
+               if (!ll->rx_skb) {
+                       BT_ERR("Can't allocate mem for new packet");
+                       ll->rx_state = HCILL_W4_PACKET_TYPE;
+                       ll->rx_count = 0;
+                       return 0;
+               }
+
+               ll->rx_skb->dev = (void *) hu->hdev;
+               bt_cb(ll->rx_skb)->pkt_type = type;
+       }
+
+       return count;
+}
+
+static struct sk_buff *ll_dequeue(struct hci_uart *hu)
+{
+       struct ll_struct *ll = hu->priv;
+       return skb_dequeue(&ll->txq);
+}
+
+static struct hci_uart_proto llp = {
+       .id             = HCI_UART_LL,
+       .open           = ll_open,
+       .close          = ll_close,
+       .recv           = ll_recv,
+       .enqueue        = ll_enqueue,
+       .dequeue        = ll_dequeue,
+       .flush          = ll_flush,
+};
+
+int ll_init(void)
+{
+       int err = hci_uart_register_proto(&llp);
+
+       if (!err)
+               BT_INFO("HCILL protocol initialized");
+       else
+               BT_ERR("HCILL protocol registration failed");
+
+       return err;
+}
+
+int ll_deinit(void)
+{
+       return hci_uart_unregister_proto(&llp);
+}
index 1097ce72393f2813bc82d140df1cca0495124b30..50113db06b9f54144e455193279deec4900028cc 100644 (file)
 #define HCIUARTGETDEVICE       _IOR('U', 202, int)
 
 /* UART protocols */
-#define HCI_UART_MAX_PROTO     4
+#define HCI_UART_MAX_PROTO     5
 
 #define HCI_UART_H4    0
 #define HCI_UART_BCSP  1
 #define HCI_UART_3WIRE 2
 #define HCI_UART_H4DS  3
+#define HCI_UART_LL    4
 
 struct hci_uart;
 
@@ -85,3 +86,8 @@ int h4_deinit(void);
 int bcsp_init(void);
 int bcsp_deinit(void);
 #endif
+
+#ifdef CONFIG_BT_HCIUART_LL
+int ll_init(void);
+int ll_deinit(void);
+#endif