2016-08-15 13:21:26 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Nordic Semiconductor ASA
|
|
|
|
* Copyright (c) 2016 Vinayak Kariappa Chettimada
|
|
|
|
*
|
2017-01-18 17:01:01 -08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-08-15 13:21:26 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stddef.h>
|
2016-09-14 20:30:50 +02:00
|
|
|
#include <string.h>
|
2016-08-15 13:21:26 +02:00
|
|
|
|
2016-11-07 11:36:56 +01:00
|
|
|
#include <zephyr.h>
|
2016-09-14 20:30:50 +02:00
|
|
|
#include <soc.h>
|
2016-08-15 13:21:26 +02:00
|
|
|
#include <init.h>
|
2016-09-14 20:30:50 +02:00
|
|
|
#include <device.h>
|
|
|
|
#include <clock_control.h>
|
|
|
|
|
2016-08-15 13:21:26 +02:00
|
|
|
#include <misc/util.h>
|
2016-09-01 21:34:56 +02:00
|
|
|
#include <misc/stack.h>
|
2016-08-15 13:21:26 +02:00
|
|
|
#include <misc/byteorder.h>
|
|
|
|
|
2017-01-17 11:01:47 +02:00
|
|
|
#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLUETOOTH_DEBUG_HCI_DRIVER)
|
2016-08-15 13:21:26 +02:00
|
|
|
#include <bluetooth/log.h>
|
2017-01-17 11:01:47 +02:00
|
|
|
#include <bluetooth/bluetooth.h>
|
2016-08-15 13:21:26 +02:00
|
|
|
#include <bluetooth/hci.h>
|
2016-11-02 13:07:55 +02:00
|
|
|
#include <drivers/bluetooth/hci_driver.h>
|
2016-08-15 13:21:26 +02:00
|
|
|
|
2016-11-05 05:13:51 +01:00
|
|
|
#ifdef CONFIG_CLOCK_CONTROL_NRF5
|
|
|
|
#include <drivers/clock_control/nrf5_clock_control.h>
|
|
|
|
#endif
|
|
|
|
|
2016-12-21 06:21:13 +01:00
|
|
|
#include "util/util.h"
|
2016-08-15 13:21:26 +02:00
|
|
|
#include "hal/ccm.h"
|
|
|
|
#include "hal/radio.h"
|
2017-03-14 14:48:54 +01:00
|
|
|
#include "ll_sw/pdu.h"
|
|
|
|
#include "ll_sw/ctrl.h"
|
|
|
|
#include "ll.h"
|
2016-09-15 10:07:56 +02:00
|
|
|
#include "hci_internal.h"
|
2016-08-15 13:21:26 +02:00
|
|
|
|
|
|
|
#include "hal/debug.h"
|
|
|
|
|
2017-01-02 18:59:22 +01:00
|
|
|
static K_SEM_DEFINE(sem_prio_recv, 0, UINT_MAX);
|
|
|
|
static K_FIFO_DEFINE(recv_fifo);
|
|
|
|
|
|
|
|
static BT_STACK_NOINIT(prio_recv_thread_stack,
|
|
|
|
CONFIG_BLUETOOTH_CONTROLLER_RX_PRIO_STACK_SIZE);
|
|
|
|
static BT_STACK_NOINIT(recv_thread_stack, CONFIG_BLUETOOTH_RX_STACK_SIZE);
|
2016-08-15 13:21:26 +02:00
|
|
|
|
2017-03-29 13:41:49 +03:00
|
|
|
#if defined(CONFIG_INIT_STACKS)
|
2017-04-20 12:00:29 -05:00
|
|
|
static u32_t prio_ts;
|
|
|
|
static u32_t rx_ts;
|
2017-03-29 13:41:49 +03:00
|
|
|
#endif
|
|
|
|
|
2017-01-02 18:59:22 +01:00
|
|
|
static void prio_recv_thread(void *p1, void *p2, void *p3)
|
2016-08-15 13:21:26 +02:00
|
|
|
{
|
|
|
|
while (1) {
|
2016-09-22 16:46:21 +02:00
|
|
|
struct radio_pdu_node_rx *node_rx;
|
|
|
|
struct net_buf *buf;
|
2017-04-20 12:00:29 -05:00
|
|
|
u8_t num_cmplt;
|
|
|
|
u16_t handle;
|
2016-08-15 13:21:26 +02:00
|
|
|
|
2016-09-22 16:46:21 +02:00
|
|
|
while ((num_cmplt = radio_rx_get(&node_rx, &handle))) {
|
2016-08-15 13:21:26 +02:00
|
|
|
|
2016-12-26 17:22:48 +02:00
|
|
|
buf = bt_buf_get_rx(K_FOREVER);
|
|
|
|
bt_buf_set_type(buf, BT_BUF_EVT);
|
|
|
|
hci_num_cmplt_encode(buf, handle, num_cmplt);
|
|
|
|
BT_DBG("Num Complete: 0x%04x:%u", handle, num_cmplt);
|
2017-01-02 13:43:12 +02:00
|
|
|
bt_recv_prio(buf);
|
2016-08-28 17:53:51 +02:00
|
|
|
|
2016-11-10 21:53:12 +02:00
|
|
|
k_yield();
|
2016-08-15 13:21:26 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 16:46:21 +02:00
|
|
|
if (node_rx) {
|
2016-08-15 13:21:26 +02:00
|
|
|
|
2017-01-02 18:59:22 +01:00
|
|
|
radio_rx_dequeue();
|
|
|
|
|
|
|
|
BT_DBG("RX node enqueue");
|
|
|
|
k_fifo_put(&recv_fifo, node_rx);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_DBG("sem take...");
|
|
|
|
k_sem_take(&sem_prio_recv, K_FOREVER);
|
|
|
|
BT_DBG("sem taken");
|
|
|
|
|
2017-03-29 13:41:49 +03:00
|
|
|
#if defined(CONFIG_INIT_STACKS)
|
|
|
|
if (k_uptime_get_32() - prio_ts > K_SECONDS(5)) {
|
|
|
|
stack_analyze("prio recv thread stack",
|
|
|
|
prio_recv_thread_stack,
|
|
|
|
sizeof(prio_recv_thread_stack));
|
|
|
|
prio_ts = k_uptime_get_32();
|
|
|
|
}
|
|
|
|
#endif
|
2017-01-02 18:59:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void recv_thread(void *p1, void *p2, void *p3)
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
struct radio_pdu_node_rx *node_rx;
|
|
|
|
struct pdu_data *pdu_data;
|
|
|
|
struct net_buf *buf;
|
|
|
|
|
|
|
|
BT_DBG("RX node get");
|
|
|
|
node_rx = k_fifo_get(&recv_fifo, K_FOREVER);
|
|
|
|
BT_DBG("RX node dequeued");
|
|
|
|
|
|
|
|
pdu_data = (void *)node_rx->pdu_data;
|
|
|
|
/* Check if we need to generate an HCI event or ACL
|
|
|
|
* data
|
|
|
|
*/
|
|
|
|
if (node_rx->hdr.type != NODE_RX_TYPE_DC_PDU ||
|
|
|
|
pdu_data->ll_id == PDU_DATA_LLID_CTRL) {
|
|
|
|
/* generate a (non-priority) HCI event */
|
|
|
|
if (hci_evt_is_discardable(node_rx)) {
|
|
|
|
buf = bt_buf_get_rx(K_NO_WAIT);
|
2016-09-22 16:46:21 +02:00
|
|
|
} else {
|
2016-12-26 17:22:48 +02:00
|
|
|
buf = bt_buf_get_rx(K_FOREVER);
|
2016-09-22 16:46:21 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 18:13:06 +02:00
|
|
|
if (buf) {
|
2017-01-02 18:59:22 +01:00
|
|
|
bt_buf_set_type(buf, BT_BUF_EVT);
|
|
|
|
hci_evt_encode(node_rx, buf);
|
2016-08-15 13:21:26 +02:00
|
|
|
}
|
2017-01-02 18:59:22 +01:00
|
|
|
} else {
|
|
|
|
/* generate ACL data */
|
|
|
|
buf = bt_buf_get_rx(K_FOREVER);
|
|
|
|
bt_buf_set_type(buf, BT_BUF_ACL_IN);
|
|
|
|
hci_acl_encode(node_rx, buf);
|
|
|
|
}
|
2016-08-15 13:21:26 +02:00
|
|
|
|
2017-01-02 18:59:22 +01:00
|
|
|
radio_rx_fc_set(node_rx->hdr.handle, 0);
|
|
|
|
node_rx->hdr.onion.next = 0;
|
|
|
|
radio_rx_mem_release(&node_rx);
|
2016-08-15 13:21:26 +02:00
|
|
|
|
2017-01-02 18:59:22 +01:00
|
|
|
if (buf) {
|
|
|
|
if (buf->len) {
|
|
|
|
BT_DBG("Packet in: type:%u len:%u",
|
|
|
|
bt_buf_get_type(buf), buf->len);
|
|
|
|
bt_recv(buf);
|
|
|
|
} else {
|
|
|
|
net_buf_unref(buf);
|
|
|
|
}
|
2016-08-28 17:53:51 +02:00
|
|
|
}
|
2016-09-01 21:34:56 +02:00
|
|
|
|
2017-01-02 18:59:22 +01:00
|
|
|
k_yield();
|
|
|
|
|
2017-03-29 13:41:49 +03:00
|
|
|
#if defined(CONFIG_INIT_STACKS)
|
|
|
|
if (k_uptime_get_32() - rx_ts > K_SECONDS(5)) {
|
|
|
|
stack_analyze("recv thread stack", recv_thread_stack,
|
|
|
|
sizeof(recv_thread_stack));
|
|
|
|
rx_ts = k_uptime_get_32();
|
|
|
|
}
|
|
|
|
#endif
|
2016-08-15 13:21:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 18:09:16 +02:00
|
|
|
static int cmd_handle(struct net_buf *buf)
|
|
|
|
{
|
|
|
|
struct net_buf *evt;
|
|
|
|
|
2017-02-01 17:32:29 +02:00
|
|
|
evt = hci_cmd_handle(buf);
|
|
|
|
if (!evt) {
|
|
|
|
return -EINVAL;
|
2016-09-21 18:09:16 +02:00
|
|
|
}
|
|
|
|
|
2017-02-01 17:32:29 +02:00
|
|
|
BT_DBG("Replying with event of %u bytes", evt->len);
|
|
|
|
bt_recv_prio(evt);
|
|
|
|
|
|
|
|
return 0;
|
2016-09-21 18:09:16 +02:00
|
|
|
}
|
|
|
|
|
2016-09-16 16:16:23 +02:00
|
|
|
static int hci_driver_send(struct net_buf *buf)
|
2016-08-15 13:21:26 +02:00
|
|
|
{
|
2017-04-20 12:00:29 -05:00
|
|
|
u8_t type;
|
2016-09-20 12:36:44 +02:00
|
|
|
int err;
|
2016-08-15 13:21:26 +02:00
|
|
|
|
|
|
|
BT_DBG("enter");
|
|
|
|
|
2016-09-21 18:09:16 +02:00
|
|
|
if (!buf->len) {
|
|
|
|
BT_ERR("Empty HCI packet");
|
|
|
|
return -EINVAL;
|
2016-08-15 13:21:26 +02:00
|
|
|
}
|
|
|
|
|
2016-09-21 18:09:16 +02:00
|
|
|
type = bt_buf_get_type(buf);
|
|
|
|
switch (type) {
|
|
|
|
case BT_BUF_ACL_OUT:
|
|
|
|
err = hci_acl_handle(buf);
|
|
|
|
break;
|
|
|
|
case BT_BUF_CMD:
|
|
|
|
err = cmd_handle(buf);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BT_ERR("Unknown HCI type %u", type);
|
|
|
|
return -EINVAL;
|
2016-08-15 13:21:26 +02:00
|
|
|
}
|
|
|
|
|
2016-09-21 18:09:16 +02:00
|
|
|
if (!err) {
|
|
|
|
net_buf_unref(buf);
|
|
|
|
}
|
2016-08-15 13:21:26 +02:00
|
|
|
|
2016-09-21 18:09:16 +02:00
|
|
|
BT_DBG("exit: %d", err);
|
2016-08-15 13:21:26 +02:00
|
|
|
|
2016-09-21 18:09:16 +02:00
|
|
|
return err;
|
2016-08-15 13:21:26 +02:00
|
|
|
}
|
|
|
|
|
2016-09-16 16:16:23 +02:00
|
|
|
static int hci_driver_open(void)
|
2016-08-15 13:21:26 +02:00
|
|
|
{
|
2017-04-20 12:00:29 -05:00
|
|
|
u32_t err;
|
2016-08-15 13:21:26 +02:00
|
|
|
|
2016-09-09 18:16:26 +02:00
|
|
|
DEBUG_INIT();
|
|
|
|
|
2017-03-14 17:09:36 +01:00
|
|
|
err = ll_init(&sem_prio_recv);
|
2016-08-15 13:21:26 +02:00
|
|
|
|
2016-09-22 16:46:21 +02:00
|
|
|
if (err) {
|
2017-03-14 17:09:36 +01:00
|
|
|
BT_ERR("LL initialization failed: %u", err);
|
|
|
|
return err;
|
2016-08-15 13:21:26 +02:00
|
|
|
}
|
|
|
|
|
2017-01-02 18:59:22 +01:00
|
|
|
k_thread_spawn(prio_recv_thread_stack, sizeof(prio_recv_thread_stack),
|
|
|
|
prio_recv_thread, NULL, NULL, NULL, K_PRIO_COOP(6), 0,
|
|
|
|
K_NO_WAIT);
|
|
|
|
|
2016-11-11 10:02:49 +02:00
|
|
|
k_thread_spawn(recv_thread_stack, sizeof(recv_thread_stack),
|
|
|
|
recv_thread, NULL, NULL, NULL, K_PRIO_COOP(7), 0,
|
|
|
|
K_NO_WAIT);
|
2016-08-15 13:21:26 +02:00
|
|
|
|
|
|
|
BT_DBG("Success.");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-14 15:00:15 +02:00
|
|
|
static const struct bt_hci_driver drv = {
|
2016-08-15 13:21:26 +02:00
|
|
|
.name = "Controller",
|
2016-10-27 16:55:01 +03:00
|
|
|
.bus = BT_HCI_DRIVER_BUS_VIRTUAL,
|
2016-09-16 16:16:23 +02:00
|
|
|
.open = hci_driver_open,
|
|
|
|
.send = hci_driver_send,
|
2016-08-15 13:21:26 +02:00
|
|
|
};
|
|
|
|
|
2016-09-16 16:16:23 +02:00
|
|
|
static int _hci_driver_init(struct device *unused)
|
2016-08-15 13:21:26 +02:00
|
|
|
{
|
|
|
|
ARG_UNUSED(unused);
|
|
|
|
|
2016-10-27 16:55:01 +03:00
|
|
|
bt_hci_driver_register(&drv);
|
2016-08-15 13:21:26 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-11 10:02:49 +02:00
|
|
|
SYS_INIT(_hci_driver_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
|