gadget_configfs.txt 10 KB

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