hc
2023-05-26 a23f51ed7a39e452c1037343a84d7db1ca2c5bd7
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
/*************************************************************************/ /*!
@File
@Title          Functions for creating debugfs directories and entries.
@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.
*/ /**************************************************************************/
 
#include <linux/module.h>
#include <linux/slab.h>
 
#include "pvr_debug.h"
#include "pvr_debugfs.h"
#include "allocmem.h"
 
#define PVR_DEBUGFS_DIR_NAME PVR_DRM_NAME
 
/* Define to set the PVR_DPF debug output level for pvr_debugfs.
 * Normally, leave this set to PVR_DBGDRIV_MESSAGE, but when debugging
 * you can temporarily change this to PVR_DBG_ERROR.
 */
#if defined(PVRSRV_NEED_PVR_DPF)
#define PVR_DEBUGFS_PVR_DPF_LEVEL      PVR_DBGDRIV_MESSAGE
#else
#define PVR_DEBUGFS_PVR_DPF_LEVEL      0
#endif
 
static struct dentry *gpsPVRDebugFSEntryDir = NULL;
 
/* Lock used when adjusting refCounts and deleting entries */
static struct mutex gDebugFSLock;
 
/*************************************************************************/ /*!
 Statistic entry read functions
*/ /**************************************************************************/
 
typedef struct _PVR_DEBUGFS_PRIV_DATA_
{
   const struct seq_operations *psReadOps;
   PVRSRV_ENTRY_WRITE_FUNC    *pfnWrite;
   void            *pvData;
   PVRSRV_INC_FSENTRY_PVDATA_REFCNT_FN *pfIncPvDataRefCnt;
   PVRSRV_DEC_FSENTRY_PVDATA_REFCNT_FN *pfDecPvDataRefCnt;
   IMG_BOOL        bValid;
   PVR_DEBUGFS_ENTRY_DATA *psDebugFSEntry;
} PVR_DEBUGFS_PRIV_DATA;
 
static IMG_BOOL _RefDirEntry(PVR_DEBUGFS_DIR_DATA *psDirEntry);
static inline void _UnrefAndMaybeDestroyDirEntry(PVR_DEBUGFS_DIR_DATA **ppsDirEntry);
static void _UnrefAndMaybeDestroyDirEntryWhileLocked(PVR_DEBUGFS_DIR_DATA **ppsDirEntry);
static IMG_BOOL _RefDebugFSEntryNoLock(PVR_DEBUGFS_ENTRY_DATA *psDebugFSEntry);
static void _UnrefAndMaybeDestroyDebugFSEntry(PVR_DEBUGFS_ENTRY_DATA **ppsDebugFSEntry);
static IMG_BOOL _RefStatEntry(PVR_DEBUGFS_DRIVER_STAT *psStatEntry);
static IMG_BOOL _UnrefAndMaybeDestroyStatEntry(PVR_DEBUGFS_DRIVER_STAT *psStatEntry);
 
static void _StatsSeqPrintf(void *pvFile, const IMG_CHAR *pszFormat, ...)
{
   IMG_CHAR  szBuffer[PVR_MAX_DEBUG_MESSAGE_LEN];
   va_list  ArgList;
 
   va_start(ArgList, pszFormat);
   vsnprintf(szBuffer, PVR_MAX_DEBUG_MESSAGE_LEN, pszFormat, ArgList);
   seq_printf((struct seq_file *)pvFile, "%s", szBuffer);
   va_end(ArgList);
}
 
static void *_DebugFSStatisticSeqStart(struct seq_file *psSeqFile, loff_t *puiPosition)
{
   PVR_DEBUGFS_DRIVER_STAT *psStatData = (PVR_DEBUGFS_DRIVER_STAT *)psSeqFile->private;
 
   if (psStatData)
   {
       /* take reference on psStatData (for duration of stat iteration) */
       if (!_RefStatEntry(psStatData))
       {
           PVR_DPF((PVR_DEBUGFS_PVR_DPF_LEVEL, "%s: Called for '%s' but failed"
                   " to take ref on stat entry, returning -EIO(%d)", __func__,
                   psStatData->pvDebugFSEntry->psEntry->d_iname, -EIO));
           return NULL;
       }
 
       if (*puiPosition == 0)
       {
           return psStatData;
       }
   }
   else
   {
       PVR_DPF((PVR_DEBUGFS_PVR_DPF_LEVEL, "%s: Called when psStatData is NULL", __FUNCTION__));
   }
 
   return NULL;
}
 
