1 /*
2  * Copyright (c) 2024, sakumisu
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include "usbh_core.h"
7 #include "usbh_bluetooth.h"
8 
9 #undef USB_DBG_TAG
10 #define USB_DBG_TAG "usbh_bluetooth"
11 #include "usb_log.h"
12 
13 #define DEV_FORMAT "/dev/bluetooth"
14 
15 static struct usbh_bluetooth g_bluetooth_class;
16 
17 #ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
18 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)];
19 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)];
20 #else
21 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_cmd_buf[USB_ALIGN_UP(256, CONFIG_USB_ALIGN_SIZE)];
22 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_evt_buf[USB_ALIGN_UP(256, CONFIG_USB_ALIGN_SIZE)];
23 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)];
24 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)];
25 #endif
26 
usbh_bluetooth_connect(struct usbh_hubport * hport,uint8_t intf)27 static int usbh_bluetooth_connect(struct usbh_hubport *hport, uint8_t intf)
28 {
29     struct usb_endpoint_descriptor *ep_desc;
30     int ret = 0;
31 #ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
32     uint8_t mult;
33     uint16_t mps;
34 #endif
35 
36     struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class;
37 
38 #ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
39     if (intf != 0) {
40         return 0;
41     }
42 #endif
43 
44     memset(bluetooth_class, 0, sizeof(struct usbh_bluetooth));
45 
46     bluetooth_class->hport = hport;
47     bluetooth_class->intf = intf;
48 #ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
49     bluetooth_class->num_of_intf_altsettings = hport->config.intf[intf + 1].altsetting_num;
50 #endif
51     hport->config.intf[intf].priv = bluetooth_class;
52 
53     for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
54         ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
55 #ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
56         if (USB_GET_ENDPOINT_TYPE(ep_desc->bmAttributes) == USB_ENDPOINT_TYPE_INTERRUPT) {
57             if (ep_desc->bEndpointAddress & 0x80) {
58                 USBH_EP_INIT(bluetooth_class->intin, ep_desc);
59             } else {
60                 return -USB_ERR_NOTSUPP;
61             }
62         } else {
63 #endif
64             if (ep_desc->bEndpointAddress & 0x80) {
65                 USBH_EP_INIT(bluetooth_class->bulkin, ep_desc);
66             } else {
67                 USBH_EP_INIT(bluetooth_class->bulkout, ep_desc);
68             }
69 #ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
70         }
71 #endif
72     }
73 #ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
74     USB_LOG_INFO("Num of altsettings:%u\r\n", bluetooth_class->num_of_intf_altsettings);
75 
76     for (uint8_t i = 0; i < bluetooth_class->num_of_intf_altsettings; i++) {
77         USB_LOG_INFO("Altsetting:%u\r\n", i);
78         for (uint8_t j = 0; j < hport->config.intf[intf + 1].altsetting[i].intf_desc.bNumEndpoints; j++) {
79             ep_desc = &bluetooth_class->hport->config.intf[intf + 1].altsetting[i].ep[j].ep_desc;
80 
81             mult = USB_GET_MULT(ep_desc->wMaxPacketSize);
82             mps = USB_GET_MAXPACKETSIZE(ep_desc->wMaxPacketSize);
83 
84             USB_LOG_INFO("\tEp=%02x Attr=%02u Mps=%d Interval=%02u Mult=%02u\r\n",
85                          ep_desc->bEndpointAddress,
86                          ep_desc->bmAttributes,
87                          mps,
88                          ep_desc->bInterval,
89                          mult);
90         }
91     }
92 
93     ret = usbh_set_interface(hport, intf, 0);
94     if (ret < 0) {
95         return ret;
96     }
97     USB_LOG_INFO("Bluetooth select altsetting 0\r\n");
98 #endif
99     strncpy(hport->config.intf[intf].devname, DEV_FORMAT, CONFIG_USBHOST_DEV_NAMELEN);
100     USB_LOG_INFO("Register Bluetooth Class:%s\r\n", hport->config.intf[intf].devname);
101     usbh_bluetooth_run(bluetooth_class);
102     return ret;
103 }
104 
usbh_bluetooth_disconnect(struct usbh_hubport * hport,uint8_t intf)105 static int usbh_bluetooth_disconnect(struct usbh_hubport *hport, uint8_t intf)
106 {
107     int ret = 0;
108 
109     struct usbh_bluetooth *bluetooth_class = (struct usbh_bluetooth *)hport->config.intf[intf].priv;
110 
111     if (hport->config.config_desc.bNumInterfaces == (intf + 1)) {
112         return 0;
113     }
114 
115     if (bluetooth_class) {
116         if (bluetooth_class->bulkin) {
117             usbh_kill_urb(&bluetooth_class->bulkin_urb);
118         }
119 
120         if (bluetooth_class->bulkout) {
121             usbh_kill_urb(&bluetooth_class->bulkout_urb);
122         }
123 #ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
124         if (bluetooth_class->intin) {
125             usbh_kill_urb(&bluetooth_class->intin_urb);
126         }
127 
128         // if (bluetooth_class->isoin) {
129         //     usbh_kill_urb(&bluetooth_class->isoin_urb);
130         // }
131 
132         // if (bluetooth_class->isoin) {
133         //     usbh_kill_urb(&bluetooth_class->isoinin_urb);
134         // }
135 #endif
136         if (hport->config.intf[intf].devname[0] != '\0') {
137             usb_osal_thread_schedule_other();
138             USB_LOG_INFO("Unregister Bluetooth Class:%s\r\n", hport->config.intf[intf].devname);
139             usbh_bluetooth_stop(bluetooth_class);
140         }
141 
142         memset(bluetooth_class, 0, sizeof(struct usbh_bluetooth));
143     }
144 
145     return ret;
146 }
147 
148 #ifdef CONFIG_USBHOST_BLUETOOTH_HCI_LOG
usbh_bluetooth_hci_dump(uint8_t * data,uint32_t len)149 static void usbh_bluetooth_hci_dump(uint8_t *data, uint32_t len)
150 {
151     uint32_t i = 0;
152 
153     for (i = 0; i < len; i++) {
154         if (i % 16 == 0) {
155             USB_LOG_RAW("\r\n");
156         }
157 
158         USB_LOG_RAW("%02x ", data[i]);
159     }
160 
161     USB_LOG_RAW("\r\n");
162 }
163 #else
164 #define usbh_bluetooth_hci_dump(data, len)
165 #endif
166 
usbh_bluetooth_hci_bulk_out(uint8_t * buffer,uint32_t buflen)167 static int usbh_bluetooth_hci_bulk_out(uint8_t *buffer, uint32_t buflen)
168 {
169     struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class;
170     struct usbh_urb *urb = &bluetooth_class->bulkout_urb;
171     int ret;
172 
173     usbh_bulk_urb_fill(urb, bluetooth_class->hport, bluetooth_class->bulkout, buffer, buflen, USB_OSAL_WAITING_FOREVER, NULL, NULL);
174     ret = usbh_submit_urb(urb);
175     if (ret == 0) {
176         ret = urb->actual_length;
177     }
178     return ret;
179 }
180 
181 #ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
usbh_bluetooth_hci_write(uint8_t hci_type,uint8_t * buffer,uint32_t buflen)182 int usbh_bluetooth_hci_write(uint8_t hci_type, uint8_t *buffer, uint32_t buflen)
183 {
184     int ret;
185 
186     g_bluetooth_tx_buf[0] = hci_type;
187     memcpy(&g_bluetooth_tx_buf[1], buffer, buflen);
188     usbh_bluetooth_hci_dump(g_bluetooth_tx_buf, buflen + 1);
189     ret = usbh_bluetooth_hci_bulk_out(g_bluetooth_tx_buf, buflen + 1);
190     return ret;
191 }
192 
usbh_bluetooth_hci_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)193 void usbh_bluetooth_hci_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
194 {
195     int ret;
196     uint32_t ep_mps;
197     uint8_t retry = 0;
198     uint16_t actual_len = 0;
199 
200     ep_mps = USB_GET_MAXPACKETSIZE(g_bluetooth_class.bulkin->wMaxPacketSize);
201 
202     USB_LOG_INFO("Create hc rx thread\r\n");
203     while (1) {
204         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);
205         ret = usbh_submit_urb(&g_bluetooth_class.bulkin_urb);
206         if (ret < 0) {
207             if (ret == -USB_ERR_SHUTDOWN) {
208                 goto delete;
209             } else {
210                 retry++;
211                 if (retry == 3) {
212                     retry = 0;
213                     goto delete;
214                 }
215                 continue;
216             }
217         }
218         actual_len += g_bluetooth_class.bulkin_urb.actual_length;
219         if (g_bluetooth_class.bulkin_urb.actual_length != ep_mps) {
220             usbh_bluetooth_hci_dump(g_bluetooth_rx_buf, actual_len);
221             usbh_bluetooth_hci_read_callback(g_bluetooth_rx_buf, actual_len);
222             actual_len = 0;
223         } else {
224             /* read continue util read short packet */
225         }
226     }
227     // clang-format off
228 delete :
229     USB_LOG_INFO("Delete hc acl rx thread\r\n");
230     usb_osal_thread_delete(NULL);
231     // clang-format on
232 }
233 
234 #else
usbh_bluetooth_hci_cmd(uint8_t * buffer,uint32_t buflen)235 static int usbh_bluetooth_hci_cmd(uint8_t *buffer, uint32_t buflen)
236 {
237     struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class;
238     struct usb_setup_packet *setup;
239 
240     if (!bluetooth_class || !bluetooth_class->hport) {
241         return -USB_ERR_INVAL;
242     }
243     setup = bluetooth_class->hport->setup;
244 
245     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE;
246     setup->bRequest = 0x00;
247     setup->wValue = 0;
248     setup->wIndex = bluetooth_class->intf;
249     setup->wLength = buflen;
250 
251     return usbh_control_transfer(bluetooth_class->hport, setup, buffer);
252 }
253 
usbh_bluetooth_hci_write(uint8_t hci_type,uint8_t * buffer,uint32_t buflen)254 int usbh_bluetooth_hci_write(uint8_t hci_type, uint8_t *buffer, uint32_t buflen)
255 {
256     int ret;
257 
258     if (hci_type == USB_BLUETOOTH_HCI_CMD) {
259         g_bluetooth_cmd_buf[0] = USB_BLUETOOTH_HCI_CMD;
260         memcpy(&g_bluetooth_cmd_buf[1], buffer, buflen);
261         usbh_bluetooth_hci_dump(g_bluetooth_cmd_buf, buflen + 1);
262         ret = usbh_bluetooth_hci_cmd(&g_bluetooth_cmd_buf[1], buflen);
263     } else if (hci_type == USB_BLUETOOTH_HCI_ACL) {
264         g_bluetooth_tx_buf[0] = USB_BLUETOOTH_HCI_ACL;
265         memcpy(&g_bluetooth_tx_buf[1], buffer, buflen);
266         usbh_bluetooth_hci_dump(g_bluetooth_tx_buf, buflen + 1);
267         ret = usbh_bluetooth_hci_bulk_out(&g_bluetooth_tx_buf[1], buflen);
268     } else {
269         ret = -1;
270     }
271 
272     return ret;
273 }
274 
usbh_bluetooth_hci_evt_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)275 void usbh_bluetooth_hci_evt_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
276 {
277     int ret;
278     uint32_t ep_mps;
279     uint32_t interval;
280     uint8_t retry = 0;
281     uint16_t actual_len = 0;
282 
283     ep_mps = USB_GET_MAXPACKETSIZE(g_bluetooth_class.intin->wMaxPacketSize);
284     interval = g_bluetooth_class.intin->bInterval;
285 
286     USB_LOG_INFO("Create hc event rx thread\r\n");
287     while (1) {
288         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);
289         ret = usbh_submit_urb(&g_bluetooth_class.intin_urb);
290         if (ret < 0) {
291             if (ret == -USB_ERR_SHUTDOWN) {
292                 goto delete;
293             } else if (ret == -USB_ERR_NAK) {
294                 usb_osal_msleep(interval);
295                 continue;
296             } else {
297                 retry++;
298                 if (retry == 3) {
299                     retry = 0;
300                     goto delete;
301                 }
302                 usb_osal_msleep(interval);
303                 continue;
304             }
305         }
306         actual_len += g_bluetooth_class.intin_urb.actual_length;
307         if (g_bluetooth_class.intin_urb.actual_length != ep_mps) {
308             g_bluetooth_evt_buf[0] = USB_BLUETOOTH_HCI_EVT;
309             usbh_bluetooth_hci_dump(g_bluetooth_evt_buf, actual_len + 1);
310             usbh_bluetooth_hci_read_callback(g_bluetooth_evt_buf, actual_len + 1);
311             actual_len = 0;
312         } else {
313             /* read continue util read short packet */
314         }
315         usb_osal_msleep(interval);
316     }
317     // clang-format off
318 delete :
319     USB_LOG_INFO("Delete hc event rx thread\r\n");
320     usb_osal_thread_delete(NULL);
321     // clang-format on
322 }
323 
usbh_bluetooth_hci_acl_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)324 void usbh_bluetooth_hci_acl_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
325 {
326     int ret;
327     uint32_t ep_mps;
328     uint8_t retry = 0;
329     uint16_t actual_len = 0;
330 
331     ep_mps = USB_GET_MAXPACKETSIZE(g_bluetooth_class.bulkin->wMaxPacketSize);
332 
333     USB_LOG_INFO("Create hc acl rx thread\r\n");
334     while (1) {
335         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);
336         ret = usbh_submit_urb(&g_bluetooth_class.bulkin_urb);
337         if (ret < 0) {
338             if (ret == -USB_ERR_SHUTDOWN) {
339                 goto delete;
340             } else {
341                 retry++;
342                 if (retry == 3) {
343                     retry = 0;
344                     goto delete;
345                 }
346                 continue;
347             }
348         }
349         actual_len += g_bluetooth_class.bulkin_urb.actual_length;
350         if (g_bluetooth_class.bulkin_urb.actual_length != ep_mps) {
351             g_bluetooth_rx_buf[0] = USB_BLUETOOTH_HCI_ACL;
352             usbh_bluetooth_hci_dump(g_bluetooth_rx_buf, actual_len + 1);
353             usbh_bluetooth_hci_read_callback(g_bluetooth_rx_buf, actual_len + 1);
354             actual_len = 0;
355         } else {
356             /* read continue util read short packet */
357         }
358     }
359     // clang-format off
360 delete :
361     USB_LOG_INFO("Delete hc acl rx thread\r\n");
362     usb_osal_thread_delete(NULL);
363     // clang-format on
364 }
365 #endif
366 
usbh_bluetooth_hci_read_callback(uint8_t * data,uint32_t len)367 __WEAK void usbh_bluetooth_hci_read_callback(uint8_t *data, uint32_t len)
368 {
369     (void)data;
370     (void)len;
371 }
372 
usbh_bluetooth_run(struct usbh_bluetooth * bluetooth_class)373 __WEAK void usbh_bluetooth_run(struct usbh_bluetooth *bluetooth_class)
374 {
375     (void)bluetooth_class;
376 }
377 
usbh_bluetooth_stop(struct usbh_bluetooth * bluetooth_class)378 __WEAK void usbh_bluetooth_stop(struct usbh_bluetooth *bluetooth_class)
379 {
380     (void)bluetooth_class;
381 }
382 
383 static const struct usbh_class_driver bluetooth_class_driver = {
384     .driver_name = "bluetooth",
385     .connect = usbh_bluetooth_connect,
386     .disconnect = usbh_bluetooth_disconnect
387 };
388 
389 #ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
390 static const uint16_t bluetooth_id_table[][2] = {
391     { 0x2fe3, 0x000c },
392     { 0, 0 },
393 };
394 
395 CLASS_INFO_DEFINE const struct usbh_class_info bluetooth_h4_nrf_class_info = {
396     .match_flags = USB_CLASS_MATCH_VID_PID | USB_CLASS_MATCH_INTF_CLASS,
397     .bInterfaceClass = 0xff,
398     .bInterfaceSubClass = 0x00,
399     .bInterfaceProtocol = 0x00,
400     .id_table = bluetooth_id_table,
401     .class_driver = &bluetooth_class_driver
402 };
403 #else
404 CLASS_INFO_DEFINE const struct usbh_class_info bluetooth_class_info = {
405     .match_flags = USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
406     .bInterfaceClass = USB_DEVICE_CLASS_WIRELESS,
407     .bInterfaceSubClass = 0x01,
408     .bInterfaceProtocol = 0x01,
409     .id_table = NULL,
410     .class_driver = &bluetooth_class_driver
411 };
412 #endif
413