fbmem.c 40 KB

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