static void _DebugFSStatisticSeqStop(struct seq_file *psSeqFile, void *pvData)
{
   PVR_DEBUGFS_DRIVER_STAT *psStatData = (PVR_DEBUGFS_DRIVER_STAT *)psSeqFile->private;
   PVR_UNREFERENCED_PARAMETER(pvData);
 
   if (psStatData)
   {
       /* drop ref taken on stat memory, and if it is now zero, be sure we don't try to read it again */
       if (psStatData->ui32RefCount > 0)
       {
           /* drop reference on psStatData (held for duration of stat iteration) */
           _UnrefAndMaybeDestroyStatEntry((void*)psStatData);
       }
   }
   else
   {
       PVR_DPF((PVR_DEBUGFS_PVR_DPF_LEVEL, "%s: Called when psStatData is NULL", __FUNCTION__));
   }
}
 
static void *_DebugFSStatisticSeqNext(struct seq_file *psSeqFile,
                     void *pvData,
                     loff_t *puiPosition)
{
   PVR_DEBUGFS_DRIVER_STAT *psStatData = (PVR_DEBUGFS_DRIVER_STAT *)psSeqFile->private;
   PVR_UNREFERENCED_PARAMETER(pvData);
 
   if (psStatData)
   {
       if (psStatData->pvData)
       {
           if (puiPosition)
           {
               (*puiPosition)++;
           }
           else
           {
               PVR_DPF((PVR_DEBUGFS_PVR_DPF_LEVEL, "%s: Called with puiPosition NULL", __FUNCTION__));
           }
       }
       else
       {
           /* psStatData->pvData is NULL */
           /* NB This is valid if the stat has no structure associated with it (eg. driver_stats, which prints totals stored in a number of global vars) */
       }
   }
   else
   {
       PVR_DPF((PVR_DEBUGFS_PVR_DPF_LEVEL, "%s: Called when psStatData is NULL", __FUNCTION__));
   }
 
   return NULL;
}
 
static int _DebugFSStatisticSeqShow(struct seq_file *psSeqFile, void *pvData)
{
   PVR_DEBUGFS_DRIVER_STAT *psStatData = (PVR_DEBUGFS_DRIVER_STAT *)pvData;
 
   if (psStatData != NULL)
   {
       psStatData->pfnStatsPrint((void*)psSeqFile, psStatData->pvData, _StatsSeqPrintf);
       return 0;
   }
   else
   {
       PVR_DPF((PVR_DEBUGFS_PVR_DPF_LEVEL, "%s: Called when psStatData is NULL, returning -ENODATA(%d)", __FUNCTION__, -ENODATA));
   }
 
   return -ENODATA;
}
 
static struct seq_operations gsDebugFSStatisticReadOps =
{
   .start = _DebugFSStatisticSeqStart,
   .stop  = _DebugFSStatisticSeqStop,
   .next  = _DebugFSStatisticSeqNext,
   .show  = _DebugFSStatisticSeqShow,
};
 
 
/*************************************************************************/ /*!
 Common internal API
*/ /**************************************************************************/
 
static int _DebugFSFileOpen(struct inode *psINode, struct file *psFile)
{
   PVR_DEBUGFS_PRIV_DATA *psPrivData;
   int iResult = -EIO;
   IMG_BOOL bRefRet = IMG_FALSE;
   PVR_DEBUGFS_ENTRY_DATA *psDebugFSEntry = NULL;
 
   mutex_lock(&gDebugFSLock);
 
   PVR_ASSERT(psINode);
   psPrivData = (PVR_DEBUGFS_PRIV_DATA *)psINode->i_private;
 
   if (psPrivData)
   {
       /* Check that psPrivData is still valid to use */
       if (psPrivData->bValid)
       {
           psDebugFSEntry = psPrivData->psDebugFSEntry;
 
           /* Take ref on stat entry before opening seq file - this ref will be dropped if we
            * fail to open the seq file or when we close it
            */
           if (psDebugFSEntry)
           {
               bRefRet = _RefDebugFSEntryNoLock(psDebugFSEntry);
               mutex_unlock(&gDebugFSLock);
               if (psPrivData->pfIncPvDataRefCnt)
               {
                   psPrivData->pfIncPvDataRefCnt(psPrivData->pvData);
               }
               if (bRefRet)
               {
                   iResult = seq_open(psFile, psPrivData->psReadOps);
                   if (iResult == 0)
                   {
                       struct seq_file *psSeqFile = psFile->private_data;
 
                       psSeqFile->private = psPrivData->pvData;
                   }
                   else
                   {
                       if (psPrivData->pfDecPvDataRefCnt)
                       {
                           psPrivData->pfDecPvDataRefCnt(psPrivData->pvData);
                       }
                       /* Drop ref if we failed to open seq file */
                       _UnrefAndMaybeDestroyDebugFSEntry(&psPrivData->psDebugFSEntry);
                       PVR_DPF((PVR_DBG_ERROR, "%s: Failed to seq_open psFile, returning %d", __FUNCTION__, iResult));
                   }
               }
           }
           else
           {
               mutex_unlock(&gDebugFSLock);
           }
       }
       else
       {
           mutex_unlock(&gDebugFSLock);
       }
   }
   else
   {
       mutex_unlock(&gDebugFSLock);
   }
 
   return iResult;
}
 
