2015-12-07 18:58:52 +01:00
|
|
|
/* h4.c - H:4 UART based Bluetooth driver */
|
2015-04-13 13:38:44 +02:00
|
|
|
|
|
|
|
/*
|
2016-06-10 11:10:18 +02:00
|
|
|
* Copyright (c) 2015-2016 Intel Corporation
|
2015-04-13 13:38:44 +02:00
|
|
|
*
|
2017-01-19 02:01:01 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-04-13 13:38:44 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 09:58:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/arch/cpu.h>
|
2015-04-13 13:38:44 +02:00
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/init.h>
|
|
|
|
#include <zephyr/drivers/uart.h>
|
|
|
|
#include <zephyr/sys/util.h>
|
|
|
|
#include <zephyr/sys/byteorder.h>
|
2015-04-29 12:36:41 +02:00
|
|
|
#include <string.h>
|
2015-04-13 13:38:44 +02:00
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/bluetooth/bluetooth.h>
|
|
|
|
#include <zephyr/bluetooth/hci.h>
|
|
|
|
#include <zephyr/drivers/bluetooth/hci_driver.h>
|
2015-04-13 13:38:44 +02:00
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
#define LOG_LEVEL CONFIG_BT_HCI_DRIVER_LOG_LEVEL
|
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(bt_driver);
|
|
|
|
|
2022-10-25 08:48:54 +02:00
|
|
|
#include "common/bt_str.h"
|
2017-05-10 16:27:16 +02:00
|
|
|
|
2016-10-27 15:55:01 +02:00
|
|
|
#include "../util.h"
|
2016-05-04 14:12:16 +02:00
|
|
|
|
2023-02-22 08:45:15 +01:00
|
|
|
static K_KERNEL_STACK_DEFINE(rx_thread_stack, CONFIG_BT_DRV_RX_STACK_SIZE);
|
2017-05-09 21:00:09 +02:00
|
|
|
static struct k_thread rx_thread_data;
|
2016-12-25 14:21:39 +01:00
|
|
|
|
|
|
|
static struct {
|
|
|
|
struct net_buf *buf;
|
2016-12-31 13:41:45 +01:00
|
|
|
struct k_fifo fifo;
|
2016-12-25 14:21:39 +01:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
uint16_t remaining;
|
|
|
|
uint16_t discard;
|
2016-12-25 14:21:39 +01:00
|
|
|
|
|
|
|
bool have_hdr;
|
2017-01-31 10:24:37 +01:00
|
|
|
bool discardable;
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t hdr_len;
|
2016-12-25 14:21:39 +01:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t type;
|
2016-12-25 14:21:39 +01:00
|
|
|
union {
|
|
|
|
struct bt_hci_evt_hdr evt;
|
|
|
|
struct bt_hci_acl_hdr acl;
|
2020-05-12 22:26:26 +02:00
|
|
|
struct bt_hci_iso_hdr iso;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t hdr[4];
|
2016-12-25 14:21:39 +01:00
|
|
|
};
|
|
|
|
} rx = {
|
2019-03-12 22:15:42 +01:00
|
|
|
.fifo = Z_FIFO_INITIALIZER(rx.fifo),
|
2016-12-25 14:21:39 +01:00
|
|
|
};
|
2015-04-13 13:38:44 +02:00
|
|
|
|
2016-12-31 18:45:00 +01:00
|
|
|
static struct {
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t type;
|
2016-12-31 18:45:00 +01:00
|
|
|
struct net_buf *buf;
|
|
|
|
struct k_fifo fifo;
|
|
|
|
} tx = {
|
2019-03-12 22:15:42 +01:00
|
|
|
.fifo = Z_FIFO_INITIALIZER(tx.fifo),
|
2016-12-31 18:45:00 +01:00
|
|
|
};
|
|
|
|
|
2022-08-17 12:45:32 +02:00
|
|
|
static const struct device *const h4_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_bt_uart));
|
2015-12-01 17:42:20 +01:00
|
|
|
|
2016-12-25 14:21:39 +01:00
|
|
|
static inline void h4_get_type(void)
|
2015-04-13 13:38:44 +02:00
|
|
|
{
|
2016-12-25 14:21:39 +01:00
|
|
|
/* Get packet type */
|
|
|
|
if (uart_fifo_read(h4_dev, &rx.type, 1) != 1) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_WRN("Unable to read H:4 packet type");
|
2024-01-09 19:21:42 +01:00
|
|
|
rx.type = BT_HCI_H4_NONE;
|
2016-12-25 14:21:39 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-04-13 13:38:44 +02:00
|
|
|
|
2016-12-25 14:21:39 +01:00
|
|
|
switch (rx.type) {
|
2024-01-09 19:21:42 +01:00
|
|
|
case BT_HCI_H4_EVT:
|
2016-12-25 14:21:39 +01:00
|
|
|
rx.remaining = sizeof(rx.evt);
|
2017-01-31 10:24:37 +01:00
|
|
|
rx.hdr_len = rx.remaining;
|
2016-12-25 14:21:39 +01:00
|
|
|
break;
|
2024-01-09 19:21:42 +01:00
|
|
|
case BT_HCI_H4_ACL:
|
2016-12-25 14:21:39 +01:00
|
|
|
rx.remaining = sizeof(rx.acl);
|
2017-01-31 10:24:37 +01:00
|
|
|
rx.hdr_len = rx.remaining;
|
2016-12-25 14:21:39 +01:00
|
|
|
break;
|
2024-01-09 19:21:42 +01:00
|
|
|
case BT_HCI_H4_ISO:
|
2020-05-12 22:26:26 +02:00
|
|
|
if (IS_ENABLED(CONFIG_BT_ISO)) {
|
|
|
|
rx.remaining = sizeof(rx.iso);
|
|
|
|
rx.hdr_len = rx.remaining;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
__fallthrough;
|
2016-12-25 14:21:39 +01:00
|
|
|
default:
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Unknown H:4 type 0x%02x", rx.type);
|
2024-01-09 19:21:42 +01:00
|
|
|
rx.type = BT_HCI_H4_NONE;
|
2016-12-25 14:21:39 +01:00
|
|
|
}
|
|
|
|
}
|
2015-04-13 13:38:44 +02:00
|
|
|
|
2021-10-28 18:19:01 +02:00
|
|
|
static void h4_read_hdr(void)
|
|
|
|
{
|
|
|
|
int bytes_read = rx.hdr_len - rx.remaining;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = uart_fifo_read(h4_dev, rx.hdr + bytes_read, rx.remaining);
|
|
|
|
if (unlikely(ret < 0)) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Unable to read from UART (ret %d)", ret);
|
2021-10-28 18:19:01 +02:00
|
|
|
} else {
|
|
|
|
rx.remaining -= ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-25 14:21:39 +01:00
|
|
|
static inline void get_acl_hdr(void)
|
|
|
|
{
|
2021-10-28 18:19:01 +02:00
|
|
|
h4_read_hdr();
|
2016-12-25 14:21:39 +01:00
|
|
|
|
|
|
|
if (!rx.remaining) {
|
2021-10-28 18:19:01 +02:00
|
|
|
struct bt_hci_acl_hdr *hdr = &rx.acl;
|
|
|
|
|
2016-12-25 14:21:39 +01:00
|
|
|
rx.remaining = sys_le16_to_cpu(hdr->len);
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Got ACL header. Payload %u bytes", rx.remaining);
|
2016-12-25 14:21:39 +01:00
|
|
|
rx.have_hdr = true;
|
2015-04-13 13:38:44 +02:00
|
|
|
}
|
2016-12-25 14:21:39 +01:00
|
|
|
}
|
2015-04-13 13:38:44 +02:00
|
|
|
|
2020-05-12 22:26:26 +02:00
|
|
|
static inline void get_iso_hdr(void)
|
|
|
|
{
|
2021-10-28 18:19:01 +02:00
|
|
|
h4_read_hdr();
|
2020-05-12 22:26:26 +02:00
|
|
|
|
|
|
|
if (!rx.remaining) {
|
2021-10-28 18:19:01 +02:00
|
|
|
struct bt_hci_iso_hdr *hdr = &rx.iso;
|
|
|
|
|
2021-11-10 14:45:23 +01:00
|
|
|
rx.remaining = bt_iso_hdr_len(sys_le16_to_cpu(hdr->len));
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Got ISO header. Payload %u bytes", rx.remaining);
|
2020-05-12 22:26:26 +02:00
|
|
|
rx.have_hdr = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-25 14:21:39 +01:00
|
|
|
static inline void get_evt_hdr(void)
|
|
|
|
{
|
|
|
|
struct bt_hci_evt_hdr *hdr = &rx.evt;
|
|
|
|
|
2021-10-28 18:19:01 +02:00
|
|
|
h4_read_hdr();
|
|
|
|
|
2017-01-31 10:24:37 +01:00
|
|
|
if (rx.hdr_len == sizeof(*hdr) && rx.remaining < sizeof(*hdr)) {
|
|
|
|
switch (rx.evt.evt) {
|
|
|
|
case BT_HCI_EVT_LE_META_EVENT:
|
|
|
|
rx.remaining++;
|
|
|
|
rx.hdr_len++;
|
|
|
|
break;
|
2024-03-01 11:45:44 +01:00
|
|
|
#if defined(CONFIG_BT_CLASSIC)
|
2017-01-31 10:24:37 +01:00
|
|
|
case BT_HCI_EVT_INQUIRY_RESULT_WITH_RSSI:
|
|
|
|
case BT_HCI_EVT_EXTENDED_INQUIRY_RESULT:
|
|
|
|
rx.discardable = true;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-25 14:21:39 +01:00
|
|
|
if (!rx.remaining) {
|
2017-01-31 10:24:37 +01:00
|
|
|
if (rx.evt.evt == BT_HCI_EVT_LE_META_EVENT &&
|
2021-12-20 16:33:49 +01:00
|
|
|
(rx.hdr[sizeof(*hdr)] == BT_HCI_EVT_LE_ADVERTISING_REPORT)) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Marking adv report as discardable");
|
2017-01-31 10:24:37 +01:00
|
|
|
rx.discardable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
rx.remaining = hdr->len - (rx.hdr_len - sizeof(*hdr));
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Got event header. Payload %u bytes", hdr->len);
|
2016-12-25 14:21:39 +01:00
|
|
|
rx.have_hdr = true;
|
|
|
|
}
|
2015-04-13 13:38:44 +02:00
|
|
|
}
|
|
|
|
|
2016-12-25 14:21:39 +01:00
|
|
|
|
|
|
|
static inline void copy_hdr(struct net_buf *buf)
|
|
|
|
{
|
2017-01-31 10:24:37 +01:00
|
|
|
net_buf_add_mem(buf, rx.hdr, rx.hdr_len);
|
2015-04-29 12:36:41 +02:00
|
|
|
}
|
2015-04-13 13:38:44 +02:00
|
|
|
|
2017-02-01 16:37:14 +01:00
|
|
|
static void reset_rx(void)
|
|
|
|
{
|
2024-01-09 19:21:42 +01:00
|
|
|
rx.type = BT_HCI_H4_NONE;
|
2018-11-29 20:12:22 +01:00
|
|
|
rx.remaining = 0U;
|
2017-02-01 16:37:14 +01:00
|
|
|
rx.have_hdr = false;
|
2018-11-29 20:12:22 +01:00
|
|
|
rx.hdr_len = 0U;
|
2017-02-01 16:37:14 +01:00
|
|
|
rx.discardable = false;
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:56:14 +02:00
|
|
|
static struct net_buf *get_rx(k_timeout_t timeout)
|
2017-02-01 16:37:14 +01:00
|
|
|
{
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("type 0x%02x, evt 0x%02x", rx.type, rx.evt.evt);
|
2017-02-01 16:37:14 +01:00
|
|
|
|
2020-05-12 22:26:26 +02:00
|
|
|
switch (rx.type) {
|
2024-01-09 19:21:42 +01:00
|
|
|
case BT_HCI_H4_EVT:
|
2019-06-28 11:09:36 +02:00
|
|
|
return bt_buf_get_evt(rx.evt.evt, rx.discardable, timeout);
|
2024-01-09 19:21:42 +01:00
|
|
|
case BT_HCI_H4_ACL:
|
2020-05-12 22:26:26 +02:00
|
|
|
return bt_buf_get_rx(BT_BUF_ACL_IN, timeout);
|
2024-01-09 19:21:42 +01:00
|
|
|
case BT_HCI_H4_ISO:
|
2020-05-12 22:26:26 +02:00
|
|
|
if (IS_ENABLED(CONFIG_BT_ISO)) {
|
|
|
|
return bt_buf_get_rx(BT_BUF_ISO_IN, timeout);
|
|
|
|
}
|
2017-02-01 16:37:14 +01:00
|
|
|
}
|
|
|
|
|
2020-05-12 22:26:26 +02:00
|
|
|
return NULL;
|
2017-02-01 16:37:14 +01:00
|
|
|
}
|
|
|
|
|
2016-12-25 14:21:39 +01:00
|
|
|
static void rx_thread(void *p1, void *p2, void *p3)
|
2015-04-29 12:36:41 +02:00
|
|
|
{
|
2015-10-28 09:26:44 +01:00
|
|
|
struct net_buf *buf;
|
2015-04-29 12:36:41 +02:00
|
|
|
|
2016-12-25 14:21:39 +01:00
|
|
|
ARG_UNUSED(p1);
|
|
|
|
ARG_UNUSED(p2);
|
|
|
|
ARG_UNUSED(p3);
|
2015-04-13 13:38:44 +02:00
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("started");
|
2015-04-29 12:36:41 +02:00
|
|
|
|
2016-12-25 14:21:39 +01:00
|
|
|
while (1) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("rx.buf %p", rx.buf);
|
2016-12-25 14:21:39 +01:00
|
|
|
|
2017-02-01 16:37:14 +01:00
|
|
|
/* We can only do the allocation if we know the initial
|
|
|
|
* header, since Command Complete/Status events must use the
|
|
|
|
* original command buffer (if available).
|
|
|
|
*/
|
|
|
|
if (rx.have_hdr && !rx.buf) {
|
|
|
|
rx.buf = get_rx(K_FOREVER);
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Got rx.buf %p", rx.buf);
|
2016-12-25 14:21:39 +01:00
|
|
|
if (rx.remaining > net_buf_tailroom(rx.buf)) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Not enough space in buffer");
|
2016-12-25 14:21:39 +01:00
|
|
|
rx.discard = rx.remaining;
|
2017-02-01 16:37:14 +01:00
|
|
|
reset_rx();
|
|
|
|
} else {
|
2016-12-25 14:21:39 +01:00
|
|
|
copy_hdr(rx.buf);
|
|
|
|
}
|
|
|
|
}
|
2016-12-31 13:41:45 +01:00
|
|
|
|
|
|
|
/* Let the ISR continue receiving new packets */
|
|
|
|
uart_irq_rx_enable(h4_dev);
|
|
|
|
|
|
|
|
buf = net_buf_get(&rx.fifo, K_FOREVER);
|
|
|
|
do {
|
|
|
|
uart_irq_rx_enable(h4_dev);
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Calling bt_recv(%p)", buf);
|
2016-12-31 13:41:45 +01:00
|
|
|
bt_recv(buf);
|
|
|
|
|
|
|
|
/* Give other threads a chance to run if the ISR
|
|
|
|
* is receiving data so fast that rx.fifo never
|
|
|
|
* or very rarely goes empty.
|
|
|
|
*/
|
|
|
|
k_yield();
|
|
|
|
|
|
|
|
uart_irq_rx_disable(h4_dev);
|
|
|
|
buf = net_buf_get(&rx.fifo, K_NO_WAIT);
|
|
|
|
} while (buf);
|
2015-05-05 09:50:14 +02:00
|
|
|
}
|
2016-12-25 14:21:39 +01:00
|
|
|
}
|
2015-04-13 13:38:44 +02:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static size_t h4_discard(const struct device *uart, size_t len)
|
2016-12-25 14:21:39 +01:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t buf[33];
|
2021-10-28 18:19:01 +02:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = uart_fifo_read(uart, buf, MIN(len, sizeof(buf)));
|
|
|
|
if (unlikely(err < 0)) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Unable to read from UART (err %d)", err);
|
2021-10-28 18:19:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2015-04-29 12:36:41 +02:00
|
|
|
|
2021-10-28 18:19:01 +02:00
|
|
|
return err;
|
2015-04-13 13:38:44 +02:00
|
|
|
}
|
|
|
|
|
2016-12-31 18:45:00 +01:00
|
|
|
static inline void read_payload(void)
|
2015-04-13 13:38:44 +02:00
|
|
|
{
|
2016-12-31 13:41:45 +01:00
|
|
|
struct net_buf *buf;
|
2016-12-25 14:21:39 +01:00
|
|
|
int read;
|
|
|
|
|
|
|
|
if (!rx.buf) {
|
2021-12-08 18:01:20 +01:00
|
|
|
size_t buf_tailroom;
|
|
|
|
|
2017-02-01 16:37:14 +01:00
|
|
|
rx.buf = get_rx(K_NO_WAIT);
|
2016-12-25 14:21:39 +01:00
|
|
|
if (!rx.buf) {
|
2017-01-31 10:24:37 +01:00
|
|
|
if (rx.discardable) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_WRN("Discarding event 0x%02x", rx.evt.evt);
|
2017-01-31 10:24:37 +01:00
|
|
|
rx.discard = rx.remaining;
|
|
|
|
reset_rx();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_WRN("Failed to allocate, deferring to rx_thread");
|
2016-12-25 14:21:39 +01:00
|
|
|
uart_irq_rx_disable(h4_dev);
|
2016-12-31 18:45:00 +01:00
|
|
|
return;
|
2016-12-25 14:21:39 +01:00
|
|
|
}
|
2015-04-13 13:38:44 +02:00
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Allocated rx.buf %p", rx.buf);
|
2016-12-31 13:41:45 +01:00
|
|
|
|
2021-12-08 18:01:20 +01:00
|
|
|
buf_tailroom = net_buf_tailroom(rx.buf);
|
|
|
|
if (buf_tailroom < rx.remaining) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Not enough space in buffer %u/%zu", rx.remaining, buf_tailroom);
|
2016-12-31 13:41:45 +01:00
|
|
|
rx.discard = rx.remaining;
|
2017-01-31 10:24:37 +01:00
|
|
|
reset_rx();
|
2016-12-31 18:45:00 +01:00
|
|
|
return;
|
2016-12-31 13:41:45 +01:00
|
|
|
}
|
|
|
|
|
2016-12-25 14:21:39 +01:00
|
|
|
copy_hdr(rx.buf);
|
|
|
|
}
|
2015-04-13 13:38:44 +02:00
|
|
|
|
2016-12-25 14:21:39 +01:00
|
|
|
read = uart_fifo_read(h4_dev, net_buf_tail(rx.buf), rx.remaining);
|
2021-10-28 18:19:01 +02:00
|
|
|
if (unlikely(read < 0)) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Failed to read UART (err %d)", read);
|
2021-10-28 18:19:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-25 14:21:39 +01:00
|
|
|
net_buf_add(rx.buf, read);
|
|
|
|
rx.remaining -= read;
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("got %d bytes, remaining %u", read, rx.remaining);
|
|
|
|
LOG_DBG("Payload (len %u): %s", rx.buf->len, bt_hex(rx.buf->data, rx.buf->len));
|
2016-12-25 14:21:39 +01:00
|
|
|
|
|
|
|
if (rx.remaining) {
|
2016-12-31 18:45:00 +01:00
|
|
|
return;
|
2015-05-05 09:50:14 +02:00
|
|
|
}
|
2015-04-13 13:38:44 +02:00
|
|
|
|
2016-12-31 13:41:45 +01:00
|
|
|
buf = rx.buf;
|
|
|
|
rx.buf = NULL;
|
|
|
|
|
2024-01-09 19:21:42 +01:00
|
|
|
if (rx.type == BT_HCI_H4_EVT) {
|
2016-12-31 13:41:45 +01:00
|
|
|
bt_buf_set_type(buf, BT_BUF_EVT);
|
2016-12-25 14:21:39 +01:00
|
|
|
} else {
|
2016-12-31 13:41:45 +01:00
|
|
|
bt_buf_set_type(buf, BT_BUF_ACL_IN);
|
2016-12-25 14:21:39 +01:00
|
|
|
}
|
2016-12-31 13:41:45 +01:00
|
|
|
|
2017-01-31 10:24:37 +01:00
|
|
|
reset_rx();
|
2016-12-31 13:41:45 +01:00
|
|
|
|
2024-03-20 14:44:07 +01:00
|
|
|
LOG_DBG("Putting buf %p to rx fifo", buf);
|
|
|
|
net_buf_put(&rx.fifo, buf);
|
2016-12-25 14:21:39 +01:00
|
|
|
}
|
2015-04-13 13:38:44 +02:00
|
|
|
|
2016-12-25 14:21:39 +01:00
|
|
|
static inline void read_header(void)
|
|
|
|
{
|
|
|
|
switch (rx.type) {
|
2024-01-09 19:21:42 +01:00
|
|
|
case BT_HCI_H4_NONE:
|
2016-12-25 14:21:39 +01:00
|
|
|
h4_get_type();
|
|
|
|
return;
|
2024-01-09 19:21:42 +01:00
|
|
|
case BT_HCI_H4_EVT:
|
2016-12-25 14:21:39 +01:00
|
|
|
get_evt_hdr();
|
|
|
|
break;
|
2024-01-09 19:21:42 +01:00
|
|
|
case BT_HCI_H4_ACL:
|
2016-12-25 14:21:39 +01:00
|
|
|
get_acl_hdr();
|
|
|
|
break;
|
2024-01-09 19:21:42 +01:00
|
|
|
case BT_HCI_H4_ISO:
|
2020-05-12 22:26:26 +02:00
|
|
|
if (IS_ENABLED(CONFIG_BT_ISO)) {
|
|
|
|
get_iso_hdr();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
__fallthrough;
|
2016-12-25 14:21:39 +01:00
|
|
|
default:
|
|
|
|
CODE_UNREACHABLE;
|
|
|
|
return;
|
|
|
|
}
|
2015-04-29 12:36:41 +02:00
|
|
|
|
2016-12-25 14:21:39 +01:00
|
|
|
if (rx.have_hdr && rx.buf) {
|
|
|
|
if (rx.remaining > net_buf_tailroom(rx.buf)) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Not enough space in buffer");
|
2016-12-25 14:21:39 +01:00
|
|
|
rx.discard = rx.remaining;
|
2017-02-01 16:37:14 +01:00
|
|
|
reset_rx();
|
2016-12-25 14:21:39 +01:00
|
|
|
} else {
|
|
|
|
copy_hdr(rx.buf);
|
|
|
|
}
|
|
|
|
}
|
2015-04-13 13:38:44 +02:00
|
|
|
}
|
|
|
|
|
2016-12-31 18:45:00 +01:00
|
|
|
static inline void process_tx(void)
|
2015-04-13 13:38:44 +02:00
|
|
|
{
|
2016-12-31 18:45:00 +01:00
|
|
|
int bytes;
|
2015-10-31 09:53:34 +01:00
|
|
|
|
2016-12-31 18:45:00 +01:00
|
|
|
if (!tx.buf) {
|
|
|
|
tx.buf = net_buf_get(&tx.fifo, K_NO_WAIT);
|
|
|
|
if (!tx.buf) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("TX interrupt but no pending buffer!");
|
2016-12-31 18:45:00 +01:00
|
|
|
uart_irq_tx_disable(h4_dev);
|
|
|
|
return;
|
2015-04-13 13:38:44 +02:00
|
|
|
}
|
2016-12-31 18:45:00 +01:00
|
|
|
}
|
2015-04-13 13:38:44 +02:00
|
|
|
|
2016-12-31 18:45:00 +01:00
|
|
|
if (!tx.type) {
|
|
|
|
switch (bt_buf_get_type(tx.buf)) {
|
|
|
|
case BT_BUF_ACL_OUT:
|
2024-01-09 19:21:42 +01:00
|
|
|
tx.type = BT_HCI_H4_ACL;
|
2016-12-31 18:45:00 +01:00
|
|
|
break;
|
|
|
|
case BT_BUF_CMD:
|
2024-01-09 19:21:42 +01:00
|
|
|
tx.type = BT_HCI_H4_CMD;
|
2016-12-31 18:45:00 +01:00
|
|
|
break;
|
2020-05-12 22:26:26 +02:00
|
|
|
case BT_BUF_ISO_OUT:
|
|
|
|
if (IS_ENABLED(CONFIG_BT_ISO)) {
|
2024-01-09 19:21:42 +01:00
|
|
|
tx.type = BT_HCI_H4_ISO;
|
2020-05-12 22:26:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
__fallthrough;
|
2016-12-31 18:45:00 +01:00
|
|
|
default:
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Unknown buffer type");
|
2016-12-31 18:45:00 +01:00
|
|
|
goto done;
|
2015-04-29 12:36:41 +02:00
|
|
|
}
|
|
|
|
|
2016-12-31 18:45:00 +01:00
|
|
|
bytes = uart_fifo_fill(h4_dev, &tx.type, 1);
|
|
|
|
if (bytes != 1) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_WRN("Unable to send H:4 type");
|
2024-01-09 19:21:42 +01:00
|
|
|
tx.type = BT_HCI_H4_NONE;
|
2016-12-25 14:21:39 +01:00
|
|
|
return;
|
2015-04-13 13:38:44 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-31 18:45:00 +01:00
|
|
|
|
|
|
|
bytes = uart_fifo_fill(h4_dev, tx.buf->data, tx.buf->len);
|
2021-10-28 19:08:13 +02:00
|
|
|
if (unlikely(bytes < 0)) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Unable to write to UART (err %d)", bytes);
|
2021-10-28 19:08:13 +02:00
|
|
|
} else {
|
|
|
|
net_buf_pull(tx.buf, bytes);
|
|
|
|
}
|
2016-12-31 18:45:00 +01:00
|
|
|
|
|
|
|
if (tx.buf->len) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2024-01-09 19:21:42 +01:00
|
|
|
tx.type = BT_HCI_H4_NONE;
|
2016-12-31 18:45:00 +01:00
|
|
|
net_buf_unref(tx.buf);
|
|
|
|
tx.buf = net_buf_get(&tx.fifo, K_NO_WAIT);
|
|
|
|
if (!tx.buf) {
|
|
|
|
uart_irq_tx_disable(h4_dev);
|
|
|
|
}
|
2015-04-13 13:38:44 +02:00
|
|
|
}
|
|
|
|
|
2016-12-31 18:45:00 +01:00
|
|
|
static inline void process_rx(void)
|
2015-04-13 13:38:44 +02:00
|
|
|
{
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("remaining %u discard %u have_hdr %u rx.buf %p len %u", rx.remaining, rx.discard,
|
|
|
|
rx.have_hdr, rx.buf, rx.buf ? rx.buf->len : 0);
|
2016-04-19 13:37:30 +02:00
|
|
|
|
2016-12-31 18:45:00 +01:00
|
|
|
if (rx.discard) {
|
|
|
|
rx.discard -= h4_discard(h4_dev, rx.discard);
|
|
|
|
return;
|
2015-04-13 13:38:44 +02:00
|
|
|
}
|
|
|
|
|
2016-12-31 18:45:00 +01:00
|
|
|
if (rx.have_hdr) {
|
|
|
|
read_payload();
|
|
|
|
} else {
|
|
|
|
read_header();
|
2015-11-07 14:04:21 +01:00
|
|
|
}
|
2016-12-31 18:45:00 +01:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void bt_uart_isr(const struct device *unused, void *user_data)
|
2016-12-31 18:45:00 +01:00
|
|
|
{
|
|
|
|
ARG_UNUSED(unused);
|
2020-06-24 19:06:44 +02:00
|
|
|
ARG_UNUSED(user_data);
|
2016-12-31 18:45:00 +01:00
|
|
|
|
|
|
|
while (uart_irq_update(h4_dev) && uart_irq_is_pending(h4_dev)) {
|
|
|
|
if (uart_irq_tx_ready(h4_dev)) {
|
|
|
|
process_tx();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uart_irq_rx_ready(h4_dev)) {
|
|
|
|
process_rx();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int h4_send(struct net_buf *buf)
|
|
|
|
{
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("buf %p type %u len %u", buf, bt_buf_get_type(buf), buf->len);
|
2015-11-07 14:04:21 +01:00
|
|
|
|
2016-12-31 18:45:00 +01:00
|
|
|
net_buf_put(&tx.fifo, buf);
|
|
|
|
uart_irq_tx_enable(h4_dev);
|
2015-09-16 09:58:22 +02:00
|
|
|
|
|
|
|
return 0;
|
2015-04-13 13:38:44 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 13:34:43 +02:00
|
|
|
/** Setup the HCI transport, which usually means to reset the Bluetooth IC
|
|
|
|
*
|
|
|
|
* @param dev The device structure for the bus connecting to the IC
|
|
|
|
*
|
|
|
|
* @return 0 on success, negative error value on failure
|
|
|
|
*/
|
2020-04-30 20:33:38 +02:00
|
|
|
int __weak bt_hci_transport_setup(const struct device *dev)
|
2019-04-18 13:34:43 +02:00
|
|
|
{
|
|
|
|
h4_discard(h4_dev, 32);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-07 18:58:52 +01:00
|
|
|
static int h4_open(void)
|
2015-04-13 13:38:44 +02:00
|
|
|
{
|
2019-04-18 13:34:43 +02:00
|
|
|
int ret;
|
2021-03-03 22:27:52 +01:00
|
|
|
k_tid_t tid;
|
2019-04-18 13:34:43 +02:00
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("");
|
2015-04-13 13:38:44 +02:00
|
|
|
|
2015-12-07 18:58:52 +01:00
|
|
|
uart_irq_rx_disable(h4_dev);
|
|
|
|
uart_irq_tx_disable(h4_dev);
|
2015-04-13 13:38:44 +02:00
|
|
|
|
2019-04-18 13:34:43 +02:00
|
|
|
ret = bt_hci_transport_setup(h4_dev);
|
|
|
|
if (ret < 0) {
|
2016-04-08 12:50:41 +02:00
|
|
|
return -EIO;
|
2016-04-05 10:07:37 +02:00
|
|
|
}
|
|
|
|
|
2016-03-03 19:14:50 +01:00
|
|
|
uart_irq_callback_set(h4_dev, bt_uart_isr);
|
|
|
|
|
2021-03-03 22:27:52 +01:00
|
|
|
tid = k_thread_create(&rx_thread_data, rx_thread_stack,
|
|
|
|
K_KERNEL_STACK_SIZEOF(rx_thread_stack),
|
|
|
|
rx_thread, NULL, NULL, NULL,
|
|
|
|
K_PRIO_COOP(CONFIG_BT_RX_PRIO),
|
|
|
|
0, K_NO_WAIT);
|
|
|
|
k_thread_name_set(tid, "bt_rx_thread");
|
2016-12-25 14:21:39 +01:00
|
|
|
|
2016-04-08 12:50:41 +02:00
|
|
|
return 0;
|
2015-04-13 13:38:44 +02:00
|
|
|
}
|
|
|
|
|
2022-01-20 14:51:13 +01:00
|
|
|
#if defined(CONFIG_BT_HCI_SETUP)
|
2023-11-15 16:13:49 +01:00
|
|
|
static int h4_setup(const struct bt_hci_setup_params *params)
|
2022-01-20 14:51:13 +01:00
|
|
|
{
|
2023-11-15 16:13:49 +01:00
|
|
|
ARG_UNUSED(params);
|
|
|
|
|
2022-01-20 14:51:13 +01:00
|
|
|
/* Extern bt_h4_vnd_setup function.
|
|
|
|
* This function executes vendor-specific commands sequence to
|
|
|
|
* initialize BT Controller before BT Host executes Reset sequence.
|
|
|
|
* bt_h4_vnd_setup function must be implemented in vendor-specific HCI
|
|
|
|
* extansion module if CONFIG_BT_HCI_SETUP is enabled.
|
|
|
|
*/
|
|
|
|
extern int bt_h4_vnd_setup(const struct device *dev);
|
|
|
|
|
|
|
|
return bt_h4_vnd_setup(h4_dev);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-03-14 14:00:15 +01:00
|
|
|
static const struct bt_hci_driver drv = {
|
2016-04-22 14:13:09 +02:00
|
|
|
.name = "H:4",
|
2016-10-27 15:55:01 +02:00
|
|
|
.bus = BT_HCI_DRIVER_BUS_UART,
|
2015-12-07 18:58:52 +01:00
|
|
|
.open = h4_open,
|
|
|
|
.send = h4_send,
|
2022-01-20 14:51:13 +01:00
|
|
|
#if defined(CONFIG_BT_HCI_SETUP)
|
|
|
|
.setup = h4_setup
|
|
|
|
#endif
|
2015-04-13 13:38:44 +02:00
|
|
|
};
|
|
|
|
|
init: remove the need for a dummy device pointer in SYS_INIT functions
The init infrastructure, found in `init.h`, is currently used by:
- `SYS_INIT`: to call functions before `main`
- `DEVICE_*`: to initialize devices
They are all sorted according to an initialization level + a priority.
`SYS_INIT` calls are really orthogonal to devices, however, the required
function signature requires a `const struct device *dev` as a first
argument. The only reason for that is because the same init machinery is
used by devices, so we have something like:
```c
struct init_entry {
int (*init)(const struct device *dev);
/* only set by DEVICE_*, otherwise NULL */
const struct device *dev;
}
```
As a result, we end up with such weird/ugly pattern:
```c
static int my_init(const struct device *dev)
{
/* always NULL! add ARG_UNUSED to avoid compiler warning */
ARG_UNUSED(dev);
...
}
```
This is really a result of poor internals isolation. This patch proposes
a to make init entries more flexible so that they can accept sytem
initialization calls like this:
```c
static int my_init(void)
{
...
}
```
This is achieved using a union:
```c
union init_function {
/* for SYS_INIT, used when init_entry.dev == NULL */
int (*sys)(void);
/* for DEVICE*, used when init_entry.dev != NULL */
int (*dev)(const struct device *dev);
};
struct init_entry {
/* stores init function (either for SYS_INIT or DEVICE*)
union init_function init_fn;
/* stores device pointer for DEVICE*, NULL for SYS_INIT. Allows
* to know which union entry to call.
*/
const struct device *dev;
}
```
This solution **does not increase ROM usage**, and allows to offer clean
public APIs for both SYS_INIT and DEVICE*. Note that however, init
machinery keeps a coupling with devices.
**NOTE**: This is a breaking change! All `SYS_INIT` functions will need
to be converted to the new signature. See the script offered in the
following commit.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
init: convert SYS_INIT functions to the new signature
Conversion scripted using scripts/utils/migrate_sys_init.py.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
manifest: update projects for SYS_INIT changes
Update modules with updated SYS_INIT calls:
- hal_ti
- lvgl
- sof
- TraceRecorderSource
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
tests: devicetree: devices: adjust test
Adjust test according to the recently introduced SYS_INIT
infrastructure.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
tests: kernel: threads: adjust SYS_INIT call
Adjust to the new signature: int (*init_fn)(void);
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-19 09:33:44 +02:00
|
|
|
static int bt_uart_init(void)
|
2015-04-13 13:38:44 +02:00
|
|
|
{
|
2015-11-23 16:08:35 +01:00
|
|
|
|
2021-08-24 19:34:48 +02:00
|
|
|
if (!device_is_ready(h4_dev)) {
|
2022-08-17 12:45:32 +02:00
|
|
|
return -ENODEV;
|
2015-12-01 17:42:20 +01:00
|
|
|
}
|
|
|
|
|
2016-10-27 15:55:01 +02:00
|
|
|
bt_hci_driver_register(&drv);
|
2015-11-23 16:08:35 +01:00
|
|
|
|
2016-03-09 18:01:20 +01:00
|
|
|
return 0;
|
2015-04-13 13:38:44 +02:00
|
|
|
}
|
2015-11-23 16:08:35 +01:00
|
|
|
|
2019-03-12 22:15:42 +01:00
|
|
|
SYS_INIT(bt_uart_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
|