Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / buz.c
blob299a4f8ca1dc54f0f197e3979abe59e1e2d800fd
1 #define MAX_KMALLOC_MEM (512*1024)
2 /*
3 buz - Iomega Buz driver version 1.0
5 Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
7 based on
9 buz.0.0.3 Copyright (C) 1998 Dave Perks <dperks@ibm.net>
11 and
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>
36 #include <linux/fs.h>
37 #include <linux/kernel.h>
38 #include <linux/major.h>
39 #include <linux/malloc.h>
40 #include <linux/mm.h>
41 #include <linux/pci.h>
42 #include <linux/signal.h>
43 #include <asm/io.h>
44 #include <asm/pgtable.h>
45 #include <asm/page.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>
59 #include "buz.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
68 BUZ
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.
78 SAA7111A
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!
87 mode 0 - N/C
88 mode 1 - S-Video Y
89 mode 2 - noise or something I don't know
90 mode 3 - Composite and S-Video C
91 mode 4 - N/C
92 mode 5 - S-Video (gain C independently selectable of gain Y)
93 mode 6 - N/C
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? */
160 #define BUZ_MAX 4
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)
182 int i, off;
183 unsigned char *mem;
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) {
190 /* Use kmalloc */
192 mem = (unsigned char *) kmalloc(v4l_bufsize, GFP_KERNEL);
193 if (mem == 0) {
194 printk(KERN_ERR "%s: kmalloc for V4L bufs failed\n", zr->name);
195 v4l_fbuffer_free(zr);
196 return -ENOBUFS;
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(MAP_NR(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)));
204 } else {
205 return -ENOBUFS;
209 return 0;
212 /* free the V4L grab buffers */
213 static void v4l_fbuffer_free(struct zoran *zr)
215 int i, off;
216 unsigned char *mem;
218 for (i = 0; i < v4l_nbufs; i++) {
219 if (!zr->v4l_gbuf[i].fbuffer)
220 continue;
222 mem = zr->v4l_gbuf[i].fbuffer;
223 for (off = 0; off < v4l_bufsize; off += PAGE_SIZE)
224 mem_map_unreserve(MAP_NR(mem + off));
225 kfree((void *) zr->v4l_gbuf[i].fbuffer);
226 zr->v4l_gbuf[i].fbuffer = NULL;
231 * Allocate the MJPEG grab buffers.
233 * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
234 * kmalloc is used to request a physically contiguous area,
235 * else we allocate the memory in framgents with get_free_page.
237 * If a Natoma chipset is present and this is a revision 1 zr36057,
238 * each MJPEG buffer needs to be physically contiguous.
239 * (RJ: This statement is from Dave Perks' original driver,
240 * I could never check it because I have a zr36067)
241 * The driver cares about this because it reduces the buffer
242 * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
244 * RJ: The contents grab buffers needs never be accessed in the driver.
245 * Therefore there is no need to allocate them with vmalloc in order
246 * to get a contiguous virtual memory space.
247 * I don't understand why many other drivers first allocate them with
248 * vmalloc (which uses internally also get_free_page, but delivers you
249 * virtual addresses) and then again have to make a lot of efforts
250 * to get the physical address.
254 static int jpg_fbuffer_alloc(struct zoran *zr)
256 int i, j, off, alloc_contig;
257 unsigned long mem;
259 /* Decide if we should alloc contiguous or fragmented memory */
260 /* This has to be identical in jpg_fbuffer_alloc and jpg_fbuffer_free */
262 alloc_contig = (zr->jpg_bufsize < MAX_KMALLOC_MEM);
264 for (i = 0; i < zr->jpg_nbufs; i++) {
265 if (zr->jpg_gbuf[i].frag_tab)
266 printk(KERN_WARNING "%s: jpg_fbuffer_alloc: buffer %d allready allocated ???\n", zr->name, i);
268 /* Allocate fragment table for this buffer */
270 mem = get_free_page(GFP_KERNEL);
271 if (mem == 0) {
272 printk(KERN_ERR "%s: jpg_fbuffer_alloc: get_free_page (frag_tab) failed for buffer %d\n", zr->name, i);
273 jpg_fbuffer_free(zr);
274 return -ENOBUFS;
276 memset((void *) mem, 0, PAGE_SIZE);
277 zr->jpg_gbuf[i].frag_tab = (u32 *) mem;
278 zr->jpg_gbuf[i].frag_tab_bus = virt_to_bus((void *) mem);
280 if (alloc_contig) {
281 mem = (unsigned long) kmalloc(zr->jpg_bufsize, GFP_KERNEL);
282 if (mem == 0) {
283 jpg_fbuffer_free(zr);
284 return -ENOBUFS;
286 zr->jpg_gbuf[i].frag_tab[0] = virt_to_bus((void *) mem);
287 zr->jpg_gbuf[i].frag_tab[1] = ((zr->jpg_bufsize / 4) << 1) | 1;
288 for (off = 0; off < zr->jpg_bufsize; off += PAGE_SIZE)
289 mem_map_reserve(MAP_NR(mem + off));
290 } else {
291 /* jpg_bufsize is alreay page aligned */
292 for (j = 0; j < zr->jpg_bufsize / PAGE_SIZE; j++) {
293 mem = get_free_page(GFP_KERNEL);
294 if (mem == 0) {
295 jpg_fbuffer_free(zr);
296 return -ENOBUFS;
298 zr->jpg_gbuf[i].frag_tab[2 * j] = virt_to_bus((void *) mem);
299 zr->jpg_gbuf[i].frag_tab[2 * j + 1] = (PAGE_SIZE / 4) << 1;
300 mem_map_reserve(MAP_NR(mem));
303 zr->jpg_gbuf[i].frag_tab[2 * j - 1] |= 1;
307 DEBUG(printk("jpg_fbuffer_alloc: %d KB allocated\n",
308 (zr->jpg_nbufs * zr->jpg_bufsize) >> 10));
309 zr->jpg_buffers_allocated = 1;
310 return 0;
313 /* free the MJPEG grab buffers */
314 static void jpg_fbuffer_free(struct zoran *zr)
316 int i, j, off, alloc_contig;
317 unsigned char *mem;
319 /* Decide if we should alloc contiguous or fragmented memory */
320 /* This has to be identical in jpg_fbuffer_alloc and jpg_fbuffer_free */
322 alloc_contig = (zr->jpg_bufsize < MAX_KMALLOC_MEM);
324 for (i = 0; i < zr->jpg_nbufs; i++) {
325 if (!zr->jpg_gbuf[i].frag_tab)
326 continue;
328 if (alloc_contig) {
329 if (zr->jpg_gbuf[i].frag_tab[0]) {
330 mem = (unsigned char *) bus_to_virt(zr->jpg_gbuf[i].frag_tab[0]);
331 for (off = 0; off < zr->jpg_bufsize; off += PAGE_SIZE)
332 mem_map_unreserve(MAP_NR(mem + off));
333 kfree((void *) mem);
334 zr->jpg_gbuf[i].frag_tab[0] = 0;
335 zr->jpg_gbuf[i].frag_tab[1] = 0;
337 } else {
338 for (j = 0; j < zr->jpg_bufsize / PAGE_SIZE; j++) {
339 if (!zr->jpg_gbuf[i].frag_tab[2 * j])
340 break;
341 mem_map_unreserve(MAP_NR(bus_to_virt(zr->jpg_gbuf[i].frag_tab[2 * j])));
342 free_page((unsigned long) bus_to_virt(zr->jpg_gbuf[i].frag_tab[2 * j]));
343 zr->jpg_gbuf[i].frag_tab[2 * j] = 0;
344 zr->jpg_gbuf[i].frag_tab[2 * j + 1] = 0;
348 free_page((unsigned long) zr->jpg_gbuf[i].frag_tab);
349 zr->jpg_gbuf[i].frag_tab = NULL;
351 zr->jpg_buffers_allocated = 0;
355 /* ----------------------------------------------------------------------- */
357 /* I2C functions */
359 #define I2C_DELAY 10
362 /* software I2C functions */
364 static void i2c_setlines(struct i2c_bus *bus, int ctrl, int data)
366 struct zoran *zr = (struct zoran *) bus->data;
367 btwrite((data << 1) | ctrl, ZR36057_I2CBR);
368 btread(ZR36057_I2CBR);
369 udelay(I2C_DELAY);
372 static int i2c_getdataline(struct i2c_bus *bus)
374 struct zoran *zr = (struct zoran *) bus->data;
375 return (btread(ZR36057_I2CBR) >> 1) & 1;
378 static void attach_inform(struct i2c_bus *bus, int id)
380 DEBUG(struct zoran *zr = (struct zoran *) bus->data);
381 DEBUG(printk(BUZ_DEBUG "-%u: i2c attach %02x\n", zr->id, id));
384 static void detach_inform(struct i2c_bus *bus, int id)
386 DEBUG(struct zoran *zr = (struct zoran *) bus->data);
387 DEBUG(printk(BUZ_DEBUG "-%u: i2c detach %02x\n", zr->id, id));
390 static struct i2c_bus zoran_i2c_bus_template =
392 "zr36057",
393 I2C_BUSID_BT848,
394 NULL,
396 SPIN_LOCK_UNLOCKED,
398 attach_inform,
399 detach_inform,
401 i2c_setlines,
402 i2c_getdataline,
403 NULL,
404 NULL,
408 /* ----------------------------------------------------------------------- */
410 static void GPIO(struct zoran *zr, unsigned bit, unsigned value)
412 u32 reg;
413 u32 mask;
415 mask = 1 << (24 + bit);
416 reg = btread(ZR36057_GPPGCR1) & ~mask;
417 if (value) {
418 reg |= mask;
420 btwrite(reg, ZR36057_GPPGCR1);
421 /* Stop any PCI posting on the GPIO bus */
422 btread(ZR36057_I2CBR);
427 * Set the registers for the size we have specified. Don't bother
428 * trying to understand this without the ZR36057 manual in front of
429 * you [AC].
431 * PS: The manual is free for download in .pdf format from
432 * www.zoran.com - nicely done those folks.
435 struct tvnorm {
436 u16 Wt, Wa, Ht, Ha, HStart, VStart;
439 static struct tvnorm tvnorms[] =
441 /* PAL-BDGHI */
442 {864, 720, 625, 576, 31, 16},
443 /* NTSC */
444 {858, 720, 525, 480, 21, 8},
446 #define TVNORMS (sizeof(tvnorms) / sizeof(tvnorm))
448 static int format2bpp(int format)
450 int bpp;
452 /* Determine the number of bytes per pixel for the video format requested */
454 switch (format) {
456 case VIDEO_PALETTE_YUV422:
457 bpp = 2;
458 break;
460 case VIDEO_PALETTE_RGB555:
461 bpp = 2;
462 break;
464 case VIDEO_PALETTE_RGB565:
465 bpp = 2;
466 break;
468 case VIDEO_PALETTE_RGB24:
469 bpp = 3;
470 break;
472 case VIDEO_PALETTE_RGB32:
473 bpp = 4;
474 break;
476 default:
477 bpp = 0;
480 return bpp;
484 * set geometry
486 static void zr36057_set_vfe(struct zoran *zr, int video_width, int video_height,
487 unsigned int video_format)
489 struct tvnorm *tvn;
490 unsigned HStart, HEnd, VStart, VEnd;
491 unsigned DispMode;
492 unsigned VidWinWid, VidWinHt;
493 unsigned hcrop1, hcrop2, vcrop1, vcrop2;
494 unsigned Wa, We, Ha, He;
495 unsigned X, Y, HorDcm, VerDcm;
496 u32 reg;
497 unsigned mask_line_size;
499 if (zr->params.norm < 0 || zr->params.norm > 1) {
500 printk(KERN_ERR "%s: set_vfe: video_norm = %d not valid\n", zr->name, zr->params.norm);
501 return;
503 if (video_width < BUZ_MIN_WIDTH || video_height < BUZ_MIN_HEIGHT) {
504 printk(KERN_ERR "%s: set_vfe: w=%d h=%d not valid\n", zr->name, video_width, video_height);
505 return;
507 tvn = &tvnorms[zr->params.norm];
509 Wa = tvn->Wa;
510 Ha = tvn->Ha;
512 /* if window has more than half of active height,
513 switch on interlacing - we want the full information */
515 zr->video_interlace = (video_height > Ha / 2);
517 /**** zr36057 ****/
519 /* horizontal */
520 VidWinWid = video_width;
521 X = (VidWinWid * 64 + tvn->Wa - 1) / tvn->Wa;
522 We = (VidWinWid * 64) / X;
523 HorDcm = 64 - X;
524 hcrop1 = 2 * ((tvn->Wa - We) / 4);
525 hcrop2 = tvn->Wa - We - hcrop1;
526 HStart = tvn->HStart | 1;
527 HEnd = HStart + tvn->Wa - 1;
528 HStart += hcrop1;
529 HEnd -= hcrop2;
530 reg = ((HStart & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HStart)
531 | ((HEnd & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HEnd);
532 reg |= ZR36057_VFEHCR_HSPol;
533 btwrite(reg, ZR36057_VFEHCR);
535 /* Vertical */
536 DispMode = !zr->video_interlace;
537 VidWinHt = DispMode ? video_height : video_height / 2;
538 Y = (VidWinHt * 64 * 2 + tvn->Ha - 1) / tvn->Ha;
539 He = (VidWinHt * 64) / Y;
540 VerDcm = 64 - Y;
541 vcrop1 = (tvn->Ha / 2 - He) / 2;
542 vcrop2 = tvn->Ha / 2 - He - vcrop1;
543 VStart = tvn->VStart;
544 VEnd = VStart + tvn->Ha / 2 - 1;
545 VStart += vcrop1;
546 VEnd -= vcrop2;
547 reg = ((VStart & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VStart)
548 | ((VEnd & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VEnd);
549 reg |= ZR36057_VFEVCR_VSPol;
550 btwrite(reg, ZR36057_VFEVCR);
552 /* scaler and pixel format */
553 reg = 0 // ZR36057_VFESPFR_ExtFl /* Trying to live without ExtFl */
554 | (HorDcm << ZR36057_VFESPFR_HorDcm)
555 | (VerDcm << ZR36057_VFESPFR_VerDcm)
556 | (DispMode << ZR36057_VFESPFR_DispMode)
557 | ZR36057_VFESPFR_LittleEndian;
558 /* RJ: I don't know, why the following has to be the opposite
559 of the corresponding ZR36060 setting, but only this way
560 we get the correct colors when uncompressing to the screen */
561 reg |= ZR36057_VFESPFR_VCLKPol;
562 /* RJ: Don't know if that is needed for NTSC also */
563 reg |= ZR36057_VFESPFR_TopField;
564 switch (video_format) {
566 case VIDEO_PALETTE_YUV422:
567 reg |= ZR36057_VFESPFR_YUV422;
568 break;
570 case VIDEO_PALETTE_RGB555:
571 reg |= ZR36057_VFESPFR_RGB555 | ZR36057_VFESPFR_ErrDif;
572 break;
574 case VIDEO_PALETTE_RGB565:
575 reg |= ZR36057_VFESPFR_RGB565 | ZR36057_VFESPFR_ErrDif;
576 break;
578 case VIDEO_PALETTE_RGB24:
579 reg |= ZR36057_VFESPFR_RGB888 | ZR36057_VFESPFR_Pack24;
580 break;
582 case VIDEO_PALETTE_RGB32:
583 reg |= ZR36057_VFESPFR_RGB888;
584 break;
586 default:
587 printk(KERN_INFO "%s: Unknown color_fmt=%x\n", zr->name, video_format);
588 return;
591 if (HorDcm >= 48) {
592 reg |= 3 << ZR36057_VFESPFR_HFilter; /* 5 tap filter */
593 } else if (HorDcm >= 32) {
594 reg |= 2 << ZR36057_VFESPFR_HFilter; /* 4 tap filter */
595 } else if (HorDcm >= 16) {
596 reg |= 1 << ZR36057_VFESPFR_HFilter; /* 3 tap filter */
598 btwrite(reg, ZR36057_VFESPFR);
600 /* display configuration */
602 reg = (16 << ZR36057_VDCR_MinPix)
603 | (VidWinHt << ZR36057_VDCR_VidWinHt)
604 | (VidWinWid << ZR36057_VDCR_VidWinWid);
605 if (triton)
606 reg &= ~ZR36057_VDCR_Triton;
607 else
608 reg |= ZR36057_VDCR_Triton;
609 btwrite(reg, ZR36057_VDCR);
611 /* Write overlay clipping mask data, but don't enable overlay clipping */
612 /* RJ: since this makes only sense on the screen, we use
613 zr->window.width instead of video_width */
615 mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
616 reg = virt_to_bus(zr->overlay_mask);
617 btwrite(reg, ZR36057_MMTR);
618 reg = virt_to_bus(zr->overlay_mask + mask_line_size);
619 btwrite(reg, ZR36057_MMBR);
620 reg = mask_line_size - (zr->window.width + 31) / 32;
621 if (DispMode == 0)
622 reg += mask_line_size;
623 reg <<= ZR36057_OCR_MaskStride;
624 btwrite(reg, ZR36057_OCR);
629 * Switch overlay on or off
632 static void zr36057_overlay(struct zoran *zr, int on)
634 int fmt, bpp;
635 u32 reg;
637 if (on) {
638 /* do the necessary settings ... */
640 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR); /* switch it off first */
642 switch (zr->buffer.depth) {
643 case 15:
644 fmt = VIDEO_PALETTE_RGB555;
645 bpp = 2;
646 break;
647 case 16:
648 fmt = VIDEO_PALETTE_RGB565;
649 bpp = 2;
650 break;
651 case 24:
652 fmt = VIDEO_PALETTE_RGB24;
653 bpp = 3;
654 break;
655 case 32:
656 fmt = VIDEO_PALETTE_RGB32;
657 bpp = 4;
658 break;
659 default:
660 fmt = 0;
661 bpp = 0;
664 zr36057_set_vfe(zr, zr->window.width, zr->window.height, fmt);
666 /* Start and length of each line MUST be 4-byte aligned.
667 This should be allready checked before the call to this routine.
668 All error messages are internal driver checking only! */
670 /* video display top and bottom registers */
672 reg = (u32) zr->buffer.base
673 + zr->window.x * bpp
674 + zr->window.y * zr->buffer.bytesperline;
675 btwrite(reg, ZR36057_VDTR);
676 if (reg & 3)
677 printk(KERN_ERR "%s: zr36057_overlay: video_address not aligned\n", zr->name);
678 if (zr->video_interlace)
679 reg += zr->buffer.bytesperline;
680 btwrite(reg, ZR36057_VDBR);
682 /* video stride, status, and frame grab register */
684 reg = zr->buffer.bytesperline - zr->window.width * bpp;
685 if (zr->video_interlace)
686 reg += zr->buffer.bytesperline;
687 if (reg & 3)
688 printk(KERN_ERR "%s: zr36057_overlay: video_stride not aligned\n", zr->name);
689 reg = (reg << ZR36057_VSSFGR_DispStride);
690 reg |= ZR36057_VSSFGR_VidOvf; /* clear overflow status */
691 btwrite(reg, ZR36057_VSSFGR);
693 /* Set overlay clipping */
695 if (zr->window.clipcount)
696 btor(ZR36057_OCR_OvlEnable, ZR36057_OCR);
698 /* ... and switch it on */
700 btor(ZR36057_VDCR_VidEn, ZR36057_VDCR);
701 } else {
702 /* Switch it off */
704 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
709 * The overlay mask has one bit for each pixel on a scan line,
710 * and the maximum window size is BUZ_MAX_WIDTH * BUZ_MAX_HEIGHT pixels.
712 static void write_overlay_mask(struct zoran *zr, struct video_clip *vp, int count)
714 unsigned mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
715 u32 *mask;
716 int x, y, width, height;
717 unsigned i, j, k;
718 u32 reg;
720 /* fill mask with one bits */
721 memset(zr->overlay_mask, ~0, mask_line_size * 4 * BUZ_MAX_HEIGHT);
722 reg = 0;
724 for (i = 0; i < count; ++i) {
725 /* pick up local copy of clip */
726 x = vp[i].x;
727 y = vp[i].y;
728 width = vp[i].width;
729 height = vp[i].height;
731 /* trim clips that extend beyond the window */
732 if (x < 0) {
733 width += x;
734 x = 0;
736 if (y < 0) {
737 height += y;
738 y = 0;
740 if (x + width > zr->window.width) {
741 width = zr->window.width - x;
743 if (y + height > zr->window.height) {
744 height = zr->window.height - y;
746 /* ignore degenerate clips */
747 if (height <= 0) {
748 continue;
750 if (width <= 0) {
751 continue;
753 /* apply clip for each scan line */
754 for (j = 0; j < height; ++j) {
755 /* reset bit for each pixel */
756 /* this can be optimized later if need be */
757 mask = zr->overlay_mask + (y + j) * mask_line_size;
758 for (k = 0; k < width; ++k) {
759 mask[(x + k) / 32] &= ~((u32) 1 << (x + k) % 32);
765 /* Enable/Disable uncompressed memory grabbing of the 36057 */
767 static void zr36057_set_memgrab(struct zoran *zr, int mode)
769 if (mode) {
770 if (btread(ZR36057_VSSFGR) & (ZR36057_VSSFGR_SnapShot | ZR36057_VSSFGR_FrameGrab))
771 printk(KERN_WARNING "%s: zr36057_set_memgrab_on with SnapShot or FrameGrab on ???\n", zr->name);
773 /* switch on VSync interrupts */
775 btwrite(IRQ_MASK, ZR36057_ISR); // Clear Interrupts
777 btor(ZR36057_ICR_GIRQ0, ZR36057_ICR);
779 /* enable SnapShot */
781 btor(ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
783 /* Set zr36057 video front end and enable video */
785 #ifdef XAWTV_HACK
786 zr36057_set_vfe(zr, zr->gwidth > 720 ? 720 : zr->gwidth, zr->gheight, zr->gformat);
787 #else
788 zr36057_set_vfe(zr, zr->gwidth, zr->gheight, zr->gformat);
789 #endif
791 zr->v4l_memgrab_active = 1;
792 } else {
793 zr->v4l_memgrab_active = 0;
795 /* switch off VSync interrupts */
797 btand(~ZR36057_ICR_GIRQ0, ZR36057_ICR);
799 /* reenable grabbing to screen if it was running */
801 if (zr->v4l_overlay_active) {
802 zr36057_overlay(zr, 1);
803 } else {
804 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
805 btand(~ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
810 static int wait_grab_pending(struct zoran *zr)
812 unsigned long flags;
814 /* wait until all pending grabs are finished */
816 if (!zr->v4l_memgrab_active)
817 return 0;
819 while (zr->v4l_pend_tail != zr->v4l_pend_head) {
820 interruptible_sleep_on(&zr->v4l_capq);
821 if (signal_pending(current))
822 return -ERESTARTSYS;
825 spin_lock_irqsave(&zr->lock, flags);
826 zr36057_set_memgrab(zr, 0);
827 spin_unlock_irqrestore(&zr->lock, flags);
829 return 0;
833 * V4L Buffer grabbing
836 static int v4l_grab(struct zoran *zr, struct video_mmap *mp)
838 unsigned long flags;
839 int res, bpp;
842 * There is a long list of limitations to what is allowed to be grabbed
843 * We don't output error messages her, since some programs (e.g. xawtv)
844 * just try several settings to find out what is valid or not.
847 /* No grabbing outside the buffer range! */
849 if (mp->frame >= v4l_nbufs || mp->frame < 0)
850 return -EINVAL;
852 /* Check size and format of the grab wanted */
854 if (mp->height < BUZ_MIN_HEIGHT || mp->width < BUZ_MIN_WIDTH)
855 return -EINVAL;
856 if (mp->height > BUZ_MAX_HEIGHT || mp->width > BUZ_MAX_WIDTH)
857 return -EINVAL;
859 bpp = format2bpp(mp->format);
860 if (bpp == 0)
861 return -EINVAL;
863 /* Check against available buffer size */
865 if (mp->height * mp->width * bpp > v4l_bufsize)
866 return -EINVAL;
868 /* The video front end needs 4-byte alinged line sizes */
870 if ((bpp == 2 && (mp->width & 1)) || (bpp == 3 && (mp->width & 3)))
871 return -EINVAL;
874 * To minimize the time spent in the IRQ routine, we avoid setting up
875 * the video front end there.
876 * If this grab has different parameters from a running streaming capture
877 * we stop the streaming capture and start it over again.
880 if (zr->v4l_memgrab_active &&
881 (zr->gwidth != mp->width || zr->gheight != mp->height || zr->gformat != mp->format)) {
882 res = wait_grab_pending(zr);
883 if (res)
884 return res;
886 zr->gwidth = mp->width;
887 zr->gheight = mp->height;
888 zr->gformat = mp->format;
889 zr->gbpl = bpp * zr->gwidth;
892 spin_lock_irqsave(&zr->lock, flags);
894 /* make sure a grab isn't going on currently with this buffer */
896 switch (zr->v4l_gbuf[mp->frame].state) {
898 default:
899 case BUZ_STATE_PEND:
900 res = -EBUSY; /* what are you doing? */
901 break;
903 case BUZ_STATE_USER:
904 case BUZ_STATE_DONE:
905 /* since there is at least one unused buffer there's room for at least one more pend[] entry */
906 zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] = mp->frame;
907 zr->v4l_gbuf[mp->frame].state = BUZ_STATE_PEND;
908 res = 0;
909 break;
913 /* put the 36057 into frame grabbing mode */
915 if (!res && !zr->v4l_memgrab_active)
916 zr36057_set_memgrab(zr, 1);
918 spin_unlock_irqrestore(&zr->lock, flags);
920 return res;
924 * Sync on a V4L buffer
927 static int v4l_sync(struct zoran *zr, int frame)
929 unsigned long flags;
932 /* check passed-in frame number */
933 if (frame >= v4l_nbufs || frame < 0) {
934 printk(KERN_ERR "%s: v4l_sync: frame %d is invalid\n", zr->name, frame);
935 return -EINVAL;
937 /* Check if is buffer was queued at all */
939 if (zr->v4l_gbuf[frame].state == BUZ_STATE_USER) {
940 // printk(KERN_ERR "%s: v4l_sync: Trying to sync on a buffer which was not queued?\n", zr->name);
941 return -EINVAL;
943 /* wait on this buffer to get ready */
945 while (zr->v4l_gbuf[frame].state == BUZ_STATE_PEND) {
946 interruptible_sleep_on(&zr->v4l_capq);
947 if (signal_pending(current))
948 return -ERESTARTSYS;
951 /* buffer should now be in BUZ_STATE_DONE */
953 if (zr->v4l_gbuf[frame].state != BUZ_STATE_DONE)
954 printk(KERN_ERR "%s: v4l_sync - internal error\n", zr->name);
956 /* Check if streaming capture has finished */
958 spin_lock_irqsave(&zr->lock, flags);
960 if (zr->v4l_pend_tail == zr->v4l_pend_head)
961 zr36057_set_memgrab(zr, 0);
963 spin_unlock_irqrestore(&zr->lock, flags);
965 return 0;
967 /*****************************************************************************
969 * Set up the Buz-specific MJPEG part *
971 *****************************************************************************/
974 * Wait til post office is no longer busy
977 static int post_office_wait(struct zoran *zr)
979 u32 por;
980 u32 ct=0;
982 while (((por = btread(ZR36057_POR)) & (ZR36057_POR_POPen | ZR36057_POR_POTime)) == ZR36057_POR_POPen) {
983 ct++;
984 if(ct>100000)
986 printk(KERN_ERR "%s: timeout on post office.\n", zr->name);
987 return -1;
989 /* wait for something to happen */
991 if ((por & ZR36057_POR_POPen) != 0) {
992 printk(KERN_WARNING "%s: pop pending %08x\n", zr->name, por);
993 return -1;
995 if ((por & (ZR36057_POR_POTime | ZR36057_POR_POPen)) != 0) {
996 printk(KERN_WARNING "%s: pop timeout %08x\n", zr->name, por);
997 return -1;
999 return 0;
1002 static int post_office_write(struct zoran *zr, unsigned guest, unsigned reg, unsigned value)
1004 u32 por;
1006 post_office_wait(zr);
1007 por = ZR36057_POR_PODir | ZR36057_POR_POTime | ((guest & 7) << 20) | ((reg & 7) << 16) | (value & 0xFF);
1008 btwrite(por, ZR36057_POR);
1009 return post_office_wait(zr);
1012 static int post_office_read(struct zoran *zr, unsigned guest, unsigned reg)
1014 u32 por;
1016 post_office_wait(zr);
1017 por = ZR36057_POR_POTime | ((guest & 7) << 20) | ((reg & 7) << 16);
1018 btwrite(por, ZR36057_POR);
1019 if (post_office_wait(zr) < 0) {
1020 return -1;
1022 return btread(ZR36057_POR) & 0xFF;
1025 static int zr36060_write_8(struct zoran *zr, unsigned reg, unsigned val)
1027 if (post_office_wait(zr)
1028 || post_office_write(zr, 0, 1, reg >> 8)
1029 || post_office_write(zr, 0, 2, reg)) {
1030 return -1;
1032 return post_office_write(zr, 0, 3, val);
1035 static int zr36060_write_16(struct zoran *zr, unsigned reg, unsigned val)
1037 if (zr36060_write_8(zr, reg + 0, val >> 8)) {
1038 return -1;
1040 return zr36060_write_8(zr, reg + 1, val >> 0);
1043 static int zr36060_write_24(struct zoran *zr, unsigned reg, unsigned val)
1045 if (zr36060_write_8(zr, reg + 0, val >> 16)) {
1046 return -1;
1048 return zr36060_write_16(zr, reg + 1, val >> 0);
1051 static int zr36060_write_32(struct zoran *zr, unsigned reg, unsigned val)
1053 if (zr36060_write_16(zr, reg + 0, val >> 16)) {
1054 return -1;
1056 return zr36060_write_16(zr, reg + 2, val >> 0);
1059 static u32 zr36060_read_8(struct zoran *zr, unsigned reg)
1061 if (post_office_wait(zr)
1062 || post_office_write(zr, 0, 1, reg >> 8)
1063 || post_office_write(zr, 0, 2, reg)) {
1064 return -1;
1066 return post_office_read(zr, 0, 3) & 0xFF;
1069 static int zr36060_reset(struct zoran *zr)
1071 return post_office_write(zr, 3, 0, 0);
1074 static void zr36060_sleep(struct zoran *zr, int sleep)
1076 GPIO(zr, 1, !sleep);
1080 static void zr36060_set_jpg(struct zoran *zr, enum zoran_codec_mode mode)
1082 struct tvnorm *tvn;
1083 u32 reg;
1084 int size;
1086 reg = (1 << 0) /* CodeMstr */
1087 |(0 << 2) /* CFIS=0 */
1088 |(0 << 6) /* Endian=0 */
1089 |(0 << 7); /* Code16=0 */
1090 zr36060_write_8(zr, 0x002, reg);
1092 switch (mode) {
1094 case BUZ_MODE_MOTION_DECOMPRESS:
1095 case BUZ_MODE_STILL_DECOMPRESS:
1096 reg = 0x00; /* Codec mode = decompression */
1097 break;
1099 case BUZ_MODE_MOTION_COMPRESS:
1100 case BUZ_MODE_STILL_COMPRESS:
1101 default:
1102 reg = 0xa4; /* Codec mode = compression with variable scale factor */
1103 break;
1106 zr36060_write_8(zr, 0x003, reg);
1108 reg = 0x00; /* reserved, mbz */
1109 zr36060_write_8(zr, 0x004, reg);
1111 reg = 0xff; /* 510 bits/block */
1112 zr36060_write_8(zr, 0x005, reg);
1114 /* JPEG markers */
1115 reg = (zr->params.jpeg_markers) & 0x38; /* DRI, DQT, DHT */
1116 if (zr->params.COM_len)
1117 reg |= JPEG_MARKER_COM;
1118 if (zr->params.APP_len)
1119 reg |= JPEG_MARKER_APP;
1120 zr36060_write_8(zr, 0x006, reg);
1122 reg = (0 << 3) /* DATERR=0 */
1123 |(0 << 2) /* END=0 */
1124 |(0 << 1) /* EOI=0 */
1125 |(0 << 0); /* EOAV=0 */
1126 zr36060_write_8(zr, 0x007, reg);
1128 /* code volume */
1130 /* Target field size in pixels: */
1131 tvn = &tvnorms[zr->params.norm];
1132 size = (tvn->Ha / 2) * (tvn->Wa) / (zr->params.HorDcm) / (zr->params.VerDcm);
1134 /* Target compressed field size in bits: */
1135 size = size * 16; /* uncompressed size in bits */
1136 size = size * zr->params.quality / 400; /* quality = 100 is a compression ratio 1:4 */
1138 /* Lower limit (arbitrary, 1 KB) */
1139 if (size < 8192)
1140 size = 8192;
1142 /* Upper limit: 7/8 of the code buffers */
1143 if (size * zr->params.field_per_buff > zr->jpg_bufsize * 7)
1144 size = zr->jpg_bufsize * 7 / zr->params.field_per_buff;
1146 reg = size;
1147 zr36060_write_32(zr, 0x009, reg);
1149 /* how do we set initial SF as a function of quality parameter? */
1150 reg = 0x0100; /* SF=1.0 */
1151 zr36060_write_16(zr, 0x011, reg);
1153 reg = 0x00ffffff; /* AF=max */
1154 zr36060_write_24(zr, 0x013, reg);
1156 reg = 0x0000; /* test */
1157 zr36060_write_16(zr, 0x024, reg);
1160 static void zr36060_set_video(struct zoran *zr, enum zoran_codec_mode mode)
1162 struct tvnorm *tvn;
1163 u32 reg;
1165 reg = (0 << 7) /* Video8=0 */
1166 |(0 << 6) /* Range=0 */
1167 |(0 << 3) /* FlDet=0 */
1168 |(1 << 2) /* FlVedge=1 */
1169 |(0 << 1) /* FlExt=0 */
1170 |(0 << 0); /* SyncMstr=0 */
1172 /* According to ZR36067 documentation, FlDet should correspond
1173 to the odd_even flag of the ZR36067 */
1174 if (zr->params.odd_even)
1175 reg |= (1 << 3);
1177 if (mode != BUZ_MODE_STILL_DECOMPRESS) {
1178 /* limit pixels to range 16..235 as per CCIR-601 */
1179 reg |= (1 << 6); /* Range=1 */
1181 zr36060_write_8(zr, 0x030, reg);
1183 reg = (0 << 7) /* VCLKPol=0 */
1184 |(0 << 6) /* PValPol=0 */
1185 |(1 << 5) /* PoePol=1 */
1186 |(0 << 4) /* SImgPol=0 */
1187 |(0 << 3) /* BLPol=0 */
1188 |(0 << 2) /* FlPol=0 */
1189 |(0 << 1) /* HSPol=0, sync on falling edge */
1190 |(1 << 0); /* VSPol=1 */
1191 zr36060_write_8(zr, 0x031, reg);
1193 switch (zr->params.HorDcm) {
1194 default:
1195 case 1:
1196 reg = (0 << 0);
1197 break; /* HScale = 0 */
1199 case 2:
1200 reg = (1 << 0);
1201 break; /* HScale = 1 */
1203 case 4:
1204 reg = (2 << 0);
1205 break; /* HScale = 2 */
1207 if (zr->params.VerDcm == 2)
1208 reg |= (1 << 2);
1209 zr36060_write_8(zr, 0x032, reg);
1211 reg = 0x80; /* BackY */
1212 zr36060_write_8(zr, 0x033, reg);
1214 reg = 0xe0; /* BackU */
1215 zr36060_write_8(zr, 0x034, reg);
1217 reg = 0xe0; /* BackV */
1218 zr36060_write_8(zr, 0x035, reg);
1220 /* sync generator */
1222 tvn = &tvnorms[zr->params.norm];
1224 reg = tvn->Ht - 1; /* Vtotal */
1225 zr36060_write_16(zr, 0x036, reg);
1227 reg = tvn->Wt - 1; /* Htotal */
1228 zr36060_write_16(zr, 0x038, reg);
1230 reg = 6 - 1; /* VsyncSize */
1231 zr36060_write_8(zr, 0x03a, reg);
1233 reg = 100 - 1; /* HsyncSize */
1234 zr36060_write_8(zr, 0x03b, reg);
1236 reg = tvn->VStart - 1; /* BVstart */
1237 zr36060_write_8(zr, 0x03c, reg);
1239 reg += tvn->Ha / 2; /* BVend */
1240 zr36060_write_16(zr, 0x03e, reg);
1242 reg = tvn->HStart - 1; /* BHstart */
1243 zr36060_write_8(zr, 0x03d, reg);
1245 reg += tvn->Wa; /* BHend */
1246 zr36060_write_16(zr, 0x040, reg);
1248 /* active area */
1249 reg = zr->params.img_y + tvn->VStart; /* Vstart */
1250 zr36060_write_16(zr, 0x042, reg);
1252 reg += zr->params.img_height; /* Vend */
1253 zr36060_write_16(zr, 0x044, reg);
1255 reg = zr->params.img_x + tvn->HStart; /* Hstart */
1256 zr36060_write_16(zr, 0x046, reg);
1258 reg += zr->params.img_width; /* Hend */
1259 zr36060_write_16(zr, 0x048, reg);
1261 /* subimage area */
1262 reg = zr->params.img_y + tvn->VStart; /* SVstart */
1263 zr36060_write_16(zr, 0x04a, reg);
1265 reg += zr->params.img_height; /* SVend */
1266 zr36060_write_16(zr, 0x04c, reg);
1268 reg = zr->params.img_x + tvn->HStart; /* SHstart */
1269 zr36060_write_16(zr, 0x04e, reg);
1271 reg += zr->params.img_width; /* SHend */
1272 zr36060_write_16(zr, 0x050, reg);
1275 static void zr36060_set_jpg_SOF(struct zoran *zr)
1277 u32 reg;
1280 reg = 0xffc0; /* SOF marker */
1281 zr36060_write_16(zr, 0x060, reg);
1283 reg = 17; /* SOF length */
1284 zr36060_write_16(zr, 0x062, reg);
1286 reg = 8; /* precision 8 bits */
1287 zr36060_write_8(zr, 0x064, reg);
1289 reg = zr->params.img_height / zr->params.VerDcm; /* image height */
1290 zr36060_write_16(zr, 0x065, reg);
1292 reg = zr->params.img_width / zr->params.HorDcm; /* image width */
1293 zr36060_write_16(zr, 0x067, reg);
1295 reg = 3; /* 3 color components */
1296 zr36060_write_8(zr, 0x069, reg);
1298 reg = 0x002100; /* Y component */
1299 zr36060_write_24(zr, 0x06a, reg);
1301 reg = 0x011101; /* U component */
1302 zr36060_write_24(zr, 0x06d, reg);
1304 reg = 0x021101; /* V component */
1305 zr36060_write_24(zr, 0x070, reg);
1308 static void zr36060_set_jpg_SOS(struct zoran *zr)
1310 u32 reg;
1313 reg = 0xffda; /* SOS marker */
1314 zr36060_write_16(zr, 0x07a, reg);
1316 reg = 12; /* SOS length */
1317 zr36060_write_16(zr, 0x07c, reg);
1319 reg = 3; /* 3 color components */
1320 zr36060_write_8(zr, 0x07e, reg);
1322 reg = 0x0000; /* Y component */
1323 zr36060_write_16(zr, 0x07f, reg);
1325 reg = 0x0111; /* U component */
1326 zr36060_write_16(zr, 0x081, reg);
1328 reg = 0x0211; /* V component */
1329 zr36060_write_16(zr, 0x083, reg);
1331 reg = 0x003f00; /* Start, end spectral scans */
1332 zr36060_write_24(zr, 0x085, reg);
1335 static void zr36060_set_jpg_DRI(struct zoran *zr)
1337 u32 reg;
1340 reg = 0xffdd; /* DRI marker */
1341 zr36060_write_16(zr, 0x0c0, reg);
1343 reg = 4; /* DRI length */
1344 zr36060_write_16(zr, 0x0c2, reg);
1346 reg = 8; /* length in MCUs */
1347 zr36060_write_16(zr, 0x0c4, reg);
1350 static void zr36060_set_jpg_DQT(struct zoran *zr)
1352 unsigned i;
1353 unsigned adr;
1354 static const u8 dqt[] =
1356 0xff, 0xdb, /* DHT marker */
1357 0x00, 0x84, /* DHT length */
1358 0x00, /* table ID 0 */
1359 0x10, 0x0b, 0x0c, 0x0e, 0x0c, 0x0a, 0x10, 0x0e,
1360 0x0d, 0x0e, 0x12, 0x11, 0x10, 0x13, 0x18, 0x28,
1361 0x1a, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23, 0x25,
1362 0x1d, 0x28, 0x3a, 0x33, 0x3d, 0x3c, 0x39, 0x33,
1363 0x38, 0x37, 0x40, 0x48, 0x5c, 0x4e, 0x40, 0x44,
1364 0x57, 0x45, 0x37, 0x38, 0x50, 0x6d, 0x51, 0x57,
1365 0x5f, 0x62, 0x67, 0x68, 0x67, 0x3e, 0x4d, 0x71,
1366 0x79, 0x70, 0x64, 0x78, 0x5c, 0x65, 0x67, 0x63,
1367 0x01, /* table ID 1 */
1368 0x11, 0x12, 0x12, 0x18, 0x15, 0x18, 0x2f, 0x1a,
1369 0x1a, 0x2f, 0x63, 0x42, 0x38, 0x42, 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,
1375 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63
1378 /* write fixed quantitization tables */
1379 adr = 0x0cc;
1380 for (i = 0; i < sizeof(dqt); ++i) {
1381 zr36060_write_8(zr, adr++, dqt[i]);
1385 static void zr36060_set_jpg_DHT(struct zoran *zr)
1387 unsigned i;
1388 unsigned adr;
1389 static const u8 dht[] =
1391 0xff, 0xc4, /* DHT marker */
1392 0x01, 0xa2, /* DHT length */
1393 0x00, /* table class 0, ID 0 */
1394 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, /* # codes of length 1..8 */
1395 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* # codes of length 8..16 */
1396 0x00, /* values for codes of length 2 */
1397 0x01, 0x02, 0x03, 0x04, 0x05, /* values for codes of length 3 */
1398 0x06, /* values for codes of length 4 */
1399 0x07, /* values for codes of length 5 */
1400 0x08, /* values for codes of length 6 */
1401 0x09, /* values for codes of length 7 */
1402 0x0a, /* values for codes of length 8 */
1403 0x0b, /* values for codes of length 9 */
1404 0x01, /* table class 0, ID 1 */
1405 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* # codes of length 1..8 */
1406 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, /* # codes of length 9..16 */
1407 0x00, 0x01, 0x02, /* values for codes of length 2 */
1408 0x03, /* values for codes of length 3 */
1409 0x04, /* values for codes of length 4 */
1410 0x05, /* values for codes of length 5 */
1411 0x06, /* values for codes of length 6 */
1412 0x07, /* values for codes of length 7 */
1413 0x08, /* values for codes of length 8 */
1414 0x09, /* values for codes of length 9 */
1415 0x0a, /* values for codes of length 10 */
1416 0x0b, /* values for codes of length 11 */
1417 0x10,
1418 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
1419 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d,
1420 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
1421 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
1422 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
1423 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
1424 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
1425 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
1426 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
1427 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
1428 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
1429 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
1430 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
1431 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
1432 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
1433 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
1434 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
1435 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
1436 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
1437 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
1438 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
1439 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
1440 0xf9, 0xfa, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04,
1441 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00,
1442 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11,
1443 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51,
1444 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08,
1445 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23,
1446 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a,
1447 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18,
1448 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35,
1449 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45,
1450 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55,
1451 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65,
1452 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75,
1453 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84,
1454 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93,
1455 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2,
1456 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
1457 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
1458 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8,
1459 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
1460 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
1461 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5,
1462 0xf6, 0xf7, 0xf8, 0xf9, 0xfa
1465 /* write fixed Huffman tables */
1466 adr = 0x1d4;
1467 for (i = 0; i < sizeof(dht); ++i) {
1468 zr36060_write_8(zr, adr++, dht[i]);
1472 static void zr36060_set_jpg_APP(struct zoran *zr)
1474 unsigned adr;
1475 int len, i;
1476 u32 reg;
1479 len = zr->params.APP_len;
1480 if (len < 0)
1481 len = 0;
1482 if (len > 60)
1483 len = 60;
1485 i = zr->params.APPn;
1486 if (i < 0)
1487 i = 0;
1488 if (i > 15)
1489 i = 15;
1491 reg = 0xffe0 + i; /* APPn marker */
1492 zr36060_write_16(zr, 0x380, reg);
1494 reg = len + 2; /* APPn len */
1495 zr36060_write_16(zr, 0x382, reg);
1497 /* write APPn data */
1498 adr = 0x384;
1499 for (i = 0; i < 60; i++) {
1500 zr36060_write_8(zr, adr++, (i < len ? zr->params.APP_data[i] : 0));
1504 static void zr36060_set_jpg_COM(struct zoran *zr)
1506 unsigned adr;
1507 int len, i;
1508 u32 reg;
1511 len = zr->params.COM_len;
1512 if (len < 0)
1513 len = 0;
1514 if (len > 60)
1515 len = 60;
1517 reg = 0xfffe; /* COM marker */
1518 zr36060_write_16(zr, 0x3c0, reg);
1520 reg = len + 2; /* COM len */
1521 zr36060_write_16(zr, 0x3c2, reg);
1523 /* write COM data */
1524 adr = 0x3c4;
1525 for (i = 0; i < 60; i++) {
1526 zr36060_write_8(zr, adr++, (i < len ? zr->params.COM_data[i] : 0));
1530 static void zr36060_set_cap(struct zoran *zr, enum zoran_codec_mode mode)
1532 unsigned i;
1533 u32 reg;
1535 zr36060_reset(zr);
1536 mdelay(10);
1538 reg = (0 << 7) /* Load=0 */
1539 |(1 << 0); /* SynRst=1 */
1540 zr36060_write_8(zr, 0x000, reg);
1542 zr36060_set_jpg(zr, mode);
1543 zr36060_set_video(zr, mode);
1544 zr36060_set_jpg_SOF(zr);
1545 zr36060_set_jpg_SOS(zr);
1546 zr36060_set_jpg_DRI(zr);
1547 zr36060_set_jpg_DQT(zr);
1548 zr36060_set_jpg_DHT(zr);
1549 zr36060_set_jpg_APP(zr);
1550 zr36060_set_jpg_COM(zr);
1552 reg = (1 << 7) /* Load=1 */
1553 |(0 << 0); /* SynRst=0 */
1554 zr36060_write_8(zr, 0x000, reg);
1556 /* wait for codec to unbusy */
1557 for (i = 0; i < 1000; ++i) {
1558 reg = zr36060_read_8(zr, 0x001);
1559 if ((reg & (1 << 7)) == 0) {
1560 DEBUG(printk(KERN_DEBUG "060: loaded, loops=%u\n", i));
1561 return;
1563 udelay(1000);
1565 printk(KERN_INFO "060: stuck busy, statux=%02x\n", reg);
1568 static void zr36057_set_jpg(struct zoran *zr, enum zoran_codec_mode mode)
1570 struct tvnorm *tvn;
1571 u32 reg;
1572 int i;
1574 tvn = &tvnorms[zr->params.norm];
1576 /* assert P_Reset */
1577 btwrite(0, ZR36057_JPC);
1579 /* re-initialize DMA ring stuff */
1580 zr->jpg_que_head = 0;
1581 zr->jpg_dma_head = 0;
1582 zr->jpg_dma_tail = 0;
1583 zr->jpg_que_tail = 0;
1584 zr->jpg_seq_num = 0;
1585 for (i = 0; i < BUZ_NUM_STAT_COM; ++i) {
1586 zr->stat_com[i] = 1; /* mark as unavailable to zr36057 */
1588 for (i = 0; i < zr->jpg_nbufs; i++) {
1589 zr->jpg_gbuf[i].state = BUZ_STATE_USER; /* nothing going on */
1592 /* MJPEG compression mode */
1593 switch (mode) {
1595 case BUZ_MODE_MOTION_COMPRESS:
1596 default:
1597 reg = ZR36057_JMC_MJPGCmpMode;
1598 break;
1600 case BUZ_MODE_MOTION_DECOMPRESS:
1601 reg = ZR36057_JMC_MJPGExpMode;
1602 reg |= ZR36057_JMC_SyncMstr;
1603 /* RJ: The following is experimental - improves the output to screen */
1604 if (zr->params.VFIFO_FB)
1605 reg |= ZR36057_JMC_VFIFO_FB;
1606 break;
1608 case BUZ_MODE_STILL_COMPRESS:
1609 reg = ZR36057_JMC_JPGCmpMode;
1610 break;
1612 case BUZ_MODE_STILL_DECOMPRESS:
1613 reg = ZR36057_JMC_JPGExpMode;
1614 break;
1617 reg |= ZR36057_JMC_JPG;
1618 if (zr->params.field_per_buff == 1)
1619 reg |= ZR36057_JMC_Fld_per_buff;
1620 btwrite(reg, ZR36057_JMC);
1622 /* vertical */
1623 btor(ZR36057_VFEVCR_VSPol, ZR36057_VFEVCR);
1624 reg = (6 << ZR36057_VSP_VsyncSize) | (tvn->Ht << ZR36057_VSP_FrmTot);
1625 btwrite(reg, ZR36057_VSP);
1626 reg = ((zr->params.img_y + tvn->VStart) << ZR36057_FVAP_NAY)
1627 | (zr->params.img_height << ZR36057_FVAP_PAY);
1628 btwrite(reg, ZR36057_FVAP);
1630 /* horizontal */
1631 btor(ZR36057_VFEHCR_HSPol, ZR36057_VFEHCR);
1632 reg = ((tvn->Wt - 100) << ZR36057_HSP_HsyncStart) | (tvn->Wt << ZR36057_HSP_LineTot);
1633 btwrite(reg, ZR36057_HSP);
1634 reg = ((zr->params.img_x + tvn->HStart) << ZR36057_FHAP_NAX)
1635 | (zr->params.img_width << ZR36057_FHAP_PAX);
1636 btwrite(reg, ZR36057_FHAP);
1638 /* field process parameters */
1639 if (zr->params.odd_even)
1640 reg = ZR36057_FPP_Odd_Even;
1641 else
1642 reg = 0;
1643 btwrite(reg, ZR36057_FPP);
1645 /* Set proper VCLK Polarity, else colors will be wrong during playback */
1646 btor(ZR36057_VFESPFR_VCLKPol, ZR36057_VFESPFR);
1648 /* code base address and FIFO threshold */
1649 reg = virt_to_bus(zr->stat_com);
1650 btwrite(reg, ZR36057_JCBA);
1651 reg = 0x50;
1652 btwrite(reg, ZR36057_JCFT);
1654 /* JPEG codec guest ID */
1655 reg = (1 << ZR36057_JCGI_JPEGuestID) | (0 << ZR36057_JCGI_JPEGuestReg);
1656 btwrite(reg, ZR36057_JCGI);
1658 /* Code transfer guest ID */
1659 reg = (0 << ZR36057_MCTCR_CodGuestID) | (3 << ZR36057_MCTCR_CodGuestReg);
1660 reg |= ZR36057_MCTCR_CFlush;
1661 btwrite(reg, ZR36057_MCTCR);
1663 /* deassert P_Reset */
1664 btwrite(ZR36057_JPC_P_Reset, ZR36057_JPC);
1667 static void zr36057_enable_jpg(struct zoran *zr, enum zoran_codec_mode mode)
1669 static int zero = 0;
1670 static int one = 1;
1672 switch (mode) {
1674 case BUZ_MODE_MOTION_COMPRESS:
1675 zr36060_set_cap(zr, mode);
1676 zr36057_set_jpg(zr, mode);
1677 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_ENABLE_OUTPUT, &one);
1678 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_INPUT, &zero);
1680 /* deassert P_Reset, assert Code transfer enable */
1681 btwrite(IRQ_MASK, ZR36057_ISR);
1682 btand(~ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
1683 break;
1685 case BUZ_MODE_MOTION_DECOMPRESS:
1686 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_ENABLE_OUTPUT, &zero);
1687 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_INPUT, &one);
1688 zr36060_set_cap(zr, mode);
1689 zr36057_set_jpg(zr, mode);
1691 /* deassert P_Reset, assert Code transfer enable */
1692 btwrite(IRQ_MASK, ZR36057_ISR);
1693 btand(~ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
1694 break;
1696 case BUZ_MODE_IDLE:
1697 default:
1698 /* shut down processing */
1699 btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
1700 btwrite(ZR36057_JPC_P_Reset, ZR36057_JPC);
1701 btand(~ZR36057_JMC_VFIFO_FB, ZR36057_JMC);
1702 btand(~ZR36057_JMC_SyncMstr, ZR36057_JMC);
1703 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1704 btwrite(0, ZR36057_ISR);
1705 zr36060_reset(zr);
1706 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_ENABLE_OUTPUT, &one);
1707 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_INPUT, &zero);
1708 break;
1711 zr->codec_mode = mode;
1715 * Queue a MJPEG buffer for capture/playback
1718 static int jpg_qbuf(struct zoran *zr, int frame, enum zoran_codec_mode mode)
1720 unsigned long flags;
1721 int res;
1723 /* Check if buffers are allocated */
1725 if (!zr->jpg_buffers_allocated) {
1726 printk(KERN_ERR "%s: jpg_qbuf: buffers not yet allocated\n", zr->name);
1727 return -ENOMEM;
1729 /* Does the user want to stop streaming? */
1731 if (frame < 0) {
1732 if (zr->codec_mode == mode) {
1733 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1734 return 0;
1735 } else {
1736 printk(KERN_ERR "%s: jpg_qbuf - stop streaming but not in streaming mode\n", zr->name);
1737 return -EINVAL;
1740 /* No grabbing outside the buffer range! */
1742 if (frame >= zr->jpg_nbufs) {
1743 printk(KERN_ERR "%s: jpg_qbuf: buffer %d out of range\n", zr->name, frame);
1744 return -EINVAL;
1746 /* what is the codec mode right now? */
1748 if (zr->codec_mode == BUZ_MODE_IDLE) {
1749 /* Ok load up the zr36060 and go */
1750 zr36057_enable_jpg(zr, mode);
1751 } else if (zr->codec_mode != mode) {
1752 /* wrong codec mode active - invalid */
1753 printk(KERN_ERR "%s: jpg_qbuf - codec in wrong mode\n", zr->name);
1754 return -EINVAL;
1756 spin_lock_irqsave(&zr->lock, flags);
1758 /* make sure a grab isn't going on currently with this buffer */
1760 switch (zr->jpg_gbuf[frame].state) {
1762 default:
1763 case BUZ_STATE_DMA:
1764 case BUZ_STATE_PEND:
1765 case BUZ_STATE_DONE:
1766 res = -EBUSY; /* what are you doing? */
1767 break;
1769 case BUZ_STATE_USER:
1770 /* since there is at least one unused buffer there's room for at least one more pend[] entry */
1771 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = frame;
1772 zr->jpg_gbuf[frame].state = BUZ_STATE_PEND;
1773 zoran_feed_stat_com(zr);
1774 res = 0;
1775 break;
1779 spin_unlock_irqrestore(&zr->lock, flags);
1781 /* Start the zr36060 when the first frame is queued */
1782 if (zr->jpg_que_head == 1) {
1783 btor(ZR36057_JMC_Go_en, ZR36057_JMC);
1784 btwrite(ZR36057_JPC_P_Reset | ZR36057_JPC_CodTrnsEn | ZR36057_JPC_Active, ZR36057_JPC);
1786 return res;
1790 * Sync on a MJPEG buffer
1793 static int jpg_sync(struct zoran *zr, struct zoran_sync *bs)
1795 unsigned long flags;
1796 int frame;
1798 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
1799 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
1800 return -EINVAL;
1802 while (zr->jpg_que_tail == zr->jpg_dma_tail) {
1803 interruptible_sleep_on(&zr->jpg_capq);
1804 if (signal_pending(current))
1805 return -ERESTARTSYS;
1808 spin_lock_irqsave(&zr->lock, flags);
1810 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
1812 /* buffer should now be in BUZ_STATE_DONE */
1814 if (zr->jpg_gbuf[frame].state != BUZ_STATE_DONE)
1815 printk(KERN_ERR "%s: jpg_sync - internal error\n", zr->name);
1817 *bs = zr->jpg_gbuf[frame].bs;
1818 zr->jpg_gbuf[frame].state = BUZ_STATE_USER;
1820 spin_unlock_irqrestore(&zr->lock, flags);
1822 return 0;
1825 /* when this is called the spinlock must be held */
1826 static void zoran_feed_stat_com(struct zoran *zr)
1828 /* move frames from pending queue to DMA */
1830 int frame, i, max_stat_com;
1832 max_stat_com = (zr->params.TmpDcm == 1) ? BUZ_NUM_STAT_COM : (BUZ_NUM_STAT_COM >> 1);
1834 while ((zr->jpg_dma_head - zr->jpg_dma_tail) < max_stat_com
1835 && zr->jpg_dma_head != zr->jpg_que_head) {
1837 frame = zr->jpg_pend[zr->jpg_dma_head & BUZ_MASK_FRAME];
1838 if (zr->params.TmpDcm == 1) {
1839 /* fill 1 stat_com entry */
1840 i = zr->jpg_dma_head & BUZ_MASK_STAT_COM;
1841 zr->stat_com[i] = zr->jpg_gbuf[frame].frag_tab_bus;
1842 } else {
1843 /* fill 2 stat_com entries */
1844 i = (zr->jpg_dma_head & 1) * 2;
1845 zr->stat_com[i] = zr->jpg_gbuf[frame].frag_tab_bus;
1846 zr->stat_com[i + 1] = zr->jpg_gbuf[frame].frag_tab_bus;
1848 zr->jpg_gbuf[frame].state = BUZ_STATE_DMA;
1849 zr->jpg_dma_head++;
1854 /* when this is called the spinlock must be held */
1855 static void zoran_reap_stat_com(struct zoran *zr)
1857 /* move frames from DMA queue to done queue */
1859 int i;
1860 u32 stat_com;
1861 unsigned int seq;
1862 unsigned int dif;
1863 int frame;
1864 struct zoran_gbuffer *gbuf;
1866 /* In motion decompress we don't have a hardware frame counter,
1867 we just count the interrupts here */
1869 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS)
1870 zr->jpg_seq_num++;
1872 while (zr->jpg_dma_tail != zr->jpg_dma_head) {
1873 if (zr->params.TmpDcm == 1)
1874 i = zr->jpg_dma_tail & BUZ_MASK_STAT_COM;
1875 else
1876 i = (zr->jpg_dma_tail & 1) * 2 + 1;
1878 stat_com = zr->stat_com[i];
1880 if ((stat_com & 1) == 0) {
1881 return;
1883 frame = zr->jpg_pend[zr->jpg_dma_tail & BUZ_MASK_FRAME];
1884 gbuf = &zr->jpg_gbuf[frame];
1885 get_fast_time(&gbuf->bs.timestamp);
1887 if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1888 gbuf->bs.length = (stat_com & 0x7fffff) >> 1;
1890 /* update sequence number with the help of the counter in stat_com */
1892 seq = stat_com >> 24;
1893 dif = (seq - zr->jpg_seq_num) & 0xff;
1894 zr->jpg_seq_num += dif;
1895 } else {
1896 gbuf->bs.length = 0;
1898 gbuf->bs.seq = zr->params.TmpDcm == 2 ? (zr->jpg_seq_num >> 1) : zr->jpg_seq_num;
1899 gbuf->state = BUZ_STATE_DONE;
1901 zr->jpg_dma_tail++;
1905 static void zoran_irq(int irq, void *dev_id, struct pt_regs *regs)
1907 u32 stat, astat;
1908 int count;
1909 struct zoran *zr;
1910 unsigned long flags;
1912 zr = (struct zoran *) dev_id;
1913 count = 0;
1915 spin_lock_irqsave(&zr->lock, flags);
1916 while (1) {
1917 /* get/clear interrupt status bits */
1918 stat = btread(ZR36057_ISR);
1919 astat = stat & IRQ_MASK;
1920 if (!astat) {
1921 break;
1923 btwrite(astat, ZR36057_ISR);
1924 IDEBUG(printk(BUZ_DEBUG "-%u: astat %08x stat %08x\n", zr->id, astat, stat));
1926 #if (IRQ_MASK & ZR36057_ISR_GIRQ0)
1927 if (astat & ZR36057_ISR_GIRQ0) {
1929 /* Interrupts may still happen when zr->v4l_memgrab_active is switched off.
1930 We simply ignore them */
1932 if (zr->v4l_memgrab_active) {
1934 /* A lot more checks should be here ... */
1935 if ((btread(ZR36057_VSSFGR) & ZR36057_VSSFGR_SnapShot) == 0)
1936 printk(KERN_WARNING "%s: BuzIRQ with SnapShot off ???\n", zr->name);
1938 if (zr->v4l_grab_frame != NO_GRAB_ACTIVE) {
1939 /* There is a grab on a frame going on, check if it has finished */
1941 if ((btread(ZR36057_VSSFGR) & ZR36057_VSSFGR_FrameGrab) == 0) {
1942 /* it is finished, notify the user */
1944 zr->v4l_gbuf[zr->v4l_grab_frame].state = BUZ_STATE_DONE;
1945 zr->v4l_grab_frame = NO_GRAB_ACTIVE;
1946 zr->v4l_grab_seq++;
1947 zr->v4l_pend_tail++;
1950 if (zr->v4l_grab_frame == NO_GRAB_ACTIVE)
1951 wake_up_interruptible(&zr->v4l_capq);
1953 /* Check if there is another grab queued */
1955 if (zr->v4l_grab_frame == NO_GRAB_ACTIVE &&
1956 zr->v4l_pend_tail != zr->v4l_pend_head) {
1958 int frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
1959 u32 reg;
1961 zr->v4l_grab_frame = frame;
1963 /* Set zr36057 video front end and enable video */
1965 /* Buffer address */
1967 reg = zr->v4l_gbuf[frame].fbuffer_bus;
1968 btwrite(reg, ZR36057_VDTR);
1969 if (zr->video_interlace)
1970 reg += zr->gbpl;
1971 btwrite(reg, ZR36057_VDBR);
1973 /* video stride, status, and frame grab register */
1975 #ifdef XAWTV_HACK
1976 reg = (zr->gwidth > 720) ? ((zr->gwidth & ~3) - 720) * zr->gbpl / zr->gwidth : 0;
1977 #else
1978 reg = 0;
1979 #endif
1980 if (zr->video_interlace)
1981 reg += zr->gbpl;
1982 reg = (reg << ZR36057_VSSFGR_DispStride);
1983 reg |= ZR36057_VSSFGR_VidOvf;
1984 reg |= ZR36057_VSSFGR_SnapShot;
1985 reg |= ZR36057_VSSFGR_FrameGrab;
1986 btwrite(reg, ZR36057_VSSFGR);
1988 btor(ZR36057_VDCR_VidEn, ZR36057_VDCR);
1992 #endif /* (IRQ_MASK & ZR36057_ISR_GIRQ0) */
1994 #if (IRQ_MASK & ZR36057_ISR_GIRQ1)
1995 if (astat & ZR36057_ISR_GIRQ1) {
1996 unsigned csr = zr36060_read_8(zr, 0x001);
1997 unsigned isr = zr36060_read_8(zr, 0x008);
1999 IDEBUG(printk(KERN_DEBUG "%s: ZR36057_ISR_GIRQ1 60_code=%02x 60_intr=%02x\n",
2000 zr->name, csr, isr));
2002 btand(~ZR36057_ICR_GIRQ1, ZR36057_ICR);
2003 zoran_reap_stat_com(zr);
2004 zoran_feed_stat_com(zr);
2006 #endif /* (IRQ_MASK & ZR36057_ISR_GIRQ1) */
2008 #if (IRQ_MASK & ZR36057_ISR_CodRepIRQ)
2009 if (astat & ZR36057_ISR_CodRepIRQ) {
2010 IDEBUG(printk(KERN_DEBUG "%s: ZR36057_ISR_CodRepIRQ\n", zr->name));
2011 btand(~ZR36057_ICR_CodRepIRQ, ZR36057_ICR);
2013 #endif /* (IRQ_MASK & ZR36057_ISR_CodRepIRQ) */
2015 #if (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ)
2016 if ((astat & ZR36057_ISR_JPEGRepIRQ) &&
2017 (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
2018 zr->codec_mode == BUZ_MODE_MOTION_COMPRESS)) {
2019 zoran_reap_stat_com(zr);
2020 zoran_feed_stat_com(zr);
2021 wake_up_interruptible(&zr->jpg_capq);
2023 #endif /* (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ) */
2025 count++;
2026 if (count > 10) {
2027 printk(KERN_WARNING "%s: irq loop %d\n", zr->name, count);
2028 if (count > 20) {
2029 btwrite(0, ZR36057_ICR);
2030 printk(KERN_ERR "%s: IRQ lockup, cleared int mask\n", zr->name);
2031 break;
2035 spin_unlock_irqrestore(&zr->lock, flags);
2038 /* Check a zoran_params struct for correctness, insert default params */
2040 static int zoran_check_params(struct zoran *zr, struct zoran_params *params)
2042 int err = 0, err0 = 0;
2044 /* insert constant params */
2046 params->major_version = MAJOR_VERSION;
2047 params->minor_version = MINOR_VERSION;
2049 /* Check input and norm */
2051 if (params->input != 0 && params->input != 1) {
2052 err++;
2054 if (params->norm != VIDEO_MODE_PAL && params->norm != VIDEO_MODE_NTSC) {
2055 err++;
2057 /* Check decimation, set default values for decimation = 1, 2, 4 */
2059 switch (params->decimation) {
2060 case 1:
2062 params->HorDcm = 1;
2063 params->VerDcm = 1;
2064 params->TmpDcm = 1;
2065 params->field_per_buff = 2;
2067 params->img_x = 0;
2068 params->img_y = 0;
2069 params->img_width = 720;
2070 params->img_height = tvnorms[params->norm].Ha / 2;
2071 break;
2073 case 2:
2075 params->HorDcm = 2;
2076 params->VerDcm = 1;
2077 params->TmpDcm = 2;
2078 params->field_per_buff = 1;
2080 params->img_x = 8;
2081 params->img_y = 0;
2082 params->img_width = 704;
2083 params->img_height = tvnorms[params->norm].Ha / 2;
2084 break;
2086 case 4:
2088 params->HorDcm = 4;
2089 params->VerDcm = 2;
2090 params->TmpDcm = 2;
2091 params->field_per_buff = 1;
2093 params->img_x = 8;
2094 params->img_y = 0;
2095 params->img_width = 704;
2096 params->img_height = tvnorms[params->norm].Ha / 2;
2097 break;
2099 case 0:
2101 /* We have to check the data the user has set */
2103 if (params->HorDcm != 1 && params->HorDcm != 2 && params->HorDcm != 4)
2104 err0++;
2105 if (params->VerDcm != 1 && params->VerDcm != 2)
2106 err0++;
2107 if (params->TmpDcm != 1 && params->TmpDcm != 2)
2108 err0++;
2109 if (params->field_per_buff != 1 && params->field_per_buff != 2)
2110 err0++;
2112 if (params->img_x < 0)
2113 err0++;
2114 if (params->img_y < 0)
2115 err0++;
2116 if (params->img_width < 0)
2117 err0++;
2118 if (params->img_height < 0)
2119 err0++;
2120 if (params->img_x + params->img_width > 720)
2121 err0++;
2122 if (params->img_y + params->img_height > tvnorms[params->norm].Ha / 2)
2123 err0++;
2124 if (params->img_width % (16 * params->HorDcm) != 0)
2125 err0++;
2126 if (params->img_height % (8 * params->VerDcm) != 0)
2127 err0++;
2129 if (err0) {
2130 err++;
2132 break;
2134 default:
2135 err++;
2136 break;
2139 if (params->quality > 100)
2140 params->quality = 100;
2141 if (params->quality < 5)
2142 params->quality = 5;
2144 if (params->APPn < 0)
2145 params->APPn = 0;
2146 if (params->APPn > 15)
2147 params->APPn = 15;
2148 if (params->APP_len < 0)
2149 params->APP_len = 0;
2150 if (params->APP_len > 60)
2151 params->APP_len = 60;
2152 if (params->COM_len < 0)
2153 params->COM_len = 0;
2154 if (params->COM_len > 60)
2155 params->COM_len = 60;
2157 if (err)
2158 return -EINVAL;
2160 return 0;
2163 static void zoran_open_init_params(struct zoran *zr)
2165 int i;
2167 /* Per default, map the V4L Buffers */
2169 zr->map_mjpeg_buffers = 0;
2171 /* User must explicitly set a window */
2173 zr->window_set = 0;
2175 zr->window.x = 0;
2176 zr->window.y = 0;
2177 zr->window.width = 0;
2178 zr->window.height = 0;
2179 zr->window.chromakey = 0;
2180 zr->window.flags = 0;
2181 zr->window.clips = NULL;
2182 zr->window.clipcount = 0;
2184 zr->video_interlace = 0;
2186 zr->v4l_memgrab_active = 0;
2187 zr->v4l_overlay_active = 0;
2189 zr->v4l_grab_frame = NO_GRAB_ACTIVE;
2190 zr->v4l_grab_seq = 0;
2192 zr->gwidth = 0;
2193 zr->gheight = 0;
2194 zr->gformat = 0;
2195 zr->gbpl = 0;
2197 /* DMA ring stuff for V4L */
2199 zr->v4l_pend_tail = 0;
2200 zr->v4l_pend_head = 0;
2201 for (i = 0; i < v4l_nbufs; i++) {
2202 zr->v4l_gbuf[i].state = BUZ_STATE_USER; /* nothing going on */
2205 /* Set necessary params and call zoran_check_params to set the defaults */
2207 zr->params.decimation = 1;
2209 zr->params.quality = 50; /* default compression factor 8 */
2210 zr->params.odd_even = 1;
2212 zr->params.APPn = 0;
2213 zr->params.APP_len = 0; /* No APPn marker */
2214 for (i = 0; i < 60; i++)
2215 zr->params.APP_data[i] = 0;
2217 zr->params.COM_len = 0; /* No COM marker */
2218 for (i = 0; i < 60; i++)
2219 zr->params.COM_data[i] = 0;
2221 zr->params.VFIFO_FB = 0;
2223 memset(zr->params.reserved, 0, sizeof(zr->params.reserved));
2225 zr->params.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
2227 i = zoran_check_params(zr, &zr->params);
2228 if (i)
2229 printk(KERN_ERR "%s: zoran_open_init_params internal error\n", zr->name);
2233 * Open a buz card. Right now the flags stuff is just playing
2236 static int zoran_open(struct video_device *dev, int flags)
2238 struct zoran *zr = (struct zoran *) dev;
2240 DEBUG(printk(KERN_INFO ": zoran_open\n"));
2242 switch (flags) {
2244 case 0:
2245 if (zr->user)
2246 return -EBUSY;
2247 zr->user++;
2249 if (v4l_fbuffer_alloc(zr) < 0) {
2250 zr->user--;
2251 return -ENOMEM;
2253 /* default setup */
2255 zoran_open_init_params(zr);
2257 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
2259 btwrite(IRQ_MASK, ZR36057_ISR); // Clears interrupts
2261 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
2263 break;
2265 default:
2266 return -EBUSY;
2269 MOD_INC_USE_COUNT;
2270 return 0;
2273 static void zoran_close(struct video_device *dev)
2275 struct zoran *zr = (struct zoran *) dev;
2277 DEBUG(printk(KERN_INFO ": zoran_close\n"));
2279 /* disable interrupts */
2280 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
2282 /* wake up sleeping beauties */
2283 wake_up_interruptible(&zr->v4l_capq);
2284 wake_up_interruptible(&zr->jpg_capq);
2286 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
2287 zr36057_set_memgrab(zr, 0);
2288 if (zr->v4l_overlay_active)
2289 zr36057_overlay(zr, 0);
2291 zr->user--;
2293 v4l_fbuffer_free(zr);
2294 jpg_fbuffer_free(zr);
2295 zr->jpg_nbufs = 0;
2297 MOD_DEC_USE_COUNT;
2298 DEBUG(printk(KERN_INFO ": zoran_close done\n"));
2302 static long zoran_read(struct video_device *dev, char *buf, unsigned long count, int nonblock)
2304 return -EINVAL;
2307 static long zoran_write(struct video_device *dev, const char *buf, unsigned long count, int nonblock)
2309 return -EINVAL;
2313 * ioctl routine
2317 static int zoran_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
2319 struct zoran *zr = (struct zoran *) dev;
2321 switch (cmd) {
2323 case VIDIOCGCAP:
2325 struct video_capability b;
2326 IOCTL_DEBUG(printk("buz ioctl VIDIOCGCAP\n"));
2327 strncpy(b.name, zr->video_dev.name, sizeof(b.name));
2328 b.type = VID_TYPE_CAPTURE |
2329 VID_TYPE_OVERLAY |
2330 VID_TYPE_CLIPPING |
2331 VID_TYPE_FRAMERAM |
2332 VID_TYPE_SCALES;
2333 /* theoretically we could also flag VID_TYPE_SUBCAPTURE
2334 but this is not even implemented in the BTTV driver */
2336 b.channels = 2; /* composite, svhs */
2337 b.audios = 0;
2338 b.maxwidth = BUZ_MAX_WIDTH;
2339 b.maxheight = BUZ_MAX_HEIGHT;
2340 b.minwidth = BUZ_MIN_WIDTH;
2341 b.minheight = BUZ_MIN_HEIGHT;
2342 if (copy_to_user(arg, &b, sizeof(b))) {
2343 return -EFAULT;
2345 return 0;
2348 case VIDIOCGCHAN:
2350 struct video_channel v;
2352 if (copy_from_user(&v, arg, sizeof(v))) {
2353 return -EFAULT;
2355 IOCTL_DEBUG(printk("buz ioctl VIDIOCGCHAN for channel %d\n", v.channel));
2356 switch (v.channel) {
2357 case 0:
2358 strcpy(v.name, "Composite");
2359 break;
2360 case 1:
2361 strcpy(v.name, "SVHS");
2362 break;
2363 default:
2364 return -EINVAL;
2366 v.tuners = 0;
2367 v.flags = 0;
2368 v.type = VIDEO_TYPE_CAMERA;
2369 v.norm = zr->params.norm;
2370 if (copy_to_user(arg, &v, sizeof(v))) {
2371 return -EFAULT;
2373 return 0;
2376 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2378 * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2379 * ^^^^^^^
2380 * The famos BTTV driver has it implemented with a struct video_channel argument
2381 * and we follow it for compatibility reasons
2383 * BTW: this is the only way the user can set the norm!
2386 case VIDIOCSCHAN:
2388 struct video_channel v;
2389 int input;
2390 int on, res;
2392 if (copy_from_user(&v, arg, sizeof(v))) {
2393 return -EFAULT;
2395 IOCTL_DEBUG(printk("buz ioctl VIDIOCSCHAN: channel=%d, norm=%d\n", v.channel, v.norm));
2396 switch (v.channel) {
2397 case 0:
2398 input = 3;
2399 break;
2400 case 1:
2401 input = 7;
2402 break;
2403 default:
2404 return -EINVAL;
2407 if (v.norm != VIDEO_MODE_PAL
2408 && v.norm != VIDEO_MODE_NTSC) {
2409 return -EINVAL;
2411 zr->params.norm = v.norm;
2412 zr->params.input = v.channel;
2414 /* We switch overlay off and on since a change in the norm
2415 needs different VFE settings */
2417 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active;
2418 if (on)
2419 zr36057_overlay(zr, 0);
2421 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &input);
2422 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &zr->params.norm);
2423 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_NORM, &zr->params.norm);
2425 if (on)
2426 zr36057_overlay(zr, 1);
2428 /* Make sure the changes come into effect */
2429 res = wait_grab_pending(zr);
2430 if (res)
2431 return res;
2433 return 0;
2436 case VIDIOCGTUNER:
2437 case VIDIOCSTUNER:
2438 return -EINVAL;
2440 case VIDIOCGPICT:
2442 struct video_picture p = zr->picture;
2444 IOCTL_DEBUG(printk("buz ioctl VIDIOCGPICT\n"));
2445 p.depth = zr->buffer.depth;
2446 switch (zr->buffer.depth) {
2447 case 15:
2448 p.palette = VIDEO_PALETTE_RGB555;
2449 break;
2451 case 16:
2452 p.palette = VIDEO_PALETTE_RGB565;
2453 break;
2455 case 24:
2456 p.palette = VIDEO_PALETTE_RGB24;
2457 break;
2459 case 32:
2460 p.palette = VIDEO_PALETTE_RGB32;
2461 break;
2464 if (copy_to_user(arg, &p, sizeof(p))) {
2465 return -EFAULT;
2467 return 0;
2470 case VIDIOCSPICT:
2472 struct video_picture p;
2474 if (copy_from_user(&p, arg, sizeof(p))) {
2475 return -EFAULT;
2477 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_PICTURE, &p);
2478 IOCTL_DEBUG(printk("buz ioctl VIDIOCSPICT bri=%d hue=%d col=%d con=%d dep=%d pal=%d\n",
2479 p.brightness, p.hue, p.colour, p.contrast, p.depth, p.palette));
2480 /* The depth and palette values have no meaning to us,
2481 should we return -EINVAL if they don't fit ? */
2482 zr->picture = p;
2483 return 0;
2486 case VIDIOCCAPTURE:
2488 int v, res;
2490 if (copy_from_user(&v, arg, sizeof(v))) {
2491 return -EFAULT;
2493 IOCTL_DEBUG(printk("buz ioctl VIDIOCCAPTURE: %d\n", v));
2494 /* If there is nothing to do, return immediatly */
2496 if ((v && zr->v4l_overlay_active) || (!v && !zr->v4l_overlay_active))
2497 return 0;
2499 if (v == 0) {
2500 zr->v4l_overlay_active = 0;
2501 if (!zr->v4l_memgrab_active)
2502 zr36057_overlay(zr, 0);
2503 /* When a grab is running, the video simply won't be switched on any more */
2504 } else {
2505 if (!zr->buffer_set || !zr->window_set) {
2506 return -EINVAL;
2508 zr->v4l_overlay_active = 1;
2509 if (!zr->v4l_memgrab_active)
2510 zr36057_overlay(zr, 1);
2511 /* When a grab is running, the video will be switched on when grab is finished */
2513 /* Make sure the changes come into effect */
2514 res = wait_grab_pending(zr);
2515 if (res)
2516 return res;
2517 return 0;
2520 case VIDIOCGWIN:
2522 IOCTL_DEBUG(printk("buz ioctl VIDIOCGWIN\n"));
2523 if (copy_to_user(arg, &zr->window, sizeof(zr->window))) {
2524 return -EFAULT;
2526 return 0;
2529 case VIDIOCSWIN:
2531 struct video_clip *vcp;
2532 struct video_window vw;
2533 int on, end, res;
2535 if (copy_from_user(&vw, arg, sizeof(vw))) {
2536 return -EFAULT;
2538 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));
2539 if (!zr->buffer_set) {
2540 return -EINVAL;
2543 * The video front end needs 4-byte alinged line sizes, we correct that
2544 * silently here if necessary
2547 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
2548 end = (vw.x + vw.width) & ~1; /* round down */
2549 vw.x = (vw.x + 1) & ~1; /* round up */
2550 vw.width = end - vw.x;
2552 if (zr->buffer.depth == 24) {
2553 end = (vw.x + vw.width) & ~3; /* round down */
2554 vw.x = (vw.x + 3) & ~3; /* round up */
2555 vw.width = end - vw.x;
2557 #if 0
2558 // At least xawtv seems to care about the following - just leave it away
2560 * Also corrected silently (as long as window fits at all):
2561 * video not fitting the screen
2563 #if 0
2564 if (vw.x < 0 || vw.y < 0 || vw.x + vw.width > zr->buffer.width ||
2565 vw.y + vw.height > zr->buffer.height) {
2566 printk(BUZ_ERR ": VIDIOCSWIN: window does not fit frame buffer: %dx%d+%d*%d\n",
2567 vw.width, vw.height, vw.x, vw.y);
2568 return -EINVAL;
2570 #else
2571 if (vw.x < 0)
2572 vw.x = 0;
2573 if (vw.y < 0)
2574 vw.y = 0;
2575 if (vw.x + vw.width > zr->buffer.width)
2576 vw.width = zr->buffer.width - vw.x;
2577 if (vw.y + vw.height > zr->buffer.height)
2578 vw.height = zr->buffer.height - vw.y;
2579 #endif
2580 #endif
2582 /* Check for vaild parameters */
2583 if (vw.width < BUZ_MIN_WIDTH || vw.height < BUZ_MIN_HEIGHT ||
2584 vw.width > BUZ_MAX_WIDTH || vw.height > BUZ_MAX_HEIGHT) {
2585 return -EINVAL;
2587 #ifdef XAWTV_HACK
2588 if (vw.width > 720)
2589 vw.width = 720;
2590 #endif
2592 zr->window.x = vw.x;
2593 zr->window.y = vw.y;
2594 zr->window.width = vw.width;
2595 zr->window.height = vw.height;
2596 zr->window.chromakey = 0;
2597 zr->window.flags = 0; // RJ: Is this intended for interlace on/off ?
2599 zr->window.clips = NULL;
2600 zr->window.clipcount = vw.clipcount;
2603 * If an overlay is running, we have to switch it off
2604 * and switch it on again in order to get the new settings in effect.
2606 * We also want to avoid that the overlay mask is written
2607 * when an overlay is running.
2610 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active;
2611 if (on)
2612 zr36057_overlay(zr, 0);
2615 * Write the overlay mask if clips are wanted.
2617 if (vw.clipcount) {
2618 vcp = vmalloc(sizeof(struct video_clip) * (vw.clipcount + 4));
2619 if (vcp == NULL) {
2620 return -ENOMEM;
2622 if (copy_from_user(vcp, vw.clips, sizeof(struct video_clip) * vw.clipcount)) {
2623 vfree(vcp);
2624 return -EFAULT;
2626 write_overlay_mask(zr, vcp, vw.clipcount);
2627 vfree(vcp);
2629 if (on)
2630 zr36057_overlay(zr, 1);
2631 zr->window_set = 1;
2633 /* Make sure the changes come into effect */
2634 res = wait_grab_pending(zr);
2635 if (res)
2636 return res;
2638 return 0;
2641 case VIDIOCGFBUF:
2643 IOCTL_DEBUG(printk("buz ioctl VIDIOCGFBUF\n"));
2644 if (copy_to_user(arg, &zr->buffer, sizeof(zr->buffer))) {
2645 return -EFAULT;
2647 return 0;
2650 case VIDIOCSFBUF:
2652 struct video_buffer v;
2654 if (!capable(CAP_SYS_ADMIN)
2655 || !capable(CAP_SYS_RAWIO))
2656 return -EPERM;
2658 if (copy_from_user(&v, arg, sizeof(v)))
2659 return -EFAULT;
2661 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));
2662 if (zr->v4l_overlay_active) {
2663 /* Has the user gotten crazy ... ? */
2664 return -EINVAL;
2666 if (v.depth != 15
2667 && v.depth != 16
2668 && v.depth != 24
2669 && v.depth != 32) {
2670 return -EINVAL;
2672 if (v.height <= 0 || v.width <= 0 || v.bytesperline <= 0) {
2673 return -EINVAL;
2675 if (v.bytesperline & 3) {
2676 return -EINVAL;
2678 if (v.base) {
2679 zr->buffer.base = (void *) ((unsigned long) v.base & ~3);
2681 zr->buffer.height = v.height;
2682 zr->buffer.width = v.width;
2683 zr->buffer.depth = v.depth;
2684 zr->buffer.bytesperline = v.bytesperline;
2686 if (zr->buffer.base)
2687 zr->buffer_set = 1;
2688 zr->window_set = 0; /* The user should set new window parameters */
2689 return 0;
2692 /* RJ: what is VIDIOCKEY intended to do ??? */
2694 case VIDIOCGFREQ:
2695 case VIDIOCSFREQ:
2696 case VIDIOCGAUDIO:
2697 case VIDIOCSAUDIO:
2698 return -EINVAL;
2700 case VIDIOCSYNC:
2702 int v;
2704 if (copy_from_user(&v, arg, sizeof(v))) {
2705 return -EFAULT;
2707 IOCTL_DEBUG(printk("buz ioctl VIDIOCSYNC %d\n", v));
2708 return v4l_sync(zr, v);
2711 case VIDIOCMCAPTURE:
2713 struct video_mmap vm;
2715 if (copy_from_user((void *) &vm, (void *) arg, sizeof(vm))) {
2716 return -EFAULT;
2718 IOCTL_DEBUG(printk("buz ioctl VIDIOCMCAPTURE frame=%d geom=%dx%d fmt=%d\n",
2719 vm.frame, vm.height, vm.width, vm.format));
2720 return v4l_grab(zr, &vm);
2723 case VIDIOCGMBUF:
2725 struct video_mbuf vm;
2726 int i;
2728 IOCTL_DEBUG(printk("buz ioctl VIDIOCGMBUF\n"));
2730 vm.size = v4l_nbufs * v4l_bufsize;
2731 vm.frames = v4l_nbufs;
2732 for (i = 0; i < v4l_nbufs; i++) {
2733 vm.offsets[i] = i * v4l_bufsize;
2736 /* The next mmap will map the V4L buffers */
2737 zr->map_mjpeg_buffers = 0;
2739 if (copy_to_user(arg, &vm, sizeof(vm))) {
2740 return -EFAULT;
2742 return 0;
2745 case VIDIOCGUNIT:
2747 struct video_unit vu;
2749 IOCTL_DEBUG(printk("buz ioctl VIDIOCGUNIT\n"));
2750 vu.video = zr->video_dev.minor;
2751 vu.vbi = VIDEO_NO_UNIT;
2752 vu.radio = VIDEO_NO_UNIT;
2753 vu.audio = VIDEO_NO_UNIT;
2754 vu.teletext = VIDEO_NO_UNIT;
2755 if (copy_to_user(arg, &vu, sizeof(vu)))
2756 return -EFAULT;
2757 return 0;
2761 * RJ: In principal we could support subcaptures for V4L grabbing.
2762 * Not even the famous BTTV driver has them, however.
2763 * If there should be a strong demand, one could consider
2764 * to implement them.
2766 case VIDIOCGCAPTURE:
2767 case VIDIOCSCAPTURE:
2768 return -EINVAL;
2770 case BUZIOC_G_PARAMS:
2772 IOCTL_DEBUG(printk("buz ioctl BUZIOC_G_PARAMS\n"));
2773 if (copy_to_user(arg, &(zr->params), sizeof(zr->params)))
2774 return -EFAULT;
2775 return 0;
2778 case BUZIOC_S_PARAMS:
2780 struct zoran_params bp;
2781 int input, on;
2783 if (zr->codec_mode != BUZ_MODE_IDLE) {
2784 return -EINVAL;
2786 if (copy_from_user(&bp, arg, sizeof(bp))) {
2787 return -EFAULT;
2789 IOCTL_DEBUG(printk("buz ioctl BUZIOC_S_PARAMS\n"));
2791 /* Check the params first before overwriting our internal values */
2793 if (zoran_check_params(zr, &bp))
2794 return -EINVAL;
2796 zr->params = bp;
2798 /* Make changes of input and norm go into effect immediatly */
2800 /* We switch overlay off and on since a change in the norm
2801 needs different VFE settings */
2803 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active;
2804 if (on)
2805 zr36057_overlay(zr, 0);
2807 input = zr->params.input == 0 ? 3 : 7;
2808 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &input);
2809 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &zr->params.norm);
2810 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_NORM, &zr->params.norm);
2812 if (on)
2813 zr36057_overlay(zr, 1);
2815 if (copy_to_user(arg, &bp, sizeof(bp))) {
2816 return -EFAULT;
2818 return 0;
2821 case BUZIOC_REQBUFS:
2823 struct zoran_requestbuffers br;
2825 if (zr->jpg_buffers_allocated) {
2826 return -EINVAL;
2828 if (copy_from_user(&br, arg, sizeof(br))) {
2829 return -EFAULT;
2831 IOCTL_DEBUG(printk("buz ioctl BUZIOC_REQBUFS count = %lu size=%lu\n",
2832 br.count, br.size));
2833 /* Enforce reasonable lower and upper limits */
2834 if (br.count < 4)
2835 br.count = 4; /* Could be choosen smaller */
2836 if (br.count > BUZ_MAX_FRAME)
2837 br.count = BUZ_MAX_FRAME;
2838 br.size = PAGE_ALIGN(br.size);
2839 if (br.size < 8192)
2840 br.size = 8192; /* Arbitrary */
2841 /* br.size is limited by 1 page for the stat_com tables to a Maximum of 2 MB */
2842 if (br.size > (512 * 1024))
2843 br.size = (512 * 1024); /* 512 K should be enough */
2844 if (zr->need_contiguous && br.size > MAX_KMALLOC_MEM)
2845 br.size = MAX_KMALLOC_MEM;
2847 zr->jpg_nbufs = br.count;
2848 zr->jpg_bufsize = br.size;
2850 if (jpg_fbuffer_alloc(zr))
2851 return -ENOMEM;
2853 /* The next mmap will map the MJPEG buffers */
2854 zr->map_mjpeg_buffers = 1;
2856 if (copy_to_user(arg, &br, sizeof(br))) {
2857 return -EFAULT;
2859 return 0;
2862 case BUZIOC_QBUF_CAPT:
2864 int nb;
2866 if (copy_from_user((void *) &nb, (void *) arg, sizeof(int))) {
2867 return -EFAULT;
2869 IOCTL_DEBUG(printk("buz ioctl BUZIOC_QBUF_CAPT %d\n", nb));
2870 return jpg_qbuf(zr, nb, BUZ_MODE_MOTION_COMPRESS);
2873 case BUZIOC_QBUF_PLAY:
2875 int nb;
2877 if (copy_from_user((void *) &nb, (void *) arg, sizeof(int))) {
2878 return -EFAULT;
2880 IOCTL_DEBUG(printk("buz ioctl BUZIOC_QBUF_PLAY %d\n", nb));
2881 return jpg_qbuf(zr, nb, BUZ_MODE_MOTION_DECOMPRESS);
2884 case BUZIOC_SYNC:
2886 struct zoran_sync bs;
2887 int res;
2889 IOCTL_DEBUG(printk("buz ioctl BUZIOC_SYNC\n"));
2890 res = jpg_sync(zr, &bs);
2891 if (copy_to_user(arg, &bs, sizeof(bs))) {
2892 return -EFAULT;
2894 return res;
2897 case BUZIOC_G_STATUS:
2899 struct zoran_status bs;
2900 int norm, input, status;
2902 if (zr->codec_mode != BUZ_MODE_IDLE) {
2903 return -EINVAL;
2905 if (copy_from_user(&bs, arg, sizeof(bs))) {
2906 return -EFAULT;
2908 IOCTL_DEBUG(printk("buz ioctl BUZIOC_G_STATUS\n"));
2909 switch (bs.input) {
2910 case 0:
2911 input = 3;
2912 break;
2913 case 1:
2914 input = 7;
2915 break;
2916 default:
2917 return -EINVAL;
2920 /* Set video norm to VIDEO_MODE_AUTO */
2922 norm = VIDEO_MODE_AUTO;
2923 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &input);
2924 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &norm);
2926 /* sleep 1 second */
2928 schedule_timeout(HZ);
2930 /* Get status of video decoder */
2932 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_GET_STATUS, &status);
2933 bs.signal = (status & DECODER_STATUS_GOOD) ? 1 : 0;
2934 bs.norm = (status & DECODER_STATUS_NTSC) ? VIDEO_MODE_NTSC : VIDEO_MODE_PAL;
2935 bs.color = (status & DECODER_STATUS_COLOR) ? 1 : 0;
2937 /* restore previous input and norm */
2938 input = zr->params.input == 0 ? 3 : 7;
2939 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &input);
2940 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &zr->params.norm);
2942 if (copy_to_user(arg, &bs, sizeof(bs))) {
2943 return -EFAULT;
2945 return 0;
2948 default:
2949 return -ENOIOCTLCMD;
2952 return 0;
2957 * This maps the buffers to user space.
2959 * Depending on the state of zr->map_mjpeg_buffers
2960 * the V4L or the MJPEG buffers are mapped
2964 static int zoran_mmap(struct video_device *dev, const char *adr, unsigned long size)
2966 struct zoran *zr = (struct zoran *) dev;
2967 unsigned long start = (unsigned long) adr;
2968 unsigned long page, pos, todo, fraglen;
2969 int i, j;
2971 if (zr->map_mjpeg_buffers) {
2972 /* Map the MJPEG buffers */
2974 if (!zr->jpg_buffers_allocated) {
2975 return -ENOMEM;
2977 if (size > zr->jpg_nbufs * zr->jpg_bufsize) {
2978 return -EINVAL;
2981 for (i = 0; i < zr->jpg_nbufs; i++) {
2982 for (j = 0; j < zr->jpg_bufsize / PAGE_SIZE; j++) {
2983 fraglen = (zr->jpg_gbuf[i].frag_tab[2 * j + 1] & ~1) << 1;
2984 todo = size;
2985 if (todo > fraglen)
2986 todo = fraglen;
2987 pos = (unsigned long) zr->jpg_gbuf[i].frag_tab[2 * j];
2988 page = virt_to_phys(bus_to_virt(pos)); /* should just be pos on i386 */
2989 if (remap_page_range(start, page, todo, PAGE_SHARED)) {
2990 printk(KERN_ERR "%s: zoran_mmap(V4L): remap_page_range failed\n", zr->name);
2991 return -EAGAIN;
2993 size -= todo;
2994 start += todo;
2995 if (size == 0)
2996 break;
2997 if (zr->jpg_gbuf[i].frag_tab[2 * j + 1] & 1)
2998 break; /* was last fragment */
3000 if (size == 0)
3001 break;
3003 } else {
3004 /* Map the V4L buffers */
3006 if (size > v4l_nbufs * v4l_bufsize) {
3007 return -EINVAL;
3010 for (i = 0; i < v4l_nbufs; i++) {
3011 todo = size;
3012 if (todo > v4l_bufsize)
3013 todo = v4l_bufsize;
3014 page = zr->v4l_gbuf[i].fbuffer_phys;
3015 DEBUG(printk("V4L remap page range %d 0x%x %d to 0x%x\n", i, page, todo, start));
3016 if (remap_page_range(start, page, todo, PAGE_SHARED)) {
3017 printk(KERN_ERR "%s: zoran_mmap(V4L): remap_page_range failed\n", zr->name);
3018 return -EAGAIN;
3020 size -= todo;
3021 start += todo;
3022 if (size == 0)
3023 break;
3026 return 0;
3029 static int zoran_init_done(struct video_device *dev)
3031 return 0;
3034 static struct video_device zoran_template =
3036 BUZ_NAME,
3037 VID_TYPE_CAPTURE | VID_TYPE_OVERLAY | VID_TYPE_CLIPPING | VID_TYPE_FRAMERAM |
3038 VID_TYPE_SCALES | VID_TYPE_SUBCAPTURE,
3039 VID_HARDWARE_ZR36067,
3040 zoran_open,
3041 zoran_close,
3042 zoran_read,
3043 zoran_write,
3044 NULL,
3045 zoran_ioctl,
3046 zoran_mmap,
3047 zoran_init_done,
3048 NULL,
3053 static int zr36057_init(int i)
3055 struct zoran *zr = &zoran[i];
3056 unsigned long mem;
3057 unsigned mem_needed;
3058 int j;
3059 int rev;
3061 /* reset zr36057 */
3062 btwrite(0, ZR36057_SPGPPCR);
3063 mdelay(10);
3065 /* default setup of all parameters which will persist beetween opens */
3067 zr->user = 0;
3069 init_waitqueue_head(&zr->v4l_capq);
3070 init_waitqueue_head(&zr->jpg_capq);
3072 zr->map_mjpeg_buffers = 0; /* Map V4L buffers by default */
3074 zr->jpg_nbufs = 0;
3075 zr->jpg_bufsize = 0;
3076 zr->jpg_buffers_allocated = 0;
3078 zr->buffer_set = 0; /* Flag if frame buffer has been set */
3079 zr->buffer.base = (void *) vidmem;
3080 zr->buffer.width = 0;
3081 zr->buffer.height = 0;
3082 zr->buffer.depth = 0;
3083 zr->buffer.bytesperline = 0;
3085 zr->params.norm = default_norm ? 1 : 0; /* Avoid nonsense settings from user */
3086 zr->params.input = default_input ? 1 : 0; /* Avoid nonsense settings from user */
3087 zr->video_interlace = 0;
3089 /* Should the following be reset at every open ? */
3091 zr->picture.colour = 32768;
3092 zr->picture.brightness = 32768;
3093 zr->picture.hue = 32768;
3094 zr->picture.contrast = 32768;
3095 zr->picture.whiteness = 0;
3096 zr->picture.depth = 0;
3097 zr->picture.palette = 0;
3099 for (j = 0; j < VIDEO_MAX_FRAME; j++) {
3100 zr->v4l_gbuf[i].fbuffer = 0;
3101 zr->v4l_gbuf[i].fbuffer_phys = 0;
3102 zr->v4l_gbuf[i].fbuffer_bus = 0;
3105 zr->stat_com = 0;
3107 /* default setup (will be repeated at every open) */
3109 zoran_open_init_params(zr);
3111 /* allocate memory *before* doing anything to the hardware in case allocation fails */
3113 /* STAT_COM table and overlay mask */
3115 mem_needed = (BUZ_NUM_STAT_COM + ((BUZ_MAX_WIDTH + 31) / 32) * BUZ_MAX_HEIGHT) * 4;
3116 mem = (unsigned long) kmalloc(mem_needed, GFP_KERNEL);
3117 if (!mem) {
3118 return -ENOMEM;
3120 memset((void *) mem, 0, mem_needed);
3122 zr->stat_com = (u32 *) mem;
3123 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
3124 zr->stat_com[j] = 1; /* mark as unavailable to zr36057 */
3126 zr->overlay_mask = (u32 *) (mem + BUZ_NUM_STAT_COM * 4);
3128 /* Initialize zr->jpg_gbuf */
3130 for (j = 0; j < BUZ_MAX_FRAME; j++) {
3131 zr->jpg_gbuf[j].frag_tab = 0;
3132 zr->jpg_gbuf[j].frag_tab_bus = 0;
3133 zr->jpg_gbuf[j].state = BUZ_STATE_USER;
3134 zr->jpg_gbuf[j].bs.frame = j;
3137 /* take zr36057 out of reset now */
3138 btwrite(ZR36057_SPGPPCR_SoftReset, ZR36057_SPGPPCR);
3139 mdelay(10);
3141 /* stop all DMA processes */
3142 btwrite(ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
3143 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
3144 /* assert P_Reset */
3145 btwrite(0, ZR36057_JPC);
3147 switch(zr->board)
3149 case BOARD_BUZ:
3151 /* set up GPIO direction */
3152 btwrite(ZR36057_SPGPPCR_SoftReset | 0, ZR36057_SPGPPCR);
3154 /* Set up guest bus timing - Guests 0..3 Tdur=12, Trec=3 */
3155 btwrite((GPIO_MASK << 24) | 0x8888, ZR36057_GPPGCR1);
3156 mdelay(10);
3158 /* reset video decoder */
3160 GPIO(zr, 0, 0);
3161 mdelay(10);
3162 GPIO(zr, 0, 1);
3163 mdelay(10);
3165 /* reset JPEG codec */
3166 zr36060_sleep(zr, 0);
3167 mdelay(10);
3168 zr36060_reset(zr);
3169 mdelay(10);
3171 /* display codec revision */
3172 if ((rev=zr36060_read_8(zr, 0x022)) == 0x33) {
3173 printk(KERN_INFO "%s: Zoran ZR36060 (rev %d)\n",
3174 zr->name, zr36060_read_8(zr, 0x023));
3175 } else {
3176 printk(KERN_ERR "%s: Zoran ZR36060 not found (Rev=%d)\n", zr->name, rev);
3177 kfree((void *) zr->stat_com);
3178 return -1;
3180 break;
3182 case BOARD_LML33:
3183 // btwrite(btread(ZR36057_SPGPPCR)&~ZR36057_SPGPPCR_SoftReset , ZR36057_SPGPPCR);
3184 // udelay(100);
3185 // btwrite(btread(ZR36057_SPGPPCR)|ZR36057_SPGPPCR_SoftReset , ZR36057_SPGPPCR);
3186 // udelay(1000);
3189 * Set up the GPIO direction
3191 btwrite(btread(ZR36057_SPGPPCR_SoftReset)|0 , ZR36057_SPGPPCR);
3192 /* Set up guest bus timing - Guests 0..2 Tdur=12, Trec=3 */
3193 btwrite(0xFF00F888, ZR36057_GPPGCR1);
3194 mdelay(10);
3195 GPIO(zr, 5, 0); /* Analog video bypass */
3196 udelay(3000);
3197 GPIO(zr, 0, 0); /* Reset 819 */
3198 udelay(3000);
3199 GPIO(zr, 0, 1); /* 819 back */
3200 udelay(3000);
3201 /* reset JPEG codec */
3202 zr36060_sleep(zr, 0);
3203 udelay(3000);
3204 zr36060_reset(zr);
3205 udelay(3000);
3207 /* display codec revision */
3208 if ((rev=zr36060_read_8(zr, 0x022)) == 0x33) {
3209 printk(KERN_INFO "%s: Zoran ZR36060 (rev %d)\n",
3210 zr->name, zr36060_read_8(zr, 0x023));
3211 } else {
3212 printk(KERN_ERR "%s: Zoran ZR36060 not found (rev=%d)\n", zr->name, rev);
3213 kfree((void *) zr->stat_com);
3214 return -1;
3216 break;
3218 /* i2c */
3219 memcpy(&zr->i2c, &zoran_i2c_bus_template, sizeof(struct i2c_bus));
3220 sprintf(zr->i2c.name, "zoran%u", zr->id);
3221 zr->i2c.data = zr;
3222 if (i2c_register_bus(&zr->i2c) < 0) {
3223 kfree((void *) zr->stat_com);
3224 return -1;
3227 * Now add the template and register the device unit.
3229 memcpy(&zr->video_dev, &zoran_template, sizeof(zoran_template));
3230 sprintf(zr->video_dev.name, "zoran%u", zr->id);
3231 if (video_register_device(&zr->video_dev, VFL_TYPE_GRABBER) < 0) {
3232 i2c_unregister_bus(&zr->i2c);
3233 kfree((void *) zr->stat_com);
3234 return -1;
3236 /* toggle JPEG codec sleep to sync PLL */
3237 zr36060_sleep(zr, 1);
3238 mdelay(10);
3239 zr36060_sleep(zr, 0);
3240 mdelay(10);
3242 /* Enable bus-mastering */
3243 pci_set_master(zr->pci_dev);
3245 j = zr->params.input == 0 ? 3 : 7;
3246 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &j);
3247 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &zr->params.norm);
3248 i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER, ENCODER_SET_NORM, &zr->params.norm);
3250 /* set individual interrupt enables (without GIRQ0)
3251 but don't global enable until zoran_open() */
3253 btwrite(IRQ_MASK & ~ZR36057_ISR_GIRQ0, ZR36057_ICR);
3255 if(request_irq(zr->pci_dev->irq, zoran_irq,
3256 SA_SHIRQ | SA_INTERRUPT, zr->name, (void *) zr)<0)
3258 printk(KERN_ERR "%s: Can't assign irq.\n", zr->name);
3259 video_unregister_device(&zr->video_dev);
3260 i2c_unregister_bus(&zr->i2c);
3261 kfree((void *) zr->stat_com);
3262 return -1;
3264 zr->initialized = 1;
3265 return 0;
3270 static void release_zoran(void)
3272 u8 command;
3273 int i;
3274 struct zoran *zr;
3276 for (i = 0; i < zoran_num; i++) {
3277 zr = &zoran[i];
3279 if (!zr->initialized)
3280 continue;
3282 /* unregister i2c_bus */
3283 i2c_unregister_bus((&zr->i2c));
3285 /* disable PCI bus-mastering */
3286 pci_read_config_byte(zr->pci_dev, PCI_COMMAND, &command);
3287 command &= ~PCI_COMMAND_MASTER;
3288 pci_write_config_byte(zr->pci_dev, PCI_COMMAND, command);
3290 /* put chip into reset */
3291 btwrite(0, ZR36057_SPGPPCR);
3293 free_irq(zr->pci_dev->irq, zr);
3295 /* unmap and free memory */
3297 kfree((void *) zr->stat_com);
3299 iounmap(zr->zr36057_mem);
3301 video_unregister_device(&zr->video_dev);
3306 * Scan for a Buz card (actually for the PCI controller ZR36057),
3307 * request the irq and map the io memory
3310 static int find_zr36057(void)
3312 unsigned char latency;
3313 struct zoran *zr;
3314 struct pci_dev *dev = NULL;
3316 zoran_num = 0;
3318 while (zoran_num < BUZ_MAX
3319 && (dev = pci_find_device(PCI_VENDOR_ID_ZORAN, PCI_DEVICE_ID_ZORAN_36057, dev)) != NULL) {
3320 zr = &zoran[zoran_num];
3321 zr->pci_dev = dev;
3322 zr->zr36057_mem = NULL;
3323 zr->id = zoran_num;
3324 sprintf(zr->name, "zoran%u", zr->id);
3326 spin_lock_init(&zr->lock);
3328 if (pci_enable_device(dev))
3329 continue;
3331 zr->zr36057_adr = pci_resource_start(zr->pci_dev, 0);
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);
3336 } else {
3337 unsigned short ss_vendor_id, ss_id;
3339 ss_vendor_id = zr->pci_dev->subsystem_vendor;
3340 ss_id = zr->pci_dev->subsystem_device;
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);
3353 if (!zr->zr36057_mem) {
3354 printk(KERN_ERR "%s: ioremap failed\n", zr->name);
3355 /* XXX handle error */
3358 /* set PCI latency timer */
3359 pci_read_config_byte(zr->pci_dev, PCI_LATENCY_TIMER, &latency);
3360 if (latency != 48) {
3361 printk(KERN_INFO "%s: Changing PCI latency from %d to 48.\n", zr->name, latency);
3362 latency = 48;
3363 pci_write_config_byte(zr->pci_dev, PCI_LATENCY_TIMER, latency);
3365 zoran_num++;
3367 if (zoran_num == 0)
3368 printk(KERN_INFO "zoran: no cards found.\n");
3370 return zoran_num;
3373 static void handle_chipset(void)
3375 if(pci_pci_problems&PCIPCI_FAIL)
3377 printk(KERN_WARNING "buz: This configuration is known to have PCI to PCI DMA problems\n");
3378 printk(KERN_WARNING "buz: You may not be able to use overlay mode.\n");
3382 if(pci_pci_problems&PCIPCI_TRITON)
3384 printk("buz: Enabling Triton support.\n");
3385 triton = 1;
3388 if(pci_pci_problems&PCIPCI_NATOMA)
3390 printk("buz: Enabling Natoma workaround.\n");
3391 natoma = 1;
3395 #ifdef MODULE
3396 int init_module(void)
3397 #else
3398 int init_zoran_cards(struct video_init *unused)
3399 #endif
3401 int i;
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) {
3409 return -EIO;
3411 printk(KERN_INFO"zoran: %d zoran card(s) found\n", zoran_num);
3413 if (zoran_num == 0)
3414 return -ENXIO;
3417 /* check the parameters we have been given, adjust if necessary */
3419 if (v4l_nbufs < 0)
3420 v4l_nbufs = 0;
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 */
3435 if (vidmem) {
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 */
3441 handle_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);
3449 } else {
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) {
3462 release_zoran();
3463 return -EIO;
3467 return 0;
3472 #ifdef MODULE
3474 void cleanup_module(void)
3476 release_zoran();
3479 #endif