aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/sep/sep_driver.c
blob: bf9f2a7db93f4b35469f9c83380ce6a1c6e4390d (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
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
/*
 *
 *  sep_driver.c - Security Processor Driver main group of functions
 *
 *  Copyright(c) 2009 Intel Corporation. All rights reserved.
 *  Copyright(c) 2009 Discretix. 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 as published by the Free
 *  Software Foundation; version 2 of the License.
 *
 *  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.
 *
 *  CONTACTS:
 *
 *  Mark Allyn		mark.a.allyn@intel.com
 *
 *  CHANGES:
 *
 *  2009.06.26	Initial publish
 *  2010.09.14  Upgrade to Medfield
 *
 */

#define DEBUG
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kdev_t.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/poll.h>
#include <linux/wait.h>
#include <linux/pci.h>
#include <linux/firmware.h>
#include <linux/slab.h>
#include <linux/ioctl.h>
#include <asm/current.h>
#include <linux/ioport.h>
#include <linux/io.h>
#include <linux/interrupt.h>
#include <linux/pagemap.h>
#include <asm/cacheflush.h>
#include <linux/sched.h>
#include <linux/delay.h>

#include <linux/netlink.h>
#include <linux/connector.h>
#include <linux/cn_proc.h>


/* Stub out memrar and rar_register if those drivers are not in config */
#if defined(CONFIG_MRST_RAR_HANDLER)
#include "../memrar/memrar.h"
#else
#include "memrar_stub.h"
#endif

#if defined(CONFIG_RAR_REGISTER)
#include <linux/rar_register.h>
#else
#include "rar_register_stub.h"
#endif

#include "sep_driver_hw_defs.h"
#include "sep_driver_config.h"
#include "sep_driver_api.h"
#include "sep_dev.h"

/*----------------------------------------
	DEFINES
-----------------------------------------*/

#define SEP_RAR_IO_MEM_REGION_SIZE 0x40000

/*--------------------------------------------
	GLOBAL variables
--------------------------------------------*/

/* Keep this a single static object for now to keep the conversion easy */

static struct sep_device sep_instance;
static struct sep_device *sep_dev;

/*
  mutex for the access to the internals of the sep driver
*/
static DEFINE_MUTEX(sep_mutex);
static DEFINE_MUTEX(ioctl_mutex);
static DEFINE_SPINLOCK(snd_rply_lck);
static unsigned long snd_rply_lck_flag;


/**
 *	sep_load_firmware	-	copy firmware cache/resident
 *	@sep: device we are loading
 *
 *	This functions copies the cache and resident from their source
 *	location into destination shared memory.
 *	@sep: pointer to struct sep_device
 */

static int sep_load_firmware(struct sep_device *sep)
{
	const struct firmware *fw;
	char *cache_name = "cache.image.bin";
	char *res_name = "resident.image.bin";
	char *extapp_name = "extapp.image.bin";

	int error = 0;
	unsigned int work1, work2, work3;

	/* set addresses and load resident */
	sep->resident_bus = sep->rar_bus;
	sep->resident_addr = sep->rar_addr;

	error = request_firmware(&fw, res_name, &sep->pdev->dev);
	if (error) {
		dev_warn(&sep->pdev->dev, "cant request resident fw\n");
		return error;
	}

	memcpy(sep->resident_addr, (void *)fw->data, fw->size);
	sep->resident_size = fw->size;
	release_firmware(fw);

	dev_dbg(&sep->pdev->dev, "resident virtual is %p\n",
		sep->resident_addr);
	dev_dbg(&sep->pdev->dev, "residnet bus is %08llx\n",
		(unsigned long long)sep->resident_bus);
	dev_dbg(&sep->pdev->dev, "resident size is %08x\n",
		sep->resident_size);

	/* set addresses for dcache (no loading needed) */
	work1 = (unsigned int)sep->resident_bus;
	work2 = (unsigned int)sep->resident_size;
	work3 = (work1 + work2 + (1024 * 4)) & 0xfffff000;
	sep->dcache_bus = (dma_addr_t)work3;

	work1 = (unsigned int)sep->resident_addr;
	work2 = (unsigned int)sep->resident_size;
	work3 = (work1 + work2 + (1024 * 4)) & 0xfffff000;
	sep->dcache_addr = (void *)work3;

	sep->dcache_size = 1024 * 128;

	/* set addresses and load cache */
	sep->cache_bus = sep->dcache_bus + sep->dcache_size;
	sep->cache_addr = sep->dcache_addr + sep->dcache_size;

	error = request_firmware(&fw, cache_name, &sep->pdev->dev);
	if (error) {
		dev_warn(&sep->pdev->dev, "cant request cache fw\n");
		return error;
	}

	memcpy(sep->cache_addr, (void *)fw->data, fw->size);
	sep->cache_size = fw->size;
	release_firmware(fw);

	dev_dbg(&sep->pdev->dev, "cache virtual is %p\n",
		sep->cache_addr);
	dev_dbg(&sep->pdev->dev, "cache bus is %08llx\n",
		(unsigned long long)sep->cache_bus);
	dev_dbg(&sep->pdev->dev, "cache size is %08x\n",
		sep->cache_size);

	/* set addresses and load extapp */
	sep->extapp_bus = sep->cache_bus + (1024 * 370);
	sep->extapp_addr = sep->cache_addr + (1024 * 370);

	error = request_firmware(&fw, extapp_name, &sep->pdev->dev);
	if (error) {
		dev_warn(&sep->pdev->dev, "cant request extapp fw\n");
		return error;
	}

	memcpy(sep->extapp_addr, (void *)fw->data, fw->size);
	sep->extapp_size = fw->size;
	release_firmware(fw);

	dev_dbg(&sep->pdev->dev, "extapp virtual is %p\n",
		sep->extapp_addr);
	dev_dbg(&sep->pdev->dev, "extapp bus is %08llx\n",
		(unsigned long long)sep->extapp_bus);
	dev_dbg(&sep->pdev->dev, "extapp size is %08x\n",
		sep->extapp_size);

	return error;
}

MODULE_FIRMWARE("sep/cache.image.bin");
MODULE_FIRMWARE("sep/resident.image.bin");
MODULE_FIRMWARE("sep/extapp.image.bin");

/**
 *	sep_dump_message	- dump the message that is pending
 *	@sep: sep device
 *
 *	Dump out the message pending in the shared message area
 */

static void sep_dump_message(struct sep_device *sep)
{
	int count;
	for (count = 0; count < 12 * 4; count += 4)
		dev_dbg(&sep->pdev->dev,
			"Word %d of the message is %x\n",
			count, *((u32 *) (sep->shared_addr + count)));
}

/**
 *	sep_map_and_alloc_shared_area	-	allocate shared block
 *	@sep: security processor
 *	@size: size of shared area
 *
 *	Allocate a shared buffer in host memory that can be used by both the
 *	kernel and also the hardware interface via DMA.
 */

static int sep_map_and_alloc_shared_area(struct sep_device *sep)
{
	/* shared_addr = ioremap_nocache(0xda00000,shared_area_size); */
	sep->shared_addr = dma_alloc_coherent(&sep->pdev->dev,
		sep->shared_size,
		&sep->shared_bus, GFP_KERNEL);

	if (!sep->shared_addr) {
		dev_warn(&sep->pdev->dev,
			"shared memory dma_alloc_coherent failed\n");
		return -ENOMEM;
	}
	dev_dbg(&sep->pdev->dev,
		"sep: shared_addr %x bytes @%p (bus %x)\n",
		sep->shared_size, sep->shared_addr, sep->shared_bus);
	return 0;
}

/**
 *	sep_unmap_and_free_shared_area	-	free shared block
 *	@sep: security processor
 *
 *	Free the shared area allocated to the security processor. The
 *	processor must have finished with this and any final posted
 *	writes cleared before we do so.
 */
static void sep_unmap_and_free_shared_area(struct sep_device *sep)
{
	dev_dbg(&sep->pdev->dev, "shared area unmap and free\n");
	dma_free_coherent(&sep->pdev->dev, sep->shared_size,
				sep->shared_addr, sep->shared_bus);
}

/**
 *	sep_shared_bus_to_virt	-	convert bus/virt addresses
 *
 *	Returns virtual address inside the shared area according
 *	to the bus address.
 *	@sep: pointer to struct sep_device
 *	@bus_address: address to convert
 */

static void *sep_shared_bus_to_virt(struct sep_device *sep,
						dma_addr_t bus_address)
{
	return sep->shared_addr + (bus_address - sep->shared_bus);
}

/*
	open function for the singleton driver
	@inode_ptr struct inode *
	@file_ptr struct file *
*/
static int sep_singleton_open(struct inode *inode_ptr, struct file *file_ptr)
{
	/*-----------------
		CODE
	---------------------*/

	int error = 0;

	struct sep_device *sep;

	if (sep_dev == NULL)
		return -ENODEV;

	/* get the sep device structure and use it for the
	   private_data field in filp for other methods */
	sep = container_of(inode_ptr->i_cdev, struct sep_device,
		sep_singleton_cdev);
	file_ptr->private_data = sep;

	dev_dbg(&sep->pdev->dev, "Singleton open for pid %d\n",
		current->pid);

	dev_dbg(&sep->pdev->dev, "calling test and set for singleton 0\n");
	if (test_and_set_bit(0, &sep->singleton_access_flag)) {
		dev_warn(&sep->pdev->dev,
			"sep singleton is not available for open; busy\n");
		error = -EBUSY;
		goto end_function;
	}

	/* count up number of users opening device
	   which prevents someone else from attempting
	   to remove device (rmmod) */
	mutex_lock(&sep_mutex);
	sep->in_use += 1;
	mutex_unlock(&sep_mutex);


	dev_dbg(&sep->pdev->dev,
		"sep_singleton_open end\n");
end_function:

	return error;
}


/**
 *	sep_open		-	device open method
 *	@inode: inode of sep device
 *	@filp: file handle to sep device
 *
 *	Open method for the SEP device. Called when userspace opens
 *	the SEP device node. Must also release the memory data pool
 *	allocations.
 *
 *	Returns zero on success otherwise an error code.
 */

static int sep_open(struct inode *inode, struct file *filp)
{
	struct sep_device *sep;

	if (sep_dev == NULL)
		return -ENODEV;

	/* get the sep device structure and use it for the
	   private_data field in filp for other methods */
	sep = container_of(inode->i_cdev, struct sep_device, sep_cdev);
	filp->private_data = sep;

	dev_dbg(&sep->pdev->dev, "Open for pid %d\n", current->pid);

	/* count up number of users opening device
	   which prevents someone else from attempting
	   to remove device (rmmod) */
	mutex_lock(&sep_mutex);
	sep->in_use += 1;
	mutex_unlock(&sep_mutex);

	/* Note that anyone is allowed to open device and
	   that blocking, if necessary, will take place
	   at transaction level (when the sep is actally
	   doing something */

	return 0;
}

/**
 *	sep_singleton_release		-	close a SEP singleton device
 *	@inode: inode of SEP device
 *	@filp: file handle being closed
 *
 *	Called on the final close of a SEP device. As the open protects against
 *	multiple simultaenous opens that means this method is called when the
 *	final reference to the open handle is dropped.
 */

static int sep_singleton_release(struct inode *inode, struct file *filp)
{
	struct sep_device *sep = filp->private_data;

	dev_dbg(&sep->pdev->dev, "Singleton release for pid %d\n",
		current->pid);

	/* decrement the number of processes having this device
	   open */
	mutex_lock(&sep_mutex);
	sep->in_use -= 1;
	mutex_unlock(&sep_mutex);

	clear_bit(0, &sep->singleton_access_flag);

	return 0;
}

/**
 *	sep_request_daemonopen	- request daemon open method
 *	@inode: inode of sep device
 *	@filp: file handle to sep device
 *
 *	Open method for the SEP request daemon. Called when
 *	request daemon in userspace opens
 *	the SEP device node.
 *
 *	Returns zero on success otherwise an error code.
 */

static int sep_request_daemon_open(struct inode *inode, struct file *filp)
{
	struct sep_device *sep;

	int error = 0;

	if (sep_dev == NULL)
		return -ENODEV;

	/* get the sep device structure and use it for the
	   private_data field in filp for other methods */
	sep = container_of(inode->i_cdev, struct sep_device, sep_daemon_cdev);
	filp->private_data = sep;

	dev_dbg(&sep->pdev->dev, "Request daemon open for pid %d\n",
		current->pid);

	/* There is supposed to be only one request daemon */
	dev_dbg(&sep->pdev->dev, "calling test and set for req_dmon open 0\n");
	if (test_and_set_bit(0, &sep->request_daemon_open)) {
		dev_warn(&sep->pdev->dev,
			"request daemon is not available for open; busy\n");
		error = -EBUSY;
		goto end_function;
	}

	/* count up number of users opening device
	   which prevents someone else from attempting
	   to remove device (rmmod) */
	mutex_lock(&sep_mutex);
	sep->in_use += 1;
	mutex_unlock(&sep_mutex);

end_function:

	return error;
}


/**
 *	sep_request_daemon_release		-	close a SEP daemon
 *	@inode: inode of SEP device
 *	@filp: file handle being closed
 *
 *	Called on the final close of a SEP daemon.
 */

static int sep_request_daemon_release(struct inode *inode, struct file *filp)
{
	struct sep_device *sep = filp->private_data;

	dev_dbg(&sep->pdev->dev, "Reques daemon release for pid %d\n",
		current->pid);

	/* clear the request_daemon_open flag */
	clear_bit(0, &sep->request_daemon_open);

	/* decrement the number of processes having this device
	   open */
	mutex_lock(&sep_mutex);
	sep->in_use -= 1;
	mutex_unlock(&sep_mutex);

	return 0;
}

/*
	This function raises interrupt to SEPm that signals that is has a
	new command from HOST
	@sep: struct sep_device *
*/
static int sep_req_daemon_send_reply_command_handler(struct sep_device *sep)
{
	/* error */
	int error = 0;


	dev_dbg(&sep->pdev->dev,
		"sep_req_daemon_send_reply_command_handler start\n");

	error = 0;

	sep_dump_message(sep);

	/* update both counters counter */
	/* logk while activity with counters */
	spin_lock_irqsave(&snd_rply_lck, snd_rply_lck_flag);
	sep->send_ct++;
	sep->reply_ct++;

	/* send the interrupt to SEP */
	sep_write_reg(sep, HW_HOST_HOST_SEP_GPR2_REG_ADDR,
		sep->send_ct);

	/* update host to sep counter */
	sep->send_ct++;

	/* done with counters */
	spin_unlock_irqrestore(&snd_rply_lck, snd_rply_lck_flag);

	dev_dbg(&sep->pdev->dev,
		"sep_req_daemon_send_reply send_ct %lx reply_ct %lx\n",
		sep->send_ct, sep->reply_ct);

	dev_dbg(&sep->pdev->dev,
		"sep_req_daemon_send_reply_command_handler end\n");

	return error;
}


/*
  this function handles the request for freeing dma table for synhronic actions
	@sep: pointere to struct sep_device
*/

static int sep_free_dma_table_data_handler(struct sep_device *sep)
{
	/* counter */
	int count = 0;

	/* dcb counter */
	int dcb_counter = 0;

	/* pointer to the current dma_resource struct */
	struct sep_dma_resource *dma_resource;

	/*-------------------------
	CODE
	-----------------------------*/

	dev_dbg(&sep->pdev->dev,
		"sep_free_dma_table_data_handler start\n");


	for (dcb_counter = 0; dcb_counter < sep->nr_dcb_creat;
		dcb_counter++) {

		dma_resource = &sep->dma_res_arr[dcb_counter];

		/* unmap and free input map array */
		if (dma_resource->in_map_array) {

			for (count = 0; count < dma_resource->in_num_pages;
				count++) {

				dma_unmap_page(&sep->pdev->dev,
					dma_resource->in_map_array[count].dma_addr,
					dma_resource->in_map_array[count].size,
					DMA_TO_DEVICE/*DMA_BIDIRECTIONAL*/);
			}

			kfree(dma_resource->in_map_array);
		}

		/* unmap output map array, DON'T free it yet */
		if (dma_resource->out_map_array) {
			for (count = 0; count < dma_resource->out_num_pages;
				count++) {

				dma_unmap_page(&sep->pdev->dev,
					dma_resource->out_map_array[count].dma_addr,
					dma_resource->out_map_array[count].size,
					DMA_FROM_DEVICE/*DMA_BIDIRECTIONAL*/);
			}

			kfree(dma_resource->out_map_array);
		}

		/* free page cache for output */
		if (dma_resource->in_page_array) {
			for (count = 0; count < dma_resource->in_num_pages;
				count++) {

				flush_dcache_page(dma_resource->in_page_array[count]);
				page_cache_release(dma_resource->in_page_array[count]);
			}

			kfree(dma_resource->in_page_array);

		}

		if (dma_resource->out_page_array) {
			for (count = 0; count < dma_resource->out_num_pages;
				count++) {

				if (!PageReserved(dma_resource->out_page_array[count]))
					SetPageDirty(dma_resource->out_page_array[count]);

				flush_dcache_page(dma_resource->out_page_array[count]);
				page_cache_release(dma_resource->out_page_array[count]);
			}

			kfree(dma_resource->out_page_array);
		}

		/* reset all the values */
		dma_resource->in_page_array = 0;
		dma_resource->out_page_array = 0;
		dma_resource->in_num_pages = 0;
		dma_resource->out_num_pages = 0;
		dma_resource->in_map_array = 0;
		dma_resource->out_map_array = 0;
		dma_resource->in_map_num_entries = 0;
		dma_resource->out_map_num_entries = 0;

	}

	sep->nr_dcb_creat = 0;
	sep->num_lli_tables_created = 0;

	dev_dbg(&sep->pdev->dev,
		"sep_free_dma_table_data_handler end\n");

	return 0;
}


/*
	This API handles the end transaction request
	@sep: struct sep_device * for sep
	@file_ptr: struct file * for file
*/
static int sep_req_daemon_end_transaction_handler(
	struct sep_device *sep, struct file *file_ptr)
{
	/*----------------------------
	CODE
	-----------------------------*/

	dev_dbg(&sep->pdev->dev,
		"sep_req_daemon_end_transaction_handler start\n");

	dev_dbg(&sep->pdev->dev,
		"sep_req_daemon_end_transaction_handler end\n");

	return 0;
}

static int sep_request_daemon_mmap(struct file  *filp,
	struct vm_area_struct  *vma)
{
	/* device */
	struct sep_device *sep = filp->private_data;

	/* physical addr */
	dma_addr_t phys_addr;

	/* error */
	int error = 0;

	/*-----------------------
	CODE
	-------------------------*/

	dev_dbg(&sep->pdev->dev, "daemon mmap start\n");

	/* check that the size of the mapped range is as the size of the message
	shared area */
	if ((vma->vm_end - vma->vm_start) > SEP_DRIVER_MMMAP_AREA_SIZE) {
		dev_warn(&sep->pdev->dev,
			"mmap requested more than allowed\n");
		error = -EAGAIN;
		goto end_function;
	}

	/* get physical address */
	phys_addr = sep->shared_bus;

	dev_dbg(&sep->pdev->dev,
		"phys_addr is %08x\n",
		(u32)phys_addr);

	if (remap_pfn_range(vma, vma->vm_start, phys_addr >> PAGE_SHIFT,
		vma->vm_end - vma->vm_start, vma->vm_page_prot)) {

		dev_warn(&sep->pdev->dev,
			"remap_page_range failed\n");
		error = -EAGAIN;
		goto end_function;
	}

end_function:

	dev_dbg(&sep->pdev->dev, "daemon mmap end\n");

	return error;
}

/* sep_request_daemon_poll
	@sep: struct sep_device * for current sep device
	@filp: struct file * for open file
	@wait: poll_table * for poll
*/
static unsigned int sep_request_daemon_poll(struct file *filp,
	poll_table  *wait)
{
	u32	mask = 0;

	/* GPR2 register */
	u32	retVal2;

	struct sep_device *sep = filp->private_data;

	/*----------------------------------------------
	CODE
	-------------------------------------------------*/
	dev_dbg(&sep->pdev->dev,
		"daemon poll: start\n");

	dev_dbg(&sep->pdev->dev, "daemon poll: calling poll wait sep_event\n");

	poll_wait(filp, &sep->event_request_daemon, wait);

	dev_dbg(&sep->pdev->dev, "daemon poll: exit poll wait sep_event\n");

	dev_dbg(&sep->pdev->dev,
		"daemon poll: send_ct is %lx reply ct is %lx\n",
		sep->send_ct, sep->reply_ct);

	/* check if the data is ready */
	if (sep->send_ct == sep->reply_ct) {

		retVal2 = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);

		dev_dbg(&sep->pdev->dev,
			"daemon poll: data check (GPR2) is %x\n", retVal2);

		/* check if PRINT request */
		if ((retVal2 >> 30) & 0x1) {

			dev_dbg(&sep->pdev->dev,
				"daemon poll: PRINTF request in\n");
			mask |= POLLIN;
			goto end_function;
		}

		/* check if NVS request */
		if (retVal2 >> 31) {

			dev_dbg(&sep->pdev->dev,
				"daemon poll: NVS request in\n");
			mask |= POLLPRI | POLLWRNORM;
		}
	}

	else {

		dev_dbg(&sep->pdev->dev,
			"daemon poll: no reply received; returning 0\n");
		mask = 0;
	}

#if SEP_DRIVER_POLLING_MODE
	/* in case not a request and we're running -goto sleep and wake up
	   application process */
	if (sep->send_ct != sep->reply_ct) {

		dev_dbg(&sep->pdev->dev,
			"daemon poll: wake up task\n");

		wake_up(&sep->event);
	}
#endif

end_function:

	dev_dbg(&sep->pdev->dev,
		"daemon poll: exit\n");
	return mask;
}

