aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/xlnx_tsmux/xlnx_mpg2tsmux.c
blob: 84f4b501570af626e3609024f45e8a064becbac7 (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
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
// SPDX-License-Identifier: GPL-2.0
/*
 * Xilinx TS mux driver
 *
 * Copyright (C) 2019 Xilinx, Inc.
 *
 * Author: Venkateshwar Rao G <venkateshwar.rao.gannavarapu@xilinx.com>
 */
#include <linux/bitops.h>
#include <linux/cdev.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/dmapool.h>
#include <linux/dma-buf.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/platform_device.h>
#include <linux/poll.h>
#include <uapi/linux/xlnx_mpg2tsmux_interface.h>

#define DRIVER_NAME "mpegtsmux-1.0"
#define DRIVER_CLASS "mpg2mux_ts_cls"
#define DRIVER_MAX_DEV (10)

/* Register offsets and bit masks */
#define XTSMUX_RST_CTRL			0x00
#define XTSMUX_GLBL_IER			0x04
#define XTSMUX_IER_STAT			0x08
#define XTSMUX_ISR_STAT			0x0c
#define XTSMUX_ERR_STAT			0x10
#define XTSMUX_LAST_NODE_PROCESSED	0x14
#define XTSMUX_MUXCONTEXT_ADDR		0x20
#define XTSMUX_STREAMCONTEXT_ADDR	0x30
#define XTSMUX_NUM_STREAM_IDTBL		0x48
#define XTSMUX_NUM_DESC			0x70
#define XTSMUX_STREAM_IDTBL_ADDR	0x78
#define XTSMUX_CONTEXT_DATA_SIZE	64

#define XTSMUX_RST_CTRL_START_MASK	BIT(0)
#define XTSMUX_GLBL_IER_ENABLE_MASK	BIT(0)
#define XTSMUX_IER_ENABLE_MASK		BIT(0)

/* Number of input/output streams supported */
#define XTSMUX_MAXIN_STRM		112
#define XTSMUX_MAXIN_PLSTRM		16
#define XTSMUX_MAXIN_TLSTRM	(XTSMUX_MAXIN_STRM + XTSMUX_MAXIN_PLSTRM)
#define XTSMUX_MAXOUT_STRM		112
#define XTSMUX_MAXOUT_PLSTRM		16
#define XTSMUX_MAXOUT_TLSTRM	(XTSMUX_MAXOUT_STRM + XTSMUX_MAXOUT_PLSTRM)
#define XTSMUX_POOL_SIZE		128
/* Initial version is tested with 256 align only */
#define XTSMUX_POOL_ALIGN		256
#define XTSMUX_STRMBL_FREE		0
#define XTSMUX_STRMBL_BUSY		1

/**
 * struct stream_context - struct to enqueue a stream context descriptor
 * @command: stream context type
 * @is_pcr_stream: flag for pcr(programmable clock recovery) stream
 * @stream_id: stream identification number
 * @extended_stream_id: extended stream id
 * @reserved1: reserved for hardware alignment
 * @pid: packet id number
 * @dmabuf_id: 0 for buf allocated by driver, nonzero for external buf
 * @size_data_in: size in bytes of input buffer
 * @pts: presentation time stamp
 * @dts: display time stamp
 * @in_buf_pointer: physical address of src buf address
 * @reserved2: reserved for hardware alignment
 * @insert_pcr: inserting pcr in stream context
 * @reserved3: reserved for hardware alignment
 * @pcr_extension: pcr extension number
 * @pcr_base: pcr base number
 */
struct stream_context {
	enum ts_mux_command command;
	u8 is_pcr_stream;
	u8 stream_id;
	u8 extended_stream_id;
	u8 reserved1;
	u16 pid;
	u16 dmabuf_id;
	u32 size_data_in;
	u64 pts;
	u64 dts;
	u64 in_buf_pointer;
	u32 reserved2;
	u8 insert_pcr;
	u8 reserved3;
	u16 pcr_extension;
	u64 pcr_base;
};

/**
 * enum node_status_info - status of stream context
 * @NOT_FILLED: node not filled
 * @UPDATED_BY_DRIVER: updated by driver
 * @READ_BY_IP: read by IP
 * @USED_BY_IP: used by IP
 * @NODE_INVALID: invalid node
 */
enum node_status_info {
	NOT_FILLED = 0,
	UPDATED_BY_DRIVER,
	READ_BY_IP,
	USED_BY_IP,
	NODE_INVALID
};

/**
 * enum stream_errors - stream context error type
 * @NO_ERROR: no error
 * @PARTIAL_FRAME_WRITTEN: partial frame written
 * @DESCRIPTOR_NOT_READABLE: descriptor not readable
 */
enum stream_errors {
	NO_ERROR = 0,
	PARTIAL_FRAME_WRITTEN,
	DESCRIPTOR_NOT_READABLE
};

/**
 * struct strm_node - struct to describe stream node in linked list
 * @node_number: node number to handle streams
 * @node_status: status of stream node
 * @element: stream context info
 * @error_code: error codes
 * @reserved1: reserved bits for hardware align
 * @tail_pointer: physical address of next stream node in linked list
 * @strm_phy_addr: physical address of stream context
 * @node: struct of linked list head
 * @reserved2: reserved for hardware align
 */
struct stream_context_node {
	u32 node_number;
	enum node_status_info node_status;
	struct stream_context element;
	enum stream_errors error_code;
	u32 reserved1;
	u64 tail_pointer;
	u64 strm_phy_addr;
	struct list_head node;
	u64 reserved2;
};

/**
 * struct strm_info - struct to describe streamid node in streamid table
 * @pid: identification number of stream
 * @continuity_counter: counter to maintain packet count for a stream
 * @usageflag: flag to know free or under use for allocating streamid node
 * @strmtbl_update: struct to know enqueue or dequeue streamid in table
 */
struct stream_info {
	u16 pid;
	u8 continuity_counter;
	u8 usageflag;
	enum strmtbl_cnxt strmtbl_update;
};

/* Enum for error handling of mux context */
enum mux_op_errs {
	MUXER_NO_ERROR = 0,
	ERROR_OUTPUT_BUFFER_IS_NOT_ACCESIBLE,
	ERROR_PARTIAL_PACKET_WRITTEN
};

/**
 * struct muxer_context - struct to describe mux node in linked list
 * @node_status: status of mux node
 * @reserved: reserved for hardware align
 * @dst_buf_start_addr: physical address of dst buf
 * @dst_buf_size: size of the output buffer
 * @dst_buf_written: size of data written in dst buf
 * @num_of_pkts_written: number of packets in dst buf
 * @error_code: error status of mux node updated by IP
 * @mux_phy_addr: physical address of muxer
 * @node: struct of linked list head
 */
struct muxer_context {
	enum node_status_info node_status;
	u32 reserved;
	u64 dst_buf_start_addr;
	u32 dst_buf_size;
	u32 dst_buf_written;
	u32 num_of_pkts_written;
	enum mux_op_errs error_code;
	u64 mux_phy_addr;
	struct list_head node;
};

/**
 * struct xlnx_tsmux_dmabufintl - dma buf internal info
 * @dbuf: reference to a buffer's dmabuf struct
 * @attach: attachment to the buffer's dmabuf
 * @sgt: scatterlist info for the buffer's dmabuf
 * @dmabuf_addr: buffer physical address
 * @dmabuf_fd: dma buffer fd
 * @buf_id: dma buffer reference id
 */
struct xlnx_tsmux_dmabufintl {
	struct dma_buf *dbuf;
	struct dma_buf_attachment *attach;
	struct sg_table *sgt;
	dma_addr_t dmabuf_addr;
	s32 dmabuf_fd;
	u16 buf_id;
};

/**
 * struct xlnx_tsmux - xilinx mpeg2 TS muxer device
 * @dev: pointer to struct device instance used by the driver
 * @iomem: base address of the HW/IP
 * @chdev: char device handle
 * @user_count: count of users who have opened the device
 * @lock: spinlock to protect driver data structures
 * @waitq: wait queue used by the driver
 * @irq: irq number
 * @id: device instance ID
 * @num_inbuf: number of input buffers allocated uisng DMA
 * @num_outbuf: number of output buffers allocated uisng DMA
 * @srcbuf_size: size of each source buffer
 * @dstbuf_size: size of each destination buffer
 * @strm_node: list containing descriptors of stream context
 * @mux_node: list containing descriptors of mux context
 * @stcxt_node_cnt: stream number used for maintaing list
 * @num_strmnodes: number of stream nodes in the streamid table
 * @intn_stream_count: internal count of streams added to stream context
 * @outbuf_idx: index number to maintain output buffers
 * @srcbuf_addrs: physical address of source buffer
 * @dstbuf_addrs: physical address of destination buffer
 * @src_kaddrs: kernel VA for source buffer allocated by the driver
 * @dst_kaddrs: kernel VA for destination buffer allocated by the driver
 * @strm_ctx_pool: dma pool to allocate stream context buffers
 * @mux_ctx_pool: dma pool to allocate mux context buffers
 * @strmtbl_addrs: physical address of streamid table
 * @strmtbl_kaddrs: kernel VA for streamid table
 * @intn_strmtbl_addrs: physical address of streamid table for internal
 * @intn_strmtbl_kaddrs: kernel VA for streamid table for internal
 * @ap_clk: interface clock
 * @src_dmabufintl: array of src DMA buf allocated by user
 * @dst_dmabufintl: array of src DMA buf allocated by user
 * @outbuf_written: size in bytes written in output buffer
 * @stream_count: stream count
 */
struct xlnx_tsmux {
	struct device *dev;
	void __iomem *iomem;
	struct cdev chdev;
	atomic_t user_count;
	/* lock is used to protect access to sync_err and wdg_err */
	spinlock_t lock;
	wait_queue_head_t waitq;
	s32 irq;
	s32 id;
	u32 num_inbuf;
	u32 num_outbuf;
	size_t srcbuf_size;
	size_t dstbuf_size;
	struct list_head strm_node;
	struct list_head mux_node;
	u32 stcxt_node_cnt;
	u32 num_strmnodes;
	atomic_t intn_stream_count;
	atomic_t outbuf_idx;
	dma_addr_t srcbuf_addrs[XTSMUX_MAXIN_TLSTRM];
	dma_addr_t dstbuf_addrs[XTSMUX_MAXOUT_TLSTRM];
	void *src_kaddrs[XTSMUX_MAXIN_TLSTRM];
	void *dst_kaddrs[XTSMUX_MAXOUT_TLSTRM];
	struct dma_pool *strm_ctx_pool;
	struct dma_pool *mux_ctx_pool;
	dma_addr_t strmtbl_addrs;
	void *strmtbl_kaddrs;
	dma_addr_t intn_strmtbl_addrs;
	void *intn_strmtbl_kaddrs;
	struct clk *ap_clk;
	struct xlnx_tsmux_dmabufintl src_dmabufintl[XTSMUX_MAXIN_STRM];
	struct xlnx_tsmux_dmabufintl dst_dmabufintl[XTSMUX_MAXOUT_STRM];
	s32 outbuf_written;
	atomic_t stream_count;
};

static inline u32 xlnx_tsmux_read(const struct xlnx_tsmux *mpgmuxts,
				  const u32 reg)
{
	return ioread32(mpgmuxts->iomem + reg);
}

static inline void xlnx_tsmux_write(const struct xlnx_tsmux *mpgmuxts,
				    const u32 reg, const u32 val)
{
	iowrite32(val, (void __iomem *)(mpgmuxts->iomem + reg));
}

/* TODO: Optimize using iowrite64 call */
static inline void xlnx_tsmux_write64(const struct xlnx_tsmux *mpgmuxts,
				      const u32 reg, const u64 val)
{
	iowrite32(lower_32_bits(val), (void __iomem *)(mpgmuxts->iomem + reg));
	iowrite32(upper_32_bits(val), (void __iomem *)(mpgmuxts->iomem +
						       reg + 4));
}

static int xlnx_tsmux_start_muxer(struct xlnx_tsmux *mpgmuxts)
{
	struct stream_context_node *new_strm_node;
	struct muxer_context *new_mux_node;

	new_mux_node = list_first_entry_or_null(&mpgmuxts->mux_node,
						struct muxer_context, node);
	if (!new_mux_node)
		return -ENXIO;

	xlnx_tsmux_write64(mpgmuxts, XTSMUX_MUXCONTEXT_ADDR,
			   new_mux_node->mux_phy_addr);

	new_strm_node = list_first_entry_or_null(&mpgmuxts->strm_node,
						 struct stream_context_node,
						 node);
	if (!new_strm_node)
		return -ENXIO;

	xlnx_tsmux_write64(mpgmuxts, XTSMUX_STREAMCONTEXT_ADDR,
			   new_strm_node->strm_phy_addr);

	xlnx_tsmux_write(mpgmuxts, XTSMUX_NUM_DESC,
			 atomic_read(&mpgmuxts->intn_stream_count));

	xlnx_tsmux_write64(mpgmuxts, XTSMUX_STREAM_IDTBL_ADDR,
			   (u64)mpgmuxts->intn_strmtbl_addrs);
	xlnx_tsmux_write(mpgmuxts, XTSMUX_NUM_STREAM_IDTBL, 1);
	xlnx_tsmux_write(mpgmuxts, XTSMUX_GLBL_IER,
			 XTSMUX_GLBL_IER_ENABLE_MASK);
	xlnx_tsmux_write(mpgmuxts, XTSMUX_IER_STAT,
			 XTSMUX_IER_ENABLE_MASK);

	xlnx_tsmux_write(mpgmuxts, XTSMUX_RST_CTRL,
			 XTSMUX_RST_CTRL_START_MASK);

	return 0;
}

static void xlnx_tsmux_stop_muxer(const struct xlnx_tsmux *mpgmuxts)
{
	xlnx_tsmux_write(mpgmuxts, XTSMUX_GLBL_IER, 0);
	xlnx_tsmux_write(mpgmuxts, XTSMUX_IER_STAT, 0);
	xlnx_tsmux_write(mpgmuxts, XTSMUX_RST_CTRL, 0);
}

static enum xlnx_tsmux_status xlnx_tsmux_get_status(const struct
						    xlnx_tsmux * mpgmuxts)
{
	u32 status;

	status = xlnx_tsmux_read(mpgmuxts, XTSMUX_RST_CTRL);

	if (!status)
		return MPG2MUX_ERROR;

	if (status & XTSMUX_RST_CTRL_START_MASK)
		return MPG2MUX_BUSY;

	return MPG2MUX_READY;
}

static struct class *xlnx_tsmux_class;
static dev_t xlnx_tsmux_devt;
static atomic_t xlnx_tsmux_ndevs = ATOMIC_INIT(0);

static int xlnx_tsmux_open(struct inode *pin, struct file *fptr)
{
	struct xlnx_tsmux *mpgtsmux;

	mpgtsmux = container_of(pin->i_cdev, struct xlnx_tsmux, chdev);

	fptr->private_data = mpgtsmux;
	atomic_inc(&mpgtsmux->user_count);
	atomic_set(&mpgtsmux->outbuf_idx, 0);
	mpgtsmux->stcxt_node_cnt = 0;

	return 0;
}

static int xlnx_tsmux_release(struct inode *pin, struct file *fptr)
{
	struct xlnx_tsmux *mpgtsmux = (struct xlnx_tsmux *)fptr->private_data;

	if (!mpgtsmux)
		return -EIO;

	return 0;
}

/* TODO: Optimize buf alloc, dealloc API's to accommodate src, dst, strmtbl */
static int xlnx_tsmux_ioctl_srcbuf_dealloc(struct xlnx_tsmux *mpgmuxts)
{
	unsigned int i;

	for (i = 0; i < mpgmuxts->num_inbuf; i++) {
		if (!mpgmuxts->src_kaddrs[i] || !mpgmuxts->srcbuf_addrs[i])
			break;
		dma_free_coherent(mpgmuxts->dev, mpgmuxts->srcbuf_size,
				  mpgmuxts->src_kaddrs[i],
				  mpgmuxts->srcbuf_addrs[i]);
		mpgmuxts->src_kaddrs[i] = NULL;
	}

	return 0;
}

static int xlnx_tsmux_ioctl_srcbuf_alloc(struct xlnx_tsmux *mpgmuxts,
					 void __user *arg)
{
	int ret;
	unsigned int i;
	struct strc_bufs_info buf_data;

	ret = copy_from_user(&buf_data, arg, sizeof(struct strc_bufs_info));
	if (ret < 0) {
		dev_dbg(mpgmuxts->dev, "Failed to read input buffer info\n");
		return ret;
	}

	if (buf_data.num_buf > XTSMUX_MAXIN_PLSTRM) {
		dev_dbg(mpgmuxts->dev, "Excessive input payload. supported %d",
			XTSMUX_MAXIN_PLSTRM);
		return -EINVAL;
	}

	mpgmuxts->num_inbuf = buf_data.num_buf;
	mpgmuxts->srcbuf_size = buf_data.buf_size;
	/* buf_size & num_buf boundary conditions are handled in application
	 * and initial version of driver tested with 32-bit addressing only
	 */
	for (i = 0; i < mpgmuxts->num_inbuf; i++) {
		mpgmuxts->src_kaddrs[i] =
			dma_alloc_coherent(mpgmuxts->dev,
					    mpgmuxts->srcbuf_size,
					    &mpgmuxts->srcbuf_addrs[i],
					    GFP_KERNEL | GFP_DMA32);
		if (!mpgmuxts->src_kaddrs[i]) {
			dev_dbg(mpgmuxts->dev, "dma alloc fail %d buffer", i);
			goto exit_free;
		}
	}

	return 0;

exit_free:
	xlnx_tsmux_ioctl_srcbuf_dealloc(mpgmuxts);

	return -ENOMEM;
}

static int xlnx_tsmux_ioctl_dstbuf_dealloc(struct xlnx_tsmux *mpgmuxts)
{
	unsigned int i;

	for (i = 0; i < mpgmuxts->num_outbuf; i++) {
		if (!mpgmuxts->dst_kaddrs[i] || !mpgmuxts->dstbuf_addrs[i])
			break;
		dma_free_coherent(mpgmuxts->dev, mpgmuxts->dstbuf_size,
				  mpgmuxts->dst_kaddrs[i],
				  mpgmuxts->dstbuf_addrs[i]);
		mpgmuxts->dst_kaddrs[i] = NULL;
	}

	return 0;
}

static int xlnx_tsmux_ioctl_dstbuf_alloc(struct xlnx_tsmux *mpgmuxts,
					 void __user *arg)
{
	int ret;
	unsigned int i;
	struct strc_bufs_info buf_data;

	ret = copy_from_user(&buf_data, arg, sizeof(struct strc_bufs_info));
	if (ret < 0) {
		dev_dbg(mpgmuxts->dev, "%s: Failed to read output buffer info",
			__func__);
		return ret;
	}

	if (buf_data.num_buf > XTSMUX_MAXOUT_PLSTRM) {
		dev_dbg(mpgmuxts->dev, "Excessive output payload supported %d",
			XTSMUX_MAXOUT_PLSTRM);
		return -EINVAL;
	}

	mpgmuxts->num_outbuf = buf_data.num_buf;
	mpgmuxts->dstbuf_size = buf_data.buf_size;
	/* buf_size & num_buf boundary conditions are handled in application*/
	for (i = 0; i < mpgmuxts->num_outbuf; i++) {
		mpgmuxts->dst_kaddrs[i] =
			dma_alloc_coherent(mpgmuxts->dev,
					    mpgmuxts->dstbuf_size,
					    &mpgmuxts->dstbuf_addrs[i],
					    GFP_KERNEL | GFP_DMA32);
		if (!mpgmuxts->dst_kaddrs[i]) {
			dev_dbg(mpgmuxts->dev, "dmamem alloc fail for %d", i);
			goto exit_free;
		}
	}

	return 0;

exit_free:
	xlnx_tsmux_ioctl_dstbuf_dealloc(mpgmuxts);

	return -ENOMEM;
}

static int xlnx_tsmux_ioctl_strmtbl_dealloc(struct xlnx_tsmux *mpgmuxts)
{
	u32 buf_size;

	buf_size = sizeof(struct stream_info) * mpgmuxts->num_strmnodes;
	if (!mpgmuxts->strmtbl_kaddrs || !mpgmuxts->strmtbl_addrs)
		return 0;

	dma_free_coherent(mpgmuxts->dev, buf_size, mpgmuxts->strmtbl_kaddrs,
			  mpgmuxts->strmtbl_addrs);
	mpgmuxts->strmtbl_kaddrs = NULL;

	if (!mpgmuxts->intn_strmtbl_kaddrs || !mpgmuxts->intn_strmtbl_addrs)
		return 0;
	dma_free_coherent(mpgmuxts->dev, buf_size,
			  mpgmuxts->intn_strmtbl_kaddrs,
			  mpgmuxts->intn_strmtbl_addrs);
	mpgmuxts->intn_strmtbl_kaddrs = NULL;

	return 0;
}

static int xlnx_tsmux_ioctl_strmtbl_alloc(struct xlnx_tsmux *mpgmuxts,
					  void __user *arg)
{
	int ret, buf_size;
	u16 num_nodes;

	ret = copy_from_user(&num_nodes, arg, sizeof(u16));
	if (ret < 0) {
		dev_dbg(mpgmuxts->dev, "Failed to read streamid table info");
		return ret;
	}
	mpgmuxts->num_strmnodes = num_nodes;
	buf_size = sizeof(struct stream_info) * mpgmuxts->num_strmnodes;

	mpgmuxts->strmtbl_kaddrs =
		dma_alloc_coherent(mpgmuxts->dev,
				    buf_size, &mpgmuxts->strmtbl_addrs,
				    GFP_KERNEL | GFP_DMA32);
	if (!mpgmuxts->strmtbl_kaddrs) {
		dev_dbg(mpgmuxts->dev, "dmamem alloc fail for strm table");
		return -ENOMEM;
	}

	/* Allocating memory for internal streamid table */
	mpgmuxts->intn_strmtbl_kaddrs =
		dma_alloc_coherent(mpgmuxts->dev,
				    buf_size, &mpgmuxts->intn_strmtbl_addrs,
				    GFP_KERNEL | GFP_DMA32);

	if (!mpgmuxts->intn_strmtbl_kaddrs) {
		dev_dbg(mpgmuxts->dev, "dmamem alloc fail for intr strm table");
		goto exist_free;
	}

	return 0;
exist_free:
	xlnx_tsmux_ioctl_strmtbl_dealloc(mpgmuxts);

	return -ENOMEM;
}

static int xlnx_tsmux_update_strminfo_table(struct xlnx_tsmux *mpgmuxts,
					    struct strc_strminfo new_strm_info)
{
	u32 i = 0;
	struct stream_info *cptr;

	cptr = (struct stream_info *)mpgmuxts->strmtbl_kaddrs;

	if (new_strm_info.strmtbl_ctxt == ADD_TO_TBL) {
	/* Finding free memory block and writing input data into the block*/
		for (i = 0; i < mpgmuxts->num_strmnodes; i++, cptr++) {
			if (!cptr->usageflag) {
				cptr->pid = new_strm_info.pid;
				cptr->continuity_counter = 0;
				cptr->usageflag = XTSMUX_STRMBL_BUSY;
				break;
			}
		}
	} else if (new_strm_info.strmtbl_ctxt == DEL_FR_TBL) {
		for (i = 0; i < mpgmuxts->num_strmnodes; i++, cptr++) {
			if (cptr->pid == new_strm_info.pid) {
				cptr->usageflag = XTSMUX_STRMBL_FREE;
				break;
			}
		}
	}

	if (i == mpgmuxts->num_strmnodes)
		return -EIO;

	return 0;
}

static int xlnx_tsmux_ioctl_update_strmtbl(struct xlnx_tsmux *mpgmuxts,
					   void __user *arg)
{
	int ret;
	struct strc_strminfo new_strm_info;

	ret = copy_from_user(&new_strm_info, arg, sizeof(struct strc_strminfo));
	if (ret < 0) {
		dev_dbg(mpgmuxts->dev, "Reading strmInfo failed");
		return ret;
	}

	return xlnx_tsmux_update_strminfo_table(mpgmuxts, new_strm_info);
}

static int xlnx_tsmux_enqueue_stream_context(struct xlnx_tsmux *mpgmuxts,
					     struct
					     stream_context_in * stream_data)
{
	struct stream_context_node *new_strm_node, *prev_strm_node;
	void *kaddr_strm_node;
	dma_addr_t strm_phy_addr;
	unsigned long flags;
	u32 i;

	kaddr_strm_node = dma_pool_alloc(mpgmuxts->strm_ctx_pool,
					 GFP_KERNEL | GFP_DMA32,
					 &strm_phy_addr);

	new_strm_node = (struct stream_context_node *)kaddr_strm_node;
	if (!new_strm_node)
		return -ENOMEM;

	/* update the stream context node */
	wmb();
	new_strm_node->element.command = stream_data->command;
	new_strm_node->element.is_pcr_stream = stream_data->is_pcr_stream;
	new_strm_node->element.stream_id = stream_data->stream_id;
	new_strm_node->element.extended_stream_id =
				stream_data->extended_stream_id;
	new_strm_node->element.pid = stream_data->pid;
	new_strm_node->element.size_data_in = stream_data->size_data_in;
	new_strm_node->element.pts = stream_data->pts;
	new_strm_node->element.dts = stream_data->dts;
	new_strm_node->element.insert_pcr = stream_data->insert_pcr;
	new_strm_node->element.pcr_base = stream_data->pcr_base;
	new_strm_node->element.pcr_extension = stream_data->pcr_extension;

	/* Check for external dma buffer */
	if (!stream_data->is_dmabuf) {
		new_strm_node->element.in_buf_pointer =
			mpgmuxts->srcbuf_addrs[stream_data->srcbuf_id];
		new_strm_node->element.dmabuf_id = 0;
	} else {
		for (i = 0; i < XTSMUX_MAXIN_STRM; i++) {
			/* Serching dma buf info based on srcbuf_id */
			if (stream_data->srcbuf_id ==
					mpgmuxts->src_dmabufintl[i].dmabuf_fd) {
				new_strm_node->element.in_buf_pointer =
					mpgmuxts->src_dmabufintl[i].dmabuf_addr;
				new_strm_node->element.dmabuf_id =
					mpgmuxts->src_dmabufintl[i].buf_id;
				break;
			}
		}

		/* No dma buf found with srcbuf_id*/
		if (i == XTSMUX_MAXIN_STRM) {
			dev_err(mpgmuxts->dev, "No DMA buffer with %d",
				stream_data->srcbuf_id);
			return -ENOMEM;
		}
	}

	new_strm_node->strm_phy_addr = (u64)strm_phy_addr;
	new_strm_node->node_number = mpgmuxts->stcxt_node_cnt + 1;
	mpgmuxts->stcxt_node_cnt++;
	new_strm_node->node_status = UPDATED_BY_DRIVER;
	new_strm_node->error_code = NO_ERROR;
	new_strm_node->tail_pointer = 0;

	spin_lock_irqsave(&mpgmuxts->lock, flags);
	/* If it is not first stream in stream node linked list find
	 * physical address of current node and add to last node in list
	 */
	if (!list_empty_careful(&mpgmuxts->strm_node)) {
		prev_strm_node = list_last_entry(&mpgmuxts->strm_node,
						 struct stream_context_node,
						 node);
		prev_strm_node->tail_pointer = new_strm_node->strm_phy_addr;
	}
	/* update the list and stream count */
	wmb();
	list_add_tail(&new_strm_node->node, &mpgmuxts->strm_node);
	atomic_inc(&mpgmuxts->stream_count);
	spin_unlock_irqrestore(&mpgmuxts->lock, flags);

	return 0;
}

static int xlnx_tsmux_set_stream_desc(struct xlnx_tsmux *mpgmuxts,
				      void __user *arg)
{
	struct stream_context_in *stream_data;
	int ret = 0;

	stream_data = kzalloc(sizeof(*stream_data), GFP_KERNEL);
	if (!stream_data)
		return -ENOMEM;

	ret = copy_from_user(stream_data, arg,
			     sizeof(struct stream_context_in));
	if (ret) {
		dev_err(mpgmuxts->dev, "Failed to copy stream data from user");
		goto error_free;
	}

	ret = xlnx_tsmux_enqueue_stream_context(mpgmuxts, stream_data);

error_free:
	kfree(stream_data);

	return ret;
}

static int xlnx_tsmux_ioctl_set_stream_context(struct xlnx_tsmux *mpgmuxts,
					       void __user *arg)
{
	int ret;

	ret = xlnx_tsmux_set_stream_desc(mpgmuxts, arg);
	if (ret < 0) {
		dev_err(mpgmuxts->dev, "Setting stream descripter failed");
		return ret;
	}

	return 0;
}

static enum xlnx_tsmux_status xlnx_tsmux_get_device_status(struct xlnx_tsmux *
							   mpgmuxts)
{
	enum xlnx_tsmux_status ip_status;

	ip_status = xlnx_tsmux_get_status(mpgmuxts);

	if (ip_status == MPG2MUX_ERROR) {
		dev_err(mpgmuxts->dev, "Failed to get device status");
		return -EACCES;
	}

	if (ip_status == MPG2MUX_BUSY)
		return -EBUSY;

	return MPG2MUX_READY;
}

static int xlnx_tsmux_ioctl_start(struct xlnx_tsmux *mpgmuxts)
{
	enum xlnx_tsmux_status ip_stat;
	int cnt;

	/* get IP status */
	ip_stat = xlnx_tsmux_get_device_status(mpgmuxts);
	if (ip_stat != MPG2MUX_READY) {
		dev_err(mpgmuxts->dev, "device is busy");
		return ip_stat;
	}

	if (list_empty(&mpgmuxts->mux_node) ||
	    list_empty(&mpgmuxts->strm_node)) {
		dev_err(mpgmuxts->dev, "No stream or mux to start device");
		return -EIO;
	}

	cnt = atomic_read(&mpgmuxts->stream_count);
	atomic_set(&mpgmuxts->intn_stream_count, cnt);

	return xlnx_tsmux_start_muxer(mpgmuxts);
}

static void xlnx_tsmux_free_dmalloc(struct xlnx_tsmux *mpgmuxts)
{
	dma_pool_destroy(mpgmuxts->strm_ctx_pool);
	dma_pool_destroy(mpgmuxts->mux_ctx_pool);
}

static int xlnx_tsmux_ioctl_stop(struct xlnx_tsmux *mpgmuxts)
{
	enum xlnx_tsmux_status ip_stat;
	unsigned long flags;

	ip_stat = xlnx_tsmux_get_device_status(mpgmuxts);
	if (ip_stat != MPG2MUX_READY) {
		dev_err(mpgmuxts->dev, "device is busy");
		return ip_stat;
	}

	/* Free all driver allocated memory and reset linked list
	 * Reset IP registers
	 */
	xlnx_tsmux_free_dmalloc(mpgmuxts);
	spin_lock_irqsave(&mpgmuxts->lock, flags);
	INIT_LIST_HEAD(&mpgmuxts->strm_node);
	INIT_LIST_HEAD(&mpgmuxts->mux_node);
	spin_unlock_irqrestore(&mpgmuxts->lock, flags);
	xlnx_tsmux_stop_muxer(mpgmuxts);

	return 0;
}

static int xlnx_tsmux_ioctl_get_status(struct xlnx_tsmux *mpgmuxts,
				       void __user *arg)
{
	int ret;
	enum xlnx_tsmux_status ip_stat;

	ip_stat = xlnx_tsmux_get_device_status(mpgmuxts);

	ret = copy_to_user(arg, (void *)&ip_stat,
			   (unsigned long)(sizeof(enum xlnx_tsmux_status)));
	if (ret) {
		dev_err(mpgmuxts->dev, "Unable to copy device status to user");
		return -EACCES;
	}

	return 0;
}

static int xlnx_tsmux_ioctl_get_outbufinfo(struct xlnx_tsmux *mpgmuxts,
					   void __user *arg)
{
	int ret;
	int out_index;
	struct out_buffer out_info;

	out_info.buf_write = mpgmuxts->outbuf_written;
	mpgmuxts->outbuf_written = 0;
	out_index = atomic_read(&mpgmuxts->outbuf_idx);
	if (out_index)
		out_info.buf_id = 0;
	else
		out_info.buf_id = 1;

	ret = copy_to_user(arg, (void *)&out_info,
			   (unsigned long)(sizeof(struct out_buffer)));
	if (ret) {
		dev_err(mpgmuxts->dev, "Unable to copy outbuf info");
		return -EACCES;
	}

	return 0;
}

static int xlnx_tsmux_enqueue_mux_context(struct xlnx_tsmux *mpgmuxts,
					  struct muxer_context_in *mux_data)
{
	struct muxer_context *new_mux_node;
	u32 out_index;
	void *kaddr_mux_node;
	dma_addr_t mux_phy_addr;
	unsigned long flags;
	s32 i;

	kaddr_mux_node = dma_pool_alloc(mpgmuxts->mux_ctx_pool,
					GFP_KERNEL | GFP_DMA32,
					&mux_phy_addr);

	new_mux_node = (struct muxer_context *)kaddr_mux_node;
	if (!new_mux_node)
		return -EAGAIN;

	new_mux_node->node_status = UPDATED_BY_DRIVER;
	new_mux_node->mux_phy_addr = (u64)mux_phy_addr;

	/* Check for external dma buffer */
	if (!mux_data->is_dmabuf) {
		out_index = 0;
		new_mux_node->dst_buf_start_addr =
			(u64)mpgmuxts->dstbuf_addrs[out_index];
		new_mux_node->dst_buf_size = mpgmuxts->dstbuf_size;
		if (out_index)
			atomic_set(&mpgmuxts->outbuf_idx, 0);
		else
			atomic_set(&mpgmuxts->outbuf_idx, 1);
	} else {
		for (i = 0; i < XTSMUX_MAXOUT_STRM; i++) {
			if (mux_data->dstbuf_id ==
			   mpgmuxts->dst_dmabufintl[i].dmabuf_fd) {
				new_mux_node->dst_buf_start_addr =
					mpgmuxts->dst_dmabufintl[i].dmabuf_addr;
				break;
			}
		}
		if (i == XTSMUX_MAXOUT_STRM) {
			dev_err(mpgmuxts->dev, "No DMA buffer with %d",
				mux_data->dstbuf_id);
			return -ENOMEM;
		}
		new_mux_node->dst_buf_size = mux_data->dmabuf_size;
	}
	new_mux_node->error_code = MUXER_NO_ERROR;

	spin_lock_irqsave(&mpgmuxts->lock, flags);
	list_add_tail(&new_mux_node->node, &mpgmuxts->mux_node);
	spin_unlock_irqrestore(&mpgmuxts->lock, flags);

	return 0;
}

static int xlnx_tsmux_set_mux_desc(struct xlnx_tsmux *mpgmuxts,
				   void __user *arg)
{
	struct muxer_context_in *mux_data;
	int ret = 0;

	mux_data = kzalloc(sizeof(*mux_data), GFP_KERNEL);
	if (!mux_data)
		return -ENOMEM;

	ret = copy_from_user(mux_data, arg,
			     sizeof(struct muxer_context_in));
	if (ret) {
		dev_err(mpgmuxts->dev, "failed to copy muxer data from user");
		goto kmem_free;
	}

	return xlnx_tsmux_enqueue_mux_context(mpgmuxts, mux_data);

kmem_free:
	kfree(mux_data);

	return ret;
}

static int xlnx_tsmux_ioctl_set_mux_context(struct xlnx_tsmux *mpgmuxts,
					    void __user *arg)
{
	int ret;

	ret = xlnx_tsmux_set_mux_desc(mpgmuxts, arg);
	if (ret < 0)
		dev_dbg(mpgmuxts->dev, "Setting mux context failed");

	return ret;
}

static int xlnx_tsmux_ioctl_verify_dmabuf(struct xlnx_tsmux *mpgmuxts,
					  void __user *arg)
{
	struct dma_buf *dbuf;
	struct dma_buf_attachment *attach;
	struct sg_table *sgt;
	struct xlnx_tsmux_dmabuf_info *dbuf_info;
	s32 i;
	int ret = 0;

	dbuf_info = kzalloc(sizeof(*dbuf_info), GFP_KERNEL);
	if (!dbuf_info)
		return -ENOMEM;

	ret = copy_from_user(dbuf_info, arg,
			     sizeof(struct xlnx_tsmux_dmabuf_info));
	if (ret) {
		dev_err(mpgmuxts->dev, "Failed to copy from user");
		goto dmak_free;
	}
	if (dbuf_info->dir != DMA_TO_MPG2MUX &&
	    dbuf_info->dir != DMA_FROM_MPG2MUX) {
		dev_err(mpgmuxts->dev, "Incorrect DMABUF direction %d",
			dbuf_info->dir);
		ret = -EINVAL;
		goto dmak_free;
	}
	dbuf = dma_buf_get(dbuf_info->buf_fd);
	if (IS_ERR(dbuf)) {
		dev_err(mpgmuxts->dev, "dma_buf_get fail fd %d direction %d",
			dbuf_info->buf_fd, dbuf_info->dir);
		ret = PTR_ERR(dbuf);
		goto dmak_free;
	}
	attach = dma_buf_attach(dbuf, mpgmuxts->dev);
	if (IS_ERR(attach)) {
		dev_err(mpgmuxts->dev, "dma_buf_attach fail fd %d dir %d",
			dbuf_info->buf_fd, dbuf_info->dir);
		ret = PTR_ERR(attach);
		goto err_dmabuf_put;
	}
	sgt = dma_buf_map_attachment(attach,
				     (enum dma_data_direction)(dbuf_info->dir));
	if (IS_ERR(sgt)) {
		dev_err(mpgmuxts->dev, "dma_buf_map_attach fail fd %d dir %d",
			dbuf_info->buf_fd, dbuf_info->dir);
		ret = PTR_ERR(sgt);
		goto err_dmabuf_detach;
	}

	if (sgt->nents > 1) {
		ret = -EIO;
		dev_dbg(mpgmuxts->dev, "Not contig nents %d fd %d direction %d",
			sgt->nents, dbuf_info->buf_fd, dbuf_info->dir);
		goto err_dmabuf_unmap_attachment;
	}
	dev_dbg(mpgmuxts->dev, "dmabuf %s is physically contiguous",
		(dbuf_info->dir ==
		 DMA_TO_MPG2MUX ? "Source" : "Destination"));

	if (dbuf_info->dir == DMA_TO_MPG2MUX) {
		for (i = 0; i < XTSMUX_MAXIN_STRM; i++) {
			if (!mpgmuxts->src_dmabufintl[i].buf_id) {
				mpgmuxts->src_dmabufintl[i].dbuf = dbuf;
				mpgmuxts->src_dmabufintl[i].attach = attach;
				mpgmuxts->src_dmabufintl[i].sgt = sgt;
				mpgmuxts->src_dmabufintl[i].dmabuf_addr =
						sg_dma_address(sgt->sgl);
				mpgmuxts->src_dmabufintl[i].dmabuf_fd =
							dbuf_info->buf_fd;
				mpgmuxts->src_dmabufintl[i].buf_id = i + 1;
				dev_dbg(mpgmuxts->dev,
					"%s: phy-addr=0x%llx for src dmabuf=%d",
					__func__,
					mpgmuxts->src_dmabufintl[i].dmabuf_addr,
					mpgmuxts->src_dmabufintl[i].dmabuf_fd);
				break;
			}
		}
		/* External src streams more than XTSMUX_MAXIN_STRM
		 * can not be handled
		 */
		if (i == XTSMUX_MAXIN_STRM) {
			ret = -EIO;
			dev_dbg(mpgmuxts->dev, "src DMA bufs more than %d",
				XTSMUX_MAXIN_STRM);
			goto err_dmabuf_unmap_attachment;
		}
	} else {
		for (i = 0; i < XTSMUX_MAXOUT_STRM; i++) {
			if (!mpgmuxts->dst_dmabufintl[i].buf_id) {
				mpgmuxts->dst_dmabufintl[i].dbuf = dbuf;
				mpgmuxts->dst_dmabufintl[i].attach = attach;
				mpgmuxts->dst_dmabufintl[i].sgt = sgt;
				mpgmuxts->dst_dmabufintl[i].dmabuf_addr =
						sg_dma_address(sgt->sgl);
				mpgmuxts->dst_dmabufintl[i].dmabuf_fd =
						dbuf_info->buf_fd;
				mpgmuxts->dst_dmabufintl[i].buf_id = i + 1;
				dev_dbg(mpgmuxts->dev,
					"phy-addr=0x%llx for src dmabuf=%d",
					mpgmuxts->dst_dmabufintl[i].dmabuf_addr,
					mpgmuxts->dst_dmabufintl[i].dmabuf_fd);
				break;
			}
		}
		/* External dst streams more than XTSMUX_MAXOUT_STRM
		 * can not be handled
		 */
		if (i == XTSMUX_MAXOUT_STRM) {
			ret = -EIO;
			dev_dbg(mpgmuxts->dev, "dst DMA bufs more than %d",
				XTSMUX_MAXOUT_STRM);
			goto err_dmabuf_unmap_attachment;
		}
	}

	return 0;

err_dmabuf_unmap_attachment:
	dma_buf_unmap_attachment(attach, sgt,
				 (enum dma_data_direction)dbuf_info->dir);
err_dmabuf_detach:
	dma_buf_detach(dbuf, attach);
err_dmabuf_put:
	dma_buf_put(dbuf);
dmak_free:
	kfree(dbuf_info);

	return ret;
}

static long xlnx_tsmux_ioctl(struct file *fptr,
			     unsigned int cmd, unsigned long data)
{
	struct xlnx_tsmux *mpgmuxts;
	void __user *arg;
	int ret;

	mpgmuxts = fptr->private_data;
	if (!mpgmuxts)
		return -EINVAL;

	arg = (void __user *)data;
	switch (cmd) {
	case MPG2MUX_INBUFALLOC:
		ret = xlnx_tsmux_ioctl_srcbuf_alloc(mpgmuxts, arg);
		break;
	case MPG2MUX_INBUFDEALLOC:
		ret = xlnx_tsmux_ioctl_srcbuf_dealloc(mpgmuxts);
		break;
	case MPG2MUX_OUTBUFALLOC:
		ret = xlnx_tsmux_ioctl_dstbuf_alloc(mpgmuxts, arg);
		break;
	case MPG2MUX_OUTBUFDEALLOC:
		ret = xlnx_tsmux_ioctl_dstbuf_dealloc(mpgmuxts);
		break;
	case MPG2MUX_STBLALLOC:
		ret = xlnx_tsmux_ioctl_strmtbl_alloc(mpgmuxts, arg);
		break;
	case MPG2MUX_STBLDEALLOC:
		ret = xlnx_tsmux_ioctl_strmtbl_dealloc(mpgmuxts);
		break;
	case MPG2MUX_TBLUPDATE:
		ret = xlnx_tsmux_ioctl_update_strmtbl(mpgmuxts, arg);
		break;
	case MPG2MUX_SETSTRM:
		ret = xlnx_tsmux_ioctl_set_stream_context(mpgmuxts, arg);
		break;
	case MPG2MUX_START:
		ret = xlnx_tsmux_ioctl_start(mpgmuxts);
		break;
	case MPG2MUX_STOP:
		ret = xlnx_tsmux_ioctl_stop(mpgmuxts);
		break;
	case MPG2MUX_STATUS:
		ret = xlnx_tsmux_ioctl_get_status(mpgmuxts, arg);
		break;
	case MPG2MUX_GETOUTBUF:
		ret = xlnx_tsmux_ioctl_get_outbufinfo(mpgmuxts, arg);
		break;
	case MPG2MUX_SETMUX:
		ret = xlnx_tsmux_ioctl_set_mux_context(mpgmuxts, arg);
		break;
	case MPG2MUX_VDBUF:
		ret = xlnx_tsmux_ioctl_verify_dmabuf(mpgmuxts, arg);
		break;
	default:
		return -EINVAL;
	}
	if (ret < 0)
		dev_err(mpgmuxts->dev, "ioctl %d failed\n", cmd);

	return ret;
}

static int xlnx_tsmux_mmap(struct file *fp, struct vm_area_struct *vma)
{
	struct xlnx_tsmux *mpgmuxts = fp->private_data;
	int ret, buf_id;

	if (!mpgmuxts)
		return -ENODEV;

	buf_id = vma->vm_pgoff;

	if (buf_id < mpgmuxts->num_inbuf) {
		if (!mpgmuxts->srcbuf_addrs[buf_id]) {
			dev_err(mpgmuxts->dev, "Mem not allocated for src %d",
				buf_id);
			return -EINVAL;
		}
		vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
		ret = remap_pfn_range(vma, vma->vm_start,
				      mpgmuxts->srcbuf_addrs[buf_id] >>
				      PAGE_SHIFT, vma->vm_end - vma->vm_start,
				      vma->vm_page_prot);
		if (ret) {
			dev_err(mpgmuxts->dev, "mmap fail bufid = %d", buf_id);
			return -EINVAL;
		}
	} else if (buf_id < (mpgmuxts->num_inbuf + mpgmuxts->num_outbuf)) {
		buf_id -= mpgmuxts->num_inbuf;
		if (!mpgmuxts->dstbuf_addrs[buf_id]) {
			dev_err(mpgmuxts->dev, "Mem not allocated fordst %d",
				buf_id);
			return -EINVAL;
		}
		vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
		ret =
		remap_pfn_range(vma, vma->vm_start,
				mpgmuxts->dstbuf_addrs[buf_id] >> PAGE_SHIFT,
				vma->vm_end - vma->vm_start, vma->vm_page_prot);
		if (ret) {
			dev_err(mpgmuxts->dev, "mmap fail buf_id = %d", buf_id);
			ret = -EINVAL;
		}
	} else {
		dev_err(mpgmuxts->dev, "Wrong buffer id -> %d buf", buf_id);
		return -EINVAL;
	}
	fp->private_data = mpgmuxts;
	return 0;
}

static __poll_t xlnx_tsmux_poll(struct file *fptr, poll_table *wait)
{
	struct xlnx_tsmux *mpgmuxts = fptr->private_data;

	poll_wait(fptr, &mpgmuxts->waitq, wait);

	if (xlnx_tsmux_read(mpgmuxts, XTSMUX_LAST_NODE_PROCESSED))
		return EPOLLIN | EPOLLPRI;

	return 0;
}

static const struct file_operations mpg2mux_fops = {
	.open = xlnx_tsmux_open,
	.release = xlnx_tsmux_release,
	.unlocked_ioctl = xlnx_tsmux_ioctl,
	.mmap = xlnx_tsmux_mmap,
	.poll = xlnx_tsmux_poll,
};

static void xlnx_tsmux_free_dmabufintl(struct xlnx_tsmux_dmabufintl
				       *intl_dmabuf, u16 dmabuf_id,
				       enum xlnx_tsmux_dma_dir dir)
{
	unsigned int i = dmabuf_id - 1;

	if (intl_dmabuf[i].dmabuf_fd) {
		dma_buf_unmap_attachment(intl_dmabuf[i].attach,
					 intl_dmabuf[i].sgt,
					 (enum dma_data_direction)dir);
		dma_buf_detach(intl_dmabuf[i].dbuf, intl_dmabuf[i].attach);
		dma_buf_put(intl_dmabuf[i].dbuf);
		intl_dmabuf[i].dmabuf_fd = 0;
		intl_dmabuf[i].buf_id = 0;
	}
}

static int xlnx_tsmux_update_complete(struct xlnx_tsmux *mpgmuxts)
{
	struct stream_context_node *tstrm_node;
	struct muxer_context *temp_mux;
	u32 num_strm_node, i;
	u32 num_strms;
	unsigned long flags;

	num_strm_node = xlnx_tsmux_read(mpgmuxts, XTSMUX_LAST_NODE_PROCESSED);
	if (num_strm_node == 0)
		return -1;

	/* Removing completed stream nodes from the list  */
	spin_lock_irqsave(&mpgmuxts->lock, flags);
	num_strms = atomic_read(&mpgmuxts->intn_stream_count);
	for (i = 0; i < num_strms; i++) {
		tstrm_node =
			list_first_entry(&mpgmuxts->strm_node,
					 struct stream_context_node, node);
		list_del(&tstrm_node->node);
		atomic_dec(&mpgmuxts->stream_count);
		if (tstrm_node->element.dmabuf_id)
			xlnx_tsmux_free_dmabufintl
				(mpgmuxts->src_dmabufintl,
				 tstrm_node->element.dmabuf_id,
				 DMA_TO_MPG2MUX);
		if (tstrm_node->node_number == num_strm_node) {
			dma_pool_free(mpgmuxts->strm_ctx_pool, tstrm_node,
				      tstrm_node->strm_phy_addr);
			break;
		}
	}

	/* Removing completed mux nodes from the list  */
	temp_mux = list_first_entry(&mpgmuxts->mux_node, struct muxer_context,
				    node);
	mpgmuxts->outbuf_written = temp_mux->dst_buf_written;

	list_del(&temp_mux->node);
	spin_unlock_irqrestore(&mpgmuxts->lock, flags);

	return 0;
}

static irqreturn_t xlnx_tsmux_intr_handler(int irq, void *ctx)
{
	u32 status;
	struct xlnx_tsmux *mpgmuxts = (struct xlnx_tsmux *)ctx;

	status = xlnx_tsmux_read(mpgmuxts, XTSMUX_ISR_STAT);
	status &= XTSMUX_IER_ENABLE_MASK;

	if (status) {
		xlnx_tsmux_write(mpgmuxts, XTSMUX_ISR_STAT, status);
		xlnx_tsmux_update_complete(mpgmuxts);
		if (mpgmuxts->outbuf_written)
			wake_up_interruptible(&mpgmuxts->waitq);
		return IRQ_HANDLED;
	}

	return IRQ_NONE;
}

static int xlnx_tsmux_probe(struct platform_device *pdev)
{
	struct xlnx_tsmux *mpgmuxts;
	struct device *dev = &pdev->dev;
	struct device *dev_crt;
	struct resource *dev_resrc;
	int ret = -1;
	unsigned long flags;

	/* DRIVER_MAX_DEV is to limit the number of instances, but
	 * Initial version is tested with single instance only.
	 * TODO: replace atomic_read with ida_simple_get
	 */
	if (atomic_read(&xlnx_tsmux_ndevs) >= DRIVER_MAX_DEV) {
		dev_err(&pdev->dev, "Limit of %d number of device is reached",
			DRIVER_MAX_DEV);
		return -EIO;
	}

	mpgmuxts = devm_kzalloc(&pdev->dev, sizeof(struct xlnx_tsmux),
				GFP_KERNEL);
	if (!mpgmuxts)
		return -ENOMEM;
	mpgmuxts->dev = &pdev->dev;
	dev_resrc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	mpgmuxts->iomem = devm_ioremap_resource(mpgmuxts->dev, dev_resrc);
	if (IS_ERR(mpgmuxts->iomem))
		return PTR_ERR(mpgmuxts->iomem);

	mpgmuxts->irq = irq_of_parse_and_map(mpgmuxts->dev->of_node, 0);
	if (!mpgmuxts->irq) {
		dev_err(mpgmuxts->dev, "Unable to get IRQ");
		return -EINVAL;
	}

	mpgmuxts->ap_clk = devm_clk_get(dev, "ap_clk");
	if (IS_ERR(mpgmuxts->ap_clk)) {
		ret = PTR_ERR(mpgmuxts->ap_clk);
		dev_err(dev, "failed to get ap clk %d\n", ret);
		goto cdev_err;
	}
	ret = clk_prepare_enable(mpgmuxts->ap_clk);
	if (ret) {
		dev_err(dev, "failed to enable ap clk %d\n", ret);
		goto err_disable_ap_clk;
	}

	/* Initializing variables used in Muxer */
	spin_lock_irqsave(&mpgmuxts->lock, flags);
	INIT_LIST_HEAD(&mpgmuxts->strm_node);
	INIT_LIST_HEAD(&mpgmuxts->mux_node);
	spin_unlock_irqrestore(&mpgmuxts->lock, flags);
	mpgmuxts->strm_ctx_pool = dma_pool_create("strcxt_pool", mpgmuxts->dev,
						  XTSMUX_POOL_SIZE,
						  XTSMUX_POOL_ALIGN,
						  XTSMUX_POOL_SIZE *
						  XTSMUX_MAXIN_TLSTRM);
	if (!mpgmuxts->strm_ctx_pool) {
		dev_err(mpgmuxts->dev, "Allocation fail for strm ctx pool");
		return -ENOMEM;
	}

	mpgmuxts->mux_ctx_pool = dma_pool_create("muxcxt_pool", mpgmuxts->dev,
						 XTSMUX_POOL_SIZE,
						 XTSMUX_POOL_SIZE,
						 XTSMUX_POOL_SIZE *
						 XTSMUX_MAXIN_TLSTRM);

	if (!mpgmuxts->mux_ctx_pool) {
		dev_err(mpgmuxts->dev, "Allocation fail for mux ctx pool");
		goto mux_err;
	}

	init_waitqueue_head(&mpgmuxts->waitq);

	ret = devm_request_irq(mpgmuxts->dev, mpgmuxts->irq,
			       xlnx_tsmux_intr_handler, IRQF_SHARED,
			       DRIVER_NAME, mpgmuxts);

	if (ret < 0) {
		dev_err(mpgmuxts->dev, "Unable to register IRQ");
		goto mux_err;
	}

	cdev_init(&mpgmuxts->chdev, &mpg2mux_fops);
	mpgmuxts->chdev.owner = THIS_MODULE;
	mpgmuxts->id = atomic_read(&xlnx_tsmux_ndevs);
	ret = cdev_add(&mpgmuxts->chdev, MKDEV(MAJOR(xlnx_tsmux_devt),
					       mpgmuxts->id), 1);

	if (ret < 0) {
		dev_err(mpgmuxts->dev, "cdev_add failed");
		goto cadd_err;
	}

	dev_crt = device_create(xlnx_tsmux_class, mpgmuxts->dev,
				MKDEV(MAJOR(xlnx_tsmux_devt), mpgmuxts->id),
				mpgmuxts, "mpgmuxts%d", mpgmuxts->id);

	if (IS_ERR(dev_crt)) {
		ret = PTR_ERR(dev_crt);
		dev_err(mpgmuxts->dev, "Unable to create device");
		goto cdev_err;
	}

	dev_info(mpgmuxts->dev,
		 "Xilinx mpeg2 TS muxer device probe completed");

	atomic_inc(&xlnx_tsmux_ndevs);

	return 0;

err_disable_ap_clk:
	clk_disable_unprepare(mpgmuxts->ap_clk);
cdev_err:
	cdev_del(&mpgmuxts->chdev);
	device_destroy(xlnx_tsmux_class, MKDEV(MAJOR(xlnx_tsmux_devt),
					       mpgmuxts->id));
cadd_err:
	dma_pool_destroy(mpgmuxts->mux_ctx_pool);
mux_err:
	dma_pool_destroy(mpgmuxts->strm_ctx_pool);

	return ret;
}

static int xlnx_tsmux_remove(struct platform_device *pdev)
{
	struct xlnx_tsmux *mpgmuxts;

	mpgmuxts = platform_get_drvdata(pdev);
	if (!mpgmuxts || !xlnx_tsmux_class)
		return -EIO;
	dma_pool_destroy(mpgmuxts->mux_ctx_pool);
	dma_pool_destroy(mpgmuxts->strm_ctx_pool);

	device_destroy(xlnx_tsmux_class, MKDEV(MAJOR(xlnx_tsmux_devt),
					       mpgmuxts->id));
	cdev_del(&mpgmuxts->chdev);
	atomic_dec(&xlnx_tsmux_ndevs);
	clk_disable_unprepare(mpgmuxts->ap_clk);

	return 0;
}

static const struct of_device_id xlnx_tsmux_of_match[] = {
	{ .compatible = "xlnx,tsmux-1.0", },
	{ }
};

static struct platform_driver xlnx_tsmux_driver = {
	.probe = xlnx_tsmux_probe,
	.remove = xlnx_tsmux_remove,
	.driver = {
		.name = DRIVER_NAME,
		.of_match_table = xlnx_tsmux_of_match,
	},
};

static int __init xlnx_tsmux_mod_init(void)
{
	int err;

	xlnx_tsmux_class = class_create(THIS_MODULE, DRIVER_NAME);
	if (IS_ERR(xlnx_tsmux_class)) {
		pr_err("%s : Unable to create driver class", __func__);
		return PTR_ERR(xlnx_tsmux_class);
	}

	err = alloc_chrdev_region(&xlnx_tsmux_devt, 0, DRIVER_MAX_DEV,
				  DRIVER_NAME);
	if (err < 0) {
		pr_err("%s : Unable to get major number", __func__);
		goto err_class;
	}

	err = platform_driver_register(&xlnx_tsmux_driver);
	if (err < 0) {
		pr_err("%s : Unable to register %s driver", __func__,
		       DRIVER_NAME);
		goto err_driver;
	}

	return 0;

err_driver:
	unregister_chrdev_region(xlnx_tsmux_devt, DRIVER_MAX_DEV);
err_class:
	class_destroy(xlnx_tsmux_class);

	return err;
}

static void __exit xlnx_tsmux_mod_exit(void)
{
	platform_driver_unregister(&xlnx_tsmux_driver);
	unregister_chrdev_region(xlnx_tsmux_devt, DRIVER_MAX_DEV);
	class_destroy(xlnx_tsmux_class);
	xlnx_tsmux_class = NULL;
}

module_init(xlnx_tsmux_mod_init);
module_exit(xlnx_tsmux_mod_exit);

MODULE_AUTHOR("Xilinx Inc.");
MODULE_DESCRIPTION("Xilinx mpeg2 transport stream muxer IP driver");
MODULE_LICENSE("GPL v2");