Skip to content

Commit 1ea9a74

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 d99d5fd commit 1ea9a74

File tree

11 files changed

+458
-0
lines changed

11 files changed

+458
-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 include)
11+
12+
target_sources(app PRIVATE src/class.c src/usbh_desc.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_ */

tests/subsys/usb/host/prj.conf

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

tests/subsys/usb/host/src/class.c

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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

Comments
 (0)