summaryrefslogtreecommitdiff
path: root/drivers/gpu/pvr/omaplfb/omaplfb_displayclass.c
blob: ca9bf3eee78a7942a65c1787813b5f5d43262445 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
/**********************************************************************
 *
 * 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/version.h>
#include <linux/kernel.h>
#include <linux/console.h>
#include <linux/fb.h>

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32))
#include <plat/vrfb.h>
#include <plat/display.h>
#else
#include <mach/vrfb.h>
#include <mach/display.h>
#endif

#ifdef RELEASE
#include <../drivers/video/omap2/omapfb/omapfb.h>
#undef DEBUG
#else
#undef DEBUG
#include <../drivers/video/omap2/omapfb/omapfb.h>
#endif

#include <linux/module.h>
#include <linux/string.h>
#include <linux/notifier.h>

#ifdef CONFIG_TILER_OMAP
#include <mach/tiler.h>
#define TILER_MIN_PADDR		0x60000000
#define TILER_MAX_PADDR		0x7fffffff
#endif

#include "img_defs.h"
#include "servicesext.h"
#include "kerneldisplay.h"
#include "omaplfb.h"

#define OMAPLFB_COMMAND_COUNT		1
#define MAX_BUFFERS_FLIPPING		4
/* Put 0 as desired bpp to use the default in the framebuffer */
#define DESIRED_BPP			0 /* Possible values 32,16,0 */

/* Pointer Display->Services */
static PFN_DC_GET_PVRJTABLE pfnGetPVRJTable = NULL;

/* Pointer to the display devices */
static OMAPLFB_DEVINFO *pDisplayDevices = NULL;

static void OMAPLFBSyncIHandler(struct work_struct*);

/*
 * Swap to display buffer. This buffer refers to one inside the
 * framebuffer memory.
 * in: hDevice, hBuffer, ui32SwapInterval, hPrivateTag, ui32ClipRectCount,
 * psClipRect
 */
static PVRSRV_ERROR SwapToDCBuffer(IMG_HANDLE hDevice,
                                   IMG_HANDLE hBuffer,
                                   IMG_UINT32 ui32SwapInterval,
                                   IMG_HANDLE hPrivateTag,
                                   IMG_UINT32 ui32ClipRectCount,
                                   IMG_RECT *psClipRect)
{
	/* Nothing to do */
	return PVRSRV_OK;
}

/*
 * Set display destination rectangle.
 * in: hDevice, hSwapChain, psRect
 */
static PVRSRV_ERROR SetDCDstRect(IMG_HANDLE hDevice,
	IMG_HANDLE hSwapChain,
	IMG_RECT *psRect)
{
	/* Nothing to do */
	return PVRSRV_ERROR_NOT_SUPPORTED;
}

/*
 * Set display source rectangle.
 * in: hDevice, hSwapChain, psRect
 */
static PVRSRV_ERROR SetDCSrcRect(IMG_HANDLE hDevice,
                                 IMG_HANDLE hSwapChain,
                                 IMG_RECT *psRect)
{
	/* Nothing to do */
	return PVRSRV_ERROR_NOT_SUPPORTED;
}

/*
 * Set display destination colour key.
 * in: hDevice, hSwapChain, ui32CKColour
 */
static PVRSRV_ERROR SetDCDstColourKey(IMG_HANDLE hDevice,
                                      IMG_HANDLE hSwapChain,
                                      IMG_UINT32 ui32CKColour)
{
	/* Nothing to do */
	return PVRSRV_ERROR_NOT_SUPPORTED;
}

/*
 * Set display source colour key.
 * in: hDevice, hSwapChain, ui32CKColour
 */
static PVRSRV_ERROR SetDCSrcColourKey(IMG_HANDLE hDevice,
                                      IMG_HANDLE hSwapChain,
                                      IMG_UINT32 ui32CKColour)
{
	/* Nothing to do */
	return PVRSRV_ERROR_NOT_SUPPORTED;
}

/*
 * Closes the display.
 * in: hDevice
 */
static PVRSRV_ERROR CloseDCDevice(IMG_HANDLE hDevice)
{
	/* Nothing to do */
	return PVRSRV_OK;
}

/*
 * Flushes the sync queue present in the specified swap chain.
 * in: psSwapChain
 */
static void FlushInternalSyncQueue(OMAPLFB_SWAPCHAIN *psSwapChain)
{
#ifdef DEBUG
	OMAPLFB_DEVINFO	*psDevInfo = (OMAPLFB_DEVINFO *) psSwapChain->pvDevInfo;
#endif
	OMAPLFB_FLIP_ITEM *psFlipItem;
	unsigned long            ulMaxIndex;
	unsigned long            i;
	
	psFlipItem = &psSwapChain->psFlipItems[psSwapChain->ulRemoveIndex];
	ulMaxIndex = psSwapChain->ulBufferCount - 1;

#ifdef DEBUG
	DEBUG_PRINTK("Flushing sync queue on display %u",
		psDevInfo->uDeviceID);
#endif
	for(i = 0; i < psSwapChain->ulBufferCount; i++)
	{
		if (psFlipItem->bValid == OMAP_FALSE)
			continue;

		DEBUG_PRINTK("Flushing swap buffer index %lu",
			psSwapChain->ulRemoveIndex);

		/* Flip the buffer if it hasn't been flipped */
		if(psFlipItem->bFlipped == OMAP_FALSE)
		{
			OMAPLFBFlip(psSwapChain,
				(unsigned long)psFlipItem->sSysAddr);
		}

		/* If the command didn't complete, assume it did */
		if(psFlipItem->bCmdCompleted == OMAP_FALSE)
		{
			DEBUG_PRINTK("Calling command complete for swap "
				"buffer index %lu",
				psSwapChain->ulRemoveIndex);
			psSwapChain->psPVRJTable->pfnPVRSRVCmdComplete(
				(IMG_HANDLE)psFlipItem->hCmdComplete,
				IMG_TRUE);
		}
		
		psSwapChain->ulRemoveIndex++;
		if(psSwapChain->ulRemoveIndex > ulMaxIndex)
			psSwapChain->ulRemoveIndex = 0;
		
		/* Put the state of the buffer to be used again later */
		psFlipItem->bFlipped = OMAP_FALSE;
		psFlipItem->bCmdCompleted = OMAP_FALSE;
		psFlipItem->bValid = OMAP_FALSE;
		psFlipItem =
			&psSwapChain->psFlipItems[psSwapChain->ulRemoveIndex];
	}

	psSwapChain->ulInsertIndex = 0;
	psSwapChain->ulRemoveIndex = 0;
}

/*
 * Sets the flush state of the specified display device
 * at the swap chain level without blocking the call.
 * in: psDevInfo, bFlushState
 */
static void SetFlushStateInternalNoLock(OMAPLFB_DEVINFO* psDevInfo,
                                        OMAP_BOOL bFlushState)
{
	OMAPLFB_SWAPCHAIN *psSwapChain = psDevInfo->psSwapChain;

	/* Nothing to do if there is no swap chain */
	if (psSwapChain == NULL){
		DEBUG_PRINTK("Swap chain is null, nothing to do for"
			" display %u", psDevInfo->uDeviceID);
		return;
	}

	if (bFlushState)
	{
		DEBUG_PRINTK("Desired flushState is true for display %u",
			psDevInfo->uDeviceID);
		if (psSwapChain->ulSetFlushStateRefCount == 0)
		{
			psSwapChain->bFlushCommands = OMAP_TRUE;
			FlushInternalSyncQueue(psSwapChain);
		}
		psSwapChain->ulSetFlushStateRefCount++;
	}
	else
	{
		DEBUG_PRINTK("Desired flushState is false for display %u",
			psDevInfo->uDeviceID);
		if (psSwapChain->ulSetFlushStateRefCount != 0)
		{
			psSwapChain->ulSetFlushStateRefCount--;
			if (psSwapChain->ulSetFlushStateRefCount == 0)
			{
				psSwapChain->bFlushCommands = OMAP_FALSE;
			}
		}
	}
}

/*
 * Sets the flush state of the specified display device
 * at the swap chain level blocking the call if needed.
 * in: psDevInfo, bFlushState
 */
static IMG_VOID SetFlushStateInternal(OMAPLFB_DEVINFO* psDevInfo,
                                      OMAP_BOOL bFlushState)
{
	DEBUG_PRINTK("Executing for display %u",
		psDevInfo->uDeviceID);
	mutex_lock(&psDevInfo->sSwapChainLockMutex);
	SetFlushStateInternalNoLock(psDevInfo, bFlushState);
	mutex_unlock(&psDevInfo->sSwapChainLockMutex);
}

