fbcmap.c 8.3 KB

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