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