/*
 * Sets the flush state of the specified display device
 * at device level blocking the call if needed.
 * in: psDevInfo, bFlushState
 */
static void SetFlushStateExternal(OMAPLFB_DEVINFO* psDevInfo,
                                  OMAP_BOOL bFlushState)
{
	DEBUG_PRINTK("Executing for display %u",
		psDevInfo->uDeviceID);
	mutex_lock(&psDevInfo->sSwapChainLockMutex);
	if (psDevInfo->bFlushCommands != bFlushState)
	{
		psDevInfo->bFlushCommands = bFlushState;
		SetFlushStateInternalNoLock(psDevInfo, bFlushState);
	}
	mutex_unlock(&psDevInfo->sSwapChainLockMutex);
}

/*
 * Unblank the framebuffer display
 * in: psDevInfo
 */
static OMAP_ERROR UnBlankDisplay(OMAPLFB_DEVINFO *psDevInfo)
{
	DEBUG_PRINTK("Executing for display %u",
		psDevInfo->uDeviceID);

	acquire_console_sem();
	if (fb_blank(psDevInfo->psLINFBInfo, FB_BLANK_UNBLANK))
	{
		release_console_sem();
		WARNING_PRINTK("fb_blank failed");
		return OMAP_ERROR_GENERIC;
	}
	release_console_sem();

	return OMAP_OK;
}

/*
 * Framebuffer listener
 * in: psNotif, event, data
 */
static int FrameBufferEvents(struct notifier_block *psNotif,
                             unsigned long event, void *data)
{
	OMAPLFB_DEVINFO *psDevInfo;
	OMAPLFB_SWAPCHAIN *psSwapChain;
	struct fb_event *psFBEvent = (struct fb_event *)data;
	OMAP_BOOL bBlanked;
	int i;

	DEBUG_PRINTK("Framebuffer event (%lu) happened", event);
	if (event != FB_EVENT_BLANK){
		DEBUG_PRINTK("Ignoring");
		return 0;
	}

	DEBUG_PRINTK("Event is FB_EVENT_BLANK");
	
	psDevInfo = 0;
	for(i = 0; i < FRAMEBUFFER_COUNT; i++)
	{
		if(psFBEvent->info == (&pDisplayDevices[i])->psLINFBInfo)
		{
			psDevInfo = &pDisplayDevices[i];
			break;
		}
	}

	if(!psDevInfo)
	{
		WARNING_PRINTK("Unable to find the display related to "
			" the framebuffer event");
		return 1;
	}

	psSwapChain = psDevInfo->psSwapChain;

	if(!psSwapChain)
	{
		DEBUG_PRINTK("No swapchain associated with this display");
		return 0;
	}

	bBlanked = (*(IMG_INT *)psFBEvent->data != 0) ?
		OMAP_TRUE: OMAP_FALSE;

	/* Check if the blank state is the same as the swap chain */
	if (bBlanked != psSwapChain->bBlanked)
	{
		DEBUG_PRINTK("Executing for display %u",
			psDevInfo->uDeviceID);

		/* Set the new blank state in the swap chain */
		psSwapChain->bBlanked = bBlanked;

		if (bBlanked)
		{
			DEBUG_PRINTK("Requesting flush state true for"
				" display %u", psDevInfo->uDeviceID);
			SetFlushStateInternal(psDevInfo, OMAP_TRUE);
		}
		else
		{
			DEBUG_PRINTK("Requesting flush state false for"
				" display %u", psDevInfo->uDeviceID);
			SetFlushStateInternal(psDevInfo, OMAP_FALSE);
		}
	}
	else
	{
		DEBUG_PRINTK("Ignoring event for display %u",
			psDevInfo->uDeviceID);
	}

	return 0;
}

/*
 * Registers a listener for changes in the framebuffer
 * in: psDevInfo
 */
static OMAP_ERROR EnableLFBEventNotification(OMAPLFB_DEVINFO *psDevInfo)
{
	OMAPLFB_SWAPCHAIN *psSwapChain = psDevInfo->psSwapChain;
	OMAP_ERROR         eError;
	
	memset(&psDevInfo->sLINNotifBlock, 0,
		sizeof(psDevInfo->sLINNotifBlock));

	/* Register the function to listen the changes */
	psDevInfo->sLINNotifBlock.notifier_call = FrameBufferEvents;
	psSwapChain->bBlanked = OMAP_FALSE;

	DEBUG_PRINTK("Registering framebuffer event listener for"
		" display %u", psDevInfo->uDeviceID);

	if (fb_register_client(&psDevInfo->sLINNotifBlock))
	{
		WARNING_PRINTK("fb_register_client failed for"
			" display %u", psDevInfo->uDeviceID);
		return OMAP_ERROR_GENERIC;
	}

	eError = UnBlankDisplay(psDevInfo);
	if (eError != OMAP_OK)
	{
		WARNING_PRINTK("UnBlankDisplay failed for"
			" display %u", psDevInfo->uDeviceID);
		return eError;
	}

	return OMAP_OK;
}

/*
 * Unregister a listener from the framebuffer
 * in: psDevInfo
 */
static OMAP_ERROR DisableLFBEventNotification(OMAPLFB_DEVINFO *psDevInfo)
{
	DEBUG_PRINTK("Removing framebuffer event listener for"
		" display %u", psDevInfo->uDeviceID);

	if (fb_unregister_client(&psDevInfo->sLINNotifBlock))
	{
		WARNING_PRINTK("fb_unregister_client failed for"
			" display %u", psDevInfo->uDeviceID);
		return OMAP_ERROR_GENERIC;
	}

	return OMAP_OK;
}

/*
 * Opens the display.
 * in: ui32DeviceID, phDevice
 * out: psSystemBufferSyncData
 */
static PVRSRV_ERROR OpenDCDevice(IMG_UINT32 ui32DeviceID,
                                 IMG_HANDLE *phDevice,
                                 PVRSRV_SYNC_DATA* psSystemBufferSyncData)
{
	OMAPLFB_DEVINFO *psDevInfo;
	int i;

	psDevInfo = 0;
	for(i = 0; i < FRAMEBUFFER_COUNT; i++)
	{
		if (ui32DeviceID == (&pDisplayDevices[i])->uDeviceID)
		{
			psDevInfo = &pDisplayDevices[i];
			break;
		}
	}

	if(!psDevInfo)
	{
		WARNING_PRINTK("Unable to identify display device with id %i",
			(int)ui32DeviceID);
		return 1;
	}

	psDevInfo->sSystemBuffer.psSyncData = psSystemBufferSyncData;
	if ( UnBlankDisplay(psDevInfo) != OMAP_OK)
	{
		WARNING_PRINTK("UnBlankDisplay failed for"
			" display %u", psDevInfo->uDeviceID);
		return PVRSRV_ERROR_UNBLANK_DISPLAY_FAILED;
	}
	*phDevice = (IMG_HANDLE)psDevInfo;

	return PVRSRV_OK;
}

/*
 * Gets the available formats for the display.
 * in: hDevice
 * out: pui32NumFormats, psFormat
 */
static PVRSRV_ERROR EnumDCFormats(IMG_HANDLE hDevice,
                                  IMG_UINT32 *pui32NumFormats,
                                  DISPLAY_FORMAT *psFormat)
{
	OMAPLFB_DEVINFO	*psDevInfo;
	if(!hDevice || !pui32NumFormats)
	{
		ERROR_PRINTK("Invalid parameters");
		return PVRSRV_ERROR_INVALID_PARAMS;
	}

	psDevInfo = (OMAPLFB_DEVINFO*)hDevice;
	*pui32NumFormats = 1;
	
	if(psFormat)
		psFormat[0] = psDevInfo->sDisplayFormat;
	else
		WARNING_PRINTK("Display format is null for"
			" display %u", psDevInfo->uDeviceID);

	return PVRSRV_OK;
}

/*
 * Gets the available dimensions for the display.
 * in: hDevice, psFormat
 * out: pui32NumDims, psDim
 */
