Table of Contents

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 added in below's Ubuntu kernel versions:

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 
SUBSYSTEM=="meson-gpiomem", GROUP="gpio", MODE="0660"
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)) ;
  }