cam_fd_context.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2020, 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_dump_dev_in_activated(
  94. struct cam_context *ctx,
  95. struct cam_dump_req_cmd *cmd)
  96. {
  97. int rc;
  98. rc = cam_context_dump_dev_to_hw(ctx, cmd);
  99. if (rc)
  100. CAM_ERR(CAM_FD, "Failed to dump device, rc=%d", rc);
  101. return rc;
  102. }
  103. static int __cam_fd_ctx_flush_dev_in_activated(struct cam_context *ctx,
  104. struct cam_flush_dev_cmd *cmd)
  105. {
  106. int rc;
  107. rc = cam_context_flush_dev_to_hw(ctx, cmd);
  108. if (rc)
  109. CAM_ERR(CAM_ICP, "Failed to flush device, rc=%d", rc);
  110. return rc;
  111. }
  112. static int __cam_fd_ctx_config_dev_in_activated(
  113. struct cam_context *ctx, struct cam_config_dev_cmd *cmd)
  114. {
  115. int rc;
  116. rc = cam_context_prepare_dev_to_hw(ctx, cmd);
  117. if (rc) {
  118. CAM_ERR(CAM_FD, "Failed in Prepare dev, rc=%d", rc);
  119. return rc;
  120. }
  121. return rc;
  122. }
  123. static int __cam_fd_ctx_handle_irq_in_activated(void *context,
  124. uint32_t evt_id, void *evt_data)
  125. {
  126. int rc;
  127. rc = cam_context_buf_done_from_hw(context, evt_data, evt_id);
  128. if (rc) {
  129. CAM_ERR(CAM_FD, "Failed in buf done, rc=%d", rc);
  130. return rc;
  131. }
  132. return rc;
  133. }
  134. /* top state machine */
  135. static struct cam_ctx_ops
  136. cam_fd_ctx_state_machine[CAM_CTX_STATE_MAX] = {
  137. /* Uninit */
  138. {
  139. .ioctl_ops = {},
  140. .crm_ops = {},
  141. .irq_ops = NULL,
  142. },
  143. /* Available */
  144. {
  145. .ioctl_ops = {
  146. .acquire_dev = __cam_fd_ctx_acquire_dev_in_available,
  147. },
  148. .crm_ops = {},
  149. .irq_ops = NULL,
  150. },
  151. /* Acquired */
  152. {
  153. .ioctl_ops = {
  154. .release_dev = __cam_fd_ctx_release_dev_in_acquired,
  155. .config_dev = __cam_fd_ctx_config_dev_in_acquired,
  156. .start_dev = __cam_fd_ctx_start_dev_in_acquired,
  157. },
  158. .crm_ops = {},
  159. .irq_ops = NULL,
  160. },
  161. /* Ready */
  162. {
  163. .ioctl_ops = { },
  164. .crm_ops = {},
  165. .irq_ops = NULL,
  166. },
  167. /* Flushed */
  168. {},
  169. /* Activated */
  170. {
  171. .ioctl_ops = {
  172. .stop_dev = __cam_fd_ctx_stop_dev_in_activated,
  173. .release_dev = __cam_fd_ctx_release_dev_in_activated,
  174. .config_dev = __cam_fd_ctx_config_dev_in_activated,
  175. .flush_dev = __cam_fd_ctx_flush_dev_in_activated,
  176. .dump_dev = __cam_fd_ctx_dump_dev_in_activated,
  177. },
  178. .crm_ops = {},
  179. .irq_ops = __cam_fd_ctx_handle_irq_in_activated,
  180. },
  181. };
  182. int cam_fd_context_init(struct cam_fd_context *fd_ctx,
  183. struct cam_context *base_ctx, struct cam_hw_mgr_intf *hw_intf,
  184. uint32_t ctx_id)
  185. {
  186. int rc;
  187. if (!base_ctx || !fd_ctx) {
  188. CAM_ERR(CAM_FD, "Invalid Context %pK %pK", base_ctx, fd_ctx);
  189. return -EINVAL;
  190. }
  191. memset(fd_ctx, 0, sizeof(*fd_ctx));
  192. rc = cam_context_init(base_ctx, fd_dev_name, CAM_FD, ctx_id,
  193. NULL, hw_intf, fd_ctx->req_base, CAM_CTX_REQ_MAX);
  194. if (rc) {
  195. CAM_ERR(CAM_FD, "Camera Context Base init failed, rc=%d", rc);
  196. return rc;
  197. }
  198. fd_ctx->base = base_ctx;
  199. base_ctx->ctx_priv = fd_ctx;
  200. base_ctx->state_machine = cam_fd_ctx_state_machine;
  201. base_ctx->max_hw_update_entries = CAM_CTX_CFG_MAX;
  202. base_ctx->max_in_map_entries = CAM_CTX_CFG_MAX;
  203. base_ctx->max_out_map_entries = CAM_CTX_CFG_MAX;
  204. return rc;
  205. }
  206. int cam_fd_context_deinit(struct cam_fd_context *fd_ctx)
  207. {
  208. int rc = 0;
  209. if (!fd_ctx || !fd_ctx->base) {
  210. CAM_ERR(CAM_FD, "Invalid inputs %pK", fd_ctx);
  211. return -EINVAL;
  212. }
  213. rc = cam_context_deinit(fd_ctx->base);
  214. if (rc)
  215. CAM_ERR(CAM_FD, "Error in base deinit, rc=%d", rc);
  216. memset(fd_ctx, 0, sizeof(*fd_ctx));
  217. return rc;
  218. }