fbmem.c 40 KB

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