Python GPIO access without 'sudo' on Linux

Hello,

I’ve started to use the Python bindings from the libsoc library on the Dragonboard 410c.

The one issue that I have is that the Python scripts need to be run as root otherwise they don’t have access to the GPIO.

There are other Jessie distributions that no longer requires sudo to run programs that access GPIO . Does anyone know how we could do that with the Linux distribution for the Dragonboard?

Regards,
Barry

Raspberry pi does it by adding a udev rule in /etc/udev/rules.d (and a adding group called “gpio” for users to belong to):
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; chown -R root:gpio /sys/devices/platform/soc/.gpio/gpio && chmod -R 770 /sys/devices/platform/soc/.gpio/gpio’"

It does looks like a bit of a hack (and won’t work with distros that use TAG+=“uaccess” rather than groups to manage permissions) but it will probably do the trick.

Thanks for the answer. A couple of us have tried this independently and we haven’t been able to get it working. As you can see from the session information below not all files are created in the ‘gpio’ group.

I’m not sure just following what they do on the Raspberry Pi is the solution from the look of the comment in item number 2 where it says “It’s also root only (doesn’t know about /dev/gpiomem)” they may have done a little bit more.

Any other ideas or suggestions?

Thanks.


cd /sys/class/gpio/
linaro@linaro-alip:/sys/class/gpio$ ls -lL
total 0
-rwxrwx--- 1 root gpio 4096 Apr 12 22:45 export
drwxr-xr-x 3 root root    0 Jan  1  1970 gpiochip0
drwxr-xr-x 3 root root    0 Jan  1  1970 gpiochip440
drwxr-xr-x 3 root root    0 Jan  1  1970 gpiochip444
drwxr-xr-x 3 root root    0 Jan  1  1970 gpiochip448
drwxr-xr-x 3 root root    0 Jan  1  1970 gpiochip480
-rwxrwx--- 1 root gpio 4096 Apr 12 22:45 unexport
linaro@linaro-alip:/sys/class/gpio$ echo 115 > export
linaro@linaro-alip:/sys/class/gpio$ ls -lL
total 0
-rwxrwx--- 1 root gpio 4096 Apr 12 22:47 export
drwxr-xr-x 3 root root    0 Apr 12 22:47 gpio115
drwxr-xr-x 3 root root    0 Apr 12 22:47 gpiochip0
drwxr-xr-x 3 root root    0 Apr 12 22:47 gpiochip440
drwxr-xr-x 3 root root    0 Apr 12 22:47 gpiochip444
drwxr-xr-x 3 root root    0 Apr 12 22:47 gpiochip448
drwxr-xr-x 3 root root    0 Apr 12 22:47 gpiochip480
-rwxrwx--- 1 root gpio 4096 Apr 12 22:45 unexport
linaro@linaro-alip:/sys/class/gpio$ ls -lL gpio115
total 0
-rw-r--r-- 1 root root 4096 Apr 12 22:49 active_low
drwxr-xr-x 4 root root    0 Jan  1  1970 device
-rw-r--r-- 1 root root 4096 Apr 12 22:49 direction
-rw-r--r-- 1 root root 4096 Apr 12 22:49 edge
drwxr-xr-x 2 root root    0 Apr 12 22:49 power
drwxrwx--- 2 root gpio    0 Apr 12 22:47 subsystem
-rw-r--r-- 1 root root 4096 Apr 12 22:47 uevent
-rw-r--r-- 1 root root 4096 Apr 12 22:49 value
linaro@linaro-alip:~$ groups
linaro adm dialout cdrom sudo audio dip video plugdev staff systemd-journal netdev bluetooth pulse-access gpio
linaro@linaro-alip:~$ more /etc/udev/rules.d/99-com.rules 
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; chown -R root:gpio /sys/devices/platform/soc/*.gpio/gpio && chmod -R 770 /sys/devices/platform/soc/*.
gpio/gpio'"