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