History log of /linux-master/drivers/gpio/gpio-sim.c
Revision Date Author Comments
# ebb03f69 22-Feb-2024 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: use for_each_hwgpio()

Display debugfs information about all simulated GPIOs, not only the
requested ones.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>


# 840a97e2 20-Feb-2024 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: delimit the fwnode name with a ":" when generating labels

Typically, whenever a human-readable name is created for objects using
a software node, its name is delimited with ":" as dashes are often used
in other parts of the name. Make gpio-sim use the same pattern. This
results in better looking default names:

gpio-sim.0:node0
gpio-sim.0:node1
gpio-sim.1:node0

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Acked-by: Linus Walleij <linus.walleij@linaro.org>


# 11498d99 14-Feb-2024 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: add lockdep asserts

We have three functions in gpio-sim that are called with the device lock
already held. We use the "_unlocked" suffix in their names to indicate
that. This has proven to be confusing though as the naming convention in
the kernel varies between using "_locked" or "_unlocked" for this
purpose. Naming convention also doesn't enforce anything. Let's remove
the suffix and add lockdep annotation at the top of these functions.

This makes it clear the function requires a lock to be held (and which
one specifically!) as well as results in a warning if it's not the case.
The only place where the information is lost is the place where the
function is called but the caller doesn't care about that information
anyway.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>


# d22f93c6 04-Dec-2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: implement the dbg_show() callback

Provide a custom implementation of the dbg_show() callback that prints
all requested lines together with their label, direction, value and
bias. This improves the code coverage of GPIOLIB.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>


# c598dc3b 05-Dec-2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: fix the email address in MODULE_AUTHOR()

Fix unterminated angle brackets in the email address in MODULE_AUTHOR().

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>


# d9d5829d 28-Sep-2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: add missing include

We use size_t, ssize_t, bool and some other types defined in
linux/types.h so include it in the driver.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>


# 9f93f183 17-Sep-2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: initialize a managed pointer when declaring it

Variables managed with __free() should typically be initialized where
they are declared so that the __free() callback is paired with its
counterpart resource allocator. Fix the second instance of using
__free() in gpio-sim to follow this pattern.

Fixes: 3faf89f27aab ("gpio: sim: simplify code with cleanup helpers")
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>


# db8588f9 15-Sep-2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: include a missing header

We're using various ERR macros from linux/err.h but the include is
missing. Add it.

Fixes: cb8c474e79be ("gpio: sim: new testing module")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>


# 88d31f83 11-Sep-2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: don't fiddle with GPIOLIB private members

We access internals of struct gpio_device and struct gpio_desc because
it's easier but it can actually be avoided and we're working towards a
better encapsulation of GPIO data structures across the kernel so let's
start at home.

Instead of checking gpio_desc flags, let's just track the requests of
GPIOs in the driver. We also already store the information about
direction of simulated lines.

For kobjects needed by sysfs callbacks: we can iterate over the children
devices of the top-level platform device and compare their fwnodes
against the one passed to the init function from probe.

While at it: fix one line break and remove the untrue part about
configfs callbacks using dev_get_drvdata() from a comment.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>


# 5cb9606a 20-Sep-2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: fix an invalid __free() usage

gpio_sim_make_line_names() returns NULL or ERR_PTR() so we must not use
__free(kfree) on the returned address. Split this function into two, one
that determines the size of the "gpio-line-names" array to allocate and
one that actually sets the names at correct offsets. The allocation and
assignment of the managed pointer happens in between.

Fixes: 3faf89f27aab ("gpio: sim: simplify code with cleanup helpers")
Reported-by: Alexey Dobriyan <adobriyan@gmail.com>
Closes: https://lore.kernel.org/all/07c32bf1-6c1a-49d9-b97d-f0ae4a2b42ab@p183/
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>


# 3faf89f2 12-Aug-2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: simplify code with cleanup helpers

Use macros defined in linux/cleanup.h to automate resource lifetime
control in gpio-sim.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Acked-by: Linus Walleij <linus.walleij@linaro.org>


# ba0294df 12-Aug-2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: replace memmove() + strstrip() with skip_spaces() + strim()

Turns out we can avoid the memmove() by using skip_spaces() and strim().
We did that in gpio-consumer, let's do it in gpio-sim.