static int _DebugFSFileClose(struct inode *psINode, struct file *psFile)
{
   int iResult;
   PVR_DEBUGFS_PRIV_DATA *psPrivData = (PVR_DEBUGFS_PRIV_DATA *)psINode->i_private;
   PVR_DEBUGFS_ENTRY_DATA *psDebugFSEntry = NULL;
 
   if (psPrivData)
   {
       psDebugFSEntry = psPrivData->psDebugFSEntry;
   }
   iResult = seq_release(psINode, psFile);
   if (psDebugFSEntry)
   {
       _UnrefAndMaybeDestroyDebugFSEntry(&psPrivData->psDebugFSEntry);
   }
   if (psPrivData && psPrivData->pfDecPvDataRefCnt)
   {
       psPrivData->pfDecPvDataRefCnt(psPrivData->pvData);
   }
   return iResult;
}
 
static ssize_t _DebugFSFileWrite(struct file *psFile,
                const char __user *pszBuffer,
                size_t uiCount,
                loff_t *puiPosition)
{
   struct inode *psINode = psFile->f_path.dentry->d_inode;
   PVR_DEBUGFS_PRIV_DATA *psPrivData = (PVR_DEBUGFS_PRIV_DATA *)psINode->i_private;
 
   if (psPrivData->pfnWrite == NULL)
   {
       PVR_DPF((PVR_DEBUGFS_PVR_DPF_LEVEL, "%s: Called for file '%s', which does not have pfnWrite defined, returning -EIO(%d)", __FUNCTION__, psFile->f_path.dentry->d_iname, -EIO));
       return -EIO;
   }
 
   return psPrivData->pfnWrite(pszBuffer, uiCount, *puiPosition, psPrivData->pvData);
}
 
static const struct file_operations gsPVRDebugFSFileOps =
{
   .owner = THIS_MODULE,
   .open = _DebugFSFileOpen,
   .read = seq_read,
   .write = _DebugFSFileWrite,
   .llseek = seq_lseek,
   .release = _DebugFSFileClose,
};
 
 
/*************************************************************************/ /*!
 Public API
*/ /**************************************************************************/
 
/*************************************************************************/ /*!
@Function       PVRDebugFSInit
@Description    Initialise PVR debugfs support. This should be called before
                using any PVRDebugFS functions.
@Return         int      On success, returns 0. Otherwise, returns an
                         error code.
*/ /**************************************************************************/
int PVRDebugFSInit(void)
{
   PVR_ASSERT(gpsPVRDebugFSEntryDir == NULL);
 
   mutex_init(&gDebugFSLock);
 
   gpsPVRDebugFSEntryDir = debugfs_create_dir(PVR_DEBUGFS_DIR_NAME, NULL);
   if (gpsPVRDebugFSEntryDir == NULL)
   {
       PVR_DPF((PVR_DBG_ERROR,
            "%s: Cannot create '%s' debugfs root directory",
            __FUNCTION__, PVR_DEBUGFS_DIR_NAME));
 
       return -ENOMEM;
   }
 
   return 0;
}
 
/*************************************************************************/ /*!
@Function       PVRDebugFSDeInit
@Description    Deinitialise PVR debugfs support. This should be called only
                if PVRDebugFSInit() has already been called. All debugfs
                directories and entries should be removed otherwise this
                function will fail.
@Return         void
*/ /**************************************************************************/
void PVRDebugFSDeInit(void)
{
   if (gpsPVRDebugFSEntryDir != NULL)
   {
       debugfs_remove(gpsPVRDebugFSEntryDir);
       gpsPVRDebugFSEntryDir = NULL;
       mutex_destroy(&gDebugFSLock);
   }
}
 
