Skip to content

Commit

Permalink
gpio: move sysfs mock device to the gpio_device
Browse files Browse the repository at this point in the history
Since gpio_device is the struct that survives if the backing
gpio_chip is removed, move the sysfs mock device to this state
container so it becomes part of the dangling state of the
GPIO device on removal.

Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
  • Loading branch information
Linus Walleij committed Feb 11, 2016
1 parent 9efd9e6 commit afbc4f3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
23 changes: 12 additions & 11 deletions drivers/gpio/gpiolib-sysfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
mutex_lock(&sysfs_lock);

/* check if chip is being removed */
if (!chip || !chip->cdev) {
if (!chip || !gdev->mockdev) {
status = -ENODEV;
goto err_unlock;
}
Expand Down Expand Up @@ -718,9 +718,10 @@ void gpiod_unexport(struct gpio_desc *desc)
}
EXPORT_SYMBOL_GPL(gpiod_unexport);

int gpiochip_sysfs_register(struct gpio_chip *chip)
int gpiochip_sysfs_register(struct gpio_device *gdev)
{
struct device *dev;
struct gpio_chip *chip = gdev->chip;

/*
* Many systems add gpio chips for SOC support very early,
Expand All @@ -732,33 +733,34 @@ int gpiochip_sysfs_register(struct gpio_chip *chip)
return 0;

/* use chip->base for the ID; it's already known to be unique */
dev = device_create_with_groups(&gpio_class, chip->parent,
dev = device_create_with_groups(&gpio_class, &gdev->dev,
MKDEV(0, 0),
chip, gpiochip_groups,
"gpiochip%d", chip->base);
if (IS_ERR(dev))
return PTR_ERR(dev);

mutex_lock(&sysfs_lock);
chip->cdev = dev;
gdev->mockdev = dev;
mutex_unlock(&sysfs_lock);

return 0;
}

void gpiochip_sysfs_unregister(struct gpio_chip *chip)
void gpiochip_sysfs_unregister(struct gpio_device *gdev)
{
struct gpio_desc *desc;
struct gpio_chip *chip = gdev->chip;
unsigned int i;

if (!chip->cdev)
if (!gdev->mockdev)
return;

device_unregister(chip->cdev);
device_unregister(gdev->mockdev);

/* prevent further gpiod exports */
mutex_lock(&sysfs_lock);
chip->cdev = NULL;
gdev->mockdev = NULL;
mutex_unlock(&sysfs_lock);

/* unregister gpiod class devices owned by sysfs */
Expand Down Expand Up @@ -787,7 +789,7 @@ static int __init gpiolib_sysfs_init(void)
*/
spin_lock_irqsave(&gpio_lock, flags);
list_for_each_entry(gdev, &gpio_devices, list) {
if (gdev->chip->cdev)
if (gdev->mockdev)
continue;

/*
Expand All @@ -800,12 +802,11 @@ static int __init gpiolib_sysfs_init(void)
* gpio_lock prevents us from doing this.
*/
spin_unlock_irqrestore(&gpio_lock, flags);
status = gpiochip_sysfs_register(gdev->chip);
status = gpiochip_sysfs_register(gdev);
spin_lock_irqsave(&gpio_lock, flags);
}
spin_unlock_irqrestore(&gpio_lock, flags);


return status;
}
postcore_initcall(gpiolib_sysfs_init);
4 changes: 2 additions & 2 deletions drivers/gpio/gpiolib.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
if (status)
goto err_remove_chardev;

status = gpiochip_sysfs_register(chip);
status = gpiochip_sysfs_register(gdev);
if (status)
goto err_remove_device;

Expand Down Expand Up @@ -615,7 +615,7 @@ void gpiochip_remove(struct gpio_chip *chip)
gdev->chip = NULL;

/* FIXME: should the legacy sysfs handling be moved to gpio_device? */
gpiochip_sysfs_unregister(chip);
gpiochip_sysfs_unregister(gdev);
gpiochip_irqchip_remove(chip);
acpi_gpiochip_remove(chip);
gpiochip_remove_pin_ranges(chip);
Expand Down
11 changes: 7 additions & 4 deletions drivers/gpio/gpiolib.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ struct acpi_device;
* @id: numerical ID number for the GPIO chip
* @dev: the GPIO device struct
* @chrdev: character device for the GPIO device
* @mockdev: class device used by the deprecated sysfs interface (may be
* NULL)
* @owner: helps prevent removal of modules exporting active GPIOs
* @chip: pointer to the corresponding gpiochip, holding static
* data for this device
Expand All @@ -41,6 +43,7 @@ struct gpio_device {
int id;
struct device dev;
struct cdev chrdev;
struct device *mockdev;
struct module *owner;
struct gpio_chip *chip;
struct list_head list;
Expand Down Expand Up @@ -190,17 +193,17 @@ static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc)

#ifdef CONFIG_GPIO_SYSFS

int gpiochip_sysfs_register(struct gpio_chip *chip);
void gpiochip_sysfs_unregister(struct gpio_chip *chip);
int gpiochip_sysfs_register(struct gpio_device *gdev);
void gpiochip_sysfs_unregister(struct gpio_device *gdev);

#else

static inline int gpiochip_sysfs_register(struct gpio_chip *chip)
static inline int gpiochip_sysfs_register(struct gpio_device *gdev)
{
return 0;
}

static inline void gpiochip_sysfs_unregister(struct gpio_chip *chip)
static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev)
{
}

Expand Down
2 changes: 0 additions & 2 deletions include/linux/gpio/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ struct gpio_device;
* @label: for diagnostics
* @gpiodev: the internal state holder, opaque struct
* @parent: optional parent device providing the GPIOs
* @cdev: class device used by sysfs interface (may be NULL)
* @owner: helps prevent removal of modules exporting active GPIOs
* @data: per-instance data assigned by the driver
* @request: optional hook for chip-specific activation, such as
Expand Down Expand Up @@ -110,7 +109,6 @@ struct gpio_chip {
const char *label;
struct gpio_device *gpiodev;
struct device *parent;
struct device *cdev;
struct module *owner;
void *data;

Expand Down

0 comments on commit afbc4f3

Please sign in to comment.