2 zr36120.c - Zoran 36120/36125 based framegrabbers
4 Copyright (C) 1998-1999 Pauline Middelink <middelin@polyware.nl>
6 This program 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 This program 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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/init.h>
24 #include <linux/errno.h>
26 #include <linux/kernel.h>
27 #include <linux/major.h>
28 #include <linux/slab.h>
29 #include <linux/vmalloc.h>
31 #include <linux/pci.h>
32 #include <linux/signal.h>
34 #include <asm/pgtable.h>
36 #include <linux/sched.h>
37 #include <linux/video_decoder.h>
39 #include <asm/uaccess.h>
43 #include "zr36120_mem.h"
45 /* mark an required function argument unused - lintism */
46 #define UNUSED(x) (void)(x)
48 /* sensible default */
53 /* Anybody who uses more than four? */
56 static unsigned int triton1
=0; /* triton1 chipset? */
57 static unsigned int cardtype
[ZORAN_MAX
]={ [ 0 ... ZORAN_MAX
-1 ] = CARDTYPE
};
58 static int video_nr
= -1;
59 static int vbi_nr
= -1;
61 static struct pci_device_id zr36120_pci_tbl
[] = {
62 { PCI_VENDOR_ID_ZORAN
,PCI_DEVICE_ID_ZORAN_36120
,
63 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0 },
66 MODULE_DEVICE_TABLE(pci
, zr36120_pci_tbl
);
68 MODULE_AUTHOR("Pauline Middelink <middelin@polyware.nl>");
69 MODULE_DESCRIPTION("Zoran ZR36120 based framegrabber");
70 MODULE_LICENSE("GPL");
72 MODULE_PARM(triton1
,"i");
73 MODULE_PARM(cardtype
,"1-" __MODULE_STRING(ZORAN_MAX
) "i");
74 MODULE_PARM(video_nr
,"i");
75 MODULE_PARM(vbi_nr
,"i");
77 static int zoran_cards
;
78 static struct zoran zorans
[ZORAN_MAX
];
81 * the meaning of each element can be found in zr36120.h
82 * Determining the value of gpdir/gpval can be tricky. The
83 * best way is to run the card under the original software
84 * and read the values from the general purpose registers
85 * 0x28 and 0x2C. How you do that is left as an exercise
86 * to the impatient reader :)
88 #define T 1 /* to separate the bools from the ints */
90 static struct tvcard tvcards
[] = {
91 /* reported working by <middelin@polyware.nl> */
92 /*0*/ { "Trust Victor II",
93 2, 0, T
, T
, T
, T
, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
94 /* reported working by <Michael.Paxton@aihw.gov.au> */
95 /*1*/ { "Aitech WaveWatcher TV-PCI",
96 3, 0, T
, F
, T
, T
, 0x7F, 0x80, { 1, TUNER(3), SVHS(6) }, { 0 } },
97 /* reported working by ? */
98 /*2*/ { "Genius Video Wonder PCI Video Capture Card",
99 2, 0, T
, T
, T
, T
, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
100 /* reported working by <Pascal.Gabriel@wanadoo.fr> */
101 /*3*/ { "Guillemot Maxi-TV PCI",
102 2, 0, T
, T
, T
, T
, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
103 /* reported working by "Craig Whitmore <lennon@igrin.co.nz> */
104 /*4*/ { "Quadrant Buster",
105 3, 3, T
, F
, T
, T
, 0x7F, 0x80, { SVHS(1), TUNER(2), 3 }, { 1, 2, 3 } },
106 /* a debug entry which has all inputs mapped */
107 /*5*/ { "ZR36120 based framegrabber (all inputs enabled)",
108 6, 0, T
, T
, T
, T
, 0x7F, 0x80, { 1, 2, 3, 4, 5, 6 }, { 0 } }
112 #define NRTVCARDS (sizeof(tvcards)/sizeof(tvcards[0]))
117 #define ENDIANESS ZORAN_VFEC_LE
120 static struct { const char name
[8]; uint mode
; uint bpp
; } palette2fmt
[] = {
121 /* n/a */ { "n/a", 0, 0 },
122 /* GREY */ { "GRAY", 0, 0 },
123 /* HI240 */ { "HI240", 0, 0 },
124 /* RGB565 */ { "RGB565", ZORAN_VFEC_RGB_RGB565
|ENDIANESS
, 2 },
125 /* RGB24 */ { "RGB24", ZORAN_VFEC_RGB_RGB888
|ENDIANESS
|ZORAN_VFEC_PACK24
, 3 },
126 /* RGB32 */ { "RGB32", ZORAN_VFEC_RGB_RGB888
|ENDIANESS
, 4 },
127 /* RGB555 */ { "RGB555", ZORAN_VFEC_RGB_RGB555
|ENDIANESS
, 2 },
128 /* YUV422 */ { "YUV422", ZORAN_VFEC_RGB_YUV422
|ENDIANESS
, 2 },
129 /* YUYV */ { "YUYV", 0, 0 },
130 /* UYVY */ { "UYVY", 0, 0 },
131 /* YUV420 */ { "YUV420", 0, 0 },
132 /* YUV411 */ { "YUV411", 0, 0 },
133 /* RAW */ { "RAW", 0, 0 },
134 /* YUV422P */ { "YUV422P", 0, 0 },
135 /* YUV411P */ { "YUV411P", 0, 0 }};
136 #define NRPALETTES (sizeof(palette2fmt)/sizeof(palette2fmt[0]))
139 /* ----------------------------------------------------------------------- */
140 /* ZORAN chipset detector */
141 /* shamelessly stolen from bttv.c */
142 /* Reason for beeing here: we need to detect if we are running on a */
143 /* Triton based chipset, and if so, enable a certain bit */
144 /* ----------------------------------------------------------------------- */
146 void __init
handle_chipset(void)
148 struct pci_dev
*dev
= NULL
;
150 /* Just in case some nut set this to something dangerous */
152 triton1
= ZORAN_VDC_TRICOM
;
154 while ((dev
= pci_find_device(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82437
, dev
)))
156 printk(KERN_INFO
"zoran: Host bridge 82437FX Triton PIIX\n");
157 triton1
= ZORAN_VDC_TRICOM
;
161 /* ----------------------------------------------------------------------- */
162 /* ZORAN functions */
163 /* ----------------------------------------------------------------------- */
165 static void zoran_set_geo(struct zoran
* ztv
, struct vidinfo
* i
);
169 void zoran_dump(struct zoran
*ztv
)
172 char *p
=str
; /* shut up, gcc! */
175 for (i
=0; i
<0x60; i
+=4) {
177 if (i
) printk("%s\n",str
);
179 p
+= sprintf(str
, KERN_DEBUG
" %04x: ",i
);
181 p
+= sprintf(p
, "%08x ",zrread(i
));
187 void reap_states(struct zoran
* ztv
)
193 * Are we busy at all?
194 * This depends on if there is a workqueue AND the
195 * videotransfer is enabled on the chip...
197 if (ztv
->workqueue
&& (zrread(ZORAN_VDC
) & ZORAN_VDC_VIDEN
))
199 struct vidinfo
* newitem
;
201 /* did we get a complete frame? */
202 if (zrread(ZORAN_VSTR
) & ZORAN_VSTR_GRAB
)
205 DEBUG(printk(CARD_DEBUG
"completed %s at %p\n",CARD
,ztv
->workqueue
->kindof
==FBUFFER_GRAB
?"grab":"read",ztv
->workqueue
));
207 /* we are done with this buffer, tell everyone */
208 ztv
->workqueue
->status
= FBUFFER_DONE
;
209 ztv
->workqueue
->fieldnr
= ztv
->fieldnr
;
210 /* not good, here for BTTV_FIELDNR reasons */
211 ztv
->lastfieldnr
= ztv
->fieldnr
;
213 switch (ztv
->workqueue
->kindof
) {
215 wake_up_interruptible(&ztv
->grabq
);
218 wake_up_interruptible(&ztv
->vbiq
);
221 printk(CARD_INFO
"somebody killed the workqueue (kindof=%d)!\n",CARD
,ztv
->workqueue
->kindof
);
224 /* item completed, skip to next item in queue */
225 write_lock(&ztv
->lock
);
226 newitem
= ztv
->workqueue
->next
;
227 ztv
->workqueue
->next
= 0; /* mark completed */
228 ztv
->workqueue
= newitem
;
229 write_unlock(&ztv
->lock
);
233 * ok, so it seems we have nothing in progress right now.
234 * Lets see if we can find some work.
238 struct vidinfo
* newitem
;
241 DEBUG(printk(CARD_DEBUG
"starting %s at %p\n",CARD
,ztv
->workqueue
->kindof
==FBUFFER_GRAB
?"grab":"read",ztv
->workqueue
));
243 /* loadup the frame settings */
244 read_lock(&ztv
->lock
);
245 zoran_set_geo(ztv
,ztv
->workqueue
);
246 read_unlock(&ztv
->lock
);
248 switch (ztv
->workqueue
->kindof
) {
251 zrand(~ZORAN_OCR_OVLEN
, ZORAN_OCR
);
252 zror(ZORAN_VSTR_SNAPSHOT
,ZORAN_VSTR
);
253 zror(ZORAN_VDC_VIDEN
,ZORAN_VDC
);
255 /* start single-shot grab */
256 zror(ZORAN_VSTR_GRAB
, ZORAN_VSTR
);
259 printk(CARD_INFO
"what is this doing on the queue? (kindof=%d)\n",CARD
,ztv
->workqueue
->kindof
);
260 write_lock(&ztv
->lock
);
261 newitem
= ztv
->workqueue
->next
;
262 ztv
->workqueue
->next
= 0;
263 ztv
->workqueue
= newitem
;
264 write_unlock(&ztv
->lock
);
266 goto again
; /* yeah, sure.. */
271 DEBUG(printk(CARD_DEBUG
"nothing in queue\n",CARD
));
274 * What? Even the workqueue is empty? Am i really here
275 * for nothing? Did i come all that way to... do nothing?
278 /* do we need to overlay? */
279 if (test_bit(STATE_OVERLAY
, &ztv
->state
))
281 /* are we already overlaying? */
282 if (!(zrread(ZORAN_OCR
) & ZORAN_OCR_OVLEN
) ||
283 !(zrread(ZORAN_VDC
) & ZORAN_VDC_VIDEN
))
285 DEBUG(printk(CARD_DEBUG
"starting overlay\n",CARD
));
287 read_lock(&ztv
->lock
);
288 zoran_set_geo(ztv
,&ztv
->overinfo
);
289 read_unlock(&ztv
->lock
);
291 zror(ZORAN_OCR_OVLEN
, ZORAN_OCR
);
292 zrand(~ZORAN_VSTR_SNAPSHOT
,ZORAN_VSTR
);
293 zror(ZORAN_VDC_VIDEN
,ZORAN_VDC
);
297 * leave overlaying on, but turn interrupts off.
299 zrand(~ZORAN_ICR_EN
,ZORAN_ICR
);
303 /* do we have any VBI idle time processing? */
304 if (test_bit(STATE_VBI
, &ztv
->state
))
306 struct vidinfo
* item
;
307 struct vidinfo
* lastitem
;
309 /* protect the workqueue */
310 write_lock(&ztv
->lock
);
311 lastitem
= ztv
->workqueue
;
313 while (lastitem
->next
) lastitem
= lastitem
->next
;
314 for (item
=ztv
->readinfo
; item
!=ztv
->readinfo
+ZORAN_VBI_BUFFERS
; item
++)
315 if (item
->next
== 0 && item
->status
== FBUFFER_FREE
)
317 DEBUG(printk(CARD_DEBUG
"%p added to queue\n",CARD
,item
));
318 item
->status
= FBUFFER_BUSY
;
320 ztv
->workqueue
= item
;
322 lastitem
->next
= item
;
325 write_unlock(&ztv
->lock
);
327 goto again
; /* hey, _i_ graduated :) */
331 * Then we must be realy IDLE
333 DEBUG(printk(CARD_DEBUG
"turning off\n",CARD
));
334 /* nothing further to do, disable DMA and further IRQs */
335 zrand(~ZORAN_VDC_VIDEN
,ZORAN_VDC
);
336 zrand(~ZORAN_ICR_EN
,ZORAN_ICR
);
340 void zoran_irq(int irq
, void *dev_id
, struct pt_regs
* regs
)
344 struct zoran
*ztv
= (struct zoran
*)dev_id
;
346 UNUSED(irq
); UNUSED(regs
);
348 /* get/clear interrupt status bits */
349 stat
=zrread(ZORAN_ISR
);
350 estat
=stat
& zrread(ZORAN_ICR
);
353 zrwrite(estat
,ZORAN_ISR
);
354 IDEBUG(printk(CARD_DEBUG
"estat %08x\n",CARD
,estat
));
355 IDEBUG(printk(CARD_DEBUG
" stat %08x\n",CARD
,stat
));
357 if (estat
& ZORAN_ISR_CODE
)
359 IDEBUG(printk(CARD_DEBUG
"CodReplIRQ\n",CARD
));
361 if (estat
& ZORAN_ISR_GIRQ0
)
363 IDEBUG(printk(CARD_DEBUG
"GIRQ0\n",CARD
));
364 if (!ztv
->card
->usegirq1
)
367 if (estat
& ZORAN_ISR_GIRQ1
)
369 IDEBUG(printk(CARD_DEBUG
"GIRQ1\n",CARD
));
370 if (ztv
->card
->usegirq1
)
376 printk(CARD_ERR
"irq loop %d (%x)\n",CARD
,count
,estat
);
379 zrwrite(0, ZORAN_ICR
);
380 printk(CARD_ERR
"IRQ lockup, cleared int mask\n",CARD
);
386 int zoran_muxsel(struct zoran
* ztv
, int channel
, int norm
)
390 /* set the new video norm */
391 rv
= i2c_control_device(&(ztv
->i2c
), I2C_DRIVERID_VIDEODECODER
, DECODER_SET_NORM
, &norm
);
396 /* map the given channel to the cards decoder's channel */
397 channel
= ztv
->card
->video_mux
[channel
] & CHANNEL_MASK
;
399 /* set the new channel */
400 rv
= i2c_control_device(&(ztv
->i2c
), I2C_DRIVERID_VIDEODECODER
, DECODER_SET_INPUT
, &channel
);
404 /* Tell the interrupt handler what to to. */
406 void zoran_cap(struct zoran
* ztv
, int on
)
408 DEBUG(printk(CARD_DEBUG
"zoran_cap(%d) state=%x\n",CARD
,on
,ztv
->state
));
414 * turn interrupts (back) on. The DMA will be enabled
415 * inside the irq handler when it detects a restart.
417 zror(ZORAN_ICR_EN
,ZORAN_ICR
);
421 * turn both interrupts and DMA off
423 zrand(~ZORAN_VDC_VIDEN
,ZORAN_VDC
);
424 zrand(~ZORAN_ICR_EN
,ZORAN_ICR
);
430 static ulong dmask
[] = {
431 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFC, 0xFFFFFFF8,
432 0xFFFFFFF0, 0xFFFFFFE0, 0xFFFFFFC0, 0xFFFFFF80,
433 0xFFFFFF00, 0xFFFFFE00, 0xFFFFFC00, 0xFFFFF800,
434 0xFFFFF000, 0xFFFFE000, 0xFFFFC000, 0xFFFF8000,
435 0xFFFF0000, 0xFFFE0000, 0xFFFC0000, 0xFFF80000,
436 0xFFF00000, 0xFFE00000, 0xFFC00000, 0xFF800000,
437 0xFF000000, 0xFE000000, 0xFC000000, 0xF8000000,
438 0xF0000000, 0xE0000000, 0xC0000000, 0x80000000
442 void zoran_built_overlay(struct zoran
* ztv
, int count
, struct video_clip
*vcp
)
445 int ystep
= (ztv
->vidXshift
+ ztv
->vidWidth
+31)/32; /* next DWORD */
448 DEBUG(printk(KERN_DEBUG
" overlay at %p, ystep=%d, clips=%d\n",ztv
->overinfo
.overlay
,ystep
,count
));
450 for (i
=0; i
<count
; i
++) {
451 struct video_clip
*vp
= vcp
+i
;
453 DEBUG(printk(KERN_DEBUG
" %d: clip(%d,%d,%d,%d)\n", i
,vp
->x
,vp
->y
,vp
->width
,vp
->height
));
457 * activate the visible portion of the screen
458 * Note we take some shortcuts here, because we
459 * know the width can never be < 32. (I.e. a DWORD)
460 * We also assume the overlay starts somewhere in
464 int start
= ztv
->vidXshift
;
465 ulong firstd
= dmask
[start
];
466 ulong lastd
= ~dmask
[(start
+ ztv
->overinfo
.w
) & 31];
467 mtop
= ztv
->overinfo
.overlay
;
468 for (i
=0; i
<ztv
->overinfo
.h
; i
++) {
469 int w
= ztv
->vidWidth
;
475 memset(line
, ~0, w
/8);
482 /* process clipping regions */
483 for (i
=0; i
<count
; i
++) {
485 if (vcp
->x
< 0 || (uint
)vcp
->x
> ztv
->overinfo
.w
||
486 vcp
->y
< 0 || vcp
->y
> ztv
->overinfo
.h
||
487 vcp
->width
< 0 || (uint
)(vcp
->x
+vcp
->width
) > ztv
->overinfo
.w
||
488 vcp
->height
< 0 || (vcp
->y
+vcp
->height
) > ztv
->overinfo
.h
)
490 DEBUG(printk(CARD_DEBUG
"invalid clipzone (%d,%d,%d,%d) not in (0,0,%d,%d), adapting\n",CARD
,vcp
->x
,vcp
->y
,vcp
->width
,vcp
->height
,ztv
->overinfo
.w
,ztv
->overinfo
.h
));
491 if (vcp
->x
< 0) vcp
->x
= 0;
492 if ((uint
)vcp
->x
> ztv
->overinfo
.w
) vcp
->x
= ztv
->overinfo
.w
;
493 if (vcp
->y
< 0) vcp
->y
= 0;
494 if (vcp
->y
> ztv
->overinfo
.h
) vcp
->y
= ztv
->overinfo
.h
;
495 if (vcp
->width
< 0) vcp
->width
= 0;
496 if ((uint
)(vcp
->x
+vcp
->width
) > ztv
->overinfo
.w
) vcp
->width
= ztv
->overinfo
.w
- vcp
->x
;
497 if (vcp
->height
< 0) vcp
->height
= 0;
498 if (vcp
->y
+vcp
->height
> ztv
->overinfo
.h
) vcp
->height
= ztv
->overinfo
.h
- vcp
->y
;
502 mtop
= &ztv
->overinfo
.overlay
[vcp
->y
*ystep
];
503 for (h
=0; h
<=vcp
->height
; h
++) {
505 int x
= ztv
->vidXshift
+ vcp
->x
;
506 for (w
=0; w
<=vcp
->width
; w
++) {
507 clear_bit(x
&31, &mtop
[x
/32]);
515 mtop
= ztv
->overinfo
.overlay
;
516 zrwrite(virt_to_bus(mtop
), ZORAN_MTOP
);
517 zrwrite(virt_to_bus(mtop
+ystep
), ZORAN_MBOT
);
518 zraor((ztv
->vidInterlace
*ystep
)<<0,~ZORAN_OCR_MASKSTRIDE
,ZORAN_OCR
);
523 u16 Wt
, Wa
, Ht
, Ha
, HStart
, VStart
;
526 static struct tvnorm tvnorms
[] = {
528 /* { 864, 720, 625, 576, 131, 21 },*/
529 /*00*/ { 864, 768, 625, 576, 81, 17 },
531 /*01*/ { 858, 720, 525, 480, 121, 10 },
533 /*02*/ { 864, 720, 625, 576, 131, 21 },
535 /*03*/ { 864, 720, 625, 576, 131, 21 },
537 /*04*/ { 858, 720, 525, 480, 121, 10 }
539 #define TVNORMS (sizeof(tvnorms)/sizeof(tvnorm))
542 * Program the chip for a setup as described in the vidinfo struct.
544 * Side-effects: calculates vidXshift, vidInterlace,
545 * vidHeight, vidWidth which are used in a later stage
546 * to calculate the overlay mask
548 * This is an internal function, as such it does not check the
549 * validity of the struct members... Spectaculair crashes will
550 * follow /very/ quick when you're wrong and the chip right :)
553 void zoran_set_geo(struct zoran
* ztv
, struct vidinfo
* i
)
557 int winWidth
, winHeight
;
558 int maxWidth
, maxHeight
, maxXOffset
, maxYOffset
;
561 DEBUG(printk(CARD_DEBUG
"set_geo(rect=(%d,%d,%d,%d), norm=%d, format=%d, bpp=%d, bpl=%d, busadr=%lx, overlay=%p)\n",CARD
,i
->x
,i
->y
,i
->w
,i
->h
,ztv
->norm
,i
->format
,i
->bpp
,i
->bpl
,i
->busadr
,i
->overlay
));
564 * make sure the DMA transfers are inhibited during our
565 * reprogramming of the chip
567 zrand(~ZORAN_VDC_VIDEN
,ZORAN_VDC
);
569 maxWidth
= tvnorms
[ztv
->norm
].Wa
;
570 maxHeight
= tvnorms
[ztv
->norm
].Ha
/2;
571 maxXOffset
= tvnorms
[ztv
->norm
].HStart
;
572 maxYOffset
= tvnorms
[ztv
->norm
].VStart
;
574 /* setup vfec register (keep ExtFl,TopField and VCLKPol settings) */
575 vfec
= (zrread(ZORAN_VFEC
) & (ZORAN_VFEC_EXTFL
|ZORAN_VFEC_TOPFIELD
|ZORAN_VFEC_VCLKPOL
)) |
576 (palette2fmt
[i
->format
].mode
& (ZORAN_VFEC_RGB
|ZORAN_VFEC_ERRDIF
|ZORAN_VFEC_LE
|ZORAN_VFEC_PACK24
));
579 * Set top, bottom ptrs. Since these must be DWORD aligned,
580 * possible adjust the x and the width of the window.
581 * so the endposition stay the same. The vidXshift will make
582 * sure we are not writing pixels before the requested x.
587 winWidth
= -winWidth
;
588 top
= i
->busadr
+ i
->x
*i
->bpp
+ i
->y
*i
->bpl
;
590 ztv
->vidXshift
= (top
& 3) / i
->bpp
;
591 winWidth
+= ztv
->vidXshift
;
592 DEBUG(printk(KERN_DEBUG
" window-x shifted %d pixels left\n",ztv
->vidXshift
));
597 * bottom points to next frame but in interleaved mode we want
598 * to 'mix' the 2 frames to one capture, so 'bot' points to one
599 * (physical) line below the top line.
602 zrwrite(top
,ZORAN_VTOP
);
603 zrwrite(bot
,ZORAN_VBOT
);
606 * Make sure the winWidth is DWORD aligned too,
607 * thereby automaticly making sure the stride to the
608 * next line is DWORD aligned too (as required by spec).
610 if ((winWidth
*i
->bpp
) & 3) {
611 DEBUG(printk(KERN_DEBUG
" window-width enlarged by %d pixels\n",(winWidth
*i
->bpp
) & 3));
612 winWidth
+= (winWidth
*i
->bpp
) & 3;
615 /* determine the DispMode and stride */
616 if (i
->h
>= 0 && i
->h
<= maxHeight
) {
617 /* single frame grab suffices for this height. */
618 vfec
|= ZORAN_VFEC_DISPMOD
;
619 ztv
->vidInterlace
= 0;
620 stride
= i
->bpl
- (winWidth
*i
->bpp
);
624 /* interleaving needed for this height */
625 ztv
->vidInterlace
= 1;
626 stride
= i
->bpl
*2 - (winWidth
*i
->bpp
);
629 if (winHeight
< 0) /* can happen for VBI! */
630 winHeight
= -winHeight
;
632 /* safety net, sometimes bpl is too short??? */
634 DEBUG(printk(CARD_DEBUG
"WARNING stride = %d\n",CARD
,stride
));
638 zraor((winHeight
<<12)|(winWidth
<<0),~(ZORAN_VDC_VIDWINHT
|ZORAN_VDC_VIDWINWID
), ZORAN_VDC
);
639 zraor(stride
<<16,~ZORAN_VSTR_DISPSTRIDE
,ZORAN_VSTR
);
641 /* remember vidWidth, vidHeight for overlay calculations */
642 ztv
->vidWidth
= winWidth
;
643 ztv
->vidHeight
= winHeight
;
644 DEBUG(printk(KERN_DEBUG
" top=%08lx, bottom=%08lx\n",top
,bot
));
645 DEBUG(printk(KERN_DEBUG
" winWidth=%d, winHeight=%d\n",winWidth
,winHeight
));
646 DEBUG(printk(KERN_DEBUG
" maxWidth=%d, maxHeight=%d\n",maxWidth
,maxHeight
));
647 DEBUG(printk(KERN_DEBUG
" stride=%d\n",stride
));
650 * determine horizontal scales and crops
654 int Hend
= Hstart
+ winWidth
;
655 DEBUG(printk(KERN_DEBUG
" Y: scale=0, start=%d, end=%d\n", Hstart
, Hend
));
656 zraor((Hstart
<<10)|(Hend
<<0),~(ZORAN_VFEH_HSTART
|ZORAN_VFEH_HEND
),ZORAN_VFEH
);
660 int X
= (winWidth
*64+Wa
-1)/Wa
;
661 int We
= winWidth
*64/X
;
663 int hcrop1
= 2*(Wa
-We
)/4;
665 * BUGFIX: Juha Nurmela <junki@qn-lpr2-165.quicknet.inet.fi>
666 * found the solution to the color phase shift.
667 * See ChangeLog for the full explanation)
669 int Hstart
= (maxXOffset
+ hcrop1
) | 1;
670 int Hend
= Hstart
+ We
- 1;
672 DEBUG(printk(KERN_DEBUG
" X: scale=%d, start=%d, end=%d\n", HorDcm
, Hstart
, Hend
));
674 zraor((Hstart
<<10)|(Hend
<<0),~(ZORAN_VFEH_HSTART
|ZORAN_VFEH_HEND
),ZORAN_VFEH
);
678 vfec
|= ZORAN_VFEC_HFILTER_1
; /* no filter */
680 vfec
|= ZORAN_VFEC_HFILTER_3
; /* 3 tap filter */
682 vfec
|= ZORAN_VFEC_HFILTER_4
; /* 4 tap filter */
683 else vfec
|= ZORAN_VFEC_HFILTER_5
; /* 5 tap filter */
687 * Determine vertical scales and crops
689 * when height is negative, we want to read starting at line 0
690 * One day someone might need access to these lines...
694 int Vend
= Vstart
+ winHeight
;
695 DEBUG(printk(KERN_DEBUG
" Y: scale=0, start=%d, end=%d\n", Vstart
, Vend
));
696 zraor((Vstart
<<10)|(Vend
<<0),~(ZORAN_VFEV_VSTART
|ZORAN_VFEV_VEND
),ZORAN_VFEV
);
700 int Y
= (winHeight
*64+Ha
-1)/Ha
;
701 int He
= winHeight
*64/Y
;
703 int vcrop1
= 2*(Ha
-He
)/4;
704 int Vstart
= maxYOffset
+ vcrop1
;
705 int Vend
= Vstart
+ He
- 1;
707 DEBUG(printk(KERN_DEBUG
" Y: scale=%d, start=%d, end=%d\n", VerDcm
, Vstart
, Vend
));
708 zraor((Vstart
<<10)|(Vend
<<0),~(ZORAN_VFEV_VSTART
|ZORAN_VFEV_VEND
),ZORAN_VFEV
);
712 DEBUG(printk(KERN_DEBUG
" F: format=%d(=%s)\n",i
->format
,palette2fmt
[i
->format
].name
));
714 /* setup the requested format */
715 zrwrite(vfec
, ZORAN_VFEC
);
719 void zoran_common_open(struct zoran
* ztv
, int flags
)
723 /* already opened? */
724 if (ztv
->users
++ != 0)
732 /* setup the encoder to the initial values */
733 ztv
->picture
.colour
=254<<7;
734 ztv
->picture
.brightness
=128<<8;
735 ztv
->picture
.hue
=128<<8;
736 ztv
->picture
.contrast
=216<<7;
737 i2c_control_device(&ztv
->i2c
, I2C_DRIVERID_VIDEODECODER
, DECODER_SET_PICTURE
, &ztv
->picture
);
739 /* default to the composite input since my camera is there */
740 zoran_muxsel(ztv
, 0, VIDEO_MODE_PAL
);
744 void zoran_common_close(struct zoran
* ztv
)
746 if (--ztv
->users
!= 0)
757 * Open a zoran card. Right now the flags are just a hack
759 static int zoran_open(struct video_device
*dev
, int flags
)
761 struct zoran
*ztv
= (struct zoran
*)dev
;
762 struct vidinfo
* item
;
765 DEBUG(printk(CARD_DEBUG
"open(dev,%d)\n",CARD
,flags
));
767 /*********************************************
768 * We really should be doing lazy allocing...
769 *********************************************/
770 /* allocate a frame buffer */
772 ztv
->fbuffer
= bmalloc(ZORAN_MAX_FBUFSIZE
);
774 /* could not get a buffer, bail out */
777 /* at this time we _always_ have a framebuffer */
778 memset(ztv
->fbuffer
,0,ZORAN_MAX_FBUFSIZE
);
780 if (!ztv
->overinfo
.overlay
)
781 ztv
->overinfo
.overlay
= (void*)kmalloc(1024*1024/8, GFP_KERNEL
);
782 if (!ztv
->overinfo
.overlay
) {
783 /* could not get an overlay buffer, bail out */
784 bfree(ztv
->fbuffer
, ZORAN_MAX_FBUFSIZE
);
787 /* at this time we _always_ have a overlay */
789 /* clear buffer status, and give them a DMAable address */
791 for (item
=ztv
->grabinfo
; item
!=ztv
->grabinfo
+ZORAN_MAX_FBUFFERS
; item
++)
793 item
->status
= FBUFFER_FREE
;
795 item
->busadr
= virt_to_bus(pos
);
796 pos
+= ZORAN_MAX_FBUFFER
;
799 /* do the common part of all open's */
800 zoran_common_open(ztv
, flags
);
806 void zoran_close(struct video_device
* dev
)
808 struct zoran
*ztv
= (struct zoran
*)dev
;
810 DEBUG(printk(CARD_DEBUG
"close(dev)\n",CARD
));
812 /* driver specific closure */
813 clear_bit(STATE_OVERLAY
, &ztv
->state
);
815 zoran_common_close(ztv
);
818 * This is sucky but right now I can't find a good way to
819 * be sure its safe to free the buffer. We wait 5-6 fields
820 * which is more than sufficient to be sure.
822 current
->state
= TASK_UNINTERRUPTIBLE
;
823 schedule_timeout(HZ
/10); /* Wait 1/10th of a second */
825 /* free the allocated framebuffer */
827 bfree( ztv
->fbuffer
, ZORAN_MAX_FBUFSIZE
);
829 if (ztv
->overinfo
.overlay
)
830 kfree( ztv
->overinfo
.overlay
);
831 ztv
->overinfo
.overlay
= 0;
836 * This read function could be used reentrant in a SMP situation.
838 * This is made possible by the spinlock which is kept till we
839 * found and marked a buffer for our own use. The lock must
840 * be released as soon as possible to prevent lock contention.
843 long zoran_read(struct video_device
* dev
, char* buf
, unsigned long count
, int nonblock
)
845 struct zoran
*ztv
= (struct zoran
*)dev
;
847 struct vidinfo
* unused
= 0;
848 struct vidinfo
* done
= 0;
850 DEBUG(printk(CARD_DEBUG
"zoran_read(%p,%ld,%d)\n",CARD
,buf
,count
,nonblock
));
852 /* find ourself a free or completed buffer */
854 struct vidinfo
* item
;
856 write_lock_irq(&ztv
->lock
);
857 for (item
=ztv
->grabinfo
; item
!=ztv
->grabinfo
+ZORAN_MAX_FBUFFERS
; item
++)
859 if (!unused
&& item
->status
== FBUFFER_FREE
)
861 if (!done
&& item
->status
== FBUFFER_DONE
)
867 /* no more free buffers, wait for them. */
868 write_unlock_irq(&ztv
->lock
);
871 interruptible_sleep_on(&ztv
->grabq
);
872 if (signal_pending(current
))
876 /* Do we have 'ready' data? */
878 /* no? than this will take a while... */
880 write_unlock_irq(&ztv
->lock
);
884 /* mark the unused buffer as wanted */
885 unused
->status
= FBUFFER_BUSY
;
888 unused
->format
= VIDEO_PALETTE_RGB24
;
889 unused
->bpp
= palette2fmt
[unused
->format
].bpp
;
890 unused
->bpl
= unused
->w
* unused
->bpp
;
892 { /* add to tail of queue */
893 struct vidinfo
* oldframe
= ztv
->workqueue
;
894 if (!oldframe
) ztv
->workqueue
= unused
;
896 while (oldframe
->next
) oldframe
= oldframe
->next
;
897 oldframe
->next
= unused
;
900 write_unlock_irq(&ztv
->lock
);
902 /* tell the state machine we want it filled /NOW/ */
905 /* wait till this buffer gets grabbed */
906 while (unused
->status
== FBUFFER_BUSY
) {
907 interruptible_sleep_on(&ztv
->grabq
);
908 /* see if a signal did it */
909 if (signal_pending(current
))
915 write_unlock_irq(&ztv
->lock
);
917 /* Yes! we got data! */
918 max
= done
->bpl
* done
->h
;
921 if (copy_to_user((void*)buf
, done
->memadr
, count
))
924 /* keep the engine running */
925 done
->status
= FBUFFER_FREE
;
928 /* tell listeners this buffer became free */
929 wake_up_interruptible(&ztv
->grabq
);
932 DEBUG(printk(CARD_DEBUG
"zoran_read() returns %lu\n",CARD
,count
));
937 long zoran_write(struct video_device
* dev
, const char* buf
, unsigned long count
, int nonblock
)
939 struct zoran
*ztv
= (struct zoran
*)dev
;
940 UNUSED(ztv
); UNUSED(dev
); UNUSED(buf
); UNUSED(count
); UNUSED(nonblock
);
941 DEBUG(printk(CARD_DEBUG
"zoran_write\n",CARD
));
946 unsigned int zoran_poll(struct video_device
*dev
, struct file
*file
, poll_table
*wait
)
948 struct zoran
*ztv
= (struct zoran
*)dev
;
949 struct vidinfo
* item
;
950 unsigned int mask
= 0;
952 poll_wait(file
, &ztv
->grabq
, wait
);
954 for (item
=ztv
->grabinfo
; item
!=ztv
->grabinfo
+ZORAN_MAX_FBUFFERS
; item
++)
955 if (item
->status
== FBUFFER_DONE
)
957 mask
|= (POLLIN
| POLLRDNORM
);
961 DEBUG(printk(CARD_DEBUG
"zoran_poll()=%x\n",CARD
,mask
));
966 /* append a new clipregion to the vector of video_clips */
968 void new_clip(struct video_window
* vw
, struct video_clip
* vcp
, int x
, int y
, int w
, int h
)
970 vcp
[vw
->clipcount
].x
= x
;
971 vcp
[vw
->clipcount
].y
= y
;
972 vcp
[vw
->clipcount
].width
= w
;
973 vcp
[vw
->clipcount
].height
= h
;
978 int zoran_ioctl(struct video_device
* dev
, unsigned int cmd
, void *arg
)
980 struct zoran
* ztv
= (struct zoran
*)dev
;
985 struct video_capability c
;
986 DEBUG(printk(CARD_DEBUG
"VIDIOCGCAP\n",CARD
));
988 strcpy(c
.name
,ztv
->video_dev
.name
);
989 c
.type
= VID_TYPE_CAPTURE
|
995 c
.type
|= VID_TYPE_TUNER
;
996 if (ztv
->have_decoder
) {
997 c
.channels
= ztv
->card
->video_inputs
;
998 c
.audios
= ztv
->card
->audio_inputs
;
1000 /* no decoder -> no channels */
1001 c
.channels
= c
.audios
= 0;
1006 if (copy_to_user(arg
,&c
,sizeof(c
)))
1013 struct video_channel v
;
1015 if (copy_from_user(&v
, arg
,sizeof(v
)))
1017 DEBUG(printk(CARD_DEBUG
"VIDIOCGCHAN(%d)\n",CARD
,v
.channel
));
1018 v
.flags
=VIDEO_VC_AUDIO
1019 #ifdef VIDEO_VC_NORM
1024 v
.type
=VIDEO_TYPE_CAMERA
;
1025 #ifdef I_EXPECT_POSSIBLE_NORMS_IN_THE_API
1026 v
.norm
=VIDEO_MODE_PAL
|
1030 v
.norm
=VIDEO_MODE_PAL
;
1032 /* too many inputs? no decoder -> no channels */
1033 if (!ztv
->have_decoder
|| v
.channel
< 0 || v
.channel
>= ztv
->card
->video_inputs
)
1036 /* now determine the name of the channel */
1037 mux
= ztv
->card
->video_mux
[v
.channel
];
1038 if (mux
& IS_TUNER
) {
1039 /* lets assume only one tuner, yes? */
1040 strcpy(v
.name
,"Television");
1041 v
.type
= VIDEO_TYPE_TV
;
1042 if (ztv
->have_tuner
) {
1043 v
.flags
|= VIDEO_VC_TUNER
;
1047 else if (mux
& IS_SVHS
)
1048 sprintf(v
.name
,"S-Video-%d",v
.channel
);
1050 sprintf(v
.name
,"CVBS-%d",v
.channel
);
1052 if (copy_to_user(arg
,&v
,sizeof(v
)))
1057 { /* set video channel */
1058 struct video_channel v
;
1059 if (copy_from_user(&v
, arg
,sizeof(v
)))
1061 DEBUG(printk(CARD_DEBUG
"VIDIOCSCHAN(%d,%d)\n",CARD
,v
.channel
,v
.norm
));
1063 /* too many inputs? no decoder -> no channels */
1064 if (!ztv
->have_decoder
|| v
.channel
>= ztv
->card
->video_inputs
|| v
.channel
< 0)
1067 if (v
.norm
!= VIDEO_MODE_PAL
&&
1068 v
.norm
!= VIDEO_MODE_NTSC
&&
1069 v
.norm
!= VIDEO_MODE_SECAM
&&
1070 v
.norm
!= VIDEO_MODE_AUTO
)
1073 /* make it happen, nr1! */
1074 return zoran_muxsel(ztv
,v
.channel
,v
.norm
);
1079 struct video_tuner v
;
1080 if (copy_from_user(&v
, arg
,sizeof(v
)))
1082 DEBUG(printk(CARD_DEBUG
"VIDIOCGTUNER(%d)\n",CARD
,v
.tuner
));
1084 /* Only no or one tuner for now */
1085 if (!ztv
->have_tuner
|| v
.tuner
)
1088 strcpy(v
.name
,"Television");
1091 v
.flags
= VIDEO_TUNER_PAL
|VIDEO_TUNER_NTSC
|VIDEO_TUNER_SECAM
;
1093 v
.signal
= 0xFFFF; /* unknown */
1095 if (copy_to_user(arg
,&v
,sizeof(v
)))
1101 struct video_tuner v
;
1102 if (copy_from_user(&v
, arg
, sizeof(v
)))
1104 DEBUG(printk(CARD_DEBUG
"VIDIOCSTUNER(%d,%d)\n",CARD
,v
.tuner
,v
.mode
));
1106 /* Only no or one tuner for now */
1107 if (!ztv
->have_tuner
|| v
.tuner
)
1110 /* and it only has certain valid modes */
1111 if( v
.mode
!= VIDEO_MODE_PAL
&&
1112 v
.mode
!= VIDEO_MODE_NTSC
&&
1113 v
.mode
!= VIDEO_MODE_SECAM
)
1117 return zoran_muxsel(ztv
,v
.tuner
,v
.mode
);
1122 struct video_picture p
= ztv
->picture
;
1123 DEBUG(printk(CARD_DEBUG
"VIDIOCGPICT\n",CARD
));
1124 p
.depth
= ztv
->depth
;
1126 case 8: p
.palette
=VIDEO_PALETTE_YUV422
;
1128 case 15: p
.palette
=VIDEO_PALETTE_RGB555
;
1130 case 16: p
.palette
=VIDEO_PALETTE_RGB565
;
1132 case 24: p
.palette
=VIDEO_PALETTE_RGB24
;
1134 case 32: p
.palette
=VIDEO_PALETTE_RGB32
;
1137 if (copy_to_user(arg
, &p
, sizeof(p
)))
1143 struct video_picture p
;
1144 if (copy_from_user(&p
, arg
,sizeof(p
)))
1146 DEBUG(printk(CARD_DEBUG
"VIDIOCSPICT(%d,%d,%d,%d,%d,%d,%d)\n",CARD
,p
.brightness
,p
.hue
,p
.colour
,p
.contrast
,p
.whiteness
,p
.depth
,p
.palette
));
1148 /* depth must match with framebuffer */
1149 if (p
.depth
!= ztv
->depth
)
1152 /* check if palette matches this bpp */
1153 if (p
.palette
>NRPALETTES
||
1154 palette2fmt
[p
.palette
].bpp
!= ztv
->overinfo
.bpp
)
1157 write_lock_irq(&ztv
->lock
);
1158 ztv
->overinfo
.format
= p
.palette
;
1160 write_unlock_irq(&ztv
->lock
);
1162 /* tell the decoder */
1163 i2c_control_device(&ztv
->i2c
, I2C_DRIVERID_VIDEODECODER
, DECODER_SET_PICTURE
, &p
);
1169 struct video_window vw
;
1170 DEBUG(printk(CARD_DEBUG
"VIDIOCGWIN\n",CARD
));
1171 read_lock(&ztv
->lock
);
1172 vw
.x
= ztv
->overinfo
.x
;
1173 vw
.y
= ztv
->overinfo
.y
;
1174 vw
.width
= ztv
->overinfo
.w
;
1175 vw
.height
= ztv
->overinfo
.h
;
1178 if (ztv
->vidInterlace
)
1179 vw
.flags
|=VIDEO_WINDOW_INTERLACE
;
1180 read_unlock(&ztv
->lock
);
1181 if (copy_to_user(arg
,&vw
,sizeof(vw
)))
1187 struct video_window vw
;
1188 struct video_clip
*vcp
;
1190 if (copy_from_user(&vw
,arg
,sizeof(vw
)))
1192 DEBUG(printk(CARD_DEBUG
"VIDIOCSWIN(%d,%d,%d,%d,%x,%d)\n",CARD
,vw
.x
,vw
.y
,vw
.width
,vw
.height
,vw
.flags
,vw
.clipcount
));
1197 if (vw
.clipcount
<0 || vw
.clipcount
>256)
1198 return -EDOM
; /* Too many! */
1203 vcp
= vmalloc(sizeof(struct video_clip
)*(vw
.clipcount
+4));
1206 if (vw
.clipcount
&& copy_from_user(vcp
,vw
.clips
,sizeof(struct video_clip
)*vw
.clipcount
)) {
1216 * strange, it seems xawtv sometimes calls us with 0
1217 * width and/or height. Ignore these values
1220 vw
.x
= ztv
->overinfo
.x
;
1222 vw
.y
= ztv
->overinfo
.y
;
1224 /* by now we are committed to the new data... */
1225 write_lock_irq(&ztv
->lock
);
1226 ztv
->overinfo
.x
= vw
.x
;
1227 ztv
->overinfo
.y
= vw
.y
;
1228 ztv
->overinfo
.w
= vw
.width
;
1229 ztv
->overinfo
.h
= vw
.height
;
1230 write_unlock_irq(&ztv
->lock
);
1233 * Impose display clips
1235 if (vw
.x
+vw
.width
> ztv
->swidth
)
1236 new_clip(&vw
, vcp
, ztv
->swidth
-vw
.x
, 0, vw
.width
-1, vw
.height
-1);
1237 if (vw
.y
+vw
.height
> ztv
->sheight
)
1238 new_clip(&vw
, vcp
, 0, ztv
->sheight
-vw
.y
, vw
.width
-1, vw
.height
-1);
1240 /* built the requested clipping zones */
1241 zoran_set_geo(ztv
, &ztv
->overinfo
);
1242 zoran_built_overlay(ztv
, vw
.clipcount
, vcp
);
1245 /* if we were on, restart the video engine */
1254 if (get_user(v
, (int *)arg
))
1256 DEBUG(printk(CARD_DEBUG
"VIDIOCCAPTURE(%d)\n",CARD
,v
));
1259 clear_bit(STATE_OVERLAY
, &ztv
->state
);
1263 /* is VIDIOCSFBUF, VIDIOCSWIN done? */
1264 if (ztv
->overinfo
.busadr
==0 || ztv
->overinfo
.w
==0 || ztv
->overinfo
.h
==0)
1267 set_bit(STATE_OVERLAY
, &ztv
->state
);
1275 struct video_buffer v
;
1276 DEBUG(printk(CARD_DEBUG
"VIDIOCGFBUF\n",CARD
));
1277 read_lock(&ztv
->lock
);
1278 v
.base
= (void *)ztv
->overinfo
.busadr
;
1279 v
.height
= ztv
->sheight
;
1280 v
.width
= ztv
->swidth
;
1281 v
.depth
= ztv
->depth
;
1282 v
.bytesperline
= ztv
->overinfo
.bpl
;
1283 read_unlock(&ztv
->lock
);
1284 if(copy_to_user(arg
, &v
,sizeof(v
)))
1290 struct video_buffer v
;
1291 if(!capable(CAP_SYS_ADMIN
))
1293 if (copy_from_user(&v
, arg
,sizeof(v
)))
1295 DEBUG(printk(CARD_DEBUG
"VIDIOCSFBUF(%p,%d,%d,%d,%d)\n",CARD
,v
.base
, v
.width
,v
.height
,v
.depth
,v
.bytesperline
));
1297 if (v
.depth
!=15 && v
.depth
!=16 && v
.depth
!=24 && v
.depth
!=32)
1299 if (v
.bytesperline
<1)
1303 write_lock_irq(&ztv
->lock
);
1304 ztv
->overinfo
.busadr
= (ulong
)v
.base
;
1305 ztv
->sheight
= v
.height
;
1306 ztv
->swidth
= v
.width
;
1307 ztv
->depth
= v
.depth
; /* bits per pixel */
1308 ztv
->overinfo
.bpp
= ((v
.depth
+1)&0x38)/8;/* bytes per pixel */
1309 ztv
->overinfo
.bpl
= v
.bytesperline
; /* bytes per line */
1310 write_unlock_irq(&ztv
->lock
);
1316 /* Will be handled higher up .. */
1323 if (get_user(i
, (int *) arg
))
1325 DEBUG(printk(CARD_DEBUG
"VIDEOCSYNC(%d)\n",CARD
,i
));
1326 if (i
<0 || i
>ZORAN_MAX_FBUFFERS
)
1328 switch (ztv
->grabinfo
[i
].status
) {
1332 /* wait till this buffer gets grabbed */
1333 while (ztv
->grabinfo
[i
].status
== FBUFFER_BUSY
) {
1334 interruptible_sleep_on(&ztv
->grabq
);
1335 /* see if a signal did it */
1336 if (signal_pending(current
))
1339 /* don't fall through; a DONE buffer is not UNUSED */
1342 ztv
->grabinfo
[i
].status
= FBUFFER_FREE
;
1343 /* tell ppl we have a spare buffer */
1344 wake_up_interruptible(&ztv
->grabq
);
1347 DEBUG(printk(CARD_DEBUG
"VIDEOCSYNC(%d) returns\n",CARD
,i
));
1351 case VIDIOCMCAPTURE
:
1353 struct video_mmap vm
;
1354 struct vidinfo
* frame
;
1355 if (copy_from_user(&vm
,arg
,sizeof(vm
)))
1357 DEBUG(printk(CARD_DEBUG
"VIDIOCMCAPTURE(%d,(%d,%d),%d)\n",CARD
,vm
.frame
,vm
.width
,vm
.height
,vm
.format
));
1358 if (vm
.frame
<0 || vm
.frame
>ZORAN_MAX_FBUFFERS
||
1359 vm
.width
<32 || vm
.width
>768 ||
1360 vm
.height
<32 || vm
.height
>576 ||
1361 vm
.format
>NRPALETTES
||
1362 palette2fmt
[vm
.format
].mode
== 0)
1365 /* we are allowed to take over UNUSED and DONE buffers */
1366 frame
= &ztv
->grabinfo
[vm
.frame
];
1367 if (frame
->status
== FBUFFER_BUSY
)
1370 /* setup the other parameters if they are given */
1371 write_lock_irq(&ztv
->lock
);
1372 frame
->w
= vm
.width
;
1373 frame
->h
= vm
.height
;
1374 frame
->format
= vm
.format
;
1375 frame
->bpp
= palette2fmt
[frame
->format
].bpp
;
1376 frame
->bpl
= frame
->w
*frame
->bpp
;
1377 frame
->status
= FBUFFER_BUSY
;
1379 { /* add to tail of queue */
1380 struct vidinfo
* oldframe
= ztv
->workqueue
;
1381 if (!oldframe
) ztv
->workqueue
= frame
;
1383 while (oldframe
->next
) oldframe
= oldframe
->next
;
1384 oldframe
->next
= frame
;
1387 write_unlock_irq(&ztv
->lock
);
1394 struct video_mbuf mb
;
1396 DEBUG(printk(CARD_DEBUG
"VIDIOCGMBUF\n",CARD
));
1397 mb
.size
= ZORAN_MAX_FBUFSIZE
;
1398 mb
.frames
= ZORAN_MAX_FBUFFERS
;
1399 for (i
=0; i
<ZORAN_MAX_FBUFFERS
; i
++)
1400 mb
.offsets
[i
] = i
*ZORAN_MAX_FBUFFER
;
1401 if(copy_to_user(arg
, &mb
,sizeof(mb
)))
1408 struct video_unit vu
;
1409 DEBUG(printk(CARD_DEBUG
"VIDIOCGUNIT\n",CARD
));
1410 vu
.video
= ztv
->video_dev
.minor
;
1411 vu
.vbi
= ztv
->vbi_dev
.minor
;
1412 vu
.radio
= VIDEO_NO_UNIT
;
1413 vu
.audio
= VIDEO_NO_UNIT
;
1414 vu
.teletext
= VIDEO_NO_UNIT
;
1415 if(copy_to_user(arg
, &vu
,sizeof(vu
)))
1422 unsigned long v
= ztv
->tuner_freq
;
1423 if (copy_to_user(arg
,&v
,sizeof(v
)))
1425 DEBUG(printk(CARD_DEBUG
"VIDIOCGFREQ\n",CARD
));
1431 if (copy_from_user(&v
, arg
, sizeof(v
)))
1433 DEBUG(printk(CARD_DEBUG
"VIDIOCSFREQ\n",CARD
));
1435 if (ztv
->have_tuner
) {
1437 if (i2c_control_device(&(ztv
->i2c
), I2C_DRIVERID_TUNER
, TUNER_SET_TVFREQ
, &fixme
) < 0)
1440 ztv
->tuner_freq
= v
;
1444 /* Why isn't this in the API?
1445 * And why doesn't it take a buffer number?
1448 unsigned long v = ztv->lastfieldnr;
1449 if (copy_to_user(arg,&v,sizeof(v)))
1451 DEBUG(printk(CARD_DEBUG "BTTV_FIELDNR\n",CARD));
1457 return -ENOIOCTLCMD
;
1463 int zoran_mmap(struct vm_area_struct
*vma
, struct video_device
* dev
, const char* adr
, unsigned long size
)
1465 struct zoran
* ztv
= (struct zoran
*)dev
;
1466 unsigned long start
= (unsigned long)adr
;
1469 DEBUG(printk(CARD_DEBUG
"zoran_mmap(0x%p,%ld)\n",CARD
,adr
,size
));
1472 if (size
> ZORAN_MAX_FBUFSIZE
|| !ztv
->fbuffer
)
1475 /* start mapping the whole shabang to user memory */
1476 pos
= (unsigned long)ztv
->fbuffer
;
1478 unsigned long page
= virt_to_phys((void*)pos
);
1479 if (remap_page_range(vma
, start
, page
, PAGE_SIZE
, PAGE_SHARED
))
1488 static struct video_device zr36120_template
=
1490 .owner
= THIS_MODULE
,
1492 .type
= VID_TYPE_TUNER
|VID_TYPE_CAPTURE
|VID_TYPE_OVERLAY
,
1493 .hardware
= VID_HARDWARE_ZR36120
,
1495 .close
= zoran_close
,
1497 .write
= zoran_write
,
1499 .ioctl
= zoran_ioctl
,
1505 int vbi_open(struct video_device
*dev
, int flags
)
1507 struct zoran
*ztv
= (struct zoran
*)dev
->priv
;
1508 struct vidinfo
* item
;
1510 DEBUG(printk(CARD_DEBUG
"vbi_open(dev,%d)\n",CARD
,flags
));
1513 * During VBI device open, we continiously grab VBI-like
1514 * data in the vbi buffer when we have nothing to do.
1515 * Only when there is an explicit request for VBI data
1516 * (read call) we /force/ a read.
1519 /* allocate buffers */
1520 for (item
=ztv
->readinfo
; item
!=ztv
->readinfo
+ZORAN_VBI_BUFFERS
; item
++)
1522 item
->status
= FBUFFER_FREE
;
1525 if (!item
->memadr
) {
1526 item
->memadr
= bmalloc(ZORAN_VBI_BUFSIZE
);
1527 if (!item
->memadr
) {
1528 /* could not get a buffer, bail out */
1529 while (item
!= ztv
->readinfo
) {
1531 bfree(item
->memadr
, ZORAN_VBI_BUFSIZE
);
1539 /* determine the DMAable address */
1540 item
->busadr
= virt_to_bus(item
->memadr
);
1543 /* do the common part of all open's */
1544 zoran_common_open(ztv
, flags
);
1546 set_bit(STATE_VBI
, &ztv
->state
);
1547 /* start read-ahead */
1554 void vbi_close(struct video_device
*dev
)
1556 struct zoran
*ztv
= (struct zoran
*)dev
->priv
;
1557 struct vidinfo
* item
;
1559 DEBUG(printk(CARD_DEBUG
"vbi_close(dev)\n",CARD
));
1561 /* driver specific closure */
1562 clear_bit(STATE_VBI
, &ztv
->state
);
1564 zoran_common_close(ztv
);
1567 * This is sucky but right now I can't find a good way to
1568 * be sure its safe to free the buffer. We wait 5-6 fields
1569 * which is more than sufficient to be sure.
1571 current
->state
= TASK_UNINTERRUPTIBLE
;
1572 schedule_timeout(HZ
/10); /* Wait 1/10th of a second */
1574 for (item
=ztv
->readinfo
; item
!=ztv
->readinfo
+ZORAN_VBI_BUFFERS
; item
++)
1577 bfree(item
->memadr
, ZORAN_VBI_BUFSIZE
);
1584 * This read function could be used reentrant in a SMP situation.
1586 * This is made possible by the spinlock which is kept till we
1587 * found and marked a buffer for our own use. The lock must
1588 * be released as soon as possible to prevent lock contention.
1591 long vbi_read(struct video_device
* dev
, char* buf
, unsigned long count
, int nonblock
)
1593 struct zoran
*ztv
= (struct zoran
*)dev
->priv
;
1595 struct vidinfo
* unused
= 0;
1596 struct vidinfo
* done
= 0;
1598 DEBUG(printk(CARD_DEBUG
"vbi_read(0x%p,%ld,%d)\n",CARD
,buf
,count
,nonblock
));
1600 /* find ourself a free or completed buffer */
1602 struct vidinfo
* item
;
1604 write_lock_irq(&ztv
->lock
);
1605 for (item
=ztv
->readinfo
; item
!=ztv
->readinfo
+ZORAN_VBI_BUFFERS
; item
++) {
1606 if (!unused
&& item
->status
== FBUFFER_FREE
)
1608 if (!done
&& item
->status
== FBUFFER_DONE
)
1614 /* no more free buffers, wait for them. */
1615 write_unlock_irq(&ztv
->lock
);
1617 return -EWOULDBLOCK
;
1618 interruptible_sleep_on(&ztv
->vbiq
);
1619 if (signal_pending(current
))
1623 /* Do we have 'ready' data? */
1625 /* no? than this will take a while... */
1627 write_unlock_irq(&ztv
->lock
);
1628 return -EWOULDBLOCK
;
1631 /* mark the unused buffer as wanted */
1632 unused
->status
= FBUFFER_BUSY
;
1634 { /* add to tail of queue */
1635 struct vidinfo
* oldframe
= ztv
->workqueue
;
1636 if (!oldframe
) ztv
->workqueue
= unused
;
1638 while (oldframe
->next
) oldframe
= oldframe
->next
;
1639 oldframe
->next
= unused
;
1642 write_unlock_irq(&ztv
->lock
);
1644 /* tell the state machine we want it filled /NOW/ */
1647 /* wait till this buffer gets grabbed */
1648 while (unused
->status
== FBUFFER_BUSY
) {
1649 interruptible_sleep_on(&ztv
->vbiq
);
1650 /* see if a signal did it */
1651 if (signal_pending(current
))
1657 write_unlock_irq(&ztv
->lock
);
1659 /* Yes! we got data! */
1660 max
= done
->bpl
* -done
->h
;
1664 /* check if the user gave us enough room to write the data */
1665 if (!access_ok(VERIFY_WRITE
, buf
, count
)) {
1671 * Now transform/strip the data from YUV to Y-only
1672 * NB. Assume the Y is in the LSB of the YUV data.
1675 unsigned char* optr
= buf
;
1676 unsigned char* eptr
= buf
+count
;
1678 /* are we beeing accessed from an old driver? */
1679 if (count
== 2*19*2048) {
1681 * Extreme HACK, old VBI programs expect 2048 points
1682 * of data, and we only got 864 orso. Double each
1683 * datapoint and clear the rest of the line.
1684 * This way we have appear to have a
1685 * sample_frequency of 29.5 Mc.
1688 unsigned char* iptr
= done
->memadr
+1;
1689 for (y
=done
->h
; optr
<eptr
&& y
<0; y
++)
1691 /* copy to doubled data to userland */
1692 for (x
=0; optr
+1<eptr
&& x
<-done
->w
; x
++)
1694 unsigned char a
= iptr
[x
*2];
1695 __put_user(a
, optr
++);
1696 __put_user(a
, optr
++);
1698 /* and clear the rest of the line */
1699 for (x
*=2; optr
<eptr
&& x
<done
->bpl
; x
++)
1700 __put_user(0, optr
++);
1707 * Other (probably newer) programs asked
1708 * us what geometry we are using, and are
1709 * reading the correct size.
1712 unsigned char* iptr
= done
->memadr
+1;
1713 for (y
=done
->h
; optr
<eptr
&& y
<0; y
++)
1715 /* copy to doubled data to userland */
1716 for (x
=0; optr
<eptr
&& x
<-done
->w
; x
++)
1717 __put_user(iptr
[x
*2], optr
++);
1718 /* and clear the rest of the line */
1719 for (;optr
<eptr
&& x
<done
->bpl
; x
++)
1720 __put_user(0, optr
++);
1727 * place the framenumber (half fieldnr) in the last long
1729 __put_user(done
->fieldnr
/2, ((ulong
*)eptr
)[-1]);
1732 /* keep the engine running */
1733 done
->status
= FBUFFER_FREE
;
1736 /* tell listeners this buffer just became free */
1737 wake_up_interruptible(&ztv
->vbiq
);
1741 DEBUG(printk(CARD_DEBUG
"vbi_read() returns %lu\n",CARD
,count
));
1746 unsigned int vbi_poll(struct video_device
*dev
, struct file
*file
, poll_table
*wait
)
1748 struct zoran
*ztv
= (struct zoran
*)dev
->priv
;
1749 struct vidinfo
* item
;
1750 unsigned int mask
= 0;
1752 poll_wait(file
, &ztv
->vbiq
, wait
);
1754 for (item
=ztv
->readinfo
; item
!=ztv
->readinfo
+ZORAN_VBI_BUFFERS
; item
++)
1755 if (item
->status
== FBUFFER_DONE
)
1757 mask
|= (POLLIN
| POLLRDNORM
);
1761 DEBUG(printk(CARD_DEBUG
"vbi_poll()=%x\n",CARD
,mask
));
1767 int vbi_ioctl(struct video_device
*dev
, unsigned int cmd
, void *arg
)
1769 struct zoran
* ztv
= (struct zoran
*)dev
->priv
;
1774 struct vbi_format f
;
1775 DEBUG(printk(CARD_DEBUG
"VIDIOCGVBIINFO\n",CARD
));
1776 f
.sampling_rate
= 14750000UL;
1777 f
.samples_per_line
= -ztv
->readinfo
[0].w
;
1778 f
.sample_format
= VIDEO_PALETTE_RAW
;
1779 f
.start
[0] = f
.start
[1] = ztv
->readinfo
[0].y
;
1781 f
.count
[0] = f
.count
[1] = -ztv
->readinfo
[0].h
;
1782 f
.flags
= VBI_INTERLACED
;
1783 if (copy_to_user(arg
,&f
,sizeof(f
)))
1789 struct vbi_format f
;
1791 if (copy_from_user(&f
, arg
,sizeof(f
)))
1793 DEBUG(printk(CARD_DEBUG
"VIDIOCSVBIINFO(%d,%d,%d,%d,%d,%d,%d,%x)\n",CARD
,f
.sampling_rate
,f
.samples_per_line
,f
.sample_format
,f
.start
[0],f
.start
[1],f
.count
[0],f
.count
[1],f
.flags
));
1795 /* lots of parameters are fixed... (PAL) */
1796 if (f
.sampling_rate
!= 14750000UL ||
1797 f
.samples_per_line
> 864 ||
1798 f
.sample_format
!= VIDEO_PALETTE_RAW
||
1800 f
.start
[0] != f
.start
[1]-312 ||
1801 f
.count
[0] != f
.count
[1] ||
1802 f
.start
[0]+f
.count
[0] >= 288 ||
1803 f
.flags
!= VBI_INTERLACED
)
1806 write_lock_irq(&ztv
->lock
);
1807 ztv
->readinfo
[0].y
= f
.start
[0];
1808 ztv
->readinfo
[0].w
= -f
.samples_per_line
;
1809 ztv
->readinfo
[0].h
= -f
.count
[0];
1810 ztv
->readinfo
[0].bpl
= f
.samples_per_line
*ztv
->readinfo
[0].bpp
;
1811 for (i
=1; i
<ZORAN_VBI_BUFFERS
; i
++)
1812 ztv
->readinfo
[i
] = ztv
->readinfo
[i
];
1813 write_unlock_irq(&ztv
->lock
);
1817 return -ENOIOCTLCMD
;
1822 static struct video_device vbi_template
=
1824 .owner
= THIS_MODULE
,
1826 .type
= VID_TYPE_CAPTURE
|VID_TYPE_TELETEXT
,
1827 .hardware
= VID_HARDWARE_ZR36120
,
1831 .write
= zoran_write
,
1838 * Scan for a Zoran chip, request the irq and map the io memory
1841 int __init
find_zoran(void)
1845 struct pci_dev
*dev
= NULL
;
1846 unsigned char revision
;
1849 while ((dev
= pci_find_device(PCI_VENDOR_ID_ZORAN
,PCI_DEVICE_ID_ZORAN_36120
, dev
)))
1851 /* Ok, a ZR36120/ZR36125 found! */
1852 ztv
= &zorans
[zoran_num
];
1855 if (pci_enable_device(dev
))
1858 pci_read_config_byte(dev
, PCI_CLASS_REVISION
, &revision
);
1859 printk(KERN_INFO
"zoran: Zoran %x (rev %d) ",
1860 dev
->device
, revision
);
1861 printk("bus: %d, devfn: %d, irq: %d, ",
1862 dev
->bus
->number
, dev
->devfn
, dev
->irq
);
1863 printk("memory: 0x%08lx.\n", ztv
->zoran_adr
);
1865 ztv
->zoran_mem
= ioremap(ztv
->zoran_adr
, 0x1000);
1866 DEBUG(printk(KERN_DEBUG
"zoran: mapped-memory at 0x%p\n",ztv
->zoran_mem
));
1868 result
= request_irq(dev
->irq
, zoran_irq
,
1869 SA_SHIRQ
|SA_INTERRUPT
,"zoran",(void *)ztv
);
1870 if (result
==-EINVAL
)
1872 iounmap(ztv
->zoran_mem
);
1873 printk(KERN_ERR
"zoran: Bad irq number or handler\n");
1877 printk(KERN_ERR
"zoran: IRQ %d busy, change your PnP config in BIOS\n",dev
->irq
);
1879 iounmap(ztv
->zoran_mem
);
1882 /* Enable bus-mastering */
1883 pci_set_master(dev
);
1888 printk(KERN_INFO
"zoran: %d Zoran card(s) found.\n",zoran_num
);
1893 int __init
init_zoran(int card
)
1895 struct zoran
*ztv
= &zorans
[card
];
1898 /* if the given cardtype valid? */
1899 if (cardtype
[card
]>=NRTVCARDS
) {
1900 printk(KERN_INFO
"invalid cardtype(%d) detected\n",cardtype
[card
]);
1904 /* reset the zoran */
1905 zrand(~ZORAN_PCI_SOFTRESET
,ZORAN_PCI
);
1907 zror(ZORAN_PCI_SOFTRESET
,ZORAN_PCI
);
1910 /* zoran chip specific details */
1911 ztv
->card
= tvcards
+cardtype
[card
]; /* point to the selected card */
1912 ztv
->norm
= 0; /* PAL */
1913 ztv
->tuner_freq
= 0;
1915 /* videocard details */
1922 ztv
->overinfo
.kindof
= FBUFFER_OVERLAY
;
1923 ztv
->overinfo
.status
= FBUFFER_FREE
;
1924 ztv
->overinfo
.x
= 0;
1925 ztv
->overinfo
.y
= 0;
1926 ztv
->overinfo
.w
= 768; /* 640 */
1927 ztv
->overinfo
.h
= 576; /* 480 */
1928 ztv
->overinfo
.format
= VIDEO_PALETTE_RGB565
;
1929 ztv
->overinfo
.bpp
= palette2fmt
[ztv
->overinfo
.format
].bpp
;
1930 ztv
->overinfo
.bpl
= ztv
->overinfo
.bpp
*ztv
->swidth
;
1931 ztv
->overinfo
.busadr
= 0;
1932 ztv
->overinfo
.memadr
= 0;
1933 ztv
->overinfo
.overlay
= 0;
1934 for (i
=0; i
<ZORAN_MAX_FBUFFERS
; i
++) {
1935 ztv
->grabinfo
[i
] = ztv
->overinfo
;
1936 ztv
->grabinfo
[i
].kindof
= FBUFFER_GRAB
;
1938 init_waitqueue_head(&ztv
->grabq
);
1941 ztv
->readinfo
[0] = ztv
->overinfo
;
1942 ztv
->readinfo
[0].kindof
= FBUFFER_VBI
;
1943 ztv
->readinfo
[0].w
= -864;
1944 ztv
->readinfo
[0].h
= -38;
1945 ztv
->readinfo
[0].format
= VIDEO_PALETTE_YUV422
;
1946 ztv
->readinfo
[0].bpp
= palette2fmt
[ztv
->readinfo
[0].format
].bpp
;
1947 ztv
->readinfo
[0].bpl
= 1024*ztv
->readinfo
[0].bpp
;
1948 for (i
=1; i
<ZORAN_VBI_BUFFERS
; i
++)
1949 ztv
->readinfo
[i
] = ztv
->readinfo
[0];
1950 init_waitqueue_head(&ztv
->vbiq
);
1952 /* maintenance data */
1953 ztv
->have_decoder
= 0;
1954 ztv
->have_tuner
= 0;
1955 ztv
->tuner_type
= 0;
1958 ztv
->lock
= RW_LOCK_UNLOCKED
;
1961 ztv
->lastfieldnr
= 0;
1964 zrand(~ZORAN_VDC_TRICOM
, ZORAN_VDC
);
1966 /* external FL determines TOP frame */
1967 zror(ZORAN_VFEC_EXTFL
, ZORAN_VFEC
);
1970 if (ztv
->card
->hsync_pos
)
1971 zrwrite(ZORAN_VFEH_HSPOL
, ZORAN_VFEH
);
1973 if (ztv
->card
->vsync_pos
)
1974 zrwrite(ZORAN_VFEV_VSPOL
, ZORAN_VFEV
);
1976 /* Set the proper General Purpuse register bits */
1977 /* implicit: no softreset, 0 waitstates */
1978 zrwrite(ZORAN_PCI_SOFTRESET
|(ztv
->card
->gpdir
<<0),ZORAN_PCI
);
1979 /* implicit: 3 duration and recovery PCI clocks on guest 0-3 */
1980 zrwrite(ztv
->card
->gpval
<<24,ZORAN_GUEST
);
1982 /* clear interrupt status */
1983 zrwrite(~0, ZORAN_ISR
);
1988 ztv
->i2c
= zoran_i2c_bus_template
;
1989 sprintf(ztv
->i2c
.name
,"zoran-%d",card
);
1990 ztv
->i2c
.data
= ztv
;
1993 * Now add the template and register the device unit
1995 ztv
->video_dev
= zr36120_template
;
1996 strcpy(ztv
->video_dev
.name
, ztv
->i2c
.name
);
1997 ztv
->video_dev
.priv
= ztv
;
1998 if (video_register_device(&ztv
->video_dev
, VFL_TYPE_GRABBER
, video_nr
) < 0)
2001 ztv
->vbi_dev
= vbi_template
;
2002 strcpy(ztv
->vbi_dev
.name
, ztv
->i2c
.name
);
2003 ztv
->vbi_dev
.priv
= ztv
;
2004 if (video_register_device(&ztv
->vbi_dev
, VFL_TYPE_VBI
, vbi_nr
) < 0) {
2005 video_unregister_device(&ztv
->video_dev
);
2008 i2c_register_bus(&ztv
->i2c
);
2010 /* set interrupt mask - the PIN enable will be set later */
2011 zrwrite(ZORAN_ICR_GIRQ0
|ZORAN_ICR_GIRQ1
|ZORAN_ICR_CODE
, ZORAN_ICR
);
2013 printk(KERN_INFO
"%s: installed %s\n",ztv
->i2c
.name
,ztv
->card
->name
);
2018 void release_zoran(int max
)
2023 for (i
=0;i
<max
; i
++)
2027 /* turn off all capturing, DMA and IRQs */
2028 /* reset the zoran */
2029 zrand(~ZORAN_PCI_SOFTRESET
,ZORAN_PCI
);
2031 zror(ZORAN_PCI_SOFTRESET
,ZORAN_PCI
);
2034 /* first disable interrupts before unmapping the memory! */
2035 zrwrite(0, ZORAN_ICR
);
2036 zrwrite(0xffffffffUL
,ZORAN_ISR
);
2039 free_irq(ztv
->dev
->irq
,ztv
);
2041 /* unregister i2c_bus */
2042 i2c_unregister_bus((&ztv
->i2c
));
2044 /* unmap and free memory */
2046 iounmap(ztv
->zoran_mem
);
2048 video_unregister_device(&ztv
->video_dev
);
2049 video_unregister_device(&ztv
->vbi_dev
);
2053 void __exit
zr36120_exit(void)
2055 release_zoran(zoran_cards
);
2058 int __init
zr36120_init(void)
2063 zoran_cards
= find_zoran();
2065 /* no cards found, no need for a driver */
2068 /* initialize Zorans */
2069 for (card
=0; card
<zoran_cards
; card
++) {
2070 if (init_zoran(card
)<0) {
2071 /* only release the zorans we have registered */
2072 release_zoran(card
);
2079 module_init(zr36120_init
);
2080 module_exit(zr36120_exit
);