1 /*
2  * Copyright (c) 2022, Telink Semiconductor (Shanghai) Co., Ltd.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/bluetooth/hci.h>
8 
9 #include <zephyr/sys/byteorder.h>
10 #include <zephyr/drivers/bluetooth/hci_driver.h>
11 
12 #include <b91_bt.h>
13 
14 #define LOG_LEVEL CONFIG_BT_HCI_DRIVER_LOG_LEVEL
15 #include <zephyr/logging/log.h>
16 LOG_MODULE_REGISTER(bt_hci_driver_b91);
17 
18 #define HCI_CMD                 0x01
19 #define HCI_ACL                 0x02
20 #define HCI_EVT                 0x04
21 
22 #define HCI_BT_B91_TIMEOUT K_MSEC(2000)
23 
24 static K_SEM_DEFINE(hci_send_sem, 1, 1);
25 
is_hci_event_discardable(const uint8_t * evt_data)26 static bool is_hci_event_discardable(const uint8_t *evt_data)
27 {
28 	uint8_t evt_type = evt_data[0];
29 
30 	switch (evt_type) {
31 #if defined(CONFIG_BT_BREDR)
32 	case BT_HCI_EVT_INQUIRY_RESULT_WITH_RSSI:
33 	case BT_HCI_EVT_EXTENDED_INQUIRY_RESULT:
34 		return true;
35 #endif
36 	case BT_HCI_EVT_LE_META_EVENT: {
37 		uint8_t subevt_type = evt_data[sizeof(struct bt_hci_evt_hdr)];
38 
39 		switch (subevt_type) {
40 		case BT_HCI_EVT_LE_ADVERTISING_REPORT:
41 			return true;
42 		default:
43 			return false;
44 		}
45 	}
46 	default:
47 		return false;
48 	}
49 }
50 
bt_b91_evt_recv(uint8_t * data,size_t len)51 static struct net_buf *bt_b91_evt_recv(uint8_t *data, size_t len)
52 {
53 	bool discardable;
54 	struct bt_hci_evt_hdr hdr;
55 	struct net_buf *buf;
56 	size_t buf_tailroom;
57 
58 	if (len < sizeof(hdr)) {
59 		LOG_ERR("Not enough data for event header");
60 		return NULL;
61 	}
62 
63 	discardable = is_hci_event_discardable(data);
64 
65 	memcpy((void *)&hdr, data, sizeof(hdr));
66 	data += sizeof(hdr);
67 	len -= sizeof(hdr);
68 
69 	if (len != hdr.len) {
70 		LOG_ERR("Event payload length is not correct");
71 		return NULL;
72 	}
73 	LOG_DBG("len %u", hdr.len);
74 
75 	buf = bt_buf_get_evt(hdr.evt, discardable, K_NO_WAIT);
76 	if (!buf) {
77 		if (discardable) {
78 			LOG_DBG("Discardable buffer pool full, ignoring event");
79 		} else {
80 			LOG_ERR("No available event buffers!");
81 		}
82 		return buf;
83 	}
84 
85 	net_buf_add_mem(buf, &hdr, sizeof(hdr));
86 
87 	buf_tailroom = net_buf_tailroom(buf);
88 	if (buf_tailroom < len) {
89 		LOG_ERR("Not enough space in buffer %zu/%zu", len, buf_tailroom);
90 		net_buf_unref(buf);
91 		return NULL;
92 	}
93 
94 	net_buf_add_mem(buf, data, len);
95 
96 	return buf;
97 }
98 
bt_b91_acl_recv(uint8_t * data,size_t len)99 static struct net_buf *bt_b91_acl_recv(uint8_t *data, size_t len)
100 {
101 	struct bt_hci_acl_hdr hdr;
102 	struct net_buf *buf;
103 	size_t buf_tailroom;
104 
105 	if (len < sizeof(hdr)) {
106 		LOG_ERR("Not enough data for ACL header");
107 		return NULL;
108 	}
109 
110 	buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_NO_WAIT);
111 	if (buf) {
112 		memcpy((void *)&hdr, data, sizeof(hdr));
113 		data += sizeof(hdr);
114 		len -= sizeof(hdr);
115 	} else {
116 		LOG_ERR("No available ACL buffers!");
117 		return NULL;
118 	}
119 
120 	if (len != sys_le16_to_cpu(hdr.len)) {
121 		LOG_ERR("ACL payload length is not correct");
122 		net_buf_unref(buf);
123 		return NULL;
124 	}
125 
126 	net_buf_add_mem(buf, &hdr, sizeof(hdr));
127 	buf_tailroom = net_buf_tailroom(buf);
128 	if (buf_tailroom < len) {
129 		LOG_ERR("Not enough space in buffer %zu/%zu", len, buf_tailroom);
130 		net_buf_unref(buf);
131 		return NULL;
132 	}
133 
134 	LOG_DBG("len %u", len);
135 	net_buf_add_mem(buf, data, len);
136 
137 	return buf;
138 }
139 
hci_b91_host_rcv_pkt(uint8_t * data,uint16_t len)140 static void hci_b91_host_rcv_pkt(uint8_t *data, uint16_t len)
141 {
142 	uint8_t pkt_indicator;
143 	struct net_buf *buf;
144 
145 	LOG_HEXDUMP_DBG(data, len, "host packet data:");
146 
147 	pkt_indicator = *data++;
148 	len -= sizeof(pkt_indicator);
149 
150 	switch (pkt_indicator) {
151 	case HCI_EVT:
152 		buf = bt_b91_evt_recv(data, len);
153 		break;
154 
155 	case HCI_ACL:
156 		buf = bt_b91_acl_recv(data, len);
157 		break;
158 
159 	default:
160 		buf = NULL;
161 		LOG_ERR("Unknown HCI type %u", pkt_indicator);
162 	}
163 
164 	if (buf) {
165 		LOG_DBG("Calling bt_recv(%p)", buf);
166 		bt_recv(buf);
167 	}
168 }
169 
hci_b91_controller_rcv_pkt_ready(void)170 static void hci_b91_controller_rcv_pkt_ready(void)
171 {
172 	k_sem_give(&hci_send_sem);
173 }
174 
175 static b91_bt_host_callback_t vhci_host_cb = {
176 	.host_send_available = hci_b91_controller_rcv_pkt_ready,
177 	.host_read_packet = hci_b91_host_rcv_pkt
178 };
179 
bt_b91_send(struct net_buf * buf)180 static int bt_b91_send(struct net_buf *buf)
181 {
182 	int err = 0;
183 	uint8_t type;
184 
185 	LOG_DBG("buf %p type %u len %u", buf, bt_buf_get_type(buf), buf->len);
186 
187 	switch (bt_buf_get_type(buf)) {
188 	case BT_BUF_ACL_OUT:
189 		type = HCI_ACL;
190 		break;
191 
192 	case BT_BUF_CMD:
193 		type = HCI_CMD;
194 		break;
195 
196 	default:
197 		LOG_ERR("Unknown type %u", bt_buf_get_type(buf));
198 		goto done;
199 	}
200 
201 	LOG_HEXDUMP_DBG(buf->data, buf->len, "Final HCI buffer:");
202 
203 	if (k_sem_take(&hci_send_sem, HCI_BT_B91_TIMEOUT) == 0) {
204 		b91_bt_host_send_packet(type, buf->data, buf->len);
205 	} else {
206 		LOG_ERR("Send packet timeout error");
207 		err = -ETIMEDOUT;
208 	}
209 
210 done:
211 	net_buf_unref(buf);
212 	k_sem_give(&hci_send_sem);
213 
214 	return err;
215 }
216 
hci_b91_open(void)217 static int hci_b91_open(void)
218 {
219 	int status;
220 
221 	status = b91_bt_controller_init();
222 	if (status) {
223 		LOG_ERR("Bluetooth controller init failed %d", status);
224 		return status;
225 	}
226 
227 	b91_bt_host_callback_register(&vhci_host_cb);
228 
229 	LOG_DBG("B91 BT started");
230 
231 	return 0;
232 }
233 
234 static const struct bt_hci_driver drv = {
235 	.name   = "BT B91",
236 	.open   = hci_b91_open,
237 	.send   = bt_b91_send,
238 	.bus    = BT_HCI_DRIVER_BUS_IPM,
239 #if defined(CONFIG_BT_DRIVER_QUIRK_NO_AUTO_DLE)
240 	.quirks = BT_QUIRK_NO_AUTO_DLE,
241 #endif
242 };
243 
bt_b91_init(void)244 static int bt_b91_init(void)
245 {
246 
247 	bt_hci_driver_register(&drv);
248 
249 	return 0;
250 }
251 
252 SYS_INIT(bt_b91_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
253