1 // SPDX-License-Identifier: GPL-2.0-only
3 * V4L2 asynchronous subdevice registration API
5 * Copyright (C) 2012-2013, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
8 #include <linux/device.h>
10 #include <linux/i2c.h>
11 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
20 #include <media/v4l2-async.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-fwnode.h>
23 #include <media/v4l2-subdev.h>
25 static int v4l2_async_notifier_call_bound(struct v4l2_async_notifier
*n
,
26 struct v4l2_subdev
*subdev
,
27 struct v4l2_async_subdev
*asd
)
29 if (!n
->ops
|| !n
->ops
->bound
)
32 return n
->ops
->bound(n
, subdev
, asd
);
35 static void v4l2_async_notifier_call_unbind(struct v4l2_async_notifier
*n
,
36 struct v4l2_subdev
*subdev
,
37 struct v4l2_async_subdev
*asd
)
39 if (!n
->ops
|| !n
->ops
->unbind
)
42 n
->ops
->unbind(n
, subdev
, asd
);
45 static int v4l2_async_notifier_call_complete(struct v4l2_async_notifier
*n
)
47 if (!n
->ops
|| !n
->ops
->complete
)
50 return n
->ops
->complete(n
);
53 static bool match_i2c(struct v4l2_async_notifier
*notifier
,
54 struct v4l2_subdev
*sd
, struct v4l2_async_subdev
*asd
)
56 #if IS_ENABLED(CONFIG_I2C)
57 struct i2c_client
*client
= i2c_verify_client(sd
->dev
);
60 asd
->match
.i2c
.adapter_id
== client
->adapter
->nr
&&
61 asd
->match
.i2c
.address
== client
->addr
;
67 static bool match_devname(struct v4l2_async_notifier
*notifier
,
68 struct v4l2_subdev
*sd
, struct v4l2_async_subdev
*asd
)
70 return !strcmp(asd
->match
.device_name
, dev_name(sd
->dev
));
73 static bool match_fwnode(struct v4l2_async_notifier
*notifier
,
74 struct v4l2_subdev
*sd
, struct v4l2_async_subdev
*asd
)
76 struct fwnode_handle
*other_fwnode
;
77 struct fwnode_handle
*dev_fwnode
;
78 bool asd_fwnode_is_ep
;
83 * Both the subdev and the async subdev can provide either an endpoint
84 * fwnode or a device fwnode. Start with the simple case of direct
87 if (sd
->fwnode
== asd
->match
.fwnode
)
91 * Otherwise, check if the sd fwnode and the asd fwnode refer to an
92 * endpoint or a device. If they're of the same type, there's no match.
93 * Technically speaking this checks if the nodes refer to a connected
94 * endpoint, which is the simplest check that works for both OF and
95 * ACPI. This won't make a difference, as drivers should not try to
96 * match unconnected endpoints.
98 sd_fwnode_is_ep
= fwnode_graph_is_endpoint(sd
->fwnode
);
99 asd_fwnode_is_ep
= fwnode_graph_is_endpoint(asd
->match
.fwnode
);
101 if (sd_fwnode_is_ep
== asd_fwnode_is_ep
)
105 * The sd and asd fwnodes are of different types. Get the device fwnode
106 * parent of the endpoint fwnode, and compare it with the other fwnode.
108 if (sd_fwnode_is_ep
) {
109 dev_fwnode
= fwnode_graph_get_port_parent(sd
->fwnode
);
110 other_fwnode
= asd
->match
.fwnode
;
112 dev_fwnode
= fwnode_graph_get_port_parent(asd
->match
.fwnode
);
113 other_fwnode
= sd
->fwnode
;
116 fwnode_handle_put(dev_fwnode
);
118 if (dev_fwnode
!= other_fwnode
)
122 * We have a heterogeneous match. Retrieve the struct device of the side
123 * that matched on a device fwnode to print its driver name.
126 dev
= notifier
->v4l2_dev
? notifier
->v4l2_dev
->dev
131 if (dev
&& dev
->driver
) {
133 dev_warn(dev
, "Driver %s uses device fwnode, incorrect match may occur\n",
135 dev_notice(dev
, "Consider updating driver %s to match on endpoints\n",
142 static bool match_custom(struct v4l2_async_notifier
*notifier
,
143 struct v4l2_subdev
*sd
, struct v4l2_async_subdev
*asd
)
145 if (!asd
->match
.custom
.match
)
149 return asd
->match
.custom
.match(sd
->dev
, asd
);
152 static LIST_HEAD(subdev_list
);
153 static LIST_HEAD(notifier_list
);
154 static DEFINE_MUTEX(list_lock
);
156 static struct v4l2_async_subdev
*
157 v4l2_async_find_match(struct v4l2_async_notifier
*notifier
,
158 struct v4l2_subdev
*sd
)
160 bool (*match
)(struct v4l2_async_notifier
*notifier
,
161 struct v4l2_subdev
*sd
, struct v4l2_async_subdev
*asd
);
162 struct v4l2_async_subdev
*asd
;
164 list_for_each_entry(asd
, ¬ifier
->waiting
, list
) {
165 /* bus_type has been verified valid before */
166 switch (asd
->match_type
) {
167 case V4L2_ASYNC_MATCH_CUSTOM
:
168 match
= match_custom
;
170 case V4L2_ASYNC_MATCH_DEVNAME
:
171 match
= match_devname
;
173 case V4L2_ASYNC_MATCH_I2C
:
176 case V4L2_ASYNC_MATCH_FWNODE
:
177 match
= match_fwnode
;
180 /* Cannot happen, unless someone breaks us */
185 /* match cannot be NULL here */
186 if (match(notifier
, sd
, asd
))
193 /* Compare two async sub-device descriptors for equivalence */
194 static bool asd_equal(struct v4l2_async_subdev
*asd_x
,
195 struct v4l2_async_subdev
*asd_y
)
197 if (asd_x
->match_type
!= asd_y
->match_type
)
200 switch (asd_x
->match_type
) {
201 case V4L2_ASYNC_MATCH_DEVNAME
:
202 return strcmp(asd_x
->match
.device_name
,
203 asd_y
->match
.device_name
) == 0;
204 case V4L2_ASYNC_MATCH_I2C
:
205 return asd_x
->match
.i2c
.adapter_id
==
206 asd_y
->match
.i2c
.adapter_id
&&
207 asd_x
->match
.i2c
.address
==
208 asd_y
->match
.i2c
.address
;
209 case V4L2_ASYNC_MATCH_FWNODE
:
210 return asd_x
->match
.fwnode
== asd_y
->match
.fwnode
;
218 /* Find the sub-device notifier registered by a sub-device driver. */
219 static struct v4l2_async_notifier
*
220 v4l2_async_find_subdev_notifier(struct v4l2_subdev
*sd
)
222 struct v4l2_async_notifier
*n
;
224 list_for_each_entry(n
, ¬ifier_list
, list
)
231 /* Get v4l2_device related to the notifier if one can be found. */
232 static struct v4l2_device
*
233 v4l2_async_notifier_find_v4l2_dev(struct v4l2_async_notifier
*notifier
)
235 while (notifier
->parent
)
236 notifier
= notifier
->parent
;
238 return notifier
->v4l2_dev
;
242 * Return true if all child sub-device notifiers are complete, false otherwise.
245 v4l2_async_notifier_can_complete(struct v4l2_async_notifier
*notifier
)
247 struct v4l2_subdev
*sd
;
249 if (!list_empty(¬ifier
->waiting
))
252 list_for_each_entry(sd
, ¬ifier
->done
, async_list
) {
253 struct v4l2_async_notifier
*subdev_notifier
=
254 v4l2_async_find_subdev_notifier(sd
);
256 if (subdev_notifier
&&
257 !v4l2_async_notifier_can_complete(subdev_notifier
))
265 * Complete the master notifier if possible. This is done when all async
266 * sub-devices have been bound; v4l2_device is also available then.
269 v4l2_async_notifier_try_complete(struct v4l2_async_notifier
*notifier
)
271 /* Quick check whether there are still more sub-devices here. */
272 if (!list_empty(¬ifier
->waiting
))
275 /* Check the entire notifier tree; find the root notifier first. */
276 while (notifier
->parent
)
277 notifier
= notifier
->parent
;
279 /* This is root if it has v4l2_dev. */
280 if (!notifier
->v4l2_dev
)
283 /* Is everything ready? */
284 if (!v4l2_async_notifier_can_complete(notifier
))
287 return v4l2_async_notifier_call_complete(notifier
);
291 v4l2_async_notifier_try_all_subdevs(struct v4l2_async_notifier
*notifier
);
293 static int v4l2_async_match_notify(struct v4l2_async_notifier
*notifier
,
294 struct v4l2_device
*v4l2_dev
,
295 struct v4l2_subdev
*sd
,
296 struct v4l2_async_subdev
*asd
)
298 struct v4l2_async_notifier
*subdev_notifier
;
301 ret
= v4l2_device_register_subdev(v4l2_dev
, sd
);
305 ret
= v4l2_async_notifier_call_bound(notifier
, sd
, asd
);
307 v4l2_device_unregister_subdev(sd
);
311 /* Remove from the waiting list */
312 list_del(&asd
->list
);
314 sd
->notifier
= notifier
;
316 /* Move from the global subdevice list to notifier's done */
317 list_move(&sd
->async_list
, ¬ifier
->done
);
320 * See if the sub-device has a notifier. If not, return here.
322 subdev_notifier
= v4l2_async_find_subdev_notifier(sd
);
323 if (!subdev_notifier
|| subdev_notifier
->parent
)
327 * Proceed with checking for the sub-device notifier's async
328 * sub-devices, and return the result. The error will be handled by the
331 subdev_notifier
->parent
= notifier
;
333 return v4l2_async_notifier_try_all_subdevs(subdev_notifier
);
336 /* Test all async sub-devices in a notifier for a match. */
338 v4l2_async_notifier_try_all_subdevs(struct v4l2_async_notifier
*notifier
)
340 struct v4l2_device
*v4l2_dev
=
341 v4l2_async_notifier_find_v4l2_dev(notifier
);
342 struct v4l2_subdev
*sd
;
348 list_for_each_entry(sd
, &subdev_list
, async_list
) {
349 struct v4l2_async_subdev
*asd
;
352 asd
= v4l2_async_find_match(notifier
, sd
);
356 ret
= v4l2_async_match_notify(notifier
, v4l2_dev
, sd
, asd
);
361 * v4l2_async_match_notify() may lead to registering a
362 * new notifier and thus changing the async subdevs
363 * list. In order to proceed safely from here, restart
364 * parsing the list from the beginning.
372 static void v4l2_async_cleanup(struct v4l2_subdev
*sd
)
374 v4l2_device_unregister_subdev(sd
);
376 * Subdevice driver will reprobe and put the subdev back
379 list_del_init(&sd
->async_list
);
383 /* Unbind all sub-devices in the notifier tree. */
385 v4l2_async_notifier_unbind_all_subdevs(struct v4l2_async_notifier
*notifier
)
387 struct v4l2_subdev
*sd
, *tmp
;
389 list_for_each_entry_safe(sd
, tmp
, ¬ifier
->done
, async_list
) {
390 struct v4l2_async_notifier
*subdev_notifier
=
391 v4l2_async_find_subdev_notifier(sd
);
394 v4l2_async_notifier_unbind_all_subdevs(subdev_notifier
);
396 v4l2_async_notifier_call_unbind(notifier
, sd
, sd
->asd
);
397 v4l2_async_cleanup(sd
);
399 list_move(&sd
->async_list
, &subdev_list
);
402 notifier
->parent
= NULL
;
405 /* See if an async sub-device can be found in a notifier's lists. */
407 __v4l2_async_notifier_has_async_subdev(struct v4l2_async_notifier
*notifier
,
408 struct v4l2_async_subdev
*asd
)
410 struct v4l2_async_subdev
*asd_y
;
411 struct v4l2_subdev
*sd
;
413 list_for_each_entry(asd_y
, ¬ifier
->waiting
, list
)
414 if (asd_equal(asd
, asd_y
))
417 list_for_each_entry(sd
, ¬ifier
->done
, async_list
) {
418 if (WARN_ON(!sd
->asd
))
421 if (asd_equal(asd
, sd
->asd
))
429 * Find out whether an async sub-device was set up already or
430 * whether it exists in a given notifier before @this_index.
431 * If @this_index < 0, search the notifier's entire @asd_list.
434 v4l2_async_notifier_has_async_subdev(struct v4l2_async_notifier
*notifier
,
435 struct v4l2_async_subdev
*asd
,
438 struct v4l2_async_subdev
*asd_y
;
441 lockdep_assert_held(&list_lock
);
443 /* Check that an asd is not being added more than once. */
444 list_for_each_entry(asd_y
, ¬ifier
->asd_list
, asd_list
) {
445 if (this_index
>= 0 && j
++ >= this_index
)
447 if (asd_equal(asd
, asd_y
))
451 /* Check that an asd does not exist in other notifiers. */
452 list_for_each_entry(notifier
, ¬ifier_list
, list
)
453 if (__v4l2_async_notifier_has_async_subdev(notifier
, asd
))
459 static int v4l2_async_notifier_asd_valid(struct v4l2_async_notifier
*notifier
,
460 struct v4l2_async_subdev
*asd
,
464 notifier
->v4l2_dev
? notifier
->v4l2_dev
->dev
: NULL
;
469 switch (asd
->match_type
) {
470 case V4L2_ASYNC_MATCH_CUSTOM
:
471 case V4L2_ASYNC_MATCH_DEVNAME
:
472 case V4L2_ASYNC_MATCH_I2C
:
473 case V4L2_ASYNC_MATCH_FWNODE
:
474 if (v4l2_async_notifier_has_async_subdev(notifier
, asd
,
476 dev_dbg(dev
, "subdev descriptor already listed in this or other notifiers\n");
481 dev_err(dev
, "Invalid match type %u on %p\n",
482 asd
->match_type
, asd
);
489 void v4l2_async_notifier_init(struct v4l2_async_notifier
*notifier
)
491 INIT_LIST_HEAD(¬ifier
->asd_list
);
493 EXPORT_SYMBOL(v4l2_async_notifier_init
);
495 static int __v4l2_async_notifier_register(struct v4l2_async_notifier
*notifier
)
497 struct v4l2_async_subdev
*asd
;
500 INIT_LIST_HEAD(¬ifier
->waiting
);
501 INIT_LIST_HEAD(¬ifier
->done
);
503 mutex_lock(&list_lock
);
505 list_for_each_entry(asd
, ¬ifier
->asd_list
, asd_list
) {
506 ret
= v4l2_async_notifier_asd_valid(notifier
, asd
, i
++);
510 list_add_tail(&asd
->list
, ¬ifier
->waiting
);
513 ret
= v4l2_async_notifier_try_all_subdevs(notifier
);
517 ret
= v4l2_async_notifier_try_complete(notifier
);
521 /* Keep also completed notifiers on the list */
522 list_add(¬ifier
->list
, ¬ifier_list
);
524 mutex_unlock(&list_lock
);
530 * On failure, unbind all sub-devices registered through this notifier.
532 v4l2_async_notifier_unbind_all_subdevs(notifier
);
535 mutex_unlock(&list_lock
);
540 int v4l2_async_notifier_register(struct v4l2_device
*v4l2_dev
,
541 struct v4l2_async_notifier
*notifier
)
545 if (WARN_ON(!v4l2_dev
|| notifier
->sd
))
548 notifier
->v4l2_dev
= v4l2_dev
;
550 ret
= __v4l2_async_notifier_register(notifier
);
552 notifier
->v4l2_dev
= NULL
;
556 EXPORT_SYMBOL(v4l2_async_notifier_register
);
558 int v4l2_async_subdev_notifier_register(struct v4l2_subdev
*sd
,
559 struct v4l2_async_notifier
*notifier
)
563 if (WARN_ON(!sd
|| notifier
->v4l2_dev
))
568 ret
= __v4l2_async_notifier_register(notifier
);
574 EXPORT_SYMBOL(v4l2_async_subdev_notifier_register
);
577 __v4l2_async_notifier_unregister(struct v4l2_async_notifier
*notifier
)
579 if (!notifier
|| (!notifier
->v4l2_dev
&& !notifier
->sd
))
582 v4l2_async_notifier_unbind_all_subdevs(notifier
);
585 notifier
->v4l2_dev
= NULL
;
587 list_del(¬ifier
->list
);
590 void v4l2_async_notifier_unregister(struct v4l2_async_notifier
*notifier
)
592 mutex_lock(&list_lock
);
594 __v4l2_async_notifier_unregister(notifier
);
596 mutex_unlock(&list_lock
);
598 EXPORT_SYMBOL(v4l2_async_notifier_unregister
);
600 static void __v4l2_async_notifier_cleanup(struct v4l2_async_notifier
*notifier
)
602 struct v4l2_async_subdev
*asd
, *tmp
;
604 if (!notifier
|| !notifier
->asd_list
.next
)
607 list_for_each_entry_safe(asd
, tmp
, ¬ifier
->asd_list
, asd_list
) {
608 switch (asd
->match_type
) {
609 case V4L2_ASYNC_MATCH_FWNODE
:
610 fwnode_handle_put(asd
->match
.fwnode
);
616 list_del(&asd
->asd_list
);
621 void v4l2_async_notifier_cleanup(struct v4l2_async_notifier
*notifier
)
623 mutex_lock(&list_lock
);
625 __v4l2_async_notifier_cleanup(notifier
);
627 mutex_unlock(&list_lock
);
629 EXPORT_SYMBOL_GPL(v4l2_async_notifier_cleanup
);
631 int v4l2_async_notifier_add_subdev(struct v4l2_async_notifier
*notifier
,
632 struct v4l2_async_subdev
*asd
)
636 mutex_lock(&list_lock
);
638 ret
= v4l2_async_notifier_asd_valid(notifier
, asd
, -1);
642 list_add_tail(&asd
->asd_list
, ¬ifier
->asd_list
);
645 mutex_unlock(&list_lock
);
648 EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_subdev
);
650 struct v4l2_async_subdev
*
651 v4l2_async_notifier_add_fwnode_subdev(struct v4l2_async_notifier
*notifier
,
652 struct fwnode_handle
*fwnode
,
653 unsigned int asd_struct_size
)
655 struct v4l2_async_subdev
*asd
;
658 asd
= kzalloc(asd_struct_size
, GFP_KERNEL
);
660 return ERR_PTR(-ENOMEM
);
662 asd
->match_type
= V4L2_ASYNC_MATCH_FWNODE
;
663 asd
->match
.fwnode
= fwnode_handle_get(fwnode
);
665 ret
= v4l2_async_notifier_add_subdev(notifier
, asd
);
667 fwnode_handle_put(fwnode
);
674 EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_fwnode_subdev
);
677 v4l2_async_notifier_add_fwnode_remote_subdev(struct v4l2_async_notifier
*notif
,
678 struct fwnode_handle
*endpoint
,
679 struct v4l2_async_subdev
*asd
)
681 struct fwnode_handle
*remote
;
684 remote
= fwnode_graph_get_remote_port_parent(endpoint
);
688 asd
->match_type
= V4L2_ASYNC_MATCH_FWNODE
;
689 asd
->match
.fwnode
= remote
;
691 ret
= v4l2_async_notifier_add_subdev(notif
, asd
);
693 fwnode_handle_put(remote
);
697 EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_fwnode_remote_subdev
);
699 struct v4l2_async_subdev
*
700 v4l2_async_notifier_add_i2c_subdev(struct v4l2_async_notifier
*notifier
,
701 int adapter_id
, unsigned short address
,
702 unsigned int asd_struct_size
)
704 struct v4l2_async_subdev
*asd
;
707 asd
= kzalloc(asd_struct_size
, GFP_KERNEL
);
709 return ERR_PTR(-ENOMEM
);
711 asd
->match_type
= V4L2_ASYNC_MATCH_I2C
;
712 asd
->match
.i2c
.adapter_id
= adapter_id
;
713 asd
->match
.i2c
.address
= address
;
715 ret
= v4l2_async_notifier_add_subdev(notifier
, asd
);
723 EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_i2c_subdev
);
725 struct v4l2_async_subdev
*
726 v4l2_async_notifier_add_devname_subdev(struct v4l2_async_notifier
*notifier
,
727 const char *device_name
,
728 unsigned int asd_struct_size
)
730 struct v4l2_async_subdev
*asd
;
733 asd
= kzalloc(asd_struct_size
, GFP_KERNEL
);
735 return ERR_PTR(-ENOMEM
);
737 asd
->match_type
= V4L2_ASYNC_MATCH_DEVNAME
;
738 asd
->match
.device_name
= device_name
;
740 ret
= v4l2_async_notifier_add_subdev(notifier
, asd
);
748 EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_devname_subdev
);
750 int v4l2_async_register_subdev(struct v4l2_subdev
*sd
)
752 struct v4l2_async_notifier
*subdev_notifier
;
753 struct v4l2_async_notifier
*notifier
;
757 * No reference taken. The reference is held by the device
758 * (struct v4l2_subdev.dev), and async sub-device does not
759 * exist independently of the device at any point of time.
761 if (!sd
->fwnode
&& sd
->dev
)
762 sd
->fwnode
= dev_fwnode(sd
->dev
);
764 mutex_lock(&list_lock
);
766 INIT_LIST_HEAD(&sd
->async_list
);
768 list_for_each_entry(notifier
, ¬ifier_list
, list
) {
769 struct v4l2_device
*v4l2_dev
=
770 v4l2_async_notifier_find_v4l2_dev(notifier
);
771 struct v4l2_async_subdev
*asd
;
776 asd
= v4l2_async_find_match(notifier
, sd
);
780 ret
= v4l2_async_match_notify(notifier
, v4l2_dev
, sd
, asd
);
784 ret
= v4l2_async_notifier_try_complete(notifier
);
791 /* None matched, wait for hot-plugging */
792 list_add(&sd
->async_list
, &subdev_list
);
795 mutex_unlock(&list_lock
);
801 * Complete failed. Unbind the sub-devices bound through registering
802 * this async sub-device.
804 subdev_notifier
= v4l2_async_find_subdev_notifier(sd
);
806 v4l2_async_notifier_unbind_all_subdevs(subdev_notifier
);
809 v4l2_async_notifier_call_unbind(notifier
, sd
, sd
->asd
);
810 v4l2_async_cleanup(sd
);
812 mutex_unlock(&list_lock
);
816 EXPORT_SYMBOL(v4l2_async_register_subdev
);
818 void v4l2_async_unregister_subdev(struct v4l2_subdev
*sd
)
820 mutex_lock(&list_lock
);
822 __v4l2_async_notifier_unregister(sd
->subdev_notifier
);
823 __v4l2_async_notifier_cleanup(sd
->subdev_notifier
);
824 kfree(sd
->subdev_notifier
);
825 sd
->subdev_notifier
= NULL
;
828 struct v4l2_async_notifier
*notifier
= sd
->notifier
;
830 list_add(&sd
->asd
->list
, ¬ifier
->waiting
);
832 v4l2_async_notifier_call_unbind(notifier
, sd
, sd
->asd
);
835 v4l2_async_cleanup(sd
);
837 mutex_unlock(&list_lock
);
839 EXPORT_SYMBOL(v4l2_async_unregister_subdev
);