linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sakari Ailus <sakari.ailus@linux.intel.com>
To: linux-media@vger.kernel.org
Cc: niklas.soderlund@ragnatech.se, maxime.ripard@free-electrons.com,
	hverkuil@xs4all.nl, laurent.pinchart@ideasonboard.com,
	pavel@ucw.cz, sre@kernel.org
Subject: [PATCH v15 19/32] v4l: async: Ensure only unique fwnodes are registered to notifiers
Date: Thu,  5 Oct 2017 00:50:38 +0300	[thread overview]
Message-ID: <20171004215051.13385-20-sakari.ailus@linux.intel.com> (raw)
In-Reply-To: <20171004215051.13385-1-sakari.ailus@linux.intel.com>

While registering a notifier, check that each newly added fwnode is
unique, and return an error if it is not. Also check that a newly added
notifier does not have the same fwnodes twice.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Acked-by: Hans Verkuil <hans.verkuil@cisco.com>
---
 drivers/media/v4l2-core/v4l2-async.c | 82 +++++++++++++++++++++++++++++++++---
 1 file changed, 77 insertions(+), 5 deletions(-)

diff --git a/drivers/media/v4l2-core/v4l2-async.c b/drivers/media/v4l2-core/v4l2-async.c
index 3621ed6e6e3c..e1fe3567127a 100644
--- a/drivers/media/v4l2-core/v4l2-async.c
+++ b/drivers/media/v4l2-core/v4l2-async.c
@@ -330,8 +330,71 @@ static void v4l2_async_notifier_unbind_all_subdevs(
 	notifier->parent = NULL;
 }
 
