Linux USB gadget configured through configfs — The Linux Kernel documentation (2024)

25th April 2013

Overview

A USB Linux Gadget is a device which has a UDC (USB Device Controller) and canbe connected to a USB Host to extend it with additional functions like a serialport or a mass storage capability.

A gadget is seen by its host as a set of configurations, each of which containsa number of interfaces which, from the gadget’s perspective, are known asfunctions, each function representing e.g. a serial connection or a SCSI disk.

Linux provides a number of functions for gadgets to use.

Creating a gadget means deciding what configurations there will beand which functions each configuration will provide.

Configfs (please see Configfs - Userspace-driven Kernel Object Configuration) lends itself nicelyfor the purpose of telling the kernel about the above mentioned decision.This document is about how to do it.

It also describes how configfs integration into gadget is designed.

Requirements

In order for this to work configfs must be available, so CONFIGFS_FS must be‘y’ or ‘m’ in .config. As of this writing USB_LIBCOMPOSITE selects CONFIGFS_FS.

Usage

(The original post describing the first functionmade available through configfs can be seen here:http://www.spinics.net/lists/linux-usb/msg76388.html)

$ modprobe libcomposite$ mount none $CONFIGFS_HOME -t configfs

where CONFIGFS_HOME is the mount point for configfs

1. Creating the gadgets

For each gadget to be created its corresponding directory must be created:

$ mkdir $CONFIGFS_HOME/usb_gadget/<gadget name>

e.g.:

$ mkdir $CONFIGFS_HOME/usb_gadget/g1.........$ cd $CONFIGFS_HOME/usb_gadget/g1

Each gadget needs to have its vendor id <VID> and product id <PID> specified:

$ echo <VID> > idVendor$ echo <PID> > idProduct

A gadget also needs its serial number, manufacturer and product strings.In order to have a place to store them, a strings subdirectory must be createdfor each language, e.g.:

$ mkdir strings/0x409

Then the strings can be specified:

$ echo <serial number> > strings/0x409/serialnumber$ echo <manufacturer> > strings/0x409/manufacturer$ echo <product> > strings/0x409/product

Further custom string descriptors can be created as directories within thelanguage’s directory, with the string text being written to the “s” attributewithin the string’s directory:

$ mkdir strings/0x409/xu.0$ echo <string text> > strings/0x409/xu.0/s

Where function drivers support it, functions may allow symlinks to these customstring descriptors to associate those strings with class descriptors.

2. Creating the configurations

Each gadget will consist of a number of configurations, their correspondingdirectories must be created:

$ mkdir configs/<name>.<number>

where <name> can be any string which is legal in a filesystem and the<number> is the configuration’s number, e.g.:

$ mkdir configs/c.1.........

Each configuration also needs its strings, so a subdirectory must be createdfor each language, e.g.:

$ mkdir configs/c.1/strings/0x409

Then the configuration string can be specified:

$ echo <configuration> > configs/c.1/strings/0x409/configuration

Some attributes can also be set for a configuration, e.g.:

$ echo 120 > configs/c.1/MaxPower

3. Creating the functions

The gadget will provide some functions, for each function its correspondingdirectory must be created:

$ mkdir functions/<name>.<instance name>

where <name> corresponds to one of allowed function names and instance nameis an arbitrary string allowed in a filesystem, e.g.:

$ mkdir functions/ncm.usb0 # usb_f_ncm.ko gets loaded with request_module().........

Each function provides its specific set of attributes, with either read-onlyor read-write access. Where applicable they need to be written to asappropriate.Please refer to Documentation/ABI/testing/configfs-usb-gadget for more information.

4. Associating the functions with their configurations

At this moment a number of gadgets is created, each of which has a number ofconfigurations specified and a number of functions available. What remainsis specifying which function is available in which configuration (the samefunction can be used in multiple configurations). This is achieved withcreating symbolic links:

$ ln -s functions/<name>.<instance name> configs/<name>.<number>

e.g.:

$ ln -s functions/ncm.usb0 configs/c.1.........

5. Enabling the gadget

All the above steps serve the purpose of composing the gadget ofconfigurations and functions.

An example directory structure might look like this:

../strings./strings/0x409./strings/0x409/serialnumber./strings/0x409/product./strings/0x409/manufacturer./configs./configs/c.1./configs/c.1/ncm.usb0 -> ../../../../usb_gadget/g1/functions/ncm.usb0./configs/c.1/strings./configs/c.1/strings/0x409./configs/c.1/strings/0x409/configuration./configs/c.1/bmAttributes./configs/c.1/MaxPower./functions./functions/ncm.usb0./functions/ncm.usb0/ifname./functions/ncm.usb0/qmult./functions/ncm.usb0/host_addr./functions/ncm.usb0/dev_addr./UDC./bcdUSB./bcdDevice./idProduct./idVendor./bMaxPacketSize0./bDeviceProtocol./bDeviceSubClass./bDeviceClass

Such a gadget must be finally enabled so that the USB host can enumerate it.

In order to enable the gadget it must be bound to a UDC (USB DeviceController):

$ echo <udc name> > UDC

where <udc name> is one of those found in /sys/class/udc/*e.g.:

$ echo s3c-hsotg > UDC

6. Disabling the gadget

$ echo "" > UDC

7. Cleaning up

Remove functions from configurations:

$ rm configs/<config name>.<number>/<function>

where <config name>.<number> specify the configuration and <function> isa symlink to a function being removed from the configuration, e.g.:

$ rm configs/c.1/ncm.usb0.........

Remove strings directories in configurations:

$ rmdir configs/<config name>.<number>/strings/<lang>

e.g.:

$ rmdir configs/c.1/strings/0x409.........

and remove the configurations:

$ rmdir configs/<config name>.<number>

e.g.:

rmdir configs/c.1.........

Remove functions (function modules are not unloaded, though):

$ rmdir functions/<name>.<instance name>

e.g.:

$ rmdir functions/ncm.usb0.........

Remove strings directories in the gadget:

$ rmdir strings/<lang>

e.g.:

$ rmdir strings/0x409

and finally remove the gadget:

$ cd ..$ rmdir <gadget name>

e.g.:

$ rmdir g1

Implementation design

Below the idea of how configfs works is presented.In configfs there are items and groups, both represented as directories.The difference between an item and a group is that a group can containother groups. In the picture below only an item is shown.Both items and groups can have attributes, which are represented as files.The user can create and remove directories, but cannot remove files,which can be read-only or read-write, depending on what they represent.

The filesystem part of configfs operates on config_items/groups andconfigfs_attributes which are generic and of the same type for allconfigured elements. However, they are embedded in usage-specificlarger structures. In the picture below there is a “cs” which containsa config_item and an “sa” which contains a configfs_attribute.

The filesystem view would be like this:

././cs (directory) | +--sa (file) | . . .

Whenever a user reads/writes the “sa” file, a function is calledwhich accepts a struct config_item and a struct configfs_attribute.In the said function the “cs” and “sa” are retrieved using the wellknown container_of technique and an appropriate sa’s function (show orstore) is called and passed the “cs” and a character buffer. The “show”is for displaying the file’s contents (copy data from the cs to thebuffer), while the “store” is for modifying the file’s contents (copy datafrom the buffer to the cs), but it is up to the implementer of thetwo functions to decide what they actually do.

typedef struct configured_structure cs;typedef struct specific_attribute sa; sa +----------------------------------+ cs | (*show)(cs *, buffer); |+-----------------+ | (*store)(cs *, buffer, length); || | | || +-------------+ | | +------------------+ || | struct |-|----|------>|struct | || | config_item | | | |configfs_attribute| || +-------------+ | | +------------------+ || | +----------------------------------+| data to be set | .| | .+-----------------+ .

The file names are decided by the config item/group designer, whilethe directories in general can be named at will. A group can havea number of its default sub-groups created automatically.

For more information on configfs please seeConfigfs - Userspace-driven Kernel Object Configuration.

The concepts described above translate to USB gadgets like this:

1. A gadget has its config group, which has some attributes (idVendor,idProduct etc) and default sub-groups (configs, functions, strings).Writing to the attributes causes the information to be stored inappropriate locations. In the configs, functions and strings sub-groupsa user can create their sub-groups to represent configurations, functions,and groups of strings in a given language.

2. The user creates configurations and functions, in the configurationscreates symbolic links to functions. This information is used when thegadget’s UDC attribute is written to, which means binding the gadgetto the UDC. The code in drivers/usb/gadget/configfs.c iterates overall configurations, and in each configuration it iterates over allfunctions and binds them. This way the whole gadget is bound.

  1. The file drivers/usb/gadget/configfs.c contains code for

    • gadget’s config_group

    • gadget’s default groups (configs, functions, strings)

    • associating functions with configurations (symlinks)

4. Each USB function naturally has its own view of what it wantsconfigured, so config_groups for particular functions are definedin the functions implementation files drivers/usb/gadget/f_*.c.

  1. Function’s code is written in such a way that it uses

usb_get_function_instance(), which, in turn, calls request_module.So, provided that modprobe works, modules for particular functionsare loaded automatically. Please note that the converse is not true:after a gadget is disabled and torn down, the modules remain loaded.

Linux USB gadget configured through configfs — The Linux Kernel  documentation (2024)

FAQs

How to enable USB in Linux kernel? ›

Authorize a device to connect:
  1. $ echo 1 > /sys/bus/usb/devices/DEVICE/authorized. De-authorize a device:
  2. $ echo 0 > /sys/bus/usb/devices/DEVICE/authorized. ...
  3. $ echo 0 > /sys/bus/usb/devices/usbX/authorized_default. ...
  4. $ echo 1 > /sys/bus/usb/devices/usbX/authorized_default.

What is Linux USB gadget? ›

The <linux/usb/gadget. h> API abstracts the peripheral controller endpoint hardware. That hardware is exposed through endpoint objects, which accept streams of IN/OUT buffers, and through callbacks that interact with gadget drivers.

How does Linux handle USB devices? ›

The USB code in the Linux kernel communicates with all USB devices using something called a urb (USB request block). This request block is described with the struct urb structure and can be found in the include/linux/usb.

What is USB UDC? ›

USB Device Controller (UDC) USB Stack Device. The UDC provides a high-level abstraction of the usb device. You can use these functions to control the main device state (start/attach/wakeup).

How to configure USB on Linux? ›

How to mount usb drive in a linux system
  1. Step 1: Plug-in USB drive to your PC.
  2. Step 2 – Detecting USB Drive. After you plug in your USB device to your Linux system USB port, It will add new block device into /dev/ directory. ...
  3. Step 3 – Creating Mount Point. ...
  4. Step 4 – Delete a Directory in USB. ...
  5. Step 5 – Formatting the USB.
Nov 1, 2023

How to check if a USB port is enabled or not in Linux? ›

Using lsusb

lsusb is a command-line utility that displays information about USB buses and connected devices on a Linux system. It's quite useful for troubleshooting USB connectivity issues and identifying unrecognized devices.

Which device is my USB Linux? ›

To find PID & VID on Linux:

Run the lsusb command to list details of your connected USB devices. Find your desired USB device in the list to see your PID and VID.

Do I need a USB for Linux? ›

If you want to install Linux on a desktop, you'll first have to create a bootable USB drive with your distribution of choice.

What is the USB file system for Linux? ›

Which File System to Use for USB Formatting?
File SystemSupported File SizeIdeal Usage
FAT32up to 4 GBFor maximum compatibility.
NTFS16 EiB - 1 KBFor internal drives, Windows system files, and large files.
exFAT16 EiB - 1 KBFor files larger than 4 GB.
UDF16EiB - 1KBFor maximum compatibility when transferring large files.
Apr 30, 2024

What is USB device mode? ›

Device mode makes it possible for the computer to present itself as different kinds of USB device classes, including serial ports, network adapters, and mass storage, or a combination thereof. A USB host like a laptop or desktop computer is able to access them just like physical USB devices.

How do I see all USB devices connected to Linux? ›

Simply use the lsusb command (LIST USB) in Linux. To display information about USB buses and connected devices under the Linux kernel, use the lsusb command-line utility. To make use of all the features of this program, you need to have a Linux kernel which supports the /proc/bus/usb interface.

How do I run Linux from a USB? ›

Making a Bootable Linux USB
  1. Download an ISO image of Ubuntu or another Linux flavor.
  2. Install the free Rufus software on your PC.
  3. Insert your flash drive, open Rufus, and select your flash drive.
  4. Choose FreeDOS from the "Boot Selection" menu.
  5. Click “Select” and then choose your ISO, then click “Start.”
Jun 10, 2024

What is USB bootloader? ›

Bootloader is a small program put into a device that allows user application codes to be programmed to the device.

What is USB HID bootloader? ›

The USB Device HID bootloader Library can be used to upgrade firmware on a target device without the need for an external programmer or debugger. Features. Supported on CORTEX-M and MIPS based MCUs. Uses Harmony 3 USB Device HID driver to communicate. Supports Live update.

What is USB root hub in Device Manager? ›

The root hub is a pseudo device that gets attached to the root of the USB device tree topology located at the rear of each HC. The attachment of the root hub starts the device topology discovery process.

How do I give permission to a USB in Linux? ›

Step 1 Enable Port Permissions on Linux
  1. Note: Linux distributions other than Ubuntu may run into issues with the following instructions:
  2. Install the following package by entering: sudo apt-get install libgconf-2-4.
  3. Add yourself to the "dialout" group: sudo usermod -a -G dialout $USER.
Aug 12, 2020

How do I enable and disable USB in Linux? ›

Creating a Linux Removable Storage Policy
  1. Go to DEVICE MANAGEMENT > Policy Management.
  2. Click ( + ).
  3. On the Configure New Policy panel, select Linux.
  4. To the right of Disable USB Storage click configure.
  5. In POLICY NAME you can type in a new title if necessary.
  6. To apply the policy to one or more devices, click Devices.

How to boot USB on Linux? ›

Insert your USB stick (or DVD) into the computer. Restart the computer. Before your computer boots your current operating system (Windows, Mac, Linux) you should see your BIOS loading screen. Check the screen or your computer's documentation to know which key to press and instruct your computer to boot on USB (or DVD).

How to enable USB port in Linux Ubuntu? ›

Using hub-ctrl

hub-ctrl is a Linux command-line utility that controls the power supply of USB hub ports, enabling users to programmatically turn USB devices on or off. Moreover, hub-ctrl simplifies USB power management, allowing for automation and customization according to user preferences.

Top Articles
Latest Posts
Article information

Author: Tyson Zemlak

Last Updated:

Views: 5431

Rating: 4.2 / 5 (63 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Tyson Zemlak

Birthday: 1992-03-17

Address: Apt. 662 96191 Quigley Dam, Kubview, MA 42013

Phone: +441678032891

Job: Community-Services Orchestrator

Hobby: Coffee roasting, Calligraphy, Metalworking, Fashion, Vehicle restoration, Shopping, Photography

Introduction: My name is Tyson Zemlak, I am a excited, light, sparkling, super, open, fair, magnificent person who loves writing and wants to share my knowledge and understanding with you.