Skip to contentSkip to conversation

gpio: mockup: updates for 4.13

PATCH Superseded Bartosz Golaszewski 3 participants 22 messages May 25, 2017 – May 29, 2017

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#4 lore raw

PATCH 2/10 gpio: mockup: add prefixes to the direction enum

In reply to Bartosz Golaszewski

All internal symbols except for the direction enum follow the same convention and use the gpio_mockup prefix. Add the prefix to the DIR_IN and DIR_OUT definitions as well for consistency across the file.

---
 drivers/gpio/gpio-mockup.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpio/gpio-mockup.c +4 -4
index c18d011..b1ee45c 100644
@@ -29,8 +29,8 @@
 #define	GPIO_MOCKUP_MAX_GC	10
 
 enum {
-	DIR_OUT = 0,
-	DIR_IN = 1,
+	GPIO_MOCKUP_DIR_OUT = 0,
+	GPIO_MOCKUP_DIR_IN = 1,
 };
 
 /*
@@ -93,7 +93,7 @@ static int gpio_mockup_dirout(struct gpio_chip *gc, unsigned int offset,
 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
 
 	gpio_mockup_set(gc, offset, value);
-	chip->lines[offset].dir = DIR_OUT;
+	chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT;
 
 	return 0;
 }
@@ -102,7 +102,7 @@ static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
 {
 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
 
-	chip->lines[offset].dir = DIR_IN;
+	chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN;
 
 	return 0;
 }
Signature · 1 line
2.9.3
#18 lore raw

PATCH 7/10 gpio: mockup: improve readability

In reply to Bartosz Golaszewski

We currently shift bits here and there without actually explaining what we're doing. Add some helper variables with names indicating their purpose to improve the code readability.

---
 drivers/gpio/gpio-mockup.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
drivers/gpio/gpio-mockup.c +13 -7
index d771112..f6efa97 100644
@@ -27,6 +27,11 @@
 
 #define GPIO_MOCKUP_NAME	"gpio-mockup"
 #define	GPIO_MOCKUP_MAX_GC	10
+/*
+ * We're storing two values per chip: the GPIO base and the number
+ * of GPIO lines.
+ */
+#define GPIO_MOCKUP_MAX_RANGES	(GPIO_MOCKUP_MAX_GC * 2)
 
 enum {
 	GPIO_MOCKUP_DIR_OUT = 0,
@@ -62,7 +67,7 @@ struct gpio_mockup_dbgfs_private {
 	int offset;
 };
 
-static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_GC << 1];
+static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
 static int gpio_mockup_params_nr;
 module_param_array(gpio_mockup_ranges, int, &gpio_mockup_params_nr, 0400);
 
