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 device *dev, struct v4l2_async_subdev *asd)
27 #if IS_ENABLED(CONFIG_I2C)
28 struct i2c_client *client = i2c_verify_client(dev);
30 asd->bus_type == V4L2_ASYNC_BUS_I2C &&
31 asd->match.i2c.adapter_id == client->adapter->nr &&
32 asd->match.i2c.address == client->addr;
38 static bool match_platform(struct device *dev, struct v4l2_async_subdev *asd)
40 return asd->bus_type == V4L2_ASYNC_BUS_PLATFORM &&
41 !strcmp(asd->match.platform.name, dev_name(dev));
44 static LIST_HEAD(subdev_list);
45 static LIST_HEAD(notifier_list);
46 static DEFINE_MUTEX(list_lock);
48 static struct v4l2_async_subdev *v4l2_async_belongs(struct v4l2_async_notifier *notifier,
49 struct v4l2_async_subdev_list *asdl)
51 struct v4l2_subdev *sd = v4l2_async_to_subdev(asdl);
52 struct v4l2_async_subdev *asd;
53 bool (*match)(struct device *,
54 struct v4l2_async_subdev *);
56 list_for_each_entry(asd, ¬ifier->waiting, list) {
57 /* bus_type has been verified valid before */
58 switch (asd->bus_type) {
59 case V4L2_ASYNC_BUS_CUSTOM:
60 match = asd->match.custom.match;
65 case V4L2_ASYNC_BUS_PLATFORM:
66 match = match_platform;
68 case V4L2_ASYNC_BUS_I2C:
72 /* Cannot happen, unless someone breaks us */
77 /* match cannot be NULL here */
78 if (match(sd->dev, asd))
85 static int v4l2_async_test_notify(struct v4l2_async_notifier *notifier,
86 struct v4l2_async_subdev_list *asdl,
87 struct v4l2_async_subdev *asd)
89 struct v4l2_subdev *sd = v4l2_async_to_subdev(asdl);
92 /* Remove from the waiting list */
95 asdl->notifier = notifier;
97 if (notifier->bound) {
98 ret = notifier->bound(notifier, sd, asd);
102 /* Move from the global subdevice list to notifier's done */
103 list_move(&asdl->list, ¬ifier->done);
105 ret = v4l2_device_register_subdev(notifier->v4l2_dev, sd);
107 if (notifier->unbind)
108 notifier->unbind(notifier, sd, asd);
112 if (list_empty(¬ifier->waiting) && notifier->complete)
113 return notifier->complete(notifier);
118 static void v4l2_async_cleanup(struct v4l2_async_subdev_list *asdl)
120 struct v4l2_subdev *sd = v4l2_async_to_subdev(asdl);
122 v4l2_device_unregister_subdev(sd);
123 /* Subdevice driver will reprobe and put asdl back onto the list */
124 list_del_init(&asdl->list);
129 int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
130 struct v4l2_async_notifier *notifier)
132 struct v4l2_async_subdev_list *asdl, *tmp;
133 struct v4l2_async_subdev *asd;
136 if (!notifier->num_subdevs || notifier->num_subdevs > V4L2_MAX_SUBDEVS)
139 notifier->v4l2_dev = v4l2_dev;
140 INIT_LIST_HEAD(¬ifier->waiting);
141 INIT_LIST_HEAD(¬ifier->done);
143 for (i = 0; i < notifier->num_subdevs; i++) {
144 asd = notifier->subdev[i];
146 switch (asd->bus_type) {
147 case V4L2_ASYNC_BUS_CUSTOM:
148 case V4L2_ASYNC_BUS_PLATFORM:
149 case V4L2_ASYNC_BUS_I2C:
152 dev_err(notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL,
153 "Invalid bus-type %u on %p\n",
157 list_add_tail(&asd->list, ¬ifier->waiting);
160 mutex_lock(&list_lock);
162 /* Keep also completed notifiers on the list */
163 list_add(¬ifier->list, ¬ifier_list);
165 list_for_each_entry_safe(asdl, tmp, &subdev_list, list) {
168 asd = v4l2_async_belongs(notifier, asdl);
172 ret = v4l2_async_test_notify(notifier, asdl, asd);
174 mutex_unlock(&list_lock);
179 mutex_unlock(&list_lock);
183 EXPORT_SYMBOL(v4l2_async_notifier_register);
185 void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
187 struct v4l2_async_subdev_list *asdl, *tmp;
188 unsigned int notif_n_subdev = notifier->num_subdevs;
189 unsigned int n_subdev = min(notif_n_subdev, V4L2_MAX_SUBDEVS);
190 struct device *dev[n_subdev];
193 mutex_lock(&list_lock);
195 list_del(¬ifier->list);
197 list_for_each_entry_safe(asdl, tmp, ¬ifier->done, list) {
198 struct v4l2_subdev *sd = v4l2_async_to_subdev(asdl);
200 dev[i] = get_device(sd->dev);
202 v4l2_async_cleanup(asdl);
204 /* If we handled USB devices, we'd have to lock the parent too */
205 device_release_driver(dev[i++]);
207 if (notifier->unbind)
208 notifier->unbind(notifier, sd, sd->asdl.asd);
211 mutex_unlock(&list_lock);
214 struct device *d = dev[i];
216 if (d && device_attach(d) < 0) {
217 const char *name = "(none)";
218 int lock = device_trylock(d);
220 if (lock && d->driver)
221 name = d->driver->name;
222 dev_err(d, "Failed to re-probe to %s\n", name);
229 * Don't care about the waiting list, it is initialised and populated
230 * upon notifier registration.
233 EXPORT_SYMBOL(v4l2_async_notifier_unregister);
235 int v4l2_async_register_subdev(struct v4l2_subdev *sd)
237 struct v4l2_async_subdev_list *asdl = &sd->asdl;
238 struct v4l2_async_notifier *notifier;
240 mutex_lock(&list_lock);
242 INIT_LIST_HEAD(&asdl->list);
244 list_for_each_entry(notifier, ¬ifier_list, list) {
245 struct v4l2_async_subdev *asd = v4l2_async_belongs(notifier, asdl);
247 int ret = v4l2_async_test_notify(notifier, asdl, asd);
248 mutex_unlock(&list_lock);
253 /* None matched, wait for hot-plugging */
254 list_add(&asdl->list, &subdev_list);
256 mutex_unlock(&list_lock);
260 EXPORT_SYMBOL(v4l2_async_register_subdev);
262 void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
264 struct v4l2_async_subdev_list *asdl = &sd->asdl;
265 struct v4l2_async_notifier *notifier = asdl->notifier;
268 if (!list_empty(&asdl->list))
269 v4l2_async_cleanup(asdl);
273 mutex_lock(&list_lock);
275 list_add(&asdl->asd->list, ¬ifier->waiting);
277 v4l2_async_cleanup(asdl);
279 if (notifier->unbind)
280 notifier->unbind(notifier, sd, sd->asdl.asd);
282 mutex_unlock(&list_lock);
284 EXPORT_SYMBOL(v4l2_async_unregister_subdev);