media: v4l2-async: Create links during v4l2_async_match_notify()
[platform/kernel/linux-rpi.git] / drivers / media / v4l2-core / v4l2-async.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * V4L2 asynchronous subdevice registration API
4  *
5  * Copyright (C) 2012-2013, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
6  */
7
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>
13 #include <linux/mm.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/of.h>
17 #include <linux/platform_device.h>
18 #include <linux/seq_file.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
21
22 #include <media/v4l2-async.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-fwnode.h>
25 #include <media/v4l2-subdev.h>
26
27 static int v4l2_async_notifier_call_bound(struct v4l2_async_notifier *n,
28                                           struct v4l2_subdev *subdev,
29                                           struct v4l2_async_subdev *asd)
30 {
31         if (!n->ops || !n->ops->bound)
32                 return 0;
33
34         return n->ops->bound(n, subdev, asd);
35 }
36
37 static void v4l2_async_notifier_call_unbind(struct v4l2_async_notifier *n,
38                                             struct v4l2_subdev *subdev,
39                                             struct v4l2_async_subdev *asd)
40 {
41         if (!n->ops || !n->ops->unbind)
42                 return;
43
44         n->ops->unbind(n, subdev, asd);
45 }
46
47 static int v4l2_async_notifier_call_complete(struct v4l2_async_notifier *n)
48 {
49         if (!n->ops || !n->ops->complete)
50                 return 0;
51
52         return n->ops->complete(n);
53 }
54
55 static bool match_i2c(struct v4l2_async_notifier *notifier,
56                       struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
57 {
58 #if IS_ENABLED(CONFIG_I2C)
59         struct i2c_client *client = i2c_verify_client(sd->dev);
60
61         return client &&
62                 asd->match.i2c.adapter_id == client->adapter->nr &&
63                 asd->match.i2c.address == client->addr;
64 #else
65         return false;
66 #endif
67 }
68
69 static bool match_fwnode(struct v4l2_async_notifier *notifier,
70                          struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
71 {
72         struct fwnode_handle *other_fwnode;
73         struct fwnode_handle *dev_fwnode;
74         bool asd_fwnode_is_ep;
75         bool sd_fwnode_is_ep;
76         struct device *dev;
77
78         /*
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
81          * fwnode matching.
82          */
83         if (sd->fwnode == asd->match.fwnode)
84                 return true;
85
86         /*
87          * Check the same situation for any possible secondary assigned to the
88          * subdev's fwnode
89          */
90         if (!IS_ERR_OR_NULL(sd->fwnode->secondary) &&
91             sd->fwnode->secondary == asd->match.fwnode)
92                 return true;
93
94         /*
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.
101          */
102         sd_fwnode_is_ep = fwnode_graph_is_endpoint(sd->fwnode);
103         asd_fwnode_is_ep = fwnode_graph_is_endpoint(asd->match.fwnode);
104
105         if (sd_fwnode_is_ep == asd_fwnode_is_ep)
106                 return false;
107
108         /*
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.
111          */
112         if (sd_fwnode_is_ep) {
113                 dev_fwnode = fwnode_graph_get_port_parent(sd->fwnode);
114                 other_fwnode = asd->match.fwnode;
115         } else {
116                 dev_fwnode = fwnode_graph_get_port_parent(asd->match.fwnode);
117                 other_fwnode = sd->fwnode;
118         }
119
120         fwnode_handle_put(dev_fwnode);
121
122         if (dev_fwnode != other_fwnode)
123                 return false;
124
125         /*
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.
128          */
129         if (sd_fwnode_is_ep)
130                 dev = notifier->v4l2_dev ? notifier->v4l2_dev->dev
131                     : notifier->sd->dev;
132         else
133                 dev = sd->dev;
134
135         if (dev && dev->driver) {
136                 if (sd_fwnode_is_ep)
137                         dev_warn(dev, "Driver %s uses device fwnode, incorrect match may occur\n",
138                                  dev->driver->name);
139                 dev_notice(dev, "Consider updating driver %s to match on endpoints\n",
140                            dev->driver->name);
141         }
142
143         return true;
144 }
145
146 static LIST_HEAD(subdev_list);
147 static LIST_HEAD(notifier_list);
148 static DEFINE_MUTEX(list_lock);
149
150 static struct v4l2_async_subdev *
151 v4l2_async_find_match(struct v4l2_async_notifier *notifier,
152                       struct v4l2_subdev *sd)
153 {
154         bool (*match)(struct v4l2_async_notifier *notifier,
155                       struct v4l2_subdev *sd, struct v4l2_async_subdev *asd);
156         struct v4l2_async_subdev *asd;
157
158         list_for_each_entry(asd, &notifier->waiting, list) {
159                 /* bus_type has been verified valid before */
160                 switch (asd->match_type) {
161                 case V4L2_ASYNC_MATCH_I2C:
162                         match = match_i2c;
163                         break;
164                 case V4L2_ASYNC_MATCH_FWNODE:
165                         match = match_fwnode;
166                         break;
167                 default:
168                         /* Cannot happen, unless someone breaks us */
169                         WARN_ON(true);
170                         return NULL;
171                 }
172
173                 /* match cannot be NULL here */
174                 if (match(notifier, sd, asd))
175                         return asd;
176         }
177
178         return NULL;
179 }
180
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)
184 {
185         if (asd_x->match_type != asd_y->match_type)
186                 return false;
187
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;
196         default:
197                 break;
198         }
199
200         return false;
201 }
202
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)
206 {
207         struct v4l2_async_notifier *n;
208
209         list_for_each_entry(n, &notifier_list, list)
210                 if (n->sd == sd)
211                         return n;
212
213         return NULL;
214 }
215
216 /* Get v4l2_device related to the notifier if one can be found. */
217 static struct v4l2_device *
218 v4l2_async_notifier_find_v4l2_dev(struct v4l2_async_notifier *notifier)
219 {
220         while (notifier->parent)
221                 notifier = notifier->parent;
222
223         return notifier->v4l2_dev;
224 }
225
226 /*
227  * Return true if all child sub-device notifiers are complete, false otherwise.
228  */
229 static bool
230 v4l2_async_notifier_can_complete(struct v4l2_async_notifier *notifier)
231 {
232         struct v4l2_subdev *sd;
233
234         if (!list_empty(&notifier->waiting))
235                 return false;
236
237         list_for_each_entry(sd, &notifier->done, async_list) {
238                 struct v4l2_async_notifier *subdev_notifier =
239                         v4l2_async_find_subdev_notifier(sd);
240
241                 if (subdev_notifier &&
242                     !v4l2_async_notifier_can_complete(subdev_notifier))
243                         return false;
244         }
245
246         return true;
247 }
248
249 /*
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.
252  */
253 static int
254 v4l2_async_notifier_try_complete(struct v4l2_async_notifier *notifier)
255 {
256         /* Quick check whether there are still more sub-devices here. */
257         if (!list_empty(&notifier->waiting))
258                 return 0;
259
260         /* Check the entire notifier tree; find the root notifier first. */
261         while (notifier->parent)
262                 notifier = notifier->parent;
263
264         /* This is root if it has v4l2_dev. */
265         if (!notifier->v4l2_dev)
266                 return 0;
267
268         /* Is everything ready? */
269         if (!v4l2_async_notifier_can_complete(notifier))
270                 return 0;
271
272         return v4l2_async_notifier_call_complete(notifier);
273 }
274
275 static int
276 v4l2_async_notifier_try_all_subdevs(struct v4l2_async_notifier *notifier);
277
278 static int v4l2_async_create_ancillary_links(struct v4l2_async_notifier *n,
279                                              struct v4l2_subdev *sd)
280 {
281         struct media_link *link = NULL;
282
283 #if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
284
285         if (sd->entity.function != MEDIA_ENT_F_LENS &&
286             sd->entity.function != MEDIA_ENT_F_FLASH)
287                 return 0;
288
289         link = media_create_ancillary_link(&n->sd->entity, &sd->entity);
290
291 #endif
292
293         return IS_ERR(link) ? PTR_ERR(link) : 0;
294 }
295
296 static int v4l2_async_match_notify(struct v4l2_async_notifier *notifier,
297                                    struct v4l2_device *v4l2_dev,
298                                    struct v4l2_subdev *sd,
299                                    struct v4l2_async_subdev *asd)
300 {
301         struct v4l2_async_notifier *subdev_notifier;
302         int ret;
303
304         ret = v4l2_device_register_subdev(v4l2_dev, sd);
305         if (ret < 0)
306                 return ret;
307
308         ret = v4l2_async_notifier_call_bound(notifier, sd, asd);
309         if (ret < 0) {
310                 v4l2_device_unregister_subdev(sd);
311                 return ret;
312         }
313
314         /*
315          * Depending of the function of the entities involved, we may want to
316          * create links between them (for example between a sensor and its lens
317          * or between a sensor's source pad and the connected device's sink
318          * pad).
319          */
320         ret = v4l2_async_create_ancillary_links(notifier, sd);
321         if (ret) {
322                 v4l2_async_notifier_call_unbind(notifier, sd, asd);
323                 v4l2_device_unregister_subdev(sd);
324                 return ret;
325         }
326
327         /* Remove from the waiting list */
328         list_del(&asd->list);
329         sd->asd = asd;
330         sd->notifier = notifier;
331
332         /* Move from the global subdevice list to notifier's done */
333         list_move(&sd->async_list, &notifier->done);
334
335         /*
336          * See if the sub-device has a notifier. If not, return here.
337          */
338         subdev_notifier = v4l2_async_find_subdev_notifier(sd);
339         if (!subdev_notifier || subdev_notifier->parent)
340                 return 0;
341
342         /*
343          * Proceed with checking for the sub-device notifier's async
344          * sub-devices, and return the result. The error will be handled by the
345          * caller.
346          */
347         subdev_notifier->parent = notifier;
348
349         return v4l2_async_notifier_try_all_subdevs(subdev_notifier);
350 }
351
352 /* Test all async sub-devices in a notifier for a match. */
353 static int
354 v4l2_async_notifier_try_all_subdevs(struct v4l2_async_notifier *notifier)
355 {
356         struct v4l2_device *v4l2_dev =
357                 v4l2_async_notifier_find_v4l2_dev(notifier);
358         struct v4l2_subdev *sd;
359
360         if (!v4l2_dev)
361                 return 0;
362
363 again:
364         list_for_each_entry(sd, &subdev_list, async_list) {
365                 struct v4l2_async_subdev *asd;
366                 int ret;
367
368                 asd = v4l2_async_find_match(notifier, sd);
369                 if (!asd)
370                         continue;
371
372                 ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd);
373                 if (ret < 0)
374                         return ret;
375
376                 /*
377                  * v4l2_async_match_notify() may lead to registering a
378                  * new notifier and thus changing the async subdevs
379                  * list. In order to proceed safely from here, restart
380                  * parsing the list from the beginning.
381                  */
382                 goto again;
383         }
384
385         return 0;
386 }
387
388 static void v4l2_async_cleanup(struct v4l2_subdev *sd)
389 {
390         v4l2_device_unregister_subdev(sd);
391         /*
392          * Subdevice driver will reprobe and put the subdev back
393          * onto the list
394          */
395         list_del_init(&sd->async_list);
396         sd->asd = NULL;
397 }
398
399 /* Unbind all sub-devices in the notifier tree. */
400 static void
401 v4l2_async_notifier_unbind_all_subdevs(struct v4l2_async_notifier *notifier)
402 {
403         struct v4l2_subdev *sd, *tmp;
404
405         list_for_each_entry_safe(sd, tmp, &notifier->done, async_list) {
406                 struct v4l2_async_notifier *subdev_notifier =
407                         v4l2_async_find_subdev_notifier(sd);
408
409                 if (subdev_notifier)
410                         v4l2_async_notifier_unbind_all_subdevs(subdev_notifier);
411
412                 v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
413                 v4l2_async_cleanup(sd);
414
415                 list_move(&sd->async_list, &subdev_list);
416         }
417
418         notifier->parent = NULL;
419 }
420
421 /* See if an async sub-device can be found in a notifier's lists. */
422 static bool
423 __v4l2_async_notifier_has_async_subdev(struct v4l2_async_notifier *notifier,
424                                        struct v4l2_async_subdev *asd)
425 {
426         struct v4l2_async_subdev *asd_y;
427         struct v4l2_subdev *sd;
428
429         list_for_each_entry(asd_y, &notifier->waiting, list)
430                 if (asd_equal(asd, asd_y))
431                         return true;
432
433         list_for_each_entry(sd, &notifier->done, async_list) {
434                 if (WARN_ON(!sd->asd))
435                         continue;
436
437                 if (asd_equal(asd, sd->asd))
438                         return true;
439         }
440
441         return false;
442 }
443
444 /*
445  * Find out whether an async sub-device was set up already or
446  * whether it exists in a given notifier before @this_index.
447  * If @this_index < 0, search the notifier's entire @asd_list.
448  */
449 static bool
450 v4l2_async_notifier_has_async_subdev(struct v4l2_async_notifier *notifier,
451                                      struct v4l2_async_subdev *asd,
452                                      int this_index)
453 {
454         struct v4l2_async_subdev *asd_y;
455         int j = 0;
456
457         lockdep_assert_held(&list_lock);
458
459         /* Check that an asd is not being added more than once. */
460         list_for_each_entry(asd_y, &notifier->asd_list, asd_list) {
461                 if (this_index >= 0 && j++ >= this_index)
462                         break;
463                 if (asd_equal(asd, asd_y))
464                         return true;
465         }
466
467         /* Check that an asd does not exist in other notifiers. */
468         list_for_each_entry(notifier, &notifier_list, list)
469                 if (__v4l2_async_notifier_has_async_subdev(notifier, asd))
470                         return true;
471
472         return false;
473 }
474
475 static int v4l2_async_notifier_asd_valid(struct v4l2_async_notifier *notifier,
476                                          struct v4l2_async_subdev *asd,
477                                          int this_index)
478 {
479         struct device *dev =
480                 notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL;
481
482         if (!asd)
483                 return -EINVAL;
484
485         switch (asd->match_type) {
486         case V4L2_ASYNC_MATCH_I2C:
487         case V4L2_ASYNC_MATCH_FWNODE:
488                 if (v4l2_async_notifier_has_async_subdev(notifier, asd,
489                                                          this_index)) {
490                         dev_dbg(dev, "subdev descriptor already listed in this or other notifiers\n");
491                         return -EEXIST;
492                 }
493                 break;
494         default:
495                 dev_err(dev, "Invalid match type %u on %p\n",
496                         asd->match_type, asd);
497                 return -EINVAL;
498         }
499
500         return 0;
501 }
502
503 void v4l2_async_notifier_init(struct v4l2_async_notifier *notifier)
504 {
505         INIT_LIST_HEAD(&notifier->asd_list);
506 }
507 EXPORT_SYMBOL(v4l2_async_notifier_init);
508
509 static int __v4l2_async_notifier_register(struct v4l2_async_notifier *notifier)
510 {
511         struct v4l2_async_subdev *asd;
512         int ret, i = 0;
513
514         INIT_LIST_HEAD(&notifier->waiting);
515         INIT_LIST_HEAD(&notifier->done);
516
517         mutex_lock(&list_lock);
518
519         list_for_each_entry(asd, &notifier->asd_list, asd_list) {
520                 ret = v4l2_async_notifier_asd_valid(notifier, asd, i++);
521                 if (ret)
522                         goto err_unlock;
523
524                 list_add_tail(&asd->list, &notifier->waiting);
525         }
526
527         ret = v4l2_async_notifier_try_all_subdevs(notifier);
528         if (ret < 0)
529                 goto err_unbind;
530
531         ret = v4l2_async_notifier_try_complete(notifier);
532         if (ret < 0)
533                 goto err_unbind;
534
535         /* Keep also completed notifiers on the list */
536         list_add(&notifier->list, &notifier_list);
537
538         mutex_unlock(&list_lock);
539
540         return 0;
541
542 err_unbind:
543         /*
544          * On failure, unbind all sub-devices registered through this notifier.
545          */
546         v4l2_async_notifier_unbind_all_subdevs(notifier);
547
548 err_unlock:
549         mutex_unlock(&list_lock);
550
551         return ret;
552 }
553
554 int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
555                                  struct v4l2_async_notifier *notifier)
556 {
557         int ret;
558
559         if (WARN_ON(!v4l2_dev || notifier->sd))
560                 return -EINVAL;
561
562         notifier->v4l2_dev = v4l2_dev;
563
564         ret = __v4l2_async_notifier_register(notifier);
565         if (ret)
566                 notifier->v4l2_dev = NULL;
567
568         return ret;
569 }
570 EXPORT_SYMBOL(v4l2_async_notifier_register);
571
572 int v4l2_async_subdev_notifier_register(struct v4l2_subdev *sd,
573                                         struct v4l2_async_notifier *notifier)
574 {
575         int ret;
576
577         if (WARN_ON(!sd || notifier->v4l2_dev))
578                 return -EINVAL;
579
580         notifier->sd = sd;
581
582         ret = __v4l2_async_notifier_register(notifier);
583         if (ret)
584                 notifier->sd = NULL;
585
586         return ret;
587 }
588 EXPORT_SYMBOL(v4l2_async_subdev_notifier_register);
589
590 static void
591 __v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
592 {
593         if (!notifier || (!notifier->v4l2_dev && !notifier->sd))
594                 return;
595
596         v4l2_async_notifier_unbind_all_subdevs(notifier);
597
598         notifier->sd = NULL;
599         notifier->v4l2_dev = NULL;
600
601         list_del(&notifier->list);
602 }
603
604 void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
605 {
606         mutex_lock(&list_lock);
607
608         __v4l2_async_notifier_unregister(notifier);
609
610         mutex_unlock(&list_lock);
611 }
612 EXPORT_SYMBOL(v4l2_async_notifier_unregister);
613
614 static void __v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier)
615 {
616         struct v4l2_async_subdev *asd, *tmp;
617
618         if (!notifier || !notifier->asd_list.next)
619                 return;
620
621         list_for_each_entry_safe(asd, tmp, &notifier->asd_list, asd_list) {
622                 switch (asd->match_type) {
623                 case V4L2_ASYNC_MATCH_FWNODE:
624                         fwnode_handle_put(asd->match.fwnode);
625                         break;
626                 default:
627                         break;
628                 }
629
630                 list_del(&asd->asd_list);
631                 kfree(asd);
632         }
633 }
634
635 void v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier)
636 {
637         mutex_lock(&list_lock);
638
639         __v4l2_async_notifier_cleanup(notifier);
640
641         mutex_unlock(&list_lock);
642 }
643 EXPORT_SYMBOL_GPL(v4l2_async_notifier_cleanup);
644
645 int __v4l2_async_notifier_add_subdev(struct v4l2_async_notifier *notifier,
646                                    struct v4l2_async_subdev *asd)
647 {
648         int ret;
649
650         mutex_lock(&list_lock);
651
652         ret = v4l2_async_notifier_asd_valid(notifier, asd, -1);
653         if (ret)
654                 goto unlock;
655
656         list_add_tail(&asd->asd_list, &notifier->asd_list);
657
658 unlock:
659         mutex_unlock(&list_lock);
660         return ret;
661 }
662 EXPORT_SYMBOL_GPL(__v4l2_async_notifier_add_subdev);
663
664 struct v4l2_async_subdev *
665 __v4l2_async_notifier_add_fwnode_subdev(struct v4l2_async_notifier *notifier,
666                                         struct fwnode_handle *fwnode,
667                                         unsigned int asd_struct_size)
668 {
669         struct v4l2_async_subdev *asd;
670         int ret;
671
672         asd = kzalloc(asd_struct_size, GFP_KERNEL);
673         if (!asd)
674                 return ERR_PTR(-ENOMEM);
675
676         asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
677         asd->match.fwnode = fwnode_handle_get(fwnode);
678
679         ret = __v4l2_async_notifier_add_subdev(notifier, asd);
680         if (ret) {
681                 fwnode_handle_put(fwnode);
682                 kfree(asd);
683                 return ERR_PTR(ret);
684         }
685
686         return asd;
687 }
688 EXPORT_SYMBOL_GPL(__v4l2_async_notifier_add_fwnode_subdev);
689
690 struct v4l2_async_subdev *
691 __v4l2_async_notifier_add_fwnode_remote_subdev(struct v4l2_async_notifier *notif,
692                                                struct fwnode_handle *endpoint,
693                                                unsigned int asd_struct_size)
694 {
695         struct v4l2_async_subdev *asd;
696         struct fwnode_handle *remote;
697
698         remote = fwnode_graph_get_remote_port_parent(endpoint);
699         if (!remote)
700                 return ERR_PTR(-ENOTCONN);
701
702         asd = __v4l2_async_notifier_add_fwnode_subdev(notif, remote,
703                                                       asd_struct_size);
704         /*
705          * Calling __v4l2_async_notifier_add_fwnode_subdev grabs a refcount,
706          * so drop the one we got in fwnode_graph_get_remote_port_parent.
707          */
708         fwnode_handle_put(remote);
709         return asd;
710 }
711 EXPORT_SYMBOL_GPL(__v4l2_async_notifier_add_fwnode_remote_subdev);
712
713 struct v4l2_async_subdev *
714 __v4l2_async_notifier_add_i2c_subdev(struct v4l2_async_notifier *notifier,
715                                      int adapter_id, unsigned short address,
716                                      unsigned int asd_struct_size)
717 {
718         struct v4l2_async_subdev *asd;
719         int ret;
720
721         asd = kzalloc(asd_struct_size, GFP_KERNEL);
722         if (!asd)
723                 return ERR_PTR(-ENOMEM);
724
725         asd->match_type = V4L2_ASYNC_MATCH_I2C;
726         asd->match.i2c.adapter_id = adapter_id;
727         asd->match.i2c.address = address;
728
729         ret = __v4l2_async_notifier_add_subdev(notifier, asd);
730         if (ret) {
731                 kfree(asd);
732                 return ERR_PTR(ret);
733         }
734
735         return asd;
736 }
737 EXPORT_SYMBOL_GPL(__v4l2_async_notifier_add_i2c_subdev);
738
739 int v4l2_async_register_subdev(struct v4l2_subdev *sd)
740 {
741         struct v4l2_async_notifier *subdev_notifier;
742         struct v4l2_async_notifier *notifier;
743         int ret;
744
745         /*
746          * No reference taken. The reference is held by the device
747          * (struct v4l2_subdev.dev), and async sub-device does not
748          * exist independently of the device at any point of time.
749          */
750         if (!sd->fwnode && sd->dev)
751                 sd->fwnode = dev_fwnode(sd->dev);
752
753         mutex_lock(&list_lock);
754
755         INIT_LIST_HEAD(&sd->async_list);
756
757         list_for_each_entry(notifier, &notifier_list, list) {
758                 struct v4l2_device *v4l2_dev =
759                         v4l2_async_notifier_find_v4l2_dev(notifier);
760                 struct v4l2_async_subdev *asd;
761
762                 if (!v4l2_dev)
763                         continue;
764
765                 asd = v4l2_async_find_match(notifier, sd);
766                 if (!asd)
767                         continue;
768
769                 ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd);
770                 if (ret)
771                         goto err_unbind;
772
773                 ret = v4l2_async_notifier_try_complete(notifier);
774                 if (ret)
775                         goto err_unbind;
776
777                 goto out_unlock;
778         }
779
780         /* None matched, wait for hot-plugging */
781         list_add(&sd->async_list, &subdev_list);
782
783 out_unlock:
784         mutex_unlock(&list_lock);
785
786         return 0;
787
788 err_unbind:
789         /*
790          * Complete failed. Unbind the sub-devices bound through registering
791          * this async sub-device.
792          */
793         subdev_notifier = v4l2_async_find_subdev_notifier(sd);
794         if (subdev_notifier)
795                 v4l2_async_notifier_unbind_all_subdevs(subdev_notifier);
796
797         if (sd->asd)
798                 v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
799         v4l2_async_cleanup(sd);
800
801         mutex_unlock(&list_lock);
802
803         return ret;
804 }
805 EXPORT_SYMBOL(v4l2_async_register_subdev);
806
807 void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
808 {
809         if (!sd->async_list.next)
810                 return;
811
812         mutex_lock(&list_lock);
813
814         __v4l2_async_notifier_unregister(sd->subdev_notifier);
815         __v4l2_async_notifier_cleanup(sd->subdev_notifier);
816         kfree(sd->subdev_notifier);
817         sd->subdev_notifier = NULL;
818
819         if (sd->asd) {
820                 struct v4l2_async_notifier *notifier = sd->notifier;
821
822                 list_add(&sd->asd->list, &notifier->waiting);
823
824                 v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
825         }
826
827         v4l2_async_cleanup(sd);
828
829         mutex_unlock(&list_lock);
830 }
831 EXPORT_SYMBOL(v4l2_async_unregister_subdev);
832
833 static void print_waiting_subdev(struct seq_file *s,
834                                  struct v4l2_async_subdev *asd)
835 {
836         switch (asd->match_type) {
837         case V4L2_ASYNC_MATCH_I2C:
838                 seq_printf(s, " [i2c] dev=%d-%04x\n", asd->match.i2c.adapter_id,
839                            asd->match.i2c.address);
840                 break;
841         case V4L2_ASYNC_MATCH_FWNODE: {
842                 struct fwnode_handle *devnode, *fwnode = asd->match.fwnode;
843
844                 devnode = fwnode_graph_is_endpoint(fwnode) ?
845                           fwnode_graph_get_port_parent(fwnode) :
846                           fwnode_handle_get(fwnode);
847
848                 seq_printf(s, " [fwnode] dev=%s, node=%pfw\n",
849                            devnode->dev ? dev_name(devnode->dev) : "nil",
850                            fwnode);
851
852                 fwnode_handle_put(devnode);
853                 break;
854         }
855         }
856 }
857
858 static const char *
859 v4l2_async_notifier_name(struct v4l2_async_notifier *notifier)
860 {
861         if (notifier->v4l2_dev)
862                 return notifier->v4l2_dev->name;
863         else if (notifier->sd)
864                 return notifier->sd->name;
865         else
866                 return "nil";
867 }
868
869 static int pending_subdevs_show(struct seq_file *s, void *data)
870 {
871         struct v4l2_async_notifier *notif;
872         struct v4l2_async_subdev *asd;
873
874         mutex_lock(&list_lock);
875
876         list_for_each_entry(notif, &notifier_list, list) {
877                 seq_printf(s, "%s:\n", v4l2_async_notifier_name(notif));
878                 list_for_each_entry(asd, &notif->waiting, list)
879                         print_waiting_subdev(s, asd);
880         }
881
882         mutex_unlock(&list_lock);
883
884         return 0;
885 }
886 DEFINE_SHOW_ATTRIBUTE(pending_subdevs);
887
888 static struct dentry *v4l2_async_debugfs_dir;
889
890 static int __init v4l2_async_init(void)
891 {
892         v4l2_async_debugfs_dir = debugfs_create_dir("v4l2-async", NULL);
893         debugfs_create_file("pending_async_subdevices", 0444,
894                             v4l2_async_debugfs_dir, NULL,
895                             &pending_subdevs_fops);
896
897         return 0;
898 }
899
900 static void __exit v4l2_async_exit(void)
901 {
902         debugfs_remove_recursive(v4l2_async_debugfs_dir);
903 }
904
905 subsys_initcall(v4l2_async_init);
906 module_exit(v4l2_async_exit);
907
908 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
909 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
910 MODULE_AUTHOR("Ezequiel Garcia <ezequiel@collabora.com>");
911 MODULE_LICENSE("GPL");