functionfs.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #ifndef __LINUX_FUNCTIONFS_H__
  2. #define __LINUX_FUNCTIONFS_H__ 1
  3. #include <linux/types.h>
  4. #include <linux/ioctl.h>
  5. #include <linux/usb/ch9.h>
  6. enum {
  7. FUNCTIONFS_DESCRIPTORS_MAGIC = 1,
  8. FUNCTIONFS_STRINGS_MAGIC = 2
  9. };
  10. #define FUNCTIONFS_SS_DESC_MAGIC 0x0055DE5C
  11. #ifndef __KERNEL__
  12. /* Descriptor of an non-audio endpoint */
  13. struct usb_endpoint_descriptor_no_audio {
  14. __u8 bLength;
  15. __u8 bDescriptorType;
  16. __u8 bEndpointAddress;
  17. __u8 bmAttributes;
  18. __le16 wMaxPacketSize;
  19. __u8 bInterval;
  20. } __attribute__((packed));
  21. /*
  22. * All numbers must be in little endian order.
  23. */
  24. struct usb_functionfs_descs_head {
  25. __le32 magic;
  26. __le32 length;
  27. __le32 fs_count;
  28. __le32 hs_count;
  29. } __attribute__((packed));
  30. /*
  31. * Descriptors format:
  32. *
  33. * | off | name | type | description |
  34. * |-----+-----------+--------------+--------------------------------------|
  35. * | 0 | magic | LE32 | FUNCTIONFS_{FS,HS}_DESCRIPTORS_MAGIC |
  36. * | 4 | length | LE32 | length of the whole data chunk |
  37. * | 8 | fs_count | LE32 | number of full-speed descriptors |
  38. * | 12 | hs_count | LE32 | number of high-speed descriptors |
  39. * | 16 | fs_descrs | Descriptor[] | list of full-speed descriptors |
  40. * | | hs_descrs | Descriptor[] | list of high-speed descriptors |
  41. * | | ss_magic | LE32 | FUNCTIONFS_SS_DESC_MAGIC |
  42. * | | ss_count | LE32 | number of super-speed descriptors |
  43. * | | ss_descrs | Descriptor[] | list of super-speed descriptors |
  44. *
  45. * ss_magic: if present then it implies that SS_DESCs are also present
  46. * descs are just valid USB descriptors and have the following format:
  47. *
  48. * | off | name | type | description |
  49. * |-----+-----------------+------+--------------------------|
  50. * | 0 | bLength | U8 | length of the descriptor |
  51. * | 1 | bDescriptorType | U8 | descriptor type |
  52. * | 2 | payload | | descriptor's payload |
  53. */
  54. struct usb_functionfs_strings_head {
  55. __le32 magic;
  56. __le32 length;
  57. __le32 str_count;
  58. __le32 lang_count;
  59. } __attribute__((packed));
  60. /*
  61. * Strings format:
  62. *
  63. * | off | name | type | description |
  64. * |-----+------------+-----------------------+----------------------------|
  65. * | 0 | magic | LE32 | FUNCTIONFS_STRINGS_MAGIC |
  66. * | 4 | length | LE32 | length of the data chunk |
  67. * | 8 | str_count | LE32 | number of strings |
  68. * | 12 | lang_count | LE32 | number of languages |
  69. * | 16 | stringtab | StringTab[lang_count] | table of strings per lang |
  70. *
  71. * For each language there is one stringtab entry (ie. there are lang_count
  72. * stringtab entires). Each StringTab has following format:
  73. *
  74. * | off | name | type | description |
  75. * |-----+---------+-------------------+------------------------------------|
  76. * | 0 | lang | LE16 | language code |
  77. * | 2 | strings | String[str_count] | array of strings in given language |
  78. *
  79. * For each string there is one strings entry (ie. there are str_count
  80. * string entries). Each String is a NUL terminated string encoded in
  81. * UTF-8.
  82. */
  83. #endif
  84. /*
  85. * Events are delivered on the ep0 file descriptor, when the user mode driver
  86. * reads from this file descriptor after writing the descriptors. Don't
  87. * stop polling this descriptor.
  88. */
  89. enum usb_functionfs_event_type {
  90. FUNCTIONFS_BIND,
  91. FUNCTIONFS_UNBIND,
  92. FUNCTIONFS_ENABLE,
  93. FUNCTIONFS_DISABLE,
  94. FUNCTIONFS_SETUP,
  95. FUNCTIONFS_SUSPEND,
  96. FUNCTIONFS_RESUME
  97. };
  98. /* NOTE: this structure must stay the same size and layout on
  99. * both 32-bit and 64-bit kernels.
  100. */
  101. struct usb_functionfs_event {
  102. union {
  103. /* SETUP: packet; DATA phase i/o precedes next event
  104. *(setup.bmRequestType & USB_DIR_IN) flags direction */
  105. struct usb_ctrlrequest setup;
  106. } __attribute__((packed)) u;
  107. /* enum usb_functionfs_event_type */
  108. __u8 type;
  109. __u8 _pad[3];
  110. } __attribute__((packed));
  111. /* Endpoint ioctls */
  112. /* The same as in gadgetfs */
  113. /* IN transfers may be reported to the gadget driver as complete
  114. * when the fifo is loaded, before the host reads the data;
  115. * OUT transfers may be reported to the host's "client" driver as
  116. * complete when they're sitting in the FIFO unread.
  117. * THIS returns how many bytes are "unclaimed" in the endpoint fifo
  118. * (needed for precise fault handling, when the hardware allows it)
  119. */
  120. #define FUNCTIONFS_FIFO_STATUS _IO('g', 1)
  121. /* discards any unclaimed data in the fifo. */
  122. #define FUNCTIONFS_FIFO_FLUSH _IO('g', 2)
  123. /* resets endpoint halt+toggle; used to implement set_interface.
  124. * some hardware (like pxa2xx) can't support this.
  125. */
  126. #define FUNCTIONFS_CLEAR_HALT _IO('g', 3)
  127. /* Specific for functionfs */
  128. /*
  129. * Returns reverse mapping of an interface. Called on EP0. If there
  130. * is no such interface returns -EDOM. If function is not active
  131. * returns -ENODEV.
  132. */
  133. #define FUNCTIONFS_INTERFACE_REVMAP _IO('g', 128)
  134. /*
  135. * Returns real bEndpointAddress of an endpoint. If function is not
  136. * active returns -ENODEV.
  137. */
  138. #define FUNCTIONFS_ENDPOINT_REVMAP _IO('g', 129)
  139. /*
  140. * Returns endpoint descriptor. If function is not active returns -ENODEV.
  141. */
  142. #define FUNCTIONFS_ENDPOINT_DESC _IOR('g', 130, \
  143. struct usb_endpoint_descriptor)
  144. #ifdef __KERNEL__
  145. struct ffs_data;
  146. struct usb_composite_dev;
  147. struct usb_configuration;
  148. static int functionfs_init(void) __attribute__((warn_unused_result));
  149. static void functionfs_cleanup(void);
  150. static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
  151. __attribute__((warn_unused_result, nonnull));
  152. static void functionfs_unbind(struct ffs_data *ffs)
  153. __attribute__((nonnull));
  154. static int functionfs_bind_config(struct usb_composite_dev *cdev,
  155. struct usb_configuration *c,
  156. struct ffs_data *ffs)
  157. __attribute__((warn_unused_result, nonnull));
  158. static int functionfs_ready_callback(struct ffs_data *ffs)
  159. __attribute__((warn_unused_result, nonnull));
  160. static void functionfs_closed_callback(struct ffs_data *ffs)
  161. __attribute__((nonnull));
  162. static void *functionfs_acquire_dev_callback(const char *dev_name)
  163. __attribute__((warn_unused_result, nonnull));
  164. static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
  165. __attribute__((nonnull));
  166. #endif
  167. #endif