[media] media-device: supress backlinks at G_TOPOLOGY ioctl
[linux-2.6-block.git] / drivers / media / media-device.c
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
23 #include <linux/compat.h>
24 #include <linux/export.h>
25 #include <linux/ioctl.h>
26 #include <linux/media.h>
27 #include <linux/types.h>
28 #include <linux/slab.h>
29
30 #include <media/media-device.h>
31 #include <media/media-devnode.h>
32 #include <media/media-entity.h>
33
34 #ifdef CONFIG_MEDIA_CONTROLLER
35
36 /* -----------------------------------------------------------------------------
37  * Userspace API
38  */
39
40 static int media_device_open(struct file *filp)
41 {
42         return 0;
43 }
44
45 static int media_device_close(struct file *filp)
46 {
47         return 0;
48 }
49
50 static 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
66         if (copy_to_user(__info, &info, sizeof(*__info)))
67                 return -EFAULT;
68         return 0;
69 }
70
71 static 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) {
81                 if (((media_entity_id(entity) == id) && !next) ||
82                     ((media_entity_id(entity) > id) && next)) {
83                         spin_unlock(&mdev->lock);
84                         return entity;
85                 }
86         }
87
88         spin_unlock(&mdev->lock);
89
90         return NULL;
91 }
92
93 static 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
99         memset(&u_ent, 0, sizeof(u_ent));
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
108         u_ent.id = media_entity_id(ent);
109         if (ent->name)
110                 strlcpy(u_ent.name, ent->name, sizeof(u_ent.name));
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;
117         memcpy(&u_ent.raw, &ent->info, sizeof(ent->info));
118         if (copy_to_user(uent, &u_ent, sizeof(u_ent)))
119                 return -EFAULT;
120         return 0;
121 }
122
123 static void media_device_kpad_to_upad(const struct media_pad *kpad,
124                                       struct media_pad_desc *upad)
125 {
126         upad->entity = media_entity_id(kpad->entity);
127         upad->index = kpad->index;
128         upad->flags = kpad->flags;
129 }
130
131 static long __media_device_enum_links(struct media_device *mdev,
132                                       struct media_links_enum *links)
133 {
134         struct media_entity *entity;
135
136         entity = find_entity(mdev, links->entity);
137         if (entity == NULL)
138                 return -EINVAL;
139
140         if (links->pads) {
141                 unsigned int p;
142
143                 for (p = 0; p < entity->num_pads; p++) {
144                         struct media_pad_desc pad;
145
146                         memset(&pad, 0, sizeof(pad));
147                         media_device_kpad_to_upad(&entity->pads[p], &pad);
148                         if (copy_to_user(&links->pads[p], &pad, sizeof(pad)))
149                                 return -EFAULT;
150                 }
151         }
152
153         if (links->links) {
154                 struct media_link *ent_link;
155                 struct media_link_desc __user *ulink = links->links;
156
157                 list_for_each_entry(ent_link, &entity->links, list) {
158                         struct media_link_desc link;
159
160                         /* Ignore backlinks. */
161                         if (ent_link->source->entity != entity)
162                                 continue;
163                         memset(&link, 0, sizeof(link));
164                         media_device_kpad_to_upad(ent_link->source,
165                                                   &link.source);
166                         media_device_kpad_to_upad(ent_link->sink,
167                                                   &link.sink);
168                         link.flags = ent_link->flags;
169                         if (copy_to_user(ulink, &link, sizeof(*ulink)))
170                                 return -EFAULT;
171                         ulink++;
172                 }
173         }
174
175         return 0;
176 }
177
178 static 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
191         if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
192                 return -EFAULT;
193
194         return 0;
195 }
196
197 static 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
235 static 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                 if (link->is_backlink)
337                         continue;
338
339                 i++;
340
341                 if (ret || !topo->links)
342                         continue;
343
344                 if (i > topo->num_links) {
345                         ret = -ENOSPC;
346                         continue;
347                 }
348
349                 memset(&ulink, 0, sizeof(ulink));
350
351                 /* Copy link fields to userspace struct */
352                 ulink.id = link->graph_obj.id;
353                 ulink.source_id = link->gobj0->id;
354                 ulink.sink_id = link->gobj1->id;
355                 ulink.flags = link->flags;
356
357                 if (media_type(link->gobj0) != MEDIA_GRAPH_PAD)
358                         ulink.flags |= MEDIA_LNK_FL_INTERFACE_LINK;
359
360                 if (copy_to_user(&topo->links[i - 1], &ulink, sizeof(ulink)))
361                         ret = -EFAULT;
362         }
363         topo->num_links = i;
364
365         return ret;
366 }
367
368 static long media_device_get_topology(struct media_device *mdev,
369                                       struct media_v2_topology __user *utopo)
370 {
371         struct media_v2_topology ktopo;
372         int ret;
373
374         ret = copy_from_user(&ktopo, utopo, sizeof(ktopo));
375
376         if (ret < 0)
377                 return ret;
378
379         ret = __media_device_get_topology(mdev, &ktopo);
380         if (ret < 0)
381                 return ret;
382
383         ret = copy_to_user(utopo, &ktopo, sizeof(*utopo));
384
385         return ret;
386 }
387
388 static long media_device_ioctl(struct file *filp, unsigned int cmd,
389                                unsigned long arg)
390 {
391         struct media_devnode *devnode = media_devnode_data(filp);
392         struct media_device *dev = to_media_device(devnode);
393         long ret;
394
395         switch (cmd) {
396         case MEDIA_IOC_DEVICE_INFO:
397                 ret = media_device_get_info(dev,
398                                 (struct media_device_info __user *)arg);
399                 break;
400
401         case MEDIA_IOC_ENUM_ENTITIES:
402                 ret = media_device_enum_entities(dev,
403                                 (struct media_entity_desc __user *)arg);
404                 break;
405
406         case MEDIA_IOC_ENUM_LINKS:
407                 mutex_lock(&dev->graph_mutex);
408                 ret = media_device_enum_links(dev,
409                                 (struct media_links_enum __user *)arg);
410                 mutex_unlock(&dev->graph_mutex);
411                 break;
412
413         case MEDIA_IOC_SETUP_LINK:
414                 mutex_lock(&dev->graph_mutex);
415                 ret = media_device_setup_link(dev,
416                                 (struct media_link_desc __user *)arg);
417                 mutex_unlock(&dev->graph_mutex);
418                 break;
419
420         case MEDIA_IOC_G_TOPOLOGY:
421                 mutex_lock(&dev->graph_mutex);
422                 ret = media_device_get_topology(dev,
423                                 (struct media_v2_topology __user *)arg);
424                 mutex_unlock(&dev->graph_mutex);
425                 break;
426
427         default:
428                 ret = -ENOIOCTLCMD;
429         }
430
431         return ret;
432 }
433
434 #ifdef CONFIG_COMPAT
435
436 struct media_links_enum32 {
437         __u32 entity;
438         compat_uptr_t pads; /* struct media_pad_desc * */
439         compat_uptr_t links; /* struct media_link_desc * */
440         __u32 reserved[4];
441 };
442
443 static long media_device_enum_links32(struct media_device *mdev,
444                                       struct media_links_enum32 __user *ulinks)
445 {
446         struct media_links_enum links;
447         compat_uptr_t pads_ptr, links_ptr;
448
449         memset(&links, 0, sizeof(links));
450
451         if (get_user(links.entity, &ulinks->entity)
452             || get_user(pads_ptr, &ulinks->pads)
453             || get_user(links_ptr, &ulinks->links))
454                 return -EFAULT;
455
456         links.pads = compat_ptr(pads_ptr);
457         links.links = compat_ptr(links_ptr);
458
459         return __media_device_enum_links(mdev, &links);
460 }
461
462 #define MEDIA_IOC_ENUM_LINKS32          _IOWR('|', 0x02, struct media_links_enum32)
463
464 static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
465                                       unsigned long arg)
466 {
467         struct media_devnode *devnode = media_devnode_data(filp);
468         struct media_device *dev = to_media_device(devnode);
469         long ret;
470
471         switch (cmd) {
472         case MEDIA_IOC_DEVICE_INFO:
473         case MEDIA_IOC_ENUM_ENTITIES:
474         case MEDIA_IOC_SETUP_LINK:
475         case MEDIA_IOC_G_TOPOLOGY:
476                 return media_device_ioctl(filp, cmd, arg);
477
478         case MEDIA_IOC_ENUM_LINKS32:
479                 mutex_lock(&dev->graph_mutex);
480                 ret = media_device_enum_links32(dev,
481                                 (struct media_links_enum32 __user *)arg);
482                 mutex_unlock(&dev->graph_mutex);
483                 break;
484
485         default:
486                 ret = -ENOIOCTLCMD;
487         }
488
489         return ret;
490 }
491 #endif /* CONFIG_COMPAT */
492
493 static const struct media_file_operations media_device_fops = {
494         .owner = THIS_MODULE,
495         .open = media_device_open,
496         .ioctl = media_device_ioctl,
497 #ifdef CONFIG_COMPAT
498         .compat_ioctl = media_device_compat_ioctl,
499 #endif /* CONFIG_COMPAT */
500         .release = media_device_close,
501 };
502
503 /* -----------------------------------------------------------------------------
504  * sysfs
505  */
506
507 static ssize_t show_model(struct device *cd,
508                           struct device_attribute *attr, char *buf)
509 {
510         struct media_device *mdev = to_media_device(to_media_devnode(cd));
511
512         return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
513 }
514
515 static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
516
517 /* -----------------------------------------------------------------------------
518  * Registration/unregistration
519  */
520
521 static void media_device_release(struct media_devnode *mdev)
522 {
523         dev_dbg(mdev->parent, "Media device released\n");
524 }
525
526 /**
527  * media_device_register - register a media device
528  * @mdev:       The media device
529  *
530  * The caller is responsible for initializing the media device before
531  * registration. The following fields must be set:
532  *
533  * - dev must point to the parent device
534  * - model must be filled with the device model name
535  */
536 int __must_check __media_device_register(struct media_device *mdev,
537                                          struct module *owner)
538 {
539         int ret;
540
541         if (WARN_ON(mdev->dev == NULL || mdev->model[0] == 0))
542                 return -EINVAL;
543
544         INIT_LIST_HEAD(&mdev->entities);
545         INIT_LIST_HEAD(&mdev->interfaces);
546         INIT_LIST_HEAD(&mdev->pads);
547         INIT_LIST_HEAD(&mdev->links);
548         spin_lock_init(&mdev->lock);
549         mutex_init(&mdev->graph_mutex);
550
551         /* Register the device node. */
552         mdev->devnode.fops = &media_device_fops;
553         mdev->devnode.parent = mdev->dev;
554         mdev->devnode.release = media_device_release;
555         ret = media_devnode_register(&mdev->devnode, owner);
556         if (ret < 0)
557                 return ret;
558
559         ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
560         if (ret < 0) {
561                 media_devnode_unregister(&mdev->devnode);
562                 return ret;
563         }
564
565         dev_dbg(mdev->dev, "Media device registered\n");
566
567         return 0;
568 }
569 EXPORT_SYMBOL_GPL(__media_device_register);
570
571 /**
572  * media_device_unregister - unregister a media device
573  * @mdev:       The media device
574  *
575  */
576 void media_device_unregister(struct media_device *mdev)
577 {
578         struct media_entity *entity;
579         struct media_entity *next;
580         struct media_interface *intf, *tmp_intf;
581
582         /* Remove all entities from the media device */
583         list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
584                 media_device_unregister_entity(entity);
585
586         /* Remove all interfaces from the media device */
587         spin_lock(&mdev->lock);
588         list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces,
589                                  graph_obj.list) {
590                 __media_remove_intf_links(intf);
591                 media_gobj_remove(&intf->graph_obj);
592                 kfree(intf);
593         }
594         spin_unlock(&mdev->lock);
595
596         device_remove_file(&mdev->devnode.dev, &dev_attr_model);
597         media_devnode_unregister(&mdev->devnode);
598
599         dev_dbg(mdev->dev, "Media device unregistered\n");
600 }
601 EXPORT_SYMBOL_GPL(media_device_unregister);
602
603 /**
604  * media_device_register_entity - Register an entity with a media device
605  * @mdev:       The media device
606  * @entity:     The entity
607  */
608 int __must_check media_device_register_entity(struct media_device *mdev,
609                                               struct media_entity *entity)
610 {
611         int i;
612
613         if (entity->type == MEDIA_ENT_T_V4L2_SUBDEV_UNKNOWN ||
614             entity->type == MEDIA_ENT_T_UNKNOWN)
615                 dev_warn(mdev->dev,
616                          "Entity type for entity %s was not initialized!\n",
617                          entity->name);
618
619         /* Warn if we apparently re-register an entity */
620         WARN_ON(entity->graph_obj.mdev != NULL);
621         entity->graph_obj.mdev = mdev;
622         INIT_LIST_HEAD(&entity->links);
623
624         spin_lock(&mdev->lock);
625         /* Initialize media_gobj embedded at the entity */
626         media_gobj_init(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj);
627
628         /* Initialize objects at the pads */
629         for (i = 0; i < entity->num_pads; i++)
630                 media_gobj_init(mdev, MEDIA_GRAPH_PAD,
631                                &entity->pads[i].graph_obj);
632
633         spin_unlock(&mdev->lock);
634
635         return 0;
636 }
637 EXPORT_SYMBOL_GPL(media_device_register_entity);
638
639 /**
640  * media_device_unregister_entity - Unregister an entity
641  * @entity:     The entity
642  *
643  * If the entity has never been registered this function will return
644  * immediately.
645  */
646 void media_device_unregister_entity(struct media_entity *entity)
647 {
648         int i;
649         struct media_device *mdev = entity->graph_obj.mdev;
650         struct media_link *link, *tmp;
651         struct media_interface *intf;
652
653         if (mdev == NULL)
654                 return;
655
656         spin_lock(&mdev->lock);
657
658         /* Remove all interface links pointing to this entity */
659         list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) {
660                 list_for_each_entry_safe(link, tmp, &intf->links, list) {
661                         if (link->entity == entity)
662                                 __media_remove_intf_link(link);
663                 }
664         }
665
666         /* Remove all data links that belong to this entity */
667         __media_entity_remove_links(entity);
668
669         /* Remove all pads that belong to this entity */
670         for (i = 0; i < entity->num_pads; i++)
671                 media_gobj_remove(&entity->pads[i].graph_obj);
672
673         /* Remove the entity */
674         media_gobj_remove(&entity->graph_obj);
675
676         spin_unlock(&mdev->lock);
677         entity->graph_obj.mdev = NULL;
678 }
679 EXPORT_SYMBOL_GPL(media_device_unregister_entity);
680
681 static void media_device_release_devres(struct device *dev, void *res)
682 {
683 }
684
685 /*
686  * media_device_get_devres() -  get media device as device resource
687  *                              creates if one doesn't exist
688 */
689 struct media_device *media_device_get_devres(struct device *dev)
690 {
691         struct media_device *mdev;
692
693         mdev = devres_find(dev, media_device_release_devres, NULL, NULL);
694         if (mdev)
695                 return mdev;
696
697         mdev = devres_alloc(media_device_release_devres,
698                                 sizeof(struct media_device), GFP_KERNEL);
699         if (!mdev)
700                 return NULL;
701         return devres_get(dev, mdev, NULL, NULL);
702 }
703 EXPORT_SYMBOL_GPL(media_device_get_devres);
704
705 /*
706  * media_device_find_devres() - find media device as device resource
707 */
708 struct media_device *media_device_find_devres(struct device *dev)
709 {
710         return devres_find(dev, media_device_release_devres, NULL, NULL);
711 }
712 EXPORT_SYMBOL_GPL(media_device_find_devres);
713
714 #endif /* CONFIG_MEDIA_CONTROLLER */