1 // SPDX-License-Identifier: GPL-2.0-only
3 * V4L2 asynchronous subdevice registration API
8 #include <linux/debugfs.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/i2c.h>
12 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
17 #include <linux/platform_device.h>
18 #include <linux/seq_file.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
22 #include <media/v4l2-async.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-fwnode.h>
25 #include <media/v4l2-subdev.h>
27 static int v4l2_async_nf_call_bound(struct v4l2_async_notifier *n,
28 struct v4l2_subdev *subdev,
29 struct v4l2_async_subdev *asd)
31 if (!n->ops || !n->ops->bound)
34 return n->ops->bound(n, subdev, asd);
37 static void v4l2_async_nf_call_unbind(struct v4l2_async_notifier *n,
38 struct v4l2_subdev *subdev,
39 struct v4l2_async_subdev *asd)
41 if (!n->ops || !n->ops->unbind)
44 n->ops->unbind(n, subdev, asd);
47 static int v4l2_async_nf_call_complete(struct v4l2_async_notifier *n)
49 if (!n->ops || !n->ops->complete)
52 return n->ops->complete(n);
55 static bool match_i2c(struct v4l2_async_notifier *notifier,
56 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_fwnode(struct v4l2_async_notifier *notifier,
70 struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
72 struct fwnode_handle *other_fwnode;
73 struct fwnode_handle *dev_fwnode;
74 bool asd_fwnode_is_ep;
79 * Both the subdev and the async subdev can provide either an endpoint
80 * fwnode or a device fwnode. Start with the simple case of direct
83 if (sd->fwnode == asd->match.fwnode)
87 * Check the same situation for any possible secondary assigned to the
90 if (!IS_ERR_OR_NULL(sd->fwnode->secondary) &&
91 sd->fwnode->secondary == asd->match.fwnode)
95 * Otherwise, check if the sd fwnode and the asd fwnode refer to an
96 * endpoint or a device. If they're of the same type, there's no match.
97 * Technically speaking this checks if the nodes refer to a connected
98 * endpoint, which is the simplest check that works for both OF and
99 * ACPI. This won't make a difference, as drivers should not try to
100 * match unconnected endpoints.
102 sd_fwnode_is_ep = fwnode_graph_is_endpoint(sd->fwnode);
103 asd_fwnode_is_ep = fwnode_graph_is_endpoint(asd->match.fwnode);
105 if (sd_fwnode_is_ep == asd_fwnode_is_ep)
109 * The sd and asd fwnodes are of different types. Get the device fwnode
110 * parent of the endpoint fwnode, and compare it with the other fwnode.
112 if (sd_fwnode_is_ep) {
113 dev_fwnode = fwnode_graph_get_port_parent(sd->fwnode);
114 other_fwnode = asd->match.fwnode;
116 dev_fwnode = fwnode_graph_get_port_parent(asd->match.fwnode);
117 other_fwnode = sd->fwnode;
120 fwnode_handle_put(dev_fwnode);
122 if (dev_fwnode != other_fwnode)
126 * We have a heterogeneous match. Retrieve the struct device of the side
127 * that matched on a device fwnode to print its driver name.
130 dev = notifier->v4l2_dev ? notifier->v4l2_dev->dev
135 if (dev && dev->driver) {
137 dev_warn(dev, "Driver %s uses device fwnode, incorrect match may occur\n",
139 dev_notice(dev, "Consider updating driver %s to match on endpoints\n",
146 static LIST_HEAD(subdev_list);
147 static LIST_HEAD(notifier_list);
148 static DEFINE_MUTEX(list_lock);
150 static struct v4l2_async_subdev *
151 v4l2_async_find_match(struct v4l2_async_notifier *notifier,
152 struct v4l2_subdev *sd)
154 bool (*match)(struct v4l2_async_notifier *notifier,
155 struct v4l2_subdev *sd, struct v4l2_async_subdev *asd);
156 struct v4l2_async_subdev *asd;
158 list_for_each_entry(asd, ¬ifier->waiting, list) {
159 /* bus_type has been verified valid before */
160 switch (asd->match_type) {
161 case V4L2_ASYNC_MATCH_I2C:
164 case V4L2_ASYNC_MATCH_FWNODE:
165 match = match_fwnode;
168 /* Cannot happen, unless someone breaks us */
173 /* match cannot be NULL here */
174 if (match(notifier, sd, asd))
181 /* Compare two async sub-device descriptors for equivalence */
182 static bool asd_equal(struct v4l2_async_subdev *asd_x,
183 struct v4l2_async_subdev *asd_y)
185 if (asd_x->match_type != asd_y->match_type)
188 switch (asd_x->match_type) {
189 case V4L2_ASYNC_MATCH_I2C:
190 return asd_x->match.i2c.adapter_id ==
191 asd_y->match.i2c.adapter_id &&
192 asd_x->match.i2c.address ==
193 asd_y->match.i2c.address;
194 case V4L2_ASYNC_MATCH_FWNODE:
195 return asd_x->match.fwnode == asd_y->match.fwnode;
203 /* Find the sub-device notifier registered by a sub-device driver. */
204 static struct v4l2_async_notifier *
205 v4l2_async_find_subdev_notifier(struct v4l2_subdev *sd)
207 struct v4l2_async_notifier *n;
209 list_for_each_entry(n, ¬ifier_list, list)
216 /* Get v4l2_device related to the notifier if one can be found. */
217 static struct v4l2_device *
218 v4l2_async_nf_find_v4l2_dev(struct v4l2_async_notifier *notifier)
220 while (notifier->parent)
221 notifier = notifier->parent;
223 return notifier->v4l2_dev;
227 * Return true if all child sub-device notifiers are complete, false otherwise.
230 v4l2_async_nf_can_complete(struct v4l2_async_notifier *notifier)
232 struct v4l2_subdev *sd;
234 if (!list_empty(¬ifier->waiting))
237 list_for_each_entry(sd, ¬ifier->done, async_list) {
238 struct v4l2_async_notifier *subdev_notifier =
239 v4l2_async_find_subdev_notifier(sd);
241 if (subdev_notifier &&
242 !v4l2_async_nf_can_complete(subdev_notifier))
250 * Complete the master notifier if possible. This is done when all async
251 * sub-devices have been bound; v4l2_device is also available then.
254 v4l2_async_nf_try_complete(struct v4l2_async_notifier *notifier)
256 /* Quick check whether there are still more sub-devices here. */
257 if (!list_empty(¬ifier->waiting))
260 /* Check the entire notifier tree; find the root notifier first. */
261 while (notifier->parent)
262 notifier = notifier->parent;
264 /* This is root if it has v4l2_dev. */
265 if (!notifier->v4l2_dev)
268 /* Is everything ready? */
269 if (!v4l2_async_nf_can_complete(notifier))
272 return v4l2_async_nf_call_complete(notifier);
276 v4l2_async_nf_try_all_subdevs(struct v4l2_async_notifier *notifier);
278 static int v4l2_async_match_notify(struct v4l2_async_notifier *notifier,
279 struct v4l2_device *v4l2_dev,
280 struct v4l2_subdev *sd,
281 struct v4l2_async_subdev *asd)
283 struct v4l2_async_notifier *subdev_notifier;
286 ret = v4l2_device_register_subdev(v4l2_dev, sd);
290 ret = v4l2_async_nf_call_bound(notifier, sd, asd);
292 v4l2_device_unregister_subdev(sd);
296 /* Remove from the waiting list */
297 list_del(&asd->list);
299 sd->notifier = notifier;
301 /* Move from the global subdevice list to notifier's done */
302 list_move(&sd->async_list, ¬ifier->done);
305 * See if the sub-device has a notifier. If not, return here.
307 subdev_notifier = v4l2_async_find_subdev_notifier(sd);
308 if (!subdev_notifier || subdev_notifier->parent)
312 * Proceed with checking for the sub-device notifier's async
313 * sub-devices, and return the result. The error will be handled by the
316 subdev_notifier->parent = notifier;
318 return v4l2_async_nf_try_all_subdevs(subdev_notifier);
321 /* Test all async sub-devices in a notifier for a match. */
323 v4l2_async_nf_try_all_subdevs(struct v4l2_async_notifier *notifier)
325 struct v4l2_device *v4l2_dev =
326 v4l2_async_nf_find_v4l2_dev(notifier);
327 struct v4l2_subdev *sd;
333 list_for_each_entry(sd, &subdev_list, async_list) {
334 struct v4l2_async_subdev *asd;
337 asd = v4l2_async_find_match(notifier, sd);
341 ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd);
346 * v4l2_async_match_notify() may lead to registering a
347 * new notifier and thus changing the async subdevs
348 * list. In order to proceed safely from here, restart
349 * parsing the list from the beginning.
357 static void v4l2_async_cleanup(struct v4l2_subdev *sd)
359 v4l2_device_unregister_subdev(sd);
361 * Subdevice driver will reprobe and put the subdev back
364 list_del_init(&sd->async_list);
368 /* Unbind all sub-devices in the notifier tree. */
370 v4l2_async_nf_unbind_all_subdevs(struct v4l2_async_notifier *notifier)
372 struct v4l2_subdev *sd, *tmp;
374 list_for_each_entry_safe(sd, tmp, ¬ifier->done, async_list) {
375 struct v4l2_async_notifier *subdev_notifier =
376 v4l2_async_find_subdev_notifier(sd);
379 v4l2_async_nf_unbind_all_subdevs(subdev_notifier);
381 v4l2_async_nf_call_unbind(notifier, sd, sd->asd);
382 v4l2_async_cleanup(sd);
384 list_move(&sd->async_list, &subdev_list);
387 notifier->parent = NULL;
390 /* See if an async sub-device can be found in a notifier's lists. */
392 __v4l2_async_nf_has_async_subdev(struct v4l2_async_notifier *notifier,
393 struct v4l2_async_subdev *asd)
395 struct v4l2_async_subdev *asd_y;
396 struct v4l2_subdev *sd;
398 list_for_each_entry(asd_y, ¬ifier->waiting, list)
399 if (asd_equal(asd, asd_y))
402 list_for_each_entry(sd, ¬ifier->done, async_list) {
403 if (WARN_ON(!sd->asd))
406 if (asd_equal(asd, sd->asd))
414 * Find out whether an async sub-device was set up already or
415 * whether it exists in a given notifier before @this_index.
416 * If @this_index < 0, search the notifier's entire @asd_list.
419 v4l2_async_nf_has_async_subdev(struct v4l2_async_notifier *notifier,
420 struct v4l2_async_subdev *asd, int this_index)
422 struct v4l2_async_subdev *asd_y;
425 lockdep_assert_held(&list_lock);
427 /* Check that an asd is not being added more than once. */
428 list_for_each_entry(asd_y, ¬ifier->asd_list, asd_list) {
429 if (this_index >= 0 && j++ >= this_index)
431 if (asd_equal(asd, asd_y))
435 /* Check that an asd does not exist in other notifiers. */
436 list_for_each_entry(notifier, ¬ifier_list, list)
437 if (__v4l2_async_nf_has_async_subdev(notifier, asd))
443 static int v4l2_async_nf_asd_valid(struct v4l2_async_notifier *notifier,
444 struct v4l2_async_subdev *asd,
448 notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL;
453 switch (asd->match_type) {
454 case V4L2_ASYNC_MATCH_I2C:
455 case V4L2_ASYNC_MATCH_FWNODE:
456 if (v4l2_async_nf_has_async_subdev(notifier, asd, this_index)) {
457 dev_dbg(dev, "subdev descriptor already listed in this or other notifiers\n");
462 dev_err(dev, "Invalid match type %u on %p\n",
463 asd->match_type, asd);
470 void v4l2_async_nf_init(struct v4l2_async_notifier *notifier)
472 INIT_LIST_HEAD(¬ifier->asd_list);
474 EXPORT_SYMBOL(v4l2_async_nf_init);
476 static int __v4l2_async_nf_register(struct v4l2_async_notifier *notifier)
478 struct v4l2_async_subdev *asd;
481 INIT_LIST_HEAD(¬ifier->waiting);
482 INIT_LIST_HEAD(¬ifier->done);
484 mutex_lock(&list_lock);
486 list_for_each_entry(asd, ¬ifier->asd_list, asd_list) {
487 ret = v4l2_async_nf_asd_valid(notifier, asd, i++);
491 list_add_tail(&asd->list, ¬ifier->waiting);
494 ret = v4l2_async_nf_try_all_subdevs(notifier);
498 ret = v4l2_async_nf_try_complete(notifier);
502 /* Keep also completed notifiers on the list */
503 list_add(¬ifier->list, ¬ifier_list);
505 mutex_unlock(&list_lock);
511 * On failure, unbind all sub-devices registered through this notifier.
513 v4l2_async_nf_unbind_all_subdevs(notifier);
516 mutex_unlock(&list_lock);
521 int v4l2_async_nf_register(struct v4l2_device *v4l2_dev,
522 struct v4l2_async_notifier *notifier)
526 if (WARN_ON(!v4l2_dev || notifier->sd))
529 notifier->v4l2_dev = v4l2_dev;
531 ret = __v4l2_async_nf_register(notifier);
533 notifier->v4l2_dev = NULL;
537 EXPORT_SYMBOL(v4l2_async_nf_register);
539 int v4l2_async_subdev_nf_register(struct v4l2_subdev *sd,
540 struct v4l2_async_notifier *notifier)
544 if (WARN_ON(!sd || notifier->v4l2_dev))
549 ret = __v4l2_async_nf_register(notifier);
555 EXPORT_SYMBOL(v4l2_async_subdev_nf_register);
558 __v4l2_async_nf_unregister(struct v4l2_async_notifier *notifier)
560 if (!notifier || (!notifier->v4l2_dev && !notifier->sd))
563 v4l2_async_nf_unbind_all_subdevs(notifier);
566 notifier->v4l2_dev = NULL;
568 list_del(¬ifier->list);
571 void v4l2_async_nf_unregister(struct v4l2_async_notifier *notifier)
573 mutex_lock(&list_lock);
575 __v4l2_async_nf_unregister(notifier);
577 mutex_unlock(&list_lock);
579 EXPORT_SYMBOL(v4l2_async_nf_unregister);
581 static void __v4l2_async_nf_cleanup(struct v4l2_async_notifier *notifier)
583 struct v4l2_async_subdev *asd, *tmp;
585 if (!notifier || !notifier->asd_list.next)
588 list_for_each_entry_safe(asd, tmp, ¬ifier->asd_list, asd_list) {
589 switch (asd->match_type) {
590 case V4L2_ASYNC_MATCH_FWNODE:
591 fwnode_handle_put(asd->match.fwnode);
597 list_del(&asd->asd_list);
602 void v4l2_async_nf_cleanup(struct v4l2_async_notifier *notifier)
604 mutex_lock(&list_lock);
606 __v4l2_async_nf_cleanup(notifier);
608 mutex_unlock(&list_lock);
610 EXPORT_SYMBOL_GPL(v4l2_async_nf_cleanup);
612 int __v4l2_async_nf_add_subdev(struct v4l2_async_notifier *notifier,
613 struct v4l2_async_subdev *asd)
617 mutex_lock(&list_lock);
619 ret = v4l2_async_nf_asd_valid(notifier, asd, -1);
623 list_add_tail(&asd->asd_list, ¬ifier->asd_list);
626 mutex_unlock(&list_lock);
629 EXPORT_SYMBOL_GPL(__v4l2_async_nf_add_subdev);
631 struct v4l2_async_subdev *
632 __v4l2_async_nf_add_fwnode(struct v4l2_async_notifier *notifier,
633 struct fwnode_handle *fwnode,
634 unsigned int asd_struct_size)
636 struct v4l2_async_subdev *asd;
639 asd = kzalloc(asd_struct_size, GFP_KERNEL);
641 return ERR_PTR(-ENOMEM);
643 asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
644 asd->match.fwnode = fwnode_handle_get(fwnode);
646 ret = __v4l2_async_nf_add_subdev(notifier, asd);
648 fwnode_handle_put(fwnode);
655 EXPORT_SYMBOL_GPL(__v4l2_async_nf_add_fwnode);
657 struct v4l2_async_subdev *
658 __v4l2_async_nf_add_fwnode_remote(struct v4l2_async_notifier *notif,
659 struct fwnode_handle *endpoint,
660 unsigned int asd_struct_size)
662 struct v4l2_async_subdev *asd;
663 struct fwnode_handle *remote;
665 remote = fwnode_graph_get_remote_port_parent(endpoint);
667 return ERR_PTR(-ENOTCONN);
669 asd = __v4l2_async_nf_add_fwnode(notif, remote, asd_struct_size);
671 * Calling __v4l2_async_nf_add_fwnode grabs a refcount,
672 * so drop the one we got in fwnode_graph_get_remote_port_parent.
674 fwnode_handle_put(remote);
677 EXPORT_SYMBOL_GPL(__v4l2_async_nf_add_fwnode_remote);
679 struct v4l2_async_subdev *
680 __v4l2_async_nf_add_i2c(struct v4l2_async_notifier *notifier, int adapter_id,
681 unsigned short address, unsigned int asd_struct_size)
683 struct v4l2_async_subdev *asd;
686 asd = kzalloc(asd_struct_size, GFP_KERNEL);
688 return ERR_PTR(-ENOMEM);
690 asd->match_type = V4L2_ASYNC_MATCH_I2C;
691 asd->match.i2c.adapter_id = adapter_id;
692 asd->match.i2c.address = address;
694 ret = __v4l2_async_nf_add_subdev(notifier, asd);
702 EXPORT_SYMBOL_GPL(__v4l2_async_nf_add_i2c);
704 int v4l2_async_register_subdev(struct v4l2_subdev *sd)
706 struct v4l2_async_notifier *subdev_notifier;
707 struct v4l2_async_notifier *notifier;
711 * No reference taken. The reference is held by the device
712 * (struct v4l2_subdev.dev), and async sub-device does not
713 * exist independently of the device at any point of time.
715 if (!sd->fwnode && sd->dev)
716 sd->fwnode = dev_fwnode(sd->dev);
718 mutex_lock(&list_lock);
720 INIT_LIST_HEAD(&sd->async_list);
722 list_for_each_entry(notifier, ¬ifier_list, list) {
723 struct v4l2_device *v4l2_dev =
724 v4l2_async_nf_find_v4l2_dev(notifier);
725 struct v4l2_async_subdev *asd;
730 asd = v4l2_async_find_match(notifier, sd);
734 ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd);
738 ret = v4l2_async_nf_try_complete(notifier);
745 /* None matched, wait for hot-plugging */
746 list_add(&sd->async_list, &subdev_list);
749 mutex_unlock(&list_lock);
755 * Complete failed. Unbind the sub-devices bound through registering
756 * this async sub-device.
758 subdev_notifier = v4l2_async_find_subdev_notifier(sd);
760 v4l2_async_nf_unbind_all_subdevs(subdev_notifier);
763 v4l2_async_nf_call_unbind(notifier, sd, sd->asd);
764 v4l2_async_cleanup(sd);
766 mutex_unlock(&list_lock);
770 EXPORT_SYMBOL(v4l2_async_register_subdev);
772 void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
774 if (!sd->async_list.next)
777 mutex_lock(&list_lock);
779 __v4l2_async_nf_unregister(sd->subdev_notifier);
780 __v4l2_async_nf_cleanup(sd->subdev_notifier);
781 kfree(sd->subdev_notifier);
782 sd->subdev_notifier = NULL;
785 struct v4l2_async_notifier *notifier = sd->notifier;
787 list_add(&sd->asd->list, ¬ifier->waiting);
789 v4l2_async_nf_call_unbind(notifier, sd, sd->asd);
792 v4l2_async_cleanup(sd);
794 mutex_unlock(&list_lock);
796 EXPORT_SYMBOL(v4l2_async_unregister_subdev);
798 static void print_waiting_subdev(struct seq_file *s,
799 struct v4l2_async_subdev *asd)
801 switch (asd->match_type) {
802 case V4L2_ASYNC_MATCH_I2C:
803 seq_printf(s, " [i2c] dev=%d-%04x\n", asd->match.i2c.adapter_id,
804 asd->match.i2c.address);
806 case V4L2_ASYNC_MATCH_FWNODE: {
807 struct fwnode_handle *devnode, *fwnode = asd->match.fwnode;
809 devnode = fwnode_graph_is_endpoint(fwnode) ?
810 fwnode_graph_get_port_parent(fwnode) :
811 fwnode_handle_get(fwnode);
813 seq_printf(s, " [fwnode] dev=%s, node=%pfw\n",
814 devnode->dev ? dev_name(devnode->dev) : "nil",
817 fwnode_handle_put(devnode);
824 v4l2_async_nf_name(struct v4l2_async_notifier *notifier)
826 if (notifier->v4l2_dev)
827 return notifier->v4l2_dev->name;
828 else if (notifier->sd)
829 return notifier->sd->name;
834 static int pending_subdevs_show(struct seq_file *s, void *data)
836 struct v4l2_async_notifier *notif;
837 struct v4l2_async_subdev *asd;
839 mutex_lock(&list_lock);
841 list_for_each_entry(notif, ¬ifier_list, list) {
842 seq_printf(s, "%s:\n", v4l2_async_nf_name(notif));
843 list_for_each_entry(asd, ¬if->waiting, list)
844 print_waiting_subdev(s, asd);
847 mutex_unlock(&list_lock);
851 DEFINE_SHOW_ATTRIBUTE(pending_subdevs);
853 static struct dentry *v4l2_async_debugfs_dir;
855 static int __init v4l2_async_init(void)
857 v4l2_async_debugfs_dir = debugfs_create_dir("v4l2-async", NULL);
858 debugfs_create_file("pending_async_subdevices", 0444,
859 v4l2_async_debugfs_dir, NULL,
860 &pending_subdevs_fops);
865 static void __exit v4l2_async_exit(void)
867 debugfs_remove_recursive(v4l2_async_debugfs_dir);
870 subsys_initcall(v4l2_async_init);
871 module_exit(v4l2_async_exit);
876 MODULE_LICENSE("GPL");