summaryrefslogtreecommitdiff
path: root/dsp/codecs/audio_hwacc_effects.c
blob: cd54b90cf338ab545ccdaa47922ab4a3a522ab19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/*
 * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/msm_audio.h>
#include <linux/compat.h>
#include "q6audio_common.h"
#include <dsp/msm-audio-effects-q6-v2.h>
#include "audio_utils_aio.h"

#define MAX_CHANNELS_SUPPORTED		8
#define WAIT_TIMEDOUT_DURATION_SECS	1

struct q6audio_effects {
	wait_queue_head_t		read_wait;
	wait_queue_head_t		write_wait;

	struct audio_client             *ac;
	struct msm_hwacc_effects_config  config;

	struct mutex			lock;

	atomic_t			in_count;
	atomic_t			out_count;

	int				opened;
	int				started;
	int				buf_alloc;
	struct msm_nt_eff_all_config audio_effects;
};

static void audio_effects_init_pp(struct audio_client *ac)
{
	int ret = 0;
	struct asm_softvolume_params softvol = {
		.period = SOFT_VOLUME_PERIOD,
		.step = SOFT_VOLUME_STEP,
		.rampingcurve = SOFT_VOLUME_CURVE_LINEAR,
	};

	if (!ac) {
		pr_err("%s: audio client null to init pp\n", __func__);
		return;
	}
	ret = q6asm_set_softvolume_v2(ac, &softvol,
				      SOFT_VOLUME_INSTANCE_1);
	if (ret < 0)
		pr_err("%s: Send SoftVolume Param failed ret=%d\n",
			__func__, ret);
}

static void audio_effects_deinit_pp(struct audio_client *ac)
{
	if (!ac) {
		pr_err("%s: audio client null to deinit pp\n", __func__);
		return;
	}
}

static void audio_effects_event_handler(uint32_t opcode, uint32_t token,
				 uint32_t *payload,  void *priv)
{
	struct q6audio_effects *effects;

	if (!payload || !priv) {
		pr_err("%s: invalid data to handle events, payload: %pK, priv: %pK\n",
			__func__, payload, priv);
		return;
	}

	effects = (struct q6audio_effects *)priv;
	switch (opcode) {
	case ASM_DATA_EVENT_WRITE_DONE_V2: {
		atomic_inc(&effects->out_count);
		wake_up(&effects->write_wait);
		break;
	}
	case ASM_DATA_EVENT_READ_DONE_V2: {
		atomic_inc(&effects->in_count);
		wake_up(&effects->read_wait);
		break;
	}
	case APR_BASIC_RSP_RESULT: {
		pr_debug("%s: APR_BASIC_RSP_RESULT Cmd[0x%x] Status[0x%x]\n",
			 __func__, payload[0], payload[1]);
		switch (payload[0]) {
		case ASM_SESSION_CMD_RUN_V2:
			pr_debug("ASM_SESSION_CMD_RUN_V2\n");
			break;
		default:
			pr_debug("%s: Payload = [0x%x] stat[0x%x]\n",
				 __func__, payload[0], payload[1]);
			break;
		}
		break;
	}
	default:
		pr_debug("%s: Unhandled Event 0x%x token = 0x%x\n",
			 __func__, opcode, token);
		break;
	}
}

static int audio_effects_shared_ioctl(struct file *file, unsigned int cmd,
				      unsigned long arg)
{
	struct q6audio_effects *effects = file->private_data;
	int rc = 0;

	switch (cmd) {
	case AUDIO_START: {
		pr_debug("%s: AUDIO_START\n", __func__);

		mutex_lock(&effects->lock);

		rc = q6asm_open_read_write_v2(effects->ac,
					FORMAT_LINEAR_PCM,
					FORMAT_MULTI_CHANNEL_LINEAR_PCM,
					effects->config.meta_mode_enabled,
					effects->config.output.bits_per_sample,
					true /*overwrite topology*/,
					ASM_STREAM_POSTPROC_TOPO_ID_HPX_MASTER);
		if (rc < 0) {
			pr_err("%s: Open failed for hw accelerated effects:rc=%d\n",
				__func__, rc);
			rc = -EINVAL;
			mutex_unlock(&effects->lock);
			goto ioctl_fail;
		}
		effects->opened = 1;

		pr_debug("%s: dec buf size: %d, num_buf: %d, enc buf size: %d, num_buf: %d\n",
			 __func__, effects->config.output.buf_size,
			 effects->config.output.num_buf,
			 effects->config.input.buf_size,
			 effects->config.input.num_buf);
		rc = q6asm_audio_client_buf_alloc_contiguous(IN, effects->ac,
					effects->config.output.buf_size,
					effects->config.output.num_buf);
		if (rc < 0) {
			pr_err("%s: Write buffer Allocation failed rc = %d\n",
				__func__, rc);
			rc = -ENOMEM;
			mutex_unlock(&effects->lock);
			goto ioctl_fail;
		}
		atomic_set(&effects->in_count, effects->config.input.num_buf);
		rc = q6asm_audio_client_buf_alloc_contiguous(OUT, effects->ac,
					effects->config.input.buf_size,
					effects->config.input.num_buf);
		if (rc < 0) {
			pr_err("%s: Read buffer Allocation failed rc = %d\n",
				__func__, rc);
			rc = -ENOMEM;
			goto readbuf_fail;
		}
		atomic_set(&effects->out_count, effects->config.output.num_buf);
		effects->buf_alloc = 1;

		pr_debug("%s: enc: sample_rate: %d, num_channels: %d\n",
			 __func__, effects->config.input.sample_rate,
			effects->config.input.num_channels);
		rc = q6asm_enc_cfg_blk_pcm(effects->ac,
					   effects->config.input.sample_rate,
					   effects->config.input.num_channels);
		if (rc < 0) {
			pr_err("%s: pcm read block config failed\n", __func__);
			rc = -EINVAL;
			goto cfg_fail;
		}
		pr_debug("%s: dec: sample_rate: %d, num_channels: %d, bit_width: %d\n",
			 __func__, effects->config.output.sample_rate,
			effects->config.output.num_channels,
			effects->config.output.bits_per_sample);
		rc = q6asm_media_format_block_pcm_format_support(
				effects->ac, effects->config.output.sample_rate,
				effects->config.output.num_channels,
				effects->config.output.bits_per_sample);
		if (rc < 0) {
			pr_err("%s: pcm write format block config failed\n",
				__func__);
			rc = -EINVAL;
			goto cfg_fail;
		}

		audio_effects_init_pp(effects->ac);

		rc = q6asm_run(effects->ac, 0x00, 0x00, 0x00);
		if (!rc)
			effects->started = 1;
		else {
			effects->started = 0;
			pr_err("%s: ASM run state failed\n", __func__);
		}
		mutex_unlock(&effects->lock);
		break;
	}
	case AUDIO_EFFECTS_WRITE: {
		char *bufptr = NULL;
		uint32_t idx = 0;
		uint32_t size = 0;

		mutex_lock(&effects->lock);

		if (!effects->started) {
			rc = -EFAULT;
			mutex_unlock(&effects->lock);
			goto ioctl_fail;
		}

		rc = wait_event_timeout(effects->write_wait,
					atomic_read(&effects->out_count),
					WAIT_TIMEDOUT_DURATION_SECS * HZ);
		if (!rc) {
			pr_err("%s: write wait_event_timeout\n", __func__);
			rc = -EFAULT;
			 mutex_unlock(&effects->lock);
			goto ioctl_fail;
		}
		if (!atomic_read(&effects->out_count)) {
			pr_err("%s: pcm stopped out_count 0\n", __func__);
			rc = -EFAULT;
			mutex_unlock(&effects->lock);
			goto ioctl_fail;
		}

		bufptr = q6asm_is_cpu_buf_avail(IN, effects->ac, &size, &idx);
		if (bufptr) {
			if ((effects->config.buf_cfg.output_len > size) ||
				copy_from_user(bufptr, (void *)arg,
					effects->config.buf_cfg.output_len)) {
				rc = -EFAULT;
				mutex_unlock(&effects->lock);
				goto ioctl_fail;
			}
			rc = q6asm_write(effects->ac,
					 effects->config.buf_cfg.output_len,
					 0, 0, NO_TIMESTAMP);
			if (rc < 0) {
				rc = -EFAULT;
				mutex_unlock(&effects->lock);
				goto ioctl_fail;
			}
			atomic_dec(&effects->out_count);
		} else {
			pr_err("%s: AUDIO_EFFECTS_WRITE: Buffer dropped\n",
				__func__);
		}
		mutex_unlock(&effects->lock);
		break;
	}
	case AUDIO_EFFECTS_READ: {
		char *bufptr = NULL;
		uint32_t idx = 0;
		uint32_t size = 0;

		mutex_lock(&effects->lock);

		if (!effects->started) {
			rc = -EFAULT;
			mutex_unlock(&effects->lock);
			goto ioctl_fail;
		}

		atomic_set(&effects->in_count, 0);

		q6asm_read_v2(effects->ac, effects->config.buf_cfg.input_len);
		/* Read might fail initially, don't error out */
		if (rc < 0)
			pr_err("%s: read failed\n", __func__);

		rc = wait_event_timeout(effects->read_wait,
					atomic_read(&effects->in_count),
					WAIT_TIMEDOUT_DURATION_SECS * HZ);
		if (!rc) {
			pr_err("%s: read wait_event_timeout\n", __func__);
			rc = -EFAULT;
			mutex_unlock(&effects->lock);
			goto ioctl_fail;
		}
		if (!atomic_read(&effects->in_count)) {
			pr_err("%s: pcm stopped in_count 0\n", __func__);
			rc = -EFAULT;
			mutex_unlock(&effects->lock);
			goto ioctl_fail;
		}

		bufptr = q6asm_is_cpu_buf_avail(OUT, effects->ac, &size, &idx);
		if (bufptr) {
			if (!((void *)arg)) {
				rc = -EFAULT;
				mutex_unlock(&effects->lock);
				goto ioctl_fail;
			}
			if ((effects->config.buf_cfg.input_len > size) ||
				copy_to_user((void *)arg, bufptr,
					  effects->config.buf_cfg.input_len)) {
				rc = -EFAULT;
				mutex_unlock(&effects->lock);
				goto ioctl_fail;
			}
		}
		mutex_unlock(&effects->lock);
		break;
	}
	default:
		pr_err("%s: Invalid effects config module\n", __func__);
		rc = -EINVAL;
		break;
	}
