Merge remote-tracking branch 'asoc/topic/tlv320aic23' into asoc-next
[linux-2.6/btrfs-unstable.git] / drivers / media / v4l2-core / v4l2-async.c
blobc85d69da35bdee1b2c0f82ac9deb395e8450b786
1 /*
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.
9 */
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);
29 return client &&
30 asd->match.i2c.adapter_id == client->adapter->nr &&
31 asd->match.i2c.address == client->addr;
32 #else
33 return false;
34 #endif
37 static bool match_devname(struct device *dev, struct v4l2_async_subdev *asd)
39 return !strcmp(asd->match.device_name.name, dev_name(dev));
42 static bool match_of(struct device *dev, struct v4l2_async_subdev *asd)
44 return dev->of_node == asd->match.of.node;
47 static LIST_HEAD(subdev_list);
48 static LIST_HEAD(notifier_list);
49 static DEFINE_MUTEX(list_lock);
51 static struct v4l2_async_subdev *v4l2_async_belongs(struct v4l2_async_notifier *notifier,
52 struct v4l2_subdev *sd)
54 struct v4l2_async_subdev *asd;
55 bool (*match)(struct device *, struct v4l2_async_subdev *);
57 list_for_each_entry(asd, &notifier->waiting, list) {
58 /* bus_type has been verified valid before */
59 switch (asd->match_type) {
60 case V4L2_ASYNC_MATCH_CUSTOM:
61 match = asd->match.custom.match;
62 if (!match)
63 /* Match always */
64 return asd;
65 break;
66 case V4L2_ASYNC_MATCH_DEVNAME:
67 match = match_devname;
68 break;
69 case V4L2_ASYNC_MATCH_I2C:
70 match = match_i2c;
71 break;
72 case V4L2_ASYNC_MATCH_OF:
73 match = match_of;
74 break;
75 default:
76 /* Cannot happen, unless someone breaks us */
77 WARN_ON(true);
78 return NULL;
81 /* match cannot be NULL here */
82 if (match(sd->dev, asd))
83 return asd;
86 return NULL;
89 static int v4l2_async_test_notify(struct v4l2_async_notifier *notifier,
90 struct v4l2_subdev *sd,
91 struct v4l2_async_subdev *asd)
93 int ret;
95 /* Remove from the waiting list */
96 list_del(&asd->list);
97 sd->asd = asd;
98 sd->notifier = notifier;
100 if (notifier->bound) {
101 ret = notifier->bound(notifier, sd, asd);
102 if (ret < 0)
103 return ret;
105 /* Move from the global subdevice list to notifier's done */
106 list_move(&sd->async_list, &notifier->done);
108 ret = v4l2_device_register_subdev(notifier->v4l2_dev, sd);
109 if (ret < 0) {
110 if (notifier->unbind)
111 notifier->unbind(notifier, sd, asd);
112 return ret;
115 if (list_empty(&notifier->waiting) && notifier->complete)
116 return notifier->complete(notifier);
118 return 0;
121 static void v4l2_async_cleanup(struct v4l2_subdev *sd)
123 v4l2_device_unregister_subdev(sd);
124 /* Subdevice driver will reprobe and put the subdev back onto the list */
125 list_del_init(&sd->async_list);
126 sd->asd = NULL;
127 sd->dev = NULL;
130 int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
131 struct v4l2_async_notifier *notifier)
133 struct v4l2_subdev *sd, *tmp;
134 struct v4l2_async_subdev *asd;
135 int i;
137 if (!notifier->num_subdevs || notifier->num_subdevs > V4L2_MAX_SUBDEVS)
138 return -EINVAL;
140 notifier->v4l2_dev = v4l2_dev;
141 INIT_LIST_HEAD(&notifier->waiting);
142 INIT_LIST_HEAD(&notifier->done);
144 for (i = 0; i < notifier->num_subdevs; i++) {
145 asd = notifier->subdevs[i];
147 switch (asd->match_type) {
148 case V4L2_ASYNC_MATCH_CUSTOM:
149 case V4L2_ASYNC_MATCH_DEVNAME:
150 case V4L2_ASYNC_MATCH_I2C:
151 case V4L2_ASYNC_MATCH_OF:
152 break;
153 default:
154 dev_err(notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL,
155 "Invalid match type %u on %p\n",
156 asd->match_type, asd);
157 return -EINVAL;
159 list_add_tail(&asd->list, &notifier->waiting);
162 mutex_lock(&list_lock);
164 /* Keep also completed notifiers on the list */
165 list_add(&notifier->list, &notifier_list);
167 list_for_each_entry_safe(sd, tmp, &subdev_list, async_list) {
168 int ret;
170 asd = v4l2_async_belongs(notifier, sd);
171 if (!asd)
172 continue;
174 ret = v4l2_async_test_notify(notifier, sd, asd);
175 if (ret < 0) {
176 mutex_unlock(&list_lock);
177 return ret;
181 mutex_unlock(&list_lock);
183 return 0;
185 EXPORT_SYMBOL(v4l2_async_notifier_register);
187 void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
189 struct v4l2_subdev *sd, *tmp;
190 unsigned int notif_n_subdev = notifier->num_subdevs;
191 unsigned int n_subdev = min(notif_n_subdev, V4L2_MAX_SUBDEVS);
192 struct device *dev[n_subdev];
193 int i = 0;
195 if (!notifier->v4l2_dev)
196 return;
198 mutex_lock(&list_lock);
200 list_del(&notifier->list);
202 list_for_each_entry_safe(sd, tmp, &notifier->done, async_list) {
203 dev[i] = get_device(sd->dev);
205 v4l2_async_cleanup(sd);
207 /* If we handled USB devices, we'd have to lock the parent too */
208 device_release_driver(dev[i++]);
210 if (notifier->unbind)
211 notifier->unbind(notifier, sd, sd->asd);
214 mutex_unlock(&list_lock);
216 while (i--) {
217 struct device *d = dev[i];
219 if (d && device_attach(d) < 0) {
220 const char *name = "(none)";
221 int lock = device_trylock(d);
223 if (lock && d->driver)
224 name = d->driver->name;
225 dev_err(d, "Failed to re-probe to %s\n", name);
226 if (lock)
227 device_unlock(d);
229 put_device(d);
232 notifier->v4l2_dev = NULL;
235 * Don't care about the waiting list, it is initialised and populated
236 * upon notifier registration.
239 EXPORT_SYMBOL(v4l2_async_notifier_unregister);
241 int v4l2_async_register_subdev(struct v4l2_subdev *sd)
243 struct v4l2_async_notifier *notifier;
245 mutex_lock(&list_lock);
247 INIT_LIST_HEAD(&sd->async_list);
249 list_for_each_entry(notifier, &notifier_list, list) {
250 struct v4l2_async_subdev *asd = v4l2_async_belongs(notifier, sd);
251 if (asd) {
252 int ret = v4l2_async_test_notify(notifier, sd, asd);
253 mutex_unlock(&list_lock);
254 return ret;
258 /* None matched, wait for hot-plugging */
259 list_add(&sd->async_list, &subdev_list);
261 mutex_unlock(&list_lock);
263 return 0;
265 EXPORT_SYMBOL(v4l2_async_register_subdev);
267 void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
269 struct v4l2_async_notifier *notifier = sd->notifier;
271 if (!sd->asd) {
272 if (!list_empty(&sd->async_list))
273 v4l2_async_cleanup(sd);
274 return;
277 mutex_lock(&list_lock);
279 list_add(&sd->asd->list, &notifier->waiting);
281 v4l2_async_cleanup(sd);
283 if (notifier->unbind)
284 notifier->unbind(notifier, sd, sd->asd);
286 mutex_unlock(&list_lock);
288 EXPORT_SYMBOL(v4l2_async_unregister_subdev);