fbmem.c 37 KB

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