From: b29396@freescale.com (Dong Aisheng)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH v4 4/6] gpio: introduce lock mechanism for gpiochip_find
Date: Fri, 25 May 2012 21:36:18 +0800 [thread overview]
Message-ID: <1337952980-14621-4-git-send-email-b29396@freescale.com> (raw)
In-Reply-To: <1337952980-14621-1-git-send-email-b29396@freescale.com>
From: Dong Aisheng <dong.aisheng@linaro.org>
The module lock will be automatically claimed for gpiochip_find function
in case the gpio module is removed during the using of gpiochip instance.
Users are responsible to call gpiochip_put to release the lock after
the using.
Signed-off-by: Dong Aisheng <dong.aisheng@linaro.org>
---
drivers/gpio/gpiolib-of.c | 5 ++++-
drivers/gpio/gpiolib.c | 17 +++++++++++++++++
include/asm-generic/gpio.h | 2 +-
3 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
index b8010a9..d521452 100644
--- a/drivers/gpio/gpiolib-of.c
+++ b/drivers/gpio/gpiolib-of.c
@@ -63,6 +63,7 @@ int of_get_named_gpio_flags(struct device_node *np, const char *propname,
int index, enum of_gpio_flags *flags)
{
struct gg_data gg_data = { .flags = flags, .out_gpio = -ENODEV };
+ struct gpio_chip *chip;
int ret;
/* .of_xlate might decide to not fill in the flags, so clear it. */
@@ -76,7 +77,9 @@ int of_get_named_gpio_flags(struct device_node *np, const char *propname,
return -EINVAL;
}
- gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
+ chip = gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
+ if (chip)
+ gpiochip_put(chip);
of_node_put(gg_data.gpiospec.np);
pr_debug("%s exited with status %d\n", __func__, ret);
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 120b2a0..6453d43 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -1144,6 +1144,18 @@ int gpiochip_remove(struct gpio_chip *chip)
EXPORT_SYMBOL_GPL(gpiochip_remove);
/**
+ * gpiochip_put() - release a gpio_chip
+ * @chip: the chip to release
+ */
+inline void gpiochip_put(struct gpio_chip *chip)
+{
+ BUG_ON(!chip);
+
+ module_put(chip->owner);
+}
+EXPORT_SYMBOL_GPL(gpiochip_put);
+
+/**
* gpiochip_find() - iterator for locating a specific gpio_chip
* @data: data to pass to match function
* @callback: Callback function to check gpio_chip
@@ -1153,6 +1165,9 @@ EXPORT_SYMBOL_GPL(gpiochip_remove);
* 0 if the device doesn't match and non-zero if it does. If the callback is
* non-zero, this function will return to the caller and not iterate over any
* more gpio_chips.
+ *
+ * Note the gpio_chip is returned with the module locked, users are responsible
+ * to release the lock with gpiochip_put(chip) after using it.
*/
struct gpio_chip *gpiochip_find(void *data,
int (*match)(struct gpio_chip *chip,
@@ -1169,6 +1184,8 @@ struct gpio_chip *gpiochip_find(void *data,
if (match(gpio_desc[i].chip, data)) {
chip = gpio_desc[i].chip;
+ if (!try_module_get(chip->owner))
+ chip = NULL;
break;
}
}
diff --git a/include/asm-generic/gpio.h b/include/asm-generic/gpio.h
index 365ea09..af372be 100644
--- a/include/asm-generic/gpio.h
+++ b/include/asm-generic/gpio.h
@@ -145,7 +145,7 @@ extern int __must_check gpiochip_remove(struct gpio_chip *chip);
extern struct gpio_chip *gpiochip_find(void *data,
int (*match)(struct gpio_chip *chip,
void *data));
-
+extern void gpiochip_put(struct gpio_chip *chip);
/* Always use the library code for GPIO management calls,
* or when sleeping may be involved.
--
1.7.0.4
next prev parent reply other threads:[~2012-05-25 13:36 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-25 13:36 [PATCH v4 1/6] gpio: fix a typo of comment message Dong Aisheng
2012-05-25 13:36 ` [PATCH v4 2/6] gpio: re-add of_node_to_gpiochip function Dong Aisheng
2012-05-26 0:01 ` Grant Likely
2012-05-26 16:15 ` Dong Aisheng
2012-05-25 13:36 ` [PATCH v4 3/6] of: release node fix for of_parse_phandle_with_args Dong Aisheng
2012-05-26 0:09 ` Grant Likely
2012-05-25 13:36 ` Dong Aisheng [this message]
2012-05-26 0:25 ` [PATCH v4 4/6] gpio: introduce lock mechanism for gpiochip_find Grant Likely
2012-05-26 16:17 ` Dong Aisheng
2012-05-30 4:10 ` Dong Aisheng
2012-05-30 6:33 ` Grant Likely
2012-05-25 13:36 ` [PATCH v4 5/6] of: add of_parse_phandle_with_args_ext function Dong Aisheng
2012-05-25 16:50 ` Stephen Warren
2012-05-25 13:36 ` [PATCH v4 6/6] pinctrl: add pinctrl gpio binding support Dong Aisheng
2012-05-25 17:03 ` Stephen Warren
2012-05-26 16:52 ` Dong Aisheng
2012-05-27 15:39 ` Stephen Warren
2012-05-30 3:01 ` Dong Aisheng
2012-05-30 3:52 ` Stephen Warren
2012-05-30 6:35 ` Grant Likely
2012-05-26 0:29 ` Grant Likely
2012-05-26 16:58 ` Dong Aisheng
2012-05-30 6:46 ` Grant Likely
2012-05-30 7:29 ` Dong Aisheng
2012-05-25 23:57 ` [PATCH v4 1/6] gpio: fix a typo of comment message Grant Likely
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1337952980-14621-4-git-send-email-b29396@freescale.com \
--to=b29396@freescale.com \
--cc=linux-arm-kernel@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).