[media] V4L: Rename v4l2_async_bus_* to v4l2_async_match_*
[deliverable/linux.git] / drivers / media / v4l2-core / v4l2-async.c
CommitLineData
e9e31049
GL
1/*
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>
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>
20
21#include <media/v4l2-async.h>
22#include <media/v4l2-device.h>
23#include <media/v4l2-subdev.h>
24
25static bool match_i2c(struct device *dev, struct v4l2_async_subdev *asd)
26{
fe05e141 27#if IS_ENABLED(CONFIG_I2C)
e9e31049
GL
28 struct i2c_client *client = i2c_verify_client(dev);
29 return client &&
e9e31049
GL
30 asd->match.i2c.adapter_id == client->adapter->nr &&
31 asd->match.i2c.address == client->addr;
fe05e141
GL
32#else
33 return false;
34#endif
e9e31049
GL
35}
36
cfca7644 37static bool match_devname(struct device *dev, struct v4l2_async_subdev *asd)
e9e31049 38{
cfca7644 39 return !strcmp(asd->match.device_name.name, dev_name(dev));
e9e31049
GL
40}
41
42static LIST_HEAD(subdev_list);
43static LIST_HEAD(notifier_list);
44static DEFINE_MUTEX(list_lock);
45
46static struct v4l2_async_subdev *v4l2_async_belongs(struct v4l2_async_notifier *notifier,
47 struct v4l2_async_subdev_list *asdl)
48{
49 struct v4l2_subdev *sd = v4l2_async_to_subdev(asdl);
50 struct v4l2_async_subdev *asd;
51 bool (*match)(struct device *,
52 struct v4l2_async_subdev *);
53
54 list_for_each_entry(asd, &notifier->waiting, list) {
55 /* bus_type has been verified valid before */
cfca7644
SN
56 switch (asd->match_type) {
57 case V4L2_ASYNC_MATCH_CUSTOM:
e9e31049
GL
58 match = asd->match.custom.match;
59 if (!match)
60 /* Match always */
61 return asd;
62 break;
cfca7644
SN
63 case V4L2_ASYNC_MATCH_DEVNAME:
64 match = match_devname;
e9e31049 65 break;
cfca7644 66 case V4L2_ASYNC_MATCH_I2C:
e9e31049
GL
67 match = match_i2c;
68 break;
69 default:
70 /* Cannot happen, unless someone breaks us */
71 WARN_ON(true);
72 return NULL;
73 }
74
75 /* match cannot be NULL here */
76 if (match(sd->dev, asd))
77 return asd;
78 }
79
80 return NULL;
81}
82
83static int v4l2_async_test_notify(struct v4l2_async_notifier *notifier,
84 struct v4l2_async_subdev_list *asdl,
85 struct v4l2_async_subdev *asd)
86{
87 struct v4l2_subdev *sd = v4l2_async_to_subdev(asdl);
88 int ret;
89
90 /* Remove from the waiting list */
91 list_del(&asd->list);
92 asdl->asd = asd;
93 asdl->notifier = notifier;
94
95 if (notifier->bound) {
96 ret = notifier->bound(notifier, sd, asd);
97 if (ret < 0)
98 return ret;
99 }
100 /* Move from the global subdevice list to notifier's done */
101 list_move(&asdl->list, &notifier->done);
102
103 ret = v4l2_device_register_subdev(notifier->v4l2_dev, sd);
104 if (ret < 0) {
105 if (notifier->unbind)
106 notifier->unbind(notifier, sd, asd);
107 return ret;
108 }
109
110 if (list_empty(&notifier->waiting) && notifier->complete)
111 return notifier->complete(notifier);
112
113 return 0;
114}
115
116static void v4l2_async_cleanup(struct v4l2_async_subdev_list *asdl)
117{
118 struct v4l2_subdev *sd = v4l2_async_to_subdev(asdl);
119
120 v4l2_device_unregister_subdev(sd);
121 /* Subdevice driver will reprobe and put asdl back onto the list */
122 list_del_init(&asdl->list);
123 asdl->asd = NULL;
124 sd->dev = NULL;
125}
126
127int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
128 struct v4l2_async_notifier *notifier)
129{
130 struct v4l2_async_subdev_list *asdl, *tmp;
131 struct v4l2_async_subdev *asd;
132 int i;
133
134 if (!notifier->num_subdevs || notifier->num_subdevs > V4L2_MAX_SUBDEVS)
135 return -EINVAL;
136
137 notifier->v4l2_dev = v4l2_dev;
138 INIT_LIST_HEAD(&notifier->waiting);
139 INIT_LIST_HEAD(&notifier->done);
140
141 for (i = 0; i < notifier->num_subdevs; i++) {
142 asd = notifier->subdev[i];
143
cfca7644
SN
144 switch (asd->match_type) {
145 case V4L2_ASYNC_MATCH_CUSTOM:
146 case V4L2_ASYNC_MATCH_DEVNAME:
147 case V4L2_ASYNC_MATCH_I2C:
e9e31049
GL
148 break;
149 default:
150 dev_err(notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL,
cfca7644
SN
151 "Invalid match type %u on %p\n",
152 asd->match_type, asd);
e9e31049
GL
153 return -EINVAL;
154 }
155 list_add_tail(&asd->list, &notifier->waiting);
156 }
157
158 mutex_lock(&list_lock);
159
160 /* Keep also completed notifiers on the list */
161 list_add(&notifier->list, &notifier_list);
162
163 list_for_each_entry_safe(asdl, tmp, &subdev_list, list) {
164 int ret;
165
166 asd = v4l2_async_belongs(notifier, asdl);
167 if (!asd)
168 continue;
169
170 ret = v4l2_async_test_notify(notifier, asdl, asd);
171 if (ret < 0) {
172 mutex_unlock(&list_lock);
173 return ret;
174 }
175 }
176
177 mutex_unlock(&list_lock);
178
179 return 0;
180}
181EXPORT_SYMBOL(v4l2_async_notifier_register);
182
183void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
184{
185 struct v4l2_async_subdev_list *asdl, *tmp;
186 unsigned int notif_n_subdev = notifier->num_subdevs;
187 unsigned int n_subdev = min(notif_n_subdev, V4L2_MAX_SUBDEVS);
188 struct device *dev[n_subdev];
189 int i = 0;
190
191 mutex_lock(&list_lock);
192
193 list_del(&notifier->list);
194
195 list_for_each_entry_safe(asdl, tmp, &notifier->done, list) {
196 struct v4l2_subdev *sd = v4l2_async_to_subdev(asdl);
197
198 dev[i] = get_device(sd->dev);
199
200 v4l2_async_cleanup(asdl);
201
202 /* If we handled USB devices, we'd have to lock the parent too */
203 device_release_driver(dev[i++]);
204
205 if (notifier->unbind)
206 notifier->unbind(notifier, sd, sd->asdl.asd);
207 }
208
209 mutex_unlock(&list_lock);
210
211 while (i--) {
212 struct device *d = dev[i];
213
214 if (d && device_attach(d) < 0) {
215 const char *name = "(none)";
216 int lock = device_trylock(d);
217
218 if (lock && d->driver)
219 name = d->driver->name;
220 dev_err(d, "Failed to re-probe to %s\n", name);
221 if (lock)
222 device_unlock(d);
223 }
224 put_device(d);
225 }
226 /*
227 * Don't care about the waiting list, it is initialised and populated
228 * upon notifier registration.
229 */
230}
231EXPORT_SYMBOL(v4l2_async_notifier_unregister);
232
233int v4l2_async_register_subdev(struct v4l2_subdev *sd)
234{
235 struct v4l2_async_subdev_list *asdl = &sd->asdl;
236 struct v4l2_async_notifier *notifier;
237
238 mutex_lock(&list_lock);
239
240 INIT_LIST_HEAD(&asdl->list);
241
242 list_for_each_entry(notifier, &notifier_list, list) {
243 struct v4l2_async_subdev *asd = v4l2_async_belongs(notifier, asdl);
244 if (asd) {
245 int ret = v4l2_async_test_notify(notifier, asdl, asd);
246 mutex_unlock(&list_lock);
247 return ret;
248 }
249 }
250
251 /* None matched, wait for hot-plugging */
252 list_add(&asdl->list, &subdev_list);
253
254 mutex_unlock(&list_lock);
255
256 return 0;
257}
258EXPORT_SYMBOL(v4l2_async_register_subdev);
259
260void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
261{
262 struct v4l2_async_subdev_list *asdl = &sd->asdl;
263 struct v4l2_async_notifier *notifier = asdl->notifier;
264
265 if (!asdl->asd) {
266 if (!list_empty(&asdl->list))
267 v4l2_async_cleanup(asdl);
268 return;
269 }
270
271 mutex_lock(&list_lock);
272
273 list_add(&asdl->asd->list, &notifier->waiting);
274
275 v4l2_async_cleanup(asdl);
276
277 if (notifier->unbind)
278 notifier->unbind(notifier, sd, sd->asdl.asd);
279
280 mutex_unlock(&list_lock);
281}
282EXPORT_SYMBOL(v4l2_async_unregister_subdev);
This page took 0.038732 seconds and 5 git commands to generate.