|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/ztest.h> |
| 8 | +#include <zephyr/usb/usbh.h> |
| 9 | +#include <zephyr/usb/usbh.h> |
| 10 | + |
| 11 | +#include "usbh_ch9.h" |
| 12 | +#include "usbh_class_api.h" |
| 13 | +#include "usbh_desc.h" |
| 14 | +#include "usbh_host.h" |
| 15 | + |
| 16 | +#include "test_descriptor.h" |
| 17 | + |
| 18 | +#include <zephyr/logging/log.h> |
| 19 | +LOG_MODULE_REGISTER(usb_test, LOG_LEVEL_DBG); |
| 20 | + |
| 21 | +USBH_CONTROLLER_DEFINE(uhs_ctx, DEVICE_DT_GET(DT_NODELABEL(zephyr_uhc0))); |
| 22 | + |
| 23 | +static const uint8_t test_hub_descriptor[] = {TEST_HUB_DESCRIPTOR}; |
| 24 | +struct usb_device test_udev = {}; |
| 25 | + |
| 26 | +/* Private class data, here just an integer but usually a custom struct. */ |
| 27 | +struct test_class_priv { |
| 28 | + enum { |
| 29 | + /* Test value stored before the class is initialized */ |
| 30 | + TEST_CLASS_PRIV_INACTIVE, |
| 31 | + /* Test value stored after the class is initialized */ |
| 32 | + TEST_CLASS_PRIV_ENABLED, |
| 33 | + } state; |
| 34 | +}; |
| 35 | + |
| 36 | +static struct test_class_priv test_class_priv = { |
| 37 | + .state = TEST_CLASS_PRIV_INACTIVE, |
| 38 | +}; |
| 39 | + |
| 40 | +static int test_class_init(struct usbh_class_data *const c_data, |
| 41 | + struct usbh_context *const uhs_ctx) |
| 42 | +{ |
| 43 | + struct test_class_priv *priv = c_data->priv; |
| 44 | + |
| 45 | + LOG_DBG("initializing %p, priv value 0x%x", c_data, *priv); |
| 46 | + |
| 47 | + zassert_equal(priv->state, TEST_CLASS_PRIV_INACTIVE, |
| 48 | + "Class should be initialized only once"); |
| 49 | + |
| 50 | + priv->state = TEST_CLASS_PRIV_ENABLED; |
| 51 | + |
| 52 | + return 0; |
| 53 | +} |
| 54 | + |
| 55 | +static int test_class_completion_cb(struct usbh_class_data *const c_data, |
| 56 | + struct usb_device *const udev, |
| 57 | + struct uhc_transfer *const xfer) |
| 58 | +{ |
| 59 | + struct test_class_priv *priv = c_data->priv; |
| 60 | + |
| 61 | + LOG_DBG("completion callback for %p, transfer %p", c_data, xfer); |
| 62 | + |
| 63 | + zassert_equal(priv->state, TEST_CLASS_PRIV_ENABLED); |
| 64 | + |
| 65 | + return 0; |
| 66 | +} |
| 67 | + |
| 68 | +static int test_class_probe(struct usbh_class_data *const c_data, |
| 69 | + struct usb_device *const udev, |
| 70 | + const void *const desc_beg, |
| 71 | + const void *const desc_end) |
| 72 | +{ |
| 73 | + struct test_class_priv *priv = c_data->priv; |
| 74 | + |
| 75 | + zassert_equal(priv->state, TEST_CLASS_PRIV_ENABLED); |
| 76 | + |
| 77 | + ; |
| 78 | + |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | +static int test_class_removed(struct usbh_class_data *const c_data, |
| 83 | + struct usb_device *const udev) |
| 84 | +{ |
| 85 | + struct test_class_priv *priv = c_data->priv; |
| 86 | + |
| 87 | + zassert_equal(priv->state, TEST_CLASS_PRIV_ENABLED); |
| 88 | + |
| 89 | + return 0; |
| 90 | +} |
| 91 | + |
| 92 | +static int test_class_rwup(struct usbh_class_data *const c_data, |
| 93 | + struct usb_device *const udev) |
| 94 | +{ |
| 95 | + struct test_class_priv *priv = c_data->priv; |
| 96 | + |
| 97 | + zassert_equal(priv->state, TEST_CLASS_PRIV_ENABLED); |
| 98 | + |
| 99 | + return 0; |
| 100 | +} |
| 101 | + |
| 102 | +static int test_class_suspended(struct usbh_class_data *const c_data, |
| 103 | + struct usb_device *const udev) |
| 104 | +{ |
| 105 | + struct test_class_priv *priv = c_data->priv; |
| 106 | + |
| 107 | + zassert_equal(priv->state, TEST_CLASS_PRIV_ENABLED); |
| 108 | + |
| 109 | + return 0; |
| 110 | +} |
| 111 | + |
| 112 | +static int test_class_resumed(struct usbh_class_data *const c_data, |
| 113 | + struct usb_device *const udev) |
| 114 | +{ |
| 115 | + struct test_class_priv *priv = c_data->priv; |
| 116 | + |
| 117 | + zassert_equal(priv->state, TEST_CLASS_PRIV_ENABLED); |
| 118 | + |
| 119 | + return 0; |
| 120 | +} |
| 121 | + |
| 122 | +static struct usbh_class_api test_class_api = { |
| 123 | + .init = &test_class_init, |
| 124 | + .completion_cb = &test_class_completion_cb, |
| 125 | + .probe = &test_class_probe, |
| 126 | + .removed = &test_class_removed, |
| 127 | + .rwup = &test_class_rwup, |
| 128 | + .suspended = &test_class_suspended, |
| 129 | + .resumed = &test_class_resumed, |
| 130 | +}; |
| 131 | + |
| 132 | +/* Define a class used in the tests */ |
| 133 | +USBH_DEFINE_CLASS(test_class, &test_class_api, &test_class_priv); |
| 134 | + |
| 135 | +ZTEST(host_class, test_class_fake_device) |
| 136 | +{ |
| 137 | + struct usbh_class_data *c_data = test_class.c_data; |
| 138 | + struct usb_device *udev = &test_udev; |
| 139 | + struct test_class_priv *priv = c_data->priv; |
| 140 | + const void *desc_intf; |
| 141 | + const void *desc_beg = test_hub_descriptor; |
| 142 | + const void *desc_end = test_hub_descriptor + sizeof(test_hub_descriptor); |
| 143 | + int ret; |
| 144 | + |
| 145 | + zassert_equal(priv->state, TEST_CLASS_PRIV_ENABLED, |
| 146 | + "The class should have been initialized by usbh_init()"); |
| 147 | + |
| 148 | + /* Get the pointer to the first interface */ |
| 149 | + desc_intf = usbh_desc_get_by_type(desc_beg, desc_end, BIT(USB_DESC_INTERFACE)); |
| 150 | + zassert_not_null(desc_intf, "Could not find the interface from test descriptors"); |
| 151 | + |
| 152 | + /* assume this interface continuse till the end and initialize the class on it */ |
| 153 | + ret = usbh_class_probe(c_data, udev, desc_intf, desc_end); |
| 154 | + zassert_ok(ret, ". (%d)", ret); |
| 155 | +} |
| 156 | + |
| 157 | +static void *usb_test_enable(void) |
| 158 | +{ |
| 159 | + int ret; |
| 160 | + |
| 161 | + ret = usbh_init(&uhs_ctx); |
| 162 | + zassert_ok(ret, "Failed to initialize USB host (%d)", ret); |
| 163 | + |
| 164 | + ret = usbh_enable(&uhs_ctx); |
| 165 | + zassert_ok(ret, "Failed to enable USB host (%d)", ret); |
| 166 | + |
| 167 | + ret = uhc_bus_reset(uhs_ctx.dev); |
| 168 | + zassert_ok(ret, "Failed to signal bus reset (%d)", ret); |
| 169 | + |
| 170 | + ret = uhc_bus_resume(uhs_ctx.dev); |
| 171 | + zassert_ok(ret, "Failed to signal bus resume (%d)", ret); |
| 172 | + |
| 173 | + ret = uhc_sof_enable(uhs_ctx.dev); |
| 174 | + zassert_ok(ret, "Failed to enable SoF generator (%d)", ret); |
| 175 | + |
| 176 | + LOG_INF("Host controller enabled"); |
| 177 | + |
| 178 | + /* Allow the host time to reset the host. */ |
| 179 | + k_msleep(200); |
| 180 | + |
| 181 | + return NULL; |
| 182 | +} |
| 183 | + |
| 184 | +static void usb_test_shutdown(void *f) |
| 185 | +{ |
| 186 | + int ret; |
| 187 | + |
| 188 | + ret = usbh_disable(&uhs_ctx); |
| 189 | + zassert_ok(ret, "Failed to enable host support (%d)", ret); |
| 190 | + |
| 191 | + ret = usbh_shutdown(&uhs_ctx); |
| 192 | + zassert_ok(ret, "Failed to shutdown host support (%d)", ret); |
| 193 | + |
| 194 | + LOG_INF("Host controller disabled"); |
| 195 | +} |
| 196 | + |
| 197 | +ZTEST_SUITE(host_class, NULL, usb_test_enable, NULL, NULL, usb_test_shutdown); |
0 commit comments