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