Skip to content

Commit 50d00f1

Browse files
author
Josuah Demangeon
committed
tests: usb: host: test basic API functionality
Add tests making sure the USB Host class APIs introduced build and run as expected. Signed-off-by: Josuah Demangeon <[email protected]>
1 parent f17fdaa commit 50d00f1

20 files changed

+662
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) 2025 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
6+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7+
project(test_usb_host)
8+
9+
target_include_directories(app PRIVATE ${ZEPHYR_BASE}/subsys/usb/host)
10+
target_include_directories(app PRIVATE ${ZEPHYR_BASE}/tests/subsys/usb/host/common/include)
11+
12+
target_sources(app PRIVATE src/main.c)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000000
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2023 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/delete-node/ &zephyr_udc0;
8+
9+
/ {
10+
zephyr_uhc0: uhc_vrt0 {
11+
compatible = "zephyr,uhc-virtual";
12+
13+
zephyr_udc0: udc_vrt0 {
14+
compatible = "zephyr,udc-virtual";
15+
num-bidir-endpoints = <8>;
16+
maximum-speed = "high-speed";
17+
};
18+
};
19+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000000
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "native_sim.overlay"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) 2023 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
zephyr_uhc0: uhc_vrt0 {
9+
compatible = "zephyr,uhc-virtual";
10+
11+
zephyr_udc0: udc_vrt0 {
12+
compatible = "zephyr,udc-virtual";
13+
num-bidir-endpoints = <8>;
14+
maximum-speed = "high-speed";
15+
};
16+
};
17+
};
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_TEST_USB_HOST_SAMPLE_DESC_H_
8+
#define ZEPHYR_TEST_USB_HOST_SAMPLE_DESC_H_
9+
10+
#include <stddef.h>
11+
#include <stdint.h>
12+
13+
#define _LE16(n) ((n) & 0xff), ((n) >> 8)
14+
15+
/*
16+
* Obtained with lsusb then verified against the
17+
* USB 2.0 standard's sample HUB descriptor
18+
*/
19+
20+
#define TEST_HUB_DEVICE_DESCRIPTOR \
21+
18, /* bLength */ \
22+
1, /* bDescriptorType */ \
23+
_LE16(0x0200), /* bcdUSB */ \
24+
0x09, /* bDeviceClass */ \
25+
0x00, /* bDeviceSubClass */ \
26+
0x02, /* bDeviceProtocol */ \
27+
64, /* bMaxPacketSize0 */ \
28+
_LE16(0x0bda), /* idVendor */ \
29+
_LE16(0x5411), /* idProduct */ \
30+
_LE16(0x0001), /* bcdDevice */ \
31+
0, /* iManufacturer */ \
32+
0, /* iProduct */ \
33+
0, /* iSerial */ \
34+
1, /* bNumConfigurations */
35+
36+
#define TEST_HUB_CONFIG_DESCRIPTOR \
37+
9, /* bLength */ \
38+
2, /* bDescriptorType */ \
39+
_LE16(0x0029), /* wTotalLength */ \
40+
1, /* bNumInterfaces */ \
41+
1, /* bConfigurationValue */ \
42+
0, /* iConfiguration */ \
43+
0xe0, /* bmAttributes */ \
44+
0, /* MaxPower */
45+
46+
#define TEST_HUB_INTERFACE_ALT0_DESCRIPTOR \
47+
9, /* bLength */ \
48+
4, /* bDescriptorType */ \
49+
0, /* bInterfaceNumber */ \
50+
0, /* bAlternateSetting */ \
51+
1, /* bNumEndpoints */ \
52+
9, /* bInterfaceClass */ \
53+
0, /* bInterfaceSubClass */ \
54+
1, /* bInterfaceProtocol */ \
55+
0, /* iInterface */
56+
57+
#define TEST_HUB_INTERFACE_ALT1_DESCRIPTOR \
58+
9, /* bLength */ \
59+
4, /* bDescriptorType */ \
60+
0, /* bInterfaceNumber */ \
61+
1, /* bAlternateSetting */ \
62+
1, /* bNumEndpoints */ \
63+
9, /* bInterfaceClass */ \
64+
0, /* bInterfaceSubClass */ \
65+
2, /* bInterfaceProtocol */ \
66+
0, /* iInterface */
67+
68+
#define TEST_HUB_ENDPOINT_DESCRIPTOR \
69+
7, /* bLength */ \
70+
5, /* bDescriptorType */ \
71+
0x81, /* bEndpointAddress */ \
72+
0x03, /* bmAttributes */ \
73+
_LE16(1), /* wMaxPacketSize */ \
74+
12, /* bInterval */
75+
76+
#define TEST_HUB_DESCRIPTOR \
77+
TEST_HUB_DEVICE_DESCRIPTOR \
78+
TEST_HUB_CONFIG_DESCRIPTOR \
79+
TEST_HUB_INTERFACE_ALT0_DESCRIPTOR \
80+
TEST_HUB_ENDPOINT_DESCRIPTOR \
81+
TEST_HUB_INTERFACE_ALT1_DESCRIPTOR \
82+
TEST_HUB_ENDPOINT_DESCRIPTOR
83+
84+
#endif /* ZEPHYR_TEST_USB_HOST_SAMPLE_DESC_H_ */
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2025 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
CONFIG_LOG=y
5+
CONFIG_ZTEST=y
6+
7+
CONFIG_USB_HOST_STACK=y
8+
CONFIG_UHC_DRIVER=y
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
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_IDLE,
33+
/* Test value stored after the class is probed */
34+
TEST_CLASS_PRIV_ENABLED,
35+
/* Test value stored after the class is initialized */
36+
TEST_CLASS_PRIV_INITIALIZED,
37+
/* Test value stored after the class is suspended */
38+
TEST_CLASS_PRIV_SUSPENDED,
39+
} state;
40+
};
41+
42+
static struct test_class_priv test_class_priv = {
43+
.state = TEST_CLASS_PRIV_INACTIVE,
44+
};
45+
46+
static int test_class_init(struct usbh_class_data *const c_data,
47+
struct usbh_context *const uhs_ctx)
48+
{
49+
struct test_class_priv *priv = c_data->priv;
50+
51+
LOG_DBG("initializing %p, priv value 0x%x", c_data, *priv);
52+
53+
zassert_equal(priv->state, TEST_CLASS_PRIV_INACTIVE,
54+
"Class should be initialized only once");
55+
56+
priv->state = TEST_CLASS_PRIV_IDLE;
57+
58+
return 0;
59+
}
60+
61+
static int test_class_completion_cb(struct usbh_class_data *const c_data,
62+
struct uhc_transfer *const xfer)
63+
{
64+
struct test_class_priv *priv = c_data->priv;
65+
66+
LOG_DBG("completion callback for %p, transfer %p", c_data, xfer);
67+
68+
zassert_equal(priv->state, TEST_CLASS_PRIV_ENABLED);
69+
70+
return -ENOTSUP;
71+
}
72+
73+
static int test_class_probe(struct usbh_class_data *const c_data,
74+
struct usb_device *const udev,
75+
const uint8_t ifnum)
76+
{
77+
struct test_class_priv *priv = c_data->priv;
78+
const void *const desc_beg = test_hub_descriptor;
79+
const void *const desc_end = test_hub_descriptor + sizeof(test_hub_descriptor);
80+
const struct usb_desc_header *desc = desc_beg;
81+
const struct usb_if_descriptor *if_desc;
82+
83+
zassert_equal(priv->state, TEST_CLASS_PRIV_IDLE);
84+
85+
desc = usbh_desc_get_by_ifnum(desc, desc_end, ifnum);
86+
if (desc == NULL) {
87+
return -ENOENT;
88+
}
89+
90+
if (desc->bDescriptorType != USB_DESC_INTERFACE) {
91+
return -ENOTSUP;
92+
}
93+
94+
if_desc = (struct usb_if_descriptor *)desc;
95+
if (if_desc->bInterfaceClass != USB_HUB_CLASSCODE ||
96+
if_desc->bInterfaceSubClass != 0x00) {
97+
return -ENOTSUP;
98+
}
99+
100+
priv->state = TEST_CLASS_PRIV_ENABLED;
101+
102+
return 0;
103+
}
104+
105+
static int test_class_removed(struct usbh_class_data *const c_data)
106+
{
107+
struct test_class_priv *priv = c_data->priv;
108+
109+
zassert_equal(priv->state, TEST_CLASS_PRIV_ENABLED);
110+
111+
priv->state = TEST_CLASS_PRIV_IDLE;
112+
113+
return 0;
114+
}
115+
116+
static int test_class_suspended(struct usbh_class_data *const c_data)
117+
{
118+
struct test_class_priv *priv = c_data->priv;
119+
120+
zassert_equal(priv->state, TEST_CLASS_PRIV_ENABLED);
121+
122+
priv->state = TEST_CLASS_PRIV_SUSPENDED;
123+
124+
return 0;
125+
}
126+
127+
static int test_class_resumed(struct usbh_class_data *const c_data)
128+
{
129+
struct test_class_priv *priv = c_data->priv;
130+
131+
zassert_equal(priv->state, TEST_CLASS_PRIV_SUSPENDED);
132+
133+
priv->state = TEST_CLASS_PRIV_ENABLED;
134+
135+
return 0;
136+
}
137+
138+
static struct usbh_class_api test_class_api = {
139+
.init = &test_class_init,
140+
.completion_cb = &test_class_completion_cb,
141+
.probe = &test_class_probe,
142+
.removed = &test_class_removed,
143+
.suspended = &test_class_suspended,
144+
.resumed = &test_class_resumed,
145+
};
146+
147+
/* Define a class used in the tests */
148+
USBH_DEFINE_CLASS(test_class, &test_class_api, &test_class_priv);
149+
150+
ZTEST(host_class, test_class_fake_device)
151+
{
152+
struct usbh_class_data *c_data = test_class.c_data;
153+
struct usb_device *udev = &test_udev;
154+
struct test_class_priv *priv = c_data->priv;
155+
int ret;
156+
157+
zassert_equal(priv->state, TEST_CLASS_PRIV_IDLE,
158+
"The class should have been initialized by usbh_init()");
159+
160+
ret = usbh_class_probe(c_data, udev, 1);
161+
zassert_equal(ret, -ENOENT, "There is no interface 1 so should be rejected");
162+
zassert_equal(priv->state, TEST_CLASS_PRIV_IDLE,
163+
"The class should not be enabled if probing failed");
164+
165+
ret = usbh_class_probe(c_data, udev, 0);
166+
zassert_ok(ret, "The interface 0 should match in this example class (%s)", strerror(-ret));
167+
zassert_equal(priv->state, TEST_CLASS_PRIV_ENABLED,
168+
"The class should be enabled if probing succeeded");
169+
170+
ret = usbh_class_suspended(c_data);
171+
zassert_ok(ret, "Susupending the class while it is running should succeed");
172+
zassert_equal(priv->state, TEST_CLASS_PRIV_SUSPENDED,
173+
"The class private state should have been updated");
174+
175+
ret = usbh_class_resumed(c_data);
176+
zassert_ok(ret, "Resuming the class after suspending should succeed");
177+
zassert_equal(priv->state, TEST_CLASS_PRIV_ENABLED,
178+
"The class private state should have been updated");
179+
180+
ret = usbh_class_removed(c_data);
181+
zassert_ok(ret, "Removing the class after probing it should succeed");
182+
zassert_equal(priv->state, TEST_CLASS_PRIV_IDLE,
183+
"The class should be back to inactive ");
184+
185+
ret = usbh_class_probe(c_data, udev, 0);
186+
zassert_ok(ret, "Probing the class again should succeed");
187+
zassert_equal(priv->state, TEST_CLASS_PRIV_ENABLED,
188+
"The class should be back to active ");
189+
190+
}
191+
192+
static void *usb_test_enable(void)
193+
{
194+
int ret;
195+
196+
ret = usbh_init(&uhs_ctx);
197+
zassert_ok(ret, "Failed to initialize USB host (%s)", strerror(-ret));
198+
199+
ret = usbh_enable(&uhs_ctx);
200+
zassert_ok(ret, "Failed to enable USB host (%s)", strerror(-ret));
201+
202+
ret = uhc_bus_reset(uhs_ctx.dev);
203+
zassert_ok(ret, "Failed to signal bus reset (%s)", strerror(-ret));
204+
205+
ret = uhc_bus_resume(uhs_ctx.dev);
206+
zassert_ok(ret, "Failed to signal bus resume (%s)", strerror(-ret));
207+
208+
ret = uhc_sof_enable(uhs_ctx.dev);
209+
zassert_ok(ret, "Failed to enable SoF generator (%s)", strerror(-ret));
210+
211+
LOG_INF("Host controller enabled");
212+
213+
/* Allow the host time to reset the host. */
214+
k_msleep(200);
215+
216+
return NULL;
217+
}
218+
219+
static void usb_test_shutdown(void *f)
220+
{
221+
int ret;
222+
223+
ret = usbh_disable(&uhs_ctx);
224+
zassert_ok(ret, "Failed to enable host support (%s)", strerror(-ret));
225+
226+
ret = usbh_shutdown(&uhs_ctx);
227+
zassert_ok(ret, "Failed to shutdown host support (%s)", strerror(-ret));
228+
229+
LOG_INF("Host controller disabled");
230+
}
231+
232+
ZTEST_SUITE(host_class, NULL, usb_test_enable, NULL, NULL, usb_test_shutdown);

0 commit comments

Comments
 (0)