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>
17 #include <linux/slab.h>
18 #include <linux/uaccess.h>
20 static u16 red2
[] __read_mostly
= {
23 static u16 green2
[] __read_mostly
= {
26 static u16 blue2
[] __read_mostly
= {
30 static u16 red4
[] __read_mostly
= {
31 0x0000, 0xaaaa, 0x5555, 0xffff
33 static u16 green4
[] __read_mostly
= {
34 0x0000, 0xaaaa, 0x5555, 0xffff
36 static u16 blue4
[] __read_mostly
= {
37 0x0000, 0xaaaa, 0x5555, 0xffff
40 static u16 red8
[] __read_mostly
= {
41 0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa
43 static u16 green8
[] __read_mostly
= {
44 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa
46 static u16 blue8
[] __read_mostly
= {
47 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa
50 static u16 red16
[] __read_mostly
= {
51 0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa,
52 0x5555, 0x5555, 0x5555, 0x5555, 0xffff, 0xffff, 0xffff, 0xffff
54 static u16 green16
[] __read_mostly
= {
55 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa,
56 0x5555, 0x5555, 0xffff, 0xffff, 0x5555, 0x5555, 0xffff, 0xffff
58 static u16 blue16
[] __read_mostly
= {
59 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa,
60 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff
63 static const struct fb_cmap default_2_colors
= {
64 .len
=2, .red
=red2
, .green
=green2
, .blue
=blue2
66 static const struct fb_cmap default_8_colors
= {
67 .len
=8, .red
=red8
, .green
=green8
, .blue
=blue8
69 static const struct fb_cmap default_4_colors
= {
70 .len
=4, .red
=red4
, .green
=green4
, .blue
=blue4
72 static const struct fb_cmap default_16_colors
= {
73 .len
=16, .red
=red16
, .green
=green16
, .blue
=blue16
79 * fb_alloc_cmap - allocate a colormap
80 * @cmap: frame buffer colormap structure
81 * @len: length of @cmap
82 * @transp: boolean, 1 if there is transparency, 0 otherwise
83 * @flags: flags for kmalloc memory allocation
85 * Allocates memory for a colormap @cmap. @len is the
86 * number of entries in the palette.
88 * Returns negative errno on error, or zero on success.
92 int fb_alloc_cmap_gfp(struct fb_cmap
*cmap
, int len
, int transp
, gfp_t flags
)
94 int size
= len
* sizeof(u16
);
97 if (cmap
->len
!= len
) {
98 fb_dealloc_cmap(cmap
);
102 cmap
->red
= kmalloc(size
, flags
);
105 cmap
->green
= kmalloc(size
, flags
);
108 cmap
->blue
= kmalloc(size
, flags
);
112 cmap
->transp
= kmalloc(size
, flags
);
121 ret
= fb_copy_cmap(fb_default_cmap(len
), cmap
);
127 fb_dealloc_cmap(cmap
);
131 int fb_alloc_cmap(struct fb_cmap
*cmap
, int len
, int transp
)
133 return fb_alloc_cmap_gfp(cmap
, len
, transp
, GFP_ATOMIC
);
137 * fb_dealloc_cmap - deallocate a colormap
138 * @cmap: frame buffer colormap structure
140 * Deallocates a colormap that was previously allocated with
145 void fb_dealloc_cmap(struct fb_cmap
*cmap
)
152 cmap
->red
= cmap
->green
= cmap
->blue
= cmap
->transp
= NULL
;
157 * fb_copy_cmap - copy a colormap
158 * @from: frame buffer colormap structure
159 * @to: frame buffer colormap structure
161 * Copy contents of colormap from @from to @to.
164 int fb_copy_cmap(const struct fb_cmap
*from
, struct fb_cmap
*to
)
166 int tooff
= 0, fromoff
= 0;
169 if (to
->start
> from
->start
)
170 fromoff
= to
->start
- from
->start
;
172 tooff
= from
->start
- to
->start
;
173 size
= to
->len
- tooff
;
174 if (size
> (int) (from
->len
- fromoff
))
175 size
= from
->len
- fromoff
;
180 memcpy(to
->red
+tooff
, from
->red
+fromoff
, size
);
181 memcpy(to
->green
+tooff
, from
->green
+fromoff
, size
);
182 memcpy(to
->blue
+tooff
, from
->blue
+fromoff
, size
);
183 if (from
->transp
&& to
->transp
)
184 memcpy(to
->transp
+tooff
, from
->transp
+fromoff
, size
);
188 int fb_cmap_to_user(const struct fb_cmap
*from
, struct fb_cmap_user
*to
)
190 int tooff
= 0, fromoff
= 0;
193 if (to
->start
> from
->start
)
194 fromoff
= to
->start
- from
->start
;
196 tooff
= from
->start
- to
->start
;
197 size
= to
->len
- tooff
;
198 if (size
> (int) (from
->len
- fromoff
))
199 size
= from
->len
- fromoff
;
204 if (copy_to_user(to
->red
+tooff
, from
->red
+fromoff
, size
))
206 if (copy_to_user(to
->green
+tooff
, from
->green
+fromoff
, size
))
208 if (copy_to_user(to
->blue
+tooff
, from
->blue
+fromoff
, size
))
210 if (from
->transp
&& to
->transp
)
211 if (copy_to_user(to
->transp
+tooff
, from
->transp
+fromoff
, size
))
217 * fb_set_cmap - set the colormap
218 * @cmap: frame buffer colormap structure
219 * @info: frame buffer info structure
221 * Sets the colormap @cmap for a screen of device @info.
223 * Returns negative errno on error, or zero on success.
227 int fb_set_cmap(struct fb_cmap
*cmap
, struct fb_info
*info
)
229 int i
, start
, rc
= 0;
230 u16
*red
, *green
, *blue
, *transp
;
231 u_int hred
, hgreen
, hblue
, htransp
= 0xffff;
236 transp
= cmap
->transp
;
239 if (start
< 0 || (!info
->fbops
->fb_setcolreg
&&
240 !info
->fbops
->fb_setcmap
))
242 if (info
->fbops
->fb_setcmap
) {
243 rc
= info
->fbops
->fb_setcmap(cmap
, info
);
245 for (i
= 0; i
< cmap
->len
; i
++) {
251 if (info
->fbops
->fb_setcolreg(start
++,
258 fb_copy_cmap(cmap
, &info
->cmap
);
263 int fb_set_user_cmap(struct fb_cmap_user
*cmap
, struct fb_info
*info
)
265 int rc
, size
= cmap
->len
* sizeof(u16
);
268 if (size
< 0 || size
< cmap
->len
)
271 memset(&umap
, 0, sizeof(struct fb_cmap
));
272 rc
= fb_alloc_cmap_gfp(&umap
, cmap
->len
, cmap
->transp
!= NULL
,
276 if (copy_from_user(umap
.red
, cmap
->red
, size
) ||
277 copy_from_user(umap
.green
, cmap
->green
, size
) ||
278 copy_from_user(umap
.blue
, cmap
->blue
, size
) ||
279 (cmap
->transp
&& copy_from_user(umap
.transp
, cmap
->transp
, size
))) {
283 umap
.start
= cmap
->start
;
284 if (!lock_fb_info(info
)) {
288 if (cmap
->start
< 0 || (!info
->fbops
->fb_setcolreg
&&
289 !info
->fbops
->fb_setcmap
)) {
293 rc
= fb_set_cmap(&umap
, info
);
295 unlock_fb_info(info
);
297 fb_dealloc_cmap(&umap
);
302 * fb_default_cmap - get default colormap
303 * @len: size of palette for a depth
305 * Gets the default colormap for a specific screen depth. @len
306 * is the size of the palette for a particular screen depth.
308 * Returns pointer to a frame buffer colormap structure.
312 const struct fb_cmap
*fb_default_cmap(int len
)
315 return &default_2_colors
;
317 return &default_4_colors
;
319 return &default_8_colors
;
320 return &default_16_colors
;
325 * fb_invert_cmaps - invert all defaults colormaps
327 * Invert all default colormaps.
331 void fb_invert_cmaps(void)
335 for (i
= 0; i
< ARRAY_SIZE(red2
); i
++) {
337 green2
[i
] = ~green2
[i
];
338 blue2
[i
] = ~blue2
[i
];
340 for (i
= 0; i
< ARRAY_SIZE(red4
); i
++) {
342 green4
[i
] = ~green4
[i
];
343 blue4
[i
] = ~blue4
[i
];
345 for (i
= 0; i
< ARRAY_SIZE(red8
); i
++) {
347 green8
[i
] = ~green8
[i
];
348 blue8
[i
] = ~blue8
[i
];
350 for (i
= 0; i
< ARRAY_SIZE(red16
); i
++) {
351 red16
[i
] = ~red16
[i
];
352 green16
[i
] = ~green16
[i
];
353 blue16
[i
] = ~blue16
[i
];
359 * Visible symbols for modules
362 EXPORT_SYMBOL(fb_alloc_cmap
);
363 EXPORT_SYMBOL(fb_dealloc_cmap
);
364 EXPORT_SYMBOL(fb_copy_cmap
);
365 EXPORT_SYMBOL(fb_set_cmap
);
366 EXPORT_SYMBOL(fb_default_cmap
);
367 EXPORT_SYMBOL(fb_invert_cmaps
);