Skip to content

Commit 4eec877

Browse files
andy-shevdtor
authored andcommitted
Input: gpio_decoder - replace custom loop by gpiod_get_array_value_cansleep()
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>
1 parent 7cda46c commit 4eec877

1 file changed

Lines changed: 15 additions & 15 deletions

File tree

drivers/input/misc/gpio_decoder.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
* encoded numeric value into an input event.
77
*/
88

9+
#include <linux/bitmap.h>
910
#include <linux/device.h>
1011
#include <linux/err.h>
1112
#include <linux/gpio/consumer.h>
1213
#include <linux/input.h>
1314
#include <linux/kernel.h>
15+
#include <linux/minmax.h>
1416
#include <linux/mod_devicetable.h>
1517
#include <linux/module.h>
1618
#include <linux/platform_device.h>
@@ -26,23 +28,18 @@ struct gpio_decoder {
2628
static int gpio_decoder_get_gpios_state(struct gpio_decoder *decoder)
2729
{
2830
struct gpio_descs *gpios = decoder->input_gpios;
29-
unsigned int ret = 0;
30-
int i, val;
31-
32-
for (i = 0; i < gpios->ndescs; i++) {
33-
val = gpiod_get_value_cansleep(gpios->desc[i]);
34-
if (val < 0) {
35-
dev_err(decoder->dev,
36-
"Error reading gpio %d: %d\n",
37-
desc_to_gpio(gpios->desc[i]), val);
38-
return val;
39-
}
40-
41-
val = !!val;
42-
ret = (ret << 1) | val;
31+
DECLARE_BITMAP(values, 32);
32+
unsigned int size;
33+
int err;
34+
35+
size = min(gpios->ndescs, 32U);
36+
err = gpiod_get_array_value_cansleep(size, gpios->desc, gpios->info, values);
37+
if (err) {
38+
dev_err(decoder->dev, "Error reading GPIO: %d\n", err);
39+
return err;
4340
}
4441

45-
return ret;
42+
return bitmap_read(values, 0, size);
4643
}
4744

4845
static void gpio_decoder_poll_gpios(struct input_dev *input)
@@ -81,6 +78,9 @@ static int gpio_decoder_probe(struct platform_device *pdev)
8178
if (decoder->input_gpios->ndescs < 2)
8279
return dev_err_probe(dev, -EINVAL, "not enough gpios found\n");
8380

81+
if (decoder->input_gpios->ndescs > 31)
82+
return dev_err_probe(dev, -EINVAL, "too many gpios found\n");
83+
8484
if (device_property_read_u32(dev, "decoder-max-value", &max))
8585
max = (1U << decoder->input_gpios->ndescs) - 1;
8686

0 commit comments

Comments
 (0)