Libiio-dev permission errors while creating buffer for ads1015 from container

I am using a verdin imx8mm on a yavia carrier board.

In my application, I am trying to create an iio_buffer but it is giving permission errors.

I have tried using privileged mode, used cap_add ALL and given direct access to /sys/bus/iio/devices folder.

reading raw channels and enabling channels works fine from inside the application. The sensor I am working with is an ads1015 which is built in.

The code is as follows for general reference:

  // Example adjustment for debugging
  for (auto& d : devices) {
    if (iio_device_is_trigger(d.trigPtr) < 0) {
      SPDLOG_ERROR("Device {} does not support trigger operations", d.name);
    } else {
      SPDLOG_INFO("Device {} supports trigger operations", d.name);
    }

    size_t sample_size = iio_device_get_sample_size(d.ptr);
    size_t num_samples = 10;  // Example: buffer 10 samples
    size_t buffer_size = sample_size * num_samples;

    SPDLOG_INFO("Attempting to create a buffer of {} bytes for device {}", buffer_size, d.name);

    iio_buffer* bufPtr = iio_device_create_buffer(d.ptr, num_samples, true);
    if (!bufPtr) {
      SPDLOG_ERROR("Failed to create IIO buffer for device {}. Sample size: {}, Buffer size: {}", d.name,
                   sample_size, buffer_size);
      SPDLOG_ERROR("Error: {}", strerror(errno));
      continue;
    }
    device_buffers[d.ptr] = bufPtr;
  }

trying from ssh:

torizon@verdin-imx8mm-14756428:/sys/bus/iio/devices/iio:device0/buffer0$ cat length
240
torizon@verdin-imx8mm-14756428:/sys/bus/iio/devices/iio:device0/buffer0$ echo 1 > enable
-sh: enable: Permission denied
torizon@verdin-imx8mm-14756428:/sys/bus/iio/devices/iio:device0/buffer0$ sudo echo 1 > enable
-sh: enable: Permission denied

trying iio_info from container built with iio_utils:

root@d39b042be0d1:/# iio_info
Library version: 0.24 (git tag: v0.24)
Compiled with backends: local xml ip usb
IIO context created with local backend.
Backend version: 0.24 (git tag: v0.24)
Backend description string: Linux d39b042be0d1 5.15.129-6.5.0-devel+git.6f8fd49366db #1-TorizonCore SMP PREEMPT Fri Dec 22 11:15:52 UTC 2023 aarch64
IIO context has 2 attributes:
        local,kernel: 5.15.129-6.5.0-devel+git.6f8fd49366db
        uri: local:
IIO context has 1 devices:
        iio:device0: ads1015 (buffer capable)
                9 channels found:
                        voltage0-voltage1:  (input, index: 0, format: le:s12/16>>4)
                        3 channel-specific attributes found:
                                attr  0: raw value: 0
                                attr  1: sampling_frequency value: 1600
                                attr  2: scale value: 1.000000000
                        voltage0-voltage3:  (input, index: 1, format: le:s12/16>>4)
                        3 channel-specific attributes found:
                                attr  0: raw value: 0
                                attr  1: sampling_frequency value: 1600
                                attr  2: scale value: 1.000000000
                        voltage1-voltage3:  (input, index: 2, format: le:s12/16>>4)
                        3 channel-specific attributes found:
                                attr  0: raw value: 0
                                attr  1: sampling_frequency value: 1600
                                attr  2: scale value: 1.000000000
                        voltage2-voltage3:  (input, index: 3, format: le:s12/16>>4)
                        3 channel-specific attributes found:
                                attr  0: raw value: 0
                                attr  1: sampling_frequency value: 1600
                                attr  2: scale value: 1.000000000
                        voltage0:  (input, index: 4, format: le:s12/16>>4)
                        3 channel-specific attributes found:
                                attr  0: raw value: 340
                                attr  1: sampling_frequency value: 1600
                                attr  2: scale value: 1.000000000
                        voltage1:  (input, index: 5, format: le:s12/16>>4)
                        3 channel-specific attributes found:
                                attr  0: raw value: 291
                                attr  1: sampling_frequency value: 1600
                                attr  2: scale value: 1.000000000
                        voltage2:  (input, index: 6, format: le:s12/16>>4)
                        3 channel-specific attributes found:
                                attr  0: raw value: 290
                                attr  1: sampling_frequency value: 1600
                                attr  2: scale value: 1.000000000
                        voltage3:  (input, index: 7, format: le:s12/16>>4)
                        3 channel-specific attributes found:
                                attr  0: raw value: 290
                                attr  1: sampling_frequency value: 1600
                                attr  2: scale value: 1.000000000
                        timestamp:  (input, index: 8, format: le:S64/64>>0)
                3 device-specific attributes found:
                                attr  0: current_timestamp_clock value: realtime

                                attr  1: sampling_frequency_available value: 128 250 490 920 1600 2400 3300
                                attr  2: scale_available value: 3 2 1 0.5 0.25 0.125
                1 buffer-specific attributes found:
                                attr  0: data_available value: 0
