audio_alac.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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_alac.h>
  6. #include <linux/compat.h>
  7. #include "audio_utils_aio.h"
  8. static struct miscdevice audio_alac_misc;
  9. static struct ws_mgr audio_alac_ws_mgr;
  10. static const struct file_operations audio_alac_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_alac_debug_fops);
  18. }
  19. static int alac_channel_map(u8 *channel_mapping, uint32_t channels);
  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_alac_cfg alac_cfg;
  28. struct msm_audio_alac_config *alac_config;
  29. u8 channel_mapping[PCM_FORMAT_MAX_NUM_CHANNEL];
  30. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  31. if (alac_channel_map(channel_mapping,
  32. audio->pcm_cfg.channel_count)) {
  33. pr_err("%s: setting channel map failed %d\n",
  34. __func__, audio->pcm_cfg.channel_count);
  35. }
  36. pr_debug("%s[%pK]: AUDIO_START session_id[%d]\n", __func__,
  37. audio, audio->ac->session);
  38. if (audio->feedback == NON_TUNNEL_MODE) {
  39. /* Configure PCM output block */
  40. rc = q6asm_enc_cfg_blk_pcm_v2(audio->ac,
  41. audio->pcm_cfg.sample_rate,
  42. audio->pcm_cfg.channel_count,
  43. 16, /*bits per sample*/
  44. false, false, channel_mapping);
  45. if (rc < 0) {
  46. pr_err("pcm output block config failed\n");
  47. break;
  48. }
  49. }
  50. alac_config = (struct msm_audio_alac_config *)audio->codec_cfg;
  51. alac_cfg.frame_length = alac_config->frameLength;
  52. alac_cfg.compatible_version = alac_config->compatVersion;
  53. alac_cfg.bit_depth = alac_config->bitDepth;
  54. alac_cfg.pb = alac_config->pb;
  55. alac_cfg.mb = alac_config->mb;
  56. alac_cfg.kb = alac_config->kb;
  57. alac_cfg.num_channels = alac_config->channelCount;
  58. alac_cfg.max_run = alac_config->maxRun;
  59. alac_cfg.max_frame_bytes = alac_config->maxSize;
  60. alac_cfg.avg_bit_rate = alac_config->averageBitRate;
  61. alac_cfg.sample_rate = alac_config->sampleRate;
  62. alac_cfg.channel_layout_tag = alac_config->channelLayout;
  63. pr_debug("%s: frame_length %d compatible_version %d bit_depth %d pb %d mb %d kb %d num_channels %d max_run %d max_frame_bytes %d avg_bit_rate %d sample_rate %d channel_layout_tag %d\n",
  64. __func__, alac_config->frameLength,
  65. alac_config->compatVersion,
  66. alac_config->bitDepth, alac_config->pb,
  67. alac_config->mb, alac_config->kb,
  68. alac_config->channelCount, alac_config->maxRun,
  69. alac_config->maxSize,
  70. alac_config->averageBitRate,
  71. alac_config->sampleRate,
  72. alac_config->channelLayout);
  73. /* Configure Media format block */
  74. rc = q6asm_media_format_block_alac(audio->ac, &alac_cfg,
  75. audio->ac->stream_id);
  76. if (rc < 0) {
  77. pr_err("cmd media format block failed\n");
  78. break;
  79. }
  80. rc = audio_aio_enable(audio);
  81. audio->eos_rsp = 0;
  82. audio->eos_flag = 0;
  83. if (!rc) {
  84. audio->enabled = 1;
  85. } else {
  86. audio->enabled = 0;
  87. pr_err("Audio Start procedure failed rc=%d\n", rc);
  88. break;
  89. }
  90. pr_debug("AUDIO_START success enable[%d]\n", audio->enabled);
  91. if (audio->stopped == 1)
  92. audio->stopped = 0;
  93. break;
  94. }
  95. default:
  96. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  97. break;
  98. }
  99. return rc;
  100. }
  101. static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  102. {
  103. struct q6audio_aio *audio = file->private_data;
  104. int rc = 0;
  105. switch (cmd) {
  106. case AUDIO_START: {
  107. rc = audio_ioctl_shared(file, cmd, (void *)arg);
  108. break;
  109. }
  110. case AUDIO_GET_ALAC_CONFIG: {
  111. if (copy_to_user((void *)arg, audio->codec_cfg,
  112. sizeof(struct msm_audio_alac_config))) {
  113. pr_err("%s:copy_to_user for AUDIO_GET_ALAC_CONFIG failed\n",
  114. __func__);
  115. rc = -EFAULT;
  116. break;
  117. }
  118. break;
  119. }
  120. case AUDIO_SET_ALAC_CONFIG: {
  121. if (copy_from_user(audio->codec_cfg, (void *)arg,
  122. sizeof(struct msm_audio_alac_config))) {
  123. pr_err("%s:copy_from_user for AUDIO_SET_ALAC_CONFIG failed\n",
  124. __func__);
  125. rc = -EFAULT;
  126. break;
  127. }
  128. break;
  129. }
  130. default: {
  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_alac_config_32 {
  141. u32 frameLength;
  142. u8 compatVersion;
  143. u8 bitDepth;
  144. u8 pb;
  145. u8 mb;
  146. u8 kb;
  147. u8 channelCount;
  148. u16 maxRun;
  149. u32 maxSize;
  150. u32 averageBitRate;
  151. u32 sampleRate;
  152. u32 channelLayout;
  153. };
  154. enum {
  155. AUDIO_GET_ALAC_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC,
  156. (AUDIO_MAX_COMMON_IOCTL_NUM+0), struct msm_audio_alac_config_32),
  157. AUDIO_SET_ALAC_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC,
  158. (AUDIO_MAX_COMMON_IOCTL_NUM+1), struct msm_audio_alac_config_32)
  159. };
  160. static long audio_compat_ioctl(struct file *file, unsigned int cmd,
  161. unsigned long arg)
  162. {
  163. struct q6audio_aio *audio = file->private_data;
  164. int rc = 0;
  165. switch (cmd) {
  166. case AUDIO_START: {
  167. rc = audio_ioctl_shared(file, cmd, (void *)arg);
  168. break;
  169. }
  170. case AUDIO_GET_ALAC_CONFIG_32: {
  171. struct msm_audio_alac_config *alac_config;
  172. struct msm_audio_alac_config_32 alac_config_32;
  173. memset(&alac_config_32, 0, sizeof(alac_config_32));
  174. alac_config = (struct msm_audio_alac_config *)audio->codec_cfg;
  175. alac_config_32.frameLength = alac_config->frameLength;
  176. alac_config_32.compatVersion =
  177. alac_config->compatVersion;
  178. alac_config_32.bitDepth = alac_config->bitDepth;
  179. alac_config_32.pb = alac_config->pb;
  180. alac_config_32.mb = alac_config->mb;
  181. alac_config_32.kb = alac_config->kb;
  182. alac_config_32.channelCount = alac_config->channelCount;
  183. alac_config_32.maxRun = alac_config->maxRun;
  184. alac_config_32.maxSize = alac_config->maxSize;
  185. alac_config_32.averageBitRate = alac_config->averageBitRate;
  186. alac_config_32.sampleRate = alac_config->sampleRate;
  187. alac_config_32.channelLayout = alac_config->channelLayout;
  188. if (copy_to_user((void *)arg, &alac_config_32,
  189. sizeof(alac_config_32))) {
  190. pr_err("%s: copy_to_user for GET_ALAC_CONFIG_32 failed\n",
  191. __func__);
  192. rc = -EFAULT;
  193. break;
  194. }
  195. break;
  196. }
  197. case AUDIO_SET_ALAC_CONFIG_32: {
  198. struct msm_audio_alac_config *alac_config;
  199. struct msm_audio_alac_config_32 alac_config_32;
  200. if (copy_from_user(&alac_config_32, (void *)arg,
  201. sizeof(alac_config_32))) {
  202. pr_err("%s: copy_from_user for SET_ALAC_CONFIG_32 failed\n"
  203. , __func__);
  204. rc = -EFAULT;
  205. break;
  206. }
  207. alac_config = (struct msm_audio_alac_config *)audio->codec_cfg;
  208. alac_config->frameLength = alac_config_32.frameLength;
  209. alac_config->compatVersion =
  210. alac_config_32.compatVersion;
  211. alac_config->bitDepth = alac_config_32.bitDepth;
  212. alac_config->pb = alac_config_32.pb;
  213. alac_config->mb = alac_config_32.mb;
  214. alac_config->kb = alac_config_32.kb;
  215. alac_config->channelCount = alac_config_32.channelCount;
  216. alac_config->maxRun = alac_config_32.maxRun;
  217. alac_config->maxSize = alac_config_32.maxSize;
  218. alac_config->averageBitRate = alac_config_32.averageBitRate;
  219. alac_config->sampleRate = alac_config_32.sampleRate;
  220. alac_config->channelLayout = alac_config_32.channelLayout;
  221. break;
  222. }
  223. default: {
  224. rc = audio->codec_compat_ioctl(file, cmd, arg);
  225. if (rc)
  226. pr_err_ratelimited("Failed in utils_ioctl: %d\n", rc);
  227. break;
  228. }
  229. }
  230. return rc;
  231. }
  232. #else
  233. #define audio_compat_ioctl NULL
  234. #endif
  235. static int audio_open(struct inode *inode, struct file *file)
  236. {
  237. struct q6audio_aio *audio = NULL;
  238. int rc = 0;
  239. /* 4 bytes represents decoder number, 1 byte for terminate string */
  240. char name[sizeof "msm_alac_" + 5];
  241. audio = kzalloc(sizeof(struct q6audio_aio), GFP_KERNEL);
  242. if (!audio)
  243. return -ENOMEM;
  244. audio->codec_cfg = kzalloc(sizeof(struct msm_audio_alac_config),
  245. GFP_KERNEL);
  246. if (!audio->codec_cfg) {
  247. kfree(audio);
  248. return -ENOMEM;
  249. }
  250. audio->pcm_cfg.buffer_size = PCM_BUFSZ_MIN;
  251. audio->miscdevice = &audio_alac_misc;
  252. audio->wakelock_voted = false;
  253. audio->audio_ws_mgr = &audio_alac_ws_mgr;
  254. audio->ac = q6asm_audio_client_alloc((app_cb) q6_audio_cb,
  255. (void *)audio);
  256. if (!audio->ac) {
  257. pr_err("Could not allocate memory for audio client\n");
  258. kfree(audio->codec_cfg);
  259. kfree(audio);
  260. return -ENOMEM;
  261. }
  262. rc = audio_aio_open(audio, file);
  263. if (rc < 0) {
  264. pr_err_ratelimited("%s: audio_aio_open rc=%d\n",
  265. __func__, rc);
  266. goto fail;
  267. }
  268. /* open in T/NT mode */
  269. if ((file->f_mode & FMODE_WRITE) && (file->f_mode & FMODE_READ)) {
  270. rc = q6asm_open_read_write(audio->ac, FORMAT_LINEAR_PCM,
  271. FORMAT_ALAC);
  272. if (rc < 0) {
  273. pr_err("NT mode Open failed rc=%d\n", rc);
  274. rc = -ENODEV;
  275. goto fail;
  276. }
  277. audio->feedback = NON_TUNNEL_MODE;
  278. /* open ALAC decoder, expected frames is always 1*/
  279. audio->buf_cfg.frames_per_buf = 0x01;
  280. audio->buf_cfg.meta_info_enable = 0x01;
  281. } else if ((file->f_mode & FMODE_WRITE) &&
  282. !(file->f_mode & FMODE_READ)) {
  283. rc = q6asm_open_write(audio->ac, FORMAT_ALAC);
  284. if (rc < 0) {
  285. pr_err("T mode Open failed rc=%d\n", rc);
  286. rc = -ENODEV;
  287. goto fail;
  288. }
  289. audio->feedback = TUNNEL_MODE;
  290. audio->buf_cfg.meta_info_enable = 0x00;
  291. } else {
  292. pr_err("Not supported mode\n");
  293. rc = -EACCES;
  294. goto fail;
  295. }
  296. snprintf(name, sizeof(name), "msm_alac_%04x", audio->ac->session);
  297. audio->dentry = config_debugfs_create_file(name, (void *)audio);
  298. if (IS_ERR_OR_NULL(audio->dentry))
  299. pr_debug("debugfs_create_file failed\n");
  300. pr_debug("%s:alacdec success mode[%d]session[%d]\n", __func__,
  301. audio->feedback,
  302. audio->ac->session);
  303. return rc;
  304. fail:
  305. q6asm_audio_client_free(audio->ac);
  306. kfree(audio->codec_cfg);
  307. kfree(audio);
  308. return rc;
  309. }
  310. static int alac_channel_map(u8 *channel_mapping, uint32_t channels)
  311. {
  312. u8 *lchannel_mapping;
  313. lchannel_mapping = channel_mapping;
  314. pr_debug("%s: channels passed: %d\n", __func__, channels);
  315. if (channels == 1) {
  316. lchannel_mapping[0] = PCM_CHANNEL_FC;
  317. } else if (channels == 2) {
  318. lchannel_mapping[0] = PCM_CHANNEL_FL;
  319. lchannel_mapping[1] = PCM_CHANNEL_FR;
  320. } else if (channels == 3) {
  321. lchannel_mapping[0] = PCM_CHANNEL_FC;
  322. lchannel_mapping[1] = PCM_CHANNEL_FL;
  323. lchannel_mapping[2] = PCM_CHANNEL_FR;
  324. } else if (channels == 4) {
  325. lchannel_mapping[0] = PCM_CHANNEL_FC;
  326. lchannel_mapping[1] = PCM_CHANNEL_FL;
  327. lchannel_mapping[2] = PCM_CHANNEL_FR;
  328. lchannel_mapping[3] = PCM_CHANNEL_CS;
  329. } else if (channels == 5) {
  330. lchannel_mapping[0] = PCM_CHANNEL_FC;
  331. lchannel_mapping[1] = PCM_CHANNEL_FL;
  332. lchannel_mapping[2] = PCM_CHANNEL_FR;
  333. lchannel_mapping[3] = PCM_CHANNEL_LS;
  334. lchannel_mapping[4] = PCM_CHANNEL_RS;
  335. } else if (channels == 6) {
  336. lchannel_mapping[0] = PCM_CHANNEL_FC;
  337. lchannel_mapping[1] = PCM_CHANNEL_FL;
  338. lchannel_mapping[2] = PCM_CHANNEL_FR;
  339. lchannel_mapping[3] = PCM_CHANNEL_LS;
  340. lchannel_mapping[4] = PCM_CHANNEL_RS;
  341. lchannel_mapping[5] = PCM_CHANNEL_LFE;
  342. } else if (channels == 7) {
  343. lchannel_mapping[0] = PCM_CHANNEL_FC;
  344. lchannel_mapping[1] = PCM_CHANNEL_FL;
  345. lchannel_mapping[2] = PCM_CHANNEL_FR;
  346. lchannel_mapping[3] = PCM_CHANNEL_LS;
  347. lchannel_mapping[4] = PCM_CHANNEL_RS;
  348. lchannel_mapping[5] = PCM_CHANNEL_CS;
  349. lchannel_mapping[6] = PCM_CHANNEL_LFE;
  350. } else if (channels == 8) {
  351. lchannel_mapping[0] = PCM_CHANNEL_FC;
  352. lchannel_mapping[1] = PCM_CHANNEL_FLC;
  353. lchannel_mapping[2] = PCM_CHANNEL_FRC;
  354. lchannel_mapping[3] = PCM_CHANNEL_FL;
  355. lchannel_mapping[4] = PCM_CHANNEL_FR;
  356. lchannel_mapping[5] = PCM_CHANNEL_LS;
  357. lchannel_mapping[6] = PCM_CHANNEL_RS;
  358. lchannel_mapping[7] = PCM_CHANNEL_LFE;
  359. } else {
  360. pr_err("%s: ERROR.unsupported num_ch = %u\n",
  361. __func__, channels);
  362. return -EINVAL;
  363. }
  364. return 0;
  365. }
  366. static const struct file_operations audio_alac_fops = {
  367. .owner = THIS_MODULE,
  368. .open = audio_open,
  369. .release = audio_aio_release,
  370. .unlocked_ioctl = audio_ioctl,
  371. .fsync = audio_aio_fsync,
  372. .compat_ioctl = audio_compat_ioctl
  373. };
  374. static struct miscdevice audio_alac_misc = {
  375. .minor = MISC_DYNAMIC_MINOR,
  376. .name = "msm_alac",
  377. .fops = &audio_alac_fops,
  378. };
  379. int __init audio_alac_init(void)
  380. {
  381. int ret = misc_register(&audio_alac_misc);
  382. if (ret == 0)
  383. device_init_wakeup(audio_alac_misc.this_device, true);
  384. audio_alac_ws_mgr.ref_cnt = 0;
  385. mutex_init(&audio_alac_ws_mgr.ws_lock);
  386. return ret;
  387. }
  388. void audio_alac_exit(void)
  389. {
  390. mutex_destroy(&audio_alac_ws_mgr.ws_lock);
  391. misc_deregister(&audio_alac_misc);
  392. }