Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <andriy.shevchenko@linux.intel.com>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

authored by

Andy Shevchenko and committed by
Bartosz Golaszewski
8a7a6103 8d1e84ab

+22 -17
+5 -5
drivers/gpio/gpiolib-legacy.c
··· 28 28 struct gpio_desc *desc; 29 29 int err; 30 30 31 - desc = gpio_to_desc(gpio); 32 - 33 31 /* Compatibility: assume unavailable "valid" GPIOs will appear later */ 34 - if (!desc && gpio_is_valid(gpio)) 32 + desc = gpio_to_desc(gpio); 33 + if (!desc) 35 34 return -EPROBE_DEFER; 36 35 37 36 err = gpiod_request(desc, label); ··· 62 63 */ 63 64 int gpio_request(unsigned gpio, const char *label) 64 65 { 65 - struct gpio_desc *desc = gpio_to_desc(gpio); 66 + struct gpio_desc *desc; 66 67 67 68 /* Compatibility: assume unavailable "valid" GPIOs will appear later */ 68 - if (!desc && gpio_is_valid(gpio)) 69 + desc = gpio_to_desc(gpio); 70 + if (!desc) 69 71 return -EPROBE_DEFER; 70 72 71 73 return gpiod_request(desc, label);
+1 -1
drivers/gpio/gpiolib-sysfs.c
··· 412 412 { 413 413 const struct gpio_device *gdev = dev_get_drvdata(dev); 414 414 415 - return sysfs_emit(buf, "%d\n", gdev->base); 415 + return sysfs_emit(buf, "%u\n", gdev->base); 416 416 } 417 417 static DEVICE_ATTR_RO(base); 418 418
+9 -10
drivers/gpio/gpiolib.c
··· 150 150 } 151 151 } 152 152 153 - if (!gpio_is_valid(gpio)) 154 - pr_warn("invalid GPIO %d\n", gpio); 155 - 156 153 return NULL; 157 154 } 158 155 EXPORT_SYMBOL_GPL(gpio_to_desc); ··· 294 297 EXPORT_SYMBOL_GPL(gpio_device_get_chip); 295 298 296 299 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ 297 - static int gpiochip_find_base_unlocked(int ngpio) 300 + static int gpiochip_find_base_unlocked(u16 ngpio) 298 301 { 302 + unsigned int base = GPIO_DYNAMIC_BASE; 299 303 struct gpio_device *gdev; 300 - int base = GPIO_DYNAMIC_BASE; 301 304 302 305 list_for_each_entry_srcu(gdev, &gpio_devices, list, 303 306 lockdep_is_held(&gpio_devices_lock)) { ··· 308 311 base = gdev->base + gdev->ngpio; 309 312 if (base < GPIO_DYNAMIC_BASE) 310 313 base = GPIO_DYNAMIC_BASE; 314 + if (base > GPIO_DYNAMIC_MAX - ngpio) 315 + break; 311 316 } 312 317 313 - if (gpio_is_valid(base)) { 318 + if (base <= GPIO_DYNAMIC_MAX - ngpio) { 314 319 pr_debug("%s: found new base at %d\n", __func__, base); 315 320 return base; 316 321 } else { ··· 748 749 if (ret) 749 750 goto err_remove_device; 750 751 751 - dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base, 752 + dev_dbg(&gdev->dev, "registered GPIOs %u to %u on %s\n", gdev->base, 752 753 gdev->base + gdev->ngpio - 1, gdev->label); 753 754 754 755 return 0; ··· 4787 4788 value = gpio_chip_get_value(gc, desc); 4788 4789 is_irq = test_bit(FLAG_USED_AS_IRQ, &desc->flags); 4789 4790 active_low = test_bit(FLAG_ACTIVE_LOW, &desc->flags); 4790 - seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s\n", 4791 + seq_printf(s, " gpio-%-3u (%-20.20s|%-20.20s) %s %s %s%s\n", 4791 4792 gpio, desc->name ?: "", gpiod_get_label(desc), 4792 4793 is_out ? "out" : "in ", 4793 4794 value >= 0 ? (value ? "hi" : "lo") : "? ", 4794 4795 is_irq ? "IRQ " : "", 4795 4796 active_low ? "ACTIVE LOW" : ""); 4796 4797 } else if (desc->name) { 4797 - seq_printf(s, " gpio-%-3d (%-20.20s)\n", gpio, desc->name); 4798 + seq_printf(s, " gpio-%-3u (%-20.20s)\n", gpio, desc->name); 4798 4799 } 4799 4800 4800 4801 gpio++; ··· 4866 4867 return 0; 4867 4868 } 4868 4869 4869 - seq_printf(s, "%s%s: GPIOs %d-%d", priv->newline ? "\n" : "", 4870 + seq_printf(s, "%s%s: GPIOs %u-%u", priv->newline ? "\n" : "", 4870 4871 dev_name(&gdev->dev), 4871 4872 gdev->base, gdev->base + gdev->ngpio - 1); 4872 4873 parent = gc->parent;
+1 -1
drivers/gpio/gpiolib.h
··· 61 61 struct module *owner; 62 62 struct gpio_chip __rcu *chip; 63 63 struct gpio_desc *descs; 64 - int base; 64 + unsigned int base; 65 65 u16 ngpio; 66 66 bool can_sleep; 67 67 const char *label;
+6
include/linux/gpio.h
··· 74 74 * Until they are all fixed, leave 0-512 space for them. 75 75 */ 76 76 #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 77 83 78 84 /* Always use the library code for GPIO management calls, 79 85 * or when sleeping may be involved.