ioctl_fail:
	return rc;
readbuf_fail:
	q6asm_audio_client_buf_free_contiguous(IN,
					effects->ac);
	mutex_unlock(&effects->lock);
	return rc;
cfg_fail:
	q6asm_audio_client_buf_free_contiguous(IN,
					effects->ac);
	q6asm_audio_client_buf_free_contiguous(OUT,
					effects->ac);
	effects->buf_alloc = 0;
	mutex_unlock(&effects->lock);
	return rc;
}

static long audio_effects_set_pp_param(struct q6audio_effects *effects,
				long *values)
{
	int rc = 0;
	int effects_module = values[0];

	switch (effects_module) {
	case VIRTUALIZER_MODULE:
		pr_debug("%s: VIRTUALIZER_MODULE\n", __func__);
		if (msm_audio_effects_is_effmodule_supp_in_top(
			effects_module, effects->ac->topology))
			msm_audio_effects_virtualizer_handler(
				effects->ac,
				&(effects->audio_effects.virtualizer),
				(long *)&values[1]);
		break;
	case REVERB_MODULE:
		pr_debug("%s: REVERB_MODULE\n", __func__);
		if (msm_audio_effects_is_effmodule_supp_in_top(
			effects_module, effects->ac->topology))
			msm_audio_effects_reverb_handler(effects->ac,
				 &(effects->audio_effects.reverb),
				 (long *)&values[1]);
		break;
	case BASS_BOOST_MODULE:
		pr_debug("%s: BASS_BOOST_MODULE\n", __func__);
		if (msm_audio_effects_is_effmodule_supp_in_top(
			effects_module, effects->ac->topology))
			msm_audio_effects_bass_boost_handler(
				effects->ac,
				&(effects->audio_effects.bass_boost),
				(long *)&values[1]);
		break;
	case PBE_MODULE:
		pr_debug("%s: PBE_MODULE\n", __func__);
		if (msm_audio_effects_is_effmodule_supp_in_top(
			effects_module, effects->ac->topology))
			msm_audio_effects_pbe_handler(
				effects->ac,
				&(effects->audio_effects.pbe),
				(long *)&values[1]);
		break;
	case EQ_MODULE:
		pr_debug("%s: EQ_MODULE\n", __func__);
		if (msm_audio_effects_is_effmodule_supp_in_top(
			effects_module, effects->ac->topology))
			msm_audio_effects_popless_eq_handler(
				effects->ac,
				&(effects->audio_effects.equalizer),
				(long *)&values[1]);
		break;
	case SOFT_VOLUME_MODULE:
		pr_debug("%s: SA PLUS VOLUME_MODULE\n", __func__);
		msm_audio_effects_volume_handler_v2(effects->ac,
				&(effects->audio_effects.saplus_vol),
				(long *)&values[1], SOFT_VOLUME_INSTANCE_1);
		break;
	case SOFT_VOLUME2_MODULE:
		pr_debug("%s: TOPOLOGY SWITCH VOLUME MODULE\n",
			 __func__);
		if (msm_audio_effects_is_effmodule_supp_in_top(
			effects_module, effects->ac->topology))
			msm_audio_effects_volume_handler_v2(effects->ac,
			      &(effects->audio_effects.topo_switch_vol),
			      (long *)&values[1], SOFT_VOLUME_INSTANCE_2);
		break;
	default:
		pr_err("%s: Invalid effects config module\n", __func__);
		rc = -EINVAL;
	}
	return rc;
}

static long audio_effects_ioctl(struct file *file, unsigned int cmd,
				unsigned long arg)
{
	struct q6audio_effects *effects = file->private_data;
	int rc = 0;
	long argvalues[MAX_PP_PARAMS_SZ] = {0};

	switch (cmd) {
	case AUDIO_SET_EFFECTS_CONFIG: {
		pr_debug("%s: AUDIO_SET_EFFECTS_CONFIG\n", __func__);
		mutex_lock(&effects->lock);
		memset(&effects->config, 0, sizeof(effects->config));
		if (copy_from_user(&effects->config, (void *)arg,
				   sizeof(effects->config))) {
			pr_err("%s: copy from user for AUDIO_SET_EFFECTS_CONFIG failed\n",
				__func__);
			rc = -EFAULT;
		}
		pr_debug("%s: write buf_size: %d, num_buf: %d, sample_rate: %d, channel: %d\n",
			 __func__, effects->config.output.buf_size,
			 effects->config.output.num_buf,
			 effects->config.output.sample_rate,
			 effects->config.output.num_channels);
		pr_debug("%s: read buf_size: %d, num_buf: %d, sample_rate: %d, channel: %d\n",
			 __func__, effects->config.input.buf_size,
			 effects->config.input.num_buf,
			 effects->config.input.sample_rate,
			 effects->config.input.num_channels);
		mutex_unlock(&effects->lock);
		break;
	}
	case AUDIO_EFFECTS_SET_BUF_LEN: {
		mutex_lock(&effects->lock);
		if (copy_from_user(&effects->config.buf_cfg, (void *)arg,
				   sizeof(effects->config.buf_cfg))) {
			pr_err("%s: copy from user for AUDIO_EFFECTS_SET_BUF_LEN failed\n",
				__func__);
			rc = -EFAULT;
		}
		pr_debug("%s: write buf len: %d, read buf len: %d\n",
			 __func__, effects->config.buf_cfg.output_len,
			 effects->config.buf_cfg.input_len);
		mutex_unlock(&effects->lock);
		break;
	}
	case AUDIO_EFFECTS_GET_BUF_AVAIL: {
		struct msm_hwacc_buf_avail buf_avail;

		buf_avail.input_num_avail = atomic_read(&effects->in_count);
		buf_avail.output_num_avail = atomic_read(&effects->out_count);
		mutex_lock(&effects->lock);
		pr_debug("%s: write buf avail: %d, read buf avail: %d\n",
			 __func__, buf_avail.output_num_avail,
			 buf_avail.input_num_avail);
		if (copy_to_user((void *)arg, &buf_avail,
				   sizeof(buf_avail))) {
			pr_err("%s: copy to user for AUDIO_EFFECTS_GET_NUM_BUF_AVAIL failed\n",
				__func__);
			rc = -EFAULT;
		}
		mutex_unlock(&effects->lock);
		break;
	}
	case AUDIO_EFFECTS_SET_PP_PARAMS: {
		mutex_lock(&effects->lock);
		if (copy_from_user(argvalues, (void *)arg,
				   MAX_PP_PARAMS_SZ*sizeof(long))) {
			pr_err("%s: copy from user for pp params failed\n",
				__func__);
			mutex_unlock(&effects->lock);
			return -EFAULT;
		}
		rc = audio_effects_set_pp_param(effects, argvalues);
		mutex_unlock(&effects->lock);
		break;
	}
	default:
		pr_debug("%s: Calling shared ioctl\n", __func__);
		rc = audio_effects_shared_ioctl(file, cmd, arg);
		break;
	}
	if (rc)
		pr_err("%s: cmd 0x%x failed\n", __func__, cmd);
	return rc;
}

#ifdef CONFIG_COMPAT
struct msm_hwacc_data_config32 {
	__u32 buf_size;
	__u32 num_buf;
	__u32 num_channels;
	__u8 channel_map[MAX_CHANNELS_SUPPORTED];
	__u32 sample_rate;
	__u32 bits_per_sample;
};

struct msm_hwacc_buf_cfg32 {
	__u32 input_len;
	__u32 output_len;
};

struct msm_hwacc_buf_avail32 {
	__u32 input_num_avail;
	__u32 output_num_avail;
};

struct msm_hwacc_effects_config32 {
	struct msm_hwacc_data_config32 input;
	struct msm_hwacc_data_config32 output;
	struct msm_hwacc_buf_cfg32 buf_cfg;
	__u32 meta_mode_enabled;
	__u32 overwrite_topology;
	__s32 topology;
};

enum {
	AUDIO_SET_EFFECTS_CONFIG32 = _IOW(AUDIO_IOCTL_MAGIC, 99,
					  struct msm_hwacc_effects_config32),
	AUDIO_EFFECTS_SET_BUF_LEN32 = _IOW(AUDIO_IOCTL_MAGIC, 100,
					   struct msm_hwacc_buf_cfg32),
	AUDIO_EFFECTS_GET_BUF_AVAIL32 = _IOW(AUDIO_IOCTL_MAGIC, 101,
					     struct msm_hwacc_buf_avail32),
	AUDIO_EFFECTS_WRITE32 = _IOW(AUDIO_IOCTL_MAGIC, 102, compat_uptr_t),
	AUDIO_EFFECTS_READ32 = _IOWR(AUDIO_IOCTL_MAGIC, 103, compat_uptr_t),
	AUDIO_EFFECTS_SET_PP_PARAMS32 = _IOW(AUDIO_IOCTL_MAGIC, 104,
					   compat_uptr_t),
	AUDIO_START32 = _IOW(AUDIO_IOCTL_MAGIC, 0, unsigned int),
};

