amrwb_in.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011-2012, 2014, 2016-2017 The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/dma-mapping.h>
  6. #include <linux/fs.h>
  7. #include <linux/module.h>
  8. #include <linux/miscdevice.h>
  9. #include <linux/msm_audio_amrwb.h>
  10. #include <linux/sched.h>
  11. #include <linux/slab.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/wait.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 + ((61+sizeof(struct meta_out_dsp)) * 10))
  22. static long amrwb_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_amrwb_enc_config *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_amrwb(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 amrwb 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_AMRWB_ENC_CONFIG: {
  94. struct msm_audio_amrwb_enc_config *cfg;
  95. struct msm_audio_amrwb_enc_config *enc_cfg;
  96. enc_cfg = audio->enc_cfg;
  97. cfg = (struct msm_audio_amrwb_enc_config *)arg;
  98. if (cfg == NULL) {
  99. pr_err("%s: NULL config pointer for %s\n",
  100. __func__, "AUDIO_SET_AMRWB_ENC_CONFIG");
  101. rc = -EINVAL;
  102. break;
  103. }
  104. if (cfg->band_mode > 8) {
  105. pr_err("%s:session id %d: invalid band mode\n",
  106. __func__, audio->ac->session);
  107. rc = -EINVAL;
  108. break;
  109. }
  110. /* ToDo: AMR WB encoder accepts values between 0-8
  111. * while openmax provides value between 9-17
  112. * as per spec
  113. */
  114. enc_cfg->band_mode = cfg->band_mode;
  115. enc_cfg->dtx_enable = (cfg->dtx_enable ? 1 : 0);
  116. /* Currently DSP does not support different frameformat */
  117. enc_cfg->frame_format = 0;
  118. pr_debug("%s:session id %d: band_mode = 0x%x dtx_enable=0x%x\n",
  119. __func__, audio->ac->session,
  120. enc_cfg->band_mode, enc_cfg->dtx_enable);
  121. break;
  122. }
  123. default:
  124. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  125. rc = -EINVAL;
  126. }
  127. return rc;
  128. }
  129. static long amrwb_in_ioctl(struct file *file,
  130. unsigned int cmd, unsigned long arg)
  131. {
  132. struct q6audio_in *audio = file->private_data;
  133. int rc = 0;
  134. switch (cmd) {
  135. case AUDIO_START:
  136. case AUDIO_STOP: {
  137. rc = amrwb_in_ioctl_shared(file, cmd, NULL);
  138. break;
  139. }
  140. case AUDIO_GET_AMRWB_ENC_CONFIG: {
  141. if (copy_to_user((void *)arg, audio->enc_cfg,
  142. sizeof(struct msm_audio_amrwb_enc_config)))
  143. pr_err("%s: copy_to_user for AUDIO_GET_AMRWB_ENC_CONFIG failed\n",
  144. __func__);
  145. rc = -EFAULT;
  146. break;
  147. }
  148. case AUDIO_SET_AMRWB_ENC_CONFIG: {
  149. struct msm_audio_amrwb_enc_config cfg;
  150. if (copy_from_user(&cfg, (void *) arg,
  151. sizeof(cfg))) {
  152. pr_err("%s: copy_from_user for AUDIO_SET_AMRWB_ENC_CONFIG failed\n",
  153. __func__);
  154. rc = -EFAULT;
  155. break;
  156. }
  157. rc = amrwb_in_ioctl_shared(file, cmd, &cfg);
  158. if (rc)
  159. pr_err("%s:AUDIO_SET_AAC_ENC_CONFIG failed. rc=%d\n",
  160. __func__, rc);
  161. break;
  162. }
  163. default:
  164. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  165. rc = -EINVAL;
  166. }
  167. return rc;
  168. }
  169. #ifdef CONFIG_COMPAT
  170. struct msm_audio_amrwb_enc_config_32 {
  171. u32 band_mode;
  172. u32 dtx_enable;
  173. u32 frame_format;
  174. };
  175. enum {
  176. AUDIO_GET_AMRWB_ENC_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC,
  177. (AUDIO_MAX_COMMON_IOCTL_NUM+0),
  178. struct msm_audio_amrwb_enc_config_32),
  179. AUDIO_SET_AMRWB_ENC_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC,
  180. (AUDIO_MAX_COMMON_IOCTL_NUM+1),
  181. struct msm_audio_amrwb_enc_config_32)
  182. };
  183. static long amrwb_in_compat_ioctl(struct file *file,
  184. unsigned int cmd, unsigned long arg)
  185. {
  186. struct q6audio_in *audio = file->private_data;
  187. int rc = 0;
  188. switch (cmd) {
  189. case AUDIO_START:
  190. case AUDIO_STOP: {
  191. rc = amrwb_in_ioctl_shared(file, cmd, NULL);
  192. break;
  193. }
  194. case AUDIO_GET_AMRWB_ENC_CONFIG_32: {
  195. struct msm_audio_amrwb_enc_config *amrwb_config;
  196. struct msm_audio_amrwb_enc_config_32 amrwb_config_32;
  197. memset(&amrwb_config_32, 0, sizeof(amrwb_config_32));
  198. amrwb_config =
  199. (struct msm_audio_amrwb_enc_config *)audio->enc_cfg;
  200. amrwb_config_32.band_mode = amrwb_config->band_mode;
  201. amrwb_config_32.dtx_enable = amrwb_config->dtx_enable;
  202. amrwb_config_32.frame_format = amrwb_config->frame_format;
  203. if (copy_to_user((void *)arg, &amrwb_config_32,
  204. sizeof(struct msm_audio_amrwb_enc_config_32))) {
  205. pr_err("%s: copy_to_user for AUDIO_GET_AMRWB_ENC_CONFIG_32 failed\n",
  206. __func__);
  207. rc = -EFAULT;
  208. }
  209. break;
  210. }
  211. case AUDIO_SET_AMRWB_ENC_CONFIG_32: {
  212. struct msm_audio_amrwb_enc_config cfg_32;
  213. if (copy_from_user(&cfg_32, (void *) arg,
  214. sizeof(cfg_32))) {
  215. pr_err("%s: copy_from_user for AUDIO_SET_AMRWB_ENC_CONFIG_32 failed\n",
  216. __func__);
  217. rc = -EFAULT;
  218. break;
  219. }
  220. cmd = AUDIO_SET_AMRWB_ENC_CONFIG;
  221. rc = amrwb_in_ioctl_shared(file, cmd, &cfg_32);
  222. if (rc)
  223. pr_err("%s:AUDIO_SET_AAC_ENC_CONFIG failed. rc=%d\n",
  224. __func__, rc);
  225. break;
  226. }
  227. default:
  228. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  229. rc = -EINVAL;
  230. }
  231. return rc;
  232. }
  233. #else
  234. #define amrwb_in_compat_ioctl NULL
  235. #endif
  236. static int amrwb_in_open(struct inode *inode, struct file *file)
  237. {
  238. struct q6audio_in *audio = NULL;
  239. struct msm_audio_amrwb_enc_config *enc_cfg;
  240. int rc = 0;
  241. audio = kzalloc(sizeof(struct q6audio_in), GFP_KERNEL);
  242. if (audio == NULL)
  243. return -ENOMEM;
  244. /* Allocate memory for encoder config param */
  245. audio->enc_cfg = kzalloc(sizeof(struct msm_audio_amrwb_enc_config),
  246. GFP_KERNEL);
  247. if (audio->enc_cfg == NULL) {
  248. kfree(audio);
  249. return -ENOMEM;
  250. }
  251. enc_cfg = audio->enc_cfg;
  252. mutex_init(&audio->lock);
  253. mutex_init(&audio->read_lock);
  254. mutex_init(&audio->write_lock);
  255. spin_lock_init(&audio->dsp_lock);
  256. init_waitqueue_head(&audio->read_wait);
  257. init_waitqueue_head(&audio->write_wait);
  258. /* Settings will be re-config at AUDIO_SET_CONFIG,
  259. * but at least we need to have initial config
  260. */
  261. audio->str_cfg.buffer_size = FRAME_SIZE;
  262. audio->str_cfg.buffer_count = FRAME_NUM;
  263. audio->min_frame_size = 32;
  264. audio->max_frames_per_buf = 10;
  265. audio->pcm_cfg.buffer_size = PCM_BUF_SIZE;
  266. audio->pcm_cfg.buffer_count = PCM_BUF_COUNT;
  267. enc_cfg->band_mode = 8;
  268. enc_cfg->dtx_enable = 0;
  269. audio->pcm_cfg.channel_count = 1;
  270. audio->pcm_cfg.sample_rate = 16000;
  271. audio->buf_cfg.meta_info_enable = 0x01;
  272. audio->buf_cfg.frames_per_buf = 0x01;
  273. audio->ac = q6asm_audio_client_alloc((app_cb)q6asm_in_cb,
  274. (void *)audio);
  275. if (!audio->ac) {
  276. pr_err("%s:audio[%pK]: Could not allocate memory for audio client\n",
  277. __func__, audio);
  278. kfree(audio->enc_cfg);
  279. kfree(audio);
  280. return -ENOMEM;
  281. }
  282. /* open amrwb encoder in T/NT mode */
  283. if ((file->f_mode & FMODE_WRITE) &&
  284. (file->f_mode & FMODE_READ)) {
  285. audio->feedback = NON_TUNNEL_MODE;
  286. rc = q6asm_open_read_write(audio->ac, FORMAT_AMRWB,
  287. FORMAT_LINEAR_PCM);
  288. if (rc < 0) {
  289. pr_err("%s:session id %d: NT mode Open failed rc=%d\n",
  290. __func__, audio->ac->session, rc);
  291. rc = -ENODEV;
  292. goto fail;
  293. }
  294. pr_info("%s:session id %d: NT mode encoder success\n",
  295. __func__, audio->ac->session);
  296. } else if (!(file->f_mode & FMODE_WRITE) &&
  297. (file->f_mode & FMODE_READ)) {
  298. audio->feedback = TUNNEL_MODE;
  299. rc = q6asm_open_read(audio->ac, FORMAT_AMRWB);
  300. if (rc < 0) {
  301. pr_err("%s:session id %d: T mode Open failed rc=%d\n",
  302. __func__, audio->ac->session, rc);
  303. rc = -ENODEV;
  304. goto fail;
  305. }
  306. /* register for tx overflow (valid for tunnel mode only) */
  307. rc = q6asm_reg_tx_overflow(audio->ac, 0x01);
  308. if (rc < 0) {
  309. pr_err("%s:session id %d: TX Overflow registration failed rc=%d\n",
  310. __func__, audio->ac->session,
  311. rc);
  312. rc = -ENODEV;
  313. goto fail;
  314. }
  315. pr_info("%s:session id %d: T mode encoder success\n",
  316. __func__, audio->ac->session);
  317. } else {
  318. pr_err("%s:session id %d: Unexpected mode\n", __func__,
  319. audio->ac->session);
  320. rc = -EACCES;
  321. goto fail;
  322. }
  323. audio->opened = 1;
  324. atomic_set(&audio->in_count, PCM_BUF_COUNT);
  325. atomic_set(&audio->out_count, 0x00);
  326. audio->enc_compat_ioctl = amrwb_in_compat_ioctl;
  327. audio->enc_ioctl = amrwb_in_ioctl;
  328. file->private_data = audio;
  329. pr_info("%s:session id %d: success\n", __func__, audio->ac->session);
  330. return 0;
  331. fail:
  332. q6asm_audio_client_free(audio->ac);
  333. kfree(audio->enc_cfg);
  334. kfree(audio);
  335. return rc;
  336. }
  337. static const struct file_operations audio_in_fops = {
  338. .owner = THIS_MODULE,
  339. .open = amrwb_in_open,
  340. .release = audio_in_release,
  341. .read = audio_in_read,
  342. .write = audio_in_write,
  343. .unlocked_ioctl = audio_in_ioctl,
  344. .compat_ioctl = audio_in_compat_ioctl
  345. };
  346. struct miscdevice audio_amrwb_in_misc = {
  347. .minor = MISC_DYNAMIC_MINOR,
  348. .name = "msm_amrwb_in",
  349. .fops = &audio_in_fops,
  350. };
  351. int __init amrwb_in_init(void)
  352. {
  353. return misc_register(&audio_amrwb_in_misc);
  354. }
  355. void amrwb_in_exit(void)
  356. {
  357. misc_deregister(&audio_amrwb_in_misc);
  358. }