summaryrefslogtreecommitdiff
path: root/class/wireless/usbh_bluetooth.c
blob: af7167c01bf47ce1abbf3b83fb5943f6d6324c19 (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 * Copyright (c) 2024, sakumisu
 *
 * SPDX-License-Identifier: Apache-2.0
 */
#include "usbh_core.h"
#include "usbh_bluetooth.h"

#undef USB_DBG_TAG
#define USB_DBG_TAG "usbh_bluetooth"
#include "usb_log.h"

#define DEV_FORMAT "/dev/bluetooth"

static struct usbh_bluetooth g_bluetooth_class;

#ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_tx_buf[USB_ALIGN_UP(CONFIG_USBHOST_BLUETOOTH_TX_SIZE, CONFIG_USB_ALIGN_SIZE)];
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_rx_buf[USB_ALIGN_UP(CONFIG_USBHOST_BLUETOOTH_RX_SIZE, CONFIG_USB_ALIGN_SIZE)];
#else
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_cmd_buf[USB_ALIGN_UP(256, CONFIG_USB_ALIGN_SIZE)];
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_evt_buf[USB_ALIGN_UP(256, CONFIG_USB_ALIGN_SIZE)];
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_tx_buf[USB_ALIGN_UP(CONFIG_USBHOST_BLUETOOTH_TX_SIZE, CONFIG_USB_ALIGN_SIZE)];
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_rx_buf[USB_ALIGN_UP(CONFIG_USBHOST_BLUETOOTH_RX_SIZE, CONFIG_USB_ALIGN_SIZE)];
#endif

static int usbh_bluetooth_connect(struct usbh_hubport *hport, uint8_t intf)
{
    struct usb_endpoint_descriptor *ep_desc;
    int ret = 0;
#ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
    uint8_t mult;
    uint16_t mps;
#endif

    struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class;

#ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
    if (intf != 0) {
        return 0;
    }
#endif

    memset(bluetooth_class, 0, sizeof(struct usbh_bluetooth));

    bluetooth_class->hport = hport;
    bluetooth_class->intf = intf;
#ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
    bluetooth_class->num_of_intf_altsettings = hport->config.intf[intf + 1].altsetting_num;
#endif
    hport->config.intf[intf].priv = bluetooth_class;

    for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
        ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
#ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
        if (USB_GET_ENDPOINT_TYPE(ep_desc->bmAttributes) == USB_ENDPOINT_TYPE_INTERRUPT) {
            if (ep_desc->bEndpointAddress & 0x80) {
                USBH_EP_INIT(bluetooth_class->intin, ep_desc);
            } else {
                return -USB_ERR_NOTSUPP;
            }
        } else {
#endif
            if (ep_desc->bEndpointAddress & 0x80) {
                USBH_EP_INIT(bluetooth_class->bulkin, ep_desc);
            } else {
                USBH_EP_INIT(bluetooth_class->bulkout, ep_desc);
            }
#ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
        }
#endif
    }
#ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
    USB_LOG_INFO("Num of altsettings:%u\r\n", bluetooth_class->num_of_intf_altsettings);

    for (uint8_t i = 0; i < bluetooth_class->num_of_intf_altsettings; i++) {
        USB_LOG_INFO("Altsetting:%u\r\n", i);
        for (uint8_t j = 0; j < hport->config.intf[intf + 1].altsetting[i].intf_desc.bNumEndpoints; j++) {
            ep_desc = &bluetooth_class->hport->config.intf[intf + 1].altsetting[i].ep[j].ep_desc;

            mult = USB_GET_MULT(ep_desc->wMaxPacketSize);
            mps = USB_GET_MAXPACKETSIZE(ep_desc->wMaxPacketSize);

            USB_LOG_INFO("\tEp=%02x Attr=%02u Mps=%d Interval=%02u Mult=%02u\r\n",
                         ep_desc->bEndpointAddress,
                         ep_desc->bmAttributes,
                         mps,
                         ep_desc->bInterval,
                         mult);
        }
    }

    ret = usbh_set_interface(hport, intf, 0);
    if (ret < 0) {
        return ret;
    }
    USB_LOG_INFO("Bluetooth select altsetting 0\r\n");
#endif
    strncpy(hport->config.intf[intf].devname, DEV_FORMAT, CONFIG_USBHOST_DEV_NAMELEN);
    USB_LOG_INFO("Register Bluetooth Class:%s\r\n", hport->config.intf[intf].devname);
    usbh_bluetooth_run(bluetooth_class);
    return ret;
}