static PVRSRV_ERROR EnumDCDims(IMG_HANDLE hDevice, 
                               DISPLAY_FORMAT *psFormat,
                               IMG_UINT32 *pui32NumDims,
                               DISPLAY_DIMS *psDim)
{
	OMAPLFB_DEVINFO	*psDevInfo;
	if(!hDevice || !psFormat || !pui32NumDims)
	{
		ERROR_PRINTK("Invalid parameters");
		return PVRSRV_ERROR_INVALID_PARAMS;
	}

	psDevInfo = (OMAPLFB_DEVINFO*)hDevice;
	*pui32NumDims = 1;
	
	if(psDim)
		psDim[0] = psDevInfo->sDisplayDim;
	else
		WARNING_PRINTK("Display dimensions are null for"
			" display %u", psDevInfo->uDeviceID);

	return PVRSRV_OK;
}

/*
 * Gets the display framebuffer physical address.
 * in: hDevice
 * out: phBuffer
 */
static PVRSRV_ERROR GetDCSystemBuffer(IMG_HANDLE hDevice, IMG_HANDLE *phBuffer)
{
	OMAPLFB_DEVINFO	*psDevInfo;

	if(!hDevice || !phBuffer)
	{
		ERROR_PRINTK("Invalid parameters");
		return PVRSRV_ERROR_INVALID_PARAMS;
	}

	psDevInfo = (OMAPLFB_DEVINFO*)hDevice;
	*phBuffer = (IMG_HANDLE)&psDevInfo->sSystemBuffer;

	return PVRSRV_OK;
}

/*
 * Gets the display general information.
 * in: hDevice
 * out: psDCInfo
 */
static PVRSRV_ERROR GetDCInfo(IMG_HANDLE hDevice, DISPLAY_INFO *psDCInfo)
{
	OMAPLFB_DEVINFO	*psDevInfo;

	if(!hDevice || !psDCInfo)
	{
		ERROR_PRINTK("Invalid parameters");
		return PVRSRV_ERROR_INVALID_PARAMS;
	}

	psDevInfo = (OMAPLFB_DEVINFO*)hDevice;
	*psDCInfo = psDevInfo->sDisplayInfo;

	return PVRSRV_OK;
}

/*
 * Gets the display framebuffer virtual address.
 * in: hDevice
 * out: ppsSysAddr, pui32ByteSize, ppvCpuVAddr, phOSMapInfo, pbIsContiguous
 */
static PVRSRV_ERROR GetDCBufferAddr(
				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)
{
	OMAPLFB_DEVINFO	*psDevInfo;
	OMAPLFB_BUFFER *psSystemBuffer;

	if(!hDevice || !hBuffer || !ppsSysAddr || !pui32ByteSize )
	{
		ERROR_PRINTK("Invalid parameters");
		return PVRSRV_ERROR_INVALID_PARAMS;
	}

	psDevInfo = (OMAPLFB_DEVINFO*)hDevice;
	psSystemBuffer = (OMAPLFB_BUFFER *)hBuffer;
	*ppsSysAddr = &psSystemBuffer->sSysAddr;
	*pui32ByteSize = (IMG_UINT32)psDevInfo->sFBInfo.ulBufferSize;

	if (ppvCpuVAddr)
		*ppvCpuVAddr = psSystemBuffer->sCPUVAddr;

	if (phOSMapInfo)
		*phOSMapInfo = (IMG_HANDLE)0;

	if (pbIsContiguous)
		*pbIsContiguous = IMG_TRUE;

	return PVRSRV_OK;
}

/*
 * Creates a swap chain. Called when a 3D application begins.
 * in: hDevice, ui32Flags, ui32BufferCount, psDstSurfAttrib, psSrcSurfAttrib
 * ui32OEMFlags
 * out: phSwapChain, ppsSyncData, pui32SwapChainID
 */
