gadget_configfs.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. ============================================
  2. Linux USB gadget configured through configfs
  3. ============================================
  4. 25th April 2013
  5. Overview
  6. ========
  7. A USB Linux Gadget is a device which has a UDC (USB Device Controller) and can
  8. be connected to a USB Host to extend it with additional functions like a serial
  9. port or a mass storage capability.
  10. A gadget is seen by its host as a set of configurations, each of which contains
  11. a number of interfaces which, from the gadget's perspective, are known as
  12. functions, each function representing e.g. a serial connection or a SCSI disk.
  13. Linux provides a number of functions for gadgets to use.
  14. Creating a gadget means deciding what configurations there will be
  15. and which functions each configuration will provide.
  16. Configfs (please see `Documentation/filesystems/configfs.rst`) lends itself nicely
  17. for the purpose of telling the kernel about the above mentioned decision.
  18. This document is about how to do it.
  19. It also describes how configfs integration into gadget is designed.
  20. Requirements
  21. ============
  22. In order for this to work configfs must be available, so CONFIGFS_FS must be
  23. 'y' or 'm' in .config. As of this writing USB_LIBCOMPOSITE selects CONFIGFS_FS.
  24. Usage
  25. =====
  26. (The original post describing the first function
  27. made available through configfs can be seen here:
  28. http://www.spinics.net/lists/linux-usb/msg76388.html)
  29. ::
  30. $ modprobe libcomposite
  31. $ mount none $CONFIGFS_HOME -t configfs
  32. where CONFIGFS_HOME is the mount point for configfs
  33. 1. Creating the gadgets
  34. -----------------------
  35. For each gadget to be created its corresponding directory must be created::
  36. $ mkdir $CONFIGFS_HOME/usb_gadget/<gadget name>
  37. e.g.::
  38. $ mkdir $CONFIGFS_HOME/usb_gadget/g1
  39. ...
  40. ...
  41. ...
  42. $ cd $CONFIGFS_HOME/usb_gadget/g1
  43. Each gadget needs to have its vendor id <VID> and product id <PID> specified::
  44. $ echo <VID> > idVendor
  45. $ echo <PID> > idProduct
  46. A gadget also needs its serial number, manufacturer and product strings.
  47. In order to have a place to store them, a strings subdirectory must be created
  48. for each language, e.g.::
  49. $ mkdir strings/0x409
  50. Then the strings can be specified::
  51. $ echo <serial number> > strings/0x409/serialnumber
  52. $ echo <manufacturer> > strings/0x409/manufacturer
  53. $ echo <product> > strings/0x409/product
  54. Further custom string descriptors can be created as directories within the
  55. language's directory, with the string text being written to the "s" attribute
  56. within the string's directory:
  57. $ mkdir strings/0x409/xu.0
  58. $ echo <string text> > strings/0x409/xu.0/s
  59. Where function drivers support it, functions may allow symlinks to these custom
  60. string descriptors to associate those strings with class descriptors.
  61. 2. Creating the configurations
  62. ------------------------------
  63. Each gadget will consist of a number of configurations, their corresponding
  64. directories must be created:
  65. $ mkdir configs/<name>.<number>
  66. where <name> can be any string which is legal in a filesystem and the
  67. <number> is the configuration's number, e.g.::
  68. $ mkdir configs/c.1
  69. ...
  70. ...
  71. ...
  72. Each configuration also needs its strings, so a subdirectory must be created
  73. for each language, e.g.::
  74. $ mkdir configs/c.1/strings/0x409
  75. Then the configuration string can be specified::
  76. $ echo <configuration> > configs/c.1/strings/0x409/configuration
  77. Some attributes can also be set for a configuration, e.g.::
  78. $ echo 120 > configs/c.1/MaxPower
  79. 3. Creating the functions
  80. -------------------------
  81. The gadget will provide some functions, for each function its corresponding
  82. directory must be created::
  83. $ mkdir functions/<name>.<instance name>
  84. where <name> corresponds to one of allowed function names and instance name
  85. is an arbitrary string allowed in a filesystem, e.g.::
  86. $ mkdir functions/ncm.usb0 # usb_f_ncm.ko gets loaded with request_module()
  87. ...
  88. ...
  89. ...
  90. Each function provides its specific set of attributes, with either read-only
  91. or read-write access. Where applicable they need to be written to as
  92. appropriate.
  93. Please refer to Documentation/ABI/testing/configfs-usb-gadget for more information.
  94. 4. Associating the functions with their configurations
  95. ------------------------------------------------------
  96. At this moment a number of gadgets is created, each of which has a number of
  97. configurations specified and a number of functions available. What remains
  98. is specifying which function is available in which configuration (the same
  99. function can be used in multiple configurations). This is achieved with
  100. creating symbolic links::
  101. $ ln -s functions/<name>.<instance name> configs/<name>.<number>
  102. e.g.::
  103. $ ln -s functions/ncm.usb0 configs/c.1
  104. ...
  105. ...
  106. ...
  107. 5. Enabling the gadget
  108. ----------------------
  109. All the above steps serve the purpose of composing the gadget of
  110. configurations and functions.
  111. An example directory structure might look like this::
  112. .
  113. ./strings
  114. ./strings/0x409
  115. ./strings/0x409/serialnumber
  116. ./strings/0x409/product
  117. ./strings/0x409/manufacturer
  118. ./configs
  119. ./configs/c.1
  120. ./configs/c.1/ncm.usb0 -> ../../../../usb_gadget/g1/functions/ncm.usb0
  121. ./configs/c.1/strings
  122. ./configs/c.1/strings/0x409
  123. ./configs/c.1/strings/0x409/configuration
  124. ./configs/c.1/bmAttributes
  125. ./configs/c.1/MaxPower
  126. ./functions
  127. ./functions/ncm.usb0
  128. ./functions/ncm.usb0/ifname
  129. ./functions/ncm.usb0/qmult
  130. ./functions/ncm.usb0/host_addr
  131. ./functions/ncm.usb0/dev_addr
  132. ./UDC
  133. ./bcdUSB
  134. ./bcdDevice
  135. ./idProduct
  136. ./idVendor
  137. ./bMaxPacketSize0
  138. ./bDeviceProtocol
  139. ./bDeviceSubClass
  140. ./bDeviceClass
  141. Such a gadget must be finally enabled so that the USB host can enumerate it.
  142. In order to enable the gadget it must be bound to a UDC (USB Device
  143. Controller)::
  144. $ echo <udc name> > UDC
  145. where <udc name> is one of those found in /sys/class/udc/*
  146. e.g.::
  147. $ echo s3c-hsotg > UDC
  148. 6. Disabling the gadget
  149. -----------------------
  150. ::
  151. $ echo "" > UDC
  152. 7. Cleaning up
  153. --------------
  154. Remove functions from configurations::
  155. $ rm configs/<config name>.<number>/<function>
  156. where <config name>.<number> specify the configuration and <function> is
  157. a symlink to a function being removed from the configuration, e.g.::
  158. $ rm configs/c.1/ncm.usb0
  159. ...
  160. ...
  161. ...
  162. Remove strings directories in configurations:
  163. $ rmdir configs/<config name>.<number>/strings/<lang>
  164. e.g.::
  165. $ rmdir configs/c.1/strings/0x409
  166. ...
  167. ...
  168. ...
  169. and remove the configurations::
  170. $ rmdir configs/<config name>.<number>
  171. e.g.::
  172. rmdir configs/c.1
  173. ...
  174. ...
  175. ...
  176. Remove functions (function modules are not unloaded, though):
  177. $ rmdir functions/<name>.<instance name>
  178. e.g.::
  179. $ rmdir functions/ncm.usb0
  180. ...
  181. ...
  182. ...
  183. Remove strings directories in the gadget::
  184. $ rmdir strings/<lang>
  185. e.g.::
  186. $ rmdir strings/0x409
  187. and finally remove the gadget::
  188. $ cd ..
  189. $ rmdir <gadget name>
  190. e.g.::
  191. $ rmdir g1
  192. Implementation design
  193. =====================
  194. Below the idea of how configfs works is presented.
  195. In configfs there are items and groups, both represented as directories.
  196. The difference between an item and a group is that a group can contain
  197. other groups. In the picture below only an item is shown.
  198. Both items and groups can have attributes, which are represented as files.
  199. The user can create and remove directories, but cannot remove files,
  200. which can be read-only or read-write, depending on what they represent.
  201. The filesystem part of configfs operates on config_items/groups and
  202. configfs_attributes which are generic and of the same type for all
  203. configured elements. However, they are embedded in usage-specific
  204. larger structures. In the picture below there is a "cs" which contains
  205. a config_item and an "sa" which contains a configfs_attribute.
  206. The filesystem view would be like this::
  207. ./
  208. ./cs (directory)
  209. |
  210. +--sa (file)
  211. |
  212. .
  213. .
  214. .
  215. Whenever a user reads/writes the "sa" file, a function is called
  216. which accepts a struct config_item and a struct configfs_attribute.
  217. In the said function the "cs" and "sa" are retrieved using the well
  218. known container_of technique and an appropriate sa's function (show or
  219. store) is called and passed the "cs" and a character buffer. The "show"
  220. is for displaying the file's contents (copy data from the cs to the
  221. buffer), while the "store" is for modifying the file's contents (copy data
  222. from the buffer to the cs), but it is up to the implementer of the
  223. two functions to decide what they actually do.
  224. ::
  225. typedef struct configured_structure cs;
  226. typedef struct specific_attribute sa;
  227. sa
  228. +----------------------------------+
  229. cs | (*show)(cs *, buffer); |
  230. +-----------------+ | (*store)(cs *, buffer, length); |
  231. | | | |
  232. | +-------------+ | | +------------------+ |
  233. | | struct |-|----|------>|struct | |
  234. | | config_item | | | |configfs_attribute| |
  235. | +-------------+ | | +------------------+ |
  236. | | +----------------------------------+
  237. | data to be set | .
  238. | | .
  239. +-----------------+ .
  240. The file names are decided by the config item/group designer, while
  241. the directories in general can be named at will. A group can have
  242. a number of its default sub-groups created automatically.
  243. For more information on configfs please see
  244. `Documentation/filesystems/configfs.rst`.
  245. The concepts described above translate to USB gadgets like this:
  246. 1. A gadget has its config group, which has some attributes (idVendor,
  247. idProduct etc) and default sub-groups (configs, functions, strings).
  248. Writing to the attributes causes the information to be stored in
  249. appropriate locations. In the configs, functions and strings sub-groups
  250. a user can create their sub-groups to represent configurations, functions,
  251. and groups of strings in a given language.
  252. 2. The user creates configurations and functions, in the configurations
  253. creates symbolic links to functions. This information is used when the
  254. gadget's UDC attribute is written to, which means binding the gadget
  255. to the UDC. The code in drivers/usb/gadget/configfs.c iterates over
  256. all configurations, and in each configuration it iterates over all
  257. functions and binds them. This way the whole gadget is bound.
  258. 3. The file drivers/usb/gadget/configfs.c contains code for
  259. - gadget's config_group
  260. - gadget's default groups (configs, functions, strings)
  261. - associating functions with configurations (symlinks)
  262. 4. Each USB function naturally has its own view of what it wants
  263. configured, so config_groups for particular functions are defined
  264. in the functions implementation files drivers/usb/gadget/f_*.c.
  265. 5. Function's code is written in such a way that it uses
  266. usb_get_function_instance(), which, in turn, calls request_module.
  267. So, provided that modprobe works, modules for particular functions
  268. are loaded automatically. Please note that the converse is not true:
  269. after a gadget is disabled and torn down, the modules remain loaded.