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