evrc_in.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2010-2017, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/module.h>
  5. #include <linux/fs.h>
  6. #include <linux/miscdevice.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/sched.h>
  9. #include <linux/wait.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/slab.h>
  12. #include <linux/msm_audio_qcp.h>
  13. #include <linux/atomic.h>
  14. #include <linux/compat.h>
  15. #include <asm/ioctls.h>
  16. #include "audio_utils.h"
  17. /* Buffer with meta*/
  18. #define PCM_BUF_SIZE (4096 + sizeof(struct meta_in))
  19. /* Maximum 10 frames in buffer with meta */
  20. #define FRAME_SIZE (1 + ((23+sizeof(struct meta_out_dsp)) * 10))
  21. static long evrc_in_ioctl_shared(struct file *file,
  22. unsigned int cmd, unsigned long arg)
  23. {
  24. struct q6audio_in *audio = file->private_data;
  25. int rc = 0;
  26. int cnt = 0;
  27. switch (cmd) {
  28. case AUDIO_START: {
  29. struct msm_audio_evrc_enc_config *enc_cfg;
  30. enc_cfg = audio->enc_cfg;
  31. pr_debug("%s:session id %d: default buf alloc[%d]\n", __func__,
  32. audio->ac->session, audio->buf_alloc);
  33. if (audio->enabled == 1) {
  34. pr_info("%s:AUDIO_START already over\n", __func__);
  35. rc = 0;
  36. break;
  37. }
  38. rc = audio_in_buf_alloc(audio);
  39. if (rc < 0) {
  40. pr_err("%s:session id %d: buffer allocation failed\n",
  41. __func__, audio->ac->session);
  42. break;
  43. }
  44. /* rate_modulation_cmd set to zero
  45. * currently not configurable from user space
  46. */
  47. rc = q6asm_enc_cfg_blk_evrc(audio->ac,
  48. audio->buf_cfg.frames_per_buf,
  49. enc_cfg->min_bit_rate,
  50. enc_cfg->max_bit_rate, 0);
  51. if (rc < 0) {
  52. pr_err("%s:session id %d: cmd evrc media format block failed\n",
  53. __func__, audio->ac->session);
  54. break;
  55. }
  56. if (audio->feedback == NON_TUNNEL_MODE) {
  57. rc = q6asm_media_format_block_pcm(audio->ac,
  58. audio->pcm_cfg.sample_rate,
  59. audio->pcm_cfg.channel_count);
  60. if (rc < 0) {
  61. pr_err("%s:session id %d: media format block failed\n",
  62. __func__, audio->ac->session);
  63. break;
  64. }
  65. }
  66. pr_debug("%s:session id %d: AUDIO_START enable[%d]\n",
  67. __func__, audio->ac->session, audio->enabled);
  68. rc = audio_in_enable(audio);
  69. if (!rc) {
  70. audio->enabled = 1;
  71. } else {
  72. audio->enabled = 0;
  73. pr_err("%s:session id %d: Audio Start procedure failed rc=%d\n",
  74. __func__, audio->ac->session, rc);
  75. break;
  76. }
  77. while (cnt++ < audio->str_cfg.buffer_count)
  78. q6asm_read(audio->ac); /* Push buffer to DSP */
  79. rc = 0;
  80. pr_debug("%s:session id %d: AUDIO_START success enable[%d]\n",
  81. __func__, audio->ac->session, audio->enabled);
  82. break;
  83. }
  84. case AUDIO_STOP: {
  85. pr_debug("%s:session id %d: AUDIO_STOP\n", __func__,
  86. audio->ac->session);
  87. rc = audio_in_disable(audio);
  88. if (rc < 0) {
  89. pr_err("%s:session id %d: Audio Stop procedure failed rc=%d\n",
  90. __func__, audio->ac->session, rc);
  91. break;
  92. }
  93. break;
  94. }
  95. case AUDIO_SET_EVRC_ENC_CONFIG: {
  96. struct msm_audio_evrc_enc_config *cfg;
  97. struct msm_audio_evrc_enc_config *enc_cfg;
  98. enc_cfg = audio->enc_cfg;
  99. cfg = (struct msm_audio_evrc_enc_config *)arg;
  100. if (cfg == NULL) {
  101. pr_err("%s: NULL config pointer for %s\n",
  102. __func__, "AUDIO_SET_EVRC_ENC_CONFIG");
  103. rc = -EINVAL;
  104. break;
  105. }
  106. if (cfg->min_bit_rate > 4 ||
  107. cfg->min_bit_rate < 1 ||
  108. (cfg->min_bit_rate == 2)) {
  109. pr_err("%s:session id %d: invalid min bitrate\n",
  110. __func__, audio->ac->session);
  111. rc = -EINVAL;
  112. break;
  113. }
  114. if (cfg->max_bit_rate > 4 ||
  115. cfg->max_bit_rate < 1 ||
  116. (cfg->max_bit_rate == 2)) {
  117. pr_err("%s:session id %d: invalid max bitrate\n",
  118. __func__, audio->ac->session);
  119. rc = -EINVAL;
  120. break;
  121. }
  122. enc_cfg->min_bit_rate = cfg->min_bit_rate;
  123. enc_cfg->max_bit_rate = cfg->max_bit_rate;
  124. pr_debug("%s:session id %d: min_bit_rate= 0x%x max_bit_rate=0x%x\n",
  125. __func__,
  126. audio->ac->session, enc_cfg->min_bit_rate,
  127. enc_cfg->max_bit_rate);
  128. break;
  129. }
  130. default:
  131. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  132. rc = -EINVAL;
  133. }
  134. return rc;
  135. }
  136. static long evrc_in_ioctl(struct file *file,
  137. unsigned int cmd, unsigned long arg)
  138. {
  139. struct q6audio_in *audio = file->private_data;
  140. int rc = 0;
  141. switch (cmd) {
  142. case AUDIO_START:
  143. case AUDIO_STOP: {
  144. rc = evrc_in_ioctl_shared(file, cmd, arg);
  145. break;
  146. }
  147. case AUDIO_GET_EVRC_ENC_CONFIG: {
  148. if (copy_to_user((void *)arg, audio->enc_cfg,
  149. sizeof(struct msm_audio_evrc_enc_config))) {
  150. pr_err("%s: copy_to_user for AUDIO_GET_EVRC_ENC_CONFIG failed\n",
  151. __func__);
  152. rc = -EFAULT;
  153. }
  154. break;
  155. }
  156. case AUDIO_SET_EVRC_ENC_CONFIG: {
  157. struct msm_audio_evrc_enc_config cfg;
  158. if (copy_from_user(&cfg, (void *) arg,
  159. sizeof(struct msm_audio_evrc_enc_config))) {
  160. pr_err("%s: copy_from_user for AUDIO_SET_EVRC_ENC_CONFIG failed\n",
  161. __func__);
  162. rc = -EFAULT;
  163. break;
  164. }
  165. rc = evrc_in_ioctl_shared(file, cmd, (unsigned long)&cfg);
  166. if (rc)
  167. pr_err("%s:AUDIO_SET_EVRC_ENC_CONFIG failed. rc= %d\n",
  168. __func__, rc);
  169. break;
  170. }
  171. default:
  172. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  173. rc = -EINVAL;
  174. }
  175. return rc;
  176. }
  177. #ifdef CONFIG_COMPAT
  178. struct msm_audio_evrc_enc_config32 {
  179. u32 cdma_rate;
  180. u32 min_bit_rate;
  181. u32 max_bit_rate;
  182. };
  183. enum {
  184. AUDIO_SET_EVRC_ENC_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC,
  185. 2, struct msm_audio_evrc_enc_config32),
  186. AUDIO_GET_EVRC_ENC_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC,
  187. 3, struct msm_audio_evrc_enc_config32)
  188. };
  189. static long evrc_in_compat_ioctl(struct file *file,
  190. unsigned int cmd, unsigned long arg)
  191. {
  192. struct q6audio_in *audio = file->private_data;
  193. int rc = 0;
  194. switch (cmd) {
  195. case AUDIO_START:
  196. case AUDIO_STOP: {
  197. rc = evrc_in_ioctl_shared(file, cmd, arg);
  198. break;
  199. }
  200. case AUDIO_GET_EVRC_ENC_CONFIG_32: {
  201. struct msm_audio_evrc_enc_config32 cfg_32;
  202. struct msm_audio_evrc_enc_config *enc_cfg;
  203. memset(&cfg_32, 0, sizeof(cfg_32));
  204. enc_cfg = audio->enc_cfg;
  205. cfg_32.cdma_rate = enc_cfg->cdma_rate;
  206. cfg_32.min_bit_rate = enc_cfg->min_bit_rate;
  207. cfg_32.max_bit_rate = enc_cfg->max_bit_rate;
  208. if (copy_to_user((void *)arg, &cfg_32,
  209. sizeof(cfg_32))) {
  210. pr_err("%s: copy_to_user for AUDIO_GET_EVRC_ENC_CONFIG_32 failed\n",
  211. __func__);
  212. rc = -EFAULT;
  213. }
  214. break;
  215. }
  216. case AUDIO_SET_EVRC_ENC_CONFIG_32: {
  217. struct msm_audio_evrc_enc_config cfg;
  218. struct msm_audio_evrc_enc_config32 cfg_32;
  219. if (copy_from_user(&cfg_32, (void *) arg,
  220. sizeof(cfg_32))) {
  221. pr_err("%s: copy_from_user for AUDIO_SET_EVRC_ENC_CONFIG_32 failed\n",
  222. __func__);
  223. rc = -EFAULT;
  224. break;
  225. }
  226. cfg.cdma_rate = cfg_32.cdma_rate;
  227. cfg.min_bit_rate = cfg_32.min_bit_rate;
  228. cfg.max_bit_rate = cfg_32.max_bit_rate;
  229. cmd = AUDIO_SET_EVRC_ENC_CONFIG;
  230. rc = evrc_in_ioctl_shared(file, cmd, (unsigned long)&cfg);
  231. if (rc)
  232. pr_err("%s:AUDIO_SET_EVRC_ENC_CONFIG failed. rc= %d\n",
  233. __func__, rc);
  234. break;
  235. }
  236. default:
  237. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  238. rc = -EINVAL;
  239. }
  240. return rc;
  241. }
  242. #else
  243. #define evrc_in_compat_ioctl NULL
  244. #endif
  245. static int evrc_in_open(struct inode *inode, struct file *file)
  246. {
  247. struct q6audio_in *audio = NULL;
  248. struct msm_audio_evrc_enc_config *enc_cfg;
  249. int rc = 0;
  250. audio = kzalloc(sizeof(struct q6audio_in), GFP_KERNEL);
  251. if (audio == NULL)
  252. return -ENOMEM;
  253. /* Allocate memory for encoder config param */
  254. audio->enc_cfg = kzalloc(sizeof(struct msm_audio_evrc_enc_config),
  255. GFP_KERNEL);
  256. if (audio->enc_cfg == NULL) {
  257. kfree(audio);
  258. return -ENOMEM;
  259. }
  260. enc_cfg = audio->enc_cfg;
  261. mutex_init(&audio->lock);
  262. mutex_init(&audio->read_lock);
  263. mutex_init(&audio->write_lock);
  264. spin_lock_init(&audio->dsp_lock);
  265. init_waitqueue_head(&audio->read_wait);
  266. init_waitqueue_head(&audio->write_wait);
  267. /* Settings will be re-config at AUDIO_SET_CONFIG,
  268. * but at least we need to have initial config
  269. */
  270. audio->str_cfg.buffer_size = FRAME_SIZE;
  271. audio->str_cfg.buffer_count = FRAME_NUM;
  272. audio->min_frame_size = 23;
  273. audio->max_frames_per_buf = 10;
  274. audio->pcm_cfg.buffer_size = PCM_BUF_SIZE;
  275. audio->pcm_cfg.buffer_count = PCM_BUF_COUNT;
  276. enc_cfg->min_bit_rate = 4;
  277. enc_cfg->max_bit_rate = 4;
  278. audio->pcm_cfg.channel_count = 1;
  279. audio->pcm_cfg.sample_rate = 8000;
  280. audio->buf_cfg.meta_info_enable = 0x01;
  281. audio->buf_cfg.frames_per_buf = 0x01;
  282. audio->event_abort = 0;
  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 evrc 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_EVRC,
  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_EVRC);
  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__,
  321. audio->ac->session, rc);
  322. rc = -ENODEV;
  323. goto fail;
  324. }
  325. pr_info("%s:session id %d: T mode encoder success\n", __func__,
  326. 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. audio->reset_event = false;
  335. atomic_set(&audio->in_count, PCM_BUF_COUNT);
  336. atomic_set(&audio->out_count, 0x00);
  337. audio->enc_compat_ioctl = evrc_in_compat_ioctl;
  338. audio->enc_ioctl = evrc_in_ioctl;
  339. file->private_data = audio;
  340. pr_info("%s:session id %d: success\n", __func__, audio->ac->session);
  341. return 0;
  342. fail:
  343. q6asm_audio_client_free(audio->ac);
  344. kfree(audio->enc_cfg);
  345. kfree(audio);
  346. return rc;
  347. }
  348. static const struct file_operations audio_in_fops = {
  349. .owner = THIS_MODULE,
  350. .open = evrc_in_open,
  351. .release = audio_in_release,
  352. .read = audio_in_read,
  353. .write = audio_in_write,
  354. .unlocked_ioctl = audio_in_ioctl,
  355. .compat_ioctl = audio_in_compat_ioctl
  356. };
  357. struct miscdevice audio_evrc_in_misc = {
  358. .minor = MISC_DYNAMIC_MINOR,
  359. .name = "msm_evrc_in",
  360. .fops = &audio_in_fops,
  361. };
  362. int __init evrc_in_init(void)
  363. {
  364. return misc_register(&audio_evrc_in_misc);
  365. }
  366. void evrc_in_exit(void)
  367. {
  368. misc_deregister(&audio_evrc_in_misc);
  369. }