Skip to content

Commit 8a7a610

Browse files
andy-shevBartosz Golaszewski
authored andcommitted
gpiolib: Get rid of never false gpio_is_valid() calls
In the cases when gpio_is_valid() is called with unsigned parameter the result is always true in the GPIO library code, hence the check for false won't ever be true. Get rid of such calls. While at it, move GPIO device base to be unsigned to clearly show it won't ever be negative. This requires a new definition for the maximum GPIO number in the system. Signed-off-by: Andy Shevchenko <[email protected]> Reviewed-by: Linus Walleij <[email protected]> Signed-off-by: Bartosz Golaszewski <[email protected]>
1 parent 8d1e84a commit 8a7a610

File tree

5 files changed

+22
-17
lines changed

5 files changed

+22
-17
lines changed

drivers/gpio/gpiolib-legacy.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
2828
struct gpio_desc *desc;
2929
int err;
3030

31-
desc = gpio_to_desc(gpio);
32-
3331
/* Compatibility: assume unavailable "valid" GPIOs will appear later */
34-
if (!desc && gpio_is_valid(gpio))
32+
desc = gpio_to_desc(gpio);
33+
if (!desc)
3534
return -EPROBE_DEFER;
3635

3736
err = gpiod_request(desc, label);
@@ -63,10 +62,11 @@ EXPORT_SYMBOL_GPL(gpio_request_one);
6362
*/
6463
int gpio_request(unsigned gpio, const char *label)
6564
{
66-
struct gpio_desc *desc = gpio_to_desc(gpio);
65+
struct gpio_desc *desc;
6766

6867
/* Compatibility: assume unavailable "valid" GPIOs will appear later */
69-
if (!desc && gpio_is_valid(gpio))
68+
desc = gpio_to_desc(gpio);
69+
if (!desc)
7070
return -EPROBE_DEFER;
7171

7272
return gpiod_request(desc, label);

drivers/gpio/gpiolib-sysfs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ static ssize_t base_show(struct device *dev,
412412
{
413413
const struct gpio_device *gdev = dev_get_drvdata(dev);
414414

415-
return sysfs_emit(buf, "%d\n", gdev->base);
415+
return sysfs_emit(buf, "%u\n", gdev->base);
416416
}
417417
static DEVICE_ATTR_RO(base);
418418

drivers/gpio/gpiolib.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,6 @@ struct gpio_desc *gpio_to_desc(unsigned gpio)
150150
}
151151
}
152152

153-
if (!gpio_is_valid(gpio))
154-
pr_warn("invalid GPIO %d\n", gpio);
155-
156153
return NULL;
157154
}
158155
EXPORT_SYMBOL_GPL(gpio_to_desc);
@@ -297,10 +294,10 @@ struct gpio_chip *gpio_device_get_chip(struct gpio_device *gdev)
297294
EXPORT_SYMBOL_GPL(gpio_device_get_chip);
298295

299296
/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
300-
static int gpiochip_find_base_unlocked(int ngpio)
297+
static int gpiochip_find_base_unlocked(u16 ngpio)
301298
{
299+
unsigned int base = GPIO_DYNAMIC_BASE;
302300
struct gpio_device *gdev;
303-
int base = GPIO_DYNAMIC_BASE;
304301

305302
list_for_each_entry_srcu(gdev, &gpio_devices, list,
306303
lockdep_is_held(&gpio_devices_lock)) {
@@ -311,9 +308,11 @@ static int gpiochip_find_base_unlocked(int ngpio)
311308
base = gdev->base + gdev->ngpio;
312309
if (base < GPIO_DYNAMIC_BASE)
313310
base = GPIO_DYNAMIC_BASE;
311+
if (base > GPIO_DYNAMIC_MAX - ngpio)
312+
break;
314313
}
315314

316-
if (gpio_is_valid(base)) {
315+
if (base <= GPIO_DYNAMIC_MAX - ngpio) {
317316
pr_debug("%s: found new base at %d\n", __func__, base);
318317
return base;
319318
} else {
@@ -749,7 +748,7 @@ static int gpiochip_setup_dev(struct gpio_device *gdev)
749748
if (ret)
750749
goto err_remove_device;
751750

752-
dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
751+
dev_dbg(&gdev->dev, "registered GPIOs %u to %u on %s\n", gdev->base,
753752
gdev->base + gdev->ngpio - 1, gdev->label);
754753

755754
return 0;
@@ -4788,14 +4787,14 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
47884787
value = gpio_chip_get_value(gc, desc);
47894788
is_irq = test_bit(FLAG_USED_AS_IRQ, &desc->flags);
47904789
active_low = test_bit(FLAG_ACTIVE_LOW, &desc->flags);
4791-
seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s\n",
4790+
seq_printf(s, " gpio-%-3u (%-20.20s|%-20.20s) %s %s %s%s\n",
47924791
gpio, desc->name ?: "", gpiod_get_label(desc),
47934792
is_out ? "out" : "in ",
47944793
value >= 0 ? (value ? "hi" : "lo") : "? ",
47954794
is_irq ? "IRQ " : "",
47964795
active_low ? "ACTIVE LOW" : "");
47974796
} else if (desc->name) {
4798-
seq_printf(s, " gpio-%-3d (%-20.20s)\n", gpio, desc->name);
4797+
seq_printf(s, " gpio-%-3u (%-20.20s)\n", gpio, desc->name);
47994798
}
48004799

48014800
gpio++;
@@ -4867,7 +4866,7 @@ static int gpiolib_seq_show(struct seq_file *s, void *v)
48674866
return 0;
48684867
}
48694868

4870-
seq_printf(s, "%s%s: GPIOs %d-%d", priv->newline ? "\n" : "",
4869+
seq_printf(s, "%s%s: GPIOs %u-%u", priv->newline ? "\n" : "",
48714870
dev_name(&gdev->dev),
48724871
gdev->base, gdev->base + gdev->ngpio - 1);
48734872
parent = gc->parent;

drivers/gpio/gpiolib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ struct gpio_device {
6161
struct module *owner;
6262
struct gpio_chip __rcu *chip;
6363
struct gpio_desc *descs;
64-
int base;
64+
unsigned int base;
6565
u16 ngpio;
6666
bool can_sleep;
6767
const char *label;

include/linux/gpio.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ static inline bool gpio_is_valid(int number)
7474
* Until they are all fixed, leave 0-512 space for them.
7575
*/
7676
#define GPIO_DYNAMIC_BASE 512
77+
/*
78+
* Define the maximum of the possible GPIO in the global numberspace.
79+
* While the GPIO base and numbers are positive, we limit it with signed
80+
* maximum as a lot of code is using negative values for special cases.
81+
*/
82+
#define GPIO_DYNAMIC_MAX INT_MAX
7783

7884
/* Always use the library code for GPIO management calls,
7985
* or when sleeping may be involved.

0 commit comments

Comments
 (0)