/**
 *	sep_release		-	close a SEP device
 *	@inode: inode of SEP device
 *	@filp: file handle being closed
 *
 *	Called on the final close of a SEP device.
 */

static int sep_release(struct inode *inode, struct file *filp)
{
	struct sep_device *sep = filp->private_data;

	dev_dbg(&sep->pdev->dev, "Release for pid %d\n", current->pid);

	/* decrement the number of processes having this device
	   open */
	mutex_lock(&sep_mutex);
	sep->in_use -= 1;

	/* is this the process that has a transaction open? */
	dev_dbg(&sep->pdev->dev, "waking up event and mmap_event\n");
	if (sep->pid_doing_transaction == current->pid) {
		clear_bit(SEP_MMAP_LOCK_BIT, &sep->in_use_flags);
		clear_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags);
		sep_free_dma_table_data_handler(sep);
		wake_up(&sep->event);
		sep->pid_doing_transaction = 0;
	}

	mutex_unlock(&sep_mutex);
	return 0;
}

/*---------------------------------------------------------------
  map function - this functions maps the message shared area
-----------------------------------------------------------------*/
static int sep_mmap(struct file *filp, struct vm_area_struct *vma)
{
	dma_addr_t bus_addr;
	struct sep_device *sep = filp->private_data;
	unsigned long error = 0;

	dev_dbg(&sep->pdev->dev, "mmap start\n");


	dev_dbg(&sep->pdev->dev, "calling wait on event_mmap, tsetbit"
		" SEP_MMAP_LOCK_BIT in_use_flags 0\n");
	/* Set the transaction busy (own the device) */
	wait_event_interruptible(sep->event,
		test_and_set_bit(SEP_MMAP_LOCK_BIT,
		&sep->in_use_flags) == 0);

	if (signal_pending(current)) {
		error = -EINTR;
		goto end_function_with_error;
	}
	sep->pid_doing_transaction = current->pid;

	/* zero the pools and the number of data pool alocation pointers */
	sep->data_pool_bytes_allocated = 0;
	sep->num_of_data_allocations = 0;

	/* check that the size of the mapped range is as the size of the message
	   shared area */
	if ((vma->vm_end - vma->vm_start) > SEP_DRIVER_MMMAP_AREA_SIZE) {
		dev_warn(&sep->pdev->dev,
			"mmap requested size is more than allowed\n");
		dev_warn(&sep->pdev->dev,
			"vma->vm_end is %08lx\n", vma->vm_end);
		dev_warn(&sep->pdev->dev,
			"vma->vm_end is %08lx\n", vma->vm_start);
		error = -EAGAIN;
		goto end_function_with_error;
	}

	dev_dbg(&sep->pdev->dev,
		"shared_addr is %p\n", sep->shared_addr);

	/* get bus address */
	bus_addr = sep->shared_bus;

	dev_dbg(&sep->pdev->dev,
		"phys_addr is %08llx\n", (unsigned long long)bus_addr);

	if (remap_pfn_range(vma, vma->vm_start, bus_addr >> PAGE_SHIFT,
		vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
		dev_warn(&sep->pdev->dev,
			"SEP Driver remap_page_range failed\n");
		error = -EAGAIN;
		goto end_function_with_error;
	}

	dev_dbg(&sep->pdev->dev, "mmap end\n");
	goto end_function;

end_function_with_error:

	/* clear the bit */
	clear_bit(SEP_MMAP_LOCK_BIT, &sep->in_use_flags);
	sep->pid_doing_transaction = 0;

	/* raise event for stuck contextes */

	dev_warn(&sep->pdev->dev, "mmap error - waking up event\n");
	wake_up(&sep->event);

end_function:

	return error;
}


/*-----------------------------------------------
  poll function
*----------------------------------------------*/
static unsigned int sep_poll(struct file *filp, poll_table *wait)
{
	u32 count = 0;
	u32 mask = 0;
	u32 retval = 0;
	u32 retval2 = 0;

	struct sep_device *sep = filp->private_data;

	dev_dbg(&sep->pdev->dev, "poll: start\n");

	/* am I the process that own the transaction? */
	if (current->pid != sep->pid_doing_transaction) {
		dev_warn(&sep->pdev->dev, "poll; wrong pid\n");
		mask = POLLERR;
		goto end_function;
	}

	/* check if send command or send_reply were activated previously */
	if (!test_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags)) {
		dev_warn(&sep->pdev->dev, "poll; lock bit set\n");
		mask = POLLERR;
		goto end_function;
	}

#if SEP_DRIVER_POLLING_MODE

	dev_dbg(&sep->pdev->dev, "poll: waiting for send_ct and gpr2\n");
	while ((retval2 == 0x0) &&
		(sep->send_ct != (retVal & 0x3FFFFFFF))) {

		retval = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);

		retval2 = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
	}

	spin_lock_irqsave(&snd_rply_lck, snd_rply_lck_flag);
	sep->reply_ct++;
	spin_unlock_irqrestore(&snd_rply_lck, snd_rply_lck_flag);

#endif

	/* add the event to the polling wait table */
	dev_dbg(&sep->pdev->dev, "poll: calling wait sep_event\n");

	poll_wait(filp, &sep->event, wait);

	dev_dbg(&sep->pdev->dev, "poll: exit wait sep_event\n");

	dev_dbg(&sep->pdev->dev, "poll: send_ct is %lx reply ct is %lx\n",
		sep->send_ct, sep->reply_ct);

	/* check if error occured during poll */
	retval2 = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);

	if (retval2 != 0x0) {

		dev_warn(&sep->pdev->dev, "poll; poll error %x\n",
			retval2);
		mask |= POLLERR;
		goto end_function;
	}

	if (sep->send_ct == sep->reply_ct) {

		retval = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
		dev_dbg(&sep->pdev->dev, "poll: data ready check (GPR2)  %x\n",
			retval);

		/* check if printf request  */
		if ((retval >> 30) & 0x1) {

			dev_dbg(&sep->pdev->dev, "poll: sep printf request\n");
			wake_up(&sep->event_request_daemon);
			goto end_function;

		}

		/* check if the this is sep reply or request */
		if (retval >> 31) {

			dev_dbg(&sep->pdev->dev, "poll: sep request\n");
			wake_up(&sep->event_request_daemon);

		} else {

			dev_dbg(&sep->pdev->dev, "poll: normal return\n");

			/* clear the bit-in case it must be set
			   again by send_reply_comand */

			clear_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags);

			sep_dump_message(sep);

			for (count = 0; count < 10 * 4; count += 4)
				dev_dbg(&sep->pdev->dev,
					"poll; debug word %d is %x\n",
					count,
					*((u32 *)(sep->shared_addr + count +
					0x1800)));

			dev_dbg(&sep->pdev->dev,
				"poll; sep reply POLLIN | POLLRDNORM\n");

			mask |= POLLIN | POLLRDNORM;

		}

	}

	else {

		dev_dbg(&sep->pdev->dev,
			"poll; no reply received; returning mask of 0\n");
		mask = 0;
	}

end_function:

	dev_dbg(&sep->pdev->dev, "poll: end\n");
	return mask;
}

/**
 *	sep_time_address	-	address in SEP memory of time
 *	@sep: SEP device we want the address from
 *
 *	Return the address of the two dwords in memory used for time
 *	setting.
 */

static u32 *sep_time_address(struct sep_device *sep)
{
	return sep->shared_addr + SEP_DRIVER_SYSTEM_TIME_MEMORY_OFFSET_IN_BYTES;
}

/**
 *	sep_set_time		-	set the SEP time
 *	@sep: the SEP we are setting the time for
 *
 *	Calculates time and sets it at the predefined address.
 *	Called with the sep mutex held.
 */
static unsigned long sep_set_time(struct sep_device *sep)
{
	struct timeval time;
	u32 *time_addr;	/* address of time as seen by the kernel */


	dev_dbg(&sep->pdev->dev,
		"sep:sep_set_time start\n");

	do_gettimeofday(&time);

	/* set value in the SYSTEM MEMORY offset */
	time_addr = sep_time_address(sep);

	time_addr[0] = SEP_TIME_VAL_TOKEN;
	time_addr[1] = time.tv_sec;

	dev_dbg(&sep->pdev->dev,
		"time.tv_sec is %lu\n", time.tv_sec);
	dev_dbg(&sep->pdev->dev,
		"time_addr is %p\n", time_addr);
	dev_dbg(&sep->pdev->dev,
		"sep->shared_addr is %p\n", sep->shared_addr);

	return time.tv_sec;
}

/*
	initializes the caller id functionality
	@sep: sep device
*/
static int sep_init_caller_id(struct sep_device *sep)
{
	/* return value */
	int ret_val;

	/* counter */
	int counter;

	struct cb_id caller_id;


	/*----------------------------
		  CODE
	---------------------------------*/

	dev_dbg(&sep->pdev->dev,
		"sep_init_caller_id start\n");

	ret_val = 0;

	/* init cb_id struct */
	caller_id.idx = CN_IDX_PROC;
	caller_id.val = CN_VAL_PROC;

	/* init caller id table */
	for (counter = 0; counter < SEP_CALLER_ID_TABLE_NUM_ENTRIES; counter++)
		sep->caller_id_table[counter].pid = 0;


	/* init access flag */
	sep->singleton_access_flag = 0;

	dev_dbg(&sep->pdev->dev,
		"caller id table init finished\n");

	/* register to netlink connector */
	/* ret_val = cn_add_callback(&caller_id,
		 "sep_caller_id", sep_cn_callback); */

	if (ret_val)
		dev_warn(&sep->pdev->dev,
			"cn_add_callback failed %x\n", ret_val);

	dev_dbg(&sep->pdev->dev,
		"sep_init_caller_id end\n");

	return ret_val;
}

