fbmem.c 41 KB

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