amrnb_in.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Copyright (c) 2010-2012, 2014, 2016-2017 The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/fs.h>
  15. #include <linux/miscdevice.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/wait.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/msm_audio_amrnb.h>
  22. #include <linux/compat.h>
  23. #include <linux/atomic.h>
  24. #include <asm/ioctls.h>
  25. #include "audio_utils.h"
  26. /* Buffer with meta*/
  27. #define PCM_BUF_SIZE (4096 + sizeof(struct meta_in))
  28. /* Maximum 10 frames in buffer with meta */
  29. #define FRAME_SIZE (1 + ((32+sizeof(struct meta_out_dsp)) * 10))
  30. static long amrnb_in_ioctl_shared(struct file *file,
  31. unsigned int cmd, void *arg)
  32. {
  33. struct q6audio_in *audio = file->private_data;
  34. int rc = 0;
  35. int cnt = 0;
  36. switch (cmd) {
  37. case AUDIO_START: {
  38. struct msm_audio_amrnb_enc_config_v2 *enc_cfg;
  39. enc_cfg = audio->enc_cfg;
  40. pr_debug("%s:session id %d: default buf alloc[%d]\n", __func__,
  41. audio->ac->session, audio->buf_alloc);
  42. if (audio->enabled == 1) {
  43. pr_info("%s:AUDIO_START already over\n", __func__);
  44. rc = 0;
  45. break;
  46. }
  47. rc = audio_in_buf_alloc(audio);
  48. if (rc < 0) {
  49. pr_err("%s:session id %d: buffer allocation failed\n",
  50. __func__, audio->ac->session);
  51. break;
  52. }
  53. rc = q6asm_enc_cfg_blk_amrnb(audio->ac,
  54. audio->buf_cfg.frames_per_buf,
  55. enc_cfg->band_mode,
  56. enc_cfg->dtx_enable);
  57. if (rc < 0) {
  58. pr_err("%s:session id %d: cmd amrnb media format block failed\n",
  59. __func__, audio->ac->session);
  60. break;
  61. }
  62. if (audio->feedback == NON_TUNNEL_MODE) {
  63. rc = q6asm_media_format_block_pcm(audio->ac,
  64. audio->pcm_cfg.sample_rate,
  65. audio->pcm_cfg.channel_count);
  66. if (rc < 0) {
  67. pr_err("%s:session id %d: media format block failed\n",
  68. __func__, audio->ac->session);
  69. break;
  70. }
  71. }
  72. pr_debug("%s:session id %d: AUDIO_START enable[%d]\n",
  73. __func__, audio->ac->session,
  74. audio->enabled);
  75. rc = audio_in_enable(audio);
  76. if (!rc) {
  77. audio->enabled = 1;
  78. } else {
  79. audio->enabled = 0;
  80. pr_err("%s:session id %d: Audio Start procedure failed rc=%d\n",
  81. __func__, audio->ac->session, rc);
  82. break;
  83. }
  84. while (cnt++ < audio->str_cfg.buffer_count)
  85. q6asm_read(audio->ac); /* Push buffer to DSP */
  86. rc = 0;
  87. pr_debug("%s:session id %d: AUDIO_START success enable[%d]\n",
  88. __func__, audio->ac->session, audio->enabled);
  89. break;
  90. }
  91. case AUDIO_STOP: {
  92. pr_debug("%s:AUDIO_STOP\n", __func__);
  93. rc = audio_in_disable(audio);
  94. if (rc < 0) {
  95. pr_err("%s:session id %d: Audio Stop procedure failed rc=%d\n",
  96. __func__, audio->ac->session, rc);
  97. break;
  98. }
  99. break;
  100. }
  101. case AUDIO_SET_AMRNB_ENC_CONFIG_V2: {
  102. struct msm_audio_amrnb_enc_config_v2 *cfg;
  103. struct msm_audio_amrnb_enc_config_v2 *enc_cfg;
  104. cfg = (struct msm_audio_amrnb_enc_config_v2 *)arg;
  105. if (cfg == NULL) {
  106. pr_err("%s: NULL config pointer for %s\n",
  107. __func__,
  108. "AUDIO_SET_AMRNB_ENC_CONFIG_V2");
  109. rc = -EINVAL;
  110. break;
  111. }
  112. enc_cfg = audio->enc_cfg;
  113. if (cfg->band_mode > 8 ||
  114. cfg->band_mode < 1) {
  115. pr_err("%s:session id %d: invalid band mode\n",
  116. __func__, audio->ac->session);
  117. rc = -EINVAL;
  118. break;
  119. }
  120. /* AMR NB encoder accepts values between 0-7
  121. * while openmax provides value between 1-8
  122. * as per spec
  123. */
  124. enc_cfg->band_mode = (cfg->band_mode - 1);
  125. enc_cfg->dtx_enable = (cfg->dtx_enable ? 1 : 0);
  126. enc_cfg->frame_format = 0;
  127. pr_debug("%s:session id %d: band_mode = 0x%x dtx_enable=0x%x\n",
  128. __func__, audio->ac->session,
  129. enc_cfg->band_mode, enc_cfg->dtx_enable);
  130. break;
  131. }
  132. default:
  133. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  134. rc = -EINVAL;
  135. }
  136. return rc;
  137. }
  138. static long amrnb_in_ioctl(struct file *file,
  139. unsigned int cmd, unsigned long arg)
  140. {
  141. struct q6audio_in *audio = file->private_data;
  142. int rc = 0;
  143. switch (cmd) {
  144. case AUDIO_START:
  145. case AUDIO_STOP: {
  146. rc = amrnb_in_ioctl_shared(file, cmd, NULL);
  147. break;
  148. }
  149. case AUDIO_GET_AMRNB_ENC_CONFIG_V2: {
  150. if (copy_to_user((void *)arg, audio->enc_cfg,
  151. sizeof(struct msm_audio_amrnb_enc_config_v2))) {
  152. pr_err("%s: copy_to_user for AUDIO_GET_AMRNB_ENC_CONFIG_V2 failed\n",
  153. __func__);
  154. rc = -EFAULT;
  155. }
  156. break;
  157. }
  158. case AUDIO_SET_AMRNB_ENC_CONFIG_V2: {
  159. struct msm_audio_amrnb_enc_config_v2 cfg;
  160. if (copy_from_user(&cfg, (void *) arg,
  161. sizeof(cfg))) {
  162. pr_err("%s: copy_from_user for AUDIO_SET_AMRNB_ENC_CONFIG_V2 failed\n",
  163. __func__);
  164. rc = -EFAULT;
  165. break;
  166. }
  167. rc = amrnb_in_ioctl_shared(file, cmd, &cfg);
  168. if (rc)
  169. pr_err("%s: AUDIO_SET_AMRNB_ENC_CONFIG_V2 failed. rc=%d\n",
  170. __func__, rc);
  171. break;
  172. }
  173. default:
  174. pr_err("%s: Unknown ioctl cmd=%d", __func__, cmd);
  175. rc = -EINVAL;
  176. }
  177. return rc;
  178. }
  179. #ifdef CONFIG_COMPAT
  180. struct msm_audio_amrnb_enc_config_v2_32 {
  181. u32 band_mode;
  182. u32 dtx_enable;
  183. u32 frame_format;
  184. };
  185. enum {
  186. AUDIO_GET_AMRNB_ENC_CONFIG_V2_32 = _IOW(AUDIO_IOCTL_MAGIC,
  187. (AUDIO_MAX_COMMON_IOCTL_NUM+2),
  188. struct msm_audio_amrnb_enc_config_v2_32),
  189. AUDIO_SET_AMRNB_ENC_CONFIG_V2_32 = _IOR(AUDIO_IOCTL_MAGIC,
  190. (AUDIO_MAX_COMMON_IOCTL_NUM+3),
  191. struct msm_audio_amrnb_enc_config_v2_32)
  192. };
  193. static long amrnb_in_compat_ioctl(struct file *file,
  194. unsigned int cmd, unsigned long arg)
  195. {
  196. struct q6audio_in *audio = file->private_data;
  197. int rc = 0;
  198. switch (cmd) {
  199. case AUDIO_START:
  200. case AUDIO_STOP: {
  201. rc = amrnb_in_ioctl_shared(file, cmd, NULL);
  202. break;
  203. }
  204. case AUDIO_GET_AMRNB_ENC_CONFIG_V2_32: {
  205. struct msm_audio_amrnb_enc_config_v2 *amrnb_config;
  206. struct msm_audio_amrnb_enc_config_v2_32 amrnb_config_32;
  207. memset(&amrnb_config_32, 0, sizeof(amrnb_config_32));
  208. amrnb_config =
  209. (struct msm_audio_amrnb_enc_config_v2 *)audio->enc_cfg;
  210. amrnb_config_32.band_mode = amrnb_config->band_mode;
  211. amrnb_config_32.dtx_enable = amrnb_config->dtx_enable;
  212. amrnb_config_32.frame_format = amrnb_config->frame_format;
  213. if (copy_to_user((void *)arg, &amrnb_config_32,
  214. sizeof(amrnb_config_32))) {
  215. pr_err("%s: copy_to_user for AUDIO_GET_AMRNB_ENC_CONFIG_V2_32 failed",
  216. __func__);
  217. rc = -EFAULT;
  218. }
  219. break;
  220. }
  221. case AUDIO_SET_AMRNB_ENC_CONFIG_V2_32: {
  222. struct msm_audio_amrnb_enc_config_v2_32 cfg_32;
  223. if (copy_from_user(&cfg_32, (void *) arg,
  224. sizeof(cfg_32))) {
  225. pr_err("%s: copy_from_user for AUDIO_SET_AMRNB_ENC_CONFIG_V2_32 failed\n",
  226. __func__);
  227. rc = -EFAULT;
  228. break;
  229. }
  230. cmd = AUDIO_SET_AMRNB_ENC_CONFIG_V2;
  231. rc = amrnb_in_ioctl_shared(file, cmd, &cfg_32);
  232. if (rc)
  233. pr_err("%s:AUDIO_SET_AMRNB_ENC_CONFIG_V2 failed rc= %d\n",
  234. __func__, rc);
  235. break;
  236. }
  237. default:
  238. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  239. rc = -EINVAL;
  240. }
  241. return rc;
  242. }
  243. #else
  244. #define amrnb_in_compat_ioctl NULL
  245. #endif
  246. static int amrnb_in_open(struct inode *inode, struct file *file)
  247. {
  248. struct q6audio_in *audio = NULL;
  249. struct msm_audio_amrnb_enc_config_v2 *enc_cfg;
  250. int rc = 0;
  251. audio = kzalloc(sizeof(struct q6audio_in), GFP_KERNEL);
  252. if (audio == NULL)
  253. return -ENOMEM;
  254. /* Allocate memory for encoder config param */
  255. audio->enc_cfg = kzalloc(sizeof(struct msm_audio_amrnb_enc_config_v2),
  256. GFP_KERNEL);
  257. if (audio->enc_cfg == NULL) {
  258. kfree(audio);
  259. return -ENOMEM;
  260. }
  261. enc_cfg = audio->enc_cfg;
  262. mutex_init(&audio->lock);
  263. mutex_init(&audio->read_lock);
  264. mutex_init(&audio->write_lock);
  265. spin_lock_init(&audio->dsp_lock);
  266. init_waitqueue_head(&audio->read_wait);
  267. init_waitqueue_head(&audio->write_wait);
  268. /* Settings will be re-config at AUDIO_SET_CONFIG,
  269. * but at least we need to have initial config
  270. */
  271. audio->str_cfg.buffer_size = FRAME_SIZE;
  272. audio->str_cfg.buffer_count = FRAME_NUM;
  273. audio->min_frame_size = 32;
  274. audio->max_frames_per_buf = 10;
  275. audio->pcm_cfg.buffer_size = PCM_BUF_SIZE;
  276. audio->pcm_cfg.buffer_count = PCM_BUF_COUNT;
  277. enc_cfg->band_mode = 7;
  278. enc_cfg->dtx_enable = 0;
  279. audio->pcm_cfg.channel_count = 1;
  280. audio->pcm_cfg.sample_rate = 8000;
  281. audio->buf_cfg.meta_info_enable = 0x01;
  282. audio->buf_cfg.frames_per_buf = 0x01;
  283. audio->ac = q6asm_audio_client_alloc((app_cb)q6asm_in_cb,
  284. (void *)audio);
  285. if (!audio->ac) {
  286. pr_err("%s: Could not allocate memory for audio client\n",
  287. __func__);
  288. kfree(audio->enc_cfg);
  289. kfree(audio);
  290. return -ENOMEM;
  291. }
  292. /* open amrnb encoder in T/NT mode */
  293. if ((file->f_mode & FMODE_WRITE) &&
  294. (file->f_mode & FMODE_READ)) {
  295. audio->feedback = NON_TUNNEL_MODE;
  296. rc = q6asm_open_read_write(audio->ac, FORMAT_AMRNB,
  297. FORMAT_LINEAR_PCM);
  298. if (rc < 0) {
  299. pr_err("%s:session id %d: NT mode Open failed rc=%d\n",
  300. __func__, audio->ac->session, rc);
  301. rc = -ENODEV;
  302. goto fail;
  303. }
  304. pr_info("%s:session id %d: NT mode encoder success\n",
  305. __func__, audio->ac->session);
  306. } else if (!(file->f_mode & FMODE_WRITE) &&
  307. (file->f_mode & FMODE_READ)) {
  308. audio->feedback = TUNNEL_MODE;
  309. rc = q6asm_open_read(audio->ac, FORMAT_AMRNB);
  310. if (rc < 0) {
  311. pr_err("%s:session id %d: T mode Open failed rc=%d\n",
  312. __func__, audio->ac->session, rc);
  313. rc = -ENODEV;
  314. goto fail;
  315. }
  316. /* register for tx overflow (valid for tunnel mode only) */
  317. rc = q6asm_reg_tx_overflow(audio->ac, 0x01);
  318. if (rc < 0) {
  319. pr_err("%s:session id %d: TX Overflow registration failed rc=%d\n",
  320. __func__, audio->ac->session,
  321. rc);
  322. rc = -ENODEV;
  323. goto fail;
  324. }
  325. pr_info("%s:session id %d: T mode encoder success\n",
  326. __func__, audio->ac->session);
  327. } else {
  328. pr_err("%s:session id %d: Unexpected mode\n", __func__,
  329. audio->ac->session);
  330. rc = -EACCES;
  331. goto fail;
  332. }
  333. audio->opened = 1;
  334. atomic_set(&audio->in_count, PCM_BUF_COUNT);
  335. atomic_set(&audio->out_count, 0x00);
  336. audio->enc_compat_ioctl = amrnb_in_compat_ioctl;
  337. audio->enc_ioctl = amrnb_in_ioctl;
  338. file->private_data = audio;
  339. pr_info("%s:session id %d: success\n", __func__, audio->ac->session);
  340. return 0;
  341. fail:
  342. q6asm_audio_client_free(audio->ac);
  343. kfree(audio->enc_cfg);
  344. kfree(audio);
  345. return rc;
  346. }
  347. static const struct file_operations audio_in_fops = {
  348. .owner = THIS_MODULE,
  349. .open = amrnb_in_open,
  350. .release = audio_in_release,
  351. .read = audio_in_read,
  352. .write = audio_in_write,
  353. .unlocked_ioctl = audio_in_ioctl,
  354. .compat_ioctl = audio_in_compat_ioctl
  355. };
  356. struct miscdevice audio_amrnb_in_misc = {
  357. .minor = MISC_DYNAMIC_MINOR,
  358. .name = "msm_amrnb_in",
  359. .fops = &audio_in_fops,
  360. };
  361. static int __init amrnb_in_init(void)
  362. {
  363. return misc_register(&audio_amrnb_in_misc);
  364. }
  365. device_initcall(amrnb_in_init);