qcelp_in.c 11 KB

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