[RFC 1/2] misc: Add of_get_misc get a reference from devicetree

From: Moritz Fischer
Date: Tue Mar 22 2016 - 18:34:11 EST


This commit enables access to a miscdevice via a reference
obtained from devicetree.
This allows to implement a of_ion_device_get() in the next step.

Signed-off-by: Moritz Fischer <moritz.fischer@xxxxxxxxx>
---
drivers/char/misc.c | 38 ++++++++++++++++++++++++++++++++++++++
include/linux/miscdevice.h | 3 +++
2 files changed, 41 insertions(+)

diff --git a/drivers/char/misc.c b/drivers/char/misc.c
index 8069b36..0623834 100644
--- a/drivers/char/misc.c
+++ b/drivers/char/misc.c
@@ -275,6 +275,44 @@ static char *misc_devnode(struct device *dev, umode_t *mode)
return NULL;
}

+#ifdef CONFIG_OF
+static int misc_of_node_match(struct device *dev, const void *data)
+{
+ return dev->of_node == data;
+}
+
+struct miscdevice *of_misc_get(struct device_node *node)
+{
+ struct miscdevice *m;
+ struct device *dev;
+ int ret = -ENODEV;
+
+ dev = class_find_device(misc_class, NULL, node,
+ misc_of_node_match);
+ if (!dev)
+ return ERR_PTR(-ENODEV);
+
+ m = dev_get_drvdata(dev);
+ if (!m)
+ goto err_dev;
+
+ if (!try_module_get(dev->parent->driver->owner))
+ goto err_dev;
+
+ return m;
+
+err_dev:
+ put_device(dev);
+ return ERR_PTR(ret);
+}
+#else
+struct misc_device *of_misc_get(struct device_node *)
+{
+ return ERR_PTR(-ENODEV);
+}
+#endif
+EXPORT_SYMBOL(of_misc_get);
+
static int __init misc_init(void)
{
int err;
diff --git a/include/linux/miscdevice.h b/include/linux/miscdevice.h
index 5430374..a4b605c 100644
--- a/include/linux/miscdevice.h
+++ b/include/linux/miscdevice.h
@@ -53,6 +53,7 @@
#define MISC_DYNAMIC_MINOR 255

struct device;
+struct device_node;
struct attribute_group;

struct miscdevice {
@@ -70,6 +71,8 @@ struct miscdevice {
extern int misc_register(struct miscdevice *misc);
extern void misc_deregister(struct miscdevice *misc);

+extern struct miscdevice *of_misc_get(struct device_node *node);
+
#define MODULE_ALIAS_MISCDEV(minor) \
MODULE_ALIAS("char-major-" __stringify(MISC_MAJOR) \
"-" __stringify(minor))
--
2.7.4