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