/*
	inserts the data into the caller id table
	@sep: sep device
	@arg: pointer to struct sep_driver_set_caller_id_t
*/
static int sep_set_caller_id_handler(struct sep_device *sep, u32 arg)
{
	/* error */
	int   error;

	/* counter */
	int   i;

	/* command arguments */
	struct sep_driver_set_caller_id_t command_args;

	/*----------------------------
		  CODE
	------------------------------*/

	dev_dbg(&sep->pdev->dev,
		"sep_set_caller_id_handler start\n");

	error = 0;

	for (i = 0; i < SEP_CALLER_ID_TABLE_NUM_ENTRIES; i++) {
		if (sep->caller_id_table[i].pid == 0)
			break;
	}

	if (i == SEP_CALLER_ID_TABLE_NUM_ENTRIES) {

		dev_warn(&sep->pdev->dev,
			"no more caller id entries left\n");
		dev_warn(&sep->pdev->dev,
			"maximum number is %d\n",
			SEP_CALLER_ID_TABLE_NUM_ENTRIES);

		error = -ENOMEM;
		goto end_function;
	}

	/* copy the data */
	if (copy_from_user(&command_args, (void *)arg,
		sizeof(command_args))) {
		dev_warn(&sep->pdev->dev,
			"copy_from_user failed\n");
		error = -EFAULT;
		goto end_function;
	}

	if (!command_args.pid || !command_args.callerIdAddress ||
		!command_args.callerIdSizeInBytes) {
		dev_warn(&sep->pdev->dev,
			"params validation error\n");
		error = -EINVAL;
		goto end_function;
	}

	dev_dbg(&sep->pdev->dev,
		"pid is %x\n", command_args.pid);
	dev_dbg(&sep->pdev->dev,
		"callerIdSizeInBytes is %x\n",
		command_args.callerIdSizeInBytes);

	if (command_args.callerIdSizeInBytes >
		SEP_CALLER_ID_HASH_SIZE_IN_BYTES) {
		dev_warn(&sep->pdev->dev,
			"callerIdSizeInBytes too big\n");
		error = -EINVAL;
		goto end_function;
	}

	sep->caller_id_table[i].pid = command_args.pid;

	if (copy_from_user(sep->caller_id_table[i].callerIdHash,
		command_args.callerIdAddress, command_args.callerIdSizeInBytes))
		dev_warn(&sep->pdev->dev,
			"copy_from_user for callerIdAddress failed\n");
		error = -EFAULT;

end_function:

	dev_dbg(&sep->pdev->dev,
		"sep_set_caller_id_handler end\n");

	return error;
}

/*
	set the caller id (if exists) to the sep
	@sep: pointer to struct_sep
*/
static int sep_set_current_caller_id(struct sep_device *sep)
{
	int i;

	dev_dbg(&sep->pdev->dev, "sep_set_current_caller_id start\n");

	dev_dbg(&sep->pdev->dev, "current process is %d\n", current->pid);

	/* zero the previous value */
	memset((void *)(sep->shared_addr +
		(SEP_CALLER_ID_OFFSET_BYTES)),
		0,
		SEP_CALLER_ID_HASH_SIZE_IN_BYTES);

	for (i = 0; i < SEP_CALLER_ID_TABLE_NUM_ENTRIES; i++) {
		if (sep->caller_id_table[i].pid == current->pid) {
			dev_dbg(&sep->pdev->dev, "Caller Id found\n");

			memcpy((void *)(sep->shared_addr +
				(SEP_CALLER_ID_OFFSET_BYTES)),
				(void *)(sep->caller_id_table[i].callerIdHash),
				SEP_CALLER_ID_HASH_SIZE_IN_BYTES);
			break;
		}
	}

	dev_dbg(&sep->pdev->dev, "sep_set_current_caller_id end\n");

	return 0;
}

/**
 *	sep_send_command_handler	-	kick off a command
 *	@sep: sep being signalled
 *
 *	This function raises interrupt to SEP that signals that is has a new
 *	command from the host
 */

static void sep_send_command_handler(struct sep_device *sep)
{
	dev_dbg(&sep->pdev->dev, "sep_send_command_handler start\n");

	dev_dbg(&sep->pdev->dev, "calling test and set for "
		" in_use_flags SEP_SEND_MSG_LOCK_BIT 0\n");
	if (test_and_set_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags)) {

		dev_warn(&sep->pdev->dev,
			"cannot send msg while awaiting reply\n");
		goto end_function;
	}

	sep_set_time(sep);

	/* only Medfield has caller id */
	if (sep->mrst == 0)
		sep_set_current_caller_id(sep);

	sep_dump_message(sep);

	/* update counter */
	spin_lock_irqsave(&snd_rply_lck, snd_rply_lck_flag);
	sep->send_ct++;
	spin_unlock_irqrestore(&snd_rply_lck, snd_rply_lck_flag);

	dev_dbg(&sep->pdev->dev, "sep_send_command_handler"
		"send_ct %lx reply_ct %lx\n", sep->send_ct, sep->reply_ct);

	/* send interrupt to SEP */
	sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x2);

end_function:

	dev_dbg(&sep->pdev->dev, "sep_send_command_handler end\n");

	return;
}

/*
  This function handles the allocate data pool memory request
  This function returns calculates the bus address of the
  allocated memory, and the offset of this area from the mapped address.
  Therefore, the FVOs in user space can calculate the exact virtual
  address of this allocated memory
	@sep: pointer to struct_sep
	@arg: pointer to struct sep_driver_alloc_t
*/
static int sep_allocate_data_pool_memory_handler(struct sep_device *sep,
	unsigned long arg)
{
	int error = 0;
	struct sep_driver_alloc_t command_args;

	/* holds the allocated buffer address in the system memory pool */
	u32 *token_addr;

	dev_dbg(&sep->pdev->dev,
		"sep_allocate_data_pool_memory_handler start\n");

	error = copy_from_user(&command_args, (void *)arg,
		sizeof(struct sep_driver_alloc_t));
	if (error) {
		dev_warn(&sep->pdev->dev,
			"allocate data pool copy from user error\n");
		goto end_function;
	}

	/* allocate memory */
	if ((sep->data_pool_bytes_allocated + command_args.num_bytes) >
		SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES) {

		dev_warn(&sep->pdev->dev,
			"no more data pool bytes available\n");
		error = -ENOMEM;
		goto end_function;
	}

	dev_dbg(&sep->pdev->dev,
		"bytes_allocated: %x\n", (int)sep->data_pool_bytes_allocated);
	dev_dbg(&sep->pdev->dev,
		"offset: %x\n", SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES);
	/* set the virtual and bus address */
	command_args.offset = SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES +
		sep->data_pool_bytes_allocated;

	dev_dbg(&sep->pdev->dev,
		"command_args.offset: %x\n", command_args.offset);

	/* addressing information is put in a place in the shared area
	   that is known by the sep */
	token_addr = (u32 *)(sep->shared_addr +
		SEP_DRIVER_DATA_POOL_ALLOCATION_OFFSET_IN_BYTES +
		(sep->num_of_data_allocations)*2*sizeof(u32));

	dev_dbg(&sep->pdev->dev,
		"allocation offset: %x\n",
		SEP_DRIVER_DATA_POOL_ALLOCATION_OFFSET_IN_BYTES);
	dev_dbg(&sep->pdev->dev, "data pool token addr is %p\n", token_addr);

	token_addr[0] = SEP_DATA_POOL_POINTERS_VAL_TOKEN;
	token_addr[1] = (u32)sep->shared_bus +
		SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES +
		sep->data_pool_bytes_allocated;

	dev_dbg(&sep->pdev->dev, "data pool token [0] %x\n", token_addr[0]);
	dev_dbg(&sep->pdev->dev, "data pool token [1] %x\n", token_addr[1]);

	/* write the memory back to the user space */
	error = copy_to_user((void *)arg, (void *)&command_args,
		sizeof(struct sep_driver_alloc_t));
	if (error) {

		dev_warn(&sep->pdev->dev,
			"allocate data pool copy to user error\n");
		goto end_function;
	}

	/* update the allocation */
	sep->data_pool_bytes_allocated += command_args.num_bytes;
	sep->num_of_data_allocations += 1;

	dev_dbg(&sep->pdev->dev, "data_allocations %d\n",
		sep->num_of_data_allocations);
	dev_dbg(&sep->pdev->dev, "bytes allocated  %d\n",
		(int)sep->data_pool_bytes_allocated);

end_function:

	dev_dbg(&sep->pdev->dev,
		"sep_allocate_data_pool_memory_handler end\n");
	return error;
}

/*
	This function locks all the physical pages of the kernel virtual buffer
	and construct a basic lli  array, where each entry holds the physical
	page address and the size that application data holds in this page
*/
static int sep_lock_kernel_pages(struct sep_device *sep,
	u32 kernel_virt_addr,
	u32 data_size,
	struct sep_lli_entry_t **lli_array_ptr,
	int in_out_flag)

{
	/* error */
	int error = 0;

	/* array of lli */
	struct sep_lli_entry_t *lli_array;

	/* map array */
	struct sep_dma_map *map_array;

	/*------------------------
	CODE
	--------------------------*/

	dev_dbg(&sep->pdev->dev,
		"sep_lock_kernel_pages start\n");

	dev_dbg(&sep->pdev->dev,
		"kernel_virt_addr is %08x\n", kernel_virt_addr);
	dev_dbg(&sep->pdev->dev,
		"data_size is %x\n", data_size);

	lli_array = kmalloc(sizeof(struct sep_lli_entry_t) * 1, GFP_ATOMIC);
	if (!lli_array) {

		dev_warn(&sep->pdev->dev, "kmalloc for lli_array failed\n");
		error = -ENOMEM;
		goto end_function;
	}

	map_array = kmalloc(sizeof(struct sep_dma_map) * 1, GFP_ATOMIC);
	if (!map_array) {
		dev_warn(&sep->pdev->dev, "kmalloc for map_array failed\n");
		error = -ENOMEM;
		goto end_function_with_error;
	}

	map_array[0].dma_addr =
		dma_map_single(&sep->pdev->dev, (void *)kernel_virt_addr,
		data_size, DMA_BIDIRECTIONAL);
	map_array[0].size = data_size;


	/* set the start address of the first page - app data may start not at
	the beginning of the page */
	lli_array[0].bus_address = (u32)map_array[0].dma_addr;
	lli_array[0].block_size = map_array[0].size;

	/* debug print */
	dev_dbg(&sep->pdev->dev,
		"lli_array[0].bus_address is %08x,"
		" lli_array[0].block_size is %x\n",
		lli_array[0].bus_address,
		lli_array[0].block_size);

	/* set the output parameters */
	if (in_out_flag == SEP_DRIVER_IN_FLAG) {
		*lli_array_ptr = lli_array;
		sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages = 1;
		sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = 0;
		sep->dma_res_arr[sep->nr_dcb_creat].in_map_array = map_array;
		sep->dma_res_arr[sep->nr_dcb_creat].in_map_num_entries = 1;
	} else {
		*lli_array_ptr = lli_array;
		sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages = 1;
		sep->dma_res_arr[sep->nr_dcb_creat].out_page_array = 0;
		sep->dma_res_arr[sep->nr_dcb_creat].out_map_array = map_array;
		sep->dma_res_arr[sep->nr_dcb_creat].out_map_num_entries = 1;
	}

	goto end_function;

end_function_with_error:

	kfree(lli_array);

end_function:

	dev_dbg(&sep->pdev->dev,
		"sep_lock_kernel_pages end\n");

	return error;
}

/*
	This function locks all the physical pages of the application
	virtual buffer and construct a basic lli  array, where each entry
	holds the physical page address and the size that application
	data holds in this physical pages
*/
static int sep_lock_user_pages(struct sep_device *sep,
	u32 app_virt_addr,
	u32 data_size,
	struct sep_lli_entry_t **lli_array_ptr,
	int in_out_flag)

{
	/* error */
	int error;

	/* the the page of the end address of the user space buffer */
	u32 end_page;

	/* the page of the start address of the user space buffer */
	u32 start_page;

	/* the range in pages */
	u32 num_pages;

	/* array of pointers ot page */
	struct page **page_array;

	/* array of lli */
	struct sep_lli_entry_t *lli_array;

	/* map array */
	struct sep_dma_map *map_array;

	/* direction of the DMA mapping for locked pages */
	enum dma_data_direction	dir;

	/* count */
	u32 count;

	/* result */
	int result;

	/*------------------------
	CODE
	--------------------------*/

	dev_dbg(&sep->pdev->dev,
		"sep_lock_user_pages start\n");

	error = 0;

	/* set start and end pages  and num pages */
	end_page = (app_virt_addr + data_size - 1) >> PAGE_SHIFT;
	start_page = app_virt_addr >> PAGE_SHIFT;
	num_pages = end_page - start_page + 1;

	dev_dbg(&sep->pdev->dev,
		"app_virt_addr is %08x\n", app_virt_addr);
	dev_dbg(&sep->pdev->dev,
		"data_size is %x\n", data_size);
	dev_dbg(&sep->pdev->dev,
		"start_page is %x\n", start_page);
	dev_dbg(&sep->pdev->dev,
		"end_page is %x\n", end_page);
	dev_dbg(&sep->pdev->dev,
		"num_pages is %x\n", num_pages);

	dev_dbg(&sep->pdev->dev,
		"starting page_array malloc\n");

	/* allocate array of pages structure pointers */
	page_array = kmalloc(sizeof(struct page *) * num_pages, GFP_ATOMIC);
	if (!page_array) {
		dev_warn(&sep->pdev->dev,
			"kmalloc for page_array failed\n");

		error = -ENOMEM;
		goto end_function;
	}

	map_array = kmalloc(sizeof(struct sep_dma_map) * num_pages, GFP_ATOMIC);
	if (!map_array) {
		dev_warn(&sep->pdev->dev,
			"kmalloc for map_array failed\n");
		error = -ENOMEM;
		goto end_function_with_error1;
	}

	lli_array = kmalloc(sizeof(struct sep_lli_entry_t) * num_pages,
		GFP_ATOMIC);

	if (!lli_array) {

		dev_warn(&sep->pdev->dev,
			"kmalloc for lli_array failed\n");
		error = -ENOMEM;
		goto end_function_with_error2;
	}

	dev_dbg(&sep->pdev->dev,
		"starting get_user_pages\n");

	/* convert the application virtual address into a set of physical */
	down_read(&current->mm->mmap_sem);
	result = get_user_pages(current, current->mm, app_virt_addr,
		num_pages,
		((in_out_flag == SEP_DRIVER_IN_FLAG) ? 0 : 1),
		0, page_array, 0);

	up_read(&current->mm->mmap_sem);

	/* check the number of pages locked - if not all then exit with error */
	if (result != num_pages) {

		dev_warn(&sep->pdev->dev,
			"not all pages locked by get_user_pages\n");
		error = -ENOMEM;
		goto end_function_with_error3;
	}

	dev_dbg(&sep->pdev->dev,
		"get_user_pages succeeded\n");

	/* set direction */
	if (in_out_flag == SEP_DRIVER_IN_FLAG)
		dir = DMA_TO_DEVICE;
	else
		dir = DMA_FROM_DEVICE;

	/* fill the array using page array data and map the pages - this action
		  will also flush the cache as needed */
	for (count = 0; count < num_pages; count++) {
		/* fill the map array */
		map_array[count].dma_addr =
			dma_map_page(&sep->pdev->dev, page_array[count],
			0, PAGE_SIZE, /*dir*/DMA_BIDIRECTIONAL);

		map_array[count].size = PAGE_SIZE;

		/* fill the lli array entry */
		lli_array[count].bus_address = (u32)map_array[count].dma_addr;
		lli_array[count].block_size = PAGE_SIZE;

		dev_warn(&sep->pdev->dev,
			"lli_array[%x].bus_address is %08x, \
			lli_array[%x].block_size is %x\n",
			count, lli_array[count].bus_address,
			count, lli_array[count].block_size);
	}

	/* check the offset for the first page -
		  data may start not at the beginning of the page */
	lli_array[0].bus_address =
		lli_array[0].bus_address + (app_virt_addr & (~PAGE_MASK));

	/* check that not all the data is in the first page only */
	if ((PAGE_SIZE - (app_virt_addr & (~PAGE_MASK))) >= data_size)
		lli_array[0].block_size = data_size;
	else
		lli_array[0].block_size =
			PAGE_SIZE - (app_virt_addr & (~PAGE_MASK));

	dev_dbg(&sep->pdev->dev,
		"lli_array[0].bus_address is %08x, \
		lli_array[0].block_size is %x\n",
		lli_array[count].bus_address,
		lli_array[count].block_size);

	/* check the size of the last page */
	if (num_pages > 1) {

		lli_array[num_pages - 1].block_size =
			(app_virt_addr + data_size) & (~PAGE_MASK);

		dev_warn(&sep->pdev->dev,
			"lli_array[%x].bus_address is %08x, \
			lli_array[%x].block_size is %x\n",
			num_pages - 1, lli_array[count].bus_address,
			num_pages - 1,
			lli_array[count].block_size);
	}

	/* set output params acording to the in_out flag */
	if (in_out_flag == SEP_DRIVER_IN_FLAG) {
		*lli_array_ptr = lli_array;
		sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages =
			num_pages;
		sep->dma_res_arr[sep->nr_dcb_creat].in_page_array =
			page_array;
		sep->dma_res_arr[sep->nr_dcb_creat].in_map_array =
			map_array;
		sep->dma_res_arr[sep->nr_dcb_creat].in_map_num_entries =
			num_pages;
	} else {
		*lli_array_ptr = lli_array;
		sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages =
			num_pages;
		sep->dma_res_arr[sep->nr_dcb_creat].out_page_array =
			page_array;
		sep->dma_res_arr[sep->nr_dcb_creat].out_map_array =
			map_array;
		sep->dma_res_arr[sep->nr_dcb_creat].out_map_num_entries =
			num_pages;
	}

	goto end_function;

end_function_with_error3:

	/* free lli array */
	kfree(lli_array);

end_function_with_error2:

	kfree(map_array);

end_function_with_error1:

	/* free page array */
	kfree(page_array);

end_function:

	dev_dbg(&sep->pdev->dev,
		"sep_lock_user_pages end\n");

	return error;
}

