demux_ts: Fix subtitle sync issues
[mplayer/glamo.git] / drivers / tdfx_vid.c
blob501f5fc60339c8a7f78dd2b4301b0219d476e7fe
1 /*
2 * Copyright (C) 2003 Alban Bedel
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <linux/config.h>
22 #include <linux/version.h>
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/mm.h>
28 #include <linux/string.h>
29 #include <linux/errno.h>
31 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,10)
32 #include <linux/malloc.h>
33 #else
34 #include <linux/slab.h>
35 #endif
37 #include <linux/pci.h>
38 #include <linux/ioport.h>
39 #include <linux/init.h>
40 #include <linux/agp_backend.h>
42 #include <asm/uaccess.h>
43 #include <asm/system.h>
44 #include <asm/io.h>
46 #include "tdfx_vid.h"
47 #include "3dfx.h"
50 #define TDFX_VID_MAJOR 178
52 MODULE_AUTHOR("Albeu");
53 MODULE_DESCRIPTION("A driver for Banshee targeted for video app");
55 #ifdef MODULE_LICENSE
56 MODULE_LICENSE("GPL");
57 #endif
59 #ifndef min
60 #define min(x,y) (((x)<(y))?(x):(y))
61 #endif
63 static struct pci_dev *pci_dev;
65 static uint8_t *tdfx_mmio_base = 0;
66 static uint32_t tdfx_mem_base = 0;
67 static uint32_t tdfx_io_base = 0;
69 static int tdfx_ram_size = 0;
71 static int tdfx_vid_in_use = 0;
73 static drm_agp_t *drm_agp = NULL;
74 static agp_kern_info agp_info;
75 static agp_memory *agp_mem = NULL;
77 static __initdata int tdfx_map_io = 1;
78 static __initdata unsigned long map_start = 0; //0x7300000;
79 static __initdata unsigned long map_max = (10*1024*1024);
81 MODULE_PARM(tdfx_map_io,"i");
82 MODULE_PARM_DESC(tdfx_map_io, "Set to 0 to use the page fault handler (you need to patch agpgart_be.c to allow the mapping in user space)\n");
83 MODULE_PARM(map_start,"l");
84 MODULE_PARM_DESC(map_start,"Use a block of physical mem instead of the agp arerture.");
85 MODULE_PARM(map_max,"l");
86 MODULE_PARM_DESC(map_max, "Maximum amout of physical memory (in bytes) that can be used\n");
88 static inline u32 tdfx_inl(unsigned int reg) {
89 return readl(tdfx_mmio_base + reg);
92 static inline void tdfx_outl(unsigned int reg, u32 val) {
93 writel(val,tdfx_mmio_base + reg);
96 static inline void banshee_make_room(int size) {
97 while((tdfx_inl(STATUS) & 0x1f) < size);
100 static inline void banshee_wait_idle(void) {
101 int i = 0;
103 banshee_make_room(1);
104 tdfx_outl(COMMAND_3D, COMMAND_3D_NOP);
106 while(1) {
107 i = (tdfx_inl(STATUS) & STATUS_BUSY) ? 0 : i + 1;
108 if(i == 3) break;
112 static unsigned long get_lfb_size(void) {
113 u32 draminit0 = 0;
114 u32 draminit1 = 0;
115 // u32 miscinit1 = 0;
116 u32 lfbsize = 0;
117 int sgram_p = 0;
119 draminit0 = tdfx_inl(DRAMINIT0);
120 draminit1 = tdfx_inl(DRAMINIT1);
122 if ((pci_dev->device == PCI_DEVICE_ID_3DFX_BANSHEE) ||
123 (pci_dev->device == PCI_DEVICE_ID_3DFX_VOODOO3)) {
124 sgram_p = (draminit1 & DRAMINIT1_MEM_SDRAM) ? 0 : 1;
126 lfbsize = sgram_p ?
127 (((draminit0 & DRAMINIT0_SGRAM_NUM) ? 2 : 1) *
128 ((draminit0 & DRAMINIT0_SGRAM_TYPE) ? 8 : 4) * 1024 * 1024) :
129 16 * 1024 * 1024;
130 } else {
131 /* Voodoo4/5 */
132 u32 chips, psize, banks;
134 chips = ((draminit0 & (1 << 26)) == 0) ? 4 : 8;
135 psize = 1 << ((draminit0 & 0x38000000) >> 28);
136 banks = ((draminit0 & (1 << 30)) == 0) ? 2 : 4;
137 lfbsize = chips * psize * banks;
138 lfbsize <<= 20;
141 #if 0
142 /* disable block writes for SDRAM (why?) */
143 miscinit1 = tdfx_inl(MISCINIT1);
144 miscinit1 |= sgram_p ? 0 : MISCINIT1_2DBLOCK_DIS;
145 miscinit1 |= MISCINIT1_CLUT_INV;
147 banshee_make_room(1);
148 tdfx_outl(MISCINIT1, miscinit1);
149 #endif
151 return lfbsize;
154 static int tdfx_vid_find_card(void)
156 struct pci_dev *dev = NULL;
157 // unsigned int card_option;
159 if((dev = pci_find_device(PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_BANSHEE, NULL)))
160 printk(KERN_INFO "tdfx_vid: Found VOODOO BANSHEE\n");
161 else if((dev = pci_find_device(PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO3, NULL)))
162 printk(KERN_INFO "tdfx_vid: Found VOODOO 3 \n");
163 else
164 return 0;
167 pci_dev = dev;
169 #if LINUX_VERSION_CODE >= 0x020300
170 tdfx_mmio_base = ioremap_nocache(dev->resource[0].start,1 << 24);
171 tdfx_mem_base = dev->resource[1].start;
172 tdfx_io_base = dev->resource[2].start;
173 #else
174 tdfx_mmio_base = ioremap_nocache(dev->base_address[1] & PCI_BASE_ADDRESS_MEM_MASK,0x4000);
175 tdfx_mem_base = dev->base_address[1] & PCI_BASE_ADDRESS_MEM_MASK;
176 tdfx_io_base = dev->base_address[2] & PCI_BASE_ADDRESS_MEM_MASK;
177 #endif
178 printk(KERN_INFO "tdfx_vid: MMIO at 0x%p\n", tdfx_mmio_base);
179 tdfx_ram_size = get_lfb_size();
181 printk(KERN_INFO "tdfx_vid: Found %d MB (%d bytes) of memory\n",
182 tdfx_ram_size / 1024 / 1024,tdfx_ram_size);
185 #if 0
187 int temp;
188 printk("List resources -----------\n");
189 for(temp=0;temp<DEVICE_COUNT_RESOURCE;temp++){
190 struct resource *res=&pci_dev->resource[temp];
191 if(res->flags){
192 int size=(1+res->end-res->start)>>20;
193 printk(KERN_DEBUG "res %d: start: 0x%X end: 0x%X (%d MB) flags=0x%X\n",temp,res->start,res->end,size,res->flags);
194 if(res->flags&(IORESOURCE_MEM|IORESOURCE_PREFETCH)){
195 if(size>tdfx_ram_size && size<=64) tdfx_ram_size=size;
200 #endif
203 return 1;
206 static int agp_init(void) {
208 drm_agp = (drm_agp_t*)inter_module_get("drm_agp");
210 if(!drm_agp) {
211 printk(KERN_ERR "tdfx_vid: Unable to get drm_agp pointer\n");
212 return 0;
215 if(drm_agp->acquire()) {
216 printk(KERN_ERR "tdfx_vid: Unable to acquire the agp backend\n");
217 drm_agp = NULL;
218 return 0;
221 drm_agp->copy_info(&agp_info);
222 #if 0
223 printk(KERN_DEBUG "AGP Version : %d %d\n"
224 "AGP Mode: %#X\nAperture Base: %p\nAperture Size: %d\n"
225 "Max memory = %d\nCurrent mem = %d\nCan use perture : %s\n"
226 "Page mask = %#X\n",
227 agp_info.version.major,agp_info.version.minor,
228 agp_info.mode,agp_info.aper_base,agp_info.aper_size,
229 agp_info.max_memory,agp_info.current_memory,
230 agp_info.cant_use_aperture ? "no" : "yes",
231 agp_info.page_mask);
232 #endif
233 drm_agp->enable(agp_info.mode);
236 printk(KERN_INFO "AGP Enabled\n");
238 return 1;
241 static void agp_close(void) {
243 if(!drm_agp) return;
245 if(agp_mem) {
246 drm_agp->unbind_memory(agp_mem);
247 drm_agp->free_memory(agp_mem);
248 agp_mem = NULL;
252 drm_agp->release();
253 inter_module_put("drm_agp");
256 static int agp_move(tdfx_vid_agp_move_t* m) {
257 u32 src = 0;
258 u32 src_h,src_l;
260 if(!(agp_mem||map_start))
261 return -EAGAIN;
263 if(m->move2 > 3) {
264 printk(KERN_DEBUG "tdfx_vid: AGP move invalid destination %d\n",
265 m->move2);
266 return -EAGAIN;
269 if(map_start)
270 src = map_start + m->src;
271 else
272 src = agp_info.aper_base + m->src;
274 src_l = (u32)src;
275 src_h = (m->width | (m->src_stride << 14)) & 0x0FFFFFFF;
277 // banshee_wait_idle();
278 banshee_make_room(6);
279 tdfx_outl(AGPHOSTADDRESSHIGH,src_h);
280 tdfx_outl(AGPHOSTADDRESSLOW,src_l);
282 tdfx_outl(AGPGRAPHICSADDRESS, m->dst);
283 tdfx_outl(AGPGRAPHICSSTRIDE, m->dst_stride);
284 tdfx_outl(AGPREQSIZE,m->src_stride*m->height);
286 tdfx_outl(AGPMOVECMD,m->move2 << 3);
287 banshee_wait_idle();
289 return 0;
292 #if 0
293 static void setup_fifo(u32 offset,ssize_t pages) {
294 long addr = agp_info.aper_base + offset;
295 u32 size = pages | 0x700; // fifo on, in agp mem, disable hole cnt
297 banshee_wait_idle();
299 tdfx_outl(CMDBASEADDR0,addr >> 4);
300 tdfx_outl(CMDRDPTRL0, addr << 4);
301 tdfx_outl(CMDRDPTRH0, addr >> 28);
302 tdfx_outl(CMDAMIN0, (addr - 4) & 0xFFFFFF);
303 tdfx_outl(CMDAMAX0, (addr - 4) & 0xFFFFFF);
304 tdfx_outl(CMDFIFODEPTH0, 0);
305 tdfx_outl(CMDHOLECNT0, 0);
306 tdfx_outl(CMDBASESIZE0,size);
308 banshee_wait_idle();
311 #endif
313 static int bump_fifo(u16 size) {
315 banshee_wait_idle();
316 tdfx_outl(CMDBUMP0 , size);
317 banshee_wait_idle();
319 return 0;
322 static void tdfx_vid_get_config(tdfx_vid_config_t* cfg) {
323 u32 in;
325 cfg->version = TDFX_VID_VERSION;
326 cfg->ram_size = tdfx_ram_size;
328 in = tdfx_inl(VIDSCREENSIZE);
329 cfg->screen_width = in & 0xFFF;
330 cfg->screen_height = (in >> 12) & 0xFFF;
331 in = (tdfx_inl(VIDPROCCFG)>> 18)& 0x7;
332 switch(in) {
333 case 0:
334 cfg->screen_format = TDFX_VID_FORMAT_BGR8;
335 break;
336 case 1:
337 cfg->screen_format = TDFX_VID_FORMAT_BGR16;
338 break;
339 case 2:
340 cfg->screen_format = TDFX_VID_FORMAT_BGR24;
341 break;
342 case 3:
343 cfg->screen_format = TDFX_VID_FORMAT_BGR32;
344 break;
345 default:
346 printk(KERN_INFO "tdfx_vid: unknown screen format %d\n",in);
347 cfg->screen_format = 0;
348 break;
350 cfg->screen_stride = tdfx_inl(VIDDESKSTRIDE) & 0x7FFF;
351 cfg->screen_start = tdfx_inl(VIDDESKSTART);
354 inline static u32 tdfx_vid_make_format(int src,u16 stride,u32 fmt) {
355 u32 r = stride & 0xFFF3;
356 u32 tdfx_fmt = 0;
358 // src and dest formats
359 switch(fmt) {
360 case TDFX_VID_FORMAT_BGR8:
361 tdfx_fmt = 1;
362 break;
363 case TDFX_VID_FORMAT_BGR16:
364 tdfx_fmt = 3;
365 break;
366 case TDFX_VID_FORMAT_BGR24:
367 tdfx_fmt = 4;
368 break;
369 case TDFX_VID_FORMAT_BGR32:
370 tdfx_fmt = 5;
371 break;
374 if(!src && !tdfx_fmt) {
375 printk(KERN_INFO "tdfx_vid: Invalid destination format %#X\n",fmt);
376 return 0;
379 if(src && !tdfx_fmt) {
380 // src only format
381 switch(fmt){
382 case TDFX_VID_FORMAT_BGR1:
383 tdfx_fmt = 0;
384 break;
385 case TDFX_VID_FORMAT_BGR15: // To check
386 tdfx_fmt = 2;
387 break;
388 case TDFX_VID_FORMAT_YUY2:
389 tdfx_fmt = 8;
390 break;
391 case TDFX_VID_FORMAT_UYVY:
392 tdfx_fmt = 9;
393 break;
394 default:
395 printk(KERN_INFO "tdfx_vid: Invalid source format %#X\n",fmt);
396 return 0;
400 r |= tdfx_fmt << 16;
402 return r;
405 static int tdfx_vid_blit(tdfx_vid_blit_t* blit) {
406 u32 src_fmt,dst_fmt,cmd = 2;
407 u32 cmin,cmax,srcbase,srcxy,srcfmt,srcsize;
408 u32 dstbase,dstxy,dstfmt,dstsize = 0;
409 u32 cmd_extra = 0,src_ck[2],dst_ck[2],rop123=0;
411 //printk(KERN_INFO "tdfx_vid: Make src fmt 0x%x\n",blit->src_format);
412 src_fmt = tdfx_vid_make_format(1,blit->src_stride,blit->src_format);
413 if(!src_fmt)
414 return 0;
415 //printk(KERN_INFO "tdfx_vid: Make dst fmt 0x%x\n", blit->dst_format);
416 dst_fmt = tdfx_vid_make_format(0,blit->dst_stride,blit->dst_format);
417 if(!dst_fmt)
418 return 0;
419 blit->colorkey &= 0x3;
420 // Be nice if user just want a simple blit
421 if((!blit->colorkey) && (!blit->rop[0]))
422 blit->rop[0] = TDFX_VID_ROP_COPY;
423 // No stretch : fix me the cmd should be 1 but it
424 // doesn't work. Maybe some other regs need to be set
425 // as non-stretch blit have more options
426 if(((!blit->dst_w) && (!blit->dst_h)) ||
427 ((blit->dst_w == blit->src_w) && (blit->dst_h == blit->src_h)))
428 cmd = 2;
430 // Save the regs otherwise fb get crazy
431 // we can perhaps avoid some ...
432 banshee_wait_idle();
433 cmin = tdfx_inl(CLIP0MIN);
434 cmax = tdfx_inl(CLIP0MAX);
435 srcbase = tdfx_inl(SRCBASE);
436 srcxy = tdfx_inl(SRCXY);
437 srcfmt = tdfx_inl(SRCFORMAT);
438 srcsize = tdfx_inl(SRCSIZE);
439 dstbase = tdfx_inl(DSTBASE);
440 dstxy = tdfx_inl(DSTXY);
441 dstfmt = tdfx_inl(DSTFORMAT);
442 if(cmd == 2)
443 dstsize = tdfx_inl(DSTSIZE);
444 if(blit->colorkey & TDFX_VID_SRC_COLORKEY) {
445 src_ck[0] = tdfx_inl(SRCCOLORKEYMIN);
446 src_ck[1] = tdfx_inl(SRCCOLORKEYMAX);
447 tdfx_outl(SRCCOLORKEYMIN,blit->src_colorkey[0]);
448 tdfx_outl(SRCCOLORKEYMAX,blit->src_colorkey[1]);
450 if(blit->colorkey & TDFX_VID_DST_COLORKEY) {
451 dst_ck[0] = tdfx_inl(DSTCOLORKEYMIN);
452 dst_ck[1] = tdfx_inl(DSTCOLORKEYMAX);
453 tdfx_outl(SRCCOLORKEYMIN,blit->dst_colorkey[0]);
454 tdfx_outl(SRCCOLORKEYMAX,blit->dst_colorkey[1]);
456 if(blit->colorkey) {
457 cmd_extra = tdfx_inl(COMMANDEXTRA_2D);
458 rop123 = tdfx_inl(ROP123);
459 tdfx_outl(COMMANDEXTRA_2D, blit->colorkey);
460 tdfx_outl(ROP123,(blit->rop[1] | (blit->rop[2] << 8) | blit->rop[3] << 16));
463 // Get rid of the clipping at the moment
464 tdfx_outl(CLIP0MIN,0);
465 tdfx_outl(CLIP0MAX,0x0fff0fff);
467 // Setup the src
468 tdfx_outl(SRCBASE,blit->src & 0x00FFFFFF);
469 tdfx_outl(SRCXY,XYREG(blit->src_x,blit->src_y));
470 tdfx_outl(SRCFORMAT,src_fmt);
471 tdfx_outl(SRCSIZE,XYREG(blit->src_w,blit->src_h));
473 // Setup the dst
474 tdfx_outl(DSTBASE,blit->dst & 0x00FFFFFF);
475 tdfx_outl(DSTXY,XYREG(blit->dst_x,blit->dst_y));
476 tdfx_outl(DSTFORMAT,dst_fmt);
477 if(cmd == 2)
478 tdfx_outl(DSTSIZE,XYREG(blit->dst_w,blit->dst_h));
480 // Send the command
481 tdfx_outl(COMMAND_2D,cmd | 0x100 | (blit->rop[0] << 24));
482 banshee_wait_idle();
484 // Now restore the regs to make fb happy
485 tdfx_outl(CLIP0MIN, cmin);
486 tdfx_outl(CLIP0MAX, cmax);
487 tdfx_outl(SRCBASE, srcbase);
488 tdfx_outl(SRCXY, srcxy);
489 tdfx_outl(SRCFORMAT, srcfmt);
490 tdfx_outl(SRCSIZE, srcsize);
491 tdfx_outl(DSTBASE, dstbase);
492 tdfx_outl(DSTXY, dstxy);
493 tdfx_outl(DSTFORMAT, dstfmt);
494 if(cmd == 2)
495 tdfx_outl(DSTSIZE, dstsize);
496 if(blit->colorkey & TDFX_VID_SRC_COLORKEY) {
497 tdfx_outl(SRCCOLORKEYMIN,src_ck[0]);
498 tdfx_outl(SRCCOLORKEYMAX,src_ck[1]);
500 if(blit->colorkey & TDFX_VID_DST_COLORKEY) {
501 tdfx_outl(SRCCOLORKEYMIN,dst_ck[0]);
502 tdfx_outl(SRCCOLORKEYMAX,dst_ck[1]);
504 if(blit->colorkey) {
505 tdfx_outl(COMMANDEXTRA_2D,cmd_extra);
506 tdfx_outl(ROP123,rop123);
508 return 1;
511 static int tdfx_vid_set_yuv(unsigned long arg) {
512 tdfx_vid_yuv_t yuv;
514 if(copy_from_user(&yuv,(tdfx_vid_yuv_t*)arg,sizeof(tdfx_vid_yuv_t))) {
515 printk(KERN_DEBUG "tdfx_vid:failed copy from userspace\n");
516 return -EFAULT;
518 banshee_make_room(2);
519 tdfx_outl(YUVBASEADDRESS,yuv.base & 0x01FFFFFF);
520 tdfx_outl(YUVSTRIDE, yuv.stride & 0x3FFF);
522 banshee_wait_idle();
524 return 0;
527 static int tdfx_vid_get_yuv(unsigned long arg) {
528 tdfx_vid_yuv_t yuv;
530 yuv.base = tdfx_inl(YUVBASEADDRESS) & 0x01FFFFFF;
531 yuv.stride = tdfx_inl(YUVSTRIDE) & 0x3FFF;
533 if(copy_to_user((tdfx_vid_yuv_t*)arg,&yuv,sizeof(tdfx_vid_yuv_t))) {
534 printk(KERN_INFO "tdfx_vid:failed copy to userspace\n");
535 return -EFAULT;
538 return 0;
541 static int tdfx_vid_set_overlay(unsigned long arg) {
542 tdfx_vid_overlay_t ov;
543 uint32_t screen_w,screen_h;
544 uint32_t vidcfg,stride,vidbuf;
545 int disp_w,disp_h;
547 if(copy_from_user(&ov,(tdfx_vid_overlay_t*)arg,sizeof(tdfx_vid_overlay_t))) {
548 printk(KERN_DEBUG "tdfx_vid:failed copy from userspace\n");
549 return -EFAULT;
552 if(ov.dst_y < 0) {
553 int shift;
554 if(-ov.dst_y >= ov.src_height) {
555 printk(KERN_DEBUG "tdfx_vid: Overlay outside of the screen ????\n");
556 return -EFAULT;
558 shift = (-ov.dst_y)/(double)ov.dst_height*ov.src_height;
559 ov.src[0] += shift*ov.src_stride;
560 ov.src_height -= shift;
561 ov.dst_height += ov.dst_y;
562 ov.dst_y = 0;
565 if(ov.dst_x < 0) {
566 int shift;
567 if(-ov.dst_x >= ov.src_width) {
568 printk(KERN_DEBUG "tdfx_vid: Overlay outside of the screen ????\n");
569 return -EFAULT;
571 shift = (-ov.dst_x)/(double)ov.dst_width*ov.src_width;
572 shift = ((shift+3)/2)*2;
573 ov.src[0] += shift*2;
574 ov.src_width -= shift;
575 ov.dst_width += ov.dst_x;
576 ov.dst_x = 0;
579 vidcfg = tdfx_inl(VIDPROCCFG);
580 // clear the overlay fmt
581 vidcfg &= ~(7 << 21);
582 switch(ov.format) {
583 case TDFX_VID_FORMAT_BGR15:
584 vidcfg |= (1 << 21);
585 break;
586 case TDFX_VID_FORMAT_BGR16:
587 vidcfg |= (7 << 21);
588 break;
589 case TDFX_VID_FORMAT_YUY2:
590 vidcfg |= (5 << 21);
591 break;
592 case TDFX_VID_FORMAT_UYVY:
593 vidcfg |= (6 << 21);
594 break;
595 default:
596 printk(KERN_DEBUG "tdfx_vid: Invalid overlay fmt 0x%x\n",ov.format);
597 return -EFAULT;
600 // YUV422 need 4 bytes aligned stride and address
601 if((ov.format == TDFX_VID_FORMAT_YUY2 ||
602 ov.format == TDFX_VID_FORMAT_UYVY)) {
603 if((ov.src_stride & ~0x3) != ov.src_stride) {
604 printk(KERN_DEBUG "tdfx_vid: YUV need a 4 bytes aligned stride %d\n",ov.src_stride);
605 return -EFAULT;
607 if((ov.src[0] & ~0x3) != ov.src[0] || (ov.src[1] & ~0x3) != ov.src[1]){
608 printk(KERN_DEBUG "tdfx_vid: YUV need a 4 bytes aligned address 0x%x 0x%x\n",ov.src[0],ov.src[1]);
609 return -EFAULT;
613 // Now we have a good input format
614 // but first get the screen size to check a bit
615 // if the size/position is valid
616 screen_w = tdfx_inl(VIDSCREENSIZE);
617 screen_h = (screen_w >> 12) & 0xFFF;
618 screen_w &= 0xFFF;
619 disp_w = ov.dst_x + ov.dst_width >= screen_w ?
620 screen_w - ov.dst_x : ov.dst_width;
621 disp_h = ov.dst_y + ov.dst_height >= screen_h ?
622 screen_h - ov.dst_y : ov.dst_height;
624 if(ov.dst_x >= screen_w || ov.dst_y >= screen_h ||
625 disp_h <= 0 || disp_h > screen_h || disp_w <= 0 || disp_w > screen_w) {
626 printk(KERN_DEBUG "tdfx_vid: Invalid overlay dimension and/or position\n");
627 return -EFAULT;
629 // Setup the vidproc
630 // H scaling
631 if(ov.src_width < ov.dst_width)
632 vidcfg |= (1<<14);
633 else
634 vidcfg &= ~(1<<14);
635 // V scaling
636 if(ov.src_height < ov.dst_height)
637 vidcfg |= (1<<15);
638 else
639 vidcfg &= ~(1<<15);
640 // Filtering can only be used in 1x mode
641 if(!(vidcfg | (1<<26)))
642 vidcfg |= (3<<16);
643 else
644 vidcfg &= ~(3<<16);
645 // disable overlay stereo mode
646 vidcfg &= ~(1<<2);
647 // Colorkey on/off
648 if(ov.use_colorkey) {
649 // Colorkey inversion
650 if(ov.invert_colorkey)
651 vidcfg |= (1<<6);
652 else
653 vidcfg &= ~(1<<6);
654 vidcfg |= (1<<5);
655 } else
656 vidcfg &= ~(1<<5);
657 // Overlay isn't VidIn
658 vidcfg &= ~(1<<9);
659 // vidcfg |= (1<<8);
660 tdfx_outl(VIDPROCCFG,vidcfg);
662 // Start coord
663 //printk(KERN_DEBUG "tdfx_vid: start %dx%d\n",ov.dst_x & 0xFFF,ov.dst_y & 0xFFF);
664 tdfx_outl(VIDOVRSTARTCRD,(ov.dst_x & 0xFFF)|((ov.dst_y & 0xFFF)<<12));
665 // End coord
666 tdfx_outl(VIDOVRENDCRD, ((ov.dst_x + disp_w-1) & 0xFFF)|
667 (((ov.dst_y + disp_h-1) & 0xFFF)<<12));
668 // H Scaling
669 tdfx_outl(VIDOVRDUDX,( ((u32)ov.src_width) << 20) / ov.dst_width);
670 // Src offset and width (in bytes)
671 tdfx_outl(VIDOVRDUDXOFF,((ov.src_width<<1) & 0xFFF) << 19);
672 // V Scaling
673 tdfx_outl(VIDOVRDVDY, ( ((u32)ov.src_height) << 20) / ov.dst_height);
674 //else
675 // tdfx_outl(VIDOVRDVDY,0);
676 // V Offset
677 tdfx_outl(VIDOVRDVDYOFF,0);
678 // Overlay stride
679 stride = tdfx_inl(VIDDESKSTRIDE) & 0xFFFF;
680 tdfx_outl(VIDDESKSTRIDE,stride | (((u32)ov.src_stride) << 16));
681 // Buffers address
682 tdfx_outl(LEFTOVBUF, ov.src[0]);
683 tdfx_outl(RIGHTOVBUF, ov.src[1]);
685 // Send a swap buffer cmd if we are not on one of the 2 buffers
686 vidbuf = tdfx_inl(VIDCUROVRSTART);
687 if(vidbuf != ov.src[0] && vidbuf != ov.src[1]) {
688 tdfx_outl(SWAPPENDING,0);
689 tdfx_outl(SWAPBUFCMD, 1);
691 //printk(KERN_DEBUG "tdfx_vid: Buf0=0x%x Buf1=0x%x Current=0x%x\n",
692 // ov.src[0],ov.src[1],tdfx_inl(VIDCUROVRSTART));
693 // Colorkey
694 if(ov.use_colorkey) {
695 tdfx_outl(VIDCHRMIN,ov.colorkey[0]);
696 tdfx_outl(VIDCHRMAX,ov.colorkey[1]);
699 return 0;
702 static int tdfx_vid_overlay_on(void) {
703 uint32_t vidcfg = tdfx_inl(VIDPROCCFG);
704 //return 0;
705 if(vidcfg & (1<<8)) { // Overlay is already on
706 //printk(KERN_DEBUG "tdfx_vid: Overlay is already on.\n");
707 return -EFAULT;
709 vidcfg |= (1<<8);
710 tdfx_outl(VIDPROCCFG,vidcfg);
711 return 0;
714 static int tdfx_vid_overlay_off(void) {
715 uint32_t vidcfg = tdfx_inl(VIDPROCCFG);
717 if(vidcfg & (1<<8)) {
718 vidcfg &= ~(1<<8);
719 tdfx_outl(VIDPROCCFG,vidcfg);
720 return 0;
723 printk(KERN_DEBUG "tdfx_vid: Overlay is already off.\n");
724 return -EFAULT;
728 static int tdfx_vid_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
730 tdfx_vid_agp_move_t move;
731 tdfx_vid_config_t cfg;
732 tdfx_vid_blit_t blit;
733 u16 int16;
735 switch(cmd) {
736 case TDFX_VID_AGP_MOVE:
737 if(copy_from_user(&move,(tdfx_vid_agp_move_t*)arg,sizeof(tdfx_vid_agp_move_t))) {
738 printk(KERN_INFO "tdfx_vid:failed copy from userspace\n");
739 return -EFAULT;
741 return agp_move(&move);
742 case TDFX_VID_BUMP0:
743 if(copy_from_user(&int16,(u16*)arg,sizeof(u16))) {
744 printk(KERN_INFO "tdfx_vid:failed copy from userspace\n");
745 return -EFAULT;
747 return bump_fifo(int16);
748 case TDFX_VID_BLIT:
749 if(copy_from_user(&blit,(tdfx_vid_blit_t*)arg,sizeof(tdfx_vid_blit_t))) {
750 printk(KERN_INFO "tdfx_vid:failed copy from userspace\n");
751 return -EFAULT;
753 if(!tdfx_vid_blit(&blit)) {
754 printk(KERN_INFO "tdfx_vid: Blit failed\n");
755 return -EFAULT;
757 return 0;
758 case TDFX_VID_GET_CONFIG:
759 if(copy_from_user(&cfg,(tdfx_vid_config_t*)arg,sizeof(tdfx_vid_config_t))) {
760 printk(KERN_INFO "tdfx_vid:failed copy from userspace\n");
761 return -EFAULT;
763 tdfx_vid_get_config(&cfg);
764 if(copy_to_user((tdfx_vid_config_t*)arg,&cfg,sizeof(tdfx_vid_config_t))) {
765 printk(KERN_INFO "tdfx_vid:failed copy to userspace\n");
766 return -EFAULT;
768 return 0;
769 case TDFX_VID_SET_YUV:
770 return tdfx_vid_set_yuv(arg);
771 case TDFX_VID_GET_YUV:
772 return tdfx_vid_get_yuv(arg);
773 case TDFX_VID_SET_OVERLAY:
774 return tdfx_vid_set_overlay(arg);
775 case TDFX_VID_OVERLAY_ON:
776 return tdfx_vid_overlay_on();
777 case TDFX_VID_OVERLAY_OFF:
778 return tdfx_vid_overlay_off();
779 default:
780 printk(KERN_ERR "tdfx_vid: Invalid ioctl %d\n",cmd);
781 return -EINVAL;
783 return 0;
788 static ssize_t tdfx_vid_read(struct file *file, char *buf, size_t count, loff_t *ppos)
790 return 0;
793 static ssize_t tdfx_vid_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
796 return 0;
799 static void tdfx_vid_mopen(struct vm_area_struct *vma) {
800 int i;
801 struct page *page;
802 unsigned long phys;
804 printk(KERN_DEBUG "tdfx_vid: mopen\n");
806 for(i = 0 ; i < agp_mem->page_count ; i++) {
807 phys = agp_mem->memory[i] & ~(0x00000fff);
808 page = virt_to_page(phys_to_virt(phys));
809 if(!page) {
810 printk(KERN_DEBUG "tdfx_vid: Can't get the page %d\%d\n",i,agp_mem->page_count);
811 return;
813 get_page(page);
815 MOD_INC_USE_COUNT;
818 static void tdfx_vid_mclose(struct vm_area_struct *vma) {
819 int i;
820 struct page *page;
821 unsigned long phys;
823 printk(KERN_DEBUG "tdfx_vid: mclose\n");
825 for(i = 0 ; i < agp_mem->page_count ; i++) {
826 phys = agp_mem->memory[i] & ~(0x00000fff);
827 page = virt_to_page(phys_to_virt(phys));
828 if(!page) {
829 printk(KERN_DEBUG "tdfx_vid: Can't get the page %d\%d\n",i,agp_mem->page_count);
830 return;
832 put_page(page);
835 MOD_DEC_USE_COUNT;
838 static struct page *tdfx_vid_nopage(struct vm_area_struct *vma,
839 unsigned long address,
840 int write_access) {
841 unsigned long off;
842 uint32_t n;
843 struct page *page;
844 unsigned long phys;
846 off = address - vma->vm_start + (vma->vm_pgoff<<PAGE_SHIFT);
847 n = off / PAGE_SIZE;
849 if(n >= agp_mem->page_count) {
850 printk(KERN_DEBUG "tdfx_vid: Too far away\n");
851 return (struct page *)0UL;
853 phys = agp_mem->memory[n] & ~(0x00000fff);
854 page = virt_to_page(phys_to_virt(phys));
855 if(!page) {
856 printk(KERN_DEBUG "tdfx_vid: Can't get the page\n");
857 return (struct page *)0UL;
859 return page;
862 /* memory handler functions */
863 static struct vm_operations_struct tdfx_vid_vm_ops = {
864 open: tdfx_vid_mopen, /* mmap-open */
865 close: tdfx_vid_mclose,/* mmap-close */
866 nopage: tdfx_vid_nopage, /* no-page fault handler */
870 static int tdfx_vid_mmap(struct file *file, struct vm_area_struct *vma)
872 size_t size;
873 #ifdef MP_DEBUG
874 printk(KERN_DEBUG "tdfx_vid: mapping agp memory into userspace\n");
875 #endif
877 size = (vma->vm_end-vma->vm_start + PAGE_SIZE - 1) / PAGE_SIZE;
879 if(map_start) { // Ok we map directly in the physcal ram
880 if(size*PAGE_SIZE > map_max) {
881 printk(KERN_ERR "tdfx_vid: Not enouth mem\n");
882 return -EAGAIN;
884 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,3)
885 if(remap_page_range(vma, vma->vm_start,map_start,
886 vma->vm_end - vma->vm_start, vma->vm_page_prot))
887 #else
888 if(remap_page_range(vma->vm_start, (unsigned long)map_start,
889 vma->vm_end - vma->vm_start, vma->vm_page_prot))
890 #endif
892 printk(KERN_ERR "tdfx_vid: error mapping video memory\n");
893 return -EAGAIN;
895 printk(KERN_INFO "Physical mem 0x%lx mapped in userspace\n",map_start);
896 return 0;
899 if(agp_mem)
900 return -EAGAIN;
902 agp_mem = drm_agp->allocate_memory(size,AGP_NORMAL_MEMORY);
903 if(!agp_mem) {
904 printk(KERN_ERR "Failed to allocate AGP memory\n");
905 return -ENOMEM;
908 if(drm_agp->bind_memory(agp_mem,0)) {
909 printk(KERN_ERR "Failed to bind the AGP memory\n");
910 drm_agp->free_memory(agp_mem);
911 agp_mem = NULL;
912 return -ENOMEM;
915 printk(KERN_INFO "%d pages of AGP mem allocated (%ld/%ld bytes) :)))\n",
916 size,vma->vm_end-vma->vm_start,size*PAGE_SIZE);
919 if(tdfx_map_io) {
920 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,3)
921 if(remap_page_range(vma, vma->vm_start,agp_info.aper_base,
922 vma->vm_end - vma->vm_start, vma->vm_page_prot))
923 #else
924 if(remap_page_range(vma->vm_start, (unsigned long)agp_info.aper_base,
925 vma->vm_end - vma->vm_start, vma->vm_page_prot))
926 #endif
928 printk(KERN_ERR "tdfx_vid: error mapping video memory\n");
929 return -EAGAIN;
931 } else {
932 // Never swap it out
933 vma->vm_flags |= VM_LOCKED | VM_IO;
934 vma->vm_ops = &tdfx_vid_vm_ops;
935 vma->vm_ops->open(vma);
936 printk(KERN_INFO "Page fault handler ready !!!!!\n");
939 return 0;
943 static int tdfx_vid_release(struct inode *inode, struct file *file)
945 #ifdef MP_DEBUG
946 printk(KERN_DEBUG "tdfx_vid: Video OFF (release)\n");
947 #endif
949 // Release the agp mem
950 if(agp_mem) {
951 drm_agp->unbind_memory(agp_mem);
952 drm_agp->free_memory(agp_mem);
953 agp_mem = NULL;
956 tdfx_vid_in_use = 0;
958 MOD_DEC_USE_COUNT;
959 return 0;
962 static long long tdfx_vid_lseek(struct file *file, long long offset, int origin)
964 return -ESPIPE;
967 static int tdfx_vid_open(struct inode *inode, struct file *file)
969 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,2)
970 int minor = MINOR(inode->i_rdev.value);
971 #else
972 int minor = MINOR(inode->i_rdev);
973 #endif
975 if(minor != 0)
976 return -ENXIO;
978 if(tdfx_vid_in_use == 1)
979 return -EBUSY;
981 tdfx_vid_in_use = 1;
982 MOD_INC_USE_COUNT;
983 return 0;
986 #if LINUX_VERSION_CODE >= 0x020400
987 static struct file_operations tdfx_vid_fops =
989 llseek: tdfx_vid_lseek,
990 read: tdfx_vid_read,
991 write: tdfx_vid_write,
992 ioctl: tdfx_vid_ioctl,
993 mmap: tdfx_vid_mmap,
994 open: tdfx_vid_open,
995 release: tdfx_vid_release
997 #else
998 static struct file_operations tdfx_vid_fops =
1000 tdfx_vid_lseek,
1001 tdfx_vid_read,
1002 tdfx_vid_write,
1003 NULL,
1004 NULL,
1005 tdfx_vid_ioctl,
1006 tdfx_vid_mmap,
1007 tdfx_vid_open,
1008 NULL,
1009 tdfx_vid_release
1011 #endif
1014 int init_module(void)
1016 tdfx_vid_in_use = 0;
1018 if(register_chrdev(TDFX_VID_MAJOR, "tdfx_vid", &tdfx_vid_fops)) {
1019 printk(KERN_ERR "tdfx_vid: unable to get major: %d\n", TDFX_VID_MAJOR);
1020 return -EIO;
1023 if(!agp_init()) {
1024 printk(KERN_ERR "tdfx_vid: AGP init failed\n");
1025 unregister_chrdev(TDFX_VID_MAJOR, "tdfx_vid");
1026 return -EINVAL;
1029 if (!tdfx_vid_find_card()) {
1030 printk(KERN_ERR "tdfx_vid: no supported devices found\n");
1031 agp_close();
1032 unregister_chrdev(TDFX_VID_MAJOR, "tdfx_vid");
1033 return -EINVAL;
1038 return 0;
1042 void cleanup_module(void)
1044 if(tdfx_mmio_base)
1045 iounmap(tdfx_mmio_base);
1046 agp_close();
1047 printk(KERN_INFO "tdfx_vid: Cleaning up module\n");
1048 unregister_chrdev(TDFX_VID_MAJOR, "tdfx_vid");