static PVRSRV_ERROR CreateDCSwapChain(IMG_HANDLE hDevice,
                                      IMG_UINT32 ui32Flags,
                                      DISPLAY_SURF_ATTRIBUTES *psDstSurfAttrib,
                                      DISPLAY_SURF_ATTRIBUTES *psSrcSurfAttrib,
                                      IMG_UINT32 ui32BufferCount,
                                      PVRSRV_SYNC_DATA **ppsSyncData,
                                      IMG_UINT32 ui32OEMFlags,
                                      IMG_HANDLE *phSwapChain,
                                      IMG_UINT32 *pui32SwapChainID)
{
	OMAPLFB_DEVINFO	*psDevInfo;
	OMAPLFB_SWAPCHAIN *psSwapChain;
	OMAPLFB_BUFFER *psBuffer;
	OMAPLFB_FLIP_ITEM *psFlipItems;
	IMG_UINT32 i;
	PVRSRV_ERROR eError = PVRSRV_OK;
	IMG_UINT32 ui32BuffersToSkip;
	
	if(!hDevice || !psDstSurfAttrib || !psSrcSurfAttrib ||
		!ppsSyncData || !phSwapChain)
	{
		ERROR_PRINTK("Invalid parameters");
		return PVRSRV_ERROR_INVALID_PARAMS;
	}
	psDevInfo = (OMAPLFB_DEVINFO*)hDevice;
	
	if (psDevInfo->sDisplayInfo.ui32MaxSwapChains == 0)
	{
		ERROR_PRINTK("Unable to operate with 0 MaxSwapChains for"
			" display %u", psDevInfo->uDeviceID);
		return PVRSRV_ERROR_NOT_SUPPORTED;
	}

	if(psDevInfo->psSwapChain != NULL)
	{
		ERROR_PRINTK("Swap chain already exists for"
			" display %u", psDevInfo->uDeviceID);
		return PVRSRV_ERROR_FLIP_CHAIN_EXISTS;
	}

	if(ui32BufferCount > psDevInfo->sDisplayInfo.ui32MaxSwapChainBuffers)
	{
		ERROR_PRINTK("Too many buffers. Trying to use %u buffers while"
			" there is only %u available for display %u",
			(unsigned int)ui32BufferCount,
			(unsigned int)psDevInfo->
			sDisplayInfo.ui32MaxSwapChainBuffers,
			psDevInfo->uDeviceID);
		return PVRSRV_ERROR_TOOMANYBUFFERS;
	}


	if ((psDevInfo->sFBInfo.ulRoundedBufferSize *
		(unsigned long)ui32BufferCount) > psDevInfo->sFBInfo.ulFBSize)
	{
		ERROR_PRINTK("Too many buffers. Trying to use %u buffers "
			"(%lu bytes each) while there is only %lu memory for"
			" display %u",
			(unsigned int)ui32BufferCount,
			psDevInfo->sFBInfo.ulRoundedBufferSize,
			psDevInfo->sFBInfo.ulFBSize,
			psDevInfo->uDeviceID);
		return PVRSRV_ERROR_TOOMANYBUFFERS;
	}

	ui32BuffersToSkip = psDevInfo->sDisplayInfo.ui32MaxSwapChainBuffers -
		ui32BufferCount;

	if((psDstSurfAttrib->pixelformat !=
		psDevInfo->sDisplayFormat.pixelformat) ||
		(psDstSurfAttrib->sDims.ui32ByteStride !=
		psDevInfo->sDisplayDim.ui32ByteStride) ||
		(psDstSurfAttrib->sDims.ui32Width !=
		psDevInfo->sDisplayDim.ui32Width) ||
		(psDstSurfAttrib->sDims.ui32Height !=
		psDevInfo->sDisplayDim.ui32Height))
	{
		ERROR_PRINTK("Destination surface attributes differ from the"
			" current framebuffer for display %u",
			psDevInfo->uDeviceID);
		return PVRSRV_ERROR_INVALID_PARAMS;
	}		

	if((psDstSurfAttrib->pixelformat !=
		psSrcSurfAttrib->pixelformat) ||
		(psDstSurfAttrib->sDims.ui32ByteStride !=
		psSrcSurfAttrib->sDims.ui32ByteStride) ||
		(psDstSurfAttrib->sDims.ui32Width !=
		psSrcSurfAttrib->sDims.ui32Width) ||
		(psDstSurfAttrib->sDims.ui32Height !=
		psSrcSurfAttrib->sDims.ui32Height))
	{
		ERROR_PRINTK("Destination surface attributes differ from the"
			" target destination surface for display %u",
			psDevInfo->uDeviceID);
		return PVRSRV_ERROR_INVALID_PARAMS;
	}		

	/* Allocate memory needed for the swap chain */
	psSwapChain = (OMAPLFB_SWAPCHAIN*)OMAPLFBAllocKernelMem(
		sizeof(OMAPLFB_SWAPCHAIN));
	if(!psSwapChain)
	{
		ERROR_PRINTK("Out of memory to allocate swap chain for"
			" display %u", psDevInfo->uDeviceID);
		return PVRSRV_ERROR_OUT_OF_MEMORY;
	}

	DEBUG_PRINTK("Creating swap chain 0x%lx for display %u",
		(unsigned long)psSwapChain, psDevInfo->uDeviceID);

	/* Allocate memory for the buffer abstraction structures */
	psBuffer = (OMAPLFB_BUFFER*)OMAPLFBAllocKernelMem(
		sizeof(OMAPLFB_BUFFER) * ui32BufferCount);
	if(!psBuffer)
	{
		ERROR_PRINTK("Out of memory to allocate the buffer"
			" abstraction structures for display %u",
			psDevInfo->uDeviceID);
		eError = PVRSRV_ERROR_OUT_OF_MEMORY;
		goto ErrorFreeSwapChain;
	}

	/* Allocate memory for the flip item abstraction structures */
	psFlipItems = (OMAPLFB_FLIP_ITEM *)OMAPLFBAllocKernelMem(
		sizeof(OMAPLFB_FLIP_ITEM) * ui32BufferCount);
	if (!psFlipItems)
	{
		ERROR_PRINTK("Out of memory to allocate the flip item"
			" abstraction structures for display %u",
			psDevInfo->uDeviceID);
		eError = PVRSRV_ERROR_OUT_OF_MEMORY;
		goto ErrorFreeBuffers;
	}

	/* Assign to the swap chain structure the initial data */
	psSwapChain->ulBufferCount = (unsigned long)ui32BufferCount;
	psSwapChain->psBuffer = psBuffer;
	psSwapChain->psFlipItems = psFlipItems;
	psSwapChain->ulInsertIndex = 0;
	psSwapChain->ulRemoveIndex = 0;
	psSwapChain->psPVRJTable = &psDevInfo->sPVRJTable;
	psSwapChain->pvDevInfo = (void*)psDevInfo;

	/*
	 * Init the workqueue (single thread, freezable and real time)
	 * and its own work for this display
	 */
	INIT_WORK(&psDevInfo->sync_display_work, OMAPLFBSyncIHandler);
	psDevInfo->sync_display_wq =
		__create_workqueue("pvr_display_sync_wq", 1, 1, 1);

	DEBUG_PRINTK("Swap chain will have %u buffers for display %u",
		(unsigned int)ui32BufferCount, psDevInfo->uDeviceID);
	/* Link the buffers available like a circular list */
	for(i=0; i<ui32BufferCount-1; i++)
	{
		psBuffer[i].psNext = &psBuffer[i+1];
	}
	psBuffer[i].psNext = &psBuffer[0];

	/* Initialize each buffer abstraction structure */
	for(i=0; i<ui32BufferCount; i++)
	{
		IMG_UINT32 ui32SwapBuffer = i + ui32BuffersToSkip;
		IMG_UINT32 ui32BufferOffset = ui32SwapBuffer *
			(IMG_UINT32)psDevInfo->sFBInfo.ulRoundedBufferSize;
		psBuffer[i].psSyncData = ppsSyncData[i];
		psBuffer[i].sSysAddr.uiAddr =
			psDevInfo->sFBInfo.sSysAddr.uiAddr +
			ui32BufferOffset;
		psBuffer[i].sCPUVAddr = psDevInfo->sFBInfo.sCPUVAddr +
			ui32BufferOffset;
		DEBUG_PRINTK("Display %u buffer index %u has physical "
			"address 0x%x",
			psDevInfo->uDeviceID,
			(unsigned int)i,
			(unsigned int)psBuffer[i].sSysAddr.uiAddr);
	}

	/* Initialize each flip item abstraction structure */
	for(i=0; i<ui32BufferCount; i++)
	{
		psFlipItems[i].bValid = OMAP_FALSE;
		psFlipItems[i].bFlipped = OMAP_FALSE;
		psFlipItems[i].bCmdCompleted = OMAP_FALSE;
	}

	mutex_lock(&psDevInfo->sSwapChainLockMutex);
	
	psDevInfo->psSwapChain = psSwapChain;
	psSwapChain->bFlushCommands = psDevInfo->bFlushCommands;
	if (psSwapChain->bFlushCommands)
		psSwapChain->ulSetFlushStateRefCount = 1;
	else
		psSwapChain->ulSetFlushStateRefCount = 0;
		
	mutex_unlock(&psDevInfo->sSwapChainLockMutex);

	if (EnableLFBEventNotification(psDevInfo)!= OMAP_OK)
	{
		WARNING_PRINTK("Couldn't enable framebuffer event"
			" notification for display %u",
			psDevInfo->uDeviceID);
		goto ErrorUnRegisterDisplayClient;
	}
	
	*phSwapChain = (IMG_HANDLE)psSwapChain;

	return PVRSRV_OK;

ErrorUnRegisterDisplayClient:
	OMAPLFBFreeKernelMem(psFlipItems);
ErrorFreeBuffers:
	OMAPLFBFreeKernelMem(psBuffer);
ErrorFreeSwapChain:
	OMAPLFBFreeKernelMem(psSwapChain);

	return eError;
}

/*
 * Destroy a swap chain. Called when a 3D application ends.
 * in: hDevice, hSwapChain
 */
static PVRSRV_ERROR DestroyDCSwapChain(IMG_HANDLE hDevice,
	IMG_HANDLE hSwapChain)
{
	OMAPLFB_DEVINFO	*psDevInfo;
	OMAPLFB_SWAPCHAIN *psSwapChain;
	OMAP_ERROR eError;
	
	if(!hDevice || !hSwapChain)
	{
		ERROR_PRINTK("Invalid parameters");
		return PVRSRV_ERROR_INVALID_PARAMS;
	}
	
	psDevInfo = (OMAPLFB_DEVINFO*)hDevice;
	psSwapChain = (OMAPLFB_SWAPCHAIN*)hSwapChain;

	if (psSwapChain != psDevInfo->psSwapChain)
	{
		ERROR_PRINTK("Swap chain handler differs from the one "
			"present in the display device pointer");
		return PVRSRV_ERROR_INVALID_PARAMS;
	}

	DEBUG_PRINTK("Destroying swap chain for display %u",
		psDevInfo->uDeviceID);

	eError = DisableLFBEventNotification(psDevInfo);
	if (eError != OMAP_OK)
	{
		WARNING_PRINTK("Couldn't disable framebuffer event "
			"notification");
	}

	mutex_lock(&psDevInfo->sSwapChainLockMutex);

	FlushInternalSyncQueue(psSwapChain);

	/*
	 * Present the buffer which is at the base of address of
	 * the framebuffer
	 */
	OMAPLFBFlip(psSwapChain,
		(unsigned long)psDevInfo->sFBInfo.sSysAddr.uiAddr);
	psDevInfo->psSwapChain = NULL;

	mutex_unlock(&psDevInfo->sSwapChainLockMutex);

	/* Destroy the workqueue */
	flush_workqueue(psDevInfo->sync_display_wq);
	destroy_workqueue(psDevInfo->sync_display_wq);

	OMAPLFBFreeKernelMem(psSwapChain->psFlipItems);
	OMAPLFBFreeKernelMem(psSwapChain->psBuffer);
	OMAPLFBFreeKernelMem(psSwapChain);

	return PVRSRV_OK;
}


/*
 * Get display buffers. These are the buffers that can be allocated
 * inside the framebuffer memory.
 * in: hDevice, hSwapChain
 * out: pui32BufferCount, phBuffer
 */
static PVRSRV_ERROR GetDCBuffers(IMG_HANDLE hDevice,
                                 IMG_HANDLE hSwapChain,
                                 IMG_UINT32 *pui32BufferCount,
                                 IMG_HANDLE *phBuffer)
{
	OMAPLFB_DEVINFO   *psDevInfo;
	OMAPLFB_SWAPCHAIN *psSwapChain;
	unsigned long      i;

	if(!hDevice || !hSwapChain || !pui32BufferCount || !phBuffer)
	{
		ERROR_PRINTK("Invalid parameters");
		return PVRSRV_ERROR_INVALID_PARAMS;
	}
	
	psDevInfo = (OMAPLFB_DEVINFO*)hDevice;
	psSwapChain = (OMAPLFB_SWAPCHAIN*)hSwapChain;
	if (psSwapChain != psDevInfo->psSwapChain)
	{
		ERROR_PRINTK("Swap chain handler differs from the one "
			"present in the display device %u pointer",
			psDevInfo->uDeviceID);
		return PVRSRV_ERROR_INVALID_PARAMS;
	}
	*pui32BufferCount = (IMG_UINT32)psSwapChain->ulBufferCount;

	for(i=0; i<psSwapChain->ulBufferCount; i++)
		phBuffer[i] = (IMG_HANDLE)&psSwapChain->psBuffer[i];

	return PVRSRV_OK;
}

