Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Andy Shevchenko and committed by
Dmitry Torokhov
4eec8772 7cda46c3

+14 -14
+14 -14
drivers/input/misc/gpio_decoder.c
··· 6 6 * encoded numeric value into an input event. 7 7 */ 8 8 9 + #include <linux/bitmap.h> 9 10 #include <linux/device.h> 10 11 #include <linux/err.h> 11 12 #include <linux/gpio/consumer.h> 12 13 #include <linux/input.h> 13 14 #include <linux/kernel.h> 15 + #include <linux/minmax.h> 14 16 #include <linux/mod_devicetable.h> 15 17 #include <linux/module.h> 16 18 #include <linux/platform_device.h> ··· 28 26 static int gpio_decoder_get_gpios_state(struct gpio_decoder *decoder) 29 27 { 30 28 struct gpio_descs *gpios = decoder->input_gpios; 31 - unsigned int ret = 0; 32 - int i, val; 29 + DECLARE_BITMAP(values, 32); 30 + unsigned int size; 31 + int err; 33 32 34 - for (i = 0; i < gpios->ndescs; i++) { 35 - val = gpiod_get_value_cansleep(gpios->desc[i]); 36 - if (val < 0) { 37 - dev_err(decoder->dev, 38 - "Error reading gpio %d: %d\n", 39 - desc_to_gpio(gpios->desc[i]), val); 40 - return val; 41 - } 42 - 43 - val = !!val; 44 - ret = (ret << 1) | val; 33 + size = min(gpios->ndescs, 32U); 34 + err = gpiod_get_array_value_cansleep(size, gpios->desc, gpios->info, values); 35 + if (err) { 36 + dev_err(decoder->dev, "Error reading GPIO: %d\n", err); 37 + return err; 45 38 } 46 39 47 - return ret; 40 + return bitmap_read(values, 0, size); 48 41 } 49 42 50 43 static void gpio_decoder_poll_gpios(struct input_dev *input) ··· 77 80 78 81 if (decoder->input_gpios->ndescs < 2) 79 82 return dev_err_probe(dev, -EINVAL, "not enough gpios found\n"); 83 + 84 + if (decoder->input_gpios->ndescs > 31) 85 + return dev_err_probe(dev, -EINVAL, "too many gpios found\n"); 80 86 81 87 if (device_property_read_u32(dev, "decoder-max-value", &max)) 82 88 max = (1U << decoder->input_gpios->ndescs) - 1;