[PATCH] fbcon: Console Rotation - Add support to rotate the logo
[linux-2.6.git] / drivers / video / fbmem.c
blob7a2a8fa50b3b83a0e6a27493a6c14f117029b6aa
1 /*
2 * linux/drivers/video/fbmem.c
4 * Copyright (C) 1994 Martin Schaller
6 * 2001 - Documented with DocBook
7 * - Brad Douglas <brad@neruo.com>
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.
14 #include <linux/config.h>
15 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/sched.h>
20 #include <linux/smp_lock.h>
21 #include <linux/kernel.h>
22 #include <linux/major.h>
23 #include <linux/slab.h>
24 #include <linux/mm.h>
25 #include <linux/mman.h>
26 #include <linux/tty.h>
27 #include <linux/init.h>
28 #include <linux/linux_logo.h>
29 #include <linux/proc_fs.h>
30 #include <linux/console.h>
31 #ifdef CONFIG_KMOD
32 #include <linux/kmod.h>
33 #endif
34 #include <linux/devfs_fs_kernel.h>
35 #include <linux/err.h>
36 #include <linux/kernel.h>
37 #include <linux/device.h>
38 #include <linux/efi.h>
40 #if defined(__mc68000__) || defined(CONFIG_APUS)
41 #include <asm/setup.h>
42 #endif
44 #include <asm/io.h>
45 #include <asm/uaccess.h>
46 #include <asm/page.h>
47 #include <asm/pgtable.h>
49 #include <linux/fb.h>
52 * Frame buffer device initialization and setup routines
55 #define FBPIXMAPSIZE (1024 * 8)
57 static struct notifier_block *fb_notifier_list;
58 struct fb_info *registered_fb[FB_MAX];
59 int num_registered_fb;
62 * Helpers
65 int fb_get_color_depth(struct fb_var_screeninfo *var,
66 struct fb_fix_screeninfo *fix)
68 int depth = 0;
70 if (fix->visual == FB_VISUAL_MONO01 ||
71 fix->visual == FB_VISUAL_MONO10)
72 depth = 1;
73 else {
74 if (var->green.length == var->blue.length &&
75 var->green.length == var->red.length &&
76 var->green.offset == var->blue.offset &&
77 var->green.offset == var->red.offset)
78 depth = var->green.length;
79 else
80 depth = var->green.length + var->red.length +
81 var->blue.length;
84 return depth;
86 EXPORT_SYMBOL(fb_get_color_depth);
89 * Data padding functions.
91 void fb_pad_aligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 s_pitch, u32 height)
93 __fb_pad_aligned_buffer(dst, d_pitch, src, s_pitch, height);
95 EXPORT_SYMBOL(fb_pad_aligned_buffer);
97 void fb_pad_unaligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 idx, u32 height,
98 u32 shift_high, u32 shift_low, u32 mod)
100 u8 mask = (u8) (0xfff << shift_high), tmp;
101 int i, j;
103 for (i = height; i--; ) {
104 for (j = 0; j < idx; j++) {
105 tmp = dst[j];
106 tmp &= mask;
107 tmp |= *src >> shift_low;
108 dst[j] = tmp;
109 tmp = *src << shift_high;
110 dst[j+1] = tmp;
111 src++;
113 tmp = dst[idx];
114 tmp &= mask;
115 tmp |= *src >> shift_low;
116 dst[idx] = tmp;
117 if (shift_high < mod) {
118 tmp = *src << shift_high;
119 dst[idx+1] = tmp;
121 src++;
122 dst += d_pitch;
125 EXPORT_SYMBOL(fb_pad_unaligned_buffer);
128 * we need to lock this section since fb_cursor
129 * may use fb_imageblit()
131 char* fb_get_buffer_offset(struct fb_info *info, struct fb_pixmap *buf, u32 size)
133 u32 align = buf->buf_align - 1, offset;
134 char *addr = buf->addr;
136 /* If IO mapped, we need to sync before access, no sharing of
137 * the pixmap is done
139 if (buf->flags & FB_PIXMAP_IO) {
140 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
141 info->fbops->fb_sync(info);
142 return addr;
145 /* See if we fit in the remaining pixmap space */
146 offset = buf->offset + align;
147 offset &= ~align;
148 if (offset + size > buf->size) {
149 /* We do not fit. In order to be able to re-use the buffer,
150 * we must ensure no asynchronous DMA'ing or whatever operation
151 * is in progress, we sync for that.
153 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
154 info->fbops->fb_sync(info);
155 offset = 0;
157 buf->offset = offset + size;
158 addr += offset;
160 return addr;
163 #ifdef CONFIG_LOGO
164 #include <linux/linux_logo.h>
166 static inline unsigned safe_shift(unsigned d, int n)
168 return n < 0 ? d >> -n : d << n;
171 static void fb_set_logocmap(struct fb_info *info,
172 const struct linux_logo *logo)
174 struct fb_cmap palette_cmap;
175 u16 palette_green[16];
176 u16 palette_blue[16];
177 u16 palette_red[16];
178 int i, j, n;
179 const unsigned char *clut = logo->clut;
181 palette_cmap.start = 0;
182 palette_cmap.len = 16;
183 palette_cmap.red = palette_red;
184 palette_cmap.green = palette_green;
185 palette_cmap.blue = palette_blue;
186 palette_cmap.transp = NULL;
188 for (i = 0; i < logo->clutsize; i += n) {
189 n = logo->clutsize - i;
190 /* palette_cmap provides space for only 16 colors at once */
191 if (n > 16)
192 n = 16;
193 palette_cmap.start = 32 + i;
194 palette_cmap.len = n;
195 for (j = 0; j < n; ++j) {
196 palette_cmap.red[j] = clut[0] << 8 | clut[0];
197 palette_cmap.green[j] = clut[1] << 8 | clut[1];
198 palette_cmap.blue[j] = clut[2] << 8 | clut[2];
199 clut += 3;
201 fb_set_cmap(&palette_cmap, info);
205 static void fb_set_logo_truepalette(struct fb_info *info,
206 const struct linux_logo *logo,
207 u32 *palette)
209 unsigned char mask[9] = { 0,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff };
210 unsigned char redmask, greenmask, bluemask;
211 int redshift, greenshift, blueshift;
212 int i;
213 const unsigned char *clut = logo->clut;
216 * We have to create a temporary palette since console palette is only
217 * 16 colors long.
219 /* Bug: Doesn't obey msb_right ... (who needs that?) */
220 redmask = mask[info->var.red.length < 8 ? info->var.red.length : 8];
221 greenmask = mask[info->var.green.length < 8 ? info->var.green.length : 8];
222 bluemask = mask[info->var.blue.length < 8 ? info->var.blue.length : 8];
223 redshift = info->var.red.offset - (8 - info->var.red.length);
224 greenshift = info->var.green.offset - (8 - info->var.green.length);
225 blueshift = info->var.blue.offset - (8 - info->var.blue.length);
227 for ( i = 0; i < logo->clutsize; i++) {
228 palette[i+32] = (safe_shift((clut[0] & redmask), redshift) |
229 safe_shift((clut[1] & greenmask), greenshift) |
230 safe_shift((clut[2] & bluemask), blueshift));
231 clut += 3;
235 static void fb_set_logo_directpalette(struct fb_info *info,
236 const struct linux_logo *logo,
237 u32 *palette)
239 int redshift, greenshift, blueshift;
240 int i;
242 redshift = info->var.red.offset;
243 greenshift = info->var.green.offset;
244 blueshift = info->var.blue.offset;
246 for (i = 32; i < logo->clutsize; i++)
247 palette[i] = i << redshift | i << greenshift | i << blueshift;
250 static void fb_set_logo(struct fb_info *info,
251 const struct linux_logo *logo, u8 *dst,
252 int depth)
254 int i, j, k;
255 const u8 *src = logo->data;
256 u8 xor = (info->fix.visual == FB_VISUAL_MONO01) ? 0xff : 0;
257 u8 fg = 1, d;
259 if (fb_get_color_depth(&info->var, &info->fix) == 3)
260 fg = 7;
262 if (info->fix.visual == FB_VISUAL_MONO01 ||
263 info->fix.visual == FB_VISUAL_MONO10)
264 fg = ~((u8) (0xfff << info->var.green.length));
266 switch (depth) {
267 case 4:
268 for (i = 0; i < logo->height; i++)
269 for (j = 0; j < logo->width; src++) {
270 *dst++ = *src >> 4;
271 j++;
272 if (j < logo->width) {
273 *dst++ = *src & 0x0f;
274 j++;
277 break;
278 case 1:
279 for (i = 0; i < logo->height; i++) {
280 for (j = 0; j < logo->width; src++) {
281 d = *src ^ xor;
282 for (k = 7; k >= 0; k--) {
283 *dst++ = ((d >> k) & 1) ? fg : 0;
284 j++;
288 break;
293 * Three (3) kinds of logo maps exist. linux_logo_clut224 (>16 colors),
294 * linux_logo_vga16 (16 colors) and linux_logo_mono (2 colors). Depending on
295 * the visual format and color depth of the framebuffer, the DAC, the
296 * pseudo_palette, and the logo data will be adjusted accordingly.
298 * Case 1 - linux_logo_clut224:
299 * Color exceeds the number of console colors (16), thus we set the hardware DAC
300 * using fb_set_cmap() appropriately. The "needs_cmapreset" flag will be set.
302 * For visuals that require color info from the pseudo_palette, we also construct
303 * one for temporary use. The "needs_directpalette" or "needs_truepalette" flags
304 * will be set.
306 * Case 2 - linux_logo_vga16:
307 * The number of colors just matches the console colors, thus there is no need
308 * to set the DAC or the pseudo_palette. However, the bitmap is packed, ie,
309 * each byte contains color information for two pixels (upper and lower nibble).
310 * To be consistent with fb_imageblit() usage, we therefore separate the two
311 * nibbles into separate bytes. The "depth" flag will be set to 4.
313 * Case 3 - linux_logo_mono:
314 * This is similar with Case 2. Each byte contains information for 8 pixels.
315 * We isolate each bit and expand each into a byte. The "depth" flag will
316 * be set to 1.
318 static struct logo_data {
319 int depth;
320 int needs_directpalette;
321 int needs_truepalette;
322 int needs_cmapreset;
323 const struct linux_logo *logo;
324 } fb_logo;
326 static void fb_rotate_logo_ud(const u8 *in, u8 *out, u32 width, u32 height)
328 u32 size = width * height, i;
330 out += size - 1;
332 for (i = size; i--; )
333 *out-- = *in++;
336 static void fb_rotate_logo_cw(const u8 *in, u8 *out, u32 width, u32 height)
338 int i, j, w = width - 1;
340 for (i = 0; i < height; i++)
341 for (j = 0; j < width; j++)
342 out[height * j + w - i] = *in++;
345 static void fb_rotate_logo_ccw(const u8 *in, u8 *out, u32 width, u32 height)
347 int i, j, w = width - 1;
349 for (i = 0; i < height; i++)
350 for (j = 0; j < width; j++)
351 out[height * (w - j) + i] = *in++;
354 static void fb_rotate_logo(struct fb_info *info, u8 *dst,
355 struct fb_image *image, int rotate)
357 u32 tmp;
359 if (rotate == FB_ROTATE_UD) {
360 image->dx = info->var.xres - image->width;
361 image->dy = info->var.yres - image->height;
362 fb_rotate_logo_ud(image->data, dst, image->width,
363 image->height);
364 } else if (rotate == FB_ROTATE_CW) {
365 tmp = image->width;
366 image->width = image->height;
367 image->height = tmp;
368 image->dx = info->var.xres - image->height;
369 fb_rotate_logo_cw(image->data, dst, image->width,
370 image->height);
371 } else if (rotate == FB_ROTATE_CCW) {
372 tmp = image->width;
373 image->width = image->height;
374 image->height = tmp;
375 image->dy = info->var.yres - image->width;
376 fb_rotate_logo_ccw(image->data, dst, image->width,
377 image->height);
380 image->data = dst;
383 static void fb_do_show_logo(struct fb_info *info, struct fb_image *image,
384 int rotate)
386 int x;
388 if (rotate == FB_ROTATE_UR) {
389 for (x = 0; x < num_online_cpus() &&
390 x * (fb_logo.logo->width + 8) <=
391 info->var.xres - fb_logo.logo->width; x++) {
392 info->fbops->fb_imageblit(info, image);
393 image->dx += fb_logo.logo->width + 8;
395 } else if (rotate == FB_ROTATE_UD) {
396 for (x = 0; x < num_online_cpus() &&
397 x * (fb_logo.logo->width + 8) <=
398 info->var.xres - fb_logo.logo->width; x++) {
399 info->fbops->fb_imageblit(info, image);
400 image->dx -= fb_logo.logo->width + 8;
402 } else if (rotate == FB_ROTATE_CW) {
403 for (x = 0; x < num_online_cpus() &&
404 x * (fb_logo.logo->width + 8) <=
405 info->var.yres - fb_logo.logo->width; x++) {
406 info->fbops->fb_imageblit(info, image);
407 image->dy += fb_logo.logo->width + 8;
409 } else if (rotate == FB_ROTATE_CCW) {
410 for (x = 0; x < num_online_cpus() &&
411 x * (fb_logo.logo->width + 8) <=
412 info->var.yres - fb_logo.logo->width; x++) {
413 info->fbops->fb_imageblit(info, image);
414 image->dy -= fb_logo.logo->width + 8;
419 int fb_prepare_logo(struct fb_info *info, int rotate)
421 int depth = fb_get_color_depth(&info->var, &info->fix);
422 int yres;
424 memset(&fb_logo, 0, sizeof(struct logo_data));
426 if (info->flags & FBINFO_MISC_TILEBLITTING)
427 return 0;
429 if (info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
430 depth = info->var.blue.length;
431 if (info->var.red.length < depth)
432 depth = info->var.red.length;
433 if (info->var.green.length < depth)
434 depth = info->var.green.length;
437 if (depth >= 8) {
438 switch (info->fix.visual) {
439 case FB_VISUAL_TRUECOLOR:
440 fb_logo.needs_truepalette = 1;
441 break;
442 case FB_VISUAL_DIRECTCOLOR:
443 fb_logo.needs_directpalette = 1;
444 fb_logo.needs_cmapreset = 1;
445 break;
446 case FB_VISUAL_PSEUDOCOLOR:
447 fb_logo.needs_cmapreset = 1;
448 break;
452 /* Return if no suitable logo was found */
453 fb_logo.logo = fb_find_logo(depth);
455 if (rotate == FB_ROTATE_UR || rotate == FB_ROTATE_UD)
456 yres = info->var.yres;
457 else
458 yres = info->var.xres;
460 if (fb_logo.logo && fb_logo.logo->height > yres) {
461 fb_logo.logo = NULL;
462 return 0;
465 /* What depth we asked for might be different from what we get */
466 if (fb_logo.logo->type == LINUX_LOGO_CLUT224)
467 fb_logo.depth = 8;
468 else if (fb_logo.logo->type == LINUX_LOGO_VGA16)
469 fb_logo.depth = 4;
470 else
471 fb_logo.depth = 1;
472 return fb_logo.logo->height;
475 int fb_show_logo(struct fb_info *info, int rotate)
477 u32 *palette = NULL, *saved_pseudo_palette = NULL;
478 unsigned char *logo_new = NULL, *logo_rotate = NULL;
479 struct fb_image image;
481 /* Return if the frame buffer is not mapped or suspended */
482 if (fb_logo.logo == NULL || info->state != FBINFO_STATE_RUNNING)
483 return 0;
485 image.depth = 8;
486 image.data = fb_logo.logo->data;
488 if (fb_logo.needs_cmapreset)
489 fb_set_logocmap(info, fb_logo.logo);
491 if (fb_logo.needs_truepalette ||
492 fb_logo.needs_directpalette) {
493 palette = kmalloc(256 * 4, GFP_KERNEL);
494 if (palette == NULL)
495 return 0;
497 if (fb_logo.needs_truepalette)
498 fb_set_logo_truepalette(info, fb_logo.logo, palette);
499 else
500 fb_set_logo_directpalette(info, fb_logo.logo, palette);
502 saved_pseudo_palette = info->pseudo_palette;
503 info->pseudo_palette = palette;
506 if (fb_logo.depth <= 4) {
507 logo_new = kmalloc(fb_logo.logo->width * fb_logo.logo->height,
508 GFP_KERNEL);
509 if (logo_new == NULL) {
510 kfree(palette);
511 if (saved_pseudo_palette)
512 info->pseudo_palette = saved_pseudo_palette;
513 return 0;
515 image.data = logo_new;
516 fb_set_logo(info, fb_logo.logo, logo_new, fb_logo.depth);
519 image.dx = 0;
520 image.dy = 0;
521 image.width = fb_logo.logo->width;
522 image.height = fb_logo.logo->height;
524 if (rotate) {
525 logo_rotate = kmalloc(fb_logo.logo->width *
526 fb_logo.logo->height, GFP_KERNEL);
527 if (logo_rotate)
528 fb_rotate_logo(info, logo_rotate, &image, rotate);
531 fb_do_show_logo(info, &image, rotate);
533 kfree(palette);
534 if (saved_pseudo_palette != NULL)
535 info->pseudo_palette = saved_pseudo_palette;
536 kfree(logo_new);
537 kfree(logo_rotate);
538 return fb_logo.logo->height;
540 #else
541 int fb_prepare_logo(struct fb_info *info, int rotate) { return 0; }
542 int fb_show_logo(struct fb_info *info, int rotate) { return 0; }
543 #endif /* CONFIG_LOGO */
545 static int fbmem_read_proc(char *buf, char **start, off_t offset,
546 int len, int *eof, void *private)
548 struct fb_info **fi;
549 int clen;
551 clen = 0;
552 for (fi = registered_fb; fi < &registered_fb[FB_MAX] && len < 4000; fi++)
553 if (*fi)
554 clen += sprintf(buf + clen, "%d %s\n",
555 (*fi)->node,
556 (*fi)->fix.id);
557 *start = buf + offset;
558 if (clen > offset)
559 clen -= offset;
560 else
561 clen = 0;
562 return clen < len ? clen : len;
565 static ssize_t
566 fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
568 unsigned long p = *ppos;
569 struct inode *inode = file->f_dentry->d_inode;
570 int fbidx = iminor(inode);
571 struct fb_info *info = registered_fb[fbidx];
572 u32 *buffer, *dst;
573 u32 __iomem *src;
574 int c, i, cnt = 0, err = 0;
575 unsigned long total_size;
577 if (!info || ! info->screen_base)
578 return -ENODEV;
580 if (info->state != FBINFO_STATE_RUNNING)
581 return -EPERM;
583 if (info->fbops->fb_read)
584 return info->fbops->fb_read(file, buf, count, ppos);
586 total_size = info->screen_size;
587 if (total_size == 0)
588 total_size = info->fix.smem_len;
590 if (p >= total_size)
591 return 0;
592 if (count >= total_size)
593 count = total_size;
594 if (count + p > total_size)
595 count = total_size - p;
597 cnt = 0;
598 buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
599 GFP_KERNEL);
600 if (!buffer)
601 return -ENOMEM;
603 src = (u32 __iomem *) (info->screen_base + p);
605 if (info->fbops->fb_sync)
606 info->fbops->fb_sync(info);
608 while (count) {
609 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
610 dst = buffer;
611 for (i = c >> 2; i--; )
612 *dst++ = fb_readl(src++);
613 if (c & 3) {
614 u8 *dst8 = (u8 *) dst;
615 u8 __iomem *src8 = (u8 __iomem *) src;
617 for (i = c & 3; i--;)
618 *dst8++ = fb_readb(src8++);
620 src = (u32 __iomem *) src8;
623 if (copy_to_user(buf, buffer, c)) {
624 err = -EFAULT;
625 break;
627 *ppos += c;
628 buf += c;
629 cnt += c;
630 count -= c;
633 kfree(buffer);
634 return (err) ? err : cnt;
637 static ssize_t
638 fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
640 unsigned long p = *ppos;
641 struct inode *inode = file->f_dentry->d_inode;
642 int fbidx = iminor(inode);
643 struct fb_info *info = registered_fb[fbidx];
644 u32 *buffer, *src;
645 u32 __iomem *dst;
646 int c, i, cnt = 0, err;
647 unsigned long total_size;
649 if (!info || !info->screen_base)
650 return -ENODEV;
652 if (info->state != FBINFO_STATE_RUNNING)
653 return -EPERM;
655 if (info->fbops->fb_write)
656 return info->fbops->fb_write(file, buf, count, ppos);
658 total_size = info->screen_size;
659 if (total_size == 0)
660 total_size = info->fix.smem_len;
662 if (p > total_size)
663 return -ENOSPC;
664 if (count >= total_size)
665 count = total_size;
666 err = 0;
667 if (count + p > total_size) {
668 count = total_size - p;
669 err = -ENOSPC;
671 cnt = 0;
672 buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
673 GFP_KERNEL);
674 if (!buffer)
675 return -ENOMEM;
677 dst = (u32 __iomem *) (info->screen_base + p);
679 if (info->fbops->fb_sync)
680 info->fbops->fb_sync(info);
682 while (count) {
683 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
684 src = buffer;
685 if (copy_from_user(src, buf, c)) {
686 err = -EFAULT;
687 break;
689 for (i = c >> 2; i--; )
690 fb_writel(*src++, dst++);
691 if (c & 3) {
692 u8 *src8 = (u8 *) src;
693 u8 __iomem *dst8 = (u8 __iomem *) dst;
695 for (i = c & 3; i--; )
696 fb_writeb(*src8++, dst8++);
698 dst = (u32 __iomem *) dst8;
700 *ppos += c;
701 buf += c;
702 cnt += c;
703 count -= c;
705 kfree(buffer);
707 return (err) ? err : cnt;
710 #ifdef CONFIG_KMOD
711 static void try_to_load(int fb)
713 request_module("fb%d", fb);
715 #endif /* CONFIG_KMOD */
718 fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var)
720 int xoffset = var->xoffset;
721 int yoffset = var->yoffset;
722 int err;
724 if (xoffset < 0 || yoffset < 0 || !info->fbops->fb_pan_display ||
725 xoffset + info->var.xres > info->var.xres_virtual ||
726 yoffset + info->var.yres > info->var.yres_virtual)
727 return -EINVAL;
728 if ((err = info->fbops->fb_pan_display(var, info)))
729 return err;
730 info->var.xoffset = var->xoffset;
731 info->var.yoffset = var->yoffset;
732 if (var->vmode & FB_VMODE_YWRAP)
733 info->var.vmode |= FB_VMODE_YWRAP;
734 else
735 info->var.vmode &= ~FB_VMODE_YWRAP;
736 return 0;
740 fb_set_var(struct fb_info *info, struct fb_var_screeninfo *var)
742 int err, flags = info->flags;
744 if (var->activate & FB_ACTIVATE_INV_MODE) {
745 struct fb_videomode mode1, mode2;
746 int ret = 0;
748 fb_var_to_videomode(&mode1, var);
749 fb_var_to_videomode(&mode2, &info->var);
750 /* make sure we don't delete the videomode of current var */
751 ret = fb_mode_is_equal(&mode1, &mode2);
753 if (!ret) {
754 struct fb_event event;
756 event.info = info;
757 event.data = &mode1;
758 ret = notifier_call_chain(&fb_notifier_list,
759 FB_EVENT_MODE_DELETE, &event);
762 if (!ret)
763 fb_delete_videomode(&mode1, &info->modelist);
765 return ret;
768 if ((var->activate & FB_ACTIVATE_FORCE) ||
769 memcmp(&info->var, var, sizeof(struct fb_var_screeninfo))) {
770 if (!info->fbops->fb_check_var) {
771 *var = info->var;
772 return 0;
775 if ((err = info->fbops->fb_check_var(var, info)))
776 return err;
778 if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
779 struct fb_videomode mode;
780 int err = 0;
782 info->var = *var;
783 if (info->fbops->fb_set_par)
784 info->fbops->fb_set_par(info);
786 fb_pan_display(info, &info->var);
788 fb_set_cmap(&info->cmap, info);
790 fb_var_to_videomode(&mode, &info->var);
792 if (info->modelist.prev && info->modelist.next &&
793 !list_empty(&info->modelist))
794 err = fb_add_videomode(&mode, &info->modelist);
796 if (!err && (flags & FBINFO_MISC_USEREVENT)) {
797 struct fb_event event;
798 int evnt = (var->activate & FB_ACTIVATE_ALL) ?
799 FB_EVENT_MODE_CHANGE_ALL :
800 FB_EVENT_MODE_CHANGE;
802 info->flags &= ~FBINFO_MISC_USEREVENT;
803 event.info = info;
804 notifier_call_chain(&fb_notifier_list, evnt,
805 &event);
809 return 0;
813 fb_blank(struct fb_info *info, int blank)
815 int ret = -EINVAL;
817 if (blank > FB_BLANK_POWERDOWN)
818 blank = FB_BLANK_POWERDOWN;
820 if (info->fbops->fb_blank)
821 ret = info->fbops->fb_blank(blank, info);
823 if (!ret) {
824 struct fb_event event;
826 event.info = info;
827 event.data = &blank;
828 notifier_call_chain(&fb_notifier_list, FB_EVENT_BLANK, &event);
831 return ret;
834 static int
835 fb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
836 unsigned long arg)
838 int fbidx = iminor(inode);
839 struct fb_info *info = registered_fb[fbidx];
840 struct fb_ops *fb = info->fbops;
841 struct fb_var_screeninfo var;
842 struct fb_fix_screeninfo fix;
843 struct fb_con2fbmap con2fb;
844 struct fb_cmap_user cmap;
845 struct fb_event event;
846 void __user *argp = (void __user *)arg;
847 int i;
849 if (!fb)
850 return -ENODEV;
851 switch (cmd) {
852 case FBIOGET_VSCREENINFO:
853 return copy_to_user(argp, &info->var,
854 sizeof(var)) ? -EFAULT : 0;
855 case FBIOPUT_VSCREENINFO:
856 if (copy_from_user(&var, argp, sizeof(var)))
857 return -EFAULT;
858 acquire_console_sem();
859 info->flags |= FBINFO_MISC_USEREVENT;
860 i = fb_set_var(info, &var);
861 info->flags &= ~FBINFO_MISC_USEREVENT;
862 release_console_sem();
863 if (i) return i;
864 if (copy_to_user(argp, &var, sizeof(var)))
865 return -EFAULT;
866 return 0;
867 case FBIOGET_FSCREENINFO:
868 return copy_to_user(argp, &info->fix,
869 sizeof(fix)) ? -EFAULT : 0;
870 case FBIOPUTCMAP:
871 if (copy_from_user(&cmap, argp, sizeof(cmap)))
872 return -EFAULT;
873 return (fb_set_user_cmap(&cmap, info));
874 case FBIOGETCMAP:
875 if (copy_from_user(&cmap, argp, sizeof(cmap)))
876 return -EFAULT;
877 return fb_cmap_to_user(&info->cmap, &cmap);
878 case FBIOPAN_DISPLAY:
879 if (copy_from_user(&var, argp, sizeof(var)))
880 return -EFAULT;
881 acquire_console_sem();
882 i = fb_pan_display(info, &var);
883 release_console_sem();
884 if (i)
885 return i;
886 if (copy_to_user(argp, &var, sizeof(var)))
887 return -EFAULT;
888 return 0;
889 case FBIO_CURSOR:
890 return -EINVAL;
891 case FBIOGET_CON2FBMAP:
892 if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
893 return -EFAULT;
894 if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
895 return -EINVAL;
896 con2fb.framebuffer = -1;
897 event.info = info;
898 event.data = &con2fb;
899 notifier_call_chain(&fb_notifier_list,
900 FB_EVENT_GET_CONSOLE_MAP, &event);
901 return copy_to_user(argp, &con2fb,
902 sizeof(con2fb)) ? -EFAULT : 0;
903 case FBIOPUT_CON2FBMAP:
904 if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
905 return - EFAULT;
906 if (con2fb.console < 0 || con2fb.console > MAX_NR_CONSOLES)
907 return -EINVAL;
908 if (con2fb.framebuffer < 0 || con2fb.framebuffer >= FB_MAX)
909 return -EINVAL;
910 #ifdef CONFIG_KMOD
911 if (!registered_fb[con2fb.framebuffer])
912 try_to_load(con2fb.framebuffer);
913 #endif /* CONFIG_KMOD */
914 if (!registered_fb[con2fb.framebuffer])
915 return -EINVAL;
916 event.info = info;
917 event.data = &con2fb;
918 return notifier_call_chain(&fb_notifier_list,
919 FB_EVENT_SET_CONSOLE_MAP,
920 &event);
921 case FBIOBLANK:
922 acquire_console_sem();
923 info->flags |= FBINFO_MISC_USEREVENT;
924 i = fb_blank(info, arg);
925 info->flags &= ~FBINFO_MISC_USEREVENT;
926 release_console_sem();
927 return i;
928 default:
929 if (fb->fb_ioctl == NULL)
930 return -EINVAL;
931 return fb->fb_ioctl(inode, file, cmd, arg, info);
935 #ifdef CONFIG_COMPAT
936 static long
937 fb_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
939 int fbidx = iminor(file->f_dentry->d_inode);
940 struct fb_info *info = registered_fb[fbidx];
941 struct fb_ops *fb = info->fbops;
942 long ret;
944 if (fb->fb_compat_ioctl == NULL)
945 return -ENOIOCTLCMD;
946 lock_kernel();
947 ret = fb->fb_compat_ioctl(file, cmd, arg, info);
948 unlock_kernel();
949 return ret;
951 #endif
953 static int
954 fb_mmap(struct file *file, struct vm_area_struct * vma)
956 int fbidx = iminor(file->f_dentry->d_inode);
957 struct fb_info *info = registered_fb[fbidx];
958 struct fb_ops *fb = info->fbops;
959 unsigned long off;
960 #if !defined(__sparc__) || defined(__sparc_v9__)
961 unsigned long start;
962 u32 len;
963 #endif
965 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
966 return -EINVAL;
967 off = vma->vm_pgoff << PAGE_SHIFT;
968 if (!fb)
969 return -ENODEV;
970 if (fb->fb_mmap) {
971 int res;
972 lock_kernel();
973 res = fb->fb_mmap(info, file, vma);
974 unlock_kernel();
975 return res;
978 #if defined(__sparc__) && !defined(__sparc_v9__)
979 /* Should never get here, all fb drivers should have their own
980 mmap routines */
981 return -EINVAL;
982 #else
983 /* !sparc32... */
984 lock_kernel();
986 /* frame buffer memory */
987 start = info->fix.smem_start;
988 len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.smem_len);
989 if (off >= len) {
990 /* memory mapped io */
991 off -= len;
992 if (info->var.accel_flags) {
993 unlock_kernel();
994 return -EINVAL;
996 start = info->fix.mmio_start;
997 len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.mmio_len);
999 unlock_kernel();
1000 start &= PAGE_MASK;
1001 if ((vma->vm_end - vma->vm_start + off) > len)
1002 return -EINVAL;
1003 off += start;
1004 vma->vm_pgoff = off >> PAGE_SHIFT;
1005 /* This is an IO map - tell maydump to skip this VMA */
1006 vma->vm_flags |= VM_IO | VM_RESERVED;
1007 #if defined(__sparc_v9__)
1008 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1009 vma->vm_end - vma->vm_start, vma->vm_page_prot))
1010 return -EAGAIN;
1011 #else
1012 #if defined(__mc68000__)
1013 #if defined(CONFIG_SUN3)
1014 pgprot_val(vma->vm_page_prot) |= SUN3_PAGE_NOCACHE;
1015 #elif defined(CONFIG_MMU)
1016 if (CPU_IS_020_OR_030)
1017 pgprot_val(vma->vm_page_prot) |= _PAGE_NOCACHE030;
1018 if (CPU_IS_040_OR_060) {
1019 pgprot_val(vma->vm_page_prot) &= _CACHEMASK040;
1020 /* Use no-cache mode, serialized */
1021 pgprot_val(vma->vm_page_prot) |= _PAGE_NOCACHE_S;
1023 #endif
1024 #elif defined(__powerpc__)
1025 vma->vm_page_prot = phys_mem_access_prot(file, off >> PAGE_SHIFT,
1026 vma->vm_end - vma->vm_start,
1027 vma->vm_page_prot);
1028 #elif defined(__alpha__)
1029 /* Caching is off in the I/O space quadrant by design. */
1030 #elif defined(__i386__) || defined(__x86_64__)
1031 if (boot_cpu_data.x86 > 3)
1032 pgprot_val(vma->vm_page_prot) |= _PAGE_PCD;
1033 #elif defined(__mips__)
1034 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1035 #elif defined(__hppa__)
1036 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
1037 #elif defined(__arm__) || defined(__sh__) || defined(__m32r__)
1038 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1039 #elif defined(__ia64__)
1040 if (efi_range_is_wc(vma->vm_start, vma->vm_end - vma->vm_start))
1041 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1042 else
1043 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1044 #else
1045 #warning What do we have to do here??
1046 #endif
1047 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1048 vma->vm_end - vma->vm_start, vma->vm_page_prot))
1049 return -EAGAIN;
1050 #endif /* !__sparc_v9__ */
1051 return 0;
1052 #endif /* !sparc32 */
1055 static int
1056 fb_open(struct inode *inode, struct file *file)
1058 int fbidx = iminor(inode);
1059 struct fb_info *info;
1060 int res = 0;
1062 if (fbidx >= FB_MAX)
1063 return -ENODEV;
1064 #ifdef CONFIG_KMOD
1065 if (!(info = registered_fb[fbidx]))
1066 try_to_load(fbidx);
1067 #endif /* CONFIG_KMOD */
1068 if (!(info = registered_fb[fbidx]))
1069 return -ENODEV;
1070 if (!try_module_get(info->fbops->owner))
1071 return -ENODEV;
1072 if (info->fbops->fb_open) {
1073 res = info->fbops->fb_open(info,1);
1074 if (res)
1075 module_put(info->fbops->owner);
1077 return res;
1080 static int
1081 fb_release(struct inode *inode, struct file *file)
1083 int fbidx = iminor(inode);
1084 struct fb_info *info;
1086 lock_kernel();
1087 info = registered_fb[fbidx];
1088 if (info->fbops->fb_release)
1089 info->fbops->fb_release(info,1);
1090 module_put(info->fbops->owner);
1091 unlock_kernel();
1092 return 0;
1095 static struct file_operations fb_fops = {
1096 .owner = THIS_MODULE,
1097 .read = fb_read,
1098 .write = fb_write,
1099 .ioctl = fb_ioctl,
1100 #ifdef CONFIG_COMPAT
1101 .compat_ioctl = fb_compat_ioctl,
1102 #endif
1103 .mmap = fb_mmap,
1104 .open = fb_open,
1105 .release = fb_release,
1106 #ifdef HAVE_ARCH_FB_UNMAPPED_AREA
1107 .get_unmapped_area = get_fb_unmapped_area,
1108 #endif
1111 static struct class *fb_class;
1114 * register_framebuffer - registers a frame buffer device
1115 * @fb_info: frame buffer info structure
1117 * Registers a frame buffer device @fb_info.
1119 * Returns negative errno on error, or zero for success.
1124 register_framebuffer(struct fb_info *fb_info)
1126 int i;
1127 struct fb_event event;
1128 struct fb_videomode mode;
1130 if (num_registered_fb == FB_MAX)
1131 return -ENXIO;
1132 num_registered_fb++;
1133 for (i = 0 ; i < FB_MAX; i++)
1134 if (!registered_fb[i])
1135 break;
1136 fb_info->node = i;
1138 fb_info->class_device = class_device_create(fb_class, NULL, MKDEV(FB_MAJOR, i),
1139 fb_info->device, "fb%d", i);
1140 if (IS_ERR(fb_info->class_device)) {
1141 /* Not fatal */
1142 printk(KERN_WARNING "Unable to create class_device for framebuffer %d; errno = %ld\n", i, PTR_ERR(fb_info->class_device));
1143 fb_info->class_device = NULL;
1144 } else
1145 fb_init_class_device(fb_info);
1147 if (fb_info->pixmap.addr == NULL) {
1148 fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);
1149 if (fb_info->pixmap.addr) {
1150 fb_info->pixmap.size = FBPIXMAPSIZE;
1151 fb_info->pixmap.buf_align = 1;
1152 fb_info->pixmap.scan_align = 1;
1153 fb_info->pixmap.access_align = 32;
1154 fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;
1157 fb_info->pixmap.offset = 0;
1159 if (!fb_info->modelist.prev || !fb_info->modelist.next)
1160 INIT_LIST_HEAD(&fb_info->modelist);
1162 fb_var_to_videomode(&mode, &fb_info->var);
1163 fb_add_videomode(&mode, &fb_info->modelist);
1164 registered_fb[i] = fb_info;
1166 devfs_mk_cdev(MKDEV(FB_MAJOR, i),
1167 S_IFCHR | S_IRUGO | S_IWUGO, "fb/%d", i);
1168 event.info = fb_info;
1169 notifier_call_chain(&fb_notifier_list,
1170 FB_EVENT_FB_REGISTERED, &event);
1171 return 0;
1176 * unregister_framebuffer - releases a frame buffer device
1177 * @fb_info: frame buffer info structure
1179 * Unregisters a frame buffer device @fb_info.
1181 * Returns negative errno on error, or zero for success.
1186 unregister_framebuffer(struct fb_info *fb_info)
1188 int i;
1190 i = fb_info->node;
1191 if (!registered_fb[i])
1192 return -EINVAL;
1193 devfs_remove("fb/%d", i);
1195 if (fb_info->pixmap.addr && (fb_info->pixmap.flags & FB_PIXMAP_DEFAULT))
1196 kfree(fb_info->pixmap.addr);
1197 fb_destroy_modelist(&fb_info->modelist);
1198 registered_fb[i]=NULL;
1199 num_registered_fb--;
1200 fb_cleanup_class_device(fb_info);
1201 class_device_destroy(fb_class, MKDEV(FB_MAJOR, i));
1202 return 0;
1206 * fb_register_client - register a client notifier
1207 * @nb: notifier block to callback on events
1209 int fb_register_client(struct notifier_block *nb)
1211 return notifier_chain_register(&fb_notifier_list, nb);
1215 * fb_unregister_client - unregister a client notifier
1216 * @nb: notifier block to callback on events
1218 int fb_unregister_client(struct notifier_block *nb)
1220 return notifier_chain_unregister(&fb_notifier_list, nb);
1224 * fb_set_suspend - low level driver signals suspend
1225 * @info: framebuffer affected
1226 * @state: 0 = resuming, !=0 = suspending
1228 * This is meant to be used by low level drivers to
1229 * signal suspend/resume to the core & clients.
1230 * It must be called with the console semaphore held
1232 void fb_set_suspend(struct fb_info *info, int state)
1234 struct fb_event event;
1236 event.info = info;
1237 if (state) {
1238 notifier_call_chain(&fb_notifier_list, FB_EVENT_SUSPEND, &event);
1239 info->state = FBINFO_STATE_SUSPENDED;
1240 } else {
1241 info->state = FBINFO_STATE_RUNNING;
1242 notifier_call_chain(&fb_notifier_list, FB_EVENT_RESUME, &event);
1247 * fbmem_init - init frame buffer subsystem
1249 * Initialize the frame buffer subsystem.
1251 * NOTE: This function is _only_ to be called by drivers/char/mem.c.
1255 static int __init
1256 fbmem_init(void)
1258 create_proc_read_entry("fb", 0, NULL, fbmem_read_proc, NULL);
1260 devfs_mk_dir("fb");
1261 if (register_chrdev(FB_MAJOR,"fb",&fb_fops))
1262 printk("unable to get major %d for fb devs\n", FB_MAJOR);
1264 fb_class = class_create(THIS_MODULE, "graphics");
1265 if (IS_ERR(fb_class)) {
1266 printk(KERN_WARNING "Unable to create fb class; errno = %ld\n", PTR_ERR(fb_class));
1267 fb_class = NULL;
1269 return 0;
1272 #ifdef MODULE
1273 module_init(fbmem_init);
1274 static void __exit
1275 fbmem_exit(void)
1277 class_destroy(fb_class);
1278 unregister_chrdev(FB_MAJOR, "fb");
1281 module_exit(fbmem_exit);
1282 MODULE_LICENSE("GPL");
1283 MODULE_DESCRIPTION("Framebuffer base");
1284 #else
1285 subsys_initcall(fbmem_init);
1286 #endif
1288 int fb_new_modelist(struct fb_info *info)
1290 struct fb_event event;
1291 struct fb_var_screeninfo var = info->var;
1292 struct list_head *pos, *n;
1293 struct fb_modelist *modelist;
1294 struct fb_videomode *m, mode;
1295 int err = 1;
1297 list_for_each_safe(pos, n, &info->modelist) {
1298 modelist = list_entry(pos, struct fb_modelist, list);
1299 m = &modelist->mode;
1300 fb_videomode_to_var(&var, m);
1301 var.activate = FB_ACTIVATE_TEST;
1302 err = fb_set_var(info, &var);
1303 fb_var_to_videomode(&mode, &var);
1304 if (err || !fb_mode_is_equal(m, &mode)) {
1305 list_del(pos);
1306 kfree(pos);
1310 err = 1;
1312 if (!list_empty(&info->modelist)) {
1313 event.info = info;
1314 err = notifier_call_chain(&fb_notifier_list,
1315 FB_EVENT_NEW_MODELIST,
1316 &event);
1319 return err;
1322 static char *video_options[FB_MAX];
1323 static int ofonly;
1325 extern const char *global_mode_option;
1328 * fb_get_options - get kernel boot parameters
1329 * @name: framebuffer name as it would appear in
1330 * the boot parameter line
1331 * (video=<name>:<options>)
1332 * @option: the option will be stored here
1334 * NOTE: Needed to maintain backwards compatibility
1336 int fb_get_options(char *name, char **option)
1338 char *opt, *options = NULL;
1339 int opt_len, retval = 0;
1340 int name_len = strlen(name), i;
1342 if (name_len && ofonly && strncmp(name, "offb", 4))
1343 retval = 1;
1345 if (name_len && !retval) {
1346 for (i = 0; i < FB_MAX; i++) {
1347 if (video_options[i] == NULL)
1348 continue;
1349 opt_len = strlen(video_options[i]);
1350 if (!opt_len)
1351 continue;
1352 opt = video_options[i];
1353 if (!strncmp(name, opt, name_len) &&
1354 opt[name_len] == ':')
1355 options = opt + name_len + 1;
1358 if (options && !strncmp(options, "off", 3))
1359 retval = 1;
1361 if (option)
1362 *option = options;
1364 return retval;
1368 * video_setup - process command line options
1369 * @options: string of options
1371 * Process command line options for frame buffer subsystem.
1373 * NOTE: This function is a __setup and __init function.
1374 * It only stores the options. Drivers have to call
1375 * fb_get_options() as necessary.
1377 * Returns zero.
1380 static int __init video_setup(char *options)
1382 int i, global = 0;
1384 if (!options || !*options)
1385 global = 1;
1387 if (!global && !strncmp(options, "ofonly", 6)) {
1388 ofonly = 1;
1389 global = 1;
1392 if (!global && !strstr(options, "fb:")) {
1393 global_mode_option = options;
1394 global = 1;
1397 if (!global) {
1398 for (i = 0; i < FB_MAX; i++) {
1399 if (video_options[i] == NULL) {
1400 video_options[i] = options;
1401 break;
1407 return 0;
1409 __setup("video=", video_setup);
1412 * Visible symbols for modules
1415 EXPORT_SYMBOL(register_framebuffer);
1416 EXPORT_SYMBOL(unregister_framebuffer);
1417 EXPORT_SYMBOL(num_registered_fb);
1418 EXPORT_SYMBOL(registered_fb);
1419 EXPORT_SYMBOL(fb_prepare_logo);
1420 EXPORT_SYMBOL(fb_show_logo);
1421 EXPORT_SYMBOL(fb_set_var);
1422 EXPORT_SYMBOL(fb_blank);
1423 EXPORT_SYMBOL(fb_pan_display);
1424 EXPORT_SYMBOL(fb_get_buffer_offset);
1425 EXPORT_SYMBOL(fb_set_suspend);
1426 EXPORT_SYMBOL(fb_register_client);
1427 EXPORT_SYMBOL(fb_unregister_client);
1428 EXPORT_SYMBOL(fb_get_options);
1429 EXPORT_SYMBOL(fb_new_modelist);
1431 MODULE_LICENSE("GPL");