Re: [PATCH v3 08/11] gpio: sim: new testing module

2021-03-12 Thread Andy Shevchenko
On Fri, Mar 12, 2021 at 09:54:58AM +0100, Bartosz Golaszewski wrote:
> On Wed, Mar 10, 2021 at 1:28 PM Andy Shevchenko
>  wrote:

...

> > > + ret = sprintf(page, "n/a\n");
> >
> > I dunno '/' (slash) is a good character to be handled in a shell.
> > I would prefer 'none' or 'not available' (I think space is easier,
> > because the rules to escape much simpler: need just to take it into
> > quotes, while / needs to be escaped separately).
> >
> 
> My test cases work fine with 'n/a' but I can change it to 'none' if
> it's less controversial.


% git grep -n -w '"none"' -- drivers/ arch/ | wc -l
371

% git grep -n -w '"n/a"' -- drivers/ arch/ | wc -l
15

% git grep -n -w '"not available"' -- drivers/ arch/ | wc -l
5

...

> But I would be creating empty properties for nothing. Better to just
> not have them at all.

Up to you.

-- 
With Best Regards,
Andy Shevchenko




Re: [PATCH v3 08/11] gpio: sim: new testing module

2021-03-12 Thread Bartosz Golaszewski
On Wed, Mar 10, 2021 at 1:28 PM Andy Shevchenko
 wrote:
>

[snip]

> > +
> > +static ssize_t gpio_sim_sysfs_line_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);
> > + int ret;
> > +
> > + mutex_lock(>lock);
> > + ret = sprintf(buf, "%u\n", !!test_bit(line_attr->offset, 
> > chip->values));
>
> Shouldn't we use sysfs_emit() in a new code?
>

TIL it exists. :) I'll use it.

[snip]

> > +
> > +static ssize_t gpio_sim_config_dev_name_show(struct config_item *item,
> > +  char *page)
> > +{
> > + struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item);
> > + struct platform_device *pdev;
> > + int ret;
> > +
> > + mutex_lock(>lock);
> > + pdev = config->pdev;
> > + if (pdev)
> > + ret = sprintf(page, "%s\n", dev_name(>dev));
> > + else
> > + ret = sprintf(page, "n/a\n");
>
> I dunno '/' (slash) is a good character to be handled in a shell.
> I would prefer 'none' or 'not available' (I think space is easier,
> because the rules to escape much simpler: need just to take it into
> quotes, while / needs to be escaped separately).
>

My test cases work fine with 'n/a' but I can change it to 'none' if
it's less controversial.

[snip]

>
> Also don't know what the rules about using s*printf() in the configfs.
> Maybe we have sysfs_emit() analogue or it doesn't applicable here at all.
> Greg?
>

There's no configfs_emit() or anything similar. Output for simple
attributes must simply not exceed 4096 bytes. It used to be PAGE_SIZE,
now it's defined in fs/configfs/file.c as SIMPLE_ATTR_SIZE. There's no
need to check the length of the string here though as we're only
showing what we received from the user-space anyway and configfs makes
sure we don't get more than SIMPLE_ATTR_SIZE in the store callback.

[snip]

> > +
> > +static int gpio_sim_config_commit_item(struct config_item *item)
> > +{
> > + struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item);
> > + struct property_entry properties[GPIO_SIM_MAX_PROP];
> > + struct platform_device_info pdevinfo;
> > + struct platform_device *pdev;
> > + unsigned int prop_idx = 0;
> > +
> > + memset(, 0, sizeof(pdevinfo));
> > + memset(properties, 0, sizeof(properties));
> > +
> > + mutex_lock(>lock);
> > +
> > + properties[prop_idx++] = PROPERTY_ENTRY_U32("gpio-sim,nr-gpios",
> > + config->num_lines);
>
> > + if (config->label[0] != '\0')
>
> I'm wondering if we need this check. Isn't core taking care of it?
>
> > + properties[prop_idx++] = 
> > PROPERTY_ENTRY_STRING("gpio-sim,label",
> > +config->label);
>
> > + if (config->line_names)
>
> Ditto.
>
> > + properties[prop_idx++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
> > + "gpio-line-names",
> > + config->line_names,
> > + config->num_line_names);
> > +

But I would be creating empty properties for nothing. Better to just
not have them at all.

[snip]

Bartosz


Re: [PATCH v3 08/11] gpio: sim: new testing module

2021-03-10 Thread Linus Walleij
On Tue, Mar 9, 2021 at 9:59 PM Bartosz Golaszewski  wrote:

