q6audio_v2_aio.c 7.2 KB

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