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