/*
	this function calculates the size of data
	that can be inserted into the lli
	table from this array the condition is that
	either the table is full
	(all etnries are entered), or there are no more
	entries in the lli array
		@sep: pointer to struct sep_device
		@lli_in_array_ptr
		@num_array_entries
		@last_table_flag
*/
static u32 sep_calculate_lli_table_max_size(struct sep_device *sep,
	struct sep_lli_entry_t *lli_in_array_ptr,
	u32 num_array_entries,
	u32 *last_table_flag)
{
	/* table data size */
	u32 table_data_size;

	/* data size for the next table */
	u32 next_table_data_size;

	/* counter */
	u32 counter;

	/*---------------------
	CODE
	----------------------*/

	table_data_size = 0;
	*last_table_flag = 0;

	/* calculate the data in the out lli table till we fill the whole
	table or till the data has ended */
	for (counter = 0;
		(counter < (SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP - 1)) &&
		(counter < num_array_entries); counter++)
		table_data_size += lli_in_array_ptr[counter].block_size;

	/* check if we reached the last entry,
	   meaning this ia the last table to build,
	   and no need to check the block alignment */
	if (counter == num_array_entries) {

		/* set the last table flag */
		*last_table_flag = 1;
		goto end_function;
	}

	/* calculate the data size of the next table. Stop if no entries left or
		 if data size is more the DMA restriction
	*/
	next_table_data_size = 0;
	for (; counter < num_array_entries; counter++) {

		next_table_data_size += lli_in_array_ptr[counter].block_size;

		if (next_table_data_size >= SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE)

			break;
	}

	/* check if the next table data size is less then DMA rstriction.
		if it is - recalculate the current table size, so that the next
		table data size will be adaquete for DMA
	*/
	if (next_table_data_size &&
		next_table_data_size < SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE)

		table_data_size -= (SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE -
			next_table_data_size);

	dev_dbg(&sep->pdev->dev,
		"table data size is %x\n", table_data_size);

end_function:

	return table_data_size;
}

/*
  this functions builds ont lli table from the lli_array according to
  the given size of data
	@sep: pointer to struct sep_device
	@lli_array_ptr: pointer to lli array
	@lli_table_ptr: pointer to lli table
	@num_processed_entries_ptr: pointer to number of entries
	@num_table_entries_ptr: pointer to number of tables
	@table_data_size: total data size
*/
static void sep_build_lli_table(struct sep_device *sep,
	struct sep_lli_entry_t	*lli_array_ptr,
	struct sep_lli_entry_t	*lli_table_ptr,
	u32 *num_processed_entries_ptr,
	u32 *num_table_entries_ptr,
	u32 table_data_size)
{
	/* current table data size */
	u32 curr_table_data_size;

	/* counter of lli array entry */
	u32 array_counter;

	/*-----------------------
	CODE
	---------------------------*/

	dev_dbg(&sep->pdev->dev,
		"sep_build_lli_table start\n");

	/* init currrent table data size and lli array entry counter */
	curr_table_data_size = 0;
	array_counter = 0;
	*num_table_entries_ptr = 1;

	dev_dbg(&sep->pdev->dev,
		"table_data_size is %x\n", table_data_size);

	/* fill the table till table size reaches the needed amount */
	while (curr_table_data_size < table_data_size) {
		/* update the number of entries in table */
		(*num_table_entries_ptr)++;

		lli_table_ptr->bus_address =
			cpu_to_le32(lli_array_ptr[array_counter].bus_address);

		lli_table_ptr->block_size =
			cpu_to_le32(lli_array_ptr[array_counter].block_size);

		curr_table_data_size += lli_array_ptr[array_counter].block_size;

		dev_dbg(&sep->pdev->dev,
			"lli_table_ptr is %08x\n", (u32)lli_table_ptr);
		dev_dbg(&sep->pdev->dev,
			"lli_table_ptr->bus_address is %08x\n",
			lli_table_ptr->bus_address);
		dev_dbg(&sep->pdev->dev,
			"lli_table_ptr->block_size is %x\n",
			lli_table_ptr->block_size);

		/* check for overflow of the table data */
		if (curr_table_data_size > table_data_size) {

			dev_dbg(&sep->pdev->dev,
				"curr_table_data_size too large\n");

			/* update the size of block in the table */
			lli_table_ptr->block_size -=
			cpu_to_le32((curr_table_data_size - table_data_size));

			/* update the physical address in the lli array */
			lli_array_ptr[array_counter].bus_address +=
			cpu_to_le32(lli_table_ptr->block_size);

			/* update the block size left in the lli array */
			lli_array_ptr[array_counter].block_size =
				(curr_table_data_size - table_data_size);

		} else

			/* advance to the next entry in the lli_array */
			array_counter++;

		dev_dbg(&sep->pdev->dev,
			"lli_table_ptr->bus_address is %08x\n",
			lli_table_ptr->bus_address);
		dev_dbg(&sep->pdev->dev,
			"lli_table_ptr->block_size is %x\n",
			lli_table_ptr->block_size);

		/* move to the next entry in table */
		lli_table_ptr++;
	}

	/* set the info entry to default */
	lli_table_ptr->bus_address = 0xffffffff;
	lli_table_ptr->block_size = 0;

	dev_dbg(&sep->pdev->dev,
		"lli_table_ptr is %08x\n", (u32)lli_table_ptr);
	dev_dbg(&sep->pdev->dev,
		"lli_table_ptr->bus_address is %08x\n",
		lli_table_ptr->bus_address);
	dev_dbg(&sep->pdev->dev,
		"lli_table_ptr->block_size is %x\n",
		lli_table_ptr->block_size);

	/* set the output parameter */
	*num_processed_entries_ptr += array_counter;

	dev_dbg(&sep->pdev->dev,
		"num_processed_entries_ptr is %x\n",
		*num_processed_entries_ptr);

	dev_dbg(&sep->pdev->dev,
		"sep_build_lli_table end\n");

	return;
}

/*
  This functions returns the physical address inside shared area according
  to the virtual address. It can be either on the externa RAM device
  (ioremapped), or on the system RAM
  This implementation is for the external RAM
*/
static dma_addr_t sep_shared_area_virt_to_bus(struct sep_device *sep,
	void *virt_address)
{
	dev_dbg(&sep->pdev->dev,
		"sh virt to phys v %08lx\n",
		(unsigned long)virt_address);
	dev_dbg(&sep->pdev->dev,
		"sh virt to phys p %08lx\n",
		(unsigned long)(sep->shared_bus
		+ (virt_address - sep->shared_addr)));

	return sep->shared_bus
		+ (size_t)(virt_address - sep->shared_addr);
}

/*
  This functions returns the virtual address inside shared area
  according to the physical address. It can be either on the
  externa RAM device (ioremapped), or on the system RAM This implementation
  is for the external RAM
*/

static void *sep_shared_area_bus_to_virt(struct sep_device *sep,
	dma_addr_t bus_address)
{
	dev_dbg(&sep->pdev->dev,
		"shared bus to virt b=%x v=%x\n",
		(u32)bus_address,
		(u32)(sep->shared_addr +
			(size_t)(bus_address - sep->shared_bus)));

	return sep->shared_addr
		+ (size_t)(bus_address - sep->shared_bus);
}

/*
  this function goes over the list of the print created tables and
  prints all the data
	@sep: pointer to struct sep_device
	@lli_table_ptr: pointer to sep_lli_entry_t
	@num_table_entries: number of entries
	@table_data_size: total data size
*/
static void sep_debug_print_lli_tables(struct sep_device *sep,
	struct sep_lli_entry_t *lli_table_ptr,
	unsigned long num_table_entries,
	unsigned long table_data_size)
{
	unsigned long table_count = 0;
	unsigned long entries_count = 0;

	dev_dbg(&sep->pdev->dev,
		"sep_debug_print_lli_tables start\n");

	table_count = 1;
	while ((unsigned long) lli_table_ptr != 0xffffffff) {
		dev_dbg(&sep->pdev->dev,
			"lli table %08lx, table_data_size is %lu\n",
			table_count, table_data_size);
		dev_dbg(&sep->pdev->dev,
			"num_table_entries is %lu\n",
			num_table_entries);

		/* print entries of the table (without info entry) */
		for (entries_count = 0; entries_count < num_table_entries;
			entries_count++, lli_table_ptr++) {

			dev_dbg(&sep->pdev->dev,
				"lli_table_ptr address is %08lx\n",
				(unsigned long) lli_table_ptr);

			dev_dbg(&sep->pdev->dev,
				"phys address is %08lx block size is %x\n",
				(unsigned long)lli_table_ptr->bus_address,
				lli_table_ptr->block_size);
		}

		/* point to the info entry */
		lli_table_ptr--;

		dev_dbg(&sep->pdev->dev,
			"phys lli_table_ptr->block_size is %x\n",
			lli_table_ptr->block_size);

		dev_dbg(&sep->pdev->dev,
			"phys lli_table_ptr->physical_address is %08lu\n",
			(unsigned long)lli_table_ptr->bus_address);


		table_data_size = lli_table_ptr->block_size & 0xffffff;
		num_table_entries = (lli_table_ptr->block_size >> 24) & 0xff;
		lli_table_ptr = (struct sep_lli_entry_t *)
			(lli_table_ptr->bus_address);

		dev_dbg(&sep->pdev->dev,
			"phys table_data_size is %lu num_table_entries is"
			" %lu lli_table_ptr is%lu\n", table_data_size,
			num_table_entries, (unsigned long)lli_table_ptr);

		if ((unsigned long)lli_table_ptr != 0xffffffff)
			lli_table_ptr = (struct sep_lli_entry_t *)
				sep_shared_bus_to_virt(sep,
				(unsigned long)lli_table_ptr);

		table_count++;
	}
	dev_dbg(&sep->pdev->dev,
			"sep_debug_print_lli_tables end\n");
}


/*
	This function creates empty lli tables when there is no data
	@sep: pointer to struct sep_device
	@lli_table_addr_ptr: pointer to lli table
	@num_entries_ptr: pointer to number of entries
	@table_data_size_ptr: point to table data size
*/
static void sep_prepare_empty_lli_table(struct sep_device *sep,
		dma_addr_t *lli_table_addr_ptr,
		u32 *num_entries_ptr,
		u32 *table_data_size_ptr)
{
	/* lli table pointer */
	struct sep_lli_entry_t *lli_table_ptr;

	/*---------------------
		  CODE
	------------------------*/

	dev_dbg(&sep->pdev->dev, "sep_prepare_empty_lli_table start\n");

	/* find the area for new table */
	lli_table_ptr =
		(struct sep_lli_entry_t *)(sep->shared_addr +
		SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
		sep->num_lli_tables_created *
		sizeof(struct sep_lli_entry_t) *
		SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP);

	lli_table_ptr->bus_address = 0;
	lli_table_ptr->block_size = 0;

	lli_table_ptr++;
	lli_table_ptr->bus_address = 0xFFFFFFFF;
	lli_table_ptr->block_size = 0;

	/* set the output parameter value */
	*lli_table_addr_ptr = sep->shared_bus +
		SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
		sep->num_lli_tables_created *
		sizeof(struct sep_lli_entry_t) *
		SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;

	/* set the num of entries and table data size for empty table */
	*num_entries_ptr = 2;
	*table_data_size_ptr = 0;

	/* update the number of created tables */
	sep->num_lli_tables_created++;

	dev_dbg(&sep->pdev->dev, "sep_prepare_empty_lli_table start\n");

}

/*
	This function prepares only input DMA table for synhronic symmetric
	operations (HASH)
*/
static int sep_prepare_input_dma_table(struct sep_device *sep,
	unsigned long app_virt_addr,
	u32 data_size,
	u32 block_size,
	dma_addr_t *lli_table_ptr,
	u32 *num_entries_ptr,
	u32 *table_data_size_ptr,
	bool isKernelVirtualAddress)

