* [PATCH] misc: Implement devm_misc_register
@ 2017-05-09 2:27 Gregory Fong
2017-05-09 5:37 ` Greg Kroah-Hartman
0 siblings, 1 reply; 2+ messages in thread
From: Gregory Fong @ 2017-05-09 2:27 UTC (permalink / raw)
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman, Arnd Bergmann, Jonathan Corbet,
linux-doc@vger.kernel.org
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
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] misc: Implement devm_misc_register
2017-05-09 2:27 [PATCH] misc: Implement devm_misc_register Gregory Fong
@ 2017-05-09 5:37 ` Greg Kroah-Hartman
0 siblings, 0 replies; 2+ messages in thread
From: Greg Kroah-Hartman @ 2017-05-09 5:37 UTC (permalink / raw)
To: Gregory Fong
Cc: linux-kernel@vger.kernel.org, Arnd Bergmann, Jonathan Corbet,
linux-doc@vger.kernel.org
On Tue, May 09, 2017 at 02:27:48AM +0000, Gregory Fong wrote:
> 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.
I'd like to see how this is actually used before accepting it, as we
don't like adding apis that do not have any users. So care to make this
a patch series, first one with this change, and the rest converting some
drivers to use it?
thanks,
greg k-h
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2017-05-09 5:37 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-05-09 2:27 [PATCH] misc: Implement devm_misc_register Gregory Fong
2017-05-09 5:37 ` Greg Kroah-Hartman
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox