hc
2023-03-13 2ec15ae1cb4be1b4fcb56c6d621123d7ebdaad6c
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
/*************************************************************************/ /*!
@File
@Title          Common Server PDump functions layer
@Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
@License        Dual MIT/GPLv2
 
The contents of this file are subject to the MIT license as set out below.
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
 
Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 ("GPL") in which case the provisions
of GPL are applicable instead of those above.
 
If you wish to allow use of your version of this file only under the terms of
GPL, and not to allow others to use your version of this file under the terms
of the MIT license, indicate your decision by deleting the provisions above
and replace them with the notice and other provisions required by GPL as set
out in the file called "GPL-COPYING" included in this distribution. If you do
not delete the provisions above, a recipient may use your version of this file
under the terms of either the MIT license or GPL.
 
This License is also included in this distribution in the file called
"MIT-COPYING".
 
EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ /**************************************************************************/
 
 
   
#if defined(PDUMP)
#include <stdarg.h>
 
#include "pvrversion.h"
#include "allocmem.h"
#include "osfunc.h"
#include "pvrsrv.h"
#include "pvr_debug.h"
#include "srvkm.h"
#include "pdump_physmem.h"
#include "hash.h"
#include "connection_server.h"
#include "sync_server.h"
#include "services_km.h"
/* pdump headers */
#include "dbgdrvif_srv5.h"
#include "pdump_osfunc.h"
#include "pdump_km.h"
 
/* Allow temporary buffer size override */
#if !defined(PDUMP_TEMP_BUFFER_SIZE)
#define PDUMP_TEMP_BUFFER_SIZE (64 * 1024U)
#endif
 
/* DEBUG */
#if 0
#define PDUMP_DBG(a)   PDumpOSDebugPrintf (a)
#else
#define PDUMP_DBG(a)
#endif
 
 
#define    PTR_PLUS(t, p, x) ((t)(((IMG_CHAR *)(p)) + (x)))
#define    VPTR_PLUS(p, x) PTR_PLUS(void *, p, x)
#define    VPTR_INC(p, x) ((p) = VPTR_PLUS(p, x))
#define MAX_PDUMP_MMU_CONTEXTS    (32)
static void *gpvTempBuffer = NULL;
 
#define PRM_FILE_SIZE_MAX    0x7FDFFFFFU /*!< Default maximum file size to split output files, 2GB-2MB as fwrite limits it to 2GB-1 on 32bit systems */
#define FRAME_UNSET            0xFFFFFFFFU /*|< Used to signify no or invalid frame number */
 
 
static IMG_BOOL        g_PDumpInitialised = IMG_FALSE;
static IMG_UINT32    g_ConnectionCount = 0;
 
 
typedef struct
{
   PDUMP_CHANNEL sCh;         /*!< Channel handles */
} PDUMP_SCRIPT;
 
typedef struct
{
   IMG_UINT32    ui32Init;    /*|< Count of bytes written to the init phase stream */
   IMG_UINT32    ui32Main;    /*!< Count of bytes written to the main stream */
   IMG_UINT32    ui32Deinit;  /*!< Count of bytes written to the deinit stream */
} PDUMP_CHANNEL_WOFFSETS;
 
typedef struct
{
   PDUMP_CHANNEL          sCh;             /*!< Channel handles */
   PDUMP_CHANNEL_WOFFSETS sWOff;           /*!< Channel file write offsets */
   IMG_UINT32             ui32FileIdx;     /*!< File index used when file size limit reached and a new file is started, parameter channel only */
   IMG_UINT32             ui32MaxFileSize; /*!< Maximum file size for parameter files */
 
   PDUMP_FILEOFFSET_T     uiZeroPageOffset; /*!< Offset of the zero page in the parameter file */
   size_t                 uiZeroPageSize;   /*!< Size of the zero page in the parameter file */
   IMG_CHAR               szZeroPageFilename[PDUMP_PARAM_MAX_FILE_NAME]; /*< PRM file name where the zero page was pdumped */
} PDUMP_PARAMETERS;
 
static PDUMP_SCRIPT     g_PDumpScript    = { { 0, 0, 0} };
static PDUMP_PARAMETERS g_PDumpParameters = { { 0, 0, 0}, {0, 0, 0}, 0, PRM_FILE_SIZE_MAX};
 
 
#if defined(PDUMP_DEBUG_OUTFILES)
/* counter increments each time debug write is called */
IMG_UINT32 g_ui32EveryLineCounter = 1U;
#endif
 
#if defined(PDUMP_DEBUG) || defined(REFCOUNT_DEBUG)
#define PDUMP_REFCOUNT_PRINT(fmt, ...) PVRSRVDebugPrintf(PVR_DBG_WARNING, __FILE__, __LINE__, fmt, __VA_ARGS__)
#else
#define PDUMP_REFCOUNT_PRINT(fmt, ...)
#endif
 
/* Prototype for the test/debug state dump routine used in debugging */
void PDumpCommonDumpState(IMG_BOOL bDumpOSLayerState);
#undef PDUMP_TRACE_STATE
 
 
/*****************************************************************************/
/*    PDump Control Module Definitions                                         */
/*****************************************************************************/
 
typedef struct _PDUMP_CAPTURE_RANGE_
{
   IMG_UINT32 ui32Start;       /*!< Start frame number of range */
   IMG_UINT32 ui32End;         /*!< Send frame number of range */
   IMG_UINT32 ui32Interval;    /*!< Frame sample rate interval */
} PDUMP_CAPTURE_RANGE;
 
/* No direct access to members from outside the control module - please */
typedef struct _PDUMP_CTRL_STATE_
{
   IMG_BOOL            bInitPhaseActive;   /*!< State of driver initialisation phase */
   IMG_UINT32          ui32Flags;          /*!< Unused */
 
   IMG_UINT32          ui32DefaultCapMode; /*!< Capture mode of the dump */
   PDUMP_CAPTURE_RANGE sCaptureRange;      /*|< The capture range for capture mode 'framed' */
   IMG_UINT32          ui32CurrentFrame;   /*!< Current frame number */
 
   IMG_BOOL            bCaptureOn;         /*!< Current capture status, is current frame in range */
   IMG_BOOL            bSuspended;         /*!< Suspend flag set on unrecoverable error */
   IMG_BOOL            bInPowerTransition; /*!< Device power transition state */
   POS_LOCK            hLock;              /*!< Exclusive lock to this structure */
} PDUMP_CTRL_STATE;
 
static PDUMP_CTRL_STATE g_PDumpCtrl =
{
   IMG_TRUE,
   0,
 
   0,              /*!< Value obtained from OS PDump layer during initialisation */
   {
       FRAME_UNSET,
       FRAME_UNSET,
       1
   },
   0,
 
   IMG_FALSE,
   IMG_FALSE,
   IMG_FALSE,
   NULL
};
 
static PVRSRV_ERROR PDumpCtrlInit(IMG_UINT32 ui32InitCapMode)
{
   g_PDumpCtrl.ui32DefaultCapMode = ui32InitCapMode;
   PVR_ASSERT(g_PDumpCtrl.ui32DefaultCapMode != 0);
 
   /* Create lock for PDUMP_CTRL_STATE struct, which is shared between pdump client
      and PDumping app. This lock will help us serialize calls from pdump client
      and PDumping app */
   PVR_LOGR_IF_ERROR(OSLockCreate(&g_PDumpCtrl.hLock, LOCK_TYPE_PASSIVE), "OSLockCreate");
   
   return PVRSRV_OK;
}
 
static void PDumpCtrlDeInit(void)
{
   if (g_PDumpCtrl.hLock)
   {
       OSLockDestroy(g_PDumpCtrl.hLock);
       g_PDumpCtrl.hLock = NULL;
   }
}
 
static INLINE void PDumpCtrlLockAcquire(void)
{
   OSLockAcquire(g_PDumpCtrl.hLock);
}
 
static INLINE void PDumpCtrlLockRelease(void)
{
   OSLockRelease(g_PDumpCtrl.hLock);
}
 
/**********************************************************************************************************
   NOTE:
   The following PDumpCtrl*** functions require the PDUMP_CTRL_STATE lock be acquired BEFORE they are
   called. This is because the PDUMP_CTRL_STATE data is shared between the PDumping App and the PDump
   client, hence an exclusive access is required. The lock can be acquired and released by using the
   PDumpCtrlLockAcquire & PDumpCtrlLockRelease functions respectively.
**********************************************************************************************************/
 
static void PDumpCtrlUpdateCaptureStatus(void)
{
   if (g_PDumpCtrl.ui32DefaultCapMode == DEBUG_CAPMODE_FRAMED)
   {
       if ((g_PDumpCtrl.ui32CurrentFrame >= g_PDumpCtrl.sCaptureRange.ui32Start) &&
           (g_PDumpCtrl.ui32CurrentFrame <= g_PDumpCtrl.sCaptureRange.ui32End) &&
           (((g_PDumpCtrl.ui32CurrentFrame - g_PDumpCtrl.sCaptureRange.ui32Start) % g_PDumpCtrl.sCaptureRange.ui32Interval) == 0))
       {
           g_PDumpCtrl.bCaptureOn = IMG_TRUE;
       }
       else
       {
           g_PDumpCtrl.bCaptureOn = IMG_FALSE;
       }
   }
   else if (g_PDumpCtrl.ui32DefaultCapMode == DEBUG_CAPMODE_CONTINUOUS)
   {
       g_PDumpCtrl.bCaptureOn = IMG_TRUE;
   }
   else
   {
       g_PDumpCtrl.bCaptureOn = IMG_FALSE;
       PVR_DPF((PVR_DBG_ERROR, "PDumpCtrlSetCurrentFrame: Unexpected capture mode (%x)", g_PDumpCtrl.ui32DefaultCapMode));
   }
 
}
 
static void PDumpCtrlSetCurrentFrame(IMG_UINT32 ui32Frame)
{
   g_PDumpCtrl.ui32CurrentFrame = ui32Frame;
   /* Mirror the value into the debug driver */
   PDumpOSSetFrame(ui32Frame);
 
   PDumpCtrlUpdateCaptureStatus();
 
#if defined(PDUMP_TRACE_STATE)    
   PDumpCommonDumpState(IMG_FALSE);
#endif
}
 
static void PDumpCtrlSetDefaultCaptureParams(IMG_UINT32 ui32Mode, IMG_UINT32 ui32Start, IMG_UINT32 ui32End, IMG_UINT32 ui32Interval)
{
   PVR_ASSERT(ui32Interval > 0);
   PVR_ASSERT(ui32End >= ui32Start);
   PVR_ASSERT((ui32Mode == DEBUG_CAPMODE_FRAMED) || (ui32Mode == DEBUG_CAPMODE_CONTINUOUS));
 
   /* Set the capture range to that supplied by the PDump client tool
    */
   g_PDumpCtrl.ui32DefaultCapMode = ui32Mode;
   g_PDumpCtrl.sCaptureRange.ui32Start = ui32Start;
   g_PDumpCtrl.sCaptureRange.ui32End = ui32End;
   g_PDumpCtrl.sCaptureRange.ui32Interval = ui32Interval;
 
   /* Reset the current frame on reset of the capture range, the helps to
    * avoid inter-pdump start frame issues when the driver is not reloaded.
    * No need to call PDumpCtrlUpdateCaptureStatus() direct as the set
    * current frame call will.
    */
   PDumpCtrlSetCurrentFrame(0);
}
 
static INLINE IMG_BOOL PDumpCtrlCapModIsFramed(void)
{
   return g_PDumpCtrl.ui32DefaultCapMode == DEBUG_CAPMODE_FRAMED;
}
 
static INLINE IMG_BOOL PDumpCtrlCapModIsContinuous(void)
{
   return g_PDumpCtrl.ui32DefaultCapMode == DEBUG_CAPMODE_CONTINUOUS;
}
 
static IMG_UINT32 PDumpCtrlGetCurrentFrame(void)
{
   return g_PDumpCtrl.ui32CurrentFrame;
}
 
static INLINE IMG_BOOL PDumpCtrlCaptureOn(void)
{
   return !g_PDumpCtrl.bSuspended && g_PDumpCtrl.bCaptureOn;
}
 
static INLINE IMG_BOOL PDumpCtrlCaptureRangePast(void)
{
   return (g_PDumpCtrl.ui32CurrentFrame > g_PDumpCtrl.sCaptureRange.ui32End);
}
 
/* Used to imply if the PDump client is connected or not. */
static INLINE IMG_BOOL PDumpCtrlCaptureRangeUnset(void)
{
   return ((g_PDumpCtrl.sCaptureRange.ui32Start == FRAME_UNSET) &&
           (g_PDumpCtrl.sCaptureRange.ui32End == FRAME_UNSET));
}
 
static IMG_BOOL PDumpCtrlIsLastCaptureFrame(void)
{
   if (g_PDumpCtrl.ui32DefaultCapMode == DEBUG_CAPMODE_FRAMED)
   {
       /* Is the next capture frame within the range end limit? */
       if ((g_PDumpCtrl.ui32CurrentFrame + g_PDumpCtrl.sCaptureRange.ui32Interval) > g_PDumpCtrl.sCaptureRange.ui32End)
       {
           return IMG_TRUE;
       }
   }
   else
   {
       PVR_DPF((PVR_DBG_ERROR, "PDumpCtrIsLastCaptureFrame: Unexpected capture mode (%x)", g_PDumpCtrl.ui32DefaultCapMode));
   }
 
   /* Return false for continuous capture mode or when in framed mode */
   return IMG_FALSE;
}
 
static INLINE IMG_BOOL PDumpCtrlInitPhaseComplete(void)
{
   return !g_PDumpCtrl.bInitPhaseActive;
}
 
static INLINE void PDumpCtrlSetInitPhaseComplete(IMG_BOOL bIsComplete)
{
   if (bIsComplete)
   {
       g_PDumpCtrl.bInitPhaseActive = IMG_FALSE;
       PDUMP_HEREA(102);
   }
   else
   {
       g_PDumpCtrl.bInitPhaseActive = IMG_TRUE;
       PDUMP_HEREA(103);
   }
}
 
static INLINE void PDumpCtrlSuspend(void)
{
   PDUMP_HEREA(104);
   g_PDumpCtrl.bSuspended = IMG_TRUE;
}
 
static INLINE void PDumpCtrlResume(void)
{
   PDUMP_HEREA(105);
   g_PDumpCtrl.bSuspended = IMG_FALSE;
}
 
static INLINE IMG_BOOL PDumpCtrlIsDumpSuspended(void)
{
   return g_PDumpCtrl.bSuspended;
}
 
static INLINE void PDumpCtrlPowerTransitionStart(void)
{
   g_PDumpCtrl.bInPowerTransition = IMG_TRUE;
}
 
static INLINE void PDumpCtrlPowerTransitionEnd(void)
{
   g_PDumpCtrl.bInPowerTransition = IMG_FALSE;
}
 
static INLINE IMG_BOOL PDumpCtrlInPowerTransition(void)
{
   return g_PDumpCtrl.bInPowerTransition;
}
 
static PVRSRV_ERROR PDumpCtrlIsCaptureFrame(IMG_BOOL *bIsCapturing)
{
   *bIsCapturing = PDumpCtrlCaptureOn();
   return PVRSRV_OK;
}
 
/********************************************************************************
   End of PDumpCtrl*** functions
*********************************************************************************/
 
/*
   Wrapper functions which need to be exposed in pdump_km.h for use in other
   pdump_*** modules safely. These functions call the specific PDumpCtrl layer
   function after acquiring the PDUMP_CTRL_STATE lock, hence making the calls 
   from other modules hassle free by avoiding the acquire/release CtrlLock
   calls.
*/
 
void PDumpPowerTransitionStart(void)
{
   PDumpCtrlLockAcquire();
   PDumpCtrlPowerTransitionStart();
   PDumpCtrlLockRelease();
}
 
void PDumpPowerTransitionEnd(void)
{
   PDumpCtrlLockAcquire();
   PDumpCtrlPowerTransitionEnd();
   PDumpCtrlLockRelease();
}
 
IMG_BOOL PDumpInPowerTransition(void)
{
   IMG_BOOL bPDumpInPowerTransition = IMG_FALSE;
   
   PDumpCtrlLockAcquire();
   bPDumpInPowerTransition = PDumpCtrlInPowerTransition();
   PDumpCtrlLockRelease();
 
   return bPDumpInPowerTransition;
}
 
IMG_BOOL PDumpIsDumpSuspended(void)
{
   IMG_BOOL bPDumpIsDumpSuspended;
 
   PDumpCtrlLockAcquire();
   bPDumpIsDumpSuspended = PDumpCtrlIsDumpSuspended();
   PDumpCtrlLockRelease();
 
   return bPDumpIsDumpSuspended;
}
 
/*****************************************************************************/
/*    PDump Common Write Layer just above PDump OS Layer                       */
/*****************************************************************************/
 
 
/* 
   Checks in this method were seeded from the original PDumpWriteILock()
   and DBGDrivWriteCM() and have grown since to ensure PDump output
   matches legacy output.
   Note: the order of the checks in this method is important as some
   writes have multiple pdump flags set!
 */
static IMG_BOOL PDumpWriteAllowed(IMG_UINT32 ui32Flags)
{
   /* Lock down the PDUMP_CTRL_STATE struct before calling the following
      PDumpCtrl*** functions. This is to avoid updates to the Control data
      while we are reading from it */
   PDumpCtrlLockAcquire();
 
   /* No writes if in framed mode and range pasted */
   if (PDumpCtrlCaptureRangePast())
   {
       PDUMP_HERE(10);
       goto unlockAndReturnFalse;
   }
 
   /* No writes while writing is suspended */
   if (PDumpCtrlIsDumpSuspended())
   {
       PDUMP_HERE(11);
       goto unlockAndReturnFalse;
   }
 
   /* Prevent PDumping during a power transition */
   if (PDumpCtrlInPowerTransition())
   {    /* except when it's flagged */
       if (ui32Flags & PDUMP_FLAGS_POWER)
       {
           PDUMP_HERE(20);
           goto unlockAndReturnTrue;
       }
       PDUMP_HERE(16);
       goto unlockAndReturnFalse;
   }
 
   /* Always allow dumping in init phase and when persistent flagged */
   if (ui32Flags & PDUMP_FLAGS_PERSISTENT)
   {
       PDUMP_HERE(12);
       goto unlockAndReturnTrue;
   }
   if (!PDumpCtrlInitPhaseComplete())
   {
       PDUMP_HERE(15);
       goto unlockAndReturnTrue;
   }
 
   /* The following checks are made when the driver has completed initialisation */
 
   /* If PDump client connected allow continuous flagged writes */
   if (PDUMP_IS_CONTINUOUS(ui32Flags))
   {
       if (PDumpCtrlCaptureRangeUnset()) /* Is client connected? */
       {
           PDUMP_HERE(13);
           goto unlockAndReturnFalse;
       }
       PDUMP_HERE(14);
       goto unlockAndReturnTrue;
   }
 
   /* No last/deinit statements allowed when not in initialisation phase */
   if (PDUMP_IS_CONTINUOUS(ui32Flags))
   {
       if (PDumpCtrlInitPhaseComplete())
       {
           PDUMP_HERE(17);
           PVR_DPF((PVR_DBG_ERROR, "PDumpWriteAllowed: DEINIT flag used at the wrong time outside of initialisation!"));
           goto unlockAndReturnFalse;
       }
   }
 
   /* 
       If no flags are provided then it is FRAMED output and the frame
       range must be checked matching expected behaviour.
    */
   if (PDumpCtrlCapModIsFramed() && !PDumpCtrlCaptureOn())
   {
       PDUMP_HERE(18);
       goto unlockAndReturnFalse;
   }
 
   PDUMP_HERE(19);
 
unlockAndReturnTrue:
   /* Allow the write to take place */
   PDumpCtrlLockRelease();
   return IMG_TRUE;
 
unlockAndReturnFalse:
   PDumpCtrlLockRelease();
   return IMG_FALSE;
}
 
#undef PDUMP_DEBUG_SCRIPT_LINES
 
#if defined(PDUMP_DEBUG_SCRIPT_LINES)
#define PDUMPOSDEBUGDRIVERWRITE(a,b,c,d) _PDumpOSDebugDriverWrite(a,b,c,d)
static IMG_UINT32 _PDumpOSDebugDriverWrite( IMG_HANDLE psStream,
                                   IMG_UINT8 *pui8Data,
                                   IMG_UINT32 ui32BCount,
                                   IMG_UINT32 ui32Flags)
{
   IMG_CHAR tmp1[80];
   IMG_CHAR* streamName = "unkn";
 
   if (g_PDumpScript.sCh.hDeinit == psStream)
       streamName = "dein";
   else if (g_PDumpScript.sCh.hInit == psStream)
       streamName = "init";
   else if (g_PDumpScript.sCh.hMain == psStream)
       streamName = "main";
 
   (void) PDumpOSSprintf(tmp1, 80, "-- %s, %x\n", streamName, ui32Flags);
   (void) PDumpOSDebugDriverWrite(psStream, tmp1, OSStringLength(tmp1));
 
   return PDumpOSDebugDriverWrite(psStream, pui8Data, ui32BCount);
}
#else
#define PDUMPOSDEBUGDRIVERWRITE(a,b,c,d) PDumpOSDebugDriverWrite(a,b,c)
#endif
 
 
/**************************************************************************/ /*!
 @Function        PDumpWriteToBuffer
 @Description    Write the supplied data to the PDump stream buffer and attempt
                to handle any buffer full conditions to ensure all the data
                requested to be written, is.
 
 @Input            psStream    The address of the PDump stream buffer to write to
 @Input            pui8Data    Pointer to the data to be written
 @Input            ui32BCount    Number of bytes to write
 @Input            ui32Flags    PDump statement flags.
 
 @Return         IMG_UINT32  Actual number of bytes written, may be less than
                                   ui32BCount when buffer full condition could not
                                   be avoided.
*/ /***************************************************************************/
static IMG_UINT32 PDumpWriteToBuffer(IMG_HANDLE psStream, IMG_UINT8 *pui8Data,
       IMG_UINT32 ui32BCount, IMG_UINT32 ui32Flags)
{
   IMG_UINT32    ui32BytesWritten = 0;
   IMG_UINT32    ui32Off = 0;
 
   while (ui32BCount > 0)
   {
       ui32BytesWritten = PDUMPOSDEBUGDRIVERWRITE(psStream, &pui8Data[ui32Off], ui32BCount, ui32Flags);
 
       if (ui32BytesWritten == 0)
       {
           PVR_DPF((PVR_DBG_MESSAGE, "PDumpWriteToBuffer: Zero bytes written - release execution"));
           PDumpOSReleaseExecution();
       }
 
       if (ui32BytesWritten != 0xFFFFFFFFU)
       {
           if (ui32BCount != ui32BytesWritten)
           {
               PVR_DPF((PVR_DBG_MESSAGE, "PDumpWriteToBuffer: partial write of %d bytes of %d bytes", ui32BytesWritten, ui32BCount));
           }
           ui32Off += ui32BytesWritten;
           ui32BCount -= ui32BytesWritten;
       }
       else
       {
           PVR_DPF((PVR_DBG_ERROR, "PDumpWriteToBuffer: Unrecoverable error received from the debug driver"));
           if( PDumpOSGetCtrlState(psStream, DBG_GET_STATE_FLAG_IS_READONLY) )
           {
               /* Fatal -suspend PDump to prevent flooding kernel log buffer */
               PVR_LOG(("PDump suspended, debug driver out of memory"));
               /*
                   Acquire the control lock before updating "suspended" state. This may not be required
                   because "this" is the context which checks the "suspended" state in PDumpWriteAllowed
                   before calling this function. So, this update is mainly for other contexts.
                   Also, all the other contexts which will/wish-to read the "suspended" state ought to be
                   waiting on the bridge lock first and then the PDUMP_OSLOCK (to pdump into script or 
                   parameter buffer). However, this acquire may be useful incase the PDump call is being
                   made from a direct bridge
               */
               PDumpCtrlLockAcquire();
               PDumpCtrlSuspend();
               PDumpCtrlLockRelease();
           }
           return 0;
       }
   }
 
   /* reset buffer counters */
   ui32BCount = ui32Off; ui32Off = 0; ui32BytesWritten = 0;
 
   return ui32BCount;
}
 
 
/**************************************************************************/ /*!
 @Function        PDumpWriteToChannel
 @Description    Write the supplied data to the PDump channel specified obeying
                 flags to write to the necessary channel buffers.
 
 @Input            psChannel    The address of the script or parameter channel object
 @Input/Output    psWOff        The address of the channel write offsets object to
                            update on successful writing
 @Input            pui8Data    Pointer to the data to be written
 @Input            ui32Size    Number of bytes to write
 @Input            ui32Flags    PDump statement flags, they may be clear (no flags)
                            which implies framed data, continuous flagged,
                            persistent flagged, or continuous AND persistent
                            flagged and they determine how the data is output.
                            On the first test app run after driver load, the
                            Display Controller dumps a resource that is both
                            continuous and persistent and this needs writing to
                            both the init (persistent) and main (continuous)
                            channel buffers to ensure the data is dumped in
                            subsequent test runs without reloading the driver.
                            In subsequent runs the PDump client 'freezes' the
                            init buffer so that only one dump of persistent data
                            for the "extended init phase" is captured to the
                            init buffer.
 
 @Return         IMG_BOOL    True when the data has been consumed, false otherwise
*/ /***************************************************************************/
static IMG_BOOL PDumpWriteToChannel(PDUMP_CHANNEL* psChannel, PDUMP_CHANNEL_WOFFSETS* psWOff,
       IMG_UINT8* pui8Data, IMG_UINT32 ui32Size, IMG_UINT32 ui32Flags)
{
   IMG_UINT32   ui32BytesWritten = 0;
 
   PDUMP_HERE(210);
 
   /* Dump data to deinit buffer when flagged as deinit */
   if (ui32Flags & PDUMP_FLAGS_DEINIT)
   {
       PDUMP_HERE(211);
       ui32BytesWritten = PDumpWriteToBuffer(psChannel->hDeinit, pui8Data, ui32Size, ui32Flags);
       if (ui32BytesWritten != ui32Size)
       {
           PVR_DPF((PVR_DBG_ERROR, "PDumpWriteToChannel: DEINIT Written length (%d) does not match data length (%d), PDump incomplete!", ui32BytesWritten, ui32Size));
           PDUMP_HERE(212);
           return IMG_FALSE;
       }
 
       if (psWOff)
       {
           psWOff->ui32Deinit += ui32Size;
       }
 
   }
   else
   {
       IMG_BOOL bDumpedToInitAlready = IMG_FALSE;
       IMG_HANDLE*  phStream = NULL;
       IMG_UINT32*  pui32Offset = NULL;
 
       /* Always append persistent data to init phase so it's available on
        * subsequent app runs, but also to the main stream if client connected */
       if (ui32Flags & PDUMP_FLAGS_PERSISTENT)
       {
           PDUMP_HERE(213);
           ui32BytesWritten = PDumpWriteToBuffer(    psChannel->hInit, pui8Data, ui32Size, ui32Flags);
           if (ui32BytesWritten != ui32Size)
           {
               PVR_DPF((PVR_DBG_ERROR, "PDumpWriteToChannel: PERSIST Written length (%d) does not match data length (%d), PDump incomplete!", ui32BytesWritten, ui32Size));
               PDUMP_HERE(214);
               return IMG_FALSE;
           }
 
           bDumpedToInitAlready = IMG_TRUE;
           if (psWOff)
           {
               psWOff->ui32Init += ui32Size;
           }
 
           /* Don't write continuous data if client not connected */
           PDumpCtrlLockAcquire();
           if (PDUMP_IS_CONTINUOUS(ui32Flags) && PDumpCtrlCaptureRangeUnset())
           {
               PDumpCtrlLockRelease();
               return IMG_TRUE;
           }
           PDumpCtrlLockRelease();
       }
 
       /* Prepare to write the data to the main stream for
        * persistent, continuous or framed data. Override and use init
        * stream if driver still in init phase and we have not written 
        * to it yet.*/
       PDumpCtrlLockAcquire();
       if (!PDumpCtrlInitPhaseComplete() && !bDumpedToInitAlready)
       {
           PDUMP_HERE(215);
           phStream = &psChannel->hInit;
           if (psWOff)
           {
               pui32Offset = &psWOff->ui32Init;
           }
       }
       else
       {
           PDUMP_HERE(216);
           phStream = &psChannel->hMain;
           if (psWOff)
           {
               pui32Offset = &psWOff->ui32Main;
           }
       }
       PDumpCtrlLockRelease();
 
       /* Write the data to the stream */
       ui32BytesWritten = PDumpWriteToBuffer(*phStream, pui8Data, ui32Size, ui32Flags);
       if (ui32BytesWritten != ui32Size)
       {
           PVR_DPF((PVR_DBG_ERROR, "PDumpWriteToChannel: MAIN Written length (%d) does not match data length (%d), PDump incomplete!", ui32BytesWritten, ui32Size));
           PDUMP_HERE(217);
           return IMG_FALSE;
       }
 
       if (pui32Offset)
       {
           *pui32Offset += ui32BytesWritten;
       }
   }
 
   return IMG_TRUE;
}
 
#if defined(PDUMP_DEBUG_OUTFILES)
 
static IMG_UINT32 _GenerateChecksum(void *pvData, size_t uiSize)
{
   IMG_UINT32 ui32Sum = 0;
   IMG_UINT32 *pui32Data = pvData;
   IMG_UINT8 *pui8Data = pvData;
   IMG_UINT32 i;
   IMG_UINT32 ui32LeftOver;
 
   for(i = 0; i < uiSize / sizeof(IMG_UINT32); i++)
   {
       ui32Sum += pui32Data[i];
   }
 
   ui32LeftOver = uiSize % sizeof(IMG_UINT32);
 
   while(ui32LeftOver)
   {
       ui32Sum += pui8Data[uiSize - ui32LeftOver];
       ui32LeftOver--;
   }
 
   return ui32Sum;
}
 
#endif
 
