1*4882a593Smuzhiyun============================================ 2*4882a593SmuzhiyunLinux USB gadget configured through configfs 3*4882a593Smuzhiyun============================================ 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun 6*4882a593Smuzhiyun25th April 2013 7*4882a593Smuzhiyun 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun 10*4882a593Smuzhiyun 11*4882a593SmuzhiyunOverview 12*4882a593Smuzhiyun======== 13*4882a593Smuzhiyun 14*4882a593SmuzhiyunA USB Linux Gadget is a device which has a UDC (USB Device Controller) and can 15*4882a593Smuzhiyunbe connected to a USB Host to extend it with additional functions like a serial 16*4882a593Smuzhiyunport or a mass storage capability. 17*4882a593Smuzhiyun 18*4882a593SmuzhiyunA gadget is seen by its host as a set of configurations, each of which contains 19*4882a593Smuzhiyuna number of interfaces which, from the gadget's perspective, are known as 20*4882a593Smuzhiyunfunctions, each function representing e.g. a serial connection or a SCSI disk. 21*4882a593Smuzhiyun 22*4882a593SmuzhiyunLinux provides a number of functions for gadgets to use. 23*4882a593Smuzhiyun 24*4882a593SmuzhiyunCreating a gadget means deciding what configurations there will be 25*4882a593Smuzhiyunand which functions each configuration will provide. 26*4882a593Smuzhiyun 27*4882a593SmuzhiyunConfigfs (please see `Documentation/filesystems/configfs.rst`) lends itself nicely 28*4882a593Smuzhiyunfor the purpose of telling the kernel about the above mentioned decision. 29*4882a593SmuzhiyunThis document is about how to do it. 30*4882a593Smuzhiyun 31*4882a593SmuzhiyunIt also describes how configfs integration into gadget is designed. 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun 35*4882a593Smuzhiyun 36*4882a593SmuzhiyunRequirements 37*4882a593Smuzhiyun============ 38*4882a593Smuzhiyun 39*4882a593SmuzhiyunIn order for this to work configfs must be available, so CONFIGFS_FS must be 40*4882a593Smuzhiyun'y' or 'm' in .config. As of this writing USB_LIBCOMPOSITE selects CONFIGFS_FS. 41*4882a593Smuzhiyun 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun 45*4882a593SmuzhiyunUsage 46*4882a593Smuzhiyun===== 47*4882a593Smuzhiyun 48*4882a593Smuzhiyun(The original post describing the first function 49*4882a593Smuzhiyunmade available through configfs can be seen here: 50*4882a593Smuzhiyunhttp://www.spinics.net/lists/linux-usb/msg76388.html) 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun:: 53*4882a593Smuzhiyun 54*4882a593Smuzhiyun $ modprobe libcomposite 55*4882a593Smuzhiyun $ mount none $CONFIGFS_HOME -t configfs 56*4882a593Smuzhiyun 57*4882a593Smuzhiyunwhere CONFIGFS_HOME is the mount point for configfs 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun1. Creating the gadgets 60*4882a593Smuzhiyun----------------------- 61*4882a593Smuzhiyun 62*4882a593SmuzhiyunFor each gadget to be created its corresponding directory must be created:: 63*4882a593Smuzhiyun 64*4882a593Smuzhiyun $ mkdir $CONFIGFS_HOME/usb_gadget/<gadget name> 65*4882a593Smuzhiyun 66*4882a593Smuzhiyune.g.:: 67*4882a593Smuzhiyun 68*4882a593Smuzhiyun $ mkdir $CONFIGFS_HOME/usb_gadget/g1 69*4882a593Smuzhiyun 70*4882a593Smuzhiyun ... 71*4882a593Smuzhiyun ... 72*4882a593Smuzhiyun ... 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun $ cd $CONFIGFS_HOME/usb_gadget/g1 75*4882a593Smuzhiyun 76*4882a593SmuzhiyunEach gadget needs to have its vendor id <VID> and product id <PID> specified:: 77*4882a593Smuzhiyun 78*4882a593Smuzhiyun $ echo <VID> > idVendor 79*4882a593Smuzhiyun $ echo <PID> > idProduct 80*4882a593Smuzhiyun 81*4882a593SmuzhiyunA gadget also needs its serial number, manufacturer and product strings. 82*4882a593SmuzhiyunIn order to have a place to store them, a strings subdirectory must be created 83*4882a593Smuzhiyunfor each language, e.g.:: 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun $ mkdir strings/0x409 86*4882a593Smuzhiyun 87*4882a593SmuzhiyunThen the strings can be specified:: 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun $ echo <serial number> > strings/0x409/serialnumber 90*4882a593Smuzhiyun $ echo <manufacturer> > strings/0x409/manufacturer 91*4882a593Smuzhiyun $ echo <product> > strings/0x409/product 92*4882a593Smuzhiyun 93*4882a593Smuzhiyun2. Creating the configurations 94*4882a593Smuzhiyun------------------------------ 95*4882a593Smuzhiyun 96*4882a593SmuzhiyunEach gadget will consist of a number of configurations, their corresponding 97*4882a593Smuzhiyundirectories must be created: 98*4882a593Smuzhiyun 99*4882a593Smuzhiyun$ mkdir configs/<name>.<number> 100*4882a593Smuzhiyun 101*4882a593Smuzhiyunwhere <name> can be any string which is legal in a filesystem and the 102*4882a593Smuzhiyun<number> is the configuration's number, e.g.:: 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun $ mkdir configs/c.1 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun ... 107*4882a593Smuzhiyun ... 108*4882a593Smuzhiyun ... 109*4882a593Smuzhiyun 110*4882a593SmuzhiyunEach configuration also needs its strings, so a subdirectory must be created 111*4882a593Smuzhiyunfor each language, e.g.:: 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun $ mkdir configs/c.1/strings/0x409 114*4882a593Smuzhiyun 115*4882a593SmuzhiyunThen the configuration string can be specified:: 116*4882a593Smuzhiyun 117*4882a593Smuzhiyun $ echo <configuration> > configs/c.1/strings/0x409/configuration 118*4882a593Smuzhiyun 119*4882a593SmuzhiyunSome attributes can also be set for a configuration, e.g.:: 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun $ echo 120 > configs/c.1/MaxPower 122*4882a593Smuzhiyun 123*4882a593Smuzhiyun3. Creating the functions 124*4882a593Smuzhiyun------------------------- 125*4882a593Smuzhiyun 126*4882a593SmuzhiyunThe gadget will provide some functions, for each function its corresponding 127*4882a593Smuzhiyundirectory must be created:: 128*4882a593Smuzhiyun 129*4882a593Smuzhiyun $ mkdir functions/<name>.<instance name> 130*4882a593Smuzhiyun 131*4882a593Smuzhiyunwhere <name> corresponds to one of allowed function names and instance name 132*4882a593Smuzhiyunis an arbitrary string allowed in a filesystem, e.g.:: 133*4882a593Smuzhiyun 134*4882a593Smuzhiyun $ mkdir functions/ncm.usb0 # usb_f_ncm.ko gets loaded with request_module() 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun ... 137*4882a593Smuzhiyun ... 138*4882a593Smuzhiyun ... 139*4882a593Smuzhiyun 140*4882a593SmuzhiyunEach function provides its specific set of attributes, with either read-only 141*4882a593Smuzhiyunor read-write access. Where applicable they need to be written to as 142*4882a593Smuzhiyunappropriate. 143*4882a593SmuzhiyunPlease refer to Documentation/ABI/*/configfs-usb-gadget* for more information. 144*4882a593Smuzhiyun 145*4882a593Smuzhiyun4. Associating the functions with their configurations 146*4882a593Smuzhiyun------------------------------------------------------ 147*4882a593Smuzhiyun 148*4882a593SmuzhiyunAt this moment a number of gadgets is created, each of which has a number of 149*4882a593Smuzhiyunconfigurations specified and a number of functions available. What remains 150*4882a593Smuzhiyunis specifying which function is available in which configuration (the same 151*4882a593Smuzhiyunfunction can be used in multiple configurations). This is achieved with 152*4882a593Smuzhiyuncreating symbolic links:: 153*4882a593Smuzhiyun 154*4882a593Smuzhiyun $ ln -s functions/<name>.<instance name> configs/<name>.<number> 155*4882a593Smuzhiyun 156*4882a593Smuzhiyune.g.:: 157*4882a593Smuzhiyun 158*4882a593Smuzhiyun $ ln -s functions/ncm.usb0 configs/c.1 159*4882a593Smuzhiyun 160*4882a593Smuzhiyun ... 161*4882a593Smuzhiyun ... 162*4882a593Smuzhiyun ... 163*4882a593Smuzhiyun 164*4882a593Smuzhiyun5. Enabling the gadget 165*4882a593Smuzhiyun---------------------- 166*4882a593Smuzhiyun 167*4882a593SmuzhiyunAll the above steps serve the purpose of composing the gadget of 168*4882a593Smuzhiyunconfigurations and functions. 169*4882a593Smuzhiyun 170*4882a593SmuzhiyunAn example directory structure might look like this:: 171*4882a593Smuzhiyun 172*4882a593Smuzhiyun . 173*4882a593Smuzhiyun ./strings 174*4882a593Smuzhiyun ./strings/0x409 175*4882a593Smuzhiyun ./strings/0x409/serialnumber 176*4882a593Smuzhiyun ./strings/0x409/product 177*4882a593Smuzhiyun ./strings/0x409/manufacturer 178*4882a593Smuzhiyun ./configs 179*4882a593Smuzhiyun ./configs/c.1 180*4882a593Smuzhiyun ./configs/c.1/ncm.usb0 -> ../../../../usb_gadget/g1/functions/ncm.usb0 181*4882a593Smuzhiyun ./configs/c.1/strings 182*4882a593Smuzhiyun ./configs/c.1/strings/0x409 183*4882a593Smuzhiyun ./configs/c.1/strings/0x409/configuration 184*4882a593Smuzhiyun ./configs/c.1/bmAttributes 185*4882a593Smuzhiyun ./configs/c.1/MaxPower 186*4882a593Smuzhiyun ./functions 187*4882a593Smuzhiyun ./functions/ncm.usb0 188*4882a593Smuzhiyun ./functions/ncm.usb0/ifname 189*4882a593Smuzhiyun ./functions/ncm.usb0/qmult 190*4882a593Smuzhiyun ./functions/ncm.usb0/host_addr 191*4882a593Smuzhiyun ./functions/ncm.usb0/dev_addr 192*4882a593Smuzhiyun ./UDC 193*4882a593Smuzhiyun ./bcdUSB 194*4882a593Smuzhiyun ./bcdDevice 195*4882a593Smuzhiyun ./idProduct 196*4882a593Smuzhiyun ./idVendor 197*4882a593Smuzhiyun ./bMaxPacketSize0 198*4882a593Smuzhiyun ./bDeviceProtocol 199*4882a593Smuzhiyun ./bDeviceSubClass 200*4882a593Smuzhiyun ./bDeviceClass 201*4882a593Smuzhiyun 202*4882a593Smuzhiyun 203*4882a593SmuzhiyunSuch a gadget must be finally enabled so that the USB host can enumerate it. 204*4882a593Smuzhiyun 205*4882a593SmuzhiyunIn order to enable the gadget it must be bound to a UDC (USB Device 206*4882a593SmuzhiyunController):: 207*4882a593Smuzhiyun 208*4882a593Smuzhiyun $ echo <udc name> > UDC 209*4882a593Smuzhiyun 210*4882a593Smuzhiyunwhere <udc name> is one of those found in /sys/class/udc/* 211*4882a593Smuzhiyune.g.:: 212*4882a593Smuzhiyun 213*4882a593Smuzhiyun $ echo s3c-hsotg > UDC 214*4882a593Smuzhiyun 215*4882a593Smuzhiyun 216*4882a593Smuzhiyun6. Disabling the gadget 217*4882a593Smuzhiyun----------------------- 218*4882a593Smuzhiyun 219*4882a593Smuzhiyun:: 220*4882a593Smuzhiyun 221*4882a593Smuzhiyun $ echo "" > UDC 222*4882a593Smuzhiyun 223*4882a593Smuzhiyun7. Cleaning up 224*4882a593Smuzhiyun-------------- 225*4882a593Smuzhiyun 226*4882a593SmuzhiyunRemove functions from configurations:: 227*4882a593Smuzhiyun 228*4882a593Smuzhiyun $ rm configs/<config name>.<number>/<function> 229*4882a593Smuzhiyun 230*4882a593Smuzhiyunwhere <config name>.<number> specify the configuration and <function> is 231*4882a593Smuzhiyuna symlink to a function being removed from the configuration, e.g.:: 232*4882a593Smuzhiyun 233*4882a593Smuzhiyun $ rm configs/c.1/ncm.usb0 234*4882a593Smuzhiyun 235*4882a593Smuzhiyun ... 236*4882a593Smuzhiyun ... 237*4882a593Smuzhiyun ... 238*4882a593Smuzhiyun 239*4882a593SmuzhiyunRemove strings directories in configurations: 240*4882a593Smuzhiyun 241*4882a593Smuzhiyun $ rmdir configs/<config name>.<number>/strings/<lang> 242*4882a593Smuzhiyun 243*4882a593Smuzhiyune.g.:: 244*4882a593Smuzhiyun 245*4882a593Smuzhiyun $ rmdir configs/c.1/strings/0x409 246*4882a593Smuzhiyun 247*4882a593Smuzhiyun ... 248*4882a593Smuzhiyun ... 249*4882a593Smuzhiyun ... 250*4882a593Smuzhiyun 251*4882a593Smuzhiyunand remove the configurations:: 252*4882a593Smuzhiyun 253*4882a593Smuzhiyun $ rmdir configs/<config name>.<number> 254*4882a593Smuzhiyun 255*4882a593Smuzhiyune.g.:: 256*4882a593Smuzhiyun 257*4882a593Smuzhiyun rmdir configs/c.1 258*4882a593Smuzhiyun 259*4882a593Smuzhiyun ... 260*4882a593Smuzhiyun ... 261*4882a593Smuzhiyun ... 262*4882a593Smuzhiyun 263*4882a593SmuzhiyunRemove functions (function modules are not unloaded, though): 264*4882a593Smuzhiyun 265*4882a593Smuzhiyun $ rmdir functions/<name>.<instance name> 266*4882a593Smuzhiyun 267*4882a593Smuzhiyune.g.:: 268*4882a593Smuzhiyun 269*4882a593Smuzhiyun $ rmdir functions/ncm.usb0 270*4882a593Smuzhiyun 271*4882a593Smuzhiyun ... 272*4882a593Smuzhiyun ... 273*4882a593Smuzhiyun ... 274*4882a593Smuzhiyun 275*4882a593SmuzhiyunRemove strings directories in the gadget:: 276*4882a593Smuzhiyun 277*4882a593Smuzhiyun $ rmdir strings/<lang> 278*4882a593Smuzhiyun 279*4882a593Smuzhiyune.g.:: 280*4882a593Smuzhiyun 281*4882a593Smuzhiyun $ rmdir strings/0x409 282*4882a593Smuzhiyun 283*4882a593Smuzhiyunand finally remove the gadget:: 284*4882a593Smuzhiyun 285*4882a593Smuzhiyun $ cd .. 286*4882a593Smuzhiyun $ rmdir <gadget name> 287*4882a593Smuzhiyun 288*4882a593Smuzhiyune.g.:: 289*4882a593Smuzhiyun 290*4882a593Smuzhiyun $ rmdir g1 291*4882a593Smuzhiyun 292*4882a593Smuzhiyun 293*4882a593Smuzhiyun 294*4882a593Smuzhiyun 295*4882a593SmuzhiyunImplementation design 296*4882a593Smuzhiyun===================== 297*4882a593Smuzhiyun 298*4882a593SmuzhiyunBelow the idea of how configfs works is presented. 299*4882a593SmuzhiyunIn configfs there are items and groups, both represented as directories. 300*4882a593SmuzhiyunThe difference between an item and a group is that a group can contain 301*4882a593Smuzhiyunother groups. In the picture below only an item is shown. 302*4882a593SmuzhiyunBoth items and groups can have attributes, which are represented as files. 303*4882a593SmuzhiyunThe user can create and remove directories, but cannot remove files, 304*4882a593Smuzhiyunwhich can be read-only or read-write, depending on what they represent. 305*4882a593Smuzhiyun 306*4882a593SmuzhiyunThe filesystem part of configfs operates on config_items/groups and 307*4882a593Smuzhiyunconfigfs_attributes which are generic and of the same type for all 308*4882a593Smuzhiyunconfigured elements. However, they are embedded in usage-specific 309*4882a593Smuzhiyunlarger structures. In the picture below there is a "cs" which contains 310*4882a593Smuzhiyuna config_item and an "sa" which contains a configfs_attribute. 311*4882a593Smuzhiyun 312*4882a593SmuzhiyunThe filesystem view would be like this:: 313*4882a593Smuzhiyun 314*4882a593Smuzhiyun ./ 315*4882a593Smuzhiyun ./cs (directory) 316*4882a593Smuzhiyun | 317*4882a593Smuzhiyun +--sa (file) 318*4882a593Smuzhiyun | 319*4882a593Smuzhiyun . 320*4882a593Smuzhiyun . 321*4882a593Smuzhiyun . 322*4882a593Smuzhiyun 323*4882a593SmuzhiyunWhenever a user reads/writes the "sa" file, a function is called 324*4882a593Smuzhiyunwhich accepts a struct config_item and a struct configfs_attribute. 325*4882a593SmuzhiyunIn the said function the "cs" and "sa" are retrieved using the well 326*4882a593Smuzhiyunknown container_of technique and an appropriate sa's function (show or 327*4882a593Smuzhiyunstore) is called and passed the "cs" and a character buffer. The "show" 328*4882a593Smuzhiyunis for displaying the file's contents (copy data from the cs to the 329*4882a593Smuzhiyunbuffer), while the "store" is for modifying the file's contents (copy data 330*4882a593Smuzhiyunfrom the buffer to the cs), but it is up to the implementer of the 331*4882a593Smuzhiyuntwo functions to decide what they actually do. 332*4882a593Smuzhiyun 333*4882a593Smuzhiyun:: 334*4882a593Smuzhiyun 335*4882a593Smuzhiyun typedef struct configured_structure cs; 336*4882a593Smuzhiyun typedef struct specific_attribute sa; 337*4882a593Smuzhiyun 338*4882a593Smuzhiyun sa 339*4882a593Smuzhiyun +----------------------------------+ 340*4882a593Smuzhiyun cs | (*show)(cs *, buffer); | 341*4882a593Smuzhiyun +-----------------+ | (*store)(cs *, buffer, length); | 342*4882a593Smuzhiyun | | | | 343*4882a593Smuzhiyun | +-------------+ | | +------------------+ | 344*4882a593Smuzhiyun | | struct |-|----|------>|struct | | 345*4882a593Smuzhiyun | | config_item | | | |configfs_attribute| | 346*4882a593Smuzhiyun | +-------------+ | | +------------------+ | 347*4882a593Smuzhiyun | | +----------------------------------+ 348*4882a593Smuzhiyun | data to be set | . 349*4882a593Smuzhiyun | | . 350*4882a593Smuzhiyun +-----------------+ . 351*4882a593Smuzhiyun 352*4882a593SmuzhiyunThe file names are decided by the config item/group designer, while 353*4882a593Smuzhiyunthe directories in general can be named at will. A group can have 354*4882a593Smuzhiyuna number of its default sub-groups created automatically. 355*4882a593Smuzhiyun 356*4882a593SmuzhiyunFor more information on configfs please see 357*4882a593Smuzhiyun`Documentation/filesystems/configfs.rst`. 358*4882a593Smuzhiyun 359*4882a593SmuzhiyunThe concepts described above translate to USB gadgets like this: 360*4882a593Smuzhiyun 361*4882a593Smuzhiyun1. A gadget has its config group, which has some attributes (idVendor, 362*4882a593SmuzhiyunidProduct etc) and default sub-groups (configs, functions, strings). 363*4882a593SmuzhiyunWriting to the attributes causes the information to be stored in 364*4882a593Smuzhiyunappropriate locations. In the configs, functions and strings sub-groups 365*4882a593Smuzhiyuna user can create their sub-groups to represent configurations, functions, 366*4882a593Smuzhiyunand groups of strings in a given language. 367*4882a593Smuzhiyun 368*4882a593Smuzhiyun2. The user creates configurations and functions, in the configurations 369*4882a593Smuzhiyuncreates symbolic links to functions. This information is used when the 370*4882a593Smuzhiyungadget's UDC attribute is written to, which means binding the gadget 371*4882a593Smuzhiyunto the UDC. The code in drivers/usb/gadget/configfs.c iterates over 372*4882a593Smuzhiyunall configurations, and in each configuration it iterates over all 373*4882a593Smuzhiyunfunctions and binds them. This way the whole gadget is bound. 374*4882a593Smuzhiyun 375*4882a593Smuzhiyun3. The file drivers/usb/gadget/configfs.c contains code for 376*4882a593Smuzhiyun 377*4882a593Smuzhiyun - gadget's config_group 378*4882a593Smuzhiyun - gadget's default groups (configs, functions, strings) 379*4882a593Smuzhiyun - associating functions with configurations (symlinks) 380*4882a593Smuzhiyun 381*4882a593Smuzhiyun4. Each USB function naturally has its own view of what it wants 382*4882a593Smuzhiyunconfigured, so config_groups for particular functions are defined 383*4882a593Smuzhiyunin the functions implementation files drivers/usb/gadget/f_*.c. 384*4882a593Smuzhiyun 385*4882a593Smuzhiyun5. Function's code is written in such a way that it uses 386*4882a593Smuzhiyun 387*4882a593Smuzhiyunusb_get_function_instance(), which, in turn, calls request_module. 388*4882a593SmuzhiyunSo, provided that modprobe works, modules for particular functions 389*4882a593Smuzhiyunare loaded automatically. Please note that the converse is not true: 390*4882a593Smuzhiyunafter a gadget is disabled and torn down, the modules remain loaded. 391