Skip to contentSkip to conversation

Input: gpio_decoder - fix bit order reversal introduced by bitmap conversion

PATCH In review Esben Haabendal 2 participants 2 messages Aug 17

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Input: gpio_decoder - fix bit order reversal introduced by bitmap conversion

The switch from a manual loop to gpiod_get_array_value_cansleep() + bitmap_read() inadvertently reversed the bit order of the decoded value. The original loop treated GPIO[0] as MSB by shifting left on each iteration, but bitmap_read() returns GPIO[0] at bit 0 (LSB).

The previous bit order was correct according to device-tree bindings, and the change broke existing users.

Fixes 4eec8772e4f5 ("Input: gpio_decoder - replace custom loop by gpiod_get_array_value_cansleep()")

---
The refactoring from a custom loop to gpiod_get_array_value_cansleep() + bitmap_read() caused the bit order to be changed from reading MSB first to LSB first.

The device-tree bindings explicitly states that that it should be MSB first, and the change obviously is a regression for existing users.

---
 drivers/input/misc/gpio_decoder.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/input/misc/gpio_decoder.c +2 -1
index 0e4a49845afa..055bfcbd5235 100644
@@ -7,6 +7,7 @@
  */
 
 #include <linux/bitmap.h>
+#include <linux/bitrev.h>
 #include <linux/dev_printk.h>
 #include <linux/device/devres.h>
 #include <linux/err.h>
@@ -39,7 +40,7 @@ static int gpio_decoder_get_gpios_state(struct gpio_decoder *decoder)
 		return err;
 	}
 
-	return bitmap_read(values, 0, size);
+	return bitrev32(bitmap_read(values, 0, size)) >> (32 - size);
 }
 
 static void gpio_decoder_poll_gpios(struct input_dev *input)

---
base-commit: 8d3ae59288f1e7d58d76558a6ee96d533bc5019f
change-id: 20260817-gpio-decoder-bitrev-2457d68fd62b

Best regards,

Signature · 1 line
Esben Haabendal <[email protected]>
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Esben Haabendal

The switch from a manual loop to gpiod_get_array_value_cansleep() + bitmap_read() inadvertently reversed the bit order of the decoded value. The original loop treated GPIO[0] as MSB by shifting left on each iteration, but bitmap_read() returns GPIO[0] at bit 0 (LSB).

The previous bit order was correct according to device-tree bindings, and the change broke existing users.

Oops, thanks for catching this!

Signature · 2 lines
With Best Regards,
Andy Shevchenko