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