fbmem.c 44 KB

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