2 * V4L2 asynchronous subdevice registration API
4 * Copyright (C) 2012-2013, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/i2c.h>
14 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21 #include <linux/types.h>
23 #include <media/v4l2-async.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-fwnode.h>
26 #include <media/v4l2-subdev.h>
28 static int v4l2_async_notifier_call_bound(struct v4l2_async_notifier *n,
29 struct v4l2_subdev *subdev,
30 struct v4l2_async_subdev *asd)
32 if (!n->ops || !n->ops->bound)
35 return n->ops->bound(n, subdev, asd);
38 static void v4l2_async_notifier_call_unbind(struct v4l2_async_notifier *n,
39 struct v4l2_subdev *subdev,
40 struct v4l2_async_subdev *asd)
42 if (!n->ops || !n->ops->unbind)
45 n->ops->unbind(n, subdev, asd);
48 static int v4l2_async_notifier_call_complete(struct v4l2_async_notifier *n)
50 if (!n->ops || !n->ops->complete)
53 return n->ops->complete(n);
56 static bool match_i2c(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
58 #if IS_ENABLED(CONFIG_I2C)
59 struct i2c_client *client = i2c_verify_client(sd->dev);
62 asd->match.i2c.adapter_id == client->adapter->nr &&
63 asd->match.i2c.address == client->addr;
69 static bool match_devname(struct v4l2_subdev *sd,
70 struct v4l2_async_subdev *asd)
72 return !strcmp(asd->match.device_name, dev_name(sd->dev));
75 static bool match_fwnode(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
77 return sd->fwnode == asd->match.fwnode;
80 static bool match_custom(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
82 if (!asd->match.custom.match)
86 return asd->match.custom.match(sd->dev, asd);
89 static LIST_HEAD(subdev_list);
90 static LIST_HEAD(notifier_list);
91 static DEFINE_MUTEX(list_lock);
93 static struct v4l2_async_subdev *
94 v4l2_async_find_match(struct v4l2_async_notifier *notifier,
95 struct v4l2_subdev *sd)
97 bool (*match)(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd);
98 struct v4l2_async_subdev *asd;
100 list_for_each_entry(asd, ¬ifier->waiting, list) {
101 /* bus_type has been verified valid before */
102 switch (asd->match_type) {
103 case V4L2_ASYNC_MATCH_CUSTOM:
104 match = match_custom;
106 case V4L2_ASYNC_MATCH_DEVNAME:
107 match = match_devname;
109 case V4L2_ASYNC_MATCH_I2C:
112 case V4L2_ASYNC_MATCH_FWNODE:
113 match = match_fwnode;
116 /* Cannot happen, unless someone breaks us */
121 /* match cannot be NULL here */
129 /* Compare two async sub-device descriptors for equivalence */
130 static bool asd_equal(struct v4l2_async_subdev *asd_x,
131 struct v4l2_async_subdev *asd_y)
133 if (asd_x->match_type != asd_y->match_type)
136 switch (asd_x->match_type) {
137 case V4L2_ASYNC_MATCH_DEVNAME:
138 return strcmp(asd_x->match.device_name,
139 asd_y->match.device_name) == 0;
140 case V4L2_ASYNC_MATCH_I2C:
141 return asd_x->match.i2c.adapter_id ==
142 asd_y->match.i2c.adapter_id &&
143 asd_x->match.i2c.address ==
144 asd_y->match.i2c.address;
145 case V4L2_ASYNC_MATCH_FWNODE:
146 return asd_x->match.fwnode == asd_y->match.fwnode;
154 /* Find the sub-device notifier registered by a sub-device driver. */
155 static struct v4l2_async_notifier *
156 v4l2_async_find_subdev_notifier(struct v4l2_subdev *sd)
158 struct v4l2_async_notifier *n;
160 list_for_each_entry(n, ¬ifier_list, list)
167 /* Get v4l2_device related to the notifier if one can be found. */
168 static struct v4l2_device *
169 v4l2_async_notifier_find_v4l2_dev(struct v4l2_async_notifier *notifier)
171 while (notifier->parent)
172 notifier = notifier->parent;
174 return notifier->v4l2_dev;
178 * Return true if all child sub-device notifiers are complete, false otherwise.
181 v4l2_async_notifier_can_complete(struct v4l2_async_notifier *notifier)
183 struct v4l2_subdev *sd;
185 if (!list_empty(¬ifier->waiting))
188 list_for_each_entry(sd, ¬ifier->done, async_list) {
189 struct v4l2_async_notifier *subdev_notifier =
190 v4l2_async_find_subdev_notifier(sd);
192 if (subdev_notifier &&
193 !v4l2_async_notifier_can_complete(subdev_notifier))
201 * Complete the master notifier if possible. This is done when all async
202 * sub-devices have been bound; v4l2_device is also available then.
205 v4l2_async_notifier_try_complete(struct v4l2_async_notifier *notifier)
207 /* Quick check whether there are still more sub-devices here. */
208 if (!list_empty(¬ifier->waiting))
211 /* Check the entire notifier tree; find the root notifier first. */
212 while (notifier->parent)
213 notifier = notifier->parent;
215 /* This is root if it has v4l2_dev. */
216 if (!notifier->v4l2_dev)
219 /* Is everything ready? */
220 if (!v4l2_async_notifier_can_complete(notifier))
223 return v4l2_async_notifier_call_complete(notifier);
227 v4l2_async_notifier_try_all_subdevs(struct v4l2_async_notifier *notifier);
229 static int v4l2_async_match_notify(struct v4l2_async_notifier *notifier,
230 struct v4l2_device *v4l2_dev,
231 struct v4l2_subdev *sd,
232 struct v4l2_async_subdev *asd)
234 struct v4l2_async_notifier *subdev_notifier;
237 ret = v4l2_device_register_subdev(v4l2_dev, sd);
241 ret = v4l2_async_notifier_call_bound(notifier, sd, asd);
243 v4l2_device_unregister_subdev(sd);
247 /* Remove from the waiting list */
248 list_del(&asd->list);
250 sd->notifier = notifier;
252 /* Move from the global subdevice list to notifier's done */
253 list_move(&sd->async_list, ¬ifier->done);
256 * See if the sub-device has a notifier. If not, return here.
258 subdev_notifier = v4l2_async_find_subdev_notifier(sd);
259 if (!subdev_notifier || subdev_notifier->parent)
263 * Proceed with checking for the sub-device notifier's async
264 * sub-devices, and return the result. The error will be handled by the
267 subdev_notifier->parent = notifier;
269 return v4l2_async_notifier_try_all_subdevs(subdev_notifier);
272 /* Test all async sub-devices in a notifier for a match. */
274 v4l2_async_notifier_try_all_subdevs(struct v4l2_async_notifier *notifier)
276 struct v4l2_device *v4l2_dev =
277 v4l2_async_notifier_find_v4l2_dev(notifier);
278 struct v4l2_subdev *sd;
284 list_for_each_entry(sd, &subdev_list, async_list) {
285 struct v4l2_async_subdev *asd;
288 asd = v4l2_async_find_match(notifier, sd);
292 ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd);
297 * v4l2_async_match_notify() may lead to registering a
298 * new notifier and thus changing the async subdevs
299 * list. In order to proceed safely from here, restart
300 * parsing the list from the beginning.
308 static void v4l2_async_cleanup(struct v4l2_subdev *sd)
310 v4l2_device_unregister_subdev(sd);
312 * Subdevice driver will reprobe and put the subdev back
315 list_del_init(&sd->async_list);
319 /* Unbind all sub-devices in the notifier tree. */
321 v4l2_async_notifier_unbind_all_subdevs(struct v4l2_async_notifier *notifier)
323 struct v4l2_subdev *sd, *tmp;
325 list_for_each_entry_safe(sd, tmp, ¬ifier->done, async_list) {
326 struct v4l2_async_notifier *subdev_notifier =
327 v4l2_async_find_subdev_notifier(sd);
330 v4l2_async_notifier_unbind_all_subdevs(subdev_notifier);
332 v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
333 v4l2_async_cleanup(sd);
335 list_move(&sd->async_list, &subdev_list);
338 notifier->parent = NULL;
341 /* See if an async sub-device can be found in a notifier's lists. */
343 __v4l2_async_notifier_has_async_subdev(struct v4l2_async_notifier *notifier,
344 struct v4l2_async_subdev *asd)
346 struct v4l2_async_subdev *asd_y;
347 struct v4l2_subdev *sd;
349 list_for_each_entry(asd_y, ¬ifier->waiting, list)
350 if (asd_equal(asd, asd_y))
353 list_for_each_entry(sd, ¬ifier->done, async_list) {
354 if (WARN_ON(!sd->asd))
357 if (asd_equal(asd, sd->asd))
365 * Find out whether an async sub-device was set up already or
366 * whether it exists in a given notifier before @this_index.
367 * If @this_index < 0, search the notifier's entire @asd_list.
370 v4l2_async_notifier_has_async_subdev(struct v4l2_async_notifier *notifier,
371 struct v4l2_async_subdev *asd,
374 struct v4l2_async_subdev *asd_y;
377 lockdep_assert_held(&list_lock);
379 /* Check that an asd is not being added more than once. */
380 list_for_each_entry(asd_y, ¬ifier->asd_list, asd_list) {
381 if (this_index >= 0 && j++ >= this_index)
383 if (asd_equal(asd, asd_y))
387 /* Check that an asd does not exist in other notifiers. */
388 list_for_each_entry(notifier, ¬ifier_list, list)
389 if (__v4l2_async_notifier_has_async_subdev(notifier, asd))
395 static int v4l2_async_notifier_asd_valid(struct v4l2_async_notifier *notifier,
396 struct v4l2_async_subdev *asd,
400 notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL;
405 switch (asd->match_type) {
406 case V4L2_ASYNC_MATCH_CUSTOM:
407 case V4L2_ASYNC_MATCH_DEVNAME:
408 case V4L2_ASYNC_MATCH_I2C:
409 case V4L2_ASYNC_MATCH_FWNODE:
410 if (v4l2_async_notifier_has_async_subdev(notifier, asd,
412 dev_dbg(dev, "subdev descriptor already listed in this or other notifiers\n");
417 dev_err(dev, "Invalid match type %u on %p\n",
418 asd->match_type, asd);
425 void v4l2_async_notifier_init(struct v4l2_async_notifier *notifier)
427 INIT_LIST_HEAD(¬ifier->asd_list);
429 EXPORT_SYMBOL(v4l2_async_notifier_init);
431 static int __v4l2_async_notifier_register(struct v4l2_async_notifier *notifier)
433 struct v4l2_async_subdev *asd;
436 INIT_LIST_HEAD(¬ifier->waiting);
437 INIT_LIST_HEAD(¬ifier->done);
439 mutex_lock(&list_lock);
441 list_for_each_entry(asd, ¬ifier->asd_list, asd_list) {
442 ret = v4l2_async_notifier_asd_valid(notifier, asd, i++);
446 list_add_tail(&asd->list, ¬ifier->waiting);
449 ret = v4l2_async_notifier_try_all_subdevs(notifier);
453 ret = v4l2_async_notifier_try_complete(notifier);
457 /* Keep also completed notifiers on the list */
458 list_add(¬ifier->list, ¬ifier_list);
460 mutex_unlock(&list_lock);
466 * On failure, unbind all sub-devices registered through this notifier.
468 v4l2_async_notifier_unbind_all_subdevs(notifier);
471 mutex_unlock(&list_lock);
476 int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
477 struct v4l2_async_notifier *notifier)
481 if (WARN_ON(!v4l2_dev || notifier->sd))
484 notifier->v4l2_dev = v4l2_dev;
486 ret = __v4l2_async_notifier_register(notifier);
488 notifier->v4l2_dev = NULL;
492 EXPORT_SYMBOL(v4l2_async_notifier_register);
494 int v4l2_async_subdev_notifier_register(struct v4l2_subdev *sd,
495 struct v4l2_async_notifier *notifier)
499 if (WARN_ON(!sd || notifier->v4l2_dev))
504 ret = __v4l2_async_notifier_register(notifier);
510 EXPORT_SYMBOL(v4l2_async_subdev_notifier_register);
513 __v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
515 if (!notifier || (!notifier->v4l2_dev && !notifier->sd))
518 v4l2_async_notifier_unbind_all_subdevs(notifier);
521 notifier->v4l2_dev = NULL;
523 list_del(¬ifier->list);
526 void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
528 mutex_lock(&list_lock);
530 __v4l2_async_notifier_unregister(notifier);
532 mutex_unlock(&list_lock);
534 EXPORT_SYMBOL(v4l2_async_notifier_unregister);
536 static void __v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier)
538 struct v4l2_async_subdev *asd, *tmp;
543 list_for_each_entry_safe(asd, tmp, ¬ifier->asd_list, asd_list) {
544 switch (asd->match_type) {
545 case V4L2_ASYNC_MATCH_FWNODE:
546 fwnode_handle_put(asd->match.fwnode);
552 list_del(&asd->asd_list);
557 void v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier)
559 mutex_lock(&list_lock);
561 __v4l2_async_notifier_cleanup(notifier);
563 mutex_unlock(&list_lock);
565 EXPORT_SYMBOL_GPL(v4l2_async_notifier_cleanup);
567 int v4l2_async_notifier_add_subdev(struct v4l2_async_notifier *notifier,
568 struct v4l2_async_subdev *asd)
572 mutex_lock(&list_lock);
574 ret = v4l2_async_notifier_asd_valid(notifier, asd, -1);
578 list_add_tail(&asd->asd_list, ¬ifier->asd_list);
581 mutex_unlock(&list_lock);
584 EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_subdev);
586 struct v4l2_async_subdev *
587 v4l2_async_notifier_add_fwnode_subdev(struct v4l2_async_notifier *notifier,
588 struct fwnode_handle *fwnode,
589 unsigned int asd_struct_size)
591 struct v4l2_async_subdev *asd;
594 asd = kzalloc(asd_struct_size, GFP_KERNEL);
596 return ERR_PTR(-ENOMEM);
598 asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
599 asd->match.fwnode = fwnode;
601 ret = v4l2_async_notifier_add_subdev(notifier, asd);
609 EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_fwnode_subdev);
611 struct v4l2_async_subdev *
612 v4l2_async_notifier_add_i2c_subdev(struct v4l2_async_notifier *notifier,
613 int adapter_id, unsigned short address,
614 unsigned int asd_struct_size)
616 struct v4l2_async_subdev *asd;
619 asd = kzalloc(asd_struct_size, GFP_KERNEL);
621 return ERR_PTR(-ENOMEM);
623 asd->match_type = V4L2_ASYNC_MATCH_I2C;
624 asd->match.i2c.adapter_id = adapter_id;
625 asd->match.i2c.address = address;
627 ret = v4l2_async_notifier_add_subdev(notifier, asd);
635 EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_i2c_subdev);
637 struct v4l2_async_subdev *
638 v4l2_async_notifier_add_devname_subdev(struct v4l2_async_notifier *notifier,
639 const char *device_name,
640 unsigned int asd_struct_size)
642 struct v4l2_async_subdev *asd;
645 asd = kzalloc(asd_struct_size, GFP_KERNEL);
647 return ERR_PTR(-ENOMEM);
649 asd->match_type = V4L2_ASYNC_MATCH_DEVNAME;
650 asd->match.device_name = device_name;
652 ret = v4l2_async_notifier_add_subdev(notifier, asd);
660 EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_devname_subdev);
662 int v4l2_async_register_subdev(struct v4l2_subdev *sd)
664 struct v4l2_async_notifier *subdev_notifier;
665 struct v4l2_async_notifier *notifier;
669 * No reference taken. The reference is held by the device
670 * (struct v4l2_subdev.dev), and async sub-device does not
671 * exist independently of the device at any point of time.
673 if (!sd->fwnode && sd->dev)
674 sd->fwnode = dev_fwnode(sd->dev);
676 mutex_lock(&list_lock);
678 INIT_LIST_HEAD(&sd->async_list);
680 list_for_each_entry(notifier, ¬ifier_list, list) {
681 struct v4l2_device *v4l2_dev =
682 v4l2_async_notifier_find_v4l2_dev(notifier);
683 struct v4l2_async_subdev *asd;
688 asd = v4l2_async_find_match(notifier, sd);
692 ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd);
696 ret = v4l2_async_notifier_try_complete(notifier);
703 /* None matched, wait for hot-plugging */
704 list_add(&sd->async_list, &subdev_list);
707 mutex_unlock(&list_lock);
713 * Complete failed. Unbind the sub-devices bound through registering
714 * this async sub-device.
716 subdev_notifier = v4l2_async_find_subdev_notifier(sd);
718 v4l2_async_notifier_unbind_all_subdevs(subdev_notifier);
721 v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
722 v4l2_async_cleanup(sd);
724 mutex_unlock(&list_lock);
728 EXPORT_SYMBOL(v4l2_async_register_subdev);
730 void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
732 mutex_lock(&list_lock);
734 __v4l2_async_notifier_unregister(sd->subdev_notifier);
735 __v4l2_async_notifier_cleanup(sd->subdev_notifier);
736 kfree(sd->subdev_notifier);
737 sd->subdev_notifier = NULL;
740 struct v4l2_async_notifier *notifier = sd->notifier;
742 list_add(&sd->asd->list, ¬ifier->waiting);
744 v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
747 v4l2_async_cleanup(sd);
749 mutex_unlock(&list_lock);
751 EXPORT_SYMBOL(v4l2_async_unregister_subdev);