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
14 #include <linux/module.h>
16 #include <linux/compat.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/smp_lock.h>
20 #include <linux/kernel.h>
21 #include <linux/major.h>
22 #include <linux/slab.h>
24 #include <linux/mman.h>
26 #include <linux/init.h>
27 #include <linux/linux_logo.h>
28 #include <linux/proc_fs.h>
29 #include <linux/seq_file.h>
30 #include <linux/console.h>
31 #include <linux/kmod.h>
32 #include <linux/err.h>
33 #include <linux/device.h>
34 #include <linux/efi.h>
41 * Frame buffer device initialization and setup routines
44 #define FBPIXMAPSIZE (1024 * 8)
46 struct fb_info *registered_fb[FB_MAX] __read_mostly;
47 int num_registered_fb __read_mostly;
53 int fb_get_color_depth(struct fb_var_screeninfo *var,
54 struct fb_fix_screeninfo *fix)
58 if (fix->visual == FB_VISUAL_MONO01 ||
59 fix->visual == FB_VISUAL_MONO10)
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;
68 depth = var->green.length + var->red.length +
74 EXPORT_SYMBOL(fb_get_color_depth);
77 * Data padding functions.
79 void fb_pad_aligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 s_pitch, u32 height)
81 __fb_pad_aligned_buffer(dst, d_pitch, src, s_pitch, height);
83 EXPORT_SYMBOL(fb_pad_aligned_buffer);
85 void fb_pad_unaligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 idx, u32 height,
86 u32 shift_high, u32 shift_low, u32 mod)
88 u8 mask = (u8) (0xfff << shift_high), tmp;
91 for (i = height; i--; ) {
92 for (j = 0; j < idx; j++) {
95 tmp |= *src >> shift_low;
97 tmp = *src << shift_high;
103 tmp |= *src >> shift_low;
105 if (shift_high < mod) {
106 tmp = *src << shift_high;
113 EXPORT_SYMBOL(fb_pad_unaligned_buffer);
116 * we need to lock this section since fb_cursor
117 * may use fb_imageblit()
119 char* fb_get_buffer_offset(struct fb_info *info, struct fb_pixmap *buf, u32 size)
121 u32 align = buf->buf_align - 1, offset;
122 char *addr = buf->addr;
124 /* If IO mapped, we need to sync before access, no sharing of
127 if (buf->flags & FB_PIXMAP_IO) {
128 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
129 info->fbops->fb_sync(info);
133 /* See if we fit in the remaining pixmap space */
134 offset = buf->offset + align;
136 if (offset + size > buf->size) {
137 /* We do not fit. In order to be able to re-use the buffer,
138 * we must ensure no asynchronous DMA'ing or whatever operation
139 * is in progress, we sync for that.
141 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
142 info->fbops->fb_sync(info);
145 buf->offset = offset + size;
153 static inline unsigned safe_shift(unsigned d, int n)
155 return n < 0 ? d >> -n : d << n;
158 static void fb_set_logocmap(struct fb_info *info,
159 const struct linux_logo *logo)
161 struct fb_cmap palette_cmap;
162 u16 palette_green[16];
163 u16 palette_blue[16];
166 const unsigned char *clut = logo->clut;
168 palette_cmap.start = 0;
169 palette_cmap.len = 16;
170 palette_cmap.red = palette_red;
171 palette_cmap.green = palette_green;
172 palette_cmap.blue = palette_blue;
173 palette_cmap.transp = NULL;
175 for (i = 0; i < logo->clutsize; i += n) {
176 n = logo->clutsize - i;
177 /* palette_cmap provides space for only 16 colors at once */
180 palette_cmap.start = 32 + i;
181 palette_cmap.len = n;
182 for (j = 0; j < n; ++j) {
183 palette_cmap.red[j] = clut[0] << 8 | clut[0];
184 palette_cmap.green[j] = clut[1] << 8 | clut[1];
185 palette_cmap.blue[j] = clut[2] << 8 | clut[2];
188 fb_set_cmap(&palette_cmap, info);
192 static void fb_set_logo_truepalette(struct fb_info *info,
193 const struct linux_logo *logo,
196 static const unsigned char mask[] = { 0,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff };
197 unsigned char redmask, greenmask, bluemask;
198 int redshift, greenshift, blueshift;
200 const unsigned char *clut = logo->clut;
203 * We have to create a temporary palette since console palette is only
206 /* Bug: Doesn't obey msb_right ... (who needs that?) */
207 redmask = mask[info->var.red.length < 8 ? info->var.red.length : 8];
208 greenmask = mask[info->var.green.length < 8 ? info->var.green.length : 8];
209 bluemask = mask[info->var.blue.length < 8 ? info->var.blue.length : 8];
210 redshift = info->var.red.offset - (8 - info->var.red.length);
211 greenshift = info->var.green.offset - (8 - info->var.green.length);
212 blueshift = info->var.blue.offset - (8 - info->var.blue.length);
214 for ( i = 0; i < logo->clutsize; i++) {
215 palette[i+32] = (safe_shift((clut[0] & redmask), redshift) |
216 safe_shift((clut[1] & greenmask), greenshift) |
217 safe_shift((clut[2] & bluemask), blueshift));
222 static void fb_set_logo_directpalette(struct fb_info *info,
223 const struct linux_logo *logo,
226 int redshift, greenshift, blueshift;
229 redshift = info->var.red.offset;
230 greenshift = info->var.green.offset;
231 blueshift = info->var.blue.offset;
233 for (i = 32; i < logo->clutsize; i++)
234 palette[i] = i << redshift | i << greenshift | i << blueshift;
237 static void fb_set_logo(struct fb_info *info,
238 const struct linux_logo *logo, u8 *dst,
242 const u8 *src = logo->data;
243 u8 xor = (info->fix.visual == FB_VISUAL_MONO01) ? 0xff : 0;
246 switch (fb_get_color_depth(&info->var, &info->fix)) {
258 if (info->fix.visual == FB_VISUAL_MONO01 ||
259 info->fix.visual == FB_VISUAL_MONO10)
260 fg = ~((u8) (0xfff << info->var.green.length));
264 for (i = 0; i < logo->height; i++)
265 for (j = 0; j < logo->width; src++) {
268 if (j < logo->width) {
269 *dst++ = *src & 0x0f;
275 for (i = 0; i < logo->height; i++) {
276 for (j = 0; j < logo->width; src++) {
278 for (k = 7; k >= 0; k--) {
279 *dst++ = ((d >> k) & 1) ? fg : 0;
289 * Three (3) kinds of logo maps exist. linux_logo_clut224 (>16 colors),
290 * linux_logo_vga16 (16 colors) and linux_logo_mono (2 colors). Depending on
291 * the visual format and color depth of the framebuffer, the DAC, the
292 * pseudo_palette, and the logo data will be adjusted accordingly.
294 * Case 1 - linux_logo_clut224:
295 * Color exceeds the number of console colors (16), thus we set the hardware DAC
296 * using fb_set_cmap() appropriately. The "needs_cmapreset" flag will be set.
298 * For visuals that require color info from the pseudo_palette, we also construct
299 * one for temporary use. The "needs_directpalette" or "needs_truepalette" flags
302 * Case 2 - linux_logo_vga16:
303 * The number of colors just matches the console colors, thus there is no need
304 * to set the DAC or the pseudo_palette. However, the bitmap is packed, ie,
305 * each byte contains color information for two pixels (upper and lower nibble).
306 * To be consistent with fb_imageblit() usage, we therefore separate the two
307 * nibbles into separate bytes. The "depth" flag will be set to 4.
309 * Case 3 - linux_logo_mono:
310 * This is similar with Case 2. Each byte contains information for 8 pixels.
311 * We isolate each bit and expand each into a byte. The "depth" flag will
314 static struct logo_data {
316 int needs_directpalette;
317 int needs_truepalette;
319 const struct linux_logo *logo;
320 } fb_logo __read_mostly;
322 static void fb_rotate_logo_ud(const u8 *in, u8 *out, u32 width, u32 height)
324 u32 size = width * height, i;
328 for (i = size; i--; )
332 static void fb_rotate_logo_cw(const u8 *in, u8 *out, u32 width, u32 height)
334 int i, j, h = height - 1;
336 for (i = 0; i < height; i++)
337 for (j = 0; j < width; j++)
338 out[height * j + h - i] = *in++;
341 static void fb_rotate_logo_ccw(const u8 *in, u8 *out, u32 width, u32 height)
343 int i, j, w = width - 1;
345 for (i = 0; i < height; i++)
346 for (j = 0; j < width; j++)
347 out[height * (w - j) + i] = *in++;
350 static void fb_rotate_logo(struct fb_info *info, u8 *dst,
351 struct fb_image *image, int rotate)
355 if (rotate == FB_ROTATE_UD) {
356 fb_rotate_logo_ud(image->data, dst, image->width,
358 image->dx = info->var.xres - image->width - image->dx;
359 image->dy = info->var.yres - image->height - image->dy;
360 } else if (rotate == FB_ROTATE_CW) {
361 fb_rotate_logo_cw(image->data, dst, image->width,
364 image->width = image->height;
367 image->dy = image->dx;
368 image->dx = info->var.xres - image->width - tmp;
369 } else if (rotate == FB_ROTATE_CCW) {
370 fb_rotate_logo_ccw(image->data, dst, image->width,
373 image->width = image->height;
376 image->dx = image->dy;
377 image->dy = info->var.yres - image->height - tmp;
383 static void fb_do_show_logo(struct fb_info *info, struct fb_image *image,
384 int rotate, unsigned int num)
388 if (rotate == FB_ROTATE_UR) {
390 x < num && image->dx + image->width <= info->var.xres;
392 info->fbops->fb_imageblit(info, image);
393 image->dx += image->width + 8;
395 } else if (rotate == FB_ROTATE_UD) {
396 for (x = 0; x < num && image->dx >= 0; x++) {
397 info->fbops->fb_imageblit(info, image);
398 image->dx -= image->width + 8;
400 } else if (rotate == FB_ROTATE_CW) {
402 x < num && image->dy + image->height <= info->var.yres;
404 info->fbops->fb_imageblit(info, image);
405 image->dy += image->height + 8;
407 } else if (rotate == FB_ROTATE_CCW) {
408 for (x = 0; x < num && image->dy >= 0; x++) {
409 info->fbops->fb_imageblit(info, image);
410 image->dy -= image->height + 8;
415 static int fb_show_logo_line(struct fb_info *info, int rotate,
416 const struct linux_logo *logo, int y,
419 u32 *palette = NULL, *saved_pseudo_palette = NULL;
420 unsigned char *logo_new = NULL, *logo_rotate = NULL;
421 struct fb_image image;
423 /* Return if the frame buffer is not mapped or suspended */
424 if (logo == NULL || info->state != FBINFO_STATE_RUNNING ||
425 info->flags & FBINFO_MODULE)
429 image.data = logo->data;
431 if (fb_logo.needs_cmapreset)
432 fb_set_logocmap(info, logo);
434 if (fb_logo.needs_truepalette ||
435 fb_logo.needs_directpalette) {
436 palette = kmalloc(256 * 4, GFP_KERNEL);
440 if (fb_logo.needs_truepalette)
441 fb_set_logo_truepalette(info, logo, palette);
443 fb_set_logo_directpalette(info, logo, palette);
445 saved_pseudo_palette = info->pseudo_palette;
446 info->pseudo_palette = palette;
449 if (fb_logo.depth <= 4) {
450 logo_new = kmalloc(logo->width * logo->height, GFP_KERNEL);
451 if (logo_new == NULL) {
453 if (saved_pseudo_palette)
454 info->pseudo_palette = saved_pseudo_palette;
457 image.data = logo_new;
458 fb_set_logo(info, logo, logo_new, fb_logo.depth);
463 image.width = logo->width;
464 image.height = logo->height;
467 logo_rotate = kmalloc(logo->width *
468 logo->height, GFP_KERNEL);
470 fb_rotate_logo(info, logo_rotate, &image, rotate);
473 fb_do_show_logo(info, &image, rotate, n);
476 if (saved_pseudo_palette != NULL)
477 info->pseudo_palette = saved_pseudo_palette;
484 #ifdef CONFIG_FB_LOGO_EXTRA
486 #define FB_LOGO_EX_NUM_MAX 10
487 static struct logo_data_extra {
488 const struct linux_logo *logo;
490 } fb_logo_ex[FB_LOGO_EX_NUM_MAX];
491 static unsigned int fb_logo_ex_num;
493 void fb_append_extra_logo(const struct linux_logo *logo, unsigned int n)
495 if (!n || fb_logo_ex_num == FB_LOGO_EX_NUM_MAX)
498 fb_logo_ex[fb_logo_ex_num].logo = logo;
499 fb_logo_ex[fb_logo_ex_num].n = n;
503 static int fb_prepare_extra_logos(struct fb_info *info, unsigned int height,
508 /* FIXME: logo_ex supports only truecolor fb. */
509 if (info->fix.visual != FB_VISUAL_TRUECOLOR)
512 for (i = 0; i < fb_logo_ex_num; i++) {
513 height += fb_logo_ex[i].logo->height;
515 height -= fb_logo_ex[i].logo->height;
523 static int fb_show_extra_logos(struct fb_info *info, int y, int rotate)
527 for (i = 0; i < fb_logo_ex_num; i++)
528 y += fb_show_logo_line(info, rotate,
529 fb_logo_ex[i].logo, y, fb_logo_ex[i].n);
534 #else /* !CONFIG_FB_LOGO_EXTRA */
536 static inline int fb_prepare_extra_logos(struct fb_info *info,
543 static inline int fb_show_extra_logos(struct fb_info *info, int y, int rotate)
548 #endif /* CONFIG_FB_LOGO_EXTRA */
551 int fb_prepare_logo(struct fb_info *info, int rotate)
553 int depth = fb_get_color_depth(&info->var, &info->fix);
556 memset(&fb_logo, 0, sizeof(struct logo_data));
558 if (info->flags & FBINFO_MISC_TILEBLITTING ||
559 info->flags & FBINFO_MODULE)
562 if (info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
563 depth = info->var.blue.length;
564 if (info->var.red.length < depth)
565 depth = info->var.red.length;
566 if (info->var.green.length < depth)
567 depth = info->var.green.length;
570 if (info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR && depth > 4) {
571 /* assume console colormap */
575 /* Return if no suitable logo was found */
576 fb_logo.logo = fb_find_logo(depth);
582 if (rotate == FB_ROTATE_UR || rotate == FB_ROTATE_UD)
583 yres = info->var.yres;
585 yres = info->var.xres;
587 if (fb_logo.logo->height > yres) {
592 /* What depth we asked for might be different from what we get */
593 if (fb_logo.logo->type == LINUX_LOGO_CLUT224)
595 else if (fb_logo.logo->type == LINUX_LOGO_VGA16)
601 if (fb_logo.depth > 4 && depth > 4) {
602 switch (info->fix.visual) {
603 case FB_VISUAL_TRUECOLOR:
604 fb_logo.needs_truepalette = 1;
606 case FB_VISUAL_DIRECTCOLOR:
607 fb_logo.needs_directpalette = 1;
608 fb_logo.needs_cmapreset = 1;
610 case FB_VISUAL_PSEUDOCOLOR:
611 fb_logo.needs_cmapreset = 1;
616 return fb_prepare_extra_logos(info, fb_logo.logo->height, yres);
619 int fb_show_logo(struct fb_info *info, int rotate)
623 y = fb_show_logo_line(info, rotate, fb_logo.logo, 0,
625 y = fb_show_extra_logos(info, y, rotate);
630 int fb_prepare_logo(struct fb_info *info, int rotate) { return 0; }
631 int fb_show_logo(struct fb_info *info, int rotate) { return 0; }
632 #endif /* CONFIG_LOGO */
634 static void *fb_seq_start(struct seq_file *m, loff_t *pos)
636 return (*pos < FB_MAX) ? pos : NULL;
639 static void *fb_seq_next(struct seq_file *m, void *v, loff_t *pos)
642 return (*pos < FB_MAX) ? pos : NULL;
645 static void fb_seq_stop(struct seq_file *m, void *v)
649 static int fb_seq_show(struct seq_file *m, void *v)
651 int i = *(loff_t *)v;
652 struct fb_info *fi = registered_fb[i];
655 seq_printf(m, "%d %s\n", fi->node, fi->fix.id);
659 static const struct seq_operations proc_fb_seq_ops = {
660 .start = fb_seq_start,
666 static int proc_fb_open(struct inode *inode, struct file *file)
668 return seq_open(file, &proc_fb_seq_ops);
671 static const struct file_operations fb_proc_fops = {
672 .owner = THIS_MODULE,
673 .open = proc_fb_open,
676 .release = seq_release,
680 fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
682 unsigned long p = *ppos;
683 struct inode *inode = file->f_path.dentry->d_inode;
684 int fbidx = iminor(inode);
685 struct fb_info *info = registered_fb[fbidx];
688 int c, i, cnt = 0, err = 0;
689 unsigned long total_size;
691 if (!info || ! info->screen_base)
694 if (info->state != FBINFO_STATE_RUNNING)
697 if (info->fbops->fb_read)
698 return info->fbops->fb_read(info, buf, count, ppos);
700 total_size = info->screen_size;
703 total_size = info->fix.smem_len;
708 if (count >= total_size)
711 if (count + p > total_size)
712 count = total_size - p;
714 buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
719 src = (u32 __iomem *) (info->screen_base + p);
721 if (info->fbops->fb_sync)
722 info->fbops->fb_sync(info);
725 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
727 for (i = c >> 2; i--; )
728 *dst++ = fb_readl(src++);
730 u8 *dst8 = (u8 *) dst;
731 u8 __iomem *src8 = (u8 __iomem *) src;
733 for (i = c & 3; i--;)
734 *dst8++ = fb_readb(src8++);
736 src = (u32 __iomem *) src8;
739 if (copy_to_user(buf, buffer, c)) {
751 return (err) ? err : cnt;
755 fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
757 unsigned long p = *ppos;
758 struct inode *inode = file->f_path.dentry->d_inode;
759 int fbidx = iminor(inode);
760 struct fb_info *info = registered_fb[fbidx];
763 int c, i, cnt = 0, err = 0;
764 unsigned long total_size;
766 if (!info || !info->screen_base)
769 if (info->state != FBINFO_STATE_RUNNING)
772 if (info->fbops->fb_write)
773 return info->fbops->fb_write(info, buf, count, ppos);
775 total_size = info->screen_size;
778 total_size = info->fix.smem_len;
783 if (count > total_size) {
788 if (count + p > total_size) {
792 count = total_size - p;
795 buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
800 dst = (u32 __iomem *) (info->screen_base + p);
802 if (info->fbops->fb_sync)
803 info->fbops->fb_sync(info);
806 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
809 if (copy_from_user(src, buf, c)) {
814 for (i = c >> 2; i--; )
815 fb_writel(*src++, dst++);
818 u8 *src8 = (u8 *) src;
819 u8 __iomem *dst8 = (u8 __iomem *) dst;
821 for (i = c & 3; i--; )
822 fb_writeb(*src8++, dst8++);
824 dst = (u32 __iomem *) dst8;
835 return (cnt) ? cnt : err;
839 fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var)
841 struct fb_fix_screeninfo *fix = &info->fix;
842 unsigned int yres = info->var.yres;
845 if (var->yoffset > 0) {
846 if (var->vmode & FB_VMODE_YWRAP) {
847 if (!fix->ywrapstep || (var->yoffset % fix->ywrapstep))
851 } else if (!fix->ypanstep || (var->yoffset % fix->ypanstep))
855 if (var->xoffset > 0 && (!fix->xpanstep ||
856 (var->xoffset % fix->xpanstep)))
859 if (err || !info->fbops->fb_pan_display ||
860 var->yoffset + yres > info->var.yres_virtual ||
861 var->xoffset + info->var.xres > info->var.xres_virtual)
864 if ((err = info->fbops->fb_pan_display(var, info)))
866 info->var.xoffset = var->xoffset;
867 info->var.yoffset = var->yoffset;
868 if (var->vmode & FB_VMODE_YWRAP)
869 info->var.vmode |= FB_VMODE_YWRAP;
871 info->var.vmode &= ~FB_VMODE_YWRAP;
875 static int fb_check_caps(struct fb_info *info, struct fb_var_screeninfo *var,
878 struct fb_event event;
879 struct fb_blit_caps caps, fbcaps;
882 memset(&caps, 0, sizeof(caps));
883 memset(&fbcaps, 0, sizeof(fbcaps));
884 caps.flags = (activate & FB_ACTIVATE_ALL) ? 1 : 0;
887 fb_notifier_call_chain(FB_EVENT_GET_REQ, &event);
888 info->fbops->fb_get_caps(info, &fbcaps, var);
890 if (((fbcaps.x ^ caps.x) & caps.x) ||
891 ((fbcaps.y ^ caps.y) & caps.y) ||
892 (fbcaps.len < caps.len))
899 fb_set_var(struct fb_info *info, struct fb_var_screeninfo *var)
901 int flags = info->flags;
904 if (var->activate & FB_ACTIVATE_INV_MODE) {
905 struct fb_videomode mode1, mode2;
907 fb_var_to_videomode(&mode1, var);
908 fb_var_to_videomode(&mode2, &info->var);
909 /* make sure we don't delete the videomode of current var */
910 ret = fb_mode_is_equal(&mode1, &mode2);
913 struct fb_event event;
917 ret = fb_notifier_call_chain(FB_EVENT_MODE_DELETE, &event);
921 fb_delete_videomode(&mode1, &info->modelist);
924 ret = (ret) ? -EINVAL : 0;
928 if ((var->activate & FB_ACTIVATE_FORCE) ||
929 memcmp(&info->var, var, sizeof(struct fb_var_screeninfo))) {
930 u32 activate = var->activate;
932 if (!info->fbops->fb_check_var) {
937 ret = info->fbops->fb_check_var(var, info);
942 if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
943 struct fb_videomode mode;
945 if (info->fbops->fb_get_caps) {
946 ret = fb_check_caps(info, var, activate);
954 if (info->fbops->fb_set_par)
955 info->fbops->fb_set_par(info);
957 fb_pan_display(info, &info->var);
958 fb_set_cmap(&info->cmap, info);
959 fb_var_to_videomode(&mode, &info->var);
961 if (info->modelist.prev && info->modelist.next &&
962 !list_empty(&info->modelist))
963 ret = fb_add_videomode(&mode, &info->modelist);
965 if (!ret && (flags & FBINFO_MISC_USEREVENT)) {
966 struct fb_event event;
967 int evnt = (activate & FB_ACTIVATE_ALL) ?
968 FB_EVENT_MODE_CHANGE_ALL :
969 FB_EVENT_MODE_CHANGE;
971 info->flags &= ~FBINFO_MISC_USEREVENT;
974 fb_notifier_call_chain(evnt, &event);
984 fb_blank(struct fb_info *info, int blank)
988 if (blank > FB_BLANK_POWERDOWN)
989 blank = FB_BLANK_POWERDOWN;
991 if (info->fbops->fb_blank)
992 ret = info->fbops->fb_blank(blank, info);
995 struct fb_event event;
999 fb_notifier_call_chain(FB_EVENT_BLANK, &event);
1006 fb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
1009 int fbidx = iminor(inode);
1010 struct fb_info *info = registered_fb[fbidx];
1011 struct fb_ops *fb = info->fbops;
1012 struct fb_var_screeninfo var;
1013 struct fb_fix_screeninfo fix;
1014 struct fb_con2fbmap con2fb;
1015 struct fb_cmap_user cmap;
1016 struct fb_event event;
1017 void __user *argp = (void __user *)arg;
1023 case FBIOGET_VSCREENINFO:
1024 return copy_to_user(argp, &info->var,
1025 sizeof(var)) ? -EFAULT : 0;
1026 case FBIOPUT_VSCREENINFO:
1027 if (copy_from_user(&var, argp, sizeof(var)))
1029 acquire_console_sem();
1030 info->flags |= FBINFO_MISC_USEREVENT;
1031 i = fb_set_var(info, &var);
1032 info->flags &= ~FBINFO_MISC_USEREVENT;
1033 release_console_sem();
1035 if (copy_to_user(argp, &var, sizeof(var)))
1038 case FBIOGET_FSCREENINFO:
1039 return copy_to_user(argp, &info->fix,
1040 sizeof(fix)) ? -EFAULT : 0;
1042 if (copy_from_user(&cmap, argp, sizeof(cmap)))
1044 return (fb_set_user_cmap(&cmap, info));
1046 if (copy_from_user(&cmap, argp, sizeof(cmap)))
1048 return fb_cmap_to_user(&info->cmap, &cmap);
1049 case FBIOPAN_DISPLAY:
1050 if (copy_from_user(&var, argp, sizeof(var)))
1052 acquire_console_sem();
1053 i = fb_pan_display(info, &var);
1054 release_console_sem();
1057 if (copy_to_user(argp, &var, sizeof(var)))
1062 case FBIOGET_CON2FBMAP:
1063 if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
1065 if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
1067 con2fb.framebuffer = -1;
1069 event.data = &con2fb;
1070 fb_notifier_call_chain(FB_EVENT_GET_CONSOLE_MAP, &event);
1071 return copy_to_user(argp, &con2fb,
1072 sizeof(con2fb)) ? -EFAULT : 0;
1073 case FBIOPUT_CON2FBMAP:
1074 if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
1076 if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
1078 if (con2fb.framebuffer < 0 || con2fb.framebuffer >= FB_MAX)
1080 if (!registered_fb[con2fb.framebuffer])
1081 request_module("fb%d", con2fb.framebuffer);
1082 if (!registered_fb[con2fb.framebuffer])
1085 event.data = &con2fb;
1086 return fb_notifier_call_chain(FB_EVENT_SET_CONSOLE_MAP,
1089 acquire_console_sem();
1090 info->flags |= FBINFO_MISC_USEREVENT;
1091 i = fb_blank(info, arg);
1092 info->flags &= ~FBINFO_MISC_USEREVENT;
1093 release_console_sem();
1096 if (fb->fb_ioctl == NULL)
1098 return fb->fb_ioctl(info, cmd, arg);
1102 #ifdef CONFIG_COMPAT
1103 struct fb_fix_screeninfo32 {
1105 compat_caddr_t smem_start;
1114 compat_caddr_t mmio_start;
1124 compat_caddr_t green;
1125 compat_caddr_t blue;
1126 compat_caddr_t transp;
1129 static int fb_getput_cmap(struct inode *inode, struct file *file,
1130 unsigned int cmd, unsigned long arg)
1132 struct fb_cmap_user __user *cmap;
1133 struct fb_cmap32 __user *cmap32;
1137 cmap = compat_alloc_user_space(sizeof(*cmap));
1138 cmap32 = compat_ptr(arg);
1140 if (copy_in_user(&cmap->start, &cmap32->start, 2 * sizeof(__u32)))
1143 if (get_user(data, &cmap32->red) ||
1144 put_user(compat_ptr(data), &cmap->red) ||
1145 get_user(data, &cmap32->green) ||
1146 put_user(compat_ptr(data), &cmap->green) ||
1147 get_user(data, &cmap32->blue) ||
1148 put_user(compat_ptr(data), &cmap->blue) ||
1149 get_user(data, &cmap32->transp) ||
1150 put_user(compat_ptr(data), &cmap->transp))
1153 err = fb_ioctl(inode, file, cmd, (unsigned long) cmap);
1156 if (copy_in_user(&cmap32->start,
1164 static int do_fscreeninfo_to_user(struct fb_fix_screeninfo *fix,
1165 struct fb_fix_screeninfo32 __user *fix32)
1170 err = copy_to_user(&fix32->id, &fix->id, sizeof(fix32->id));
1172 data = (__u32) (unsigned long) fix->smem_start;
1173 err |= put_user(data, &fix32->smem_start);
1175 err |= put_user(fix->smem_len, &fix32->smem_len);
1176 err |= put_user(fix->type, &fix32->type);
1177 err |= put_user(fix->type_aux, &fix32->type_aux);
1178 err |= put_user(fix->visual, &fix32->visual);
1179 err |= put_user(fix->xpanstep, &fix32->xpanstep);
1180 err |= put_user(fix->ypanstep, &fix32->ypanstep);
1181 err |= put_user(fix->ywrapstep, &fix32->ywrapstep);
1182 err |= put_user(fix->line_length, &fix32->line_length);
1184 data = (__u32) (unsigned long) fix->mmio_start;
1185 err |= put_user(data, &fix32->mmio_start);
1187 err |= put_user(fix->mmio_len, &fix32->mmio_len);
1188 err |= put_user(fix->accel, &fix32->accel);
1189 err |= copy_to_user(fix32->reserved, fix->reserved,
1190 sizeof(fix->reserved));
1195 static int fb_get_fscreeninfo(struct inode *inode, struct file *file,
1196 unsigned int cmd, unsigned long arg)
1198 mm_segment_t old_fs;
1199 struct fb_fix_screeninfo fix;
1200 struct fb_fix_screeninfo32 __user *fix32;
1203 fix32 = compat_ptr(arg);
1207 err = fb_ioctl(inode, file, cmd, (unsigned long) &fix);
1211 err = do_fscreeninfo_to_user(&fix, fix32);
1217 fb_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1219 struct inode *inode = file->f_path.dentry->d_inode;
1220 int fbidx = iminor(inode);
1221 struct fb_info *info = registered_fb[fbidx];
1222 struct fb_ops *fb = info->fbops;
1223 long ret = -ENOIOCTLCMD;
1227 case FBIOGET_VSCREENINFO:
1228 case FBIOPUT_VSCREENINFO:
1229 case FBIOPAN_DISPLAY:
1230 case FBIOGET_CON2FBMAP:
1231 case FBIOPUT_CON2FBMAP:
1232 arg = (unsigned long) compat_ptr(arg);
1234 ret = fb_ioctl(inode, file, cmd, arg);
1237 case FBIOGET_FSCREENINFO:
1238 ret = fb_get_fscreeninfo(inode, file, cmd, arg);
1243 ret = fb_getput_cmap(inode, file, cmd, arg);
1247 if (fb->fb_compat_ioctl)
1248 ret = fb->fb_compat_ioctl(info, cmd, arg);
1257 fb_mmap(struct file *file, struct vm_area_struct * vma)
1259 int fbidx = iminor(file->f_path.dentry->d_inode);
1260 struct fb_info *info = registered_fb[fbidx];
1261 struct fb_ops *fb = info->fbops;
1263 unsigned long start;
1266 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
1268 off = vma->vm_pgoff << PAGE_SHIFT;
1274 res = fb->fb_mmap(info, vma);
1281 /* frame buffer memory */
1282 start = info->fix.smem_start;
1283 len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.smem_len);
1285 /* memory mapped io */
1287 if (info->var.accel_flags) {
1291 start = info->fix.mmio_start;
1292 len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.mmio_len);
1296 if ((vma->vm_end - vma->vm_start + off) > len)
1299 vma->vm_pgoff = off >> PAGE_SHIFT;
1300 /* This is an IO map - tell maydump to skip this VMA */
1301 vma->vm_flags |= VM_IO | VM_RESERVED;
1302 fb_pgprotect(file, vma, off);
1303 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1304 vma->vm_end - vma->vm_start, vma->vm_page_prot))
1310 fb_open(struct inode *inode, struct file *file)
1312 int fbidx = iminor(inode);
1313 struct fb_info *info;
1316 if (fbidx >= FB_MAX)
1319 if (!(info = registered_fb[fbidx]))
1320 request_module("fb%d", fbidx);
1321 if (!(info = registered_fb[fbidx])) {
1325 if (!try_module_get(info->fbops->owner)) {
1329 file->private_data = info;
1330 if (info->fbops->fb_open) {
1331 res = info->fbops->fb_open(info,1);
1333 module_put(info->fbops->owner);
1335 #ifdef CONFIG_FB_DEFERRED_IO
1337 fb_deferred_io_open(info, inode, file);
1345 fb_release(struct inode *inode, struct file *file)
1347 struct fb_info * const info = file->private_data;
1350 if (info->fbops->fb_release)
1351 info->fbops->fb_release(info,1);
1352 module_put(info->fbops->owner);
1357 static const struct file_operations fb_fops = {
1358 .owner = THIS_MODULE,
1362 #ifdef CONFIG_COMPAT
1363 .compat_ioctl = fb_compat_ioctl,
1367 .release = fb_release,
1368 #ifdef HAVE_ARCH_FB_UNMAPPED_AREA
1369 .get_unmapped_area = get_fb_unmapped_area,
1371 #ifdef CONFIG_FB_DEFERRED_IO
1372 .fsync = fb_deferred_io_fsync,
1376 struct class *fb_class;
1377 EXPORT_SYMBOL(fb_class);
1379 static int fb_check_foreignness(struct fb_info *fi)
1381 const bool foreign_endian = fi->flags & FBINFO_FOREIGN_ENDIAN;
1383 fi->flags &= ~FBINFO_FOREIGN_ENDIAN;
1386 fi->flags |= foreign_endian ? 0 : FBINFO_BE_MATH;
1388 fi->flags |= foreign_endian ? FBINFO_BE_MATH : 0;
1389 #endif /* __BIG_ENDIAN */
1391 if (fi->flags & FBINFO_BE_MATH && !fb_be_math(fi)) {
1392 pr_err("%s: enable CONFIG_FB_BIG_ENDIAN to "
1393 "support this framebuffer\n", fi->fix.id);
1395 } else if (!(fi->flags & FBINFO_BE_MATH) && fb_be_math(fi)) {
1396 pr_err("%s: enable CONFIG_FB_LITTLE_ENDIAN to "
1397 "support this framebuffer\n", fi->fix.id);
1405 * register_framebuffer - registers a frame buffer device
1406 * @fb_info: frame buffer info structure
1408 * Registers a frame buffer device @fb_info.
1410 * Returns negative errno on error, or zero for success.
1415 register_framebuffer(struct fb_info *fb_info)
1418 struct fb_event event;
1419 struct fb_videomode mode;
1421 if (num_registered_fb == FB_MAX)
1424 if (fb_check_foreignness(fb_info))
1427 num_registered_fb++;
1428 for (i = 0 ; i < FB_MAX; i++)
1429 if (!registered_fb[i])
1433 fb_info->dev = device_create(fb_class, fb_info->device,
1434 MKDEV(FB_MAJOR, i), NULL, "fb%d", i);
1435 if (IS_ERR(fb_info->dev)) {
1437 printk(KERN_WARNING "Unable to create device for framebuffer %d; errno = %ld\n", i, PTR_ERR(fb_info->dev));
1438 fb_info->dev = NULL;
1440 fb_init_device(fb_info);
1442 if (fb_info->pixmap.addr == NULL) {
1443 fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);
1444 if (fb_info->pixmap.addr) {
1445 fb_info->pixmap.size = FBPIXMAPSIZE;
1446 fb_info->pixmap.buf_align = 1;
1447 fb_info->pixmap.scan_align = 1;
1448 fb_info->pixmap.access_align = 32;
1449 fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;
1452 fb_info->pixmap.offset = 0;
1454 if (!fb_info->pixmap.blit_x)
1455 fb_info->pixmap.blit_x = ~(u32)0;
1457 if (!fb_info->pixmap.blit_y)
1458 fb_info->pixmap.blit_y = ~(u32)0;
1460 if (!fb_info->modelist.prev || !fb_info->modelist.next)
1461 INIT_LIST_HEAD(&fb_info->modelist);
1463 fb_var_to_videomode(&mode, &fb_info->var);
1464 fb_add_videomode(&mode, &fb_info->modelist);
1465 registered_fb[i] = fb_info;
1467 event.info = fb_info;
1468 fb_notifier_call_chain(FB_EVENT_FB_REGISTERED, &event);
1474 * unregister_framebuffer - releases a frame buffer device
1475 * @fb_info: frame buffer info structure
1477 * Unregisters a frame buffer device @fb_info.
1479 * Returns negative errno on error, or zero for success.
1481 * This function will also notify the framebuffer console
1482 * to release the driver.
1484 * This is meant to be called within a driver's module_exit()
1485 * function. If this is called outside module_exit(), ensure
1486 * that the driver implements fb_open() and fb_release() to
1487 * check that no processes are using the device.
1491 unregister_framebuffer(struct fb_info *fb_info)
1493 struct fb_event event;
1497 if (!registered_fb[i]) {
1502 event.info = fb_info;
1503 ret = fb_notifier_call_chain(FB_EVENT_FB_UNBIND, &event);
1510 if (fb_info->pixmap.addr &&
1511 (fb_info->pixmap.flags & FB_PIXMAP_DEFAULT))
1512 kfree(fb_info->pixmap.addr);
1513 fb_destroy_modelist(&fb_info->modelist);
1514 registered_fb[i]=NULL;
1515 num_registered_fb--;
1516 fb_cleanup_device(fb_info);
1517 device_destroy(fb_class, MKDEV(FB_MAJOR, i));
1518 event.info = fb_info;
1519 fb_notifier_call_chain(FB_EVENT_FB_UNREGISTERED, &event);
1525 * fb_set_suspend - low level driver signals suspend
1526 * @info: framebuffer affected
1527 * @state: 0 = resuming, !=0 = suspending
1529 * This is meant to be used by low level drivers to
1530 * signal suspend/resume to the core & clients.
1531 * It must be called with the console semaphore held
1533 void fb_set_suspend(struct fb_info *info, int state)
1535 struct fb_event event;
1539 fb_notifier_call_chain(FB_EVENT_SUSPEND, &event);
1540 info->state = FBINFO_STATE_SUSPENDED;
1542 info->state = FBINFO_STATE_RUNNING;
1543 fb_notifier_call_chain(FB_EVENT_RESUME, &event);
1548 * fbmem_init - init frame buffer subsystem
1550 * Initialize the frame buffer subsystem.
1552 * NOTE: This function is _only_ to be called by drivers/char/mem.c.
1559 proc_create("fb", 0, NULL, &fb_proc_fops);
1561 if (register_chrdev(FB_MAJOR,"fb",&fb_fops))
1562 printk("unable to get major %d for fb devs\n", FB_MAJOR);
1564 fb_class = class_create(THIS_MODULE, "graphics");
1565 if (IS_ERR(fb_class)) {
1566 printk(KERN_WARNING "Unable to create fb class; errno = %ld\n", PTR_ERR(fb_class));
1573 module_init(fbmem_init);
1577 remove_proc_entry("fb", NULL);
1578 class_destroy(fb_class);
1579 unregister_chrdev(FB_MAJOR, "fb");
1582 module_exit(fbmem_exit);
1583 MODULE_LICENSE("GPL");
1584 MODULE_DESCRIPTION("Framebuffer base");
1586 subsys_initcall(fbmem_init);
1589 int fb_new_modelist(struct fb_info *info)
1591 struct fb_event event;
1592 struct fb_var_screeninfo var = info->var;
1593 struct list_head *pos, *n;
1594 struct fb_modelist *modelist;
1595 struct fb_videomode *m, mode;
1598 list_for_each_safe(pos, n, &info->modelist) {
1599 modelist = list_entry(pos, struct fb_modelist, list);
1600 m = &modelist->mode;
1601 fb_videomode_to_var(&var, m);
1602 var.activate = FB_ACTIVATE_TEST;
1603 err = fb_set_var(info, &var);
1604 fb_var_to_videomode(&mode, &var);
1605 if (err || !fb_mode_is_equal(m, &mode)) {
1613 if (!list_empty(&info->modelist)) {
1615 err = fb_notifier_call_chain(FB_EVENT_NEW_MODELIST, &event);
1621 static char *video_options[FB_MAX] __read_mostly;
1622 static int ofonly __read_mostly;
1625 * fb_get_options - get kernel boot parameters
1626 * @name: framebuffer name as it would appear in
1627 * the boot parameter line
1628 * (video=<name>:<options>)
1629 * @option: the option will be stored here
1631 * NOTE: Needed to maintain backwards compatibility
1633 int fb_get_options(char *name, char **option)
1635 char *opt, *options = NULL;
1636 int opt_len, retval = 0;
1637 int name_len = strlen(name), i;
1639 if (name_len && ofonly && strncmp(name, "offb", 4))
1642 if (name_len && !retval) {
1643 for (i = 0; i < FB_MAX; i++) {
1644 if (video_options[i] == NULL)
1646 opt_len = strlen(video_options[i]);
1649 opt = video_options[i];
1650 if (!strncmp(name, opt, name_len) &&
1651 opt[name_len] == ':')
1652 options = opt + name_len + 1;
1655 if (options && !strncmp(options, "off", 3))
1666 * video_setup - process command line options
1667 * @options: string of options
1669 * Process command line options for frame buffer subsystem.
1671 * NOTE: This function is a __setup and __init function.
1672 * It only stores the options. Drivers have to call
1673 * fb_get_options() as necessary.
1678 static int __init video_setup(char *options)
1682 if (!options || !*options)
1685 if (!global && !strncmp(options, "ofonly", 6)) {
1690 if (!global && !strstr(options, "fb:")) {
1691 fb_mode_option = options;
1696 for (i = 0; i < FB_MAX; i++) {
1697 if (video_options[i] == NULL) {
1698 video_options[i] = options;
1707 __setup("video=", video_setup);
1711 * Visible symbols for modules
1714 EXPORT_SYMBOL(register_framebuffer);
1715 EXPORT_SYMBOL(unregister_framebuffer);
1716 EXPORT_SYMBOL(num_registered_fb);
1717 EXPORT_SYMBOL(registered_fb);
1718 EXPORT_SYMBOL(fb_show_logo);
1719 EXPORT_SYMBOL(fb_set_var);
1720 EXPORT_SYMBOL(fb_blank);
1721 EXPORT_SYMBOL(fb_pan_display);
1722 EXPORT_SYMBOL(fb_get_buffer_offset);
1723 EXPORT_SYMBOL(fb_set_suspend);
1724 EXPORT_SYMBOL(fb_get_options);
1726 MODULE_LICENSE("GPL");