{
	/* pointer to the info entry of the table - the last entry */
	struct sep_lli_entry_t *info_entry_ptr;

	/* array of pointers ot page */
	struct sep_lli_entry_t *lli_array_ptr;

	/* points to the first entry to be processed in the lli_in_array */
	u32 current_entry = 0;

	/* num entries in the virtual buffer */
	u32 sep_lli_entries = 0;

	/* lli table pointer */
	struct sep_lli_entry_t *in_lli_table_ptr;

	/* the total data in one table */
	u32 table_data_size = 0;

	/* flag for last table */
	u32 last_table_flag = 0;

	/* number of entries in lli table */
	u32 num_entries_in_table = 0;

	/* next table address */
	u32 lli_table_alloc_addr = 0;

	/* error */
	int error = 0;

	/*------------------------
	CODE
	--------------------------*/

	dev_dbg(&sep->pdev->dev,
		"sep_prepare_input_dma_table start\n");

	dev_dbg(&sep->pdev->dev,
		"data_size is %x\n", data_size);
	dev_dbg(&sep->pdev->dev,
		"block_size is %x\n", block_size);

	/* initialize the pages pointers */
	sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = 0;
	sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages = 0;

	/* set the kernel address for first table to be allocated */
	lli_table_alloc_addr = (u32)(sep->shared_addr +
		SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
		sep->num_lli_tables_created *
		sizeof(struct sep_lli_entry_t) *
		SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP);

	if (data_size == 0) {

		/* special case  - create meptu table - 2 entries, zero data */
		sep_prepare_empty_lli_table(sep, lli_table_ptr,
			num_entries_ptr,
			table_data_size_ptr);

		goto update_dcb_counter;
	}

	/* check if the pages are in Kernel Virtual Address layout */
	if (isKernelVirtualAddress == true)

		/* lock the pages of the kernel
		   buffer and translate them to pages */
		error = sep_lock_kernel_pages(sep, app_virt_addr,
			data_size, &lli_array_ptr, SEP_DRIVER_IN_FLAG);
	else

		/* lock the pages of the user buffer
		   and translate them to pages */
		error = sep_lock_user_pages(sep, app_virt_addr,
			data_size, &lli_array_ptr, SEP_DRIVER_IN_FLAG);

	if (error)
		goto end_function;

	dev_dbg(&sep->pdev->dev,
		"output sep_in_num_pages is %x\n",
		sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages);

	current_entry = 0;
	info_entry_ptr = 0;

	sep_lli_entries =
		sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages;

	/* loop till all the entries in in array are not processed */
	while (current_entry < sep_lli_entries) {

		/* set the new input and output tables */
		in_lli_table_ptr =
			(struct sep_lli_entry_t *)lli_table_alloc_addr;

		lli_table_alloc_addr += sizeof(struct sep_lli_entry_t) *
			SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;

		if (lli_table_alloc_addr >
			((u32)sep->shared_addr +
			SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
			SYNCHRONIC_DMA_TABLES_AREA_SIZE_BYTES)) {

			error = -ENOMEM;
			goto end_function_error;

		}

		/* update the number of created tables */
		sep->num_lli_tables_created++;

		/* calculate the maximum size of data for input table */
		table_data_size = sep_calculate_lli_table_max_size(sep,
			&lli_array_ptr[current_entry],
			(sep_lli_entries - current_entry),
			&last_table_flag);

		/* if this is not the last table -
		   then allign it to the block size */
		if (!last_table_flag)
			table_data_size =
			(table_data_size / block_size) * block_size;

		dev_dbg(&sep->pdev->dev,
			"output table_data_size is %x\n",
			table_data_size);

		/* construct input lli table */
		sep_build_lli_table(sep, &lli_array_ptr[current_entry],
			in_lli_table_ptr,
			&current_entry, &num_entries_in_table, table_data_size);

		if (info_entry_ptr == 0) {

			/* set the output parameters to physical addresses */
			*lli_table_ptr = sep_shared_area_virt_to_bus(sep,
				in_lli_table_ptr);
			*num_entries_ptr = num_entries_in_table;
			*table_data_size_ptr = table_data_size;

			dev_dbg(&sep->pdev->dev,
				"output lli_table_in_ptr is %08x\n",
				(u32)*lli_table_ptr);

		}

		else {

			/* update the info entry of the previous in table */
			info_entry_ptr->bus_address =
				sep_shared_area_virt_to_bus(sep,
				in_lli_table_ptr);
				info_entry_ptr->block_size =
				((num_entries_in_table) << 24) |
				(table_data_size);
		}

		/* save the pointer to the info entry of the current tables */
		info_entry_ptr = in_lli_table_ptr + num_entries_in_table - 1;

	}

	/* print input tables */
	sep_debug_print_lli_tables(sep, (struct sep_lli_entry_t *)
		sep_shared_area_bus_to_virt(sep, *lli_table_ptr),
		*num_entries_ptr,
		*table_data_size_ptr);

	/* the array of the pages */
	kfree(lli_array_ptr);

update_dcb_counter:

	/* update dcb counter */
	sep->nr_dcb_creat++;

	goto end_function;

end_function_error:

	/* free all the allocated resources */
	kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_map_array);
	kfree(lli_array_ptr);
	kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_page_array);

end_function:

	dev_dbg(&sep->pdev->dev,
		"sep_prepare_input_dma_table end\n");

	return error;

}
/*
 This function creates the input and output dma tables for
 symmetric operations (AES/DES) according to the block size from LLI arays
	@sep: pointer to struct_sep
	@lli_in_array:
	@sep_in_lli_entries:
	@lli_out_array:
	@sep_out_lli_entries
	@block_size
	@lli_table_in_ptr
	@lli_table_out_ptr
	@in_num_entries_ptr
	@out_num_entries_ptr
	@table_data_size_ptr
*/
static int sep_construct_dma_tables_from_lli(
	struct sep_device *sep,
	struct sep_lli_entry_t *lli_in_array,
	u32	sep_in_lli_entries,
	struct sep_lli_entry_t *lli_out_array,
	u32	sep_out_lli_entries,
	u32	block_size,
	dma_addr_t *lli_table_in_ptr,
	dma_addr_t *lli_table_out_ptr,
	u32	*in_num_entries_ptr,
	u32	*out_num_entries_ptr,
	u32	*table_data_size_ptr)
{
	/* points to the area where next lli table can be allocated */
	u32 lli_table_alloc_addr = 0;

	/* input lli table */
	struct sep_lli_entry_t *in_lli_table_ptr = 0;

	/* output lli table */
	struct sep_lli_entry_t *out_lli_table_ptr = 0;

	/* pointer to the info entry of the table - the last entry */
	struct sep_lli_entry_t *info_in_entry_ptr = 0;

	/* pointer to the info entry of the table - the last entry */
	struct sep_lli_entry_t *info_out_entry_ptr = 0;

	/* points to the first entry to be processed in the lli_in_array */
	u32 current_in_entry = 0;

	/* points to the first entry to be processed in the lli_out_array */
	u32 current_out_entry = 0;

	/* max size of the input table */
	u32 in_table_data_size = 0;

	/* max size of the output table */
	u32 out_table_data_size = 0;

	/* flag te signifies if this is the last tables build */
	u32 last_table_flag = 0;

	/* the data size that should be in table */
	u32 table_data_size = 0;

	/* number of etnries in the input table */
	u32 num_entries_in_table = 0;

	/* number of etnries in the output table */
	u32 num_entries_out_table = 0;

	/*---------------------
	CODE
	------------------------*/

	dev_dbg(&sep->pdev->dev,
		"sep_construct_dma_tables_from_lli start\n");

	/* initiate to point after the message area */
	lli_table_alloc_addr = (u32)(sep->shared_addr +
		SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
		(sep->num_lli_tables_created *
		(sizeof(struct sep_lli_entry_t) *
		SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP)));

	/* loop till all the entries in in array are not processed */
	while (current_in_entry < sep_in_lli_entries) {

		/* set the new input and output tables */
		in_lli_table_ptr =
			(struct sep_lli_entry_t *)lli_table_alloc_addr;

		lli_table_alloc_addr += sizeof(struct sep_lli_entry_t) *
			SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;

		/* set the first output tables */
		out_lli_table_ptr =
			(struct sep_lli_entry_t *)lli_table_alloc_addr;

		/* check if the DMA table area limit was overrun */
		if ((lli_table_alloc_addr + sizeof(struct sep_lli_entry_t) *
			SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP) >
			((u32)sep->shared_addr +
			SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
			SYNCHRONIC_DMA_TABLES_AREA_SIZE_BYTES)) {

			dev_warn(&sep->pdev->dev,
				"dma table limit overrun\n");
			return -ENOMEM;
		}

		/* update the number of the lli tables created */
		sep->num_lli_tables_created += 2;

		lli_table_alloc_addr += sizeof(struct sep_lli_entry_t) *
			SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;

		/* calculate the maximum size of data for input table */
		in_table_data_size =
			sep_calculate_lli_table_max_size(sep,
			&lli_in_array[current_in_entry],
			(sep_in_lli_entries - current_in_entry),
			&last_table_flag);

		/* calculate the maximum size of data for output table */
		out_table_data_size =
			sep_calculate_lli_table_max_size(sep,
			&lli_out_array[current_out_entry],
			(sep_out_lli_entries - current_out_entry),
			&last_table_flag);

		dev_dbg(&sep->pdev->dev,
			"in_table_data_size is %x\n",
			in_table_data_size);

		dev_dbg(&sep->pdev->dev,
			"out_table_data_size is %x\n",
			out_table_data_size);

		table_data_size = in_table_data_size;

		if (!last_table_flag) {
			/* if this is not the last table,
			   then must check where the data is smallest
			   and then align it to the block size */
			if (table_data_size > out_table_data_size)
				table_data_size = out_table_data_size;

			/* now calculate the table size so that
			   it will be module block size */
			table_data_size = (table_data_size / block_size) *
				block_size;
		}

		dev_dbg(&sep->pdev->dev,
			"table_data_size is %x\n", table_data_size);

		/* construct input lli table */
		sep_build_lli_table(sep, &lli_in_array[current_in_entry],
			in_lli_table_ptr,
			&current_in_entry,
			&num_entries_in_table,
			table_data_size);

		/* construct output lli table */
		sep_build_lli_table(sep, &lli_out_array[current_out_entry],
			out_lli_table_ptr,
			&current_out_entry,
			&num_entries_out_table,
			table_data_size);

		/* if info entry is null - this is the first table built */
		if (info_in_entry_ptr == 0) {
			/* set the output parameters to physical addresses */
			*lli_table_in_ptr =
			sep_shared_area_virt_to_bus(sep, in_lli_table_ptr);

			*in_num_entries_ptr = num_entries_in_table;

			*lli_table_out_ptr =
				sep_shared_area_virt_to_bus(sep,
				out_lli_table_ptr);

			*out_num_entries_ptr = num_entries_out_table;
			*table_data_size_ptr = table_data_size;

			dev_dbg(&sep->pdev->dev,
			"output lli_table_in_ptr is %08x\n",
				(u32)*lli_table_in_ptr);
			dev_dbg(&sep->pdev->dev,
			"output lli_table_out_ptr is %08x\n",
				(u32)*lli_table_out_ptr);
		} else {
			/* update the info entry of the previous in table */
			info_in_entry_ptr->bus_address =
				sep_shared_area_virt_to_bus(sep,
				in_lli_table_ptr);

			info_in_entry_ptr->block_size =
				((num_entries_in_table) << 24) |
				(table_data_size);

			/* update the info entry of the previous in table */
			info_out_entry_ptr->bus_address =
				sep_shared_area_virt_to_bus(sep,
				out_lli_table_ptr);

			info_out_entry_ptr->block_size =
				((num_entries_out_table) << 24) |
				(table_data_size);

			dev_dbg(&sep->pdev->dev,
				"output lli_table_in_ptr:%08x  %08x\n",
				(u32)info_in_entry_ptr->bus_address,
				info_in_entry_ptr->block_size);

			dev_dbg(&sep->pdev->dev,
				"output lli_table_out_ptr:%08x  %08x\n",
				(u32)info_out_entry_ptr->bus_address,
				info_out_entry_ptr->block_size);
		}

		/* save the pointer to the info entry of the current tables */
		info_in_entry_ptr = in_lli_table_ptr +
			num_entries_in_table - 1;
		info_out_entry_ptr = out_lli_table_ptr +
			num_entries_out_table - 1;

		dev_dbg(&sep->pdev->dev,
			"output num_entries_out_table is %x\n",
			(u32)num_entries_out_table);
		dev_dbg(&sep->pdev->dev,
			"output info_in_entry_ptr is %x\n",
			(u32)info_in_entry_ptr);
		dev_dbg(&sep->pdev->dev,
			"output info_out_entry_ptr is %x\n",
			(u32)info_out_entry_ptr);
	}

	/* print input tables */
	sep_debug_print_lli_tables(sep,
	(struct sep_lli_entry_t *)
	sep_shared_area_bus_to_virt(sep, *lli_table_in_ptr),
	*in_num_entries_ptr,
	*table_data_size_ptr);

	/* print output tables */
	sep_debug_print_lli_tables(sep,
	(struct sep_lli_entry_t *)
	sep_shared_area_bus_to_virt(sep, *lli_table_out_ptr),
	*out_num_entries_ptr,
	*table_data_size_ptr);

	dev_dbg(&sep->pdev->dev,
		"sep_construct_dma_tables_from_lli end\n");

	return 0;
}

/*
	This function builds input and output DMA tables for synhronic
	symmetric operations (AES, DES, HASH). It also checks that each table
	is of the modular block size
*/
static int sep_prepare_input_output_dma_table(struct sep_device *sep,
	unsigned long app_virt_in_addr,
	unsigned long app_virt_out_addr,
	u32 data_size,
	u32 block_size,
	dma_addr_t *lli_table_in_ptr,
	dma_addr_t *lli_table_out_ptr,
	u32 *in_num_entries_ptr,
	u32 *out_num_entries_ptr,
	u32 *table_data_size_ptr,
	bool isKernelVirtualAddress)

{
	/* array of pointers of page */
	struct sep_lli_entry_t *lli_in_array;

	/* array of pointers of page */
	struct sep_lli_entry_t *lli_out_array;

	/* error */
	int error = 0;

	/*------------------------
	CODE
	--------------------------*/

	dev_dbg(&sep->pdev->dev,
		"sep_prepare_input_output_dma_table start\n");

	if (data_size == 0) {

		/* prepare empty table for input and output */
		sep_prepare_empty_lli_table(sep, lli_table_in_ptr,
			in_num_entries_ptr, table_data_size_ptr);

		sep_prepare_empty_lli_table(sep, lli_table_out_ptr,
			out_num_entries_ptr, table_data_size_ptr);

		goto update_dcb_counter;
	}

	/* initialize the pages pointers */
	sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = 0;
	sep->dma_res_arr[sep->nr_dcb_creat].out_page_array = 0;

	/* check if the pages are in Kernel Virtual Address layout */
	if (isKernelVirtualAddress == true) {

		/* lock the pages of the kernel buffer and
		translate them to pages */
		error = sep_lock_kernel_pages(sep, app_virt_in_addr,
			data_size, &lli_in_array, SEP_DRIVER_IN_FLAG);

		if (error) {

			dev_warn(&sep->pdev->dev,
				"sep_lock_kernel_pages for input"
				" virtual buffer failed\n");
			goto end_function;

		}
	}

	else {

		/* lock the pages of the user buffer and
		translate them to pages */
		error = sep_lock_user_pages(sep, app_virt_in_addr,
			data_size, &lli_in_array, SEP_DRIVER_IN_FLAG);
		if (error) {
			dev_warn(&sep->pdev->dev,
				"sep_lock_user_pages for input virtual"
				" buffer failed\n");
			goto end_function;
		}
	}

	if (isKernelVirtualAddress == true) {
		error = sep_lock_kernel_pages(sep, app_virt_out_addr,
			data_size, &lli_out_array, SEP_DRIVER_OUT_FLAG);

		if (error) {
			dev_warn(&sep->pdev->dev,
				"sep_lock_kernel_pages \
				for output virtual buffer failed\n");
			goto end_function_free_lli_in;
		}

	}

	else {
		error = sep_lock_user_pages(sep, app_virt_out_addr,
			data_size, &lli_out_array, SEP_DRIVER_OUT_FLAG);

		if (error) {
			dev_warn(&sep->pdev->dev,
				"sep_lock_user_pages \
				for output virtual buffer failed\n");
			goto end_function_free_lli_in;
		}
	}

	dev_dbg(&sep->pdev->dev,
		"sep_in_num_pages is %x\n",
		sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages);
	dev_dbg(&sep->pdev->dev,
		"sep_out_num_pages is %x\n",
		sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages);
	dev_dbg(&sep->pdev->dev,
		"SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP is %x\n",
		SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP);


	/* call the fucntion that creates table from the lli arrays */
	error = sep_construct_dma_tables_from_lli(sep, lli_in_array,
		sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages,
		lli_out_array,
		sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages,
		block_size, lli_table_in_ptr, lli_table_out_ptr,
		in_num_entries_ptr, out_num_entries_ptr, table_data_size_ptr);

	if (error) {
		dev_warn(&sep->pdev->dev,
			"sep_construct_dma_tables_from_lli failed\n");
		goto end_function_with_error;
	}

	kfree(lli_out_array);
	kfree(lli_in_array);

update_dcb_counter:

	/* update dcb counter */
	sep->nr_dcb_creat++;

	/* fall through - free the lli entry arrays */
	dev_dbg(&sep->pdev->dev,
		"in_num_entries_ptr is %08x\n", *in_num_entries_ptr);
	dev_dbg(&sep->pdev->dev,
		"out_num_entries_ptr is %08x\n", *out_num_entries_ptr);
	dev_dbg(&sep->pdev->dev,
		"table_data_size_ptr is %08x\n", *table_data_size_ptr);

	goto end_function;

end_function_with_error:

	kfree(sep->dma_res_arr[sep->nr_dcb_creat].out_map_array);
	kfree(sep->dma_res_arr[sep->nr_dcb_creat].out_page_array);
	kfree(lli_out_array);


end_function_free_lli_in:

	kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_map_array);
	kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_page_array);
	kfree(lli_in_array);

end_function:

	dev_dbg(&sep->pdev->dev,
		"sep_prepare_input_output_dma_table"
		" end result = %d\n", error);

	return error;

}

