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