/*************************************************************************/ /*!
@Function        PVRDebugFSCreateEntryDir
@Description    Create a directory for debugfs entries that will be located
               under the root directory, as created by
               PVRDebugFSCreateEntries().
@Input            pszName         String containing the name for the directory.
@Input            psParentDir     The parent directory in which to create the new
                            directory. This should either be NULL, meaning it
                            should be created in the root directory, or a
                            pointer to a directory as returned by this
                            function.
@Output            ppsNewDir     On success, points to the newly created
                            directory.
@Return            int             On success, returns 0. Otherwise, returns an
                            error code.
*/ /**************************************************************************/
int PVRDebugFSCreateEntryDir(IMG_CHAR *pszName,
                PVR_DEBUGFS_DIR_DATA *psParentDir,
                PVR_DEBUGFS_DIR_DATA **ppsNewDir)
{
   PVR_DEBUGFS_DIR_DATA *psNewDir;
 
   PVR_ASSERT(gpsPVRDebugFSEntryDir != NULL);
 
   if (pszName == NULL || ppsNewDir == NULL)
   {
       PVR_DPF((PVR_DBG_ERROR, "%s:   Invalid param", __FUNCTION__));
       return -EINVAL;
   }
 
   psNewDir = OSAllocMemNoStats(sizeof(*psNewDir));
 
   if (psNewDir == NULL)
   {
       PVR_DPF((PVR_DBG_ERROR,
            "%s: Cannot allocate memory for '%s' pvr_debugfs structure",
            __FUNCTION__, pszName));
       return -ENOMEM;
   }
 
   psNewDir->psParentDir = psParentDir;
   psNewDir->psDir = debugfs_create_dir(pszName, (psNewDir->psParentDir) ? psNewDir->psParentDir->psDir : gpsPVRDebugFSEntryDir);
 
   if (psNewDir->psDir == NULL)
   {
       PVR_DPF((PVR_DBG_ERROR,
            "%s: Cannot create '%s' debugfs directory",
            __FUNCTION__, pszName));
 
       OSFreeMemNoStats(psNewDir);
       return -ENOMEM;
   }
 
   *ppsNewDir = psNewDir;
   psNewDir->ui32RefCount = 1;
 
   /* if parent directory is not gpsPVRDebugFSEntryDir, increment its refCount */
   if (psNewDir->psParentDir)
   {
       /* if we fail to acquire the reference that probably means that
        * parent dir was already freed - we have to cleanup in this situation */
       if (!_RefDirEntry(psNewDir->psParentDir))
       {
           _UnrefAndMaybeDestroyDirEntry(ppsNewDir);
           return -EFAULT;
       }
   }
 
   return 0;
}
 
/*************************************************************************/ /*!
@Function        PVRDebugFSRemoveEntryDir
@Description    Remove a directory that was created by
               PVRDebugFSCreateEntryDir(). Any directories or files created
               under the directory being removed should be removed first.
@Input          ppsDir       Pointer representing the directory to be removed.
                             Has to be double pointer to avoid possible races
                             and use-after-free situations.
@Return            void
*/ /**************************************************************************/
void PVRDebugFSRemoveEntryDir(PVR_DEBUGFS_DIR_DATA **ppsDir)
{
   _UnrefAndMaybeDestroyDirEntry(ppsDir);
}
 
/*************************************************************************/ /*!
@Function        PVRDebugFSCreateEntry
@Description    Create an entry in the specified directory.
@Input            pszName            String containing the name for the entry.
@Input            psParentDir        Pointer from PVRDebugFSCreateEntryDir()
                               representing the directory in which to create
                               the entry or NULL for the root directory.
@Input            psReadOps        Pointer to structure containing the necessary
                               functions to read from the entry.
@Input            pfnWrite        Callback function used to write to the entry.
@Input            pvData            Private data to be passed to the read
                               functions, in the seq_file private member, and
                               the write function callback.
@Output            ppsNewEntry        On success, points to the newly created entry.
@Return            int                On success, returns 0. Otherwise, returns an
                               error code.
*/ /**************************************************************************/
int PVRDebugFSCreateEntry(const char *pszName,
             PVR_DEBUGFS_DIR_DATA *psParentDir,
             const struct seq_operations *psReadOps,
             PVRSRV_ENTRY_WRITE_FUNC *pfnWrite,
             PVRSRV_INC_FSENTRY_PVDATA_REFCNT_FN *pfnIncPvDataRefCnt,
             PVRSRV_DEC_FSENTRY_PVDATA_REFCNT_FN *pfnDecPvDataRefCnt,
             void *pvData,
             PVR_DEBUGFS_ENTRY_DATA **ppsNewEntry)
{
   PVR_DEBUGFS_PRIV_DATA *psPrivData;
   PVR_DEBUGFS_ENTRY_DATA *psDebugFSEntry;
   struct dentry *psEntry;
   umode_t uiMode;
 
   PVR_ASSERT(gpsPVRDebugFSEntryDir != NULL);
   PVR_ASSERT(!((pfnIncPvDataRefCnt != NULL && pfnDecPvDataRefCnt == NULL) ||
              (pfnIncPvDataRefCnt == NULL && pfnDecPvDataRefCnt != NULL)));
 
   psPrivData = OSAllocMemNoStats(sizeof(*psPrivData));
   if (psPrivData == NULL)
   {
       return -ENOMEM;
   }
   psDebugFSEntry = OSAllocMemNoStats(sizeof(*psDebugFSEntry));
   if (psDebugFSEntry == NULL)
   {
       OSFreeMemNoStats(psPrivData);
       return -ENOMEM;
   }
 
   psPrivData->psReadOps = psReadOps;
   psPrivData->pfnWrite = pfnWrite;
   psPrivData->pvData = (void*)pvData;
   psPrivData->pfIncPvDataRefCnt = pfnIncPvDataRefCnt;
   psPrivData->pfDecPvDataRefCnt = pfnDecPvDataRefCnt;
   psPrivData->bValid = IMG_TRUE;
   /* Store ptr to debugFSEntry in psPrivData, so a ref can be taken on it
    * when the client opens a file */
   psPrivData->psDebugFSEntry = psDebugFSEntry;
 
   uiMode = S_IFREG;
 
   if (psReadOps != NULL)
   {
       uiMode |= S_IRUGO;
   }
 
   if (pfnWrite != NULL)
   {
       uiMode |= S_IWUSR;
   }
 
   psDebugFSEntry->psParentDir = psParentDir;
   psDebugFSEntry->ui32RefCount = 1;
   psDebugFSEntry->psStatData = (PVR_DEBUGFS_DRIVER_STAT*)pvData;
 
   if (psDebugFSEntry->psParentDir)
   {
       /* increment refCount of parent directory */
       if (!_RefDirEntry(psDebugFSEntry->psParentDir))
       {
           kfree(psDebugFSEntry);
           kfree(psPrivData);
           return -EFAULT;
       }
   }
 
   psEntry = debugfs_create_file(pszName,
                     uiMode,
                     (psParentDir != NULL) ? psParentDir->psDir : gpsPVRDebugFSEntryDir,
                     psPrivData,
                     &gsPVRDebugFSFileOps);
   if (IS_ERR(psEntry))
   {
       PVR_DPF((PVR_DBG_ERROR,
            "%s: Cannot create debugfs '%s' file",
            __FUNCTION__, pszName));
 
       return PTR_ERR(psEntry);
   }
 
   psDebugFSEntry->psEntry = psEntry;
   *ppsNewEntry = (void*)psDebugFSEntry;
 
   return 0;
}
 
