2 * V4L2 asynchronous subdevice registration API
4 * Copyright (C) 2012-2013, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
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.
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/i2c.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/types.h>
21 #include <media/v4l2-async.h>
22 #include <media/v4l2-device.h>
23 #include <media/v4l2-subdev.h>
25 static bool match_i2c(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
27 #if IS_ENABLED(CONFIG_I2C)
28 struct i2c_client *client = i2c_verify_client(sd->dev);
30 asd->match.i2c.adapter_id == client->adapter->nr &&
31 asd->match.i2c.address == client->addr;
37 static bool match_devname(struct v4l2_subdev *sd,
38 struct v4l2_async_subdev *asd)
40 return !strcmp(asd->match.device_name.name, dev_name(sd->dev));
43 static bool match_of(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
45 return !of_node_cmp(of_node_full_name(sd->of_node),
46 of_node_full_name(asd->match.of.node));
49 static bool match_custom(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
51 if (!asd->match.custom.match)
55 return asd->match.custom.match(sd->dev, asd);
58 static LIST_HEAD(subdev_list);
59 static LIST_HEAD(notifier_list);
60 static DEFINE_MUTEX(list_lock);
62 static struct v4l2_async_subdev *v4l2_async_belongs(struct v4l2_async_notifier *notifier,
63 struct v4l2_subdev *sd)
65 bool (*match)(struct v4l2_subdev *, struct v4l2_async_subdev *);
66 struct v4l2_async_subdev *asd;
68 list_for_each_entry(asd, ¬ifier->waiting, list) {
69 /* bus_type has been verified valid before */
70 switch (asd->match_type) {
71 case V4L2_ASYNC_MATCH_CUSTOM:
74 case V4L2_ASYNC_MATCH_DEVNAME:
75 match = match_devname;
77 case V4L2_ASYNC_MATCH_I2C:
80 case V4L2_ASYNC_MATCH_OF:
84 /* Cannot happen, unless someone breaks us */
89 /* match cannot be NULL here */
97 static int v4l2_async_test_notify(struct v4l2_async_notifier *notifier,
98 struct v4l2_subdev *sd,
99 struct v4l2_async_subdev *asd)
103 if (notifier->bound) {
104 ret = notifier->bound(notifier, sd, asd);
109 ret = v4l2_device_register_subdev(notifier->v4l2_dev, sd);
111 if (notifier->unbind)
112 notifier->unbind(notifier, sd, asd);
116 /* Remove from the waiting list */
117 list_del(&asd->list);
119 sd->notifier = notifier;
121 /* Move from the global subdevice list to notifier's done */
122 list_move(&sd->async_list, ¬ifier->done);
124 if (list_empty(¬ifier->waiting) && notifier->complete)
125 return notifier->complete(notifier);
130 static void v4l2_async_cleanup(struct v4l2_subdev *sd)
132 v4l2_device_unregister_subdev(sd);
133 /* Subdevice driver will reprobe and put the subdev back onto the list */
134 list_del_init(&sd->async_list);
139 int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
140 struct v4l2_async_notifier *notifier)
142 struct v4l2_subdev *sd, *tmp;
143 struct v4l2_async_subdev *asd;
146 if (!notifier->num_subdevs || notifier->num_subdevs > V4L2_MAX_SUBDEVS)
149 notifier->v4l2_dev = v4l2_dev;
150 INIT_LIST_HEAD(¬ifier->waiting);
151 INIT_LIST_HEAD(¬ifier->done);
153 for (i = 0; i < notifier->num_subdevs; i++) {
154 asd = notifier->subdevs[i];
156 switch (asd->match_type) {
157 case V4L2_ASYNC_MATCH_CUSTOM:
158 case V4L2_ASYNC_MATCH_DEVNAME:
159 case V4L2_ASYNC_MATCH_I2C:
160 case V4L2_ASYNC_MATCH_OF:
163 dev_err(notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL,
164 "Invalid match type %u on %p\n",
165 asd->match_type, asd);
168 list_add_tail(&asd->list, ¬ifier->waiting);
171 mutex_lock(&list_lock);
173 list_for_each_entry_safe(sd, tmp, &subdev_list, async_list) {
176 asd = v4l2_async_belongs(notifier, sd);
180 ret = v4l2_async_test_notify(notifier, sd, asd);
182 mutex_unlock(&list_lock);
187 /* Keep also completed notifiers on the list */
188 list_add(¬ifier->list, ¬ifier_list);
190 mutex_unlock(&list_lock);
194 EXPORT_SYMBOL(v4l2_async_notifier_register);
196 void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
198 struct v4l2_subdev *sd, *tmp;
199 unsigned int notif_n_subdev = notifier->num_subdevs;
200 unsigned int n_subdev = min(notif_n_subdev, V4L2_MAX_SUBDEVS);
204 if (!notifier->v4l2_dev)
207 dev = kmalloc_array(n_subdev, sizeof(*dev), GFP_KERNEL);
209 dev_err(notifier->v4l2_dev->dev,
210 "Failed to allocate device cache!\n");
213 mutex_lock(&list_lock);
215 list_del(¬ifier->list);
217 list_for_each_entry_safe(sd, tmp, ¬ifier->done, async_list) {
220 d = get_device(sd->dev);
222 v4l2_async_cleanup(sd);
224 /* If we handled USB devices, we'd have to lock the parent too */
225 device_release_driver(d);
227 if (notifier->unbind)
228 notifier->unbind(notifier, sd, sd->asd);
231 * Store device at the device cache, in order to call
232 * put_device() on the final step
240 mutex_unlock(&list_lock);
243 * Call device_attach() to reprobe devices
245 * NOTE: If dev allocation fails, i is 0, and the whole loop won't be
249 struct device *d = dev[i];
251 if (d && device_attach(d) < 0) {
252 const char *name = "(none)";
253 int lock = device_trylock(d);
255 if (lock && d->driver)
256 name = d->driver->name;
257 dev_err(d, "Failed to re-probe to %s\n", name);
265 notifier->v4l2_dev = NULL;
268 * Don't care about the waiting list, it is initialised and populated
269 * upon notifier registration.
272 EXPORT_SYMBOL(v4l2_async_notifier_unregister);
274 int v4l2_async_register_subdev(struct v4l2_subdev *sd)
276 struct v4l2_async_notifier *notifier;
279 * No reference taken. The reference is held by the device
280 * (struct v4l2_subdev.dev), and async sub-device does not
281 * exist independently of the device at any point of time.
283 if (!sd->of_node && sd->dev)
284 sd->of_node = sd->dev->of_node;
286 mutex_lock(&list_lock);
288 INIT_LIST_HEAD(&sd->async_list);
290 list_for_each_entry(notifier, ¬ifier_list, list) {
291 struct v4l2_async_subdev *asd = v4l2_async_belongs(notifier, sd);
293 int ret = v4l2_async_test_notify(notifier, sd, asd);
294 mutex_unlock(&list_lock);
299 /* None matched, wait for hot-plugging */
300 list_add(&sd->async_list, &subdev_list);
302 mutex_unlock(&list_lock);
306 EXPORT_SYMBOL(v4l2_async_register_subdev);
308 void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
310 struct v4l2_async_notifier *notifier = sd->notifier;
313 if (!list_empty(&sd->async_list))
314 v4l2_async_cleanup(sd);
318 mutex_lock(&list_lock);
320 list_add(&sd->asd->list, ¬ifier->waiting);
322 v4l2_async_cleanup(sd);
324 if (notifier->unbind)
325 notifier->unbind(notifier, sd, sd->asd);
327 mutex_unlock(&list_lock);
329 EXPORT_SYMBOL(v4l2_async_unregister_subdev);