1 /*
2  * Copyright (c) 2018 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zephyr/tc_util.h>
9 
10 #include <zephyr/sys/byteorder.h>
11 #include <zephyr/usb/usb_device.h>
12 
13 /* Max packet size for endpoints */
14 #if defined(CONFIG_USB_DC_HAS_HS_SUPPORT)
15 #define BULK_EP_MPS		512
16 #else
17 #define BULK_EP_MPS		64
18 #endif
19 
20 #define ENDP_BULK_IN		0x81
21 
22 #define VALID_EP		ENDP_BULK_IN
23 #define INVALID_EP		0x20
24 
25 struct usb_device_desc {
26 	struct usb_if_descriptor if0;
27 	struct usb_ep_descriptor if0_in_ep;
28 } __packed;
29 
30 #define INITIALIZER_IF							\
31 	{								\
32 		.bLength = sizeof(struct usb_if_descriptor),		\
33 		.bDescriptorType = USB_DESC_INTERFACE,			\
34 		.bInterfaceNumber = 0,					\
35 		.bAlternateSetting = 0,					\
36 		.bNumEndpoints = 1,					\
37 		.bInterfaceClass = USB_BCC_VENDOR,			\
38 		.bInterfaceSubClass = 0,				\
39 		.bInterfaceProtocol = 0,				\
40 		.iInterface = 0,					\
41 	}
42 
43 #define INITIALIZER_IF_EP(addr, attr, mps, interval)			\
44 	{								\
45 		.bLength = sizeof(struct usb_ep_descriptor),		\
46 		.bDescriptorType = USB_DESC_ENDPOINT,			\
47 		.bEndpointAddress = addr,				\
48 		.bmAttributes = attr,					\
49 		.wMaxPacketSize = sys_cpu_to_le16(mps),			\
50 		.bInterval = interval,					\
51 	}
52 
53 USBD_CLASS_DESCR_DEFINE(primary, 0) struct usb_device_desc dev_desc = {
54 	.if0 = INITIALIZER_IF,
55 	.if0_in_ep = INITIALIZER_IF_EP(ENDP_BULK_IN, USB_DC_EP_BULK,
56 				       BULK_EP_MPS, 0),
57 };
58 
status_cb(struct usb_cfg_data * cfg,enum usb_dc_status_code status,const uint8_t * param)59 static void status_cb(struct usb_cfg_data *cfg,
60 		      enum usb_dc_status_code status,
61 		      const uint8_t *param)
62 {
63 	ARG_UNUSED(cfg);
64 	ARG_UNUSED(status);
65 	ARG_UNUSED(param);
66 }
67 
68 /* EP Bulk IN handler, used to send data to the Host */
bulk_in(uint8_t ep,enum usb_dc_ep_cb_status_code ep_status)69 static void bulk_in(uint8_t ep, enum usb_dc_ep_cb_status_code ep_status)
70 {
71 }
72 
73 /* Describe EndPoints configuration */
74 static struct usb_ep_cfg_data device_ep[] = {
75 	{
76 		.ep_cb = bulk_in,
77 		.ep_addr = ENDP_BULK_IN
78 	},
79 };
80 
81 USBD_DEFINE_CFG_DATA(device_config) = {
82 	.usb_device_description = NULL,
83 	.interface_descriptor = &dev_desc.if0,
84 	.cb_usb_status = status_cb,
85 	.interface = {
86 		.vendor_handler = NULL,
87 		.class_handler = NULL,
88 		.custom_handler = NULL,
89 	},
90 	.num_endpoints = ARRAY_SIZE(device_ep),
91 	.endpoint = device_ep,
92 };
93 
ZTEST(device_usb,test_usb_disable)94 ZTEST(device_usb, test_usb_disable)
95 {
96 	zassert_equal(usb_disable(), TC_PASS, "usb_disable() failed");
97 }
98 
ZTEST(device_usb,test_usb_deconfig)99 ZTEST(device_usb, test_usb_deconfig)
100 {
101 	zassert_equal(usb_deconfig(), TC_PASS, "usb_deconfig() failed");
102 }
103 
104 /* Test USB Device Controller API */
ZTEST(device_usb,test_usb_dc_api)105 ZTEST(device_usb, test_usb_dc_api)
106 {
107 	/* Control endpoints are configured */
108 	zassert_equal(usb_dc_ep_mps(0x0), 64,
109 		      "usb_dc_ep_mps(0x00) failed");
110 	zassert_equal(usb_dc_ep_mps(0x80), 64,
111 		      "usb_dc_ep_mps(0x80) failed");
112 
113 	/* Bulk EP is not configured yet */
114 	zassert_equal(usb_dc_ep_mps(ENDP_BULK_IN), 0,
115 		      "usb_dc_ep_mps(ENDP_BULK_IN) not configured");
116 }
117 
118 /* Test USB Device Controller API for invalid parameters */
ZTEST(device_usb,test_usb_dc_api_invalid)119 ZTEST(device_usb, test_usb_dc_api_invalid)
120 {
121 	uint32_t size;
122 	uint8_t byte;
123 
124 	/* Set stall to invalid EP */
125 	zassert_not_equal(usb_dc_ep_set_stall(INVALID_EP), TC_PASS,
126 			  "usb_dc_ep_set_stall(INVALID_EP)");
127 
128 	/* Clear stall to invalid EP */
129 	zassert_not_equal(usb_dc_ep_clear_stall(INVALID_EP), TC_PASS,
130 			  "usb_dc_ep_clear_stall(INVALID_EP)");
131 
132 	/* Check if the selected endpoint is stalled */
133 	zassert_not_equal(usb_dc_ep_is_stalled(INVALID_EP, &byte), TC_PASS,
134 			  "usb_dc_ep_is_stalled(INVALID_EP, stalled)");
135 	zassert_not_equal(usb_dc_ep_is_stalled(VALID_EP, NULL), TC_PASS,
136 			  "usb_dc_ep_is_stalled(VALID_EP, NULL)");
137 
138 	/* Halt invalid EP */
139 	zassert_not_equal(usb_dc_ep_halt(INVALID_EP), TC_PASS,
140 			  "usb_dc_ep_halt(INVALID_EP)");
141 
142 	/* Enable invalid EP */
143 	zassert_not_equal(usb_dc_ep_enable(INVALID_EP), TC_PASS,
144 			  "usb_dc_ep_enable(INVALID_EP)");
145 
146 	/* Disable invalid EP */
147 	zassert_not_equal(usb_dc_ep_disable(INVALID_EP), TC_PASS,
148 			  "usb_dc_ep_disable(INVALID_EP)");
149 
150 	/* Flush invalid EP */
151 	zassert_not_equal(usb_dc_ep_flush(INVALID_EP), TC_PASS,
152 			  "usb_dc_ep_flush(INVALID_EP)");
153 
154 	/* Set callback to invalid EP */
155 	zassert_not_equal(usb_dc_ep_set_callback(INVALID_EP, NULL), TC_PASS,
156 			  "usb_dc_ep_set_callback(INVALID_EP, NULL)");
157 
158 	/* Write to invalid EP */
159 	zassert_not_equal(usb_dc_ep_write(INVALID_EP, &byte, sizeof(byte),
160 					  &size),
161 			  TC_PASS, "usb_dc_ep_write(INVALID_EP)");
162 
163 	/* Read invalid EP */
164 	zassert_not_equal(usb_dc_ep_read(INVALID_EP, &byte, sizeof(byte),
165 					 &size),
166 			  TC_PASS, "usb_dc_ep_read(INVALID_EP)");
167 	zassert_not_equal(usb_dc_ep_read_wait(INVALID_EP, &byte, sizeof(byte),
168 					      &size),
169 			  TC_PASS, "usb_dc_ep_read_wait(INVALID_EP)");
170 	zassert_not_equal(usb_dc_ep_read_continue(INVALID_EP), TC_PASS,
171 			  "usb_dc_ep_read_continue(INVALID_EP)");
172 
173 	/* Get endpoint max packet size for invalid EP */
174 	zassert_not_equal(usb_dc_ep_mps(INVALID_EP), TC_PASS,
175 			  "usb_dc_ep_mps(INVALID_EP)");
176 }
177 
ZTEST(device_usb,test_usb_dc_api_read_write)178 ZTEST(device_usb, test_usb_dc_api_read_write)
179 {
180 	uint32_t size;
181 	uint8_t byte;
182 
183 	/* Read invalid EP */
184 	zassert_not_equal(usb_read(INVALID_EP, &byte, sizeof(byte), &size),
185 			  TC_PASS, "usb_read(INVALID_EP)");
186 
187 	/* Write to invalid EP */
188 	zassert_not_equal(usb_write(INVALID_EP, &byte, sizeof(byte), &size),
189 			  TC_PASS, "usb_write(INVALID_EP)");
190 }
191 
192 /* test case main entry */
device_usb_setup(void)193 static void *device_usb_setup(void)
194 {
195 	int ret;
196 
197 	ret = usb_enable(NULL);
198 	zassert_true(ret == 0, "Failed to enable USB");
199 	/*
200 	 *Judge failure whether is due to failing to enable USB.
201 	 */
202 
203 	return NULL;
204 }
205 ZTEST_SUITE(device_usb, NULL, device_usb_setup, NULL, NULL, NULL);
206