Commit ef7c7553 authored by Bamvor Jian Zhang's avatar Bamvor Jian Zhang Committed by Linus Walleij

gpiolib: improve overlap check of range of gpio

There are limitations for the current checker:
1.  Could not check the overlap if the new gpiochip is the secondly
    gpiochip.
2.  Could not check the overlap if the new gpiochip is overlap
    with the left of gpiochip. E.g. if we insert [c, d] between
    [a,b] and [e, f], and e >= c + d, it will successful even if
    c < a + b.
3.  Allow overlap of base of different gpiochip.

This patch fix these issues by checking the overlap of both right and
left gpiochip in the same loop statement.
Signed-off-by: default avatarBamvor Jian Zhang <bamvor.zhangjian@linaro.org>
[Tweaked to remove unnecessary ret variable]
Signed-off-by: default avatarLinus Walleij <linus.walleij@linaro.org>
parent ed37915c
...@@ -182,7 +182,7 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction); ...@@ -182,7 +182,7 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction);
/* /*
* Add a new chip to the global chips list, keeping the list of chips sorted * Add a new chip to the global chips list, keeping the list of chips sorted
* by base order. * by range(means [base, base + ngpio - 1]) order.
* *
* Return -EBUSY if the new chip overlaps with some other chip's integer * Return -EBUSY if the new chip overlaps with some other chip's integer
* space. * space.
...@@ -190,31 +190,48 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction); ...@@ -190,31 +190,48 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction);
static int gpiochip_add_to_list(struct gpio_chip *chip) static int gpiochip_add_to_list(struct gpio_chip *chip)
{ {
struct list_head *pos; struct list_head *pos;
struct gpio_chip *_chip; struct gpio_chip *iterator;
int err = 0; struct gpio_chip *previous = NULL;
if (list_empty(&gpio_chips)) {
pos = gpio_chips.next;
goto found;
}
/* find where to insert our chip */
list_for_each(pos, &gpio_chips) { list_for_each(pos, &gpio_chips) {
_chip = list_entry(pos, struct gpio_chip, list); iterator = list_entry(pos, struct gpio_chip, list);
/* shall we insert before _chip? */ if (iterator->base >= chip->base + chip->ngpio) {
if (_chip->base >= chip->base + chip->ngpio) /*
break; * Iterator is the first GPIO chip so there is no
* previous one
*/
if (previous == NULL) {
goto found;
} else {
/*
* We found a valid range(means
* [base, base + ngpio - 1]) between previous
* and iterator chip.
*/
if (previous->base + previous->ngpio
<= chip->base)
goto found;
}
}
previous = iterator;
} }
/* are we stepping on the chip right before? */ /* We are beyond the last chip in the list */
if (pos != &gpio_chips && pos->prev != &gpio_chips) { if (iterator->base + iterator->ngpio <= chip->base)
_chip = list_entry(pos->prev, struct gpio_chip, list); goto found;
if (_chip->base + _chip->ngpio > chip->base) {
dev_err(chip->parent, dev_err(chip->parent,
"GPIO integer space overlap, cannot add chip\n"); "GPIO integer space overlap, cannot add chip\n");
err = -EBUSY; return -EBUSY;
}
}
if (!err) found:
list_add_tail(&chip->list, pos); list_add_tail(&chip->list, pos);
return 0;
return err;
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment