audio_alac.c 12 KB

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