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/sys/byteorder.h>
9 
10 #include <zephyr/usb/usbd.h>
11 #include <usbd_uac2_macros.h>
12 
13 /* 48 kHz headset with implicit feedback, allowed on both Full and High-Speed */
14 static const uint8_t reference_ac_interface_descriptor[] = {
15 	/* 4.7.2 Class-Specific AC Interface Descriptor */
16 	0x09,			/* bLength = 9 */
17 	0x24,			/* bDescriptorType = CS_INTERFACE */
18 	0x01,			/* bDescriptorSubtype = HEADER */
19 	0x00, 0x02,		/* bcdADC = 02.00 */
20 	0x04,			/* bCategory = HEADSET */
21 	0x6b, 0x00,		/* wTotalLength = 0x6b = 107 */
22 	0x00,			/* bmControls = Latency Control not present */
23 };
24 
25 static const uint8_t reference_ac_clock_source_descriptor[] = {
26 	/* 4.7.2.1 Clock Source Descriptor */
27 	0x08,			/* bLength = 8 */
28 	0x24,			/* bDescriptorType = CS_INTERFACE */
29 	0x0a,			/* bDescriptorSubtype = CLOCK_SOURCE */
30 	0x01,			/* bClockID = 1 */
31 	0x03,			/* bmAttributes = Internal programmable */
32 	0x03,			/* bmControls = frequency host programmable */
33 	0x00,			/* bAssocTerminal = 0 (not associated) */
34 	0x00,			/* iClockSource = 0 (no string descriptor) */
35 };
36 
37 static const uint8_t reference_ac_hp_input_terminal_descriptor[] = {
38 	/* 4.7.2.4 Input Terminal Descriptor */
39 	0x11,			/* bLength = 17 */
40 	0x24,			/* bDescriptorType = CS_INTERFACE */
41 	0x02,			/* bDescriptorSubtype = INPUT_TERMINAL */
42 	0x02,			/* bTerminalID = 2 */
43 	0x01, 0x01,		/* wTerminalType = 0x0101 (USB streaming) */
44 	0x00,			/* bAssocTerminal = 0 (not associated) */
45 	0x01,			/* bCSourceID = 1 (main clock) */
46 	0x02,			/* bNrChannels = 2 */
47 	0x03, 0x00, 0x00, 0x00,	/* bmChannelConfig = Front Left, Front Right */
48 	0x00,			/* iChannelNames = 0 (all pre-defined) */
49 	0x00, 0x00,		/* bmControls = none present */
50 	0x00,			/* iTerminal = 0 (no string descriptor) */
51 };
52 
53 static const uint8_t reference_ac_hp_feature_unit_descriptor[] = {
54 	/* 4.7.2.8 Feature Unit Descriptor */
55 	0x12,			/* bLength = 18 */
56 	0x24,			/* bDescriptorType = CS_INTERFACE */
57 	0x06,			/* bDescriptorSubtype = FEATURE_UNIT */
58 	0x03,			/* bUnitID = 3 */
59 	0x02,			/* bSourceID = 2 (streaming input) */
60 	0x03, 0x30, 0x00, 0x00, /* bmaControls(0): Mute and Auto Gain */
61 	0x00, 0x30, 0x00, 0x00, /* bmaControls(1): Auto Gain */
62 	0x00, 0x30, 0x00, 0x00, /* bmaControls(2): Auto Gain */
63 	0x00,			/* iFeature = 0 (no string descriptor)*/
64 };
65 
66 static const uint8_t reference_ac_hp_output_terminal_descriptor[] = {
67 	/* 4.7.2.5 Output Terminal Descriptor */
68 	0x0c,			/* bLength = 12 */
69 	0x24,			/* bDescriptorType = CS_INTERFACE */
70 	0x03,			/* bDescriptorSubtype = OUTPUT_TERMINAL */
71 	0x04,			/* bTerminalID = 4 */
72 	0x02, 0x04,		/* wTerminalType = 0x0402 (Headset) */
73 	0x05,			/* bAssocTerminal = 5 (headset input) */
74 	0x03,			/* bSourceID = 3 (headphones feature unit) */
75 	0x01,			/* bCSourceID = 1 (main clock) */
76 	0x00, 0x00,		/* bmControls = none present */
77 	0x00,			/* iTerminal = 0 (no string descriptor) */
78 };
79 
80 static const uint8_t reference_ac_mic_input_terminal_descriptor[] = {
81 	/* 4.7.2.4 Input Terminal Descriptor */
82 	0x11,			/* bLength = 17 */
83 	0x24,			/* bDescriptorType = CS_INTERFACE */
84 	0x02,			/* bDescriptorSubtype = INPUT_TERMINAL */
85 	0x05,			/* bTerminalID = 5 */
86 	0x02, 0x04,		/* wTerminalType = 0x0402 (Headset) */
87 	0x04,			/* bAssocTerminal = 4 (headset output) */
88 	0x01,			/* bCSourceID = 1 (main clock) */
89 	0x01,			/* bNrChannels = 1 */
90 	0x01, 0x00, 0x00, 0x00,	/* bmChannelConfig = Front Left */
91 	0x00,			/* iChannelNames = 0 (all pre-defined) */
92 	0x00, 0x00,		/* bmControls = none present */
93 	0x00,			/* iTerminal = 0 (no string descriptor) */
94 };
95 
96 static const uint8_t reference_ac_mic_feature_unit_descriptor[] = {
97 	/* 4.7.2.8 Feature Unit Descriptor */
98 	0x0e,			/* bLength = 14 */
99 	0x24,			/* bDescriptorType = CS_INTERFACE */
100 	0x06,			/* bDescriptorSubtype = FEATURE_UNIT */
101 	0x06,			/* bUnitID = 6 */
102 	0x05,			/* bSourceID = 5 (headset input) */
103 	0x03, 0x00, 0x00, 0x00, /* bmaControls(0): Mute */
104 	0x00, 0x30, 0x00, 0x00, /* bmaControls(1): Auto Gain */
105 	0x00,			/* iFeature = 0 (no string descriptor)*/
106 };
107 
108 static const uint8_t reference_ac_mic_output_terminal_descriptor[] = {
109 	/* 4.7.2.5 Output Terminal Descriptor */
110 	0x0c,			/* bLength = 12 */
111 	0x24,			/* bDescriptorType = CS_INTERFACE */
112 	0x03,			/* bDescriptorSubtype = OUTPUT_TERMINAL */
113 	0x07,			/* bTerminalID = 7 */
114 	0x01, 0x01,		/* wTerminalType = 0x0101 (USB streaming) */
115 	0x00,			/* bAssocTerminal = 0 (not associated) */
116 	0x06,			/* bSourceID = 6 (mic feature unit) */
117 	0x01,			/* bCSourceID = 1 (main clock) */
118 	0x00, 0x00,		/* bmControls = none present */
119 	0x00,			/* iTerminal = 0 (no string descriptor) */
120 };
121 
122 /* USB IN = Audio device streaming output */
123 static const uint8_t reference_as_in_cs_general_descriptor[] = {
124 	/* 4.9.2 Class-Specific AS Interface Descriptor */
125 	0x10,			/* bLength = 16 */
126 	0x24,			/* bDescriptorType = CS_INTERFACE */
127 	0x01,			/* bDescriptorSubtype = AS_GENERAL */
128 	0x07,			/* bTerminalLink = 7 (USB streaming output) */
129 	0x00,			/* bmControls = non present */
130 	0x01,			/* bFormatType = 1 */
131 	0x01, 0x00, 0x00, 0x00,	/* bmFormats = PCM */
132 	0x01,			/* bNrChannels = 1 */
133 	0x01, 0x00, 0x00, 0x00,	/* bmChannelConfig = Front Left */
134 	0x00,			/* iChannelNames = 0 (all pre-defined) */
135 };
136 
137 static const uint8_t reference_as_in_cs_format_descriptor[] = {
138 	/* Universal Serial Bus Device Class Definition for Audio Data Formats
139 	 * Release 2.0, May 31, 2006. 2.3.1.6 Type I Format Type Descriptor
140 	 */
141 	0x06,			/* bLength = 6 */
142 	0x24,			/* bDescriptorType = CS_INTERFACE */
143 	0x02,			/* bDescriptorSubtype = FORMAT_TYPE */
144 	0x01,			/* bFormatType = 1 */
145 	0x02,			/* bSubslotSize = 2 */
146 	0x10,			/* bBitResolution = 16 */
147 };
148 
149 /* USB OUT = Audio device streaming input */
150 static const uint8_t reference_as_out_cs_general_descriptor[] = {
151 	/* 4.9.2 Class-Specific AS Interface Descriptor */
152 	0x10,			/* bLength = 16 */
153 	0x24,			/* bDescriptorType = CS_INTERFACE */
154 	0x01,			/* bDescriptorSubtype = AS_GENERAL */
155 	0x02,			/* bTerminalLink = 2 (USB streaming input) */
156 	0x00,			/* bmControls = non present */
157 	0x01,			/* bFormatType = 1 */
158 	0x01, 0x00, 0x00, 0x00,	/* bmFormats = PCM */
159 	0x02,			/* bNrChannels = 2 */
160 	0x03, 0x00, 0x00, 0x00,	/* bmChannelConfig = Front Left, Front Right */
161 	0x00,			/* iChannelNames = 0 (all pre-defined) */
162 };
163 
164 static const uint8_t reference_as_out_cs_format_descriptor[] = {
165 	/* Universal Serial Bus Device Class Definition for Audio Data Formats
166 	 * Release 2.0, May 31, 2006. 2.3.1.6 Type I Format Type Descriptor
167 	 */
168 	0x06,			/* bLength = 6 */
169 	0x24,			/* bDescriptorType = CS_INTERFACE */
170 	0x02,			/* bDescriptorSubtype = FORMAT_TYPE */
171 	0x01,			/* bFormatType = 1 */
172 	0x02,			/* bSubslotSize = 2 */
173 	0x10,			/* bBitResolution = 16 */
174 };
175 
176 static const uint8_t reference_as_ep_descriptor[] = {
177 	/* 4.10.1.2 Class-Specific AS Isochronous Audio Data Endpoint Descriptor
178 	 */
179 	0x08,			/* bLength = 8 */
180 	0x25,			/* bDescriptorType = CS_ENDPOINT */
181 	0x01,			/* bDescriptorSubtype = EP_GENERAL */
182 	0x00,			/* bmAttributes = MaxPacketsOnly not set */
183 	0x00,			/* bmControls = non present */
184 	0x00,			/* bLockDelayUnits = 0 (Undefined) */
185 	0x00, 0x00,		/* wLockDelay = 0 */
186 };
187 
188 VALIDATE_INSTANCE(DT_NODELABEL(uac2_headset))
189 
190 UAC2_DESCRIPTOR_ARRAYS(DT_NODELABEL(uac2_headset))
191 
192 const static struct usb_desc_header *uac2_fs_descriptor_set[] =
193 	UAC2_FS_DESCRIPTOR_PTRS_ARRAY(DT_NODELABEL(uac2_headset));
194 
195 const static struct usb_desc_header *uac2_hs_descriptor_set[] =
196 	UAC2_HS_DESCRIPTOR_PTRS_ARRAY(DT_NODELABEL(uac2_headset));
197 
198 VALIDATE_INSTANCE(DT_NODELABEL(uac2_headset))
199 
200 /* 192 kHz 24-bit stereo headphones allowed on High-Speed only */
201 static const uint8_t reference_hs_ac_interface_descriptor[] = {
202 	/* 4.7.2 Class-Specific AC Interface Descriptor */
203 	0x09,			/* bLength = 9 */
204 	0x24,			/* bDescriptorType = CS_INTERFACE */
205 	0x01,			/* bDescriptorSubtype = HEADER */
206 	0x00, 0x02,		/* bcdADC = 02.00 */
207 	0xff,			/* bCategory = OTHER */
208 	0x2e, 0x00,		/* wTotalLength = 0x2e = 46 */
209 	0x00,			/* bmControls = Latency Control not present */
210 };
211 
212 static const uint8_t reference_hs_ac_clock_source_descriptor[] = {
213 	/* 4.7.2.1 Clock Source Descriptor */
214 	0x08,			/* bLength = 8 */
215 	0x24,			/* bDescriptorType = CS_INTERFACE */
216 	0x0a,			/* bDescriptorSubtype = CLOCK_SOURCE */
217 	0x01,			/* bClockID = 1 */
218 	0x03,			/* bmAttributes = Internal programmable */
219 	0x03,			/* bmControls = frequency host programmable */
220 	0x00,			/* bAssocTerminal = 0 (not associated) */
221 	0x00,			/* iClockSource = 0 (no string descriptor) */
222 };
223 
224 static const uint8_t reference_hs_ac_input_terminal_descriptor[] = {
225 	/* 4.7.2.4 Input Terminal Descriptor */
226 	0x11,			/* bLength = 17 */
227 	0x24,			/* bDescriptorType = CS_INTERFACE */
228 	0x02,			/* bDescriptorSubtype = INPUT_TERMINAL */
229 	0x02,			/* bTerminalID = 2 */
230 	0x01, 0x01,		/* wTerminalType = 0x0101 (USB streaming) */
231 	0x00,			/* bAssocTerminal = 0 (not associated) */
232 	0x01,			/* bCSourceID = 1 (main clock) */
233 	0x02,			/* bNrChannels = 2 */
234 	0x03, 0x00, 0x00, 0x00,	/* bmChannelConfig = Front Left, Front Right */
235 	0x00,			/* iChannelNames = 0 (all pre-defined) */
236 	0x00, 0x00,		/* bmControls = none present */
237 	0x00,			/* iTerminal = 0 (no string descriptor) */
238 };
239 
240 static const uint8_t reference_hs_ac_output_terminal_descriptor[] = {
241 	/* 4.7.2.5 Output Terminal Descriptor */
242 	0x0c,			/* bLength = 12 */
243 	0x24,			/* bDescriptorType = CS_INTERFACE */
244 	0x03,			/* bDescriptorSubtype = OUTPUT_TERMINAL */
245 	0x03,			/* bTerminalID = 3 */
246 	0x02, 0x03,		/* wTerminalType = 0x0302 (Headphones) */
247 	0x00,			/* bAssocTerminal = 0 (none) */
248 	0x02,			/* bSourceID = 2 (streaming input) */
249 	0x01,			/* bCSourceID = 1 (main clock) */
250 	0x00, 0x00,		/* bmControls = none present */
251 	0x00,			/* iTerminal = 0 (no string descriptor) */
252 };
253 
254 static const uint8_t reference_hs_as_cs_general_descriptor[] = {
255 	/* 4.9.2 Class-Specific AS Interface Descriptor */
256 	0x10,			/* bLength = 16 */
257 	0x24,			/* bDescriptorType = CS_INTERFACE */
258 	0x01,			/* bDescriptorSubtype = AS_GENERAL */
259 	0x02,			/* bTerminalLink = 2 (USB streaming input) */
260 	0x00,			/* bmControls = non present */
261 	0x01,			/* bFormatType = 1 */
262 	0x01, 0x00, 0x00, 0x00,	/* bmFormats = PCM */
263 	0x02,			/* bNrChannels = 2 */
264 	0x03, 0x00, 0x00, 0x00,	/* bmChannelConfig = Front Left, Front Right */
265 	0x00,			/* iChannelNames = 0 (all pre-defined) */
266 };
267 
268 static const uint8_t reference_hs_as_cs_format_descriptor[] = {
269 	/* Universal Serial Bus Device Class Definition for Audio Data Formats
270 	 * Release 2.0, May 31, 2006. 2.3.1.6 Type I Format Type Descriptor
271 	 */
272 	0x06,			/* bLength = 6 */
273 	0x24,			/* bDescriptorType = CS_INTERFACE */
274 	0x02,			/* bDescriptorSubtype = FORMAT_TYPE */
275 	0x01,			/* bFormatType = 1 */
276 	0x03,			/* bSubslotSize = 3 */
277 	0x18,			/* bBitResolution = 24 */
278 };
279 
280 VALIDATE_INSTANCE(DT_NODELABEL(hs_uac2_headphones))
281 
282 UAC2_DESCRIPTOR_ARRAYS(DT_NODELABEL(hs_uac2_headphones))
283 
284 const static struct usb_desc_header *fs_headphones_descriptor_set[] =
285 	UAC2_FS_DESCRIPTOR_PTRS_ARRAY(DT_NODELABEL(hs_uac2_headphones));
286 
287 const static struct usb_desc_header *hs_headphones_descriptor_set[] =
288 	UAC2_HS_DESCRIPTOR_PTRS_ARRAY(DT_NODELABEL(hs_uac2_headphones));
289 
test_uac2_descriptors(const struct usb_desc_header ** descriptors,enum usbd_speed speed)290 static void test_uac2_descriptors(const struct usb_desc_header **descriptors,
291 				  enum usbd_speed speed)
292 {
293 	const struct usb_desc_header **ptr = descriptors;
294 
295 	const struct usb_association_descriptor *iad;
296 	const struct usb_if_descriptor *iface;
297 	const struct usb_ep_descriptor *ep;
298 
299 	/* Headset has 3 interfaces: 1 AudioControl and 2 AudioStreaming */
300 	iad = (const struct usb_association_descriptor *)*ptr;
301 	zassert_not_null(iad);
302 	zassert_equal(iad->bLength, sizeof(struct usb_association_descriptor));
303 	zassert_equal(iad->bDescriptorType, USB_DESC_INTERFACE_ASSOC);
304 	zassert_equal(iad->bFirstInterface, FIRST_INTERFACE_NUMBER);
305 	zassert_equal(iad->bInterfaceCount, 3);
306 	zassert_equal(iad->bFunctionClass, AUDIO_FUNCTION);
307 	zassert_equal(iad->bFunctionSubClass, FUNCTION_SUBCLASS_UNDEFINED);
308 	zassert_equal(iad->bFunctionProtocol, AF_VERSION_02_00);
309 	zassert_equal(iad->iFunction, 0);
310 	ptr++;
311 
312 	/* AudioControl interface goes first */
313 	iface = (const struct usb_if_descriptor *)*ptr;
314 	zassert_not_null(iface);
315 	zassert_equal(iface->bLength, sizeof(struct usb_if_descriptor));
316 	zassert_equal(iface->bDescriptorType, USB_DESC_INTERFACE);
317 	zassert_equal(iface->bInterfaceNumber, FIRST_INTERFACE_NUMBER);
318 	zassert_equal(iface->bAlternateSetting, 0);
319 	zassert_equal(iface->bNumEndpoints, 0);
320 	zassert_equal(iface->bInterfaceClass, AUDIO);
321 	zassert_equal(iface->bInterfaceSubClass, AUDIOCONTROL);
322 	zassert_equal(iface->bInterfaceProtocol, IP_VERSION_02_00);
323 	zassert_equal(iface->iInterface, 0);
324 	ptr++;
325 
326 	/* AudioControl class-specific descriptors */
327 	zassert_mem_equal(reference_ac_interface_descriptor, *ptr,
328 		ARRAY_SIZE(reference_ac_interface_descriptor));
329 	ptr++;
330 	zassert_mem_equal(reference_ac_clock_source_descriptor, *ptr,
331 		ARRAY_SIZE(reference_ac_clock_source_descriptor));
332 	ptr++;
333 	zassert_mem_equal(reference_ac_hp_input_terminal_descriptor, *ptr,
334 		ARRAY_SIZE(reference_ac_hp_input_terminal_descriptor));
335 	ptr++;
336 	zassert_mem_equal(reference_ac_hp_feature_unit_descriptor, *ptr,
337 		ARRAY_SIZE(reference_ac_hp_feature_unit_descriptor));
338 	ptr++;
339 	zassert_mem_equal(reference_ac_hp_output_terminal_descriptor, *ptr,
340 		ARRAY_SIZE(reference_ac_hp_output_terminal_descriptor));
341 	ptr++;
342 	zassert_mem_equal(reference_ac_mic_input_terminal_descriptor, *ptr,
343 		ARRAY_SIZE(reference_ac_mic_input_terminal_descriptor));
344 	ptr++;
345 	zassert_mem_equal(reference_ac_mic_feature_unit_descriptor, *ptr,
346 		ARRAY_SIZE(reference_ac_mic_feature_unit_descriptor));
347 	ptr++;
348 	zassert_mem_equal(reference_ac_mic_output_terminal_descriptor, *ptr,
349 		ARRAY_SIZE(reference_ac_mic_output_terminal_descriptor));
350 	ptr++;
351 
352 	/* AudioStreaming OUT interface Alt 0 without endpoints */
353 	iface = (const struct usb_if_descriptor *)*ptr;
354 	zassert_not_null(iface);
355 	zassert_equal(iface->bLength, sizeof(struct usb_if_descriptor));
356 	zassert_equal(iface->bDescriptorType, USB_DESC_INTERFACE);
357 	zassert_equal(iface->bInterfaceNumber, FIRST_INTERFACE_NUMBER + 1);
358 	zassert_equal(iface->bAlternateSetting, 0);
359 	zassert_equal(iface->bNumEndpoints, 0);
360 	zassert_equal(iface->bInterfaceClass, AUDIO);
361 	zassert_equal(iface->bInterfaceSubClass, AUDIOSTREAMING);
362 	zassert_equal(iface->bInterfaceProtocol, IP_VERSION_02_00);
363 	zassert_equal(iface->iInterface, 0);
364 	ptr++;
365 
366 	/* AudioStreaming OUT interface Alt 1 with endpoint */
367 	iface = (const struct usb_if_descriptor *)*ptr;
368 	zassert_not_null(iface);
369 	zassert_equal(iface->bLength, sizeof(struct usb_if_descriptor));
370 	zassert_equal(iface->bDescriptorType, USB_DESC_INTERFACE);
371 	zassert_equal(iface->bInterfaceNumber, FIRST_INTERFACE_NUMBER + 1);
372 	zassert_equal(iface->bAlternateSetting, 1);
373 	zassert_equal(iface->bNumEndpoints, 1);
374 	zassert_equal(iface->bInterfaceClass, AUDIO);
375 	zassert_equal(iface->bInterfaceSubClass, AUDIOSTREAMING);
376 	zassert_equal(iface->bInterfaceProtocol, IP_VERSION_02_00);
377 	zassert_equal(iface->iInterface, 0);
378 	ptr++;
379 
380 	/* AudioStreaming OUT class-specific descriptors */
381 	zassert_mem_equal(reference_as_out_cs_general_descriptor, *ptr,
382 		ARRAY_SIZE(reference_as_out_cs_general_descriptor));
383 	ptr++;
384 	zassert_mem_equal(reference_as_out_cs_format_descriptor, *ptr,
385 		ARRAY_SIZE(reference_as_out_cs_format_descriptor));
386 	ptr++;
387 
388 	/* Isochronous OUT endpoint descriptor */
389 	ep = (const struct usb_ep_descriptor *)*ptr;
390 	zassert_not_null(ep);
391 	zassert_equal(ep->bLength, sizeof(struct usb_ep_descriptor));
392 	zassert_equal(ep->bDescriptorType, USB_DESC_ENDPOINT);
393 	zassert_equal(ep->bEndpointAddress, FIRST_OUT_EP_ADDR);
394 	zassert_equal(ptr - descriptors,
395 		UAC2_DESCRIPTOR_AS_DATA_EP_INDEX(DT_NODELABEL(as_iso_out)));
396 	zassert_equal(ep->Attributes.transfer, USB_EP_TYPE_ISO);
397 	zassert_equal(ep->Attributes.synch, 1 /* Asynchronous */);
398 	zassert_equal(ep->Attributes.usage, 0 /* Data Endpoint */);
399 	if (speed == USBD_SPEED_FS) {
400 		zassert_equal(sys_le16_to_cpu(ep->wMaxPacketSize), 196);
401 	} else {
402 		zassert_equal(speed, USBD_SPEED_HS);
403 		zassert_equal(sys_le16_to_cpu(ep->wMaxPacketSize), 28);
404 	}
405 	zassert_equal(ep->bInterval, 1);
406 	ptr++;
407 
408 	/* AudioStreaming OUT endpoint descriptor */
409 	zassert_mem_equal(reference_as_ep_descriptor, *ptr,
410 		ARRAY_SIZE(reference_as_ep_descriptor));
411 	ptr++;
412 
413 	/* AudioStreaming IN interface Alt 0 without endpoints */
414 	iface = (const struct usb_if_descriptor *)*ptr;
415 	zassert_not_null(iface);
416 	zassert_equal(iface->bLength, sizeof(struct usb_if_descriptor));
417 	zassert_equal(iface->bDescriptorType, USB_DESC_INTERFACE);
418 	zassert_equal(iface->bInterfaceNumber, FIRST_INTERFACE_NUMBER + 2);
419 	zassert_equal(iface->bAlternateSetting, 0);
420 	zassert_equal(iface->bNumEndpoints, 0);
421 	zassert_equal(iface->bInterfaceClass, AUDIO);
422 	zassert_equal(iface->bInterfaceSubClass, AUDIOSTREAMING);
423 	zassert_equal(iface->bInterfaceProtocol, IP_VERSION_02_00);
424 	zassert_equal(iface->iInterface, 0);
425 	ptr++;
426 
427 	/* AudioStreaming IN interface Alt 1 with endpoint */
428 	iface = (const struct usb_if_descriptor *)*ptr;
429 	zassert_not_null(iface);
430 	zassert_equal(iface->bLength, sizeof(struct usb_if_descriptor));
431 	zassert_equal(iface->bDescriptorType, USB_DESC_INTERFACE);
432 	zassert_equal(iface->bInterfaceNumber, FIRST_INTERFACE_NUMBER + 2);
433 	zassert_equal(iface->bAlternateSetting, 1);
434 	zassert_equal(iface->bNumEndpoints, 1);
435 	zassert_equal(iface->bInterfaceClass, AUDIO);
436 	zassert_equal(iface->bInterfaceSubClass, AUDIOSTREAMING);
437 	zassert_equal(iface->bInterfaceProtocol, IP_VERSION_02_00);
438 	zassert_equal(iface->iInterface, 0);
439 	ptr++;
440 
441 	/* AudioStreaming IN class-specific descriptors */
442 	zassert_mem_equal(reference_as_in_cs_general_descriptor, *ptr,
443 		ARRAY_SIZE(reference_as_in_cs_general_descriptor));
444 	ptr++;
445 	zassert_mem_equal(reference_as_in_cs_format_descriptor, *ptr,
446 		ARRAY_SIZE(reference_as_in_cs_format_descriptor));
447 	ptr++;
448 
449 	/* Isochronous IN endpoint descriptor */
450 	ep = (const struct usb_ep_descriptor *)*ptr;
451 	zassert_not_null(ep);
452 	zassert_equal(ep->bLength, sizeof(struct usb_ep_descriptor));
453 	zassert_equal(ep->bDescriptorType, USB_DESC_ENDPOINT);
454 	zassert_equal(ep->bEndpointAddress, FIRST_IN_EP_ADDR);
455 	zassert_equal(ptr - descriptors,
456 		UAC2_DESCRIPTOR_AS_DATA_EP_INDEX(DT_NODELABEL(as_iso_in)));
457 	zassert_equal(ep->Attributes.transfer, USB_EP_TYPE_ISO);
458 	zassert_equal(ep->Attributes.synch, 1 /* Asynchronous */);
459 	zassert_equal(ep->Attributes.usage, 2 /* Implicit Feedback Data */);
460 	if (speed == USBD_SPEED_FS) {
461 		zassert_equal(sys_le16_to_cpu(ep->wMaxPacketSize), 98);
462 	} else {
463 		zassert_equal(speed, USBD_SPEED_HS);
464 		zassert_equal(sys_le16_to_cpu(ep->wMaxPacketSize), 14);
465 	}
466 	zassert_equal(ep->bInterval, 1);
467 	ptr++;
468 
469 	/* AudioStreaming IN endpoint descriptor */
470 	zassert_mem_equal(reference_as_ep_descriptor, *ptr,
471 		ARRAY_SIZE(reference_as_ep_descriptor));
472 	ptr++;
473 
474 	/* Confirm there is no trailing data */
475 	zassert_equal(*ptr, NULL);
476 }
477 
ZTEST(uac2_desc,test_fs_uac2_descriptors)478 ZTEST(uac2_desc, test_fs_uac2_descriptors)
479 {
480 	test_uac2_descriptors(uac2_fs_descriptor_set, USBD_SPEED_FS);
481 }
482 
ZTEST(uac2_desc,test_hs_uac2_descriptors)483 ZTEST(uac2_desc, test_hs_uac2_descriptors)
484 {
485 	test_uac2_descriptors(uac2_hs_descriptor_set, USBD_SPEED_HS);
486 }
487 
ZTEST(uac2_desc,test_fs_hs_iface_and_ep_descriptors_not_shared)488 ZTEST(uac2_desc, test_fs_hs_iface_and_ep_descriptors_not_shared)
489 {
490 	const struct usb_desc_header **fs_ptr = uac2_fs_descriptor_set;
491 	const struct usb_desc_header **hs_ptr = uac2_hs_descriptor_set;
492 
493 	while (*fs_ptr && *hs_ptr) {
494 		const struct usb_desc_header *fs = *fs_ptr;
495 		const struct usb_desc_header *hs = *hs_ptr;
496 
497 		zassert_equal(fs->bDescriptorType, hs->bDescriptorType);
498 
499 		/* IAD, Interface and Endpoint descriptors must not be shared
500 		 * because there can be different number of interfaces between
501 		 * the speeds and thus descriptor fixup can assign different
502 		 * interface numbers and/or endpoint numbers at runtime.
503 		 */
504 		if (fs->bDescriptorType == USB_DESC_INTERFACE_ASSOC) {
505 			zassert_not_equal(fs, hs, "Shared IAD descriptor");
506 		} else if (fs->bDescriptorType == USB_DESC_INTERFACE) {
507 			zassert_not_equal(fs, hs, "Shared interface descriptor");
508 		} else if (fs->bDescriptorType == USB_DESC_ENDPOINT) {
509 			zassert_not_equal(fs, hs, "Shared endpoint descriptor");
510 		} else {
511 			zassert_equal(fs, hs);
512 		}
513 
514 		fs_ptr++;
515 		hs_ptr++;
516 	}
517 }
518 
ZTEST(uac2_desc,test_hs_only_headset)519 ZTEST(uac2_desc, test_hs_only_headset)
520 {
521 	const struct usb_desc_header **ptr = hs_headphones_descriptor_set;
522 
523 	const struct usb_association_descriptor *iad;
524 	const struct usb_if_descriptor *iface;
525 	const struct usb_ep_descriptor *ep;
526 
527 	/* Allowed only at High-Speed so Full-Speed should be NULL */
528 	zassert_equal(*fs_headphones_descriptor_set, NULL);
529 
530 	/* Headset has 3 interfaces: 1 AudioControl and 2 AudioStreaming */
531 	iad = (const struct usb_association_descriptor *)*ptr;
532 	zassert_not_null(iad);
533 	zassert_equal(iad->bLength, sizeof(struct usb_association_descriptor));
534 	zassert_equal(iad->bDescriptorType, USB_DESC_INTERFACE_ASSOC);
535 	zassert_equal(iad->bFirstInterface, FIRST_INTERFACE_NUMBER);
536 	zassert_equal(iad->bInterfaceCount, 2);
537 	zassert_equal(iad->bFunctionClass, AUDIO_FUNCTION);
538 	zassert_equal(iad->bFunctionSubClass, FUNCTION_SUBCLASS_UNDEFINED);
539 	zassert_equal(iad->bFunctionProtocol, AF_VERSION_02_00);
540 	zassert_equal(iad->iFunction, 0);
541 	ptr++;
542 
543 	/* AudioControl interface goes first */
544 	iface = (const struct usb_if_descriptor *)*ptr;
545 	zassert_not_null(iface);
546 	zassert_equal(iface->bLength, sizeof(struct usb_if_descriptor));
547 	zassert_equal(iface->bDescriptorType, USB_DESC_INTERFACE);
548 	zassert_equal(iface->bInterfaceNumber, FIRST_INTERFACE_NUMBER);
549 	zassert_equal(iface->bAlternateSetting, 0);
550 	zassert_equal(iface->bNumEndpoints, 0);
551 	zassert_equal(iface->bInterfaceClass, AUDIO);
552 	zassert_equal(iface->bInterfaceSubClass, AUDIOCONTROL);
553 	zassert_equal(iface->bInterfaceProtocol, IP_VERSION_02_00);
554 	zassert_equal(iface->iInterface, 0);
555 	ptr++;
556 
557 	/* AudioControl class-specific descriptors */
558 	zassert_mem_equal(reference_hs_ac_interface_descriptor, *ptr,
559 		ARRAY_SIZE(reference_hs_ac_interface_descriptor));
560 	ptr++;
561 	zassert_mem_equal(reference_hs_ac_clock_source_descriptor, *ptr,
562 		ARRAY_SIZE(reference_hs_ac_clock_source_descriptor));
563 	ptr++;
564 	zassert_mem_equal(reference_hs_ac_input_terminal_descriptor, *ptr,
565 		ARRAY_SIZE(reference_hs_ac_input_terminal_descriptor));
566 	ptr++;
567 	zassert_mem_equal(reference_hs_ac_output_terminal_descriptor, *ptr,
568 		ARRAY_SIZE(reference_hs_ac_output_terminal_descriptor));
569 	ptr++;
570 
571 	/* AudioStreaming OUT interface Alt 0 without endpoints */
572 	iface = (const struct usb_if_descriptor *)*ptr;
573 	zassert_not_null(iface);
574 	zassert_equal(iface->bLength, sizeof(struct usb_if_descriptor));
575 	zassert_equal(iface->bDescriptorType, USB_DESC_INTERFACE);
576 	zassert_equal(iface->bInterfaceNumber, FIRST_INTERFACE_NUMBER + 1);
577 	zassert_equal(iface->bAlternateSetting, 0);
578 	zassert_equal(iface->bNumEndpoints, 0);
579 	zassert_equal(iface->bInterfaceClass, AUDIO);
580 	zassert_equal(iface->bInterfaceSubClass, AUDIOSTREAMING);
581 	zassert_equal(iface->bInterfaceProtocol, IP_VERSION_02_00);
582 	zassert_equal(iface->iInterface, 0);
583 	ptr++;
584 
585 	/* AudioStreaming OUT interface Alt 1 with endpoints */
586 	iface = (const struct usb_if_descriptor *)*ptr;
587 	zassert_not_null(iface);
588 	zassert_equal(iface->bLength, sizeof(struct usb_if_descriptor));
589 	zassert_equal(iface->bDescriptorType, USB_DESC_INTERFACE);
590 	zassert_equal(iface->bInterfaceNumber, FIRST_INTERFACE_NUMBER + 1);
591 	zassert_equal(iface->bAlternateSetting, 1);
592 	zassert_equal(iface->bNumEndpoints, 2);
593 	zassert_equal(iface->bInterfaceClass, AUDIO);
594 	zassert_equal(iface->bInterfaceSubClass, AUDIOSTREAMING);
595 	zassert_equal(iface->bInterfaceProtocol, IP_VERSION_02_00);
596 	zassert_equal(iface->iInterface, 0);
597 	ptr++;
598 
599 	/* AudioStreaming OUT class-specific descriptors */
600 	zassert_mem_equal(reference_hs_as_cs_general_descriptor, *ptr,
601 		ARRAY_SIZE(reference_hs_as_cs_general_descriptor));
602 	ptr++;
603 	zassert_mem_equal(reference_hs_as_cs_format_descriptor, *ptr,
604 		ARRAY_SIZE(reference_hs_as_cs_format_descriptor));
605 	ptr++;
606 
607 	/* Isochronous OUT endpoint descriptor */
608 	ep = (const struct usb_ep_descriptor *)*ptr;
609 	zassert_not_null(ep);
610 	zassert_equal(ep->bLength, sizeof(struct usb_ep_descriptor));
611 	zassert_equal(ep->bDescriptorType, USB_DESC_ENDPOINT);
612 	zassert_equal(ep->bEndpointAddress, FIRST_OUT_EP_ADDR);
613 	zassert_equal(ptr - hs_headphones_descriptor_set,
614 		UAC2_DESCRIPTOR_AS_DATA_EP_INDEX(DT_NODELABEL(hs_as_iso_out)));
615 	zassert_equal(ep->Attributes.transfer, USB_EP_TYPE_ISO);
616 	zassert_equal(ep->Attributes.synch, 1 /* Asynchronous */);
617 	zassert_equal(ep->Attributes.usage, 0 /* Data Endpoint */);
618 	zassert_equal(sys_le16_to_cpu(ep->wMaxPacketSize), 150);
619 	zassert_equal(ep->bInterval, 1);
620 	ptr++;
621 
622 	/* AudioStreaming OUT endpoint descriptor */
623 	zassert_mem_equal(reference_as_ep_descriptor, *ptr,
624 		ARRAY_SIZE(reference_as_ep_descriptor));
625 	ptr++;
626 
627 	/* Isochronous IN explicit feedback endpoint descriptor */
628 	ep = (const struct usb_ep_descriptor *)*ptr;
629 	zassert_not_null(ep);
630 	zassert_equal(ep->bLength, sizeof(struct usb_ep_descriptor));
631 	zassert_equal(ep->bDescriptorType, USB_DESC_ENDPOINT);
632 	zassert_equal(ep->bEndpointAddress, FIRST_IN_EP_ADDR);
633 	zassert_equal(ptr - hs_headphones_descriptor_set,
634 		UAC2_DESCRIPTOR_AS_FEEDBACK_EP_INDEX(DT_NODELABEL(hs_as_iso_out)));
635 	zassert_equal(ep->Attributes.transfer, USB_EP_TYPE_ISO);
636 	zassert_equal(ep->Attributes.synch, 0 /* No Synchronization */);
637 	zassert_equal(ep->Attributes.usage, 1 /* Explicit Feedback-Endpoint */);
638 	zassert_equal(sys_le16_to_cpu(ep->wMaxPacketSize), 4);
639 	zassert_equal(ep->bInterval, 1);
640 	ptr++;
641 
642 	/* Confirm there is no trailing data */
643 	zassert_equal(*ptr, NULL);
644 }
645 
646 ZTEST_SUITE(uac2_desc, NULL, NULL, NULL, NULL, NULL);
647