xf86RamDac.c revision 706f2543
1/*
2 * Copyright 1998 by Alan Hourihane, Wigan, England.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Alan Hourihane not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission.  Alan Hourihane makes no representations
11 * about the suitability of this software for any purpose.  It is provided
12 * "as is" without express or implied warranty.
13 *
14 * ALAN HOURIHANE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL ALAN HOURIHANE BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
21 *
22 * Authors:  Alan Hourihane, <alanh@fairlite.demon.co.uk>
23 *
24 * Generic RAMDAC access routines.
25 */
26
27#ifdef HAVE_XORG_CONFIG_H
28#include <xorg-config.h>
29#endif
30
31#include "xf86.h"
32#include "xf86_OSproc.h"
33
34#include "xf86RamDacPriv.h"
35
36int RamDacHWPrivateIndex = -1;
37int RamDacScreenPrivateIndex = -1;
38
39RamDacRecPtr
40RamDacCreateInfoRec(void)
41{
42    RamDacRecPtr infoRec;
43
44    infoRec = calloc(1, sizeof(RamDacRec));
45
46    return infoRec;
47}
48
49RamDacHelperRecPtr
50RamDacHelperCreateInfoRec(void)
51{
52    RamDacHelperRecPtr infoRec;
53
54    infoRec = calloc(1, sizeof(RamDacHelperRec));
55
56    return infoRec;
57}
58
59void
60RamDacDestroyInfoRec(RamDacRecPtr infoRec)
61{
62    free(infoRec);
63}
64
65void
66RamDacHelperDestroyInfoRec(RamDacHelperRecPtr infoRec)
67{
68    free(infoRec);
69}
70
71Bool
72RamDacInit(ScrnInfoPtr pScrn, RamDacRecPtr ramdacPriv)
73{
74    RamDacScreenRecPtr ramdacScrPtr;
75
76    /*
77     * make sure the RamDacRec is allocated
78     */
79    if (!RamDacGetRec(pScrn))
80	return FALSE;
81    ramdacScrPtr =
82	((RamDacScreenRecPtr) (pScrn)->privates[RamDacGetScreenIndex()].ptr);
83    ramdacScrPtr->RamDacRec = ramdacPriv;
84
85    return TRUE;
86}
87
88void
89RamDacGetRecPrivate(void)
90{
91    if (RamDacHWPrivateIndex < 0)
92	RamDacHWPrivateIndex = xf86AllocateScrnInfoPrivateIndex();
93    if (RamDacScreenPrivateIndex < 0)
94	RamDacScreenPrivateIndex = xf86AllocateScrnInfoPrivateIndex();
95    return;
96}
97
98Bool
99RamDacGetRec(ScrnInfoPtr scrp)
100{
101    RamDacGetRecPrivate();
102    /*
103     * New privates are always set to NULL, so we can check if the allocation
104     * has already been done.
105     */
106    if (scrp->privates[RamDacHWPrivateIndex].ptr != NULL)
107	return TRUE;
108    if (scrp->privates[RamDacScreenPrivateIndex].ptr != NULL)
109	return TRUE;
110
111    scrp->privates[RamDacHWPrivateIndex].ptr =
112					xnfcalloc(sizeof(RamDacHWRec), 1);
113    scrp->privates[RamDacScreenPrivateIndex].ptr =
114					xnfcalloc(sizeof(RamDacScreenRec), 1);
115
116    return TRUE;
117}
118
119void
120RamDacFreeRec(ScrnInfoPtr pScrn)
121{
122    RamDacHWRecPtr ramdacHWPtr;
123    RamDacScreenRecPtr ramdacScrPtr;
124
125    if (RamDacHWPrivateIndex < 0)
126	return;
127
128    if (RamDacScreenPrivateIndex < 0)
129	return;
130
131    ramdacHWPtr = RAMDACHWPTR(pScrn);
132    ramdacScrPtr = ((RamDacScreenRecPtr)
133				(pScrn)->privates[RamDacGetScreenIndex()].ptr);
134
135    free(ramdacHWPtr);
136    ramdacHWPtr = NULL;
137
138    free(ramdacScrPtr);
139    ramdacScrPtr = NULL;
140}
141
142int
143RamDacGetHWIndex(void)
144{
145    return RamDacHWPrivateIndex;
146}
147
148int
149RamDacGetScreenIndex(void)
150{
151    return RamDacScreenPrivateIndex;
152}
153