[PATCH] vesafb: Fix return code of vesafb_setcolreg
[linux-2.6.git] / drivers / video / vesafb.c
1 /*
2  * framebuffer driver for VBE 2.0 compliant graphic boards
3  *
4  * switching to graphics mode happens at boot time (while
5  * running in real mode, see arch/i386/boot/video.S).
6  *
7  * (c) 1998 Gerd Knorr <kraxel@goldbach.in-berlin.de>
8  *
9  */
10
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include <linux/mm.h>
16 #include <linux/tty.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
19 #include <linux/fb.h>
20 #include <linux/ioport.h>
21 #include <linux/init.h>
22 #include <linux/platform_device.h>
23
24 #include <video/vga.h>
25 #include <asm/io.h>
26 #include <asm/mtrr.h>
27
28 #define dac_reg (0x3c8)
29 #define dac_val (0x3c9)
30
31 /* --------------------------------------------------------------------- */
32
33 static struct fb_var_screeninfo vesafb_defined __initdata = {
34         .activate       = FB_ACTIVATE_NOW,
35         .height         = -1,
36         .width          = -1,
37         .right_margin   = 32,
38         .upper_margin   = 16,
39         .lower_margin   = 4,
40         .vsync_len      = 4,
41         .vmode          = FB_VMODE_NONINTERLACED,
42 };
43
44 static struct fb_fix_screeninfo vesafb_fix __initdata = {
45         .id     = "VESA VGA",
46         .type   = FB_TYPE_PACKED_PIXELS,
47         .accel  = FB_ACCEL_NONE,
48 };
49
50 static int             inverse   = 0;
51 static int             mtrr      = 0; /* disable mtrr */
52 static int             vram_remap __initdata = 0; /* Set amount of memory to be used */
53 static int             vram_total __initdata = 0; /* Set total amount of memory */
54 static int             pmi_setpal = 0;  /* pmi for palette changes ??? */
55 static int             ypan       = 0;  /* 0..nothing, 1..ypan, 2..ywrap */
56 static unsigned short  *pmi_base  = NULL;
57 static void            (*pmi_start)(void);
58 static void            (*pmi_pal)(void);
59 static int             depth;
60 static int             vga_compat;
61 /* --------------------------------------------------------------------- */
62
63 static int vesafb_pan_display(struct fb_var_screeninfo *var,
64                               struct fb_info *info)
65 {
66 #ifdef __i386__
67         int offset;
68
69         offset = (var->yoffset * info->fix.line_length + var->xoffset) / 4;
70
71         __asm__ __volatile__(
72                 "call *(%%edi)"
73                 : /* no return value */
74                 : "a" (0x4f07),         /* EAX */
75                   "b" (0),              /* EBX */
76                   "c" (offset),         /* ECX */
77                   "d" (offset >> 16),   /* EDX */
78                   "D" (&pmi_start));    /* EDI */
79 #endif
80         return 0;
81 }
82
83 static int vesa_setpalette(int regno, unsigned red, unsigned green,
84                             unsigned blue)
85 {
86         int shift = 16 - depth;
87         int err = -EINVAL;
88
89 #ifdef __i386__
90         struct { u_char blue, green, red, pad; } entry;
91
92         if (pmi_setpal) {
93                 entry.red   = red   >> shift;
94                 entry.green = green >> shift;
95                 entry.blue  = blue  >> shift;
96                 entry.pad   = 0;
97                 __asm__ __volatile__(
98                 "call *(%%esi)"
99                 : /* no return value */
100                 : "a" (0x4f09),         /* EAX */
101                   "b" (0),              /* EBX */
102                   "c" (1),              /* ECX */
103                   "d" (regno),          /* EDX */
104                   "D" (&entry),         /* EDI */
105                   "S" (&pmi_pal));      /* ESI */
106                 err = 0;
107         }
108 #endif
109
110 /*
111  * without protected mode interface and if VGA compatible,
112  * try VGA registers...
113  */
114         if (err && vga_compat) {
115                 outb_p(regno,       dac_reg);
116                 outb_p(red   >> shift, dac_val);
117                 outb_p(green >> shift, dac_val);
118                 outb_p(blue  >> shift, dac_val);
119                 err = 0;
120         }
121
122         return err;
123 }
124
125 static int vesafb_setcolreg(unsigned regno, unsigned red, unsigned green,
126                             unsigned blue, unsigned transp,
127                             struct fb_info *info)
128 {
129         int err = 0;
130
131         /*
132          *  Set a single color register. The values supplied are
133          *  already rounded down to the hardware's capabilities
134          *  (according to the entries in the `var' structure). Return
135          *  != 0 for invalid regno.
136          */
137         
138         if (regno >= info->cmap.len)
139                 return 1;
140
141         if (info->var.bits_per_pixel == 8)
142                 err = vesa_setpalette(regno,red,green,blue);
143         else if (regno < 16) {
144                 switch (info->var.bits_per_pixel) {
145                 case 16:
146                         if (info->var.red.offset == 10) {
147                                 /* 1:5:5:5 */
148                                 ((u32*) (info->pseudo_palette))[regno] =
149                                         ((red   & 0xf800) >>  1) |
150                                         ((green & 0xf800) >>  6) |
151                                         ((blue  & 0xf800) >> 11);
152                         } else {
153                                 /* 0:5:6:5 */
154                                 ((u32*) (info->pseudo_palette))[regno] =
155                                         ((red   & 0xf800)      ) |
156                                         ((green & 0xfc00) >>  5) |
157                                         ((blue  & 0xf800) >> 11);
158                         }
159                         break;
160                 case 24:
161                 case 32:
162                         red   >>= 8;
163                         green >>= 8;
164                         blue  >>= 8;
165                         ((u32 *)(info->pseudo_palette))[regno] =
166                                 (red   << info->var.red.offset)   |
167                                 (green << info->var.green.offset) |
168                                 (blue  << info->var.blue.offset);
169                         break;
170                 }
171         }
172
173         return err;
174 }
175
176 static struct fb_ops vesafb_ops = {
177         .owner          = THIS_MODULE,
178         .fb_setcolreg   = vesafb_setcolreg,
179         .fb_pan_display = vesafb_pan_display,
180         .fb_fillrect    = cfb_fillrect,
181         .fb_copyarea    = cfb_copyarea,
182         .fb_imageblit   = cfb_imageblit,
183 };
184
185 static int __init vesafb_setup(char *options)
186 {
187         char *this_opt;
188         
189         if (!options || !*options)
190                 return 0;
191         
192         while ((this_opt = strsep(&options, ",")) != NULL) {
193                 if (!*this_opt) continue;
194                 
195                 if (! strcmp(this_opt, "inverse"))
196                         inverse=1;
197                 else if (! strcmp(this_opt, "redraw"))
198                         ypan=0;
199                 else if (! strcmp(this_opt, "ypan"))
200                         ypan=1;
201                 else if (! strcmp(this_opt, "ywrap"))
202                         ypan=2;
203                 else if (! strcmp(this_opt, "vgapal"))
204                         pmi_setpal=0;
205                 else if (! strcmp(this_opt, "pmipal"))
206                         pmi_setpal=1;
207                 else if (! strncmp(this_opt, "mtrr:", 5))
208                         mtrr = simple_strtoul(this_opt+5, NULL, 0);
209                 else if (! strcmp(this_opt, "nomtrr"))
210                         mtrr=0;
211                 else if (! strncmp(this_opt, "vtotal:", 7))
212                         vram_total = simple_strtoul(this_opt+7, NULL, 0);
213                 else if (! strncmp(this_opt, "vremap:", 7))
214                         vram_remap = simple_strtoul(this_opt+7, NULL, 0);
215         }
216         return 0;
217 }
218
219 static int __init vesafb_probe(struct platform_device *dev)
220 {
221         struct fb_info *info;
222         int i, err;
223         unsigned int size_vmode;
224         unsigned int size_remap;
225         unsigned int size_total;
226
227         if (screen_info.orig_video_isVGA != VIDEO_TYPE_VLFB)
228                 return -ENODEV;
229
230         vga_compat = (screen_info.capabilities & 2) ? 0 : 1;
231         vesafb_fix.smem_start = screen_info.lfb_base;
232         vesafb_defined.bits_per_pixel = screen_info.lfb_depth;
233         if (15 == vesafb_defined.bits_per_pixel)
234                 vesafb_defined.bits_per_pixel = 16;
235         vesafb_defined.xres = screen_info.lfb_width;
236         vesafb_defined.yres = screen_info.lfb_height;
237         vesafb_fix.line_length = screen_info.lfb_linelength;
238         vesafb_fix.visual   = (vesafb_defined.bits_per_pixel == 8) ?
239                 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
240
241         /*   size_vmode -- that is the amount of memory needed for the
242          *                 used video mode, i.e. the minimum amount of
243          *                 memory we need. */
244         size_vmode = vesafb_defined.yres * vesafb_fix.line_length;
245
246         /*   size_total -- all video memory we have. Used for mtrr
247          *                 entries, ressource allocation and bounds
248          *                 checking. */
249         size_total = screen_info.lfb_size * 65536;
250         if (vram_total)
251                 size_total = vram_total * 1024 * 1024;
252         if (size_total < size_vmode)
253                 size_total = size_vmode;
254
255         /*   size_remap -- the amount of video memory we are going to
256          *                 use for vesafb.  With modern cards it is no
257          *                 option to simply use size_total as that
258          *                 wastes plenty of kernel address space. */
259         size_remap  = size_vmode * 2;
260         if (vram_remap)
261                 size_remap = vram_remap * 1024 * 1024;
262         if (size_remap < size_vmode)
263                 size_remap = size_vmode;
264         if (size_remap > size_total)
265                 size_remap = size_total;
266         vesafb_fix.smem_len = size_remap;
267
268 #ifndef __i386__
269         screen_info.vesapm_seg = 0;
270 #endif
271
272         if (!request_mem_region(vesafb_fix.smem_start, size_total, "vesafb")) {
273                 printk(KERN_WARNING
274                        "vesafb: cannot reserve video memory at 0x%lx\n",
275                         vesafb_fix.smem_start);
276                 /* We cannot make this fatal. Sometimes this comes from magic
277                    spaces our resource handlers simply don't know about */
278         }
279
280         info = framebuffer_alloc(sizeof(u32) * 256, &dev->dev);
281         if (!info) {
282                 release_mem_region(vesafb_fix.smem_start, size_total);
283                 return -ENOMEM;
284         }
285         info->pseudo_palette = info->par;
286         info->par = NULL;
287
288         info->screen_base = ioremap(vesafb_fix.smem_start, vesafb_fix.smem_len);
289         if (!info->screen_base) {
290                 printk(KERN_ERR
291                        "vesafb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
292                         vesafb_fix.smem_len, vesafb_fix.smem_start);
293                 err = -EIO;
294                 goto err;
295         }
296
297         printk(KERN_INFO "vesafb: framebuffer at 0x%lx, mapped to 0x%p, "
298                "using %dk, total %dk\n",
299                vesafb_fix.smem_start, info->screen_base,
300                size_remap/1024, size_total/1024);
301         printk(KERN_INFO "vesafb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
302                vesafb_defined.xres, vesafb_defined.yres, vesafb_defined.bits_per_pixel, vesafb_fix.line_length, screen_info.pages);
303
304         if (screen_info.vesapm_seg) {
305                 printk(KERN_INFO "vesafb: protected mode interface info at %04x:%04x\n",
306                        screen_info.vesapm_seg,screen_info.vesapm_off);
307         }
308
309         if (screen_info.vesapm_seg < 0xc000)
310                 ypan = pmi_setpal = 0; /* not available or some DOS TSR ... */
311
312         if (ypan || pmi_setpal) {
313                 pmi_base  = (unsigned short*)phys_to_virt(((unsigned long)screen_info.vesapm_seg << 4) + screen_info.vesapm_off);
314                 pmi_start = (void*)((char*)pmi_base + pmi_base[1]);
315                 pmi_pal   = (void*)((char*)pmi_base + pmi_base[2]);
316                 printk(KERN_INFO "vesafb: pmi: set display start = %p, set palette = %p\n",pmi_start,pmi_pal);
317                 if (pmi_base[3]) {
318                         printk(KERN_INFO "vesafb: pmi: ports = ");
319                                 for (i = pmi_base[3]/2; pmi_base[i] != 0xffff; i++)
320                                         printk("%x ",pmi_base[i]);
321                         printk("\n");
322                         if (pmi_base[i] != 0xffff) {
323                                 /*
324                                  * memory areas not supported (yet?)
325                                  *
326                                  * Rules are: we have to set up a descriptor for the requested
327                                  * memory area and pass it in the ES register to the BIOS function.
328                                  */
329                                 printk(KERN_INFO "vesafb: can't handle memory requests, pmi disabled\n");
330                                 ypan = pmi_setpal = 0;
331                         }
332                 }
333         }
334
335         if (vesafb_defined.bits_per_pixel == 8 && !pmi_setpal && !vga_compat) {
336                 printk(KERN_WARNING "vesafb: hardware palette is unchangeable,\n"
337                                     "        colors may be incorrect\n");
338                 vesafb_fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
339         }
340
341         vesafb_defined.xres_virtual = vesafb_defined.xres;
342         vesafb_defined.yres_virtual = vesafb_fix.smem_len / vesafb_fix.line_length;
343         if (ypan && vesafb_defined.yres_virtual > vesafb_defined.yres) {
344                 printk(KERN_INFO "vesafb: scrolling: %s using protected mode interface, yres_virtual=%d\n",
345                        (ypan > 1) ? "ywrap" : "ypan",vesafb_defined.yres_virtual);
346         } else {
347                 printk(KERN_INFO "vesafb: scrolling: redraw\n");
348                 vesafb_defined.yres_virtual = vesafb_defined.yres;
349                 ypan = 0;
350         }
351
352         /* some dummy values for timing to make fbset happy */
353         vesafb_defined.pixclock     = 10000000 / vesafb_defined.xres * 1000 / vesafb_defined.yres;
354         vesafb_defined.left_margin  = (vesafb_defined.xres / 8) & 0xf8;
355         vesafb_defined.hsync_len    = (vesafb_defined.xres / 8) & 0xf8;
356         
357         vesafb_defined.red.offset    = screen_info.red_pos;
358         vesafb_defined.red.length    = screen_info.red_size;
359         vesafb_defined.green.offset  = screen_info.green_pos;
360         vesafb_defined.green.length  = screen_info.green_size;
361         vesafb_defined.blue.offset   = screen_info.blue_pos;
362         vesafb_defined.blue.length   = screen_info.blue_size;
363         vesafb_defined.transp.offset = screen_info.rsvd_pos;
364         vesafb_defined.transp.length = screen_info.rsvd_size;
365
366         if (vesafb_defined.bits_per_pixel <= 8) {
367                 depth = vesafb_defined.green.length;
368                 vesafb_defined.red.length =
369                 vesafb_defined.green.length =
370                 vesafb_defined.blue.length =
371                 vesafb_defined.bits_per_pixel;
372         }
373
374         printk(KERN_INFO "vesafb: %s: "
375                "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
376                (vesafb_defined.bits_per_pixel > 8) ?
377                "Truecolor" : (vga_compat || pmi_setpal) ?
378                "Pseudocolor" : "Static Pseudocolor",
379                screen_info.rsvd_size,
380                screen_info.red_size,
381                screen_info.green_size,
382                screen_info.blue_size,
383                screen_info.rsvd_pos,
384                screen_info.red_pos,
385                screen_info.green_pos,
386                screen_info.blue_pos);
387
388         vesafb_fix.ypanstep  = ypan     ? 1 : 0;
389         vesafb_fix.ywrapstep = (ypan>1) ? 1 : 0;
390
391         /* request failure does not faze us, as vgacon probably has this
392          * region already (FIXME) */
393         request_region(0x3c0, 32, "vesafb");
394
395 #ifdef CONFIG_MTRR
396         if (mtrr) {
397                 unsigned int temp_size = size_total;
398                 unsigned int type = 0;
399
400                 switch (mtrr) {
401                 case 1:
402                         type = MTRR_TYPE_UNCACHABLE;
403                         break;
404                 case 2:
405                         type = MTRR_TYPE_WRBACK;
406                         break;
407                 case 3:
408                         type = MTRR_TYPE_WRCOMB;
409                         break;
410                 case 4:
411                         type = MTRR_TYPE_WRTHROUGH;
412                         break;
413                 default:
414                         type = 0;
415                         break;
416                 }
417
418                 if (type) {
419                         int rc;
420
421                         /* Find the largest power-of-two */
422                         while (temp_size & (temp_size - 1))
423                                 temp_size &= (temp_size - 1);
424
425                         /* Try and find a power of two to add */
426                         do {
427                                 rc = mtrr_add(vesafb_fix.smem_start, temp_size,
428                                               type, 1);
429                                 temp_size >>= 1;
430                         } while (temp_size >= PAGE_SIZE && rc == -EINVAL);
431                 }
432         }
433 #endif
434         
435         info->fbops = &vesafb_ops;
436         info->var = vesafb_defined;
437         info->fix = vesafb_fix;
438         info->flags = FBINFO_FLAG_DEFAULT |
439                 (ypan) ? FBINFO_HWACCEL_YPAN : 0;
440
441         if (!ypan)
442                 info->fbops->fb_pan_display = NULL;
443
444         if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
445                 err = -ENOMEM;
446                 goto err;
447         }
448         if (register_framebuffer(info)<0) {
449                 err = -EINVAL;
450                 fb_dealloc_cmap(&info->cmap);
451                 goto err;
452         }
453         printk(KERN_INFO "fb%d: %s frame buffer device\n",
454                info->node, info->fix.id);
455         return 0;
456 err:
457         framebuffer_release(info);
458         release_mem_region(vesafb_fix.smem_start, size_total);
459         return err;
460 }
461
462 static struct platform_driver vesafb_driver = {
463         .probe  = vesafb_probe,
464         .driver = {
465                 .name   = "vesafb",
466         },
467 };
468
469 static struct platform_device *vesafb_device;
470
471 static int __init vesafb_init(void)
472 {
473         int ret;
474         char *option = NULL;
475
476         /* ignore error return of fb_get_options */
477         fb_get_options("vesafb", &option);
478         vesafb_setup(option);
479         ret = platform_driver_register(&vesafb_driver);
480
481         if (!ret) {
482                 vesafb_device = platform_device_alloc("vesafb", 0);
483
484                 if (vesafb_device)
485                         ret = platform_device_add(vesafb_device);
486                 else
487                         ret = -ENOMEM;
488
489                 if (ret) {
490                         platform_device_put(vesafb_device);
491                         platform_driver_unregister(&vesafb_driver);
492                 }
493         }
494
495         return ret;
496 }
497 module_init(vesafb_init);
498
499 MODULE_LICENSE("GPL");