audio_mp3.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* mp3 audio output device
  2. *
  3. * Copyright (C) 2008 Google, Inc.
  4. * Copyright (C) 2008 HTC Corporation
  5. * Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include "audio_utils_aio.h"
  18. static struct miscdevice audio_mp3_misc;
  19. static struct ws_mgr audio_mp3_ws_mgr;
  20. #ifdef CONFIG_DEBUG_FS
  21. static const struct file_operations audio_mp3_debug_fops = {
  22. .read = audio_aio_debug_read,
  23. .open = audio_aio_debug_open,
  24. };
  25. #endif
  26. static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  27. {
  28. struct q6audio_aio *audio = file->private_data;
  29. int rc = 0;
  30. switch (cmd) {
  31. case AUDIO_START: {
  32. pr_debug("%s[%pK]: AUDIO_START session_id[%d]\n", __func__,
  33. audio, audio->ac->session);
  34. if (audio->feedback == NON_TUNNEL_MODE) {
  35. /* Configure PCM output block */
  36. rc = q6asm_enc_cfg_blk_pcm(audio->ac,
  37. audio->pcm_cfg.sample_rate,
  38. audio->pcm_cfg.channel_count);
  39. if (rc < 0) {
  40. pr_err("pcm output block config failed\n");
  41. break;
  42. }
  43. }
  44. rc = audio_aio_enable(audio);
  45. audio->eos_rsp = 0;
  46. audio->eos_flag = 0;
  47. if (!rc) {
  48. rc = enable_volume_ramp(audio);
  49. if (rc < 0) {
  50. pr_err("%s: Failed to enable volume ramp\n",
  51. __func__);
  52. }
  53. audio->enabled = 1;
  54. } else {
  55. audio->enabled = 0;
  56. pr_err("Audio Start procedure failed rc=%d\n", rc);
  57. break;
  58. }
  59. pr_info("%s: AUDIO_START sessionid[%d]enable[%d]\n", __func__,
  60. audio->ac->session,
  61. audio->enabled);
  62. if (audio->stopped == 1)
  63. audio->stopped = 0;
  64. break;
  65. }
  66. default:
  67. pr_debug("%s[%pK]: Calling utils ioctl\n", __func__, audio);
  68. rc = audio->codec_ioctl(file, cmd, arg);
  69. }
  70. return rc;
  71. }
  72. static int audio_open(struct inode *inode, struct file *file)
  73. {
  74. struct q6audio_aio *audio = NULL;
  75. int rc = 0;
  76. #ifdef CONFIG_DEBUG_FS
  77. /* 4 bytes represents decoder number, 1 byte for terminate string */
  78. char name[sizeof "msm_mp3_" + 5];
  79. #endif
  80. audio = kzalloc(sizeof(struct q6audio_aio), GFP_KERNEL);
  81. if (audio == NULL)
  82. return -ENOMEM;
  83. audio->pcm_cfg.buffer_size = PCM_BUFSZ_MIN;
  84. audio->miscdevice = &audio_mp3_misc;
  85. audio->wakelock_voted = false;
  86. audio->audio_ws_mgr = &audio_mp3_ws_mgr;
  87. audio->ac = q6asm_audio_client_alloc((app_cb) q6_audio_cb,
  88. (void *)audio);
  89. if (!audio->ac) {
  90. pr_err("Could not allocate memory for audio client\n");
  91. kfree(audio);
  92. return -ENOMEM;
  93. }
  94. rc = audio_aio_open(audio, file);
  95. if (rc < 0) {
  96. pr_err("%s: audio_aio_open rc=%d\n",
  97. __func__, rc);
  98. goto fail;
  99. }
  100. /* open in T/NT mode */
  101. if ((file->f_mode & FMODE_WRITE) && (file->f_mode & FMODE_READ)) {
  102. rc = q6asm_open_read_write(audio->ac, FORMAT_LINEAR_PCM,
  103. FORMAT_MP3);
  104. if (rc < 0) {
  105. pr_err("NT mode Open failed rc=%d\n", rc);
  106. rc = -ENODEV;
  107. goto fail;
  108. }
  109. audio->feedback = NON_TUNNEL_MODE;
  110. /* open MP3 decoder, expected frames is always 1
  111. * audio->buf_cfg.frames_per_buf = 0x01;
  112. */
  113. audio->buf_cfg.meta_info_enable = 0x01;
  114. } else if ((file->f_mode & FMODE_WRITE) &&
  115. !(file->f_mode & FMODE_READ)) {
  116. rc = q6asm_open_write(audio->ac, FORMAT_MP3);
  117. if (rc < 0) {
  118. pr_err("T mode Open failed rc=%d\n", rc);
  119. rc = -ENODEV;
  120. goto fail;
  121. }
  122. audio->feedback = TUNNEL_MODE;
  123. audio->buf_cfg.meta_info_enable = 0x00;
  124. } else {
  125. pr_err("Not supported mode\n");
  126. rc = -EACCES;
  127. goto fail;
  128. }
  129. #ifdef CONFIG_DEBUG_FS
  130. snprintf(name, sizeof(name), "msm_mp3_%04x", audio->ac->session);
  131. audio->dentry = debugfs_create_file(name, S_IFREG | 0444,
  132. NULL, (void *)audio,
  133. &audio_mp3_debug_fops);
  134. if (IS_ERR(audio->dentry))
  135. pr_debug("debugfs_create_file failed\n");
  136. #endif
  137. pr_info("%s:mp3dec success mode[%d]session[%d]\n", __func__,
  138. audio->feedback,
  139. audio->ac->session);
  140. return rc;
  141. fail:
  142. q6asm_audio_client_free(audio->ac);
  143. kfree(audio);
  144. return rc;
  145. }
  146. static const struct file_operations audio_mp3_fops = {
  147. .owner = THIS_MODULE,
  148. .open = audio_open,
  149. .release = audio_aio_release,
  150. .unlocked_ioctl = audio_ioctl,
  151. .fsync = audio_aio_fsync,
  152. };
  153. static struct miscdevice audio_mp3_misc = {
  154. .minor = MISC_DYNAMIC_MINOR,
  155. .name = "msm_mp3",
  156. .fops = &audio_mp3_fops,
  157. };
  158. static int __init audio_mp3_init(void)
  159. {
  160. int ret = misc_register(&audio_mp3_misc);
  161. if (ret == 0)
  162. device_init_wakeup(audio_mp3_misc.this_device, true);
  163. audio_mp3_ws_mgr.ref_cnt = 0;
  164. mutex_init(&audio_mp3_ws_mgr.ws_lock);
  165. return ret;
  166. }
  167. device_initcall(audio_mp3_init);