fbmem.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780
  1. /*
  2. * linux/drivers/video/fbmem.c
  3. *
  4. * Copyright (C) 1994 Martin Schaller
  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
  11. * for more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/compat.h>
  15. #include <linux/types.h>
  16. #include <linux/errno.h>
  17. #include <linux/smp_lock.h>
  18. #include <linux/kernel.h>
  19. #include <linux/major.h>
  20. #include <linux/slab.h>
  21. #include <linux/mm.h>
  22. #include <linux/mman.h>
  23. #include <linux/vt.h>
  24. #include <linux/init.h>
  25. #include <linux/linux_logo.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/console.h>
  29. #include <linux/kmod.h>
  30. #include <linux/err.h>
  31. #include <linux/device.h>
  32. #include <linux/efi.h>
  33. #include <linux/fb.h>
  34. #include <asm/fb.h>
  35. /*
  36. * Frame buffer device initialization and setup routines
  37. */
  38. #define FBPIXMAPSIZE (1024 * 8)
  39. struct fb_info *registered_fb[FB_MAX] __read_mostly;
  40. int num_registered_fb __read_mostly;
  41. /*
  42. * Helpers
  43. */
  44. int fb_get_color_depth(struct fb_var_screeninfo *var,
  45. struct fb_fix_screeninfo *fix)
  46. {
  47. int depth = 0;
  48. if (fix->visual == FB_VISUAL_MONO01 ||
  49. fix->visual == FB_VISUAL_MONO10)
  50. depth = 1;
  51. else {
  52. if (var->green.length == var->blue.length &&
  53. var->green.length == var->red.length &&
  54. var->green.offset == var->blue.offset &&
  55. var->green.offset == var->red.offset)
  56. depth = var->green.length;
  57. else
  58. depth = var->green.length + var->red.length +
  59. var->blue.length;
  60. }
  61. return depth;
  62. }
  63. EXPORT_SYMBOL(fb_get_color_depth);
  64. /*
  65. * Data padding functions.
  66. */
  67. void fb_pad_aligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 s_pitch, u32 height)
  68. {
  69. __fb_pad_aligned_buffer(dst, d_pitch, src, s_pitch, height);
  70. }
  71. EXPORT_SYMBOL(fb_pad_aligned_buffer);
  72. void fb_pad_unaligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 idx, u32 height,
  73. u32 shift_high, u32 shift_low, u32 mod)
  74. {
  75. u8 mask = (u8) (0xfff << shift_high), tmp;
  76. int i, j;
  77. for (i = height; i--; ) {
  78. for (j = 0; j < idx; j++) {
  79. tmp = dst[j];
  80. tmp &= mask;
  81. tmp |= *src >> shift_low;
  82. dst[j] = tmp;
  83. tmp = *src << shift_high;
  84. dst[j+1] = tmp;
  85. src++;
  86. }
  87. tmp = dst[idx];
  88. tmp &= mask;
  89. tmp |= *src >> shift_low;
  90. dst[idx] = tmp;
  91. if (shift_high < mod) {
  92. tmp = *src << shift_high;
  93. dst[idx+1] = tmp;
  94. }
  95. src++;
  96. dst += d_pitch;
  97. }
  98. }
  99. EXPORT_SYMBOL(fb_pad_unaligned_buffer);
  100. /*
  101. * we need to lock this section since fb_cursor
  102. * may use fb_imageblit()
  103. */
  104. char* fb_get_buffer_offset(struct fb_info *info, struct fb_pixmap *buf, u32 size)
  105. {
  106. u32 align = buf->buf_align - 1, offset;
  107. char *addr = buf->addr;
  108. /* If IO mapped, we need to sync before access, no sharing of
  109. * the pixmap is done
  110. */
  111. if (buf->flags & FB_PIXMAP_IO) {
  112. if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
  113. info->fbops->fb_sync(info);
  114. return addr;
  115. }
  116. /* See if we fit in the remaining pixmap space */
  117. offset = buf->offset + align;
  118. offset &= ~align;
  119. if (offset + size > buf->size) {
  120. /* We do not fit. In order to be able to re-use the buffer,
  121. * we must ensure no asynchronous DMA'ing or whatever operation
  122. * is in progress, we sync for that.
  123. */
  124. if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
  125. info->fbops->fb_sync(info);
  126. offset = 0;
  127. }
  128. buf->offset = offset + size;
  129. addr += offset;
  130. return addr;
  131. }
  132. #ifdef CONFIG_LOGO
  133. static inline unsigned safe_shift(unsigned d, int n)
  134. {
  135. return n < 0 ? d >> -n : d << n;
  136. }
  137. static void fb_set_logocmap(struct fb_info *info,
  138. const struct linux_logo *logo)
  139. {
  140. struct fb_cmap palette_cmap;
  141. u16 palette_green[16];
  142. u16 palette_blue[16];
  143. u16 palette_red[16];
  144. int i, j, n;
  145. const unsigned char *clut = logo->clut;
  146. palette_cmap.start = 0;
  147. palette_cmap.len = 16;
  148. palette_cmap.red = palette_red;
  149. palette_cmap.green = palette_green;
  150. palette_cmap.blue = palette_blue;
  151. palette_cmap.transp = NULL;
  152. for (i = 0; i < logo->clutsize; i += n) {
  153. n = logo->clutsize - i;
  154. /* palette_cmap provides space for only 16 colors at once */
  155. if (n > 16)
  156. n = 16;
  157. palette_cmap.start = 32 + i;
  158. palette_cmap.len = n;
  159. for (j = 0; j < n; ++j) {
  160. palette_cmap.red[j] = clut[0] << 8 | clut[0];
  161. palette_cmap.green[j] = clut[1] << 8 | clut[1];
  162. palette_cmap.blue[j] = clut[2] << 8 | clut[2];
  163. clut += 3;
  164. }
  165. fb_set_cmap(&palette_cmap, info);
  166. }
  167. }
  168. static void fb_set_logo_truepalette(struct fb_info *info,
  169. const struct linux_logo *logo,
  170. u32 *palette)
  171. {
  172. static const unsigned char mask[] = { 0,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff };
  173. unsigned char redmask, greenmask, bluemask;
  174. int redshift, greenshift, blueshift;
  175. int i;
  176. const unsigned char *clut = logo->clut;
  177. /*
  178. * We have to create a temporary palette since console palette is only
  179. * 16 colors long.
  180. */
  181. /* Bug: Doesn't obey msb_right ... (who needs that?) */
  182. redmask = mask[info->var.red.length < 8 ? info->var.red.length : 8];
  183. greenmask = mask[info->var.green.length < 8 ? info->var.green.length : 8];
  184. bluemask = mask[info->var.blue.length < 8 ? info->var.blue.length : 8];
  185. redshift = info->var.red.offset - (8 - info->var.red.length);
  186. greenshift = info->var.green.offset - (8 - info->var.green.length);
  187. blueshift = info->var.blue.offset - (8 - info->var.blue.length);
  188. for ( i = 0; i < logo->clutsize; i++) {
  189. palette[i+32] = (safe_shift((clut[0] & redmask), redshift) |
  190. safe_shift((clut[1] & greenmask), greenshift) |
  191. safe_shift((clut[2] & bluemask), blueshift));
  192. clut += 3;
  193. }
  194. }
  195. static void fb_set_logo_directpalette(struct fb_info *info,
  196. const struct linux_logo *logo,
  197. u32 *palette)
  198. {
  199. int redshift, greenshift, blueshift;
  200. int i;
  201. redshift = info->var.red.offset;
  202. greenshift = info->var.green.offset;
  203. blueshift = info->var.blue.offset;
  204. for (i = 32; i < 32 + logo->clutsize; i++)
  205. palette[i] = i << redshift | i << greenshift | i << blueshift;
  206. }
  207. static void fb_set_logo(struct fb_info *info,
  208. const struct linux_logo *logo, u8 *dst,
  209. int depth)
  210. {
  211. int i, j, k;
  212. const u8 *src = logo->data;
  213. u8 xor = (info->fix.visual == FB_VISUAL_MONO01) ? 0xff : 0;
  214. u8 fg = 1, d;
  215. switch (fb_get_color_depth(&info->var, &info->fix)) {
  216. case 1:
  217. fg = 1;
  218. break;
  219. case 2:
  220. fg = 3;
  221. break;
  222. default:
  223. fg = 7;
  224. break;
  225. }
  226. if (info->fix.visual == FB_VISUAL_MONO01 ||
  227. info->fix.visual == FB_VISUAL_MONO10)
  228. fg = ~((u8) (0xfff << info->var.green.length));
  229. switch (depth) {
  230. case 4:
  231. for (i = 0; i < logo->height; i++)
  232. for (j = 0; j < logo->width; src++) {
  233. *dst++ = *src >> 4;
  234. j++;
  235. if (j < logo->width) {
  236. *dst++ = *src & 0x0f;
  237. j++;
  238. }
  239. }
  240. break;
  241. case 1:
  242. for (i = 0; i < logo->height; i++) {
  243. for (j = 0; j < logo->width; src++) {
  244. d = *src ^ xor;
  245. for (k = 7; k >= 0; k--) {
  246. *dst++ = ((d >> k) & 1) ? fg : 0;
  247. j++;
  248. }
  249. }
  250. }
  251. break;
  252. }
  253. }
  254. /*
  255. * Three (3) kinds of logo maps exist. linux_logo_clut224 (>16 colors),
  256. * linux_logo_vga16 (16 colors) and linux_logo_mono (2 colors). Depending on
  257. * the visual format and color depth of the framebuffer, the DAC, the
  258. * pseudo_palette, and the logo data will be adjusted accordingly.
  259. *
  260. * Case 1 - linux_logo_clut224:
  261. * Color exceeds the number of console colors (16), thus we set the hardware DAC
  262. * using fb_set_cmap() appropriately. The "needs_cmapreset" flag will be set.
  263. *
  264. * For visuals that require color info from the pseudo_palette, we also construct
  265. * one for temporary use. The "needs_directpalette" or "needs_truepalette" flags
  266. * will be set.
  267. *
  268. * Case 2 - linux_logo_vga16:
  269. * The number of colors just matches the console colors, thus there is no need
  270. * to set the DAC or the pseudo_palette. However, the bitmap is packed, ie,
  271. * each byte contains color information for two pixels (upper and lower nibble).
  272. * To be consistent with fb_imageblit() usage, we therefore separate the two
  273. * nibbles into separate bytes. The "depth" flag will be set to 4.
  274. *
  275. * Case 3 - linux_logo_mono:
  276. * This is similar with Case 2. Each byte contains information for 8 pixels.
  277. * We isolate each bit and expand each into a byte. The "depth" flag will
  278. * be set to 1.
  279. */
  280. static struct logo_data {
  281. int depth;
  282. int needs_directpalette;
  283. int needs_truepalette;
  284. int needs_cmapreset;
  285. const struct linux_logo *logo;
  286. } fb_logo __read_mostly;
  287. static void fb_rotate_logo_ud(const u8 *in, u8 *out, u32 width, u32 height)
  288. {
  289. u32 size = width * height, i;
  290. out += size - 1;
  291. for (i = size; i--; )
  292. *out-- = *in++;
  293. }
  294. static void fb_rotate_logo_cw(const u8 *in, u8 *out, u32 width, u32 height)
  295. {
  296. int i, j, h = height - 1;
  297. for (i = 0; i < height; i++)
  298. for (j = 0; j < width; j++)
  299. out[height * j + h - i] = *in++;
  300. }
  301. static void fb_rotate_logo_ccw(const u8 *in, u8 *out, u32 width, u32 height)
  302. {
  303. int i, j, w = width - 1;
  304. for (i = 0; i < height; i++)
  305. for (j = 0; j < width; j++)
  306. out[height * (w - j) + i] = *in++;
  307. }
  308. static void fb_rotate_logo(struct fb_info *info, u8 *dst,
  309. struct fb_image *image, int rotate)
  310. {
  311. u32 tmp;
  312. if (rotate == FB_ROTATE_UD) {
  313. fb_rotate_logo_ud(image->data, dst, image->width,
  314. image->height);
  315. image->dx = info->var.xres - image->width - image->dx;
  316. image->dy = info->var.yres - image->height - image->dy;
  317. } else if (rotate == FB_ROTATE_CW) {
  318. fb_rotate_logo_cw(image->data, dst, image->width,
  319. image->height);
  320. tmp = image->width;
  321. image->width = image->height;
  322. image->height = tmp;
  323. tmp = image->dy;
  324. image->dy = image->dx;
  325. image->dx = info->var.xres - image->width - tmp;
  326. } else if (rotate == FB_ROTATE_CCW) {
  327. fb_rotate_logo_ccw(image->data, dst, image->width,
  328. image->height);
  329. tmp = image->width;
  330. image->width = image->height;
  331. image->height = tmp;
  332. tmp = image->dx;
  333. image->dx = image->dy;
  334. image->dy = info->var.yres - image->height - tmp;
  335. }
  336. image->data = dst;
  337. }
  338. static void fb_do_show_logo(struct fb_info *info, struct fb_image *image,
  339. int rotate, unsigned int num)
  340. {
  341. unsigned int x;
  342. if (rotate == FB_ROTATE_UR) {
  343. for (x = 0;
  344. x < num && image->dx + image->width <= info->var.xres;
  345. x++) {
  346. info->fbops->fb_imageblit(info, image);
  347. image->dx += image->width + 8;
  348. }
  349. } else if (rotate == FB_ROTATE_UD) {
  350. for (x = 0; x < num && image->dx >= 0; x++) {
  351. info->fbops->fb_imageblit(info, image);
  352. image->dx -= image->width + 8;
  353. }
  354. } else if (rotate == FB_ROTATE_CW) {
  355. for (x = 0;
  356. x < num && image->dy + image->height <= info->var.yres;
  357. x++) {
  358. info->fbops->fb_imageblit(info, image);
  359. image->dy += image->height + 8;
  360. }
  361. } else if (rotate == FB_ROTATE_CCW) {
  362. for (x = 0; x < num && image->dy >= 0; x++) {
  363. info->fbops->fb_imageblit(info, image);
  364. image->dy -= image->height + 8;
  365. }
  366. }
  367. }
  368. static int fb_show_logo_line(struct fb_info *info, int rotate,
  369. const struct linux_logo *logo, int y,
  370. unsigned int n)
  371. {
  372. u32 *palette = NULL, *saved_pseudo_palette = NULL;
  373. unsigned char *logo_new = NULL, *logo_rotate = NULL;
  374. struct fb_image image;
  375. /* Return if the frame buffer is not mapped or suspended */
  376. if (logo == NULL || info->state != FBINFO_STATE_RUNNING ||
  377. info->flags & FBINFO_MODULE)
  378. return 0;
  379. image.depth = 8;
  380. image.data = logo->data;
  381. if (fb_logo.needs_cmapreset)
  382. fb_set_logocmap(info, logo);
  383. if (fb_logo.needs_truepalette ||
  384. fb_logo.needs_directpalette) {
  385. palette = kmalloc(256 * 4, GFP_KERNEL);
  386. if (palette == NULL)
  387. return 0;
  388. if (fb_logo.needs_truepalette)
  389. fb_set_logo_truepalette(info, logo, palette);
  390. else
  391. fb_set_logo_directpalette(info, logo, palette);
  392. saved_pseudo_palette = info->pseudo_palette;
  393. info->pseudo_palette = palette;
  394. }
  395. if (fb_logo.depth <= 4) {
  396. logo_new = kmalloc(logo->width * logo->height, GFP_KERNEL);
  397. if (logo_new == NULL) {
  398. kfree(palette);
  399. if (saved_pseudo_palette)
  400. info->pseudo_palette = saved_pseudo_palette;
  401. return 0;
  402. }
  403. image.data = logo_new;
  404. fb_set_logo(info, logo, logo_new, fb_logo.depth);
  405. }
  406. image.dx = 0;
  407. image.dy = y;
  408. image.width = logo->width;
  409. image.height = logo->height;
  410. if (rotate) {
  411. logo_rotate = kmalloc(logo->width *
  412. logo->height, GFP_KERNEL);
  413. if (logo_rotate)
  414. fb_rotate_logo(info, logo_rotate, &image, rotate);
  415. }
  416. fb_do_show_logo(info, &image, rotate, n);
  417. kfree(palette);
  418. if (saved_pseudo_palette != NULL)
  419. info->pseudo_palette = saved_pseudo_palette;
  420. kfree(logo_new);
  421. kfree(logo_rotate);
  422. return logo->height;
  423. }
  424. #ifdef CONFIG_FB_LOGO_EXTRA
  425. #define FB_LOGO_EX_NUM_MAX 10
  426. static struct logo_data_extra {
  427. const struct linux_logo *logo;
  428. unsigned int n;
  429. } fb_logo_ex[FB_LOGO_EX_NUM_MAX];
  430. static unsigned int fb_logo_ex_num;
  431. void fb_append_extra_logo(const struct linux_logo *logo, unsigned int n)
  432. {
  433. if (!n || fb_logo_ex_num == FB_LOGO_EX_NUM_MAX)
  434. return;
  435. fb_logo_ex[fb_logo_ex_num].logo = logo;
  436. fb_logo_ex[fb_logo_ex_num].n = n;
  437. fb_logo_ex_num++;
  438. }
  439. static int fb_prepare_extra_logos(struct fb_info *info, unsigned int height,
  440. unsigned int yres)
  441. {
  442. unsigned int i;
  443. /* FIXME: logo_ex supports only truecolor fb. */
  444. if (info->fix.visual != FB_VISUAL_TRUECOLOR)
  445. fb_logo_ex_num = 0;
  446. for (i = 0; i < fb_logo_ex_num; i++) {
  447. if (fb_logo_ex[i].logo->type != fb_logo.logo->type) {
  448. fb_logo_ex[i].logo = NULL;
  449. continue;
  450. }
  451. height += fb_logo_ex[i].logo->height;
  452. if (height > yres) {
  453. height -= fb_logo_ex[i].logo->height;
  454. fb_logo_ex_num = i;
  455. break;
  456. }
  457. }
  458. return height;
  459. }
  460. static int fb_show_extra_logos(struct fb_info *info, int y, int rotate)
  461. {
  462. unsigned int i;
  463. for (i = 0; i < fb_logo_ex_num; i++)
  464. y += fb_show_logo_line(info, rotate,
  465. fb_logo_ex[i].logo, y, fb_logo_ex[i].n);
  466. return y;
  467. }
  468. #else /* !CONFIG_FB_LOGO_EXTRA */
  469. static inline int fb_prepare_extra_logos(struct fb_info *info,
  470. unsigned int height,
  471. unsigned int yres)
  472. {
  473. return height;
  474. }
  475. static inline int fb_show_extra_logos(struct fb_info *info, int y, int rotate)
  476. {
  477. return y;
  478. }
  479. #endif /* CONFIG_FB_LOGO_EXTRA */
  480. int fb_prepare_logo(struct fb_info *info, int rotate)
  481. {
  482. int depth = fb_get_color_depth(&info->var, &info->fix);
  483. unsigned int yres;
  484. memset(&fb_logo, 0, sizeof(struct logo_data));
  485. if (info->flags & FBINFO_MISC_TILEBLITTING ||
  486. info->flags & FBINFO_MODULE)
  487. return 0;
  488. if (info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  489. depth = info->var.blue.length;
  490. if (info->var.red.length < depth)
  491. depth = info->var.red.length;
  492. if (info->var.green.length < depth)
  493. depth = info->var.green.length;
  494. }
  495. if (info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR && depth > 4) {
  496. /* assume console colormap */
  497. depth = 4;
  498. }
  499. /* Return if no suitable logo was found */
  500. fb_logo.logo = fb_find_logo(depth);
  501. if (!fb_logo.logo) {
  502. return 0;
  503. }
  504. if (rotate == FB_ROTATE_UR || rotate == FB_ROTATE_UD)
  505. yres = info->var.yres;
  506. else
  507. yres = info->var.xres;
  508. if (fb_logo.logo->height > yres) {
  509. fb_logo.logo = NULL;
  510. return 0;
  511. }
  512. /* What depth we asked for might be different from what we get */
  513. if (fb_logo.logo->type == LINUX_LOGO_CLUT224)
  514. fb_logo.depth = 8;
  515. else if (fb_logo.logo->type == LINUX_LOGO_VGA16)
  516. fb_logo.depth = 4;
  517. else
  518. fb_logo.depth = 1;
  519. if (fb_logo.depth > 4 && depth > 4) {
  520. switch (info->fix.visual) {
  521. case FB_VISUAL_TRUECOLOR:
  522. fb_logo.needs_truepalette = 1;
  523. break;
  524. case FB_VISUAL_DIRECTCOLOR:
  525. fb_logo.needs_directpalette = 1;
  526. fb_logo.needs_cmapreset = 1;
  527. break;
  528. case FB_VISUAL_PSEUDOCOLOR:
  529. fb_logo.needs_cmapreset = 1;
  530. break;
  531. }
  532. }
  533. return fb_prepare_extra_logos(info, fb_logo.logo->height, yres);
  534. }
  535. int fb_show_logo(struct fb_info *info, int rotate)
  536. {
  537. int y;
  538. y = fb_show_logo_line(info, rotate, fb_logo.logo, 0,
  539. num_online_cpus());
  540. y = fb_show_extra_logos(info, y, rotate);
  541. return y;
  542. }
  543. #else
  544. int fb_prepare_logo(struct fb_info *info, int rotate) { return 0; }
  545. int fb_show_logo(struct fb_info *info, int rotate) { return 0; }
  546. #endif /* CONFIG_LOGO */
  547. static void *fb_seq_start(struct seq_file *m, loff_t *pos)
  548. {
  549. return (*pos < FB_MAX) ? pos : NULL;
  550. }
  551. static void *fb_seq_next(struct seq_file *m, void *v, loff_t *pos)
  552. {
  553. (*pos)++;
  554. return (*pos < FB_MAX) ? pos : NULL;
  555. }
  556. static void fb_seq_stop(struct seq_file *m, void *v)
  557. {
  558. }
  559. static int fb_seq_show(struct seq_file *m, void *v)
  560. {
  561. int i = *(loff_t *)v;
  562. struct fb_info *fi = registered_fb[i];
  563. if (fi)
  564. seq_printf(m, "%d %s\n", fi->node, fi->fix.id);
  565. return 0;
  566. }
  567. static const struct seq_operations proc_fb_seq_ops = {
  568. .start = fb_seq_start,
  569. .next = fb_seq_next,
  570. .stop = fb_seq_stop,
  571. .show = fb_seq_show,
  572. };
  573. static int proc_fb_open(struct inode *inode, struct file *file)
  574. {
  575. return seq_open(file, &proc_fb_seq_ops);
  576. }
  577. static const struct file_operations fb_proc_fops = {
  578. .owner = THIS_MODULE,
  579. .open = proc_fb_open,
  580. .read = seq_read,
  581. .llseek = seq_lseek,
  582. .release = seq_release,
  583. };
  584. static ssize_t
  585. fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  586. {
  587. unsigned long p = *ppos;
  588. struct inode *inode = file->f_path.dentry->d_inode;
  589. int fbidx = iminor(inode);
  590. struct fb_info *info = registered_fb[fbidx];
  591. u32 *buffer, *dst;
  592. u32 __iomem *src;
  593. int c, i, cnt = 0, err = 0;
  594. unsigned long total_size;
  595. if (!info || ! info->screen_base)
  596. return -ENODEV;
  597. if (info->state != FBINFO_STATE_RUNNING)
  598. return -EPERM;
  599. if (info->fbops->fb_read)
  600. return info->fbops->fb_read(info, buf, count, ppos);
  601. total_size = info->screen_size;
  602. if (total_size == 0)
  603. total_size = info->fix.smem_len;
  604. if (p >= total_size)
  605. return 0;
  606. if (count >= total_size)
  607. count = total_size;
  608. if (count + p > total_size)
  609. count = total_size - p;
  610. buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
  611. GFP_KERNEL);
  612. if (!buffer)
  613. return -ENOMEM;
  614. src = (u32 __iomem *) (info->screen_base + p);
  615. if (info->fbops->fb_sync)
  616. info->fbops->fb_sync(info);
  617. while (count) {
  618. c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  619. dst = buffer;
  620. for (i = c >> 2; i--; )
  621. *dst++ = fb_readl(src++);
  622. if (c & 3) {
  623. u8 *dst8 = (u8 *) dst;
  624. u8 __iomem *src8 = (u8 __iomem *) src;
  625. for (i = c & 3; i--;)
  626. *dst8++ = fb_readb(src8++);
  627. src = (u32 __iomem *) src8;
  628. }
  629. if (copy_to_user(buf, buffer, c)) {
  630. err = -EFAULT;
  631. break;
  632. }
  633. *ppos += c;
  634. buf += c;
  635. cnt += c;
  636. count -= c;
  637. }
  638. kfree(buffer);
  639. return (err) ? err : cnt;
  640. }
  641. static ssize_t
  642. fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  643. {
  644. unsigned long p = *ppos;
  645. struct inode *inode = file->f_path.dentry->d_inode;
  646. int fbidx = iminor(inode);
  647. struct fb_info *info = registered_fb[fbidx];
  648. u32 *buffer, *src;
  649. u32 __iomem *dst;
  650. int c, i, cnt = 0, err = 0;
  651. unsigned long total_size;
  652. if (!info || !info->screen_base)
  653. return -ENODEV;
  654. if (info->state != FBINFO_STATE_RUNNING)
  655. return -EPERM;
  656. if (info->fbops->fb_write)
  657. return info->fbops->fb_write(info, buf, count, ppos);
  658. total_size = info->screen_size;
  659. if (total_size == 0)
  660. total_size = info->fix.smem_len;
  661. if (p > total_size)
  662. return -EFBIG;
  663. if (count > total_size) {
  664. err = -EFBIG;
  665. count = total_size;
  666. }
  667. if (count + p > total_size) {
  668. if (!err)
  669. err = -ENOSPC;
  670. count = total_size - p;
  671. }
  672. buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
  673. GFP_KERNEL);
  674. if (!buffer)
  675. return -ENOMEM;
  676. dst = (u32 __iomem *) (info->screen_base + p);
  677. if (info->fbops->fb_sync)
  678. info->fbops->fb_sync(info);
  679. while (count) {
  680. c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  681. src = buffer;
  682. if (copy_from_user(src, buf, c)) {
  683. err = -EFAULT;
  684. break;
  685. }
  686. for (i = c >> 2; i--; )
  687. fb_writel(*src++, dst++);
  688. if (c & 3) {
  689. u8 *src8 = (u8 *) src;
  690. u8 __iomem *dst8 = (u8 __iomem *) dst;
  691. for (i = c & 3; i--; )
  692. fb_writeb(*src8++, dst8++);
  693. dst = (u32 __iomem *) dst8;
  694. }
  695. *ppos += c;
  696. buf += c;
  697. cnt += c;
  698. count -= c;
  699. }
  700. kfree(buffer);
  701. return (cnt) ? cnt : err;
  702. }
  703. int
  704. fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var)
  705. {
  706. struct fb_fix_screeninfo *fix = &info->fix;
  707. unsigned int yres = info->var.yres;
  708. int err = 0;
  709. if (var->yoffset > 0) {
  710. if (var->vmode & FB_VMODE_YWRAP) {
  711. if (!fix->ywrapstep || (var->yoffset % fix->ywrapstep))
  712. err = -EINVAL;
  713. else
  714. yres = 0;
  715. } else if (!fix->ypanstep || (var->yoffset % fix->ypanstep))
  716. err = -EINVAL;
  717. }
  718. if (var->xoffset > 0 && (!fix->xpanstep ||
  719. (var->xoffset % fix->xpanstep)))
  720. err = -EINVAL;
  721. if (err || !info->fbops->fb_pan_display ||
  722. var->yoffset + yres > info->var.yres_virtual ||
  723. var->xoffset + info->var.xres > info->var.xres_virtual)
  724. return -EINVAL;
  725. if ((err = info->fbops->fb_pan_display(var, info)))
  726. return err;
  727. info->var.xoffset = var->xoffset;
  728. info->var.yoffset = var->yoffset;
  729. if (var->vmode & FB_VMODE_YWRAP)
  730. info->var.vmode |= FB_VMODE_YWRAP;
  731. else
  732. info->var.vmode &= ~FB_VMODE_YWRAP;
  733. return 0;
  734. }
  735. static int fb_check_caps(struct fb_info *info, struct fb_var_screeninfo *var,
  736. u32 activate)
  737. {
  738. struct fb_event event;
  739. struct fb_blit_caps caps, fbcaps;
  740. int err = 0;
  741. memset(&caps, 0, sizeof(caps));
  742. memset(&fbcaps, 0, sizeof(fbcaps));
  743. caps.flags = (activate & FB_ACTIVATE_ALL) ? 1 : 0;
  744. event.info = info;
  745. event.data = &caps;
  746. fb_notifier_call_chain(FB_EVENT_GET_REQ, &event);
  747. info->fbops->fb_get_caps(info, &fbcaps, var);
  748. if (((fbcaps.x ^ caps.x) & caps.x) ||
  749. ((fbcaps.y ^ caps.y) & caps.y) ||
  750. (fbcaps.len < caps.len))
  751. err = -EINVAL;
  752. return err;
  753. }
  754. int
  755. fb_set_var(struct fb_info *info, struct fb_var_screeninfo *var)
  756. {
  757. int flags = info->flags;
  758. int ret = 0;
  759. if (var->activate & FB_ACTIVATE_INV_MODE) {
  760. struct fb_videomode mode1, mode2;
  761. fb_var_to_videomode(&mode1, var);
  762. fb_var_to_videomode(&mode2, &info->var);
  763. /* make sure we don't delete the videomode of current var */
  764. ret = fb_mode_is_equal(&mode1, &mode2);
  765. if (!ret) {
  766. struct fb_event event;
  767. event.info = info;
  768. event.data = &mode1;
  769. ret = fb_notifier_call_chain(FB_EVENT_MODE_DELETE, &event);
  770. }
  771. if (!ret)
  772. fb_delete_videomode(&mode1, &info->modelist);
  773. ret = (ret) ? -EINVAL : 0;
  774. goto done;
  775. }
  776. if ((var->activate & FB_ACTIVATE_FORCE) ||
  777. memcmp(&info->var, var, sizeof(struct fb_var_screeninfo))) {
  778. u32 activate = var->activate;
  779. if (!info->fbops->fb_check_var) {
  780. *var = info->var;
  781. goto done;
  782. }
  783. ret = info->fbops->fb_check_var(var, info);
  784. if (ret)
  785. goto done;
  786. if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
  787. struct fb_videomode mode;
  788. if (info->fbops->fb_get_caps) {
  789. ret = fb_check_caps(info, var, activate);
  790. if (ret)
  791. goto done;
  792. }
  793. info->var = *var;
  794. if (info->fbops->fb_set_par)
  795. info->fbops->fb_set_par(info);
  796. fb_pan_display(info, &info->var);
  797. fb_set_cmap(&info->cmap, info);
  798. fb_var_to_videomode(&mode, &info->var);
  799. if (info->modelist.prev && info->modelist.next &&
  800. !list_empty(&info->modelist))
  801. ret = fb_add_videomode(&mode, &info->modelist);
  802. if (!ret && (flags & FBINFO_MISC_USEREVENT)) {
  803. struct fb_event event;
  804. int evnt = (activate & FB_ACTIVATE_ALL) ?
  805. FB_EVENT_MODE_CHANGE_ALL :
  806. FB_EVENT_MODE_CHANGE;
  807. info->flags &= ~FBINFO_MISC_USEREVENT;
  808. event.info = info;
  809. event.data = &mode;
  810. fb_notifier_call_chain(evnt, &event);
  811. }
  812. }
  813. }
  814. done:
  815. return ret;
  816. }
  817. int
  818. fb_blank(struct fb_info *info, int blank)
  819. {
  820. int ret = -EINVAL;
  821. if (blank > FB_BLANK_POWERDOWN)
  822. blank = FB_BLANK_POWERDOWN;
  823. if (info->fbops->fb_blank)
  824. ret = info->fbops->fb_blank(blank, info);
  825. if (!ret) {
  826. struct fb_event event;
  827. event.info = info;
  828. event.data = &blank;
  829. fb_notifier_call_chain(FB_EVENT_BLANK, &event);
  830. }
  831. return ret;
  832. }
  833. static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
  834. unsigned long arg)
  835. {
  836. struct fb_ops *fb;
  837. struct fb_var_screeninfo var;
  838. struct fb_fix_screeninfo fix;
  839. struct fb_con2fbmap con2fb;
  840. struct fb_cmap cmap_from;
  841. struct fb_cmap_user cmap;
  842. struct fb_event event;
  843. void __user *argp = (void __user *)arg;
  844. long ret = 0;
  845. switch (cmd) {
  846. case FBIOGET_VSCREENINFO:
  847. if (!lock_fb_info(info))
  848. return -ENODEV;
  849. var = info->var;
  850. unlock_fb_info(info);
  851. ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0;
  852. break;
  853. case FBIOPUT_VSCREENINFO:
  854. if (copy_from_user(&var, argp, sizeof(var)))
  855. return -EFAULT;
  856. if (!lock_fb_info(info))
  857. return -ENODEV;
  858. acquire_console_sem();
  859. info->flags |= FBINFO_MISC_USEREVENT;
  860. ret = fb_set_var(info, &var);
  861. info->flags &= ~FBINFO_MISC_USEREVENT;
  862. release_console_sem();
  863. unlock_fb_info(info);
  864. if (!ret && copy_to_user(argp, &var, sizeof(var)))
  865. ret = -EFAULT;
  866. break;
  867. case FBIOGET_FSCREENINFO:
  868. if (!lock_fb_info(info))
  869. return -ENODEV;
  870. fix = info->fix;
  871. unlock_fb_info(info);
  872. ret = copy_to_user(argp, &fix, sizeof(fix)) ? -EFAULT : 0;
  873. break;
  874. case FBIOPUTCMAP:
  875. if (copy_from_user(&cmap, argp, sizeof(cmap)))
  876. return -EFAULT;
  877. ret = fb_set_user_cmap(&cmap, info);
  878. break;
  879. case FBIOGETCMAP:
  880. if (copy_from_user(&cmap, argp, sizeof(cmap)))
  881. return -EFAULT;
  882. if (!lock_fb_info(info))
  883. return -ENODEV;
  884. cmap_from = info->cmap;
  885. unlock_fb_info(info);
  886. ret = fb_cmap_to_user(&cmap_from, &cmap);
  887. break;
  888. case FBIOPAN_DISPLAY:
  889. if (copy_from_user(&var, argp, sizeof(var)))
  890. return -EFAULT;
  891. if (!lock_fb_info(info))
  892. return -ENODEV;
  893. acquire_console_sem();
  894. ret = fb_pan_display(info, &var);
  895. release_console_sem();
  896. unlock_fb_info(info);
  897. if (ret == 0 && copy_to_user(argp, &var, sizeof(var)))
  898. return -EFAULT;
  899. break;
  900. case FBIO_CURSOR:
  901. ret = -EINVAL;
  902. break;
  903. case FBIOGET_CON2FBMAP:
  904. if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
  905. return -EFAULT;
  906. if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
  907. return -EINVAL;
  908. con2fb.framebuffer = -1;
  909. event.data = &con2fb;
  910. if (!lock_fb_info(info))
  911. return -ENODEV;
  912. event.info = info;
  913. fb_notifier_call_chain(FB_EVENT_GET_CONSOLE_MAP, &event);
  914. unlock_fb_info(info);
  915. ret = copy_to_user(argp, &con2fb, sizeof(con2fb)) ? -EFAULT : 0;
  916. break;
  917. case FBIOPUT_CON2FBMAP:
  918. if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
  919. return -EFAULT;
  920. if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
  921. return -EINVAL;
  922. if (con2fb.framebuffer < 0 || con2fb.framebuffer >= FB_MAX)
  923. return -EINVAL;
  924. if (!registered_fb[con2fb.framebuffer])
  925. request_module("fb%d", con2fb.framebuffer);
  926. if (!registered_fb[con2fb.framebuffer]) {
  927. ret = -EINVAL;
  928. break;
  929. }
  930. event.data = &con2fb;
  931. if (!lock_fb_info(info))
  932. return -ENODEV;
  933. event.info = info;
  934. ret = fb_notifier_call_chain(FB_EVENT_SET_CONSOLE_MAP,
  935. &event);
  936. unlock_fb_info(info);
  937. break;
  938. case FBIOBLANK:
  939. if (!lock_fb_info(info))
  940. return -ENODEV;
  941. acquire_console_sem();
  942. info->flags |= FBINFO_MISC_USEREVENT;
  943. ret = fb_blank(info, arg);
  944. info->flags &= ~FBINFO_MISC_USEREVENT;
  945. release_console_sem();
  946. unlock_fb_info(info);
  947. break;
  948. default:
  949. if (!lock_fb_info(info))
  950. return -ENODEV;
  951. fb = info->fbops;
  952. if (fb->fb_ioctl)
  953. ret = fb->fb_ioctl(info, cmd, arg);
  954. else
  955. ret = -ENOTTY;
  956. unlock_fb_info(info);
  957. }
  958. return ret;
  959. }
  960. static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  961. {
  962. struct inode *inode = file->f_path.dentry->d_inode;
  963. int fbidx = iminor(inode);
  964. struct fb_info *info = registered_fb[fbidx];
  965. return do_fb_ioctl(info, cmd, arg);
  966. }
  967. #ifdef CONFIG_COMPAT
  968. struct fb_fix_screeninfo32 {
  969. char id[16];
  970. compat_caddr_t smem_start;
  971. u32 smem_len;
  972. u32 type;
  973. u32 type_aux;
  974. u32 visual;
  975. u16 xpanstep;
  976. u16 ypanstep;
  977. u16 ywrapstep;
  978. u32 line_length;
  979. compat_caddr_t mmio_start;
  980. u32 mmio_len;
  981. u32 accel;
  982. u16 reserved[3];
  983. };
  984. struct fb_cmap32 {
  985. u32 start;
  986. u32 len;
  987. compat_caddr_t red;
  988. compat_caddr_t green;
  989. compat_caddr_t blue;
  990. compat_caddr_t transp;
  991. };
  992. static int fb_getput_cmap(struct fb_info *info, unsigned int cmd,
  993. unsigned long arg)
  994. {
  995. struct fb_cmap_user __user *cmap;
  996. struct fb_cmap32 __user *cmap32;
  997. __u32 data;
  998. int err;
  999. cmap = compat_alloc_user_space(sizeof(*cmap));
  1000. cmap32 = compat_ptr(arg);
  1001. if (copy_in_user(&cmap->start, &cmap32->start, 2 * sizeof(__u32)))
  1002. return -EFAULT;
  1003. if (get_user(data, &cmap32->red) ||
  1004. put_user(compat_ptr(data), &cmap->red) ||
  1005. get_user(data, &cmap32->green) ||
  1006. put_user(compat_ptr(data), &cmap->green) ||
  1007. get_user(data, &cmap32->blue) ||
  1008. put_user(compat_ptr(data), &cmap->blue) ||
  1009. get_user(data, &cmap32->transp) ||
  1010. put_user(compat_ptr(data), &cmap->transp))
  1011. return -EFAULT;
  1012. err = do_fb_ioctl(info, cmd, (unsigned long) cmap);
  1013. if (!err) {
  1014. if (copy_in_user(&cmap32->start,
  1015. &cmap->start,
  1016. 2 * sizeof(__u32)))
  1017. err = -EFAULT;
  1018. }
  1019. return err;
  1020. }
  1021. static int do_fscreeninfo_to_user(struct fb_fix_screeninfo *fix,
  1022. struct fb_fix_screeninfo32 __user *fix32)
  1023. {
  1024. __u32 data;
  1025. int err;
  1026. err = copy_to_user(&fix32->id, &fix->id, sizeof(fix32->id));
  1027. data = (__u32) (unsigned long) fix->smem_start;
  1028. err |= put_user(data, &fix32->smem_start);
  1029. err |= put_user(fix->smem_len, &fix32->smem_len);
  1030. err |= put_user(fix->type, &fix32->type);
  1031. err |= put_user(fix->type_aux, &fix32->type_aux);
  1032. err |= put_user(fix->visual, &fix32->visual);
  1033. err |= put_user(fix->xpanstep, &fix32->xpanstep);
  1034. err |= put_user(fix->ypanstep, &fix32->ypanstep);
  1035. err |= put_user(fix->ywrapstep, &fix32->ywrapstep);
  1036. err |= put_user(fix->line_length, &fix32->line_length);
  1037. data = (__u32) (unsigned long) fix->mmio_start;
  1038. err |= put_user(data, &fix32->mmio_start);
  1039. err |= put_user(fix->mmio_len, &fix32->mmio_len);
  1040. err |= put_user(fix->accel, &fix32->accel);
  1041. err |= copy_to_user(fix32->reserved, fix->reserved,
  1042. sizeof(fix->reserved));
  1043. return err;
  1044. }
  1045. static int fb_get_fscreeninfo(struct fb_info *info, unsigned int cmd,
  1046. unsigned long arg)
  1047. {
  1048. mm_segment_t old_fs;
  1049. struct fb_fix_screeninfo fix;
  1050. struct fb_fix_screeninfo32 __user *fix32;
  1051. int err;
  1052. fix32 = compat_ptr(arg);
  1053. old_fs = get_fs();
  1054. set_fs(KERNEL_DS);
  1055. err = do_fb_ioctl(info, cmd, (unsigned long) &fix);
  1056. set_fs(old_fs);
  1057. if (!err)
  1058. err = do_fscreeninfo_to_user(&fix, fix32);
  1059. return err;
  1060. }
  1061. static long fb_compat_ioctl(struct file *file, unsigned int cmd,
  1062. unsigned long arg)
  1063. {
  1064. struct inode *inode = file->f_path.dentry->d_inode;
  1065. int fbidx = iminor(inode);
  1066. struct fb_info *info = registered_fb[fbidx];
  1067. struct fb_ops *fb = info->fbops;
  1068. long ret = -ENOIOCTLCMD;
  1069. switch(cmd) {
  1070. case FBIOGET_VSCREENINFO:
  1071. case FBIOPUT_VSCREENINFO:
  1072. case FBIOPAN_DISPLAY:
  1073. case FBIOGET_CON2FBMAP:
  1074. case FBIOPUT_CON2FBMAP:
  1075. arg = (unsigned long) compat_ptr(arg);
  1076. case FBIOBLANK:
  1077. ret = do_fb_ioctl(info, cmd, arg);
  1078. break;
  1079. case FBIOGET_FSCREENINFO:
  1080. ret = fb_get_fscreeninfo(info, cmd, arg);
  1081. break;
  1082. case FBIOGETCMAP:
  1083. case FBIOPUTCMAP:
  1084. ret = fb_getput_cmap(info, cmd, arg);
  1085. break;
  1086. default:
  1087. if (fb->fb_compat_ioctl)
  1088. ret = fb->fb_compat_ioctl(info, cmd, arg);
  1089. break;
  1090. }
  1091. return ret;
  1092. }
  1093. #endif
  1094. static int
  1095. fb_mmap(struct file *file, struct vm_area_struct * vma)
  1096. __acquires(&info->lock)
  1097. __releases(&info->lock)
  1098. {
  1099. int fbidx = iminor(file->f_path.dentry->d_inode);
  1100. struct fb_info *info = registered_fb[fbidx];
  1101. struct fb_ops *fb = info->fbops;
  1102. unsigned long off;
  1103. unsigned long start;
  1104. u32 len;
  1105. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  1106. return -EINVAL;
  1107. off = vma->vm_pgoff << PAGE_SHIFT;
  1108. if (!fb)
  1109. return -ENODEV;
  1110. if (fb->fb_mmap) {
  1111. int res;
  1112. mutex_lock(&info->lock);
  1113. res = fb->fb_mmap(info, vma);
  1114. mutex_unlock(&info->lock);
  1115. return res;
  1116. }
  1117. mutex_lock(&info->lock);
  1118. /* frame buffer memory */
  1119. start = info->fix.smem_start;
  1120. len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.smem_len);
  1121. if (off >= len) {
  1122. /* memory mapped io */
  1123. off -= len;
  1124. if (info->var.accel_flags) {
  1125. mutex_unlock(&info->lock);
  1126. return -EINVAL;
  1127. }
  1128. start = info->fix.mmio_start;
  1129. len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.mmio_len);
  1130. }
  1131. mutex_unlock(&info->lock);
  1132. start &= PAGE_MASK;
  1133. if ((vma->vm_end - vma->vm_start + off) > len)
  1134. return -EINVAL;
  1135. off += start;
  1136. vma->vm_pgoff = off >> PAGE_SHIFT;
  1137. /* This is an IO map - tell maydump to skip this VMA */
  1138. vma->vm_flags |= VM_IO | VM_RESERVED;
  1139. fb_pgprotect(file, vma, off);
  1140. if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
  1141. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  1142. return -EAGAIN;
  1143. return 0;
  1144. }
  1145. static int
  1146. fb_open(struct inode *inode, struct file *file)
  1147. __acquires(&info->lock)
  1148. __releases(&info->lock)
  1149. {
  1150. int fbidx = iminor(inode);
  1151. struct fb_info *info;
  1152. int res = 0;
  1153. if (fbidx >= FB_MAX)
  1154. return -ENODEV;
  1155. info = registered_fb[fbidx];
  1156. if (!info)
  1157. request_module("fb%d", fbidx);
  1158. info = registered_fb[fbidx];
  1159. if (!info)
  1160. return -ENODEV;
  1161. mutex_lock(&info->lock);
  1162. if (!try_module_get(info->fbops->owner)) {
  1163. res = -ENODEV;
  1164. goto out;
  1165. }
  1166. file->private_data = info;
  1167. if (info->fbops->fb_open) {
  1168. res = info->fbops->fb_open(info,1);
  1169. if (res)
  1170. module_put(info->fbops->owner);
  1171. }
  1172. #ifdef CONFIG_FB_DEFERRED_IO
  1173. if (info->fbdefio)
  1174. fb_deferred_io_open(info, inode, file);
  1175. #endif
  1176. out:
  1177. mutex_unlock(&info->lock);
  1178. return res;
  1179. }
  1180. static int
  1181. fb_release(struct inode *inode, struct file *file)
  1182. __acquires(&info->lock)
  1183. __releases(&info->lock)
  1184. {
  1185. struct fb_info * const info = file->private_data;
  1186. mutex_lock(&info->lock);
  1187. if (info->fbops->fb_release)
  1188. info->fbops->fb_release(info,1);
  1189. module_put(info->fbops->owner);
  1190. mutex_unlock(&info->lock);
  1191. return 0;
  1192. }
  1193. static const struct file_operations fb_fops = {
  1194. .owner = THIS_MODULE,
  1195. .read = fb_read,
  1196. .write = fb_write,
  1197. .unlocked_ioctl = fb_ioctl,
  1198. #ifdef CONFIG_COMPAT
  1199. .compat_ioctl = fb_compat_ioctl,
  1200. #endif
  1201. .mmap = fb_mmap,
  1202. .open = fb_open,
  1203. .release = fb_release,
  1204. #ifdef HAVE_ARCH_FB_UNMAPPED_AREA
  1205. .get_unmapped_area = get_fb_unmapped_area,
  1206. #endif
  1207. #ifdef CONFIG_FB_DEFERRED_IO
  1208. .fsync = fb_deferred_io_fsync,
  1209. #endif
  1210. };
  1211. struct class *fb_class;
  1212. EXPORT_SYMBOL(fb_class);
  1213. static int fb_check_foreignness(struct fb_info *fi)
  1214. {
  1215. const bool foreign_endian = fi->flags & FBINFO_FOREIGN_ENDIAN;
  1216. fi->flags &= ~FBINFO_FOREIGN_ENDIAN;
  1217. #ifdef __BIG_ENDIAN
  1218. fi->flags |= foreign_endian ? 0 : FBINFO_BE_MATH;
  1219. #else
  1220. fi->flags |= foreign_endian ? FBINFO_BE_MATH : 0;
  1221. #endif /* __BIG_ENDIAN */
  1222. if (fi->flags & FBINFO_BE_MATH && !fb_be_math(fi)) {
  1223. pr_err("%s: enable CONFIG_FB_BIG_ENDIAN to "
  1224. "support this framebuffer\n", fi->fix.id);
  1225. return -ENOSYS;
  1226. } else if (!(fi->flags & FBINFO_BE_MATH) && fb_be_math(fi)) {
  1227. pr_err("%s: enable CONFIG_FB_LITTLE_ENDIAN to "
  1228. "support this framebuffer\n", fi->fix.id);
  1229. return -ENOSYS;
  1230. }
  1231. return 0;
  1232. }
  1233. /**
  1234. * register_framebuffer - registers a frame buffer device
  1235. * @fb_info: frame buffer info structure
  1236. *
  1237. * Registers a frame buffer device @fb_info.
  1238. *
  1239. * Returns negative errno on error, or zero for success.
  1240. *
  1241. */
  1242. int
  1243. register_framebuffer(struct fb_info *fb_info)
  1244. {
  1245. int i;
  1246. struct fb_event event;
  1247. struct fb_videomode mode;
  1248. if (num_registered_fb == FB_MAX)
  1249. return -ENXIO;
  1250. if (fb_check_foreignness(fb_info))
  1251. return -ENOSYS;
  1252. num_registered_fb++;
  1253. for (i = 0 ; i < FB_MAX; i++)
  1254. if (!registered_fb[i])
  1255. break;
  1256. fb_info->node = i;
  1257. mutex_init(&fb_info->lock);
  1258. fb_info->dev = device_create(fb_class, fb_info->device,
  1259. MKDEV(FB_MAJOR, i), NULL, "fb%d", i);
  1260. if (IS_ERR(fb_info->dev)) {
  1261. /* Not fatal */
  1262. printk(KERN_WARNING "Unable to create device for framebuffer %d; errno = %ld\n", i, PTR_ERR(fb_info->dev));
  1263. fb_info->dev = NULL;
  1264. } else
  1265. fb_init_device(fb_info);
  1266. if (fb_info->pixmap.addr == NULL) {
  1267. fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);
  1268. if (fb_info->pixmap.addr) {
  1269. fb_info->pixmap.size = FBPIXMAPSIZE;
  1270. fb_info->pixmap.buf_align = 1;
  1271. fb_info->pixmap.scan_align = 1;
  1272. fb_info->pixmap.access_align = 32;
  1273. fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;
  1274. }
  1275. }
  1276. fb_info->pixmap.offset = 0;
  1277. if (!fb_info->pixmap.blit_x)
  1278. fb_info->pixmap.blit_x = ~(u32)0;
  1279. if (!fb_info->pixmap.blit_y)
  1280. fb_info->pixmap.blit_y = ~(u32)0;
  1281. if (!fb_info->modelist.prev || !fb_info->modelist.next)
  1282. INIT_LIST_HEAD(&fb_info->modelist);
  1283. fb_var_to_videomode(&mode, &fb_info->var);
  1284. fb_add_videomode(&mode, &fb_info->modelist);
  1285. registered_fb[i] = fb_info;
  1286. event.info = fb_info;
  1287. fb_notifier_call_chain(FB_EVENT_FB_REGISTERED, &event);
  1288. return 0;
  1289. }
  1290. /**
  1291. * unregister_framebuffer - releases a frame buffer device
  1292. * @fb_info: frame buffer info structure
  1293. *
  1294. * Unregisters a frame buffer device @fb_info.
  1295. *
  1296. * Returns negative errno on error, or zero for success.
  1297. *
  1298. * This function will also notify the framebuffer console
  1299. * to release the driver.
  1300. *
  1301. * This is meant to be called within a driver's module_exit()
  1302. * function. If this is called outside module_exit(), ensure
  1303. * that the driver implements fb_open() and fb_release() to
  1304. * check that no processes are using the device.
  1305. */
  1306. int
  1307. unregister_framebuffer(struct fb_info *fb_info)
  1308. {
  1309. struct fb_event event;
  1310. int i, ret = 0;
  1311. i = fb_info->node;
  1312. if (!registered_fb[i]) {
  1313. ret = -EINVAL;
  1314. goto done;
  1315. }
  1316. event.info = fb_info;
  1317. ret = fb_notifier_call_chain(FB_EVENT_FB_UNBIND, &event);
  1318. if (ret) {
  1319. ret = -EINVAL;
  1320. goto done;
  1321. }
  1322. if (fb_info->pixmap.addr &&
  1323. (fb_info->pixmap.flags & FB_PIXMAP_DEFAULT))
  1324. kfree(fb_info->pixmap.addr);
  1325. fb_destroy_modelist(&fb_info->modelist);
  1326. registered_fb[i]=NULL;
  1327. num_registered_fb--;
  1328. fb_cleanup_device(fb_info);
  1329. device_destroy(fb_class, MKDEV(FB_MAJOR, i));
  1330. event.info = fb_info;
  1331. fb_notifier_call_chain(FB_EVENT_FB_UNREGISTERED, &event);
  1332. done:
  1333. return ret;
  1334. }
  1335. /**
  1336. * fb_set_suspend - low level driver signals suspend
  1337. * @info: framebuffer affected
  1338. * @state: 0 = resuming, !=0 = suspending
  1339. *
  1340. * This is meant to be used by low level drivers to
  1341. * signal suspend/resume to the core & clients.
  1342. * It must be called with the console semaphore held
  1343. */
  1344. void fb_set_suspend(struct fb_info *info, int state)
  1345. {
  1346. struct fb_event event;
  1347. event.info = info;
  1348. if (state) {
  1349. fb_notifier_call_chain(FB_EVENT_SUSPEND, &event);
  1350. info->state = FBINFO_STATE_SUSPENDED;
  1351. } else {
  1352. info->state = FBINFO_STATE_RUNNING;
  1353. fb_notifier_call_chain(FB_EVENT_RESUME, &event);
  1354. }
  1355. }
  1356. /**
  1357. * fbmem_init - init frame buffer subsystem
  1358. *
  1359. * Initialize the frame buffer subsystem.
  1360. *
  1361. * NOTE: This function is _only_ to be called by drivers/char/mem.c.
  1362. *
  1363. */
  1364. static int __init
  1365. fbmem_init(void)
  1366. {
  1367. proc_create("fb", 0, NULL, &fb_proc_fops);
  1368. if (register_chrdev(FB_MAJOR,"fb",&fb_fops))
  1369. printk("unable to get major %d for fb devs\n", FB_MAJOR);
  1370. fb_class = class_create(THIS_MODULE, "graphics");
  1371. if (IS_ERR(fb_class)) {
  1372. printk(KERN_WARNING "Unable to create fb class; errno = %ld\n", PTR_ERR(fb_class));
  1373. fb_class = NULL;
  1374. }
  1375. return 0;
  1376. }
  1377. #ifdef MODULE
  1378. module_init(fbmem_init);
  1379. static void __exit
  1380. fbmem_exit(void)
  1381. {
  1382. remove_proc_entry("fb", NULL);
  1383. class_destroy(fb_class);
  1384. unregister_chrdev(FB_MAJOR, "fb");
  1385. }
  1386. module_exit(fbmem_exit);
  1387. MODULE_LICENSE("GPL");
  1388. MODULE_DESCRIPTION("Framebuffer base");
  1389. #else
  1390. subsys_initcall(fbmem_init);
  1391. #endif
  1392. int fb_new_modelist(struct fb_info *info)
  1393. {
  1394. struct fb_event event;
  1395. struct fb_var_screeninfo var = info->var;
  1396. struct list_head *pos, *n;
  1397. struct fb_modelist *modelist;
  1398. struct fb_videomode *m, mode;
  1399. int err = 1;
  1400. list_for_each_safe(pos, n, &info->modelist) {
  1401. modelist = list_entry(pos, struct fb_modelist, list);
  1402. m = &modelist->mode;
  1403. fb_videomode_to_var(&var, m);
  1404. var.activate = FB_ACTIVATE_TEST;
  1405. err = fb_set_var(info, &var);
  1406. fb_var_to_videomode(&mode, &var);
  1407. if (err || !fb_mode_is_equal(m, &mode)) {
  1408. list_del(pos);
  1409. kfree(pos);
  1410. }
  1411. }
  1412. err = 1;
  1413. if (!list_empty(&info->modelist)) {
  1414. event.info = info;
  1415. err = fb_notifier_call_chain(FB_EVENT_NEW_MODELIST, &event);
  1416. }
  1417. return err;
  1418. }
  1419. static char *video_options[FB_MAX] __read_mostly;
  1420. static int ofonly __read_mostly;
  1421. /**
  1422. * fb_get_options - get kernel boot parameters
  1423. * @name: framebuffer name as it would appear in
  1424. * the boot parameter line
  1425. * (video=<name>:<options>)
  1426. * @option: the option will be stored here
  1427. *
  1428. * NOTE: Needed to maintain backwards compatibility
  1429. */
  1430. int fb_get_options(char *name, char **option)
  1431. {
  1432. char *opt, *options = NULL;
  1433. int opt_len, retval = 0;
  1434. int name_len = strlen(name), i;
  1435. if (name_len && ofonly && strncmp(name, "offb", 4))
  1436. retval = 1;
  1437. if (name_len && !retval) {
  1438. for (i = 0; i < FB_MAX; i++) {
  1439. if (video_options[i] == NULL)
  1440. continue;
  1441. opt_len = strlen(video_options[i]);
  1442. if (!opt_len)
  1443. continue;
  1444. opt = video_options[i];
  1445. if (!strncmp(name, opt, name_len) &&
  1446. opt[name_len] == ':')
  1447. options = opt + name_len + 1;
  1448. }
  1449. }
  1450. if (options && !strncmp(options, "off", 3))
  1451. retval = 1;
  1452. if (option)
  1453. *option = options;
  1454. return retval;
  1455. }
  1456. #ifndef MODULE
  1457. /**
  1458. * video_setup - process command line options
  1459. * @options: string of options
  1460. *
  1461. * Process command line options for frame buffer subsystem.
  1462. *
  1463. * NOTE: This function is a __setup and __init function.
  1464. * It only stores the options. Drivers have to call
  1465. * fb_get_options() as necessary.
  1466. *
  1467. * Returns zero.
  1468. *
  1469. */
  1470. static int __init video_setup(char *options)
  1471. {
  1472. int i, global = 0;
  1473. if (!options || !*options)
  1474. global = 1;
  1475. if (!global && !strncmp(options, "ofonly", 6)) {
  1476. ofonly = 1;
  1477. global = 1;
  1478. }
  1479. if (!global && !strstr(options, "fb:")) {
  1480. fb_mode_option = options;
  1481. global = 1;
  1482. }
  1483. if (!global) {
  1484. for (i = 0; i < FB_MAX; i++) {
  1485. if (video_options[i] == NULL) {
  1486. video_options[i] = options;
  1487. break;
  1488. }
  1489. }
  1490. }
  1491. return 1;
  1492. }
  1493. __setup("video=", video_setup);
  1494. #endif
  1495. /*
  1496. * Visible symbols for modules
  1497. */
  1498. EXPORT_SYMBOL(register_framebuffer);
  1499. EXPORT_SYMBOL(unregister_framebuffer);
  1500. EXPORT_SYMBOL(num_registered_fb);
  1501. EXPORT_SYMBOL(registered_fb);
  1502. EXPORT_SYMBOL(fb_show_logo);
  1503. EXPORT_SYMBOL(fb_set_var);
  1504. EXPORT_SYMBOL(fb_blank);
  1505. EXPORT_SYMBOL(fb_pan_display);
  1506. EXPORT_SYMBOL(fb_get_buffer_offset);
  1507. EXPORT_SYMBOL(fb_set_suspend);
  1508. EXPORT_SYMBOL(fb_get_options);
  1509. MODULE_LICENSE("GPL");