/*************************************************************************/ /*!
@Function        PVRDebugFSRemoveEntry
@Description    Removes an entry that was created by PVRDebugFSCreateEntry().
@Input          ppsDebugFSEntry  Pointer representing the entry to be removed.
                Has to be double pointer to avoid possible races
                and use-after-free situations.
@Return            void
*/ /**************************************************************************/
void PVRDebugFSRemoveEntry(PVR_DEBUGFS_ENTRY_DATA **ppsDebugFSEntry)
{
   _UnrefAndMaybeDestroyDebugFSEntry(ppsDebugFSEntry);
}
 
/*************************************************************************/ /*!
@Function        PVRDebugFSCreateStatisticEntry
@Description    Create a statistic entry in the specified directory.
@Input            pszName            String containing the name for the entry.
@Input            psDir            Pointer from PVRDebugFSCreateEntryDir()
                               representing the directory in which to create
                               the entry or NULL for the root directory.
@Input            pfnStatsPrint    A callback function used to print all the
                               statistics when reading from the statistic
                               entry.
@Input            pfnIncStatMemRefCount    A callback function used take a
                                       reference on the memory backing the
                                       statistic.
@Input            pfnDecStatMemRefCount    A callback function used drop a
                                       reference on the memory backing the
                                       statistic.
@Input            pvData            Private data to be passed to the provided
                               callback function.
 
@Return            PVR_DEBUGFS_DRIVER_STAT*   On success, a pointer representing
                                          the newly created statistic entry.
                                          Otherwise, NULL.
*/ /**************************************************************************/
PVR_DEBUGFS_DRIVER_STAT *PVRDebugFSCreateStatisticEntry(const char *pszName,
                    PVR_DEBUGFS_DIR_DATA *psDir,
                    OS_STATS_PRINT_FUNC *pfnStatsPrint,
                    PVRSRV_INC_STAT_MEM_REFCOUNT_FUNC    *pfnIncStatMemRefCount,
                    PVRSRV_INC_STAT_MEM_REFCOUNT_FUNC    *pfnDecStatMemRefCount,
                    void *pvData)
{
   PVR_DEBUGFS_DRIVER_STAT *psStatData;
   PVR_DEBUGFS_ENTRY_DATA * psDebugFSEntry;
 
   int iResult;
 
   if (pszName == NULL || pfnStatsPrint == NULL)
   {
       return NULL;
   }
   if ((pfnIncStatMemRefCount != NULL || pfnDecStatMemRefCount != NULL) && pvData == NULL)
   {
       return NULL;
   }
 
   psStatData = OSAllocZMemNoStats(sizeof(*psStatData));
   if (psStatData == NULL)
   {
       return NULL;
   }
 
   psStatData->pvData = pvData;
   psStatData->pfnStatsPrint = pfnStatsPrint;
   psStatData->pfnIncStatMemRefCount = pfnIncStatMemRefCount;
   psStatData->pfnDecStatMemRefCount = pfnDecStatMemRefCount;
   psStatData->ui32RefCount = 1;
 
   iResult = PVRDebugFSCreateEntry(pszName,
                   psDir,
                   &gsDebugFSStatisticReadOps,
                   NULL,
                   (PVRSRV_INC_FSENTRY_PVDATA_REFCNT_FN *) _RefStatEntry,
                   (PVRSRV_DEC_FSENTRY_PVDATA_REFCNT_FN *) _UnrefAndMaybeDestroyStatEntry,
                   psStatData,
                   &psDebugFSEntry);
   if (iResult != 0)
   {
       OSFreeMemNoStats(psStatData);
       return NULL;
   }
   psStatData->pvDebugFSEntry = (void*)psDebugFSEntry;
 
   if (pfnIncStatMemRefCount)
   {
       /* call function to take reference on the memory holding the stat */
       psStatData->pfnIncStatMemRefCount((void*)psStatData->pvData);
   }
 
   psDebugFSEntry->ui32RefCount = 1;
 
   return psStatData;
}
 