PVRSRV_ERROR PDumpWriteParameter(IMG_UINT8 *pui8Data, IMG_UINT32 ui32Size, IMG_UINT32 ui32Flags,
       IMG_UINT32* pui32FileOffset, IMG_CHAR* aszFilenameStr)
{
   PVRSRV_ERROR eError = PVRSRV_OK;
   IMG_BOOL bPDumpCtrlInitPhaseComplete = IMG_FALSE;
 
   PVR_ASSERT(pui8Data && (ui32Size!=0));
   PVR_ASSERT(pui32FileOffset && aszFilenameStr);
 
   PDUMP_HERE(1);
 
   if (!PDumpWriteAllowed(ui32Flags))
   {
       /* Abort write for the above reason but indicate what happened to
        * caller to avoid disrupting the driver, caller should treat it as OK
        * but skip any related PDump writes to the script file.  */
       return PVRSRV_ERROR_PDUMP_NOT_ALLOWED;
   }
 
   PDUMP_HERE(2);
 
   PDumpCtrlLockAcquire();
   bPDumpCtrlInitPhaseComplete = PDumpCtrlInitPhaseComplete();
   PDumpCtrlLockRelease();
 
   if (!bPDumpCtrlInitPhaseComplete || (ui32Flags & PDUMP_FLAGS_PERSISTENT))
   {
       PDUMP_HERE(3);
 
       /* Init phase stream not expected to get above the file size max */
       PVR_ASSERT(g_PDumpParameters.sWOff.ui32Init < g_PDumpParameters.ui32MaxFileSize);
 
       /* Return the file write offset at which the parameter data was dumped */
       *pui32FileOffset = g_PDumpParameters.sWOff.ui32Init;
   }
   else
   {
       PDUMP_HERE(4);
 
       /* Do we need to signal the PDump client that a split is required? */
       if (g_PDumpParameters.sWOff.ui32Main + ui32Size > g_PDumpParameters.ui32MaxFileSize)
       {
           PDUMP_HERE(5);
           PDumpOSSetSplitMarker(g_PDumpParameters.sCh.hMain, g_PDumpParameters.sWOff.ui32Main);
           g_PDumpParameters.ui32FileIdx++;
           g_PDumpParameters.sWOff.ui32Main = 0;
       }
 
       /* Return the file write offset at which the parameter data was dumped */
       *pui32FileOffset = g_PDumpParameters.sWOff.ui32Main;
   }
 
   /* Create the parameter file name, based on index, to be used in the script */
   if (g_PDumpParameters.ui32FileIdx == 0)
   {
       eError = PDumpOSSprintf(aszFilenameStr, PDUMP_PARAM_MAX_FILE_NAME, PDUMP_PARAM_0_FILE_NAME);
   }
   else
   {
       PDUMP_HERE(6);
       eError = PDumpOSSprintf(aszFilenameStr, PDUMP_PARAM_MAX_FILE_NAME, PDUMP_PARAM_N_FILE_NAME, g_PDumpParameters.ui32FileIdx);
   }
   PVR_LOGG_IF_ERROR(eError, "PDumpOSSprintf", errExit);
 
   /* Write the parameter data to the parameter channel */
   eError = PVRSRV_ERROR_PDUMP_BUFFER_FULL;
   if (!PDumpWriteToChannel(&g_PDumpParameters.sCh, &g_PDumpParameters.sWOff, pui8Data, ui32Size, ui32Flags))
   {
       PDUMP_HERE(7);
       PVR_LOGG_IF_ERROR(eError, "PDumpWrite", errExit);
   }
#if defined(PDUMP_DEBUG_OUTFILES)
   else
   {
       IMG_UINT32 ui32Checksum;
       PDUMP_GET_SCRIPT_STRING();
 
       ui32Checksum = _GenerateChecksum(pui8Data, ui32Size);
 
       /* CHK CHKSUM SIZE PRMOFFSET PRMFILE */
       eError = PDumpOSBufprintf(hScript, ui32MaxLen, "-- CHK 0x%08X 0x%08X 0x%08X %s",
                                   ui32Checksum,
                                   ui32Size,
                                   *pui32FileOffset,
                                   aszFilenameStr);
       if(eError != PVRSRV_OK)
       {
           goto errExit;
       }
 
       PDumpWriteScript(hScript, ui32Flags);
   }
#endif
 
   return PVRSRV_OK;
 
errExit:
   return eError;
}
 
 
IMG_BOOL PDumpWriteScript(IMG_HANDLE hString, IMG_UINT32 ui32Flags)
{
   PVR_ASSERT(hString);
 
   PDUMP_HERE(201);
 
   if (!PDumpWriteAllowed(ui32Flags))
   {
       /* Abort write for the above reasons but indicated it was OK to
        * caller to avoid disrupting the driver */
       return IMG_TRUE;
   }
 
   return PDumpWriteToChannel(&g_PDumpScript.sCh, NULL, (IMG_UINT8*) hString, (IMG_UINT32) OSStringLength((IMG_CHAR*) hString), ui32Flags);
}
 
 
/*****************************************************************************/
 
 
 
 
 
 
struct _PDUMP_CONNECTION_DATA_ {
   IMG_UINT32                ui32RefCount;
   POS_LOCK                hLock;
   DLLIST_NODE                sListHead;
   IMG_BOOL                bLastInto;
   IMG_UINT32                ui32LastSetFrameNumber;
   IMG_BOOL                bWasInCaptureRange;
   IMG_BOOL                bIsInCaptureRange;
   IMG_BOOL                bLastTransitionFailed;
   SYNC_CONNECTION_DATA    *psSyncConnectionData;
};
 
static PDUMP_CONNECTION_DATA * _PDumpConnectionAcquire(PDUMP_CONNECTION_DATA *psPDumpConnectionData)
{
   IMG_UINT32 ui32RefCount;
 
   OSLockAcquire(psPDumpConnectionData->hLock);
   ui32RefCount = ++psPDumpConnectionData->ui32RefCount;
   OSLockRelease(psPDumpConnectionData->hLock);
 
   PDUMP_REFCOUNT_PRINT("%s: PDump connection %p, refcount = %d",
                        __FUNCTION__, psPDumpConnectionData, ui32RefCount);
 
   return psPDumpConnectionData;
}
 
static void _PDumpConnectionRelease(PDUMP_CONNECTION_DATA *psPDumpConnectionData)
{
   IMG_UINT32 ui32RefCount;
 
   OSLockAcquire(psPDumpConnectionData->hLock);
   ui32RefCount = --psPDumpConnectionData->ui32RefCount;
   OSLockRelease(psPDumpConnectionData->hLock);
 
   if (ui32RefCount == 0)
   {
       OSLockDestroy(psPDumpConnectionData->hLock);
       PVR_ASSERT(dllist_is_empty(&psPDumpConnectionData->sListHead));
       OSFreeMem(psPDumpConnectionData);
   }
 
   PDUMP_REFCOUNT_PRINT("%s: PDump connection %p, refcount = %d",
                        __FUNCTION__, psPDumpConnectionData, ui32RefCount);
}
 
/**************************************************************************
 * Function Name  : GetTempBuffer
 * Inputs         : None
 * Outputs        : None
 * Returns        : Temporary buffer address, or NULL
 * Description    : Get temporary buffer address.
**************************************************************************/
static void *GetTempBuffer(void)
{
   /*
    * Allocate the temporary buffer, if it hasn't been allocated already.
    * Return the address of the temporary buffer, or NULL if it
    * couldn't be allocated.
    * It is expected that the buffer will be allocated once, at driver
    * load time, and left in place until the driver unloads.
    */
 
   if (gpvTempBuffer == NULL)
   {
       gpvTempBuffer = OSAllocMem(PDUMP_TEMP_BUFFER_SIZE);
       if (gpvTempBuffer == NULL)
       {
           PVR_DPF((PVR_DBG_ERROR, "GetTempBuffer: OSAllocMem failed"));
       }
   }
 
   return gpvTempBuffer;
}
 
static void FreeTempBuffer(void)
{
 
   if (gpvTempBuffer != NULL)
   {
       OSFreeMem(gpvTempBuffer);
       gpvTempBuffer = NULL;
   }
}
 
/**************************************************************************
 * Function Name  : PDumpParameterChannelZeroedPageBlock
 * Inputs         : None
 * Outputs        : None
 * Returns        : PVRSRV_ERROR
 * Description    : Set up the zero page block in the parameter stream
**************************************************************************/
static PVRSRV_ERROR PDumpParameterChannelZeroedPageBlock(void)
{
   IMG_UINT8 aui8Zero[32] = { 0 };
   size_t uiBytesToWrite;
   PVRSRV_ERROR eError;
 
   g_PDumpParameters.uiZeroPageSize = OSGetPageSize();
 
   /* ensure the zero page size of a multiple of the zero source on the stack */
   PVR_ASSERT(g_PDumpParameters.uiZeroPageSize % sizeof(aui8Zero) == 0);
 
   /* the first write gets the parameter file name and stream offset,
    * then subsequent writes do not need to know this as the data is
    * contiguous in the stream
    */
   PDUMP_LOCK();
   eError = PDumpWriteParameter(aui8Zero,
                           sizeof(aui8Zero),
                           0,
                           &g_PDumpParameters.uiZeroPageOffset,
                           g_PDumpParameters.szZeroPageFilename);
 
   if(eError != PVRSRV_OK)
   {
       /* Also treat PVRSRV_ERROR_PDUMP_NOT_ALLOWED as an error in this case
        * as it should never happen since all writes during driver Init are allowed.
        */
       goto err_write;
   }
 
   uiBytesToWrite = g_PDumpParameters.uiZeroPageSize - sizeof(aui8Zero);
 
   while(uiBytesToWrite)
   {
       IMG_BOOL bOK;
 
       bOK = PDumpWriteToChannel(&g_PDumpParameters.sCh, &g_PDumpParameters.sWOff,
                                   aui8Zero,
                                   sizeof(aui8Zero), 0);
 
       if(!bOK)
       {
           eError = PVRSRV_ERROR_PDUMP_BUFFER_FULL;
           goto err_write;
       }
 
       uiBytesToWrite -= sizeof(aui8Zero);
   }
 
err_write:
   PDUMP_UNLOCK();
 
   if(eError != PVRSRV_OK)
   {
       PVR_DPF((PVR_DBG_ERROR, "Failed to initialise parameter stream zero block"));
   }
 
   return eError;
}
 
/**************************************************************************
 * Function Name  : PDumpGetParameterZeroPageInfo
 * Inputs         : None
 * Outputs        : puiZeroPageOffset: will be set to the offset of the zero page
 *                : puiZeroPageSize: will be set to the size of the zero page
 *                : ppszZeroPageFilename: will be set to a pointer to the PRM file name
 *                :                       containing the zero page
 * Returns        : None
 * Description    : Get information about the zero page
**************************************************************************/
void PDumpGetParameterZeroPageInfo(PDUMP_FILEOFFSET_T *puiZeroPageOffset,
                   size_t *puiZeroPageSize,
                   const IMG_CHAR **ppszZeroPageFilename)
{
       *puiZeroPageOffset = g_PDumpParameters.uiZeroPageOffset;
       *puiZeroPageSize = g_PDumpParameters.uiZeroPageSize;
       *ppszZeroPageFilename = g_PDumpParameters.szZeroPageFilename;
}
 
PVRSRV_ERROR PDumpInitCommon(void)
{
   PVRSRV_ERROR eError;
   IMG_UINT32 ui32InitCapMode = 0;
   IMG_CHAR* pszEnvComment = NULL;
 
   PDUMP_HEREA(2010);
 
   /* Allocate temporary buffer for copying from user space */
   (void) GetTempBuffer();
 
   /* create the global PDump lock */
   eError = PDumpCreateLockKM();
   PVR_LOGG_IF_ERROR(eError, "PDumpCreateLockKM", errExit);
 
   /* Call environment specific PDump initialisation */
   eError = PDumpOSInit(&g_PDumpParameters.sCh, &g_PDumpScript.sCh, &ui32InitCapMode, &pszEnvComment);
   PVR_LOGG_IF_ERROR(eError, "PDumpOSInit", errExitLock);
 
   /* Initialise PDump control module in common layer */
   eError = PDumpCtrlInit(ui32InitCapMode);
   PVR_LOGG_IF_ERROR(eError, "PDumpCtrlInit", errExitOSDeInit);
 
   /* Test PDump initialised and ready by logging driver details */
   eError = PDumpCommentWithFlags(PDUMP_FLAGS_CONTINUOUS, "Driver Product Version: %s - %s (%s)", PVRVERSION_STRING, PVR_BUILD_DIR, PVR_BUILD_TYPE);
   PVR_LOGG_IF_ERROR(eError, "PDumpCommentWithFlags", errExitCtrl);
   if (pszEnvComment != NULL)
   {
       eError = PDumpCommentWithFlags(PDUMP_FLAGS_CONTINUOUS, "%s", pszEnvComment);
       PVR_LOGG_IF_ERROR(eError, "PDumpCommentWithFlags", errExitCtrl);
   }
   eError = PDumpCommentWithFlags(PDUMP_FLAGS_CONTINUOUS, "Start of Init Phase");
   PVR_LOGG_IF_ERROR(eError, "PDumpCommentWithFlags", errExitCtrl);
 
   eError = PDumpParameterChannelZeroedPageBlock();
   PVR_LOGG_IF_ERROR(eError, "PDumpParameterChannelZeroedPageBlock", errExitCtrl);
 
   g_PDumpInitialised = IMG_TRUE;
 
   PDUMP_HEREA(2011);
 
   return PVRSRV_OK;
 
errExitCtrl:
   PDumpCtrlDeInit();
errExitOSDeInit:
   PDUMP_HEREA(2018);
   PDumpOSDeInit(&g_PDumpParameters.sCh, &g_PDumpScript.sCh);
errExitLock:
   PDUMP_HEREA(2019);
   PDumpDestroyLockKM();
errExit:
   return eError;
}
 
void PDumpDeInitCommon(void)
{
   PDUMP_HEREA(2020);
 
   g_PDumpInitialised = IMG_FALSE;
 
   /* Free temporary buffer */
   FreeTempBuffer();
 
   /* DeInit the PDUMP_CTRL_STATE data */
   PDumpCtrlDeInit();
 
   /* Call environment specific PDump Deinitialisation */
   PDumpOSDeInit(&g_PDumpParameters.sCh, &g_PDumpScript.sCh);
 
   /* take down the global PDump lock */
   PDumpDestroyLockKM();
}
 
IMG_BOOL PDumpReady(void)
{
   return g_PDumpInitialised;
}
 
void PDumpStopInitPhase(IMG_BOOL bPDumpClient, IMG_BOOL bInitClient)
{
   /* Check with the OS we a running on */
   if (PDumpOSAllowInitPhaseToComplete(bPDumpClient, bInitClient))
   {
       if (bInitClient)
       {
           /* We only ouptut this once for bInitClient init phase ending OSs */
           PDUMPCOMMENT("Stop Init Phase");
       }
       PDumpCtrlLockAcquire();
       PDumpCtrlSetInitPhaseComplete(IMG_TRUE);
       PDumpCtrlLockRelease();
   }
}
 
PVRSRV_ERROR PDumpIsLastCaptureFrameKM(IMG_BOOL *pbIsLastCaptureFrame)
{
   PDumpCtrlLockAcquire();
   *pbIsLastCaptureFrame = PDumpCtrlIsLastCaptureFrame();
   PDumpCtrlLockRelease();
 
   return PVRSRV_OK;
}
 
 
 
typedef struct _PDUMP_Transition_DATA_ {
   PFN_PDUMP_TRANSITION    pfnCallback;
   void                    *hPrivData;
   PDUMP_CONNECTION_DATA    *psPDumpConnectionData;
   DLLIST_NODE                sNode;
} PDUMP_Transition_DATA;
 
PVRSRV_ERROR PDumpRegisterTransitionCallback(PDUMP_CONNECTION_DATA *psPDumpConnectionData,
                                             PFN_PDUMP_TRANSITION pfnCallback,
                                             void *hPrivData,
                                             void **ppvHandle)
{
   PDUMP_Transition_DATA *psData;
   PVRSRV_ERROR eError;
 
   psData = OSAllocMem(sizeof(*psData));
   if (psData == NULL)
   {
       eError = PVRSRV_ERROR_OUT_OF_MEMORY;
       goto fail_alloc;
   }
 
   /* Setup the callback and add it to the list for this process */
   psData->pfnCallback = pfnCallback;
   psData->hPrivData = hPrivData;
   dllist_add_to_head(&psPDumpConnectionData->sListHead, &psData->sNode);
 
   /* Take a reference on the connection so it doesn't get freed too early */
   psData->psPDumpConnectionData =_PDumpConnectionAcquire(psPDumpConnectionData);
   *ppvHandle = psData;
 
   return PVRSRV_OK;
 
fail_alloc:
   PVR_ASSERT(eError != PVRSRV_OK);
   return eError;
}
 
