Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752962AbcCVWeH (ORCPT ); Tue, 22 Mar 2016 18:34:07 -0400 Received: from mail-pf0-f173.google.com ([209.85.192.173]:36271 "EHLO mail-pf0-f173.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752668AbcCVWeD (ORCPT ); Tue, 22 Mar 2016 18:34:03 -0400 From: Moritz Fischer 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 Subject: [RFC 1/2] misc: Add of_get_misc get a reference from devicetree Date: Tue, 22 Mar 2016 15:33:50 -0700 Message-Id: <1458686031-31031-2-git-send-email-moritz.fischer@ettus.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1458686031-31031-1-git-send-email-moritz.fischer@ettus.com> References: <1458686031-31031-1-git-send-email-moritz.fischer@ettus.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2001 Lines: 82 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 --- 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