Skip to contentSkip to conversation

gpio-sim: configfs-based GPIO simulator

PATCH Superseded Bartosz Golaszewski 2 participants 28 messages Dec 3, 2021 – Dec 6, 2021

Subsystems (4)

v12 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v12 cover gpio-sim: configfs-based GPIO simulator

Another iteration of gpio-sim patches. This time the changes are quite small. I removed the ifdefs from gpiolib.c as requested by Andy. In this version gpiolib-of will also prefer fwnodes over of_nodes and - if set - will convert them to of_nodes before proceeding.

Tested both with configfs as well as device-tree.

v1 -> v2:
- add selftests for gpio-sim
- add helper programs for selftests
- update the configfs rename callback to work with the new API introduced in v5.11
- fix a missing quote in the documentation
- use !! whenever using bits operation that are required to return 0 or 1
- use provided bitmap API instead of reimplementing copy or fill operations
- fix a deadlock in gpio_sim_direction_output()
- add new read-only configfs attributes for mapping of configfs items to GPIO device names
- and address other minor issues pointed out in reviews of v1

v2 -> v3:
- use devm_bitmap_alloc() instead of the zalloc variant if we're initializing the bitmap with 1s
- drop the patch exporting device_is_bound()
- don't return -ENODEV from dev_nam and chip_name configfs attributes, return a string indicating that the device is not available yet ('n/a')
- fix indentation where it makes sense
- don't protect IDA functions which use their own locking and where it's not needed
- use kmemdup() instead of kzalloc() + memcpy()
- collected review tags
- minor coding style fixes

v3 -> v4:
- return 'none' instead of 'n/a' from dev_name and chip_name before the device is registered
- use sysfs_emit() instead of s*printf()
- drop GPIO_SIM_MAX_PROP as it's only used in an array's definition where it's fine to hardcode the value

v4 -> v5:
- drop lib patches that are already upstream
- use BIT() instead of (1UL << bit) for flags
- fix refcounting for the configfs_dirent in rename()
- drop d_move() from the rename() callback
- free memory allocated for the live and pending groups in configfs_d_iput() and not in detach_groups()
- make sure that if a group of some name is in the live directory, a new group with the same name cannot be created in the pending directory

v5 -> v6:
- go back to using (1UL << bit) instead of BIT()
- if the live group dentry doesn't exist for whatever reason at the time when mkdir() in the pending group is called (would be a BUG()), return -ENOENT instead of -EEXIST which should only be returned if given subsystem already exists in either live or pending group

v6 -> v7:
- as detailed by Andy in commit 6fda593f3082 ("gpio: mockup: Convert to use software nodes") removing device properties after the platform device is removed but before the GPIO device gets dropped can lead to a use-after-free bug - use software nodes to manually control the freeing of the properties

v7 -> v8:
- fixed some minor coding style issues as pointed out by Andy

v8 -> v9:
- dropped the patches implementing committable-items and reworked the driver to not use them
- reworked the gpio-line-names property and configuring specific lines in general
- many smaller tweaks here and there

v9 -> v10:
- make writing to 'live' wait for the probe to finish and report an error to user-space if it failed
- add the ability to hog lines from the kernel-space
- rework locking (drop separate locks for line context objects)
- rework the sysfs interface (create a separate group for each line with a constant number of attributes instead of going the other way around)

v10 -> v11:
- rework the configfs structure to represent a deeper hierarchy that gpiolib supports, namely: multiple banks per platform device

v11 -> v12:
- simplify patch 2/7 by removing any mentions of OF from gpiolib.c
- improve the documentation by adding rest markups
- add a device-tree sample to the docs
- drop some trailing whitespaces from the driver
- make gpio_sim_make_bank_swnode() static
- fix coding style in patch 6/7
- add patch 3/7 that makes the OF part of gpiolib prefer to use gpio_chip's fwnode (if set) over of_node

Bartosz Golaszewski (7):
gpiolib: provide gpiod_remove_hogs()
gpiolib: allow to specify the firmware node in struct gpio_chip
gpiolib: of: make fwnode take precedence in struct gpio_chip
gpio: sim: new testing module
selftests: gpio: provide a helper for reading chip info
selftests: gpio: add a helper for reading GPIO line names
selftests: gpio: add test cases for gpio-sim

 Documentation/admin-guide/gpio/gpio-sim.rst   |  134 ++
 drivers/gpio/Kconfig                          |    8 +
 drivers/gpio/Makefile                         |    1 +
 drivers/gpio/gpio-sim.c                       | 1594 +++++++++++++++++
 drivers/gpio/gpiolib-of.c                     |    3 +
 drivers/gpio/gpiolib.c                        |   18 +-
 include/linux/gpio/driver.h                   |    2 +
 include/linux/gpio/machine.h                  |    2 +
 tools/testing/selftests/gpio/.gitignore       |    2 +
 tools/testing/selftests/gpio/Makefile         |    4 +-
 tools/testing/selftests/gpio/config           |    1 +
 tools/testing/selftests/gpio/gpio-chip-info.c |   57 +
 tools/testing/selftests/gpio/gpio-line-name.c |   55 +
 tools/testing/selftests/gpio/gpio-sim.sh      |  396 ++++
 14 files changed, 2274 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/admin-guide/gpio/gpio-sim.rst
 create mode 100644 drivers/gpio/gpio-sim.c
 create mode 100644 tools/testing/selftests/gpio/gpio-chip-info.c
 create mode 100644 tools/testing/selftests/gpio/gpio-line-name.c
 create mode 100755 tools/testing/selftests/gpio/gpio-sim.sh
Signature · 1 line
2.25.1
#1 lore raw

PATCH v12 1/7 gpiolib: provide gpiod_remove_hogs()

Currently all users of gpiod_add_hogs() call it only once at system init so there never was any need for a mechanism allowing to remove them. Now the upcoming gpio-sim will need to tear down chips with hogged lines so provide a function that allows to remove hogs.

---
 drivers/gpio/gpiolib.c       | 11 +++++++++++
 include/linux/gpio/machine.h |  2 ++
 2 files changed, 13 insertions(+)
drivers/gpio/gpiolib.c +11 -0
index abfbf546d159..22b98a590a88 100644
@@ -3540,6 +3540,17 @@ void gpiod_add_hogs(struct gpiod_hog *hogs)
 }
 EXPORT_SYMBOL_GPL(gpiod_add_hogs);
 
+void gpiod_remove_hogs(struct gpiod_hog *hogs)
+{
+	struct gpiod_hog *hog;
+
+	mutex_lock(&gpio_machine_hogs_mutex);
+	for (hog = &hogs[0]; hog->chip_label; hog++)
+		list_del(&hog->list);
+	mutex_unlock(&gpio_machine_hogs_mutex);
+}
+EXPORT_SYMBOL_GPL(gpiod_remove_hogs);
+
 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
 {
 	const char *dev_id = dev ? dev_name(dev) : NULL;
include/linux/gpio/machine.h +2 -0
index d755e529c1e3..2647dd10b541 100644
@@ -100,6 +100,7 @@ void gpiod_add_lookup_table(struct gpiod_lookup_table *table);
 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n);
 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table);
 void gpiod_add_hogs(struct gpiod_hog *hogs);
+void gpiod_remove_hogs(struct gpiod_hog *hogs);
 #else /* ! CONFIG_GPIOLIB */
 static inline
 void gpiod_add_lookup_table(struct gpiod_lookup_table *table) {}
@@ -108,6 +109,7 @@ void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n) {}
 static inline
 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table) {}
 static inline void gpiod_add_hogs(struct gpiod_hog *hogs) {}
+static inline void gpiod_remove_hogs(struct gpiod_hog *hogs) {}
 #endif /* CONFIG_GPIOLIB */
 
 #endif /* __LINUX_GPIO_MACHINE_H */
Signature · 1 line
2.25.1
#2 lore raw

PATCH v12 2/7 gpiolib: allow to specify the firmware node in struct gpio_chip

In reply to Bartosz Golaszewski

Software nodes allow us to represent hierarchies for device components that don't have their struct device representation yet - for instance:
banks of GPIOs under a common GPIO expander. The core gpiolib core however doesn't offer any way of passing this information from the drivers.

This extends struct gpio_chip with a pointer to fwnode that can be set by the driver and used to pass device properties for child nodes.

This is similar to how we handle device-tree sub-nodes with CONFIG_OF_GPIO enabled.

---
 drivers/gpio/gpiolib.c      | 7 ++++++-
 include/linux/gpio/driver.h | 2 ++
 2 files changed, 8 insertions(+), 1 deletion(-)
