How to use configfs

From RidgeRun Developer Wiki


Configuration file system introduction

configfs is a ram-based filesystem that provides the converse of sysfs's functionality. Where sysfs is a filesystem-based view of kernel objects, configfs is a filesystem-based manager of kernel objects, or config_items.

Both sysfs and configfs can and should exist together on the same system. One is not a replacement for the other.

Enabling configfs

Symbol: CONFIGFS_FS [=n]
Prompt: Userspace-driven configuration filesystem
  Defined at fs/configfs/Kconfig:1
  Depends on: SYSFS
  Location:
    -> Kernel configuration
      -> File systems
        -> Pseudo filesystems                                                                              
  Selected by: NETCONSOLE_DYNAMIC && NETDEVICES && NETCONSOLE && SYSFS && EXPERIMENTAL || DLM && EXPERIMENT

PWM - example driver that uses the configuration file system

A driver is needed that uses configfs, so the Bill Gatliff PWM driver will be used as the example. If your kernel doesn't have the PWM driver, you can get it from one Bill's PWM Linux git repository.

Symbol: GPIO_PWM [=y]
Prompt: GPIO+hrtimer PWM device emulation
   Defined at drivers/pwm/Kconfig:11
   Depends on: GENERIC_PWM
   Location:
     -> Kernel configuration
       -> Device Drivers
         -> PWM Support (GENERIC_PWM [=y])

Mounting the configuration file system

First, verify your kernel has support for configfs.

fgrep configfs /proc/filesystems

with the expected output being:

nodev	configfs

Mount the configuration file system at the conventional location:

mkdir -p /config
mount -t configfs none /config

Accessing the configuration file system

You can verify the PWM entries are present in the configuration file system:

find /config

with expected output:

/config
/config/gpio_pwm

Using PWM via the configuration file system

The PWM driver will be used as the example of how to use the configuration file system

GPIO=102

# create configurable GPIO PWM
mkdir /config/gpio_pwm/$GPIO
echo 1 > /sys/class/pwm/gpio_pwm\:$GPIO/export

# configure for 10 ms with a 50% duty cycle - simple 100 hz square wave
echo 10000000 > /sys/class/pwm/gpio_pwm\:$GPIO/period_ns
echo 5000000 > /sys/class/pwm/gpio_pwm\:$GPIO/duty_ns

# start the PWM
echo 1 > /sys/class/pwm/gpio_pwm\:$GPIO/run

# stop the PWM
echo 0 > /sys/class/pwm/gpio_pwm\:$GPIO/run

# destroy the configurable GPIO PWM
echo 1 > /sys/class/pwm/gpio_pwm\:$GPIO/unexport
rm -rf /config/gpio_pwm/$GPIO