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