]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/v4l2-core/v4l2-async.c
ipv4: convert dst_metrics.refcnt from atomic_t to refcount_t
[mirror_ubuntu-artful-kernel.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>
758d90e1 15#include <linux/mm.h>
e9e31049
GL
16#include <linux/module.h>
17#include <linux/mutex.h>
ecdf0cfe 18#include <linux/of.h>
e9e31049
GL
19#include <linux/platform_device.h>
20#include <linux/slab.h>
21#include <linux/types.h>
22
23#include <media/v4l2-async.h>
24#include <media/v4l2-device.h>
25#include <media/v4l2-subdev.h>
26
86217651 27static bool match_i2c(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
e9e31049 28{
fe05e141 29#if IS_ENABLED(CONFIG_I2C)
86217651 30 struct i2c_client *client = i2c_verify_client(sd->dev);
e9e31049 31 return client &&
e9e31049
GL
32 asd->match.i2c.adapter_id == client->adapter->nr &&
33 asd->match.i2c.address == client->addr;
fe05e141
GL
34#else
35 return false;
36#endif
e9e31049
GL
37}
38
86217651
SA
39static bool match_devname(struct v4l2_subdev *sd,
40 struct v4l2_async_subdev *asd)
e9e31049 41{
86217651 42 return !strcmp(asd->match.device_name.name, dev_name(sd->dev));
e9e31049
GL
43}
44
ecdf0cfe
SA
45static bool match_fwnode(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
46{
47 if (!is_of_node(sd->fwnode) || !is_of_node(asd->match.fwnode.fwnode))
48 return sd->fwnode == asd->match.fwnode.fwnode;
49
50 return !of_node_cmp(of_node_full_name(to_of_node(sd->fwnode)),
51 of_node_full_name(
52 to_of_node(asd->match.fwnode.fwnode)));
53}
54
86217651
SA
55static bool match_custom(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
56{
57 if (!asd->match.custom.match)
58 /* Match always */
59 return true;
60
61 return asd->match.custom.match(sd->dev, asd);
e7359f8e
SN
62}
63
e9e31049
GL
64static LIST_HEAD(subdev_list);
65static LIST_HEAD(notifier_list);
66static DEFINE_MUTEX(list_lock);
67
68static struct v4l2_async_subdev *v4l2_async_belongs(struct v4l2_async_notifier *notifier,
b426b3a6 69 struct v4l2_subdev *sd)
e9e31049 70{
86217651 71 bool (*match)(struct v4l2_subdev *, struct v4l2_async_subdev *);
e9e31049 72 struct v4l2_async_subdev *asd;
e9e31049
GL
73
74 list_for_each_entry(asd, &notifier->waiting, list) {
75 /* bus_type has been verified valid before */
cfca7644
SN
76 switch (asd->match_type) {
77 case V4L2_ASYNC_MATCH_CUSTOM:
86217651 78 match = match_custom;
e9e31049 79 break;
cfca7644
SN
80 case V4L2_ASYNC_MATCH_DEVNAME:
81 match = match_devname;
e9e31049 82 break;
cfca7644 83 case V4L2_ASYNC_MATCH_I2C:
e9e31049
GL
84 match = match_i2c;
85 break;
ecdf0cfe
SA
86 case V4L2_ASYNC_MATCH_FWNODE:
87 match = match_fwnode;
88 break;
e9e31049
GL
89 default:
90 /* Cannot happen, unless someone breaks us */
91 WARN_ON(true);
92 return NULL;
93 }
94
95 /* match cannot be NULL here */
86217651 96 if (match(sd, asd))
e9e31049
GL
97 return asd;
98 }
99
100 return NULL;
101}
102
103static int v4l2_async_test_notify(struct v4l2_async_notifier *notifier,
b426b3a6 104 struct v4l2_subdev *sd,
e9e31049
GL
105 struct v4l2_async_subdev *asd)
106{
e9e31049
GL
107 int ret;
108
e9e31049
GL
109 if (notifier->bound) {
110 ret = notifier->bound(notifier, sd, asd);
111 if (ret < 0)
112 return ret;
113 }
e9e31049
GL
114
115 ret = v4l2_device_register_subdev(notifier->v4l2_dev, sd);
116 if (ret < 0) {
117 if (notifier->unbind)
118 notifier->unbind(notifier, sd, asd);
119 return ret;
120 }
121
47b037a0
TT
122 /* Remove from the waiting list */
123 list_del(&asd->list);
124 sd->asd = asd;
125 sd->notifier = notifier;
126
127 /* Move from the global subdevice list to notifier's done */
128 list_move(&sd->async_list, &notifier->done);
129
e9e31049
GL
130 if (list_empty(&notifier->waiting) && notifier->complete)
131 return notifier->complete(notifier);
132
133 return 0;
134}
135
b426b3a6 136static void v4l2_async_cleanup(struct v4l2_subdev *sd)
e9e31049 137{
e9e31049 138 v4l2_device_unregister_subdev(sd);
b426b3a6
SN
139 /* Subdevice driver will reprobe and put the subdev back onto the list */
140 list_del_init(&sd->async_list);
141 sd->asd = NULL;
e9e31049
GL
142 sd->dev = NULL;
143}
144
145int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
146 struct v4l2_async_notifier *notifier)
147{
b426b3a6 148 struct v4l2_subdev *sd, *tmp;
e9e31049
GL
149 struct v4l2_async_subdev *asd;
150 int i;
151
fbf1e94f
NS
152 if (!v4l2_dev || !notifier->num_subdevs ||
153 notifier->num_subdevs > V4L2_MAX_SUBDEVS)
e9e31049
GL
154 return -EINVAL;
155
156 notifier->v4l2_dev = v4l2_dev;
157 INIT_LIST_HEAD(&notifier->waiting);
158 INIT_LIST_HEAD(&notifier->done);
159
160 for (i = 0; i < notifier->num_subdevs; i++) {
e8419d08 161 asd = notifier->subdevs[i];
e9e31049 162
cfca7644
SN
163 switch (asd->match_type) {
164 case V4L2_ASYNC_MATCH_CUSTOM:
165 case V4L2_ASYNC_MATCH_DEVNAME:
166 case V4L2_ASYNC_MATCH_I2C:
ecdf0cfe 167 case V4L2_ASYNC_MATCH_FWNODE:
e9e31049
GL
168 break;
169 default:
170 dev_err(notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL,
cfca7644
SN
171 "Invalid match type %u on %p\n",
172 asd->match_type, asd);
e9e31049
GL
173 return -EINVAL;
174 }
175 list_add_tail(&asd->list, &notifier->waiting);
176 }
177
178 mutex_lock(&list_lock);
179
b426b3a6 180 list_for_each_entry_safe(sd, tmp, &subdev_list, async_list) {
e9e31049
GL
181 int ret;
182
b426b3a6 183 asd = v4l2_async_belongs(notifier, sd);
e9e31049
GL
184 if (!asd)
185 continue;
186
b426b3a6 187 ret = v4l2_async_test_notify(notifier, sd, asd);
e9e31049
GL
188 if (ret < 0) {
189 mutex_unlock(&list_lock);
190 return ret;
191 }
192 }
193
47b037a0
TT
194 /* Keep also completed notifiers on the list */
195 list_add(&notifier->list, &notifier_list);
196
e9e31049
GL
197 mutex_unlock(&list_lock);
198
199 return 0;
200}
201EXPORT_SYMBOL(v4l2_async_notifier_register);
202
203void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
204{
b426b3a6 205 struct v4l2_subdev *sd, *tmp;
e9e31049
GL
206 unsigned int notif_n_subdev = notifier->num_subdevs;
207 unsigned int n_subdev = min(notif_n_subdev, V4L2_MAX_SUBDEVS);
24e9a47e 208 struct device **dev;
e9e31049
GL
209 int i = 0;
210
8e3fbfee
LP
211 if (!notifier->v4l2_dev)
212 return;
213
758d90e1 214 dev = kvmalloc_array(n_subdev, sizeof(*dev), GFP_KERNEL);
24e9a47e
MCC
215 if (!dev) {
216 dev_err(notifier->v4l2_dev->dev,
217 "Failed to allocate device cache!\n");
218 }
219
e9e31049
GL
220 mutex_lock(&list_lock);
221
222 list_del(&notifier->list);
223
ceedcc4e 224 list_for_each_entry_safe(sd, tmp, &notifier->done, async_list) {
24e9a47e
MCC
225 struct device *d;
226
227 d = get_device(sd->dev);
e9e31049 228
b426b3a6 229 v4l2_async_cleanup(sd);
e9e31049
GL
230
231 /* If we handled USB devices, we'd have to lock the parent too */
24e9a47e 232 device_release_driver(d);
e9e31049
GL
233
234 if (notifier->unbind)
b426b3a6 235 notifier->unbind(notifier, sd, sd->asd);
24e9a47e
MCC
236
237 /*
238 * Store device at the device cache, in order to call
239 * put_device() on the final step
240 */
241 if (dev)
242 dev[i++] = d;
243 else
244 put_device(d);
e9e31049
GL
245 }
246
247 mutex_unlock(&list_lock);
248
24e9a47e
MCC
249 /*
250 * Call device_attach() to reprobe devices
251 *
252 * NOTE: If dev allocation fails, i is 0, and the whole loop won't be
253 * executed.
254 */
e9e31049
GL
255 while (i--) {
256 struct device *d = dev[i];
257
258 if (d && device_attach(d) < 0) {
259 const char *name = "(none)";
260 int lock = device_trylock(d);
261
262 if (lock && d->driver)
263 name = d->driver->name;
264 dev_err(d, "Failed to re-probe to %s\n", name);
265 if (lock)
266 device_unlock(d);
267 }
268 put_device(d);
269 }
758d90e1 270 kvfree(dev);
8e3fbfee
LP
271
272 notifier->v4l2_dev = NULL;
273
e9e31049
GL
274 /*
275 * Don't care about the waiting list, it is initialised and populated
276 * upon notifier registration.
277 */
278}
279EXPORT_SYMBOL(v4l2_async_notifier_unregister);
280
281int v4l2_async_register_subdev(struct v4l2_subdev *sd)
282{
e9e31049
GL
283 struct v4l2_async_notifier *notifier;
284
86217651
SA
285 /*
286 * No reference taken. The reference is held by the device
287 * (struct v4l2_subdev.dev), and async sub-device does not
288 * exist independently of the device at any point of time.
289 */
859969b3
SA
290 if (!sd->fwnode && sd->dev)
291 sd->fwnode = dev_fwnode(sd->dev);
86217651 292
e9e31049
GL
293 mutex_lock(&list_lock);
294
b426b3a6 295 INIT_LIST_HEAD(&sd->async_list);
e9e31049
GL
296
297 list_for_each_entry(notifier, &notifier_list, list) {
b426b3a6 298 struct v4l2_async_subdev *asd = v4l2_async_belongs(notifier, sd);
e9e31049 299 if (asd) {
b426b3a6 300 int ret = v4l2_async_test_notify(notifier, sd, asd);
e9e31049
GL
301 mutex_unlock(&list_lock);
302 return ret;
303 }
304 }
305
306 /* None matched, wait for hot-plugging */
b426b3a6 307 list_add(&sd->async_list, &subdev_list);
e9e31049
GL
308
309 mutex_unlock(&list_lock);
310
311 return 0;
312}
313EXPORT_SYMBOL(v4l2_async_register_subdev);
314
315void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
316{
b426b3a6 317 struct v4l2_async_notifier *notifier = sd->notifier;
e9e31049 318
b426b3a6
SN
319 if (!sd->asd) {
320 if (!list_empty(&sd->async_list))
321 v4l2_async_cleanup(sd);
e9e31049
GL
322 return;
323 }
324
325 mutex_lock(&list_lock);
326
b426b3a6 327 list_add(&sd->asd->list, &notifier->waiting);
e9e31049 328
b426b3a6 329 v4l2_async_cleanup(sd);
e9e31049
GL
330
331 if (notifier->unbind)
b426b3a6 332 notifier->unbind(notifier, sd, sd->asd);
e9e31049
GL
333
334 mutex_unlock(&list_lock);
335}
336EXPORT_SYMBOL(v4l2_async_unregister_subdev);