1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zephyr/drivers/usb/udc.h>
9 #include <zephyr/sys/byteorder.h>
10 #include <zephyr/usb/usb_ch9.h>
11 
12 #include <zephyr/logging/log.h>
13 LOG_MODULE_REGISTER(udc_test, LOG_LEVEL_INF);
14 
15 /*
16  * Simple test for API rules, allocation, queue, and dequeu
17  * of the endpoint requests. USB device controller should not be
18  * connected to the host as this state is not covered by this test.
19  */
20 
21 #define FALSE_EP_ADDR		0x0FU
22 
23 K_MSGQ_DEFINE(test_msgq, sizeof(struct udc_event), 8, sizeof(uint32_t));
24 static K_KERNEL_STACK_DEFINE(test_udc_stack, 512);
25 static struct k_thread test_udc_thread_data;
26 static K_SEM_DEFINE(ep_queue_sem, 0, 1);
27 static uint8_t last_used_ep;
28 static uint8_t test_event_ctx;
29 
test_udc_event_handler(const struct device * dev,const struct udc_event * const event)30 static int test_udc_event_handler(const struct device *dev,
31 				  const struct udc_event *const event)
32 {
33 	return k_msgq_put(&test_msgq, event, K_NO_WAIT);
34 }
35 
event_ep_request(const struct device * dev,struct udc_event event)36 static void event_ep_request(const struct device *dev, struct udc_event event)
37 {
38 	struct udc_buf_info *bi;
39 	int err;
40 
41 	bi = udc_get_buf_info(event.buf);
42 
43 	err = udc_ep_buf_free(dev, event.buf);
44 	zassert_ok(err, "Failed to free request buffer");
45 
46 	if (bi->err == -ECONNABORTED && bi->ep == last_used_ep) {
47 		k_sem_give(&ep_queue_sem);
48 	}
49 }
50 
test_udc_thread(void * p1,void * p2,void * p3)51 static void test_udc_thread(void *p1, void *p2, void *p3)
52 {
53 	ARG_UNUSED(p2);
54 	ARG_UNUSED(p3);
55 
56 	const struct device *dev = p1;
57 	struct udc_event event;
58 
59 	while (true) {
60 		k_msgq_get(&test_msgq, &event, K_FOREVER);
61 
62 		zassert_equal(udc_get_event_ctx(event.dev), &test_event_ctx,
63 			      "Wrong pointer to higher layer context");
64 
65 		switch (event.type) {
66 		case UDC_EVT_VBUS_REMOVED:
67 			LOG_DBG("VBUS remove event");
68 			break;
69 		case UDC_EVT_VBUS_READY:
70 			LOG_DBG("VBUS detected event");
71 			break;
72 		case UDC_EVT_SUSPEND:
73 			LOG_DBG("Suspend event");
74 			break;
75 		case UDC_EVT_RESUME:
76 			LOG_DBG("Resume event");
77 			break;
78 		case UDC_EVT_RESET:
79 			LOG_DBG("Reset event");
80 			break;
81 		case UDC_EVT_SOF:
82 			LOG_DBG("SoF event");
83 			break;
84 		case UDC_EVT_EP_REQUEST:
85 			event_ep_request(dev, event);
86 			break;
87 		case UDC_EVT_ERROR:
88 			LOG_DBG("Error event");
89 			break;
90 		default:
91 			break;
92 		};
93 	}
94 }
95 
test_udc_ep_try_config(const struct device * dev,struct usb_ep_descriptor * ed)96 static void test_udc_ep_try_config(const struct device *dev,
97 				   struct usb_ep_descriptor *ed)
98 {
99 	uint16_t mps = sys_le16_to_cpu(ed->wMaxPacketSize);
100 	int err;
101 
102 	err = udc_ep_try_config(dev, ed->bEndpointAddress,
103 				ed->bmAttributes, &mps,
104 				ed->bInterval);
105 	zassert_equal(err, 0, "Failed to test endpoint configuration");
106 
107 	if (ed->bmAttributes == USB_EP_TYPE_CONTROL ||
108 	    ed->bmAttributes == USB_EP_TYPE_ISO) {
109 		/*
110 		 * Skip subsequent test since udc_ep_try_config() does not
111 		 * update mps argument for control and iso endpoints.
112 		 */
113 		return;
114 	}
115 
116 	mps = 0;
117 	err = udc_ep_try_config(dev, ed->bEndpointAddress,
118 				ed->bmAttributes, &mps,
119 				ed->bInterval);
120 	zassert_equal(err, 0, "Failed to test endpoint configuration");
121 	zassert_not_equal(mps, 0, "Failed to test endpoint configuration");
122 }
123 
test_udc_ep_enable(const struct device * dev,struct usb_ep_descriptor * ed)124 static void test_udc_ep_enable(const struct device *dev,
125 			       struct usb_ep_descriptor *ed)
126 {
127 	uint8_t ctrl_ep = USB_EP_DIR_IS_IN(ed->bEndpointAddress) ?
128 			  USB_CONTROL_EP_IN : USB_CONTROL_EP_OUT;
129 	/* Possible return values 0, -EINVAL, -ENODEV, -EALREADY, -EPERM. */
130 	int err1, err2, err3, err4;
131 
132 	err1 = udc_ep_enable(dev, ed->bEndpointAddress, ed->bmAttributes,
133 			     sys_le16_to_cpu(ed->wMaxPacketSize),
134 			     ed->bInterval);
135 	err2 = udc_ep_enable(dev, ed->bEndpointAddress, ed->bmAttributes,
136 			     sys_le16_to_cpu(ed->wMaxPacketSize),
137 			     ed->bInterval);
138 	err3 = udc_ep_enable(dev, FALSE_EP_ADDR, ed->bmAttributes,
139 			     sys_le16_to_cpu(ed->wMaxPacketSize),
140 			     ed->bInterval);
141 	err4 = udc_ep_enable(dev, ctrl_ep, ed->bmAttributes,
142 			     sys_le16_to_cpu(ed->wMaxPacketSize),
143 			     ed->bInterval);
144 
145 	if (!udc_is_initialized(dev) && !udc_is_enabled(dev)) {
146 		zassert_equal(err1, -EPERM, "Not failed to enable endpoint");
147 		zassert_equal(err2, -EPERM, "Not failed to enable endpoint");
148 		zassert_equal(err3, -EPERM, "Not failed to enable endpoint");
149 		zassert_equal(err4, -EINVAL, "Not failed to enable endpoint");
150 	} else if (udc_is_initialized(dev) && !udc_is_enabled(dev)) {
151 		zassert_equal(err1, -EPERM, "Not failed to enable endpoint");
152 		zassert_equal(err2, -EPERM, "Not failed to enable endpoint");
153 		zassert_equal(err3, -EPERM, "Not failed to enable endpoint");
154 		zassert_equal(err4, -EINVAL, "Not failed to enable endpoint");
155 	} else {
156 		zassert_equal(err1, 0, "Failed to enable endpoint");
157 		zassert_equal(err2, -EALREADY, "Not failed to enable endpoint");
158 		zassert_equal(err3, -ENODEV, "Not failed to enable endpoint");
159 		zassert_equal(err4, -EINVAL, "Not failed to enable endpoint");
160 	}
161 }
162 
test_udc_ep_disable(const struct device * dev,struct usb_ep_descriptor * ed)163 static void test_udc_ep_disable(const struct device *dev,
164 				struct usb_ep_descriptor *ed)
165 {
166 	uint8_t ctrl_ep = USB_EP_DIR_IS_IN(ed->bEndpointAddress) ?
167 			  USB_CONTROL_EP_IN : USB_CONTROL_EP_OUT;
168 	/* Possible return values 0, -EINVAL, -ENODEV, -EALREADY, -EPERM. */
169 	int err1, err2, err3, err4;
170 
171 	err1 = udc_ep_disable(dev, ed->bEndpointAddress);
172 	err2 = udc_ep_disable(dev, ed->bEndpointAddress);
173 	err3 = udc_ep_disable(dev, FALSE_EP_ADDR);
174 	err4 = udc_ep_disable(dev, ctrl_ep);
175 
176 	if (!udc_is_initialized(dev) && !udc_is_enabled(dev)) {
177 		zassert_equal(err1, -EPERM, "Not failed to disable endpoint");
178 		zassert_equal(err2, -EPERM, "Not failed to disable endpoint");
179 		zassert_equal(err3, -EPERM, "Not failed to disable endpoint");
180 		zassert_equal(err4, -EINVAL, "Not failed to disable endpoint");
181 	} else if (udc_is_initialized(dev) && !udc_is_enabled(dev)) {
182 		zassert_equal(err1, -EALREADY, "Failed to disable endpoint");
183 		zassert_equal(err2, -EALREADY, "Not failed to disable endpoint");
184 		zassert_equal(err3, -ENODEV, "Not failed to disable endpoint");
185 		zassert_equal(err4, -EINVAL, "Not failed to disable endpoint");
186 	} else {
187 		zassert_equal(err1, 0, "Failed to disable endpoint");
188 		zassert_equal(err2, -EALREADY, "Not failed to disable endpoint");
189 		zassert_equal(err3, -ENODEV, "Not failed to disable endpoint");
190 		zassert_equal(err4, -EINVAL, "Not failed to disable endpoint");
191 	}
192 }
193 
test_udc_ep_buf_alloc(const struct device * dev,struct usb_ep_descriptor * ed)194 static struct net_buf *test_udc_ep_buf_alloc(const struct device *dev,
195 					     struct usb_ep_descriptor *ed)
196 {
197 	struct net_buf *buf;
198 
199 	buf = udc_ep_buf_alloc(dev, ed->bEndpointAddress,
200 			       USB_MPS_TO_TPL(sys_le16_to_cpu(ed->wMaxPacketSize)));
201 
202 	zassert_not_null(buf, "Failed to allocate request");
203 
204 	return buf;
205 }
206 
test_udc_ep_buf_free(const struct device * dev,struct net_buf * buf)207 static void test_udc_ep_buf_free(const struct device *dev,
208 				 struct net_buf *buf)
209 {
210 	int err;
211 
212 	if (buf == NULL) {
213 		return;
214 	}
215 
216 	err = udc_ep_buf_free(dev, buf);
217 	zassert_ok(err, "Failed to free request");
218 }
219 
test_udc_ep_halt(const struct device * dev,struct usb_ep_descriptor * ed)220 static void test_udc_ep_halt(const struct device *dev,
221 			     struct usb_ep_descriptor *ed)
222 {
223 	/* Possible return values 0, -ENODEV, -ENOTSUP, -EPERM. */
224 	int err1, err2;
225 
226 	err1 = udc_ep_set_halt(dev, ed->bEndpointAddress);
227 	err2 = udc_ep_set_halt(dev, FALSE_EP_ADDR);
228 
229 	if (udc_is_enabled(dev)) {
230 		if (ed->bmAttributes == USB_EP_TYPE_ISO) {
231 			zassert_equal(err1, -ENOTSUP, "Not failed to set halt");
232 		} else {
233 			zassert_equal(err1, 0, "Failed to set halt");
234 		}
235 
236 		zassert_equal(err2, -ENODEV, "Not failed to set halt");
237 	} else {
238 		zassert_equal(err1, -EPERM, "Not failed to set halt");
239 		zassert_equal(err2, -EPERM, "Not failed to set halt");
240 	}
241 
242 	err1 = udc_ep_clear_halt(dev, ed->bEndpointAddress);
243 	err2 = udc_ep_clear_halt(dev, FALSE_EP_ADDR);
244 
245 	if (udc_is_enabled(dev)) {
246 		if (ed->bmAttributes == USB_EP_TYPE_ISO) {
247 			zassert_equal(err1, -ENOTSUP, "Not failed to clear halt");
248 		} else {
249 			zassert_equal(err1, 0, "Failed to clear halt ");
250 		}
251 
252 		zassert_equal(err2, -ENODEV, "Not failed to clear halt");
253 	} else {
254 		zassert_equal(err1, -EPERM, "Not failed to clear halt");
255 		zassert_equal(err2, -EPERM, "Not failed to clear halt");
256 	}
257 }
258 
test_udc_ep_enqueue(const struct device * dev,struct net_buf * buf)259 static void test_udc_ep_enqueue(const struct device *dev,
260 				struct net_buf *buf)
261 {
262 	/* Possible return values 0, -EPERM, -ENODEV, -EACCES(TBD), -EBUSY (TBD) */
263 	int err1, err2 = 0;
264 	struct net_buf *false_buf = NULL;
265 
266 	err1 = udc_ep_enqueue(dev, buf);
267 	if (udc_is_enabled(dev)) {
268 		false_buf = udc_ep_buf_alloc(dev, FALSE_EP_ADDR, 64);
269 		zassert_not_null(false_buf, "Failed to allocate request");
270 		err2 = udc_ep_enqueue(dev, false_buf);
271 	}
272 
273 	if (udc_is_enabled(dev)) {
274 		zassert_equal(err1, 0, "Failed to queue request");
275 		zassert_equal(err2, -ENODEV, "Not failed to queue request");
276 	} else {
277 		zassert_equal(err1, -EPERM, "Not failed to queue request");
278 	}
279 
280 	test_udc_ep_buf_free(dev, false_buf);
281 }
282 
test_udc_ep_dequeue(const struct device * dev,struct usb_ep_descriptor * ed)283 static void test_udc_ep_dequeue(const struct device *dev,
284 				struct usb_ep_descriptor *ed)
285 {
286 	/* Possible return values 0, -EPERM, -ENODEV, -EACCES(TBD) */
287 	int err1, err2;
288 
289 	err1 = udc_ep_dequeue(dev, ed->bEndpointAddress);
290 	err2 = udc_ep_dequeue(dev, FALSE_EP_ADDR);
291 
292 	if (!udc_is_initialized(dev)) {
293 		zassert_equal(err1, -EPERM, "Not failed to dequeue");
294 		zassert_equal(err2, -EPERM, "Not failed to dequeue");
295 	} else {
296 		zassert_equal(err1, 0, "Failed to dequeue");
297 		zassert_equal(err2, -ENODEV, "Not failed to dequeue");
298 	}
299 }
300 
test_udc_wakeup(const struct device * dev)301 static void test_udc_wakeup(const struct device *dev)
302 {
303 	int err;
304 
305 	err = udc_host_wakeup(dev);
306 
307 	if (!udc_is_enabled(dev)) {
308 		zassert_equal(err, -EPERM, "Not failed to request host wakeup");
309 	}
310 }
311 
test_udc_set_address(const struct device * dev,uint8_t addr)312 static void test_udc_set_address(const struct device *dev, uint8_t addr)
313 {
314 	int err;
315 
316 	err = udc_set_address(dev, addr);
317 
318 	if (!udc_is_enabled(dev)) {
319 		zassert_equal(err, -EPERM, "Not failed to set address");
320 	}
321 }
322 
test_udc_ep_api(const struct device * dev,struct usb_ep_descriptor * ed)323 static void test_udc_ep_api(const struct device *dev,
324 			    struct usb_ep_descriptor *ed)
325 {
326 	const int num_of_iterations = 10;
327 	struct net_buf *buf;
328 	int err;
329 
330 	last_used_ep = ed->bEndpointAddress;
331 
332 	for (int i = 0; i < num_of_iterations; i++) {
333 		err = udc_ep_enable(dev, ed->bEndpointAddress, ed->bmAttributes,
334 				    sys_le16_to_cpu(ed->wMaxPacketSize),
335 				    ed->bInterval);
336 		zassert_ok(err, "Failed to enable endpoint");
337 
338 		/* It needs a little reserve for memory management overhead. */
339 		for (int n = 0; n < (CONFIG_UDC_BUF_COUNT - 4); n++) {
340 			buf = udc_ep_buf_alloc(dev, ed->bEndpointAddress,
341 				USB_MPS_TO_TPL(sys_le16_to_cpu(ed->wMaxPacketSize)));
342 			zassert_not_null(buf,
343 					 "Failed to allocate request (%d) for 0x%02x",
344 					 n, ed->bEndpointAddress);
345 
346 			udc_ep_buf_set_zlp(buf);
347 			err = udc_ep_enqueue(dev, buf);
348 			zassert_ok(err, "Failed to queue request");
349 			k_yield();
350 		}
351 
352 		err = udc_ep_disable(dev, ed->bEndpointAddress);
353 		zassert_ok(err, "Failed to disable endpoint");
354 
355 		err = udc_ep_dequeue(dev, ed->bEndpointAddress);
356 		zassert_ok(err, "Failed to dequeue endpoint");
357 
358 		err = k_sem_take(&ep_queue_sem, K_MSEC(100));
359 		zassert_ok(err, "Timeout to dequeue endpoint %x %d", last_used_ep, err);
360 	}
361 }
362 
test_udc_ep_mps(uint8_t type)363 static void test_udc_ep_mps(uint8_t type)
364 {
365 	uint16_t mps[] = {8, 16, 32, 64, 512, 1024};
366 	struct usb_ep_descriptor ed = {
367 		.bLength = sizeof(struct usb_ep_descriptor),
368 		.bDescriptorType = USB_DESC_ENDPOINT,
369 		.bEndpointAddress = 0x01,
370 		.bmAttributes = type,
371 		.wMaxPacketSize = sys_cpu_to_le16(0),
372 		.bInterval = 0,
373 	};
374 	const struct device *dev;
375 	uint16_t supported = 0;
376 	int err;
377 
378 	dev = DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0));
379 	zassert_true(device_is_ready(dev), "UDC device not ready");
380 
381 	err = udc_init(dev, test_udc_event_handler, &test_event_ctx);
382 	zassert_ok(err, "Failed to initialize UDC driver");
383 
384 	err = udc_enable(dev);
385 	zassert_ok(err, "Failed to enable UDC driver");
386 
387 	if (type == USB_EP_TYPE_INTERRUPT) {
388 		ed.bInterval = 1;
389 	}
390 
391 	for (uint8_t i = 1; i < 16U; i++) {
392 		err = udc_ep_try_config(dev, i,
393 					ed.bmAttributes, &supported,
394 					ed.bInterval);
395 		if (!err) {
396 			ed.bEndpointAddress = i;
397 			break;
398 		}
399 	}
400 
401 	zassert_ok(err, "Failed to determine MPS");
402 
403 	for (int i = 0; i < ARRAY_SIZE(mps); i++) {
404 		if (mps[i] > supported) {
405 			continue;
406 		}
407 
408 		ed.wMaxPacketSize = sys_cpu_to_le16(mps[i]);
409 		test_udc_ep_api(dev, &ed);
410 
411 		ed.bEndpointAddress |= USB_EP_DIR_IN;
412 		test_udc_ep_api(dev, &ed);
413 	}
414 
415 	err = udc_disable(dev);
416 	zassert_ok(err, "Failed to disable UDC driver");
417 
418 	err = udc_shutdown(dev);
419 	zassert_ok(err, "Failed to shoot-down UDC driver");
420 }
421 
test_udc_device_get(void)422 static void *test_udc_device_get(void)
423 {
424 	struct udc_device_caps caps;
425 	const struct device *dev;
426 
427 	dev = DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0));
428 	zassert_true(device_is_ready(dev), "UDC device not ready");
429 
430 	caps = udc_caps(dev);
431 	LOG_INF("UDC device HS: %u", caps.hs);
432 
433 	k_thread_create(&test_udc_thread_data, test_udc_stack,
434 			K_KERNEL_STACK_SIZEOF(test_udc_stack),
435 			test_udc_thread,
436 			(void *)dev, NULL, NULL,
437 			K_PRIO_COOP(9), 0, K_NO_WAIT);
438 
439 	k_thread_name_set(&test_udc_thread_data, "test-udc");
440 
441 	return (void *)dev;
442 }
443 
444 static struct usb_ep_descriptor ed_ctrl_out = {
445 	.bLength = sizeof(struct usb_ep_descriptor),
446 	.bDescriptorType = USB_DESC_ENDPOINT,
447 	.bEndpointAddress = USB_CONTROL_EP_OUT,
448 	.bmAttributes = USB_EP_TYPE_CONTROL,
449 	.wMaxPacketSize = sys_cpu_to_le16(64),
450 	.bInterval = 0,
451 };
452 
453 static struct usb_ep_descriptor ed_ctrl_in = {
454 	.bLength = sizeof(struct usb_ep_descriptor),
455 	.bDescriptorType = USB_DESC_ENDPOINT,
456 	.bEndpointAddress = USB_CONTROL_EP_IN,
457 	.bmAttributes = USB_EP_TYPE_CONTROL,
458 	.wMaxPacketSize = sys_cpu_to_le16(64),
459 	.bInterval = 0,
460 };
461 
462 static struct usb_ep_descriptor ed_bulk_out = {
463 	.bLength = sizeof(struct usb_ep_descriptor),
464 	.bDescriptorType = USB_DESC_ENDPOINT,
465 	.bEndpointAddress = 0x01,
466 	.bmAttributes = USB_EP_TYPE_BULK,
467 	.wMaxPacketSize = sys_cpu_to_le16(64),
468 	.bInterval = 0,
469 };
470 
471 static struct usb_ep_descriptor ed_bulk_in = {
472 	.bLength = sizeof(struct usb_ep_descriptor),
473 	.bDescriptorType = USB_DESC_ENDPOINT,
474 	.bEndpointAddress = 0x81,
475 	.bmAttributes = USB_EP_TYPE_BULK,
476 	.wMaxPacketSize = sys_cpu_to_le16(64),
477 	.bInterval = 0,
478 };
479 
ZTEST(udc_driver_test,test_udc_not_initialized)480 ZTEST(udc_driver_test, test_udc_not_initialized)
481 {
482 	const struct device *dev;
483 	struct net_buf *buf;
484 	int err;
485 
486 	dev = DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0));
487 	zassert_true(device_is_ready(dev), "UDC device not ready");
488 
489 	err = udc_init(dev, NULL, NULL);
490 	zassert_equal(err, -EINVAL, "Not failed to initialize UDC");
491 
492 	err = udc_shutdown(dev);
493 	zassert_equal(err, -EALREADY, "Not failed to shutdown UDC");
494 
495 	err = udc_enable(dev);
496 	zassert_equal(err, -EPERM, "Not failed to enable UDC driver");
497 
498 	test_udc_set_address(dev, 0);
499 	test_udc_set_address(dev, 1);
500 	test_udc_wakeup(dev);
501 
502 	test_udc_ep_try_config(dev, &ed_ctrl_out);
503 	test_udc_ep_try_config(dev, &ed_ctrl_in);
504 	test_udc_ep_try_config(dev, &ed_bulk_out);
505 	test_udc_ep_try_config(dev, &ed_bulk_in);
506 
507 	buf = test_udc_ep_buf_alloc(dev, &ed_bulk_out);
508 	test_udc_ep_enable(dev, &ed_bulk_out);
509 	test_udc_ep_enqueue(dev, buf);
510 	test_udc_ep_halt(dev, &ed_bulk_out);
511 	test_udc_ep_disable(dev, &ed_bulk_out);
512 	test_udc_ep_dequeue(dev, &ed_bulk_out);
513 	test_udc_ep_buf_free(dev, buf);
514 
515 	err = udc_disable(dev);
516 	zassert_equal(err, -EALREADY, "Not failed to disable UDC driver");
517 }
518 
ZTEST(udc_driver_test,test_udc_initialized)519 ZTEST(udc_driver_test, test_udc_initialized)
520 {
521 	const struct device *dev;
522 	struct net_buf *buf;
523 	int err;
524 
525 	dev = DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0));
526 	zassert_true(device_is_ready(dev), "UDC device not ready");
527 
528 	err = udc_init(dev, test_udc_event_handler, &test_event_ctx);
529 	zassert_ok(err, "Failed to initialize UDC driver");
530 
531 	test_udc_set_address(dev, 0);
532 	test_udc_set_address(dev, 1);
533 	test_udc_wakeup(dev);
534 
535 	test_udc_ep_try_config(dev, &ed_ctrl_out);
536 	test_udc_ep_try_config(dev, &ed_ctrl_in);
537 	test_udc_ep_try_config(dev, &ed_bulk_out);
538 	test_udc_ep_try_config(dev, &ed_bulk_in);
539 
540 	buf = test_udc_ep_buf_alloc(dev, &ed_bulk_out);
541 	test_udc_ep_enable(dev, &ed_bulk_out);
542 	test_udc_ep_enqueue(dev, buf);
543 	test_udc_ep_halt(dev, &ed_bulk_out);
544 	test_udc_ep_disable(dev, &ed_bulk_out);
545 	test_udc_ep_dequeue(dev, &ed_bulk_out);
546 	test_udc_ep_buf_free(dev, buf);
547 
548 	err = udc_shutdown(dev);
549 	zassert_ok(err, "Failed to shootdown UDC driver");
550 }
551 
ZTEST(udc_driver_test,test_udc_enabled)552 ZTEST(udc_driver_test, test_udc_enabled)
553 {
554 	const struct device *dev;
555 	struct net_buf *buf;
556 	int err;
557 
558 	dev = DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0));
559 	zassert_true(device_is_ready(dev), "UDC device not ready");
560 
561 	err = udc_init(dev, test_udc_event_handler, &test_event_ctx);
562 	zassert_ok(err, "Failed to initialize UDC driver");
563 
564 	err = udc_enable(dev);
565 	zassert_ok(err, "Failed to enable UDC driver");
566 
567 	err = udc_enable(dev);
568 	zassert_equal(err, -EALREADY, "Not failed to enable UDC driver");
569 
570 	err = udc_disable(dev);
571 	zassert_ok(err, "Failed to disable UDC driver");
572 
573 	err = udc_enable(dev);
574 	zassert_ok(err, "Failed to enable UDC driver");
575 
576 	test_udc_set_address(dev, 0);
577 	test_udc_set_address(dev, 1);
578 
579 	test_udc_ep_try_config(dev, &ed_ctrl_out);
580 	test_udc_ep_try_config(dev, &ed_ctrl_in);
581 	test_udc_ep_try_config(dev, &ed_bulk_out);
582 	test_udc_ep_try_config(dev, &ed_bulk_in);
583 
584 	buf = test_udc_ep_buf_alloc(dev, &ed_bulk_out);
585 	test_udc_ep_enable(dev, &ed_bulk_out);
586 	test_udc_ep_enqueue(dev, buf);
587 	test_udc_ep_halt(dev, &ed_bulk_out);
588 	test_udc_ep_disable(dev, &ed_bulk_out);
589 	test_udc_ep_dequeue(dev, &ed_bulk_out);
590 	test_udc_ep_buf_free(dev, buf);
591 
592 	err = udc_shutdown(dev);
593 	zassert_equal(err, -EBUSY, "Not failed to shoot-down UDC driver");
594 
595 	err = udc_disable(dev);
596 	zassert_ok(err, "Failed to disable UDC driver");
597 
598 	err = udc_shutdown(dev);
599 	zassert_ok(err, "Failed to shoot-down UDC driver");
600 }
601 
ZTEST(udc_driver_test,test_udc_ep_buf)602 ZTEST(udc_driver_test, test_udc_ep_buf)
603 {
604 	test_udc_ep_mps(USB_EP_TYPE_BULK);
605 }
606 
ZTEST(udc_driver_test,test_udc_ep_int)607 ZTEST(udc_driver_test, test_udc_ep_int)
608 {
609 	test_udc_ep_mps(USB_EP_TYPE_INTERRUPT);
610 }
611 
ZTEST(udc_driver_test,test_udc_ep_iso)612 ZTEST(udc_driver_test, test_udc_ep_iso)
613 {
614 	test_udc_ep_mps(USB_EP_TYPE_ISO);
615 }
616 
617 ZTEST_SUITE(udc_driver_test, NULL, test_udc_device_get, NULL, NULL, NULL);
618