summaryrefslogtreecommitdiffstats
path: root/drivers/video/fbcmap.c
blob: ce86225958f4599e9d96f6c783a379c4ff90b569 (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
/*
 *  linux/drivers/video/fbcmap.c -- Colormap handling for frame buffer devices
 *
 *	Created 15 Jun 1997 by Geert Uytterhoeven
 *
 *	2001 - Documented with DocBook
 *	- Brad Douglas <brad@neruo.com>
 *
 *  This file is subject to the terms and conditions of the GNU General Public
 *  License.  See the file COPYING in the main directory of this archive for
 *  more details.
 */

#include <linux/string.h>
#include <linux/module.h>
#include <linux/tty.h>
#include <linux/fb.h>
#include <linux/slab.h>

#include <asm/uaccess.h>

static u16 red2[] = {
    0x0000, 0xaaaa
};
static u16 green2[] = {
    0x0000, 0xaaaa
};
static u16 blue2[] = {
    0x0000, 0xaaaa
};

static u16 red4[] = {
    0x0000, 0xaaaa, 0x5555, 0xffff
};
static u16 green4[] = {
    0x0000, 0xaaaa, 0x5555, 0xffff
};
static u16 blue4[] = {
    0x0000, 0xaaaa, 0x5555, 0xffff
};

static u16 red8[] = {
    0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa
};
static u16 green8[] = {
    0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa
};
static u16 blue8[] = {
    0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa
};

static u16 red16[] = {
    0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa,
    0x5555, 0x5555, 0x5555, 0x5555, 0xffff, 0xffff, 0xffff, 0xffff
};
static u16 green16[] = {
    0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa,
    0x5555, 0x5555, 0xffff, 0xffff, 0x5555, 0x5555, 0xffff, 0xffff
};
static u16 blue16[] = {
    0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa,
    0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff
};

static struct fb_cmap default_2_colors = {
    0, 2, red2, green2, blue2, NULL
};
static struct fb_cmap default_8_colors = {
    0, 8, red8, green8, blue8, NULL
};
static struct fb_cmap default_4_colors = {
    0, 4, red4, green4, blue4, NULL
};
static struct fb_cmap default_16_colors = {
    0, 16, red16, green16, blue16, NULL
};


/**
 *	fb_alloc_cmap - allocate a colormap
 *	@cmap: frame buffer colormap structure
 *	@len: length of @cmap
 *	@transp: boolean, 1 if there is transparency, 0 otherwise
 *
 *	Allocates memory for a colormap @cmap.  @len is the
 *	number of entries in the palette.
 *
 *	Returns -1 errno on error, or zero on success.
 *
 */

int fb_alloc_cmap(struct fb_cmap *cmap, int len, int transp)
{
    int size = len*sizeof(u16);

    if (cmap->len != len) {
	if (cmap->red)
	    kfree(cmap->red);
	if (cmap->green)
	    kfree(cmap->green);
	if (cmap->blue)
	    kfree(cmap->blue);
	if (cmap->transp)
	    kfree(cmap->transp);
	cmap->red = cmap->green = cmap->blue = cmap->transp = NULL;
	cmap->len = 0;
	if (!len)
	    return 0;
	if (!(cmap->red = kmalloc(size, GFP_ATOMIC)))
	    return -1;
	if (!(cmap->green = kmalloc(size, GFP_ATOMIC)))
	    return -1;
	if (!(cmap->blue = kmalloc(size, GFP_ATOMIC)))
	    return -1;
	if (transp) {
	    if (!(cmap->transp = kmalloc(size, GFP_ATOMIC)))
		return -1;
	} else
	    cmap->transp = NULL;
    }
    cmap->start = 0;
    cmap->len = len;
    fb_copy_cmap(fb_default_cmap(len), cmap, 0);
    return 0;
}


/**
 *	fb_copy_cmap - copy a colormap
 *	@from: frame buffer colormap structure
 *	@to: frame buffer colormap structure
 *	@fsfromto: determine copy method
 *
 *	Copy contents of colormap from @from to @to.
 *
 *	@fsfromto accepts the following integer parameters:
 *	0: memcpy function
 *	1: copy_from_user() function to copy from userspace
 *	2: copy_to_user() function to copy to userspace
 *
 */

void fb_copy_cmap(struct fb_cmap *from, struct fb_cmap *to, int fsfromto)
{
    int size;
    int tooff = 0, fromoff = 0;

    if (to->start > from->start)
	fromoff = to->start-from->start;
    else
	tooff = from->start-to->start;
    size = to->len-tooff;
    if (size > from->len-fromoff)
	size = from->len-fromoff;
    if (size < 0)
	return;
    size *= sizeof(u16);
    
    switch (fsfromto) {
    case 0:
	memcpy(to->red+tooff, from->red+fromoff, size);
	memcpy(to->green+tooff, from->green+fromoff, size);
	memcpy(to->blue+tooff, from->blue+fromoff, size);
	if (from->transp && to->transp)
	    memcpy(to->transp+tooff, from->transp+fromoff, size);
        break;
    case 1:
	copy_from_user(to->red+tooff, from->red+fromoff, size);
	copy_from_user(to->green+tooff, from->green+fromoff, size);
	copy_from_user(to->blue+tooff, from->blue+fromoff, size);
	if (from->transp && to->transp)
            copy_from_user(to->transp+tooff, from->transp+fromoff, size);
	break;
    case 2:
	copy_to_user(to->red+tooff, from->red+fromoff, size);
	copy_to_user(to->green+tooff, from->green+fromoff, size);
	copy_to_user(to->blue+tooff, from->blue+fromoff, size);
	if (from->transp && to->transp)
	    copy_to_user(to->transp+tooff, from->transp+fromoff, size);
	break;
    }
}


/**
 *	fb_get_cmap - get a colormap
 *	@cmap: frame buffer colormap
 *	@kspc: boolean, 0 copy local, 1 put_user() function
 *	@getcolreg: pointer to a function to get a color register
 *	@info: frame buffer info structure
 *
 *	Get a colormap @cmap for a screen of device @info.
 *
 *	Returns negative errno on error, or zero on success.
 *
 */

int fb_get_cmap(struct fb_cmap *cmap, int kspc,
    	    	int (*getcolreg)(u_int, u_int *, u_int *, u_int *, u_int *,
				 struct fb_info *),
		struct fb_info *info)
{
    int i, start;
    u16 *red, *green, *blue, *transp;
    u_int hred, hgreen, hblue, htransp;

    red = cmap->red;
    green = cmap->green;
    blue = cmap->blue;
    transp = cmap->transp;
    start = cmap->start;
    if (start < 0)
	return -EINVAL;
    for (i = 0; i < cmap->len; i++) {
	if (getcolreg(start++, &hred, &hgreen, &hblue, &htransp, info))
	    return 0;
	if (kspc) {
	    *red = hred;
	    *green = hgreen;
	    *blue = hblue;
	    if (transp)
		*transp = htransp;
	} else {
	    put_user(hred, red);
	    put_user(hgreen, green);
	    put_user(hblue, blue);
	    if (transp)
		put_user(htransp, transp);
	}
	red++;
	green++;
	blue++;
	if (transp)
	    transp++;
    }
    return 0;
}


/**
 *	fb_set_cmap - set the colormap
 *	@cmap: frame buffer colormap structure
 *	@kspc: boolean, 0 copy local, 1 get_user() function
 *	@info: frame buffer info structure
 *
 *	Sets the colormap @cmap for a screen of device @info.
 *
 *	Returns negative errno on error, or zero on success.
 *
 */

int fb_set_cmap(struct fb_cmap *cmap, int kspc,
    	    	int (*setcolreg)(u_int, u_int, u_int, u_int, u_int,
				 struct fb_info *),
		struct fb_info *info)
{
    int i, start;
    u16 *red, *green, *blue, *transp;
    u_int hred, hgreen, hblue, htransp;

    red = cmap->red;
    green = cmap->green;
    blue = cmap->blue;
    transp = cmap->transp;
    start = cmap->start;

    if (start < 0)
	return -EINVAL;
    for (i = 0; i < cmap->len; i++) {
	if (kspc) {
	    hred = *red;
	    hgreen = *green;
	    hblue = *blue;
	    htransp = transp ? *transp : 0;
	} else {
	    get_user(hred, red);
	    get_user(hgreen, green);
	    get_user(hblue, blue);
	    if (transp)
		get_user(htransp, transp);
	    else
		htransp = 0;
	}
	red++;
	green++;
	blue++;
	if (transp)
	    transp++;
	if (setcolreg(start++, hred, hgreen, hblue, htransp, info))
	    return 0;
    }
    return 0;
}


/**
 *	fb_default_cmap - get default colormap
 *	@len: size of palette for a depth
 *
 *	Gets the default colormap for a specific screen depth.  @len
 *	is the size of the palette for a particular screen depth.
 *
 *	Returns pointer to a frame buffer colormap structure.
 *
 */

struct fb_cmap *fb_default_cmap(int len)
{
    if (len <= 2)
	return &default_2_colors;
    if (len <= 4)
	return &default_4_colors;
    if (len <= 8)
	return &default_8_colors;
    return &default_16_colors;
}


/**
 *	fb_invert_cmaps - invert all defaults colormaps
 *
 *	Invert all default colormaps.
 *
 */

void fb_invert_cmaps(void)
{
    u_int i;

    for (i = 0; i < 2; i++) {
	red2[i] = ~red2[i];
	green2[i] = ~green2[i];
	blue2[i] = ~blue2[i];
    }
    for (i = 0; i < 4; i++) {
	red4[i] = ~red4[i];
	green4[i] = ~green4[i];
	blue4[i] = ~blue4[i];
    }
    for (i = 0; i < 8; i++) {
	red8[i] = ~red8[i];
	green8[i] = ~green8[i];
	blue8[i] = ~blue8[i];
    }
    for (i = 0; i < 16; i++) {
	red16[i] = ~red16[i];
	green16[i] = ~green16[i];
	blue16[i] = ~blue16[i];
    }
}


    /*
     *  Visible symbols for modules
     */

EXPORT_SYMBOL(fb_alloc_cmap);
EXPORT_SYMBOL(fb_copy_cmap);
EXPORT_SYMBOL(fb_get_cmap);
EXPORT_SYMBOL(fb_set_cmap);
EXPORT_SYMBOL(fb_default_cmap);
EXPORT_SYMBOL(fb_invert_cmaps);