void PDumpUnregisterTransitionCallback(void *pvHandle)
{
   PDUMP_Transition_DATA *psData = pvHandle;
 
   dllist_remove_node(&psData->sNode);
   _PDumpConnectionRelease(psData->psPDumpConnectionData);
   OSFreeMem(psData);
}
 
PVRSRV_ERROR PDumpTransition(PDUMP_CONNECTION_DATA *psPDumpConnectionData, IMG_BOOL bInto, IMG_UINT32 ui32PDumpFlags)
{
   DLLIST_NODE *psNode, *psNext;
   PVRSRV_ERROR eError;
 
   /* Only call the callbacks if we've really done a Transition */
   if (bInto != psPDumpConnectionData->bLastInto)
   {
       /* We're Transitioning either into or out of capture range */
       dllist_foreach_node(&psPDumpConnectionData->sListHead, psNode, psNext)
       {
           PDUMP_Transition_DATA *psData =
               IMG_CONTAINER_OF(psNode, PDUMP_Transition_DATA, sNode);
 
           eError = psData->pfnCallback(psData->hPrivData, bInto, ui32PDumpFlags);
 
           if (eError != PVRSRV_OK)
           {
               return eError;
           }
       }
 
       if (bInto)
       {
           SyncConnectionPDumpSyncBlocks(psPDumpConnectionData->psSyncConnectionData);
       }
       psPDumpConnectionData->bLastInto = bInto;
   }
   return PVRSRV_OK;
}
 
PVRSRV_ERROR PDumpIsCaptureFrameKM(IMG_BOOL *bIsCapturing)
{
   PDumpCtrlLockAcquire();
   PDumpCtrlIsCaptureFrame(bIsCapturing);
   PDumpCtrlLockRelease();
 
   return PVRSRV_OK;
}
 
static PVRSRV_ERROR _PDumpSetFrameKM(CONNECTION_DATA *psConnection,
                                     IMG_UINT32 ui32Frame)
{
   PDUMP_CONNECTION_DATA *psPDumpConnectionData = psConnection->psPDumpConnectionData;
   IMG_BOOL bWasInCaptureRange = IMG_FALSE;
   IMG_BOOL bIsInCaptureRange = IMG_FALSE;
   PVRSRV_ERROR eError;
 
   /*
       Note:
       As we can't test to see if the new frame will be in capture range
       before we set the frame number and we don't want to roll back the
       frame number if we fail then we have to save the "transient" data
       which decides if we're entering or exiting capture range along
       with a failure boolean so we know what's required on a retry
   */
   if (psPDumpConnectionData->ui32LastSetFrameNumber != ui32Frame)
   {
       (void) PDumpCommentWithFlags(PDUMP_FLAGS_CONTINUOUS, "Set pdump frame %u", ui32Frame);
 
       /*
           The boolean values below decide if the PDump transition
           should trigger because of the current context setting the
           frame number, hence the functions below should execute
           atomically and do not give a chance to some other context
           to transition
       */
       PDumpCtrlLockAcquire(); 
       
       PDumpCtrlIsCaptureFrame(&bWasInCaptureRange);
       PDumpCtrlSetCurrentFrame(ui32Frame);
       PDumpCtrlIsCaptureFrame(&bIsInCaptureRange);
 
       PDumpCtrlLockRelease();
 
       psPDumpConnectionData->ui32LastSetFrameNumber = ui32Frame;
 
       /* Save the Transition data incase we fail the Transition */
       psPDumpConnectionData->bWasInCaptureRange = bWasInCaptureRange;
       psPDumpConnectionData->bIsInCaptureRange = bIsInCaptureRange;
   }
   else if (psPDumpConnectionData->bLastTransitionFailed)
   {
       /* Load the Transition data so we can try again */
       bWasInCaptureRange = psPDumpConnectionData->bWasInCaptureRange;
       bIsInCaptureRange = psPDumpConnectionData->bIsInCaptureRange;
   }
   else
   {
       /* New frame is the same as the last frame set and the last
        * transition succeeded, no need to perform another transition.
        */
       return PVRSRV_OK;
   }
 
   if (!bWasInCaptureRange && bIsInCaptureRange)
   {
       eError = PDumpTransition(psPDumpConnectionData, IMG_TRUE, PDUMP_FLAGS_NONE);
       if (eError != PVRSRV_OK)
       {
           goto fail_Transition;
       }
   }
   else if (bWasInCaptureRange && !bIsInCaptureRange)
   {
       eError = PDumpTransition(psPDumpConnectionData, IMG_FALSE, PDUMP_FLAGS_NONE);
       if (eError != PVRSRV_OK)
       {
           goto fail_Transition;
       }
   }
   else
   {
       /* Here both previous and current frames are in or out of range.
        * There is no transition in this case.
        */
   }
 
   psPDumpConnectionData->bLastTransitionFailed = IMG_FALSE;
   return PVRSRV_OK;
 
fail_Transition:
   psPDumpConnectionData->bLastTransitionFailed = IMG_TRUE;
   return eError;
}
 
PVRSRV_ERROR PDumpSetFrameKM(CONNECTION_DATA *psConnection,
                             PVRSRV_DEVICE_NODE * psDeviceNode,
                             IMG_UINT32 ui32Frame)
{
   PVRSRV_ERROR eError = PVRSRV_OK;    
 
   PVR_UNREFERENCED_PARAMETER(psDeviceNode);
 
#if defined(PDUMP_TRACE_STATE)
   PVR_DPF((PVR_DBG_WARNING, "PDumpSetFrameKM: ui32Frame( %d )", ui32Frame));
#endif
 
#if defined(PDUMP_DEBUG_OUTFILES)
   (void) PDumpCommentWithFlags(PDUMP_FLAGS_CONTINUOUS, "Set pdump frame %u (pre)", ui32Frame);
#endif
 
   eError = _PDumpSetFrameKM(psConnection, ui32Frame);
   if ((eError != PVRSRV_OK) && (eError != PVRSRV_ERROR_RETRY))
   {
       PVR_LOG_ERROR(eError, "_PDumpSetFrameKM");
   }
 
#if defined(PDUMP_DEBUG_OUTFILES)
   (void) PDumpCommentWithFlags(PDUMP_FLAGS_CONTINUOUS, "Set pdump frame %u (post)", ui32Frame);
#endif
 
   return eError;
}
 
PVRSRV_ERROR PDumpGetFrameKM(CONNECTION_DATA *psConnection,
                             PVRSRV_DEVICE_NODE * psDeviceNode,
                             IMG_UINT32* pui32Frame)
{
   PVRSRV_ERROR eError = PVRSRV_OK;
 
   PVR_UNREFERENCED_PARAMETER(psConnection);
 
   /*
       It may be safe to avoid acquiring this lock here as all the other calls
       which read/modify current frame will wait on the PDump Control bridge
       lock first. Also, in no way as of now, does the PDumping app modify the
       current frame through a call which acquires the global bridge lock.
       Still, as a legacy we acquire and then read.
   */    
   PDumpCtrlLockAcquire();
 
   *pui32Frame = PDumpCtrlGetCurrentFrame();
 
   PDumpCtrlLockRelease();
   return eError;
}
 
PVRSRV_ERROR PDumpSetDefaultCaptureParamsKM(IMG_UINT32 ui32Mode,
                                           IMG_UINT32 ui32Start,
                                           IMG_UINT32 ui32End,
                                           IMG_UINT32 ui32Interval,
                                           IMG_UINT32 ui32MaxParamFileSize)
{
   /*
       Acquire PDUMP_CTRL_STATE struct lock before modifications as a 
       PDumping app may be reading the state data for some checks
   */
   PDumpCtrlLockAcquire();
   PDumpCtrlSetDefaultCaptureParams(ui32Mode, ui32Start, ui32End, ui32Interval);
   PDumpCtrlLockRelease();
 
   if (ui32MaxParamFileSize == 0)
   {
       g_PDumpParameters.ui32MaxFileSize = PRM_FILE_SIZE_MAX;
   }
   else
   {
       g_PDumpParameters.ui32MaxFileSize = ui32MaxParamFileSize;
   }
   return PVRSRV_OK;
}
 
 
/**************************************************************************
 * Function Name  : PDumpReg32
 * Inputs         : pszPDumpDevName, Register offset, and value to write
 * Outputs        : None
 * Returns        : PVRSRV_ERROR
 * Description    : Create a PDUMP string, which represents a register write
**************************************************************************/
PVRSRV_ERROR PDumpReg32(IMG_CHAR    *pszPDumpRegName,
                       IMG_UINT32    ui32Reg,
                       IMG_UINT32    ui32Data,
                       IMG_UINT32    ui32Flags)
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING()
   PDUMP_DBG(("PDumpReg32"));
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "WRW :%s:0x%08X 0x%08X", pszPDumpRegName, ui32Reg, ui32Data);
 
   if (eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
   PDumpWriteScript(hScript, ui32Flags);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
 
/**************************************************************************
 * Function Name  : PDumpReg64
 * Inputs         : pszPDumpDevName, Register offset, and value to write
 * Outputs        : None
 * Returns        : PVRSRV_ERROR
 * Description    : Create a PDUMP string, which represents a register write
**************************************************************************/
PVRSRV_ERROR PDumpReg64(IMG_CHAR    *pszPDumpRegName,
                       IMG_UINT32    ui32Reg,
                       IMG_UINT64    ui64Data,
                       IMG_UINT32    ui32Flags)
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING()
   PDUMP_DBG(("PDumpRegKM"));
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "WRW64 :%s:0x%08X 0x%010llX", pszPDumpRegName, ui32Reg, ui64Data);
 
   if (eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
   PDumpWriteScript(hScript, ui32Flags);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
/**************************************************************************
 * Function Name  : PDumpRegLabelToReg64
 * Returns        : PVRSRV_ERROR
 * Description    : Create a PDUMP string, which represents a register write from a register label
**************************************************************************/
PVRSRV_ERROR PDumpRegLabelToReg64(IMG_CHAR *pszPDumpRegName,
                                  IMG_UINT32 ui32RegDst,
                                  IMG_UINT32 ui32RegSrc,
                                  IMG_UINT32 ui32Flags)
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING()
   PDUMP_DBG(("PDumpRegLabelToReg64"));
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "WRW64 :%s:0x%08X :%s:0x%08X", pszPDumpRegName, ui32RegDst, pszPDumpRegName, ui32RegSrc);
 
   if (eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
   PDumpWriteScript(hScript, ui32Flags);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
 
}
 
