audio_aac.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /* aac 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_aac.h>
  18. #include <linux/compat.h>
  19. #include "audio_utils_aio.h"
  20. #define AUDIO_AAC_DUAL_MONO_INVALID -1
  21. #define PCM_BUFSZ_MIN_AAC ((8*1024) + sizeof(struct dec_meta_out))
  22. static struct miscdevice audio_aac_misc;
  23. static struct ws_mgr audio_aac_ws_mgr;
  24. #ifdef CONFIG_DEBUG_FS
  25. static const struct file_operations audio_aac_debug_fops = {
  26. .read = audio_aio_debug_read,
  27. .open = audio_aio_debug_open,
  28. };
  29. #endif
  30. static long audio_ioctl_shared(struct file *file, unsigned int cmd,
  31. void *arg)
  32. {
  33. struct q6audio_aio *audio = file->private_data;
  34. int rc = 0;
  35. switch (cmd) {
  36. case AUDIO_START: {
  37. struct asm_aac_cfg aac_cfg;
  38. struct msm_audio_aac_config *aac_config;
  39. uint32_t sbr_ps = 0x00;
  40. pr_debug("%s: AUDIO_START session_id[%d]\n", __func__,
  41. audio->ac->session);
  42. if (audio->feedback == NON_TUNNEL_MODE) {
  43. /* Configure PCM output block */
  44. rc = q6asm_enc_cfg_blk_pcm(audio->ac, 0, 0);
  45. if (rc < 0) {
  46. pr_err("pcm output block config failed\n");
  47. break;
  48. }
  49. }
  50. /* turn on both sbr and ps */
  51. rc = q6asm_enable_sbrps(audio->ac, sbr_ps);
  52. if (rc < 0)
  53. pr_err("sbr-ps enable failed\n");
  54. aac_config = (struct msm_audio_aac_config *)audio->codec_cfg;
  55. if (aac_config->sbr_ps_on_flag)
  56. aac_cfg.aot = AAC_ENC_MODE_EAAC_P;
  57. else if (aac_config->sbr_on_flag)
  58. aac_cfg.aot = AAC_ENC_MODE_AAC_P;
  59. else
  60. aac_cfg.aot = AAC_ENC_MODE_AAC_LC;
  61. switch (aac_config->format) {
  62. case AUDIO_AAC_FORMAT_ADTS:
  63. aac_cfg.format = 0x00;
  64. break;
  65. case AUDIO_AAC_FORMAT_LOAS:
  66. aac_cfg.format = 0x01;
  67. break;
  68. case AUDIO_AAC_FORMAT_ADIF:
  69. aac_cfg.format = 0x02;
  70. break;
  71. default:
  72. case AUDIO_AAC_FORMAT_RAW:
  73. aac_cfg.format = 0x03;
  74. }
  75. aac_cfg.ep_config = aac_config->ep_config;
  76. aac_cfg.section_data_resilience =
  77. aac_config->aac_section_data_resilience_flag;
  78. aac_cfg.scalefactor_data_resilience =
  79. aac_config->aac_scalefactor_data_resilience_flag;
  80. aac_cfg.spectral_data_resilience =
  81. aac_config->aac_spectral_data_resilience_flag;
  82. aac_cfg.ch_cfg = audio->pcm_cfg.channel_count;
  83. if (audio->feedback == TUNNEL_MODE) {
  84. aac_cfg.sample_rate = aac_config->sample_rate;
  85. aac_cfg.ch_cfg = aac_config->channel_configuration;
  86. } else {
  87. aac_cfg.sample_rate = audio->pcm_cfg.sample_rate;
  88. aac_cfg.ch_cfg = audio->pcm_cfg.channel_count;
  89. }
  90. pr_debug("%s:format=%x aot=%d ch=%d sr=%d\n",
  91. __func__, aac_cfg.format,
  92. aac_cfg.aot, aac_cfg.ch_cfg,
  93. aac_cfg.sample_rate);
  94. /* Configure Media format block */
  95. rc = q6asm_media_format_block_aac(audio->ac, &aac_cfg);
  96. if (rc < 0) {
  97. pr_err("cmd media format block failed\n");
  98. break;
  99. }
  100. rc = audio_aio_enable(audio);
  101. audio->eos_rsp = 0;
  102. audio->eos_flag = 0;
  103. if (!rc) {
  104. rc = enable_volume_ramp(audio);
  105. if (rc < 0) {
  106. pr_err("%s: Failed to enable volume ramp\n",
  107. __func__);
  108. }
  109. audio->enabled = 1;
  110. } else {
  111. audio->enabled = 0;
  112. pr_err("Audio Start procedure failed rc=%d\n", rc);
  113. break;
  114. }
  115. pr_info("%s: AUDIO_START sessionid[%d]enable[%d]\n", __func__,
  116. audio->ac->session,
  117. audio->enabled);
  118. if (audio->stopped == 1)
  119. audio->stopped = 0;
  120. break;
  121. }
  122. case AUDIO_SET_AAC_CONFIG: {
  123. struct msm_audio_aac_config *aac_config;
  124. uint16_t sce_left = 1, sce_right = 2;
  125. pr_debug("%s: AUDIO_SET_AAC_CONFIG\n", __func__);
  126. aac_config = (struct msm_audio_aac_config *)arg;
  127. if (aac_config == NULL) {
  128. pr_err("%s: Invalid config pointer\n", __func__);
  129. rc = -EINVAL;
  130. break;
  131. }
  132. memcpy(audio->codec_cfg, aac_config,
  133. sizeof(struct msm_audio_aac_config));
  134. /* PL_PR is 0 only need to check PL_SR */
  135. if (aac_config->dual_mono_mode >
  136. AUDIO_AAC_DUAL_MONO_PL_SR) {
  137. pr_err("%s:Invalid dual_mono mode =%d\n", __func__,
  138. aac_config->dual_mono_mode);
  139. } else {
  140. /* convert the data from user into sce_left
  141. * and sce_right based on the definitions
  142. */
  143. pr_debug("%s: modify dual_mono mode =%d\n", __func__,
  144. aac_config->dual_mono_mode);
  145. switch (aac_config->dual_mono_mode) {
  146. case AUDIO_AAC_DUAL_MONO_PL_PR:
  147. sce_left = 1;
  148. sce_right = 1;
  149. break;
  150. case AUDIO_AAC_DUAL_MONO_SL_SR:
  151. sce_left = 2;
  152. sce_right = 2;
  153. break;
  154. case AUDIO_AAC_DUAL_MONO_SL_PR:
  155. sce_left = 2;
  156. sce_right = 1;
  157. break;
  158. case AUDIO_AAC_DUAL_MONO_PL_SR:
  159. default:
  160. sce_left = 1;
  161. sce_right = 2;
  162. break;
  163. }
  164. rc = q6asm_cfg_dual_mono_aac(audio->ac,
  165. sce_left, sce_right);
  166. if (rc < 0)
  167. pr_err("%s:asm cmd dualmono failed rc=%d\n",
  168. __func__, rc);
  169. }
  170. break;
  171. }
  172. default:
  173. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  174. break;
  175. }
  176. return rc;
  177. }
  178. static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  179. {
  180. struct q6audio_aio *audio = file->private_data;
  181. int rc = 0;
  182. switch (cmd) {
  183. case AUDIO_START: {
  184. rc = audio_ioctl_shared(file, cmd, (void *)arg);
  185. break;
  186. }
  187. case AUDIO_GET_AAC_CONFIG: {
  188. if (copy_to_user((void *)arg, audio->codec_cfg,
  189. sizeof(struct msm_audio_aac_config))) {
  190. pr_err("%s: copy_to_user for AUDIO_GET_AAC_CONFIG failed\n",
  191. __func__);
  192. rc = -EFAULT;
  193. break;
  194. }
  195. break;
  196. }
  197. case AUDIO_SET_AAC_CONFIG: {
  198. struct msm_audio_aac_config aac_config;
  199. pr_debug("%s: AUDIO_SET_AAC_CONFIG\n", __func__);
  200. if (copy_from_user(&aac_config, (void *)arg,
  201. sizeof(aac_config))) {
  202. pr_err("%s: copy_from_user for AUDIO_SET_AAC_CONFIG failed\n",
  203. __func__);
  204. rc = -EFAULT;
  205. break;
  206. }
  207. rc = audio_ioctl_shared(file, cmd, &aac_config);
  208. if (rc)
  209. pr_err("%s:AUDIO_SET_AAC_CONFIG failed. Rc= %d\n",
  210. __func__, rc);
  211. break;
  212. }
  213. default: {
  214. pr_debug("%s[%pK]: Calling utils ioctl\n", __func__, audio);
  215. rc = audio->codec_ioctl(file, cmd, arg);
  216. if (rc)
  217. pr_err("%s[%pK]:Failed in utils_ioctl: %d\n",
  218. __func__, audio, rc);
  219. }
  220. }
  221. return rc;
  222. }
  223. #ifdef CONFIG_COMPAT
  224. struct msm_audio_aac_config32 {
  225. s16 format;
  226. u16 audio_object;
  227. u16 ep_config; /* 0 ~ 3 useful only obj = ERLC */
  228. u16 aac_section_data_resilience_flag;
  229. u16 aac_scalefactor_data_resilience_flag;
  230. u16 aac_spectral_data_resilience_flag;
  231. u16 sbr_on_flag;
  232. u16 sbr_ps_on_flag;
  233. u16 dual_mono_mode;
  234. u16 channel_configuration;
  235. u16 sample_rate;
  236. };
  237. enum {
  238. AUDIO_SET_AAC_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC,
  239. (AUDIO_MAX_COMMON_IOCTL_NUM+0), struct msm_audio_aac_config32),
  240. AUDIO_GET_AAC_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC,
  241. (AUDIO_MAX_COMMON_IOCTL_NUM+1), struct msm_audio_aac_config32)
  242. };
  243. static long audio_compat_ioctl(struct file *file, unsigned int cmd,
  244. unsigned long arg)
  245. {
  246. struct q6audio_aio *audio = file->private_data;
  247. int rc = 0;
  248. switch (cmd) {
  249. case AUDIO_START: {
  250. rc = audio_ioctl_shared(file, cmd, (void *)arg);
  251. break;
  252. }
  253. case AUDIO_GET_AAC_CONFIG_32: {
  254. struct msm_audio_aac_config *aac_config;
  255. struct msm_audio_aac_config32 aac_config_32;
  256. aac_config = (struct msm_audio_aac_config *)audio->codec_cfg;
  257. aac_config_32.format = aac_config->format;
  258. aac_config_32.audio_object = aac_config->audio_object;
  259. aac_config_32.ep_config = aac_config->ep_config;
  260. aac_config_32.aac_section_data_resilience_flag =
  261. aac_config->aac_section_data_resilience_flag;
  262. aac_config_32.aac_scalefactor_data_resilience_flag =
  263. aac_config->aac_scalefactor_data_resilience_flag;
  264. aac_config_32.aac_spectral_data_resilience_flag =
  265. aac_config->aac_spectral_data_resilience_flag;
  266. aac_config_32.sbr_on_flag = aac_config->sbr_on_flag;
  267. aac_config_32.sbr_ps_on_flag = aac_config->sbr_ps_on_flag;
  268. aac_config_32.dual_mono_mode = aac_config->dual_mono_mode;
  269. aac_config_32.channel_configuration =
  270. aac_config->channel_configuration;
  271. aac_config_32.sample_rate = aac_config->sample_rate;
  272. if (copy_to_user((void *)arg, &aac_config_32,
  273. sizeof(aac_config_32))) {
  274. pr_err("%s: copy_to_user for AUDIO_GET_AAC_CONFIG_32 failed\n",
  275. __func__);
  276. rc = -EFAULT;
  277. break;
  278. }
  279. break;
  280. }
  281. case AUDIO_SET_AAC_CONFIG_32: {
  282. struct msm_audio_aac_config aac_config;
  283. struct msm_audio_aac_config32 aac_config_32;
  284. pr_debug("%s: AUDIO_SET_AAC_CONFIG\n", __func__);
  285. if (copy_from_user(&aac_config_32, (void *)arg,
  286. sizeof(aac_config_32))) {
  287. pr_err("%s: copy_from_user for AUDIO_SET_AAC_CONFIG_32 failed\n",
  288. __func__);
  289. rc = -EFAULT;
  290. break;
  291. }
  292. aac_config.format = aac_config_32.format;
  293. aac_config.audio_object = aac_config_32.audio_object;
  294. aac_config.ep_config = aac_config_32.ep_config;
  295. aac_config.aac_section_data_resilience_flag =
  296. aac_config_32.aac_section_data_resilience_flag;
  297. aac_config.aac_scalefactor_data_resilience_flag =
  298. aac_config_32.aac_scalefactor_data_resilience_flag;
  299. aac_config.aac_spectral_data_resilience_flag =
  300. aac_config_32.aac_spectral_data_resilience_flag;
  301. aac_config.sbr_on_flag = aac_config_32.sbr_on_flag;
  302. aac_config.sbr_ps_on_flag = aac_config_32.sbr_ps_on_flag;
  303. aac_config.dual_mono_mode = aac_config_32.dual_mono_mode;
  304. aac_config.channel_configuration =
  305. aac_config_32.channel_configuration;
  306. aac_config.sample_rate = aac_config_32.sample_rate;
  307. cmd = AUDIO_SET_AAC_CONFIG;
  308. rc = audio_ioctl_shared(file, cmd, &aac_config);
  309. if (rc)
  310. pr_err("%s:AUDIO_SET_AAC_CONFIG failed. Rc= %d\n",
  311. __func__, rc);
  312. break;
  313. }
  314. default: {
  315. pr_debug("%s[%pK]: Calling utils ioctl\n", __func__, audio);
  316. rc = audio->codec_compat_ioctl(file, cmd, arg);
  317. if (rc)
  318. pr_err("%s[%pK]:Failed in utils_ioctl: %d\n",
  319. __func__, audio, rc);
  320. }
  321. }
  322. return rc;
  323. }
  324. #else
  325. #define audio_compat_ioctl NULL
  326. #endif
  327. static int audio_open(struct inode *inode, struct file *file)
  328. {
  329. struct q6audio_aio *audio = NULL;
  330. int rc = 0;
  331. struct msm_audio_aac_config *aac_config = NULL;
  332. #ifdef CONFIG_DEBUG_FS
  333. /* 4 bytes represents decoder number, 1 byte for terminate string */
  334. char name[sizeof "msm_aac_" + 5];
  335. #endif
  336. audio = kzalloc(sizeof(struct q6audio_aio), GFP_KERNEL);
  337. if (audio == NULL)
  338. return -ENOMEM;
  339. audio->codec_cfg = kzalloc(sizeof(struct msm_audio_aac_config),
  340. GFP_KERNEL);
  341. if (audio->codec_cfg == NULL) {
  342. kfree(audio);
  343. return -ENOMEM;
  344. }
  345. aac_config = audio->codec_cfg;
  346. /* Settings will be re-config at AUDIO_SET_CONFIG,
  347. * but at least we need to have initial config
  348. */
  349. audio->pcm_cfg.buffer_size = PCM_BUFSZ_MIN_AAC;
  350. audio->miscdevice = &audio_aac_misc;
  351. audio->wakelock_voted = false;
  352. audio->audio_ws_mgr = &audio_aac_ws_mgr;
  353. aac_config->dual_mono_mode = AUDIO_AAC_DUAL_MONO_INVALID;
  354. audio->ac = q6asm_audio_client_alloc((app_cb) q6_audio_cb,
  355. (void *)audio);
  356. if (!audio->ac) {
  357. pr_err("Could not allocate memory for audio client\n");
  358. kfree(audio->codec_cfg);
  359. kfree(audio);
  360. return -ENOMEM;
  361. }
  362. rc = audio_aio_open(audio, file);
  363. if (rc < 0) {
  364. pr_err("%s: audio_aio_open rc=%d\n",
  365. __func__, rc);
  366. goto fail;
  367. }
  368. /* open in T/NT mode */
  369. if ((file->f_mode & FMODE_WRITE) && (file->f_mode & FMODE_READ)) {
  370. rc = q6asm_open_read_write(audio->ac, FORMAT_LINEAR_PCM,
  371. FORMAT_MPEG4_AAC);
  372. if (rc < 0) {
  373. pr_err("NT mode Open failed rc=%d\n", rc);
  374. rc = -ENODEV;
  375. goto fail;
  376. }
  377. audio->feedback = NON_TUNNEL_MODE;
  378. /* open AAC decoder, expected frames is always 1
  379. * audio->buf_cfg.frames_per_buf = 0x01;
  380. */
  381. audio->buf_cfg.meta_info_enable = 0x01;
  382. } else if ((file->f_mode & FMODE_WRITE) &&
  383. !(file->f_mode & FMODE_READ)) {
  384. rc = q6asm_open_write(audio->ac, FORMAT_MPEG4_AAC);
  385. if (rc < 0) {
  386. pr_err("T mode Open failed rc=%d\n", rc);
  387. rc = -ENODEV;
  388. goto fail;
  389. }
  390. audio->feedback = TUNNEL_MODE;
  391. audio->buf_cfg.meta_info_enable = 0x00;
  392. } else {
  393. pr_err("Not supported mode\n");
  394. rc = -EACCES;
  395. goto fail;
  396. }
  397. #ifdef CONFIG_DEBUG_FS
  398. snprintf(name, sizeof(name), "msm_aac_%04x", audio->ac->session);
  399. audio->dentry = debugfs_create_file(name, S_IFREG | 0444,
  400. NULL, (void *)audio,
  401. &audio_aac_debug_fops);
  402. if (IS_ERR(audio->dentry))
  403. pr_debug("debugfs_create_file failed\n");
  404. #endif
  405. pr_info("%s:aacdec success mode[%d]session[%d]\n", __func__,
  406. audio->feedback,
  407. audio->ac->session);
  408. return rc;
  409. fail:
  410. q6asm_audio_client_free(audio->ac);
  411. kfree(audio->codec_cfg);
  412. kfree(audio);
  413. return rc;
  414. }
  415. static const struct file_operations audio_aac_fops = {
  416. .owner = THIS_MODULE,
  417. .open = audio_open,
  418. .release = audio_aio_release,
  419. .unlocked_ioctl = audio_ioctl,
  420. .fsync = audio_aio_fsync,
  421. .compat_ioctl = audio_compat_ioctl
  422. };
  423. static struct miscdevice audio_aac_misc = {
  424. .minor = MISC_DYNAMIC_MINOR,
  425. .name = "msm_aac",
  426. .fops = &audio_aac_fops,
  427. };
  428. static int __init audio_aac_init(void)
  429. {
  430. int ret = misc_register(&audio_aac_misc);
  431. if (ret == 0)
  432. device_init_wakeup(audio_aac_misc.this_device, true);
  433. audio_aac_ws_mgr.ref_cnt = 0;
  434. mutex_init(&audio_aac_ws_mgr.ws_lock);
  435. return ret;
  436. }
  437. device_initcall(audio_aac_init);