History log of /linux-master/tools/iio/iio_utils.c
Revision Date Author Comments
# 4e6500bf 07-Jan-2024 Petre Rodan <petre.rodan@subdimension.ro>

tools: iio: replace seekdir() in iio_generic_buffer

Replace seekdir() with rewinddir() in order to fix a localized glibc bug.

One of the glibc patches that stable Gentoo is using causes an improper
directory stream positioning bug on 32bit arm. That in turn ends up as a
floating point exception in iio_generic_buffer.

The attached patch provides a fix by using an equivalent function which
should not cause trouble for other distros and is easier to reason about
in general as it obviously always goes back to to the start.

https://sourceware.org/bugzilla/show_bug.cgi?id=31212

Signed-off-by: Petre Rodan <petre.rodan@subdimension.ro>
Link: https://lore.kernel.org/r/20240108103224.3986-1-petre.rodan@subdimension.ro
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>


# f2edf0c8 16-Jan-2023 Yulong Zhang <yulong.zhang@metoak.net>

tools/iio/iio_utils:fix memory leak

1. fopen sysfs without fclose.
2. asprintf filename without free.
3. if asprintf return error,do not need to free the buffer.

Signed-off-by: Yulong Zhang <yulong.zhang@metoak.net>
Link: https://lore.kernel.org/r/20230117025147.69890-1-yulong.zhang@metoak.net
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>


# 72b2aa38 13-Oct-2022 Matti Vaittinen <mazziesaccount@gmail.com>

tools: iio: iio_utils: fix digit calculation

The iio_utils uses a digit calculation in order to know length of the
file name containing a buffer number. The digit calculation does not
work for number 0.

This leads to allocation of one character too small buffer for the
file-name when file name contains value '0'. (Eg. buffer0).

Fix digit calculation by returning one digit to be present for number
'0'.

Fixes: 096f9b862e60 ("tools:iio:iio_utils: implement digit calculation")
Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
Link: https://lore.kernel.org/r/Y0f+tKCz+ZAIoroQ@dc75zzyyyyyyyyyyyyycy-3.rev.dnainternet.fi
Cc: <Stable@vger.kernel.org>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>


# 8827faab 14-Feb-2021 Alexandru Ardelean <alexandru.ardelean@analog.com>

tools: iio: convert iio_generic_buffer to use new IIO buffer API

This change makes use of the new IIO buffer API to read data from an IIO
buffer.
It doesn't read the /sys/bus/iio/devices/iio:deviceX/scan_elements dir
anymore, it reads /sys/bus/iio/devices/iio:deviceX/bufferY, where all the
scan_elements have been merged together with the old/classical buffer
attributes.

And it makes use of the new IIO_BUFFER_GET_FD_IOCTL ioctl to get an FD for
the IIO buffer for which to read data from.
It also does a quick sanity check to see that -EBUSY is returned if reading
the chardev after the ioctl() has succeeded.

This was tested with the following cases:
1. Tested buffer0 works with ioctl()
2. Tested that buffer0 can't be opened via /dev/iio:deviceX after ioctl()
This check should be omitted under normal operation; it's being done
here to check that the driver change is sane
3. Moved valid buffer0 to be buffer1, and tested that data comes from it

Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
Link: https://lore.kernel.org/r/20210215104043.91251-25-alexandru.ardelean@analog.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>


# a605c8f4 14-Feb-2021 Alexandru Ardelean <alexandru.ardelean@analog.com>

tools: iio: make iioutils_get_type() private in iio_utils

This is a bit of a tidy-up, but also helps with extending the
iioutils_get_type() function a bit, as we don't need to use it outside of
the iio_utils.c file. So, we'll need to update it only in one place.

With this change, the 'unsigned' types are updated to 'unsigned int' in the
iioutils_get_type() function definition.

Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
Link: https://lore.kernel.org/r/20210215104043.91251-23-alexandru.ardelean@analog.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>


