Skip to content

Commit 1c3cdb1

Browse files
committed
gpio: move descriptors into gpio_device
We need gpio_device to hold the descriptors so that they can be lifecycled with the struct gpio_device held from userspace. Move the descriptor array into gpio_device. Also rename it from "desc" (singularis) to "descs" (pluralis) to reflect the fact that it is an array. Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent afbc4f3 commit 1c3cdb1

File tree

4 files changed

+26
-35
lines changed

4 files changed

+26
-35
lines changed

drivers/gpio/gpiolib-sysfs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ void gpiochip_sysfs_unregister(struct gpio_device *gdev)
765765

766766
/* unregister gpiod class devices owned by sysfs */
767767
for (i = 0; i < chip->ngpio; i++) {
768-
desc = &chip->desc[i];
768+
desc = &chip->gpiodev->descs[i];
769769
if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
770770
gpiod_free(desc);
771771
}

drivers/gpio/gpiolib.c

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ struct gpio_desc *gpio_to_desc(unsigned gpio)
8888
if (gdev->chip->base <= gpio &&
8989
gdev->chip->base + gdev->chip->ngpio > gpio) {
9090
spin_unlock_irqrestore(&gpio_lock, flags);
91-
return &gdev->chip->desc[gpio - gdev->chip->base];
91+
return &gdev->descs[gpio - gdev->chip->base];
9292
}
9393
}
9494

@@ -110,7 +110,7 @@ struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
110110
if (hwnum >= chip->ngpio)
111111
return ERR_PTR(-EINVAL);
112112

113-
return &chip->desc[hwnum];
113+
return &chip->gpiodev->descs[hwnum];
114114
}
115115

116116
/**
@@ -120,7 +120,7 @@ struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
120120
*/
121121
int desc_to_gpio(const struct gpio_desc *desc)
122122
{
123-
return desc->chip->base + (desc - &desc->chip->desc[0]);
123+
return desc->chip->base + (desc - &desc->chip->gpiodev->descs[0]);
124124
}
125125
EXPORT_SYMBOL_GPL(desc_to_gpio);
126126

@@ -277,7 +277,7 @@ static struct gpio_desc *gpio_name_to_desc(const char * const name)
277277
int i;
278278

279279
for (i = 0; i != gdev->chip->ngpio; ++i) {
280-
struct gpio_desc *gpio = &gdev->chip->desc[i];
280+
struct gpio_desc *gpio = &gdev->descs[i];
281281

282282
if (!gpio->name || !name)
283283
continue;
@@ -320,7 +320,7 @@ static int gpiochip_set_desc_names(struct gpio_chip *gc)
320320

321321
/* Then add all names to the GPIO descriptors */
322322
for (i = 0; i != gc->ngpio; ++i)
323-
gc->desc[i].name = gc->names[i];
323+
gc->gpiodev->descs[i].name = gc->names[i];
324324

325325
return 0;
326326
}
@@ -431,7 +431,6 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
431431
int status = 0;
432432
unsigned i;
433433
int base = chip->base;
434-
struct gpio_desc *descs;
435434
struct gpio_device *gdev;
436435

437436
/*
@@ -470,9 +469,9 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
470469
else
471470
gdev->owner = THIS_MODULE;
472471

473-
/* FIXME: devm_kcalloc() these and move to gpio_device */
474-
descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
475-
if (!descs) {
472+
gdev->descs = devm_kcalloc(&gdev->dev, chip->ngpio,
473+
sizeof(gdev->descs[0]), GFP_KERNEL);
474+
if (!gdev->descs) {
476475
status = -ENOMEM;
477476
goto err_free_gdev;
478477
}
@@ -483,7 +482,7 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
483482
if (chip->ngpio == 0) {
484483
chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
485484
status = -EINVAL;
486-
goto err_free_descs;
485+
goto err_free_gdev;
487486
}
488487

489488
spin_lock_irqsave(&gpio_lock, flags);
@@ -493,19 +492,19 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
493492
if (base < 0) {
494493
status = base;
495494
spin_unlock_irqrestore(&gpio_lock, flags);
496-
goto err_free_descs;
495+
goto err_free_gdev;
497496
}
498497
chip->base = base;
499498
}
500499

501500
status = gpiodev_add_to_list(gdev);
502501
if (status) {
503502
spin_unlock_irqrestore(&gpio_lock, flags);
504-
goto err_free_descs;
503+
goto err_free_gdev;
505504
}
506505

507506
for (i = 0; i < chip->ngpio; i++) {
508-
struct gpio_desc *desc = &descs[i];
507+
struct gpio_desc *desc = &gdev->descs[i];
509508

510509
/* REVISIT: maybe a pointer to gpio_device is better */
511510
desc->chip = chip;
@@ -518,7 +517,6 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
518517
*/
519518
desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
520519
}
521-
chip->desc = descs;
522520

523521
spin_unlock_irqrestore(&gpio_lock, flags);
524522

