All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thierry Reding <thierry.reding@gmail.com>
To: Jassi Brar <jassisinghbrar@gmail.com>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH 01/19] mailbox: Add device-managed registration functions
Date: Wed, 21 Nov 2018 15:54:11 +0100	[thread overview]
Message-ID: <20181121145429.7582-2-thierry.reding@gmail.com> (raw)
In-Reply-To: <20181121145429.7582-1-thierry.reding@gmail.com>

From: Thierry Reding <treding@nvidia.com>

Add device-managed equivalents of the mbox_controller_register() and
mbox_controller_unregister() functions that can be used to have the
devres infrastructure automatically unregister mailbox controllers on
driver probe failure or driver removal. This can help remove a lot of
boiler plate code from drivers.

Signed-off-by: Thierry Reding <treding@nvidia.com>
---
 drivers/mailbox/mailbox.c          | 70 ++++++++++++++++++++++++++++++
 include/linux/mailbox_controller.h |  5 +++
 2 files changed, 75 insertions(+)

diff --git a/drivers/mailbox/mailbox.c b/drivers/mailbox/mailbox.c
index 674b35f402f5..eb781e2b19cb 100644
--- a/drivers/mailbox/mailbox.c
+++ b/drivers/mailbox/mailbox.c
@@ -515,3 +515,73 @@ void mbox_controller_unregister(struct mbox_controller *mbox)
 	mutex_unlock(&con_mutex);
 }
 EXPORT_SYMBOL_GPL(mbox_controller_unregister);
+
+static void __devm_mbox_controller_unregister(struct device *dev, void *res)
+{
+	struct mbox_controller *mbox = res;
+
+	mbox_controller_unregister(mbox);
+}
+
+static int devm_mbox_controller_match(struct device *dev, void *res, void *data)
+{
+	struct mbox_controller **mbox = res;
+
+	if (WARN_ON(!mbox || !*mbox))
+		return 0;
+
+	return *mbox == data;
+}
+
+/**
+ * devm_mbox_controller_register() - managed mbox_controller_register()
+ * @dev: device owning the mailbox controller being registered
+ * @mbox: mailbox controller being registered
+ *
+ * This function adds a device-managed resource that will make sure that the
+ * mailbox controller, which is registered using mbox_controller_register()
+ * as part of this function, will be unregistered along with the rest of
+ * device-managed resources upon driver probe failure or driver removal.
+ *
+ * Returns 0 on success or a negative error code on failure.
+ */
+int devm_mbox_controller_register(struct device *dev,
+				  struct mbox_controller *mbox)
+{
+	struct mbox_controller **ptr;
+	int err;
+
+	ptr = devres_alloc(__devm_mbox_controller_unregister, sizeof(*ptr),
+			   GFP_KERNEL);
+	if (!ptr)
+		return -ENOMEM;
+
+	err = mbox_controller_register(mbox);
+	if (err < 0) {
+		devres_free(ptr);
+		return err;
+	}
+
+	devres_add(dev, ptr);
+	*ptr = mbox;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(devm_mbox_controller_register);
+
+/**
+ * devm_mbox_controller_unregister() - managed mbox_controller_unregister()
+ * @dev: device owning the mailbox controller being unregistered
+ * @mbox: mailbox controller being unregistered
+ *
+ * This function unregisters the mailbox controller and removes the device-
+ * managed resource that was set up to automatically unregister the mailbox
+ * controller on driver probe failure or driver removal. It's typically not
+ * necessary to call this function.
+ */
+void devm_mbox_controller_unregister(struct device *dev, struct mbox_controller *mbox)
+{
+	WARN_ON(devres_release(dev, __devm_mbox_controller_unregister,
+			       devm_mbox_controller_match, mbox));
+}
+EXPORT_SYMBOL_GPL(devm_mbox_controller_unregister);
diff --git a/include/linux/mailbox_controller.h b/include/linux/mailbox_controller.h
index 74deadb42d76..9b0b21207345 100644
--- a/include/linux/mailbox_controller.h
+++ b/include/linux/mailbox_controller.h
@@ -131,4 +131,9 @@ void mbox_controller_unregister(struct mbox_controller *mbox); /* can sleep */
 void mbox_chan_received_data(struct mbox_chan *chan, void *data); /* atomic */
 void mbox_chan_txdone(struct mbox_chan *chan, int r); /* atomic */
 
+int devm_mbox_controller_register(struct device *dev,
+				  struct mbox_controller *mbox);
+void devm_mbox_controller_unregister(struct device *dev,
+				     struct mbox_controller *mbox);
+
 #endif /* __MAILBOX_CONTROLLER_H */
-- 
2.19.1


  reply	other threads:[~2018-11-21 14:54 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-21 14:54 [PATCH 00/19] mailbox: Device-managed registration Thierry Reding
2018-11-21 14:54 ` Thierry Reding [this message]
2018-11-21 14:54 ` [PATCH 02/19] mailbox: arm-mhu: Use device-managed registration API Thierry Reding
2018-11-21 14:54 ` [PATCH 03/19] mailbox: bcm2835: " Thierry Reding
2018-11-21 14:54 ` [PATCH 04/19] mailbox: bcm-flexrm: " Thierry Reding
2018-11-21 14:54 ` [PATCH 05/19] mailbox: bcm-pdc: " Thierry Reding
2018-11-21 14:54 ` [PATCH 06/19] mailbox: hi3660: " Thierry Reding
2018-11-21 14:54 ` [PATCH 07/19] mailbox: hi6220: " Thierry Reding
2018-11-21 14:54 ` [PATCH 08/19] mailbox: imx: " Thierry Reding
2018-11-21 14:54 ` [PATCH 09/19] mailbox: altera: " Thierry Reding
2018-11-21 14:54 ` [PATCH 10/19] mailbox: sti: " Thierry Reding
2018-11-21 14:54 ` [PATCH 11/19] mailbox: xgene-slimpro: " Thierry Reding
2018-11-21 14:54 ` [PATCH 12/19] mailbox: mtk-cmdq: " Thierry Reding
2018-11-21 14:54 ` [PATCH 13/19] mailbox: mtk-cmdq: Remove needless devm_kfree() calls Thierry Reding
2018-11-21 14:54 ` [PATCH 14/19] mailbox: omap: Use device-managed registration API Thierry Reding
2018-11-21 14:54 ` [PATCH 15/19] mailbox: platform-mhu: " Thierry Reding
2018-11-21 14:54 ` [PATCH 16/19] mailbox: qcom-apcs: " Thierry Reding
2018-11-21 14:54 ` [PATCH 17/19] mailbox: rockchip: " Thierry Reding
2018-11-21 14:54 ` [PATCH 18/19] mailbox: stm32-ipcc: " Thierry Reding
2018-11-21 14:54 ` [PATCH 19/19] mailbox: ti-msgmgr: " Thierry Reding
2018-11-28  9:39 ` [PATCH 00/19] mailbox: Device-managed registration Thierry Reding
2018-12-05 16:14   ` Thierry Reding
2018-12-07  4:09     ` Jassi Brar
2018-12-11 17:04 ` Jassi Brar
2018-12-17 14:35   ` Thierry Reding
2018-12-17 14:36     ` Jassi Brar

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=20181121145429.7582-2-thierry.reding@gmail.com \
    --to=thierry.reding@gmail.com \
    --cc=jassisinghbrar@gmail.com \
    --cc=linux-kernel@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.