blob: 87e4f4ec0fdb144c8201da5c0381032bb1ae451f [file] [log] [blame]
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301/* aac audio output device
2 *
3 * Copyright (C) 2008 Google, Inc.
4 * Copyright (C) 2008 HTC Corporation
Satya Krishna Pindiproli3442f932018-01-10 16:21:40 +05305 * Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05306 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#include <linux/msm_audio_aac.h>
19#include <linux/compat.h>
20#include <soc/qcom/socinfo.h>
21#include "audio_utils_aio.h"
22
23#define AUDIO_AAC_DUAL_MONO_INVALID -1
24
25
26/* Default number of pre-allocated event packets */
27#define PCM_BUFSZ_MIN_AACM ((8*1024) + sizeof(struct dec_meta_out))
28static struct miscdevice audio_multiaac_misc;
29static struct ws_mgr audio_multiaac_ws_mgr;
30
31#ifdef CONFIG_DEBUG_FS
32static const struct file_operations audio_aac_debug_fops = {
33 .read = audio_aio_debug_read,
34 .open = audio_aio_debug_open,
35};
36#endif
37
38static long audio_ioctl_shared(struct file *file, unsigned int cmd,
39 void *arg)
40{
41 struct q6audio_aio *audio = file->private_data;
42 int rc = 0;
43
44 switch (cmd) {
45 case AUDIO_START: {
46 struct asm_aac_cfg aac_cfg;
47 struct msm_audio_aac_config *aac_config;
48 uint32_t sbr_ps = 0x00;
49
50 aac_config = (struct msm_audio_aac_config *)audio->codec_cfg;
51 if (audio->feedback == TUNNEL_MODE) {
52 aac_cfg.sample_rate = aac_config->sample_rate;
53 aac_cfg.ch_cfg = aac_config->channel_configuration;
54 } else {
55 aac_cfg.sample_rate = audio->pcm_cfg.sample_rate;
56 aac_cfg.ch_cfg = audio->pcm_cfg.channel_count;
57 }
58 pr_debug("%s: AUDIO_START session_id[%d]\n", __func__,
59 audio->ac->session);
60 if (audio->feedback == NON_TUNNEL_MODE) {
61 /* Configure PCM output block */
Satya Krishna Pindiproli3442f932018-01-10 16:21:40 +053062 rc = q6asm_enc_cfg_blk_pcm_native(audio->ac,
Satya Krishna Pindiprolie169e992017-09-22 11:34:35 +053063 audio->pcm_cfg.sample_rate,
64 audio->pcm_cfg.channel_count);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053065 if (rc < 0) {
66 pr_err("pcm output block config failed\n");
67 break;
68 }
69 }
70 /* turn on both sbr and ps */
71 rc = q6asm_enable_sbrps(audio->ac, sbr_ps);
72 if (rc < 0)
73 pr_err("sbr-ps enable failed\n");
74 if (aac_config->sbr_ps_on_flag)
75 aac_cfg.aot = AAC_ENC_MODE_EAAC_P;
76 else if (aac_config->sbr_on_flag)
77 aac_cfg.aot = AAC_ENC_MODE_AAC_P;
78 else
79 aac_cfg.aot = AAC_ENC_MODE_AAC_LC;
80
81 switch (aac_config->format) {
82 case AUDIO_AAC_FORMAT_ADTS:
83 aac_cfg.format = 0x00;
84 break;
85 case AUDIO_AAC_FORMAT_LOAS:
86 aac_cfg.format = 0x01;
87 break;
88 case AUDIO_AAC_FORMAT_ADIF:
89 aac_cfg.format = 0x02;
90 break;
91 default:
92 case AUDIO_AAC_FORMAT_RAW:
93 aac_cfg.format = 0x03;
94 }
95 aac_cfg.ep_config = aac_config->ep_config;
96 aac_cfg.section_data_resilience =
97 aac_config->aac_section_data_resilience_flag;
98 aac_cfg.scalefactor_data_resilience =
99 aac_config->aac_scalefactor_data_resilience_flag;
100 aac_cfg.spectral_data_resilience =
101 aac_config->aac_spectral_data_resilience_flag;
102
103 pr_debug("%s:format=%x aot=%d ch=%d sr=%d\n",
104 __func__, aac_cfg.format,
105 aac_cfg.aot, aac_cfg.ch_cfg,
106 aac_cfg.sample_rate);
107
108 /* Configure Media format block */
109 rc = q6asm_media_format_block_multi_aac(audio->ac, &aac_cfg);
110 if (rc < 0) {
111 pr_err("cmd media format block failed\n");
112 break;
113 }
Xiaoyu Yed99897f2017-11-13 23:44:32 -0800114
115 /* Fall back to the default number of channels
116 * if aac_cfg.ch_cfg is not between 1-6
117 */
118 if ((aac_cfg.ch_cfg == 0) || (aac_cfg.ch_cfg > 6))
119 aac_cfg.ch_cfg = 2;
120
121 rc = q6asm_set_encdec_chan_map(audio->ac, aac_cfg.ch_cfg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530122 if (rc < 0) {
123 pr_err("%s: cmd set encdec_chan_map failed\n",
124 __func__);
125 break;
126 }
127 rc = audio_aio_enable(audio);
128 audio->eos_rsp = 0;
129 audio->eos_flag = 0;
130 if (!rc) {
131 audio->enabled = 1;
132 } else {
133 audio->enabled = 0;
134 pr_err("Audio Start procedure failed rc=%d\n", rc);
135 break;
136 }
137 pr_info("%s: AUDIO_START sessionid[%d]enable[%d]\n", __func__,
138 audio->ac->session,
139 audio->enabled);
140 if (audio->stopped == 1)
141 audio->stopped = 0;
142 break;
143 }
144 case AUDIO_SET_AAC_CONFIG: {
145 struct msm_audio_aac_config *aac_config;
146 uint16_t sce_left = 1, sce_right = 2;
147
148 if (arg == NULL) {
149 pr_err("%s: NULL config pointer\n", __func__);
150 rc = -EINVAL;
151 break;
152 }
153 memcpy(audio->codec_cfg, arg,
154 sizeof(struct msm_audio_aac_config));
155 aac_config = audio->codec_cfg;
156 if (aac_config->dual_mono_mode >
157 AUDIO_AAC_DUAL_MONO_PL_SR) {
158 pr_err("%s:AUDIO_SET_AAC_CONFIG: Invalid dual_mono mode =%d\n",
159 __func__, aac_config->dual_mono_mode);
160 } else {
161 /* convert the data from user into sce_left
162 * and sce_right based on the definitions
163 */
164 pr_debug("%s: AUDIO_SET_AAC_CONFIG: modify dual_mono mode =%d\n",
165 __func__, aac_config->dual_mono_mode);
166 switch (aac_config->dual_mono_mode) {
167 case AUDIO_AAC_DUAL_MONO_PL_PR:
168 sce_left = 1;
169 sce_right = 1;
170 break;
171 case AUDIO_AAC_DUAL_MONO_SL_SR:
172 sce_left = 2;
173 sce_right = 2;
174 break;
175 case AUDIO_AAC_DUAL_MONO_SL_PR:
176 sce_left = 2;
177 sce_right = 1;
178 break;
179 case AUDIO_AAC_DUAL_MONO_PL_SR:
180 default:
181 sce_left = 1;
182 sce_right = 2;
183 break;
184 }
185 rc = q6asm_cfg_dual_mono_aac(audio->ac,
186 sce_left, sce_right);
187 if (rc < 0)
188 pr_err("%s: asm cmd dualmono failed rc=%d\n",
189 __func__, rc);
190 } break;
191 break;
192 }
193 case AUDIO_SET_AAC_MIX_CONFIG: {
194 u32 *mix_coeff = (u32 *)arg;
195
196 if (!arg) {
197 pr_err("%s: Invalid param for %s\n",
198 __func__, "AUDIO_SET_AAC_MIX_CONFIG");
199 rc = -EINVAL;
200 break;
201 }
202 pr_debug("%s, AUDIO_SET_AAC_MIX_CONFIG", __func__);
203 pr_debug("%s, value of coeff = %d",
204 __func__, *mix_coeff);
205 q6asm_cfg_aac_sel_mix_coef(audio->ac, *mix_coeff);
206 if (rc < 0)
207 pr_err("%s asm aac_sel_mix_coef failed rc=%d\n",
208 __func__, rc);
209 break;
210 }
211 default:
212 pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
213 rc = -EINVAL;
214 break;
215 }
216 return rc;
217}
218
219static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
220{
221 struct q6audio_aio *audio = file->private_data;
222 int rc = 0;
223
224 switch (cmd) {
225 case AUDIO_START: {
226 rc = audio_ioctl_shared(file, cmd, (void *)arg);
227 break;
228 }
229 case AUDIO_GET_AAC_CONFIG: {
230 if (copy_to_user((void *)arg, audio->codec_cfg,
231 sizeof(struct msm_audio_aac_config))) {
232 pr_err("%s: copy_to_user for AUDIO_GET_AAC_CONFIG failed\n"
233 , __func__);
234 rc = -EFAULT;
235 break;
236 }
237 break;
238 }
239 case AUDIO_SET_AAC_CONFIG: {
240 struct msm_audio_aac_config aac_config;
241
242 if (copy_from_user(&aac_config, (void *)arg,
243 sizeof(aac_config))) {
244 pr_err("%s: copy_from_user for AUDIO_SET_AAC_CONFIG failed\n"
245 , __func__);
246 rc = -EFAULT;
247 }
248 rc = audio_ioctl_shared(file, cmd, &aac_config);
249 if (rc)
250 pr_err("%s:AUDIO_SET_AAC_CONFIG failed. Rc= %d\n",
251 __func__, rc);
252 break;
253 }
254 case AUDIO_SET_AAC_MIX_CONFIG: {
255 u32 mix_config;
256
257 pr_debug("%s, AUDIO_SET_AAC_MIX_CONFIG", __func__);
258 if (copy_from_user(&mix_config, (void *)arg,
259 sizeof(u32))) {
260 pr_err("%s: copy_from_user for AUDIO_SET_AAC_MIX_CONFIG failed\n",
261 __func__);
262 rc = -EFAULT;
263 break;
264 }
265 rc = audio_ioctl_shared(file, cmd, &mix_config);
266 if (rc)
267 pr_err("%s:AUDIO_SET_AAC_CONFIG failed. Rc= %d\n",
268 __func__, rc);
269 break;
270 }
271 default: {
272 pr_debug("Calling utils ioctl\n");
273 rc = audio->codec_ioctl(file, cmd, arg);
274 }
275 }
276 return rc;
277}
278
279#ifdef CONFIG_COMPAT
280struct msm_audio_aac_config32 {
281 s16 format;
282 u16 audio_object;
283 u16 ep_config; /* 0 ~ 3 useful only obj = ERLC */
284 u16 aac_section_data_resilience_flag;
285 u16 aac_scalefactor_data_resilience_flag;
286 u16 aac_spectral_data_resilience_flag;
287 u16 sbr_on_flag;
288 u16 sbr_ps_on_flag;
289 u16 dual_mono_mode;
290 u16 channel_configuration;
291 u16 sample_rate;
292};
293
294enum {
295 AUDIO_SET_AAC_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC,
296 (AUDIO_MAX_COMMON_IOCTL_NUM+0), struct msm_audio_aac_config32),
297 AUDIO_GET_AAC_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC,
298 (AUDIO_MAX_COMMON_IOCTL_NUM+1), struct msm_audio_aac_config32),
299};
300
301static long audio_compat_ioctl(struct file *file, unsigned int cmd,
302 unsigned long arg)
303{
304 struct q6audio_aio *audio = file->private_data;
305 int rc = 0;
306
307 switch (cmd) {
308 case AUDIO_START: {
309 rc = audio_ioctl_shared(file, cmd, (void *)arg);
310 break;
311 }
312 case AUDIO_GET_AAC_CONFIG_32: {
313 struct msm_audio_aac_config *aac_config;
314 struct msm_audio_aac_config32 aac_config_32;
315
316 memset(&aac_config_32, 0, sizeof(aac_config_32));
317
318 aac_config = (struct msm_audio_aac_config *)audio->codec_cfg;
319 aac_config_32.format = aac_config->format;
320 aac_config_32.audio_object = aac_config->audio_object;
321 aac_config_32.ep_config = aac_config->ep_config;
322 aac_config_32.aac_section_data_resilience_flag =
323 aac_config->aac_section_data_resilience_flag;
324 aac_config_32.aac_scalefactor_data_resilience_flag =
325 aac_config->aac_scalefactor_data_resilience_flag;
326 aac_config_32.aac_spectral_data_resilience_flag =
327 aac_config->aac_spectral_data_resilience_flag;
328 aac_config_32.sbr_on_flag = aac_config->sbr_on_flag;
329 aac_config_32.sbr_ps_on_flag = aac_config->sbr_ps_on_flag;
330 aac_config_32.dual_mono_mode = aac_config->dual_mono_mode;
331 aac_config_32.channel_configuration =
332 aac_config->channel_configuration;
333 aac_config_32.sample_rate = aac_config->sample_rate;
334
335 if (copy_to_user((void *)arg, &aac_config_32,
336 sizeof(aac_config_32))) {
337 pr_err("%s: copy_to_user for AUDIO_GET_AAC_CONFIG_32 failed\n",
338 __func__);
339 rc = -EFAULT;
340 break;
341 }
342 break;
343 }
344 case AUDIO_SET_AAC_CONFIG_32: {
345 struct msm_audio_aac_config aac_config;
346 struct msm_audio_aac_config32 aac_config_32;
347
348 pr_debug("%s: AUDIO_SET_AAC_CONFIG\n", __func__);
349 if (copy_from_user(&aac_config_32, (void *)arg,
350 sizeof(aac_config_32))) {
351 pr_err(
352 "%s: copy_from_user for AUDIO_SET_AAC_CONFIG_32 failed",
353 __func__);
354 rc = -EFAULT;
355 break;
356 }
357 aac_config.format = aac_config_32.format;
358 aac_config.audio_object = aac_config_32.audio_object;
359 aac_config.ep_config = aac_config_32.ep_config;
360 aac_config.aac_section_data_resilience_flag =
361 aac_config_32.aac_section_data_resilience_flag;
362 aac_config.aac_scalefactor_data_resilience_flag =
363 aac_config_32.aac_scalefactor_data_resilience_flag;
364 aac_config.aac_spectral_data_resilience_flag =
365 aac_config_32.aac_spectral_data_resilience_flag;
366 aac_config.sbr_on_flag = aac_config_32.sbr_on_flag;
367 aac_config.sbr_ps_on_flag = aac_config_32.sbr_ps_on_flag;
368 aac_config.dual_mono_mode = aac_config_32.dual_mono_mode;
369 aac_config.channel_configuration =
370 aac_config_32.channel_configuration;
371 aac_config.sample_rate = aac_config_32.sample_rate;
372
373 cmd = AUDIO_SET_AAC_CONFIG;
374 rc = audio_ioctl_shared(file, cmd, &aac_config);
375 if (rc)
376 pr_err("%s:AUDIO_SET_AAC_CONFIG failed. rc= %d\n",
377 __func__, rc);
378 break;
379 }
380 case AUDIO_SET_AAC_MIX_CONFIG: {
381 u32 mix_config;
382
383 pr_debug("%s, AUDIO_SET_AAC_MIX_CONFIG\n", __func__);
384 if (copy_from_user(&mix_config, (void *)arg,
385 sizeof(u32))) {
386 pr_err("%s: copy_from_user for AUDIO_SET_AAC_MIX_CONFIG failed\n"
387 , __func__);
388 rc = -EFAULT;
389 break;
390 }
391 rc = audio_ioctl_shared(file, cmd, &mix_config);
392 if (rc)
393 pr_err("%s:AUDIO_SET_AAC_CONFIG failed. Rc= %d\n",
394 __func__, rc);
395 break;
396 }
397 default: {
398 pr_debug("Calling utils ioctl\n");
399 rc = audio->codec_compat_ioctl(file, cmd, arg);
400 }
401 }
402 return rc;
403}
404#else
405#define audio_compat_ioctl NULL
406#endif
407
408static int audio_open(struct inode *inode, struct file *file)
409{
410 struct q6audio_aio *audio = NULL;
411 int rc = 0;
412 struct msm_audio_aac_config *aac_config = NULL;
413
414#ifdef CONFIG_DEBUG_FS
415 /* 4 bytes represents decoder number, 1 byte for terminate string */
416 char name[sizeof "msm_multi_aac_" + 5];
417#endif
418 audio = kzalloc(sizeof(struct q6audio_aio), GFP_KERNEL);
419
420 if (audio == NULL)
421 return -ENOMEM;
422
423 audio->codec_cfg = kzalloc(sizeof(struct msm_audio_aac_config),
424 GFP_KERNEL);
425 if (audio->codec_cfg == NULL) {
426 kfree(audio);
427 return -ENOMEM;
428 }
429
430 aac_config = audio->codec_cfg;
431
432 audio->pcm_cfg.buffer_size = PCM_BUFSZ_MIN_AACM;
433 audio->miscdevice = &audio_multiaac_misc;
434 audio->wakelock_voted = false;
435 audio->audio_ws_mgr = &audio_multiaac_ws_mgr;
436 aac_config->dual_mono_mode = AUDIO_AAC_DUAL_MONO_INVALID;
437
438 audio->ac = q6asm_audio_client_alloc((app_cb) q6_audio_cb,
439 (void *)audio);
440
441 if (!audio->ac) {
442 pr_err("Could not allocate memory for audio client\n");
443 kfree(audio->codec_cfg);
444 kfree(audio);
445 return -ENOMEM;
446 }
447 rc = audio_aio_open(audio, file);
448 if (rc < 0) {
449 pr_err("%s: audio_aio_open rc=%d\n",
450 __func__, rc);
451 goto fail;
452 }
453
454 /* open in T/NT mode */
455 if ((file->f_mode & FMODE_WRITE) && (file->f_mode & FMODE_READ)) {
456 rc = q6asm_open_read_write(audio->ac, FORMAT_LINEAR_PCM,
457 FORMAT_MPEG4_MULTI_AAC);
458 if (rc < 0) {
459 pr_err("NT mode Open failed rc=%d\n", rc);
460 rc = -ENODEV;
461 goto fail;
462 }
463 audio->feedback = NON_TUNNEL_MODE;
464 /* open AAC decoder, expected frames is always 1
465 * audio->buf_cfg.frames_per_buf = 0x01;
466 */
467 audio->buf_cfg.meta_info_enable = 0x01;
468 } else if ((file->f_mode & FMODE_WRITE) &&
469 !(file->f_mode & FMODE_READ)) {
470 rc = q6asm_open_write(audio->ac, FORMAT_MPEG4_MULTI_AAC);
471 if (rc < 0) {
472 pr_err("T mode Open failed rc=%d\n", rc);
473 rc = -ENODEV;
474 goto fail;
475 }
476 audio->feedback = TUNNEL_MODE;
477 audio->buf_cfg.meta_info_enable = 0x00;
478 } else {
479 pr_err("Not supported mode\n");
480 rc = -EACCES;
481 goto fail;
482 }
483
484#ifdef CONFIG_DEBUG_FS
485 snprintf(name, sizeof(name), "msm_multi_aac_%04x", audio->ac->session);
486 audio->dentry = debugfs_create_file(name, S_IFREG | 0444,
487 NULL, (void *)audio,
488 &audio_aac_debug_fops);
489
490 if (IS_ERR(audio->dentry))
491 pr_debug("debugfs_create_file failed\n");
492#endif
493 pr_info("%s:AAC 5.1 Decoder OPEN success mode[%d]session[%d]\n",
494 __func__, audio->feedback, audio->ac->session);
495 return rc;
496fail:
497 q6asm_audio_client_free(audio->ac);
498 kfree(audio->codec_cfg);
499 kfree(audio);
500 return rc;
501}
502
503static const struct file_operations audio_aac_fops = {
504 .owner = THIS_MODULE,
505 .open = audio_open,
506 .release = audio_aio_release,
507 .unlocked_ioctl = audio_ioctl,
508 .fsync = audio_aio_fsync,
509 .compat_ioctl = audio_compat_ioctl
510};
511
512static struct miscdevice audio_multiaac_misc = {
513 .minor = MISC_DYNAMIC_MINOR,
514 .name = "msm_multi_aac",
515 .fops = &audio_aac_fops,
516};
517
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530518int __init audio_multiaac_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530519{
520 int ret = misc_register(&audio_multiaac_misc);
521
522 if (ret == 0)
523 device_init_wakeup(audio_multiaac_misc.this_device, true);
524 audio_multiaac_ws_mgr.ref_cnt = 0;
525 mutex_init(&audio_multiaac_ws_mgr.ws_lock);
526
527 return ret;
528}
529
Asish Bhattacharya5faacb32017-12-04 17:23:15 +0530530void audio_multiaac_exit(void)
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530531{
532 mutex_destroy(&audio_multiaac_ws_mgr.ws_lock);
533 misc_deregister(&audio_multiaac_misc);
534}