/**************************************************************************
 * Function Name  : PDumpRegLabelToMem32
 * Returns        : PVRSRV_ERROR
 * Description    : Create a PDUMP string, which represents a memory write from a register label
**************************************************************************/
PVRSRV_ERROR PDumpRegLabelToMem32(IMG_CHAR *pszPDumpRegName,
                                  IMG_UINT32 ui32Reg,
                                  PMR *psPMR,
                                  IMG_DEVMEM_OFFSET_T uiLogicalOffset,
                                  IMG_UINT32 ui32Flags)
{
   PVRSRV_ERROR eErr;
   IMG_CHAR aszMemspaceName[PHYSMEM_PDUMP_MEMSPACE_MAX_LENGTH];
   IMG_CHAR aszSymbolicName[PHYSMEM_PDUMP_SYMNAME_MAX_LENGTH];
   IMG_DEVMEM_OFFSET_T uiPDumpSymbolicOffset;
   IMG_DEVMEM_OFFSET_T uiNextSymName;
 
   PDUMP_GET_SCRIPT_STRING()
   PDUMP_DBG(("PDumpRegLabelToMem32"));
 
   eErr = PMR_PDumpSymbolicAddr(psPMR,
                                     uiLogicalOffset,
                                     PHYSMEM_PDUMP_MEMSPACE_MAX_LENGTH,
                                     aszMemspaceName,
                                     PHYSMEM_PDUMP_SYMNAME_MAX_LENGTH,
                                     aszSymbolicName,
                                     &uiPDumpSymbolicOffset,
                                     &uiNextSymName);
 
   if (eErr != PVRSRV_OK)
   {
       return eErr;
   }
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "WRW :%s:%s:0x%llX :%s:0x%08X",aszMemspaceName, aszSymbolicName,
                           uiPDumpSymbolicOffset, pszPDumpRegName, ui32Reg);
 
 
   if (eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
   PDumpWriteScript(hScript, ui32Flags);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
/**************************************************************************
 * Function Name  : PDumpRegLabelToMem64
 * Returns        : PVRSRV_ERROR
 * Description    : Create a PDUMP string, which represents a memory write from a register label
**************************************************************************/
PVRSRV_ERROR PDumpRegLabelToMem64(IMG_CHAR *pszPDumpRegName,
                                 IMG_UINT32 ui32Reg,
                                 PMR *psPMR,
                                 IMG_DEVMEM_OFFSET_T uiLogicalOffset,
                                 IMG_UINT32 ui32Flags)
{
   PVRSRV_ERROR eErr;
   IMG_CHAR aszMemspaceName[PHYSMEM_PDUMP_MEMSPACE_MAX_LENGTH];
   IMG_CHAR aszSymbolicName[PHYSMEM_PDUMP_SYMNAME_MAX_LENGTH];
   IMG_DEVMEM_OFFSET_T uiPDumpSymbolicOffset;
   IMG_DEVMEM_OFFSET_T uiNextSymName;
 
   PDUMP_GET_SCRIPT_STRING()
   PDUMP_DBG(("PDumpRegLabelToMem64"));
 
   eErr = PMR_PDumpSymbolicAddr(psPMR,
                                    uiLogicalOffset,
                                    PHYSMEM_PDUMP_MEMSPACE_MAX_LENGTH,
                                    aszMemspaceName,
                                    PHYSMEM_PDUMP_SYMNAME_MAX_LENGTH,
                                    aszSymbolicName,
                                    &uiPDumpSymbolicOffset,
                                    &uiNextSymName);
 
   if (eErr != PVRSRV_OK)
   {
       return eErr;
   }
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "WRW64 :%s:%s:0x%llX :%s:0x%08X",aszMemspaceName, aszSymbolicName,
                           uiPDumpSymbolicOffset, pszPDumpRegName, ui32Reg);
 
 
   if (eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
 
   PDumpWriteScript(hScript, ui32Flags);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
 
/**************************************************************************
 * Function Name  : PDumpMemLabelToInternalVar
 * Returns        : PVRSRV_ERROR
 * Description    : Create a PDUMP string, which represents an internal var write using a memory label
**************************************************************************/
PVRSRV_ERROR PDumpMemLabelToInternalVar(IMG_CHAR *pszInternalVar,
                                        PMR *psPMR,
                                        IMG_DEVMEM_OFFSET_T uiLogicalOffset,
                                        IMG_UINT32    ui32Flags)
{
   PVRSRV_ERROR eErr;
   IMG_CHAR aszMemspaceName[PHYSMEM_PDUMP_MEMSPACE_MAX_LENGTH];
   IMG_CHAR aszSymbolicName[PHYSMEM_PDUMP_SYMNAME_MAX_LENGTH];
   IMG_DEVMEM_OFFSET_T uiPDumpSymbolicOffset;
   IMG_DEVMEM_OFFSET_T uiNextSymName;
 
   PDUMP_GET_SCRIPT_STRING()
   PDUMP_DBG(("PDumpMemLabelToInternalVar"));
 
   eErr = PMR_PDumpSymbolicAddr(psPMR,
                                     uiLogicalOffset,
                                     PHYSMEM_PDUMP_MEMSPACE_MAX_LENGTH,
                                     aszMemspaceName,
                                     PHYSMEM_PDUMP_SYMNAME_MAX_LENGTH,
                                     aszSymbolicName,
                                     &uiPDumpSymbolicOffset,
                                     &uiNextSymName);
 
 
   if (eErr != PVRSRV_OK)
   {
       return eErr;
   }
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "WRW %s :%s:%s:0x%llX", pszInternalVar,
                           aszMemspaceName, aszSymbolicName, uiPDumpSymbolicOffset);
 
 
   if (eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
   PDumpWriteScript(hScript, ui32Flags);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
/*!
******************************************************************************
 
 @Function    PDumpWriteRegANDValueOp
 
 @Description
 
 Emits the PDump commands for the logical OR operation
 Var <- Var OR Value
 
 @Return   PVRSRV_ERROR
 
******************************************************************************/
PVRSRV_ERROR PDumpWriteVarORValueOp    (const IMG_CHAR *pszInternalVariable,
                                         const IMG_UINT64 ui64Value,
                                         const IMG_UINT32 ui32PDumpFlags)
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING();
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript,
           ui32MaxLen,
           "OR %s %s 0x%llX",
           pszInternalVariable,
           pszInternalVariable,
           ui64Value);
 
   if(eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
   PDumpWriteScript( hScript, ui32PDumpFlags);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
 
/*******************************************************************************************************
 * Function Name  : PDumpRegLabelToInternalVar
 * Outputs        : None
 * Returns        : PVRSRV_ERROR
 * Description    : Create a PDUMP string, which writes a register label into an internal variable
********************************************************************************************************/
PVRSRV_ERROR PDumpRegLabelToInternalVar(IMG_CHAR *pszPDumpRegName,
                                        IMG_UINT32 ui32Reg,
                                        IMG_CHAR *pszInternalVar,
                                        IMG_UINT32 ui32Flags)
 
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING()
   PDUMP_DBG(("PDumpRegLabelToInternalVar"));
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "WRW %s :%s:0x%08X", pszInternalVar, pszPDumpRegName, ui32Reg);
 
   if (eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
   PDumpWriteScript(hScript, ui32Flags);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
 
}
 
/*******************************************************************************************************
 * Function Name  : PDumpInternalVarToReg32
 * Outputs        : None
 * Returns        : PVRSRV_ERROR
 * Description    : Create a PDUMP string, which represents a register write from an internal variable
********************************************************************************************************/
PVRSRV_ERROR PDumpInternalVarToReg32(IMG_CHAR *pszPDumpRegName,
                                     IMG_UINT32    ui32Reg,
                                     IMG_CHAR *pszInternalVar,
                                     IMG_UINT32    ui32Flags)
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING()
   PDUMP_DBG(("PDumpInternalVarToReg32"));
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "WRW :%s:0x%08X %s", pszPDumpRegName, ui32Reg, pszInternalVar);
 
   if (eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
   PDumpWriteScript(hScript, ui32Flags);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
/*******************************************************************************************************
 * Function Name  : PDumpInternalVarToReg64
 * Outputs        : None
 * Returns        : PVRSRV_ERROR
 * Description    : Create a PDUMP string, which represents a register write from an internal variable
********************************************************************************************************/
PVRSRV_ERROR PDumpInternalVarToReg64(IMG_CHAR *pszPDumpRegName,
                                     IMG_UINT32    ui32Reg,
                                     IMG_CHAR *pszInternalVar,
                                     IMG_UINT32    ui32Flags)
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING()
   PDUMP_DBG(("PDumpInternalVarToReg64"));
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "WRW64 :%s:0x%08X %s", pszPDumpRegName, ui32Reg, pszInternalVar);
 
   if (eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
   PDumpWriteScript(hScript, ui32Flags);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
 
 
/*******************************************************************************************************
 * Function Name  : PDumpMemLabelToMem32
 * Outputs        : None
 * Returns        : PVRSRV_ERROR
 * Description    : Create a PDUMP string, which represents a memory write from a memory label
********************************************************************************************************/
PVRSRV_ERROR PDumpMemLabelToMem32(PMR *psPMRSource,
                                  PMR *psPMRDest,
                                  IMG_DEVMEM_OFFSET_T uiLogicalOffsetSource,
                                  IMG_DEVMEM_OFFSET_T uiLogicalOffsetDest,
                                  IMG_UINT32    ui32Flags)
{
   PVRSRV_ERROR eErr;
   IMG_CHAR aszMemspaceNameSource[PHYSMEM_PDUMP_MEMSPACE_MAX_LENGTH];
   IMG_CHAR aszSymbolicNameSource[PHYSMEM_PDUMP_SYMNAME_MAX_LENGTH];
   IMG_CHAR aszMemspaceNameDest[PHYSMEM_PDUMP_MEMSPACE_MAX_LENGTH];
   IMG_CHAR aszSymbolicNameDest[PHYSMEM_PDUMP_MEMSPNAME_SYMB_ADDR_MAX_LENGTH];
   IMG_DEVMEM_OFFSET_T uiPDumpSymbolicOffsetSource;
   IMG_DEVMEM_OFFSET_T uiPDumpSymbolicOffsetDest;
   IMG_DEVMEM_OFFSET_T uiNextSymNameSource;
   IMG_DEVMEM_OFFSET_T uiNextSymNameDest;
 
 
   PDUMP_GET_SCRIPT_STRING()
   PDUMP_DBG(("PDumpMemLabelToMem32"));
 
   eErr = PMR_PDumpSymbolicAddr(psPMRSource,
                                     uiLogicalOffsetSource,
                                     PHYSMEM_PDUMP_MEMSPACE_MAX_LENGTH,
                                     aszMemspaceNameSource,
                                     PHYSMEM_PDUMP_SYMNAME_MAX_LENGTH,
                                     aszSymbolicNameSource,
                                     &uiPDumpSymbolicOffsetSource,
                                     &uiNextSymNameSource);
 
   if (eErr != PVRSRV_OK)
   {
       return eErr;
   }
 
   eErr = PMR_PDumpSymbolicAddr(psPMRDest,
                                     uiLogicalOffsetDest,
                                     PHYSMEM_PDUMP_MEMSPACE_MAX_LENGTH,
                                     aszMemspaceNameDest,
                                     PHYSMEM_PDUMP_SYMNAME_MAX_LENGTH,
                                     aszSymbolicNameDest,
                                     &uiPDumpSymbolicOffsetDest,
                                     &uiNextSymNameDest);
 
 
   if (eErr != PVRSRV_OK)
   {
       return eErr;
   }
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "WRW :%s:%s:0x%llX :%s:%s:0x%llX",aszMemspaceNameDest, aszSymbolicNameDest,
                           uiPDumpSymbolicOffsetDest, aszMemspaceNameSource, aszSymbolicNameSource,
                           uiPDumpSymbolicOffsetSource);
 
 
   if (eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
 
   PDumpWriteScript(hScript, ui32Flags);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
/*******************************************************************************************************
 * Function Name  : PDumpMemLabelToMem64
 * Outputs        : None
 * Returns        : PVRSRV_ERROR
 * Description    : Create a PDUMP string, which represents a memory write from a memory label
********************************************************************************************************/
PVRSRV_ERROR PDumpMemLabelToMem64(PMR *psPMRSource,
                                 PMR *psPMRDest,
                                 IMG_DEVMEM_OFFSET_T uiLogicalOffsetSource,
                                 IMG_DEVMEM_OFFSET_T uiLogicalOffsetDest,
                                 IMG_UINT32    ui32Flags)
{
   PVRSRV_ERROR eErr;
   IMG_CHAR aszMemspaceNameSource[PHYSMEM_PDUMP_MEMSPACE_MAX_LENGTH];
   IMG_CHAR aszSymbolicNameSource[PHYSMEM_PDUMP_SYMNAME_MAX_LENGTH];
   IMG_CHAR aszMemspaceNameDest[PHYSMEM_PDUMP_MEMSPACE_MAX_LENGTH];
   IMG_CHAR aszSymbolicNameDest[PHYSMEM_PDUMP_SYMNAME_MAX_LENGTH];
   IMG_DEVMEM_OFFSET_T uiPDumpSymbolicOffsetSource;
   IMG_DEVMEM_OFFSET_T uiPDumpSymbolicOffsetDest;
   IMG_DEVMEM_OFFSET_T uiNextSymNameSource;
   IMG_DEVMEM_OFFSET_T uiNextSymNameDest;
 
 
   PDUMP_GET_SCRIPT_STRING()
   PDUMP_DBG(("PDumpMemLabelToMem64"));
 
   eErr = PMR_PDumpSymbolicAddr(psPMRSource,
                                    uiLogicalOffsetSource,
                                    PHYSMEM_PDUMP_MEMSPACE_MAX_LENGTH,
                                    aszMemspaceNameSource,
                                    PHYSMEM_PDUMP_SYMNAME_MAX_LENGTH,
                                    aszSymbolicNameSource,
                                    &uiPDumpSymbolicOffsetSource,
                                    &uiNextSymNameSource);
 
   if (eErr != PVRSRV_OK)
   {
       return eErr;
   }
 
   eErr = PMR_PDumpSymbolicAddr(psPMRDest,
                                    uiLogicalOffsetDest,
                                    PHYSMEM_PDUMP_MEMSPACE_MAX_LENGTH,
                                    aszMemspaceNameDest,
                                    PHYSMEM_PDUMP_SYMNAME_MAX_LENGTH,
                                    aszSymbolicNameDest,
                                    &uiPDumpSymbolicOffsetDest,
                                    &uiNextSymNameDest);
 
 
   if (eErr != PVRSRV_OK)
   {
       return eErr;
   }
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "WRW64 :%s:%s:0x%llX :%s:%s:0x%llX",aszMemspaceNameDest, aszSymbolicNameDest,
                           uiPDumpSymbolicOffsetDest, aszMemspaceNameSource, aszSymbolicNameSource,
                           uiPDumpSymbolicOffsetSource);
 
 
   if (eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
 
   PDumpWriteScript(hScript, ui32Flags);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
 
 
/*!
******************************************************************************
 
 @Function    PDumpWriteVarSHRValueOp
 
 @Description
 
 Emits the PDump commands for the logical SHR operation
 Var <-  Var SHR Value
 
 @Return   PVRSRV_ERROR
 
******************************************************************************/
PVRSRV_ERROR PDumpWriteVarSHRValueOp (const IMG_CHAR *pszInternalVariable,
                                      const IMG_UINT64 ui64Value,
                                      const IMG_UINT32 ui32PDumpFlags)
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING();
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript,
           ui32MaxLen,
           "SHR %s %s 0x%llX",
           pszInternalVariable,
           pszInternalVariable,
           ui64Value);
 
   if(eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
   PDumpWriteScript( hScript, ui32PDumpFlags);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
 
/*!
******************************************************************************
 
 @Function    PDumpWriteRegANDValueOp
 
 @Description
 
 Emits the PDump commands for the logical AND operation
 Var <-  Var AND Value
 
 @Return   PVRSRV_ERROR
 
******************************************************************************/
PVRSRV_ERROR PDumpWriteVarANDValueOp (const IMG_CHAR *pszInternalVariable,
                                      const IMG_UINT64 ui64Value,
                                      const IMG_UINT32 ui32PDumpFlags)
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING();
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript,
           ui32MaxLen,
           "AND %s %s 0x%llX",
           pszInternalVariable,
           pszInternalVariable,
           ui64Value);
 
   if(eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
   PDumpWriteScript( hScript, ui32PDumpFlags);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
 
/**************************************************************************
 * Function Name  : PDumpSAW
 * Inputs         : pszDevSpaceName -- device space from which to output
 *                  ui32Offset -- offset value from register base
 *                  ui32NumSaveBytes -- number of bytes to output
 *                  pszOutfileName -- name of file to output to
 *                  ui32OutfileOffsetByte -- offset into output file to write
 *                  uiPDumpFlags -- flags to pass to PDumpOSWriteScript
 * Outputs        : None
 * Returns        : PVRSRV_ERROR
 * Description    : Dumps the contents of a register bank into a file
 *                  NB: ui32NumSaveBytes must be divisible by 4
**************************************************************************/
PVRSRV_ERROR PDumpSAW(IMG_CHAR      *pszDevSpaceName,
                      IMG_UINT32    ui32HPOffsetBytes,
                      IMG_UINT32    ui32NumSaveBytes,
                      IMG_CHAR      *pszOutfileName,
                      IMG_UINT32    ui32OutfileOffsetByte,
                      PDUMP_FLAGS_T uiPDumpFlags)
{
   PVRSRV_ERROR eError;
 
   PDUMP_GET_SCRIPT_STRING()
 
   PVR_DPF((PVR_DBG_ERROR, "PDumpSAW\n"));
 
   PDUMP_LOCK();
   eError = PDumpOSBufprintf(hScript,
                             ui32MaxLen,
                             "SAW :%s:0x%x 0x%x 0x%x %s\n",
                             pszDevSpaceName,
                             ui32HPOffsetBytes,
                             ui32NumSaveBytes / (IMG_UINT32)sizeof(IMG_UINT32),
                             ui32OutfileOffsetByte,
                             pszOutfileName);
 
   if(eError != PVRSRV_OK)
   {
       PVR_DPF((PVR_DBG_ERROR, "PDumpSAW PDumpOSBufprintf failed: eError=%u\n", eError));
       PDUMP_UNLOCK();
       return eError;
   }
 
   if(! PDumpWriteScript(hScript, uiPDumpFlags))
   {
       PVR_DPF((PVR_DBG_ERROR, "PDumpSAW PDumpWriteScript failed!\n"));
   }
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
   
}
 
 
/**************************************************************************
 * Function Name  : PDumpRegPolKM
 * Inputs         : Description of what this register read is trying to do
 *                    pszPDumpDevName
 *                    Register offset
 *                    expected value
 *                    mask for that value
 * Outputs        : None
 * Returns        : None
 * Description    : Create a PDUMP string which represents a register read
 *                    with the expected value
**************************************************************************/
PVRSRV_ERROR PDumpRegPolKM(IMG_CHAR                *pszPDumpRegName,
                          IMG_UINT32            ui32RegAddr, 
                          IMG_UINT32            ui32RegValue, 
                          IMG_UINT32            ui32Mask,
                          IMG_UINT32            ui32Flags,
                          PDUMP_POLL_OPERATOR    eOperator)
{
   /* Timings correct for linux and XP */
   /* Timings should be passed in */
   #define POLL_DELAY            1000U
   #define POLL_COUNT_LONG        (2000000000U / POLL_DELAY)
   #define POLL_COUNT_SHORT    (1000000U / POLL_DELAY)
 
   PVRSRV_ERROR eErr;
   IMG_UINT32    ui32PollCount;
 
   PDUMP_GET_SCRIPT_STRING();
   PDUMP_DBG(("PDumpRegPolKM"));
 
   ui32PollCount = POLL_COUNT_LONG;
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "POL :%s:0x%08X 0x%08X 0x%08X %d %u %d",
                           pszPDumpRegName, ui32RegAddr, ui32RegValue,
                           ui32Mask, eOperator, ui32PollCount, POLL_DELAY);
   if(eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
   PDumpWriteScript(hScript, ui32Flags);
 
   PDUMP_UNLOCK();
   return PVRSRV_OK;
}
 
/* Never call direct, needs caller to hold OS Lock.
 * Use PDumpCommentWithFlags() from within the server.
 * Clients call this via the bridge and PDumpCommentKM().
 */
static PVRSRV_ERROR _PDumpWriteComment(IMG_CHAR *pszComment, IMG_UINT32 ui32Flags)
{
   PVRSRV_ERROR eErr;
#if defined(PDUMP_DEBUG_OUTFILES)
   IMG_CHAR pszTemp[256];
#endif
   PDUMP_GET_SCRIPT_STRING();
   PDUMP_DBG(("PDumpCommentKM"));
 
   if((pszComment == NULL) || (PDumpOSBuflen(pszComment, ui32MaxLen) == 0))
   {
       /* PDumpOSVerifyLineEnding silently fails if pszComment is too short to
          actually hold the line endings that it's trying to enforce, so
          short circuit it and force safety */
       pszComment = "\n";
   }
   else
   {
       /* Put line ending sequence at the end if it isn't already there */
       PDumpOSVerifyLineEnding(pszComment, ui32MaxLen);
   }
 
#if defined(PDUMP_DEBUG_OUTFILES)
   /* Prefix comment with PID and line number */
   eErr = PDumpOSSprintf(pszTemp, 256, "%u %u:%lu %s: %s",
       g_ui32EveryLineCounter,
       OSGetCurrentClientProcessIDKM(),
       (unsigned long)OSGetCurrentClientThreadIDKM(),
       OSGetCurrentClientProcessNameKM(),
       pszComment);
 
   /* Append the comment to the script stream */
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "-- %s",
       pszTemp);
#else
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "-- %s",
       pszComment);
#endif
   if( (eErr != PVRSRV_OK) &&
       (eErr != PVRSRV_ERROR_PDUMP_BUF_OVERFLOW))
   {
       PVR_LOGG_IF_ERROR(eErr, "PDumpOSBufprintf", ErrUnlock);
   }
 
   if (!PDumpWriteScript(hScript, ui32Flags))
   {
       if(PDUMP_IS_CONTINUOUS(ui32Flags))
       {
           eErr = PVRSRV_ERROR_PDUMP_BUFFER_FULL;
           PVR_LOGG_IF_ERROR(eErr, "PDumpWriteScript", ErrUnlock);
       }
       else
       {
           eErr = PVRSRV_ERROR_CMD_NOT_PROCESSED;
           PVR_LOGG_IF_ERROR(eErr, "PDumpWriteScript", ErrUnlock);
       }
   }
 
ErrUnlock:
   return eErr;
}
 
/**************************************************************************
 * Function Name  : PDumpCommentKM
 * Inputs         : pszComment, ui32Flags
 * Outputs        : None
 * Returns        : None
 * Description    : Dumps a pre-formatted comment, primarily called from the
 *                : bridge.
**************************************************************************/
PVRSRV_ERROR PDumpCommentKM(IMG_CHAR *pszComment, IMG_UINT32 ui32Flags)
{
   PVRSRV_ERROR eErr = PVRSRV_OK;
 
   PDUMP_LOCK();
 
   eErr =  _PDumpWriteComment(pszComment, ui32Flags);
 
   PDUMP_UNLOCK();
   return eErr;
}
 
/**************************************************************************
 * Function Name  : PDumpCommentWithFlags
 * Inputs         : psPDev - PDev for PDump device
 *                  : pszFormat - format string for comment
 *                  : ... - args for format string
 * Outputs        : None
 * Returns        : None
 * Description    : PDumps a comments
**************************************************************************/
PVRSRV_ERROR PDumpCommentWithFlags(IMG_UINT32 ui32Flags, IMG_CHAR * pszFormat, ...)
{
   PVRSRV_ERROR eErr = PVRSRV_OK;
   va_list args;
 
   va_start(args, pszFormat);
   PDumpCommentWithFlagsVA(ui32Flags, pszFormat, args);
   va_end(args);
 
   return eErr;
}
 
/**************************************************************************
 * Function Name  : PDumpCommentWithFlagsVA
 * Inputs         : psPDev    - PDev for PDump device
 *                  : pszFormat - format string for comment
 *                  : args      - pre-started va_list args for format string
 * Outputs        : None
 * Returns        : None
 * Description    : PDumps a comments
**************************************************************************/
PVRSRV_ERROR PDumpCommentWithFlagsVA(IMG_UINT32 ui32Flags, const IMG_CHAR * pszFormat, va_list args)
{
   PVRSRV_ERROR eErr = PVRSRV_OK;
   PDUMP_GET_MSG_STRING();
 
   PDUMP_LOCK();
 
   /* Construct the string */
   eErr = PDumpOSVSprintf(pszMsg, ui32MaxLen, pszFormat, args);
 
   if(eErr != PVRSRV_OK)
   {
       goto Unlock;
   }
 
   eErr =  _PDumpWriteComment(pszMsg, ui32Flags);
 
Unlock:
   PDUMP_UNLOCK();
   return eErr;
}
 
/*************************************************************************/ /*!
 * Function Name  : PDumpPanic
 * Inputs         : ui32PanicNo - Unique number for panic condition
 *                  : pszPanicMsg - Panic reason message limited to ~90 chars
 *                  : pszPPFunc   - Function name string where panic occurred
 *                  : ui32PPline  - Source line number where panic occurred
 * Outputs        : None
 * Returns        : PVRSRV_ERROR
 * Description    : PDumps a panic assertion. Used when the host driver
 *                : detects a condition that will lead to an invalid PDump
 *                : script that cannot be played back off-line.
 */ /*************************************************************************/
PVRSRV_ERROR PDumpPanic(IMG_UINT32      ui32PanicNo,
                       IMG_CHAR*       pszPanicMsg,
                       const IMG_CHAR* pszPPFunc,
                       IMG_UINT32      ui32PPline)
{
   PVRSRV_ERROR   eError = PVRSRV_OK;
   PDUMP_FLAGS_T  uiPDumpFlags = PDUMP_FLAGS_CONTINUOUS;
   IMG_CHAR       pszConsoleMsg[] =
"COM ***************************************************************************\n"
"COM Script invalid and not compatible with off-line playback. Check test \n"
"COM parameters and driver configuration, stop imminent.\n"
"COM ***************************************************************************\n";
   PDUMP_GET_SCRIPT_STRING();
 
   /* Log the panic condition to the live kern.log in both REL and DEB mode 
    * to aid user PDump trouble shooting. */
   PVR_LOG(("PDUMP PANIC %08x: %s", ui32PanicNo, pszPanicMsg));
   PVR_DPF((PVR_DBG_MESSAGE, "PDUMP PANIC start %s:%d", pszPPFunc, ui32PPline));
 
   /* Check the supplied panic reason string is within length limits */
   PVR_ASSERT(OSStringLength(pszPanicMsg)+sizeof("PANIC   ") < PVRSRV_PDUMP_MAX_COMMENT_SIZE-1);
 
   /* Obtain lock to keep the multi-line
    * panic statement together in a single atomic write */
   PDUMP_LOCK();
 
 
   /* Write -- Panic start (Function:line) */
   eError = PDumpOSBufprintf(hScript, ui32MaxLen, "-- Panic start (%s:%d)", pszPPFunc, ui32PPline);
   PVR_LOGG_IF_ERROR(eError, "PDumpOSBufprintf", e1);
   (void)PDumpWriteScript(hScript, uiPDumpFlags);
 
   /* Write COM <message> x4 */
   eError = PDumpOSBufprintf(hScript, ui32MaxLen, pszConsoleMsg);
   PVR_LOGG_IF_ERROR(eError, "PDumpOSBufprintf", e1);
   (void)PDumpWriteScript(hScript, uiPDumpFlags);
 
   /* Write PANIC no msg command */
   eError = PDumpOSBufprintf(hScript, ui32MaxLen, "PANIC %08x %s", ui32PanicNo, pszPanicMsg);
   PVR_LOGG_IF_ERROR(eError, "PDumpOSBufprintf", e1);
   (void)PDumpWriteScript(hScript, uiPDumpFlags);
 
   /* Write -- Panic end */
   eError = PDumpOSBufprintf(hScript, ui32MaxLen, "-- Panic end");
   PVR_LOGG_IF_ERROR(eError, "PDumpOSBufprintf", e1);
   (void)PDumpWriteScript(hScript, uiPDumpFlags);
 
e1:
   PDUMP_UNLOCK();
 
   return eError;
}
 
/*************************************************************************/ /*!
 * Function Name  : PDumpCaptureError
 * Inputs         : ui32ErrorNo - Unique number for panic condition
 *                : pszErrorMsg - Panic reason message limited to ~90 chars
 *                : pszPPFunc   - Function name string where panic occurred
 *                : ui32PPline  - Source line number where panic occurred
 * Outputs        : None
 * Returns        : PVRSRV_ERROR
 * Description    : PDumps an error string to the script file to interrupt
 *                : play back to inform user of a fatal issue that occurred
 *                : during PDump capture.
 */ /*************************************************************************/
PVRSRV_ERROR PDumpCaptureError(PVRSRV_ERROR    ui32ErrorNo,
                       IMG_CHAR*       pszErrorMsg,
                       const IMG_CHAR* pszPPFunc,
                       IMG_UINT32      ui32PPline)
{
   IMG_CHAR*       pszFormatStr = "DRIVER_ERROR: %3d: %s";
   PDUMP_FLAGS_T   uiPDumpFlags = PDUMP_FLAGS_CONTINUOUS;
 
   /* Need to return an error using this macro */
   PDUMP_GET_SCRIPT_STRING();
 
   /* Check the supplied panic reason string is within length limits */
   PVR_ASSERT(OSStringLength(pszErrorMsg)+sizeof(pszFormatStr) < PVRSRV_PDUMP_MAX_COMMENT_SIZE-1);
 
   /* Obtain lock to keep the multi-line 
    * panic statement together in a single atomic write */
   PDUMP_LOCK();
 
   /* Write driver error message to the script file */
   (void) PDumpOSBufprintf(hScript, ui32MaxLen, pszFormatStr, ui32ErrorNo, pszErrorMsg);
   (void) PDumpWriteScript(hScript, uiPDumpFlags);
 
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
/*!
******************************************************************************
 
 @Function    PDumpBitmapKM
 
 @Description
 
 Dumps a bitmap from device memory to a file
 
 @Input    psDevId
 @Input    pszFileName
 @Input    ui32FileOffset
 @Input    ui32Width
 @Input    ui32Height
 @Input    ui32StrideInBytes
 @Input    sDevBaseAddr
 @Input    ui32Size
 @Input    ePixelFormat
 @Input    eMemFormat
 @Input    ui32PDumpFlags
 
 @Return   PVRSRV_ERROR            :
 
******************************************************************************/
PVRSRV_ERROR PDumpBitmapKM(    PVRSRV_DEVICE_NODE *psDeviceNode,
                           IMG_CHAR *pszFileName,
                           IMG_UINT32 ui32FileOffset,
                           IMG_UINT32 ui32Width,
                           IMG_UINT32 ui32Height,
                           IMG_UINT32 ui32StrideInBytes,
                           IMG_DEV_VIRTADDR sDevBaseAddr,
                           IMG_UINT32 ui32MMUContextID,
                           IMG_UINT32 ui32Size,
                           PDUMP_PIXEL_FORMAT ePixelFormat,
                           IMG_UINT32 ui32AddrMode,
                           IMG_UINT32 ui32PDumpFlags)
{
   PVRSRV_DEVICE_IDENTIFIER *psDevId = &psDeviceNode->sDevId;
   PVRSRV_ERROR eErr=0;
   PDUMP_GET_SCRIPT_STRING();
 
   PDumpCommentWithFlags(ui32PDumpFlags, "Dump bitmap of render.");
   
   switch (ePixelFormat)
   {
       case PVRSRV_PDUMP_PIXEL_FORMAT_YUV8:
       {
           PDumpCommentWithFlags(ui32PDumpFlags, "YUV data. Switching from SII to SAB. Width=0x%08X Height=0x%08X Stride=0x%08X",
                                                    ui32Width, ui32Height, ui32StrideInBytes);
           PDUMP_LOCK();
           eErr = PDumpOSBufprintf(hScript,
                                   ui32MaxLen,
                                   "SAB :%s:v%x:0x%010llX 0x%08X 0x%08X %s.bin\n",
                                   psDevId->pszPDumpDevName,
                                   ui32MMUContextID,
                                   sDevBaseAddr.uiAddr,
                                   ui32Size,
                                   ui32FileOffset,
                                   pszFileName);
           
           if (eErr != PVRSRV_OK)
           {
               PDUMP_UNLOCK();
               return eErr;
           }
           
           PDumpWriteScript( hScript, ui32PDumpFlags);
           PDUMP_UNLOCK();        
           break;
       }
       case PVRSRV_PDUMP_PIXEL_FORMAT_420PL12YUV8: // YUV420 2 planes
       {
           const IMG_UINT32 ui32Plane0Size = ui32StrideInBytes*ui32Height;
           const IMG_UINT32 ui32Plane1Size = ui32Plane0Size>>1; // YUV420
           const IMG_UINT32 ui32Plane1FileOffset = ui32FileOffset + ui32Plane0Size;
           const IMG_UINT32 ui32Plane1MemOffset = ui32Plane0Size;
           
           PDumpCommentWithFlags(ui32PDumpFlags, "YUV420 2-plane. Width=0x%08X Height=0x%08X Stride=0x%08X",
                                                    ui32Width, ui32Height, ui32StrideInBytes);
           PDUMP_LOCK();
           eErr = PDumpOSBufprintf(hScript,
                       ui32MaxLen,
                       "SII %s %s.bin :%s:v%x:0x%010llX 0x%08X 0x%08X :%s:v%x:0x%010llX 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X",
                       pszFileName,
                       pszFileName,
                       
                       // Plane 0 (Y)
                       psDevId->pszPDumpDevName,    // memsp
                       ui32MMUContextID,            // Context id
                       sDevBaseAddr.uiAddr,        // virtaddr
                       ui32Plane0Size,                // size
                       ui32FileOffset,                // fileoffset
                       
                       // Plane 1 (UV)
                       psDevId->pszPDumpDevName,    // memsp
                       ui32MMUContextID,            // Context id
                       sDevBaseAddr.uiAddr+ui32Plane1MemOffset,    // virtaddr
                       ui32Plane1Size,                // size
                       ui32Plane1FileOffset,        // fileoffset
                       
                       ePixelFormat,
                       ui32Width,
                       ui32Height,
                       ui32StrideInBytes,
                       ui32AddrMode);
                       
           if (eErr != PVRSRV_OK)
           {
               PDUMP_UNLOCK();
               return eErr;
           }
           
           PDumpWriteScript( hScript, ui32PDumpFlags);
           PDUMP_UNLOCK();
           break;
       }
       
       case PVRSRV_PDUMP_PIXEL_FORMAT_YUV_YV12: // YUV420 3 planes
       {
           const IMG_UINT32 ui32Plane0Size = ui32StrideInBytes*ui32Height;
           const IMG_UINT32 ui32Plane1Size = ui32Plane0Size>>2; // YUV420
           const IMG_UINT32 ui32Plane2Size = ui32Plane1Size;
           const IMG_UINT32 ui32Plane1FileOffset = ui32FileOffset + ui32Plane0Size;
           const IMG_UINT32 ui32Plane2FileOffset = ui32Plane1FileOffset + ui32Plane1Size;
           const IMG_UINT32 ui32Plane1MemOffset = ui32Plane0Size;
           const IMG_UINT32 ui32Plane2MemOffset = ui32Plane0Size+ui32Plane1Size;
   
           PDumpCommentWithFlags(ui32PDumpFlags, "YUV420 3-plane. Width=0x%08X Height=0x%08X Stride=0x%08X",
                                                    ui32Width, ui32Height, ui32StrideInBytes);
           PDUMP_LOCK();
           eErr = PDumpOSBufprintf(hScript,
                       ui32MaxLen,
                       "SII %s %s.bin :%s:v%x:0x%010llX 0x%08X 0x%08X :%s:v%x:0x%010llX 0x%08X 0x%08X :%s:v%x:0x%010llX 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X",
                       pszFileName,
                       pszFileName,
                       
                       // Plane 0 (Y)
                       psDevId->pszPDumpDevName,    // memsp
                       ui32MMUContextID,            // MMU context id
                       sDevBaseAddr.uiAddr,        // virtaddr
                       ui32Plane0Size,                // size
                       ui32FileOffset,                // fileoffset
                       
                       // Plane 1 (U)
                       psDevId->pszPDumpDevName,    // memsp
                       ui32MMUContextID,            // MMU context id
                       sDevBaseAddr.uiAddr+ui32Plane1MemOffset,    // virtaddr
                       ui32Plane1Size,                // size
                       ui32Plane1FileOffset,        // fileoffset
                       
                       // Plane 2 (V)
                       psDevId->pszPDumpDevName,    // memsp
                       ui32MMUContextID,            // MMU context id
                       sDevBaseAddr.uiAddr+ui32Plane2MemOffset,    // virtaddr
                       ui32Plane2Size,                // size
                       ui32Plane2FileOffset,        // fileoffset
                       
                       ePixelFormat,
                       ui32Width,
                       ui32Height,
                       ui32StrideInBytes,
                       ui32AddrMode);
                       
           if (eErr != PVRSRV_OK)
           {
               PDUMP_UNLOCK();
               return eErr;
           }
           
           PDumpWriteScript( hScript, ui32PDumpFlags);
           PDUMP_UNLOCK();
           break;
       }
       
       case PVRSRV_PDUMP_PIXEL_FORMAT_YUV_YV32: // YV32 - 4 contiguous planes in the order VUYA, stride can be > width.
       {
           const IMG_UINT32 ui32PlaneSize = ui32StrideInBytes*ui32Height; // All 4 planes are the same size
           const IMG_UINT32 ui32Plane0FileOffset = ui32FileOffset + (ui32PlaneSize<<1);        // SII plane 0 is Y, which is YV32 plane 2
           const IMG_UINT32 ui32Plane1FileOffset = ui32FileOffset + ui32PlaneSize;                // SII plane 1 is U, which is YV32 plane 1
           const IMG_UINT32 ui32Plane2FileOffset = ui32FileOffset;                                // SII plane 2 is V, which is YV32 plane 0
           const IMG_UINT32 ui32Plane3FileOffset = ui32Plane0FileOffset + ui32PlaneSize;        // SII plane 3 is A, which is YV32 plane 3
           const IMG_UINT32 ui32Plane0MemOffset = ui32PlaneSize<<1;
           const IMG_UINT32 ui32Plane1MemOffset = ui32PlaneSize;
           const IMG_UINT32 ui32Plane2MemOffset = 0;
           const IMG_UINT32 ui32Plane3MemOffset = ui32Plane0MemOffset + ui32PlaneSize;
                                                    
           PDumpCommentWithFlags(ui32PDumpFlags, "YV32 4 planes. Width=0x%08X Height=0x%08X Stride=0x%08X",
                                                    ui32Width, ui32Height, ui32StrideInBytes);
           
           PDumpCommentWithFlags(ui32PDumpFlags, "YV32 plane size is 0x%08X", ui32PlaneSize);
           
           PDumpCommentWithFlags(ui32PDumpFlags, "YV32 Plane 0 Mem Offset=0x%08X", ui32Plane0MemOffset);
           PDumpCommentWithFlags(ui32PDumpFlags, "YV32 Plane 1 Mem Offset=0x%08X", ui32Plane1MemOffset);
           PDumpCommentWithFlags(ui32PDumpFlags, "YV32 Plane 2 Mem Offset=0x%08X", ui32Plane2MemOffset);
           PDumpCommentWithFlags(ui32PDumpFlags, "YV32 Plane 3 Mem Offset=0x%08X", ui32Plane3MemOffset);
           
           /*
               SII <imageset> <filename>    :<memsp1>:v<id1>:<virtaddr1> <size1> <fileoffset1>        Y
                                           :<memsp2>:v<id2>:<virtaddr2> <size2> <fileoffset2>        U
                                           :<memsp3>:v<id3>:<virtaddr3> <size3> <fileoffset3>        V
                                           :<memsp4>:v<id4>:<virtaddr4> <size4> <fileoffset4>        A
                                           <pixfmt> <width> <height> <stride> <addrmode>
           */
           PDUMP_LOCK();
           eErr = PDumpOSBufprintf(hScript,
                       ui32MaxLen,
                       "SII %s %s.bin :%s:v%x:0x%010llX 0x%08X 0x%08X :%s:v%x:0x%010llX 0x%08X 0x%08X :%s:v%x:0x%010llX 0x%08X 0x%08X :%s:v%x:0x%010llX 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X",
                       pszFileName,
                       pszFileName,
                       
                       // Plane 0 (V)
                       psDevId->pszPDumpDevName,    // memsp
                       ui32MMUContextID,            // MMU context id
                       sDevBaseAddr.uiAddr+ui32Plane0MemOffset,    // virtaddr
                       ui32PlaneSize,                // size
                       ui32Plane0FileOffset,        // fileoffset
                       
                       // Plane 1 (U)
                       psDevId->pszPDumpDevName,    // memsp
                       ui32MMUContextID,            // MMU context id
                       sDevBaseAddr.uiAddr+ui32Plane1MemOffset,    // virtaddr
                       ui32PlaneSize,                // size
                       ui32Plane1FileOffset,        // fileoffset
                       
                       // Plane 2 (Y)
                       psDevId->pszPDumpDevName,    // memsp
                       ui32MMUContextID,            // MMU context id
                       sDevBaseAddr.uiAddr+ui32Plane2MemOffset,    // virtaddr
                       ui32PlaneSize,                // size
                       ui32Plane2FileOffset,        // fileoffset
                       
                       // Plane 3 (A)
                       psDevId->pszPDumpDevName,    // memsp
                       ui32MMUContextID,            // MMU context id
                       sDevBaseAddr.uiAddr+ui32Plane3MemOffset,    // virtaddr
                       ui32PlaneSize,                // size
                       ui32Plane3FileOffset,        // fileoffset
                       
                       ePixelFormat,
                       ui32Width,
                       ui32Height,
                       ui32StrideInBytes,
                       ui32AddrMode);
                       
           if (eErr != PVRSRV_OK)
           {
               PDUMP_UNLOCK();
               return eErr;
           }
           
           PDumpWriteScript( hScript, ui32PDumpFlags);
           PDUMP_UNLOCK();
           break;
       }
               
       default: // Single plane formats
       {
           PDUMP_LOCK();
           eErr = PDumpOSBufprintf(hScript,
                       ui32MaxLen,
                       "SII %s %s.bin :%s:v%x:0x%010llX 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X",
                       pszFileName,
                       pszFileName,
                       psDevId->pszPDumpDevName,
                       ui32MMUContextID,
                       sDevBaseAddr.uiAddr,
                       ui32Size,
                       ui32FileOffset,
                       ePixelFormat,
                       ui32Width,
                       ui32Height,
                       ui32StrideInBytes,
                       ui32AddrMode);
                       
           if (eErr != PVRSRV_OK)
           {
               PDUMP_UNLOCK();
               return eErr;
           }
 
           PDumpWriteScript( hScript, ui32PDumpFlags);
           PDUMP_UNLOCK();
           break;
       }
   }
 
   return PVRSRV_OK;
}
 
/*!
******************************************************************************
 
 @Function    PDumpReadRegKM
 
 @Description
 
 Dumps a read from a device register to a file
 
 @Input    psConnection         : connection info
 @Input    pszFileName
 @Input    ui32FileOffset
 @Input    ui32Address
 @Input    ui32Size
 @Input    ui32PDumpFlags
 
 @Return   PVRSRV_ERROR            :
 
******************************************************************************/
PVRSRV_ERROR PDumpReadRegKM        (    IMG_CHAR *pszPDumpRegName,
                                   IMG_CHAR *pszFileName,
                                   IMG_UINT32 ui32FileOffset,
                                   IMG_UINT32 ui32Address,
                                   IMG_UINT32 ui32Size,
                                   IMG_UINT32 ui32PDumpFlags)
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING();
 
   PVR_UNREFERENCED_PARAMETER(ui32Size);
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript,
           ui32MaxLen,
           "SAB :%s:0x%08X 0x%08X %s",
           pszPDumpRegName,
           ui32Address,
           ui32FileOffset,
           pszFileName);
   if(eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
   PDumpWriteScript( hScript, ui32PDumpFlags);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
/*****************************************************************************
 @name        PDumpRegRead32
 @brief        Dump 32-bit register read to script
 @param        pszPDumpDevName - pdump device name
 @param        ui32RegOffset - register offset
 @param        ui32Flags - pdump flags
 @return    Error
*****************************************************************************/
PVRSRV_ERROR PDumpRegRead32(IMG_CHAR *pszPDumpRegName,
                           const IMG_UINT32 ui32RegOffset,
                           IMG_UINT32 ui32Flags)
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING();
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "RDW :%s:0x%X",
                           pszPDumpRegName, 
                           ui32RegOffset);
   if(eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
   PDumpWriteScript(hScript, ui32Flags);
   PDUMP_UNLOCK();
   return PVRSRV_OK;
}
 
/*****************************************************************************
 @name        PDumpRegRead64
 @brief        Dump 64-bit register read to script
 @param        pszPDumpDevName - pdump device name
 @param        ui32RegOffset - register offset
 @param        ui32Flags - pdump flags
 @return    Error
*****************************************************************************/
PVRSRV_ERROR PDumpRegRead64(IMG_CHAR *pszPDumpRegName,
                           const IMG_UINT32 ui32RegOffset,
                           IMG_UINT32 ui32Flags)
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING();
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "RDW64 :%s:0x%X",
                           pszPDumpRegName, 
                           ui32RegOffset);
   if(eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
   PDumpWriteScript(hScript, ui32Flags);
   PDUMP_UNLOCK();
   return PVRSRV_OK;
}
 
 
/*****************************************************************************
 FUNCTION    : PDumpWriteShiftedMaskedValue
 
 PURPOSE    : Emits the PDump commands for writing a masked shifted address
              into another location
 
 PARAMETERS    : PDump symbolic name and offset of target word
              PDump symbolic name and offset of source address
              right shift amount
              left shift amount
              mask
 
 RETURNS    : None
*****************************************************************************/
PVRSRV_ERROR
PDumpWriteShiftedMaskedValue(const IMG_CHAR *pszDestRegspaceName,
                             const IMG_CHAR *pszDestSymbolicName,
                             IMG_DEVMEM_OFFSET_T uiDestOffset,
                             const IMG_CHAR *pszRefRegspaceName,
                             const IMG_CHAR *pszRefSymbolicName,
                             IMG_DEVMEM_OFFSET_T uiRefOffset,
                             IMG_UINT32 uiSHRAmount,
                             IMG_UINT32 uiSHLAmount,
                             IMG_UINT32 uiMask,
                             IMG_DEVMEM_SIZE_T uiWordSize,
                             IMG_UINT32 uiPDumpFlags)
{
   PVRSRV_ERROR         eError;
 
    /* Suffix of WRW command in PDump (i.e. WRW or WRW64) */
    const IMG_CHAR       *pszWrwSuffix;
 
    /* Internal PDump register used for interim calculation */
    const IMG_CHAR       *pszPDumpIntRegSpace;
    IMG_UINT32           uiPDumpIntRegNum;
 
   PDUMP_GET_SCRIPT_STRING();
 
    if ((uiWordSize != 4) && (uiWordSize != 8))
    {
        return PVRSRV_ERROR_NOT_SUPPORTED;
    }
 
    pszWrwSuffix = (uiWordSize == 8) ? "64" : "";
 
    /* Should really "Acquire" a pdump register here */
    pszPDumpIntRegSpace = pszDestRegspaceName;
    uiPDumpIntRegNum = 1;
        
   PDUMP_LOCK();
   eError = PDumpOSBufprintf(hScript,
                              ui32MaxLen,
                              /* Should this be "MOV" instead? */
                              "WRW :%s:$%d :%s:%s:" IMG_DEVMEM_OFFSET_FMTSPEC "\n",
                              /* dest */
                              pszPDumpIntRegSpace,
                              uiPDumpIntRegNum,
                              /* src */
                              pszRefRegspaceName,
                              pszRefSymbolicName,
                              uiRefOffset);
    if (eError != PVRSRV_OK)
    {
        goto ErrUnlock;
    }
 
    PDumpWriteScript(hScript, uiPDumpFlags);
 
    if (uiSHRAmount > 0)
    {
        eError = PDumpOSBufprintf(hScript,
                                  ui32MaxLen,
                                  "SHR :%s:$%d :%s:$%d 0x%X\n",
                                  /* dest */
                                  pszPDumpIntRegSpace,
                                  uiPDumpIntRegNum,
                                  /* src A */
                                  pszPDumpIntRegSpace,
                                  uiPDumpIntRegNum,
                                  /* src B */
                                  uiSHRAmount);
        if (eError != PVRSRV_OK)
        {
            goto ErrUnlock;
        }
        PDumpWriteScript(hScript, uiPDumpFlags);
    }
    
    if (uiSHLAmount > 0)
    {
        eError = PDumpOSBufprintf(hScript,
                                  ui32MaxLen,
                                  "SHL :%s:$%d :%s:$%d 0x%X\n",
                                  /* dest */
                                  pszPDumpIntRegSpace,
                                  uiPDumpIntRegNum,
                                  /* src A */
                                  pszPDumpIntRegSpace,
                                  uiPDumpIntRegNum,
                                  /* src B */
                                  uiSHLAmount);
        if (eError != PVRSRV_OK)
        {
            goto ErrUnlock;
        }
        PDumpWriteScript(hScript, uiPDumpFlags);
    }
    
    if (uiMask != (1ULL << (8*uiWordSize))-1)
    {
        eError = PDumpOSBufprintf(hScript,
                                  ui32MaxLen,
                                  "AND :%s:$%d :%s:$%d 0x%X\n",
                                  /* dest */
                                  pszPDumpIntRegSpace,
                                  uiPDumpIntRegNum,
                                  /* src A */
                                  pszPDumpIntRegSpace,
                                  uiPDumpIntRegNum,
                                  /* src B */
                                  uiMask);
        if (eError != PVRSRV_OK)
        {
            goto ErrUnlock;
        }
        PDumpWriteScript(hScript, uiPDumpFlags);
    }
 
    eError = PDumpOSBufprintf(hScript,
                              ui32MaxLen,
                              "WRW%s :%s:%s:" IMG_DEVMEM_OFFSET_FMTSPEC " :%s:$%d\n",
                              pszWrwSuffix,
                              /* dest */
                              pszDestRegspaceName,
                              pszDestSymbolicName,
                              uiDestOffset,
                              /* src */
                              pszPDumpIntRegSpace,
                              uiPDumpIntRegNum);
    if(eError != PVRSRV_OK)
    {
        goto ErrUnlock;
    }
    PDumpWriteScript(hScript, uiPDumpFlags);
 
ErrUnlock:
   PDUMP_UNLOCK();
   return eError;
}
 
 
PVRSRV_ERROR
PDumpWriteSymbAddress(const IMG_CHAR *pszDestSpaceName,
                      IMG_DEVMEM_OFFSET_T uiDestOffset,
                      const IMG_CHAR *pszRefSymbolicName,
                      IMG_DEVMEM_OFFSET_T uiRefOffset,
                      const IMG_CHAR *pszPDumpDevName,
                      IMG_UINT32 ui32WordSize,
                      IMG_UINT32 ui32AlignShift,
                      IMG_UINT32 ui32Shift,
                      IMG_UINT32 uiPDumpFlags)
{
    const IMG_CHAR       *pszWrwSuffix = "";
   PVRSRV_ERROR         eError = PVRSRV_OK;
 
   PDUMP_GET_SCRIPT_STRING();
 
    if (ui32WordSize == 8)
    {
        pszWrwSuffix = "64";
    }
 
    PDUMP_LOCK();
 
    if (ui32AlignShift != ui32Shift)
    {
        /* Write physical address into a variable */
        eError = PDumpOSBufprintf(hScript,
                                ui32MaxLen,
                                "WRW%s :%s:$1 %s:" IMG_DEVMEM_OFFSET_FMTSPEC "\n",
                                pszWrwSuffix,
                                /* dest */
                                pszPDumpDevName,
                                /* src */
                                pszRefSymbolicName,
                                uiRefOffset);
       if (eError != PVRSRV_OK)
       {
           goto symbAddress_error;
       }
        PDumpWriteScript(hScript, uiPDumpFlags);
 
        /* apply address alignment  */
        eError = PDumpOSBufprintf(hScript,
                                ui32MaxLen,
                                "SHR :%s:$1 :%s:$1 0x%X",
                                /* dest */
                                pszPDumpDevName,
                                /* src A */
                                pszPDumpDevName,
                                /* src B */
                                ui32AlignShift);
       if (eError != PVRSRV_OK)
       {
           goto symbAddress_error;
       }
        PDumpWriteScript(hScript, uiPDumpFlags);
 
        /* apply address shift  */
        eError = PDumpOSBufprintf(hScript,
                                ui32MaxLen,
                                "SHL :%s:$1 :%s:$1 0x%X",
                                /* dest */
                                pszPDumpDevName,
                                /* src A */
                                pszPDumpDevName,
                                /* src B */
                                ui32Shift);
       if (eError != PVRSRV_OK)
       {
           goto symbAddress_error;
       }
        PDumpWriteScript(hScript, uiPDumpFlags);
 
 
        /* write result to register */
        eError = PDumpOSBufprintf(hScript,
                                ui32MaxLen,
                                "WRW%s :%s:0x%08X :%s:$1",
                                pszWrwSuffix,
                                pszDestSpaceName,
                                (IMG_UINT32)uiDestOffset,
                                pszPDumpDevName);
       if (eError != PVRSRV_OK)
       {
           goto symbAddress_error;
       }
        PDumpWriteScript(hScript, uiPDumpFlags);
    }
    else
    {
       eError = PDumpOSBufprintf(hScript,
                                 ui32MaxLen,
                                 "WRW%s :%s:" IMG_DEVMEM_OFFSET_FMTSPEC " %s:" IMG_DEVMEM_OFFSET_FMTSPEC "\n",
                                 pszWrwSuffix,
                                 /* dest */
                                 pszDestSpaceName,
                                 uiDestOffset,
                                 /* src */
                                 pszRefSymbolicName,
                                 uiRefOffset);
       if (eError != PVRSRV_OK)
       {
           goto symbAddress_error;
       }
       PDumpWriteScript(hScript, uiPDumpFlags);
    }
 
symbAddress_error:
 
    PDUMP_UNLOCK();
 
   return eError;
}
 
/**************************************************************************
 * Function Name  : PDumpIDLWithFlags
 * Inputs         : Idle time in clocks
 * Outputs        : None
 * Returns        : Error
 * Description    : Dump IDL command to script
**************************************************************************/
PVRSRV_ERROR PDumpIDLWithFlags(IMG_UINT32 ui32Clocks, IMG_UINT32 ui32Flags)
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING();
   PDUMP_DBG(("PDumpIDLWithFlags"));
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "IDL %u", ui32Clocks);
   if(eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
   PDumpWriteScript(hScript, ui32Flags);
   PDUMP_UNLOCK();
   return PVRSRV_OK;
}
 
 
/**************************************************************************
 * Function Name  : PDumpIDL
 * Inputs         : Idle time in clocks
 * Outputs        : None
 * Returns        : Error
 * Description    : Dump IDL command to script
**************************************************************************/
PVRSRV_ERROR PDumpIDL(IMG_UINT32 ui32Clocks)
{
   return PDumpIDLWithFlags(ui32Clocks, PDUMP_FLAGS_CONTINUOUS);
}
 
