mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Moritz Fischer <moritz.fischer@ettus.com>
To: arnd@arndb.de
Cc: gregkh@linuxfoundation.org, arve@android.com,
	riandrews@android.com, labbott@redhat.com, gioh.kim@lge.com,
	sumit.semwal@linaro.org, dan.carpenter@oracle.com,
	sriram@marirs.net.in, linux-kernel@vger.kernel.org,
	devel@driverdev.osuosl.org,
	Moritz Fischer <moritz.fischer@ettus.com>
Subject: [RFC 1/2] misc: Add of_get_misc get a reference from devicetree
Date: Tue, 22 Mar 2016 15:33:50 -0700	[thread overview]
Message-ID: <1458686031-31031-2-git-send-email-moritz.fischer@ettus.com> (raw)
In-Reply-To: <1458686031-31031-1-git-send-email-moritz.fischer@ettus.com>

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@ettus.com>
---
 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

  reply	other threads:[~2016-03-22 22:34 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-22 22:33 [RFC 0/2] staging: ion: of_ion_device_get Moritz Fischer
2016-03-22 22:33 ` Moritz Fischer [this message]
2016-03-22 22:54   ` [RFC 1/2] misc: Add of_get_misc get a reference from devicetree Moritz Fischer
2016-03-22 22:33 ` [RFC 2/2] staging: android: ion: Add of_ion_device_get function Moritz Fischer
2016-03-22 22:50   ` Moritz Fischer
2016-03-23  4:55   ` Dan Carpenter
2016-03-22 22:51 ` [RFC 0/2] staging: ion: of_ion_device_get Laura Abbott
2016-03-22 23:08   ` Moritz Fischer
2016-03-22 23:20     ` Laura Abbott
2016-03-22 23:38       ` Moritz Fischer
2016-03-22 23:40       ` Moritz Fischer

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=1458686031-31031-2-git-send-email-moritz.fischer@ettus.com \
    --to=moritz.fischer@ettus.com \
    --cc=arnd@arndb.de \
    --cc=arve@android.com \
    --cc=dan.carpenter@oracle.com \
    --cc=devel@driverdev.osuosl.org \
    --cc=gioh.kim@lge.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=labbott@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=riandrews@android.com \
    --cc=sriram@marirs.net.in \
    --cc=sumit.semwal@linaro.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®