1 /*
2  * Copyright (c) 2019 Nordic Semiconductor ASA
3  * Copyright 2025 NXP
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/init.h>
9 #include <zephyr/sys/byteorder.h>
10 
11 #include <zephyr/bluetooth/bluetooth.h>
12 #include <zephyr/bluetooth/hci.h>
13 #include <zephyr/bluetooth/hci_types.h>
14 #include <zephyr/drivers/bluetooth.h>
15 
16 #include <zephyr/device.h>
17 #include <zephyr/ipc/ipc_service.h>
18 
19 #define LOG_LEVEL CONFIG_BT_HCI_DRIVER_LOG_LEVEL
20 #include <zephyr/logging/log.h>
21 LOG_MODULE_REGISTER(bt_hci_driver);
22 
23 BUILD_ASSERT(!IS_ENABLED(CONFIG_BT_CONN) || IS_ENABLED(CONFIG_BT_HCI_ACL_FLOW_CONTROL),
24 	     "HCI IPC driver can drop ACL data without Controller-to-Host ACL flow control");
25 
26 #define DT_DRV_COMPAT zephyr_bt_hci_ipc
27 
28 #define IPC_BOUND_TIMEOUT_IN_MS K_MSEC(CONFIG_BT_HCI_IPC_ENDPOINT_BOUND_TIMEOUT_MS)
29 
30 /* The retry of ipc_service_send function requires a small (tens of us) delay.
31  * In order to ensure proper delay k_usleep is used when the system clock is
32  * precise enough and available (CONFIG_SYS_CLOCK_TICKS_PER_SEC different than 0).
33  */
34 #define USE_SLEEP_BETWEEN_IPC_RETRIES COND_CODE_0(CONFIG_SYS_CLOCK_TICKS_PER_SEC, \
35 	(false), \
36 	((USEC_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC) > CONFIG_BT_HCI_IPC_SEND_RETRY_DELAY_US))
37 
38 struct ipc_data {
39 	bt_hci_recv_t recv;
40 	struct ipc_ept hci_ept;
41 	struct ipc_ept_cfg hci_ept_cfg;
42 	struct k_sem bound_sem;
43 	const struct device *ipc;
44 };
45 
is_hci_event_discardable(const uint8_t * evt_data)46 static bool is_hci_event_discardable(const uint8_t *evt_data)
47 {
48 	uint8_t evt_type = evt_data[0];
49 
50 	switch (evt_type) {
51 #if defined(CONFIG_BT_CLASSIC)
52 	case BT_HCI_EVT_INQUIRY_RESULT_WITH_RSSI:
53 	case BT_HCI_EVT_EXTENDED_INQUIRY_RESULT:
54 		return true;
55 #endif
56 	case BT_HCI_EVT_LE_META_EVENT: {
57 		uint8_t subevt_type = evt_data[sizeof(struct bt_hci_evt_hdr)];
58 
59 		switch (subevt_type) {
60 		case BT_HCI_EVT_LE_ADVERTISING_REPORT:
61 			return true;
62 #if defined(CONFIG_BT_EXT_ADV)
63 		case BT_HCI_EVT_LE_EXT_ADVERTISING_REPORT:
64 		{
65 			const struct bt_hci_evt_le_ext_advertising_report *ext_adv =
66 				(void *)&evt_data[3];
67 
68 			return (ext_adv->num_reports == 1) &&
69 				   ((ext_adv->adv_info[0].evt_type &
70 					 BT_HCI_LE_ADV_EVT_TYPE_LEGACY) != 0);
71 		}
72 #endif
73 		default:
74 			return false;
75 		}
76 	}
77 	default:
78 		return false;
79 	}
80 }
81 
bt_ipc_evt_recv(const uint8_t * data,size_t remaining)82 static struct net_buf *bt_ipc_evt_recv(const uint8_t *data, size_t remaining)
83 {
84 	bool discardable;
85 	struct bt_hci_evt_hdr hdr;
86 	struct net_buf *buf;
87 	size_t buf_tailroom;
88 
89 	if (remaining < sizeof(hdr)) {
90 		LOG_ERR("Not enough data (%u) for event header (%zu)", remaining, sizeof(hdr));
91 		return NULL;
92 	}
93 
94 	discardable = is_hci_event_discardable(data);
95 
96 	memcpy((void *)&hdr, data, sizeof(hdr));
97 	data += sizeof(hdr);
98 	remaining -= sizeof(hdr);
99 
100 	if (remaining != hdr.len) {
101 		LOG_ERR("Event payload length is not correct (%u != %u)", remaining, hdr.len);
102 		return NULL;
103 	}
104 	LOG_DBG("len %u", hdr.len);
105 
106 	do {
107 		buf = bt_buf_get_evt(hdr.evt, discardable, discardable ? K_NO_WAIT : K_SECONDS(10));
108 		if (!buf) {
109 			if (discardable) {
110 				LOG_DBG("Discardable buffer pool full, ignoring event");
111 				return buf;
112 			}
113 			LOG_WRN("Couldn't allocate a buffer after waiting 10 seconds.");
114 		}
115 	} while (!buf);
116 
117 	net_buf_add_mem(buf, &hdr, sizeof(hdr));
118 
119 	buf_tailroom = net_buf_tailroom(buf);
120 	if (buf_tailroom < remaining) {
121 		LOG_ERR("Not enough space in buffer %zu/%zu", remaining, buf_tailroom);
122 		net_buf_unref(buf);
123 		return NULL;
124 	}
125 
126 	net_buf_add_mem(buf, data, remaining);
127 
128 	return buf;
129 }
130 
bt_ipc_acl_recv(const uint8_t * data,size_t remaining)131 static struct net_buf *bt_ipc_acl_recv(const uint8_t *data, size_t remaining)
132 {
133 	struct bt_hci_acl_hdr hdr;
134 	struct net_buf *buf;
135 	size_t buf_tailroom;
136 
137 	if (remaining < sizeof(hdr)) {
138 		LOG_ERR("Not enough data (%u) for ACL header (%zu)", remaining, sizeof(hdr));
139 		return NULL;
140 	}
141 
142 	buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_NO_WAIT);
143 	if (buf) {
144 		memcpy((void *)&hdr, data, sizeof(hdr));
145 		data += sizeof(hdr);
146 		remaining -= sizeof(hdr);
147 
148 		net_buf_add_mem(buf, &hdr, sizeof(hdr));
149 	} else {
150 		LOG_ERR("No available ACL buffers!");
151 		return NULL;
152 	}
153 
154 	if (remaining != sys_le16_to_cpu(hdr.len)) {
155 		LOG_ERR("ACL payload length is not correct (%u != %u)", remaining,
156 			sys_le16_to_cpu(hdr.len));
157 		net_buf_unref(buf);
158 		return NULL;
159 	}
160 
161 	buf_tailroom = net_buf_tailroom(buf);
162 	if (buf_tailroom < remaining) {
163 		LOG_ERR("Not enough space in buffer %zu/%zu", remaining, buf_tailroom);
164 		net_buf_unref(buf);
165 		return NULL;
166 	}
167 
168 	LOG_DBG("len %u", remaining);
169 	net_buf_add_mem(buf, data, remaining);
170 
171 	return buf;
172 }
173 
bt_ipc_iso_recv(const uint8_t * data,size_t remaining)174 static struct net_buf *bt_ipc_iso_recv(const uint8_t *data, size_t remaining)
175 {
176 	struct bt_hci_iso_hdr hdr;
177 	static size_t fail_cnt;
178 	struct net_buf *buf;
179 	size_t buf_tailroom;
180 
181 	if (remaining < sizeof(hdr)) {
182 		LOG_ERR("Not enough data (%u) for ISO header (%zu)", remaining, sizeof(hdr));
183 		return NULL;
184 	}
185 
186 	buf = bt_buf_get_rx(BT_BUF_ISO_IN, K_NO_WAIT);
187 	if (buf) {
188 		memcpy((void *)&hdr, data, sizeof(hdr));
189 		data += sizeof(hdr);
190 		remaining -= sizeof(hdr);
191 
192 		net_buf_add_mem(buf, &hdr, sizeof(hdr));
193 
194 		fail_cnt = 0U;
195 	} else {
196 		if ((fail_cnt % 100U) == 0U) {
197 			LOG_ERR("No available ISO buffers (%zu)!", fail_cnt);
198 		}
199 
200 		fail_cnt++;
201 
202 		return NULL;
203 	}
204 
205 	if (remaining != bt_iso_hdr_len(sys_le16_to_cpu(hdr.len))) {
206 		LOG_ERR("ISO payload length is not correct (%u != %lu)", remaining,
207 			bt_iso_hdr_len(sys_le16_to_cpu(hdr.len)));
208 		net_buf_unref(buf);
209 		return NULL;
210 	}
211 
212 	buf_tailroom = net_buf_tailroom(buf);
213 	if (buf_tailroom < remaining) {
214 		LOG_ERR("Not enough space in buffer %zu/%zu", remaining, buf_tailroom);
215 		net_buf_unref(buf);
216 		return NULL;
217 	}
218 
219 	LOG_DBG("len %zu", remaining);
220 	net_buf_add_mem(buf, data, remaining);
221 
222 	return buf;
223 }
224 
bt_ipc_rx(const struct device * dev,const uint8_t * data,size_t len)225 static void bt_ipc_rx(const struct device *dev, const uint8_t *data, size_t len)
226 {
227 	struct ipc_data *ipc = dev->data;
228 	uint8_t pkt_indicator;
229 	struct net_buf *buf = NULL;
230 	size_t remaining = len;
231 
232 	LOG_HEXDUMP_DBG(data, len, "ipc data:");
233 
234 	pkt_indicator = *data++;
235 	remaining -= sizeof(pkt_indicator);
236 
237 	switch (pkt_indicator) {
238 	case BT_HCI_H4_EVT:
239 		buf = bt_ipc_evt_recv(data, remaining);
240 		break;
241 
242 	case BT_HCI_H4_ACL:
243 		buf = bt_ipc_acl_recv(data, remaining);
244 		break;
245 
246 	case BT_HCI_H4_ISO:
247 		buf = bt_ipc_iso_recv(data, remaining);
248 		break;
249 
250 	default:
251 		LOG_ERR("Unknown HCI type %u", pkt_indicator);
252 		return;
253 	}
254 
255 	if (buf) {
256 		LOG_DBG("Calling bt_recv(%p)", buf);
257 		ipc->recv(dev, buf);
258 
259 		LOG_HEXDUMP_DBG(buf->data, buf->len, "RX buf payload:");
260 	}
261 }
262 
bt_ipc_send(const struct device * dev,struct net_buf * buf)263 static int bt_ipc_send(const struct device *dev, struct net_buf *buf)
264 {
265 	struct ipc_data *data = dev->data;
266 	int err;
267 
268 	LOG_DBG("buf %p type %u len %u", buf, buf->data[0], buf->len);
269 
270 	for (int retries = 0; retries < CONFIG_BT_HCI_IPC_SEND_RETRY_COUNT + 1; retries++) {
271 		err = ipc_service_send(&data->hci_ept, buf->data, buf->len);
272 		if ((err >= 0) || (err != -ENOMEM)) {
273 			break;
274 		}
275 
276 		if (USE_SLEEP_BETWEEN_IPC_RETRIES) {
277 			k_usleep(CONFIG_BT_HCI_IPC_SEND_RETRY_DELAY_US);
278 		} else {
279 			k_busy_wait(CONFIG_BT_HCI_IPC_SEND_RETRY_DELAY_US);
280 		}
281 	}
282 
283 	if (err < 0) {
284 		LOG_ERR("Failed to send (err %d)", err);
285 		return err;
286 	}
287 
288 	net_buf_unref(buf);
289 
290 	return 0;
291 }
292 
hci_ept_bound(void * priv)293 static void hci_ept_bound(void *priv)
294 {
295 	const struct device *dev = priv;
296 	struct ipc_data *ipc = dev->data;
297 
298 	k_sem_give(&ipc->bound_sem);
299 }
300 
hci_ept_recv(const void * data,size_t len,void * priv)301 static void hci_ept_recv(const void *data, size_t len, void *priv)
302 {
303 	const struct device *dev = priv;
304 
305 	bt_ipc_rx(dev, data, len);
306 }
307 
bt_hci_transport_setup(const struct device * dev)308 int __weak bt_hci_transport_setup(const struct device *dev)
309 {
310 	ARG_UNUSED(dev);
311 	return 0;
312 }
313 
bt_hci_transport_teardown(const struct device * dev)314 int __weak bt_hci_transport_teardown(const struct device *dev)
315 {
316 	ARG_UNUSED(dev);
317 	return 0;
318 }
319 
bt_ipc_open(const struct device * dev,bt_hci_recv_t recv)320 static int bt_ipc_open(const struct device *dev, bt_hci_recv_t recv)
321 {
322 	struct ipc_data *ipc = dev->data;
323 	int err;
324 
325 	err = bt_hci_transport_setup(NULL);
326 	if (err) {
327 		LOG_ERR("HCI transport setup failed with: %d\n", err);
328 		return err;
329 	}
330 
331 	LOG_DBG("");
332 
333 	err = ipc_service_open_instance(ipc->ipc);
334 	if (err && (err != -EALREADY)) {
335 		LOG_ERR("IPC service instance initialization failed: %d\n", err);
336 		return err;
337 	}
338 
339 	err = ipc_service_register_endpoint(ipc->ipc, &ipc->hci_ept, &ipc->hci_ept_cfg);
340 	if (err) {
341 		LOG_ERR("Registering endpoint failed with %d", err);
342 		return err;
343 	}
344 
345 	err = k_sem_take(&ipc->bound_sem, IPC_BOUND_TIMEOUT_IN_MS);
346 	if (err) {
347 		LOG_ERR("Endpoint binding failed with %d", err);
348 		return err;
349 	}
350 
351 	ipc->recv = recv;
352 
353 	return 0;
354 }
355 
bt_ipc_close(const struct device * dev)356 static int bt_ipc_close(const struct device *dev)
357 {
358 	struct ipc_data *ipc = dev->data;
359 	int err;
360 
361 	err = ipc_service_deregister_endpoint(&ipc->hci_ept);
362 	if (err) {
363 		LOG_ERR("Deregistering HCI endpoint failed with: %d", err);
364 		return err;
365 	}
366 
367 	err = ipc_service_close_instance(ipc->ipc);
368 	if (err) {
369 		LOG_ERR("Closing IPC service failed with: %d", err);
370 		return err;
371 	}
372 
373 	err = bt_hci_transport_teardown(NULL);
374 	if (err) {
375 		LOG_ERR("HCI transport teardown failed with: %d", err);
376 		return err;
377 	}
378 
379 	ipc->recv = NULL;
380 
381 	return 0;
382 }
383 
384 static DEVICE_API(bt_hci, drv) = {
385 	.open		= bt_ipc_open,
386 	.close		= bt_ipc_close,
387 	.send		= bt_ipc_send,
388 };
389 
390 #define IPC_DEVICE_INIT(inst) \
391 	static struct ipc_data ipc_data_##inst = { \
392 		.bound_sem = Z_SEM_INITIALIZER(ipc_data_##inst.bound_sem, 0, 1), \
393 		.hci_ept_cfg = { \
394 			.name = DT_INST_PROP(inst, bt_hci_ipc_name), \
395 			.cb = { \
396 				.bound    = hci_ept_bound, \
397 				.received = hci_ept_recv, \
398 			}, \
399 			.priv = (void *)DEVICE_DT_INST_GET(inst), \
400 		}, \
401 		.ipc = DEVICE_DT_GET(DT_INST_PARENT(inst)), \
402 	}; \
403 	DEVICE_DT_INST_DEFINE(inst, NULL, NULL, &ipc_data_##inst, NULL, \
404 			      POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &drv)
405 
406 DT_INST_FOREACH_STATUS_OKAY(IPC_DEVICE_INIT)
407