aboutsummaryrefslogtreecommitdiff
path: root/techpack/audio/dsp/q6usm.c
blob: 5b822ae993586a232c1c1d88efa98181d3fd5173 (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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
/* Copyright (c) 2012-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/mutex.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <dsp/msm_audio_ion.h>
#include <dsp/apr_audio-v2.h>
#include <ipc/apr_us.h>
#include "q6usm.h"

#define ADSP_MEMORY_MAP_SHMEM8_4K_POOL 3

#define MEM_4K_OFFSET 4095
#define MEM_4K_MASK 0xfffff000

#define USM_SESSION_MAX 0x02 /* aDSP:USM limit */

#define READDONE_IDX_STATUS     0

#define WRITEDONE_IDX_STATUS    0

/* Standard timeout in the asynchronous ops */
#define Q6USM_TIMEOUT_JIFFIES	(1*HZ) /* 1 sec */

static DEFINE_MUTEX(session_lock);

static struct us_client *session[USM_SESSION_MAX];
static int32_t q6usm_mmapcallback(struct apr_client_data *data, void *priv);
static int32_t q6usm_callback(struct apr_client_data *data, void *priv);
static void q6usm_add_hdr(struct us_client *usc, struct apr_hdr *hdr,
			  uint32_t pkt_size, bool cmd_flg);

struct usm_mmap {
	atomic_t ref_cnt;
	atomic_t cmd_state;
	wait_queue_head_t cmd_wait;
	void *apr;
	int mem_handle;
};

static struct usm_mmap this_mmap;

static void q6usm_add_mmaphdr(struct apr_hdr *hdr,
			      uint32_t pkt_size, bool cmd_flg, u32 token)
{
	hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
				       APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
	hdr->src_port = 0;
	hdr->dest_port = 0;
	if (cmd_flg) {
		hdr->token = token;
		atomic_set(&this_mmap.cmd_state, 1);
	}
	hdr->pkt_size  = pkt_size;
}

static int q6usm_memory_map(phys_addr_t buf_add, int dir, uint32_t bufsz,
		uint32_t bufcnt, uint32_t session, uint32_t *mem_handle)
{
	struct usm_cmd_memory_map_region mem_region_map;
	int rc = 0;

	if (this_mmap.apr == NULL) {
		pr_err("%s: APR handle NULL\n", __func__);
		return -EINVAL;
	}

	q6usm_add_mmaphdr(&mem_region_map.hdr,
			  sizeof(struct usm_cmd_memory_map_region), true,
			  ((session << 8) | dir));

	mem_region_map.hdr.opcode = USM_CMD_SHARED_MEM_MAP_REGION;
	mem_region_map.mempool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;

	mem_region_map.num_regions = 1;
	mem_region_map.flags = 0;

	mem_region_map.shm_addr_lsw = lower_32_bits(buf_add);
	mem_region_map.shm_addr_msw =
			msm_audio_populate_upper_32_bits(buf_add);
	mem_region_map.mem_size_bytes = bufsz * bufcnt;

	rc = apr_send_pkt(this_mmap.apr, (uint32_t *) &mem_region_map);
	if (rc < 0) {
		pr_err("%s: mem_map op[0x%x]rc[%d]\n",
		       __func__, mem_region_map.hdr.opcode, rc);
		rc = -EINVAL;
		goto fail_cmd;
	}

	rc = wait_event_timeout(this_mmap.cmd_wait,
				(atomic_read(&this_mmap.cmd_state) == 0),
				Q6USM_TIMEOUT_JIFFIES);
	if (!rc) {
		rc = -ETIME;
		pr_err("%s: timeout. waited for memory_map\n", __func__);
	} else {
		*mem_handle = this_mmap.mem_handle;
		rc = 0;
	}
fail_cmd:
	return rc;
}

int q6usm_memory_unmap(phys_addr_t buf_add, int dir, uint32_t session,
			uint32_t mem_handle)
{
	struct usm_cmd_memory_unmap_region mem_unmap;
	int rc = 0;

	if (this_mmap.apr == NULL) {
		pr_err("%s: APR handle NULL\n", __func__);
		return -EINVAL;
	}

	q6usm_add_mmaphdr(&mem_unmap.hdr,
			  sizeof(struct usm_cmd_memory_unmap_region), true,
			  ((session << 8) | dir));
	mem_unmap.hdr.opcode = USM_CMD_SHARED_MEM_UNMAP_REGION;
	mem_unmap.mem_map_handle = mem_handle;

	rc = apr_send_pkt(this_mmap.apr, (uint32_t *) &mem_unmap);
	if (rc < 0) {
		pr_err("%s: mem_unmap op[0x%x] rc[%d]\n",
		       __func__, mem_unmap.hdr.opcode, rc);
		goto fail_cmd;
	}

	rc = wait_event_timeout(this_mmap.cmd_wait,
				(atomic_read(&this_mmap.cmd_state) == 0),
				Q6USM_TIMEOUT_JIFFIES);
	if (!rc) {
		rc = -ETIME;
		pr_err("%s: timeout. waited for memory_unmap\n", __func__);
	} else
		rc = 0;
fail_cmd:
	return rc;
}

static int q6usm_session_alloc(struct us_client *usc)
{
	int ind = 0;

	mutex_lock(&session_lock);
	for (ind = 0; ind < USM_SESSION_MAX; ++ind) {
		if (!session[ind]) {
			session[ind] = usc;
			mutex_unlock(&session_lock);
			++ind; /* session id: 0 reserved */
			pr_debug("%s: session[%d] was allocated\n",
				  __func__, ind);
			return ind;
		}
	}
	mutex_unlock(&session_lock);
	return -ENOMEM;
}

static void q6usm_session_free(struct us_client *usc)
{
	/* Session index was incremented during allocation */
	uint16_t ind = (uint16_t)usc->session - 1;

	pr_debug("%s: to free session[%d]\n", __func__, ind);
	if (ind < USM_SESSION_MAX) {
		mutex_lock(&session_lock);
		session[ind] = NULL;
		mutex_unlock(&session_lock);
	}
}

static int q6usm_us_client_buf_free(unsigned int dir,
			     struct us_client *usc)
{
	struct us_port_data *port;
	int rc = 0;

	if ((usc == NULL) ||
	    ((dir != IN) && (dir != OUT)))
		return -EINVAL;

	mutex_lock(&usc->cmd_lock);
	port = &usc->port[dir];
	if (port == NULL) {
		mutex_unlock(&usc->cmd_lock);
		return -EINVAL;
	}

	if (port->data == NULL) {
		mutex_unlock(&usc->cmd_lock);
		return 0;
	}

	rc = q6usm_memory_unmap(port->phys, dir, usc->session,
				*((uint32_t *)port->ext));
	pr_debug("%s: data[%pK]phys[%llx][%pK]\n", __func__,
		 (void *)port->data, (u64)port->phys, (void *)&port->phys);

	msm_audio_ion_free(port->client, port->handle);

	port->data = NULL;
	port->phys = 0;
	port->buf_size = 0;
	port->buf_cnt = 0;
	port->client = NULL;
	port->handle = NULL;

	mutex_unlock(&usc->cmd_lock);
	return rc;
}

int q6usm_us_param_buf_free(unsigned int dir,
			struct us_client *usc)
{
	struct us_port_data *port;
	int rc = 0;

	if ((usc == NULL) ||
		((dir != IN) && (dir != OUT)))
		return -EINVAL;

	mutex_lock(&usc->cmd_lock);
	port = &usc->port[dir];
	if (port == NULL) {
		mutex_unlock(&usc->cmd_lock);
		return -EINVAL;
	}

	if (port->param_buf == NULL) {
		mutex_unlock(&usc->cmd_lock);
		return 0;
	}

	rc = q6usm_memory_unmap(port->param_phys, dir, usc->session,
				*((uint32_t *)port->param_buf_mem_handle));
	pr_debug("%s: data[%pK]phys[%llx][%pK]\n", __func__,
		 (void *)port->param_buf, (u64)port->param_phys,
		 (void *)&port->param_phys);

	msm_audio_ion_free(port->param_client, port->param_handle);

	port->param_buf = NULL;
	port->param_phys = 0;
	port->param_buf_size = 0;
	port->param_client = NULL;
	port->param_handle = NULL;

	mutex_unlock(&usc->cmd_lock);
	return rc;
}

void q6usm_us_client_free(struct us_client *usc)
{
	int loopcnt = 0;
	struct us_port_data *port;
	uint32_t *p_mem_handle = NULL;

	if ((usc == NULL) ||
	    !(usc->session))
		return;

	for (loopcnt = 0; loopcnt <= OUT; ++loopcnt) {
		port = &usc->port[loopcnt];
		if (port->data == NULL)
			continue;
		pr_debug("%s: loopcnt = %d\n", __func__, loopcnt);
		q6usm_us_client_buf_free(loopcnt, usc);
		q6usm_us_param_buf_free(loopcnt, usc);
	}
	q6usm_session_free(usc);
	apr_deregister(usc->apr);

	pr_debug("%s: APR De-Register\n", __func__);

	if (atomic_read(&this_mmap.ref_cnt) <= 0) {
		pr_err("%s: APR Common Port Already Closed\n", __func__);
		goto done;
	}

	atomic_dec(&this_mmap.ref_cnt);
	if (atomic_read(&this_mmap.ref_cnt) == 0) {
		apr_deregister(this_mmap.apr);
		pr_debug("%s: APR De-Register common port\n", __func__);
	}

done:
	p_mem_handle = (uint32_t *)usc->port[IN].ext;
	kfree(p_mem_handle);
	kfree(usc);
	pr_debug("%s:\n", __func__);
}

struct us_client *q6usm_us_client_alloc(
	void (*cb)(uint32_t, uint32_t, uint32_t *, void *),
	void *priv)
{
	struct us_client *usc;
	uint32_t *p_mem_handle = NULL;
	int n;
	int lcnt = 0;

	usc = kzalloc(sizeof(struct us_client), GFP_KERNEL);
	if (usc == NULL)
		return NULL;

	p_mem_handle = kzalloc(sizeof(uint32_t) * 4, GFP_KERNEL);
	if (p_mem_handle == NULL) {
		kfree(usc);
		return NULL;
	}

	n = q6usm_session_alloc(usc);
	if (n <= 0)
		goto fail_session;
	usc->session = n;
	usc->cb = cb;
	usc->priv = priv;
	usc->apr = apr_register("ADSP", "USM",
				(apr_fn)q6usm_callback,
				((usc->session) << 8 | 0x0001),
				usc);

	if (usc->apr == NULL) {
		pr_err("%s: Registration with APR failed\n", __func__);
		goto fail;
	}
	pr_debug("%s: Registering the common port with APR\n", __func__);
	if (atomic_read(&this_mmap.ref_cnt) == 0) {
		this_mmap.apr = apr_register("ADSP", "USM",
					     (apr_fn)q6usm_mmapcallback,
					     0x0FFFFFFFF, &this_mmap);
		if (this_mmap.apr == NULL) {
			pr_err("%s: USM port registration failed\n",
			       __func__);
			goto fail;
		}
	}

	atomic_inc(&this_mmap.ref_cnt);
	init_waitqueue_head(&usc->cmd_wait);
	mutex_init(&usc->cmd_lock);
	for (lcnt = 0; lcnt <= OUT; ++lcnt) {
		mutex_init(&usc->port[lcnt].lock);
		spin_lock_init(&usc->port[lcnt].dsp_lock);
		usc->port[lcnt].ext = (void *)p_mem_handle++;
		usc->port[lcnt].param_buf_mem_handle = (void *)p_mem_handle++;
		pr_err("%s: usc->port[%d].ext=%pK;\n",
		       __func__, lcnt, usc->port[lcnt].ext);
	}
	atomic_set(&usc->cmd_state, 0);

	return usc;
fail:
	kfree(p_mem_handle);
	q6usm_us_client_free(usc);
	return NULL;
fail_session:
	kfree(p_mem_handle);
	kfree(usc);
	return NULL;
}

int q6usm_us_client_buf_alloc(unsigned int dir,
			      struct us_client *usc,
			      unsigned int bufsz,
			      unsigned int bufcnt)
{
	int rc = 0;
	struct us_port_data *port = NULL;
	unsigned int size = bufsz*bufcnt;
	size_t len;

	if ((usc == NULL) ||
	    ((dir != IN) && (dir != OUT)) || (size == 0) ||
	    (usc->session <= 0 || usc->session > USM_SESSION_MAX)) {
		pr_err("%s: wrong parameters: size=%d; bufcnt=%d\n",
		       __func__, size, bufcnt);
		return -EINVAL;
	}

	mutex_lock(&usc->cmd_lock);

	port = &usc->port[dir];

	/* The size to allocate should be multiple of 4K bytes */
	size = PAGE_ALIGN(size);

	rc = msm_audio_ion_alloc("ultrasound_client",
		&port->client, &port->handle,
		size, &port->phys,
		&len, &port->data);

	if (rc) {
		pr_err("%s: US ION allocation failed, rc = %d\n",
			__func__, rc);
		mutex_unlock(&usc->cmd_lock);
		return -ENOMEM;
	}

	port->buf_cnt = bufcnt;
	port->buf_size = bufsz;
	pr_debug("%s: data[%pK]; phys[%llx]; [%pK]\n", __func__,
		 (void *)port->data,
		 (u64)port->phys,
		 (void *)&port->phys);

	rc = q6usm_memory_map(port->phys, dir, size, 1, usc->session,
				(uint32_t *)port->ext);
	if (rc < 0) {
		pr_err("%s: CMD Memory_map failed\n", __func__);
		mutex_unlock(&usc->cmd_lock);
		q6usm_us_client_buf_free(dir, usc);
		q6usm_us_param_buf_free(dir, usc);
	} else {
		mutex_unlock(&usc->cmd_lock);
		rc = 0;
	}

	return rc;
}

int q6usm_us_param_buf_alloc(unsigned int dir,
			struct us_client *usc,
			unsigned int bufsz)
{
	int rc = 0;
	struct us_port_data *port = NULL;
	unsigned int size = bufsz;
	size_t len;

	if ((usc == NULL) ||
		((dir != IN) && (dir != OUT)) ||
		(usc->session <= 0 || usc->session > USM_SESSION_MAX)) {
		pr_err("%s: wrong parameters: direction=%d, bufsz=%d\n",
			__func__, dir, bufsz);
		return -EINVAL;
	}

	mutex_lock(&usc->cmd_lock);

	port = &usc->port[dir];

	if (bufsz == 0) {
		pr_debug("%s: bufsz=0, get/set param commands are forbidden\n",
			__func__);
		port->param_buf = NULL;
		mutex_unlock(&usc->cmd_lock);
		return rc;
	}

	/* The size to allocate should be multiple of 4K bytes */
	size = PAGE_ALIGN(size);

	rc = msm_audio_ion_alloc("ultrasound_client",
		&port->param_client, &port->param_handle,
		size, &port->param_phys,
		&len, &port->param_buf);

	if (rc) {
		pr_err("%s: US ION allocation failed, rc = %d\n",
			__func__, rc);
		mutex_unlock(&usc->cmd_lock);
		return -ENOMEM;
	}

	port->param_buf_size = bufsz;
	pr_debug("%s: param_buf[%pK]; param_phys[%llx]; [%pK]\n", __func__,
		 (void *)port->param_buf,
		 (u64)port->param_phys,
		 (void *)&port->param_phys);

	rc = q6usm_memory_map(port->param_phys, (IN | OUT), size, 1,
			usc->session, (uint32_t *)port->param_buf_mem_handle);
	if (rc < 0) {
		pr_err("%s: CMD Memory_map failed\n", __func__);
		mutex_unlock(&usc->cmd_lock);
		q6usm_us_client_buf_free(dir, usc);
		q6usm_us_param_buf_free(dir, usc);
	} else {
		mutex_unlock(&usc->cmd_lock);
		rc = 0;
	}

	return rc;
}

static int32_t q6usm_mmapcallback(struct apr_client_data *data, void *priv)
{
	uint32_t token;
	uint32_t *payload = data->payload;

	pr_debug("%s: ptr0[0x%x]; ptr1[0x%x]; opcode[0x%x]\n",
		 __func__, payload[0], payload[1], data->opcode);
	pr_debug("%s: token[0x%x]; payload_size[%d]; src[%d]; dest[%d];\n",
		 __func__, data->token, data->payload_size,
		 data->src_port, data->dest_port);

	if (data->opcode == APR_BASIC_RSP_RESULT) {
		/* status field check */
		if (payload[1]) {
			pr_err("%s: wrong response[%d] on cmd [%d]\n",
			       __func__, payload[1], payload[0]);
		} else {
			token = data->token;
			switch (payload[0]) {
			case USM_CMD_SHARED_MEM_UNMAP_REGION:
				if (atomic_read(&this_mmap.cmd_state)) {
					atomic_set(&this_mmap.cmd_state, 0);
					wake_up(&this_mmap.cmd_wait);
				}
				/* fallthrough */
			case USM_CMD_SHARED_MEM_MAP_REGION:
				/* For MEM_MAP, additional answer is waited, */
				/* therfore, no wake-up here */
				pr_debug("%s: cmd[0x%x]; result[0x%x]\n",
					 __func__, payload[0], payload[1]);
				break;
			default:
				pr_debug("%s: wrong command[0x%x]\n",
					 __func__, payload[0]);
				break;
			}
		}
	} else {
		if (data->opcode == USM_CMDRSP_SHARED_MEM_MAP_REGION) {
			this_mmap.mem_handle = payload[0];
			pr_debug("%s: memory map handle = 0x%x",
				__func__, payload[0]);
			if (atomic_read(&this_mmap.cmd_state)) {
				atomic_set(&this_mmap.cmd_state, 0);
				wake_up(&this_mmap.cmd_wait);
			}
		}
	}
	return 0;
}


static int32_t q6usm_callback(struct apr_client_data *data, void *priv)
{
	struct us_client *usc = (struct us_client *)priv;
	unsigned long dsp_flags;
	uint32_t *payload = data->payload;
	uint32_t token = data->token;
	uint32_t opcode = Q6USM_EVENT_UNDEF;

	if (usc == NULL) {
		pr_err("%s: client info is NULL\n", __func__);
		return -EINVAL;
	}

	if (data->opcode == APR_BASIC_RSP_RESULT) {
		/* status field check */
		if (payload[1]) {
			pr_err("%s: wrong response[%d] on cmd [%d]\n",
			       __func__, payload[1], payload[0]);
			if (usc->cb)
				usc->cb(data->opcode, token,
					(uint32_t *)data->payload, usc->priv);
		} else {
			switch (payload[0]) {
			case USM_SESSION_CMD_RUN:
			case USM_STREAM_CMD_CLOSE:
				if (token != usc->session) {
					pr_err("%s: wrong token[%d]",
					       __func__, token);
					break;
				}
			case USM_STREAM_CMD_OPEN_READ:
			case USM_STREAM_CMD_OPEN_WRITE:
			case USM_STREAM_CMD_SET_ENC_PARAM:
			case USM_DATA_CMD_MEDIA_FORMAT_UPDATE:
			case USM_SESSION_CMD_SIGNAL_DETECT_MODE:
			case USM_STREAM_CMD_SET_PARAM:
			case USM_STREAM_CMD_GET_PARAM:
				if (atomic_read(&usc->cmd_state)) {
					atomic_set(&usc->cmd_state, 0);
					wake_up(&usc->cmd_wait);
				}
				if (usc->cb)
					usc->cb(data->opcode, token,
						(uint32_t *)data->payload,
						usc->priv);
				break;
			default:
				break;
			}
		}
		return 0;
	}

	switch (data->opcode) {
	case RESET_EVENTS: {
		pr_err("%s: Reset event is received: %d %d\n",
				__func__,
				data->reset_event,
				data->reset_proc);

		opcode = RESET_EVENTS;

		apr_reset(this_mmap.apr);
		this_mmap.apr = NULL;

		apr_reset(usc->apr);
		usc->apr = NULL;

		break;
	}


	case USM_DATA_EVENT_READ_DONE: {
		struct us_port_data *port = &usc->port[OUT];

		opcode = Q6USM_EVENT_READ_DONE;
		spin_lock_irqsave(&port->dsp_lock, dsp_flags);
		if (payload[READDONE_IDX_STATUS]) {
			pr_err("%s: wrong READDONE[%d]; token[%d]\n",
			       __func__,
			       payload[READDONE_IDX_STATUS],
			       token);
			token = USM_WRONG_TOKEN;
			spin_unlock_irqrestore(&port->dsp_lock,
					       dsp_flags);
			break;
		}

		if (port->expected_token != token) {
			u32 cpu_buf = port->cpu_buf;

			pr_err("%s: expected[%d] != token[%d]\n",
				__func__, port->expected_token, token);
			pr_debug("%s: dsp_buf=%d; cpu_buf=%d;\n",
				__func__,   port->dsp_buf, cpu_buf);

			token = USM_WRONG_TOKEN;
			/* To prevent data handle continiue */
			port->expected_token = USM_WRONG_TOKEN;
			spin_unlock_irqrestore(&port->dsp_lock,
					       dsp_flags);
			break;
		} /* port->expected_token != data->token */

		port->expected_token = token + 1;
		if (port->expected_token == port->buf_cnt)
			port->expected_token = 0;

		/* gap support */
		if (port->expected_token != port->cpu_buf) {
			port->dsp_buf = port->expected_token;
			token = port->dsp_buf; /* for callback */
		} else
			port->dsp_buf = token;

		spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
		break;
	} /* case USM_DATA_EVENT_READ_DONE */

	case USM_DATA_EVENT_WRITE_DONE: {
		struct us_port_data *port = &usc->port[IN];

		opcode = Q6USM_EVENT_WRITE_DONE;
		if (payload[WRITEDONE_IDX_STATUS]) {
			pr_err("%s: wrong WRITEDONE_IDX_STATUS[%d]\n",
			       __func__,
			       payload[WRITEDONE_IDX_STATUS]);
			break;
		}

		spin_lock_irqsave(&port->dsp_lock, dsp_flags);
		port->dsp_buf = token + 1;
		if (port->dsp_buf == port->buf_cnt)
			port->dsp_buf = 0;
		spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);

		break;
	} /* case USM_DATA_EVENT_WRITE_DONE */

	case USM_SESSION_EVENT_SIGNAL_DETECT_RESULT: {
		pr_debug("%s: US detect result: result=%d",
			 __func__,
			 payload[0]);
		opcode = Q6USM_EVENT_SIGNAL_DETECT_RESULT;

		break;
	} /* case USM_SESSION_EVENT_SIGNAL_DETECT_RESULT */

	default:
		return 0;

	} /* switch */

	if (usc->cb)
		usc->cb(opcode, token,
			data->payload, usc->priv);

	return 0;
}

