blob: 85a6a34128a8ecfcba63c372886bcc00cadf74d7 [file] [log] [blame]
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -03001/*
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{
Guennadi Liakhovetskife05e142013-06-24 05:13:51 -030027#if IS_ENABLED(CONFIG_I2C)
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030028 struct i2c_client *client = i2c_verify_client(dev);
29 return client &&
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030030 asd->match.i2c.adapter_id == client->adapter->nr &&
31 asd->match.i2c.address == client->addr;
Guennadi Liakhovetskife05e142013-06-24 05:13:51 -030032#else
33 return false;
34#endif
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030035}
36
Sylwester Nawrockicfca7642013-07-19 12:14:46 -030037static bool match_devname(struct device *dev, struct v4l2_async_subdev *asd)
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030038{
Sylwester Nawrockicfca7642013-07-19 12:14:46 -030039 return !strcmp(asd->match.device_name.name, dev_name(dev));
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030040}
41
Sylwester Nawrockie7359f82013-07-19 12:21:29 -030042static bool match_of(struct device *dev, struct v4l2_async_subdev *asd)
43{
44 return dev->of_node == asd->match.of.node;
45}
46
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030047static LIST_HEAD(subdev_list);
48static LIST_HEAD(notifier_list);
49static DEFINE_MUTEX(list_lock);
50
51static struct v4l2_async_subdev *v4l2_async_belongs(struct v4l2_async_notifier *notifier,
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -030052 struct v4l2_subdev *sd)
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030053{
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030054 struct v4l2_async_subdev *asd;
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -030055 bool (*match)(struct device *, struct v4l2_async_subdev *);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030056
57 list_for_each_entry(asd, &notifier->waiting, list) {
58 /* bus_type has been verified valid before */
Sylwester Nawrockicfca7642013-07-19 12:14:46 -030059 switch (asd->match_type) {
60 case V4L2_ASYNC_MATCH_CUSTOM:
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030061 match = asd->match.custom.match;
62 if (!match)
63 /* Match always */
64 return asd;
65 break;
Sylwester Nawrockicfca7642013-07-19 12:14:46 -030066 case V4L2_ASYNC_MATCH_DEVNAME:
67 match = match_devname;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030068 break;
Sylwester Nawrockicfca7642013-07-19 12:14:46 -030069 case V4L2_ASYNC_MATCH_I2C:
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030070 match = match_i2c;
71 break;
Sylwester Nawrockie7359f82013-07-19 12:21:29 -030072 case V4L2_ASYNC_MATCH_OF:
73 match = match_of;
74 break;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030075 default:
76 /* Cannot happen, unless someone breaks us */
77 WARN_ON(true);
78 return NULL;
79 }
80
81 /* match cannot be NULL here */
82 if (match(sd->dev, asd))
83 return asd;
84 }
85
86 return NULL;
87}
88
89static int v4l2_async_test_notify(struct v4l2_async_notifier *notifier,
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -030090 struct v4l2_subdev *sd,
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030091 struct v4l2_async_subdev *asd)
92{
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030093 int ret;
94
95 /* Remove from the waiting list */
96 list_del(&asd->list);
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -030097 sd->asd = asd;
98 sd->notifier = notifier;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030099
100 if (notifier->bound) {
101 ret = notifier->bound(notifier, sd, asd);
102 if (ret < 0)
103 return ret;
104 }
105 /* Move from the global subdevice list to notifier's done */
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300106 list_move(&sd->async_list, &notifier->done);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300107
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;
113 }
114
115 if (list_empty(&notifier->waiting) && notifier->complete)
116 return notifier->complete(notifier);
117
118 return 0;
119}
120
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300121static void v4l2_async_cleanup(struct v4l2_subdev *sd)
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300122{
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300123 v4l2_device_unregister_subdev(sd);
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300124 /* Subdevice driver will reprobe and put the subdev back onto the list */
125 list_del_init(&sd->async_list);
126 sd->asd = NULL;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300127 sd->dev = NULL;
128}
129
130int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
131 struct v4l2_async_notifier *notifier)
132{
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300133 struct v4l2_subdev *sd, *tmp;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300134 struct v4l2_async_subdev *asd;
135 int i;
136
137 if (!notifier->num_subdevs || notifier->num_subdevs > V4L2_MAX_SUBDEVS)
138 return -EINVAL;
139
140 notifier->v4l2_dev = v4l2_dev;
141 INIT_LIST_HEAD(&notifier->waiting);
142 INIT_LIST_HEAD(&notifier->done);
143
144 for (i = 0; i < notifier->num_subdevs; i++) {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -0300145 asd = notifier->subdevs[i];
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300146
Sylwester Nawrockicfca7642013-07-19 12:14:46 -0300147 switch (asd->match_type) {
148 case V4L2_ASYNC_MATCH_CUSTOM:
149 case V4L2_ASYNC_MATCH_DEVNAME:
150 case V4L2_ASYNC_MATCH_I2C:
Sylwester Nawrockie7359f82013-07-19 12:21:29 -0300151 case V4L2_ASYNC_MATCH_OF:
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300152 break;
153 default:
154 dev_err(notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL,
Sylwester Nawrockicfca7642013-07-19 12:14:46 -0300155 "Invalid match type %u on %p\n",
156 asd->match_type, asd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300157 return -EINVAL;
158 }
159 list_add_tail(&asd->list, &notifier->waiting);
160 }
161
162 mutex_lock(&list_lock);
163
164 /* Keep also completed notifiers on the list */
165 list_add(&notifier->list, &notifier_list);
166
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300167 list_for_each_entry_safe(sd, tmp, &subdev_list, async_list) {
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300168 int ret;
169
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300170 asd = v4l2_async_belongs(notifier, sd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300171 if (!asd)
172 continue;
173
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300174 ret = v4l2_async_test_notify(notifier, sd, asd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300175 if (ret < 0) {
176 mutex_unlock(&list_lock);
177 return ret;
178 }
179 }
180
181 mutex_unlock(&list_lock);
182
183 return 0;
184}
185EXPORT_SYMBOL(v4l2_async_notifier_register);
186
187void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
188{
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300189 struct v4l2_subdev *sd, *tmp;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300190 unsigned int notif_n_subdev = notifier->num_subdevs;
191 unsigned int n_subdev = min(notif_n_subdev, V4L2_MAX_SUBDEVS);
Mauro Carvalho Chehab24e9a472013-11-02 06:20:16 -0300192 struct device **dev;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300193 int i = 0;
194
Laurent Pinchart8e3fbfe2013-07-03 07:49:06 -0300195 if (!notifier->v4l2_dev)
196 return;
197
Mauro Carvalho Chehab24e9a472013-11-02 06:20:16 -0300198 dev = kmalloc(n_subdev * sizeof(*dev), GFP_KERNEL);
199 if (!dev) {
200 dev_err(notifier->v4l2_dev->dev,
201 "Failed to allocate device cache!\n");
202 }
203
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300204 mutex_lock(&list_lock);
205
206 list_del(&notifier->list);
207
Sylwester Nawrockiceedcc42013-07-31 13:10:18 -0300208 list_for_each_entry_safe(sd, tmp, &notifier->done, async_list) {
Mauro Carvalho Chehab24e9a472013-11-02 06:20:16 -0300209 struct device *d;
210
211 d = get_device(sd->dev);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300212
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300213 v4l2_async_cleanup(sd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300214
215 /* If we handled USB devices, we'd have to lock the parent too */
Mauro Carvalho Chehab24e9a472013-11-02 06:20:16 -0300216 device_release_driver(d);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300217
218 if (notifier->unbind)
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300219 notifier->unbind(notifier, sd, sd->asd);
Mauro Carvalho Chehab24e9a472013-11-02 06:20:16 -0300220
221 /*
222 * Store device at the device cache, in order to call
223 * put_device() on the final step
224 */
225 if (dev)
226 dev[i++] = d;
227 else
228 put_device(d);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300229 }
230
231 mutex_unlock(&list_lock);
232
Mauro Carvalho Chehab24e9a472013-11-02 06:20:16 -0300233 /*
234 * Call device_attach() to reprobe devices
235 *
236 * NOTE: If dev allocation fails, i is 0, and the whole loop won't be
237 * executed.
238 */
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300239 while (i--) {
240 struct device *d = dev[i];
241
242 if (d && device_attach(d) < 0) {
243 const char *name = "(none)";
244 int lock = device_trylock(d);
245
246 if (lock && d->driver)
247 name = d->driver->name;
248 dev_err(d, "Failed to re-probe to %s\n", name);
249 if (lock)
250 device_unlock(d);
251 }
252 put_device(d);
253 }
Mauro Carvalho Chehab24e9a472013-11-02 06:20:16 -0300254 kfree(dev);
Laurent Pinchart8e3fbfe2013-07-03 07:49:06 -0300255
256 notifier->v4l2_dev = NULL;
257
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300258 /*
259 * Don't care about the waiting list, it is initialised and populated
260 * upon notifier registration.
261 */
262}
263EXPORT_SYMBOL(v4l2_async_notifier_unregister);
264
265int v4l2_async_register_subdev(struct v4l2_subdev *sd)
266{
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300267 struct v4l2_async_notifier *notifier;
268
269 mutex_lock(&list_lock);
270
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300271 INIT_LIST_HEAD(&sd->async_list);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300272
273 list_for_each_entry(notifier, &notifier_list, list) {
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300274 struct v4l2_async_subdev *asd = v4l2_async_belongs(notifier, sd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300275 if (asd) {
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300276 int ret = v4l2_async_test_notify(notifier, sd, asd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300277 mutex_unlock(&list_lock);
278 return ret;
279 }
280 }
281
282 /* None matched, wait for hot-plugging */
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300283 list_add(&sd->async_list, &subdev_list);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300284
285 mutex_unlock(&list_lock);
286
287 return 0;
288}
289EXPORT_SYMBOL(v4l2_async_register_subdev);
290
291void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
292{
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300293 struct v4l2_async_notifier *notifier = sd->notifier;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300294
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300295 if (!sd->asd) {
296 if (!list_empty(&sd->async_list))
297 v4l2_async_cleanup(sd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300298 return;
299 }
300
301 mutex_lock(&list_lock);
302
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300303 list_add(&sd->asd->list, &notifier->waiting);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300304
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300305 v4l2_async_cleanup(sd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300306
307 if (notifier->unbind)
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300308 notifier->unbind(notifier, sd, sd->asd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300309
310 mutex_unlock(&list_lock);
311}
312EXPORT_SYMBOL(v4l2_async_unregister_subdev);