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.
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
);
30 asd
->match
.i2c
.adapter_id
== client
->adapter
->nr
&&
31 asd
->match
.i2c
.address
== client
->addr
;
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
, ¬ifier
->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
;
66 case V4L2_ASYNC_MATCH_DEVNAME
:
67 match
= match_devname
;
69 case V4L2_ASYNC_MATCH_I2C
:
72 case V4L2_ASYNC_MATCH_OF
:
76 /* Cannot happen, unless someone breaks us */
81 /* match cannot be NULL here */
82 if (match(sd
->dev
, asd
))
89 static int v4l2_async_test_notify(struct v4l2_async_notifier
*notifier
,
90 struct v4l2_subdev
*sd
,
91 struct v4l2_async_subdev
*asd
)
95 /* Remove from the waiting list */
98 sd
->notifier
= notifier
;
100 if (notifier
->bound
) {
101 ret
= notifier
->bound(notifier
, sd
, asd
);
105 /* Move from the global subdevice list to notifier's done */
106 list_move(&sd
->async_list
, ¬ifier
->done
);
108 ret
= v4l2_device_register_subdev(notifier
->v4l2_dev
, sd
);
110 if (notifier
->unbind
)
111 notifier
->unbind(notifier
, sd
, asd
);
115 if (list_empty(¬ifier
->waiting
) && notifier
->complete
)
116 return notifier
->complete(notifier
);
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
);
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
;
137 if (!notifier
->num_subdevs
|| notifier
->num_subdevs
> V4L2_MAX_SUBDEVS
)
140 notifier
->v4l2_dev
= v4l2_dev
;
141 INIT_LIST_HEAD(¬ifier
->waiting
);
142 INIT_LIST_HEAD(¬ifier
->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
:
154 dev_err(notifier
->v4l2_dev
? notifier
->v4l2_dev
->dev
: NULL
,
155 "Invalid match type %u on %p\n",
156 asd
->match_type
, asd
);
159 list_add_tail(&asd
->list
, ¬ifier
->waiting
);
162 mutex_lock(&list_lock
);
164 /* Keep also completed notifiers on the list */
165 list_add(¬ifier
->list
, ¬ifier_list
);
167 list_for_each_entry_safe(sd
, tmp
, &subdev_list
, async_list
) {
170 asd
= v4l2_async_belongs(notifier
, sd
);
174 ret
= v4l2_async_test_notify(notifier
, sd
, asd
);
176 mutex_unlock(&list_lock
);
181 mutex_unlock(&list_lock
);
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
);
195 if (!notifier
->v4l2_dev
)
198 dev
= kmalloc(n_subdev
* sizeof(*dev
), GFP_KERNEL
);
200 dev_err(notifier
->v4l2_dev
->dev
,
201 "Failed to allocate device cache!\n");
204 mutex_lock(&list_lock
);
206 list_del(¬ifier
->list
);
208 list_for_each_entry_safe(sd
, tmp
, ¬ifier
->done
, async_list
) {
211 d
= get_device(sd
->dev
);
213 v4l2_async_cleanup(sd
);
215 /* If we handled USB devices, we'd have to lock the parent too */
216 device_release_driver(d
);
218 if (notifier
->unbind
)
219 notifier
->unbind(notifier
, sd
, sd
->asd
);
222 * Store device at the device cache, in order to call
223 * put_device() on the final step
231 mutex_unlock(&list_lock
);
234 * Call device_attach() to reprobe devices
236 * NOTE: If dev allocation fails, i is 0, and the whole loop won't be
240 struct device
*d
= dev
[i
];
242 if (d
&& device_attach(d
) < 0) {
243 const char *name
= "(none)";
244 int lock
= device_trylock(d
);
246 if (lock
&& d
->driver
)
247 name
= d
->driver
->name
;
248 dev_err(d
, "Failed to re-probe to %s\n", name
);
256 notifier
->v4l2_dev
= NULL
;
259 * Don't care about the waiting list, it is initialised and populated
260 * upon notifier registration.
263 EXPORT_SYMBOL(v4l2_async_notifier_unregister
);
265 int v4l2_async_register_subdev(struct v4l2_subdev
*sd
)
267 struct v4l2_async_notifier
*notifier
;
269 mutex_lock(&list_lock
);
271 INIT_LIST_HEAD(&sd
->async_list
);
273 list_for_each_entry(notifier
, ¬ifier_list
, list
) {
274 struct v4l2_async_subdev
*asd
= v4l2_async_belongs(notifier
, sd
);
276 int ret
= v4l2_async_test_notify(notifier
, sd
, asd
);
277 mutex_unlock(&list_lock
);
282 /* None matched, wait for hot-plugging */
283 list_add(&sd
->async_list
, &subdev_list
);
285 mutex_unlock(&list_lock
);
289 EXPORT_SYMBOL(v4l2_async_register_subdev
);
291 void v4l2_async_unregister_subdev(struct v4l2_subdev
*sd
)
293 struct v4l2_async_notifier
*notifier
= sd
->notifier
;
296 if (!list_empty(&sd
->async_list
))
297 v4l2_async_cleanup(sd
);
301 mutex_lock(&list_lock
);
303 list_add(&sd
->asd
->list
, ¬ifier
->waiting
);
305 v4l2_async_cleanup(sd
);
307 if (notifier
->unbind
)
308 notifier
->unbind(notifier
, sd
, sd
->asd
);
310 mutex_unlock(&list_lock
);
312 EXPORT_SYMBOL(v4l2_async_unregister_subdev
);