uint32_t q6usm_get_virtual_address(int dir,
				   struct us_client *usc,
				   struct vm_area_struct *vms)
{
	uint32_t ret = 0xffffffff;

	if (vms && (usc != NULL) && ((dir == IN) || (dir == OUT))) {
		struct us_port_data *port = &usc->port[dir];
		int size = PAGE_ALIGN(port->buf_size * port->buf_cnt);
		struct audio_buffer ab;

		ab.phys = port->phys;
		ab.data = port->data;
		ab.used = 1;
		ab.size = size;
		ab.actual_size = size;
		ab.handle = port->handle;
		ab.client = port->client;

		ret = msm_audio_ion_mmap(&ab, vms);

	}
	return ret;
}

static void q6usm_add_hdr(struct us_client *usc, struct apr_hdr *hdr,
			  uint32_t pkt_size, bool cmd_flg)
{
	mutex_lock(&usc->cmd_lock);
	hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
				       APR_HDR_LEN(sizeof(struct apr_hdr)),
				       APR_PKT_VER);
	hdr->src_svc = ((struct apr_svc *)usc->apr)->id;
	hdr->src_domain = APR_DOMAIN_APPS;
	hdr->dest_svc = APR_SVC_USM;
	hdr->dest_domain = APR_DOMAIN_ADSP;
	hdr->src_port = (usc->session << 8) | 0x0001;
	hdr->dest_port = (usc->session << 8) | 0x0001;
	if (cmd_flg) {
		hdr->token = usc->session;
		atomic_set(&usc->cmd_state, 1);
	}
	hdr->pkt_size  = pkt_size;
	mutex_unlock(&usc->cmd_lock);
}