drivers/gpio/gpiolib.c +6 -1
index 22b98a590a88..6af732bf4c99 100644
@@ -593,13 +593,18 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 			       struct lock_class_key *lock_key,
 			       struct lock_class_key *request_key)
 {
-	struct fwnode_handle *fwnode = gc->parent ? dev_fwnode(gc->parent) : NULL;
+	struct fwnode_handle *fwnode = NULL;
 	unsigned long	flags;
 	int		ret = 0;
 	unsigned	i;
 	int		base = gc->base;
 	struct gpio_device *gdev;
 
+	if (gc->fwnode)
+		fwnode = gc->fwnode;
+	else if (gc->parent)
+		fwnode = dev_fwnode(gc->parent);
+
 	/*
 	 * First: allocate and populate the internal stat container, and
 	 * set up the struct device.
include/linux/gpio/driver.h +2 -0
index a673a359e20b..b0728c8ad90c 100644
@@ -289,6 +289,7 @@ struct gpio_irq_chip {
  *	number or the name of the SoC IP-block implementing it.
  * @gpiodev: the internal state holder, opaque struct
  * @parent: optional parent device providing the GPIOs
+ * @fwnode: optional fwnode providing this controller's properties
  * @owner: helps prevent removal of modules exporting active GPIOs
  * @request: optional hook for chip-specific activation, such as
  *	enabling module power and clock; may sleep
@@ -377,6 +378,7 @@ struct gpio_chip {
 	const char		*label;
 	struct gpio_device	*gpiodev;
 	struct device		*parent;
+	struct fwnode_handle	*fwnode;
 	struct module		*owner;
 
 	int			(*request)(struct gpio_chip *gc,
Signature · 1 line
2.25.1
#3 lore raw

PATCH v12 3/7 gpiolib: of: make fwnode take precedence in struct gpio_chip

In reply to Bartosz Golaszewski

If the driver sets the fwnode in struct gpio_chip, let it take precedence over the of_node.

---
 drivers/gpio/gpiolib-of.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpio/gpiolib-of.c +3 -0
index 0ad288ab6262..91dcf2c6cdd8 100644
@@ -1046,6 +1046,9 @@ void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev)
 	if (gc->parent)
 		gdev->dev.of_node = gc->parent->of_node;
 
+	if (gc->fwnode)
+		gc->of_node = to_of_node(gc->fwnode);
+
 	/* If the gpiochip has an assigned OF node this takes precedence */
 	if (gc->of_node)
 		gdev->dev.of_node = gc->of_node;
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH v12 3/7] gpiolib: of: make fwnode take precedence in struct gpio_chip

Quoted 13 lines · Bartosz Golaszewski

If the driver sets the fwnode in struct gpio_chip, let it take precedence over the of_node.

---
 drivers/gpio/gpiolib-of.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpio/gpiolib-of.c +0 -0
index 0ad288ab6262..91dcf2c6cdd8 100644

drivers/gpio/gpiolib-of.c void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev) · line 1054

Quoted 9 lines
drivers/gpio/gpiolib-of.c +3 -0
@@ -1046,6 +1046,9 @@ void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev)
 	if (gc->parent)
 		gdev->dev.of_node = gc->parent->of_node;
 
+	if (gc->fwnode)
+		gc->of_node = to_of_node(gc->fwnode);
+
 	/* If the gpiochip has an assigned OF node this takes precedence */
 	if (gc->of_node)
 		gdev->dev.of_node = gc->of_node;

Similar should be done in acpi_gpio_dev_init():

	if (gc->fwnode)
		device_set_node(&gdev->dev, gc->fwnode);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH v12 3/7] gpiolib: of: make fwnode take precedence in struct gpio_chip

Quoted 1 line · Andy Shevchenko

On Fri, Dec 03, 2021 at 02:29:59PM +0100, Bartosz Golaszewski wrote:

...

Quoted 12 lines

drivers/gpio/gpiolib-of.c void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev) · line 1054

Quoted 9 lines
drivers/gpio/gpiolib-of.c +3 -0
 	if (gc->parent)
 		gdev->dev.of_node = gc->parent->of_node;
 
+	if (gc->fwnode)
+		gc->of_node = to_of_node(gc->fwnode);
+
 	/* If the gpiochip has an assigned OF node this takes precedence */
 	if (gc->of_node)
 		gdev->dev.of_node = gc->of_node;

Similar should be done in acpi_gpio_dev_init():

Quoted 2 lines
	if (gc->fwnode)
		device_set_node(&gdev->dev, gc->fwnode);

Hmm... On the second though this should be rather

	if (gc->fwnode)
		set_secondary_fwnode(&gdev->dev, gc->fwnode);

So the logic will be that:
- if we have parent, set primary fwnode to it
- if we have fwnode, set secondary one to it
- otherwise do nothing

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

Re: [PATCH v12 3/7] gpiolib: of: make fwnode take precedence in struct gpio_chip

Quoted 22 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko

On Fri, Dec 03, 2021 at 02:29:59PM +0100, Bartosz Golaszewski wrote:

...

Quoted 12 lines

drivers/gpio/gpiolib-of.c void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev) · line 1054

Quoted 9 lines
drivers/gpio/gpiolib-of.c +3 -0
 	if (gc->parent)
 		gdev->dev.of_node = gc->parent->of_node;
 
+	if (gc->fwnode)
+		gc->of_node = to_of_node(gc->fwnode);
+
 	/* If the gpiochip has an assigned OF node this takes precedence */
 	if (gc->of_node)
 		gdev->dev.of_node = gc->of_node;

Similar should be done in acpi_gpio_dev_init():

Quoted 2 lines
	if (gc->fwnode)
		device_set_node(&gdev->dev, gc->fwnode);

Hmm... On the second though this should be rather

Quoted 7 lines
	if (gc->fwnode)
		set_secondary_fwnode(&gdev->dev, gc->fwnode);

So the logic will be that:
- if we have parent, set primary fwnode to it
- if we have fwnode, set secondary one to it
- otherwise do nothing

Heck, it's Friday...

If we have parent device for several GPIO devices, this won't work right now due to limitations of fwnode regarding to the sturct device.

So, it means we may not have shared primary with different secondary fwnodes.

So, come back to the initial suggestion (overwrite it for now):

	/*
	 * If custom fwnode provided, use it. Currently we may not
	 * handle the case where shared primary node has different
	 * secondary ones. Ideally we have to use
	 * set_secondary_fwnode() here.
	 */
	if (gc->fwnode)
		device_set_node(&gdev->dev, gc->fwnode);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

Re: [PATCH v12 3/7] gpiolib: of: make fwnode take precedence in struct gpio_chip

Quoted 41 lines · Andy Shevchenko
Quoted 22 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko

On Fri, Dec 03, 2021 at 02:29:59PM +0100, Bartosz Golaszewski wrote:

...

Quoted 10 lines

drivers/gpio/gpiolib-of.c void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev) · line 1054

Quoted 9 lines
  if (gc->parent)
          gdev->dev.of_node = gc->parent->of_node;

+ if (gc->fwnode)

drivers/gpio/gpiolib-of.c +2 -0
+         gc->of_node = to_of_node(gc->fwnode);
+
  /* If the gpiochip has an assigned OF node this takes precedence */
  if (gc->of_node)
          gdev->dev.of_node = gc->of_node;
Quoted 4 lines

Similar should be done in acpi_gpio_dev_init():

    if (gc->fwnode)
            device_set_node(&gdev->dev, gc->fwnode);

Hmm... On the second though this should be rather

Quoted 7 lines
      if (gc->fwnode)
              set_secondary_fwnode(&gdev->dev, gc->fwnode);

So the logic will be that:
- if we have parent, set primary fwnode to it
- if we have fwnode, set secondary one to it
- otherwise do nothing

Heck, it's Friday...

If we have parent device for several GPIO devices, this won't work right now due to limitations of fwnode regarding to the sturct device.

So, it means we may not have shared primary with different secondary fwnodes.

So, come back to the initial suggestion (overwrite it for now):

Quoted 9 lines

/*
* If custom fwnode provided, use it. Currently we may not
* handle the case where shared primary node has different
* secondary ones. Ideally we have to use
* set_secondary_fwnode() here.
*/
if (gc->fwnode)
device_set_node(&gdev->dev, gc->fwnode);

Other parts of gpiolib-of depend on the of_node being there. Converting it to fwnode is a whole other task so for now I suggest we just convert the fwnode to of_node in struct gpio_chip as per my patch.

Bart

#8 lore raw

Re: [PATCH v12 3/7] gpiolib: of: make fwnode take precedence in struct gpio_chip

Quoted 5 lines · Bartosz Golaszewski
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko

On Fri, Dec 03, 2021 at 02:29:59PM +0100, Bartosz Golaszewski wrote:

...

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

drivers/gpio/gpiolib-of.c void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev) · line 1054

Quoted 9 lines
  if (gc->parent)
          gdev->dev.of_node = gc->parent->of_node;

+ if (gc->fwnode)

drivers/gpio/gpiolib-of.c +2 -0
+         gc->of_node = to_of_node(gc->fwnode);
+
  /* If the gpiochip has an assigned OF node this takes precedence */
  if (gc->of_node)
          gdev->dev.of_node = gc->of_node;

Similar should be done in acpi_gpio_dev_init():

^^^^^^ (1)

...

Quoted 17 lines
Quoted 7 lines

If we have parent device for several GPIO devices, this won't work right now due to limitations of fwnode regarding to the sturct device.

So, it means we may not have shared primary with different secondary fwnodes.

So, come back to the initial suggestion (overwrite it for now):

Quoted 9 lines

/*
* If custom fwnode provided, use it. Currently we may not
* handle the case where shared primary node has different
* secondary ones. Ideally we have to use
* set_secondary_fwnode() here.
*/
if (gc->fwnode)
device_set_node(&gdev->dev, gc->fwnode);

