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