Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/fb.h> |
| 17 | #include <linux/slab.h> |
| 18 | |
| 19 | #include <asm/uaccess.h> |
| 20 | |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 21 | static u16 red2[] __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | 0x0000, 0xaaaa |
| 23 | }; |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 24 | static u16 green2[] __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | 0x0000, 0xaaaa |
| 26 | }; |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 27 | static u16 blue2[] __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | 0x0000, 0xaaaa |
| 29 | }; |
| 30 | |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 31 | static u16 red4[] __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | 0x0000, 0xaaaa, 0x5555, 0xffff |
| 33 | }; |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 34 | static u16 green4[] __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | 0x0000, 0xaaaa, 0x5555, 0xffff |
| 36 | }; |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 37 | static u16 blue4[] __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | 0x0000, 0xaaaa, 0x5555, 0xffff |
| 39 | }; |
| 40 | |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 41 | static u16 red8[] __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | 0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa |
| 43 | }; |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 44 | static u16 green8[] __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa |
| 46 | }; |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 47 | static u16 blue8[] __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa |
| 49 | }; |
| 50 | |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 51 | static u16 red16[] __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | 0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa, |
| 53 | 0x5555, 0x5555, 0x5555, 0x5555, 0xffff, 0xffff, 0xffff, 0xffff |
| 54 | }; |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 55 | static u16 green16[] __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa, |
| 57 | 0x5555, 0x5555, 0xffff, 0xffff, 0x5555, 0x5555, 0xffff, 0xffff |
| 58 | }; |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 59 | static u16 blue16[] __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, |
| 61 | 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff |
| 62 | }; |
| 63 | |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 64 | static const struct fb_cmap default_2_colors = { |
| 65 | .len=2, .red=red2, .green=green2, .blue=blue2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | }; |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 67 | static const struct fb_cmap default_8_colors = { |
| 68 | .len=8, .red=red8, .green=green8, .blue=blue8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | }; |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 70 | static const struct fb_cmap default_4_colors = { |
| 71 | .len=4, .red=red4, .green=green4, .blue=blue4 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | }; |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 73 | static const struct fb_cmap default_16_colors = { |
| 74 | .len=16, .red=red16, .green=green16, .blue=blue16 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Alan Curry | db77ec2 | 2006-03-27 01:17:30 -0800 | [diff] [blame] | 88 | * Returns negative errno on error, or zero on success. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
| 115 | return 0; |
| 116 | |
| 117 | fail: |
| 118 | fb_dealloc_cmap(cmap); |
Alan Curry | db77ec2 | 2006-03-27 01:17:30 -0800 | [diff] [blame] | 119 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
| 147 | * Copy contents of colormap from @from to @to. |
| 148 | */ |
| 149 | |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 150 | int fb_copy_cmap(const struct fb_cmap *from, struct fb_cmap *to) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { |
| 152 | int tooff = 0, fromoff = 0; |
| 153 | int size; |
| 154 | |
| 155 | if (to->start > from->start) |
| 156 | fromoff = to->start - from->start; |
| 157 | else |
| 158 | tooff = from->start - to->start; |
| 159 | size = to->len - tooff; |
| 160 | if (size > (int) (from->len - fromoff)) |
| 161 | size = from->len - fromoff; |
| 162 | if (size <= 0) |
| 163 | return -EINVAL; |
| 164 | size *= sizeof(u16); |
| 165 | |
| 166 | memcpy(to->red+tooff, from->red+fromoff, size); |
| 167 | memcpy(to->green+tooff, from->green+fromoff, size); |
| 168 | memcpy(to->blue+tooff, from->blue+fromoff, size); |
| 169 | if (from->transp && to->transp) |
| 170 | memcpy(to->transp+tooff, from->transp+fromoff, size); |
| 171 | return 0; |
| 172 | } |
| 173 | |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 174 | int fb_cmap_to_user(const struct fb_cmap *from, struct fb_cmap_user *to) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
| 176 | int tooff = 0, fromoff = 0; |
| 177 | int size; |
| 178 | |
| 179 | if (to->start > from->start) |
| 180 | fromoff = to->start - from->start; |
| 181 | else |
| 182 | tooff = from->start - to->start; |
| 183 | size = to->len - tooff; |
| 184 | if (size > (int) (from->len - fromoff)) |
| 185 | size = from->len - fromoff; |
| 186 | if (size <= 0) |
| 187 | return -EINVAL; |
| 188 | size *= sizeof(u16); |
| 189 | |
| 190 | if (copy_to_user(to->red+tooff, from->red+fromoff, size)) |
| 191 | return -EFAULT; |
| 192 | if (copy_to_user(to->green+tooff, from->green+fromoff, size)) |
| 193 | return -EFAULT; |
| 194 | if (copy_to_user(to->blue+tooff, from->blue+fromoff, size)) |
| 195 | return -EFAULT; |
| 196 | if (from->transp && to->transp) |
| 197 | if (copy_to_user(to->transp+tooff, from->transp+fromoff, size)) |
| 198 | return -EFAULT; |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * fb_set_cmap - set the colormap |
| 204 | * @cmap: frame buffer colormap structure |
| 205 | * @info: frame buffer info structure |
| 206 | * |
| 207 | * Sets the colormap @cmap for a screen of device @info. |
| 208 | * |
| 209 | * Returns negative errno on error, or zero on success. |
| 210 | * |
| 211 | */ |
| 212 | |
| 213 | int fb_set_cmap(struct fb_cmap *cmap, struct fb_info *info) |
| 214 | { |
Michal Januszewski | 03e259a | 2005-07-27 11:46:08 -0700 | [diff] [blame] | 215 | int i, start, rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | u16 *red, *green, *blue, *transp; |
| 217 | u_int hred, hgreen, hblue, htransp = 0xffff; |
| 218 | |
| 219 | red = cmap->red; |
| 220 | green = cmap->green; |
| 221 | blue = cmap->blue; |
| 222 | transp = cmap->transp; |
| 223 | start = cmap->start; |
| 224 | |
Benjamin Herrenschmidt | 7149437 | 2005-05-01 08:59:22 -0700 | [diff] [blame] | 225 | if (start < 0 || (!info->fbops->fb_setcolreg && |
| 226 | !info->fbops->fb_setcmap)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | return -EINVAL; |
Michal Januszewski | 03e259a | 2005-07-27 11:46:08 -0700 | [diff] [blame] | 228 | if (info->fbops->fb_setcmap) { |
| 229 | rc = info->fbops->fb_setcmap(cmap, info); |
| 230 | } else { |
| 231 | for (i = 0; i < cmap->len; i++) { |
| 232 | hred = *red++; |
| 233 | hgreen = *green++; |
| 234 | hblue = *blue++; |
| 235 | if (transp) |
| 236 | htransp = *transp++; |
| 237 | if (info->fbops->fb_setcolreg(start++, |
| 238 | hred, hgreen, hblue, |
| 239 | htransp, info)) |
| 240 | break; |
| 241 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
Michal Januszewski | 03e259a | 2005-07-27 11:46:08 -0700 | [diff] [blame] | 243 | if (rc == 0) |
| 244 | fb_copy_cmap(cmap, &info->cmap); |
| 245 | |
| 246 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | int fb_set_user_cmap(struct fb_cmap_user *cmap, struct fb_info *info) |
| 250 | { |
Michal Januszewski | 03e259a | 2005-07-27 11:46:08 -0700 | [diff] [blame] | 251 | int rc, size = cmap->len * sizeof(u16); |
| 252 | struct fb_cmap umap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Michal Januszewski | 03e259a | 2005-07-27 11:46:08 -0700 | [diff] [blame] | 254 | if (cmap->start < 0 || (!info->fbops->fb_setcolreg && |
| 255 | !info->fbops->fb_setcmap)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | return -EINVAL; |
Benjamin Herrenschmidt | 7149437 | 2005-05-01 08:59:22 -0700 | [diff] [blame] | 257 | |
Michal Januszewski | 03e259a | 2005-07-27 11:46:08 -0700 | [diff] [blame] | 258 | memset(&umap, 0, sizeof(struct fb_cmap)); |
| 259 | rc = fb_alloc_cmap(&umap, cmap->len, cmap->transp != NULL); |
| 260 | if (rc) |
Benjamin Herrenschmidt | 7149437 | 2005-05-01 08:59:22 -0700 | [diff] [blame] | 261 | return rc; |
Michal Januszewski | 03e259a | 2005-07-27 11:46:08 -0700 | [diff] [blame] | 262 | if (copy_from_user(umap.red, cmap->red, size) || |
| 263 | copy_from_user(umap.green, cmap->green, size) || |
| 264 | copy_from_user(umap.blue, cmap->blue, size) || |
| 265 | (cmap->transp && copy_from_user(umap.transp, cmap->transp, size))) { |
| 266 | fb_dealloc_cmap(&umap); |
| 267 | return -EFAULT; |
Benjamin Herrenschmidt | 7149437 | 2005-05-01 08:59:22 -0700 | [diff] [blame] | 268 | } |
Michal Januszewski | 03e259a | 2005-07-27 11:46:08 -0700 | [diff] [blame] | 269 | umap.start = cmap->start; |
| 270 | rc = fb_set_cmap(&umap, info); |
| 271 | fb_dealloc_cmap(&umap); |
| 272 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /** |
| 276 | * fb_default_cmap - get default colormap |
| 277 | * @len: size of palette for a depth |
| 278 | * |
| 279 | * Gets the default colormap for a specific screen depth. @len |
| 280 | * is the size of the palette for a particular screen depth. |
| 281 | * |
| 282 | * Returns pointer to a frame buffer colormap structure. |
| 283 | * |
| 284 | */ |
| 285 | |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 286 | const struct fb_cmap *fb_default_cmap(int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | { |
| 288 | if (len <= 2) |
| 289 | return &default_2_colors; |
| 290 | if (len <= 4) |
| 291 | return &default_4_colors; |
| 292 | if (len <= 8) |
| 293 | return &default_8_colors; |
| 294 | return &default_16_colors; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | /** |
| 299 | * fb_invert_cmaps - invert all defaults colormaps |
| 300 | * |
| 301 | * Invert all default colormaps. |
| 302 | * |
| 303 | */ |
| 304 | |
| 305 | void fb_invert_cmaps(void) |
| 306 | { |
| 307 | u_int i; |
| 308 | |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 309 | for (i = 0; i < ARRAY_SIZE(red2); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | red2[i] = ~red2[i]; |
| 311 | green2[i] = ~green2[i]; |
| 312 | blue2[i] = ~blue2[i]; |
| 313 | } |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 314 | for (i = 0; i < ARRAY_SIZE(red4); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | red4[i] = ~red4[i]; |
| 316 | green4[i] = ~green4[i]; |
| 317 | blue4[i] = ~blue4[i]; |
| 318 | } |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 319 | for (i = 0; i < ARRAY_SIZE(red8); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | red8[i] = ~red8[i]; |
| 321 | green8[i] = ~green8[i]; |
| 322 | blue8[i] = ~blue8[i]; |
| 323 | } |
Helge Deller | adf6b20 | 2006-12-08 02:40:27 -0800 | [diff] [blame] | 324 | for (i = 0; i < ARRAY_SIZE(red16); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | red16[i] = ~red16[i]; |
| 326 | green16[i] = ~green16[i]; |
| 327 | blue16[i] = ~blue16[i]; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | |
| 332 | /* |
| 333 | * Visible symbols for modules |
| 334 | */ |
| 335 | |
| 336 | EXPORT_SYMBOL(fb_alloc_cmap); |
| 337 | EXPORT_SYMBOL(fb_dealloc_cmap); |
| 338 | EXPORT_SYMBOL(fb_copy_cmap); |
| 339 | EXPORT_SYMBOL(fb_set_cmap); |
| 340 | EXPORT_SYMBOL(fb_default_cmap); |
| 341 | EXPORT_SYMBOL(fb_invert_cmaps); |