fbmem.c 38 KB

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