functionfs.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ====================
  2. How FunctionFS works
  3. ====================
  4. Overview
  5. ========
  6. From kernel point of view it is just a composite function with some
  7. unique behaviour. It may be added to an USB configuration only after
  8. the user space driver has registered by writing descriptors and
  9. strings (the user space program has to provide the same information
  10. that kernel level composite functions provide when they are added to
  11. the configuration).
  12. This in particular means that the composite initialisation functions
  13. may not be in init section (ie. may not use the __init tag).
  14. From user space point of view it is a file system which when
  15. mounted provides an "ep0" file. User space driver need to
  16. write descriptors and strings to that file. It does not need
  17. to worry about endpoints, interfaces or strings numbers but
  18. simply provide descriptors such as if the function was the
  19. only one (endpoints and strings numbers starting from one and
  20. interface numbers starting from zero). The FunctionFS changes
  21. them as needed also handling situation when numbers differ in
  22. different configurations.
  23. For more information about FunctionFS descriptors see :doc:`functionfs-desc`
  24. When descriptors and strings are written "ep#" files appear
  25. (one for each declared endpoint) which handle communication on
  26. a single endpoint. Again, FunctionFS takes care of the real
  27. numbers and changing of the configuration (which means that
  28. "ep1" file may be really mapped to (say) endpoint 3 (and when
  29. configuration changes to (say) endpoint 2)). "ep0" is used
  30. for receiving events and handling setup requests.
  31. When all files are closed the function disables itself.
  32. What I also want to mention is that the FunctionFS is designed in such
  33. a way that it is possible to mount it several times so in the end
  34. a gadget could use several FunctionFS functions. The idea is that
  35. each FunctionFS instance is identified by the device name used
  36. when mounting.
  37. One can imagine a gadget that has an Ethernet, MTP and HID interfaces
  38. where the last two are implemented via FunctionFS. On user space
  39. level it would look like this::
  40. $ insmod g_ffs.ko idVendor=<ID> iSerialNumber=<string> functions=mtp,hid
  41. $ mkdir /dev/ffs-mtp && mount -t functionfs mtp /dev/ffs-mtp
  42. $ ( cd /dev/ffs-mtp && mtp-daemon ) &
  43. $ mkdir /dev/ffs-hid && mount -t functionfs hid /dev/ffs-hid
  44. $ ( cd /dev/ffs-hid && hid-daemon ) &
  45. On kernel level the gadget checks ffs_data->dev_name to identify
  46. whether its FunctionFS is designed for MTP ("mtp") or HID ("hid").
  47. If no "functions" module parameters is supplied, the driver accepts
  48. just one function with any name.
  49. When "functions" module parameter is supplied, only functions
  50. with listed names are accepted. In particular, if the "functions"
  51. parameter's value is just a one-element list, then the behaviour
  52. is similar to when there is no "functions" at all; however,
  53. only a function with the specified name is accepted.
  54. The gadget is registered only after all the declared function
  55. filesystems have been mounted and USB descriptors of all functions
  56. have been written to their ep0's.
  57. Conversely, the gadget is unregistered after the first USB function
  58. closes its endpoints.
  59. DMABUF interface
  60. ================
  61. FunctionFS additionally supports a DMABUF based interface, where the
  62. userspace can attach DMABUF objects (externally created) to an endpoint,
  63. and subsequently use them for data transfers.
  64. A userspace application can then use this interface to share DMABUF
  65. objects between several interfaces, allowing it to transfer data in a
  66. zero-copy fashion, for instance between IIO and the USB stack.
  67. As part of this interface, three new IOCTLs have been added. These three
  68. IOCTLs have to be performed on a data endpoint (ie. not ep0). They are:
  69. ``FUNCTIONFS_DMABUF_ATTACH(int)``
  70. Attach the DMABUF object, identified by its file descriptor, to the
  71. data endpoint. Returns zero on success, and a negative errno value
  72. on error.
  73. ``FUNCTIONFS_DMABUF_DETACH(int)``
  74. Detach the given DMABUF object, identified by its file descriptor,
  75. from the data endpoint. Returns zero on success, and a negative
  76. errno value on error. Note that closing the endpoint's file
  77. descriptor will automatically detach all attached DMABUFs.
  78. ``FUNCTIONFS_DMABUF_TRANSFER(struct usb_ffs_dmabuf_transfer_req *)``
  79. Enqueue the previously attached DMABUF to the transfer queue.
  80. The argument is a structure that packs the DMABUF's file descriptor,
  81. the size in bytes to transfer (which should generally correspond to
  82. the size of the DMABUF), and a 'flags' field which is unused
  83. for now. Returns zero on success, and a negative errno value on
  84. error.