cam_fd_context.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include "cam_debug_util.h"
  8. #include "cam_fd_context.h"
  9. #include "cam_trace.h"
  10. static const char fd_dev_name[] = "cam-fd";
  11. /* Functions in Available state */
  12. static int __cam_fd_ctx_acquire_dev_in_available(struct cam_context *ctx,
  13. struct cam_acquire_dev_cmd *cmd)
  14. {
  15. int rc;
  16. rc = cam_context_acquire_dev_to_hw(ctx, cmd);
  17. if (rc) {
  18. CAM_ERR(CAM_FD, "Failed in Acquire dev, rc=%d", rc);
  19. return rc;
  20. }
  21. ctx->state = CAM_CTX_ACQUIRED;
  22. trace_cam_context_state("FD", ctx);
  23. return rc;
  24. }
  25. /* Functions in Acquired state */
  26. static int __cam_fd_ctx_release_dev_in_acquired(struct cam_context *ctx,
  27. struct cam_release_dev_cmd *cmd)
  28. {
  29. int rc;
  30. rc = cam_context_release_dev_to_hw(ctx, cmd);
  31. if (rc) {
  32. CAM_ERR(CAM_FD, "Failed in Release dev, rc=%d", rc);
  33. return rc;
  34. }
  35. ctx->state = CAM_CTX_AVAILABLE;
  36. trace_cam_context_state("FD", ctx);
  37. return rc;
  38. }
  39. static int __cam_fd_ctx_config_dev_in_acquired(struct cam_context *ctx,
  40. struct cam_config_dev_cmd *cmd)
  41. {
  42. int rc;
  43. rc = cam_context_prepare_dev_to_hw(ctx, cmd);
  44. if (rc) {
  45. CAM_ERR(CAM_FD, "Failed in Prepare dev, rc=%d", rc);
  46. return rc;
  47. }
  48. return rc;
  49. }
  50. static int __cam_fd_ctx_start_dev_in_acquired(struct cam_context *ctx,
  51. struct cam_start_stop_dev_cmd *cmd)
  52. {
  53. int rc;
  54. rc = cam_context_start_dev_to_hw(ctx, cmd);
  55. if (rc) {
  56. CAM_ERR(CAM_FD, "Failed in Start dev, rc=%d", rc);
  57. return rc;
  58. }
  59. ctx->state = CAM_CTX_ACTIVATED;
  60. trace_cam_context_state("FD", ctx);
  61. return rc;
  62. }
  63. /* Functions in Activated state */
  64. static int __cam_fd_ctx_stop_dev_in_activated(struct cam_context *ctx,
  65. struct cam_start_stop_dev_cmd *cmd)
  66. {
  67. int rc;
  68. rc = cam_context_stop_dev_to_hw(ctx);
  69. if (rc) {
  70. CAM_ERR(CAM_FD, "Failed in Stop dev, rc=%d", rc);
  71. return rc;
  72. }
  73. ctx->state = CAM_CTX_ACQUIRED;
  74. trace_cam_context_state("FD", ctx);
  75. return rc;
  76. }
  77. static int __cam_fd_ctx_release_dev_in_activated(struct cam_context *ctx,
  78. struct cam_release_dev_cmd *cmd)
  79. {
  80. int rc;
  81. rc = __cam_fd_ctx_stop_dev_in_activated(ctx, NULL);
  82. if (rc) {
  83. CAM_ERR(CAM_FD, "Failed in Stop dev, rc=%d", rc);
  84. return rc;
  85. }
  86. rc = __cam_fd_ctx_release_dev_in_acquired(ctx, cmd);
  87. if (rc) {
  88. CAM_ERR(CAM_FD, "Failed in Release dev, rc=%d", rc);
  89. return rc;
  90. }
  91. return rc;
  92. }
  93. static int __cam_fd_ctx_flush_dev_in_activated(struct cam_context *ctx,
  94. struct cam_flush_dev_cmd *cmd)
  95. {
  96. int rc;
  97. rc = cam_context_flush_dev_to_hw(ctx, cmd);
  98. if (rc)
  99. CAM_ERR(CAM_ICP, "Failed to flush device, rc=%d", rc);
  100. return rc;
  101. }
  102. static int __cam_fd_ctx_config_dev_in_activated(
  103. struct cam_context *ctx, struct cam_config_dev_cmd *cmd)
  104. {
  105. int rc;
  106. rc = cam_context_prepare_dev_to_hw(ctx, cmd);
  107. if (rc) {
  108. CAM_ERR(CAM_FD, "Failed in Prepare dev, rc=%d", rc);
  109. return rc;
  110. }
  111. return rc;
  112. }
  113. static int __cam_fd_ctx_handle_irq_in_activated(void *context,
  114. uint32_t evt_id, void *evt_data)
  115. {
  116. int rc;
  117. rc = cam_context_buf_done_from_hw(context, evt_data, evt_id);
  118. if (rc) {
  119. CAM_ERR(CAM_FD, "Failed in buf done, rc=%d", rc);
  120. return rc;
  121. }
  122. return rc;
  123. }
  124. /* top state machine */
  125. static struct cam_ctx_ops
  126. cam_fd_ctx_state_machine[CAM_CTX_STATE_MAX] = {
  127. /* Uninit */
  128. {
  129. .ioctl_ops = {},
  130. .crm_ops = {},
  131. .irq_ops = NULL,
  132. },
  133. /* Available */
  134. {
  135. .ioctl_ops = {
  136. .acquire_dev = __cam_fd_ctx_acquire_dev_in_available,
  137. },
  138. .crm_ops = {},
  139. .irq_ops = NULL,
  140. },
  141. /* Acquired */
  142. {
  143. .ioctl_ops = {
  144. .release_dev = __cam_fd_ctx_release_dev_in_acquired,
  145. .config_dev = __cam_fd_ctx_config_dev_in_acquired,
  146. .start_dev = __cam_fd_ctx_start_dev_in_acquired,
  147. },
  148. .crm_ops = {},
  149. .irq_ops = NULL,
  150. },
  151. /* Ready */
  152. {
  153. .ioctl_ops = { },
  154. .crm_ops = {},
  155. .irq_ops = NULL,
  156. },
  157. /* Flushed */
  158. {},
  159. /* Activated */
  160. {
  161. .ioctl_ops = {
  162. .stop_dev = __cam_fd_ctx_stop_dev_in_activated,
  163. .release_dev = __cam_fd_ctx_release_dev_in_activated,
  164. .config_dev = __cam_fd_ctx_config_dev_in_activated,
  165. .flush_dev = __cam_fd_ctx_flush_dev_in_activated,
  166. },
  167. .crm_ops = {},
  168. .irq_ops = __cam_fd_ctx_handle_irq_in_activated,
  169. },
  170. };
  171. int cam_fd_context_init(struct cam_fd_context *fd_ctx,
  172. struct cam_context *base_ctx, struct cam_hw_mgr_intf *hw_intf,
  173. uint32_t ctx_id)
  174. {
  175. int rc;
  176. if (!base_ctx || !fd_ctx) {
  177. CAM_ERR(CAM_FD, "Invalid Context %pK %pK", base_ctx, fd_ctx);
  178. return -EINVAL;
  179. }
  180. memset(fd_ctx, 0, sizeof(*fd_ctx));
  181. rc = cam_context_init(base_ctx, fd_dev_name, CAM_FD, ctx_id,
  182. NULL, hw_intf, fd_ctx->req_base, CAM_CTX_REQ_MAX);
  183. if (rc) {
  184. CAM_ERR(CAM_FD, "Camera Context Base init failed, rc=%d", rc);
  185. return rc;
  186. }
  187. fd_ctx->base = base_ctx;
  188. base_ctx->ctx_priv = fd_ctx;
  189. base_ctx->state_machine = cam_fd_ctx_state_machine;
  190. return rc;
  191. }
  192. int cam_fd_context_deinit(struct cam_fd_context *fd_ctx)
  193. {
  194. int rc = 0;
  195. if (!fd_ctx || !fd_ctx->base) {
  196. CAM_ERR(CAM_FD, "Invalid inputs %pK", fd_ctx);
  197. return -EINVAL;
  198. }
  199. rc = cam_context_deinit(fd_ctx->base);
  200. if (rc)
  201. CAM_ERR(CAM_FD, "Error in base deinit, rc=%d", rc);
  202. memset(fd_ctx, 0, sizeof(*fd_ctx));
  203. return rc;
  204. }