fbmem.c 45 KB

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