audio_g711mlaw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2016-2017, 2019 The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/types.h>
  15. #include <linux/msm_audio_g711_dec.h>
  16. #include <linux/compat.h>
  17. #include "audio_utils_aio.h"
  18. static struct miscdevice audio_g711mlaw_misc;
  19. static struct ws_mgr audio_g711_ws_mgr;
  20. static const struct file_operations audio_g711_debug_fops = {
  21. .read = audio_aio_debug_read,
  22. .open = audio_aio_debug_open,
  23. };
  24. static struct dentry *config_debugfs_create_file(const char *name, void *data)
  25. {
  26. return debugfs_create_file(name, S_IFREG | 0444,
  27. NULL, (void *)data, &audio_g711_debug_fops);
  28. }
  29. static int g711_channel_map(u8 *channel_mapping, uint32_t channels);
  30. static long audio_ioctl_shared(struct file *file, unsigned int cmd,
  31. void *arg)
  32. {
  33. struct q6audio_aio *audio = file->private_data;
  34. int rc = 0;
  35. switch (cmd) {
  36. case AUDIO_START: {
  37. struct asm_g711_dec_cfg g711_dec_cfg;
  38. struct msm_audio_g711_dec_config *g711_dec_config;
  39. u8 channel_mapping[PCM_FORMAT_MAX_NUM_CHANNEL];
  40. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  41. memset(&g711_dec_cfg, 0, sizeof(g711_dec_cfg));
  42. if (g711_channel_map(channel_mapping,
  43. audio->pcm_cfg.channel_count)) {
  44. pr_err("%s: setting channel map failed %d\n",
  45. __func__, audio->pcm_cfg.channel_count);
  46. }
  47. pr_debug("%s[%pK]: AUDIO_START session_id[%d]\n", __func__,
  48. audio, audio->ac->session);
  49. if (audio->feedback == NON_TUNNEL_MODE) {
  50. /* Configure PCM output block */
  51. rc = q6asm_enc_cfg_blk_pcm_v2(audio->ac,
  52. audio->pcm_cfg.sample_rate,
  53. audio->pcm_cfg.channel_count,
  54. 16, /*bits per sample*/
  55. false, false, channel_mapping);
  56. if (rc < 0) {
  57. pr_err("%s: pcm output block config failed rc=%d\n",
  58. __func__, rc);
  59. break;
  60. }
  61. }
  62. g711_dec_config =
  63. (struct msm_audio_g711_dec_config *)audio->codec_cfg;
  64. g711_dec_cfg.sample_rate = g711_dec_config->sample_rate;
  65. /* Configure Media format block */
  66. rc = q6asm_media_format_block_g711(audio->ac, &g711_dec_cfg,
  67. audio->ac->stream_id);
  68. if (rc < 0) {
  69. pr_err("%s: cmd media format block failed rc=%d\n",
  70. __func__, rc);
  71. break;
  72. }
  73. rc = audio_aio_enable(audio);
  74. audio->eos_rsp = 0;
  75. audio->eos_flag = 0;
  76. if (!rc) {
  77. audio->enabled = 1;
  78. } else {
  79. audio->enabled = 0;
  80. pr_err("%s: Audio Start procedure failed rc=%d\n",
  81. __func__, rc);
  82. break;
  83. }
  84. pr_debug("%s: AUDIO_START success enable[%d]\n",
  85. __func__, audio->enabled);
  86. if (audio->stopped == 1)
  87. audio->stopped = 0;
  88. break;
  89. }
  90. default:
  91. pr_debug("%s: Unknown ioctl cmd = %d", __func__, cmd);
  92. break;
  93. }
  94. return rc;
  95. }
  96. static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  97. {
  98. struct q6audio_aio *audio = file->private_data;
  99. int rc = 0;
  100. switch (cmd) {
  101. case AUDIO_START: {
  102. rc = audio_ioctl_shared(file, cmd, (void *)arg);
  103. break;
  104. }
  105. case AUDIO_GET_G711_DEC_CONFIG: {
  106. if (copy_to_user((void *)arg, audio->codec_cfg,
  107. sizeof(struct msm_audio_g711_dec_config))) {
  108. pr_err("%s: AUDIO_GET_G711_DEC_CONFIG failed\n",
  109. __func__);
  110. rc = -EFAULT;
  111. }
  112. break;
  113. }
  114. case AUDIO_SET_G711_DEC_CONFIG: {
  115. if (copy_from_user(audio->codec_cfg, (void *)arg,
  116. sizeof(struct msm_audio_g711_dec_config))) {
  117. pr_err("%s: AUDIO_SET_G711_DEC_CONFIG failed\n",
  118. __func__);
  119. rc = -EFAULT;
  120. }
  121. break;
  122. }
  123. default: {
  124. rc = audio->codec_ioctl(file, cmd, arg);
  125. if (rc)
  126. pr_err("%s: Failed in audio_aio_ioctl: %d cmd=%d\n",
  127. __func__, rc, cmd);
  128. break;
  129. }
  130. }
  131. return rc;
  132. }
  133. #ifdef CONFIG_COMPAT
  134. struct msm_audio_g711_dec_config_32 {
  135. u32 sample_rate;
  136. };
  137. enum {
  138. AUDIO_SET_G711_DEC_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC,
  139. (AUDIO_MAX_COMMON_IOCTL_NUM+0), struct msm_audio_g711_dec_config_32),
  140. AUDIO_GET_G711_DEC_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC,
  141. (AUDIO_MAX_COMMON_IOCTL_NUM+1), struct msm_audio_g711_dec_config_32)
  142. };
  143. static long audio_compat_ioctl(struct file *file, unsigned int cmd,
  144. unsigned long arg)
  145. {
  146. struct q6audio_aio *audio = file->private_data;
  147. int rc = 0;
  148. switch (cmd) {
  149. case AUDIO_START: {
  150. rc = audio_ioctl_shared(file, cmd, (void *)arg);
  151. break;
  152. }
  153. case AUDIO_GET_G711_DEC_CONFIG_32: {
  154. struct msm_audio_g711_dec_config *g711_dec_config;
  155. struct msm_audio_g711_dec_config_32 g711_dec_config_32;
  156. memset(&g711_dec_config_32, 0, sizeof(g711_dec_config_32));
  157. g711_dec_config =
  158. (struct msm_audio_g711_dec_config *)audio->codec_cfg;
  159. g711_dec_config_32.sample_rate = g711_dec_config->sample_rate;
  160. if (copy_to_user((void *)arg, &g711_dec_config_32,
  161. sizeof(g711_dec_config_32))) {
  162. pr_err("%s: copy_to_user for AUDIO_GET_G711_DEC_CONFIG failed\n",
  163. __func__);
  164. rc = -EFAULT;
  165. }
  166. break;
  167. }
  168. case AUDIO_SET_G711_DEC_CONFIG_32: {
  169. struct msm_audio_g711_dec_config *g711_dec_config;
  170. struct msm_audio_g711_dec_config_32 g711_dec_config_32;
  171. memset(&g711_dec_config_32, 0, sizeof(g711_dec_config_32));
  172. if (copy_from_user(&g711_dec_config_32, (void *)arg,
  173. sizeof(g711_dec_config_32))) {
  174. pr_err("%s: copy_from_user for AUDIO_SET_G711_DEC_CONFIG failed\n",
  175. __func__);
  176. rc = -EFAULT;
  177. break;
  178. }
  179. g711_dec_config =
  180. (struct msm_audio_g711_dec_config *)audio->codec_cfg;
  181. g711_dec_config->sample_rate = g711_dec_config_32.sample_rate;
  182. break;
  183. }
  184. default: {
  185. rc = audio->codec_compat_ioctl(file, cmd, arg);
  186. if (rc)
  187. pr_err("%s: Failed in audio_aio_compat_ioctl: %d cmd=%d\n",
  188. __func__, rc, cmd);
  189. break;
  190. }
  191. }
  192. return rc;
  193. }
  194. #else
  195. #define audio_compat_ioctl NULL
  196. #endif
  197. static int audio_open(struct inode *inode, struct file *file)
  198. {
  199. struct q6audio_aio *audio = NULL;
  200. int rc = 0;
  201. /* 4 bytes represents decoder number, 1 byte for terminate string */
  202. char name[sizeof "msm_g711_" + 5];
  203. audio = kzalloc(sizeof(struct q6audio_aio), GFP_KERNEL);
  204. if (!audio)
  205. return -ENOMEM;
  206. audio->codec_cfg = kzalloc(sizeof(struct msm_audio_g711_dec_config),
  207. GFP_KERNEL);
  208. if (!audio->codec_cfg) {
  209. kfree(audio);
  210. return -ENOMEM;
  211. }
  212. audio->pcm_cfg.buffer_size = PCM_BUFSZ_MIN;
  213. audio->miscdevice = &audio_g711mlaw_misc;
  214. audio->wakelock_voted = false;
  215. audio->audio_ws_mgr = &audio_g711_ws_mgr;
  216. init_waitqueue_head(&audio->event_wait);
  217. audio->ac = q6asm_audio_client_alloc((app_cb) q6_audio_cb,
  218. (void *)audio);
  219. if (!audio->ac) {
  220. pr_err("%s: Could not allocate memory for audio client\n",
  221. __func__);
  222. kfree(audio->codec_cfg);
  223. kfree(audio);
  224. return -ENOMEM;
  225. }
  226. rc = audio_aio_open(audio, file);
  227. if (rc < 0) {
  228. pr_err_ratelimited("%s: audio_aio_open rc=%d\n",
  229. __func__, rc);
  230. goto fail;
  231. }
  232. /* open in T/NT mode */ /*foramt:G711_ALAW*/
  233. if ((file->f_mode & FMODE_WRITE) && (file->f_mode & FMODE_READ)) {
  234. rc = q6asm_open_read_write(audio->ac, FORMAT_LINEAR_PCM,
  235. FORMAT_G711_MLAW_FS);
  236. if (rc < 0) {
  237. pr_err("%s: NT mode Open failed rc=%d\n", __func__, rc);
  238. goto fail;
  239. }
  240. audio->feedback = NON_TUNNEL_MODE;
  241. /* open G711 decoder, expected frames is always 1*/
  242. audio->buf_cfg.frames_per_buf = 0x01;
  243. audio->buf_cfg.meta_info_enable = 0x01;
  244. } else if ((file->f_mode & FMODE_WRITE) &&
  245. !(file->f_mode & FMODE_READ)) {
  246. rc = q6asm_open_write(audio->ac, FORMAT_G711_MLAW_FS);
  247. if (rc < 0) {
  248. pr_err("%s: T mode Open failed rc=%d\n", __func__, rc);
  249. goto fail;
  250. }
  251. audio->feedback = TUNNEL_MODE;
  252. audio->buf_cfg.meta_info_enable = 0x00;
  253. } else {
  254. pr_err("%s: %d mode is not supported\n", __func__,
  255. file->f_mode);
  256. rc = -EACCES;
  257. goto fail;
  258. }
  259. snprintf(name, sizeof(name), "msm_g711_%04x", audio->ac->session);
  260. audio->dentry = config_debugfs_create_file(name, (void *)audio);
  261. if (IS_ERR_OR_NULL(audio->dentry))
  262. pr_debug("%s: debugfs_create_file failed\n", __func__);
  263. pr_debug("%s: g711dec success mode[%d]session[%d]\n", __func__,
  264. audio->feedback,
  265. audio->ac->session);
  266. return rc;
  267. fail:
  268. q6asm_audio_client_free(audio->ac);
  269. kfree(audio->codec_cfg);
  270. kfree(audio);
  271. return rc;
  272. }
  273. static int g711_channel_map(u8 *channel_mapping, uint32_t channels)
  274. {
  275. u8 *lchannel_mapping;
  276. lchannel_mapping = channel_mapping;
  277. pr_debug("%s: channels passed: %d\n", __func__, channels);
  278. if (channels == 1) {
  279. lchannel_mapping[0] = PCM_CHANNEL_FC;
  280. } else if (channels == 2) {
  281. lchannel_mapping[0] = PCM_CHANNEL_FL;
  282. lchannel_mapping[1] = PCM_CHANNEL_FR;
  283. } else if (channels == 3) {
  284. lchannel_mapping[0] = PCM_CHANNEL_FC;
  285. lchannel_mapping[1] = PCM_CHANNEL_FL;
  286. lchannel_mapping[2] = PCM_CHANNEL_FR;
  287. } else if (channels == 4) {
  288. lchannel_mapping[0] = PCM_CHANNEL_FC;
  289. lchannel_mapping[1] = PCM_CHANNEL_FL;
  290. lchannel_mapping[2] = PCM_CHANNEL_FR;
  291. lchannel_mapping[3] = PCM_CHANNEL_CS;
  292. } else if (channels == 5) {
  293. lchannel_mapping[0] = PCM_CHANNEL_FC;
  294. lchannel_mapping[1] = PCM_CHANNEL_FL;
  295. lchannel_mapping[2] = PCM_CHANNEL_FR;
  296. lchannel_mapping[3] = PCM_CHANNEL_LS;
  297. lchannel_mapping[4] = PCM_CHANNEL_RS;
  298. } else if (channels == 6) {
  299. lchannel_mapping[0] = PCM_CHANNEL_FC;
  300. lchannel_mapping[1] = PCM_CHANNEL_FL;
  301. lchannel_mapping[2] = PCM_CHANNEL_FR;
  302. lchannel_mapping[3] = PCM_CHANNEL_LS;
  303. lchannel_mapping[4] = PCM_CHANNEL_RS;
  304. lchannel_mapping[5] = PCM_CHANNEL_LFE;
  305. } else if (channels == 7) {
  306. lchannel_mapping[0] = PCM_CHANNEL_FC;
  307. lchannel_mapping[1] = PCM_CHANNEL_FL;
  308. lchannel_mapping[2] = PCM_CHANNEL_FR;
  309. lchannel_mapping[3] = PCM_CHANNEL_LS;
  310. lchannel_mapping[4] = PCM_CHANNEL_RS;
  311. lchannel_mapping[5] = PCM_CHANNEL_CS;
  312. lchannel_mapping[6] = PCM_CHANNEL_LFE;
  313. } else if (channels == 8) {
  314. lchannel_mapping[0] = PCM_CHANNEL_FC;
  315. lchannel_mapping[1] = PCM_CHANNEL_FLC;
  316. lchannel_mapping[2] = PCM_CHANNEL_FRC;
  317. lchannel_mapping[3] = PCM_CHANNEL_FL;
  318. lchannel_mapping[4] = PCM_CHANNEL_FR;
  319. lchannel_mapping[5] = PCM_CHANNEL_LS;
  320. lchannel_mapping[6] = PCM_CHANNEL_RS;
  321. lchannel_mapping[7] = PCM_CHANNEL_LFE;
  322. } else {
  323. pr_err("%s: ERROR.unsupported num_ch = %u\n",
  324. __func__, channels);
  325. return -EINVAL;
  326. }
  327. return 0;
  328. }
  329. static const struct file_operations audio_g711_fops = {
  330. .owner = THIS_MODULE,
  331. .open = audio_open,
  332. .release = audio_aio_release,
  333. .unlocked_ioctl = audio_ioctl,
  334. .compat_ioctl = audio_compat_ioctl,
  335. .fsync = audio_aio_fsync,
  336. };
  337. static struct miscdevice audio_g711mlaw_misc = {
  338. .minor = MISC_DYNAMIC_MINOR,
  339. .name = "msm_g711mlaw",
  340. .fops = &audio_g711_fops,
  341. };
  342. int __init audio_g711mlaw_init(void)
  343. {
  344. int ret = misc_register(&audio_g711mlaw_misc);
  345. if (ret == 0)
  346. device_init_wakeup(audio_g711mlaw_misc.this_device, true);
  347. audio_g711_ws_mgr.ref_cnt = 0;
  348. mutex_init(&audio_g711_ws_mgr.ws_lock);
  349. return ret;
  350. }
  351. void audio_g711mlaw_exit(void)
  352. {
  353. mutex_destroy(&audio_g711_ws_mgr.ws_lock);
  354. misc_deregister(&audio_g711mlaw_misc);
  355. }