static uint32_t q6usm_ext2int_format(uint32_t ext_format)
{
	uint32_t int_format = INVALID_FORMAT;

	switch (ext_format) {
	case FORMAT_USPS_EPOS:
		int_format = US_POINT_EPOS_FORMAT_V2;
		break;
	case FORMAT_USRAW:
		int_format = US_RAW_FORMAT_V2;
		break;
	case FORMAT_USPROX:
		int_format = US_PROX_FORMAT_V4;
		break;
	case FORMAT_USGES_SYNC:
		int_format = US_GES_SYNC_FORMAT;
		break;
	case FORMAT_USRAW_SYNC:
		int_format = US_RAW_SYNC_FORMAT;
		break;
	default:
		pr_err("%s: Invalid format[%d]\n", __func__, ext_format);
		break;
	}

	return int_format;
}

int q6usm_open_read(struct us_client *usc,
		    uint32_t format)
{
	uint32_t int_format = INVALID_FORMAT;
	int rc = 0x00;
	struct usm_stream_cmd_open_read open;

	if ((usc == NULL) || (usc->apr == NULL)) {
		pr_err("%s: client or its apr is NULL\n", __func__);
		return -EINVAL;
	}

	pr_debug("%s: session[%d]", __func__, usc->session);

	q6usm_add_hdr(usc, &open.hdr, sizeof(open), true);
	open.hdr.opcode = USM_STREAM_CMD_OPEN_READ;
	open.src_endpoint = 0; /* AFE */
	open.pre_proc_top = 0; /* No preprocessing required */

	int_format = q6usm_ext2int_format(format);
	if (int_format == INVALID_FORMAT)
		return -EINVAL;

	open.uMode = STREAM_PRIORITY_NORMAL;
	open.format = int_format;

	rc = apr_send_pkt(usc->apr, (uint32_t *) &open);
	if (rc < 0) {
		pr_err("%s: open failed op[0x%x]rc[%d]\n",
		       __func__, open.hdr.opcode, rc);
		goto fail_cmd;
	}
	rc = wait_event_timeout(usc->cmd_wait,
				(atomic_read(&usc->cmd_state) == 0),
				Q6USM_TIMEOUT_JIFFIES);
	if (!rc) {
		rc = -ETIME;
		pr_err("%s: timeout, waited for OPEN_READ rc[%d]\n",
		       __func__, rc);
		goto fail_cmd;
	} else
		rc = 0;
fail_cmd:
	return rc;
}


