blob: 6fc18fea2e458a5219a5fa833b6f90b545e8b1a8 [file] [log] [blame]
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -05001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * vimc-streamer.c Virtual Media Controller Driver
4 *
5 * Copyright (C) 2018 Lucas A. M. Magalhães <lucmaga@gmail.com>
6 *
7 */
8
9#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/freezer.h>
12#include <linux/kthread.h>
13
14#include "vimc-streamer.h"
15
16/**
17 * vimc_get_source_entity - get the entity connected with the first sink pad
18 *
19 * @ent: reference media_entity
20 *
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.
23 */
24static struct media_entity *vimc_get_source_entity(struct media_entity *ent)
25{
26 struct media_pad *pad;
27 int i;
28
29 for (i = 0; i < ent->num_pads; i++) {
30 if (ent->pads[i].flags & MEDIA_PAD_FL_SOURCE)
31 continue;
32 pad = media_entity_remote_pad(&ent->pads[i]);
33 return pad ? pad->entity : NULL;
34 }
35 return NULL;
36}
37
38/*
39 * vimc_streamer_pipeline_terminate - Disable stream in all ved in stream
40 *
41 * @stream: the pointer to the stream structure with the pipeline to be
42 * disabled.
43 *
44 * Calls s_stream to disable the stream in each entity of the pipeline
45 *
46 */
47static void vimc_streamer_pipeline_terminate(struct vimc_stream *stream)
48{
Helen Fornazier6f3f3e12019-03-06 17:42:43 -050049 struct vimc_ent_device *ved;
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -050050 struct v4l2_subdev *sd;
51
52 while (stream->pipe_size) {
53 stream->pipe_size--;
Helen Fornazier6f3f3e12019-03-06 17:42:43 -050054 ved = stream->ved_pipeline[stream->pipe_size];
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -050055 stream->ved_pipeline[stream->pipe_size] = NULL;
56
Helen Fornazier6f3f3e12019-03-06 17:42:43 -050057 if (!is_media_entity_v4l2_subdev(ved->ent))
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -050058 continue;
59
Helen Fornazier6f3f3e12019-03-06 17:42:43 -050060 sd = media_entity_to_v4l2_subdev(ved->ent);
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -050061 v4l2_subdev_call(sd, video, s_stream, 0);
62 }
63}
64
65/*
66 * vimc_streamer_pipeline_init - initializes the stream structure
67 *
68 * @stream: the pointer to the stream structure to be initialized
69 * @ved: the pointer to the vimc entity initializing the stream
70 *
71 * Initializes the stream structure. Walks through the entity graph to
72 * construct the pipeline used later on the streamer thread.
73 * Calls s_stream to enable stream in all entities of the pipeline.
74 */
75static int vimc_streamer_pipeline_init(struct vimc_stream *stream,
76 struct vimc_ent_device *ved)
77{
78 struct media_entity *entity;
79 struct video_device *vdev;
80 struct v4l2_subdev *sd;
81 int ret = 0;
82
83 stream->pipe_size = 0;
84 while (stream->pipe_size < VIMC_STREAMER_PIPELINE_MAX_SIZE) {
85 if (!ved) {
86 vimc_streamer_pipeline_terminate(stream);
87 return -EINVAL;
88 }
89 stream->ved_pipeline[stream->pipe_size++] = ved;
90
Helen Fornazier6f3f3e12019-03-06 17:42:43 -050091 if (is_media_entity_v4l2_subdev(ved->ent)) {
92 sd = media_entity_to_v4l2_subdev(ved->ent);
93 ret = v4l2_subdev_call(sd, video, s_stream, 1);
94 if (ret && ret != -ENOIOCTLCMD) {
95 pr_err("subdev_call error %s\n",
96 ved->ent->name);
97 vimc_streamer_pipeline_terminate(stream);
98 return ret;
99 }
100 }
101
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500102 entity = vimc_get_source_entity(ved->ent);
103 /* Check if the end of the pipeline was reached*/
104 if (!entity)
105 return 0;
106
Helen Fornazier6f3f3e12019-03-06 17:42:43 -0500107 /* Get the next device in the pipeline */
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500108 if (is_media_entity_v4l2_subdev(entity)) {
109 sd = media_entity_to_v4l2_subdev(entity);
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500110 ved = v4l2_get_subdevdata(sd);
111 } else {
112 vdev = container_of(entity,
113 struct video_device,
114 entity);
115 ved = video_get_drvdata(vdev);
116 }
117 }
118
119 vimc_streamer_pipeline_terminate(stream);
120 return -EINVAL;
121}
122
123static int vimc_streamer_thread(void *data)
124{
125 struct vimc_stream *stream = data;
Helen Fornazierb72e4492019-03-06 17:42:41 -0500126 u8 *frame = NULL;
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500127 int i;
128
129 set_freezable();
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500130
131 for (;;) {
132 try_to_freeze();
133 if (kthread_should_stop())
134 break;
135
136 for (i = stream->pipe_size - 1; i >= 0; i--) {
Helen Fornazierb72e4492019-03-06 17:42:41 -0500137 frame = stream->ved_pipeline[i]->process_frame(
138 stream->ved_pipeline[i], frame);
139 if (!frame || IS_ERR(frame))
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500140 break;
141 }
142 //wait for 60hz
Helen Fornazier2978a502019-03-06 17:42:38 -0500143 set_current_state(TASK_UNINTERRUPTIBLE);
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500144 schedule_timeout(HZ / 60);
145 }
146
147 return 0;
148}
149
150int vimc_streamer_s_stream(struct vimc_stream *stream,
151 struct vimc_ent_device *ved,
152 int enable)
153{
154 int ret;
155
156 if (!stream || !ved)
157 return -EINVAL;
158
159 if (enable) {
160 if (stream->kthread)
161 return 0;
162
163 ret = vimc_streamer_pipeline_init(stream, ved);
164 if (ret)
165 return ret;
166
167 stream->kthread = kthread_run(vimc_streamer_thread, stream,
168 "vimc-streamer thread");
169
170 if (IS_ERR(stream->kthread))
171 return PTR_ERR(stream->kthread);
172
173 } else {
174 if (!stream->kthread)
175 return 0;
176
177 ret = kthread_stop(stream->kthread);
178 if (ret)
179 return ret;
180
181 stream->kthread = NULL;
182
183 vimc_streamer_pipeline_terminate(stream);
184 }
185
186 return 0;
187}
188EXPORT_SYMBOL_GPL(vimc_streamer_s_stream);
189
190MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Streamer");
191MODULE_AUTHOR("Lucas A. M. Magalhães <lucmaga@gmail.com>");
192MODULE_LICENSE("GPL");