[PATCH v1 1/3] mailbox: Provide devm variants for mbox_request_channel{,_byname}()

Uwe Kleine-König <[email protected]>
Newsgroups org.kernel.vger.linux-kernel
Message-ID <986b6e23bb2008ee9a45e678b6a56e44d11683d1.1786547950.git.u.kleine-koenig@baylibre.com>
The new functions devm_mbox_request_channel() and
devm_mbox_request_channel_byname() allow to simplify resource management
for callers in their .probe() and .remove() functions.

Signed-off-by: Uwe Kleine-König <[email protected]>
---
 drivers/mailbox/mailbox.c      | 44 ++++++++++++++++++++++++++++++++++
 include/linux/mailbox_client.h |  5 ++++
 2 files changed, 49 insertions(+)

diff --git a/drivers/mailbox/mailbox.c b/drivers/mailbox/mailbox.c
index efacd24a085d..f51b4ca02041 100644
--- a/drivers/mailbox/mailbox.c
+++ b/drivers/mailbox/mailbox.c
@@ -490,6 +490,31 @@ struct mbox_chan *mbox_request_channel(struct mbox_client *cl, int index)
 }
 EXPORT_SYMBOL_GPL(mbox_request_channel);
 
+static void devm_mbox_free_channel(void *data)
+{
+	struct mbox_chan *chan = data;
+
+	mbox_free_channel(chan);
+}
+
+struct mbox_chan *devm_mbox_request_channel(struct device *dev,
+					    struct mbox_client *cl, int index)
+{
+	struct mbox_chan *chan;
+	int ret;
+
+	chan = mbox_request_channel(cl, index);
+	if (IS_ERR(chan))
+		return chan;
+
+	ret = devm_add_action_or_reset(dev, devm_mbox_free_channel, chan);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return chan;
+}
+EXPORT_SYMBOL_GPL(devm_mbox_request_channel);
+
 struct mbox_chan *mbox_request_channel_byname(struct mbox_client *cl,
 					      const char *name)
 {
@@ -504,6 +529,25 @@ struct mbox_chan *mbox_request_channel_byname(struct mbox_client *cl,
 }
 EXPORT_SYMBOL_GPL(mbox_request_channel_byname);
 
+struct mbox_chan *devm_mbox_request_channel_byname(struct device *dev,
+						   struct mbox_client *cl,
+						   const char *name)
+{
+	struct mbox_chan *chan;
+	int ret;
+
+	chan = mbox_request_channel_byname(cl, name);
+	if (IS_ERR(chan))
+		return chan;
+
+	ret = devm_add_action_or_reset(dev, devm_mbox_free_channel, chan);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return chan;
+}
+EXPORT_SYMBOL_GPL(devm_mbox_request_channel_byname);
+
 /**
  * mbox_free_channel - The client relinquishes control of a mailbox
  *			channel by this call.
diff --git a/include/linux/mailbox_client.h b/include/linux/mailbox_client.h
index e5997120f45c..ce550a93bf61 100644
--- a/include/linux/mailbox_client.h
+++ b/include/linux/mailbox_client.h
@@ -40,7 +40,12 @@ struct mbox_client {
 int mbox_bind_client(struct mbox_chan *chan, struct mbox_client *cl);
 struct mbox_chan *mbox_request_channel_byname(struct mbox_client *cl,
 					      const char *name);
+struct mbox_chan *devm_mbox_request_channel_byname(struct device *dev,
+						   struct mbox_client *cl,
+						   const char *name);
 struct mbox_chan *mbox_request_channel(struct mbox_client *cl, int index);
+struct mbox_chan *devm_mbox_request_channel(struct device *dev,
+					    struct mbox_client *cl, int index);
 int mbox_send_message(struct mbox_chan *chan, void *mssg);
 int mbox_flush(struct mbox_chan *chan, unsigned long timeout);
 void mbox_client_txdone(struct mbox_chan *chan, int r); /* atomic */
-- 
2.47.3
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.