audio_amrwb.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* amrwb 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 <linux/compat.h>
  18. #include <linux/types.h>
  19. #include "audio_utils_aio.h"
  20. static struct miscdevice audio_amrwb_misc;
  21. static struct ws_mgr audio_amrwb_ws_mgr;
  22. #ifdef CONFIG_DEBUG_FS
  23. static const struct file_operations audio_amrwb_debug_fops = {
  24. .read = audio_aio_debug_read,
  25. .open = audio_aio_debug_open,
  26. };
  27. #endif
  28. static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  29. {
  30. struct q6audio_aio *audio = file->private_data;
  31. int rc = 0;
  32. switch (cmd) {
  33. case AUDIO_START: {
  34. pr_debug("%s[%pK]: AUDIO_START session_id[%d]\n", __func__,
  35. audio, audio->ac->session);
  36. if (audio->feedback == NON_TUNNEL_MODE) {
  37. /* Configure PCM output block */
  38. rc = q6asm_enc_cfg_blk_pcm(audio->ac,
  39. audio->pcm_cfg.sample_rate,
  40. audio->pcm_cfg.channel_count);
  41. if (rc < 0) {
  42. pr_err("pcm output block config failed\n");
  43. break;
  44. }
  45. }
  46. rc = audio_aio_enable(audio);
  47. audio->eos_rsp = 0;
  48. audio->eos_flag = 0;
  49. if (!rc) {
  50. audio->enabled = 1;
  51. } else {
  52. audio->enabled = 0;
  53. pr_err("Audio Start procedure failed rc=%d\n", rc);
  54. break;
  55. }
  56. pr_debug("%s: AUDIO_START sessionid[%d]enable[%d]\n", __func__,
  57. audio->ac->session,
  58. audio->enabled);
  59. if (audio->stopped == 1)
  60. audio->stopped = 0;
  61. break;
  62. }
  63. default:
  64. pr_debug("%s[%pK]: Calling utils ioctl\n", __func__, audio);
  65. rc = audio->codec_ioctl(file, cmd, arg);
  66. }
  67. return rc;
  68. }
  69. static long audio_compat_ioctl(struct file *file, unsigned int cmd,
  70. unsigned long arg)
  71. {
  72. struct q6audio_aio *audio = file->private_data;
  73. int rc = 0;
  74. switch (cmd) {
  75. case AUDIO_START: {
  76. pr_debug("%s[%pK]: AUDIO_START session_id[%d]\n", __func__,
  77. audio, audio->ac->session);
  78. if (audio->feedback == NON_TUNNEL_MODE) {
  79. /* Configure PCM output block */
  80. rc = q6asm_enc_cfg_blk_pcm(audio->ac,
  81. audio->pcm_cfg.sample_rate,
  82. audio->pcm_cfg.channel_count);
  83. if (rc < 0) {
  84. pr_err("%s: pcm output block config failed rc=%d\n",
  85. __func__, rc);
  86. break;
  87. }
  88. }
  89. rc = audio_aio_enable(audio);
  90. audio->eos_rsp = 0;
  91. audio->eos_flag = 0;
  92. if (!rc) {
  93. audio->enabled = 1;
  94. } else {
  95. audio->enabled = 0;
  96. pr_err("%s: Audio Start procedure failed rc=%d\n",
  97. __func__, rc);
  98. break;
  99. }
  100. pr_debug("%s: AUDIO_START sessionid[%d]enable[%d]\n", __func__,
  101. audio->ac->session,
  102. audio->enabled);
  103. if (audio->stopped == 1)
  104. audio->stopped = 0;
  105. break;
  106. }
  107. default:
  108. pr_debug("%s[%pK]: Calling compat ioctl\n", __func__, audio);
  109. rc = audio->codec_compat_ioctl(file, cmd, arg);
  110. }
  111. return rc;
  112. }
  113. static int audio_open(struct inode *inode, struct file *file)
  114. {
  115. struct q6audio_aio *audio = NULL;
  116. int rc = 0;
  117. #ifdef CONFIG_DEBUG_FS
  118. /* 4 bytes represents decoder number, 1 byte for terminate string */
  119. char name[sizeof "msm_amrwb_" + 5];
  120. #endif
  121. audio = kzalloc(sizeof(struct q6audio_aio), GFP_KERNEL);
  122. if (audio == NULL)
  123. return -ENOMEM;
  124. audio->pcm_cfg.buffer_size = PCM_BUFSZ_MIN;
  125. audio->miscdevice = &audio_amrwb_misc;
  126. audio->wakelock_voted = false;
  127. audio->audio_ws_mgr = &audio_amrwb_ws_mgr;
  128. audio->ac = q6asm_audio_client_alloc((app_cb) q6_audio_cb,
  129. (void *)audio);
  130. if (!audio->ac) {
  131. pr_err("Could not allocate memory for audio client\n");
  132. kfree(audio);
  133. return -ENOMEM;
  134. }
  135. rc = audio_aio_open(audio, file);
  136. if (rc < 0) {
  137. pr_err("%s: audio_aio_open rc=%d\n",
  138. __func__, rc);
  139. goto fail;
  140. }
  141. /* open in T/NT mode */
  142. if ((file->f_mode & FMODE_WRITE) && (file->f_mode & FMODE_READ)) {
  143. rc = q6asm_open_read_write(audio->ac, FORMAT_LINEAR_PCM,
  144. FORMAT_AMRWB);
  145. if (rc < 0) {
  146. pr_err("NT mode Open failed rc=%d\n", rc);
  147. rc = -ENODEV;
  148. goto fail;
  149. }
  150. audio->feedback = NON_TUNNEL_MODE;
  151. audio->buf_cfg.frames_per_buf = 0x01;
  152. audio->buf_cfg.meta_info_enable = 0x01;
  153. } else if ((file->f_mode & FMODE_WRITE) &&
  154. !(file->f_mode & FMODE_READ)) {
  155. rc = q6asm_open_write(audio->ac, FORMAT_AMRWB);
  156. if (rc < 0) {
  157. pr_err("T mode Open failed rc=%d\n", rc);
  158. rc = -ENODEV;
  159. goto fail;
  160. }
  161. audio->feedback = TUNNEL_MODE;
  162. audio->buf_cfg.meta_info_enable = 0x00;
  163. } else {
  164. pr_err("Not supported mode\n");
  165. rc = -EACCES;
  166. goto fail;
  167. }
  168. #ifdef CONFIG_DEBUG_FS
  169. snprintf(name, sizeof(name), "msm_amrwb_%04x", audio->ac->session);
  170. audio->dentry = debugfs_create_file(name, S_IFREG | 0444,
  171. NULL, (void *)audio,
  172. &audio_amrwb_debug_fops);
  173. if (IS_ERR(audio->dentry))
  174. pr_debug("debugfs_create_file failed\n");
  175. #endif
  176. pr_info("%s: AMRWB dec success mode[%d]session[%d]\n", __func__,
  177. audio->feedback,
  178. audio->ac->session);
  179. return 0;
  180. fail:
  181. q6asm_audio_client_free(audio->ac);
  182. kfree(audio);
  183. return rc;
  184. }
  185. static const struct file_operations audio_amrwb_fops = {
  186. .owner = THIS_MODULE,
  187. .open = audio_open,
  188. .release = audio_aio_release,
  189. .unlocked_ioctl = audio_ioctl,
  190. .fsync = audio_aio_fsync,
  191. .compat_ioctl = audio_compat_ioctl,
  192. };
  193. static struct miscdevice audio_amrwb_misc = {
  194. .minor = MISC_DYNAMIC_MINOR,
  195. .name = "msm_amrwb",
  196. .fops = &audio_amrwb_fops,
  197. };
  198. int __init audio_amrwb_init(void)
  199. {
  200. int ret = misc_register(&audio_amrwb_misc);
  201. if (ret == 0)
  202. device_init_wakeup(audio_amrwb_misc.this_device, true);
  203. audio_amrwb_ws_mgr.ref_cnt = 0;
  204. mutex_init(&audio_amrwb_ws_mgr.ws_lock);
  205. return ret;
  206. }
  207. void audio_amrwb_exit(void)
  208. {
  209. mutex_destroy(&audio_amrwb_ws_mgr.ws_lock);
  210. misc_deregister(&audio_amrwb_misc);
  211. }