4

I was making (my first) kernel module to play with the gpio pins of my pandaboard and interrupts.

Already "built-in", I noticed you can do (briefly)

cd /sys/class/gpio
echo 138 > export  # a file gpio138 appears    
echo out > gpio138/direction
echo 1 > gpio138/value

to turn some voltage high or low on connector pins.

Similarly on a kernel module you have module parameters and you can do echo 3 > /sys/module/my_module/parameters/delay_seconds for instance

My questions :

  1. What handles /sys/class/gpio ? A kernel module ? a driver ?

  2. Whatever 1. is, is it possible to have more complicated module parameters in a kernel module, with some directory structure ? Like a 'delays' directory containing the params for delays, ... /sys/module/my_module/parameters/delays/delay_led1_seconds

  3. Can you have the parameters somewhere else than in the path /sys/module/my_module/parameters/... ? (/sys/class/a_name/... for instance)

  4. How does the gpio thing creates new/deletes files in /sys/class/gpio when you write to [un]export ?

  • I believe echo 138 > /export was a typo -- it should have been echo 138 > export, so I changed that. – goldilocks Dec 27 '13 at 13:41
2

1) What handles /sys/class/gpio ? A kernel module ? a driver ?

It's a kernel interface similar to the /proc directory.

2) is it possible to have more complicated module parameters in a kernel module, with some directory structure ? Like a 'delays' directory containing the params for delays

Yes; some things in /proc and /sys do use directory hierarchies. If you want to modify or expand them, though, you have to modify the kernel.

#3 has a similar answer -- to make changes you need to change the relevant kernel code.

4) How does the gpio thing creates new/deletes files in /sys/class/gpio when you write to [un]export ?

These are not files on disk, they're just system interfaces.1 When you go to read data from a procfs or sysfs file, what you are really doing is making a request for information from the kernel. The data is then formatted and returned. It probably isn't stored anywhere in the form you see it, although parts of it may be stored in the kernel.

When you write to such a file -- not all of them allow this -- you're sending a request to the kernel to do something specific. This can include, e.g., activating or expanding the GPIO interface.


1. read and write calls are always system calls anyway, since normal files are normally on disk, and the kernel is needed to access hardware. Hence using a filesystem style API here is natural; even if they are not "real files", accessing whatever resource they represent must involve system calls.

1
  1. Can you have the parameters somewhere else than in the path /sys/module/my_module/parameters/... ? (/sys/class/a_name/... for instance)

I'd use /sys/class/module_name/* for parameters, it's really easy to use it. See code in http://lxr.free-electrons.com/source/drivers/gpio/gpiolib.c#L757 as example (there is also example for read only attribute __ATTR_RO).

static struct class_attribute gpio_class_attrs[] = {
        __ATTR(export, 0200, NULL, export_store),
        __ATTR(unexport, 0200, NULL, unexport_store),
        __ATTR_NULL,
};

static struct class gpio_class = {
        .name =         "gpio",
        .owner =        THIS_MODULE,

        .class_attrs =  gpio_class_attrs,
};

/*
 * /sys/class/gpio/export ... write-only
 *      integer N ... number of GPIO to export (full access)
 * /sys/class/gpio/unexport ... write-only
 *      integer N ... number of GPIO to unexport
 */
static ssize_t export_store(struct class *class,
                                struct class_attribute *attr,
                                const char *buf, size_t len)
{
    // ...

Btw. I recommend you to read paper The sysfs Filesystem by Patrick Mochel and short blog post by Greg Kroah-Hartman How to Create a sysfs File Correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.