fbmem.c 43 KB

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