g711alaw_in.c 9.6 KB

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