linux-mmc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
To: linux-mmc@vger.kernel.org
Cc: Chris Ball <cjb@laptop.org>, Magnus Damm <magnus.damm@gmail.com>
Subject: [PATCH 05/10 v3] mmc: convert slot functions to managed allocation
Date: Fri, 25 May 2012 17:16:07 +0200 (CEST)	[thread overview]
Message-ID: <Pine.LNX.4.64.1205251359200.13353@axis700.grange> (raw)
In-Reply-To: <Pine.LNX.4.64.1205251321020.13353@axis700.grange>

This prepares for the addition of further slot functions.

Signed-off-by: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
---
 drivers/mmc/core/host.c      |    2 +
 drivers/mmc/core/slot-gpio.c |   49 +++++++++++++++++++++++++++++++----------
 include/linux/mmc/host.h     |    3 ++
 3 files changed, 42 insertions(+), 12 deletions(-)

diff --git a/drivers/mmc/core/host.c b/drivers/mmc/core/host.c
index b8c5290..74cf29a5 100644
--- a/drivers/mmc/core/host.c
+++ b/drivers/mmc/core/host.c
@@ -32,6 +32,7 @@
 static void mmc_host_classdev_release(struct device *dev)
 {
 	struct mmc_host *host = cls_dev_to_mmc_host(dev);
+	mutex_destroy(&host->slot.lock);
 	kfree(host);
 }
 
@@ -327,6 +328,7 @@ struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
 
 	mmc_host_clk_init(host);
 
+	mutex_init(&host->slot.lock);
 	host->slot.cd_irq = -EINVAL;
 
 	spin_lock_init(&host->lock);
diff --git a/drivers/mmc/core/slot-gpio.c b/drivers/mmc/core/slot-gpio.c
index d70b790..f14c8a1 100644
--- a/drivers/mmc/core/slot-gpio.c
+++ b/drivers/mmc/core/slot-gpio.c
@@ -29,6 +29,33 @@ static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
 	return IRQ_HANDLED;
 }
 
