From cca221e92868cd6137e51bd7aa1e35f0bba90e72 Mon Sep 17 00:00:00 2001 From: Yuriy Vynnychek Date: Mon, 23 May 2022 14:50:52 +0300 Subject: [PATCH] drivers: bluetooth: hci: introduce new Telink B91 HCI driver Telink B91 Bluetooth HCI driver basic support. Signed-off-by: Yuriy Vynnychek --- drivers/bluetooth/hci/CMakeLists.txt | 1 + drivers/bluetooth/hci/Kconfig | 5 + drivers/bluetooth/hci/hci_b91.c | 253 +++++++++++++++++++++++++++ subsys/bluetooth/host/Kconfig | 2 + 4 files changed, 261 insertions(+) create mode 100644 drivers/bluetooth/hci/hci_b91.c diff --git a/drivers/bluetooth/hci/CMakeLists.txt b/drivers/bluetooth/hci/CMakeLists.txt index a24128b624b..1e1f61ac64e 100644 --- a/drivers/bluetooth/hci/CMakeLists.txt +++ b/drivers/bluetooth/hci/CMakeLists.txt @@ -1,5 +1,6 @@ # SPDX-License-Identifier: Apache-2.0 +zephyr_library_sources_ifdef(CONFIG_BT_B91 hci_b91.c) zephyr_library_sources_ifdef(CONFIG_BT_ESP32 hci_esp32.c) zephyr_library_sources_ifdef(CONFIG_BT_H4 h4.c) zephyr_library_sources_ifdef(CONFIG_BT_H5 h5.c) diff --git a/drivers/bluetooth/hci/Kconfig b/drivers/bluetooth/hci/Kconfig index e52fcfc7029..f8430b8dccd 100644 --- a/drivers/bluetooth/hci/Kconfig +++ b/drivers/bluetooth/hci/Kconfig @@ -70,6 +70,11 @@ config BT_ESP32 help Espressif HCI bluetooth interface +config BT_B91 + bool "Telink B91 HCI driver" + help + Telink B91 HCI bluetooth interface + config BT_NO_DRIVER bool "No default HCI driver" help diff --git a/drivers/bluetooth/hci/hci_b91.c b/drivers/bluetooth/hci/hci_b91.c new file mode 100644 index 00000000000..03128c1b0db --- /dev/null +++ b/drivers/bluetooth/hci/hci_b91.c @@ -0,0 +1,253 @@ +/* + * Copyright (c) 2022, Telink Semiconductor (Shanghai) Co., Ltd. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_HCI_DRIVER) +#define LOG_MODULE_NAME bt_hci_driver_b91 +#include "common/log.h" + +#include +#include + +#include + +#define HCI_CMD 0x01 +#define HCI_ACL 0x02 +#define HCI_EVT 0x04 + +#define HCI_BT_B91_TIMEOUT K_MSEC(2000) + +static K_SEM_DEFINE(hci_send_sem, 1, 1); + +static bool is_hci_event_discardable(const uint8_t *evt_data) +{ + uint8_t evt_type = evt_data[0]; + + switch (evt_type) { +#if defined(CONFIG_BT_BREDR) + case BT_HCI_EVT_INQUIRY_RESULT_WITH_RSSI: + case BT_HCI_EVT_EXTENDED_INQUIRY_RESULT: + return true; +#endif + case BT_HCI_EVT_LE_META_EVENT: { + uint8_t subevt_type = evt_data[sizeof(struct bt_hci_evt_hdr)]; + + switch (subevt_type) { + case BT_HCI_EVT_LE_ADVERTISING_REPORT: + return true; + default: + return false; + } + } + default: + return false; + } +} + +static struct net_buf *bt_b91_evt_recv(uint8_t *data, size_t len) +{ + bool discardable; + struct bt_hci_evt_hdr hdr; + struct net_buf *buf; + size_t buf_tailroom; + + if (len < sizeof(hdr)) { + BT_ERR("Not enough data for event header"); + return NULL; + } + + discardable = is_hci_event_discardable(data); + + memcpy((void *)&hdr, data, sizeof(hdr)); + data += sizeof(hdr); + len -= sizeof(hdr); + + if (len != hdr.len) { + BT_ERR("Event payload length is not correct"); + return NULL; + } + BT_DBG("len %u", hdr.len); + + buf = bt_buf_get_evt(hdr.evt, discardable, K_NO_WAIT); + if (!buf) { + if (discardable) { + BT_DBG("Discardable buffer pool full, ignoring event"); + } else { + BT_ERR("No available event buffers!"); + } + return buf; + } + + net_buf_add_mem(buf, &hdr, sizeof(hdr)); + + buf_tailroom = net_buf_tailroom(buf); + if (buf_tailroom < len) { + BT_ERR("Not enough space in buffer %zu/%zu", + len, buf_tailroom); + net_buf_unref(buf); + return NULL; + } + + net_buf_add_mem(buf, data, len); + + return buf; +} + +static struct net_buf *bt_b91_acl_recv(uint8_t *data, size_t len) +{ + struct bt_hci_acl_hdr hdr; + struct net_buf *buf; + size_t buf_tailroom; + + if (len < sizeof(hdr)) { + BT_ERR("Not enough data for ACL header"); + return NULL; + } + + buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_NO_WAIT); + if (buf) { + memcpy((void *)&hdr, data, sizeof(hdr)); + data += sizeof(hdr); + len -= sizeof(hdr); + } else { + BT_ERR("No available ACL buffers!"); + return NULL; + } + + if (len != sys_le16_to_cpu(hdr.len)) { + BT_ERR("ACL payload length is not correct"); + net_buf_unref(buf); + return NULL; + } + + net_buf_add_mem(buf, &hdr, sizeof(hdr)); + buf_tailroom = net_buf_tailroom(buf); + if (buf_tailroom < len) { + BT_ERR("Not enough space in buffer %zu/%zu", + len, buf_tailroom); + net_buf_unref(buf); + return NULL; + } + + BT_DBG("len %u", len); + net_buf_add_mem(buf, data, len); + + return buf; +} + +static void hci_b91_host_rcv_pkt(uint8_t *data, uint16_t len) +{ + uint8_t pkt_indicator; + struct net_buf *buf; + + BT_HEXDUMP_DBG(data, len, "host packet data:"); + + pkt_indicator = *data++; + len -= sizeof(pkt_indicator); + + switch (pkt_indicator) { + case HCI_EVT: + buf = bt_b91_evt_recv(data, len); + break; + + case HCI_ACL: + buf = bt_b91_acl_recv(data, len); + break; + + default: + buf = NULL; + BT_ERR("Unknown HCI type %u", pkt_indicator); + } + + if (buf) { + BT_DBG("Calling bt_recv(%p)", buf); + bt_recv(buf); + } +} + +static void hci_b91_controller_rcv_pkt_ready(void) +{ + k_sem_give(&hci_send_sem); +} + +static b91_bt_host_callback_t vhci_host_cb = { + .host_send_available = hci_b91_controller_rcv_pkt_ready, + .host_read_packet = hci_b91_host_rcv_pkt +}; + +static int bt_b91_send(struct net_buf *buf) +{ + int err = 0; + uint8_t type; + + BT_DBG("buf %p type %u len %u", buf, bt_buf_get_type(buf), buf->len); + + switch (bt_buf_get_type(buf)) { + case BT_BUF_ACL_OUT: + type = HCI_ACL; + break; + + case BT_BUF_CMD: + type = HCI_CMD; + break; + + default: + BT_ERR("Unknown type %u", bt_buf_get_type(buf)); + goto done; + } + + BT_HEXDUMP_DBG(buf->data, buf->len, "Final HCI buffer:"); + + if (k_sem_take(&hci_send_sem, HCI_BT_B91_TIMEOUT) == 0) { + b91_bt_host_send_packet(type, buf->data, buf->len); + } else { + BT_ERR("Send packet timeout error"); + err = -ETIMEDOUT; + } + +done: + net_buf_unref(buf); + k_sem_give(&hci_send_sem); + + return err; +} + +static int hci_b91_open(void) +{ + int status; + + status = b91_bt_controller_init(); + if (status) { + BT_ERR("Bluetooth controller init failed %d", status); + return status; + } + + b91_bt_host_callback_register(&vhci_host_cb); + + BT_DBG("B91 BT started"); + + return 0; +} + +static const struct bt_hci_driver drv = { + .name = "BT B91", + .open = hci_b91_open, + .send = bt_b91_send, + .bus = BT_HCI_DRIVER_BUS_IPM, +#if defined(CONFIG_BT_DRIVER_QUIRK_NO_AUTO_DLE) + .quirks = BT_QUIRK_NO_AUTO_DLE, +#endif +}; + +static int bt_b91_init(const struct device *unused) +{ + ARG_UNUSED(unused); + + bt_hci_driver_register(&drv); + + return 0; +} + +SYS_INIT(bt_b91_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE); diff --git a/subsys/bluetooth/host/Kconfig b/subsys/bluetooth/host/Kconfig index 6eac61708cc..38854156f61 100644 --- a/subsys/bluetooth/host/Kconfig +++ b/subsys/bluetooth/host/Kconfig @@ -25,6 +25,7 @@ config BT_HCI_TX_STACK_SIZE default 768 if BT_CTLR && BT_LL_SW_SPLIT default 512 if BT_USERCHAN default 640 if BT_STM32_IPM + default 1024 if BT_B91 # Even if no driver is selected the following default is still # needed e.g. for unit tests. This default will also server as # the worst-case stack size if an out-of-tree controller is used. @@ -61,6 +62,7 @@ config BT_HCI_RESERVE default 1 if BT_STM32_IPM default 1 if BT_USERCHAN default 1 if BT_ESP32 + default 0 if BT_B91 # Even if no driver is selected the following default is still # needed e.g. for unit tests. default 0