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