Quoted 4 lines

Other parts of gpiolib-of depend on the of_node being there. Converting it to fwnode is a whole other task so for now I suggest we just convert the fwnode to of_node in struct gpio_chip as per my patch.

But this is about ACPI counterpart. If you do this, do this in both cases. Above code for ACPI (1).

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

Re: [PATCH v12 3/7] gpiolib: of: make fwnode take precedence in struct gpio_chip

Quoted 44 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko

On Fri, Dec 03, 2021 at 02:29:59PM +0100, Bartosz Golaszewski wrote:

...

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

drivers/gpio/gpiolib-of.c void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev) · line 1054

Quoted 9 lines
  if (gc->parent)
          gdev->dev.of_node = gc->parent->of_node;

+ if (gc->fwnode)

drivers/gpio/gpiolib-of.c +2 -0
+         gc->of_node = to_of_node(gc->fwnode);
+
  /* If the gpiochip has an assigned OF node this takes precedence */
  if (gc->of_node)
          gdev->dev.of_node = gc->of_node;

Similar should be done in acpi_gpio_dev_init():

^^^^^^ (1)

...

Quoted 17 lines
Quoted 16 lines

If we have parent device for several GPIO devices, this won't work right now due to limitations of fwnode regarding to the sturct device.

So, it means we may not have shared primary with different secondary fwnodes.

So, come back to the initial suggestion (overwrite it for now):

/*
* If custom fwnode provided, use it. Currently we may not
* handle the case where shared primary node has different
* secondary ones. Ideally we have to use
* set_secondary_fwnode() here.
*/
if (gc->fwnode)
device_set_node(&gdev->dev, gc->fwnode);

Quoted 8 lines
Quoted 4 lines

Other parts of gpiolib-of depend on the of_node being there. Converting it to fwnode is a whole other task so for now I suggest we just convert the fwnode to of_node in struct gpio_chip as per my patch.

But this is about ACPI counterpart. If you do this, do this in both cases. Above code for ACPI (1).

This series concerns the gpio-sim driver and it only uses configfs (with manually created platform devices) or device-tree. I would prefer to do ACPI separately and I'd like you to lead that because I neither have any HW to test nor claim to understand it. :)

Bart

#10 lore raw

Re: [PATCH v12 3/7] gpiolib: of: make fwnode take precedence in struct gpio_chip

Quoted 2 lines · Bartosz Golaszewski

On Fri, Dec 3, 2021 at 9:10 PM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 4 lines

This series concerns the gpio-sim driver and it only uses configfs (with manually created platform devices) or device-tree. I would prefer to do ACPI separately and I'd like you to lead that because I neither have any HW to test nor claim to understand it. :)

Please, mention this in the commit message that ACPI is not covered (yet).

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

Re: [PATCH v12 3/7] gpiolib: of: make fwnode take precedence in struct gpio_chip

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

On Fri, Dec 3, 2021 at 9:10 PM Andy Shevchenko
<[email protected]> wrote:

Quoted 9 lines

...

Quoted 4 lines

This series concerns the gpio-sim driver and it only uses configfs (with manually created platform devices) or device-tree. I would prefer to do ACPI separately and I'd like you to lead that because I neither have any HW to test nor claim to understand it. :)

Please, mention this in the commit message that ACPI is not covered (yet).

But the commit message says: "gpiolib: of: make fwnode take precedence in struct gpio_chip" - it says OF right here. :)

Bart

#12 lore raw

Re: [PATCH v12 3/7] gpiolib: of: make fwnode take precedence in struct gpio_chip

Quoted 14 lines · Bartosz Golaszewski
Quoted 12 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski

On Fri, Dec 3, 2021 at 9:10 PM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 4 lines

This series concerns the gpio-sim driver and it only uses configfs (with manually created platform devices) or device-tree. I would prefer to do ACPI separately and I'd like you to lead that because I neither have any HW to test nor claim to understand it. :)

Quoted 4 lines
Quoted 1 line

Please, mention this in the commit message that ACPI is not covered (yet).

But the commit message says: "gpiolib: of: make fwnode take precedence in struct gpio_chip" - it says OF right here. :)

It implies that reader should have a 6th sense to know about ACPI and what else? Please, be explicit over implicit.

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

Re: [PATCH v12 3/7] gpiolib: of: make fwnode take precedence in struct gpio_chip

Quoted 15 lines · Andy Shevchenko
Quoted 14 lines · Bartosz Golaszewski
Quoted 12 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski

On Fri, Dec 3, 2021 at 9:10 PM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 4 lines

This series concerns the gpio-sim driver and it only uses configfs (with manually created platform devices) or device-tree. I would prefer to do ACPI separately and I'd like you to lead that because I neither have any HW to test nor claim to understand it. :)

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Please, mention this in the commit message that ACPI is not covered (yet).

But the commit message says: "gpiolib: of: make fwnode take precedence in struct gpio_chip" - it says OF right here. :)

It implies that reader should have a 6th sense to know about ACPI and what else? Please, be explicit over implicit.

The problem here is that you change one case and haven't touched the rest which brings inconsistency in the behaviour on different resource providers.

We need a record to be sure what disbalance this patch brought. That's why I asked to mention ACPI branch.

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

Re: [PATCH v12 3/7] gpiolib: of: make fwnode take precedence in struct gpio_chip

In reply to Bartosz Golaszewski
Quoted 2 lines · Bartosz Golaszewski

If the driver sets the fwnode in struct gpio_chip, let it take precedence over the of_node.

By the way, have you tried this on pure DT-less/ACPI-less platform (CONFIG_OF=n, CONFIG_ACPI=n)? I believe gpio-sim in that case won't work, because this doesn't affect swnode case, right?

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

Re: [PATCH v12 3/7] gpiolib: of: make fwnode take precedence in struct gpio_chip

Quoted 9 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski

If the driver sets the fwnode in struct gpio_chip, let it take precedence over the of_node.

By the way, have you tried this on pure DT-less/ACPI-less platform (CONFIG_OF=n, CONFIG_ACPI=n)? I believe gpio-sim in that case won't work, because this doesn't affect swnode case, right?

Works just fine on a BeagleBone Black - both the regular GPIO controllers as well as DT-instantiated gpio-sim.

Bart

#16 lore raw

Re: [PATCH v12 3/7] gpiolib: of: make fwnode take precedence in struct gpio_chip

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

If the driver sets the fwnode in struct gpio_chip, let it take precedence over the of_node.

Quoted 7 lines
Quoted 4 lines

By the way, have you tried this on pure DT-less/ACPI-less platform (CONFIG_OF=n, CONFIG_ACPI=n)? I believe gpio-sim in that case won't work, because this doesn't affect swnode case, right?

Works just fine on a BeagleBone Black - both the regular GPIO controllers as well as DT-instantiated gpio-sim.

Yeah, I realized that myself why.

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

Re: [PATCH v12 3/7] gpiolib: of: make fwnode take precedence in struct gpio_chip

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

If the driver sets the fwnode in struct gpio_chip, let it take precedence over the of_node.

By the way, have you tried this on pure DT-less/ACPI-less platform (CONFIG_OF=n, CONFIG_ACPI=n)? I believe gpio-sim in that case won't work, because this doesn't affect swnode case, right?

Okay, swnode will work (*) due to previous patch.

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

PATCH v12 4/7 gpio: sim: new testing module

In reply to Bartosz Golaszewski

Implement a new, modern GPIO testing module controlled by configfs attributes instead of module parameters. The goal of this driver is to provide a replacement for gpio-mockup that will be easily extensible with new features and doesn't require reloading the module to change the setup.

---
 Documentation/admin-guide/gpio/gpio-sim.rst |  134 ++
 drivers/gpio/Kconfig                        |    8 +
 drivers/gpio/Makefile                       |    1 +
 drivers/gpio/gpio-sim.c                     | 1594 +++++++++++++++++++
 4 files changed, 1737 insertions(+)
 create mode 100644 Documentation/admin-guide/gpio/gpio-sim.rst
 create mode 100644 drivers/gpio/gpio-sim.c
