Skip to contentSkip to conversation

gpio: mockup: support dynamically created and removed chips

PATCH In review Bartosz Golaszewski 7 participants 74 messages Sep 4, 2020 – Sep 11, 2020

Subsystems (6)
Documentation ACPI Library code GPIO +2 more subsystems
lore
Share & tools
lore
#1 lore raw

PATCH 1/23 lib: cmdline: export next_arg()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We want to use this helper in a module (gpio-mockup) for parsing user input when creating dummy devices. Let's export it.

---
 lib/cmdline.c | 1 +
 1 file changed, 1 insertion(+)
lib/cmdline.c +1 -0
index fbb9981a04a4..e194f76d546e 100644
@@ -247,3 +247,4 @@ char *next_arg(char *args, char **param, char **val)
 	/* Chew up trailing spaces. */
 	return skip_spaces(next);
 }
+EXPORT_SYMBOL(next_arg);
Signature · 1 line
2.26.1
#2 lore raw

PATCH 2/23 lib: string_helpers: provide kfree_strarray()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

There's a common pattern of dynamically allocating an array of char pointers and then also dynamically allocating each string in this array. Provide a helper for freeing such a string array with one call.

---
 include/linux/string_helpers.h |  2 ++
 lib/string_helpers.c           | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+)
include/linux/string_helpers.h +2 -0
index 86f150c2a6b6..55b25120a1c6 100644
@@ -94,4 +94,6 @@ char *kstrdup_quotable(const char *src, gfp_t gfp);
 char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
 char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
 
+void kfree_strarray(char **str_array, size_t num_str);
+
 #endif
lib/string_helpers.c +22 -0
index 963050c0283e..22505efc6aae 100644
@@ -649,3 +649,25 @@ char *kstrdup_quotable_file(struct file *file, gfp_t gfp)
 	return pathname;
 }
 EXPORT_SYMBOL_GPL(kstrdup_quotable_file);
+
+/**
+ * kfree_strarray - free a number of dynamically allocated strings contained
+ *                  in an array and the array itself
+ *
+ * @str_array: Dynamically allocated array of strings to free. If NULL - the
+ *             function does nothing.
+ * @num_str: Number of strings (starting from the beginning of the array) to
+ *           free.
+ */
+void kfree_strarray(char **str_array, size_t num_str)
+{
+	unsigned int i;
+
+	if (!str_array)
+		return;
+
+	for (i = 0; i < num_str; i++)
+		kfree(str_array[i]);
+	kfree(str_array);
+}
+EXPORT_SYMBOL_GPL(kfree_strarray);
Signature · 1 line
2.26.1
#11 lore raw

PATCH 4/23 gpiolib: generalize devprop_gpiochip_set_names() for device properties

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

devprop_gpiochip_set_names() is overly complicated with taking the fwnode argument (which requires using dev_fwnode() & of_fwnode_handle() in ACPI and OF GPIO code respectively). Let's just switch to using the generic device properties.

This allows us to pull the code setting line names directly into gpiochip_add_data_with_key() instead of handling it separately for ACPI and OF.

---
 drivers/gpio/gpiolib-acpi.c    |  3 ---
 drivers/gpio/gpiolib-devprop.c | 19 ++++++++++---------
 drivers/gpio/gpiolib-of.c      |  5 -----
 drivers/gpio/gpiolib.c         |  8 ++++----
 include/linux/gpio/driver.h    |  3 +--
 5 files changed, 15 insertions(+), 23 deletions(-)
drivers/gpio/gpiolib-acpi.c +0 -3
index 54ca3c18b291..834a12f3219e 100644
@@ -1221,9 +1221,6 @@ void acpi_gpiochip_add(struct gpio_chip *chip)
 		return;
 	}
 
-	if (!chip->names)
-		devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent));
-
 	acpi_gpiochip_request_regions(acpi_gpio);
 	acpi_gpiochip_scan_gpios(acpi_gpio);
 	acpi_walk_dep_device_list(handle);
drivers/gpio/gpiolib-devprop.c +10 -9
index 26741032fa9e..a28659b4f9c9 100644
@@ -17,25 +17,24 @@
 /**
  * devprop_gpiochip_set_names - Set GPIO line names using device properties
  * @chip: GPIO chip whose lines should be named, if possible
- * @fwnode: Property Node containing the gpio-line-names property
  *
  * Looks for device property "gpio-line-names" and if it exists assigns
  * GPIO line names for the chip. The memory allocated for the assigned
- * names belong to the underlying firmware node and should not be released
+ * names belong to the underlying software node and should not be released
  * by the caller.
  */