+static int mmc_gpio_alloc(struct mmc_host *host)
+{
+	size_t len = strlen(dev_name(host->parent)) + 4;
+	struct mmc_gpio *ctx;
+
+	mutex_lock(&host->slot.lock);
+
+	ctx = host->slot.handler_priv;
+	if (!ctx) {
+		/*
+		 * devm_kzalloc() can be called after device_initialize(), even
+		 * before device_add(), i.e., between mmc_alloc_host() and
+		 * mmc_add_host()
+		 */
+		ctx = devm_kzalloc(&host->class_dev, sizeof(*ctx) + len,
+				   GFP_KERNEL);
+		if (ctx) {
+			snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
+			host->slot.handler_priv = ctx;
+		}
+	}
+
+	mutex_unlock(&host->slot.lock);
+
+	return ctx ? 0 : -ENOMEM;
+}
+
 int mmc_gpio_get_cd(struct mmc_host *host)
 {
 	struct mmc_gpio *ctx = host->slot.handler_priv;
@@ -43,20 +70,24 @@ EXPORT_SYMBOL(mmc_gpio_get_cd);
 
 int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio)
 {
-	size_t len = strlen(dev_name(host->parent)) + 4;
 	struct mmc_gpio *ctx;
 	int irq = gpio_to_irq(gpio);
 	int ret;
 
-	ctx = kmalloc(sizeof(*ctx) + len, GFP_KERNEL);
-	if (!ctx)
-		return -ENOMEM;
+	ret = mmc_gpio_alloc(host);
+	if (ret < 0)
+		return ret;
 
-	snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
+	ctx = host->slot.handler_priv;
 
 	ret = gpio_request_one(gpio, GPIOF_DIR_IN, ctx->cd_label);
 	if (ret < 0)
-		goto egpioreq;
+		/*
+		 * don't bother freeing gpio. It might still get used by other
+		 * slot functions, in any case it will be freed, when the device
+		 * is destroyed.
+		 */
+		return ret;
 
 	/*
 	 * Even if gpio_to_irq() returns a valid IRQ number, the platform might
@@ -83,10 +114,6 @@ int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio)
 	host->slot.handler_priv = ctx;
 
 	return 0;
-
-egpioreq:
-	kfree(ctx);
-	return ret;
 }
 EXPORT_SYMBOL(mmc_gpio_request_cd);
 
@@ -102,7 +129,5 @@ void mmc_gpio_free_cd(struct mmc_host *host)
 		host->slot.cd_irq = -EINVAL;
 	}
 	gpio_free(ctx->cd_gpio);
-	host->slot.handler_priv = NULL;
-	kfree(ctx);
 }
 EXPORT_SYMBOL(mmc_gpio_free_cd);
diff --git a/include/linux/mmc/host.h b/include/linux/mmc/host.h
index 1b3a917..3f3c8c9 100644
--- a/include/linux/mmc/host.h
+++ b/include/linux/mmc/host.h
@@ -11,6 +11,7 @@
 #define LINUX_MMC_HOST_H
 
 #include <linux/leds.h>
+#include <linux/mutex.h>
 #include <linux/sched.h>
 #include <linux/device.h>
 #include <linux/fault-inject.h>
@@ -154,6 +155,7 @@ struct mmc_async_req {
  * struct mmc_slot - MMC slot functions
  *
  * @cd_irq:		MMC/SD-card slot hotplug detection IRQ or -EINVAL
+ * @lock:		protect the @handler_priv pointer
  * @handler_priv:	MMC/SD-card slot context
  *
  * Some MMC/SD host controllers implement slot-functions like card and
@@ -163,6 +165,7 @@ struct mmc_async_req {
  */
 struct mmc_slot {
 	int cd_irq;
+	struct mutex lock;
 	void *handler_priv;
 };
 
-- 
1.7.2.5


  parent reply	other threads:[~2012-05-25 15:16 UTC|newest]

Thread overview: 90+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-25 15:14 [PATCH 0/0/6 v3] mmc core, tmio / sdhi and sh-mmcif updates Guennadi Liakhovetski
2012-05-25 15:14 ` [PATCH 0/3:1/6 v3] mmc: sh-mmcif: clock management updates Guennadi Liakhovetski
2012-05-25 15:14   ` [PATCH 1/3 v3] mmc: sh_mmcif: simplify and use meaningful label names in error-handling Guennadi Liakhovetski
2012-05-25 15:14   ` [PATCH 2/3 v3] mmc: sh_mmcif: fix clock management Guennadi Liakhovetski
2012-05-25 15:14   ` [PATCH 3/3 v3] mmc: sh_mmcif: re-read the clock frequency every time the clock is turned on Guennadi Liakhovetski
2012-05-25 15:14 ` [PATCH 0/5:2/6 v3] mmc: sh_mmcif: add regulator support Guennadi Liakhovetski
2012-05-25 15:14   ` [PATCH 1/5 v3] mmc: sh_mmcif: remove redundant .down_pwr() callback Guennadi Liakhovetski
2012-05-25 15:14   ` [PATCH 2/5 v3] sh: ecovec: remove unused .down_pwr() MMCIF callback Guennadi Liakhovetski
2012-05-25 15:14   ` [PATCH 3/5 v3] mmc: sh_mmcif: remove unused .down_pwr() callback Guennadi Liakhovetski
2012-05-25 15:14   ` [PATCH 4/5 v3] mmc: add a function to get a regulator, supplying card's Vdd Guennadi Liakhovetski
2012-05-28  3:20     ` Ulf Hansson
2012-05-28  3:32       ` Philip Rakity
2012-05-28  8:12         ` Adrian Hunter
2012-05-28 14:43           ` Mark Brown
2012-06-11 12:39             ` Guennadi Liakhovetski
2012-06-11 14:23               ` Mark Brown
2012-06-11 14:36                 ` Philip Rakity
2012-06-11 14:43                   ` Ulf Hansson
2012-05-28 14:45     ` Mark Brown
2012-06-11 11:03       ` Guennadi Liakhovetski
2012-06-11 13:41         ` Chris Ball
2012-05-25 15:14   ` [PATCH 5/5 v3] mmc: sh_mmcif: add regulator support Guennadi Liakhovetski
2012-05-25 15:15 ` [PATCH 0/8:3/6 v3] mmc: tmio: clock and PM updates Guennadi Liakhovetski
2012-05-25 15:15   ` [PATCH 1/8 v3] mmc: tmio: stop interface clock before runtime PM suspending Guennadi Liakhovetski
2012-05-25 15:15   ` [PATCH 2/8 v3] mmc: tmio: don't needlessly enable interrupts during probing Guennadi Liakhovetski
2012-05-25 15:15   ` [PATCH 3/8 v3] mmc: tmio: add callbacks to enable-update and disable the interface clock Guennadi Liakhovetski
2012-05-25 15:15   ` [PATCH 4/8 v3] mmc: sdhi: implement tmio-mmc clock enable-update and disable callbacks Guennadi Liakhovetski
2012-05-25 15:15   ` [PATCH 5/8 v3] mmc: tmio: add regulator support Guennadi Liakhovetski
2012-05-25 15:15   ` [PATCH 6/8 v3] mmc: sdhi: do not install dummy callbacks Guennadi Liakhovetski
2012-05-25 15:15   ` [PATCH 7/8 v3] mmc: tmio: use MMC opcode defines instead of numbers Guennadi Liakhovetski
2012-05-25 15:15   ` [PATCH 8/8 v3] mmc: tmio: remove a duplicated comment line Guennadi Liakhovetski
2012-05-25 15:15 ` [PATCH 00/10:4/6 v3] mmc: cd-gpio: extend to handle more slot functions Guennadi Liakhovetski
2012-05-25 15:15   ` [PATCH 01/10 v3] mmc: extend and rename cd-gpio helpers to handle more slot GPIO functions Guennadi Liakhovetski
2012-05-25 15:15   ` [PATCH 02/10 v3] mmc: use a more generic name for slot function types and fields Guennadi Liakhovetski
2012-05-25 15:15   ` [PATCH 03/10 v3] mmc: add two capability flags for CD and WP signal polarity Guennadi Liakhovetski
2012-05-25 15:16   ` [PATCH 04/10 v3] mmc: add CD GPIO polling support to slot functions Guennadi Liakhovetski
2012-05-25 15:16   ` Guennadi Liakhovetski [this message]
2012-05-25 15:16   ` [PATCH 06/10 v3] mmc: add WP pin handler " Guennadi Liakhovetski
2012-05-25 15:16   ` [PATCH 07/10 v3] mmc: tmio: support caps2 flags Guennadi Liakhovetski
2012-05-25 15:16   ` [PATCH 08/10 v3] mmc: sh_mobile_sdhi: " Guennadi Liakhovetski
2012-05-25 15:16   ` [PATCH 09/10 v3] ARM: mach-shmobile: specify inverted SDHI card-detect signal polarity Guennadi Liakhovetski
2012-05-25 15:16   ` [PATCH 10/10 v3] mmc: tmio: use generic GPIO CD and WP handlers Guennadi Liakhovetski
2012-05-25 15:16 ` [PATCH 0/2:5/6 v3] mmc: add primitive OF support to sh-mmcif and sdhi Guennadi Liakhovetski
2012-05-25 15:16   ` [PATCH 1/2 v3] mmc: sdhi: add OF support, make platform data optional Guennadi Liakhovetski
2012-05-25 15:17   ` [PATCH 2/2 v3] mmc: sh-mmcif: " Guennadi Liakhovetski
2012-05-25 15:17 ` [PATCH 0/3:6/6 v3] sh, ARM: mach-shmobile: update MMC platform data Guennadi Liakhovetski
2012-05-25 15:17   ` [PATCH 1/3 v3] sh: ecovec: switch MMC power control to regulators Guennadi Liakhovetski
2012-05-25 15:17   ` [PATCH 2/3 v3] ARM: mach-shmobile: switch all SDHI instances on mackerel to use GPIO CD Guennadi Liakhovetski
2012-05-25 15:17   ` [PATCH 3/3 v3] ARM: mach-shmobile: specify IRQ names on mackerel SDHI0 interface Guennadi Liakhovetski
2012-05-25 15:31 ` [PATCH 0/0/6 v3] mmc core, tmio / sdhi and sh-mmcif updates Guennadi Liakhovetski
2012-05-25 15:45   ` Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 0/3:1/6 v3] mmc: sh-mmcif: clock management updates Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 1/3 v3] mmc: sh_mmcif: simplify and use meaningful label names in error-handling Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 2/3 v3] mmc: sh_mmcif: fix clock management Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 3/3 v3] mmc: sh_mmcif: re-read the clock frequency every time the clock is turned on Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 0/5:2/6 v3] mmc: sh_mmcif: add regulator support Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 1/5 v3] mmc: sh_mmcif: remove redundant .down_pwr() callback Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 2/5 v3] sh: ecovec: remove unused .down_pwr() MMCIF callback Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 3/5 v3] mmc: sh_mmcif: remove unused .down_pwr() callback Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 4/5 v3] mmc: add a function to get a regulator, supplying card's Vdd Guennadi Liakhovetski
2012-05-28 14:46     ` Mark Brown
2012-05-25 15:45   ` [PATCH 5/5 v3] mmc: sh_mmcif: add regulator support Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 0/8:3/6 v3] mmc: tmio: clock and PM updates Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 1/8 v3] mmc: tmio: stop interface clock before runtime PM suspending Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 2/8 v3] mmc: tmio: don't needlessly enable interrupts during probing Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 3/8 v3] mmc: tmio: add callbacks to enable-update and disable the interface clock Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 4/8 v3] mmc: sdhi: implement tmio-mmc clock enable-update and disable callbacks Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 5/8 v3] mmc: tmio: add regulator support Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 6/8 v3] mmc: sdhi: do not install dummy callbacks Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 7/8 v3] mmc: tmio: use MMC opcode defines instead of numbers Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 8/8 v3] mmc: tmio: remove a duplicated comment line Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 00/10:4/6 v3] mmc: cd-gpio: extend to handle more slot functions Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 01/10 v3] mmc: extend and rename cd-gpio helpers to handle more slot GPIO functions Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 02/10 v3] mmc: use a more generic name for slot function types and fields Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 03/10 v3] mmc: add two capability flags for CD and WP signal polarity Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 04/10 v3] mmc: add CD GPIO polling support to slot functions Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 05/10 v3] mmc: convert slot functions to managed allocation Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 06/10 v3] mmc: add WP pin handler to slot functions Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 07/10 v3] mmc: tmio: support caps2 flags Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 08/10 v3] mmc: sh_mobile_sdhi: " Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 09/10 v3] ARM: mach-shmobile: specify inverted SDHI card-detect signal polarity Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 10/10 v3] mmc: tmio: use generic GPIO CD and WP handlers Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 0/2:5/6 v3] mmc: add primitive OF support to sh-mmcif and sdhi Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 1/2 v3] mmc: sdhi: add OF support, make platform data optional Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 2/2 v3] mmc: sh-mmcif: " Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 0/3:6/6 v3] sh, ARM: mach-shmobile: update MMC platform data Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 1/3 v3] sh: ecovec: switch MMC power control to regulators Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 2/3 v3] ARM: mach-shmobile: switch all SDHI instances on mackerel to use GPIO CD Guennadi Liakhovetski
2012-05-25 15:45   ` [PATCH 3/3 v3] ARM: mach-shmobile: specify IRQ names on mackerel SDHI0 interface Guennadi Liakhovetski
2012-05-25 16:01   ` [PATCH 0/0/6 v3] mmc core, tmio / sdhi and sh-mmcif updates Guennadi Liakhovetski

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=Pine.LNX.4.64.1205251359200.13353@axis700.grange \
    --to=g.liakhovetski@gmx.de \
    --cc=cjb@laptop.org \
    --cc=linux-mmc@vger.kernel.org \
    --cc=magnus.damm@gmail.com \
    /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).