blob: 61813508954891baa08383f27927482ab01a6e18 [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
Sakari Ailus86217652015-06-11 12:18:01 -070025static bool match_i2c(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030026{
Guennadi Liakhovetskife05e142013-06-24 05:13:51 -030027#if IS_ENABLED(CONFIG_I2C)
Sakari Ailus86217652015-06-11 12:18:01 -070028 struct i2c_client *client = i2c_verify_client(sd->dev);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030029 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
Sakari Ailus86217652015-06-11 12:18:01 -070037static bool match_devname(struct v4l2_subdev *sd,
38 struct v4l2_async_subdev *asd)
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030039{
Sakari Ailus86217652015-06-11 12:18:01 -070040 return !strcmp(asd->match.device_name.name, dev_name(sd->dev));
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030041}
42
Sakari Ailus86217652015-06-11 12:18:01 -070043static bool match_of(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
Sylwester Nawrockie7359f82013-07-19 12:21:29 -030044{
Javi Merinod2180e02016-12-05 08:09:56 -020045 return !of_node_cmp(of_node_full_name(sd->of_node),
46 of_node_full_name(asd->match.of.node));
Sakari Ailus86217652015-06-11 12:18:01 -070047}
48
49static bool match_custom(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
50{
51 if (!asd->match.custom.match)
52 /* Match always */
53 return true;
54
55 return asd->match.custom.match(sd->dev, asd);
Sylwester Nawrockie7359f82013-07-19 12:21:29 -030056}
57
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030058static LIST_HEAD(subdev_list);
59static LIST_HEAD(notifier_list);
60static DEFINE_MUTEX(list_lock);
61
62static struct v4l2_async_subdev *v4l2_async_belongs(struct v4l2_async_notifier *notifier,
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -030063 struct v4l2_subdev *sd)
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030064{
Sakari Ailus86217652015-06-11 12:18:01 -070065 bool (*match)(struct v4l2_subdev *, struct v4l2_async_subdev *);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030066 struct v4l2_async_subdev *asd;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030067
68 list_for_each_entry(asd, &notifier->waiting, list) {
69 /* bus_type has been verified valid before */
Sylwester Nawrockicfca7642013-07-19 12:14:46 -030070 switch (asd->match_type) {
71 case V4L2_ASYNC_MATCH_CUSTOM:
Sakari Ailus86217652015-06-11 12:18:01 -070072 match = match_custom;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030073 break;
Sylwester Nawrockicfca7642013-07-19 12:14:46 -030074 case V4L2_ASYNC_MATCH_DEVNAME:
75 match = match_devname;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030076 break;
Sylwester Nawrockicfca7642013-07-19 12:14:46 -030077 case V4L2_ASYNC_MATCH_I2C:
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030078 match = match_i2c;
79 break;
Sylwester Nawrockie7359f82013-07-19 12:21:29 -030080 case V4L2_ASYNC_MATCH_OF:
81 match = match_of;
82 break;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030083 default:
84 /* Cannot happen, unless someone breaks us */
85 WARN_ON(true);
86 return NULL;
87 }
88
89 /* match cannot be NULL here */
Sakari Ailus86217652015-06-11 12:18:01 -070090 if (match(sd, asd))
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030091 return asd;
92 }
93
94 return NULL;
95}
96
97static int v4l2_async_test_notify(struct v4l2_async_notifier *notifier,
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -030098 struct v4l2_subdev *sd,
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030099 struct v4l2_async_subdev *asd)
100{
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300101 int ret;
102
103 /* Remove from the waiting list */
104 list_del(&asd->list);
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300105 sd->asd = asd;
106 sd->notifier = notifier;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300107
108 if (notifier->bound) {
109 ret = notifier->bound(notifier, sd, asd);
110 if (ret < 0)
111 return ret;
112 }
113 /* Move from the global subdevice list to notifier's done */
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300114 list_move(&sd->async_list, &notifier->done);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300115
116 ret = v4l2_device_register_subdev(notifier->v4l2_dev, sd);
117 if (ret < 0) {
118 if (notifier->unbind)
119 notifier->unbind(notifier, sd, asd);
120 return ret;
121 }
122
123 if (list_empty(&notifier->waiting) && notifier->complete)
124 return notifier->complete(notifier);
125
126 return 0;
127}
128
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300129static void v4l2_async_cleanup(struct v4l2_subdev *sd)
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300130{
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300131 v4l2_device_unregister_subdev(sd);
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300132 /* Subdevice driver will reprobe and put the subdev back onto the list */
133 list_del_init(&sd->async_list);
134 sd->asd = NULL;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300135 sd->dev = NULL;
136}
137
138int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
139 struct v4l2_async_notifier *notifier)
140{
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300141 struct v4l2_subdev *sd, *tmp;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300142 struct v4l2_async_subdev *asd;
143 int i;
144
145 if (!notifier->num_subdevs || notifier->num_subdevs > V4L2_MAX_SUBDEVS)
146 return -EINVAL;
147
148 notifier->v4l2_dev = v4l2_dev;
149 INIT_LIST_HEAD(&notifier->waiting);
150 INIT_LIST_HEAD(&notifier->done);
151
152 for (i = 0; i < notifier->num_subdevs; i++) {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -0300153 asd = notifier->subdevs[i];
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300154
Sylwester Nawrockicfca7642013-07-19 12:14:46 -0300155 switch (asd->match_type) {
156 case V4L2_ASYNC_MATCH_CUSTOM:
157 case V4L2_ASYNC_MATCH_DEVNAME:
158 case V4L2_ASYNC_MATCH_I2C:
Sylwester Nawrockie7359f82013-07-19 12:21:29 -0300159 case V4L2_ASYNC_MATCH_OF:
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300160 break;
161 default:
162 dev_err(notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL,
Sylwester Nawrockicfca7642013-07-19 12:14:46 -0300163 "Invalid match type %u on %p\n",
164 asd->match_type, asd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300165 return -EINVAL;
166 }
167 list_add_tail(&asd->list, &notifier->waiting);
168 }
169
170 mutex_lock(&list_lock);
171
172 /* Keep also completed notifiers on the list */
173 list_add(&notifier->list, &notifier_list);
174
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300175 list_for_each_entry_safe(sd, tmp, &subdev_list, async_list) {
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300176 int ret;
177
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300178 asd = v4l2_async_belongs(notifier, sd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300179 if (!asd)
180 continue;
181
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300182 ret = v4l2_async_test_notify(notifier, sd, asd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300183 if (ret < 0) {
184 mutex_unlock(&list_lock);
185 return ret;
186 }
187 }
188
189 mutex_unlock(&list_lock);
190
191 return 0;
192}
193EXPORT_SYMBOL(v4l2_async_notifier_register);
194
195void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
196{
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300197 struct v4l2_subdev *sd, *tmp;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300198 unsigned int notif_n_subdev = notifier->num_subdevs;
199 unsigned int n_subdev = min(notif_n_subdev, V4L2_MAX_SUBDEVS);
Mauro Carvalho Chehab24e9a472013-11-02 06:20:16 -0300200 struct device **dev;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300201 int i = 0;
202
Laurent Pinchart8e3fbfe2013-07-03 07:49:06 -0300203 if (!notifier->v4l2_dev)
204 return;
205
Markus Elfringf9e9c062016-12-26 15:14:33 -0200206 dev = kmalloc_array(n_subdev, sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab24e9a472013-11-02 06:20:16 -0300207 if (!dev) {
208 dev_err(notifier->v4l2_dev->dev,
209 "Failed to allocate device cache!\n");
210 }
211
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300212 mutex_lock(&list_lock);
213
214 list_del(&notifier->list);
215
Sylwester Nawrockiceedcc42013-07-31 13:10:18 -0300216 list_for_each_entry_safe(sd, tmp, &notifier->done, async_list) {
Mauro Carvalho Chehab24e9a472013-11-02 06:20:16 -0300217 struct device *d;
218
219 d = get_device(sd->dev);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300220
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300221 v4l2_async_cleanup(sd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300222
223 /* If we handled USB devices, we'd have to lock the parent too */
Mauro Carvalho Chehab24e9a472013-11-02 06:20:16 -0300224 device_release_driver(d);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300225
226 if (notifier->unbind)
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300227 notifier->unbind(notifier, sd, sd->asd);
Mauro Carvalho Chehab24e9a472013-11-02 06:20:16 -0300228
229 /*
230 * Store device at the device cache, in order to call
231 * put_device() on the final step
232 */
233 if (dev)
234 dev[i++] = d;
235 else
236 put_device(d);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300237 }
238
239 mutex_unlock(&list_lock);
240
Mauro Carvalho Chehab24e9a472013-11-02 06:20:16 -0300241 /*
242 * Call device_attach() to reprobe devices
243 *
244 * NOTE: If dev allocation fails, i is 0, and the whole loop won't be
245 * executed.
246 */
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300247 while (i--) {
248 struct device *d = dev[i];
249
250 if (d && device_attach(d) < 0) {
251 const char *name = "(none)";
252 int lock = device_trylock(d);
253
254 if (lock && d->driver)
255 name = d->driver->name;
256 dev_err(d, "Failed to re-probe to %s\n", name);
257 if (lock)
258 device_unlock(d);
259 }
260 put_device(d);
261 }
Mauro Carvalho Chehab24e9a472013-11-02 06:20:16 -0300262 kfree(dev);
Laurent Pinchart8e3fbfe2013-07-03 07:49:06 -0300263
264 notifier->v4l2_dev = NULL;
265
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300266 /*
267 * Don't care about the waiting list, it is initialised and populated
268 * upon notifier registration.
269 */
270}
271EXPORT_SYMBOL(v4l2_async_notifier_unregister);
272
273int v4l2_async_register_subdev(struct v4l2_subdev *sd)
274{
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300275 struct v4l2_async_notifier *notifier;
276
Sakari Ailus86217652015-06-11 12:18:01 -0700277 /*
278 * No reference taken. The reference is held by the device
279 * (struct v4l2_subdev.dev), and async sub-device does not
280 * exist independently of the device at any point of time.
281 */
282 if (!sd->of_node && sd->dev)
283 sd->of_node = sd->dev->of_node;
284
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300285 mutex_lock(&list_lock);
286
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300287 INIT_LIST_HEAD(&sd->async_list);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300288
289 list_for_each_entry(notifier, &notifier_list, list) {
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300290 struct v4l2_async_subdev *asd = v4l2_async_belongs(notifier, sd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300291 if (asd) {
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300292 int ret = v4l2_async_test_notify(notifier, sd, asd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300293 mutex_unlock(&list_lock);
294 return ret;
295 }
296 }
297
298 /* None matched, wait for hot-plugging */
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300299 list_add(&sd->async_list, &subdev_list);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300300
301 mutex_unlock(&list_lock);
302
303 return 0;
304}
305EXPORT_SYMBOL(v4l2_async_register_subdev);
306
307void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
308{
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300309 struct v4l2_async_notifier *notifier = sd->notifier;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300310
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300311 if (!sd->asd) {
312 if (!list_empty(&sd->async_list))
313 v4l2_async_cleanup(sd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300314 return;
315 }
316
317 mutex_lock(&list_lock);
318
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300319 list_add(&sd->asd->list, &notifier->waiting);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300320
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300321 v4l2_async_cleanup(sd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300322
323 if (notifier->unbind)
Sylwester Nawrockib426b3a2013-07-22 08:01:33 -0300324 notifier->unbind(notifier, sd, sd->asd);
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300325
326 mutex_unlock(&list_lock);
327}
328EXPORT_SYMBOL(v4l2_async_unregister_subdev);