fbmem.c 37 KB

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