The usb-gadget interface

The usb-gadget interface permits snaps to access the USB Gadget API using configfs.

USB gadget is a feature of the Linux kernel that allows USB connected devices to identify as USB peripherals, such as USB keyboards or storage devices. They are unrelated to Ubuntu Core gadget snaps.

The usb-gadget interface requires snapd version 2.71+. Prior to snapd 2.72, function filesystem devices need mount-control and system-files interfaces to provide the necessary permissions.

Developer details

Auto-connect: no

The USB Gadget API, through configfs, can be loaded through libcomposite which then provides /sys/kernel/config/usb_gadget/ . This can then be used to create and discover new USB gadgets. This interface allows snaps to read and write to those paths.

USB gadgets using the function filesystem can use a definition based on the following:

  usb-gadget:
    interface: usb-gadget
    ffs-mounts:
      - name: ffs-dev0
        where: /dev/ffs-dev0
        persistent: {true,false}

Code examples

The test code can be found in the snapd repository:
snapd/interfaces/builtin/usb_gadget_test.go at master · canonical/snapd · GitHub

The source code for the interface is in the snapd repository:
snapd/interfaces/builtin/usb_gadget.go at master · canonical/snapd · GitHub

@degville from snapd 2.72+ it’s now possible to specify function-fs devices that should be supported by the usb-gadget. Prior to 2.72 you would need mount-control and system-files to get the needed permissions to use function-fs devices.

The syntax for the new usb-gadget interface is thus:

  usb-gadget:
    interface: usb-gadget
    ffs-mounts:
      - name: ffs-dev0
        where: /dev/ffs-dev0
        persistent: {true,false}

This will add the neccessary permissions to mount a ffs device on the system.

Updates - thanks @pmeulengracht!