fbmem.c 45 KB

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