audio_amrwbplus.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* amr-wbplus audio output device
  2. *
  3. * Copyright (C) 2008 Google, Inc.
  4. * Copyright (C) 2008 HTC Corporation
  5. * Copyright (c) 2010-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/msm_audio_amrwbplus.h>
  18. #include <linux/compat.h>
  19. #include "audio_utils_aio.h"
  20. static struct miscdevice audio_amrwbplus_misc;
  21. static struct ws_mgr audio_amrwbplus_ws_mgr;
  22. #ifdef CONFIG_DEBUG_FS
  23. static const struct file_operations audio_amrwbplus_debug_fops = {
  24. .read = audio_aio_debug_read,
  25. .open = audio_aio_debug_open,
  26. };
  27. static void config_debug_fs(struct q6audio_aio *audio)
  28. {
  29. if (audio != NULL) {
  30. char name[sizeof("msm_amrwbplus_") + 5];
  31. snprintf(name, sizeof(name), "msm_amrwbplus_%04x",
  32. audio->ac->session);
  33. audio->dentry = debugfs_create_file(name, S_IFREG | 0444,
  34. NULL, (void *)audio,
  35. &audio_amrwbplus_debug_fops);
  36. if (IS_ERR(audio->dentry))
  37. pr_debug("debugfs_create_file failed\n");
  38. }
  39. }
  40. #else
  41. static void config_debug_fs(struct q6audio_aio *audio)
  42. {
  43. }
  44. #endif
  45. static long audio_ioctl_shared(struct file *file, unsigned int cmd,
  46. void *arg)
  47. {
  48. struct asm_amrwbplus_cfg q6_amrwbplus_cfg;
  49. struct msm_audio_amrwbplus_config_v2 *amrwbplus_drv_config;
  50. struct q6audio_aio *audio = file->private_data;
  51. int rc = 0;
  52. switch (cmd) {
  53. case AUDIO_START:
  54. pr_err("%s[%pK]: AUDIO_START session_id[%d]\n", __func__,
  55. audio, audio->ac->session);
  56. if (audio->feedback == NON_TUNNEL_MODE) {
  57. /* Configure PCM output block */
  58. rc = q6asm_enc_cfg_blk_pcm(audio->ac,
  59. audio->pcm_cfg.sample_rate,
  60. audio->pcm_cfg.channel_count);
  61. if (rc < 0) {
  62. pr_err("pcm output block config failed\n");
  63. break;
  64. }
  65. }
  66. amrwbplus_drv_config =
  67. (struct msm_audio_amrwbplus_config_v2 *)audio->codec_cfg;
  68. q6_amrwbplus_cfg.size_bytes =
  69. amrwbplus_drv_config->size_bytes;
  70. q6_amrwbplus_cfg.version =
  71. amrwbplus_drv_config->version;
  72. q6_amrwbplus_cfg.num_channels =
  73. amrwbplus_drv_config->num_channels;
  74. q6_amrwbplus_cfg.amr_band_mode =
  75. amrwbplus_drv_config->amr_band_mode;
  76. q6_amrwbplus_cfg.amr_dtx_mode =
  77. amrwbplus_drv_config->amr_dtx_mode;
  78. q6_amrwbplus_cfg.amr_frame_fmt =
  79. amrwbplus_drv_config->amr_frame_fmt;
  80. q6_amrwbplus_cfg.amr_lsf_idx =
  81. amrwbplus_drv_config->amr_lsf_idx;
  82. rc = q6asm_media_format_block_amrwbplus(audio->ac,
  83. &q6_amrwbplus_cfg);
  84. if (rc < 0) {
  85. pr_err("q6asm_media_format_block_amrwb+ failed...\n");
  86. break;
  87. }
  88. rc = audio_aio_enable(audio);
  89. audio->eos_rsp = 0;
  90. audio->eos_flag = 0;
  91. if (!rc) {
  92. audio->enabled = 1;
  93. } else {
  94. audio->enabled = 0;
  95. pr_err("Audio Start procedure failed rc=%d\n", rc);
  96. break;
  97. }
  98. pr_debug("%s:AUDIO_START sessionid[%d]enable[%d]\n", __func__,
  99. audio->ac->session,
  100. audio->enabled);
  101. if (audio->stopped == 1)
  102. audio->stopped = 0;
  103. break;
  104. default:
  105. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  106. rc = -EINVAL;
  107. break;
  108. }
  109. return rc;
  110. }
  111. static long audio_ioctl(struct file *file, unsigned int cmd,
  112. unsigned long arg)
  113. {
  114. struct q6audio_aio *audio = file->private_data;
  115. int rc = 0;
  116. switch (cmd) {
  117. case AUDIO_START: {
  118. rc = audio_ioctl_shared(file, cmd, (void *)arg);
  119. break;
  120. }
  121. case AUDIO_GET_AMRWBPLUS_CONFIG_V2: {
  122. if ((audio) && (arg) && (audio->codec_cfg)) {
  123. if (copy_to_user((void *)arg, audio->codec_cfg,
  124. sizeof(struct msm_audio_amrwbplus_config_v2))) {
  125. rc = -EFAULT;
  126. pr_err("%s: copy_to_user for AUDIO_GET_AMRWBPLUS_CONFIG_V2 failed\n",
  127. __func__);
  128. break;
  129. }
  130. } else {
  131. pr_err("%s: wb+ config v2 invalid parameters\n"
  132. , __func__);
  133. rc = -EFAULT;
  134. break;
  135. }
  136. break;
  137. }
  138. case AUDIO_SET_AMRWBPLUS_CONFIG_V2: {
  139. if ((audio) && (arg) && (audio->codec_cfg)) {
  140. if (copy_from_user(audio->codec_cfg, (void *)arg,
  141. sizeof(struct msm_audio_amrwbplus_config_v2))) {
  142. rc = -EFAULT;
  143. pr_err("%s: copy_from_user for AUDIO_SET_AMRWBPLUS_CONFIG_V2 failed\n",
  144. __func__);
  145. break;
  146. }
  147. } else {
  148. pr_err("%s: wb+ config invalid parameters\n",
  149. __func__);
  150. rc = -EFAULT;
  151. break;
  152. }
  153. break;
  154. }
  155. default: {
  156. pr_debug("%s[%pK]: Calling utils ioctl\n", __func__, audio);
  157. rc = audio->codec_ioctl(file, cmd, arg);
  158. break;
  159. }
  160. }
  161. return rc;
  162. }
  163. #ifdef CONFIG_COMPAT
  164. struct msm_audio_amrwbplus_config_v2_32 {
  165. u32 size_bytes;
  166. u32 version;
  167. u32 num_channels;
  168. u32 amr_band_mode;
  169. u32 amr_dtx_mode;
  170. u32 amr_frame_fmt;
  171. u32 amr_lsf_idx;
  172. };
  173. enum {
  174. AUDIO_GET_AMRWBPLUS_CONFIG_V2_32 = _IOR(AUDIO_IOCTL_MAGIC,
  175. (AUDIO_MAX_COMMON_IOCTL_NUM+2),
  176. struct msm_audio_amrwbplus_config_v2_32),
  177. AUDIO_SET_AMRWBPLUS_CONFIG_V2_32 = _IOW(AUDIO_IOCTL_MAGIC,
  178. (AUDIO_MAX_COMMON_IOCTL_NUM+3),
  179. struct msm_audio_amrwbplus_config_v2_32)
  180. };
  181. static long audio_compat_ioctl(struct file *file, unsigned int cmd,
  182. unsigned long arg)
  183. {
  184. struct q6audio_aio *audio = file->private_data;
  185. int rc = 0;
  186. switch (cmd) {
  187. case AUDIO_START: {
  188. rc = audio_ioctl_shared(file, cmd, (void *)arg);
  189. break;
  190. }
  191. case AUDIO_GET_AMRWBPLUS_CONFIG_V2_32: {
  192. if (audio && arg && (audio->codec_cfg)) {
  193. struct msm_audio_amrwbplus_config_v2 *amrwbplus_config;
  194. struct msm_audio_amrwbplus_config_v2_32
  195. amrwbplus_config_32;
  196. memset(&amrwbplus_config_32, 0,
  197. sizeof(amrwbplus_config_32));
  198. amrwbplus_config =
  199. (struct msm_audio_amrwbplus_config_v2 *)
  200. audio->codec_cfg;
  201. amrwbplus_config_32.size_bytes =
  202. amrwbplus_config->size_bytes;
  203. amrwbplus_config_32.version =
  204. amrwbplus_config->version;
  205. amrwbplus_config_32.num_channels =
  206. amrwbplus_config->num_channels;
  207. amrwbplus_config_32.amr_band_mode =
  208. amrwbplus_config->amr_band_mode;
  209. amrwbplus_config_32.amr_dtx_mode =
  210. amrwbplus_config->amr_dtx_mode;
  211. amrwbplus_config_32.amr_frame_fmt =
  212. amrwbplus_config->amr_frame_fmt;
  213. amrwbplus_config_32.amr_lsf_idx =
  214. amrwbplus_config->amr_lsf_idx;
  215. if (copy_to_user((void *)arg, &amrwbplus_config_32,
  216. sizeof(amrwbplus_config_32))) {
  217. rc = -EFAULT;
  218. pr_err("%s: copy_to_user for AUDIO_GET_AMRWBPLUS_CONFIG_V2_32 failed\n"
  219. , __func__);
  220. }
  221. } else {
  222. pr_err("%s: wb+ Get config v2 invalid parameters\n"
  223. , __func__);
  224. rc = -EFAULT;
  225. }
  226. break;
  227. }
  228. case AUDIO_SET_AMRWBPLUS_CONFIG_V2_32: {
  229. if ((audio) && (arg) && (audio->codec_cfg)) {
  230. struct msm_audio_amrwbplus_config_v2 *amrwbplus_config;
  231. struct msm_audio_amrwbplus_config_v2_32
  232. amrwbplus_config_32;
  233. if (copy_from_user(&amrwbplus_config_32, (void *)arg,
  234. sizeof(struct msm_audio_amrwbplus_config_v2_32))) {
  235. rc = -EFAULT;
  236. pr_err("%s: copy_from_user for AUDIO_SET_AMRWBPLUS_CONFIG_V2_32 failed\n"
  237. , __func__);
  238. break;
  239. }
  240. amrwbplus_config =
  241. (struct msm_audio_amrwbplus_config_v2 *)
  242. audio->codec_cfg;
  243. amrwbplus_config->size_bytes =
  244. amrwbplus_config_32.size_bytes;
  245. amrwbplus_config->version =
  246. amrwbplus_config_32.version;
  247. amrwbplus_config->num_channels =
  248. amrwbplus_config_32.num_channels;
  249. amrwbplus_config->amr_band_mode =
  250. amrwbplus_config_32.amr_band_mode;
  251. amrwbplus_config->amr_dtx_mode =
  252. amrwbplus_config_32.amr_dtx_mode;
  253. amrwbplus_config->amr_frame_fmt =
  254. amrwbplus_config_32.amr_frame_fmt;
  255. amrwbplus_config->amr_lsf_idx =
  256. amrwbplus_config_32.amr_lsf_idx;
  257. } else {
  258. pr_err("%s: wb+ config invalid parameters\n",
  259. __func__);
  260. rc = -EFAULT;
  261. }
  262. break;
  263. }
  264. default: {
  265. pr_debug("%s[%pK]: Calling utils ioctl\n", __func__, audio);
  266. rc = audio->codec_compat_ioctl(file, cmd, arg);
  267. break;
  268. }
  269. }
  270. return rc;
  271. }
  272. #else
  273. #define audio_compat_ioctl NULL
  274. #endif
  275. static int audio_open(struct inode *inode, struct file *file)
  276. {
  277. struct q6audio_aio *audio = NULL;
  278. int rc = 0;
  279. audio = kzalloc(sizeof(struct q6audio_aio), GFP_KERNEL);
  280. if (audio == NULL)
  281. return -ENOMEM;
  282. audio->codec_cfg =
  283. kzalloc(sizeof(struct msm_audio_amrwbplus_config_v2), GFP_KERNEL);
  284. if (audio->codec_cfg == NULL) {
  285. kfree(audio);
  286. return -ENOMEM;
  287. }
  288. audio->pcm_cfg.buffer_size = PCM_BUFSZ_MIN;
  289. audio->miscdevice = &audio_amrwbplus_misc;
  290. audio->wakelock_voted = false;
  291. audio->audio_ws_mgr = &audio_amrwbplus_ws_mgr;
  292. audio->ac =
  293. q6asm_audio_client_alloc((app_cb) q6_audio_cb, (void *)audio);
  294. if (!audio->ac) {
  295. pr_err("Could not allocate memory for audio client\n");
  296. kfree(audio->codec_cfg);
  297. kfree(audio);
  298. return -ENOMEM;
  299. }
  300. rc = audio_aio_open(audio, file);
  301. if (rc < 0) {
  302. pr_err("%s: audio_aio_open rc=%d\n",
  303. __func__, rc);
  304. goto fail;
  305. }
  306. /* open in T/NT mode */
  307. if ((file->f_mode & FMODE_WRITE) && (file->f_mode & FMODE_READ)) {
  308. rc = q6asm_open_read_write(audio->ac, FORMAT_LINEAR_PCM,
  309. FORMAT_AMR_WB_PLUS);
  310. if (rc < 0) {
  311. pr_err("amrwbplus NT mode Open failed rc=%d\n", rc);
  312. rc = -ENODEV;
  313. goto fail;
  314. }
  315. audio->feedback = NON_TUNNEL_MODE;
  316. audio->buf_cfg.frames_per_buf = 0x01;
  317. audio->buf_cfg.meta_info_enable = 0x01;
  318. } else if ((file->f_mode & FMODE_WRITE) &&
  319. !(file->f_mode & FMODE_READ)) {
  320. rc = q6asm_open_write(audio->ac, FORMAT_AMR_WB_PLUS);
  321. if (rc < 0) {
  322. pr_err("wb+ T mode Open failed rc=%d\n", rc);
  323. rc = -ENODEV;
  324. goto fail;
  325. }
  326. audio->feedback = TUNNEL_MODE;
  327. audio->buf_cfg.meta_info_enable = 0x00;
  328. } else {
  329. pr_err("audio_amrwbplus Not supported mode\n");
  330. rc = -EACCES;
  331. goto fail;
  332. }
  333. config_debug_fs(audio);
  334. pr_debug("%s: AMRWBPLUS dec success mode[%d]session[%d]\n", __func__,
  335. audio->feedback,
  336. audio->ac->session);
  337. return 0;
  338. fail:
  339. q6asm_audio_client_free(audio->ac);
  340. kfree(audio->codec_cfg);
  341. kfree(audio);
  342. return rc;
  343. }
  344. static const struct file_operations audio_amrwbplus_fops = {
  345. .owner = THIS_MODULE,
  346. .open = audio_open,
  347. .release = audio_aio_release,
  348. .unlocked_ioctl = audio_ioctl,
  349. .fsync = audio_aio_fsync,
  350. .compat_ioctl = audio_compat_ioctl
  351. };
  352. static struct miscdevice audio_amrwbplus_misc = {
  353. .minor = MISC_DYNAMIC_MINOR,
  354. .name = "msm_amrwbplus",
  355. .fops = &audio_amrwbplus_fops,
  356. };
  357. static int __init audio_amrwbplus_init(void)
  358. {
  359. int ret = misc_register(&audio_amrwbplus_misc);
  360. if (ret == 0)
  361. device_init_wakeup(audio_amrwbplus_misc.this_device, true);
  362. audio_amrwbplus_ws_mgr.ref_cnt = 0;
  363. mutex_init(&audio_amrwbplus_ws_mgr.ws_lock);
  364. return ret;
  365. }
  366. device_initcall(audio_amrwbplus_init);