# 208a68c8 27-Jun-2019 Bastien Nocera <hadess@hadess.net>

iio: iio-utils: Fix possible incorrect mask calculation

On some machines, iio-sensor-proxy was returning all 0's for IIO sensor
values. It turns out that the bits_used for this sensor is 32, which makes
the mask calculation:

*mask = (1 << 32) - 1;

If the compiler interprets the 1 literals as 32-bit ints, it generates
undefined behavior depending on compiler version and optimization level.
On my system, it optimizes out the shift, so the mask value becomes

*mask = (1) - 1;

With a mask value of 0, iio-sensor-proxy will always return 0 for every axis.

Avoid incorrect 0 values caused by compiler optimization.

See original fix by Brett Dutro <brett.dutro@gmail.com> in
iio-sensor-proxy:
https://github.com/hadess/iio-sensor-proxy/commit/9615ceac7c134d838660e209726cd86aa2064fd3

Signed-off-by: Bastien Nocera <hadess@hadess.net>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>


# d2912cb1 04-Jun-2019 Thomas Gleixner <tglx@linutronix.de>

treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500

Based on 2 normalized pattern(s):

this program is free software you can redistribute it and or modify
it under the terms of the gnu general public license version 2 as
published by the free software foundation

this program is free software you can redistribute it and or modify
it under the terms of the gnu general public license version 2 as
published by the free software foundation #

extracted by the scancode license scanner the SPDX license identifier

GPL-2.0-only

has been chosen to replace the boilerplate/reference in 4122 file(s).

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Enrico Weigelt <info@metux.net>
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Allison Randal <allison@lohutok.net>
Cc: linux-spdx@vger.kernel.org
Link: https://lkml.kernel.org/r/20190604081206.933168790@linutronix.de
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>


# 6356f1b9 22-Aug-2016 Matt Ranostay <mranostay@gmail.com>

iio: iio-utils: use channel modifier scaling if it exists

Now there are channel modifiers with their own scaling those should be
used when possible over the generic channel type scaling.

Examples are of IIO_TEMP channel having a generic scaling value, and
another having IIO_MOD_TEMP_AMBIENT modifier with another scaling value.

Previously the first scaling value for a channel type would be applied
to all channels of like type in iio_generic_buffer

Signed-off-by: Matt Ranostay <mranostay@gmail.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 95ddd3f4 24-Jul-2015 Joo Aun Saw <jasaw@dius.com.au>

tools: iio: remove unnecessary double pointer

Remove unnecessary double pointer from channel sorting function.

Signed-off-by: Joo Aun Saw <jasaw@dius.com.au>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 6b20f406 24-Jul-2015 Joo Aun Saw <jasaw@dius.com.au>

tools: iio: Set caller's ci_array pointer to NULL after free

On error, caller's ci_array is freed and set to NULL to avoid
potential double free if some other user of this code is not
sufficiently careful. Counter is reset to zero for consistency.

Signed-off-by: Joo Aun Saw <jasaw@dius.com.au>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 5e37c523 27-Jul-2015 Joo Aun Saw <jasaw@dius.com.au>

tools: iio: iio_utils: Make calc_digits static

Signed-off-by: Joo Aun Saw <jasaw@dius.com.au>
Acked-by: Daniel Baluta <daniel.baluta@intel.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 280f0925 28-Jul-2015 Joo Aun Saw <jasaw@dius.com.au>

tools: iio: iio_utils: remove unnecessary define guard

Signed-off-by: Joo Aun Saw <jasaw@dius.com.au>
Acked-by: Daniel Baluta <daniel.baluta@intel.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# ae067cb6 24-Jul-2015 Irina Tirdea <irina.tirdea@intel.com>

tools: iio: fix mask for 32 bit sensor data

When the the sensor data uses 32 bits out of 32, generic_buffer prints
the value 0 for all data read.

