2023-10-11 13:02:29

by Bartosz Golaszewski

[permalink] [raw]
Subject: [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device()

From: Bartosz Golaszewski <[email protected]>

Accessing struct gpio_chip backing a GPIO device is only allowed for the
actual providers of that chip.

Similarly to how we introduced gpio_device_find() in order to replace
the abused gpiochip_find(), let's introduce a counterpart to
gpiod_to_chip() that returns a reference to the GPIO device owning the
descriptor. This is done in order to later remove gpiod_to_chip()
entirely.

Signed-off-by: Bartosz Golaszewski <[email protected]>
---
drivers/gpio/gpiolib.c | 21 +++++++++++++++++++++
include/linux/gpio/driver.h | 1 +
2 files changed, 22 insertions(+)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index ca2b5b424284..1e0ed6f5bcd5 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -220,6 +220,27 @@ struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
}
EXPORT_SYMBOL_GPL(gpiod_to_chip);

+/**
+ * gpiod_to_gpio_device() - Return the GPIO device to which this descriptor
+ * belongs.
+ * @desc: Descriptor for which to return the GPIO device.
+ *
+ * This *DOES NOT* increase the reference count of the GPIO device as it's
+ * expected that the descriptor is requested and the users already holds a
+ * reference to the device.
+ *
+ * Returns:
+ * Address of the GPIO device owning this descriptor.
+ */
+struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc)
+{
+ if (!desc)
+ return NULL;
+
+ return desc->gdev;
+}
+EXPORT_SYMBOL_GPL(gpiod_to_gpio_device);
+
/**
* gpio_device_get_chip() - Get the gpio_chip implementation of this GPIO device
* @gdev: GPIO device
diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h
index 0484bf90b25d..7a8725be1225 100644
--- a/include/linux/gpio/driver.h
+++ b/include/linux/gpio/driver.h
@@ -784,6 +784,7 @@ int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset);
void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset);

struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
+struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc);

#else /* CONFIG_GPIOLIB */

--
2.39.2


2023-10-11 14:58:48

by Peter Rosin

[permalink] [raw]
Subject: Re: [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device()

Hi!

2023-10-11 at 15:02, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <[email protected]>
>
> Accessing struct gpio_chip backing a GPIO device is only allowed for the
> actual providers of that chip.
>
> Similarly to how we introduced gpio_device_find() in order to replace
> the abused gpiochip_find(), let's introduce a counterpart to
> gpiod_to_chip() that returns a reference to the GPIO device owning the
> descriptor. This is done in order to later remove gpiod_to_chip()
> entirely.
>
> Signed-off-by: Bartosz Golaszewski <[email protected]>

Reviewed-by: Peter Rosin <[email protected]>

Cheers,
Peter

2023-10-11 15:23:50

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device()

On Wed, Oct 11, 2023 at 4:02 PM Bartosz Golaszewski <[email protected]> wrote:
>
> From: Bartosz Golaszewski <[email protected]>
>
> Accessing struct gpio_chip backing a GPIO device is only allowed for the
> actual providers of that chip.
>
> Similarly to how we introduced gpio_device_find() in order to replace
> the abused gpiochip_find(), let's introduce a counterpart to
> gpiod_to_chip() that returns a reference to the GPIO device owning the
> descriptor. This is done in order to later remove gpiod_to_chip()
> entirely.

My concern with this API is the following scenario:
1. One driver requests the GPIO descriptor.
2. Another driver does take an arbitrary number, converts to a
descriptor and calls for this API.

Is there any (potential) problem?

--
With Best Regards,
Andy Shevchenko

2023-10-11 15:29:47

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device()

On Wed, Oct 11, 2023 at 4:02 PM Bartosz Golaszewski <[email protected]> wrote:

...

> +struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc)
> +{
> + if (!desc)
> + return NULL;
> +
> + return desc->gdev;

Wondering if we may use (part of) the validate_desc() (in a form of
VALIDATE_DESC_VOID() macro call).

> +}

--
With Best Regards,
Andy Shevchenko

2023-10-11 15:39:54

by Bartosz Golaszewski

[permalink] [raw]
Subject: Re: [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device()

On Wed, 11 Oct 2023 at 17:23, Andy Shevchenko <[email protected]> wrote:
>
> On Wed, Oct 11, 2023 at 4:02 PM Bartosz Golaszewski <[email protected]> wrote:
> >
> > From: Bartosz Golaszewski <[email protected]>
> >
> > Accessing struct gpio_chip backing a GPIO device is only allowed for the
> > actual providers of that chip.
> >
> > Similarly to how we introduced gpio_device_find() in order to replace
> > the abused gpiochip_find(), let's introduce a counterpart to
> > gpiod_to_chip() that returns a reference to the GPIO device owning the
> > descriptor. This is done in order to later remove gpiod_to_chip()
> > entirely.
>
> My concern with this API is the following scenario:
> 1. One driver requests the GPIO descriptor.
> 2. Another driver does take an arbitrary number, converts to a
> descriptor and calls for this API.
>
> Is there any (potential) problem?

YES! And I have it already on my TODO list! But it's great to know I'm
not the only one seeing it.

Basically we need to

The end-goal should be to make gpio_to_desc() an internal GPIOLIB
symbol. There are still around 10 users outside drivers/gpio/ that
will need to be addressed in one way or another. Preferably by being
converted to using descriptors.

Bart

>
> --
> With Best Regards,
> Andy Shevchenko

2023-10-12 06:58:16

by Linus Walleij

[permalink] [raw]
Subject: Re: [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device()

On Wed, Oct 11, 2023 at 5:39 PM Bartosz Golaszewski
<[email protected]> wrote:

> The end-goal should be to make gpio_to_desc() an internal GPIOLIB
> symbol. There are still around 10 users outside drivers/gpio/ that
> will need to be addressed in one way or another. Preferably by being
> converted to using descriptors.

Conversely desc_to_gpio() as well, here is one fix for the current
merge window (yeah I keep churning away at this...)
https://lore.kernel.org/alsa-devel/[email protected]/

Yours,
Linus Walleij