fbcmap.c 8.4 KB

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