[PATCH] fbdev: prevent drivers that have hardware cursors from calling software curso...
[safe/jmp/linux-2.6] / drivers / video / fbmem.c
1 /*
2  *  linux/drivers/video/fbmem.c
3  *
4  *  Copyright (C) 1994 Martin Schaller
5  *
6  *      2001 - Documented with DocBook
7  *      - Brad Douglas <brad@neruo.com>
8  *
9  * This file is subject to the terms and conditions of the GNU General Public
10  * License.  See the file COPYING in the main directory of this archive
11  * for more details.
12  */
13
14 #include <linux/config.h>
15 #include <linux/module.h>
16
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/sched.h>
20 #include <linux/smp_lock.h>
21 #include <linux/kernel.h>
22 #include <linux/major.h>
23 #include <linux/slab.h>
24 #include <linux/mm.h>
25 #include <linux/mman.h>
26 #include <linux/tty.h>
27 #include <linux/init.h>
28 #include <linux/linux_logo.h>
29 #include <linux/proc_fs.h>
30 #include <linux/console.h>
31 #ifdef CONFIG_KMOD
32 #include <linux/kmod.h>
33 #endif
34 #include <linux/devfs_fs_kernel.h>
35 #include <linux/err.h>
36 #include <linux/kernel.h>
37 #include <linux/device.h>
38 #include <linux/efi.h>
39
40 #if defined(__mc68000__) || defined(CONFIG_APUS)
41 #include <asm/setup.h>
42 #endif
43
44 #include <asm/io.h>
45 #include <asm/uaccess.h>
46 #include <asm/page.h>
47 #include <asm/pgtable.h>
48
49 #include <linux/fb.h>
50
51     /*
52      *  Frame buffer device initialization and setup routines
53      */
54
55 #define FBPIXMAPSIZE    (1024 * 8)
56
57 static struct notifier_block *fb_notifier_list;
58 struct fb_info *registered_fb[FB_MAX];
59 int num_registered_fb;
60
61 /*
62  * Helpers
63  */
64
65 int fb_get_color_depth(struct fb_var_screeninfo *var)
66 {
67         if (var->green.length == var->blue.length &&
68             var->green.length == var->red.length &&
69             !var->green.offset && !var->blue.offset &&
70             !var->red.offset)
71                 return var->green.length;
72         else
73                 return (var->green.length + var->red.length +
74                         var->blue.length);
75 }
76 EXPORT_SYMBOL(fb_get_color_depth);
77
78 /*
79  * Data padding functions.
80  */
81 void fb_pad_aligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 s_pitch, u32 height)
82 {
83         int i, j;
84
85         for (i = height; i--; ) {
86                 /* s_pitch is a few bytes at the most, memcpy is suboptimal */
87                 for (j = 0; j < s_pitch; j++)
88                         dst[j] = src[j];
89                 src += s_pitch;
90                 dst += d_pitch;
91         }
92 }
93 EXPORT_SYMBOL(fb_pad_aligned_buffer);
94
95 void fb_pad_unaligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 idx, u32 height,
96                                 u32 shift_high, u32 shift_low, u32 mod)
97 {
98         u8 mask = (u8) (0xfff << shift_high), tmp;
99         int i, j;
100
101         for (i = height; i--; ) {
102                 for (j = 0; j < idx; j++) {
103                         tmp = dst[j];
104                         tmp &= mask;
105                         tmp |= *src >> shift_low;
106                         dst[j] = tmp;
107                         tmp = *src << shift_high;
108                         dst[j+1] = tmp;
109                         src++;
110                 }
111                 tmp = dst[idx];
112                 tmp &= mask;
113                 tmp |= *src >> shift_low;
114                 dst[idx] = tmp;
115                 if (shift_high < mod) {
116                         tmp = *src << shift_high;
117                         dst[idx+1] = tmp;
118                 }
119                 src++;
120                 dst += d_pitch;
121         }
122 }
123 EXPORT_SYMBOL(fb_pad_unaligned_buffer);
124
125 /*
126  * we need to lock this section since fb_cursor
127  * may use fb_imageblit()
128  */
129 char* fb_get_buffer_offset(struct fb_info *info, struct fb_pixmap *buf, u32 size)
130 {
131         u32 align = buf->buf_align - 1, offset;
132         char *addr = buf->addr;
133
134         /* If IO mapped, we need to sync before access, no sharing of
135          * the pixmap is done
136          */
137         if (buf->flags & FB_PIXMAP_IO) {
138                 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
139                         info->fbops->fb_sync(info);
140                 return addr;
141         }
142
143         /* See if we fit in the remaining pixmap space */
144         offset = buf->offset + align;
145         offset &= ~align;
146         if (offset + size > buf->size) {
147                 /* We do not fit. In order to be able to re-use the buffer,
148                  * we must ensure no asynchronous DMA'ing or whatever operation
149                  * is in progress, we sync for that.
150                  */
151                 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
152                         info->fbops->fb_sync(info);
153                 offset = 0;
154         }
155         buf->offset = offset + size;
156         addr += offset;
157
158         return addr;
159 }
160
161 #ifdef CONFIG_LOGO
162 #include <linux/linux_logo.h>
163
164 static inline unsigned safe_shift(unsigned d, int n)
165 {
166         return n < 0 ? d >> -n : d << n;
167 }
168
169 static void fb_set_logocmap(struct fb_info *info,
170                                    const struct linux_logo *logo)
171 {
172         struct fb_cmap palette_cmap;
173         u16 palette_green[16];
174         u16 palette_blue[16];
175         u16 palette_red[16];
176         int i, j, n;
177         const unsigned char *clut = logo->clut;
178
179         palette_cmap.start = 0;
180         palette_cmap.len = 16;
181         palette_cmap.red = palette_red;
182         palette_cmap.green = palette_green;
183         palette_cmap.blue = palette_blue;
184         palette_cmap.transp = NULL;
185
186         for (i = 0; i < logo->clutsize; i += n) {
187                 n = logo->clutsize - i;
188                 /* palette_cmap provides space for only 16 colors at once */
189                 if (n > 16)
190                         n = 16;
191                 palette_cmap.start = 32 + i;
192                 palette_cmap.len = n;
193                 for (j = 0; j < n; ++j) {
194                         palette_cmap.red[j] = clut[0] << 8 | clut[0];
195                         palette_cmap.green[j] = clut[1] << 8 | clut[1];
196                         palette_cmap.blue[j] = clut[2] << 8 | clut[2];
197                         clut += 3;
198                 }
199                 fb_set_cmap(&palette_cmap, info);
200         }
201 }
202
203 static void  fb_set_logo_truepalette(struct fb_info *info,
204                                             const struct linux_logo *logo,
205                                             u32 *palette)
206 {
207         unsigned char mask[9] = { 0,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff };
208         unsigned char redmask, greenmask, bluemask;
209         int redshift, greenshift, blueshift;
210         int i;
211         const unsigned char *clut = logo->clut;
212
213         /*
214          * We have to create a temporary palette since console palette is only
215          * 16 colors long.
216          */
217         /* Bug: Doesn't obey msb_right ... (who needs that?) */
218         redmask   = mask[info->var.red.length   < 8 ? info->var.red.length   : 8];
219         greenmask = mask[info->var.green.length < 8 ? info->var.green.length : 8];
220         bluemask  = mask[info->var.blue.length  < 8 ? info->var.blue.length  : 8];
221         redshift   = info->var.red.offset   - (8 - info->var.red.length);
222         greenshift = info->var.green.offset - (8 - info->var.green.length);
223         blueshift  = info->var.blue.offset  - (8 - info->var.blue.length);
224
225         for ( i = 0; i < logo->clutsize; i++) {
226                 palette[i+32] = (safe_shift((clut[0] & redmask), redshift) |
227                                  safe_shift((clut[1] & greenmask), greenshift) |
228                                  safe_shift((clut[2] & bluemask), blueshift));
229                 clut += 3;
230         }
231 }
232
233 static void fb_set_logo_directpalette(struct fb_info *info,
234                                              const struct linux_logo *logo,
235                                              u32 *palette)
236 {
237         int redshift, greenshift, blueshift;
238         int i;
239
240         redshift = info->var.red.offset;
241         greenshift = info->var.green.offset;
242         blueshift = info->var.blue.offset;
243
244         for (i = 32; i < logo->clutsize; i++)
245                 palette[i] = i << redshift | i << greenshift | i << blueshift;
246 }
247
248 static void fb_set_logo(struct fb_info *info,
249                                const struct linux_logo *logo, u8 *dst,
250                                int depth)
251 {
252         int i, j, k, fg = 1;
253         const u8 *src = logo->data;
254         u8 d, xor = (info->fix.visual == FB_VISUAL_MONO01) ? 0xff : 0;
255
256         if (fb_get_color_depth(&info->var) == 3)
257                 fg = 7;
258
259         switch (depth) {
260         case 4:
261                 for (i = 0; i < logo->height; i++)
262                         for (j = 0; j < logo->width; src++) {
263                                 *dst++ = *src >> 4;
264                                 j++;
265                                 if (j < logo->width) {
266                                         *dst++ = *src & 0x0f;
267                                         j++;
268                                 }
269                         }
270                 break;
271         case 1:
272                 for (i = 0; i < logo->height; i++) {
273                         for (j = 0; j < logo->width; src++) {
274                                 d = *src ^ xor;
275                                 for (k = 7; k >= 0; k--) {
276                                         *dst++ = ((d >> k) & 1) ? fg : 0;
277                                         j++;
278                                 }
279                         }
280                 }
281                 break;
282         }
283 }
284
285 /*
286  * Three (3) kinds of logo maps exist.  linux_logo_clut224 (>16 colors),
287  * linux_logo_vga16 (16 colors) and linux_logo_mono (2 colors).  Depending on
288  * the visual format and color depth of the framebuffer, the DAC, the
289  * pseudo_palette, and the logo data will be adjusted accordingly.
290  *
291  * Case 1 - linux_logo_clut224:
292  * Color exceeds the number of console colors (16), thus we set the hardware DAC
293  * using fb_set_cmap() appropriately.  The "needs_cmapreset"  flag will be set.
294  *
295  * For visuals that require color info from the pseudo_palette, we also construct
296  * one for temporary use. The "needs_directpalette" or "needs_truepalette" flags
297  * will be set.
298  *
299  * Case 2 - linux_logo_vga16:
300  * The number of colors just matches the console colors, thus there is no need
301  * to set the DAC or the pseudo_palette.  However, the bitmap is packed, ie,
302  * each byte contains color information for two pixels (upper and lower nibble).
303  * To be consistent with fb_imageblit() usage, we therefore separate the two
304  * nibbles into separate bytes. The "depth" flag will be set to 4.
305  *
306  * Case 3 - linux_logo_mono:
307  * This is similar with Case 2.  Each byte contains information for 8 pixels.
308  * We isolate each bit and expand each into a byte. The "depth" flag will
309  * be set to 1.
310  */
311 static struct logo_data {
312         int depth;
313         int needs_directpalette;
314         int needs_truepalette;
315         int needs_cmapreset;
316         const struct linux_logo *logo;
317 } fb_logo;
318
319 int fb_prepare_logo(struct fb_info *info)
320 {
321         int depth = fb_get_color_depth(&info->var);
322
323         memset(&fb_logo, 0, sizeof(struct logo_data));
324
325         if (info->flags & FBINFO_MISC_TILEBLITTING)
326                 return 0;
327
328         if (info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
329                 depth = info->var.blue.length;
330                 if (info->var.red.length < depth)
331                         depth = info->var.red.length;
332                 if (info->var.green.length < depth)
333                         depth = info->var.green.length;
334         }
335
336         if (depth >= 8) {
337                 switch (info->fix.visual) {
338                 case FB_VISUAL_TRUECOLOR:
339                         fb_logo.needs_truepalette = 1;
340                         break;
341                 case FB_VISUAL_DIRECTCOLOR:
342                         fb_logo.needs_directpalette = 1;
343                         fb_logo.needs_cmapreset = 1;
344                         break;
345                 case FB_VISUAL_PSEUDOCOLOR:
346                         fb_logo.needs_cmapreset = 1;
347                         break;
348                 }
349         }
350
351         /* Return if no suitable logo was found */
352         fb_logo.logo = fb_find_logo(depth);
353         
354         if (!fb_logo.logo || fb_logo.logo->height > info->var.yres) {
355                 fb_logo.logo = NULL;
356                 return 0;
357         }
358         /* What depth we asked for might be different from what we get */
359         if (fb_logo.logo->type == LINUX_LOGO_CLUT224)
360                 fb_logo.depth = 8;
361         else if (fb_logo.logo->type == LINUX_LOGO_VGA16)
362                 fb_logo.depth = 4;
363         else
364                 fb_logo.depth = 1;              
365         return fb_logo.logo->height;
366 }
367
368 int fb_show_logo(struct fb_info *info)
369 {
370         u32 *palette = NULL, *saved_pseudo_palette = NULL;
371         unsigned char *logo_new = NULL;
372         struct fb_image image;
373         int x;
374
375         /* Return if the frame buffer is not mapped or suspended */
376         if (fb_logo.logo == NULL || info->state != FBINFO_STATE_RUNNING)
377                 return 0;
378
379         image.depth = 8;
380         image.data = fb_logo.logo->data;
381
382         if (fb_logo.needs_cmapreset)
383                 fb_set_logocmap(info, fb_logo.logo);
384
385         if (fb_logo.needs_truepalette || 
386             fb_logo.needs_directpalette) {
387                 palette = kmalloc(256 * 4, GFP_KERNEL);
388                 if (palette == NULL)
389                         return 0;
390
391                 if (fb_logo.needs_truepalette)
392                         fb_set_logo_truepalette(info, fb_logo.logo, palette);
393                 else
394                         fb_set_logo_directpalette(info, fb_logo.logo, palette);
395
396                 saved_pseudo_palette = info->pseudo_palette;
397                 info->pseudo_palette = palette;
398         }
399
400         if (fb_logo.depth <= 4) {
401                 logo_new = kmalloc(fb_logo.logo->width * fb_logo.logo->height, 
402                                    GFP_KERNEL);
403                 if (logo_new == NULL) {
404                         kfree(palette);
405                         if (saved_pseudo_palette)
406                                 info->pseudo_palette = saved_pseudo_palette;
407                         return 0;
408                 }
409                 image.data = logo_new;
410                 fb_set_logo(info, fb_logo.logo, logo_new, fb_logo.depth);
411         }
412
413         image.width = fb_logo.logo->width;
414         image.height = fb_logo.logo->height;
415         image.dy = 0;
416
417         for (x = 0; x < num_online_cpus() * (fb_logo.logo->width + 8) &&
418              x <= info->var.xres-fb_logo.logo->width; x += (fb_logo.logo->width + 8)) {
419                 image.dx = x;
420                 info->fbops->fb_imageblit(info, &image);
421         }
422         
423         kfree(palette);
424         if (saved_pseudo_palette != NULL)
425                 info->pseudo_palette = saved_pseudo_palette;
426         kfree(logo_new);
427         return fb_logo.logo->height;
428 }
429 #else
430 int fb_prepare_logo(struct fb_info *info) { return 0; }
431 int fb_show_logo(struct fb_info *info) { return 0; }
432 #endif /* CONFIG_LOGO */
433
434 static int fbmem_read_proc(char *buf, char **start, off_t offset,
435                            int len, int *eof, void *private)
436 {
437         struct fb_info **fi;
438         int clen;
439
440         clen = 0;
441         for (fi = registered_fb; fi < &registered_fb[FB_MAX] && len < 4000; fi++)
442                 if (*fi)
443                         clen += sprintf(buf + clen, "%d %s\n",
444                                         (*fi)->node,
445                                         (*fi)->fix.id);
446         *start = buf + offset;
447         if (clen > offset)
448                 clen -= offset;
449         else
450                 clen = 0;
451         return clen < len ? clen : len;
452 }
453
454 static ssize_t
455 fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
456 {
457         unsigned long p = *ppos;
458         struct inode *inode = file->f_dentry->d_inode;
459         int fbidx = iminor(inode);
460         struct fb_info *info = registered_fb[fbidx];
461         u32 *buffer, *dst;
462         u32 __iomem *src;
463         int c, i, cnt = 0, err = 0;
464         unsigned long total_size;
465
466         if (!info || ! info->screen_base)
467                 return -ENODEV;
468
469         if (info->state != FBINFO_STATE_RUNNING)
470                 return -EPERM;
471
472         if (info->fbops->fb_read)
473                 return info->fbops->fb_read(file, buf, count, ppos);
474         
475         total_size = info->screen_size;
476         if (total_size == 0)
477                 total_size = info->fix.smem_len;
478
479         if (p >= total_size)
480             return 0;
481         if (count >= total_size)
482             count = total_size;
483         if (count + p > total_size)
484                 count = total_size - p;
485
486         cnt = 0;
487         buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
488                          GFP_KERNEL);
489         if (!buffer)
490                 return -ENOMEM;
491
492         src = (u32 __iomem *) (info->screen_base + p);
493
494         if (info->fbops->fb_sync)
495                 info->fbops->fb_sync(info);
496
497         while (count) {
498                 c  = (count > PAGE_SIZE) ? PAGE_SIZE : count;
499                 dst = buffer;
500                 for (i = c >> 2; i--; )
501                         *dst++ = fb_readl(src++);
502                 if (c & 3) {
503                         u8 *dst8 = (u8 *) dst;
504                         u8 __iomem *src8 = (u8 __iomem *) src;
505
506                         for (i = c & 3; i--;)
507                                 *dst8++ = fb_readb(src8++);
508
509                         src = (u32 __iomem *) src8;
510                 }
511
512                 if (copy_to_user(buf, buffer, c)) {
513                         err = -EFAULT;
514                         break;
515                 }
516                 *ppos += c;
517                 buf += c;
518                 cnt += c;
519                 count -= c;
520         }
521
522         kfree(buffer);
523         return (err) ? err : cnt;
524 }
525
526 static ssize_t
527 fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
528 {
529         unsigned long p = *ppos;
530         struct inode *inode = file->f_dentry->d_inode;
531         int fbidx = iminor(inode);
532         struct fb_info *info = registered_fb[fbidx];
533         u32 *buffer, *src;
534         u32 __iomem *dst;
535         int c, i, cnt = 0, err;
536         unsigned long total_size;
537
538         if (!info || !info->screen_base)
539                 return -ENODEV;
540
541         if (info->state != FBINFO_STATE_RUNNING)
542                 return -EPERM;
543
544         if (info->fbops->fb_write)
545                 return info->fbops->fb_write(file, buf, count, ppos);
546         
547         total_size = info->screen_size;
548         if (total_size == 0)
549                 total_size = info->fix.smem_len;
550
551         if (p > total_size)
552             return -ENOSPC;
553         if (count >= total_size)
554             count = total_size;
555         err = 0;
556         if (count + p > total_size) {
557             count = total_size - p;
558             err = -ENOSPC;
559         }
560         cnt = 0;
561         buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
562                          GFP_KERNEL);
563         if (!buffer)
564                 return -ENOMEM;
565
566         dst = (u32 __iomem *) (info->screen_base + p);
567
568         if (info->fbops->fb_sync)
569                 info->fbops->fb_sync(info);
570
571         while (count) {
572                 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
573                 src = buffer;
574                 if (copy_from_user(src, buf, c)) {
575                         err = -EFAULT;
576                         break;
577                 }
578                 for (i = c >> 2; i--; )
579                         fb_writel(*src++, dst++);
580                 if (c & 3) {
581                         u8 *src8 = (u8 *) src;
582                         u8 __iomem *dst8 = (u8 __iomem *) dst;
583
584                         for (i = c & 3; i--; )
585                                 fb_writeb(*src8++, dst8++);
586
587                         dst = (u32 __iomem *) dst8;
588                 }
589                 *ppos += c;
590                 buf += c;
591                 cnt += c;
592                 count -= c;
593         }
594         kfree(buffer);
595
596         return (err) ? err : cnt;
597 }
598
599 #ifdef CONFIG_KMOD
600 static void try_to_load(int fb)
601 {
602         request_module("fb%d", fb);
603 }
604 #endif /* CONFIG_KMOD */
605
606 int
607 fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var)
608 {
609         int xoffset = var->xoffset;
610         int yoffset = var->yoffset;
611         int err;
612
613         if (xoffset < 0 || yoffset < 0 || !info->fbops->fb_pan_display ||
614             xoffset + info->var.xres > info->var.xres_virtual ||
615             yoffset + info->var.yres > info->var.yres_virtual)
616                 return -EINVAL;
617         if ((err = info->fbops->fb_pan_display(var, info)))
618                 return err;
619         info->var.xoffset = var->xoffset;
620         info->var.yoffset = var->yoffset;
621         if (var->vmode & FB_VMODE_YWRAP)
622                 info->var.vmode |= FB_VMODE_YWRAP;
623         else
624                 info->var.vmode &= ~FB_VMODE_YWRAP;
625         return 0;
626 }
627
628 int
629 fb_set_var(struct fb_info *info, struct fb_var_screeninfo *var)
630 {
631         int err, flags = info->flags;
632
633         if (var->activate & FB_ACTIVATE_INV_MODE) {
634                 struct fb_videomode mode1, mode2;
635                 int ret = 0;
636
637                 fb_var_to_videomode(&mode1, var);
638                 fb_var_to_videomode(&mode2, &info->var);
639                 /* make sure we don't delete the videomode of current var */
640                 ret = fb_mode_is_equal(&mode1, &mode2);
641
642                 if (!ret) {
643                     struct fb_event event;
644
645                     event.info = info;
646                     event.data = &mode1;
647                     ret = notifier_call_chain(&fb_notifier_list,
648                                               FB_EVENT_MODE_DELETE, &event);
649                 }
650
651                 if (!ret)
652                     fb_delete_videomode(&mode1, &info->modelist);
653
654                 return ret;
655         }
656
657         if ((var->activate & FB_ACTIVATE_FORCE) ||
658             memcmp(&info->var, var, sizeof(struct fb_var_screeninfo))) {
659                 if (!info->fbops->fb_check_var) {
660                         *var = info->var;
661                         return 0;
662                 }
663
664                 if ((err = info->fbops->fb_check_var(var, info)))
665                         return err;
666
667                 if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
668                         struct fb_videomode mode;
669                         int err = 0;
670
671                         info->var = *var;
672                         if (info->fbops->fb_set_par)
673                                 info->fbops->fb_set_par(info);
674
675                         fb_pan_display(info, &info->var);
676
677                         fb_set_cmap(&info->cmap, info);
678
679                         fb_var_to_videomode(&mode, &info->var);
680
681                         if (info->modelist.prev && info->modelist.next &&
682                             !list_empty(&info->modelist))
683                                 err = fb_add_videomode(&mode, &info->modelist);
684
685                         if (!err && (flags & FBINFO_MISC_USEREVENT)) {
686                                 struct fb_event event;
687                                 int evnt = (var->activate & FB_ACTIVATE_ALL) ?
688                                         FB_EVENT_MODE_CHANGE_ALL :
689                                         FB_EVENT_MODE_CHANGE;
690
691                                 info->flags &= ~FBINFO_MISC_USEREVENT;
692                                 event.info = info;
693                                 notifier_call_chain(&fb_notifier_list, evnt,
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 = 32;
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         unregister_chrdev(FB_MAJOR, "fb");
1172 }
1173
1174 module_exit(fbmem_exit);
1175 MODULE_LICENSE("GPL");
1176 MODULE_DESCRIPTION("Framebuffer base");
1177 #else
1178 subsys_initcall(fbmem_init);
1179 #endif
1180
1181 int fb_new_modelist(struct fb_info *info)
1182 {
1183         struct fb_event event;
1184         struct fb_var_screeninfo var = info->var;
1185         struct list_head *pos, *n;
1186         struct fb_modelist *modelist;
1187         struct fb_videomode *m, mode;
1188         int err = 1;
1189
1190         list_for_each_safe(pos, n, &info->modelist) {
1191                 modelist = list_entry(pos, struct fb_modelist, list);
1192                 m = &modelist->mode;
1193                 fb_videomode_to_var(&var, m);
1194                 var.activate = FB_ACTIVATE_TEST;
1195                 err = fb_set_var(info, &var);
1196                 fb_var_to_videomode(&mode, &var);
1197                 if (err || !fb_mode_is_equal(m, &mode)) {
1198                         list_del(pos);
1199                         kfree(pos);
1200                 }
1201         }
1202
1203         err = 1;
1204
1205         if (!list_empty(&info->modelist)) {
1206                 event.info = info;
1207                 err = notifier_call_chain(&fb_notifier_list,
1208                                            FB_EVENT_NEW_MODELIST,
1209                                            &event);
1210         }
1211
1212         return err;
1213 }
1214
1215 static char *video_options[FB_MAX];
1216 static int ofonly;
1217
1218 extern const char *global_mode_option;
1219
1220 /**
1221  * fb_get_options - get kernel boot parameters
1222  * @name:   framebuffer name as it would appear in
1223  *          the boot parameter line
1224  *          (video=<name>:<options>)
1225  * @option: the option will be stored here
1226  *
1227  * NOTE: Needed to maintain backwards compatibility
1228  */
1229 int fb_get_options(char *name, char **option)
1230 {
1231         char *opt, *options = NULL;
1232         int opt_len, retval = 0;
1233         int name_len = strlen(name), i;
1234
1235         if (name_len && ofonly && strncmp(name, "offb", 4))
1236                 retval = 1;
1237
1238         if (name_len && !retval) {
1239                 for (i = 0; i < FB_MAX; i++) {
1240                         if (video_options[i] == NULL)
1241                                 continue;
1242                         opt_len = strlen(video_options[i]);
1243                         if (!opt_len)
1244                                 continue;
1245                         opt = video_options[i];
1246                         if (!strncmp(name, opt, name_len) &&
1247                             opt[name_len] == ':')
1248                                 options = opt + name_len + 1;
1249                 }
1250         }
1251         if (options && !strncmp(options, "off", 3))
1252                 retval = 1;
1253
1254         if (option)
1255                 *option = options;
1256
1257         return retval;
1258 }
1259
1260 /**
1261  *      video_setup - process command line options
1262  *      @options: string of options
1263  *
1264  *      Process command line options for frame buffer subsystem.
1265  *
1266  *      NOTE: This function is a __setup and __init function.
1267  *            It only stores the options.  Drivers have to call
1268  *            fb_get_options() as necessary.
1269  *
1270  *      Returns zero.
1271  *
1272  */
1273 static int __init video_setup(char *options)
1274 {
1275         int i, global = 0;
1276
1277         if (!options || !*options)
1278                 global = 1;
1279
1280         if (!global && !strncmp(options, "ofonly", 6)) {
1281                 ofonly = 1;
1282                 global = 1;
1283         }
1284
1285         if (!global && !strstr(options, "fb:")) {
1286                 global_mode_option = options;
1287                 global = 1;
1288         }
1289
1290         if (!global) {
1291                 for (i = 0; i < FB_MAX; i++) {
1292                         if (video_options[i] == NULL) {
1293                                 video_options[i] = options;
1294                                 break;
1295                         }
1296
1297                 }
1298         }
1299
1300         return 0;
1301 }
1302 __setup("video=", video_setup);
1303
1304     /*
1305      *  Visible symbols for modules
1306      */
1307
1308 EXPORT_SYMBOL(register_framebuffer);
1309 EXPORT_SYMBOL(unregister_framebuffer);
1310 EXPORT_SYMBOL(num_registered_fb);
1311 EXPORT_SYMBOL(registered_fb);
1312 EXPORT_SYMBOL(fb_prepare_logo);
1313 EXPORT_SYMBOL(fb_show_logo);
1314 EXPORT_SYMBOL(fb_set_var);
1315 EXPORT_SYMBOL(fb_blank);
1316 EXPORT_SYMBOL(fb_pan_display);
1317 EXPORT_SYMBOL(fb_get_buffer_offset);
1318 EXPORT_SYMBOL(fb_set_suspend);
1319 EXPORT_SYMBOL(fb_register_client);
1320 EXPORT_SYMBOL(fb_unregister_client);
1321 EXPORT_SYMBOL(fb_get_options);
1322 EXPORT_SYMBOL(fb_new_modelist);
1323
1324 MODULE_LICENSE("GPL");