| 1 | /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ |
| 2 | #ifndef __LINUX_FUNCTIONFS_H__ |
| 3 | #define __LINUX_FUNCTIONFS_H__ |
| 4 | |
| 5 | |
| 6 | #include <linux/const.h> |
| 7 | #include <linux/types.h> |
| 8 | #include <linux/ioctl.h> |
| 9 | |
| 10 | #include <linux/usb/ch9.h> |
| 11 | |
| 12 | |
| 13 | enum { |
| 14 | 	FUNCTIONFS_DESCRIPTORS_MAGIC = 1, |
| 15 | 	FUNCTIONFS_STRINGS_MAGIC = 2, |
| 16 | 	FUNCTIONFS_DESCRIPTORS_MAGIC_V2 = 3, |
| 17 | }; |
| 18 | |
| 19 | enum functionfs_flags { |
| 20 | 	FUNCTIONFS_HAS_FS_DESC = 1, |
| 21 | 	FUNCTIONFS_HAS_HS_DESC = 2, |
| 22 | 	FUNCTIONFS_HAS_SS_DESC = 4, |
| 23 | 	FUNCTIONFS_HAS_MS_OS_DESC = 8, |
| 24 | 	FUNCTIONFS_VIRTUAL_ADDR = 16, |
| 25 | 	FUNCTIONFS_EVENTFD = 32, |
| 26 | 	FUNCTIONFS_ALL_CTRL_RECIP = 64, |
| 27 | 	FUNCTIONFS_CONFIG0_SETUP = 128, |
| 28 | }; |
| 29 | |
| 30 | /* Descriptor of an non-audio endpoint */ |
| 31 | struct usb_endpoint_descriptor_no_audio { |
| 32 | 	__u8 bLength; |
| 33 | 	__u8 bDescriptorType; |
| 34 | |
| 35 | 	__u8 bEndpointAddress; |
| 36 | 	__u8 bmAttributes; |
| 37 | 	__le16 wMaxPacketSize; |
| 38 | 	__u8 bInterval; |
| 39 | } __attribute__((packed)); |
| 40 | |
| 41 | /** |
| 42 | * struct usb_dfu_functional_descriptor - DFU Functional descriptor |
| 43 | * @bLength:		Size of the descriptor (bytes) |
| 44 | * @bDescriptorType:	USB_DT_DFU_FUNCTIONAL |
| 45 | * @bmAttributes:	DFU attributes |
| 46 | * @wDetachTimeOut:	Maximum time to wait after DFU_DETACH (ms, le16) |
| 47 | * @wTransferSize:	Maximum number of bytes per control-write (le16) |
| 48 | * @bcdDFUVersion:	DFU Spec version (BCD, le16) |
| 49 | */ |
| 50 | struct usb_dfu_functional_descriptor { |
| 51 | 	__u8 bLength; |
| 52 | 	__u8 bDescriptorType; |
| 53 | 	__u8 bmAttributes; |
| 54 | 	__le16 wDetachTimeOut; |
| 55 | 	__le16 wTransferSize; |
| 56 | 	__le16 bcdDFUVersion; |
| 57 | } __attribute__ ((packed)); |
| 58 | |
| 59 | /* from DFU functional descriptor bmAttributes */ |
| 60 | #define DFU_FUNC_ATT_CAN_DOWNLOAD	_BITUL(0) |
| 61 | #define DFU_FUNC_ATT_CAN_UPLOAD		_BITUL(1) |
| 62 | #define DFU_FUNC_ATT_MANIFEST_TOLERANT	_BITUL(2) |
| 63 | #define DFU_FUNC_ATT_WILL_DETACH	_BITUL(3) |
| 64 | |
| 65 | |
| 66 | struct usb_functionfs_descs_head_v2 { |
| 67 | 	__le32 magic; |
| 68 | 	__le32 length; |
| 69 | 	__le32 flags; |
| 70 | 	/* |
| 71 | 	 * __le32 fs_count, hs_count, fs_count; must be included manually in |
| 72 | 	 * the structure taking flags into consideration. |
| 73 | 	 */ |
| 74 | } __attribute__((packed)); |
| 75 | |
| 76 | /* Legacy format, deprecated as of 3.14. */ |
| 77 | struct usb_functionfs_descs_head { |
| 78 | 	__le32 magic; |
| 79 | 	__le32 length; |
| 80 | 	__le32 fs_count; |
| 81 | 	__le32 hs_count; |
| 82 | } __attribute__((packed, deprecated)); |
| 83 | |
| 84 | /* MS OS Descriptor header */ |
| 85 | struct usb_os_desc_header { |
| 86 | 	__u8	interface; |
| 87 | 	__le32	dwLength; |
| 88 | 	__le16	bcdVersion; |
| 89 | 	__le16	wIndex; |
| 90 | 	union { |
| 91 | 		struct { |
| 92 | 			__u8	bCount; |
| 93 | 			__u8	Reserved; |
| 94 | 		}; |
| 95 | 		__le16	wCount; |
| 96 | 	}; |
| 97 | } __attribute__((packed)); |
| 98 | |
| 99 | struct usb_ext_compat_desc { |
| 100 | 	__u8	bFirstInterfaceNumber; |
| 101 | 	__u8	Reserved1; |
| 102 | 	__struct_group(/* no tag */, IDs, /* no attrs */, |
| 103 | 		__u8	CompatibleID[8]; |
| 104 | 		__u8	SubCompatibleID[8]; |
| 105 | 	); |
| 106 | 	__u8	Reserved2[6]; |
| 107 | }; |
| 108 | |
| 109 | struct usb_ext_prop_desc { |
| 110 | 	__le32	dwSize; |
| 111 | 	__le32	dwPropertyDataType; |
| 112 | 	__le16	wPropertyNameLength; |
| 113 | } __attribute__((packed)); |
| 114 | |
| 115 | /* Flags for usb_ffs_dmabuf_transfer_req->flags (none for now) */ |
| 116 | #define USB_FFS_DMABUF_TRANSFER_MASK	0x0 |
| 117 | |
| 118 | /** |
| 119 | * struct usb_ffs_dmabuf_transfer_req - Transfer request for a DMABUF object |
| 120 | * @fd:		file descriptor of the DMABUF object |
| 121 | * @flags:	one or more USB_FFS_DMABUF_TRANSFER_* flags |
| 122 | * @length:	number of bytes used in this DMABUF for the data transfer. |
| 123 | *		Should generally be set to the DMABUF's size. |
| 124 | */ |
| 125 | struct usb_ffs_dmabuf_transfer_req { |
| 126 | 	int fd; |
| 127 | 	__u32 flags; |
| 128 | 	__u64 length; |
| 129 | } __attribute__((packed)); |
| 130 | |
| 131 | |
| 132 | /** |
| 133 | * DOC: descriptors |
| 134 | * |
| 135 | * Descriptors format: |
| 136 | * |
| 137 | * +-----+-----------+--------------+--------------------------------------+ |
| 138 | * | off | name | type | description | |
| 139 | * +-----+-----------+--------------+--------------------------------------+ |
| 140 | * | 0 | magic | LE32 | FUNCTIONFS_DESCRIPTORS_MAGIC_V2 | |
| 141 | * +-----+-----------+--------------+--------------------------------------+ |
| 142 | * | 4 | length | LE32 | length of the whole data chunk | |
| 143 | * +-----+-----------+--------------+--------------------------------------+ |
| 144 | * | 8 | flags | LE32 | combination of functionfs_flags | |
| 145 | * +-----+-----------+--------------+--------------------------------------+ |
| 146 | * | | eventfd | LE32 | eventfd file descriptor | |
| 147 | * +-----+-----------+--------------+--------------------------------------+ |
| 148 | * | | fs_count | LE32 | number of full-speed descriptors | |
| 149 | * +-----+-----------+--------------+--------------------------------------+ |
| 150 | * | | hs_count | LE32 | number of high-speed descriptors | |
| 151 | * +-----+-----------+--------------+--------------------------------------+ |
| 152 | * | | ss_count | LE32 | number of super-speed descriptors | |
| 153 | * +-----+-----------+--------------+--------------------------------------+ |
| 154 | * | | os_count | LE32 | number of MS OS descriptors | |
| 155 | * +-----+-----------+--------------+--------------------------------------+ |
| 156 | * | | fs_descrs | Descriptor[] | list of full-speed descriptors | |
| 157 | * +-----+-----------+--------------+--------------------------------------+ |
| 158 | * | | hs_descrs | Descriptor[] | list of high-speed descriptors | |
| 159 | * +-----+-----------+--------------+--------------------------------------+ |
| 160 | * | | ss_descrs | Descriptor[] | list of super-speed descriptors | |
| 161 | * +-----+-----------+--------------+--------------------------------------+ |
| 162 | * | | os_descrs | OSDesc[] | list of MS OS descriptors | |
| 163 | * +-----+-----------+--------------+--------------------------------------+ |
| 164 | * |
| 165 | * Depending on which flags are set, various fields may be missing in the |
| 166 | * structure. Any flags that are not recognised cause the whole block to be |
| 167 | * rejected with -ENOSYS. |
| 168 | * |
| 169 | * Legacy descriptors format (deprecated as of 3.14): |
| 170 | * |
| 171 | * +-----+-----------+--------------+--------------------------------------+ |
| 172 | * | off | name | type | description | |
| 173 | * +-----+-----------+--------------+--------------------------------------+ |
| 174 | * | 0 | magic | LE32 | FUNCTIONFS_DESCRIPTORS_MAGIC | |
| 175 | * +-----+-----------+--------------+--------------------------------------+ |
| 176 | * | 4 | length | LE32 | length of the whole data chunk | |
| 177 | * +-----+-----------+--------------+--------------------------------------+ |
| 178 | * | 8 | fs_count | LE32 | number of full-speed descriptors | |
| 179 | * +-----+-----------+--------------+--------------------------------------+ |
| 180 | * | 12 | hs_count | LE32 | number of high-speed descriptors | |
| 181 | * +-----+-----------+--------------+--------------------------------------+ |
| 182 | * | 16 | fs_descrs | Descriptor[] | list of full-speed descriptors | |
| 183 | * +-----+-----------+--------------+--------------------------------------+ |
| 184 | * | | hs_descrs | Descriptor[] | list of high-speed descriptors | |
| 185 | * +-----+-----------+--------------+--------------------------------------+ |
| 186 | * |
| 187 | * All numbers must be in little endian order. |
| 188 | * |
| 189 | * Descriptor[] is an array of valid USB descriptors which have the following |
| 190 | * format: |
| 191 | * |
| 192 | * +-----+-----------------+------+--------------------------+ |
| 193 | * | off | name | type | description | |
| 194 | * +-----+-----------------+------+--------------------------+ |
| 195 | * | 0 | bLength | U8 | length of the descriptor | |
| 196 | * +-----+-----------------+------+--------------------------+ |
| 197 | * | 1 | bDescriptorType | U8 | descriptor type | |
| 198 | * +-----+-----------------+------+--------------------------+ |
| 199 | * | 2 | payload | | descriptor's payload | |
| 200 | * +-----+-----------------+------+--------------------------+ |
| 201 | * |
| 202 | * OSDesc[] is an array of valid MS OS Feature Descriptors which have one of |
| 203 | * the following formats: |
| 204 | * |
| 205 | * +-----+-----------------+------+--------------------------+ |
| 206 | * | off | name | type | description | |
| 207 | * +-----+-----------------+------+--------------------------+ |
| 208 | * | 0 | interface | U8 | related interface number | |
| 209 | * +-----+-----------------+------+--------------------------+ |
| 210 | * | 1 | dwLength | U32 | length of the descriptor | |
| 211 | * +-----+-----------------+------+--------------------------+ |
| 212 | * | 5 | bcdVersion | U16 | currently supported: 1 | |
| 213 | * +-----+-----------------+------+--------------------------+ |
| 214 | * | 7 | wIndex | U16 | currently supported: 4 | |
| 215 | * +-----+-----------------+------+--------------------------+ |
| 216 | * | 9 | bCount | U8 | number of ext. compat. | |
| 217 | * +-----+-----------------+------+--------------------------+ |
| 218 | * | 10 | Reserved | U8 | 0 | |
| 219 | * +-----+-----------------+------+--------------------------+ |
| 220 | * | 11 | ExtCompat[] | | list of ext. compat. d. | |
| 221 | * +-----+-----------------+------+--------------------------+ |
| 222 | * |
| 223 | * +-----+-----------------+------+--------------------------+ |
| 224 | * | off | name | type | description | |
| 225 | * +-----+-----------------+------+--------------------------+ |
| 226 | * | 0 | interface | U8 | related interface number | |
| 227 | * +-----+-----------------+------+--------------------------+ |
| 228 | * | 1 | dwLength | U32 | length of the descriptor | |
| 229 | * +-----+-----------------+------+--------------------------+ |
| 230 | * | 5 | bcdVersion | U16 | currently supported: 1 | |
| 231 | * +-----+-----------------+------+--------------------------+ |
| 232 | * | 7 | wIndex | U16 | currently supported: 5 | |
| 233 | * +-----+-----------------+------+--------------------------+ |
| 234 | * | 9 | wCount | U16 | number of ext. compat. | |
| 235 | * +-----+-----------------+------+--------------------------+ |
| 236 | * | 11 | ExtProp[] | | list of ext. prop. d. | |
| 237 | * +-----+-----------------+------+--------------------------+ |
| 238 | * |
| 239 | * ExtCompat[] is an array of valid Extended Compatibility descriptors |
| 240 | * which have the following format: |
| 241 | * |
| 242 | * +-----+-----------------------+------+-------------------------------------+ |
| 243 | * | off | name | type | description | |
| 244 | * +-----+-----------------------+------+-------------------------------------+ |
| 245 | * | 0 | bFirstInterfaceNumber | U8 | index of the interface or of the 1st| |
| 246 | * +-----+-----------------------+------+-------------------------------------+ |
| 247 | * | | | | interface in an IAD group | |
| 248 | * +-----+-----------------------+------+-------------------------------------+ |
| 249 | * | 1 | Reserved | U8 | 1 | |
| 250 | * +-----+-----------------------+------+-------------------------------------+ |
| 251 | * | 2 | CompatibleID | U8[8]| compatible ID string | |
| 252 | * +-----+-----------------------+------+-------------------------------------+ |
| 253 | * | 10 | SubCompatibleID | U8[8]| subcompatible ID string | |
| 254 | * +-----+-----------------------+------+-------------------------------------+ |
| 255 | * | 18 | Reserved | U8[6]| 0 | |
| 256 | * +-----+-----------------------+------+-------------------------------------+ |
| 257 | * |
| 258 | * ExtProp[] is an array of valid Extended Properties descriptors |
| 259 | * which have the following format: |
| 260 | * |
| 261 | * +-----+-----------------------+------+-------------------------------------+ |
| 262 | * | off | name | type | description | |
| 263 | * +-----+-----------------------+------+-------------------------------------+ |
| 264 | * | 0 | dwSize | U32 | length of the descriptor | |
| 265 | * +-----+-----------------------+------+-------------------------------------+ |
| 266 | * | 4 | dwPropertyDataType | U32 | 1..7 | |
| 267 | * +-----+-----------------------+------+-------------------------------------+ |
| 268 | * | 8 | wPropertyNameLength | U16 | bPropertyName length (NL) | |
| 269 | * +-----+-----------------------+------+-------------------------------------+ |
| 270 | * | 10 | bPropertyName |U8[NL]| name of this property | |
| 271 | * +-----+-----------------------+------+-------------------------------------+ |
| 272 | * |10+NL| dwPropertyDataLength | U32 | bPropertyData length (DL) | |
| 273 | * +-----+-----------------------+------+-------------------------------------+ |
| 274 | * |14+NL| bProperty |U8[DL]| payload of this property | |
| 275 | * +-----+-----------------------+------+-------------------------------------+ |
| 276 | */ |
| 277 | |
| 278 | struct usb_functionfs_strings_head { |
| 279 | 	__le32 magic; |
| 280 | 	__le32 length; |
| 281 | 	__le32 str_count; |
| 282 | 	__le32 lang_count; |
| 283 | } __attribute__((packed)); |
| 284 | |
| 285 | /* |
| 286 | * Strings format: |
| 287 | * |
| 288 | * | off | name | type | description | |
| 289 | * |-----+------------+-----------------------+----------------------------| |
| 290 | * | 0 | magic | LE32 | FUNCTIONFS_STRINGS_MAGIC | |
| 291 | * | 4 | length | LE32 | length of the data chunk | |
| 292 | * | 8 | str_count | LE32 | number of strings | |
| 293 | * | 12 | lang_count | LE32 | number of languages | |
| 294 | * | 16 | stringtab | StringTab[lang_count] | table of strings per lang | |
| 295 | * |
| 296 | * For each language there is one stringtab entry (ie. there are lang_count |
| 297 | * stringtab entries). Each StringTab has following format: |
| 298 | * |
| 299 | * | off | name | type | description | |
| 300 | * |-----+---------+-------------------+------------------------------------| |
| 301 | * | 0 | lang | LE16 | language code | |
| 302 | * | 2 | strings | String[str_count] | array of strings in given language | |
| 303 | * |
| 304 | * For each string there is one strings entry (ie. there are str_count |
| 305 | * string entries). Each String is a NUL terminated string encoded in |
| 306 | * UTF-8. |
| 307 | */ |
| 308 | |
| 309 | |
| 310 | |
| 311 | /* |
| 312 | * Events are delivered on the ep0 file descriptor, when the user mode driver |
| 313 | * reads from this file descriptor after writing the descriptors. Don't |
| 314 | * stop polling this descriptor. |
| 315 | */ |
| 316 | |
| 317 | enum usb_functionfs_event_type { |
| 318 | 	FUNCTIONFS_BIND, |
| 319 | 	FUNCTIONFS_UNBIND, |
| 320 | |
| 321 | 	FUNCTIONFS_ENABLE, |
| 322 | 	FUNCTIONFS_DISABLE, |
| 323 | |
| 324 | 	FUNCTIONFS_SETUP, |
| 325 | |
| 326 | 	FUNCTIONFS_SUSPEND, |
| 327 | 	FUNCTIONFS_RESUME |
| 328 | }; |
| 329 | |
| 330 | /* NOTE: this structure must stay the same size and layout on |
| 331 | * both 32-bit and 64-bit kernels. |
| 332 | */ |
| 333 | struct usb_functionfs_event { |
| 334 | 	union { |
| 335 | 		/* SETUP: packet; DATA phase i/o precedes next event |
| 336 | 		 *(setup.bmRequestType & USB_DIR_IN) flags direction */ |
| 337 | 		struct usb_ctrlrequest	setup; |
| 338 | 	} __attribute__((packed)) u; |
| 339 | |
| 340 | 	/* enum usb_functionfs_event_type */ |
| 341 | 	__u8				type; |
| 342 | 	__u8				_pad[3]; |
| 343 | } __attribute__((packed)); |
| 344 | |
| 345 | |
| 346 | /* Endpoint ioctls */ |
| 347 | /* The same as in gadgetfs */ |
| 348 | |
| 349 | /* IN transfers may be reported to the gadget driver as complete |
| 350 | *	when the fifo is loaded, before the host reads the data; |
| 351 | * OUT transfers may be reported to the host's "client" driver as |
| 352 | *	complete when they're sitting in the FIFO unread. |
| 353 | * THIS returns how many bytes are "unclaimed" in the endpoint fifo |
| 354 | * (needed for precise fault handling, when the hardware allows it) |
| 355 | */ |
| 356 | #define	FUNCTIONFS_FIFO_STATUS	_IO('g', 1) |
| 357 | |
| 358 | /* discards any unclaimed data in the fifo. */ |
| 359 | #define	FUNCTIONFS_FIFO_FLUSH	_IO('g', 2) |
| 360 | |
| 361 | /* resets endpoint halt+toggle; used to implement set_interface. |
| 362 | * some hardware (like pxa2xx) can't support this. |
| 363 | */ |
| 364 | #define	FUNCTIONFS_CLEAR_HALT	_IO('g', 3) |
| 365 | |
| 366 | /* Specific for functionfs */ |
| 367 | |
| 368 | /* |
| 369 | * Returns reverse mapping of an interface. Called on EP0. If there |
| 370 | * is no such interface returns -EDOM. If function is not active |
| 371 | * returns -ENODEV. |
| 372 | */ |
| 373 | #define	FUNCTIONFS_INTERFACE_REVMAP	_IO('g', 128) |
| 374 | |
| 375 | /* |
| 376 | * Returns real bEndpointAddress of an endpoint. If endpoint shuts down |
| 377 | * during the call, returns -ESHUTDOWN. |
| 378 | */ |
| 379 | #define	FUNCTIONFS_ENDPOINT_REVMAP	_IO('g', 129) |
| 380 | |
| 381 | /* |
| 382 | * Returns endpoint descriptor. If endpoint shuts down during the call, |
| 383 | * returns -ESHUTDOWN. |
| 384 | */ |
| 385 | #define	FUNCTIONFS_ENDPOINT_DESC	_IOR('g', 130, \ |
| 386 | 					 struct usb_endpoint_descriptor) |
| 387 | |
| 388 | /* |
| 389 | * Attach the DMABUF object, identified by its file descriptor, to the |
| 390 | * data endpoint. Returns zero on success, and a negative errno value |
| 391 | * on error. |
| 392 | */ |
| 393 | #define FUNCTIONFS_DMABUF_ATTACH	_IOW('g', 131, int) |
| 394 | |
| 395 | |
| 396 | /* |
| 397 | * Detach the given DMABUF object, identified by its file descriptor, |
| 398 | * from the data endpoint. Returns zero on success, and a negative |
| 399 | * errno value on error. Note that closing the endpoint's file |
| 400 | * descriptor will automatically detach all attached DMABUFs. |
| 401 | */ |
| 402 | #define FUNCTIONFS_DMABUF_DETACH	_IOW('g', 132, int) |
| 403 | |
| 404 | /* |
| 405 | * Enqueue the previously attached DMABUF to the transfer queue. |
| 406 | * The argument is a structure that packs the DMABUF's file descriptor, |
| 407 | * the size in bytes to transfer (which should generally correspond to |
| 408 | * the size of the DMABUF), and a 'flags' field which is unused |
| 409 | * for now. Returns zero on success, and a negative errno value on |
| 410 | * error. |
| 411 | */ |
| 412 | #define FUNCTIONFS_DMABUF_TRANSFER	_IOW('g', 133, \ |
| 413 | 					 struct usb_ffs_dmabuf_transfer_req) |
| 414 | |
| 415 | #endif /* __LINUX_FUNCTIONFS_H__ */ |