fbcmap.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. * @flags: flags for kmalloc memory allocation
  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_gfp(struct fb_cmap *cmap, int len, int transp, gfp_t flags)
  83. {
  84. int size = len * sizeof(u16);
  85. int ret = -ENOMEM;
  86. if (cmap->len != len) {
  87. fb_dealloc_cmap(cmap);
  88. if (!len)
  89. return 0;
  90. cmap->red = kmalloc(size, flags);
  91. if (!cmap->red)
  92. goto fail;
  93. cmap->green = kmalloc(size, flags);
  94. if (!cmap->green)
  95. goto fail;
  96. cmap->blue = kmalloc(size, flags);
  97. if (!cmap->blue)
  98. goto fail;
  99. if (transp) {
  100. cmap->transp = kmalloc(size, flags);
  101. if (!cmap->transp)
  102. goto fail;
  103. } else {
  104. cmap->transp = NULL;
  105. }
  106. }
  107. cmap->start = 0;
  108. cmap->len = len;
  109. ret = fb_copy_cmap(fb_default_cmap(len), cmap);
  110. if (ret)
  111. goto fail;
  112. return 0;
  113. fail:
  114. fb_dealloc_cmap(cmap);
  115. return ret;
  116. }
  117. int fb_alloc_cmap(struct fb_cmap *cmap, int len, int transp)
  118. {
  119. return fb_alloc_cmap_gfp(cmap, len, transp, GFP_ATOMIC);
  120. }
  121. /**
  122. * fb_dealloc_cmap - deallocate a colormap
  123. * @cmap: frame buffer colormap structure
  124. *
  125. * Deallocates a colormap that was previously allocated with
  126. * fb_alloc_cmap().
  127. *
  128. */
  129. void fb_dealloc_cmap(struct fb_cmap *cmap)
  130. {
  131. kfree(cmap->red);
  132. kfree(cmap->green);
  133. kfree(cmap->blue);
  134. kfree(cmap->transp);
  135. cmap->red = cmap->green = cmap->blue = cmap->transp = NULL;
  136. cmap->len = 0;
  137. }
  138. /**
  139. * fb_copy_cmap - copy a colormap
  140. * @from: frame buffer colormap structure
  141. * @to: frame buffer colormap structure
  142. *
  143. * Copy contents of colormap from @from to @to.
  144. */
  145. int fb_copy_cmap(const struct fb_cmap *from, struct fb_cmap *to)
  146. {
  147. unsigned int tooff = 0, fromoff = 0;
  148. size_t size;
  149. if (!to || !from)
  150. return -EINVAL;
  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. if (from->red && to->red)
  162. memcpy(to->red+tooff, from->red+fromoff, size);
  163. if (from->green && to->green)
  164. memcpy(to->green+tooff, from->green+fromoff, size);
  165. if (from->blue && to->blue)
  166. memcpy(to->blue+tooff, from->blue+fromoff, size);
  167. if (from->transp && to->transp)
  168. memcpy(to->transp+tooff, from->transp+fromoff, size);
  169. return 0;
  170. }
  171. int fb_cmap_to_user(const struct fb_cmap *from, struct fb_cmap_user *to)
  172. {
  173. unsigned int tooff = 0, fromoff = 0;
  174. size_t size;
  175. if (!to || !from || (int)(to->start) < 0)
  176. return -EINVAL;
  177. if (to->start > from->start)
  178. fromoff = to->start - from->start;
  179. else
  180. tooff = from->start - to->start;
  181. if (fromoff >= from->len || tooff >= to->len)
  182. return -EINVAL;
  183. size = min_t(size_t, to->len - tooff, from->len - fromoff);
  184. if (size == 0)
  185. return -EINVAL;
  186. size *= sizeof(u16);
  187. if (from->red && to->red)
  188. if (copy_to_user(to->red+tooff, from->red+fromoff, size))
  189. return -EFAULT;
  190. if (from->green && to->green)
  191. if (copy_to_user(to->green+tooff, from->green+fromoff, size))
  192. return -EFAULT;
  193. if (from->blue && to->blue)
  194. if (copy_to_user(to->blue+tooff, from->blue+fromoff, size))
  195. return -EFAULT;
  196. if (from->transp && to->transp)
  197. if (copy_to_user(to->transp+tooff, from->transp+fromoff, size))
  198. return -EFAULT;
  199. return 0;
  200. }
  201. /**
  202. * fb_set_cmap - set the colormap
  203. * @cmap: frame buffer colormap structure
  204. * @info: frame buffer info structure
  205. *
  206. * Sets the colormap @cmap for a screen of device @info.
  207. *
  208. * Returns negative errno on error, or zero on success.
  209. *
  210. */
  211. int fb_set_cmap(struct fb_cmap *cmap, struct fb_info *info)
  212. {
  213. int i, start, rc = 0;
  214. u16 *red, *green, *blue, *transp;
  215. u_int hred, hgreen, hblue, htransp = 0xffff;
  216. red = cmap->red;
  217. green = cmap->green;
  218. blue = cmap->blue;
  219. transp = cmap->transp;
  220. start = cmap->start;
  221. if (start < 0 || (!info->fbops->fb_setcolreg &&
  222. !info->fbops->fb_setcmap))
  223. return -EINVAL;
  224. if (info->fbops->fb_setcmap) {
  225. rc = info->fbops->fb_setcmap(cmap, info);
  226. } else {
  227. for (i = 0; i < cmap->len; i++) {
  228. hred = *red++;
  229. hgreen = *green++;
  230. hblue = *blue++;
  231. if (transp)
  232. htransp = *transp++;
  233. if (info->fbops->fb_setcolreg(start++,
  234. hred, hgreen, hblue,
  235. htransp, info))
  236. break;
  237. }
  238. }
  239. if (rc == 0)
  240. fb_copy_cmap(cmap, &info->cmap);
  241. return rc;
  242. }
  243. int fb_set_user_cmap(struct fb_cmap_user *cmap, struct fb_info *info)
  244. {
  245. int rc, size = cmap->len * sizeof(u16);
  246. struct fb_cmap umap;
  247. if (size < 0 || size < cmap->len)
  248. return -E2BIG;
  249. memset(&umap, 0, sizeof(struct fb_cmap));
  250. rc = fb_alloc_cmap_gfp(&umap, cmap->len, cmap->transp != NULL,
  251. GFP_KERNEL);
  252. if (rc)
  253. return rc;
  254. if (copy_from_user(umap.red, cmap->red, size) ||
  255. copy_from_user(umap.green, cmap->green, size) ||
  256. copy_from_user(umap.blue, cmap->blue, size) ||
  257. (cmap->transp && copy_from_user(umap.transp, cmap->transp, size))) {
  258. rc = -EFAULT;
  259. goto out;
  260. }
  261. umap.start = cmap->start;
  262. if (!lock_fb_info(info)) {
  263. rc = -ENODEV;
  264. goto out;
  265. }
  266. if (cmap->start < 0 || (!info->fbops->fb_setcolreg &&
  267. !info->fbops->fb_setcmap)) {
  268. rc = -EINVAL;
  269. goto out1;
  270. }
  271. rc = fb_set_cmap(&umap, info);
  272. out1:
  273. unlock_fb_info(info);
  274. out:
  275. fb_dealloc_cmap(&umap);
  276. return rc;
  277. }
  278. /**
  279. * fb_default_cmap - get default colormap
  280. * @len: size of palette for a depth
  281. *
  282. * Gets the default colormap for a specific screen depth. @len
  283. * is the size of the palette for a particular screen depth.
  284. *
  285. * Returns pointer to a frame buffer colormap structure.
  286. *
  287. */
  288. const struct fb_cmap *fb_default_cmap(int len)
  289. {
  290. if (len <= 2)
  291. return &default_2_colors;
  292. if (len <= 4)
  293. return &default_4_colors;
  294. if (len <= 8)
  295. return &default_8_colors;
  296. return &default_16_colors;
  297. }
  298. /**
  299. * fb_invert_cmaps - invert all defaults colormaps
  300. *
  301. * Invert all default colormaps.
  302. *
  303. */
  304. void fb_invert_cmaps(void)
  305. {
  306. u_int i;
  307. for (i = 0; i < ARRAY_SIZE(red2); i++) {
  308. red2[i] = ~red2[i];
  309. green2[i] = ~green2[i];
  310. blue2[i] = ~blue2[i];
  311. }
  312. for (i = 0; i < ARRAY_SIZE(red4); i++) {
  313. red4[i] = ~red4[i];
  314. green4[i] = ~green4[i];
  315. blue4[i] = ~blue4[i];
  316. }
  317. for (i = 0; i < ARRAY_SIZE(red8); i++) {
  318. red8[i] = ~red8[i];
  319. green8[i] = ~green8[i];
  320. blue8[i] = ~blue8[i];
  321. }
  322. for (i = 0; i < ARRAY_SIZE(red16); i++) {
  323. red16[i] = ~red16[i];
  324. green16[i] = ~green16[i];
  325. blue16[i] = ~blue16[i];
  326. }
  327. }
  328. /*
  329. * Visible symbols for modules
  330. */
  331. EXPORT_SYMBOL(fb_alloc_cmap);
  332. EXPORT_SYMBOL(fb_dealloc_cmap);
  333. EXPORT_SYMBOL(fb_copy_cmap);
  334. EXPORT_SYMBOL(fb_set_cmap);
  335. EXPORT_SYMBOL(fb_default_cmap);
  336. EXPORT_SYMBOL(fb_invert_cmaps);