1 #define MAX_KMALLOC_MEM (512*1024)
3 buz - Iomega Buz driver version 1.0
5 Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
9 buz.0.0.3 Copyright (C) 1998 Dave Perks <dperks@ibm.net>
13 bttv - Bt848 frame grabber driver
15 Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
16 & Marcus Metzler (mocm@thp.uni-koeln.de)
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include <linux/module.h>
34 #include <linux/delay.h>
35 #include <linux/errno.h>
37 #include <linux/kernel.h>
38 #include <linux/major.h>
39 #include <linux/malloc.h>
41 #include <linux/pci.h>
42 #include <linux/signal.h>
44 #include <asm/pgtable.h>
46 #include <linux/sched.h>
47 #include <asm/segment.h>
48 #include <linux/types.h>
49 #include <linux/wrapper.h>
50 #include <linux/spinlock.h>
52 #include <linux/videodev.h>
54 #include <linux/version.h>
55 #include <asm/uaccess.h>
57 #include <linux/i2c.h>
59 #include <linux/video_decoder.h>
60 #include <linux/video_encoder.h>
62 #define IRQ_MASK ( ZR36057_ISR_GIRQ0 | /* ZR36057_ISR_GIRQ1 | ZR36057_ISR_CodRepIRQ | */ ZR36057_ISR_JPEGRepIRQ )
63 #define GPIO_MASK 0xdf
69 GPIO0 = 1, take board out of reset
70 GPIO1 = 1, take JPEG codec out of sleep mode
71 GPIO3 = 1, deassert FRAME# to 36060
74 GIRQ0 signals a vertical sync of the video signal
75 GIRQ1 signals that ZR36060's DATERR# line is asserted.
79 In their infinite wisdom, the Iomega engineers decided to
80 use the same input line for composite and S-Video Color,
81 although there are two entries not connected at all!
82 Through this ingenious strike, it is not possible to
83 keep two running video sources connected at the same time
84 to Composite and S-VHS input!
88 mode 2 - noise or something I don't know
89 mode 3 - Composite and S-Video C
91 mode 5 - S-Video (gain C independently selectable of gain Y)
93 mode 7 - S-Video (gain C adapted to gain Y)
96 #define MAJOR_VERSION 1 /* driver major version */
97 #define MINOR_VERSION 0 /* driver minor version */
99 #define BUZ_NAME "Iomega BUZ V-1.0" /* name of the driver */
101 #define DEBUG(x) /* Debug driver */
102 #define IDEBUG(x) /* Debug interrupt handler */
103 #define IOCTL_DEBUG(x)
106 /* The parameters for this driver */
109 The video mem address of the video card.
110 The driver has a little database for some videocards
111 to determine it from there. If your video card is not in there
112 you have either to give it to the driver as a parameter
113 or set in in a VIDIOCSFBUF ioctl
116 static unsigned long vidmem
= 0; /* Video memory base address */
118 /* Special purposes only: */
120 static int triton
= 0; /* 0=no, 1=yes */
121 static int natoma
= 0; /* 0=no, 1=yes */
124 Number and size of grab buffers for Video 4 Linux
125 The vast majority of applications should not need more than 2,
126 the very popular BTTV driver actually does ONLY have 2.
127 Time sensitive applications might need more, the maximum
128 is VIDEO_MAX_FRAME (defined in <linux/videodev.h>).
130 The size is set so that the maximum possible request
131 can be satisfied. Decrease it, if bigphys_area alloc'd
132 memory is low. If you don't have the bigphys_area patch,
133 set it to 128 KB. Will you allow only to grab small
134 images with V4L, but that's better than nothing.
136 v4l_bufsize has to be given in KB !
140 static int v4l_nbufs
= 2;
141 static int v4l_bufsize
= 128; /* Everybody should be able to work with this setting */
144 Default input and video norm at startup of the driver.
147 static int default_input
= 0; /* 0=Composite, 1=S-VHS */
148 static int default_norm
= 0; /* 0=PAL, 1=NTSC */
150 MODULE_PARM(vidmem
, "i");
151 MODULE_PARM(triton
, "i");
152 MODULE_PARM(natoma
, "i");
153 MODULE_PARM(v4l_nbufs
, "i");
154 MODULE_PARM(v4l_bufsize
, "i");
155 MODULE_PARM(default_input
, "i");
156 MODULE_PARM(default_norm
, "i");
158 /* Anybody who uses more than four? */
161 static int zoran_num
; /* number of Buzs in use */
162 static struct zoran zoran
[BUZ_MAX
];
164 /* forward references */
166 static void v4l_fbuffer_free(struct zoran
*zr
);
167 static void jpg_fbuffer_free(struct zoran
*zr
);
168 static void zoran_feed_stat_com(struct zoran
*zr
);
173 * Allocate the V4L grab buffers
175 * These have to be pysically contiguous.
176 * If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
179 static int v4l_fbuffer_alloc(struct zoran
*zr
)
184 for (i
= 0; i
< v4l_nbufs
; i
++) {
185 if (zr
->v4l_gbuf
[i
].fbuffer
)
186 printk(KERN_WARNING
"%s: v4l_fbuffer_alloc: buffer %d allready allocated ?\n", zr
->name
, i
);
188 if (v4l_bufsize
<= MAX_KMALLOC_MEM
) {
191 mem
= (unsigned char *) kmalloc(v4l_bufsize
, GFP_KERNEL
);
193 printk(KERN_ERR
"%s: kmalloc for V4L bufs failed\n", zr
->name
);
194 v4l_fbuffer_free(zr
);
197 zr
->v4l_gbuf
[i
].fbuffer
= mem
;
198 zr
->v4l_gbuf
[i
].fbuffer_phys
= virt_to_phys(mem
);
199 zr
->v4l_gbuf
[i
].fbuffer_bus
= virt_to_bus(mem
);
200 for (off
= 0; off
< v4l_bufsize
; off
+= PAGE_SIZE
)
201 mem_map_reserve(MAP_NR(mem
+ off
));
202 DEBUG(printk(BUZ_INFO
": V4L frame %d mem 0x%x (bus: 0x%x=%d)\n", i
, mem
, virt_to_bus(mem
), virt_to_bus(mem
)));
211 /* free the V4L grab buffers */
212 static void v4l_fbuffer_free(struct zoran
*zr
)
217 for (i
= 0; i
< v4l_nbufs
; i
++) {
218 if (!zr
->v4l_gbuf
[i
].fbuffer
)
221 mem
= zr
->v4l_gbuf
[i
].fbuffer
;
222 for (off
= 0; off
< v4l_bufsize
; off
+= PAGE_SIZE
)
223 mem_map_unreserve(MAP_NR(mem
+ off
));
224 kfree((void *) zr
->v4l_gbuf
[i
].fbuffer
);
225 zr
->v4l_gbuf
[i
].fbuffer
= NULL
;
230 * Allocate the MJPEG grab buffers.
232 * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
233 * kmalloc is used to request a physically contiguous area,
234 * else we allocate the memory in framgents with get_free_page.
236 * If a Natoma chipset is present and this is a revision 1 zr36057,
237 * each MJPEG buffer needs to be physically contiguous.
238 * (RJ: This statement is from Dave Perks' original driver,
239 * I could never check it because I have a zr36067)
240 * The driver cares about this because it reduces the buffer
241 * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
243 * RJ: The contents grab buffers needs never be accessed in the driver.
244 * Therefore there is no need to allocate them with vmalloc in order
245 * to get a contiguous virtual memory space.
246 * I don't understand why many other drivers first allocate them with
247 * vmalloc (which uses internally also get_free_page, but delivers you
248 * virtual addresses) and then again have to make a lot of efforts
249 * to get the physical address.
253 static int jpg_fbuffer_alloc(struct zoran
*zr
)
255 int i
, j
, off
, alloc_contig
;
258 /* Decide if we should alloc contiguous or fragmented memory */
259 /* This has to be identical in jpg_fbuffer_alloc and jpg_fbuffer_free */
261 alloc_contig
= (zr
->jpg_bufsize
< MAX_KMALLOC_MEM
);
263 for (i
= 0; i
< zr
->jpg_nbufs
; i
++) {
264 if (zr
->jpg_gbuf
[i
].frag_tab
)
265 printk(KERN_WARNING
"%s: jpg_fbuffer_alloc: buffer %d allready allocated ???\n", zr
->name
, i
);
267 /* Allocate fragment table for this buffer */
269 mem
= get_free_page(GFP_KERNEL
);
271 printk(KERN_ERR
"%s: jpg_fbuffer_alloc: get_free_page (frag_tab) failed for buffer %d\n", zr
->name
, i
);
272 jpg_fbuffer_free(zr
);
275 memset((void *) mem
, 0, PAGE_SIZE
);
276 zr
->jpg_gbuf
[i
].frag_tab
= (u32
*) mem
;
277 zr
->jpg_gbuf
[i
].frag_tab_bus
= virt_to_bus((void *) mem
);
280 mem
= (unsigned long) kmalloc(zr
->jpg_bufsize
, GFP_KERNEL
);
282 jpg_fbuffer_free(zr
);
285 zr
->jpg_gbuf
[i
].frag_tab
[0] = virt_to_bus((void *) mem
);
286 zr
->jpg_gbuf
[i
].frag_tab
[1] = ((zr
->jpg_bufsize
/ 4) << 1) | 1;
287 for (off
= 0; off
< zr
->jpg_bufsize
; off
+= PAGE_SIZE
)
288 mem_map_reserve(MAP_NR(mem
+ off
));
290 /* jpg_bufsize is alreay page aligned */
291 for (j
= 0; j
< zr
->jpg_bufsize
/ PAGE_SIZE
; j
++) {
292 mem
= get_free_page(GFP_KERNEL
);
294 jpg_fbuffer_free(zr
);
297 zr
->jpg_gbuf
[i
].frag_tab
[2 * j
] = virt_to_bus((void *) mem
);
298 zr
->jpg_gbuf
[i
].frag_tab
[2 * j
+ 1] = (PAGE_SIZE
/ 4) << 1;
299 mem_map_reserve(MAP_NR(mem
));
302 zr
->jpg_gbuf
[i
].frag_tab
[2 * j
- 1] |= 1;
306 DEBUG(printk("jpg_fbuffer_alloc: %d KB allocated\n",
307 (zr
->jpg_nbufs
* zr
->jpg_bufsize
) >> 10));
308 zr
->jpg_buffers_allocated
= 1;
312 /* free the MJPEG grab buffers */
313 static void jpg_fbuffer_free(struct zoran
*zr
)
315 int i
, j
, off
, alloc_contig
;
318 /* Decide if we should alloc contiguous or fragmented memory */
319 /* This has to be identical in jpg_fbuffer_alloc and jpg_fbuffer_free */
321 alloc_contig
= (zr
->jpg_bufsize
< MAX_KMALLOC_MEM
);
323 for (i
= 0; i
< zr
->jpg_nbufs
; i
++) {
324 if (!zr
->jpg_gbuf
[i
].frag_tab
)
328 if (zr
->jpg_gbuf
[i
].frag_tab
[0]) {
329 mem
= (unsigned char *) bus_to_virt(zr
->jpg_gbuf
[i
].frag_tab
[0]);
330 for (off
= 0; off
< zr
->jpg_bufsize
; off
+= PAGE_SIZE
)
331 mem_map_unreserve(MAP_NR(mem
+ off
));
333 zr
->jpg_gbuf
[i
].frag_tab
[0] = 0;
334 zr
->jpg_gbuf
[i
].frag_tab
[1] = 0;
337 for (j
= 0; j
< zr
->jpg_bufsize
/ PAGE_SIZE
; j
++) {
338 if (!zr
->jpg_gbuf
[i
].frag_tab
[2 * j
])
340 mem_map_unreserve(MAP_NR(bus_to_virt(zr
->jpg_gbuf
[i
].frag_tab
[2 * j
])));
341 free_page((unsigned long) bus_to_virt(zr
->jpg_gbuf
[i
].frag_tab
[2 * j
]));
342 zr
->jpg_gbuf
[i
].frag_tab
[2 * j
] = 0;
343 zr
->jpg_gbuf
[i
].frag_tab
[2 * j
+ 1] = 0;
347 free_page((unsigned long) zr
->jpg_gbuf
[i
].frag_tab
);
348 zr
->jpg_gbuf
[i
].frag_tab
= NULL
;
350 zr
->jpg_buffers_allocated
= 0;
354 /* ----------------------------------------------------------------------- */
361 /* software I2C functions */
363 static void i2c_setlines(struct i2c_bus
*bus
, int ctrl
, int data
)
365 struct zoran
*zr
= (struct zoran
*) bus
->data
;
366 btwrite((data
<< 1) | ctrl
, ZR36057_I2CBR
);
367 btread(ZR36057_I2CBR
);
371 static int i2c_getdataline(struct i2c_bus
*bus
)
373 struct zoran
*zr
= (struct zoran
*) bus
->data
;
374 return (btread(ZR36057_I2CBR
) >> 1) & 1;
377 void attach_inform(struct i2c_bus
*bus
, int id
)
379 DEBUG(struct zoran
*zr
= (struct zoran
*) bus
->data
);
380 DEBUG(printk(BUZ_DEBUG
"-%u: i2c attach %02x\n", zr
->id
, id
));
383 void detach_inform(struct i2c_bus
*bus
, int id
)
385 DEBUG(struct zoran
*zr
= (struct zoran
*) bus
->data
);
386 DEBUG(printk(BUZ_DEBUG
"-%u: i2c detach %02x\n", zr
->id
, id
));
389 static struct i2c_bus zoran_i2c_bus_template
=
407 /* ----------------------------------------------------------------------- */
409 static void GPIO(struct zoran
*zr
, unsigned bit
, unsigned value
)
414 mask
= 1 << (24 + bit
);
415 reg
= btread(ZR36057_GPPGCR1
) & ~mask
;
419 btwrite(reg
, ZR36057_GPPGCR1
);
420 /* Stop any PCI posting on the GPIO bus */
421 btread(ZR36057_I2CBR
);
426 * Set the registers for the size we have specified. Don't bother
427 * trying to understand this without the ZR36057 manual in front of
430 * PS: The manual is free for download in .pdf format from
431 * www.zoran.com - nicely done those folks.
435 u16 Wt
, Wa
, Ht
, Ha
, HStart
, VStart
;
438 static struct tvnorm tvnorms
[] =
441 {864, 720, 625, 576, 31, 16},
443 {858, 720, 525, 480, 21, 8},
445 #define TVNORMS (sizeof(tvnorms) / sizeof(tvnorm))
447 static int format2bpp(int format
)
451 /* Determine the number of bytes per pixel for the video format requested */
455 case VIDEO_PALETTE_YUV422
:
459 case VIDEO_PALETTE_RGB555
:
463 case VIDEO_PALETTE_RGB565
:
467 case VIDEO_PALETTE_RGB24
:
471 case VIDEO_PALETTE_RGB32
:
485 static void zr36057_set_vfe(struct zoran
*zr
, int video_width
, int video_height
,
486 unsigned int video_format
)
489 unsigned HStart
, HEnd
, VStart
, VEnd
;
491 unsigned VidWinWid
, VidWinHt
;
492 unsigned hcrop1
, hcrop2
, vcrop1
, vcrop2
;
493 unsigned Wa
, We
, Ha
, He
;
494 unsigned X
, Y
, HorDcm
, VerDcm
;
496 unsigned mask_line_size
;
498 if (zr
->params
.norm
< 0 || zr
->params
.norm
> 1) {
499 printk(KERN_ERR
"%s: set_vfe: video_norm = %d not valid\n", zr
->name
, zr
->params
.norm
);
502 if (video_width
< BUZ_MIN_WIDTH
|| video_height
< BUZ_MIN_HEIGHT
) {
503 printk(KERN_ERR
"%s: set_vfe: w=%d h=%d not valid\n", zr
->name
, video_width
, video_height
);
506 tvn
= &tvnorms
[zr
->params
.norm
];
511 /* if window has more than half of active height,
512 switch on interlacing - we want the full information */
514 zr
->video_interlace
= (video_height
> Ha
/ 2);
519 VidWinWid
= video_width
;
520 X
= (VidWinWid
* 64 + tvn
->Wa
- 1) / tvn
->Wa
;
521 We
= (VidWinWid
* 64) / X
;
523 hcrop1
= 2 * ((tvn
->Wa
- We
) / 4);
524 hcrop2
= tvn
->Wa
- We
- hcrop1
;
525 HStart
= tvn
->HStart
| 1;
526 HEnd
= HStart
+ tvn
->Wa
- 1;
529 reg
= ((HStart
& ZR36057_VFEHCR_Hmask
) << ZR36057_VFEHCR_HStart
)
530 | ((HEnd
& ZR36057_VFEHCR_Hmask
) << ZR36057_VFEHCR_HEnd
);
531 reg
|= ZR36057_VFEHCR_HSPol
;
532 btwrite(reg
, ZR36057_VFEHCR
);
535 DispMode
= !zr
->video_interlace
;
536 VidWinHt
= DispMode
? video_height
: video_height
/ 2;
537 Y
= (VidWinHt
* 64 * 2 + tvn
->Ha
- 1) / tvn
->Ha
;
538 He
= (VidWinHt
* 64) / Y
;
540 vcrop1
= (tvn
->Ha
/ 2 - He
) / 2;
541 vcrop2
= tvn
->Ha
/ 2 - He
- vcrop1
;
542 VStart
= tvn
->VStart
;
543 VEnd
= VStart
+ tvn
->Ha
/ 2 - 1;
546 reg
= ((VStart
& ZR36057_VFEVCR_Vmask
) << ZR36057_VFEVCR_VStart
)
547 | ((VEnd
& ZR36057_VFEVCR_Vmask
) << ZR36057_VFEVCR_VEnd
);
548 reg
|= ZR36057_VFEVCR_VSPol
;
549 btwrite(reg
, ZR36057_VFEVCR
);
551 /* scaler and pixel format */
552 reg
= 0 // ZR36057_VFESPFR_ExtFl /* Trying to live without ExtFl */
553 | (HorDcm
<< ZR36057_VFESPFR_HorDcm
)
554 | (VerDcm
<< ZR36057_VFESPFR_VerDcm
)
555 | (DispMode
<< ZR36057_VFESPFR_DispMode
)
556 | ZR36057_VFESPFR_LittleEndian
;
557 /* RJ: I don't know, why the following has to be the opposite
558 of the corresponding ZR36060 setting, but only this way
559 we get the correct colors when uncompressing to the screen */
560 reg
|= ZR36057_VFESPFR_VCLKPol
;
561 /* RJ: Don't know if that is needed for NTSC also */
562 reg
|= ZR36057_VFESPFR_TopField
;
563 switch (video_format
) {
565 case VIDEO_PALETTE_YUV422
:
566 reg
|= ZR36057_VFESPFR_YUV422
;
569 case VIDEO_PALETTE_RGB555
:
570 reg
|= ZR36057_VFESPFR_RGB555
| ZR36057_VFESPFR_ErrDif
;
573 case VIDEO_PALETTE_RGB565
:
574 reg
|= ZR36057_VFESPFR_RGB565
| ZR36057_VFESPFR_ErrDif
;
577 case VIDEO_PALETTE_RGB24
:
578 reg
|= ZR36057_VFESPFR_RGB888
| ZR36057_VFESPFR_Pack24
;
581 case VIDEO_PALETTE_RGB32
:
582 reg
|= ZR36057_VFESPFR_RGB888
;
586 printk(KERN_INFO
"%s: Unknown color_fmt=%x\n", zr
->name
, video_format
);
591 reg
|= 3 << ZR36057_VFESPFR_HFilter
; /* 5 tap filter */
592 } else if (HorDcm
>= 32) {
593 reg
|= 2 << ZR36057_VFESPFR_HFilter
; /* 4 tap filter */
594 } else if (HorDcm
>= 16) {
595 reg
|= 1 << ZR36057_VFESPFR_HFilter
; /* 3 tap filter */
597 btwrite(reg
, ZR36057_VFESPFR
);
599 /* display configuration */
601 reg
= (16 << ZR36057_VDCR_MinPix
)
602 | (VidWinHt
<< ZR36057_VDCR_VidWinHt
)
603 | (VidWinWid
<< ZR36057_VDCR_VidWinWid
);
605 reg
&= ~ZR36057_VDCR_Triton
;
607 reg
|= ZR36057_VDCR_Triton
;
608 btwrite(reg
, ZR36057_VDCR
);
610 /* Write overlay clipping mask data, but don't enable overlay clipping */
611 /* RJ: since this makes only sense on the screen, we use
612 zr->window.width instead of video_width */
614 mask_line_size
= (BUZ_MAX_WIDTH
+ 31) / 32;
615 reg
= virt_to_bus(zr
->overlay_mask
);
616 btwrite(reg
, ZR36057_MMTR
);
617 reg
= virt_to_bus(zr
->overlay_mask
+ mask_line_size
);
618 btwrite(reg
, ZR36057_MMBR
);
619 reg
= mask_line_size
- (zr
->window
.width
+ 31) / 32;
621 reg
+= mask_line_size
;
622 reg
<<= ZR36057_OCR_MaskStride
;
623 btwrite(reg
, ZR36057_OCR
);
628 * Switch overlay on or off
631 static void zr36057_overlay(struct zoran
*zr
, int on
)
637 /* do the necessary settings ... */
639 btand(~ZR36057_VDCR_VidEn
, ZR36057_VDCR
); /* switch it off first */
641 switch (zr
->buffer
.depth
) {
643 fmt
= VIDEO_PALETTE_RGB555
;
647 fmt
= VIDEO_PALETTE_RGB565
;
651 fmt
= VIDEO_PALETTE_RGB24
;
655 fmt
= VIDEO_PALETTE_RGB32
;
663 zr36057_set_vfe(zr
, zr
->window
.width
, zr
->window
.height
, fmt
);
665 /* Start and length of each line MUST be 4-byte aligned.
666 This should be allready checked before the call to this routine.
667 All error messages are internal driver checking only! */
669 /* video display top and bottom registers */
671 reg
= (u32
) zr
->buffer
.base
673 + zr
->window
.y
* zr
->buffer
.bytesperline
;
674 btwrite(reg
, ZR36057_VDTR
);
676 printk(KERN_ERR
"%s: zr36057_overlay: video_address not aligned\n", zr
->name
);
677 if (zr
->video_interlace
)
678 reg
+= zr
->buffer
.bytesperline
;
679 btwrite(reg
, ZR36057_VDBR
);
681 /* video stride, status, and frame grab register */
683 reg
= zr
->buffer
.bytesperline
- zr
->window
.width
* bpp
;
684 if (zr
->video_interlace
)
685 reg
+= zr
->buffer
.bytesperline
;
687 printk(KERN_ERR
"%s: zr36057_overlay: video_stride not aligned\n", zr
->name
);
688 reg
= (reg
<< ZR36057_VSSFGR_DispStride
);
689 reg
|= ZR36057_VSSFGR_VidOvf
; /* clear overflow status */
690 btwrite(reg
, ZR36057_VSSFGR
);
692 /* Set overlay clipping */
694 if (zr
->window
.clipcount
)
695 btor(ZR36057_OCR_OvlEnable
, ZR36057_OCR
);
697 /* ... and switch it on */
699 btor(ZR36057_VDCR_VidEn
, ZR36057_VDCR
);
703 btand(~ZR36057_VDCR_VidEn
, ZR36057_VDCR
);
708 * The overlay mask has one bit for each pixel on a scan line,
709 * and the maximum window size is BUZ_MAX_WIDTH * BUZ_MAX_HEIGHT pixels.
711 static void write_overlay_mask(struct zoran
*zr
, struct video_clip
*vp
, int count
)
713 unsigned mask_line_size
= (BUZ_MAX_WIDTH
+ 31) / 32;
715 int x
, y
, width
, height
;
719 /* fill mask with one bits */
720 memset(zr
->overlay_mask
, ~0, mask_line_size
* 4 * BUZ_MAX_HEIGHT
);
723 for (i
= 0; i
< count
; ++i
) {
724 /* pick up local copy of clip */
728 height
= vp
[i
].height
;
730 /* trim clips that extend beyond the window */
739 if (x
+ width
> zr
->window
.width
) {
740 width
= zr
->window
.width
- x
;
742 if (y
+ height
> zr
->window
.height
) {
743 height
= zr
->window
.height
- y
;
745 /* ignore degenerate clips */
752 /* apply clip for each scan line */
753 for (j
= 0; j
< height
; ++j
) {
754 /* reset bit for each pixel */
755 /* this can be optimized later if need be */
756 mask
= zr
->overlay_mask
+ (y
+ j
) * mask_line_size
;
757 for (k
= 0; k
< width
; ++k
) {
758 mask
[(x
+ k
) / 32] &= ~((u32
) 1 << (x
+ k
) % 32);
764 /* Enable/Disable uncompressed memory grabbing of the 36057 */
766 static void zr36057_set_memgrab(struct zoran
*zr
, int mode
)
769 if (btread(ZR36057_VSSFGR
) & (ZR36057_VSSFGR_SnapShot
| ZR36057_VSSFGR_FrameGrab
))
770 printk(KERN_WARNING
"%s: zr36057_set_memgrab_on with SnapShot or FrameGrab on ???\n", zr
->name
);
772 /* switch on VSync interrupts */
774 btwrite(IRQ_MASK
, ZR36057_ISR
); // Clear Interrupts
776 btor(ZR36057_ICR_GIRQ0
, ZR36057_ICR
);
778 /* enable SnapShot */
780 btor(ZR36057_VSSFGR_SnapShot
, ZR36057_VSSFGR
);
782 /* Set zr36057 video front end and enable video */
785 zr36057_set_vfe(zr
, zr
->gwidth
> 720 ? 720 : zr
->gwidth
, zr
->gheight
, zr
->gformat
);
787 zr36057_set_vfe(zr
, zr
->gwidth
, zr
->gheight
, zr
->gformat
);
790 zr
->v4l_memgrab_active
= 1;
792 zr
->v4l_memgrab_active
= 0;
794 /* switch off VSync interrupts */
796 btand(~ZR36057_ICR_GIRQ0
, ZR36057_ICR
);
798 /* reenable grabbing to screen if it was running */
800 if (zr
->v4l_overlay_active
) {
801 zr36057_overlay(zr
, 1);
803 btand(~ZR36057_VDCR_VidEn
, ZR36057_VDCR
);
804 btand(~ZR36057_VSSFGR_SnapShot
, ZR36057_VSSFGR
);
809 static int wait_grab_pending(struct zoran
*zr
)
813 /* wait until all pending grabs are finished */
815 if (!zr
->v4l_memgrab_active
)
818 while (zr
->v4l_pend_tail
!= zr
->v4l_pend_head
) {
819 interruptible_sleep_on(&zr
->v4l_capq
);
820 if (signal_pending(current
))
824 spin_lock_irqsave(&zr
->lock
, flags
);
825 zr36057_set_memgrab(zr
, 0);
826 spin_unlock_irqrestore(&zr
->lock
, flags
);
832 * V4L Buffer grabbing
835 static int v4l_grab(struct zoran
*zr
, struct video_mmap
*mp
)
841 * There is a long list of limitations to what is allowed to be grabbed
842 * We don't output error messages her, since some programs (e.g. xawtv)
843 * just try several settings to find out what is valid or not.
846 /* No grabbing outside the buffer range! */
848 if (mp
->frame
>= v4l_nbufs
|| mp
->frame
< 0)
851 /* Check size and format of the grab wanted */
853 if (mp
->height
< BUZ_MIN_HEIGHT
|| mp
->width
< BUZ_MIN_WIDTH
)
855 if (mp
->height
> BUZ_MAX_HEIGHT
|| mp
->width
> BUZ_MAX_WIDTH
)
858 bpp
= format2bpp(mp
->format
);
862 /* Check against available buffer size */
864 if (mp
->height
* mp
->width
* bpp
> v4l_bufsize
)
867 /* The video front end needs 4-byte alinged line sizes */
869 if ((bpp
== 2 && (mp
->width
& 1)) || (bpp
== 3 && (mp
->width
& 3)))
873 * To minimize the time spent in the IRQ routine, we avoid setting up
874 * the video front end there.
875 * If this grab has different parameters from a running streaming capture
876 * we stop the streaming capture and start it over again.
879 if (zr
->v4l_memgrab_active
&&
880 (zr
->gwidth
!= mp
->width
|| zr
->gheight
!= mp
->height
|| zr
->gformat
!= mp
->format
)) {
881 res
= wait_grab_pending(zr
);
885 zr
->gwidth
= mp
->width
;
886 zr
->gheight
= mp
->height
;
887 zr
->gformat
= mp
->format
;
888 zr
->gbpl
= bpp
* zr
->gwidth
;
891 spin_lock_irqsave(&zr
->lock
, flags
);
893 /* make sure a grab isn't going on currently with this buffer */
895 switch (zr
->v4l_gbuf
[mp
->frame
].state
) {
899 res
= -EBUSY
; /* what are you doing? */
904 /* since there is at least one unused buffer there's room for at least one more pend[] entry */
905 zr
->v4l_pend
[zr
->v4l_pend_head
++ & V4L_MASK_FRAME
] = mp
->frame
;
906 zr
->v4l_gbuf
[mp
->frame
].state
= BUZ_STATE_PEND
;
912 /* put the 36057 into frame grabbing mode */
914 if (!res
&& !zr
->v4l_memgrab_active
)
915 zr36057_set_memgrab(zr
, 1);
917 spin_unlock_irqrestore(&zr
->lock
, flags
);
923 * Sync on a V4L buffer
926 static int v4l_sync(struct zoran
*zr
, int frame
)
931 /* check passed-in frame number */
932 if (frame
>= v4l_nbufs
|| frame
< 0) {
933 printk(KERN_ERR
"%s: v4l_sync: frame %d is invalid\n", zr
->name
, frame
);
936 /* Check if is buffer was queued at all */
938 if (zr
->v4l_gbuf
[frame
].state
== BUZ_STATE_USER
) {
939 // printk(KERN_ERR "%s: v4l_sync: Trying to sync on a buffer which was not queued?\n", zr->name);
942 /* wait on this buffer to get ready */
944 while (zr
->v4l_gbuf
[frame
].state
== BUZ_STATE_PEND
) {
945 interruptible_sleep_on(&zr
->v4l_capq
);
946 if (signal_pending(current
))
950 /* buffer should now be in BUZ_STATE_DONE */
952 if (zr
->v4l_gbuf
[frame
].state
!= BUZ_STATE_DONE
)
953 printk(KERN_ERR
"%s: v4l_sync - internal error\n", zr
->name
);
955 /* Check if streaming capture has finished */
957 spin_lock_irqsave(&zr
->lock
, flags
);
959 if (zr
->v4l_pend_tail
== zr
->v4l_pend_head
)
960 zr36057_set_memgrab(zr
, 0);
962 spin_unlock_irqrestore(&zr
->lock
, flags
);
966 /*****************************************************************************
968 * Set up the Buz-specific MJPEG part *
970 *****************************************************************************/
973 * Wait til post office is no longer busy
976 static int post_office_wait(struct zoran
*zr
)
981 while (((por
= btread(ZR36057_POR
)) & (ZR36057_POR_POPen
| ZR36057_POR_POTime
)) == ZR36057_POR_POPen
) {
985 printk(KERN_ERR
"%s: timeout on post office.\n", zr
->name
);
988 /* wait for something to happen */
990 if ((por
& ZR36057_POR_POPen
) != 0) {
991 printk(KERN_WARNING
"%s: pop pending %08x\n", zr
->name
, por
);
994 if ((por
& (ZR36057_POR_POTime
| ZR36057_POR_POPen
)) != 0) {
995 printk(KERN_WARNING
"%s: pop timeout %08x\n", zr
->name
, por
);
1001 static int post_office_write(struct zoran
*zr
, unsigned guest
, unsigned reg
, unsigned value
)
1005 post_office_wait(zr
);
1006 por
= ZR36057_POR_PODir
| ZR36057_POR_POTime
| ((guest
& 7) << 20) | ((reg
& 7) << 16) | (value
& 0xFF);
1007 btwrite(por
, ZR36057_POR
);
1008 return post_office_wait(zr
);
1011 static int post_office_read(struct zoran
*zr
, unsigned guest
, unsigned reg
)
1015 post_office_wait(zr
);
1016 por
= ZR36057_POR_POTime
| ((guest
& 7) << 20) | ((reg
& 7) << 16);
1017 btwrite(por
, ZR36057_POR
);
1018 if (post_office_wait(zr
) < 0) {
1021 return btread(ZR36057_POR
) & 0xFF;
1024 static int zr36060_write_8(struct zoran
*zr
, unsigned reg
, unsigned val
)
1026 if (post_office_wait(zr
)
1027 || post_office_write(zr
, 0, 1, reg
>> 8)
1028 || post_office_write(zr
, 0, 2, reg
)) {
1031 return post_office_write(zr
, 0, 3, val
);
1034 static int zr36060_write_16(struct zoran
*zr
, unsigned reg
, unsigned val
)
1036 if (zr36060_write_8(zr
, reg
+ 0, val
>> 8)) {
1039 return zr36060_write_8(zr
, reg
+ 1, val
>> 0);
1042 static int zr36060_write_24(struct zoran
*zr
, unsigned reg
, unsigned val
)
1044 if (zr36060_write_8(zr
, reg
+ 0, val
>> 16)) {
1047 return zr36060_write_16(zr
, reg
+ 1, val
>> 0);
1050 static int zr36060_write_32(struct zoran
*zr
, unsigned reg
, unsigned val
)
1052 if (zr36060_write_16(zr
, reg
+ 0, val
>> 16)) {
1055 return zr36060_write_16(zr
, reg
+ 2, val
>> 0);
1058 static u32
zr36060_read_8(struct zoran
*zr
, unsigned reg
)
1060 if (post_office_wait(zr
)
1061 || post_office_write(zr
, 0, 1, reg
>> 8)
1062 || post_office_write(zr
, 0, 2, reg
)) {
1065 return post_office_read(zr
, 0, 3) & 0xFF;
1068 static int zr36060_reset(struct zoran
*zr
)
1070 return post_office_write(zr
, 3, 0, 0);
1073 static void zr36060_sleep(struct zoran
*zr
, int sleep
)
1075 GPIO(zr
, 1, !sleep
);
1079 static void zr36060_set_jpg(struct zoran
*zr
, enum zoran_codec_mode mode
)
1085 reg
= (1 << 0) /* CodeMstr */
1086 |(0 << 2) /* CFIS=0 */
1087 |(0 << 6) /* Endian=0 */
1088 |(0 << 7); /* Code16=0 */
1089 zr36060_write_8(zr
, 0x002, reg
);
1093 case BUZ_MODE_MOTION_DECOMPRESS
:
1094 case BUZ_MODE_STILL_DECOMPRESS
:
1095 reg
= 0x00; /* Codec mode = decompression */
1098 case BUZ_MODE_MOTION_COMPRESS
:
1099 case BUZ_MODE_STILL_COMPRESS
:
1101 reg
= 0xa4; /* Codec mode = compression with variable scale factor */
1105 zr36060_write_8(zr
, 0x003, reg
);
1107 reg
= 0x00; /* reserved, mbz */
1108 zr36060_write_8(zr
, 0x004, reg
);
1110 reg
= 0xff; /* 510 bits/block */
1111 zr36060_write_8(zr
, 0x005, reg
);
1114 reg
= (zr
->params
.jpeg_markers
) & 0x38; /* DRI, DQT, DHT */
1115 if (zr
->params
.COM_len
)
1116 reg
|= JPEG_MARKER_COM
;
1117 if (zr
->params
.APP_len
)
1118 reg
|= JPEG_MARKER_APP
;
1119 zr36060_write_8(zr
, 0x006, reg
);
1121 reg
= (0 << 3) /* DATERR=0 */
1122 |(0 << 2) /* END=0 */
1123 |(0 << 1) /* EOI=0 */
1124 |(0 << 0); /* EOAV=0 */
1125 zr36060_write_8(zr
, 0x007, reg
);
1129 /* Target field size in pixels: */
1130 tvn
= &tvnorms
[zr
->params
.norm
];
1131 size
= (tvn
->Ha
/ 2) * (tvn
->Wa
) / (zr
->params
.HorDcm
) / (zr
->params
.VerDcm
);
1133 /* Target compressed field size in bits: */
1134 size
= size
* 16; /* uncompressed size in bits */
1135 size
= size
* zr
->params
.quality
/ 400; /* quality = 100 is a compression ratio 1:4 */
1137 /* Lower limit (arbitrary, 1 KB) */
1141 /* Upper limit: 7/8 of the code buffers */
1142 if (size
* zr
->params
.field_per_buff
> zr
->jpg_bufsize
* 7)
1143 size
= zr
->jpg_bufsize
* 7 / zr
->params
.field_per_buff
;
1146 zr36060_write_32(zr
, 0x009, reg
);
1148 /* how do we set initial SF as a function of quality parameter? */
1149 reg
= 0x0100; /* SF=1.0 */
1150 zr36060_write_16(zr
, 0x011, reg
);
1152 reg
= 0x00ffffff; /* AF=max */
1153 zr36060_write_24(zr
, 0x013, reg
);
1155 reg
= 0x0000; /* test */
1156 zr36060_write_16(zr
, 0x024, reg
);
1159 static void zr36060_set_video(struct zoran
*zr
, enum zoran_codec_mode mode
)
1164 reg
= (0 << 7) /* Video8=0 */
1165 |(0 << 6) /* Range=0 */
1166 |(0 << 3) /* FlDet=0 */
1167 |(1 << 2) /* FlVedge=1 */
1168 |(0 << 1) /* FlExt=0 */
1169 |(0 << 0); /* SyncMstr=0 */
1171 /* According to ZR36067 documentation, FlDet should correspond
1172 to the odd_even flag of the ZR36067 */
1173 if (zr
->params
.odd_even
)
1176 if (mode
!= BUZ_MODE_STILL_DECOMPRESS
) {
1177 /* limit pixels to range 16..235 as per CCIR-601 */
1178 reg
|= (1 << 6); /* Range=1 */
1180 zr36060_write_8(zr
, 0x030, reg
);
1182 reg
= (0 << 7) /* VCLKPol=0 */
1183 |(0 << 6) /* PValPol=0 */
1184 |(1 << 5) /* PoePol=1 */
1185 |(0 << 4) /* SImgPol=0 */
1186 |(0 << 3) /* BLPol=0 */
1187 |(0 << 2) /* FlPol=0 */
1188 |(0 << 1) /* HSPol=0, sync on falling edge */
1189 |(1 << 0); /* VSPol=1 */
1190 zr36060_write_8(zr
, 0x031, reg
);
1192 switch (zr
->params
.HorDcm
) {
1196 break; /* HScale = 0 */
1200 break; /* HScale = 1 */
1204 break; /* HScale = 2 */
1206 if (zr
->params
.VerDcm
== 2)
1208 zr36060_write_8(zr
, 0x032, reg
);
1210 reg
= 0x80; /* BackY */
1211 zr36060_write_8(zr
, 0x033, reg
);
1213 reg
= 0xe0; /* BackU */
1214 zr36060_write_8(zr
, 0x034, reg
);
1216 reg
= 0xe0; /* BackV */
1217 zr36060_write_8(zr
, 0x035, reg
);
1219 /* sync generator */
1221 tvn
= &tvnorms
[zr
->params
.norm
];
1223 reg
= tvn
->Ht
- 1; /* Vtotal */
1224 zr36060_write_16(zr
, 0x036, reg
);
1226 reg
= tvn
->Wt
- 1; /* Htotal */
1227 zr36060_write_16(zr
, 0x038, reg
);
1229 reg
= 6 - 1; /* VsyncSize */
1230 zr36060_write_8(zr
, 0x03a, reg
);
1232 reg
= 100 - 1; /* HsyncSize */
1233 zr36060_write_8(zr
, 0x03b, reg
);
1235 reg
= tvn
->VStart
- 1; /* BVstart */
1236 zr36060_write_8(zr
, 0x03c, reg
);
1238 reg
+= tvn
->Ha
/ 2; /* BVend */
1239 zr36060_write_16(zr
, 0x03e, reg
);
1241 reg
= tvn
->HStart
- 1; /* BHstart */
1242 zr36060_write_8(zr
, 0x03d, reg
);
1244 reg
+= tvn
->Wa
; /* BHend */
1245 zr36060_write_16(zr
, 0x040, reg
);
1248 reg
= zr
->params
.img_y
+ tvn
->VStart
; /* Vstart */
1249 zr36060_write_16(zr
, 0x042, reg
);
1251 reg
+= zr
->params
.img_height
; /* Vend */
1252 zr36060_write_16(zr
, 0x044, reg
);
1254 reg
= zr
->params
.img_x
+ tvn
->HStart
; /* Hstart */
1255 zr36060_write_16(zr
, 0x046, reg
);
1257 reg
+= zr
->params
.img_width
; /* Hend */
1258 zr36060_write_16(zr
, 0x048, reg
);
1261 reg
= zr
->params
.img_y
+ tvn
->VStart
; /* SVstart */
1262 zr36060_write_16(zr
, 0x04a, reg
);
1264 reg
+= zr
->params
.img_height
; /* SVend */
1265 zr36060_write_16(zr
, 0x04c, reg
);
1267 reg
= zr
->params
.img_x
+ tvn
->HStart
; /* SHstart */
1268 zr36060_write_16(zr
, 0x04e, reg
);
1270 reg
+= zr
->params
.img_width
; /* SHend */
1271 zr36060_write_16(zr
, 0x050, reg
);
1274 static void zr36060_set_jpg_SOF(struct zoran
*zr
)
1279 reg
= 0xffc0; /* SOF marker */
1280 zr36060_write_16(zr
, 0x060, reg
);
1282 reg
= 17; /* SOF length */
1283 zr36060_write_16(zr
, 0x062, reg
);
1285 reg
= 8; /* precision 8 bits */
1286 zr36060_write_8(zr
, 0x064, reg
);
1288 reg
= zr
->params
.img_height
/ zr
->params
.VerDcm
; /* image height */
1289 zr36060_write_16(zr
, 0x065, reg
);
1291 reg
= zr
->params
.img_width
/ zr
->params
.HorDcm
; /* image width */
1292 zr36060_write_16(zr
, 0x067, reg
);
1294 reg
= 3; /* 3 color components */
1295 zr36060_write_8(zr
, 0x069, reg
);
1297 reg
= 0x002100; /* Y component */
1298 zr36060_write_24(zr
, 0x06a, reg
);
1300 reg
= 0x011101; /* U component */
1301 zr36060_write_24(zr
, 0x06d, reg
);
1303 reg
= 0x021101; /* V component */
1304 zr36060_write_24(zr
, 0x070, reg
);
1307 static void zr36060_set_jpg_SOS(struct zoran
*zr
)
1312 reg
= 0xffda; /* SOS marker */
1313 zr36060_write_16(zr
, 0x07a, reg
);
1315 reg
= 12; /* SOS length */
1316 zr36060_write_16(zr
, 0x07c, reg
);
1318 reg
= 3; /* 3 color components */
1319 zr36060_write_8(zr
, 0x07e, reg
);
1321 reg
= 0x0000; /* Y component */
1322 zr36060_write_16(zr
, 0x07f, reg
);
1324 reg
= 0x0111; /* U component */
1325 zr36060_write_16(zr
, 0x081, reg
);
1327 reg
= 0x0211; /* V component */
1328 zr36060_write_16(zr
, 0x083, reg
);
1330 reg
= 0x003f00; /* Start, end spectral scans */
1331 zr36060_write_24(zr
, 0x085, reg
);
1334 static void zr36060_set_jpg_DRI(struct zoran
*zr
)
1339 reg
= 0xffdd; /* DRI marker */
1340 zr36060_write_16(zr
, 0x0c0, reg
);
1342 reg
= 4; /* DRI length */
1343 zr36060_write_16(zr
, 0x0c2, reg
);
1345 reg
= 8; /* length in MCUs */
1346 zr36060_write_16(zr
, 0x0c4, reg
);
1349 static void zr36060_set_jpg_DQT(struct zoran
*zr
)
1353 static const u8 dqt
[] =
1355 0xff, 0xdb, /* DHT marker */
1356 0x00, 0x84, /* DHT length */
1357 0x00, /* table ID 0 */
1358 0x10, 0x0b, 0x0c, 0x0e, 0x0c, 0x0a, 0x10, 0x0e,
1359 0x0d, 0x0e, 0x12, 0x11, 0x10, 0x13, 0x18, 0x28,
1360 0x1a, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23, 0x25,
1361 0x1d, 0x28, 0x3a, 0x33, 0x3d, 0x3c, 0x39, 0x33,
1362 0x38, 0x37, 0x40, 0x48, 0x5c, 0x4e, 0x40, 0x44,
1363 0x57, 0x45, 0x37, 0x38, 0x50, 0x6d, 0x51, 0x57,
1364 0x5f, 0x62, 0x67, 0x68, 0x67, 0x3e, 0x4d, 0x71,
1365 0x79, 0x70, 0x64, 0x78, 0x5c, 0x65, 0x67, 0x63,
1366 0x01, /* table ID 1 */
1367 0x11, 0x12, 0x12, 0x18, 0x15, 0x18, 0x2f, 0x1a,
1368 0x1a, 0x2f, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63,
1369 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1370 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1371 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1372 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1373 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1374 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63
1377 /* write fixed quantitization tables */
1379 for (i
= 0; i
< sizeof(dqt
); ++i
) {
1380 zr36060_write_8(zr
, adr
++, dqt
[i
]);
1384 static void zr36060_set_jpg_DHT(struct zoran
*zr
)
1388 static const u8 dht
[] =
1390 0xff, 0xc4, /* DHT marker */
1391 0x01, 0xa2, /* DHT length */
1392 0x00, /* table class 0, ID 0 */
1393 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, /* # codes of length 1..8 */
1394 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* # codes of length 8..16 */
1395 0x00, /* values for codes of length 2 */
1396 0x01, 0x02, 0x03, 0x04, 0x05, /* values for codes of length 3 */
1397 0x06, /* values for codes of length 4 */
1398 0x07, /* values for codes of length 5 */
1399 0x08, /* values for codes of length 6 */
1400 0x09, /* values for codes of length 7 */
1401 0x0a, /* values for codes of length 8 */
1402 0x0b, /* values for codes of length 9 */
1403 0x01, /* table class 0, ID 1 */
1404 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* # codes of length 1..8 */
1405 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, /* # codes of length 9..16 */
1406 0x00, 0x01, 0x02, /* values for codes of length 2 */
1407 0x03, /* values for codes of length 3 */
1408 0x04, /* values for codes of length 4 */
1409 0x05, /* values for codes of length 5 */
1410 0x06, /* values for codes of length 6 */
1411 0x07, /* values for codes of length 7 */
1412 0x08, /* values for codes of length 8 */
1413 0x09, /* values for codes of length 9 */
1414 0x0a, /* values for codes of length 10 */
1415 0x0b, /* values for codes of length 11 */
1417 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
1418 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d,
1419 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
1420 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
1421 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
1422 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
1423 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
1424 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
1425 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
1426 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
1427 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
1428 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
1429 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
1430 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
1431 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
1432 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
1433 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
1434 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
1435 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
1436 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
1437 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
1438 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
1439 0xf9, 0xfa, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04,
1440 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00,
1441 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11,
1442 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51,
1443 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08,
1444 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23,
1445 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a,
1446 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18,
1447 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35,
1448 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45,
1449 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55,
1450 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65,
1451 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75,
1452 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84,
1453 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93,
1454 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2,
1455 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
1456 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
1457 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8,
1458 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
1459 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
1460 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5,
1461 0xf6, 0xf7, 0xf8, 0xf9, 0xfa
1464 /* write fixed Huffman tables */
1466 for (i
= 0; i
< sizeof(dht
); ++i
) {
1467 zr36060_write_8(zr
, adr
++, dht
[i
]);
1471 static void zr36060_set_jpg_APP(struct zoran
*zr
)
1478 len
= zr
->params
.APP_len
;
1484 i
= zr
->params
.APPn
;
1490 reg
= 0xffe0 + i
; /* APPn marker */
1491 zr36060_write_16(zr
, 0x380, reg
);
1493 reg
= len
+ 2; /* APPn len */
1494 zr36060_write_16(zr
, 0x382, reg
);
1496 /* write APPn data */
1498 for (i
= 0; i
< 60; i
++) {
1499 zr36060_write_8(zr
, adr
++, (i
< len
? zr
->params
.APP_data
[i
] : 0));
1503 static void zr36060_set_jpg_COM(struct zoran
*zr
)
1510 len
= zr
->params
.COM_len
;
1516 reg
= 0xfffe; /* COM marker */
1517 zr36060_write_16(zr
, 0x3c0, reg
);
1519 reg
= len
+ 2; /* COM len */
1520 zr36060_write_16(zr
, 0x3c2, reg
);
1522 /* write COM data */
1524 for (i
= 0; i
< 60; i
++) {
1525 zr36060_write_8(zr
, adr
++, (i
< len
? zr
->params
.COM_data
[i
] : 0));
1529 static void zr36060_set_cap(struct zoran
*zr
, enum zoran_codec_mode mode
)
1537 reg
= (0 << 7) /* Load=0 */
1538 |(1 << 0); /* SynRst=1 */
1539 zr36060_write_8(zr
, 0x000, reg
);
1541 zr36060_set_jpg(zr
, mode
);
1542 zr36060_set_video(zr
, mode
);
1543 zr36060_set_jpg_SOF(zr
);
1544 zr36060_set_jpg_SOS(zr
);
1545 zr36060_set_jpg_DRI(zr
);
1546 zr36060_set_jpg_DQT(zr
);
1547 zr36060_set_jpg_DHT(zr
);
1548 zr36060_set_jpg_APP(zr
);
1549 zr36060_set_jpg_COM(zr
);
1551 reg
= (1 << 7) /* Load=1 */
1552 |(0 << 0); /* SynRst=0 */
1553 zr36060_write_8(zr
, 0x000, reg
);
1555 /* wait for codec to unbusy */
1556 for (i
= 0; i
< 1000; ++i
) {
1557 reg
= zr36060_read_8(zr
, 0x001);
1558 if ((reg
& (1 << 7)) == 0) {
1559 DEBUG(printk(KERN_DEBUG
"060: loaded, loops=%u\n", i
));
1564 printk(KERN_INFO
"060: stuck busy, statux=%02x\n", reg
);
1567 static void zr36057_set_jpg(struct zoran
*zr
, enum zoran_codec_mode mode
)
1573 tvn
= &tvnorms
[zr
->params
.norm
];
1575 /* assert P_Reset */
1576 btwrite(0, ZR36057_JPC
);
1578 /* re-initialize DMA ring stuff */
1579 zr
->jpg_que_head
= 0;
1580 zr
->jpg_dma_head
= 0;
1581 zr
->jpg_dma_tail
= 0;
1582 zr
->jpg_que_tail
= 0;
1583 zr
->jpg_seq_num
= 0;
1584 for (i
= 0; i
< BUZ_NUM_STAT_COM
; ++i
) {
1585 zr
->stat_com
[i
] = 1; /* mark as unavailable to zr36057 */
1587 for (i
= 0; i
< zr
->jpg_nbufs
; i
++) {
1588 zr
->jpg_gbuf
[i
].state
= BUZ_STATE_USER
; /* nothing going on */
1591 /* MJPEG compression mode */
1594 case BUZ_MODE_MOTION_COMPRESS
:
1596 reg
= ZR36057_JMC_MJPGCmpMode
;
1599 case BUZ_MODE_MOTION_DECOMPRESS
:
1600 reg
= ZR36057_JMC_MJPGExpMode
;
1601 reg
|= ZR36057_JMC_SyncMstr
;
1602 /* RJ: The following is experimental - improves the output to screen */
1603 if (zr
->params
.VFIFO_FB
)
1604 reg
|= ZR36057_JMC_VFIFO_FB
;
1607 case BUZ_MODE_STILL_COMPRESS
:
1608 reg
= ZR36057_JMC_JPGCmpMode
;
1611 case BUZ_MODE_STILL_DECOMPRESS
:
1612 reg
= ZR36057_JMC_JPGExpMode
;
1616 reg
|= ZR36057_JMC_JPG
;
1617 if (zr
->params
.field_per_buff
== 1)
1618 reg
|= ZR36057_JMC_Fld_per_buff
;
1619 btwrite(reg
, ZR36057_JMC
);
1622 btor(ZR36057_VFEVCR_VSPol
, ZR36057_VFEVCR
);
1623 reg
= (6 << ZR36057_VSP_VsyncSize
) | (tvn
->Ht
<< ZR36057_VSP_FrmTot
);
1624 btwrite(reg
, ZR36057_VSP
);
1625 reg
= ((zr
->params
.img_y
+ tvn
->VStart
) << ZR36057_FVAP_NAY
)
1626 | (zr
->params
.img_height
<< ZR36057_FVAP_PAY
);
1627 btwrite(reg
, ZR36057_FVAP
);
1630 btor(ZR36057_VFEHCR_HSPol
, ZR36057_VFEHCR
);
1631 reg
= ((tvn
->Wt
- 100) << ZR36057_HSP_HsyncStart
) | (tvn
->Wt
<< ZR36057_HSP_LineTot
);
1632 btwrite(reg
, ZR36057_HSP
);
1633 reg
= ((zr
->params
.img_x
+ tvn
->HStart
) << ZR36057_FHAP_NAX
)
1634 | (zr
->params
.img_width
<< ZR36057_FHAP_PAX
);
1635 btwrite(reg
, ZR36057_FHAP
);
1637 /* field process parameters */
1638 if (zr
->params
.odd_even
)
1639 reg
= ZR36057_FPP_Odd_Even
;
1642 btwrite(reg
, ZR36057_FPP
);
1644 /* Set proper VCLK Polarity, else colors will be wrong during playback */
1645 btor(ZR36057_VFESPFR_VCLKPol
, ZR36057_VFESPFR
);
1647 /* code base address and FIFO threshold */
1648 reg
= virt_to_bus(zr
->stat_com
);
1649 btwrite(reg
, ZR36057_JCBA
);
1651 btwrite(reg
, ZR36057_JCFT
);
1653 /* JPEG codec guest ID */
1654 reg
= (1 << ZR36057_JCGI_JPEGuestID
) | (0 << ZR36057_JCGI_JPEGuestReg
);
1655 btwrite(reg
, ZR36057_JCGI
);
1657 /* Code transfer guest ID */
1658 reg
= (0 << ZR36057_MCTCR_CodGuestID
) | (3 << ZR36057_MCTCR_CodGuestReg
);
1659 reg
|= ZR36057_MCTCR_CFlush
;
1660 btwrite(reg
, ZR36057_MCTCR
);
1662 /* deassert P_Reset */
1663 btwrite(ZR36057_JPC_P_Reset
, ZR36057_JPC
);
1666 static void zr36057_enable_jpg(struct zoran
*zr
, enum zoran_codec_mode mode
)
1668 static int zero
= 0;
1673 case BUZ_MODE_MOTION_COMPRESS
:
1674 zr36060_set_cap(zr
, mode
);
1675 zr36057_set_jpg(zr
, mode
);
1676 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEODECODER
, DECODER_ENABLE_OUTPUT
, &one
);
1677 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEOENCODER
, ENCODER_SET_INPUT
, &zero
);
1679 /* deassert P_Reset, assert Code transfer enable */
1680 btwrite(IRQ_MASK
, ZR36057_ISR
);
1681 btand(~ZR36057_MCTCR_CFlush
, ZR36057_MCTCR
);
1684 case BUZ_MODE_MOTION_DECOMPRESS
:
1685 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEODECODER
, DECODER_ENABLE_OUTPUT
, &zero
);
1686 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEOENCODER
, ENCODER_SET_INPUT
, &one
);
1687 zr36060_set_cap(zr
, mode
);
1688 zr36057_set_jpg(zr
, mode
);
1690 /* deassert P_Reset, assert Code transfer enable */
1691 btwrite(IRQ_MASK
, ZR36057_ISR
);
1692 btand(~ZR36057_MCTCR_CFlush
, ZR36057_MCTCR
);
1697 /* shut down processing */
1698 btor(ZR36057_MCTCR_CFlush
, ZR36057_MCTCR
);
1699 btwrite(ZR36057_JPC_P_Reset
, ZR36057_JPC
);
1700 btand(~ZR36057_JMC_VFIFO_FB
, ZR36057_JMC
);
1701 btand(~ZR36057_JMC_SyncMstr
, ZR36057_JMC
);
1702 btand(~ZR36057_JMC_Go_en
, ZR36057_JMC
);
1703 btwrite(0, ZR36057_ISR
);
1705 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEODECODER
, DECODER_ENABLE_OUTPUT
, &one
);
1706 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEOENCODER
, ENCODER_SET_INPUT
, &zero
);
1710 zr
->codec_mode
= mode
;
1714 * Queue a MJPEG buffer for capture/playback
1717 static int jpg_qbuf(struct zoran
*zr
, int frame
, enum zoran_codec_mode mode
)
1719 unsigned long flags
;
1722 /* Check if buffers are allocated */
1724 if (!zr
->jpg_buffers_allocated
) {
1725 printk(KERN_ERR
"%s: jpg_qbuf: buffers not yet allocated\n", zr
->name
);
1728 /* Does the user want to stop streaming? */
1731 if (zr
->codec_mode
== mode
) {
1732 zr36057_enable_jpg(zr
, BUZ_MODE_IDLE
);
1735 printk(KERN_ERR
"%s: jpg_qbuf - stop streaming but not in streaming mode\n", zr
->name
);
1739 /* No grabbing outside the buffer range! */
1741 if (frame
>= zr
->jpg_nbufs
) {
1742 printk(KERN_ERR
"%s: jpg_qbuf: buffer %d out of range\n", zr
->name
, frame
);
1745 /* what is the codec mode right now? */
1747 if (zr
->codec_mode
== BUZ_MODE_IDLE
) {
1748 /* Ok load up the zr36060 and go */
1749 zr36057_enable_jpg(zr
, mode
);
1750 } else if (zr
->codec_mode
!= mode
) {
1751 /* wrong codec mode active - invalid */
1752 printk(KERN_ERR
"%s: jpg_qbuf - codec in wrong mode\n", zr
->name
);
1755 spin_lock_irqsave(&zr
->lock
, flags
);
1757 /* make sure a grab isn't going on currently with this buffer */
1759 switch (zr
->jpg_gbuf
[frame
].state
) {
1763 case BUZ_STATE_PEND
:
1764 case BUZ_STATE_DONE
:
1765 res
= -EBUSY
; /* what are you doing? */
1768 case BUZ_STATE_USER
:
1769 /* since there is at least one unused buffer there's room for at least one more pend[] entry */
1770 zr
->jpg_pend
[zr
->jpg_que_head
++ & BUZ_MASK_FRAME
] = frame
;
1771 zr
->jpg_gbuf
[frame
].state
= BUZ_STATE_PEND
;
1772 zoran_feed_stat_com(zr
);
1778 spin_unlock_irqrestore(&zr
->lock
, flags
);
1780 /* Start the zr36060 when the first frame is queued */
1781 if (zr
->jpg_que_head
== 1) {
1782 btor(ZR36057_JMC_Go_en
, ZR36057_JMC
);
1783 btwrite(ZR36057_JPC_P_Reset
| ZR36057_JPC_CodTrnsEn
| ZR36057_JPC_Active
, ZR36057_JPC
);
1789 * Sync on a MJPEG buffer
1792 static int jpg_sync(struct zoran
*zr
, struct zoran_sync
*bs
)
1794 unsigned long flags
;
1797 if (zr
->codec_mode
!= BUZ_MODE_MOTION_DECOMPRESS
&&
1798 zr
->codec_mode
!= BUZ_MODE_MOTION_COMPRESS
) {
1801 while (zr
->jpg_que_tail
== zr
->jpg_dma_tail
) {
1802 interruptible_sleep_on(&zr
->jpg_capq
);
1803 if (signal_pending(current
))
1804 return -ERESTARTSYS
;
1807 spin_lock_irqsave(&zr
->lock
, flags
);
1809 frame
= zr
->jpg_pend
[zr
->jpg_que_tail
++ & BUZ_MASK_FRAME
];
1811 /* buffer should now be in BUZ_STATE_DONE */
1813 if (zr
->jpg_gbuf
[frame
].state
!= BUZ_STATE_DONE
)
1814 printk(KERN_ERR
"%s: jpg_sync - internal error\n", zr
->name
);
1816 *bs
= zr
->jpg_gbuf
[frame
].bs
;
1817 zr
->jpg_gbuf
[frame
].state
= BUZ_STATE_USER
;
1819 spin_unlock_irqrestore(&zr
->lock
, flags
);
1824 /* when this is called the spinlock must be held */
1825 static void zoran_feed_stat_com(struct zoran
*zr
)
1827 /* move frames from pending queue to DMA */
1829 int frame
, i
, max_stat_com
;
1831 max_stat_com
= (zr
->params
.TmpDcm
== 1) ? BUZ_NUM_STAT_COM
: (BUZ_NUM_STAT_COM
>> 1);
1833 while ((zr
->jpg_dma_head
- zr
->jpg_dma_tail
) < max_stat_com
1834 && zr
->jpg_dma_head
!= zr
->jpg_que_head
) {
1836 frame
= zr
->jpg_pend
[zr
->jpg_dma_head
& BUZ_MASK_FRAME
];
1837 if (zr
->params
.TmpDcm
== 1) {
1838 /* fill 1 stat_com entry */
1839 i
= zr
->jpg_dma_head
& BUZ_MASK_STAT_COM
;
1840 zr
->stat_com
[i
] = zr
->jpg_gbuf
[frame
].frag_tab_bus
;
1842 /* fill 2 stat_com entries */
1843 i
= (zr
->jpg_dma_head
& 1) * 2;
1844 zr
->stat_com
[i
] = zr
->jpg_gbuf
[frame
].frag_tab_bus
;
1845 zr
->stat_com
[i
+ 1] = zr
->jpg_gbuf
[frame
].frag_tab_bus
;
1847 zr
->jpg_gbuf
[frame
].state
= BUZ_STATE_DMA
;
1853 /* when this is called the spinlock must be held */
1854 static void zoran_reap_stat_com(struct zoran
*zr
)
1856 /* move frames from DMA queue to done queue */
1863 struct zoran_gbuffer
*gbuf
;
1865 /* In motion decompress we don't have a hardware frame counter,
1866 we just count the interrupts here */
1868 if (zr
->codec_mode
== BUZ_MODE_MOTION_DECOMPRESS
)
1871 while (zr
->jpg_dma_tail
!= zr
->jpg_dma_head
) {
1872 if (zr
->params
.TmpDcm
== 1)
1873 i
= zr
->jpg_dma_tail
& BUZ_MASK_STAT_COM
;
1875 i
= (zr
->jpg_dma_tail
& 1) * 2 + 1;
1877 stat_com
= zr
->stat_com
[i
];
1879 if ((stat_com
& 1) == 0) {
1882 frame
= zr
->jpg_pend
[zr
->jpg_dma_tail
& BUZ_MASK_FRAME
];
1883 gbuf
= &zr
->jpg_gbuf
[frame
];
1884 get_fast_time(&gbuf
->bs
.timestamp
);
1886 if (zr
->codec_mode
== BUZ_MODE_MOTION_COMPRESS
) {
1887 gbuf
->bs
.length
= (stat_com
& 0x7fffff) >> 1;
1889 /* update sequence number with the help of the counter in stat_com */
1891 seq
= stat_com
>> 24;
1892 dif
= (seq
- zr
->jpg_seq_num
) & 0xff;
1893 zr
->jpg_seq_num
+= dif
;
1895 gbuf
->bs
.length
= 0;
1897 gbuf
->bs
.seq
= zr
->params
.TmpDcm
== 2 ? (zr
->jpg_seq_num
>> 1) : zr
->jpg_seq_num
;
1898 gbuf
->state
= BUZ_STATE_DONE
;
1904 static void zoran_irq(int irq
, void *dev_id
, struct pt_regs
*regs
)
1909 unsigned long flags
;
1911 zr
= (struct zoran
*) dev_id
;
1914 spin_lock_irqsave(&zr
->lock
, flags
);
1916 /* get/clear interrupt status bits */
1917 stat
= btread(ZR36057_ISR
);
1918 astat
= stat
& IRQ_MASK
;
1922 btwrite(astat
, ZR36057_ISR
);
1923 IDEBUG(printk(BUZ_DEBUG
"-%u: astat %08x stat %08x\n", zr
->id
, astat
, stat
));
1925 #if (IRQ_MASK & ZR36057_ISR_GIRQ0)
1926 if (astat
& ZR36057_ISR_GIRQ0
) {
1928 /* Interrupts may still happen when zr->v4l_memgrab_active is switched off.
1929 We simply ignore them */
1931 if (zr
->v4l_memgrab_active
) {
1933 /* A lot more checks should be here ... */
1934 if ((btread(ZR36057_VSSFGR
) & ZR36057_VSSFGR_SnapShot
) == 0)
1935 printk(KERN_WARNING
"%s: BuzIRQ with SnapShot off ???\n", zr
->name
);
1937 if (zr
->v4l_grab_frame
!= NO_GRAB_ACTIVE
) {
1938 /* There is a grab on a frame going on, check if it has finished */
1940 if ((btread(ZR36057_VSSFGR
) & ZR36057_VSSFGR_FrameGrab
) == 0) {
1941 /* it is finished, notify the user */
1943 zr
->v4l_gbuf
[zr
->v4l_grab_frame
].state
= BUZ_STATE_DONE
;
1944 zr
->v4l_grab_frame
= NO_GRAB_ACTIVE
;
1946 zr
->v4l_pend_tail
++;
1949 if (zr
->v4l_grab_frame
== NO_GRAB_ACTIVE
)
1950 wake_up_interruptible(&zr
->v4l_capq
);
1952 /* Check if there is another grab queued */
1954 if (zr
->v4l_grab_frame
== NO_GRAB_ACTIVE
&&
1955 zr
->v4l_pend_tail
!= zr
->v4l_pend_head
) {
1957 int frame
= zr
->v4l_pend
[zr
->v4l_pend_tail
& V4L_MASK_FRAME
];
1960 zr
->v4l_grab_frame
= frame
;
1962 /* Set zr36057 video front end and enable video */
1964 /* Buffer address */
1966 reg
= zr
->v4l_gbuf
[frame
].fbuffer_bus
;
1967 btwrite(reg
, ZR36057_VDTR
);
1968 if (zr
->video_interlace
)
1970 btwrite(reg
, ZR36057_VDBR
);
1972 /* video stride, status, and frame grab register */
1975 reg
= (zr
->gwidth
> 720) ? ((zr
->gwidth
& ~3) - 720) * zr
->gbpl
/ zr
->gwidth
: 0;
1979 if (zr
->video_interlace
)
1981 reg
= (reg
<< ZR36057_VSSFGR_DispStride
);
1982 reg
|= ZR36057_VSSFGR_VidOvf
;
1983 reg
|= ZR36057_VSSFGR_SnapShot
;
1984 reg
|= ZR36057_VSSFGR_FrameGrab
;
1985 btwrite(reg
, ZR36057_VSSFGR
);
1987 btor(ZR36057_VDCR_VidEn
, ZR36057_VDCR
);
1991 #endif /* (IRQ_MASK & ZR36057_ISR_GIRQ0) */
1993 #if (IRQ_MASK & ZR36057_ISR_GIRQ1)
1994 if (astat
& ZR36057_ISR_GIRQ1
) {
1995 unsigned csr
= zr36060_read_8(zr
, 0x001);
1996 unsigned isr
= zr36060_read_8(zr
, 0x008);
1998 IDEBUG(printk(KERN_DEBUG
"%s: ZR36057_ISR_GIRQ1 60_code=%02x 60_intr=%02x\n",
1999 zr
->name
, csr
, isr
));
2001 btand(~ZR36057_ICR_GIRQ1
, ZR36057_ICR
);
2002 zoran_reap_stat_com(zr
);
2003 zoran_feed_stat_com(zr
);
2005 #endif /* (IRQ_MASK & ZR36057_ISR_GIRQ1) */
2007 #if (IRQ_MASK & ZR36057_ISR_CodRepIRQ)
2008 if (astat
& ZR36057_ISR_CodRepIRQ
) {
2009 IDEBUG(printk(KERN_DEBUG
"%s: ZR36057_ISR_CodRepIRQ\n", zr
->name
));
2010 btand(~ZR36057_ICR_CodRepIRQ
, ZR36057_ICR
);
2012 #endif /* (IRQ_MASK & ZR36057_ISR_CodRepIRQ) */
2014 #if (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ)
2015 if ((astat
& ZR36057_ISR_JPEGRepIRQ
) &&
2016 (zr
->codec_mode
== BUZ_MODE_MOTION_DECOMPRESS
||
2017 zr
->codec_mode
== BUZ_MODE_MOTION_COMPRESS
)) {
2018 zoran_reap_stat_com(zr
);
2019 zoran_feed_stat_com(zr
);
2020 wake_up_interruptible(&zr
->jpg_capq
);
2022 #endif /* (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ) */
2026 printk(KERN_WARNING
"%s: irq loop %d\n", zr
->name
, count
);
2028 btwrite(0, ZR36057_ICR
);
2029 printk(KERN_ERR
"%s: IRQ lockup, cleared int mask\n", zr
->name
);
2034 spin_unlock_irqrestore(&zr
->lock
, flags
);
2037 /* Check a zoran_params struct for correctness, insert default params */
2039 static int zoran_check_params(struct zoran
*zr
, struct zoran_params
*params
)
2041 int err
= 0, err0
= 0;
2043 /* insert constant params */
2045 params
->major_version
= MAJOR_VERSION
;
2046 params
->minor_version
= MINOR_VERSION
;
2048 /* Check input and norm */
2050 if (params
->input
!= 0 && params
->input
!= 1) {
2053 if (params
->norm
!= VIDEO_MODE_PAL
&& params
->norm
!= VIDEO_MODE_NTSC
) {
2056 /* Check decimation, set default values for decimation = 1, 2, 4 */
2058 switch (params
->decimation
) {
2064 params
->field_per_buff
= 2;
2068 params
->img_width
= 720;
2069 params
->img_height
= tvnorms
[params
->norm
].Ha
/ 2;
2077 params
->field_per_buff
= 1;
2081 params
->img_width
= 704;
2082 params
->img_height
= tvnorms
[params
->norm
].Ha
/ 2;
2090 params
->field_per_buff
= 1;
2094 params
->img_width
= 704;
2095 params
->img_height
= tvnorms
[params
->norm
].Ha
/ 2;
2100 /* We have to check the data the user has set */
2102 if (params
->HorDcm
!= 1 && params
->HorDcm
!= 2 && params
->HorDcm
!= 4)
2104 if (params
->VerDcm
!= 1 && params
->VerDcm
!= 2)
2106 if (params
->TmpDcm
!= 1 && params
->TmpDcm
!= 2)
2108 if (params
->field_per_buff
!= 1 && params
->field_per_buff
!= 2)
2111 if (params
->img_x
< 0)
2113 if (params
->img_y
< 0)
2115 if (params
->img_width
< 0)
2117 if (params
->img_height
< 0)
2119 if (params
->img_x
+ params
->img_width
> 720)
2121 if (params
->img_y
+ params
->img_height
> tvnorms
[params
->norm
].Ha
/ 2)
2123 if (params
->img_width
% (16 * params
->HorDcm
) != 0)
2125 if (params
->img_height
% (8 * params
->VerDcm
) != 0)
2138 if (params
->quality
> 100)
2139 params
->quality
= 100;
2140 if (params
->quality
< 5)
2141 params
->quality
= 5;
2143 if (params
->APPn
< 0)
2145 if (params
->APPn
> 15)
2147 if (params
->APP_len
< 0)
2148 params
->APP_len
= 0;
2149 if (params
->APP_len
> 60)
2150 params
->APP_len
= 60;
2151 if (params
->COM_len
< 0)
2152 params
->COM_len
= 0;
2153 if (params
->COM_len
> 60)
2154 params
->COM_len
= 60;
2162 static void zoran_open_init_params(struct zoran
*zr
)
2166 /* Per default, map the V4L Buffers */
2168 zr
->map_mjpeg_buffers
= 0;
2170 /* User must explicitly set a window */
2176 zr
->window
.width
= 0;
2177 zr
->window
.height
= 0;
2178 zr
->window
.chromakey
= 0;
2179 zr
->window
.flags
= 0;
2180 zr
->window
.clips
= NULL
;
2181 zr
->window
.clipcount
= 0;
2183 zr
->video_interlace
= 0;
2185 zr
->v4l_memgrab_active
= 0;
2186 zr
->v4l_overlay_active
= 0;
2188 zr
->v4l_grab_frame
= NO_GRAB_ACTIVE
;
2189 zr
->v4l_grab_seq
= 0;
2196 /* DMA ring stuff for V4L */
2198 zr
->v4l_pend_tail
= 0;
2199 zr
->v4l_pend_head
= 0;
2200 for (i
= 0; i
< v4l_nbufs
; i
++) {
2201 zr
->v4l_gbuf
[i
].state
= BUZ_STATE_USER
; /* nothing going on */
2204 /* Set necessary params and call zoran_check_params to set the defaults */
2206 zr
->params
.decimation
= 1;
2208 zr
->params
.quality
= 50; /* default compression factor 8 */
2209 zr
->params
.odd_even
= 1;
2211 zr
->params
.APPn
= 0;
2212 zr
->params
.APP_len
= 0; /* No APPn marker */
2213 for (i
= 0; i
< 60; i
++)
2214 zr
->params
.APP_data
[i
] = 0;
2216 zr
->params
.COM_len
= 0; /* No COM marker */
2217 for (i
= 0; i
< 60; i
++)
2218 zr
->params
.COM_data
[i
] = 0;
2220 zr
->params
.VFIFO_FB
= 0;
2222 memset(zr
->params
.reserved
, 0, sizeof(zr
->params
.reserved
));
2224 zr
->params
.jpeg_markers
= JPEG_MARKER_DHT
| JPEG_MARKER_DQT
;
2226 i
= zoran_check_params(zr
, &zr
->params
);
2228 printk(KERN_ERR
"%s: zoran_open_init_params internal error\n", zr
->name
);
2232 * Open a buz card. Right now the flags stuff is just playing
2235 static int zoran_open(struct video_device
*dev
, int flags
)
2237 struct zoran
*zr
= (struct zoran
*) dev
;
2239 DEBUG(printk(KERN_INFO
": zoran_open\n"));
2248 if (v4l_fbuffer_alloc(zr
) < 0) {
2254 zoran_open_init_params(zr
);
2256 zr36057_enable_jpg(zr
, BUZ_MODE_IDLE
);
2258 btwrite(IRQ_MASK
, ZR36057_ISR
); // Clears interrupts
2260 btor(ZR36057_ICR_IntPinEn
, ZR36057_ICR
);
2272 static void zoran_close(struct video_device
*dev
)
2274 struct zoran
*zr
= (struct zoran
*) dev
;
2276 DEBUG(printk(KERN_INFO
": zoran_close\n"));
2278 /* disable interrupts */
2279 btand(~ZR36057_ICR_IntPinEn
, ZR36057_ICR
);
2281 /* wake up sleeping beauties */
2282 wake_up_interruptible(&zr
->v4l_capq
);
2283 wake_up_interruptible(&zr
->jpg_capq
);
2285 zr36057_enable_jpg(zr
, BUZ_MODE_IDLE
);
2286 zr36057_set_memgrab(zr
, 0);
2287 if (zr
->v4l_overlay_active
)
2288 zr36057_overlay(zr
, 0);
2292 v4l_fbuffer_free(zr
);
2293 jpg_fbuffer_free(zr
);
2297 DEBUG(printk(KERN_INFO
": zoran_close done\n"));
2301 static long zoran_read(struct video_device
*dev
, char *buf
, unsigned long count
, int nonblock
)
2306 static long zoran_write(struct video_device
*dev
, const char *buf
, unsigned long count
, int nonblock
)
2316 static int zoran_ioctl(struct video_device
*dev
, unsigned int cmd
, void *arg
)
2318 struct zoran
*zr
= (struct zoran
*) dev
;
2324 struct video_capability b
;
2325 IOCTL_DEBUG(printk("buz ioctl VIDIOCGCAP\n"));
2326 strncpy(b
.name
, zr
->video_dev
.name
, sizeof(b
.name
));
2327 b
.type
= VID_TYPE_CAPTURE
|
2332 /* theoretically we could also flag VID_TYPE_SUBCAPTURE
2333 but this is not even implemented in the BTTV driver */
2335 b
.channels
= 2; /* composite, svhs */
2337 b
.maxwidth
= BUZ_MAX_WIDTH
;
2338 b
.maxheight
= BUZ_MAX_HEIGHT
;
2339 b
.minwidth
= BUZ_MIN_WIDTH
;
2340 b
.minheight
= BUZ_MIN_HEIGHT
;
2341 if (copy_to_user(arg
, &b
, sizeof(b
))) {
2349 struct video_channel v
;
2351 if (copy_from_user(&v
, arg
, sizeof(v
))) {
2354 IOCTL_DEBUG(printk("buz ioctl VIDIOCGCHAN for channel %d\n", v
.channel
));
2355 switch (v
.channel
) {
2357 strcpy(v
.name
, "Composite");
2360 strcpy(v
.name
, "SVHS");
2367 v
.type
= VIDEO_TYPE_CAMERA
;
2368 v
.norm
= zr
->params
.norm
;
2369 if (copy_to_user(arg
, &v
, sizeof(v
))) {
2375 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2377 * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2379 * The famos BTTV driver has it implemented with a struct video_channel argument
2380 * and we follow it for compatibility reasons
2382 * BTW: this is the only way the user can set the norm!
2387 struct video_channel v
;
2391 if (copy_from_user(&v
, arg
, sizeof(v
))) {
2394 IOCTL_DEBUG(printk("buz ioctl VIDIOCSCHAN: channel=%d, norm=%d\n", v
.channel
, v
.norm
));
2395 switch (v
.channel
) {
2406 if (v
.norm
!= VIDEO_MODE_PAL
2407 && v
.norm
!= VIDEO_MODE_NTSC
) {
2410 zr
->params
.norm
= v
.norm
;
2411 zr
->params
.input
= v
.channel
;
2413 /* We switch overlay off and on since a change in the norm
2414 needs different VFE settings */
2416 on
= zr
->v4l_overlay_active
&& !zr
->v4l_memgrab_active
;
2418 zr36057_overlay(zr
, 0);
2420 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEODECODER
, DECODER_SET_INPUT
, &input
);
2421 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEODECODER
, DECODER_SET_NORM
, &zr
->params
.norm
);
2422 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEOENCODER
, ENCODER_SET_NORM
, &zr
->params
.norm
);
2425 zr36057_overlay(zr
, 1);
2427 /* Make sure the changes come into effect */
2428 res
= wait_grab_pending(zr
);
2441 struct video_picture p
= zr
->picture
;
2443 IOCTL_DEBUG(printk("buz ioctl VIDIOCGPICT\n"));
2444 p
.depth
= zr
->buffer
.depth
;
2445 switch (zr
->buffer
.depth
) {
2447 p
.palette
= VIDEO_PALETTE_RGB555
;
2451 p
.palette
= VIDEO_PALETTE_RGB565
;
2455 p
.palette
= VIDEO_PALETTE_RGB24
;
2459 p
.palette
= VIDEO_PALETTE_RGB32
;
2463 if (copy_to_user(arg
, &p
, sizeof(p
))) {
2471 struct video_picture p
;
2473 if (copy_from_user(&p
, arg
, sizeof(p
))) {
2476 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEODECODER
, DECODER_SET_PICTURE
, &p
);
2477 IOCTL_DEBUG(printk("buz ioctl VIDIOCSPICT bri=%d hue=%d col=%d con=%d dep=%d pal=%d\n",
2478 p
.brightness
, p
.hue
, p
.colour
, p
.contrast
, p
.depth
, p
.palette
));
2479 /* The depth and palette values have no meaning to us,
2480 should we return -EINVAL if they don't fit ? */
2489 if (copy_from_user(&v
, arg
, sizeof(v
))) {
2492 IOCTL_DEBUG(printk("buz ioctl VIDIOCCAPTURE: %d\n", v
));
2493 /* If there is nothing to do, return immediatly */
2495 if ((v
&& zr
->v4l_overlay_active
) || (!v
&& !zr
->v4l_overlay_active
))
2499 zr
->v4l_overlay_active
= 0;
2500 if (!zr
->v4l_memgrab_active
)
2501 zr36057_overlay(zr
, 0);
2502 /* When a grab is running, the video simply won't be switched on any more */
2504 if (!zr
->buffer_set
|| !zr
->window_set
) {
2507 zr
->v4l_overlay_active
= 1;
2508 if (!zr
->v4l_memgrab_active
)
2509 zr36057_overlay(zr
, 1);
2510 /* When a grab is running, the video will be switched on when grab is finished */
2512 /* Make sure the changes come into effect */
2513 res
= wait_grab_pending(zr
);
2521 IOCTL_DEBUG(printk("buz ioctl VIDIOCGWIN\n"));
2522 if (copy_to_user(arg
, &zr
->window
, sizeof(zr
->window
))) {
2530 struct video_clip
*vcp
;
2531 struct video_window vw
;
2534 if (copy_from_user(&vw
, arg
, sizeof(vw
))) {
2537 IOCTL_DEBUG(printk("buz ioctl VIDIOCSWIN: x=%d y=%d w=%d h=%d clipcount=%d\n", vw
.x
, vw
.y
, vw
.width
, vw
.height
, vw
.clipcount
));
2538 if (!zr
->buffer_set
) {
2542 * The video front end needs 4-byte alinged line sizes, we correct that
2543 * silently here if necessary
2546 if (zr
->buffer
.depth
== 15 || zr
->buffer
.depth
== 16) {
2547 end
= (vw
.x
+ vw
.width
) & ~1; /* round down */
2548 vw
.x
= (vw
.x
+ 1) & ~1; /* round up */
2549 vw
.width
= end
- vw
.x
;
2551 if (zr
->buffer
.depth
== 24) {
2552 end
= (vw
.x
+ vw
.width
) & ~3; /* round down */
2553 vw
.x
= (vw
.x
+ 3) & ~3; /* round up */
2554 vw
.width
= end
- vw
.x
;
2557 // At least xawtv seems to care about the following - just leave it away
2559 * Also corrected silently (as long as window fits at all):
2560 * video not fitting the screen
2563 if (vw
.x
< 0 || vw
.y
< 0 || vw
.x
+ vw
.width
> zr
->buffer
.width
||
2564 vw
.y
+ vw
.height
> zr
->buffer
.height
) {
2565 printk(BUZ_ERR
": VIDIOCSWIN: window does not fit frame buffer: %dx%d+%d*%d\n",
2566 vw
.width
, vw
.height
, vw
.x
, vw
.y
);
2574 if (vw
.x
+ vw
.width
> zr
->buffer
.width
)
2575 vw
.width
= zr
->buffer
.width
- vw
.x
;
2576 if (vw
.y
+ vw
.height
> zr
->buffer
.height
)
2577 vw
.height
= zr
->buffer
.height
- vw
.y
;
2581 /* Check for vaild parameters */
2582 if (vw
.width
< BUZ_MIN_WIDTH
|| vw
.height
< BUZ_MIN_HEIGHT
||
2583 vw
.width
> BUZ_MAX_WIDTH
|| vw
.height
> BUZ_MAX_HEIGHT
) {
2591 zr
->window
.x
= vw
.x
;
2592 zr
->window
.y
= vw
.y
;
2593 zr
->window
.width
= vw
.width
;
2594 zr
->window
.height
= vw
.height
;
2595 zr
->window
.chromakey
= 0;
2596 zr
->window
.flags
= 0; // RJ: Is this intended for interlace on/off ?
2598 zr
->window
.clips
= NULL
;
2599 zr
->window
.clipcount
= vw
.clipcount
;
2602 * If an overlay is running, we have to switch it off
2603 * and switch it on again in order to get the new settings in effect.
2605 * We also want to avoid that the overlay mask is written
2606 * when an overlay is running.
2609 on
= zr
->v4l_overlay_active
&& !zr
->v4l_memgrab_active
;
2611 zr36057_overlay(zr
, 0);
2614 * Write the overlay mask if clips are wanted.
2617 vcp
= vmalloc(sizeof(struct video_clip
) * (vw
.clipcount
+ 4));
2621 if (copy_from_user(vcp
, vw
.clips
, sizeof(struct video_clip
) * vw
.clipcount
)) {
2625 write_overlay_mask(zr
, vcp
, vw
.clipcount
);
2629 zr36057_overlay(zr
, 1);
2632 /* Make sure the changes come into effect */
2633 res
= wait_grab_pending(zr
);
2642 IOCTL_DEBUG(printk("buz ioctl VIDIOCGFBUF\n"));
2643 if (copy_to_user(arg
, &zr
->buffer
, sizeof(zr
->buffer
))) {
2651 struct video_buffer v
;
2653 if (!capable(CAP_SYS_ADMIN
)
2654 || !capable(CAP_SYS_RAWIO
))
2657 if (copy_from_user(&v
, arg
, sizeof(v
)))
2660 IOCTL_DEBUG(printk("buz ioctl VIDIOCSFBUF: base=0x%x w=%d h=%d depth=%d bpl=%d\n", (u32
) v
.base
, v
.width
, v
.height
, v
.depth
, v
.bytesperline
));
2661 if (zr
->v4l_overlay_active
) {
2662 /* Has the user gotten crazy ... ? */
2671 if (v
.height
<= 0 || v
.width
<= 0 || v
.bytesperline
<= 0) {
2674 if (v
.bytesperline
& 3) {
2678 zr
->buffer
.base
= (void *) ((unsigned long) v
.base
& ~3);
2680 zr
->buffer
.height
= v
.height
;
2681 zr
->buffer
.width
= v
.width
;
2682 zr
->buffer
.depth
= v
.depth
;
2683 zr
->buffer
.bytesperline
= v
.bytesperline
;
2685 if (zr
->buffer
.base
)
2687 zr
->window_set
= 0; /* The user should set new window parameters */
2691 /* RJ: what is VIDIOCKEY intended to do ??? */
2703 if (copy_from_user(&v
, arg
, sizeof(v
))) {
2706 IOCTL_DEBUG(printk("buz ioctl VIDIOCSYNC %d\n", v
));
2707 return v4l_sync(zr
, v
);
2710 case VIDIOCMCAPTURE
:
2712 struct video_mmap vm
;
2714 if (copy_from_user((void *) &vm
, (void *) arg
, sizeof(vm
))) {
2717 IOCTL_DEBUG(printk("buz ioctl VIDIOCMCAPTURE frame=%d geom=%dx%d fmt=%d\n",
2718 vm
.frame
, vm
.height
, vm
.width
, vm
.format
));
2719 return v4l_grab(zr
, &vm
);
2724 struct video_mbuf vm
;
2727 IOCTL_DEBUG(printk("buz ioctl VIDIOCGMBUF\n"));
2729 vm
.size
= v4l_nbufs
* v4l_bufsize
;
2730 vm
.frames
= v4l_nbufs
;
2731 for (i
= 0; i
< v4l_nbufs
; i
++) {
2732 vm
.offsets
[i
] = i
* v4l_bufsize
;
2735 /* The next mmap will map the V4L buffers */
2736 zr
->map_mjpeg_buffers
= 0;
2738 if (copy_to_user(arg
, &vm
, sizeof(vm
))) {
2746 struct video_unit vu
;
2748 IOCTL_DEBUG(printk("buz ioctl VIDIOCGUNIT\n"));
2749 vu
.video
= zr
->video_dev
.minor
;
2750 vu
.vbi
= VIDEO_NO_UNIT
;
2751 vu
.radio
= VIDEO_NO_UNIT
;
2752 vu
.audio
= VIDEO_NO_UNIT
;
2753 vu
.teletext
= VIDEO_NO_UNIT
;
2754 if (copy_to_user(arg
, &vu
, sizeof(vu
)))
2760 * RJ: In principal we could support subcaptures for V4L grabbing.
2761 * Not even the famous BTTV driver has them, however.
2762 * If there should be a strong demand, one could consider
2763 * to implement them.
2765 case VIDIOCGCAPTURE
:
2766 case VIDIOCSCAPTURE
:
2769 case BUZIOC_G_PARAMS
:
2771 IOCTL_DEBUG(printk("buz ioctl BUZIOC_G_PARAMS\n"));
2772 if (copy_to_user(arg
, &(zr
->params
), sizeof(zr
->params
)))
2777 case BUZIOC_S_PARAMS
:
2779 struct zoran_params bp
;
2782 if (zr
->codec_mode
!= BUZ_MODE_IDLE
) {
2785 if (copy_from_user(&bp
, arg
, sizeof(bp
))) {
2788 IOCTL_DEBUG(printk("buz ioctl BUZIOC_S_PARAMS\n"));
2790 /* Check the params first before overwriting our internal values */
2792 if (zoran_check_params(zr
, &bp
))
2797 /* Make changes of input and norm go into effect immediatly */
2799 /* We switch overlay off and on since a change in the norm
2800 needs different VFE settings */
2802 on
= zr
->v4l_overlay_active
&& !zr
->v4l_memgrab_active
;
2804 zr36057_overlay(zr
, 0);
2806 input
= zr
->params
.input
== 0 ? 3 : 7;
2807 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEODECODER
, DECODER_SET_INPUT
, &input
);
2808 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEODECODER
, DECODER_SET_NORM
, &zr
->params
.norm
);
2809 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEOENCODER
, ENCODER_SET_NORM
, &zr
->params
.norm
);
2812 zr36057_overlay(zr
, 1);
2814 if (copy_to_user(arg
, &bp
, sizeof(bp
))) {
2820 case BUZIOC_REQBUFS
:
2822 struct zoran_requestbuffers br
;
2824 if (zr
->jpg_buffers_allocated
) {
2827 if (copy_from_user(&br
, arg
, sizeof(br
))) {
2830 IOCTL_DEBUG(printk("buz ioctl BUZIOC_REQBUFS count = %lu size=%lu\n",
2831 br
.count
, br
.size
));
2832 /* Enforce reasonable lower and upper limits */
2834 br
.count
= 4; /* Could be choosen smaller */
2835 if (br
.count
> BUZ_MAX_FRAME
)
2836 br
.count
= BUZ_MAX_FRAME
;
2837 br
.size
= PAGE_ALIGN(br
.size
);
2839 br
.size
= 8192; /* Arbitrary */
2840 /* br.size is limited by 1 page for the stat_com tables to a Maximum of 2 MB */
2841 if (br
.size
> (512 * 1024))
2842 br
.size
= (512 * 1024); /* 512 K should be enough */
2843 if (zr
->need_contiguous
&& br
.size
> MAX_KMALLOC_MEM
)
2844 br
.size
= MAX_KMALLOC_MEM
;
2846 zr
->jpg_nbufs
= br
.count
;
2847 zr
->jpg_bufsize
= br
.size
;
2849 if (jpg_fbuffer_alloc(zr
))
2852 /* The next mmap will map the MJPEG buffers */
2853 zr
->map_mjpeg_buffers
= 1;
2855 if (copy_to_user(arg
, &br
, sizeof(br
))) {
2861 case BUZIOC_QBUF_CAPT
:
2865 if (copy_from_user((void *) &nb
, (void *) arg
, sizeof(int))) {
2868 IOCTL_DEBUG(printk("buz ioctl BUZIOC_QBUF_CAPT %d\n", nb
));
2869 return jpg_qbuf(zr
, nb
, BUZ_MODE_MOTION_COMPRESS
);
2872 case BUZIOC_QBUF_PLAY
:
2876 if (copy_from_user((void *) &nb
, (void *) arg
, sizeof(int))) {
2879 IOCTL_DEBUG(printk("buz ioctl BUZIOC_QBUF_PLAY %d\n", nb
));
2880 return jpg_qbuf(zr
, nb
, BUZ_MODE_MOTION_DECOMPRESS
);
2885 struct zoran_sync bs
;
2888 IOCTL_DEBUG(printk("buz ioctl BUZIOC_SYNC\n"));
2889 res
= jpg_sync(zr
, &bs
);
2890 if (copy_to_user(arg
, &bs
, sizeof(bs
))) {
2896 case BUZIOC_G_STATUS
:
2898 struct zoran_status bs
;
2899 int norm
, input
, status
;
2901 if (zr
->codec_mode
!= BUZ_MODE_IDLE
) {
2904 if (copy_from_user(&bs
, arg
, sizeof(bs
))) {
2907 IOCTL_DEBUG(printk("buz ioctl BUZIOC_G_STATUS\n"));
2919 /* Set video norm to VIDEO_MODE_AUTO */
2921 norm
= VIDEO_MODE_AUTO
;
2922 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEODECODER
, DECODER_SET_INPUT
, &input
);
2923 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEODECODER
, DECODER_SET_NORM
, &norm
);
2925 /* sleep 1 second */
2927 schedule_timeout(HZ
);
2929 /* Get status of video decoder */
2931 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEODECODER
, DECODER_GET_STATUS
, &status
);
2932 bs
.signal
= (status
& DECODER_STATUS_GOOD
) ? 1 : 0;
2933 bs
.norm
= (status
& DECODER_STATUS_NTSC
) ? VIDEO_MODE_NTSC
: VIDEO_MODE_PAL
;
2934 bs
.color
= (status
& DECODER_STATUS_COLOR
) ? 1 : 0;
2936 /* restore previous input and norm */
2937 input
= zr
->params
.input
== 0 ? 3 : 7;
2938 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEODECODER
, DECODER_SET_INPUT
, &input
);
2939 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEODECODER
, DECODER_SET_NORM
, &zr
->params
.norm
);
2941 if (copy_to_user(arg
, &bs
, sizeof(bs
))) {
2948 return -ENOIOCTLCMD
;
2956 * This maps the buffers to user space.
2958 * Depending on the state of zr->map_mjpeg_buffers
2959 * the V4L or the MJPEG buffers are mapped
2963 static int zoran_mmap(struct video_device
*dev
, const char *adr
, unsigned long size
)
2965 struct zoran
*zr
= (struct zoran
*) dev
;
2966 unsigned long start
= (unsigned long) adr
;
2967 unsigned long page
, pos
, todo
, fraglen
;
2970 if (zr
->map_mjpeg_buffers
) {
2971 /* Map the MJPEG buffers */
2973 if (!zr
->jpg_buffers_allocated
) {
2976 if (size
> zr
->jpg_nbufs
* zr
->jpg_bufsize
) {
2980 for (i
= 0; i
< zr
->jpg_nbufs
; i
++) {
2981 for (j
= 0; j
< zr
->jpg_bufsize
/ PAGE_SIZE
; j
++) {
2982 fraglen
= (zr
->jpg_gbuf
[i
].frag_tab
[2 * j
+ 1] & ~1) << 1;
2986 pos
= (unsigned long) zr
->jpg_gbuf
[i
].frag_tab
[2 * j
];
2987 page
= virt_to_phys(bus_to_virt(pos
)); /* should just be pos on i386 */
2988 if (remap_page_range(start
, page
, todo
, PAGE_SHARED
)) {
2989 printk(KERN_ERR
"%s: zoran_mmap(V4L): remap_page_range failed\n", zr
->name
);
2996 if (zr
->jpg_gbuf
[i
].frag_tab
[2 * j
+ 1] & 1)
2997 break; /* was last fragment */
3003 /* Map the V4L buffers */
3005 if (size
> v4l_nbufs
* v4l_bufsize
) {
3009 for (i
= 0; i
< v4l_nbufs
; i
++) {
3011 if (todo
> v4l_bufsize
)
3013 page
= zr
->v4l_gbuf
[i
].fbuffer_phys
;
3014 DEBUG(printk("V4L remap page range %d 0x%x %d to 0x%x\n", i
, page
, todo
, start
));
3015 if (remap_page_range(start
, page
, todo
, PAGE_SHARED
)) {
3016 printk(KERN_ERR
"%s: zoran_mmap(V4L): remap_page_range failed\n", zr
->name
);
3028 static int zoran_init_done(struct video_device
*dev
)
3033 static struct video_device zoran_template
=
3036 VID_TYPE_CAPTURE
| VID_TYPE_OVERLAY
| VID_TYPE_CLIPPING
| VID_TYPE_FRAMERAM
|
3037 VID_TYPE_SCALES
| VID_TYPE_SUBCAPTURE
,
3038 VID_HARDWARE_BT848
, /* Not true, but the buz is not yet in the list */
3052 static int zr36057_init(int i
)
3054 struct zoran
*zr
= &zoran
[i
];
3056 unsigned mem_needed
;
3061 btwrite(0, ZR36057_SPGPPCR
);
3064 /* default setup of all parameters which will persist beetween opens */
3068 init_waitqueue_head(&zr
->v4l_capq
);
3069 init_waitqueue_head(&zr
->jpg_capq
);
3071 zr
->map_mjpeg_buffers
= 0; /* Map V4L buffers by default */
3074 zr
->jpg_bufsize
= 0;
3075 zr
->jpg_buffers_allocated
= 0;
3077 zr
->buffer_set
= 0; /* Flag if frame buffer has been set */
3078 zr
->buffer
.base
= (void *) vidmem
;
3079 zr
->buffer
.width
= 0;
3080 zr
->buffer
.height
= 0;
3081 zr
->buffer
.depth
= 0;
3082 zr
->buffer
.bytesperline
= 0;
3084 zr
->params
.norm
= default_norm
? 1 : 0; /* Avoid nonsense settings from user */
3085 zr
->params
.input
= default_input
? 1 : 0; /* Avoid nonsense settings from user */
3086 zr
->video_interlace
= 0;
3088 /* Should the following be reset at every open ? */
3090 zr
->picture
.colour
= 32768;
3091 zr
->picture
.brightness
= 32768;
3092 zr
->picture
.hue
= 32768;
3093 zr
->picture
.contrast
= 32768;
3094 zr
->picture
.whiteness
= 0;
3095 zr
->picture
.depth
= 0;
3096 zr
->picture
.palette
= 0;
3098 for (j
= 0; j
< VIDEO_MAX_FRAME
; j
++) {
3099 zr
->v4l_gbuf
[i
].fbuffer
= 0;
3100 zr
->v4l_gbuf
[i
].fbuffer_phys
= 0;
3101 zr
->v4l_gbuf
[i
].fbuffer_bus
= 0;
3106 /* default setup (will be repeated at every open) */
3108 zoran_open_init_params(zr
);
3110 /* allocate memory *before* doing anything to the hardware in case allocation fails */
3112 /* STAT_COM table and overlay mask */
3114 mem_needed
= (BUZ_NUM_STAT_COM
+ ((BUZ_MAX_WIDTH
+ 31) / 32) * BUZ_MAX_HEIGHT
) * 4;
3115 mem
= (unsigned long) kmalloc(mem_needed
, GFP_KERNEL
);
3119 memset((void *) mem
, 0, mem_needed
);
3121 zr
->stat_com
= (u32
*) mem
;
3122 for (j
= 0; j
< BUZ_NUM_STAT_COM
; j
++) {
3123 zr
->stat_com
[j
] = 1; /* mark as unavailable to zr36057 */
3125 zr
->overlay_mask
= (u32
*) (mem
+ BUZ_NUM_STAT_COM
* 4);
3127 /* Initialize zr->jpg_gbuf */
3129 for (j
= 0; j
< BUZ_MAX_FRAME
; j
++) {
3130 zr
->jpg_gbuf
[j
].frag_tab
= 0;
3131 zr
->jpg_gbuf
[j
].frag_tab_bus
= 0;
3132 zr
->jpg_gbuf
[j
].state
= BUZ_STATE_USER
;
3133 zr
->jpg_gbuf
[j
].bs
.frame
= j
;
3136 /* take zr36057 out of reset now */
3137 btwrite(ZR36057_SPGPPCR_SoftReset
, ZR36057_SPGPPCR
);
3140 /* stop all DMA processes */
3141 btwrite(ZR36057_MCTCR_CFlush
, ZR36057_MCTCR
);
3142 btand(~ZR36057_VDCR_VidEn
, ZR36057_VDCR
);
3143 /* assert P_Reset */
3144 btwrite(0, ZR36057_JPC
);
3150 /* set up GPIO direction */
3151 btwrite(ZR36057_SPGPPCR_SoftReset
| 0, ZR36057_SPGPPCR
);
3153 /* Set up guest bus timing - Guests 0..3 Tdur=12, Trec=3 */
3154 btwrite((GPIO_MASK
<< 24) | 0x8888, ZR36057_GPPGCR1
);
3157 /* reset video decoder */
3164 /* reset JPEG codec */
3165 zr36060_sleep(zr
, 0);
3169 zr36060_sleep(zr
, 1);
3172 /* display codec revision */
3173 if ((rev
=zr36060_read_8(zr
, 0x022)) == 0x33) {
3174 printk(KERN_INFO
"%s: Zoran ZR36060 (rev %d)\n",
3175 zr
->name
, zr36060_read_8(zr
, 0x023));
3177 printk(KERN_ERR
"%s: Zoran ZR36060 not found (Rev=%d)\n", zr
->name
, rev
);
3178 kfree((void *) zr
->stat_com
);
3184 // btwrite(btread(ZR36057_SPGPPCR)&~ZR36057_SPGPPCR_SoftReset , ZR36057_SPGPPCR);
3186 // btwrite(btread(ZR36057_SPGPPCR)|ZR36057_SPGPPCR_SoftReset , ZR36057_SPGPPCR);
3190 * Set up the GPIO direction
3192 btwrite(btread(ZR36057_SPGPPCR_SoftReset
)|0 , ZR36057_SPGPPCR
);
3193 /* Set up guest bus timing - Guests 0..2 Tdur=12, Trec=3 */
3194 btwrite(0xFF00F888, ZR36057_GPPGCR1
);
3196 GPIO(zr
, 5, 0); /* Analog video bypass */
3198 GPIO(zr
, 0, 0); /* Reset 819 */
3200 GPIO(zr
, 0, 1); /* 819 back */
3202 /* reset JPEG codec */
3203 zr36060_sleep(zr
, 0);
3207 zr36060_sleep(zr
, 1);
3210 /* display codec revision */
3211 if ((rev
=zr36060_read_8(zr
, 0x022)) == 0x33) {
3212 printk(KERN_INFO
"%s: Zoran ZR36060 (rev %d)\n",
3213 zr
->name
, zr36060_read_8(zr
, 0x023));
3215 printk(KERN_ERR
"%s: Zoran ZR36060 not found (rev=%d)\n", zr
->name
, rev
);
3216 // kfree((void *) zr->stat_com);
3222 memcpy(&zr
->i2c
, &zoran_i2c_bus_template
, sizeof(struct i2c_bus
));
3223 sprintf(zr
->i2c
.name
, "zoran%u", zr
->id
);
3225 if (i2c_register_bus(&zr
->i2c
) < 0) {
3226 kfree((void *) zr
->stat_com
);
3230 * Now add the template and register the device unit.
3232 memcpy(&zr
->video_dev
, &zoran_template
, sizeof(zoran_template
));
3233 sprintf(zr
->video_dev
.name
, "zoran%u", zr
->id
);
3234 if (video_register_device(&zr
->video_dev
, VFL_TYPE_GRABBER
) < 0) {
3235 i2c_unregister_bus(&zr
->i2c
);
3236 kfree((void *) zr
->stat_com
);
3239 /* toggle JPEG codec sleep to sync PLL */
3240 zr36060_sleep(zr
, 1);
3242 zr36060_sleep(zr
, 0);
3245 /* Enable bus-mastering */
3246 pci_set_master(zr
->pci_dev
);
3248 j
= zr
->params
.input
== 0 ? 3 : 7;
3249 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEODECODER
, DECODER_SET_INPUT
, &j
);
3250 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEODECODER
, DECODER_SET_NORM
, &zr
->params
.norm
);
3251 i2c_control_device(&zr
->i2c
, I2C_DRIVERID_VIDEOENCODER
, ENCODER_SET_NORM
, &zr
->params
.norm
);
3253 /* set individual interrupt enables (without GIRQ0)
3254 but don't global enable until zoran_open() */
3256 btwrite(IRQ_MASK
& ~ZR36057_ISR_GIRQ0
, ZR36057_ICR
);
3258 if(request_irq(zr
->pci_dev
->irq
, zoran_irq
,
3259 SA_SHIRQ
| SA_INTERRUPT
, zr
->name
, (void *) zr
)<0)
3261 printk(KERN_ERR
"%s: Can't assign irq.\n", zr
->name
);
3262 video_unregister_device(&zr
->video_dev
);
3263 i2c_unregister_bus(&zr
->i2c
);
3264 kfree((void *) zr
->stat_com
);
3267 zr
->initialized
= 1;
3273 static void release_zoran(void)
3279 for (i
= 0; i
< zoran_num
; i
++) {
3282 if (!zr
->initialized
)
3285 /* unregister i2c_bus */
3286 i2c_unregister_bus((&zr
->i2c
));
3288 /* disable PCI bus-mastering */
3289 pci_read_config_byte(zr
->pci_dev
, PCI_COMMAND
, &command
);
3290 command
&= ~PCI_COMMAND_MASTER
;
3291 pci_write_config_byte(zr
->pci_dev
, PCI_COMMAND
, command
);
3293 /* put chip into reset */
3294 btwrite(0, ZR36057_SPGPPCR
);
3296 free_irq(zr
->pci_dev
->irq
, zr
);
3298 /* unmap and free memory */
3300 kfree((void *) zr
->stat_com
);
3302 iounmap(zr
->zr36057_mem
);
3304 video_unregister_device(&zr
->video_dev
);
3309 * Scan for a Buz card (actually for the PCI controller ZR36057),
3310 * request the irq and map the io memory
3313 static int find_zr36057(void)
3315 unsigned char latency
;
3317 struct pci_dev
*dev
= NULL
;
3321 while (zoran_num
< BUZ_MAX
3322 && (dev
= pci_find_device(PCI_VENDOR_ID_ZORAN
, PCI_DEVICE_ID_ZORAN_36057
, dev
)) != NULL
) {
3323 zr
= &zoran
[zoran_num
];
3325 zr
->zr36057_mem
= NULL
;
3327 sprintf(zr
->name
, "zoran%u", zr
->id
);
3329 spin_lock_init(&zr
->lock
);
3331 zr
->zr36057_adr
= zr
->pci_dev
->resource
[0].start
;
3332 pci_read_config_byte(zr
->pci_dev
, PCI_CLASS_REVISION
, &zr
->revision
);
3333 if (zr
->revision
< 2) {
3334 printk(KERN_INFO
"%s: Zoran ZR36057 (rev %d) irq: %d, memory: 0x%08x.\n",
3335 zr
->name
, zr
->revision
, zr
->pci_dev
->irq
, zr
->zr36057_adr
);
3337 unsigned short ss_vendor_id
, ss_id
;
3339 pci_read_config_word(zr
->pci_dev
, PCI_SUBSYSTEM_VENDOR_ID
, &ss_vendor_id
);
3340 pci_read_config_word(zr
->pci_dev
, PCI_SUBSYSTEM_ID
, &ss_id
);
3341 printk(KERN_INFO
"%s: Zoran ZR36067 (rev %d) irq: %d, memory: 0x%08x\n",
3342 zr
->name
, zr
->revision
, zr
->pci_dev
->irq
, zr
->zr36057_adr
);
3343 printk(KERN_INFO
"%s: subsystem vendor=0x%04x id=0x%04x\n",
3344 zr
->name
, ss_vendor_id
, ss_id
);
3345 if(ss_vendor_id
==0xFF10 && ss_id
== 0xDE41)
3347 zr
->board
= BOARD_LML33
;
3348 printk(KERN_INFO
"%s: LML33 detected.\n", zr
->name
);
3352 zr
->zr36057_mem
= ioremap(zr
->zr36057_adr
, 0x1000);
3354 /* set PCI latency timer */
3355 pci_read_config_byte(zr
->pci_dev
, PCI_LATENCY_TIMER
, &latency
);
3356 if (latency
!= 48) {
3357 printk(KERN_INFO
"%s: Changing PCI latency from %d to 48.\n", zr
->name
, latency
);
3359 pci_write_config_byte(zr
->pci_dev
, PCI_LATENCY_TIMER
, latency
);
3364 printk(KERN_INFO
"zoran: no cards found.\n");
3369 #include "chipsets.h"
3371 static void handle_chipset(void)
3374 struct pci_dev
*dev
= NULL
;
3376 for (index
= 0; index
< sizeof(black
) / sizeof(black
[0]); index
++) {
3377 if ((dev
= pci_find_device(black
[index
].vendor
, black
[index
].device
, dev
)) != NULL
) {
3378 printk(KERN_INFO
": Host bridge: %s, ", black
[index
].name
);
3379 switch (black
[index
].action
) {
3382 printk("enabling Triton support.\n");
3387 printk("enabling Natoma workaround.\n");
3396 int init_module(void)
3398 int init_zoran_cards(struct video_init
*unused
)
3404 printk(KERN_INFO
"Zoran driver 1.00 (c) 1999 Rainer Johanni, Dave Perks.\n");
3406 /* Look for Buz cards */
3408 if (find_zr36057() <= 0) {
3411 printk(KERN_INFO
"zoran: %d zoran card(s) found\n", zoran_num
);
3417 /* check the parameters we have been given, adjust if necessary */
3421 if (v4l_nbufs
> VIDEO_MAX_FRAME
)
3422 v4l_nbufs
= VIDEO_MAX_FRAME
;
3423 /* The user specfies the in KB, we want them in byte (and page aligned) */
3424 v4l_bufsize
= PAGE_ALIGN(v4l_bufsize
* 1024);
3425 if (v4l_bufsize
< 32768)
3426 v4l_bufsize
= 32768;
3427 /* 2 MB is arbitrary but sufficient for the maximum possible images */
3428 if (v4l_bufsize
> 2048 * 1024)
3429 v4l_bufsize
= 2048 * 1024;
3431 printk(KERN_INFO
"zoran: using %d V4L buffers of size %d KB\n", v4l_nbufs
, v4l_bufsize
>> 10);
3433 /* Use parameter for vidmem or try to find a video card */
3436 printk(KERN_INFO
"zoran: Using supplied video memory base address @ 0x%lx\n", vidmem
);
3439 /* check if we have a Triton or Natome chipset */
3443 /* take care of Natoma chipset and a revision 1 zr36057 */
3445 for (i
= 0; i
< zoran_num
; i
++) {
3446 if (natoma
&& zoran
[i
].revision
<= 1) {
3447 zoran
[i
].need_contiguous
= 1;
3448 printk(KERN_INFO
"%s: ZR36057/Natome bug, max. buffer size is 128K\n", zoran
[i
].name
);
3450 zoran
[i
].need_contiguous
= 0;
3454 /* initialize the Buzs */
3456 /* We have to know which ones must be released if an error occurs */
3457 for (i
= 0; i
< zoran_num
; i
++)
3458 zoran
[i
].initialized
= 0;
3460 for (i
= 0; i
< zoran_num
; i
++) {
3461 if (zr36057_init(i
) < 0) {
3474 void cleanup_module(void)