gpio: mockup: extensions for testing purposes
Subsystems (3)
v2 cover gpio: mockup: extensions for testing purposes
I would like to create an automated test-suite for libgpiod, but the gpio-mockup driver is quite limited when it comes to current user space functionality - I can't test neither line event notifications nor finding GPIO lines by name.
This series proposes to extend the gpio framework by allowing to inject line events from the kernel code and by providing a debugfs interface for that to the gpio-mockup driver. We also allow the user to request that the mockup driver name the lines.
The first two patches only contain coding style changes which, I believe, will make the driver easier to maintain.
The third patch adds the option to have the lines named.
The fourth patch adds a devres flavor of irq_alloc_descs() to be used in patch 5, which actually adds the dummy irqchip.
The last two patches implement the debugfs directory structure that can be used by the user space to inject line events.
v1 -> v2:
- made the event injection self-contained within the driver by using the irq_work as suggested by Lars-Peter Clauses
Bartosz Golaszewski (7):
gpio: mockup: readability tweaks
gpio: mockup: code shrink
gpio: mockup: implement naming the lines
irqdesc: add memory managed version of irq_alloc_descs()
gpio: mockup: add a dummy irqchip
gpiolib: include <gpio/consumer.h> from gpiolib.h
gpio: mockup: implement event injecting over debugfs
drivers/gpio/Kconfig | 1 + drivers/gpio/gpio-mockup.c | 349 ++++++++++++++++++++++++++++++++++++--------- drivers/gpio/gpiolib.h | 1 + include/linux/irq.h | 19 +++ kernel/irq/devres.c | 38 +++++ 5 files changed, 338 insertions(+), 70 deletions(-)
Signature · 1 line
2.9.3
PATCH v2 1/7 gpio: mockup: readability tweaks
The following patch tries to improve the readability of the mockup driver.
The driver is called gpio-mockup, so add the same prefix to all functions and structures.
Add some newlines and use a temporary pointer in gpio_mockup_add().
Drop the name of the direction enum and rename the enum values to better reflect their purpose.
--- drivers/gpio/gpio-mockup.c | 123 ++++++++++++++++++++++++--------------------- 1 file changed, 66 insertions(+), 57 deletions(-)
drivers/gpio/gpio-mockup.c +66 -57
@@ -16,12 +16,12 @@#include <linux/gpio/driver.h> #include <linux/platform_device.h> -#define GPIO_NAME "gpio-mockup" -#define MAX_GC 10 +#define GPIO_MOCKUP_NAME "gpio-mockup" +#define GPIO_MOCKUP_MAX_GC 10 -enum direction { -OUT , -IN +enum { +DIR_IN =0 , +DIR_OUT , };/*
@@ -29,98 +29,104 @@ enum direction {* @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out * @value: Configures status of the gpio as 0(low) or 1(high) */ -struct gpio_pin_status { -enum direction dir; +struct gpio_mockup_line_status { +int dir;bool value; }; -struct mockup_gpio_controller { +struct gpio_mockup_chip {struct gpio_chip gc; -struct gpio_pin_status *stats; +struct gpio_mockup_line_status *lines; }; -static int gpio_mockup_ranges[MAX_GC <<1 ]; +static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_GC <<1 ];static int gpio_mockup_params_nr;module_param_array (gpio_mockup_ranges, int, &gpio_mockup_params_nr,0400 ); -static const char pins_name_start ='A' ; +static const char gpio_mockup_name_start ='A' ; -static int mockup_gpio_get (struct gpio_chip *gc,unsigned int offset) +static int gpio_mockup_get (struct gpio_chip *gc,unsigned int offset) { -struct mockup_gpio_controller *cntr =gpiochip_get_data (gc); +struct gpio_mockup_chip *chip =gpiochip_get_data (gc); -return cntr->stats[offset].value; +return chip->lines[offset].value; } -static void mockup_gpio_set (struct gpio_chip *gc,unsigned int offset, +static void gpio_mockup_set (struct gpio_chip *gc,unsigned int offset,int value) { -struct mockup_gpio_controller *cntr =gpiochip_get_data (gc); +struct gpio_mockup_chip *chip =gpiochip_get_data (gc); - cntr->stats[offset].value = !!value; + chip->lines[offset].value = !!value; } -static int mockup_gpio_dirout (struct gpio_chip *gc,unsigned int offset, +static int gpio_mockup_dirout (struct gpio_chip *gc,unsigned int offset,int value) { -struct mockup_gpio_controller *cntr =gpiochip_get_data (gc); +struct gpio_mockup_chip *chip =gpiochip_get_data (gc); + +gpio_mockup_set (gc, offset, value); + chip->lines[offset].dir =DIR_OUT ; -mockup_gpio_set (gc, offset, value); - cntr->stats[offset].dir =OUT ;return 0 ; } -static int mockup_gpio_dirin (struct gpio_chip *gc,unsigned int offset) +static int gpio_mockup_dirin (struct gpio_chip *gc,unsigned int offset) { -struct mockup_gpio_controller *cntr =gpiochip_get_data (gc); +struct gpio_mockup_chip *chip =gpiochip_get_data (gc); + + chip->lines[offset].dir =DIR_IN ; - cntr->stats[offset].dir =IN ;return 0 ; } -static int mockup_gpio_get_direction (struct gpio_chip *gc,unsigned int offset) +static int gpio_mockup_get_direction (struct gpio_chip *gc,unsigned int offset) { -struct mockup_gpio_controller *cntr =gpiochip_get_data (gc); +struct gpio_mockup_chip *chip =gpiochip_get_data (gc); -return cntr->stats[offset].dir; +return chip->lines[offset].dir; } -static int mockup_gpio_add (struct device *dev, -struct mockup_gpio_controller *cntr, +static int gpio_mockup_add (struct device *dev, +struct gpio_mockup_chip *chip,const char *name,int base,int ngpio) { +struct gpio_chip *gc = &chip->gc;int ret; - cntr->gc.base = base; - cntr->gc.ngpio = ngpio; - cntr->gc.label = name; - cntr->gc.owner =THIS_MODULE ; - cntr->gc.parent = dev; - cntr->gc.get = mockup_gpio_get; - cntr->gc.set = mockup_gpio_set; - cntr->gc.direction_output = mockup_gpio_dirout; - cntr->gc.direction_input = mockup_gpio_dirin; - cntr->gc.get_direction = mockup_gpio_get_direction; - cntr->stats =devm_kzalloc (dev,sizeof (*cntr->stats) * cntr->gc.ngpio, + gc->base = base; + gc->ngpio = ngpio; + gc->label = name; + gc->owner =THIS_MODULE ; + gc->parent = dev; + gc->get = gpio_mockup_get; + gc->set = gpio_mockup_set; + gc->direction_output = gpio_mockup_dirout; + gc->direction_input = gpio_mockup_dirin; + gc->get_direction = gpio_mockup_get_direction; + + chip->lines =devm_kzalloc (dev,sizeof (*chip->lines) * gc->ngpio,GFP_KERNEL ); -if (!cntr->stats) { +if (!chip->lines) { ret = -ENOMEM ; goto err; } - ret =devm_gpiochip_add_data (dev, &cntr->gc, cntr); + + ret =devm_gpiochip_add_data (dev, &chip->gc, chip);if (ret) goto err;dev_info (dev,"gpio<%d..%d> add successful!" , base, base + ngpio);return 0 ; + err:dev_err (dev,"gpio<%d..%d> add failed!" , base, base + ngpio);return ret; } -static int mockup_gpio_probe (struct platform_device *pdev) +static int gpio_mockup_probe (struct platform_device *pdev) { -struct mockup_gpio_controller *cntr; +struct gpio_mockup_chip *chips;struct device *dev = &pdev->dev;int ret, i, base, ngpio;char *chip_name;
@@ -128,15 +134,17 @@ static int mockup_gpio_probe(struct platform_device *pdev)if (gpio_mockup_params_nr <2 )return -EINVAL ; - cntr =devm_kzalloc (dev,sizeof (*cntr) * (gpio_mockup_params_nr >>1 ), -GFP_KERNEL ); -if (!cntr) + chips =devm_kzalloc (dev, +sizeof (*chips) * (gpio_mockup_params_nr >>1 ), +GFP_KERNEL ); +if (!chips)return -ENOMEM ; -platform_set_drvdata (pdev, cntr); +platform_set_drvdata (pdev, chips);for (i =0 ; i < gpio_mockup_params_nr >>1 ; i++) { base = gpio_mockup_ranges[i *2 ]; +if (base ==-1 ) ngpio = gpio_mockup_ranges[i *2 +1 ];else
@@ -144,16 +152,17 @@ static int mockup_gpio_probe(struct platform_device *pdev)if (ngpio >=0 ) { chip_name =devm_kasprintf (dev,GFP_KERNEL , -"%s-%c" ,GPIO_NAME , - pins_name_start + i); +"%s-%c" ,GPIO_MOCKUP_NAME , + gpio_mockup_name_start + i);if (!chip_name)return -ENOMEM ; - ret =mockup_gpio_add (dev, &cntr[i], + ret =gpio_mockup_add (dev, &chips[i], chip_name, base, ngpio); }else { ret =-1 ; } +if (ret) {if (base <0 ) dev_err(dev, "gpio<%d..%d> add failed\n",
@@ -169,11 +178,11 @@ static int mockup_gpio_probe(struct platform_device *pdev)return 0 ; } -static struct platform_driver mockup_gpio_driver = { +static struct platform_driver gpio_mockup_driver = { .driver = { - .name =GPIO_NAME , + .name =GPIO_MOCKUP_NAME , }, - .probe = mockup_gpio_probe, + .probe = gpio_mockup_probe, };static struct platform_device *pdev;
@@ -181,7 +190,7 @@ static int __init mock_device_init(void) {int err; - pdev =platform_device_alloc (GPIO_NAME ,-1 ); + pdev =platform_device_alloc (GPIO_MOCKUP_NAME ,-1 );if (!pdev)return -ENOMEM ;
@@ -191,7 +200,7 @@ static int __init mock_device_init(void)return err; } - err =platform_driver_register (&mockup_gpio_driver); + err =platform_driver_register (&gpio_mockup_driver);if (err) {platform_device_unregister (pdev);return err;
@@ -202,7 +211,7 @@ static int __init mock_device_init(void) static void __exit mock_device_exit(void) { -platform_driver_unregister (&mockup_gpio_driver); +platform_driver_unregister (&gpio_mockup_driver);platform_device_unregister (pdev); }
Signature · 1 line
2.9.3
Re: [PATCH v2 1/7] gpio: mockup: readability tweaks
Quoted 6 lines · Bartosz Golaszewski
The following patch tries to improve the readability of the mockup driver.
The driver is called gpio-mockup, so add the same prefix to all functions and structures.
Quoted 6 lines
Add some newlines and use a temporary pointer in
gpio_mockup_add().Drop the name of the direction enum and rename the enum values to better reflect their purpose.
Signed-off-by Bartosz Golaszewski <[email protected]>
Patch applied.
Yours,
Linus Walleij
PATCH v2 2/7 gpio: mockup: code shrink
In reply to Bartosz GolaszewskiMoving a couple of lines around allows us to shrink the code a bit while keeping the same functionality.
--- drivers/gpio/gpio-mockup.c | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-)
drivers/gpio/gpio-mockup.c +8 -21
@@ -92,7 +92,6 @@ static int gpio_mockup_add(struct device *dev, const char *name, int base, int ngpio) { struct gpio_chip *gc = &chip->gc; -int ret; gc->base = base; gc->ngpio = ngpio;
@@ -107,21 +106,10 @@ static int gpio_mockup_add(struct device *dev, chip->lines =devm_kzalloc (dev,sizeof (*chip->lines) * gc->ngpio,GFP_KERNEL ); -if (!chip->lines) { - ret = -ENOMEM ; - goto err; - } - - ret =devm_gpiochip_add_data (dev, &chip->gc, chip); -if (ret) - goto err; - -dev_info (dev,"gpio<%d..%d> add successful!" , base, base + ngpio); -return 0 ; +if (!chip->lines) +return -ENOMEM ; -err: -dev_err (dev,"gpio<%d..%d> add failed!" , base, base + ngpio); -return ret; +return devm_gpiochip_add_data (dev, &chip->gc, chip); }static int gpio_mockup_probe (struct platform_device *pdev)
@@ -164,15 +152,14 @@ static int gpio_mockup_probe(struct platform_device *pdev) }if (ret) { -if (base <0 ) -dev_err (dev,"gpio<%d..%d> add failed\n" , - base, ngpio); -else -dev_err (dev,"gpio<%d..%d> add failed\n" , - base, base + ngpio); +dev_err (dev,"gpio<%d..%d> add failed\n" , + base, base <0 ? ngpio : base + ngpio);return ret; } + +dev_info (dev,"gpio<%d..%d> add successful!" , + base, base + ngpio); }return 0 ;
Signature · 1 line
2.9.3
Re: [PATCH v2 2/7] gpio: mockup: code shrink
Quoted 4 lines · Bartosz Golaszewski
Moving a couple of lines around allows us to shrink the code a bit while keeping the same functionality.
Signed-off-by Bartosz Golaszewski <[email protected]>
Patch applied.
Yours,
Linus Walleij
PATCH v2 3/7 gpio: mockup: implement naming the lines
In reply to Bartosz GolaszewskiIn order to allow testing line lookup by name from user space, add a new boolean parameter that indicates whether we want the lines to be named. The name is created by concatenating the chip name and the line offset value.
--- drivers/gpio/gpio-mockup.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
drivers/gpio/gpio-mockup.c +35 -0
@@ -15,6 +15,7 @@#include <linux/module.h> #include <linux/gpio/driver.h> #include <linux/platform_device.h> +#include <linux/slab.h> #define GPIO_MOCKUP_NAME "gpio-mockup"#define GPIO_MOCKUP_MAX_GC 10
@@ -43,6 +44,10 @@ static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_GC << 1];static int gpio_mockup_params_nr;module_param_array (gpio_mockup_ranges, int, &gpio_mockup_params_nr,0400 ); +static bool gpio_mockup_named_lines; +module_param_named (gpio_mockup_named_lines, + gpio_mockup_named_lines, bool,0400 ); +static const char gpio_mockup_name_start ='A' ;static int gpio_mockup_get (struct gpio_chip *gc,unsigned int offset)
@@ -87,11 +92,35 @@ static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)return chip->lines[offset].dir; } +static int gpio_mockup_name_lines (struct device *dev, +struct gpio_mockup_chip *chip) +{ +struct gpio_chip *gc = &chip->gc; +char **names; +int i; + + names =devm_kzalloc (dev,sizeof (char *) * gc->ngpio,GFP_KERNEL ); +if (!names) +return -ENOMEM ; + +for (i =0 ; i < gc->ngpio; i++) { + names[i] =devm_kasprintf (dev,GFP_KERNEL , +"%s-%d" , gc->label, i); +if (!names[i]) +return -ENOMEM ; + } + + gc->names = (const char *const *)names; + +return 0 ; +} +static int gpio_mockup_add (struct device *dev,struct gpio_mockup_chip *chip,const char *name,int base,int ngpio) {struct gpio_chip *gc = &chip->gc; +int ret; gc->base = base; gc->ngpio = ngpio;
@@ -109,6 +138,12 @@ static int gpio_mockup_add(struct device *dev,if (!chip->lines)return -ENOMEM ; +if (gpio_mockup_named_lines) { + ret =gpio_mockup_name_lines (dev, chip); +if (ret) +return ret; + } +return devm_gpiochip_add_data (dev, &chip->gc, chip); }
Signature · 1 line
2.9.3
Re: [PATCH v2 3/7] gpio: mockup: implement naming the lines
Quoted 6 lines · Bartosz Golaszewski
In order to allow testing line lookup by name from user space, add a new boolean parameter that indicates whether we want the lines to be named. The name is created by concatenating the chip name and the line offset value.
Signed-off-by Bartosz Golaszewski <[email protected]>
Patch applied.
Yours,
Linus Walleij
PATCH v2 4/7 irqdesc: add memory managed version of irq_alloc_descs()
In reply to Bartosz GolaszewskiAdd a devres flavor of __devm_irq_alloc_descs() and corresponding helper macros.
--- include/linux/irq.h | 19 +++++++++++++++++++ kernel/irq/devres.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+)
include/linux/irq.h +19 -0
@@ -715,6 +715,10 @@ unsigned int arch_dynirq_lower_bound(unsigned int from);int __irq_alloc_descs (int irq,unsigned int from,unsigned int cnt,int node,struct module *owner,const struct cpumask *affinity); +int __devm_irq_alloc_descs (struct device *dev,int irq,unsigned int from, +unsigned int cnt,int node,struct module *owner, +const struct cpumask *affinity); +/* use macros to avoid needing export.h for THIS_MODULE */ #define irq_alloc_descs (irq, from, cnt, node) \ __irq_alloc_descs(irq, from, cnt, node, THIS_MODULE, NULL)
@@ -731,6 +735,21 @@ int __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,#define irq_alloc_descs_from (from, cnt, node) \ irq_alloc_descs(-1, from, cnt, node) +#define devm_irq_alloc_descs (dev, irq, from, cnt, node) \ + __devm_irq_alloc_descs(dev, irq, from, cnt, node, THIS_MODULE, NULL) + +#define devm_irq_alloc_desc (dev, node) \ + devm_irq_alloc_descs(dev, -1, 0, 1, node) + +#define devm_irq_alloc_desc_at (dev, at, node) \ + devm_irq_alloc_descs(dev, at, at, 1, node) + +#define devm_irq_alloc_desc_from (dev, from, node) \ + devm_irq_alloc_descs(dev, -1, from, 1, node) + +#define devm_irq_alloc_descs_from (dev, from, cnt, node) \ + devm_irq_alloc_descs(dev, -1, from, cnt, node) +void irq_free_descs (unsigned int irq,unsigned int cnt);static inline void irq_free_desc (unsigned int irq) {
kernel/irq/devres.c +38 -0
@@ -2,6 +2,7 @@#include <linux/interrupt.h> #include <linux/device.h> #include <linux/gfp.h> +#include <linux/irq.h> /* * Device resource management aware IRQ request/free implementation.
@@ -137,3 +138,40 @@ void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id)free_irq (irq, dev_id); }EXPORT_SYMBOL (devm_free_irq); + +struct irq_desc_devres { +int from; +int cnt; +}; + +static void devm_irq_desc_release (struct device *dev,void *res) +{ +struct irq_desc_devres *this = res; + +irq_free_descs (this->from, this->cnt); +} + +int __devm_irq_alloc_descs (struct device *dev,int irq,unsigned int from, +unsigned int cnt,int node,struct module *owner, +const struct cpumask *affinity) +{ +struct irq_desc_devres *dr; +int base; + + dr =devres_alloc (devm_irq_desc_release,sizeof (*dr),GFP_KERNEL ); +if (!dr) +return -ENOMEM ; + + base =__irq_alloc_descs (irq, from, cnt, node, owner, affinity); +if (base <0 ) { +devres_free (dr); +return base; + } + + dr->from = base; + dr->cnt = cnt; +devres_add (dev, dr); + +return base; +} +EXPORT_SYMBOL_GPL (__devm_irq_alloc_descs);
Signature · 1 line
2.9.3
Re: [PATCH v2 4/7] irqdesc: add memory managed version of irq_alloc_descs()
Quoted 4 lines · Bartosz Golaszewski
Add a devres flavor of
__devm_irq_alloc_descs()and corresponding helper macros.Signed-off-by Bartosz Golaszewski <[email protected]>
Oh that was interesting. I will leave the IRQ subsystems specific review to Thomas and Marc, but I think you need to also update Documentation/driver-model/devres.txt
where you find a long list of all memorymanaged API calls.
Maybe the list is a bit silly actually, but anyway.
Yours,
Linus Walleij
PATCH v2 5/7 gpio: mockup: add a dummy irqchip
In reply to Bartosz GolaszewskiSetup a dummy irqchip that will allow us to inject line events for testing purposes.
--- drivers/gpio/Kconfig | 1 + drivers/gpio/gpio-mockup.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+)
drivers/gpio/Kconfig +1 -0
@@ -298,6 +298,7 @@ config GPIO_MOCKUP tristate "GPIO Testing Driver" depends on GPIOLIB && SYSFS select GPIO_SYSFS + select GPIOLIB_IRQCHIP help This enables GPIO Testing driver, which provides a way to test GPIO subsystem through sysfs(or char device) and debugfs. GPIO_SYSFS
drivers/gpio/gpio-mockup.c +65 -0
98 lines not on this page · raw message
Signature · 1 line
2.9.3
Re: [PATCH v2 5/7] gpio: mockup: add a dummy irqchip
Quoted 4 lines · Bartosz Golaszewski
Setup a dummy irqchip that will allow us to inject line events for testing purposes.
Signed-off-by Bartosz Golaszewski <[email protected]>
This is obviously exactly how we should do this.
However since it needs the devm* helpers from the irq subsystem I can't apply them until the irqchip maintainers merge or ack that patch.
If you want it in soon I suggest resending it with non-memorymanaged accessors for the descs and later make a separate patch to switch over to the devm_* versions when/if it is accepted.
If you're not in a hurry, just wait :)
Yours,
Linus Walleij
Re: [PATCH v2 5/7] gpio: mockup: add a dummy irqchip
2017-02-06 14:29 GMT+01:00 Linus Walleij <[email protected]>:
Quoted 19 lines
Quoted 4 lines · Bartosz Golaszewski
Setup a dummy irqchip that will allow us to inject line events for testing purposes.
Signed-off-by Bartosz Golaszewski <[email protected]>This is obviously exactly how we should do this.
However since it needs the devm* helpers from the irq subsystem I can't apply them until the irqchip maintainers merge or ack that patch.
If you want it in soon I suggest resending it with non-memorymanaged accessors for the descs and later make a separate patch to switch over to the devm_* versions when/if it is accepted.
Quoted 4 lines
If you're not in a hurry, just wait :)
Yours,
Linus Walleij
I think I'll do it, since it would be nice to have it go in for 4.11 (if you're ready to merge it that late into the release cycle).
Thanks,
Bartosz
Re: [PATCH v2 5/7] gpio: mockup: add a dummy irqchip
Quoted 20 lines · Bartosz Golaszewski
2017-02-06 14:29 GMT+01:00 Linus Walleij <[email protected]>:
Quoted 19 lines
Quoted 4 lines · Bartosz Golaszewski
Setup a dummy irqchip that will allow us to inject line events for testing purposes.
Signed-off-by Bartosz Golaszewski <[email protected]>This is obviously exactly how we should do this.
However since it needs the devm* helpers from the irq subsystem I can't apply them until the irqchip maintainers merge or ack that patch.
If you want it in soon I suggest resending it with non-memorymanaged accessors for the descs and later make a separate patch to switch over to the devm_* versions when/if it is accepted.
Quoted 7 lines
Quoted 4 lines
If you're not in a hurry, just wait :)
Yours,
Linus WalleijI think I'll do it, since it would be nice to have it go in for 4.11 (if you're ready to merge it that late into the release cycle).
I will merge this. Expanding the testing is paramount.
Yours,
Linus Walleij
PATCH v2 6/7 gpiolib: include <gpio/consumer.h> from gpiolib.h
In reply to Bartosz GolaszewskiWe want pull gpiolib.h in from the gpio mockup driver, but gpiod_flags is defined in consumer.h, so we need that too indirectly.
--- drivers/gpio/gpiolib.h | 1 + 1 file changed, 1 insertion(+)
drivers/gpio/gpiolib.h +1 -0
@@ -13,6 +13,7 @@#define GPIOLIB_H #include <linux/gpio/driver.h> +#include <linux/gpio/consumer.h> #include <linux/err.h> #include <linux/device.h> #include <linux/module.h>
Signature · 1 line
2.9.3
Re: [PATCH v2 6/7] gpiolib: include <gpio/consumer.h> from gpiolib.h
Quoted 4 lines · Bartosz Golaszewski
We want pull
gpiolib.hin from the gpio mockup driver, butgpiod_flagsis defined inconsumer.h, so we need that too indirectly.Signed-off-by Bartosz Golaszewski <[email protected]>
Can't you just add the include to the mockup driver...?
Yours,
Linus Walleij
PATCH v2 7/7 gpio: mockup: implement event injecting over debugfs
In reply to Bartosz GolaszewskiCreate a debugfs directory for every mockup chip and a single file for every line. Writing (0 or 1) to these files allows the user to inject line events (falling or rising edge respectively).
--- drivers/gpio/gpio-mockup.c | 115 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 114 insertions(+), 1 deletion(-)
drivers/gpio/gpio-mockup.c +114 -1
@@ -19,6 +19,10 @@#include <linux/interrupt.h> #include <linux/irq.h> #include <linux/irq_work.h> +#include <linux/debugfs.h> +#include <linux/uaccess.h> + +#include "gpiolib.h" #define GPIO_MOCKUP_NAME "gpio-mockup"#define GPIO_MOCKUP_MAX_GC 10
@@ -47,6 +51,13 @@ struct gpio_mockup_chip {struct gpio_chip gc;struct gpio_mockup_line_status *lines;struct gpio_mockup_irq_context irq_ctx; +struct dentry *dbg_dir; +}; + +struct gpio_mockup_dbgfs_private { +struct gpio_mockup_chip *chip; +struct gpio_desc *desc; +int offset; };static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_GC <<1 ];
@@ -58,6 +69,7 @@ module_param_named(gpio_mockup_named_lines, gpio_mockup_named_lines, bool, 0400);static const char gpio_mockup_name_start ='A' ; +static struct dentry *gpio_mockup_dbg_dir;static int gpio_mockup_get (struct gpio_chip *gc,unsigned int offset) {
@@ -175,6 +187,94 @@ static int gpio_mockup_irqchip_setup(struct device *dev,return 0 ; } +static ssize_t gpio_mockup_event_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; +struct gpio_chip *gc; +int status, val; +char buf; + + sfile = file->private_data; + priv = sfile->private; + desc = priv->desc; + chip = priv->chip; + gc = &chip->gc; + + status =copy_from_user (&buf, usr_buf,1 ); +if (status) +return status; + +if (buf =='0' ) + val =0 ; +else if (buf =='1' ) + val =1 ; +else +return -EINVAL ; + +gpiod_set_value_cansleep (desc, val); + priv->chip->irq_ctx.irq = gc->irq_base + priv->offset; +irq_work_queue (&priv->chip->irq_ctx.work); + +return size; +} + +static int gpio_mockup_event_open (struct inode *inode,struct file *file) +{ +return single_open (file,NULL , inode->i_private); +} + +static const struct file_operations gpio_mockup_event_ops = { + .owner =THIS_MODULE , + .open = gpio_mockup_event_open, + .write = gpio_mockup_event_write, + .llseek = no_llseek, +}; + +static void gpio_mockup_debugfs_setup (struct device *dev, +struct gpio_mockup_chip *chip) +{ +struct gpio_mockup_dbgfs_private *priv; +struct dentry *evfile; +struct gpio_chip *gc; +char *name; +int i; + + gc = &chip->gc; + + chip->dbg_dir =debugfs_create_dir (gc->label, gpio_mockup_dbg_dir); +if (!chip->dbg_dir) + goto err; + +for (i =0 ; i < gc->ngpio; i++) { + name =devm_kasprintf (dev,GFP_KERNEL ,"%d" , i); +if (!name) + goto err; + + priv =devm_kzalloc (dev,sizeof (*priv),GFP_KERNEL ); +if (!priv) + goto err; + + priv->chip = chip; + priv->offset = i; + priv->desc = &gc->gpiodev->descs[i]; + + evfile =debugfs_create_file (name,0200 , chip->dbg_dir, priv, + &gpio_mockup_event_ops); +if (!evfile) + goto err; + } + +return ; + +err: +dev_err (dev,"error creating debugfs directory\n" ); +} +static int gpio_mockup_add (struct device *dev,struct gpio_mockup_chip *chip,const char *name,int base,int ngpio)
@@ -209,7 +309,14 @@ static int gpio_mockup_add(struct device *dev,if (ret)return ret; -return devm_gpiochip_add_data (dev, &chip->gc, chip); + ret =devm_gpiochip_add_data (dev, &chip->gc, chip); +if (ret) +return ret; + +if (gpio_mockup_dbg_dir) +gpio_mockup_debugfs_setup (dev, chip); + +return 0 ; }static int gpio_mockup_probe (struct platform_device *pdev)
@@ -277,6 +384,11 @@ static int __init mock_device_init(void) {int err; + gpio_mockup_dbg_dir =debugfs_create_dir ("gpio-mockup-event" ,NULL ); +if (!gpio_mockup_dbg_dir) +pr_err ("%s: error creating debugfs directory\n" , +GPIO_MOCKUP_NAME ); + pdev =platform_device_alloc (GPIO_MOCKUP_NAME ,-1 );if (!pdev)return -ENOMEM ;
@@ -298,6 +410,7 @@ static int __init mock_device_init(void) static void __exit mock_device_exit(void) { +debugfs_remove_recursive (gpio_mockup_dbg_dir);platform_driver_unregister (&gpio_mockup_driver);platform_device_unregister (pdev); }
Signature · 1 line
2.9.3
2017-02-06 13:10 GMT+01:00 Bartosz Golaszewski <[email protected]>:
Quoted 18 lines
I would like to create an automated test-suite for libgpiod, but the gpio-mockup driver is quite limited when it comes to current user space functionality - I can't test neither line event notifications nor finding GPIO lines by name.
This series proposes to extend the gpio framework by allowing to inject line events from the kernel code and by providing a debugfs interface for that to the gpio-mockup driver. We also allow the user to request that the mockup driver name the lines.
The first two patches only contain coding style changes which, I believe, will make the driver easier to maintain.
The third patch adds the option to have the lines named.
The fourth patch adds a devres flavor of
irq_alloc_descs()to be used in patch 5, which actually adds the dummy irqchip.
Quoted 6 lines
The last two patches implement the debugfs directory structure that can be used by the user space to inject line events.
v1 -> v2:
- made the event injection self-contained within the driver by using theirq_workas suggested by Lars-Peter Clauses
s/ Lars-Peter Clauses/ Lars-Peter Clausen/
sorry!
Thanks,
Bartosz