/*****************************************************************************
 FUNCTION    : PDumpRegBasedCBP
    
 PURPOSE    : Dump CBP command to script
 
 PARAMETERS    :
             
 RETURNS    : None
*****************************************************************************/
PVRSRV_ERROR PDumpRegBasedCBP(IMG_CHAR        *pszPDumpRegName,
                             IMG_UINT32    ui32RegOffset,
                             IMG_UINT32    ui32WPosVal,
                             IMG_UINT32    ui32PacketSize,
                             IMG_UINT32    ui32BufferSize,
                             IMG_UINT32    ui32Flags)
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING();
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript,
            ui32MaxLen,
            "CBP :%s:0x%08X 0x%08X 0x%08X 0x%08X",
            pszPDumpRegName,
            ui32RegOffset,
            ui32WPosVal,
            ui32PacketSize,
            ui32BufferSize);
   if(eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
   PDumpWriteScript(hScript, ui32Flags);
   PDUMP_UNLOCK();
   
   return PVRSRV_OK;        
}
 
PVRSRV_ERROR PDumpTRG(IMG_CHAR *pszMemSpace,
                      IMG_UINT32 ui32MMUCtxID,
                      IMG_UINT32 ui32RegionID,
                      IMG_BOOL bEnable,
                      IMG_UINT64 ui64VAddr,
                      IMG_UINT64 ui64LenBytes,
                      IMG_UINT32 ui32XStride,
                      IMG_UINT32 ui32Flags)
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING();
 
   PDUMP_LOCK();
   if(bEnable)
   {
       eErr = PDumpOSBufprintf(hScript, ui32MaxLen,
                        "TRG :%s:v%u %u 0x%08llX 0x%08llX %u",
                        pszMemSpace, ui32MMUCtxID, ui32RegionID,
                        ui64VAddr, ui64LenBytes, ui32XStride);
   }
   else
   {
       eErr = PDumpOSBufprintf(hScript, ui32MaxLen,
                        "TRG :%s:v%u %u",
                        pszMemSpace, ui32MMUCtxID, ui32RegionID);
 
   }
   if(eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
   PDumpWriteScript(hScript, ui32Flags);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
/**************************************************************************
 * Function Name  : PDumpConnectionNotify
 * Description    : Called by the srvcore to tell PDump core that the
 *                  PDump capture and control client has connected
 **************************************************************************/
void PDumpConnectionNotify(void)
{
   PVRSRV_DATA            *psPVRSRVData = PVRSRVGetPVRSRVData();
   PVRSRV_DEVICE_NODE    *psThis;
 
   /* Give PDump control a chance to end the init phase, depends on OS */
   if (!PDumpCtrlInitPhaseComplete())
   {
       PDumpStopInitPhase(IMG_TRUE, IMG_FALSE);
   }
 
   g_ConnectionCount++;
   PVR_LOG(("PDump has connected (%u)", g_ConnectionCount));
   
   /* Reset the parameter file attributes */
   g_PDumpParameters.sWOff.ui32Main = g_PDumpParameters.sWOff.ui32Init;
   g_PDumpParameters.ui32FileIdx = 0;
 
   /* Loop over all known devices */
   psThis = psPVRSRVData->psDeviceNodeList;
   while (psThis)
   {
       if (psThis->pfnPDumpInitDevice)
       {
           /* Reset pdump according to connected device */
           psThis->pfnPDumpInitDevice(psThis);
       }
       psThis = psThis->psNext;
   }
}
 
/**************************************************************************
 * Function Name  : PDumpDisconnectionNotify
 * Description    : Called by the connection_server to tell PDump core that
 *                  the PDump capture and control client has disconnected
 **************************************************************************/
void PDumpDisconnectionNotify(void)
{
   PVRSRV_ERROR eErr;
 
   if (PDumpCtrlCaptureOn())
   {
       PVR_LOG(("PDump killed, output files may be invalid or incomplete!"));
 
       /* Disable capture in server, in case PDump client was killed and did
        * not get a chance to reset the capture parameters.
        */
       eErr = PDumpSetDefaultCaptureParamsKM( DEBUG_CAPMODE_FRAMED,
                                              FRAME_UNSET, FRAME_UNSET, 1, 0);
       PVR_LOG_IF_ERROR(eErr, "PVRSRVPDumpSetDefaultCaptureParams");
   }
   else
   {
       PVR_LOG(("PDump disconnected"));
   }
}
 
/**************************************************************************
 * Function Name  : PDumpIfKM
 * Inputs         : pszPDumpCond - string for condition
 * Outputs        : None
 * Returns        : None
 * Description    : Create a PDUMP string which represents IF command 
                   with condition.
**************************************************************************/
PVRSRV_ERROR PDumpIfKM(IMG_CHAR        *pszPDumpCond)
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING()
   PDUMP_DBG(("PDumpIfKM"));
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "IF %s\n", pszPDumpCond);
 
   if (eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
   PDumpWriteScript(hScript, PDUMP_FLAGS_CONTINUOUS);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
/**************************************************************************
 * Function Name  : PDumpElseKM
 * Inputs         : pszPDumpCond - string for condition
 * Outputs        : None
 * Returns        : None
 * Description    : Create a PDUMP string which represents ELSE command 
                   with condition.
**************************************************************************/
PVRSRV_ERROR PDumpElseKM(IMG_CHAR        *pszPDumpCond)
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING()
   PDUMP_DBG(("PDumpElseKM"));
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "ELSE %s\n", pszPDumpCond);
 
   if (eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
   PDumpWriteScript(hScript, PDUMP_FLAGS_CONTINUOUS);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
/**************************************************************************
 * Function Name  : PDumpFiKM
 * Inputs         : pszPDumpCond - string for condition
 * Outputs        : None
 * Returns        : None
 * Description    : Create a PDUMP string which represents FI command 
                   with condition.
**************************************************************************/
PVRSRV_ERROR PDumpFiKM(IMG_CHAR        *pszPDumpCond)
{
   PVRSRV_ERROR eErr;
   PDUMP_GET_SCRIPT_STRING()
   PDUMP_DBG(("PDumpFiKM"));
 
   PDUMP_LOCK();
   eErr = PDumpOSBufprintf(hScript, ui32MaxLen, "FI %s\n", pszPDumpCond);
 
   if (eErr != PVRSRV_OK)
   {
       PDUMP_UNLOCK();
       return eErr;
   }
 
   PDumpWriteScript(hScript, PDUMP_FLAGS_CONTINUOUS);
   PDUMP_UNLOCK();
 
   return PVRSRV_OK;
}
 
PVRSRV_ERROR PDumpCreateLockKM(void)
{
   return PDumpOSCreateLock();
}
 
void PDumpDestroyLockKM(void)
{
   PDumpOSDestroyLock();
}
 
void PDumpLock(void)
{
   PDumpOSLock();
}
 
void PDumpUnlock(void)
{
   PDumpOSUnlock();
}
 
#if defined(PVR_TESTING_UTILS)
extern void PDumpOSDumpState(void);
 
#if !defined(LINUX)
void PDumpOSDumpState(IMG_BOOL bDumpOSLayerState)
{
   PVR_UNREFERENCED_PARAMETER(bDumpOSLayerState);
}
#endif
 
void PDumpCommonDumpState(IMG_BOOL bDumpOSLayerState)
{
   PVR_LOG(("--- PDUMP COMMON: g_PDumpInitialised( %d )",
           g_PDumpInitialised) );
   PVR_LOG(("--- PDUMP COMMON: g_PDumpScript.sCh.hInit( %p ) g_PDumpScript.sCh.hMain( %p ) g_PDumpScript.sCh.hDeinit( %p )",
           g_PDumpScript.sCh.hInit, g_PDumpScript.sCh.hMain, g_PDumpScript.sCh.hDeinit) );
   PVR_LOG(("--- PDUMP COMMON: g_PDumpParameters.sCh.hInit( %p ) g_PDumpParameters.sCh.hMain( %p ) g_PDumpParameters.sCh.hDeinit( %p )",
           g_PDumpParameters.sCh.hInit, g_PDumpParameters.sCh.hMain, g_PDumpParameters.sCh.hDeinit) );
   PVR_LOG(("--- PDUMP COMMON: g_PDumpParameters.sWOff.ui32Init( %d ) g_PDumpParameters.sWOff.ui32Main( %d ) g_PDumpParameters.sWOff.ui32Deinit( %d )",
           g_PDumpParameters.sWOff.ui32Init, g_PDumpParameters.sWOff.ui32Main, g_PDumpParameters.sWOff.ui32Deinit) );
   PVR_LOG(("--- PDUMP COMMON: g_PDumpParameters.ui32FileIdx( %d )",
           g_PDumpParameters.ui32FileIdx) );
 
   PVR_LOG(("--- PDUMP COMMON: g_PDumpCtrl( %p ) bInitPhaseActive( %d ) ui32Flags( %x )",
           &g_PDumpCtrl, g_PDumpCtrl.bInitPhaseActive, g_PDumpCtrl.ui32Flags) );
   PVR_LOG(("--- PDUMP COMMON: ui32DefaultCapMode( %d ) ui32CurrentFrame( %d )",
           g_PDumpCtrl.ui32DefaultCapMode, g_PDumpCtrl.ui32CurrentFrame) );
   PVR_LOG(("--- PDUMP COMMON: sCaptureRange.ui32Start( %x ) sCaptureRange.ui32End( %x ) sCaptureRange.ui32Interval( %u )",
           g_PDumpCtrl.sCaptureRange.ui32Start, g_PDumpCtrl.sCaptureRange.ui32End, g_PDumpCtrl.sCaptureRange.ui32Interval) );
   PVR_LOG(("--- PDUMP COMMON: bCaptureOn( %d ) bSuspended( %d ) bInPowerTransition( %d )",
           g_PDumpCtrl.bCaptureOn, g_PDumpCtrl.bSuspended, g_PDumpCtrl.bInPowerTransition) );
 
   if (bDumpOSLayerState)
   {
       PDumpOSDumpState();
   }
}
#endif
 
 
PVRSRV_ERROR PDumpRegisterConnection(SYNC_CONNECTION_DATA *psSyncConnectionData,
                                    PDUMP_CONNECTION_DATA **ppsPDumpConnectionData)
{
   PDUMP_CONNECTION_DATA *psPDumpConnectionData;
   PVRSRV_ERROR eError;
 
   PVR_ASSERT(ppsPDumpConnectionData != NULL);
 
   psPDumpConnectionData = OSAllocMem(sizeof(*psPDumpConnectionData));
   if (psPDumpConnectionData == NULL)
   {
       eError = PVRSRV_ERROR_OUT_OF_MEMORY;
       goto fail_alloc;
   }
 
   eError = OSLockCreate(&psPDumpConnectionData->hLock, LOCK_TYPE_PASSIVE);
   if (eError != PVRSRV_OK)
   {
       goto fail_lockcreate;
   }
 
   dllist_init(&psPDumpConnectionData->sListHead);
   psPDumpConnectionData->ui32RefCount = 1;
   psPDumpConnectionData->bLastInto = IMG_FALSE;
   psPDumpConnectionData->ui32LastSetFrameNumber = FRAME_UNSET;
   psPDumpConnectionData->bLastTransitionFailed = IMG_FALSE;
 
   /*
    * Although we don't take a ref count here, handle base destruction
    * will ensure that any resource that might trigger us to do a
    * Transition will have been freed before the sync blocks which
    * are keeping the sync connection data alive.
    */
   psPDumpConnectionData->psSyncConnectionData = psSyncConnectionData;
   *ppsPDumpConnectionData = psPDumpConnectionData;
 
   return PVRSRV_OK;
 
fail_lockcreate:
   OSFreeMem(psPDumpConnectionData);
fail_alloc:
   PVR_ASSERT(eError != PVRSRV_OK);
   return eError;
}
 
void PDumpUnregisterConnection(PDUMP_CONNECTION_DATA *psPDumpConnectionData)
{
   _PDumpConnectionRelease(psPDumpConnectionData);
}
 
 
 
#else    /* defined(PDUMP) */
/* disable warning about empty module */
#ifdef    _WIN32
#pragma warning (disable:4206)
#endif
#endif    /* defined(PDUMP) */
/*****************************************************************************
 End of file (pdump_common.c)
*****************************************************************************/