functionfs.h 5.7 KB

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