/*************************************************************************/ /*!
@Function        PVRDebugFSRemoveStatisticEntry
@Description    Removes a statistic entry that was created by
               PVRDebugFSCreateStatisticEntry().
@Input            psStatEntry  Pointer representing the statistic entry to be
                            removed.
@Return            void
*/ /**************************************************************************/
void PVRDebugFSRemoveStatisticEntry(PVR_DEBUGFS_DRIVER_STAT *psStatEntry)
{
   PVR_ASSERT(psStatEntry != NULL);
   /* drop reference on pvStatEntry*/
   _UnrefAndMaybeDestroyStatEntry(psStatEntry);
}
 
#if defined(PVRSRV_ENABLE_MEMTRACK_STATS_FILE)
static void *_DebugFSRawStatisticSeqStart(struct seq_file *psSeqFile,
                                          loff_t *puiPosition)
{
   PVR_DEBUGFS_RAW_DRIVER_STAT *psStatData =
           (PVR_DEBUGFS_RAW_DRIVER_STAT *) psSeqFile->private;
 
   if (psStatData)
   {
       if (*puiPosition == 0)
       {
           return psStatData;
       }
   }
   else
   {
       PVR_DPF((PVR_DEBUGFS_PVR_DPF_LEVEL, "%s: Called when psStatData is"
               " NULL", __func__));
   }
 
   return NULL;
}
 
static void _DebugFSRawStatisticSeqStop(struct seq_file *psSeqFile,
                                        void *pvData)
{
   PVR_DEBUGFS_RAW_DRIVER_STAT *psStatData =
           (PVR_DEBUGFS_RAW_DRIVER_STAT *) psSeqFile->private;
 
   if (!psStatData)
   {
       PVR_DPF((PVR_DEBUGFS_PVR_DPF_LEVEL, "%s: Called when psStatData is"
               " NULL", __func__));
   }
}
 
static void *_DebugFSRawStatisticSeqNext(struct seq_file *psSeqFile,
                                         void *pvData,
                                         loff_t *puiPosition)
{
   PVR_DEBUGFS_RAW_DRIVER_STAT *psStatData =
           (PVR_DEBUGFS_RAW_DRIVER_STAT *) psSeqFile->private;
   PVR_UNREFERENCED_PARAMETER(pvData);
 
   if (!psStatData)
   {
       PVR_DPF((PVR_DEBUGFS_PVR_DPF_LEVEL, "%s: Called when psStatData is"
               " NULL", __func__));
   }
 
   return NULL;
}
 
static int _DebugFSRawStatisticSeqShow(struct seq_file *psSeqFile, void *pvData)
{
   PVR_DEBUGFS_RAW_DRIVER_STAT *psStatData =
           (PVR_DEBUGFS_RAW_DRIVER_STAT *) pvData;
 
   if (psStatData != NULL)
   {
       psStatData->pfStatsPrint((void *) psSeqFile, NULL,
                                _StatsSeqPrintf);
       return 0;
   }
   else
   {
       PVR_DPF((PVR_DEBUGFS_PVR_DPF_LEVEL, "%s: Called when psStatData is"
               " NULL, returning -ENODATA(%d)", __FUNCTION__, -ENODATA));
   }
 
   return -ENODATA;
}
 
