Skip to content

Commit afbc4f3

Browse files
author
Linus Walleij
committed
gpio: move sysfs mock device to the gpio_device
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 <[email protected]>
1 parent 9efd9e6 commit afbc4f3

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

drivers/gpio/gpiolib-sysfs.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
572572
mutex_lock(&sysfs_lock);
573573

574574
/* check if chip is being removed */
575-
if (!chip || !chip->cdev) {
575+
if (!chip || !gdev->mockdev) {
576576
status = -ENODEV;
577577
goto err_unlock;
578578
}
@@ -718,9 +718,10 @@ void gpiod_unexport(struct gpio_desc *desc)
718718
}
719719
EXPORT_SYMBOL_GPL(gpiod_unexport);
720720

721-
int gpiochip_sysfs_register(struct gpio_chip *chip)
721+
int gpiochip_sysfs_register(struct gpio_device *gdev)
722722
{
723723
struct device *dev;
724+
struct gpio_chip *chip = gdev->chip;
724725

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

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

742743
mutex_lock(&sysfs_lock);
743-
chip->cdev = dev;
744+
gdev->mockdev = dev;
744745
mutex_unlock(&sysfs_lock);
745746

746747
return 0;
747748
}
748749

749-
void gpiochip_sysfs_unregister(struct gpio_chip *chip)
750+
void gpiochip_sysfs_unregister(struct gpio_device *gdev)
750751
{
751752
struct gpio_desc *desc;
753+
struct gpio_chip *chip = gdev->chip;
752754
unsigned int i;
753755

754-
if (!chip->cdev)
756+
if (!gdev->mockdev)
755757
return;
756758

757-
device_unregister(chip->cdev);
759+
device_unregister(gdev->mockdev);
758760

759761
/* prevent further gpiod exports */
760762
mutex_lock(&sysfs_lock);
761-
chip->cdev = NULL;
763+
gdev->mockdev = NULL;
762764
mutex_unlock(&sysfs_lock);
763765

764766
/* unregister gpiod class devices owned by sysfs */
@@ -787,7 +789,7 @@ static int __init gpiolib_sysfs_init(void)
787789
*/
788790
spin_lock_irqsave(&gpio_lock, flags);
789791
list_for_each_entry(gdev, &gpio_devices, list) {
790-
if (gdev->chip->cdev)
792+
if (gdev->mockdev)
791793
continue;
792794

793795
/*
@@ -800,12 +802,11 @@ static int __init gpiolib_sysfs_init(void)
800802
* gpio_lock prevents us from doing this.
801803
*/
802804
spin_unlock_irqrestore(&gpio_lock, flags);
803-
status = gpiochip_sysfs_register(gdev->chip);
805+
status = gpiochip_sysfs_register(gdev);
804806
spin_lock_irqsave(&gpio_lock, flags);
805807
}
806808
spin_unlock_irqrestore(&gpio_lock, flags);
807809

808-
809810
return status;
810811
}
811812
postcore_initcall(gpiolib_sysfs_init);

drivers/gpio/gpiolib.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
558558
if (status)
559559
goto err_remove_chardev;
560560

561-
status = gpiochip_sysfs_register(chip);
561+
status = gpiochip_sysfs_register(gdev);
562562
if (status)
563563
goto err_remove_device;
564564

@@ -615,7 +615,7 @@ void gpiochip_remove(struct gpio_chip *chip)
615615
gdev->chip = NULL;
616616

617617
/* FIXME: should the legacy sysfs handling be moved to gpio_device? */
618-
gpiochip_sysfs_unregister(chip);
618+
gpiochip_sysfs_unregister(gdev);
619619
gpiochip_irqchip_remove(chip);
620620
acpi_gpiochip_remove(chip);
621621
gpiochip_remove_pin_ranges(chip);

drivers/gpio/gpiolib.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ struct acpi_device;
2727
* @id: numerical ID number for the GPIO chip
2828
* @dev: the GPIO device struct
2929
* @chrdev: character device for the GPIO device
30+
* @mockdev: class device used by the deprecated sysfs interface (may be
31+
* NULL)
3032
* @owner: helps prevent removal of modules exporting active GPIOs
3133
* @chip: pointer to the corresponding gpiochip, holding static
3234
* data for this device
@@ -41,6 +43,7 @@ struct gpio_device {
4143
int id;
4244
struct device dev;
4345
struct cdev chrdev;
46+
struct device *mockdev;
4447
struct module *owner;
4548
struct gpio_chip *chip;
4649
struct list_head list;
@@ -190,17 +193,17 @@ static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc)
190193

191194
#ifdef CONFIG_GPIO_SYSFS
192195

193-
int gpiochip_sysfs_register(struct gpio_chip *chip);
194-
void gpiochip_sysfs_unregister(struct gpio_chip *chip);
196+
int gpiochip_sysfs_register(struct gpio_device *gdev);
197+
void gpiochip_sysfs_unregister(struct gpio_device *gdev);
195198

196199
#else
197200

198-
static inline int gpiochip_sysfs_register(struct gpio_chip *chip)
201+
static inline int gpiochip_sysfs_register(struct gpio_device *gdev)
199202
{
200203
return 0;
201204
}
202205

203-
static inline void gpiochip_sysfs_unregister(struct gpio_chip *chip)
206+
static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev)
204207
{
205208
}
206209

include/linux/gpio/driver.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ struct gpio_device;
2424
* @label: for diagnostics
2525
* @gpiodev: the internal state holder, opaque struct
2626
* @parent: optional parent device providing the GPIOs
27-
* @cdev: class device used by sysfs interface (may be NULL)
2827
* @owner: helps prevent removal of modules exporting active GPIOs
2928
* @data: per-instance data assigned by the driver
3029
* @request: optional hook for chip-specific activation, such as
@@ -110,7 +109,6 @@ struct gpio_chip {
110109
const char *label;
111110
struct gpio_device *gpiodev;
112111
struct device *parent;
113-
struct device *cdev;
114112
struct module *owner;
115113
void *data;
116114

0 commit comments

Comments
 (0)