Skip to contentSkip to conversation

gpiolib: Add "unknown" direction support

PATCH Superseded Peter Tyser 7 participants 18 messages Feb 17, 2011 – Feb 21, 2011

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/4 gpiolib: Add "unknown" direction support

Previously, gpiolib would unconditionally flag all GPIO pins as inputs, regardless of their true state. This resulted in all GPIO output pins initially being incorrectly identified as "input" in the GPIO sysfs.

Since the direction of GPIOs is not known prior to having their direction set, instead set the default direction to "unknown" to prevent user confusion. A pin with an "unknown" direction can not be written or read via sysfs; it must first be configured as an input or output before it can be used.

While we're playing with the direction flag in/out defines, rename them to a more descriptive FLAG_DIR_* format.

Cc Alek Du <[email protected]>
Cc Samuel Ortiz <[email protected]>
Cc David Brownell <[email protected]>
Cc Eric Miao <[email protected]>
Cc Uwe Kleine-K?nig <[email protected]>
Cc Mark Brown <[email protected]>
Cc Joe Perches <[email protected]>
Cc Alan Cox <[email protected]>
Cc Grant Likely <[email protected]>

---
Change since V1:
- This patch is new and is based on feedback from Alan to support a new "unknown" direction.
- Rename FLAG_IS_OUT to FLAG_DIR_OUT, and add FLAG_DIR_IN

 drivers/gpio/gpiolib.c |   82 +++++++++++++++++++++++++++--------------------
 1 files changed, 47 insertions(+), 35 deletions(-)
