]>
Commit | Line | Data |
---|---|---|
176fb0d1 LP |
1 | /* |
2 | * Media device | |
3 | * | |
4 | * Copyright (C) 2010 Nokia Corporation | |
5 | * | |
6 | * Contacts: Laurent Pinchart <[email protected]> | |
7 | * Sakari Ailus <[email protected]> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License version 2 as | |
11 | * published by the Free Software Foundation. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | */ | |
22 | ||
b0a1f2a8 SA |
23 | #include <linux/compat.h> |
24 | #include <linux/export.h> | |
665faa97 | 25 | #include <linux/idr.h> |
176fb0d1 | 26 | #include <linux/ioctl.h> |
140d8816 | 27 | #include <linux/media.h> |
57208e5e | 28 | #include <linux/slab.h> |
497e80cd | 29 | #include <linux/types.h> |
176fb0d1 LP |
30 | |
31 | #include <media/media-device.h> | |
32 | #include <media/media-devnode.h> | |
53e269c1 | 33 | #include <media/media-entity.h> |
176fb0d1 | 34 | |
e576d60b SK |
35 | #ifdef CONFIG_MEDIA_CONTROLLER |
36 | ||
140d8816 LP |
37 | /* ----------------------------------------------------------------------------- |
38 | * Userspace API | |
39 | */ | |
40 | ||
41 | static int media_device_open(struct file *filp) | |
42 | { | |
43 | return 0; | |
44 | } | |
45 | ||
46 | static int media_device_close(struct file *filp) | |
47 | { | |
48 | return 0; | |
49 | } | |
50 | ||
51 | static int media_device_get_info(struct media_device *dev, | |
52 | struct media_device_info __user *__info) | |
53 | { | |
54 | struct media_device_info info; | |
55 | ||
56 | memset(&info, 0, sizeof(info)); | |
57 | ||
58 | strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver)); | |
59 | strlcpy(info.model, dev->model, sizeof(info.model)); | |
60 | strlcpy(info.serial, dev->serial, sizeof(info.serial)); | |
61 | strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info)); | |
62 | ||
63 | info.media_version = MEDIA_API_VERSION; | |
64 | info.hw_revision = dev->hw_revision; | |
65 | info.driver_version = dev->driver_version; | |
66 | ||
b17d3945 NT |
67 | if (copy_to_user(__info, &info, sizeof(*__info))) |
68 | return -EFAULT; | |
69 | return 0; | |
140d8816 LP |
70 | } |
71 | ||
1651333b LP |
72 | static struct media_entity *find_entity(struct media_device *mdev, u32 id) |
73 | { | |
74 | struct media_entity *entity; | |
75 | int next = id & MEDIA_ENT_ID_FLAG_NEXT; | |
76 | ||
77 | id &= ~MEDIA_ENT_ID_FLAG_NEXT; | |
78 | ||
79 | spin_lock(&mdev->lock); | |
80 | ||
81 | media_device_for_each_entity(entity, mdev) { | |
fa762394 MCC |
82 | if (((media_entity_id(entity) == id) && !next) || |
83 | ((media_entity_id(entity) > id) && next)) { | |
1651333b LP |
84 | spin_unlock(&mdev->lock); |
85 | return entity; | |
86 | } | |
87 | } | |
88 | ||
89 | spin_unlock(&mdev->lock); | |
90 | ||
91 | return NULL; | |
92 | } | |
93 | ||
94 | static long media_device_enum_entities(struct media_device *mdev, | |
95 | struct media_entity_desc __user *uent) | |
96 | { | |
97 | struct media_entity *ent; | |
98 | struct media_entity_desc u_ent; | |
99 | ||
e6a62346 | 100 | memset(&u_ent, 0, sizeof(u_ent)); |
1651333b LP |
101 | if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id))) |
102 | return -EFAULT; | |
103 | ||
104 | ent = find_entity(mdev, u_ent.id); | |
105 | ||
106 | if (ent == NULL) | |
107 | return -EINVAL; | |
108 | ||
fa762394 | 109 | u_ent.id = media_entity_id(ent); |
de8eae36 LP |
110 | if (ent->name) |
111 | strlcpy(u_ent.name, ent->name, sizeof(u_ent.name)); | |
0e576b76 | 112 | u_ent.type = ent->function; |
8ed8c88c | 113 | u_ent.revision = 0; /* Unused */ |
1651333b | 114 | u_ent.flags = ent->flags; |
8ed8c88c | 115 | u_ent.group_id = 0; /* Unused */ |
1651333b LP |
116 | u_ent.pads = ent->num_pads; |
117 | u_ent.links = ent->num_links - ent->num_backlinks; | |
fa5034c6 | 118 | memcpy(&u_ent.raw, &ent->info, sizeof(ent->info)); |
1651333b LP |
119 | if (copy_to_user(uent, &u_ent, sizeof(u_ent))) |
120 | return -EFAULT; | |
121 | return 0; | |
122 | } | |
123 | ||
124 | static void media_device_kpad_to_upad(const struct media_pad *kpad, | |
125 | struct media_pad_desc *upad) | |
126 | { | |
fa762394 | 127 | upad->entity = media_entity_id(kpad->entity); |
1651333b LP |
128 | upad->index = kpad->index; |
129 | upad->flags = kpad->flags; | |
130 | } | |
131 | ||
b0a1f2a8 SA |
132 | static long __media_device_enum_links(struct media_device *mdev, |
133 | struct media_links_enum *links) | |
1651333b LP |
134 | { |
135 | struct media_entity *entity; | |
1651333b | 136 | |
b0a1f2a8 | 137 | entity = find_entity(mdev, links->entity); |
1651333b LP |
138 | if (entity == NULL) |
139 | return -EINVAL; | |
140 | ||
b0a1f2a8 | 141 | if (links->pads) { |
1651333b LP |
142 | unsigned int p; |
143 | ||
144 | for (p = 0; p < entity->num_pads; p++) { | |
145 | struct media_pad_desc pad; | |
c88e739b DC |
146 | |
147 | memset(&pad, 0, sizeof(pad)); | |
1651333b | 148 | media_device_kpad_to_upad(&entity->pads[p], &pad); |
b0a1f2a8 | 149 | if (copy_to_user(&links->pads[p], &pad, sizeof(pad))) |
1651333b LP |
150 | return -EFAULT; |
151 | } | |
152 | } | |
153 | ||
b0a1f2a8 | 154 | if (links->links) { |
b83e2508 MCC |
155 | struct media_link *link; |
156 | struct media_link_desc __user *ulink_desc = links->links; | |
1651333b | 157 | |
b83e2508 MCC |
158 | list_for_each_entry(link, &entity->links, list) { |
159 | struct media_link_desc klink_desc; | |
1651333b LP |
160 | |
161 | /* Ignore backlinks. */ | |
b83e2508 | 162 | if (link->source->entity != entity) |
1651333b | 163 | continue; |
b83e2508 MCC |
164 | memset(&klink_desc, 0, sizeof(klink_desc)); |
165 | media_device_kpad_to_upad(link->source, | |
166 | &klink_desc.source); | |
167 | media_device_kpad_to_upad(link->sink, | |
168 | &klink_desc.sink); | |
169 | klink_desc.flags = link->flags; | |
170 | if (copy_to_user(ulink_desc, &klink_desc, | |
171 | sizeof(*ulink_desc))) | |
1651333b | 172 | return -EFAULT; |
b83e2508 | 173 | ulink_desc++; |
1651333b LP |
174 | } |
175 | } | |
b0a1f2a8 SA |
176 | |
177 | return 0; | |
178 | } | |
179 | ||
180 | static long media_device_enum_links(struct media_device *mdev, | |
181 | struct media_links_enum __user *ulinks) | |
182 | { | |
183 | struct media_links_enum links; | |
184 | int rval; | |
185 | ||
186 | if (copy_from_user(&links, ulinks, sizeof(links))) | |
187 | return -EFAULT; | |
188 | ||
189 | rval = __media_device_enum_links(mdev, &links); | |
190 | if (rval < 0) | |
191 | return rval; | |
192 | ||
1651333b LP |
193 | if (copy_to_user(ulinks, &links, sizeof(*ulinks))) |
194 | return -EFAULT; | |
b0a1f2a8 | 195 | |
1651333b LP |
196 | return 0; |
197 | } | |
198 | ||
97548ed4 LP |
199 | static long media_device_setup_link(struct media_device *mdev, |
200 | struct media_link_desc __user *_ulink) | |
201 | { | |
202 | struct media_link *link = NULL; | |
203 | struct media_link_desc ulink; | |
204 | struct media_entity *source; | |
205 | struct media_entity *sink; | |
206 | int ret; | |
207 | ||
208 | if (copy_from_user(&ulink, _ulink, sizeof(ulink))) | |
209 | return -EFAULT; | |
210 | ||
211 | /* Find the source and sink entities and link. | |
212 | */ | |
213 | source = find_entity(mdev, ulink.source.entity); | |
214 | sink = find_entity(mdev, ulink.sink.entity); | |
215 | ||
216 | if (source == NULL || sink == NULL) | |
217 | return -EINVAL; | |
218 | ||
219 | if (ulink.source.index >= source->num_pads || | |
220 | ulink.sink.index >= sink->num_pads) | |
221 | return -EINVAL; | |
222 | ||
223 | link = media_entity_find_link(&source->pads[ulink.source.index], | |
224 | &sink->pads[ulink.sink.index]); | |
225 | if (link == NULL) | |
226 | return -EINVAL; | |
227 | ||
228 | /* Setup the link on both entities. */ | |
229 | ret = __media_entity_setup_link(link, ulink.flags); | |
230 | ||
231 | if (copy_to_user(_ulink, &ulink, sizeof(ulink))) | |
232 | return -EFAULT; | |
233 | ||
234 | return ret; | |
235 | } | |
236 | ||
8309f47c MCC |
237 | static long __media_device_get_topology(struct media_device *mdev, |
238 | struct media_v2_topology *topo) | |
239 | { | |
240 | struct media_entity *entity; | |
241 | struct media_interface *intf; | |
242 | struct media_pad *pad; | |
243 | struct media_link *link; | |
b3b7a9f1 MCC |
244 | struct media_v2_entity kentity, *uentity; |
245 | struct media_v2_interface kintf, *uintf; | |
246 | struct media_v2_pad kpad, *upad; | |
247 | struct media_v2_link klink, *ulink; | |
9033b1a4 MCC |
248 | unsigned int i; |
249 | int ret = 0; | |
8309f47c MCC |
250 | |
251 | topo->topology_version = mdev->topology_version; | |
252 | ||
253 | /* Get entities and number of entities */ | |
254 | i = 0; | |
b3b7a9f1 | 255 | uentity = media_get_uptr(topo->ptr_entities); |
8309f47c MCC |
256 | media_device_for_each_entity(entity, mdev) { |
257 | i++; | |
b3b7a9f1 | 258 | if (ret || !uentity) |
8309f47c MCC |
259 | continue; |
260 | ||
261 | if (i > topo->num_entities) { | |
262 | ret = -ENOSPC; | |
263 | continue; | |
264 | } | |
265 | ||
266 | /* Copy fields to userspace struct if not error */ | |
b3b7a9f1 MCC |
267 | memset(&kentity, 0, sizeof(kentity)); |
268 | kentity.id = entity->graph_obj.id; | |
269 | kentity.function = entity->function; | |
270 | strncpy(kentity.name, entity->name, | |
271 | sizeof(kentity.name)); | |
8309f47c | 272 | |
b3b7a9f1 | 273 | if (copy_to_user(uentity, &kentity, sizeof(kentity))) |
8309f47c | 274 | ret = -EFAULT; |
b3b7a9f1 | 275 | uentity++; |
8309f47c MCC |
276 | } |
277 | topo->num_entities = i; | |
278 | ||
279 | /* Get interfaces and number of interfaces */ | |
280 | i = 0; | |
b3b7a9f1 | 281 | uintf = media_get_uptr(topo->ptr_interfaces); |
8309f47c MCC |
282 | media_device_for_each_intf(intf, mdev) { |
283 | i++; | |
b3b7a9f1 | 284 | if (ret || !uintf) |
8309f47c MCC |
285 | continue; |
286 | ||
287 | if (i > topo->num_interfaces) { | |
288 | ret = -ENOSPC; | |
289 | continue; | |
290 | } | |
291 | ||
b3b7a9f1 | 292 | memset(&kintf, 0, sizeof(kintf)); |
8309f47c MCC |
293 | |
294 | /* Copy intf fields to userspace struct */ | |
b3b7a9f1 MCC |
295 | kintf.id = intf->graph_obj.id; |
296 | kintf.intf_type = intf->type; | |
297 | kintf.flags = intf->flags; | |
8309f47c MCC |
298 | |
299 | if (media_type(&intf->graph_obj) == MEDIA_GRAPH_INTF_DEVNODE) { | |
300 | struct media_intf_devnode *devnode; | |
301 | ||
302 | devnode = intf_to_devnode(intf); | |
303 | ||
b3b7a9f1 MCC |
304 | kintf.devnode.major = devnode->major; |
305 | kintf.devnode.minor = devnode->minor; | |
8309f47c MCC |
306 | } |
307 | ||
b3b7a9f1 | 308 | if (copy_to_user(uintf, &kintf, sizeof(kintf))) |
8309f47c | 309 | ret = -EFAULT; |
b3b7a9f1 | 310 | uintf++; |
8309f47c MCC |
311 | } |
312 | topo->num_interfaces = i; | |
313 | ||
314 | /* Get pads and number of pads */ | |
315 | i = 0; | |
b3b7a9f1 | 316 | upad = media_get_uptr(topo->ptr_pads); |
8309f47c MCC |
317 | media_device_for_each_pad(pad, mdev) { |
318 | i++; | |
b3b7a9f1 | 319 | if (ret || !upad) |
8309f47c MCC |
320 | continue; |
321 | ||
322 | if (i > topo->num_pads) { | |
323 | ret = -ENOSPC; | |
324 | continue; | |
325 | } | |
326 | ||
b3b7a9f1 | 327 | memset(&kpad, 0, sizeof(kpad)); |
8309f47c MCC |
328 | |
329 | /* Copy pad fields to userspace struct */ | |
b3b7a9f1 MCC |
330 | kpad.id = pad->graph_obj.id; |
331 | kpad.entity_id = pad->entity->graph_obj.id; | |
332 | kpad.flags = pad->flags; | |
8309f47c | 333 | |
b3b7a9f1 | 334 | if (copy_to_user(upad, &kpad, sizeof(kpad))) |
8309f47c | 335 | ret = -EFAULT; |
b3b7a9f1 | 336 | upad++; |
8309f47c MCC |
337 | } |
338 | topo->num_pads = i; | |
339 | ||
340 | /* Get links and number of links */ | |
341 | i = 0; | |
b3b7a9f1 | 342 | ulink = media_get_uptr(topo->ptr_links); |
8309f47c | 343 | media_device_for_each_link(link, mdev) { |
39d1ebc6 MCC |
344 | if (link->is_backlink) |
345 | continue; | |
346 | ||
8309f47c MCC |
347 | i++; |
348 | ||
b3b7a9f1 | 349 | if (ret || !ulink) |
8309f47c MCC |
350 | continue; |
351 | ||
352 | if (i > topo->num_links) { | |
353 | ret = -ENOSPC; | |
354 | continue; | |
355 | } | |
356 | ||
b3b7a9f1 | 357 | memset(&klink, 0, sizeof(klink)); |
8309f47c MCC |
358 | |
359 | /* Copy link fields to userspace struct */ | |
b3b7a9f1 MCC |
360 | klink.id = link->graph_obj.id; |
361 | klink.source_id = link->gobj0->id; | |
362 | klink.sink_id = link->gobj1->id; | |
363 | klink.flags = link->flags; | |
8309f47c | 364 | |
b3b7a9f1 | 365 | if (copy_to_user(ulink, &klink, sizeof(klink))) |
8309f47c | 366 | ret = -EFAULT; |
b3b7a9f1 | 367 | ulink++; |
8309f47c MCC |
368 | } |
369 | topo->num_links = i; | |
370 | ||
371 | return ret; | |
372 | } | |
373 | ||
374 | static long media_device_get_topology(struct media_device *mdev, | |
375 | struct media_v2_topology __user *utopo) | |
376 | { | |
377 | struct media_v2_topology ktopo; | |
378 | int ret; | |
379 | ||
a5c82e56 DC |
380 | if (copy_from_user(&ktopo, utopo, sizeof(ktopo))) |
381 | return -EFAULT; | |
8309f47c MCC |
382 | |
383 | ret = __media_device_get_topology(mdev, &ktopo); | |
384 | if (ret < 0) | |
385 | return ret; | |
386 | ||
a5c82e56 DC |
387 | if (copy_to_user(utopo, &ktopo, sizeof(*utopo))) |
388 | return -EFAULT; | |
8309f47c | 389 | |
a5c82e56 | 390 | return 0; |
8309f47c MCC |
391 | } |
392 | ||
140d8816 LP |
393 | static long media_device_ioctl(struct file *filp, unsigned int cmd, |
394 | unsigned long arg) | |
395 | { | |
396 | struct media_devnode *devnode = media_devnode_data(filp); | |
397 | struct media_device *dev = to_media_device(devnode); | |
398 | long ret; | |
399 | ||
400 | switch (cmd) { | |
401 | case MEDIA_IOC_DEVICE_INFO: | |
402 | ret = media_device_get_info(dev, | |
403 | (struct media_device_info __user *)arg); | |
404 | break; | |
405 | ||
1651333b LP |
406 | case MEDIA_IOC_ENUM_ENTITIES: |
407 | ret = media_device_enum_entities(dev, | |
408 | (struct media_entity_desc __user *)arg); | |
409 | break; | |
410 | ||
411 | case MEDIA_IOC_ENUM_LINKS: | |
412 | mutex_lock(&dev->graph_mutex); | |
413 | ret = media_device_enum_links(dev, | |
414 | (struct media_links_enum __user *)arg); | |
415 | mutex_unlock(&dev->graph_mutex); | |
416 | break; | |
417 | ||
97548ed4 LP |
418 | case MEDIA_IOC_SETUP_LINK: |
419 | mutex_lock(&dev->graph_mutex); | |
420 | ret = media_device_setup_link(dev, | |
421 | (struct media_link_desc __user *)arg); | |
422 | mutex_unlock(&dev->graph_mutex); | |
423 | break; | |
424 | ||
8309f47c MCC |
425 | case MEDIA_IOC_G_TOPOLOGY: |
426 | mutex_lock(&dev->graph_mutex); | |
427 | ret = media_device_get_topology(dev, | |
428 | (struct media_v2_topology __user *)arg); | |
429 | mutex_unlock(&dev->graph_mutex); | |
430 | break; | |
431 | ||
140d8816 LP |
432 | default: |
433 | ret = -ENOIOCTLCMD; | |
434 | } | |
435 | ||
436 | return ret; | |
437 | } | |
438 | ||
b0a1f2a8 SA |
439 | #ifdef CONFIG_COMPAT |
440 | ||
441 | struct media_links_enum32 { | |
442 | __u32 entity; | |
443 | compat_uptr_t pads; /* struct media_pad_desc * */ | |
444 | compat_uptr_t links; /* struct media_link_desc * */ | |
445 | __u32 reserved[4]; | |
446 | }; | |
447 | ||
448 | static long media_device_enum_links32(struct media_device *mdev, | |
449 | struct media_links_enum32 __user *ulinks) | |
450 | { | |
451 | struct media_links_enum links; | |
452 | compat_uptr_t pads_ptr, links_ptr; | |
453 | ||
454 | memset(&links, 0, sizeof(links)); | |
455 | ||
456 | if (get_user(links.entity, &ulinks->entity) | |
457 | || get_user(pads_ptr, &ulinks->pads) | |
458 | || get_user(links_ptr, &ulinks->links)) | |
459 | return -EFAULT; | |
460 | ||
461 | links.pads = compat_ptr(pads_ptr); | |
462 | links.links = compat_ptr(links_ptr); | |
463 | ||
464 | return __media_device_enum_links(mdev, &links); | |
465 | } | |
466 | ||
467 | #define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32) | |
468 | ||
469 | static long media_device_compat_ioctl(struct file *filp, unsigned int cmd, | |
470 | unsigned long arg) | |
471 | { | |
472 | struct media_devnode *devnode = media_devnode_data(filp); | |
473 | struct media_device *dev = to_media_device(devnode); | |
474 | long ret; | |
475 | ||
476 | switch (cmd) { | |
477 | case MEDIA_IOC_DEVICE_INFO: | |
478 | case MEDIA_IOC_ENUM_ENTITIES: | |
479 | case MEDIA_IOC_SETUP_LINK: | |
8309f47c | 480 | case MEDIA_IOC_G_TOPOLOGY: |
b0a1f2a8 SA |
481 | return media_device_ioctl(filp, cmd, arg); |
482 | ||
483 | case MEDIA_IOC_ENUM_LINKS32: | |
484 | mutex_lock(&dev->graph_mutex); | |
485 | ret = media_device_enum_links32(dev, | |
486 | (struct media_links_enum32 __user *)arg); | |
487 | mutex_unlock(&dev->graph_mutex); | |
488 | break; | |
489 | ||
490 | default: | |
491 | ret = -ENOIOCTLCMD; | |
492 | } | |
493 | ||
494 | return ret; | |
495 | } | |
496 | #endif /* CONFIG_COMPAT */ | |
497 | ||
176fb0d1 LP |
498 | static const struct media_file_operations media_device_fops = { |
499 | .owner = THIS_MODULE, | |
140d8816 LP |
500 | .open = media_device_open, |
501 | .ioctl = media_device_ioctl, | |
b0a1f2a8 SA |
502 | #ifdef CONFIG_COMPAT |
503 | .compat_ioctl = media_device_compat_ioctl, | |
504 | #endif /* CONFIG_COMPAT */ | |
140d8816 | 505 | .release = media_device_close, |
176fb0d1 LP |
506 | }; |
507 | ||
508 | /* ----------------------------------------------------------------------------- | |
509 | * sysfs | |
510 | */ | |
511 | ||
512 | static ssize_t show_model(struct device *cd, | |
513 | struct device_attribute *attr, char *buf) | |
514 | { | |
515 | struct media_device *mdev = to_media_device(to_media_devnode(cd)); | |
516 | ||
517 | return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model); | |
518 | } | |
519 | ||
520 | static DEVICE_ATTR(model, S_IRUGO, show_model, NULL); | |
521 | ||
522 | /* ----------------------------------------------------------------------------- | |
523 | * Registration/unregistration | |
524 | */ | |
525 | ||
526 | static void media_device_release(struct media_devnode *mdev) | |
527 | { | |
8f5a3188 | 528 | dev_dbg(mdev->parent, "Media device released\n"); |
176fb0d1 LP |
529 | } |
530 | ||
b2ed8af9 MCC |
531 | /** |
532 | * media_device_register_entity - Register an entity with a media device | |
533 | * @mdev: The media device | |
534 | * @entity: The entity | |
535 | */ | |
536 | int __must_check media_device_register_entity(struct media_device *mdev, | |
537 | struct media_entity *entity) | |
538 | { | |
539 | unsigned int i; | |
540 | ||
541 | if (entity->function == MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN || | |
542 | entity->function == MEDIA_ENT_F_UNKNOWN) | |
543 | dev_warn(mdev->dev, | |
544 | "Entity type for entity %s was not initialized!\n", | |
545 | entity->name); | |
546 | ||
547 | /* Warn if we apparently re-register an entity */ | |
548 | WARN_ON(entity->graph_obj.mdev != NULL); | |
549 | entity->graph_obj.mdev = mdev; | |
550 | INIT_LIST_HEAD(&entity->links); | |
551 | entity->num_links = 0; | |
552 | entity->num_backlinks = 0; | |
553 | ||
554 | spin_lock(&mdev->lock); | |
665faa97 SA |
555 | |
556 | entity->internal_idx = ida_simple_get(&mdev->entity_internal_idx, 1, 0, | |
557 | GFP_KERNEL); | |
558 | if (entity->internal_idx < 0) { | |
559 | spin_unlock(&mdev->lock); | |
560 | return entity->internal_idx; | |
561 | } | |
562 | ||
563 | mdev->entity_internal_idx_max = | |
564 | max(mdev->entity_internal_idx_max, entity->internal_idx); | |
565 | ||
b2ed8af9 MCC |
566 | /* Initialize media_gobj embedded at the entity */ |
567 | media_gobj_create(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj); | |
568 | ||
569 | /* Initialize objects at the pads */ | |
570 | for (i = 0; i < entity->num_pads; i++) | |
571 | media_gobj_create(mdev, MEDIA_GRAPH_PAD, | |
572 | &entity->pads[i].graph_obj); | |
573 | ||
574 | spin_unlock(&mdev->lock); | |
575 | ||
576 | return 0; | |
577 | } | |
578 | EXPORT_SYMBOL_GPL(media_device_register_entity); | |
579 | ||
821ed366 | 580 | static void __media_device_unregister_entity(struct media_entity *entity) |
b2ed8af9 MCC |
581 | { |
582 | struct media_device *mdev = entity->graph_obj.mdev; | |
583 | struct media_link *link, *tmp; | |
584 | struct media_interface *intf; | |
585 | unsigned int i; | |
586 | ||
665faa97 SA |
587 | ida_simple_remove(&mdev->entity_internal_idx, entity->internal_idx); |
588 | ||
b2ed8af9 MCC |
589 | /* Remove all interface links pointing to this entity */ |
590 | list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) { | |
591 | list_for_each_entry_safe(link, tmp, &intf->links, list) { | |
592 | if (link->entity == entity) | |
593 | __media_remove_intf_link(link); | |
594 | } | |
595 | } | |
596 | ||
597 | /* Remove all data links that belong to this entity */ | |
598 | __media_entity_remove_links(entity); | |
599 | ||
600 | /* Remove all pads that belong to this entity */ | |
601 | for (i = 0; i < entity->num_pads; i++) | |
602 | media_gobj_destroy(&entity->pads[i].graph_obj); | |
603 | ||
604 | /* Remove the entity */ | |
605 | media_gobj_destroy(&entity->graph_obj); | |
606 | ||
b2ed8af9 MCC |
607 | entity->graph_obj.mdev = NULL; |
608 | } | |
821ed366 MCC |
609 | |
610 | void media_device_unregister_entity(struct media_entity *entity) | |
611 | { | |
612 | struct media_device *mdev = entity->graph_obj.mdev; | |
613 | ||
614 | if (mdev == NULL) | |
615 | return; | |
616 | ||
617 | spin_lock(&mdev->lock); | |
618 | __media_device_unregister_entity(entity); | |
619 | spin_unlock(&mdev->lock); | |
620 | } | |
b2ed8af9 MCC |
621 | EXPORT_SYMBOL_GPL(media_device_unregister_entity); |
622 | ||
176fb0d1 | 623 | /** |
9832e155 | 624 | * media_device_init() - initialize a media device |
176fb0d1 LP |
625 | * @mdev: The media device |
626 | * | |
627 | * The caller is responsible for initializing the media device before | |
628 | * registration. The following fields must be set: | |
629 | * | |
630 | * - dev must point to the parent device | |
631 | * - model must be filled with the device model name | |
632 | */ | |
9832e155 | 633 | void media_device_init(struct media_device *mdev) |
176fb0d1 | 634 | { |
53e269c1 | 635 | INIT_LIST_HEAD(&mdev->entities); |
57cf79b7 | 636 | INIT_LIST_HEAD(&mdev->interfaces); |
9155d859 MCC |
637 | INIT_LIST_HEAD(&mdev->pads); |
638 | INIT_LIST_HEAD(&mdev->links); | |
53e269c1 | 639 | spin_lock_init(&mdev->lock); |
503c3d82 | 640 | mutex_init(&mdev->graph_mutex); |
665faa97 | 641 | ida_init(&mdev->entity_internal_idx); |
53e269c1 | 642 | |
9832e155 JMC |
643 | dev_dbg(mdev->dev, "Media device initialized\n"); |
644 | } | |
645 | EXPORT_SYMBOL_GPL(media_device_init); | |
646 | ||
9832e155 JMC |
647 | void media_device_cleanup(struct media_device *mdev) |
648 | { | |
665faa97 SA |
649 | ida_destroy(&mdev->entity_internal_idx); |
650 | mdev->entity_internal_idx_max = 0; | |
9832e155 JMC |
651 | mutex_destroy(&mdev->graph_mutex); |
652 | } | |
653 | EXPORT_SYMBOL_GPL(media_device_cleanup); | |
654 | ||
9832e155 JMC |
655 | int __must_check __media_device_register(struct media_device *mdev, |
656 | struct module *owner) | |
657 | { | |
658 | int ret; | |
659 | ||
176fb0d1 LP |
660 | /* Register the device node. */ |
661 | mdev->devnode.fops = &media_device_fops; | |
662 | mdev->devnode.parent = mdev->dev; | |
663 | mdev->devnode.release = media_device_release; | |
8a950796 JMC |
664 | |
665 | /* Set version 0 to indicate user-space that the graph is static */ | |
666 | mdev->topology_version = 0; | |
667 | ||
85de721c | 668 | ret = media_devnode_register(&mdev->devnode, owner); |
176fb0d1 LP |
669 | if (ret < 0) |
670 | return ret; | |
671 | ||
672 | ret = device_create_file(&mdev->devnode.dev, &dev_attr_model); | |
673 | if (ret < 0) { | |
674 | media_devnode_unregister(&mdev->devnode); | |
675 | return ret; | |
676 | } | |
677 | ||
8f5a3188 MCC |
678 | dev_dbg(mdev->dev, "Media device registered\n"); |
679 | ||
176fb0d1 LP |
680 | return 0; |
681 | } | |
85de721c | 682 | EXPORT_SYMBOL_GPL(__media_device_register); |
176fb0d1 | 683 | |
176fb0d1 LP |
684 | void media_device_unregister(struct media_device *mdev) |
685 | { | |
53e269c1 LP |
686 | struct media_entity *entity; |
687 | struct media_entity *next; | |
d47109fa MCC |
688 | struct media_interface *intf, *tmp_intf; |
689 | ||
821ed366 MCC |
690 | if (mdev == NULL) |
691 | return; | |
692 | ||
693 | spin_lock(&mdev->lock); | |
694 | ||
223d19c5 | 695 | /* Check if mdev was ever registered at all */ |
821ed366 MCC |
696 | if (!media_devnode_is_registered(&mdev->devnode)) { |
697 | spin_unlock(&mdev->lock); | |
223d19c5 | 698 | return; |
821ed366 | 699 | } |
223d19c5 | 700 | |
f50d5166 MCC |
701 | /* Remove all entities from the media device */ |
702 | list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list) | |
821ed366 | 703 | __media_device_unregister_entity(entity); |
f50d5166 | 704 | |
d47109fa | 705 | /* Remove all interfaces from the media device */ |
d47109fa MCC |
706 | list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces, |
707 | graph_obj.list) { | |
708 | __media_remove_intf_links(intf); | |
c350ef83 | 709 | media_gobj_destroy(&intf->graph_obj); |
d47109fa MCC |
710 | kfree(intf); |
711 | } | |
821ed366 | 712 | |
d47109fa | 713 | spin_unlock(&mdev->lock); |
53e269c1 | 714 | |
176fb0d1 LP |
715 | device_remove_file(&mdev->devnode.dev, &dev_attr_model); |
716 | media_devnode_unregister(&mdev->devnode); | |
8f5a3188 MCC |
717 | |
718 | dev_dbg(mdev->dev, "Media device unregistered\n"); | |
176fb0d1 LP |
719 | } |
720 | EXPORT_SYMBOL_GPL(media_device_unregister); | |
53e269c1 | 721 | |
d062f911 SK |
722 | static void media_device_release_devres(struct device *dev, void *res) |
723 | { | |
724 | } | |
725 | ||
d062f911 SK |
726 | struct media_device *media_device_get_devres(struct device *dev) |
727 | { | |
728 | struct media_device *mdev; | |
729 | ||
730 | mdev = devres_find(dev, media_device_release_devres, NULL, NULL); | |
731 | if (mdev) | |
732 | return mdev; | |
733 | ||
734 | mdev = devres_alloc(media_device_release_devres, | |
735 | sizeof(struct media_device), GFP_KERNEL); | |
736 | if (!mdev) | |
737 | return NULL; | |
738 | return devres_get(dev, mdev, NULL, NULL); | |
739 | } | |
740 | EXPORT_SYMBOL_GPL(media_device_get_devres); | |
741 | ||
d062f911 SK |
742 | struct media_device *media_device_find_devres(struct device *dev) |
743 | { | |
744 | return devres_find(dev, media_device_release_devres, NULL, NULL); | |
745 | } | |
746 | EXPORT_SYMBOL_GPL(media_device_find_devres); | |
e576d60b SK |
747 | |
748 | #endif /* CONFIG_MEDIA_CONTROLLER */ |