4 * Copyright (C) 2010 Nokia Corporation
6 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7 * Sakari Ailus <sakari.ailus@iki.fi>
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 strlcpy(info
.driver
, dev
->dev
->driver
->name
, sizeof(info
.driver
));
62 strlcpy(info
.model
, dev
->model
, sizeof(info
.model
));
63 strlcpy(info
.serial
, dev
->serial
, sizeof(info
.serial
));
64 strlcpy(info
.bus_info
, dev
->bus_info
, sizeof(info
.bus_info
));
66 info
.media_version
= MEDIA_API_VERSION
;
67 info
.hw_revision
= dev
->hw_revision
;
68 info
.driver_version
= dev
->driver_version
;
70 if (copy_to_user(__info
, &info
, sizeof(*__info
)))
75 static struct media_entity
*find_entity(struct media_device
*mdev
, u32 id
)
77 struct media_entity
*entity
;
78 int next
= id
& MEDIA_ENT_ID_FLAG_NEXT
;
80 id
&= ~MEDIA_ENT_ID_FLAG_NEXT
;
82 spin_lock(&mdev
->lock
);
84 media_device_for_each_entity(entity
, mdev
) {
85 if (((media_entity_id(entity
) == id
) && !next
) ||
86 ((media_entity_id(entity
) > id
) && next
)) {
87 spin_unlock(&mdev
->lock
);
92 spin_unlock(&mdev
->lock
);
97 static long media_device_enum_entities(struct media_device
*mdev
,
98 struct media_entity_desc __user
*uent
)
100 struct media_entity
*ent
;
101 struct media_entity_desc u_ent
;
103 memset(&u_ent
, 0, sizeof(u_ent
));
104 if (copy_from_user(&u_ent
.id
, &uent
->id
, sizeof(u_ent
.id
)))
107 ent
= find_entity(mdev
, u_ent
.id
);
112 u_ent
.id
= media_entity_id(ent
);
114 strlcpy(u_ent
.name
, ent
->name
, sizeof(u_ent
.name
));
115 u_ent
.type
= ent
->function
;
116 u_ent
.revision
= 0; /* Unused */
117 u_ent
.flags
= ent
->flags
;
118 u_ent
.group_id
= 0; /* Unused */
119 u_ent
.pads
= ent
->num_pads
;
120 u_ent
.links
= ent
->num_links
- ent
->num_backlinks
;
123 * Workaround for a bug at media-ctl <= v1.10 that makes it to
124 * do the wrong thing if the entity function doesn't belong to
125 * either MEDIA_ENT_F_OLD_BASE or MEDIA_ENT_F_OLD_SUBDEV_BASE
128 * Non-subdevices are expected to be at the MEDIA_ENT_F_OLD_BASE,
129 * or, otherwise, will be silently ignored by media-ctl when
130 * printing the graphviz diagram. So, map them into the devnode
133 if (ent
->function
< MEDIA_ENT_F_OLD_BASE
||
134 ent
->function
> MEDIA_ENT_T_DEVNODE_UNKNOWN
) {
135 if (is_media_entity_v4l2_subdev(ent
))
136 u_ent
.type
= MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN
;
137 else if (ent
->function
!= MEDIA_ENT_F_IO_V4L
)
138 u_ent
.type
= MEDIA_ENT_T_DEVNODE_UNKNOWN
;
141 memcpy(&u_ent
.raw
, &ent
->info
, sizeof(ent
->info
));
142 if (copy_to_user(uent
, &u_ent
, sizeof(u_ent
)))
147 static void media_device_kpad_to_upad(const struct media_pad
*kpad
,
148 struct media_pad_desc
*upad
)
150 upad
->entity
= media_entity_id(kpad
->entity
);
151 upad
->index
= kpad
->index
;
152 upad
->flags
= kpad
->flags
;
155 static long __media_device_enum_links(struct media_device
*mdev
,
156 struct media_links_enum
*links
)
158 struct media_entity
*entity
;
160 entity
= find_entity(mdev
, links
->entity
);
167 for (p
= 0; p
< entity
->num_pads
; p
++) {
168 struct media_pad_desc pad
;
170 memset(&pad
, 0, sizeof(pad
));
171 media_device_kpad_to_upad(&entity
->pads
[p
], &pad
);
172 if (copy_to_user(&links
->pads
[p
], &pad
, sizeof(pad
)))
178 struct media_link
*link
;
179 struct media_link_desc __user
*ulink_desc
= links
->links
;
181 list_for_each_entry(link
, &entity
->links
, list
) {
182 struct media_link_desc klink_desc
;
184 /* Ignore backlinks. */
185 if (link
->source
->entity
!= entity
)
187 memset(&klink_desc
, 0, sizeof(klink_desc
));
188 media_device_kpad_to_upad(link
->source
,
190 media_device_kpad_to_upad(link
->sink
,
192 klink_desc
.flags
= link
->flags
;
193 if (copy_to_user(ulink_desc
, &klink_desc
,
194 sizeof(*ulink_desc
)))
203 static long media_device_enum_links(struct media_device
*mdev
,
204 struct media_links_enum __user
*ulinks
)
206 struct media_links_enum links
;
209 if (copy_from_user(&links
, ulinks
, sizeof(links
)))
212 rval
= __media_device_enum_links(mdev
, &links
);
216 if (copy_to_user(ulinks
, &links
, sizeof(*ulinks
)))
222 static long media_device_setup_link(struct media_device
*mdev
,
223 struct media_link_desc __user
*_ulink
)
225 struct media_link
*link
= NULL
;
226 struct media_link_desc ulink
;
227 struct media_entity
*source
;
228 struct media_entity
*sink
;
231 if (copy_from_user(&ulink
, _ulink
, sizeof(ulink
)))
234 /* Find the source and sink entities and link.
236 source
= find_entity(mdev
, ulink
.source
.entity
);
237 sink
= find_entity(mdev
, ulink
.sink
.entity
);
239 if (source
== NULL
|| sink
== NULL
)
242 if (ulink
.source
.index
>= source
->num_pads
||
243 ulink
.sink
.index
>= sink
->num_pads
)
246 link
= media_entity_find_link(&source
->pads
[ulink
.source
.index
],
247 &sink
->pads
[ulink
.sink
.index
]);
251 /* Setup the link on both entities. */
252 ret
= __media_entity_setup_link(link
, ulink
.flags
);
254 if (copy_to_user(_ulink
, &ulink
, sizeof(ulink
)))
260 #if 0 /* Let's postpone it to Kernel 4.6 */
261 static long __media_device_get_topology(struct media_device
*mdev
,
262 struct media_v2_topology
*topo
)
264 struct media_entity
*entity
;
265 struct media_interface
*intf
;
266 struct media_pad
*pad
;
267 struct media_link
*link
;
268 struct media_v2_entity kentity
, *uentity
;
269 struct media_v2_interface kintf
, *uintf
;
270 struct media_v2_pad kpad
, *upad
;
271 struct media_v2_link klink
, *ulink
;
275 topo
->topology_version
= mdev
->topology_version
;
277 /* Get entities and number of entities */
279 uentity
= media_get_uptr(topo
->ptr_entities
);
280 media_device_for_each_entity(entity
, mdev
) {
285 if (i
> topo
->num_entities
) {
290 /* Copy fields to userspace struct if not error */
291 memset(&kentity
, 0, sizeof(kentity
));
292 kentity
.id
= entity
->graph_obj
.id
;
293 kentity
.function
= entity
->function
;
294 strncpy(kentity
.name
, entity
->name
,
295 sizeof(kentity
.name
));
297 if (copy_to_user(uentity
, &kentity
, sizeof(kentity
)))
301 topo
->num_entities
= i
;
303 /* Get interfaces and number of interfaces */
305 uintf
= media_get_uptr(topo
->ptr_interfaces
);
306 media_device_for_each_intf(intf
, mdev
) {
311 if (i
> topo
->num_interfaces
) {
316 memset(&kintf
, 0, sizeof(kintf
));
318 /* Copy intf fields to userspace struct */
319 kintf
.id
= intf
->graph_obj
.id
;
320 kintf
.intf_type
= intf
->type
;
321 kintf
.flags
= intf
->flags
;
323 if (media_type(&intf
->graph_obj
) == MEDIA_GRAPH_INTF_DEVNODE
) {
324 struct media_intf_devnode
*devnode
;
326 devnode
= intf_to_devnode(intf
);
328 kintf
.devnode
.major
= devnode
->major
;
329 kintf
.devnode
.minor
= devnode
->minor
;
332 if (copy_to_user(uintf
, &kintf
, sizeof(kintf
)))
336 topo
->num_interfaces
= i
;
338 /* Get pads and number of pads */
340 upad
= media_get_uptr(topo
->ptr_pads
);
341 media_device_for_each_pad(pad
, mdev
) {
346 if (i
> topo
->num_pads
) {
351 memset(&kpad
, 0, sizeof(kpad
));
353 /* Copy pad fields to userspace struct */
354 kpad
.id
= pad
->graph_obj
.id
;
355 kpad
.entity_id
= pad
->entity
->graph_obj
.id
;
356 kpad
.flags
= pad
->flags
;
358 if (copy_to_user(upad
, &kpad
, sizeof(kpad
)))
364 /* Get links and number of links */
366 ulink
= media_get_uptr(topo
->ptr_links
);
367 media_device_for_each_link(link
, mdev
) {
368 if (link
->is_backlink
)
376 if (i
> topo
->num_links
) {
381 memset(&klink
, 0, sizeof(klink
));
383 /* Copy link fields to userspace struct */
384 klink
.id
= link
->graph_obj
.id
;
385 klink
.source_id
= link
->gobj0
->id
;
386 klink
.sink_id
= link
->gobj1
->id
;
387 klink
.flags
= link
->flags
;
389 if (copy_to_user(ulink
, &klink
, sizeof(klink
)))
398 static long media_device_get_topology(struct media_device
*mdev
,
399 struct media_v2_topology __user
*utopo
)
401 struct media_v2_topology ktopo
;
404 if (copy_from_user(&ktopo
, utopo
, sizeof(ktopo
)))
407 ret
= __media_device_get_topology(mdev
, &ktopo
);
411 if (copy_to_user(utopo
, &ktopo
, sizeof(*utopo
)))
418 static long media_device_ioctl(struct file
*filp
, unsigned int cmd
,
421 struct media_devnode
*devnode
= media_devnode_data(filp
);
422 struct media_device
*dev
= to_media_device(devnode
);
426 case MEDIA_IOC_DEVICE_INFO
:
427 ret
= media_device_get_info(dev
,
428 (struct media_device_info __user
*)arg
);
431 case MEDIA_IOC_ENUM_ENTITIES
:
432 ret
= media_device_enum_entities(dev
,
433 (struct media_entity_desc __user
*)arg
);
436 case MEDIA_IOC_ENUM_LINKS
:
437 mutex_lock(&dev
->graph_mutex
);
438 ret
= media_device_enum_links(dev
,
439 (struct media_links_enum __user
*)arg
);
440 mutex_unlock(&dev
->graph_mutex
);
443 case MEDIA_IOC_SETUP_LINK
:
444 mutex_lock(&dev
->graph_mutex
);
445 ret
= media_device_setup_link(dev
,
446 (struct media_link_desc __user
*)arg
);
447 mutex_unlock(&dev
->graph_mutex
);
450 #if 0 /* Let's postpone it to Kernel 4.6 */
451 case MEDIA_IOC_G_TOPOLOGY
:
452 mutex_lock(&dev
->graph_mutex
);
453 ret
= media_device_get_topology(dev
,
454 (struct media_v2_topology __user
*)arg
);
455 mutex_unlock(&dev
->graph_mutex
);
467 struct media_links_enum32
{
469 compat_uptr_t pads
; /* struct media_pad_desc * */
470 compat_uptr_t links
; /* struct media_link_desc * */
474 static long media_device_enum_links32(struct media_device
*mdev
,
475 struct media_links_enum32 __user
*ulinks
)
477 struct media_links_enum links
;
478 compat_uptr_t pads_ptr
, links_ptr
;
480 memset(&links
, 0, sizeof(links
));
482 if (get_user(links
.entity
, &ulinks
->entity
)
483 || get_user(pads_ptr
, &ulinks
->pads
)
484 || get_user(links_ptr
, &ulinks
->links
))
487 links
.pads
= compat_ptr(pads_ptr
);
488 links
.links
= compat_ptr(links_ptr
);
490 return __media_device_enum_links(mdev
, &links
);
493 #define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
495 static long media_device_compat_ioctl(struct file
*filp
, unsigned int cmd
,
498 struct media_devnode
*devnode
= media_devnode_data(filp
);
499 struct media_device
*dev
= to_media_device(devnode
);
503 case MEDIA_IOC_DEVICE_INFO
:
504 case MEDIA_IOC_ENUM_ENTITIES
:
505 case MEDIA_IOC_SETUP_LINK
:
506 #if 0 /* Let's postpone it to Kernel 4.6 */
507 case MEDIA_IOC_G_TOPOLOGY
:
509 return media_device_ioctl(filp
, cmd
, arg
);
511 case MEDIA_IOC_ENUM_LINKS32
:
512 mutex_lock(&dev
->graph_mutex
);
513 ret
= media_device_enum_links32(dev
,
514 (struct media_links_enum32 __user
*)arg
);
515 mutex_unlock(&dev
->graph_mutex
);
524 #endif /* CONFIG_COMPAT */
526 static const struct media_file_operations media_device_fops
= {
527 .owner
= THIS_MODULE
,
528 .open
= media_device_open
,
529 .ioctl
= media_device_ioctl
,
531 .compat_ioctl
= media_device_compat_ioctl
,
532 #endif /* CONFIG_COMPAT */
533 .release
= media_device_close
,
536 /* -----------------------------------------------------------------------------
540 static ssize_t
show_model(struct device
*cd
,
541 struct device_attribute
*attr
, char *buf
)
543 struct media_device
*mdev
= to_media_device(to_media_devnode(cd
));
545 return sprintf(buf
, "%.*s\n", (int)sizeof(mdev
->model
), mdev
->model
);
548 static DEVICE_ATTR(model
, S_IRUGO
, show_model
, NULL
);
550 /* -----------------------------------------------------------------------------
551 * Registration/unregistration
554 static void media_device_release(struct media_devnode
*mdev
)
556 dev_dbg(mdev
->parent
, "Media device released\n");
560 * media_device_register_entity - Register an entity with a media device
561 * @mdev: The media device
562 * @entity: The entity
564 int __must_check
media_device_register_entity(struct media_device
*mdev
,
565 struct media_entity
*entity
)
570 if (entity
->function
== MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN
||
571 entity
->function
== MEDIA_ENT_F_UNKNOWN
)
573 "Entity type for entity %s was not initialized!\n",
576 /* Warn if we apparently re-register an entity */
577 WARN_ON(entity
->graph_obj
.mdev
!= NULL
);
578 entity
->graph_obj
.mdev
= mdev
;
579 INIT_LIST_HEAD(&entity
->links
);
580 entity
->num_links
= 0;
581 entity
->num_backlinks
= 0;
583 if (!ida_pre_get(&mdev
->entity_internal_idx
, GFP_KERNEL
))
586 spin_lock(&mdev
->lock
);
588 ret
= ida_get_new_above(&mdev
->entity_internal_idx
, 1,
589 &entity
->internal_idx
);
591 spin_unlock(&mdev
->lock
);
595 mdev
->entity_internal_idx_max
=
596 max(mdev
->entity_internal_idx_max
, entity
->internal_idx
);
598 /* Initialize media_gobj embedded at the entity */
599 media_gobj_create(mdev
, MEDIA_GRAPH_ENTITY
, &entity
->graph_obj
);
601 /* Initialize objects at the pads */
602 for (i
= 0; i
< entity
->num_pads
; i
++)
603 media_gobj_create(mdev
, MEDIA_GRAPH_PAD
,
604 &entity
->pads
[i
].graph_obj
);
606 spin_unlock(&mdev
->lock
);
610 EXPORT_SYMBOL_GPL(media_device_register_entity
);
612 static void __media_device_unregister_entity(struct media_entity
*entity
)
614 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
615 struct media_link
*link
, *tmp
;
616 struct media_interface
*intf
;
619 ida_simple_remove(&mdev
->entity_internal_idx
, entity
->internal_idx
);
621 /* Remove all interface links pointing to this entity */
622 list_for_each_entry(intf
, &mdev
->interfaces
, graph_obj
.list
) {
623 list_for_each_entry_safe(link
, tmp
, &intf
->links
, list
) {
624 if (link
->entity
== entity
)
625 __media_remove_intf_link(link
);
629 /* Remove all data links that belong to this entity */
630 __media_entity_remove_links(entity
);
632 /* Remove all pads that belong to this entity */
633 for (i
= 0; i
< entity
->num_pads
; i
++)
634 media_gobj_destroy(&entity
->pads
[i
].graph_obj
);
636 /* Remove the entity */
637 media_gobj_destroy(&entity
->graph_obj
);
639 entity
->graph_obj
.mdev
= NULL
;
642 void media_device_unregister_entity(struct media_entity
*entity
)
644 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
649 spin_lock(&mdev
->lock
);
650 __media_device_unregister_entity(entity
);
651 spin_unlock(&mdev
->lock
);
653 EXPORT_SYMBOL_GPL(media_device_unregister_entity
);
656 * media_device_init() - initialize a media device
657 * @mdev: The media device
659 * The caller is responsible for initializing the media device before
660 * registration. The following fields must be set:
662 * - dev must point to the parent device
663 * - model must be filled with the device model name
665 void media_device_init(struct media_device
*mdev
)
667 INIT_LIST_HEAD(&mdev
->entities
);
668 INIT_LIST_HEAD(&mdev
->interfaces
);
669 INIT_LIST_HEAD(&mdev
->pads
);
670 INIT_LIST_HEAD(&mdev
->links
);
671 spin_lock_init(&mdev
->lock
);
672 mutex_init(&mdev
->graph_mutex
);
673 ida_init(&mdev
->entity_internal_idx
);
675 dev_dbg(mdev
->dev
, "Media device initialized\n");
677 EXPORT_SYMBOL_GPL(media_device_init
);
679 void media_device_cleanup(struct media_device
*mdev
)
681 ida_destroy(&mdev
->entity_internal_idx
);
682 mdev
->entity_internal_idx_max
= 0;
683 mutex_destroy(&mdev
->graph_mutex
);
685 EXPORT_SYMBOL_GPL(media_device_cleanup
);
687 int __must_check
__media_device_register(struct media_device
*mdev
,
688 struct module
*owner
)
692 /* Register the device node. */
693 mdev
->devnode
.fops
= &media_device_fops
;
694 mdev
->devnode
.parent
= mdev
->dev
;
695 mdev
->devnode
.release
= media_device_release
;
697 /* Set version 0 to indicate user-space that the graph is static */
698 mdev
->topology_version
= 0;
700 ret
= media_devnode_register(&mdev
->devnode
, owner
);
704 ret
= device_create_file(&mdev
->devnode
.dev
, &dev_attr_model
);
706 media_devnode_unregister(&mdev
->devnode
);
710 dev_dbg(mdev
->dev
, "Media device registered\n");
714 EXPORT_SYMBOL_GPL(__media_device_register
);
716 void media_device_unregister(struct media_device
*mdev
)
718 struct media_entity
*entity
;
719 struct media_entity
*next
;
720 struct media_interface
*intf
, *tmp_intf
;
725 spin_lock(&mdev
->lock
);
727 /* Check if mdev was ever registered at all */
728 if (!media_devnode_is_registered(&mdev
->devnode
)) {
729 spin_unlock(&mdev
->lock
);
733 /* Remove all entities from the media device */
734 list_for_each_entry_safe(entity
, next
, &mdev
->entities
, graph_obj
.list
)
735 __media_device_unregister_entity(entity
);
737 /* Remove all interfaces from the media device */
738 list_for_each_entry_safe(intf
, tmp_intf
, &mdev
->interfaces
,
740 __media_remove_intf_links(intf
);
741 media_gobj_destroy(&intf
->graph_obj
);
745 spin_unlock(&mdev
->lock
);
747 device_remove_file(&mdev
->devnode
.dev
, &dev_attr_model
);
748 media_devnode_unregister(&mdev
->devnode
);
750 dev_dbg(mdev
->dev
, "Media device unregistered\n");
752 EXPORT_SYMBOL_GPL(media_device_unregister
);
754 static void media_device_release_devres(struct device
*dev
, void *res
)
758 struct media_device
*media_device_get_devres(struct device
*dev
)
760 struct media_device
*mdev
;
762 mdev
= devres_find(dev
, media_device_release_devres
, NULL
, NULL
);
766 mdev
= devres_alloc(media_device_release_devres
,
767 sizeof(struct media_device
), GFP_KERNEL
);
770 return devres_get(dev
, mdev
, NULL
, NULL
);
772 EXPORT_SYMBOL_GPL(media_device_get_devres
);
774 struct media_device
*media_device_find_devres(struct device
*dev
)
776 return devres_find(dev
, media_device_release_devres
, NULL
, NULL
);
778 EXPORT_SYMBOL_GPL(media_device_find_devres
);
780 #endif /* CONFIG_MEDIA_CONTROLLER */