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 #include <linux/compat.h>
24 #include <linux/export.h>
25 #include <linux/idr.h>
26 #include <linux/ioctl.h>
27 #include <linux/media.h>
28 #include <linux/slab.h>
29 #include <linux/types.h>
30 #include <linux/pci.h>
31 #include <linux/usb.h>
33 #include <media/media-device.h>
34 #include <media/media-devnode.h>
35 #include <media/media-entity.h>
37 #ifdef CONFIG_MEDIA_CONTROLLER
39 /* -----------------------------------------------------------------------------
43 static int media_device_open(struct file
*filp
)
48 static int media_device_close(struct file
*filp
)
53 static int media_device_get_info(struct media_device
*dev
,
54 struct media_device_info __user
*__info
)
56 struct media_device_info info
;
58 memset(&info
, 0, sizeof(info
));
60 if (dev
->driver_name
[0])
61 strlcpy(info
.driver
, dev
->driver_name
, sizeof(info
.driver
));
63 strlcpy(info
.driver
, dev
->dev
->driver
->name
, sizeof(info
.driver
));
65 strlcpy(info
.model
, dev
->model
, sizeof(info
.model
));
66 strlcpy(info
.serial
, dev
->serial
, sizeof(info
.serial
));
67 strlcpy(info
.bus_info
, dev
->bus_info
, sizeof(info
.bus_info
));
69 info
.media_version
= MEDIA_API_VERSION
;
70 info
.hw_revision
= dev
->hw_revision
;
71 info
.driver_version
= dev
->driver_version
;
73 if (copy_to_user(__info
, &info
, sizeof(*__info
)))
78 static struct media_entity
*find_entity(struct media_device
*mdev
, u32 id
)
80 struct media_entity
*entity
;
81 int next
= id
& MEDIA_ENT_ID_FLAG_NEXT
;
83 id
&= ~MEDIA_ENT_ID_FLAG_NEXT
;
85 spin_lock(&mdev
->lock
);
87 media_device_for_each_entity(entity
, mdev
) {
88 if (((media_entity_id(entity
) == id
) && !next
) ||
89 ((media_entity_id(entity
) > id
) && next
)) {
90 spin_unlock(&mdev
->lock
);
95 spin_unlock(&mdev
->lock
);
100 static long media_device_enum_entities(struct media_device
*mdev
,
101 struct media_entity_desc __user
*uent
)
103 struct media_entity
*ent
;
104 struct media_entity_desc u_ent
;
106 memset(&u_ent
, 0, sizeof(u_ent
));
107 if (copy_from_user(&u_ent
.id
, &uent
->id
, sizeof(u_ent
.id
)))
110 ent
= find_entity(mdev
, u_ent
.id
);
115 u_ent
.id
= media_entity_id(ent
);
117 strlcpy(u_ent
.name
, ent
->name
, sizeof(u_ent
.name
));
118 u_ent
.type
= ent
->function
;
119 u_ent
.revision
= 0; /* Unused */
120 u_ent
.flags
= ent
->flags
;
121 u_ent
.group_id
= 0; /* Unused */
122 u_ent
.pads
= ent
->num_pads
;
123 u_ent
.links
= ent
->num_links
- ent
->num_backlinks
;
124 memcpy(&u_ent
.raw
, &ent
->info
, sizeof(ent
->info
));
125 if (copy_to_user(uent
, &u_ent
, sizeof(u_ent
)))
130 static void media_device_kpad_to_upad(const struct media_pad
*kpad
,
131 struct media_pad_desc
*upad
)
133 upad
->entity
= media_entity_id(kpad
->entity
);
134 upad
->index
= kpad
->index
;
135 upad
->flags
= kpad
->flags
;
138 static long __media_device_enum_links(struct media_device
*mdev
,
139 struct media_links_enum
*links
)
141 struct media_entity
*entity
;
143 entity
= find_entity(mdev
, links
->entity
);
150 for (p
= 0; p
< entity
->num_pads
; p
++) {
151 struct media_pad_desc pad
;
153 memset(&pad
, 0, sizeof(pad
));
154 media_device_kpad_to_upad(&entity
->pads
[p
], &pad
);
155 if (copy_to_user(&links
->pads
[p
], &pad
, sizeof(pad
)))
161 struct media_link
*link
;
162 struct media_link_desc __user
*ulink_desc
= links
->links
;
164 list_for_each_entry(link
, &entity
->links
, list
) {
165 struct media_link_desc klink_desc
;
167 /* Ignore backlinks. */
168 if (link
->source
->entity
!= entity
)
170 memset(&klink_desc
, 0, sizeof(klink_desc
));
171 media_device_kpad_to_upad(link
->source
,
173 media_device_kpad_to_upad(link
->sink
,
175 klink_desc
.flags
= link
->flags
;
176 if (copy_to_user(ulink_desc
, &klink_desc
,
177 sizeof(*ulink_desc
)))
186 static long media_device_enum_links(struct media_device
*mdev
,
187 struct media_links_enum __user
*ulinks
)
189 struct media_links_enum links
;
192 if (copy_from_user(&links
, ulinks
, sizeof(links
)))
195 rval
= __media_device_enum_links(mdev
, &links
);
199 if (copy_to_user(ulinks
, &links
, sizeof(*ulinks
)))
205 static long media_device_setup_link(struct media_device
*mdev
,
206 struct media_link_desc __user
*_ulink
)
208 struct media_link
*link
= NULL
;
209 struct media_link_desc ulink
;
210 struct media_entity
*source
;
211 struct media_entity
*sink
;
214 if (copy_from_user(&ulink
, _ulink
, sizeof(ulink
)))
217 /* Find the source and sink entities and link.
219 source
= find_entity(mdev
, ulink
.source
.entity
);
220 sink
= find_entity(mdev
, ulink
.sink
.entity
);
222 if (source
== NULL
|| sink
== NULL
)
225 if (ulink
.source
.index
>= source
->num_pads
||
226 ulink
.sink
.index
>= sink
->num_pads
)
229 link
= media_entity_find_link(&source
->pads
[ulink
.source
.index
],
230 &sink
->pads
[ulink
.sink
.index
]);
234 /* Setup the link on both entities. */
235 ret
= __media_entity_setup_link(link
, ulink
.flags
);
237 if (copy_to_user(_ulink
, &ulink
, sizeof(ulink
)))
243 static long __media_device_get_topology(struct media_device
*mdev
,
244 struct media_v2_topology
*topo
)
246 struct media_entity
*entity
;
247 struct media_interface
*intf
;
248 struct media_pad
*pad
;
249 struct media_link
*link
;
250 struct media_v2_entity kentity
, *uentity
;
251 struct media_v2_interface kintf
, *uintf
;
252 struct media_v2_pad kpad
, *upad
;
253 struct media_v2_link klink
, *ulink
;
257 topo
->topology_version
= mdev
->topology_version
;
259 /* Get entities and number of entities */
261 uentity
= media_get_uptr(topo
->ptr_entities
);
262 media_device_for_each_entity(entity
, mdev
) {
267 if (i
> topo
->num_entities
) {
272 /* Copy fields to userspace struct if not error */
273 memset(&kentity
, 0, sizeof(kentity
));
274 kentity
.id
= entity
->graph_obj
.id
;
275 kentity
.function
= entity
->function
;
276 strncpy(kentity
.name
, entity
->name
,
277 sizeof(kentity
.name
));
279 if (copy_to_user(uentity
, &kentity
, sizeof(kentity
)))
283 topo
->num_entities
= i
;
285 /* Get interfaces and number of interfaces */
287 uintf
= media_get_uptr(topo
->ptr_interfaces
);
288 media_device_for_each_intf(intf
, mdev
) {
293 if (i
> topo
->num_interfaces
) {
298 memset(&kintf
, 0, sizeof(kintf
));
300 /* Copy intf fields to userspace struct */
301 kintf
.id
= intf
->graph_obj
.id
;
302 kintf
.intf_type
= intf
->type
;
303 kintf
.flags
= intf
->flags
;
305 if (media_type(&intf
->graph_obj
) == MEDIA_GRAPH_INTF_DEVNODE
) {
306 struct media_intf_devnode
*devnode
;
308 devnode
= intf_to_devnode(intf
);
310 kintf
.devnode
.major
= devnode
->major
;
311 kintf
.devnode
.minor
= devnode
->minor
;
314 if (copy_to_user(uintf
, &kintf
, sizeof(kintf
)))
318 topo
->num_interfaces
= i
;
320 /* Get pads and number of pads */
322 upad
= media_get_uptr(topo
->ptr_pads
);
323 media_device_for_each_pad(pad
, mdev
) {
328 if (i
> topo
->num_pads
) {
333 memset(&kpad
, 0, sizeof(kpad
));
335 /* Copy pad fields to userspace struct */
336 kpad
.id
= pad
->graph_obj
.id
;
337 kpad
.entity_id
= pad
->entity
->graph_obj
.id
;
338 kpad
.flags
= pad
->flags
;
340 if (copy_to_user(upad
, &kpad
, sizeof(kpad
)))
346 /* Get links and number of links */
348 ulink
= media_get_uptr(topo
->ptr_links
);
349 media_device_for_each_link(link
, mdev
) {
350 if (link
->is_backlink
)
358 if (i
> topo
->num_links
) {
363 memset(&klink
, 0, sizeof(klink
));
365 /* Copy link fields to userspace struct */
366 klink
.id
= link
->graph_obj
.id
;
367 klink
.source_id
= link
->gobj0
->id
;
368 klink
.sink_id
= link
->gobj1
->id
;
369 klink
.flags
= link
->flags
;
371 if (copy_to_user(ulink
, &klink
, sizeof(klink
)))
380 static long media_device_get_topology(struct media_device
*mdev
,
381 struct media_v2_topology __user
*utopo
)
383 struct media_v2_topology ktopo
;
386 if (copy_from_user(&ktopo
, utopo
, sizeof(ktopo
)))
389 ret
= __media_device_get_topology(mdev
, &ktopo
);
393 if (copy_to_user(utopo
, &ktopo
, sizeof(*utopo
)))
399 static long media_device_ioctl(struct file
*filp
, unsigned int cmd
,
402 struct media_devnode
*devnode
= media_devnode_data(filp
);
403 struct media_device
*dev
= to_media_device(devnode
);
407 case MEDIA_IOC_DEVICE_INFO
:
408 ret
= media_device_get_info(dev
,
409 (struct media_device_info __user
*)arg
);
412 case MEDIA_IOC_ENUM_ENTITIES
:
413 ret
= media_device_enum_entities(dev
,
414 (struct media_entity_desc __user
*)arg
);
417 case MEDIA_IOC_ENUM_LINKS
:
418 mutex_lock(&dev
->graph_mutex
);
419 ret
= media_device_enum_links(dev
,
420 (struct media_links_enum __user
*)arg
);
421 mutex_unlock(&dev
->graph_mutex
);
424 case MEDIA_IOC_SETUP_LINK
:
425 mutex_lock(&dev
->graph_mutex
);
426 ret
= media_device_setup_link(dev
,
427 (struct media_link_desc __user
*)arg
);
428 mutex_unlock(&dev
->graph_mutex
);
431 case MEDIA_IOC_G_TOPOLOGY
:
432 mutex_lock(&dev
->graph_mutex
);
433 ret
= media_device_get_topology(dev
,
434 (struct media_v2_topology __user
*)arg
);
435 mutex_unlock(&dev
->graph_mutex
);
447 struct media_links_enum32
{
449 compat_uptr_t pads
; /* struct media_pad_desc * */
450 compat_uptr_t links
; /* struct media_link_desc * */
454 static long media_device_enum_links32(struct media_device
*mdev
,
455 struct media_links_enum32 __user
*ulinks
)
457 struct media_links_enum links
;
458 compat_uptr_t pads_ptr
, links_ptr
;
460 memset(&links
, 0, sizeof(links
));
462 if (get_user(links
.entity
, &ulinks
->entity
)
463 || get_user(pads_ptr
, &ulinks
->pads
)
464 || get_user(links_ptr
, &ulinks
->links
))
467 links
.pads
= compat_ptr(pads_ptr
);
468 links
.links
= compat_ptr(links_ptr
);
470 return __media_device_enum_links(mdev
, &links
);
473 #define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
475 static long media_device_compat_ioctl(struct file
*filp
, unsigned int cmd
,
478 struct media_devnode
*devnode
= media_devnode_data(filp
);
479 struct media_device
*dev
= to_media_device(devnode
);
483 case MEDIA_IOC_DEVICE_INFO
:
484 case MEDIA_IOC_ENUM_ENTITIES
:
485 case MEDIA_IOC_SETUP_LINK
:
486 case MEDIA_IOC_G_TOPOLOGY
:
487 return media_device_ioctl(filp
, cmd
, arg
);
489 case MEDIA_IOC_ENUM_LINKS32
:
490 mutex_lock(&dev
->graph_mutex
);
491 ret
= media_device_enum_links32(dev
,
492 (struct media_links_enum32 __user
*)arg
);
493 mutex_unlock(&dev
->graph_mutex
);
502 #endif /* CONFIG_COMPAT */
504 static const struct media_file_operations media_device_fops
= {
505 .owner
= THIS_MODULE
,
506 .open
= media_device_open
,
507 .ioctl
= media_device_ioctl
,
509 .compat_ioctl
= media_device_compat_ioctl
,
510 #endif /* CONFIG_COMPAT */
511 .release
= media_device_close
,
514 /* -----------------------------------------------------------------------------
518 static ssize_t
show_model(struct device
*cd
,
519 struct device_attribute
*attr
, char *buf
)
521 struct media_device
*mdev
= to_media_device(to_media_devnode(cd
));
523 return sprintf(buf
, "%.*s\n", (int)sizeof(mdev
->model
), mdev
->model
);
526 static DEVICE_ATTR(model
, S_IRUGO
, show_model
, NULL
);
528 /* -----------------------------------------------------------------------------
529 * Registration/unregistration
532 static void media_device_release(struct media_devnode
*mdev
)
534 dev_dbg(mdev
->parent
, "Media device released\n");
538 * media_device_register_entity - Register an entity with a media device
539 * @mdev: The media device
540 * @entity: The entity
542 int __must_check
media_device_register_entity(struct media_device
*mdev
,
543 struct media_entity
*entity
)
548 if (entity
->function
== MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN
||
549 entity
->function
== MEDIA_ENT_F_UNKNOWN
)
551 "Entity type for entity %s was not initialized!\n",
554 /* Warn if we apparently re-register an entity */
555 WARN_ON(entity
->graph_obj
.mdev
!= NULL
);
556 entity
->graph_obj
.mdev
= mdev
;
557 INIT_LIST_HEAD(&entity
->links
);
558 entity
->num_links
= 0;
559 entity
->num_backlinks
= 0;
561 if (!ida_pre_get(&mdev
->entity_internal_idx
, GFP_KERNEL
))
564 spin_lock(&mdev
->lock
);
566 ret
= ida_get_new_above(&mdev
->entity_internal_idx
, 1,
567 &entity
->internal_idx
);
569 spin_unlock(&mdev
->lock
);
573 mdev
->entity_internal_idx_max
=
574 max(mdev
->entity_internal_idx_max
, entity
->internal_idx
);
576 /* Initialize media_gobj embedded at the entity */
577 media_gobj_create(mdev
, MEDIA_GRAPH_ENTITY
, &entity
->graph_obj
);
579 /* Initialize objects at the pads */
580 for (i
= 0; i
< entity
->num_pads
; i
++)
581 media_gobj_create(mdev
, MEDIA_GRAPH_PAD
,
582 &entity
->pads
[i
].graph_obj
);
584 spin_unlock(&mdev
->lock
);
588 EXPORT_SYMBOL_GPL(media_device_register_entity
);
590 static void __media_device_unregister_entity(struct media_entity
*entity
)
592 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
593 struct media_link
*link
, *tmp
;
594 struct media_interface
*intf
;
597 ida_simple_remove(&mdev
->entity_internal_idx
, entity
->internal_idx
);
599 /* Remove all interface links pointing to this entity */
600 list_for_each_entry(intf
, &mdev
->interfaces
, graph_obj
.list
) {
601 list_for_each_entry_safe(link
, tmp
, &intf
->links
, list
) {
602 if (link
->entity
== entity
)
603 __media_remove_intf_link(link
);
607 /* Remove all data links that belong to this entity */
608 __media_entity_remove_links(entity
);
610 /* Remove all pads that belong to this entity */
611 for (i
= 0; i
< entity
->num_pads
; i
++)
612 media_gobj_destroy(&entity
->pads
[i
].graph_obj
);
614 /* Remove the entity */
615 media_gobj_destroy(&entity
->graph_obj
);
617 entity
->graph_obj
.mdev
= NULL
;
620 void media_device_unregister_entity(struct media_entity
*entity
)
622 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
627 spin_lock(&mdev
->lock
);
628 __media_device_unregister_entity(entity
);
629 spin_unlock(&mdev
->lock
);
631 EXPORT_SYMBOL_GPL(media_device_unregister_entity
);
634 * media_device_init() - initialize a media device
635 * @mdev: The media device
637 * The caller is responsible for initializing the media device before
638 * registration. The following fields must be set:
640 * - dev must point to the parent device
641 * - model must be filled with the device model name
643 void media_device_init(struct media_device
*mdev
)
645 INIT_LIST_HEAD(&mdev
->entities
);
646 INIT_LIST_HEAD(&mdev
->interfaces
);
647 INIT_LIST_HEAD(&mdev
->pads
);
648 INIT_LIST_HEAD(&mdev
->links
);
649 spin_lock_init(&mdev
->lock
);
650 mutex_init(&mdev
->graph_mutex
);
651 ida_init(&mdev
->entity_internal_idx
);
653 dev_dbg(mdev
->dev
, "Media device initialized\n");
655 EXPORT_SYMBOL_GPL(media_device_init
);
657 void media_device_cleanup(struct media_device
*mdev
)
659 ida_destroy(&mdev
->entity_internal_idx
);
660 mdev
->entity_internal_idx_max
= 0;
661 mutex_destroy(&mdev
->graph_mutex
);
663 EXPORT_SYMBOL_GPL(media_device_cleanup
);
665 int __must_check
__media_device_register(struct media_device
*mdev
,
666 struct module
*owner
)
670 /* Register the device node. */
671 mdev
->devnode
.fops
= &media_device_fops
;
672 mdev
->devnode
.parent
= mdev
->dev
;
673 mdev
->devnode
.release
= media_device_release
;
675 /* Set version 0 to indicate user-space that the graph is static */
676 mdev
->topology_version
= 0;
678 ret
= media_devnode_register(&mdev
->devnode
, owner
);
682 ret
= device_create_file(&mdev
->devnode
.dev
, &dev_attr_model
);
684 media_devnode_unregister(&mdev
->devnode
);
688 dev_dbg(mdev
->dev
, "Media device registered\n");
692 EXPORT_SYMBOL_GPL(__media_device_register
);
694 void media_device_unregister(struct media_device
*mdev
)
696 struct media_entity
*entity
;
697 struct media_entity
*next
;
698 struct media_interface
*intf
, *tmp_intf
;
703 spin_lock(&mdev
->lock
);
705 /* Check if mdev was ever registered at all */
706 if (!media_devnode_is_registered(&mdev
->devnode
)) {
707 spin_unlock(&mdev
->lock
);
711 /* Remove all entities from the media device */
712 list_for_each_entry_safe(entity
, next
, &mdev
->entities
, graph_obj
.list
)
713 __media_device_unregister_entity(entity
);
715 /* Remove all interfaces from the media device */
716 list_for_each_entry_safe(intf
, tmp_intf
, &mdev
->interfaces
,
718 __media_remove_intf_links(intf
);
719 media_gobj_destroy(&intf
->graph_obj
);
723 spin_unlock(&mdev
->lock
);
725 device_remove_file(&mdev
->devnode
.dev
, &dev_attr_model
);
726 media_devnode_unregister(&mdev
->devnode
);
728 dev_dbg(mdev
->dev
, "Media device unregistered\n");
730 EXPORT_SYMBOL_GPL(media_device_unregister
);
732 static void media_device_release_devres(struct device
*dev
, void *res
)
736 struct media_device
*media_device_get_devres(struct device
*dev
)
738 struct media_device
*mdev
;
740 mdev
= devres_find(dev
, media_device_release_devres
, NULL
, NULL
);
744 mdev
= devres_alloc(media_device_release_devres
,
745 sizeof(struct media_device
), GFP_KERNEL
);
748 return devres_get(dev
, mdev
, NULL
, NULL
);
750 EXPORT_SYMBOL_GPL(media_device_get_devres
);
752 struct media_device
*media_device_find_devres(struct device
*dev
)
754 return devres_find(dev
, media_device_release_devres
, NULL
, NULL
);
756 EXPORT_SYMBOL_GPL(media_device_find_devres
);
758 void media_device_pci_init(struct media_device
*mdev
,
759 struct pci_dev
*pci_dev
,
763 mdev
->dev
= &pci_dev
->dev
;
766 strlcpy(mdev
->model
, name
, sizeof(mdev
->model
));
768 strlcpy(mdev
->model
, pci_name(pci_dev
), sizeof(mdev
->model
));
770 sprintf(mdev
->bus_info
, "PCI:%s", pci_name(pci_dev
));
772 mdev
->hw_revision
= (pci_dev
->subsystem_vendor
<< 16)
773 | pci_dev
->subsystem_device
;
775 mdev
->driver_version
= LINUX_VERSION_CODE
;
777 media_device_init(mdev
);
780 EXPORT_SYMBOL_GPL(media_device_pci_init
);
782 void __media_device_usb_init(struct media_device
*mdev
,
783 struct usb_device
*udev
,
784 const char *board_name
,
785 const char *driver_name
)
788 mdev
->dev
= &udev
->dev
;
791 strlcpy(mdev
->driver_name
, driver_name
,
792 sizeof(mdev
->driver_name
));
795 strlcpy(mdev
->model
, board_name
, sizeof(mdev
->model
));
796 else if (udev
->product
)
797 strlcpy(mdev
->model
, udev
->product
, sizeof(mdev
->model
));
799 strlcpy(mdev
->model
, "unknown model", sizeof(mdev
->model
));
801 strlcpy(mdev
->serial
, udev
->serial
, sizeof(mdev
->serial
));
802 usb_make_path(udev
, mdev
->bus_info
, sizeof(mdev
->bus_info
));
803 mdev
->hw_revision
= le16_to_cpu(udev
->descriptor
.bcdDevice
);
804 mdev
->driver_version
= LINUX_VERSION_CODE
;
806 media_device_init(mdev
);
809 EXPORT_SYMBOL_GPL(__media_device_usb_init
);
812 #endif /* CONFIG_MEDIA_CONTROLLER */