In this case, the mask is shifted 32 bits, which is beyond the size of
an integer. This will lead to the mask always being 0. Before printing,
the mask is applied to the raw value, thus generating a final value of 0.

Fix the mask by shifting a 64 bit value instead of an integer.

Signed-off-by: Irina Tirdea <irina.tirdea@intel.com>
Acked-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 7868dfd2 22-Jul-2015 Joo Aun Saw <jasaw@dius.com.au>

tools: iio: make scale and offset files optional

Make scale and offset optional by adding -ENOENT check as not all
drivers implement them.

Signed-off-by: Joo Aun Saw <jasaw@dius.com.au>
Acked-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# d9abc615 17-Jul-2015 Cristina Opriceana <cristina.opriceana@gmail.com>

tools: iio: Send error messages to stderr

This patch indends to make some cleanup and send printf
error messages to stderr. The changes were performed with coccinelle
for failure messages and manual for other cases, such as wrong usage
messages.

Signed-off-by: Cristina Opriceana <cristina.opriceana@gmail.com>
Reviewed-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 34cbea19 13-Jul-2015 Cristina Opriceana <cristina.opriceana@gmail.com>

tools: iio: Add ARRAY_SIZE macro

Calculation of the length of an array can be done with the ARRAY_SIZE
macro to make code more abstract and remove the associated
checkpatch.pl warning.

Signed-off-by: Cristina Opriceana <cristina.opriceana@gmail.com>
Acked-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# ff1ac639 13-Jul-2015 Cristina Opriceana <cristina.opriceana@gmail.com>

tools: iio: Remove explicit NULL comparison

Remove explicit NULL comparison and write it in its simpler form as
recommended by checkpatch.pl.

Signed-off-by: Cristina Opriceana <cristina.opriceana@gmail.com>
Reviewed-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# a9d7acc8 10-Jun-2015 Hartmut Knaack <knaack.h@gmx.de>

tools:iio: rename variables

Use more appropriate/common variable names:
* namepf instead of nameFile in iio_utils.c
* ret instead of retval in lsiio.c

Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 7663a4aa 10-Jun-2015 Hartmut Knaack <knaack.h@gmx.de>

tools:iio: adjust coding style

Fix various coding style issues, including:
* have spaces around operators
* indentation
* consolidate parameters in same line
* required braces
* adjust/drop comments
* multiline comment style
* delete unnecessary empty lines
* add empty lines to visualize logial code blocks
* typos

Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 9d475254 31-May-2015 Hartmut Knaack <knaack.h@gmx.de>

tools:iio:iio_utils: pass strings as const

Mark strings, which are not supposed to be changed (basedir, filename,
value), as const in function parameters.

Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 1e7c3478 31-May-2015 Hartmut Knaack <knaack.h@gmx.de>

tools:iio:iio_utils: initialize count during declaration

In build_channel_array(), count can be initialized already during variable
declaration.

Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# ace76e42 31-May-2015 Hartmut Knaack <knaack.h@gmx.de>

tools:iio:iio_utils: move up reset of sysfsfp

In iioutils_get_type() it is logically better fitting to have sysfsfp
assigned zero right after closing it.

Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 33ebcb21 31-May-2015 Hartmut Knaack <knaack.h@gmx.de>

tools:iio:iio_utils: refactor assignment of is_signed

Change the assignment of *is_signed in iioutils_get_type() to a one-liner,
as already done with *be.

Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 0e799878 31-May-2015 Hartmut Knaack <knaack.h@gmx.de>

tools:iio: return values directly

Return directly, if no common cleanup is required.

Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 5dc65d79 31-May-2015 Hartmut Knaack <knaack.h@gmx.de>

tools:iio:iio_utils: add missing documentation

Fully document public functions and elements.

Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 53118557 31-May-2015 Hartmut Knaack <knaack.h@gmx.de>

tools:iio:iio_utils: add error handling

