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>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21 #include <linux/types.h>
23 #include <media/v4l2-async.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-subdev.h>
27 static bool match_i2c(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
29 #if IS_ENABLED(CONFIG_I2C)
30 struct i2c_client *client = i2c_verify_client(sd->dev);
32 asd->match.i2c.adapter_id == client->adapter->nr &&
33 asd->match.i2c.address == client->addr;
39 static bool match_devname(struct v4l2_subdev *sd,
40 struct v4l2_async_subdev *asd)
42 return !strcmp(asd->match.device_name.name, dev_name(sd->dev));
45 static bool match_fwnode(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
47 return sd->fwnode == asd->match.fwnode.fwnode;
50 static bool match_custom(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
52 if (!asd->match.custom.match)
56 return asd->match.custom.match(sd->dev, asd);
59 static LIST_HEAD(subdev_list);
60 static LIST_HEAD(notifier_list);
61 static DEFINE_MUTEX(list_lock);
63 static struct v4l2_async_subdev *v4l2_async_belongs(struct v4l2_async_notifier *notifier,
64 struct v4l2_subdev *sd)
66 bool (*match)(struct v4l2_subdev *, struct v4l2_async_subdev *);
67 struct v4l2_async_subdev *asd;
69 list_for_each_entry(asd, ¬ifier->waiting, list) {
70 /* bus_type has been verified valid before */
71 switch (asd->match_type) {
72 case V4L2_ASYNC_MATCH_CUSTOM:
75 case V4L2_ASYNC_MATCH_DEVNAME:
76 match = match_devname;
78 case V4L2_ASYNC_MATCH_I2C:
81 case V4L2_ASYNC_MATCH_FWNODE:
85 /* Cannot happen, unless someone breaks us */
90 /* match cannot be NULL here */
98 static int v4l2_async_test_notify(struct v4l2_async_notifier *notifier,
99 struct v4l2_subdev *sd,
100 struct v4l2_async_subdev *asd)
104 if (notifier->bound) {
105 ret = notifier->bound(notifier, sd, asd);
110 ret = v4l2_device_register_subdev(notifier->v4l2_dev, sd);
112 if (notifier->unbind)
113 notifier->unbind(notifier, sd, asd);
117 /* Remove from the waiting list */
118 list_del(&asd->list);
120 sd->notifier = notifier;
122 /* Move from the global subdevice list to notifier's done */
123 list_move(&sd->async_list, ¬ifier->done);
125 if (list_empty(¬ifier->waiting) && notifier->complete)
126 return notifier->complete(notifier);
131 static void v4l2_async_cleanup(struct v4l2_subdev *sd)
133 v4l2_device_unregister_subdev(sd);
134 /* Subdevice driver will reprobe and put the subdev back onto the list */
135 list_del_init(&sd->async_list);
140 int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
141 struct v4l2_async_notifier *notifier)
143 struct v4l2_subdev *sd, *tmp;
144 struct v4l2_async_subdev *asd;
147 if (!v4l2_dev || !notifier->num_subdevs ||
148 notifier->num_subdevs > V4L2_MAX_SUBDEVS)
151 notifier->v4l2_dev = v4l2_dev;
152 INIT_LIST_HEAD(¬ifier->waiting);
153 INIT_LIST_HEAD(¬ifier->done);
155 for (i = 0; i < notifier->num_subdevs; i++) {
156 asd = notifier->subdevs[i];
158 switch (asd->match_type) {
159 case V4L2_ASYNC_MATCH_CUSTOM:
160 case V4L2_ASYNC_MATCH_DEVNAME:
161 case V4L2_ASYNC_MATCH_I2C:
162 case V4L2_ASYNC_MATCH_FWNODE:
165 dev_err(notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL,
166 "Invalid match type %u on %p\n",
167 asd->match_type, asd);
170 list_add_tail(&asd->list, ¬ifier->waiting);
173 mutex_lock(&list_lock);
175 list_for_each_entry_safe(sd, tmp, &subdev_list, async_list) {
178 asd = v4l2_async_belongs(notifier, sd);
182 ret = v4l2_async_test_notify(notifier, sd, asd);
184 mutex_unlock(&list_lock);
189 /* Keep also completed notifiers on the list */
190 list_add(¬ifier->list, ¬ifier_list);
192 mutex_unlock(&list_lock);
196 EXPORT_SYMBOL(v4l2_async_notifier_register);
198 void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
200 struct v4l2_subdev *sd, *tmp;
201 unsigned int notif_n_subdev = notifier->num_subdevs;
202 unsigned int n_subdev = min(notif_n_subdev, V4L2_MAX_SUBDEVS);
206 if (!notifier->v4l2_dev)
209 dev = kvmalloc_array(n_subdev, sizeof(*dev), GFP_KERNEL);
211 dev_err(notifier->v4l2_dev->dev,
212 "Failed to allocate device cache!\n");
215 mutex_lock(&list_lock);
217 list_del(¬ifier->list);
219 list_for_each_entry_safe(sd, tmp, ¬ifier->done, async_list) {
222 d = get_device(sd->dev);
224 v4l2_async_cleanup(sd);
226 /* If we handled USB devices, we'd have to lock the parent too */
227 device_release_driver(d);
229 if (notifier->unbind)
230 notifier->unbind(notifier, sd, sd->asd);
233 * Store device at the device cache, in order to call
234 * put_device() on the final step
242 mutex_unlock(&list_lock);
245 * Call device_attach() to reprobe devices
247 * NOTE: If dev allocation fails, i is 0, and the whole loop won't be
251 struct device *d = dev[i];
253 if (d && device_attach(d) < 0) {
254 const char *name = "(none)";
255 int lock = device_trylock(d);
257 if (lock && d->driver)
258 name = d->driver->name;
259 dev_err(d, "Failed to re-probe to %s\n", name);
267 notifier->v4l2_dev = NULL;
270 * Don't care about the waiting list, it is initialised and populated
271 * upon notifier registration.
274 EXPORT_SYMBOL(v4l2_async_notifier_unregister);
276 int v4l2_async_register_subdev(struct v4l2_subdev *sd)
278 struct v4l2_async_notifier *notifier;
281 * No reference taken. The reference is held by the device
282 * (struct v4l2_subdev.dev), and async sub-device does not
283 * exist independently of the device at any point of time.
285 if (!sd->fwnode && sd->dev)
286 sd->fwnode = dev_fwnode(sd->dev);
288 mutex_lock(&list_lock);
290 INIT_LIST_HEAD(&sd->async_list);
292 list_for_each_entry(notifier, ¬ifier_list, list) {
293 struct v4l2_async_subdev *asd = v4l2_async_belongs(notifier, sd);
295 int ret = v4l2_async_test_notify(notifier, sd, asd);
296 mutex_unlock(&list_lock);
301 /* None matched, wait for hot-plugging */
302 list_add(&sd->async_list, &subdev_list);
304 mutex_unlock(&list_lock);
308 EXPORT_SYMBOL(v4l2_async_register_subdev);
310 void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
312 struct v4l2_async_notifier *notifier = sd->notifier;
315 if (!list_empty(&sd->async_list))
316 v4l2_async_cleanup(sd);
320 mutex_lock(&list_lock);
322 list_add(&sd->asd->list, ¬ifier->waiting);
324 v4l2_async_cleanup(sd);
326 if (notifier->unbind)
327 notifier->unbind(notifier, sd, sd->asd);
329 mutex_unlock(&list_lock);
331 EXPORT_SYMBOL(v4l2_async_unregister_subdev);