Skip to content

Commit 9d846b1

Browse files
author
Bartosz Golaszewski
committed
gpiolib: check the return value of gpio_chip::get_direction()
As per the API contract - gpio_chip::get_direction() may fail and return a negative error number. However, we treat it as if it always returned 0 or 1. Check the return value of the callback and propagate the error number up the stack. Cc: [email protected] Reviewed-by: Linus Walleij <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Bartosz Golaszewski <[email protected]>
1 parent 0ad2507 commit 9d846b1

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

drivers/gpio/gpiolib.c

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,8 +1057,11 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
10571057
desc->gdev = gdev;
10581058

10591059
if (gc->get_direction && gpiochip_line_is_valid(gc, desc_index)) {
1060-
assign_bit(FLAG_IS_OUT,
1061-
&desc->flags, !gc->get_direction(gc, desc_index));
1060+
ret = gc->get_direction(gc, desc_index);
1061+
if (ret < 0)
1062+
goto err_cleanup_desc_srcu;
1063+
1064+
assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
10621065
} else {
10631066
assign_bit(FLAG_IS_OUT,
10641067
&desc->flags, !gc->direction_input);
@@ -2728,13 +2731,18 @@ int gpiod_direction_input_nonotify(struct gpio_desc *desc)
27282731
if (guard.gc->direction_input) {
27292732
ret = guard.gc->direction_input(guard.gc,
27302733
gpio_chip_hwgpio(desc));
2731-
} else if (guard.gc->get_direction &&
2732-
(guard.gc->get_direction(guard.gc,
2733-
gpio_chip_hwgpio(desc)) != 1)) {
2734-
gpiod_warn(desc,
2735-
"%s: missing direction_input() operation and line is output\n",
2736-
__func__);
2737-
return -EIO;
2734+
} else if (guard.gc->get_direction) {
2735+
ret = guard.gc->get_direction(guard.gc,
2736+
gpio_chip_hwgpio(desc));
2737+
if (ret < 0)
2738+
return ret;
2739+
2740+
if (ret != GPIO_LINE_DIRECTION_IN) {
2741+
gpiod_warn(desc,
2742+
"%s: missing direction_input() operation and line is output\n",
2743+
__func__);
2744+
return -EIO;
2745+
}
27382746
}
27392747
if (ret == 0) {
27402748
clear_bit(FLAG_IS_OUT, &desc->flags);
@@ -2771,12 +2779,18 @@ static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
27712779
gpio_chip_hwgpio(desc), val);
27722780
} else {
27732781
/* Check that we are in output mode if we can */
2774-
if (guard.gc->get_direction &&
2775-
guard.gc->get_direction(guard.gc, gpio_chip_hwgpio(desc))) {
2776-
gpiod_warn(desc,
2777-
"%s: missing direction_output() operation\n",
2778-
__func__);
2779-
return -EIO;
2782+
if (guard.gc->get_direction) {
2783+
ret = guard.gc->get_direction(guard.gc,
2784+
gpio_chip_hwgpio(desc));
2785+
if (ret < 0)
2786+
return ret;
2787+
2788+
if (ret != GPIO_LINE_DIRECTION_OUT) {
2789+
gpiod_warn(desc,
2790+
"%s: missing direction_output() operation\n",
2791+
__func__);
2792+
return -EIO;
2793+
}
27802794
}
27812795
/*
27822796
* If we can't actively set the direction, we are some

0 commit comments

Comments
 (0)