Merge git://git.infradead.org/iommu-2.6
[linux-2.6] / drivers / video / matrox / matroxfb_base.c
1 /*
2  *
3  * Hardware accelerated Matrox Millennium I, II, Mystique, G100, G200 and G400
4  *
5  * (c) 1998-2002 Petr Vandrovec <vandrove@vc.cvut.cz>
6  *
7  * Portions Copyright (c) 2001 Matrox Graphics Inc.
8  *
9  * Version: 1.65 2002/08/14
10  *
11  * MTRR stuff: 1998 Tom Rini <trini@kernel.crashing.org>
12  *
13  * Contributors: "menion?" <menion@mindless.com>
14  *                     Betatesting, fixes, ideas
15  *
16  *               "Kurt Garloff" <garloff@suse.de>
17  *                     Betatesting, fixes, ideas, videomodes, videomodes timmings
18  *
19  *               "Tom Rini" <trini@kernel.crashing.org>
20  *                     MTRR stuff, PPC cleanups, betatesting, fixes, ideas
21  *
22  *               "Bibek Sahu" <scorpio@dodds.net>
23  *                     Access device through readb|w|l and write b|w|l
24  *                     Extensive debugging stuff
25  *
26  *               "Daniel Haun" <haund@usa.net>
27  *                     Testing, hardware cursor fixes
28  *
29  *               "Scott Wood" <sawst46+@pitt.edu>
30  *                     Fixes
31  *
32  *               "Gerd Knorr" <kraxel@goldbach.isdn.cs.tu-berlin.de>
33  *                     Betatesting
34  *
35  *               "Kelly French" <targon@hazmat.com>
36  *               "Fernando Herrera" <fherrera@eurielec.etsit.upm.es>
37  *                     Betatesting, bug reporting
38  *
39  *               "Pablo Bianucci" <pbian@pccp.com.ar>
40  *                     Fixes, ideas, betatesting
41  *
42  *               "Inaky Perez Gonzalez" <inaky@peloncho.fis.ucm.es>
43  *                     Fixes, enhandcements, ideas, betatesting
44  *
45  *               "Ryuichi Oikawa" <roikawa@rr.iiij4u.or.jp>
46  *                     PPC betatesting, PPC support, backward compatibility
47  *
48  *               "Paul Womar" <Paul@pwomar.demon.co.uk>
49  *               "Owen Waller" <O.Waller@ee.qub.ac.uk>
50  *                     PPC betatesting
51  *
52  *               "Thomas Pornin" <pornin@bolet.ens.fr>
53  *                     Alpha betatesting
54  *
55  *               "Pieter van Leuven" <pvl@iae.nl>
56  *               "Ulf Jaenicke-Roessler" <ujr@physik.phy.tu-dresden.de>
57  *                     G100 testing
58  *
59  *               "H. Peter Arvin" <hpa@transmeta.com>
60  *                     Ideas
61  *
62  *               "Cort Dougan" <cort@cs.nmt.edu>
63  *                     CHRP fixes and PReP cleanup
64  *
65  *               "Mark Vojkovich" <mvojkovi@ucsd.edu>
66  *                     G400 support
67  *
68  *               "Samuel Hocevar" <sam@via.ecp.fr>
69  *                     Fixes
70  *
71  *               "Anton Altaparmakov" <AntonA@bigfoot.com>
72  *                     G400 MAX/non-MAX distinction
73  *
74  *               "Ken Aaker" <kdaaker@rchland.vnet.ibm.com>
75  *                     memtype extension (needed for GXT130P RS/6000 adapter)
76  *
77  *               "Uns Lider" <unslider@miranda.org>
78  *                     G100 PLNWT fixes
79  *
80  *               "Denis Zaitsev" <zzz@cd-club.ru>
81  *                     Fixes
82  *
83  *               "Mike Pieper" <mike@pieper-family.de>
84  *                     TVOut enhandcements, V4L2 control interface.
85  *
86  *               "Diego Biurrun" <diego@biurrun.de>
87  *                     DFP testing
88  *
89  * (following author is not in any relation with this code, but his code
90  *  is included in this driver)
91  *
92  * Based on framebuffer driver for VBE 2.0 compliant graphic boards
93  *     (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
94  *
95  * (following author is not in any relation with this code, but his ideas
96  *  were used when writing this driver)
97  *
98  *               FreeVBE/AF (Matrox), "Shawn Hargreaves" <shawn@talula.demon.co.uk>
99  *
100  */
101
102 #include <linux/version.h>
103
104 #define __OLD_VIDIOC_
105
106 #include "matroxfb_base.h"
107 #include "matroxfb_misc.h"
108 #include "matroxfb_accel.h"
109 #include "matroxfb_DAC1064.h"
110 #include "matroxfb_Ti3026.h"
111 #include "matroxfb_maven.h"
112 #include "matroxfb_crtc2.h"
113 #include "matroxfb_g450.h"
114 #include <linux/matroxfb.h>
115 #include <linux/interrupt.h>
116 #include <linux/uaccess.h>
117
118 #ifdef CONFIG_PPC_PMAC
119 #include <asm/machdep.h>
120 unsigned char nvram_read_byte(int);
121 static int default_vmode = VMODE_NVRAM;
122 static int default_cmode = CMODE_NVRAM;
123 #endif
124
125 static void matroxfb_unregister_device(struct matrox_fb_info* minfo);
126
127 /* --------------------------------------------------------------------- */
128
129 /*
130  * card parameters
131  */
132
133 /* --------------------------------------------------------------------- */
134
135 static struct fb_var_screeninfo vesafb_defined = {
136         640,480,640,480,/* W,H, W, H (virtual) load xres,xres_virtual*/
137         0,0,            /* virtual -> visible no offset */
138         8,              /* depth -> load bits_per_pixel */
139         0,              /* greyscale ? */
140         {0,0,0},        /* R */
141         {0,0,0},        /* G */
142         {0,0,0},        /* B */
143         {0,0,0},        /* transparency */
144         0,              /* standard pixel format */
145         FB_ACTIVATE_NOW,
146         -1,-1,
147         FB_ACCELF_TEXT, /* accel flags */
148         39721L,48L,16L,33L,10L,
149         96L,2L,~0,      /* No sync info */
150         FB_VMODE_NONINTERLACED,
151         0, {0,0,0,0,0}
152 };
153
154
155
156 /* --------------------------------------------------------------------- */
157 static void update_crtc2(WPMINFO unsigned int pos) {
158         struct matroxfb_dh_fb_info* info = ACCESS_FBINFO(crtc2.info);
159
160         /* Make sure that displays are compatible */
161         if (info && (info->fbcon.var.bits_per_pixel == ACCESS_FBINFO(fbcon).var.bits_per_pixel)
162                  && (info->fbcon.var.xres_virtual == ACCESS_FBINFO(fbcon).var.xres_virtual)
163                  && (info->fbcon.var.green.length == ACCESS_FBINFO(fbcon).var.green.length)
164                  ) {
165                 switch (ACCESS_FBINFO(fbcon).var.bits_per_pixel) {
166                         case 16:
167                         case 32:
168                                 pos = pos * 8;
169                                 if (info->interlaced) {
170                                         mga_outl(0x3C2C, pos);
171                                         mga_outl(0x3C28, pos + ACCESS_FBINFO(fbcon).var.xres_virtual * ACCESS_FBINFO(fbcon).var.bits_per_pixel / 8);
172                                 } else {
173                                         mga_outl(0x3C28, pos);
174                                 }
175                                 break;
176                 }
177         }
178 }
179
180 static void matroxfb_crtc1_panpos(WPMINFO2) {
181         if (ACCESS_FBINFO(crtc1.panpos) >= 0) {
182                 unsigned long flags;
183                 int panpos;
184
185                 matroxfb_DAC_lock_irqsave(flags);
186                 panpos = ACCESS_FBINFO(crtc1.panpos);
187                 if (panpos >= 0) {
188                         unsigned int extvga_reg;
189
190                         ACCESS_FBINFO(crtc1.panpos) = -1; /* No update pending anymore */
191                         extvga_reg = mga_inb(M_EXTVGA_INDEX);
192                         mga_setr(M_EXTVGA_INDEX, 0x00, panpos);
193                         if (extvga_reg != 0x00) {
194                                 mga_outb(M_EXTVGA_INDEX, extvga_reg);
195                         }
196                 }
197                 matroxfb_DAC_unlock_irqrestore(flags);
198         }
199 }
200
201 static irqreturn_t matrox_irq(int irq, void *dev_id)
202 {
203         u_int32_t status;
204         int handled = 0;
205
206         MINFO_FROM(dev_id);
207
208         status = mga_inl(M_STATUS);
209
210         if (status & 0x20) {
211                 mga_outl(M_ICLEAR, 0x20);
212                 ACCESS_FBINFO(crtc1.vsync.cnt)++;
213                 matroxfb_crtc1_panpos(PMINFO2);
214                 wake_up_interruptible(&ACCESS_FBINFO(crtc1.vsync.wait));
215                 handled = 1;
216         }
217         if (status & 0x200) {
218                 mga_outl(M_ICLEAR, 0x200);
219                 ACCESS_FBINFO(crtc2.vsync.cnt)++;
220                 wake_up_interruptible(&ACCESS_FBINFO(crtc2.vsync.wait));
221                 handled = 1;
222         }
223         return IRQ_RETVAL(handled);
224 }
225
226 int matroxfb_enable_irq(WPMINFO int reenable) {
227         u_int32_t bm;
228
229         if (ACCESS_FBINFO(devflags.accelerator) == FB_ACCEL_MATROX_MGAG400)
230                 bm = 0x220;
231         else
232                 bm = 0x020;
233
234         if (!test_and_set_bit(0, &ACCESS_FBINFO(irq_flags))) {
235                 if (request_irq(ACCESS_FBINFO(pcidev)->irq, matrox_irq,
236                                 IRQF_SHARED, "matroxfb", MINFO)) {
237                         clear_bit(0, &ACCESS_FBINFO(irq_flags));
238                         return -EINVAL;
239                 }
240                 /* Clear any pending field interrupts */
241                 mga_outl(M_ICLEAR, bm);
242                 mga_outl(M_IEN, mga_inl(M_IEN) | bm);
243         } else if (reenable) {
244                 u_int32_t ien;
245
246                 ien = mga_inl(M_IEN);
247                 if ((ien & bm) != bm) {
248                         printk(KERN_DEBUG "matroxfb: someone disabled IRQ [%08X]\n", ien);
249                         mga_outl(M_IEN, ien | bm);
250                 }
251         }
252         return 0;
253 }
254
255 static void matroxfb_disable_irq(WPMINFO2) {
256         if (test_and_clear_bit(0, &ACCESS_FBINFO(irq_flags))) {
257                 /* Flush pending pan-at-vbl request... */
258                 matroxfb_crtc1_panpos(PMINFO2);
259                 if (ACCESS_FBINFO(devflags.accelerator) == FB_ACCEL_MATROX_MGAG400)
260                         mga_outl(M_IEN, mga_inl(M_IEN) & ~0x220);
261                 else
262                         mga_outl(M_IEN, mga_inl(M_IEN) & ~0x20);
263                 free_irq(ACCESS_FBINFO(pcidev)->irq, MINFO);
264         }
265 }
266
267 int matroxfb_wait_for_sync(WPMINFO u_int32_t crtc) {
268         struct matrox_vsync *vs;
269         unsigned int cnt;
270         int ret;
271
272         switch (crtc) {
273                 case 0:
274                         vs = &ACCESS_FBINFO(crtc1.vsync);
275                         break;
276                 case 1:
277                         if (ACCESS_FBINFO(devflags.accelerator) != FB_ACCEL_MATROX_MGAG400) {
278                                 return -ENODEV;
279                         }
280                         vs = &ACCESS_FBINFO(crtc2.vsync);
281                         break;
282                 default:
283                         return -ENODEV;
284         }
285         ret = matroxfb_enable_irq(PMINFO 0);
286         if (ret) {
287                 return ret;
288         }
289
290         cnt = vs->cnt;
291         ret = wait_event_interruptible_timeout(vs->wait, cnt != vs->cnt, HZ/10);
292         if (ret < 0) {
293                 return ret;
294         }
295         if (ret == 0) {
296                 matroxfb_enable_irq(PMINFO 1);
297                 return -ETIMEDOUT;
298         }
299         return 0;
300 }
301
302 /* --------------------------------------------------------------------- */
303
304 static void matrox_pan_var(WPMINFO struct fb_var_screeninfo *var) {
305         unsigned int pos;
306         unsigned short p0, p1, p2;
307 #ifdef CONFIG_FB_MATROX_32MB
308         unsigned int p3;
309 #endif
310         int vbl;
311         unsigned long flags;
312
313         CRITFLAGS
314
315         DBG(__func__)
316
317         if (ACCESS_FBINFO(dead))
318                 return;
319
320         ACCESS_FBINFO(fbcon).var.xoffset = var->xoffset;
321         ACCESS_FBINFO(fbcon).var.yoffset = var->yoffset;
322         pos = (ACCESS_FBINFO(fbcon).var.yoffset * ACCESS_FBINFO(fbcon).var.xres_virtual + ACCESS_FBINFO(fbcon).var.xoffset) * ACCESS_FBINFO(curr.final_bppShift) / 32;
323         pos += ACCESS_FBINFO(curr.ydstorg.chunks);
324         p0 = ACCESS_FBINFO(hw).CRTC[0x0D] = pos & 0xFF;
325         p1 = ACCESS_FBINFO(hw).CRTC[0x0C] = (pos & 0xFF00) >> 8;
326         p2 = ACCESS_FBINFO(hw).CRTCEXT[0] = (ACCESS_FBINFO(hw).CRTCEXT[0] & 0xB0) | ((pos >> 16) & 0x0F) | ((pos >> 14) & 0x40);
327 #ifdef CONFIG_FB_MATROX_32MB
328         p3 = ACCESS_FBINFO(hw).CRTCEXT[8] = pos >> 21;
329 #endif
330
331         /* FB_ACTIVATE_VBL and we can acquire interrupts? Honor FB_ACTIVATE_VBL then... */
332         vbl = (var->activate & FB_ACTIVATE_VBL) && (matroxfb_enable_irq(PMINFO 0) == 0);
333
334         CRITBEGIN
335
336         matroxfb_DAC_lock_irqsave(flags);
337         mga_setr(M_CRTC_INDEX, 0x0D, p0);
338         mga_setr(M_CRTC_INDEX, 0x0C, p1);
339 #ifdef CONFIG_FB_MATROX_32MB
340         if (ACCESS_FBINFO(devflags.support32MB))
341                 mga_setr(M_EXTVGA_INDEX, 0x08, p3);
342 #endif
343         if (vbl) {
344                 ACCESS_FBINFO(crtc1.panpos) = p2;
345         } else {
346                 /* Abort any pending change */
347                 ACCESS_FBINFO(crtc1.panpos) = -1;
348                 mga_setr(M_EXTVGA_INDEX, 0x00, p2);
349         }
350         matroxfb_DAC_unlock_irqrestore(flags);
351
352         update_crtc2(PMINFO pos);
353
354         CRITEND
355 }
356
357 static void matroxfb_remove(WPMINFO int dummy) {
358         /* Currently we are holding big kernel lock on all dead & usecount updates.
359          * Destroy everything after all users release it. Especially do not unregister
360          * framebuffer and iounmap memory, neither fbmem nor fbcon-cfb* does not check
361          * for device unplugged when in use.
362          * In future we should point mmio.vbase & video.vbase somewhere where we can
363          * write data without causing too much damage...
364          */
365
366         ACCESS_FBINFO(dead) = 1;
367         if (ACCESS_FBINFO(usecount)) {
368                 /* destroy it later */
369                 return;
370         }
371         matroxfb_unregister_device(MINFO);
372         unregister_framebuffer(&ACCESS_FBINFO(fbcon));
373         matroxfb_g450_shutdown(PMINFO2);
374 #ifdef CONFIG_MTRR
375         if (ACCESS_FBINFO(mtrr.vram_valid))
376                 mtrr_del(ACCESS_FBINFO(mtrr.vram), ACCESS_FBINFO(video.base), ACCESS_FBINFO(video.len));
377 #endif
378         mga_iounmap(ACCESS_FBINFO(mmio.vbase));
379         mga_iounmap(ACCESS_FBINFO(video.vbase));
380         release_mem_region(ACCESS_FBINFO(video.base), ACCESS_FBINFO(video.len_maximum));
381         release_mem_region(ACCESS_FBINFO(mmio.base), 16384);
382 #ifdef CONFIG_FB_MATROX_MULTIHEAD
383         kfree(minfo);
384 #endif
385 }
386
387         /*
388          * Open/Release the frame buffer device
389          */
390
391 static int matroxfb_open(struct fb_info *info, int user)
392 {
393         MINFO_FROM_INFO(info);
394
395         DBG_LOOP(__func__)
396
397         if (ACCESS_FBINFO(dead)) {
398                 return -ENXIO;
399         }
400         ACCESS_FBINFO(usecount)++;
401         if (user) {
402                 ACCESS_FBINFO(userusecount)++;
403         }
404         return(0);
405 }
406
407 static int matroxfb_release(struct fb_info *info, int user)
408 {
409         MINFO_FROM_INFO(info);
410
411         DBG_LOOP(__func__)
412
413         if (user) {
414                 if (0 == --ACCESS_FBINFO(userusecount)) {
415                         matroxfb_disable_irq(PMINFO2);
416                 }
417         }
418         if (!(--ACCESS_FBINFO(usecount)) && ACCESS_FBINFO(dead)) {
419                 matroxfb_remove(PMINFO 0);
420         }
421         return(0);
422 }
423
424 static int matroxfb_pan_display(struct fb_var_screeninfo *var,
425                 struct fb_info* info) {
426         MINFO_FROM_INFO(info);
427
428         DBG(__func__)
429
430         matrox_pan_var(PMINFO var);
431         return 0;
432 }
433
434 static int matroxfb_get_final_bppShift(CPMINFO int bpp) {
435         int bppshft2;
436
437         DBG(__func__)
438
439         bppshft2 = bpp;
440         if (!bppshft2) {
441                 return 8;
442         }
443         if (isInterleave(MINFO))
444                 bppshft2 >>= 1;
445         if (ACCESS_FBINFO(devflags.video64bits))
446                 bppshft2 >>= 1;
447         return bppshft2;
448 }
449
450 static int matroxfb_test_and_set_rounding(CPMINFO int xres, int bpp) {
451         int over;
452         int rounding;
453
454         DBG(__func__)
455
456         switch (bpp) {
457                 case 0:         return xres;
458                 case 4:         rounding = 128;
459                                 break;
460                 case 8:         rounding = 64;  /* doc says 64; 32 is OK for G400 */
461                                 break;
462                 case 16:        rounding = 32;
463                                 break;
464                 case 24:        rounding = 64;  /* doc says 64; 32 is OK for G400 */
465                                 break;
466                 default:        rounding = 16;
467                                 /* on G400, 16 really does not work */
468                                 if (ACCESS_FBINFO(devflags.accelerator) == FB_ACCEL_MATROX_MGAG400)
469                                         rounding = 32;
470                                 break;
471         }
472         if (isInterleave(MINFO)) {
473                 rounding *= 2;
474         }
475         over = xres % rounding;
476         if (over)
477                 xres += rounding-over;
478         return xres;
479 }
480
481 static int matroxfb_pitch_adjust(CPMINFO int xres, int bpp) {
482         const int* width;
483         int xres_new;
484
485         DBG(__func__)
486
487         if (!bpp) return xres;
488
489         width = ACCESS_FBINFO(capable.vxres);
490
491         if (ACCESS_FBINFO(devflags.precise_width)) {
492                 while (*width) {
493                         if ((*width >= xres) && (matroxfb_test_and_set_rounding(PMINFO *width, bpp) == *width)) {
494                                 break;
495                         }
496                         width++;
497                 }
498                 xres_new = *width;
499         } else {
500                 xres_new = matroxfb_test_and_set_rounding(PMINFO xres, bpp);
501         }
502         return xres_new;
503 }
504
505 static int matroxfb_get_cmap_len(struct fb_var_screeninfo *var) {
506
507         DBG(__func__)
508
509         switch (var->bits_per_pixel) {
510                 case 4:
511                         return 16;      /* pseudocolor... 16 entries HW palette */
512                 case 8:
513                         return 256;     /* pseudocolor... 256 entries HW palette */
514                 case 16:
515                         return 16;      /* directcolor... 16 entries SW palette */
516                                         /* Mystique: truecolor, 16 entries SW palette, HW palette hardwired into 1:1 mapping */
517                 case 24:
518                         return 16;      /* directcolor... 16 entries SW palette */
519                                         /* Mystique: truecolor, 16 entries SW palette, HW palette hardwired into 1:1 mapping */
520                 case 32:
521                         return 16;      /* directcolor... 16 entries SW palette */
522                                         /* Mystique: truecolor, 16 entries SW palette, HW palette hardwired into 1:1 mapping */
523         }
524         return 16;      /* return something reasonable... or panic()? */
525 }
526
527 static int matroxfb_decode_var(CPMINFO struct fb_var_screeninfo *var, int *visual, int *video_cmap_len, unsigned int* ydstorg) {
528         struct RGBT {
529                 unsigned char bpp;
530                 struct {
531                         unsigned char offset,
532                                       length;
533                 } red,
534                   green,
535                   blue,
536                   transp;
537                 signed char visual;
538         };
539         static const struct RGBT table[]= {
540                 { 8,{ 0,8},{0,8},{0,8},{ 0,0},MX_VISUAL_PSEUDOCOLOR},
541                 {15,{10,5},{5,5},{0,5},{15,1},MX_VISUAL_DIRECTCOLOR},
542                 {16,{11,5},{5,6},{0,5},{ 0,0},MX_VISUAL_DIRECTCOLOR},
543                 {24,{16,8},{8,8},{0,8},{ 0,0},MX_VISUAL_DIRECTCOLOR},
544                 {32,{16,8},{8,8},{0,8},{24,8},MX_VISUAL_DIRECTCOLOR}
545         };
546         struct RGBT const *rgbt;
547         unsigned int bpp = var->bits_per_pixel;
548         unsigned int vramlen;
549         unsigned int memlen;
550
551         DBG(__func__)
552
553         switch (bpp) {
554                 case 4:  if (!ACCESS_FBINFO(capable.cfb4)) return -EINVAL;
555                          break;
556                 case 8:  break;
557                 case 16: break;
558                 case 24: break;
559                 case 32: break;
560                 default: return -EINVAL;
561         }
562         *ydstorg = 0;
563         vramlen = ACCESS_FBINFO(video.len_usable);
564         if (var->yres_virtual < var->yres)
565                 var->yres_virtual = var->yres;
566         if (var->xres_virtual < var->xres)
567                 var->xres_virtual = var->xres;
568
569         var->xres_virtual = matroxfb_pitch_adjust(PMINFO var->xres_virtual, bpp);
570         memlen = var->xres_virtual * bpp * var->yres_virtual / 8;
571         if (memlen > vramlen) {
572                 var->yres_virtual = vramlen * 8 / (var->xres_virtual * bpp);
573                 memlen = var->xres_virtual * bpp * var->yres_virtual / 8;
574         }
575         /* There is hardware bug that no line can cross 4MB boundary */
576         /* give up for CFB24, it is impossible to easy workaround it */
577         /* for other try to do something */
578         if (!ACCESS_FBINFO(capable.cross4MB) && (memlen > 0x400000)) {
579                 if (bpp == 24) {
580                         /* sorry */
581                 } else {
582                         unsigned int linelen;
583                         unsigned int m1 = linelen = var->xres_virtual * bpp / 8;
584                         unsigned int m2 = PAGE_SIZE;    /* or 128 if you do not need PAGE ALIGNED address */
585                         unsigned int max_yres;
586
587                         while (m1) {
588                                 int t;
589
590                                 while (m2 >= m1) m2 -= m1;
591                                 t = m1;
592                                 m1 = m2;
593                                 m2 = t;
594                         }
595                         m2 = linelen * PAGE_SIZE / m2;
596                         *ydstorg = m2 = 0x400000 % m2;
597                         max_yres = (vramlen - m2) / linelen;
598                         if (var->yres_virtual > max_yres)
599                                 var->yres_virtual = max_yres;
600                 }
601         }
602         /* YDSTLEN contains only signed 16bit value */
603         if (var->yres_virtual > 32767)
604                 var->yres_virtual = 32767;
605         /* we must round yres/xres down, we already rounded y/xres_virtual up
606            if it was possible. We should return -EINVAL, but I disagree */
607         if (var->yres_virtual < var->yres)
608                 var->yres = var->yres_virtual;
609         if (var->xres_virtual < var->xres)
610                 var->xres = var->xres_virtual;
611         if (var->xoffset + var->xres > var->xres_virtual)
612                 var->xoffset = var->xres_virtual - var->xres;
613         if (var->yoffset + var->yres > var->yres_virtual)
614                 var->yoffset = var->yres_virtual - var->yres;
615
616         if (bpp == 16 && var->green.length == 5) {
617                 bpp--; /* an artifical value - 15 */
618         }
619
620         for (rgbt = table; rgbt->bpp < bpp; rgbt++);
621 #define SETCLR(clr)\
622         var->clr.offset = rgbt->clr.offset;\
623         var->clr.length = rgbt->clr.length
624         SETCLR(red);
625         SETCLR(green);
626         SETCLR(blue);
627         SETCLR(transp);
628 #undef  SETCLR
629         *visual = rgbt->visual;
630
631         if (bpp > 8)
632                 dprintk("matroxfb: truecolor: "
633                         "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
634                         var->transp.length, var->red.length, var->green.length, var->blue.length,
635                         var->transp.offset, var->red.offset, var->green.offset, var->blue.offset);
636
637         *video_cmap_len = matroxfb_get_cmap_len(var);
638         dprintk(KERN_INFO "requested %d*%d/%dbpp (%d*%d)\n", var->xres, var->yres, var->bits_per_pixel,
639                                 var->xres_virtual, var->yres_virtual);
640         return 0;
641 }
642
643 static int matroxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
644                               unsigned blue, unsigned transp,
645                               struct fb_info *fb_info)
646 {
647 #ifdef CONFIG_FB_MATROX_MULTIHEAD
648         struct matrox_fb_info* minfo = container_of(fb_info, struct matrox_fb_info, fbcon);
649 #endif
650
651         DBG(__func__)
652
653         /*
654          *  Set a single color register. The values supplied are
655          *  already rounded down to the hardware's capabilities
656          *  (according to the entries in the `var' structure). Return
657          *  != 0 for invalid regno.
658          */
659
660         if (regno >= ACCESS_FBINFO(curr.cmap_len))
661                 return 1;
662
663         if (ACCESS_FBINFO(fbcon).var.grayscale) {
664                 /* gray = 0.30*R + 0.59*G + 0.11*B */
665                 red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
666         }
667
668         red = CNVT_TOHW(red, ACCESS_FBINFO(fbcon).var.red.length);
669         green = CNVT_TOHW(green, ACCESS_FBINFO(fbcon).var.green.length);
670         blue = CNVT_TOHW(blue, ACCESS_FBINFO(fbcon).var.blue.length);
671         transp = CNVT_TOHW(transp, ACCESS_FBINFO(fbcon).var.transp.length);
672
673         switch (ACCESS_FBINFO(fbcon).var.bits_per_pixel) {
674         case 4:
675         case 8:
676                 mga_outb(M_DAC_REG, regno);
677                 mga_outb(M_DAC_VAL, red);
678                 mga_outb(M_DAC_VAL, green);
679                 mga_outb(M_DAC_VAL, blue);
680                 break;
681         case 16:
682                 if (regno >= 16)
683                         break;
684                 {
685                         u_int16_t col =
686                                 (red << ACCESS_FBINFO(fbcon).var.red.offset)     |
687                                 (green << ACCESS_FBINFO(fbcon).var.green.offset) |
688                                 (blue << ACCESS_FBINFO(fbcon).var.blue.offset)   |
689                                 (transp << ACCESS_FBINFO(fbcon).var.transp.offset); /* for 1:5:5:5 */
690                         ACCESS_FBINFO(cmap[regno]) = col | (col << 16);
691                 }
692                 break;
693         case 24:
694         case 32:
695                 if (regno >= 16)
696                         break;
697                 ACCESS_FBINFO(cmap[regno]) =
698                         (red   << ACCESS_FBINFO(fbcon).var.red.offset)   |
699                         (green << ACCESS_FBINFO(fbcon).var.green.offset) |
700                         (blue  << ACCESS_FBINFO(fbcon).var.blue.offset)  |
701                         (transp << ACCESS_FBINFO(fbcon).var.transp.offset);     /* 8:8:8:8 */
702                 break;
703         }
704         return 0;
705 }
706
707 static void matroxfb_init_fix(WPMINFO2)
708 {
709         struct fb_fix_screeninfo *fix = &ACCESS_FBINFO(fbcon).fix;
710         DBG(__func__)
711
712         strcpy(fix->id,"MATROX");
713
714         fix->xpanstep = 8;      /* 8 for 8bpp, 4 for 16bpp, 2 for 32bpp */
715         fix->ypanstep = 1;
716         fix->ywrapstep = 0;
717         fix->mmio_start = ACCESS_FBINFO(mmio.base);
718         fix->mmio_len = ACCESS_FBINFO(mmio.len);
719         fix->accel = ACCESS_FBINFO(devflags.accelerator);
720 }
721
722 static void matroxfb_update_fix(WPMINFO2)
723 {
724         struct fb_fix_screeninfo *fix = &ACCESS_FBINFO(fbcon).fix;
725         DBG(__func__)
726
727         mutex_lock(&ACCESS_FBINFO(fbcon).mm_lock);
728         fix->smem_start = ACCESS_FBINFO(video.base) + ACCESS_FBINFO(curr.ydstorg.bytes);
729         fix->smem_len = ACCESS_FBINFO(video.len_usable) - ACCESS_FBINFO(curr.ydstorg.bytes);
730         mutex_unlock(&ACCESS_FBINFO(fbcon).mm_lock);
731 }
732
733 static int matroxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
734 {
735         int err;
736         int visual;
737         int cmap_len;
738         unsigned int ydstorg;
739         MINFO_FROM_INFO(info);
740
741         if (ACCESS_FBINFO(dead)) {
742                 return -ENXIO;
743         }
744         if ((err = matroxfb_decode_var(PMINFO var, &visual, &cmap_len, &ydstorg)) != 0)
745                 return err;
746         return 0;
747 }
748
749 static int matroxfb_set_par(struct fb_info *info)
750 {
751         int err;
752         int visual;
753         int cmap_len;
754         unsigned int ydstorg;
755         struct fb_var_screeninfo *var;
756         MINFO_FROM_INFO(info);
757
758         DBG(__func__)
759
760         if (ACCESS_FBINFO(dead)) {
761                 return -ENXIO;
762         }
763
764         var = &info->var;
765         if ((err = matroxfb_decode_var(PMINFO var, &visual, &cmap_len, &ydstorg)) != 0)
766                 return err;
767         ACCESS_FBINFO(fbcon.screen_base) = vaddr_va(ACCESS_FBINFO(video.vbase)) + ydstorg;
768         matroxfb_update_fix(PMINFO2);
769         ACCESS_FBINFO(fbcon).fix.visual = visual;
770         ACCESS_FBINFO(fbcon).fix.type = FB_TYPE_PACKED_PIXELS;
771         ACCESS_FBINFO(fbcon).fix.type_aux = 0;
772         ACCESS_FBINFO(fbcon).fix.line_length = (var->xres_virtual * var->bits_per_pixel) >> 3;
773         {
774                 unsigned int pos;
775
776                 ACCESS_FBINFO(curr.cmap_len) = cmap_len;
777                 ydstorg += ACCESS_FBINFO(devflags.ydstorg);
778                 ACCESS_FBINFO(curr.ydstorg.bytes) = ydstorg;
779                 ACCESS_FBINFO(curr.ydstorg.chunks) = ydstorg >> (isInterleave(MINFO)?3:2);
780                 if (var->bits_per_pixel == 4)
781                         ACCESS_FBINFO(curr.ydstorg.pixels) = ydstorg;
782                 else
783                         ACCESS_FBINFO(curr.ydstorg.pixels) = (ydstorg * 8) / var->bits_per_pixel;
784                 ACCESS_FBINFO(curr.final_bppShift) = matroxfb_get_final_bppShift(PMINFO var->bits_per_pixel);
785                 {       struct my_timming mt;
786                         struct matrox_hw_state* hw;
787                         int out;
788
789                         matroxfb_var2my(var, &mt);
790                         mt.crtc = MATROXFB_SRC_CRTC1;
791                         /* CRTC1 delays */
792                         switch (var->bits_per_pixel) {
793                                 case  0:        mt.delay = 31 + 0; break;
794                                 case 16:        mt.delay = 21 + 8; break;
795                                 case 24:        mt.delay = 17 + 8; break;
796                                 case 32:        mt.delay = 16 + 8; break;
797                                 default:        mt.delay = 31 + 8; break;
798                         }
799
800                         hw = &ACCESS_FBINFO(hw);
801
802                         down_read(&ACCESS_FBINFO(altout).lock);
803                         for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
804                                 if (ACCESS_FBINFO(outputs[out]).src == MATROXFB_SRC_CRTC1 &&
805                                     ACCESS_FBINFO(outputs[out]).output->compute) {
806                                         ACCESS_FBINFO(outputs[out]).output->compute(ACCESS_FBINFO(outputs[out]).data, &mt);
807                                 }
808                         }
809                         up_read(&ACCESS_FBINFO(altout).lock);
810                         ACCESS_FBINFO(crtc1).pixclock = mt.pixclock;
811                         ACCESS_FBINFO(crtc1).mnp = mt.mnp;
812                         ACCESS_FBINFO(hw_switch->init(PMINFO &mt));
813                         pos = (var->yoffset * var->xres_virtual + var->xoffset) * ACCESS_FBINFO(curr.final_bppShift) / 32;
814                         pos += ACCESS_FBINFO(curr.ydstorg.chunks);
815
816                         hw->CRTC[0x0D] = pos & 0xFF;
817                         hw->CRTC[0x0C] = (pos & 0xFF00) >> 8;
818                         hw->CRTCEXT[0] = (hw->CRTCEXT[0] & 0xF0) | ((pos >> 16) & 0x0F) | ((pos >> 14) & 0x40);
819                         hw->CRTCEXT[8] = pos >> 21;
820                         ACCESS_FBINFO(hw_switch->restore(PMINFO2));
821                         update_crtc2(PMINFO pos);
822                         down_read(&ACCESS_FBINFO(altout).lock);
823                         for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
824                                 if (ACCESS_FBINFO(outputs[out]).src == MATROXFB_SRC_CRTC1 &&
825                                     ACCESS_FBINFO(outputs[out]).output->program) {
826                                         ACCESS_FBINFO(outputs[out]).output->program(ACCESS_FBINFO(outputs[out]).data);
827                                 }
828                         }
829                         for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
830                                 if (ACCESS_FBINFO(outputs[out]).src == MATROXFB_SRC_CRTC1 &&
831                                     ACCESS_FBINFO(outputs[out]).output->start) {
832                                         ACCESS_FBINFO(outputs[out]).output->start(ACCESS_FBINFO(outputs[out]).data);
833                                 }
834                         }
835                         up_read(&ACCESS_FBINFO(altout).lock);
836                         matrox_cfbX_init(PMINFO2);
837                 }
838         }
839         ACCESS_FBINFO(initialized) = 1;
840         return 0;
841 }
842
843 static int matroxfb_get_vblank(WPMINFO struct fb_vblank *vblank)
844 {
845         unsigned int sts1;
846
847         matroxfb_enable_irq(PMINFO 0);
848         memset(vblank, 0, sizeof(*vblank));
849         vblank->flags = FB_VBLANK_HAVE_VCOUNT | FB_VBLANK_HAVE_VSYNC |
850                         FB_VBLANK_HAVE_VBLANK | FB_VBLANK_HAVE_HBLANK;
851         sts1 = mga_inb(M_INSTS1);
852         vblank->vcount = mga_inl(M_VCOUNT);
853         /* BTW, on my PIII/450 with G400, reading M_INSTS1
854            byte makes this call about 12% slower (1.70 vs. 2.05 us
855            per ioctl()) */
856         if (sts1 & 1)
857                 vblank->flags |= FB_VBLANK_HBLANKING;
858         if (sts1 & 8)
859                 vblank->flags |= FB_VBLANK_VSYNCING;
860         if (vblank->vcount >= ACCESS_FBINFO(fbcon).var.yres)
861                 vblank->flags |= FB_VBLANK_VBLANKING;
862         if (test_bit(0, &ACCESS_FBINFO(irq_flags))) {
863                 vblank->flags |= FB_VBLANK_HAVE_COUNT;
864                 /* Only one writer, aligned int value...
865                    it should work without lock and without atomic_t */
866                 vblank->count = ACCESS_FBINFO(crtc1).vsync.cnt;
867         }
868         return 0;
869 }
870
871 static struct matrox_altout panellink_output = {
872         .name    = "Panellink output",
873 };
874
875 static int matroxfb_ioctl(struct fb_info *info,
876                           unsigned int cmd, unsigned long arg)
877 {
878         void __user *argp = (void __user *)arg;
879         MINFO_FROM_INFO(info);
880
881         DBG(__func__)
882
883         if (ACCESS_FBINFO(dead)) {
884                 return -ENXIO;
885         }
886
887         switch (cmd) {
888                 case FBIOGET_VBLANK:
889                         {
890                                 struct fb_vblank vblank;
891                                 int err;
892
893                                 err = matroxfb_get_vblank(PMINFO &vblank);
894                                 if (err)
895                                         return err;
896                                 if (copy_to_user(argp, &vblank, sizeof(vblank)))
897                                         return -EFAULT;
898                                 return 0;
899                         }
900                 case FBIO_WAITFORVSYNC:
901                         {
902                                 u_int32_t crt;
903
904                                 if (get_user(crt, (u_int32_t __user *)arg))
905                                         return -EFAULT;
906
907                                 return matroxfb_wait_for_sync(PMINFO crt);
908                         }
909                 case MATROXFB_SET_OUTPUT_MODE:
910                         {
911                                 struct matroxioc_output_mode mom;
912                                 struct matrox_altout *oproc;
913                                 int val;
914
915                                 if (copy_from_user(&mom, argp, sizeof(mom)))
916                                         return -EFAULT;
917                                 if (mom.output >= MATROXFB_MAX_OUTPUTS)
918                                         return -ENXIO;
919                                 down_read(&ACCESS_FBINFO(altout.lock));
920                                 oproc = ACCESS_FBINFO(outputs[mom.output]).output;
921                                 if (!oproc) {
922                                         val = -ENXIO;
923                                 } else if (!oproc->verifymode) {
924                                         if (mom.mode == MATROXFB_OUTPUT_MODE_MONITOR) {
925                                                 val = 0;
926                                         } else {
927                                                 val = -EINVAL;
928                                         }
929                                 } else {
930                                         val = oproc->verifymode(ACCESS_FBINFO(outputs[mom.output]).data, mom.mode);
931                                 }
932                                 if (!val) {
933                                         if (ACCESS_FBINFO(outputs[mom.output]).mode != mom.mode) {
934                                                 ACCESS_FBINFO(outputs[mom.output]).mode = mom.mode;
935                                                 val = 1;
936                                         }
937                                 }
938                                 up_read(&ACCESS_FBINFO(altout.lock));
939                                 if (val != 1)
940                                         return val;
941                                 switch (ACCESS_FBINFO(outputs[mom.output]).src) {
942                                         case MATROXFB_SRC_CRTC1:
943                                                 matroxfb_set_par(info);
944                                                 break;
945                                         case MATROXFB_SRC_CRTC2:
946                                                 {
947                                                         struct matroxfb_dh_fb_info* crtc2;
948
949                                                         down_read(&ACCESS_FBINFO(crtc2.lock));
950                                                         crtc2 = ACCESS_FBINFO(crtc2.info);
951                                                         if (crtc2)
952                                                                 crtc2->fbcon.fbops->fb_set_par(&crtc2->fbcon);
953                                                         up_read(&ACCESS_FBINFO(crtc2.lock));
954                                                 }
955                                                 break;
956                                 }
957                                 return 0;
958                         }
959                 case MATROXFB_GET_OUTPUT_MODE:
960                         {
961                                 struct matroxioc_output_mode mom;
962                                 struct matrox_altout *oproc;
963                                 int val;
964
965                                 if (copy_from_user(&mom, argp, sizeof(mom)))
966                                         return -EFAULT;
967                                 if (mom.output >= MATROXFB_MAX_OUTPUTS)
968                                         return -ENXIO;
969                                 down_read(&ACCESS_FBINFO(altout.lock));
970                                 oproc = ACCESS_FBINFO(outputs[mom.output]).output;
971                                 if (!oproc) {
972                                         val = -ENXIO;
973                                 } else {
974                                         mom.mode = ACCESS_FBINFO(outputs[mom.output]).mode;
975                                         val = 0;
976                                 }
977                                 up_read(&ACCESS_FBINFO(altout.lock));
978                                 if (val)
979                                         return val;
980                                 if (copy_to_user(argp, &mom, sizeof(mom)))
981                                         return -EFAULT;
982                                 return 0;
983                         }
984                 case MATROXFB_SET_OUTPUT_CONNECTION:
985                         {
986                                 u_int32_t tmp;
987                                 int i;
988                                 int changes;
989
990                                 if (copy_from_user(&tmp, argp, sizeof(tmp)))
991                                         return -EFAULT;
992                                 for (i = 0; i < 32; i++) {
993                                         if (tmp & (1 << i)) {
994                                                 if (i >= MATROXFB_MAX_OUTPUTS)
995                                                         return -ENXIO;
996                                                 if (!ACCESS_FBINFO(outputs[i]).output)
997                                                         return -ENXIO;
998                                                 switch (ACCESS_FBINFO(outputs[i]).src) {
999                                                         case MATROXFB_SRC_NONE:
1000                                                         case MATROXFB_SRC_CRTC1:
1001                                                                 break;
1002                                                         default:
1003                                                                 return -EBUSY;
1004                                                 }
1005                                         }
1006                                 }
1007                                 if (ACCESS_FBINFO(devflags.panellink)) {
1008                                         if (tmp & MATROXFB_OUTPUT_CONN_DFP) {
1009                                                 if (tmp & MATROXFB_OUTPUT_CONN_SECONDARY)
1010                                                         return -EINVAL;
1011                                                 for (i = 0; i < MATROXFB_MAX_OUTPUTS; i++) {
1012                                                         if (ACCESS_FBINFO(outputs[i]).src == MATROXFB_SRC_CRTC2) {
1013                                                                 return -EBUSY;
1014                                                         }
1015                                                 }
1016                                         }
1017                                 }
1018                                 changes = 0;
1019                                 for (i = 0; i < MATROXFB_MAX_OUTPUTS; i++) {
1020                                         if (tmp & (1 << i)) {
1021                                                 if (ACCESS_FBINFO(outputs[i]).src != MATROXFB_SRC_CRTC1) {
1022                                                         changes = 1;
1023                                                         ACCESS_FBINFO(outputs[i]).src = MATROXFB_SRC_CRTC1;
1024                                                 }
1025                                         } else if (ACCESS_FBINFO(outputs[i]).src == MATROXFB_SRC_CRTC1) {
1026                                                 changes = 1;
1027                                                 ACCESS_FBINFO(outputs[i]).src = MATROXFB_SRC_NONE;
1028                                         }
1029                                 }
1030                                 if (!changes)
1031                                         return 0;
1032                                 matroxfb_set_par(info);
1033                                 return 0;
1034                         }
1035                 case MATROXFB_GET_OUTPUT_CONNECTION:
1036                         {
1037                                 u_int32_t conn = 0;
1038                                 int i;
1039
1040                                 for (i = 0; i < MATROXFB_MAX_OUTPUTS; i++) {
1041                                         if (ACCESS_FBINFO(outputs[i]).src == MATROXFB_SRC_CRTC1) {
1042                                                 conn |= 1 << i;
1043                                         }
1044                                 }
1045                                 if (put_user(conn, (u_int32_t __user *)arg))
1046                                         return -EFAULT;
1047                                 return 0;
1048                         }
1049                 case MATROXFB_GET_AVAILABLE_OUTPUTS:
1050                         {
1051                                 u_int32_t conn = 0;
1052                                 int i;
1053
1054                                 for (i = 0; i < MATROXFB_MAX_OUTPUTS; i++) {
1055                                         if (ACCESS_FBINFO(outputs[i]).output) {
1056                                                 switch (ACCESS_FBINFO(outputs[i]).src) {
1057                                                         case MATROXFB_SRC_NONE:
1058                                                         case MATROXFB_SRC_CRTC1:
1059                                                                 conn |= 1 << i;
1060                                                                 break;
1061                                                 }
1062                                         }
1063                                 }
1064                                 if (ACCESS_FBINFO(devflags.panellink)) {
1065                                         if (conn & MATROXFB_OUTPUT_CONN_DFP)
1066                                                 conn &= ~MATROXFB_OUTPUT_CONN_SECONDARY;
1067                                         if (conn & MATROXFB_OUTPUT_CONN_SECONDARY)
1068                                                 conn &= ~MATROXFB_OUTPUT_CONN_DFP;
1069                                 }
1070                                 if (put_user(conn, (u_int32_t __user *)arg))
1071                                         return -EFAULT;
1072                                 return 0;
1073                         }
1074                 case MATROXFB_GET_ALL_OUTPUTS:
1075                         {
1076                                 u_int32_t conn = 0;
1077                                 int i;
1078
1079                                 for (i = 0; i < MATROXFB_MAX_OUTPUTS; i++) {
1080                                         if (ACCESS_FBINFO(outputs[i]).output) {
1081                                                 conn |= 1 << i;
1082                                         }
1083                                 }
1084                                 if (put_user(conn, (u_int32_t __user *)arg))
1085                                         return -EFAULT;
1086                                 return 0;
1087                         }
1088                 case VIDIOC_QUERYCAP:
1089                         {
1090                                 struct v4l2_capability r;
1091
1092                                 memset(&r, 0, sizeof(r));
1093                                 strcpy(r.driver, "matroxfb");
1094                                 strcpy(r.card, "Matrox");
1095                                 sprintf(r.bus_info, "PCI:%s", pci_name(ACCESS_FBINFO(pcidev)));
1096                                 r.version = KERNEL_VERSION(1,0,0);
1097                                 r.capabilities = V4L2_CAP_VIDEO_OUTPUT;
1098                                 if (copy_to_user(argp, &r, sizeof(r)))
1099                                         return -EFAULT;
1100                                 return 0;
1101
1102                         }
1103                 case VIDIOC_QUERYCTRL:
1104                         {
1105                                 struct v4l2_queryctrl qctrl;
1106                                 int err;
1107
1108                                 if (copy_from_user(&qctrl, argp, sizeof(qctrl)))
1109                                         return -EFAULT;
1110
1111                                 down_read(&ACCESS_FBINFO(altout).lock);
1112                                 if (!ACCESS_FBINFO(outputs[1]).output) {
1113                                         err = -ENXIO;
1114                                 } else if (ACCESS_FBINFO(outputs[1]).output->getqueryctrl) {
1115                                         err = ACCESS_FBINFO(outputs[1]).output->getqueryctrl(ACCESS_FBINFO(outputs[1]).data, &qctrl);
1116                                 } else {
1117                                         err = -EINVAL;
1118                                 }
1119                                 up_read(&ACCESS_FBINFO(altout).lock);
1120                                 if (err >= 0 &&
1121                                     copy_to_user(argp, &qctrl, sizeof(qctrl)))
1122                                         return -EFAULT;
1123                                 return err;
1124                         }
1125                 case VIDIOC_G_CTRL:
1126                         {
1127                                 struct v4l2_control ctrl;
1128                                 int err;
1129
1130                                 if (copy_from_user(&ctrl, argp, sizeof(ctrl)))
1131                                         return -EFAULT;
1132
1133                                 down_read(&ACCESS_FBINFO(altout).lock);
1134                                 if (!ACCESS_FBINFO(outputs[1]).output) {
1135                                         err = -ENXIO;
1136                                 } else if (ACCESS_FBINFO(outputs[1]).output->getctrl) {
1137                                         err = ACCESS_FBINFO(outputs[1]).output->getctrl(ACCESS_FBINFO(outputs[1]).data, &ctrl);
1138                                 } else {
1139                                         err = -EINVAL;
1140                                 }
1141                                 up_read(&ACCESS_FBINFO(altout).lock);
1142                                 if (err >= 0 &&
1143                                     copy_to_user(argp, &ctrl, sizeof(ctrl)))
1144                                         return -EFAULT;
1145                                 return err;
1146                         }
1147                 case VIDIOC_S_CTRL_OLD:
1148                 case VIDIOC_S_CTRL:
1149                         {
1150                                 struct v4l2_control ctrl;
1151                                 int err;
1152
1153                                 if (copy_from_user(&ctrl, argp, sizeof(ctrl)))
1154                                         return -EFAULT;
1155
1156                                 down_read(&ACCESS_FBINFO(altout).lock);
1157                                 if (!ACCESS_FBINFO(outputs[1]).output) {
1158                                         err = -ENXIO;
1159                                 } else if (ACCESS_FBINFO(outputs[1]).output->setctrl) {
1160                                         err = ACCESS_FBINFO(outputs[1]).output->setctrl(ACCESS_FBINFO(outputs[1]).data, &ctrl);
1161                                 } else {
1162                                         err = -EINVAL;
1163                                 }
1164                                 up_read(&ACCESS_FBINFO(altout).lock);
1165                                 return err;
1166                         }
1167         }
1168         return -ENOTTY;
1169 }
1170
1171 /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
1172
1173 static int matroxfb_blank(int blank, struct fb_info *info)
1174 {
1175         int seq;
1176         int crtc;
1177         CRITFLAGS
1178         MINFO_FROM_INFO(info);
1179
1180         DBG(__func__)
1181
1182         if (ACCESS_FBINFO(dead))
1183                 return 1;
1184
1185         switch (blank) {
1186         case FB_BLANK_NORMAL:  seq = 0x20; crtc = 0x00; break; /* works ??? */
1187         case FB_BLANK_VSYNC_SUSPEND:  seq = 0x20; crtc = 0x10; break;
1188         case FB_BLANK_HSYNC_SUSPEND:  seq = 0x20; crtc = 0x20; break;
1189         case FB_BLANK_POWERDOWN:  seq = 0x20; crtc = 0x30; break;
1190         default: seq = 0x00; crtc = 0x00; break;
1191         }
1192
1193         CRITBEGIN
1194
1195         mga_outb(M_SEQ_INDEX, 1);
1196         mga_outb(M_SEQ_DATA, (mga_inb(M_SEQ_DATA) & ~0x20) | seq);
1197         mga_outb(M_EXTVGA_INDEX, 1);
1198         mga_outb(M_EXTVGA_DATA, (mga_inb(M_EXTVGA_DATA) & ~0x30) | crtc);
1199
1200         CRITEND
1201         return 0;
1202 }
1203
1204 static struct fb_ops matroxfb_ops = {
1205         .owner =        THIS_MODULE,
1206         .fb_open =      matroxfb_open,
1207         .fb_release =   matroxfb_release,
1208         .fb_check_var = matroxfb_check_var,
1209         .fb_set_par =   matroxfb_set_par,
1210         .fb_setcolreg = matroxfb_setcolreg,
1211         .fb_pan_display =matroxfb_pan_display,
1212         .fb_blank =     matroxfb_blank,
1213         .fb_ioctl =     matroxfb_ioctl,
1214 /*      .fb_fillrect =  <set by matrox_cfbX_init>, */
1215 /*      .fb_copyarea =  <set by matrox_cfbX_init>, */
1216 /*      .fb_imageblit = <set by matrox_cfbX_init>, */
1217 /*      .fb_cursor =    <set by matrox_cfbX_init>, */
1218 };
1219
1220 #define RSDepth(X)      (((X) >> 8) & 0x0F)
1221 #define RS8bpp          0x1
1222 #define RS15bpp         0x2
1223 #define RS16bpp         0x3
1224 #define RS32bpp         0x4
1225 #define RS4bpp          0x5
1226 #define RS24bpp         0x6
1227 #define RSText          0x7
1228 #define RSText8         0x8
1229 /* 9-F */
1230 static struct { struct fb_bitfield red, green, blue, transp; int bits_per_pixel; } colors[] = {
1231         { {  0, 8, 0}, { 0, 8, 0}, { 0, 8, 0}, {  0, 0, 0},  8 },
1232         { { 10, 5, 0}, { 5, 5, 0}, { 0, 5, 0}, { 15, 1, 0}, 16 },
1233         { { 11, 5, 0}, { 5, 6, 0}, { 0, 5, 0}, {  0, 0, 0}, 16 },
1234         { { 16, 8, 0}, { 8, 8, 0}, { 0, 8, 0}, { 24, 8, 0}, 32 },
1235         { {  0, 8, 0}, { 0, 8, 0}, { 0, 8, 0}, {  0, 0, 0},  4 },
1236         { { 16, 8, 0}, { 8, 8, 0}, { 0, 8, 0}, {  0, 0, 0}, 24 },
1237         { {  0, 6, 0}, { 0, 6, 0}, { 0, 6, 0}, {  0, 0, 0},  0 },       /* textmode with (default) VGA8x16 */
1238         { {  0, 6, 0}, { 0, 6, 0}, { 0, 6, 0}, {  0, 0, 0},  0 },       /* textmode hardwired to VGA8x8 */
1239 };
1240
1241 /* initialized by setup, see explanation at end of file (search for MODULE_PARM_DESC) */
1242 static unsigned int mem;                /* "matrox:mem:xxxxxM" */
1243 static int option_precise_width = 1;    /* cannot be changed, option_precise_width==0 must imply noaccel */
1244 static int inv24;                       /* "matrox:inv24" */
1245 static int cross4MB = -1;               /* "matrox:cross4MB" */
1246 static int disabled;                    /* "matrox:disabled" */
1247 static int noaccel;                     /* "matrox:noaccel" */
1248 static int nopan;                       /* "matrox:nopan" */
1249 static int no_pci_retry;                /* "matrox:nopciretry" */
1250 static int novga;                       /* "matrox:novga" */
1251 static int nobios;                      /* "matrox:nobios" */
1252 static int noinit = 1;                  /* "matrox:init" */
1253 static int inverse;                     /* "matrox:inverse" */
1254 static int sgram;                       /* "matrox:sgram" */
1255 #ifdef CONFIG_MTRR
1256 static int mtrr = 1;                    /* "matrox:nomtrr" */
1257 #endif
1258 static int grayscale;                   /* "matrox:grayscale" */
1259 static int dev = -1;                    /* "matrox:dev:xxxxx" */
1260 static unsigned int vesa = ~0;          /* "matrox:vesa:xxxxx" */
1261 static int depth = -1;                  /* "matrox:depth:xxxxx" */
1262 static unsigned int xres;               /* "matrox:xres:xxxxx" */
1263 static unsigned int yres;               /* "matrox:yres:xxxxx" */
1264 static unsigned int upper = ~0;         /* "matrox:upper:xxxxx" */
1265 static unsigned int lower = ~0;         /* "matrox:lower:xxxxx" */
1266 static unsigned int vslen;              /* "matrox:vslen:xxxxx" */
1267 static unsigned int left = ~0;          /* "matrox:left:xxxxx" */
1268 static unsigned int right = ~0;         /* "matrox:right:xxxxx" */
1269 static unsigned int hslen;              /* "matrox:hslen:xxxxx" */
1270 static unsigned int pixclock;           /* "matrox:pixclock:xxxxx" */
1271 static int sync = -1;                   /* "matrox:sync:xxxxx" */
1272 static unsigned int fv;                 /* "matrox:fv:xxxxx" */
1273 static unsigned int fh;                 /* "matrox:fh:xxxxxk" */
1274 static unsigned int maxclk;             /* "matrox:maxclk:xxxxM" */
1275 static int dfp;                         /* "matrox:dfp */
1276 static int dfp_type = -1;               /* "matrox:dfp:xxx */
1277 static int memtype = -1;                /* "matrox:memtype:xxx" */
1278 static char outputs[8];                 /* "matrox:outputs:xxx" */
1279
1280 #ifndef MODULE
1281 static char videomode[64];              /* "matrox:mode:xxxxx" or "matrox:xxxxx" */
1282 #endif
1283
1284 static int matroxfb_getmemory(WPMINFO unsigned int maxSize, unsigned int *realSize){
1285         vaddr_t vm;
1286         unsigned int offs;
1287         unsigned int offs2;
1288         unsigned char orig;
1289         unsigned char bytes[32];
1290         unsigned char* tmp;
1291
1292         DBG(__func__)
1293
1294         vm = ACCESS_FBINFO(video.vbase);
1295         maxSize &= ~0x1FFFFF;   /* must be X*2MB (really it must be 2 or X*4MB) */
1296         /* at least 2MB */
1297         if (maxSize < 0x0200000) return 0;
1298         if (maxSize > 0x2000000) maxSize = 0x2000000;
1299
1300         mga_outb(M_EXTVGA_INDEX, 0x03);
1301         orig = mga_inb(M_EXTVGA_DATA);
1302         mga_outb(M_EXTVGA_DATA, orig | 0x80);
1303
1304         tmp = bytes;
1305         for (offs = 0x100000; offs < maxSize; offs += 0x200000)
1306                 *tmp++ = mga_readb(vm, offs);
1307         for (offs = 0x100000; offs < maxSize; offs += 0x200000)
1308                 mga_writeb(vm, offs, 0x02);
1309         mga_outb(M_CACHEFLUSH, 0x00);
1310         for (offs = 0x100000; offs < maxSize; offs += 0x200000) {
1311                 if (mga_readb(vm, offs) != 0x02)
1312                         break;
1313                 mga_writeb(vm, offs, mga_readb(vm, offs) - 0x02);
1314                 if (mga_readb(vm, offs))
1315                         break;
1316         }
1317         tmp = bytes;
1318         for (offs2 = 0x100000; offs2 < maxSize; offs2 += 0x200000)
1319                 mga_writeb(vm, offs2, *tmp++);
1320
1321         mga_outb(M_EXTVGA_INDEX, 0x03);
1322         mga_outb(M_EXTVGA_DATA, orig);
1323
1324         *realSize = offs - 0x100000;
1325 #ifdef CONFIG_FB_MATROX_MILLENIUM
1326         ACCESS_FBINFO(interleave) = !(!isMillenium(MINFO) || ((offs - 0x100000) & 0x3FFFFF));
1327 #endif
1328         return 1;
1329 }
1330
1331 struct video_board {
1332         int maxvram;
1333         int maxdisplayable;
1334         int accelID;
1335         struct matrox_switch* lowlevel;
1336                  };
1337 #ifdef CONFIG_FB_MATROX_MILLENIUM
1338 static struct video_board vbMillennium          = {0x0800000, 0x0800000, FB_ACCEL_MATROX_MGA2064W,      &matrox_millennium};
1339 static struct video_board vbMillennium2         = {0x1000000, 0x0800000, FB_ACCEL_MATROX_MGA2164W,      &matrox_millennium};
1340 static struct video_board vbMillennium2A        = {0x1000000, 0x0800000, FB_ACCEL_MATROX_MGA2164W_AGP,  &matrox_millennium};
1341 #endif  /* CONFIG_FB_MATROX_MILLENIUM */
1342 #ifdef CONFIG_FB_MATROX_MYSTIQUE
1343 static struct video_board vbMystique            = {0x0800000, 0x0800000, FB_ACCEL_MATROX_MGA1064SG,     &matrox_mystique};
1344 #endif  /* CONFIG_FB_MATROX_MYSTIQUE */
1345 #ifdef CONFIG_FB_MATROX_G
1346 static struct video_board vbG100                = {0x0800000, 0x0800000, FB_ACCEL_MATROX_MGAG100,       &matrox_G100};
1347 static struct video_board vbG200                = {0x1000000, 0x1000000, FB_ACCEL_MATROX_MGAG200,       &matrox_G100};
1348 #ifdef CONFIG_FB_MATROX_32MB
1349 /* from doc it looks like that accelerator can draw only to low 16MB :-( Direct accesses & displaying are OK for
1350    whole 32MB */
1351 static struct video_board vbG400                = {0x2000000, 0x1000000, FB_ACCEL_MATROX_MGAG400,       &matrox_G100};
1352 #else
1353 static struct video_board vbG400                = {0x2000000, 0x1000000, FB_ACCEL_MATROX_MGAG400,       &matrox_G100};
1354 #endif
1355 #endif
1356
1357 #define DEVF_VIDEO64BIT         0x0001
1358 #define DEVF_SWAPS              0x0002
1359 #define DEVF_SRCORG             0x0004
1360 #define DEVF_DUALHEAD           0x0008
1361 #define DEVF_CROSS4MB           0x0010
1362 #define DEVF_TEXT4B             0x0020
1363 /* #define DEVF_recycled        0x0040  */
1364 /* #define DEVF_recycled        0x0080  */
1365 #define DEVF_SUPPORT32MB        0x0100
1366 #define DEVF_ANY_VXRES          0x0200
1367 #define DEVF_TEXT16B            0x0400
1368 #define DEVF_CRTC2              0x0800
1369 #define DEVF_MAVEN_CAPABLE      0x1000
1370 #define DEVF_PANELLINK_CAPABLE  0x2000
1371 #define DEVF_G450DAC            0x4000
1372
1373 #define DEVF_GCORE      (DEVF_VIDEO64BIT | DEVF_SWAPS | DEVF_CROSS4MB)
1374 #define DEVF_G2CORE     (DEVF_GCORE | DEVF_ANY_VXRES | DEVF_MAVEN_CAPABLE | DEVF_PANELLINK_CAPABLE | DEVF_SRCORG | DEVF_DUALHEAD)
1375 #define DEVF_G100       (DEVF_GCORE) /* no doc, no vxres... */
1376 #define DEVF_G200       (DEVF_G2CORE)
1377 #define DEVF_G400       (DEVF_G2CORE | DEVF_SUPPORT32MB | DEVF_TEXT16B | DEVF_CRTC2)
1378 /* if you'll find how to drive DFP... */
1379 #define DEVF_G450       (DEVF_GCORE | DEVF_ANY_VXRES | DEVF_SUPPORT32MB | DEVF_TEXT16B | DEVF_CRTC2 | DEVF_G450DAC | DEVF_SRCORG | DEVF_DUALHEAD)
1380 #define DEVF_G550       (DEVF_G450)
1381
1382 static struct board {
1383         unsigned short vendor, device, rev, svid, sid;
1384         unsigned int flags;
1385         unsigned int maxclk;
1386         enum mga_chip chip;
1387         struct video_board* base;
1388         const char* name;
1389                 } dev_list[] = {
1390 #ifdef CONFIG_FB_MATROX_MILLENIUM
1391         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_MIL,       0xFF,
1392                 0,                      0,
1393                 DEVF_TEXT4B,
1394                 230000,
1395                 MGA_2064,
1396                 &vbMillennium,
1397                 "Millennium (PCI)"},
1398         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_MIL_2,     0xFF,
1399                 0,                      0,
1400                 DEVF_SWAPS,
1401                 220000,
1402                 MGA_2164,
1403                 &vbMillennium2,
1404                 "Millennium II (PCI)"},
1405         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_MIL_2_AGP, 0xFF,
1406                 0,                      0,
1407                 DEVF_SWAPS,
1408                 250000,
1409                 MGA_2164,
1410                 &vbMillennium2A,
1411                 "Millennium II (AGP)"},
1412 #endif
1413 #ifdef CONFIG_FB_MATROX_MYSTIQUE
1414         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_MYS,       0x02,
1415                 0,                      0,
1416                 DEVF_VIDEO64BIT | DEVF_CROSS4MB,
1417                 180000,
1418                 MGA_1064,
1419                 &vbMystique,
1420                 "Mystique (PCI)"},
1421         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_MYS,       0xFF,
1422                 0,                      0,
1423                 DEVF_VIDEO64BIT | DEVF_SWAPS | DEVF_CROSS4MB,
1424                 220000,
1425                 MGA_1164,
1426                 &vbMystique,
1427                 "Mystique 220 (PCI)"},
1428         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_MYS_AGP,   0x02,
1429                 0,                      0,
1430                 DEVF_VIDEO64BIT | DEVF_CROSS4MB,
1431                 180000,
1432                 MGA_1064,
1433                 &vbMystique,
1434                 "Mystique (AGP)"},
1435         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_MYS_AGP,   0xFF,
1436                 0,                      0,
1437                 DEVF_VIDEO64BIT | DEVF_SWAPS | DEVF_CROSS4MB,
1438                 220000,
1439                 MGA_1164,
1440                 &vbMystique,
1441                 "Mystique 220 (AGP)"},
1442 #endif
1443 #ifdef CONFIG_FB_MATROX_G
1444         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G100_MM,   0xFF,
1445                 0,                      0,
1446                 DEVF_G100,
1447                 230000,
1448                 MGA_G100,
1449                 &vbG100,
1450                 "MGA-G100 (PCI)"},
1451         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G100_AGP,  0xFF,
1452                 0,                      0,
1453                 DEVF_G100,
1454                 230000,
1455                 MGA_G100,
1456                 &vbG100,
1457                 "MGA-G100 (AGP)"},
1458         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G200EV_PCI,        0xFF,
1459                 0,                      0,
1460                 DEVF_G200,
1461                 230000,
1462                 MGA_G200,
1463                 &vbG200,
1464                 "MGA-G200eV (PCI)"},
1465         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G200_PCI,  0xFF,
1466                 0,                      0,
1467                 DEVF_G200,
1468                 250000,
1469                 MGA_G200,
1470                 &vbG200,
1471                 "MGA-G200 (PCI)"},
1472         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G200_AGP,  0xFF,
1473                 PCI_SS_VENDOR_ID_MATROX,        PCI_SS_ID_MATROX_GENERIC,
1474                 DEVF_G200,
1475                 220000,
1476                 MGA_G200,
1477                 &vbG200,
1478                 "MGA-G200 (AGP)"},
1479         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G200_AGP,  0xFF,
1480                 PCI_SS_VENDOR_ID_MATROX,        PCI_SS_ID_MATROX_MYSTIQUE_G200_AGP,
1481                 DEVF_G200,
1482                 230000,
1483                 MGA_G200,
1484                 &vbG200,
1485                 "Mystique G200 (AGP)"},
1486         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G200_AGP,  0xFF,
1487                 PCI_SS_VENDOR_ID_MATROX,        PCI_SS_ID_MATROX_MILLENIUM_G200_AGP,
1488                 DEVF_G200,
1489                 250000,
1490                 MGA_G200,
1491                 &vbG200,
1492                 "Millennium G200 (AGP)"},
1493         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G200_AGP,  0xFF,
1494                 PCI_SS_VENDOR_ID_MATROX,        PCI_SS_ID_MATROX_MARVEL_G200_AGP,
1495                 DEVF_G200,
1496                 230000,
1497                 MGA_G200,
1498                 &vbG200,
1499                 "Marvel G200 (AGP)"},
1500         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G200_AGP,  0xFF,
1501                 PCI_SS_VENDOR_ID_SIEMENS_NIXDORF,       PCI_SS_ID_SIEMENS_MGA_G200_AGP,
1502                 DEVF_G200,
1503                 230000,
1504                 MGA_G200,
1505                 &vbG200,
1506                 "MGA-G200 (AGP)"},
1507         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G200_AGP,  0xFF,
1508                 0,                      0,
1509                 DEVF_G200,
1510                 230000,
1511                 MGA_G200,
1512                 &vbG200,
1513                 "G200 (AGP)"},
1514         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G400,      0x80,
1515                 PCI_SS_VENDOR_ID_MATROX,        PCI_SS_ID_MATROX_MILLENNIUM_G400_MAX_AGP,
1516                 DEVF_G400,
1517                 360000,
1518                 MGA_G400,
1519                 &vbG400,
1520                 "Millennium G400 MAX (AGP)"},
1521         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G400,      0x80,
1522                 0,                      0,
1523                 DEVF_G400,
1524                 300000,
1525                 MGA_G400,
1526                 &vbG400,
1527                 "G400 (AGP)"},
1528         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G400,      0xFF,
1529                 0,                      0,
1530                 DEVF_G450,
1531                 360000,
1532                 MGA_G450,
1533                 &vbG400,
1534                 "G450"},
1535         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G550,      0xFF,
1536                 0,                      0,
1537                 DEVF_G550,
1538                 360000,
1539                 MGA_G550,
1540                 &vbG400,
1541                 "G550"},
1542 #endif
1543         {0,                     0,                              0xFF,
1544                 0,                      0,
1545                 0,
1546                 0,
1547                 0,
1548                 NULL,
1549                 NULL}};
1550
1551 #ifndef MODULE
1552 static struct fb_videomode defaultmode = {
1553         /* 640x480 @ 60Hz, 31.5 kHz */
1554         NULL, 60, 640, 480, 39721, 40, 24, 32, 11, 96, 2,
1555         0, FB_VMODE_NONINTERLACED
1556 };
1557 #endif /* !MODULE */
1558
1559 static int hotplug = 0;
1560
1561 static void setDefaultOutputs(WPMINFO2) {
1562         unsigned int i;
1563         const char* ptr;
1564
1565         ACCESS_FBINFO(outputs[0]).default_src = MATROXFB_SRC_CRTC1;
1566         if (ACCESS_FBINFO(devflags.g450dac)) {
1567                 ACCESS_FBINFO(outputs[1]).default_src = MATROXFB_SRC_CRTC1;
1568                 ACCESS_FBINFO(outputs[2]).default_src = MATROXFB_SRC_CRTC1;
1569         } else if (dfp) {
1570                 ACCESS_FBINFO(outputs[2]).default_src = MATROXFB_SRC_CRTC1;
1571         }
1572         ptr = outputs;
1573         for (i = 0; i < MATROXFB_MAX_OUTPUTS; i++) {
1574                 char c = *ptr++;
1575
1576                 if (c == 0) {
1577                         break;
1578                 }
1579                 if (c == '0') {
1580                         ACCESS_FBINFO(outputs[i]).default_src = MATROXFB_SRC_NONE;
1581                 } else if (c == '1') {
1582                         ACCESS_FBINFO(outputs[i]).default_src = MATROXFB_SRC_CRTC1;
1583                 } else if (c == '2' && ACCESS_FBINFO(devflags.crtc2)) {
1584                         ACCESS_FBINFO(outputs[i]).default_src = MATROXFB_SRC_CRTC2;
1585                 } else {
1586                         printk(KERN_ERR "matroxfb: Unknown outputs setting\n");
1587                         break;
1588                 }
1589         }
1590         /* Nullify this option for subsequent adapters */
1591         outputs[0] = 0;
1592 }
1593
1594 static int initMatrox2(WPMINFO struct board* b){
1595         unsigned long ctrlptr_phys = 0;
1596         unsigned long video_base_phys = 0;
1597         unsigned int memsize;
1598         int err;
1599
1600         static struct pci_device_id intel_82437[] = {
1601                 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437) },
1602                 { },
1603         };
1604
1605         DBG(__func__)
1606
1607         /* set default values... */
1608         vesafb_defined.accel_flags = FB_ACCELF_TEXT;
1609
1610         ACCESS_FBINFO(hw_switch) = b->base->lowlevel;
1611         ACCESS_FBINFO(devflags.accelerator) = b->base->accelID;
1612         ACCESS_FBINFO(max_pixel_clock) = b->maxclk;
1613
1614         printk(KERN_INFO "matroxfb: Matrox %s detected\n", b->name);
1615         ACCESS_FBINFO(capable.plnwt) = 1;
1616         ACCESS_FBINFO(chip) = b->chip;
1617         ACCESS_FBINFO(capable.srcorg) = b->flags & DEVF_SRCORG;
1618         ACCESS_FBINFO(devflags.video64bits) = b->flags & DEVF_VIDEO64BIT;
1619         if (b->flags & DEVF_TEXT4B) {
1620                 ACCESS_FBINFO(devflags.vgastep) = 4;
1621                 ACCESS_FBINFO(devflags.textmode) = 4;
1622                 ACCESS_FBINFO(devflags.text_type_aux) = FB_AUX_TEXT_MGA_STEP16;
1623         } else if (b->flags & DEVF_TEXT16B) {
1624                 ACCESS_FBINFO(devflags.vgastep) = 16;
1625                 ACCESS_FBINFO(devflags.textmode) = 1;
1626                 ACCESS_FBINFO(devflags.text_type_aux) = FB_AUX_TEXT_MGA_STEP16;
1627         } else {
1628                 ACCESS_FBINFO(devflags.vgastep) = 8;
1629                 ACCESS_FBINFO(devflags.textmode) = 1;
1630                 ACCESS_FBINFO(devflags.text_type_aux) = FB_AUX_TEXT_MGA_STEP8;
1631         }
1632 #ifdef CONFIG_FB_MATROX_32MB
1633         ACCESS_FBINFO(devflags.support32MB) = (b->flags & DEVF_SUPPORT32MB) != 0;
1634 #endif
1635         ACCESS_FBINFO(devflags.precise_width) = !(b->flags & DEVF_ANY_VXRES);
1636         ACCESS_FBINFO(devflags.crtc2) = (b->flags & DEVF_CRTC2) != 0;
1637         ACCESS_FBINFO(devflags.maven_capable) = (b->flags & DEVF_MAVEN_CAPABLE) != 0;
1638         ACCESS_FBINFO(devflags.dualhead) = (b->flags & DEVF_DUALHEAD) != 0;
1639         ACCESS_FBINFO(devflags.dfp_type) = dfp_type;
1640         ACCESS_FBINFO(devflags.g450dac) = (b->flags & DEVF_G450DAC) != 0;
1641         ACCESS_FBINFO(devflags.textstep) = ACCESS_FBINFO(devflags.vgastep) * ACCESS_FBINFO(devflags.textmode);
1642         ACCESS_FBINFO(devflags.textvram) = 65536 / ACCESS_FBINFO(devflags.textmode);
1643         setDefaultOutputs(PMINFO2);
1644         if (b->flags & DEVF_PANELLINK_CAPABLE) {
1645                 ACCESS_FBINFO(outputs[2]).data = MINFO;
1646                 ACCESS_FBINFO(outputs[2]).output = &panellink_output;
1647                 ACCESS_FBINFO(outputs[2]).src = ACCESS_FBINFO(outputs[2]).default_src;
1648                 ACCESS_FBINFO(outputs[2]).mode = MATROXFB_OUTPUT_MODE_MONITOR;
1649                 ACCESS_FBINFO(devflags.panellink) = 1;
1650         }
1651
1652         if (ACCESS_FBINFO(capable.cross4MB) < 0)
1653                 ACCESS_FBINFO(capable.cross4MB) = b->flags & DEVF_CROSS4MB;
1654         if (b->flags & DEVF_SWAPS) {
1655                 ctrlptr_phys = pci_resource_start(ACCESS_FBINFO(pcidev), 1);
1656                 video_base_phys = pci_resource_start(ACCESS_FBINFO(pcidev), 0);
1657                 ACCESS_FBINFO(devflags.fbResource) = PCI_BASE_ADDRESS_0;
1658         } else {
1659                 ctrlptr_phys = pci_resource_start(ACCESS_FBINFO(pcidev), 0);
1660                 video_base_phys = pci_resource_start(ACCESS_FBINFO(pcidev), 1);
1661                 ACCESS_FBINFO(devflags.fbResource) = PCI_BASE_ADDRESS_1;
1662         }
1663         err = -EINVAL;
1664         if (!ctrlptr_phys) {
1665                 printk(KERN_ERR "matroxfb: control registers are not available, matroxfb disabled\n");
1666                 goto fail;
1667         }
1668         if (!video_base_phys) {
1669                 printk(KERN_ERR "matroxfb: video RAM is not available in PCI address space, matroxfb disabled\n");
1670                 goto fail;
1671         }
1672         memsize = b->base->maxvram;
1673         if (!request_mem_region(ctrlptr_phys, 16384, "matroxfb MMIO")) {
1674                 goto fail;
1675         }
1676         if (!request_mem_region(video_base_phys, memsize, "matroxfb FB")) {
1677                 goto failCtrlMR;
1678         }
1679         ACCESS_FBINFO(video.len_maximum) = memsize;
1680         /* convert mem (autodetect k, M) */
1681         if (mem < 1024) mem *= 1024;
1682         if (mem < 0x00100000) mem *= 1024;
1683
1684         if (mem && (mem < memsize))
1685                 memsize = mem;
1686         err = -ENOMEM;
1687         if (mga_ioremap(ctrlptr_phys, 16384, MGA_IOREMAP_MMIO, &ACCESS_FBINFO(mmio.vbase))) {
1688                 printk(KERN_ERR "matroxfb: cannot ioremap(%lX, 16384), matroxfb disabled\n", ctrlptr_phys);
1689                 goto failVideoMR;
1690         }
1691         ACCESS_FBINFO(mmio.base) = ctrlptr_phys;
1692         ACCESS_FBINFO(mmio.len) = 16384;
1693         ACCESS_FBINFO(video.base) = video_base_phys;
1694         if (mga_ioremap(video_base_phys, memsize, MGA_IOREMAP_FB, &ACCESS_FBINFO(video.vbase))) {
1695                 printk(KERN_ERR "matroxfb: cannot ioremap(%lX, %d), matroxfb disabled\n",
1696                         video_base_phys, memsize);
1697                 goto failCtrlIO;
1698         }
1699         {
1700                 u_int32_t cmd;
1701                 u_int32_t mga_option;
1702
1703                 pci_read_config_dword(ACCESS_FBINFO(pcidev), PCI_OPTION_REG, &mga_option);
1704                 pci_read_config_dword(ACCESS_FBINFO(pcidev), PCI_COMMAND, &cmd);
1705                 mga_option &= 0x7FFFFFFF; /* clear BIG_ENDIAN */
1706                 mga_option |= MX_OPTION_BSWAP;
1707                 /* disable palette snooping */
1708                 cmd &= ~PCI_COMMAND_VGA_PALETTE;
1709                 if (pci_dev_present(intel_82437)) {
1710                         if (!(mga_option & 0x20000000) && !ACCESS_FBINFO(devflags.nopciretry)) {
1711                                 printk(KERN_WARNING "matroxfb: Disabling PCI retries due to i82437 present\n");
1712                         }
1713                         mga_option |= 0x20000000;
1714                         ACCESS_FBINFO(devflags.nopciretry) = 1;
1715                 }
1716                 pci_write_config_dword(ACCESS_FBINFO(pcidev), PCI_COMMAND, cmd);
1717                 pci_write_config_dword(ACCESS_FBINFO(pcidev), PCI_OPTION_REG, mga_option);
1718                 ACCESS_FBINFO(hw).MXoptionReg = mga_option;
1719
1720                 /* select non-DMA memory for PCI_MGA_DATA, otherwise dump of PCI cfg space can lock PCI bus */
1721                 /* maybe preinit() candidate, but it is same... for all devices... at this time... */
1722                 pci_write_config_dword(ACCESS_FBINFO(pcidev), PCI_MGA_INDEX, 0x00003C00);
1723         }
1724
1725         err = -ENXIO;
1726         matroxfb_read_pins(PMINFO2);
1727         if (ACCESS_FBINFO(hw_switch)->preinit(PMINFO2)) {
1728                 goto failVideoIO;
1729         }
1730
1731         err = -ENOMEM;
1732         if (!matroxfb_getmemory(PMINFO memsize, &ACCESS_FBINFO(video.len)) || !ACCESS_FBINFO(video.len)) {
1733                 printk(KERN_ERR "matroxfb: cannot determine memory size\n");
1734                 goto failVideoIO;
1735         }
1736         ACCESS_FBINFO(devflags.ydstorg) = 0;
1737
1738         ACCESS_FBINFO(video.base) = video_base_phys;
1739         ACCESS_FBINFO(video.len_usable) = ACCESS_FBINFO(video.len);
1740         if (ACCESS_FBINFO(video.len_usable) > b->base->maxdisplayable)
1741                 ACCESS_FBINFO(video.len_usable) = b->base->maxdisplayable;
1742 #ifdef CONFIG_MTRR
1743         if (mtrr) {
1744                 ACCESS_FBINFO(mtrr.vram) = mtrr_add(video_base_phys, ACCESS_FBINFO(video.len), MTRR_TYPE_WRCOMB, 1);
1745                 ACCESS_FBINFO(mtrr.vram_valid) = 1;
1746                 printk(KERN_INFO "matroxfb: MTRR's turned on\n");
1747         }
1748 #endif  /* CONFIG_MTRR */
1749
1750         if (!ACCESS_FBINFO(devflags.novga))
1751                 request_region(0x3C0, 32, "matrox");
1752         matroxfb_g450_connect(PMINFO2);
1753         ACCESS_FBINFO(hw_switch->reset(PMINFO2));
1754
1755         ACCESS_FBINFO(fbcon.monspecs.hfmin) = 0;
1756         ACCESS_FBINFO(fbcon.monspecs.hfmax) = fh;
1757         ACCESS_FBINFO(fbcon.monspecs.vfmin) = 0;
1758         ACCESS_FBINFO(fbcon.monspecs.vfmax) = fv;
1759         ACCESS_FBINFO(fbcon.monspecs.dpms) = 0; /* TBD */
1760
1761         /* static settings */
1762         vesafb_defined.red = colors[depth-1].red;
1763         vesafb_defined.green = colors[depth-1].green;
1764         vesafb_defined.blue = colors[depth-1].blue;
1765         vesafb_defined.bits_per_pixel = colors[depth-1].bits_per_pixel;
1766         vesafb_defined.grayscale = grayscale;
1767         vesafb_defined.vmode = 0;
1768         if (noaccel)
1769                 vesafb_defined.accel_flags &= ~FB_ACCELF_TEXT;
1770
1771         ACCESS_FBINFO(fbops) = matroxfb_ops;
1772         ACCESS_FBINFO(fbcon.fbops) = &ACCESS_FBINFO(fbops);
1773         ACCESS_FBINFO(fbcon.pseudo_palette) = ACCESS_FBINFO(cmap);
1774         /* after __init time we are like module... no logo */
1775         ACCESS_FBINFO(fbcon.flags) = hotplug ? FBINFO_FLAG_MODULE : FBINFO_FLAG_DEFAULT;
1776         ACCESS_FBINFO(fbcon.flags) |= FBINFO_PARTIAL_PAN_OK |    /* Prefer panning for scroll under MC viewer/edit */
1777                                       FBINFO_HWACCEL_COPYAREA |  /* We have hw-assisted bmove */
1778                                       FBINFO_HWACCEL_FILLRECT |  /* And fillrect */
1779                                       FBINFO_HWACCEL_IMAGEBLIT | /* And imageblit */
1780                                       FBINFO_HWACCEL_XPAN |      /* And we support both horizontal */
1781                                       FBINFO_HWACCEL_YPAN;       /* And vertical panning */
1782         ACCESS_FBINFO(video.len_usable) &= PAGE_MASK;
1783         fb_alloc_cmap(&ACCESS_FBINFO(fbcon.cmap), 256, 1);
1784
1785 #ifndef MODULE
1786         /* mode database is marked __init!!! */
1787         if (!hotplug) {
1788                 fb_find_mode(&vesafb_defined, &ACCESS_FBINFO(fbcon), videomode[0]?videomode:NULL,
1789                         NULL, 0, &defaultmode, vesafb_defined.bits_per_pixel);
1790         }
1791 #endif /* !MODULE */
1792
1793         /* mode modifiers */
1794         if (hslen)
1795                 vesafb_defined.hsync_len = hslen;
1796         if (vslen)
1797                 vesafb_defined.vsync_len = vslen;
1798         if (left != ~0)
1799                 vesafb_defined.left_margin = left;
1800         if (right != ~0)
1801                 vesafb_defined.right_margin = right;
1802         if (upper != ~0)
1803                 vesafb_defined.upper_margin = upper;
1804         if (lower != ~0)
1805                 vesafb_defined.lower_margin = lower;
1806         if (xres)
1807                 vesafb_defined.xres = xres;
1808         if (yres)
1809                 vesafb_defined.yres = yres;
1810         if (sync != -1)
1811                 vesafb_defined.sync = sync;
1812         else if (vesafb_defined.sync == ~0) {
1813                 vesafb_defined.sync = 0;
1814                 if (yres < 400)
1815                         vesafb_defined.sync |= FB_SYNC_HOR_HIGH_ACT;
1816                 else if (yres < 480)
1817                         vesafb_defined.sync |= FB_SYNC_VERT_HIGH_ACT;
1818         }
1819
1820         /* fv, fh, maxclk limits was specified */
1821         {
1822                 unsigned int tmp;
1823
1824                 if (fv) {
1825                         tmp = fv * (vesafb_defined.upper_margin + vesafb_defined.yres
1826                                   + vesafb_defined.lower_margin + vesafb_defined.vsync_len);
1827                         if ((tmp < fh) || (fh == 0)) fh = tmp;
1828                 }
1829                 if (fh) {
1830                         tmp = fh * (vesafb_defined.left_margin + vesafb_defined.xres
1831                                   + vesafb_defined.right_margin + vesafb_defined.hsync_len);
1832                         if ((tmp < maxclk) || (maxclk == 0)) maxclk = tmp;
1833                 }
1834                 tmp = (maxclk + 499) / 500;
1835                 if (tmp) {
1836                         tmp = (2000000000 + tmp) / tmp;
1837                         if (tmp > pixclock) pixclock = tmp;
1838                 }
1839         }
1840         if (pixclock) {
1841                 if (pixclock < 2000)            /* > 500MHz */
1842                         pixclock = 4000;        /* 250MHz */
1843                 if (pixclock > 1000000)
1844                         pixclock = 1000000;     /* 1MHz */
1845                 vesafb_defined.pixclock = pixclock;
1846         }
1847
1848         /* FIXME: Where to move this?! */
1849 #if defined(CONFIG_PPC_PMAC)
1850 #ifndef MODULE
1851         if (machine_is(powermac)) {
1852                 struct fb_var_screeninfo var;
1853                 if (default_vmode <= 0 || default_vmode > VMODE_MAX)
1854                         default_vmode = VMODE_640_480_60;
1855 #ifdef CONFIG_NVRAM
1856                 if (default_cmode == CMODE_NVRAM)
1857                         default_cmode = nvram_read_byte(NV_CMODE);
1858 #endif
1859                 if (default_cmode < CMODE_8 || default_cmode > CMODE_32)
1860                         default_cmode = CMODE_8;
1861                 if (!mac_vmode_to_var(default_vmode, default_cmode, &var)) {
1862                         var.accel_flags = vesafb_defined.accel_flags;
1863                         var.xoffset = var.yoffset = 0;
1864                         /* Note: mac_vmode_to_var() does not set all parameters */
1865                         vesafb_defined = var;
1866                 }
1867         }
1868 #endif /* !MODULE */
1869 #endif /* CONFIG_PPC_PMAC */
1870         vesafb_defined.xres_virtual = vesafb_defined.xres;
1871         if (nopan) {
1872                 vesafb_defined.yres_virtual = vesafb_defined.yres;
1873         } else {
1874                 vesafb_defined.yres_virtual = 65536; /* large enough to be INF, but small enough
1875                                                         to yres_virtual * xres_virtual < 2^32 */
1876         }
1877         matroxfb_init_fix(PMINFO2);
1878         ACCESS_FBINFO(fbcon.screen_base) = vaddr_va(ACCESS_FBINFO(video.vbase));
1879         matroxfb_update_fix(PMINFO2);
1880         /* Normalize values (namely yres_virtual) */
1881         matroxfb_check_var(&vesafb_defined, &ACCESS_FBINFO(fbcon));
1882         /* And put it into "current" var. Do NOT program hardware yet, or we'll not take over
1883          * vgacon correctly. fbcon_startup will call fb_set_par for us, WITHOUT check_var,
1884          * and unfortunately it will do it BEFORE vgacon contents is saved, so it won't work
1885          * anyway. But we at least tried... */
1886         ACCESS_FBINFO(fbcon.var) = vesafb_defined;
1887         err = -EINVAL;
1888
1889         printk(KERN_INFO "matroxfb: %dx%dx%dbpp (virtual: %dx%d)\n",
1890                 vesafb_defined.xres, vesafb_defined.yres, vesafb_defined.bits_per_pixel,
1891                 vesafb_defined.xres_virtual, vesafb_defined.yres_virtual);
1892         printk(KERN_INFO "matroxfb: framebuffer at 0x%lX, mapped to 0x%p, size %d\n",
1893                 ACCESS_FBINFO(video.base), vaddr_va(ACCESS_FBINFO(video.vbase)), ACCESS_FBINFO(video.len));
1894
1895 /* We do not have to set currcon to 0... register_framebuffer do it for us on first console
1896  * and we do not want currcon == 0 for subsequent framebuffers */
1897
1898         ACCESS_FBINFO(fbcon).device = &ACCESS_FBINFO(pcidev)->dev;
1899         if (register_framebuffer(&ACCESS_FBINFO(fbcon)) < 0) {
1900                 goto failVideoIO;
1901         }
1902         printk("fb%d: %s frame buffer device\n",
1903                ACCESS_FBINFO(fbcon.node), ACCESS_FBINFO(fbcon.fix.id));
1904
1905         /* there is no console on this fb... but we have to initialize hardware
1906          * until someone tells me what is proper thing to do */
1907         if (!ACCESS_FBINFO(initialized)) {
1908                 printk(KERN_INFO "fb%d: initializing hardware\n",
1909                        ACCESS_FBINFO(fbcon.node));
1910                 /* We have to use FB_ACTIVATE_FORCE, as we had to put vesafb_defined to the fbcon.var
1911                  * already before, so register_framebuffer works correctly. */
1912                 vesafb_defined.activate |= FB_ACTIVATE_FORCE;
1913                 fb_set_var(&ACCESS_FBINFO(fbcon), &vesafb_defined);
1914         }
1915
1916         return 0;
1917 failVideoIO:;
1918         matroxfb_g450_shutdown(PMINFO2);
1919         mga_iounmap(ACCESS_FBINFO(video.vbase));
1920 failCtrlIO:;
1921         mga_iounmap(ACCESS_FBINFO(mmio.vbase));
1922 failVideoMR:;
1923         release_mem_region(video_base_phys, ACCESS_FBINFO(video.len_maximum));
1924 failCtrlMR:;
1925         release_mem_region(ctrlptr_phys, 16384);
1926 fail:;
1927         return err;
1928 }
1929
1930 static LIST_HEAD(matroxfb_list);
1931 static LIST_HEAD(matroxfb_driver_list);
1932
1933 #define matroxfb_l(x) list_entry(x, struct matrox_fb_info, next_fb)
1934 #define matroxfb_driver_l(x) list_entry(x, struct matroxfb_driver, node)
1935 int matroxfb_register_driver(struct matroxfb_driver* drv) {
1936         struct matrox_fb_info* minfo;
1937
1938         list_add(&drv->node, &matroxfb_driver_list);
1939         for (minfo = matroxfb_l(matroxfb_list.next);
1940              minfo != matroxfb_l(&matroxfb_list);
1941              minfo = matroxfb_l(minfo->next_fb.next)) {
1942                 void* p;
1943
1944                 if (minfo->drivers_count == MATROXFB_MAX_FB_DRIVERS)
1945                         continue;
1946                 p = drv->probe(minfo);
1947                 if (p) {
1948                         minfo->drivers_data[minfo->drivers_count] = p;
1949                         minfo->drivers[minfo->drivers_count++] = drv;
1950                 }
1951         }
1952         return 0;
1953 }
1954
1955 void matroxfb_unregister_driver(struct matroxfb_driver* drv) {
1956         struct matrox_fb_info* minfo;
1957
1958         list_del(&drv->node);
1959         for (minfo = matroxfb_l(matroxfb_list.next);
1960              minfo != matroxfb_l(&matroxfb_list);
1961              minfo = matroxfb_l(minfo->next_fb.next)) {
1962                 int i;
1963
1964                 for (i = 0; i < minfo->drivers_count; ) {
1965                         if (minfo->drivers[i] == drv) {
1966                                 if (drv && drv->remove)
1967                                         drv->remove(minfo, minfo->drivers_data[i]);
1968                                 minfo->drivers[i] = minfo->drivers[--minfo->drivers_count];
1969                                 minfo->drivers_data[i] = minfo->drivers_data[minfo->drivers_count];
1970                         } else
1971                                 i++;
1972                 }
1973         }
1974 }
1975
1976 static void matroxfb_register_device(struct matrox_fb_info* minfo) {
1977         struct matroxfb_driver* drv;
1978         int i = 0;
1979         list_add(&ACCESS_FBINFO(next_fb), &matroxfb_list);
1980         for (drv = matroxfb_driver_l(matroxfb_driver_list.next);
1981              drv != matroxfb_driver_l(&matroxfb_driver_list);
1982              drv = matroxfb_driver_l(drv->node.next)) {
1983                 if (drv && drv->probe) {
1984                         void *p = drv->probe(minfo);
1985                         if (p) {
1986                                 minfo->drivers_data[i] = p;
1987                                 minfo->drivers[i++] = drv;
1988                                 if (i == MATROXFB_MAX_FB_DRIVERS)
1989                                         break;
1990                         }
1991                 }
1992         }
1993         minfo->drivers_count = i;
1994 }
1995
1996 static void matroxfb_unregister_device(struct matrox_fb_info* minfo) {
1997         int i;
1998
1999         list_del(&ACCESS_FBINFO(next_fb));
2000         for (i = 0; i < minfo->drivers_count; i++) {
2001                 struct matroxfb_driver* drv = minfo->drivers[i];
2002
2003                 if (drv && drv->remove)
2004                         drv->remove(minfo, minfo->drivers_data[i]);
2005         }
2006 }
2007
2008 static int matroxfb_probe(struct pci_dev* pdev, const struct pci_device_id* dummy) {
2009         struct board* b;
2010         u_int16_t svid;
2011         u_int16_t sid;
2012         struct matrox_fb_info* minfo;
2013         int err;
2014         u_int32_t cmd;
2015 #ifndef CONFIG_FB_MATROX_MULTIHEAD
2016         static int registered = 0;
2017 #endif
2018         DBG(__func__)
2019
2020         svid = pdev->subsystem_vendor;
2021         sid = pdev->subsystem_device;
2022         for (b = dev_list; b->vendor; b++) {
2023                 if ((b->vendor != pdev->vendor) || (b->device != pdev->device) || (b->rev < pdev->revision)) continue;
2024                 if (b->svid)
2025                         if ((b->svid != svid) || (b->sid != sid)) continue;
2026                 break;
2027         }
2028         /* not match... */
2029         if (!b->vendor)
2030                 return -ENODEV;
2031         if (dev > 0) {
2032                 /* not requested one... */
2033                 dev--;
2034                 return -ENODEV;
2035         }
2036         pci_read_config_dword(pdev, PCI_COMMAND, &cmd);
2037         if (pci_enable_device(pdev)) {
2038                 return -1;
2039         }
2040
2041 #ifdef CONFIG_FB_MATROX_MULTIHEAD
2042         minfo = kmalloc(sizeof(*minfo), GFP_KERNEL);
2043         if (!minfo)
2044                 return -1;
2045 #else
2046         if (registered) /* singlehead driver... */
2047                 return -1;
2048         minfo = &matroxfb_global_mxinfo;
2049 #endif
2050         memset(MINFO, 0, sizeof(*MINFO));
2051
2052         ACCESS_FBINFO(pcidev) = pdev;
2053         ACCESS_FBINFO(dead) = 0;
2054         ACCESS_FBINFO(usecount) = 0;
2055         ACCESS_FBINFO(userusecount) = 0;
2056
2057         pci_set_drvdata(pdev, MINFO);
2058         /* DEVFLAGS */
2059         ACCESS_FBINFO(devflags.memtype) = memtype;
2060         if (memtype != -1)
2061                 noinit = 0;
2062         if (cmd & PCI_COMMAND_MEMORY) {
2063                 ACCESS_FBINFO(devflags.novga) = novga;
2064                 ACCESS_FBINFO(devflags.nobios) = nobios;
2065                 ACCESS_FBINFO(devflags.noinit) = noinit;
2066                 /* subsequent heads always needs initialization and must not enable BIOS */
2067                 novga = 1;
2068                 nobios = 1;
2069                 noinit = 0;
2070         } else {
2071                 ACCESS_FBINFO(devflags.novga) = 1;
2072                 ACCESS_FBINFO(devflags.nobios) = 1;
2073                 ACCESS_FBINFO(devflags.noinit) = 0;
2074         }
2075
2076         ACCESS_FBINFO(devflags.nopciretry) = no_pci_retry;
2077         ACCESS_FBINFO(devflags.mga_24bpp_fix) = inv24;
2078         ACCESS_FBINFO(devflags.precise_width) = option_precise_width;
2079         ACCESS_FBINFO(devflags.sgram) = sgram;
2080         ACCESS_FBINFO(capable.cross4MB) = cross4MB;
2081
2082         spin_lock_init(&ACCESS_FBINFO(lock.DAC));
2083         spin_lock_init(&ACCESS_FBINFO(lock.accel));
2084         init_rwsem(&ACCESS_FBINFO(crtc2.lock));
2085         init_rwsem(&ACCESS_FBINFO(altout.lock));
2086         mutex_init(&ACCESS_FBINFO(fbcon).lock);
2087         mutex_init(&ACCESS_FBINFO(fbcon).mm_lock);
2088         ACCESS_FBINFO(irq_flags) = 0;
2089         init_waitqueue_head(&ACCESS_FBINFO(crtc1.vsync.wait));
2090         init_waitqueue_head(&ACCESS_FBINFO(crtc2.vsync.wait));
2091         ACCESS_FBINFO(crtc1.panpos) = -1;
2092
2093         err = initMatrox2(PMINFO b);
2094         if (!err) {
2095 #ifndef CONFIG_FB_MATROX_MULTIHEAD
2096                 registered = 1;
2097 #endif
2098                 matroxfb_register_device(MINFO);
2099                 return 0;
2100         }
2101 #ifdef CONFIG_FB_MATROX_MULTIHEAD
2102         kfree(minfo);
2103 #endif
2104         return -1;
2105 }
2106
2107 static void pci_remove_matrox(struct pci_dev* pdev) {
2108         struct matrox_fb_info* minfo;
2109
2110         minfo = pci_get_drvdata(pdev);
2111         matroxfb_remove(PMINFO 1);
2112 }
2113
2114 static struct pci_device_id matroxfb_devices[] = {
2115 #ifdef CONFIG_FB_MATROX_MILLENIUM
2116         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_MIL,
2117                 PCI_ANY_ID,     PCI_ANY_ID,     0, 0, 0},
2118         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_MIL_2,
2119                 PCI_ANY_ID,     PCI_ANY_ID,     0, 0, 0},
2120         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_MIL_2_AGP,
2121                 PCI_ANY_ID,     PCI_ANY_ID,     0, 0, 0},
2122 #endif
2123 #ifdef CONFIG_FB_MATROX_MYSTIQUE
2124         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_MYS,
2125                 PCI_ANY_ID,     PCI_ANY_ID,     0, 0, 0},
2126 #endif
2127 #ifdef CONFIG_FB_MATROX_G
2128         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G100_MM,
2129                 PCI_ANY_ID,     PCI_ANY_ID,     0, 0, 0},
2130         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G100_AGP,
2131                 PCI_ANY_ID,     PCI_ANY_ID,     0, 0, 0},
2132         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G200EV_PCI,
2133                 PCI_ANY_ID,     PCI_ANY_ID,     0, 0, 0},
2134         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G200_PCI,
2135                 PCI_ANY_ID,     PCI_ANY_ID,     0, 0, 0},
2136         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G200_AGP,
2137                 PCI_ANY_ID,     PCI_ANY_ID,     0, 0, 0},
2138         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G400,
2139                 PCI_ANY_ID,     PCI_ANY_ID,     0, 0, 0},
2140         {PCI_VENDOR_ID_MATROX,  PCI_DEVICE_ID_MATROX_G550,
2141                 PCI_ANY_ID,     PCI_ANY_ID,     0, 0, 0},
2142 #endif
2143         {0,                     0,
2144                 0,              0,              0, 0, 0}
2145 };
2146
2147 MODULE_DEVICE_TABLE(pci, matroxfb_devices);
2148
2149
2150 static struct pci_driver matroxfb_driver = {
2151         .name =         "matroxfb",
2152         .id_table =     matroxfb_devices,
2153         .probe =        matroxfb_probe,
2154         .remove =       pci_remove_matrox,
2155 };
2156
2157 /* **************************** init-time only **************************** */
2158
2159 #define RSResolution(X) ((X) & 0x0F)
2160 #define RS640x400       1
2161 #define RS640x480       2
2162 #define RS800x600       3
2163 #define RS1024x768      4
2164 #define RS1280x1024     5
2165 #define RS1600x1200     6
2166 #define RS768x576       7
2167 #define RS960x720       8
2168 #define RS1152x864      9
2169 #define RS1408x1056     10
2170 #define RS640x350       11
2171 #define RS1056x344      12      /* 132 x 43 text */
2172 #define RS1056x400      13      /* 132 x 50 text */
2173 #define RS1056x480      14      /* 132 x 60 text */
2174 #define RSNoxNo         15
2175 /* 10-FF */
2176 static struct { int xres, yres, left, right, upper, lower, hslen, vslen, vfreq; } timmings[] __initdata = {
2177         {  640,  400,  48, 16, 39,  8,  96, 2, 70 },
2178         {  640,  480,  48, 16, 33, 10,  96, 2, 60 },
2179         {  800,  600, 144, 24, 28,  8, 112, 6, 60 },
2180         { 1024,  768, 160, 32, 30,  4, 128, 4, 60 },
2181         { 1280, 1024, 224, 32, 32,  4, 136, 4, 60 },
2182         { 1600, 1200, 272, 48, 32,  5, 152, 5, 60 },
2183         {  768,  576, 144, 16, 28,  6, 112, 4, 60 },
2184         {  960,  720, 144, 24, 28,  8, 112, 4, 60 },
2185         { 1152,  864, 192, 32, 30,  4, 128, 4, 60 },
2186         { 1408, 1056, 256, 40, 32,  5, 144, 5, 60 },
2187         {  640,  350,  48, 16, 39,  8,  96, 2, 70 },
2188         { 1056,  344,  96, 24, 59, 44, 160, 2, 70 },
2189         { 1056,  400,  96, 24, 39,  8, 160, 2, 70 },
2190         { 1056,  480,  96, 24, 36, 12, 160, 3, 60 },
2191         {    0,    0,  ~0, ~0, ~0, ~0,   0, 0,  0 }
2192 };
2193
2194 #define RSCreate(X,Y)   ((X) | ((Y) << 8))
2195 static struct { unsigned int vesa; unsigned int info; } *RSptr, vesamap[] __initdata = {
2196 /* default must be first */
2197         {    ~0, RSCreate(RSNoxNo,     RS8bpp ) },
2198         { 0x101, RSCreate(RS640x480,   RS8bpp ) },
2199         { 0x100, RSCreate(RS640x400,   RS8bpp ) },
2200         { 0x180, RSCreate(RS768x576,   RS8bpp ) },
2201         { 0x103, RSCreate(RS800x600,   RS8bpp ) },
2202         { 0x188, RSCreate(RS960x720,   RS8bpp ) },
2203         { 0x105, RSCreate(RS1024x768,  RS8bpp ) },
2204         { 0x190, RSCreate(RS1152x864,  RS8bpp ) },
2205         { 0x107, RSCreate(RS1280x1024, RS8bpp ) },
2206         { 0x198, RSCreate(RS1408x1056, RS8bpp ) },
2207         { 0x11C, RSCreate(RS1600x1200, RS8bpp ) },
2208         { 0x110, RSCreate(RS640x480,   RS15bpp) },
2209         { 0x181, RSCreate(RS768x576,   RS15bpp) },
2210         { 0x113, RSCreate(RS800x600,   RS15bpp) },
2211         { 0x189, RSCreate(RS960x720,   RS15bpp) },
2212         { 0x116, RSCreate(RS1024x768,  RS15bpp) },
2213         { 0x191, RSCreate(RS1152x864,  RS15bpp) },
2214         { 0x119, RSCreate(RS1280x1024, RS15bpp) },
2215         { 0x199, RSCreate(RS1408x1056, RS15bpp) },
2216         { 0x11D, RSCreate(RS1600x1200, RS15bpp) },
2217         { 0x111, RSCreate(RS640x480,   RS16bpp) },
2218         { 0x182, RSCreate(RS768x576,   RS16bpp) },
2219         { 0x114, RSCreate(RS800x600,   RS16bpp) },
2220         { 0x18A, RSCreate(RS960x720,   RS16bpp) },
2221         { 0x117, RSCreate(RS1024x768,  RS16bpp) },
2222         { 0x192, RSCreate(RS1152x864,  RS16bpp) },
2223         { 0x11A, RSCreate(RS1280x1024, RS16bpp) },
2224         { 0x19A, RSCreate(RS1408x1056, RS16bpp) },
2225         { 0x11E, RSCreate(RS1600x1200, RS16bpp) },
2226         { 0x1B2, RSCreate(RS640x480,   RS24bpp) },
2227         { 0x184, RSCreate(RS768x576,   RS24bpp) },
2228         { 0x1B5, RSCreate(RS800x600,   RS24bpp) },
2229         { 0x18C, RSCreate(RS960x720,   RS24bpp) },
2230         { 0x1B8, RSCreate(RS1024x768,  RS24bpp) },
2231         { 0x194, RSCreate(RS1152x864,  RS24bpp) },
2232         { 0x1BB, RSCreate(RS1280x1024, RS24bpp) },
2233         { 0x19C, RSCreate(RS1408x1056, RS24bpp) },
2234         { 0x1BF, RSCreate(RS1600x1200, RS24bpp) },
2235         { 0x112, RSCreate(RS640x480,   RS32bpp) },
2236         { 0x183, RSCreate(RS768x576,   RS32bpp) },
2237         { 0x115, RSCreate(RS800x600,   RS32bpp) },
2238         { 0x18B, RSCreate(RS960x720,   RS32bpp) },
2239         { 0x118, RSCreate(RS1024x768,  RS32bpp) },
2240         { 0x193, RSCreate(RS1152x864,  RS32bpp) },
2241         { 0x11B, RSCreate(RS1280x1024, RS32bpp) },
2242         { 0x19B, RSCreate(RS1408x1056, RS32bpp) },
2243         { 0x11F, RSCreate(RS1600x1200, RS32bpp) },
2244         { 0x010, RSCreate(RS640x350,   RS4bpp ) },
2245         { 0x012, RSCreate(RS640x480,   RS4bpp ) },
2246         { 0x102, RSCreate(RS800x600,   RS4bpp ) },
2247         { 0x104, RSCreate(RS1024x768,  RS4bpp ) },
2248         { 0x106, RSCreate(RS1280x1024, RS4bpp ) },
2249         {     0, 0                              }};
2250
2251 static void __init matroxfb_init_params(void) {
2252         /* fh from kHz to Hz */
2253         if (fh < 1000)
2254                 fh *= 1000;     /* 1kHz minimum */
2255         /* maxclk */
2256         if (maxclk < 1000) maxclk *= 1000;      /* kHz -> Hz, MHz -> kHz */
2257         if (maxclk < 1000000) maxclk *= 1000;   /* kHz -> Hz, 1MHz minimum */
2258         /* fix VESA number */
2259         if (vesa != ~0)
2260                 vesa &= 0x1DFF;         /* mask out clearscreen, acceleration and so on */
2261
2262         /* static settings */
2263         for (RSptr = vesamap; RSptr->vesa; RSptr++) {
2264                 if (RSptr->vesa == vesa) break;
2265         }
2266         if (!RSptr->vesa) {
2267                 printk(KERN_ERR "Invalid vesa mode 0x%04X\n", vesa);
2268                 RSptr = vesamap;
2269         }
2270         {
2271                 int res = RSResolution(RSptr->info)-1;
2272                 if (left == ~0)
2273                         left = timmings[res].left;
2274                 if (!xres)
2275                         xres = timmings[res].xres;
2276                 if (right == ~0)
2277                         right = timmings[res].right;
2278                 if (!hslen)
2279                         hslen = timmings[res].hslen;
2280                 if (upper == ~0)
2281                         upper = timmings[res].upper;
2282                 if (!yres)
2283                         yres = timmings[res].yres;
2284                 if (lower == ~0)
2285                         lower = timmings[res].lower;
2286                 if (!vslen)
2287                         vslen = timmings[res].vslen;
2288                 if (!(fv||fh||maxclk||pixclock))
2289                         fv = timmings[res].vfreq;
2290                 if (depth == -1)
2291                         depth = RSDepth(RSptr->info);
2292         }
2293 }
2294
2295 static int __init matrox_init(void) {
2296         int err;
2297
2298         matroxfb_init_params();
2299         err = pci_register_driver(&matroxfb_driver);
2300         dev = -1;       /* accept all new devices... */
2301         return err;
2302 }
2303
2304 /* **************************** exit-time only **************************** */
2305
2306 static void __exit matrox_done(void) {
2307         pci_unregister_driver(&matroxfb_driver);
2308 }
2309
2310 #ifndef MODULE
2311
2312 /* ************************* init in-kernel code ************************** */
2313
2314 static int __init matroxfb_setup(char *options) {
2315         char *this_opt;
2316
2317         DBG(__func__)
2318
2319         if (!options || !*options)
2320                 return 0;
2321
2322         while ((this_opt = strsep(&options, ",")) != NULL) {
2323                 if (!*this_opt) continue;
2324
2325                 dprintk("matroxfb_setup: option %s\n", this_opt);
2326
2327                 if (!strncmp(this_opt, "dev:", 4))
2328                         dev = simple_strtoul(this_opt+4, NULL, 0);
2329                 else if (!strncmp(this_opt, "depth:", 6)) {
2330                         switch (simple_strtoul(this_opt+6, NULL, 0)) {
2331                                 case 0: depth = RSText; break;
2332                                 case 4: depth = RS4bpp; break;
2333                                 case 8: depth = RS8bpp; break;
2334                                 case 15:depth = RS15bpp; break;
2335                                 case 16:depth = RS16bpp; break;
2336                                 case 24:depth = RS24bpp; break;
2337                                 case 32:depth = RS32bpp; break;
2338                                 default:
2339                                         printk(KERN_ERR "matroxfb: unsupported color depth\n");
2340                         }
2341                 } else if (!strncmp(this_opt, "xres:", 5))
2342                         xres = simple_strtoul(this_opt+5, NULL, 0);
2343                 else if (!strncmp(this_opt, "yres:", 5))
2344                         yres = simple_strtoul(this_opt+5, NULL, 0);
2345                 else if (!strncmp(this_opt, "vslen:", 6))
2346                         vslen = simple_strtoul(this_opt+6, NULL, 0);
2347                 else if (!strncmp(this_opt, "hslen:", 6))
2348                         hslen = simple_strtoul(this_opt+6, NULL, 0);
2349                 else if (!strncmp(this_opt, "left:", 5))
2350                         left = simple_strtoul(this_opt+5, NULL, 0);
2351                 else if (!strncmp(this_opt, "right:", 6))
2352                         right = simple_strtoul(this_opt+6, NULL, 0);
2353                 else if (!strncmp(this_opt, "upper:", 6))
2354                         upper = simple_strtoul(this_opt+6, NULL, 0);
2355                 else if (!strncmp(this_opt, "lower:", 6))
2356                         lower = simple_strtoul(this_opt+6, NULL, 0);
2357                 else if (!strncmp(this_opt, "pixclock:", 9))
2358                         pixclock = simple_strtoul(this_opt+9, NULL, 0);
2359                 else if (!strncmp(this_opt, "sync:", 5))
2360                         sync = simple_strtoul(this_opt+5, NULL, 0);
2361                 else if (!strncmp(this_opt, "vesa:", 5))
2362                         vesa = simple_strtoul(this_opt+5, NULL, 0);
2363                 else if (!strncmp(this_opt, "maxclk:", 7))
2364                         maxclk = simple_strtoul(this_opt+7, NULL, 0);
2365                 else if (!strncmp(this_opt, "fh:", 3))
2366                         fh = simple_strtoul(this_opt+3, NULL, 0);
2367                 else if (!strncmp(this_opt, "fv:", 3))
2368                         fv = simple_strtoul(this_opt+3, NULL, 0);
2369                 else if (!strncmp(this_opt, "mem:", 4))
2370                         mem = simple_strtoul(this_opt+4, NULL, 0);
2371                 else if (!strncmp(this_opt, "mode:", 5))
2372                         strlcpy(videomode, this_opt+5, sizeof(videomode));
2373                 else if (!strncmp(this_opt, "outputs:", 8))
2374                         strlcpy(outputs, this_opt+8, sizeof(outputs));
2375                 else if (!strncmp(this_opt, "dfp:", 4)) {
2376                         dfp_type = simple_strtoul(this_opt+4, NULL, 0);
2377                         dfp = 1;
2378                 }
2379 #ifdef CONFIG_PPC_PMAC
2380                 else if (!strncmp(this_opt, "vmode:", 6)) {
2381                         unsigned int vmode = simple_strtoul(this_opt+6, NULL, 0);
2382                         if (vmode > 0 && vmode <= VMODE_MAX)
2383                                 default_vmode = vmode;
2384                 } else if (!strncmp(this_opt, "cmode:", 6)) {
2385                         unsigned int cmode = simple_strtoul(this_opt+6, NULL, 0);
2386                         switch (cmode) {
2387                                 case 0:
2388                                 case 8:
2389                                         default_cmode = CMODE_8;
2390                                         break;
2391                                 case 15:
2392                                 case 16:
2393                                         default_cmode = CMODE_16;
2394                                         break;
2395                                 case 24:
2396                                 case 32:
2397                                         default_cmode = CMODE_32;
2398                                         break;
2399                         }
2400                 }
2401 #endif
2402                 else if (!strcmp(this_opt, "disabled")) /* nodisabled does not exist */
2403                         disabled = 1;
2404                 else if (!strcmp(this_opt, "enabled"))  /* noenabled does not exist */
2405                         disabled = 0;
2406                 else if (!strcmp(this_opt, "sgram"))    /* nosgram == sdram */
2407                         sgram = 1;
2408                 else if (!strcmp(this_opt, "sdram"))
2409                         sgram = 0;
2410                 else if (!strncmp(this_opt, "memtype:", 8))
2411                         memtype = simple_strtoul(this_opt+8, NULL, 0);
2412                 else {
2413                         int value = 1;
2414
2415                         if (!strncmp(this_opt, "no", 2)) {
2416                                 value = 0;
2417                                 this_opt += 2;
2418                         }
2419                         if (! strcmp(this_opt, "inverse"))
2420                                 inverse = value;
2421                         else if (!strcmp(this_opt, "accel"))
2422                                 noaccel = !value;
2423                         else if (!strcmp(this_opt, "pan"))
2424                                 nopan = !value;
2425                         else if (!strcmp(this_opt, "pciretry"))
2426                                 no_pci_retry = !value;
2427                         else if (!strcmp(this_opt, "vga"))
2428                                 novga = !value;
2429                         else if (!strcmp(this_opt, "bios"))
2430                                 nobios = !value;
2431                         else if (!strcmp(this_opt, "init"))
2432                                 noinit = !value;
2433 #ifdef CONFIG_MTRR
2434                         else if (!strcmp(this_opt, "mtrr"))
2435                                 mtrr = value;
2436 #endif
2437                         else if (!strcmp(this_opt, "inv24"))
2438                                 inv24 = value;
2439                         else if (!strcmp(this_opt, "cross4MB"))
2440                                 cross4MB = value;
2441                         else if (!strcmp(this_opt, "grayscale"))
2442                                 grayscale = value;
2443                         else if (!strcmp(this_opt, "dfp"))
2444                                 dfp = value;
2445                         else {
2446                                 strlcpy(videomode, this_opt, sizeof(videomode));
2447                         }
2448                 }
2449         }
2450         return 0;
2451 }
2452
2453 static int __initdata initialized = 0;
2454
2455 static int __init matroxfb_init(void)
2456 {
2457         char *option = NULL;
2458         int err = 0;
2459
2460         DBG(__func__)
2461
2462         if (fb_get_options("matroxfb", &option))
2463                 return -ENODEV;
2464         matroxfb_setup(option);
2465
2466         if (disabled)
2467                 return -ENXIO;
2468         if (!initialized) {
2469                 initialized = 1;
2470                 err = matrox_init();
2471         }
2472         hotplug = 1;
2473         /* never return failure, user can hotplug matrox later... */
2474         return err;
2475 }
2476
2477 module_init(matroxfb_init);
2478
2479 #else
2480
2481 /* *************************** init module code **************************** */
2482
2483 MODULE_AUTHOR("(c) 1998-2002 Petr Vandrovec <vandrove@vc.cvut.cz>");
2484 MODULE_DESCRIPTION("Accelerated FBDev driver for Matrox Millennium/Mystique/G100/G200/G400/G450/G550");
2485 MODULE_LICENSE("GPL");
2486
2487 module_param(mem, int, 0);
2488 MODULE_PARM_DESC(mem, "Size of available memory in MB, KB or B (2,4,8,12,16MB, default=autodetect)");
2489 module_param(disabled, int, 0);
2490 MODULE_PARM_DESC(disabled, "Disabled (0 or 1=disabled) (default=0)");
2491 module_param(noaccel, int, 0);
2492 MODULE_PARM_DESC(noaccel, "Do not use accelerating engine (0 or 1=disabled) (default=0)");
2493 module_param(nopan, int, 0);
2494 MODULE_PARM_DESC(nopan, "Disable pan on startup (0 or 1=disabled) (default=0)");
2495 module_param(no_pci_retry, int, 0);
2496 MODULE_PARM_DESC(no_pci_retry, "PCI retries enabled (0 or 1=disabled) (default=0)");
2497 module_param(novga, int, 0);
2498 MODULE_PARM_DESC(novga, "VGA I/O (0x3C0-0x3DF) disabled (0 or 1=disabled) (default=0)");
2499 module_param(nobios, int, 0);
2500 MODULE_PARM_DESC(nobios, "Disables ROM BIOS (0 or 1=disabled) (default=do not change BIOS state)");
2501 module_param(noinit, int, 0);
2502 MODULE_PARM_DESC(noinit, "Disables W/SG/SD-RAM and bus interface initialization (0 or 1=do not initialize) (default=0)");
2503 module_param(memtype, int, 0);
2504 MODULE_PARM_DESC(memtype, "Memory type for G200/G400 (see Documentation/fb/matroxfb.txt for explanation) (default=3 for G200, 0 for G400)");
2505 #ifdef CONFIG_MTRR
2506 module_param(mtrr, int, 0);
2507 MODULE_PARM_DESC(mtrr, "This speeds up video memory accesses (0=disabled or 1) (default=1)");
2508 #endif
2509 module_param(sgram, int, 0);
2510 MODULE_PARM_DESC(sgram, "Indicates that G100/G200/G400 has SGRAM memory (0=SDRAM, 1=SGRAM) (default=0)");
2511 module_param(inv24, int, 0);
2512 MODULE_PARM_DESC(inv24, "Inverts clock polarity for 24bpp and loop frequency > 100MHz (default=do not invert polarity)");
2513 module_param(inverse, int, 0);
2514 MODULE_PARM_DESC(inverse, "Inverse (0 or 1) (default=0)");
2515 #ifdef CONFIG_FB_MATROX_MULTIHEAD
2516 module_param(dev, int, 0);
2517 MODULE_PARM_DESC(dev, "Multihead support, attach to device ID (0..N) (default=all working)");
2518 #else
2519 module_param(dev, int, 0);
2520 MODULE_PARM_DESC(dev, "Multihead support, attach to device ID (0..N) (default=first working)");
2521 #endif
2522 module_param(vesa, int, 0);
2523 MODULE_PARM_DESC(vesa, "Startup videomode (0x000-0x1FF) (default=0x101)");
2524 module_param(xres, int, 0);
2525 MODULE_PARM_DESC(xres, "Horizontal resolution (px), overrides xres from vesa (default=vesa)");
2526 module_param(yres, int, 0);
2527 MODULE_PARM_DESC(yres, "Vertical resolution (scans), overrides yres from vesa (default=vesa)");
2528 module_param(upper, int, 0);
2529 MODULE_PARM_DESC(upper, "Upper blank space (scans), overrides upper from vesa (default=vesa)");
2530 module_param(lower, int, 0);
2531 MODULE_PARM_DESC(lower, "Lower blank space (scans), overrides lower from vesa (default=vesa)");
2532 module_param(vslen, int, 0);
2533 MODULE_PARM_DESC(vslen, "Vertical sync length (scans), overrides lower from vesa (default=vesa)");
2534 module_param(left, int, 0);
2535 MODULE_PARM_DESC(left, "Left blank space (px), overrides left from vesa (default=vesa)");
2536 module_param(right, int, 0);
2537 MODULE_PARM_DESC(right, "Right blank space (px), overrides right from vesa (default=vesa)");
2538 module_param(hslen, int, 0);
2539 MODULE_PARM_DESC(hslen, "Horizontal sync length (px), overrides hslen from vesa (default=vesa)");
2540 module_param(pixclock, int, 0);
2541 MODULE_PARM_DESC(pixclock, "Pixelclock (ns), overrides pixclock from vesa (default=vesa)");
2542 module_param(sync, int, 0);
2543 MODULE_PARM_DESC(sync, "Sync polarity, overrides sync from vesa (default=vesa)");
2544 module_param(depth, int, 0);
2545 MODULE_PARM_DESC(depth, "Color depth (0=text,8,15,16,24,32) (default=vesa)");
2546 module_param(maxclk, int, 0);
2547 MODULE_PARM_DESC(maxclk, "Startup maximal clock, 0-999MHz, 1000-999999kHz, 1000000-INF Hz");
2548 module_param(fh, int, 0);
2549 MODULE_PARM_DESC(fh, "Startup horizontal frequency, 0-999kHz, 1000-INF Hz");
2550 module_param(fv, int, 0);
2551 MODULE_PARM_DESC(fv, "Startup vertical frequency, 0-INF Hz\n"
2552 "You should specify \"fv:max_monitor_vsync,fh:max_monitor_hsync,maxclk:max_monitor_dotclock\"");
2553 module_param(grayscale, int, 0);
2554 MODULE_PARM_DESC(grayscale, "Sets display into grayscale. Works perfectly with paletized videomode (4, 8bpp), some limitations apply to 16, 24 and 32bpp videomodes (default=nograyscale)");
2555 module_param(cross4MB, int, 0);
2556 MODULE_PARM_DESC(cross4MB, "Specifies that 4MB boundary can be in middle of line. (default=autodetected)");
2557 module_param(dfp, int, 0);
2558 MODULE_PARM_DESC(dfp, "Specifies whether to use digital flat panel interface of G200/G400 (0 or 1) (default=0)");
2559 module_param(dfp_type, int, 0);
2560 MODULE_PARM_DESC(dfp_type, "Specifies DFP interface type (0 to 255) (default=read from hardware)");
2561 module_param_string(outputs, outputs, sizeof(outputs), 0);
2562 MODULE_PARM_DESC(outputs, "Specifies which CRTC is mapped to which output (string of up to three letters, consisting of 0 (disabled), 1 (CRTC1), 2 (CRTC2)) (default=111 for Gx50, 101 for G200/G400 with DFP, and 100 for all other devices)");
2563 #ifdef CONFIG_PPC_PMAC
2564 module_param_named(vmode, default_vmode, int, 0);
2565 MODULE_PARM_DESC(vmode, "Specify the vmode mode number that should be used (640x480 default)");
2566 module_param_named(cmode, default_cmode, int, 0);
2567 MODULE_PARM_DESC(cmode, "Specify the video depth that should be used (8bit default)");
2568 #endif
2569
2570 int __init init_module(void){
2571
2572         DBG(__func__)
2573
2574         if (disabled)
2575                 return -ENXIO;
2576
2577         if (depth == 0)
2578                 depth = RSText;
2579         else if (depth == 4)
2580                 depth = RS4bpp;
2581         else if (depth == 8)
2582                 depth = RS8bpp;
2583         else if (depth == 15)
2584                 depth = RS15bpp;
2585         else if (depth == 16)
2586                 depth = RS16bpp;
2587         else if (depth == 24)
2588                 depth = RS24bpp;
2589         else if (depth == 32)
2590                 depth = RS32bpp;
2591         else if (depth != -1) {
2592                 printk(KERN_ERR "matroxfb: depth %d is not supported, using default\n", depth);
2593                 depth = -1;
2594         }
2595         matrox_init();
2596         /* never return failure; user can hotplug matrox later... */
2597         return 0;
2598 }
2599 #endif  /* MODULE */
2600
2601 module_exit(matrox_done);
2602 EXPORT_SYMBOL(matroxfb_register_driver);
2603 EXPORT_SYMBOL(matroxfb_unregister_driver);
2604 EXPORT_SYMBOL(matroxfb_wait_for_sync);
2605 EXPORT_SYMBOL(matroxfb_enable_irq);
2606
2607 /*
2608  * Overrides for Emacs so that we follow Linus's tabbing style.
2609  * ---------------------------------------------------------------------------
2610  * Local variables:
2611  * c-basic-offset: 8
2612  * End:
2613  */
2614