blob: 70adbd9a01a2fe0d423326bac865023b649537aa [file] [log] [blame]
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -03001/*
2 * V4L2 asynchronous subdevice registration API
3 *
4 * Copyright (C) 2012-2013, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/device.h>
12#include <linux/err.h>
13#include <linux/i2c.h>
14#include <linux/list.h>
Tomasz Figa758d90e2017-06-19 00:53:43 -030015#include <linux/mm.h>
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030016#include <linux/module.h>
17#include <linux/mutex.h>
Sakari Ailusecdf0cf2016-08-16 06:54:59 -030018#include <linux/of.h>
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030019#include <linux/platform_device.h>
20#include <linux/slab.h>
21#include <linux/types.h>
22
23#include <media/v4l2-async.h>
24#include <media/v4l2-device.h>
Sakari Ailus9ca46532017-08-17 11:28:21 -040025#include <media/v4l2-fwnode.h>
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030026#include <media/v4l2-subdev.h>
27
Sakari Ailusddddc182017-09-01 08:27:32 -040028static int v4l2_async_notifier_call_bound(struct v4l2_async_notifier *n,
29 struct v4l2_subdev *subdev,
30 struct v4l2_async_subdev *asd)
31{
32 if (!n->ops || !n->ops->bound)
33 return 0;
34
35 return n->ops->bound(n, subdev, asd);
36}
37
38static void v4l2_async_notifier_call_unbind(struct v4l2_async_notifier *n,
39 struct v4l2_subdev *subdev,
40 struct v4l2_async_subdev *asd)
41{
42 if (!n->ops || !n->ops->unbind)
43 return;
44
45 n->ops->unbind(n, subdev, asd);
46}
47
48static int v4l2_async_notifier_call_complete(struct v4l2_async_notifier *n)
49{
50 if (!n->ops || !n->ops->complete)
51 return 0;
52
53 return n->ops->complete(n);
54}
55
Sakari Ailus86217652015-06-11 12:18:01 -070056static bool match_i2c(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030057{
Guennadi Liakhovetskife05e142013-06-24 05:13:51 -030058#if IS_ENABLED(CONFIG_I2C)
Sakari Ailus86217652015-06-11 12:18:01 -070059 struct i2c_client *client = i2c_verify_client(sd->dev);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030060 return client &&
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030061 asd->match.i2c.adapter_id == client->adapter->nr &&
62 asd->match.i2c.address == client->addr;
Guennadi Liakhovetskife05e142013-06-24 05:13:51 -030063#else
64 return false;
65#endif
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030066}
67
Sakari Ailus86217652015-06-11 12:18:01 -070068static bool match_devname(struct v4l2_subdev *sd,
69 struct v4l2_async_subdev *asd)
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030070{
Mauro Carvalho Chehab4e48afe2017-09-27 10:12:00 -040071 return !strcmp(asd->match.device_name, dev_name(sd->dev));
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030072}
73
Sakari Ailusecdf0cf2016-08-16 06:54:59 -030074static bool match_fwnode(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
75{
Mauro Carvalho Chehab4e48afe2017-09-27 10:12:00 -040076 return sd->fwnode == asd->match.fwnode;
Sakari Ailusecdf0cf2016-08-16 06:54:59 -030077}
78
Sakari Ailus86217652015-06-11 12:18:01 -070079static bool match_custom(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
80{
81 if (!asd->match.custom.match)
82 /* Match always */
83 return true;
84
85 return asd->match.custom.match(sd->dev, asd);
Sylwester Nawrockie7359f82013-07-19 12:21:29 -030086}
87
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030088static LIST_HEAD(subdev_list);
89static LIST_HEAD(notifier_list);
90static DEFINE_MUTEX(list_lock);
91
Sakari Ailusc8114d92017-09-04 12:44:39 -040092static struct v4l2_async_subdev *v4l2_async_find_match(
93 struct v4l2_async_notifier *notifier, struct v4l2_subdev *sd)
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030094{
Sakari Ailus86217652015-06-11 12:18:01 -070095 bool (*match)(struct v4l2_subdev *, struct v4l2_async_subdev *);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030096 struct v4l2_async_subdev *asd;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030097
98 list_for_each_entry(asd, &notifier->waiting, list) {
99 /* bus_type has been verified valid before */
Sylwester Nawrockicfca7642013-07-19 12:14:46 -0300100 switch (asd->match_type) {
101 case V4L2_ASYNC_MATCH_CUSTOM:
Sakari Ailus86217652015-06-11 12:18:01 -0700102 match = match_custom;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300103 break;
Sylwester Nawrockicfca7642013-07-19 12:14:46 -0300104 case V4L2_ASYNC_MATCH_DEVNAME:
105 match = match_devname;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300106 break;
Sylwester Nawrockicfca7642013-07-19 12:14:46 -0300107 case V4L2_ASYNC_MATCH_I2C:
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300108 match = match_i2c;
109 break;
Sakari Ailusecdf0cf2016-08-16 06:54:59 -0300110 case V4L2_ASYNC_MATCH_FWNODE:
111 match = match_fwnode;
112 break;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300113 default:
114 /* Cannot happen, unless someone breaks us */
115 WARN_ON(true);
116 return NULL;
117 }
118
119 /* match cannot be NULL here */
Sakari Ailus86217652015-06-11 12:18:01 -0700120 if (match(sd, asd))
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300121 return asd;
122 }
123
124 return NULL;
125}
126
Steve Longerbeama6e70032018-09-29 15:54:05 -0400127/* Compare two async sub-device descriptors for equivalence */
128static bool asd_equal(struct v4l2_async_subdev *asd_x,
129 struct v4l2_async_subdev *asd_y)
130{
131 if (asd_x->match_type != asd_y->match_type)
132 return false;
133
134 switch (asd_x->match_type) {
135 case V4L2_ASYNC_MATCH_DEVNAME:
136 return strcmp(asd_x->match.device_name,
137 asd_y->match.device_name) == 0;
138 case V4L2_ASYNC_MATCH_I2C:
139 return asd_x->match.i2c.adapter_id ==
140 asd_y->match.i2c.adapter_id &&
141 asd_x->match.i2c.address ==
142 asd_y->match.i2c.address;
143 case V4L2_ASYNC_MATCH_FWNODE:
144 return asd_x->match.fwnode == asd_y->match.fwnode;
145 default:
146 break;
147 }
148
149 return false;
150}
151
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400152/* Find the sub-device notifier registered by a sub-device driver. */
153static struct v4l2_async_notifier *v4l2_async_find_subdev_notifier(
154 struct v4l2_subdev *sd)
155{
156 struct v4l2_async_notifier *n;
157
158 list_for_each_entry(n, &notifier_list, list)
159 if (n->sd == sd)
160 return n;
161
162 return NULL;
163}
164
165/* Get v4l2_device related to the notifier if one can be found. */
166static struct v4l2_device *v4l2_async_notifier_find_v4l2_dev(
167 struct v4l2_async_notifier *notifier)
168{
169 while (notifier->parent)
170 notifier = notifier->parent;
171
172 return notifier->v4l2_dev;
173}
174
175/*
176 * Return true if all child sub-device notifiers are complete, false otherwise.
177 */
178static bool v4l2_async_notifier_can_complete(
179 struct v4l2_async_notifier *notifier)
180{
181 struct v4l2_subdev *sd;
182
183 if (!list_empty(&notifier->waiting))
184 return false;
185
186 list_for_each_entry(sd, &notifier->done, async_list) {
187 struct v4l2_async_notifier *subdev_notifier =
188 v4l2_async_find_subdev_notifier(sd);
189
190 if (subdev_notifier &&
191 !v4l2_async_notifier_can_complete(subdev_notifier))
192 return false;
193 }
194
195 return true;
196}
197
198/*
199 * Complete the master notifier if possible. This is done when all async
200 * sub-devices have been bound; v4l2_device is also available then.
201 */
202static int v4l2_async_notifier_try_complete(
203 struct v4l2_async_notifier *notifier)
204{
205 /* Quick check whether there are still more sub-devices here. */
206 if (!list_empty(&notifier->waiting))
207 return 0;
208
209 /* Check the entire notifier tree; find the root notifier first. */
210 while (notifier->parent)
211 notifier = notifier->parent;
212
213 /* This is root if it has v4l2_dev. */
214 if (!notifier->v4l2_dev)
215 return 0;
216
217 /* Is everything ready? */
218 if (!v4l2_async_notifier_can_complete(notifier))
219 return 0;
220
221 return v4l2_async_notifier_call_complete(notifier);
222}
223
224static int v4l2_async_notifier_try_all_subdevs(
225 struct v4l2_async_notifier *notifier);
226
Sakari Ailusc8114d92017-09-04 12:44:39 -0400227static int v4l2_async_match_notify(struct v4l2_async_notifier *notifier,
Sakari Ailusa3620cb2017-09-24 20:48:08 -0400228 struct v4l2_device *v4l2_dev,
Sakari Ailusc8114d92017-09-04 12:44:39 -0400229 struct v4l2_subdev *sd,
230 struct v4l2_async_subdev *asd)
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300231{
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400232 struct v4l2_async_notifier *subdev_notifier;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300233 int ret;
234
Sakari Ailusa3620cb2017-09-24 20:48:08 -0400235 ret = v4l2_device_register_subdev(v4l2_dev, sd);
Sakari Ailusddddc182017-09-01 08:27:32 -0400236 if (ret < 0)
237 return ret;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300238
Sakari Ailus24def9b2017-07-17 10:04:20 -0400239 ret = v4l2_async_notifier_call_bound(notifier, sd, asd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300240 if (ret < 0) {
Sakari Ailus24def9b2017-07-17 10:04:20 -0400241 v4l2_device_unregister_subdev(sd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300242 return ret;
243 }
244
Tuukka Toivonen47b037a2017-01-27 08:32:56 -0200245 /* Remove from the waiting list */
246 list_del(&asd->list);
247 sd->asd = asd;
248 sd->notifier = notifier;
249
250 /* Move from the global subdevice list to notifier's done */
251 list_move(&sd->async_list, &notifier->done);
252
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400253 /*
254 * See if the sub-device has a notifier. If not, return here.
255 */
256 subdev_notifier = v4l2_async_find_subdev_notifier(sd);
257 if (!subdev_notifier || subdev_notifier->parent)
258 return 0;
259
260 /*
261 * Proceed with checking for the sub-device notifier's async
262 * sub-devices, and return the result. The error will be handled by the
263 * caller.
264 */
265 subdev_notifier->parent = notifier;
266
267 return v4l2_async_notifier_try_all_subdevs(subdev_notifier);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300268}
269
Sakari Ailusa3620cb2017-09-24 20:48:08 -0400270/* Test all async sub-devices in a notifier for a match. */
271static int v4l2_async_notifier_try_all_subdevs(
272 struct v4l2_async_notifier *notifier)
273{
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400274 struct v4l2_device *v4l2_dev =
275 v4l2_async_notifier_find_v4l2_dev(notifier);
276 struct v4l2_subdev *sd;
Sakari Ailusa3620cb2017-09-24 20:48:08 -0400277
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400278 if (!v4l2_dev)
279 return 0;
280
281again:
282 list_for_each_entry(sd, &subdev_list, async_list) {
Sakari Ailusa3620cb2017-09-24 20:48:08 -0400283 struct v4l2_async_subdev *asd;
284 int ret;
285
286 asd = v4l2_async_find_match(notifier, sd);
287 if (!asd)
288 continue;
289
290 ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd);
291 if (ret < 0)
292 return ret;
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400293
294 /*
295 * v4l2_async_match_notify() may lead to registering a
296 * new notifier and thus changing the async subdevs
297 * list. In order to proceed safely from here, restart
298 * parsing the list from the beginning.
299 */
300 goto again;
Sakari Ailusa3620cb2017-09-24 20:48:08 -0400301 }
302
303 return 0;
304}
305
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300306static void v4l2_async_cleanup(struct v4l2_subdev *sd)
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300307{
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300308 v4l2_device_unregister_subdev(sd);
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300309 /* Subdevice driver will reprobe and put the subdev back onto the list */
310 list_del_init(&sd->async_list);
311 sd->asd = NULL;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300312}
313
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400314/* Unbind all sub-devices in the notifier tree. */
Sakari Ailusfb45f432017-10-02 06:24:54 -0400315static void v4l2_async_notifier_unbind_all_subdevs(
316 struct v4l2_async_notifier *notifier)
317{
318 struct v4l2_subdev *sd, *tmp;
319
320 list_for_each_entry_safe(sd, tmp, &notifier->done, async_list) {
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400321 struct v4l2_async_notifier *subdev_notifier =
322 v4l2_async_find_subdev_notifier(sd);
323
324 if (subdev_notifier)
325 v4l2_async_notifier_unbind_all_subdevs(subdev_notifier);
326
Sakari Ailusddddc182017-09-01 08:27:32 -0400327 v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
Sakari Ailusfb45f432017-10-02 06:24:54 -0400328 v4l2_async_cleanup(sd);
329
330 list_move(&sd->async_list, &subdev_list);
331 }
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400332
333 notifier->parent = NULL;
Sakari Ailusfb45f432017-10-02 06:24:54 -0400334}
335
Steve Longerbeama6e70032018-09-29 15:54:05 -0400336/* See if an async sub-device can be found in a notifier's lists. */
337static bool
338__v4l2_async_notifier_has_async_subdev(struct v4l2_async_notifier *notifier,
339 struct v4l2_async_subdev *asd)
Sakari Ailus466cae62017-09-20 03:51:54 -0400340{
Steve Longerbeama6e70032018-09-29 15:54:05 -0400341 struct v4l2_async_subdev *asd_y;
Sakari Ailus466cae62017-09-20 03:51:54 -0400342 struct v4l2_subdev *sd;
343
Steve Longerbeama6e70032018-09-29 15:54:05 -0400344 list_for_each_entry(asd_y, &notifier->waiting, list)
345 if (asd_equal(asd, asd_y))
Sakari Ailus466cae62017-09-20 03:51:54 -0400346 return true;
Sakari Ailus466cae62017-09-20 03:51:54 -0400347
348 list_for_each_entry(sd, &notifier->done, async_list) {
349 if (WARN_ON(!sd->asd))
350 continue;
351
Steve Longerbeama6e70032018-09-29 15:54:05 -0400352 if (asd_equal(asd, sd->asd))
Sakari Ailus466cae62017-09-20 03:51:54 -0400353 return true;
354 }
355
356 return false;
357}
358
359/*
Steve Longerbeama6e70032018-09-29 15:54:05 -0400360 * Find out whether an async sub-device was set up already or
Sakari Ailus466cae62017-09-20 03:51:54 -0400361 * whether it exists in a given notifier before @this_index.
Steve Longerbeam66beb322018-09-29 15:54:19 -0400362 * If @this_index < 0, search the notifier's entire @asd_list.
Sakari Ailus466cae62017-09-20 03:51:54 -0400363 */
Steve Longerbeama6e70032018-09-29 15:54:05 -0400364static bool
365v4l2_async_notifier_has_async_subdev(struct v4l2_async_notifier *notifier,
366 struct v4l2_async_subdev *asd,
Steve Longerbeam66beb322018-09-29 15:54:19 -0400367 int this_index)
Sakari Ailus466cae62017-09-20 03:51:54 -0400368{
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -0400369 struct v4l2_async_subdev *asd_y;
Steve Longerbeam66beb322018-09-29 15:54:19 -0400370 int j = 0;
Sakari Ailus466cae62017-09-20 03:51:54 -0400371
372 lockdep_assert_held(&list_lock);
373
Steve Longerbeama6e70032018-09-29 15:54:05 -0400374 /* Check that an asd is not being added more than once. */
Steve Longerbeam66beb322018-09-29 15:54:19 -0400375 list_for_each_entry(asd_y, &notifier->asd_list, asd_list) {
376 if (this_index >= 0 && j++ >= this_index)
377 break;
378 if (asd_equal(asd, asd_y))
379 return true;
Sakari Ailus466cae62017-09-20 03:51:54 -0400380 }
381
Steve Longerbeama6e70032018-09-29 15:54:05 -0400382 /* Check that an asd does not exist in other notifiers. */
Sakari Ailus466cae62017-09-20 03:51:54 -0400383 list_for_each_entry(notifier, &notifier_list, list)
Steve Longerbeama6e70032018-09-29 15:54:05 -0400384 if (__v4l2_async_notifier_has_async_subdev(notifier, asd))
Sakari Ailus466cae62017-09-20 03:51:54 -0400385 return true;
386
387 return false;
388}
389
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -0400390static int v4l2_async_notifier_asd_valid(struct v4l2_async_notifier *notifier,
391 struct v4l2_async_subdev *asd,
Steve Longerbeam66beb322018-09-29 15:54:19 -0400392 int this_index)
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300393{
Sakari Ailus466cae62017-09-20 03:51:54 -0400394 struct device *dev =
395 notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL;
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -0400396
397 if (!asd)
398 return -EINVAL;
399
400 switch (asd->match_type) {
401 case V4L2_ASYNC_MATCH_CUSTOM:
402 case V4L2_ASYNC_MATCH_DEVNAME:
403 case V4L2_ASYNC_MATCH_I2C:
404 case V4L2_ASYNC_MATCH_FWNODE:
405 if (v4l2_async_notifier_has_async_subdev(notifier, asd,
406 this_index)) {
407 dev_dbg(dev, "subdev descriptor already listed in this or other notifiers\n");
408 return -EEXIST;
409 }
410 break;
411 default:
412 dev_err(dev, "Invalid match type %u on %p\n",
413 asd->match_type, asd);
414 return -EINVAL;
415 }
416
417 return 0;
418}
419
420void v4l2_async_notifier_init(struct v4l2_async_notifier *notifier)
421{
422 mutex_lock(&list_lock);
423
424 INIT_LIST_HEAD(&notifier->asd_list);
425
426 mutex_unlock(&list_lock);
427}
428EXPORT_SYMBOL(v4l2_async_notifier_init);
429
430static int __v4l2_async_notifier_register(struct v4l2_async_notifier *notifier)
431{
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300432 struct v4l2_async_subdev *asd;
Steve Longerbeam66beb322018-09-29 15:54:19 -0400433 int ret, i = 0;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300434
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300435 INIT_LIST_HEAD(&notifier->waiting);
436 INIT_LIST_HEAD(&notifier->done);
437
Sakari Ailus466cae62017-09-20 03:51:54 -0400438 mutex_lock(&list_lock);
439
Steve Longerbeam66beb322018-09-29 15:54:19 -0400440 list_for_each_entry(asd, &notifier->asd_list, asd_list) {
441 ret = v4l2_async_notifier_asd_valid(notifier, asd, i++);
442 if (ret)
443 goto err_unlock;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300444
Steve Longerbeam66beb322018-09-29 15:54:19 -0400445 list_add_tail(&asd->list, &notifier->waiting);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300446 }
447
Sakari Ailusa3620cb2017-09-24 20:48:08 -0400448 ret = v4l2_async_notifier_try_all_subdevs(notifier);
Sakari Ailus466cae62017-09-20 03:51:54 -0400449 if (ret < 0)
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400450 goto err_unbind;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300451
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400452 ret = v4l2_async_notifier_try_complete(notifier);
Sakari Ailus466cae62017-09-20 03:51:54 -0400453 if (ret < 0)
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400454 goto err_unbind;
Sakari Ailusfb45f432017-10-02 06:24:54 -0400455
Tuukka Toivonen47b037a2017-01-27 08:32:56 -0200456 /* Keep also completed notifiers on the list */
457 list_add(&notifier->list, &notifier_list);
458
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300459 mutex_unlock(&list_lock);
460
461 return 0;
Sakari Ailusfb45f432017-10-02 06:24:54 -0400462
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400463err_unbind:
464 /*
465 * On failure, unbind all sub-devices registered through this notifier.
466 */
Sakari Ailusfb45f432017-10-02 06:24:54 -0400467 v4l2_async_notifier_unbind_all_subdevs(notifier);
468
Sakari Ailus466cae62017-09-20 03:51:54 -0400469err_unlock:
Sakari Ailusfb45f432017-10-02 06:24:54 -0400470 mutex_unlock(&list_lock);
471
472 return ret;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300473}
Sakari Ailusa3620cb2017-09-24 20:48:08 -0400474
475int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
476 struct v4l2_async_notifier *notifier)
477{
478 int ret;
479
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400480 if (WARN_ON(!v4l2_dev || notifier->sd))
Sakari Ailusa3620cb2017-09-24 20:48:08 -0400481 return -EINVAL;
482
483 notifier->v4l2_dev = v4l2_dev;
484
485 ret = __v4l2_async_notifier_register(notifier);
486 if (ret)
487 notifier->v4l2_dev = NULL;
488
489 return ret;
490}
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300491EXPORT_SYMBOL(v4l2_async_notifier_register);
492
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400493int v4l2_async_subdev_notifier_register(struct v4l2_subdev *sd,
494 struct v4l2_async_notifier *notifier)
495{
496 int ret;
497
498 if (WARN_ON(!sd || notifier->v4l2_dev))
499 return -EINVAL;
500
501 notifier->sd = sd;
502
503 ret = __v4l2_async_notifier_register(notifier);
504 if (ret)
505 notifier->sd = NULL;
506
507 return ret;
508}
509EXPORT_SYMBOL(v4l2_async_subdev_notifier_register);
510
Sakari Ailusaef69d52017-09-24 18:47:44 -0400511static void __v4l2_async_notifier_unregister(
512 struct v4l2_async_notifier *notifier)
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300513{
Sakari Ailusaef69d52017-09-24 18:47:44 -0400514 if (!notifier || (!notifier->v4l2_dev && !notifier->sd))
Laurent Pinchart8e3fbfe2013-07-03 07:49:06 -0300515 return;
516
Sakari Ailusfb45f432017-10-02 06:24:54 -0400517 v4l2_async_notifier_unbind_all_subdevs(notifier);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300518
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400519 notifier->sd = NULL;
Laurent Pinchart8e3fbfe2013-07-03 07:49:06 -0300520 notifier->v4l2_dev = NULL;
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400521
522 list_del(&notifier->list);
Sakari Ailusaef69d52017-09-24 18:47:44 -0400523}
524
525void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
526{
527 mutex_lock(&list_lock);
528
529 __v4l2_async_notifier_unregister(notifier);
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400530
531 mutex_unlock(&list_lock);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300532}
533EXPORT_SYMBOL(v4l2_async_notifier_unregister);
534
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -0400535static void __v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier)
Sakari Ailus9ca46532017-08-17 11:28:21 -0400536{
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -0400537 struct v4l2_async_subdev *asd, *tmp;
Sakari Ailus9ca46532017-08-17 11:28:21 -0400538
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -0400539 if (!notifier)
Sakari Ailus9ca46532017-08-17 11:28:21 -0400540 return;
541
Steve Longerbeam66beb322018-09-29 15:54:19 -0400542 list_for_each_entry_safe(asd, tmp, &notifier->asd_list, asd_list) {
543 switch (asd->match_type) {
544 case V4L2_ASYNC_MATCH_FWNODE:
545 fwnode_handle_put(asd->match.fwnode);
546 break;
547 default:
548 break;
Sakari Ailus9ca46532017-08-17 11:28:21 -0400549 }
550
Steve Longerbeam66beb322018-09-29 15:54:19 -0400551 list_del(&asd->asd_list);
552 kfree(asd);
Sakari Ailus9ca46532017-08-17 11:28:21 -0400553 }
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -0400554}
Sakari Ailus9ca46532017-08-17 11:28:21 -0400555
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -0400556void v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier)
557{
558 mutex_lock(&list_lock);
559
560 __v4l2_async_notifier_cleanup(notifier);
561
562 mutex_unlock(&list_lock);
Sakari Ailus9ca46532017-08-17 11:28:21 -0400563}
564EXPORT_SYMBOL_GPL(v4l2_async_notifier_cleanup);
565
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -0400566int v4l2_async_notifier_add_subdev(struct v4l2_async_notifier *notifier,
567 struct v4l2_async_subdev *asd)
568{
569 int ret;
570
571 mutex_lock(&list_lock);
572
Steve Longerbeam66beb322018-09-29 15:54:19 -0400573 ret = v4l2_async_notifier_asd_valid(notifier, asd, -1);
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -0400574 if (ret)
575 goto unlock;
576
577 list_add_tail(&asd->asd_list, &notifier->asd_list);
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -0400578
579unlock:
580 mutex_unlock(&list_lock);
581 return ret;
582}
583EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_subdev);
584
Steve Longerbeam23989b42018-09-29 15:54:07 -0400585struct v4l2_async_subdev *
586v4l2_async_notifier_add_fwnode_subdev(struct v4l2_async_notifier *notifier,
587 struct fwnode_handle *fwnode,
588 unsigned int asd_struct_size)
589{
590 struct v4l2_async_subdev *asd;
591 int ret;
592
593 asd = kzalloc(asd_struct_size, GFP_KERNEL);
594 if (!asd)
595 return ERR_PTR(-ENOMEM);
596
597 asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
598 asd->match.fwnode = fwnode;
599
600 ret = v4l2_async_notifier_add_subdev(notifier, asd);
601 if (ret) {
602 kfree(asd);
603 return ERR_PTR(ret);
604 }
605
606 return asd;
607}
608EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_fwnode_subdev);
609
610struct v4l2_async_subdev *
611v4l2_async_notifier_add_i2c_subdev(struct v4l2_async_notifier *notifier,
612 int adapter_id, unsigned short address,
613 unsigned int asd_struct_size)
614{
615 struct v4l2_async_subdev *asd;
616 int ret;
617
618 asd = kzalloc(asd_struct_size, GFP_KERNEL);
619 if (!asd)
620 return ERR_PTR(-ENOMEM);
621
622 asd->match_type = V4L2_ASYNC_MATCH_I2C;
623 asd->match.i2c.adapter_id = adapter_id;
624 asd->match.i2c.address = address;
625
626 ret = v4l2_async_notifier_add_subdev(notifier, asd);
627 if (ret) {
628 kfree(asd);
629 return ERR_PTR(ret);
630 }
631
632 return asd;
633}
634EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_i2c_subdev);
635
636struct v4l2_async_subdev *
637v4l2_async_notifier_add_devname_subdev(struct v4l2_async_notifier *notifier,
638 const char *device_name,
639 unsigned int asd_struct_size)
640{
641 struct v4l2_async_subdev *asd;
642 int ret;
643
644 asd = kzalloc(asd_struct_size, GFP_KERNEL);
645 if (!asd)
646 return ERR_PTR(-ENOMEM);
647
648 asd->match_type = V4L2_ASYNC_MATCH_DEVNAME;
649 asd->match.device_name = device_name;
650
651 ret = v4l2_async_notifier_add_subdev(notifier, asd);
652 if (ret) {
653 kfree(asd);
654 return ERR_PTR(ret);
655 }
656
657 return asd;
658}
659EXPORT_SYMBOL_GPL(v4l2_async_notifier_add_devname_subdev);
660
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300661int v4l2_async_register_subdev(struct v4l2_subdev *sd)
662{
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400663 struct v4l2_async_notifier *subdev_notifier;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300664 struct v4l2_async_notifier *notifier;
Sakari Ailusfb45f432017-10-02 06:24:54 -0400665 int ret;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300666
Sakari Ailus86217652015-06-11 12:18:01 -0700667 /*
668 * No reference taken. The reference is held by the device
669 * (struct v4l2_subdev.dev), and async sub-device does not
670 * exist independently of the device at any point of time.
671 */
Sakari Ailus859969b2016-08-26 20:17:25 -0300672 if (!sd->fwnode && sd->dev)
673 sd->fwnode = dev_fwnode(sd->dev);
Sakari Ailus86217652015-06-11 12:18:01 -0700674
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300675 mutex_lock(&list_lock);
676
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300677 INIT_LIST_HEAD(&sd->async_list);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300678
679 list_for_each_entry(notifier, &notifier_list, list) {
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400680 struct v4l2_device *v4l2_dev =
681 v4l2_async_notifier_find_v4l2_dev(notifier);
682 struct v4l2_async_subdev *asd;
Sakari Ailusfb45f432017-10-02 06:24:54 -0400683
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400684 if (!v4l2_dev)
685 continue;
686
687 asd = v4l2_async_find_match(notifier, sd);
Sakari Ailusfb45f432017-10-02 06:24:54 -0400688 if (!asd)
689 continue;
690
Niklas Söderlund487cc852017-11-15 10:43:58 -0500691 ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd);
Sakari Ailusfb45f432017-10-02 06:24:54 -0400692 if (ret)
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400693 goto err_unbind;
Sakari Ailusfb45f432017-10-02 06:24:54 -0400694
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400695 ret = v4l2_async_notifier_try_complete(notifier);
Sakari Ailusfb45f432017-10-02 06:24:54 -0400696 if (ret)
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400697 goto err_unbind;
Sakari Ailusfb45f432017-10-02 06:24:54 -0400698
699 goto out_unlock;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300700 }
701
702 /* None matched, wait for hot-plugging */
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300703 list_add(&sd->async_list, &subdev_list);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300704
Sakari Ailusfb45f432017-10-02 06:24:54 -0400705out_unlock:
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300706 mutex_unlock(&list_lock);
707
708 return 0;
Sakari Ailusfb45f432017-10-02 06:24:54 -0400709
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400710err_unbind:
711 /*
712 * Complete failed. Unbind the sub-devices bound through registering
713 * this async sub-device.
714 */
715 subdev_notifier = v4l2_async_find_subdev_notifier(sd);
716 if (subdev_notifier)
717 v4l2_async_notifier_unbind_all_subdevs(subdev_notifier);
718
719 if (sd->asd)
720 v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
Sakari Ailusfb45f432017-10-02 06:24:54 -0400721 v4l2_async_cleanup(sd);
722
Sakari Ailusfb45f432017-10-02 06:24:54 -0400723 mutex_unlock(&list_lock);
724
725 return ret;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300726}
727EXPORT_SYMBOL(v4l2_async_register_subdev);
728
729void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
730{
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300731 mutex_lock(&list_lock);
732
Sakari Ailusaef69d52017-09-24 18:47:44 -0400733 __v4l2_async_notifier_unregister(sd->subdev_notifier);
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -0400734 __v4l2_async_notifier_cleanup(sd->subdev_notifier);
Sakari Ailusaef69d52017-09-24 18:47:44 -0400735 kfree(sd->subdev_notifier);
736 sd->subdev_notifier = NULL;
737
Sakari Ailus7fc4fdb2017-10-03 02:26:32 -0400738 if (sd->asd) {
739 struct v4l2_async_notifier *notifier = sd->notifier;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300740
Sakari Ailus7fc4fdb2017-10-03 02:26:32 -0400741 list_add(&sd->asd->list, &notifier->waiting);
742
Sakari Ailusddddc182017-09-01 08:27:32 -0400743 v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
Sakari Ailus7fc4fdb2017-10-03 02:26:32 -0400744 }
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300745
Niklas Söderlund633d1852017-10-02 16:16:52 -0400746 v4l2_async_cleanup(sd);
747
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300748 mutex_unlock(&list_lock);
749}
750EXPORT_SYMBOL(v4l2_async_unregister_subdev);