Documentation/admin-guide/gpio/gpio-sim.rst new file +134 -0
new file mode 100644
index 000000000000..d8a90c81b9ee
@@ -0,0 +1,134 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later
+
+Configfs GPIO Simulator
+=======================
+
+The configfs GPIO Simulator (gpio-sim) provides a way to create simulated GPIO
+chips for testing purposes. The lines exposed by these chips can be accessed
+using the standard GPIO character device interface as well as manipulated
+using sysfs attributes.
+
+Creating simulated chips
+------------------------
+
+The gpio-sim module registers a configfs subsystem called ``'gpio-sim'``. For
+details of the configfs filesystem, please refer to the configfs documentation.
+
+The user can create a hierarchy of configfs groups and items as well as modify
+values of exposed attributes. Once the chip is instantiated, this hierarchy
+will be translated to appropriate device properties. The general structure is:
+
+**Group:** ``/config/gpio-sim``
+
+This is the top directory of the gpio-sim configfs tree.
+
+**Group:** ``/config/gpio-sim/gpio-device``
+
+**Attribute:** ``/config/gpio-sim/gpio-device/dev_name``
+
+**Attribute:** ``/config/gpio-sim/gpio-device/live``
+
+This is a directory representing a GPIO platform device. The ``'dev_name'``
+attribute is read-only and allows the user-space to read the platform device
+name (e.g. ``'gpio-sim.0'``). The ``'live'`` attribute allows to trigger the
+actual creation of the device once it's fully configured. The accepted values
+are: ``'1'`` to enable the simulated device and ``'0'`` to disable and tear
+it down.
+
+**Group:** ``/config/gpio-sim/gpio-device/gpio-bankX``
+
+**Attribute:** ``/config/gpio-sim/gpio-device/gpio-bankX/chip_name``
+
+**Attribute:** ``/config/gpio-sim/gpio-device/gpio-bankX/num_lines``
+
+This group represents a bank of GPIOs under the top platform device. The
+``'chip_name'`` attribute is read-only and allows the user-space to read the
+device name of the bank device. The ``'num_lines'`` attribute allows to specify
+the number of lines exposed by this bank.
+
+**Group:** ``/config/gpio-sim/gpio-device/gpio-bankX/lineY``
+
+**Attribute:** ``/config/gpio-sim/gpio-device/gpio-bankX/lineY/name``
+
+This group represents a single line at the offset Y. The 'name' attribute
+allows to set the line name as represented by the 'gpio-line-names' property.
+
+**Item:** ``/config/gpio-sim/gpio-device/gpio-bankX/lineY/hog``
+
+**Attribute:** ``/config/gpio-sim/gpio-device/gpio-bankX/lineY/hog/name``
+
+**Attribute:** ``/config/gpio-sim/gpio-device/gpio-bankX/lineY/hog/direction``
+
+This item makes the gpio-sim module hog the associated line. The ``'name'``
+attribute specifies the in-kernel consumer name to use. The ``'direction'``
+attribute specifies the hog direction and must be one of: ``'input'``,
+``'output-high'`` and ``'output-low'``.
+
+Inside each bank directory, there's a set of attributes that can be used to
+configure the new chip. Additionally the user can ``mkdir()`` subdirectories
+inside the chip's directory that allow to pass additional configuration for
+specific lines. The name of those subdirectories must take the form of:
+``'line<offset>'`` (e.g. ``'line0'``, ``'line20'``, etc.) as the name will be
+used by the module to assign the config to the specific line at given offset.
+
+Once the confiuration is complete, the ``'live'`` attribute must be set to 1 in
+order to instantiate the chip. It can be set back to 0 to destroy the simulated
+chip. The module will synchronously wait for the new simulated device to be
+successfully probed and if this doesn't happen, writing to ``'live'`` will
+result in an error.
+
+Simulated GPIO chips can also be defined in device-tree. The compatible string
+must be: ``"gpio-simulator"``. Supported properties are:
+
+  ``"gpio-sim,label"`` - chip label
+
+Other standard GPIO properties (like ``"gpio-line-names"``, ``"ngpios"`` or
+``"gpio-hog"``) are also supported. Please refer to the GPIO documentation for
+details.
+
+An example device-tree code defining a GPIO simulator:
+
+.. code-block :: none
+
+    gpio-sim {
+        compatible = "gpio-simulator";
+
+        bank0 {
+            gpio-controller;
+            #gpio-cells = <2>;
+            ngpios = <16>;
+            gpio-sim,label = "dt-bank0";
+            gpio-line-names = "", "sim-foo", "", "sim-bar";
+        };
+
+        bank1 {
+            gpio-controller;
+            #gpio-cells = <2>;
+            ngpios = <8>;
+            gpio-sim,label = "dt-bank1";
+
+            line3 {
+                gpio-hog;
+                gpios = <3 0>;
+                output-high;
+                line-name = "sim-hog-from-dt";
+            };
+        };
+    };
+
+Manipulating simulated lines
+----------------------------
+
+Each simulated GPIO chip creates a separate sysfs group under its device
+directory for each exposed line
+(e.g. ``/sys/devices/platform/gpio-sim.X/gpiochipY/``). The name of each group
+is of the form: ``'sim_gpioX'`` where X is the offset of the line. Inside each
+group there are two attibutes:
+
+    ``pull`` - allows to read and set the current simulated pull setting for
+               every line, when writing the value must be one of: ``'pull-up'``,
+               ``'pull-down'``
+
+    ``value`` - allows to read the current value of the line which may be
+                different from the pull if the line is being driven from
+                user-space
drivers/gpio/Kconfig +8 -0
index 60d9374c72c0..9acdb4d1047b 100644
@@ -1694,6 +1694,14 @@ config GPIO_VIRTIO
 	  These virtual GPIOs can be routed to real GPIOs or attached to
 	  simulators on the host (like QEMU).
 
+config GPIO_SIM
+	tristate "GPIO Simulator Module"
+	select IRQ_SIM
+	select CONFIGFS_FS
+	help
+	  This enables the GPIO simulator - a configfs-based GPIO testing
+	  driver.
+
 endmenu
 
 endif
drivers/gpio/Makefile +1 -0
index 71ee9fc2ff83..f21577de2474 100644
@@ -133,6 +133,7 @@ obj-$(CONFIG_GPIO_SAMA5D2_PIOBU)	+= gpio-sama5d2-piobu.o
 obj-$(CONFIG_GPIO_SCH311X)		+= gpio-sch311x.o
 obj-$(CONFIG_GPIO_SCH)			+= gpio-sch.o
 obj-$(CONFIG_GPIO_SIFIVE)		+= gpio-sifive.o
+obj-$(CONFIG_GPIO_SIM)			+= gpio-sim.o
 obj-$(CONFIG_GPIO_SIOX)			+= gpio-siox.o
 obj-$(CONFIG_GPIO_SL28CPLD)		+= gpio-sl28cpld.o
 obj-$(CONFIG_GPIO_SODAVILLE)		+= gpio-sodaville.o
drivers/gpio/gpio-sim.c new file +1594 -0

1,594 lines not on this page · raw message

Signature · 1 line
2.25.1
#19 lore raw

Re: [PATCH v12 4/7] gpio: sim: new testing module

Quoted 5 lines · Bartosz Golaszewski

Implement a new, modern GPIO testing module controlled by configfs attributes instead of module parameters. The goal of this driver is to provide a replacement for gpio-mockup that will be easily extensible with new features and doesn't require reloading the module to change the setup.

...

Documentation/admin-guide/gpio/gpio-sim.rst line 36

Quoted 12 lines
Documentation/admin-guide/gpio/gpio-sim.rst new file +12 -0
+**Group:** ``/config/gpio-sim/gpio-device``
+
+**Attribute:** ``/config/gpio-sim/gpio-device/dev_name``
+
+**Attribute:** ``/config/gpio-sim/gpio-device/live``
+
+This is a directory representing a GPIO platform device. The ``'dev_name'``
+attribute is read-only and allows the user-space to read the platform device
+name (e.g. ``'gpio-sim.0'``). The ``'live'`` attribute allows to trigger the
+actual creation of the device once it's fully configured. The accepted values
+are: ``'1'`` to enable the simulated device and ``'0'`` to disable and tear
+it down.

Perhaps it makes sense to describe properties in the order you expect to be used, then it will be naturally to 'read and repeat' without jumping forward and backward through the documentation.

...

Documentation/admin-guide/gpio/gpio-sim.rst line 42

Quoted 5 lines
Documentation/admin-guide/gpio/gpio-sim.rst new file +4 -0
+**Group:** ``/config/gpio-sim/gpio-device/gpio-bankX``
+
+**Attribute:** ``/config/gpio-sim/gpio-device/gpio-bankX/chip_name``
 
+**Attribute:** ``/config/gpio-sim/gpio-device/gpio-bankX/num_lines``

Why not to use the same name as in DT, i.e. ngpios?

...

drivers/gpio/gpio-sim.c line 15

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+#include <linux/gpio/driver.h>
+#include <linux/gpio/machine.h>

I would rather move this group below to emphasize that this is closer to GPIO then to other APIs.

drivers/gpio/gpio-sim.c line 31

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+#include <linux/sysfs.h>
+

...here.

Quoted 1 line

+#include "gpiolib.h"

...

drivers/gpio/gpio-sim.c line 61

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+static int gpio_sim_apply_pull(struct gpio_sim_chip *chip,
+			       unsigned int offset, int value)

I would use up to 100 here...