@@ -583,9 +581,6 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
583581
spin_lock_irqsave(&gpio_lock, flags);
584582
list_del(&gdev->list);
585583
spin_unlock_irqrestore(&gpio_lock, flags);
586-
chip->desc = NULL;
587-
err_free_descs:
588-
kfree(descs);
589584
err_free_gdev:
590585
ida_simple_remove(&gpio_ida, gdev->id);
591586
kfree(gdev);
@@ -608,7 +603,7 @@ void gpiochip_remove(struct gpio_chip *chip)
608603
struct gpio_device *gdev = chip->gpiodev;
609604
struct gpio_desc *desc;
610605
unsigned long flags;
611-
unsigned id;
606+
unsigned i;
612607
bool requested = false;
613608

614609
/* Numb the device, cancelling all outstanding operations */
@@ -623,8 +618,8 @@ void gpiochip_remove(struct gpio_chip *chip)
623618
of_gpiochip_remove(chip);
624619

625620
spin_lock_irqsave(&gpio_lock, flags);
626-
for (id = 0; id < chip->ngpio; id++) {
627-
desc = &chip->desc[id];
621+
for (i = 0; i < chip->ngpio; i++) {
622+
desc = &gdev->descs[i];
628623
desc->chip = NULL;
629624
if (test_bit(FLAG_REQUESTED, &desc->flags))
630625
requested = true;
@@ -635,10 +630,6 @@ void gpiochip_remove(struct gpio_chip *chip)
635630
dev_crit(&chip->gpiodev->dev,
636631
"REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
637632

638-
/* FIXME: need to be moved to gpio_device and held there */
639-
kfree(chip->desc);
640-
chip->desc = NULL;
641-
642633
/*
643634
* The gpiochip side puts its use of the device to rest here:
644635
* if there are no userspace clients, the chardev and device will
@@ -1250,7 +1241,7 @@ const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
12501241
if (offset >= chip->ngpio)
12511242
return NULL;
12521243

1253-
desc = &chip->desc[offset];
1244+
desc = &chip->gpiodev->descs[offset];
12541245

12551246
if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
12561247
return NULL;
@@ -1837,14 +1828,14 @@ int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
18371828
if (offset >= chip->ngpio)
18381829
return -EINVAL;
18391830

1840-
if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1831+
if (test_bit(FLAG_IS_OUT, &chip->gpiodev->descs[offset].flags)) {
18411832
chip_err(chip,
18421833
"%s: tried to flag a GPIO set as output for IRQ\n",
18431834
__func__);
18441835
return -EIO;
18451836
}
18461837

1847-
set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1838+
set_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
18481839
return 0;
18491840
}
18501841
EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
@@ -1862,7 +1853,7 @@ void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
18621853
if (offset >= chip->ngpio)
18631854
return;
18641855

1865-
clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1856+
clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
18661857
}
18671858
EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
18681859

@@ -2549,8 +2540,8 @@ static void gpiochip_free_hogs(struct gpio_chip *chip)
25492540
int id;
25502541

25512542
for (id = 0; id < chip->ngpio; id++) {
2552-
if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2553-
gpiochip_free_own_desc(&chip->desc[id]);
2543+
if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
2544+
gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
25542545
}
25552546
}
25562547

@@ -2673,7 +2664,7 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
26732664
{
26742665
unsigned i;
26752666
unsigned gpio = chip->base;
2676-
struct gpio_desc *gdesc = &chip->desc[0];
2667+
struct gpio_desc *gdesc = &chip->gpiodev->descs[0];
26772668
int is_out;
26782669
int is_irq;
26792670

drivers/gpio/gpiolib.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct acpi_device;
3232
* @owner: helps prevent removal of modules exporting active GPIOs
3333
* @chip: pointer to the corresponding gpiochip, holding static
3434
* data for this device
35+
* @descs: array of ngpio descriptors.
3536
* @list: links gpio_device:s together for traversal
3637
*
3738
* This state container holds most of the runtime variable data
@@ -46,6 +47,7 @@ struct gpio_device {
4647
struct device *mockdev;
4748
struct module *owner;
4849
struct gpio_chip *chip;
50+
struct gpio_desc *descs;
4951
struct list_head list;
5052
};
5153

@@ -152,7 +154,7 @@ int gpiod_hog(struct gpio_desc *desc, const char *name,
152154
*/
153155
static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc)
154156
{
155-
return desc - &desc->chip->desc[0];
157+
return desc - &desc->chip->gpiodev->descs[0];
156158
}
157159

158160
/* With descriptor prefix */

include/linux/gpio/driver.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ struct gpio_device;
5252
* get rid of the static GPIO number space in the long run.
5353
* @ngpio: the number of GPIOs handled by this controller; the last GPIO
5454
* handled is (base + ngpio - 1).
55-
* @desc: array of ngpio descriptors. Private.
5655
* @names: if set, must be an array of strings to use as alternative
5756
* names for the GPIOs in this chip. Any entry in the array
5857
* may be NULL if there is no alias for the GPIO, however the
@@ -140,7 +139,6 @@ struct gpio_chip {
140139
struct gpio_chip *chip);
141140
int base;
142141
u16 ngpio;
143-
struct gpio_desc *desc;
144142
const char *const *names;
145143
bool can_sleep;
146144
bool irq_not_threaded;

0 commit comments

Comments
 (0)