static int usbh_bluetooth_disconnect(struct usbh_hubport *hport, uint8_t intf)
{
    int ret = 0;

    struct usbh_bluetooth *bluetooth_class = (struct usbh_bluetooth *)hport->config.intf[intf].priv;

    if (hport->config.config_desc.bNumInterfaces == (intf + 1)) {
        return 0;
    }

    if (bluetooth_class) {
        if (bluetooth_class->bulkin) {
            usbh_kill_urb(&bluetooth_class->bulkin_urb);
        }

        if (bluetooth_class->bulkout) {
            usbh_kill_urb(&bluetooth_class->bulkout_urb);
        }
#ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
        if (bluetooth_class->intin) {
            usbh_kill_urb(&bluetooth_class->intin_urb);
        }

        // if (bluetooth_class->isoin) {
        //     usbh_kill_urb(&bluetooth_class->isoin_urb);
        // }

        // if (bluetooth_class->isoin) {
        //     usbh_kill_urb(&bluetooth_class->isoinin_urb);
        // }
#endif
        if (hport->config.intf[intf].devname[0] != '\0') {
            usb_osal_thread_schedule_other();
            USB_LOG_INFO("Unregister Bluetooth Class:%s\r\n", hport->config.intf[intf].devname);
            usbh_bluetooth_stop(bluetooth_class);
        }

        memset(bluetooth_class, 0, sizeof(struct usbh_bluetooth));
    }

    return ret;
}

#ifdef CONFIG_USBHOST_BLUETOOTH_HCI_LOG
static void usbh_bluetooth_hci_dump(uint8_t *data, uint32_t len)
{
    uint32_t i = 0;

    for (i = 0; i < len; i++) {
        if (i % 16 == 0) {
            USB_LOG_RAW("\r\n");
        }

        USB_LOG_RAW("%02x ", data[i]);
    }

    USB_LOG_RAW("\r\n");
}
#else
#define usbh_bluetooth_hci_dump(data, len)
#endif

static int usbh_bluetooth_hci_bulk_out(uint8_t *buffer, uint32_t buflen)
{
    struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class;
    struct usbh_urb *urb = &bluetooth_class->bulkout_urb;
    int ret;

    usbh_bulk_urb_fill(urb, bluetooth_class->hport, bluetooth_class->bulkout, buffer, buflen, USB_OSAL_WAITING_FOREVER, NULL, NULL);
    ret = usbh_submit_urb(urb);
    if (ret == 0) {
        ret = urb->actual_length;
    }
    return ret;
}

#ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
int usbh_bluetooth_hci_write(uint8_t hci_type, uint8_t *buffer, uint32_t buflen)
{
    int ret;

    g_bluetooth_tx_buf[0] = hci_type;
    memcpy(&g_bluetooth_tx_buf[1], buffer, buflen);
    usbh_bluetooth_hci_dump(g_bluetooth_tx_buf, buflen + 1);
    ret = usbh_bluetooth_hci_bulk_out(g_bluetooth_tx_buf, buflen + 1);
    return ret;
}

