fbcmap.c 8.4 KB

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