ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / 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/config.h>
15 #include <linux/module.h>
16
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/sched.h>
20 #include <linux/smp_lock.h>
21 #include <linux/kernel.h>
22 #include <linux/major.h>
23 #include <linux/slab.h>
24 #include <linux/mm.h>
25 #include <linux/mman.h>
26 #include <linux/tty.h>
27 #include <linux/init.h>
28 #include <linux/linux_logo.h>
29 #include <linux/proc_fs.h>
30 #include <linux/console.h>
31 #ifdef CONFIG_KMOD
32 #include <linux/kmod.h>
33 #endif
34 #include <linux/devfs_fs_kernel.h>
35 #include <linux/err.h>
36 #include <linux/kernel.h>
37 #include <linux/device.h>
38
39 #if defined(__mc68000__) || defined(CONFIG_APUS)
40 #include <asm/setup.h>
41 #endif
42
43 #include <asm/io.h>
44 #include <asm/uaccess.h>
45 #include <asm/page.h>
46 #include <asm/pgtable.h>
47
48 #include <linux/fb.h>
49
50 #ifdef CONFIG_FRAMEBUFFER_CONSOLE
51 #include "console/fbcon.h"
52 #endif
53     /*
54      *  Frame buffer device initialization and setup routines
55      */
56
57 extern int acornfb_init(void);
58 extern int acornfb_setup(char*);
59 extern int amifb_init(void);
60 extern int amifb_setup(char*);
61 extern int atafb_init(void);
62 extern int atafb_setup(char*);
63 extern int macfb_init(void);
64 extern int macfb_setup(char*);
65 extern int cyberfb_init(void);
66 extern int cyberfb_setup(char*);
67 extern int pm2fb_init(void);
68 extern int pm2fb_setup(char*);
69 extern int pm3fb_init(void);
70 extern int pm3fb_setup(char*);
71 extern int clps711xfb_init(void);
72 extern int cyber2000fb_init(void);
73 extern int cyber2000fb_setup(char*);
74 extern int retz3fb_init(void);
75 extern int retz3fb_setup(char*);
76 extern int clgenfb_init(void);
77 extern int clgenfb_setup(char*);
78 extern int hitfb_init(void);
79 extern int vfb_init(void);
80 extern int vfb_setup(char*);
81 extern int offb_init(void);
82 extern int atyfb_init(void);
83 extern int atyfb_setup(char*);
84 extern int aty128fb_init(void);
85 extern int aty128fb_setup(char*);
86 extern int neofb_init(void);
87 extern int neofb_setup(char*);
88 extern int igafb_init(void);
89 extern int igafb_setup(char*);
90 extern int imsttfb_init(void);
91 extern int imsttfb_setup(char*);
92 extern int dnfb_init(void);
93 extern int tgafb_init(void);
94 extern int tgafb_setup(char*);
95 extern int virgefb_init(void);
96 extern int virgefb_setup(char*);
97 extern int resolver_video_setup(char*);
98 extern int s3triofb_init(void);
99 extern int vesafb_init(void);
100 extern int vesafb_setup(char*);
101 extern int vga16fb_init(void);
102 extern int vga16fb_setup(char*);
103 extern int hgafb_init(void);
104 extern int hgafb_setup(char*);
105 extern int matroxfb_init(void);
106 extern int matroxfb_setup(char*);
107 extern int hpfb_init(void);
108 extern int platinumfb_init(void);
109 extern int platinumfb_setup(char*);
110 extern int control_init(void);
111 extern int control_setup(char*);
112 extern int valkyriefb_init(void);
113 extern int valkyriefb_setup(char*);
114 extern int chips_init(void);
115 extern int g364fb_init(void);
116 extern int sa1100fb_init(void);
117 extern int fm2fb_init(void);
118 extern int fm2fb_setup(char*);
119 extern int q40fb_init(void);
120 extern int sun3fb_init(void);
121 extern int sun3fb_setup(char *);
122 extern int sgivwfb_init(void);
123 extern int sgivwfb_setup(char*);
124 extern int rivafb_init(void);
125 extern int rivafb_setup(char*);
126 extern int tdfxfb_init(void);
127 extern int tdfxfb_setup(char*);
128 extern int tridentfb_init(void);
129 extern int tridentfb_setup(char*);
130 extern int sisfb_init(void);
131 extern int sisfb_setup(char*);
132 extern int stifb_init(void);
133 extern int stifb_setup(char*);
134 extern int pmagbafb_init(void);
135 extern int pmagbbfb_init(void);
136 extern int maxinefb_init(void);
137 extern int tx3912fb_init(void);
138 extern int tx3912fb_setup(char*);
139 extern int radeonfb_init(void);
140 extern int radeonfb_setup(char*);
141 extern int radeonfb_old_init(void);
142 extern int radeonfb_old_setup(char*);
143 extern int e1355fb_init(void);
144 extern int e1355fb_setup(char*);
145 extern int pvr2fb_init(void);
146 extern int pvr2fb_setup(char*);
147 extern int sstfb_init(void);
148 extern int sstfb_setup(char*);
149 extern int i810fb_init(void);
150 extern int i810fb_setup(char*);
151 extern int ffb_init(void);
152 extern int ffb_setup(char*);
153 extern int cg6_init(void);
154 extern int cg6_setup(char*);
155 extern int cg3_init(void);
156 extern int cg3_setup(char*);
157 extern int bw2_init(void);
158 extern int bw2_setup(char*);
159 extern int cg14_init(void);
160 extern int cg14_setup(char*);
161 extern int p9100_init(void);
162 extern int p9100_setup(char*);
163 extern int tcx_init(void);
164 extern int tcx_setup(char*);
165 extern int leo_init(void);
166 extern int leo_setup(char*);
167 extern int kyrofb_init(void);
168 extern int kyrofb_setup(char*);
169
170 static struct {
171         const char *name;
172         int (*init)(void);
173         int (*setup)(char*);
174 } fb_drivers[] __initdata = {
175
176         /*
177          * Chipset specific drivers that use resource management
178          */
179 #ifdef CONFIG_FB_RETINAZ3
180         { "retz3fb", retz3fb_init, retz3fb_setup },
181 #endif
182 #ifdef CONFIG_FB_AMIGA
183         { "amifb", amifb_init, amifb_setup },
184 #endif
185 #ifdef CONFIG_FB_CLPS711X
186         { "clps711xfb", clps711xfb_init, NULL },
187 #endif
188 #ifdef CONFIG_FB_CYBER
189         { "cyberfb", cyberfb_init, cyberfb_setup },
190 #endif
191 #ifdef CONFIG_FB_CYBER2000
192         { "cyber2000fb", cyber2000fb_init, cyber2000fb_setup },
193 #endif
194 #ifdef CONFIG_FB_PM2
195         { "pm2fb", pm2fb_init, pm2fb_setup },
196 #endif
197 #ifdef CONFIG_FB_PM3
198         { "pm3fb", pm3fb_init, pm3fb_setup },
199 #endif           
200 #ifdef CONFIG_FB_CLGEN
201         { "clgenfb", clgenfb_init, clgenfb_setup },
202 #endif
203 #ifdef CONFIG_FB_ATY
204         { "atyfb", atyfb_init, atyfb_setup },
205 #endif
206 #ifdef CONFIG_FB_MATROX
207         { "matroxfb", matroxfb_init, matroxfb_setup },
208 #endif
209 #ifdef CONFIG_FB_ATY128
210         { "aty128fb", aty128fb_init, aty128fb_setup },
211 #endif
212 #ifdef CONFIG_FB_NEOMAGIC
213         { "neofb", neofb_init, neofb_setup },
214 #endif
215 #ifdef CONFIG_FB_VIRGE
216         { "virgefb", virgefb_init, virgefb_setup },
217 #endif
218 #ifdef CONFIG_FB_RIVA
219         { "rivafb", rivafb_init, rivafb_setup },
220 #endif
221 #ifdef CONFIG_FB_3DFX
222         { "tdfxfb", tdfxfb_init, tdfxfb_setup },
223 #endif
224 #ifdef CONFIG_FB_RADEON
225         { "radeonfb", radeonfb_init, radeonfb_setup },
226 #endif
227 #ifdef CONFIG_FB_RADEON_OLD
228         { "radeonfb_old", radeonfb_old_init, radeonfb_old_setup },
229 #endif
230 #ifdef CONFIG_FB_CONTROL
231         { "controlfb", control_init, control_setup },
232 #endif
233 #ifdef CONFIG_FB_PLATINUM
234         { "platinumfb", platinumfb_init, platinumfb_setup },
235 #endif
236 #ifdef CONFIG_FB_VALKYRIE
237         { "valkyriefb", valkyriefb_init, valkyriefb_setup },
238 #endif
239 #ifdef CONFIG_FB_CT65550
240         { "chipsfb", chips_init, NULL },
241 #endif
242 #ifdef CONFIG_FB_IMSTT
243         { "imsttfb", imsttfb_init, imsttfb_setup },
244 #endif
245 #ifdef CONFIG_FB_S3TRIO
246         { "s3triofb", s3triofb_init, NULL },
247 #endif 
248 #ifdef CONFIG_FB_FM2
249         { "fm2fb", fm2fb_init, fm2fb_setup },
250 #endif 
251 #ifdef CONFIG_FB_SIS
252         { "sisfb", sisfb_init, sisfb_setup },
253 #endif
254 #ifdef CONFIG_FB_TRIDENT
255         { "tridentfb", tridentfb_init, tridentfb_setup },
256 #endif
257 #ifdef CONFIG_FB_I810
258         { "i810fb", i810fb_init, i810fb_setup },
259 #endif  
260 #ifdef CONFIG_FB_STI
261         { "stifb", stifb_init, stifb_setup },
262 #endif
263 #ifdef CONFIG_FB_FFB
264         { "ffb", ffb_init, ffb_setup },
265 #endif
266 #ifdef CONFIG_FB_CG6
267         { "cg6fb", cg6_init, cg6_setup },
268 #endif
269 #ifdef CONFIG_FB_CG3
270         { "cg3fb", cg3_init, cg3_setup },
271 #endif
272 #ifdef CONFIG_FB_BW2
273         { "bw2fb", bw2_init, bw2_setup },
274 #endif
275 #ifdef CONFIG_FB_CG14
276         { "cg14fb", cg14_init, cg14_setup },
277 #endif
278 #ifdef CONFIG_FB_P9100
279         { "p9100fb", p9100_init, p9100_setup },
280 #endif
281 #ifdef CONFIG_FB_TCX
282         { "tcxfb", tcx_init, tcx_setup },
283 #endif
284 #ifdef CONFIG_FB_LEO
285         { "leofb", leo_init, leo_setup },
286 #endif
287
288         /*
289          * Generic drivers that are used as fallbacks
290          * 
291          * These depend on resource management and must be initialized
292          * _after_ all other frame buffer devices that use resource
293          * management!
294          */
295
296 #ifdef CONFIG_FB_OF
297         { "offb", offb_init, NULL },
298 #endif
299 #ifdef CONFIG_FB_VESA
300         { "vesafb", vesafb_init, vesafb_setup },
301 #endif 
302
303         /*
304          * Chipset specific drivers that don't use resource management (yet)
305          */
306
307 #ifdef CONFIG_FB_SGIVW
308         { "sgivwfb", sgivwfb_init, sgivwfb_setup },
309 #endif
310 #ifdef CONFIG_FB_ACORN
311         { "acornfb", acornfb_init, acornfb_setup },
312 #endif
313 #ifdef CONFIG_FB_ATARI
314         { "atafb", atafb_init, atafb_setup },
315 #endif
316 #ifdef CONFIG_FB_MAC
317         { "macfb", macfb_init, macfb_setup },
318 #endif
319 #ifdef CONFIG_FB_HGA
320         { "hgafb", hgafb_init, hgafb_setup },
321 #endif 
322 #ifdef CONFIG_FB_IGA
323         { "igafb", igafb_init, igafb_setup },
324 #endif
325 #ifdef CONFIG_APOLLO
326         { "apollofb", dnfb_init, NULL },
327 #endif
328 #ifdef CONFIG_FB_Q40
329         { "q40fb", q40fb_init, NULL },
330 #endif
331 #ifdef CONFIG_FB_TGA
332         { "tgafb", tgafb_init, tgafb_setup },
333 #endif
334 #ifdef CONFIG_FB_HP300
335         { "hpfb", hpfb_init, NULL },
336 #endif 
337 #ifdef CONFIG_FB_G364
338         { "g364fb", g364fb_init, NULL },
339 #endif
340 #ifdef CONFIG_FB_SA1100
341         { "sa1100fb", sa1100fb_init, NULL },
342 #endif
343 #ifdef CONFIG_FB_SUN3
344         { "sun3fb", sun3fb_init, sun3fb_setup },
345 #endif
346 #ifdef CONFIG_FB_HIT
347         { "hitfb", hitfb_init, NULL },
348 #endif
349 #ifdef CONFIG_FB_TX3912
350         { "tx3912fb", tx3912fb_init, tx3912fb_setup },
351 #endif
352 #ifdef CONFIG_FB_E1355
353         { "e1355fb", e1355fb_init, e1355fb_setup },
354 #endif
355 #ifdef CONFIG_FB_PVR2
356         { "pvr2fb", pvr2fb_init, pvr2fb_setup },
357 #endif
358 #ifdef CONFIG_FB_PMAG_BA
359         { "pmagbafb", pmagbafb_init, NULL },
360 #endif          
361 #ifdef CONFIG_FB_PMAGB_B
362         { "pmagbbfb", pmagbbfb_init, NULL },
363 #endif
364 #ifdef CONFIG_FB_MAXINE
365         { "maxinefb", maxinefb_init, NULL },
366 #endif            
367 #ifdef CONFIG_FB_VOODOO1
368         { "sstfb", sstfb_init, sstfb_setup },
369 #endif
370 #ifdef CONFIG_FB_KYRO
371         { "kyrofb", kyrofb_init, kyrofb_setup },
372 #endif
373
374         /*
375          * Generic drivers that don't use resource management (yet)
376          */
377
378 #ifdef CONFIG_FB_VGA16
379         { "vga16fb", vga16fb_init, vga16fb_setup },
380 #endif 
381
382 #ifdef CONFIG_GSP_RESOLVER
383         /* Not a real frame buffer device... */
384         { "resolverfb", NULL, resolver_video_setup },
385 #endif
386
387 #ifdef CONFIG_FB_VIRTUAL
388         /*
389          * Vfb must be last to avoid that it becomes your primary display if
390          * other display devices are present
391          */
392         { "vfb", vfb_init, vfb_setup },
393 #endif
394 };
395
396 #define NUM_FB_DRIVERS  (sizeof(fb_drivers)/sizeof(*fb_drivers))
397 #define FBPIXMAPSIZE    16384
398
399 extern const char *global_mode_option;
400
401 static initcall_t pref_init_funcs[FB_MAX];
402 static int num_pref_init_funcs __initdata = 0;
403 static struct notifier_block *fb_notifier_list;
404 struct fb_info *registered_fb[FB_MAX];
405 int num_registered_fb;
406
407 #ifdef CONFIG_FB_OF
408 static int ofonly __initdata = 0;
409 #endif
410
411 /*
412  * Drawing helpers.
413  */
414 u8 sys_inbuf(struct fb_info *info, u8 *src)
415 {       
416         return *src;
417 }
418
419 void sys_outbuf(struct fb_info *info, u8 *dst, u8 *src, unsigned int size)
420 {
421         memcpy(dst, src, size);
422 }       
423
424 void fb_move_buf_aligned(struct fb_info *info, struct fb_pixmap *buf,
425                         u8 *dst, u32 d_pitch, u8 *src, u32 s_pitch,
426                         u32 height)
427 {
428         int i;
429
430         for (i = height; i--; ) {
431                 buf->outbuf(info, dst, src, s_pitch);
432                 src += s_pitch;
433                 dst += d_pitch;
434         }
435 }
436
437 void fb_move_buf_unaligned(struct fb_info *info, struct fb_pixmap *buf,
438                         u8 *dst, u32 d_pitch, u8 *src, u32 idx,
439                         u32 height, u32 shift_high, u32 shift_low,
440                         u32 mod)
441 {
442         u8 mask = (u8) (0xfff << shift_high), tmp;
443         int i, j;
444
445         for (i = height; i--; ) {
446                 for (j = 0; j < idx; j++) {
447                         tmp = buf->inbuf(info, dst+j);
448                         tmp &= mask;
449                         tmp |= *src >> shift_low;
450                         buf->outbuf(info, dst+j, &tmp, 1);
451                         tmp = *src << shift_high;
452                         buf->outbuf(info, dst+j+1, &tmp, 1);
453                         src++;
454                 }
455                 tmp = buf->inbuf(info, dst+idx);
456                 tmp &= mask;
457                 tmp |= *src >> shift_low;
458                 buf->outbuf(info, dst+idx, &tmp, 1);
459                 if (shift_high < mod) {
460                         tmp = *src << shift_high;
461                         buf->outbuf(info, dst+idx+1, &tmp, 1);
462                 }       
463                 src++;
464                 dst += d_pitch;
465         }
466 }
467
468 /*
469  * we need to lock this section since fb_cursor
470  * may use fb_imageblit()
471  */
472 char* fb_get_buffer_offset(struct fb_info *info, struct fb_pixmap *buf, u32 size)
473 {
474         u32 align = buf->buf_align - 1, offset;
475         char *addr = buf->addr;
476
477         /* If IO mapped, we need to sync before access, no sharing of
478          * the pixmap is done
479          */
480         if (buf->flags & FB_PIXMAP_IO) {
481                 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
482                         info->fbops->fb_sync(info);
483                 return addr;
484         }
485
486         /* See if we fit in the remaining pixmap space */
487         offset = buf->offset + align;
488         offset &= ~align;
489         if (offset + size > buf->size) {
490                 /* We do not fit. In order to be able to re-use the buffer,
491                  * we must ensure no asynchronous DMA'ing or whatever operation
492                  * is in progress, we sync for that.
493                  */
494                 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
495                         info->fbops->fb_sync(info);
496                 offset = 0;
497         }
498         buf->offset = offset + size;
499         addr += offset;
500
501         return addr;
502 }
503
504 #ifdef CONFIG_LOGO
505 #include <linux/linux_logo.h>
506
507 static inline unsigned safe_shift(unsigned d, int n)
508 {
509         return n < 0 ? d >> -n : d << n;
510 }
511
512 static void __init fb_set_logocmap(struct fb_info *info,
513                                    const struct linux_logo *logo)
514 {
515         struct fb_cmap palette_cmap;
516         u16 palette_green[16];
517         u16 palette_blue[16];
518         u16 palette_red[16];
519         int i, j, n;
520         const unsigned char *clut = logo->clut;
521
522         palette_cmap.start = 0;
523         palette_cmap.len = 16;
524         palette_cmap.red = palette_red;
525         palette_cmap.green = palette_green;
526         palette_cmap.blue = palette_blue;
527         palette_cmap.transp = NULL;
528
529         for (i = 0; i < logo->clutsize; i += n) {
530                 n = logo->clutsize - i;
531                 /* palette_cmap provides space for only 16 colors at once */
532                 if (n > 16)
533                         n = 16;
534                 palette_cmap.start = 32 + i;
535                 palette_cmap.len = n;
536                 for (j = 0; j < n; ++j) {
537                         palette_cmap.red[j] = clut[0] << 8 | clut[0];
538                         palette_cmap.green[j] = clut[1] << 8 | clut[1];
539                         palette_cmap.blue[j] = clut[2] << 8 | clut[2];
540                         clut += 3;
541                 }
542                 fb_set_cmap(&palette_cmap, 1, info);
543         }
544 }
545
546 static void  __init fb_set_logo_truepalette(struct fb_info *info,
547                                             const struct linux_logo *logo,
548                                             u32 *palette)
549 {
550         unsigned char mask[9] = { 0,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff };
551         unsigned char redmask, greenmask, bluemask;
552         int redshift, greenshift, blueshift;
553         int i;
554         const unsigned char *clut = logo->clut;
555
556         /*
557          * We have to create a temporary palette since console palette is only
558          * 16 colors long.
559          */
560         /* Bug: Doesn't obey msb_right ... (who needs that?) */
561         redmask   = mask[info->var.red.length   < 8 ? info->var.red.length   : 8];
562         greenmask = mask[info->var.green.length < 8 ? info->var.green.length : 8];
563         bluemask  = mask[info->var.blue.length  < 8 ? info->var.blue.length  : 8];
564         redshift   = info->var.red.offset   - (8 - info->var.red.length);
565         greenshift = info->var.green.offset - (8 - info->var.green.length);
566         blueshift  = info->var.blue.offset  - (8 - info->var.blue.length);
567
568         for ( i = 0; i < logo->clutsize; i++) {
569                 palette[i+32] = (safe_shift((clut[0] & redmask), redshift) |
570                                  safe_shift((clut[1] & greenmask), greenshift) |
571                                  safe_shift((clut[2] & bluemask), blueshift));
572                 clut += 3;
573         }
574 }
575
576 static void __init fb_set_logo_directpalette(struct fb_info *info,
577                                              const struct linux_logo *logo,
578                                              u32 *palette)
579 {
580         int redshift, greenshift, blueshift;
581         int i;
582
583         redshift = info->var.red.offset;
584         greenshift = info->var.green.offset;
585         blueshift = info->var.blue.offset;
586
587         for (i = 32; i < logo->clutsize; i++)
588                 palette[i] = i << redshift | i << greenshift | i << blueshift;
589 }
590
591 static void __init fb_set_logo(struct fb_info *info,
592                                const struct linux_logo *logo, u8 *dst,
593                                int depth)
594 {
595         int i, j, shift;
596         const u8 *src = logo->data;
597         u8 d, xor = 0;
598
599         switch (depth) {
600         case 4:
601                 for (i = 0; i < logo->height; i++)
602                         for (j = 0; j < logo->width; src++) {
603                                 *dst++ = *src >> 4;
604                                 j++;
605                                 if (j < logo->width) {
606                                         *dst++ = *src & 0x0f;
607                                         j++;
608                                 }
609                         }
610                 break;
611         case ~1:
612                 xor = 0xff;
613         case 1:
614                 for (i = 0; i < logo->height; i++) {
615                         shift = 7;
616                         d = *src++ ^ xor;
617                         for (j = 0; j < logo->width; j++) {
618                                 *dst++ = (d >> shift) & 1;
619                                 shift = (shift-1) & 7;
620                                 if (shift == 7)
621                                         d = *src++ ^ xor;
622                         }
623                 }
624                 break;
625         }
626 }
627
628 /*
629  * Three (3) kinds of logo maps exist.  linux_logo_clut224 (>16 colors),
630  * linux_logo_vga16 (16 colors) and linux_logo_mono (2 colors).  Depending on
631  * the visual format and color depth of the framebuffer, the DAC, the
632  * pseudo_palette, and the logo data will be adjusted accordingly.
633  *
634  * Case 1 - linux_logo_clut224:
635  * Color exceeds the number of console colors (16), thus we set the hardware DAC
636  * using fb_set_cmap() appropriately.  The "needs_cmapreset"  flag will be set.
637  *
638  * For visuals that require color info from the pseudo_palette, we also construct
639  * one for temporary use. The "needs_directpalette" or "needs_truepalette" flags
640  * will be set.
641  *
642  * Case 2 - linux_logo_vga16:
643  * The number of colors just matches the console colors, thus there is no need
644  * to set the DAC or the pseudo_palette.  However, the bitmap is packed, ie,
645  * each byte contains color information for two pixels (upper and lower nibble).
646  * To be consistent with fb_imageblit() usage, we therefore separate the two
647  * nibbles into separate bytes. The "depth" flag will be set to 4.
648  *
649  * Case 3 - linux_logo_mono:
650  * This is similar with Case 2.  Each byte contains information for 8 pixels.
651  * We isolate each bit and expand each into a byte. The "depth" flag will
652  * be set to 1.
653  */
654 static struct logo_data {
655         int depth;
656         int needs_directpalette;
657         int needs_truepalette;
658         int needs_cmapreset;
659         const struct linux_logo *logo;
660 } fb_logo;
661
662 int fb_prepare_logo(struct fb_info *info)
663 {
664         memset(&fb_logo, 0, sizeof(struct logo_data));
665
666         switch (info->fix.visual) {
667         case FB_VISUAL_TRUECOLOR:
668                 if (info->var.bits_per_pixel >= 8)
669                         fb_logo.needs_truepalette = 1;
670                 break;
671         case FB_VISUAL_DIRECTCOLOR:
672                 if (info->var.bits_per_pixel >= 24) {
673                         fb_logo.needs_directpalette = 1;
674                         fb_logo.needs_cmapreset = 1;
675                 }
676                 break;
677         case FB_VISUAL_PSEUDOCOLOR:
678                 fb_logo.needs_cmapreset = 1;
679                 break;
680         }
681
682         /* Return if no suitable logo was found */
683         fb_logo.logo = fb_find_logo(info->var.bits_per_pixel);
684         
685         if (!fb_logo.logo || fb_logo.logo->height > info->var.yres) {
686                 fb_logo.logo = NULL;
687                 return 0;
688         }
689         /* What depth we asked for might be different from what we get */
690         if (fb_logo.logo->type == LINUX_LOGO_CLUT224)
691                 fb_logo.depth = 8;
692         else if (fb_logo.logo->type == LINUX_LOGO_VGA16)
693                 fb_logo.depth = 4;
694         else
695                 fb_logo.depth = 1;              
696         return fb_logo.logo->height;
697 }
698
699 int fb_show_logo(struct fb_info *info)
700 {
701         u32 *palette = NULL, *saved_pseudo_palette = NULL;
702         unsigned char *logo_new = NULL;
703         struct fb_image image;
704         int x;
705
706         /* Return if the frame buffer is not mapped or suspended */
707         if (fb_logo.logo == NULL || info->state != FBINFO_STATE_RUNNING)
708                 return 0;
709
710         image.depth = fb_logo.depth;
711         image.data = fb_logo.logo->data;
712
713         if (fb_logo.needs_cmapreset)
714                 fb_set_logocmap(info, fb_logo.logo);
715
716         if (fb_logo.needs_truepalette || 
717             fb_logo.needs_directpalette) {
718                 palette = kmalloc(256 * 4, GFP_KERNEL);
719                 if (palette == NULL)
720                         return 0;
721
722                 if (fb_logo.needs_truepalette)
723                         fb_set_logo_truepalette(info, fb_logo.logo, palette);
724                 else
725                         fb_set_logo_directpalette(info, fb_logo.logo, palette);
726
727                 saved_pseudo_palette = info->pseudo_palette;
728                 info->pseudo_palette = palette;
729         }
730
731         if (fb_logo.depth == 4) {
732                 logo_new = kmalloc(fb_logo.logo->width * fb_logo.logo->height, 
733                                    GFP_KERNEL);
734                 if (logo_new == NULL) {
735                         if (palette)
736                                 kfree(palette);
737                         if (saved_pseudo_palette)
738                                 info->pseudo_palette = saved_pseudo_palette;
739                         return 0;
740                 }
741                 image.data = logo_new;
742                 fb_set_logo(info, fb_logo.logo, logo_new, fb_logo.depth);
743         }
744
745         image.width = fb_logo.logo->width;
746         image.height = fb_logo.logo->height;
747         image.dy = 0;
748
749         for (x = 0; x < num_online_cpus() * (fb_logo.logo->width + 8) &&
750              x <= info->var.xres-fb_logo.logo->width; x += (fb_logo.logo->width + 8)) {
751                 image.dx = x;
752                 info->fbops->fb_imageblit(info, &image);
753         }
754         
755         if (palette != NULL)
756                 kfree(palette);
757         if (saved_pseudo_palette != NULL)
758                 info->pseudo_palette = saved_pseudo_palette;
759         if (logo_new != NULL)
760                 kfree(logo_new);
761         return fb_logo.logo->height;
762 }
763 #else
764 int fb_prepare_logo(struct fb_info *info) { return 0; }
765 int fb_show_logo(struct fb_info *info) { return 0; }
766 #endif /* CONFIG_LOGO */
767
768 static int fbmem_read_proc(char *buf, char **start, off_t offset,
769                            int len, int *eof, void *private)
770 {
771         struct fb_info **fi;
772         int clen;
773
774         clen = 0;
775         for (fi = registered_fb; fi < &registered_fb[FB_MAX] && len < 4000; fi++)
776                 if (*fi)
777                         clen += sprintf(buf + clen, "%d %s\n",
778                                         (*fi)->node,
779                                         (*fi)->fix.id);
780         *start = buf + offset;
781         if (clen > offset)
782                 clen -= offset;
783         else
784                 clen = 0;
785         return clen < len ? clen : len;
786 }
787
788 static ssize_t
789 fb_read(struct file *file, char *buf, size_t count, loff_t *ppos)
790 {
791         unsigned long p = *ppos;
792         struct inode *inode = file->f_dentry->d_inode;
793         int fbidx = iminor(inode);
794         struct fb_info *info = registered_fb[fbidx];
795
796         if (!info || ! info->screen_base)
797                 return -ENODEV;
798
799         if (info->state != FBINFO_STATE_RUNNING)
800                 return -EPERM;
801
802         if (info->fbops->fb_read)
803                 return info->fbops->fb_read(file, buf, count, ppos);
804         
805         if (p >= info->fix.smem_len)
806             return 0;
807         if (count >= info->fix.smem_len)
808             count = info->fix.smem_len;
809         if (count + p > info->fix.smem_len)
810                 count = info->fix.smem_len - p;
811         if (info->fbops->fb_sync)
812                 info->fbops->fb_sync(info);
813         if (count) {
814             char *base_addr;
815
816             base_addr = info->screen_base;
817             count -= copy_to_user(buf, base_addr+p, count);
818             if (!count)
819                 return -EFAULT;
820             *ppos += count;
821         }
822         return count;
823 }
824
825 static ssize_t
826 fb_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
827 {
828         unsigned long p = *ppos;
829         struct inode *inode = file->f_dentry->d_inode;
830         int fbidx = iminor(inode);
831         struct fb_info *info = registered_fb[fbidx];
832         int err;
833
834         if (!info || !info->screen_base)
835                 return -ENODEV;
836
837         if (info->state != FBINFO_STATE_RUNNING)
838                 return -EPERM;
839
840         if (info->fbops->fb_write)
841                 return info->fbops->fb_write(file, buf, count, ppos);
842         
843         if (p > info->fix.smem_len)
844             return -ENOSPC;
845         if (count >= info->fix.smem_len)
846             count = info->fix.smem_len;
847         err = 0;
848         if (count + p > info->fix.smem_len) {
849             count = info->fix.smem_len - p;
850             err = -ENOSPC;
851         }
852         if (info->fbops->fb_sync)
853                 info->fbops->fb_sync(info);
854         if (count) {
855             char *base_addr;
856
857             base_addr = info->screen_base;
858             count -= copy_from_user(base_addr+p, buf, count);
859             *ppos += count;
860             err = -EFAULT;
861         }
862         if (count)
863                 return count;
864         return err;
865 }
866
867 #ifdef CONFIG_KMOD
868 static void try_to_load(int fb)
869 {
870         request_module("fb%d", fb);
871 }
872 #endif /* CONFIG_KMOD */
873
874 void
875 fb_load_cursor_image(struct fb_info *info)
876 {
877         unsigned int width = (info->cursor.image.width + 7) >> 3;
878         u8 *data = (u8 *) info->cursor.image.data;
879
880         info->sprite.outbuf(info, info->sprite.addr, data, width);
881 }
882
883 int
884 fb_cursor(struct fb_info *info, struct fb_cursor *sprite)
885 {
886         struct fb_cursor cursor;
887         int err;
888         
889         if (copy_from_user(&cursor, sprite, sizeof(struct fb_cursor)))
890                 return -EFAULT;
891
892         if (cursor.set & FB_CUR_SETCUR)
893                 info->cursor.enable = 1;
894         
895         if (cursor.set & FB_CUR_SETCMAP) {
896                 err = fb_copy_cmap(&cursor.image.cmap, &sprite->image.cmap, 1);
897                 if (err)
898                         return err;
899         }
900         
901         if (cursor.set & FB_CUR_SETSHAPE) {
902                 int size = ((cursor.image.width + 7) >> 3) * cursor.image.height;               
903                 if ((cursor.image.height != info->cursor.image.height) ||
904                     (cursor.image.width != info->cursor.image.width))
905                         cursor.set |= FB_CUR_SETSIZE;
906                 
907                 cursor.image.data = kmalloc(size, GFP_KERNEL);
908                 if (!cursor.image.data)
909                         return -ENOMEM;
910                 
911                 cursor.mask = kmalloc(size, GFP_KERNEL);
912                 if (!cursor.mask) {
913                         kfree(cursor.image.data);
914                         return -ENOMEM;
915                 }
916                 
917                 if (copy_from_user(cursor.image.data, sprite->image.data, size) ||
918                     copy_from_user(cursor.mask, sprite->mask, size)) { 
919                         kfree(cursor.image.data);
920                         kfree(cursor.mask);
921                         return -EFAULT;
922                 }
923         }
924         info->cursor.set = cursor.set;
925         info->cursor.rop = cursor.rop;
926         err = info->fbops->fb_cursor(info, &cursor);
927         return err;
928 }
929
930 int
931 fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var)
932 {
933         int xoffset = var->xoffset;
934         int yoffset = var->yoffset;
935         int err;
936
937         if (xoffset < 0 || yoffset < 0 || !info->fbops->fb_pan_display ||
938             xoffset + info->var.xres > info->var.xres_virtual ||
939             yoffset + info->var.yres > info->var.yres_virtual)
940                 return -EINVAL;
941         if ((err = info->fbops->fb_pan_display(var, info)))
942                 return err;
943         info->var.xoffset = var->xoffset;
944         info->var.yoffset = var->yoffset;
945         if (var->vmode & FB_VMODE_YWRAP)
946                 info->var.vmode |= FB_VMODE_YWRAP;
947         else
948                 info->var.vmode &= ~FB_VMODE_YWRAP;
949         return 0;
950 }
951
952 int
953 fb_set_var(struct fb_info *info, struct fb_var_screeninfo *var)
954 {
955         int err;
956
957         if ((var->activate & FB_ACTIVATE_FORCE) ||
958             memcmp(&info->var, var, sizeof(struct fb_var_screeninfo))) {
959                 if (!info->fbops->fb_check_var) {
960                         *var = info->var;
961                         return 0;
962                 }
963
964                 if ((err = info->fbops->fb_check_var(var, info)))
965                         return err;
966
967                 if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
968                         info->var = *var;
969
970                         if (info->fbops->fb_set_par)
971                                 info->fbops->fb_set_par(info);
972
973                         fb_pan_display(info, &info->var);
974
975                         fb_set_cmap(&info->cmap, 1, info);
976
977                         notifier_call_chain(&fb_notifier_list, FB_EVENT_MODE_CHANGE, info);
978                 }
979         }
980         return 0;
981 }
982
983 int
984 fb_blank(struct fb_info *info, int blank)
985 {       
986         /* ??? Variable sized stack allocation.  */
987         u16 black[info->cmap.len];
988         struct fb_cmap cmap;
989         
990         if (info->fbops->fb_blank && !info->fbops->fb_blank(blank, info))
991                 return 0;
992         if (blank) { 
993                 memset(black, 0, info->cmap.len * sizeof(u16));
994                 cmap.red = cmap.green = cmap.blue = black;
995                 cmap.transp = info->cmap.transp ? black : NULL;
996                 cmap.start = info->cmap.start;
997                 cmap.len = info->cmap.len;
998         } else
999                 cmap = info->cmap;
1000         return fb_set_cmap(&cmap, 1, info);
1001 }
1002
1003 static int 
1004 fb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
1005          unsigned long arg)
1006 {
1007         int fbidx = iminor(inode);
1008         struct fb_info *info = registered_fb[fbidx];
1009         struct fb_ops *fb = info->fbops;
1010         struct fb_var_screeninfo var;
1011         struct fb_fix_screeninfo fix;
1012 #ifdef CONFIG_FRAMEBUFFER_CONSOLE
1013         struct fb_con2fbmap con2fb;
1014 #endif
1015         struct fb_cmap cmap;
1016         int i, rc;
1017         
1018         if (!fb)
1019                 return -ENODEV;
1020         switch (cmd) {
1021         case FBIOGET_VSCREENINFO:
1022                 return copy_to_user((void *) arg, &info->var,
1023                                     sizeof(var)) ? -EFAULT : 0;
1024         case FBIOPUT_VSCREENINFO:
1025                 if (copy_from_user(&var, (void *) arg, sizeof(var)))
1026                         return -EFAULT;
1027                 acquire_console_sem();
1028                 i = fb_set_var(info, &var);
1029                 release_console_sem();
1030                 if (i) return i;
1031                 if (copy_to_user((void *) arg, &var, sizeof(var)))
1032                         return -EFAULT;
1033                 return 0;
1034         case FBIOGET_FSCREENINFO:
1035                 return copy_to_user((void *) arg, &info->fix,
1036                                     sizeof(fix)) ? -EFAULT : 0;
1037         case FBIOPUTCMAP:
1038                 if (copy_from_user(&cmap, (void *) arg, sizeof(cmap)))
1039                         return -EFAULT;
1040                 return (fb_set_cmap(&cmap, 0, info));
1041         case FBIOGETCMAP:
1042                 if (copy_from_user(&cmap, (void *) arg, sizeof(cmap)))
1043                         return -EFAULT;
1044                 return (fb_copy_cmap(&info->cmap, &cmap, 2));
1045         case FBIOPAN_DISPLAY:
1046                 if (copy_from_user(&var, (void *) arg, sizeof(var)))
1047                         return -EFAULT;
1048                 acquire_console_sem();
1049                 i = fb_pan_display(info, &var);
1050                 release_console_sem();
1051                 if (i)
1052                         return i;
1053                 if (copy_to_user((void *) arg, &var, sizeof(var)))
1054                         return -EFAULT;
1055                 return 0;
1056         case FBIO_CURSOR:
1057                 acquire_console_sem();
1058                 rc = fb_cursor(info, (struct fb_cursor *) arg);
1059                 release_console_sem();
1060                 return rc;
1061 #ifdef CONFIG_FRAMEBUFFER_CONSOLE
1062         case FBIOGET_CON2FBMAP:
1063                 if (copy_from_user(&con2fb, (void *)arg, sizeof(con2fb)))
1064                         return -EFAULT;
1065                 if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
1066                     return -EINVAL;
1067                 con2fb.framebuffer = con2fb_map[con2fb.console-1];
1068                 return copy_to_user((void *)arg, &con2fb,
1069                                     sizeof(con2fb)) ? -EFAULT : 0;
1070         case FBIOPUT_CON2FBMAP:
1071                 if (copy_from_user(&con2fb, (void *)arg, sizeof(con2fb)))
1072                         return - EFAULT;
1073                 if (con2fb.console < 0 || con2fb.console > MAX_NR_CONSOLES)
1074                     return -EINVAL;
1075                 if (con2fb.framebuffer < 0 || con2fb.framebuffer >= FB_MAX)
1076                     return -EINVAL;
1077 #ifdef CONFIG_KMOD
1078                 if (!registered_fb[con2fb.framebuffer])
1079                     try_to_load(con2fb.framebuffer);
1080 #endif /* CONFIG_KMOD */
1081                 if (!registered_fb[con2fb.framebuffer])
1082                     return -EINVAL;
1083                 if (con2fb.console != 0)
1084                         set_con2fb_map(con2fb.console-1, con2fb.framebuffer);
1085                 else
1086                         fb_console_init();              
1087                 return 0;
1088 #endif  /* CONFIG_FRAMEBUFFER_CONSOLE */
1089         case FBIOBLANK:
1090                 acquire_console_sem();
1091                 i = fb_blank(info, arg);
1092                 release_console_sem();
1093                 return i;
1094         default:
1095                 if (fb->fb_ioctl == NULL)
1096                         return -EINVAL;
1097                 return fb->fb_ioctl(inode, file, cmd, arg, info);
1098         }
1099 }
1100
1101 static int 
1102 fb_mmap(struct file *file, struct vm_area_struct * vma)
1103 {
1104         int fbidx = iminor(file->f_dentry->d_inode);
1105         struct fb_info *info = registered_fb[fbidx];
1106         struct fb_ops *fb = info->fbops;
1107         unsigned long off;
1108 #if !defined(__sparc__) || defined(__sparc_v9__)
1109         unsigned long start;
1110         u32 len;
1111 #endif
1112
1113         if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
1114                 return -EINVAL;
1115         off = vma->vm_pgoff << PAGE_SHIFT;
1116         if (!fb)
1117                 return -ENODEV;
1118         if (fb->fb_mmap) {
1119                 int res;
1120                 lock_kernel();
1121                 res = fb->fb_mmap(info, file, vma);
1122                 unlock_kernel();
1123                 return res;
1124         }
1125
1126 #if defined(__sparc__) && !defined(__sparc_v9__)
1127         /* Should never get here, all fb drivers should have their own
1128            mmap routines */
1129         return -EINVAL;
1130 #else
1131         /* !sparc32... */
1132         lock_kernel();
1133
1134         /* frame buffer memory */
1135         start = info->fix.smem_start;
1136         len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.smem_len);
1137         if (off >= len) {
1138                 /* memory mapped io */
1139                 off -= len;
1140                 if (info->var.accel_flags) {
1141                         unlock_kernel();
1142                         return -EINVAL;
1143                 }
1144                 start = info->fix.mmio_start;
1145                 len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.mmio_len);
1146         }
1147         unlock_kernel();
1148         start &= PAGE_MASK;
1149         if ((vma->vm_end - vma->vm_start + off) > len)
1150                 return -EINVAL;
1151         off += start;
1152         vma->vm_pgoff = off >> PAGE_SHIFT;
1153         /* This is an IO map - tell maydump to skip this VMA */
1154         vma->vm_flags |= VM_IO;
1155 #if defined(__sparc_v9__)
1156         vma->vm_flags |= (VM_SHM | VM_LOCKED);
1157         if (io_remap_page_range(vma, vma->vm_start, off,
1158                                 vma->vm_end - vma->vm_start, vma->vm_page_prot, 0))
1159                 return -EAGAIN;
1160 #else
1161 #if defined(__mc68000__)
1162 #if defined(CONFIG_SUN3)
1163         pgprot_val(vma->vm_page_prot) |= SUN3_PAGE_NOCACHE;
1164 #elif defined(CONFIG_MMU)
1165         if (CPU_IS_020_OR_030)
1166                 pgprot_val(vma->vm_page_prot) |= _PAGE_NOCACHE030;
1167         if (CPU_IS_040_OR_060) {
1168                 pgprot_val(vma->vm_page_prot) &= _CACHEMASK040;
1169                 /* Use no-cache mode, serialized */
1170                 pgprot_val(vma->vm_page_prot) |= _PAGE_NOCACHE_S;
1171         }
1172 #endif
1173 #elif defined(__powerpc__)
1174         pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE|_PAGE_GUARDED;
1175 #elif defined(__alpha__)
1176         /* Caching is off in the I/O space quadrant by design.  */
1177 #elif defined(__i386__) || defined(__x86_64__)
1178         if (boot_cpu_data.x86 > 3)
1179                 pgprot_val(vma->vm_page_prot) |= _PAGE_PCD;
1180 #elif defined(__mips__)
1181         pgprot_val(vma->vm_page_prot) &= ~_CACHE_MASK;
1182         pgprot_val(vma->vm_page_prot) |= _CACHE_UNCACHED;
1183 #elif defined(__hppa__)
1184         pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
1185 #elif defined(__ia64__) || defined(__arm__) || defined(__sh__)
1186         vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1187 #else
1188 #warning What do we have to do here??
1189 #endif
1190         if (io_remap_page_range(vma, vma->vm_start, off,
1191                              vma->vm_end - vma->vm_start, vma->vm_page_prot))
1192                 return -EAGAIN;
1193 #endif /* !__sparc_v9__ */
1194         return 0;
1195 #endif /* !sparc32 */
1196 }
1197
1198 static int
1199 fb_open(struct inode *inode, struct file *file)
1200 {
1201         int fbidx = iminor(inode);
1202         struct fb_info *info;
1203         int res = 0;
1204
1205         if (fbidx >= FB_MAX)
1206                 return -ENODEV;
1207 #ifdef CONFIG_KMOD
1208         if (!(info = registered_fb[fbidx]))
1209                 try_to_load(fbidx);
1210 #endif /* CONFIG_KMOD */
1211         if (!(info = registered_fb[fbidx]))
1212                 return -ENODEV;
1213         if (!try_module_get(info->fbops->owner))
1214                 return -ENODEV;
1215         if (info->fbops->fb_open) {
1216                 res = info->fbops->fb_open(info,1);
1217                 if (res)
1218                         module_put(info->fbops->owner);
1219         }
1220         return res;
1221 }
1222
1223 static int 
1224 fb_release(struct inode *inode, struct file *file)
1225 {
1226         int fbidx = iminor(inode);
1227         struct fb_info *info;
1228
1229         lock_kernel();
1230         info = registered_fb[fbidx];
1231         if (info->fbops->fb_release)
1232                 info->fbops->fb_release(info,1);
1233         module_put(info->fbops->owner);
1234         unlock_kernel();
1235         return 0;
1236 }
1237
1238 static struct file_operations fb_fops = {
1239         .owner =        THIS_MODULE,
1240         .read =         fb_read,
1241         .write =        fb_write,
1242         .ioctl =        fb_ioctl,
1243         .mmap =         fb_mmap,
1244         .open =         fb_open,
1245         .release =      fb_release,
1246 #ifdef HAVE_ARCH_FB_UNMAPPED_AREA
1247         .get_unmapped_area = get_fb_unmapped_area,
1248 #endif
1249 };
1250
1251 static struct class_simple *fb_class;
1252
1253 /**
1254  *      register_framebuffer - registers a frame buffer device
1255  *      @fb_info: frame buffer info structure
1256  *
1257  *      Registers a frame buffer device @fb_info.
1258  *
1259  *      Returns negative errno on error, or zero for success.
1260  *
1261  */
1262
1263 int
1264 register_framebuffer(struct fb_info *fb_info)
1265 {
1266         int i;
1267         struct class_device *c;
1268
1269         if (num_registered_fb == FB_MAX)
1270                 return -ENXIO;
1271         num_registered_fb++;
1272         for (i = 0 ; i < FB_MAX; i++)
1273                 if (!registered_fb[i])
1274                         break;
1275         fb_info->node = i;
1276
1277         c = class_simple_device_add(fb_class, MKDEV(FB_MAJOR, i), NULL, "fb%d", i);
1278         if (IS_ERR(c)) {
1279                 /* Not fatal */
1280                 printk(KERN_WARNING "Unable to create class_device for framebuffer %d; errno = %ld\n", i, PTR_ERR(c));
1281         }
1282         
1283         if (fb_info->pixmap.addr == NULL) {
1284                 fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);
1285                 if (fb_info->pixmap.addr) {
1286                         fb_info->pixmap.size = FBPIXMAPSIZE;
1287                         fb_info->pixmap.buf_align = 1;
1288                         fb_info->pixmap.scan_align = 1;
1289                         fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;
1290                 }
1291         }       
1292         fb_info->pixmap.offset = 0;
1293         if (fb_info->pixmap.outbuf == NULL)
1294                 fb_info->pixmap.outbuf = sys_outbuf;
1295         if (fb_info->pixmap.inbuf == NULL)
1296                 fb_info->pixmap.inbuf = sys_inbuf;
1297
1298         if (fb_info->sprite.addr == NULL) {
1299                 fb_info->sprite.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);
1300                 if (fb_info->sprite.addr) {
1301                         fb_info->sprite.size = FBPIXMAPSIZE;
1302                         fb_info->sprite.buf_align = 1;
1303                         fb_info->sprite.scan_align = 1;
1304                         fb_info->sprite.flags = FB_PIXMAP_DEFAULT;
1305                 }
1306         }
1307         fb_info->sprite.offset = 0;
1308         if (fb_info->sprite.outbuf == NULL)
1309                 fb_info->sprite.outbuf = sys_outbuf;
1310         if (fb_info->sprite.inbuf == NULL)
1311                 fb_info->sprite.inbuf = sys_inbuf;
1312
1313         registered_fb[i] = fb_info;
1314
1315         devfs_mk_cdev(MKDEV(FB_MAJOR, i),
1316                         S_IFCHR | S_IRUGO | S_IWUGO, "fb/%d", i);
1317         return 0;
1318 }
1319
1320
1321 /**
1322  *      unregister_framebuffer - releases a frame buffer device
1323  *      @fb_info: frame buffer info structure
1324  *
1325  *      Unregisters a frame buffer device @fb_info.
1326  *
1327  *      Returns negative errno on error, or zero for success.
1328  *
1329  */
1330
1331 int
1332 unregister_framebuffer(struct fb_info *fb_info)
1333 {
1334         int i;
1335
1336         i = fb_info->node;
1337         if (!registered_fb[i])
1338                 return -EINVAL;
1339         devfs_remove("fb/%d", i);
1340
1341         if (fb_info->pixmap.addr && (fb_info->pixmap.flags & FB_PIXMAP_DEFAULT))
1342                 kfree(fb_info->pixmap.addr);
1343         if (fb_info->sprite.addr && (fb_info->sprite.flags & FB_PIXMAP_DEFAULT))
1344                 kfree(fb_info->sprite.addr);
1345         registered_fb[i]=NULL;
1346         num_registered_fb--;
1347         class_simple_device_remove(MKDEV(FB_MAJOR, i));
1348         return 0;
1349 }
1350
1351 /**
1352  *      fb_register_client - register a client notifier
1353  *      @nb: notifier block to callback on events
1354  */
1355 int fb_register_client(struct notifier_block *nb)
1356 {
1357         return notifier_chain_register(&fb_notifier_list, nb);
1358 }
1359
1360 /**
1361  *      fb_unregister_client - unregister a client notifier
1362  *      @nb: notifier block to callback on events
1363  */
1364 int fb_unregister_client(struct notifier_block *nb)
1365 {
1366         return notifier_chain_unregister(&fb_notifier_list, nb);
1367 }
1368
1369 /**
1370  *      fb_set_suspend - low level driver signals suspend
1371  *      @info: framebuffer affected
1372  *      @state: 0 = resuming, !=0 = suspending
1373  *
1374  *      This is meant to be used by low level drivers to
1375  *      signal suspend/resume to the core & clients.
1376  *      It must be called with the console semaphore held
1377  */
1378 void fb_set_suspend(struct fb_info *info, int state)
1379 {
1380         if (state) {
1381                 notifier_call_chain(&fb_notifier_list, FB_EVENT_SUSPEND, info);
1382                 info->state = FBINFO_STATE_SUSPENDED;
1383         } else {
1384                 info->state = FBINFO_STATE_RUNNING;
1385                 notifier_call_chain(&fb_notifier_list, FB_EVENT_RESUME, info);
1386         }
1387 }
1388
1389 /**
1390  *      fbmem_init - init frame buffer subsystem
1391  *
1392  *      Initialize the frame buffer subsystem.
1393  *
1394  *      NOTE: This function is _only_ to be called by drivers/char/mem.c.
1395  *
1396  */
1397
1398 void __init 
1399 fbmem_init(void)
1400 {
1401         int i;
1402
1403         create_proc_read_entry("fb", 0, 0, fbmem_read_proc, NULL);
1404
1405         devfs_mk_dir("fb");
1406         if (register_chrdev(FB_MAJOR,"fb",&fb_fops))
1407                 printk("unable to get major %d for fb devs\n", FB_MAJOR);
1408
1409         fb_class = class_simple_create(THIS_MODULE, "graphics");
1410         if (IS_ERR(fb_class)) {
1411                 printk(KERN_WARNING "Unable to create fb class; errno = %ld\n", PTR_ERR(fb_class));
1412                 fb_class = NULL;
1413         }
1414
1415 #ifdef CONFIG_FB_OF
1416         if (ofonly) {
1417                 offb_init();
1418                 return;
1419         }
1420 #endif
1421
1422         /*
1423          *  Probe for all builtin frame buffer devices
1424          */
1425         for (i = 0; i < num_pref_init_funcs; i++)
1426                 pref_init_funcs[i]();
1427
1428         for (i = 0; i < NUM_FB_DRIVERS; i++)
1429                 if (fb_drivers[i].init)
1430                         fb_drivers[i].init();
1431 }
1432
1433
1434 /**
1435  *      video_setup - process command line options
1436  *      @options: string of options
1437  *
1438  *      Process command line options for frame buffer subsystem.
1439  *
1440  *      NOTE: This function is a __setup and __init function.
1441  *
1442  *      Returns zero.
1443  *
1444  */
1445
1446 int __init video_setup(char *options)
1447 {
1448         int i, j;
1449
1450         if (!options || !*options)
1451                 return 0;
1452            
1453 #ifdef CONFIG_FB_OF
1454         if (!strcmp(options, "ofonly")) {
1455                 ofonly = 1;
1456                 return 0;
1457         }
1458 #endif
1459
1460         if (num_pref_init_funcs == FB_MAX)
1461                 return 0;
1462
1463         for (i = 0; i < NUM_FB_DRIVERS; i++) {
1464                 j = strlen(fb_drivers[i].name);
1465                 if (!strncmp(options, fb_drivers[i].name, j) &&
1466                         options[j] == ':') {
1467                         if (!strcmp(options+j+1, "off"))
1468                                 fb_drivers[i].init = NULL;
1469                         else {
1470                                 if (fb_drivers[i].init) {
1471                                         pref_init_funcs[num_pref_init_funcs++] =
1472                                                 fb_drivers[i].init;
1473                                         fb_drivers[i].init = NULL;
1474                                 }
1475                                 if (fb_drivers[i].setup)
1476                                         fb_drivers[i].setup(options+j+1);
1477                         }
1478                         return 0;
1479                 }
1480         }
1481
1482         /*
1483          * If we get here no fb was specified.
1484          * We consider the argument to be a global video mode option.
1485          */
1486         global_mode_option = options;
1487         return 0;
1488 }
1489
1490 __setup("video=", video_setup);
1491
1492     /*
1493      *  Visible symbols for modules
1494      */
1495
1496 EXPORT_SYMBOL(register_framebuffer);
1497 EXPORT_SYMBOL(unregister_framebuffer);
1498 EXPORT_SYMBOL(num_registered_fb);
1499 EXPORT_SYMBOL(registered_fb);
1500 EXPORT_SYMBOL(fb_prepare_logo);
1501 EXPORT_SYMBOL(fb_show_logo);
1502 EXPORT_SYMBOL(fb_set_var);
1503 EXPORT_SYMBOL(fb_blank);
1504 EXPORT_SYMBOL(fb_pan_display);
1505 EXPORT_SYMBOL(fb_get_buffer_offset);
1506 EXPORT_SYMBOL(fb_move_buf_unaligned);
1507 EXPORT_SYMBOL(fb_move_buf_aligned);
1508 EXPORT_SYMBOL(fb_load_cursor_image);
1509 EXPORT_SYMBOL(fb_set_suspend);
1510 EXPORT_SYMBOL(fb_register_client);
1511 EXPORT_SYMBOL(fb_unregister_client);
1512
1513 MODULE_LICENSE("GPL");