void usbh_bluetooth_hci_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
{
    int ret;
    uint32_t ep_mps;
    uint8_t retry = 0;
    uint16_t actual_len = 0;

    ep_mps = USB_GET_MAXPACKETSIZE(g_bluetooth_class.bulkin->wMaxPacketSize);

    USB_LOG_INFO("Create hc rx thread\r\n");
    while (1) {
        usbh_bulk_urb_fill(&g_bluetooth_class.bulkin_urb, g_bluetooth_class.hport, g_bluetooth_class.bulkin, &g_bluetooth_rx_buf[actual_len], ep_mps, USB_OSAL_WAITING_FOREVER, NULL, NULL);
        ret = usbh_submit_urb(&g_bluetooth_class.bulkin_urb);
        if (ret < 0) {
            if (ret == -USB_ERR_SHUTDOWN) {
                goto delete;
            } else {
                retry++;
                if (retry == 3) {
                    retry = 0;
                    goto delete;
                }
                continue;
            }
        }
        actual_len += g_bluetooth_class.bulkin_urb.actual_length;
        if (g_bluetooth_class.bulkin_urb.actual_length != ep_mps) {
            usbh_bluetooth_hci_dump(g_bluetooth_rx_buf, actual_len);
            usbh_bluetooth_hci_read_callback(g_bluetooth_rx_buf, actual_len);
            actual_len = 0;
        } else {
            /* read continue util read short packet */
        }
    }
    // clang-format off
delete :
    USB_LOG_INFO("Delete hc acl rx thread\r\n");
    usb_osal_thread_delete(NULL);
    // clang-format on
}

#else
static int usbh_bluetooth_hci_cmd(uint8_t *buffer, uint32_t buflen)
{
    struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class;
    struct usb_setup_packet *setup;

    if (!bluetooth_class || !bluetooth_class->hport) {
        return -USB_ERR_INVAL;
    }
    setup = bluetooth_class->hport->setup;

    setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE;
    setup->bRequest = 0x00;
    setup->wValue = 0;
    setup->wIndex = bluetooth_class->intf;
    setup->wLength = buflen;

    return usbh_control_transfer(bluetooth_class->hport, setup, buffer);
}

int usbh_bluetooth_hci_write(uint8_t hci_type, uint8_t *buffer, uint32_t buflen)
{
    int ret;

    if (hci_type == USB_BLUETOOTH_HCI_CMD) {
        g_bluetooth_cmd_buf[0] = USB_BLUETOOTH_HCI_CMD;
        memcpy(&g_bluetooth_cmd_buf[1], buffer, buflen);
        usbh_bluetooth_hci_dump(g_bluetooth_cmd_buf, buflen + 1);
        ret = usbh_bluetooth_hci_cmd(&g_bluetooth_cmd_buf[1], buflen);
    } else if (hci_type == USB_BLUETOOTH_HCI_ACL) {
        g_bluetooth_tx_buf[0] = USB_BLUETOOTH_HCI_ACL;
        memcpy(&g_bluetooth_tx_buf[1], buffer, buflen);
        usbh_bluetooth_hci_dump(g_bluetooth_tx_buf, buflen + 1);
        ret = usbh_bluetooth_hci_bulk_out(&g_bluetooth_tx_buf[1], buflen);
    } else {
        ret = -1;
    }

    return ret;
}

void usbh_bluetooth_hci_evt_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
{
    int ret;
    uint32_t ep_mps;
    uint32_t interval;
    uint8_t retry = 0;
    uint16_t actual_len = 0;

    ep_mps = USB_GET_MAXPACKETSIZE(g_bluetooth_class.intin->wMaxPacketSize);
    interval = g_bluetooth_class.intin->bInterval;

    USB_LOG_INFO("Create hc event rx thread\r\n");
    while (1) {
        usbh_int_urb_fill(&g_bluetooth_class.intin_urb, g_bluetooth_class.hport, g_bluetooth_class.intin, &g_bluetooth_evt_buf[1 + actual_len], ep_mps, USB_OSAL_WAITING_FOREVER, NULL, NULL);
        ret = usbh_submit_urb(&g_bluetooth_class.intin_urb);
        if (ret < 0) {
            if (ret == -USB_ERR_SHUTDOWN) {
                goto delete;
            } else if (ret == -USB_ERR_NAK) {
                usb_osal_msleep(interval);
                continue;
            } else {
                retry++;
                if (retry == 3) {
                    retry = 0;
                    goto delete;
                }
                usb_osal_msleep(interval);
                continue;
            }
        }
        actual_len += g_bluetooth_class.intin_urb.actual_length;
        if (g_bluetooth_class.intin_urb.actual_length != ep_mps) {
            g_bluetooth_evt_buf[0] = USB_BLUETOOTH_HCI_EVT;
            usbh_bluetooth_hci_dump(g_bluetooth_evt_buf, actual_len + 1);
            usbh_bluetooth_hci_read_callback(g_bluetooth_evt_buf, actual_len + 1);
            actual_len = 0;
        } else {
            /* read continue util read short packet */
        }
        usb_osal_msleep(interval);
    }
    // clang-format off
delete :
    USB_LOG_INFO("Delete hc event rx thread\r\n");
    usb_osal_thread_delete(NULL);
    // clang-format on
}