-void devprop_gpiochip_set_names(struct gpio_chip *chip,
-				const struct fwnode_handle *fwnode)
+int devprop_gpiochip_set_names(struct gpio_chip *chip)
 {
 	struct gpio_device *gdev = chip->gpiodev;
+	struct device *dev = chip->parent;
 	const char **names;
 	int ret, i;
 	int count;
 
-	count = fwnode_property_read_string_array(fwnode, "gpio-line-names",
+	count = device_property_read_string_array(dev, "gpio-line-names",
 						  NULL, 0);
 	if (count < 0)
-		return;
+		return 0;
 
 	if (count > gdev->ngpio) {
 		dev_warn(&gdev->dev, "gpio-line-names is length %d but should be at most length %d",
@@ -45,19 +44,21 @@ void devprop_gpiochip_set_names(struct gpio_chip *chip,
 
 	names = kcalloc(count, sizeof(*names), GFP_KERNEL);
 	if (!names)
-		return;
+		return -ENOMEM;
 
-	ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
+	ret = device_property_read_string_array(dev, "gpio-line-names",
 						names, count);
 	if (ret < 0) {
 		dev_warn(&gdev->dev, "failed to read GPIO line names\n");
 		kfree(names);
-		return;
+		return ret;
 	}
 
 	for (i = 0; i < count; i++)
 		gdev->descs[i].name = names[i];
 
 	kfree(names);
+
+	return 0;
 }
 EXPORT_SYMBOL_GPL(devprop_gpiochip_set_names);
drivers/gpio/gpiolib-of.c +0 -5
index bd31dd3b6a75..2f895a2b8411 100644
@@ -1026,11 +1026,6 @@ int of_gpiochip_add(struct gpio_chip *chip)
 	if (ret)
 		return ret;
 
-	/* If the chip defines names itself, these take precedence */
-	if (!chip->names)
-		devprop_gpiochip_set_names(chip,
-					   of_fwnode_handle(chip->of_node));
-
 	of_node_get(chip->of_node);
 
 	ret = of_gpiochip_scan_gpios(chip);
drivers/gpio/gpiolib.c +4 -4
index 80137c1b3cdc..0d390f0ec32c 100644
@@ -340,9 +340,6 @@ static int gpiochip_set_desc_names(struct gpio_chip *gc)
 	struct gpio_device *gdev = gc->gpiodev;
 	int i;
 
-	if (!gc->names)
-		return 0;
-
 	/* First check all names if they are unique */
 	for (i = 0; i != gc->ngpio; ++i) {
 		struct gpio_desc *gpio;
@@ -621,7 +618,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 	INIT_LIST_HEAD(&gdev->pin_ranges);
 #endif
 
-	ret = gpiochip_set_desc_names(gc);
+	if (gc->names)
+		ret = gpiochip_set_desc_names(gc);
+	else
+		ret = devprop_gpiochip_set_names(gc);
 	if (ret)
 		goto err_remove_from_list;
 
include/linux/gpio/driver.h +1 -2
index d1cef5c2715c..56485a040b82 100644
@@ -756,8 +756,7 @@ struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
 					    enum gpiod_flags dflags);
 void gpiochip_free_own_desc(struct gpio_desc *desc);
 
-void devprop_gpiochip_set_names(struct gpio_chip *gc,
-				const struct fwnode_handle *fwnode);
+int devprop_gpiochip_set_names(struct gpio_chip *gc);
 
 #ifdef CONFIG_GPIOLIB
 
Signature · 1 line
2.26.1
#14 lore raw

PATCH 5/23 gpiolib: unexport devprop_gpiochip_set_names()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Now that devprop_gpiochip_set_names() is only used in a single place inside drivers/gpio/gpiolib.c, there's no need anymore for it to be exported or to even live in its own source file. Pull this function into the core source file for gpiolib.

---
 drivers/gpio/Makefile          |  1 -
 drivers/gpio/gpiolib-devprop.c | 64 ----------------------------------
 drivers/gpio/gpiolib.c         | 48 +++++++++++++++++++++++++
 include/linux/gpio/driver.h    |  2 --
 4 files changed, 48 insertions(+), 67 deletions(-)
 delete mode 100644 drivers/gpio/gpiolib-devprop.c
drivers/gpio/Makefile +0 -1
index 4f9abff4f2dc..639275eb4e4d 100644
@@ -6,7 +6,6 @@ ccflags-$(CONFIG_DEBUG_GPIO)	+= -DDEBUG
 obj-$(CONFIG_GPIOLIB)		+= gpiolib.o
 obj-$(CONFIG_GPIOLIB)		+= gpiolib-devres.o
 obj-$(CONFIG_GPIOLIB)		+= gpiolib-legacy.o
-obj-$(CONFIG_GPIOLIB)		+= gpiolib-devprop.o
 obj-$(CONFIG_GPIOLIB)		+= gpiolib-cdev.o
 obj-$(CONFIG_OF_GPIO)		+= gpiolib-of.o
 obj-$(CONFIG_GPIO_SYSFS)	+= gpiolib-sysfs.o
drivers/gpio/gpiolib-devprop.c deleted +0 -64

64 lines not on this page · raw message

drivers/gpio/gpiolib.c +48 -0

54 lines not on this page · raw message

include/linux/gpio/driver.h +0 -2
index 56485a040b82..4a7e295c3640 100644
@@ -756,8 +756,6 @@ struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
 					    enum gpiod_flags dflags);
 void gpiochip_free_own_desc(struct gpio_desc *desc);
 
-int devprop_gpiochip_set_names(struct gpio_chip *gc);
-
 #ifdef CONFIG_GPIOLIB
 
 /* lock/unlock as IRQ */
Signature · 1 line
2.26.1
#17 lore raw

PATCH 6/23 gpiolib: switch to simpler IDA interface

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We don't need to specify any ranges when allocating IDs so we can switch to ida_alloc() and ida_free() instead of the ida_simple_ counterparts.

ida_simple_get(ida, 0, 0, gfp) is equivalent to ida_alloc_range(ida, 0, UINT_MAX, gfp) which is equivalent to ida_alloc(ida, gfp). Note: IDR will never actually allocate an ID larger than INT_MAX.

---
 drivers/gpio/gpiolib.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpio/gpiolib.c +3 -3
index 15c99cf560ee..591777bc2285 100644
@@ -471,7 +471,7 @@ static void gpiodevice_release(struct device *dev)
 	struct gpio_device *gdev = dev_get_drvdata(dev);
 
 	list_del(&gdev->list);
-	ida_simple_remove(&gpio_ida, gdev->id);
+	ida_free(&gpio_ida, gdev->id);
 	kfree_const(gdev->label);
 	kfree(gdev->descs);
 	kfree(gdev);
@@ -582,7 +582,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 		gc->of_node = gdev->dev.of_node;
 #endif
 
-	gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
+	gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
 	if (gdev->id < 0) {
 		ret = gdev->id;
 		goto err_free_gdev;
@@ -753,7 +753,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 err_free_descs:
 	kfree(gdev->descs);
 err_free_ida:
-	ida_simple_remove(&gpio_ida, gdev->id);
+	ida_free(&gpio_ida, gdev->id);
 err_free_gdev:
 	/* failures here can mean systems won't boot... */
 	pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
Signature · 1 line
2.26.1
#20 lore raw

PATCH 7/23 gpio: mockup: drop unneeded includes

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This module doesn't need gpio/consumer.h - it's a provider. It also doesn't use any symbols from init.h so let's remove both includes.

---
 drivers/gpio/gpio-mockup.c | 2 --
 1 file changed, 2 deletions(-)
drivers/gpio/gpio-mockup.c +0 -2
index bc345185db26..349782cdb4d7 100644
@@ -8,9 +8,7 @@
  */
 
 #include <linux/debugfs.h>
-#include <linux/gpio/consumer.h>
 #include <linux/gpio/driver.h>
-#include <linux/init.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/irq_sim.h>
Signature · 1 line
2.26.1
#43 lore raw

PATCH 16/23 gpio: mockup: refactor the module init function

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This is in preparation for dynamically created chips.

Let's split out the code that can be reused when creating chips at run-time. Let's also move the code preparing the device properties into a separate routine. This has the advantage of simplifying the error handling.

---
 drivers/gpio/gpio-mockup.c | 165 ++++++++++++++++++++-----------------
 1 file changed, 90 insertions(+), 75 deletions(-)
drivers/gpio/gpio-mockup.c +90 -75
index 995e37fef9c5..eb94ddac5fee 100644
@@ -524,16 +524,78 @@ static __init char **gpio_mockup_make_line_names(const char *label,
 	return names;
 }
 
-static int __init gpio_mockup_init(void)
+static int __init gpio_mockup_register_device(struct property_entry *properties)
 {
-	struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
-	char chip_label[GPIO_MOCKUP_LABEL_SIZE];
 	struct gpio_mockup_device *mockup_dev;
-	int i, prop, num_chips, err = 0, base;
 	struct platform_device_info pdevinfo;
-	char **line_names;
+
+	memset(&pdevinfo, 0, sizeof(pdevinfo));
+
+	mockup_dev = kzalloc(sizeof(*mockup_dev), GFP_KERNEL);
+	if (!mockup_dev)
+		return -ENOMEM;
+
+	pdevinfo.name = "gpio-mockup";
+	pdevinfo.properties = properties;
+
+	pdevinfo.id = ida_alloc(&gpio_mockup_ida, GFP_KERNEL);
+	if (pdevinfo.id < 0) {
+		kfree(mockup_dev);
+		return pdevinfo.id;
+	}
+
+	mockup_dev->pdev = platform_device_register_full(&pdevinfo);
+	if (IS_ERR(mockup_dev->pdev)) {
+		ida_free(&gpio_mockup_ida, pdevinfo.id);
+		kfree(mockup_dev);
+		return PTR_ERR(mockup_dev->pdev);
+	}
+
+	list_add(&mockup_dev->list, &gpio_mockup_devices);
+
+	return 0;
+}
+
+static int __init gpio_mockup_register_one_chip_from_params(int idx)
+{
+	char chip_label[GPIO_MOCKUP_LABEL_SIZE], **line_names = NULL;
+	struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
+	int prop = 0, base, ret;
 	u16 ngpio;
 
+	memset(properties, 0, sizeof(properties));
+
+	snprintf(chip_label, sizeof(chip_label), "gpio-mockup-%c", idx + 'A');
+	properties[prop++] = PROPERTY_ENTRY_STRING("chip-label",
+						   chip_label);
+
+	base = gpio_mockup_range_base(idx);
+	if (base >= 0)
+		properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
+							base);
+
+	ngpio = base < 0 ? gpio_mockup_range_ngpio(idx)
+			 : gpio_mockup_range_ngpio(idx) - base;
+	properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
+
+	if (gpio_mockup_named_lines) {
+		line_names = gpio_mockup_make_line_names(chip_label, ngpio);
+		if (!line_names)
+			return -ENOMEM;
+
+		properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
+					"gpio-line-names", line_names, ngpio);
+	}
+
+	ret = gpio_mockup_register_device(properties);
+	kfree_strarray(line_names, ngpio);
+	return ret;
+}
+
+static int __init gpio_mockup_register_chips_from_params(void)
+{
+	int num_chips, i, ret;
+
 	if ((gpio_mockup_num_ranges < 2) ||
 	    (gpio_mockup_num_ranges % 2) ||
 	    (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
@@ -551,86 +613,39 @@ static int __init gpio_mockup_init(void)
 			return -EINVAL;
 	}
 
-	gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
-
-	err = platform_driver_register(&gpio_mockup_driver);
-	if (err) {
-		pr_err("error registering platform driver\n");
-		debugfs_remove_recursive(gpio_mockup_dbg_dir);
-		return err;
-	}
-
 	for (i = 0; i < num_chips; i++) {
-		memset(properties, 0, sizeof(properties));
-		memset(&pdevinfo, 0, sizeof(pdevinfo));
-		prop = 0;
-		line_names = NULL;
-
-		snprintf(chip_label, sizeof(chip_label),
-			 "gpio-mockup-%c", i + 'A');
-		properties[prop++] = PROPERTY_ENTRY_STRING("chip-label",
-							   chip_label);
-
-		base = gpio_mockup_range_base(i);
-		if (base >= 0)
-			properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
-								base);
-
-		ngpio = base < 0 ? gpio_mockup_range_ngpio(i)
-				 : gpio_mockup_range_ngpio(i) - base;
-		properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
-
-		if (gpio_mockup_named_lines) {
-			line_names = gpio_mockup_make_line_names(chip_label,
-								 ngpio);
-			if (!line_names) {
-				err = -ENOMEM;
-				goto err_out;
-			}
-
-			properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
-						"gpio-line-names",
-						line_names, ngpio);
+		ret = gpio_mockup_register_one_chip_from_params(i);
+		if (ret) {
+			gpio_mockup_unregister_devices();
+			return ret;
 		}
+	}
 
-		pdevinfo.name = "gpio-mockup";
-		pdevinfo.properties = properties;
+	return 0;
+}
 
-		pdevinfo.id = ida_alloc(&gpio_mockup_ida, GFP_KERNEL);
-		if (pdevinfo.id < 0) {
-			kfree_strarray(line_names, ngpio);
-			err = pdevinfo.id;
-			goto err_out;
-		}
+static int __init gpio_mockup_init(void)
+{
+	int ret;
 
-		mockup_dev = kzalloc(sizeof(*mockup_dev), GFP_KERNEL);
-		if (!mockup_dev) {
-			kfree_strarray(line_names, ngpio);
-			ida_free(&gpio_mockup_ida, pdevinfo.id);
-			err = -ENOMEM;
-			goto err_out;
-		}
+	gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
 
-		mockup_dev->pdev = platform_device_register_full(&pdevinfo);
-		kfree_strarray(line_names, ngpio);
-		if (IS_ERR(mockup_dev->pdev)) {
-			pr_err("error registering device");
-			ida_free(&gpio_mockup_ida, pdevinfo.id);
-			kfree(mockup_dev);
-			err = PTR_ERR(mockup_dev->pdev);
-			goto err_out;
-		}
+	ret = platform_driver_register(&gpio_mockup_driver);
+	if (ret) {
+		pr_err("error registering platform driver\n");
+		debugfs_remove_recursive(gpio_mockup_dbg_dir);
+		return ret;
+	}
 
-		list_add(&mockup_dev->list, &gpio_mockup_devices);
+	ret = gpio_mockup_register_chips_from_params();
+	if (ret) {
+		pr_err("error registering device");
+		debugfs_remove_recursive(gpio_mockup_dbg_dir);
+		platform_driver_unregister(&gpio_mockup_driver);
+		return ret;
 	}
 
 	return 0;
-
-err_out:
-	platform_driver_unregister(&gpio_mockup_driver);
-	gpio_mockup_unregister_devices();
-	debugfs_remove_recursive(gpio_mockup_dbg_dir);
-	return err;
 }
 
 static void __exit gpio_mockup_exit(void)
Signature · 1 line
2.26.1
#38 lore raw

PATCH 15/23 gpio: mockup: use dynamic device IDs

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We're currently creating chips at module init time only so using a static index for dummy devices is fine. We want to support dynamically created chips however so we need to switch to dynamic device IDs.

---
 drivers/gpio/gpio-mockup.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
drivers/gpio/gpio-mockup.c +16 -1
index 96976ba66598..995e37fef9c5 100644
@@ -9,6 +9,7 @@
 
 #include <linux/debugfs.h>
 #include <linux/gpio/driver.h>
+#include <linux/idr.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/irq_sim.h>
@@ -70,6 +71,8 @@ module_param_named(gpio_mockup_named_lines,
 
 static struct dentry *gpio_mockup_dbg_dir;
 
+static DEFINE_IDA(gpio_mockup_ida);
+
 static int gpio_mockup_range_base(unsigned int index)
 {
 	return gpio_mockup_ranges[index * 2];
@@ -480,8 +483,12 @@ static LIST_HEAD(gpio_mockup_devices);
 
 static void gpio_mockup_unregister_one_device(struct gpio_mockup_device *dev)
 {
+	int id;
+
 	list_del(&dev->list);
+	id = dev->pdev->id;
 	platform_device_unregister(dev->pdev);
+	ida_free(&gpio_mockup_ida, id);
 	kfree(dev);
 }
 
@@ -587,12 +594,19 @@ static int __init gpio_mockup_init(void)
 		}
 
 		pdevinfo.name = "gpio-mockup";
-		pdevinfo.id = i;
 		pdevinfo.properties = properties;
 
+		pdevinfo.id = ida_alloc(&gpio_mockup_ida, GFP_KERNEL);
+		if (pdevinfo.id < 0) {
+			kfree_strarray(line_names, ngpio);
+			err = pdevinfo.id;
+			goto err_out;
+		}
+
 		mockup_dev = kzalloc(sizeof(*mockup_dev), GFP_KERNEL);
 		if (!mockup_dev) {
 			kfree_strarray(line_names, ngpio);
+			ida_free(&gpio_mockup_ida, pdevinfo.id);
 			err = -ENOMEM;
 			goto err_out;
 		}
@@ -601,6 +615,7 @@ static int __init gpio_mockup_init(void)
 		kfree_strarray(line_names, ngpio);
 		if (IS_ERR(mockup_dev->pdev)) {
 			pr_err("error registering device");
+			ida_free(&gpio_mockup_ida, pdevinfo.id);
 			kfree(mockup_dev);
 			err = PTR_ERR(mockup_dev->pdev);
 			goto err_out;
Signature · 1 line
2.26.1
#35 lore raw

PATCH 14/23 gpio: mockup: use the generic 'gpio-line-names' property

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

GPIO line names are currently created by the driver from the chip label. We'll want to support custom formats for line names (for instance: to name all lines the same) for user-space tests so create them in the module init function and pass them to the driver using the standard 'gpio-line-names' property.

---
 drivers/gpio/gpio-mockup.c | 70 +++++++++++++++++++++-----------------
 1 file changed, 38 insertions(+), 32 deletions(-)
drivers/gpio/gpio-mockup.c +38 -32
index ce83f1df1933..96976ba66598 100644
@@ -18,6 +18,7 @@
 #include <linux/platform_device.h>
 #include <linux/property.h>
 #include <linux/slab.h>
+#include <linux/string_helpers.h>
 #include <linux/uaccess.h>
 
 #include "gpiolib.h"
@@ -378,29 +379,6 @@ static void gpio_mockup_debugfs_setup(struct device *dev,
 	return;
 }
 
-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_kcalloc(dev, gc->ngpio, sizeof(char *), 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 void gpio_mockup_dispose_mappings(void *data)
 {
 	struct gpio_mockup_chip *chip = data;
@@ -468,12 +446,6 @@ static int gpio_mockup_probe(struct platform_device *pdev)
 	for (i = 0; i < gc->ngpio; i++)
 		chip->lines[i].dir = GPIO_LINE_DIRECTION_IN;
 
-	if (device_property_read_bool(dev, "named-gpio-lines")) {
-		rv = gpio_mockup_name_lines(dev, chip);
-		if (rv)
-			return rv;
-	}
-
 	chip->irq_sim_domain = devm_irq_domain_create_sim(dev, NULL,
 							  gc->ngpio);
 	if (IS_ERR(chip->irq_sim_domain))
@@ -524,6 +496,27 @@ static void gpio_mockup_unregister_devices(void)
 	}
 }
 
+static __init char **gpio_mockup_make_line_names(const char *label,
+						 unsigned int num_lines)
+{
+	unsigned int i;
+	char **names;
+
+	names = kcalloc(num_lines + 1, sizeof(char *), GFP_KERNEL);
+	if (!names)
+		return NULL;
+
+	for (i = 0; i < num_lines; i++) {
+		names[i] = kasprintf(GFP_KERNEL, "%s-%u", label, i);
+		if (!names[i]) {
+			kfree_strarray(names, i);
+			return NULL;
+		}
+	}
+
+	return names;
+}
+
 static int __init gpio_mockup_init(void)
 {
 	struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
@@ -531,6 +524,7 @@ static int __init gpio_mockup_init(void)
 	struct gpio_mockup_device *mockup_dev;
 	int i, prop, num_chips, err = 0, base;
 	struct platform_device_info pdevinfo;
+	char **line_names;
 	u16 ngpio;
 
 	if ((gpio_mockup_num_ranges < 2) ||
@@ -563,6 +557,7 @@ static int __init gpio_mockup_init(void)
 		memset(properties, 0, sizeof(properties));
 		memset(&pdevinfo, 0, sizeof(pdevinfo));
 		prop = 0;
+		line_names = NULL;
 
 		snprintf(chip_label, sizeof(chip_label),
 			 "gpio-mockup-%c", i + 'A');
@@ -578,9 +573,18 @@ static int __init gpio_mockup_init(void)
 				 : gpio_mockup_range_ngpio(i) - base;
 		properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
 
-		if (gpio_mockup_named_lines)
-			properties[prop++] = PROPERTY_ENTRY_BOOL(
-						"named-gpio-lines");
+		if (gpio_mockup_named_lines) {
+			line_names = gpio_mockup_make_line_names(chip_label,
+								 ngpio);
+			if (!line_names) {
+				err = -ENOMEM;
+				goto err_out;
+			}
+
+			properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
+						"gpio-line-names",
+						line_names, ngpio);
+		}
 
 		pdevinfo.name = "gpio-mockup";
 		pdevinfo.id = i;
@@ -588,11 +592,13 @@ static int __init gpio_mockup_init(void)
 
 		mockup_dev = kzalloc(sizeof(*mockup_dev), GFP_KERNEL);
 		if (!mockup_dev) {
+			kfree_strarray(line_names, ngpio);
 			err = -ENOMEM;
 			goto err_out;
 		}
 
 		mockup_dev->pdev = platform_device_register_full(&pdevinfo);
+		kfree_strarray(line_names, ngpio);
 		if (IS_ERR(mockup_dev->pdev)) {
 			pr_err("error registering device");
 			kfree(mockup_dev);
Signature · 1 line
2.26.1
#49 lore raw

PATCH 19/23 gpio: mockup: add a symlink for the per-chip debugfs directory

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We used to have a symlink named after the chip's label linking to the per-chip directory named after the chip's name. This was removed by commit d51ee07a8de7 ("gpio: mockup: don't create the debugfs link named after the label") because there were no users of it.

This changeset proposes to reintroduce debugfs symlinks but inverted:
the link named after the device name points to the directory named after the label. This way user-space can dynamically create a chip (once that functionality is available), detect its creation over uevent and match the device name to the label by resolving the link.

---
 drivers/gpio/gpio-mockup.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/gpio/gpio-mockup.c +19 -1
index 7df990662c17..bc4609e047ef 100644
@@ -52,6 +52,7 @@ struct gpio_mockup_chip {
 	struct gpio_mockup_line_status *lines;
 	struct irq_domain *irq_sim_domain;
 	struct dentry *dbg_dir;
+	struct dentry *dbg_link;
 	struct mutex lock;
 };
 
@@ -355,6 +356,13 @@ static void gpio_mockup_remove_chip_debugfs_entry(void *data)
 	debugfs_remove_recursive(entry);
 }
 
+static void gpio_mockup_remove_chip_debugfs_link(void *data)
+{
+	struct dentry *link = data;
+
+	debugfs_remove(link);
+}
+
 static int gpio_mockup_debugfs_setup(struct device *dev,
 				     struct gpio_mockup_chip *chip)
 {
@@ -368,7 +376,7 @@ static int gpio_mockup_debugfs_setup(struct device *dev,
 	gc = &chip->gc;
 	devname = dev_name(&gc->gpiodev->dev);
 
-	chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
+	chip->dbg_dir = debugfs_create_dir(gc->label, gpio_mockup_dbg_dir);
 	if (IS_ERR(chip->dbg_dir))
 		return PTR_ERR(chip->dbg_dir);
 
@@ -377,6 +385,16 @@ static int gpio_mockup_debugfs_setup(struct device *dev,
 	if (ret)
 		return ret;
 
+	chip->dbg_link = debugfs_create_symlink(devname, gpio_mockup_dbg_dir,
+						gc->label);
+	if (IS_ERR(chip->dbg_link))
+		return PTR_ERR(chip->dbg_link);
+
+	ret = devm_add_action_or_reset(dev,
+			gpio_mockup_remove_chip_debugfs_link, chip->dbg_link);
+	if (ret)
+		return ret;
+
 	for (i = 0; i < gc->ngpio; i++) {
 		name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
 		if (!name)
Signature · 1 line
2.26.1
#48 lore raw

PATCH 18/23 gpio: mockup: require debugfs to build

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Debugfs has become the standard way of interfacing with gpio-mockup to the point where the module is not very useful without it anymore. Let's make it a hard requirement to build gpio-mockup.

Let's also add error checks whenever calling debugfs routines as we now don't expect them to fail.

The device sub-directories must now be removed when the device is detached to correctly support dynamically created chips.

The call to debugfs_remove_recursive() in module exit must be moved to the bottom or we'd risk to remove the root directory before devices can unregister their own sub-directories.

---
 drivers/gpio/Kconfig       |  1 +
 drivers/gpio/gpio-mockup.c | 41 ++++++++++++++++++++++++++++----------
 2 files changed, 32 insertions(+), 10 deletions(-)
drivers/gpio/Kconfig +1 -0
index 8030fd91a3cc..515f345757d8 100644
@@ -1567,6 +1567,7 @@ config GPIO_AGGREGATOR
 
 config GPIO_MOCKUP
 	tristate "GPIO Testing Driver"
+	depends on DEBUG_FS
 	select IRQ_SIM
 	help
 	  This enables GPIO Testing driver, which provides a way to test GPIO
drivers/gpio/gpio-mockup.c +31 -10

91 lines not on this page · raw message

Signature · 1 line
2.26.1
#54 lore raw

PATCH 23/23 Documentation: gpio: add documentation for gpio-mockup

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

There's some documentation for gpio-mockup's debugfs interface in the driver's source but it's not much. Add proper documentation for this testing module.

---
 .../admin-guide/gpio/gpio-mockup.rst          | 87 +++++++++++++++++++
 1 file changed, 87 insertions(+)
 create mode 100644 Documentation/admin-guide/gpio/gpio-mockup.rst
Documentation/admin-guide/gpio/gpio-mockup.rst new file +87 -0
new file mode 100644
index 000000000000..1d452ee55f8d
@@ -0,0 +1,87 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+GPIO Testing Driver
+===================
+
+The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO
+chips for testing purposes. There are two ways of configuring the chips exposed
+by the module. The lines can be accessed using the standard GPIO character
+device interface as well as manipulated using the dedicated debugfs directory
+structure.
+
+Creating simulated chips using debugfs
+--------------------------------------
+
+When the gpio-mockup module is loaded (or builtin) it creates its own directory
+in debugfs. Assuming debugfs is mounted at /sys/kernel/debug/, the directory
+will be located at /sys/kernel/debug/gpio-mockup/. Inside this directory there
+are two attributes: new_device and delete_device.
+
+New chips can be created by writing a single line containing a number of
+options to "new_device". For example:
+
+.. code-block:: sh
+
+    $ echo "label=my-mockup num_lines=4 named_lines" > /sys/kernel/debug/gpio-mockup/new_device
+
+Supported options:
+
+    num_lines=<num_lines> - number of GPIO lines to expose
+
+    label=<label> - label of the dummy chip
+
+    named_lines - defines whether dummy lines should be named, the names are
+                  of the form X-Y where X is the chip's label and Y is the
+                  line's offset
+
+Note: only num_lines is mandatory.
+
+Chips can be dynamically removed by writing the chip's label to
+"delete_device". For example:
+
+.. code-block:: sh
+
+    echo "gpio-mockup.0" > /sys/kernel/debug/gpio-mockup/delete_device
+
+Creating simulated chips using module params
+--------------------------------------------
+
+Note: this is an older, now deprecated method kept for backward compatibility
+for user-space tools.
+
+When loading the gpio-mockup driver a number of parameters can be passed to the
+module.
+
+    gpio_mockup_ranges
+
+        This parameter takes an argument in the form of an array of integer
+        pairs. Each pair defines the base GPIO number (if any) and the number
+        of lines exposed by the chip. If the base GPIO is -1, the gpiolib
+        will assign it automatically.
+
+        Example: gpio_mockup_ranges=-1,8,-1,16,405,4
+
+        The line above creates three chips. The first one will expose 8 lines,
+        the second 16 and the third 4. The base GPIO for the third chip is set
+        to 405 while for two first chips it will be assigned automatically.
+
+    gpio_named_lines
+
+        This parameter doesn't take any arguments. It lets the driver know that
+        GPIO lines exposed by it should be named.
+
+        The name format is: gpio-mockup-X-Y where X is the letter associated
+        with the mockup chip and Y is the line offset.
+
+Manipulating simulated lines
+----------------------------
+
+Each mockup chip creates its own subdirectory in /sys/kernel/debug/gpio-mockup/.
+The directory is named after the chip's label. A symlink is also created, named
+after the chip's name, which points to the label directory.
+
+Inside each subdirectory, there's a separate attribute for each GPIO line. The
+name of the attribute represents the line's offset in the chip.
+
+Reading from a line attribute returns the current value. Writing to it (0 or 1)
+changes its pull.
Signature · 1 line
2.26.1
#53 lore raw

PATCH 22/23 gpio: mockup: provide a way to create new dummy chips

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Add a new debugfs attribute 'new_device' that allows to dynamically create new dummy chips according to specification.

New chips are created by writing a number of supported parameters to the new attribute of which 'label' and 'num_lines' are mandatory. The new attribute is designed to be easily exstensible with new parameters. For now we simply provide the same functionality that the module params expose but with the intention of introducing new options (such as custom line name formats).

---
 drivers/gpio/gpio-mockup.c | 231 +++++++++++++++++++++++++++++++++++--
 1 file changed, 220 insertions(+), 11 deletions(-)
drivers/gpio/gpio-mockup.c +220 -11
index 9d2de78a45c2..6577d18671df 100644
@@ -5,6 +5,7 @@
  * Copyright (C) 2014  Kamlakant Patel <[email protected]>
  * Copyright (C) 2015-2016  Bamvor Jian Zhang <[email protected]>
  * Copyright (C) 2017 Bartosz Golaszewski <[email protected]>
+ * Copyright (C) 2020 Bartosz Golaszewski <[email protected]>
  */
 
 #include <linux/debugfs.h>
@@ -14,6 +15,7 @@
 #include <linux/irq.h>
 #include <linux/irq_sim.h>
 #include <linux/irqdomain.h>
+#include <linux/kernel.h>
 #include <linux/list.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
@@ -546,8 +548,8 @@ static void gpio_mockup_unregister_devices(void)
 	mutex_unlock(&gpio_mockup_devices_lock);
 }
 
-static __init char **gpio_mockup_make_line_names(const char *label,
-						 unsigned int num_lines)
+static char **gpio_mockup_make_line_names(const char *label,
+					  unsigned int num_lines)
 {
 	unsigned int i;
 	char **names;
@@ -567,7 +569,7 @@ static __init char **gpio_mockup_make_line_names(const char *label,
 	return names;
 }
 
-static int __init gpio_mockup_register_device(struct property_entry *properties)
+static int gpio_mockup_register_device(struct property_entry *properties)
 {
 	struct gpio_mockup_device *mockup_dev;
 	struct platform_device_info pdevinfo;
@@ -641,8 +643,7 @@ static int __init gpio_mockup_register_chips_from_params(void)
 {
 	int num_chips, i, ret;
 
-	if ((gpio_mockup_num_ranges < 2) ||
-	    (gpio_mockup_num_ranges % 2) ||
+	if ((gpio_mockup_num_ranges % 2) ||
 	    (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
 		return -EINVAL;
 
@@ -669,6 +670,205 @@ static int __init gpio_mockup_register_chips_from_params(void)
 	return 0;
 }
 
+/*
+ * We store all data associated with device properties in this structure. It's
+ * only needed until we register the platform device at which point the driver
+ * core makes a deep copy of all property data (even the string arrays).
+ *
+ * The reason to keep them bunched up is simple: we can have a single function
+ * to free all resources which simplifies error handling.
+ */
+struct gpio_mockup_prop_data {
+	char chip_label[GPIO_MOCKUP_LABEL_SIZE];
+	u16 ngpio;
+	bool named_lines;
+	char **line_names;
+};
+
+/* We don't free the structure itself - it's expected to live on the stack. */
+static void
+gpio_mockup_free_property_data(struct gpio_mockup_prop_data *prop_data)
+{
+	kfree_strarray(prop_data->line_names, prop_data->ngpio);
+}
+
+/*
+ * Each supported option is parsed by a separate callback - this way the
+ * 'new_device' attribute is easily exstensible.
+ */
+struct gpio_mockup_new_device_opt {
+	char *name;
+	int (*func)(const char *val, struct gpio_mockup_prop_data *prop_data,
+		    struct property_entry *properties, int *prop_idx);
+	bool has_val;
+};
+
+static int
+gpio_mockup_parse_label(const char *val,
+		struct gpio_mockup_prop_data *prop_data,
+		struct property_entry *properties, int *prop_idx)
+{
+	snprintf(prop_data->chip_label, sizeof(prop_data->chip_label), val);
+	properties[(*prop_idx)++] =
+		PROPERTY_ENTRY_STRING("chip-label", prop_data->chip_label);
+
+	return 0;
+}
+
+static int gpio_mockup_parse_num_lines(const char *val,
+			struct gpio_mockup_prop_data *prop_data,
+			struct property_entry *properties, int *prop_idx)
+{
+	int ret;
+
+	ret = kstrtou16(val, 10, &prop_data->ngpio);
+	if (ret) {
+		pr_err("invalid new_lines format: %s\n", val);
+		return ret;
+	}
+
+	properties[(*prop_idx)++] = PROPERTY_ENTRY_U16("nr-gpios",
+						     prop_data->ngpio);
+
+	return 0;
+}
+
+static int gpio_mockup_parse_named_lines(const char *val,
+			struct gpio_mockup_prop_data *prop_data,
+			struct property_entry *properties, int *prop_idx)
+{
+	prop_data->named_lines = true;
+
+	return 0;
+}
+
+static struct gpio_mockup_new_device_opt gpio_mockup_new_device_opts[] = {
+	{
+		.name = "label",
+		.func = gpio_mockup_parse_label,
+		.has_val = true,
+	},
+	{
+		.name = "num_lines",
+		.func = gpio_mockup_parse_num_lines,
+		.has_val = true,
+	},
+	{
+		.name = "named_lines",
+		.func = gpio_mockup_parse_named_lines,
+		.has_val = false,
+	},
+};
+
+static int
+gpio_mockup_parse_one_opt(const char *key, const char *val,
+			  struct gpio_mockup_prop_data *prop_data,
+			  struct property_entry *properties, int *prop_idx)
+{
+	struct gpio_mockup_new_device_opt *opt;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(gpio_mockup_new_device_opts); i++) {
+		opt = &gpio_mockup_new_device_opts[i];
+
+		if (strcmp(key, opt->name) == 0) {
+			if (opt->has_val && !val) {
+				pr_err("%s option requires an argument\n",
+				       opt->name);
+				return -EINVAL;
+			}
+
+			if (!opt->has_val && val) {
+				pr_err("%s option doesn't take any arguments\n",
+				       opt->name);
+				return -EINVAL;
+			}
+
+			return opt->func(val, prop_data, properties, prop_idx);
+		}
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static int
+gpio_mockup_new_device_from_opts(char *opts,
+				 struct gpio_mockup_prop_data *prop_data)
+{
+	struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
+	int prop_idx = 0, ret;
+	char *key, *val;
+
+	memset(properties, 0, sizeof(properties));
+
+	while (*opts) {
+		if (prop_idx >= GPIO_MOCKUP_MAX_PROP)
+			return -EINVAL;
+
+		opts = next_arg(opts, &key, &val);
+
+		ret = gpio_mockup_parse_one_opt(key, val, prop_data,
+						properties, &prop_idx);
+		if (ret)
+			return ret;
+	}
+
+	/* This is the only mandatory property. */
+	if (!prop_data->ngpio) {
+		pr_err("number of lines must be specified\n");
+		return -EINVAL;
+	}
+
+	/*
+	 * Line names must be created at the end - once we know how
+	 * many GPIOs there are.
+	 */
+	if (prop_data->named_lines) {
+		prop_data->line_names = gpio_mockup_make_line_names(
+							prop_data->chip_label,
+							prop_data->ngpio);
+		if (!prop_data->line_names)
+			return -ENOMEM;
+
+		properties[prop_idx++] =
+			PROPERTY_ENTRY_STRING_ARRAY_LEN("gpio-line-names",
+							prop_data->line_names,
+							prop_data->ngpio);
+	}
+
+	return gpio_mockup_register_device(properties);
+}
+
+static ssize_t gpio_mockup_debugfs_new_device_write(struct file *file,
+						    const char __user *usr_buf,
+						    size_t size, loff_t *ppos)
+{
+	struct gpio_mockup_prop_data prop_data;
+	char opts[128];
+	int ret;
+
+	if (*ppos != 0 || size > sizeof(opts))
+		return -EINVAL;
+
+	ret = getline_from_user(opts, sizeof(opts), usr_buf, size);
+	if (ret < 0)
+		return ret;
+
+	memset(&prop_data, 0, sizeof(prop_data));
+
+	ret = gpio_mockup_new_device_from_opts(opts, &prop_data);
+	gpio_mockup_free_property_data(&prop_data);
+	return ret < 0 ? ret : size;
+}
+
+static const struct file_operations gpio_mockup_debugfs_new_device_ops = {
+	.owner = THIS_MODULE,
+	.open = gpio_mockup_debugfs_open,
+	.write = gpio_mockup_debugfs_new_device_write,
+	.llseek = no_llseek,
+	.release = single_release,
+};
+
 static ssize_t gpio_mockup_debugfs_delete_device_write(struct file *file,
 						const char __user *usr_buf,
 						size_t size, loff_t *ppos)
@@ -726,6 +926,13 @@ static int __init gpio_mockup_debugfs_init(void)
 	if (IS_ERR(gpio_mockup_dbg_dir))
 		return PTR_ERR(gpio_mockup_dbg_dir);
 
+	entry = debugfs_create_file("new_device", 0200, gpio_mockup_dbg_dir,
+				NULL, &gpio_mockup_debugfs_new_device_ops);
+	if (IS_ERR(entry)) {
+		debugfs_remove_recursive(gpio_mockup_dbg_dir);
+		return PTR_ERR(entry);
+	}
+
 	entry = debugfs_create_file("delete_device", 0200, gpio_mockup_dbg_dir,
 				NULL, &gpio_mockup_debugfs_delete_device_ops);
 	if (IS_ERR(entry)) {
@@ -751,12 +958,14 @@ static int __init gpio_mockup_init(void)
 		return ret;
 	}
 
-	ret = gpio_mockup_register_chips_from_params();
-	if (ret) {
-		pr_err("error registering device");
-		debugfs_remove_recursive(gpio_mockup_dbg_dir);
-		platform_driver_unregister(&gpio_mockup_driver);
-		return ret;
+	if (gpio_mockup_ranges > 0) {
+		ret = gpio_mockup_register_chips_from_params();
+		if (ret) {
+			pr_err("error registering device");
+			debugfs_remove_recursive(gpio_mockup_dbg_dir);
+			platform_driver_unregister(&gpio_mockup_driver);
+			return ret;
+		}
 	}
 
 	return 0;
Signature · 1 line
2.26.1
#50 lore raw

PATCH 20/23 gpio: mockup: add a lock for dummy device list

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We'll soon add the possibility to create chips dynamically over debugfs attributes. Since multiple threads will be able to create devices at
once: add a mutex to protect the device list.

---
 drivers/gpio/gpio-mockup.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpio/gpio-mockup.c +7 -0
index bc4609e047ef..1353239dc315 100644
@@ -517,6 +517,7 @@ struct gpio_mockup_device {
 };
 
 static LIST_HEAD(gpio_mockup_devices);
+static DEFINE_MUTEX(gpio_mockup_devices_lock);
 
 static void gpio_mockup_unregister_one_device(struct gpio_mockup_device *dev)
 {
@@ -534,10 +535,14 @@ static void gpio_mockup_unregister_devices(void)
 	struct gpio_mockup_device *mockup_dev;
 	struct list_head *curr, *next;
 
+	mutex_lock(&gpio_mockup_devices_lock);
+
 	list_for_each_safe(curr, next, &gpio_mockup_devices) {
 		mockup_dev = list_entry(curr, struct gpio_mockup_device, list);
 		gpio_mockup_unregister_one_device(mockup_dev);
 	}
+
+	mutex_unlock(&gpio_mockup_devices_lock);
 }
 
 static __init char **gpio_mockup_make_line_names(const char *label,
@@ -588,7 +593,9 @@ static int __init gpio_mockup_register_device(struct property_entry *properties)
 		return PTR_ERR(mockup_dev->pdev);
 	}
 
+	mutex_lock(&gpio_mockup_devices_lock);
 	list_add(&mockup_dev->list, &gpio_mockup_devices);
+	mutex_unlock(&gpio_mockup_devices_lock);
 
 	return 0;
 }
Signature · 1 line
2.26.1
#51 lore raw

PATCH 21/23 gpio: mockup: provide a way to delete dummy chips

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Add a new debugfs attribute 'delete_device' to which the chip label can be written to dynamically remove the associated dummy device.

---
 drivers/gpio/gpio-mockup.c | 70 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 69 insertions(+), 1 deletion(-)
drivers/gpio/gpio-mockup.c +69 -1
index 1353239dc315..9d2de78a45c2 100644
@@ -19,6 +19,7 @@
 #include <linux/platform_device.h>
 #include <linux/property.h>
 #include <linux/slab.h>
+#include <linux/string.h>
 #include <linux/string_helpers.h>
 #include <linux/uaccess.h>
 
@@ -668,14 +669,81 @@ static int __init gpio_mockup_register_chips_from_params(void)
 	return 0;
 }
 
-static int __init gpio_mockup_init(void)
+static ssize_t gpio_mockup_debugfs_delete_device_write(struct file *file,
+						const char __user *usr_buf,
+						size_t size, loff_t *ppos)
 {
+	struct gpio_mockup_device *mockup_dev;
+	char label[GPIO_MOCKUP_LABEL_SIZE];
+	struct list_head *curr;
+	struct device *dev;
+	const char *prop;
 	int ret;
 
+	if (*ppos != 0)
+		return -EINVAL;
+
+	ret = getline_from_user(label, sizeof(label), usr_buf, size);
+	if (ret < 0)
+		return ret;
+
+	mutex_lock(&gpio_mockup_devices_lock);
+
+	list_for_each(curr, &gpio_mockup_devices) {
+		mockup_dev = list_entry(curr, struct gpio_mockup_device, list);
+		dev = &mockup_dev->pdev->dev;
+
+		ret = device_property_read_string(dev, "chip-label", &prop);
+		if (ret) {
+			mutex_unlock(&gpio_mockup_devices_lock);
+			return ret;
+		}
+
+		if (sysfs_streq(label, prop)) {
+			gpio_mockup_unregister_one_device(mockup_dev);
+			mutex_unlock(&gpio_mockup_devices_lock);
+			return size;
+		}
+	}
+
+	mutex_unlock(&gpio_mockup_devices_lock);
+	return -ENODEV;
+}
+
+static const struct file_operations gpio_mockup_debugfs_delete_device_ops = {
+	.owner = THIS_MODULE,
+	.open = gpio_mockup_debugfs_open,
+	.write = gpio_mockup_debugfs_delete_device_write,
+	.llseek = no_llseek,
+	.release = single_release,
+};
+
+static int __init gpio_mockup_debugfs_init(void)
+{
+	struct dentry *entry;
+
 	gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
 	if (IS_ERR(gpio_mockup_dbg_dir))
 		return PTR_ERR(gpio_mockup_dbg_dir);
 
+	entry = debugfs_create_file("delete_device", 0200, gpio_mockup_dbg_dir,
+				NULL, &gpio_mockup_debugfs_delete_device_ops);
+	if (IS_ERR(entry)) {
+		debugfs_remove_recursive(gpio_mockup_dbg_dir);
+		return PTR_ERR(entry);
+	}
+
+	return 0;
+}
+
+static int __init gpio_mockup_init(void)
+{
+	int ret;
+
+	ret = gpio_mockup_debugfs_init();
+	if (ret)
+		return ret;
+
 	ret = platform_driver_register(&gpio_mockup_driver);
 	if (ret) {
 		pr_err("error registering platform driver\n");
Signature · 1 line
2.26.1
#47 lore raw

PATCH 17/23 gpio: mockup: rename and move around debugfs callbacks

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We will soon introduce new debugfs attributes for dynamically created chips. We'll reuse the gpio_mockup_debugfs_open() helper for them but the relevant write callbacks will of course be different.

Let's rename gpio_mockup_debugfs_write() to gpio_mockup_debugfs_pull_write() to avoid confusion with new write callbacks and move gpio_mockup_debugfs_open() higher up in the code to separate it from the pull/value attribute which will no longer be the only user.

---
 drivers/gpio/gpio-mockup.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
drivers/gpio/gpio-mockup.c +13 -13
index eb94ddac5fee..29fbf007ab26 100644
@@ -262,9 +262,14 @@ static void gpio_mockup_free(struct gpio_chip *gc, unsigned int offset)
 	__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)
+static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file)
+{
+	return single_open(file, NULL, inode->i_private);
+}
+
+static ssize_t gpio_mockup_debugfs_value_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;
@@ -287,9 +292,9 @@ static ssize_t gpio_mockup_debugfs_read(struct file *file,
 	return simple_read_from_buffer(usr_buf, size, ppos, buf, cnt);
 }
 
-static ssize_t gpio_mockup_debugfs_write(struct file *file,
-					 const char __user *usr_buf,
-					 size_t size, loff_t *ppos)
+static ssize_t gpio_mockup_debugfs_pull_write(struct file *file,
+					      const char __user *usr_buf,
+					      size_t size, loff_t *ppos)
 {
 	struct gpio_mockup_dbgfs_private *priv;
 	int rv, val;
@@ -313,11 +318,6 @@ static ssize_t gpio_mockup_debugfs_write(struct file *file,
 	return size;
 }
 
-static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file)
-{
-	return single_open(file, NULL, inode->i_private);
-}
-
 /*
  * 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
@@ -342,8 +342,8 @@ static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file)
 static const struct file_operations gpio_mockup_debugfs_ops = {
 	.owner = THIS_MODULE,
 	.open = gpio_mockup_debugfs_open,
-	.read = gpio_mockup_debugfs_read,
-	.write = gpio_mockup_debugfs_write,
+	.read = gpio_mockup_debugfs_value_read,
+	.write = gpio_mockup_debugfs_pull_write,
 	.llseek = no_llseek,
 	.release = single_release,
 };
Signature · 1 line
2.26.1
#31 lore raw

PATCH 13/23 gpio: mockup: pass the chip label as device property

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

While we do check the "chip-name" property in probe(), we never actually use it. Let's pass the chip label to the driver using device properties as we'll want to allow users to define their own once dynamically created chips are supported.

The property is renamed to "chip-label" to not cause any confusion with the actual chip name which is of the form: "gpiochipX".

If the "chip-label" property is missing, let's do what most devices in drivers/gpio/ do and use dev_name().

---
 drivers/gpio/gpio-mockup.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
drivers/gpio/gpio-mockup.c +8 -9
index e8a19a28ed13..ce83f1df1933 100644
@@ -433,21 +433,14 @@ static int gpio_mockup_probe(struct platform_device *pdev)
 	if (rv)
 		return rv;
 
-	rv = device_property_read_string(dev, "chip-name", &name);
+	rv = device_property_read_string(dev, "chip-label", &name);
 	if (rv)
-		name = NULL;
+		name = dev_name(dev);
 
 	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
 	if (!chip)
 		return -ENOMEM;
 
-	if (!name) {
-		name = devm_kasprintf(dev, GFP_KERNEL,
-				      "%s-%c", pdev->name, pdev->id + 'A');
-		if (!name)
-			return -ENOMEM;
-	}
-
 	mutex_init(&chip->lock);
 
 	gc = &chip->gc;
@@ -534,6 +527,7 @@ static void gpio_mockup_unregister_devices(void)
 static int __init gpio_mockup_init(void)
 {
 	struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
+	char chip_label[GPIO_MOCKUP_LABEL_SIZE];
 	struct gpio_mockup_device *mockup_dev;
 	int i, prop, num_chips, err = 0, base;
 	struct platform_device_info pdevinfo;
@@ -570,6 +564,11 @@ static int __init gpio_mockup_init(void)
 		memset(&pdevinfo, 0, sizeof(pdevinfo));
 		prop = 0;
 
+		snprintf(chip_label, sizeof(chip_label),
+			 "gpio-mockup-%c", i + 'A');
+		properties[prop++] = PROPERTY_ENTRY_STRING("chip-label",
+							   chip_label);
+
 		base = gpio_mockup_range_base(i);
 		if (base >= 0)
 			properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
Signature · 1 line
2.26.1
#30 lore raw

PATCH 12/23 gpio: mockup: define a constant for chip label size

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We'll be using this value in many places in this driver soon so define a constant to avoid using magic values.

---
 drivers/gpio/gpio-mockup.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpio/gpio-mockup.c +1 -0
index 801fba6496a4..e8a19a28ed13 100644
@@ -32,6 +32,7 @@
 #define GPIO_MOCKUP_MAX_RANGES	(10 * 2)
 /* Maximum of three properties + the sentinel. */
 #define GPIO_MOCKUP_MAX_PROP	4
+#define GPIO_MOCKUP_LABEL_SIZE	32
 
 /*
  * struct gpio_pin_status - structure describing a GPIO status
Signature · 1 line
2.26.1
#29 lore raw

PATCH 11/23 gpio: mockup: remove the limit on number of dummy chips

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

As a preparation for implementing dynamically created dummy GPIO chips:
drop the limit on the number of chips. Let's use a linked list to store the chip context structures.

Let's rename gpio_mockup_unregister_pdevs() to gpio_mockup_unregister_devices() as we're now handling structures in which struct platform_device is embedded instead of operating on platform devices directly.

Note: this does not remove the limit on the number of ranges passed as a module parameter. For now nothing changes in how the module works for user-space. This patch will however allow us to create a higher number of chips once we can do this dynamically.

---
 drivers/gpio/gpio-mockup.c | 59 ++++++++++++++++++++++++++------------
 1 file changed, 40 insertions(+), 19 deletions(-)
drivers/gpio/gpio-mockup.c +40 -19
index 19c092f814fd..801fba6496a4 100644
@@ -13,6 +13,7 @@
 #include <linux/irq.h>
 #include <linux/irq_sim.h>
 #include <linux/irqdomain.h>
+#include <linux/list.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
 #include <linux/property.h>
@@ -24,12 +25,11 @@
 #undef pr_fmt
 #define pr_fmt(fmt)		KBUILD_MODNAME ": " fmt
 
-#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)
+#define GPIO_MOCKUP_MAX_RANGES	(10 * 2)
 /* Maximum of three properties + the sentinel. */
 #define GPIO_MOCKUP_MAX_PROP	4
 
@@ -505,27 +505,37 @@ static struct platform_driver gpio_mockup_driver = {
 	.probe = gpio_mockup_probe,
 };
 
-static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
+struct gpio_mockup_device {
+	struct list_head list;
+	struct platform_device *pdev;
+};
 
-static void gpio_mockup_unregister_pdevs(void)
+static LIST_HEAD(gpio_mockup_devices);
+
+static void gpio_mockup_unregister_one_device(struct gpio_mockup_device *dev)
 {
-	struct platform_device *pdev;
-	int i;
+	list_del(&dev->list);
+	platform_device_unregister(dev->pdev);
+	kfree(dev);
+}
 
-	for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
-		pdev = gpio_mockup_pdevs[i];
+static void gpio_mockup_unregister_devices(void)
+{
+	struct gpio_mockup_device *mockup_dev;
+	struct list_head *curr, *next;
 
-		if (pdev)
-			platform_device_unregister(pdev);
+	list_for_each_safe(curr, next, &gpio_mockup_devices) {
+		mockup_dev = list_entry(curr, struct gpio_mockup_device, list);
+		gpio_mockup_unregister_one_device(mockup_dev);
 	}
 }
 
 static int __init gpio_mockup_init(void)
 {
 	struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
+	struct gpio_mockup_device *mockup_dev;
 	int i, prop, num_chips, err = 0, base;
 	struct platform_device_info pdevinfo;
-	struct platform_device *pdev;
 	u16 ngpio;
 
 	if ((gpio_mockup_num_ranges < 2) ||
@@ -576,26 +586,37 @@ static int __init gpio_mockup_init(void)
 		pdevinfo.id = i;
 		pdevinfo.properties = properties;
 
-		pdev = platform_device_register_full(&pdevinfo);
-		if (IS_ERR(pdev)) {
+		mockup_dev = kzalloc(sizeof(*mockup_dev), GFP_KERNEL);
+		if (!mockup_dev) {
+			err = -ENOMEM;
+			goto err_out;
+		}
+
+		mockup_dev->pdev = platform_device_register_full(&pdevinfo);
+		if (IS_ERR(mockup_dev->pdev)) {
 			pr_err("error registering device");
-			platform_driver_unregister(&gpio_mockup_driver);
-			gpio_mockup_unregister_pdevs();
-			debugfs_remove_recursive(gpio_mockup_dbg_dir);
-			return PTR_ERR(pdev);
+			kfree(mockup_dev);
+			err = PTR_ERR(mockup_dev->pdev);
+			goto err_out;
 		}
 
-		gpio_mockup_pdevs[i] = pdev;
+		list_add(&mockup_dev->list, &gpio_mockup_devices);
 	}
 
 	return 0;
+
+err_out:
+	platform_driver_unregister(&gpio_mockup_driver);
+	gpio_mockup_unregister_devices();
+	debugfs_remove_recursive(gpio_mockup_dbg_dir);
+	return err;
 }
 
 static void __exit gpio_mockup_exit(void)
 {
 	debugfs_remove_recursive(gpio_mockup_dbg_dir);
 	platform_driver_unregister(&gpio_mockup_driver);
-	gpio_mockup_unregister_pdevs();
+	gpio_mockup_unregister_devices();
 }
 
 module_init(gpio_mockup_init);
Signature · 1 line
2.26.1
#25 lore raw

PATCH 10/23 gpio: mockup: fix resource leak in error path

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

If the module init function fails after creating the debugs directory, it's never removed. Add proper cleanup calls to avoid this resource leak.

---
 drivers/gpio/gpio-mockup.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpio/gpio-mockup.c +2 -0
index 78c97f7b6893..19c092f814fd 100644
@@ -550,6 +550,7 @@ static int __init gpio_mockup_init(void)
 	err = platform_driver_register(&gpio_mockup_driver);
 	if (err) {
 		pr_err("error registering platform driver\n");
+		debugfs_remove_recursive(gpio_mockup_dbg_dir);
 		return err;
 	}
 
@@ -580,6 +581,7 @@ static int __init gpio_mockup_init(void)
 			pr_err("error registering device");
 			platform_driver_unregister(&gpio_mockup_driver);
 			gpio_mockup_unregister_pdevs();
+			debugfs_remove_recursive(gpio_mockup_dbg_dir);
 			return PTR_ERR(pdev);
 		}
 
Signature · 1 line
2.26.1
#23 lore raw

PATCH 9/23 gpio: mockup: use KBUILD_MODNAME

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Drop the definition for the driver name. Let's use KBUILD_MODNAME for the log format and use the "gpio-mockup" value directly in the only place where it's relevant: in the name of the device.

---
 drivers/gpio/gpio-mockup.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/gpio/gpio-mockup.c +3 -4
index 73cd51459c2a..78c97f7b6893 100644
@@ -22,9 +22,8 @@
 #include "gpiolib.h"
 
 #undef pr_fmt
-#define pr_fmt(fmt)		GPIO_MOCKUP_NAME ": " fmt
+#define pr_fmt(fmt)		KBUILD_MODNAME ": " fmt
 
-#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
@@ -501,7 +500,7 @@ static int gpio_mockup_probe(struct platform_device *pdev)
 
 static struct platform_driver gpio_mockup_driver = {
 	.driver = {
-		.name = GPIO_MOCKUP_NAME,
+		.name = "gpio-mockup",
 	},
 	.probe = gpio_mockup_probe,
 };
@@ -572,7 +571,7 @@ static int __init gpio_mockup_init(void)
 			properties[prop++] = PROPERTY_ENTRY_BOOL(
 						"named-gpio-lines");
 
-		pdevinfo.name = GPIO_MOCKUP_NAME;
+		pdevinfo.name = "gpio-mockup";
 		pdevinfo.id = i;
 		pdevinfo.properties = properties;
 
Signature · 1 line
2.26.1
#21 lore raw

PATCH 8/23 gpio: mockup: use pr_fmt()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We don't need a custom logging helper. Let's use the standard pr_fmt() macro which allows us to use all pr_*() routines with custom format.

---
 drivers/gpio/gpio-mockup.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/gpio/gpio-mockup.c +5 -4
index 349782cdb4d7..73cd51459c2a 100644
@@ -21,6 +21,9 @@
 
 #include "gpiolib.h"
 
+#undef pr_fmt
+#define pr_fmt(fmt)		GPIO_MOCKUP_NAME ": " fmt
+
 #define GPIO_MOCKUP_NAME	"gpio-mockup"
 #define GPIO_MOCKUP_MAX_GC	10
 /*
@@ -31,8 +34,6 @@
 /* Maximum of three properties + the sentinel. */
 #define GPIO_MOCKUP_MAX_PROP	4
 
-#define gpio_mockup_err(...)	pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__)
-
 /*
  * struct gpio_pin_status - structure describing a GPIO status
  * @dir:       Configures direction of gpio as "in" or "out"
@@ -549,7 +550,7 @@ static int __init gpio_mockup_init(void)
 
 	err = platform_driver_register(&gpio_mockup_driver);
 	if (err) {
-		gpio_mockup_err("error registering platform driver\n");
+		pr_err("error registering platform driver\n");
 		return err;
 	}
 
@@ -577,7 +578,7 @@ static int __init gpio_mockup_init(void)
 
 		pdev = platform_device_register_full(&pdevinfo);
 		if (IS_ERR(pdev)) {
-			gpio_mockup_err("error registering device");
+			pr_err("error registering device");
 			platform_driver_unregister(&gpio_mockup_driver);
 			gpio_mockup_unregister_pdevs();
 			return PTR_ERR(pdev);
Signature · 1 line
2.26.1
#4 lore raw

PATCH 3/23 lib: uaccess: provide getline_from_user()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Provide a uaccess helper that allows callers to copy a single line from user memory. This is useful for debugfs write callbacks.

---
 include/linux/uaccess.h |  3 +++
 lib/usercopy.c          | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
include/linux/uaccess.h +3 -0
index 94b285411659..5aedd8ac5c31 100644
@@ -333,6 +333,9 @@ long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
 		long count);
 long strnlen_user_nofault(const void __user *unsafe_addr, long count);
 
+ssize_t getline_from_user(char *dst, size_t dst_size,
+			  const char __user *src, size_t src_size);
+
 /**
  * get_kernel_nofault(): safely attempt to read from a location
  * @val: read into this variable
lib/usercopy.c +37 -0
index b26509f112f9..55aaaf93d847 100644
@@ -87,3 +87,40 @@ int check_zeroed_user(const void __user *from, size_t size)
 	return -EFAULT;
 }
 EXPORT_SYMBOL(check_zeroed_user);
+
+/**
+ * getline_from_user - Copy a single line from user
+ * @dst: Where to copy the line to
+ * @dst_size: Size of the destination buffer
+ * @src: Where to copy the line from
+ * @src_size: Size of the source user buffer
+ *
+ * Copies a number of characters from given user buffer into the dst buffer.
+ * The number of bytes is limited to the lesser of the sizes of both buffers.
+ * If the copied string contains a newline, its first occurrence is replaced
+ * by a NULL byte in the destination buffer. Otherwise the function ensures
+ * the copied string is NULL-terminated.
+ *
+ * Returns the number of copied bytes or a negative error number on failure.
+ */
+
+ssize_t getline_from_user(char *dst, size_t dst_size,
+			  const char __user *src, size_t src_size)
+{
+	size_t size = min_t(size_t, dst_size, src_size);
+	char *c;
+	int ret;
+
+	ret = copy_from_user(dst, src, size);
+	if (ret)
+		return -EFAULT;
+
+	dst[size - 1] = '\0';
+
+	c = strchrnul(dst, '\n');
+	if (*c)
+		*c = '\0';
+
+	return c - dst;
+}
+EXPORT_SYMBOL(getline_from_user);
Signature · 1 line
2.26.1
#0 lore raw

cover gpio: mockup: support dynamically created and removed chips

From: Bartosz Golaszewski <[email protected]>

We're about to merge w V2 user API for GPIO. In user-space we're using the gpio-mockup driver for testing but it's quite cumbersome (needs unloading and reloading to change chip configuration) and not very extensible (config is passed over module params).

This series proposes to extend the debugfs interface to support dynamic creation and removal of dummy chips, with extensible options.

First 3 patches add some lib functionality we'll use later on. Next 3 contain general gpiolib refactoring and can be picked up independently.

Next we refactor gpio-mockup and finally add the delete_device and new_device attributes. Last patch adds documentation for gpio-mockup so I'm not going into detail on how the new interface works - the doc describes it pretty well.

Bartosz Golaszewski (23):
lib: cmdline: export next_arg()
lib: string_helpers: provide kfree_strarray()
lib: uaccess: provide getline_from_user()
gpiolib: generalize devprop_gpiochip_set_names() for device properties
gpiolib: unexport devprop_gpiochip_set_names()
gpiolib: switch to simpler IDA interface
gpio: mockup: drop unneeded includes
gpio: mockup: use pr_fmt()
gpio: mockup: use KBUILD_MODNAME
gpio: mockup: fix resource leak in error path
gpio: mockup: remove the limit on number of dummy chips
gpio: mockup: define a constant for chip label size
gpio: mockup: pass the chip label as device property
gpio: mockup: use the generic 'gpio-line-names' property
gpio: mockup: use dynamic device IDs
gpio: mockup: refactor the module init function
gpio: mockup: rename and move around debugfs callbacks
gpio: mockup: require debugfs to build
gpio: mockup: add a symlink for the per-chip debugfs directory
gpio: mockup: add a lock for dummy device list
gpio: mockup: provide a way to delete dummy chips
gpio: mockup: provide a way to create new dummy chips
Documentation: gpio: add documentation for gpio-mockup

 .../admin-guide/gpio/gpio-mockup.rst          |  87 +++
 drivers/gpio/Kconfig                          |   1 +
 drivers/gpio/Makefile                         |   1 -
 drivers/gpio/gpio-mockup.c                    | 614 ++++++++++++++----
 drivers/gpio/gpiolib-acpi.c                   |   3 -
 drivers/gpio/gpiolib-devprop.c                |  63 --
 drivers/gpio/gpiolib-of.c                     |   5 -
 drivers/gpio/gpiolib.c                        |  62 +-
 include/linux/gpio/driver.h                   |   3 -
 include/linux/string_helpers.h                |   2 +
 include/linux/uaccess.h                       |   3 +
 lib/cmdline.c                                 |   1 +
 lib/string_helpers.c                          |  22 +
 lib/usercopy.c                                |  37 ++
 14 files changed, 705 insertions(+), 199 deletions(-)
 create mode 100644 Documentation/admin-guide/gpio/gpio-mockup.rst
 delete mode 100644 drivers/gpio/gpiolib-devprop.c
Signature · 1 line
2.26.1
#5 lore raw

Re: [PATCH 03/23] lib: uaccess: provide getline_from_user()

In reply to Bartosz Golaszewski
Quoted 4 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Provide a uaccess helper that allows callers to copy a single line from user memory. This is useful for debugfs write callbacks.

Doesn't mm/util.c provides us something like this? strndup_user()?

Quoted 69 lines
---
 include/linux/uaccess.h |  3 +++
 lib/usercopy.c          | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
include/linux/uaccess.h +3 -0
index 94b285411659..5aedd8ac5c31 100644
@@ -333,6 +333,9 @@ long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
 		long count);
 long strnlen_user_nofault(const void __user *unsafe_addr, long count);
 
+ssize_t getline_from_user(char *dst, size_t dst_size,
+			  const char __user *src, size_t src_size);
+
 /**
  * get_kernel_nofault(): safely attempt to read from a location
  * @val: read into this variable
lib/usercopy.c +37 -0
index b26509f112f9..55aaaf93d847 100644
@@ -87,3 +87,40 @@ int check_zeroed_user(const void __user *from, size_t size)
 	return -EFAULT;
 }
 EXPORT_SYMBOL(check_zeroed_user);
+
+/**
+ * getline_from_user - Copy a single line from user
+ * @dst: Where to copy the line to
+ * @dst_size: Size of the destination buffer
+ * @src: Where to copy the line from
+ * @src_size: Size of the source user buffer
+ *
+ * Copies a number of characters from given user buffer into the dst buffer.
+ * The number of bytes is limited to the lesser of the sizes of both buffers.
+ * If the copied string contains a newline, its first occurrence is replaced
+ * by a NULL byte in the destination buffer. Otherwise the function ensures
+ * the copied string is NULL-terminated.
+ *
+ * Returns the number of copied bytes or a negative error number on failure.
+ */
+
+ssize_t getline_from_user(char *dst, size_t dst_size,
+			  const char __user *src, size_t src_size)
+{
+	size_t size = min_t(size_t, dst_size, src_size);
+	char *c;
+	int ret;
+
+	ret = copy_from_user(dst, src, size);
+	if (ret)
+		return -EFAULT;
+
+	dst[size - 1] = '\0';
+
+	c = strchrnul(dst, '\n');
+	if (*c)
+		*c = '\0';
+
+	return c - dst;
+}
+EXPORT_SYMBOL(getline_from_user);
Signature · 1 line
2.26.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#18 lore raw

Re: [PATCH 06/23] gpiolib: switch to simpler IDA interface

In reply to Bartosz Golaszewski
Quoted 9 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We don't need to specify any ranges when allocating IDs so we can switch to ida_alloc() and ida_free() instead of the ida_simple_ counterparts.

ida_simple_get(ida, 0, 0, gfp) is equivalent to ida_alloc_range(ida, 0, UINT_MAX, gfp) which is equivalent to ida_alloc(ida, gfp). Note: IDR will never actually allocate an ID larger than INT_MAX.

Have you considered switching to XArray API?

Quoted 39 lines
---
 drivers/gpio/gpiolib.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpio/gpiolib.c +3 -3
index 15c99cf560ee..591777bc2285 100644
@@ -471,7 +471,7 @@ static void gpiodevice_release(struct device *dev)
 	struct gpio_device *gdev = dev_get_drvdata(dev);
 
 	list_del(&gdev->list);
-	ida_simple_remove(&gpio_ida, gdev->id);
+	ida_free(&gpio_ida, gdev->id);
 	kfree_const(gdev->label);
 	kfree(gdev->descs);
 	kfree(gdev);
@@ -582,7 +582,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 		gc->of_node = gdev->dev.of_node;
 #endif
 
-	gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
+	gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
 	if (gdev->id < 0) {
 		ret = gdev->id;
 		goto err_free_gdev;
@@ -753,7 +753,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 err_free_descs:
 	kfree(gdev->descs);
 err_free_ida:
-	ida_simple_remove(&gpio_ida, gdev->id);
+	ida_free(&gpio_ida, gdev->id);
 err_free_gdev:
 	/* failures here can mean systems won't boot... */
 	pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
Signature · 1 line
2.26.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#24 lore raw

Re: [PATCH 09/23] gpio: mockup: use KBUILD_MODNAME

In reply to Bartosz Golaszewski
Quoted 16 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Drop the definition for the driver name. Let's use KBUILD_MODNAME for the log format and use the "gpio-mockup" value directly in the only place where it's relevant: in the name of the device.

---
 drivers/gpio/gpio-mockup.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/gpio/gpio-mockup.c +0 -0
index 73cd51459c2a..78c97f7b6893 100644

drivers/gpio/gpio-mockup.c line 25

Quoted 5 lines
drivers/gpio/gpio-mockup.c +1 -1
@@ -22,9 +22,8 @@
 #include "gpiolib.h"
 
 #undef pr_fmt
-#define pr_fmt(fmt)		GPIO_MOCKUP_NAME ": " fmt
+#define pr_fmt(fmt)		KBUILD_MODNAME ": " fmt

This should be part of previous patch with something like "this is equivalent right now".

Quoted 25 lines
drivers/gpio/gpio-mockup.c +2 -3
-#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
@@ -501,7 +500,7 @@ static int gpio_mockup_probe(struct platform_device *pdev)
 
 static struct platform_driver gpio_mockup_driver = {
 	.driver = {
-		.name = GPIO_MOCKUP_NAME,
+		.name = "gpio-mockup",
 	},
 	.probe = gpio_mockup_probe,
 };
@@ -572,7 +571,7 @@ static int __init gpio_mockup_init(void)
 			properties[prop++] = PROPERTY_ENTRY_BOOL(
 						"named-gpio-lines");
 
-		pdevinfo.name = GPIO_MOCKUP_NAME;
+		pdevinfo.name = "gpio-mockup";
 		pdevinfo.id = i;
 		pdevinfo.properties = properties;
 
Signature · 1 line
2.26.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#15 lore raw

Re: [PATCH 05/23] gpiolib: unexport devprop_gpiochip_set_names()

In reply to Bartosz Golaszewski
Quoted 6 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Now that devprop_gpiochip_set_names() is only used in a single place inside drivers/gpio/gpiolib.c, there's no need anymore for it to be exported or to even live in its own source file. Pull this function into the core source file for gpiolib.

I have mixed feelings about this. We may simply unexport and attach object file to gpiolib.o. Would it be expected to see more functions in this file in the future?

Quoted 166 lines
---
 drivers/gpio/Makefile          |  1 -
 drivers/gpio/gpiolib-devprop.c | 64 ----------------------------------
 drivers/gpio/gpiolib.c         | 48 +++++++++++++++++++++++++
 include/linux/gpio/driver.h    |  2 --
 4 files changed, 48 insertions(+), 67 deletions(-)
 delete mode 100644 drivers/gpio/gpiolib-devprop.c
drivers/gpio/Makefile +0 -1
index 4f9abff4f2dc..639275eb4e4d 100644
@@ -6,7 +6,6 @@ ccflags-$(CONFIG_DEBUG_GPIO)	+= -DDEBUG
 obj-$(CONFIG_GPIOLIB)		+= gpiolib.o
 obj-$(CONFIG_GPIOLIB)		+= gpiolib-devres.o
 obj-$(CONFIG_GPIOLIB)		+= gpiolib-legacy.o
-obj-$(CONFIG_GPIOLIB)		+= gpiolib-devprop.o
 obj-$(CONFIG_GPIOLIB)		+= gpiolib-cdev.o
 obj-$(CONFIG_OF_GPIO)		+= gpiolib-of.o
 obj-$(CONFIG_GPIO_SYSFS)	+= gpiolib-sysfs.o
drivers/gpio/gpiolib-devprop.c deleted +0 -64
deleted file mode 100644
index a28659b4f9c9..000000000000
@@ -1,64 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-/*
- * Device property helpers for GPIO chips.
- *
- * Copyright (C) 2016, Intel Corporation
- * Author: Mika Westerberg <[email protected]>
- */
-
-#include <linux/property.h>
-#include <linux/slab.h>
-#include <linux/gpio/consumer.h>
-#include <linux/gpio/driver.h>
-#include <linux/export.h>
-
-#include "gpiolib.h"
-
-/**
- * devprop_gpiochip_set_names - Set GPIO line names using device properties
- * @chip: GPIO chip whose lines should be named, if possible
- *
- * Looks for device property "gpio-line-names" and if it exists assigns
- * GPIO line names for the chip. The memory allocated for the assigned
- * names belong to the underlying software node and should not be released
- * by the caller.
- */
-int devprop_gpiochip_set_names(struct gpio_chip *chip)
-{
-	struct gpio_device *gdev = chip->gpiodev;
-	struct device *dev = chip->parent;
-	const char **names;
-	int ret, i;
-	int count;
-
-	count = device_property_read_string_array(dev, "gpio-line-names",
-						  NULL, 0);
-	if (count < 0)
-		return 0;
-
-	if (count > gdev->ngpio) {
-		dev_warn(&gdev->dev, "gpio-line-names is length %d but should be at most length %d",
-			 count, gdev->ngpio);
-		count = gdev->ngpio;
-	}
-
-	names = kcalloc(count, sizeof(*names), GFP_KERNEL);
-	if (!names)
-		return -ENOMEM;
-
-	ret = device_property_read_string_array(dev, "gpio-line-names",
-						names, count);
-	if (ret < 0) {
-		dev_warn(&gdev->dev, "failed to read GPIO line names\n");
-		kfree(names);
-		return ret;
-	}
-
-	for (i = 0; i < count; i++)
-		gdev->descs[i].name = names[i];
-
-	kfree(names);
-
-	return 0;
-}
-EXPORT_SYMBOL_GPL(devprop_gpiochip_set_names);
drivers/gpio/gpiolib.c +48 -0
index 0d390f0ec32c..15c99cf560ee 100644
@@ -358,6 +358,54 @@ static int gpiochip_set_desc_names(struct gpio_chip *gc)
 	return 0;
 }
 
+/*
+ * devprop_gpiochip_set_names - Set GPIO line names using device properties
+ * @chip: GPIO chip whose lines should be named, if possible
+ *
+ * Looks for device property "gpio-line-names" and if it exists assigns
+ * GPIO line names for the chip. The memory allocated for the assigned
+ * names belong to the underlying software node and should not be released
+ * by the caller.
+ */
+static int devprop_gpiochip_set_names(struct gpio_chip *chip)
+{
+	struct gpio_device *gdev = chip->gpiodev;
+	struct device *dev = chip->parent;
+	const char **names;
+	int ret, i;
+	int count;
+
+	count = device_property_read_string_array(dev, "gpio-line-names",
+						  NULL, 0);
+	if (count < 0)
+		return 0;
+
+	if (count > gdev->ngpio) {
+		dev_warn(&gdev->dev, "gpio-line-names is length %d but should be at most length %d",
+			 count, gdev->ngpio);
+		count = gdev->ngpio;
+	}
+
+	names = kcalloc(count, sizeof(*names), GFP_KERNEL);
+	if (!names)
+		return -ENOMEM;
+
+	ret = device_property_read_string_array(dev, "gpio-line-names",
+						names, count);
+	if (ret < 0) {
+		dev_warn(&gdev->dev, "failed to read GPIO line names\n");
+		kfree(names);
+		return ret;
+	}
+
+	for (i = 0; i < count; i++)
+		gdev->descs[i].name = names[i];
+
+	kfree(names);
+
+	return 0;
+}
+
 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
 {
 	unsigned long *p;
include/linux/gpio/driver.h +0 -2
index 56485a040b82..4a7e295c3640 100644
@@ -756,8 +756,6 @@ struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
 					    enum gpiod_flags dflags);
 void gpiochip_free_own_desc(struct gpio_desc *desc);
 
-int devprop_gpiochip_set_names(struct gpio_chip *gc);
-
 #ifdef CONFIG_GPIOLIB
 
 /* lock/unlock as IRQ */
Signature · 1 line
2.26.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH 02/23] lib: string_helpers: provide kfree_strarray()

In reply to Bartosz Golaszewski
Quoted 37 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

There's a common pattern of dynamically allocating an array of char pointers and then also dynamically allocating each string in this array. Provide a helper for freeing such a string array with one call.

---
 include/linux/string_helpers.h |  2 ++
 lib/string_helpers.c           | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+)
include/linux/string_helpers.h +2 -0
index 86f150c2a6b6..55b25120a1c6 100644
@@ -94,4 +94,6 @@ char *kstrdup_quotable(const char *src, gfp_t gfp);
 char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
 char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
 
+void kfree_strarray(char **str_array, size_t num_str);
+
 #endif
lib/string_helpers.c +6 -0
index 963050c0283e..22505efc6aae 100644
@@ -649,3 +649,25 @@ char *kstrdup_quotable_file(struct file *file, gfp_t gfp)
 	return pathname;
 }
 EXPORT_SYMBOL_GPL(kstrdup_quotable_file);
+
+/**
+ * kfree_strarray - free a number of dynamically allocated strings contained
+ *                  in an array and the array itself
+ *
+ * @str_array: Dynamically allocated array of strings to free. If NULL - the

lib/string_helpers.c char *kstrdup_quotable_file(struct file *file, gfp_t gfp) · line 667

Quoted 10 lines
lib/string_helpers.c +9 -0
+ *             function does nothing.
+ * @num_str: Number of strings (starting from the beginning of the array) to
+ *           free.
+ */
+void kfree_strarray(char **str_array, size_t num_str)
+{
+	unsigned int i;
 
+	if (!str_array)
+		return;

Shouldn't we expect that caller will supply NULL, 0 and above check is not needed?

Quoted 9 lines
lib/string_helpers.c +5 -0
+	for (i = 0; i < num_str; i++)
+		kfree(str_array[i]);
+	kfree(str_array);
 
+}
+EXPORT_SYMBOL_GPL(kfree_strarray);
Signature · 1 line
2.26.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#22 lore raw

Re: [PATCH 08/23] gpio: mockup: use pr_fmt()

In reply to Bartosz Golaszewski
Quoted 15 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We don't need a custom logging helper. Let's use the standard pr_fmt() macro which allows us to use all pr_*() routines with custom format.

---
 drivers/gpio/gpio-mockup.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/gpio/gpio-mockup.c +0 -0
index 349782cdb4d7..73cd51459c2a 100644

drivers/gpio/gpio-mockup.c line 26

Quoted 6 lines
drivers/gpio/gpio-mockup.c +3 -0
@@ -21,6 +21,9 @@
 
 #include "gpiolib.h"
 
+#undef pr_fmt
+#define pr_fmt(fmt)		GPIO_MOCKUP_NAME ": " fmt
+

Just put definition to be first line of code before other inclusions and drop unnecessary #undef.

Quoted 33 lines
drivers/gpio/gpio-mockup.c +2 -4
 #define GPIO_MOCKUP_NAME	"gpio-mockup"
 #define GPIO_MOCKUP_MAX_GC	10
 /*
@@ -31,8 +34,6 @@
 /* Maximum of three properties + the sentinel. */
 #define GPIO_MOCKUP_MAX_PROP	4
 
-#define gpio_mockup_err(...)	pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__)
-
 /*
  * struct gpio_pin_status - structure describing a GPIO status
  * @dir:       Configures direction of gpio as "in" or "out"
@@ -549,7 +550,7 @@ static int __init gpio_mockup_init(void)
 
 	err = platform_driver_register(&gpio_mockup_driver);
 	if (err) {
-		gpio_mockup_err("error registering platform driver\n");
+		pr_err("error registering platform driver\n");
 		return err;
 	}
 
@@ -577,7 +578,7 @@ static int __init gpio_mockup_init(void)
 
 		pdev = platform_device_register_full(&pdevinfo);
 		if (IS_ERR(pdev)) {
-			gpio_mockup_err("error registering device");
+			pr_err("error registering device");
 			platform_driver_unregister(&gpio_mockup_driver);
 			gpio_mockup_unregister_pdevs();
 			return PTR_ERR(pdev);
Signature · 1 line
2.26.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#12 lore raw

Re: [PATCH 04/23] gpiolib: generalize devprop_gpiochip_set_names() for device properties

In reply to Bartosz Golaszewski
Quoted 56 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

devprop_gpiochip_set_names() is overly complicated with taking the fwnode argument (which requires using dev_fwnode() & of_fwnode_handle() in ACPI and OF GPIO code respectively). Let's just switch to using the generic device properties.

This allows us to pull the code setting line names directly into gpiochip_add_data_with_key() instead of handling it separately for ACPI and OF.

---
 drivers/gpio/gpiolib-acpi.c    |  3 ---
 drivers/gpio/gpiolib-devprop.c | 19 ++++++++++---------
 drivers/gpio/gpiolib-of.c      |  5 -----
 drivers/gpio/gpiolib.c         |  8 ++++----
 include/linux/gpio/driver.h    |  3 +--
 5 files changed, 15 insertions(+), 23 deletions(-)
drivers/gpio/gpiolib-acpi.c +0 -3
index 54ca3c18b291..834a12f3219e 100644
@@ -1221,9 +1221,6 @@ void acpi_gpiochip_add(struct gpio_chip *chip)
 		return;
 	}
 
-	if (!chip->names)
-		devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent));
-
 	acpi_gpiochip_request_regions(acpi_gpio);
 	acpi_gpiochip_scan_gpios(acpi_gpio);
 	acpi_walk_dep_device_list(handle);
drivers/gpio/gpiolib-devprop.c +3 -4
index 26741032fa9e..a28659b4f9c9 100644
@@ -17,25 +17,24 @@
 /**
  * devprop_gpiochip_set_names - Set GPIO line names using device properties
  * @chip: GPIO chip whose lines should be named, if possible
- * @fwnode: Property Node containing the gpio-line-names property
  *
  * Looks for device property "gpio-line-names" and if it exists assigns
  * GPIO line names for the chip. The memory allocated for the assigned
- * names belong to the underlying firmware node and should not be released
+ * names belong to the underlying software node and should not be released
  * by the caller.
  */
-void devprop_gpiochip_set_names(struct gpio_chip *chip,
-				const struct fwnode_handle *fwnode)
+int devprop_gpiochip_set_names(struct gpio_chip *chip)
 {
 	struct gpio_device *gdev = chip->gpiodev;
+	struct device *dev = chip->parent;

drivers/gpio/gpiolib-devprop.c line 37

Quoted 10 lines
drivers/gpio/gpiolib-devprop.c +2 -2
 	const char **names;
 	int ret, i;
 	int count;
 
-	count = fwnode_property_read_string_array(fwnode, "gpio-line-names",
+	count = device_property_read_string_array(dev, "gpio-line-names",
 						  NULL, 0);
 	if (count < 0)
-		return;
+		return 0;

Can we introduce a followup to 33ee09cd59ce ("device property: Add helpers to count items in an array") for strings?

Quoted 86 lines
drivers/gpio/gpiolib-devprop.c +5 -3
 	if (count > gdev->ngpio) {
 		dev_warn(&gdev->dev, "gpio-line-names is length %d but should be at most length %d",
@@ -45,19 +44,21 @@ void devprop_gpiochip_set_names(struct gpio_chip *chip,
 
 	names = kcalloc(count, sizeof(*names), GFP_KERNEL);
 	if (!names)
-		return;
+		return -ENOMEM;
 
-	ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
+	ret = device_property_read_string_array(dev, "gpio-line-names",
 						names, count);
 	if (ret < 0) {
 		dev_warn(&gdev->dev, "failed to read GPIO line names\n");
 		kfree(names);
-		return;
+		return ret;
 	}
 
 	for (i = 0; i < count; i++)
 		gdev->descs[i].name = names[i];
 
 	kfree(names);
+
+	return 0;
 }
 EXPORT_SYMBOL_GPL(devprop_gpiochip_set_names);
drivers/gpio/gpiolib-of.c +0 -5
index bd31dd3b6a75..2f895a2b8411 100644
@@ -1026,11 +1026,6 @@ int of_gpiochip_add(struct gpio_chip *chip)
 	if (ret)
 		return ret;
 
-	/* If the chip defines names itself, these take precedence */
-	if (!chip->names)
-		devprop_gpiochip_set_names(chip,
-					   of_fwnode_handle(chip->of_node));
-
 	of_node_get(chip->of_node);
 
 	ret = of_gpiochip_scan_gpios(chip);
drivers/gpio/gpiolib.c +4 -4
index 80137c1b3cdc..0d390f0ec32c 100644
@@ -340,9 +340,6 @@ static int gpiochip_set_desc_names(struct gpio_chip *gc)
 	struct gpio_device *gdev = gc->gpiodev;
 	int i;
 
-	if (!gc->names)
-		return 0;
-
 	/* First check all names if they are unique */
 	for (i = 0; i != gc->ngpio; ++i) {
 		struct gpio_desc *gpio;
@@ -621,7 +618,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 	INIT_LIST_HEAD(&gdev->pin_ranges);
 #endif
 
-	ret = gpiochip_set_desc_names(gc);
+	if (gc->names)
+		ret = gpiochip_set_desc_names(gc);
+	else
+		ret = devprop_gpiochip_set_names(gc);
 	if (ret)
 		goto err_remove_from_list;
 
include/linux/gpio/driver.h +1 -2
index d1cef5c2715c..56485a040b82 100644
@@ -756,8 +756,7 @@ struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
 					    enum gpiod_flags dflags);
 void gpiochip_free_own_desc(struct gpio_desc *desc);
 
-void devprop_gpiochip_set_names(struct gpio_chip *gc,
-				const struct fwnode_handle *fwnode);
+int devprop_gpiochip_set_names(struct gpio_chip *gc);
 
 #ifdef CONFIG_GPIOLIB
 
Signature · 1 line
2.26.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#36 lore raw

Re: [PATCH 14/23] gpio: mockup: use the generic 'gpio-line-names' property

In reply to Bartosz Golaszewski
Quoted 121 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

GPIO line names are currently created by the driver from the chip label. We'll want to support custom formats for line names (for instance: to name all lines the same) for user-space tests so create them in the module init function and pass them to the driver using the standard 'gpio-line-names' property.

---
 drivers/gpio/gpio-mockup.c | 70 +++++++++++++++++++++-----------------
 1 file changed, 38 insertions(+), 32 deletions(-)
drivers/gpio/gpio-mockup.c +26 -32
index ce83f1df1933..96976ba66598 100644
@@ -18,6 +18,7 @@
 #include <linux/platform_device.h>
 #include <linux/property.h>
 #include <linux/slab.h>
+#include <linux/string_helpers.h>
 #include <linux/uaccess.h>
 
 #include "gpiolib.h"
@@ -378,29 +379,6 @@ static void gpio_mockup_debugfs_setup(struct device *dev,
 	return;
 }
 
-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_kcalloc(dev, gc->ngpio, sizeof(char *), 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 void gpio_mockup_dispose_mappings(void *data)
 {
 	struct gpio_mockup_chip *chip = data;
@@ -468,12 +446,6 @@ static int gpio_mockup_probe(struct platform_device *pdev)
 	for (i = 0; i < gc->ngpio; i++)
 		chip->lines[i].dir = GPIO_LINE_DIRECTION_IN;
 
-	if (device_property_read_bool(dev, "named-gpio-lines")) {
-		rv = gpio_mockup_name_lines(dev, chip);
-		if (rv)
-			return rv;
-	}
-
 	chip->irq_sim_domain = devm_irq_domain_create_sim(dev, NULL,
 							  gc->ngpio);
 	if (IS_ERR(chip->irq_sim_domain))
@@ -524,6 +496,27 @@ static void gpio_mockup_unregister_devices(void)
 	}
 }
 
+static __init char **gpio_mockup_make_line_names(const char *label,
+						 unsigned int num_lines)
+{
+	unsigned int i;
+	char **names;
+
+	names = kcalloc(num_lines + 1, sizeof(char *), GFP_KERNEL);
+	if (!names)
+		return NULL;
+
+	for (i = 0; i < num_lines; i++) {
+		names[i] = kasprintf(GFP_KERNEL, "%s-%u", label, i);
+		if (!names[i]) {
+			kfree_strarray(names, i);
+			return NULL;
+		}
+	}
+
+	return names;
+}
+
 static int __init gpio_mockup_init(void)
 {
 	struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
@@ -531,6 +524,7 @@ static int __init gpio_mockup_init(void)
 	struct gpio_mockup_device *mockup_dev;
 	int i, prop, num_chips, err = 0, base;
 	struct platform_device_info pdevinfo;
+	char **line_names;
 	u16 ngpio;
 
 	if ((gpio_mockup_num_ranges < 2) ||
@@ -563,6 +557,7 @@ static int __init gpio_mockup_init(void)
 		memset(properties, 0, sizeof(properties));
 		memset(&pdevinfo, 0, sizeof(pdevinfo));
 		prop = 0;
+		line_names = NULL;
 
 		snprintf(chip_label, sizeof(chip_label),
 			 "gpio-mockup-%c", i + 'A');
@@ -578,9 +573,18 @@ static int __init gpio_mockup_init(void)
 				 : gpio_mockup_range_ngpio(i) - base;
 		properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
 
-		if (gpio_mockup_named_lines)
-			properties[prop++] = PROPERTY_ENTRY_BOOL(
-						"named-gpio-lines");
+		if (gpio_mockup_named_lines) {
+			line_names = gpio_mockup_make_line_names(chip_label,

drivers/gpio/gpio-mockup.c static int __init gpio_mockup_init(void) · line 587

Quoted 10 lines
drivers/gpio/gpio-mockup.c +10 -0
+								 ngpio);
+			if (!line_names) {
+				err = -ENOMEM;
+				goto err_out;
+			}
+
+			properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
+						"gpio-line-names",
+						line_names, ngpio);
+		}

Indentation here looks quite deep. Maybe introduce a helper in between where you assign properties?

Quoted 19 lines
drivers/gpio/gpio-mockup.c +2 -0
 		pdevinfo.name = "gpio-mockup";
 		pdevinfo.id = i;
@@ -588,11 +592,13 @@ static int __init gpio_mockup_init(void)
 
 		mockup_dev = kzalloc(sizeof(*mockup_dev), GFP_KERNEL);
 		if (!mockup_dev) {
+			kfree_strarray(line_names, ngpio);
 			err = -ENOMEM;
 			goto err_out;
 		}
 
 		mockup_dev->pdev = platform_device_register_full(&pdevinfo);
+		kfree_strarray(line_names, ngpio);
 		if (IS_ERR(mockup_dev->pdev)) {
 			pr_err("error registering device");
 			kfree(mockup_dev);
Signature · 1 line
2.26.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#32 lore raw

Re: [PATCH 13/23] gpio: mockup: pass the chip label as device property

In reply to Bartosz Golaszewski
Quoted 7 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

While we do check the "chip-name" property in probe(), we never actually use it. Let's pass the chip label to the driver using device properties as we'll want to allow users to define their own once dynamically created chips are supported.

Quoted 5 lines

The property is renamed to "chip-label" to not cause any confusion with the actual chip name which is of the form: "gpiochipX".

If the "chip-label" property is missing, let's do what most devices in drivers/gpio/ do and use dev_name().

Just wondering if we have a documentation in kernel how this mockup mechanism works and what kind of properties it uses.

Side note: moving to software nodes would make some advantages in future such as visibility properties and their values (not yet implemented, but there is an idea to move forward).

Quoted 56 lines
---
 drivers/gpio/gpio-mockup.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
drivers/gpio/gpio-mockup.c +8 -9
index e8a19a28ed13..ce83f1df1933 100644
@@ -433,21 +433,14 @@ static int gpio_mockup_probe(struct platform_device *pdev)
 	if (rv)
 		return rv;
 
-	rv = device_property_read_string(dev, "chip-name", &name);
+	rv = device_property_read_string(dev, "chip-label", &name);
 	if (rv)
-		name = NULL;
+		name = dev_name(dev);
 
 	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
 	if (!chip)
 		return -ENOMEM;
 
-	if (!name) {
-		name = devm_kasprintf(dev, GFP_KERNEL,
-				      "%s-%c", pdev->name, pdev->id + 'A');
-		if (!name)
-			return -ENOMEM;
-	}
-
 	mutex_init(&chip->lock);
 
 	gc = &chip->gc;
@@ -534,6 +527,7 @@ static void gpio_mockup_unregister_devices(void)
 static int __init gpio_mockup_init(void)
 {
 	struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
+	char chip_label[GPIO_MOCKUP_LABEL_SIZE];
 	struct gpio_mockup_device *mockup_dev;
 	int i, prop, num_chips, err = 0, base;
 	struct platform_device_info pdevinfo;
@@ -570,6 +564,11 @@ static int __init gpio_mockup_init(void)
 		memset(&pdevinfo, 0, sizeof(pdevinfo));
 		prop = 0;
 
+		snprintf(chip_label, sizeof(chip_label),
+			 "gpio-mockup-%c", i + 'A');
+		properties[prop++] = PROPERTY_ENTRY_STRING("chip-label",
+							   chip_label);
+
 		base = gpio_mockup_range_base(i);
 		if (base >= 0)
 			properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
Signature · 1 line
2.26.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#39 lore raw

Re: [PATCH 15/23] gpio: mockup: use dynamic device IDs

In reply to Bartosz Golaszewski
Quoted 5 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We're currently creating chips at module init time only so using a static index for dummy devices is fine. We want to support dynamically created chips however so we need to switch to dynamic device IDs.

It misses ida_destroy().

What about XArray API?

Quoted 71 lines
---
 drivers/gpio/gpio-mockup.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
drivers/gpio/gpio-mockup.c +16 -1
index 96976ba66598..995e37fef9c5 100644
@@ -9,6 +9,7 @@
 
 #include <linux/debugfs.h>
 #include <linux/gpio/driver.h>
+#include <linux/idr.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/irq_sim.h>
@@ -70,6 +71,8 @@ module_param_named(gpio_mockup_named_lines,
 
 static struct dentry *gpio_mockup_dbg_dir;
 
+static DEFINE_IDA(gpio_mockup_ida);
+
 static int gpio_mockup_range_base(unsigned int index)
 {
 	return gpio_mockup_ranges[index * 2];
@@ -480,8 +483,12 @@ static LIST_HEAD(gpio_mockup_devices);
 
 static void gpio_mockup_unregister_one_device(struct gpio_mockup_device *dev)
 {
+	int id;
+
 	list_del(&dev->list);
+	id = dev->pdev->id;
 	platform_device_unregister(dev->pdev);
+	ida_free(&gpio_mockup_ida, id);
 	kfree(dev);
 }
 
@@ -587,12 +594,19 @@ static int __init gpio_mockup_init(void)
 		}
 
 		pdevinfo.name = "gpio-mockup";
-		pdevinfo.id = i;
 		pdevinfo.properties = properties;
 
+		pdevinfo.id = ida_alloc(&gpio_mockup_ida, GFP_KERNEL);
+		if (pdevinfo.id < 0) {
+			kfree_strarray(line_names, ngpio);
+			err = pdevinfo.id;
+			goto err_out;
+		}
+
 		mockup_dev = kzalloc(sizeof(*mockup_dev), GFP_KERNEL);
 		if (!mockup_dev) {
 			kfree_strarray(line_names, ngpio);
+			ida_free(&gpio_mockup_ida, pdevinfo.id);
 			err = -ENOMEM;
 			goto err_out;
 		}
@@ -601,6 +615,7 @@ static int __init gpio_mockup_init(void)
 		kfree_strarray(line_names, ngpio);
 		if (IS_ERR(mockup_dev->pdev)) {
 			pr_err("error registering device");
+			ida_free(&gpio_mockup_ida, pdevinfo.id);
 			kfree(mockup_dev);
 			err = PTR_ERR(mockup_dev->pdev);
 			goto err_out;
Signature · 1 line
2.26.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#52 lore raw

Re: [PATCH 21/23] gpio: mockup: provide a way to delete dummy chips

In reply to Bartosz Golaszewski
Quoted 39 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Add a new debugfs attribute 'delete_device' to which the chip label can be written to dynamically remove the associated dummy device.

---
 drivers/gpio/gpio-mockup.c | 70 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 69 insertions(+), 1 deletion(-)
drivers/gpio/gpio-mockup.c +10 -1
index 1353239dc315..9d2de78a45c2 100644
@@ -19,6 +19,7 @@
 #include <linux/platform_device.h>
 #include <linux/property.h>
 #include <linux/slab.h>
+#include <linux/string.h>
 #include <linux/string_helpers.h>
 #include <linux/uaccess.h>
 
@@ -668,14 +669,81 @@ static int __init gpio_mockup_register_chips_from_params(void)
 	return 0;
 }
 
-static int __init gpio_mockup_init(void)
+static ssize_t gpio_mockup_debugfs_delete_device_write(struct file *file,
+						const char __user *usr_buf,
+						size_t size, loff_t *ppos)
 {
+	struct gpio_mockup_device *mockup_dev;
+	char label[GPIO_MOCKUP_LABEL_SIZE];
+	struct list_head *curr;
+	struct device *dev;
+	const char *prop;
 	int ret;
 
+	if (*ppos != 0)

drivers/gpio/gpio-mockup.c static int __init gpio_mockup_register_chips_from_params(void) · line 693

Quoted 10 lines
drivers/gpio/gpio-mockup.c +10 -0
+		return -EINVAL;
+
+	ret = getline_from_user(label, sizeof(label), usr_buf, size);
+	if (ret < 0)
+		return ret;
+
+	mutex_lock(&gpio_mockup_devices_lock);
+
+	list_for_each(curr, &gpio_mockup_devices) {
+		mockup_dev = list_entry(curr, struct gpio_mockup_device, list);

Why not list_for_each_entry()?

Quoted 59 lines
drivers/gpio/gpio-mockup.c +49 -0
+		dev = &mockup_dev->pdev->dev;
+
+		ret = device_property_read_string(dev, "chip-label", &prop);
+		if (ret) {
+			mutex_unlock(&gpio_mockup_devices_lock);
+			return ret;
+		}
+
+		if (sysfs_streq(label, prop)) {
+			gpio_mockup_unregister_one_device(mockup_dev);
+			mutex_unlock(&gpio_mockup_devices_lock);
+			return size;
+		}
+	}
+
+	mutex_unlock(&gpio_mockup_devices_lock);
+	return -ENODEV;
+}
+
+static const struct file_operations gpio_mockup_debugfs_delete_device_ops = {
+	.owner = THIS_MODULE,
+	.open = gpio_mockup_debugfs_open,
+	.write = gpio_mockup_debugfs_delete_device_write,
+	.llseek = no_llseek,
+	.release = single_release,
+};
+
+static int __init gpio_mockup_debugfs_init(void)
+{
+	struct dentry *entry;
+
 	gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
 	if (IS_ERR(gpio_mockup_dbg_dir))
 		return PTR_ERR(gpio_mockup_dbg_dir);
 
+	entry = debugfs_create_file("delete_device", 0200, gpio_mockup_dbg_dir,
+				NULL, &gpio_mockup_debugfs_delete_device_ops);
+	if (IS_ERR(entry)) {
+		debugfs_remove_recursive(gpio_mockup_dbg_dir);
+		return PTR_ERR(entry);
+	}
+
+	return 0;
+}
+
+static int __init gpio_mockup_init(void)
+{
+	int ret;
+
+	ret = gpio_mockup_debugfs_init();
+	if (ret)
+		return ret;
+
 	ret = platform_driver_register(&gpio_mockup_driver);
 	if (ret) {
 		pr_err("error registering platform driver\n");
Signature · 1 line
2.26.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#44 lore raw

Re: [PATCH 16/23] gpio: mockup: refactor the module init function

In reply to Bartosz Golaszewski
Quoted 8 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This is in preparation for dynamically created chips.

Let's split out the code that can be reused when creating chips at run-time. Let's also move the code preparing the device properties into a separate routine. This has the advantage of simplifying the error handling.

Almost all contents of this patch should go to proposed helper as I mentioned before. Will make this patch quite small and understandable.

Quoted 206 lines
---
 drivers/gpio/gpio-mockup.c | 165 ++++++++++++++++++++-----------------
 1 file changed, 90 insertions(+), 75 deletions(-)
drivers/gpio/gpio-mockup.c +90 -75
index 995e37fef9c5..eb94ddac5fee 100644
@@ -524,16 +524,78 @@ static __init char **gpio_mockup_make_line_names(const char *label,
 	return names;
 }
 
-static int __init gpio_mockup_init(void)
+static int __init gpio_mockup_register_device(struct property_entry *properties)
 {
-	struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
-	char chip_label[GPIO_MOCKUP_LABEL_SIZE];
 	struct gpio_mockup_device *mockup_dev;
-	int i, prop, num_chips, err = 0, base;
 	struct platform_device_info pdevinfo;
-	char **line_names;
+
+	memset(&pdevinfo, 0, sizeof(pdevinfo));
+
+	mockup_dev = kzalloc(sizeof(*mockup_dev), GFP_KERNEL);
+	if (!mockup_dev)
+		return -ENOMEM;
+
+	pdevinfo.name = "gpio-mockup";
+	pdevinfo.properties = properties;
+
+	pdevinfo.id = ida_alloc(&gpio_mockup_ida, GFP_KERNEL);
+	if (pdevinfo.id < 0) {
+		kfree(mockup_dev);
+		return pdevinfo.id;
+	}
+
+	mockup_dev->pdev = platform_device_register_full(&pdevinfo);
+	if (IS_ERR(mockup_dev->pdev)) {
+		ida_free(&gpio_mockup_ida, pdevinfo.id);
+		kfree(mockup_dev);
+		return PTR_ERR(mockup_dev->pdev);
+	}
+
+	list_add(&mockup_dev->list, &gpio_mockup_devices);
+
+	return 0;
+}
+
+static int __init gpio_mockup_register_one_chip_from_params(int idx)
+{
+	char chip_label[GPIO_MOCKUP_LABEL_SIZE], **line_names = NULL;
+	struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
+	int prop = 0, base, ret;
 	u16 ngpio;
 
+	memset(properties, 0, sizeof(properties));
+
+	snprintf(chip_label, sizeof(chip_label), "gpio-mockup-%c", idx + 'A');
+	properties[prop++] = PROPERTY_ENTRY_STRING("chip-label",
+						   chip_label);
+
+	base = gpio_mockup_range_base(idx);
+	if (base >= 0)
+		properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
+							base);
+
+	ngpio = base < 0 ? gpio_mockup_range_ngpio(idx)
+			 : gpio_mockup_range_ngpio(idx) - base;
+	properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
+
+	if (gpio_mockup_named_lines) {
+		line_names = gpio_mockup_make_line_names(chip_label, ngpio);
+		if (!line_names)
+			return -ENOMEM;
+
+		properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
+					"gpio-line-names", line_names, ngpio);
+	}
+
+	ret = gpio_mockup_register_device(properties);
+	kfree_strarray(line_names, ngpio);
+	return ret;
+}
+
+static int __init gpio_mockup_register_chips_from_params(void)
+{
+	int num_chips, i, ret;
+
 	if ((gpio_mockup_num_ranges < 2) ||
 	    (gpio_mockup_num_ranges % 2) ||
 	    (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
@@ -551,86 +613,39 @@ static int __init gpio_mockup_init(void)
 			return -EINVAL;
 	}
 
-	gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
-
-	err = platform_driver_register(&gpio_mockup_driver);
-	if (err) {
-		pr_err("error registering platform driver\n");
-		debugfs_remove_recursive(gpio_mockup_dbg_dir);
-		return err;
-	}
-
 	for (i = 0; i < num_chips; i++) {
-		memset(properties, 0, sizeof(properties));
-		memset(&pdevinfo, 0, sizeof(pdevinfo));
-		prop = 0;
-		line_names = NULL;
-
-		snprintf(chip_label, sizeof(chip_label),
-			 "gpio-mockup-%c", i + 'A');
-		properties[prop++] = PROPERTY_ENTRY_STRING("chip-label",
-							   chip_label);
-
-		base = gpio_mockup_range_base(i);
-		if (base >= 0)
-			properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
-								base);
-
-		ngpio = base < 0 ? gpio_mockup_range_ngpio(i)
-				 : gpio_mockup_range_ngpio(i) - base;
-		properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
-
-		if (gpio_mockup_named_lines) {
-			line_names = gpio_mockup_make_line_names(chip_label,
-								 ngpio);
-			if (!line_names) {
-				err = -ENOMEM;
-				goto err_out;
-			}
-
-			properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
-						"gpio-line-names",
-						line_names, ngpio);
+		ret = gpio_mockup_register_one_chip_from_params(i);
+		if (ret) {
+			gpio_mockup_unregister_devices();
+			return ret;
 		}
+	}
 
-		pdevinfo.name = "gpio-mockup";
-		pdevinfo.properties = properties;
+	return 0;
+}
 
-		pdevinfo.id = ida_alloc(&gpio_mockup_ida, GFP_KERNEL);
-		if (pdevinfo.id < 0) {
-			kfree_strarray(line_names, ngpio);
-			err = pdevinfo.id;
-			goto err_out;
-		}
+static int __init gpio_mockup_init(void)
+{
+	int ret;
 
-		mockup_dev = kzalloc(sizeof(*mockup_dev), GFP_KERNEL);
-		if (!mockup_dev) {
-			kfree_strarray(line_names, ngpio);
-			ida_free(&gpio_mockup_ida, pdevinfo.id);
-			err = -ENOMEM;
-			goto err_out;
-		}
+	gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
 
-		mockup_dev->pdev = platform_device_register_full(&pdevinfo);
-		kfree_strarray(line_names, ngpio);
-		if (IS_ERR(mockup_dev->pdev)) {
-			pr_err("error registering device");
-			ida_free(&gpio_mockup_ida, pdevinfo.id);
-			kfree(mockup_dev);
-			err = PTR_ERR(mockup_dev->pdev);
-			goto err_out;
-		}
+	ret = platform_driver_register(&gpio_mockup_driver);
+	if (ret) {
+		pr_err("error registering platform driver\n");
+		debugfs_remove_recursive(gpio_mockup_dbg_dir);
+		return ret;
+	}
 
-		list_add(&mockup_dev->list, &gpio_mockup_devices);
+	ret = gpio_mockup_register_chips_from_params();
+	if (ret) {
+		pr_err("error registering device");
+		debugfs_remove_recursive(gpio_mockup_dbg_dir);
+		platform_driver_unregister(&gpio_mockup_driver);
+		return ret;
 	}
 
 	return 0;
-
-err_out:
-	platform_driver_unregister(&gpio_mockup_driver);
-	gpio_mockup_unregister_devices();
-	debugfs_remove_recursive(gpio_mockup_dbg_dir);
-	return err;
 }
 
 static void __exit gpio_mockup_exit(void)
Signature · 1 line
2.26.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#55 lore raw

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

In reply to Bartosz Golaszewski
Quoted 5 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

There's some documentation for gpio-mockup's debugfs interface in the driver's source but it's not much. Add proper documentation for this testing module.

I always for the documentation!

Quoted 102 lines
---
 .../admin-guide/gpio/gpio-mockup.rst          | 87 +++++++++++++++++++
 1 file changed, 87 insertions(+)
 create mode 100644 Documentation/admin-guide/gpio/gpio-mockup.rst
Documentation/admin-guide/gpio/gpio-mockup.rst new file +87 -0
new file mode 100644
index 000000000000..1d452ee55f8d
@@ -0,0 +1,87 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+GPIO Testing Driver
+===================
+
+The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO
+chips for testing purposes. There are two ways of configuring the chips exposed
+by the module. The lines can be accessed using the standard GPIO character
+device interface as well as manipulated using the dedicated debugfs directory
+structure.
+
+Creating simulated chips using debugfs
+--------------------------------------
+
+When the gpio-mockup module is loaded (or builtin) it creates its own directory
+in debugfs. Assuming debugfs is mounted at /sys/kernel/debug/, the directory
+will be located at /sys/kernel/debug/gpio-mockup/. Inside this directory there
+are two attributes: new_device and delete_device.
+
+New chips can be created by writing a single line containing a number of
+options to "new_device". For example:
+
+.. code-block:: sh
+
+    $ echo "label=my-mockup num_lines=4 named_lines" > /sys/kernel/debug/gpio-mockup/new_device
+
+Supported options:
+
+    num_lines=<num_lines> - number of GPIO lines to expose
+
+    label=<label> - label of the dummy chip
+
+    named_lines - defines whether dummy lines should be named, the names are
+                  of the form X-Y where X is the chip's label and Y is the
+                  line's offset
+
+Note: only num_lines is mandatory.
+
+Chips can be dynamically removed by writing the chip's label to
+"delete_device". For example:
+
+.. code-block:: sh
+
+    echo "gpio-mockup.0" > /sys/kernel/debug/gpio-mockup/delete_device
+
+Creating simulated chips using module params
+--------------------------------------------
+
+Note: this is an older, now deprecated method kept for backward compatibility
+for user-space tools.
+
+When loading the gpio-mockup driver a number of parameters can be passed to the
+module.
+
+    gpio_mockup_ranges
+
+        This parameter takes an argument in the form of an array of integer
+        pairs. Each pair defines the base GPIO number (if any) and the number
+        of lines exposed by the chip. If the base GPIO is -1, the gpiolib
+        will assign it automatically.
+
+        Example: gpio_mockup_ranges=-1,8,-1,16,405,4
+
+        The line above creates three chips. The first one will expose 8 lines,
+        the second 16 and the third 4. The base GPIO for the third chip is set
+        to 405 while for two first chips it will be assigned automatically.
+
+    gpio_named_lines
+
+        This parameter doesn't take any arguments. It lets the driver know that
+        GPIO lines exposed by it should be named.
+
+        The name format is: gpio-mockup-X-Y where X is the letter associated
+        with the mockup chip and Y is the line offset.
+
+Manipulating simulated lines
+----------------------------
+
+Each mockup chip creates its own subdirectory in /sys/kernel/debug/gpio-mockup/.
+The directory is named after the chip's label. A symlink is also created, named
+after the chip's name, which points to the label directory.
+
+Inside each subdirectory, there's a separate attribute for each GPIO line. The
+name of the attribute represents the line's offset in the chip.
+
+Reading from a line attribute returns the current value. Writing to it (0 or 1)
+changes its pull.
Signature · 1 line
2.26.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#26 lore raw

Re: [PATCH 10/23] gpio: mockup: fix resource leak in error path

In reply to Bartosz Golaszewski
Quoted 5 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

If the module init function fails after creating the debugs directory, it's never removed. Add proper cleanup calls to avoid this resource leak.

Does it fix existing bug?

Quoted 28 lines
---
 drivers/gpio/gpio-mockup.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpio/gpio-mockup.c +2 -0
index 78c97f7b6893..19c092f814fd 100644
@@ -550,6 +550,7 @@ static int __init gpio_mockup_init(void)
 	err = platform_driver_register(&gpio_mockup_driver);
 	if (err) {
 		pr_err("error registering platform driver\n");
+		debugfs_remove_recursive(gpio_mockup_dbg_dir);
 		return err;
 	}
 
@@ -580,6 +581,7 @@ static int __init gpio_mockup_init(void)
 			pr_err("error registering device");
 			platform_driver_unregister(&gpio_mockup_driver);
 			gpio_mockup_unregister_pdevs();
+			debugfs_remove_recursive(gpio_mockup_dbg_dir);
 			return PTR_ERR(pdev);
 		}
 
Signature · 1 line
2.26.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
reviewer
#56 lore raw

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

In reply to Bartosz Golaszewski

Hi,

Quoted 18 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

There's some documentation for gpio-mockup's debugfs interface in the driver's source but it's not much. Add proper documentation for this testing module.

---
 .../admin-guide/gpio/gpio-mockup.rst          | 87 +++++++++++++++++++
 1 file changed, 87 insertions(+)
 create mode 100644 Documentation/admin-guide/gpio/gpio-mockup.rst
Documentation/admin-guide/gpio/gpio-mockup.rst new file +0 -0
new file mode 100644
index 000000000000..1d452ee55f8d

Documentation/admin-guide/gpio/gpio-mockup.rst line 10

Quoted 10 lines
Documentation/admin-guide/gpio/gpio-mockup.rst new file +10 -0
@@ -0,0 +1,87 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+GPIO Testing Driver
+===================
+
+The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO
+chips for testing purposes. There are two ways of configuring the chips exposed
+by the module. The lines can be accessed using the standard GPIO character
+device interface as well as manipulated using the dedicated debugfs directory
+structure.

Could configfs be used for this instead of debugfs? debugfs is ad hoc.

Quoted 54 lines
Documentation/admin-guide/gpio/gpio-mockup.rst new file +54 -0
+
+Creating simulated chips using debugfs
+--------------------------------------
+
+When the gpio-mockup module is loaded (or builtin) it creates its own directory
+in debugfs. Assuming debugfs is mounted at /sys/kernel/debug/, the directory
+will be located at /sys/kernel/debug/gpio-mockup/. Inside this directory there
+are two attributes: new_device and delete_device.
+
+New chips can be created by writing a single line containing a number of
+options to "new_device". For example:
+
+.. code-block:: sh
+
+    $ echo "label=my-mockup num_lines=4 named_lines" > /sys/kernel/debug/gpio-mockup/new_device
+
+Supported options:
+
+    num_lines=<num_lines> - number of GPIO lines to expose
+
+    label=<label> - label of the dummy chip
+
+    named_lines - defines whether dummy lines should be named, the names are
+                  of the form X-Y where X is the chip's label and Y is the
+                  line's offset
+
+Note: only num_lines is mandatory.
+
+Chips can be dynamically removed by writing the chip's label to
+"delete_device". For example:
+
+.. code-block:: sh
+
+    echo "gpio-mockup.0" > /sys/kernel/debug/gpio-mockup/delete_device
+
+Creating simulated chips using module params
+--------------------------------------------
+
+Note: this is an older, now deprecated method kept for backward compatibility
+for user-space tools.
+
+When loading the gpio-mockup driver a number of parameters can be passed to the
+module.
+
+    gpio_mockup_ranges
+
+        This parameter takes an argument in the form of an array of integer
+        pairs. Each pair defines the base GPIO number (if any) and the number
+        of lines exposed by the chip. If the base GPIO is -1, the gpiolib
+        will assign it automatically.
+
+        Example: gpio_mockup_ranges=-1,8,-1,16,405,4
+
+        The line above creates three chips. The first one will expose 8 lines,

Documentation/admin-guide/gpio/gpio-mockup.rst line 74

Quoted 10 lines
Documentation/admin-guide/gpio/gpio-mockup.rst new file +10 -0
+        the second 16 and the third 4. The base GPIO for the third chip is set
+        to 405 while for two first chips it will be assigned automatically.
+
+    gpio_named_lines
+
+        This parameter doesn't take any arguments. It lets the driver know that
+        GPIO lines exposed by it should be named.
+
+        The name format is: gpio-mockup-X-Y where X is the letter associated
+        with the mockup chip and Y is the line offset.

Where does this 'X' letter associated with the mockup chip come from?

Documentation/admin-guide/gpio/gpio-mockup.rst line 87

Quoted 13 lines
Documentation/admin-guide/gpio/gpio-mockup.rst new file +13 -0
+
+Manipulating simulated lines
+----------------------------
+
+Each mockup chip creates its own subdirectory in /sys/kernel/debug/gpio-mockup/.
+The directory is named after the chip's label. A symlink is also created, named
+after the chip's name, which points to the label directory.
+
+Inside each subdirectory, there's a separate attribute for each GPIO line. The
+name of the attribute represents the line's offset in the chip.
+
+Reading from a line attribute returns the current value. Writing to it (0 or 1)
+changes its pull.

What does "pull" mean here?

thanks.

Signature · 1 line
~Randy
#57 lore raw

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

Quoted 1 line · Randy Dunlap

On 9/4/20 8:45 AM, Bartosz Golaszewski wrote:

...

Quoted 11 lines

Documentation/admin-guide/gpio/gpio-mockup.rst line 10

Quoted 8 lines
Documentation/admin-guide/gpio/gpio-mockup.rst new file +8 -0
+GPIO Testing Driver
+===================
+
+The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO
+chips for testing purposes. There are two ways of configuring the chips exposed
+by the module. The lines can be accessed using the standard GPIO character
+device interface as well as manipulated using the dedicated debugfs directory
+structure.

Could configfs be used for this instead of debugfs? debugfs is ad hoc.

Actually sounds like a good idea.

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

Re: [PATCH 03/23] lib: uaccess: provide getline_from_user()

In reply to Andy Shevchenko
Quoted 10 lines · Andy Shevchenko
Quoted 4 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Provide a uaccess helper that allows callers to copy a single line from user memory. This is useful for debugfs write callbacks.

Doesn't mm/util.c provides us something like this? strndup_user()?

Yes, there's both strndup_user() as well as strncpy_from_user(). The problem is that they rely on the strings being NULL-terminated. This is not guaranteed for debugfs file_operations write callbacks. We need some helper that takes the minimum of bytes provided by userspace and the buffer size and figure out how many bytes to actually copy IMO.

Bart

#7 lore raw

Re: [PATCH 03/23] lib: uaccess: provide getline_from_user()

Quoted 9 lines · Bartosz Golaszewski
Quoted 6 lines · Andy Shevchenko
Quoted 1 line · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Doesn't mm/util.c provides us something like this? strndup_user()?

Quoted 5 lines

Yes, there's both strndup_user() as well as strncpy_from_user(). The problem is that they rely on the strings being NULL-terminated. This is not guaranteed for debugfs file_operations write callbacks. We need some helper that takes the minimum of bytes provided by userspace and the buffer size and figure out how many bytes to actually copy IMO.

Wouldn't this [1] approach work?

[1]: https://elixir.bootlin.com/linux/v5.9-rc3/source/arch/x86/kernel/cpu/mtrr/if.c#L93

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

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

In reply to Andy Shevchenko
Quoted 18 lines · Andy Shevchenko
Quoted 1 line · Randy Dunlap

On 9/4/20 8:45 AM, Bartosz Golaszewski wrote:

...

Quoted 11 lines

Documentation/admin-guide/gpio/gpio-mockup.rst line 10

Quoted 8 lines
Documentation/admin-guide/gpio/gpio-mockup.rst new file +8 -0
+GPIO Testing Driver
+===================
+
+The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO
+chips for testing purposes. There are two ways of configuring the chips exposed
+by the module. The lines can be accessed using the standard GPIO character
+device interface as well as manipulated using the dedicated debugfs directory
+structure.

Could configfs be used for this instead of debugfs? debugfs is ad hoc.

Quoted 2 lines

Actually sounds like a good idea.

Well, then we can go on and write an entirely new mockup driver (ditching module params and dropping any backwards compatibility) because we're already using debugfs for line values.

How would we pass the device properties to configfs created GPIO chips anyway? Devices seem to only be created using mkdir. Am I missing something?

Bart

#8 lore raw

Re: [PATCH 03/23] lib: uaccess: provide getline_from_user()

In reply to Andy Shevchenko
Quoted 18 lines · Andy Shevchenko
Quoted 9 lines · Bartosz Golaszewski
Quoted 6 lines · Andy Shevchenko
Quoted 1 line · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Doesn't mm/util.c provides us something like this? strndup_user()?

Quoted 5 lines

Yes, there's both strndup_user() as well as strncpy_from_user(). The problem is that they rely on the strings being NULL-terminated. This is not guaranteed for debugfs file_operations write callbacks. We need some helper that takes the minimum of bytes provided by userspace and the buffer size and figure out how many bytes to actually copy IMO.

Quoted 4 lines

Wouldn't this [1] approach work?

[1]: https://elixir.bootlin.com/linux/v5.9-rc3/source/arch/x86/kernel/cpu/mtrr/if.c#L93

Sure, but this is pretty much what I do in getline_from_user(). If anything we should port mtrr_write() to using getline_from_user() once it's available upstream, no?

Bart

#73 lore raw

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

In reply to Randy Dunlap
Quoted 103 lines · Randy Dunlap

Hi,

Quoted 18 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

There's some documentation for gpio-mockup's debugfs interface in the driver's source but it's not much. Add proper documentation for this testing module.

---
 .../admin-guide/gpio/gpio-mockup.rst          | 87 +++++++++++++++++++
 1 file changed, 87 insertions(+)
 create mode 100644 Documentation/admin-guide/gpio/gpio-mockup.rst
Documentation/admin-guide/gpio/gpio-mockup.rst new file +0 -0
new file mode 100644
index 000000000000..1d452ee55f8d

Documentation/admin-guide/gpio/gpio-mockup.rst line 10

Quoted 10 lines
Documentation/admin-guide/gpio/gpio-mockup.rst new file +10 -0
@@ -0,0 +1,87 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+GPIO Testing Driver
+===================
+
+The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO
+chips for testing purposes. There are two ways of configuring the chips exposed
+by the module. The lines can be accessed using the standard GPIO character
+device interface as well as manipulated using the dedicated debugfs directory
+structure.

Could configfs be used for this instead of debugfs? debugfs is ad hoc.

Quoted 54 lines
Documentation/admin-guide/gpio/gpio-mockup.rst new file +54 -0
+
+Creating simulated chips using debugfs
+--------------------------------------
+
+When the gpio-mockup module is loaded (or builtin) it creates its own directory
+in debugfs. Assuming debugfs is mounted at /sys/kernel/debug/, the directory
+will be located at /sys/kernel/debug/gpio-mockup/. Inside this directory there
+are two attributes: new_device and delete_device.
+
+New chips can be created by writing a single line containing a number of
+options to "new_device". For example:
+
+.. code-block:: sh
+
+    $ echo "label=my-mockup num_lines=4 named_lines" > /sys/kernel/debug/gpio-mockup/new_device
+
+Supported options:
+
+    num_lines=<num_lines> - number of GPIO lines to expose
+
+    label=<label> - label of the dummy chip
+
+    named_lines - defines whether dummy lines should be named, the names are
+                  of the form X-Y where X is the chip's label and Y is the
+                  line's offset
+
+Note: only num_lines is mandatory.
+
+Chips can be dynamically removed by writing the chip's label to
+"delete_device". For example:
+
+.. code-block:: sh
+
+    echo "gpio-mockup.0" > /sys/kernel/debug/gpio-mockup/delete_device
+
+Creating simulated chips using module params
+--------------------------------------------
+
+Note: this is an older, now deprecated method kept for backward compatibility
+for user-space tools.
+
+When loading the gpio-mockup driver a number of parameters can be passed to the
+module.
+
+    gpio_mockup_ranges
+
+        This parameter takes an argument in the form of an array of integer
+        pairs. Each pair defines the base GPIO number (if any) and the number
+        of lines exposed by the chip. If the base GPIO is -1, the gpiolib
+        will assign it automatically.
+
+        Example: gpio_mockup_ranges=-1,8,-1,16,405,4
+
+        The line above creates three chips. The first one will expose 8 lines,

Documentation/admin-guide/gpio/gpio-mockup.rst line 74

Quoted 10 lines
Documentation/admin-guide/gpio/gpio-mockup.rst new file +10 -0
+        the second 16 and the third 4. The base GPIO for the third chip is set
+        to 405 while for two first chips it will be assigned automatically.
+
+    gpio_named_lines
+
+        This parameter doesn't take any arguments. It lets the driver know that
+        GPIO lines exposed by it should be named.
+
+        The name format is: gpio-mockup-X-Y where X is the letter associated
+        with the mockup chip and Y is the line offset.

Where does this 'X' letter associated with the mockup chip come from?

Quoted 16 lines

Documentation/admin-guide/gpio/gpio-mockup.rst line 87

Quoted 13 lines
Documentation/admin-guide/gpio/gpio-mockup.rst new file +13 -0
+
+Manipulating simulated lines
+----------------------------
+
+Each mockup chip creates its own subdirectory in /sys/kernel/debug/gpio-mockup/.
+The directory is named after the chip's label. A symlink is also created, named
+after the chip's name, which points to the label directory.
+
+Inside each subdirectory, there's a separate attribute for each GPIO line. The
+name of the attribute represents the line's offset in the chip.
+
+Reading from a line attribute returns the current value. Writing to it (0 or 1)
+changes its pull.

What does "pull" mean here?

Yeah I should probably clarify this. "Pull" here means a simulated pull-down/up resistor basically.

Bart

#19 lore raw

Re: [PATCH 06/23] gpiolib: switch to simpler IDA interface

In reply to Andy Shevchenko
Quoted 7 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We don't need to specify any ranges when allocating IDs so we can switch to ida_alloc() and ida_free() instead of the ida_simple_ counterparts.

Quoted 7 lines
Quoted 4 lines

ida_simple_get(ida, 0, 0, gfp) is equivalent to ida_alloc_range(ida, 0, UINT_MAX, gfp) which is equivalent to ida_alloc(ida, gfp). Note: IDR will never actually allocate an ID larger than INT_MAX.

Have you considered switching to XArray API?

IDA uses an xarray internally but wraps it in a more straightforward API. No need to use the low-level API IMO.

Bart

#16 lore raw

Re: [PATCH 05/23] gpiolib: unexport devprop_gpiochip_set_names()

In reply to Andy Shevchenko
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Quoted 9 lines
Quoted 4 lines

Now that devprop_gpiochip_set_names() is only used in a single place inside drivers/gpio/gpiolib.c, there's no need anymore for it to be exported or to even live in its own source file. Pull this function into the core source file for gpiolib.

I have mixed feelings about this. We may simply unexport and attach object file to gpiolib.o. Would it be expected to see more functions in this file in the future?

This file was created for this function alone over 4 years ago and never saw any new routines. Personally I think there's no reason to keep a single helper in its own file. We usually only split big chunks of code out of gpiolib.c.

Bart

#13 lore raw

Re: [PATCH 04/23] gpiolib: generalize devprop_gpiochip_set_names() for device properties

In reply to Andy Shevchenko
Quoted 62 lines · Andy Shevchenko
Quoted 60 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

devprop_gpiochip_set_names() is overly complicated with taking the fwnode argument (which requires using dev_fwnode() & of_fwnode_handle() in ACPI and OF GPIO code respectively). Let's just switch to using the generic device properties.

This allows us to pull the code setting line names directly into gpiochip_add_data_with_key() instead of handling it separately for ACPI and OF.

---
 drivers/gpio/gpiolib-acpi.c    |  3 ---
 drivers/gpio/gpiolib-devprop.c | 19 ++++++++++---------
 drivers/gpio/gpiolib-of.c      |  5 -----
 drivers/gpio/gpiolib.c         |  8 ++++----
 include/linux/gpio/driver.h    |  3 +--
 5 files changed, 15 insertions(+), 23 deletions(-)
drivers/gpio/gpiolib-acpi.c +0 -3
index 54ca3c18b291..834a12f3219e 100644
@@ -1221,9 +1221,6 @@ void acpi_gpiochip_add(struct gpio_chip *chip)
              return;
      }
 
-     if (!chip->names)
-             devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent));
-
      acpi_gpiochip_request_regions(acpi_gpio);
      acpi_gpiochip_scan_gpios(acpi_gpio);
      acpi_walk_dep_device_list(handle);
drivers/gpio/gpiolib-devprop.c +3 -4
index 26741032fa9e..a28659b4f9c9 100644
@@ -17,25 +17,24 @@
 /**
  * devprop_gpiochip_set_names - Set GPIO line names using device properties
  * @chip: GPIO chip whose lines should be named, if possible
- * @fwnode: Property Node containing the gpio-line-names property
  *
  * Looks for device property "gpio-line-names" and if it exists assigns
  * GPIO line names for the chip. The memory allocated for the assigned
- * names belong to the underlying firmware node and should not be released
+ * names belong to the underlying software node and should not be released
  * by the caller.
  */
-void devprop_gpiochip_set_names(struct gpio_chip *chip,
-                             const struct fwnode_handle *fwnode)
+int devprop_gpiochip_set_names(struct gpio_chip *chip)
 {
      struct gpio_device *gdev = chip->gpiodev;
+     struct device *dev = chip->parent;
      const char **names;
      int ret, i;
      int count;
 
Quoted 10 lines

drivers/gpio/gpiolib-devprop.c line 37

Quoted 6 lines
drivers/gpio/gpiolib-devprop.c +2 -2
-     count = fwnode_property_read_string_array(fwnode, "gpio-line-names",
+     count = device_property_read_string_array(dev, "gpio-line-names",
                                                NULL, 0);
      if (count < 0)
-             return;
+             return 0;

Can we introduce a followup to 33ee09cd59ce ("device property: Add helpers to count items in an array") for strings?

Sure, I'll do this in v2.

Bart

#37 lore raw

Re: [PATCH 14/23] gpio: mockup: use the generic 'gpio-line-names' property

In reply to Andy Shevchenko
Quoted 128 lines · Andy Shevchenko
Quoted 126 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

GPIO line names are currently created by the driver from the chip label. We'll want to support custom formats for line names (for instance: to name all lines the same) for user-space tests so create them in the module init function and pass them to the driver using the standard 'gpio-line-names' property.

---
 drivers/gpio/gpio-mockup.c | 70 +++++++++++++++++++++-----------------
 1 file changed, 38 insertions(+), 32 deletions(-)
drivers/gpio/gpio-mockup.c +31 -32
index ce83f1df1933..96976ba66598 100644
@@ -18,6 +18,7 @@
 #include <linux/platform_device.h>
 #include <linux/property.h>
 #include <linux/slab.h>
+#include <linux/string_helpers.h>
 #include <linux/uaccess.h>
 
 #include "gpiolib.h"
@@ -378,29 +379,6 @@ static void gpio_mockup_debugfs_setup(struct device *dev,
      return;
 }
 
-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_kcalloc(dev, gc->ngpio, sizeof(char *), 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 void gpio_mockup_dispose_mappings(void *data)
 {
      struct gpio_mockup_chip *chip = data;
@@ -468,12 +446,6 @@ static int gpio_mockup_probe(struct platform_device *pdev)
      for (i = 0; i < gc->ngpio; i++)
              chip->lines[i].dir = GPIO_LINE_DIRECTION_IN;
 
-     if (device_property_read_bool(dev, "named-gpio-lines")) {
-             rv = gpio_mockup_name_lines(dev, chip);
-             if (rv)
-                     return rv;
-     }
-
      chip->irq_sim_domain = devm_irq_domain_create_sim(dev, NULL,
                                                        gc->ngpio);
      if (IS_ERR(chip->irq_sim_domain))
@@ -524,6 +496,27 @@ static void gpio_mockup_unregister_devices(void)
      }
 }
 
+static __init char **gpio_mockup_make_line_names(const char *label,
+                                              unsigned int num_lines)
+{
+     unsigned int i;
+     char **names;
+
+     names = kcalloc(num_lines + 1, sizeof(char *), GFP_KERNEL);
+     if (!names)
+             return NULL;
+
+     for (i = 0; i < num_lines; i++) {
+             names[i] = kasprintf(GFP_KERNEL, "%s-%u", label, i);
+             if (!names[i]) {
+                     kfree_strarray(names, i);
+                     return NULL;
+             }
+     }
+
+     return names;
+}
+
 static int __init gpio_mockup_init(void)
 {
      struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
@@ -531,6 +524,7 @@ static int __init gpio_mockup_init(void)
      struct gpio_mockup_device *mockup_dev;
      int i, prop, num_chips, err = 0, base;
      struct platform_device_info pdevinfo;
+     char **line_names;
      u16 ngpio;
 
      if ((gpio_mockup_num_ranges < 2) ||
@@ -563,6 +557,7 @@ static int __init gpio_mockup_init(void)
              memset(properties, 0, sizeof(properties));
              memset(&pdevinfo, 0, sizeof(pdevinfo));
              prop = 0;
+             line_names = NULL;
 
              snprintf(chip_label, sizeof(chip_label),
                       "gpio-mockup-%c", i + 'A');
@@ -578,9 +573,18 @@ static int __init gpio_mockup_init(void)
                               : gpio_mockup_range_ngpio(i) - base;
              properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
 
-             if (gpio_mockup_named_lines)
-                     properties[prop++] = PROPERTY_ENTRY_BOOL(
-                                             "named-gpio-lines");
+             if (gpio_mockup_named_lines) {
+                     line_names = gpio_mockup_make_line_names(chip_label,
+                                                              ngpio);
+                     if (!line_names) {
+                             err = -ENOMEM;
+                             goto err_out;
+                     }
Quoted 9 lines

drivers/gpio/gpio-mockup.c static int __init gpio_mockup_init(void) · line 587

Quoted 5 lines
drivers/gpio/gpio-mockup.c +5 -0
+
+                     properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
+                                             "gpio-line-names",
+                                             line_names, ngpio);
+             }

Indentation here looks quite deep. Maybe introduce a helper in between where you assign properties?

No need for that - this gets flattened later on in the series.

Bart

#40 lore raw

Re: [PATCH 15/23] gpio: mockup: use dynamic device IDs

In reply to Andy Shevchenko
Quoted 10 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We're currently creating chips at module init time only so using a static index for dummy devices is fine. We want to support dynamically created chips however so we need to switch to dynamic device IDs.

It misses ida_destroy().

No, we always call ida_free() for separate IDs when removing devices and we remove all devices at module exit so no need to call ida_destroy().

Quoted 2 lines

What about XArray API?

Answered that somewhere - xarray is already used internally by IDA.

Bart

#33 lore raw

Re: [PATCH 13/23] gpio: mockup: pass the chip label as device property

In reply to Andy Shevchenko
Quoted 15 lines · Andy Shevchenko
Quoted 7 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

While we do check the "chip-name" property in probe(), we never actually use it. Let's pass the chip label to the driver using device properties as we'll want to allow users to define their own once dynamically created chips are supported.

Quoted 5 lines

The property is renamed to "chip-label" to not cause any confusion with the actual chip name which is of the form: "gpiochipX".

If the "chip-label" property is missing, let's do what most devices in drivers/gpio/ do and use dev_name().

Quoted 6 lines

Just wondering if we have a documentation in kernel how this mockup mechanism works and what kind of properties it uses.

Side note: moving to software nodes would make some advantages in future such as visibility properties and their values (not yet implemented, but there is an idea to move forward).

Seems like we're implicitly using software nodes already:
fwnode_create_software_node() is called when adding device properties.

Bart

#28 lore raw

Re: [PATCH 10/23] gpio: mockup: fix resource leak in error path

In reply to Bartosz Golaszewski
Quoted 5 lines · Bartosz Golaszewski
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

If the module init function fails after creating the debugs directory, it's never removed. Add proper cleanup calls to avoid this resource leak.

Does it fix existing bug?

You mean - should it go into stable? The bug is quite unlikely but yeah, probably.

Yes. That sounds to me like Fixes: is needed.

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

Re: [PATCH 13/23] gpio: mockup: pass the chip label as device property

In reply to Bartosz Golaszewski
Quoted 19 lines · Bartosz Golaszewski
Quoted 17 lines · Andy Shevchenko
Quoted 7 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

While we do check the "chip-name" property in probe(), we never actually use it. Let's pass the chip label to the driver using device properties as we'll want to allow users to define their own once dynamically created chips are supported.

Quoted 5 lines

The property is renamed to "chip-label" to not cause any confusion with the actual chip name which is of the form: "gpiochipX".

If the "chip-label" property is missing, let's do what most devices in drivers/gpio/ do and use dev_name().

Just wondering if we have a documentation in kernel how this mockup mechanism works and what kind of properties it uses.

Quoted 6 lines
Quoted 3 lines

Side note: moving to software nodes would make some advantages in future such as visibility properties and their values (not yet implemented, but there is an idea to move forward).

Seems like we're implicitly using software nodes already:
fwnode_create_software_node() is called when adding device properties.

It's not the same APIs, though the former is called from the latter. But maybe for now it's okay.

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

Re: [PATCH 03/23] lib: uaccess: provide getline_from_user()

In reply to Bartosz Golaszewski
Quoted 20 lines · Bartosz Golaszewski
Quoted 18 lines · Andy Shevchenko
Quoted 14 lines · Bartosz Golaszewski
Quoted 6 lines · Andy Shevchenko
Quoted 1 line · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Doesn't mm/util.c provides us something like this? strndup_user()?

Yes, there's both strndup_user() as well as strncpy_from_user(). The problem is that they rely on the strings being NULL-terminated. This is not guaranteed for debugfs file_operations write callbacks. We need some helper that takes the minimum of bytes provided by userspace and the buffer size and figure out how many bytes to actually copy IMO.

Quoted 8 lines
Quoted 4 lines

Wouldn't this [1] approach work?

[1]: https://elixir.bootlin.com/linux/v5.9-rc3/source/arch/x86/kernel/cpu/mtrr/if.c#L93

Sure, but this is pretty much what I do in getline_from_user(). If anything we should port mtrr_write() to using getline_from_user() once it's available upstream, no?

But you may provide getline_from_user() as inline in the same header where strncpy_from_user() is declared. It will be like 3 LOCs?

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

Re: [PATCH 16/23] gpio: mockup: refactor the module init function

In reply to Bartosz Golaszewski
Quoted 12 lines · Bartosz Golaszewski
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This is in preparation for dynamically created chips.

Let's split out the code that can be reused when creating chips at run-time. Let's also move the code preparing the device properties into a separate routine. This has the advantage of simplifying the error handling.

Quoted 4 lines
Quoted 2 lines

Almost all contents of this patch should go to proposed helper as I mentioned before. Will make this patch quite small and understandable.

Sorry, I'm not sure what you're referring to.

I meant if you move changes done here to the patch where I complained about indentation you might have better series. Have you considered that?

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

Re: [PATCH 15/23] gpio: mockup: use dynamic device IDs

In reply to Bartosz Golaszewski
Quoted 9 lines · Bartosz Golaszewski
Quoted 7 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We're currently creating chips at module init time only so using a static index for dummy devices is fine. We want to support dynamically created chips however so we need to switch to dynamic device IDs.

Quoted 5 lines
Quoted 1 line

It misses ida_destroy().

No, we always call ida_free() for separate IDs when removing devices and we remove all devices at module exit so no need to call ida_destroy().

Perhaps couple of words about this in the commit message?

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

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

In reply to Bartosz Golaszewski
Quoted 23 lines · Bartosz Golaszewski
Quoted 18 lines · Andy Shevchenko
Quoted 1 line · Randy Dunlap

On 9/4/20 8:45 AM, Bartosz Golaszewski wrote:

...

Quoted 11 lines

Documentation/admin-guide/gpio/gpio-mockup.rst line 10

Quoted 8 lines
Documentation/admin-guide/gpio/gpio-mockup.rst new file +8 -0
+GPIO Testing Driver
+===================
+
+The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO
+chips for testing purposes. There are two ways of configuring the chips exposed
+by the module. The lines can be accessed using the standard GPIO character
+device interface as well as manipulated using the dedicated debugfs directory
+structure.

Could configfs be used for this instead of debugfs? debugfs is ad hoc.

Quoted 2 lines

Actually sounds like a good idea.

Quoted 7 lines

Well, then we can go on and write an entirely new mockup driver (ditching module params and dropping any backwards compatibility) because we're already using debugfs for line values.

How would we pass the device properties to configfs created GPIO chips anyway? Devices seem to only be created using mkdir. Am I missing something?

Same way how USB composite works, no?

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

Re: [PATCH 03/23] lib: uaccess: provide getline_from_user()

In reply to Andy Shevchenko
Quoted 27 lines · Andy Shevchenko
Quoted 25 lines · Bartosz Golaszewski
Quoted 22 lines · Andy Shevchenko
Quoted 9 lines · Bartosz Golaszewski
Quoted 6 lines · Andy Shevchenko
Quoted 1 line · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Doesn't mm/util.c provides us something like this? strndup_user()?

Quoted 5 lines

Yes, there's both strndup_user() as well as strncpy_from_user(). The problem is that they rely on the strings being NULL-terminated. This is not guaranteed for debugfs file_operations write callbacks. We need some helper that takes the minimum of bytes provided by userspace and the buffer size and figure out how many bytes to actually copy IMO.

Wouldn't this [1] approach work?

[1]: https://elixir.bootlin.com/linux/v5.9-rc3/source/arch/x86/kernel/cpu/mtrr/if.c#L93

Quoted 7 lines
Quoted 3 lines

Sure, but this is pretty much what I do in getline_from_user(). If anything we should port mtrr_write() to using getline_from_user() once it's available upstream, no?

But you may provide getline_from_user() as inline in the same header where strncpy_from_user() is declared. It will be like 3 LOCs?

May be more than that. I'll see what I can do.

Bart

#42 lore raw

Re: [PATCH 15/23] gpio: mockup: use dynamic device IDs

In reply to Andy Shevchenko
Quoted 11 lines · Andy Shevchenko
Quoted 9 lines · Bartosz Golaszewski
Quoted 7 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We're currently creating chips at module init time only so using a static index for dummy devices is fine. We want to support dynamically created chips however so we need to switch to dynamic device IDs.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

It misses ida_destroy().

No, we always call ida_free() for separate IDs when removing devices and we remove all devices at module exit so no need to call ida_destroy().

Perhaps couple of words about this in the commit message?

But ida_destroy() and ida_free() are already well documented. It's clear that we remove all devices at exit and that every device removes its ID, there's really no point in mentioning it again.

Bart

#60 lore raw

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

In reply to Andy Shevchenko
Quoted 29 lines · Andy Shevchenko
Quoted 27 lines · Bartosz Golaszewski
Quoted 18 lines · Andy Shevchenko
Quoted 1 line · Randy Dunlap

On 9/4/20 8:45 AM, Bartosz Golaszewski wrote:

...

Quoted 11 lines

Documentation/admin-guide/gpio/gpio-mockup.rst line 10

Quoted 8 lines
Documentation/admin-guide/gpio/gpio-mockup.rst new file +8 -0
+GPIO Testing Driver
+===================
+
+The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO
+chips for testing purposes. There are two ways of configuring the chips exposed
+by the module. The lines can be accessed using the standard GPIO character
+device interface as well as manipulated using the dedicated debugfs directory
+structure.

Could configfs be used for this instead of debugfs? debugfs is ad hoc.

Quoted 2 lines

Actually sounds like a good idea.

Well, then we can go on and write an entirely new mockup driver (ditching module params and dropping any backwards compatibility) because we're already using debugfs for line values.

Quoted 6 lines
Quoted 3 lines

How would we pass the device properties to configfs created GPIO chips anyway? Devices seem to only be created using mkdir. Am I missing something?

Same way how USB composite works, no?

OK, so create a new chip directory in configfs, configure it using some defined configfs attributes and then finally instantiate it from sysfs?

Makes sense and is probably the right way to go. Now the question is:
is it fine to just entirely remove the previous gpio-mockup? Should we keep some backwards compatibility? Should we introduce an entirely new module and have a transition period before removing previous gpio-mockup?

Also: this is a testing module so to me debugfs is just fine. Is configfs considered stable ABI like sysfs?

Bart

#61 lore raw

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

Quoted 42 lines · Bartosz Golaszewski
Quoted 29 lines · Andy Shevchenko
Quoted 27 lines · Bartosz Golaszewski
Quoted 18 lines · Andy Shevchenko
Quoted 1 line · Randy Dunlap

On 9/4/20 8:45 AM, Bartosz Golaszewski wrote:

...

Quoted 11 lines

Documentation/admin-guide/gpio/gpio-mockup.rst line 10

Quoted 8 lines
Documentation/admin-guide/gpio/gpio-mockup.rst new file +8 -0
+GPIO Testing Driver
+===================
+
+The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO
+chips for testing purposes. There are two ways of configuring the chips exposed
+by the module. The lines can be accessed using the standard GPIO character
+device interface as well as manipulated using the dedicated debugfs directory
+structure.

Could configfs be used for this instead of debugfs? debugfs is ad hoc.

Quoted 2 lines

Actually sounds like a good idea.

Well, then we can go on and write an entirely new mockup driver (ditching module params and dropping any backwards compatibility) because we're already using debugfs for line values.

Quoted 6 lines
Quoted 3 lines

How would we pass the device properties to configfs created GPIO chips anyway? Devices seem to only be created using mkdir. Am I missing something?

Same way how USB composite works, no?

OK, so create a new chip directory in configfs, configure it using some defined configfs attributes and then finally instantiate it from sysfs?

Quoted 8 lines

Makes sense and is probably the right way to go. Now the question is:
is it fine to just entirely remove the previous gpio-mockup? Should we keep some backwards compatibility? Should we introduce an entirely new module and have a transition period before removing previous gpio-mockup?

Also: this is a testing module so to me debugfs is just fine. Is configfs considered stable ABI like sysfs?

Yes it is. Or at least until you fix all existing users so that if you do change it, no one notices it happening :)

thanks,

greg k-h

#71 lore raw

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

In reply to Andy Shevchenko
Quoted 52 lines · Andy Shevchenko
Quoted 7 lines · Bartosz Golaszewski
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Bartosz Golaszewski
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Randy Dunlap

On 9/4/20 8:45 AM, Bartosz Golaszewski wrote:

...

Quoted 26 lines
Quoted 19 lines
Quoted 19 lines
Quoted 12 lines
Quoted 11 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+GPIO Testing Driver
+===================
+
+The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO
+chips for testing purposes. There are two ways of configuring the chips exposed
+by the module. The lines can be accessed using the standard GPIO character
+device interface as well as manipulated using the dedicated debugfs directory
+structure.

Could configfs be used for this instead of debugfs? debugfs is ad hoc.

Quoted 2 lines

Actually sounds like a good idea.

Well, then we can go on and write an entirely new mockup driver (ditching module params and dropping any backwards compatibility) because we're already using debugfs for line values.

Quoted 6 lines
Quoted 3 lines

How would we pass the device properties to configfs created GPIO chips anyway? Devices seem to only be created using mkdir. Am I missing something?

Same way how USB composite works, no?

Quoted 6 lines

OK, so create a new chip directory in configfs, configure it using some defined configfs attributes and then finally instantiate it from sysfs?

Makes sense and is probably the right way to go. Now the question is:
is it fine to just entirely remove the previous gpio-mockup?

Since, for example, I never saw device property bindings for that driver I assume that it was never considered as an ABI, so feel free to hack it in either direction.

Quoted 2 lines

Should we
keep some backwards compatibility?

Quoted 8 lines

I wouldn't probably spend time on this.

Quoted 3 lines

Should we introduce an entirely new
module and have a transition period before removing previous gpio-mockup?

Neither transition period.

I wouldn't rush this actually. gpio-mockup is used a lot by libgpiod and probably by Kent's Go library. My main goal with this series is to extend it to allow for more advanced testing like simulating spurious irqs to test the software debouncer or custom line name formats to test name lookups.

I need to think about it some more. An entirely new configfs interface would take time too.

Bart

#62 lore raw

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

In reply to Greg Kroah-Hartman
Quoted 50 lines · Greg Kroah-Hartman
Quoted 48 lines · Bartosz Golaszewski
Quoted 29 lines · Andy Shevchenko
Quoted 27 lines · Bartosz Golaszewski
Quoted 18 lines · Andy Shevchenko
Quoted 1 line · Randy Dunlap

On 9/4/20 8:45 AM, Bartosz Golaszewski wrote:

...

Quoted 11 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+GPIO Testing Driver
+===================
+
+The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO
+chips for testing purposes. There are two ways of configuring the chips exposed
+by the module. The lines can be accessed using the standard GPIO character
+device interface as well as manipulated using the dedicated debugfs directory
+structure.

Could configfs be used for this instead of debugfs? debugfs is ad hoc.

Quoted 2 lines

Actually sounds like a good idea.

Well, then we can go on and write an entirely new mockup driver (ditching module params and dropping any backwards compatibility) because we're already using debugfs for line values.

Quoted 6 lines
Quoted 3 lines

How would we pass the device properties to configfs created GPIO chips anyway? Devices seem to only be created using mkdir. Am I missing something?

Same way how USB composite works, no?

OK, so create a new chip directory in configfs, configure it using some defined configfs attributes and then finally instantiate it from sysfs?

Makes sense and is probably the right way to go. Now the question is:
is it fine to just entirely remove the previous gpio-mockup? Should we keep some backwards compatibility? Should we introduce an entirely new module and have a transition period before removing previous gpio-mockup?

Quoted 6 lines
Quoted 2 lines

Also: this is a testing module so to me debugfs is just fine. Is configfs considered stable ABI like sysfs?

Yes it is. Or at least until you fix all existing users so that if you do change it, no one notices it happening :)

Then another question is: do we really want to commit to a stable ABI for a module we only use for testing purposes and which doesn't interact with any real hardware.

Rewriting this module without any legacy cruft is tempting though. :)

Bart

#63 lore raw

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Greg Kroah-Hartman

On Mon, Sep 07, 2020 at 02:06:15PM +0200, Bartosz Golaszewski wrote:

...

Quoted 9 lines
Quoted 3 lines

Yes it is. Or at least until you fix all existing users so that if you do change it, no one notices it happening :)

Then another question is: do we really want to commit to a stable ABI for a module we only use for testing purposes and which doesn't interact with any real hardware.

Rewriting this module without any legacy cruft is tempting though. :)

Another thought spoken loudly: maybe it can be unified with GPIO aggregator code? In that case it makes sense.

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

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

Hi Andy,

Quoted 11 lines · Andy Shevchenko
Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Greg Kroah-Hartman

On Mon, Sep 07, 2020 at 02:06:15PM +0200, Bartosz Golaszewski wrote:

...

Quoted 4 lines
Quoted 3 lines

Yes it is. Or at least until you fix all existing users so that if you do change it, no one notices it happening :)

Quoted 8 lines
Quoted 5 lines

Then another question is: do we really want to commit to a stable ABI for a module we only use for testing purposes and which doesn't interact with any real hardware.

Rewriting this module without any legacy cruft is tempting though. :)

Another thought spoken loudly: maybe it can be unified with GPIO aggregator code? In that case it makes sense.

You want to aggregate GPIOs out of thin air?

From DT, that would be something like

    gpios = <&gpio1 2>, <0>, <0>, <&gpio2, 5>;

?

For writing into ".../new_device", we could agree on something like "0" means not backed by an existing GPIO?

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#66 lore raw

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

In reply to Andy Shevchenko
Quoted 12 lines · Andy Shevchenko
Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Greg Kroah-Hartman

On Mon, Sep 07, 2020 at 02:06:15PM +0200, Bartosz Golaszewski wrote:

...

Quoted 4 lines
Quoted 3 lines

Yes it is. Or at least until you fix all existing users so that if you do change it, no one notices it happening :)

Quoted 9 lines
Quoted 5 lines

Then another question is: do we really want to commit to a stable ABI for a module we only use for testing purposes and which doesn't interact with any real hardware.

Rewriting this module without any legacy cruft is tempting though. :)

Another thought spoken loudly: maybe it can be unified with GPIO aggregator code? In that case it makes sense.

Cc'ing Geert but I don't quite see how this would make sense. :)

Also one thing I'm not sure about re configfs is the interface we use to read values/set pull i.e. the line attributes in debugfs, do you think configfs allows this type of attributes?

Bart

#65 lore raw

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

In reply to Geert Uytterhoeven
Quoted 27 lines · Geert Uytterhoeven

Hi Andy,

Quoted 11 lines · Andy Shevchenko
Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Greg Kroah-Hartman

On Mon, Sep 07, 2020 at 02:06:15PM +0200, Bartosz Golaszewski wrote:

...

Quoted 4 lines
Quoted 3 lines

Yes it is. Or at least until you fix all existing users so that if you do change it, no one notices it happening :)

Quoted 8 lines
Quoted 5 lines

Then another question is: do we really want to commit to a stable ABI for a module we only use for testing purposes and which doesn't interact with any real hardware.

Rewriting this module without any legacy cruft is tempting though. :)

Another thought spoken loudly: maybe it can be unified with GPIO aggregator code? In that case it makes sense.

You want to aggregate GPIOs out of thin air?

Quoted 9 lines

From DT, that would be something like

    gpios = <&gpio1 2>, <0>, <0>, <&gpio2, 5>;

?

For writing into ".../new_device", we could agree on something like "0" means not backed by an existing GPIO?

I'm really not sure this makes any sense. Why complicate an otherwise elegant module that is gpio-aggregator with functionalities that obviously don't belong here? I want to add various parameters that would affect the way the simulated chips work - this really doesn't need to go into the aggregator.

Bart

#72 lore raw

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

In reply to Bartosz Golaszewski
Quoted 60 lines · Bartosz Golaszewski
Quoted 58 lines · Andy Shevchenko
Quoted 7 lines · Bartosz Golaszewski
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Bartosz Golaszewski
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Randy Dunlap

On 9/4/20 8:45 AM, Bartosz Golaszewski wrote:

...

Quoted 26 lines
Quoted 19 lines
Quoted 19 lines
Quoted 12 lines
Quoted 11 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+GPIO Testing Driver
+===================
+
+The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO
+chips for testing purposes. There are two ways of configuring the chips exposed
+by the module. The lines can be accessed using the standard GPIO character
+device interface as well as manipulated using the dedicated debugfs directory
+structure.

Could configfs be used for this instead of debugfs? debugfs is ad hoc.

Quoted 2 lines

Actually sounds like a good idea.

Well, then we can go on and write an entirely new mockup driver (ditching module params and dropping any backwards compatibility) because we're already using debugfs for line values.

Quoted 6 lines
Quoted 3 lines

How would we pass the device properties to configfs created GPIO chips anyway? Devices seem to only be created using mkdir. Am I missing something?

Same way how USB composite works, no?

Quoted 6 lines

OK, so create a new chip directory in configfs, configure it using some defined configfs attributes and then finally instantiate it from sysfs?

Makes sense and is probably the right way to go. Now the question is:
is it fine to just entirely remove the previous gpio-mockup?

Since, for example, I never saw device property bindings for that driver I assume that it was never considered as an ABI, so feel free to hack it in either direction.

Quoted 2 lines

Should we
keep some backwards compatibility?

I wouldn't probably spend time on this.

Quoted 3 lines

Should we introduce an entirely new
module and have a transition period before removing previous gpio-mockup?

Quoted 8 lines
Quoted 2 lines

Neither transition period.

I wouldn't rush this actually. gpio-mockup is used a lot by libgpiod and probably by Kent's Go library. My main goal with this series is to extend it to allow for more advanced testing like simulating spurious irqs to test the software debouncer or custom line name formats to test name lookups.

When I wrote above I didn't mean this should be done in a hurry. Just during one release to make all parties ready for the change.

Quoted 2 lines

I need to think about it some more. An entirely new configfs interface would take time too.

Sure!

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

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

In reply to Bartosz Golaszewski
Quoted 7 lines · Bartosz Golaszewski
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Bartosz Golaszewski
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Randy Dunlap

On 9/4/20 8:45 AM, Bartosz Golaszewski wrote:

...

Quoted 26 lines
Quoted 19 lines
Quoted 19 lines
Quoted 12 lines
Quoted 11 lines

Documentation/admin-guide/gpio/gpio-mockup.rst line 10

Quoted 8 lines
Documentation/admin-guide/gpio/gpio-mockup.rst new file +8 -0
+GPIO Testing Driver
+===================
+
+The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO
+chips for testing purposes. There are two ways of configuring the chips exposed
+by the module. The lines can be accessed using the standard GPIO character
+device interface as well as manipulated using the dedicated debugfs directory
+structure.

Could configfs be used for this instead of debugfs? debugfs is ad hoc.

Quoted 2 lines

Actually sounds like a good idea.

Well, then we can go on and write an entirely new mockup driver (ditching module params and dropping any backwards compatibility) because we're already using debugfs for line values.

Quoted 6 lines
Quoted 3 lines

How would we pass the device properties to configfs created GPIO chips anyway? Devices seem to only be created using mkdir. Am I missing something?

Same way how USB composite works, no?

Quoted 6 lines

OK, so create a new chip directory in configfs, configure it using some defined configfs attributes and then finally instantiate it from sysfs?

Makes sense and is probably the right way to go. Now the question is:
is it fine to just entirely remove the previous gpio-mockup?

Since, for example, I never saw device property bindings for that driver I assume that it was never considered as an ABI, so feel free to hack it in either direction.

Quoted 2 lines

Should we
keep some backwards compatibility?

I wouldn't probably spend time on this.

Quoted 3 lines

Should we introduce an entirely new
module and have a transition period before removing previous gpio-mockup?

Neither transition period.

Quoted 2 lines

Also: this is a testing module so to me debugfs is just fine. Is configfs considered stable ABI like sysfs?

But this one is a good question. I think ConfigFS is stricter than DebugFS, up to being an ABI. But never did myself such a thing, so would like to hear experienced developers.

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

Re: [PATCH 16/23] gpio: mockup: refactor the module init function

In reply to Andy Shevchenko
Quoted 6 lines · Andy Shevchenko
Quoted 4 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This is in preparation for dynamically created chips.

Quoted 8 lines
Quoted 4 lines

Let's split out the code that can be reused when creating chips at run-time. Let's also move the code preparing the device properties into a separate routine. This has the advantage of simplifying the error handling.

Almost all contents of this patch should go to proposed helper as I mentioned before. Will make this patch quite small and understandable.

Sorry, I'm not sure what you're referring to.

Bart

#27 lore raw

Re: [PATCH 10/23] gpio: mockup: fix resource leak in error path

In reply to Andy Shevchenko
Quoted 9 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

If the module init function fails after creating the debugs directory, it's never removed. Add proper cleanup calls to avoid this resource leak.

Does it fix existing bug?

You mean - should it go into stable? The bug is quite unlikely but yeah, probably.

Bart

#67 lore raw

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

In reply to Greg Kroah-Hartman
Quoted 50 lines · Greg Kroah-Hartman
Quoted 48 lines · Bartosz Golaszewski
Quoted 29 lines · Andy Shevchenko
Quoted 27 lines · Bartosz Golaszewski
Quoted 18 lines · Andy Shevchenko
Quoted 1 line · Randy Dunlap

On 9/4/20 8:45 AM, Bartosz Golaszewski wrote:

...

Quoted 11 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+GPIO Testing Driver
+===================
+
+The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO
+chips for testing purposes. There are two ways of configuring the chips exposed
+by the module. The lines can be accessed using the standard GPIO character
+device interface as well as manipulated using the dedicated debugfs directory
+structure.

Could configfs be used for this instead of debugfs? debugfs is ad hoc.

Quoted 2 lines

Actually sounds like a good idea.

Well, then we can go on and write an entirely new mockup driver (ditching module params and dropping any backwards compatibility) because we're already using debugfs for line values.

Quoted 6 lines
Quoted 3 lines

How would we pass the device properties to configfs created GPIO chips anyway? Devices seem to only be created using mkdir. Am I missing something?

Same way how USB composite works, no?

OK, so create a new chip directory in configfs, configure it using some defined configfs attributes and then finally instantiate it from sysfs?

Makes sense and is probably the right way to go. Now the question is:
is it fine to just entirely remove the previous gpio-mockup? Should we keep some backwards compatibility? Should we introduce an entirely new module and have a transition period before removing previous gpio-mockup?

Quoted 6 lines
Quoted 2 lines

Also: this is a testing module so to me debugfs is just fine. Is configfs considered stable ABI like sysfs?

Yes it is. Or at least until you fix all existing users so that if you do change it, no one notices it happening :)

Got it. One more question: the current debugfs interface we're using in gpio-mockup exists to allow to read current values of GPIO lines in output mode (check how the user drives dummy lines) and to set their simulated pull-up/pull-down resistors (what values the user reads in input mode).

This works like this: in /sys/kernel/debug/gpio-mockup every dummy chip creates its own directory (e.g. /sys/kernel/debug/gpio-mockup/gpiochip0) and inside this directory there's an attribute per line named after the line's offset (e.g. /sys/kernel/debug/gpio-mockup/gpiochip0/4). Writing 0 or 1 to this attribute sets the pull resistor. Reading from it yields the current value (0 or 1 as well).

This is pretty non-standard so I proposed to put it in debugfs. If we were to use configfs - is this where something like this should go? Or rather sysfs? Is it even suitable/acceptable for sysfs?

Thanks,
Bartosz

#69 lore raw

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

In reply to Greg Kroah-Hartman
Quoted 75 lines · Greg Kroah-Hartman
Quoted 73 lines · Bartosz Golaszewski
Quoted 50 lines · Greg Kroah-Hartman
Quoted 48 lines · Bartosz Golaszewski
Quoted 29 lines · Andy Shevchenko
Quoted 27 lines · Bartosz Golaszewski
Quoted 18 lines · Andy Shevchenko
Quoted 1 line · Randy Dunlap

On 9/4/20 8:45 AM, Bartosz Golaszewski wrote:

...

Quoted 11 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+GPIO Testing Driver
+===================
+
+The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO
+chips for testing purposes. There are two ways of configuring the chips exposed
+by the module. The lines can be accessed using the standard GPIO character
+device interface as well as manipulated using the dedicated debugfs directory
+structure.

Could configfs be used for this instead of debugfs? debugfs is ad hoc.

Quoted 2 lines

Actually sounds like a good idea.

Well, then we can go on and write an entirely new mockup driver (ditching module params and dropping any backwards compatibility) because we're already using debugfs for line values.

Quoted 6 lines
Quoted 3 lines

How would we pass the device properties to configfs created GPIO chips anyway? Devices seem to only be created using mkdir. Am I missing something?

Same way how USB composite works, no?

OK, so create a new chip directory in configfs, configure it using some defined configfs attributes and then finally instantiate it from sysfs?

Makes sense and is probably the right way to go. Now the question is:
is it fine to just entirely remove the previous gpio-mockup? Should we keep some backwards compatibility? Should we introduce an entirely new module and have a transition period before removing previous gpio-mockup?

Quoted 6 lines
Quoted 2 lines

Also: this is a testing module so to me debugfs is just fine. Is configfs considered stable ABI like sysfs?

Yes it is. Or at least until you fix all existing users so that if you do change it, no one notices it happening :)

Got it. One more question: the current debugfs interface we're using in gpio-mockup exists to allow to read current values of GPIO lines in output mode (check how the user drives dummy lines) and to set their simulated pull-up/pull-down resistors (what values the user reads in input mode).

This works like this: in /sys/kernel/debug/gpio-mockup every dummy chip creates its own directory (e.g. /sys/kernel/debug/gpio-mockup/gpiochip0) and inside this directory there's an attribute per line named after the line's offset (e.g. /sys/kernel/debug/gpio-mockup/gpiochip0/4). Writing 0 or 1 to this attribute sets the pull resistor. Reading from it yields the current value (0 or 1 as well).

Quoted 8 lines
Quoted 3 lines

This is pretty non-standard so I proposed to put it in debugfs. If we were to use configfs - is this where something like this should go? Or rather sysfs? Is it even suitable/acceptable for sysfs?

That sounds like it would work in sysfs just fine as-is, why don't you all want to use that? configfs is good for "set a bunch of attributes to different values and then do a 'create/go/work'" type action.

I've started looking into it. I need to first implement committable items for configfs because mockup GPIO chips need to be configured before they're instantiated. It'll be configfs to configure and instantiate each chip and a set of sysfs attributes to manipulate existing chips.

Bartosz

#68 lore raw

Re: [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup

In reply to Bartosz Golaszewski
Quoted 73 lines · Bartosz Golaszewski
Quoted 50 lines · Greg Kroah-Hartman
Quoted 48 lines · Bartosz Golaszewski
Quoted 29 lines · Andy Shevchenko
Quoted 27 lines · Bartosz Golaszewski
Quoted 18 lines · Andy Shevchenko
Quoted 1 line · Randy Dunlap

On 9/4/20 8:45 AM, Bartosz Golaszewski wrote:

...

Quoted 11 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+GPIO Testing Driver
+===================
+
+The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO
+chips for testing purposes. There are two ways of configuring the chips exposed
+by the module. The lines can be accessed using the standard GPIO character
+device interface as well as manipulated using the dedicated debugfs directory
+structure.

Could configfs be used for this instead of debugfs? debugfs is ad hoc.

Quoted 2 lines

Actually sounds like a good idea.

Well, then we can go on and write an entirely new mockup driver (ditching module params and dropping any backwards compatibility) because we're already using debugfs for line values.

Quoted 6 lines
Quoted 3 lines

How would we pass the device properties to configfs created GPIO chips anyway? Devices seem to only be created using mkdir. Am I missing something?

Same way how USB composite works, no?

OK, so create a new chip directory in configfs, configure it using some defined configfs attributes and then finally instantiate it from sysfs?

Makes sense and is probably the right way to go. Now the question is:
is it fine to just entirely remove the previous gpio-mockup? Should we keep some backwards compatibility? Should we introduce an entirely new module and have a transition period before removing previous gpio-mockup?

Quoted 6 lines
Quoted 2 lines

Also: this is a testing module so to me debugfs is just fine. Is configfs considered stable ABI like sysfs?

Yes it is. Or at least until you fix all existing users so that if you do change it, no one notices it happening :)

Got it. One more question: the current debugfs interface we're using in gpio-mockup exists to allow to read current values of GPIO lines in output mode (check how the user drives dummy lines) and to set their simulated pull-up/pull-down resistors (what values the user reads in input mode).

This works like this: in /sys/kernel/debug/gpio-mockup every dummy chip creates its own directory (e.g. /sys/kernel/debug/gpio-mockup/gpiochip0) and inside this directory there's an attribute per line named after the line's offset (e.g. /sys/kernel/debug/gpio-mockup/gpiochip0/4). Writing 0 or 1 to this attribute sets the pull resistor. Reading from it yields the current value (0 or 1 as well).

Quoted 3 lines

This is pretty non-standard so I proposed to put it in debugfs. If we were to use configfs - is this where something like this should go? Or rather sysfs? Is it even suitable/acceptable for sysfs?

That sounds like it would work in sysfs just fine as-is, why don't you all want to use that? configfs is good for "set a bunch of attributes to different values and then do a 'create/go/work'" type action.

thanks,

greg k-h