audio_alac.c 12 KB

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