amrnb_in.c 10 KB

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