x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / drivers / video / fbcmap.c
blobf53b9f1d6aba23830d3b5c1a551054cf9f8b4538
1 /*
2 * linux/drivers/video/fbcmap.c -- Colormap handling for frame buffer devices
4 * Created 15 Jun 1997 by Geert Uytterhoeven
6 * 2001 - Documented with DocBook
7 * - Brad Douglas <brad@neruo.com>
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.
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>
20 static u16 red2[] __read_mostly = {
21 0x0000, 0xaaaa
23 static u16 green2[] __read_mostly = {
24 0x0000, 0xaaaa
26 static u16 blue2[] __read_mostly = {
27 0x0000, 0xaaaa
30 static u16 red4[] __read_mostly = {
31 0x0000, 0xaaaa, 0x5555, 0xffff
33 static u16 green4[] __read_mostly = {
34 0x0000, 0xaaaa, 0x5555, 0xffff
36 static u16 blue4[] __read_mostly = {
37 0x0000, 0xaaaa, 0x5555, 0xffff
40 static u16 red8[] __read_mostly = {
41 0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa
43 static u16 green8[] __read_mostly = {
44 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa
46 static u16 blue8[] __read_mostly = {
47 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa
50 static u16 red16[] __read_mostly = {
51 0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa,
52 0x5555, 0x5555, 0x5555, 0x5555, 0xffff, 0xffff, 0xffff, 0xffff
54 static u16 green16[] __read_mostly = {
55 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa,
56 0x5555, 0x5555, 0xffff, 0xffff, 0x5555, 0x5555, 0xffff, 0xffff
58 static u16 blue16[] __read_mostly = {
59 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa,
60 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff
63 static const struct fb_cmap default_2_colors = {
64 .len=2, .red=red2, .green=green2, .blue=blue2
66 static const struct fb_cmap default_8_colors = {
67 .len=8, .red=red8, .green=green8, .blue=blue8
69 static const struct fb_cmap default_4_colors = {
70 .len=4, .red=red4, .green=green4, .blue=blue4
72 static const struct fb_cmap default_16_colors = {
73 .len=16, .red=red16, .green=green16, .blue=blue16
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
84 * Allocates memory for a colormap @cmap. @len is the
85 * number of entries in the palette.
87 * Returns negative errno on error, or zero on success.
91 int fb_alloc_cmap(struct fb_cmap *cmap, int len, int transp)
93 int size = len*sizeof(u16);
95 if (cmap->len != len) {
96 fb_dealloc_cmap(cmap);
97 if (!len)
98 return 0;
99 if (!(cmap->red = kmalloc(size, GFP_ATOMIC)))
100 goto fail;
101 if (!(cmap->green = kmalloc(size, GFP_ATOMIC)))
102 goto fail;
103 if (!(cmap->blue = kmalloc(size, GFP_ATOMIC)))
104 goto fail;
105 if (transp) {
106 if (!(cmap->transp = kmalloc(size, GFP_ATOMIC)))
107 goto fail;
108 } else
109 cmap->transp = NULL;
111 cmap->start = 0;
112 cmap->len = len;
113 fb_copy_cmap(fb_default_cmap(len), cmap);
114 return 0;
116 fail:
117 fb_dealloc_cmap(cmap);
118 return -ENOMEM;
122 * fb_dealloc_cmap - deallocate a colormap
123 * @cmap: frame buffer colormap structure
125 * Deallocates a colormap that was previously allocated with
126 * fb_alloc_cmap().
130 void fb_dealloc_cmap(struct fb_cmap *cmap)
132 kfree(cmap->red);
133 kfree(cmap->green);
134 kfree(cmap->blue);
135 kfree(cmap->transp);
137 cmap->red = cmap->green = cmap->blue = cmap->transp = NULL;
138 cmap->len = 0;
142 * fb_copy_cmap - copy a colormap
143 * @from: frame buffer colormap structure
144 * @to: frame buffer colormap structure
146 * Copy contents of colormap from @from to @to.
149 int fb_copy_cmap(const struct fb_cmap *from, struct fb_cmap *to)
151 int tooff = 0, fromoff = 0;
152 int size;
154 if (to->start > from->start)
155 fromoff = to->start - from->start;
156 else
157 tooff = from->start - to->start;
158 size = to->len - tooff;
159 if (size > (int) (from->len - fromoff))
160 size = from->len - fromoff;
161 if (size <= 0)
162 return -EINVAL;
163 size *= sizeof(u16);
165 memcpy(to->red+tooff, from->red+fromoff, size);
166 memcpy(to->green+tooff, from->green+fromoff, size);
167 memcpy(to->blue+tooff, from->blue+fromoff, size);
168 if (from->transp && to->transp)
169 memcpy(to->transp+tooff, from->transp+fromoff, size);
170 return 0;
173 int fb_cmap_to_user(const struct fb_cmap *from, struct fb_cmap_user *to)
175 int tooff = 0, fromoff = 0;
176 int size;
178 if (to->start > from->start)
179 fromoff = to->start - from->start;
180 else
181 tooff = from->start - to->start;
182 size = to->len - tooff;
183 if (size > (int) (from->len - fromoff))
184 size = from->len - fromoff;
185 if (size <= 0)
186 return -EINVAL;
187 size *= sizeof(u16);
189 if (copy_to_user(to->red+tooff, from->red+fromoff, size))
190 return -EFAULT;
191 if (copy_to_user(to->green+tooff, from->green+fromoff, size))
192 return -EFAULT;
193 if (copy_to_user(to->blue+tooff, from->blue+fromoff, size))
194 return -EFAULT;
195 if (from->transp && to->transp)
196 if (copy_to_user(to->transp+tooff, from->transp+fromoff, size))
197 return -EFAULT;
198 return 0;
202 * fb_set_cmap - set the colormap
203 * @cmap: frame buffer colormap structure
204 * @info: frame buffer info structure
206 * Sets the colormap @cmap for a screen of device @info.
208 * Returns negative errno on error, or zero on success.
212 int fb_set_cmap(struct fb_cmap *cmap, struct fb_info *info)
214 int i, start, rc = 0;
215 u16 *red, *green, *blue, *transp;
216 u_int hred, hgreen, hblue, htransp = 0xffff;
218 red = cmap->red;
219 green = cmap->green;
220 blue = cmap->blue;
221 transp = cmap->transp;
222 start = cmap->start;
224 if (start < 0 || (!info->fbops->fb_setcolreg &&
225 !info->fbops->fb_setcmap))
226 return -EINVAL;
227 if (info->fbops->fb_setcmap) {
228 rc = info->fbops->fb_setcmap(cmap, info);
229 } else {
230 for (i = 0; i < cmap->len; i++) {
231 hred = *red++;
232 hgreen = *green++;
233 hblue = *blue++;
234 if (transp)
235 htransp = *transp++;
236 if (info->fbops->fb_setcolreg(start++,
237 hred, hgreen, hblue,
238 htransp, info))
239 break;
242 if (rc == 0)
243 fb_copy_cmap(cmap, &info->cmap);
245 return rc;
248 int fb_set_user_cmap(struct fb_cmap_user *cmap, struct fb_info *info)
250 int rc, size = cmap->len * sizeof(u16);
251 struct fb_cmap umap;
253 memset(&umap, 0, sizeof(struct fb_cmap));
254 rc = fb_alloc_cmap(&umap, cmap->len, cmap->transp != NULL);
255 if (rc)
256 return rc;
257 if (copy_from_user(umap.red, cmap->red, size) ||
258 copy_from_user(umap.green, cmap->green, size) ||
259 copy_from_user(umap.blue, cmap->blue, size) ||
260 (cmap->transp && copy_from_user(umap.transp, cmap->transp, size))) {
261 rc = -EFAULT;
262 goto out;
264 umap.start = cmap->start;
265 if (!lock_fb_info(info)) {
266 rc = -ENODEV;
267 goto out;
269 if (cmap->start < 0 || (!info->fbops->fb_setcolreg &&
270 !info->fbops->fb_setcmap)) {
271 rc = -EINVAL;
272 goto out1;
274 rc = fb_set_cmap(&umap, info);
275 out1:
276 unlock_fb_info(info);
277 out:
278 fb_dealloc_cmap(&umap);
279 return rc;
283 * fb_default_cmap - get default colormap
284 * @len: size of palette for a depth
286 * Gets the default colormap for a specific screen depth. @len
287 * is the size of the palette for a particular screen depth.
289 * Returns pointer to a frame buffer colormap structure.
293 const struct fb_cmap *fb_default_cmap(int len)
295 if (len <= 2)
296 return &default_2_colors;
297 if (len <= 4)
298 return &default_4_colors;
299 if (len <= 8)
300 return &default_8_colors;
301 return &default_16_colors;
306 * fb_invert_cmaps - invert all defaults colormaps
308 * Invert all default colormaps.
312 void fb_invert_cmaps(void)
314 u_int i;
316 for (i = 0; i < ARRAY_SIZE(red2); i++) {
317 red2[i] = ~red2[i];
318 green2[i] = ~green2[i];
319 blue2[i] = ~blue2[i];
321 for (i = 0; i < ARRAY_SIZE(red4); i++) {
322 red4[i] = ~red4[i];
323 green4[i] = ~green4[i];
324 blue4[i] = ~blue4[i];
326 for (i = 0; i < ARRAY_SIZE(red8); i++) {
327 red8[i] = ~red8[i];
328 green8[i] = ~green8[i];
329 blue8[i] = ~blue8[i];
331 for (i = 0; i < ARRAY_SIZE(red16); i++) {
332 red16[i] = ~red16[i];
333 green16[i] = ~green16[i];
334 blue16[i] = ~blue16[i];
340 * Visible symbols for modules
343 EXPORT_SYMBOL(fb_alloc_cmap);
344 EXPORT_SYMBOL(fb_dealloc_cmap);
345 EXPORT_SYMBOL(fb_copy_cmap);
346 EXPORT_SYMBOL(fb_set_cmap);
347 EXPORT_SYMBOL(fb_default_cmap);
348 EXPORT_SYMBOL(fb_invert_cmaps);