audio_mp3.c 4.4 KB

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