gpio: mockup: improve the user-space testing interface
Subsystems (3)
cover gpio: mockup: improve the user-space testing interface
From: Bartosz Golaszewski <[email protected]>
This series aims at reworking the gpio-mockup debugfs interface. The reason for that is the fact that certain known problems with this testing module exist and the user-space tests are broken anyway after commit fa38869b0161 ("gpiolib: Don't support irq sharing for userspace") which made it impossible for gpio-mockup to ignore certain events (e.g. only receive notifications about rising edge events).
The first three patches improve the interrupt simulator. The first one makes the struct irq_chip part of the irq_sim structure so that we can support multiple instances at once. The second delegates the irq number mapping to the irq domain subsystem. The third patch provides a helper that will allow users to check the current configuration of a dummy interrupt and decide whether it should be fired depending on external logic.
Next six patches improve the gpio-mockup module. Patches 4-5 have been reviewed before but missed the last merge window. Patch 6 is there because we're already breaking the debugfs interface anyway and it removes a link that has no users. Patches 7-8 are minor tweaks.
Last patch introduces a rework of the debugfs interface. With this change each mockup chip is represented by a directory named after the chip's device name under <debugfs mount point>/gpio-mockup/. Each line is represented by a file using the line's offset as the name under the chip's directory. Reading from the line's file yields the current *value*, writing to the line's file changes the current "pull". Default pull for mockup lines is down. More info on that can be found in the comment added by this change to the gpio-mockup code.
This is somewhat inspired by the idea of the gpio-simulator by Uwe Kleine-König except that I strongly belive that when testing certain user API code paths we should not be using the same paths for verification. That's why there's a separate interface (debugfs) sharing as little as possible with the character device that allows to check if various operations (reading and setting values, events) work as expected instead of two connected dummy chips sharing the same interface.
If accepted this will of course require major modification of user-space tests in libgpiod once upstream.
Bartosz Golaszewski (9):
irq/irq_sim: don't share the irq_chip structure between simulators irq/irq_sim: use irq domain
irq/irq_sim: provide irq_sim_get_type()
gpio: mockup: add locking
gpio: mockup: implement get_multiple()
gpio: mockup: don't create the debugfs link named after the label
gpio: mockup: change the type of 'offset' to unsigned int
gpio: mockup: change the signature of unlocked get/set helpers
gpio: mockup: rework debugfs interface
drivers/gpio/gpio-mockup.c | 186 +++++++++++++++++++++++++++++++------ include/linux/irq_sim.h | 10 +- kernel/irq/irq_sim.c | 127 ++++++++++++++++++------- 3 files changed, 262 insertions(+), 61 deletions(-)
Signature · 1 line
2.19.1
PATCH 1/9 irq/irq_sim: don't share the irq_chip structure between simulators
From: Bartosz Golaszewski <[email protected]>
We want to support multiple instances of irq_sim. Make struct irq_chip part of the irq_sim structure.
--- include/linux/irq_sim.h | 2 ++ kernel/irq/irq_sim.c | 12 +++++------- 2 files changed, 7 insertions(+), 7 deletions(-)
include/linux/irq_sim.h +2 -0
@@ -6,6 +6,7 @@#ifndef _LINUX_IRQ_SIM_H#define _LINUX_IRQ_SIM_H +#include <linux/irq.h> #include <linux/irq_work.h> #include <linux/device.h>
@@ -25,6 +26,7 @@ struct irq_sim_irq_ctx { };struct irq_sim { +struct irq_chip chip;struct irq_sim_work_ctx work_ctx;int irq_base;unsigned int irq_count;
kernel/irq/irq_sim.c +5 -7
@@ -25,12 +25,6 @@ static void irq_sim_irqunmask(struct irq_data *data) irq_ctx->enabled = true; } -static struct irq_chip irq_sim_irqchip = { - .name ="irq_sim" , - .irq_mask = irq_sim_irqmask, - .irq_unmask = irq_sim_irqunmask, -}; -static void irq_sim_handle_irq (struct irq_work *work) {struct irq_sim_work_ctx *work_ctx;
@@ -74,6 +68,10 @@ int irq_sim_init(struct irq_sim *sim, unsigned int num_irqs)return sim->irq_base; } + sim->chip.name ="irq_sim" ; + sim->chip.irq_mask = irq_sim_irqmask; + sim->chip.irq_unmask = irq_sim_irqunmask; + sim->work_ctx.pending =bitmap_zalloc (num_irqs,GFP_KERNEL );if (!sim->work_ctx.pending) {kfree (sim->irqs);
@@ -84,7 +82,7 @@ int irq_sim_init(struct irq_sim *sim, unsigned int num_irqs)for (i =0 ; i < num_irqs; i++) { sim->irqs[i].irqnum = sim->irq_base + i; sim->irqs[i].enabled = false; -irq_set_chip (sim->irq_base + i, &irq_sim_irqchip); +irq_set_chip (sim->irq_base + i, &sim->chip);irq_set_chip_data (sim->irq_base + i, &sim->irqs[i]);irq_set_handler (sim->irq_base + i, &handle_simple_irq); irq_modify_status(sim->irq_base + i,
Signature · 1 line
2.19.1
PATCH 2/9 irq/irq_sim: use irq domain
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Delegate the offset to virq number mapping to the provided framework instead of handling it locally.
--- include/linux/irq_sim.h | 6 +-- kernel/irq/irq_sim.c | 94 +++++++++++++++++++++++++++++------------ 2 files changed, 71 insertions(+), 29 deletions(-)
include/linux/irq_sim.h +3 -3
@@ -8,6 +8,7 @@#include <linux/irq.h> #include <linux/irq_work.h> +#include <linux/irqdomain.h> #include <linux/device.h> /*
@@ -21,16 +22,15 @@ struct irq_sim_work_ctx { };struct irq_sim_irq_ctx { -int irqnum;bool enabled; };struct irq_sim {struct irq_chip chip; +struct irq_domain *domain;struct irq_sim_work_ctx work_ctx; -int irq_base; +int virq_base;unsigned int irq_count; -struct irq_sim_irq_ctx *irqs; };int irq_sim_init (struct irq_sim *sim,unsigned int num_irqs);
kernel/irq/irq_sim.c +68 -26
142 lines not on this page · raw message
Signature · 1 line
2.19.1
PATCH 3/9 irq/irq_sim: provide irq_sim_get_type()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Provide a helper that allows users to retrieve the configured flow type of dummy interrupts. That allows certain users to decide whether an irq needs to be fired depending on its edge/level/... configuration.
--- include/linux/irq_sim.h | 2 ++ kernel/irq/irq_sim.c | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+)
include/linux/irq_sim.h +2 -0
@@ -23,6 +23,7 @@ struct irq_sim_work_ctx {struct irq_sim_irq_ctx {bool enabled; +unsigned int type; };struct irq_sim {
@@ -39,5 +40,6 @@ int devm_irq_sim_init(struct device *dev, struct irq_sim *sim,void irq_sim_fini (struct irq_sim *sim);void irq_sim_fire (struct irq_sim *sim,unsigned int offset);int irq_sim_irqnum (struct irq_sim *sim,unsigned int offset); +int irq_sim_get_type (struct irq_sim *sim,unsigned int offset);#endif /* _LINUX_IRQ_SIM_H */
kernel/irq/irq_sim.c +25 -0
@@ -25,6 +25,15 @@ static void irq_sim_irqunmask(struct irq_data *data) irq_ctx->enabled = true; } +static int irq_sim_set_type (struct irq_data *data,unsigned int type) +{ +struct irq_sim_irq_ctx *irq_ctx =irq_data_get_irq_chip_data (data); + + irq_ctx->type = type; + +return 0 ; +} +static void irq_sim_handle_irq (struct irq_work *work) {struct irq_sim_work_ctx *work_ctx;
@@ -107,6 +116,7 @@ int irq_sim_init(struct irq_sim *sim, unsigned int num_irqs) sim->chip.name ="irq_sim" ; sim->chip.irq_mask = irq_sim_irqmask; sim->chip.irq_unmask = irq_sim_irqunmask; + sim->chip.irq_set_type = irq_sim_set_type; sim->work_ctx.pending =bitmap_zalloc (num_irqs,GFP_KERNEL );if (!sim->work_ctx.pending) {
@@ -220,3 +230,18 @@ int irq_sim_irqnum(struct irq_sim *sim, unsigned int offset)return irq_find_mapping (sim->domain, offset); }EXPORT_SYMBOL_GPL (irq_sim_irqnum); + +/** +* irq_sim_get_type - Get the configured flow type of a dummy interrupt. +* +* @sim: The interrupt simulator object. +* @offset: Offset of the simulated interrupt for which to retrieve +* the type. +*/ +int irq_sim_get_type (struct irq_sim *sim,unsigned int offset) +{ +struct irq_sim_irq_ctx *ctx =irq_sim_get_ctx (sim, offset); + +return ctx->type; +} +EXPORT_SYMBOL_GPL (irq_sim_get_type);
Signature · 1 line
2.19.1
Re: [PATCH 3/9] irq/irq_sim: provide irq_sim_get_type()
Hello Bartosz,
Quoted 3 lines · Bartosz Golaszewski
Provide a helper that allows users to retrieve the configured flow type of dummy interrupts. That allows certain users to decide whether an irq needs to be fired depending on its edge/level/... configuration.
You don't talk about the .set_type callback here; is this intended?
I wonder how you think this should be used. Assume the mockup-driver is directed to pull up a certain line, does it do something like that:
def mockup_setval(self, val): irqtype = irq_sim_get_type(...) if irqtype == LEVEL_HIGH: if val: fire_irq() else if irqtype == EDGE_RISING: if val and not prev_val: fire_irq() else if irqtype == LEVEL_LOW: if not val: fire_irq() else if irqtype == EDGE_FALLING: if not val and prev_val: fire_irq()
I wonder if that logic should be done in the same place as where the irq type is stored. Otherwise that .type member is only a data store provided by the irq simulator. So I suggest to either move the variable that mirrors the current level of the line into the irq simulator, or keep the irqtype variable in the mockup driver. Both approaches would make it unnecessary to provide an accessor function for the type member.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | http://www.pengutronix.de/ |
Re: [PATCH 3/9] irq/irq_sim: provide irq_sim_get_type()
śr., 23 sty 2019 o 20:18 Uwe Kleine-König
<[email protected]> napisał(a):
Quoted 31 lines
Hello Bartosz,
Quoted 3 lines · Bartosz Golaszewski
Provide a helper that allows users to retrieve the configured flow type of dummy interrupts. That allows certain users to decide whether an irq needs to be fired depending on its edge/level/... configuration.
You don't talk about the .set_type callback here; is this intended?
I wonder how you think this should be used. Assume the mockup-driver is directed to pull up a certain line, does it do something like that:
def mockup_setval(self, val): irqtype = irq_sim_get_type(...) if irqtype == LEVEL_HIGH: if val: fire_irq() else if irqtype == EDGE_RISING: if val and not prev_val: fire_irq() else if irqtype == LEVEL_LOW: if not val: fire_irq() else if irqtype == EDGE_FALLING: if not val and prev_val: fire_irq()
Quoted 7 lines
I wonder if that logic should be done in the same place as where the irq type is stored. Otherwise that .type member is only a data store provided by the irq simulator. So I suggest to either move the variable that mirrors the current level of the line into the irq simulator, or keep the irqtype variable in the mockup driver. Both approaches would make it unnecessary to provide an accessor function for the type member.
Yeah, might be better to go back to my previous idea of adding irq_sim_fire_edge(), but maybe it should be irq_sim_fire_type() instead, so that irq_sim_fire() fires unconditionally and irq_sim_fire_type() would fire only if the passed flag is the same as the one previously configured by the set_type() callback.
Thanks,
Bartosz
Re: [PATCH 3/9] irq/irq_sim: provide irq_sim_get_type()
Quoted 41 lines · Bartosz Golaszewski
śr., 23 sty 2019 o 20:18 Uwe Kleine-König
<[email protected]> napisał(a):Quoted 31 lines
Hello Bartosz,
Quoted 3 lines · Bartosz Golaszewski
Provide a helper that allows users to retrieve the configured flow type of dummy interrupts. That allows certain users to decide whether an irq needs to be fired depending on its edge/level/... configuration.
You don't talk about the .set_type callback here; is this intended?
I wonder how you think this should be used. Assume the mockup-driver is directed to pull up a certain line, does it do something like that:
def mockup_setval(self, val): irqtype = irq_sim_get_type(...) if irqtype == LEVEL_HIGH: if val: fire_irq() else if irqtype == EDGE_RISING: if val and not prev_val: fire_irq() else if irqtype == LEVEL_LOW: if not val: fire_irq() else if irqtype == EDGE_FALLING: if not val and prev_val: fire_irq()Quoted 7 lines
I wonder if that logic should be done in the same place as where the irq type is stored. Otherwise that .type member is only a data store provided by the irq simulator. So I suggest to either move the variable that mirrors the current level of the line into the irq simulator, or keep the irqtype variable in the mockup driver. Both approaches would make it unnecessary to provide an accessor function for the type member.
Quoted 5 lines
Yeah, might be better to go back to my previous idea of adding
irq_sim_fire_edge(), but maybe it should beirq_sim_fire_type()instead, so thatirq_sim_fire()fires unconditionally andirq_sim_fire_type()would fire only if the passed flag is the same as the one previously configured by theset_type()callback.
How (if at all) do you intend to support level sensitive irqs with this interface? It probably works (but I didn't thought it through completely), but firing a LEVEL_HIGH sensitive irq on
irq_sim_fire_type(EDGE_RISING)
might look at least surprising and needs proper comments and thoughts.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | http://www.pengutronix.de/ |
PATCH 4/9 gpio: mockup: add locking
In reply to Bartosz GolaszewskiWhile no user reported any race condition problems with gpio-mockup, let's be on the safe side and use a mutex when performing any changes on the dummy chip structures.
--- drivers/gpio/gpio-mockup.c | 50 ++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 7 deletions(-)
drivers/gpio/gpio-mockup.c +43 -7
@@ -54,6 +54,7 @@ struct gpio_mockup_chip {struct gpio_mockup_line_status *lines;struct irq_sim irqsim;struct dentry *dbg_dir; +struct mutex lock; };struct gpio_mockup_dbgfs_private {
@@ -82,29 +83,53 @@ static int gpio_mockup_range_ngpio(unsigned int index)return gpio_mockup_ranges[index *2 +1 ]; } -static int gpio_mockup_get (struct gpio_chip *gc,unsigned int offset) +static int __gpio_mockup_get (struct gpio_chip *gc,unsigned int offset) {struct gpio_mockup_chip *chip =gpiochip_get_data (gc);return chip->lines[offset].value; } -static void gpio_mockup_set (struct gpio_chip *gc, -unsigned int offset,int value) +static int gpio_mockup_get (struct gpio_chip *gc,unsigned int offset) +{ +struct gpio_mockup_chip *chip =gpiochip_get_data (gc); +int val; + +mutex_lock (&chip->lock); + val =__gpio_mockup_get (gc, offset); +mutex_unlock (&chip->lock); + +return val; +} + +static void __gpio_mockup_set (struct gpio_chip *gc, +unsigned int offset,int value) {struct gpio_mockup_chip *chip =gpiochip_get_data (gc); chip->lines[offset].value = !!value; } +static void gpio_mockup_set (struct gpio_chip *gc, +unsigned int offset,int value) +{ +struct gpio_mockup_chip *chip =gpiochip_get_data (gc); + +mutex_lock (&chip->lock); +__gpio_mockup_set (gc, offset, value); +mutex_unlock (&chip->lock); +} +static void gpio_mockup_set_multiple (struct gpio_chip *gc,unsigned long *mask,unsigned long *bits) { +struct gpio_mockup_chip *chip =gpiochip_get_data (gc);unsigned int bit; +mutex_lock (&chip->lock);for_each_set_bit (bit, mask, gc->ngpio) -gpio_mockup_set (gc, bit,test_bit (bit, bits)); - +__gpio_mockup_set (gc, bit,test_bit (bit, bits)); +mutex_unlock (&chip->lock); } static int gpio_mockup_dirout(struct gpio_chip *gc,
@@ -112,8 +137,10 @@ static int gpio_mockup_dirout(struct gpio_chip *gc, {struct gpio_mockup_chip *chip =gpiochip_get_data (gc); -gpio_mockup_set (gc, offset, value); +mutex_lock (&chip->lock); chip->lines[offset].dir =GPIO_MOCKUP_DIR_OUT ; +__gpio_mockup_set (gc, offset, value); +mutex_unlock (&chip->lock);return 0 ; }
@@ -122,7 +149,9 @@ static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset) {struct gpio_mockup_chip *chip =gpiochip_get_data (gc); +mutex_lock (&chip->lock); chip->lines[offset].dir =GPIO_MOCKUP_DIR_IN ; +mutex_unlock (&chip->lock);return 0 ; }
@@ -130,8 +159,13 @@ static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)static int gpio_mockup_get_direction (struct gpio_chip *gc,unsigned int offset) {struct gpio_mockup_chip *chip =gpiochip_get_data (gc); +int direction; -return !chip->lines[offset].dir; +mutex_lock (&chip->lock); + direction = !chip->lines[offset].dir; +mutex_unlock (&chip->lock); + +return direction; }static int gpio_mockup_to_irq (struct gpio_chip *gc,unsigned int offset)
@@ -283,6 +317,8 @@ static int gpio_mockup_probe(struct platform_device *pdev)return -ENOMEM ; } +mutex_init (&chip->lock); + gc = &chip->gc; gc->base = base; gc->ngpio = ngpio;
Signature · 1 line
2.19.1
PATCH 5/9 gpio: mockup: implement get_multiple()
In reply to Bartosz GolaszewskiWe already support set_multiple(). Implement get_multiple() as well.
--- drivers/gpio/gpio-mockup.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
drivers/gpio/gpio-mockup.c +17 -0
@@ -102,6 +102,22 @@ static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)return val; } +static int gpio_mockup_get_multiple (struct gpio_chip *gc, +unsigned long *mask,unsigned long *bits) +{ +struct gpio_mockup_chip *chip =gpiochip_get_data (gc); +unsigned int bit, val; + +mutex_lock (&chip->lock); +for_each_set_bit (bit, mask, gc->ngpio) { + val =__gpio_mockup_get (gc, bit); +__assign_bit (bit, bits, val); + } +mutex_unlock (&chip->lock); + +return 0 ; +} +static void __gpio_mockup_set (struct gpio_chip *gc,unsigned int offset,int value) {
@@ -327,6 +343,7 @@ static int gpio_mockup_probe(struct platform_device *pdev) gc->parent = dev; gc->get = gpio_mockup_get; gc->set = gpio_mockup_set; + gc->get_multiple = gpio_mockup_get_multiple; gc->set_multiple = gpio_mockup_set_multiple; gc->direction_output = gpio_mockup_dirout; gc->direction_input = gpio_mockup_dirin;
Signature · 1 line
2.19.1
PATCH 6/9 gpio: mockup: don't create the debugfs link named after the label
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
User-space tests no longer use it and we're breaking the interface anyway.
--- drivers/gpio/gpio-mockup.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/gpio/gpio-mockup.c +1 -5
@@ -234,7 +234,7 @@ static void gpio_mockup_debugfs_setup(struct device *dev, struct gpio_mockup_chip *chip) {struct gpio_mockup_dbgfs_private *priv; -struct dentry *evfile, *link; +struct dentry *evfile;struct gpio_chip *gc;const char *devname;char *name;
@@ -247,10 +247,6 @@ static void gpio_mockup_debugfs_setup(struct device *dev,if (IS_ERR_OR_NULL (chip->dbg_dir)) goto err; - link =debugfs_create_symlink (gc->label, gpio_mockup_dbg_dir, devname); -if (IS_ERR_OR_NULL (link)) - goto err; -for (i =0 ; i < gc->ngpio; i++) { name =devm_kasprintf (dev,GFP_KERNEL ,"%d" , i);if (!name)
Signature · 1 line
2.19.1
PATCH 7/9 gpio: mockup: change the type of 'offset' to unsigned int
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
This field can never be negative.
--- drivers/gpio/gpio-mockup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpio-mockup.c +1 -1
@@ -60,7 +60,7 @@ struct gpio_mockup_chip {struct gpio_mockup_dbgfs_private {struct gpio_mockup_chip *chip;struct gpio_desc *desc; -int offset; +unsigned int offset; };static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES ];
Signature · 1 line
2.19.1
PATCH 8/9 gpio: mockup: change the signature of unlocked get/set helpers
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
The unlocked variants only get called from places where we already have the pointer to the underlying gpio_mockup_chip structure, so take it as parameter instead of struct gpio_chip.
--- drivers/gpio/gpio-mockup.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-)
drivers/gpio/gpio-mockup.c +8 -11
@@ -83,10 +83,9 @@ static int gpio_mockup_range_ngpio(unsigned int index)return gpio_mockup_ranges[index *2 +1 ]; } -static int __gpio_mockup_get (struct gpio_chip *gc,unsigned int offset) +static int __gpio_mockup_get (struct gpio_mockup_chip *chip, +unsigned int offset) { -struct gpio_mockup_chip *chip =gpiochip_get_data (gc); -return chip->lines[offset].value; }
@@ -96,7 +95,7 @@ static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)int val;mutex_lock (&chip->lock); - val =__gpio_mockup_get (gc, offset); + val =__gpio_mockup_get (chip, offset);mutex_unlock (&chip->lock);return val;
@@ -110,7 +109,7 @@ static int gpio_mockup_get_multiple(struct gpio_chip *gc,mutex_lock (&chip->lock);for_each_set_bit (bit, mask, gc->ngpio) { - val =__gpio_mockup_get (gc, bit); + val =__gpio_mockup_get (chip, bit);__assign_bit (bit, bits, val); }mutex_unlock (&chip->lock);
@@ -118,11 +117,9 @@ static int gpio_mockup_get_multiple(struct gpio_chip *gc,return 0 ; } -static void __gpio_mockup_set (struct gpio_chip *gc, +static void __gpio_mockup_set (struct gpio_mockup_chip *chip,unsigned int offset,int value) { -struct gpio_mockup_chip *chip =gpiochip_get_data (gc); - chip->lines[offset].value = !!value; }
@@ -132,7 +129,7 @@ static void gpio_mockup_set(struct gpio_chip *gc,struct gpio_mockup_chip *chip =gpiochip_get_data (gc);mutex_lock (&chip->lock); -__gpio_mockup_set (gc, offset, value); +__gpio_mockup_set (chip, offset, value);mutex_unlock (&chip->lock); }
@@ -144,7 +141,7 @@ static void gpio_mockup_set_multiple(struct gpio_chip *gc,mutex_lock (&chip->lock);for_each_set_bit (bit, mask, gc->ngpio) -__gpio_mockup_set (gc, bit,test_bit (bit, bits)); +__gpio_mockup_set (chip, bit,test_bit (bit, bits));mutex_unlock (&chip->lock); }
@@ -155,7 +152,7 @@ static int gpio_mockup_dirout(struct gpio_chip *gc,mutex_lock (&chip->lock); chip->lines[offset].dir =GPIO_MOCKUP_DIR_OUT ; -__gpio_mockup_set (gc, offset, value); +__gpio_mockup_set (chip, offset, value);mutex_unlock (&chip->lock);return 0 ;
Signature · 1 line
2.19.1
PATCH 9/9 gpio: mockup: rework debugfs interface
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Modify the way the debugfs interface works in gpio-mockup. Introduce the concept of dummy pull config which will keep the mockup lines in known state. The pull values can be modified by writing to the debugfs files corresponding to lines. Lines in input mode always report the current pull value, lines in output mode change the line value but it will revert back to the one specified by current pull when released.
--- drivers/gpio/gpio-mockup.c | 114 ++++++++++++++++++++++++++++++++----- 1 file changed, 100 insertions(+), 14 deletions(-)
drivers/gpio/gpio-mockup.c +100 -14
@@ -47,6 +47,7 @@ enum {struct gpio_mockup_line_status {int dir;int value; +int pull; };struct gpio_mockup_chip {
@@ -188,15 +189,56 @@ static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)return irq_sim_irqnum (&chip->irqsim, offset); } -static ssize_t gpio_mockup_event_write (struct file *file, -const char __user *usr_buf, -size_t size,loff_t *ppos) +static void gpio_mockup_free (struct gpio_chip *gc,unsigned int offset) +{ +struct gpio_mockup_chip *chip =gpiochip_get_data (gc); + +__gpio_mockup_set (chip, offset, chip->lines[offset].pull); +} + +static ssize_t gpio_mockup_debugfs_read (struct file *file, +char __user *usr_buf, +size_t size,loff_t *ppos) +{ +struct gpio_mockup_dbgfs_private *priv; +struct gpio_mockup_chip *chip; +struct seq_file *sfile; +struct gpio_chip *gc; +char buf[3 ]; +int val, rv; + +if (*ppos !=0 ) +return 0 ; + + sfile = file->private_data; + priv = sfile->private; + chip = priv->chip; + gc = &chip->gc; + + val =gpio_mockup_get (gc, priv->offset); +snprintf (buf,sizeof (buf),"%d\n" , val); + + rv =copy_to_user (usr_buf, buf,sizeof (buf)); +if (rv) +return rv; + +return sizeof (buf) -1 ; +} + +static ssize_t gpio_mockup_debugfs_write (struct file *file, +const char __user *usr_buf, +size_t size,loff_t *ppos) {struct gpio_mockup_dbgfs_private *priv;struct gpio_mockup_chip *chip;struct seq_file *sfile;struct gpio_desc *desc; -int rv, val; +unsigned int irq_type; +struct gpio_chip *gc; +int rv, val, curr; + +if (*ppos !=0 ) +return -EINVAL ; rv =kstrtoint_from_user (usr_buf, size,0 , &val);if (rv)
@@ -206,24 +248,67 @@ static ssize_t gpio_mockup_event_write(struct file *file, sfile = file->private_data; priv = sfile->private; - desc = priv->desc; chip = priv->chip; + gc = &chip->gc; + desc = &gc->gpiodev->descs[priv->offset]; + +mutex_lock (&chip->lock); + +if (!test_bit (FLAG_IS_OUT , &desc->flags)) { +if (test_bit (FLAG_REQUESTED , &desc->flags)) { + curr =__gpio_mockup_get (chip, priv->offset); +if (curr == val) + goto out; -gpiod_set_value_cansleep (desc, val); -irq_sim_fire (&chip->irqsim, priv->offset); + irq_type =irq_sim_get_type (&chip->irqsim, + priv->offset); + +if ((val ==0 && (irq_type &IRQ_TYPE_EDGE_FALLING )) || + (val ==1 && (irq_type &IRQ_TYPE_EDGE_RISING ))) +irq_sim_fire (&chip->irqsim, priv->offset); + } + +__gpio_mockup_set (chip, priv->offset, val); + } + +out: + chip->lines[priv->offset].pull = val; +mutex_unlock (&chip->lock);return size; } -static int gpio_mockup_event_open (struct inode *inode,struct file *file) +static int gpio_mockup_debugfs_open (struct inode *inode,struct file *file) {return single_open (file,NULL , inode->i_private); } -static const struct file_operations gpio_mockup_event_ops = { +/* +* Each mockup chip is represented by a directory named after the chip's device +* name under /sys/kernel/debug/gpio-mockup/. Each line is represented by +* a file using the line's offset as the name under the chip's directory. +* +* Reading from the line's file yields the current *value*, writing to the +* line's file changes the current *pull*. Default pull for mockup lines is +* down. +* +* Examples: +* - when a line pulled down is requested in output mode and driven high, its +* value will return to 0 once it's released +* - when the line is requested in output mode and driven high, writing 0 to +* the corresponding debugfs file will change the pull to down but the +* reported value will still be 1 until the line is released +* - line requested in input mode always reports the same value as its pull +* configuration +* - when the line is requested in input mode and monitored for events, writing +* the same value to the debugfs file will be a noop, while writing the +* opposite value will generate a dummy interrupt with an appropriate edge +*/ +static const struct file_operations gpio_mockup_debugfs_ops = { .owner =THIS_MODULE , - .open = gpio_mockup_event_open, - .write = gpio_mockup_event_write, + .open = gpio_mockup_debugfs_open, + .read = gpio_mockup_debugfs_read, + .write = gpio_mockup_debugfs_write, .llseek = no_llseek, };
@@ -258,7 +343,7 @@ static void gpio_mockup_debugfs_setup(struct device *dev, priv->desc = &gc->gpiodev->descs[i]; evfile =debugfs_create_file (name,0200 , chip->dbg_dir, priv, - &gpio_mockup_event_ops); + &gpio_mockup_debugfs_ops);if (IS_ERR_OR_NULL (evfile)) goto err; }
@@ -266,7 +351,7 @@ static void gpio_mockup_debugfs_setup(struct device *dev,return ; err: -dev_err (dev,"error creating debugfs event files\n" ); +dev_err (dev,"error creating debugfs files\n" ); } static int gpio_mockup_name_lines(struct device *dev,
@@ -342,6 +427,7 @@ static int gpio_mockup_probe(struct platform_device *pdev) gc->direction_input = gpio_mockup_dirin; gc->get_direction = gpio_mockup_get_direction; gc->to_irq = gpio_mockup_to_irq; + gc->free = gpio_mockup_free; chip->lines =devm_kcalloc (dev, gc->ngpio,sizeof (*chip->lines),GFP_KERNEL );
@@ -415,7 +501,7 @@ static int __init gpio_mockup_init(void)return -EINVAL ; } - gpio_mockup_dbg_dir =debugfs_create_dir ("gpio-mockup-event" ,NULL ); + gpio_mockup_dbg_dir =debugfs_create_dir ("gpio-mockup" ,NULL );if (IS_ERR_OR_NULL (gpio_mockup_dbg_dir))gpio_mockup_err ("error creating debugfs directory\n" );
Signature · 1 line
2.19.1
Quoted 41 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
This series aims at reworking the gpio-mockup debugfs interface. The reason for that is the fact that certain known problems with this testing module exist and the user-space tests are broken anyway after commit
fa38869b0161("gpiolib: Don't support irq sharing for userspace") which made it impossible for gpio-mockup to ignore certain events (e.g. only receive notifications about rising edge events).The first three patches improve the interrupt simulator. The first one makes the struct
irq_chippart of theirq_simstructure so that we can support multiple instances at once. The second delegates the irq number mapping to the irq domain subsystem. The third patch provides a helper that will allow users to check the current configuration of a dummy interrupt and decide whether it should be fired depending on external logic.Next six patches improve the gpio-mockup module. Patches 4-5 have been reviewed before but missed the last merge window. Patch 6 is there because we're already breaking the debugfs interface anyway and it removes a link that has no users. Patches 7-8 are minor tweaks.
Last patch introduces a rework of the debugfs interface. With this change each mockup chip is represented by a directory named after the chip's device name under <debugfs mount point>/gpio-mockup/. Each line is represented by a file using the line's offset as the name under the chip's directory. Reading from the line's file yields the current *value*, writing to the line's file changes the current "pull". Default pull for mockup lines is down. More info on that can be found in the comment added by this change to the gpio-mockup code.
This is somewhat inspired by the idea of the gpio-simulator by Uwe Kleine-König except that I strongly belive that when testing certain user API code paths we should not be using the same paths for verification. That's why there's a separate interface (debugfs) sharing as little as possible with the character device that allows to check if various operations (reading and setting values, events) work as expected instead of two connected dummy chips sharing the same interface.
Quoted 2 lines
If accepted this will of course require major modification of user-space tests in libgpiod once upstream.
I like how this is developing and how I see the two major contributors to simulated GPIOs cooperating on this :)
It'd be nice to get Marc's feedback on the irqchip changes so we can solidify that part.
Yours,
Linus Walleij