diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2015-01-18 12:03:13 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2015-01-18 12:03:13 -0500 |
commit | b62a9c2058ee4c60677422fb7a8d6129320a4251 (patch) | |
tree | cb3d3f19f6a4d7d7664be4437ebf6876f44f5bfe | |
parent | 66893885bbf95b6c9030d97804cb678a70804edf (diff) | |
parent | 7b8792bbdffdff3abda704f89c6a45ea97afdc62 (diff) |
Merge tag 'gpio-v3.19-4' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio
Pull GPIO fixes from Linus Walleij:
"Here is a set of fixes that mainly appeared when Johan Hovold started
exercising the removal path of the GPIO library, dealing with
hotplugging of GPIO controllers. Details from tag:
A slew of fixes dealing with some irritating bugs (non-regressions)
that have been around forever in the GPIO subsystem, most of them also
tagged for stable:
- A large slew of fixes from Johan Hovold who is finally testing and
reviewing the removal path of the GPIO drivers.
- Fix of_get_named_gpiod_flags() so it works as expected.
- Fix an IRQ handling bug in the crystalcove driver"
* tag 'gpio-v3.19-4' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio:
gpiolib: of: Correct error handling in of_get_named_gpiod_flags
gpio: sysfs: fix gpio attribute-creation race
gpio: sysfs: fix gpio device-attribute leak
gpio: sysfs: fix gpio-chip device-attribute leak
gpio: unregister gpiochip device before removing it
gpio: fix sleep-while-atomic in gpiochip_remove
gpio: fix memory leak and sleep-while-atomic
gpio: clean up gpiochip_add error handling
gpio: fix gpio-chip list corruption
gpio: fix memory and reference leaks in gpiochip_add error path
gpio: crystalcove: use handle_nested_irq
-rw-r--r-- | drivers/gpio/gpio-crystalcove.c | 2 | ||||
-rw-r--r-- | drivers/gpio/gpiolib-of.c | 10 | ||||
-rw-r--r-- | drivers/gpio/gpiolib-sysfs.c | 92 | ||||
-rw-r--r-- | drivers/gpio/gpiolib.c | 58 | ||||
-rw-r--r-- | drivers/gpio/gpiolib.h | 1 |
5 files changed, 92 insertions, 71 deletions
diff --git a/drivers/gpio/gpio-crystalcove.c b/drivers/gpio/gpio-crystalcove.c index 55d4803d71b0..3d9e08f7e823 100644 --- a/drivers/gpio/gpio-crystalcove.c +++ b/drivers/gpio/gpio-crystalcove.c | |||
@@ -272,7 +272,7 @@ static irqreturn_t crystalcove_gpio_irq_handler(int irq, void *data) | |||
272 | for (gpio = 0; gpio < CRYSTALCOVE_GPIO_NUM; gpio++) { | 272 | for (gpio = 0; gpio < CRYSTALCOVE_GPIO_NUM; gpio++) { |
273 | if (pending & BIT(gpio)) { | 273 | if (pending & BIT(gpio)) { |
274 | virq = irq_find_mapping(cg->chip.irqdomain, gpio); | 274 | virq = irq_find_mapping(cg->chip.irqdomain, gpio); |
275 | generic_handle_irq(virq); | 275 | handle_nested_irq(virq); |
276 | } | 276 | } |
277 | } | 277 | } |
278 | 278 | ||
diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c index 604dbe60bdee..08261f2b3a82 100644 --- a/drivers/gpio/gpiolib-of.c +++ b/drivers/gpio/gpiolib-of.c | |||
@@ -45,8 +45,14 @@ static int of_gpiochip_find_and_xlate(struct gpio_chip *gc, void *data) | |||
45 | return false; | 45 | return false; |
46 | 46 | ||
47 | ret = gc->of_xlate(gc, &gg_data->gpiospec, gg_data->flags); | 47 | ret = gc->of_xlate(gc, &gg_data->gpiospec, gg_data->flags); |
48 | if (ret < 0) | 48 | if (ret < 0) { |
49 | return false; | 49 | /* We've found the gpio chip, but the translation failed. |
50 | * Return true to stop looking and return the translation | ||
51 | * error via out_gpio | ||
52 | */ | ||
53 | gg_data->out_gpio = ERR_PTR(ret); | ||
54 | return true; | ||
55 | } | ||
50 | 56 | ||
51 | gg_data->out_gpio = gpiochip_get_desc(gc, ret); | 57 | gg_data->out_gpio = gpiochip_get_desc(gc, ret); |
52 | return true; | 58 | return true; |
diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c index 2ac1800b58bb..f62aa115d79a 100644 --- a/drivers/gpio/gpiolib-sysfs.c +++ b/drivers/gpio/gpiolib-sysfs.c | |||
@@ -128,7 +128,7 @@ static ssize_t gpio_value_store(struct device *dev, | |||
128 | return status; | 128 | return status; |
129 | } | 129 | } |
130 | 130 | ||
131 | static const DEVICE_ATTR(value, 0644, | 131 | static DEVICE_ATTR(value, 0644, |
132 | gpio_value_show, gpio_value_store); | 132 | gpio_value_show, gpio_value_store); |
133 | 133 | ||
134 | static irqreturn_t gpio_sysfs_irq(int irq, void *priv) | 134 | static irqreturn_t gpio_sysfs_irq(int irq, void *priv) |
@@ -353,17 +353,46 @@ static ssize_t gpio_active_low_store(struct device *dev, | |||
353 | return status ? : size; | 353 | return status ? : size; |
354 | } | 354 | } |
355 | 355 | ||
356 | static const DEVICE_ATTR(active_low, 0644, | 356 | static DEVICE_ATTR(active_low, 0644, |
357 | gpio_active_low_show, gpio_active_low_store); | 357 | gpio_active_low_show, gpio_active_low_store); |
358 | 358 | ||
359 | static const struct attribute *gpio_attrs[] = { | 359 | static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr, |
360 | int n) | ||
361 | { | ||
362 | struct device *dev = container_of(kobj, struct device, kobj); | ||
363 | struct gpio_desc *desc = dev_get_drvdata(dev); | ||
364 | umode_t mode = attr->mode; | ||
365 | bool show_direction = test_bit(FLAG_SYSFS_DIR, &desc->flags); | ||
366 | |||
367 | if (attr == &dev_attr_direction.attr) { | ||
368 | if (!show_direction) | ||
369 | mode = 0; | ||
370 | } else if (attr == &dev_attr_edge.attr) { | ||
371 | if (gpiod_to_irq(desc) < 0) | ||
372 | mode = 0; | ||
373 | if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags)) | ||
374 | mode = 0; | ||
375 | } | ||
376 | |||
377 | return mode; | ||
378 | } | ||
379 | |||
380 | static struct attribute *gpio_attrs[] = { | ||
381 | &dev_attr_direction.attr, | ||
382 | &dev_attr_edge.attr, | ||
360 | &dev_attr_value.attr, | 383 | &dev_attr_value.attr, |
361 | &dev_attr_active_low.attr, | 384 | &dev_attr_active_low.attr, |
362 | NULL, | 385 | NULL, |
363 | }; | 386 | }; |
364 | 387 | ||
365 | static const struct attribute_group gpio_attr_group = { | 388 | static const struct attribute_group gpio_group = { |
366 | .attrs = (struct attribute **) gpio_attrs, | 389 | .attrs = gpio_attrs, |
390 | .is_visible = gpio_is_visible, | ||
391 | }; | ||
392 | |||
393 | static const struct attribute_group *gpio_groups[] = { | ||
394 | &gpio_group, | ||
395 | NULL | ||
367 | }; | 396 | }; |
368 | 397 | ||
369 | /* | 398 | /* |
@@ -400,16 +429,13 @@ static ssize_t chip_ngpio_show(struct device *dev, | |||
400 | } | 429 | } |
401 | static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); | 430 | static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); |
402 | 431 | ||
403 | static const struct attribute *gpiochip_attrs[] = { | 432 | static struct attribute *gpiochip_attrs[] = { |
404 | &dev_attr_base.attr, | 433 | &dev_attr_base.attr, |
405 | &dev_attr_label.attr, | 434 | &dev_attr_label.attr, |
406 | &dev_attr_ngpio.attr, | 435 | &dev_attr_ngpio.attr, |
407 | NULL, | 436 | NULL, |
408 | }; | 437 | }; |
409 | 438 | ATTRIBUTE_GROUPS(gpiochip); | |
410 | static const struct attribute_group gpiochip_attr_group = { | ||
411 | .attrs = (struct attribute **) gpiochip_attrs, | ||
412 | }; | ||
413 | 439 | ||
414 | /* | 440 | /* |
415 | * /sys/class/gpio/export ... write-only | 441 | * /sys/class/gpio/export ... write-only |
@@ -556,45 +582,30 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change) | |||
556 | goto fail_unlock; | 582 | goto fail_unlock; |
557 | } | 583 | } |
558 | 584 | ||
559 | if (!desc->chip->direction_input || !desc->chip->direction_output) | 585 | if (desc->chip->direction_input && desc->chip->direction_output && |
560 | direction_may_change = false; | 586 | direction_may_change) { |
587 | set_bit(FLAG_SYSFS_DIR, &desc->flags); | ||
588 | } | ||
589 | |||
561 | spin_unlock_irqrestore(&gpio_lock, flags); | 590 | spin_unlock_irqrestore(&gpio_lock, flags); |
562 | 591 | ||
563 | offset = gpio_chip_hwgpio(desc); | 592 | offset = gpio_chip_hwgpio(desc); |
564 | if (desc->chip->names && desc->chip->names[offset]) | 593 | if (desc->chip->names && desc->chip->names[offset]) |
565 | ioname = desc->chip->names[offset]; | 594 | ioname = desc->chip->names[offset]; |
566 | 595 | ||
567 | dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), | 596 | dev = device_create_with_groups(&gpio_class, desc->chip->dev, |
568 | desc, ioname ? ioname : "gpio%u", | 597 | MKDEV(0, 0), desc, gpio_groups, |
569 | desc_to_gpio(desc)); | 598 | ioname ? ioname : "gpio%u", |
599 | desc_to_gpio(desc)); | ||
570 | if (IS_ERR(dev)) { | 600 | if (IS_ERR(dev)) { |
571 | status = PTR_ERR(dev); | 601 | status = PTR_ERR(dev); |
572 | goto fail_unlock; | 602 | goto fail_unlock; |
573 | } | 603 | } |
574 | 604 | ||
575 | status = sysfs_create_group(&dev->kobj, &gpio_attr_group); | ||
576 | if (status) | ||
577 | goto fail_unregister_device; | ||
578 | |||
579 | if (direction_may_change) { | ||
580 | status = device_create_file(dev, &dev_attr_direction); | ||
581 | if (status) | ||
582 | goto fail_unregister_device; | ||
583 | } | ||
584 | |||
585 | if (gpiod_to_irq(desc) >= 0 && (direction_may_change || | ||
586 | !test_bit(FLAG_IS_OUT, &desc->flags))) { | ||
587 | status = device_create_file(dev, &dev_attr_edge); | ||
588 | if (status) | ||
589 | goto fail_unregister_device; | ||
590 | } | ||
591 | |||
592 | set_bit(FLAG_EXPORT, &desc->flags); | 605 | set_bit(FLAG_EXPORT, &desc->flags); |
593 | mutex_unlock(&sysfs_lock); | 606 | mutex_unlock(&sysfs_lock); |
594 | return 0; | 607 | return 0; |
595 | 608 | ||
596 | fail_unregister_device: | ||
597 | device_unregister(dev); | ||
598 | fail_unlock: | 609 | fail_unlock: |
599 | mutex_unlock(&sysfs_lock); | 610 | mutex_unlock(&sysfs_lock); |
600 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); | 611 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
@@ -718,6 +729,7 @@ void gpiod_unexport(struct gpio_desc *desc) | |||
718 | dev = class_find_device(&gpio_class, NULL, desc, match_export); | 729 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
719 | if (dev) { | 730 | if (dev) { |
720 | gpio_setup_irq(desc, dev, 0); | 731 | gpio_setup_irq(desc, dev, 0); |
732 | clear_bit(FLAG_SYSFS_DIR, &desc->flags); | ||
721 | clear_bit(FLAG_EXPORT, &desc->flags); | 733 | clear_bit(FLAG_EXPORT, &desc->flags); |
722 | } else | 734 | } else |
723 | status = -ENODEV; | 735 | status = -ENODEV; |
@@ -750,13 +762,13 @@ int gpiochip_export(struct gpio_chip *chip) | |||
750 | 762 | ||
751 | /* use chip->base for the ID; it's already known to be unique */ | 763 | /* use chip->base for the ID; it's already known to be unique */ |
752 | mutex_lock(&sysfs_lock); | 764 | mutex_lock(&sysfs_lock); |
753 | dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip, | 765 | dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0), |
754 | "gpiochip%d", chip->base); | 766 | chip, gpiochip_groups, |
755 | if (!IS_ERR(dev)) { | 767 | "gpiochip%d", chip->base); |
756 | status = sysfs_create_group(&dev->kobj, | 768 | if (IS_ERR(dev)) |
757 | &gpiochip_attr_group); | ||
758 | } else | ||
759 | status = PTR_ERR(dev); | 769 | status = PTR_ERR(dev); |
770 | else | ||
771 | status = 0; | ||
760 | chip->exported = (status == 0); | 772 | chip->exported = (status == 0); |
761 | mutex_unlock(&sysfs_lock); | 773 | mutex_unlock(&sysfs_lock); |
762 | 774 | ||
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 487afe6f22fc..568aa2b6bdb0 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c | |||
@@ -248,29 +248,30 @@ int gpiochip_add(struct gpio_chip *chip) | |||
248 | base = gpiochip_find_base(chip->ngpio); | 248 | base = gpiochip_find_base(chip->ngpio); |
249 | if (base < 0) { | 249 | if (base < 0) { |
250 | status = base; | 250 | status = base; |
251 | goto unlock; | 251 | spin_unlock_irqrestore(&gpio_lock, flags); |
252 | goto err_free_descs; | ||
252 | } | 253 | } |
253 | chip->base = base; | 254 | chip->base = base; |
254 | } | 255 | } |
255 | 256 | ||
256 | status = gpiochip_add_to_list(chip); | 257 | status = gpiochip_add_to_list(chip); |
258 | if (status) { | ||
259 | spin_unlock_irqrestore(&gpio_lock, flags); | ||
260 | goto err_free_descs; | ||
261 | } | ||
257 | 262 | ||
258 | if (status == 0) { | 263 | for (id = 0; id < chip->ngpio; id++) { |
259 | for (id = 0; id < chip->ngpio; id++) { | 264 | struct gpio_desc *desc = &descs[id]; |
260 | struct gpio_desc *desc = &descs[id]; | 265 | |
261 | desc->chip = chip; | 266 | desc->chip = chip; |
262 | 267 | ||
263 | /* REVISIT: most hardware initializes GPIOs as | 268 | /* REVISIT: most hardware initializes GPIOs as inputs (often |
264 | * inputs (often with pullups enabled) so power | 269 | * with pullups enabled) so power usage is minimized. Linux |
265 | * usage is minimized. Linux code should set the | 270 | * code should set the gpio direction first thing; but until |
266 | * gpio direction first thing; but until it does, | 271 | * it does, and in case chip->get_direction is not set, we may |
267 | * and in case chip->get_direction is not set, | 272 | * expose the wrong direction in sysfs. |
268 | * we may expose the wrong direction in sysfs. | 273 | */ |
269 | */ | 274 | desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0; |
270 | desc->flags = !chip->direction_input | ||
271 | ? (1 << FLAG_IS_OUT) | ||
272 | : 0; | ||
273 | } | ||
274 | } | 275 | } |
275 | 276 | ||
276 | chip->desc = descs; | 277 | chip->desc = descs; |
@@ -284,12 +285,9 @@ int gpiochip_add(struct gpio_chip *chip) | |||
284 | of_gpiochip_add(chip); | 285 | of_gpiochip_add(chip); |
285 | acpi_gpiochip_add(chip); | 286 | acpi_gpiochip_add(chip); |
286 | 287 | ||
287 | if (status) | ||
288 | goto fail; | ||
289 | |||
290 | status = gpiochip_export(chip); | 288 | status = gpiochip_export(chip); |
291 | if (status) | 289 | if (status) |
292 | goto fail; | 290 | goto err_remove_chip; |
293 | 291 | ||
294 | pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__, | 292 | pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__, |
295 | chip->base, chip->base + chip->ngpio - 1, | 293 | chip->base, chip->base + chip->ngpio - 1, |
@@ -297,11 +295,15 @@ int gpiochip_add(struct gpio_chip *chip) | |||
297 | 295 | ||
298 | return 0; | 296 | return 0; |
299 | 297 | ||
300 | unlock: | 298 | err_remove_chip: |
299 | acpi_gpiochip_remove(chip); | ||
300 | of_gpiochip_remove(chip); | ||
301 | spin_lock_irqsave(&gpio_lock, flags); | ||
302 | list_del(&chip->list); | ||
301 | spin_unlock_irqrestore(&gpio_lock, flags); | 303 | spin_unlock_irqrestore(&gpio_lock, flags); |
302 | fail: | ||
303 | kfree(descs); | ||
304 | chip->desc = NULL; | 304 | chip->desc = NULL; |
305 | err_free_descs: | ||
306 | kfree(descs); | ||
305 | 307 | ||
306 | /* failures here can mean systems won't boot... */ | 308 | /* failures here can mean systems won't boot... */ |
307 | pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__, | 309 | pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__, |
@@ -325,14 +327,15 @@ void gpiochip_remove(struct gpio_chip *chip) | |||
325 | unsigned long flags; | 327 | unsigned long flags; |
326 | unsigned id; | 328 | unsigned id; |
327 | 329 | ||
328 | acpi_gpiochip_remove(chip); | 330 | gpiochip_unexport(chip); |
329 | |||
330 | spin_lock_irqsave(&gpio_lock, flags); | ||
331 | 331 | ||
332 | gpiochip_irqchip_remove(chip); | 332 | gpiochip_irqchip_remove(chip); |
333 | |||
334 | acpi_gpiochip_remove(chip); | ||
333 | gpiochip_remove_pin_ranges(chip); | 335 | gpiochip_remove_pin_ranges(chip); |
334 | of_gpiochip_remove(chip); | 336 | of_gpiochip_remove(chip); |
335 | 337 | ||
338 | spin_lock_irqsave(&gpio_lock, flags); | ||
336 | for (id = 0; id < chip->ngpio; id++) { | 339 | for (id = 0; id < chip->ngpio; id++) { |
337 | if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) | 340 | if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) |
338 | dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); | 341 | dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); |
@@ -342,7 +345,6 @@ void gpiochip_remove(struct gpio_chip *chip) | |||
342 | 345 | ||
343 | list_del(&chip->list); | 346 | list_del(&chip->list); |
344 | spin_unlock_irqrestore(&gpio_lock, flags); | 347 | spin_unlock_irqrestore(&gpio_lock, flags); |
345 | gpiochip_unexport(chip); | ||
346 | 348 | ||
347 | kfree(chip->desc); | 349 | kfree(chip->desc); |
348 | chip->desc = NULL; | 350 | chip->desc = NULL; |
diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index e3a52113a541..550a5eafbd38 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h | |||
@@ -77,6 +77,7 @@ struct gpio_desc { | |||
77 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ | 77 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ |
78 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ | 78 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ |
79 | #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ | 79 | #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ |
80 | #define FLAG_SYSFS_DIR 10 /* show sysfs direction attribute */ | ||
80 | 81 | ||
81 | #define ID_SHIFT 16 /* add new flags before this one */ | 82 | #define ID_SHIFT 16 /* add new flags before this one */ |
82 | 83 | ||