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
|
/**********************************************************************
*
* Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
* Copyright(c) 2008 Imagination Technologies Ltd. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful but, except
* as otherwise stated in writing, without any warranty; without even the
* implied warranty of merchantability or fitness for a particular purpose.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*
* The full GNU General Public License is included in this distribution in
* the file called "COPYING".
*
* Contact Information:
* Imagination Technologies Ltd. <gpl-support@imgtec.com>
* Home Park Estate, Kings Langley, Herts, WD4 8LZ, UK
*
******************************************************************************/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/version.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <img_defs.h>
#include <servicesext.h>
#include <kernelbuffer.h>
#include "bc_cat.h"
#include <linux/slab.h>
#include <linux/dma-mapping.h>
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36))
#include <linux/mutex.h>
#endif
#if defined(BC_DISCONTIG_BUFFERS)
#include <linux/vmalloc.h>
#endif
#define DEVNAME "bccat"
#define DRVNAME DEVNAME
#define DEVICE_COUNT 10
#define BC_EXAMPLE_NUM_BUFFERS 3
#define BUFFERCLASS_DEVICE_NAME "Example Bufferclass Device (SW)"
#ifndef UNREFERENCED_PARAMETER
#define UNREFERENCED_PARAMETER(param) (param) = (param)
#endif
MODULE_SUPPORTED_DEVICE(DEVNAME);
#define unref__ __attribute__ ((unused))
typedef struct BC_CAT_BUFFER_TAG
{
unsigned long ulSize;
IMG_HANDLE hMemHandle;
#if defined(BC_DISCONTIG_BUFFERS)
IMG_SYS_PHYADDR *psSysAddr;
#else
IMG_SYS_PHYADDR sSysAddr;
IMG_SYS_PHYADDR sPageAlignSysAddr;
#endif
IMG_CPU_VIRTADDR sCPUVAddr;
PVRSRV_SYNC_DATA *psSyncData;
struct BC_CAT_BUFFER_TAG *psNext;
} BC_CAT_BUFFER;
typedef struct BC_CAT_DEVINFO_TAG
{
int ref;
unsigned long ulDeviceID;
BC_CAT_BUFFER *psSystemBuffer;
unsigned long ulNumBuffers;
PVRSRV_BC_BUFFER2SRV_KMJTABLE sPVRJTable;
PVRSRV_BC_SRV2BUFFER_KMJTABLE sBCJTable;
IMG_HANDLE hPVRServices;
unsigned long ulRefCount;
BUFFER_INFO sBufferInfo;
enum BC_memory buf_type;
} BC_CAT_DEVINFO;
typedef enum _BCE_ERROR_
{
BCE_OK = 0,
BCE_ERROR_GENERIC = 1,
BCE_ERROR_OUT_OF_MEMORY = 2,
BCE_ERROR_TOO_FEW_BUFFERS = 3,
BCE_ERROR_INVALID_PARAMS = 4,
BCE_ERROR_INIT_FAILURE = 5,
BCE_ERROR_CANT_REGISTER_CALLBACK = 6,
BCE_ERROR_INVALID_DEVICE = 7,
BCE_ERROR_DEVICE_REGISTER_FAILED = 8,
BCE_ERROR_NO_PRIMARY = 9
} BCE_ERROR;
extern IMG_IMPORT IMG_BOOL PVRGetBufferClassJTable(
PVRSRV_BC_BUFFER2SRV_KMJTABLE *psJTable);
static int bc_open(struct inode *i, struct file *f);
static int bc_release(struct inode *i, struct file *f);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
static int bc_ioctl(struct inode *inode, struct file *file,
unsigned int cmd, unsigned long arg);
#else
static long bc_ioctl(struct file *file,
unsigned int cmd, unsigned long arg);
static long bc_ioctl_unlocked(struct file *file,
unsigned int cmd, unsigned long arg);
#endif
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36))
static DEFINE_MUTEX(sBCExampleBridgeMutex);
#endif
static int bc_mmap(struct file *filp, struct vm_area_struct *vma);
static int BC_CreateBuffers(int id, bc_buf_params_t *p);
static PVRSRV_ERROR BC_DestroyBuffers(int id);
static PVRSRV_ERROR BC_Register(int id);
static PVRSRV_ERROR BC_Unregister(int id);
static PVRSRV_ERROR BCOpenPVRServices(IMG_HANDLE *phPVRServices);
static PVRSRV_ERROR BCClosePVRServices(IMG_HANDLE hPVRServices);
static IMG_VOID *BCAllocKernelMem(unsigned long ulSize);
static IMG_VOID BCFreeKernelMem(IMG_VOID *pvMem);
static BCE_ERROR BCAllocContigMemory(unsigned long ulSize,
IMG_HANDLE * phMemHandle,
IMG_CPU_VIRTADDR *pLinAddr,
IMG_CPU_PHYADDR *pPhysAddr);
static IMG_VOID BCFreeContigMemory(unsigned long ulSize,
IMG_HANDLE hMemHandle,
IMG_CPU_VIRTADDR LinAddr,
IMG_CPU_PHYADDR PhysAddr);
static IMG_SYS_PHYADDR CpuPAddrToSysPAddrBC(IMG_CPU_PHYADDR cpu_paddr);
static IMG_CPU_PHYADDR SysPAddrToCpuPAddrBC(IMG_SYS_PHYADDR sys_paddr);
static PVRSRV_ERROR BCGetLibFuncAddr(IMG_HANDLE hExtDrv,
IMG_CHAR *szFunctionName,
PFN_BC_GET_PVRJTABLE *ppfnFuncTable);
static BC_CAT_DEVINFO * GetAnchorPtr(int id);
static int major;
static struct class *bc_class;
static IMG_VOID *device[DEVICE_COUNT] = { 0 };
static PFN_BC_GET_PVRJTABLE pfnGetPVRJTable = IMG_NULL;
static int width_align;
static struct file_operations bc_cat_fops = {
.open = bc_open,
.release = bc_release,
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
.ioctl = bc_ioctl,
#else
.unlocked_ioctl = bc_ioctl_unlocked,
#ifdef CONFIG_COMPAT
.compat_ioctl = bc_ioctl,
#endif
#endif
.mmap = bc_mmap,
};
/*****************************************************************************
* func implementation
* **************************************************************************/
#define file_to_id(file) (iminor(file->f_path.dentry->d_inode))
static BC_CAT_DEVINFO * GetAnchorPtr(int id)
{
return (BC_CAT_DEVINFO *)device[id];
}
static IMG_VOID SetAnchorPtr(int id, BC_CAT_DEVINFO *psDevInfo)
{
device[id] = (IMG_VOID*)psDevInfo;
}
#if 0
static PVRSRV_ERROR OpenBCDevice(IMG_HANDLE *phDevice)
{
BC_CAT_DEVINFO *psDevInfo;
psDevInfo = GetAnchorPtr(id);
*phDevice = (IMG_HANDLE)psDevInfo;
return PVRSRV_OK;
}
#else
#define OPEN_FXN(id) \
static PVRSRV_ERROR OpenBCDevice##id(IMG_UINT32 ui32DeviceID, IMG_HANDLE *phDevice)\
{ \
BC_CAT_DEVINFO *psDevInfo; \
UNREFERENCED_PARAMETER(ui32DeviceID); \
psDevInfo = GetAnchorPtr (id); \
*phDevice = (IMG_HANDLE) psDevInfo; \
return PVRSRV_OK; \
}
OPEN_FXN(0)
OPEN_FXN(1)
OPEN_FXN(2)
OPEN_FXN(3)
OPEN_FXN(4)
OPEN_FXN(5)
OPEN_FXN(6)
OPEN_FXN(7)
OPEN_FXN(8)
OPEN_FXN(9)
#endif
static PVRSRV_ERROR CloseBCDevice(IMG_UINT32 handle , IMG_HANDLE hDevice)
{
PVR_UNREFERENCED_PARAMETER(hDevice);
return PVRSRV_OK;
}
static PVRSRV_ERROR GetBCBuffer(IMG_HANDLE hDevice,
IMG_UINT32 ui32BufferNumber,
PVRSRV_SYNC_DATA *psSyncData,
IMG_HANDLE *phBuffer)
{
BC_CAT_DEVINFO *psDevInfo;
if (!hDevice || !phBuffer)
return PVRSRV_ERROR_INVALID_PARAMS;
psDevInfo = (BC_CAT_DEVINFO*)hDevice;
if (ui32BufferNumber < psDevInfo->sBufferInfo.ui32BufferCount) {
psDevInfo->psSystemBuffer[ui32BufferNumber].psSyncData = psSyncData;
*phBuffer = (IMG_HANDLE)&psDevInfo->psSystemBuffer[ui32BufferNumber];
} else {
return PVRSRV_ERROR_INVALID_PARAMS;
}
return PVRSRV_OK;
}
static PVRSRV_ERROR GetBCInfo(IMG_HANDLE hDevice, BUFFER_INFO *psBCInfo)
{
BC_CAT_DEVINFO *psDevInfo;
if (!hDevice || !psBCInfo)
return PVRSRV_ERROR_INVALID_PARAMS;
psDevInfo = (BC_CAT_DEVINFO*)hDevice;
*psBCInfo = psDevInfo->sBufferInfo;
return PVRSRV_OK;
}
static PVRSRV_ERROR GetBCBufferAddr(IMG_HANDLE hDevice,
IMG_HANDLE hBuffer,
IMG_SYS_PHYADDR **ppsSysAddr,
IMG_UINT32 *pui32ByteSize,
IMG_VOID **ppvCpuVAddr,
IMG_HANDLE *phOSMapInfo,
IMG_BOOL *pbIsContiguous,
IMG_UINT32 *pui32TilingStride)
{
BC_CAT_BUFFER *psBuffer;
PVR_UNREFERENCED_PARAMETER(pui32TilingStride);
if (!hDevice || !hBuffer || !ppsSysAddr || !pui32ByteSize)
return PVRSRV_ERROR_INVALID_PARAMS;
psBuffer = (BC_CAT_BUFFER *) hBuffer;
*ppvCpuVAddr = psBuffer->sCPUVAddr;
*phOSMapInfo = IMG_NULL;
*pui32ByteSize = (IMG_UINT32)psBuffer->ulSize;
#if defined(BC_DISCONTIG_BUFFERS)
*ppsSysAddr = psBuffer->psSysAddr;
*pbIsContiguous = IMG_FALSE;
#else
*ppsSysAddr = &psBuffer->sPageAlignSysAddr;
*pbIsContiguous = IMG_TRUE;
#endif
return PVRSRV_OK;
}
static int BC_CreateBuffers(int id, bc_buf_params_t *p)
{
BC_CAT_DEVINFO *psDevInfo;
#if !defined(BC_DISCONTIG_BUFFERS)
IMG_CPU_PHYADDR paddr;
#endif
IMG_UINT32 i, stride;
unsigned long ulSize;
PVRSRV_PIXEL_FORMAT pixel_fmt;
//IMG_UINT32 ui32MaxWidth = 320 * 4;
if (p->count <= 0)
return -EINVAL;
if (p->width <= 1 || p->width % width_align || p->height <= 1)
return -EINVAL;
switch (p->fourcc) {
case BC_PIX_FMT_YV12:
pixel_fmt = PVRSRV_PIXEL_FORMAT_YV12;
stride = p->width;
break;
case BC_PIX_FMT_I420:
pixel_fmt = PVRSRV_PIXEL_FORMAT_I420;
stride = p->width;
break;
case BC_PIX_FMT_NV12:
pixel_fmt = PVRSRV_PIXEL_FORMAT_NV12;
stride = p->width;
break;
case BC_PIX_FMT_UYVY:
pixel_fmt = PVRSRV_PIXEL_FORMAT_FOURCC_ORG_UYVY;
stride = p->width << 1;
break;
case BC_PIX_FMT_RGB565:
pixel_fmt = PVRSRV_PIXEL_FORMAT_RGB565;
stride = p->width << 1;
break;
case BC_PIX_FMT_YUYV:
pixel_fmt = PVRSRV_PIXEL_FORMAT_FOURCC_ORG_YUYV;
stride = p->width << 1;
break;
case BC_PIX_FMT_ARGB:
pixel_fmt = PVRSRV_PIXEL_FORMAT_ARGB8888;
stride = p->width << 2;
break;
default:
return -EINVAL;
break;
}
if (p->type != BC_MEMORY_MMAP && p->type != BC_MEMORY_USERPTR)
return -EINVAL;
psDevInfo = GetAnchorPtr(id);
if (psDevInfo == NULL)
{
return (BCE_ERROR_DEVICE_REGISTER_FAILED);
}
if (psDevInfo->ulNumBuffers)
{
return (BCE_ERROR_GENERIC);
}
psDevInfo->buf_type = p->type;
psDevInfo->psSystemBuffer =
BCAllocKernelMem(sizeof(BC_CAT_BUFFER) * p->count);
if (!psDevInfo->psSystemBuffer)
return -ENOMEM;
memset(psDevInfo->psSystemBuffer, 0, sizeof(BC_CAT_BUFFER) * p->count);
ulSize = p->height * stride;
if (pixel_fmt == PVRSRV_PIXEL_FORMAT_NV12)
ulSize += (stride >> 1) * (p->height >> 1) << 1;
if ((pixel_fmt == PVRSRV_PIXEL_FORMAT_I420) || (pixel_fmt == PVRSRV_PIXEL_FORMAT_YV12) )
{
ulSize += (stride >> 1) * (p->height >> 1);
ulSize += (stride >> 1) * (p->height >> 1);
}
for (i=0; i < p->count; i++) {
if (psDevInfo->buf_type == BC_MEMORY_MMAP) {
#if defined(BC_DISCONTIG_BUFFERS)
if (BCAllocDiscontigMemory(ulSize,
&psDevInfo->psSystemBuffer[i].hMemHandle,
&psDevInfo->psSystemBuffer[i].sCPUVAddr,
&psDevInfo->psSystemBuffer[i].psSysAddr) != BCE_OK)
{
break;
}
#else
if (BCAllocContigMemory(ulSize,
&psDevInfo->psSystemBuffer[i].hMemHandle,
&psDevInfo->psSystemBuffer[i].sCPUVAddr,
&paddr) != BCE_OK)
{
/*TODO should free() and return failure*/
break;
}
psDevInfo->psSystemBuffer[i].sSysAddr = CpuPAddrToSysPAddrBC(paddr);
psDevInfo->psSystemBuffer[i].sPageAlignSysAddr.uiAddr =
psDevInfo->psSystemBuffer[i].sSysAddr.uiAddr & 0xFFFFF000;
}
#endif
psDevInfo->ulNumBuffers++;
psDevInfo->psSystemBuffer[i].ulSize = ulSize;
psDevInfo->psSystemBuffer[i].psSyncData = NULL;
}
p->count = psDevInfo->ulNumBuffers;
psDevInfo->sBufferInfo.ui32BufferCount = (IMG_UINT32)psDevInfo->ulNumBuffers;
psDevInfo->sBufferInfo.pixelformat = pixel_fmt;
psDevInfo->sBufferInfo.ui32Width = p->width;
psDevInfo->sBufferInfo.ui32Height = p->height;
psDevInfo->sBufferInfo.ui32ByteStride = stride;
psDevInfo->sBufferInfo.ui32BufferDeviceID = id;
psDevInfo->sBufferInfo.ui32Flags = PVRSRV_BC_FLAGS_YUVCSC_FULL_RANGE |
PVRSRV_BC_FLAGS_YUVCSC_BT601;
psDevInfo->sBCJTable.ui32TableSize = sizeof(PVRSRV_BC_SRV2BUFFER_KMJTABLE);
psDevInfo->sBCJTable.pfnOpenBCDevice = OpenBCDevice0;
psDevInfo->sBCJTable.pfnCloseBCDevice = CloseBCDevice;
psDevInfo->sBCJTable.pfnGetBCBuffer = GetBCBuffer;
psDevInfo->sBCJTable.pfnGetBCInfo = GetBCInfo;
psDevInfo->sBCJTable.pfnGetBufferAddr = GetBCBufferAddr;
/*
if (psDevInfo->sBufferInfo.ui32Width < ui32MaxWidth)
{
switch(pixel_fmt)
{
case PVRSRV_PIXEL_FORMAT_NV12:
case PVRSRV_PIXEL_FORMAT_I420:
{
psDevInfo->sBufferInfo.ui32Width += 320;
psDevInfo->sBufferInfo.ui32Height += 160;
psDevInfo->sBufferInfo.ui32ByteStride = psDevInfo->sBufferInfo.ui32Width;
break;
}
case PVRSRV_PIXEL_FORMAT_FOURCC_ORG_VYUY:
case PVRSRV_PIXEL_FORMAT_FOURCC_ORG_UYVY:
case PVRSRV_PIXEL_FORMAT_FOURCC_ORG_YUYV:
case PVRSRV_PIXEL_FORMAT_FOURCC_ORG_YVYU:
{
psDevInfo->sBufferInfo.ui32Width += 320;
psDevInfo->sBufferInfo.ui32Height += 160;
psDevInfo->sBufferInfo.ui32ByteStride = ui32Width*2;
break;
}
case PVRSRV_PIXEL_FORMAT_RGB565:
{
psDevInfo->sBufferInfo.ui32Width += 320;
psDevInfo->sBufferInfo.ui32Height += 160;
psDevInfo->sBufferInfo.ui32ByteStride = ui32Width*2;
break;
}
default:
{
return (BCE_ERROR_INVALID_PARAMS);
}
}
}
else
{
psDevInfo->sBufferInfo.ui32Width = BC_EXAMPLE_WIDTH;
psDevInfo->sBufferInfo.ui32Height = BC_EXAMPLE_HEIGHT;
psDevInfo->sBufferInfo.ui32ByteStride = BC_EXAMPLE_STRIDE;
}
*/
return (BCE_OK);
}
static PVRSRV_ERROR BC_DestroyBuffers(int id)
{
BC_CAT_DEVINFO *psDevInfo;
IMG_UINT32 i;
psDevInfo = GetAnchorPtr(id);
if (psDevInfo == NULL)
{
return (BCE_ERROR_DEVICE_REGISTER_FAILED);
}
if (psDevInfo->buf_type == BC_MEMORY_MMAP)
for (i = 0; i < psDevInfo->ulNumBuffers; i++) {
#if defined(BC_DISCONTIG_BUFFERS)
BCFreeDiscontigMemory(psDevInfo->psSystemBuffer[i].ulSize,
psDevInfo->psSystemBuffer[i].hMemHandle,
psDevInfo->psSystemBuffer[i].sCPUVAddr,
psDevInfo->psSystemBuffer[i].psSysAddr);
#else
BCFreeContigMemory(psDevInfo->psSystemBuffer[i].ulSize,
psDevInfo->psSystemBuffer[i].hMemHandle,
psDevInfo->psSystemBuffer[i].sCPUVAddr,
SysPAddrToCpuPAddrBC(psDevInfo->psSystemBuffer[i].sSysAddr));
#endif
}
// BCFreeKernelMem(psDevInfo->psSystemBuffer);
psDevInfo->ulNumBuffers = 0;
psDevInfo->sBufferInfo.pixelformat = PVRSRV_PIXEL_FORMAT_UNKNOWN;
psDevInfo->sBufferInfo.ui32Width = 0;
psDevInfo->sBufferInfo.ui32Height = 0;
psDevInfo->sBufferInfo.ui32ByteStride = 0;
psDevInfo->sBufferInfo.ui32BufferDeviceID = id;
psDevInfo->sBufferInfo.ui32Flags = 0;
psDevInfo->sBufferInfo.ui32BufferCount = (IMG_UINT32)psDevInfo->ulNumBuffers;
return PVRSRV_OK;
}
static PVRSRV_ERROR BC_Register(id)
{
BC_CAT_DEVINFO *psDevInfo;
//psDevInfo = GetAnchorPtr();
psDevInfo = GetAnchorPtr(id);
if (psDevInfo == NULL)
{
psDevInfo = (BC_CAT_DEVINFO *)BCAllocKernelMem(sizeof(BC_CAT_DEVINFO));
if (!psDevInfo)
return PVRSRV_ERROR_OUT_OF_MEMORY;
psDevInfo->ref = 0;
psDevInfo->ulRefCount = 0;
SetAnchorPtr(id, (IMG_VOID*)psDevInfo);
if (BCOpenPVRServices(&psDevInfo->hPVRServices) != PVRSRV_OK)
return PVRSRV_ERROR_INIT_FAILURE;
if (BCGetLibFuncAddr(psDevInfo->hPVRServices, "PVRGetBufferClassJTable",
&pfnGetPVRJTable) != PVRSRV_OK)
return PVRSRV_ERROR_INIT_FAILURE;
if (!(*pfnGetPVRJTable)(&psDevInfo->sPVRJTable))
return PVRSRV_ERROR_INIT_FAILURE;
psDevInfo->ulNumBuffers = 0;
psDevInfo->psSystemBuffer = BCAllocKernelMem(sizeof(BC_CAT_BUFFER) * BC_EXAMPLE_NUM_BUFFERS);
psDevInfo->sBufferInfo.pixelformat = PVRSRV_PIXEL_FORMAT_UNKNOWN;
psDevInfo->sBufferInfo.ui32Width = 0;
psDevInfo->sBufferInfo.ui32Height = 0;
psDevInfo->sBufferInfo.ui32ByteStride = 0;
psDevInfo->sBufferInfo.ui32BufferDeviceID = id;
psDevInfo->sBufferInfo.ui32Flags = 0;
psDevInfo->sBufferInfo.ui32BufferCount = (IMG_UINT32)psDevInfo->ulNumBuffers;
psDevInfo->sBCJTable.ui32TableSize = sizeof(PVRSRV_BC_SRV2BUFFER_KMJTABLE);
strncpy(psDevInfo->sBufferInfo.szDeviceName, BUFFERCLASS_DEVICE_NAME, MAX_BUFFER_DEVICE_NAME_SIZE);
#if 0
psDevInfo->sBCJTable.pfnOpenBCDevice = OpenBCDevice;
#else
if (id == 0) {
psDevInfo->sBCJTable.pfnOpenBCDevice = OpenBCDevice0;
} else if (id == 1) {
psDevInfo->sBCJTable.pfnOpenBCDevice = OpenBCDevice1;
} else if (id == 2) {
psDevInfo->sBCJTable.pfnOpenBCDevice = OpenBCDevice2;
} else if (id == 3) {
psDevInfo->sBCJTable.pfnOpenBCDevice = OpenBCDevice3;
} else if (id == 4) {
psDevInfo->sBCJTable.pfnOpenBCDevice = OpenBCDevice4;
} else if (id == 5) {
psDevInfo->sBCJTable.pfnOpenBCDevice = OpenBCDevice5;
} else if (id == 6) {
psDevInfo->sBCJTable.pfnOpenBCDevice = OpenBCDevice6;
} else if (id == 7) {
psDevInfo->sBCJTable.pfnOpenBCDevice = OpenBCDevice7;
} else if (id == 8) {
psDevInfo->sBCJTable.pfnOpenBCDevice = OpenBCDevice8;
} else if (id == 9) {
psDevInfo->sBCJTable.pfnOpenBCDevice = OpenBCDevice9;
} else {
printk("bad device id: %d\n", id);
return PVRSRV_ERROR_DEVICE_REGISTER_FAILED;
}
#endif
psDevInfo->sBCJTable.pfnCloseBCDevice = CloseBCDevice;
psDevInfo->sBCJTable.pfnGetBCBuffer = GetBCBuffer;
psDevInfo->sBCJTable.pfnGetBCInfo = GetBCInfo;
psDevInfo->sBCJTable.pfnGetBufferAddr = GetBCBufferAddr;
if (psDevInfo->sPVRJTable.pfnPVRSRVRegisterBCDevice(
&psDevInfo->sBCJTable,
(IMG_UINT32*)&psDevInfo->ulDeviceID) != PVRSRV_OK)
return PVRSRV_ERROR_DEVICE_REGISTER_FAILED;
}
psDevInfo->ulRefCount++;
return PVRSRV_OK;
}
static PVRSRV_ERROR BC_Unregister(int id)
{
BC_CAT_DEVINFO *psDevInfo;
// PVRSRV_BC_BUFFER2SRV_KMJTABLE *psJTable;
if ((psDevInfo = GetAnchorPtr(id)) == IMG_NULL)
return PVRSRV_ERROR_DEVICE_REGISTER_FAILED;
psDevInfo->ulRefCount--;
if (psDevInfo->ulRefCount)
return PVRSRV_ERROR_RETRY;
if (psDevInfo->ulRefCount == 0)
{
PVRSRV_BC_BUFFER2SRV_KMJTABLE *psJTable = &psDevInfo->sPVRJTable;
if (psJTable->pfnPVRSRVRemoveBCDevice(psDevInfo->ulDeviceID) != PVRSRV_OK)
//return PVRSRV_ERROR_GENERIC;
return 1;
if (BCClosePVRServices(psDevInfo->hPVRServices) != PVRSRV_OK) {
psDevInfo->hPVRServices = IMG_NULL;
//return PVRSRV_ERROR_GENERIC;
return 1;
}
if (psDevInfo->psSystemBuffer)
{
BCFreeKernelMem(psDevInfo->psSystemBuffer);
}
BCFreeKernelMem(psDevInfo);
SetAnchorPtr(id, IMG_NULL);
}
return PVRSRV_OK;
}
static int __init bc_cat_init(void)
{
struct device *bc_dev;
int id;
/* texture buffer width should be multiple of 8 for OMAP3 ES3.x,
* or 32 for ES2.x */
#ifdef PLAT_TI8168
width_align = 8;
#else
width_align = 8;
//width_align = cpu_is_omap3530() && ( omap_rev() < OMAP3430_REV_ES3_0 ) ? 32 : 8;
#endif
major = register_chrdev(0, DEVNAME, &bc_cat_fops);
if (major <= 0) {
printk(KERN_ERR DRVNAME ": unable to get major number\n");
goto ExitDisable;
}
bc_class = class_create(THIS_MODULE, DEVNAME);
if (IS_ERR(bc_class)) {
printk(KERN_ERR DRVNAME ": upable to create device class\n");
goto ExitUnregister;
}
for (id = 0; id < DEVICE_COUNT; id++) {
bc_dev = device_create(bc_class, NULL, MKDEV(major, id), NULL,
DEVNAME "%d", id);
if (IS_ERR(bc_dev)) {
printk(KERN_ERR DRVNAME ": unable to create device %d\n", id);
goto ExitDestroyClass;
}
if (BC_Register(id) != PVRSRV_OK) {
printk (KERN_ERR DRVNAME ": can't register BC service %d\n", id);
if (id > 0) {
/* lets live with the drivers that we were able to create soi
* far, even though it isn't as many as we'd like
*/
break;
}
goto ExitUnregister;
}
}
return 0;
ExitDestroyClass:
class_destroy(bc_class);
ExitUnregister:
unregister_chrdev(major, DEVNAME);
ExitDisable:
return -EBUSY;
}
static void __exit bc_cat_cleanup(void)
{
int id=0;
for (id = 0; id < DEVICE_COUNT; id++) {
device_destroy(bc_class, MKDEV(major, id));
}
class_destroy(bc_class);
unregister_chrdev(major, DEVNAME);
for (id = 0; id < DEVICE_COUNT; id++) {
if (BC_DestroyBuffers(id) != PVRSRV_OK) {
printk(KERN_ERR DRVNAME ": can't free texture buffers\n");
return;
}
if (BC_Unregister(id) != PVRSRV_OK) {
printk(KERN_ERR DRVNAME ": can't un-register BC service\n");
return;
}
}
}
static IMG_VOID *BCAllocKernelMem(unsigned long ulSize)
{
return kmalloc(ulSize, GFP_KERNEL);
}
static IMG_VOID BCFreeKernelMem(IMG_VOID *pvMem)
{
kfree(pvMem);
}
#if defined(BC_DISCONTIG_BUFFERS)
#define RANGE_TO_PAGES(range) (((range) + (PAGE_SIZE - 1)) >> PAGE_SHIFT)
#define VMALLOC_TO_PAGE_PHYS(vAddr) page_to_phys(vmalloc_to_page(vAddr))
BCE_ERROR BCAllocDiscontigMemory(unsigned long ulSize,
BCE_HANDLE unref__ *phMemHandle,
IMG_CPU_VIRTADDR *pLinAddr,
IMG_SYS_PHYADDR **ppPhysAddr)
{
unsigned long ulPages = RANGE_TO_PAGES(ulSize);
IMG_SYS_PHYADDR *pPhysAddr;
unsigned long ulPage;
IMG_CPU_VIRTADDR LinAddr;
LinAddr = __vmalloc(ulSize, GFP_KERNEL | __GFP_HIGHMEM, pgprot_noncached(PAGE_KERNEL));
if (!LinAddr)
{
return BCE_ERROR_OUT_OF_MEMORY;
}
pPhysAddr = kmalloc(ulPages * sizeof(IMG_SYS_PHYADDR), GFP_KERNEL);
if (!pPhysAddr)
{
vfree(LinAddr);
return BCE_ERROR_OUT_OF_MEMORY;
}
*pLinAddr = LinAddr;
for (ulPage = 0; ulPage < ulPages; ulPage++)
{
pPhysAddr[ulPage].uiAddr = VMALLOC_TO_PAGE_PHYS(LinAddr);
LinAddr += PAGE_SIZE;
}
*ppPhysAddr = pPhysAddr;
return BCE_OK;
}
void BCFreeDiscontigMemory(unsigned long ulSize,
BCE_HANDLE unref__ hMemHandle,
IMG_CPU_VIRTADDR LinAddr,
IMG_SYS_PHYADDR *pPhysAddr)
{
kfree(pPhysAddr);
vfree(LinAddr);
}
#else
BCE_ERROR BCAllocContigMemory(unsigned long ulSize,
IMG_HANDLE unref__ *phMemHandle,
IMG_CPU_VIRTADDR *pLinAddr,
IMG_CPU_PHYADDR *pPhysAddr)
{
#if defined(BCE_USE_SET_MEMORY)
void *pvLinAddr;
unsigned long ulAlignedSize = PAGE_ALIGN(ulSize);
int iPages = (int)(ulAlignedSize >> PAGE_SHIFT);
int iError;
pvLinAddr = kmalloc(ulAlignedSize, GFP_KERNEL);
BUG_ON(((unsigned long)pvLinAddr) & ~PAGE_MASK);
iError = set_memory_wc((unsigned long)pvLinAddr, iPages);
if (iError != 0)
{
printk(KERN_ERR DRVNAME ": BCAllocContigMemory: set_memory_wc failed (%d)\n", iError);
return (BCE_ERROR_OUT_OF_MEMORY);
}
pPhysAddr->uiAddr = virt_to_phys(pvLinAddr);
*pLinAddr = pvLinAddr;
return (BCE_OK);
#else
dma_addr_t dma;
void *pvLinAddr;
pvLinAddr = dma_alloc_coherent(NULL, ulSize, &dma, GFP_KERNEL);
if (pvLinAddr == NULL)
{
return (BCE_ERROR_OUT_OF_MEMORY);
}
pPhysAddr->uiAddr = dma;
*pLinAddr = pvLinAddr;
return (BCE_OK);
#endif
#if 0
IMG_VOID *pvLinAddr;
gfp_t mask = GFP_KERNEL;
pvLinAddr = alloc_pages_exact(ui32Size, mask);
if (pvLinAddr == IMG_NULL)
return PVRSRV_ERROR_OUT_OF_MEMORY;
pPhysAddr->uiAddr = virt_to_phys(pvLinAddr);
*pLinAddr = pvLinAddr;
#endif
// return PVRSRV_OK;
}
static IMG_VOID BCFreeContigMemory(unsigned long ulSize,
IMG_HANDLE unref__ hMemHandle,
IMG_CPU_VIRTADDR LinAddr,
IMG_CPU_PHYADDR PhysAddr)
{
// free_pages_exact(LinAddr, ui32Size);
#if defined(BCE_USE_SET_MEMORY)
unsigned long ulAlignedSize = PAGE_ALIGN(ulSize);
int iError;
int iPages = (int)(ulAlignedSize >> PAGE_SHIFT);
iError = set_memory_wb((unsigned long)LinAddr, iPages);
if (iError != 0)
{
printk(KERN_ERR DRVNAME ": BCFreeContigMemory: set_memory_wb failed (%d)\n", iError);
}
kfree(LinAddr);
#else
dma_free_coherent(NULL, ulSize, LinAddr, (dma_addr_t)PhysAddr.uiAddr);
#endif
}
#endif
static IMG_SYS_PHYADDR CpuPAddrToSysPAddrBC(IMG_CPU_PHYADDR cpu_paddr)
{
IMG_SYS_PHYADDR sys_paddr;
sys_paddr.uiAddr = cpu_paddr.uiAddr;
return sys_paddr;
}
static IMG_CPU_PHYADDR SysPAddrToCpuPAddrBC(IMG_SYS_PHYADDR sys_paddr)
{
IMG_CPU_PHYADDR cpu_paddr;
cpu_paddr.uiAddr = sys_paddr.uiAddr;
return cpu_paddr;
}
static PVRSRV_ERROR BCOpenPVRServices (IMG_HANDLE *phPVRServices)
{
*phPVRServices = 0;
return PVRSRV_OK;
}
static PVRSRV_ERROR BCClosePVRServices (IMG_HANDLE unref__ hPVRServices)
{
return PVRSRV_OK;
}
static PVRSRV_ERROR BCGetLibFuncAddr(IMG_HANDLE unref__ hExtDrv,
IMG_CHAR *szFunctionName,
PFN_BC_GET_PVRJTABLE *ppfnFuncTable)
{
if (strcmp("PVRGetBufferClassJTable", szFunctionName) != 0)
return PVRSRV_ERROR_INVALID_PARAMS;
*ppfnFuncTable = PVRGetBufferClassJTable;
return PVRSRV_OK;
}
static int bc_open(struct inode *i, struct file *f)
{
BC_CAT_DEVINFO *devinfo;
int id = file_to_id(f);
if ((devinfo = GetAnchorPtr(id)) == IMG_NULL) {
printk("no device %d\n", id);
return -ENODEV;
}
if (devinfo->ref) {
printk("device %d busy\n", id);
return -EBUSY;
}
devinfo->ref++;
return 0;
}
static int bc_release(struct inode *i, struct file *f)
{
BC_CAT_DEVINFO *devinfo;
int id = file_to_id(f);
if ((devinfo = GetAnchorPtr(id)) == IMG_NULL)
return -ENODEV;
for (id = 0; id < DEVICE_COUNT; id++) {
if (BC_DestroyBuffers(id) != PVRSRV_OK) {
printk(KERN_ERR DRVNAME ": can't free texture buffer \n");
}
}
if (devinfo->ref)
devinfo->ref--;
return 0;
}
static int bc_mmap(struct file *filp, struct vm_area_struct *vma)
{
#if defined(DEBUG)
printk("bc_mmap: vma->vm_start = %#lx\n", vma->vm_start);
printk("bc_mmap: vma->vm_pgoff = %#lx\n", vma->vm_pgoff);
printk("bc_mmap: size = %#lx\n", vma->vm_end - vma->vm_start);
#endif
/*FIXME check start & size*/
if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
vma->vm_end - vma->vm_start,
vma->vm_page_prot)) {
printk("bc_mmap: failed remap_pfn_range\n");
return -EAGAIN;
}
return 0;
}
int ReconfigureBuffer(int id, bc_buf_params_t *p, unsigned int *uiSucceed)
{
BCE_ERROR eError;
eError = BC_DestroyBuffers(id);
if (eError != BCE_OK)
{
*uiSucceed = 0;
return -1;
}
eError = BC_CreateBuffers(id,p);
if (eError != BCE_OK)
{
*uiSucceed = 0;
return -1;
}
*uiSucceed = 1;
return 0;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
static int bc_ioctl(struct inode *inode, struct file *file,
unsigned int cmd, unsigned long arg)
#else
static long bc_ioctl(struct file *file,
unsigned int cmd, unsigned long arg)
#endif
{
BC_CAT_DEVINFO *devinfo;
//PVR_UNREFERENCED_PARAMETER(inode);
int id = file_to_id (file);
if ((devinfo = GetAnchorPtr(id)) == IMG_NULL)
return -ENODEV;
switch(_IOC_NR(cmd)) {
case _IOC_NR(BCIOGET_BUFFERCOUNT):
{
BCIO_package *params = (BCIO_package *)arg;
if (!access_ok(VERIFY_WRITE, params, sizeof(BCIO_package)))
return -EFAULT;
params->output = devinfo->sBufferInfo.ui32BufferCount;
break;
}
case _IOC_NR(BCIOGET_BUFFERPHYADDR):
{
int idx;
BCIO_package *params = (BCIO_package *)arg;
if (!access_ok(VERIFY_WRITE, params, sizeof(BCIO_package)))
return -EFAULT;
idx = params->input;
if (idx < 0 || idx > devinfo->ulNumBuffers) {
printk(KERN_ERR DRVNAME
": BCIOGET_BUFFERADDR - idx out of range\n");
return -EINVAL;
}
params->output = devinfo->psSystemBuffer[idx].sSysAddr.uiAddr;
break;
}
case _IOC_NR(BCIOGET_BUFFERIDX):
{
int idx;
BC_CAT_BUFFER *buffer;
BCIO_package *params = (BCIO_package *)arg;
if (!access_ok(VERIFY_WRITE, params, sizeof(BCIO_package)))
return -EFAULT;
for (idx = 0; idx < devinfo->ulNumBuffers; idx++) {
buffer = &devinfo->psSystemBuffer[idx];
if (params->input == (int)buffer->sSysAddr.uiAddr) {
params->output = idx;
return 0;
}
}
printk(KERN_ERR DRVNAME ": BCIOGET_BUFFERIDX- buffer not found\n");
return -EINVAL;
break;
}
case _IOC_NR(BCIOREQ_BUFFERS):
{
bc_buf_params_t *p = (bc_buf_params_t *) arg;
if (!access_ok(VERIFY_WRITE, p, sizeof(bc_buf_params_t)))
return -EFAULT;
return BC_CreateBuffers(id, p);
break;
}
case _IOC_NR(BCIOSET_BUFFERPHYADDR):
{
bc_buf_ptr_t p;
IMG_CPU_PHYADDR img_pa;
if (copy_from_user(&p, (void __user *)arg, sizeof(p)))
return -EFAULT;
if (p.index >= devinfo->ulNumBuffers || !p.pa)
return -EINVAL;
/*TODO check buffer size*/
img_pa.uiAddr = p.pa;
devinfo->psSystemBuffer[p.index].sCPUVAddr = phys_to_virt(p.pa);
devinfo->psSystemBuffer[p.index].sSysAddr =
CpuPAddrToSysPAddrBC(img_pa);
devinfo->psSystemBuffer[p.index].sPageAlignSysAddr.uiAddr =
devinfo->psSystemBuffer[p.index].sSysAddr.uiAddr &
0xFFFFF000;
break;
}
case _IOC_NR(BCIORECONFIGURE_BUFFERS):
{
unsigned int outputparam;
bc_buf_params_t *p = (bc_buf_params_t *) arg;
if(ReconfigureBuffer(id,p,&outputparam) == -1)
{
return -EFAULT;
}
break;
}
default:
return -EFAULT;
}
return 0;
}
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36))
static long bc_ioctl_unlocked(struct file *file, unsigned int cmd, unsigned long arg)
{
int res;
mutex_lock(&sBCExampleBridgeMutex);
res = bc_ioctl(file, cmd, arg);
mutex_unlock(&sBCExampleBridgeMutex);
return res;
}
#endif
module_init(bc_cat_init);
module_exit(bc_cat_cleanup);
MODULE_LICENSE("GPL v2");
|