ERROR: checking for trigger : Input/output error (5)

Any pointers would be welcome.

Best Regards.

Greetings @geopaxpvtltd,

Have you tried writing the buffer file descriptor as the root user? I mean as the actual root user not just sudo.

Another idea is maybe you have to create a udev rule to change the permissions here similar to this forum post: What permissions required for non-root access to libiio buffers? - Q&A - Linux Software Drivers - EngineerZone

We actually had to do something similar with PWM which is also accessed via /sys. See the udev rule we use for PWM here: meta-toradex-torizon/recipes-core/udev/files/94-toradex-pwm.rules at kirkstone-6.x.y · torizon/meta-toradex-torizon · GitHub

This rule adds the PWM file descriptors to the “pwm” group. This means any user that is in the “pwm” group can then access these file descriptors as well.

Best Regards,
Jeremias

Hi @jeremias.tx ,

Thank you for the pointers. I am not that familiar with linux and I tried with the following rule using a friend known as chatgpt:

# Set permissions for IIO sysfs devices
SUBSYSTEM=="iio", KERNEL=="iio:device*", ACTION=="add", PROGRAM="/bin/sh -c 'chmod -R 777 /sys/bus/iio/devices/$kernel'"

This showed some progress as setting a trigger was not working before and it started working. But buffer creation is still giving permission denied error. all the permissions look correct to me. maybe I am still missing something?

Pl note the container runs in privileged mode and all devices are mounted and cap_add is set to all.

UPDATE: adding another rule like follows takes care of the permission error:

# Set group ownership and permissions for IIO devices
SUBSYSTEM=="iio", KERNEL=="iio:device*", MODE="0777"
SUBSYSTEM=="iio", KERNEL=="iio:device*", ACTION=="add", PROGRAM="/bin/sh -c 'chmod -R 777 /sys/bus/iio/devices/$kernel'"

but it introduces another error while buffer creation. argument error.

are the interrupts set up for the builtin ads1015 sensor? asking in lieu of this:

In that link you posted they get a “Unable to allocate buffer: Invalid argument” error. Is this the exact error message you get? Or do you get “write error: Invalid argument”?

I ask cause when I tried to enable the buffer myself via sysfs I get the latter error, which implies something different. A “write error” implies whatever logic is used to process the writing of this file failed. For example if you try to write an integer to buffer/length then it’s fine. But if you try to write a string of letters then you also get a “write error”.

If I write “0” to buffer/enable I get no error strangely enough. But writing “1” results in a “write error” as it seems unable to process this for some reason.

That said, that’s as far as my investigation has gotten so far. I’m not too familiar with this driver so I’m not sure where the writing logic for this sysfs file is located. That is assuming the logic exists somewhere.

Best Regards,
Jeremias

I get write error: invalid argument

Yes that is happening with me as well. but all the examples I have seen, they just write 1 to the enable to enable the buffer.

I am seeing the same thing.

What course of action do you advise?

Best Regards.

I did some more research into the ads1015 driver. It seems you must setup a trigger prior to enabling the buffer. I suppose the device/driver only supports triggered buffers. To setup a trigger you can see the solution on this other thread: Colibri imx6ULL and Iris Carrier - ADC problems - #2 by daniel_m.tx

On my device once I setup a trigger I was able to enable the buffer via sysfs:

root@verdin-imx8mm-06827778:~# cat /sys/devices/platform/soc@0/soc@0\:bus@30800000/30a20000.i2c/i2c-0/0-0049/iio\:device0/buffer/enable
1

Best Regards,
Jeremias

hi @geopaxpvtltd ,

could the suggestion from @jeremias.tx help you to get a working solution?

Hi @andi.tx and @jeremias.tx,

Apologies for not responding earlier. I had some other commitments that needed addressing.

Indeed creating a trigger manually and enabling buffer works but as you can see from the thread you linked, they were still unable to find a solution which seems to be my exact use case.

Please note I am just working with this sensor as an example at the moment until I get my hands on a prototype custom carrier board.

Best Regards,

Indeed creating a trigger manually and enabling buffer works but as you can see from the thread you linked, they were still unable to find a solution which seems to be my exact use case.

I’m confused what is your issue exactly? You originally stated you could not create a buffer. We have been able to achieve this now. So what’s the issue now? In the other thread I linked after the buffer issue was resolved that customer had issues with multi-channel conversion which is not supported by the driver.

Best Regards,
Jeremias

Hi @jeremias.tx!

I apologize for the confusion.

My end goal is to work with an IMU using Libiio. Currently, I am creating a mechanism to read different channels from a single buffer.

As I do not have the carrier board with the IMU right now, I was working with the builtin ads1015 and trying to create a buffer for multiple channels at once; which as you have linked in the other thread, appears to be a hardware issue as ads1015 does not support multi channel adc conversions.

So my original question regarding creation of a buffer appears to be resolved albeit with the hardware limitation of ads1015. I will have to check if this approach works with the other sensor when it arrives. The IMU might not have this limitation (The multiple channel adc conversion limitation).

I see, thank you for clarifying. Glad I was able to help as much as I could.

1 Like