static long audio_effects_compat_ioctl(struct file *file, unsigned int cmd,
					unsigned long arg)
{
	struct q6audio_effects *effects = file->private_data;
	int rc = 0, i;

	switch (cmd) {
	case AUDIO_SET_EFFECTS_CONFIG32: {
		struct msm_hwacc_effects_config32 config32;
		struct msm_hwacc_effects_config *config = &effects->config;

		mutex_lock(&effects->lock);
		memset(&effects->config, 0, sizeof(effects->config));
		if (copy_from_user(&config32, (void *)arg,
				   sizeof(config32))) {
			pr_err("%s: copy to user for AUDIO_SET_EFFECTS_CONFIG failed\n",
				__func__);
			rc = -EFAULT;
			mutex_unlock(&effects->lock);
			break;
		}
		config->input.buf_size = config32.input.buf_size;
		config->input.num_buf = config32.input.num_buf;
		config->input.num_channels = config32.input.num_channels;
		config->input.sample_rate = config32.input.sample_rate;
		config->input.bits_per_sample = config32.input.bits_per_sample;
		config->input.buf_size = config32.input.buf_size;
		for (i = 0; i < MAX_CHANNELS_SUPPORTED; i++)
			config->input.channel_map[i] =
						config32.input.channel_map[i];
		config->output.buf_size = config32.output.buf_size;
		config->output.num_buf = config32.output.num_buf;
		config->output.num_channels = config32.output.num_channels;
		config->output.sample_rate = config32.output.sample_rate;
		config->output.bits_per_sample =
					 config32.output.bits_per_sample;
		config->output.buf_size = config32.output.buf_size;
		for (i = 0; i < MAX_CHANNELS_SUPPORTED; i++)
			config->output.channel_map[i] =
						config32.output.channel_map[i];
		config->buf_cfg.input_len = config32.buf_cfg.input_len;
		config->buf_cfg.output_len = config32.buf_cfg.output_len;
		config->meta_mode_enabled = config32.meta_mode_enabled;
		config->overwrite_topology = config32.overwrite_topology;
		config->topology = config32.topology;
		pr_debug("%s: write buf_size: %d, num_buf: %d, sample_rate: %d, channels: %d\n",
			 __func__, effects->config.output.buf_size,
			 effects->config.output.num_buf,
			 effects->config.output.sample_rate,
			 effects->config.output.num_channels);
		pr_debug("%s: read buf_size: %d, num_buf: %d, sample_rate: %d, channels: %d\n",
			 __func__, effects->config.input.buf_size,
			 effects->config.input.num_buf,
			 effects->config.input.sample_rate,
			 effects->config.input.num_channels);
		mutex_unlock(&effects->lock);
		break;
	}
	case AUDIO_EFFECTS_SET_BUF_LEN32: {
		struct msm_hwacc_buf_cfg32 buf_cfg32;
		struct msm_hwacc_effects_config *config = &effects->config;

		mutex_lock(&effects->lock);
		if (copy_from_user(&buf_cfg32, (void *)arg,
				   sizeof(buf_cfg32))) {
			pr_err("%s: copy from user for AUDIO_EFFECTS_SET_BUF_LEN failed\n",
				__func__);
			rc = -EFAULT;
			mutex_unlock(&effects->lock);
			break;
		}
		config->buf_cfg.input_len = buf_cfg32.input_len;
		config->buf_cfg.output_len = buf_cfg32.output_len;
		pr_debug("%s: write buf len: %d, read buf len: %d\n",
			 __func__, effects->config.buf_cfg.output_len,
			 effects->config.buf_cfg.input_len);
		mutex_unlock(&effects->lock);
		break;
	}
	case AUDIO_EFFECTS_GET_BUF_AVAIL32: {
		struct msm_hwacc_buf_avail32 buf_avail;

		memset(&buf_avail, 0, sizeof(buf_avail));

		mutex_lock(&effects->lock);
		buf_avail.input_num_avail = atomic_read(&effects->in_count);
		buf_avail.output_num_avail = atomic_read(&effects->out_count);
		pr_debug("%s: write buf avail: %d, read buf avail: %d\n",
			 __func__, buf_avail.output_num_avail,
			 buf_avail.input_num_avail);
		if (copy_to_user((void *)arg, &buf_avail,
				   sizeof(buf_avail))) {
			pr_err("%s: copy to user for AUDIO_EFFECTS_GET_NUM_BUF_AVAIL failed\n",
				__func__);
			rc = -EFAULT;
		}
		mutex_unlock(&effects->lock);
		break;
	}
	case AUDIO_EFFECTS_SET_PP_PARAMS32: {
		long argvalues[MAX_PP_PARAMS_SZ] = {0};
		int argvalues32[MAX_PP_PARAMS_SZ] = {0};

		mutex_lock(&effects->lock);
		if (copy_from_user(argvalues32, (void *)arg,
				   MAX_PP_PARAMS_SZ*sizeof(int))) {
			pr_err("%s: copy from user failed for pp params\n",
				__func__);
			mutex_unlock(&effects->lock);
			return -EFAULT;
		}
		for (i = 0; i < MAX_PP_PARAMS_SZ; i++)
			argvalues[i] = argvalues32[i];

		rc = audio_effects_set_pp_param(effects, argvalues);
		mutex_unlock(&effects->lock);
		break;
	}
	case AUDIO_START32: {
		rc = audio_effects_shared_ioctl(file, AUDIO_START, arg);
		break;
	}
	case AUDIO_EFFECTS_WRITE32: {
		rc = audio_effects_shared_ioctl(file, AUDIO_EFFECTS_WRITE, arg);
		break;
	}
	case AUDIO_EFFECTS_READ32: {
		rc = audio_effects_shared_ioctl(file, AUDIO_EFFECTS_READ, arg);
		break;
	}
	default:
		pr_debug("%s: unhandled ioctl\n", __func__);
		rc = -EINVAL;
		break;
	}
	return rc;
}
#endif

