Skip to content

Commit 93548f8

Browse files
author
Bartosz Golaszewski
committed
gpiolib: provide gpio_device_get_desc()
Getting the GPIO descriptor directly from the gpio_chip struct is dangerous as we don't take the reference to the underlying GPIO device. In order to start working towards removing gpiochip_get_desc(), let's provide a safer variant that works with an existing reference to struct gpio_device. Signed-off-by: Bartosz Golaszewski <[email protected]> Reviewed-by: Linus Walleij <[email protected]>
1 parent d62fcd9 commit 93548f8

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

drivers/gpio/gpiolib.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,27 +147,49 @@ struct gpio_desc *gpio_to_desc(unsigned gpio)
147147
}
148148
EXPORT_SYMBOL_GPL(gpio_to_desc);
149149

150+
/* This function is deprecated and will be removed soon, don't use. */
151+
struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc,
152+
unsigned int hwnum)
153+
{
154+
return gpio_device_get_desc(gc->gpiodev, hwnum);
155+
}
156+
EXPORT_SYMBOL_GPL(gpiochip_get_desc);
157+
150158
/**
151-
* gpiochip_get_desc - get the GPIO descriptor corresponding to the given
152-
* hardware number for this chip
153-
* @gc: GPIO chip
159+
* gpio_device_get_desc() - get the GPIO descriptor corresponding to the given
160+
* hardware number for this GPIO device
161+
* @gdev: GPIO device to get the descriptor from
154162
* @hwnum: hardware number of the GPIO for this chip
155163
*
156164
* Returns:
157-
* A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
158-
* in the given chip for the specified hardware number.
165+
* A pointer to the GPIO descriptor or %EINVAL if no GPIO exists in the given
166+
* chip for the specified hardware number or %ENODEV if the underlying chip
167+
* already vanished.
168+
*
169+
* The reference count of struct gpio_device is *NOT* increased like when the
170+
* GPIO is being requested for exclusive usage. It's up to the caller to make
171+
* sure the GPIO device will stay alive together with the descriptor returned
172+
* by this function.
159173
*/
160-
struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc,
161-
unsigned int hwnum)
174+
struct gpio_desc *
175+
gpio_device_get_desc(struct gpio_device *gdev, unsigned int hwnum)
162176
{
163-
struct gpio_device *gdev = gc->gpiodev;
177+
struct gpio_chip *gc;
178+
179+
/*
180+
* FIXME: This will be locked once we protect gdev->chip everywhere
181+
* with SRCU.
182+
*/
183+
gc = gdev->chip;
184+
if (!gc)
185+
return ERR_PTR(-ENODEV);
164186

165187
if (hwnum >= gdev->ngpio)
166188
return ERR_PTR(-EINVAL);
167189

168190
return &gdev->descs[hwnum];
169191
}
170-
EXPORT_SYMBOL_GPL(gpiochip_get_desc);
192+
EXPORT_SYMBOL_GPL(gpio_device_get_desc);
171193

172194
/**
173195
* desc_to_gpio - convert a GPIO descriptor to the integer namespace

include/linux/gpio/driver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,8 @@ struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
770770
void gpiochip_free_own_desc(struct gpio_desc *desc);
771771

772772
struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum);
773+
struct gpio_desc *
774+
gpio_device_get_desc(struct gpio_device *gdev, unsigned int hwnum);
773775

774776
#ifdef CONFIG_GPIOLIB
775777

0 commit comments

Comments
 (0)