gpio: mockup: rework the testing driver - alternative version
Subsystems (2)
cover gpio: mockup: rework the testing driver - alternative version
From: Bartosz Golaszewski <[email protected]>
It's very late in the release cycle and we cannot seem to get the irq subsystem maintainers' ack on the proposed change in the interrupt simulator. This series contains only the gpio-mockup patches from the previous submission in order to at least partially track the line's state and add a way to verify the results of uAPI calls. The interrupts are fired regardless of their type configuration. This will be fixed once irq_sim supports it.
Bartosz Golaszewski (6):
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 | 188 +++++++++++++++++++++++++++++++------ 1 file changed, 161 insertions(+), 27 deletions(-)
Signature · 1 line
2.20.1
PATCH 1/6 gpio: mockup: add locking
From: Bartosz Golaszewski <[email protected]>
While 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.20.1
PATCH 2/6 gpio: mockup: implement get_multiple()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
We 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.20.1
PATCH 3/6 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.20.1
PATCH 4/6 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.20.1
PATCH 5/6 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.20.1
PATCH 6/6 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 | 116 ++++++++++++++++++++++++++++++++----- 1 file changed, 102 insertions(+), 14 deletions(-)
drivers/gpio/gpio-mockup.c +102 -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; +struct gpio_chip *gc; +struct irq_sim *sim; +int rv, val, curr; + +if (*ppos !=0 ) +return -EINVAL ; rv =kstrtoint_from_user (usr_buf, size,0 , &val);if (rv)
@@ -206,24 +248,69 @@ 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]; + sim = &chip->irqsim; + +mutex_lock (&chip->lock); + +if (test_bit (FLAG_REQUESTED , &desc->flags) && + !test_bit (FLAG_IS_OUT , &desc->flags)) { + curr =__gpio_mockup_get (chip, priv->offset); +if (curr == val) + goto out; + +/* +* FIXME: fire edge interrupts according to the type +* configuration once irq_sim supports this. +*/ +irq_sim_fire (&chip->irqsim, priv->offset); + } -gpiod_set_value_cansleep (desc, val); -irq_sim_fire (&chip->irqsim, priv->offset); +/* Change the value unless we're actively driving the line. */ +if (!test_bit (FLAG_REQUESTED , &desc->flags) || + !test_bit (FLAG_IS_OUT , &desc->flags)) +__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 +345,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 +353,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 +429,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 +503,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.20.1