aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpio/gpiolib.c')
-rw-r--r--drivers/gpio/gpiolib.c143
1 files changed, 82 insertions, 61 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index fff9786cdc6..c2534d62911 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -88,13 +88,14 @@ static int gpiod_request(struct gpio_desc *desc, const char *label);
88static void gpiod_free(struct gpio_desc *desc); 88static void gpiod_free(struct gpio_desc *desc);
89static int gpiod_direction_input(struct gpio_desc *desc); 89static int gpiod_direction_input(struct gpio_desc *desc);
90static int gpiod_direction_output(struct gpio_desc *desc, int value); 90static int gpiod_direction_output(struct gpio_desc *desc, int value);
91static int gpiod_get_direction(const struct gpio_desc *desc);
91static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); 92static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
92static int gpiod_get_value_cansleep(struct gpio_desc *desc); 93static int gpiod_get_value_cansleep(const struct gpio_desc *desc);
93static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); 94static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
94static int gpiod_get_value(struct gpio_desc *desc); 95static int gpiod_get_value(const struct gpio_desc *desc);
95static void gpiod_set_value(struct gpio_desc *desc, int value); 96static void gpiod_set_value(struct gpio_desc *desc, int value);
96static int gpiod_cansleep(struct gpio_desc *desc); 97static int gpiod_cansleep(const struct gpio_desc *desc);
97static int gpiod_to_irq(struct gpio_desc *desc); 98static int gpiod_to_irq(const struct gpio_desc *desc);
98static int gpiod_export(struct gpio_desc *desc, bool direction_may_change); 99static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
99static int gpiod_export_link(struct device *dev, const char *name, 100static int gpiod_export_link(struct device *dev, const char *name,
100 struct gpio_desc *desc); 101 struct gpio_desc *desc);
@@ -171,12 +172,12 @@ static int gpio_ensure_requested(struct gpio_desc *desc)
171 return 0; 172 return 0;
172} 173}
173 174
174/* caller holds gpio_lock *OR* gpio is marked as requested */ 175static struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
175static struct gpio_chip *gpiod_to_chip(struct gpio_desc *desc)
176{ 176{
177 return desc->chip; 177 return desc ? desc->chip : NULL;
178} 178}
179 179
180/* caller holds gpio_lock *OR* gpio is marked as requested */
180struct gpio_chip *gpio_to_chip(unsigned gpio) 181struct gpio_chip *gpio_to_chip(unsigned gpio)
181{ 182{
182 return gpiod_to_chip(gpio_to_desc(gpio)); 183 return gpiod_to_chip(gpio_to_desc(gpio));
@@ -207,7 +208,7 @@ static int gpiochip_find_base(int ngpio)
207} 208}
208 209
209/* caller ensures gpio is valid and requested, chip->get_direction may sleep */ 210/* caller ensures gpio is valid and requested, chip->get_direction may sleep */
210static int gpiod_get_direction(struct gpio_desc *desc) 211static int gpiod_get_direction(const struct gpio_desc *desc)
211{ 212{
212 struct gpio_chip *chip; 213 struct gpio_chip *chip;
213 unsigned offset; 214 unsigned offset;
@@ -223,11 +224,13 @@ static int gpiod_get_direction(struct gpio_desc *desc)
223 if (status > 0) { 224 if (status > 0) {
224 /* GPIOF_DIR_IN, or other positive */ 225 /* GPIOF_DIR_IN, or other positive */
225 status = 1; 226 status = 1;
226 clear_bit(FLAG_IS_OUT, &desc->flags); 227 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
228 * so it does not affect constness per se */
229 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
227 } 230 }
228 if (status == 0) { 231 if (status == 0) {
229 /* GPIOF_DIR_OUT */ 232 /* GPIOF_DIR_OUT */
230 set_bit(FLAG_IS_OUT, &desc->flags); 233 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
231 } 234 }
232 return status; 235 return status;
233} 236}
@@ -263,7 +266,7 @@ static DEFINE_MUTEX(sysfs_lock);
263static ssize_t gpio_direction_show(struct device *dev, 266static ssize_t gpio_direction_show(struct device *dev,
264 struct device_attribute *attr, char *buf) 267 struct device_attribute *attr, char *buf)
265{ 268{
266 struct gpio_desc *desc = dev_get_drvdata(dev); 269 const struct gpio_desc *desc = dev_get_drvdata(dev);
267 ssize_t status; 270 ssize_t status;
268 271
269 mutex_lock(&sysfs_lock); 272 mutex_lock(&sysfs_lock);
@@ -654,6 +657,11 @@ static ssize_t export_store(struct class *class,
654 goto done; 657 goto done;
655 658
656 desc = gpio_to_desc(gpio); 659 desc = gpio_to_desc(gpio);
660 /* reject invalid GPIOs */
661 if (!desc) {
662 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
663 return -EINVAL;
664 }
657 665
658 /* No extra locking here; FLAG_SYSFS just signifies that the 666 /* No extra locking here; FLAG_SYSFS just signifies that the
659 * request and export were done by on behalf of userspace, so 667 * request and export were done by on behalf of userspace, so
@@ -690,12 +698,14 @@ static ssize_t unexport_store(struct class *class,
690 if (status < 0) 698 if (status < 0)
691 goto done; 699 goto done;
692 700
693 status = -EINVAL;
694
695 desc = gpio_to_desc(gpio); 701 desc = gpio_to_desc(gpio);
696 /* reject bogus commands (gpio_unexport ignores them) */ 702 /* reject bogus commands (gpio_unexport ignores them) */
697 if (!desc) 703 if (!desc) {
698 goto done; 704 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
705 return -EINVAL;
706 }
707
708 status = -EINVAL;
699 709
700 /* No extra locking here; FLAG_SYSFS just signifies that the 710 /* No extra locking here; FLAG_SYSFS just signifies that the
701 * request and export were done by on behalf of userspace, so 711 * request and export were done by on behalf of userspace, so
@@ -846,8 +856,10 @@ static int gpiod_export_link(struct device *dev, const char *name,
846{ 856{
847 int status = -EINVAL; 857 int status = -EINVAL;
848 858
849 if (!desc) 859 if (!desc) {
850 goto done; 860 pr_warn("%s: invalid GPIO\n", __func__);
861 return -EINVAL;
862 }
851 863
852 mutex_lock(&sysfs_lock); 864 mutex_lock(&sysfs_lock);
853 865
@@ -865,7 +877,6 @@ static int gpiod_export_link(struct device *dev, const char *name,
865 877
866 mutex_unlock(&sysfs_lock); 878 mutex_unlock(&sysfs_lock);
867 879
868done:
869 if (status) 880 if (status)
870 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), 881 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
871 status); 882 status);
@@ -896,8 +907,10 @@ static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
896 struct device *dev = NULL; 907 struct device *dev = NULL;
897 int status = -EINVAL; 908 int status = -EINVAL;
898 909
899 if (!desc) 910 if (!desc) {
900 goto done; 911 pr_warn("%s: invalid GPIO\n", __func__);
912 return -EINVAL;
913 }
901 914
902 mutex_lock(&sysfs_lock); 915 mutex_lock(&sysfs_lock);
903 916
@@ -914,7 +927,6 @@ static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
914unlock: 927unlock:
915 mutex_unlock(&sysfs_lock); 928 mutex_unlock(&sysfs_lock);
916 929
917done:
918 if (status) 930 if (status)
919 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), 931 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
920 status); 932 status);
@@ -940,8 +952,8 @@ static void gpiod_unexport(struct gpio_desc *desc)
940 struct device *dev = NULL; 952 struct device *dev = NULL;
941 953
942 if (!desc) { 954 if (!desc) {
943 status = -EINVAL; 955 pr_warn("%s: invalid GPIO\n", __func__);
944 goto done; 956 return;
945 } 957 }
946 958
947 mutex_lock(&sysfs_lock); 959 mutex_lock(&sysfs_lock);
@@ -962,7 +974,7 @@ static void gpiod_unexport(struct gpio_desc *desc)
962 device_unregister(dev); 974 device_unregister(dev);
963 put_device(dev); 975 put_device(dev);
964 } 976 }
965done: 977
966 if (status) 978 if (status)
967 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), 979 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
968 status); 980 status);
@@ -1384,12 +1396,13 @@ static int gpiod_request(struct gpio_desc *desc, const char *label)
1384 int status = -EPROBE_DEFER; 1396 int status = -EPROBE_DEFER;
1385 unsigned long flags; 1397 unsigned long flags;
1386 1398
1387 spin_lock_irqsave(&gpio_lock, flags);
1388
1389 if (!desc) { 1399 if (!desc) {
1390 status = -EINVAL; 1400 pr_warn("%s: invalid GPIO\n", __func__);
1391 goto done; 1401 return -EINVAL;
1392 } 1402 }
1403
1404 spin_lock_irqsave(&gpio_lock, flags);
1405
1393 chip = desc->chip; 1406 chip = desc->chip;
1394 if (chip == NULL) 1407 if (chip == NULL)
1395 goto done; 1408 goto done;
@@ -1432,8 +1445,7 @@ static int gpiod_request(struct gpio_desc *desc, const char *label)
1432done: 1445done:
1433 if (status) 1446 if (status)
1434 pr_debug("_gpio_request: gpio-%d (%s) status %d\n", 1447 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
1435 desc ? desc_to_gpio(desc) : -1, 1448 desc_to_gpio(desc), label ? : "?", status);
1436 label ? : "?", status);
1437 spin_unlock_irqrestore(&gpio_lock, flags); 1449 spin_unlock_irqrestore(&gpio_lock, flags);
1438 return status; 1450 return status;
1439} 1451}
@@ -1616,10 +1628,13 @@ static int gpiod_direction_input(struct gpio_desc *desc)
1616 int status = -EINVAL; 1628 int status = -EINVAL;
1617 int offset; 1629 int offset;
1618 1630
1631 if (!desc) {
1632 pr_warn("%s: invalid GPIO\n", __func__);
1633 return -EINVAL;
1634 }
1635
1619 spin_lock_irqsave(&gpio_lock, flags); 1636 spin_lock_irqsave(&gpio_lock, flags);
1620 1637
1621 if (!desc)
1622 goto fail;
1623 chip = desc->chip; 1638 chip = desc->chip;
1624 if (!chip || !chip->get || !chip->direction_input) 1639 if (!chip || !chip->get || !chip->direction_input)
1625 goto fail; 1640 goto fail;
@@ -1655,13 +1670,9 @@ lose:
1655 return status; 1670 return status;
1656fail: 1671fail:
1657 spin_unlock_irqrestore(&gpio_lock, flags); 1672 spin_unlock_irqrestore(&gpio_lock, flags);
1658 if (status) { 1673 if (status)
1659 int gpio = -1; 1674 pr_debug("%s: gpio-%d status %d\n", __func__,
1660 if (desc) 1675 desc_to_gpio(desc), status);
1661 gpio = desc_to_gpio(desc);
1662 pr_debug("%s: gpio-%d status %d\n",
1663 __func__, gpio, status);
1664 }
1665 return status; 1676 return status;
1666} 1677}
1667 1678
@@ -1678,6 +1689,11 @@ static int gpiod_direction_output(struct gpio_desc *desc, int value)
1678 int status = -EINVAL; 1689 int status = -EINVAL;
1679 int offset; 1690 int offset;
1680 1691
1692 if (!desc) {
1693 pr_warn("%s: invalid GPIO\n", __func__);
1694 return -EINVAL;
1695 }
1696
1681 /* Open drain pin should not be driven to 1 */ 1697 /* Open drain pin should not be driven to 1 */
1682 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 1698 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1683 return gpiod_direction_input(desc); 1699 return gpiod_direction_input(desc);
@@ -1688,8 +1704,6 @@ static int gpiod_direction_output(struct gpio_desc *desc, int value)
1688 1704
1689 spin_lock_irqsave(&gpio_lock, flags); 1705 spin_lock_irqsave(&gpio_lock, flags);
1690 1706
1691 if (!desc)
1692 goto fail;
1693 chip = desc->chip; 1707 chip = desc->chip;
1694 if (!chip || !chip->set || !chip->direction_output) 1708 if (!chip || !chip->set || !chip->direction_output)
1695 goto fail; 1709 goto fail;
@@ -1725,13 +1739,9 @@ lose:
1725 return status; 1739 return status;
1726fail: 1740fail:
1727 spin_unlock_irqrestore(&gpio_lock, flags); 1741 spin_unlock_irqrestore(&gpio_lock, flags);
1728 if (status) { 1742 if (status)
1729 int gpio = -1; 1743 pr_debug("%s: gpio-%d status %d\n", __func__,
1730 if (desc) 1744 desc_to_gpio(desc), status);
1731 gpio = desc_to_gpio(desc);
1732 pr_debug("%s: gpio-%d status %d\n",
1733 __func__, gpio, status);
1734 }
1735 return status; 1745 return status;
1736} 1746}
1737 1747
@@ -1753,10 +1763,13 @@ static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1753 int status = -EINVAL; 1763 int status = -EINVAL;
1754 int offset; 1764 int offset;
1755 1765
1766 if (!desc) {
1767 pr_warn("%s: invalid GPIO\n", __func__);
1768 return -EINVAL;
1769 }
1770
1756 spin_lock_irqsave(&gpio_lock, flags); 1771 spin_lock_irqsave(&gpio_lock, flags);
1757 1772
1758 if (!desc)
1759 goto fail;
1760 chip = desc->chip; 1773 chip = desc->chip;
1761 if (!chip || !chip->set || !chip->set_debounce) 1774 if (!chip || !chip->set || !chip->set_debounce)
1762 goto fail; 1775 goto fail;
@@ -1776,13 +1789,9 @@ static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1776 1789
1777fail: 1790fail:
1778 spin_unlock_irqrestore(&gpio_lock, flags); 1791 spin_unlock_irqrestore(&gpio_lock, flags);
1779 if (status) { 1792 if (status)
1780 int gpio = -1; 1793 pr_debug("%s: gpio-%d status %d\n", __func__,
1781 if (desc) 1794 desc_to_gpio(desc), status);
1782 gpio = desc_to_gpio(desc);
1783 pr_debug("%s: gpio-%d status %d\n",
1784 __func__, gpio, status);
1785 }
1786 1795
1787 return status; 1796 return status;
1788} 1797}
@@ -1824,12 +1833,14 @@ EXPORT_SYMBOL_GPL(gpio_set_debounce);
1824 * It returns the zero or nonzero value provided by the associated 1833 * It returns the zero or nonzero value provided by the associated
1825 * gpio_chip.get() method; or zero if no such method is provided. 1834 * gpio_chip.get() method; or zero if no such method is provided.
1826 */ 1835 */
1827static int gpiod_get_value(struct gpio_desc *desc) 1836static int gpiod_get_value(const struct gpio_desc *desc)
1828{ 1837{
1829 struct gpio_chip *chip; 1838 struct gpio_chip *chip;
1830 int value; 1839 int value;
1831 int offset; 1840 int offset;
1832 1841
1842 if (!desc)
1843 return 0;
1833 chip = desc->chip; 1844 chip = desc->chip;
1834 offset = gpio_chip_hwgpio(desc); 1845 offset = gpio_chip_hwgpio(desc);
1835 /* Should be using gpio_get_value_cansleep() */ 1846 /* Should be using gpio_get_value_cansleep() */
@@ -1912,6 +1923,8 @@ static void gpiod_set_value(struct gpio_desc *desc, int value)
1912{ 1923{
1913 struct gpio_chip *chip; 1924 struct gpio_chip *chip;
1914 1925
1926 if (!desc)
1927 return;
1915 chip = desc->chip; 1928 chip = desc->chip;
1916 /* Should be using gpio_set_value_cansleep() */ 1929 /* Should be using gpio_set_value_cansleep() */
1917 WARN_ON(chip->can_sleep); 1930 WARN_ON(chip->can_sleep);
@@ -1938,8 +1951,10 @@ EXPORT_SYMBOL_GPL(__gpio_set_value);
1938 * This is used directly or indirectly to implement gpio_cansleep(). It 1951 * This is used directly or indirectly to implement gpio_cansleep(). It
1939 * returns nonzero if access reading or writing the GPIO value can sleep. 1952 * returns nonzero if access reading or writing the GPIO value can sleep.
1940 */ 1953 */
1941static int gpiod_cansleep(struct gpio_desc *desc) 1954static int gpiod_cansleep(const struct gpio_desc *desc)
1942{ 1955{
1956 if (!desc)
1957 return 0;
1943 /* only call this on GPIOs that are valid! */ 1958 /* only call this on GPIOs that are valid! */
1944 return desc->chip->can_sleep; 1959 return desc->chip->can_sleep;
1945} 1960}
@@ -1959,11 +1974,13 @@ EXPORT_SYMBOL_GPL(__gpio_cansleep);
1959 * It returns the number of the IRQ signaled by this (input) GPIO, 1974 * It returns the number of the IRQ signaled by this (input) GPIO,
1960 * or a negative errno. 1975 * or a negative errno.
1961 */ 1976 */
1962static int gpiod_to_irq(struct gpio_desc *desc) 1977static int gpiod_to_irq(const struct gpio_desc *desc)
1963{ 1978{
1964 struct gpio_chip *chip; 1979 struct gpio_chip *chip;
1965 int offset; 1980 int offset;
1966 1981
1982 if (!desc)
1983 return -EINVAL;
1967 chip = desc->chip; 1984 chip = desc->chip;
1968 offset = gpio_chip_hwgpio(desc); 1985 offset = gpio_chip_hwgpio(desc);
1969 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; 1986 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
@@ -1980,13 +1997,15 @@ EXPORT_SYMBOL_GPL(__gpio_to_irq);
1980 * Common examples include ones connected to I2C or SPI chips. 1997 * Common examples include ones connected to I2C or SPI chips.
1981 */ 1998 */
1982 1999
1983static int gpiod_get_value_cansleep(struct gpio_desc *desc) 2000static int gpiod_get_value_cansleep(const struct gpio_desc *desc)
1984{ 2001{
1985 struct gpio_chip *chip; 2002 struct gpio_chip *chip;
1986 int value; 2003 int value;
1987 int offset; 2004 int offset;
1988 2005
1989 might_sleep_if(extra_checks); 2006 might_sleep_if(extra_checks);
2007 if (!desc)
2008 return 0;
1990 chip = desc->chip; 2009 chip = desc->chip;
1991 offset = gpio_chip_hwgpio(desc); 2010 offset = gpio_chip_hwgpio(desc);
1992 value = chip->get ? chip->get(chip, offset) : 0; 2011 value = chip->get ? chip->get(chip, offset) : 0;
@@ -2005,6 +2024,8 @@ static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
2005 struct gpio_chip *chip; 2024 struct gpio_chip *chip;
2006 2025
2007 might_sleep_if(extra_checks); 2026 might_sleep_if(extra_checks);
2027 if (!desc)
2028 return;
2008 chip = desc->chip; 2029 chip = desc->chip;
2009 trace_gpio_value(desc_to_gpio(desc), 0, value); 2030 trace_gpio_value(desc_to_gpio(desc), 0, value);
2010 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 2031 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))