drivers/gpio/gpio-sim.c line 73

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+	if (test_bit(FLAG_REQUESTED, &desc->flags) &&
+	    !test_bit(FLAG_IS_OUT, &desc->flags)) {

...here and so on.

But it's up to you.

...

drivers/gpio/gpio-sim.c line 75

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+		curr_val = !!test_bit(offset, chip->value_map);
+		if (curr_val == value)

Do you use curr_val anywhere else? Perhaps combine these two lines.

Quoted 1 line
(quoted diff) +1 -0
+			goto set_pull;

...

Quoted 6 lines
drivers/gpio/gpio-sim.c new file +6 -0
+static int gpio_sim_set_config(struct gpio_chip *gc,
+				  unsigned int offset, unsigned long config)
+{
+	struct gpio_sim_chip *chip = gpiochip_get_data(gc);
+
+	switch (pinconf_to_config_param(config)) {

drivers/gpio/gpio-sim.c line 204

Quoted 10 lines
drivers/gpio/gpio-sim.c new file +9 -0
+	case PIN_CONFIG_BIAS_PULL_UP:
+		return gpio_sim_apply_pull(chip, offset, 1);
+	case PIN_CONFIG_BIAS_PULL_DOWN:
+		return gpio_sim_apply_pull(chip, offset, 0);
+	default:
 
+		break;
+	}
+
+	return -ENOTSUPP;

return directly from switch-case?

Quoted 1 line

+}

...

drivers/gpio/gpio-sim.c line 255

Quoted 8 lines
drivers/gpio/gpio-sim.c new file +8 -0
+static ssize_t gpio_sim_sysfs_pull_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
+	struct gpio_sim_chip *chip = dev_get_drvdata(dev);
+	char *repr;
+	int pull;
	int pull_up;

? Also, where is "pull-none"?

drivers/gpio/gpio-sim.c line 266

Quoted 10 lines
drivers/gpio/gpio-sim.c new file +9 -0
+	mutex_lock(&chip->lock);
+	pull = !!test_bit(line_attr->offset, chip->pull_map);
+	mutex_unlock(&chip->lock);
 
+	if (pull)
+		repr = "pull-up";
+	else
+		repr = "pull-down";
+
+	return sysfs_emit(buf, "%s\n", repr);
	return sysfs_emit(buf, "%pull-s\n", pull_up ? "up" : "down");

?

Quoted 1 line

+}

...

Quoted 4 lines
drivers/gpio/gpio-sim.c new file +4 -0
+static ssize_t gpio_sim_sysfs_pull_store(struct device *dev,
+					 struct device_attribute *attr,
+					 const char *buf, size_t len)
+{

drivers/gpio/gpio-sim.c line 282

Quoted 10 lines
drivers/gpio/gpio-sim.c new file +10 -0
+	struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
+	struct gpio_sim_chip *chip = dev_get_drvdata(dev);
+	int ret, pull;
+
+	if (sysfs_streq(buf, "pull-down"))
+		pull = 0;
+	else if (sysfs_streq(buf, "pull-up"))
+		pull = 1;
+	else
+		return -EINVAL;

sysfs_match_string() and use the very same string array in the above function to print them?

Same question about "pull-none".

Quoted 6 lines
drivers/gpio/gpio-sim.c new file +6 -0
+	ret = gpio_sim_apply_pull(chip, line_attr->offset, pull);
+	if (ret)
+		return ret;
+
+	return len;
+}

...

drivers/gpio/gpio-sim.c line 330

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+		attr_group->name = devm_kasprintf(dev, GFP_KERNEL,
+						  "sim_gpio%u", i);

Wondering if you can use devm_kasprintf_strarray().

Quoted 2 lines
(quoted diff) +2 -0
+		if (!attr_group->name)
+			return -ENOMEM;

...

drivers/gpio/gpio-sim.c line 396

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+	/* Default to input mode. */
+	bitmap_fill(chip->direction_map, num_lines);

More accurate is to use bitmap_set(). If we ever debug this it also helpful.

...

drivers/gpio/gpio-sim.c line 412

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+	ret = devm_add_action_or_reset(dev, gpio_sim_mutex_destroy,
+				       &chip->lock);

It's 81, fine to have on one line.

Quoted 2 lines
(quoted diff) +2 -0
+	if (ret)
+		return ret;

...

drivers/gpio/gpio-sim.c line 647

Quoted 12 lines
drivers/gpio/gpio-sim.c new file +12 -0
+static char *gpio_sim_strdup_trimmed(const char *str, size_t count)
+{
+	char *dup, *trimmed, *ret;
+
+	dup = kstrndup(str, count, GFP_KERNEL);
+	if (!dup)
+		return NULL;
+
+	trimmed = strstrip(dup);
+	ret = kstrdup(trimmed, GFP_KERNEL);
+	kfree(dup);
+	return ret;

Why not memmove() instead of additional memory allocation?

Or if you really want to save bytes, krealloc() after?

	char *dup, *start, *ret;
	size_t len;

	dup = kstrndup(str, count, GFP_KERNEL);
	if (!dup)
		return NULL;

	start = strstrip(dup);
	len = strlen(start) - (start - dup);

	memmove(dup, start, len + 1);

	ret = krealloc(dup, len + 1, GFP_KERNEL);
	if (ret)
		return ret;

	kfree(dup);
	return NULL;

?

Quoted 1 line

+}

...

drivers/gpio/gpio-sim.c line 680

Quoted 1 line
drivers/gpio/gpio-sim.c new file +1 -0
+	return sprintf(page, "%c\n", live ? '1' : '0');
	return sprintf(page, "%d\n", live ? 1 : 0);

?

...

drivers/gpio/gpio-sim.c line 754

Quoted 8 lines
drivers/gpio/gpio-sim.c new file +7 -0
+	list_for_each_entry(bank, &dev->bank_list, siblings) {
+		list_for_each_entry(line, &bank->line_list, siblings) {
+			if (line->hog)
+				num_hogs++;
+		}
+	}
 
+

No need to have a blank line here, but up to you.

Quoted 2 lines
(quoted diff) +2 -0
+	if (!num_hogs)
+		return 0;

...

drivers/gpio/gpio-sim.c line 855

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+		list_for_each_entry(pos, &dev->bank_list, siblings) {
+			if (this == pos || (!this->label || !pos->label))

Too many parentheses.

Quoted 5 lines
drivers/gpio/gpio-sim.c new file +5 -0
+				continue;
+
+			if (strcmp(this->label, pos->label) == 0)
+				return true;
+		}

...

drivers/gpio/gpio-sim.c line 955

Quoted 1 line
drivers/gpio/gpio-sim.c new file +1 -0
+	ret = kstrtouint(page, 10, &live);

Why not kstrtobool() (according to the documentation)?

Quoted 5 lines
drivers/gpio/gpio-sim.c new file +5 -0
+	if (ret)
+		return ret;
+
+	mutex_lock(&dev->lock);
+

drivers/gpio/gpio-sim.c line 969

Quoted 10 lines
drivers/gpio/gpio-sim.c new file +9 -0
+	if ((live == 0 && !gpio_sim_device_is_live_unlocked(dev)) ||
+	    (live == 1 && gpio_sim_device_is_live_unlocked(dev)))
+		ret = -EPERM;
+	else if (live == 1)
+		ret = gpio_sim_device_activate_unlocked(dev);
+	else if (live == 0)
+		gpio_sim_device_deactivate_unlocked(dev);
 
+	else
+		ret = -EINVAL;

This will gone if above is being applied.

Quoted 1 line
(quoted diff) +1 -0
+	mutex_unlock(&dev->lock);

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+	mutex_lock(&dev->lock);
+	ret = sprintf(page, "%s\n", bank->label ?: "");

Don't we use "?" in the GPIO library for similar situations?

Quoted 1 line
(quoted diff) +1 -0
+	mutex_unlock(&dev->lock);

...

drivers/gpio/gpio-sim.c line 1096

Quoted 1 line
drivers/gpio/gpio-sim.c new file +1 -0
+	ret = kstrtouint(page, 10, &num_lines);

Why not allowing any digit base?

Quoted 2 lines
(quoted diff) +2 -0
+	if (ret)
+		return ret;

...

Quoted 15 lines
drivers/gpio/gpio-sim.c new file +15 -0
+	switch (dir) {
+	case GPIOD_IN:
+		repr = "input";
+		break;
+	case GPIOD_OUT_HIGH:
+		repr = "output-high";
+		break;
+	case GPIOD_OUT_LOW:
+		repr = "output-low";
+		break;
+	default:
+		/* This would be a programmer bug. */
+		WARN(1, "Unexpected hog direction value: %d", dir);
+		return -EINVAL;
+	}

drivers/gpio/gpio-sim.c line 1278

Quoted 10 lines
drivers/gpio/gpio-sim.c new file +8 -0
 
 
+	if (strcmp(trimmed, "input") == 0)
+		dir = GPIOD_IN;
+	else if (strcmp(trimmed, "output-high") == 0)
+		dir = GPIOD_OUT_HIGH;
+	else if (strcmp(trimmed, "output-low") == 0)
+		dir = GPIOD_OUT_LOW;
+	else
+		dir = -EINVAL;

Same idea, i.e. static string array and use it above and here with help of match_string().

...

drivers/gpio/gpio-sim.c line 1299

Quoted 5 lines
drivers/gpio/gpio-sim.c new file +4 -0
+static struct configfs_attribute *gpio_sim_hog_config_attrs[] = {
+	&gpio_sim_hog_config_attr_name,
+	&gpio_sim_hog_config_attr_direction,
 
+	NULL,

Comma is not needed.

Quoted 1 line

+};

...

drivers/gpio/gpio-sim.c line 1531

Quoted 9 lines
drivers/gpio/gpio-sim.c new file +9 -0
+	id = ida_alloc(&gpio_sim_ida, GFP_KERNEL);
+	if (id < 0) {
+		kfree(dev);
+		return ERR_PTR(id);
+	}
+
+	config_group_init_type_name(&dev->group, name,
+				    &gpio_sim_device_config_group_type);
+	dev->id = id;

If you group this assignment with above allocation it would look better.

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

Re: [PATCH v12 4/7] gpio: sim: new testing module

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

