audio_wma.c 9.2 KB

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