1 /*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <zephyr/usb/usbd.h>
9 #include <zephyr/usb/usbh.h>
10
11 #include "usbh_ch9.h"
12 #include "usbh_device.h"
13
14 #include <zephyr/logging/log.h>
15 LOG_MODULE_REGISTER(usb_test, LOG_LEVEL_INF);
16
17 #define TEST_DEFAULT_INTERFACE 0
18 #define TEST_DEFAULT_ALTERNATE 1
19
20 USBD_CONFIGURATION_DEFINE(test_fs_config,
21 USB_SCD_SELF_POWERED | USB_SCD_REMOTE_WAKEUP,
22 200, NULL);
23
24 USBD_CONFIGURATION_DEFINE(test_hs_config,
25 USB_SCD_SELF_POWERED | USB_SCD_REMOTE_WAKEUP,
26 200, NULL);
27
28
29 USBD_DESC_LANG_DEFINE(test_lang);
30 USBD_DESC_STRING_DEFINE(test_mfg, "ZEPHYR", 1);
31 USBD_DESC_STRING_DEFINE(test_product, "Zephyr USB Test", 2);
32 USBD_DESC_STRING_DEFINE(test_sn, "0123456789ABCDEF", 3);
33
34 USBD_DEVICE_DEFINE(test_usbd,
35 DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0)),
36 0x2fe3, 0xffff);
37
38 USBH_CONTROLLER_DEFINE(uhs_ctx, DEVICE_DT_GET(DT_NODELABEL(zephyr_uhc0)));
39
40 /* Get Configuration request test */
ZTEST(device_next,test_get_configuration)41 ZTEST(device_next, test_get_configuration)
42 {
43 struct usb_device *udev;
44 uint8_t cfg = 0;
45 int err;
46
47 udev = usbh_device_get_any(&uhs_ctx);
48 err = usbh_req_get_cfg(udev, &cfg);
49
50 switch (udev->state) {
51 case USB_STATE_DEFAULT:
52 /* Not specified, expect protocol error */
53 zassert_equal(err, -EPIPE, "Transfer status is not a protocol error");
54 break;
55 case USB_STATE_ADDRESSED:
56 /* TODO: Expect zero value */
57 zassert_equal(err, 0, "Transfer status is an error");
58 zassert_equal(cfg, 0, "Device not in address state");
59 break;
60 case USB_STATE_CONFIGURED:
61 /* TODO: Expect non-zero valid configuration value */
62 zassert_equal(err, 0, "Transfer status is an error");
63 zassert_not_equal(cfg, 0, "Device not in configured state");
64 break;
65 default:
66 break;
67 }
68 }
69
70 /* Set Interface request test */
ZTEST(device_next,test_set_interface)71 ZTEST(device_next, test_set_interface)
72 {
73 struct usb_device *udev;
74 int err;
75
76 udev = usbh_device_get_any(&uhs_ctx);
77 err = usbh_req_set_alt(udev, TEST_DEFAULT_INTERFACE,
78 TEST_DEFAULT_ALTERNATE);
79
80 switch (udev->state) {
81 case USB_STATE_DEFAULT:
82 /* Not specified, expect protocol error */
83 case USB_STATE_ADDRESSED:
84 /* Expect protocol error */
85 zassert_equal(err, -EPIPE, "Transfer status is not a protocol error");
86 break;
87 case USB_STATE_CONFIGURED:
88 /* TODO */
89 default:
90 break;
91 }
92 }
93
usb_test_enable(void)94 static void *usb_test_enable(void)
95 {
96 struct usb_device *udev;
97 int err;
98
99 err = usbh_init(&uhs_ctx);
100 zassert_equal(err, 0, "Failed to initialize USB host");
101
102 err = usbh_enable(&uhs_ctx);
103 zassert_equal(err, 0, "Failed to enable USB host");
104
105 err = uhc_bus_reset(uhs_ctx.dev);
106 zassert_equal(err, 0, "Failed to signal bus reset");
107
108 err = uhc_bus_resume(uhs_ctx.dev);
109 zassert_equal(err, 0, "Failed to signal bus resume");
110
111 err = uhc_sof_enable(uhs_ctx.dev);
112 zassert_equal(err, 0, "Failed to enable SoF generator");
113
114 LOG_INF("Host controller enabled");
115
116 err = usbd_add_descriptor(&test_usbd, &test_lang);
117 zassert_equal(err, 0, "Failed to initialize descriptor (%d)", err);
118
119 err = usbd_add_descriptor(&test_usbd, &test_mfg);
120 zassert_equal(err, 0, "Failed to initialize descriptor (%d)", err);
121
122 err = usbd_add_descriptor(&test_usbd, &test_product);
123 zassert_equal(err, 0, "Failed to initialize descriptor (%d)", err);
124
125 err = usbd_add_descriptor(&test_usbd, &test_sn);
126 zassert_equal(err, 0, "Failed to initialize descriptor (%d)", err);
127
128 if (usbd_caps_speed(&test_usbd) == USBD_SPEED_HS) {
129 err = usbd_add_configuration(&test_usbd, USBD_SPEED_HS, &test_hs_config);
130 zassert_equal(err, 0, "Failed to add configuration (%d)");
131 }
132
133 err = usbd_add_configuration(&test_usbd, USBD_SPEED_FS, &test_fs_config);
134 zassert_equal(err, 0, "Failed to add configuration (%d)");
135
136 if (usbd_caps_speed(&test_usbd) == USBD_SPEED_HS) {
137 err = usbd_register_all_classes(&test_usbd, USBD_SPEED_HS, 1);
138 zassert_equal(err, 0, "Failed to unregister all instances(%d)");
139
140 err = usbd_unregister_all_classes(&test_usbd, USBD_SPEED_HS, 1);
141 zassert_equal(err, 0, "Failed to unregister all instances(%d)");
142
143 err = usbd_register_class(&test_usbd, "loopback_0", USBD_SPEED_HS, 1);
144 zassert_equal(err, 0, "Failed to register loopback_0 class (%d)");
145 }
146
147 err = usbd_register_all_classes(&test_usbd, USBD_SPEED_FS, 1);
148 zassert_equal(err, 0, "Failed to unregister all instances(%d)");
149
150 err = usbd_unregister_all_classes(&test_usbd, USBD_SPEED_FS, 1);
151 zassert_equal(err, 0, "Failed to unregister all instances(%d)");
152
153 err = usbd_register_class(&test_usbd, "loopback_0", USBD_SPEED_FS, 1);
154 zassert_equal(err, 0, "Failed to register loopback_0 class (%d)");
155
156 err = usbd_init(&test_usbd);
157 zassert_equal(err, 0, "Failed to initialize device support");
158
159 err = usbd_enable(&test_usbd);
160 zassert_equal(err, 0, "Failed to enable device support");
161
162 LOG_INF("Device support enabled");
163 udev = usbh_device_get_any(&uhs_ctx);
164 udev->state = USB_STATE_DEFAULT;
165
166 return NULL;
167 }
168
usb_test_shutdown(void * f)169 static void usb_test_shutdown(void *f)
170 {
171 int err;
172
173 err = usbd_disable(&test_usbd);
174 zassert_equal(err, 0, "Failed to enable device support");
175
176 err = usbd_shutdown(&test_usbd);
177 zassert_equal(err, 0, "Failed to shutdown device support");
178
179 LOG_INF("Device support disabled");
180
181 err = usbh_disable(&uhs_ctx);
182 zassert_equal(err, 0, "Failed to disable USB host");
183
184 LOG_INF("Host controller disabled");
185 }
186
187 ZTEST_SUITE(device_next, NULL, usb_test_enable, NULL, NULL, usb_test_shutdown);
188