/*
 * Sets the display state.
 * in: ui32State, hDevice
 */
static IMG_VOID SetDCState(IMG_HANDLE hDevice, IMG_UINT32 ui32State)
{
	OMAPLFB_DEVINFO *psDevInfo = (OMAPLFB_DEVINFO *)hDevice;

	switch (ui32State)
	{
		case DC_STATE_FLUSH_COMMANDS:
			DEBUG_PRINTK("Setting state to flush commands for"
				" display %u", psDevInfo->uDeviceID);
			SetFlushStateExternal(psDevInfo, OMAP_TRUE);
			break;
		case DC_STATE_NO_FLUSH_COMMANDS:
			DEBUG_PRINTK("Setting state to not flush commands for"
				" display %u", psDevInfo->uDeviceID);
			SetFlushStateExternal(psDevInfo, OMAP_FALSE);
			break;
		default:
			WARNING_PRINTK("Unknown command state %u for display"
				" %u", (unsigned int)ui32State,
				psDevInfo->uDeviceID);
			break;
	}
}

/*
 * Swap to display system buffer. This buffer refers to the one which
 * is that fits in the framebuffer memory.
 * in: hDevice, hSwapChain
 */
static PVRSRV_ERROR SwapToDCSystem(IMG_HANDLE hDevice,
                                   IMG_HANDLE hSwapChain)
{
	OMAPLFB_DEVINFO   *psDevInfo;
	OMAPLFB_SWAPCHAIN *psSwapChain;

	if(!hDevice || !hSwapChain)
	{
		ERROR_PRINTK("Invalid parameters");
		return PVRSRV_ERROR_INVALID_PARAMS;
	}

	psDevInfo = (OMAPLFB_DEVINFO*)hDevice;
	psSwapChain = (OMAPLFB_SWAPCHAIN*)hSwapChain;

	DEBUG_PRINTK("Executing for display %u",
		psDevInfo->uDeviceID);

	if (psSwapChain != psDevInfo->psSwapChain)
	{
		ERROR_PRINTK("Swap chain handler differs from the one "
			"present in the display device %u pointer",
			psDevInfo->uDeviceID);
		return PVRSRV_ERROR_INVALID_PARAMS;
	}
	
	mutex_lock(&psDevInfo->sSwapChainLockMutex);
	
	FlushInternalSyncQueue(psSwapChain);
	OMAPLFBFlip(psSwapChain,
		(unsigned long)psDevInfo->sFBInfo.sSysAddr.uiAddr);

	mutex_unlock(&psDevInfo->sSwapChainLockMutex);

	return PVRSRV_OK;
}

/*
 * Handles the synchronization with the display
 * in: work
 */
static void OMAPLFBSyncIHandler(struct work_struct *work)
{
	OMAPLFB_DEVINFO *psDevInfo = container_of(work, OMAPLFB_DEVINFO,
		sync_display_work);
	OMAPLFB_FLIP_ITEM *psFlipItem;
	OMAPLFB_SWAPCHAIN *psSwapChain;
	unsigned long ulMaxIndex;

	mutex_lock(&psDevInfo->sSwapChainLockMutex);

	psSwapChain = psDevInfo->psSwapChain;
	if (!psSwapChain || psSwapChain->bFlushCommands)
		goto ExitUnlock;

	psFlipItem = &psSwapChain->psFlipItems[psSwapChain->ulRemoveIndex];
	ulMaxIndex = psSwapChain->ulBufferCount - 1;

	/* Iterate through the flip items and flip them if necessary */
	while (psFlipItem->bValid) {
		/* Update display */
		OMAPLFBPresentSync(psDevInfo, psFlipItem);

		psFlipItem->ulSwapInterval--;
		psFlipItem->bFlipped = OMAP_TRUE;

		if (psFlipItem->ulSwapInterval == 0) {

			/* Mark the flip item as completed to reuse it */
			psSwapChain->ulRemoveIndex++;
			if (psSwapChain->ulRemoveIndex > ulMaxIndex)
				psSwapChain->ulRemoveIndex = 0;
			psFlipItem->bCmdCompleted = OMAP_FALSE;
			psFlipItem->bFlipped = OMAP_FALSE;
			psFlipItem->bValid = OMAP_FALSE;

			psSwapChain->psPVRJTable->pfnPVRSRVCmdComplete(
				(IMG_HANDLE)psFlipItem->hCmdComplete,
				IMG_TRUE);
			psFlipItem->bCmdCompleted = OMAP_TRUE;
		} else {
			/*
			 * Here the swap interval is not zero yet
			 * we need to schedule another work until
			 * it reaches zero
			 */
			queue_work(psDevInfo->sync_display_wq,
				&psDevInfo->sync_display_work);
			break;
		}

		psFlipItem =
			&psSwapChain->psFlipItems[psSwapChain->ulRemoveIndex];
	}

ExitUnlock:
	mutex_unlock(&psDevInfo->sSwapChainLockMutex);
}

/*
 * Performs a flip. This function takes the necessary steps to present
 * the buffer to be flipped in the display.
 * in: hCmdCookie, ui32DataSize, pvData
 */
static IMG_BOOL ProcessFlip(IMG_HANDLE  hCmdCookie,
                            IMG_UINT32  ui32DataSize,
                            IMG_VOID   *pvData)
{
	DISPLAYCLASS_FLIP_COMMAND *psFlipCmd;
	OMAPLFB_DEVINFO *psDevInfo;
	OMAPLFB_BUFFER *psBuffer;
	OMAPLFB_SWAPCHAIN *psSwapChain;
#if defined(SYS_USING_INTERRUPTS)
	OMAPLFB_FLIP_ITEM* psFlipItem;
	unsigned long ulMaxIndex;
#endif

	if(!hCmdCookie || !pvData)
	{
		WARNING_PRINTK("Ignoring call with NULL parameters");
		return IMG_FALSE;
	}
	
	psFlipCmd = (DISPLAYCLASS_FLIP_COMMAND*)pvData;

	if (psFlipCmd == IMG_NULL ||
		sizeof(DISPLAYCLASS_FLIP_COMMAND) != ui32DataSize)
	{
		WARNING_PRINTK("NULL command or command data size is wrong");
		return IMG_FALSE;
	}
	
	psDevInfo = (OMAPLFB_DEVINFO*)psFlipCmd->hExtDevice;
	psBuffer = (OMAPLFB_BUFFER*)psFlipCmd->hExtBuffer;
	psSwapChain = (OMAPLFB_SWAPCHAIN*) psFlipCmd->hExtSwapChain;

	mutex_lock(&psDevInfo->sSwapChainLockMutex);

	if (psDevInfo->bDeviceSuspended)
	{
		/* If is suspended then assume the commands are completed */
		psSwapChain->psPVRJTable->pfnPVRSRVCmdComplete(
			hCmdCookie, IMG_TRUE);
		goto ExitTrueUnlock;
	}

#if defined(SYS_USING_INTERRUPTS)

	if( psFlipCmd->ui32SwapInterval == 0 ||
		psDevInfo->ignore_sync ||
		psSwapChain->bFlushCommands == OMAP_TRUE)
	{
#endif
		OMAPLFBFlip(psSwapChain,
			(unsigned long)psBuffer->sSysAddr.uiAddr);
		psSwapChain->psPVRJTable->pfnPVRSRVCmdComplete(
			hCmdCookie, IMG_TRUE);

#if defined(SYS_USING_INTERRUPTS)
		goto ExitTrueUnlock;
	}

	psFlipItem = &psSwapChain->psFlipItems[psSwapChain->ulInsertIndex];

	if(psFlipItem->bValid == OMAP_FALSE)
	{
		/* Mark the flip item as not flipped */
		ulMaxIndex = psSwapChain->ulBufferCount - 1;
		psFlipItem->bFlipped = OMAP_FALSE;

		/*
		 * The buffer is queued here, must be consumed by the workqueue
		 */
		psFlipItem->hCmdComplete = (OMAP_HANDLE)hCmdCookie;
		psFlipItem->ulSwapInterval =
			(unsigned long)psFlipCmd->ui32SwapInterval;
		psFlipItem->sSysAddr = &psBuffer->sSysAddr;
		psFlipItem->bValid = OMAP_TRUE;

		psSwapChain->ulInsertIndex++;
		if(psSwapChain->ulInsertIndex > ulMaxIndex)
			psSwapChain->ulInsertIndex = 0;

		/* Give work to the workqueue to sync with the display */
		queue_work(psDevInfo->sync_display_wq, &psDevInfo->sync_display_work);

		goto ExitTrueUnlock;
	} else
		WARNING_PRINTK("Dropping frame! %p index %lu is the flip "
			"queue full?", psFlipItem, psSwapChain->ulInsertIndex);

	mutex_unlock(&psDevInfo->sSwapChainLockMutex);
	return IMG_FALSE;
#endif

ExitTrueUnlock:
	mutex_unlock(&psDevInfo->sSwapChainLockMutex);
	return IMG_TRUE;
}