Implement a new, modern GPIO testing module controlled by configfs attributes instead of module parameters. The goal of this driver is to provide a replacement for gpio-mockup that will be easily extensible with new features and doesn't require reloading the module to change the setup.

...

Quoted 17 lines

Documentation/admin-guide/gpio/gpio-sim.rst line 36

Quoted 12 lines
Documentation/admin-guide/gpio/gpio-sim.rst new file +12 -0
+**Group:** ``/config/gpio-sim/gpio-device``
+
+**Attribute:** ``/config/gpio-sim/gpio-device/dev_name``
+
+**Attribute:** ``/config/gpio-sim/gpio-device/live``
+
+This is a directory representing a GPIO platform device. The ``'dev_name'``
+attribute is read-only and allows the user-space to read the platform device
+name (e.g. ``'gpio-sim.0'``). The ``'live'`` attribute allows to trigger the
+actual creation of the device once it's fully configured. The accepted values
+are: ``'1'`` to enable the simulated device and ``'0'`` to disable and tear
+it down.

Perhaps it makes sense to describe properties in the order you expect to be used, then it will be naturally to 'read and repeat' without jumping forward and backward through the documentation.

This is such order though. You naturally configure the device, then bank, then lines, then hogs.

Quoted 10 lines

...

Documentation/admin-guide/gpio/gpio-sim.rst line 42

Quoted 5 lines
Documentation/admin-guide/gpio/gpio-sim.rst new file +4 -0
+**Group:** ``/config/gpio-sim/gpio-device/gpio-bankX``
+
+**Attribute:** ``/config/gpio-sim/gpio-device/gpio-bankX/chip_name``
 
+**Attribute:** ``/config/gpio-sim/gpio-device/gpio-bankX/num_lines``

Why not to use the same name as in DT, i.e. ngpios?

This would be the only attribute that follows the DT naming, the label, line names etc. wouldn't use the same name anyway. I don't see any advantage of it as num_lines is actually more intuitive than ngpios IMO.

Quoted 8 lines

...

drivers/gpio/gpio-sim.c line 15

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+#include <linux/gpio/driver.h>
+#include <linux/gpio/machine.h>

I would rather move this group below to emphasize that this is closer to GPIO then to other APIs.

Quoted 5 lines

drivers/gpio/gpio-sim.c line 31

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+#include <linux/sysfs.h>
+

...here.

With the number of headers in this file, I'd stick with alphabetical order.

Quoted 7 lines
Quoted 1 line

+#include "gpiolib.h"

...

drivers/gpio/gpio-sim.c line 61

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+static int gpio_sim_apply_pull(struct gpio_sim_chip *chip,
+                            unsigned int offset, int value)
Quoted 9 lines

I would use up to 100 here...

