fbcmap.c 7.8 KB

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