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
v4l2_async_nf_call_bound(struct v4l2_async_notifier * n,struct v4l2_subdev * subdev,struct v4l2_async_subdev * asd)27 static int v4l2_async_nf_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
v4l2_async_nf_call_unbind(struct v4l2_async_notifier * n,struct v4l2_subdev * subdev,struct v4l2_async_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)
40 {
41 if (!n->ops || !n->ops->unbind)
42 return;
43
44 n->ops->unbind(n, subdev, asd);
45 }
46
v4l2_async_nf_call_complete(struct v4l2_async_notifier * n)47 static int v4l2_async_nf_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
match_i2c(struct v4l2_async_notifier * notifier,struct v4l2_subdev * sd,struct v4l2_async_subdev * asd)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
match_fwnode(struct v4l2_async_notifier * notifier,struct v4l2_subdev * sd,struct v4l2_async_subdev * asd)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 *
v4l2_async_find_match(struct v4l2_async_notifier * notifier,struct v4l2_subdev * sd)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, ¬ifier->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 */
asd_equal(struct v4l2_async_subdev * asd_x,struct v4l2_async_subdev * asd_y)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 *
v4l2_async_find_subdev_notifier(struct v4l2_subdev * sd)205 v4l2_async_find_subdev_notifier(struct v4l2_subdev *sd)
206 {
207 struct v4l2_async_notifier *n;
208
209 list_for_each_entry(n, ¬ifier_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 *
v4l2_async_nf_find_v4l2_dev(struct v4l2_async_notifier * notifier)218 v4l2_async_nf_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
v4l2_async_nf_can_complete(struct v4l2_async_notifier * notifier)230 v4l2_async_nf_can_complete(struct v4l2_async_notifier *notifier)
231 {
232 struct v4l2_subdev *sd;
233
234 if (!list_empty(¬ifier->waiting))
235 return false;
236
237 list_for_each_entry(sd, ¬ifier->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_nf_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
v4l2_async_nf_try_complete(struct v4l2_async_notifier * notifier)254 v4l2_async_nf_try_complete(struct v4l2_async_notifier *notifier)
255 {
256 /* Quick check whether there are still more sub-devices here. */
257 if (!list_empty(¬ifier->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_nf_can_complete(notifier))
270 return 0;
271
272 return v4l2_async_nf_call_complete(notifier);
273 }
274
275 static int
276 v4l2_async_nf_try_all_subdevs(struct v4l2_async_notifier *notifier);
277
v4l2_async_match_notify(struct v4l2_async_notifier * notifier,struct v4l2_device * v4l2_dev,struct v4l2_subdev * sd,struct v4l2_async_subdev * asd)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)
282 {
283 struct v4l2_async_notifier *subdev_notifier;
284 int ret;
285
286 ret = v4l2_device_register_subdev(v4l2_dev, sd);
287 if (ret < 0)
288 return ret;
289
290 ret = v4l2_async_nf_call_bound(notifier, sd, asd);
291 if (ret < 0) {
292 v4l2_device_unregister_subdev(sd);
293 return ret;
294 }
295
296 /* Remove from the waiting list */
297 list_del(&asd->list);
298 sd->asd = asd;
299 sd->notifier = notifier;
300
301 /* Move from the global subdevice list to notifier's done */
302 list_move(&sd->async_list, ¬ifier->done);
303
304 /*
305 * See if the sub-device has a notifier. If not, return here.
306 */
307 subdev_notifier = v4l2_async_find_subdev_notifier(sd);
308 if (!subdev_notifier || subdev_notifier->parent)
309 return 0;
310
311 /*
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
314 * caller.
315 */
316 subdev_notifier->parent = notifier;
317
318 return v4l2_async_nf_try_all_subdevs(subdev_notifier);
319 }
320
321 /* Test all async sub-devices in a notifier for a match. */
322 static int
v4l2_async_nf_try_all_subdevs(struct v4l2_async_notifier * notifier)323 v4l2_async_nf_try_all_subdevs(struct v4l2_async_notifier *notifier)
324 {
325 struct v4l2_device *v4l2_dev =
326 v4l2_async_nf_find_v4l2_dev(notifier);
327 struct v4l2_subdev *sd;
328
329 if (!v4l2_dev)
330 return 0;
331
332 again:
333 list_for_each_entry(sd, &subdev_list, async_list) {
334 struct v4l2_async_subdev *asd;
335 int ret;
336
337 asd = v4l2_async_find_match(notifier, sd);
338 if (!asd)
339 continue;
340
341 ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd);
342 if (ret < 0)
343 return ret;
344
345 /*
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.
350 */
351 goto again;
352 }
353
354 return 0;
355 }
356
v4l2_async_cleanup(struct v4l2_subdev * sd)357 static void v4l2_async_cleanup(struct v4l2_subdev *sd)
358 {
359 v4l2_device_unregister_subdev(sd);
360 /*
361 * Subdevice driver will reprobe and put the subdev back
362 * onto the list
363 */
364 list_del_init(&sd->async_list);
365 sd->asd = NULL;
366 }
367
368 /* Unbind all sub-devices in the notifier tree. */
369 static void
v4l2_async_nf_unbind_all_subdevs(struct v4l2_async_notifier * notifier)370 v4l2_async_nf_unbind_all_subdevs(struct v4l2_async_notifier *notifier)
371 {
372 struct v4l2_subdev *sd, *tmp;
373
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);
377
378 if (subdev_notifier)
379 v4l2_async_nf_unbind_all_subdevs(subdev_notifier);
380
381 v4l2_async_nf_call_unbind(notifier, sd, sd->asd);
382 v4l2_async_cleanup(sd);
383
384 list_move(&sd->async_list, &subdev_list);
385 }
386
387 notifier->parent = NULL;
388 }
389
390 /* See if an async sub-device can be found in a notifier's lists. */
391 static bool
__v4l2_async_nf_has_async_subdev(struct v4l2_async_notifier * notifier,struct v4l2_async_subdev * asd)392 __v4l2_async_nf_has_async_subdev(struct v4l2_async_notifier *notifier,
393 struct v4l2_async_subdev *asd)
394 {
395 struct v4l2_async_subdev *asd_y;
396 struct v4l2_subdev *sd;
397
398 list_for_each_entry(asd_y, ¬ifier->waiting, list)
399 if (asd_equal(asd, asd_y))
400 return true;
401
402 list_for_each_entry(sd, ¬ifier->done, async_list) {
403 if (WARN_ON(!sd->asd))
404 continue;
405
406 if (asd_equal(asd, sd->asd))
407 return true;
408 }
409
410 return false;
411 }
412
413 /*
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.
417 */
418 static bool
v4l2_async_nf_has_async_subdev(struct v4l2_async_notifier * notifier,struct v4l2_async_subdev * asd,int this_index)419 v4l2_async_nf_has_async_subdev(struct v4l2_async_notifier *notifier,
420 struct v4l2_async_subdev *asd, int this_index)
421 {
422 struct v4l2_async_subdev *asd_y;
423 int j = 0;
424
425 lockdep_assert_held(&list_lock);
426
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)
430 break;
431 if (asd_equal(asd, asd_y))
432 return true;
433 }
434
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))
438 return true;
439
440 return false;
441 }
442
v4l2_async_nf_asd_valid(struct v4l2_async_notifier * notifier,struct v4l2_async_subdev * asd,int this_index)443 static int v4l2_async_nf_asd_valid(struct v4l2_async_notifier *notifier,
444 struct v4l2_async_subdev *asd,
445 int this_index)
446 {
447 struct device *dev =
448 notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL;
449
450 if (!asd)
451 return -EINVAL;
452
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");
458 return -EEXIST;
459 }
460 break;
461 default:
462 dev_err(dev, "Invalid match type %u on %p\n",
463 asd->match_type, asd);
464 return -EINVAL;
465 }
466
467 return 0;
468 }
469
v4l2_async_nf_init(struct v4l2_async_notifier * notifier)470 void v4l2_async_nf_init(struct v4l2_async_notifier *notifier)
471 {
472 INIT_LIST_HEAD(¬ifier->asd_list);
473 }
474 EXPORT_SYMBOL(v4l2_async_nf_init);
475
__v4l2_async_nf_register(struct v4l2_async_notifier * notifier)476 static int __v4l2_async_nf_register(struct v4l2_async_notifier *notifier)
477 {
478 struct v4l2_async_subdev *asd;
479 int ret, i = 0;
480
481 INIT_LIST_HEAD(¬ifier->waiting);
482 INIT_LIST_HEAD(¬ifier->done);
483
484 mutex_lock(&list_lock);
485
486 list_for_each_entry(asd, ¬ifier->asd_list, asd_list) {
487 ret = v4l2_async_nf_asd_valid(notifier, asd, i++);
488 if (ret)
489 goto err_unlock;
490
491 list_add_tail(&asd->list, ¬ifier->waiting);
492 }
493
494 ret = v4l2_async_nf_try_all_subdevs(notifier);
495 if (ret < 0)
496 goto err_unbind;
497
498 ret = v4l2_async_nf_try_complete(notifier);
499 if (ret < 0)
500 goto err_unbind;
501
502 /* Keep also completed notifiers on the list */
503 list_add(¬ifier->list, ¬ifier_list);
504
505 mutex_unlock(&list_lock);
506
507 return 0;
508
509 err_unbind:
510 /*
511 * On failure, unbind all sub-devices registered through this notifier.
512 */
513 v4l2_async_nf_unbind_all_subdevs(notifier);
514
515 err_unlock:
516 mutex_unlock(&list_lock);
517
518 return ret;
519 }
520
v4l2_async_nf_register(struct v4l2_device * v4l2_dev,struct v4l2_async_notifier * notifier)521 int v4l2_async_nf_register(struct v4l2_device *v4l2_dev,
522 struct v4l2_async_notifier *notifier)
523 {
524 int ret;
525
526 if (WARN_ON(!v4l2_dev || notifier->sd))
527 return -EINVAL;
528
529 notifier->v4l2_dev = v4l2_dev;
530
531 ret = __v4l2_async_nf_register(notifier);
532 if (ret)
533 notifier->v4l2_dev = NULL;
534
535 return ret;
536 }
537 EXPORT_SYMBOL(v4l2_async_nf_register);
538
v4l2_async_subdev_nf_register(struct v4l2_subdev * sd,struct v4l2_async_notifier * notifier)539 int v4l2_async_subdev_nf_register(struct v4l2_subdev *sd,
540 struct v4l2_async_notifier *notifier)
541 {
542 int ret;
543
544 if (WARN_ON(!sd || notifier->v4l2_dev))
545 return -EINVAL;
546
547 notifier->sd = sd;
548
549 ret = __v4l2_async_nf_register(notifier);
550 if (ret)
551 notifier->sd = NULL;
552
553 return ret;
554 }
555 EXPORT_SYMBOL(v4l2_async_subdev_nf_register);
556
557 static void
__v4l2_async_nf_unregister(struct v4l2_async_notifier * notifier)558 __v4l2_async_nf_unregister(struct v4l2_async_notifier *notifier)
559 {
560 if (!notifier || (!notifier->v4l2_dev && !notifier->sd))
561 return;
562
563 v4l2_async_nf_unbind_all_subdevs(notifier);
564
565 notifier->sd = NULL;
566 notifier->v4l2_dev = NULL;
567
568 list_del(¬ifier->list);
569 }
570
v4l2_async_nf_unregister(struct v4l2_async_notifier * notifier)571 void v4l2_async_nf_unregister(struct v4l2_async_notifier *notifier)
572 {
573 mutex_lock(&list_lock);
574
575 __v4l2_async_nf_unregister(notifier);
576
577 mutex_unlock(&list_lock);
578 }
579 EXPORT_SYMBOL(v4l2_async_nf_unregister);
580
__v4l2_async_nf_cleanup(struct v4l2_async_notifier * notifier)581 static void __v4l2_async_nf_cleanup(struct v4l2_async_notifier *notifier)
582 {
583 struct v4l2_async_subdev *asd, *tmp;
584
585 if (!notifier || !notifier->asd_list.next)
586 return;
587
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);
592 break;
593 default:
594 break;
595 }
596
597 list_del(&asd->asd_list);
598 kfree(asd);
599 }
600 }
601
v4l2_async_nf_cleanup(struct v4l2_async_notifier * notifier)602 void v4l2_async_nf_cleanup(struct v4l2_async_notifier *notifier)
603 {
604 mutex_lock(&list_lock);
605
606 __v4l2_async_nf_cleanup(notifier);
607
608 mutex_unlock(&list_lock);
609 }
610 EXPORT_SYMBOL_GPL(v4l2_async_nf_cleanup);
611
__v4l2_async_nf_add_subdev(struct v4l2_async_notifier * notifier,struct v4l2_async_subdev * asd)612 int __v4l2_async_nf_add_subdev(struct v4l2_async_notifier *notifier,
613 struct v4l2_async_subdev *asd)
614 {
615 int ret;
616
617 mutex_lock(&list_lock);
618
619 ret = v4l2_async_nf_asd_valid(notifier, asd, -1);
620 if (ret)
621 goto unlock;
622
623 list_add_tail(&asd->asd_list, ¬ifier->asd_list);
624
625 unlock:
626 mutex_unlock(&list_lock);
627 return ret;
628 }
629 EXPORT_SYMBOL_GPL(__v4l2_async_nf_add_subdev);
630
631 struct v4l2_async_subdev *
__v4l2_async_nf_add_fwnode(struct v4l2_async_notifier * notifier,struct fwnode_handle * fwnode,unsigned int asd_struct_size)632 __v4l2_async_nf_add_fwnode(struct v4l2_async_notifier *notifier,
633 struct fwnode_handle *fwnode,
634 unsigned int asd_struct_size)
635 {
636 struct v4l2_async_subdev *asd;
637 int ret;
638
639 asd = kzalloc(asd_struct_size, GFP_KERNEL);
640 if (!asd)
641 return ERR_PTR(-ENOMEM);
642
643 asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
644 asd->match.fwnode = fwnode_handle_get(fwnode);
645
646 ret = __v4l2_async_nf_add_subdev(notifier, asd);
647 if (ret) {
648 fwnode_handle_put(fwnode);
649 kfree(asd);
650 return ERR_PTR(ret);
651 }
652
653 return asd;
654 }
655 EXPORT_SYMBOL_GPL(__v4l2_async_nf_add_fwnode);
656
657 struct v4l2_async_subdev *
__v4l2_async_nf_add_fwnode_remote(struct v4l2_async_notifier * notif,struct fwnode_handle * endpoint,unsigned int asd_struct_size)658 __v4l2_async_nf_add_fwnode_remote(struct v4l2_async_notifier *notif,
659 struct fwnode_handle *endpoint,
660 unsigned int asd_struct_size)
661 {
662 struct v4l2_async_subdev *asd;
663 struct fwnode_handle *remote;
664
665 remote = fwnode_graph_get_remote_port_parent(endpoint);
666 if (!remote)
667 return ERR_PTR(-ENOTCONN);
668
669 asd = __v4l2_async_nf_add_fwnode(notif, remote, asd_struct_size);
670 /*
671 * Calling __v4l2_async_nf_add_fwnode grabs a refcount,
672 * so drop the one we got in fwnode_graph_get_remote_port_parent.
673 */
674 fwnode_handle_put(remote);
675 return asd;
676 }
677 EXPORT_SYMBOL_GPL(__v4l2_async_nf_add_fwnode_remote);
678
679 struct v4l2_async_subdev *
__v4l2_async_nf_add_i2c(struct v4l2_async_notifier * notifier,int adapter_id,unsigned short address,unsigned int asd_struct_size)680 __v4l2_async_nf_add_i2c(struct v4l2_async_notifier *notifier, int adapter_id,
681 unsigned short address, unsigned int asd_struct_size)
682 {
683 struct v4l2_async_subdev *asd;
684 int ret;
685
686 asd = kzalloc(asd_struct_size, GFP_KERNEL);
687 if (!asd)
688 return ERR_PTR(-ENOMEM);
689
690 asd->match_type = V4L2_ASYNC_MATCH_I2C;
691 asd->match.i2c.adapter_id = adapter_id;
692 asd->match.i2c.address = address;
693
694 ret = __v4l2_async_nf_add_subdev(notifier, asd);
695 if (ret) {
696 kfree(asd);
697 return ERR_PTR(ret);
698 }
699
700 return asd;
701 }
702 EXPORT_SYMBOL_GPL(__v4l2_async_nf_add_i2c);
703
v4l2_async_register_subdev(struct v4l2_subdev * sd)704 int v4l2_async_register_subdev(struct v4l2_subdev *sd)
705 {
706 struct v4l2_async_notifier *subdev_notifier;
707 struct v4l2_async_notifier *notifier;
708 int ret;
709
710 /*
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.
714 */
715 if (!sd->fwnode && sd->dev)
716 sd->fwnode = dev_fwnode(sd->dev);
717
718 mutex_lock(&list_lock);
719
720 INIT_LIST_HEAD(&sd->async_list);
721
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;
726
727 if (!v4l2_dev)
728 continue;
729
730 asd = v4l2_async_find_match(notifier, sd);
731 if (!asd)
732 continue;
733
734 ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd);
735 if (ret)
736 goto err_unbind;
737
738 ret = v4l2_async_nf_try_complete(notifier);
739 if (ret)
740 goto err_unbind;
741
742 goto out_unlock;
743 }
744
745 /* None matched, wait for hot-plugging */
746 list_add(&sd->async_list, &subdev_list);
747
748 out_unlock:
749 mutex_unlock(&list_lock);
750
751 return 0;
752
753 err_unbind:
754 /*
755 * Complete failed. Unbind the sub-devices bound through registering
756 * this async sub-device.
757 */
758 subdev_notifier = v4l2_async_find_subdev_notifier(sd);
759 if (subdev_notifier)
760 v4l2_async_nf_unbind_all_subdevs(subdev_notifier);
761
762 if (sd->asd)
763 v4l2_async_nf_call_unbind(notifier, sd, sd->asd);
764 v4l2_async_cleanup(sd);
765
766 mutex_unlock(&list_lock);
767
768 return ret;
769 }
770 EXPORT_SYMBOL(v4l2_async_register_subdev);
771
v4l2_async_unregister_subdev(struct v4l2_subdev * sd)772 void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
773 {
774 if (!sd->async_list.next)
775 return;
776
777 mutex_lock(&list_lock);
778
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;
783
784 if (sd->asd) {
785 struct v4l2_async_notifier *notifier = sd->notifier;
786
787 list_add(&sd->asd->list, ¬ifier->waiting);
788
789 v4l2_async_nf_call_unbind(notifier, sd, sd->asd);
790 }
791
792 v4l2_async_cleanup(sd);
793
794 mutex_unlock(&list_lock);
795 }
796 EXPORT_SYMBOL(v4l2_async_unregister_subdev);
797
print_waiting_subdev(struct seq_file * s,struct v4l2_async_subdev * asd)798 static void print_waiting_subdev(struct seq_file *s,
799 struct v4l2_async_subdev *asd)
800 {
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);
805 break;
806 case V4L2_ASYNC_MATCH_FWNODE: {
807 struct fwnode_handle *devnode, *fwnode = asd->match.fwnode;
808
809 devnode = fwnode_graph_is_endpoint(fwnode) ?
810 fwnode_graph_get_port_parent(fwnode) :
811 fwnode_handle_get(fwnode);
812
813 seq_printf(s, " [fwnode] dev=%s, node=%pfw\n",
814 devnode->dev ? dev_name(devnode->dev) : "nil",
815 fwnode);
816
817 fwnode_handle_put(devnode);
818 break;
819 }
820 }
821 }
822
823 static const char *
v4l2_async_nf_name(struct v4l2_async_notifier * notifier)824 v4l2_async_nf_name(struct v4l2_async_notifier *notifier)
825 {
826 if (notifier->v4l2_dev)
827 return notifier->v4l2_dev->name;
828 else if (notifier->sd)
829 return notifier->sd->name;
830 else
831 return "nil";
832 }
833
pending_subdevs_show(struct seq_file * s,void * data)834 static int pending_subdevs_show(struct seq_file *s, void *data)
835 {
836 struct v4l2_async_notifier *notif;
837 struct v4l2_async_subdev *asd;
838
839 mutex_lock(&list_lock);
840
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);
845 }
846
847 mutex_unlock(&list_lock);
848
849 return 0;
850 }
851 DEFINE_SHOW_ATTRIBUTE(pending_subdevs);
852
853 static struct dentry *v4l2_async_debugfs_dir;
854
v4l2_async_init(void)855 static int __init v4l2_async_init(void)
856 {
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);
861
862 return 0;
863 }
864
v4l2_async_exit(void)865 static void __exit v4l2_async_exit(void)
866 {
867 debugfs_remove_recursive(v4l2_async_debugfs_dir);
868 }
869
870 subsys_initcall(v4l2_async_init);
871 module_exit(v4l2_async_exit);
872
873 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
874 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
875 MODULE_AUTHOR("Ezequiel Garcia <ezequiel@collabora.com>");
876 MODULE_LICENSE("GPL");
877