mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Gregory Fong <Gregory.Fong@virginorbit.com>
To: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Arnd Bergmann <arnd@arndb.de>, Jonathan Corbet <corbet@lwn.net>,
	"linux-doc@vger.kernel.org" <linux-doc@vger.kernel.org>
Subject: [PATCH] misc: Implement devm_misc_register
Date: Tue, 9 May 2017 02:27:48 +0000	[thread overview]
Message-ID: <1494296848-91232-1-git-send-email-gregory.fong@virginorbit.com> (raw)

Add device managed devm_misc_register() to allow simplifying some
miscdevice code.

Signed-off-by: Gregory Fong <gregory.fong@virginorbit.com>
---
This seemed like it would be handy for removing a large chunk of the cleanup
code in various miscdevice users.  Let me know whether you think it'd be worth
going ahead changing this throughout the codebase where appropriate.

 Documentation/driver-model/devres.txt |  5 ++++-
 drivers/char/misc.c                   | 37 +++++++++++++++++++++++++++++++++++
 include/linux/miscdevice.h            |  2 ++
 3 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/Documentation/driver-model/devres.txt b/Documentation/driver-model/devres.txt
index bf34d5b..3b193f0 100644
--- a/Documentation/driver-model/devres.txt
+++ b/Documentation/driver-model/devres.txt
@@ -335,7 +335,10 @@ MEM
   devm_kzalloc()
 
 MFD
- devm_mfd_add_devices()
+  devm_mfd_add_devices()
+
+MISC
+  devm_misc_register()
 
 PER-CPU MEM
   devm_alloc_percpu()
diff --git a/drivers/char/misc.c b/drivers/char/misc.c
index c9cd1ea..b7f653a 100644
--- a/drivers/char/misc.c
+++ b/drivers/char/misc.c
@@ -265,6 +265,43 @@ void misc_deregister(struct miscdevice *misc)
 EXPORT_SYMBOL(misc_register);
 EXPORT_SYMBOL(misc_deregister);
 
+static void devm_misc_dereg(struct device *dev, void *res)
+{
+	misc_deregister(*(struct miscdevice **)res);
+}
+
+/**
+ * devm_misc_register - Resource-managed misc_register
+ * @dev:	Device to allocate miscdevice for
+ * @misc:	Device structure filled by the device driver
+ *
+ * Managed misc_register. The miscdevice registered with this function is
+ * automatically unregistered on driver detach. This function calls
+ * misc_register() internally; refer to its documentation for more information.
+ *
+ * RETURNS:
+ * 0 on success, negative error number on failure.
+ */
+int devm_misc_register(struct device *dev, struct miscdevice *misc)
+{
+	struct miscdevice **misc_ptr;
+	int ret;
+
+	misc_ptr = devres_alloc(devm_misc_dereg, sizeof(*misc_ptr), GFP_KERNEL);
+	if (!misc_ptr)
+		return -ENOMEM;
+
+	*misc_ptr = misc;
+	ret = misc_register(misc);
+	if (!ret)
+		devres_add(dev, misc_ptr);
+	else
+		devres_free(misc_ptr);
+
+	return ret;
+}
+EXPORT_SYMBOL(devm_misc_register);
+
 static char *misc_devnode(struct device *dev, umode_t *mode)
 {
 	struct miscdevice *c = dev_get_drvdata(dev);
diff --git a/include/linux/miscdevice.h b/include/linux/miscdevice.h
index 762b5fe..5289b3f 100644
--- a/include/linux/miscdevice.h
+++ b/include/linux/miscdevice.h
@@ -74,6 +74,8 @@ struct miscdevice  {
 extern int misc_register(struct miscdevice *misc);
 extern void misc_deregister(struct miscdevice *misc);
 
+int devm_misc_register(struct device *dev, struct miscdevice *misc);
+
 /*
  * Helper macro for drivers that don't do anything special in the initcall.
  * This helps in eleminating of boilerplate code.
-- 
1.9.1

             reply	other threads:[~2017-05-09  2:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-09  2:27 Gregory Fong [this message]
2017-05-09  5:37 ` Greg Kroah-Hartman

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=1494296848-91232-1-git-send-email-gregory.fong@virginorbit.com \
    --to=gregory.fong@virginorbit.com \
    --cc=arnd@arndb.de \
    --cc=corbet@lwn.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-doc@vger.kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®