int q6usm_enc_cfg_blk(struct us_client *usc, struct us_encdec_cfg *us_cfg)
{
	uint32_t int_format = INVALID_FORMAT;
	struct usm_stream_cmd_encdec_cfg_blk  enc_cfg_obj;
	struct usm_stream_cmd_encdec_cfg_blk  *enc_cfg = &enc_cfg_obj;
	int rc = 0;
	uint32_t total_cfg_size =
		sizeof(struct usm_stream_cmd_encdec_cfg_blk);
	uint32_t round_params_size = 0;
	uint8_t  is_allocated = 0;


	if ((usc == NULL) || (us_cfg == NULL)) {
		pr_err("%s: wrong input", __func__);
		return -EINVAL;
	}

	int_format = q6usm_ext2int_format(us_cfg->format_id);
	if (int_format == INVALID_FORMAT) {
		pr_err("%s: wrong input format[%d]",
		       __func__, us_cfg->format_id);
		return -EINVAL;
	}

	/* Transparent configuration data is after enc_cfg */
	/* Integer number of u32s is required */
	round_params_size = ((us_cfg->params_size + 3)/4) * 4;
	if (round_params_size > USM_MAX_CFG_DATA_SIZE) {
		/* Dynamic allocated encdec_cfg_blk is required */
		/* static part use */
		round_params_size -= USM_MAX_CFG_DATA_SIZE;
		total_cfg_size += round_params_size;
		enc_cfg = kzalloc(total_cfg_size, GFP_KERNEL);
		if (enc_cfg == NULL) {
			pr_err("%s: enc_cfg[%d] allocation failed\n",
			       __func__, total_cfg_size);
			return -ENOMEM;
		}
		is_allocated = 1;
	} else
		round_params_size = 0;

	q6usm_add_hdr(usc, &enc_cfg->hdr, total_cfg_size, true);

	enc_cfg->hdr.opcode = USM_STREAM_CMD_SET_ENC_PARAM;
	enc_cfg->param_id = USM_PARAM_ID_ENCDEC_ENC_CFG_BLK;
	enc_cfg->param_size = sizeof(struct usm_encode_cfg_blk)+
				round_params_size;
	enc_cfg->enc_blk.frames_per_buf = 1;
	enc_cfg->enc_blk.format_id = int_format;
	enc_cfg->enc_blk.cfg_size = sizeof(struct usm_cfg_common)+
				    USM_MAX_CFG_DATA_SIZE +
				    round_params_size;
	memcpy(&(enc_cfg->enc_blk.cfg_common), &(us_cfg->cfg_common),
	       sizeof(struct usm_cfg_common));

	/* Transparent data copy */
	memcpy(enc_cfg->enc_blk.transp_data, us_cfg->params,
	       us_cfg->params_size);
	pr_debug("%s: cfg_size[%d], params_size[%d]\n",
		__func__,
		enc_cfg->enc_blk.cfg_size,
		us_cfg->params_size);
	pr_debug("%s: params[%d,%d,%d,%d, %d,%d,%d,%d]\n",
		__func__,
		enc_cfg->enc_blk.transp_data[0],
		enc_cfg->enc_blk.transp_data[1],
		enc_cfg->enc_blk.transp_data[2],
		enc_cfg->enc_blk.transp_data[3],
		enc_cfg->enc_blk.transp_data[4],
		enc_cfg->enc_blk.transp_data[5],
		enc_cfg->enc_blk.transp_data[6],
		enc_cfg->enc_blk.transp_data[7]
	       );
	pr_debug("%s: srate:%d, ch=%d, bps= %d;\n",
		__func__, enc_cfg->enc_blk.cfg_common.sample_rate,
		enc_cfg->enc_blk.cfg_common.ch_cfg,
		enc_cfg->enc_blk.cfg_common.bits_per_sample);
	pr_debug("dmap:[0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x]; dev_id=0x%x\n",
		enc_cfg->enc_blk.cfg_common.data_map[0],
		enc_cfg->enc_blk.cfg_common.data_map[1],
		enc_cfg->enc_blk.cfg_common.data_map[2],
		enc_cfg->enc_blk.cfg_common.data_map[3],
		enc_cfg->enc_blk.cfg_common.data_map[4],
		enc_cfg->enc_blk.cfg_common.data_map[5],
		enc_cfg->enc_blk.cfg_common.data_map[6],
		enc_cfg->enc_blk.cfg_common.data_map[7],
		enc_cfg->enc_blk.cfg_common.dev_id);

	rc = apr_send_pkt(usc->apr, (uint32_t *) enc_cfg);
	if (rc < 0) {
		pr_err("%s:Comamnd open failed\n", __func__);
		rc = -EINVAL;
		goto fail_cmd;
	}
	rc = wait_event_timeout(usc->cmd_wait,
				(atomic_read(&usc->cmd_state) == 0),
				Q6USM_TIMEOUT_JIFFIES);
	if (!rc) {
		rc = -ETIME;
		pr_err("%s: timeout opcode[0x%x]\n",
		       __func__, enc_cfg->hdr.opcode);
	} else
		rc = 0;

fail_cmd:
	if (is_allocated == 1)
		kfree(enc_cfg);

	return rc;
}

