q6audio_v2_aio.c 7.1 KB

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