Skip to content

Commit be2fff6

Browse files
ezequielgarciamchehab
authored andcommitted
media: add helpers for memory-to-memory media controller
A memory-to-memory pipeline device consists in three entities: two DMA engine and one video processing entities. The DMA engine entities are linked to a V4L interface. This commit add a new v4l2_m2m_{un}register_media_controller API to register this topology. For instance, a typical mem2mem device topology would look like this: Device topology - entity 1: source (1 pad, 1 link) type Node subtype V4L flags 0 pad0: Source -> "proc":1 [ENABLED,IMMUTABLE] - entity 3: proc (2 pads, 2 links) type Node subtype Unknown flags 0 pad0: Source -> "sink":0 [ENABLED,IMMUTABLE] pad1: Sink <- "source":0 [ENABLED,IMMUTABLE] - entity 6: sink (1 pad, 1 link) type Node subtype V4L flags 0 pad0: Sink <- "proc":0 [ENABLED,IMMUTABLE] [hans.verkuil@cisco.com: mark interface links as IMMUTABLE] Suggested-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Ezequiel Garcia <ezequiel@collabora.com> Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com> Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
1 parent 4d1e454 commit be2fff6

3 files changed

Lines changed: 217 additions & 5 deletions

File tree

‎drivers/media/v4l2-core/v4l2-dev.c‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ static void v4l2_device_release(struct device *cd)
202202
mutex_unlock(&videodev_lock);
203203

204204
#if defined(CONFIG_MEDIA_CONTROLLER)
205-
if (v4l2_dev->mdev) {
205+
if (v4l2_dev->mdev && vdev->vfl_dir != VFL_DIR_M2M) {
206206
/* Remove interfaces and interface links */
207207
media_devnode_remove(vdev->intf_devnode);
208208
if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN)
@@ -733,19 +733,22 @@ static void determine_valid_ioctls(struct video_device *vdev)
733733
BASE_VIDIOC_PRIVATE);
734734
}
735735

