fbmem.c 44 KB

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