vimc-streamer.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/module.h>
  10. #include <linux/freezer.h>
  11. #include <linux/kthread.h>
  12. #include "vimc-streamer.h"
  13. /**
  14. * vimc_get_source_entity - get the entity connected with the first sink pad
  15. *
  16. * @ent: reference media_entity
  17. *
  18. * Helper function that returns the media entity containing the source pad
  19. * linked with the first sink pad from the given media entity pad list.
  20. */
  21. static struct media_entity *vimc_get_source_entity(struct media_entity *ent)
  22. {
  23. struct media_pad *pad;
  24. int i;
  25. for (i = 0; i < ent->num_pads; i++) {
  26. if (ent->pads[i].flags & MEDIA_PAD_FL_SOURCE)
  27. continue;
  28. pad = media_entity_remote_pad(&ent->pads[i]);
  29. return pad ? pad->entity : NULL;
  30. }
  31. return NULL;
  32. }
  33. /*
  34. * vimc_streamer_pipeline_terminate - Disable stream in all ved in stream
  35. *
  36. * @stream: the pointer to the stream structure with the pipeline to be
  37. * disabled.
  38. *
  39. * Calls s_stream to disable the stream in each entity of the pipeline
  40. *
  41. */
  42. static void vimc_streamer_pipeline_terminate(struct vimc_stream *stream)
  43. {
  44. struct media_entity *entity;
  45. struct v4l2_subdev *sd;
  46. while (stream->pipe_size) {
  47. stream->pipe_size--;
  48. entity = stream->ved_pipeline[stream->pipe_size]->ent;
  49. entity = vimc_get_source_entity(entity);
  50. stream->ved_pipeline[stream->pipe_size] = NULL;
  51. if (!is_media_entity_v4l2_subdev(entity))
  52. continue;
  53. sd = media_entity_to_v4l2_subdev(entity);
  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 s_stream to enable stream in all entities of the pipeline.
  66. */
  67. static int vimc_streamer_pipeline_init(struct vimc_stream *stream,
  68. struct vimc_ent_device *ved)
  69. {
  70. struct media_entity *entity;
  71. struct video_device *vdev;
  72. struct v4l2_subdev *sd;
  73. int ret = 0;
  74. stream->pipe_size = 0;
  75. while (stream->pipe_size < VIMC_STREAMER_PIPELINE_MAX_SIZE) {
  76. if (!ved) {
  77. vimc_streamer_pipeline_terminate(stream);
  78. return -EINVAL;
  79. }
  80. stream->ved_pipeline[stream->pipe_size++] = ved;
  81. entity = vimc_get_source_entity(ved->ent);
  82. /* Check if the end of the pipeline was reached*/
  83. if (!entity)
  84. return 0;
  85. if (is_media_entity_v4l2_subdev(entity)) {
  86. sd = media_entity_to_v4l2_subdev(entity);
  87. ret = v4l2_subdev_call(sd, video, s_stream, 1);
  88. if (ret && ret != -ENOIOCTLCMD) {
  89. vimc_streamer_pipeline_terminate(stream);
  90. return ret;
  91. }
  92. ved = v4l2_get_subdevdata(sd);
  93. } else {
  94. vdev = container_of(entity,
  95. struct video_device,
  96. entity);
  97. ved = video_get_drvdata(vdev);
  98. }
  99. }
  100. vimc_streamer_pipeline_terminate(stream);
  101. return -EINVAL;
  102. }
  103. static int vimc_streamer_thread(void *data)
  104. {
  105. struct vimc_stream *stream = data;
  106. int i;
  107. set_freezable();
  108. for (;;) {
  109. try_to_freeze();
  110. if (kthread_should_stop())
  111. break;
  112. for (i = stream->pipe_size - 1; i >= 0; i--) {
  113. stream->frame = stream->ved_pipeline[i]->process_frame(
  114. stream->ved_pipeline[i],
  115. stream->frame);
  116. if (!stream->frame)
  117. break;
  118. if (IS_ERR(stream->frame))
  119. break;
  120. }
  121. //wait for 60hz
  122. set_current_state(TASK_UNINTERRUPTIBLE);
  123. schedule_timeout(HZ / 60);
  124. }
  125. return 0;
  126. }
  127. int vimc_streamer_s_stream(struct vimc_stream *stream,
  128. struct vimc_ent_device *ved,
  129. int enable)
  130. {
  131. int ret;
  132. if (!stream || !ved)
  133. return -EINVAL;
  134. if (enable) {
  135. if (stream->kthread)
  136. return 0;
  137. ret = vimc_streamer_pipeline_init(stream, ved);
  138. if (ret)
  139. return ret;
  140. stream->kthread = kthread_run(vimc_streamer_thread, stream,
  141. "vimc-streamer thread");
  142. if (IS_ERR(stream->kthread))
  143. return PTR_ERR(stream->kthread);
  144. } else {
  145. if (!stream->kthread)
  146. return 0;
  147. ret = kthread_stop(stream->kthread);
  148. if (ret)
  149. return ret;
  150. stream->kthread = NULL;
  151. vimc_streamer_pipeline_terminate(stream);
  152. }
  153. return 0;
  154. }
  155. EXPORT_SYMBOL_GPL(vimc_streamer_s_stream);
  156. MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Streamer");
  157. MODULE_AUTHOR("Lucas A. M. Magalhães <lucmaga@gmail.com>");
  158. MODULE_LICENSE("GPL");