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