736-
static int video_register_media_controller(struct video_device *vdev, int type)
736+
static int video_register_media_controller(struct video_device *vdev)
737737
{
738738
#if defined(CONFIG_MEDIA_CONTROLLER)
739739
u32 intf_type;
740740
int ret;
741741

742-
if (!vdev->v4l2_dev->mdev)
742+
/* Memory-to-memory devices are more complex and use
743+
* their own function to register its mc entities.
744+
*/
745+
if (!vdev->v4l2_dev->mdev || vdev->vfl_dir == VFL_DIR_M2M)
743746
return 0;
744747

745748
vdev->entity.obj_type = MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
746749
vdev->entity.function = MEDIA_ENT_F_UNKNOWN;
747750

748-
switch (type) {
751+
switch (vdev->vfl_type) {
749752
case VFL_TYPE_GRABBER:
750753
intf_type = MEDIA_INTF_T_V4L_VIDEO;
751754
vdev->entity.function = MEDIA_ENT_F_IO_V4L;
@@ -994,7 +997,7 @@ int __video_register_device(struct video_device *vdev,
994997
v4l2_device_get(vdev->v4l2_dev);
995998

996999
/* Part 5: Register the entity. */
997-
ret = video_register_media_controller(vdev, type);
1000+
ret = video_register_media_controller(vdev);
9981001

9991002
/* Part 6: Activate this minor. The char device can now be used. */
10001003
set_bit(V4L2_FL_REGISTERED, &vdev->flags);

‎drivers/media/v4l2-core/v4l2-mem2mem.c‎

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
#include <linux/sched.h>
1818
#include <linux/slab.h>
1919

20+
#include <media/media-device.h>
2021
#include <media/videobuf2-v4l2.h>
2122
#include <media/v4l2-mem2mem.h>
2223
#include <media/v4l2-dev.h>
24+
#include <media/v4l2-device.h>
2325
#include <media/v4l2-fh.h>
2426
#include <media/v4l2-event.h>
2527

@@ -50,6 +52,17 @@ module_param(debug, bool, 0644);
5052
* offsets but for different queues */
5153
#define DST_QUEUE_OFF_BASE (1 << 30)
5254

55+
enum v4l2_m2m_entity_type {
56+
MEM2MEM_ENT_TYPE_SOURCE,
57+
MEM2MEM_ENT_TYPE_SINK,
58+
MEM2MEM_ENT_TYPE_PROC
59+
};
60+
61+
static const char * const m2m_entity_name[] = {
62+
"source",
63+
"sink",
64+
"proc"
65+
};
5366

5467
/**
5568
* struct v4l2_m2m_dev - per-device context
@@ -60,6 +73,15 @@ module_param(debug, bool, 0644);
6073
*/
6174
struct v4l2_m2m_dev {
6275
struct v4l2_m2m_ctx *curr_ctx;
76+
#ifdef CONFIG_MEDIA_CONTROLLER
77+
struct media_entity *source;
78+
struct media_pad source_pad;
79+
struct media_entity sink;
80+
struct media_pad sink_pad;
81+
struct media_entity proc;
82+
struct media_pad proc_pads[2];
83+
struct media_intf_devnode *intf_devnode;
84+
#endif
6385

6486
struct list_head job_queue;
6587
spinlock_t job_spinlock;
@@ -594,6 +616,174 @@ int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
594616
}
595617
EXPORT_SYMBOL(v4l2_m2m_mmap);
596618

619+
#if defined(CONFIG_MEDIA_CONTROLLER)
620+
void v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev *m2m_dev)
621+
{
622+
media_remove_intf_links(&m2m_dev->intf_devnode->intf);
623+
media_devnode_remove(m2m_dev->intf_devnode);
624+
625+
media_entity_remove_links(m2m_dev->source);
626+
media_entity_remove_links(&m2m_dev->sink);
627+
media_entity_remove_links(&m2m_dev->proc);
628+
media_device_unregister_entity(m2m_dev->source);
629+
media_device_unregister_entity(&m2m_dev->sink);
630+
media_device_unregister_entity(&m2m_dev->proc);
631+
kfree(m2m_dev->source->name);
632+
kfree(m2m_dev->sink.name);
633+
kfree(m2m_dev->proc.name);
634+
}
635+
EXPORT_SYMBOL_GPL(v4l2_m2m_unregister_media_controller);
636+
637+
static int v4l2_m2m_register_entity(struct media_device *mdev,
638+
struct v4l2_m2m_dev *m2m_dev, enum v4l2_m2m_entity_type type,
639+
struct video_device *vdev, int function)
640+
{
641+
struct media_entity *entity;
642+
struct media_pad *pads;
643+
char *name;
644+
unsigned int len;
645+
int num_pads;
646+
int ret;
647+
648+
switch (type) {
649+
case MEM2MEM_ENT_TYPE_SOURCE:
650+
entity = m2m_dev->source;
651+
pads = &m2m_dev->source_pad;
652+
pads[0].flags = MEDIA_PAD_FL_SOURCE;
653+
num_pads = 1;
654+
break;
655+
case MEM2MEM_ENT_TYPE_SINK:
656+
entity = &m2m_dev->sink;
657+
pads = &m2m_dev->sink_pad;
658+
pads[0].flags = MEDIA_PAD_FL_SINK;
659+
num_pads = 1;
660+
break;
661+
case MEM2MEM_ENT_TYPE_PROC:
662+
entity = &m2m_dev->proc;
663+
pads = m2m_dev->proc_pads;
664+
pads[0].flags = MEDIA_PAD_FL_SINK;
665+
pads[1].flags = MEDIA_PAD_FL_SOURCE;
666+
num_pads = 2;
667+
break;
668+
default:
669+
return -EINVAL;
670+
}
671+
672+
entity->obj_type = MEDIA_ENTITY_TYPE_BASE;
673+
if (type != MEM2MEM_ENT_TYPE_PROC) {
674+
entity->info.dev.major = VIDEO_MAJOR;
675+
entity->info.dev.minor = vdev->minor;
676+
}
677+
len = strlen(vdev->name) + 2 + strlen(m2m_entity_name[type]);
678+
name = kmalloc(len, GFP_KERNEL);
679+
if (!name)
680+
return -ENOMEM;
681+
snprintf(name, len, "%s-%s", vdev->name, m2m_entity_name[type]);
682+
entity->name = name;
683+
entity->function = function;
684+
685+
ret = media_entity_pads_init(entity, num_pads, pads);
686+
if (ret)
687+
return ret;
688+
ret = media_device_register_entity(mdev, entity);
689+
if (ret)
690+
return ret;
691+
692+
return 0;
693+
}
694+
695+
int v4l2_m2m_register_media_controller(struct v4l2_m2m_dev *m2m_dev,
696+
struct video_device *vdev, int function)
697+
{
698+
struct media_device *mdev = vdev->v4l2_dev->mdev;
699+
struct media_link *link;
700+
int ret;
701+
702+
if (!mdev)
703+
return 0;
704+
705+
/* A memory-to-memory device consists in two
706+
* DMA engine and one video processing entities.
707+
* The DMA engine entities are linked to a V4L interface
708+
*/
709+
710+
/* Create the three entities with their pads */
711+
m2m_dev->source = &vdev->entity;
712+
ret = v4l2_m2m_register_entity(mdev, m2m_dev,
713+
MEM2MEM_ENT_TYPE_SOURCE, vdev, MEDIA_ENT_F_IO_V4L);
714+
if (ret)
715+
return ret;
716+
ret = v4l2_m2m_register_entity(mdev, m2m_dev,
717+
MEM2MEM_ENT_TYPE_PROC, vdev, function);
718+
if (ret)
719+
goto err_rel_entity0;
720+
ret = v4l2_m2m_register_entity(mdev, m2m_dev,
721+
MEM2MEM_ENT_TYPE_SINK, vdev, MEDIA_ENT_F_IO_V4L);
722+
if (ret)
723+
goto err_rel_entity1;
724+
725+
/* Connect the three entities */
726+
ret = media_create_pad_link(m2m_dev->source, 0, &m2m_dev->proc, 1,
727+
MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
728+
if (ret)
729+
goto err_rel_entity2;
730+
731+
ret = media_create_pad_link(&m2m_dev->proc, 0, &m2m_dev->sink, 0,
732+
MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
733+
if (ret)
734+
goto err_rm_links0;
735+
736+
/* Create video interface */
737+
m2m_dev->intf_devnode = media_devnode_create(mdev,
738+
MEDIA_INTF_T_V4L_VIDEO, 0,
739+
VIDEO_MAJOR, vdev->minor);
740+
if (!m2m_dev->intf_devnode) {
741+
ret = -ENOMEM;
742+
goto err_rm_links1;
743+
}
744+
745+
/* Connect the two DMA engines to the interface */
746+
link = media_create_intf_link(m2m_dev->source,
747+
&m2m_dev->intf_devnode->intf,
748+
MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
749+
if (!link) {
750+
ret = -ENOMEM;
751+
goto err_rm_devnode;
752+
}
753+
754+
link = media_create_intf_link(&m2m_dev->sink,
755+
&m2m_dev->intf_devnode->intf,
756+
MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
757+
if (!link) {
758+
ret = -ENOMEM;
759+
goto err_rm_intf_link;
760+
}
761+
return 0;
762+
763+
err_rm_intf_link:
764+
media_remove_intf_links(&m2m_dev->intf_devnode->intf);
765+
err_rm_devnode:
766+
media_devnode_remove(m2m_dev->intf_devnode);
767+
err_rm_links1:
768+
media_entity_remove_links(&m2m_dev->sink);
769+
err_rm_links0:
770+
media_entity_remove_links(&m2m_dev->proc);
771+
media_entity_remove_links(m2m_dev->source);
772+
err_rel_entity2:
773+
media_device_unregister_entity(&m2m_dev->proc);
774+
kfree(m2m_dev->proc.name);
775+
err_rel_entity1:
776+
media_device_unregister_entity(&m2m_dev->sink);
777+
kfree(m2m_dev->sink.name);
778+
err_rel_entity0:
779+
media_device_unregister_entity(m2m_dev->source);
780+
kfree(m2m_dev->source->name);
781+
return ret;
782+
return 0;
783+
}
784+
EXPORT_SYMBOL_GPL(v4l2_m2m_register_media_controller);
785+
#endif
786+
597787
struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops)
598788
{
599789
struct v4l2_m2m_dev *m2m_dev;

‎include/media/v4l2-mem2mem.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct v4l2_m2m_ops {
4747
void (*job_abort)(void *priv);
4848
};
4949

50+
struct video_device;
5051
struct v4l2_m2m_dev;
5152

5253
/**
@@ -322,6 +323,24 @@ int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
322323
*/
323324
struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops);
324325

326+
#if defined(CONFIG_MEDIA_CONTROLLER)
327+
void v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev *m2m_dev);
328+
int v4l2_m2m_register_media_controller(struct v4l2_m2m_dev *m2m_dev,
329+
struct video_device *vdev, int function);
330+
#else
331+
static inline void
332+
v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev *m2m_dev)
333+
{
334+
}
335+
336+
static inline int
337+
v4l2_m2m_register_media_controller(struct v4l2_m2m_dev *m2m_dev,
338+
struct video_device *vdev, int function)
339+
{
340+
return 0;
341+
}
342+
#endif
343+
325344
/**
326345
* v4l2_m2m_release() - cleans up and frees a m2m_dev structure
327346
*

0 commit comments

Comments
 (0)