vimc-streamer.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // 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. #include <linux/init.h>
  9. #include <linux/freezer.h>
  10. #include <linux/kthread.h>
  11. #include "vimc-streamer.h"
  12. /**
  13. * vimc_get_source_entity - get the entity connected with the first sink pad
  14. *
  15. * @ent: reference media_entity
  16. *
  17. * Helper function that returns the media entity containing the source pad
  18. * linked with the first sink pad from the given media entity pad list.
  19. *
  20. * Return: The source pad or NULL, if it wasn't found.
  21. */
  22. static struct media_entity *vimc_get_source_entity(struct media_entity *ent)
  23. {
  24. struct media_pad *pad;
  25. int i;
  26. for (i = 0; i < ent->num_pads; i++) {
  27. if (ent->pads[i].flags & MEDIA_PAD_FL_SOURCE)
  28. continue;
  29. pad = media_pad_remote_pad_first(&ent->pads[i]);
  30. return pad ? pad->entity : NULL;
  31. }
  32. return NULL;
  33. }
  34. /**
  35. * vimc_streamer_pipeline_terminate - Disable stream in all ved in stream
  36. *
  37. * @stream: the pointer to the stream structure with the pipeline to be
  38. * disabled.
  39. *
  40. * Calls s_stream to disable the stream in each entity of the pipeline
  41. *
  42. */
  43. static void vimc_streamer_pipeline_terminate(struct vimc_stream *stream)
  44. {
  45. struct vimc_ent_device *ved;
  46. struct v4l2_subdev *sd;
  47. while (stream->pipe_size) {
  48. stream->pipe_size--;
  49. ved = stream->ved_pipeline[stream->pipe_size];
  50. stream->ved_pipeline[stream->pipe_size] = NULL;
  51. if (!is_media_entity_v4l2_subdev(ved->ent))
  52. continue;
  53. sd = media_entity_to_v4l2_subdev(ved->ent);
  54. /*
  55. * Do not call .s_stream() to stop an already
  56. * stopped/unstarted subdev.
  57. */
  58. if (!v4l2_subdev_is_streaming(sd))
  59. continue;
  60. v4l2_subdev_call(sd, video, s_stream, 0);
  61. }
  62. }
  63. /**
  64. * vimc_streamer_pipeline_init - Initializes the stream structure
  65. *
  66. * @stream: the pointer to the stream structure to be initialized
  67. * @ved: the pointer to the vimc entity initializing the stream
  68. *
  69. * Initializes the stream structure. Walks through the entity graph to
  70. * construct the pipeline used later on the streamer thread.
  71. * Calls vimc_streamer_s_stream() to enable stream in all entities of
  72. * the pipeline.
  73. *
  74. * Return: 0 if success, error code otherwise.
  75. */
  76. static int vimc_streamer_pipeline_init(struct vimc_stream *stream,
  77. struct vimc_ent_device *ved)
  78. {
  79. struct media_entity *entity;
  80. struct video_device *vdev;
  81. struct v4l2_subdev *sd;
  82. int ret = 0;
  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. if (is_media_entity_v4l2_subdev(ved->ent)) {
  91. sd = media_entity_to_v4l2_subdev(ved->ent);
  92. ret = v4l2_subdev_call(sd, video, s_stream, 1);
  93. if (ret && ret != -ENOIOCTLCMD) {
  94. dev_err(ved->dev, "subdev_call error %s\n",
  95. ved->ent->name);
  96. vimc_streamer_pipeline_terminate(stream);
  97. return ret;
  98. }
  99. }
  100. entity = vimc_get_source_entity(ved->ent);
  101. /* Check if the end of the pipeline was reached */
  102. if (!entity) {
  103. /* the first entity of the pipe should be source only */
  104. if (!vimc_is_source(ved->ent)) {
  105. dev_err(ved->dev,
  106. "first entity in the pipe '%s' is not a source\n",
  107. ved->ent->name);
  108. vimc_streamer_pipeline_terminate(stream);
  109. return -EPIPE;
  110. }
  111. return 0;
  112. }
  113. /* Get the next device in the pipeline */
  114. if (is_media_entity_v4l2_subdev(entity)) {
  115. sd = media_entity_to_v4l2_subdev(entity);
  116. ved = v4l2_get_subdevdata(sd);
  117. } else {
  118. vdev = container_of(entity,
  119. struct video_device,
  120. entity);
  121. ved = video_get_drvdata(vdev);
  122. }
  123. }
  124. vimc_streamer_pipeline_terminate(stream);
  125. return -EINVAL;
  126. }
  127. /**
  128. * vimc_streamer_thread - Process frames through the pipeline
  129. *
  130. * @data: vimc_stream struct of the current stream
  131. *
  132. * From the source to the sink, gets a frame from each subdevice and send to
  133. * the next one of the pipeline at a fixed framerate.
  134. *
  135. * Return:
  136. * Always zero (created as ``int`` instead of ``void`` to comply with
  137. * kthread API).
  138. */
  139. static int vimc_streamer_thread(void *data)
  140. {
  141. struct vimc_stream *stream = data;
  142. u8 *frame = NULL;
  143. int i;
  144. set_freezable();
  145. for (;;) {
  146. try_to_freeze();
  147. if (kthread_should_stop())
  148. break;
  149. for (i = stream->pipe_size - 1; i >= 0; i--) {
  150. frame = stream->ved_pipeline[i]->process_frame(
  151. stream->ved_pipeline[i], frame);
  152. if (!frame || IS_ERR(frame))
  153. break;
  154. }
  155. //wait for 60hz
  156. set_current_state(TASK_UNINTERRUPTIBLE);
  157. schedule_timeout(HZ / 60);
  158. }
  159. return 0;
  160. }
  161. /**
  162. * vimc_streamer_s_stream - Start/stop the streaming on the media pipeline
  163. *
  164. * @stream: the pointer to the stream structure of the current stream
  165. * @ved: pointer to the vimc entity of the entity of the stream
  166. * @enable: flag to determine if stream should start/stop
  167. *
  168. * When starting, check if there is no ``stream->kthread`` allocated. This
  169. * should indicate that a stream is already running. Then, it initializes the
  170. * pipeline, creates and runs a kthread to consume buffers through the pipeline.
  171. * When stopping, analogously check if there is a stream running, stop the
  172. * thread and terminates the pipeline.
  173. *
  174. * Return: 0 if success, error code otherwise.
  175. */
  176. int vimc_streamer_s_stream(struct vimc_stream *stream,
  177. struct vimc_ent_device *ved,
  178. int enable)
  179. {
  180. int ret;
  181. if (!stream || !ved)
  182. return -EINVAL;
  183. if (enable) {
  184. if (stream->kthread)
  185. return 0;
  186. ret = vimc_streamer_pipeline_init(stream, ved);
  187. if (ret)
  188. return ret;
  189. stream->kthread = kthread_run(vimc_streamer_thread, stream,
  190. "vimc-streamer thread");
  191. if (IS_ERR(stream->kthread)) {
  192. ret = PTR_ERR(stream->kthread);
  193. dev_err(ved->dev, "kthread_run failed with %d\n", ret);
  194. vimc_streamer_pipeline_terminate(stream);
  195. stream->kthread = NULL;
  196. return ret;
  197. }
  198. } else {
  199. if (!stream->kthread)
  200. return 0;
  201. ret = kthread_stop(stream->kthread);
  202. /*
  203. * kthread_stop returns -EINTR in cases when streamon was
  204. * immediately followed by streamoff, and the thread didn't had
  205. * a chance to run. Ignore errors to stop the stream in the
  206. * pipeline.
  207. */
  208. if (ret)
  209. dev_dbg(ved->dev, "kthread_stop returned '%d'\n", ret);
  210. stream->kthread = NULL;
  211. vimc_streamer_pipeline_terminate(stream);
  212. }
  213. return 0;
  214. }