static int audio_effects_release(struct inode *inode, struct file *file)
{
	struct q6audio_effects *effects = file->private_data;
	int rc = 0;

	if (!effects) {
		pr_err("%s: effect is NULL\n", __func__);
		return -EINVAL;
	}
	if (effects->opened) {
		rc = wait_event_timeout(effects->write_wait,
					atomic_read(&effects->out_count),
					WAIT_TIMEDOUT_DURATION_SECS * HZ);
		if (!rc)
			pr_err("%s: write wait_event_timeout failed\n",
				__func__);
		rc = wait_event_timeout(effects->read_wait,
					atomic_read(&effects->in_count),
					WAIT_TIMEDOUT_DURATION_SECS * HZ);
		if (!rc)
			pr_err("%s: read wait_event_timeout failed\n",
				__func__);
		rc = q6asm_cmd(effects->ac, CMD_CLOSE);
		if (rc < 0)
			pr_err("%s[%pK]:Failed to close the session rc=%d\n",
				__func__, effects, rc);
		effects->opened = 0;
		effects->started = 0;

		audio_effects_deinit_pp(effects->ac);
	}

	if (effects->buf_alloc) {
		q6asm_audio_client_buf_free_contiguous(IN, effects->ac);
		q6asm_audio_client_buf_free_contiguous(OUT, effects->ac);
	}
	q6asm_audio_client_free(effects->ac);

	mutex_destroy(&effects->lock);
	kfree(effects);

	pr_debug("%s: close session success\n", __func__);
	return rc;
}