#if defined(LDM_PLATFORM)

/*
 *  Function called when the driver must suspend
 */
void OMAPLFBDriverSuspend(void)
{
	OMAPLFB_DEVINFO *psDevInfo;
	int i;

	if(!pDisplayDevices)
		return;

	for(i = 0; i < FRAMEBUFFER_COUNT; i++)
	{
		psDevInfo = &pDisplayDevices[i];

		mutex_lock(&psDevInfo->sSwapChainLockMutex);

		if (psDevInfo->bDeviceSuspended)
		{
			mutex_unlock(&psDevInfo->sSwapChainLockMutex);
			continue;
		}

		psDevInfo->bDeviceSuspended = OMAP_TRUE;
		SetFlushStateInternalNoLock(psDevInfo, OMAP_TRUE);

		mutex_unlock(&psDevInfo->sSwapChainLockMutex);
	}
}

/*
 *  Function called when the driver must resume
 */
void OMAPLFBDriverResume(void)
{
	OMAPLFB_DEVINFO *psDevInfo;
	int i;

	if(!pDisplayDevices)
		return;

	for(i = 0; i < FRAMEBUFFER_COUNT; i++)
	{
		psDevInfo = &pDisplayDevices[i];

		mutex_lock(&psDevInfo->sSwapChainLockMutex);

		if (!psDevInfo->bDeviceSuspended)
		{
			mutex_unlock(&psDevInfo->sSwapChainLockMutex);
			continue;
		}

		SetFlushStateInternalNoLock(psDevInfo, OMAP_FALSE);
		psDevInfo->bDeviceSuspended = OMAP_FALSE;

		mutex_unlock(&psDevInfo->sSwapChainLockMutex);
	}
}
#endif /* defined(LDM_PLATFORM) */

/*
 * Frees the kernel framebuffer
 * in: psDevInfo
 */
static void DeInitDev(OMAPLFB_DEVINFO *psDevInfo)
{
	struct fb_info *psLINFBInfo = psDevInfo->psLINFBInfo;
	struct module *psLINFBOwner;

	acquire_console_sem();
	psLINFBOwner = psLINFBInfo->fbops->owner;

	if (psLINFBInfo->fbops->fb_release != NULL)
		(void) psLINFBInfo->fbops->fb_release(psLINFBInfo, 0);

	module_put(psLINFBOwner);

	release_console_sem();
}

/*
 *  Deinitialization routine for the 3rd party display driver
 */
OMAP_ERROR OMAPLFBDeinit(void)
{
	OMAPLFB_DEVINFO *psDevInfo;
	PVRSRV_DC_DISP2SRV_KMJTABLE *psJTable;
	int i;

	DEBUG_PRINTK("Deinitializing 3rd party display driver");

	if(!pDisplayDevices)
		return OMAP_OK;

	for(i = 0; i < FRAMEBUFFER_COUNT; i++)
	{
		psDevInfo = &pDisplayDevices[i];

		/* Remove the ProcessFlip command callback */
		psJTable = &psDevInfo->sPVRJTable;
		if (psDevInfo->sPVRJTable.pfnPVRSRVRemoveCmdProcList(
			psDevInfo->uDeviceID,
			OMAPLFB_COMMAND_COUNT) != PVRSRV_OK)
		{
			ERROR_PRINTK("Unable to remove callback for "
				"ProcessFlip command for display %u",
				psDevInfo->uDeviceID);
			return OMAP_ERROR_GENERIC;
		}

		/* Remove the display device from services */
		if (psJTable->pfnPVRSRVRemoveDCDevice(
			psDevInfo->uDeviceID) != PVRSRV_OK)
		{
			ERROR_PRINTK("Unable to remove the display %u "
				"from services", psDevInfo->uDeviceID);
			return OMAP_ERROR_GENERIC;
		}

		DeInitDev(psDevInfo);
	}

	OMAPLFBFreeKernelMem(pDisplayDevices);

	return OMAP_OK;
}

/*
 * Extracts the framebuffer data from the kernel driver
 * in: psDevInfo
 */
