2022-05-23 13:50:52 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Telink Semiconductor (Shanghai) Co., Ltd.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
#include <zephyr/bluetooth/hci.h>
|
2022-05-23 13:50:52 +02:00
|
|
|
|
2022-07-18 13:08:11 +02:00
|
|
|
#include <zephyr/sys/byteorder.h>
|
|
|
|
#include <zephyr/drivers/bluetooth/hci_driver.h>
|
2022-05-23 13:50:52 +02:00
|
|
|
|
|
|
|
#include <b91_bt.h>
|
|
|
|
|
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_hci_driver_b91);
|
|
|
|
|
2022-05-23 13:50:52 +02:00
|
|
|
#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) {
|
2024-03-01 11:45:44 +01:00
|
|
|
#if defined(CONFIG_BT_CLASSIC)
|
2022-05-23 13:50:52 +02:00
|
|
|
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)) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Not enough data for event header");
|
2022-05-23 13:50:52 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
discardable = is_hci_event_discardable(data);
|
|
|
|
|
|
|
|
memcpy((void *)&hdr, data, sizeof(hdr));
|
|
|
|
data += sizeof(hdr);
|
|
|
|
len -= sizeof(hdr);
|
|
|
|
|
|
|
|
if (len != hdr.len) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Event payload length is not correct");
|
2022-05-23 13:50:52 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("len %u", hdr.len);
|
2022-05-23 13:50:52 +02:00
|
|
|
|
|
|
|
buf = bt_buf_get_evt(hdr.evt, discardable, K_NO_WAIT);
|
|
|
|
if (!buf) {
|
|
|
|
if (discardable) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Discardable buffer pool full, ignoring event");
|
2022-05-23 13:50:52 +02:00
|
|
|
} else {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("No available event buffers!");
|
2022-05-23 13:50:52 +02:00
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
net_buf_add_mem(buf, &hdr, sizeof(hdr));
|
|
|
|
|
|
|
|
buf_tailroom = net_buf_tailroom(buf);
|
|
|
|
if (buf_tailroom < len) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Not enough space in buffer %zu/%zu", len, buf_tailroom);
|
2022-05-23 13:50:52 +02:00
|
|
|
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)) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Not enough data for ACL header");
|
2022-05-23 13:50:52 +02:00
|
|
|
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 {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("No available ACL buffers!");
|
2022-05-23 13:50:52 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len != sys_le16_to_cpu(hdr.len)) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("ACL payload length is not correct");
|
2022-05-23 13:50:52 +02:00
|
|
|
net_buf_unref(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
net_buf_add_mem(buf, &hdr, sizeof(hdr));
|
|
|
|
buf_tailroom = net_buf_tailroom(buf);
|
|
|
|
if (buf_tailroom < len) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Not enough space in buffer %zu/%zu", len, buf_tailroom);
|
2022-05-23 13:50:52 +02:00
|
|
|
net_buf_unref(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("len %u", len);
|
2022-05-23 13:50:52 +02:00
|
|
|
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;
|
|
|
|
|
2022-10-24 11:23:12 +02:00
|
|
|
LOG_HEXDUMP_DBG(data, len, "host packet data:");
|
2022-05-23 13:50:52 +02:00
|
|
|
|
|
|
|
pkt_indicator = *data++;
|
|
|
|
len -= sizeof(pkt_indicator);
|
|
|
|
|
|
|
|
switch (pkt_indicator) {
|
2024-01-09 19:21:42 +01:00
|
|
|
case BT_HCI_H4_EVT:
|
2022-05-23 13:50:52 +02:00
|
|
|
buf = bt_b91_evt_recv(data, len);
|
|
|
|
break;
|
|
|
|
|
2024-01-09 19:21:42 +01:00
|
|
|
case BT_HCI_H4_ACL:
|
2022-05-23 13:50:52 +02:00
|
|
|
buf = bt_b91_acl_recv(data, len);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
buf = NULL;
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Unknown HCI type %u", pkt_indicator);
|
2022-05-23 13:50:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (buf) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Calling bt_recv(%p)", buf);
|
2022-05-23 13:50:52 +02:00
|
|
|
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;
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("buf %p type %u len %u", buf, bt_buf_get_type(buf), buf->len);
|
2022-05-23 13:50:52 +02:00
|
|
|
|
|
|
|
switch (bt_buf_get_type(buf)) {
|
|
|
|
case BT_BUF_ACL_OUT:
|
2024-01-09 19:21:42 +01:00
|
|
|
type = BT_HCI_H4_ACL;
|
2022-05-23 13:50:52 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_BUF_CMD:
|
2024-01-09 19:21:42 +01:00
|
|
|
type = BT_HCI_H4_CMD;
|
2022-05-23 13:50:52 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Unknown type %u", bt_buf_get_type(buf));
|
2022-05-23 13:50:52 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2022-10-24 11:23:12 +02:00
|
|
|
LOG_HEXDUMP_DBG(buf->data, buf->len, "Final HCI buffer:");
|
2022-05-23 13:50:52 +02:00
|
|
|
|
|
|
|
if (k_sem_take(&hci_send_sem, HCI_BT_B91_TIMEOUT) == 0) {
|
|
|
|
b91_bt_host_send_packet(type, buf->data, buf->len);
|
|
|
|
} else {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Send packet timeout error");
|
2022-05-23 13:50:52 +02:00
|
|
|
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) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Bluetooth controller init failed %d", status);
|
2022-05-23 13:50:52 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
b91_bt_host_callback_register(&vhci_host_cb);
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("B91 BT started");
|
2022-05-23 13:50:52 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
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_b91_init(void)
|
2022-05-23 13:50:52 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
bt_hci_driver_register(&drv);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SYS_INIT(bt_b91_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
|