1 // SPDX-License-Identifier: GPL-2.0+
3 * vimc-streamer.c Virtual Media Controller Driver
5 * Copyright (C) 2018 Lucas A. M. Magalhães <lucmaga@gmail.com>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/freezer.h>
12 #include <linux/kthread.h>
14 #include "vimc-streamer.h"
17 * vimc_get_source_entity - get the entity connected with the first sink pad
19 * @ent: reference media_entity
21 * Helper function that returns the media entity containing the source pad
22 * linked with the first sink pad from the given media entity pad list.
24 static struct media_entity *vimc_get_source_entity(struct media_entity *ent)
26 struct media_pad *pad;
29 for (i = 0; i < ent->num_pads; i++) {
30 if (ent->pads[i].flags & MEDIA_PAD_FL_SOURCE)
32 pad = media_entity_remote_pad(&ent->pads[i]);
33 return pad ? pad->entity : NULL;
39 * vimc_streamer_pipeline_terminate - Disable stream in all ved in stream
41 * @stream: the pointer to the stream structure with the pipeline to be
44 * Calls s_stream to disable the stream in each entity of the pipeline
47 static void vimc_streamer_pipeline_terminate(struct vimc_stream *stream)
49 struct vimc_ent_device *ved;
50 struct v4l2_subdev *sd;
52 while (stream->pipe_size) {
54 ved = stream->ved_pipeline[stream->pipe_size];
56 stream->ved_pipeline[stream->pipe_size] = NULL;
58 if (!is_media_entity_v4l2_subdev(ved->ent))
61 sd = media_entity_to_v4l2_subdev(ved->ent);
62 v4l2_subdev_call(sd, video, s_stream, 0);
67 * vimc_streamer_pipeline_init - initializes the stream structure
69 * @stream: the pointer to the stream structure to be initialized
70 * @ved: the pointer to the vimc entity initializing the stream
72 * Initializes the stream structure. Walks through the entity graph to
73 * construct the pipeline used later on the streamer thread.
74 * Calls s_stream to enable stream in all entities of the pipeline.
76 static int vimc_streamer_pipeline_init(struct vimc_stream *stream,
77 struct vimc_ent_device *ved)
79 struct media_entity *entity;
80 struct video_device *vdev;
81 struct v4l2_subdev *sd;
84 stream->pipe_size = 0;
85 while (stream->pipe_size < VIMC_STREAMER_PIPELINE_MAX_SIZE) {
87 vimc_streamer_pipeline_terminate(stream);
90 stream->ved_pipeline[stream->pipe_size++] = ved;
93 if (is_media_entity_v4l2_subdev(ved->ent)) {
94 sd = media_entity_to_v4l2_subdev(ved->ent);
95 ret = v4l2_subdev_call(sd, video, s_stream, 1);
96 if (ret && ret != -ENOIOCTLCMD) {
97 pr_err("subdev_call error %s\n",
99 vimc_streamer_pipeline_terminate(stream);
104 entity = vimc_get_source_entity(ved->ent);
105 /* Check if the end of the pipeline was reached*/
109 /* Get the next device in the pipeline */
110 if (is_media_entity_v4l2_subdev(entity)) {
111 sd = media_entity_to_v4l2_subdev(entity);
112 ved = v4l2_get_subdevdata(sd);
114 vdev = container_of(entity,
117 ved = video_get_drvdata(vdev);
121 vimc_streamer_pipeline_terminate(stream);
126 * vimc_streamer_thread - process frames through the pipeline
128 * @data: vimc_stream struct of the current stream
130 * From the source to the sink, gets a frame from each subdevice and send to
131 * the next one of the pipeline at a fixed framerate.
133 static int vimc_streamer_thread(void *data)
135 struct vimc_stream *stream = data;
143 if (kthread_should_stop())
146 for (i = stream->pipe_size - 1; i >= 0; i--) {
147 frame = stream->ved_pipeline[i]->process_frame(
148 stream->ved_pipeline[i], frame);
149 if (!frame || IS_ERR(frame))
153 set_current_state(TASK_UNINTERRUPTIBLE);
154 schedule_timeout(HZ / 60);
161 * vimc_streamer_s_stream - start/stop the streaming on the media pipeline
163 * @stream: the pointer to the stream structure of the current stream
164 * @ved: pointer to the vimc entity of the entity of the stream
165 * @enable: flag to determine if stream should start/stop
167 * When starting, check if there is no stream->kthread allocated. This should
168 * indicate that a stream is already running. Then, it initializes
169 * the pipeline, creates and runs a kthread to consume buffers through the
171 * When stopping, analogously check if there is a stream running, stop
172 * the thread and terminates the pipeline.
174 int vimc_streamer_s_stream(struct vimc_stream *stream,
175 struct vimc_ent_device *ved,
187 ret = vimc_streamer_pipeline_init(stream, ved);
191 stream->kthread = kthread_run(vimc_streamer_thread, stream,
192 "vimc-streamer thread");
194 if (IS_ERR(stream->kthread))
195 return PTR_ERR(stream->kthread);
198 if (!stream->kthread)
201 ret = kthread_stop(stream->kthread);
205 stream->kthread = NULL;
207 vimc_streamer_pipeline_terminate(stream);
212 EXPORT_SYMBOL_GPL(vimc_streamer_s_stream);