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