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