fbmem.c 39 KB

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