audio_ape.c 10 KB

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