Skip to contentSkip to conversation

gpio sysfs: add a "toggle" value

PATCH In review Mike Frysinger 4 participants 13 messages Jun 3, 2009 – Jun 4, 2009 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio sysfs: add a "toggle" value

---
 drivers/gpio/gpiolib.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
drivers/gpio/gpiolib.c +6 -1
index 51a8d41..7f79732 100644
@@ -274,7 +274,12 @@ static ssize_t gpio_value_store(struct device *dev,
 	else {
 		long		value;
 
-		status = strict_strtol(buf, 0, &value);
+		if (sysfs_streq(buf, "toggle")) {
+			value = !gpio_get_value_cansleep(gpio);
+			status = 0;
+		} else
+			status = strict_strtol(buf, 0, &value);
+
 		if (status == 0) {
 			gpio_set_value_cansleep(gpio, value != 0);
 			status = size;
Signature · 1 line
1.6.3.1
#1 lore raw
Quoted 1 line · On Wed, 3 Jun 2009 04:16:59 -0400 Mike Frysinger <[email protected]> wrote:

-ENOCHANGELOGAGAIN

Quoted 8 lines
drivers/gpio/gpiolib.c +0 -0
index 51a8d41..7f79732 100644
@@ -274,7 +274,12 @@ static ssize_t gpio_value_store(struct device *dev,
 	else {
 		long		value;
 

drivers/gpio/gpiolib.c static ssize_t gpio_value_store(struct device *dev, · line 285

Quoted 10 lines
drivers/gpio/gpiolib.c +6 -1
-		status = strict_strtol(buf, 0, &value);
+		if (sysfs_streq(buf, "toggle")) {
+			value = !gpio_get_value_cansleep(gpio);
+			status = 0;
+		} else
+			status = strict_strtol(buf, 0, &value);
+
 		if (status == 0) {
 			gpio_set_value_cansleep(gpio, value != 0);
 			status = size;

A suitable place to document this is Documentation/gpio.txt.

#2 lore raw
Quoted 4 lines · Andrew Morton
Quoted 1 line · On Wed,  3 Jun 2009 04:16:59 -0400 Mike Frysinger wrote:

-ENOCHANGELOGAGAIN

it's pretty damned self explanatory

Quoted 8 lines
Quoted 8 lines
drivers/gpio/gpiolib.c +0 -0
index 51a8d41..7f79732 100644
@@ -274,7 +274,12 @@ static ssize_t gpio_value_store(struct device *dev,
       else {
               long            value;
 
Quoted 12 lines

drivers/gpio/gpiolib.c static ssize_t gpio_value_store(struct device *dev, · line 285

Quoted 10 lines
drivers/gpio/gpiolib.c +6 -1
-             status = strict_strtol(buf, 0, &value);
+             if (sysfs_streq(buf, "toggle")) {
+                     value = !gpio_get_value_cansleep(gpio);
+                     status = 0;
+             } else
+                     status = strict_strtol(buf, 0, &value);
+
               if (status == 0) {
                       gpio_set_value_cansleep(gpio, value != 0);
                       status = size;

A suitable place to document this is Documentation/gpio.txt.

yes, but i wanted to make sure David didnt reject the idea before i spend time writing documentation.
-mike

#3 lore raw
Quoted 5 lines · Mike Frysinger
Quoted 2 lines

A suitable place to document this is Documentation/gpio.txt.

yes, but i wanted to make sure David didnt reject the idea before i spend time writing documentation.

My initial reaction was that the function is somewhat superfluous, so having a use case in the patch comment would be good: why can't userspace do the toggle itself?

On the other hand, I find it hard to object to such a small feature. If that's the worst feature bloat in Linux this year, we're overdue for a big party. ;)

#4 lore raw
Quoted 9 lines · David Brownell
Quoted 4 lines · Mike Frysinger
Quoted 1 line

A suitable place to document this is Documentation/gpio.txt.

yes, but i wanted to make sure David didnt reject the idea before i spend time writing documentation.

My initial reaction was that the function is somewhat superfluous, so having a use case in the patch comment would be good:  why can't userspace do the toggle itself?

when people were using the simple-gpio driver i wrote (which had pretty much function parity with the sysfs implementation), i had a customer or two ask for toggle for their systems. i didnt get details for why they wanted a toggle however ... they wanted it in their app and adding it was trivial.

Quoted 3 lines

On the other hand, I find it hard to object to such a small feature. If that's the worst feature bloat in Linux this year, we're overdue for a big party.  ;)

my next request was for a 1 letter command set and chaining operations. say you want to toggle a gpio line 4 times, having to go between kernel and userspace for that seems kind of heavy. echo TTTT > value
-mike

#5 lore raw
Quoted 4 lines · On Thu, 4 Jun 2009 04:52:35 -0400 Mike Frysinger <[email protected]> wrote:

my next request was for a 1 letter command set and chaining operations. say you want to toggle a gpio line 4 times, having to go between kernel and userspace for that seems kind of heavy. echo TTTT > value

I was wondering whether "t" would be better than "toggle".

If you're going to do that, wouldn't you need a "d"(elay) too? To prevent large variations in interval between different platforms. 1 millisec, perhaps.

0d1dd0d1ddd0d1, too.

This could be fun.

#6 lore raw
In reply to Mike Frysinger
Quoted 4 lines · Mike Frysinger

my next request was for a 1 letter command set and chaining operations.  say you want to toggle a gpio line 4 times, having to go between kernel and userspace for that seems kind of heavy. echo TTTT > value

Having that kind of stuff in kernelspace seems wrong; if you're going that route, embed Guile or something. ;)

The "TTTT" thing could as easily be scripted in BASH.

#7 lore raw
Quoted 10 lines · David Brownell
Quoted 4 lines · Mike Frysinger

my next request was for a 1 letter command set and chaining operations.  say you want to toggle a gpio line 4 times, having to go between kernel and userspace for that seems kind of heavy. echo TTTT > value

Having that kind of stuff in kernelspace seems wrong; if you're going that route, embed Guile or something.  ;)

The "TTTT" thing could as easily be scripted in BASH.

the point was to lower the number of user<->kernel transitions. yes, all of this stuff can of course be done in the shell, but it's hard to push limits when the interface is tying your hands and sitting on your back.
-mike

#8 lore raw
Quoted 4 lines · Mike Frysinger

the point was to lower the number of user<->kernel transitions.  yes, all of this stuff can of course be done in the shell, but it's hard to push limits when the interface is tying your hands and sitting on your back.

So what's the application that needs to do all this in userspace yet which can't hook up a quick kernel driver?

I don't like the idea of embedding any kind of interpreter in the sysfs interface, but that's the direction you seem to be heading ...

#9 lore raw
Quoted 6 lines · David Brownell
Quoted 4 lines · Mike Frysinger

the point was to lower the number of user<->kernel transitions.  yes, all of this stuff can of course be done in the shell, but it's hard to push limits when the interface is tying your hands and sitting on your back.

Quoted 6 lines

So what's the application that needs to do all this in userspace yet which can't hook up a quick kernel driver?

I don't like the idea of embedding any kind of interpreter in the sysfs interface, but that's the direction you seem to be heading ...

i dont view the userspace GPIO interface as a simple "i want to wiggle pins for fun and testing". i see it as a dynamic method for building userspace drivers for simple devices. as such, having a very simple "command language" for optimizing the interaction provides a pretty good trade off between kernel complexity and lowering overhead imo. -mike

#10 lore raw
Quoted 7 lines · Mike Frysinger
Quoted 3 lines

I don't like the idea of embedding any kind of interpreter in the sysfs interface, but that's the direction you seem to be heading ...

i dont view the userspace GPIO interface as a simple "i want to wiggle pins for fun and testing".  i see it as a dynamic method for building userspace drivers for simple devices.

I don't. I see it as an adjunct to real drivers. Or maybe we just differ on what "simple" means here.

Quoted 3 lines

as such, having a very simple
"command language" for optimizing the interaction provides a pretty good trade off between kernel complexity and lowering overhead imo.

If that's what you want, why don't you write a custom driver that provides the kind of command interpreter you want?

#11 lore raw
Quoted 12 lines · David Brownell
Quoted 7 lines · Mike Frysinger
Quoted 3 lines

I don't like the idea of embedding any kind of interpreter in the sysfs interface, but that's the direction you seem to be heading ...

i dont view the userspace GPIO interface as a simple "i want to wiggle pins for fun and testing".  i see it as a dynamic method for building userspace drivers for simple devices.

I don't.  I see it as an adjunct to real drivers.  Or maybe we just differ on what "simple" means here.

Quoted 6 lines
Quoted 3 lines

as such, having a very simple
"command language" for optimizing the interaction provides a pretty good trade off between kernel complexity and lowering overhead imo.

If that's what you want, why don't you write a custom driver that provides the kind of command interpreter you want?

i had "simple-gpio" but it was rejected as it duplicated too much with the gpio sysfs interface

does vectored writes (writve) work with sysfs ? if so, then that should work for back-to-back commands rather than having sysfs do stream parsing.
-mike

#12 lore raw
In reply to Mike Frysinger
Quoted 7 lines · On Thu, 4 Jun 2009 04:16:14 -0400 Mike Frysinger <[email protected]> wrote:
Quoted 4 lines · Andrew Morton
Quoted 1 line · On Wed, __3 Jun 2009 04:16:59 -0400 Mike Frysinger wrote:

-ENOCHANGELOGAGAIN

it's pretty damned self explanatory

The implementation is simple, but the implementation doesn't tell us why you believe this feature should be added to Linux.

Is it because doing it from userspace is too slow?

It is because doing it from userspace is racy? If so, isn't that userspace already busted?

Isn't a userspace driver which doesn't already know the state of its GPIO bits rather lame?

etc.