int q6usm_dec_cfg_blk(struct us_client *usc, struct us_encdec_cfg *us_cfg)
{

	uint32_t int_format = INVALID_FORMAT;
	struct usm_stream_media_format_update dec_cfg_obj;
	struct usm_stream_media_format_update *dec_cfg = &dec_cfg_obj;

	int rc = 0;
	uint32_t total_cfg_size = sizeof(struct usm_stream_media_format_update);
	uint32_t round_params_size = 0;
	uint8_t  is_allocated = 0;


	if ((usc == NULL) || (us_cfg == NULL)) {
		pr_err("%s: wrong input", __func__);
		return -EINVAL;
	}

	int_format = q6usm_ext2int_format(us_cfg->format_id);
	if (int_format == INVALID_FORMAT) {
		pr_err("%s: wrong input format[%d]",
		       __func__, us_cfg->format_id);
		return -EINVAL;
	}

	/* Transparent configuration data is after enc_cfg */
	/* Integer number of u32s is required */
	round_params_size = ((us_cfg->params_size + 3)/4) * 4;
	if (round_params_size > USM_MAX_CFG_DATA_SIZE) {
		/* Dynamic allocated encdec_cfg_blk is required */
		/* static part use */
		round_params_size -= USM_MAX_CFG_DATA_SIZE;
		total_cfg_size += round_params_size;
		dec_cfg = kzalloc(total_cfg_size, GFP_KERNEL);
		if (dec_cfg == NULL) {
			pr_err("%s:dec_cfg[%d] allocation failed\n",
			       __func__, total_cfg_size);
			return -ENOMEM;
		}
		is_allocated = 1;
	} else { /* static transp_data is enough */
		round_params_size = 0;
	}

	q6usm_add_hdr(usc, &dec_cfg->hdr, total_cfg_size, true);

	dec_cfg->hdr.opcode = USM_DATA_CMD_MEDIA_FORMAT_UPDATE;
	dec_cfg->format_id = int_format;
	dec_cfg->cfg_size = sizeof(struct usm_cfg_common) +
			    USM_MAX_CFG_DATA_SIZE +
			    round_params_size;
	memcpy(&(dec_cfg->cfg_common), &(us_cfg->cfg_common),
	       sizeof(struct usm_cfg_common));
	/* Transparent data copy */
	memcpy(dec_cfg->transp_data, us_cfg->params, us_cfg->params_size);
	pr_debug("%s: cfg_size[%d], params_size[%d]; parambytes[%d,%d,%d,%d]\n",
		__func__,
		dec_cfg->cfg_size,
		us_cfg->params_size,
		dec_cfg->transp_data[0],
		dec_cfg->transp_data[1],
		dec_cfg->transp_data[2],
		dec_cfg->transp_data[3]
	       );

	rc = apr_send_pkt(usc->apr, (uint32_t *) dec_cfg);
	if (rc < 0) {
		pr_err("%s:Comamnd open failed\n", __func__);
		rc = -EINVAL;
		goto fail_cmd;
	}
	rc = wait_event_timeout(usc->cmd_wait,
				(atomic_read(&usc->cmd_state) == 0),
				Q6USM_TIMEOUT_JIFFIES);
	if (!rc) {
		rc = -ETIME;
		pr_err("%s: timeout opcode[0x%x]\n",
		       __func__, dec_cfg->hdr.opcode);
	} else
		rc = 0;

fail_cmd:
	if (is_allocated == 1)
		kfree(dec_cfg);

	return rc;
}