Add error handling to calls which can indicate a major problem by
returning an error code.
This also sets ret to -ENOENT in iioutils_get_type() and
iioutils_get_param_float() to indicate if no matching directory entry was
found.

Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# e9e45b43 31-May-2015 Hartmut Knaack <knaack.h@gmx.de>

tools:iio: catch errors in string allocation

This patch catches errors in string allocation in generic_buffer.c,
iio_event_monitor.c, iio_utils.c and lsiio.c.

Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 2156b179 31-May-2015 Hartmut Knaack <knaack.h@gmx.de>

tools:iio:iio_utils: mark private function static

Functions _write_sysfs_int() and _write_sysfs_string() are supposed to
be called only by public wrappers, so make them static.

Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 096f9b86 31-May-2015 Hartmut Knaack <knaack.h@gmx.de>

tools:iio:iio_utils: implement digit calculation

Previously, the return value of sscanf() was treated as an indication of
the digits it would have read. Yet, sscanf() only returns the amount of
valid matches.
Therefore, introduce a function to calculate the decimal digits of the
read number and use this one to commence a colon search, as originally
intended.

Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# dc8b5d6e 31-May-2015 Hartmut Knaack <knaack.h@gmx.de>

tools:iio:iio_utils: check amount of matches

fscanf() usually returns the number of input items successfully matched
and assigned, which can be fewer than provided (or even zero).
Add a check in iioutils_get_type() to make sure all items are matched.

Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 2b6a6e67 31-May-2015 Hartmut Knaack <knaack.h@gmx.de>

tools:iio: save errno first

The man-page of errno states, that errno should be saved before doing any
library call, as that call may have changed the value of errno. So, when
encountering any error, save errno first.
This patch affects generic_buffer.c, iio_event_monitor.c and iio_utils.c.

Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 121b5e50 31-May-2015 Hartmut Knaack <knaack.h@gmx.de>

tools:iio:iio_utils: fix allocation handling

In build_channel_array(), count needs to be decreased in more places since
current->name and current->generic_name would be freed on the error path,
although they have not been allocated, yet.
This also requires to free current->name, when it is allocated, but
current->generic_name is not yet allocated.

Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 63f05c85 31-May-2015 Hartmut Knaack <knaack.h@gmx.de>

tools:iio: free channel-array completely

In iio_utils.c build_channel_array() dynamically allocates the string
generic_name in the current iio_channel_info, which doesn't got freed in
case of an error.
This dynamically allocated channel-array is used by generic_buffer, and
needs to be freed on the error/exit path.

Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 66dd08fd 31-May-2015 Hartmut Knaack <knaack.h@gmx.de>

tools:iio:iio_utils: free scan_el_dir on exit

In the error path, the string scan_el_dir got freed, while it was missing when
build_channel_array() finished without errors.

Signed-off-by: Hartmut Knaack <knaack.h@gmx.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# d9d7b990 27-Mar-2015 Irina Tirdea <irina.tirdea@intel.com>

tools: iio: generic_buffer: Fix generic scale extraction

When using generic_buffer to read data, the scale is not properly
detected for scale shared by type. This is caused by a problem
with the generation of generic name out of the full name.
E.g.: for current->name in_accel_z, the extracted generic name
is "in" (when it should be "in_accel"). This is used in generic_buffer
to generate scale and offset paths (in_accel_scale).

Consider the in_ or out_ prefix when extracting the generic name
from the full name.

Signed-off-by: Irina Tirdea <irina.tirdea@intel.com>
Reviewed-by: Daniel Baluta <daniel.baluta@intel.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>


# 817020cf 26-Feb-2015 Roberta Dobrescu <roberta.dobrescu@gmail.com>

iio: Move iio userspace applications out of staging

This patch moves iio userspace applications out of staging, to tools/iio/
and adds a Makefile in order to compile them easily. It also adds tools/iio/
to MAINTAINERS file.

Signed-off-by: Roberta Dobrescu <roberta.dobrescu@gmail.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>