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