Another GPIO permissions issue

Hi, I’m trying to access gpio using libgpiod on TX2 with dev kit. I have an application that works fine running with sudo. If I run without I get an access error. My user is called tx2. It seems like the udev rules are not firing during boot as the permissions for gpio stay with root:root whereas they should be root:gpio? Any ideas why this is not working or how to fix?

Thanks in advance,
Felix

Relevant terminal output below:

$ cat /etc/nv_tegra_release
# R32 (release), REVISION: 7.5, GCID: 36557527, BOARD: t186ref, EABI: aarch64, DATE: Tue Jun 11 23:18:04 UTC 2024

$ ls -l /dev/gpio*
crw------- 1 root root 254, 0 Nov 12 10:00 /dev/gpiochip0
crw------- 1 root root 254, 1 Nov 12 10:00 /dev/gpiochip1
crw------- 1 root root 254, 2 Nov 12 10:00 /dev/gpiochip2
crw------- 1 root root 254, 3 Nov 12 10:00 /dev/gpiochip3
crw------- 1 root root 254, 4 Nov 12 10:00 /dev/gpiochip4

$ ls -l /sys/class/gpio
total 0
--w--w---- 1 root gpio 4096 Nov 12 10:00 export
lrwxrwxrwx 1 root root    0 Nov 12 10:00 gpiochip216 -> ../../devices/bpmp_i2c/i2c-4/4-003c/max77620-gpio/gpio/gpiochip216
lrwxrwxrwx 1 root root    0 Nov 12 10:00 gpiochip224 -> ../../devices/3160000.i2c/i2c-0/0-0077/gpio/gpiochip224
lrwxrwxrwx 1 root root    0 Nov 12 10:00 gpiochip240 -> ../../devices/3160000.i2c/i2c-0/0-0074/gpio/gpiochip240
lrwxrwxrwx 1 root root    0 Nov 12 10:00 gpiochip256 -> ../../devices/c2f0000.gpio/gpio/gpiochip256
lrwxrwxrwx 1 root root    0 Nov 12 10:00 gpiochip320 -> ../../devices/2200000.gpio/gpio/gpiochip320
--w--w---- 1 root gpio 4096 Nov 12 10:00 unexport

$ ls /lib/udev/rules.d/*gpio*
/lib/udev/rules.d/60-jetson-gpio-common.rules

$ cat  /lib/udev/rules.d/60-jetson-gpio-common.rules
# Copyright (c) 2012-2017 Ben Croston <ben@croston.org>.
# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

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", DEVPATH=="/*/gpiochip*/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'"

SUBSYSTEM=="pwm", KERNEL=="pwmchip*", ACTION=="add", \
    PROGRAM="/bin/bash -c 'chown root:gpio /sys%p/{,un}export; chmod 220 /sys%p/{,un}export'"
SUBSYSTEM=="pwm", KERNEL=="pwmchip*", ACTION=="change", \
    PROGRAM="/bin/bash -c 'chown root:gpio /sys%p/%E{EXPORT}/{period,duty_cycle,enable}; chmod 660 /sys%p/%E{EXPORT}/{period,duty_cycle,enable}'"

SUBSYSTEM=="spidev", KERNEL=="spidev*", ACTION=="add", \
    GROUP="gpio", MODE="0660"

$ groups tx2
tx2 : tx2 adm dialout cdrom sudo audio dip video plugdev i2c lpadmin gdm lightdm gpio weston-launch sambashare gpiod

I also tried appending

SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", \
    PROGRAM="/bin/sh -c 'chown root:gpio /dev/%k; chmod 660 /dev/%k'"

to the end of the 60-jetson-gpio-common.rules file but that did not help.

I tried manual
$ sudo chown root:gpio /dev/gpiochip0
in the terminal and that fixes my issue with the application access, but is obviously not persisted through a reboot. I just need to get the udev recipe right. To be honest I’m a bit surprised that this does not work out of the box.

Hi,
Please refer to the posts to run some commands in startup:

Jetson Nano FAQ
Q: How to autorun an app or commands at startup?

Just in case someone else is looking at this topic…

I ran:
$journalctl -b
and found:
Mar 03 01:58:02 sampextx2 systemd-udevd[2856]: Invalid rule /lib/udev/rules.d/60-jetson-gpio-common.rules:36: unknown key 'KERNAL'

So that was bad spelling. Sigh… another day another bit of knowledge acquired.

I added a UDEV rule as follows:

SUBSYSTEM=="gpio", KERNEL=="gpiochip320", ACTION=="add",  PROGRAM="/bin/sh -c 'chown root:gpio /dev/gpiochip0; chmod 660 /dev/gpiochip0'"

This works. It gives gpio group members access to gpio320 aka gpio0 on the J21 (on the dev kit carrier).
If you use other gpio chips you may need to add them too. I’m not sure how to get the udev to automatically map from 320 to 0. Anyone know that detail?

BTW: I think this is safer than using systemd, as the UDEV rules will be processed earlier during the boot. This matters for my case as I will be using systemd to run my app as a service.

1 Like

Is there any Linux experts here on the forum who can tell me why my rule sometimes works and sometimes does not?

I thought I had it working but then this morning I booted up the machine and the GPIO would not work - Note that gpiochip0 is owned by root group, not gpio group.

$ ls -l /dev/gpiochip*
crw------- 1 root root 254, 0 Nov 14 15:29 /dev/gpiochip0
crw------- 1 root root 254, 1 Nov 14 15:29 /dev/gpiochip1
crw------- 1 root root 254, 2 Nov 14 15:29 /dev/gpiochip2
crw------- 1 root root 254, 3 Nov 14 15:29 /dev/gpiochip3
crw------- 1 root root 254, 4 Nov 14 15:29 /dev/gpiochip4

It seems like the UDEV rule did not run. I shutdown the machine and powered it up again and this time it works…

~$ ls -l /dev/gpiochip*
crw-rw---- 1 root gpio 254, 0 Nov 18 10:30 /dev/gpiochip0
crw------- 1 root root 254, 1 Nov 18 10:30 /dev/gpiochip1
crw------- 1 root root 254, 2 Nov 18 10:30 /dev/gpiochip2
crw------- 1 root root 254, 3 Nov 18 10:30 /dev/gpiochip3
crw------- 1 root root 254, 4 Nov 18 10:30 /dev/gpiochip4

Perhaps there is a race condition between adding /sys/class/gpio320 and then creating the /dev/gpiochip0? Does anyone know how to create a rule that fires when /dev/gpiochip0 is created.
I tried

SUBSYSTEM=="gpio", KERNEL=="gpiochip0", ACTION=="add",  PROGRAM="/bin/sh -c 'chown root:gpio /dev/gpiochip0; chmod 660 /dev/gpiochip0'"

But this does not seem to work.

Can anyone suggest a reliable way to get gpio working with the gpio group for access?

I’ve marked the solution again. It is possible this was my mistake as I’ve only seen it once.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.