q6audio_v2_aio.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/module.h>
  5. #include <linux/fs.h>
  6. #include <linux/miscdevice.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/sched.h>
  9. #include <linux/wait.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/slab.h>
  12. #include <linux/atomic.h>
  13. #include <asm/ioctls.h>
  14. #include "audio_utils_aio.h"
  15. void q6_audio_cb(uint32_t opcode, uint32_t token,
  16. uint32_t *payload, void *priv)
  17. {
  18. struct q6audio_aio *audio = (struct q6audio_aio *)priv;
  19. pr_debug("%s:opcode = %x token = 0x%x\n", __func__, opcode, token);
  20. switch (opcode) {
  21. case ASM_DATA_EVENT_WRITE_DONE_V2:
  22. case ASM_DATA_EVENT_READ_DONE_V2:
  23. case ASM_DATA_EVENT_RENDERED_EOS:
  24. case ASM_DATA_EVENT_RENDERED_EOS_V2:
  25. case ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2:
  26. case ASM_STREAM_CMD_SET_ENCDEC_PARAM:
  27. case ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY:
  28. case ASM_DATA_EVENT_ENC_SR_CM_CHANGE_NOTIFY:
  29. case RESET_EVENTS:
  30. audio_aio_cb(opcode, token, payload, audio);
  31. break;
  32. default:
  33. pr_debug("%s:Unhandled event = 0x%8x\n", __func__, opcode);
  34. break;
  35. }
  36. }
  37. void audio_aio_cb(uint32_t opcode, uint32_t token,
  38. uint32_t *payload, void *priv/*struct q6audio_aio *audio*/)
  39. {
  40. struct q6audio_aio *audio = (struct q6audio_aio *)priv;
  41. union msm_audio_event_payload e_payload;
  42. spin_lock(&enc_dec_lock);
  43. if (audio == NULL) {
  44. pr_err("%s: failed to get q6audio value\n", __func__);
  45. goto error;
  46. }
  47. switch (opcode) {
  48. case ASM_DATA_EVENT_WRITE_DONE_V2:
  49. pr_debug("%s[%pK]:ASM_DATA_EVENT_WRITE_DONE token = 0x%x\n",
  50. __func__, audio, token);
  51. audio_aio_async_write_ack(audio, token, payload);
  52. break;
  53. case ASM_DATA_EVENT_READ_DONE_V2:
  54. pr_debug("%s[%pK]:ASM_DATA_EVENT_READ_DONE token = 0x%x\n",
  55. __func__, audio, token);
  56. audio_aio_async_read_ack(audio, token, payload);
  57. break;
  58. case ASM_DATA_EVENT_RENDERED_EOS:
  59. case ASM_DATA_EVENT_RENDERED_EOS_V2:
  60. /* EOS Handle */
  61. pr_debug("%s[%pK]:ASM_DATA_CMDRSP_EOS\n", __func__, audio);
  62. if (audio->feedback) { /* Non-Tunnel mode */
  63. audio->eos_rsp = 1;
  64. /* propagate input EOS i/p buffer,
  65. * after receiving DSP acknowledgment
  66. */
  67. if (audio->eos_flag &&
  68. (audio->eos_write_payload.aio_buf.buf_addr)) {
  69. audio_aio_post_event(audio,
  70. AUDIO_EVENT_WRITE_DONE,
  71. audio->eos_write_payload);
  72. memset(&audio->eos_write_payload, 0,
  73. sizeof(union msm_audio_event_payload));
  74. audio->eos_flag = 0;
  75. }
  76. } else { /* Tunnel mode */
  77. audio->eos_rsp = 1;
  78. wake_up(&audio->write_wait);
  79. wake_up(&audio->cmd_wait);
  80. }
  81. break;
  82. case ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2:
  83. case ASM_STREAM_CMD_SET_ENCDEC_PARAM:
  84. pr_debug("%s[%pK]:payload0[%x] payloa1d[%x]opcode= 0x%x\n",
  85. __func__, audio, payload[0], payload[1], opcode);
  86. break;
  87. case ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY:
  88. case ASM_DATA_EVENT_ENC_SR_CM_CHANGE_NOTIFY:
  89. pr_debug("%s[%pK]: ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY, payload[0]-sr = %d, payload[1]-chl = %d, payload[2] = %d, payload[3] = %d\n",
  90. __func__, audio, payload[0],
  91. payload[1], payload[2], payload[3]);
  92. pr_debug("%s[%pK]: ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY, sr(prev) = %d, chl(prev) = %d,",
  93. __func__, audio, audio->pcm_cfg.sample_rate,
  94. audio->pcm_cfg.channel_count);
  95. audio->pcm_cfg.sample_rate = payload[0];
  96. audio->pcm_cfg.channel_count = payload[1] & 0xFFFF;
  97. e_payload.stream_info.chan_info = audio->pcm_cfg.channel_count;
  98. e_payload.stream_info.sample_rate = audio->pcm_cfg.sample_rate;
  99. audio_aio_post_event(audio, AUDIO_EVENT_STREAM_INFO, e_payload);
  100. break;
  101. case RESET_EVENTS:
  102. pr_err("%s: Received opcode:0x%x\n", __func__, opcode);
  103. audio->stopped = 1;
  104. audio->reset_event = true;
  105. wake_up(&audio->event_wait);
  106. break;
  107. default:
  108. break;
  109. }
  110. error:
  111. spin_unlock(&enc_dec_lock);
  112. }
  113. int extract_meta_out_info(struct q6audio_aio *audio,
  114. struct audio_aio_buffer_node *buf_node, int dir)
  115. {
  116. struct dec_meta_out *meta_data = buf_node->kvaddr;
  117. uint32_t temp;
  118. if (dir) { /* input buffer - Write */
  119. if (audio->buf_cfg.meta_info_enable) {
  120. if (buf_node->buf.buf_len <
  121. sizeof(struct dec_meta_in)) {
  122. pr_debug("%s: invalid buf len %d\n",
  123. __func__, buf_node->buf.buf_len);
  124. return -EINVAL;
  125. }
  126. memcpy(&buf_node->meta_info.meta_in,
  127. (char *)buf_node->kvaddr, sizeof(struct dec_meta_in));
  128. } else {
  129. memset(&buf_node->meta_info.meta_in,
  130. 0, sizeof(struct dec_meta_in));
  131. }
  132. pr_debug("%s[%pK]:i/p: msw_ts %d lsw_ts %d nflags 0x%8x\n",
  133. __func__, audio,
  134. buf_node->meta_info.meta_in.ntimestamp.highpart,
  135. buf_node->meta_info.meta_in.ntimestamp.lowpart,
  136. buf_node->meta_info.meta_in.nflags);
  137. } else { /* output buffer - Read */
  138. memcpy((char *)buf_node->kvaddr,
  139. &buf_node->meta_info.meta_out,
  140. sizeof(struct dec_meta_out));
  141. meta_data->meta_out_dsp[0].nflags = 0x00000000;
  142. temp = meta_data->meta_out_dsp[0].msw_ts;
  143. meta_data->meta_out_dsp[0].msw_ts =
  144. meta_data->meta_out_dsp[0].lsw_ts;
  145. meta_data->meta_out_dsp[0].lsw_ts = temp;
  146. pr_debug("%s[%pK]:o/p: msw_ts %d lsw_ts %d nflags 0x%8x, num_frames = %d\n",
  147. __func__, audio,
  148. ((struct dec_meta_out *)buf_node->kvaddr)->
  149. meta_out_dsp[0].msw_ts,
  150. ((struct dec_meta_out *)buf_node->kvaddr)->
  151. meta_out_dsp[0].lsw_ts,
  152. ((struct dec_meta_out *)buf_node->kvaddr)->
  153. meta_out_dsp[0].nflags,
  154. ((struct dec_meta_out *)buf_node->kvaddr)->num_of_frames);
  155. }
  156. return 0;
  157. }
  158. /* Read buffer from DSP / Handle Ack from DSP */
  159. void audio_aio_async_read_ack(struct q6audio_aio *audio, uint32_t token,
  160. uint32_t *payload)
  161. {
  162. unsigned long flags;
  163. union msm_audio_event_payload event_payload;
  164. struct audio_aio_buffer_node *filled_buf;
  165. int ret;
  166. pr_debug("%s\n", __func__);
  167. /* No active flush in progress */
  168. if (audio->rflush)
  169. return;
  170. /* Statistics of read */
  171. atomic_add(payload[4], &audio->in_bytes);
  172. atomic_add(payload[9], &audio->in_samples);
  173. spin_lock_irqsave(&audio->dsp_lock, flags);
  174. if (list_empty(&audio->in_queue)) {
  175. spin_unlock_irqrestore(&audio->dsp_lock, flags);
  176. pr_warn("%s unexpected ack from dsp\n", __func__);
  177. return;
  178. }
  179. filled_buf = list_first_entry(&audio->in_queue,
  180. struct audio_aio_buffer_node, list);
  181. pr_debug("%s token: 0x[%x], filled_buf->token: 0x[%x]",
  182. __func__, token, filled_buf->token);
  183. if (token == (filled_buf->token)) {
  184. list_del(&filled_buf->list);
  185. spin_unlock_irqrestore(&audio->dsp_lock, flags);
  186. event_payload.aio_buf = filled_buf->buf;
  187. /* Read done Buffer due to flush/normal condition
  188. * after EOS event, so append EOS buffer
  189. */
  190. if (audio->eos_rsp == 0x1) {
  191. event_payload.aio_buf.data_len =
  192. insert_eos_buf(audio, filled_buf);
  193. /* Reset flag back to indicate eos intimated */
  194. audio->eos_rsp = 0;
  195. } else {
  196. filled_buf->meta_info.meta_out.num_of_frames
  197. = payload[9];
  198. event_payload.aio_buf.data_len = payload[4]
  199. + payload[5] + sizeof(struct dec_meta_out);
  200. pr_debug("%s[%pK]:nr of frames 0x%8x len=%d\n",
  201. __func__, audio,
  202. filled_buf->meta_info.meta_out.num_of_frames,
  203. event_payload.aio_buf.data_len);
  204. ret = extract_meta_out_info(audio, filled_buf, 0);
  205. audio->eos_rsp = 0;
  206. }
  207. pr_debug("%s, posting read done to the app here\n", __func__);
  208. audio_aio_post_event(audio, AUDIO_EVENT_READ_DONE,
  209. event_payload);
  210. kfree(filled_buf);
  211. } else {
  212. pr_err("%s[%pK]:expected=%x ret=%x\n",
  213. __func__, audio, filled_buf->token, token);
  214. spin_unlock_irqrestore(&audio->dsp_lock, flags);
  215. }
  216. }