summaryrefstree
path: root/kernel/sound/usb/exynos_usb_audio.c
blob: f412adea1452994f17ef1d8f1f629e2a38ca22de (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
/*
 *   USB Audio offloading Driver for Exynos
 *
 *   Copyright (c) 2017 by Kyounghye Yun <k-hye.yun@samsung.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   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.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */


#include <linux/bitops.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/usb.h>
#include <linux/usb/audio.h>
#include <linux/usb/audio-v2.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/io.h>
#include <linux/dma-mapping.h>
#include <linux/completion.h>

#include <sound/samsung/abox.h>
#include <linux/usb/exynos_usb_audio.h>

#define DEBUG 1

static const unsigned long USB_LOCK_ID_OUT = 0x4F425355; // ASCII Value : USBO
static const unsigned long USB_LOCK_ID_IN = 0x49425355; // ASCII Value : USBI

void exynos_usb_audio_set_device(struct usb_device *udev)
{
	usb_audio->udev = udev;
}

int exynos_usb_audio_map_buf(struct usb_device *udev)
{
	ABOX_IPC_MSG msg;
	struct platform_device *pdev = usb_audio->abox;
	struct device *dev = &pdev->dev;
	struct IPC_ERAP_MSG *erap_msg = &msg.msg.erap;
	struct ERAP_USB_AUDIO_PARAM *erap_usb = &erap_msg->param.usbaudio;
	struct hcd_hw_info *hwinfo = &udev->hwinfo;
	struct usb_device *usb_dev = udev;
	u32 id;
	int ret;

	if (DEBUG)
		pr_info("USB_AUDIO_IPC : %s ", __func__);

	id = (((usb_dev->descriptor.idVendor) << 16) |
			(usb_dev->descriptor.idProduct));

	msg.ipcid = IPC_ERAP;
	erap_msg->msgtype = REALTIME_USB;
	erap_usb->type = IPC_USB_PCM_BUF;

	erap_usb->param1 = id;

	/* iommu map for in data buffer */
	usb_audio->in_buf_addr = hwinfo->in_dma;

	if (DEBUG) {
		pr_info("pcm in data buffer pa addr : %#08x %08x \n",
				upper_32_bits(le64_to_cpu(hwinfo->in_dma)),
				lower_32_bits(le64_to_cpu(hwinfo->in_dma)));
	}

	ret = abox_iommu_map(dev, USB_AUDIO_PCM_INBUF,
					hwinfo->in_dma, PAGE_SIZE * 256, 0);
	if (ret) {
		pr_err("abox iommu mapping for pcm in buf is failed \n");
		return ret;
	}

	/* iommu map for out data buffer */
	usb_audio->out_buf_addr = hwinfo->out_dma;

	if (DEBUG) {
		pr_info("pcm out data buffer pa addr : %#08x %08x \n",
				upper_32_bits(le64_to_cpu(hwinfo->out_dma)),
				lower_32_bits(le64_to_cpu(hwinfo->out_dma)));
	}

	ret = abox_iommu_map(dev, USB_AUDIO_PCM_OUTBUF,
					hwinfo->out_dma, PAGE_SIZE * 256, 0);
	if (ret) {
		pr_err("abox iommu mapping for pcm out buf is failed \n");
		return ret;
	}

	return 0;
}

int exynos_usb_audio_pcmbuf(struct usb_device *udev)
{
	ABOX_IPC_MSG msg;
	struct platform_device *pdev = usb_audio->abox;
	struct device *dev = &pdev->dev;
	struct IPC_ERAP_MSG *erap_msg = &msg.msg.erap;
	struct ERAP_USB_AUDIO_PARAM *erap_usb = &erap_msg->param.usbaudio;
	struct hcd_hw_info *hwinfo = &udev->hwinfo;
	u64 out_dma;
	int ret;

	if (DEBUG)
		pr_info("USB_AUDIO_IPC : %s\n", __func__);

	if (!usb_audio->is_audio || !otg_connection) {
		dev_info(dev, "USB_AUDIO_IPC : is_audio is 0. return!\n");
		return -1;
	}

	out_dma = abox_iova_to_phys(dev, USB_AUDIO_PCM_OUTBUF);

	msg.ipcid = IPC_ERAP;
	erap_msg->msgtype = REALTIME_USB;
	erap_usb->type = IPC_USB_PCM_BUF;

	erap_usb->param1 = lower_32_bits(le64_to_cpu(out_dma));
	erap_usb->param2 = upper_32_bits(le64_to_cpu(out_dma));
	erap_usb->param3 = lower_32_bits(le64_to_cpu(hwinfo->in_dma));
	erap_usb->param4 = upper_32_bits(le64_to_cpu(hwinfo->in_dma));

	if (DEBUG) {
		pr_info("pcm out data buffer pa addr : %#08x %08x\n",
				upper_32_bits(le64_to_cpu(out_dma)),
				lower_32_bits(le64_to_cpu(out_dma)));
		pr_info("pcm in data buffer pa addr : %#08x %08x\n",
				upper_32_bits(le64_to_cpu(hwinfo->in_dma)),
				lower_32_bits(le64_to_cpu(hwinfo->in_dma)));
		pr_info("erap param2 : %#08x param1 : %08x\n",
				erap_usb->param2, erap_usb->param1);
		pr_info("erap param4 : %#08x param3 : %08x\n",
				erap_usb->param4, erap_usb->param3);
	}

	ret = abox_start_ipc_transaction(dev, msg.ipcid, &msg, sizeof(msg), 0, 1);
	if (ret) {
		dev_err(&usb_audio->udev->dev, "erap usb transfer pcm buffer is failed\n");
		return -1;
	}

	return 0;
}

int exynos_usb_audio_setrate(int iface, int rate, int alt)
{
	ABOX_IPC_MSG msg;
	struct platform_device *pdev = usb_audio->abox;
	struct device *dev = &pdev->dev;
	struct IPC_ERAP_MSG *erap_msg = &msg.msg.erap;
	struct ERAP_USB_AUDIO_PARAM *erap_usb = &erap_msg->param.usbaudio;
	int ret;

	if (DEBUG)
		pr_info("USB_AUDIO_IPC : %s\n", __func__);

	if (!usb_audio->is_audio || !otg_connection) {
		dev_info(dev, "USB_AUDIO_IPC : is_audio is 0. return!\n");
		return -1;
	}

	msg.ipcid = IPC_ERAP;
	erap_msg->msgtype = REALTIME_USB;
	erap_usb->type = IPC_USB_SAMPLE_RATE;

	erap_usb->param1 = iface;
	erap_usb->param2 = rate;
	erap_usb->param3 = alt;

	ret = abox_start_ipc_transaction(dev, msg.ipcid, &msg, sizeof(msg), 0, 1);
	if (ret) {
		dev_err(&usb_audio->udev->dev, "erap usb transfer sample rate is failed\n");
		return -1;
	}

	return 0;
}

int exynos_usb_audio_setintf(struct usb_device *udev, int iface, int alt, int direction)
{
	ABOX_IPC_MSG msg;
	struct platform_device *pdev = usb_audio->abox;
	struct device *dev = &pdev->dev;
	struct IPC_ERAP_MSG *erap_msg = &msg.msg.erap;
	struct ERAP_USB_AUDIO_PARAM *erap_usb = &erap_msg->param.usbaudio;
	struct hcd_hw_info *hwinfo = &udev->hwinfo;
	u64 in_offset, out_offset;
	int ret;
	unsigned long left_time;

	if (!usb_audio->pcm_open_done) {
		dev_info(dev, "USB_AUDIO_IPC : pcm node was not opened!\n");
		return -EPERM;
	}

	if (DEBUG)
		dev_info(&udev->dev, "USB_AUDIO_IPC : %s, alt = %d\n",
				__func__, alt);

	if (!usb_audio->is_audio || !otg_connection) {
		dev_info(dev, "USB_AUDIO_IPC : is_audio is 0. return!\n");
		return -1;
	}

	msg.ipcid = IPC_ERAP;
	erap_msg->msgtype = REALTIME_USB;
	erap_usb->type = IPC_USB_SET_INTF;

	erap_usb->param1 = alt;
	erap_usb->param2 = iface;

	if (alt == 0 && direction == SNDRV_PCM_STREAM_PLAYBACK) {
		if (usb_audio->pcm_open_done &&
				(!usb_audio->outdeq_map_done ||
				(hwinfo->out_deq != hwinfo->old_out_deq))) {
			dev_info(dev, "out_deq wait required\n");
			left_time = wait_for_completion_timeout(&usb_audio->out_conn_stop,
								msecs_to_jiffies(1000));
			if (!left_time)
				dev_info(dev, "%s: timeout for OUT connection done\n");
		}
	} else if (alt == 0 && direction == SNDRV_PCM_STREAM_CAPTURE) {
		if (usb_audio->pcm_open_done &&
				(!usb_audio->indeq_map_done ||
				(hwinfo->in_deq != hwinfo->old_in_deq))) {
			dev_info(dev, "in_deq wait required\n");
			left_time = wait_for_completion_timeout(&usb_audio->in_conn_stop,
								msecs_to_jiffies(1000));
			if (!left_time)
				dev_info(dev, "%s: timeout for IN connection done\n");
		}
	}

	if (!usb_audio->is_audio || !otg_connection) {
		dev_info(dev, "USB_AUDIO_IPC : is_audio = 0. return!\n");
		return -1;
	}

	mutex_lock(&usb_audio->lock);
	if (direction) {
		/* IN EP */
		dev_dbg(dev, "in deq : %#08llx, %#08llx, %#08llx, %#08llx\n",
				hwinfo->in_deq, hwinfo->old_in_deq,
				hwinfo->fb_out_deq, hwinfo->fb_old_out_deq);
		if (!usb_audio->indeq_map_done ||
			(hwinfo->in_deq != hwinfo->old_in_deq)) {
			dev_info(dev, "in_deq map required\n");
			if (usb_audio->indeq_map_done) {
				ret = abox_iommu_unmap(dev, USB_AUDIO_IN_DEQ);
				if (ret < 0) {
					pr_err("un-map of in buf failed %d\n", ret);
					goto err;
				}
			}
			usb_audio->indeq_map_done = 1;

			in_offset = hwinfo->in_deq % PAGE_SIZE;
			ret = abox_iommu_map(dev, USB_AUDIO_IN_DEQ,
					(hwinfo->in_deq - in_offset),
					PAGE_SIZE, 0);
			if (ret < 0) {
				pr_err("map for in buf failed %d\n", ret);
				goto err;
			}
		}

		if (hwinfo->fb_out_deq) {
			dev_dbg(dev, "fb_in deq : %#08llx\n", hwinfo->fb_out_deq);
			if (!usb_audio->fb_outdeq_map_done ||
				(hwinfo->fb_out_deq != hwinfo->fb_old_out_deq)) {
				if (usb_audio->fb_outdeq_map_done) {
					ret = abox_iommu_unmap(dev, USB_AUDIO_FBOUT_DEQ);
					if (ret < 0) {
						pr_err("un-map for fb_out buf failed %d\n", ret);
						goto err;
					}
				}
				usb_audio->fb_outdeq_map_done = 1;

				out_offset = hwinfo->fb_out_deq % PAGE_SIZE;
				ret = abox_iommu_map(dev, USB_AUDIO_FBOUT_DEQ,
						(hwinfo->fb_out_deq - out_offset),
						PAGE_SIZE, 0);
				if (ret < 0) {
					pr_err("map for fb_out buf failed %d\n", ret);
					goto err;
				}
			}
		}

		erap_usb->param3 = lower_32_bits(le64_to_cpu(hwinfo->in_deq));
		erap_usb->param4 = upper_32_bits(le64_to_cpu(hwinfo->in_deq));
	} else {
		/* OUT EP */
		dev_dbg(dev, "out deq : %#08llx, %#08llx, %#08llx, %#08llx\n",
				hwinfo->out_deq, hwinfo->old_out_deq,
				hwinfo->fb_in_deq, hwinfo->fb_old_in_deq);
		if (!usb_audio->outdeq_map_done ||
			(hwinfo->out_deq != hwinfo->old_out_deq)) {
			dev_info(dev, "out_deq map required\n");
			if (usb_audio->outdeq_map_done) {
				ret = abox_iommu_unmap(dev, USB_AUDIO_OUT_DEQ);
				if (ret < 0) {
					pr_err("un-map for out buf failed %d\n", ret);
					goto err;
				}
			}
			usb_audio->outdeq_map_done = 1;

			out_offset = hwinfo->out_deq % PAGE_SIZE;
			ret = abox_iommu_map(dev, USB_AUDIO_OUT_DEQ,
					(hwinfo->out_deq - out_offset),
					PAGE_SIZE, 0);
			if (ret < 0) {
				pr_err("map for out buf failed %d\n", ret);
				goto err;
			}
		}

		if (hwinfo->fb_in_deq) {
			dev_info(dev, "fb_in deq : %#08llx\n", hwinfo->fb_in_deq);
			if (!usb_audio->fb_indeq_map_done ||
				(hwinfo->fb_in_deq != hwinfo->fb_old_in_deq)) {
				if (usb_audio->fb_indeq_map_done) {
					ret = abox_iommu_unmap(dev, USB_AUDIO_FBIN_DEQ);
					if (ret < 0) {
						pr_err("un-map for fb_in buf failed %d\n", ret);
						goto err;
					}
				}
				usb_audio->fb_indeq_map_done = 1;

				in_offset = hwinfo->fb_in_deq % PAGE_SIZE;
				ret = abox_iommu_map(dev, USB_AUDIO_FBIN_DEQ,
						(hwinfo->fb_in_deq - in_offset),
						PAGE_SIZE, 0);
				if (ret < 0) {
					pr_err("map for fb_in buf failed %d\n", ret);
					goto err;
				}
			}
		}

		erap_usb->param3 = lower_32_bits(le64_to_cpu(hwinfo->out_deq));
		erap_usb->param4 = upper_32_bits(le64_to_cpu(hwinfo->out_deq));
	}

	/* one more check connection to prevent kernel panic */
	if (!usb_audio->is_audio || !otg_connection) {
		dev_info(dev, "USB_AUDIO_IPC : is_audio is 0. return!\n");
		return -1;
	}

	ret = abox_start_ipc_transaction(dev, msg.ipcid, &msg, sizeof(msg), 0, 1);
	if (ret) {
		dev_err(&usb_audio->udev->dev, "erap usb hcd control failed\n");
		goto err;
	}
	mutex_unlock(&usb_audio->lock);

	if (DEBUG) {
		dev_info(&udev->dev, "Alt#%d / Intf#%d / Direction %s / EP DEQ : %#08x %08x\n",
				erap_usb->param1, erap_usb->param2,
				direction ? "IN" : "OUT",
				erap_usb->param4, erap_usb->param3);
	}

	return 0;

err:
	dev_err(&udev->dev, "%s error = %d\n", __func__, ret);
	mutex_unlock(&usb_audio->lock);
	return ret;
}

int exynos_usb_audio_hcd(struct usb_device *udev)
{
	ABOX_IPC_MSG msg;
	struct platform_device *pdev = usb_audio->abox;
	struct device *dev = &pdev->dev;
	struct IPC_ERAP_MSG *erap_msg = &msg.msg.erap;
	struct ERAP_USB_AUDIO_PARAM *erap_usb = &erap_msg->param.usbaudio;
	struct hcd_hw_info *hwinfo = &udev->hwinfo;
	int ret;

	if (DEBUG) {
		dev_info(&udev->dev, "USB_AUDIO_IPC : %s\n", __func__);
		dev_info(&udev->dev, "=======[Check HW INFO] ========\n");
		dev_info(&udev->dev, "slot_id : %d\n", hwinfo->slot_id);
		dev_info(&udev->dev, "dcbaa : %#08llx\n", hwinfo->dcbaa_dma);
		dev_info(&udev->dev, "save : %#08llx\n", hwinfo->save_dma);
		dev_info(&udev->dev, "in_ctx : %#08llx\n", hwinfo->in_ctx);
		dev_info(&udev->dev, "out_ctx : %#08llx\n", hwinfo->out_ctx);
		dev_info(&udev->dev, "erst : %#08x %08x\n",
					upper_32_bits(le64_to_cpu(hwinfo->erst_addr)),
					lower_32_bits(le64_to_cpu(hwinfo->erst_addr)));
		dev_info(&udev->dev, "use_uram : %d\n", hwinfo->use_uram);
		dev_info(&udev->dev, "===============================\n");
	}

	/* back up each address for unmap */
	usb_audio->dcbaa_dma = hwinfo->dcbaa_dma;
	usb_audio->save_dma = hwinfo->save_dma;
	usb_audio->in_ctx = hwinfo->in_ctx;
	usb_audio->out_ctx = hwinfo->out_ctx;
	usb_audio->erst_addr = hwinfo->erst_addr;
	usb_audio->speed = hwinfo->speed;
	usb_audio->use_uram = hwinfo->use_uram;

	if (DEBUG)
		dev_info(dev, "USB_AUDIO_IPC : SFR MAPPING!\n");

	mutex_lock(&usb_audio->lock);
	ret = abox_iommu_map(dev, USB_AUDIO_XHCI_BASE, USB_AUDIO_XHCI_BASE, PAGE_SIZE * 16, 0);
	/*
	 * Check whether usb buffer was unmapped already.
	 * If not, unmap all buffers and try map again.
	 */
	if (ret == -EADDRINUSE) {
		cancel_work_sync(&usb_audio->usb_work);
		pr_err("iommu unmapping not done. unmap here\n", ret);
		exynos_usb_audio_unmap_all();
		ret = abox_iommu_map(dev, USB_AUDIO_XHCI_BASE,
				USB_AUDIO_XHCI_BASE, PAGE_SIZE * 16, 0);
	}

	if (ret) {
		pr_err("iommu mapping for in buf failed %d\n", ret);
		goto err;
	}

	/*DCBAA mapping*/
	ret = abox_iommu_map(dev, USB_AUDIO_SAVE_RESTORE, hwinfo->save_dma, PAGE_SIZE, 0);
	if (ret) {
		pr_err(" abox iommu mapping for save_restore buffer is failed\n");
		goto err;
	}

	/*Device Context mapping*/
	ret = abox_iommu_map(dev, USB_AUDIO_DEV_CTX, hwinfo->out_ctx, PAGE_SIZE, 0);
	if (ret) {
		pr_err(" abox iommu mapping for device ctx is failed\n");
		goto err;
	}

	/*Input Context mapping*/
	ret = abox_iommu_map(dev, USB_AUDIO_INPUT_CTX, hwinfo->in_ctx, PAGE_SIZE, 0);
	if (ret) {
		pr_err(" abox iommu mapping for input ctx is failed\n");
		goto err;
	}

	if (hwinfo->use_uram) {
		/*URAM Mapping*/
		ret = abox_iommu_map(dev, USB_URAM_BASE, USB_URAM_BASE, PAGE_SIZE * 3, 0);
		if (ret) {
			pr_err(" abox iommu mapping for URAM buffer is failed\n");
			goto err;
		}

		/* Mapping both URAM and original ERST address */
		ret = abox_iommu_map(dev, USB_AUDIO_ERST, hwinfo->erst_addr,
								PAGE_SIZE, 0);
		if (ret) {
			pr_err(" abox iommu mapping for erst is failed\n");
			goto err;
		}
	} else {
		/*ERST mapping*/
		ret = abox_iommu_map(dev, USB_AUDIO_ERST, hwinfo->erst_addr,
								PAGE_SIZE, 0);
		if (ret) {
			pr_err(" abox iommu mapping for erst is failed\n");
			goto err;
		}
	}

	/* notify to Abox descriptor is ready*/
	msg.ipcid = IPC_ERAP;
	erap_msg->msgtype = REALTIME_USB;
	erap_usb->type = IPC_USB_XHCI;

	erap_usb->param1 = usb_audio->is_first_probe;
	erap_usb->param2 = hwinfo->slot_id;
	erap_usb->param3 = lower_32_bits(le64_to_cpu(hwinfo->erst_addr));
	erap_usb->param4 = upper_32_bits(le64_to_cpu(hwinfo->erst_addr));

	ret = abox_start_ipc_transaction(dev, msg.ipcid, &msg, sizeof(msg), 0, 1);
	if (ret) {
		dev_err(&usb_audio->udev->dev, "erap usb hcd control failed\n");
		goto err;
	}
	usb_audio->is_first_probe = 0;
	mutex_unlock(&usb_audio->lock);

	return 0;

err:
	dev_err(&udev->dev, "%s error = %d\n", __func__, ret);
	usb_audio->is_first_probe = 0;
	mutex_unlock(&usb_audio->lock);
	return ret;
}

int exynos_usb_audio_desc(struct usb_device *udev)
{
	ABOX_IPC_MSG msg;
	struct platform_device *pdev = usb_audio->abox;
	struct device *dev = &pdev->dev;
	struct IPC_ERAP_MSG *erap_msg = &msg.msg.erap;
	struct ERAP_USB_AUDIO_PARAM *erap_usb = &erap_msg->param.usbaudio;
	int ret, configuration, cfgno, i;
	unsigned char *buffer;
	unsigned int len = udev->rawdesc_length;
	u64 desc_addr;
	u64 offset;

	if (DEBUG)
		dev_info(&udev->dev, "USB_AUDIO_IPC : %s\n", __func__);

	configuration = usb_choose_configuration(udev);

	cfgno = -1;
	for (i = 0; i < udev->descriptor.bNumConfigurations; i++) {
		if (udev->config[i].desc.bConfigurationValue ==
				configuration) {
			cfgno = i;
			pr_info("%s - chosen = %d, c = %d\n", __func__,
				i, configuration);
			break;
		}
	}

	if (cfgno == -1) {
		pr_info("%s - config select error, i=%d, c=%d\n",
			__func__, i, configuration);
		cfgno = 0;
	}

	/* need to memory mapping for usb descriptor */
	buffer = udev->rawdescriptors[cfgno];
	desc_addr = virt_to_phys(buffer);
	offset = desc_addr % PAGE_SIZE;

	/* store address information */
	usb_audio->desc_addr = desc_addr;
	usb_audio->offset = offset;

	desc_addr -= offset;

	mutex_lock(&usb_audio->lock);
	ret = abox_iommu_map(dev, USB_AUDIO_DESC, desc_addr, (PAGE_SIZE * 2), 0);
	if (ret) {
		dev_err(&udev->dev, "USB AUDIO: abox iommu mapping for usb descriptor is failed\n");
		goto err;
	}

	/* notify to Abox descriptor is ready*/
	msg.ipcid = IPC_ERAP;
	erap_msg->msgtype = REALTIME_USB;
	erap_usb->type = IPC_USB_DESC;

	erap_usb->param1 = 1;
	erap_usb->param2 = len;
	erap_usb->param3 = offset;
	erap_usb->param4 = usb_audio->speed;

	if (DEBUG)
		dev_info(&udev->dev, "paddr : %#08llx / offset : %#08llx / len : %d / speed : %d\n",
							desc_addr + offset , offset, len, usb_audio->speed);

	ret = abox_start_ipc_transaction(dev, msg.ipcid, &msg, sizeof(msg), 0, 1);
	if (ret) {
		dev_err(&usb_audio->udev->dev, "erap usb desc  control failed\n");
		goto err;
	}
	mutex_unlock(&usb_audio->lock);

	dev_info(&udev->dev, "USB AUDIO: Mapping descriptor for using on Abox USB F/W & Nofity mapping is done!");

	return 0;

err:
	dev_err(&udev->dev, "%s error = %d\n", __func__, ret);
	mutex_unlock(&usb_audio->lock);
	return ret;
}

int exynos_usb_audio_conn(int is_conn)
{
	ABOX_IPC_MSG msg;
	struct platform_device *pdev = usb_audio->abox;
	struct device *dev = &pdev->dev;
	struct IPC_ERAP_MSG *erap_msg = &msg.msg.erap;
	struct ERAP_USB_AUDIO_PARAM *erap_usb = &erap_msg->param.usbaudio;
	int ret;

	if (DEBUG)
		pr_info("USB_AUDIO_IPC : %s\n", __func__);

	pr_info("USB DEVICE IS %s\n", is_conn? "CONNECTION" : "DISCONNECTION");

	msg.ipcid = IPC_ERAP;
	erap_msg->msgtype = REALTIME_USB;
	erap_usb->type = IPC_USB_CONN;

	erap_usb->param1 = is_conn;

	if (!is_conn) {
		if (usb_audio->is_audio) {
			usb_audio->is_audio = 0;
			usb_audio->usb_audio_state = USB_AUDIO_REMOVING;
			reinit_completion(&usb_audio->discon_done);
			ret = abox_start_ipc_transaction(dev, msg.ipcid, &msg, sizeof(msg), 0, 0);
			if (ret) {
				pr_err("erap usb dis_conn control failed\n");
				return -1;
			}
		} else {
			pr_err("Is not USB Audio device\n");
		}
	} else {
		cancel_work_sync(&usb_audio->usb_work);
		usb_audio->indeq_map_done = 0;
		usb_audio->outdeq_map_done = 0;
		usb_audio->fb_indeq_map_done = 0;
		usb_audio->fb_outdeq_map_done = 0;
		usb_audio->pcm_open_done = 0;
		ret = abox_start_ipc_transaction(dev, msg.ipcid, &msg, sizeof(msg), 0, 1);
		if (ret) {
			pr_err("erap usb conn control failed\n");
			return -1;
		}
		usb_audio->usb_audio_state = USB_AUDIO_CONNECT;
	}

#if 0
	if (!is_conn) {
		/* USB can be disconnected without pcm close.
		 * To prevent remnant dram request, release all in here.
		 */
		abox_request_dram_on(pdev, (void *)USB_LOCK_ID_IN, 0);
		abox_request_dram_on(pdev, (void *)USB_LOCK_ID_OUT, 0);
	}
#endif

	return 0;
}

int exynos_usb_audio_pcm(int is_open, int direction)
{
	ABOX_IPC_MSG msg;
	struct platform_device *pdev = usb_audio->abox;
	struct device *dev = &pdev->dev;
	struct IPC_ERAP_MSG *erap_msg = &msg.msg.erap;
	struct ERAP_USB_AUDIO_PARAM *erap_usb = &erap_msg->param.usbaudio;
	int ret;

	if (DEBUG)
		dev_info(dev, "USB_AUDIO_IPC : %s\n", __func__);

	if (!usb_audio->is_audio || !otg_connection) {
		dev_info(dev, "USB_AUDIO_IPC : is_audio is 0. return!\n");
		return -1;
	}

	if (is_open)
		usb_audio->pcm_open_done = 1;
#ifdef CONFIG_USB_DEBUG_DETAILED_LOG
	dev_info(dev, "PCM %s dir %s\n", is_open? "OPEN" : "CLOSE",
				direction ? "IN" : "OUT");
#endif
	msg.ipcid = IPC_ERAP;
	erap_msg->msgtype = REALTIME_USB;
	erap_usb->type = IPC_USB_PCM_OPEN;

	erap_usb->param1 = is_open;
	erap_usb->param2 = direction;

	ret = abox_start_ipc_transaction(dev, msg.ipcid, &msg, sizeof(msg), 0, 1);
	if (ret) {
		dev_err(&usb_audio->udev->dev, "ERAP USB PCM control failed\n");
		return -1;
	}

	return 0;
}

int exynos_usb_audio_l2(int is_l2)
{
	ABOX_IPC_MSG msg;
	struct platform_device *pdev = usb_audio->abox;
	struct device *dev = &pdev->dev;
	struct IPC_ERAP_MSG *erap_msg = &msg.msg.erap;
	struct ERAP_USB_AUDIO_PARAM *erap_usb = &erap_msg->param.usbaudio;
	int ret;

	if (!usb_audio->is_audio || !otg_connection)
		return 0;

	if (DEBUG)
		dev_info(dev, "USB_AUDIO_IPC : %s\n", __func__);
	dev_info(dev, " USB Audio  %s the L2 power mode\n", is_l2 ? "ENTER" : "EXIT");

	msg.ipcid = IPC_ERAP;
	erap_msg->msgtype = REALTIME_USB;
	erap_usb->type = IPC_USB_L2;

	/* 1: device entered L2 , 0: device exited L2 */
	erap_usb->param1 = is_l2;

	ret = abox_start_ipc_transaction(dev, msg.ipcid, &msg, sizeof(msg), 0, 1);
	if (ret) {
		dev_err(&usb_audio->udev->dev, "ERAP USB L2 control failed\n");
		return -1;
	}

	return 0;
}

void exynos_usb_audio_work(struct work_struct *w)
{
	pr_info("%s\n", __func__);

	/* Don't unmap in USB_AUDIO_TIMEOUT_PROBE state */
	if (usb_audio->usb_audio_state !=
			USB_AUDIO_REMOVING) {
		pr_info("%s, not removing state\n", __func__);
		return;
	}

	exynos_usb_audio_unmap_all();
	usb_audio->usb_audio_state = USB_AUDIO_DISCONNECT;
	complete(&usb_audio->discon_done);
}

irqreturn_t exynos_usb_audio_irq_handler(int irq, void *dev_id, ABOX_IPC_MSG *msg)
{
	struct IPC_ERAP_MSG *erap_msg = &msg->msg.erap;

	if (DEBUG)
		pr_info("%s: IRQ = %d, type=%d, par1 = %d\n", __func__,
				irq, erap_msg->param.usbaudio.type,
				erap_msg->param.usbaudio.param1);

	if ( irq == IPC_ERAP ) {
		if (erap_msg->msgtype == REALTIME_USB) {
			switch (erap_msg->param.usbaudio.type) {
			case IPC_USB_TASK:
				if (erap_msg->param.usbaudio.param1) {
					pr_info("irq : %d /* param1 : 1 , IN EP task done */\n", irq);
				} else {
					pr_info("irq : %d /* param0 : 0 , OUT EP task done */\n", irq);
					/* Don't schedule in TIMEOUT_PROBE */
					if (usb_audio->usb_audio_state ==
							USB_AUDIO_REMOVING)
						schedule_work(&usb_audio
							->usb_work);
				}
				break;
			case IPC_USB_STOP_DONE:
				if (erap_msg->param.usbaudio.param1 ==
						SNDRV_PCM_STREAM_PLAYBACK)
					complete(&usb_audio->out_conn_stop);
				else if (erap_msg->param.usbaudio.param1 ==
						SNDRV_PCM_STREAM_CAPTURE)
					complete(&usb_audio->in_conn_stop);
				else
					pr_err("Unexpected USB_STOP_DONE param!!\n");

				break;
			default:
				pr_err("%s: unknown usb msg type\n", __func__);
				break;
			}
		} else {
			pr_err("%s: unknown message type\n", __func__);
		}
	} else {
		pr_err("%s: unknown command\n", __func__);
	}
	/* FIXME */
	return IRQ_HANDLED;
}

int exynos_usb_audio_init(struct device *dev, struct platform_device *pdev)
{
	struct device_node *np = dev->of_node;
	struct device_node *np_abox;
	struct platform_device *pdev_abox;

	if (DEBUG)
		dev_info(dev, "USB_AUDIO_IPC : %s\n", __func__);

	np_abox =of_parse_phandle(np, "abox", 0);
	if(!np_abox) {
		dev_err(dev, "Failed to get abox device node\n");
		return -EPROBE_DEFER;
	}

	pdev_abox = of_find_device_by_node(np_abox);
	if (!pdev_abox) {
		dev_err(&usb_audio->udev->dev, "Failed to get abox platform device\n");
		return -EPROBE_DEFER;
	}

	mutex_init(&usb_audio->lock);
	init_completion(&usb_audio->in_conn_stop);
	init_completion(&usb_audio->out_conn_stop);
	init_completion(&usb_audio->discon_done);
	usb_audio->abox = pdev_abox;
	usb_audio->hcd_pdev = pdev;
	usb_audio->udev = NULL;
	usb_audio->is_audio = 0;
	usb_audio->is_first_probe = 1;
	usb_audio->usb_audio_state = USB_AUDIO_DISCONNECT;

	abox_register_irq_handler(&pdev_abox->dev, IPC_ERAP,
				exynos_usb_audio_irq_handler, &usb_audio);

	return 0;
}

int exynos_usb_audio_unmap_all(void)
{
	struct platform_device *pdev = usb_audio->abox;
	struct device *dev = &pdev->dev;
	struct hcd_hw_info *hwinfo = &usb_audio->udev->hwinfo;
	u64 addr;
	u64 offset;
	int ret;

	if (DEBUG)
		dev_info(dev, "USB_AUDIO_IPC : %s\n", __func__);

	/* unmapping in pcm buffer */
	addr = usb_audio->in_buf_addr;
	if (DEBUG)
		pr_info("PCM IN BUFFER FREE: paddr = %#08llx\n", addr);

	ret = abox_iommu_unmap(dev, USB_AUDIO_PCM_INBUF);
	if (ret < 0) {
		pr_err("iommu un-mapping for in buf failed %d\n", ret);
		return ret;
	}

	addr = usb_audio->out_buf_addr;
	if (DEBUG)
		pr_info("PCM OUT BUFFER FREE: paddr = %#08llx\n", addr);

	ret = abox_iommu_unmap(dev, USB_AUDIO_PCM_OUTBUF);
	if (ret < 0) {
		pr_err("iommu unmapping for pcm out buf failed\n");
		return ret;
	}

	/* unmapping usb descriptor */
	addr = usb_audio->desc_addr;
	offset = usb_audio->offset;

	if (DEBUG)
		pr_info("DESC BUFFER: paddr : %#08llx / offset : %#08llx\n",
				addr, offset);

	ret = abox_iommu_unmap(dev, USB_AUDIO_DESC);
	if (ret < 0) {
		pr_err("iommu unmapping for descriptor failed\n");
		return ret;
	}

	ret = abox_iommu_unmap(dev, USB_AUDIO_SAVE_RESTORE);
	if (ret < 0) {
		pr_err(" abox iommu unmapping for dcbaa failed\n");
		return ret;
	}

	/*Device Context unmapping*/
	ret = abox_iommu_unmap(dev, USB_AUDIO_DEV_CTX);
	if (ret < 0) {
		pr_err(" abox iommu unmapping for device ctx failed\n");
		return ret;
	}

	/*Input Context unmapping*/
	ret = abox_iommu_unmap(dev, USB_AUDIO_INPUT_CTX);
	if (ret < 0) {
		pr_err(" abox iommu unmapping for input ctx failed\n");
		return ret;
	}

	/*ERST unmapping*/
	ret = abox_iommu_unmap(dev, USB_AUDIO_ERST);
	if (ret < 0) {
		pr_err(" abox iommu Un-mapping for erst failed\n");
		return ret;
	}

	ret = abox_iommu_unmap(dev, USB_AUDIO_IN_DEQ);
	if (ret < 0) {
		pr_err("abox iommu un-mapping for in buf failed %d\n", ret);
		return ret;
	}

	ret = abox_iommu_unmap(dev, USB_AUDIO_FBOUT_DEQ);
	if (ret < 0) {
		pr_err("abox iommu un-mapping for fb_out buf failed %d\n", ret);
		return ret;
	}

	ret = abox_iommu_unmap(dev, USB_AUDIO_OUT_DEQ);
	if (ret < 0) {
		pr_err("iommu un-mapping for out buf failed %d\n", ret);
		return ret;
	}

	ret = abox_iommu_unmap(dev, USB_AUDIO_FBIN_DEQ);
	if (ret < 0) {
		pr_err("iommu un-mapping for fb_in buf failed %d\n", ret);
		return ret;
	}

	ret = abox_iommu_unmap(dev, USB_AUDIO_XHCI_BASE);
	if (ret < 0) {
		pr_err(" abox iommu Un-mapping for in buf failed\n");
		return ret;
	}

	if (hwinfo->use_uram) {
		ret = abox_iommu_unmap(dev, USB_URAM_BASE);
		if (ret < 0) {
			pr_err(" abox iommu Un-mapping for in buf failed - URAM\n");
			return ret;
		}
	}

	return 0;
}

int exynos_usb_audio_exit(void)
{
	/* future use */
	return 0;
}