fbcmap.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 -1 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 -1;
  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;
  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. return -EINVAL;
  201. for (i = 0; i < cmap->len; i++) {
  202. hred = *red++;
  203. hgreen = *green++;
  204. hblue = *blue++;
  205. if (transp)
  206. htransp = *transp++;
  207. if (info->fbops->fb_setcolreg(start++,
  208. hred, hgreen, hblue, htransp,
  209. info))
  210. break;
  211. }
  212. return 0;
  213. }
  214. int fb_set_user_cmap(struct fb_cmap_user *cmap, struct fb_info *info)
  215. {
  216. int i, start;
  217. u16 __user *red, *green, *blue, *transp;
  218. u_int hred, hgreen, hblue, htransp = 0xffff;
  219. red = cmap->red;
  220. green = cmap->green;
  221. blue = cmap->blue;
  222. transp = cmap->transp;
  223. start = cmap->start;
  224. if (start < 0 || !info->fbops->fb_setcolreg)
  225. return -EINVAL;
  226. for (i = 0; i < cmap->len; i++, red++, blue++, green++) {
  227. if (get_user(hred, red) ||
  228. get_user(hgreen, green) ||
  229. get_user(hblue, blue) ||
  230. (transp && get_user(htransp, transp)))
  231. return -EFAULT;
  232. if (info->fbops->fb_setcolreg(start++,
  233. hred, hgreen, hblue, htransp,
  234. info))
  235. return 0;
  236. if (transp)
  237. transp++;
  238. }
  239. return 0;
  240. }
  241. /**
  242. * fb_default_cmap - get default colormap
  243. * @len: size of palette for a depth
  244. *
  245. * Gets the default colormap for a specific screen depth. @len
  246. * is the size of the palette for a particular screen depth.
  247. *
  248. * Returns pointer to a frame buffer colormap structure.
  249. *
  250. */
  251. struct fb_cmap *fb_default_cmap(int len)
  252. {
  253. if (len <= 2)
  254. return &default_2_colors;
  255. if (len <= 4)
  256. return &default_4_colors;
  257. if (len <= 8)
  258. return &default_8_colors;
  259. return &default_16_colors;
  260. }
  261. /**
  262. * fb_invert_cmaps - invert all defaults colormaps
  263. *
  264. * Invert all default colormaps.
  265. *
  266. */
  267. void fb_invert_cmaps(void)
  268. {
  269. u_int i;
  270. for (i = 0; i < 2; i++) {
  271. red2[i] = ~red2[i];
  272. green2[i] = ~green2[i];
  273. blue2[i] = ~blue2[i];
  274. }
  275. for (i = 0; i < 4; i++) {
  276. red4[i] = ~red4[i];
  277. green4[i] = ~green4[i];
  278. blue4[i] = ~blue4[i];
  279. }
  280. for (i = 0; i < 8; i++) {
  281. red8[i] = ~red8[i];
  282. green8[i] = ~green8[i];
  283. blue8[i] = ~blue8[i];
  284. }
  285. for (i = 0; i < 16; i++) {
  286. red16[i] = ~red16[i];
  287. green16[i] = ~green16[i];
  288. blue16[i] = ~blue16[i];
  289. }
  290. }
  291. /*
  292. * Visible symbols for modules
  293. */
  294. EXPORT_SYMBOL(fb_alloc_cmap);
  295. EXPORT_SYMBOL(fb_dealloc_cmap);
  296. EXPORT_SYMBOL(fb_copy_cmap);
  297. EXPORT_SYMBOL(fb_set_cmap);
  298. EXPORT_SYMBOL(fb_default_cmap);
  299. EXPORT_SYMBOL(fb_invert_cmaps);