int q6usm_open_write(struct us_client *usc,
		     uint32_t format)
{
	int rc = 0;
	uint32_t int_format = INVALID_FORMAT;
	struct usm_stream_cmd_open_write open;

	if ((usc == NULL) || (usc->apr == NULL)) {
		pr_err("%s: APR handle NULL\n", __func__);
		return -EINVAL;
	}

	pr_debug("%s: session[%d]", __func__, usc->session);

	q6usm_add_hdr(usc, &open.hdr, sizeof(open), true);
	open.hdr.opcode = USM_STREAM_CMD_OPEN_WRITE;

	int_format = q6usm_ext2int_format(format);
	if (int_format == INVALID_FORMAT) {
		pr_err("%s: wrong format[%d]", __func__, format);
		return -EINVAL;
	}

	open.format = int_format;

	rc = apr_send_pkt(usc->apr, (uint32_t *) &open);
	if (rc < 0) {
		pr_err("%s:open failed op[0x%x]rc[%d]\n",
		       __func__, open.hdr.opcode, rc);
		goto fail_cmd;
	}
	rc = wait_event_timeout(usc->cmd_wait,
				(atomic_read(&usc->cmd_state) == 0),
				Q6USM_TIMEOUT_JIFFIES);
	if (!rc) {
		rc = -ETIME;
		pr_err("%s:timeout. waited for OPEN_WRITR rc[%d]\n",
		       __func__, rc);
		goto fail_cmd;
	} else
		rc = 0;

fail_cmd:
	return rc;
}

int q6usm_run(struct us_client *usc, uint32_t flags,
	      uint32_t msw_ts, uint32_t lsw_ts)
{
	struct usm_stream_cmd_run run;
	int rc = 0;

	if ((usc == NULL) || (usc->apr == NULL)) {
		pr_err("%s: APR handle NULL\n", __func__);
		return -EINVAL;
	}
	q6usm_add_hdr(usc, &run.hdr, sizeof(run), true);

	run.hdr.opcode = USM_SESSION_CMD_RUN;
	run.flags    = flags;
	run.msw_ts   = msw_ts;
	run.lsw_ts   = lsw_ts;

	rc = apr_send_pkt(usc->apr, (uint32_t *) &run);
	if (rc < 0) {
		pr_err("%s: Commmand run failed[%d]\n", __func__, rc);
		goto fail_cmd;
	}

	rc = wait_event_timeout(usc->cmd_wait,
				(atomic_read(&usc->cmd_state) == 0),
				Q6USM_TIMEOUT_JIFFIES);
	if (!rc) {
		rc = -ETIME;
		pr_err("%s: timeout. waited for run success rc[%d]\n",
		       __func__, rc);
	} else
		rc = 0;

fail_cmd:
	return rc;
}



