audio_amrnb.c 5.7 KB

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