static int audio_effects_open(struct inode *inode, struct file *file)
{
	struct q6audio_effects *effects;
	int rc = 0;

	effects = kzalloc(sizeof(struct q6audio_effects), GFP_KERNEL);
	if (!effects)
		return -ENOMEM;

	effects->ac = q6asm_audio_client_alloc(
					(app_cb)audio_effects_event_handler,
					(void *)effects);
	if (!effects->ac) {
		pr_err("%s: Could not allocate memory for audio client\n",
			__func__);
		kfree(effects);
		return -ENOMEM;
	}

	init_waitqueue_head(&effects->read_wait);
	init_waitqueue_head(&effects->write_wait);
	mutex_init(&effects->lock);

	effects->opened = 0;
	effects->started = 0;
	effects->buf_alloc = 0;
	file->private_data = effects;
	pr_debug("%s: open session success\n", __func__);
	return rc;
}

static const struct file_operations audio_effects_fops = {
	.owner = THIS_MODULE,
	.open = audio_effects_open,
	.release = audio_effects_release,
	.unlocked_ioctl = audio_effects_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl = audio_effects_compat_ioctl,
#endif
};

struct miscdevice audio_effects_misc = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = "msm_hweffects",
	.fops = &audio_effects_fops,
};

int __init audio_effects_init(void)
{
	return misc_register(&audio_effects_misc);
}

void __exit audio_effects_exit(void)
{
	misc_deregister(&audio_effects_misc);
}

MODULE_DESCRIPTION("Audio hardware accelerated effects driver");
MODULE_LICENSE("GPL v2");