void usbh_bluetooth_hci_acl_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
{
    int ret;
    uint32_t ep_mps;
    uint8_t retry = 0;
    uint16_t actual_len = 0;

    ep_mps = USB_GET_MAXPACKETSIZE(g_bluetooth_class.bulkin->wMaxPacketSize);

    USB_LOG_INFO("Create hc acl rx thread\r\n");
    while (1) {
        usbh_bulk_urb_fill(&g_bluetooth_class.bulkin_urb, g_bluetooth_class.hport, g_bluetooth_class.bulkin, &g_bluetooth_rx_buf[1 + actual_len], ep_mps, USB_OSAL_WAITING_FOREVER, NULL, NULL);
        ret = usbh_submit_urb(&g_bluetooth_class.bulkin_urb);
        if (ret < 0) {
            if (ret == -USB_ERR_SHUTDOWN) {
                goto delete;
            } else {
                retry++;
                if (retry == 3) {
                    retry = 0;
                    goto delete;
                }
                continue;
            }
        }
        actual_len += g_bluetooth_class.bulkin_urb.actual_length;
        if (g_bluetooth_class.bulkin_urb.actual_length != ep_mps) {
            g_bluetooth_rx_buf[0] = USB_BLUETOOTH_HCI_ACL;
            usbh_bluetooth_hci_dump(g_bluetooth_rx_buf, actual_len + 1);
            usbh_bluetooth_hci_read_callback(g_bluetooth_rx_buf, actual_len + 1);
            actual_len = 0;
        } else {
            /* read continue util read short packet */
        }
    }
    // clang-format off
delete :
    USB_LOG_INFO("Delete hc acl rx thread\r\n");
    usb_osal_thread_delete(NULL);
    // clang-format on
}
#endif

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

__WEAK void usbh_bluetooth_run(struct usbh_bluetooth *bluetooth_class)
{
    (void)bluetooth_class;
}

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

static const struct usbh_class_driver bluetooth_class_driver = {
    .driver_name = "bluetooth",
    .connect = usbh_bluetooth_connect,
    .disconnect = usbh_bluetooth_disconnect
};

#ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
static const uint16_t bluetooth_id_table[][2] = {
    { 0x2fe3, 0x000c },
    { 0, 0 },
};

CLASS_INFO_DEFINE const struct usbh_class_info bluetooth_h4_nrf_class_info = {
    .match_flags = USB_CLASS_MATCH_VID_PID | USB_CLASS_MATCH_INTF_CLASS,
    .bInterfaceClass = 0xff,
    .bInterfaceSubClass = 0x00,
    .bInterfaceProtocol = 0x00,
    .id_table = bluetooth_id_table,
    .class_driver = &bluetooth_class_driver
};
#else
CLASS_INFO_DEFINE const struct usbh_class_info bluetooth_class_info = {
    .match_flags = USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
    .bInterfaceClass = USB_DEVICE_CLASS_WIRELESS,
    .bInterfaceSubClass = 0x01,
    .bInterfaceProtocol = 0x01,
    .id_table = NULL,
    .class_driver = &bluetooth_class_driver
};
#endif