aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohan Hovold <johan@kernel.org>2016-07-03 12:32:05 -0400
committerLinus Walleij <linus.walleij@linaro.org>2016-07-04 10:51:29 -0400
commit85b03b3033fd4eba82665b3b9902c095a08cc52f (patch)
tree6903637e3ff3c9d821dd3718d12c2321df9c25bf
parent87041a58d3b19455d39baed2a5e5bb43b58fb915 (diff)
Revert "gpiolib: Split GPIO flags parsing and GPIO configuration"
This reverts commit 923b93e451db876d1479d3e4458fce14fec31d1c. Make sure consumers do not overwrite gpio flags for pins that have already been claimed. While adding support for gpio drivers to refuse a request using unsupported flags, the order of when the requested flag was checked and the new flags were applied was reversed to that consumers could overwrite flags for already requested gpios. This not only affects device-tree setups where two drivers could request the same gpio using conflicting configurations, but also allowed user space to clear gpio flags for already claimed pins simply by attempting to export them through the sysfs interface. By for example clearing the FLAG_ACTIVE_LOW flag this way, user space could effectively change the polarity of a signal. Reverting this change obviously prevents gpio drivers from doing sanity checks on the flags in their request callbacks. Fortunately only one recently added driver (gpio-tps65218 in v4.6) appears to do this, and a follow up patch could restore this functionality through a different interface. Cc: stable <stable@vger.kernel.org> # 4.4 Signed-off-by: Johan Hovold <johan@kernel.org> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
-rw-r--r--drivers/gpio/gpiolib-legacy.c8
-rw-r--r--drivers/gpio/gpiolib.c52
2 files changed, 20 insertions, 40 deletions
diff --git a/drivers/gpio/gpiolib-legacy.c b/drivers/gpio/gpiolib-legacy.c
index 3a5c7011ad3b..8b830996fe02 100644
--- a/drivers/gpio/gpiolib-legacy.c
+++ b/drivers/gpio/gpiolib-legacy.c
@@ -28,6 +28,10 @@ int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
28 if (!desc && gpio_is_valid(gpio)) 28 if (!desc && gpio_is_valid(gpio))
29 return -EPROBE_DEFER; 29 return -EPROBE_DEFER;
30 30
31 err = gpiod_request(desc, label);
32 if (err)
33 return err;
34
31 if (flags & GPIOF_OPEN_DRAIN) 35 if (flags & GPIOF_OPEN_DRAIN)
32 set_bit(FLAG_OPEN_DRAIN, &desc->flags); 36 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
33 37
@@ -37,10 +41,6 @@ int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
37 if (flags & GPIOF_ACTIVE_LOW) 41 if (flags & GPIOF_ACTIVE_LOW)
38 set_bit(FLAG_ACTIVE_LOW, &desc->flags); 42 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
39 43
40 err = gpiod_request(desc, label);
41 if (err)
42 return err;
43
44 if (flags & GPIOF_DIR_IN) 44 if (flags & GPIOF_DIR_IN)
45 err = gpiod_direction_input(desc); 45 err = gpiod_direction_input(desc);
46 else 46 else
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 570771ed19e6..be74bd370f1f 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -1352,14 +1352,6 @@ static int __gpiod_request(struct gpio_desc *desc, const char *label)
1352 spin_lock_irqsave(&gpio_lock, flags); 1352 spin_lock_irqsave(&gpio_lock, flags);
1353 } 1353 }
1354done: 1354done:
1355 if (status < 0) {
1356 /* Clear flags that might have been set by the caller before
1357 * requesting the GPIO.
1358 */
1359 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1360 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1361 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1362 }
1363 spin_unlock_irqrestore(&gpio_lock, flags); 1355 spin_unlock_irqrestore(&gpio_lock, flags);
1364 return status; 1356 return status;
1365} 1357}
@@ -2587,28 +2579,13 @@ struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
2587} 2579}
2588EXPORT_SYMBOL_GPL(gpiod_get_optional); 2580EXPORT_SYMBOL_GPL(gpiod_get_optional);
2589 2581
2590/**
2591 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2592 * @desc: gpio to be setup
2593 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2594 * of_get_gpio_hog()
2595 *
2596 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2597 */
2598static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2599{
2600 if (lflags & GPIO_ACTIVE_LOW)
2601 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2602 if (lflags & GPIO_OPEN_DRAIN)
2603 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2604 if (lflags & GPIO_OPEN_SOURCE)
2605 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2606}
2607 2582
2608/** 2583/**
2609 * gpiod_configure_flags - helper function to configure a given GPIO 2584 * gpiod_configure_flags - helper function to configure a given GPIO
2610 * @desc: gpio whose value will be assigned 2585 * @desc: gpio whose value will be assigned
2611 * @con_id: function within the GPIO consumer 2586 * @con_id: function within the GPIO consumer
2587 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2588 * of_get_gpio_hog()
2612 * @dflags: gpiod_flags - optional GPIO initialization flags 2589 * @dflags: gpiod_flags - optional GPIO initialization flags
2613 * 2590 *
2614 * Return 0 on success, -ENOENT if no GPIO has been assigned to the 2591 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
@@ -2616,10 +2593,17 @@ static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2616 * occurred while trying to acquire the GPIO. 2593 * occurred while trying to acquire the GPIO.
2617 */ 2594 */
2618static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, 2595static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
2619 enum gpiod_flags dflags) 2596 unsigned long lflags, enum gpiod_flags dflags)
2620{ 2597{
2621 int status; 2598 int status;
2622 2599
2600 if (lflags & GPIO_ACTIVE_LOW)
2601 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2602 if (lflags & GPIO_OPEN_DRAIN)
2603 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2604 if (lflags & GPIO_OPEN_SOURCE)
2605 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2606
2623 /* No particular flag request, return here... */ 2607 /* No particular flag request, return here... */
2624 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) { 2608 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2625 pr_debug("no flags found for %s\n", con_id); 2609 pr_debug("no flags found for %s\n", con_id);
@@ -2686,13 +2670,11 @@ struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2686 return desc; 2670 return desc;
2687 } 2671 }
2688 2672
2689 gpiod_parse_flags(desc, lookupflags);
2690
2691 status = gpiod_request(desc, con_id); 2673 status = gpiod_request(desc, con_id);
2692 if (status < 0) 2674 if (status < 0)
2693 return ERR_PTR(status); 2675 return ERR_PTR(status);
2694 2676
2695 status = gpiod_configure_flags(desc, con_id, flags); 2677 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
2696 if (status < 0) { 2678 if (status < 0) {
2697 dev_dbg(dev, "setup of GPIO %s failed\n", con_id); 2679 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2698 gpiod_put(desc); 2680 gpiod_put(desc);
@@ -2748,6 +2730,10 @@ struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2748 if (IS_ERR(desc)) 2730 if (IS_ERR(desc))
2749 return desc; 2731 return desc;
2750 2732
2733 ret = gpiod_request(desc, NULL);
2734 if (ret)
2735 return ERR_PTR(ret);
2736
2751 if (active_low) 2737 if (active_low)
2752 set_bit(FLAG_ACTIVE_LOW, &desc->flags); 2738 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2753 2739
@@ -2758,10 +2744,6 @@ struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2758 set_bit(FLAG_OPEN_SOURCE, &desc->flags); 2744 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2759 } 2745 }
2760 2746
2761 ret = gpiod_request(desc, NULL);
2762 if (ret)
2763 return ERR_PTR(ret);
2764
2765 return desc; 2747 return desc;
2766} 2748}
2767EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod); 2749EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
@@ -2814,8 +2796,6 @@ int gpiod_hog(struct gpio_desc *desc, const char *name,
2814 chip = gpiod_to_chip(desc); 2796 chip = gpiod_to_chip(desc);
2815 hwnum = gpio_chip_hwgpio(desc); 2797 hwnum = gpio_chip_hwgpio(desc);
2816 2798
2817 gpiod_parse_flags(desc, lflags);
2818
2819 local_desc = gpiochip_request_own_desc(chip, hwnum, name); 2799 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2820 if (IS_ERR(local_desc)) { 2800 if (IS_ERR(local_desc)) {
2821 status = PTR_ERR(local_desc); 2801 status = PTR_ERR(local_desc);
@@ -2824,7 +2804,7 @@ int gpiod_hog(struct gpio_desc *desc, const char *name,
2824 return status; 2804 return status;
2825 } 2805 }
2826 2806
2827 status = gpiod_configure_flags(desc, name, dflags); 2807 status = gpiod_configure_flags(desc, name, lflags, dflags);
2828 if (status < 0) { 2808 if (status < 0) {
2829 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n", 2809 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
2830 name, chip->label, hwnum, status); 2810 name, chip->label, hwnum, status);