/*
 This function prepares the linked dma tables and puts the
 address for the linked list of tables inta a dcb (data control
 block) the address of which is known by the sep hardware
	@app_in_address: unsigned long; for data buffer in (user space)
	@app_out_address: unsigned long; for data buffer out (user space)
	@data_in_size: u32; for size of data
	@block_size: u32; for block size
	@tail_block_size: u32; for size of tail block
	@isApplet: bool; to indicate external app
	@isKernelVirtualAddress: bool; to indicate kernel space
 */
static int sep_prepare_input_output_dma_table_in_dcb(struct sep_device *sep,
	unsigned long  app_in_address,
	unsigned long  app_out_address,
	u32              data_in_size,
	u32              block_size,
	u32              tail_block_size,
	bool            isApplet,
	bool            isKernelVirtualAddress)

{
	/* error */
	int error = 0;

	/* size of tail */
	u32 tail_size = 0;

	/* address of the created dcb table */
	struct sep_dcb_t *dcb_table_ptr = 0;

	/* the physical address of the first input DMA table */
	dma_addr_t in_first_mlli_address = 0;

	/* number of entries in the first input DMA table */
	u32  in_first_num_entries = 0;

	/* the physical address of the first output DMA table */
	dma_addr_t  out_first_mlli_address = 0;

	/* number of entries in the first output DMA table */
	u32  out_first_num_entries = 0;

	/* data in the first input/output table */
	u32  first_data_size = 0;

	dev_dbg(&sep->pdev->dev,
		"prepare_input_output_dma_table_in_dcb start\n");

	if (sep->nr_dcb_creat == SEP_MAX_NUM_SYNC_DMA_OPS) {

		/*No more DCBS to allocate*/
		dev_warn(&sep->pdev->dev, "no more dcb's available\n");
		error = -EFAULT;
		goto end_function;
	}

	/* allocate new DCB */
	dcb_table_ptr = (struct sep_dcb_t *)(sep->shared_addr +
		SEP_DRIVER_SYSTEM_DCB_MEMORY_OFFSET_IN_BYTES +
		(sep->nr_dcb_creat * sizeof(struct sep_dcb_t)));

	/* set the default values in the dcb */
	dcb_table_ptr->input_mlli_address = 0;
	dcb_table_ptr->input_mlli_num_entries = 0;
	dcb_table_ptr->input_mlli_data_size = 0;
	dcb_table_ptr->output_mlli_address = 0;
	dcb_table_ptr->output_mlli_num_entries = 0;
	dcb_table_ptr->output_mlli_data_size = 0;
	dcb_table_ptr->tail_data_size = 0;
	dcb_table_ptr->out_vr_tail_pt = 0;

	if (isApplet == true) {
		tail_size = data_in_size % block_size;

		if (tail_size) {

			if (data_in_size < tail_block_size) {
				dev_warn(&sep->pdev->dev,
					"data in size smaller than tail"
					"  block size\n");
				error = -EFAULT;
				goto end_function;
			}

			if (tail_block_size)
				/* case the tail size should be
				   bigger than the real block size */
				tail_size = tail_block_size +
					((data_in_size -
					tail_block_size) % block_size);
		}

		/* check if there is enough data for dma operation */
		if (data_in_size < SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE) {

			if (isKernelVirtualAddress == true) {
				memcpy(dcb_table_ptr->tail_data,
					(void *)app_in_address, data_in_size);
			}

			else {
				if (copy_from_user(dcb_table_ptr->tail_data,
					(void *)app_in_address,
					data_in_size)) {

					dev_warn(&sep->pdev->dev,
						"failed to copy tail\n");
					error = -EFAULT;
					goto end_function;
				}
			}

			dcb_table_ptr->tail_data_size = data_in_size;

			/* set the output user-space address for mem2mem op */
			if (app_out_address)
				dcb_table_ptr->out_vr_tail_pt =
					(void *)app_out_address;

			/*Update both data length parameters in order to avoid
			  second data copy and allow building of empty mlli
			  tables */
			tail_size = 0x0;
			data_in_size = 0x0;
		}

		if (tail_size) {

			if (isKernelVirtualAddress == true) {
				memcpy(dcb_table_ptr->tail_data,
					(void *)(app_in_address + data_in_size -
					tail_size), tail_size);
			}

			else {
				/* we have tail data - copy it to dcb */
				if (copy_from_user(dcb_table_ptr->tail_data,
					(void *)(app_in_address +
					data_in_size - tail_size),
					tail_size)) {


					dev_warn(&sep->pdev->dev,
						"failed to copy tail\n");
					error = -EFAULT;
					goto end_function;
				}
			}

			if (app_out_address)
				/*Caclulate the output address
				  according to tail data size.*/
				dcb_table_ptr->out_vr_tail_pt =
					(void *)(app_out_address +
					data_in_size - tail_size);

			/*save the real tail data size*/
			dcb_table_ptr->tail_data_size = tail_size;
			/*Update the data size without the tail
			  data size AKA data for the dma*/
			data_in_size = (data_in_size - tail_size);
		}
	}

	/* create the MLLI tables */
	/* check if we need to build only input table or input/output */
	if (app_out_address) {
		/* prepare input/output tables */
		error = sep_prepare_input_output_dma_table(sep,
			app_in_address,
			app_out_address,
			data_in_size,
			block_size,
			&in_first_mlli_address,
			&out_first_mlli_address,
			&in_first_num_entries,
			&out_first_num_entries,
			&first_data_size,
			isKernelVirtualAddress);
	}

	else {
		/* prepare input tables */
		error = sep_prepare_input_dma_table(sep,
			app_in_address,
			data_in_size,
			block_size,
			&in_first_mlli_address,
			&in_first_num_entries,
			&first_data_size,
			isKernelVirtualAddress);
	}

	if (error) {
		dev_warn(&sep->pdev->dev, "prepare dma table call failed"
			" from prepare dcb call\n");
		goto end_function;
	}

	/* set the dcb values */
	dcb_table_ptr->input_mlli_address = in_first_mlli_address;
	dcb_table_ptr->input_mlli_num_entries = in_first_num_entries;
	dcb_table_ptr->input_mlli_data_size = first_data_size;
	dcb_table_ptr->output_mlli_address = out_first_mlli_address;
	dcb_table_ptr->output_mlli_num_entries = out_first_num_entries;
	dcb_table_ptr->output_mlli_data_size = first_data_size;

end_function:

	dev_dbg(&sep->pdev->dev,
		"sep_prepare_input_output_dma_table_in_dcb end\n");
	return error;

}


/*
  this function handles tha request for creation of the DMA table
  for the synchronic symmetric operations (AES,DES)
	@sep: pointer to struct sep_device
	@arg: pointer to struct sep_driver_build_sync_table_t
*/
static int sep_create_sync_dma_tables_handler(struct sep_device *sep,
						unsigned long arg)
{
	int error = 0;

	/* command arguments */
	struct sep_driver_build_sync_table_t command_args;

	dev_dbg(&sep->pdev->dev,
		"sep_create_sync_dma_tables_handler start\n");

	error = copy_from_user(&command_args, (void *)arg,
		sizeof(struct sep_driver_build_sync_table_t));
	if (error) {
		dev_warn(&sep->pdev->dev, "create dma tables;"
			" copy_from_user fails\n");
		goto end_function;
	}

	dev_dbg(&sep->pdev->dev,
		"app_in_address is %08lx\n", command_args.app_in_address);
	dev_dbg(&sep->pdev->dev,
		"app_out_address is %08lx\n", command_args.app_out_address);
	dev_dbg(&sep->pdev->dev,
		"data_size is %u\n", command_args.data_in_size);
	dev_dbg(&sep->pdev->dev,
		"block_size is %u\n", command_args.block_size);

	/* validate user parameters */
	if (!command_args.app_in_address) {

		dev_warn(&sep->pdev->dev,
			"params validation error\n");

		error = -EINVAL;
		goto end_function;
	}

	error = sep_prepare_input_output_dma_table_in_dcb(sep,
		command_args.app_in_address,
		command_args.app_out_address,
		command_args.data_in_size,
		command_args.block_size,
		0x0,
		false,
		false);

end_function:

	dev_dbg(&sep->pdev->dev, "sep_create_sync_dma_tables_handler end\n");
	return error;
}

/*
	This function frees the dma tables and dcb block
	@sep: pointer to struct sep_device
	@isApplet: indicates external application
	@isKernelVirtualAddress: indicates kernel addresses
*/
static int sep_free_dma_tables_and_dcb(struct sep_device *sep, bool isApplet,
	bool isKernelVirtualAddress)
{

	int i = 0;
	int error = 0;
	int error_temp = 0;

	struct sep_dcb_t *dcb_table_ptr;

	/*----------------
	CODE
	-----------------*/

	dev_dbg(&sep->pdev->dev, "sep_free_dma_tables_and_dcb start\n");

	if (isApplet == true) {

		/* set pointer to first dcb table */
		dcb_table_ptr = (struct sep_dcb_t *)
			(sep->shared_addr +
			SEP_DRIVER_SYSTEM_DCB_MEMORY_OFFSET_IN_BYTES);

		/* go over each dcb and see if tail pointer must be updated */
		for (i = 0; i < sep->nr_dcb_creat; i++, dcb_table_ptr++) {

			if (dcb_table_ptr->out_vr_tail_pt) {

				if (isKernelVirtualAddress == true) {
					memcpy(dcb_table_ptr->out_vr_tail_pt,
						dcb_table_ptr->tail_data,
						dcb_table_ptr->tail_data_size);
				}

				else {
					error_temp = copy_to_user(
						dcb_table_ptr->out_vr_tail_pt,
						dcb_table_ptr->tail_data,
						dcb_table_ptr->tail_data_size);
				}

				if (error_temp) {
					/* we do not exist right away,
					   since we need to release
					   the dma resource */

					error = error_temp;
				}
			}
		}
	}

	/* free the output pages, if any */
	sep_free_dma_table_data_handler(sep);

	dev_dbg(&sep->pdev->dev, "sep_free_dma_tables_and_dcb end\n");
	return error;
}

/*
  this function sets the bus and virtual addresses of the static pool
  and returns the virtual address
*/
static int sep_get_static_pool_addr_handler(struct sep_device *sep,
	unsigned long arg)
{
	int error = 0;
	struct sep_driver_static_pool_addr_t command_args;

	u32 *static_pool_addr = 0;

	dev_dbg(&sep->pdev->dev, "sep_get_static_pool_addr_handler start\n");

	static_pool_addr = (u32 *)(sep->shared_addr +
		SEP_DRIVER_SYSTEM_RAR_MEMORY_OFFSET_IN_BYTES);

	static_pool_addr[0] = SEP_STATIC_POOL_VAL_TOKEN;
	static_pool_addr[1] = sep->shared_bus +
		SEP_DRIVER_STATIC_AREA_OFFSET_IN_BYTES;

	command_args.static_virt_address = sep->shared_addr +
		SEP_DRIVER_STATIC_AREA_OFFSET_IN_BYTES;

	dev_dbg(&sep->pdev->dev, "static pool: physical %x virtual %x\n",
		(u32)static_pool_addr[1],
		(u32)command_args.static_virt_address);

	/* send the parameters to user application */
	error = copy_to_user((void *) arg, &command_args,
		sizeof(struct sep_driver_static_pool_addr_t));

	dev_dbg(&sep->pdev->dev, "sep_get_static_pool_addr_handler end\n");

	return error;
}

/*
	This function starts the sep device
	@sep: pointer to struct sep_device
*/
static int sep_start_handler(struct sep_device *sep)
{
	unsigned long reg_val;
	unsigned long error = 0;

	dev_dbg(&sep->pdev->dev, "sep_start_handler start\n");

	/* wait in polling for message from SEP */
	do
		reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
	while (!reg_val);

	/* check the value */
	if (reg_val == 0x1)
		/* fatal error - read error status from GPRO */
		error = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
	dev_dbg(&sep->pdev->dev, "sep_start_handler end\n");
	return error;
}

/*
	This function performs a checsum for messages that are sent
	to the sep
*/
static u32 sep_check_sum_calc(u8 *data, u32 length)
{
	u32 sum = 0;
	u16 *Tdata = (u16 *)data;

	while (length > 1) {
		/*  This is the inner loop */
		sum += *Tdata++;
		length -= 2;
	}

	/*  Add left-over byte, if any */
	if (length > 0)
		sum += *(u8 *)Tdata;

	/*  Fold 32-bit sum to 16 bits */
	while (sum>>16)
		sum = (sum & 0xffff) + (sum >> 16);

	return ~sum & 0xFFFF;
}

/*
  this function handles the request for SEP initialization
*/
static int sep_init_handler(struct sep_device *sep, unsigned long arg)
{
	u32 message_buff[14];
	u32 counter;
	int error = 0;
	u32 reg_val;
	dma_addr_t new_base_addr;
	struct sep_driver_init_t command_args;

	dev_dbg(&sep->pdev->dev, "sep_init_handler start\n");
	error = 0;

	/* make sure that we have not initialized already */
	reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);

	if (reg_val != 0x2) {
		error = SEP_ALREADY_INITIALIZED_ERR;
		dev_warn(&sep->pdev->dev,
			"init; device already initialized\n");
		goto end_function;
	}

	/* only root can initialize */
	if (!capable(CAP_SYS_ADMIN)) {
		dev_warn(&sep->pdev->dev,
			"init; only root can init\n");
		error = -EACCES;
		goto end_function;
	}

	/* copy in the parameters */
	error = copy_from_user(&command_args, (void *) arg,
		sizeof(struct sep_driver_init_t));

	if (error) {
		dev_warn(&sep->pdev->dev,
			"init; copy_from_user failed %x\n", error);
		goto end_function;
	}

	/* validate parameters */
	if (!command_args.message_addr || !command_args.sep_sram_addr ||
		command_args.message_size_in_words > 14) {

		dev_warn(&sep->pdev->dev,
			"init; parameter error\n");
		error = -EINVAL;
		goto end_function;
	}

	/* copy in the sep init message */
	error = copy_from_user(message_buff,
		command_args.message_addr,
		command_args.message_size_in_words*sizeof(u32));

	if (error) {

		dev_warn(&sep->pdev->dev,
			"init; copy sep init message failed %x\n", error);
		goto end_function;
	}

	/* load resident, cache, and extapp firmware */
	error = sep_load_firmware(sep);

	if (error) {

		dev_warn(&sep->pdev->dev,
			"init; copy sep init message failed %x\n", error);
		goto end_function;
	}

	/* compute the base address */
	new_base_addr = sep->shared_bus;

	if (sep->resident_bus < new_base_addr)
		new_base_addr = sep->resident_bus;

	if (sep->cache_bus < new_base_addr)
		new_base_addr = sep->cache_bus;

	if (sep->dcache_bus < new_base_addr)
		new_base_addr = sep->dcache_bus;

	/* put physical addresses in sep message */
	message_buff[3] = (u32)new_base_addr;
	message_buff[4] = (u32)sep->shared_bus;
	message_buff[6] = (u32)sep->resident_bus;
	message_buff[7] = (u32)sep->cache_bus;
	message_buff[8] = (u32)sep->dcache_bus;

	message_buff[command_args.message_size_in_words - 1] = 0x0;
	message_buff[command_args.message_size_in_words - 1] =
		sep_check_sum_calc((u8 *)message_buff,
		command_args.message_size_in_words*sizeof(u32));

	/* debug print of message */
	for (counter = 0; counter < command_args.message_size_in_words;
		counter++)

		dev_dbg(&sep->pdev->dev,
			"init; sep message word %d is %x\n",
			counter, message_buff[counter]);

	/* tell the sep the sram address */
	sep_write_reg(sep, HW_SRAM_ADDR_REG_ADDR, command_args.sep_sram_addr);

	/* push the message to the sep */
	for (counter = 0; counter < command_args.message_size_in_words;
		counter++) {

		sep_write_reg(sep, HW_SRAM_DATA_REG_ADDR,
			message_buff[counter]);

		sep_wait_sram_write(sep);
	}

	/* signal sep that message is ready and to init */
	sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x1);

	/* wait for acknowledge */
	dev_dbg(&sep->pdev->dev, "init; waiting for msg response\n");

	do
		reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
	while (!(reg_val & 0xFFFFFFFD));

	if (reg_val == 0x1) {

		dev_warn(&sep->pdev->dev, "init; device int failed\n");
		error = sep_read_reg(sep, 0x8060);
		dev_warn(&sep->pdev->dev, "init; sw monitor is %x\n", error);
		error = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
		dev_warn(&sep->pdev->dev, "init; error is %x\n", error);
		goto end_function;
	}

	dev_dbg(&sep->pdev->dev, "init; end CC INIT, reg_val is %x\n",
		reg_val);

	/* signal sep to zero the GPR3 */
	sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x10);

	/* wait for response */
	dev_dbg(&sep->pdev->dev, "init; waiting for zero set response\n");

	do
		reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
	while (reg_val != 0);