Suggested-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>


# a40fe1ff 11-Aug-2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: simplify gpio_sim_device_config_live_store()

Simplify the logic when checking the current live value against the user
input.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>


# 39df52dd 09-Aug-2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: use sysfs_streq() and avoid an strdup()

When comparing strings passed to us from configfs, we can pass the page
argument directly to sysfs_streq() and avoid manual string trimming.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>


# 43818a4b 11-Jul-2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: fix a typo in comment

It was supposed to say 'for' not 'fo'.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>


# 6e39c1ac 22-Aug-2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: pass the GPIO device's software node to irq domain

Associate the swnode of the GPIO device's (which is the interrupt
controller here) with the irq domain. Otherwise the interrupt-controller
device attribute is a no-op.

Fixes: cb8c474e79be ("gpio: sim: new testing module")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>


# ab4109f9 22-Aug-2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: dispose of irq mappings before destroying the irq_sim domain

If a GPIO simulator device is unbound with interrupts still requested,
we will hit a use-after-free issue in __irq_domain_deactivate_irq(). The
owner of the irq domain must dispose of all mappings before destroying
the domain object.

Fixes: cb8c474e79be ("gpio: sim: new testing module")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>


# 5a78d5db 01-Aug-2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: mark the GPIO chip as a one that can sleep

Simulated chips use a mutex for synchronization in driver callbacks so
they must not be called from interrupt context. Set the can_sleep field
of the GPIO chip to true to force users to only use threaded irqs.

Fixes: cb8c474e79be ("gpio: sim: new testing module")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>


# d7459efc 07-Jun-2023 Kent Gibson <warthog618@gmail.com>

gpio: sim: quietly ignore configured lines outside the bank

The user-space policy of the gpio-sim is that configuration for lines
with offsets outside the bounds of the corresponding bank is ignored,
but gpio-sim is still using that configuration when constructing the
sim. In the case of named lines this results in temporarily allocating
space for names that are not used, and for hogs results in errors being
logged when the gpio-sim attempts to register the out of range hog with
gpiolib:

gpiochip_machine_hog: unable to get GPIO desc: -22

Add checks to filter out any line configuration outside the bounds
of the bank when constructing the sim.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>


# 95ae9979 06-Jun-2023 Kent Gibson <warthog618@gmail.com>

gpio: sim: fix memory corruption when adding named lines and unnamed hogs

When constructing the sim, gpio-sim constructs an array of named lines,
sized based on the largest offset of any named line, and then initializes
that array with the names of all lines, including unnamed hogs with higher
offsets. In doing so it writes NULLs beyond the extent of the array.

Add a check that only named lines are used to initialize the array.

Fixes: cb8c474e79be ("gpio: sim: new testing module")
Signed-off-by: Kent Gibson<warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>


# 0c14f3aa 09-Mar-2023 Andy Shevchenko <andriy.shevchenko@linux.intel.com>

gpio: sim: Deactivate device in reversed order

Run the steps to deactivate device in the reserved order to what
it has been done in gpio_sim_device_activate_unlocked().

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>


# 4827aae0 12-Feb-2023 Andy Shevchenko <andriy.shevchenko@linux.intel.com>

gpio: sim: Use %pfwP specifier instead of calling fwnode API directly

Instead of calling fwnode_get_name() and supply as %s, use %pfwP which
will do the same inside printf() call.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>


# 79eeab1d 16-Feb-2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: fix a memory leak

Fix an inverted logic bug in gpio_sim_remove_hogs() that leads to GPIO
hog structures never being freed.

Fixes: cb8c474e79be ("gpio: sim: new testing module")
Reported-by: Mirsad Goran Todorovac <mirsad.todorovac@alu.unizg.hr>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>


# 11e47bbd 12-Dec-2022 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

gpio: sim: set a limit on the number of GPIOs

