aac_in.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2010-2017, 2020 The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/fs.h>
  7. #include <linux/miscdevice.h>
  8. #include <linux/uaccess.h>
  9. #include <linux/sched.h>
  10. #include <linux/wait.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/slab.h>
  13. #include <audio/linux/msm_audio_aac.h>
  14. #include <linux/compat.h>
  15. #include <linux/atomic.h>
  16. #include <asm/ioctls.h>
  17. #include "audio_utils.h"
  18. /* Buffer with meta*/
  19. #define PCM_BUF_SIZE (4096 + sizeof(struct meta_in))
  20. /* Maximum 5 frames in buffer with meta */
  21. #define FRAME_SIZE (1 + ((1536+sizeof(struct meta_out_dsp)) * 5))
  22. #define AAC_FORMAT_ADTS 65535
  23. #define MAX_SAMPLE_RATE_384K 384000
  24. static long aac_in_ioctl_shared(struct file *file, unsigned int cmd, void *arg)
  25. {
  26. struct q6audio_in *audio = file->private_data;
  27. int rc = 0;
  28. int cnt = 0;
  29. switch (cmd) {
  30. case AUDIO_START: {
  31. struct msm_audio_aac_enc_config *enc_cfg;
  32. struct msm_audio_aac_config *aac_config;
  33. uint32_t aac_mode = AAC_ENC_MODE_AAC_LC;
  34. enc_cfg = audio->enc_cfg;
  35. aac_config = audio->codec_cfg;
  36. /* ENCODE CFG (after new set of API's are published )bharath*/
  37. pr_debug("%s:session id %d: default buf alloc[%d]\n", __func__,
  38. audio->ac->session, audio->buf_alloc);
  39. if (audio->enabled == 1) {
  40. pr_info("%s:AUDIO_START already over\n", __func__);
  41. rc = 0;
  42. break;
  43. }
  44. if (audio->opened) {
  45. rc = audio_in_buf_alloc(audio);
  46. if (rc < 0) {
  47. pr_err("%s:session id %d: buffer allocation failed\n",
  48. __func__, audio->ac->session);
  49. break;
  50. }
  51. } else {
  52. if (audio->feedback == NON_TUNNEL_MODE) {
  53. pr_debug("%s: starting in non_tunnel mode",
  54. __func__);
  55. rc = q6asm_open_read_write(audio->ac,
  56. FORMAT_MPEG4_AAC, FORMAT_LINEAR_PCM);
  57. if (rc < 0) {
  58. pr_err("%s:open read write failed\n",
  59. __func__);
  60. break;
  61. }
  62. }
  63. if (audio->feedback == TUNNEL_MODE) {
  64. pr_debug("%s: starting in tunnel mode",
  65. __func__);
  66. rc = q6asm_open_read(audio->ac,
  67. FORMAT_MPEG4_AAC);
  68. if (rc < 0) {
  69. pr_err("%s:open read failed\n",
  70. __func__);
  71. break;
  72. }
  73. }
  74. audio->stopped = 0;
  75. }
  76. pr_debug("%s:sbr_ps_flag = %d, sbr_flag = %d\n", __func__,
  77. aac_config->sbr_ps_on_flag, aac_config->sbr_on_flag);
  78. if (aac_config->sbr_ps_on_flag)
  79. aac_mode = AAC_ENC_MODE_EAAC_P;
  80. else if (aac_config->sbr_on_flag)
  81. aac_mode = AAC_ENC_MODE_AAC_P;
  82. else
  83. aac_mode = AAC_ENC_MODE_AAC_LC;
  84. rc = q6asm_enc_cfg_blk_aac(audio->ac,
  85. audio->buf_cfg.frames_per_buf,
  86. enc_cfg->sample_rate,
  87. enc_cfg->channels,
  88. enc_cfg->bit_rate,
  89. aac_mode,
  90. enc_cfg->stream_format);
  91. if (rc < 0) {
  92. pr_err("%s:session id %d: cmd media format block failed\n",
  93. __func__, audio->ac->session);
  94. break;
  95. }
  96. if (audio->feedback == NON_TUNNEL_MODE) {
  97. rc = q6asm_media_format_block_pcm(audio->ac,
  98. audio->pcm_cfg.sample_rate,
  99. audio->pcm_cfg.channel_count);
  100. if (rc < 0) {
  101. pr_err("%s:session id %d: media format block failed\n",
  102. __func__, audio->ac->session);
  103. break;
  104. }
  105. }
  106. rc = audio_in_enable(audio);
  107. if (!rc) {
  108. audio->enabled = 1;
  109. } else {
  110. audio->enabled = 0;
  111. pr_err("%s:session id %d: Audio Start procedure failed rc=%d\n",
  112. __func__, audio->ac->session, rc);
  113. break;
  114. }
  115. while (cnt++ < audio->str_cfg.buffer_count)
  116. q6asm_read(audio->ac);
  117. pr_debug("%s:session id %d: AUDIO_START success enable[%d]\n",
  118. __func__, audio->ac->session, audio->enabled);
  119. break;
  120. }
  121. case AUDIO_STOP: {
  122. pr_debug("%s:session id %d: Rxed AUDIO_STOP\n", __func__,
  123. audio->ac->session);
  124. rc = audio_in_disable(audio);
  125. if (rc < 0) {
  126. pr_err("%s:session id %d: Audio Stop procedure failed rc=%d\n",
  127. __func__, audio->ac->session, rc);
  128. break;
  129. }
  130. break;
  131. }
  132. case AUDIO_GET_AAC_ENC_CONFIG: {
  133. struct msm_audio_aac_enc_config *cfg;
  134. struct msm_audio_aac_enc_config *enc_cfg;
  135. cfg = (struct msm_audio_aac_enc_config *)arg;
  136. if (cfg == NULL) {
  137. pr_err("%s: NULL config pointer for %s\n",
  138. __func__, "AUDIO_GET_AAC_CONFIG");
  139. rc = -EINVAL;
  140. break;
  141. }
  142. memset(cfg, 0, sizeof(*cfg));
  143. enc_cfg = audio->enc_cfg;
  144. if (enc_cfg->channels == CH_MODE_MONO)
  145. cfg->channels = 1;
  146. else
  147. cfg->channels = 2;
  148. cfg->sample_rate = enc_cfg->sample_rate;
  149. cfg->bit_rate = enc_cfg->bit_rate;
  150. switch (enc_cfg->stream_format) {
  151. case 0x00:
  152. cfg->stream_format = AUDIO_AAC_FORMAT_ADTS;
  153. break;
  154. case 0x01:
  155. cfg->stream_format = AUDIO_AAC_FORMAT_LOAS;
  156. break;
  157. case 0x02:
  158. cfg->stream_format = AUDIO_AAC_FORMAT_ADIF;
  159. break;
  160. default:
  161. case 0x03:
  162. cfg->stream_format = AUDIO_AAC_FORMAT_RAW;
  163. }
  164. pr_debug("%s:session id %d: Get-aac-cfg: format=%d sr=%d bitrate=%d\n",
  165. __func__, audio->ac->session,
  166. cfg->stream_format, cfg->sample_rate, cfg->bit_rate);
  167. break;
  168. }
  169. case AUDIO_SET_AAC_ENC_CONFIG: {
  170. struct msm_audio_aac_enc_config *cfg;
  171. struct msm_audio_aac_enc_config *enc_cfg;
  172. uint32_t min_bitrate, max_bitrate;
  173. cfg = (struct msm_audio_aac_enc_config *)arg;
  174. if (cfg == NULL) {
  175. pr_err("%s: NULL config pointer for %s\n",
  176. "AUDIO_SET_AAC_ENC_CONFIG", __func__);
  177. rc = -EINVAL;
  178. break;
  179. }
  180. enc_cfg = audio->enc_cfg;
  181. pr_debug("%s:session id %d: Set-aac-cfg: stream=%d\n", __func__,
  182. audio->ac->session, cfg->stream_format);
  183. switch (cfg->stream_format) {
  184. case AUDIO_AAC_FORMAT_ADTS:
  185. enc_cfg->stream_format = 0x00;
  186. break;
  187. case AUDIO_AAC_FORMAT_LOAS:
  188. enc_cfg->stream_format = 0x01;
  189. break;
  190. case AUDIO_AAC_FORMAT_ADIF:
  191. enc_cfg->stream_format = 0x02;
  192. break;
  193. case AUDIO_AAC_FORMAT_RAW:
  194. enc_cfg->stream_format = 0x03;
  195. break;
  196. default:
  197. pr_err("%s:session id %d: unsupported AAC format %d\n",
  198. __func__, audio->ac->session,
  199. cfg->stream_format);
  200. rc = -EINVAL;
  201. break;
  202. }
  203. if (cfg->channels == 1) {
  204. cfg->channels = CH_MODE_MONO;
  205. } else if (cfg->channels == 2) {
  206. cfg->channels = CH_MODE_STEREO;
  207. } else {
  208. rc = -EINVAL;
  209. break;
  210. }
  211. if (cfg->sample_rate > MAX_SAMPLE_RATE_384K) {
  212. pr_err("%s: ERROR: invalid sample rate = %u",
  213. __func__, cfg->sample_rate);
  214. rc = -EINVAL;
  215. break;
  216. }
  217. min_bitrate = ((cfg->sample_rate)*(cfg->channels))/2;
  218. /* This calculation should be based on AAC mode. But we cannot
  219. * get AAC mode in this setconfig. min_bitrate's logical max
  220. * value is 24000. So if min_bitrate is higher than 24000,
  221. * choose 24000.
  222. */
  223. if (min_bitrate > 24000)
  224. min_bitrate = 24000;
  225. max_bitrate = 6*(cfg->sample_rate)*(cfg->channels);
  226. if (max_bitrate > 192000)
  227. max_bitrate = 192000;
  228. if ((cfg->bit_rate < min_bitrate) ||
  229. (cfg->bit_rate > max_bitrate)) {
  230. pr_err("%s: bitrate permissible: max=%d, min=%d\n",
  231. __func__, max_bitrate, min_bitrate);
  232. pr_err("%s: ERROR in setting bitrate = %d\n",
  233. __func__, cfg->bit_rate);
  234. rc = -EINVAL;
  235. break;
  236. }
  237. enc_cfg->sample_rate = cfg->sample_rate;
  238. enc_cfg->channels = cfg->channels;
  239. enc_cfg->bit_rate = cfg->bit_rate;
  240. pr_debug("%s:session id %d: Set-aac-cfg:SR= 0x%x ch=0x%x bitrate=0x%x, format(adts/raw) = %d\n",
  241. __func__, audio->ac->session, enc_cfg->sample_rate,
  242. enc_cfg->channels, enc_cfg->bit_rate,
  243. enc_cfg->stream_format);
  244. break;
  245. }
  246. case AUDIO_SET_AAC_CONFIG: {
  247. struct msm_audio_aac_config *aac_cfg;
  248. struct msm_audio_aac_config *audio_aac_cfg;
  249. struct msm_audio_aac_enc_config *enc_cfg;
  250. enc_cfg = audio->enc_cfg;
  251. audio_aac_cfg = audio->codec_cfg;
  252. aac_cfg = (struct msm_audio_aac_config *)arg;
  253. if (aac_cfg == NULL) {
  254. pr_err("%s: NULL config pointer %s\n",
  255. __func__, "AUDIO_SET_AAC_CONFIG");
  256. rc = -EINVAL;
  257. break;
  258. }
  259. pr_debug("%s:session id %d: AUDIO_SET_AAC_CONFIG: sbr_flag = %d sbr_ps_flag = %d\n",
  260. __func__, audio->ac->session, aac_cfg->sbr_on_flag,
  261. aac_cfg->sbr_ps_on_flag);
  262. audio_aac_cfg->sbr_on_flag = aac_cfg->sbr_on_flag;
  263. audio_aac_cfg->sbr_ps_on_flag = aac_cfg->sbr_ps_on_flag;
  264. if ((audio_aac_cfg->sbr_on_flag == 1) ||
  265. (audio_aac_cfg->sbr_ps_on_flag == 1)) {
  266. if (enc_cfg->sample_rate < 24000) {
  267. pr_err("%s: ERROR in setting samplerate = %d\n",
  268. __func__, enc_cfg->sample_rate);
  269. rc = -EINVAL;
  270. break;
  271. }
  272. }
  273. break;
  274. }
  275. default:
  276. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  277. rc = -EINVAL;
  278. }
  279. return rc;
  280. }
  281. static long aac_in_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  282. {
  283. struct q6audio_in *audio = file->private_data;
  284. int rc = 0;
  285. switch (cmd) {
  286. case AUDIO_START:
  287. case AUDIO_STOP: {
  288. rc = aac_in_ioctl_shared(file, cmd, NULL);
  289. break;
  290. }
  291. case AUDIO_GET_AAC_ENC_CONFIG: {
  292. struct msm_audio_aac_enc_config cfg;
  293. rc = aac_in_ioctl_shared(file, cmd, &cfg);
  294. if (rc) {
  295. pr_err("%s:AUDIO_GET_AAC_ENC_CONFIG failed. rc=%d\n",
  296. __func__, rc);
  297. break;
  298. }
  299. if (copy_to_user((void *)arg, &cfg, sizeof(cfg))) {
  300. pr_err("%s: copy_to_user for AUDIO_GET_AAC_ENC_CONFIG failed\n",
  301. __func__);
  302. rc = -EFAULT;
  303. }
  304. break;
  305. }
  306. case AUDIO_SET_AAC_ENC_CONFIG: {
  307. struct msm_audio_aac_enc_config cfg;
  308. if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) {
  309. pr_err("%s: copy_from_user for AUDIO_SET_AAC_ENC_CONFIG failed\n",
  310. __func__);
  311. rc = -EFAULT;
  312. break;
  313. }
  314. rc = aac_in_ioctl_shared(file, cmd, &cfg);
  315. if (rc)
  316. pr_err("%s:AUDIO_SET_AAC_ENC_CONFIG failed. rc=%d\n",
  317. __func__, rc);
  318. break;
  319. }
  320. case AUDIO_GET_AAC_CONFIG: {
  321. if (copy_to_user((void *)arg, &audio->codec_cfg,
  322. sizeof(struct msm_audio_aac_config))) {
  323. pr_err("%s: copy_to_user for AUDIO_GET_AAC_CONFIG failed\n",
  324. __func__);
  325. rc = -EFAULT;
  326. break;
  327. }
  328. break;
  329. }
  330. case AUDIO_SET_AAC_CONFIG: {
  331. struct msm_audio_aac_config aac_cfg;
  332. if (copy_from_user(&aac_cfg, (void *)arg,
  333. sizeof(struct msm_audio_aac_config))) {
  334. pr_err("%s: copy_to_user for AUDIO_SET_CONFIG failed\n",
  335. __func__);
  336. rc = -EFAULT;
  337. break;
  338. }
  339. rc = aac_in_ioctl_shared(file, cmd, &aac_cfg);
  340. if (rc)
  341. pr_err("%s:AUDIO_SET_AAC_CONFIG failed. rc=%d\n",
  342. __func__, rc);
  343. break;
  344. }
  345. default:
  346. pr_err("%s: Unknown ioctl cmd=%d\n", __func__, cmd);
  347. rc = -EINVAL;
  348. }
  349. return rc;
  350. }
  351. #ifdef CONFIG_COMPAT
  352. struct msm_audio_aac_enc_config32 {
  353. u32 channels;
  354. u32 sample_rate;
  355. u32 bit_rate;
  356. u32 stream_format;
  357. };
  358. struct msm_audio_aac_config32 {
  359. s16 format;
  360. u16 audio_object;
  361. u16 ep_config; /* 0 ~ 3 useful only obj = ERLC */
  362. u16 aac_section_data_resilience_flag;
  363. u16 aac_scalefactor_data_resilience_flag;
  364. u16 aac_spectral_data_resilience_flag;
  365. u16 sbr_on_flag;
  366. u16 sbr_ps_on_flag;
  367. u16 dual_mono_mode;
  368. u16 channel_configuration;
  369. u16 sample_rate;
  370. };
  371. enum {
  372. AUDIO_SET_AAC_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC,
  373. (AUDIO_MAX_COMMON_IOCTL_NUM+0), struct msm_audio_aac_config32),
  374. AUDIO_GET_AAC_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC,
  375. (AUDIO_MAX_COMMON_IOCTL_NUM+1), struct msm_audio_aac_config32),
  376. AUDIO_SET_AAC_ENC_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC,
  377. (AUDIO_MAX_COMMON_IOCTL_NUM+3), struct msm_audio_aac_enc_config32),
  378. AUDIO_GET_AAC_ENC_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC,
  379. (AUDIO_MAX_COMMON_IOCTL_NUM+4), struct msm_audio_aac_enc_config32)
  380. };
  381. static long aac_in_compat_ioctl(struct file *file, unsigned int cmd,
  382. unsigned long arg)
  383. {
  384. struct q6audio_in *audio = file->private_data;
  385. int rc = 0;
  386. switch (cmd) {
  387. case AUDIO_START:
  388. case AUDIO_STOP: {
  389. rc = aac_in_ioctl_shared(file, cmd, NULL);
  390. break;
  391. }
  392. case AUDIO_GET_AAC_ENC_CONFIG_32: {
  393. struct msm_audio_aac_enc_config cfg;
  394. struct msm_audio_aac_enc_config32 cfg_32;
  395. memset(&cfg_32, 0, sizeof(cfg_32));
  396. cmd = AUDIO_GET_AAC_ENC_CONFIG;
  397. rc = aac_in_ioctl_shared(file, cmd, &cfg);
  398. if (rc) {
  399. pr_err("%s:AUDIO_GET_AAC_ENC_CONFIG_32 failed. Rc= %d\n",
  400. __func__, rc);
  401. break;
  402. }
  403. cfg_32.channels = cfg.channels;
  404. cfg_32.sample_rate = cfg.sample_rate;
  405. cfg_32.bit_rate = cfg.bit_rate;
  406. cfg_32.stream_format = cfg.stream_format;
  407. if (copy_to_user((void *)arg, &cfg_32, sizeof(cfg_32))) {
  408. pr_err("%s: copy_to_user for AUDIO_GET_AAC_ENC_CONFIG_32 failed\n",
  409. __func__);
  410. rc = -EFAULT;
  411. }
  412. break;
  413. }
  414. case AUDIO_SET_AAC_ENC_CONFIG_32: {
  415. struct msm_audio_aac_enc_config cfg;
  416. struct msm_audio_aac_enc_config32 cfg_32;
  417. if (copy_from_user(&cfg_32, (void *)arg, sizeof(cfg_32))) {
  418. pr_err("%s: copy_from_user for AUDIO_GET_AAC_ENC_CONFIG_32 failed\n",
  419. __func__);
  420. rc = -EFAULT;
  421. break;
  422. }
  423. cfg.channels = cfg_32.channels;
  424. cfg.sample_rate = cfg_32.sample_rate;
  425. cfg.bit_rate = cfg_32.bit_rate;
  426. cfg.stream_format = cfg_32.stream_format;
  427. /* The command should be converted from 32 bit to normal
  428. * before the shared ioctl is called as shared ioctl
  429. * can process only normal commands
  430. */
  431. cmd = AUDIO_SET_AAC_ENC_CONFIG;
  432. rc = aac_in_ioctl_shared(file, cmd, &cfg);
  433. if (rc)
  434. pr_err("%s:AUDIO_SET_AAC_ENC_CONFIG_32 failed. rc=%d\n",
  435. __func__, rc);
  436. break;
  437. }
  438. case AUDIO_GET_AAC_CONFIG_32: {
  439. struct msm_audio_aac_config *aac_config;
  440. struct msm_audio_aac_config32 aac_config_32;
  441. aac_config = (struct msm_audio_aac_config *)audio->codec_cfg;
  442. aac_config_32.format = aac_config->format;
  443. aac_config_32.audio_object = aac_config->audio_object;
  444. aac_config_32.ep_config = aac_config->ep_config;
  445. aac_config_32.aac_section_data_resilience_flag =
  446. aac_config->aac_section_data_resilience_flag;
  447. aac_config_32.aac_scalefactor_data_resilience_flag =
  448. aac_config->aac_scalefactor_data_resilience_flag;
  449. aac_config_32.aac_spectral_data_resilience_flag =
  450. aac_config->aac_spectral_data_resilience_flag;
  451. aac_config_32.sbr_on_flag = aac_config->sbr_on_flag;
  452. aac_config_32.sbr_ps_on_flag = aac_config->sbr_ps_on_flag;
  453. aac_config_32.dual_mono_mode = aac_config->dual_mono_mode;
  454. aac_config_32.channel_configuration =
  455. aac_config->channel_configuration;
  456. aac_config_32.sample_rate = aac_config->sample_rate;
  457. if (copy_to_user((void *)arg, &aac_config_32,
  458. sizeof(aac_config_32))) {
  459. pr_err("%s: copy_to_user for AUDIO_GET_AAC_CONFIG_32 failed\n",
  460. __func__);
  461. rc = -EFAULT;
  462. break;
  463. }
  464. break;
  465. }
  466. case AUDIO_SET_AAC_CONFIG_32: {
  467. struct msm_audio_aac_config aac_cfg;
  468. struct msm_audio_aac_config32 aac_cfg_32;
  469. if (copy_from_user(&aac_cfg_32, (void *)arg,
  470. sizeof(aac_cfg_32))) {
  471. pr_err("%s: copy_from_user for AUDIO_SET_AAC_CONFIG_32 failed\n",
  472. __func__);
  473. rc = -EFAULT;
  474. break;
  475. }
  476. aac_cfg.format = aac_cfg_32.format;
  477. aac_cfg.audio_object = aac_cfg_32.audio_object;
  478. aac_cfg.ep_config = aac_cfg_32.ep_config;
  479. aac_cfg.aac_section_data_resilience_flag =
  480. aac_cfg_32.aac_section_data_resilience_flag;
  481. aac_cfg.aac_scalefactor_data_resilience_flag =
  482. aac_cfg_32.aac_scalefactor_data_resilience_flag;
  483. aac_cfg.aac_spectral_data_resilience_flag =
  484. aac_cfg_32.aac_spectral_data_resilience_flag;
  485. aac_cfg.sbr_on_flag = aac_cfg_32.sbr_on_flag;
  486. aac_cfg.sbr_ps_on_flag = aac_cfg_32.sbr_ps_on_flag;
  487. aac_cfg.dual_mono_mode = aac_cfg_32.dual_mono_mode;
  488. aac_cfg.channel_configuration =
  489. aac_cfg_32.channel_configuration;
  490. aac_cfg.sample_rate = aac_cfg_32.sample_rate;
  491. cmd = AUDIO_SET_AAC_CONFIG;
  492. rc = aac_in_ioctl_shared(file, cmd, &aac_cfg);
  493. if (rc)
  494. pr_err("%s:AUDIO_SET_AAC_CONFIG failed. Rc= %d\n",
  495. __func__, rc);
  496. break;
  497. }
  498. default:
  499. pr_err("%s: Unknown ioctl cmd = %d\n", __func__, cmd);
  500. rc = -EINVAL;
  501. }
  502. return rc;
  503. }
  504. #else
  505. #define aac_in_compat_ioctl NULL
  506. #endif
  507. static int aac_in_open(struct inode *inode, struct file *file)
  508. {
  509. struct q6audio_in *audio = NULL;
  510. struct msm_audio_aac_enc_config *enc_cfg;
  511. struct msm_audio_aac_config *aac_config;
  512. int rc = 0;
  513. audio = kzalloc(sizeof(struct q6audio_in), GFP_KERNEL);
  514. if (audio == NULL)
  515. return -ENOMEM;
  516. /* Allocate memory for encoder config param */
  517. audio->enc_cfg = kzalloc(sizeof(struct msm_audio_aac_enc_config),
  518. GFP_KERNEL);
  519. if (audio->enc_cfg == NULL) {
  520. kfree(audio);
  521. return -ENOMEM;
  522. }
  523. enc_cfg = audio->enc_cfg;
  524. audio->codec_cfg = kzalloc(sizeof(struct msm_audio_aac_config),
  525. GFP_KERNEL);
  526. if (audio->codec_cfg == NULL) {
  527. kfree(audio->enc_cfg);
  528. kfree(audio);
  529. return -ENOMEM;
  530. }
  531. aac_config = audio->codec_cfg;
  532. mutex_init(&audio->lock);
  533. mutex_init(&audio->read_lock);
  534. mutex_init(&audio->write_lock);
  535. spin_lock_init(&audio->dsp_lock);
  536. init_waitqueue_head(&audio->read_wait);
  537. init_waitqueue_head(&audio->write_wait);
  538. /* Settings will be re-config at AUDIO_SET_CONFIG,
  539. * but at least we need to have initial config
  540. */
  541. audio->str_cfg.buffer_size = FRAME_SIZE;
  542. audio->str_cfg.buffer_count = FRAME_NUM;
  543. audio->min_frame_size = 1536;
  544. audio->max_frames_per_buf = 5;
  545. enc_cfg->sample_rate = 8000;
  546. enc_cfg->channels = 1;
  547. enc_cfg->bit_rate = 16000;
  548. enc_cfg->stream_format = 0x00;/* 0:ADTS, 3:RAW */
  549. audio->buf_cfg.meta_info_enable = 0x01;
  550. audio->buf_cfg.frames_per_buf = 0x01;
  551. audio->pcm_cfg.buffer_count = PCM_BUF_COUNT;
  552. audio->pcm_cfg.buffer_size = PCM_BUF_SIZE;
  553. aac_config->format = AUDIO_AAC_FORMAT_ADTS;
  554. aac_config->audio_object = AUDIO_AAC_OBJECT_LC;
  555. aac_config->sbr_on_flag = 0;
  556. aac_config->sbr_ps_on_flag = 0;
  557. aac_config->channel_configuration = 1;
  558. audio->ac = q6asm_audio_client_alloc((app_cb)q6asm_in_cb,
  559. (void *)audio);
  560. if (!audio->ac) {
  561. pr_err("%s: Could not allocate memory for audio client\n",
  562. __func__);
  563. kfree(audio->enc_cfg);
  564. kfree(audio->codec_cfg);
  565. kfree(audio);
  566. return -ENOMEM;
  567. }
  568. /* open aac encoder in tunnel mode */
  569. audio->buf_cfg.frames_per_buf = 0x01;
  570. if ((file->f_mode & FMODE_WRITE) &&
  571. (file->f_mode & FMODE_READ)) {
  572. audio->feedback = NON_TUNNEL_MODE;
  573. rc = q6asm_open_read_write(audio->ac, FORMAT_MPEG4_AAC,
  574. FORMAT_LINEAR_PCM);
  575. if (rc < 0) {
  576. pr_err("%s:session id %d: NT Open failed rc=%d\n",
  577. __func__, audio->ac->session, rc);
  578. rc = -ENODEV;
  579. goto fail;
  580. }
  581. audio->buf_cfg.meta_info_enable = 0x01;
  582. pr_info("%s:session id %d: NT mode encoder success\n", __func__,
  583. audio->ac->session);
  584. } else if (!(file->f_mode & FMODE_WRITE) &&
  585. (file->f_mode & FMODE_READ)) {
  586. audio->feedback = TUNNEL_MODE;
  587. rc = q6asm_open_read(audio->ac, FORMAT_MPEG4_AAC);
  588. if (rc < 0) {
  589. pr_err("%s:session id %d: Tunnel Open failed rc=%d\n",
  590. __func__, audio->ac->session, rc);
  591. rc = -ENODEV;
  592. goto fail;
  593. }
  594. /* register for tx overflow (valid for tunnel mode only) */
  595. rc = q6asm_reg_tx_overflow(audio->ac, 0x01);
  596. if (rc < 0) {
  597. pr_err("%s:session id %d: TX Overflow registration failed rc=%d\n",
  598. __func__,
  599. audio->ac->session, rc);
  600. rc = -ENODEV;
  601. goto fail;
  602. }
  603. audio->buf_cfg.meta_info_enable = 0x00;
  604. pr_info("%s:session id %d: T mode encoder success\n", __func__,
  605. audio->ac->session);
  606. } else {
  607. pr_err("%s:session id %d: Unexpected mode\n", __func__,
  608. audio->ac->session);
  609. rc = -EACCES;
  610. goto fail;
  611. }
  612. audio->opened = 1;
  613. audio->reset_event = false;
  614. atomic_set(&audio->in_count, PCM_BUF_COUNT);
  615. atomic_set(&audio->out_count, 0x00);
  616. audio->enc_compat_ioctl = aac_in_compat_ioctl;
  617. audio->enc_ioctl = aac_in_ioctl;
  618. file->private_data = audio;
  619. pr_info("%s:session id %d: success\n", __func__, audio->ac->session);
  620. return 0;
  621. fail:
  622. q6asm_audio_client_free(audio->ac);
  623. kfree(audio->enc_cfg);
  624. kfree(audio->codec_cfg);
  625. kfree(audio);
  626. return rc;
  627. }
  628. static const struct file_operations audio_in_fops = {
  629. .owner = THIS_MODULE,
  630. .open = aac_in_open,
  631. .release = audio_in_release,
  632. .read = audio_in_read,
  633. .write = audio_in_write,
  634. .unlocked_ioctl = audio_in_ioctl,
  635. .compat_ioctl = audio_in_compat_ioctl
  636. };
  637. struct miscdevice audio_aac_in_misc = {
  638. .minor = MISC_DYNAMIC_MINOR,
  639. .name = "msm_aac_in",
  640. .fops = &audio_in_fops,
  641. };
  642. int __init aac_in_init(void)
  643. {
  644. return misc_register(&audio_aac_in_misc);
  645. }
  646. void aac_in_exit(void)
  647. {
  648. misc_deregister(&audio_aac_in_misc);
  649. }