1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 #ifndef _UAPI__LINUX_FUNCTIONFS_H__
3 #define _UAPI__LINUX_FUNCTIONFS_H__
4 
5 
6 #include <linux/types.h>
7 #include <linux/ioctl.h>
8 
9 #include <linux/usb/ch9.h>
10 
11 
12 enum {
13 	FUNCTIONFS_DESCRIPTORS_MAGIC = 1,
14 	FUNCTIONFS_STRINGS_MAGIC = 2,
15 	FUNCTIONFS_DESCRIPTORS_MAGIC_V2 = 3,
16 };
17 
18 enum functionfs_flags {
19 	FUNCTIONFS_HAS_FS_DESC = 1,
20 	FUNCTIONFS_HAS_HS_DESC = 2,
21 	FUNCTIONFS_HAS_SS_DESC = 4,
22 	FUNCTIONFS_HAS_MS_OS_DESC = 8,
23 	FUNCTIONFS_VIRTUAL_ADDR = 16,
24 	FUNCTIONFS_EVENTFD = 32,
25 	FUNCTIONFS_ALL_CTRL_RECIP = 64,
26 	FUNCTIONFS_CONFIG0_SETUP = 128,
27 };
28 
29 /* Descriptor of an non-audio endpoint */
30 struct usb_endpoint_descriptor_no_audio {
31 	__u8  bLength;
32 	__u8  bDescriptorType;
33 
34 	__u8  bEndpointAddress;
35 	__u8  bmAttributes;
36 	__le16 wMaxPacketSize;
37 	__u8  bInterval;
38 } __attribute__((packed));
39 
40 struct usb_functionfs_descs_head_v2 {
41 	__le32 magic;
42 	__le32 length;
43 	__le32 flags;
44 	/*
45 	 * __le32 fs_count, hs_count, fs_count; must be included manually in
46 	 * the structure taking flags into consideration.
47 	 */
48 } __attribute__((packed));
49 
50 /* Legacy format, deprecated as of 3.14. */
51 struct usb_functionfs_descs_head {
52 	__le32 magic;
53 	__le32 length;
54 	__le32 fs_count;
55 	__le32 hs_count;
56 } __attribute__((packed, deprecated));
57 
58 /* MS OS Descriptor header */
59 struct usb_os_desc_header {
60 	__u8	interface;
61 	__le32	dwLength;
62 	__le16	bcdVersion;
63 	__le16	wIndex;
64 	union {
65 		struct {
66 			__u8	bCount;
67 			__u8	Reserved;
68 		};
69 		__le16	wCount;
70 	};
71 } __attribute__((packed));
72 
73 struct usb_ext_compat_desc {
74 	__u8	bFirstInterfaceNumber;
75 	__u8	Reserved1;
76 	__struct_group(/* no tag */, IDs, /* no attrs */,
77 		__u8	CompatibleID[8];
78 		__u8	SubCompatibleID[8];
79 	);
80 	__u8	Reserved2[6];
81 };
82 
83 struct usb_ext_prop_desc {
84 	__le32	dwSize;
85 	__le32	dwPropertyDataType;
86 	__le16	wPropertyNameLength;
87 } __attribute__((packed));
88 
89 #ifndef __KERNEL__
90 
91 /*
92  * Descriptors format:
93  *
94  * | off | name      | type         | description                          |
95  * |-----+-----------+--------------+--------------------------------------|
96  * |   0 | magic     | LE32         | FUNCTIONFS_DESCRIPTORS_MAGIC_V2      |
97  * |   4 | length    | LE32         | length of the whole data chunk       |
98  * |   8 | flags     | LE32         | combination of functionfs_flags      |
99  * |     | eventfd   | LE32         | eventfd file descriptor              |
100  * |     | fs_count  | LE32         | number of full-speed descriptors     |
101  * |     | hs_count  | LE32         | number of high-speed descriptors     |
102  * |     | ss_count  | LE32         | number of super-speed descriptors    |
103  * |     | os_count  | LE32         | number of MS OS descriptors          |
104  * |     | fs_descrs | Descriptor[] | list of full-speed descriptors       |
105  * |     | hs_descrs | Descriptor[] | list of high-speed descriptors       |
106  * |     | ss_descrs | Descriptor[] | list of super-speed descriptors      |
107  * |     | os_descrs | OSDesc[]     | list of MS OS descriptors            |
108  *
109  * Depending on which flags are set, various fields may be missing in the
110  * structure.  Any flags that are not recognised cause the whole block to be
111  * rejected with -ENOSYS.
112  *
113  * Legacy descriptors format (deprecated as of 3.14):
114  *
115  * | off | name      | type         | description                          |
116  * |-----+-----------+--------------+--------------------------------------|
117  * |   0 | magic     | LE32         | FUNCTIONFS_DESCRIPTORS_MAGIC         |
118  * |   4 | length    | LE32         | length of the whole data chunk       |
119  * |   8 | fs_count  | LE32         | number of full-speed descriptors     |
120  * |  12 | hs_count  | LE32         | number of high-speed descriptors     |
121  * |  16 | fs_descrs | Descriptor[] | list of full-speed descriptors       |
122  * |     | hs_descrs | Descriptor[] | list of high-speed descriptors       |
123  *
124  * All numbers must be in little endian order.
125  *
126  * Descriptor[] is an array of valid USB descriptors which have the following
127  * format:
128  *
129  * | off | name            | type | description              |
130  * |-----+-----------------+------+--------------------------|
131  * |   0 | bLength         | U8   | length of the descriptor |
132  * |   1 | bDescriptorType | U8   | descriptor type          |
133  * |   2 | payload         |      | descriptor's payload     |
134  *
135  * OSDesc[] is an array of valid MS OS Feature Descriptors which have one of
136  * the following formats:
137  *
138  * | off | name            | type | description              |
139  * |-----+-----------------+------+--------------------------|
140  * |   0 | inteface        | U8   | related interface number |
141  * |   1 | dwLength        | U32  | length of the descriptor |
142  * |   5 | bcdVersion      | U16  | currently supported: 1   |
143  * |   7 | wIndex          | U16  | currently supported: 4   |
144  * |   9 | bCount          | U8   | number of ext. compat.   |
145  * |  10 | Reserved        | U8   | 0                        |
146  * |  11 | ExtCompat[]     |      | list of ext. compat. d.  |
147  *
148  * | off | name            | type | description              |
149  * |-----+-----------------+------+--------------------------|
150  * |   0 | inteface        | U8   | related interface number |
151  * |   1 | dwLength        | U32  | length of the descriptor |
152  * |   5 | bcdVersion      | U16  | currently supported: 1   |
153  * |   7 | wIndex          | U16  | currently supported: 5   |
154  * |   9 | wCount          | U16  | number of ext. compat.   |
155  * |  11 | ExtProp[]       |      | list of ext. prop. d.    |
156  *
157  * ExtCompat[] is an array of valid Extended Compatiblity descriptors
158  * which have the following format:
159  *
160  * | off | name                  | type | description                         |
161  * |-----+-----------------------+------+-------------------------------------|
162  * |   0 | bFirstInterfaceNumber | U8   | index of the interface or of the 1st|
163  * |     |                       |      | interface in an IAD group           |
164  * |   1 | Reserved              | U8   | 1                                   |
165  * |   2 | CompatibleID          | U8[8]| compatible ID string                |
166  * |  10 | SubCompatibleID       | U8[8]| subcompatible ID string             |
167  * |  18 | Reserved              | U8[6]| 0                                   |
168  *
169  * ExtProp[] is an array of valid Extended Properties descriptors
170  * which have the following format:
171  *
172  * | off | name                  | type | description                         |
173  * |-----+-----------------------+------+-------------------------------------|
174  * |   0 | dwSize                | U32  | length of the descriptor            |
175  * |   4 | dwPropertyDataType    | U32  | 1..7                                |
176  * |   8 | wPropertyNameLength   | U16  | bPropertyName length (NL)           |
177  * |  10 | bPropertyName         |U8[NL]| name of this property               |
178  * |10+NL| dwPropertyDataLength  | U32  | bPropertyData length (DL)           |
179  * |14+NL| bProperty             |U8[DL]| payload of this property            |
180  */
181 
182 struct usb_functionfs_strings_head {
183 	__le32 magic;
184 	__le32 length;
185 	__le32 str_count;
186 	__le32 lang_count;
187 } __attribute__((packed));
188 
189 /*
190  * Strings format:
191  *
192  * | off | name       | type                  | description                |
193  * |-----+------------+-----------------------+----------------------------|
194  * |   0 | magic      | LE32                  | FUNCTIONFS_STRINGS_MAGIC   |
195  * |   4 | length     | LE32                  | length of the data chunk   |
196  * |   8 | str_count  | LE32                  | number of strings          |
197  * |  12 | lang_count | LE32                  | number of languages        |
198  * |  16 | stringtab  | StringTab[lang_count] | table of strings per lang  |
199  *
200  * For each language there is one stringtab entry (ie. there are lang_count
201  * stringtab entires).  Each StringTab has following format:
202  *
203  * | off | name    | type              | description                        |
204  * |-----+---------+-------------------+------------------------------------|
205  * |   0 | lang    | LE16              | language code                      |
206  * |   2 | strings | String[str_count] | array of strings in given language |
207  *
208  * For each string there is one strings entry (ie. there are str_count
209  * string entries).  Each String is a NUL terminated string encoded in
210  * UTF-8.
211  */
212 
213 #endif
214 
215 
216 /*
217  * Events are delivered on the ep0 file descriptor, when the user mode driver
218  * reads from this file descriptor after writing the descriptors.  Don't
219  * stop polling this descriptor.
220  */
221 
222 enum usb_functionfs_event_type {
223 	FUNCTIONFS_BIND,
224 	FUNCTIONFS_UNBIND,
225 
226 	FUNCTIONFS_ENABLE,
227 	FUNCTIONFS_DISABLE,
228 
229 	FUNCTIONFS_SETUP,
230 
231 	FUNCTIONFS_SUSPEND,
232 	FUNCTIONFS_RESUME
233 };
234 
235 /* NOTE:  this structure must stay the same size and layout on
236  * both 32-bit and 64-bit kernels.
237  */
238 struct usb_functionfs_event {
239 	union {
240 		/* SETUP: packet; DATA phase i/o precedes next event
241 		 *(setup.bmRequestType & USB_DIR_IN) flags direction */
242 		struct usb_ctrlrequest	setup;
243 	} __attribute__((packed)) u;
244 
245 	/* enum usb_functionfs_event_type */
246 	__u8				type;
247 	__u8				_pad[3];
248 } __attribute__((packed));
249 
250 
251 /* Endpoint ioctls */
252 /* The same as in gadgetfs */
253 
254 /* IN transfers may be reported to the gadget driver as complete
255  *	when the fifo is loaded, before the host reads the data;
256  * OUT transfers may be reported to the host's "client" driver as
257  *	complete when they're sitting in the FIFO unread.
258  * THIS returns how many bytes are "unclaimed" in the endpoint fifo
259  * (needed for precise fault handling, when the hardware allows it)
260  */
261 #define	FUNCTIONFS_FIFO_STATUS	_IO('g', 1)
262 
263 /* discards any unclaimed data in the fifo. */
264 #define	FUNCTIONFS_FIFO_FLUSH	_IO('g', 2)
265 
266 /* resets endpoint halt+toggle; used to implement set_interface.
267  * some hardware (like pxa2xx) can't support this.
268  */
269 #define	FUNCTIONFS_CLEAR_HALT	_IO('g', 3)
270 
271 /* Specific for functionfs */
272 
273 /*
274  * Returns reverse mapping of an interface.  Called on EP0.  If there
275  * is no such interface returns -EDOM.  If function is not active
276  * returns -ENODEV.
277  */
278 #define	FUNCTIONFS_INTERFACE_REVMAP	_IO('g', 128)
279 
280 /*
281  * Returns real bEndpointAddress of an endpoint. If endpoint shuts down
282  * during the call, returns -ESHUTDOWN.
283  */
284 #define	FUNCTIONFS_ENDPOINT_REVMAP	_IO('g', 129)
285 
286 /*
287  * Returns endpoint descriptor. If endpoint shuts down during the call,
288  * returns -ESHUTDOWN.
289  */
290 #define	FUNCTIONFS_ENDPOINT_DESC	_IOR('g', 130, \
291 					     struct usb_endpoint_descriptor)
292 
293 
294 
295 #endif /* _UAPI__LINUX_FUNCTIONFS_H__ */
296