With the removal of ARCH_NR_GPIOS in commit 7b61212f2a07 ("gpiolib: Get
rid of ARCH_NR_GPIOS") the gpiolib core no longer sanitizes the number
of GPIOs for us. This causes the gpio-sim selftests to now fail when
setting the number of GPIOs to 99999 and expecting the probe() to fail.

Set a sane limit of 1024 on the number of simulated GPIOs and bail out
of probe if it's exceeded.

Reported-by: kernel test robot <oliver.sang@intel.com>
Link: https://lore.kernel.org/oe-lkp/202212112236.756f5db9-oliver.sang@intel.com
Fixes: 7b61212f2a07 ("gpiolib: Get rid of ARCH_NR_GPIOS")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>


# 7329b071 12-Jul-2022 Bartosz Golaszewski <brgl@bgdev.pl>

gpio: sim: fix the chip_name configfs item

The chip_name configs attribute always displays the device name of the
first GPIO bank because the logic of the relevant function is simply
wrong.

Fix it by correctly comparing the bank's swnode against the GPIO
device's children.

Fixes: cb8c474e79be ("gpio: sim: new testing module")
Cc: stable@vger.kernel.org
Reported-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Reviewed-by: Kent Gibson <warthog618@gmail.com>
Tested-by: Kent Gibson <warthog618@gmail.com>


# c680c6a8 20-May-2022 Christophe JAILLET <christophe.jaillet@wanadoo.fr>

gpio: sim: Use correct order for the parameters of devm_kcalloc()

We should have 'n', then 'size', not the opposite.
This is harmless because the 2 values are just multiplied, but having
the correct order silence a (unpublished yet) smatch warning.

Fixes: cb8c474e79be ("gpio: sim: new testing module")
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>


# 3836c73e 13-Apr-2022 Bartosz Golaszewski <brgl@bgdev.pl>

gpio: sim: fix setting and getting multiple lines

We need to take mask into account in the set/get_multiple() callbacks.
Use bitmap_replace() instead of bitmap_copy().

Fixes: cb8c474e79be ("gpio: sim: new testing module")
Cc: stable@vger.kernel.org
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>


# 55d01c98 08-Mar-2022 Bartosz Golaszewski <brgl@bgdev.pl>

gpio: sim: fix a typo

Just noticed this when applying Andy's patch. s/childred/children/

Fixes: cb8c474e79be ("gpio: sim: new testing module")
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>


# a9a5b720 07-Mar-2022 Andy Shevchenko <andriy.shevchenko@linux.intel.com>

gpio: sim: Declare gpio_sim_hog_config_item_ops static

Compiler is not happy:

warning: symbol 'gpio_sim_hog_config_item_ops' was not declared. Should it be static?

Fixes: cb8c474e79be ("gpio: sim: new testing module")
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>


# c162ca0b 08-Feb-2022 Bartosz Golaszewski <brgl@bgdev.pl>

gpio: sim: fix hogs with custom chip labels

We always assign the default device name as the chip_label in hog
structures which makes it impossible to assign hogs to chips. Let's
first check if a custom label was set and then copy it instead of the
default device name.

Fixes: cb8c474e79be ("gpio: sim: new testing module")
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>


# dda8e143 20-Jan-2022 Bartosz Golaszewski <brgl@bgdev.pl>

gpio: sim: check the label length when setting up device properties

If the user-space sets the chip label to an empty string - we should
check the length and not override the default name or else line hogs
will not be properly attached.

Fixes: cb8c474e79be ("gpio: sim: new testing module")
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>


# a2d05fb7 21-Dec-2021 Yang Yingliang <yangyingliang@huawei.com>

gpio: sim: add missing fwnode_handle_put() in gpio_sim_probe()

Calling fwnode_handle_put() when break out of device_for_each_child_node(),
or the device node reference will be leakd.

Fixes: 83960fcf4818 ("gpio: sim: new testing module")
Reported-by: Hulk Robot <hulkci@huawei.com>
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>


# c08995bf 18-Dec-2021 Tom Rix <trix@redhat.com>

gpio: sim: fix uninitialized ret variable

Building with clang returns this error:

gpio-sim.c:889:7: error: variable 'ret' is uninitialized
when used here

ret should be the status of the call to
gpio_sim_make_bank_swnode stored in bank->swnode.

Fixes: 83960fcf4818 ("gpio: sim: new testing module")
Signed-off-by: Tom Rix <trix@redhat.com>
Reviewed-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>


# cb8c474e 07-Dec-2021 Bartosz Golaszewski <brgl@bgdev.pl>

gpio: sim: new testing module

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 <brgl@bgdev.pl>
Acked-by: Linus Walleij <linus.walleij@linaro.org>