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