static struct seq_operations gsDebugFSRawStatisticReadOps =
{
   .start = _DebugFSRawStatisticSeqStart,
   .stop  = _DebugFSRawStatisticSeqStop,
   .next  = _DebugFSRawStatisticSeqNext,
   .show  = _DebugFSRawStatisticSeqShow,
};
 
PVR_DEBUGFS_RAW_DRIVER_STAT *PVRDebugFSCreateRawStatisticEntry(
                                             const IMG_CHAR *pszFileName,
                                             void *pvParentDir,
                                             OS_STATS_PRINT_FUNC *pfStatsPrint)
{
   PVR_DEBUGFS_RAW_DRIVER_STAT *psStatData;
   PVR_DEBUGFS_ENTRY_DATA *psDebugFsEntry;
 
   int iResult;
 
   if (pszFileName == NULL || pfStatsPrint == NULL)
   {
       return NULL;
   }
 
   psStatData = OSAllocZMemNoStats(sizeof(*psStatData));
   if (psStatData == NULL)
   {
       return NULL;
   }
 
   psStatData->pfStatsPrint = pfStatsPrint;
 
   PVR_ASSERT((pvParentDir == NULL));
 
   iResult = PVRDebugFSCreateEntry(pszFileName,
                                   pvParentDir,
                                   &gsDebugFSRawStatisticReadOps,
                                   NULL,
                                   NULL,
                                   NULL,
                                   psStatData,
                                   &psDebugFsEntry);
   if (iResult != 0)
   {
       OSFreeMemNoStats(psStatData);
       return NULL;
   }
   psStatData->pvDebugFsEntry = (void *) psDebugFsEntry;
 
   psDebugFsEntry->ui32RefCount = 1;
 
   return psStatData;
}
 
void PVRDebugFSRemoveRawStatisticEntry(PVR_DEBUGFS_RAW_DRIVER_STAT *psStatEntry)
{
   PVR_ASSERT(psStatEntry != NULL);
 
   PVRDebugFSRemoveEntry(&psStatEntry->pvDebugFsEntry);
   OSFreeMemNoStats(psStatEntry);
}
#endif
 
static IMG_BOOL _RefDirEntry(PVR_DEBUGFS_DIR_DATA *psDirEntry)
{
   IMG_BOOL bStatus = IMG_FALSE;
 
   PVR_ASSERT(psDirEntry != NULL && psDirEntry->psDir != NULL);
 
   mutex_lock(&gDebugFSLock);
 
   if (psDirEntry->ui32RefCount > 0)
   {
       /* Increment refCount */
       psDirEntry->ui32RefCount++;
       bStatus = IMG_TRUE;
   }
   else
   {
       PVR_DPF((PVR_DEBUGFS_PVR_DPF_LEVEL, "%s: Called to ref psDirEntry '%s'"
               " when ui32RefCount is zero", __FUNCTION__,
               psDirEntry->psDir->d_iname));
   }
 
   mutex_unlock(&gDebugFSLock);
 
   return bStatus;
}
 
static void _UnrefAndMaybeDestroyDirEntryWhileLocked(PVR_DEBUGFS_DIR_DATA **ppsDirEntry)
{
   PVR_DEBUGFS_DIR_DATA *psDirEntry = *ppsDirEntry;
 
   PVR_ASSERT(psDirEntry != NULL && psDirEntry->psDir != NULL);
 
   if (psDirEntry->ui32RefCount > 0)
   {
       /* Decrement refCount and free if now zero */
       if (--psDirEntry->ui32RefCount == 0)
       {
           /* if parent directory is not gpsPVRDebugFSEntryDir, decrement its refCount */
           debugfs_remove(psDirEntry->psDir);
           if (psDirEntry->psParentDir)
           {
               _UnrefAndMaybeDestroyDirEntryWhileLocked(&psDirEntry->psParentDir);
           }
           OSFreeMemNoStats(psDirEntry);
           *ppsDirEntry = NULL;
       }
   }
   else
   {
       PVR_DPF((PVR_DEBUGFS_PVR_DPF_LEVEL, "%s: Called to unref psDirEntry '%s'"
               " when ui32RefCount is zero", __FUNCTION__,
               psDirEntry->psDir->d_iname));
   }
}
 
static inline void _UnrefAndMaybeDestroyDirEntry(PVR_DEBUGFS_DIR_DATA **ppsDirEntry)
{
   mutex_lock(&gDebugFSLock);
   _UnrefAndMaybeDestroyDirEntryWhileLocked(ppsDirEntry);
   mutex_unlock(&gDebugFSLock);
}
 
static IMG_BOOL _RefDebugFSEntryNoLock(PVR_DEBUGFS_ENTRY_DATA *psDebugFSEntry)
{
   IMG_BOOL bResult = IMG_FALSE;
 
   PVR_ASSERT(psDebugFSEntry != NULL);
 
   bResult = (psDebugFSEntry->ui32RefCount > 0);
   if (bResult)
   {
       /* Increment refCount of psDebugFSEntry */
       psDebugFSEntry->ui32RefCount++;
   }
 
   return bResult;
}
 
