[PATCH] misc: Implement devm_misc_register

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

Signed-off-by: Gregory Fong <gregory.fong@xxxxxxxxxxxxxxx>
---
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
--
To unsubscribe from this list: send the line "unsubscribe linux-doc" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Linux FS]     [Yosemite Forum]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Video 4 Linux]     [Device Mapper]     [Linux Resources]

  Powered by Linux