vimc-streamer.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 <[email protected]>
  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. v4l2_subdev_call(sd, video, s_stream, 0);
  55. }
  56. }
  57. /**
  58. * vimc_streamer_pipeline_init - Initializes the stream structure
  59. *
  60. * @stream: the pointer to the stream structure to be initialized
  61. * @ved: the pointer to the vimc entity initializing the stream
  62. *
  63. * Initializes the stream structure. Walks through the entity graph to
  64. * construct the pipeline used later on the streamer thread.
  65. * Calls vimc_streamer_s_stream() to enable stream in all entities of
  66. * the pipeline.
  67. *
  68. * Return: 0 if success, error code otherwise.
  69. */
  70. static int vimc_streamer_pipeline_init(struct vimc_stream *stream,
  71. struct vimc_ent_device *ved)
  72. {
  73. struct media_entity *entity;
  74. struct video_device *vdev;
  75. struct v4l2_subdev *sd;
  76. int ret = 0;
  77. stream->pipe_size = 0;
  78. while (stream->pipe_size < VIMC_STREAMER_PIPELINE_MAX_SIZE) {
  79. if (!ved) {
  80. vimc_streamer_pipeline_terminate(stream);
  81. return -EINVAL;
  82. }
  83. stream->ved_pipeline[stream->pipe_size++] = ved;
  84. if (is_media_entity_v4l2_subdev(ved->ent)) {
  85. sd = media_entity_to_v4l2_subdev(ved->ent);
  86. ret = v4l2_subdev_call(sd, video, s_stream, 1);
  87. if (ret && ret != -ENOIOCTLCMD) {
  88. dev_err(ved->dev, "subdev_call error %s\n",
  89. ved->ent->name);
  90. vimc_streamer_pipeline_terminate(stream);
  91. return ret;
  92. }
  93. }
  94. entity = vimc_get_source_entity(ved->ent);
  95. /* Check if the end of the pipeline was reached */
  96. if (!entity) {
  97. /* the first entity of the pipe should be source only */
  98. if (!vimc_is_source(ved->ent)) {
  99. dev_err(ved->dev,
  100. "first entity in the pipe '%s' is not a source\n",
  101. ved->ent->name);
  102. vimc_streamer_pipeline_terminate(stream);
  103. return -EPIPE;
  104. }
  105. return 0;
  106. }
  107. /* Get the next device in the pipeline */
  108. if (is_media_entity_v4l2_subdev(entity)) {
  109. sd = media_entity_to_v4l2_subdev(entity);
  110. 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. vimc_streamer_pipeline_terminate(stream);
  119. return -EINVAL;
  120. }
  121. /**
  122. * vimc_streamer_thread - Process frames through the pipeline
  123. *
  124. * @data: vimc_stream struct of the current stream
  125. *
  126. * From the source to the sink, gets a frame from each subdevice and send to
  127. * the next one of the pipeline at a fixed framerate.
  128. *
  129. * Return:
  130. * Always zero (created as ``int`` instead of ``void`` to comply with
  131. * kthread API).
  132. */
  133. static int vimc_streamer_thread(void *data)
  134. {
  135. struct vimc_stream *stream = data;
  136. u8 *frame = NULL;
  137. int i;
  138. set_freezable();
  139. for (;;) {
  140. try_to_freeze();
  141. if (kthread_should_stop())
  142. break;
  143. for (i = stream->pipe_size - 1; i >= 0; i--) {
  144. frame = stream->ved_pipeline[i]->process_frame(
  145. stream->ved_pipeline[i], frame);
  146. if (!frame || IS_ERR(frame))
  147. break;
  148. }
  149. //wait for 60hz
  150. set_current_state(TASK_UNINTERRUPTIBLE);
  151. schedule_timeout(HZ / 60);
  152. }
  153. return 0;
  154. }
  155. /**
  156. * vimc_streamer_s_stream - Start/stop the streaming on the media pipeline
  157. *
  158. * @stream: the pointer to the stream structure of the current stream
  159. * @ved: pointer to the vimc entity of the entity of the stream
  160. * @enable: flag to determine if stream should start/stop
  161. *
  162. * When starting, check if there is no ``stream->kthread`` allocated. This
  163. * should indicate that a stream is already running. Then, it initializes the
  164. * pipeline, creates and runs a kthread to consume buffers through the pipeline.
  165. * When stopping, analogously check if there is a stream running, stop the
  166. * thread and terminates the pipeline.
  167. *
  168. * Return: 0 if success, error code otherwise.
  169. */
  170. int vimc_streamer_s_stream(struct vimc_stream *stream,
  171. struct vimc_ent_device *ved,
  172. int enable)
  173. {
  174. int ret;
  175. if (!stream || !ved)
  176. return -EINVAL;
  177. if (enable) {
  178. if (stream->kthread)
  179. return 0;
  180. ret = vimc_streamer_pipeline_init(stream, ved);
  181. if (ret)
  182. return ret;
  183. stream->kthread = kthread_run(vimc_streamer_thread, stream,
  184. "vimc-streamer thread");
  185. if (IS_ERR(stream->kthread)) {
  186. ret = PTR_ERR(stream->kthread);
  187. dev_err(ved->dev, "kthread_run failed with %d\n", ret);
  188. vimc_streamer_pipeline_terminate(stream);
  189. stream->kthread = NULL;
  190. return ret;
  191. }
  192. } else {
  193. if (!stream->kthread)
  194. return 0;
  195. ret = kthread_stop(stream->kthread);
  196. /*
  197. * kthread_stop returns -EINTR in cases when streamon was
  198. * immediately followed by streamoff, and the thread didn't had
  199. * a chance to run. Ignore errors to stop the stream in the
  200. * pipeline.
  201. */
  202. if (ret)
  203. dev_dbg(ved->dev, "kthread_stop returned '%d'\n", ret);
  204. stream->kthread = NULL;
  205. vimc_streamer_pipeline_terminate(stream);
  206. }
  207. return 0;
  208. }