fbmem.c 38 KB

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