> From: 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.
>
> Signed-off-by: Bartosz Golaszewski 

This looks really useful and helpful and clean to me!
Reviewed-by: Linus Walleij 

Yours,
Linus Walleij


Re: [PATCH v3 08/11] gpio: sim: new testing module

2021-03-10 Thread Andy Shevchenko
On Tue, Mar 09, 2021 at 09:59:18PM +0100, Bartosz Golaszewski wrote:
> From: 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.

Thanks for an update!
Looks much better now, although few minor comments below.
Reviewed-by: Andy Shevchenko 

> 
> Signed-off-by: Bartosz Golaszewski 
> ---
>  Documentation/admin-guide/gpio/gpio-sim.rst |  72 ++
>  drivers/gpio/Kconfig|   8 +
>  drivers/gpio/Makefile   |   1 +
>  drivers/gpio/gpio-sim.c | 879 
>  4 files changed, 960 insertions(+)
>  create mode 100644 Documentation/admin-guide/gpio/gpio-sim.rst
>  create mode 100644 drivers/gpio/gpio-sim.c
> 
> diff --git a/Documentation/admin-guide/gpio/gpio-sim.rst 
> b/Documentation/admin-guide/gpio/gpio-sim.rst
> new file mode 100644
> index ..08eac487e35e
> --- /dev/null
> +++ b/Documentation/admin-guide/gpio/gpio-sim.rst
> @@ -0,0 +1,72 @@
> +.. 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'. It's a
> +subsystem with committable items which means two subdirectories are created 
> in
> +the filesystem: pending and live. For more information on configfs and
> +committable items, please refer to Documentation/filesystems/configfs.rst.
> +
> +In order to instantiate a new simulated chip, the user needs to mkdir() a new
> +directory in pending/. Inside each new directory, there's a set of attributes
> +that can be used to configure the new chip. Once the configuration is 
> complete,
> +the user needs to use rename() to move the chip to the live/ directory. This
> +creates and registers the new device.
> +
> +In order to destroy a simulated chip, it has to be moved back to pending 
> first
> +and then removed using rmdir().
> +
> +Currently supported configuration attributes are:
> +
> +  num_lines - an unsigned integer value defining the number of GPIO lines to
> +  export
> +
> +  label - a string defining the label for the GPIO chip
> +
> +  line_names - a list of GPIO line names in the form of quoted strings
> +   separated by commas, e.g.: '"foo", "bar", "", "foobar"'. The
> +   number of strings doesn't have to be equal to the value set in
> +   the num_lines attribute. If it's lower than the number of 
> lines,
> +   the remaining lines are unnamed. If it's larger, the 
> superfluous
> +   lines are ignored. A name of the form: '""' means the line
> +   should be unnamed.
> +
> +Additionally two read-only attributes named 'chip_name' and 'dev_name' are
> +exposed in order to provide users with a mapping from configfs directories to
> +the actual devices created in the kernel. The former returns the name of the
> +GPIO device as assigned by gpiolib (i.e. "gpiochip0", "gpiochip1", etc.). The
> +latter returns the parent device name as defined by the gpio-sim driver (i.e.
> +"gpio-sim.0", "gpio-sim.1", etc.). This allows user-space to map the configfs
> +items both to the correct character device file as well as the associated 
> entry
> +in sysfs.
> +
> +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
> +
> +  "gpio-sim,nr-gpios" - number of lines
> +
> +Other standard GPIO properties (like "gpio-line-names" and gpio-hog) are also
> +supported.
> +
> +Manipulating simulated lines
> +
> +
> +Each simulated GPIO chip creates a sysfs attribute group under its device
> +directory called 'line-ctrl'. Inside each group, there's a separate attribute
> +for each GPIO line. The name of the attribute is of the form 'gpioX' where X
> +is the line's offset in the chip.
> +
> +Reading from a line attribute returns the current value. Writing to it (0 or 
> 1)
> +changes the configuration of the simulated pull-up/pull-down resistor
> +(1 - pull-up, 0 - pull-down).
> diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
> index e3607ec4c2e8..b6b6150d0d04 100644
> --- a/drivers/gpio/Kconfig
> +++ b/drivers/gpio/Kconfig
> @@ -1633,6 +1633,14 @@ config GPIO_MOCKUP
> tools/testing/selftests/gpio/gpio-mockup.sh. Reference the usage in
> it.
>  
> +config GPIO_SIM
> +