Re: [PATCH] staging: gpib: simplify and fix get_data_lines
From: Dan Carpenter
Date: Wed Aug 27 2025 - 04:07:45 EST
On Wed, Aug 27, 2025 at 10:15:33AM +0300, Dan Carpenter wrote:
> On Wed, Aug 27, 2025 at 12:05:02AM +0200, Osama Abdelkader wrote:
> > The function `get_data_lines()` in gpib_bitbang.c currently reads 8
> > GPIO descriptors individually and combines them into a byte.
> > This has two issues:
> >
> > * `gpiod_get_value()` returns an `int` which may be negative on
> > error. Assigning it directly into a `u8` may propagate unexpected
> > values. Masking ensures only the LSB is used.
>
> Using the last bit in an error code is not really "error handling"...
>
> What you could do instead would be something like:
>
> int ret;
>
> for (i = 0; i < 8; i++) {
> ret |= (gpiod_get_value(lines[i]) & 1) << i;
> if (ret < 0) {
> pr_err("something failed\n");
> return -EINVAL;
I meant to write "return 0;". It's type u8.
Also that doesn't work. The masks and shift mess it up.
u8 val = 0;
int ret;
for (i = 0; i < 8; i++) {
ret = gpiod_get_value(lines[i]);
if (ret < 0) {
pr_err("something failed\n");
continue;
}
val |= ret << i;
}
return ~val;
regards,
dan carpenter