qcelp_in.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2010-2017, 2020 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 <audio/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 + ((35+sizeof(struct meta_out_dsp)) * 10))
  21. static long qcelp_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_qcelp_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. /* reduced_rate_level, rate_modulation_cmd set to zero
  45. * currently not configurable from user space
  46. */
  47. rc = q6asm_enc_cfg_blk_qcelp(audio->ac,
  48. audio->buf_cfg.frames_per_buf,
  49. enc_cfg->min_bit_rate,
  50. enc_cfg->max_bit_rate, 0, 0);
  51. if (rc < 0) {
  52. pr_err("%s:session id %d: cmd qcelp 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", __func__,
  67. 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,
  91. rc);
  92. break;
  93. }
  94. break;
  95. }
  96. case AUDIO_SET_QCELP_ENC_CONFIG: {
  97. struct msm_audio_qcelp_enc_config *cfg;
  98. struct msm_audio_qcelp_enc_config *enc_cfg;
  99. enc_cfg = audio->enc_cfg;
  100. cfg = (struct msm_audio_qcelp_enc_config *)arg;
  101. if (cfg == NULL) {
  102. pr_err("%s: NULL config pointer\n", __func__);
  103. rc = -EINVAL;
  104. break;
  105. }
  106. if (cfg->min_bit_rate > 4 ||
  107. cfg->min_bit_rate < 1) {
  108. pr_err("%s:session id %d: invalid min bitrate\n",
  109. __func__, audio->ac->session);
  110. rc = -EINVAL;
  111. break;
  112. }
  113. if (cfg->max_bit_rate > 4 ||
  114. cfg->max_bit_rate < 1) {
  115. pr_err("%s:session id %d: invalid max bitrate\n",
  116. __func__, audio->ac->session);
  117. rc = -EINVAL;
  118. break;
  119. }
  120. enc_cfg->cdma_rate = cfg->cdma_rate;
  121. enc_cfg->min_bit_rate = cfg->min_bit_rate;
  122. enc_cfg->max_bit_rate = cfg->max_bit_rate;
  123. pr_debug("%s:session id %d: min_bit_rate= 0x%x max_bit_rate=0x%x\n",
  124. __func__,
  125. audio->ac->session, enc_cfg->min_bit_rate,
  126. enc_cfg->max_bit_rate);
  127. break;
  128. }
  129. default:
  130. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  131. rc = -EINVAL;
  132. }
  133. return rc;
  134. }
  135. static long qcelp_in_ioctl(struct file *file,
  136. unsigned int cmd, unsigned long arg)
  137. {
  138. struct q6audio_in *audio = file->private_data;
  139. int rc = 0;
  140. switch (cmd) {
  141. case AUDIO_START:
  142. case AUDIO_STOP: {
  143. rc = qcelp_in_ioctl_shared(file, cmd, arg);
  144. break;
  145. }
  146. case AUDIO_GET_QCELP_ENC_CONFIG: {
  147. if (copy_to_user((void *)arg, audio->enc_cfg,
  148. sizeof(struct msm_audio_qcelp_enc_config))) {
  149. pr_err(
  150. "%s: copy_to_user for AUDIO_GET_QCELP_ENC_CONFIG failed",
  151. __func__);
  152. rc = -EFAULT;
  153. }
  154. break;
  155. }
  156. case AUDIO_SET_QCELP_ENC_CONFIG: {
  157. struct msm_audio_qcelp_enc_config cfg;
  158. if (copy_from_user(&cfg, (void *) arg,
  159. sizeof(cfg))) {
  160. pr_err(
  161. "%s: copy_from_user for AUDIO_SET_QCELP_ENC_CONFIG failed",
  162. __func__);
  163. rc = -EFAULT;
  164. break;
  165. }
  166. rc = qcelp_in_ioctl_shared(file, cmd, (unsigned long)&cfg);
  167. if (rc)
  168. pr_err("%s:AUDIO_SET_QCELP_ENC_CONFIG failed. Rc= %d\n",
  169. __func__, rc);
  170. break;
  171. }
  172. default:
  173. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  174. rc = -EINVAL;
  175. }
  176. return rc;
  177. }
  178. #ifdef CONFIG_COMPAT
  179. struct msm_audio_qcelp_enc_config32 {
  180. u32 cdma_rate;
  181. u32 min_bit_rate;
  182. u32 max_bit_rate;
  183. };
  184. enum {
  185. AUDIO_SET_QCELP_ENC_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC,
  186. 0, struct msm_audio_qcelp_enc_config32),
  187. AUDIO_GET_QCELP_ENC_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC,
  188. 1, struct msm_audio_qcelp_enc_config32)
  189. };
  190. static long qcelp_in_compat_ioctl(struct file *file,
  191. unsigned int cmd, unsigned long arg)
  192. {
  193. struct q6audio_in *audio = file->private_data;
  194. int rc = 0;
  195. switch (cmd) {
  196. case AUDIO_START:
  197. case AUDIO_STOP: {
  198. rc = qcelp_in_ioctl_shared(file, cmd, arg);
  199. break;
  200. }
  201. case AUDIO_GET_QCELP_ENC_CONFIG_32: {
  202. struct msm_audio_qcelp_enc_config32 cfg_32;
  203. struct msm_audio_qcelp_enc_config *enc_cfg;
  204. memset(&cfg_32, 0, sizeof(cfg_32));
  205. enc_cfg = (struct msm_audio_qcelp_enc_config *)audio->enc_cfg;
  206. cfg_32.cdma_rate = enc_cfg->cdma_rate;
  207. cfg_32.min_bit_rate = enc_cfg->min_bit_rate;
  208. cfg_32.max_bit_rate = enc_cfg->max_bit_rate;
  209. if (copy_to_user((void *)arg, &cfg_32,
  210. sizeof(cfg_32))) {
  211. pr_err("%s: copy_to_user for AUDIO_GET_QCELP_ENC_CONFIG_32 failed\n",
  212. __func__);
  213. rc = -EFAULT;
  214. }
  215. break;
  216. }
  217. case AUDIO_SET_QCELP_ENC_CONFIG_32: {
  218. struct msm_audio_qcelp_enc_config32 cfg_32;
  219. struct msm_audio_qcelp_enc_config cfg;
  220. if (copy_from_user(&cfg_32, (void *) arg,
  221. sizeof(cfg_32))) {
  222. pr_err("%s: copy_from_user for AUDIO_SET_QCELP_ENC_CONFIG_32 failed\n",
  223. __func__);
  224. rc = -EFAULT;
  225. break;
  226. }
  227. cfg.cdma_rate = cfg_32.cdma_rate;
  228. cfg.min_bit_rate = cfg_32.min_bit_rate;
  229. cfg.max_bit_rate = cfg_32.max_bit_rate;
  230. cmd = AUDIO_SET_QCELP_ENC_CONFIG;
  231. rc = qcelp_in_ioctl_shared(file, cmd, (unsigned long)&cfg);
  232. if (rc)
  233. pr_err("%s:AUDIO_SET_QCELP_ENC_CONFIG 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 qcelp_in_compat_ioctl NULL
  245. #endif
  246. static int qcelp_in_open(struct inode *inode, struct file *file)
  247. {
  248. struct q6audio_in *audio = NULL;
  249. struct msm_audio_qcelp_enc_config *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_qcelp_enc_config),
  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 = 35;
  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->min_bit_rate = 4;
  278. enc_cfg->max_bit_rate = 4;
  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->event_abort = 0;
  284. audio->ac = q6asm_audio_client_alloc((app_cb)q6asm_in_cb,
  285. (void *)audio);
  286. if (!audio->ac) {
  287. pr_err("%s: Could not allocate memory for audio client\n",
  288. __func__);
  289. kfree(audio->enc_cfg);
  290. kfree(audio);
  291. return -ENOMEM;
  292. }
  293. /* open qcelp encoder in T/NT mode */
  294. if ((file->f_mode & FMODE_WRITE) &&
  295. (file->f_mode & FMODE_READ)) {
  296. audio->feedback = NON_TUNNEL_MODE;
  297. rc = q6asm_open_read_write(audio->ac, FORMAT_V13K,
  298. FORMAT_LINEAR_PCM);
  299. if (rc < 0) {
  300. pr_err("%s:session id %d: NT mode Open failed rc=%d\n",
  301. __func__, audio->ac->session, rc);
  302. rc = -ENODEV;
  303. goto fail;
  304. }
  305. pr_info("%s:session id %d: NT mode encoder success\n", __func__,
  306. audio->ac->session);
  307. } else if (!(file->f_mode & FMODE_WRITE) &&
  308. (file->f_mode & FMODE_READ)) {
  309. audio->feedback = TUNNEL_MODE;
  310. rc = q6asm_open_read(audio->ac, FORMAT_V13K);
  311. if (rc < 0) {
  312. pr_err("%s:session id %d: T mode Open failed rc=%d\n",
  313. __func__, audio->ac->session, rc);
  314. rc = -ENODEV;
  315. goto fail;
  316. }
  317. /* register for tx overflow (valid for tunnel mode only) */
  318. rc = q6asm_reg_tx_overflow(audio->ac, 0x01);
  319. if (rc < 0) {
  320. pr_err("%s:session id %d: TX Overflow registration failed rc=%d\n",
  321. __func__, 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 = qcelp_in_compat_ioctl;
  338. audio->enc_ioctl = qcelp_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 = qcelp_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_qcelp_in_misc = {
  358. .minor = MISC_DYNAMIC_MINOR,
  359. .name = "msm_qcelp_in",
  360. .fops = &audio_in_fops,
  361. };
  362. int __init qcelp_in_init(void)
  363. {
  364. return misc_register(&audio_qcelp_in_misc);
  365. }
  366. void qcelp_in_exit(void)
  367. {
  368. misc_deregister(&audio_qcelp_in_misc);
  369. }