blob: f26570d83d1452c17fb394fdf661f7284c7f23a7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/fb.h>
17#include <linux/slab.h>
Krzysztof Helt84902b72007-10-16 01:29:04 -070018#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Helge Delleradf6b202006-12-08 02:40:27 -080020static u16 red2[] __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 0x0000, 0xaaaa
22};
Helge Delleradf6b202006-12-08 02:40:27 -080023static u16 green2[] __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 0x0000, 0xaaaa
25};
Helge Delleradf6b202006-12-08 02:40:27 -080026static u16 blue2[] __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 0x0000, 0xaaaa
28};
29
Helge Delleradf6b202006-12-08 02:40:27 -080030static u16 red4[] __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 0x0000, 0xaaaa, 0x5555, 0xffff
32};
Helge Delleradf6b202006-12-08 02:40:27 -080033static u16 green4[] __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 0x0000, 0xaaaa, 0x5555, 0xffff
35};
Helge Delleradf6b202006-12-08 02:40:27 -080036static u16 blue4[] __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 0x0000, 0xaaaa, 0x5555, 0xffff
38};
39
Helge Delleradf6b202006-12-08 02:40:27 -080040static u16 red8[] __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa
42};
Helge Delleradf6b202006-12-08 02:40:27 -080043static u16 green8[] __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa
45};
Helge Delleradf6b202006-12-08 02:40:27 -080046static u16 blue8[] __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa
48};
49
Helge Delleradf6b202006-12-08 02:40:27 -080050static u16 red16[] __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa,
52 0x5555, 0x5555, 0x5555, 0x5555, 0xffff, 0xffff, 0xffff, 0xffff
53};
Helge Delleradf6b202006-12-08 02:40:27 -080054static u16 green16[] __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa,
56 0x5555, 0x5555, 0xffff, 0xffff, 0x5555, 0x5555, 0xffff, 0xffff
57};
Helge Delleradf6b202006-12-08 02:40:27 -080058static u16 blue16[] __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa,
60 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff
61};
62
Helge Delleradf6b202006-12-08 02:40:27 -080063static const struct fb_cmap default_2_colors = {
64 .len=2, .red=red2, .green=green2, .blue=blue2
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
Helge Delleradf6b202006-12-08 02:40:27 -080066static const struct fb_cmap default_8_colors = {
67 .len=8, .red=red8, .green=green8, .blue=blue8
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
Helge Delleradf6b202006-12-08 02:40:27 -080069static const struct fb_cmap default_4_colors = {
70 .len=4, .red=red4, .green=green4, .blue=blue4
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
Helge Delleradf6b202006-12-08 02:40:27 -080072static const struct fb_cmap default_16_colors = {
73 .len=16, .red=red16, .green=green16, .blue=blue16
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
76
Helge Delleradf6b202006-12-08 02:40:27 -080077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/**
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
Randy Dunlap22a95942010-11-26 05:20:25 +000083 * @flags: flags for kmalloc memory allocation
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 *
85 * Allocates memory for a colormap @cmap. @len is the
86 * number of entries in the palette.
87 *
Alan Currydb77ec22006-03-27 01:17:30 -080088 * Returns negative errno on error, or zero on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 *
90 */
91
Dan Carpenter1e7c7802010-11-16 12:11:02 +030092int fb_alloc_cmap_gfp(struct fb_cmap *cmap, int len, int transp, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Dan Carpenterc3531032010-11-13 13:06:38 +030094 int size = len * sizeof(u16);
Dan Carpenter1e7c7802010-11-16 12:11:02 +030095 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Dan Carpenterc3531032010-11-13 13:06:38 +030097 if (cmap->len != len) {
98 fb_dealloc_cmap(cmap);
99 if (!len)
100 return 0;
101
Dan Carpenter1e7c7802010-11-16 12:11:02 +0300102 cmap->red = kmalloc(size, flags);
Dan Carpenterc3531032010-11-13 13:06:38 +0300103 if (!cmap->red)
104 goto fail;
Dan Carpenter1e7c7802010-11-16 12:11:02 +0300105 cmap->green = kmalloc(size, flags);
Dan Carpenterc3531032010-11-13 13:06:38 +0300106 if (!cmap->green)
107 goto fail;
Dan Carpenter1e7c7802010-11-16 12:11:02 +0300108 cmap->blue = kmalloc(size, flags);
Dan Carpenterc3531032010-11-13 13:06:38 +0300109 if (!cmap->blue)
110 goto fail;
111 if (transp) {
Dan Carpenter1e7c7802010-11-16 12:11:02 +0300112 cmap->transp = kmalloc(size, flags);
Dan Carpenterc3531032010-11-13 13:06:38 +0300113 if (!cmap->transp)
114 goto fail;
115 } else {
116 cmap->transp = NULL;
117 }
118 }
119 cmap->start = 0;
120 cmap->len = len;
Dan Carpenter1e7c7802010-11-16 12:11:02 +0300121 ret = fb_copy_cmap(fb_default_cmap(len), cmap);
122 if (ret)
123 goto fail;
Dan Carpenterc3531032010-11-13 13:06:38 +0300124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126fail:
Dan Carpenterc3531032010-11-13 13:06:38 +0300127 fb_dealloc_cmap(cmap);
Dan Carpenter1e7c7802010-11-16 12:11:02 +0300128 return ret;
129}
130
131int fb_alloc_cmap(struct fb_cmap *cmap, int len, int transp)
132{
133 return fb_alloc_cmap_gfp(cmap, len, transp, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
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
145void 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
Helge Delleradf6b202006-12-08 02:40:27 -0800164int fb_copy_cmap(const struct fb_cmap *from, struct fb_cmap *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 int tooff = 0, fromoff = 0;
167 int size;
168
Pawan Kumar03b1b6b2013-10-31 16:19:44 +0530169 if (!to || !from)
170 return -EINVAL;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (to->start > from->start)
173 fromoff = to->start - from->start;
174 else
175 tooff = from->start - to->start;
176 size = to->len - tooff;
177 if (size > (int) (from->len - fromoff))
178 size = from->len - fromoff;
179 if (size <= 0)
180 return -EINVAL;
181 size *= sizeof(u16);
182
Pawan Kumar03b1b6b2013-10-31 16:19:44 +0530183 if (from->red && to->red)
184 memcpy(to->red+tooff, from->red+fromoff, size);
185 if (from->green && to->green)
186 memcpy(to->green+tooff, from->green+fromoff, size);
187 if (from->blue && to->blue)
188 memcpy(to->blue+tooff, from->blue+fromoff, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (from->transp && to->transp)
190 memcpy(to->transp+tooff, from->transp+fromoff, size);
191 return 0;
192}
193
Helge Delleradf6b202006-12-08 02:40:27 -0800194int fb_cmap_to_user(const struct fb_cmap *from, struct fb_cmap_user *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 int tooff = 0, fromoff = 0;
197 int size;
198
Pawan Kumar03b1b6b2013-10-31 16:19:44 +0530199 if (!to || !from)
200 return -EINVAL;
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (to->start > from->start)
203 fromoff = to->start - from->start;
204 else
205 tooff = from->start - to->start;
Shalabh Jaina2b52372013-11-12 15:10:44 -0800206 if ((to->len <= tooff) || (from->len <= fromoff))
207 return -EINVAL;
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 size = to->len - tooff;
Shalabh Jaina2b52372013-11-12 15:10:44 -0800210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (size > (int) (from->len - fromoff))
212 size = from->len - fromoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 size *= sizeof(u16);
214
Pawan Kumar03b1b6b2013-10-31 16:19:44 +0530215 if (from->red && to->red)
216 if (copy_to_user(to->red+tooff, from->red+fromoff, size))
217 return -EFAULT;
218 if (from->green && to->green)
219 if (copy_to_user(to->green+tooff, from->green+fromoff, size))
220 return -EFAULT;
221 if (from->blue && to->blue)
222 if (copy_to_user(to->blue+tooff, from->blue+fromoff, size))
223 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (from->transp && to->transp)
225 if (copy_to_user(to->transp+tooff, from->transp+fromoff, size))
226 return -EFAULT;
227 return 0;
228}
229
230/**
231 * fb_set_cmap - set the colormap
232 * @cmap: frame buffer colormap structure
233 * @info: frame buffer info structure
234 *
235 * Sets the colormap @cmap for a screen of device @info.
236 *
237 * Returns negative errno on error, or zero on success.
238 *
239 */
240
241int fb_set_cmap(struct fb_cmap *cmap, struct fb_info *info)
242{
Michal Januszewski03e259a2005-07-27 11:46:08 -0700243 int i, start, rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 u16 *red, *green, *blue, *transp;
245 u_int hred, hgreen, hblue, htransp = 0xffff;
246
247 red = cmap->red;
248 green = cmap->green;
249 blue = cmap->blue;
250 transp = cmap->transp;
251 start = cmap->start;
252
Benjamin Herrenschmidt71494372005-05-01 08:59:22 -0700253 if (start < 0 || (!info->fbops->fb_setcolreg &&
254 !info->fbops->fb_setcmap))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -EINVAL;
Michal Januszewski03e259a2005-07-27 11:46:08 -0700256 if (info->fbops->fb_setcmap) {
257 rc = info->fbops->fb_setcmap(cmap, info);
258 } else {
259 for (i = 0; i < cmap->len; i++) {
260 hred = *red++;
261 hgreen = *green++;
262 hblue = *blue++;
263 if (transp)
264 htransp = *transp++;
265 if (info->fbops->fb_setcolreg(start++,
266 hred, hgreen, hblue,
267 htransp, info))
268 break;
269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
Michal Januszewski03e259a2005-07-27 11:46:08 -0700271 if (rc == 0)
272 fb_copy_cmap(cmap, &info->cmap);
273
274 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277int fb_set_user_cmap(struct fb_cmap_user *cmap, struct fb_info *info)
278{
Michal Januszewski03e259a2005-07-27 11:46:08 -0700279 int rc, size = cmap->len * sizeof(u16);
280 struct fb_cmap umap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Dan Carpenter1e7c7802010-11-16 12:11:02 +0300282 if (size < 0 || size < cmap->len)
283 return -E2BIG;
284
Michal Januszewski03e259a2005-07-27 11:46:08 -0700285 memset(&umap, 0, sizeof(struct fb_cmap));
Dan Carpenter1e7c7802010-11-16 12:11:02 +0300286 rc = fb_alloc_cmap_gfp(&umap, cmap->len, cmap->transp != NULL,
287 GFP_KERNEL);
Michal Januszewski03e259a2005-07-27 11:46:08 -0700288 if (rc)
Benjamin Herrenschmidt71494372005-05-01 08:59:22 -0700289 return rc;
Michal Januszewski03e259a2005-07-27 11:46:08 -0700290 if (copy_from_user(umap.red, cmap->red, size) ||
291 copy_from_user(umap.green, cmap->green, size) ||
292 copy_from_user(umap.blue, cmap->blue, size) ||
293 (cmap->transp && copy_from_user(umap.transp, cmap->transp, size))) {
Andrea Righi1f5e31d2009-02-04 15:12:03 -0800294 rc = -EFAULT;
295 goto out;
Benjamin Herrenschmidt71494372005-05-01 08:59:22 -0700296 }
Michal Januszewski03e259a2005-07-27 11:46:08 -0700297 umap.start = cmap->start;
Andrea Righi1f5e31d2009-02-04 15:12:03 -0800298 if (!lock_fb_info(info)) {
299 rc = -ENODEV;
300 goto out;
301 }
302 if (cmap->start < 0 || (!info->fbops->fb_setcolreg &&
303 !info->fbops->fb_setcmap)) {
304 rc = -EINVAL;
305 goto out1;
306 }
Michal Januszewski03e259a2005-07-27 11:46:08 -0700307 rc = fb_set_cmap(&umap, info);
Andrea Righi1f5e31d2009-02-04 15:12:03 -0800308out1:
309 unlock_fb_info(info);
310out:
Michal Januszewski03e259a2005-07-27 11:46:08 -0700311 fb_dealloc_cmap(&umap);
312 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315/**
316 * fb_default_cmap - get default colormap
317 * @len: size of palette for a depth
318 *
319 * Gets the default colormap for a specific screen depth. @len
320 * is the size of the palette for a particular screen depth.
321 *
322 * Returns pointer to a frame buffer colormap structure.
323 *
324 */
325
Helge Delleradf6b202006-12-08 02:40:27 -0800326const struct fb_cmap *fb_default_cmap(int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 if (len <= 2)
329 return &default_2_colors;
330 if (len <= 4)
331 return &default_4_colors;
332 if (len <= 8)
333 return &default_8_colors;
334 return &default_16_colors;
335}
336
337
338/**
339 * fb_invert_cmaps - invert all defaults colormaps
340 *
341 * Invert all default colormaps.
342 *
343 */
344
345void fb_invert_cmaps(void)
346{
347 u_int i;
348
Helge Delleradf6b202006-12-08 02:40:27 -0800349 for (i = 0; i < ARRAY_SIZE(red2); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 red2[i] = ~red2[i];
351 green2[i] = ~green2[i];
352 blue2[i] = ~blue2[i];
353 }
Helge Delleradf6b202006-12-08 02:40:27 -0800354 for (i = 0; i < ARRAY_SIZE(red4); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 red4[i] = ~red4[i];
356 green4[i] = ~green4[i];
357 blue4[i] = ~blue4[i];
358 }
Helge Delleradf6b202006-12-08 02:40:27 -0800359 for (i = 0; i < ARRAY_SIZE(red8); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 red8[i] = ~red8[i];
361 green8[i] = ~green8[i];
362 blue8[i] = ~blue8[i];
363 }
Helge Delleradf6b202006-12-08 02:40:27 -0800364 for (i = 0; i < ARRAY_SIZE(red16); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 red16[i] = ~red16[i];
366 green16[i] = ~green16[i];
367 blue16[i] = ~blue16[i];
368 }
369}
370
371
372 /*
373 * Visible symbols for modules
374 */
375
376EXPORT_SYMBOL(fb_alloc_cmap);
377EXPORT_SYMBOL(fb_dealloc_cmap);
378EXPORT_SYMBOL(fb_copy_cmap);
379EXPORT_SYMBOL(fb_set_cmap);
380EXPORT_SYMBOL(fb_default_cmap);
381EXPORT_SYMBOL(fb_invert_cmaps);