end_function:
	dev_dbg(&sep->pdev->dev, "init is done\n");
	return error;
}

/*
	This API handles the end transaction request
	@sep: pointer to struct sep_device
*/
static int sep_end_transaction_handler(struct sep_device *sep)
{
	/*----------------------------
	CODE
	-----------------------------*/

	dev_dbg(&sep->pdev->dev, "sep_end_transaction_handler start\n");

	/* clear the data pool pointers Token */
	memset((void *)(sep->shared_addr +
		SEP_DRIVER_DATA_POOL_ALLOCATION_OFFSET_IN_BYTES),
		0, sep->num_of_data_allocations*2*sizeof(u32));

	/* check that all the dma resources were freed */
	sep_free_dma_table_data_handler(sep);

	clear_bit(SEP_MMAP_LOCK_BIT, &sep->in_use_flags);

	sep->pid_doing_transaction = 0;
	/* raise event for stuck contextes */
	wake_up(&sep->event);

	dev_dbg(&sep->pdev->dev, "waking up event\n");
	dev_dbg(&sep->pdev->dev, "sep_end_transaction_handler end\n");

	return 0;
}

/*
	This function will retrieve the RAR buffer physical addresses, type
	& size corresponding to the RAR handles provided in the buffers vector.
	@sep: pointer to struct sep_device
	@arg: pointer to user parameters
*/

static int sep_prepare_dcb_handler(struct sep_device *sep, unsigned long arg)
{
	/* error */
	int error = 0;

	/* command arguments */
	struct sep_driver_build_dcb_t command_args;

	/*--------------
			CODE
	-----------------*/
	dev_dbg(&sep->pdev->dev, "sep_prepare_dcb_handler start\n");

	/* get the command arguments */
	if (copy_from_user(&command_args, (void *)arg,
		sizeof(struct sep_driver_build_dcb_t))) {

		dev_warn(&sep->pdev->dev,
			"prepare dcb handler: copy_from_user failed\n");
		error = -EFAULT;
		goto end_function;
	}

	dev_dbg(&sep->pdev->dev,
		"app_in_address is %08lx\n", command_args.app_in_address);
	dev_dbg(&sep->pdev->dev,
		"app_out_address is %08lx\n", command_args.app_out_address);
	dev_dbg(&sep->pdev->dev,
		"data_size is %x\n", command_args.data_in_size);
	dev_dbg(&sep->pdev->dev,
		"block_size is %x\n", command_args.block_size);
	dev_dbg(&sep->pdev->dev,
		"tail block_size is %x\n", command_args.tail_block_size);

	error = sep_prepare_input_output_dma_table_in_dcb(sep,
		command_args.app_in_address, command_args.app_out_address,
		command_args.data_in_size, command_args.block_size,
		command_args.tail_block_size, true, false);

end_function:

	dev_dbg(&sep->pdev->dev, "sep_prepare_dcb_handler end\n");
	return error;

}

/*
	this function frees the DCB resources
	and updates the needed user-space buffers
	@sep: pointer to struct sep_device
*/
static int sep_free_dcb_handler(struct sep_device *sep)
{

	int error = 0;


	/*----------------
			CODE
	-----------------*/

	dev_dbg(&sep->pdev->dev, "sep_prepare_dcb_handler start\n");
	dev_dbg(&sep->pdev->dev, "num of DCBs %x\n", sep->nr_dcb_creat);

	error = sep_free_dma_tables_and_dcb(sep, false, false);

	dev_dbg(&sep->pdev->dev, "sep_free_dcb_handler end\n");
	return error;
}

/*
	This function will retrieve the RAR buffer physical addresses, type
	& size corresponding to the RAR handles provided in the buffers vector.
	@sep: pointer to struct sep_device
	@arg: pointer to user parameters
*/

static int sep_rar_prepare_output_msg_handler(struct sep_device *sep,
	unsigned long arg)
{
	/* error return code */
	int error = 0;

	/* command args */
	struct sep_driver_rar_handle_to_bus command_args;
	struct RAR_buffer rar_buf;

	/* bus address */
	dma_addr_t  rar_bus = 0;

	/* holds the RAR address in the system memory offset */
	u32 *rar_addr;

	/*------------------
	CODE
	---------------------*/

	dev_dbg(&sep->pdev->dev,
		"sep_rar_prepare_output_msg_handler start\n");

	/* copy the data */
	if (copy_from_user(&command_args,
		(void *)arg,
		sizeof(command_args))) {

		dev_warn(&sep->pdev->dev, "rar msg; copy from user error\n");
		error = -EFAULT;
		goto end_function;
	}

	/* call to translation function only if user handle is not NULL */
	if (command_args.rar_handle) {

		memset(&rar_buf, 0, sizeof(rar_buf));
		rar_buf.info.handle = (u32)command_args.rar_handle;

		if (rar_handle_to_bus(&rar_buf, 1) != 1) {
			dev_dbg(&sep->pdev->dev,
				"rar_handle_to_bus failure\n");
			error = -EFAULT;
			goto end_function;
		}

		rar_bus = rar_buf.bus_address;
	}

	dev_dbg(&sep->pdev->dev, "rar msg; rar_addr_bus = %x\n",
		(u32)rar_bus);

	/* set value in the SYSTEM MEMORY offset */
	rar_addr = (u32 *)(sep->shared_addr +
		SEP_DRIVER_SYSTEM_RAR_MEMORY_OFFSET_IN_BYTES);

	/* copy the physical address to the System Area.
		 The SEP will follow this address */
	rar_addr[0] = SEP_RAR_VAL_TOKEN;
	rar_addr[1] = rar_bus;

end_function:

	dev_dbg(&sep->pdev->dev,
		"sep_rar_prepare_output_msg_handler start\n");

	return error;
}

/*
	This function tells the sep where the extapp is located
	@sep: pointer to struct sep_device
	@arg: pointer to user parameters
*/
static int sep_realloc_ext_cache_handler(struct sep_device *sep,
	unsigned long arg)
{

	int error = 0;
	struct sep_driver_realloc_ext_cache_t   command_args;

	/* holds the new ext cache address in the system memory offset */
	u32 *system_addr;

	if (copy_from_user(&command_args, (void *)arg, sizeof(command_args))) {

		dev_warn(&sep->pdev->dev,
			"ext cache init: copy from user error\n");
		error = -EFAULT;
		goto end_function;

	}

	/* set value in the SYSTEM MEMORY offset */
	system_addr = (u32 *)(sep->shared_addr +
		SEP_DRIVER_SYSTEM_EXT_CACHE_ADDR_OFFSET_IN_BYTES);

	/* copy the physical address to the System Area.
	   which is used directly by the sep */

	system_addr[0] = SEP_EXT_CACHE_ADDR_VAL_TOKEN;
	dev_dbg(&sep->pdev->dev,
		"ext cache init; system addr 0 is %x\n", system_addr[0]);
	system_addr[1] = sep->extapp_bus;
	dev_dbg(&sep->pdev->dev,
		"ext cache init; system addr 1 is %x\n", system_addr[1]);

end_function:

	return error;
}


/*
 *	Ioctl API
 *	@filp: pointer to struct file
 *	@cmd: command
 *	@arg: pointer to argument structure
*/
static long sep_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	int error = 0;
	struct sep_device *sep = filp->private_data;

	dev_dbg(&sep->pdev->dev, "ioctl start\n");

	dev_dbg(&sep->pdev->dev, "cmd is %x\n", cmd);
	dev_dbg(&sep->pdev->dev,
		"SEP_IOCSENDSEPCOMMAND is %x\n", SEP_IOCSENDSEPCOMMAND);
	dev_dbg(&sep->pdev->dev,
		"SEP_IOCALLOCDATAPOLL is %x\n", SEP_IOCALLOCDATAPOLL);
	dev_dbg(&sep->pdev->dev,
		"SEP_IOCCREATESYMDMATABLE is %x\n", SEP_IOCCREATESYMDMATABLE);
	dev_dbg(&sep->pdev->dev,
		"SEP_IOCFREEDMATABLEDATA is %x\n", SEP_IOCFREEDMATABLEDATA);
	dev_dbg(&sep->pdev->dev,
		"SEP_IOCSEPSTART is %x\n", SEP_IOCSEPSTART);
	dev_dbg(&sep->pdev->dev,
		"SEP_IOCSEPINIT is %x\n", SEP_IOCSEPINIT);
	dev_dbg(&sep->pdev->dev,
		"SEP_IOCGETSTATICPOOLADDR is %x\n", SEP_IOCGETSTATICPOOLADDR);
	dev_dbg(&sep->pdev->dev,
		"SEP_IOCENDTRANSACTION is %x\n", SEP_IOCENDTRANSACTION);
	dev_dbg(&sep->pdev->dev,
		"SEP_IOCREALLOCEXTCACHE is %x\n", SEP_IOCREALLOCEXTCACHE);
	dev_dbg(&sep->pdev->dev,
		"SEP_IOCRARPREPAREMESSAGE is %x\n", SEP_IOCRARPREPAREMESSAGE);
	dev_dbg(&sep->pdev->dev,
		"SEP_IOCPREPAREDCB is %x\n", SEP_IOCPREPAREDCB);
	dev_dbg(&sep->pdev->dev,
		"SEP_IOCFREEDCB is %x\n", SEP_IOCFREEDCB);

	/* make sure we own this device */
	if ((current->pid != sep->pid_doing_transaction) &&
		(sep->pid_doing_transaction != 0)) {

		dev_dbg(&sep->pdev->dev, "ioctl pid is not owner\n");
		error = -EACCES;
		goto end_function;
	}

	/* check that the command is for sep device */
	if (_IOC_TYPE(cmd) != SEP_IOC_MAGIC_NUMBER) {
		dev_dbg(&sep->pdev->dev, "ioctl incorrect magic nbr\n");
		error = -EFAULT;
		goto end_function;
	}

	/* lock access to ioctl because we don't
	   want the daemon to interfere with operation */
	mutex_lock(&ioctl_mutex);

	switch (cmd) {
	case SEP_IOCSENDSEPCOMMAND:
		/* send command to SEP */
		sep_send_command_handler(sep);
		break;
	case SEP_IOCALLOCDATAPOLL:
		/* allocate data pool */
		error = sep_allocate_data_pool_memory_handler(sep, arg);
		break;
	case SEP_IOCCREATESYMDMATABLE:
		/* create dma table for synhronic operation */
		error = sep_create_sync_dma_tables_handler(sep, arg);
		break;
	case SEP_IOCFREEDMATABLEDATA:
		/* free the pages */
		error = sep_free_dma_table_data_handler(sep);
		break;
	case SEP_IOCSEPSTART:
		/* start command to sep */
		error = sep_start_handler(sep);
		break;
	case SEP_IOCSEPINIT:
		/* init command to sep */
		error = sep_init_handler(sep, arg);
		break;
	case SEP_IOCGETSTATICPOOLADDR:
		/* get the physical and virtual addresses of the static pool */
		error = sep_get_static_pool_addr_handler(sep, arg);
		break;
	case SEP_IOCENDTRANSACTION:
		error = sep_end_transaction_handler(sep);
		break;
	case SEP_IOCREALLOCEXTCACHE:
		if (sep->mrst)
			error = -ENODEV;
		else
			error = sep_realloc_ext_cache_handler(sep, arg);
		break;
	case SEP_IOCRARPREPAREMESSAGE:
		error = sep_rar_prepare_output_msg_handler(sep, arg);
		break;
	case SEP_IOCPREPAREDCB:
		error = sep_prepare_dcb_handler(sep, arg);
		break;
	case SEP_IOCFREEDCB:
		error = sep_free_dcb_handler(sep);
		break;
	default:
		error = -ENOTTY;
		break;
	}

	mutex_unlock(&ioctl_mutex);

end_function:

	dev_dbg(&sep->pdev->dev, "ioctl end\n");
	return error;
}

/*
	singleton ioctl
 *	@filp: pointer to struct file
 *	@cmd: command
 *	@arg: pointer to argument structure
*/
static long sep_singleton_ioctl(struct file  *filp, u32 cmd, unsigned long arg)
{

	/* error */
	long error;
	struct sep_device *sep = filp->private_data;

	/*------------------------
		CODE
	------------------------*/
	error = 0;

	dev_dbg(&sep->pdev->dev,
		"singleton_ioctl start\n");

	dev_dbg(&sep->pdev->dev,
		"cmd is %x\n", cmd);

	/* check that the command is for sep device */
	if (_IOC_TYPE(cmd) != SEP_IOC_MAGIC_NUMBER) {
		dev_warn(&sep->pdev->dev, "singleton ictl wrong magic\n");
		error =  -ENOTTY;
		goto end_function;
	}

	/* make sure we own this device */
	if ((current->pid != sep->pid_doing_transaction) &&
		(sep->pid_doing_transaction != 0)) {

		dev_dbg(&sep->pdev->dev, "singleton ioctl pid is not owner\n");
		error = -EACCES;
		goto end_function;
	}

	switch (cmd) {

	case SEP_IOCTLSETCALLERID:

		mutex_lock(&ioctl_mutex);
		error = sep_set_caller_id_handler(sep, arg);
		mutex_unlock(&ioctl_mutex);
		break;

	default:

		error = sep_ioctl(filp, cmd, arg);
		break;

	}

end_function:

	dev_dbg(&sep->pdev->dev, "singleton ioctl end\n");
	return error;
}

/**
 *	sep_request_daemon_ioctl - ioctl for daemon
 *	@filp: pointer to struct file
 *	@cmd: command
 *	@arg: pointer to argument structure
 *	Called by the request daemon to perform ioctls on the daemon device
 */

static long sep_request_daemon_ioctl(struct file *filp, u32 cmd,
	unsigned long arg)
{

	long error = 0;

	struct sep_device *sep = filp->private_data;

	/*------------------------
	CODE
	------------------------*/

	dev_dbg(&sep->pdev->dev,
		"daemon ioctl: start\n");

	dev_dbg(&sep->pdev->dev,
		"daemon ioctl: cmd is %x\n", cmd);

	/* check that the command is for sep device */
	if (_IOC_TYPE(cmd) != SEP_IOC_MAGIC_NUMBER) {
		dev_warn(&sep->pdev->dev,
			"daemon ioctl: wrong magic number\n");
		error = -EFAULT;
		goto end_function;
	}

	/* only one process can access ioctl at any given time */
	mutex_lock(&ioctl_mutex);

	switch (cmd) {
	case SEP_IOCSENDSEPRPLYCOMMAND:

		/* send reply command to SEP */
		error = sep_req_daemon_send_reply_command_handler(sep);
		break;

	case SEP_IOCENDTRANSACTION:

		/* end req daemon transaction, do nothing */
		error = sep_req_daemon_end_transaction_handler(sep, filp);
		break;

	default:
		dev_dbg(&sep->pdev->dev,
		"daemon ioctl: no such IOCTL\n");
		error = -ENOIOCTLCMD;
	}

	mutex_unlock(&ioctl_mutex);

end_function:

	dev_dbg(&sep->pdev->dev,
		"daemon ioctl: end\n");
	return error;

}

#if !SEP_DRIVER_POLLING_MODE
/*
 *	Inerrupt Handler
 *	@irq: interrupt
 *	@dev_id: device id
*/

