audio_wma.c 8.9 KB

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