static OMAP_ERROR InitDev(OMAPLFB_DEVINFO *psDevInfo, int fb_idx)
{
	struct fb_info *psLINFBInfo;
	struct module *psLINFBOwner;
	OMAPLFB_FBINFO *psPVRFBInfo = &psDevInfo->sFBInfo;
	unsigned long FBSize;
	int buffers_available;

	/* Check if the framebuffer index to use is valid */
	if (fb_idx < 0 || fb_idx >= num_registered_fb)
	{
		ERROR_PRINTK("Framebuffer index %i out of range, "
			"only %i available", fb_idx, num_registered_fb);
		return OMAP_ERROR_INVALID_DEVICE;
	}

	/* Get the framebuffer from the kernel */
	if (!registered_fb[fb_idx])
	{
		ERROR_PRINTK("Framebuffer index %i is null", fb_idx);
		return OMAP_ERROR_INVALID_DEVICE;
	}

	psLINFBInfo = registered_fb[fb_idx];

	/* Check the framebuffer width and height are valid */
	if(psLINFBInfo->var.xres <= 0 || psLINFBInfo->var.yres <= 0)
	{
		ERROR_PRINTK("Framebuffer %i has an invalid state, "
			"width and height are %u,%u", fb_idx,
			psLINFBInfo->var.xres, psLINFBInfo->var.yres);
		return OMAP_ERROR_INVALID_DEVICE;
	}

	/* Configure framebuffer for flipping and desired bpp */
	psLINFBInfo->var.yres_virtual = psLINFBInfo->var.yres *
		MAX_BUFFERS_FLIPPING;

	if(DESIRED_BPP != 0){
		psLINFBInfo->var.bits_per_pixel = DESIRED_BPP;
		if(DESIRED_BPP == 16){
			psLINFBInfo->var.red.offset     = 11;
			psLINFBInfo->var.red.length     = 5;
			psLINFBInfo->var.green.offset   = 5;
			psLINFBInfo->var.green.length   = 6;
			psLINFBInfo->var.blue.offset    = 0;
			psLINFBInfo->var.blue.length    = 5;
			psLINFBInfo->var.transp.offset  = 0;
			psLINFBInfo->var.transp.length  = 0;
		}
		else if(DESIRED_BPP == 32)
		{
			psLINFBInfo->var.red.offset     = 16;
			psLINFBInfo->var.red.length     = 8;
			psLINFBInfo->var.green.offset   = 8;
			psLINFBInfo->var.green.length   = 8;
			psLINFBInfo->var.blue.offset    = 0;
			psLINFBInfo->var.blue.length    = 8;
			psLINFBInfo->var.transp.offset  = 0;
			psLINFBInfo->var.transp.length  = 0;
		}
		else
			WARNING_PRINTK("Unknown bits per pixel format %i",
				DESIRED_BPP);
	}
	acquire_console_sem();

	FBSize = (psLINFBInfo->screen_size) != 0 ?
		psLINFBInfo->screen_size : psLINFBInfo->fix.smem_len;
	psPVRFBInfo->sSysAddr.uiAddr = psLINFBInfo->fix.smem_start;
	psPVRFBInfo->sCPUVAddr = psLINFBInfo->screen_base;
	psPVRFBInfo->ulWidth = psLINFBInfo->var.xres;
	psPVRFBInfo->ulHeight = psLINFBInfo->var.yres;
	psPVRFBInfo->ulByteStride = psLINFBInfo->fix.line_length;
	psPVRFBInfo->ulFBSize = FBSize;
	psPVRFBInfo->ulBufferSize =
		psPVRFBInfo->ulHeight * psPVRFBInfo->ulByteStride;

	/* Calculate the buffers according to the flipping technique */
#if defined(FLIP_TECHNIQUE_FRAMEBUFFER)
	psLINFBInfo->var.activate = FB_ACTIVATE_FORCE;
	fb_set_var(psLINFBInfo, &psLINFBInfo->var);
	buffers_available =
		psLINFBInfo->var.yres_virtual / psLINFBInfo->var.yres;

#elif defined(FLIP_TECHNIQUE_OVERLAY)
	buffers_available =
		psPVRFBInfo->ulFBSize / psPVRFBInfo->ulBufferSize;

#else
#error No flipping technique selected, please define \
	FLIP_TECHNIQUE_FRAMEBUFFER or FLIP_TECHNIQUE_OVERLAY
#endif

	if(buffers_available <= 1)
	{
		/*
		 * Flipping is not supported, return the framebuffer to
		 * its original state
		 */
		psLINFBInfo->var.yres_virtual = psLINFBInfo->var.yres;
		psLINFBInfo->var.activate = FB_ACTIVATE_FORCE;
		fb_set_var(psLINFBInfo, &psLINFBInfo->var);
		buffers_available = 1;
	}
	psDevInfo->sDisplayInfo.ui32MaxSwapChainBuffers = buffers_available;

	psLINFBOwner = psLINFBInfo->fbops->owner;
	if (!try_module_get(psLINFBOwner))
	{
		ERROR_PRINTK("Couldn't get framebuffer module");
		release_console_sem();
		return OMAP_ERROR_GENERIC;
	}

	if (psLINFBInfo->fbops->fb_open != NULL)
	{
		if (psLINFBInfo->fbops->fb_open(psLINFBInfo, 0))
		{
			ERROR_PRINTK("Couldn't open framebuffer with"
				" index %d", fb_idx);
			module_put(psLINFBOwner);
			release_console_sem();
			return OMAP_ERROR_GENERIC;
		}
	}
	psDevInfo->psLINFBInfo = psLINFBInfo;

	/* Extract the needed data from the framebuffer structures */
	FBSize = (psLINFBInfo->screen_size) != 0 ?
		psLINFBInfo->screen_size : psLINFBInfo->fix.smem_len;
	DEBUG_PRINTK("Framebuffer index %d information:", fb_idx);
	DEBUG_PRINTK("*Physical address: 0x%lx",
		psLINFBInfo->fix.smem_start);
	DEBUG_PRINTK("*Virtual address: 0x%lx",
		(unsigned long)psLINFBInfo->screen_base);
	DEBUG_PRINTK("*Size (bytes): %lu",FBSize);
	DEBUG_PRINTK("*Width, height: %u,%u",
		psLINFBInfo->var.xres, psLINFBInfo->var.yres);
	DEBUG_PRINTK("*Virtual width, height: %u,%u",
		psLINFBInfo->var.xres_virtual, psLINFBInfo->var.yres_virtual);
	DEBUG_PRINTK("*Rotation: %u", psLINFBInfo->var.rotate);
	DEBUG_PRINTK("*Stride (bytes): %u",
		(unsigned int)psLINFBInfo->fix.line_length);

#ifdef CONFIG_TILER_OMAP
	/* If TILER is being used, use correct physical stride and FB size */
	if ((psPVRFBInfo->sSysAddr.uiAddr >= TILER_MIN_PADDR) &&
		(psPVRFBInfo->sSysAddr.uiAddr <= TILER_MAX_PADDR)) {
		unsigned long max_rows;
		u32 tiler_naddr;
		/* Get the number of maximum pixel rows */
		max_rows = psPVRFBInfo->ulFBSize / psPVRFBInfo->ulByteStride;
		/* Get the physical stride according to the TILER container */
		tiler_naddr = tiler_get_natural_addr(
			(void *)psPVRFBInfo->sSysAddr.uiAddr);
		psPVRFBInfo->ulByteStride = tiler_stride(tiler_naddr);
		/* Calculate the whole TILER region to map in bytes */
		psPVRFBInfo->ulFBSize = max_rows * psPVRFBInfo->ulByteStride;
		/* Re-calculate buffer size with previous stride */
		psPVRFBInfo->ulBufferSize =
			psPVRFBInfo->ulHeight * psPVRFBInfo->ulByteStride;
	}
#endif

	/* Get physical display size for DPI calculation */
	if (psLINFBInfo->var.width < 0 || psLINFBInfo->var.height < 0) {
		psDevInfo->sDisplayInfo.ui32PhysicalWidthmm = 0;
		psDevInfo->sDisplayInfo.ui32PhysicalHeightmm = 0;
	} else {
		psDevInfo->sDisplayInfo.ui32PhysicalWidthmm =
			psLINFBInfo->var.width;
		psDevInfo->sDisplayInfo.ui32PhysicalHeightmm =
			psLINFBInfo->var.height;
	}

	/* XXX: Page aligning with 16bpp causes the
	 * position of framebuffer address to look in the wrong place.
	 */
	psPVRFBInfo->ulRoundedBufferSize =
			OMAPLFB_PAGE_ROUNDUP(psPVRFBInfo->ulBufferSize);

	psDevInfo->sFBInfo.sSysAddr.uiAddr = psPVRFBInfo->sSysAddr.uiAddr;
	psDevInfo->sFBInfo.sCPUVAddr = psPVRFBInfo->sCPUVAddr;

	/* Get the current bits per pixel configured in the framebuffer */
	DEBUG_PRINTK("*Bits per pixel: %d", psLINFBInfo->var.bits_per_pixel);
	if(psLINFBInfo->var.bits_per_pixel == 16)
	{
		if((psLINFBInfo->var.red.length == 5) &&
			(psLINFBInfo->var.green.length == 6) && 
			(psLINFBInfo->var.blue.length == 5) && 
			(psLINFBInfo->var.red.offset == 11) &&
			(psLINFBInfo->var.green.offset == 5) && 
			(psLINFBInfo->var.blue.offset == 0) && 
			(psLINFBInfo->var.red.msb_right == 0))
		{
			DEBUG_PRINTK("*Format: RGB565");
			psPVRFBInfo->ePixelFormat = PVRSRV_PIXEL_FORMAT_RGB565;
		}
		else
			WARNING_PRINTK("*Format: Unknown framebuffer"
				" format");
	}
	else if(psLINFBInfo->var.bits_per_pixel == 32)
	{
		if((psLINFBInfo->var.red.length == 8) &&
			(psLINFBInfo->var.green.length == 8) && 
			(psLINFBInfo->var.blue.length == 8) && 
			(psLINFBInfo->var.red.offset == 16) &&
			(psLINFBInfo->var.green.offset == 8) && 
			(psLINFBInfo->var.blue.offset == 0) && 
			(psLINFBInfo->var.red.msb_right == 0))
		{
			psPVRFBInfo->ePixelFormat =
				PVRSRV_PIXEL_FORMAT_ARGB8888;
			DEBUG_PRINTK("*Format: ARGB8888");
		}
		else
			WARNING_PRINTK("*Format: Unknown framebuffer"
				"format");
	}	
	else
		WARNING_PRINTK("*Format: Unknown framebuffer format");

	release_console_sem();
	return OMAP_OK;
}

/*
 *  Initialization routine for the 3rd party display driver
 */
