Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *
0004  * Hardware accelerated Matrox Millennium I, II, Mystique, G100, G200, G400 and G450.
0005  *
0006  * (c) 1998-2002 Petr Vandrovec <vandrove@vc.cvut.cz>
0007  *
0008  * Portions Copyright (c) 2001 Matrox Graphics Inc.
0009  *
0010  * Version: 1.65 2002/08/14
0011  *
0012  */
0013 
0014 #include "matroxfb_maven.h"
0015 #include "matroxfb_crtc2.h"
0016 #include "matroxfb_misc.h"
0017 #include "matroxfb_DAC1064.h"
0018 #include <linux/matroxfb.h>
0019 #include <linux/slab.h>
0020 #include <linux/uaccess.h>
0021 
0022 /* **************************************************** */
0023 
0024 static int mem = 8192;
0025 
0026 module_param(mem, int, 0);
0027 MODULE_PARM_DESC(mem, "Memory size reserved for dualhead (default=8MB)");
0028 
0029 /* **************************************************** */
0030 
0031 static int matroxfb_dh_setcolreg(unsigned regno, unsigned red, unsigned green,
0032         unsigned blue, unsigned transp, struct fb_info* info) {
0033     u_int32_t col;
0034 #define m2info (container_of(info, struct matroxfb_dh_fb_info, fbcon))
0035 
0036     if (regno >= 16)
0037         return 1;
0038     if (m2info->fbcon.var.grayscale) {
0039         /* gray = 0.30*R + 0.59*G + 0.11*B */
0040         red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
0041     }
0042     red = CNVT_TOHW(red, m2info->fbcon.var.red.length);
0043     green = CNVT_TOHW(green, m2info->fbcon.var.green.length);
0044     blue = CNVT_TOHW(blue, m2info->fbcon.var.blue.length);
0045     transp = CNVT_TOHW(transp, m2info->fbcon.var.transp.length);
0046 
0047     col = (red << m2info->fbcon.var.red.offset)     |
0048           (green << m2info->fbcon.var.green.offset) |
0049           (blue << m2info->fbcon.var.blue.offset)   |
0050           (transp << m2info->fbcon.var.transp.offset);
0051 
0052     switch (m2info->fbcon.var.bits_per_pixel) {
0053         case 16:
0054             m2info->cmap[regno] = col | (col << 16);
0055             break;
0056         case 32:
0057             m2info->cmap[regno] = col;
0058             break;
0059     }
0060     return 0;
0061 #undef m2info
0062 }
0063 
0064 static void matroxfb_dh_restore(struct matroxfb_dh_fb_info* m2info,
0065         struct my_timming* mt,
0066         int mode,
0067         unsigned int pos) {
0068     u_int32_t tmp;
0069     u_int32_t datactl;
0070     struct matrox_fb_info *minfo = m2info->primary_dev;
0071 
0072     switch (mode) {
0073         case 15:
0074             tmp = 0x00200000;
0075             break;
0076         case 16:
0077             tmp = 0x00400000;
0078             break;
0079 /*      case 32: */
0080         default:
0081             tmp = 0x00800000;
0082             break;
0083     }
0084     tmp |= 0x00000001;  /* enable CRTC2 */
0085     datactl = 0;
0086     if (minfo->outputs[1].src == MATROXFB_SRC_CRTC2) {
0087         if (minfo->devflags.g450dac) {
0088             tmp |= 0x00000006; /* source from secondary pixel PLL */
0089             /* no vidrst when in monitor mode */
0090             if (minfo->outputs[1].mode != MATROXFB_OUTPUT_MODE_MONITOR) {
0091                 tmp |=  0xC0001000; /* Enable H/V vidrst */
0092             }
0093         } else {
0094             tmp |= 0x00000002; /* source from VDOCLK */
0095             tmp |= 0xC0000000; /* enable vvidrst & hvidrst */
0096             /* MGA TVO is our clock source */
0097         }
0098     } else if (minfo->outputs[0].src == MATROXFB_SRC_CRTC2) {
0099         tmp |= 0x00000004; /* source from pixclock */
0100         /* PIXPLL is our clock source */
0101     }
0102     if (minfo->outputs[0].src == MATROXFB_SRC_CRTC2) {
0103         tmp |= 0x00100000;  /* connect CRTC2 to DAC */
0104     }
0105     if (mt->interlaced) {
0106         tmp |= 0x02000000;  /* interlaced, second field is bigger, as G450 apparently ignores it */
0107         mt->VDisplay >>= 1;
0108         mt->VSyncStart >>= 1;
0109         mt->VSyncEnd >>= 1;
0110         mt->VTotal >>= 1;
0111     }
0112     if ((mt->HTotal & 7) == 2) {
0113         datactl |= 0x00000010;
0114         mt->HTotal &= ~7;
0115     }
0116     tmp |= 0x10000000;  /* 0x10000000 is VIDRST polarity */
0117     mga_outl(0x3C14, ((mt->HDisplay - 8) << 16) | (mt->HTotal - 8));
0118     mga_outl(0x3C18, ((mt->HSyncEnd - 8) << 16) | (mt->HSyncStart - 8));
0119     mga_outl(0x3C1C, ((mt->VDisplay - 1) << 16) | (mt->VTotal - 1));
0120     mga_outl(0x3C20, ((mt->VSyncEnd - 1) << 16) | (mt->VSyncStart - 1));
0121     mga_outl(0x3C24, ((mt->VSyncStart) << 16) | (mt->HSyncStart));  /* preload */
0122     {
0123         u_int32_t linelen = m2info->fbcon.var.xres_virtual * (m2info->fbcon.var.bits_per_pixel >> 3);
0124         if (tmp & 0x02000000) {
0125             /* field #0 is smaller, so... */
0126             mga_outl(0x3C2C, pos);          /* field #1 vmemory start */
0127             mga_outl(0x3C28, pos + linelen);    /* field #0 vmemory start */
0128             linelen <<= 1;
0129             m2info->interlaced = 1;
0130         } else {
0131             mga_outl(0x3C28, pos);      /* vmemory start */
0132             m2info->interlaced = 0;
0133         }
0134         mga_outl(0x3C40, linelen);
0135     }
0136     mga_outl(0x3C4C, datactl);  /* data control */
0137     if (tmp & 0x02000000) {
0138         int i;
0139 
0140         mga_outl(0x3C10, tmp & ~0x02000000);
0141         for (i = 0; i < 2; i++) {
0142             unsigned int nl;
0143             unsigned int lastl = 0;
0144 
0145             while ((nl = mga_inl(0x3C48) & 0xFFF) >= lastl) {
0146                 lastl = nl;
0147             }
0148         }
0149     }
0150     mga_outl(0x3C10, tmp);
0151     minfo->hw.crtc2.ctl = tmp;
0152 
0153     tmp = mt->VDisplay << 16;   /* line compare */
0154     if (mt->sync & FB_SYNC_HOR_HIGH_ACT)
0155         tmp |= 0x00000100;
0156     if (mt->sync & FB_SYNC_VERT_HIGH_ACT)
0157         tmp |= 0x00000200;
0158     mga_outl(0x3C44, tmp);
0159 }
0160 
0161 static void matroxfb_dh_disable(struct matroxfb_dh_fb_info* m2info) {
0162     struct matrox_fb_info *minfo = m2info->primary_dev;
0163 
0164     mga_outl(0x3C10, 0x00000004);   /* disable CRTC2, CRTC1->DAC1, PLL as clock source */
0165     minfo->hw.crtc2.ctl = 0x00000004;
0166 }
0167 
0168 static void matroxfb_dh_pan_var(struct matroxfb_dh_fb_info* m2info,
0169         struct fb_var_screeninfo* var) {
0170     unsigned int pos;
0171     unsigned int linelen;
0172     unsigned int pixelsize;
0173     struct matrox_fb_info *minfo = m2info->primary_dev;
0174 
0175     m2info->fbcon.var.xoffset = var->xoffset;
0176     m2info->fbcon.var.yoffset = var->yoffset;
0177     pixelsize = m2info->fbcon.var.bits_per_pixel >> 3;
0178     linelen = m2info->fbcon.var.xres_virtual * pixelsize;
0179     pos = m2info->fbcon.var.yoffset * linelen + m2info->fbcon.var.xoffset * pixelsize;
0180     pos += m2info->video.offbase;
0181     if (m2info->interlaced) {
0182         mga_outl(0x3C2C, pos);
0183         mga_outl(0x3C28, pos + linelen);
0184     } else {
0185         mga_outl(0x3C28, pos);
0186     }
0187 }
0188 
0189 static int matroxfb_dh_decode_var(struct matroxfb_dh_fb_info* m2info,
0190         struct fb_var_screeninfo* var,
0191         int *visual,
0192         int *video_cmap_len,
0193         int *mode) {
0194     unsigned int mask;
0195     unsigned int memlen;
0196     unsigned int vramlen;
0197 
0198     switch (var->bits_per_pixel) {
0199         case 16:    mask = 0x1F;
0200                 break;
0201         case 32:    mask = 0x0F;
0202                 break;
0203         default:    return -EINVAL;
0204     }
0205     vramlen = m2info->video.len_usable;
0206     if (var->yres_virtual < var->yres)
0207         var->yres_virtual = var->yres;
0208     if (var->xres_virtual < var->xres)
0209         var->xres_virtual = var->xres;
0210     var->xres_virtual = (var->xres_virtual + mask) & ~mask;
0211     if (var->yres_virtual > 32767)
0212         return -EINVAL;
0213     memlen = var->xres_virtual * var->yres_virtual * (var->bits_per_pixel >> 3);
0214     if (memlen > vramlen)
0215         return -EINVAL;
0216     if (var->xoffset + var->xres > var->xres_virtual)
0217         var->xoffset = var->xres_virtual - var->xres;
0218     if (var->yoffset + var->yres > var->yres_virtual)
0219         var->yoffset = var->yres_virtual - var->yres;
0220 
0221     var->xres &= ~7;
0222     var->left_margin &= ~7;
0223     var->right_margin &= ~7;
0224     var->hsync_len &= ~7;
0225 
0226     *mode = var->bits_per_pixel;
0227     if (var->bits_per_pixel == 16) {
0228         if (var->green.length == 5) {
0229             var->red.offset = 10;
0230             var->red.length = 5;
0231             var->green.offset = 5;
0232             var->green.length = 5;
0233             var->blue.offset = 0;
0234             var->blue.length = 5;
0235             var->transp.offset = 15;
0236             var->transp.length = 1;
0237             *mode = 15;
0238         } else {
0239             var->red.offset = 11;
0240             var->red.length = 5;
0241             var->green.offset = 5;
0242             var->green.length = 6;
0243             var->blue.offset = 0;
0244             var->blue.length = 5;
0245             var->transp.offset = 0;
0246             var->transp.length = 0;
0247         }
0248     } else {
0249             var->red.offset = 16;
0250             var->red.length = 8;
0251             var->green.offset = 8;
0252             var->green.length = 8;
0253             var->blue.offset = 0;
0254             var->blue.length = 8;
0255             var->transp.offset = 24;
0256             var->transp.length = 8;
0257     }
0258     *visual = FB_VISUAL_TRUECOLOR;
0259     *video_cmap_len = 16;
0260     return 0;
0261 }
0262 
0263 static int matroxfb_dh_open(struct fb_info* info, int user) {
0264 #define m2info (container_of(info, struct matroxfb_dh_fb_info, fbcon))
0265     struct matrox_fb_info *minfo = m2info->primary_dev;
0266 
0267     if (minfo) {
0268         int err;
0269 
0270         if (minfo->dead) {
0271             return -ENXIO;
0272         }
0273         err = minfo->fbops.fb_open(&minfo->fbcon, user);
0274         if (err) {
0275             return err;
0276         }
0277     }
0278     return 0;
0279 #undef m2info
0280 }
0281 
0282 static int matroxfb_dh_release(struct fb_info* info, int user) {
0283 #define m2info (container_of(info, struct matroxfb_dh_fb_info, fbcon))
0284     int err = 0;
0285     struct matrox_fb_info *minfo = m2info->primary_dev;
0286 
0287     if (minfo) {
0288         err = minfo->fbops.fb_release(&minfo->fbcon, user);
0289     }
0290     return err;
0291 #undef m2info
0292 }
0293 
0294 /*
0295  * This function is called before the register_framebuffer so
0296  * no locking is needed.
0297  */
0298 static void matroxfb_dh_init_fix(struct matroxfb_dh_fb_info *m2info)
0299 {
0300     struct fb_fix_screeninfo *fix = &m2info->fbcon.fix;
0301 
0302     strcpy(fix->id, "MATROX DH");
0303 
0304     fix->smem_start = m2info->video.base;
0305     fix->smem_len = m2info->video.len_usable;
0306     fix->ypanstep = 1;
0307     fix->ywrapstep = 0;
0308     fix->xpanstep = 8;  /* TBD */
0309     fix->mmio_start = m2info->mmio.base;
0310     fix->mmio_len = m2info->mmio.len;
0311     fix->accel = 0;     /* no accel... */
0312 }
0313 
0314 static int matroxfb_dh_check_var(struct fb_var_screeninfo* var, struct fb_info* info) {
0315 #define m2info (container_of(info, struct matroxfb_dh_fb_info, fbcon))
0316     int visual;
0317     int cmap_len;
0318     int mode;
0319 
0320     return matroxfb_dh_decode_var(m2info, var, &visual, &cmap_len, &mode);
0321 #undef m2info
0322 }
0323 
0324 static int matroxfb_dh_set_par(struct fb_info* info) {
0325 #define m2info (container_of(info, struct matroxfb_dh_fb_info, fbcon))
0326     int visual;
0327     int cmap_len;
0328     int mode;
0329     int err;
0330     struct fb_var_screeninfo* var = &info->var;
0331     struct matrox_fb_info *minfo = m2info->primary_dev;
0332 
0333     if ((err = matroxfb_dh_decode_var(m2info, var, &visual, &cmap_len, &mode)) != 0)
0334         return err;
0335     /* cmap */
0336     {
0337         m2info->fbcon.screen_base = vaddr_va(m2info->video.vbase);
0338         m2info->fbcon.fix.visual = visual;
0339         m2info->fbcon.fix.type = FB_TYPE_PACKED_PIXELS;
0340         m2info->fbcon.fix.type_aux = 0;
0341         m2info->fbcon.fix.line_length = (var->xres_virtual * var->bits_per_pixel) >> 3;
0342     }
0343     {
0344         struct my_timming mt;
0345         unsigned int pos;
0346         int out;
0347         int cnt;
0348 
0349         matroxfb_var2my(&m2info->fbcon.var, &mt);
0350         mt.crtc = MATROXFB_SRC_CRTC2;
0351         /* CRTC2 delay */
0352         mt.delay = 34;
0353 
0354         pos = (m2info->fbcon.var.yoffset * m2info->fbcon.var.xres_virtual + m2info->fbcon.var.xoffset) * m2info->fbcon.var.bits_per_pixel >> 3;
0355         pos += m2info->video.offbase;
0356         cnt = 0;
0357         down_read(&minfo->altout.lock);
0358         for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
0359             if (minfo->outputs[out].src == MATROXFB_SRC_CRTC2) {
0360                 cnt++;
0361                 if (minfo->outputs[out].output->compute) {
0362                     minfo->outputs[out].output->compute(minfo->outputs[out].data, &mt);
0363                 }
0364             }
0365         }
0366         minfo->crtc2.pixclock = mt.pixclock;
0367         minfo->crtc2.mnp = mt.mnp;
0368         up_read(&minfo->altout.lock);
0369         if (cnt) {
0370             matroxfb_dh_restore(m2info, &mt, mode, pos);
0371         } else {
0372             matroxfb_dh_disable(m2info);
0373         }
0374         DAC1064_global_init(minfo);
0375         DAC1064_global_restore(minfo);
0376         down_read(&minfo->altout.lock);
0377         for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
0378             if (minfo->outputs[out].src == MATROXFB_SRC_CRTC2 &&
0379                 minfo->outputs[out].output->program) {
0380                 minfo->outputs[out].output->program(minfo->outputs[out].data);
0381             }
0382         }
0383         for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
0384             if (minfo->outputs[out].src == MATROXFB_SRC_CRTC2 &&
0385                 minfo->outputs[out].output->start) {
0386                 minfo->outputs[out].output->start(minfo->outputs[out].data);
0387             }
0388         }
0389         up_read(&minfo->altout.lock);
0390     }
0391     m2info->initialized = 1;
0392     return 0;
0393 #undef m2info
0394 }
0395 
0396 static int matroxfb_dh_pan_display(struct fb_var_screeninfo* var, struct fb_info* info) {
0397 #define m2info (container_of(info, struct matroxfb_dh_fb_info, fbcon))
0398     matroxfb_dh_pan_var(m2info, var);
0399     return 0;
0400 #undef m2info
0401 }
0402 
0403 static int matroxfb_dh_get_vblank(const struct matroxfb_dh_fb_info* m2info, struct fb_vblank* vblank) {
0404     struct matrox_fb_info *minfo = m2info->primary_dev;
0405 
0406     matroxfb_enable_irq(minfo, 0);
0407     memset(vblank, 0, sizeof(*vblank));
0408     vblank->flags = FB_VBLANK_HAVE_VCOUNT | FB_VBLANK_HAVE_VBLANK;
0409     /* mask out reserved bits + field number (odd/even) */
0410     vblank->vcount = mga_inl(0x3C48) & 0x000007FF;
0411     /* compatibility stuff */
0412     if (vblank->vcount >= m2info->fbcon.var.yres)
0413         vblank->flags |= FB_VBLANK_VBLANKING;
0414     if (test_bit(0, &minfo->irq_flags)) {
0415                 vblank->flags |= FB_VBLANK_HAVE_COUNT;
0416                 /* Only one writer, aligned int value...
0417                    it should work without lock and without atomic_t */
0418         vblank->count = minfo->crtc2.vsync.cnt;
0419         }
0420     return 0;
0421 }
0422 
0423 static int matroxfb_dh_ioctl(struct fb_info *info,
0424         unsigned int cmd,
0425         unsigned long arg)
0426 {
0427 #define m2info (container_of(info, struct matroxfb_dh_fb_info, fbcon))
0428     struct matrox_fb_info *minfo = m2info->primary_dev;
0429 
0430     DBG(__func__)
0431 
0432     switch (cmd) {
0433         case FBIOGET_VBLANK:
0434             {
0435                 struct fb_vblank vblank;
0436                 int err;
0437 
0438                 err = matroxfb_dh_get_vblank(m2info, &vblank);
0439                 if (err)
0440                     return err;
0441                 if (copy_to_user((void __user *)arg, &vblank, sizeof(vblank)))
0442                     return -EFAULT;
0443                 return 0;
0444             }
0445         case FBIO_WAITFORVSYNC:
0446             {
0447                 u_int32_t crt;
0448 
0449                 if (get_user(crt, (u_int32_t __user *)arg))
0450                     return -EFAULT;
0451 
0452                 if (crt != 0)
0453                     return -ENODEV;
0454                 return matroxfb_wait_for_sync(minfo, 1);
0455             }
0456         case MATROXFB_SET_OUTPUT_MODE:
0457         case MATROXFB_GET_OUTPUT_MODE:
0458         case MATROXFB_GET_ALL_OUTPUTS:
0459             {
0460                 return minfo->fbcon.fbops->fb_ioctl(&minfo->fbcon, cmd, arg);
0461             }
0462         case MATROXFB_SET_OUTPUT_CONNECTION:
0463             {
0464                 u_int32_t tmp;
0465                 int out;
0466                 int changes;
0467 
0468                 if (get_user(tmp, (u_int32_t __user *)arg))
0469                     return -EFAULT;
0470                 for (out = 0; out < 32; out++) {
0471                     if (tmp & (1 << out)) {
0472                         if (out >= MATROXFB_MAX_OUTPUTS)
0473                             return -ENXIO;
0474                         if (!minfo->outputs[out].output)
0475                             return -ENXIO;
0476                         switch (minfo->outputs[out].src) {
0477                             case MATROXFB_SRC_NONE:
0478                             case MATROXFB_SRC_CRTC2:
0479                                 break;
0480                             default:
0481                                 return -EBUSY;
0482                         }
0483                     }
0484                 }
0485                 if (minfo->devflags.panellink) {
0486                     if (tmp & MATROXFB_OUTPUT_CONN_DFP)
0487                         return -EINVAL;
0488                     if ((minfo->outputs[2].src == MATROXFB_SRC_CRTC1) && tmp)
0489                         return -EBUSY;
0490                 }
0491                 changes = 0;
0492                 for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
0493                     if (tmp & (1 << out)) {
0494                         if (minfo->outputs[out].src != MATROXFB_SRC_CRTC2) {
0495                             changes = 1;
0496                             minfo->outputs[out].src = MATROXFB_SRC_CRTC2;
0497                         }
0498                     } else if (minfo->outputs[out].src == MATROXFB_SRC_CRTC2) {
0499                         changes = 1;
0500                         minfo->outputs[out].src = MATROXFB_SRC_NONE;
0501                     }
0502                 }
0503                 if (!changes)
0504                     return 0;
0505                 matroxfb_dh_set_par(info);
0506                 return 0;
0507             }
0508         case MATROXFB_GET_OUTPUT_CONNECTION:
0509             {
0510                 u_int32_t conn = 0;
0511                 int out;
0512 
0513                 for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
0514                     if (minfo->outputs[out].src == MATROXFB_SRC_CRTC2) {
0515                         conn |= 1 << out;
0516                     }
0517                 }
0518                 if (put_user(conn, (u_int32_t __user *)arg))
0519                     return -EFAULT;
0520                 return 0;
0521             }
0522         case MATROXFB_GET_AVAILABLE_OUTPUTS:
0523             {
0524                 u_int32_t tmp = 0;
0525                 int out;
0526 
0527                 for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
0528                     if (minfo->outputs[out].output) {
0529                         switch (minfo->outputs[out].src) {
0530                             case MATROXFB_SRC_NONE:
0531                             case MATROXFB_SRC_CRTC2:
0532                                 tmp |= 1 << out;
0533                                 break;
0534                         }
0535                     }
0536                 }
0537                 if (minfo->devflags.panellink) {
0538                     tmp &= ~MATROXFB_OUTPUT_CONN_DFP;
0539                     if (minfo->outputs[2].src == MATROXFB_SRC_CRTC1) {
0540                         tmp = 0;
0541                     }
0542                 }
0543                 if (put_user(tmp, (u_int32_t __user *)arg))
0544                     return -EFAULT;
0545                 return 0;
0546             }
0547     }
0548     return -ENOTTY;
0549 #undef m2info
0550 }
0551 
0552 static int matroxfb_dh_blank(int blank, struct fb_info* info) {
0553 #define m2info (container_of(info, struct matroxfb_dh_fb_info, fbcon))
0554     switch (blank) {
0555         case 1:
0556         case 2:
0557         case 3:
0558         case 4:
0559         default:;
0560     }
0561     /* do something... */
0562     return 0;
0563 #undef m2info
0564 }
0565 
0566 static const struct fb_ops matroxfb_dh_ops = {
0567     .owner =    THIS_MODULE,
0568     .fb_open =  matroxfb_dh_open,
0569     .fb_release =   matroxfb_dh_release,
0570     .fb_check_var = matroxfb_dh_check_var,
0571     .fb_set_par =   matroxfb_dh_set_par,
0572     .fb_setcolreg = matroxfb_dh_setcolreg,
0573     .fb_pan_display =matroxfb_dh_pan_display,
0574     .fb_blank = matroxfb_dh_blank,
0575     .fb_ioctl = matroxfb_dh_ioctl,
0576     .fb_fillrect =  cfb_fillrect,
0577     .fb_copyarea =  cfb_copyarea,
0578     .fb_imageblit = cfb_imageblit,
0579 };
0580 
0581 static struct fb_var_screeninfo matroxfb_dh_defined = {
0582         640,480,640,480,/* W,H, virtual W,H */
0583         0,0,        /* offset */
0584         32,     /* depth */
0585         0,      /* gray */
0586         {0,0,0},    /* R */
0587         {0,0,0},    /* G */
0588         {0,0,0},    /* B */
0589         {0,0,0},    /* alpha */
0590         0,      /* nonstd */
0591         FB_ACTIVATE_NOW,
0592         -1,-1,      /* display size */
0593         0,      /* accel flags */
0594         39721L,48L,16L,33L,10L,
0595         96L,2,0,    /* no sync info */
0596         FB_VMODE_NONINTERLACED,
0597 };
0598 
0599 static int matroxfb_dh_regit(const struct matrox_fb_info *minfo,
0600                  struct matroxfb_dh_fb_info *m2info)
0601 {
0602 #define minfo (m2info->primary_dev)
0603     void* oldcrtc2;
0604 
0605     m2info->fbcon.fbops = &matroxfb_dh_ops;
0606     m2info->fbcon.flags = FBINFO_FLAG_DEFAULT;
0607     m2info->fbcon.flags |= FBINFO_HWACCEL_XPAN |
0608                    FBINFO_HWACCEL_YPAN;
0609     m2info->fbcon.pseudo_palette = m2info->cmap;
0610     fb_alloc_cmap(&m2info->fbcon.cmap, 256, 1);
0611 
0612     if (mem < 64)
0613         mem *= 1024;
0614     if (mem < 64*1024)
0615         mem *= 1024;
0616     mem &= ~0x00000FFF; /* PAGE_MASK? */
0617     if (minfo->video.len_usable + mem <= minfo->video.len)
0618         m2info->video.offbase = minfo->video.len - mem;
0619     else if (minfo->video.len < mem) {
0620         return -ENOMEM;
0621     } else { /* check yres on first head... */
0622         m2info->video.borrowed = mem;
0623         minfo->video.len_usable -= mem;
0624         m2info->video.offbase = minfo->video.len_usable;
0625     }
0626     m2info->video.base = minfo->video.base + m2info->video.offbase;
0627     m2info->video.len = m2info->video.len_usable = m2info->video.len_maximum = mem;
0628     m2info->video.vbase.vaddr = vaddr_va(minfo->video.vbase) + m2info->video.offbase;
0629     m2info->mmio.base = minfo->mmio.base;
0630     m2info->mmio.vbase = minfo->mmio.vbase;
0631     m2info->mmio.len = minfo->mmio.len;
0632 
0633     matroxfb_dh_init_fix(m2info);
0634     if (register_framebuffer(&m2info->fbcon)) {
0635         return -ENXIO;
0636     }
0637     if (!m2info->initialized)
0638         fb_set_var(&m2info->fbcon, &matroxfb_dh_defined);
0639     down_write(&minfo->crtc2.lock);
0640     oldcrtc2 = minfo->crtc2.info;
0641     minfo->crtc2.info = m2info;
0642     up_write(&minfo->crtc2.lock);
0643     if (oldcrtc2) {
0644         printk(KERN_ERR "matroxfb_crtc2: Internal consistency check failed: crtc2 already present: %p\n",
0645             oldcrtc2);
0646     }
0647     return 0;
0648 #undef minfo
0649 }
0650 
0651 /* ************************** */
0652 
0653 static int matroxfb_dh_registerfb(struct matroxfb_dh_fb_info* m2info) {
0654 #define minfo (m2info->primary_dev)
0655     if (matroxfb_dh_regit(minfo, m2info)) {
0656         printk(KERN_ERR "matroxfb_crtc2: secondary head failed to register\n");
0657         return -1;
0658     }
0659     printk(KERN_INFO "matroxfb_crtc2: secondary head of fb%u was registered as fb%u\n",
0660         minfo->fbcon.node, m2info->fbcon.node);
0661     m2info->fbcon_registered = 1;
0662     return 0;
0663 #undef minfo
0664 }
0665 
0666 static void matroxfb_dh_deregisterfb(struct matroxfb_dh_fb_info* m2info) {
0667 #define minfo (m2info->primary_dev)
0668     if (m2info->fbcon_registered) {
0669         int id;
0670         struct matroxfb_dh_fb_info* crtc2;
0671 
0672         down_write(&minfo->crtc2.lock);
0673         crtc2 = minfo->crtc2.info;
0674         if (crtc2 == m2info)
0675             minfo->crtc2.info = NULL;
0676         up_write(&minfo->crtc2.lock);
0677         if (crtc2 != m2info) {
0678             printk(KERN_ERR "matroxfb_crtc2: Internal consistency check failed: crtc2 mismatch at unload: %p != %p\n",
0679                 crtc2, m2info);
0680             printk(KERN_ERR "matroxfb_crtc2: Expect kernel crash after module unload.\n");
0681             return;
0682         }
0683         id = m2info->fbcon.node;
0684         unregister_framebuffer(&m2info->fbcon);
0685         /* return memory back to primary head */
0686         minfo->video.len_usable += m2info->video.borrowed;
0687         printk(KERN_INFO "matroxfb_crtc2: fb%u unregistered\n", id);
0688         m2info->fbcon_registered = 0;
0689     }
0690 #undef minfo
0691 }
0692 
0693 static void* matroxfb_crtc2_probe(struct matrox_fb_info* minfo) {
0694     struct matroxfb_dh_fb_info* m2info;
0695 
0696     /* hardware is CRTC2 incapable... */
0697     if (!minfo->devflags.crtc2)
0698         return NULL;
0699     m2info = kzalloc(sizeof(*m2info), GFP_KERNEL);
0700     if (!m2info)
0701         return NULL;
0702 
0703     m2info->primary_dev = minfo;
0704     if (matroxfb_dh_registerfb(m2info)) {
0705         kfree(m2info);
0706         printk(KERN_ERR "matroxfb_crtc2: CRTC2 framebuffer failed to register\n");
0707         return NULL;
0708     }
0709     return m2info;
0710 }
0711 
0712 static void matroxfb_crtc2_remove(struct matrox_fb_info* minfo, void* crtc2) {
0713     matroxfb_dh_deregisterfb(crtc2);
0714     kfree(crtc2);
0715 }
0716 
0717 static struct matroxfb_driver crtc2 = {
0718         .name =     "Matrox G400 CRTC2",
0719         .probe =    matroxfb_crtc2_probe,
0720         .remove =   matroxfb_crtc2_remove };
0721 
0722 static int matroxfb_crtc2_init(void) {
0723     if (fb_get_options("matrox_crtc2fb", NULL))
0724         return -ENODEV;
0725 
0726     matroxfb_register_driver(&crtc2);
0727     return 0;
0728 }
0729 
0730 static void matroxfb_crtc2_exit(void) {
0731     matroxfb_unregister_driver(&crtc2);
0732 }
0733 
0734 MODULE_AUTHOR("(c) 1999-2002 Petr Vandrovec <vandrove@vc.cvut.cz>");
0735 MODULE_DESCRIPTION("Matrox G400 CRTC2 driver");
0736 MODULE_LICENSE("GPL");
0737 module_init(matroxfb_crtc2_init);
0738 module_exit(matroxfb_crtc2_exit);
0739 /* we do not have __setup() yet */