blob: e4566ffb223f29a8124f84e94feaf1a7cfc865b9 [file] [log] [blame]
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -03001============================================
2Linux USB gadget configured through configfs
3============================================
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +02004
5
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300625th April 2013
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +02007
8
9
10
11Overview
12========
13
14A USB Linux Gadget is a device which has a UDC (USB Device Controller) and can
15be connected to a USB Host to extend it with additional functions like a serial
16port or a mass storage capability.
17
18A gadget is seen by its host as a set of configurations, each of which contains
19a number of interfaces which, from the gadget's perspective, are known as
20functions, each function representing e.g. a serial connection or a SCSI disk.
21
22Linux provides a number of functions for gadgets to use.
23
24Creating a gadget means deciding what configurations there will be
25and which functions each configuration will provide.
26
Mauro Carvalho Chehab98264992020-04-27 23:17:21 +020027Configfs (please see `Documentation/filesystems/configfs.rst`) lends itself nicely
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +020028for the purpose of telling the kernel about the above mentioned decision.
29This document is about how to do it.
30
31It also describes how configfs integration into gadget is designed.
32
33
34
35
36Requirements
37============
38
39In order for this to work configfs must be available, so CONFIGFS_FS must be
40'y' or 'm' in .config. As of this writing USB_LIBCOMPOSITE selects CONFIGFS_FS.
41
42
43
44
45Usage
46=====
47
48(The original post describing the first function
49made available through configfs can be seen here:
50http://www.spinics.net/lists/linux-usb/msg76388.html)
51
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -030052::
53
54 $ modprobe libcomposite
55 $ mount none $CONFIGFS_HOME -t configfs
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +020056
57where CONFIGFS_HOME is the mount point for configfs
58
591. Creating the gadgets
60-----------------------
61
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -030062For each gadget to be created its corresponding directory must be created::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +020063
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -030064 $ mkdir $CONFIGFS_HOME/usb_gadget/<gadget name>
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +020065
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -030066e.g.::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +020067
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -030068 $ mkdir $CONFIGFS_HOME/usb_gadget/g1
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +020069
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -030070 ...
71 ...
72 ...
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +020073
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -030074 $ cd $CONFIGFS_HOME/usb_gadget/g1
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +020075
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -030076Each gadget needs to have its vendor id <VID> and product id <PID> specified::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +020077
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -030078 $ echo <VID> > idVendor
79 $ echo <PID> > idProduct
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +020080
81A gadget also needs its serial number, manufacturer and product strings.
82In order to have a place to store them, a strings subdirectory must be created
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -030083for each language, e.g.::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +020084
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -030085 $ mkdir strings/0x409
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +020086
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -030087Then the strings can be specified::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +020088
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -030089 $ echo <serial number> > strings/0x409/serialnumber
90 $ echo <manufacturer> > strings/0x409/manufacturer
91 $ echo <product> > strings/0x409/product
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +020092
932. Creating the configurations
94------------------------------
95
96Each gadget will consist of a number of configurations, their corresponding
97directories must be created:
98
99$ mkdir configs/<name>.<number>
100
101where <name> can be any string which is legal in a filesystem and the
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300102<number> is the configuration's number, e.g.::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200103
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300104 $ mkdir configs/c.1
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200105
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300106 ...
107 ...
108 ...
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200109
110Each configuration also needs its strings, so a subdirectory must be created
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300111for each language, e.g.::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200112
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300113 $ mkdir configs/c.1/strings/0x409
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200114
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300115Then the configuration string can be specified::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200116
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300117 $ echo <configuration> > configs/c.1/strings/0x409/configuration
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200118
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300119Some attributes can also be set for a configuration, e.g.::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200120
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300121 $ echo 120 > configs/c.1/MaxPower
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200122
1233. Creating the functions
124-------------------------
125
126The gadget will provide some functions, for each function its corresponding
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300127directory must be created::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200128
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300129 $ mkdir functions/<name>.<instance name>
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200130
131where <name> corresponds to one of allowed function names and instance name
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300132is an arbitrary string allowed in a filesystem, e.g.::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200133
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300134 $ mkdir functions/ncm.usb0 # usb_f_ncm.ko gets loaded with request_module()
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200135
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300136 ...
137 ...
138 ...
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200139
140Each function provides its specific set of attributes, with either read-only
141or read-write access. Where applicable they need to be written to as
142appropriate.
Wei Ming Chenf7529782021-05-06 20:20:20 +0800143Please refer to Documentation/ABI/testing/configfs-usb-gadget for more information.
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200144
1454. Associating the functions with their configurations
146------------------------------------------------------
147
148At this moment a number of gadgets is created, each of which has a number of
149configurations specified and a number of functions available. What remains
150is specifying which function is available in which configuration (the same
151function can be used in multiple configurations). This is achieved with
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300152creating symbolic links::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200153
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300154 $ ln -s functions/<name>.<instance name> configs/<name>.<number>
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200155
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300156e.g.::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200157
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300158 $ ln -s functions/ncm.usb0 configs/c.1
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200159
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300160 ...
161 ...
162 ...
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200163
1645. Enabling the gadget
165----------------------
166
167All the above steps serve the purpose of composing the gadget of
168configurations and functions.
169
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300170An example directory structure might look like this::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200171
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300172 .
173 ./strings
174 ./strings/0x409
175 ./strings/0x409/serialnumber
176 ./strings/0x409/product
177 ./strings/0x409/manufacturer
178 ./configs
179 ./configs/c.1
180 ./configs/c.1/ncm.usb0 -> ../../../../usb_gadget/g1/functions/ncm.usb0
181 ./configs/c.1/strings
182 ./configs/c.1/strings/0x409
183 ./configs/c.1/strings/0x409/configuration
184 ./configs/c.1/bmAttributes
185 ./configs/c.1/MaxPower
186 ./functions
187 ./functions/ncm.usb0
188 ./functions/ncm.usb0/ifname
189 ./functions/ncm.usb0/qmult
190 ./functions/ncm.usb0/host_addr
191 ./functions/ncm.usb0/dev_addr
192 ./UDC
193 ./bcdUSB
194 ./bcdDevice
195 ./idProduct
196 ./idVendor
197 ./bMaxPacketSize0
198 ./bDeviceProtocol
199 ./bDeviceSubClass
200 ./bDeviceClass
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200201
202
203Such a gadget must be finally enabled so that the USB host can enumerate it.
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200204
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300205In order to enable the gadget it must be bound to a UDC (USB Device
206Controller)::
207
208 $ echo <udc name> > UDC
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200209
210where <udc name> is one of those found in /sys/class/udc/*
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300211e.g.::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200212
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300213 $ echo s3c-hsotg > UDC
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200214
215
2166. Disabling the gadget
217-----------------------
218
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300219::
220
221 $ echo "" > UDC
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200222
2237. Cleaning up
224--------------
225
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300226Remove functions from configurations::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200227
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300228 $ rm configs/<config name>.<number>/<function>
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200229
230where <config name>.<number> specify the configuration and <function> is
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300231a symlink to a function being removed from the configuration, e.g.::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200232
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300233 $ rm configs/c.1/ncm.usb0
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200234
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300235 ...
236 ...
237 ...
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200238
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300239Remove strings directories in configurations:
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200240
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300241 $ rmdir configs/<config name>.<number>/strings/<lang>
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200242
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300243e.g.::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200244
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300245 $ rmdir configs/c.1/strings/0x409
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200246
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300247 ...
248 ...
249 ...
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200250
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300251and remove the configurations::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200252
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300253 $ rmdir configs/<config name>.<number>
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200254
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300255e.g.::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200256
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300257 rmdir configs/c.1
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200258
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300259 ...
260 ...
261 ...
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200262
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300263Remove functions (function modules are not unloaded, though):
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200264
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300265 $ rmdir functions/<name>.<instance name>
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200266
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300267e.g.::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200268
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300269 $ rmdir functions/ncm.usb0
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200270
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300271 ...
272 ...
273 ...
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200274
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300275Remove strings directories in the gadget::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200276
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300277 $ rmdir strings/<lang>
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200278
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300279e.g.::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200280
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300281 $ rmdir strings/0x409
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200282
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300283and finally remove the gadget::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200284
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300285 $ cd ..
286 $ rmdir <gadget name>
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200287
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300288e.g.::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200289
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300290 $ rmdir g1
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200291
292
293
294
295Implementation design
296=====================
297
298Below the idea of how configfs works is presented.
299In configfs there are items and groups, both represented as directories.
300The difference between an item and a group is that a group can contain
301other groups. In the picture below only an item is shown.
302Both items and groups can have attributes, which are represented as files.
303The user can create and remove directories, but cannot remove files,
304which can be read-only or read-write, depending on what they represent.
305
306The filesystem part of configfs operates on config_items/groups and
307configfs_attributes which are generic and of the same type for all
308configured elements. However, they are embedded in usage-specific
309larger structures. In the picture below there is a "cs" which contains
310a config_item and an "sa" which contains a configfs_attribute.
311
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300312The filesystem view would be like this::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200313
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300314 ./
315 ./cs (directory)
316 |
317 +--sa (file)
318 |
319 .
320 .
321 .
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200322
323Whenever a user reads/writes the "sa" file, a function is called
324which accepts a struct config_item and a struct configfs_attribute.
325In the said function the "cs" and "sa" are retrieved using the well
326known container_of technique and an appropriate sa's function (show or
327store) is called and passed the "cs" and a character buffer. The "show"
328is for displaying the file's contents (copy data from the cs to the
329buffer), while the "store" is for modifying the file's contents (copy data
330from the buffer to the cs), but it is up to the implementer of the
331two functions to decide what they actually do.
332
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300333::
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200334
Mauro Carvalho Chehabd80b5002019-04-15 23:56:01 -0300335 typedef struct configured_structure cs;
336 typedef struct specific_attribute sa;
337
338 sa
339 +----------------------------------+
340 cs | (*show)(cs *, buffer); |
341 +-----------------+ | (*store)(cs *, buffer, length); |
342 | | | |
343 | +-------------+ | | +------------------+ |
344 | | struct |-|----|------>|struct | |
345 | | config_item | | | |configfs_attribute| |
346 | +-------------+ | | +------------------+ |
347 | | +----------------------------------+
348 | data to be set | .
349 | | .
350 +-----------------+ .
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200351
352The file names are decided by the config item/group designer, while
353the directories in general can be named at will. A group can have
354a number of its default sub-groups created automatically.
355
356For more information on configfs please see
Mauro Carvalho Chehab98264992020-04-27 23:17:21 +0200357`Documentation/filesystems/configfs.rst`.
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200358
359The concepts described above translate to USB gadgets like this:
360
3611. A gadget has its config group, which has some attributes (idVendor,
362idProduct etc) and default sub-groups (configs, functions, strings).
363Writing to the attributes causes the information to be stored in
364appropriate locations. In the configs, functions and strings sub-groups
365a user can create their sub-groups to represent configurations, functions,
366and groups of strings in a given language.
367
3682. The user creates configurations and functions, in the configurations
369creates symbolic links to functions. This information is used when the
370gadget's UDC attribute is written to, which means binding the gadget
371to the UDC. The code in drivers/usb/gadget/configfs.c iterates over
372all configurations, and in each configuration it iterates over all
373functions and binds them. This way the whole gadget is bound.
374
3753. The file drivers/usb/gadget/configfs.c contains code for
376
377 - gadget's config_group
378 - gadget's default groups (configs, functions, strings)
379 - associating functions with configurations (symlinks)
380
3814. Each USB function naturally has its own view of what it wants
382configured, so config_groups for particular functions are defined
383in the functions implementation files drivers/usb/gadget/f_*.c.
384
Frans Klaver720a8d32014-10-01 15:30:31 +02003855. Function's code is written in such a way that it uses
Andrzej Pietrasiewicz5e654a42013-06-13 10:37:25 +0200386
387usb_get_function_instance(), which, in turn, calls request_module.
388So, provided that modprobe works, modules for particular functions
389are loaded automatically. Please note that the converse is not true:
390after a gadget is disabled and torn down, the modules remain loaded.