4 * Copyright (C) 2010 Nokia Corporation
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.
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.
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
23 /* We need to access legacy defines from linux/media.h */
24 #define __NEED_MEDIA_LEGACY_API
26 #include <linux/compat.h>
27 #include <linux/export.h>
28 #include <linux/idr.h>
29 #include <linux/ioctl.h>
30 #include <linux/media.h>
31 #include <linux/slab.h>
32 #include <linux/types.h>
34 #include <media/media-device.h>
35 #include <media/media-devnode.h>
36 #include <media/media-entity.h>
38 #ifdef CONFIG_MEDIA_CONTROLLER
40 /* -----------------------------------------------------------------------------
44 static int media_device_open(struct file *filp)
49 static int media_device_close(struct file *filp)
54 static int media_device_get_info(struct media_device *dev,
55 struct media_device_info __user *__info)
57 struct media_device_info info;
59 memset(&info, 0, sizeof(info));
61 if (dev->driver_name[0])
62 strlcpy(info.driver, dev->driver_name, sizeof(info.driver));
64 strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver));
66 strlcpy(info.model, dev->model, sizeof(info.model));
67 strlcpy(info.serial, dev->serial, sizeof(info.serial));
68 strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info));
70 info.media_version = MEDIA_API_VERSION;
71 info.hw_revision = dev->hw_revision;
72 info.driver_version = dev->driver_version;
74 if (copy_to_user(__info, &info, sizeof(*__info)))
79 static struct media_entity *find_entity(struct media_device *mdev, u32 id)
81 struct media_entity *entity;
82 int next = id & MEDIA_ENT_ID_FLAG_NEXT;
84 id &= ~MEDIA_ENT_ID_FLAG_NEXT;
86 spin_lock(&mdev->lock);
88 media_device_for_each_entity(entity, mdev) {
89 if (((media_entity_id(entity) == id) && !next) ||
90 ((media_entity_id(entity) > id) && next)) {
91 spin_unlock(&mdev->lock);
96 spin_unlock(&mdev->lock);
101 static long media_device_enum_entities(struct media_device *mdev,
102 struct media_entity_desc __user *uent)
104 struct media_entity *ent;
105 struct media_entity_desc u_ent;
107 memset(&u_ent, 0, sizeof(u_ent));
108 if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id)))
111 ent = find_entity(mdev, u_ent.id);
116 u_ent.id = media_entity_id(ent);
118 strlcpy(u_ent.name, ent->name, sizeof(u_ent.name));
119 u_ent.type = ent->function;
120 u_ent.revision = 0; /* Unused */
121 u_ent.flags = ent->flags;
122 u_ent.group_id = 0; /* Unused */
123 u_ent.pads = ent->num_pads;
124 u_ent.links = ent->num_links - ent->num_backlinks;
127 * Workaround for a bug at media-ctl <= v1.10 that makes it to
128 * do the wrong thing if the entity function doesn't belong to
129 * either MEDIA_ENT_F_OLD_BASE or MEDIA_ENT_F_OLD_SUBDEV_BASE
132 * Non-subdevices are expected to be at the MEDIA_ENT_F_OLD_BASE,
133 * or, otherwise, will be silently ignored by media-ctl when
134 * printing the graphviz diagram. So, map them into the devnode
137 if (ent->function < MEDIA_ENT_F_OLD_BASE ||
138 ent->function > MEDIA_ENT_T_DEVNODE_UNKNOWN) {
139 if (is_media_entity_v4l2_subdev(ent))
140 u_ent.type = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
141 else if (ent->function != MEDIA_ENT_F_IO_V4L)
142 u_ent.type = MEDIA_ENT_T_DEVNODE_UNKNOWN;
145 memcpy(&u_ent.raw, &ent->info, sizeof(ent->info));
146 if (copy_to_user(uent, &u_ent, sizeof(u_ent)))
151 static void media_device_kpad_to_upad(const struct media_pad *kpad,
152 struct media_pad_desc *upad)
154 upad->entity = media_entity_id(kpad->entity);
155 upad->index = kpad->index;
156 upad->flags = kpad->flags;
159 static long __media_device_enum_links(struct media_device *mdev,
160 struct media_links_enum *links)
162 struct media_entity *entity;
164 entity = find_entity(mdev, links->entity);
171 for (p = 0; p < entity->num_pads; p++) {
172 struct media_pad_desc pad;
174 memset(&pad, 0, sizeof(pad));
175 media_device_kpad_to_upad(&entity->pads[p], &pad);
176 if (copy_to_user(&links->pads[p], &pad, sizeof(pad)))
182 struct media_link *link;
183 struct media_link_desc __user *ulink_desc = links->links;
185 list_for_each_entry(link, &entity->links, list) {
186 struct media_link_desc klink_desc;
188 /* Ignore backlinks. */
189 if (link->source->entity != entity)
191 memset(&klink_desc, 0, sizeof(klink_desc));
192 media_device_kpad_to_upad(link->source,
194 media_device_kpad_to_upad(link->sink,
196 klink_desc.flags = link->flags;
197 if (copy_to_user(ulink_desc, &klink_desc,
198 sizeof(*ulink_desc)))
207 static long media_device_enum_links(struct media_device *mdev,
208 struct media_links_enum __user *ulinks)
210 struct media_links_enum links;
213 if (copy_from_user(&links, ulinks, sizeof(links)))
216 rval = __media_device_enum_links(mdev, &links);
220 if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
226 static long media_device_setup_link(struct media_device *mdev,
227 struct media_link_desc __user *_ulink)
229 struct media_link *link = NULL;
230 struct media_link_desc ulink;
231 struct media_entity *source;
232 struct media_entity *sink;
235 if (copy_from_user(&ulink, _ulink, sizeof(ulink)))
238 /* Find the source and sink entities and link.
240 source = find_entity(mdev, ulink.source.entity);
241 sink = find_entity(mdev, ulink.sink.entity);
243 if (source == NULL || sink == NULL)
246 if (ulink.source.index >= source->num_pads ||
247 ulink.sink.index >= sink->num_pads)
250 link = media_entity_find_link(&source->pads[ulink.source.index],
251 &sink->pads[ulink.sink.index]);
255 /* Setup the link on both entities. */
256 ret = __media_entity_setup_link(link, ulink.flags);
258 if (copy_to_user(_ulink, &ulink, sizeof(ulink)))
264 static long __media_device_get_topology(struct media_device *mdev,
265 struct media_v2_topology *topo)
267 struct media_entity *entity;
268 struct media_interface *intf;
269 struct media_pad *pad;
270 struct media_link *link;
271 struct media_v2_entity kentity, *uentity;
272 struct media_v2_interface kintf, *uintf;
273 struct media_v2_pad kpad, *upad;
274 struct media_v2_link klink, *ulink;
278 topo->topology_version = mdev->topology_version;
280 /* Get entities and number of entities */
282 uentity = media_get_uptr(topo->ptr_entities);
283 media_device_for_each_entity(entity, mdev) {
288 if (i > topo->num_entities) {
293 /* Copy fields to userspace struct if not error */
294 memset(&kentity, 0, sizeof(kentity));
295 kentity.id = entity->graph_obj.id;
296 kentity.function = entity->function;
297 strncpy(kentity.name, entity->name,
298 sizeof(kentity.name));
300 if (copy_to_user(uentity, &kentity, sizeof(kentity)))
304 topo->num_entities = i;
306 /* Get interfaces and number of interfaces */
308 uintf = media_get_uptr(topo->ptr_interfaces);
309 media_device_for_each_intf(intf, mdev) {
314 if (i > topo->num_interfaces) {
319 memset(&kintf, 0, sizeof(kintf));
321 /* Copy intf fields to userspace struct */
322 kintf.id = intf->graph_obj.id;
323 kintf.intf_type = intf->type;
324 kintf.flags = intf->flags;
326 if (media_type(&intf->graph_obj) == MEDIA_GRAPH_INTF_DEVNODE) {
327 struct media_intf_devnode *devnode;
329 devnode = intf_to_devnode(intf);
331 kintf.devnode.major = devnode->major;
332 kintf.devnode.minor = devnode->minor;
335 if (copy_to_user(uintf, &kintf, sizeof(kintf)))
339 topo->num_interfaces = i;
341 /* Get pads and number of pads */
343 upad = media_get_uptr(topo->ptr_pads);
344 media_device_for_each_pad(pad, mdev) {
349 if (i > topo->num_pads) {
354 memset(&kpad, 0, sizeof(kpad));
356 /* Copy pad fields to userspace struct */
357 kpad.id = pad->graph_obj.id;
358 kpad.entity_id = pad->entity->graph_obj.id;
359 kpad.flags = pad->flags;
361 if (copy_to_user(upad, &kpad, sizeof(kpad)))
367 /* Get links and number of links */
369 ulink = media_get_uptr(topo->ptr_links);
370 media_device_for_each_link(link, mdev) {
371 if (link->is_backlink)
379 if (i > topo->num_links) {
384 memset(&klink, 0, sizeof(klink));
386 /* Copy link fields to userspace struct */
387 klink.id = link->graph_obj.id;
388 klink.source_id = link->gobj0->id;
389 klink.sink_id = link->gobj1->id;
390 klink.flags = link->flags;
392 if (copy_to_user(ulink, &klink, sizeof(klink)))
401 static long media_device_get_topology(struct media_device *mdev,
402 struct media_v2_topology __user *utopo)
404 struct media_v2_topology ktopo;
407 if (copy_from_user(&ktopo, utopo, sizeof(ktopo)))
410 ret = __media_device_get_topology(mdev, &ktopo);
414 if (copy_to_user(utopo, &ktopo, sizeof(*utopo)))
420 static long media_device_ioctl(struct file *filp, unsigned int cmd,
423 struct media_devnode *devnode = media_devnode_data(filp);
424 struct media_device *dev = to_media_device(devnode);
428 case MEDIA_IOC_DEVICE_INFO:
429 ret = media_device_get_info(dev,
430 (struct media_device_info __user *)arg);
433 case MEDIA_IOC_ENUM_ENTITIES:
434 ret = media_device_enum_entities(dev,
435 (struct media_entity_desc __user *)arg);
438 case MEDIA_IOC_ENUM_LINKS:
439 mutex_lock(&dev->graph_mutex);
440 ret = media_device_enum_links(dev,
441 (struct media_links_enum __user *)arg);
442 mutex_unlock(&dev->graph_mutex);
445 case MEDIA_IOC_SETUP_LINK:
446 mutex_lock(&dev->graph_mutex);
447 ret = media_device_setup_link(dev,
448 (struct media_link_desc __user *)arg);
449 mutex_unlock(&dev->graph_mutex);
452 case MEDIA_IOC_G_TOPOLOGY:
453 mutex_lock(&dev->graph_mutex);
454 ret = media_device_get_topology(dev,
455 (struct media_v2_topology __user *)arg);
456 mutex_unlock(&dev->graph_mutex);
468 struct media_links_enum32 {
470 compat_uptr_t pads; /* struct media_pad_desc * */
471 compat_uptr_t links; /* struct media_link_desc * */
475 static long media_device_enum_links32(struct media_device *mdev,
476 struct media_links_enum32 __user *ulinks)
478 struct media_links_enum links;
479 compat_uptr_t pads_ptr, links_ptr;
481 memset(&links, 0, sizeof(links));
483 if (get_user(links.entity, &ulinks->entity)
484 || get_user(pads_ptr, &ulinks->pads)
485 || get_user(links_ptr, &ulinks->links))
488 links.pads = compat_ptr(pads_ptr);
489 links.links = compat_ptr(links_ptr);
491 return __media_device_enum_links(mdev, &links);
494 #define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
496 static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
499 struct media_devnode *devnode = media_devnode_data(filp);
500 struct media_device *dev = to_media_device(devnode);
504 case MEDIA_IOC_DEVICE_INFO:
505 case MEDIA_IOC_ENUM_ENTITIES:
506 case MEDIA_IOC_SETUP_LINK:
507 case MEDIA_IOC_G_TOPOLOGY:
508 return media_device_ioctl(filp, cmd, arg);
510 case MEDIA_IOC_ENUM_LINKS32:
511 mutex_lock(&dev->graph_mutex);
512 ret = media_device_enum_links32(dev,
513 (struct media_links_enum32 __user *)arg);
514 mutex_unlock(&dev->graph_mutex);
523 #endif /* CONFIG_COMPAT */
525 static const struct media_file_operations media_device_fops = {
526 .owner = THIS_MODULE,
527 .open = media_device_open,
528 .ioctl = media_device_ioctl,
530 .compat_ioctl = media_device_compat_ioctl,
531 #endif /* CONFIG_COMPAT */
532 .release = media_device_close,
535 /* -----------------------------------------------------------------------------
539 static ssize_t show_model(struct device *cd,
540 struct device_attribute *attr, char *buf)
542 struct media_device *mdev = to_media_device(to_media_devnode(cd));
544 return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
547 static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
549 /* -----------------------------------------------------------------------------
550 * Registration/unregistration
553 static void media_device_release(struct media_devnode *mdev)
555 dev_dbg(mdev->parent, "Media device released\n");
559 * media_device_register_entity - Register an entity with a media device
560 * @mdev: The media device
561 * @entity: The entity
563 int __must_check media_device_register_entity(struct media_device *mdev,
564 struct media_entity *entity)
569 if (entity->function == MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN ||
570 entity->function == MEDIA_ENT_F_UNKNOWN)
572 "Entity type for entity %s was not initialized!\n",
575 /* Warn if we apparently re-register an entity */
576 WARN_ON(entity->graph_obj.mdev != NULL);
577 entity->graph_obj.mdev = mdev;
578 INIT_LIST_HEAD(&entity->links);
579 entity->num_links = 0;
580 entity->num_backlinks = 0;
582 if (!ida_pre_get(&mdev->entity_internal_idx, GFP_KERNEL))
585 spin_lock(&mdev->lock);
587 ret = ida_get_new_above(&mdev->entity_internal_idx, 1,
588 &entity->internal_idx);
590 spin_unlock(&mdev->lock);
594 mdev->entity_internal_idx_max =
595 max(mdev->entity_internal_idx_max, entity->internal_idx);
597 /* Initialize media_gobj embedded at the entity */
598 media_gobj_create(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj);
600 /* Initialize objects at the pads */
601 for (i = 0; i < entity->num_pads; i++)
602 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
603 &entity->pads[i].graph_obj);
605 spin_unlock(&mdev->lock);
609 EXPORT_SYMBOL_GPL(media_device_register_entity);
611 static void __media_device_unregister_entity(struct media_entity *entity)
613 struct media_device *mdev = entity->graph_obj.mdev;
614 struct media_link *link, *tmp;
615 struct media_interface *intf;
618 ida_simple_remove(&mdev->entity_internal_idx, entity->internal_idx);
620 /* Remove all interface links pointing to this entity */
621 list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) {
622 list_for_each_entry_safe(link, tmp, &intf->links, list) {
623 if (link->entity == entity)
624 __media_remove_intf_link(link);
628 /* Remove all data links that belong to this entity */
629 __media_entity_remove_links(entity);
631 /* Remove all pads that belong to this entity */
632 for (i = 0; i < entity->num_pads; i++)
633 media_gobj_destroy(&entity->pads[i].graph_obj);
635 /* Remove the entity */
636 media_gobj_destroy(&entity->graph_obj);
638 entity->graph_obj.mdev = NULL;
641 void media_device_unregister_entity(struct media_entity *entity)
643 struct media_device *mdev = entity->graph_obj.mdev;
648 spin_lock(&mdev->lock);
649 __media_device_unregister_entity(entity);
650 spin_unlock(&mdev->lock);
652 EXPORT_SYMBOL_GPL(media_device_unregister_entity);
655 * media_device_init() - initialize a media device
656 * @mdev: The media device
658 * The caller is responsible for initializing the media device before
659 * registration. The following fields must be set:
661 * - dev must point to the parent device
662 * - model must be filled with the device model name
664 void media_device_init(struct media_device *mdev)
666 INIT_LIST_HEAD(&mdev->entities);
667 INIT_LIST_HEAD(&mdev->interfaces);
668 INIT_LIST_HEAD(&mdev->pads);
669 INIT_LIST_HEAD(&mdev->links);
670 spin_lock_init(&mdev->lock);
671 mutex_init(&mdev->graph_mutex);
672 ida_init(&mdev->entity_internal_idx);
674 dev_dbg(mdev->dev, "Media device initialized\n");
676 EXPORT_SYMBOL_GPL(media_device_init);
678 void media_device_cleanup(struct media_device *mdev)
680 ida_destroy(&mdev->entity_internal_idx);
681 mdev->entity_internal_idx_max = 0;
682 mutex_destroy(&mdev->graph_mutex);
684 EXPORT_SYMBOL_GPL(media_device_cleanup);
686 int __must_check __media_device_register(struct media_device *mdev,
687 struct module *owner)
691 /* Register the device node. */
692 mdev->devnode.fops = &media_device_fops;
693 mdev->devnode.parent = mdev->dev;
694 mdev->devnode.release = media_device_release;
696 /* Set version 0 to indicate user-space that the graph is static */
697 mdev->topology_version = 0;
699 ret = media_devnode_register(&mdev->devnode, owner);
703 ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
705 media_devnode_unregister(&mdev->devnode);
709 dev_dbg(mdev->dev, "Media device registered\n");
713 EXPORT_SYMBOL_GPL(__media_device_register);
715 void media_device_unregister(struct media_device *mdev)
717 struct media_entity *entity;
718 struct media_entity *next;
719 struct media_interface *intf, *tmp_intf;
724 spin_lock(&mdev->lock);
726 /* Check if mdev was ever registered at all */
727 if (!media_devnode_is_registered(&mdev->devnode)) {
728 spin_unlock(&mdev->lock);
732 /* Remove all entities from the media device */
733 list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
734 __media_device_unregister_entity(entity);
736 /* Remove all interfaces from the media device */
737 list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces,
739 __media_remove_intf_links(intf);
740 media_gobj_destroy(&intf->graph_obj);
744 spin_unlock(&mdev->lock);
746 device_remove_file(&mdev->devnode.dev, &dev_attr_model);
747 media_devnode_unregister(&mdev->devnode);
749 dev_dbg(mdev->dev, "Media device unregistered\n");
751 EXPORT_SYMBOL_GPL(media_device_unregister);
753 static void media_device_release_devres(struct device *dev, void *res)
757 struct media_device *media_device_get_devres(struct device *dev)
759 struct media_device *mdev;
761 mdev = devres_find(dev, media_device_release_devres, NULL, NULL);
765 mdev = devres_alloc(media_device_release_devres,
766 sizeof(struct media_device), GFP_KERNEL);
769 return devres_get(dev, mdev, NULL, NULL);
771 EXPORT_SYMBOL_GPL(media_device_get_devres);
773 struct media_device *media_device_find_devres(struct device *dev)
775 return devres_find(dev, media_device_release_devres, NULL, NULL);
777 EXPORT_SYMBOL_GPL(media_device_find_devres);
779 #endif /* CONFIG_MEDIA_CONTROLLER */