summaryrefslogtreecommitdiff
path: root/third_party/zephyr_bluetooth-2.7.5/ble_hci_usbh.c
blob: 233d5544585f95776a7ba545e76e2721c2e160cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "usbh_core.h"
#include "usbh_bluetooth.h"

#include <zephyr.h>

/* compatible with low version that less than v2.7.5 */

/* @brief The HCI event shall be given to bt_recv_prio */
#define __BT_HCI_EVT_FLAG_RECV_PRIO BIT(0)
/* @brief  The HCI event shall be given to bt_recv. */
#define __BT_HCI_EVT_FLAG_RECV      BIT(1)

/** @brief Get HCI event flags.
 *
 * Helper for the HCI driver to get HCI event flags that describes rules that.
 * must be followed.
 *
 * When CONFIG_BT_RECV_IS_RX_THREAD is enabled the flags
 * __BT_HCI_EVT_FLAG_RECV and __BT_HCI_EVT_FLAG_RECV_PRIO indicates if the event
 * should be given to bt_recv or bt_recv_prio.
 *
 * @param evt HCI event code.
 *
 * @return HCI event flags for the specified event.
 */
static inline uint8_t __bt_hci_evt_get_flags(uint8_t evt)
{
    switch (evt) {
        case BT_HCI_EVT_DISCONN_COMPLETE:
            return __BT_HCI_EVT_FLAG_RECV | __BT_HCI_EVT_FLAG_RECV_PRIO;
#if defined(CONFIG_BT_CONN) || defined(CONFIG_BT_ISO)
        case BT_HCI_EVT_NUM_COMPLETED_PACKETS:
#if defined(CONFIG_BT_CONN)
        case BT_HCI_EVT_DATA_BUF_OVERFLOW:
#endif /* defined(CONFIG_BT_CONN) */
#endif /* CONFIG_BT_CONN ||  CONFIG_BT_ISO */
        case BT_HCI_EVT_CMD_COMPLETE:
        case BT_HCI_EVT_CMD_STATUS:
            return __BT_HCI_EVT_FLAG_RECV_PRIO;
        default:
            return __BT_HCI_EVT_FLAG_RECV;
    }
}

