q6audio_v2_aio.c 7.0 KB

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