|
| 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 | +LOG_MODULE_REGISTER(app, LOG_LEVEL_INF); |
| 15 | + |
| 16 | +/* |
| 17 | + * Minimal USB Host initialization. |
| 18 | + */ |
| 19 | + |
| 20 | +USBH_CONTROLLER_DEFINE(app_usbh, |
| 21 | + DEVICE_DT_GET(DT_NODELABEL(virtual_uhc0))); |
| 22 | + |
| 23 | +struct usbh_context *app_usbh_init_controller(void) |
| 24 | +{ |
| 25 | + int err; |
| 26 | + |
| 27 | + err = usbh_init(&app_usbh); |
| 28 | + if (err) { |
| 29 | + LOG_ERR("Failed to initialize host support"); |
| 30 | + return NULL; |
| 31 | + } |
| 32 | + |
| 33 | + return &app_usbh; |
| 34 | +} |
| 35 | + |
| 36 | +USBD_DEVICE_DEFINE(app_usbd, DEVICE_DT_GET(DT_NODELABEL(virtual_udc0)), 0x2fe3, 0x9999); |
| 37 | +USBD_DESC_LANG_DEFINE(app_lang); |
| 38 | +USBD_DESC_MANUFACTURER_DEFINE(app_mfr, "Nordic"); |
| 39 | +USBD_DESC_PRODUCT_DEFINE(app_product, "Virtual UVC device"); |
| 40 | +USBD_DESC_CONFIG_DEFINE(fs_cfg_desc, "FS Configuration"); |
| 41 | +USBD_CONFIGURATION_DEFINE(app_fs_config, 0, 200, &fs_cfg_desc); |
| 42 | + |
| 43 | +/* |
| 44 | + * Minimal USB Device initialization. |
| 45 | + */ |
| 46 | + |
| 47 | +struct usbd_context *app_usbd_init_device(void) |
| 48 | +{ |
| 49 | + int ret; |
| 50 | + |
| 51 | + ret = usbd_add_descriptor(&app_usbd, &app_lang); |
| 52 | + if (ret != 0) { |
| 53 | + LOG_ERR("Failed to initialize language descriptor (%d)", ret); |
| 54 | + return NULL; |
| 55 | + } |
| 56 | + |
| 57 | + ret = usbd_add_descriptor(&app_usbd, &app_mfr); |
| 58 | + if (ret != 0) { |
| 59 | + LOG_ERR("Failed to initialize manufacturer descriptor (%d)", ret); |
| 60 | + return NULL; |
| 61 | + } |
| 62 | + |
| 63 | + ret = usbd_add_descriptor(&app_usbd, &app_product); |
| 64 | + if (ret != 0) { |
| 65 | + LOG_ERR("Failed to initialize product descriptor (%d)", ret); |
| 66 | + return NULL; |
| 67 | + } |
| 68 | + |
| 69 | + ret = usbd_add_configuration(&app_usbd, USBD_SPEED_FS, &app_fs_config); |
| 70 | + if (ret != 0) { |
| 71 | + LOG_ERR("Failed to add Full-Speed configuration"); |
| 72 | + return NULL; |
| 73 | + } |
| 74 | + |
| 75 | + ret = usbd_register_all_classes(&app_usbd, USBD_SPEED_FS, 1, NULL); |
| 76 | + if (ret != 0) { |
| 77 | + LOG_ERR("Failed to add register classes"); |
| 78 | + return NULL; |
| 79 | + } |
| 80 | + |
| 81 | + usbd_device_set_code_triple(&app_usbd, USBD_SPEED_FS, USB_BCC_MISCELLANEOUS, 0x02, 0x01); |
| 82 | + |
| 83 | + usbd_self_powered(&app_usbd, USB_SCD_SELF_POWERED); |
| 84 | + |
| 85 | + ret = usbd_init(&app_usbd); |
| 86 | + if (ret != 0) { |
| 87 | + LOG_ERR("Failed to initialize device support"); |
| 88 | + return NULL; |
| 89 | + } |
| 90 | + |
| 91 | + return &app_usbd; |
| 92 | +} |
| 93 | + |
| 94 | +/* |
| 95 | + * Test using this host connected to this device via UVB |
| 96 | + */ |
| 97 | + |
| 98 | +const struct device *const uvc_dev = DEVICE_DT_GET(DT_NODELABEL(uvc_device)); |
| 99 | +const struct device *const video_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera)); |
| 100 | + |
| 101 | +ZTEST(usb_uvc, test_virtual_device_virtual_host) |
| 102 | +{ |
| 103 | + struct usbd_context *usbd_ctx; |
| 104 | + struct usbh_context *usbh_ctx; |
| 105 | + int ret; |
| 106 | + |
| 107 | + uvc_set_video_dev(uvc_dev, video_dev); |
| 108 | + |
| 109 | + usbd_ctx = app_usbd_init_device(); |
| 110 | + zassert_not_null(usbd_ctx, "USB device initialization must succeed"); |
| 111 | + |
| 112 | + usbh_ctx = app_usbh_init_controller(); |
| 113 | + zassert_not_null(usbh_ctx, "USB host initialization must succeed"); |
| 114 | + |
| 115 | + ret = usbd_enable(usbd_ctx); |
| 116 | + zassert_ok(ret, "USB device error code %d must be 0", ret); |
| 117 | + |
| 118 | + ret = usbh_enable(usbh_ctx); |
| 119 | + zassert_ok(ret, "USB host enable error code %d must be 0", ret); |
| 120 | + |
| 121 | + k_sleep(K_MSEC(500)); |
| 122 | + |
| 123 | + /* TODO: test the video devices here. */ |
| 124 | + |
| 125 | + ret = usbh_disable(usbh_ctx); |
| 126 | + zassert_ok(ret, "USB host disable error code %d must be 0", ret); |
| 127 | + |
| 128 | + ret = usbd_disable(usbd_ctx); |
| 129 | + zassert_ok(ret, "USB device disable error code %d must be 0", ret); |
| 130 | + |
| 131 | + ret = usbh_shutdown(usbh_ctx); |
| 132 | + zassert_ok(ret, "USB host shutdown error code %d must be 0", ret); |
| 133 | + |
| 134 | + ret = usbd_shutdown(usbd_ctx); |
| 135 | + zassert_ok(ret, "USB device shutdown error code %d must be 0", ret); |
| 136 | +} |
| 137 | + |
| 138 | +ZTEST_SUITE(usb_uvc, NULL, NULL, NULL, NULL, NULL); |
0 commit comments