1 /*
2  * Copyright (c) 2018 Intel Corporation
3  * Copyright (c) 2023 Nordic Semiconductor ASA
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 /* Bluetooth HCI USB transport layer implementation with following
9  * endpoint configuration:
10  *  - HCI commands through control endpoint (host-to-device only)
11  *  - HCI events through interrupt IN endpoint
12  *  - ACL data through one bulk IN and one bulk OUT endpoints
13  *
14  * TODO: revise transfer handling so that Bluetooth net_bufs can be used
15  * directly without intermediate copying.
16  *
17  * Limitations:
18  *  - Remote wakeup before IN transfer is not yet supported.
19  *  - H4 transport layer is not yet supported
20  */
21 
22 #include <zephyr/init.h>
23 #include <zephyr/sys/byteorder.h>
24 
25 #include <zephyr/usb/usbd.h>
26 #include <zephyr/usb/usb_ch9.h>
27 #include <zephyr/drivers/usb/udc.h>
28 
29 #include <zephyr/net/buf.h>
30 
31 #include <zephyr/bluetooth/buf.h>
32 #include <zephyr/bluetooth/hci_raw.h>
33 #include <zephyr/bluetooth/l2cap.h>
34 #include <zephyr/bluetooth/hci_vs.h>
35 #include <zephyr/drivers/bluetooth/hci_driver.h>
36 #include <zephyr/sys/atomic.h>
37 
38 #include <zephyr/logging/log.h>
39 LOG_MODULE_REGISTER(bt_hci, CONFIG_USBD_BT_HCI_LOG_LEVEL);
40 
41 #define BT_HCI_SUBCLASS		0x01
42 #define BT_HCI_PROTOCOL		0x01
43 
44 /*
45  * The actual endpoint addresses may differ from the following because
46  * the resources may be reallocated by the stack depending on the number
47  * of functions in a configuration and the properties of the controller.
48  */
49 #define BT_HCI_EP_EVENTS		0x81
50 #define BT_HCI_EP_ACL_DATA_IN		0x82
51 #define BT_HCI_EP_ACL_DATA_OUT		0x02
52 #define BT_HCI_EP_VOICE_IN		0x83
53 #define BT_HCI_EP_VOICE_OUT		0x03
54 
55 #define BT_HCI_EP_MPS_EVENTS		16
56 #define BT_HCI_EP_FS_MPS_ACL_DATA	64
57 #define BT_HCI_EP_HS_MPS_ACL_DATA	512
58 #define BT_HCI_EP_MPS_VOICE		9
59 
60 #define BT_HCI_EP_INTERVAL_EVENTS	1
61 #define BT_HCI_EP_INTERVAL_VOICE	3
62 
63 #define BT_HCI_CLASS_ENABLED		0
64 #define BT_HCI_ACL_RX_ENGAGED		1
65 
66 static K_FIFO_DEFINE(bt_hci_rx_queue);
67 static K_FIFO_DEFINE(bt_hci_tx_queue);
68 
69 /*
70  * Transfers through three endpoints proceed in a synchronous manner,
71  * with maximum packet size of high speed bulk endpoint.
72  *
73  * REVISE: global (bulk, interrupt, iso) specific pools would be more
74  * RAM usage efficient.
75  */
76 UDC_BUF_POOL_DEFINE(bt_hci_ep_pool,
77 		    3, 512,
78 		    sizeof(struct udc_buf_info), NULL);
79 /* HCI RX/TX threads */
80 static K_KERNEL_STACK_DEFINE(rx_thread_stack, CONFIG_BT_HCI_TX_STACK_SIZE);
81 static struct k_thread rx_thread_data;
82 static K_KERNEL_STACK_DEFINE(tx_thread_stack, CONFIG_USBD_BT_HCI_TX_STACK_SIZE);
83 static struct k_thread tx_thread_data;
84 
85 /*
86  * We do not support voice channels and we do not implement
87  * isochronous endpoints handling, these are only available to match
88  * the recomendet configuration in the Bluetooth specification and
89  * avoid issues with Linux kernel btusb driver.
90  */
91 struct usbd_bt_hci_desc {
92 	struct usb_association_descriptor iad;
93 	struct usb_if_descriptor if0;
94 	struct usb_ep_descriptor if0_int_ep;
95 	struct usb_ep_descriptor if0_in_ep;
96 	struct usb_ep_descriptor if0_out_ep;
97 	struct usb_ep_descriptor if0_hs_in_ep;
98 	struct usb_ep_descriptor if0_hs_out_ep;
99 
100 	struct usb_if_descriptor if1_0;
101 	struct usb_ep_descriptor if1_0_iso_in_ep;
102 	struct usb_ep_descriptor if1_0_iso_out_ep;
103 	struct usb_if_descriptor if1_1;
104 	struct usb_ep_descriptor if1_1_iso_in_ep;
105 	struct usb_ep_descriptor if1_1_iso_out_ep;
106 
107 	struct usb_desc_header nil_desc;
108 };
109 
110 struct bt_hci_data {
111 	struct net_buf *acl_buf;
112 	struct usbd_bt_hci_desc *const desc;
113 	const struct usb_desc_header **const fs_desc;
114 	const struct usb_desc_header **const hs_desc;
115 	uint16_t acl_len;
116 	struct k_sem sync_sem;
117 	atomic_t state;
118 };
119 
120 /*
121  * Make supported device request visible for the stack
122  * bRequest 0x00 and bRequest 0xE0.
123  */
124 static const struct usbd_cctx_vendor_req bt_hci_vregs =
125 	USBD_VENDOR_REQ(0x00, 0xe0);
126 
bt_hci_get_int_in(struct usbd_class_data * const c_data)127 static uint8_t bt_hci_get_int_in(struct usbd_class_data *const c_data)
128 {
129 	struct bt_hci_data *data = usbd_class_get_private(c_data);
130 	struct usbd_bt_hci_desc *desc = data->desc;
131 
132 	return desc->if0_int_ep.bEndpointAddress;
133 }
134 
bt_hci_get_bulk_in(struct usbd_class_data * const c_data)135 static uint8_t bt_hci_get_bulk_in(struct usbd_class_data *const c_data)
136 {
137 	struct usbd_context *uds_ctx = usbd_class_get_ctx(c_data);
138 	struct bt_hci_data *data = usbd_class_get_private(c_data);
139 	struct usbd_bt_hci_desc *desc = data->desc;
140 
141 	if (usbd_bus_speed(uds_ctx) == USBD_SPEED_HS) {
142 		return desc->if0_hs_in_ep.bEndpointAddress;
143 	}
144 
145 	return desc->if0_in_ep.bEndpointAddress;
146 }
147 
bt_hci_get_bulk_out(struct usbd_class_data * const c_data)148 static uint8_t bt_hci_get_bulk_out(struct usbd_class_data *const c_data)
149 {
150 	struct usbd_context *uds_ctx = usbd_class_get_ctx(c_data);
151 	struct bt_hci_data *data = usbd_class_get_private(c_data);
152 	struct usbd_bt_hci_desc *desc = data->desc;
153 
154 	if (usbd_bus_speed(uds_ctx) == USBD_SPEED_HS) {
155 		return desc->if0_hs_out_ep.bEndpointAddress;
156 	}
157 
158 	return desc->if0_out_ep.bEndpointAddress;
159 }
160 
bt_hci_buf_alloc(const uint8_t ep)161 struct net_buf *bt_hci_buf_alloc(const uint8_t ep)
162 {
163 	struct net_buf *buf = NULL;
164 	struct udc_buf_info *bi;
165 
166 	buf = net_buf_alloc(&bt_hci_ep_pool, K_NO_WAIT);
167 	if (!buf) {
168 		return NULL;
169 	}
170 
171 	bi = udc_get_buf_info(buf);
172 	memset(bi, 0, sizeof(struct udc_buf_info));
173 	bi->ep = ep;
174 
175 	return buf;
176 }
177 
bt_hci_tx_sync_in(struct usbd_class_data * const c_data,struct net_buf * const bt_buf,const uint8_t ep)178 static void bt_hci_tx_sync_in(struct usbd_class_data *const c_data,
179 			      struct net_buf *const bt_buf, const uint8_t ep)
180 {
181 	struct bt_hci_data *hci_data = usbd_class_get_private(c_data);
182 	struct net_buf *buf;
183 
184 	buf = bt_hci_buf_alloc(ep);
185 	if (buf == NULL) {
186 		LOG_ERR("Failed to allocate buffer");
187 		return;
188 	}
189 
190 	net_buf_add_mem(buf, bt_buf->data, bt_buf->len);
191 	usbd_ep_enqueue(c_data, buf);
192 	k_sem_take(&hci_data->sync_sem, K_FOREVER);
193 	net_buf_unref(buf);
194 }
195 
bt_hci_tx_thread(void * p1,void * p2,void * p3)196 static void bt_hci_tx_thread(void *p1, void *p2, void *p3)
197 {
198 	struct usbd_class_data *const c_data = p1;
199 
200 	ARG_UNUSED(p2);
201 	ARG_UNUSED(p3);
202 
203 	while (true) {
204 		struct net_buf *bt_buf;
205 		uint8_t ep;
206 
207 		bt_buf = net_buf_get(&bt_hci_tx_queue, K_FOREVER);
208 
209 		switch (bt_buf_get_type(bt_buf)) {
210 		case BT_BUF_EVT:
211 			ep = bt_hci_get_int_in(c_data);
212 			break;
213 		case BT_BUF_ACL_IN:
214 			ep = bt_hci_get_bulk_in(c_data);
215 			break;
216 		default:
217 			LOG_ERR("Unknown type %u", bt_buf_get_type(bt_buf));
218 			continue;
219 		}
220 
221 
222 		bt_hci_tx_sync_in(c_data, bt_buf, ep);
223 		net_buf_unref(bt_buf);
224 	}
225 }
226 
bt_hci_rx_thread(void * a,void * b,void * c)227 static void bt_hci_rx_thread(void *a, void *b, void *c)
228 {
229 	while (true) {
230 		struct net_buf *buf;
231 		int err;
232 
233 		/* FIXME: Do we need a separate thread for bt_send()? */
234 		buf = net_buf_get(&bt_hci_rx_queue, K_FOREVER);
235 
236 		err = bt_send(buf);
237 		if (err) {
238 			LOG_ERR("Error sending to driver");
239 			net_buf_unref(buf);
240 		}
241 	}
242 }
243 
bt_hci_acl_out_start(struct usbd_class_data * const c_data)244 static int bt_hci_acl_out_start(struct usbd_class_data *const c_data)
245 {
246 	struct bt_hci_data *hci_data = usbd_class_get_private(c_data);
247 	struct net_buf *buf;
248 	uint8_t ep;
249 	int ret;
250 
251 	if (!atomic_test_bit(&hci_data->state, BT_HCI_CLASS_ENABLED)) {
252 		return -EPERM;
253 	}
254 
255 	if (atomic_test_and_set_bit(&hci_data->state, BT_HCI_ACL_RX_ENGAGED)) {
256 		return -EBUSY;
257 	}
258 
259 	ep = bt_hci_get_bulk_out(c_data);
260 	buf = bt_hci_buf_alloc(ep);
261 	if (buf == NULL) {
262 		return -ENOMEM;
263 	}
264 
265 	ret = usbd_ep_enqueue(c_data, buf);
266 	if (ret) {
267 		LOG_ERR("Failed to enqueue net_buf for 0x%02x", ep);
268 		net_buf_unref(buf);
269 	}
270 
271 	return  ret;
272 }
273 
hci_pkt_get_len(struct net_buf * const buf,const uint8_t * data,const size_t size)274 static uint16_t hci_pkt_get_len(struct net_buf *const buf,
275 				const uint8_t *data, const size_t size)
276 {
277 	size_t hdr_len = 0;
278 	uint16_t len = 0;
279 
280 	switch (bt_buf_get_type(buf)) {
281 	case BT_BUF_CMD: {
282 		struct bt_hci_cmd_hdr *cmd_hdr;
283 
284 		hdr_len = sizeof(*cmd_hdr);
285 		cmd_hdr = (struct bt_hci_cmd_hdr *)data;
286 		len = cmd_hdr->param_len + hdr_len;
287 		break;
288 	}
289 	case BT_BUF_ACL_OUT: {
290 		struct bt_hci_acl_hdr *acl_hdr;
291 
292 		hdr_len = sizeof(*acl_hdr);
293 		acl_hdr = (struct bt_hci_acl_hdr *)data;
294 		len = sys_le16_to_cpu(acl_hdr->len) + hdr_len;
295 		break;
296 	}
297 	case BT_BUF_ISO_OUT: {
298 		struct bt_hci_iso_hdr *iso_hdr;
299 
300 		hdr_len = sizeof(*iso_hdr);
301 		iso_hdr = (struct bt_hci_iso_hdr *)data;
302 		len = bt_iso_hdr_len(sys_le16_to_cpu(iso_hdr->len)) + hdr_len;
303 		break;
304 	}
305 	default:
306 		LOG_ERR("Unknown BT buffer type");
307 		return 0;
308 	}
309 
310 	return (size < hdr_len) ? 0 : len;
311 }
312 
bt_hci_acl_out_cb(struct usbd_class_data * const c_data,struct net_buf * const buf,const int err)313 static int bt_hci_acl_out_cb(struct usbd_class_data *const c_data,
314 			     struct net_buf *const buf, const int err)
315 {
316 	struct bt_hci_data *hci_data = usbd_class_get_private(c_data);
317 
318 	if (err) {
319 		goto restart_out_transfer;
320 	}
321 
322 	if (hci_data->acl_buf == NULL) {
323 		hci_data->acl_buf = bt_buf_get_tx(BT_BUF_ACL_OUT, K_FOREVER,
324 						  buf->data, buf->len);
325 		if (hci_data->acl_buf == NULL) {
326 			LOG_ERR("Failed to allocate net_buf");
327 			goto restart_out_transfer;
328 		}
329 
330 		hci_data->acl_len = hci_pkt_get_len(hci_data->acl_buf,
331 						    buf->data,
332 						    buf->len);
333 		LOG_DBG("acl_len %u, chunk %u", hci_data->acl_len, buf->len);
334 
335 		if (hci_data->acl_len == 0) {
336 			LOG_ERR("Failed to get packet length");
337 			net_buf_unref(hci_data->acl_buf);
338 			hci_data->acl_buf = NULL;
339 		}
340 	} else {
341 		if (net_buf_tailroom(hci_data->acl_buf) < buf->len) {
342 			LOG_ERR("Buffer tailroom too small");
343 			net_buf_unref(hci_data->acl_buf);
344 			hci_data->acl_buf = NULL;
345 			goto restart_out_transfer;
346 		}
347 
348 		/*
349 		 * Take over the next chunk if HCI packet is
350 		 * larger than USB_MAX_FS_BULK_MPS.
351 		 */
352 		net_buf_add_mem(hci_data->acl_buf, buf->data, buf->len);
353 		LOG_INF("len %u, chunk %u", hci_data->acl_buf->len, buf->len);
354 	}
355 
356 	if (hci_data->acl_buf != NULL && hci_data->acl_len == hci_data->acl_buf->len) {
357 		net_buf_put(&bt_hci_rx_queue, hci_data->acl_buf);
358 		hci_data->acl_buf = NULL;
359 		hci_data->acl_len = 0;
360 	}
361 
362 restart_out_transfer:
363 	/* TODO: add function to reset transfer buffer and allow to reuse it */
364 	net_buf_unref(buf);
365 	atomic_clear_bit(&hci_data->state, BT_HCI_ACL_RX_ENGAGED);
366 
367 	return bt_hci_acl_out_start(c_data);
368 }
369 
bt_hci_request(struct usbd_class_data * const c_data,struct net_buf * buf,int err)370 static int bt_hci_request(struct usbd_class_data *const c_data,
371 			  struct net_buf *buf, int err)
372 {
373 	struct usbd_context *uds_ctx = usbd_class_get_ctx(c_data);
374 	struct bt_hci_data *hci_data = usbd_class_get_private(c_data);
375 	struct udc_buf_info *bi;
376 
377 	bi = udc_get_buf_info(buf);
378 
379 	if (bi->ep == bt_hci_get_bulk_out(c_data)) {
380 		return bt_hci_acl_out_cb(c_data, buf, err);
381 	}
382 
383 	if (bi->ep == bt_hci_get_bulk_in(c_data) ||
384 	    bi->ep == bt_hci_get_int_in(c_data)) {
385 		k_sem_give(&hci_data->sync_sem);
386 
387 		return 0;
388 	}
389 
390 	return usbd_ep_buf_free(uds_ctx, buf);
391 }
392 
bt_hci_update(struct usbd_class_data * const c_data,uint8_t iface,uint8_t alternate)393 static void bt_hci_update(struct usbd_class_data *const c_data,
394 			  uint8_t iface, uint8_t alternate)
395 {
396 	LOG_DBG("New configuration, interface %u alternate %u",
397 		iface, alternate);
398 }
399 
bt_hci_enable(struct usbd_class_data * const c_data)400 static void bt_hci_enable(struct usbd_class_data *const c_data)
401 {
402 	struct bt_hci_data *hci_data = usbd_class_get_private(c_data);
403 
404 	atomic_set_bit(&hci_data->state, BT_HCI_CLASS_ENABLED);
405 	LOG_INF("Configuration enabled");
406 
407 	if (bt_hci_acl_out_start(c_data)) {
408 		LOG_ERR("Failed to start ACL OUT transfer");
409 	}
410 }
411 
bt_hci_disable(struct usbd_class_data * const c_data)412 static void bt_hci_disable(struct usbd_class_data *const c_data)
413 {
414 	struct bt_hci_data *hci_data = usbd_class_get_private(c_data);
415 
416 	atomic_clear_bit(&hci_data->state, BT_HCI_CLASS_ENABLED);
417 	LOG_INF("Configuration disabled");
418 }
419 
bt_hci_ctd(struct usbd_class_data * const c_data,const struct usb_setup_packet * const setup,const struct net_buf * const buf)420 static int bt_hci_ctd(struct usbd_class_data *const c_data,
421 		      const struct usb_setup_packet *const setup,
422 		      const struct net_buf *const buf)
423 {
424 	struct net_buf *cmd_buf;
425 
426 	/* We expect host-to-device class request */
427 	if (setup->RequestType.type != USB_REQTYPE_TYPE_CLASS) {
428 		errno = -ENOTSUP;
429 
430 		return 0;
431 	}
432 
433 	LOG_DBG("bmRequestType 0x%02x bRequest 0x%02x",
434 		setup->bmRequestType, setup->bRequest);
435 
436 	cmd_buf = bt_buf_get_tx(BT_BUF_CMD, K_NO_WAIT, buf->data, buf->len);
437 	if (!cmd_buf) {
438 		LOG_ERR("Cannot get free buffer");
439 
440 		return -ENOMEM;
441 	}
442 
443 	net_buf_put(&bt_hci_rx_queue, cmd_buf);
444 
445 	return 0;
446 }
447 
bt_hci_get_desc(struct usbd_class_data * const c_data,const enum usbd_speed speed)448 static void *bt_hci_get_desc(struct usbd_class_data *const c_data,
449 			     const enum usbd_speed speed)
450 {
451 	struct bt_hci_data *data = usbd_class_get_private(c_data);
452 
453 	if (speed == USBD_SPEED_HS) {
454 		return data->hs_desc;
455 	}
456 
457 	return data->fs_desc;
458 }
459 
bt_hci_init(struct usbd_class_data * const c_data)460 static int bt_hci_init(struct usbd_class_data *const c_data)
461 {
462 
463 	struct bt_hci_data *data = usbd_class_get_private(c_data);
464 	struct usbd_bt_hci_desc *desc = data->desc;
465 
466 	desc->iad.bFirstInterface = desc->if0.bInterfaceNumber;
467 
468 	return 0;
469 }
470 
471 static struct usbd_class_api bt_hci_api = {
472 	.request = bt_hci_request,
473 	.update = bt_hci_update,
474 	.enable = bt_hci_enable,
475 	.disable = bt_hci_disable,
476 	.control_to_dev = bt_hci_ctd,
477 	.get_desc = bt_hci_get_desc,
478 	.init = bt_hci_init,
479 };
480 
481 #define BT_HCI_DESCRIPTOR_DEFINE(n)						\
482 static struct usbd_bt_hci_desc bt_hci_desc_##n = {				\
483 	.iad = {								\
484 		.bLength = sizeof(struct usb_association_descriptor),		\
485 		.bDescriptorType = USB_DESC_INTERFACE_ASSOC,			\
486 		.bFirstInterface = 0,						\
487 		.bInterfaceCount = 0x02,					\
488 		.bFunctionClass = USB_BCC_WIRELESS_CONTROLLER,			\
489 		.bFunctionSubClass = BT_HCI_SUBCLASS,				\
490 		.bFunctionProtocol = BT_HCI_PROTOCOL,				\
491 		.iFunction = 0,							\
492 	},									\
493 										\
494 	.if0 = {								\
495 		.bLength = sizeof(struct usb_if_descriptor),			\
496 		.bDescriptorType = USB_DESC_INTERFACE,				\
497 		.bInterfaceNumber = 0,						\
498 		.bAlternateSetting = 0,						\
499 		.bNumEndpoints = 3,						\
500 		.bInterfaceClass = USB_BCC_WIRELESS_CONTROLLER,			\
501 		.bInterfaceSubClass = BT_HCI_SUBCLASS,				\
502 		.bInterfaceProtocol = BT_HCI_PROTOCOL,				\
503 		.iInterface = 0,						\
504 	},									\
505 										\
506 	.if0_int_ep = {								\
507 		.bLength = sizeof(struct usb_ep_descriptor),			\
508 		.bDescriptorType = USB_DESC_ENDPOINT,				\
509 		.bEndpointAddress = BT_HCI_EP_EVENTS,				\
510 		.bmAttributes = USB_EP_TYPE_INTERRUPT,				\
511 		.wMaxPacketSize = sys_cpu_to_le16(BT_HCI_EP_MPS_EVENTS),	\
512 		.bInterval = BT_HCI_EP_INTERVAL_EVENTS,				\
513 	},									\
514 										\
515 	.if0_in_ep = {								\
516 		.bLength = sizeof(struct usb_ep_descriptor),			\
517 		.bDescriptorType = USB_DESC_ENDPOINT,				\
518 		.bEndpointAddress = BT_HCI_EP_ACL_DATA_IN,			\
519 		.bmAttributes = USB_EP_TYPE_BULK,				\
520 		.wMaxPacketSize = sys_cpu_to_le16(BT_HCI_EP_FS_MPS_ACL_DATA),	\
521 		.bInterval = 0,							\
522 	},									\
523 										\
524 	.if0_out_ep = {								\
525 		.bLength = sizeof(struct usb_ep_descriptor),			\
526 		.bDescriptorType = USB_DESC_ENDPOINT,				\
527 		.bEndpointAddress = BT_HCI_EP_ACL_DATA_OUT,			\
528 		.bmAttributes = USB_EP_TYPE_BULK,				\
529 		.wMaxPacketSize = sys_cpu_to_le16(BT_HCI_EP_FS_MPS_ACL_DATA),	\
530 		.bInterval = 0,							\
531 	},									\
532 										\
533 	.if0_hs_in_ep = {							\
534 		.bLength = sizeof(struct usb_ep_descriptor),			\
535 		.bDescriptorType = USB_DESC_ENDPOINT,				\
536 		.bEndpointAddress = BT_HCI_EP_ACL_DATA_IN,			\
537 		.bmAttributes = USB_EP_TYPE_BULK,				\
538 		.wMaxPacketSize = sys_cpu_to_le16(BT_HCI_EP_HS_MPS_ACL_DATA),	\
539 		.bInterval = 0,							\
540 	},									\
541 										\
542 	.if0_hs_out_ep = {							\
543 		.bLength = sizeof(struct usb_ep_descriptor),			\
544 		.bDescriptorType = USB_DESC_ENDPOINT,				\
545 		.bEndpointAddress = BT_HCI_EP_ACL_DATA_OUT,			\
546 		.bmAttributes = USB_EP_TYPE_BULK,				\
547 		.wMaxPacketSize = sys_cpu_to_le16(BT_HCI_EP_HS_MPS_ACL_DATA),	\
548 		.bInterval = 0,							\
549 	},									\
550 	.if1_0 = {								\
551 		.bLength = sizeof(struct usb_if_descriptor),			\
552 		.bDescriptorType = USB_DESC_INTERFACE,				\
553 		.bInterfaceNumber = 1,						\
554 		.bAlternateSetting = 0,						\
555 		.bNumEndpoints = 2,						\
556 		.bInterfaceClass = USB_BCC_WIRELESS_CONTROLLER,			\
557 		.bInterfaceSubClass = BT_HCI_SUBCLASS,				\
558 		.bInterfaceProtocol = BT_HCI_PROTOCOL,				\
559 		.iInterface = 0,						\
560 	},									\
561 										\
562 	.if1_0_iso_in_ep = {							\
563 		.bLength = sizeof(struct usb_ep_descriptor),			\
564 		.bDescriptorType = USB_DESC_ENDPOINT,				\
565 		.bEndpointAddress = BT_HCI_EP_VOICE_IN,				\
566 		.bmAttributes = USB_EP_TYPE_ISO,				\
567 		.wMaxPacketSize = 0,						\
568 		.bInterval = BT_HCI_EP_INTERVAL_VOICE,				\
569 	},									\
570 										\
571 	.if1_0_iso_out_ep = {							\
572 		.bLength = sizeof(struct usb_ep_descriptor),			\
573 		.bDescriptorType = USB_DESC_ENDPOINT,				\
574 		.bEndpointAddress = BT_HCI_EP_VOICE_OUT,			\
575 		.bmAttributes = USB_EP_TYPE_ISO,				\
576 		.wMaxPacketSize = 0,						\
577 		.bInterval = BT_HCI_EP_INTERVAL_VOICE,				\
578 	},									\
579 										\
580 	.if1_1 = {								\
581 		.bLength = sizeof(struct usb_if_descriptor),			\
582 		.bDescriptorType = USB_DESC_INTERFACE,				\
583 		.bInterfaceNumber = 1,						\
584 		.bAlternateSetting = 1,						\
585 		.bNumEndpoints = 2,						\
586 		.bInterfaceClass = USB_BCC_WIRELESS_CONTROLLER,			\
587 		.bInterfaceSubClass = BT_HCI_SUBCLASS,				\
588 		.bInterfaceProtocol = BT_HCI_PROTOCOL,				\
589 		.iInterface = 0,						\
590 	},									\
591 										\
592 	.if1_1_iso_in_ep = {							\
593 		.bLength = sizeof(struct usb_ep_descriptor),			\
594 		.bDescriptorType = USB_DESC_ENDPOINT,				\
595 		.bEndpointAddress = BT_HCI_EP_VOICE_IN,				\
596 		.bmAttributes = USB_EP_TYPE_ISO,				\
597 		.wMaxPacketSize = BT_HCI_EP_MPS_VOICE,				\
598 		.bInterval = BT_HCI_EP_INTERVAL_VOICE,				\
599 	},									\
600 										\
601 	.if1_1_iso_out_ep = {							\
602 		.bLength = sizeof(struct usb_ep_descriptor),			\
603 		.bDescriptorType = USB_DESC_ENDPOINT,				\
604 		.bEndpointAddress = BT_HCI_EP_VOICE_OUT,			\
605 		.bmAttributes = USB_EP_TYPE_ISO,				\
606 		.wMaxPacketSize = BT_HCI_EP_MPS_VOICE,				\
607 		.bInterval = BT_HCI_EP_INTERVAL_VOICE,				\
608 	},									\
609 										\
610 	.nil_desc = {								\
611 		.bLength = 0,							\
612 		.bDescriptorType = 0,						\
613 	},									\
614 };										\
615 										\
616 const static struct usb_desc_header *bt_hci_fs_desc_##n[] = {			\
617 	(struct usb_desc_header *) &bt_hci_desc_##n.iad,			\
618 	(struct usb_desc_header *) &bt_hci_desc_##n.if0,			\
619 	(struct usb_desc_header *) &bt_hci_desc_##n.if0_int_ep,			\
620 	(struct usb_desc_header *) &bt_hci_desc_##n.if0_in_ep,			\
621 	(struct usb_desc_header *) &bt_hci_desc_##n.if0_out_ep,			\
622 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_0,			\
623 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_0_iso_in_ep,		\
624 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_0_iso_out_ep,		\
625 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_1,			\
626 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_1_iso_in_ep,		\
627 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_1_iso_out_ep,		\
628 	(struct usb_desc_header *) &bt_hci_desc_##n.nil_desc,			\
629 };										\
630 										\
631 const static struct usb_desc_header *bt_hci_hs_desc_##n[] = {			\
632 	(struct usb_desc_header *) &bt_hci_desc_##n.iad,			\
633 	(struct usb_desc_header *) &bt_hci_desc_##n.if0,			\
634 	(struct usb_desc_header *) &bt_hci_desc_##n.if0_int_ep,			\
635 	(struct usb_desc_header *) &bt_hci_desc_##n.if0_hs_in_ep,		\
636 	(struct usb_desc_header *) &bt_hci_desc_##n.if0_hs_out_ep,		\
637 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_0,			\
638 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_0_iso_in_ep,		\
639 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_0_iso_out_ep,		\
640 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_1,			\
641 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_1_iso_in_ep,		\
642 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_1_iso_out_ep,		\
643 	(struct usb_desc_header *) &bt_hci_desc_##n.nil_desc,			\
644 };
645 
646 #define BT_HCI_CLASS_DATA_DEFINE(n)						\
647 	static struct bt_hci_data bt_hci_data_##n = {				\
648 		.sync_sem = Z_SEM_INITIALIZER(bt_hci_data_##n.sync_sem, 0, 1),	\
649 		.desc = &bt_hci_desc_##n,					\
650 		.fs_desc = bt_hci_fs_desc_##n,					\
651 		.hs_desc = bt_hci_hs_desc_##n,					\
652 	};									\
653 										\
654 	USBD_DEFINE_CLASS(bt_hci_##n, &bt_hci_api,				\
655 			  &bt_hci_data_##n, &bt_hci_vregs);
656 
657 /*
658  * Bluetooth subsystem does not support multiple HCI instances,
659  * but we are almost ready for it.
660  */
661 BT_HCI_DESCRIPTOR_DEFINE(0)
662 BT_HCI_CLASS_DATA_DEFINE(0)
663 
bt_hci_preinit(void)664 static int bt_hci_preinit(void)
665 {
666 	int ret;
667 
668 	ret = bt_enable_raw(&bt_hci_tx_queue);
669 	if (ret) {
670 		LOG_ERR("Failed to open Bluetooth raw channel: %d", ret);
671 		return ret;
672 	}
673 
674 	k_thread_create(&rx_thread_data, rx_thread_stack,
675 			K_KERNEL_STACK_SIZEOF(rx_thread_stack),
676 			bt_hci_rx_thread, NULL, NULL, NULL,
677 			K_PRIO_COOP(CONFIG_USBD_BT_HCI_RX_THREAD_PRIORITY),
678 			0, K_NO_WAIT);
679 
680 	k_thread_name_set(&rx_thread_data, "bt_hci_rx");
681 
682 	k_thread_create(&tx_thread_data, tx_thread_stack,
683 			K_KERNEL_STACK_SIZEOF(tx_thread_stack),
684 			bt_hci_tx_thread, (void *)&bt_hci_0, NULL, NULL,
685 			K_PRIO_COOP(CONFIG_USBD_BT_HCI_TX_THREAD_PRIORITY),
686 			0, K_NO_WAIT);
687 
688 	k_thread_name_set(&tx_thread_data, "bt_hci_tx");
689 
690 	return 0;
691 }
692 
693 SYS_INIT(bt_hci_preinit, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
694