added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / video / fbmem.c
blob1d6fb419379994c71ee8a980272e8b3d58943436
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/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>
23 #include <linux/mm.h>
24 #include <linux/mman.h>
25 #include <linux/vt.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>
35 #include <linux/fb.h>
37 #include <asm/fb.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;
50 * Helpers
53 int fb_get_color_depth(struct fb_var_screeninfo *var,
54 struct fb_fix_screeninfo *fix)
56 int depth = 0;
58 if (fix->visual == FB_VISUAL_MONO01 ||
59 fix->visual == FB_VISUAL_MONO10)
60 depth = 1;
61 else {
62 if (var->green.length == var->blue.length &&
63 var->green.length == var->red.length &&
64 var->green.offset == var->blue.offset &&
65 var->green.offset == var->red.offset)
66 depth = var->green.length;
67 else
68 depth = var->green.length + var->red.length +
69 var->blue.length;
72 return depth;
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;
89 int i, j;
91 for (i = height; i--; ) {
92 for (j = 0; j < idx; j++) {
93 tmp = dst[j];
94 tmp &= mask;
95 tmp |= *src >> shift_low;
96 dst[j] = tmp;
97 tmp = *src << shift_high;
98 dst[j+1] = tmp;
99 src++;
101 tmp = dst[idx];
102 tmp &= mask;
103 tmp |= *src >> shift_low;
104 dst[idx] = tmp;
105 if (shift_high < mod) {
106 tmp = *src << shift_high;
107 dst[idx+1] = tmp;
109 src++;
110 dst += d_pitch;
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
125 * the pixmap is done
127 if (buf->flags & FB_PIXMAP_IO) {
128 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
129 info->fbops->fb_sync(info);
130 return addr;
133 /* See if we fit in the remaining pixmap space */
134 offset = buf->offset + align;
135 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);
143 offset = 0;
145 buf->offset = offset + size;
146 addr += offset;
148 return addr;
151 #ifdef CONFIG_LOGO
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];
164 u16 palette_red[16];
165 int i, j, n;
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 */
178 if (n > 16)
179 n = 16;
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];
186 clut += 3;
188 fb_set_cmap(&palette_cmap, info);
192 static void fb_set_logo_truepalette(struct fb_info *info,
193 const struct linux_logo *logo,
194 u32 *palette)
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;
199 int i;
200 const unsigned char *clut = logo->clut;
203 * We have to create a temporary palette since console palette is only
204 * 16 colors long.
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));
218 clut += 3;
222 static void fb_set_logo_directpalette(struct fb_info *info,
223 const struct linux_logo *logo,
224 u32 *palette)
226 int redshift, greenshift, blueshift;
227 int i;
229 redshift = info->var.red.offset;
230 greenshift = info->var.green.offset;
231 blueshift = info->var.blue.offset;
233 for (i = 32; i < 32 + 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,
239 int depth)
241 int i, j, k;
242 const u8 *src = logo->data;
243 u8 xor = (info->fix.visual == FB_VISUAL_MONO01) ? 0xff : 0;
244 u8 fg = 1, d;
246 switch (fb_get_color_depth(&info->var, &info->fix)) {
247 case 1:
248 fg = 1;
249 break;
250 case 2:
251 fg = 3;
252 break;
253 default:
254 fg = 7;
255 break;
258 if (info->fix.visual == FB_VISUAL_MONO01 ||
259 info->fix.visual == FB_VISUAL_MONO10)
260 fg = ~((u8) (0xfff << info->var.green.length));
262 switch (depth) {
263 case 4:
264 for (i = 0; i < logo->height; i++)
265 for (j = 0; j < logo->width; src++) {
266 *dst++ = *src >> 4;
267 j++;
268 if (j < logo->width) {
269 *dst++ = *src & 0x0f;
270 j++;
273 break;
274 case 1:
275 for (i = 0; i < logo->height; i++) {
276 for (j = 0; j < logo->width; src++) {
277 d = *src ^ xor;
278 for (k = 7; k >= 0; k--) {
279 *dst++ = ((d >> k) & 1) ? fg : 0;
280 j++;
284 break;
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
300 * will be set.
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
312 * be set to 1.
314 static struct logo_data {
315 int depth;
316 int needs_directpalette;
317 int needs_truepalette;
318 int needs_cmapreset;
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;
326 out += size - 1;
328 for (i = size; i--; )
329 *out-- = *in++;
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)
353 u32 tmp;
355 if (rotate == FB_ROTATE_UD) {
356 fb_rotate_logo_ud(image->data, dst, image->width,
357 image->height);
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,
362 image->height);
363 tmp = image->width;
364 image->width = image->height;
365 image->height = tmp;
366 tmp = image->dy;
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,
371 image->height);
372 tmp = image->width;
373 image->width = image->height;
374 image->height = tmp;
375 tmp = image->dx;
376 image->dx = image->dy;
377 image->dy = info->var.yres - image->height - tmp;
380 image->data = dst;
383 static void fb_do_show_logo(struct fb_info *info, struct fb_image *image,
384 int rotate, unsigned int num)
386 unsigned int x;
388 if (rotate == FB_ROTATE_UR) {
389 for (x = 0;
390 x < num && image->dx + image->width <= info->var.xres;
391 x++) {
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) {
401 for (x = 0;
402 x < num && image->dy + image->height <= info->var.yres;
403 x++) {
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,
417 unsigned int n)
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)
426 return 0;
428 image.depth = 8;
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);
437 if (palette == NULL)
438 return 0;
440 if (fb_logo.needs_truepalette)
441 fb_set_logo_truepalette(info, logo, palette);
442 else
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) {
452 kfree(palette);
453 if (saved_pseudo_palette)
454 info->pseudo_palette = saved_pseudo_palette;
455 return 0;
457 image.data = logo_new;
458 fb_set_logo(info, logo, logo_new, fb_logo.depth);
461 image.dx = 0;
462 image.dy = y;
463 image.width = logo->width;
464 image.height = logo->height;
466 if (rotate) {
467 logo_rotate = kmalloc(logo->width *
468 logo->height, GFP_KERNEL);
469 if (logo_rotate)
470 fb_rotate_logo(info, logo_rotate, &image, rotate);
473 fb_do_show_logo(info, &image, rotate, n);
475 kfree(palette);
476 if (saved_pseudo_palette != NULL)
477 info->pseudo_palette = saved_pseudo_palette;
478 kfree(logo_new);
479 kfree(logo_rotate);
480 return logo->height;
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;
489 unsigned int n;
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)
496 return;
498 fb_logo_ex[fb_logo_ex_num].logo = logo;
499 fb_logo_ex[fb_logo_ex_num].n = n;
500 fb_logo_ex_num++;
503 static int fb_prepare_extra_logos(struct fb_info *info, unsigned int height,
504 unsigned int yres)
506 unsigned int i;
508 /* FIXME: logo_ex supports only truecolor fb. */
509 if (info->fix.visual != FB_VISUAL_TRUECOLOR)
510 fb_logo_ex_num = 0;
512 for (i = 0; i < fb_logo_ex_num; i++) {
513 if (fb_logo_ex[i].logo->type != fb_logo.logo->type) {
514 fb_logo_ex[i].logo = NULL;
515 continue;
517 height += fb_logo_ex[i].logo->height;
518 if (height > yres) {
519 height -= fb_logo_ex[i].logo->height;
520 fb_logo_ex_num = i;
521 break;
524 return height;
527 static int fb_show_extra_logos(struct fb_info *info, int y, int rotate)
529 unsigned int i;
531 for (i = 0; i < fb_logo_ex_num; i++)
532 y += fb_show_logo_line(info, rotate,
533 fb_logo_ex[i].logo, y, fb_logo_ex[i].n);
535 return y;
538 #else /* !CONFIG_FB_LOGO_EXTRA */
540 static inline int fb_prepare_extra_logos(struct fb_info *info,
541 unsigned int height,
542 unsigned int yres)
544 return height;
547 static inline int fb_show_extra_logos(struct fb_info *info, int y, int rotate)
549 return y;
552 #endif /* CONFIG_FB_LOGO_EXTRA */
555 int fb_prepare_logo(struct fb_info *info, int rotate)
557 int depth = fb_get_color_depth(&info->var, &info->fix);
558 unsigned int yres;
560 memset(&fb_logo, 0, sizeof(struct logo_data));
562 if (info->flags & FBINFO_MISC_TILEBLITTING ||
563 info->flags & FBINFO_MODULE)
564 return 0;
566 if (info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
567 depth = info->var.blue.length;
568 if (info->var.red.length < depth)
569 depth = info->var.red.length;
570 if (info->var.green.length < depth)
571 depth = info->var.green.length;
574 if (info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR && depth > 4) {
575 /* assume console colormap */
576 depth = 4;
579 /* Return if no suitable logo was found */
580 fb_logo.logo = fb_find_logo(depth);
582 if (!fb_logo.logo) {
583 return 0;
586 if (rotate == FB_ROTATE_UR || rotate == FB_ROTATE_UD)
587 yres = info->var.yres;
588 else
589 yres = info->var.xres;
591 if (fb_logo.logo->height > yres) {
592 fb_logo.logo = NULL;
593 return 0;
596 /* What depth we asked for might be different from what we get */
597 if (fb_logo.logo->type == LINUX_LOGO_CLUT224)
598 fb_logo.depth = 8;
599 else if (fb_logo.logo->type == LINUX_LOGO_VGA16)
600 fb_logo.depth = 4;
601 else
602 fb_logo.depth = 1;
605 if (fb_logo.depth > 4 && depth > 4) {
606 switch (info->fix.visual) {
607 case FB_VISUAL_TRUECOLOR:
608 fb_logo.needs_truepalette = 1;
609 break;
610 case FB_VISUAL_DIRECTCOLOR:
611 fb_logo.needs_directpalette = 1;
612 fb_logo.needs_cmapreset = 1;
613 break;
614 case FB_VISUAL_PSEUDOCOLOR:
615 fb_logo.needs_cmapreset = 1;
616 break;
620 return fb_prepare_extra_logos(info, fb_logo.logo->height, yres);
623 int fb_show_logo(struct fb_info *info, int rotate)
625 int y;
627 y = fb_show_logo_line(info, rotate, fb_logo.logo, 0,
628 num_online_cpus());
629 y = fb_show_extra_logos(info, y, rotate);
631 return y;
633 #else
634 int fb_prepare_logo(struct fb_info *info, int rotate) { return 0; }
635 int fb_show_logo(struct fb_info *info, int rotate) { return 0; }
636 #endif /* CONFIG_LOGO */
638 static void *fb_seq_start(struct seq_file *m, loff_t *pos)
640 return (*pos < FB_MAX) ? pos : NULL;
643 static void *fb_seq_next(struct seq_file *m, void *v, loff_t *pos)
645 (*pos)++;
646 return (*pos < FB_MAX) ? pos : NULL;
649 static void fb_seq_stop(struct seq_file *m, void *v)
653 static int fb_seq_show(struct seq_file *m, void *v)
655 int i = *(loff_t *)v;
656 struct fb_info *fi = registered_fb[i];
658 if (fi)
659 seq_printf(m, "%d %s\n", fi->node, fi->fix.id);
660 return 0;
663 static const struct seq_operations proc_fb_seq_ops = {
664 .start = fb_seq_start,
665 .next = fb_seq_next,
666 .stop = fb_seq_stop,
667 .show = fb_seq_show,
670 static int proc_fb_open(struct inode *inode, struct file *file)
672 return seq_open(file, &proc_fb_seq_ops);
675 static const struct file_operations fb_proc_fops = {
676 .owner = THIS_MODULE,
677 .open = proc_fb_open,
678 .read = seq_read,
679 .llseek = seq_lseek,
680 .release = seq_release,
683 static ssize_t
684 fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
686 unsigned long p = *ppos;
687 struct inode *inode = file->f_path.dentry->d_inode;
688 int fbidx = iminor(inode);
689 struct fb_info *info = registered_fb[fbidx];
690 u32 *buffer, *dst;
691 u32 __iomem *src;
692 int c, i, cnt = 0, err = 0;
693 unsigned long total_size;
695 if (!info || ! info->screen_base)
696 return -ENODEV;
698 if (info->state != FBINFO_STATE_RUNNING)
699 return -EPERM;
701 if (info->fbops->fb_read)
702 return info->fbops->fb_read(info, buf, count, ppos);
704 total_size = info->screen_size;
706 if (total_size == 0)
707 total_size = info->fix.smem_len;
709 if (p >= total_size)
710 return 0;
712 if (count >= total_size)
713 count = total_size;
715 if (count + p > total_size)
716 count = total_size - p;
718 buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
719 GFP_KERNEL);
720 if (!buffer)
721 return -ENOMEM;
723 src = (u32 __iomem *) (info->screen_base + p);
725 if (info->fbops->fb_sync)
726 info->fbops->fb_sync(info);
728 while (count) {
729 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
730 dst = buffer;
731 for (i = c >> 2; i--; )
732 *dst++ = fb_readl(src++);
733 if (c & 3) {
734 u8 *dst8 = (u8 *) dst;
735 u8 __iomem *src8 = (u8 __iomem *) src;
737 for (i = c & 3; i--;)
738 *dst8++ = fb_readb(src8++);
740 src = (u32 __iomem *) src8;
743 if (copy_to_user(buf, buffer, c)) {
744 err = -EFAULT;
745 break;
747 *ppos += c;
748 buf += c;
749 cnt += c;
750 count -= c;
753 kfree(buffer);
755 return (err) ? err : cnt;
758 static ssize_t
759 fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
761 unsigned long p = *ppos;
762 struct inode *inode = file->f_path.dentry->d_inode;
763 int fbidx = iminor(inode);
764 struct fb_info *info = registered_fb[fbidx];
765 u32 *buffer, *src;
766 u32 __iomem *dst;
767 int c, i, cnt = 0, err = 0;
768 unsigned long total_size;
770 if (!info || !info->screen_base)
771 return -ENODEV;
773 if (info->state != FBINFO_STATE_RUNNING)
774 return -EPERM;
776 if (info->fbops->fb_write)
777 return info->fbops->fb_write(info, buf, count, ppos);
779 total_size = info->screen_size;
781 if (total_size == 0)
782 total_size = info->fix.smem_len;
784 if (p > total_size)
785 return -EFBIG;
787 if (count > total_size) {
788 err = -EFBIG;
789 count = total_size;
792 if (count + p > total_size) {
793 if (!err)
794 err = -ENOSPC;
796 count = total_size - p;
799 buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
800 GFP_KERNEL);
801 if (!buffer)
802 return -ENOMEM;
804 dst = (u32 __iomem *) (info->screen_base + p);
806 if (info->fbops->fb_sync)
807 info->fbops->fb_sync(info);
809 while (count) {
810 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
811 src = buffer;
813 if (copy_from_user(src, buf, c)) {
814 err = -EFAULT;
815 break;
818 for (i = c >> 2; i--; )
819 fb_writel(*src++, dst++);
821 if (c & 3) {
822 u8 *src8 = (u8 *) src;
823 u8 __iomem *dst8 = (u8 __iomem *) dst;
825 for (i = c & 3; i--; )
826 fb_writeb(*src8++, dst8++);
828 dst = (u32 __iomem *) dst8;
831 *ppos += c;
832 buf += c;
833 cnt += c;
834 count -= c;
837 kfree(buffer);
839 return (cnt) ? cnt : err;
843 fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var)
845 struct fb_fix_screeninfo *fix = &info->fix;
846 unsigned int yres = info->var.yres;
847 int err = 0;
849 if (var->yoffset > 0) {
850 if (var->vmode & FB_VMODE_YWRAP) {
851 if (!fix->ywrapstep || (var->yoffset % fix->ywrapstep))
852 err = -EINVAL;
853 else
854 yres = 0;
855 } else if (!fix->ypanstep || (var->yoffset % fix->ypanstep))
856 err = -EINVAL;
859 if (var->xoffset > 0 && (!fix->xpanstep ||
860 (var->xoffset % fix->xpanstep)))
861 err = -EINVAL;
863 if (err || !info->fbops->fb_pan_display ||
864 var->yoffset + yres > info->var.yres_virtual ||
865 var->xoffset + info->var.xres > info->var.xres_virtual)
866 return -EINVAL;
868 if ((err = info->fbops->fb_pan_display(var, info)))
869 return err;
870 info->var.xoffset = var->xoffset;
871 info->var.yoffset = var->yoffset;
872 if (var->vmode & FB_VMODE_YWRAP)
873 info->var.vmode |= FB_VMODE_YWRAP;
874 else
875 info->var.vmode &= ~FB_VMODE_YWRAP;
876 return 0;
879 static int fb_check_caps(struct fb_info *info, struct fb_var_screeninfo *var,
880 u32 activate)
882 struct fb_event event;
883 struct fb_blit_caps caps, fbcaps;
884 int err = 0;
886 memset(&caps, 0, sizeof(caps));
887 memset(&fbcaps, 0, sizeof(fbcaps));
888 caps.flags = (activate & FB_ACTIVATE_ALL) ? 1 : 0;
889 event.info = info;
890 event.data = &caps;
891 fb_notifier_call_chain(FB_EVENT_GET_REQ, &event);
892 info->fbops->fb_get_caps(info, &fbcaps, var);
894 if (((fbcaps.x ^ caps.x) & caps.x) ||
895 ((fbcaps.y ^ caps.y) & caps.y) ||
896 (fbcaps.len < caps.len))
897 err = -EINVAL;
899 return err;
903 fb_set_var(struct fb_info *info, struct fb_var_screeninfo *var)
905 int flags = info->flags;
906 int ret = 0;
908 if (var->activate & FB_ACTIVATE_INV_MODE) {
909 struct fb_videomode mode1, mode2;
911 fb_var_to_videomode(&mode1, var);
912 fb_var_to_videomode(&mode2, &info->var);
913 /* make sure we don't delete the videomode of current var */
914 ret = fb_mode_is_equal(&mode1, &mode2);
916 if (!ret) {
917 struct fb_event event;
919 event.info = info;
920 event.data = &mode1;
921 ret = fb_notifier_call_chain(FB_EVENT_MODE_DELETE, &event);
924 if (!ret)
925 fb_delete_videomode(&mode1, &info->modelist);
928 ret = (ret) ? -EINVAL : 0;
929 goto done;
932 if ((var->activate & FB_ACTIVATE_FORCE) ||
933 memcmp(&info->var, var, sizeof(struct fb_var_screeninfo))) {
934 u32 activate = var->activate;
936 if (!info->fbops->fb_check_var) {
937 *var = info->var;
938 goto done;
941 ret = info->fbops->fb_check_var(var, info);
943 if (ret)
944 goto done;
946 if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
947 struct fb_videomode mode;
949 if (info->fbops->fb_get_caps) {
950 ret = fb_check_caps(info, var, activate);
952 if (ret)
953 goto done;
956 info->var = *var;
958 if (info->fbops->fb_set_par)
959 info->fbops->fb_set_par(info);
961 fb_pan_display(info, &info->var);
962 fb_set_cmap(&info->cmap, info);
963 fb_var_to_videomode(&mode, &info->var);
965 if (info->modelist.prev && info->modelist.next &&
966 !list_empty(&info->modelist))
967 ret = fb_add_videomode(&mode, &info->modelist);
969 if (!ret && (flags & FBINFO_MISC_USEREVENT)) {
970 struct fb_event event;
971 int evnt = (activate & FB_ACTIVATE_ALL) ?
972 FB_EVENT_MODE_CHANGE_ALL :
973 FB_EVENT_MODE_CHANGE;
975 info->flags &= ~FBINFO_MISC_USEREVENT;
976 event.info = info;
977 event.data = &mode;
978 fb_notifier_call_chain(evnt, &event);
983 done:
984 return ret;
988 fb_blank(struct fb_info *info, int blank)
990 int ret = -EINVAL;
992 if (blank > FB_BLANK_POWERDOWN)
993 blank = FB_BLANK_POWERDOWN;
995 if (info->fbops->fb_blank)
996 ret = info->fbops->fb_blank(blank, info);
998 if (!ret) {
999 struct fb_event event;
1001 event.info = info;
1002 event.data = &blank;
1003 fb_notifier_call_chain(FB_EVENT_BLANK, &event);
1006 return ret;
1009 static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
1010 unsigned long arg)
1012 struct fb_ops *fb;
1013 struct fb_var_screeninfo var;
1014 struct fb_fix_screeninfo fix;
1015 struct fb_con2fbmap con2fb;
1016 struct fb_cmap cmap_from;
1017 struct fb_cmap_user cmap;
1018 struct fb_event event;
1019 void __user *argp = (void __user *)arg;
1020 long ret = 0;
1022 switch (cmd) {
1023 case FBIOGET_VSCREENINFO:
1024 if (!lock_fb_info(info))
1025 return -ENODEV;
1026 var = info->var;
1027 unlock_fb_info(info);
1029 ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0;
1030 break;
1031 case FBIOPUT_VSCREENINFO:
1032 if (copy_from_user(&var, argp, sizeof(var)))
1033 return -EFAULT;
1034 if (!lock_fb_info(info))
1035 return -ENODEV;
1036 acquire_console_sem();
1037 info->flags |= FBINFO_MISC_USEREVENT;
1038 ret = fb_set_var(info, &var);
1039 info->flags &= ~FBINFO_MISC_USEREVENT;
1040 release_console_sem();
1041 unlock_fb_info(info);
1042 if (!ret && copy_to_user(argp, &var, sizeof(var)))
1043 ret = -EFAULT;
1044 break;
1045 case FBIOGET_FSCREENINFO:
1046 if (!lock_fb_info(info))
1047 return -ENODEV;
1048 fix = info->fix;
1049 unlock_fb_info(info);
1051 ret = copy_to_user(argp, &fix, sizeof(fix)) ? -EFAULT : 0;
1052 break;
1053 case FBIOPUTCMAP:
1054 if (copy_from_user(&cmap, argp, sizeof(cmap)))
1055 return -EFAULT;
1056 ret = fb_set_user_cmap(&cmap, info);
1057 break;
1058 case FBIOGETCMAP:
1059 if (copy_from_user(&cmap, argp, sizeof(cmap)))
1060 return -EFAULT;
1061 if (!lock_fb_info(info))
1062 return -ENODEV;
1063 cmap_from = info->cmap;
1064 unlock_fb_info(info);
1065 ret = fb_cmap_to_user(&cmap_from, &cmap);
1066 break;
1067 case FBIOPAN_DISPLAY:
1068 if (copy_from_user(&var, argp, sizeof(var)))
1069 return -EFAULT;
1070 if (!lock_fb_info(info))
1071 return -ENODEV;
1072 acquire_console_sem();
1073 ret = fb_pan_display(info, &var);
1074 release_console_sem();
1075 unlock_fb_info(info);
1076 if (ret == 0 && copy_to_user(argp, &var, sizeof(var)))
1077 return -EFAULT;
1078 break;
1079 case FBIO_CURSOR:
1080 ret = -EINVAL;
1081 break;
1082 case FBIOGET_CON2FBMAP:
1083 if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
1084 return -EFAULT;
1085 if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
1086 return -EINVAL;
1087 con2fb.framebuffer = -1;
1088 event.data = &con2fb;
1089 if (!lock_fb_info(info))
1090 return -ENODEV;
1091 event.info = info;
1092 fb_notifier_call_chain(FB_EVENT_GET_CONSOLE_MAP, &event);
1093 unlock_fb_info(info);
1094 ret = copy_to_user(argp, &con2fb, sizeof(con2fb)) ? -EFAULT : 0;
1095 break;
1096 case FBIOPUT_CON2FBMAP:
1097 if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
1098 return -EFAULT;
1099 if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
1100 return -EINVAL;
1101 if (con2fb.framebuffer < 0 || con2fb.framebuffer >= FB_MAX)
1102 return -EINVAL;
1103 if (!registered_fb[con2fb.framebuffer])
1104 request_module("fb%d", con2fb.framebuffer);
1105 if (!registered_fb[con2fb.framebuffer]) {
1106 ret = -EINVAL;
1107 break;
1109 event.data = &con2fb;
1110 if (!lock_fb_info(info))
1111 return -ENODEV;
1112 event.info = info;
1113 ret = fb_notifier_call_chain(FB_EVENT_SET_CONSOLE_MAP, &event);
1114 unlock_fb_info(info);
1115 break;
1116 case FBIOBLANK:
1117 if (!lock_fb_info(info))
1118 return -ENODEV;
1119 acquire_console_sem();
1120 info->flags |= FBINFO_MISC_USEREVENT;
1121 ret = fb_blank(info, arg);
1122 info->flags &= ~FBINFO_MISC_USEREVENT;
1123 release_console_sem();
1124 unlock_fb_info(info);
1125 break;
1126 default:
1127 if (!lock_fb_info(info))
1128 return -ENODEV;
1129 fb = info->fbops;
1130 if (fb->fb_ioctl)
1131 ret = fb->fb_ioctl(info, cmd, arg);
1132 else
1133 ret = -ENOTTY;
1134 unlock_fb_info(info);
1136 return ret;
1139 static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1141 struct inode *inode = file->f_path.dentry->d_inode;
1142 int fbidx = iminor(inode);
1143 struct fb_info *info = registered_fb[fbidx];
1145 return do_fb_ioctl(info, cmd, arg);
1148 #ifdef CONFIG_COMPAT
1149 struct fb_fix_screeninfo32 {
1150 char id[16];
1151 compat_caddr_t smem_start;
1152 u32 smem_len;
1153 u32 type;
1154 u32 type_aux;
1155 u32 visual;
1156 u16 xpanstep;
1157 u16 ypanstep;
1158 u16 ywrapstep;
1159 u32 line_length;
1160 compat_caddr_t mmio_start;
1161 u32 mmio_len;
1162 u32 accel;
1163 u16 reserved[3];
1166 struct fb_cmap32 {
1167 u32 start;
1168 u32 len;
1169 compat_caddr_t red;
1170 compat_caddr_t green;
1171 compat_caddr_t blue;
1172 compat_caddr_t transp;
1175 static int fb_getput_cmap(struct fb_info *info, unsigned int cmd,
1176 unsigned long arg)
1178 struct fb_cmap_user __user *cmap;
1179 struct fb_cmap32 __user *cmap32;
1180 __u32 data;
1181 int err;
1183 cmap = compat_alloc_user_space(sizeof(*cmap));
1184 cmap32 = compat_ptr(arg);
1186 if (copy_in_user(&cmap->start, &cmap32->start, 2 * sizeof(__u32)))
1187 return -EFAULT;
1189 if (get_user(data, &cmap32->red) ||
1190 put_user(compat_ptr(data), &cmap->red) ||
1191 get_user(data, &cmap32->green) ||
1192 put_user(compat_ptr(data), &cmap->green) ||
1193 get_user(data, &cmap32->blue) ||
1194 put_user(compat_ptr(data), &cmap->blue) ||
1195 get_user(data, &cmap32->transp) ||
1196 put_user(compat_ptr(data), &cmap->transp))
1197 return -EFAULT;
1199 err = do_fb_ioctl(info, cmd, (unsigned long) cmap);
1201 if (!err) {
1202 if (copy_in_user(&cmap32->start,
1203 &cmap->start,
1204 2 * sizeof(__u32)))
1205 err = -EFAULT;
1207 return err;
1210 static int do_fscreeninfo_to_user(struct fb_fix_screeninfo *fix,
1211 struct fb_fix_screeninfo32 __user *fix32)
1213 __u32 data;
1214 int err;
1216 err = copy_to_user(&fix32->id, &fix->id, sizeof(fix32->id));
1218 data = (__u32) (unsigned long) fix->smem_start;
1219 err |= put_user(data, &fix32->smem_start);
1221 err |= put_user(fix->smem_len, &fix32->smem_len);
1222 err |= put_user(fix->type, &fix32->type);
1223 err |= put_user(fix->type_aux, &fix32->type_aux);
1224 err |= put_user(fix->visual, &fix32->visual);
1225 err |= put_user(fix->xpanstep, &fix32->xpanstep);
1226 err |= put_user(fix->ypanstep, &fix32->ypanstep);
1227 err |= put_user(fix->ywrapstep, &fix32->ywrapstep);
1228 err |= put_user(fix->line_length, &fix32->line_length);
1230 data = (__u32) (unsigned long) fix->mmio_start;
1231 err |= put_user(data, &fix32->mmio_start);
1233 err |= put_user(fix->mmio_len, &fix32->mmio_len);
1234 err |= put_user(fix->accel, &fix32->accel);
1235 err |= copy_to_user(fix32->reserved, fix->reserved,
1236 sizeof(fix->reserved));
1238 return err;
1241 static int fb_get_fscreeninfo(struct fb_info *info, unsigned int cmd,
1242 unsigned long arg)
1244 mm_segment_t old_fs;
1245 struct fb_fix_screeninfo fix;
1246 struct fb_fix_screeninfo32 __user *fix32;
1247 int err;
1249 fix32 = compat_ptr(arg);
1251 old_fs = get_fs();
1252 set_fs(KERNEL_DS);
1253 err = do_fb_ioctl(info, cmd, (unsigned long) &fix);
1254 set_fs(old_fs);
1256 if (!err)
1257 err = do_fscreeninfo_to_user(&fix, fix32);
1259 return err;
1262 static long fb_compat_ioctl(struct file *file, unsigned int cmd,
1263 unsigned long arg)
1265 struct inode *inode = file->f_path.dentry->d_inode;
1266 int fbidx = iminor(inode);
1267 struct fb_info *info = registered_fb[fbidx];
1268 struct fb_ops *fb = info->fbops;
1269 long ret = -ENOIOCTLCMD;
1271 switch(cmd) {
1272 case FBIOGET_VSCREENINFO:
1273 case FBIOPUT_VSCREENINFO:
1274 case FBIOPAN_DISPLAY:
1275 case FBIOGET_CON2FBMAP:
1276 case FBIOPUT_CON2FBMAP:
1277 arg = (unsigned long) compat_ptr(arg);
1278 case FBIOBLANK:
1279 ret = do_fb_ioctl(info, cmd, arg);
1280 break;
1282 case FBIOGET_FSCREENINFO:
1283 ret = fb_get_fscreeninfo(info, cmd, arg);
1284 break;
1286 case FBIOGETCMAP:
1287 case FBIOPUTCMAP:
1288 ret = fb_getput_cmap(info, cmd, arg);
1289 break;
1291 default:
1292 if (fb->fb_compat_ioctl)
1293 ret = fb->fb_compat_ioctl(info, cmd, arg);
1294 break;
1296 return ret;
1298 #endif
1300 static int
1301 fb_mmap(struct file *file, struct vm_area_struct * vma)
1302 __acquires(&info->lock)
1303 __releases(&info->lock)
1305 int fbidx = iminor(file->f_path.dentry->d_inode);
1306 struct fb_info *info = registered_fb[fbidx];
1307 struct fb_ops *fb = info->fbops;
1308 unsigned long off;
1309 unsigned long start;
1310 u32 len;
1312 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
1313 return -EINVAL;
1314 off = vma->vm_pgoff << PAGE_SHIFT;
1315 if (!fb)
1316 return -ENODEV;
1317 if (fb->fb_mmap) {
1318 int res;
1319 mutex_lock(&info->lock);
1320 res = fb->fb_mmap(info, vma);
1321 mutex_unlock(&info->lock);
1322 return res;
1325 mutex_lock(&info->lock);
1327 /* frame buffer memory */
1328 start = info->fix.smem_start;
1329 len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.smem_len);
1330 if (off >= len) {
1331 /* memory mapped io */
1332 off -= len;
1333 if (info->var.accel_flags) {
1334 mutex_unlock(&info->lock);
1335 return -EINVAL;
1337 start = info->fix.mmio_start;
1338 len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.mmio_len);
1340 mutex_unlock(&info->lock);
1341 start &= PAGE_MASK;
1342 if ((vma->vm_end - vma->vm_start + off) > len)
1343 return -EINVAL;
1344 off += start;
1345 vma->vm_pgoff = off >> PAGE_SHIFT;
1346 /* This is an IO map - tell maydump to skip this VMA */
1347 vma->vm_flags |= VM_IO | VM_RESERVED;
1348 fb_pgprotect(file, vma, off);
1349 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1350 vma->vm_end - vma->vm_start, vma->vm_page_prot))
1351 return -EAGAIN;
1352 return 0;
1355 static int
1356 fb_open(struct inode *inode, struct file *file)
1357 __acquires(&info->lock)
1358 __releases(&info->lock)
1360 int fbidx = iminor(inode);
1361 struct fb_info *info;
1362 int res = 0;
1364 if (fbidx >= FB_MAX)
1365 return -ENODEV;
1366 info = registered_fb[fbidx];
1367 if (!info)
1368 request_module("fb%d", fbidx);
1369 info = registered_fb[fbidx];
1370 if (!info)
1371 return -ENODEV;
1372 mutex_lock(&info->lock);
1373 if (!try_module_get(info->fbops->owner)) {
1374 res = -ENODEV;
1375 goto out;
1377 file->private_data = info;
1378 if (info->fbops->fb_open) {
1379 res = info->fbops->fb_open(info,1);
1380 if (res)
1381 module_put(info->fbops->owner);
1383 #ifdef CONFIG_FB_DEFERRED_IO
1384 if (info->fbdefio)
1385 fb_deferred_io_open(info, inode, file);
1386 #endif
1387 out:
1388 mutex_unlock(&info->lock);
1389 return res;
1392 static int
1393 fb_release(struct inode *inode, struct file *file)
1394 __acquires(&info->lock)
1395 __releases(&info->lock)
1397 struct fb_info * const info = file->private_data;
1399 mutex_lock(&info->lock);
1400 if (info->fbops->fb_release)
1401 info->fbops->fb_release(info,1);
1402 module_put(info->fbops->owner);
1403 mutex_unlock(&info->lock);
1404 return 0;
1407 static const struct file_operations fb_fops = {
1408 .owner = THIS_MODULE,
1409 .read = fb_read,
1410 .write = fb_write,
1411 .unlocked_ioctl = fb_ioctl,
1412 #ifdef CONFIG_COMPAT
1413 .compat_ioctl = fb_compat_ioctl,
1414 #endif
1415 .mmap = fb_mmap,
1416 .open = fb_open,
1417 .release = fb_release,
1418 #ifdef HAVE_ARCH_FB_UNMAPPED_AREA
1419 .get_unmapped_area = get_fb_unmapped_area,
1420 #endif
1421 #ifdef CONFIG_FB_DEFERRED_IO
1422 .fsync = fb_deferred_io_fsync,
1423 #endif
1426 struct class *fb_class;
1427 EXPORT_SYMBOL(fb_class);
1429 static int fb_check_foreignness(struct fb_info *fi)
1431 const bool foreign_endian = fi->flags & FBINFO_FOREIGN_ENDIAN;
1433 fi->flags &= ~FBINFO_FOREIGN_ENDIAN;
1435 #ifdef __BIG_ENDIAN
1436 fi->flags |= foreign_endian ? 0 : FBINFO_BE_MATH;
1437 #else
1438 fi->flags |= foreign_endian ? FBINFO_BE_MATH : 0;
1439 #endif /* __BIG_ENDIAN */
1441 if (fi->flags & FBINFO_BE_MATH && !fb_be_math(fi)) {
1442 pr_err("%s: enable CONFIG_FB_BIG_ENDIAN to "
1443 "support this framebuffer\n", fi->fix.id);
1444 return -ENOSYS;
1445 } else if (!(fi->flags & FBINFO_BE_MATH) && fb_be_math(fi)) {
1446 pr_err("%s: enable CONFIG_FB_LITTLE_ENDIAN to "
1447 "support this framebuffer\n", fi->fix.id);
1448 return -ENOSYS;
1451 return 0;
1455 * register_framebuffer - registers a frame buffer device
1456 * @fb_info: frame buffer info structure
1458 * Registers a frame buffer device @fb_info.
1460 * Returns negative errno on error, or zero for success.
1465 register_framebuffer(struct fb_info *fb_info)
1467 int i;
1468 struct fb_event event;
1469 struct fb_videomode mode;
1471 if (num_registered_fb == FB_MAX)
1472 return -ENXIO;
1474 if (fb_check_foreignness(fb_info))
1475 return -ENOSYS;
1477 num_registered_fb++;
1478 for (i = 0 ; i < FB_MAX; i++)
1479 if (!registered_fb[i])
1480 break;
1481 fb_info->node = i;
1482 mutex_init(&fb_info->lock);
1484 fb_info->dev = device_create(fb_class, fb_info->device,
1485 MKDEV(FB_MAJOR, i), NULL, "fb%d", i);
1486 if (IS_ERR(fb_info->dev)) {
1487 /* Not fatal */
1488 printk(KERN_WARNING "Unable to create device for framebuffer %d; errno = %ld\n", i, PTR_ERR(fb_info->dev));
1489 fb_info->dev = NULL;
1490 } else
1491 fb_init_device(fb_info);
1493 if (fb_info->pixmap.addr == NULL) {
1494 fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);
1495 if (fb_info->pixmap.addr) {
1496 fb_info->pixmap.size = FBPIXMAPSIZE;
1497 fb_info->pixmap.buf_align = 1;
1498 fb_info->pixmap.scan_align = 1;
1499 fb_info->pixmap.access_align = 32;
1500 fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;
1503 fb_info->pixmap.offset = 0;
1505 if (!fb_info->pixmap.blit_x)
1506 fb_info->pixmap.blit_x = ~(u32)0;
1508 if (!fb_info->pixmap.blit_y)
1509 fb_info->pixmap.blit_y = ~(u32)0;
1511 if (!fb_info->modelist.prev || !fb_info->modelist.next)
1512 INIT_LIST_HEAD(&fb_info->modelist);
1514 fb_var_to_videomode(&mode, &fb_info->var);
1515 fb_add_videomode(&mode, &fb_info->modelist);
1516 registered_fb[i] = fb_info;
1518 event.info = fb_info;
1519 if (!lock_fb_info(fb_info))
1520 return -ENODEV;
1521 fb_notifier_call_chain(FB_EVENT_FB_REGISTERED, &event);
1522 unlock_fb_info(fb_info);
1523 return 0;
1528 * unregister_framebuffer - releases a frame buffer device
1529 * @fb_info: frame buffer info structure
1531 * Unregisters a frame buffer device @fb_info.
1533 * Returns negative errno on error, or zero for success.
1535 * This function will also notify the framebuffer console
1536 * to release the driver.
1538 * This is meant to be called within a driver's module_exit()
1539 * function. If this is called outside module_exit(), ensure
1540 * that the driver implements fb_open() and fb_release() to
1541 * check that no processes are using the device.
1545 unregister_framebuffer(struct fb_info *fb_info)
1547 struct fb_event event;
1548 int i, ret = 0;
1550 i = fb_info->node;
1551 if (!registered_fb[i]) {
1552 ret = -EINVAL;
1553 goto done;
1557 if (!lock_fb_info(fb_info))
1558 return -ENODEV;
1559 event.info = fb_info;
1560 ret = fb_notifier_call_chain(FB_EVENT_FB_UNBIND, &event);
1561 unlock_fb_info(fb_info);
1563 if (ret) {
1564 ret = -EINVAL;
1565 goto done;
1568 if (fb_info->pixmap.addr &&
1569 (fb_info->pixmap.flags & FB_PIXMAP_DEFAULT))
1570 kfree(fb_info->pixmap.addr);
1571 fb_destroy_modelist(&fb_info->modelist);
1572 registered_fb[i]=NULL;
1573 num_registered_fb--;
1574 fb_cleanup_device(fb_info);
1575 device_destroy(fb_class, MKDEV(FB_MAJOR, i));
1576 event.info = fb_info;
1577 fb_notifier_call_chain(FB_EVENT_FB_UNREGISTERED, &event);
1578 done:
1579 return ret;
1583 * fb_set_suspend - low level driver signals suspend
1584 * @info: framebuffer affected
1585 * @state: 0 = resuming, !=0 = suspending
1587 * This is meant to be used by low level drivers to
1588 * signal suspend/resume to the core & clients.
1589 * It must be called with the console semaphore held
1591 void fb_set_suspend(struct fb_info *info, int state)
1593 struct fb_event event;
1595 if (!lock_fb_info(info))
1596 return;
1597 event.info = info;
1598 if (state) {
1599 fb_notifier_call_chain(FB_EVENT_SUSPEND, &event);
1600 info->state = FBINFO_STATE_SUSPENDED;
1601 } else {
1602 info->state = FBINFO_STATE_RUNNING;
1603 fb_notifier_call_chain(FB_EVENT_RESUME, &event);
1605 unlock_fb_info(info);
1609 * fbmem_init - init frame buffer subsystem
1611 * Initialize the frame buffer subsystem.
1613 * NOTE: This function is _only_ to be called by drivers/char/mem.c.
1617 static int __init
1618 fbmem_init(void)
1620 proc_create("fb", 0, NULL, &fb_proc_fops);
1622 if (register_chrdev(FB_MAJOR,"fb",&fb_fops))
1623 printk("unable to get major %d for fb devs\n", FB_MAJOR);
1625 fb_class = class_create(THIS_MODULE, "graphics");
1626 if (IS_ERR(fb_class)) {
1627 printk(KERN_WARNING "Unable to create fb class; errno = %ld\n", PTR_ERR(fb_class));
1628 fb_class = NULL;
1630 return 0;
1633 #ifdef MODULE
1634 module_init(fbmem_init);
1635 static void __exit
1636 fbmem_exit(void)
1638 remove_proc_entry("fb", NULL);
1639 class_destroy(fb_class);
1640 unregister_chrdev(FB_MAJOR, "fb");
1643 module_exit(fbmem_exit);
1644 MODULE_LICENSE("GPL");
1645 MODULE_DESCRIPTION("Framebuffer base");
1646 #else
1647 subsys_initcall(fbmem_init);
1648 #endif
1650 int fb_new_modelist(struct fb_info *info)
1652 struct fb_event event;
1653 struct fb_var_screeninfo var = info->var;
1654 struct list_head *pos, *n;
1655 struct fb_modelist *modelist;
1656 struct fb_videomode *m, mode;
1657 int err = 1;
1659 list_for_each_safe(pos, n, &info->modelist) {
1660 modelist = list_entry(pos, struct fb_modelist, list);
1661 m = &modelist->mode;
1662 fb_videomode_to_var(&var, m);
1663 var.activate = FB_ACTIVATE_TEST;
1664 err = fb_set_var(info, &var);
1665 fb_var_to_videomode(&mode, &var);
1666 if (err || !fb_mode_is_equal(m, &mode)) {
1667 list_del(pos);
1668 kfree(pos);
1672 err = 1;
1674 if (!list_empty(&info->modelist)) {
1675 if (!lock_fb_info(info))
1676 return -ENODEV;
1677 event.info = info;
1678 err = fb_notifier_call_chain(FB_EVENT_NEW_MODELIST, &event);
1679 unlock_fb_info(info);
1682 return err;
1685 static char *video_options[FB_MAX] __read_mostly;
1686 static int ofonly __read_mostly;
1689 * fb_get_options - get kernel boot parameters
1690 * @name: framebuffer name as it would appear in
1691 * the boot parameter line
1692 * (video=<name>:<options>)
1693 * @option: the option will be stored here
1695 * NOTE: Needed to maintain backwards compatibility
1697 int fb_get_options(char *name, char **option)
1699 char *opt, *options = NULL;
1700 int opt_len, retval = 0;
1701 int name_len = strlen(name), i;
1703 if (name_len && ofonly && strncmp(name, "offb", 4))
1704 retval = 1;
1706 if (name_len && !retval) {
1707 for (i = 0; i < FB_MAX; i++) {
1708 if (video_options[i] == NULL)
1709 continue;
1710 opt_len = strlen(video_options[i]);
1711 if (!opt_len)
1712 continue;
1713 opt = video_options[i];
1714 if (!strncmp(name, opt, name_len) &&
1715 opt[name_len] == ':')
1716 options = opt + name_len + 1;
1719 if (options && !strncmp(options, "off", 3))
1720 retval = 1;
1722 if (option)
1723 *option = options;
1725 return retval;
1728 #ifndef MODULE
1730 * video_setup - process command line options
1731 * @options: string of options
1733 * Process command line options for frame buffer subsystem.
1735 * NOTE: This function is a __setup and __init function.
1736 * It only stores the options. Drivers have to call
1737 * fb_get_options() as necessary.
1739 * Returns zero.
1742 static int __init video_setup(char *options)
1744 int i, global = 0;
1746 if (!options || !*options)
1747 global = 1;
1749 if (!global && !strncmp(options, "ofonly", 6)) {
1750 ofonly = 1;
1751 global = 1;
1754 if (!global && !strstr(options, "fb:")) {
1755 fb_mode_option = options;
1756 global = 1;
1759 if (!global) {
1760 for (i = 0; i < FB_MAX; i++) {
1761 if (video_options[i] == NULL) {
1762 video_options[i] = options;
1763 break;
1769 return 1;
1771 __setup("video=", video_setup);
1772 #endif
1775 * Visible symbols for modules
1778 EXPORT_SYMBOL(register_framebuffer);
1779 EXPORT_SYMBOL(unregister_framebuffer);
1780 EXPORT_SYMBOL(num_registered_fb);
1781 EXPORT_SYMBOL(registered_fb);
1782 EXPORT_SYMBOL(fb_show_logo);
1783 EXPORT_SYMBOL(fb_set_var);
1784 EXPORT_SYMBOL(fb_blank);
1785 EXPORT_SYMBOL(fb_pan_display);
1786 EXPORT_SYMBOL(fb_get_buffer_offset);
1787 EXPORT_SYMBOL(fb_set_suspend);
1788 EXPORT_SYMBOL(fb_get_options);
1790 MODULE_LICENSE("GPL");