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>
51 #include <linux/vmalloc.h>
53 #include <linux/videodev.h>
55 #include <linux/version.h>
56 #include <asm/uaccess.h>
58 #include <linux/i2c-old.h>
60 #include <linux/video_decoder.h>
61 #include <linux/video_encoder.h>
63 #define IRQ_MASK ( ZR36057_ISR_GIRQ0 | /* ZR36057_ISR_GIRQ1 | ZR36057_ISR_CodRepIRQ | */ ZR36057_ISR_JPEGRepIRQ )
64 #define GPIO_MASK 0xdf
70 GPIO0 = 1, take board out of reset
71 GPIO1 = 1, take JPEG codec out of sleep mode
72 GPIO3 = 1, deassert FRAME# to 36060
75 GIRQ0 signals a vertical sync of the video signal
76 GIRQ1 signals that ZR36060's DATERR# line is asserted.
80 In their infinite wisdom, the Iomega engineers decided to
81 use the same input line for composite and S-Video Color,
82 although there are two entries not connected at all!
83 Through this ingenious strike, it is not possible to
84 keep two running video sources connected at the same time
85 to Composite and S-VHS input!
89 mode 2 - noise or something I don't know
90 mode 3 - Composite and S-Video C
92 mode 5 - S-Video (gain C independently selectable of gain Y)
94 mode 7 - S-Video (gain C adapted to gain Y)
97 #define MAJOR_VERSION 1 /* driver major version */
98 #define MINOR_VERSION 0 /* driver minor version */
100 #define BUZ_NAME "Iomega BUZ V-1.0" /* name of the driver */
102 #define DEBUG(x) /* Debug driver */
103 #define IDEBUG(x) /* Debug interrupt handler */
104 #define IOCTL_DEBUG(x)
107 /* The parameters for this driver */
110 The video mem address of the video card.
111 The driver has a little database for some videocards
112 to determine it from there. If your video card is not in there
113 you have either to give it to the driver as a parameter
114 or set in in a VIDIOCSFBUF ioctl
117 static unsigned long vidmem = 0; /* Video memory base address */
119 /* Special purposes only: */
121 static int triton = 0; /* 0=no, 1=yes */
122 static int natoma = 0; /* 0=no, 1=yes */
125 Number and size of grab buffers for Video 4 Linux
126 The vast majority of applications should not need more than 2,
127 the very popular BTTV driver actually does ONLY have 2.
128 Time sensitive applications might need more, the maximum
129 is VIDEO_MAX_FRAME (defined in <linux/videodev.h>).
131 The size is set so that the maximum possible request
132 can be satisfied. Decrease it, if bigphys_area alloc'd
133 memory is low. If you don't have the bigphys_area patch,
134 set it to 128 KB. Will you allow only to grab small
135 images with V4L, but that's better than nothing.
137 v4l_bufsize has to be given in KB !
141 static int v4l_nbufs = 2;
142 static int v4l_bufsize = 128; /* Everybody should be able to work with this setting */
145 Default input and video norm at startup of the driver.
148 static int default_input = 0; /* 0=Composite, 1=S-VHS */
149 static int default_norm = 0; /* 0=PAL, 1=NTSC */
151 MODULE_PARM(vidmem, "i");
152 MODULE_PARM(triton, "i");
153 MODULE_PARM(natoma, "i");
154 MODULE_PARM(v4l_nbufs, "i");
155 MODULE_PARM(v4l_bufsize, "i");
156 MODULE_PARM(default_input, "i");
157 MODULE_PARM(default_norm, "i");
159 /* Anybody who uses more than four? */
162 static int zoran_num; /* number of Buzs in use */
163 static struct zoran zoran[BUZ_MAX];
165 /* forward references */
167 static void v4l_fbuffer_free(struct zoran *zr);
168 static void jpg_fbuffer_free(struct zoran *zr);
169 static void zoran_feed_stat_com(struct zoran *zr);
174 * Allocate the V4L grab buffers
176 * These have to be pysically contiguous.
177 * If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
180 static int v4l_fbuffer_alloc(struct zoran *zr)
185 for (i = 0; i < v4l_nbufs; i++) {
186 if (zr->v4l_gbuf[i].fbuffer)
187 printk(KERN_WARNING "%s: v4l_fbuffer_alloc: buffer %d allready allocated ?\n", zr->name, i);
189 if (v4l_bufsize <= MAX_KMALLOC_MEM) {
192 mem = (unsigned char *) kmalloc(v4l_bufsize, GFP_KERNEL);
194 printk(KERN_ERR "%s: kmalloc for V4L bufs failed\n", zr->name);
195 v4l_fbuffer_free(zr);
198 zr->v4l_gbuf[i].fbuffer = mem;
199 zr->v4l_gbuf[i].fbuffer_phys = virt_to_phys(mem);
200 zr->v4l_gbuf[i].fbuffer_bus = virt_to_bus(mem);
201 for (off = 0; off < v4l_bufsize; off += PAGE_SIZE)
202 mem_map_reserve(virt_to_page(mem + off));
203 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)));
205 v4l_fbuffer_free(zr);
213 /* free the V4L grab buffers */
214 static void v4l_fbuffer_free(struct zoran *zr)
219 for (i = 0; i < v4l_nbufs; i++) {
220 if (!zr->v4l_gbuf[i].fbuffer)
223 mem = zr->v4l_gbuf[i].fbuffer;
224 for (off = 0; off < v4l_bufsize; off += PAGE_SIZE)
225 mem_map_unreserve(virt_to_page(mem + off));
226 kfree((void *) zr->v4l_gbuf[i].fbuffer);
227 zr->v4l_gbuf[i].fbuffer = NULL;
232 * Allocate the MJPEG grab buffers.
234 * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
235 * kmalloc is used to request a physically contiguous area,
236 * else we allocate the memory in framgents with get_free_page.
238 * If a Natoma chipset is present and this is a revision 1 zr36057,
239 * each MJPEG buffer needs to be physically contiguous.
240 * (RJ: This statement is from Dave Perks' original driver,
241 * I could never check it because I have a zr36067)
242 * The driver cares about this because it reduces the buffer
243 * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
245 * RJ: The contents grab buffers needs never be accessed in the driver.
246 * Therefore there is no need to allocate them with vmalloc in order
247 * to get a contiguous virtual memory space.
248 * I don't understand why many other drivers first allocate them with
249 * vmalloc (which uses internally also get_free_page, but delivers you
250 * virtual addresses) and then again have to make a lot of efforts
251 * to get the physical address.
255 static int jpg_fbuffer_alloc(struct zoran *zr)
257 int i, j, off, alloc_contig;
260 /* Decide if we should alloc contiguous or fragmented memory */
261 /* This has to be identical in jpg_fbuffer_alloc and jpg_fbuffer_free */
263 alloc_contig = (zr->jpg_bufsize < MAX_KMALLOC_MEM);
265 for (i = 0; i < zr->jpg_nbufs; i++) {
266 if (zr->jpg_gbuf[i].frag_tab)
267 printk(KERN_WARNING "%s: jpg_fbuffer_alloc: buffer %d allready allocated ???\n", zr->name, i);
269 /* Allocate fragment table for this buffer */
271 mem = get_free_page(GFP_KERNEL);
273 printk(KERN_ERR "%s: jpg_fbuffer_alloc: get_free_page (frag_tab) failed for buffer %d\n", zr->name, i);
274 jpg_fbuffer_free(zr);
277 memset((void *) mem, 0, PAGE_SIZE);
278 zr->jpg_gbuf[i].frag_tab = (u32 *) mem;
279 zr->jpg_gbuf[i].frag_tab_bus = virt_to_bus((void *) mem);
282 mem = (unsigned long) kmalloc(zr->jpg_bufsize, GFP_KERNEL);
284 jpg_fbuffer_free(zr);
287 zr->jpg_gbuf[i].frag_tab[0] = virt_to_bus((void *) mem);
288 zr->jpg_gbuf[i].frag_tab[1] = ((zr->jpg_bufsize / 4) << 1) | 1;
289 for (off = 0; off < zr->jpg_bufsize; off += PAGE_SIZE)
290 mem_map_reserve(virt_to_page(mem + off));
292 /* jpg_bufsize is alreay page aligned */
293 for (j = 0; j < zr->jpg_bufsize / PAGE_SIZE; j++) {
294 mem = get_free_page(GFP_KERNEL);
296 jpg_fbuffer_free(zr);
299 zr->jpg_gbuf[i].frag_tab[2 * j] = virt_to_bus((void *) mem);
300 zr->jpg_gbuf[i].frag_tab[2 * j + 1] = (PAGE_SIZE / 4) << 1;
301 mem_map_reserve(virt_to_page(mem));
304 zr->jpg_gbuf[i].frag_tab[2 * j - 1] |= 1;
308 DEBUG(printk("jpg_fbuffer_alloc: %d KB allocated\n",
309 (zr->jpg_nbufs * zr->jpg_bufsize) >> 10));
310 zr->jpg_buffers_allocated = 1;
314 /* free the MJPEG grab buffers */
315 static void jpg_fbuffer_free(struct zoran *zr)
317 int i, j, off, alloc_contig;
320 /* Decide if we should alloc contiguous or fragmented memory */
321 /* This has to be identical in jpg_fbuffer_alloc and jpg_fbuffer_free */
323 alloc_contig = (zr->jpg_bufsize < MAX_KMALLOC_MEM);
325 for (i = 0; i < zr->jpg_nbufs; i++) {
326 if (!zr->jpg_gbuf[i].frag_tab)
330 if (zr->jpg_gbuf[i].frag_tab[0]) {
331 mem = (unsigned char *) bus_to_virt(zr->jpg_gbuf[i].frag_tab[0]);
332 for (off = 0; off < zr->jpg_bufsize; off += PAGE_SIZE)
333 mem_map_unreserve(virt_to_page(mem + off));
335 zr->jpg_gbuf[i].frag_tab[0] = 0;
336 zr->jpg_gbuf[i].frag_tab[1] = 0;
339 for (j = 0; j < zr->jpg_bufsize / PAGE_SIZE; j++) {
340 if (!zr->jpg_gbuf[i].frag_tab[2 * j])
342 mem_map_unreserve(virt_to_page(bus_to_virt(zr->jpg_gbuf[i].frag_tab[2 * j])));
343 free_page((unsigned long) bus_to_virt(zr->jpg_gbuf[i].frag_tab[2 * j]));
344 zr->jpg_gbuf[i].frag_tab[2 * j] = 0;
345 zr->jpg_gbuf[i].frag_tab[2 * j + 1] = 0;
349 free_page((unsigned long) zr->jpg_gbuf[i].frag_tab);
350 zr->jpg_gbuf[i].frag_tab = NULL;
352 zr->jpg_buffers_allocated = 0;
356 /* ----------------------------------------------------------------------- */
363 /* software I2C functions */
365 static void i2c_setlines(struct i2c_bus *bus, int ctrl, int data)
367 struct zoran *zr = (struct zoran *) bus->data;
368 btwrite((data << 1) | ctrl, ZR36057_I2CBR);
369 btread(ZR36057_I2CBR);
373 static int i2c_getdataline(struct i2c_bus *bus)
375 struct zoran *zr = (struct zoran *) bus->data;
376 return (btread(ZR36057_I2CBR) >> 1) & 1;
379 static void attach_inform(struct i2c_bus *bus, int id)
381 DEBUG(struct zoran *zr = (struct zoran *) bus->data);
382 DEBUG(printk(BUZ_DEBUG "-%u: i2c attach %02x\n", zr->id, id));
385 static void detach_inform(struct i2c_bus *bus, int id)
387 DEBUG(struct zoran *zr = (struct zoran *) bus->data);
388 DEBUG(printk(BUZ_DEBUG "-%u: i2c detach %02x\n", zr->id, id));
391 static struct i2c_bus zoran_i2c_bus_template =
409 /* ----------------------------------------------------------------------- */
411 static void GPIO(struct zoran *zr, unsigned bit, unsigned value)
416 mask = 1 << (24 + bit);
417 reg = btread(ZR36057_GPPGCR1) & ~mask;
421 btwrite(reg, ZR36057_GPPGCR1);
422 /* Stop any PCI posting on the GPIO bus */
423 btread(ZR36057_I2CBR);
428 * Set the registers for the size we have specified. Don't bother
429 * trying to understand this without the ZR36057 manual in front of
432 * PS: The manual is free for download in .pdf format from
433 * www.zoran.com - nicely done those folks.
437 u16 Wt, Wa, Ht, Ha, HStart, VStart;
440 static struct tvnorm tvnorms[] =
443 {864, 720, 625, 576, 31, 16},
445 {858, 720, 525, 480, 21, 8},
447 #define TVNORMS (sizeof(tvnorms) / sizeof(tvnorm))
449 static int format2bpp(int format)
453 /* Determine the number of bytes per pixel for the video format requested */
457 case VIDEO_PALETTE_YUV422:
461 case VIDEO_PALETTE_RGB555:
465 case VIDEO_PALETTE_RGB565:
469 case VIDEO_PALETTE_RGB24:
473 case VIDEO_PALETTE_RGB32:
487 static void zr36057_set_vfe(struct zoran *zr, int video_width, int video_height,
488 unsigned int video_format)
491 unsigned HStart, HEnd, VStart, VEnd;
493 unsigned VidWinWid, VidWinHt;
494 unsigned hcrop1, hcrop2, vcrop1, vcrop2;
495 unsigned Wa, We, Ha, He;
496 unsigned X, Y, HorDcm, VerDcm;
498 unsigned mask_line_size;
500 if (zr->params.norm < 0 || zr->params.norm > 1) {
501 printk(KERN_ERR "%s: set_vfe: video_norm = %d not valid\n", zr->name, zr->params.norm);
504 if (video_width < BUZ_MIN_WIDTH || video_height < BUZ_MIN_HEIGHT) {
505 printk(KERN_ERR "%s: set_vfe: w=%d h=%d not valid\n", zr->name, video_width, video_height);
508 tvn = &tvnorms[zr->params.norm];
513 /* if window has more than half of active height,
514 switch on interlacing - we want the full information */
516 zr->video_interlace = (video_height > Ha / 2);
521 VidWinWid = video_width;
522 X = (VidWinWid * 64 + tvn->Wa - 1) / tvn->Wa;
523 We = (VidWinWid * 64) / X;
525 hcrop1 = 2 * ((tvn->Wa - We) / 4);
526 hcrop2 = tvn->Wa - We - hcrop1;
527 HStart = tvn->HStart | 1;
528 HEnd = HStart + tvn->Wa - 1;
531 reg = ((HStart & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HStart)
532 | ((HEnd & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HEnd);
533 reg |= ZR36057_VFEHCR_HSPol;
534 btwrite(reg, ZR36057_VFEHCR);
537 DispMode = !zr->video_interlace;
538 VidWinHt = DispMode ? video_height : video_height / 2;
539 Y = (VidWinHt * 64 * 2 + tvn->Ha - 1) / tvn->Ha;
540 He = (VidWinHt * 64) / Y;
542 vcrop1 = (tvn->Ha / 2 - He) / 2;
543 vcrop2 = tvn->Ha / 2 - He - vcrop1;
544 VStart = tvn->VStart;
545 VEnd = VStart + tvn->Ha / 2 - 1;
548 reg = ((VStart & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VStart)
549 | ((VEnd & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VEnd);
550 reg |= ZR36057_VFEVCR_VSPol;
551 btwrite(reg, ZR36057_VFEVCR);
553 /* scaler and pixel format */
554 reg = 0 // ZR36057_VFESPFR_ExtFl /* Trying to live without ExtFl */
555 | (HorDcm << ZR36057_VFESPFR_HorDcm)
556 | (VerDcm << ZR36057_VFESPFR_VerDcm)
557 | (DispMode << ZR36057_VFESPFR_DispMode)
558 | ZR36057_VFESPFR_LittleEndian;
559 /* RJ: I don't know, why the following has to be the opposite
560 of the corresponding ZR36060 setting, but only this way
561 we get the correct colors when uncompressing to the screen */
562 reg |= ZR36057_VFESPFR_VCLKPol;
563 /* RJ: Don't know if that is needed for NTSC also */
564 reg |= ZR36057_VFESPFR_TopField;
565 switch (video_format) {
567 case VIDEO_PALETTE_YUV422:
568 reg |= ZR36057_VFESPFR_YUV422;
571 case VIDEO_PALETTE_RGB555:
572 reg |= ZR36057_VFESPFR_RGB555 | ZR36057_VFESPFR_ErrDif;
575 case VIDEO_PALETTE_RGB565:
576 reg |= ZR36057_VFESPFR_RGB565 | ZR36057_VFESPFR_ErrDif;
579 case VIDEO_PALETTE_RGB24:
580 reg |= ZR36057_VFESPFR_RGB888 | ZR36057_VFESPFR_Pack24;
583 case VIDEO_PALETTE_RGB32:
584 reg |= ZR36057_VFESPFR_RGB888;
588 printk(KERN_INFO "%s: Unknown color_fmt=%x\n", zr->name, video_format);
593 reg |= 3 << ZR36057_VFESPFR_HFilter; /* 5 tap filter */
594 } else if (HorDcm >= 32) {
595 reg |= 2 << ZR36057_VFESPFR_HFilter; /* 4 tap filter */
596 } else if (HorDcm >= 16) {
597 reg |= 1 << ZR36057_VFESPFR_HFilter; /* 3 tap filter */
599 btwrite(reg, ZR36057_VFESPFR);
601 /* display configuration */
603 reg = (16 << ZR36057_VDCR_MinPix)
604 | (VidWinHt << ZR36057_VDCR_VidWinHt)
605 | (VidWinWid << ZR36057_VDCR_VidWinWid);
607 reg &= ~ZR36057_VDCR_Triton;
609 reg |= ZR36057_VDCR_Triton;
610 btwrite(reg, ZR36057_VDCR);
612 /* Write overlay clipping mask data, but don't enable overlay clipping */
613 /* RJ: since this makes only sense on the screen, we use
614 zr->window.width instead of video_width */
616 mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
617 reg = virt_to_bus(zr->overlay_mask);
618 btwrite(reg, ZR36057_MMTR);
619 reg = virt_to_bus(zr->overlay_mask + mask_line_size);
620 btwrite(reg, ZR36057_MMBR);
621 reg = mask_line_size - (zr->window.width + 31) / 32;
623 reg += mask_line_size;
624 reg <<= ZR36057_OCR_MaskStride;
625 btwrite(reg, ZR36057_OCR);
630 * Switch overlay on or off
633 static void zr36057_overlay(struct zoran *zr, int on)
639 /* do the necessary settings ... */
641 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR); /* switch it off first */
643 switch (zr->buffer.depth) {
645 fmt = VIDEO_PALETTE_RGB555;
649 fmt = VIDEO_PALETTE_RGB565;
653 fmt = VIDEO_PALETTE_RGB24;
657 fmt = VIDEO_PALETTE_RGB32;
665 zr36057_set_vfe(zr, zr->window.width, zr->window.height, fmt);
667 /* Start and length of each line MUST be 4-byte aligned.
668 This should be allready checked before the call to this routine.
669 All error messages are internal driver checking only! */
671 /* video display top and bottom registers */
673 reg = (u32) zr->buffer.base
675 + zr->window.y * zr->buffer.bytesperline;
676 btwrite(reg, ZR36057_VDTR);
678 printk(KERN_ERR "%s: zr36057_overlay: video_address not aligned\n", zr->name);
679 if (zr->video_interlace)
680 reg += zr->buffer.bytesperline;
681 btwrite(reg, ZR36057_VDBR);
683 /* video stride, status, and frame grab register */
685 reg = zr->buffer.bytesperline - zr->window.width * bpp;
686 if (zr->video_interlace)
687 reg += zr->buffer.bytesperline;
689 printk(KERN_ERR "%s: zr36057_overlay: video_stride not aligned\n", zr->name);
690 reg = (reg << ZR36057_VSSFGR_DispStride);
691 reg |= ZR36057_VSSFGR_VidOvf; /* clear overflow status */
692 btwrite(reg, ZR36057_VSSFGR);
694 /* Set overlay clipping */
696 if (zr->window.clipcount)
697 btor(ZR36057_OCR_OvlEnable, ZR36057_OCR);
699 /* ... and switch it on */
701 btor(ZR36057_VDCR_VidEn, ZR36057_VDCR);
705 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
710 * The overlay mask has one bit for each pixel on a scan line,
711 * and the maximum window size is BUZ_MAX_WIDTH * BUZ_MAX_HEIGHT pixels.
713 static void write_overlay_mask(struct zoran *zr, struct video_clip *vp, int count)
715 unsigned mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
717 int x, y, width, height;
721 /* fill mask with one bits */
722 memset(zr->overlay_mask, ~0, mask_line_size * 4 * BUZ_MAX_HEIGHT);
725 for (i = 0; i < count; ++i) {
726 /* pick up local copy of clip */
730 height = vp[i].height;
732 /* trim clips that extend beyond the window */
741 if (x + width > zr->window.width) {
742 width = zr->window.width - x;
744 if (y + height > zr->window.height) {
745 height = zr->window.height - y;
747 /* ignore degenerate clips */
754 /* apply clip for each scan line */
755 for (j = 0; j < height; ++j) {
756 /* reset bit for each pixel */
757 /* this can be optimized later if need be */
758 mask = zr->overlay_mask + (y + j) * mask_line_size;
759 for (k = 0; k < width; ++k) {
760 mask[(x + k) / 32] &= ~((u32) 1 << (x + k) % 32);
766 /* Enable/Disable uncompressed memory grabbing of the 36057 */
768 static void zr36057_set_memgrab(struct zoran *zr, int mode)
771 if (btread(ZR36057_VSSFGR) & (ZR36057_VSSFGR_SnapShot | ZR36057_VSSFGR_FrameGrab))
772 printk(KERN_WARNING "%s: zr36057_set_memgrab_on with SnapShot or FrameGrab on ???\n", zr->name);
774 /* switch on VSync interrupts */
776 btwrite(IRQ_MASK, ZR36057_ISR); // Clear Interrupts
778 btor(ZR36057_ICR_GIRQ0, ZR36057_ICR);
780 /* enable SnapShot */
782 btor(ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
784 /* Set zr36057 video front end and enable video */
787 zr36057_set_vfe(zr, zr->gwidth > 720 ? 720 : zr->gwidth, zr->gheight, zr->gformat);
789 zr36057_set_vfe(zr, zr->gwidth, zr->gheight, zr->gformat);
792 zr->v4l_memgrab_active = 1;
794 zr->v4l_memgrab_active = 0;
796 /* switch off VSync interrupts */
798 btand(~ZR36057_ICR_GIRQ0, ZR36057_ICR);
800 /* reenable grabbing to screen if it was running */
802 if (zr->v4l_overlay_active) {
803 zr36057_overlay(zr, 1);
805 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
806 btand(~ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
811 static int wait_grab_pending(struct zoran *zr)
815 /* wait until all pending grabs are finished */
817 if (!zr->v4l_memgrab_active)
820 while (zr->v4l_pend_tail != zr->v4l_pend_head) {
821 interruptible_sleep_on(&zr->v4l_capq);
822 if (signal_pending(current))
826 spin_lock_irqsave(&zr->lock, flags);
827 zr36057_set_memgrab(zr, 0);
828 spin_unlock_irqrestore(&zr->lock, flags);
834 * V4L Buffer grabbing
837 static int v4l_grab(struct zoran *zr, struct video_mmap *mp)
843 * There is a long list of limitations to what is allowed to be grabbed
844 * We don't output error messages her, since some programs (e.g. xawtv)
845 * just try several settings to find out what is valid or not.
848 /* No grabbing outside the buffer range! */
850 if (mp->frame >= v4l_nbufs || mp->frame < 0)
853 /* Check size and format of the grab wanted */
855 if (mp->height < BUZ_MIN_HEIGHT || mp->width < BUZ_MIN_WIDTH)
857 if (mp->height > BUZ_MAX_HEIGHT || mp->width > BUZ_MAX_WIDTH)
860 bpp = format2bpp(mp->format);
864 /* Check against available buffer size */
866 if (mp->height * mp->width * bpp > v4l_bufsize)
869 /* The video front end needs 4-byte alinged line sizes */
871 if ((bpp == 2 && (mp->width & 1)) || (bpp == 3 && (mp->width & 3)))
875 * To minimize the time spent in the IRQ routine, we avoid setting up
876 * the video front end there.
877 * If this grab has different parameters from a running streaming capture
878 * we stop the streaming capture and start it over again.
881 if (zr->v4l_memgrab_active &&
882 (zr->gwidth != mp->width || zr->gheight != mp->height || zr->gformat != mp->format)) {
883 res = wait_grab_pending(zr);
887 zr->gwidth = mp->width;
888 zr->gheight = mp->height;
889 zr->gformat = mp->format;
890 zr->gbpl = bpp * zr->gwidth;
893 spin_lock_irqsave(&zr->lock, flags);
895 /* make sure a grab isn't going on currently with this buffer */
897 switch (zr->v4l_gbuf[mp->frame].state) {
901 res = -EBUSY; /* what are you doing? */
906 /* since there is at least one unused buffer there's room for at least one more pend[] entry */
907 zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] = mp->frame;
908 zr->v4l_gbuf[mp->frame].state = BUZ_STATE_PEND;
914 /* put the 36057 into frame grabbing mode */
916 if (!res && !zr->v4l_memgrab_active)
917 zr36057_set_memgrab(zr, 1);
919 spin_unlock_irqrestore(&zr->lock, flags);
925 * Sync on a V4L buffer
928 static int v4l_sync(struct zoran *zr, int frame)
933 /* check passed-in frame number */
934 if (frame >= v4l_nbufs || frame < 0) {
935 printk(KERN_ERR "%s: v4l_sync: frame %d is invalid\n", zr->name, frame);
938 /* Check if is buffer was queued at all */
940 if (zr->v4l_gbuf[frame].state == BUZ_STATE_USER) {
941 // printk(KERN_ERR "%s: v4l_sync: Trying to sync on a buffer which was not queued?\n", zr->name);
944 /* wait on this buffer to get ready */
946 while (zr->v4l_gbuf[frame].state == BUZ_STATE_PEND) {
947 interruptible_sleep_on(&zr->v4l_capq);
948 if (signal_pending(current))
952 /* buffer should now be in BUZ_STATE_DONE */
954 if (zr->v4l_gbuf[frame].state != BUZ_STATE_DONE)
955 printk(KERN_ERR "%s: v4l_sync - internal error\n", zr->name);
957 /* Check if streaming capture has finished */
959 spin_lock_irqsave(&zr->lock, flags);
961 if (zr->v4l_pend_tail == zr->v4l_pend_head)
962 zr36057_set_memgrab(zr, 0);
964 spin_unlock_irqrestore(&zr->lock, flags);
968 /*****************************************************************************
970 * Set up the Buz-specific MJPEG part *
972 *****************************************************************************/
975 * Wait til post office is no longer busy
978 static int post_office_wait(struct zoran *zr)
983 while (((por = btread(ZR36057_POR)) & (ZR36057_POR_POPen | ZR36057_POR_POTime)) == ZR36057_POR_POPen) {
987 printk(KERN_ERR "%s: timeout on post office.\n", zr->name);
990 /* wait for something to happen */
992 if ((por & ZR36057_POR_POPen) != 0) {
993 printk(KERN_WARNING "%s: pop pending %08x\n", zr->name, por);
996 if ((por & (ZR36057_POR_POTime | ZR36057_POR_POPen)) != 0) {
997 printk(KERN_WARNING "%s: pop timeout %08x\n", zr->name, por);
1003 static int post_office_write(struct zoran *zr, unsigned guest, unsigned reg, unsigned value)
1007 post_office_wait(zr);
1008 por = ZR36057_POR_PODir | ZR36057_POR_POTime | ((guest & 7) << 20) | ((reg & 7) << 16) | (value & 0xFF);
1009 btwrite(por, ZR36057_POR);
1010 return post_office_wait(zr);
1013 static int post_office_read(struct zoran *zr, unsigned guest, unsigned reg)
1017 post_office_wait(zr);
1018 por = ZR36057_POR_POTime | ((guest & 7) << 20) | ((reg & 7) << 16);
1019 btwrite(por, ZR36057_POR);
1020 if (post_office_wait(zr) < 0) {
1023 return btread(ZR36057_POR) & 0xFF;
1026 static int zr36060_write_8(struct zoran *zr, unsigned reg, unsigned val)
1028 if (post_office_wait(zr)
1029 || post_office_write(zr, 0, 1, reg >> 8)
1030 || post_office_write(zr, 0, 2, reg)) {
1033 return post_office_write(zr, 0, 3, val);
1036 static int zr36060_write_16(struct zoran *zr, unsigned reg, unsigned val)
1038 if (zr36060_write_8(zr, reg + 0, val >> 8)) {
1041 return zr36060_write_8(zr, reg + 1, val >> 0);
1044 static int zr36060_write_24(struct zoran *zr, unsigned reg, unsigned val)
1046 if (zr36060_write_8(zr, reg + 0, val >> 16)) {
1049 return zr36060_write_16(zr, reg + 1, val >> 0);
1052 static int zr36060_write_32(struct zoran *zr, unsigned reg, unsigned val)
1054 if (zr36060_write_16(zr, reg + 0, val >> 16)) {
1057 return zr36060_write_16(zr, reg + 2, val >> 0);
1060 static u32 zr36060_read_8(struct zoran *zr, unsigned reg)
1062 if (post_office_wait(zr)
1063 || post_office_write(zr, 0, 1, reg >> 8)
1064 || post_office_write(zr, 0, 2, reg)) {
1067 return post_office_read(zr, 0, 3) & 0xFF;
1070 static int zr36060_reset(struct zoran *zr)
1072 return post_office_write(zr, 3, 0, 0);
1075 static void zr36060_sleep(struct zoran *zr, int sleep)
1077 GPIO(zr, 1, !sleep);
1081 static void zr36060_set_jpg(struct zoran *zr, enum zoran_codec_mode mode)
1087 reg = (1 << 0) /* CodeMstr */
1088 |(0 << 2) /* CFIS=0 */
1089 |(0 << 6) /* Endian=0 */
1090 |(0 << 7); /* Code16=0 */
1091 zr36060_write_8(zr, 0x002, reg);
1095 case BUZ_MODE_MOTION_DECOMPRESS:
1096 case BUZ_MODE_STILL_DECOMPRESS:
1097 reg = 0x00; /* Codec mode = decompression */
1100 case BUZ_MODE_MOTION_COMPRESS:
1101 case BUZ_MODE_STILL_COMPRESS:
1103 reg = 0xa4; /* Codec mode = compression with variable scale factor */
1107 zr36060_write_8(zr, 0x003, reg);
1109 reg = 0x00; /* reserved, mbz */
1110 zr36060_write_8(zr, 0x004, reg);
1112 reg = 0xff; /* 510 bits/block */
1113 zr36060_write_8(zr, 0x005, reg);
1116 reg = (zr->params.jpeg_markers) & 0x38; /* DRI, DQT, DHT */
1117 if (zr->params.COM_len)
1118 reg |= JPEG_MARKER_COM;
1119 if (zr->params.APP_len)
1120 reg |= JPEG_MARKER_APP;
1121 zr36060_write_8(zr, 0x006, reg);
1123 reg = (0 << 3) /* DATERR=0 */
1124 |(0 << 2) /* END=0 */
1125 |(0 << 1) /* EOI=0 */
1126 |(0 << 0); /* EOAV=0 */
1127 zr36060_write_8(zr, 0x007, reg);
1131 /* Target field size in pixels: */
1132 tvn = &tvnorms[zr->params.norm];
1133 size = (tvn->Ha / 2) * (tvn->Wa) / (zr->params.HorDcm) / (zr->params.VerDcm);
1135 /* Target compressed field size in bits: */
1136 size = size * 16; /* uncompressed size in bits */
1137 size = size * zr->params.quality / 400; /* quality = 100 is a compression ratio 1:4 */
1139 /* Lower limit (arbitrary, 1 KB) */
1143 /* Upper limit: 7/8 of the code buffers */
1144 if (size * zr->params.field_per_buff > zr->jpg_bufsize * 7)
1145 size = zr->jpg_bufsize * 7 / zr->params.field_per_buff;
1148 zr36060_write_32(zr, 0x009, reg);
1150 /* how do we set initial SF as a function of quality parameter? */
1151 reg = 0x0100; /* SF=1.0 */
1152 zr36060_write_16(zr, 0x011, reg);
1154 reg = 0x00ffffff; /* AF=max */
1155 zr36060_write_24(zr, 0x013, reg);
1157 reg = 0x0000; /* test */
1158 zr36060_write_16(zr, 0x024, reg);
1161 static void zr36060_set_video(struct zoran *zr, enum zoran_codec_mode mode)
1166 reg = (0 << 7) /* Video8=0 */
1167 |(0 << 6) /* Range=0 */
1168 |(0 << 3) /* FlDet=0 */
1169 |(1 << 2) /* FlVedge=1 */
1170 |(0 << 1) /* FlExt=0 */
1171 |(0 << 0); /* SyncMstr=0 */
1173 /* According to ZR36067 documentation, FlDet should correspond
1174 to the odd_even flag of the ZR36067 */
1175 if (zr->params.odd_even)
1178 if (mode != BUZ_MODE_STILL_DECOMPRESS) {
1179 /* limit pixels to range 16..235 as per CCIR-601 */
1180 reg |= (1 << 6); /* Range=1 */
1182 zr36060_write_8(zr, 0x030, reg);
1184 reg = (0 << 7) /* VCLKPol=0 */
1185 |(0 << 6) /* PValPol=0 */
1186 |(1 << 5) /* PoePol=1 */
1187 |(0 << 4) /* SImgPol=0 */
1188 |(0 << 3) /* BLPol=0 */
1189 |(0 << 2) /* FlPol=0 */
1190 |(0 << 1) /* HSPol=0, sync on falling edge */
1191 |(1 << 0); /* VSPol=1 */
1192 zr36060_write_8(zr, 0x031, reg);
1194 switch (zr->params.HorDcm) {
1198 break; /* HScale = 0 */
1202 break; /* HScale = 1 */
1206 break; /* HScale = 2 */
1208 if (zr->params.VerDcm == 2)
1210 zr36060_write_8(zr, 0x032, reg);
1212 reg = 0x80; /* BackY */
1213 zr36060_write_8(zr, 0x033, reg);
1215 reg = 0xe0; /* BackU */
1216 zr36060_write_8(zr, 0x034, reg);
1218 reg = 0xe0; /* BackV */
1219 zr36060_write_8(zr, 0x035, reg);
1221 /* sync generator */
1223 tvn = &tvnorms[zr->params.norm];
1225 reg = tvn->Ht - 1; /* Vtotal */
1226 zr36060_write_16(zr, 0x036, reg);
1228 reg = tvn->Wt - 1; /* Htotal */
1229 zr36060_write_16(zr, 0x038, reg);
1231 reg = 6 - 1; /* VsyncSize */
1232 zr36060_write_8(zr, 0x03a, reg);
1234 reg = 100 - 1; /* HsyncSize */
1235 zr36060_write_8(zr, 0x03b, reg);
1237 reg = tvn->VStart - 1; /* BVstart */
1238 zr36060_write_8(zr, 0x03c, reg);
1240 reg += tvn->Ha / 2; /* BVend */
1241 zr36060_write_16(zr, 0x03e, reg);
1243 reg = tvn->HStart - 1; /* BHstart */
1244 zr36060_write_8(zr, 0x03d, reg);
1246 reg += tvn->Wa; /* BHend */
1247 zr36060_write_16(zr, 0x040, reg);
1250 reg = zr->params.img_y + tvn->VStart; /* Vstart */
1251 zr36060_write_16(zr, 0x042, reg);
1253 reg += zr->params.img_height; /* Vend */
1254 zr36060_write_16(zr, 0x044, reg);
1256 reg = zr->params.img_x + tvn->HStart; /* Hstart */
1257 zr36060_write_16(zr, 0x046, reg);
1259 reg += zr->params.img_width; /* Hend */
1260 zr36060_write_16(zr, 0x048, reg);
1263 reg = zr->params.img_y + tvn->VStart; /* SVstart */
1264 zr36060_write_16(zr, 0x04a, reg);
1266 reg += zr->params.img_height; /* SVend */
1267 zr36060_write_16(zr, 0x04c, reg);
1269 reg = zr->params.img_x + tvn->HStart; /* SHstart */
1270 zr36060_write_16(zr, 0x04e, reg);
1272 reg += zr->params.img_width; /* SHend */
1273 zr36060_write_16(zr, 0x050, reg);
1276 static void zr36060_set_jpg_SOF(struct zoran *zr)
1281 reg = 0xffc0; /* SOF marker */
1282 zr36060_write_16(zr, 0x060, reg);
1284 reg = 17; /* SOF length */
1285 zr36060_write_16(zr, 0x062, reg);
1287 reg = 8; /* precision 8 bits */
1288 zr36060_write_8(zr, 0x064, reg);
1290 reg = zr->params.img_height / zr->params.VerDcm; /* image height */
1291 zr36060_write_16(zr, 0x065, reg);
1293 reg = zr->params.img_width / zr->params.HorDcm; /* image width */
1294 zr36060_write_16(zr, 0x067, reg);
1296 reg = 3; /* 3 color components */
1297 zr36060_write_8(zr, 0x069, reg);
1299 reg = 0x002100; /* Y component */
1300 zr36060_write_24(zr, 0x06a, reg);
1302 reg = 0x011101; /* U component */
1303 zr36060_write_24(zr, 0x06d, reg);
1305 reg = 0x021101; /* V component */
1306 zr36060_write_24(zr, 0x070, reg);
1309 static void zr36060_set_jpg_SOS(struct zoran *zr)
1314 reg = 0xffda; /* SOS marker */
1315 zr36060_write_16(zr, 0x07a, reg);
1317 reg = 12; /* SOS length */
1318 zr36060_write_16(zr, 0x07c, reg);
1320 reg = 3; /* 3 color components */
1321 zr36060_write_8(zr, 0x07e, reg);
1323 reg = 0x0000; /* Y component */
1324 zr36060_write_16(zr, 0x07f, reg);
1326 reg = 0x0111; /* U component */
1327 zr36060_write_16(zr, 0x081, reg);
1329 reg = 0x0211; /* V component */
1330 zr36060_write_16(zr, 0x083, reg);
1332 reg = 0x003f00; /* Start, end spectral scans */
1333 zr36060_write_24(zr, 0x085, reg);
1336 static void zr36060_set_jpg_DRI(struct zoran *zr)
1341 reg = 0xffdd; /* DRI marker */
1342 zr36060_write_16(zr, 0x0c0, reg);
1344 reg = 4; /* DRI length */
1345 zr36060_write_16(zr, 0x0c2, reg);
1347 reg = 8; /* length in MCUs */
1348 zr36060_write_16(zr, 0x0c4, reg);
1351 static void zr36060_set_jpg_DQT(struct zoran *zr)
1355 static const u8 dqt[] =
1357 0xff, 0xdb, /* DHT marker */
1358 0x00, 0x84, /* DHT length */
1359 0x00, /* table ID 0 */
1360 0x10, 0x0b, 0x0c, 0x0e, 0x0c, 0x0a, 0x10, 0x0e,
1361 0x0d, 0x0e, 0x12, 0x11, 0x10, 0x13, 0x18, 0x28,
1362 0x1a, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23, 0x25,
1363 0x1d, 0x28, 0x3a, 0x33, 0x3d, 0x3c, 0x39, 0x33,
1364 0x38, 0x37, 0x40, 0x48, 0x5c, 0x4e, 0x40, 0x44,
1365 0x57, 0x45, 0x37, 0x38, 0x50, 0x6d, 0x51, 0x57,
1366 0x5f, 0x62, 0x67, 0x68, 0x67, 0x3e, 0x4d, 0x71,
1367 0x79, 0x70, 0x64, 0x78, 0x5c, 0x65, 0x67, 0x63,
1368 0x01, /* table ID 1 */
1369 0x11, 0x12, 0x12, 0x18, 0x15, 0x18, 0x2f, 0x1a,
1370 0x1a, 0x2f, 0x63, 0x42, 0x38, 0x42, 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,
1375 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1376 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63
1379 /* write fixed quantitization tables */
1381 for (i = 0; i < sizeof(dqt); ++i) {
1382 zr36060_write_8(zr, adr++, dqt[i]);
1386 static void zr36060_set_jpg_DHT(struct zoran *zr)
1390 static const u8 dht[] =
1392 0xff, 0xc4, /* DHT marker */
1393 0x01, 0xa2, /* DHT length */
1394 0x00, /* table class 0, ID 0 */
1395 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, /* # codes of length 1..8 */
1396 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* # codes of length 8..16 */
1397 0x00, /* values for codes of length 2 */
1398 0x01, 0x02, 0x03, 0x04, 0x05, /* values for codes of length 3 */
1399 0x06, /* values for codes of length 4 */
1400 0x07, /* values for codes of length 5 */
1401 0x08, /* values for codes of length 6 */
1402 0x09, /* values for codes of length 7 */
1403 0x0a, /* values for codes of length 8 */
1404 0x0b, /* values for codes of length 9 */
1405 0x01, /* table class 0, ID 1 */
1406 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* # codes of length 1..8 */
1407 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, /* # codes of length 9..16 */
1408 0x00, 0x01, 0x02, /* values for codes of length 2 */
1409 0x03, /* values for codes of length 3 */
1410 0x04, /* values for codes of length 4 */
1411 0x05, /* values for codes of length 5 */
1412 0x06, /* values for codes of length 6 */
1413 0x07, /* values for codes of length 7 */
1414 0x08, /* values for codes of length 8 */
1415 0x09, /* values for codes of length 9 */
1416 0x0a, /* values for codes of length 10 */
1417 0x0b, /* values for codes of length 11 */
1419 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
1420 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d,
1421 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
1422 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
1423 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
1424 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
1425 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
1426 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
1427 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
1428 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
1429 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
1430 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
1431 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
1432 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
1433 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
1434 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
1435 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
1436 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
1437 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
1438 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
1439 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
1440 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
1441 0xf9, 0xfa, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04,
1442 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00,
1443 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11,
1444 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51,
1445 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08,
1446 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23,
1447 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a,
1448 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18,
1449 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35,
1450 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45,
1451 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55,
1452 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65,
1453 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75,
1454 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84,
1455 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93,
1456 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2,
1457 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
1458 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
1459 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8,
1460 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
1461 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
1462 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5,
1463 0xf6, 0xf7, 0xf8, 0xf9, 0xfa
1466 /* write fixed Huffman tables */
1468 for (i = 0; i < sizeof(dht); ++i) {
1469 zr36060_write_8(zr, adr++, dht[i]);
1473 static void zr36060_set_jpg_APP(struct zoran *zr)
1480 len = zr->params.APP_len;
1486 i = zr->params.APPn;
1492 reg = 0xffe0 + i; /* APPn marker */
1493 zr36060_write_16(zr, 0x380, reg);
1495 reg = len + 2; /* APPn len */
1496 zr36060_write_16(zr, 0x382, reg);
1498 /* write APPn data */
1500 for (i = 0; i < 60; i++) {
1501 zr36060_write_8(zr, adr++, (i < len ? zr->params.APP_data[i] : 0));
1505 static void zr36060_set_jpg_COM(struct zoran *zr)
1512 len = zr->params.COM_len;
1518 reg = 0xfffe; /* COM marker */
1519 zr36060_write_16(zr, 0x3c0, reg);
1521 reg = len + 2; /* COM len */
1522 zr36060_write_16(zr, 0x3c2, reg);
1524 /* write COM data */
1526 for (i = 0; i < 60; i++) {
1527 zr36060_write_8(zr, adr++, (i < len ? zr->params.COM_data[i] : 0));
1531 static void zr36060_set_cap(struct zoran *zr, enum zoran_codec_mode mode)
1539 reg = (0 << 7) /* Load=0 */
1540 |(1 << 0); /* SynRst=1 */
1541 zr36060_write_8(zr, 0x000, reg);
1543 zr36060_set_jpg(zr, mode);
1544 zr36060_set_video(zr, mode);
1545 zr36060_set_jpg_SOF(zr);
1546 zr36060_set_jpg_SOS(zr);
1547 zr36060_set_jpg_DRI(zr);
1548 zr36060_set_jpg_DQT(zr);
1549 zr36060_set_jpg_DHT(zr);
1550 zr36060_set_jpg_APP(zr);
1551 zr36060_set_jpg_COM(zr);
1553 reg = (1 << 7) /* Load=1 */
1554 |(0 << 0); /* SynRst=0 */
1555 zr36060_write_8(zr, 0x000, reg);
1557 /* wait for codec to unbusy */
1558 for (i = 0; i < 1000; ++i) {
1559 reg = zr36060_read_8(zr, 0x001);
1560 if ((reg & (1 << 7)) == 0) {
1561 DEBUG(printk(KERN_DEBUG "060: loaded, loops=%u\n", i));
1566 printk(KERN_INFO "060: stuck busy, statux=%02x\n", reg);
1569 static void zr36057_set_jpg(struct zoran *zr, enum zoran_codec_mode mode)
1575 tvn = &tvnorms[zr->params.norm];
1577 /* assert P_Reset */
1578 btwrite(0, ZR36057_JPC);
1580 /* re-initialize DMA ring stuff */
1581 zr->jpg_que_head = 0;
1582 zr->jpg_dma_head = 0;
1583 zr->jpg_dma_tail = 0;
1584 zr->jpg_que_tail = 0;
1585 zr->jpg_seq_num = 0;
1586 for (i = 0; i < BUZ_NUM_STAT_COM; ++i) {
1587 zr->stat_com[i] = 1; /* mark as unavailable to zr36057 */
1589 for (i = 0; i < zr->jpg_nbufs; i++) {
1590 zr->jpg_gbuf[i].state = BUZ_STATE_USER; /* nothing going on */
1593 /* MJPEG compression mode */
1596 case BUZ_MODE_MOTION_COMPRESS:
1598 reg = ZR36057_JMC_MJPGCmpMode;
1601 case BUZ_MODE_MOTION_DECOMPRESS:
1602 reg = ZR36057_JMC_MJPGExpMode;
1603 reg |= ZR36057_JMC_SyncMstr;
1604 /* RJ: The following is experimental - improves the output to screen */
1605 if (zr->params.VFIFO_FB)
1606 reg |= ZR36057_JMC_VFIFO_FB;
1609 case BUZ_MODE_STILL_COMPRESS:
1610 reg = ZR36057_JMC_JPGCmpMode;
1613 case BUZ_MODE_STILL_DECOMPRESS:
1614 reg = ZR36057_JMC_JPGExpMode;
1618 reg |= ZR36057_JMC_JPG;
1619 if (zr->params.field_per_buff == 1)
1620 reg |= ZR36057_JMC_Fld_per_buff;
1621 btwrite(reg, ZR36057_JMC);
1624 btor(ZR36057_VFEVCR_VSPol, ZR36057_VFEVCR);
1625 reg = (6 << ZR36057_VSP_VsyncSize) | (tvn->Ht << ZR36057_VSP_FrmTot);
1626 btwrite(reg, ZR36057_VSP);
1627 reg = ((zr->params.img_y + tvn->VStart) << ZR36057_FVAP_NAY)
1628 | (zr->params.img_height << ZR36057_FVAP_PAY);
1629 btwrite(reg, ZR36057_FVAP);
1632 btor(ZR36057_VFEHCR_HSPol, ZR36057_VFEHCR);
1633 reg = ((tvn->Wt - 100) << ZR36057_HSP_HsyncStart) | (tvn->Wt << ZR36057_HSP_LineTot);
1634 btwrite(reg, ZR36057_HSP);
1635 reg = ((zr->params.img_x + tvn->HStart) << ZR36057_FHAP_NAX)
1636 | (zr->params.img_width << ZR36057_FHAP_PAX);
1637 btwrite(reg, ZR36057_FHAP);
1639 /* field process parameters */
1640 if (zr->params.odd_even)
1641 reg = ZR36057_FPP_Odd_Even;
1644 btwrite(reg, ZR36057_FPP);
1646 /* Set proper VCLK Polarity, else colors will be wrong during playback */
1647 btor(ZR36057_VFESPFR_VCLKPol, ZR36057_VFESPFR);
1649 /* code base address and FIFO threshold */
1650 reg = virt_to_bus(zr->stat_com);
1651 btwrite(reg, ZR36057_JCBA);
1653 btwrite(reg, ZR36057_JCFT);
1655 /* JPEG codec guest ID */
1656 reg = (1 << ZR36057_JCGI_JPEGuestID) | (0 << ZR36057_JCGI_JPEGuestReg);
1657 btwrite(reg, ZR36057_JCGI);
1659 /* Code transfer guest ID */
1660 reg = (0 << ZR36057_MCTCR_CodGuestID) | (3 << ZR36057_MCTCR_CodGuestReg);
1661 reg |= ZR36057_MCTCR_CFlush;
1662 btwrite(reg, ZR36057_MCTCR);
1664 /* deassert P_Reset */
1665 btwrite(ZR36057_JPC_P_Reset, ZR36057_JPC);
1668 static void zr36057_enable_jpg(struct zoran *zr, enum zoran_codec_mode mode)
1670 static int zero = 0;
1675 case BUZ_MODE_MOTION_COMPRESS:
1676 zr36060_set_cap(zr, mode);
1677 zr36057_set_jpg(zr, mode);
1678 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_ENABLE_OUTPUT, &one);
1679 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_INPUT, &zero);
1681 /* deassert P_Reset, assert Code transfer enable */
1682 btwrite(IRQ_MASK, ZR36057_ISR);
1683 btand(~ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
1686 case BUZ_MODE_MOTION_DECOMPRESS:
1687 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_ENABLE_OUTPUT, &zero);
1688 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_INPUT, &one);
1689 zr36060_set_cap(zr, mode);
1690 zr36057_set_jpg(zr, mode);
1692 /* deassert P_Reset, assert Code transfer enable */
1693 btwrite(IRQ_MASK, ZR36057_ISR);
1694 btand(~ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
1699 /* shut down processing */
1700 btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
1701 btwrite(ZR36057_JPC_P_Reset, ZR36057_JPC);
1702 btand(~ZR36057_JMC_VFIFO_FB, ZR36057_JMC);
1703 btand(~ZR36057_JMC_SyncMstr, ZR36057_JMC);
1704 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1705 btwrite(0, ZR36057_ISR);
1707 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_ENABLE_OUTPUT, &one);
1708 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_INPUT, &zero);
1712 zr->codec_mode = mode;
1716 * Queue a MJPEG buffer for capture/playback
1719 static int jpg_qbuf(struct zoran *zr, int frame, enum zoran_codec_mode mode)
1721 unsigned long flags;
1724 /* Check if buffers are allocated */
1726 if (!zr->jpg_buffers_allocated) {
1727 printk(KERN_ERR "%s: jpg_qbuf: buffers not yet allocated\n", zr->name);
1730 /* Does the user want to stop streaming? */
1733 if (zr->codec_mode == mode) {
1734 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1737 printk(KERN_ERR "%s: jpg_qbuf - stop streaming but not in streaming mode\n", zr->name);
1741 /* No grabbing outside the buffer range! */
1743 if (frame >= zr->jpg_nbufs) {
1744 printk(KERN_ERR "%s: jpg_qbuf: buffer %d out of range\n", zr->name, frame);
1747 /* what is the codec mode right now? */
1749 if (zr->codec_mode == BUZ_MODE_IDLE) {
1750 /* Ok load up the zr36060 and go */
1751 zr36057_enable_jpg(zr, mode);
1752 } else if (zr->codec_mode != mode) {
1753 /* wrong codec mode active - invalid */
1754 printk(KERN_ERR "%s: jpg_qbuf - codec in wrong mode\n", zr->name);
1757 spin_lock_irqsave(&zr->lock, flags);
1759 /* make sure a grab isn't going on currently with this buffer */
1761 switch (zr->jpg_gbuf[frame].state) {
1765 case BUZ_STATE_PEND:
1766 case BUZ_STATE_DONE:
1767 res = -EBUSY; /* what are you doing? */
1770 case BUZ_STATE_USER:
1771 /* since there is at least one unused buffer there's room for at least one more pend[] entry */
1772 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = frame;
1773 zr->jpg_gbuf[frame].state = BUZ_STATE_PEND;
1774 zoran_feed_stat_com(zr);
1780 spin_unlock_irqrestore(&zr->lock, flags);
1782 /* Start the zr36060 when the first frame is queued */
1783 if (zr->jpg_que_head == 1) {
1784 btor(ZR36057_JMC_Go_en, ZR36057_JMC);
1785 btwrite(ZR36057_JPC_P_Reset | ZR36057_JPC_CodTrnsEn | ZR36057_JPC_Active, ZR36057_JPC);
1791 * Sync on a MJPEG buffer
1794 static int jpg_sync(struct zoran *zr, struct zoran_sync *bs)
1796 unsigned long flags;
1799 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
1800 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
1803 while (zr->jpg_que_tail == zr->jpg_dma_tail) {
1804 interruptible_sleep_on(&zr->jpg_capq);
1805 if (signal_pending(current))
1806 return -ERESTARTSYS;
1809 spin_lock_irqsave(&zr->lock, flags);
1811 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
1813 /* buffer should now be in BUZ_STATE_DONE */
1815 if (zr->jpg_gbuf[frame].state != BUZ_STATE_DONE)
1816 printk(KERN_ERR "%s: jpg_sync - internal error\n", zr->name);
1818 *bs = zr->jpg_gbuf[frame].bs;
1819 zr->jpg_gbuf[frame].state = BUZ_STATE_USER;
1821 spin_unlock_irqrestore(&zr->lock, flags);
1826 /* when this is called the spinlock must be held */
1827 static void zoran_feed_stat_com(struct zoran *zr)
1829 /* move frames from pending queue to DMA */
1831 int frame, i, max_stat_com;
1833 max_stat_com = (zr->params.TmpDcm == 1) ? BUZ_NUM_STAT_COM : (BUZ_NUM_STAT_COM >> 1);
1835 while ((zr->jpg_dma_head - zr->jpg_dma_tail) < max_stat_com
1836 && zr->jpg_dma_head != zr->jpg_que_head) {
1838 frame = zr->jpg_pend[zr->jpg_dma_head & BUZ_MASK_FRAME];
1839 if (zr->params.TmpDcm == 1) {
1840 /* fill 1 stat_com entry */
1841 i = zr->jpg_dma_head & BUZ_MASK_STAT_COM;
1842 zr->stat_com[i] = zr->jpg_gbuf[frame].frag_tab_bus;
1844 /* fill 2 stat_com entries */
1845 i = (zr->jpg_dma_head & 1) * 2;
1846 zr->stat_com[i] = zr->jpg_gbuf[frame].frag_tab_bus;
1847 zr->stat_com[i + 1] = zr->jpg_gbuf[frame].frag_tab_bus;
1849 zr->jpg_gbuf[frame].state = BUZ_STATE_DMA;
1855 /* when this is called the spinlock must be held */
1856 static void zoran_reap_stat_com(struct zoran *zr)
1858 /* move frames from DMA queue to done queue */
1865 struct zoran_gbuffer *gbuf;
1867 /* In motion decompress we don't have a hardware frame counter,
1868 we just count the interrupts here */
1870 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS)
1873 while (zr->jpg_dma_tail != zr->jpg_dma_head) {
1874 if (zr->params.TmpDcm == 1)
1875 i = zr->jpg_dma_tail & BUZ_MASK_STAT_COM;
1877 i = (zr->jpg_dma_tail & 1) * 2 + 1;
1879 stat_com = zr->stat_com[i];
1881 if ((stat_com & 1) == 0) {
1884 frame = zr->jpg_pend[zr->jpg_dma_tail & BUZ_MASK_FRAME];
1885 gbuf = &zr->jpg_gbuf[frame];
1886 get_fast_time(&gbuf->bs.timestamp);
1888 if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1889 gbuf->bs.length = (stat_com & 0x7fffff) >> 1;
1891 /* update sequence number with the help of the counter in stat_com */
1893 seq = stat_com >> 24;
1894 dif = (seq - zr->jpg_seq_num) & 0xff;
1895 zr->jpg_seq_num += dif;
1897 gbuf->bs.length = 0;
1899 gbuf->bs.seq = zr->params.TmpDcm == 2 ? (zr->jpg_seq_num >> 1) : zr->jpg_seq_num;
1900 gbuf->state = BUZ_STATE_DONE;
1906 static void zoran_irq(int irq, void *dev_id, struct pt_regs *regs)
1911 unsigned long flags;
1913 zr = (struct zoran *) dev_id;
1916 spin_lock_irqsave(&zr->lock, flags);
1918 /* get/clear interrupt status bits */
1919 stat = btread(ZR36057_ISR);
1920 astat = stat & IRQ_MASK;
1924 btwrite(astat, ZR36057_ISR);
1925 IDEBUG(printk(BUZ_DEBUG "-%u: astat %08x stat %08x\n", zr->id, astat, stat));
1927 #if (IRQ_MASK & ZR36057_ISR_GIRQ0)
1928 if (astat & ZR36057_ISR_GIRQ0) {
1930 /* Interrupts may still happen when zr->v4l_memgrab_active is switched off.
1931 We simply ignore them */
1933 if (zr->v4l_memgrab_active) {
1935 /* A lot more checks should be here ... */
1936 if ((btread(ZR36057_VSSFGR) & ZR36057_VSSFGR_SnapShot) == 0)
1937 printk(KERN_WARNING "%s: BuzIRQ with SnapShot off ???\n", zr->name);
1939 if (zr->v4l_grab_frame != NO_GRAB_ACTIVE) {
1940 /* There is a grab on a frame going on, check if it has finished */
1942 if ((btread(ZR36057_VSSFGR) & ZR36057_VSSFGR_FrameGrab) == 0) {
1943 /* it is finished, notify the user */
1945 zr->v4l_gbuf[zr->v4l_grab_frame].state = BUZ_STATE_DONE;
1946 zr->v4l_grab_frame = NO_GRAB_ACTIVE;
1948 zr->v4l_pend_tail++;
1951 if (zr->v4l_grab_frame == NO_GRAB_ACTIVE)
1952 wake_up_interruptible(&zr->v4l_capq);
1954 /* Check if there is another grab queued */
1956 if (zr->v4l_grab_frame == NO_GRAB_ACTIVE &&
1957 zr->v4l_pend_tail != zr->v4l_pend_head) {
1959 int frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
1962 zr->v4l_grab_frame = frame;
1964 /* Set zr36057 video front end and enable video */
1966 /* Buffer address */
1968 reg = zr->v4l_gbuf[frame].fbuffer_bus;
1969 btwrite(reg, ZR36057_VDTR);
1970 if (zr->video_interlace)
1972 btwrite(reg, ZR36057_VDBR);
1974 /* video stride, status, and frame grab register */
1977 reg = (zr->gwidth > 720) ? ((zr->gwidth & ~3) - 720) * zr->gbpl / zr->gwidth : 0;
1981 if (zr->video_interlace)
1983 reg = (reg << ZR36057_VSSFGR_DispStride);
1984 reg |= ZR36057_VSSFGR_VidOvf;
1985 reg |= ZR36057_VSSFGR_SnapShot;
1986 reg |= ZR36057_VSSFGR_FrameGrab;
1987 btwrite(reg, ZR36057_VSSFGR);
1989 btor(ZR36057_VDCR_VidEn, ZR36057_VDCR);
1993 #endif /* (IRQ_MASK & ZR36057_ISR_GIRQ0) */
1995 #if (IRQ_MASK & ZR36057_ISR_GIRQ1)
1996 if (astat & ZR36057_ISR_GIRQ1) {
1997 unsigned csr = zr36060_read_8(zr, 0x001);
1998 unsigned isr = zr36060_read_8(zr, 0x008);
2000 IDEBUG(printk(KERN_DEBUG "%s: ZR36057_ISR_GIRQ1 60_code=%02x 60_intr=%02x\n",
2001 zr->name, csr, isr));
2003 btand(~ZR36057_ICR_GIRQ1, ZR36057_ICR);
2004 zoran_reap_stat_com(zr);
2005 zoran_feed_stat_com(zr);
2007 #endif /* (IRQ_MASK & ZR36057_ISR_GIRQ1) */
2009 #if (IRQ_MASK & ZR36057_ISR_CodRepIRQ)
2010 if (astat & ZR36057_ISR_CodRepIRQ) {
2011 IDEBUG(printk(KERN_DEBUG "%s: ZR36057_ISR_CodRepIRQ\n", zr->name));
2012 btand(~ZR36057_ICR_CodRepIRQ, ZR36057_ICR);
2014 #endif /* (IRQ_MASK & ZR36057_ISR_CodRepIRQ) */
2016 #if (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ)
2017 if ((astat & ZR36057_ISR_JPEGRepIRQ) &&
2018 (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
2019 zr->codec_mode == BUZ_MODE_MOTION_COMPRESS)) {
2020 zoran_reap_stat_com(zr);
2021 zoran_feed_stat_com(zr);
2022 wake_up_interruptible(&zr->jpg_capq);
2024 #endif /* (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ) */
2028 printk(KERN_WARNING "%s: irq loop %d\n", zr->name, count);
2030 btwrite(0, ZR36057_ICR);
2031 printk(KERN_ERR "%s: IRQ lockup, cleared int mask\n", zr->name);
2036 spin_unlock_irqrestore(&zr->lock, flags);
2039 /* Check a zoran_params struct for correctness, insert default params */
2041 static int zoran_check_params(struct zoran *zr, struct zoran_params *params)
2043 int err = 0, err0 = 0;
2045 /* insert constant params */
2047 params->major_version = MAJOR_VERSION;
2048 params->minor_version = MINOR_VERSION;
2050 /* Check input and norm */
2052 if (params->input != 0 && params->input != 1) {
2055 if (params->norm != VIDEO_MODE_PAL && params->norm != VIDEO_MODE_NTSC) {
2058 /* Check decimation, set default values for decimation = 1, 2, 4 */
2060 switch (params->decimation) {
2066 params->field_per_buff = 2;
2070 params->img_width = 720;
2071 params->img_height = tvnorms[params->norm].Ha / 2;
2079 params->field_per_buff = 1;
2083 params->img_width = 704;
2084 params->img_height = tvnorms[params->norm].Ha / 2;
2092 params->field_per_buff = 1;
2096 params->img_width = 704;
2097 params->img_height = tvnorms[params->norm].Ha / 2;
2102 /* We have to check the data the user has set */
2104 if (params->HorDcm != 1 && params->HorDcm != 2 && params->HorDcm != 4)
2106 if (params->VerDcm != 1 && params->VerDcm != 2)
2108 if (params->TmpDcm != 1 && params->TmpDcm != 2)
2110 if (params->field_per_buff != 1 && params->field_per_buff != 2)
2113 if (params->img_x < 0)
2115 if (params->img_y < 0)
2117 if (params->img_width < 0)
2119 if (params->img_height < 0)
2121 if (params->img_x + params->img_width > 720)
2123 if (params->img_y + params->img_height > tvnorms[params->norm].Ha / 2)
2125 if (params->img_width % (16 * params->HorDcm) != 0)
2127 if (params->img_height % (8 * params->VerDcm) != 0)
2140 if (params->quality > 100)
2141 params->quality = 100;
2142 if (params->quality < 5)
2143 params->quality = 5;
2145 if (params->APPn < 0)
2147 if (params->APPn > 15)
2149 if (params->APP_len < 0)
2150 params->APP_len = 0;
2151 if (params->APP_len > 60)
2152 params->APP_len = 60;
2153 if (params->COM_len < 0)
2154 params->COM_len = 0;
2155 if (params->COM_len > 60)
2156 params->COM_len = 60;
2164 static void zoran_open_init_params(struct zoran *zr)
2168 /* Per default, map the V4L Buffers */
2170 zr->map_mjpeg_buffers = 0;
2172 /* User must explicitly set a window */
2178 zr->window.width = 0;
2179 zr->window.height = 0;
2180 zr->window.chromakey = 0;
2181 zr->window.flags = 0;
2182 zr->window.clips = NULL;
2183 zr->window.clipcount = 0;
2185 zr->video_interlace = 0;
2187 zr->v4l_memgrab_active = 0;
2188 zr->v4l_overlay_active = 0;
2190 zr->v4l_grab_frame = NO_GRAB_ACTIVE;
2191 zr->v4l_grab_seq = 0;
2198 /* DMA ring stuff for V4L */
2200 zr->v4l_pend_tail = 0;
2201 zr->v4l_pend_head = 0;
2202 for (i = 0; i < v4l_nbufs; i++) {
2203 zr->v4l_gbuf[i].state = BUZ_STATE_USER; /* nothing going on */
2206 /* Set necessary params and call zoran_check_params to set the defaults */
2208 zr->params.decimation = 1;
2210 zr->params.quality = 50; /* default compression factor 8 */
2211 zr->params.odd_even = 1;
2213 zr->params.APPn = 0;
2214 zr->params.APP_len = 0; /* No APPn marker */
2215 for (i = 0; i < 60; i++)
2216 zr->params.APP_data[i] = 0;
2218 zr->params.COM_len = 0; /* No COM marker */
2219 for (i = 0; i < 60; i++)
2220 zr->params.COM_data[i] = 0;
2222 zr->params.VFIFO_FB = 0;
2224 memset(zr->params.reserved, 0, sizeof(zr->params.reserved));
2226 zr->params.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
2228 i = zoran_check_params(zr, &zr->params);
2230 printk(KERN_ERR "%s: zoran_open_init_params internal error\n", zr->name);
2234 * Open a buz card. Right now the flags stuff is just playing
2237 static int zoran_open(struct video_device *dev, int flags)
2239 struct zoran *zr = (struct zoran *) dev;
2241 DEBUG(printk(KERN_INFO ": zoran_open\n"));
2250 if (v4l_fbuffer_alloc(zr) < 0) {
2256 zoran_open_init_params(zr);
2258 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
2260 btwrite(IRQ_MASK, ZR36057_ISR); // Clears interrupts
2262 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
2274 static void zoran_close(struct video_device *dev)
2276 struct zoran *zr = (struct zoran *) dev;
2278 DEBUG(printk(KERN_INFO ": zoran_close\n"));
2280 /* disable interrupts */
2281 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
2283 /* wake up sleeping beauties */
2284 wake_up_interruptible(&zr->v4l_capq);
2285 wake_up_interruptible(&zr->jpg_capq);
2287 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
2288 zr36057_set_memgrab(zr, 0);
2289 if (zr->v4l_overlay_active)
2290 zr36057_overlay(zr, 0);
2294 v4l_fbuffer_free(zr);
2295 jpg_fbuffer_free(zr);
2299 DEBUG(printk(KERN_INFO ": zoran_close done\n"));
2303 static long zoran_read(struct video_device *dev, char *buf, unsigned long count, int nonblock)
2308 static long zoran_write(struct video_device *dev, const char *buf, unsigned long count, int nonblock)
2318 static int zoran_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
2320 struct zoran *zr = (struct zoran *) dev;
2326 struct video_capability b;
2327 IOCTL_DEBUG(printk("buz ioctl VIDIOCGCAP\n"));
2328 strncpy(b.name, zr->video_dev.name, sizeof(b.name));
2329 b.type = VID_TYPE_CAPTURE |
2334 /* theoretically we could also flag VID_TYPE_SUBCAPTURE
2335 but this is not even implemented in the BTTV driver */
2337 b.channels = 2; /* composite, svhs */
2339 b.maxwidth = BUZ_MAX_WIDTH;
2340 b.maxheight = BUZ_MAX_HEIGHT;
2341 b.minwidth = BUZ_MIN_WIDTH;
2342 b.minheight = BUZ_MIN_HEIGHT;
2343 if (copy_to_user(arg, &b, sizeof(b))) {
2351 struct video_channel v;
2353 if (copy_from_user(&v, arg, sizeof(v))) {
2356 IOCTL_DEBUG(printk("buz ioctl VIDIOCGCHAN for channel %d\n", v.channel));
2357 switch (v.channel) {
2359 strcpy(v.name, "Composite");
2362 strcpy(v.name, "SVHS");
2369 v.type = VIDEO_TYPE_CAMERA;
2370 v.norm = zr->params.norm;
2371 if (copy_to_user(arg, &v, sizeof(v))) {
2377 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2379 * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2381 * The famos BTTV driver has it implemented with a struct video_channel argument
2382 * and we follow it for compatibility reasons
2384 * BTW: this is the only way the user can set the norm!
2389 struct video_channel v;
2393 if (copy_from_user(&v, arg, sizeof(v))) {
2396 IOCTL_DEBUG(printk("buz ioctl VIDIOCSCHAN: channel=%d, norm=%d\n", v.channel, v.norm));
2397 switch (v.channel) {
2408 if (v.norm != VIDEO_MODE_PAL
2409 && v.norm != VIDEO_MODE_NTSC) {
2412 zr->params.norm = v.norm;
2413 zr->params.input = v.channel;
2415 /* We switch overlay off and on since a change in the norm
2416 needs different VFE settings */
2418 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active;
2420 zr36057_overlay(zr, 0);
2422 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &input);
2423 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &zr->params.norm);
2424 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_NORM, &zr->params.norm);
2427 zr36057_overlay(zr, 1);
2429 /* Make sure the changes come into effect */
2430 res = wait_grab_pending(zr);
2443 struct video_picture p = zr->picture;
2445 IOCTL_DEBUG(printk("buz ioctl VIDIOCGPICT\n"));
2446 p.depth = zr->buffer.depth;
2447 switch (zr->buffer.depth) {
2449 p.palette = VIDEO_PALETTE_RGB555;
2453 p.palette = VIDEO_PALETTE_RGB565;
2457 p.palette = VIDEO_PALETTE_RGB24;
2461 p.palette = VIDEO_PALETTE_RGB32;
2465 if (copy_to_user(arg, &p, sizeof(p))) {
2473 struct video_picture p;
2475 if (copy_from_user(&p, arg, sizeof(p))) {
2478 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_PICTURE, &p);
2479 IOCTL_DEBUG(printk("buz ioctl VIDIOCSPICT bri=%d hue=%d col=%d con=%d dep=%d pal=%d\n",
2480 p.brightness, p.hue, p.colour, p.contrast, p.depth, p.palette));
2481 /* The depth and palette values have no meaning to us,
2482 should we return -EINVAL if they don't fit ? */
2491 if (copy_from_user(&v, arg, sizeof(v))) {
2494 IOCTL_DEBUG(printk("buz ioctl VIDIOCCAPTURE: %d\n", v));
2495 /* If there is nothing to do, return immediatly */
2497 if ((v && zr->v4l_overlay_active) || (!v && !zr->v4l_overlay_active))
2501 zr->v4l_overlay_active = 0;
2502 if (!zr->v4l_memgrab_active)
2503 zr36057_overlay(zr, 0);
2504 /* When a grab is running, the video simply won't be switched on any more */
2506 if (!zr->buffer_set || !zr->window_set) {
2509 zr->v4l_overlay_active = 1;
2510 if (!zr->v4l_memgrab_active)
2511 zr36057_overlay(zr, 1);
2512 /* When a grab is running, the video will be switched on when grab is finished */
2514 /* Make sure the changes come into effect */
2515 res = wait_grab_pending(zr);
2523 IOCTL_DEBUG(printk("buz ioctl VIDIOCGWIN\n"));
2524 if (copy_to_user(arg, &zr->window, sizeof(zr->window))) {
2532 struct video_clip *vcp;
2533 struct video_window vw;
2536 if (copy_from_user(&vw, arg, sizeof(vw))) {
2539 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));
2540 if (!zr->buffer_set) {
2544 * The video front end needs 4-byte alinged line sizes, we correct that
2545 * silently here if necessary
2548 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
2549 end = (vw.x + vw.width) & ~1; /* round down */
2550 vw.x = (vw.x + 1) & ~1; /* round up */
2551 vw.width = end - vw.x;
2553 if (zr->buffer.depth == 24) {
2554 end = (vw.x + vw.width) & ~3; /* round down */
2555 vw.x = (vw.x + 3) & ~3; /* round up */
2556 vw.width = end - vw.x;
2559 // At least xawtv seems to care about the following - just leave it away
2561 * Also corrected silently (as long as window fits at all):
2562 * video not fitting the screen
2565 if (vw.x < 0 || vw.y < 0 || vw.x + vw.width > zr->buffer.width ||
2566 vw.y + vw.height > zr->buffer.height) {
2567 printk(BUZ_ERR ": VIDIOCSWIN: window does not fit frame buffer: %dx%d+%d*%d\n",
2568 vw.width, vw.height, vw.x, vw.y);
2576 if (vw.x + vw.width > zr->buffer.width)
2577 vw.width = zr->buffer.width - vw.x;
2578 if (vw.y + vw.height > zr->buffer.height)
2579 vw.height = zr->buffer.height - vw.y;
2583 /* Check for vaild parameters */
2584 if (vw.width < BUZ_MIN_WIDTH || vw.height < BUZ_MIN_HEIGHT ||
2585 vw.width > BUZ_MAX_WIDTH || vw.height > BUZ_MAX_HEIGHT) {
2593 zr->window.x = vw.x;
2594 zr->window.y = vw.y;
2595 zr->window.width = vw.width;
2596 zr->window.height = vw.height;
2597 zr->window.chromakey = 0;
2598 zr->window.flags = 0; // RJ: Is this intended for interlace on/off ?
2600 zr->window.clips = NULL;
2601 zr->window.clipcount = vw.clipcount;
2604 * If an overlay is running, we have to switch it off
2605 * and switch it on again in order to get the new settings in effect.
2607 * We also want to avoid that the overlay mask is written
2608 * when an overlay is running.
2611 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active;
2613 zr36057_overlay(zr, 0);
2616 * Write the overlay mask if clips are wanted.
2619 vcp = vmalloc(sizeof(struct video_clip) * (vw.clipcount + 4));
2623 if (copy_from_user(vcp, vw.clips, sizeof(struct video_clip) * vw.clipcount)) {
2627 write_overlay_mask(zr, vcp, vw.clipcount);
2631 zr36057_overlay(zr, 1);
2634 /* Make sure the changes come into effect */
2635 res = wait_grab_pending(zr);
2644 IOCTL_DEBUG(printk("buz ioctl VIDIOCGFBUF\n"));
2645 if (copy_to_user(arg, &zr->buffer, sizeof(zr->buffer))) {
2653 struct video_buffer v;
2655 if (!capable(CAP_SYS_ADMIN)
2656 || !capable(CAP_SYS_RAWIO))
2659 if (copy_from_user(&v, arg, sizeof(v)))
2662 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));
2663 if (zr->v4l_overlay_active) {
2664 /* Has the user gotten crazy ... ? */
2673 if (v.height <= 0 || v.width <= 0 || v.bytesperline <= 0) {
2676 if (v.bytesperline & 3) {
2680 zr->buffer.base = (void *) ((unsigned long) v.base & ~3);
2682 zr->buffer.height = v.height;
2683 zr->buffer.width = v.width;
2684 zr->buffer.depth = v.depth;
2685 zr->buffer.bytesperline = v.bytesperline;
2687 if (zr->buffer.base)
2689 zr->window_set = 0; /* The user should set new window parameters */
2693 /* RJ: what is VIDIOCKEY intended to do ??? */
2705 if (copy_from_user(&v, arg, sizeof(v))) {
2708 IOCTL_DEBUG(printk("buz ioctl VIDIOCSYNC %d\n", v));
2709 return v4l_sync(zr, v);
2712 case VIDIOCMCAPTURE:
2714 struct video_mmap vm;
2716 if (copy_from_user((void *) &vm, (void *) arg, sizeof(vm))) {
2719 IOCTL_DEBUG(printk("buz ioctl VIDIOCMCAPTURE frame=%d geom=%dx%d fmt=%d\n",
2720 vm.frame, vm.height, vm.width, vm.format));
2721 return v4l_grab(zr, &vm);
2726 struct video_mbuf vm;
2729 IOCTL_DEBUG(printk("buz ioctl VIDIOCGMBUF\n"));
2731 vm.size = v4l_nbufs * v4l_bufsize;
2732 vm.frames = v4l_nbufs;
2733 for (i = 0; i < v4l_nbufs; i++) {
2734 vm.offsets[i] = i * v4l_bufsize;
2737 /* The next mmap will map the V4L buffers */
2738 zr->map_mjpeg_buffers = 0;
2740 if (copy_to_user(arg, &vm, sizeof(vm))) {
2748 struct video_unit vu;
2750 IOCTL_DEBUG(printk("buz ioctl VIDIOCGUNIT\n"));
2751 vu.video = zr->video_dev.minor;
2752 vu.vbi = VIDEO_NO_UNIT;
2753 vu.radio = VIDEO_NO_UNIT;
2754 vu.audio = VIDEO_NO_UNIT;
2755 vu.teletext = VIDEO_NO_UNIT;
2756 if (copy_to_user(arg, &vu, sizeof(vu)))
2762 * RJ: In principal we could support subcaptures for V4L grabbing.
2763 * Not even the famous BTTV driver has them, however.
2764 * If there should be a strong demand, one could consider
2765 * to implement them.
2767 case VIDIOCGCAPTURE:
2768 case VIDIOCSCAPTURE:
2771 case BUZIOC_G_PARAMS:
2773 IOCTL_DEBUG(printk("buz ioctl BUZIOC_G_PARAMS\n"));
2774 if (copy_to_user(arg, &(zr->params), sizeof(zr->params)))
2779 case BUZIOC_S_PARAMS:
2781 struct zoran_params bp;
2784 if (zr->codec_mode != BUZ_MODE_IDLE) {
2787 if (copy_from_user(&bp, arg, sizeof(bp))) {
2790 IOCTL_DEBUG(printk("buz ioctl BUZIOC_S_PARAMS\n"));
2792 /* Check the params first before overwriting our internal values */
2794 if (zoran_check_params(zr, &bp))
2799 /* Make changes of input and norm go into effect immediatly */
2801 /* We switch overlay off and on since a change in the norm
2802 needs different VFE settings */
2804 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active;
2806 zr36057_overlay(zr, 0);
2808 input = zr->params.input == 0 ? 3 : 7;
2809 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &input);
2810 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &zr->params.norm);
2811 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_NORM, &zr->params.norm);
2814 zr36057_overlay(zr, 1);
2816 if (copy_to_user(arg, &bp, sizeof(bp))) {
2822 case BUZIOC_REQBUFS:
2824 struct zoran_requestbuffers br;
2826 if (zr->jpg_buffers_allocated) {
2829 if (copy_from_user(&br, arg, sizeof(br))) {
2832 IOCTL_DEBUG(printk("buz ioctl BUZIOC_REQBUFS count = %lu size=%lu\n",
2833 br.count, br.size));
2834 /* Enforce reasonable lower and upper limits */
2836 br.count = 4; /* Could be choosen smaller */
2837 if (br.count > BUZ_MAX_FRAME)
2838 br.count = BUZ_MAX_FRAME;
2839 br.size = PAGE_ALIGN(br.size);
2841 br.size = 8192; /* Arbitrary */
2842 /* br.size is limited by 1 page for the stat_com tables to a Maximum of 2 MB */
2843 if (br.size > (512 * 1024))
2844 br.size = (512 * 1024); /* 512 K should be enough */
2845 if (zr->need_contiguous && br.size > MAX_KMALLOC_MEM)
2846 br.size = MAX_KMALLOC_MEM;
2848 zr->jpg_nbufs = br.count;
2849 zr->jpg_bufsize = br.size;
2851 if (jpg_fbuffer_alloc(zr))
2854 /* The next mmap will map the MJPEG buffers */
2855 zr->map_mjpeg_buffers = 1;
2857 if (copy_to_user(arg, &br, sizeof(br))) {
2863 case BUZIOC_QBUF_CAPT:
2867 if (copy_from_user((void *) &nb, (void *) arg, sizeof(int))) {
2870 IOCTL_DEBUG(printk("buz ioctl BUZIOC_QBUF_CAPT %d\n", nb));
2871 return jpg_qbuf(zr, nb, BUZ_MODE_MOTION_COMPRESS);
2874 case BUZIOC_QBUF_PLAY:
2878 if (copy_from_user((void *) &nb, (void *) arg, sizeof(int))) {
2881 IOCTL_DEBUG(printk("buz ioctl BUZIOC_QBUF_PLAY %d\n", nb));
2882 return jpg_qbuf(zr, nb, BUZ_MODE_MOTION_DECOMPRESS);
2887 struct zoran_sync bs;
2890 IOCTL_DEBUG(printk("buz ioctl BUZIOC_SYNC\n"));
2891 res = jpg_sync(zr, &bs);
2892 if (copy_to_user(arg, &bs, sizeof(bs))) {
2898 case BUZIOC_G_STATUS:
2900 struct zoran_status bs;
2901 int norm, input, status;
2903 if (zr->codec_mode != BUZ_MODE_IDLE) {
2906 if (copy_from_user(&bs, arg, sizeof(bs))) {
2909 IOCTL_DEBUG(printk("buz ioctl BUZIOC_G_STATUS\n"));
2921 /* Set video norm to VIDEO_MODE_AUTO */
2923 norm = VIDEO_MODE_AUTO;
2924 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &input);
2925 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &norm);
2927 /* sleep 1 second */
2929 schedule_timeout(HZ);
2931 /* Get status of video decoder */
2933 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_GET_STATUS, &status);
2934 bs.signal = (status & DECODER_STATUS_GOOD) ? 1 : 0;
2935 bs.norm = (status & DECODER_STATUS_NTSC) ? VIDEO_MODE_NTSC : VIDEO_MODE_PAL;
2936 bs.color = (status & DECODER_STATUS_COLOR) ? 1 : 0;
2938 /* restore previous input and norm */
2939 input = zr->params.input == 0 ? 3 : 7;
2940 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &input);
2941 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &zr->params.norm);
2943 if (copy_to_user(arg, &bs, sizeof(bs))) {
2950 return -ENOIOCTLCMD;
2958 * This maps the buffers to user space.
2960 * Depending on the state of zr->map_mjpeg_buffers
2961 * the V4L or the MJPEG buffers are mapped
2965 static int zoran_mmap(struct video_device *dev, const char *adr, unsigned long size)
2967 struct zoran *zr = (struct zoran *) dev;
2968 unsigned long start = (unsigned long) adr;
2969 unsigned long page, pos, todo, fraglen;
2972 if (zr->map_mjpeg_buffers) {
2973 /* Map the MJPEG buffers */
2975 if (!zr->jpg_buffers_allocated) {
2978 if (size > zr->jpg_nbufs * zr->jpg_bufsize) {
2982 for (i = 0; i < zr->jpg_nbufs; i++) {
2983 for (j = 0; j < zr->jpg_bufsize / PAGE_SIZE; j++) {
2984 fraglen = (zr->jpg_gbuf[i].frag_tab[2 * j + 1] & ~1) << 1;
2988 pos = (unsigned long) zr->jpg_gbuf[i].frag_tab[2 * j];
2989 page = virt_to_phys(bus_to_virt(pos)); /* should just be pos on i386 */
2990 if (remap_page_range(start, page, todo, PAGE_SHARED)) {
2991 printk(KERN_ERR "%s: zoran_mmap(V4L): remap_page_range failed\n", zr->name);
2998 if (zr->jpg_gbuf[i].frag_tab[2 * j + 1] & 1)
2999 break; /* was last fragment */
3005 /* Map the V4L buffers */
3007 if (size > v4l_nbufs * v4l_bufsize) {
3011 for (i = 0; i < v4l_nbufs; i++) {
3013 if (todo > v4l_bufsize)
3015 page = zr->v4l_gbuf[i].fbuffer_phys;
3016 DEBUG(printk("V4L remap page range %d 0x%x %d to 0x%x\n", i, page, todo, start));
3017 if (remap_page_range(start, page, todo, PAGE_SHARED)) {
3018 printk(KERN_ERR "%s: zoran_mmap(V4L): remap_page_range failed\n", zr->name);
3030 static struct video_device zoran_template =
3033 type: VID_TYPE_CAPTURE | VID_TYPE_OVERLAY | VID_TYPE_CLIPPING | VID_TYPE_FRAMERAM |
3034 VID_TYPE_SCALES | VID_TYPE_SUBCAPTURE,
3035 hardware: VID_HARDWARE_ZR36067,
3044 static int zr36057_init(int i)
3046 struct zoran *zr = &zoran[i];
3048 unsigned mem_needed;
3053 btwrite(0, ZR36057_SPGPPCR);
3056 /* default setup of all parameters which will persist beetween opens */
3060 init_waitqueue_head(&zr->v4l_capq);
3061 init_waitqueue_head(&zr->jpg_capq);
3063 zr->map_mjpeg_buffers = 0; /* Map V4L buffers by default */
3066 zr->jpg_bufsize = 0;
3067 zr->jpg_buffers_allocated = 0;
3069 zr->buffer_set = 0; /* Flag if frame buffer has been set */
3070 zr->buffer.base = (void *) vidmem;
3071 zr->buffer.width = 0;
3072 zr->buffer.height = 0;
3073 zr->buffer.depth = 0;
3074 zr->buffer.bytesperline = 0;
3076 zr->params.norm = default_norm ? 1 : 0; /* Avoid nonsense settings from user */
3077 zr->params.input = default_input ? 1 : 0; /* Avoid nonsense settings from user */
3078 zr->video_interlace = 0;
3080 /* Should the following be reset at every open ? */
3082 zr->picture.colour = 32768;
3083 zr->picture.brightness = 32768;
3084 zr->picture.hue = 32768;
3085 zr->picture.contrast = 32768;
3086 zr->picture.whiteness = 0;
3087 zr->picture.depth = 0;
3088 zr->picture.palette = 0;
3090 for (j = 0; j < VIDEO_MAX_FRAME; j++) {
3091 zr->v4l_gbuf[i].fbuffer = 0;
3092 zr->v4l_gbuf[i].fbuffer_phys = 0;
3093 zr->v4l_gbuf[i].fbuffer_bus = 0;
3098 /* default setup (will be repeated at every open) */
3100 zoran_open_init_params(zr);
3102 /* allocate memory *before* doing anything to the hardware in case allocation fails */
3104 /* STAT_COM table and overlay mask */
3106 mem_needed = (BUZ_NUM_STAT_COM + ((BUZ_MAX_WIDTH + 31) / 32) * BUZ_MAX_HEIGHT) * 4;
3107 mem = (unsigned long) kmalloc(mem_needed, GFP_KERNEL);
3111 memset((void *) mem, 0, mem_needed);
3113 zr->stat_com = (u32 *) mem;
3114 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
3115 zr->stat_com[j] = 1; /* mark as unavailable to zr36057 */
3117 zr->overlay_mask = (u32 *) (mem + BUZ_NUM_STAT_COM * 4);
3119 /* Initialize zr->jpg_gbuf */
3121 for (j = 0; j < BUZ_MAX_FRAME; j++) {
3122 zr->jpg_gbuf[j].frag_tab = 0;
3123 zr->jpg_gbuf[j].frag_tab_bus = 0;
3124 zr->jpg_gbuf[j].state = BUZ_STATE_USER;
3125 zr->jpg_gbuf[j].bs.frame = j;
3128 /* take zr36057 out of reset now */
3129 btwrite(ZR36057_SPGPPCR_SoftReset, ZR36057_SPGPPCR);
3132 /* stop all DMA processes */
3133 btwrite(ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
3134 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
3135 /* assert P_Reset */
3136 btwrite(0, ZR36057_JPC);
3142 /* set up GPIO direction */
3143 btwrite(ZR36057_SPGPPCR_SoftReset | 0, ZR36057_SPGPPCR);
3145 /* Set up guest bus timing - Guests 0..3 Tdur=12, Trec=3 */
3146 btwrite((GPIO_MASK << 24) | 0x8888, ZR36057_GPPGCR1);
3149 /* reset video decoder */
3156 /* reset JPEG codec */
3157 zr36060_sleep(zr, 0);
3162 /* display codec revision */
3163 if ((rev=zr36060_read_8(zr, 0x022)) == 0x33) {
3164 printk(KERN_INFO "%s: Zoran ZR36060 (rev %d)\n",
3165 zr->name, zr36060_read_8(zr, 0x023));
3167 printk(KERN_ERR "%s: Zoran ZR36060 not found (Rev=%d)\n", zr->name, rev);
3168 kfree((void *) zr->stat_com);
3174 // btwrite(btread(ZR36057_SPGPPCR)&~ZR36057_SPGPPCR_SoftReset , ZR36057_SPGPPCR);
3176 // btwrite(btread(ZR36057_SPGPPCR)|ZR36057_SPGPPCR_SoftReset , ZR36057_SPGPPCR);
3180 * Set up the GPIO direction
3182 btwrite(btread(ZR36057_SPGPPCR_SoftReset)|0 , ZR36057_SPGPPCR);
3183 /* Set up guest bus timing - Guests 0..2 Tdur=12, Trec=3 */
3184 btwrite(0xFF00F888, ZR36057_GPPGCR1);
3186 GPIO(zr, 5, 0); /* Analog video bypass */
3188 GPIO(zr, 0, 0); /* Reset 819 */
3190 GPIO(zr, 0, 1); /* 819 back */
3192 /* reset JPEG codec */
3193 zr36060_sleep(zr, 0);
3198 /* display codec revision */
3199 if ((rev=zr36060_read_8(zr, 0x022)) == 0x33) {
3200 printk(KERN_INFO "%s: Zoran ZR36060 (rev %d)\n",
3201 zr->name, zr36060_read_8(zr, 0x023));
3203 printk(KERN_ERR "%s: Zoran ZR36060 not found (rev=%d)\n", zr->name, rev);
3204 kfree((void *) zr->stat_com);
3210 memcpy(&zr->i2c, &zoran_i2c_bus_template, sizeof(struct i2c_bus));
3211 sprintf(zr->i2c.name, "zoran%u", zr->id);
3213 if (i2c_register_bus(&zr->i2c) < 0) {
3214 kfree((void *) zr->stat_com);
3218 * Now add the template and register the device unit.
3220 memcpy(&zr->video_dev, &zoran_template, sizeof(zoran_template));
3221 sprintf(zr->video_dev.name, "zoran%u", zr->id);
3222 if (video_register_device(&zr->video_dev, VFL_TYPE_GRABBER) < 0) {
3223 i2c_unregister_bus(&zr->i2c);
3224 kfree((void *) zr->stat_com);
3227 /* toggle JPEG codec sleep to sync PLL */
3228 zr36060_sleep(zr, 1);
3230 zr36060_sleep(zr, 0);
3233 /* Enable bus-mastering */
3234 pci_set_master(zr->pci_dev);
3236 j = zr->params.input == 0 ? 3 : 7;
3237 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &j);
3238 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &zr->params.norm);
3239 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_NORM, &zr->params.norm);
3241 /* set individual interrupt enables (without GIRQ0)
3242 but don't global enable until zoran_open() */
3244 btwrite(IRQ_MASK & ~ZR36057_ISR_GIRQ0, ZR36057_ICR);
3246 if(request_irq(zr->pci_dev->irq, zoran_irq,
3247 SA_SHIRQ | SA_INTERRUPT, zr->name, (void *) zr)<0)
3249 printk(KERN_ERR "%s: Can't assign irq.\n", zr->name);
3250 video_unregister_device(&zr->video_dev);
3251 i2c_unregister_bus(&zr->i2c);
3252 kfree((void *) zr->stat_com);
3255 zr->initialized = 1;
3261 static void release_zoran(void)
3267 for (i = 0; i < zoran_num; i++) {
3270 if (!zr->initialized)
3273 /* unregister i2c_bus */
3274 i2c_unregister_bus((&zr->i2c));
3276 /* disable PCI bus-mastering */
3277 pci_read_config_byte(zr->pci_dev, PCI_COMMAND, &command);
3278 command &= ~PCI_COMMAND_MASTER;
3279 pci_write_config_byte(zr->pci_dev, PCI_COMMAND, command);
3281 /* put chip into reset */
3282 btwrite(0, ZR36057_SPGPPCR);
3284 free_irq(zr->pci_dev->irq, zr);
3286 /* unmap and free memory */
3288 kfree((void *) zr->stat_com);
3290 iounmap(zr->zr36057_mem);
3292 video_unregister_device(&zr->video_dev);
3297 * Scan for a Buz card (actually for the PCI controller ZR36057),
3298 * request the irq and map the io memory
3301 static int find_zr36057(void)
3303 unsigned char latency;
3305 struct pci_dev *dev = NULL;
3309 while (zoran_num < BUZ_MAX
3310 && (dev = pci_find_device(PCI_VENDOR_ID_ZORAN, PCI_DEVICE_ID_ZORAN_36057, dev)) != NULL) {
3311 zr = &zoran[zoran_num];
3313 zr->zr36057_mem = NULL;
3315 sprintf(zr->name, "zoran%u", zr->id);
3317 spin_lock_init(&zr->lock);
3319 if (pci_enable_device(dev))
3322 zr->zr36057_adr = pci_resource_start(zr->pci_dev, 0);
3323 pci_read_config_byte(zr->pci_dev, PCI_CLASS_REVISION, &zr->revision);
3324 if (zr->revision < 2) {
3325 printk(KERN_INFO "%s: Zoran ZR36057 (rev %d) irq: %d, memory: 0x%08x.\n",
3326 zr->name, zr->revision, zr->pci_dev->irq, zr->zr36057_adr);
3328 unsigned short ss_vendor_id, ss_id;
3330 ss_vendor_id = zr->pci_dev->subsystem_vendor;
3331 ss_id = zr->pci_dev->subsystem_device;
3332 printk(KERN_INFO "%s: Zoran ZR36067 (rev %d) irq: %d, memory: 0x%08x\n",
3333 zr->name, zr->revision, zr->pci_dev->irq, zr->zr36057_adr);
3334 printk(KERN_INFO "%s: subsystem vendor=0x%04x id=0x%04x\n",
3335 zr->name, ss_vendor_id, ss_id);
3336 if(ss_vendor_id==0xFF10 && ss_id == 0xDE41)
3338 zr->board = BOARD_LML33;
3339 printk(KERN_INFO "%s: LML33 detected.\n", zr->name);
3343 zr->zr36057_mem = ioremap(zr->zr36057_adr, 0x1000);
3344 if (!zr->zr36057_mem) {
3345 printk(KERN_ERR "%s: ioremap failed\n", zr->name);
3346 /* XXX handle error */
3349 /* set PCI latency timer */
3350 pci_read_config_byte(zr->pci_dev, PCI_LATENCY_TIMER, &latency);
3351 if (latency != 48) {
3352 printk(KERN_INFO "%s: Changing PCI latency from %d to 48.\n", zr->name, latency);
3354 pci_write_config_byte(zr->pci_dev, PCI_LATENCY_TIMER, latency);
3359 printk(KERN_INFO "zoran: no cards found.\n");
3364 static void handle_chipset(void)
3366 if(pci_pci_problems&PCIPCI_FAIL)
3368 printk(KERN_WARNING "buz: This configuration is known to have PCI to PCI DMA problems\n");
3369 printk(KERN_WARNING "buz: You may not be able to use overlay mode.\n");
3373 if(pci_pci_problems&PCIPCI_TRITON)
3375 printk("buz: Enabling Triton support.\n");
3379 if(pci_pci_problems&PCIPCI_NATOMA)
3381 printk("buz: Enabling Natoma workaround.\n");
3387 int init_module(void)
3389 int init_zoran_cards(struct video_init *unused)
3395 printk(KERN_INFO "Zoran driver 1.00 (c) 1999 Rainer Johanni, Dave Perks.\n");
3397 /* Look for Buz cards */
3399 if (find_zr36057() <= 0) {
3402 printk(KERN_INFO"zoran: %d zoran card(s) found\n", zoran_num);
3408 /* check the parameters we have been given, adjust if necessary */
3412 if (v4l_nbufs > VIDEO_MAX_FRAME)
3413 v4l_nbufs = VIDEO_MAX_FRAME;
3414 /* The user specfies the in KB, we want them in byte (and page aligned) */
3415 v4l_bufsize = PAGE_ALIGN(v4l_bufsize * 1024);
3416 if (v4l_bufsize < 32768)
3417 v4l_bufsize = 32768;
3418 /* 2 MB is arbitrary but sufficient for the maximum possible images */
3419 if (v4l_bufsize > 2048 * 1024)
3420 v4l_bufsize = 2048 * 1024;
3422 printk(KERN_INFO "zoran: using %d V4L buffers of size %d KB\n", v4l_nbufs, v4l_bufsize >> 10);
3424 /* Use parameter for vidmem or try to find a video card */
3427 printk(KERN_INFO "zoran: Using supplied video memory base address @ 0x%lx\n", vidmem);
3430 /* check if we have a Triton or Natome chipset */
3434 /* take care of Natoma chipset and a revision 1 zr36057 */
3436 for (i = 0; i < zoran_num; i++) {
3437 if (natoma && zoran[i].revision <= 1) {
3438 zoran[i].need_contiguous = 1;
3439 printk(KERN_INFO "%s: ZR36057/Natome bug, max. buffer size is 128K\n", zoran[i].name);
3441 zoran[i].need_contiguous = 0;
3445 /* initialize the Buzs */
3447 /* We have to know which ones must be released if an error occurs */
3448 for (i = 0; i < zoran_num; i++)
3449 zoran[i].initialized = 0;
3451 for (i = 0; i < zoran_num; i++) {
3452 if (zr36057_init(i) < 0) {
3465 void cleanup_module(void)