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