Menu

#115 Add support for /dev/gpiomem (rootless GPIO access)

Fixed
None
High
Patch
2016-01-03
2015-08-21
Luke Wren
No

Currently, libraries such as RPi.GPIO are accessing /dev/mem to get to the GPIO registers directly, which works very well but has a number of issues, not the least of them the fact that you have to run as root whenever you touch GPIO.

As a first step away from this, we're adding a device, /dev/gpiomem, which gives you access to the GPIO registers without root permission. It will always map your pointer to the correct location of the registers, so there is no need to parse /proc/devicetree to try and figure out where they are.

See this pull request on raspberrypi/linux for more details.

I've attached a patch for c_gpio.c which will first check for /dev/gpiomem and then fall back to requiring root privileges if it doesn't exist. /dev/gpiomem will appear in the next Raspbian release (or sooner via rpi-update), at which point RPi.GPIO and similar tools will no longer require root.

1 Attachments

Discussion

  • Ben Croston

    Ben Croston - 2015-08-24
    • status: New --> Accepted
     
  • Ben Croston

    Ben Croston - 2015-08-24

    This hopefully be part of the next release - ETA w/c 24/8/2015.

     
  • Luke Wren

    Luke Wren - 2015-08-24

    Awesome! Thanks.

     
  • Ben Croston

    Ben Croston - 2015-08-31
    • status: Accepted --> Started
     
  • Ben Croston

    Ben Croston - 2015-08-31

    Coded it ready for testing... waiting for the new kernel with /dev/gpiomem to compile!

     
  • Ben Croston

    Ben Croston - 2015-08-31

    Tested - passes unit test suite as root.

    Does not work as a normal user - file permissions on /dev/gpiomem don't look right:
    crw------- 1 root root 244, 0 Jan 1 1970 /dev/gpiomem

    Did a sudo chmod 666 /dev/gpiomem and ran unit test suite again.
    GPIO.setup / GPIO.input / GPIO.output work OK. Nothing works that uses /sys/class/gpio i.e. events.

    On my system, the gpio group doesn't exist (never has done). It is a very old installation of Raspbian that has just been dist-upgraded for the last couple of years. In other words, the gpio group and udev rules for /sys/class/gpio don't exist on my system. These used to be created by installing the pifacecommon raspbian package. See http://piface.github.io/pifacecommon/installation.html . I can't find out where or how it is set up now - it has now moved somewhere else! Has anyone any ideas?

     

    Last edit: Ben Croston 2015-08-31
    • Anonymous

      Anonymous - 2015-09-12

      I had no gpio group and wrong permission on my ArchLinux system. I solved by

      groupadd -r gpio
      

      and the following udev rules, which I copied from this thread post

      SUBSYSTEM=="bcm2835-gpiomem", KERNEL=="gpiomem", GROUP="gpio", MODE="0660"
      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'"
      

      Hope this helps.

       
  • Anonymous

    Anonymous - 2015-09-12

    After fixing permission, I'm still unable to use events: I suspect that this is a timing issue with the /sys/class/gpio udev rule. Permission are changed right after the relevant files (/sys/class/gpio/gpioXX/edge ...) are created, but too late for access by the python code.

     

    Last edit: Anonymous 2015-09-12
  • Ben Croston

    Ben Croston - 2015-09-13

    Have found the following in spindle (this is how it gets in the Foundation Raspbian image):
    https://github.com/RPi-Distro/spindle/blob/master/wheezy-stage3

    make_udev_com_rule() {
      onvm_chroot sh -l -e - <<EOF
    printf "SUBSYSTEM==\"gpio*\", PROGRAM=\"/bin/sh -c 'chown -R root:gpio /sys/class/gpio && chmod -R 770 /sys/class/gpio; chown -R root:gpio /sys/devices/virtual/gpio && chmod -R 770 /sys/devices/virtual/gpio'\"\n" > /etc/udev/rules.d/99-com.rules
    printf 'SUBSYSTEM=="i2c-dev", GROUP="i2c", MODE="0660"\n' >> /etc/udev/rules.d/99-com.rules
    printf 'SUBSYSTEM=="spidev", GROUP="spi", MODE="0660"\n' >> /etc/udev/rules.d/99-com.rules
    EOF
    }
    

    Edit: the /sys/class/gpio/gpioXX/* files do not have the correct owner/permissions with the above (default in latest Raspbian)

    Also found in spindle:

    groupadd -f -r spi
    groupadd -f -r i2c
    groupadd -f -r gpio
    for GRP in gpio spi i2c; do
      adduser pi $GRP
    done
    
     

    Last edit: Ben Croston 2015-09-13
  • Ben Croston

    Ben Croston - 2015-09-13

    Created 0.6.0a2 - should work around the timing issues discovered by Stefano Miccoli above.

    Have also added create_gpio_user_permissions.py that creates the gpio group and udev rules that work (thanks again to Stefano Miccoli). Note that the udev settings in the Foundation Raspbian Wheezy do not work properly. I will patch spindle an create a pull request. In the meantime, I will enhance create_gpio_user_permissions.py so that it detects and replaces incorrect udev settings. Should this be run automatically on installation (by pip and apt-get)?

     

    Last edit: Ben Croston 2015-09-13
    • Anonymous

      Anonymous - 2015-09-14

      While create_gpio_user_permissions.py is a good idea for helping inexperienced users and therefore could be included in the distribution by a

      scripts = ['create_gpio_user_permissions.py']
      

      clause in setup.py, I think that it should not be run automatically at install time.

      With pip usally one installs in an isolated virtual environment (created via virtualenv or pyvenv), therefore pip should not tamper with system files, or require superuser privileges for running.

      As what regards Debian, the gpio group should be created by the postinst script . You can run

      grep -El 'groupadd|addgroup' /var/lib/dpkg/info/*postinst
      

      to have a list of examples.

      The /etc/udev/rules.d/99-gpio.rules should be handled by dpkg via the conffiles control file. See also https://www.debian.org/doc/debian-policy/ch-files.html#s-config-files

       

      Last edit: Anonymous 2015-09-14
  • Randy Marsh

    Randy Marsh - 2015-09-22

    Any update on when this new version will be released?

     
  • Ben Croston

    Ben Croston - 2015-09-24

    Raspbian Jessie .deb packages of 0.6.0a3 have been produced/tested and will hopefully be in the next Foundation Raspbian release (due out shortly I believe). For other Linux distributions, testing is still ongoing.

     
  • Ben Croston

    Ben Croston - 2016-01-03
    • status: Started --> Fixed
     
  • Ben Croston

    Ben Croston - 2016-01-03

    Release 0.6.1 has been made for Jessie and Wheezy. You do not need sudo under Jessie.

    In Wheezy, you do not need sudo once user groups and udev settings have been modified. At the moment this has to be done manually.

    Other distributions will need the gpio group, udev settings and correct kernel that provides /dev/gpiomem adding for sudoless operation.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.