@@ -341,23 +346,24 @@ static int gpio_mockup_add(struct device *dev,
 
 static int gpio_mockup_probe(struct platform_device *pdev)
 {
-	struct gpio_mockup_chip *chips;
+	int ret, i, base, ngpio, num_chips;
 	struct device *dev = &pdev->dev;
-	int ret, i, base, ngpio;
+	struct gpio_mockup_chip *chips;
 	char *chip_name;
 
 	if (gpio_mockup_params_nr < 2 || (gpio_mockup_params_nr % 2))
 		return -EINVAL;
 
-	chips = devm_kzalloc(dev,
-			     sizeof(*chips) * (gpio_mockup_params_nr >> 1),
-			     GFP_KERNEL);
+	/* Each chip is described by two values. */
+	num_chips = gpio_mockup_params_nr / 2;
+
+	chips = devm_kzalloc(dev, sizeof(*chips) * num_chips, GFP_KERNEL);
 	if (!chips)
 		return -ENOMEM;
 
 	platform_set_drvdata(pdev, chips);
 
-	for (i = 0; i < gpio_mockup_params_nr >> 1; i++) {
+	for (i = 0; i < num_chips; i++) {
 		base = gpio_mockup_ranges[i * 2];
 
 		if (base == -1)
Signature · 1 line
2.9.3
#20 lore raw

PATCH 9/10 gpio: mockup: improve the error message

In reply to Bartosz Golaszewski

Indicate the error number and make the message a bit more elaborate.

---
 drivers/gpio/gpio-mockup.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpio/gpio-mockup.c +3 -2
index 3ffc3ce..96a6c6e 100644
@@ -385,8 +385,9 @@ static int gpio_mockup_probe(struct platform_device *pdev)
 		}
 
 		if (ret) {
-			dev_err(dev, "gpio<%d..%d> add failed\n",
-				base, base < 0 ? ngpio : base + ngpio);
+			dev_err(dev,
+				"adding gpiochip failed: %d (base: %d, ngpio: %d)\n",
+				ret, base, base < 0 ? ngpio : base + ngpio);
 
 			return ret;
 		}
Signature · 1 line
2.9.3
#0 lore raw

cover gpio: mockup: updates for 4.13

This series contains a couple bug fixes and other minor updates for the GPIO testing module.

Bartosz Golaszewski (10):
gpio: mockup: fix direction values
gpio: mockup: add prefixes to the direction enum
gpio: mockup: be quiet unless something goes wrong
gpio: mockup: support irqmask and irqunmask
gpio: mockup: improve the debugfs input sanitization
gpio: mockup: refuse to accept an odd number of GPIO ranges
gpio: mockup: improve readability
gpio: mockup: don't return magic numbers from probe()
gpio: mockup: improve the error message
gpio: mockup: add myself as author

 drivers/gpio/gpio-mockup.c | 82 ++++++++++++++++++++++++++++++----------------
 1 file changed, 54 insertions(+), 28 deletions(-)
Signature · 1 line
2.9.3
#1 lore raw

PATCH 1/10 gpio: mockup: fix direction values

The comment in linux/gpio/driver.h says:

@get_direction: returns direction for signal "offset", 0=out, 1=in

We got those switched at some point. Fix the values.

---
 drivers/gpio/gpio-mockup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpio/gpio-mockup.c +2 -2
index c6dadac..c18d011 100644
@@ -29,8 +29,8 @@
 #define	GPIO_MOCKUP_MAX_GC	10
 
 enum {
-	DIR_IN = 0,
-	DIR_OUT,
+	DIR_OUT = 0,
+	DIR_IN = 1,
 };
 
 /*
Signature · 1 line
2.9.3
#6 lore raw

PATCH 3/10 gpio: mockup: be quiet unless something goes wrong

In reply to Bartosz Golaszewski

When inserting and removing the module repeatedly (e.g. when running the libgpiod test-suite) the kernel log gets clobbered with messages reporting successful creation of dummy gpiochips.

Remove this message and only emit logs when something bad happens.

---
 drivers/gpio/gpio-mockup.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/gpio/gpio-mockup.c +0 -3
index b1ee45c..c17578e 100644
@@ -364,9 +364,6 @@ static int gpio_mockup_probe(struct platform_device *pdev)
 
 			return ret;
 		}
-
-		dev_info(dev, "gpio<%d..%d> add successful!",
-			 base, base + ngpio);
 	}
 
 	return 0;
Signature · 1 line
2.9.3
#8 lore raw

PATCH 4/10 gpio: mockup: support irqmask and irqunmask

In reply to Bartosz Golaszewski

Even though this is a testing module, be nice and actually implement these functions.

---
 drivers/gpio/gpio-mockup.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)
drivers/gpio/gpio-mockup.c +20 -6
index c17578e..ba8d62a 100644
@@ -41,6 +41,7 @@ enum {
 struct gpio_mockup_line_status {
 	int dir;
 	bool value;
+	bool irq_enabled;
 };
 
 struct gpio_mockup_irq_context {
@@ -142,12 +143,21 @@ static int gpio_mockup_to_irq(struct gpio_chip *chip, unsigned int offset)
 	return chip->irq_base + offset;
 }
 
-/*
- * While we should generally support irqmask and irqunmask, this driver is
- * for testing purposes only so we don't care.
- */
-static void gpio_mockup_irqmask(struct irq_data *d) { }
-static void gpio_mockup_irqunmask(struct irq_data *d) { }
+static void gpio_mockup_irqmask(struct irq_data *data)
+{
+	struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
+	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
+
+	chip->lines[data->irq - gc->irq_base].irq_enabled = false;
+}
+
+static void gpio_mockup_irqunmask(struct irq_data *data)
+{
+	struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
+	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
+
+	chip->lines[data->irq - gc->irq_base].irq_enabled = true;
+}
 
 static struct irq_chip gpio_mockup_irqchip = {
 	.name		= GPIO_MOCKUP_NAME,
@@ -178,6 +188,7 @@ static int gpio_mockup_irqchip_setup(struct device *dev,
 
 	for (i = 0; i < gc->ngpio; i++) {
 		irq_set_chip(irq_base + i, gc->irqchip);
+		irq_set_chip_data(irq_base + i, gc);
 		irq_set_handler(irq_base + i, &handle_simple_irq);
 		irq_modify_status(irq_base + i,
 				  IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
@@ -206,6 +217,9 @@ static ssize_t gpio_mockup_event_write(struct file *file,
 	chip = priv->chip;
 	gc = &chip->gc;
 
+	if (!chip->lines[priv->offset].irq_enabled)
+		return size;
+
 	if (copy_from_user(&buf, usr_buf, 1))
 		return -EFAULT;
 
Signature · 1 line
2.9.3
#10 lore raw

PATCH 5/10 gpio: mockup: improve the debugfs input sanitization

In reply to Bartosz Golaszewski

We're currently only checking the first character of the input to the debugfs event files, so a string like '0sdfdsf' is valid and indicates a falling edge event.

Be more strict and only allow '0', '1', '0\n' & '1\n'.

---
 drivers/gpio/gpio-mockup.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/gpio/gpio-mockup.c +10 -4
index ba8d62a..b197b93 100644
@@ -208,8 +208,8 @@ static ssize_t gpio_mockup_event_write(struct file *file,
 	struct seq_file *sfile;
 	struct gpio_desc *desc;
 	struct gpio_chip *gc;
+	char buf[2];
 	int val;
-	char buf;
 
 	sfile = file->private_data;
 	priv = sfile->private;
@@ -220,12 +220,18 @@ static ssize_t gpio_mockup_event_write(struct file *file,
 	if (!chip->lines[priv->offset].irq_enabled)
 		return size;
 
-	if (copy_from_user(&buf, usr_buf, 1))
+	if (size > 2)
+		return -EINVAL;
+
+	if (copy_from_user(&buf, usr_buf, 2))
 		return -EFAULT;
 
-	if (buf == '0')
+	if (size == 2 && buf[1] != '\n')
+		return -EINVAL;
+
+	if (buf[0] == '0')
 		val = 0;
-	else if (buf == '1')
+	else if (buf[0] == '1')
 		val = 1;
 	else
 		return -EINVAL;
Signature · 1 line
2.9.3
#15 lore raw

PATCH 6/10 gpio: mockup: refuse to accept an odd number of GPIO ranges

In reply to Bartosz Golaszewski

Currently we ignore the last odd range value, since each chip is described by two values. Be more strict and require the user to pass an even number of ranges.

---
 drivers/gpio/gpio-mockup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpio-mockup.c +1 -1
index b197b93..d771112 100644
@@ -346,7 +346,7 @@ static int gpio_mockup_probe(struct platform_device *pdev)
 	int ret, i, base, ngpio;
 	char *chip_name;
 
-	if (gpio_mockup_params_nr < 2)
+	if (gpio_mockup_params_nr < 2 || (gpio_mockup_params_nr % 2))
 		return -EINVAL;
 
 	chips = devm_kzalloc(dev,
Signature · 1 line
2.9.3
#19 lore raw

PATCH 8/10 gpio: mockup: don't return magic numbers from probe()

In reply to Bartosz Golaszewski

When the requested number of GPIO lines is 0, return -EINVAL, not -1 which is -EPERM.

---
 drivers/gpio/gpio-mockup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpio-mockup.c +1 -1
index f6efa97..3ffc3ce 100644
@@ -381,7 +381,7 @@ static int gpio_mockup_probe(struct platform_device *pdev)
 			ret = gpio_mockup_add(dev, &chips[i],
 					      chip_name, base, ngpio);
 		} else {
-			ret = -1;
+			ret = -EINVAL;
 		}
 
 		if (ret) {
Signature · 1 line
2.9.3
#21 lore raw

PATCH 10/10 gpio: mockup: add myself as author

In reply to Bartosz Golaszewski

Just taking credit for the recent changes and new features. :)

---
 drivers/gpio/gpio-mockup.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpio/gpio-mockup.c +2 -0
index 96a6c6e..d870597 100644
@@ -3,6 +3,7 @@
  *
  * Copyright (C) 2014  Kamlakant Patel <[email protected]>
  * Copyright (C) 2015-2016  Bamvor Jian Zhang <[email protected]>
+ * Copyright (C) 2017 Bartosz Golaszewski <[email protected]>
  *
  * This program is free software; you can redistribute  it and/or modify it
  * under  the terms of  the GNU General  Public License as published by the
@@ -444,5 +445,6 @@ module_exit(mock_device_exit);
 
 MODULE_AUTHOR("Kamlakant Patel <[email protected]>");
 MODULE_AUTHOR("Bamvor Jian Zhang <[email protected]>");
+MODULE_AUTHOR("Bartosz Golaszewski <[email protected]>");
 MODULE_DESCRIPTION("GPIO Testing driver");
 MODULE_LICENSE("GPL v2");
Signature · 1 line
2.9.3
#2 lore raw

Re: [PATCH 01/10] gpio: mockup: fix direction values

In reply to Bartosz Golaszewski
Quoted 5 lines · Bartosz Golaszewski

The comment in linux/gpio/driver.h says:

@get_direction: returns direction for signal "offset", 0=out, 1=in

We got those switched at some point. Fix the values.

Shouldn't it have a Fixes tag?

Quoted 24 lines
---
 drivers/gpio/gpio-mockup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpio/gpio-mockup.c +2 -3
index c6dadac..c18d011 100644
@@ -29,8 +29,8 @@
 #define        GPIO_MOCKUP_MAX_GC      10
 
 enum {
-       DIR_IN = 0,
-       DIR_OUT,
+       DIR_OUT = 0,
+       DIR_IN = 1,
 };
 
 /*
--

2.9.3

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#11 lore raw

Re: [PATCH 05/10] gpio: mockup: improve the debugfs input sanitization

In reply to Bartosz Golaszewski
Quoted 5 lines · Bartosz Golaszewski

We're currently only checking the first character of the input to the debugfs event files, so a string like '0sdfdsf' is valid and indicates a falling edge event.

Be more strict and only allow '0', '1', '0\n' & '1\n'.

Why not to be so strict and use

kstrtobool_from_user();

instead?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#12 lore raw

Re: [PATCH 05/10] gpio: mockup: improve the debugfs input sanitization

Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Bartosz Golaszewski

We're currently only checking the first character of the input to the debugfs event files, so a string like '0sdfdsf' is valid and indicates a falling edge event.

Quoted 7 lines
Quoted 1 line

Be more strict and only allow '0', '1', '0\n' & '1\n'.

Why not to be so strict and use

kstrtobool_from_user();

instead?

Or if you still wish to be strict,
ret = kstrtou8_from_user();
if (ret)
return ret;

if (val > 1)
return -ERANGE;

or alike.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#16 lore raw

Re: [PATCH 06/10] gpio: mockup: refuse to accept an odd number of GPIO ranges

In reply to Bartosz Golaszewski

drivers/gpio/gpio-mockup.c static int gpio_mockup_probe(struct platform_device *pdev) · line 349

Quoted 6 lines · Bartosz Golaszewski

Currently we ignore the last odd range value, since each chip is described by two values. Be more strict and require the user to pass an even number of ranges.

drivers/gpio/gpio-mockup.c +1 -1
-       if (gpio_mockup_params_nr < 2)
+       if (gpio_mockup_params_nr < 2 || (gpio_mockup_params_nr % 2))
     if (((gpio_mockup_params_nr + 1) & ~3) < 3)

3 might have a suffix regarding to the type of variable.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#17 lore raw

Re: [PATCH 06/10] gpio: mockup: refuse to accept an odd number of GPIO ranges

Quoted 11 lines · Andy Shevchenko

drivers/gpio/gpio-mockup.c static int gpio_mockup_probe(struct platform_device *pdev) · line 349

Quoted 6 lines · Bartosz Golaszewski

Currently we ignore the last odd range value, since each chip is described by two values. Be more strict and require the user to pass an even number of ranges.

drivers/gpio/gpio-mockup.c +1 -1
-       if (gpio_mockup_params_nr < 2)
+       if (gpio_mockup_params_nr < 2 || (gpio_mockup_params_nr % 2))
     if (((gpio_mockup_params_nr + 1) & ~3) < 3)

3 might have a suffix regarding to the type of variable.

Oh, ignore that, sorry, for a noise.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#13 lore raw

Re: [PATCH 05/10] gpio: mockup: improve the debugfs input sanitization

In reply to Andy Shevchenko

2017-05-27 18:47 GMT+02:00 Andy Shevchenko <[email protected]>:

Quoted 7 lines
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Bartosz Golaszewski

We're currently only checking the first character of the input to the debugfs event files, so a string like '0sdfdsf' is valid and indicates a falling edge event.

Quoted 8 lines
Quoted 7 lines
Quoted 1 line

Be more strict and only allow '0', '1', '0\n' & '1\n'.

Why not to be so strict and use

kstrtobool_from_user();

instead?

Because it doesn't really make sense here - we're indicating a RISING or FALLING edge event. This doesn't really correspond well with boolean values IMO.

Quoted 9 lines

Or if you still wish to be strict,
ret = kstrtou8_from_user();
if (ret)
return ret;

if (val > 1)
return -ERANGE;

or alike.

This one looks good, I'll include it in v2.

Thanks,
Bartosz

#5 lore raw

Re: [PATCH 02/10] gpio: mockup: add prefixes to the direction enum

In reply to Bartosz Golaszewski
Quoted 6 lines · Bartosz Golaszewski

All internal symbols except for the direction enum follow the same convention and use the gpio_mockup prefix. Add the prefix to the DIR_IN and DIR_OUT definitions as well for consistency across the file.

Patch applied.

Yours,
Linus Walleij

#7 lore raw

Re: [PATCH 03/10] gpio: mockup: be quiet unless something goes wrong

In reply to Bartosz Golaszewski
Quoted 7 lines · Bartosz Golaszewski

When inserting and removing the module repeatedly (e.g. when running the libgpiod test-suite) the kernel log gets clobbered with messages reporting successful creation of dummy gpiochips.

Remove this message and only emit logs when something bad happens.

Patch applied.

Yours,
Linus Walleij

#14 lore raw

Re: [PATCH 05/10] gpio: mockup: improve the debugfs input sanitization

In reply to Bartosz Golaszewski
Quoted 7 lines · Bartosz Golaszewski

We're currently only checking the first character of the input to the debugfs event files, so a string like '0sdfdsf' is valid and indicates a falling edge event.

Be more strict and only allow '0', '1', '0\n' & '1\n'.

Since I applied patches 1-4 you only need to resend from this point when posting v2.

Yours,
Linus Walleij