int q6usm_read(struct us_client *usc, uint32_t read_ind)
{
	struct usm_stream_cmd_read read;
	struct us_port_data *port = NULL;
	int rc = 0;
	u32 read_counter = 0;
	u32 loop_ind = 0;
	u64 buf_addr = 0;

	if ((usc == NULL) || (usc->apr == NULL)) {
		pr_err("%s: APR handle NULL\n", __func__);
		return -EINVAL;
	}
	port = &usc->port[OUT];

	if (read_ind > port->buf_cnt) {
		pr_err("%s: wrong read_ind[%d]\n",
		       __func__, read_ind);
		return -EINVAL;
	}
	if (read_ind == port->cpu_buf) {
		pr_err("%s: no free region\n", __func__);
		return 0;
	}

	if (read_ind > port->cpu_buf) { /* 1 range */
		read_counter = read_ind - port->cpu_buf;
	} else { /* 2 ranges */
		read_counter = (port->buf_cnt - port->cpu_buf) + read_ind;
	}

	q6usm_add_hdr(usc, &read.hdr, sizeof(read), false);

	read.hdr.opcode = USM_DATA_CMD_READ;
	read.buf_size = port->buf_size;
	buf_addr = (u64)(port->phys) + port->buf_size * (port->cpu_buf);
	read.buf_addr_lsw = lower_32_bits(buf_addr);
	read.buf_addr_msw = msm_audio_populate_upper_32_bits(buf_addr);
	read.mem_map_handle = *((uint32_t *)(port->ext));

	for (loop_ind = 0; loop_ind < read_counter; ++loop_ind) {
		u32 temp_cpu_buf = port->cpu_buf;

		buf_addr = (u64)(port->phys) +
				port->buf_size * (port->cpu_buf);
		read.buf_addr_lsw = lower_32_bits(buf_addr);
		read.buf_addr_msw = msm_audio_populate_upper_32_bits(buf_addr);
		read.seq_id = port->cpu_buf;
		read.hdr.token = port->cpu_buf;
		read.counter = 1;

		++(port->cpu_buf);
		if (port->cpu_buf == port->buf_cnt)
			port->cpu_buf = 0;

		rc = apr_send_pkt(usc->apr, (uint32_t *) &read);

		if (rc < 0) {
			port->cpu_buf = temp_cpu_buf;

			pr_err("%s:read op[0x%x]rc[%d]\n",
			       __func__, read.hdr.opcode, rc);
			break;
		}

		rc = 0;
	} /* bufs loop */

	return rc;
}

int q6usm_write(struct us_client *usc, uint32_t write_ind)
{
	int rc = 0;
	struct usm_stream_cmd_write cmd_write;
	struct us_port_data *port = NULL;
	u32 current_dsp_buf = 0;
	u64 buf_addr = 0;

	if ((usc == NULL) || (usc->apr == NULL)) {
		pr_err("%s: APR handle NULL\n", __func__);
		return -EINVAL;
	}
	port = &usc->port[IN];

	current_dsp_buf = port->dsp_buf;
	/* free region, caused by new dsp_buf report from DSP, */
	/* can be only extended */
	if (port->cpu_buf >= current_dsp_buf) {
		/* 2 -part free region, including empty buffer */
		if ((write_ind <= port->cpu_buf)  &&
		    (write_ind > current_dsp_buf)) {
			pr_err("%s: wrong w_ind[%d]; d_buf=%d; c_buf=%d\n",
			       __func__, write_ind,
			       current_dsp_buf, port->cpu_buf);
			return -EINVAL;
		}
	} else {
		/* 1 -part free region */
		if ((write_ind <= port->cpu_buf)  ||
		    (write_ind > current_dsp_buf)) {
			pr_err("%s: wrong w_ind[%d]; d_buf=%d; c_buf=%d\n",
			       __func__, write_ind,
			       current_dsp_buf, port->cpu_buf);
			return -EINVAL;
		}
	}

	q6usm_add_hdr(usc, &cmd_write.hdr, sizeof(cmd_write), false);

	cmd_write.hdr.opcode = USM_DATA_CMD_WRITE;
	cmd_write.buf_size = port->buf_size;
	buf_addr = (u64)(port->phys) + port->buf_size * (port->cpu_buf);
	cmd_write.buf_addr_lsw = lower_32_bits(buf_addr);
	cmd_write.buf_addr_msw = msm_audio_populate_upper_32_bits(buf_addr);
	cmd_write.mem_map_handle = *((uint32_t *)(port->ext));
	cmd_write.res0 = 0;
	cmd_write.res1 = 0;
	cmd_write.res2 = 0;

	while (port->cpu_buf != write_ind) {
		u32 temp_cpu_buf = port->cpu_buf;

		buf_addr = (u64)(port->phys) +
				port->buf_size * (port->cpu_buf);
		cmd_write.buf_addr_lsw = lower_32_bits(buf_addr);
		cmd_write.buf_addr_msw =
				msm_audio_populate_upper_32_bits(buf_addr);
		cmd_write.seq_id = port->cpu_buf;
		cmd_write.hdr.token = port->cpu_buf;

		++(port->cpu_buf);
		if (port->cpu_buf == port->buf_cnt)
			port->cpu_buf = 0;

		rc = apr_send_pkt(usc->apr, (uint32_t *) &cmd_write);

		if (rc < 0) {
			port->cpu_buf = temp_cpu_buf;
			pr_err("%s:write op[0x%x];rc[%d];cpu_buf[%d]\n",
			       __func__, cmd_write.hdr.opcode,
			       rc, port->cpu_buf);
			break;
		}

		rc = 0;
	}

	return rc;
}

bool q6usm_is_write_buf_full(struct us_client *usc, uint32_t *free_region)
{
	struct us_port_data *port = NULL;
	u32 cpu_buf = 0;

	if ((usc == NULL) || !free_region) {
		pr_err("%s: input data wrong\n", __func__);
		return false;
	}
	port = &usc->port[IN];
	cpu_buf = port->cpu_buf + 1;
	if (cpu_buf == port->buf_cnt)
		cpu_buf = 0;

	*free_region = port->dsp_buf;

	return cpu_buf == *free_region;
}

