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