[PATCH v2 11/15] media: tegra-video: video: Resolve video device via media graph on notifications

From: Svyatoslav Ryhel

Date: Sun Nov 29 2026 - 13:08:48 EST


Walk downstream from the notifying subdevice along source pad links via
find_pipeline_video_device() to locate the associated video_device and
channel instead of relying on v4l2_get_subdev_hostdata(sd), and filter
notifications to V4L2_EVENT_SOURCE_CHANGE before forwarding events and
marking streaming vb2 queues in error.

Signed-off-by: Svyatoslav Ryhel <clamor95@xxxxxxxxx>
---
.../media/platform/nvidia/tegra-video/video.c | 74 +++++++++++++++----
1 file changed, 59 insertions(+), 15 deletions(-)

diff --git a/drivers/media/platform/nvidia/tegra-video/video.c b/drivers/media/platform/nvidia/tegra-video/video.c
index 2e84385708bac..9b8f57415778f 100644
--- a/drivers/media/platform/nvidia/tegra-video/video.c
+++ b/drivers/media/platform/nvidia/tegra-video/video.c
@@ -7,38 +7,82 @@
#include <linux/module.h>
#include <linux/platform_device.h>

+#include <media/media-entity.h>
+#include <media/v4l2-dev.h>
#include <media/v4l2-event.h>
+#include <media/v4l2-subdev.h>

#include "video.h"

-static void tegra_v4l2_dev_release(struct v4l2_device *v4l2_dev)
+/* Walk from the caller to the video device via active links */
+static struct video_device *find_pipeline_video_device(struct v4l2_subdev *sd)
{
- struct tegra_video_device *vid;
+ struct media_entity *entity = &sd->entity;
+ struct video_device *vdev = NULL;

- vid = container_of(v4l2_dev, struct tegra_video_device, v4l2_dev);
+ while (entity) {
+ struct media_pad *src_pad = NULL;
+ struct media_pad *remote_pad;
+ unsigned int i;

- /* cleanup channels here as all video device nodes are released */
- tegra_channels_cleanup(vid->vi);
+ if (is_media_entity_v4l2_video_device(entity)) {
+ vdev = media_entity_to_video_device(entity);
+ break;
+ }

- v4l2_device_unregister(v4l2_dev);
- media_device_unregister(&vid->media_dev);
- media_device_cleanup(&vid->media_dev);
- kfree(vid);
+ for (i = 0; i < entity->num_pads; i++) {
+ if (entity->pads[i].flags & MEDIA_PAD_FL_SOURCE) {
+ src_pad = &entity->pads[i];
+ break;
+ }
+ }
+
+ if (!src_pad)
+ break;
+
+ remote_pad = media_pad_remote_pad_first(src_pad);
+ if (!remote_pad)
+ break;
+
+ entity = remote_pad->entity;
+ }
+
+ return vdev;
}

static void tegra_v4l2_dev_notify(struct v4l2_subdev *sd,
unsigned int notification, void *arg)
{
- struct tegra_vi_channel *chan;
const struct v4l2_event *ev = arg;
+ struct video_device *video;

- if (notification != V4L2_DEVICE_NOTIFY_EVENT)
+ if (notification != V4L2_DEVICE_NOTIFY_EVENT ||
+ ev->type != V4L2_EVENT_SOURCE_CHANGE)
return;

- chan = v4l2_get_subdev_hostdata(sd);
- v4l2_event_queue(&chan->video, arg);
- if (ev->type == V4L2_EVENT_SOURCE_CHANGE && vb2_is_streaming(&chan->queue))
- vb2_queue_error(&chan->queue);
+ video = find_pipeline_video_device(sd);
+ if (video) {
+ struct tegra_vi_channel *chan = video_get_drvdata(video);
+
+ v4l2_event_queue(video, ev);
+ if (vb2_is_streaming(&chan->queue))
+ vb2_queue_error(&chan->queue);
+ }
+}
+
+static void tegra_v4l2_dev_release(struct v4l2_device *v4l2_dev)
+{
+ struct tegra_video_device *vid;
+
+ vid = container_of(v4l2_dev, struct tegra_video_device, v4l2_dev);
+
+ /* cleanup channels here as all video device nodes are released */
+ tegra_channels_cleanup(vid->vi);
+
+ v4l2_device_unregister(v4l2_dev);
+ media_device_unregister(&vid->media_dev);
+ media_device_cleanup(&vid->media_dev);
+ kfree(vid);
}

static int host1x_video_probe(struct host1x_device *dev)
--
2.53.0