drivers/gpio/gpio-sim.c line 73

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+     if (test_bit(FLAG_REQUESTED, &desc->flags) &&
+         !test_bit(FLAG_IS_OUT, &desc->flags)) {

...here and so on.

But it's up to you.

Nah, the lines are broken just fine. Let's not overuse the limit.

Quoted 9 lines

...

drivers/gpio/gpio-sim.c line 75

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+             curr_val = !!test_bit(offset, chip->value_map);
+             if (curr_val == value)

Do you use curr_val anywhere else? Perhaps combine these two lines.

Quoted 1 line

+ goto set_pull;

Good point.

Quoted 14 lines

...

Quoted 11 lines
drivers/gpio/gpio-sim.c new file +11 -0
+static int gpio_sim_set_config(struct gpio_chip *gc,
+                               unsigned int offset, unsigned long config)
+{
+     struct gpio_sim_chip *chip = gpiochip_get_data(gc);
+
+     switch (pinconf_to_config_param(config)) {
+     case PIN_CONFIG_BIAS_PULL_UP:
+             return gpio_sim_apply_pull(chip, offset, 1);
+     case PIN_CONFIG_BIAS_PULL_DOWN:
+             return gpio_sim_apply_pull(chip, offset, 0);
+     default:
Quoted 7 lines

drivers/gpio/gpio-sim.c line 204

Quoted 4 lines
drivers/gpio/gpio-sim.c new file +4 -0
+             break;
+     }
+
+     return -ENOTSUPP;

return directly from switch-case?

This may be a personal preference but I don't like returning functions without a return at the end. Always looks wrong at first glance. I'd like to keep it.

Quoted 15 lines
Quoted 1 line

+}

...

drivers/gpio/gpio-sim.c line 255

Quoted 8 lines
drivers/gpio/gpio-sim.c new file +8 -0
+static ssize_t gpio_sim_sysfs_pull_show(struct device *dev,
+                                     struct device_attribute *attr,
+                                     char *buf)
+{
+     struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
+     struct gpio_sim_chip *chip = dev_get_drvdata(dev);
+     char *repr;
+     int pull;
        int pull_up;
Quoted 2 lines

? Also, where is "pull-none"?

There isn't. If it's ever needed, we can extend the driver but so far there hasn't been a need for it when testing from user-space.

Quoted 11 lines

drivers/gpio/gpio-sim.c line 266

Quoted 10 lines
drivers/gpio/gpio-sim.c new file +9 -0
+     mutex_lock(&chip->lock);
+     pull = !!test_bit(line_attr->offset, chip->pull_map);
+     mutex_unlock(&chip->lock);
 
+     if (pull)
+             repr = "pull-up";
+     else
+             repr = "pull-down";
+
+     return sysfs_emit(buf, "%s\n", repr);
Quoted 3 lines
        return sysfs_emit(buf, "%pull-s\n", pull_up ? "up" : "down");

?

Sure.

Quoted 34 lines
Quoted 2 lines

+}

...

Quoted 4 lines
drivers/gpio/gpio-sim.c new file +4 -0
+static ssize_t gpio_sim_sysfs_pull_store(struct device *dev,
+                                      struct device_attribute *attr,
+                                      const char *buf, size_t len)
+{

drivers/gpio/gpio-sim.c line 282

Quoted 10 lines
drivers/gpio/gpio-sim.c new file +10 -0
+     struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
+     struct gpio_sim_chip *chip = dev_get_drvdata(dev);
+     int ret, pull;
+
+     if (sysfs_streq(buf, "pull-down"))
+             pull = 0;
+     else if (sysfs_streq(buf, "pull-up"))
+             pull = 1;
+     else
+             return -EINVAL;

sysfs_match_string() and use the very same string array in the above function to print them?

Same question about "pull-none".

Quoted 6 lines
drivers/gpio/gpio-sim.c new file +6 -0
+     ret = gpio_sim_apply_pull(chip, line_attr->offset, pull);
+     if (ret)
+             return ret;
+
+     return len;
+}

...

Quoted 5 lines

drivers/gpio/gpio-sim.c line 330

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+             attr_group->name = devm_kasprintf(dev, GFP_KERNEL,
+                                               "sim_gpio%u", i);

Wondering if you can use devm_kasprintf_strarray().

I would need to do that in a separate loop and handle the new array, I think it's an overkill here.

Quoted 10 lines
Quoted 2 lines
(quoted diff) +2 -0
+             if (!attr_group->name)
+                     return -ENOMEM;

...

drivers/gpio/gpio-sim.c line 396

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+     /* Default to input mode. */
+     bitmap_fill(chip->direction_map, num_lines);

More accurate is to use bitmap_set(). If we ever debug this it also helpful.

I'm not sure what you mean, this sets all bits to 1.

Quoted 76 lines

...

drivers/gpio/gpio-sim.c line 412

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+     ret = devm_add_action_or_reset(dev, gpio_sim_mutex_destroy,
+                                    &chip->lock);

It's 81, fine to have on one line.

Quoted 2 lines
(quoted diff) +2 -0
+     if (ret)
+             return ret;

...

drivers/gpio/gpio-sim.c line 647

Quoted 12 lines
drivers/gpio/gpio-sim.c new file +12 -0
+static char *gpio_sim_strdup_trimmed(const char *str, size_t count)
+{
+     char *dup, *trimmed, *ret;
+
+     dup = kstrndup(str, count, GFP_KERNEL);
+     if (!dup)
+             return NULL;
+
+     trimmed = strstrip(dup);
+     ret = kstrdup(trimmed, GFP_KERNEL);
+     kfree(dup);
+     return ret;

Why not memmove() instead of additional memory allocation?

Or if you really want to save bytes, krealloc() after?

        char *dup, *start, *ret;
        size_t len;

        dup = kstrndup(str, count, GFP_KERNEL);
        if (!dup)
                return NULL;

        start = strstrip(dup);
        len = strlen(start) - (start - dup);

        memmove(dup, start, len + 1);

        ret = krealloc(dup, len + 1, GFP_KERNEL);
        if (ret)
                return ret;

        kfree(dup);
        return NULL;

?

Quoted 1 line

+}

...

Quoted 1 line

+ return sprintf(page, "%c\n", live ? '1' : '0');

        return sprintf(page, "%d\n", live ? 1 : 0);

?

...

drivers/gpio/gpio-sim.c line 754

Quoted 8 lines
drivers/gpio/gpio-sim.c new file +7 -0
+     list_for_each_entry(bank, &dev->bank_list, siblings) {
+             list_for_each_entry(line, &bank->line_list, siblings) {
+                     if (line->hog)
+                             num_hogs++;
+             }
+     }
 
+

No need to have a blank line here, but up to you.

Review

Quoted 2 lines
(quoted diff) +2 -0
+     if (!num_hogs)
+             return 0;
Quoted 7 lines

...

drivers/gpio/gpio-sim.c line 855

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+             list_for_each_entry(pos, &dev->bank_list, siblings) {
+                     if (this == pos || (!this->label || !pos->label))

Too many parentheses.

No, this is the logic here. Skip either if both pointers point to the same object or check if the labels are missing in at least one.

Quoted 6 lines

drivers/gpio/gpio-sim.c line 860

Quoted 5 lines
drivers/gpio/gpio-sim.c new file +5 -0
+                             continue;
+
+                     if (strcmp(this->label, pos->label) == 0)
+                             return true;
+             }
Quoted 6 lines

...

Quoted 1 line

+ ret = kstrtouint(page, 10, &live);

Why not kstrtobool() (according to the documentation)?

Sure.

Quoted 18 lines
Quoted 5 lines
drivers/gpio/gpio-sim.c new file +5 -0
+     if (ret)
+             return ret;
+
+     mutex_lock(&dev->lock);
+

drivers/gpio/gpio-sim.c line 969

Quoted 10 lines
drivers/gpio/gpio-sim.c new file +9 -0
+     if ((live == 0 && !gpio_sim_device_is_live_unlocked(dev)) ||
+         (live == 1 && gpio_sim_device_is_live_unlocked(dev)))
+             ret = -EPERM;
+     else if (live == 1)
+             ret = gpio_sim_device_activate_unlocked(dev);
+     else if (live == 0)
+             gpio_sim_device_deactivate_unlocked(dev);
 
+     else
+             ret = -EINVAL;

This will gone if above is being applied.

Quoted 9 lines
Quoted 1 line

+ mutex_unlock(&dev->lock);

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+     mutex_lock(&dev->lock);
+     ret = sprintf(page, "%s\n", bank->label ?: "");

Don't we use "?" in the GPIO library for similar situations?

We use it in gpiolib to indicate a lack of label but here, it's the configuration part. I think an empty string works fine.

Quoted 8 lines
Quoted 1 line

+ mutex_unlock(&dev->lock);

...

Quoted 1 line

+ ret = kstrtouint(page, 10, &num_lines);

Why not allowing any digit base?

Sure.

Quoted 32 lines
Quoted 2 lines
(quoted diff) +2 -0
+     if (ret)
+             return ret;

...

Quoted 15 lines
drivers/gpio/gpio-sim.c new file +15 -0
+     switch (dir) {
+     case GPIOD_IN:
+             repr = "input";
+             break;
+     case GPIOD_OUT_HIGH:
+             repr = "output-high";
+             break;
+     case GPIOD_OUT_LOW:
+             repr = "output-low";
+             break;
+     default:
+             /* This would be a programmer bug. */
+             WARN(1, "Unexpected hog direction value: %d", dir);
+             return -EINVAL;
+     }

drivers/gpio/gpio-sim.c line 1278

Quoted 10 lines
drivers/gpio/gpio-sim.c new file +8 -0
 
 
+     if (strcmp(trimmed, "input") == 0)
+             dir = GPIOD_IN;
+     else if (strcmp(trimmed, "output-high") == 0)
+             dir = GPIOD_OUT_HIGH;
+     else if (strcmp(trimmed, "output-low") == 0)
+             dir = GPIOD_OUT_LOW;
+     else
+             dir = -EINVAL;
Quoted 3 lines

Same idea, i.e. static string array and use it above and here with help of match_string().

It would be great but GPIOD_IN etc. are bit flags and not sequence enums.

Quoted 24 lines

...

drivers/gpio/gpio-sim.c line 1299

Quoted 5 lines
drivers/gpio/gpio-sim.c new file +4 -0
+static struct configfs_attribute *gpio_sim_hog_config_attrs[] = {
+     &gpio_sim_hog_config_attr_name,
+     &gpio_sim_hog_config_attr_direction,
 
+     NULL,

Comma is not needed.

Quoted 1 line

+};

...

drivers/gpio/gpio-sim.c line 1531

Quoted 9 lines
drivers/gpio/gpio-sim.c new file +9 -0
+     id = ida_alloc(&gpio_sim_ida, GFP_KERNEL);
+     if (id < 0) {
+             kfree(dev);
+             return ERR_PTR(id);
+     }
+
+     config_group_init_type_name(&dev->group, name,
+                                 &gpio_sim_device_config_group_type);
+     dev->id = id;
Quoted 2 lines

If you group this assignment with above allocation it would look better.

Actually I think it looks better now with allocating the resources first, then setting up the structure.

Bart

Quoted 5 lines

--
With Best Regards,
Andy Shevchenko

#21 lore raw

Re: [PATCH v12 4/7] gpio: sim: new testing module

Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko

On Fri, Dec 03, 2021 at 02:30:00PM +0100, Bartosz Golaszewski wrote:

...

Quoted 6 lines
Quoted 6 lines

drivers/gpio/gpio-sim.c line 15

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+#include <linux/gpio/driver.h>
+#include <linux/gpio/machine.h>

I would rather move this group below to emphasize that this is closer to GPIO then to other APIs.

Quoted 7 lines
Quoted 5 lines

drivers/gpio/gpio-sim.c line 31

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+#include <linux/sysfs.h>
+

...here.

With the number of headers in this file, I'd stick with alphabetical order.

I understand that and agree, but my point is orthogonal to this. The idea is to emphasize that "hey. this driver has tough relations with the GPIO subsystem". This is the way, for example, IIO does and I like it.

Quoted 1 line
Quoted 1 line
Quoted 1 line

+#include "gpiolib.h"

...

Quoted 8 lines
Quoted 8 lines

drivers/gpio/gpio-sim.c line 61

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+static int gpio_sim_apply_pull(struct gpio_sim_chip *chip,
+                            unsigned int offset, int value)

I would use up to 100 here...

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+     if (test_bit(FLAG_REQUESTED, &desc->flags) &&
+         !test_bit(FLAG_IS_OUT, &desc->flags)) {
Quoted 6 lines
Quoted 4 lines

...here and so on.

But it's up to you.

Nah, the lines are broken just fine. Let's not overuse the limit.

Yes, but I would consider to join back those which are up to ~83 characters (I already pointed out at least to one example like this).

...

Quoted 9 lines
Quoted 9 lines

drivers/gpio/gpio-sim.c line 282

Quoted 6 lines
drivers/gpio/gpio-sim.c new file +6 -0
+     if (sysfs_streq(buf, "pull-down"))
+             pull = 0;
+     else if (sysfs_streq(buf, "pull-up"))
+             pull = 1;
+     else
+             return -EINVAL;

sysfs_match_string() and use the very same string array in the above function to print them?

I suppose you agree on this?

...

Quoted 6 lines
Quoted 4 lines

drivers/gpio/gpio-sim.c line 396

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+     /* Default to input mode. */
+     bitmap_fill(chip->direction_map, num_lines);

More accurate is to use bitmap_set(). If we ever debug this it also helpful.

I'm not sure what you mean, this sets all bits to 1.

Nope, it may set _more_ than all bits. That's why bitmap_set() is more accurate, because it will do exact setting.

...

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
drivers/gpio/gpio-sim.c new file +8 -0
+     if (strcmp(trimmed, "input") == 0)
+             dir = GPIOD_IN;
+     else if (strcmp(trimmed, "output-high") == 0)
+             dir = GPIOD_OUT_HIGH;
+     else if (strcmp(trimmed, "output-low") == 0)
+             dir = GPIOD_OUT_LOW;
+     else
+             dir = -EINVAL;
Quoted 4 lines
Quoted 2 lines

Same idea, i.e. static string array and use it above and here with help of match_string().

It would be great but GPIOD_IN etc. are bit flags and not sequence enums.

Ah, okay, it will make rather sparse array.

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

Re: [PATCH v12 4/7] gpio: sim: new testing module

Quoted 20 lines · Andy Shevchenko
Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko

On Fri, Dec 03, 2021 at 02:30:00PM +0100, Bartosz Golaszewski wrote:

...

Quoted 12 lines
Quoted 11 lines

drivers/gpio/gpio-sim.c line 15

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+#include <linux/gpio/driver.h>
+#include <linux/gpio/machine.h>

I would rather move this group below to emphasize that this is closer to GPIO then to other APIs.

drivers/gpio/gpio-sim.c line 31

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+#include <linux/sysfs.h>
+

...here.

Quoted 6 lines
Quoted 1 line

With the number of headers in this file, I'd stick with alphabetical order.

I understand that and agree, but my point is orthogonal to this. The idea is to emphasize that "hey. this driver has tough relations with the GPIO subsystem". This is the way, for example, IIO does and I like it.

I really don't think this is necessary.

Quoted 14 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

+#include "gpiolib.h"

...

Quoted 10 lines
Quoted 10 lines

drivers/gpio/gpio-sim.c line 61

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+static int gpio_sim_apply_pull(struct gpio_sim_chip *chip,
+                            unsigned int offset, int value)

I would use up to 100 here...

drivers/gpio/gpio-sim.c line 73

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+     if (test_bit(FLAG_REQUESTED, &desc->flags) &&
+         !test_bit(FLAG_IS_OUT, &desc->flags)) {

...here and so on.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

But it's up to you.

Nah, the lines are broken just fine. Let's not overuse the limit.

Yes, but I would consider to join back those which are up to ~83 characters (I already pointed out at least to one example like this).

I like the old-style limit TBH.

Quoted 9 lines

...

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
drivers/gpio/gpio-sim.c new file +6 -0
+     if (sysfs_streq(buf, "pull-down"))
+             pull = 0;
+     else if (sysfs_streq(buf, "pull-up"))
+             pull = 1;
+     else
+             return -EINVAL;
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

sysfs_match_string() and use the very same string array in the above function to print them?

I suppose you agree on this?

Yes, already changed that in v13.

Quoted 5 lines

...

Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+     /* Default to input mode. */
+     bitmap_fill(chip->direction_map, num_lines);
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

More accurate is to use bitmap_set(). If we ever debug this it also helpful.

I'm not sure what you mean, this sets all bits to 1.

Nope, it may set _more_ than all bits. That's why bitmap_set() is more accurate, because it will do exact setting.

Can this in any way affect any of the code? If the driver is correct, it will never use anything beyond the last line bit. If it does, it needs fixing. It's as if we cared about what happens to padding added to structures by the compiler (as long as we're not passing it to user-space of course).

Quoted 11 lines

...

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
drivers/gpio/gpio-sim.c new file +8 -0
+     if (strcmp(trimmed, "input") == 0)
+             dir = GPIOD_IN;
+     else if (strcmp(trimmed, "output-high") == 0)
+             dir = GPIOD_OUT_HIGH;
+     else if (strcmp(trimmed, "output-low") == 0)
+             dir = GPIOD_OUT_LOW;
+     else
+             dir = -EINVAL;
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Same idea, i.e. static string array and use it above and here with help of match_string().

It would be great but GPIOD_IN etc. are bit flags and not sequence enums.

Ah, okay, it will make rather sparse array.

Idea for the future: introduce match_string_ext() with flags one of which would allow sparse string arrays?

Bart

#23 lore raw

Re: [PATCH v12 4/7] gpio: sim: new testing module

Quoted 5 lines · Bartosz Golaszewski
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski

On Fri, Dec 3, 2021 at 9:08 PM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Nah, the lines are broken just fine. Let's not overuse the limit.

Yes, but I would consider to join back those which are up to ~83 characters (I already pointed out at least to one example like this).

I like the old-style limit TBH.

And it's fine. It has remark about overlapping in case of readability and 81 characters on one line is fine as some cases when it is up to ~83 (for old style).

Anyways, it doesn't worth of spending more time on this. Your choice then.

...

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines

drivers/gpio/gpio-sim.c line 396

Quoted 2 lines
drivers/gpio/gpio-sim.c new file +2 -0
+     /* Default to input mode. */
+     bitmap_fill(chip->direction_map, num_lines);

More accurate is to use bitmap_set(). If we ever debug this it also helpful.

I'm not sure what you mean, this sets all bits to 1.

Quoted 8 lines
Quoted 2 lines

Nope, it may set _more_ than all bits. That's why bitmap_set() is more accurate, because it will do exact setting.

Can this in any way affect any of the code? If the driver is correct, it will never use anything beyond the last line bit. If it does, it needs fixing. It's as if we cared about what happens to padding added to structures by the compiler (as long as we're not passing it to user-space of course).

I haven't checked if it affects current code. Consider this as heads up, because developers often forget about this nuance of bitmap_fill() / bitmap_clear().

...

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

drivers/gpio/gpio-sim.c line 1278

Quoted 8 lines
drivers/gpio/gpio-sim.c new file +8 -0
+     if (strcmp(trimmed, "input") == 0)
+             dir = GPIOD_IN;
+     else if (strcmp(trimmed, "output-high") == 0)
+             dir = GPIOD_OUT_HIGH;
+     else if (strcmp(trimmed, "output-low") == 0)
+             dir = GPIOD_OUT_LOW;
+     else
+             dir = -EINVAL;

Same idea, i.e. static string array and use it above and here with help of match_string().

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

It would be great but GPIOD_IN etc. are bit flags and not sequence enums.

Ah, okay, it will make rather sparse array.

Idea for the future: introduce match_string_ext() with flags one of which would allow sparse string arrays?

I thought about that, but since it's a mix of the bits, it might not be so universal anyway, I would rather think of something which uses 1-bit bit fields unified under a bit mask, and not a mix of 2 or more bits.

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

PATCH v12 5/7 selftests: gpio: provide a helper for reading chip info

In reply to Bartosz Golaszewski

Add a simple program that allows to retrieve chip properties from the GPIO character device. This will be used in gpio-sim selftests.

---
 tools/testing/selftests/gpio/.gitignore       |  1 +
 tools/testing/selftests/gpio/Makefile         |  2 +-
 tools/testing/selftests/gpio/gpio-chip-info.c | 57 +++++++++++++++++++
 3 files changed, 59 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/gpio/gpio-chip-info.c
tools/testing/selftests/gpio/.gitignore +1 -0
index a4969f7ee020..4ea4f58dab1a 100644
@@ -1,2 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
 gpio-mockup-cdev
+gpio-chip-info
tools/testing/selftests/gpio/Makefile +1 -1
index d7b312b44a62..a40b818c394e 100644
@@ -2,7 +2,7 @@
 
 TEST_PROGS := gpio-mockup.sh
 TEST_FILES := gpio-mockup-sysfs.sh
-TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev
+TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info
 CFLAGS += -O2 -g -Wall -I../../../../usr/include/
 
 include ../lib.mk
tools/testing/selftests/gpio/gpio-chip-info.c new file +57 -0

57 lines not on this page · raw message

Signature · 1 line
2.25.1
#25 lore raw

PATCH v12 6/7 selftests: gpio: add a helper for reading GPIO line names

In reply to Bartosz Golaszewski

Add a simple program that allows to read GPIO line names from the character device. This will be used in gpio-sim selftests.

---
 tools/testing/selftests/gpio/.gitignore       |  1 +
 tools/testing/selftests/gpio/Makefile         |  2 +-
 tools/testing/selftests/gpio/gpio-line-name.c | 55 +++++++++++++++++++
 3 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/gpio/gpio-line-name.c
tools/testing/selftests/gpio/.gitignore +1 -0
index 4ea4f58dab1a..ededb077a3a6 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 gpio-mockup-cdev
 gpio-chip-info
+gpio-line-name
tools/testing/selftests/gpio/Makefile +1 -1
index a40b818c394e..293aa9749408 100644
@@ -2,7 +2,7 @@
 
 TEST_PROGS := gpio-mockup.sh
 TEST_FILES := gpio-mockup-sysfs.sh
-TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info
+TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info gpio-line-name
 CFLAGS += -O2 -g -Wall -I../../../../usr/include/
 
 include ../lib.mk
tools/testing/selftests/gpio/gpio-line-name.c new file +55 -0

55 lines not on this page · raw message

Signature · 1 line
2.25.1
#26 lore raw

PATCH v12 7/7 selftests: gpio: add test cases for gpio-sim

In reply to Bartosz Golaszewski

Add a set of tests for the new gpio-sim module. This is a pure shell test-suite and uses the helper programs available in the gpio selftests directory. These test-cases only test the functionalities exposed by the gpio-sim driver, not those handled by core gpiolib code.

---
 tools/testing/selftests/gpio/Makefile    |   2 +-
 tools/testing/selftests/gpio/config      |   1 +
 tools/testing/selftests/gpio/gpio-sim.sh | 396 +++++++++++++++++++++++
 3 files changed, 398 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/gpio/gpio-sim.sh
tools/testing/selftests/gpio/Makefile +1 -1
index 293aa9749408..71b306602368 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 
-TEST_PROGS := gpio-mockup.sh
+TEST_PROGS := gpio-mockup.sh gpio-sim.sh
 TEST_FILES := gpio-mockup-sysfs.sh
 TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info gpio-line-name
 CFLAGS += -O2 -g -Wall -I../../../../usr/include/
tools/testing/selftests/gpio/config +1 -0
index ce100342c20b..409a8532facc 100644
@@ -1,3 +1,4 @@
 CONFIG_GPIOLIB=y
 CONFIG_GPIO_CDEV=y
 CONFIG_GPIO_MOCKUP=m
+CONFIG_GPIO_SIM=m
tools/testing/selftests/gpio/gpio-sim.sh new file +396 -0

396 lines not on this page · raw message

Signature · 1 line
2.25.1
Reviewed-by on the v12 series
#27 lore raw
In reply to Bartosz Golaszewski
Quoted 6 lines · Bartosz Golaszewski

Another iteration of gpio-sim patches. This time the changes are quite small. I removed the ifdefs from gpiolib.c as requested by Andy. In this version gpiolib-of will also prefer fwnodes over of_nodes and - if set - will convert them to of_nodes before proceeding.

Tested both with configfs as well as device-tree.

After addressing a comment in patch 3:

for patches 1-3.
Patch 4 has got full review but nothing specifically critical there.

Signature · 2 lines
With Best Regards,
Andy Shevchenko