Rootless GPIO access: /dev/gpiomem

/dev/gpiomem provides user with root-free access to the GPIO registers. Opening /dev/gpiomem device file and calling mmap() function will map the GPIO registers' memory map area to the user's pointer.
This driver is available in below's Ubuntu kernel versions:

  • ODROID-C2: 3.14.79-105 kernel version or later
  • ODROID-C0/C1+: 3.10.104-182 kernel version or later
  • ODROID-XU4: 4.9.x kernel version or later

How to enable gpiomem driver

/dev/gpiomem driver is included as module by default. In order to use gpiomem driver, it is necessary to configure account access permission.

$ sudo addgroup gpio
$ sudo usermod -a -G gpio odroid    
$ cat /etc/udev/rules.d/90-gpiomem.rules 
  • ODROID-C0/C1+/C2
SUBSYSTEM=="meson-gpiomem", GROUP="gpio", MODE="0660"
  • ODROID-XU4
SUBSYSTEM=="exynos-gpiomem", GROUP="gpio", MODE="0660"

Example

Hardkernel's wiringPi library which is for GPIO control uses /dev/gpiomem device file for rootless gpio control. Usage is very simple. Just open /dev/gpiomem device file instead of /dev/mem.

  // Open the master /dev/memory device
  if (access("/dev/gpiomem",0) == 0) {
    if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
      return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/gpiomem: %s\n", strerror (errno)) ;
  }
  else {
    if (geteuid () != 0)
      (void)wiringPiFailure (WPI_FATAL, "wiringPiSetup: Must be root. (Did you forget sudo?)\n") ;

    if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
      return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
  }

Rootless GPIO SYS-filesystem: /sys/class/gpio

$ sudo addgroup gpio
$ sudo usermod -a -G gpio odroid    
$ cat /etc/udev/rules.d/90-odroid-sysfs.rules 
SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:gpio /sys/class/gpio/export /sys/class/gpio/unexport ; chmod 220 /sys/class/gpio/export /sys/class/gpio/unexport'"
SUBSYSTEM=="gpio", KERNEL=="gpio*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:gpio /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value ; chmod 660 /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value'"
en/gpiomem.txt · Last modified: 2017/02/03 15:21 by brian.kim
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0