drivers/gpio/gpiolib.c +50 -41
index 649550e..0113c10 100644
@@ -49,13 +49,14 @@ struct gpio_desc {
 	unsigned long		flags;
 /* flag symbols are bit numbers */
 #define FLAG_REQUESTED	0
-#define FLAG_IS_OUT	1
-#define FLAG_RESERVED	2
-#define FLAG_EXPORT	3	/* protected by sysfs_lock */
-#define FLAG_SYSFS	4	/* exported via /sys/class/gpio/control */
-#define FLAG_TRIG_FALL	5	/* trigger on falling edge */
-#define FLAG_TRIG_RISE	6	/* trigger on rising edge */
-#define FLAG_ACTIVE_LOW	7	/* sysfs value has active low */
+#define FLAG_DIR_OUT	1	/* pin is an output */
+#define FLAG_DIR_IN	2	/* pin is an input */
+#define FLAG_RESERVED	3
+#define FLAG_EXPORT	4	/* protected by sysfs_lock */
+#define FLAG_SYSFS	5	/* exported via /sys/class/gpio/control */
+#define FLAG_TRIG_FALL	6	/* trigger on falling edge */
+#define FLAG_TRIG_RISE	7	/* trigger on rising edge */
+#define FLAG_ACTIVE_LOW	8	/* sysfs value has active low */
 
 #define ID_SHIFT	16	/* add new flags before this one */
 
@@ -220,15 +221,22 @@ static ssize_t gpio_direction_show(struct device *dev,
 {
 	const struct gpio_desc	*desc = dev_get_drvdata(dev);
 	ssize_t			status;
+	const char		*dir_str;
 
 	mutex_lock(&sysfs_lock);
 
-	if (!test_bit(FLAG_EXPORT, &desc->flags))
+	if (!test_bit(FLAG_EXPORT, &desc->flags)) {
 		status = -EIO;
-	else
-		status = sprintf(buf, "%s\n",
-			test_bit(FLAG_IS_OUT, &desc->flags)
-				? "out" : "in");
+	} else {
+		if (test_bit(FLAG_DIR_OUT, &desc->flags))
+			dir_str = "out";
+		else if (test_bit(FLAG_DIR_IN, &desc->flags))
+			dir_str = "in";
+		else
+			dir_str = "unknown";
+
+		status = sprintf(buf, "%s\n", dir_str);
+	}
 
 	mutex_unlock(&sysfs_lock);
 	return status;
@@ -272,6 +280,9 @@ static ssize_t gpio_value_show(struct device *dev,
 
 	if (!test_bit(FLAG_EXPORT, &desc->flags)) {
 		status = -EIO;
+	} else if ((!test_bit(FLAG_DIR_OUT, &desc->flags)) &&
+			(!test_bit(FLAG_DIR_IN, &desc->flags))) {
+		status = -EPERM;
 	} else {
 		int value;
 
@@ -297,7 +308,7 @@ static ssize_t gpio_value_store(struct device *dev,
 
 	if (!test_bit(FLAG_EXPORT, &desc->flags))
 		status = -EIO;
-	else if (!test_bit(FLAG_IS_OUT, &desc->flags))
+	else if (!test_bit(FLAG_DIR_OUT, &desc->flags))
 		status = -EPERM;
 	else {
 		long		value;
@@ -741,7 +752,7 @@ int gpio_export(unsigned gpio, bool direction_may_change)
 
 			if (!status && gpio_to_irq(gpio) >= 0
 					&& (direction_may_change
-						|| !test_bit(FLAG_IS_OUT,
+						|| test_bit(FLAG_DIR_IN,
 							&desc->flags)))
 				status = device_create_file(dev,
 						&dev_attr_edge);
@@ -1059,22 +1070,10 @@ int gpiochip_add(struct gpio_chip *chip)
 			break;
 		}
 	}
-	if (status == 0) {
-		for (id = base; id < base + chip->ngpio; id++) {
+	if (status == 0)
+		for (id = base; id < base + chip->ngpio; id++)
 			gpio_desc[id].chip = chip;
 
-			/* REVISIT:  most hardware initializes GPIOs as
-			 * inputs (often with pullups enabled) so power
-			 * usage is minimized.  Linux code should set the
-			 * gpio direction first thing; but until it does,
-			 * we may expose the wrong direction in sysfs.
-			 */
-			gpio_desc[id].flags = !chip->direction_input
-				? (1 << FLAG_IS_OUT)
-				: 0;
-		}
-	}
-
 	of_gpiochip_add(chip);
 
 unlock:
@@ -1402,8 +1401,10 @@ int gpio_direction_input(unsigned gpio)
 	}
 
 	status = chip->direction_input(chip, gpio);
-	if (status == 0)
-		clear_bit(FLAG_IS_OUT, &desc->flags);
+	if (status == 0) {
+		clear_bit(FLAG_DIR_OUT, &desc->flags);
+		set_bit(FLAG_DIR_IN, &desc->flags);
+	}
 lose:
 	return status;
 fail:
@@ -1455,8 +1456,10 @@ int gpio_direction_output(unsigned gpio, int value)
 	}
 
 	status = chip->direction_output(chip, gpio, value);
-	if (status == 0)
-		set_bit(FLAG_IS_OUT, &desc->flags);
+	if (status == 0) {
+		clear_bit(FLAG_DIR_IN, &desc->flags);
+		set_bit(FLAG_DIR_OUT, &desc->flags);
+	}
 lose:
 	return status;
 fail:
@@ -1643,21 +1646,27 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
 	unsigned		i;
 	unsigned		gpio = chip->base;
 	struct gpio_desc	*gdesc = &gpio_desc[gpio];
-	int			is_out;
+	const char		*dir_str;
+	int			unknown = 0;
 
 	for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
 		if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
 			continue;
 
-		is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
-		seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
-			gpio, gdesc->label,
-			is_out ? "out" : "in ",
-			chip->get
-				? (chip->get(chip, i) ? "hi" : "lo")
-				: "?  ");
+		if (test_bit(FLAG_DIR_IN, &gdesc->flags)) {
+			dir_str = "in ";
+		} else if (test_bit(FLAG_DIR_OUT, &gdesc->flags)) {
+			dir_str = "out";
+		} else {
+			dir_str = "?  ";
+			unknown = 1;
+		}
+
+		seq_printf(s, " gpio-%-3d (%-20.20s) %s %s", gpio, gdesc->label,
+			dir_str, (chip->get && !unknown) ?
+			(chip->get(chip, i) ? "hi" : "lo") : "?");
 
-		if (!is_out) {
+		if (test_bit(FLAG_DIR_IN, &gdesc->flags)) {
 			int		irq = gpio_to_irq(gpio);
 			struct irq_desc	*desc = irq_to_desc(irq);
 
Signature · 1 line
1.7.0.4
#1 lore raw

PATCH v2 2/4 gpiolib: Add ability to get GPIO pin direction

Add a new get_direction() function to the gpio_chip structure. This is useful so that the direction of a pin can be determined when its initially exported. Previously, the direction defaulted to "unknown" regardless of the actual configuration of the GPIO pin.

If a GPIO driver implements get_direction(), it is called in gpio_request() to set the initial direction of the pin accurately.

Cc Alek Du <[email protected]>
Cc Samuel Ortiz <[email protected]>
Cc David Brownell <[email protected]>
Cc Eric Miao <[email protected]>
Cc Uwe Kleine-K?nig <[email protected]>
Cc Mark Brown <[email protected]>
Cc Joe Perches <[email protected]>
Cc Alan Cox <[email protected]>
Cc Grant Likely <[email protected]>

---
Changes since v1:
- Add support for "unknown" direction

 drivers/gpio/gpiolib.c     |   20 ++++++++++++++++++++
 include/asm-generic/gpio.h |    4 ++++
 2 files changed, 24 insertions(+), 0 deletions(-)
drivers/gpio/gpiolib.c +20 -0
index 0113c10..7723beb 100644
@@ -1214,6 +1214,26 @@ int gpio_request(unsigned gpio, const char *label)
 		}
 	}
 
+	if (chip->get_direction) {
+		/* chip->get_direction may sleep */
+		spin_unlock_irqrestore(&gpio_lock, flags);
+		switch (chip->get_direction(chip, gpio - chip->base)) {
+		case -1:
+			clear_bit(FLAG_DIR_OUT, &desc->flags);
+			clear_bit(FLAG_DIR_IN, &desc->flags);
+			break;
+		case 0:
+			set_bit(FLAG_DIR_IN, &desc->flags);
+			clear_bit(FLAG_DIR_OUT, &desc->flags);
+			break;
+		case 1:
+			set_bit(FLAG_DIR_OUT, &desc->flags);
+			clear_bit(FLAG_DIR_IN, &desc->flags);
+			break;
+		}
+		spin_lock_irqsave(&gpio_lock, flags);
+	}
+
 done:
 	if (status)
 		pr_debug("gpio_request: gpio-%d (%s) status %d\n",
include/asm-generic/gpio.h +4 -0
index ff5c660..6af5a3e 100644
@@ -57,6 +57,8 @@ struct device_node;
  * @direction_input: configures signal "offset" as input, or returns error
  * @get: returns value for signal "offset"; for output signals this
  *	returns either the value actually sensed, or zero
+ * @get_direction: optional hook to determine if a GPIO signal is configured
+ *	as an input (0) or output (1), or unknown (-1)
  * @direction_output: configures signal "offset" as output, or returns error
  * @set: assigns output value for signal "offset"
  * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
@@ -101,6 +103,8 @@ struct gpio_chip {
 						unsigned offset);
 	int			(*get)(struct gpio_chip *chip,
 						unsigned offset);
+	int			(*get_direction)(struct gpio_chip *chip,
+						unsigned offset);
 	int			(*direction_output)(struct gpio_chip *chip,
 						unsigned offset, int value);
 	int			(*set_debounce)(struct gpio_chip *chip,
Signature · 1 line
1.7.0.4
#2 lore raw

Re: [PATCH v2 2/4] gpiolib: Add ability to get GPIO pin direction

Quoted 31 lines · Peter Tyser

Add a new get_direction() function to the gpio_chip structure. This is useful so that the direction of a pin can be determined when its initially exported. Previously, the direction defaulted to "unknown" regardless of the actual configuration of the GPIO pin.

If a GPIO driver implements get_direction(), it is called in gpio_request() to set the initial direction of the pin accurately.

Cc Alek Du <[email protected]>
Cc Samuel Ortiz <[email protected]>
Cc David Brownell <[email protected]>
Cc Eric Miao <[email protected]>
Cc Uwe Kleine-K?nig <[email protected]>
Cc Mark Brown <[email protected]>
Cc Joe Perches <[email protected]>
Cc Alan Cox <[email protected]>
Cc Grant Likely <[email protected]>

---
Changes since v1:
- Add support for "unknown" direction

 drivers/gpio/gpiolib.c     |   20 ++++++++++++++++++++
 include/asm-generic/gpio.h |    4 ++++
 2 files changed, 24 insertions(+), 0 deletions(-)
drivers/gpio/gpiolib.c +0 -0
index 0113c10..7723beb 100644

drivers/gpio/gpiolib.c int gpio_request(unsigned gpio, const char *label) · line 1221

Quoted 8 lines
drivers/gpio/gpiolib.c +5 -0
@@ -1214,6 +1214,26 @@ int gpio_request(unsigned gpio, const char *label)
 		}
 	}
 
+	if (chip->get_direction) {
+		/* chip->get_direction may sleep */
+		spin_unlock_irqrestore(&gpio_lock, flags);
+		switch (chip->get_direction(chip, gpio - chip->base)) {
+		case -1:

Use some #defines instead of hardcoded values?

drivers/gpio/gpiolib.c int gpio_request(unsigned gpio, const char *label) · line 1234

Quoted 13 lines
drivers/gpio/gpiolib.c +13 -0
+			clear_bit(FLAG_DIR_OUT, &desc->flags);
+			clear_bit(FLAG_DIR_IN, &desc->flags);
+			break;
+		case 0:
+			set_bit(FLAG_DIR_IN, &desc->flags);
+			clear_bit(FLAG_DIR_OUT, &desc->flags);
+			break;
+		case 1:
+			set_bit(FLAG_DIR_OUT, &desc->flags);
+			clear_bit(FLAG_DIR_IN, &desc->flags);
+			break;
+		}
+		spin_lock_irqsave(&gpio_lock, flags);

You are changing the flags without the lock. I think it would be good to copy the locking-pattern of chip->request for now.

Regards,

Wolfram

Signature · 2 lines
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#3 lore raw

PATCH v2 3/4 gpio: pca953x: Implement get_direction() hook

In reply to Peter Tyser

Adding the get_direction() function allows the accurate GPIO pin direction data to be shown in sysfs. Previously, the direction was was always initialized to 'unknown'.

Cc Alek Du <[email protected]>
Cc Samuel Ortiz <[email protected]>
Cc David Brownell <[email protected]>
Cc Eric Miao <[email protected]>
Cc Uwe Kleine-K?nig <[email protected]>
Cc Mark Brown <[email protected]>
Cc Joe Perches <[email protected]>
Cc Alan Cox <[email protected]>
Cc Grant Likely <[email protected]>

---
Changes since v1:
- None

 drivers/gpio/pca953x.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
drivers/gpio/pca953x.c +11 -0
index b473429..66d36b1 100644
@@ -168,6 +168,16 @@ exit:
 	return ret;
 }
 
+static int pca953x_gpio_get_direction(struct gpio_chip *gc, unsigned off)
+{
+	struct pca953x_chip *chip;
+
+	chip = container_of(gc, struct pca953x_chip, gpio_chip);
+
+	/* return 0 if IO pin is input, 1 if its an output */
+	return chip->reg_direction & (1u << off) ? 0 : 1;
+}
+
 static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
 {
 	struct pca953x_chip *chip;
@@ -221,6 +231,7 @@ static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
 
 	gc->direction_input  = pca953x_gpio_direction_input;
 	gc->direction_output = pca953x_gpio_direction_output;
+	gc->get_direction = pca953x_gpio_get_direction;
 	gc->get = pca953x_gpio_get_value;
 	gc->set = pca953x_gpio_set_value;
 	gc->can_sleep = 1;
Signature · 1 line
1.7.0.4
#4 lore raw

PATCH v2 4/4 gpio: Add support for Intel ICHx/3100/Series[56] GPIO

In reply to Peter Tyser

This driver works on many Intel chipsets, including the ICH6, ICH7, ICH8, ICH9, ICH10, 3100, Series 5/3400 (Ibex Peak), Series 6/C200 (Cougar Point).

Additional Intel chipsets should be easily supported if needed, eg the ICH1-5, EP80579, etc.

Tested on a QM57 (Ibex Peak) and 3100.

Cc Alek Du <[email protected]>
Cc Samuel Ortiz <[email protected]>
Cc David Brownell <[email protected]>
Cc Eric Miao <[email protected]>
Cc Uwe Kleine-K?nig <[email protected]>
Cc Mark Brown <[email protected]>
Cc Joe Perches <[email protected]>
Cc Alan Cox <[email protected]>
Cc Grant Likely <[email protected]>

---
Changes since v1:
- Fixed grammar in Kconfig menu entry

 MAINTAINERS              |    5 +
 drivers/gpio/Kconfig     |   11 +
 drivers/gpio/Makefile    |    1 +
 drivers/gpio/ichx_gpio.c |  551 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 568 insertions(+), 0 deletions(-)
 create mode 100644 drivers/gpio/ichx_gpio.c
MAINTAINERS +5 -0
index 4837907..1778396 100644
@@ -3108,6 +3108,11 @@ W:	http://www.developer.ibm.com/welcome/netfinity/serveraid.html
 S:	Supported
 F:	drivers/scsi/ips.*
 
+ICHX GPIO DRIVER
+M:	Peter Tyser <[email protected]>
+S:	Maintained
+F:	drivers/gpio/ichx_gpio.c
+
 IDE SUBSYSTEM
 M:	"David S. Miller" <[email protected]>
 L:	[email protected]
drivers/gpio/Kconfig +11 -0
index 664660e..13b7998 100644
@@ -319,6 +319,17 @@ config GPIO_BT8XX
 
 	  If unsure, say N.
 
+config GPIO_ICHX
+	tristate "Intel ICHx GPIO"
+	depends on PCI && GPIOLIB && X86
+	help
+	  Say yes here to support the GPIO functionality of a number of Intel
+	  ICH-based chipsets.  Currently supported devices: ICH6, ICH7, ICH8
+	  ICH9, ICH10, Series 5/3400 (eg Ibex Peak), Series 6/C200 (eg
+	  Cougar Point), and 3100.
+
+	  If unsure, say N.
+
 config GPIO_LANGWELL
 	bool "Intel Langwell/Penwell GPIO support"
 	depends on PCI
drivers/gpio/Makefile +1 -0
index 3351cf8..8900bc6 100644
@@ -11,6 +11,7 @@ obj-$(CONFIG_GPIOLIB)		+= gpiolib.o
 obj-$(CONFIG_GPIO_ADP5520)	+= adp5520-gpio.o
 obj-$(CONFIG_GPIO_ADP5588)	+= adp5588-gpio.o
 obj-$(CONFIG_GPIO_BASIC_MMIO)	+= basic_mmio_gpio.o
+obj-$(CONFIG_GPIO_ICHX)		+= ichx_gpio.o
 obj-$(CONFIG_GPIO_LANGWELL)	+= langwell_gpio.o
 obj-$(CONFIG_GPIO_MAX730X)	+= max730x.o
 obj-$(CONFIG_GPIO_MAX7300)	+= max7300.o
drivers/gpio/ichx_gpio.c new file +551 -0

551 lines not on this page · raw message

Signature · 1 line
1.7.0.4
#5 lore raw
In reply to Peter Tyser
Quoted 10 lines · Peter Tyser

Previously, gpiolib would unconditionally flag all GPIO pins as inputs, regardless of their true state.  This resulted in all GPIO output pins initially being incorrectly identified as "input" in the GPIO sysfs.

Since the direction of GPIOs is not known prior to having their direction set, instead set the default direction to "unknown" to prevent user confusion.  A pin with an "unknown" direction can not be written or read via sysfs; it must first be configured as an input or output before it can be used.

Hrm... that's why I don't like the original definition of gpio_request() which is vague on the pin configurations. The pin configuration should be clear upon requesting, otherwise it's a potential issue.

Anyway, this unknown state looks to be a good mitigation to this underlying problem. I'm good with it.

Quoted 193 lines

While we're playing with the direction flag in/out defines, rename them to a more descriptive FLAG_DIR_* format.

Cc Alek Du <[email protected]>
Cc Samuel Ortiz <[email protected]>
Cc David Brownell <[email protected]>
Cc Eric Miao <[email protected]>
Cc Uwe Kleine-K?nig <[email protected]>
Cc Mark Brown <[email protected]>
Cc Joe Perches <[email protected]>
Cc Alan Cox <[email protected]>
Cc Grant Likely <[email protected]>

---
Change since V1:
- This patch is new and is based on feedback from Alan to support a new "unknown" direction.
- Rename FLAG_IS_OUT to FLAG_DIR_OUT, and add FLAG_DIR_IN

 drivers/gpio/gpiolib.c |   82 +++++++++++++++++++++++++++--------------------

 1 files changed, 47 insertions(+), 35 deletions(-)
drivers/gpio/gpiolib.c +24 -13
index 649550e..0113c10 100644
@@ -49,13 +49,14 @@ struct gpio_desc {
        unsigned long           flags;
  /* flag symbols are bit numbers */
  #define FLAG_REQUESTED 0
-#define FLAG_IS_OUT    1
-#define FLAG_RESERVED  2
-#define FLAG_EXPORT    3       /* protected by sysfs_lock */
-#define FLAG_SYSFS     4       /* exported via /sys/class/gpio/control */
-#define FLAG_TRIG_FALL 5       /* trigger on falling edge */
-#define FLAG_TRIG_RISE 6       /* trigger on rising edge */
-#define FLAG_ACTIVE_LOW        7       /* sysfs value has active low */
+#define FLAG_DIR_OUT   1       /* pin is an output */
+#define FLAG_DIR_IN    2       /* pin is an input */
+#define FLAG_RESERVED  3
+#define FLAG_EXPORT    4       /* protected by sysfs_lock */
+#define FLAG_SYSFS     5       /* exported via /sys/class/gpio/control */
+#define FLAG_TRIG_FALL 6       /* trigger on falling edge */
+#define FLAG_TRIG_RISE 7       /* trigger on rising edge */
+#define FLAG_ACTIVE_LOW        8       /* sysfs value has active low */
 
  #define ID_SHIFT       16      /* add new flags before this one */
 
@@ -220,15 +221,22 @@ static ssize_t gpio_direction_show(struct device *dev,
  {
        const struct gpio_desc  *desc = dev_get_drvdata(dev);
        ssize_t                 status;
+       const char              *dir_str;
 
        mutex_lock(&sysfs_lock);
 
-       if (!test_bit(FLAG_EXPORT, &desc->flags))
+       if (!test_bit(FLAG_EXPORT, &desc->flags)) {
                status = -EIO;
-       else
-               status = sprintf(buf, "%s\n",
-                       test_bit(FLAG_IS_OUT, &desc->flags)
-                               ? "out" : "in");
+       } else {
+               if (test_bit(FLAG_DIR_OUT, &desc->flags))
+                       dir_str = "out";
+               else if (test_bit(FLAG_DIR_IN, &desc->flags))
+                       dir_str = "in";
+               else
+                       dir_str = "unknown";
+
+               status = sprintf(buf, "%s\n", dir_str);
+       }
 
        mutex_unlock(&sysfs_lock);
        return status;
@@ -272,6 +280,9 @@ static ssize_t gpio_value_show(struct device *dev,
 
        if (!test_bit(FLAG_EXPORT, &desc->flags)) {
                status = -EIO;
+       } else if ((!test_bit(FLAG_DIR_OUT, &desc->flags)) &&
+                       (!test_bit(FLAG_DIR_IN, &desc->flags))) {
+               status = -EPERM;
        } else {
                int value;
 
@@ -297,7 +308,7 @@ static ssize_t gpio_value_store(struct device *dev,
 
        if (!test_bit(FLAG_EXPORT, &desc->flags))
                status = -EIO;
-       else if (!test_bit(FLAG_IS_OUT, &desc->flags))
+       else if (!test_bit(FLAG_DIR_OUT, &desc->flags))
                status = -EPERM;
        else {
                long            value;
@@ -741,7 +752,7 @@ int gpio_export(unsigned gpio, bool direction_may_change)
 

                       if (!status && gpio_to_irq(gpio) >= 0 && (direction_may_change

-                                               || !test_bit(FLAG_IS_OUT,
+                                               || test_bit(FLAG_DIR_IN,

                                                       &desc->flags))) status = device_create_file(dev, &dev_attr_edge);

drivers/gpio/gpiolib.c +2 -14
@@ -1059,22 +1070,10 @@ int gpiochip_add(struct gpio_chip *chip)
                        break;
                }
        }
-       if (status == 0) {
-               for (id = base; id < base + chip->ngpio; id++) {
+       if (status == 0)
+               for (id = base; id < base + chip->ngpio; id++)
                        gpio_desc[id].chip = chip;
 
-                       /* REVISIT:  most hardware initializes GPIOs as
-                        * inputs (often with pullups enabled) so power
-                        * usage is minimized.  Linux code should set the
-                        * gpio direction first thing; but until it does,
-                        * we may expose the wrong direction in sysfs.
-                        */
-                       gpio_desc[id].flags = !chip->direction_input
-                               ? (1 << FLAG_IS_OUT)
-                               : 0;
-               }
-       }
-
        of_gpiochip_add(chip);
 

 unlock:

drivers/gpio/gpiolib.c +4 -2
@@ -1402,8 +1401,10 @@ int gpio_direction_input(unsigned gpio)
        }
 
        status = chip->direction_input(chip, gpio);
-       if (status == 0)
-               clear_bit(FLAG_IS_OUT, &desc->flags);
+       if (status == 0) {
+               clear_bit(FLAG_DIR_OUT, &desc->flags);
+               set_bit(FLAG_DIR_IN, &desc->flags);
+       }

 lose:
       return status;
 fail:

drivers/gpio/gpiolib.c +4 -2
@@ -1455,8 +1456,10 @@ int gpio_direction_output(unsigned gpio, int value)
        }
 
        status = chip->direction_output(chip, gpio, value);
-       if (status == 0)
-               set_bit(FLAG_IS_OUT, &desc->flags);
+       if (status == 0) {
+               clear_bit(FLAG_DIR_IN, &desc->flags);
+               set_bit(FLAG_DIR_OUT, &desc->flags);
+       }

 lose:
       return status;
 fail:

drivers/gpio/gpiolib.c +15 -10
@@ -1643,21 +1646,27 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
        unsigned                i;
        unsigned                gpio = chip->base;
        struct gpio_desc        *gdesc = &gpio_desc[gpio];
-       int                     is_out;
+       const char              *dir_str;
+       int                     unknown = 0;
 
        for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
                if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
                        continue;
 
-               is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
-               seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
-                       gpio, gdesc->label,
-                       is_out ? "out" : "in ",
-                       chip->get
-                               ? (chip->get(chip, i) ? "hi" : "lo")
-                               : "?  ");
+               if (test_bit(FLAG_DIR_IN, &gdesc->flags)) {
+                       dir_str = "in ";
+               } else if (test_bit(FLAG_DIR_OUT, &gdesc->flags)) {
+                       dir_str = "out";
+               } else {
+                       dir_str = "?  ";
+                       unknown = 1;
+               }
+
+               seq_printf(s, " gpio-%-3d (%-20.20s) %s %s", gpio, gdesc->label,
+                       dir_str, (chip->get && !unknown) ?
+                       (chip->get(chip, i) ? "hi" : "lo") : "?");
 
-               if (!is_out) {
+               if (test_bit(FLAG_DIR_IN, &gdesc->flags)) {
                        int             irq = gpio_to_irq(gpio);
                        struct irq_desc *desc = irq_to_desc(irq);
 
--
Quoted 3 lines

1.7.0.4

���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

#6 lore raw
Quoted 12 lines · Eric Miao
Quoted 10 lines · Peter Tyser

Previously, gpiolib would unconditionally flag all GPIO pins as inputs, regardless of their true state.  This resulted in all GPIO output pins initially being incorrectly identified as "input" in the GPIO sysfs.

Since the direction of GPIOs is not known prior to having their direction set, instead set the default direction to "unknown" to prevent user confusion.  A pin with an "unknown" direction can not be written or read via sysfs; it must first be configured as an input or output before it can be used.

Quoted 3 lines

Hrm... that's why I don't like the original definition of gpio_request() which is vague on the pin configurations. The pin configuration should be clear upon requesting, otherwise it's a potential issue.

I don't know (state: "unknown" :-) what you would prefer here. An implicit gpio_direction_input()? Or gpio_reserve_and_direction_input()?

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#7 lore raw
In reply to Eric Miao
Quoted 12 lines · Eric Miao
Quoted 10 lines · Peter Tyser

Previously, gpiolib would unconditionally flag all GPIO pins as inputs, regardless of their true state. This resulted in all GPIO output pins initially being incorrectly identified as "input" in the GPIO sysfs.

Since the direction of GPIOs is not known prior to having their direction set, instead set the default direction to "unknown" to prevent user confusion. A pin with an "unknown" direction can not be written or read via sysfs; it must first be configured as an input or output before it can be used.

Quoted 2 lines

Hrm... that's why I don't like the original definition of gpio_request() which is vague on the pin configurations.

Actually it doesn't say anything at all about the current configuration at all. Requesting a pin grants you exclusive access to that pin, if it succeeds. So it is solely about ownership and not about configuration.
Once you own a pin you are allowed to modify its configuration, otherwise you should never touch it. And you shouldn't make any assumptions about its previous configuration, if you want to use it as input you should explicitly configure it as an input pin.

Quoted 2 lines

The pin configuration should be clear upon requesting, otherwise it's a potential issue.

How so?

Quoted 4 lines

Anyway, this unknown state looks to be a good mitigation to this underlying problem. I'm good with it.

- Lars

#8 lore raw
Quoted 12 lines · On Thursday 17 February 2011, 13:03:54 Lars-Peter Clausen wrote:
Quoted 11 lines · Eric Miao
Quoted 9 lines · Peter Tyser

Previously, gpiolib would unconditionally flag all GPIO pins as inputs, regardless of their true state. This resulted in all GPIO output pins initially being incorrectly identified as "input" in the GPIO sysfs.

Since the direction of GPIOs is not known prior to having their direction set, instead set the default direction to "unknown" to prevent user confusion. A pin with an "unknown" direction can not be written or read via sysfs; it must first be configured as an input or output before it can be used.

Quoted 6 lines
Quoted 2 lines

Hrm... that's why I don't like the original definition of gpio_request() which is vague on the pin configurations.

Actually it doesn't say anything at all about the current configuration at all. Requesting a pin grants you exclusive access to that pin, if it succeeds. So it is solely about ownership and not about configuration.

Well, ownership is a bit misleading here. You must request a GPIO to change its direction. But to set or get a value this isn't required.
In general one could expect if you requested a GPIO you are the only one to call any function on it. On the other hand, this may be bad in some situations where you want to read a GPIO value from different places.

Alexander

#9 lore raw
Quoted 16 lines · Alexander Stein
Quoted 15 lines · On Thursday 17 February 2011, 13:03:54 Lars-Peter Clausen wrote:
Quoted 13 lines · Eric Miao
Quoted 9 lines · Peter Tyser

Previously, gpiolib would unconditionally flag all GPIO pins as inputs, regardless of their true state. This resulted in all GPIO output pins initially being incorrectly identified as "input" in the GPIO sysfs.

Since the direction of GPIOs is not known prior to having their direction set, instead set the default direction to "unknown" to prevent user confusion. A pin with an "unknown" direction can not be written or read via sysfs; it must first be configured as an input or output before it can be used.

Hrm... that's why I don't like the original definition of gpio_request() which is vague on the pin configurations.

Quoted 6 lines
Quoted 3 lines

Actually it doesn't say anything at all about the current configuration at all. Requesting a pin grants you exclusive access to that pin, if it succeeds. So it is solely about ownership and not about configuration.

Well, ownership is a bit misleading here. You must request a GPIO to change its direction. But to set or get a value this isn't required.

Well, it is not enforced in the actual code, but the GPIO API is based on convention and I would consider it a misusage of the API to call gpio_{set,get}_value without requesting the pin and having configured its direction prior to it.

Quoted 3 lines

In general one could expect if you requested a GPIO you are the only one to call any function on it. On the other hand, this may be bad in some situations where you want to read a GPIO value from different places.

Sharing GPIOs in read-only mode, is indeed something that is not covered by the GPIO API. It might be worth adding a gpio_request_shared, which would only permit setting the direction to input. Futher gpio_request_shared calls would be allowed but gpio_request calls would fail.

- Lars

#10 lore raw
Quoted 8 lines · On Thursday 17 February 2011, 14:06:06 Lars-Peter Clausen wrote:
Quoted 2 lines

Well, ownership is a bit misleading here. You must request a GPIO to change its direction. But to set or get a value this isn't required.

Well, it is not enforced in the actual code, but the GPIO API is based on convention and I would consider it a misusage of the API to call gpio_{set,get}_value without requesting the pin and having configured its direction prior to it.

Quoted 8 lines
Quoted 3 lines

In general one could expect if you requested a GPIO you are the only one to call any function on it. On the other hand, this may be bad in some situations where you want to read a GPIO value from different places.

Sharing GPIOs in read-only mode, is indeed something that is not covered by the GPIO API. It might be worth adding a gpio_request_shared, which would only permit setting the direction to input. Futher gpio_request_shared calls would be allowed but gpio_request calls would fail.

gpio_request_shared sounds interesting, but in this case an implicit gpio_direction_input call has to be done in the first shared request. But the user must/should not call gpio_direction_input himself which is quite a bit different than using a normal gpio_requested gpio.

Alexander

#11 lore raw
Quoted 7 lines
Quoted 5 lines

Sharing GPIOs in read-only mode, is indeed something that is not covered by the GPIO API. It might be worth adding a gpio_request_shared, which would only permit setting the direction to input. Futher gpio_request_shared calls would be allowed but gpio_request calls would fail.

gpio_request_shared sounds interesting, but in this case an implicit

Can you name a use-case? One reason is that we won't need to implement it if there is no user, another one is that it could potentially weaken abstractions?

Regards,

Wolfram

Signature · 2 lines
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#12 lore raw
Quoted 11 lines · On Monday 21 February 2011, 10:19:16 Wolfram Sang wrote:
Quoted 7 lines
Quoted 5 lines

Sharing GPIOs in read-only mode, is indeed something that is not covered by the GPIO API. It might be worth adding a gpio_request_shared, which would only permit setting the direction to input. Futher gpio_request_shared calls would be allowed but gpio_request calls would fail.

gpio_request_shared sounds interesting, but in this case an implicit

Can you name a use-case? One reason is that we won't need to implement it if there is no user, another one is that it could potentially weaken abstractions?

We had exported our 5V_enable gpio to sysfs to allow a user-space application to enable/disable devices connected to 5V circuit.
But on the other hand we had to read the current status of this gpio in the power-fail interrupt handler to distinguish between false-positive (5V disabled) and a correct detection.
As the sysfs export requests the gpio we cannot gpio_request it in the power- fail driver and just used gpio_get_value without request.
We set the direction already in the machine startup code and never touched it again.

Alexander

#13 lore raw
Quoted 5 lines

We had exported our 5V_enable gpio to sysfs to allow a user-space application to enable/disable devices connected to 5V circuit.
But on the other hand we had to read the current status of this gpio in the power-fail interrupt handler to distinguish between false-positive (5V disabled) and a correct detection.

What about gpio_export() (description in Documentation/gpio.txt)?

Regards,

Wolfram

Signature · 2 lines
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#14 lore raw
Quoted 7 lines · On Monday 21 February 2011, 10:47:56 Wolfram Sang wrote:
Quoted 5 lines

We had exported our 5V_enable gpio to sysfs to allow a user-space application to enable/disable devices connected to 5V circuit. But on the other hand we had to read the current status of this gpio in the power-fail interrupt handler to distinguish between false-positive (5V disabled) and a correct detection.

What about gpio_export() (description in Documentation/gpio.txt)?

Ah, I didn't know about this. I just expected this is only used from sysfs part. But you have to make sure your .ko is loaded before userspace is accessing sysfs and tries to export the GPIO.
Or is it "allowed" by the API convention to gpio_request and gpio_export (and set direction) a GPIO in the machine startup code which will later be used in a different place?

Alexander

#15 lore raw
Quoted 7 lines · Alexander Stein
Quoted 6 lines · On Monday 21 February 2011, 10:47:56 Wolfram Sang wrote:
Quoted 5 lines

We had exported our 5V_enable gpio to sysfs to allow a user-space application to enable/disable devices connected to 5V circuit. But on the other hand we had to read the current status of this gpio in the power-fail interrupt handler to distinguish between false-positive (5V disabled) and a correct detection.

Quoted 5 lines
Quoted 1 line

What about gpio_export() (description in Documentation/gpio.txt)?

Ah, I didn't know about this. I just expected this is only used from sysfs part. But you have to make sure your .ko is loaded before userspace is accessing sysfs and tries to export the GPIO.

Eh? Userspace doesn't export the GPIO in that case.

Quoted 3 lines

Or is it "allowed" by the API convention to gpio_request and gpio_export (and set direction) a GPIO in the machine startup code which will later be used in a different place?

different place = userspace? Well, that's the main intention of gpio_export(). (I have the feeling we are missing each other here, thoguh) I'd suggest looking a bit further in the docs/code. It should make clear what is possible.

Regards,

Wolfram

Signature · 2 lines
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#16 lore raw
Quoted 8 lines · On Monday 21 February 2011, 12:22:40 Wolfram Sang wrote:
Quoted 7 lines · Alexander Stein
Quoted 6 lines · On Monday 21 February 2011, 10:47:56 Wolfram Sang wrote:
Quoted 5 lines

We had exported our 5V_enable gpio to sysfs to allow a user-space application to enable/disable devices connected to 5V circuit. But on the other hand we had to read the current status of this gpio in the power-fail interrupt handler to distinguish between false-positive (5V disabled) and a correct detection.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

What about gpio_export() (description in Documentation/gpio.txt)?

Ah, I didn't know about this. I just expected this is only used from sysfs part. But you have to make sure your .ko is loaded before userspace is accessing sysfs and tries to export the GPIO.

Eh? Userspace doesn't export the GPIO in that case.

Sure, but you have to make sure you have it exported or userspace will fail.

Quoted 8 lines
Quoted 3 lines

Or is it "allowed" by the API convention to gpio_request and gpio_export (and set direction) a GPIO in the machine startup code which will later be used in a different place?

different place = userspace? Well, that's the main intention of gpio_export(). (I have the feeling we are missing each other here, thoguh) I'd suggest looking a bit further in the docs/code. It should make clear what is possible.

No, with different place I mean a kernel module driver which will be loaded later using insmod. In this case this module is just expecting the GPIO has already be exported and set proper direction without requesting the GPIO itself.

Alexander

#17 lore raw
Quoted 28 lines · Alexander Stein
Quoted 8 lines · On Monday 21 February 2011, 12:22:40 Wolfram Sang wrote:
Quoted 7 lines · Alexander Stein
Quoted 6 lines · On Monday 21 February 2011, 10:47:56 Wolfram Sang wrote:
Quoted 5 lines

We had exported our 5V_enable gpio to sysfs to allow a user-space application to enable/disable devices connected to 5V circuit. But on the other hand we had to read the current status of this gpio in the power-fail interrupt handler to distinguish between false-positive (5V disabled) and a correct detection.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

What about gpio_export() (description in Documentation/gpio.txt)?

Ah, I didn't know about this. I just expected this is only used from sysfs part. But you have to make sure your .ko is loaded before userspace is accessing sysfs and tries to export the GPIO.

Eh? Userspace doesn't export the GPIO in that case.

Sure, but you have to make sure you have it exported or userspace will fail.

Quoted 8 lines
Quoted 3 lines

Or is it "allowed" by the API convention to gpio_request and gpio_export (and set direction) a GPIO in the machine startup code which will later be used in a different place?

different place = userspace? Well, that's the main intention of gpio_export(). (I have the feeling we are missing each other here, thoguh) I'd suggest looking a bit further in the docs/code. It should make clear what is possible.

Quoted 4 lines

No, with different place I mean a kernel module driver which will be loaded later using insmod. In this case this module is just expecting the GPIO has already be exported and set proper direction without requesting the GPIO itself.

If the module does the gpio_request, then it should also do the gpio_export (and gpio_unexport/gpio_free on module exit). In this case when the module is not loaded the gpio is not in use by the kernel and user space is able to access it via sysfs. When the module is in use the gpio_export call will make the gpio available via sysfs.

If the gpio_request is done in your platform code, then the gpio_export should also go in the platform code. This way the gpio is always in use by the kernel, but also exported to userspace so it will be available via sysfs.

~Ryan

Signature · 7 lines
Bluewater Systems Ltd - ARM Technology Solution Centre

Ryan Mallon         		5 Amuri Park, 404 Barbadoes St
[email protected]         	PO Box 13 889, Christchurch 8013
http://www.bluewatersys.com	New Zealand
Phone: +64 3 3779127		Freecall: Australia 1800 148 751
Fax:   +64 3 3779135			  USA 1800 261 2934