Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * ffs-test.c -- user mode filesystem api for usb composite function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2010 Samsung Electronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *                    Author: Michal Nazarewicz <mina86@mina86.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) /* $(CROSS_COMPILE)cc -Wall -Wextra -g -o ffs-test ffs-test.c -lpthread */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #define _DEFAULT_SOURCE /* for endian.h */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <endian.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <pthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <tools/le_byteshift.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include "../../include/uapi/linux/usb/functionfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) /******************** Little Endian Handling ********************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * cpu_to_le16/32 are used when initializing structures, a context where a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * function call is not allowed. To solve this, we code cpu_to_le16/32 in a way
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * that allows them to be used when initializing structures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #if __BYTE_ORDER == __LITTLE_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define cpu_to_le16(x)  (x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define cpu_to_le32(x)  (x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define cpu_to_le16(x)  ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define cpu_to_le32(x)  \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >>  8) | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	(((x) & 0x0000ff00u) <<  8) | (((x) & 0x000000ffu) << 24))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define le32_to_cpu(x)  le32toh(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define le16_to_cpu(x)  le16toh(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) /******************** Messages and Errors ***********************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static const char argv0[] = "ffs-test";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static unsigned verbosity = 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static void _msg(unsigned level, const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (level < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		level = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	else if (level > 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		level = 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	if (level <= verbosity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		static const char levels[8][6] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			[2] = "crit:",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			[3] = "err: ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			[4] = "warn:",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			[5] = "note:",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			[6] = "info:",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			[7] = "dbg: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		int _errno = errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		va_list ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		fprintf(stderr, "%s: %s ", argv0, levels[level]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		va_start(ap, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		vfprintf(stderr, fmt, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		va_end(ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		if (fmt[strlen(fmt) - 1] != '\n') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			char buffer[128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			strerror_r(_errno, buffer, sizeof buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			fprintf(stderr, ": (-%d) %s\n", _errno, buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		fflush(stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) #define die(...)  (_msg(2, __VA_ARGS__), exit(1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) #define err(...)   _msg(3, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) #define warn(...)  _msg(4, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) #define note(...)  _msg(5, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) #define info(...)  _msg(6, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) #define debug(...) _msg(7, __VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #define die_on(cond, ...) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (cond) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		die(__VA_ARGS__); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /******************** Descriptors and Strings *******************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct usb_functionfs_descs_head_v2 header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	__le32 fs_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	__le32 hs_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	__le32 ss_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		struct usb_interface_descriptor intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		struct usb_endpoint_descriptor_no_audio sink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		struct usb_endpoint_descriptor_no_audio source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	} __attribute__((packed)) fs_descs, hs_descs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		struct usb_interface_descriptor intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		struct usb_endpoint_descriptor_no_audio sink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		struct usb_ss_ep_comp_descriptor sink_comp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		struct usb_endpoint_descriptor_no_audio source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		struct usb_ss_ep_comp_descriptor source_comp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	} ss_descs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) } __attribute__((packed)) descriptors = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	.header = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		.flags = cpu_to_le32(FUNCTIONFS_HAS_FS_DESC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 				     FUNCTIONFS_HAS_HS_DESC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 				     FUNCTIONFS_HAS_SS_DESC),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		.length = cpu_to_le32(sizeof descriptors),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	.fs_count = cpu_to_le32(3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	.fs_descs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		.intf = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			.bLength = sizeof descriptors.fs_descs.intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			.bDescriptorType = USB_DT_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			.bNumEndpoints = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			.bInterfaceClass = USB_CLASS_VENDOR_SPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			.iInterface = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		.sink = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			.bLength = sizeof descriptors.fs_descs.sink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			.bDescriptorType = USB_DT_ENDPOINT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			.bEndpointAddress = 1 | USB_DIR_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			.bmAttributes = USB_ENDPOINT_XFER_BULK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			/* .wMaxPacketSize = autoconfiguration (kernel) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		.source = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			.bLength = sizeof descriptors.fs_descs.source,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			.bDescriptorType = USB_DT_ENDPOINT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			.bEndpointAddress = 2 | USB_DIR_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			.bmAttributes = USB_ENDPOINT_XFER_BULK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			/* .wMaxPacketSize = autoconfiguration (kernel) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	.hs_count = cpu_to_le32(3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	.hs_descs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		.intf = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			.bLength = sizeof descriptors.fs_descs.intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			.bDescriptorType = USB_DT_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			.bNumEndpoints = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			.bInterfaceClass = USB_CLASS_VENDOR_SPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			.iInterface = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		.sink = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			.bLength = sizeof descriptors.hs_descs.sink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			.bDescriptorType = USB_DT_ENDPOINT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			.bEndpointAddress = 1 | USB_DIR_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			.bmAttributes = USB_ENDPOINT_XFER_BULK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			.wMaxPacketSize = cpu_to_le16(512),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		.source = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			.bLength = sizeof descriptors.hs_descs.source,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			.bDescriptorType = USB_DT_ENDPOINT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			.bEndpointAddress = 2 | USB_DIR_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			.bmAttributes = USB_ENDPOINT_XFER_BULK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			.wMaxPacketSize = cpu_to_le16(512),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			.bInterval = 1, /* NAK every 1 uframe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	.ss_count = cpu_to_le32(5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	.ss_descs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		.intf = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 			.bLength = sizeof descriptors.fs_descs.intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			.bDescriptorType = USB_DT_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			.bNumEndpoints = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			.bInterfaceClass = USB_CLASS_VENDOR_SPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			.iInterface = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		.sink = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			.bLength = sizeof descriptors.hs_descs.sink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			.bDescriptorType = USB_DT_ENDPOINT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			.bEndpointAddress = 1 | USB_DIR_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			.bmAttributes = USB_ENDPOINT_XFER_BULK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			.wMaxPacketSize = cpu_to_le16(1024),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		.sink_comp = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			.bLength = USB_DT_SS_EP_COMP_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			.bMaxBurst = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			.bmAttributes = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			.wBytesPerInterval = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		.source = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			.bLength = sizeof descriptors.hs_descs.source,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			.bDescriptorType = USB_DT_ENDPOINT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			.bEndpointAddress = 2 | USB_DIR_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			.bmAttributes = USB_ENDPOINT_XFER_BULK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			.wMaxPacketSize = cpu_to_le16(1024),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			.bInterval = 1, /* NAK every 1 uframe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		.source_comp = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			.bLength = USB_DT_SS_EP_COMP_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			.bMaxBurst = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			.bmAttributes = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			.wBytesPerInterval = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static size_t descs_to_legacy(void **legacy, const void *descriptors_v2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	const unsigned char *descs_end, *descs_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	__u32 length, fs_count = 0, hs_count = 0, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	/* Read v2 header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			const struct usb_functionfs_descs_head_v2 header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			const __le32 counts[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		} __attribute__((packed)) *const in = descriptors_v2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		const __le32 *counts = in->counts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		__u32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		if (le32_to_cpu(in->header.magic) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		    FUNCTIONFS_DESCRIPTORS_MAGIC_V2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		length = le32_to_cpu(in->header.length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		if (length <= sizeof in->header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		length -= sizeof in->header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		flags = le32_to_cpu(in->header.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		if (flags & ~(FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			      FUNCTIONFS_HAS_SS_DESC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) #define GET_NEXT_COUNT_IF_FLAG(ret, flg) do {		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			if (!(flags & (flg)))		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 				break;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			if (length < 4)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 				return 0;		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			ret = le32_to_cpu(*counts);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			length -= 4;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			++counts;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		GET_NEXT_COUNT_IF_FLAG(fs_count, FUNCTIONFS_HAS_FS_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		GET_NEXT_COUNT_IF_FLAG(hs_count, FUNCTIONFS_HAS_HS_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		GET_NEXT_COUNT_IF_FLAG(count, FUNCTIONFS_HAS_SS_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		count = fs_count + hs_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		descs_start = (const void *)counts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) #undef GET_NEXT_COUNT_IF_FLAG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	 * Find the end of FS and HS USB descriptors.  SS descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	 * are ignored since legacy format does not support them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	descs_end = descs_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		if (length < *descs_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		length -= *descs_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		descs_end += *descs_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	} while (--count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	/* Allocate legacy descriptors and copy the data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) #pragma GCC diagnostic push
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			struct usb_functionfs_descs_head header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			__u8 descriptors[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		} __attribute__((packed)) *out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) #pragma GCC diagnostic pop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		length = sizeof out->header + (descs_end - descs_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		out = malloc(length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		out->header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		out->header.length = cpu_to_le32(length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		out->header.fs_count = cpu_to_le32(fs_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		out->header.hs_count = cpu_to_le32(hs_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		memcpy(out->descriptors, descs_start, descs_end - descs_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		*legacy = out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	return length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) #define STR_INTERFACE_ "Source/Sink"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	struct usb_functionfs_strings_head header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		__le16 code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		const char str1[sizeof STR_INTERFACE_];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	} __attribute__((packed)) lang0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) } __attribute__((packed)) strings = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	.header = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		.magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		.length = cpu_to_le32(sizeof strings),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		.str_count = cpu_to_le32(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		.lang_count = cpu_to_le32(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	.lang0 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		cpu_to_le16(0x0409), /* en-us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		STR_INTERFACE_,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) #define STR_INTERFACE strings.lang0.str1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) /******************** Files and Threads Handling ****************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static ssize_t read_wrap(struct thread *t, void *buf, size_t nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static ssize_t write_wrap(struct thread *t, const void *buf, size_t nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static ssize_t ep0_consume(struct thread *t, const void *buf, size_t nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static ssize_t fill_in_buf(struct thread *t, void *buf, size_t nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static ssize_t empty_out_buf(struct thread *t, const void *buf, size_t nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static struct thread {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	const char *const filename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	size_t buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	ssize_t (*in)(struct thread *, void *, size_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	const char *const in_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	ssize_t (*out)(struct thread *, const void *, size_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	const char *const out_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	pthread_t id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	ssize_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) } threads[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		"ep0", 4 * sizeof(struct usb_functionfs_event),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		read_wrap, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		ep0_consume, "<consume>",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		0, 0, NULL, 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		"ep1", 8 * 1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		fill_in_buf, "<in>",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		write_wrap, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		0, 0, NULL, 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		"ep2", 8 * 1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		read_wrap, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		empty_out_buf, "<out>",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		0, 0, NULL, 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static void init_thread(struct thread *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	t->buf = malloc(t->buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	die_on(!t->buf, "malloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	t->fd = open(t->filename, O_RDWR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	die_on(t->fd < 0, "%s", t->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static void cleanup_thread(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	struct thread *t = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	int ret, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	fd = t->fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	if (t->fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	t->fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	/* test the FIFO ioctls (non-ep0 code paths) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	if (t != threads) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		ret = ioctl(fd, FUNCTIONFS_FIFO_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 			/* ENODEV reported after disconnect */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			if (errno != ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 				err("%s: get fifo status", t->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		} else if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			warn("%s: unclaimed = %d\n", t->filename, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			if (ioctl(fd, FUNCTIONFS_FIFO_FLUSH) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 				err("%s: fifo flush", t->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	if (close(fd) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		err("%s: close", t->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	free(t->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	t->buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static void *start_thread_helper(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	const char *name, *op, *in_name, *out_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	struct thread *t = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	info("%s: starts\n", t->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	in_name = t->in_name ? t->in_name : t->filename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	out_name = t->out_name ? t->out_name : t->filename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	pthread_cleanup_push(cleanup_thread, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		pthread_testcancel();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		ret = t->in(t, t->buf, t->buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 			ret = t->out(t, t->buf, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 			name = out_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 			op = "write";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 			name = in_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			op = "read";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			/* nop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		} else if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 			debug("%s: %s: EOF", name, op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		} else if (errno == EINTR || errno == EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 			debug("%s: %s", name, op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 			warn("%s: %s", name, op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	pthread_cleanup_pop(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	t->status = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	info("%s: ends\n", t->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) static void start_thread(struct thread *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	debug("%s: starting\n", t->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	die_on(pthread_create(&t->id, NULL, start_thread_helper, t) < 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	       "pthread_create(%s)", t->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) static void join_thread(struct thread *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	int ret = pthread_join(t->id, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		err("%s: joining thread", t->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		debug("%s: joined\n", t->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) static ssize_t read_wrap(struct thread *t, void *buf, size_t nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	return read(t->fd, buf, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) static ssize_t write_wrap(struct thread *t, const void *buf, size_t nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	return write(t->fd, buf, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) /******************** Empty/Fill buffer routines ****************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) /* 0 -- stream of zeros, 1 -- i % 63, 2 -- pipe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) enum pattern { PAT_ZERO, PAT_SEQ, PAT_PIPE };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) static enum pattern pattern;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) fill_in_buf(struct thread *ignore, void *buf, size_t nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	__u8 *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	(void)ignore;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	switch (pattern) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	case PAT_ZERO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		memset(buf, 0, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	case PAT_SEQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		for (p = buf, i = 0; i < nbytes; ++i, ++p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 			*p = i % 63;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	case PAT_PIPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		return fread(buf, 1, nbytes, stdin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	return nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) empty_out_buf(struct thread *ignore, const void *buf, size_t nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	const __u8 *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	__u8 expected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	(void)ignore;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	switch (pattern) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	case PAT_ZERO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		expected = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		for (p = buf, len = 0; len < nbytes; ++p, ++len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 			if (*p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 				goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	case PAT_SEQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		for (p = buf, len = 0; len < nbytes; ++p, ++len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 			if (*p != len % 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 				expected = len % 63;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 				goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	case PAT_PIPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		ret = fwrite(buf, nbytes, 1, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 			fflush(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) invalid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		err("bad OUT byte %zd, expected %02x got %02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		    len, expected, *p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		for (p = buf, len = 0; len < nbytes; ++p, ++len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 			if (0 == (len % 32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 				fprintf(stderr, "%4zd:", len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 			fprintf(stderr, " %02x", *p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 			if (31 == (len % 32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 				fprintf(stderr, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		fflush(stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		errno = EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) /******************** Endpoints routines ************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) static void handle_setup(const struct usb_ctrlrequest *setup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	printf("bRequestType = %d\n", setup->bRequestType);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	printf("bRequest     = %d\n", setup->bRequest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	printf("wValue       = %d\n", le16_to_cpu(setup->wValue));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	printf("wIndex       = %d\n", le16_to_cpu(setup->wIndex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	printf("wLength      = %d\n", le16_to_cpu(setup->wLength));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) ep0_consume(struct thread *ignore, const void *buf, size_t nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	static const char *const names[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		[FUNCTIONFS_BIND] = "BIND",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		[FUNCTIONFS_UNBIND] = "UNBIND",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		[FUNCTIONFS_ENABLE] = "ENABLE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		[FUNCTIONFS_DISABLE] = "DISABLE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 		[FUNCTIONFS_SETUP] = "SETUP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		[FUNCTIONFS_SUSPEND] = "SUSPEND",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 		[FUNCTIONFS_RESUME] = "RESUME",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	const struct usb_functionfs_event *event = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	size_t n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	(void)ignore;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	for (n = nbytes / sizeof *event; n; --n, ++event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 		switch (event->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 		case FUNCTIONFS_BIND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		case FUNCTIONFS_UNBIND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		case FUNCTIONFS_ENABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		case FUNCTIONFS_DISABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		case FUNCTIONFS_SETUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 		case FUNCTIONFS_SUSPEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		case FUNCTIONFS_RESUME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 			printf("Event %s\n", names[event->type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 			if (event->type == FUNCTIONFS_SETUP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 				handle_setup(&event->u.setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 			printf("Event %03u (unknown)\n", event->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	return nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) static void ep0_init(struct thread *t, bool legacy_descriptors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	void *legacy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	if (legacy_descriptors) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 		info("%s: writing descriptors\n", t->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 		goto legacy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	info("%s: writing descriptors (in v2 format)\n", t->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	ret = write(t->fd, &descriptors, sizeof descriptors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	if (ret < 0 && errno == EINVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 		warn("%s: new format rejected, trying legacy\n", t->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) legacy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 		len = descs_to_legacy(&legacy, &descriptors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 		if (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 			ret = write(t->fd, legacy, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 			free(legacy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	die_on(ret < 0, "%s: write: descriptors", t->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	info("%s: writing strings\n", t->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	ret = write(t->fd, &strings, sizeof strings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	die_on(ret < 0, "%s: write: strings", t->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) /******************** Main **************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	bool legacy_descriptors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	legacy_descriptors = argc > 2 && !strcmp(argv[1], "-l");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	init_thread(threads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	ep0_init(threads, legacy_descriptors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	for (i = 1; i < sizeof threads / sizeof *threads; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		init_thread(threads + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	for (i = 1; i < sizeof threads / sizeof *threads; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 		start_thread(threads + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	start_thread_helper(threads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	for (i = 1; i < sizeof threads / sizeof *threads; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 		join_thread(threads + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }