audio_g711mlaw.c 11 KB

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