audio_ape.c 9.7 KB

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