OMAP_ERROR OMAPLFBInit(struct omaplfb_device *omaplfb_dev)
{
	OMAPLFB_DEVINFO *psDevInfo;
	PFN_CMD_PROC pfnCmdProcList[OMAPLFB_COMMAND_COUNT];
	IMG_UINT32 aui32SyncCountList[OMAPLFB_COMMAND_COUNT][2];
	int i;

	DEBUG_PRINTK("Initializing 3rd party display driver");
	DEBUG_PRINTK("Found %u framebuffers", FRAMEBUFFER_COUNT);

#if defined(REQUIRES_TWO_FRAMEBUFFERS)
	/*
	 * Fail hard if there isn't at least two framebuffers available
	 */
	if(FRAMEBUFFER_COUNT < 2)
	{
		ERROR_PRINTK("Driver needs at least two framebuffers");
		return OMAP_ERROR_INIT_FAILURE;
	}
#endif

	/*
	 * Obtain the function pointer for the jump table from
	 * services to fill it with the function pointers that we want
	 */
	if(OMAPLFBGetLibFuncAddr ("PVRGetDisplayClassJTable",
		&pfnGetPVRJTable) != OMAP_OK)
	{
		ERROR_PRINTK("Unable to get the function to get the"
			" jump table display->services");
		return OMAP_ERROR_INIT_FAILURE;
	}

	/*
	 * Allocate the display device structures, one per framebuffer
	 */
	pDisplayDevices = (OMAPLFB_DEVINFO *)OMAPLFBAllocKernelMem(
			sizeof(OMAPLFB_DEVINFO) * FRAMEBUFFER_COUNT);
	if(!pDisplayDevices)
	{
		ERROR_PRINTK("Out of memory");
		return OMAP_ERROR_OUT_OF_MEMORY;
	}
	memset(pDisplayDevices, 0, sizeof(OMAPLFB_DEVINFO) *
		FRAMEBUFFER_COUNT);
	omaplfb_dev->display_info_list = pDisplayDevices;
	omaplfb_dev->display_count = FRAMEBUFFER_COUNT;

	/*
	 * Initialize each display device
	 */
	for (i = FRAMEBUFFER_COUNT - 1; i >= 0; i--)
	{
		DEBUG_PRINTK("-> Initializing display device %i", i);
		/*
		 * Here we get the framebuffer data for each display device
		 * and check for error
		 */
		if(InitDev(&pDisplayDevices[i], i) != OMAP_OK)
		{
			ERROR_PRINTK("Unable to initialize display "
				"device %i",i);
			OMAPLFBFreeKernelMem(pDisplayDevices);
			pDisplayDevices = NULL;
			return OMAP_ERROR_INIT_FAILURE;
		}

    		/*
		 * Populate each display device structure
		*/
		DEBUG_PRINTK("-> Populating display device %i", i);
		psDevInfo = &pDisplayDevices[i];

		if(!(*pfnGetPVRJTable)(&psDevInfo->sPVRJTable))
		{
			ERROR_PRINTK("Unable to get the jump table"
				" display->services");
			return OMAP_ERROR_INIT_FAILURE;
		}

		mutex_init(&psDevInfo->sSwapChainLockMutex);

		psDevInfo->psSwapChain = 0;
		psDevInfo->bFlushCommands = OMAP_FALSE;
		psDevInfo->bDeviceSuspended = OMAP_FALSE;
		psDevInfo->ignore_sync = OMAP_FALSE;

		if(psDevInfo->sDisplayInfo.ui32MaxSwapChainBuffers > 1)
		{
			if(MAX_BUFFERS_FLIPPING == 1)
			{
				DEBUG_PRINTK("Flipping support is possible"
					" but you decided not to use it, "
					"no swap chain will be created");
			}

			DEBUG_PRINTK("Flipping support");
			if(psDevInfo->sDisplayInfo.ui32MaxSwapChainBuffers >
				MAX_BUFFERS_FLIPPING)
			psDevInfo->sDisplayInfo.ui32MaxSwapChainBuffers =
				MAX_BUFFERS_FLIPPING;
		}
		else
		{
			DEBUG_PRINTK("Flipping not supported, no swap chain"
				" will be created");
		}

		if (psDevInfo->sDisplayInfo.ui32MaxSwapChainBuffers == 0)
		{
			psDevInfo->sDisplayInfo.ui32MaxSwapChains = 0;
			psDevInfo->sDisplayInfo.ui32MaxSwapInterval = 0;
		}
		else
		{
			psDevInfo->sDisplayInfo.ui32MaxSwapChains = 1;
			psDevInfo->sDisplayInfo.ui32MaxSwapInterval = 3;
		}
		psDevInfo->sDisplayInfo.ui32MinSwapInterval = 0;

		/* Get the display and framebuffer needed info */
		strncpy(psDevInfo->sDisplayInfo.szDisplayName,
			DISPLAY_DEVICE_NAME, MAX_DISPLAY_NAME_SIZE);
		psDevInfo->sDisplayFormat.pixelformat =
			psDevInfo->sFBInfo.ePixelFormat;
		psDevInfo->sDisplayDim.ui32Width =
			(IMG_UINT32)psDevInfo->sFBInfo.ulWidth;
		psDevInfo->sDisplayDim.ui32Height =
			(IMG_UINT32)psDevInfo->sFBInfo.ulHeight;
		psDevInfo->sDisplayDim.ui32ByteStride =
			(IMG_UINT32)psDevInfo->sFBInfo.ulByteStride;
		psDevInfo->sSystemBuffer.sSysAddr =
			psDevInfo->sFBInfo.sSysAddr;
		psDevInfo->sSystemBuffer.sCPUVAddr =
			psDevInfo->sFBInfo.sCPUVAddr;
		psDevInfo->sSystemBuffer.ulBufferSize =
			psDevInfo->sFBInfo.ulRoundedBufferSize;
		DEBUG_PRINTK("Buffers available: %u (%lu bytes per buffer)",
			psDevInfo->sDisplayInfo.ui32MaxSwapChainBuffers,
			psDevInfo->sFBInfo.ulBufferSize);

		/* Populate the function table that services will use */
		psDevInfo->sDCJTable.ui32TableSize =
			sizeof(PVRSRV_DC_SRV2DISP_KMJTABLE);
		psDevInfo->sDCJTable.pfnOpenDCDevice = OpenDCDevice;
		psDevInfo->sDCJTable.pfnCloseDCDevice = CloseDCDevice;
		psDevInfo->sDCJTable.pfnEnumDCFormats = EnumDCFormats;
		psDevInfo->sDCJTable.pfnEnumDCDims = EnumDCDims;
		psDevInfo->sDCJTable.pfnGetDCSystemBuffer = GetDCSystemBuffer;
		psDevInfo->sDCJTable.pfnGetDCInfo = GetDCInfo;
		psDevInfo->sDCJTable.pfnGetBufferAddr = GetDCBufferAddr;
		psDevInfo->sDCJTable.pfnCreateDCSwapChain = CreateDCSwapChain;
		psDevInfo->sDCJTable.pfnDestroyDCSwapChain =
			DestroyDCSwapChain;
		psDevInfo->sDCJTable.pfnSetDCDstRect = SetDCDstRect;
		psDevInfo->sDCJTable.pfnSetDCSrcRect = SetDCSrcRect;
		psDevInfo->sDCJTable.pfnSetDCDstColourKey = SetDCDstColourKey;
		psDevInfo->sDCJTable.pfnSetDCSrcColourKey = SetDCSrcColourKey;
		psDevInfo->sDCJTable.pfnGetDCBuffers = GetDCBuffers;
		psDevInfo->sDCJTable.pfnSwapToDCBuffer = SwapToDCBuffer;
		psDevInfo->sDCJTable.pfnSwapToDCSystem = SwapToDCSystem;
		psDevInfo->sDCJTable.pfnSetDCState = SetDCState;

		/* Register the display device */
		if(psDevInfo->sPVRJTable.pfnPVRSRVRegisterDCDevice(
			&psDevInfo->sDCJTable,
			&psDevInfo->uDeviceID) != PVRSRV_OK)
		{
			ERROR_PRINTK("Unable to register the jump table"
				" services->display");
			return OMAP_ERROR_DEVICE_REGISTER_FAILED;
		}

		DEBUG_PRINTK("Display device %i registered with id %u",
			i, psDevInfo->uDeviceID);

		/*
		 * Register the ProcessFlip function to notify when a frame is
		 * ready to be flipped
		 */
		pfnCmdProcList[DC_FLIP_COMMAND] = ProcessFlip;
		aui32SyncCountList[DC_FLIP_COMMAND][0] = 0;
		aui32SyncCountList[DC_FLIP_COMMAND][1] = 2;
		if (psDevInfo->sPVRJTable.pfnPVRSRVRegisterCmdProcList(
			psDevInfo->uDeviceID, &pfnCmdProcList[0],
			aui32SyncCountList, OMAPLFB_COMMAND_COUNT) != PVRSRV_OK)
		{
			ERROR_PRINTK("Unable to register callback for "
				"ProcessFlip command");
			return OMAP_ERROR_CANT_REGISTER_CALLBACK;
		}

	}
	return OMAP_OK;
}