g711mlaw_in.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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_g711.h>
  13. #include <linux/atomic.h>
  14. #include <linux/compat.h>
  15. #include <asm/ioctls.h>
  16. #include "audio_utils.h"
  17. #ifdef CONFIG_COMPAT
  18. #undef PROC_ADD
  19. #endif
  20. /* Buffer with meta*/
  21. #define PCM_BUF_SIZE (4096 + sizeof(struct meta_in))
  22. /* Maximum 10 frames in buffer with meta */
  23. #define FRAME_SIZE (1 + ((320+sizeof(struct meta_out_dsp)) * 10))
  24. static long g711_in_ioctl_shared(struct file *file,
  25. unsigned int cmd, unsigned long arg)
  26. {
  27. struct q6audio_in *audio = file->private_data;
  28. int rc = 0;
  29. int cnt = 0;
  30. switch (cmd) {
  31. case AUDIO_START: {
  32. struct msm_audio_g711_enc_config *enc_cfg;
  33. enc_cfg = (struct msm_audio_g711_enc_config *)audio->enc_cfg;
  34. pr_debug("%s:session id %d: default buf alloc[%d]\n", __func__,
  35. audio->ac->session, audio->buf_alloc);
  36. if (audio->enabled == 1) {
  37. rc = 0;
  38. break;
  39. }
  40. rc = audio_in_buf_alloc(audio);
  41. if (rc < 0) {
  42. pr_err("%s:session id %d: buffer allocation failed rc=%d\n",
  43. __func__, audio->ac->session, rc);
  44. break;
  45. }
  46. pr_debug("%s: sample rate %d", __func__, enc_cfg->sample_rate);
  47. rc = q6asm_enc_cfg_blk_g711(audio->ac,
  48. audio->buf_cfg.frames_per_buf,
  49. enc_cfg->sample_rate);
  50. if (rc < 0) {
  51. pr_err("%s:session id %d: cmd g711 media format block failed rc=%d\n",
  52. __func__, audio->ac->session, rc);
  53. break;
  54. }
  55. if (audio->feedback == NON_TUNNEL_MODE) {
  56. rc = q6asm_media_format_block_pcm(audio->ac,
  57. audio->pcm_cfg.sample_rate,
  58. audio->pcm_cfg.channel_count);
  59. if (rc < 0) {
  60. pr_err("%s:session id %d: media format block failed rc=%d\n",
  61. __func__, audio->ac->session, rc);
  62. break;
  63. }
  64. }
  65. pr_debug("%s:session id %d: AUDIO_START enable[%d]\n", __func__,
  66. audio->ac->session, 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:session id %d: AUDIO_STOP\n", __func__,
  85. audio->ac->session);
  86. rc = audio_in_disable(audio);
  87. if (rc < 0) {
  88. pr_err("%s:session id %d: Audio Stop procedure failed rc=%d\n",
  89. __func__, audio->ac->session,
  90. rc);
  91. break;
  92. }
  93. break;
  94. }
  95. case AUDIO_SET_G711_ENC_CONFIG: {
  96. struct msm_audio_g711_enc_config *cfg;
  97. struct msm_audio_g711_enc_config *enc_cfg;
  98. enc_cfg = (struct msm_audio_g711_enc_config *)audio->enc_cfg;
  99. cfg = (struct msm_audio_g711_enc_config *)arg;
  100. if (cfg == NULL) {
  101. pr_err("%s: NULL config pointer\n", __func__);
  102. rc = -EINVAL;
  103. break;
  104. }
  105. if (cfg->sample_rate != 8000 &&
  106. cfg->sample_rate != 16000) {
  107. pr_err("%s:session id %d: invalid sample rate\n",
  108. __func__, audio->ac->session);
  109. rc = -EINVAL;
  110. break;
  111. }
  112. enc_cfg->sample_rate = cfg->sample_rate;
  113. pr_debug("%s:session id %d: sample_rate= 0x%x",
  114. __func__,
  115. audio->ac->session, enc_cfg->sample_rate);
  116. break;
  117. }
  118. default:
  119. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  120. rc = -ENOIOCTLCMD;
  121. }
  122. return rc;
  123. }
  124. static long g711_in_ioctl(struct file *file,
  125. unsigned int cmd, unsigned long arg)
  126. {
  127. struct q6audio_in *audio = file->private_data;
  128. int rc = 0;
  129. switch (cmd) {
  130. case AUDIO_START:
  131. case AUDIO_STOP: {
  132. rc = g711_in_ioctl_shared(file, cmd, arg);
  133. break;
  134. }
  135. case AUDIO_GET_G711_ENC_CONFIG: {
  136. if (copy_to_user((void *)arg, audio->enc_cfg,
  137. sizeof(struct msm_audio_g711_enc_config))) {
  138. pr_err(
  139. "%s: copy_to_user for AUDIO_GET_g711_ENC_CONFIG failed",
  140. __func__);
  141. rc = -EFAULT;
  142. }
  143. break;
  144. }
  145. case AUDIO_SET_G711_ENC_CONFIG: {
  146. struct msm_audio_g711_enc_config cfg;
  147. if (copy_from_user(&cfg, (void *) arg,
  148. sizeof(cfg))) {
  149. pr_err(
  150. "%s: copy_from_user for AUDIO_GET_G711_ENC_CONFIG failed",
  151. __func__);
  152. rc = -EFAULT;
  153. break;
  154. }
  155. rc = g711_in_ioctl_shared(file, cmd, (unsigned long)&cfg);
  156. if (rc)
  157. pr_err("%s:AUDIO_GET_G711_ENC_CONFIG failed. Rc= %d\n",
  158. __func__, rc);
  159. break;
  160. }
  161. default:
  162. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  163. rc = -ENOIOCTLCMD;
  164. }
  165. return rc;
  166. }
  167. #ifdef CONFIG_COMPAT
  168. struct msm_audio_g711_enc_config32 {
  169. uint32_t sample_rate;
  170. };
  171. enum {
  172. AUDIO_SET_G711_ENC_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC,
  173. (AUDIO_MAX_COMMON_IOCTL_NUM+0), struct msm_audio_g711_enc_config32),
  174. AUDIO_GET_G711_ENC_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC,
  175. (AUDIO_MAX_COMMON_IOCTL_NUM+1), struct msm_audio_g711_enc_config32)
  176. };
  177. static long g711_in_compat_ioctl(struct file *file,
  178. unsigned int cmd, unsigned long arg)
  179. {
  180. struct q6audio_in *audio = file->private_data;
  181. int rc = 0;
  182. switch (cmd) {
  183. case AUDIO_START:
  184. case AUDIO_STOP: {
  185. rc = g711_in_ioctl_shared(file, cmd, arg);
  186. break;
  187. }
  188. case AUDIO_GET_G711_ENC_CONFIG_32: {
  189. struct msm_audio_g711_enc_config32 cfg_32;
  190. struct msm_audio_g711_enc_config32 *enc_cfg;
  191. enc_cfg = (struct msm_audio_g711_enc_config32 *)audio->enc_cfg;
  192. cfg_32.sample_rate = enc_cfg->sample_rate;
  193. if (copy_to_user((void *)arg, &cfg_32,
  194. sizeof(cfg_32))) {
  195. pr_err("%s: copy_to_user for AUDIO_GET_G711_ENC_CONFIG_32 failed\n",
  196. __func__);
  197. rc = -EFAULT;
  198. }
  199. break;
  200. }
  201. case AUDIO_SET_G711_ENC_CONFIG_32: {
  202. struct msm_audio_g711_enc_config32 cfg_32;
  203. struct msm_audio_g711_enc_config32 cfg;
  204. if (copy_from_user(&cfg_32, (void *) arg,
  205. sizeof(cfg_32))) {
  206. pr_err("%s: copy_from_user for AUDIO_SET_G711_ENC_CONFIG_32 failed\n",
  207. __func__);
  208. rc = -EFAULT;
  209. break;
  210. }
  211. cfg.sample_rate = cfg_32.sample_rate;
  212. cmd = AUDIO_SET_G711_ENC_CONFIG;
  213. rc = g711_in_ioctl_shared(file, cmd, (unsigned long)&cfg);
  214. if (rc)
  215. pr_err("%s:AUDIO_SET_G711_ENC_CONFIG failed. rc= %d\n",
  216. __func__, rc);
  217. break;
  218. }
  219. default:
  220. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  221. rc = -ENOIOCTLCMD;
  222. }
  223. return rc;
  224. }
  225. #else
  226. #define g711_in_compat_ioctl NULL
  227. #endif
  228. static int g711_in_open(struct inode *inode, struct file *file)
  229. {
  230. struct q6audio_in *audio = NULL;
  231. struct msm_audio_g711_enc_config *enc_cfg;
  232. int rc = 0;
  233. audio = kzalloc(sizeof(struct q6audio_in), GFP_KERNEL);
  234. if (audio == NULL)
  235. return -ENOMEM;
  236. /* Allocate memory for encoder config param */
  237. audio->enc_cfg = kzalloc(sizeof(struct msm_audio_g711_enc_config),
  238. GFP_KERNEL);
  239. if (audio->enc_cfg == NULL) {
  240. kfree(audio);
  241. return -ENOMEM;
  242. }
  243. enc_cfg = audio->enc_cfg;
  244. mutex_init(&audio->lock);
  245. mutex_init(&audio->read_lock);
  246. mutex_init(&audio->write_lock);
  247. spin_lock_init(&audio->dsp_lock);
  248. init_waitqueue_head(&audio->read_wait);
  249. init_waitqueue_head(&audio->write_wait);
  250. /*
  251. * Settings will be re-config at AUDIO_SET_CONFIG,
  252. * but at least we need to have initial config
  253. */
  254. audio->str_cfg.buffer_size = FRAME_SIZE;
  255. audio->str_cfg.buffer_count = FRAME_NUM;
  256. audio->min_frame_size = 320;
  257. audio->max_frames_per_buf = 10;
  258. audio->pcm_cfg.buffer_size = PCM_BUF_SIZE;
  259. audio->pcm_cfg.buffer_count = PCM_BUF_COUNT;
  260. enc_cfg->sample_rate = 8000;
  261. audio->pcm_cfg.channel_count = 1;
  262. audio->pcm_cfg.sample_rate = 8000;
  263. audio->buf_cfg.meta_info_enable = 0x01;
  264. audio->buf_cfg.frames_per_buf = 0x01;
  265. audio->event_abort = 0;
  266. audio->ac = q6asm_audio_client_alloc((app_cb)q6asm_in_cb,
  267. (void *)audio);
  268. if (!audio->ac) {
  269. kfree(audio->enc_cfg);
  270. kfree(audio);
  271. return -ENOMEM;
  272. }
  273. /* open g711 encoder in T/NT mode */
  274. if ((file->f_mode & FMODE_WRITE) &&
  275. (file->f_mode & FMODE_READ)) {
  276. audio->feedback = NON_TUNNEL_MODE;
  277. rc = q6asm_open_read_write(audio->ac, FORMAT_G711_MLAW_FS,
  278. FORMAT_LINEAR_PCM);
  279. if (rc < 0) {
  280. pr_err("%s:session id %d: NT mode Open failed rc=%d\n",
  281. __func__, audio->ac->session, rc);
  282. rc = -ENODEV;
  283. goto fail;
  284. }
  285. } else if (!(file->f_mode & FMODE_WRITE) &&
  286. (file->f_mode & FMODE_READ)) {
  287. audio->feedback = TUNNEL_MODE;
  288. rc = q6asm_open_read(audio->ac, FORMAT_G711_MLAW_FS);
  289. if (rc < 0) {
  290. pr_err("%s:session id %d: T mode Open failed rc=%d\n",
  291. __func__, audio->ac->session, rc);
  292. rc = -ENODEV;
  293. goto fail;
  294. }
  295. /* register for tx overflow (valid for tunnel mode only) */
  296. rc = q6asm_reg_tx_overflow(audio->ac, 0x01);
  297. if (rc < 0) {
  298. pr_err("%s:session id %d: TX Overflow registration failed rc=%d\n",
  299. __func__, audio->ac->session, rc);
  300. rc = -ENODEV;
  301. goto fail;
  302. }
  303. } else {
  304. pr_err("%s:session id %d: Unexpected mode\n", __func__,
  305. audio->ac->session);
  306. rc = -EACCES;
  307. goto fail;
  308. }
  309. audio->opened = 1;
  310. audio->reset_event = false;
  311. atomic_set(&audio->in_count, PCM_BUF_COUNT);
  312. atomic_set(&audio->out_count, 0x00);
  313. audio->enc_compat_ioctl = g711_in_compat_ioctl;
  314. audio->enc_ioctl = g711_in_ioctl;
  315. file->private_data = audio;
  316. pr_info("%s:session id %d: success\n", __func__, audio->ac->session);
  317. return 0;
  318. fail:
  319. q6asm_audio_client_free(audio->ac);
  320. kfree(audio->enc_cfg);
  321. kfree(audio);
  322. return rc;
  323. }
  324. static const struct file_operations audio_in_fops = {
  325. .owner = THIS_MODULE,
  326. .open = g711_in_open,
  327. .release = audio_in_release,
  328. .read = audio_in_read,
  329. .write = audio_in_write,
  330. .unlocked_ioctl = audio_in_ioctl,
  331. #ifdef CONFIG_COMPAT
  332. .compat_ioctl = audio_in_compat_ioctl,
  333. #endif
  334. };
  335. struct miscdevice audio_g711mlaw_in_misc = {
  336. .minor = MISC_DYNAMIC_MINOR,
  337. .name = "msm_g711mlaw_in",
  338. .fops = &audio_in_fops,
  339. };
  340. int __init g711mlaw_in_init(void)
  341. {
  342. return misc_register(&audio_g711mlaw_in_misc);
  343. }
  344. void g711mlaw_in_exit(void)
  345. {
  346. misc_deregister(&audio_g711mlaw_in_misc);
  347. }