static bool is_hci_event_discardable(const uint8_t *evt_data)
{
    uint8_t evt_type = evt_data[0];

    switch (evt_type) {
#if defined(CONFIG_BT_BREDR)
        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 *usbh_bt_evt_recv(uint8_t *data, size_t remaining)
{
    bool discardable = false;
    struct bt_hci_evt_hdr hdr;
    struct net_buf *buf;
    size_t buf_tailroom;

    if (remaining < sizeof(hdr)) {
        USB_LOG_ERR("Not enough data for event header\r\n");
        return NULL;
    }

    discardable = is_hci_event_discardable(data);

    memcpy((void *)&hdr, data, sizeof(hdr));
    data += sizeof(hdr);
    remaining -= sizeof(hdr);

    if (remaining != hdr.len) {
        USB_LOG_ERR("Event payload length is not correct\r\n");
        return NULL;
    }

    buf = bt_buf_get_evt(hdr.evt, discardable, K_NO_WAIT);
    if (!buf) {
        if (discardable) {
            USB_LOG_DBG("Discardable buffer pool full, ignoring event\r\n");
        } else {
            USB_LOG_ERR("No available event buffers!\r\n");
        }
        return buf;
    }

    net_buf_add_mem(buf, &hdr, sizeof(hdr));

    buf_tailroom = net_buf_tailroom(buf);
    if (buf_tailroom < remaining) {
        USB_LOG_ERR("Not enough space in buffer %zu/%zu\r\n", remaining, buf_tailroom);
        net_buf_unref(buf);
        return NULL;
    }

    net_buf_add_mem(buf, data, remaining);

    return buf;
}

static struct net_buf *usbh_bt_acl_recv(uint8_t *data, size_t remaining)
{
    struct bt_hci_acl_hdr hdr;
    struct net_buf *buf;
    size_t buf_tailroom;

    if (remaining < sizeof(hdr)) {
        USB_LOG_ERR("Not enough data for ACL header\r\n");
        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);
        remaining -= sizeof(hdr);

        net_buf_add_mem(buf, &hdr, sizeof(hdr));
    } else {
        USB_LOG_ERR("No available ACL buffers!\r\n");
        return NULL;
    }

    if (remaining != sys_le16_to_cpu(hdr.len)) {
        USB_LOG_ERR("ACL payload length is not correct\r\n");
        net_buf_unref(buf);
        return NULL;
    }

    buf_tailroom = net_buf_tailroom(buf);
    if (buf_tailroom < remaining) {
        USB_LOG_ERR("Not enough space in buffer %zu/%zu\r\n", remaining, buf_tailroom);
        net_buf_unref(buf);
        return NULL;
    }

    USB_LOG_DBG("len %u", remaining);
    net_buf_add_mem(buf, data, remaining);

    return buf;
}

static struct net_buf *usbh_bt_iso_recv(uint8_t *data, size_t remaining)
{
    struct bt_hci_iso_hdr hdr;
    struct net_buf *buf;
    size_t buf_tailroom;

    if (remaining < sizeof(hdr)) {
        USB_LOG_ERR("Not enough data for ISO header\r\n");
        return NULL;
    }

    buf = bt_buf_get_rx(BT_BUF_ISO_IN, K_NO_WAIT);
    if (buf) {
        memcpy((void *)&hdr, data, sizeof(hdr));
        data += sizeof(hdr);
        remaining -= sizeof(hdr);

        net_buf_add_mem(buf, &hdr, sizeof(hdr));
    } else {
        USB_LOG_ERR("No available ISO buffers!\r\n");
        return NULL;
    }

    /*
     * Per Bluetooth Core Spec v5.x Vol 4 Part E Section 5.4.5,
     * the ISO_Data_Load_Length field occupies bits 0..13 (14 bits) of the
     * 16-bit length field in the HCI ISO Data packet header.
     * Bits 14..15 are RFU and must be masked out.
     * This matches Zephyr's bt_iso_hdr_len() macro definition:
     *   #define bt_iso_hdr_len(h) ((h) & BIT_MASK(14))  // BIT_MASK(14) == 0x3FFF
     */
    if (remaining != (sys_le16_to_cpu(hdr.len) & 0x3FFF)) {
        USB_LOG_ERR("ISO payload length is not correct\r\n");
        net_buf_unref(buf);
        return NULL;
    }

    buf_tailroom = net_buf_tailroom(buf);
    if (buf_tailroom < remaining) {
        USB_LOG_ERR("Not enough space in buffer %zu/%zu\r\n", remaining, buf_tailroom);
        net_buf_unref(buf);
        return NULL;
    }

    USB_LOG_DBG("len %zu", remaining);
    net_buf_add_mem(buf, data, remaining);

    return buf;
}

static int usbh_hci_host_rcv_pkt(uint8_t *data, uint32_t len)
{
    struct net_buf *buf = NULL;
    size_t remaining = len;
    uint8_t pkt_indicator;

    pkt_indicator = *data++;
    remaining -= sizeof(pkt_indicator);

    switch (pkt_indicator) {
        case USB_BLUETOOTH_HCI_EVT:
            buf = usbh_bt_evt_recv(data, remaining);
            break;

        case USB_BLUETOOTH_HCI_ACL:
            buf = usbh_bt_acl_recv(data, remaining);
            break;

        case USB_BLUETOOTH_HCI_ISO:
            buf = usbh_bt_iso_recv(data, remaining);
            break;
        default:
            USB_LOG_ERR("Unknown HCI type %u\r\n", pkt_indicator);
            return -1;
    }

    if (buf) {
#ifdef CONFIG_BT_RECV_IS_RX_THREAD
        if (pkt_indicator == USB_BLUETOOTH_HCI_EVT) {
            struct bt_hci_evt_hdr *hdr = (void *)buf->data;
            uint8_t evt_flags = __bt_hci_evt_get_flags(hdr->evt);

            if (evt_flags & __BT_HCI_EVT_FLAG_RECV_PRIO) {
                bt_recv_prio(buf);
            } else {
                bt_recv(buf);
            }
        } else {
            bt_recv(buf);
        }
#else
        bt_recv(buf);
#endif
    }

    return 0;
}

static int bt_usbh_open(void)
{
    return 0;
}

static int bt_usbh_send(struct net_buf *buf)
{
    int err = 0;
    uint8_t pkt_indicator;

    switch (bt_buf_get_type(buf)) {
        case BT_BUF_ACL_OUT:
            pkt_indicator = USB_BLUETOOTH_HCI_ACL;
            break;
        case BT_BUF_CMD:
            pkt_indicator = USB_BLUETOOTH_HCI_CMD;
            break;
        case BT_BUF_ISO_OUT:
            pkt_indicator = USB_BLUETOOTH_HCI_ISO;
            break;
        default:
            USB_LOG_ERR("Unknown type %u", bt_buf_get_type(buf));
            goto done;
    }

    err = usbh_bluetooth_hci_write(pkt_indicator, buf->data, buf->len);
    if (err < 0) {
        err = -EIO;
    } else {
        err = 0;
    }
done:
    net_buf_unref(buf);

    return err;
}

static const struct bt_hci_driver usbh_drv = {
    .name = "hci_usb",
    .open = bt_usbh_open,
    .send = bt_usbh_send,
    .bus = BT_HCI_DRIVER_BUS_USB,
#if defined(CONFIG_BT_DRIVER_QUIRK_NO_AUTO_DLE)
    .quirks = BT_QUIRK_NO_AUTO_DLE,
#endif
};

__WEAK void usbh_bluetooth_run_callback(void)
{
    /* bt_enable() */
}

__WEAK void usbh_bluetooth_stop_callback(void)
{
    /* bt_disable() */
}

void usbh_bluetooth_run(struct usbh_bluetooth *bluetooth_class)
{
    static bool s_registered;

    (void)bluetooth_class;

    if (!s_registered) {
        bt_hci_driver_register(&usbh_drv);

#ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
        usb_osal_thread_create("ble_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_rx_thread, NULL);
#else
        usb_osal_thread_create("ble_evt", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_evt_rx_thread, NULL);
        usb_osal_thread_create("ble_acl", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_acl_rx_thread, NULL);
#endif
        s_registered = true;
    }
    usbh_bluetooth_run_callback();
}

void usbh_bluetooth_stop(struct usbh_bluetooth *bluetooth_class)
{
    (void)bluetooth_class;
    usbh_bluetooth_stop_callback();
}

void usbh_bluetooth_hci_read_callback(uint8_t *data, uint32_t len)
{
    usbh_hci_host_rcv_pkt(data, len);
}