]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
Input: gpio_decoder - replace custom loop by gpiod_get_array_value_cansleep()
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Thu, 13 Nov 2025 15:44:44 +0000 (16:44 +0100)
committerDmitry Torokhov <dmitry.torokhov@gmail.com>
Wed, 21 Jan 2026 20:22:43 +0000 (12:22 -0800)
There is a custom loop that repeats parts of gpiod_get_array_value_cansleep().
Use that in conjunction with bitmap API to make code shorter and easier to
follow.

With this done, add an upper check for amount of GPIOs given based on
the driver's code.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Link: https://patch.msgid.link/20251113154616.3107676-4-andriy.shevchenko@linux.intel.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
drivers/input/misc/gpio_decoder.c

index a2ae400790f91d25f3c8aa9fcb7f9f803caf869b..7f319b9325503b182b3d8d7d3554946e5e6016d2 100644 (file)
@@ -6,11 +6,13 @@
  * encoded numeric value into an input event.
  */
 
+#include <linux/bitmap.h>
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/gpio/consumer.h>
 #include <linux/input.h>
 #include <linux/kernel.h>
+#include <linux/minmax.h>
 #include <linux/mod_devicetable.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
@@ -26,23 +28,18 @@ struct gpio_decoder {
 static int gpio_decoder_get_gpios_state(struct gpio_decoder *decoder)
 {
        struct gpio_descs *gpios = decoder->input_gpios;
-       unsigned int ret = 0;
-       int i, val;
-
-       for (i = 0; i < gpios->ndescs; i++) {
-               val = gpiod_get_value_cansleep(gpios->desc[i]);
-               if (val < 0) {
-                       dev_err(decoder->dev,
-                               "Error reading gpio %d: %d\n",
-                               desc_to_gpio(gpios->desc[i]), val);
-                       return val;
-               }
-
-               val = !!val;
-               ret = (ret << 1) | val;
+       DECLARE_BITMAP(values, 32);
+       unsigned int size;
+       int err;
+
+       size = min(gpios->ndescs, 32U);
+       err = gpiod_get_array_value_cansleep(size, gpios->desc, gpios->info, values);
+       if (err) {
+               dev_err(decoder->dev, "Error reading GPIO: %d\n", err);
+               return err;
        }
 
-       return ret;
+       return bitmap_read(values, 0, size);
 }
 
 static void gpio_decoder_poll_gpios(struct input_dev *input)
@@ -81,6 +78,9 @@ static int gpio_decoder_probe(struct platform_device *pdev)
        if (decoder->input_gpios->ndescs < 2)
                return dev_err_probe(dev, -EINVAL, "not enough gpios found\n");
 
+       if (decoder->input_gpios->ndescs > 31)
+               return dev_err_probe(dev, -EINVAL, "too many gpios found\n");
+
        if (device_property_read_u32(dev, "decoder-max-value", &max))
                max = (1U << decoder->input_gpios->ndescs) - 1;