2 * linux/drivers/video/fbcmap.c -- Colormap handling for frame buffer devices
4 * Created 15 Jun 1997 by Geert Uytterhoeven
6 * 2001 - Documented with DocBook
7 * - Brad Douglas <brad@neruo.com>
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file COPYING in the main directory of this archive for
14 #include <linux/string.h>
15 #include <linux/module.h>
16 #include <linux/tty.h>
18 #include <linux/slab.h>
20 #include <asm/uaccess.h>
25 static u16 green2[] = {
28 static u16 blue2[] = {
33 0x0000, 0xaaaa, 0x5555, 0xffff
35 static u16 green4[] = {
36 0x0000, 0xaaaa, 0x5555, 0xffff
38 static u16 blue4[] = {
39 0x0000, 0xaaaa, 0x5555, 0xffff
43 0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa
45 static u16 green8[] = {
46 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa
48 static u16 blue8[] = {
49 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa
52 static u16 red16[] = {
53 0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa,
54 0x5555, 0x5555, 0x5555, 0x5555, 0xffff, 0xffff, 0xffff, 0xffff
56 static u16 green16[] = {
57 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa,
58 0x5555, 0x5555, 0xffff, 0xffff, 0x5555, 0x5555, 0xffff, 0xffff
60 static u16 blue16[] = {
61 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa,
62 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff
65 static struct fb_cmap default_2_colors = {
66 0, 2, red2, green2, blue2, NULL
68 static struct fb_cmap default_8_colors = {
69 0, 8, red8, green8, blue8, NULL
71 static struct fb_cmap default_4_colors = {
72 0, 4, red4, green4, blue4, NULL
74 static struct fb_cmap default_16_colors = {
75 0, 16, red16, green16, blue16, NULL
80 * fb_alloc_cmap - allocate a colormap
81 * @cmap: frame buffer colormap structure
82 * @len: length of @cmap
83 * @transp: boolean, 1 if there is transparency, 0 otherwise
85 * Allocates memory for a colormap @cmap. @len is the
86 * number of entries in the palette.
88 * Returns -1 errno on error, or zero on success.
92 int fb_alloc_cmap(struct fb_cmap *cmap, int len, int transp)
94 int size = len*sizeof(u16);
96 if (cmap->len != len) {
105 cmap->red = cmap->green = cmap->blue = cmap->transp = NULL;
109 if (!(cmap->red = kmalloc(size, GFP_ATOMIC)))
111 if (!(cmap->green = kmalloc(size, GFP_ATOMIC)))
113 if (!(cmap->blue = kmalloc(size, GFP_ATOMIC)))
116 if (!(cmap->transp = kmalloc(size, GFP_ATOMIC)))
123 fb_copy_cmap(fb_default_cmap(len), cmap, 0);
129 * fb_copy_cmap - copy a colormap
130 * @from: frame buffer colormap structure
131 * @to: frame buffer colormap structure
132 * @fsfromto: determine copy method
134 * Copy contents of colormap from @from to @to.
136 * @fsfromto accepts the following integer parameters:
138 * 1: copy_from_user() function to copy from userspace
139 * 2: copy_to_user() function to copy to userspace
143 void fb_copy_cmap(struct fb_cmap *from, struct fb_cmap *to, int fsfromto)
146 int tooff = 0, fromoff = 0;
148 if (to->start > from->start)
149 fromoff = to->start-from->start;
151 tooff = from->start-to->start;
152 size = to->len-tooff;
153 if (size > from->len-fromoff)
154 size = from->len-fromoff;
161 memcpy(to->red+tooff, from->red+fromoff, size);
162 memcpy(to->green+tooff, from->green+fromoff, size);
163 memcpy(to->blue+tooff, from->blue+fromoff, size);
164 if (from->transp && to->transp)
165 memcpy(to->transp+tooff, from->transp+fromoff, size);
168 copy_from_user(to->red+tooff, from->red+fromoff, size);
169 copy_from_user(to->green+tooff, from->green+fromoff, size);
170 copy_from_user(to->blue+tooff, from->blue+fromoff, size);
171 if (from->transp && to->transp)
172 copy_from_user(to->transp+tooff, from->transp+fromoff, size);
175 copy_to_user(to->red+tooff, from->red+fromoff, size);
176 copy_to_user(to->green+tooff, from->green+fromoff, size);
177 copy_to_user(to->blue+tooff, from->blue+fromoff, size);
178 if (from->transp && to->transp)
179 copy_to_user(to->transp+tooff, from->transp+fromoff, size);
186 * fb_get_cmap - get a colormap
187 * @cmap: frame buffer colormap
188 * @kspc: boolean, 0 copy local, 1 put_user() function
189 * @getcolreg: pointer to a function to get a color register
190 * @info: frame buffer info structure
192 * Get a colormap @cmap for a screen of device @info.
194 * Returns negative errno on error, or zero on success.
198 int fb_get_cmap(struct fb_cmap *cmap, int kspc,
199 int (*getcolreg)(u_int, u_int *, u_int *, u_int *, u_int *,
201 struct fb_info *info)
204 u16 *red, *green, *blue, *transp;
205 u_int hred, hgreen, hblue, htransp;
210 transp = cmap->transp;
214 for (i = 0; i < cmap->len; i++) {
215 if (getcolreg(start++, &hred, &hgreen, &hblue, &htransp, info))
225 put_user(hgreen, green);
226 put_user(hblue, blue);
228 put_user(htransp, transp);
241 * fb_set_cmap - set the colormap
242 * @cmap: frame buffer colormap structure
243 * @kspc: boolean, 0 copy local, 1 get_user() function
244 * @info: frame buffer info structure
246 * Sets the colormap @cmap for a screen of device @info.
248 * Returns negative errno on error, or zero on success.
252 int fb_set_cmap(struct fb_cmap *cmap, int kspc,
253 int (*setcolreg)(u_int, u_int, u_int, u_int, u_int,
255 struct fb_info *info)
258 u16 *red, *green, *blue, *transp;
259 u_int hred, hgreen, hblue, htransp;
264 transp = cmap->transp;
269 for (i = 0; i < cmap->len; i++) {
274 htransp = transp ? *transp : 0;
277 get_user(hgreen, green);
278 get_user(hblue, blue);
280 get_user(htransp, transp);
289 if (setcolreg(start++, hred, hgreen, hblue, htransp, info))
297 * fb_default_cmap - get default colormap
298 * @len: size of palette for a depth
300 * Gets the default colormap for a specific screen depth. @len
301 * is the size of the palette for a particular screen depth.
303 * Returns pointer to a frame buffer colormap structure.
307 struct fb_cmap *fb_default_cmap(int len)
310 return &default_2_colors;
312 return &default_4_colors;
314 return &default_8_colors;
315 return &default_16_colors;
320 * fb_invert_cmaps - invert all defaults colormaps
322 * Invert all default colormaps.
326 void fb_invert_cmaps(void)
330 for (i = 0; i < 2; i++) {
332 green2[i] = ~green2[i];
333 blue2[i] = ~blue2[i];
335 for (i = 0; i < 4; i++) {
337 green4[i] = ~green4[i];
338 blue4[i] = ~blue4[i];
340 for (i = 0; i < 8; i++) {
342 green8[i] = ~green8[i];
343 blue8[i] = ~blue8[i];
345 for (i = 0; i < 16; i++) {
346 red16[i] = ~red16[i];
347 green16[i] = ~green16[i];
348 blue16[i] = ~blue16[i];
354 * Visible symbols for modules
357 EXPORT_SYMBOL(fb_alloc_cmap);
358 EXPORT_SYMBOL(fb_copy_cmap);
359 EXPORT_SYMBOL(fb_get_cmap);
360 EXPORT_SYMBOL(fb_set_cmap);
361 EXPORT_SYMBOL(fb_default_cmap);
362 EXPORT_SYMBOL(fb_invert_cmaps);