0001 ============================================
0002 Linux USB gadget configured through configfs
0003 ============================================
0004
0005
0006 25th April 2013
0007
0008
0009
0010
0011 Overview
0012 ========
0013
0014 A USB Linux Gadget is a device which has a UDC (USB Device Controller) and can
0015 be connected to a USB Host to extend it with additional functions like a serial
0016 port or a mass storage capability.
0017
0018 A gadget is seen by its host as a set of configurations, each of which contains
0019 a number of interfaces which, from the gadget's perspective, are known as
0020 functions, each function representing e.g. a serial connection or a SCSI disk.
0021
0022 Linux provides a number of functions for gadgets to use.
0023
0024 Creating a gadget means deciding what configurations there will be
0025 and which functions each configuration will provide.
0026
0027 Configfs (please see `Documentation/filesystems/configfs.rst`) lends itself nicely
0028 for the purpose of telling the kernel about the above mentioned decision.
0029 This document is about how to do it.
0030
0031 It also describes how configfs integration into gadget is designed.
0032
0033
0034
0035
0036 Requirements
0037 ============
0038
0039 In order for this to work configfs must be available, so CONFIGFS_FS must be
0040 'y' or 'm' in .config. As of this writing USB_LIBCOMPOSITE selects CONFIGFS_FS.
0041
0042
0043
0044
0045 Usage
0046 =====
0047
0048 (The original post describing the first function
0049 made available through configfs can be seen here:
0050 http://www.spinics.net/lists/linux-usb/msg76388.html)
0051
0052 ::
0053
0054 $ modprobe libcomposite
0055 $ mount none $CONFIGFS_HOME -t configfs
0056
0057 where CONFIGFS_HOME is the mount point for configfs
0058
0059 1. Creating the gadgets
0060 -----------------------
0061
0062 For each gadget to be created its corresponding directory must be created::
0063
0064 $ mkdir $CONFIGFS_HOME/usb_gadget/<gadget name>
0065
0066 e.g.::
0067
0068 $ mkdir $CONFIGFS_HOME/usb_gadget/g1
0069
0070 ...
0071 ...
0072 ...
0073
0074 $ cd $CONFIGFS_HOME/usb_gadget/g1
0075
0076 Each gadget needs to have its vendor id <VID> and product id <PID> specified::
0077
0078 $ echo <VID> > idVendor
0079 $ echo <PID> > idProduct
0080
0081 A gadget also needs its serial number, manufacturer and product strings.
0082 In order to have a place to store them, a strings subdirectory must be created
0083 for each language, e.g.::
0084
0085 $ mkdir strings/0x409
0086
0087 Then the strings can be specified::
0088
0089 $ echo <serial number> > strings/0x409/serialnumber
0090 $ echo <manufacturer> > strings/0x409/manufacturer
0091 $ echo <product> > strings/0x409/product
0092
0093 2. Creating the configurations
0094 ------------------------------
0095
0096 Each gadget will consist of a number of configurations, their corresponding
0097 directories must be created:
0098
0099 $ mkdir configs/<name>.<number>
0100
0101 where <name> can be any string which is legal in a filesystem and the
0102 <number> is the configuration's number, e.g.::
0103
0104 $ mkdir configs/c.1
0105
0106 ...
0107 ...
0108 ...
0109
0110 Each configuration also needs its strings, so a subdirectory must be created
0111 for each language, e.g.::
0112
0113 $ mkdir configs/c.1/strings/0x409
0114
0115 Then the configuration string can be specified::
0116
0117 $ echo <configuration> > configs/c.1/strings/0x409/configuration
0118
0119 Some attributes can also be set for a configuration, e.g.::
0120
0121 $ echo 120 > configs/c.1/MaxPower
0122
0123 3. Creating the functions
0124 -------------------------
0125
0126 The gadget will provide some functions, for each function its corresponding
0127 directory must be created::
0128
0129 $ mkdir functions/<name>.<instance name>
0130
0131 where <name> corresponds to one of allowed function names and instance name
0132 is an arbitrary string allowed in a filesystem, e.g.::
0133
0134 $ mkdir functions/ncm.usb0 # usb_f_ncm.ko gets loaded with request_module()
0135
0136 ...
0137 ...
0138 ...
0139
0140 Each function provides its specific set of attributes, with either read-only
0141 or read-write access. Where applicable they need to be written to as
0142 appropriate.
0143 Please refer to Documentation/ABI/testing/configfs-usb-gadget for more information.
0144
0145 4. Associating the functions with their configurations
0146 ------------------------------------------------------
0147
0148 At this moment a number of gadgets is created, each of which has a number of
0149 configurations specified and a number of functions available. What remains
0150 is specifying which function is available in which configuration (the same
0151 function can be used in multiple configurations). This is achieved with
0152 creating symbolic links::
0153
0154 $ ln -s functions/<name>.<instance name> configs/<name>.<number>
0155
0156 e.g.::
0157
0158 $ ln -s functions/ncm.usb0 configs/c.1
0159
0160 ...
0161 ...
0162 ...
0163
0164 5. Enabling the gadget
0165 ----------------------
0166
0167 All the above steps serve the purpose of composing the gadget of
0168 configurations and functions.
0169
0170 An example directory structure might look like this::
0171
0172 .
0173 ./strings
0174 ./strings/0x409
0175 ./strings/0x409/serialnumber
0176 ./strings/0x409/product
0177 ./strings/0x409/manufacturer
0178 ./configs
0179 ./configs/c.1
0180 ./configs/c.1/ncm.usb0 -> ../../../../usb_gadget/g1/functions/ncm.usb0
0181 ./configs/c.1/strings
0182 ./configs/c.1/strings/0x409
0183 ./configs/c.1/strings/0x409/configuration
0184 ./configs/c.1/bmAttributes
0185 ./configs/c.1/MaxPower
0186 ./functions
0187 ./functions/ncm.usb0
0188 ./functions/ncm.usb0/ifname
0189 ./functions/ncm.usb0/qmult
0190 ./functions/ncm.usb0/host_addr
0191 ./functions/ncm.usb0/dev_addr
0192 ./UDC
0193 ./bcdUSB
0194 ./bcdDevice
0195 ./idProduct
0196 ./idVendor
0197 ./bMaxPacketSize0
0198 ./bDeviceProtocol
0199 ./bDeviceSubClass
0200 ./bDeviceClass
0201
0202
0203 Such a gadget must be finally enabled so that the USB host can enumerate it.
0204
0205 In order to enable the gadget it must be bound to a UDC (USB Device
0206 Controller)::
0207
0208 $ echo <udc name> > UDC
0209
0210 where <udc name> is one of those found in /sys/class/udc/*
0211 e.g.::
0212
0213 $ echo s3c-hsotg > UDC
0214
0215
0216 6. Disabling the gadget
0217 -----------------------
0218
0219 ::
0220
0221 $ echo "" > UDC
0222
0223 7. Cleaning up
0224 --------------
0225
0226 Remove functions from configurations::
0227
0228 $ rm configs/<config name>.<number>/<function>
0229
0230 where <config name>.<number> specify the configuration and <function> is
0231 a symlink to a function being removed from the configuration, e.g.::
0232
0233 $ rm configs/c.1/ncm.usb0
0234
0235 ...
0236 ...
0237 ...
0238
0239 Remove strings directories in configurations:
0240
0241 $ rmdir configs/<config name>.<number>/strings/<lang>
0242
0243 e.g.::
0244
0245 $ rmdir configs/c.1/strings/0x409
0246
0247 ...
0248 ...
0249 ...
0250
0251 and remove the configurations::
0252
0253 $ rmdir configs/<config name>.<number>
0254
0255 e.g.::
0256
0257 rmdir configs/c.1
0258
0259 ...
0260 ...
0261 ...
0262
0263 Remove functions (function modules are not unloaded, though):
0264
0265 $ rmdir functions/<name>.<instance name>
0266
0267 e.g.::
0268
0269 $ rmdir functions/ncm.usb0
0270
0271 ...
0272 ...
0273 ...
0274
0275 Remove strings directories in the gadget::
0276
0277 $ rmdir strings/<lang>
0278
0279 e.g.::
0280
0281 $ rmdir strings/0x409
0282
0283 and finally remove the gadget::
0284
0285 $ cd ..
0286 $ rmdir <gadget name>
0287
0288 e.g.::
0289
0290 $ rmdir g1
0291
0292
0293
0294
0295 Implementation design
0296 =====================
0297
0298 Below the idea of how configfs works is presented.
0299 In configfs there are items and groups, both represented as directories.
0300 The difference between an item and a group is that a group can contain
0301 other groups. In the picture below only an item is shown.
0302 Both items and groups can have attributes, which are represented as files.
0303 The user can create and remove directories, but cannot remove files,
0304 which can be read-only or read-write, depending on what they represent.
0305
0306 The filesystem part of configfs operates on config_items/groups and
0307 configfs_attributes which are generic and of the same type for all
0308 configured elements. However, they are embedded in usage-specific
0309 larger structures. In the picture below there is a "cs" which contains
0310 a config_item and an "sa" which contains a configfs_attribute.
0311
0312 The filesystem view would be like this::
0313
0314 ./
0315 ./cs (directory)
0316 |
0317 +--sa (file)
0318 |
0319 .
0320 .
0321 .
0322
0323 Whenever a user reads/writes the "sa" file, a function is called
0324 which accepts a struct config_item and a struct configfs_attribute.
0325 In the said function the "cs" and "sa" are retrieved using the well
0326 known container_of technique and an appropriate sa's function (show or
0327 store) is called and passed the "cs" and a character buffer. The "show"
0328 is for displaying the file's contents (copy data from the cs to the
0329 buffer), while the "store" is for modifying the file's contents (copy data
0330 from the buffer to the cs), but it is up to the implementer of the
0331 two functions to decide what they actually do.
0332
0333 ::
0334
0335 typedef struct configured_structure cs;
0336 typedef struct specific_attribute sa;
0337
0338 sa
0339 +----------------------------------+
0340 cs | (*show)(cs *, buffer); |
0341 +-----------------+ | (*store)(cs *, buffer, length); |
0342 | | | |
0343 | +-------------+ | | +------------------+ |
0344 | | struct |-|----|------>|struct | |
0345 | | config_item | | | |configfs_attribute| |
0346 | +-------------+ | | +------------------+ |
0347 | | +----------------------------------+
0348 | data to be set | .
0349 | | .
0350 +-----------------+ .
0351
0352 The file names are decided by the config item/group designer, while
0353 the directories in general can be named at will. A group can have
0354 a number of its default sub-groups created automatically.
0355
0356 For more information on configfs please see
0357 `Documentation/filesystems/configfs.rst`.
0358
0359 The concepts described above translate to USB gadgets like this:
0360
0361 1. A gadget has its config group, which has some attributes (idVendor,
0362 idProduct etc) and default sub-groups (configs, functions, strings).
0363 Writing to the attributes causes the information to be stored in
0364 appropriate locations. In the configs, functions and strings sub-groups
0365 a user can create their sub-groups to represent configurations, functions,
0366 and groups of strings in a given language.
0367
0368 2. The user creates configurations and functions, in the configurations
0369 creates symbolic links to functions. This information is used when the
0370 gadget's UDC attribute is written to, which means binding the gadget
0371 to the UDC. The code in drivers/usb/gadget/configfs.c iterates over
0372 all configurations, and in each configuration it iterates over all
0373 functions and binds them. This way the whole gadget is bound.
0374
0375 3. The file drivers/usb/gadget/configfs.c contains code for
0376
0377 - gadget's config_group
0378 - gadget's default groups (configs, functions, strings)
0379 - associating functions with configurations (symlinks)
0380
0381 4. Each USB function naturally has its own view of what it wants
0382 configured, so config_groups for particular functions are defined
0383 in the functions implementation files drivers/usb/gadget/f_*.c.
0384
0385 5. Function's code is written in such a way that it uses
0386
0387 usb_get_function_instance(), which, in turn, calls request_module.
0388 So, provided that modprobe works, modules for particular functions
0389 are loaded automatically. Please note that the converse is not true:
0390 after a gadget is disabled and torn down, the modules remain loaded.