static irqreturn_t sep_inthandler(int irq, void *dev_id)
{
	irqreturn_t int_error = IRQ_HANDLED;
	u32 reg_val, reg_val2 = 0;
	struct sep_device *sep = dev_id;

	/* read the IRR register to check if this is SEP interrupt */
	reg_val = sep_read_reg(sep, HW_HOST_IRR_REG_ADDR);
	dev_dbg(&sep->pdev->dev,
		"SEP Interrupt - reg is %08x\n", reg_val);

	if (reg_val & (0x1 << 13)) {

		/* update the counter of reply messages */
		/* must be locked */
		spin_lock_irqsave(&snd_rply_lck, snd_rply_lck_flag);
		sep->reply_ct++;
		spin_unlock_irqrestore(&snd_rply_lck, snd_rply_lck_flag);

		dev_dbg(&sep->pdev->dev, "sep int: send_ct %lx reply_ct %lx\n",
			sep->send_ct, sep->reply_ct);

		/* is this printf or daemon request? */
		reg_val2 = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
		dev_dbg(&sep->pdev->dev,
			"SEP Interrupt - reg2 is %08x\n", reg_val2);

		if ((reg_val2 >> 30) & 0x1) {

			dev_dbg(&sep->pdev->dev, "int: printf request\n");
			wake_up(&sep->event_request_daemon);
		}

		else if (reg_val2 >> 31) {

			dev_dbg(&sep->pdev->dev, "int: daemon request\n");
			wake_up(&sep->event_request_daemon);
		} else {

			dev_dbg(&sep->pdev->dev, "int: sep reply\n");
			wake_up(&sep->event);
		}

	} else {

		dev_dbg(&sep->pdev->dev, "int: not sep interrupt\n");
		int_error = IRQ_NONE;
	}

	if (int_error == IRQ_HANDLED)
		sep_write_reg(sep, HW_HOST_ICR_REG_ADDR, reg_val);

	return int_error;
}

#endif

/*
  Function that is called by rar_register when it is ready with
  a region (only for Moorestown)
  @sep_context_pointer: pointer to struct sep_device
*/
static int sep_callback(unsigned long sep_context_pointer)
{
	int error = 0;
	struct sep_device *sep =
		(struct sep_device *)sep_context_pointer;

	dma_addr_t rar_end_address = 0;

	dev_dbg(&sep->pdev->dev, "callback start\n");

	error = rar_get_address(RAR_TYPE_IMAGE, &sep->rar_bus,
		&rar_end_address);

	if (error) {
		dev_warn(&sep->pdev->dev, "mrst cant get rar region\n");
		goto end_function;
	}

	sep->rar_size = (size_t)(rar_end_address - sep->rar_bus + 1);

	if (!request_mem_region(sep->rar_bus, sep->rar_size,
		"sep_sec_driver")) {
		dev_warn(&sep->pdev->dev,
			"request mem region for mrst failed\n");
		error = -1;
		goto end_function;
	}

	sep->rar_addr = ioremap_nocache(sep->rar_bus, sep->rar_size);
	if (!sep->rar_addr) {
		dev_warn(&sep->pdev->dev,
			"ioremap nocache for mrst rar failed\n");
		error = -1;
		goto end_function;
	}

	dev_dbg(&sep->pdev->dev, "rar start is %p, phy is %p,"
		" size is %x\n", sep->rar_addr, (void *)sep->rar_bus,
		sep->rar_size);

	sep->probed = 1;

end_function:

	dev_dbg(&sep->pdev->dev, "callback end\n");
	return error;
}

/*
  Function that is activated on the successful probe of the SEP device
*/
static int __devinit sep_probe(struct pci_dev *pdev,
	const struct pci_device_id *ent)
{
	int error = 0;
	struct sep_device *sep;

	pr_debug("Sep pci probe starting\n");
	if (sep_dev != NULL) {
		dev_warn(&pdev->dev, "only one SEP supported.\n");
		return -EBUSY;
	}

	/* enable the device */
	error = pci_enable_device(pdev);
	if (error) {
		dev_warn(&pdev->dev, "error enabling pci device\n");
		goto end_function;
	}

	/* zero out sep structure */
	memset((void *)&sep_instance, 0, sizeof(sep_instance));

	/* set the pci dev pointer */
	sep_dev = &sep_instance;
	sep = &sep_instance;

	sep->pdev = pdev;

	if (pdev->device == MRST_PCI_DEVICE_ID)
		sep->mrst = 1;
	else
		sep->mrst = 0;

	dev_dbg(&sep->pdev->dev, "PCI obtained, device being prepared\n");

#if !defined(CONFIG_RAR_REGISTER) || !defined(CONFIG_MRST_RAR_HANDLER)
	if (sep->mrst) {
		dev_warn(&sep->pdev->dev,
			"You cannot have mrst and not have rar\n");
		dev_warn(&sep->pdev->dev,
			"Please configure the rar register and\n");
		dev_warn(&sep->pdev->dev,
			"ar handler and then recompile this kernel\n");
		sep->probed = 0;
		return -ENODEV;
	}
#endif

	/* set up our register area */
	sep->reg_physical_addr = pci_resource_start(sep->pdev, 0);
	if (!sep->reg_physical_addr) {
		dev_warn(&sep->pdev->dev, "Error getting register start\n");
		pci_dev_put(sep->pdev);
		return -ENODEV;
	}

	sep->reg_physical_end = pci_resource_end(sep->pdev, 0);
	if (!sep->reg_physical_end) {
		dev_warn(&sep->pdev->dev, "Error getting register end\n");
		pci_dev_put(sep->pdev);
		return -ENODEV;
	}

	sep->reg_addr = ioremap_nocache(sep->reg_physical_addr,
		(size_t)(sep->reg_physical_end - sep->reg_physical_addr + 1));
	if (!sep->reg_addr) {
		dev_warn(&sep->pdev->dev, "Error getting register virtual\n");
		pci_dev_put(sep->pdev);
		return -ENODEV;
	}

	dev_dbg(&sep->pdev->dev,
		"Register area start %p end %p virtual %p\n",
		(void *)sep->reg_physical_addr,
		(void *)sep->reg_physical_end,
		sep->reg_addr);

	/* allocate the shared area */
	sep->shared_size = SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES +
		SYNCHRONIC_DMA_TABLES_AREA_SIZE_BYTES +
		SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES +
		SEP_DRIVER_STATIC_AREA_SIZE_IN_BYTES +
		SEP_DRIVER_SYSTEM_DATA_MEMORY_SIZE_IN_BYTES;

	if (sep_map_and_alloc_shared_area(sep)) {
		error = -ENOMEM;
		/* allocation failed */
		goto end_function_error;
	}

	/* note that we will not set sep->probed at this time
	   because it cannot be set until after the callback
	   is called by the rar_register driver (for Moorestown)
	   is completed, which may end up being called after this
	   driver is finished probing because of order of compile
	   for the drivers in the kernel. Pleae note that attempts
	   to open this device while sep->probed is 0 will result
	   in -ENODEV error return */

	/* the next section depends on type of unit */
	if (sep->mrst) {
		error = register_rar(RAR_TYPE_IMAGE, &sep_callback,
			(unsigned long)sep);
		if (error) {
			dev_dbg(&sep->pdev->dev,
				"error register_rar\n");
			goto end_function_deallocate_sep_shared_area;
		}
	} else {
        	sep->rar_size = FAKE_RAR_SIZE;
		sep->rar_addr = dma_alloc_coherent(NULL,
			sep->rar_size, &sep->rar_bus, GFP_KERNEL);
		if (sep->rar_addr == NULL) {
			dev_warn(&sep->pdev->dev, "cant allocate mfld rar\n");
			goto end_function_deallocate_sep_shared_area;
		}

		dev_dbg(&sep->pdev->dev, "rar start is %p, phy is %p,"
			" size is %x\n", sep->rar_addr, (void *)sep->rar_bus,
			sep->rar_size);
		sep->probed = 1;
	}

#if !SEP_DRIVER_POLLING_MODE

	dev_dbg(&sep->pdev->dev,
		"about to write IMR and ICR REG_ADDR\n");

	/* clear ICR register */
	sep_write_reg(sep, HW_HOST_ICR_REG_ADDR, 0xFFFFFFFF);

	/* set the IMR register - open only GPR 2 */
	sep_write_reg(sep, HW_HOST_IMR_REG_ADDR, (~(0x1 << 13)));

	dev_dbg(&sep->pdev->dev,
		"about to call request_irq\n");
	/* get the interrupt line */
	error = request_irq(pdev->irq, sep_inthandler, IRQF_SHARED,
		"sep_driver", sep);

	if (error)
		goto end_function_free_res;

	dev_dbg(&sep->pdev->dev,
		"about to write IMR REG_ADDR");

	/* set the IMR register - open only GPR 2 */
	sep_write_reg(sep, HW_HOST_IMR_REG_ADDR, (~(0x1 << 13)));

end_function_free_res:
	dma_free_coherent(&sep->pdev->dev, 2 * SEP_RAR_IO_MEM_REGION_SIZE,
			sep->rar_addr, sep->rar_bus);
#endif				/* SEP_DRIVER_POLLING_MODE */

goto end_function;

end_function_deallocate_sep_shared_area:
	/* de-allocate shared area */
	sep_unmap_and_free_shared_area(sep);

end_function_error:
	iounmap(sep->reg_addr);
	sep_dev = NULL;

end_function:
	return error;
}

static const struct pci_device_id sep_pci_id_tbl[] = {
	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, MRST_PCI_DEVICE_ID)},
	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, MFLD_PCI_DEVICE_ID)},
	{0}
};

MODULE_DEVICE_TABLE(pci, sep_pci_id_tbl);

/* field for registering driver to PCI device */
static struct pci_driver sep_pci_driver = {
	.name = "sep_sec_driver",
	.id_table = sep_pci_id_tbl,
	.probe = sep_probe
	/* FIXME: remove handler */
};

/* file operation for singleton sep operations */
static const struct file_operations singleton_file_operations = {
	.owner = THIS_MODULE,
	.unlocked_ioctl = sep_singleton_ioctl,
	.poll = sep_poll,
	.open = sep_singleton_open,
	.release = sep_singleton_release,
	.mmap = sep_mmap,
};

/* file operation for daemon operations */
static const struct file_operations daemon_file_operations = {
	.owner = THIS_MODULE,
	.unlocked_ioctl = sep_request_daemon_ioctl,
	.poll = sep_request_daemon_poll,
	.open = sep_request_daemon_open,
	.release = sep_request_daemon_release,
	.mmap = sep_request_daemon_mmap,
};

/* the files operations structure of the driver */
static const struct file_operations sep_file_operations = {
	.owner = THIS_MODULE,
	.unlocked_ioctl = sep_ioctl,
	.poll = sep_poll,
	.open = sep_open,
	.release = sep_release,
	.mmap = sep_mmap,
};

/*
  this function registers the driver to the file system
*/
static int sep_register_driver_to_fs(struct sep_device *sep)
{
	int ret_val;
	int major;

	if (sep->mrst)
		ret_val = alloc_chrdev_region(&sep->sep_devno,
			0, 2, "sep_sec_driver");
	else
		ret_val = alloc_chrdev_region(&sep->sep_devno,
			0, 3, "sep_sec_driver");

	if (ret_val) {
		dev_warn(&sep->pdev->dev,
			"sep - major number allocation failed, retval is %d\n",
			ret_val);
		return ret_val;
	}

	major = MAJOR(sep->sep_devno);

	/* for the sep device itself */
	/* init cdev */
	cdev_init(&sep->sep_cdev, &sep_file_operations);
	sep->sep_cdev.owner = THIS_MODULE;

	/* register the driver with the kernel */
	ret_val = cdev_add(&sep->sep_cdev, sep->sep_devno, 1);
	if (ret_val) {
		dev_warn(&sep->pdev->dev,
			"sep - cdev_add failed, retval is %d\n", ret_val);
		/* unregister dev numbers */
		goto end_function_unregister_devnum;
	}

	/* for the sep daemon port */
	major = MAJOR(sep->sep_devno);
	sep->sep_daemon_devno = MKDEV(major, 2);
	cdev_init(&sep->sep_daemon_cdev, &daemon_file_operations);

	sep->sep_daemon_cdev.owner = THIS_MODULE;

	ret_val = cdev_add(&sep->sep_daemon_cdev, sep->sep_daemon_devno, 1);
	if (ret_val) {
		dev_warn(&sep->pdev->dev,
			"sep daemon - cdev_add failed, retval is %d\n",
			ret_val);
		/* unregister dev numbers */
		cdev_del(&sep->sep_cdev);
		goto end_function_unregister_devnum;
	}

	major = MAJOR(sep->sep_devno);
	sep->sep_singleton_devno = MKDEV(major, 1);
	cdev_init(&sep->sep_singleton_cdev, &singleton_file_operations);

	sep->sep_singleton_cdev.owner = THIS_MODULE;

	if (sep->mrst == 0) {

		ret_val = cdev_add(&sep->sep_singleton_cdev,
			sep->sep_singleton_devno, 1);
		if (ret_val) {
			dev_warn(&sep->pdev->dev,
				"sep singleton -"
				" cdev_add failed, retval is %d\n",
				ret_val);
			/* unregister dev numbers */
			cdev_del(&sep->sep_cdev);
			cdev_del(&sep->sep_daemon_cdev);
			goto end_function_unregister_devnum;
		}

	}

	goto end_function;

end_function_unregister_devnum:

	if (sep->mrst)
		unregister_chrdev_region(sep->sep_devno, 2);
	else
		unregister_chrdev_region(sep->sep_devno, 3);

end_function:

	return ret_val;
}


/*--------------------------------------------------------------
  init function
----------------------------------------------------------------*/
static int __init sep_init(void)
{
	int ret_val = 0;
	struct sep_device *sep;

	sep = &sep_instance;

	pr_debug("Sep driver: Init start\n");

	ret_val = pci_register_driver(&sep_pci_driver);
	if (ret_val) {
		pr_debug("sep_driver:sep_driver_to_device failed,"
			" ret_val is %d\n", ret_val);
		goto end_function;
	}

	init_waitqueue_head(&sep->event);
	init_waitqueue_head(&sep->event_request_daemon);

	if (sep->mrst == 0) {
		ret_val = sep_init_caller_id(sep);
		if (ret_val) {
			dev_warn(&sep->pdev->dev,
				"cant init caller id\n");
			goto end_function_unregister_pci;
		}

	}

	/* register driver to fs */
	ret_val = sep_register_driver_to_fs(sep);
	if (ret_val) {
		dev_warn(&sep->pdev->dev,
			"error registering device to file\n");
		goto end_function_unregister_pci;
	}

	goto end_function;

end_function_unregister_pci:
	pci_unregister_driver(&sep_pci_driver);

end_function:
	dev_dbg(&sep->pdev->dev, "Init end\n");
	return ret_val;
}


/*-------------------------------------------------------------
  exit function
--------------------------------------------------------------*/
static void __exit sep_exit(void)
{
	struct sep_device *sep;

	sep = &sep_instance;
	pr_debug("Exit start\n");

	/* unregister from fs */
	cdev_del(&sep->sep_cdev);
	cdev_del(&sep->sep_singleton_cdev);
	cdev_del(&sep->sep_daemon_cdev);

	/* unregister dev numbers */
	if (sep->mrst)
		unregister_chrdev_region(sep->sep_devno, 2);
	else
		unregister_chrdev_region(sep->sep_devno, 3);

	/* free the irq */
	free_irq(sep->pdev->irq, sep);

	/* unregister the driver */
	pci_unregister_driver(&sep_pci_driver);

	/* calculate the total size for de-allocation */
	/* FIXME: We need to do this in the unload for the device */
	/* free shared area  */
	if (sep_dev) {
		sep_unmap_and_free_shared_area(sep_dev);
		dev_dbg(&sep->pdev->dev,
			"free pages SEP SHARED AREA\n");
		iounmap((void *) sep_dev->reg_addr);
		dev_dbg(&sep->pdev->dev,
			"iounmap\n");
	}
	pr_debug("release_mem_region\n");
	pr_debug("Exit end\n");
}


module_init(sep_init);
module_exit(sep_exit);

MODULE_LICENSE("GPL");