+/* See if an fwnode can be found in a notifier's lists. */
+static bool __v4l2_async_notifier_fwnode_has_async_subdev(
+	struct v4l2_async_notifier *notifier, struct fwnode_handle *fwnode)
+{
+	struct v4l2_async_subdev *asd;
+	struct v4l2_subdev *sd;
+
+	list_for_each_entry(asd, &notifier->waiting, list) {
+		if (asd->match_type != V4L2_ASYNC_MATCH_FWNODE)
+			continue;
+
+		if (asd->match.fwnode.fwnode == fwnode)
+			return true;
+	}
+
+	list_for_each_entry(sd, &notifier->done, async_list) {
+		if (WARN_ON(!sd->asd))
+			continue;
+
+		if (sd->asd->match_type != V4L2_ASYNC_MATCH_FWNODE)
+			continue;
+
+		if (sd->asd->match.fwnode.fwnode == fwnode)
+			return true;
+	}
+
+	return false;
+}
+
+/*
+ * Find out whether an async sub-device was set up for an fwnode already or
+ * whether it exists in a given notifier before @this_index.
+ */
+static bool v4l2_async_notifier_fwnode_has_async_subdev(
+	struct v4l2_async_notifier *notifier, struct fwnode_handle *fwnode,
+	unsigned int this_index)
+{
+	unsigned int j;
+
+	lockdep_assert_held(&list_lock);
+
+	/* Check that an fwnode is not being added more than once. */
+	for (j = 0; j < this_index; j++) {
+		struct v4l2_async_subdev *asd = notifier->subdevs[this_index];
+		struct v4l2_async_subdev *other_asd = notifier->subdevs[j];
+
+		if (other_asd->match_type == V4L2_ASYNC_MATCH_FWNODE &&
+		    asd->match.fwnode.fwnode ==
+		    other_asd->match.fwnode.fwnode)
+			return true;
+	}
+
+	/* Check than an fwnode did not exist in other notifiers. */
+	list_for_each_entry(notifier, &notifier_list, list)
+		if (__v4l2_async_notifier_fwnode_has_async_subdev(
+			    notifier, fwnode))
+			return true;
+
+	return false;
+}
+
 static int __v4l2_async_notifier_register(struct v4l2_async_notifier *notifier)
 {
+	struct device *dev =
+		notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL;
 	struct v4l2_async_subdev *asd;
 	int ret;
 	int i;
@@ -342,6 +405,8 @@ static int __v4l2_async_notifier_register(struct v4l2_async_notifier *notifier)
 	INIT_LIST_HEAD(&notifier->waiting);
 	INIT_LIST_HEAD(&notifier->done);
 
+	mutex_lock(&list_lock);
+
 	for (i = 0; i < notifier->num_subdevs; i++) {
 		asd = notifier->subdevs[i];
 
@@ -349,19 +414,25 @@ static int __v4l2_async_notifier_register(struct v4l2_async_notifier *notifier)
 		case V4L2_ASYNC_MATCH_CUSTOM:
 		case V4L2_ASYNC_MATCH_DEVNAME:
 		case V4L2_ASYNC_MATCH_I2C:
+			break;
 		case V4L2_ASYNC_MATCH_FWNODE:
+			if (v4l2_async_notifier_fwnode_has_async_subdev(
+				    notifier, asd->match.fwnode.fwnode, i)) {
+				dev_err(dev,
+					"fwnode has already been registered or in notifier's subdev list\n");
+				ret = -EEXIST;
+				goto out_unlock;
+			}
 			break;
 		default:
-			dev_err(notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL,
-				"Invalid match type %u on %p\n",
+			dev_err(dev, "Invalid match type %u on %p\n",
 				asd->match_type, asd);
-			return -EINVAL;
+			ret = -EINVAL;
+			goto out_unlock;
 		}
 		list_add_tail(&asd->list, &notifier->waiting);
 	}
 
-	mutex_lock(&list_lock);
-
 	ret = v4l2_async_notifier_try_all_subdevs(notifier);
 	if (ret)
 		goto err_unbind;
@@ -373,6 +444,7 @@ static int __v4l2_async_notifier_register(struct v4l2_async_notifier *notifier)
 	/* Keep also completed notifiers on the list */
 	list_add(&notifier->list, &notifier_list);
 
+out_unlock:
 	mutex_unlock(&list_lock);
 
 	return 0;
-- 
2.11.0

  parent reply	other threads:[~2017-10-04 21:50 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-04 21:50 [PATCH v15 00/32] Unified fwnode endpoint parser, async sub-device notifier support, N9 flash DTS Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 01/32] v4l: async: Remove re-probing support Sakari Ailus
2017-10-08 21:50   ` Sebastian Reichel
2017-10-09 11:22   ` Mauro Carvalho Chehab
2017-10-09 14:06     ` Sakari Ailus
2017-10-09 14:08       ` Hans Verkuil
2017-10-09 14:18         ` Sakari Ailus
2017-10-09 14:20           ` Hans Verkuil
2017-10-09 15:27             ` Mauro Carvalho Chehab
     [not found]           ` <CGME20171009164457epcas1p3c5e134e4bb5d85498fe8d4f00332f2fc@epcas1p3.samsung.com>
2017-10-09 16:44             ` Sylwester Nawrocki
2017-10-09 19:18               ` Laurent Pinchart
2017-10-10 12:18               ` Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 02/32] v4l: async: Don't set sd->dev NULL in v4l2_async_cleanup Sakari Ailus
2017-10-08 21:50   ` Sebastian Reichel
2017-10-09 11:21   ` Hans Verkuil
2017-10-04 21:50 ` [PATCH v15 03/32] v4l: async: fix unbind error in v4l2_async_notifier_unregister() Sakari Ailus
2017-10-08 21:50   ` Sebastian Reichel
2017-10-09 11:23   ` Hans Verkuil
2017-10-04 21:50 ` [PATCH v15 04/32] v4l: async: Fix notifier complete callback error handling Sakari Ailus
2017-10-09 11:45   ` Hans Verkuil
2017-10-10 12:57     ` Sakari Ailus
2017-10-10 13:18       ` Hans Verkuil
2017-10-10 14:21         ` Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 05/32] v4l: async: Correctly serialise async sub-device unregistration Sakari Ailus
2017-10-08 21:52   ` Sebastian Reichel
2017-10-09 11:45   ` Hans Verkuil
2017-10-04 21:50 ` [PATCH v15 06/32] v4l: async: Use more intuitive names for internal functions Sakari Ailus
2017-10-08 21:53   ` Sebastian Reichel
2017-10-04 21:50 ` [PATCH v15 07/32] v4l: async: Add V4L2 async documentation to the documentation build Sakari Ailus
2017-10-08 21:54   ` Sebastian Reichel
2017-10-09 11:31   ` Mauro Carvalho Chehab
2017-10-04 21:50 ` [PATCH v15 08/32] v4l: fwnode: Support generic parsing of graph endpoints in a device Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 09/32] omap3isp: Use generic parser for parsing fwnode endpoints Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 10/32] rcar-vin: " Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 11/32] omap3isp: Fix check for our own sub-devices Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 12/32] omap3isp: Print the name of the entity where no source pads could be found Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 13/32] v4l: async: Move async subdev notifier operations to a separate structure Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 14/32] v4l: async: Introduce helpers for calling async ops callbacks Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 15/32] v4l: async: Register sub-devices before calling bound callback Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 16/32] v4l: async: Allow async notifier register call succeed with no subdevs Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 17/32] v4l: async: Prepare for async sub-device notifiers Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 18/32] v4l: async: Allow binding notifiers to sub-devices Sakari Ailus
2017-10-04 21:50 ` Sakari Ailus [this message]
2017-10-04 21:50 ` [PATCH v15 20/32] dt: bindings: Add a binding for flash LED devices associated to a sensor Sakari Ailus
2017-10-13 13:41   ` Rob Herring
2017-10-04 21:50 ` [PATCH v15 21/32] dt: bindings: Add lens-focus binding for image sensors Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 22/32] v4l: fwnode: Move KernelDoc documentation to the header Sakari Ailus
2017-10-05 12:03   ` [PATCH v15.1 " Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 23/32] v4l: fwnode: Add a helper function for parsing generic references Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 24/32] v4l: fwnode: Add a helper function to obtain device / integer references Sakari Ailus
2017-10-18 13:56   ` [PATCH v15.1 " Sakari Ailus
2017-10-18 15:32     ` Sakari Ailus
2017-10-19  6:52       ` Hans Verkuil
2017-10-20 11:35         ` Sakari Ailus
2017-10-24 20:32         ` [PATCH v15.2 " Sakari Ailus
2017-10-24 20:36           ` Sakari Ailus
2017-10-25  7:28           ` Hans Verkuil
2017-10-04 21:50 ` [PATCH v15 25/32] v4l: fwnode: Add convenience function for parsing common external refs Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 26/32] v4l: fwnode: Add a convenience function for registering sensors Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 27/32] dt: bindings: smiapp: Document lens-focus and flash-leds properties Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 28/32] smiapp: Add support for flash and lens devices Sakari Ailus
2017-10-06 11:21   ` Pavel Machek
2017-10-04 21:50 ` [PATCH v15 29/32] et8ek8: " Sakari Ailus
2017-10-06 11:21   ` Pavel Machek
2017-10-04 21:50 ` [PATCH v15 30/32] ov5670: " Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 31/32] ov13858: " Sakari Ailus
2017-10-04 21:50 ` [PATCH v15 32/32] arm: dts: omap3: N9/N950: Add flash references to the camera Sakari Ailus
2017-10-05  6:11 ` [PATCH v15 00/32] Unified fwnode endpoint parser, async sub-device notifier support, N9 flash DTS Sakari Ailus

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=20171004215051.13385-20-sakari.ailus@linux.intel.com \
    --to=sakari.ailus@linux.intel.com \
    --cc=hverkuil@xs4all.nl \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=linux-media@vger.kernel.org \
    --cc=maxime.ripard@free-electrons.com \
    --cc=niklas.soderlund@ragnatech.se \
    --cc=pavel@ucw.cz \
    --cc=sre@kernel.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;
as well as URLs for NNTP newsgroup(s).