audio_ape.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/types.h>
  15. #include <linux/msm_audio_ape.h>
  16. #include <linux/compat.h>
  17. #include "audio_utils_aio.h"
  18. static struct miscdevice audio_ape_misc;
  19. static struct ws_mgr audio_ape_ws_mgr;
  20. static const struct file_operations audio_ape_debug_fops = {
  21. .read = audio_aio_debug_read,
  22. .open = audio_aio_debug_open,
  23. };
  24. static struct dentry *config_debugfs_create_file(const char *name, void *data)
  25. {
  26. return debugfs_create_file(name, S_IFREG | 0444,
  27. NULL, (void *)data, &audio_ape_debug_fops);
  28. }
  29. static long audio_ioctl_shared(struct file *file, unsigned int cmd,
  30. void *arg)
  31. {
  32. struct q6audio_aio *audio = file->private_data;
  33. int rc = 0;
  34. switch (cmd) {
  35. case AUDIO_START: {
  36. struct asm_ape_cfg ape_cfg;
  37. struct msm_audio_ape_config *ape_config;
  38. pr_debug("%s[%pK]: AUDIO_START session_id[%d]\n", __func__,
  39. audio, audio->ac->session);
  40. if (audio->feedback == NON_TUNNEL_MODE) {
  41. /* Configure PCM output block */
  42. rc = q6asm_enc_cfg_blk_pcm(audio->ac,
  43. audio->pcm_cfg.sample_rate,
  44. audio->pcm_cfg.channel_count);
  45. if (rc < 0) {
  46. pr_err("pcm output block config failed\n");
  47. break;
  48. }
  49. }
  50. ape_config = (struct msm_audio_ape_config *)audio->codec_cfg;
  51. ape_cfg.compatible_version = ape_config->compatibleVersion;
  52. ape_cfg.compression_level = ape_config->compressionLevel;
  53. ape_cfg.format_flags = ape_config->formatFlags;
  54. ape_cfg.blocks_per_frame = ape_config->blocksPerFrame;
  55. ape_cfg.final_frame_blocks = ape_config->finalFrameBlocks;
  56. ape_cfg.total_frames = ape_config->totalFrames;
  57. ape_cfg.bits_per_sample = ape_config->bitsPerSample;
  58. ape_cfg.num_channels = ape_config->numChannels;
  59. ape_cfg.sample_rate = ape_config->sampleRate;
  60. ape_cfg.seek_table_present = ape_config->seekTablePresent;
  61. pr_debug("%s: compatibleVersion %d compressionLevel %d formatFlags %d blocksPerFrame %d finalFrameBlocks %d totalFrames %d bitsPerSample %d numChannels %d sampleRate %d seekTablePresent %d\n",
  62. __func__, ape_config->compatibleVersion,
  63. ape_config->compressionLevel,
  64. ape_config->formatFlags,
  65. ape_config->blocksPerFrame,
  66. ape_config->finalFrameBlocks,
  67. ape_config->totalFrames,
  68. ape_config->bitsPerSample,
  69. ape_config->numChannels,
  70. ape_config->sampleRate,
  71. ape_config->seekTablePresent);
  72. /* Configure Media format block */
  73. rc = q6asm_media_format_block_ape(audio->ac, &ape_cfg,
  74. audio->ac->stream_id);
  75. if (rc < 0) {
  76. pr_err("cmd media format block failed\n");
  77. break;
  78. }
  79. rc = audio_aio_enable(audio);
  80. audio->eos_rsp = 0;
  81. audio->eos_flag = 0;
  82. if (!rc) {
  83. audio->enabled = 1;
  84. } else {
  85. audio->enabled = 0;
  86. pr_err("Audio Start procedure failed rc=%d\n", rc);
  87. break;
  88. }
  89. pr_debug("AUDIO_START success enable[%d]\n", audio->enabled);
  90. if (audio->stopped == 1)
  91. audio->stopped = 0;
  92. break;
  93. }
  94. default:
  95. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  96. break;
  97. }
  98. return rc;
  99. }
  100. static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  101. {
  102. struct q6audio_aio *audio = file->private_data;
  103. int rc = 0;
  104. switch (cmd) {
  105. case AUDIO_START: {
  106. rc = audio_ioctl_shared(file, cmd, (void *)arg);
  107. break;
  108. }
  109. case AUDIO_GET_APE_CONFIG: {
  110. if (copy_to_user((void *)arg, audio->codec_cfg,
  111. sizeof(struct msm_audio_ape_config))) {
  112. pr_err("%s:copy_to_user for AUDIO_GET_APE_CONFIG failed\n",
  113. __func__);
  114. rc = -EFAULT;
  115. break;
  116. }
  117. break;
  118. }
  119. case AUDIO_SET_APE_CONFIG: {
  120. if (copy_from_user(audio->codec_cfg, (void *)arg,
  121. sizeof(struct msm_audio_ape_config))) {
  122. pr_err("%s:copy_from_user for AUDIO_SET_APE_CONFIG failed\n",
  123. __func__);
  124. rc = -EFAULT;
  125. break;
  126. }
  127. break;
  128. }
  129. default: {
  130. pr_debug("%s[%pK]: Calling utils ioctl\n", __func__, audio);
  131. rc = audio->codec_ioctl(file, cmd, arg);
  132. if (rc)
  133. pr_err_ratelimited("Failed in utils_ioctl: %d\n", rc);
  134. break;
  135. }
  136. }
  137. return rc;
  138. }
  139. #ifdef CONFIG_COMPAT
  140. struct msm_audio_ape_config_32 {
  141. u16 compatibleVersion;
  142. u16 compressionLevel;
  143. u32 formatFlags;
  144. u32 blocksPerFrame;
  145. u32 finalFrameBlocks;
  146. u32 totalFrames;
  147. u16 bitsPerSample;
  148. u16 numChannels;
  149. u32 sampleRate;
  150. u32 seekTablePresent;
  151. };
  152. enum {
  153. AUDIO_GET_APE_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC,
  154. (AUDIO_MAX_COMMON_IOCTL_NUM+0), struct msm_audio_ape_config_32),
  155. AUDIO_SET_APE_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC,
  156. (AUDIO_MAX_COMMON_IOCTL_NUM+1), struct msm_audio_ape_config_32)
  157. };
  158. static long audio_compat_ioctl(struct file *file, unsigned int cmd,
  159. unsigned long arg)
  160. {
  161. struct q6audio_aio *audio = file->private_data;
  162. int rc = 0;
  163. switch (cmd) {
  164. case AUDIO_START: {
  165. rc = audio_ioctl_shared(file, cmd, (void *)arg);
  166. break;
  167. }
  168. case AUDIO_GET_APE_CONFIG_32: {
  169. struct msm_audio_ape_config *ape_config;
  170. struct msm_audio_ape_config_32 ape_config_32;
  171. memset(&ape_config_32, 0, sizeof(ape_config_32));
  172. ape_config = (struct msm_audio_ape_config *)audio->codec_cfg;
  173. ape_config_32.compatibleVersion = ape_config->compatibleVersion;
  174. ape_config_32.compressionLevel =
  175. ape_config->compressionLevel;
  176. ape_config_32.formatFlags = ape_config->formatFlags;
  177. ape_config_32.blocksPerFrame = ape_config->blocksPerFrame;
  178. ape_config_32.finalFrameBlocks = ape_config->finalFrameBlocks;
  179. ape_config_32.totalFrames = ape_config->totalFrames;
  180. ape_config_32.bitsPerSample = ape_config->bitsPerSample;
  181. ape_config_32.numChannels = ape_config->numChannels;
  182. ape_config_32.sampleRate = ape_config->sampleRate;
  183. ape_config_32.seekTablePresent = ape_config->seekTablePresent;
  184. if (copy_to_user((void *)arg, &ape_config_32,
  185. sizeof(ape_config_32))) {
  186. pr_err("%s: copy_to_user for GET_APE_CONFIG_32 failed\n",
  187. __func__);
  188. rc = -EFAULT;
  189. break;
  190. }
  191. break;
  192. }
  193. case AUDIO_SET_APE_CONFIG_32: {
  194. struct msm_audio_ape_config *ape_config;
  195. struct msm_audio_ape_config_32 ape_config_32;
  196. if (copy_from_user(&ape_config_32, (void *)arg,
  197. sizeof(ape_config_32))) {
  198. pr_err("%s: copy_from_user for SET_APE_CONFIG_32 failed\n"
  199. , __func__);
  200. rc = -EFAULT;
  201. break;
  202. }
  203. ape_config = (struct msm_audio_ape_config *)audio->codec_cfg;
  204. ape_config->compatibleVersion = ape_config_32.compatibleVersion;
  205. ape_config->compressionLevel =
  206. ape_config_32.compressionLevel;
  207. ape_config->formatFlags = ape_config_32.formatFlags;
  208. ape_config->blocksPerFrame = ape_config_32.blocksPerFrame;
  209. ape_config->finalFrameBlocks = ape_config_32.finalFrameBlocks;
  210. ape_config->totalFrames = ape_config_32.totalFrames;
  211. ape_config->bitsPerSample = ape_config_32.bitsPerSample;
  212. ape_config->numChannels = ape_config_32.numChannels;
  213. ape_config->sampleRate = ape_config_32.sampleRate;
  214. ape_config->seekTablePresent = ape_config_32.seekTablePresent;
  215. break;
  216. }
  217. default: {
  218. pr_debug("%s[%pK]: Calling utils ioctl\n", __func__, audio);
  219. rc = audio->codec_compat_ioctl(file, cmd, arg);
  220. if (rc)
  221. pr_err_ratelimited("Failed in utils_ioctl: %d\n", rc);
  222. break;
  223. }
  224. }
  225. return rc;
  226. }
  227. #else
  228. #define audio_compat_ioctl NULL
  229. #endif
  230. static int audio_open(struct inode *inode, struct file *file)
  231. {
  232. struct q6audio_aio *audio = NULL;
  233. int rc = 0;
  234. /* 4 bytes represents decoder number, 1 byte for terminate string */
  235. char name[sizeof "msm_ape_" + 5];
  236. audio = kzalloc(sizeof(struct q6audio_aio), GFP_KERNEL);
  237. if (!audio)
  238. return -ENOMEM;
  239. audio->codec_cfg = kzalloc(sizeof(struct msm_audio_ape_config),
  240. GFP_KERNEL);
  241. if (!audio->codec_cfg) {
  242. kfree(audio);
  243. return -ENOMEM;
  244. }
  245. audio->pcm_cfg.buffer_size = PCM_BUFSZ_MIN;
  246. audio->miscdevice = &audio_ape_misc;
  247. audio->wakelock_voted = false;
  248. audio->audio_ws_mgr = &audio_ape_ws_mgr;
  249. audio->ac = q6asm_audio_client_alloc((app_cb) q6_audio_cb,
  250. (void *)audio);
  251. if (!audio->ac) {
  252. pr_err("Could not allocate memory for audio client\n");
  253. kfree(audio->codec_cfg);
  254. kfree(audio);
  255. return -ENOMEM;
  256. }
  257. rc = audio_aio_open(audio, file);
  258. if (rc < 0) {
  259. pr_err_ratelimited("%s: audio_aio_open rc=%d\n",
  260. __func__, rc);
  261. goto fail;
  262. }
  263. /* open in T/NT mode */
  264. if ((file->f_mode & FMODE_WRITE) && (file->f_mode & FMODE_READ)) {
  265. rc = q6asm_open_read_write(audio->ac, FORMAT_LINEAR_PCM,
  266. FORMAT_APE);
  267. if (rc < 0) {
  268. pr_err("NT mode Open failed rc=%d\n", rc);
  269. rc = -ENODEV;
  270. goto fail;
  271. }
  272. audio->feedback = NON_TUNNEL_MODE;
  273. /* open APE decoder, expected frames is always 1*/
  274. audio->buf_cfg.frames_per_buf = 0x01;
  275. audio->buf_cfg.meta_info_enable = 0x01;
  276. } else if ((file->f_mode & FMODE_WRITE) &&
  277. !(file->f_mode & FMODE_READ)) {
  278. rc = q6asm_open_write(audio->ac, FORMAT_APE);
  279. if (rc < 0) {
  280. pr_err("T mode Open failed rc=%d\n", rc);
  281. rc = -ENODEV;
  282. goto fail;
  283. }
  284. audio->feedback = TUNNEL_MODE;
  285. audio->buf_cfg.meta_info_enable = 0x00;
  286. } else {
  287. pr_err("Not supported mode\n");
  288. rc = -EACCES;
  289. goto fail;
  290. }
  291. snprintf(name, sizeof(name), "msm_ape_%04x", audio->ac->session);
  292. audio->dentry = config_debugfs_create_file(name, (void *)audio);
  293. if (IS_ERR_OR_NULL(audio->dentry))
  294. pr_debug("debugfs_create_file failed\n");
  295. pr_debug("%s:apedec success mode[%d]session[%d]\n", __func__,
  296. audio->feedback,
  297. audio->ac->session);
  298. return rc;
  299. fail:
  300. q6asm_audio_client_free(audio->ac);
  301. kfree(audio->codec_cfg);
  302. kfree(audio);
  303. return rc;
  304. }
  305. static const struct file_operations audio_ape_fops = {
  306. .owner = THIS_MODULE,
  307. .open = audio_open,
  308. .release = audio_aio_release,
  309. .unlocked_ioctl = audio_ioctl,
  310. .fsync = audio_aio_fsync,
  311. .compat_ioctl = audio_compat_ioctl
  312. };
  313. static struct miscdevice audio_ape_misc = {
  314. .minor = MISC_DYNAMIC_MINOR,
  315. .name = "msm_ape",
  316. .fops = &audio_ape_fops,
  317. };
  318. int __init audio_ape_init(void)
  319. {
  320. int ret = misc_register(&audio_ape_misc);
  321. if (ret == 0)
  322. device_init_wakeup(audio_ape_misc.this_device, true);
  323. audio_ape_ws_mgr.ref_cnt = 0;
  324. mutex_init(&audio_ape_ws_mgr.ws_lock);
  325. return ret;
  326. }
  327. void audio_ape_exit(void)
  328. {
  329. mutex_destroy(&audio_ape_ws_mgr.ws_lock);
  330. misc_deregister(&audio_ape_misc);
  331. }