|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/device.h> |
| 8 | +#include <zephyr/logging/log.h> |
| 9 | +#include <zephyr/usb/usbd.h> |
| 10 | +#include <zephyr/usb/usbh.h> |
| 11 | +#include <zephyr/usb/class/usbd_uvc.h> |
| 12 | +#include <zephyr/ztest.h> |
| 13 | + |
| 14 | +#include "../../../../../drivers/video/video_common.h" |
| 15 | + |
| 16 | +LOG_MODULE_REGISTER(app, LOG_LEVEL_INF); |
| 17 | + |
| 18 | +/* |
| 19 | + * Minimal USB Host initialization. |
| 20 | + */ |
| 21 | + |
| 22 | +USBH_CONTROLLER_DEFINE(app_usbh, |
| 23 | + DEVICE_DT_GET(DT_NODELABEL(virtual_uhc0))); |
| 24 | + |
| 25 | +struct usbh_context *app_usbh_init_controller(void) |
| 26 | +{ |
| 27 | + int err; |
| 28 | + |
| 29 | + err = usbh_init(&app_usbh); |
| 30 | + if (err) { |
| 31 | + LOG_ERR("Failed to initialize host support"); |
| 32 | + return NULL; |
| 33 | + } |
| 34 | + |
| 35 | + return &app_usbh; |
| 36 | +} |
| 37 | + |
| 38 | +USBD_DEVICE_DEFINE(app_usbd, DEVICE_DT_GET(DT_NODELABEL(virtual_udc0)), 0x2fe3, 0x9999); |
| 39 | +USBD_DESC_LANG_DEFINE(app_lang); |
| 40 | +USBD_DESC_MANUFACTURER_DEFINE(app_mfr, "Nordic"); |
| 41 | +USBD_DESC_PRODUCT_DEFINE(app_product, "Virtual UVC device"); |
| 42 | +USBD_DESC_CONFIG_DEFINE(fs_cfg_desc, "FS Configuration"); |
| 43 | +USBD_CONFIGURATION_DEFINE(app_fs_config, 0, 200, &fs_cfg_desc); |
| 44 | + |
| 45 | +/* |
| 46 | + * Minimal USB Device initialization. |
| 47 | + */ |
| 48 | + |
| 49 | +struct usbd_context *app_usbd_init_device(void) |
| 50 | +{ |
| 51 | + int ret; |
| 52 | + |
| 53 | + ret = usbd_add_descriptor(&app_usbd, &app_lang); |
| 54 | + if (ret != 0) { |
| 55 | + LOG_ERR("Failed to initialize language descriptor (%d)", ret); |
| 56 | + return NULL; |
| 57 | + } |
| 58 | + |
| 59 | + ret = usbd_add_descriptor(&app_usbd, &app_mfr); |
| 60 | + if (ret != 0) { |
| 61 | + LOG_ERR("Failed to initialize manufacturer descriptor (%d)", ret); |
| 62 | + return NULL; |
| 63 | + } |
| 64 | + |
| 65 | + ret = usbd_add_descriptor(&app_usbd, &app_product); |
| 66 | + if (ret != 0) { |
| 67 | + LOG_ERR("Failed to initialize product descriptor (%d)", ret); |
| 68 | + return NULL; |
| 69 | + } |
| 70 | + |
| 71 | + ret = usbd_add_configuration(&app_usbd, USBD_SPEED_FS, &app_fs_config); |
| 72 | + if (ret != 0) { |
| 73 | + LOG_ERR("Failed to add Full-Speed configuration"); |
| 74 | + return NULL; |
| 75 | + } |
| 76 | + |
| 77 | + ret = usbd_register_all_classes(&app_usbd, USBD_SPEED_FS, 1, NULL); |
| 78 | + if (ret != 0) { |
| 79 | + LOG_ERR("Failed to add register classes"); |
| 80 | + return NULL; |
| 81 | + } |
| 82 | + |
| 83 | + usbd_device_set_code_triple(&app_usbd, USBD_SPEED_FS, USB_BCC_MISCELLANEOUS, 0x02, 0x01); |
| 84 | + |
| 85 | + usbd_self_powered(&app_usbd, USB_SCD_SELF_POWERED); |
| 86 | + |
| 87 | + ret = usbd_init(&app_usbd); |
| 88 | + if (ret != 0) { |
| 89 | + LOG_ERR("Failed to initialize device support"); |
| 90 | + return NULL; |
| 91 | + } |
| 92 | + |
| 93 | + return &app_usbd; |
| 94 | +} |
| 95 | + |
| 96 | +/* |
| 97 | + * Test using this host connected to this device via UVB |
| 98 | + */ |
| 99 | + |
| 100 | +const struct video_format test_formats[] = { |
| 101 | + {.pixelformat = VIDEO_PIX_FMT_YUYV, .width = 640, .height = 480}, |
| 102 | + {.pixelformat = VIDEO_PIX_FMT_YUYV, .width = 320, .height = 240}, |
| 103 | + {.pixelformat = VIDEO_PIX_FMT_YUYV, .width = 160, .height = 120}, |
| 104 | +}; |
| 105 | + |
| 106 | +const struct device *const uvc_dev = DEVICE_DT_GET(DT_NODELABEL(uvc_device)); |
| 107 | +const struct device *const video_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera)); |
| 108 | + |
| 109 | +ZTEST(usb_uvc, test_virtual_device_virtual_host) |
| 110 | +{ |
| 111 | + struct usbd_context *usbd_ctx; |
| 112 | + struct usbh_context *usbh_ctx; |
| 113 | + int ret; |
| 114 | + |
| 115 | + uvc_set_video_dev(uvc_dev, video_dev); |
| 116 | + |
| 117 | + for (size_t i = 0; i < ARRAY_SIZE(test_formats); i++) { |
| 118 | + struct video_format fmt = test_formats[i]; |
| 119 | + |
| 120 | + ret = video_estimate_fmt_size(&fmt); |
| 121 | + zassert_ok(ret); |
| 122 | + |
| 123 | + ret = uvc_add_format(uvc_dev, &fmt); |
| 124 | + zassert_ok(ret); |
| 125 | + } |
| 126 | + |
| 127 | + usbd_ctx = app_usbd_init_device(); |
| 128 | + zassert_not_null(usbd_ctx, "USB device initialization must succeed"); |
| 129 | + |
| 130 | + usbh_ctx = app_usbh_init_controller(); |
| 131 | + zassert_not_null(usbh_ctx, "USB host initialization must succeed"); |
| 132 | + |
| 133 | + ret = usbd_enable(usbd_ctx); |
| 134 | + zassert_ok(ret, "USB device error code %d must be 0", ret); |
| 135 | + |
| 136 | + ret = usbh_enable(usbh_ctx); |
| 137 | + zassert_ok(ret, "USB host enable error code %d must be 0", ret); |
| 138 | + |
| 139 | + k_sleep(K_MSEC(500)); |
| 140 | + |
| 141 | + /* TODO: test the video devices here. */ |
| 142 | + |
| 143 | + ret = usbh_disable(usbh_ctx); |
| 144 | + zassert_ok(ret, "USB host disable error code %d must be 0", ret); |
| 145 | + |
| 146 | + ret = usbd_disable(usbd_ctx); |
| 147 | + zassert_ok(ret, "USB device disable error code %d must be 0", ret); |
| 148 | + |
| 149 | + ret = usbh_shutdown(usbh_ctx); |
| 150 | + zassert_ok(ret, "USB host shutdown error code %d must be 0", ret); |
| 151 | + |
| 152 | + ret = usbd_shutdown(usbd_ctx); |
| 153 | + zassert_ok(ret, "USB device shutdown error code %d must be 0", ret); |
| 154 | +} |
| 155 | + |
| 156 | +ZTEST_SUITE(usb_uvc, NULL, NULL, NULL, NULL, NULL); |
0 commit comments