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 | |
| 21 | static u16 red2[] = { |
| 22 | 0x0000, 0xaaaa |
| 23 | }; |
| 24 | static u16 green2[] = { |
| 25 | 0x0000, 0xaaaa |
| 26 | }; |
| 27 | static u16 blue2[] = { |
| 28 | 0x0000, 0xaaaa |
| 29 | }; |
| 30 | |
| 31 | static u16 red4[] = { |
| 32 | 0x0000, 0xaaaa, 0x5555, 0xffff |
| 33 | }; |
| 34 | static u16 green4[] = { |
| 35 | 0x0000, 0xaaaa, 0x5555, 0xffff |
| 36 | }; |
| 37 | static u16 blue4[] = { |
| 38 | 0x0000, 0xaaaa, 0x5555, 0xffff |
| 39 | }; |
| 40 | |
| 41 | static u16 red8[] = { |
| 42 | 0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa |
| 43 | }; |
| 44 | static u16 green8[] = { |
| 45 | 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa |
| 46 | }; |
| 47 | static u16 blue8[] = { |
| 48 | 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa |
| 49 | }; |
| 50 | |
| 51 | static u16 red16[] = { |
| 52 | 0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa, |
| 53 | 0x5555, 0x5555, 0x5555, 0x5555, 0xffff, 0xffff, 0xffff, 0xffff |
| 54 | }; |
| 55 | static u16 green16[] = { |
| 56 | 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa, |
| 57 | 0x5555, 0x5555, 0xffff, 0xffff, 0x5555, 0x5555, 0xffff, 0xffff |
| 58 | }; |
| 59 | static u16 blue16[] = { |
| 60 | 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, |
| 61 | 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff |
| 62 | }; |
| 63 | |
| 64 | static struct fb_cmap default_2_colors = { |
| 65 | 0, 2, red2, green2, blue2, NULL |
| 66 | }; |
| 67 | static struct fb_cmap default_8_colors = { |
| 68 | 0, 8, red8, green8, blue8, NULL |
| 69 | }; |
| 70 | static struct fb_cmap default_4_colors = { |
| 71 | 0, 4, red4, green4, blue4, NULL |
| 72 | }; |
| 73 | static struct fb_cmap default_16_colors = { |
| 74 | 0, 16, red16, green16, blue16, NULL |
| 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 | * |
Alan Curry | db77ec2 | 2006-03-27 01:17:30 -0800 | [diff] [blame] | 87 | * Returns negative errno on error, or zero on success. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | * |
| 89 | */ |
| 90 | |
| 91 | int fb_alloc_cmap(struct fb_cmap *cmap, int len, int transp) |
| 92 | { |
| 93 | int size = len*sizeof(u16); |
| 94 | |
| 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; |
| 110 | } |
| 111 | cmap->start = 0; |
| 112 | cmap->len = len; |
| 113 | fb_copy_cmap(fb_default_cmap(len), cmap); |
| 114 | return 0; |
| 115 | |
| 116 | fail: |
| 117 | fb_dealloc_cmap(cmap); |
Alan Curry | db77ec2 | 2006-03-27 01:17:30 -0800 | [diff] [blame] | 118 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * fb_dealloc_cmap - deallocate a colormap |
| 123 | * @cmap: frame buffer colormap structure |
| 124 | * |
| 125 | * Deallocates a colormap that was previously allocated with |
| 126 | * fb_alloc_cmap(). |
| 127 | * |
| 128 | */ |
| 129 | |
| 130 | void fb_dealloc_cmap(struct fb_cmap *cmap) |
| 131 | { |
| 132 | kfree(cmap->red); |
| 133 | kfree(cmap->green); |
| 134 | kfree(cmap->blue); |
| 135 | kfree(cmap->transp); |
| 136 | |
| 137 | cmap->red = cmap->green = cmap->blue = cmap->transp = NULL; |
| 138 | cmap->len = 0; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * fb_copy_cmap - copy a colormap |
| 143 | * @from: frame buffer colormap structure |
| 144 | * @to: frame buffer colormap structure |
| 145 | * |
| 146 | * Copy contents of colormap from @from to @to. |
| 147 | */ |
| 148 | |
| 149 | int fb_copy_cmap(struct fb_cmap *from, struct fb_cmap *to) |
| 150 | { |
| 151 | int tooff = 0, fromoff = 0; |
| 152 | int size; |
| 153 | |
| 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); |
| 164 | |
| 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; |
| 171 | } |
| 172 | |
| 173 | int fb_cmap_to_user(struct fb_cmap *from, struct fb_cmap_user *to) |
| 174 | { |
| 175 | int tooff = 0, fromoff = 0; |
| 176 | int size; |
| 177 | |
| 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); |
| 188 | |
| 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; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * fb_set_cmap - set the colormap |
| 203 | * @cmap: frame buffer colormap structure |
| 204 | * @info: frame buffer info structure |
| 205 | * |
| 206 | * Sets the colormap @cmap for a screen of device @info. |
| 207 | * |
| 208 | * Returns negative errno on error, or zero on success. |
| 209 | * |
| 210 | */ |
| 211 | |
| 212 | int fb_set_cmap(struct fb_cmap *cmap, struct fb_info *info) |
| 213 | { |
Michal Januszewski | 03e259a | 2005-07-27 11:46:08 -0700 | [diff] [blame] | 214 | int i, start, rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | u16 *red, *green, *blue, *transp; |
| 216 | u_int hred, hgreen, hblue, htransp = 0xffff; |
| 217 | |
| 218 | red = cmap->red; |
| 219 | green = cmap->green; |
| 220 | blue = cmap->blue; |
| 221 | transp = cmap->transp; |
| 222 | start = cmap->start; |
| 223 | |
Benjamin Herrenschmidt | 7149437 | 2005-05-01 08:59:22 -0700 | [diff] [blame] | 224 | if (start < 0 || (!info->fbops->fb_setcolreg && |
| 225 | !info->fbops->fb_setcmap)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | return -EINVAL; |
Michal Januszewski | 03e259a | 2005-07-27 11:46:08 -0700 | [diff] [blame] | 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; |
| 240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
Michal Januszewski | 03e259a | 2005-07-27 11:46:08 -0700 | [diff] [blame] | 242 | if (rc == 0) |
| 243 | fb_copy_cmap(cmap, &info->cmap); |
| 244 | |
| 245 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | int fb_set_user_cmap(struct fb_cmap_user *cmap, struct fb_info *info) |
| 249 | { |
Michal Januszewski | 03e259a | 2005-07-27 11:46:08 -0700 | [diff] [blame] | 250 | int rc, size = cmap->len * sizeof(u16); |
| 251 | struct fb_cmap umap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Michal Januszewski | 03e259a | 2005-07-27 11:46:08 -0700 | [diff] [blame] | 253 | if (cmap->start < 0 || (!info->fbops->fb_setcolreg && |
| 254 | !info->fbops->fb_setcmap)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | return -EINVAL; |
Benjamin Herrenschmidt | 7149437 | 2005-05-01 08:59:22 -0700 | [diff] [blame] | 256 | |
Michal Januszewski | 03e259a | 2005-07-27 11:46:08 -0700 | [diff] [blame] | 257 | memset(&umap, 0, sizeof(struct fb_cmap)); |
| 258 | rc = fb_alloc_cmap(&umap, cmap->len, cmap->transp != NULL); |
| 259 | if (rc) |
Benjamin Herrenschmidt | 7149437 | 2005-05-01 08:59:22 -0700 | [diff] [blame] | 260 | return rc; |
Michal Januszewski | 03e259a | 2005-07-27 11:46:08 -0700 | [diff] [blame] | 261 | if (copy_from_user(umap.red, cmap->red, size) || |
| 262 | copy_from_user(umap.green, cmap->green, size) || |
| 263 | copy_from_user(umap.blue, cmap->blue, size) || |
| 264 | (cmap->transp && copy_from_user(umap.transp, cmap->transp, size))) { |
| 265 | fb_dealloc_cmap(&umap); |
| 266 | return -EFAULT; |
Benjamin Herrenschmidt | 7149437 | 2005-05-01 08:59:22 -0700 | [diff] [blame] | 267 | } |
Michal Januszewski | 03e259a | 2005-07-27 11:46:08 -0700 | [diff] [blame] | 268 | umap.start = cmap->start; |
| 269 | rc = fb_set_cmap(&umap, info); |
| 270 | fb_dealloc_cmap(&umap); |
| 271 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
| 275 | * fb_default_cmap - get default colormap |
| 276 | * @len: size of palette for a depth |
| 277 | * |
| 278 | * Gets the default colormap for a specific screen depth. @len |
| 279 | * is the size of the palette for a particular screen depth. |
| 280 | * |
| 281 | * Returns pointer to a frame buffer colormap structure. |
| 282 | * |
| 283 | */ |
| 284 | |
| 285 | struct fb_cmap *fb_default_cmap(int len) |
| 286 | { |
| 287 | if (len <= 2) |
| 288 | return &default_2_colors; |
| 289 | if (len <= 4) |
| 290 | return &default_4_colors; |
| 291 | if (len <= 8) |
| 292 | return &default_8_colors; |
| 293 | return &default_16_colors; |
| 294 | } |
| 295 | |
| 296 | |
| 297 | /** |
| 298 | * fb_invert_cmaps - invert all defaults colormaps |
| 299 | * |
| 300 | * Invert all default colormaps. |
| 301 | * |
| 302 | */ |
| 303 | |
| 304 | void fb_invert_cmaps(void) |
| 305 | { |
| 306 | u_int i; |
| 307 | |
| 308 | for (i = 0; i < 2; i++) { |
| 309 | red2[i] = ~red2[i]; |
| 310 | green2[i] = ~green2[i]; |
| 311 | blue2[i] = ~blue2[i]; |
| 312 | } |
| 313 | for (i = 0; i < 4; i++) { |
| 314 | red4[i] = ~red4[i]; |
| 315 | green4[i] = ~green4[i]; |
| 316 | blue4[i] = ~blue4[i]; |
| 317 | } |
| 318 | for (i = 0; i < 8; i++) { |
| 319 | red8[i] = ~red8[i]; |
| 320 | green8[i] = ~green8[i]; |
| 321 | blue8[i] = ~blue8[i]; |
| 322 | } |
| 323 | for (i = 0; i < 16; i++) { |
| 324 | red16[i] = ~red16[i]; |
| 325 | green16[i] = ~green16[i]; |
| 326 | blue16[i] = ~blue16[i]; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | |
| 331 | /* |
| 332 | * Visible symbols for modules |
| 333 | */ |
| 334 | |
| 335 | EXPORT_SYMBOL(fb_alloc_cmap); |
| 336 | EXPORT_SYMBOL(fb_dealloc_cmap); |
| 337 | EXPORT_SYMBOL(fb_copy_cmap); |
| 338 | EXPORT_SYMBOL(fb_set_cmap); |
| 339 | EXPORT_SYMBOL(fb_default_cmap); |
| 340 | EXPORT_SYMBOL(fb_invert_cmaps); |