static void _UnrefAndMaybeDestroyDebugFSEntry(PVR_DEBUGFS_ENTRY_DATA **ppsDebugFSEntry)
{
   PVR_DEBUGFS_ENTRY_DATA *psDebugFSEntry;
 
   mutex_lock(&gDebugFSLock);
   /* Decrement refCount of psDebugFSEntry, and free if now zero */
   psDebugFSEntry = *ppsDebugFSEntry;
   PVR_ASSERT(psDebugFSEntry != NULL);
 
   if (psDebugFSEntry->ui32RefCount > 0)
   {
       if (--psDebugFSEntry->ui32RefCount == 0)
       {
           struct dentry *psEntry = psDebugFSEntry->psEntry;
 
           if (psEntry)
           {
               /* Free any private data that was provided to debugfs_create_file() */
               if (psEntry->d_inode->i_private != NULL)
               {
                   PVR_DEBUGFS_PRIV_DATA *psPrivData = (PVR_DEBUGFS_PRIV_DATA*)psDebugFSEntry->psEntry->d_inode->i_private;
 
                   psPrivData->bValid = IMG_FALSE;
                   psPrivData->psDebugFSEntry = NULL;
                   OSFreeMemNoStats(psEntry->d_inode->i_private);
                   psEntry->d_inode->i_private = NULL;
               }
               debugfs_remove(psEntry);
           }
           /* decrement refcount of parent directory */
           if (psDebugFSEntry->psParentDir)
           {
               _UnrefAndMaybeDestroyDirEntryWhileLocked(&psDebugFSEntry->psParentDir);
           }
 
           /* now free the memory allocated for psDebugFSEntry */
           OSFreeMemNoStats(psDebugFSEntry);
           *ppsDebugFSEntry = NULL;
       }
   }
   else
   {
       PVR_DPF((PVR_DEBUGFS_PVR_DPF_LEVEL, "%s: Called to unref psDebugFSEntry '%s' when ui32RefCount is zero", __FUNCTION__, psDebugFSEntry->psEntry->d_iname));
   }
 
   mutex_unlock(&gDebugFSLock);
}
 
static IMG_BOOL _RefStatEntry(PVR_DEBUGFS_DRIVER_STAT *psStatEntry)
{
   IMG_BOOL bResult = IMG_FALSE;
 
   PVR_ASSERT(psStatEntry != NULL);
 
   mutex_lock(&gDebugFSLock);
 
   bResult = (psStatEntry->ui32RefCount > 0);
   if (bResult)
   {
       /* Increment refCount of psStatEntry */
       psStatEntry->ui32RefCount++;
   }
   else
   {
       PVR_DPF((PVR_DEBUGFS_PVR_DPF_LEVEL, "%s: Called to ref psStatEntry '%s' when ui32RefCount is zero", __FUNCTION__, psStatEntry->pvDebugFSEntry->psEntry->d_iname));
   }
 
   mutex_unlock(&gDebugFSLock);
 
   return bResult;
}
 
static IMG_BOOL _UnrefAndMaybeDestroyStatEntry(PVR_DEBUGFS_DRIVER_STAT *psStatEntry)
{
   IMG_BOOL bResult;
 
   PVR_ASSERT(psStatEntry != NULL);
 
   mutex_lock(&gDebugFSLock);
 
   bResult = (psStatEntry->ui32RefCount > 0);
 
   if (bResult)
   {
       /* Decrement refCount of psStatData, and free if now zero */
       if (--psStatEntry->ui32RefCount == 0)
       {
           mutex_unlock(&gDebugFSLock);
 
           if (psStatEntry->pvDebugFSEntry)
           {
               _UnrefAndMaybeDestroyDebugFSEntry((PVR_DEBUGFS_ENTRY_DATA**)&psStatEntry->pvDebugFSEntry);
           }
           if (psStatEntry->pfnDecStatMemRefCount)
           {
               /* call function to drop reference on the memory holding the stat */
               psStatEntry->pfnDecStatMemRefCount((void*)psStatEntry->pvData);
           }
           OSFreeMemNoStats(psStatEntry);
       }
       else
       {
           mutex_unlock(&gDebugFSLock);
       }
   }
   else
   {
       PVR_DPF((PVR_DEBUGFS_PVR_DPF_LEVEL, "%s: Called to unref psStatEntry '%s' when ui32RefCount is zero", __FUNCTION__, psStatEntry->pvDebugFSEntry->psEntry->d_iname));
       mutex_unlock(&gDebugFSLock);
   }
 
   return bResult;
}