functionfs.txt 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. *How FunctionFS works*
  2. From kernel point of view it is just a composite function with some
  3. unique behaviour. It may be added to an USB configuration only after
  4. the user space driver has registered by writing descriptors and
  5. strings (the user space program has to provide the same information
  6. that kernel level composite functions provide when they are added to
  7. the configuration).
  8. This in particular means that the composite initialisation functions
  9. may not be in init section (ie. may not use the __init tag).
  10. From user space point of view it is a file system which when
  11. mounted provides an "ep0" file. User space driver need to
  12. write descriptors and strings to that file. It does not need
  13. to worry about endpoints, interfaces or strings numbers but
  14. simply provide descriptors such as if the function was the
  15. only one (endpoints and strings numbers starting from one and
  16. interface numbers starting from zero). The FunctionFS changes
  17. them as needed also handling situation when numbers differ in
  18. different configurations.
  19. When descriptors and strings are written "ep#" files appear
  20. (one for each declared endpoint) which handle communication on
  21. a single endpoint. Again, FunctionFS takes care of the real
  22. numbers and changing of the configuration (which means that
  23. "ep1" file may be really mapped to (say) endpoint 3 (and when
  24. configuration changes to (say) endpoint 2)). "ep0" is used
  25. for receiving events and handling setup requests.
  26. When all files are closed the function disables itself.
  27. What I also want to mention is that the FunctionFS is designed in such
  28. a way that it is possible to mount it several times so in the end
  29. a gadget could use several FunctionFS functions. The idea is that
  30. each FunctionFS instance is identified by the device name used
  31. when mounting.
  32. One can imagine a gadget that has an Ethernet, MTP and HID interfaces
  33. where the last two are implemented via FunctionFS. On user space
  34. level it would look like this:
  35. $ insmod g_ffs.ko idVendor=<ID> iSerialNumber=<string> functions=mtp,hid
  36. $ mkdir /dev/ffs-mtp && mount -t functionfs mtp /dev/ffs-mtp
  37. $ ( cd /dev/ffs-mtp && mtp-daemon ) &
  38. $ mkdir /dev/ffs-hid && mount -t functionfs hid /dev/ffs-hid
  39. $ ( cd /dev/ffs-hid && hid-daemon ) &
  40. On kernel level the gadget checks ffs_data->dev_name to identify
  41. whether it's FunctionFS designed for MTP ("mtp") or HID ("hid").
  42. If no "functions" module parameters is supplied, the driver accepts
  43. just one function with any name.
  44. When "functions" module parameter is supplied, only functions
  45. with listed names are accepted. In particular, if the "functions"
  46. parameter's value is just a one-element list, then the behaviour
  47. is similar to when there is no "functions" at all; however,
  48. only a function with the specified name is accepted.
  49. The gadget is registered only after all the declared function
  50. filesystems have been mounted and USB descriptors of all functions
  51. have been written to their ep0's.
  52. Conversely, the gadget is unregistered after the first USB function
  53. closes its endpoints.