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