int q6usm_cmd(struct us_client *usc, int cmd)
{
	struct apr_hdr hdr;
	int rc = 0;
	atomic_t *state;

	if ((usc == NULL) || (usc->apr == NULL)) {
		pr_err("%s: APR handle NULL\n", __func__);
		return -EINVAL;
	}
	q6usm_add_hdr(usc, &hdr, sizeof(hdr), true);
	switch (cmd) {
	case CMD_CLOSE:
		hdr.opcode = USM_STREAM_CMD_CLOSE;
		state = &usc->cmd_state;
		break;

	default:
		pr_err("%s:Invalid format[%d]\n", __func__, cmd);
		goto fail_cmd;
	}

	rc = apr_send_pkt(usc->apr, (uint32_t *) &hdr);
	if (rc < 0) {
		pr_err("%s: Command 0x%x failed\n", __func__, hdr.opcode);
		goto fail_cmd;
	}
	rc = wait_event_timeout(usc->cmd_wait, (atomic_read(state) == 0),
				Q6USM_TIMEOUT_JIFFIES);
	if (!rc) {
		rc = -ETIME;
		pr_err("%s:timeout. waited for response opcode[0x%x]\n",
		       __func__, hdr.opcode);
	} else
		rc = 0;
fail_cmd:
	return rc;
}

int q6usm_set_us_detection(struct us_client *usc,
			   struct usm_session_cmd_detect_info *detect_info,
			   uint16_t detect_info_size)
{
	int rc = 0;

	if ((usc == NULL) ||
	    (detect_info_size == 0) ||
	    (detect_info == NULL)) {
		pr_err("%s: wrong input: usc=0x%pK, inf_size=%d; info=0x%pK",
		       __func__,
		       usc,
		       detect_info_size,
		       detect_info);
		return -EINVAL;
	}

	q6usm_add_hdr(usc, &detect_info->hdr, detect_info_size, true);

	detect_info->hdr.opcode = USM_SESSION_CMD_SIGNAL_DETECT_MODE;

	rc = apr_send_pkt(usc->apr, (uint32_t *)detect_info);
	if (rc < 0) {
		pr_err("%s:Comamnd signal detect failed\n", __func__);
		return -EINVAL;
	}
	rc = wait_event_timeout(usc->cmd_wait,
				(atomic_read(&usc->cmd_state) == 0),
				Q6USM_TIMEOUT_JIFFIES);
	if (!rc) {
		rc = -ETIME;
		pr_err("%s: CMD_SIGNAL_DETECT_MODE: timeout=%d\n",
		       __func__, Q6USM_TIMEOUT_JIFFIES);
	} else
		rc = 0;

	return rc;
}

int q6usm_set_us_stream_param(int dir, struct us_client *usc,
		uint32_t module_id, uint32_t param_id, uint32_t buf_size)
{
	int rc = 0;
	struct usm_stream_cmd_set_param cmd_set_param;
	struct us_port_data *port = NULL;

	if ((usc == NULL) || (usc->apr == NULL)) {
		pr_err("%s: APR handle NULL\n", __func__);
		return -EINVAL;
	}
	port = &usc->port[dir];

	q6usm_add_hdr(usc, &cmd_set_param.hdr, sizeof(cmd_set_param), true);

	cmd_set_param.hdr.opcode = USM_STREAM_CMD_SET_PARAM;
	cmd_set_param.buf_size = buf_size;
	cmd_set_param.buf_addr_msw =
			msm_audio_populate_upper_32_bits(port->param_phys);
	cmd_set_param.buf_addr_lsw = lower_32_bits(port->param_phys);
	cmd_set_param.mem_map_handle =
			*((uint32_t *)(port->param_buf_mem_handle));
	cmd_set_param.module_id = module_id;
	cmd_set_param.param_id = param_id;
	cmd_set_param.hdr.token = 0;

	rc = apr_send_pkt(usc->apr, (uint32_t *) &cmd_set_param);

	if (rc < 0) {
		pr_err("%s:write op[0x%x];rc[%d]\n",
			__func__, cmd_set_param.hdr.opcode, rc);
	}

	rc = wait_event_timeout(usc->cmd_wait,
				(atomic_read(&usc->cmd_state) == 0),
				Q6USM_TIMEOUT_JIFFIES);
	if (!rc) {
		rc = -ETIME;
		pr_err("%s: CMD_SET_PARAM: timeout=%d\n",
			__func__, Q6USM_TIMEOUT_JIFFIES);
	} else
		rc = 0;

	return rc;
}

int q6usm_get_us_stream_param(int dir, struct us_client *usc,
		uint32_t module_id, uint32_t param_id, uint32_t buf_size)
{
	int rc = 0;
	struct usm_stream_cmd_get_param cmd_get_param;
	struct us_port_data *port = NULL;

	if ((usc == NULL) || (usc->apr == NULL)) {
		pr_err("%s: APR handle NULL\n", __func__);
		return -EINVAL;
	}
	port = &usc->port[dir];

	q6usm_add_hdr(usc, &cmd_get_param.hdr, sizeof(cmd_get_param), true);

	cmd_get_param.hdr.opcode = USM_STREAM_CMD_GET_PARAM;
	cmd_get_param.buf_size = buf_size;
	cmd_get_param.buf_addr_msw =
			msm_audio_populate_upper_32_bits(port->param_phys);
	cmd_get_param.buf_addr_lsw = lower_32_bits(port->param_phys);
	cmd_get_param.mem_map_handle =
			*((uint32_t *)(port->param_buf_mem_handle));
	cmd_get_param.module_id = module_id;
	cmd_get_param.param_id = param_id;
	cmd_get_param.hdr.token = 0;

	rc = apr_send_pkt(usc->apr, (uint32_t *) &cmd_get_param);

	if (rc < 0) {
		pr_err("%s:write op[0x%x];rc[%d]\n",
			__func__, cmd_get_param.hdr.opcode, rc);
	}

	rc = wait_event_timeout(usc->cmd_wait,
				(atomic_read(&usc->cmd_state) == 0),
				Q6USM_TIMEOUT_JIFFIES);
	if (!rc) {
		rc = -ETIME;
		pr_err("%s: CMD_GET_PARAM: timeout=%d\n",
			__func__, Q6USM_TIMEOUT_JIFFIES);
	} else
		rc = 0;

	return rc;
}

static int __init q6usm_init(void)
{
	pr_debug("%s\n", __func__);
	init_waitqueue_head(&this_mmap.cmd_wait);
	memset(session, 0, sizeof(session));
	return 0;
}

device_initcall(q6usm_init);