q6audio_v2_aio.c 7.3 KB

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