Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
2 * linux/drivers/video/fbcmap.c -- Colormap handling for frame buffer devices
3 *
4 * Created 15 Jun 1997 by Geert Uytterhoeven
5 *
6 * 2001 - Documented with DocBook
7 * - Brad Douglas <brad@neruo.com>
8 *
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
11 * more details.
12 */
13
14#include <linux/export.h>
15#include <linux/string.h>
16#include <linux/module.h>
17#include <linux/fb.h>
18#include <linux/slab.h>
19#include <linux/uaccess.h>
20
21static u16 red2[] __read_mostly = {
22 0x0000, 0xaaaa
23};
24static u16 green2[] __read_mostly = {
25 0x0000, 0xaaaa
26};
27static u16 blue2[] __read_mostly = {
28 0x0000, 0xaaaa
29};
30
31static u16 red4[] __read_mostly = {
32 0x0000, 0xaaaa, 0x5555, 0xffff
33};
34static u16 green4[] __read_mostly = {
35 0x0000, 0xaaaa, 0x5555, 0xffff
36};
37static u16 blue4[] __read_mostly = {
38 0x0000, 0xaaaa, 0x5555, 0xffff
39};
40
41static u16 red8[] __read_mostly = {
42 0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa
43};
44static u16 green8[] __read_mostly = {
45 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa
46};
47static u16 blue8[] __read_mostly = {
48 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa
49};
50
51static u16 red16[] __read_mostly = {
52 0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa,
53 0x5555, 0x5555, 0x5555, 0x5555, 0xffff, 0xffff, 0xffff, 0xffff
54};
55static u16 green16[] __read_mostly = {
56 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa,
57 0x5555, 0x5555, 0xffff, 0xffff, 0x5555, 0x5555, 0xffff, 0xffff
58};
59static u16 blue16[] __read_mostly = {
60 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa,
61 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff
62};
63
64static const struct fb_cmap default_2_colors = {
65 .len=2, .red=red2, .green=green2, .blue=blue2
66};
67static const struct fb_cmap default_8_colors = {
68 .len=8, .red=red8, .green=green8, .blue=blue8
69};
70static const struct fb_cmap default_4_colors = {
71 .len=4, .red=red4, .green=green4, .blue=blue4
72};
73static const struct fb_cmap default_16_colors = {
74 .len=16, .red=red16, .green=green16, .blue=blue16
75};
76
77
78
79/**
80 * fb_alloc_cmap_gfp - allocate a colormap
81 * @cmap: frame buffer colormap structure
82 * @len: length of @cmap
83 * @transp: boolean, 1 if there is transparency, 0 otherwise
84 * @flags: flags for kmalloc memory allocation
85 *
86 * Allocates memory for a colormap @cmap. @len is the
87 * number of entries in the palette.
88 *
89 * Returns negative errno on error, or zero on success.
90 *
91 */
92
93int fb_alloc_cmap_gfp(struct fb_cmap *cmap, int len, int transp, gfp_t flags)
94{
95 int size = len * sizeof(u16);
96 int ret = -ENOMEM;
97
98 flags |= __GFP_NOWARN;
99
100 if (cmap->len != len) {
101 fb_dealloc_cmap(cmap);
102 if (!len)
103 return 0;
104
105 cmap->red = kzalloc(size, flags);
106 if (!cmap->red)
107 goto fail;
108 cmap->green = kzalloc(size, flags);
109 if (!cmap->green)
110 goto fail;
111 cmap->blue = kzalloc(size, flags);
112 if (!cmap->blue)
113 goto fail;
114 if (transp) {
115 cmap->transp = kzalloc(size, flags);
116 if (!cmap->transp)
117 goto fail;
118 } else {
119 cmap->transp = NULL;
120 }
121 }
122 cmap->start = 0;
123 cmap->len = len;
124 ret = fb_copy_cmap(fb_default_cmap(len), cmap);
125 if (ret)
126 goto fail;
127 return 0;
128
129fail:
130 fb_dealloc_cmap(cmap);
131 return ret;
132}
133
134int fb_alloc_cmap(struct fb_cmap *cmap, int len, int transp)
135{
136 return fb_alloc_cmap_gfp(cmap, len, transp, GFP_ATOMIC);
137}
138
139/**
140 * fb_dealloc_cmap - deallocate a colormap
141 * @cmap: frame buffer colormap structure
142 *
143 * Deallocates a colormap that was previously allocated with
144 * fb_alloc_cmap().
145 *
146 */
147
148void fb_dealloc_cmap(struct fb_cmap *cmap)
149{
150 kfree(cmap->red);
151 kfree(cmap->green);
152 kfree(cmap->blue);
153 kfree(cmap->transp);
154
155 cmap->red = cmap->green = cmap->blue = cmap->transp = NULL;
156 cmap->len = 0;
157}
158
159/**
160 * fb_copy_cmap - copy a colormap
161 * @from: frame buffer colormap structure
162 * @to: frame buffer colormap structure
163 *
164 * Copy contents of colormap from @from to @to.
165 */
166
167int fb_copy_cmap(const struct fb_cmap *from, struct fb_cmap *to)
168{
169 unsigned int tooff = 0, fromoff = 0;
170 size_t size;
171
172 if (to->start > from->start)
173 fromoff = to->start - from->start;
174 else
175 tooff = from->start - to->start;
176 if (fromoff >= from->len || tooff >= to->len)
177 return -EINVAL;
178
179 size = min_t(size_t, to->len - tooff, from->len - fromoff);
180 if (size == 0)
181 return -EINVAL;
182 size *= sizeof(u16);
183
184 memcpy(to->red+tooff, from->red+fromoff, size);
185 memcpy(to->green+tooff, from->green+fromoff, size);
186 memcpy(to->blue+tooff, from->blue+fromoff, size);
187 if (from->transp && to->transp)
188 memcpy(to->transp+tooff, from->transp+fromoff, size);
189 return 0;
190}
191
192int fb_cmap_to_user(const struct fb_cmap *from, struct fb_cmap_user *to)
193{
194 unsigned int tooff = 0, fromoff = 0;
195 size_t size;
196
197 if (to->start > from->start)
198 fromoff = to->start - from->start;
199 else
200 tooff = from->start - to->start;
201 if (fromoff >= from->len || tooff >= to->len)
202 return -EINVAL;
203
204 size = min_t(size_t, to->len - tooff, from->len - fromoff);
205 if (size == 0)
206 return -EINVAL;
207 size *= sizeof(u16);
208
209 if (copy_to_user(to->red+tooff, from->red+fromoff, size))
210 return -EFAULT;
211 if (copy_to_user(to->green+tooff, from->green+fromoff, size))
212 return -EFAULT;
213 if (copy_to_user(to->blue+tooff, from->blue+fromoff, size))
214 return -EFAULT;
215 if (from->transp && to->transp)
216 if (copy_to_user(to->transp+tooff, from->transp+fromoff, size))
217 return -EFAULT;
218 return 0;
219}
220
221/**
222 * fb_set_cmap - set the colormap
223 * @cmap: frame buffer colormap structure
224 * @info: frame buffer info structure
225 *
226 * Sets the colormap @cmap for a screen of device @info.
227 *
228 * Returns negative errno on error, or zero on success.
229 *
230 */
231
232int fb_set_cmap(struct fb_cmap *cmap, struct fb_info *info)
233{
234 int i, start, rc = 0;
235 u16 *red, *green, *blue, *transp;
236 u_int hred, hgreen, hblue, htransp = 0xffff;
237
238 red = cmap->red;
239 green = cmap->green;
240 blue = cmap->blue;
241 transp = cmap->transp;
242 start = cmap->start;
243
244 if (start < 0 || (!info->fbops->fb_setcolreg &&
245 !info->fbops->fb_setcmap))
246 return -EINVAL;
247 if (info->fbops->fb_setcmap) {
248 rc = info->fbops->fb_setcmap(cmap, info);
249 } else {
250 for (i = 0; i < cmap->len; i++) {
251 hred = *red++;
252 hgreen = *green++;
253 hblue = *blue++;
254 if (transp)
255 htransp = *transp++;
256 if (info->fbops->fb_setcolreg(start++,
257 hred, hgreen, hblue,
258 htransp, info))
259 break;
260 }
261 }
262 if (rc == 0)
263 fb_copy_cmap(cmap, &info->cmap);
264
265 return rc;
266}
267
268int fb_set_user_cmap(struct fb_cmap_user *cmap, struct fb_info *info)
269{
270 int rc, size = cmap->len * sizeof(u16);
271 struct fb_cmap umap;
272
273 if (size < 0 || size < cmap->len)
274 return -E2BIG;
275
276 memset(&umap, 0, sizeof(struct fb_cmap));
277 rc = fb_alloc_cmap_gfp(&umap, cmap->len, cmap->transp != NULL,
278 GFP_KERNEL);
279 if (rc)
280 return rc;
281 if (copy_from_user(umap.red, cmap->red, size) ||
282 copy_from_user(umap.green, cmap->green, size) ||
283 copy_from_user(umap.blue, cmap->blue, size) ||
284 (cmap->transp && copy_from_user(umap.transp, cmap->transp, size))) {
285 rc = -EFAULT;
286 goto out;
287 }
288 umap.start = cmap->start;
289 lock_fb_info(info);
290 rc = fb_set_cmap(&umap, info);
291 unlock_fb_info(info);
292out:
293 fb_dealloc_cmap(&umap);
294 return rc;
295}
296
297/**
298 * fb_default_cmap - get default colormap
299 * @len: size of palette for a depth
300 *
301 * Gets the default colormap for a specific screen depth. @len
302 * is the size of the palette for a particular screen depth.
303 *
304 * Returns pointer to a frame buffer colormap structure.
305 *
306 */
307
308const struct fb_cmap *fb_default_cmap(int len)
309{
310 if (len <= 2)
311 return &default_2_colors;
312 if (len <= 4)
313 return &default_4_colors;
314 if (len <= 8)
315 return &default_8_colors;
316 return &default_16_colors;
317}
318
319
320/**
321 * fb_invert_cmaps - invert all defaults colormaps
322 *
323 * Invert all default colormaps.
324 *
325 */
326
327void fb_invert_cmaps(void)
328{
329 u_int i;
330
331 for (i = 0; i < ARRAY_SIZE(red2); i++) {
332 red2[i] = ~red2[i];
333 green2[i] = ~green2[i];
334 blue2[i] = ~blue2[i];
335 }
336 for (i = 0; i < ARRAY_SIZE(red4); i++) {
337 red4[i] = ~red4[i];
338 green4[i] = ~green4[i];
339 blue4[i] = ~blue4[i];
340 }
341 for (i = 0; i < ARRAY_SIZE(red8); i++) {
342 red8[i] = ~red8[i];
343 green8[i] = ~green8[i];
344 blue8[i] = ~blue8[i];
345 }
346 for (i = 0; i < ARRAY_SIZE(red16); i++) {
347 red16[i] = ~red16[i];
348 green16[i] = ~green16[i];
349 blue16[i] = ~blue16[i];
350 }
351}
352
353
354 /*
355 * Visible symbols for modules
356 */
357
358EXPORT_SYMBOL(fb_alloc_cmap);
359EXPORT_SYMBOL(fb_dealloc_cmap);
360EXPORT_SYMBOL(fb_copy_cmap);
361EXPORT_SYMBOL(fb_set_cmap);
362EXPORT_SYMBOL(fb_default_cmap);
363EXPORT_SYMBOL(fb_invert_cmaps);