2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
6 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
8 * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
10 * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
12 * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
14 * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
19 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
21 * Iomega Buz driver version 1.0
22 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
25 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
27 * bttv - Bt848 frame grabber driver
28 * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
29 * & Marcus Metzler (mocm@thp.uni-koeln.de)
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
47 #include <linux/version.h>
48 #include <linux/init.h>
49 #include <linux/module.h>
50 #include <linux/delay.h>
51 #include <linux/slab.h>
52 #include <linux/pci.h>
53 #include <linux/vmalloc.h>
54 #include <linux/wait.h>
55 #include <linux/byteorder/generic.h>
57 #include <linux/interrupt.h>
58 #include <linux/i2c.h>
59 #include <linux/i2c-algo-bit.h>
61 #include <linux/spinlock.h>
62 #define MAP_NR(x) virt_to_page(x)
63 #define ZORAN_HARDWARE VID_HARDWARE_ZR36067
64 #define ZORAN_VID_TYPE ( \
70 VID_TYPE_MJPEG_DECODER | \
71 VID_TYPE_MJPEG_ENCODER \
74 #include <linux/videodev.h>
75 #include <media/v4l2-common.h>
76 #include "videocodec.h"
79 #include <asm/uaccess.h>
80 #include <linux/proc_fs.h>
82 #include <linux/video_decoder.h>
83 #include <linux/video_encoder.h>
84 #include <linux/mutex.h>
86 #include "zoran_device.h"
87 #include "zoran_card.h"
89 #ifdef CONFIG_VIDEO_V4L2
90 /* we declare some card type definitions here, they mean
91 * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
92 #define ZORAN_V4L2_VID_FLAGS ( \
94 V4L2_CAP_VIDEO_CAPTURE |\
95 V4L2_CAP_VIDEO_OUTPUT |\
96 V4L2_CAP_VIDEO_OVERLAY \
100 #include <asm/byteorder.h>
102 #if defined(CONFIG_VIDEO_V4L2) && defined(CONFIG_VIDEO_V4L1_COMPAT)
103 #define ZFMT(pal, fcc, cs) \
104 .palette = (pal), .fourcc = (fcc), .colorspace = (cs)
105 #elif defined(CONFIG_VIDEO_V4L2)
106 #define ZFMT(pal, fcc, cs) \
107 .fourcc = (fcc), .colorspace = (cs)
109 #define ZFMT(pal, fcc, cs) \
113 const struct zoran_format zoran_formats[] = {
115 .name = "15-bit RGB LE",
116 ZFMT(VIDEO_PALETTE_RGB555,
117 V4L2_PIX_FMT_RGB555, V4L2_COLORSPACE_SRGB),
119 .flags = ZORAN_FORMAT_CAPTURE |
120 ZORAN_FORMAT_OVERLAY,
121 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
122 ZR36057_VFESPFR_LittleEndian,
124 .name = "15-bit RGB BE",
126 V4L2_PIX_FMT_RGB555X, V4L2_COLORSPACE_SRGB),
128 .flags = ZORAN_FORMAT_CAPTURE |
129 ZORAN_FORMAT_OVERLAY,
130 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
132 .name = "16-bit RGB LE",
133 ZFMT(VIDEO_PALETTE_RGB565,
134 V4L2_PIX_FMT_RGB565, V4L2_COLORSPACE_SRGB),
136 .flags = ZORAN_FORMAT_CAPTURE |
137 ZORAN_FORMAT_OVERLAY,
138 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
139 ZR36057_VFESPFR_LittleEndian,
141 .name = "16-bit RGB BE",
143 V4L2_PIX_FMT_RGB565, V4L2_COLORSPACE_SRGB),
145 .flags = ZORAN_FORMAT_CAPTURE |
146 ZORAN_FORMAT_OVERLAY,
147 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
149 .name = "24-bit RGB",
150 ZFMT(VIDEO_PALETTE_RGB24,
151 V4L2_PIX_FMT_BGR24, V4L2_COLORSPACE_SRGB),
153 .flags = ZORAN_FORMAT_CAPTURE |
154 ZORAN_FORMAT_OVERLAY,
155 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
157 .name = "32-bit RGB LE",
158 ZFMT(VIDEO_PALETTE_RGB32,
159 V4L2_PIX_FMT_BGR32, V4L2_COLORSPACE_SRGB),
161 .flags = ZORAN_FORMAT_CAPTURE |
162 ZORAN_FORMAT_OVERLAY,
163 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
165 .name = "32-bit RGB BE",
167 V4L2_PIX_FMT_RGB32, V4L2_COLORSPACE_SRGB),
169 .flags = ZORAN_FORMAT_CAPTURE |
170 ZORAN_FORMAT_OVERLAY,
171 .vfespfr = ZR36057_VFESPFR_RGB888,
173 .name = "4:2:2, packed, YUYV",
174 ZFMT(VIDEO_PALETTE_YUV422,
175 V4L2_PIX_FMT_YUYV, V4L2_COLORSPACE_SMPTE170M),
177 .flags = ZORAN_FORMAT_CAPTURE |
178 ZORAN_FORMAT_OVERLAY,
179 .vfespfr = ZR36057_VFESPFR_YUV422,
181 .name = "4:2:2, packed, UYVY",
182 ZFMT(VIDEO_PALETTE_UYVY,
183 V4L2_PIX_FMT_UYVY, V4L2_COLORSPACE_SMPTE170M),
185 .flags = ZORAN_FORMAT_CAPTURE |
186 ZORAN_FORMAT_OVERLAY,
187 .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
189 .name = "Hardware-encoded Motion-JPEG",
191 V4L2_PIX_FMT_MJPEG, V4L2_COLORSPACE_SMPTE170M),
193 .flags = ZORAN_FORMAT_CAPTURE |
194 ZORAN_FORMAT_PLAYBACK |
195 ZORAN_FORMAT_COMPRESSED,
198 #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
200 // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
203 extern int *zr_debug;
205 #define dprintk(num, format, args...) \
207 if (*zr_debug >= num) \
208 printk(format, ##args); \
211 extern int v4l_nbufs;
212 extern int v4l_bufsize;
213 extern int jpg_nbufs;
214 extern int jpg_bufsize;
215 extern int pass_through;
217 static int lock_norm = 0; /* 1=Don't change TV standard (norm) */
218 module_param(lock_norm, int, 0);
219 MODULE_PARM_DESC(lock_norm, "Users can't change norm");
221 #ifdef CONFIG_VIDEO_V4L2
222 /* small helper function for calculating buffersizes for v4l2
223 * we calculate the nearest higher power-of-two, which
224 * will be the recommended buffersize */
226 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
228 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
229 __u32 num = (1024 * 512) / (div);
238 if (result > jpg_bufsize)
246 /* forward references */
247 static void v4l_fbuffer_free(struct file *file);
248 static void jpg_fbuffer_free(struct file *file);
251 * Allocate the V4L grab buffers
253 * These have to be pysically contiguous.
254 * If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
255 * else we try to allocate them with bigphysarea_alloc_pages
256 * if the bigphysarea patch is present in the kernel,
257 * else we try to use high memory (if the user has bootet
258 * Linux with the necessary memory left over).
262 get_high_mem (unsigned long size)
265 * Check if there is usable memory at the end of Linux memory
266 * of at least size. Return the physical address of this memory,
267 * return 0 on failure.
269 * The idea is from Alexandro Rubini's book "Linux device drivers".
270 * The driver from him which is downloadable from O'Reilly's
271 * web site misses the "virt_to_phys(high_memory)" part
272 * (and therefore doesn't work at all - at least with 2.2.x kernels).
274 * It should be unnecessary to mention that THIS IS DANGEROUS,
275 * if more than one driver at a time has the idea to use this memory!!!!
278 volatile unsigned char __iomem *mem;
280 unsigned long hi_mem_ph;
283 /* Map the high memory to user space */
285 hi_mem_ph = virt_to_phys(high_memory);
287 mem = ioremap(hi_mem_ph, size);
290 KERN_ERR "%s: get_high_mem() - ioremap failed\n",
295 for (i = 0; i < size; i++) {
296 /* Check if it is memory */
299 if (readb(mem + i) != c)
303 if (readb(mem + i) != c)
305 writeb(0, mem + i); /* zero out memory */
307 /* give the kernel air to breath */
308 if ((i & 0x3ffff) == 0x3ffff)
317 "%s: get_high_mem() - requested %lu, avail %lu\n",
318 ZORAN_NAME, size, i);
326 v4l_fbuffer_alloc (struct file *file)
328 struct zoran_fh *fh = file->private_data;
329 struct zoran *zr = fh->zr;
332 unsigned long pmem = 0;
334 /* we might have old buffers lying around... */
335 if (fh->v4l_buffers.ready_to_be_freed) {
336 v4l_fbuffer_free(file);
339 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
340 if (fh->v4l_buffers.buffer[i].fbuffer)
343 "%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
347 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
351 (unsigned char *) kmalloc(fh->v4l_buffers.
357 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
359 v4l_fbuffer_free(file);
362 fh->v4l_buffers.buffer[i].fbuffer = mem;
363 fh->v4l_buffers.buffer[i].fbuffer_phys =
365 fh->v4l_buffers.buffer[i].fbuffer_bus =
367 for (off = 0; off < fh->v4l_buffers.buffer_size;
369 SetPageReserved(MAP_NR(mem + off));
372 "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
373 ZR_DEVNAME(zr), i, (unsigned long) mem,
377 /* Use high memory which has been left at boot time */
379 /* Ok., Ok. this is an evil hack - we make
380 * the assumption that physical addresses are
381 * the same as bus addresses (true at least
382 * for Intel processors). The whole method of
383 * obtaining and using this memory is not very
384 * nice - but I hope it saves some poor users
385 * from kernel hacking, which might have even
386 * more evil results */
390 fh->v4l_buffers.num_buffers *
391 fh->v4l_buffers.buffer_size;
393 pmem = get_high_mem(size);
397 "%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
398 ZR_DEVNAME(zr), size >> 10);
401 fh->v4l_buffers.buffer[0].fbuffer = NULL;
402 fh->v4l_buffers.buffer[0].fbuffer_phys = pmem;
403 fh->v4l_buffers.buffer[0].fbuffer_bus = pmem;
406 "%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
407 ZR_DEVNAME(zr), size >> 10);
409 fh->v4l_buffers.buffer[i].fbuffer = NULL;
410 fh->v4l_buffers.buffer[i].fbuffer_phys =
411 pmem + i * fh->v4l_buffers.buffer_size;
412 fh->v4l_buffers.buffer[i].fbuffer_bus =
413 pmem + i * fh->v4l_buffers.buffer_size;
418 fh->v4l_buffers.allocated = 1;
423 /* free the V4L grab buffers */
425 v4l_fbuffer_free (struct file *file)
427 struct zoran_fh *fh = file->private_data;
428 struct zoran *zr = fh->zr;
432 dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
434 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
435 if (!fh->v4l_buffers.buffer[i].fbuffer)
438 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
439 mem = fh->v4l_buffers.buffer[i].fbuffer;
440 for (off = 0; off < fh->v4l_buffers.buffer_size;
442 ClearPageReserved(MAP_NR(mem + off));
443 kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
445 fh->v4l_buffers.buffer[i].fbuffer = NULL;
448 fh->v4l_buffers.allocated = 0;
449 fh->v4l_buffers.ready_to_be_freed = 0;
453 * Allocate the MJPEG grab buffers.
455 * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
456 * kmalloc is used to request a physically contiguous area,
457 * else we allocate the memory in framgents with get_zeroed_page.
459 * If a Natoma chipset is present and this is a revision 1 zr36057,
460 * each MJPEG buffer needs to be physically contiguous.
461 * (RJ: This statement is from Dave Perks' original driver,
462 * I could never check it because I have a zr36067)
463 * The driver cares about this because it reduces the buffer
464 * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
466 * RJ: The contents grab buffers needs never be accessed in the driver.
467 * Therefore there is no need to allocate them with vmalloc in order
468 * to get a contiguous virtual memory space.
469 * I don't understand why many other drivers first allocate them with
470 * vmalloc (which uses internally also get_zeroed_page, but delivers you
471 * virtual addresses) and then again have to make a lot of efforts
472 * to get the physical address.
475 * On big-endian architectures (such as ppc) some extra steps
476 * are needed. When reading and writing to the stat_com array
477 * and fragment buffers, the device expects to see little-
478 * endian values. The use of cpu_to_le32() and le32_to_cpu()
479 * in this function (and one or two others in zoran_device.c)
480 * ensure that these values are always stored in little-endian
481 * form, regardless of architecture. The zr36057 does Very Bad
482 * Things on big endian architectures if the stat_com array
483 * and fragment buffers are not little-endian.
487 jpg_fbuffer_alloc (struct file *file)
489 struct zoran_fh *fh = file->private_data;
490 struct zoran *zr = fh->zr;
494 /* we might have old buffers lying around */
495 if (fh->jpg_buffers.ready_to_be_freed) {
496 jpg_fbuffer_free(file);
499 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
500 if (fh->jpg_buffers.buffer[i].frag_tab)
503 "%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
506 /* Allocate fragment table for this buffer */
508 mem = get_zeroed_page(GFP_KERNEL);
512 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
514 jpg_fbuffer_free(file);
517 fh->jpg_buffers.buffer[i].frag_tab = (u32 *) mem;
518 fh->jpg_buffers.buffer[i].frag_tab_bus =
519 virt_to_bus((void *) mem);
521 //if (alloc_contig) {
522 if (fh->jpg_buffers.need_contiguous) {
524 (unsigned long) kmalloc(fh->jpg_buffers.
530 "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
532 jpg_fbuffer_free(file);
535 fh->jpg_buffers.buffer[i].frag_tab[0] =
536 cpu_to_le32(virt_to_bus((void *) mem));
537 fh->jpg_buffers.buffer[i].frag_tab[1] =
538 cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
539 for (off = 0; off < fh->jpg_buffers.buffer_size;
541 SetPageReserved(MAP_NR(mem + off));
543 /* jpg_bufsize is allreay page aligned */
545 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
547 mem = get_zeroed_page(GFP_KERNEL);
551 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
553 jpg_fbuffer_free(file);
557 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
558 cpu_to_le32(virt_to_bus((void *) mem));
559 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
561 cpu_to_le32((PAGE_SIZE / 4) << 1);
562 SetPageReserved(MAP_NR(mem));
565 fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
570 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
572 (fh->jpg_buffers.num_buffers *
573 fh->jpg_buffers.buffer_size) >> 10);
575 fh->jpg_buffers.allocated = 1;
580 /* free the MJPEG grab buffers */
582 jpg_fbuffer_free (struct file *file)
584 struct zoran_fh *fh = file->private_data;
585 struct zoran *zr = fh->zr;
589 dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
591 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
592 if (!fh->jpg_buffers.buffer[i].frag_tab)
595 //if (alloc_contig) {
596 if (fh->jpg_buffers.need_contiguous) {
597 if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
598 mem = (unsigned char *) bus_to_virt(le32_to_cpu(
599 fh->jpg_buffers.buffer[i].frag_tab[0]));
601 off < fh->jpg_buffers.buffer_size;
603 ClearPageReserved(MAP_NR
606 fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
607 fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
611 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
613 if (!fh->jpg_buffers.buffer[i].
616 ClearPageReserved(MAP_NR
620 buffer[i].frag_tab[2 *
622 free_page((unsigned long)
628 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
630 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
635 free_page((unsigned long) fh->jpg_buffers.buffer[i].
637 fh->jpg_buffers.buffer[i].frag_tab = NULL;
640 fh->jpg_buffers.allocated = 0;
641 fh->jpg_buffers.ready_to_be_freed = 0;
645 * V4L Buffer grabbing
649 zoran_v4l_set_format (struct file *file,
652 const struct zoran_format *format)
654 struct zoran_fh *fh = file->private_data;
655 struct zoran *zr = fh->zr;
658 /* Check size and format of the grab wanted */
660 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
661 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
664 "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
665 ZR_DEVNAME(zr), width, height);
669 bpp = (format->depth + 7) / 8;
671 /* Check against available buffer size */
672 if (height * width * bpp > fh->v4l_buffers.buffer_size) {
675 "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
676 ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
680 /* The video front end needs 4-byte alinged line sizes */
682 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
685 "%s: v4l_set_format() - wrong frame alingment\n",
690 fh->v4l_settings.width = width;
691 fh->v4l_settings.height = height;
692 fh->v4l_settings.format = format;
693 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
699 zoran_v4l_queue_frame (struct file *file,
702 struct zoran_fh *fh = file->private_data;
703 struct zoran *zr = fh->zr;
707 if (!fh->v4l_buffers.allocated) {
710 "%s: v4l_queue_frame() - buffers not yet allocated\n",
715 /* No grabbing outside the buffer range! */
716 if (num >= fh->v4l_buffers.num_buffers || num < 0) {
719 "%s: v4l_queue_frame() - buffer %d is out of range\n",
720 ZR_DEVNAME(zr), num);
724 spin_lock_irqsave(&zr->spinlock, flags);
726 if (fh->v4l_buffers.active == ZORAN_FREE) {
727 if (zr->v4l_buffers.active == ZORAN_FREE) {
728 zr->v4l_buffers = fh->v4l_buffers;
729 fh->v4l_buffers.active = ZORAN_ACTIVE;
733 "%s: v4l_queue_frame() - another session is already capturing\n",
739 /* make sure a grab isn't going on currently with this buffer */
741 switch (zr->v4l_buffers.buffer[num].state) {
744 if (zr->v4l_buffers.active == ZORAN_FREE) {
745 fh->v4l_buffers.active = ZORAN_FREE;
746 zr->v4l_buffers.allocated = 0;
748 res = -EBUSY; /* what are you doing? */
753 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
754 ZR_DEVNAME(zr), num);
756 /* since there is at least one unused buffer there's room for at least
757 * one more pend[] entry */
758 zr->v4l_pend[zr->v4l_pend_head++ &
759 V4L_MASK_FRAME] = num;
760 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
761 zr->v4l_buffers.buffer[num].bs.length =
762 fh->v4l_settings.bytesperline *
763 zr->v4l_settings.height;
764 fh->v4l_buffers.buffer[num] =
765 zr->v4l_buffers.buffer[num];
770 spin_unlock_irqrestore(&zr->spinlock, flags);
772 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
773 zr->v4l_buffers.active = fh->v4l_buffers.active;
779 v4l_grab (struct file *file,
780 struct video_mmap *mp)
782 struct zoran_fh *fh = file->private_data;
783 struct zoran *zr = fh->zr;
786 for (i = 0; i < NUM_FORMATS; i++) {
787 if (zoran_formats[i].palette == mp->format &&
788 zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
789 !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
792 if (i == NUM_FORMATS || zoran_formats[i].depth == 0) {
795 "%s: v4l_grab() - wrong bytes-per-pixel format\n",
801 * To minimize the time spent in the IRQ routine, we avoid setting up
802 * the video front end there.
803 * If this grab has different parameters from a running streaming capture
804 * we stop the streaming capture and start it over again.
806 if (zr->v4l_memgrab_active &&
807 (zr->v4l_settings.width != mp->width ||
808 zr->v4l_settings.height != mp->height ||
809 zr->v4l_settings.format->palette != mp->format)) {
810 res = wait_grab_pending(zr);
814 if ((res = zoran_v4l_set_format(file,
819 zr->v4l_settings = fh->v4l_settings;
821 /* queue the frame in the pending queue */
822 if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
823 fh->v4l_buffers.active = ZORAN_FREE;
827 /* put the 36057 into frame grabbing mode */
828 if (!res && !zr->v4l_memgrab_active)
829 zr36057_set_memgrab(zr, 1);
831 //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
837 * Sync on a V4L buffer
841 v4l_sync (struct file *file,
844 struct zoran_fh *fh = file->private_data;
845 struct zoran *zr = fh->zr;
848 if (fh->v4l_buffers.active == ZORAN_FREE) {
851 "%s: v4l_sync() - no grab active for this session\n",
856 /* check passed-in frame number */
857 if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
859 KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
860 ZR_DEVNAME(zr), frame);
864 /* Check if is buffer was queued at all */
865 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
868 "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
873 /* wait on this buffer to get ready */
874 if (!wait_event_interruptible_timeout(zr->v4l_capq,
875 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
878 if (signal_pending(current))
881 /* buffer should now be in BUZ_STATE_DONE */
882 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
884 KERN_ERR "%s: v4l_sync() - internal state error\n",
887 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
888 fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
890 spin_lock_irqsave(&zr->spinlock, flags);
892 /* Check if streaming capture has finished */
893 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
894 zr36057_set_memgrab(zr, 0);
895 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
896 fh->v4l_buffers.active = zr->v4l_buffers.active =
898 zr->v4l_buffers.allocated = 0;
902 spin_unlock_irqrestore(&zr->spinlock, flags);
908 * Queue a MJPEG buffer for capture/playback
912 zoran_jpg_queue_frame (struct file *file,
914 enum zoran_codec_mode mode)
916 struct zoran_fh *fh = file->private_data;
917 struct zoran *zr = fh->zr;
921 /* Check if buffers are allocated */
922 if (!fh->jpg_buffers.allocated) {
925 "%s: jpg_queue_frame() - buffers not yet allocated\n",
930 /* No grabbing outside the buffer range! */
931 if (num >= fh->jpg_buffers.num_buffers || num < 0) {
934 "%s: jpg_queue_frame() - buffer %d out of range\n",
935 ZR_DEVNAME(zr), num);
939 /* what is the codec mode right now? */
940 if (zr->codec_mode == BUZ_MODE_IDLE) {
941 zr->jpg_settings = fh->jpg_settings;
942 } else if (zr->codec_mode != mode) {
943 /* wrong codec mode active - invalid */
946 "%s: jpg_queue_frame() - codec in wrong mode\n",
951 if (fh->jpg_buffers.active == ZORAN_FREE) {
952 if (zr->jpg_buffers.active == ZORAN_FREE) {
953 zr->jpg_buffers = fh->jpg_buffers;
954 fh->jpg_buffers.active = ZORAN_ACTIVE;
958 "%s: jpg_queue_frame() - another session is already capturing\n",
964 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
965 /* Ok load up the jpeg codec */
966 zr36057_enable_jpg(zr, mode);
969 spin_lock_irqsave(&zr->spinlock, flags);
972 switch (zr->jpg_buffers.buffer[num].state) {
976 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
979 /* since there is at least one unused buffer there's room for at
980 *least one more pend[] entry */
981 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
983 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
984 fh->jpg_buffers.buffer[num] =
985 zr->jpg_buffers.buffer[num];
986 zoran_feed_stat_com(zr);
991 if (zr->jpg_buffers.active == ZORAN_FREE) {
992 fh->jpg_buffers.active = ZORAN_FREE;
993 zr->jpg_buffers.allocated = 0;
995 res = -EBUSY; /* what are you doing? */
1000 spin_unlock_irqrestore(&zr->spinlock, flags);
1002 if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
1003 zr->jpg_buffers.active = fh->jpg_buffers.active;
1010 jpg_qbuf (struct file *file,
1012 enum zoran_codec_mode mode)
1014 struct zoran_fh *fh = file->private_data;
1015 struct zoran *zr = fh->zr;
1018 /* Does the user want to stop streaming? */
1020 if (zr->codec_mode == mode) {
1021 if (fh->jpg_buffers.active == ZORAN_FREE) {
1024 "%s: jpg_qbuf(-1) - session not active\n",
1028 fh->jpg_buffers.active = zr->jpg_buffers.active =
1030 zr->jpg_buffers.allocated = 0;
1031 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1036 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
1042 if ((res = zoran_jpg_queue_frame(file, frame, mode)))
1045 /* Start the jpeg codec when the first frame is queued */
1046 if (!res && zr->jpg_que_head == 1)
1053 * Sync on a MJPEG buffer
1057 jpg_sync (struct file *file,
1058 struct zoran_sync *bs)
1060 struct zoran_fh *fh = file->private_data;
1061 struct zoran *zr = fh->zr;
1062 unsigned long flags;
1065 if (fh->jpg_buffers.active == ZORAN_FREE) {
1068 "%s: jpg_sync() - capture is not currently active\n",
1072 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
1073 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
1076 "%s: jpg_sync() - codec not in streaming mode\n",
1080 if (!wait_event_interruptible_timeout(zr->jpg_capq,
1081 (zr->jpg_que_tail != zr->jpg_dma_tail ||
1082 zr->jpg_dma_tail == zr->jpg_dma_head),
1086 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1088 zr->codec->control(zr->codec, CODEC_G_STATUS,
1092 "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
1093 ZR_DEVNAME(zr), isr);
1098 if (signal_pending(current))
1099 return -ERESTARTSYS;
1101 spin_lock_irqsave(&zr->spinlock, flags);
1103 if (zr->jpg_dma_tail != zr->jpg_dma_head)
1104 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
1106 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
1108 /* buffer should now be in BUZ_STATE_DONE */
1110 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
1113 "%s: jpg_sync() - internal state error\n",
1116 *bs = zr->jpg_buffers.buffer[frame].bs;
1118 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
1119 fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
1121 spin_unlock_irqrestore(&zr->spinlock, flags);
1127 zoran_open_init_session (struct file *file)
1130 struct zoran_fh *fh = file->private_data;
1131 struct zoran *zr = fh->zr;
1133 /* Per default, map the V4L Buffers */
1134 fh->map_mode = ZORAN_MAP_MODE_RAW;
1136 /* take over the card's current settings */
1137 fh->overlay_settings = zr->overlay_settings;
1138 fh->overlay_settings.is_set = 0;
1139 fh->overlay_settings.format = zr->overlay_settings.format;
1140 fh->overlay_active = ZORAN_FREE;
1143 fh->v4l_settings = zr->v4l_settings;
1146 memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
1147 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1148 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1149 fh->v4l_buffers.buffer[i].bs.frame = i;
1151 fh->v4l_buffers.allocated = 0;
1152 fh->v4l_buffers.ready_to_be_freed = 0;
1153 fh->v4l_buffers.active = ZORAN_FREE;
1154 fh->v4l_buffers.buffer_size = v4l_bufsize;
1155 fh->v4l_buffers.num_buffers = v4l_nbufs;
1158 fh->jpg_settings = zr->jpg_settings;
1161 memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
1162 for (i = 0; i < BUZ_MAX_FRAME; i++) {
1163 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1164 fh->jpg_buffers.buffer[i].bs.frame = i;
1166 fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
1167 fh->jpg_buffers.allocated = 0;
1168 fh->jpg_buffers.ready_to_be_freed = 0;
1169 fh->jpg_buffers.active = ZORAN_FREE;
1170 fh->jpg_buffers.buffer_size = jpg_bufsize;
1171 fh->jpg_buffers.num_buffers = jpg_nbufs;
1175 zoran_close_end_session (struct file *file)
1177 struct zoran_fh *fh = file->private_data;
1178 struct zoran *zr = fh->zr;
1181 if (fh->overlay_active != ZORAN_FREE) {
1182 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
1183 zr->v4l_overlay_active = 0;
1184 if (!zr->v4l_memgrab_active)
1185 zr36057_overlay(zr, 0);
1186 zr->overlay_mask = NULL;
1190 if (fh->v4l_buffers.active != ZORAN_FREE) {
1193 spin_lock_irqsave(&zr->spinlock, flags);
1194 zr36057_set_memgrab(zr, 0);
1195 zr->v4l_buffers.allocated = 0;
1196 zr->v4l_buffers.active = fh->v4l_buffers.active =
1198 spin_unlock_irqrestore(&zr->spinlock, flags);
1202 if (fh->v4l_buffers.allocated ||
1203 fh->v4l_buffers.ready_to_be_freed) {
1204 v4l_fbuffer_free(file);
1208 if (fh->jpg_buffers.active != ZORAN_FREE) {
1209 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1210 zr->jpg_buffers.allocated = 0;
1211 zr->jpg_buffers.active = fh->jpg_buffers.active =
1216 if (fh->jpg_buffers.allocated ||
1217 fh->jpg_buffers.ready_to_be_freed) {
1218 jpg_fbuffer_free(file);
1223 * Open a zoran card. Right now the flags stuff is just playing
1227 zoran_open (struct inode *inode,
1230 unsigned int minor = iminor(inode);
1231 struct zoran *zr = NULL;
1232 struct zoran_fh *fh;
1233 int i, res, first_open = 0, have_module_locks = 0;
1235 /* find the device */
1236 for (i = 0; i < zoran_num; i++) {
1237 if (zoran[i].video_dev->minor == minor) {
1244 dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
1246 goto open_unlock_and_return;
1249 /* see fs/device.c - the kernel already locks during open(),
1250 * so locking ourselves only causes deadlocks */
1251 /*mutex_lock(&zr->resource_lock);*/
1255 KERN_ERR "%s: no TV decoder loaded for device!\n",
1258 goto open_unlock_and_return;
1261 /* try to grab a module lock */
1262 if (!try_module_get(THIS_MODULE)) {
1265 "%s: failed to acquire my own lock! PANIC!\n",
1268 goto open_unlock_and_return;
1270 if (!try_module_get(zr->decoder->driver->driver.owner)) {
1273 "%s: failed to grab ownership of i2c decoder\n",
1276 module_put(THIS_MODULE);
1277 goto open_unlock_and_return;
1280 !try_module_get(zr->encoder->driver->driver.owner)) {
1283 "%s: failed to grab ownership of i2c encoder\n",
1286 module_put(zr->decoder->driver->driver.owner);
1287 module_put(THIS_MODULE);
1288 goto open_unlock_and_return;
1291 have_module_locks = 1;
1293 if (zr->user >= 2048) {
1294 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1295 ZR_DEVNAME(zr), zr->user);
1297 goto open_unlock_and_return;
1300 dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1301 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1303 /* now, create the open()-specific file_ops struct */
1304 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1308 "%s: zoran_open() - allocation of zoran_fh failed\n",
1311 goto open_unlock_and_return;
1313 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1314 * on norm-change! */
1316 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1317 if (!fh->overlay_mask) {
1320 "%s: zoran_open() - allocation of overlay_mask failed\n",
1324 goto open_unlock_and_return;
1327 if (zr->user++ == 0)
1330 /*mutex_unlock(&zr->resource_lock);*/
1332 /* default setup - TODO: look at flags */
1333 if (first_open) { /* First device open */
1334 zr36057_restart(zr);
1335 zoran_open_init_params(zr);
1336 zoran_init_hardware(zr);
1338 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1341 /* set file_ops stuff */
1342 file->private_data = fh;
1344 zoran_open_init_session(file);
1348 open_unlock_and_return:
1349 /* if we grabbed locks, release them accordingly */
1350 if (have_module_locks) {
1351 module_put(zr->decoder->driver->driver.owner);
1353 module_put(zr->encoder->driver->driver.owner);
1355 module_put(THIS_MODULE);
1358 /* if there's no device found, we didn't obtain the lock either */
1360 /*mutex_unlock(&zr->resource_lock);*/
1367 zoran_close (struct inode *inode,
1370 struct zoran_fh *fh = file->private_data;
1371 struct zoran *zr = fh->zr;
1373 dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1374 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1376 /* kernel locks (fs/device.c), so don't do that ourselves
1377 * (prevents deadlocks) */
1378 /*mutex_lock(&zr->resource_lock);*/
1380 zoran_close_end_session(file);
1382 if (zr->user-- == 1) { /* Last process */
1383 /* Clean up JPEG process */
1384 wake_up_interruptible(&zr->jpg_capq);
1385 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1386 zr->jpg_buffers.allocated = 0;
1387 zr->jpg_buffers.active = ZORAN_FREE;
1389 /* disable interrupts */
1390 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1393 print_interrupts(zr);
1396 zr->v4l_overlay_active = 0;
1397 zr36057_overlay(zr, 0);
1398 zr->overlay_mask = NULL;
1401 wake_up_interruptible(&zr->v4l_capq);
1402 zr36057_set_memgrab(zr, 0);
1403 zr->v4l_buffers.allocated = 0;
1404 zr->v4l_buffers.active = ZORAN_FREE;
1405 zoran_set_pci_master(zr, 0);
1407 if (!pass_through) { /* Switch to color bar */
1408 int zero = 0, two = 2;
1409 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1410 encoder_command(zr, ENCODER_SET_INPUT, &two);
1414 file->private_data = NULL;
1415 kfree(fh->overlay_mask);
1418 /* release locks on the i2c modules */
1419 module_put(zr->decoder->driver->driver.owner);
1421 module_put(zr->encoder->driver->driver.owner);
1423 module_put(THIS_MODULE);
1425 /*mutex_unlock(&zr->resource_lock);*/
1427 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1434 zoran_read (struct file *file,
1439 /* we simply don't support read() (yet)... */
1445 zoran_write (struct file *file,
1446 const char __user *data,
1450 /* ...and the same goes for write() */
1456 setup_fbuffer (struct file *file,
1458 const struct zoran_format *fmt,
1463 struct zoran_fh *fh = file->private_data;
1464 struct zoran *zr = fh->zr;
1466 /* (Ronald) v4l/v4l2 guidelines */
1467 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1470 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1471 ALi Magik (that needs very low latency while the card needs a
1472 higher value always) */
1474 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1477 /* we need a bytesperline value, even if not given */
1479 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1482 if (zr->overlay_active) {
1483 /* dzjee... stupid users... don't even bother to turn off
1484 * overlay before changing the memory location...
1485 * normally, we would return errors here. However, one of
1486 * the tools that does this is... xawtv! and since xawtv
1487 * is used by +/- 99% of the users, we'd rather be user-
1488 * friendly and silently do as if nothing went wrong */
1491 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1493 zr36057_overlay(zr, 0);
1497 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1500 "%s: setup_fbuffer() - no valid overlay format given\n",
1504 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1507 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1508 ZR_DEVNAME(zr), width, height, bytesperline);
1511 if (bytesperline & 3) {
1514 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1515 ZR_DEVNAME(zr), bytesperline);
1519 zr->buffer.base = (void *) ((unsigned long) base & ~3);
1520 zr->buffer.height = height;
1521 zr->buffer.width = width;
1522 zr->buffer.depth = fmt->depth;
1523 zr->overlay_settings.format = fmt;
1524 zr->buffer.bytesperline = bytesperline;
1526 /* The user should set new window parameters */
1527 zr->overlay_settings.is_set = 0;
1534 setup_window (struct file *file,
1539 struct video_clip __user *clips,
1541 void __user *bitmap)
1543 struct zoran_fh *fh = file->private_data;
1544 struct zoran *zr = fh->zr;
1545 struct video_clip *vcp = NULL;
1549 if (!zr->buffer.base) {
1552 "%s: setup_window() - frame buffer has to be set first\n",
1557 if (!fh->overlay_settings.format) {
1560 "%s: setup_window() - no overlay format set\n",
1566 * The video front end needs 4-byte alinged line sizes, we correct that
1567 * silently here if necessary
1569 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1570 end = (x + width) & ~1; /* round down */
1571 x = (x + 1) & ~1; /* round up */
1575 if (zr->buffer.depth == 24) {
1576 end = (x + width) & ~3; /* round down */
1577 x = (x + 3) & ~3; /* round up */
1581 if (width > BUZ_MAX_WIDTH)
1582 width = BUZ_MAX_WIDTH;
1583 if (height > BUZ_MAX_HEIGHT)
1584 height = BUZ_MAX_HEIGHT;
1586 /* Check for vaild parameters */
1587 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1588 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1591 "%s: setup_window() - width = %d or height = %d invalid\n",
1592 ZR_DEVNAME(zr), width, height);
1596 fh->overlay_settings.x = x;
1597 fh->overlay_settings.y = y;
1598 fh->overlay_settings.width = width;
1599 fh->overlay_settings.height = height;
1600 fh->overlay_settings.clipcount = clipcount;
1603 * If an overlay is running, we have to switch it off
1604 * and switch it on again in order to get the new settings in effect.
1606 * We also want to avoid that the overlay mask is written
1607 * when an overlay is running.
1610 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1611 zr->overlay_active != ZORAN_FREE &&
1612 fh->overlay_active != ZORAN_FREE;
1614 zr36057_overlay(zr, 0);
1617 * Write the overlay mask if clips are wanted.
1618 * We prefer a bitmap.
1621 /* fake value - it just means we want clips */
1622 fh->overlay_settings.clipcount = 1;
1624 if (copy_from_user(fh->overlay_mask, bitmap,
1625 (width * height + 7) / 8)) {
1628 } else if (clipcount > 0) {
1629 /* write our own bitmap from the clips */
1630 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1634 "%s: setup_window() - Alloc of clip mask failed\n",
1639 (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1643 write_overlay_mask(file, vcp, clipcount);
1647 fh->overlay_settings.is_set = 1;
1648 if (fh->overlay_active != ZORAN_FREE &&
1649 zr->overlay_active != ZORAN_FREE)
1650 zr->overlay_settings = fh->overlay_settings;
1653 zr36057_overlay(zr, 1);
1655 /* Make sure the changes come into effect */
1656 return wait_grab_pending(zr);
1660 setup_overlay (struct file *file,
1663 struct zoran_fh *fh = file->private_data;
1664 struct zoran *zr = fh->zr;
1666 /* If there is nothing to do, return immediatly */
1667 if ((on && fh->overlay_active != ZORAN_FREE) ||
1668 (!on && fh->overlay_active == ZORAN_FREE))
1671 /* check whether we're touching someone else's overlay */
1672 if (on && zr->overlay_active != ZORAN_FREE &&
1673 fh->overlay_active == ZORAN_FREE) {
1676 "%s: setup_overlay() - overlay is already active for another session\n",
1680 if (!on && zr->overlay_active != ZORAN_FREE &&
1681 fh->overlay_active == ZORAN_FREE) {
1684 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1690 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1691 zr->v4l_overlay_active = 0;
1692 /* When a grab is running, the video simply
1693 * won't be switched on any more */
1694 if (!zr->v4l_memgrab_active)
1695 zr36057_overlay(zr, 0);
1696 zr->overlay_mask = NULL;
1698 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1701 "%s: setup_overlay() - buffer or window not set\n",
1705 if (!fh->overlay_settings.format) {
1708 "%s: setup_overlay() - no overlay format set\n",
1712 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1713 zr->v4l_overlay_active = 1;
1714 zr->overlay_mask = fh->overlay_mask;
1715 zr->overlay_settings = fh->overlay_settings;
1716 if (!zr->v4l_memgrab_active)
1717 zr36057_overlay(zr, 1);
1718 /* When a grab is running, the video will be
1719 * switched on when grab is finished */
1722 /* Make sure the changes come into effect */
1723 return wait_grab_pending(zr);
1726 #ifdef CONFIG_VIDEO_V4L2
1727 /* get the status of a buffer in the clients buffer queue */
1729 zoran_v4l2_buffer_status (struct file *file,
1730 struct v4l2_buffer *buf,
1733 struct zoran_fh *fh = file->private_data;
1734 struct zoran *zr = fh->zr;
1736 buf->flags = V4L2_BUF_FLAG_MAPPED;
1738 switch (fh->map_mode) {
1739 case ZORAN_MAP_MODE_RAW:
1742 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1743 !fh->v4l_buffers.allocated) {
1746 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1751 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1752 buf->length = fh->v4l_buffers.buffer_size;
1755 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1756 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1757 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1758 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1759 buf->flags |= V4L2_BUF_FLAG_DONE;
1761 fh->v4l_buffers.buffer[num].bs.timestamp;
1763 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1766 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1767 buf->field = V4L2_FIELD_TOP;
1769 buf->field = V4L2_FIELD_INTERLACED;
1773 case ZORAN_MAP_MODE_JPG_REC:
1774 case ZORAN_MAP_MODE_JPG_PLAY:
1777 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1778 !fh->jpg_buffers.allocated) {
1781 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1786 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1787 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1788 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1789 buf->length = fh->jpg_buffers.buffer_size;
1791 /* these variables are only written after frame has been captured */
1792 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1793 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1794 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1796 fh->jpg_buffers.buffer[num].bs.timestamp;
1798 fh->jpg_buffers.buffer[num].bs.length;
1799 buf->flags |= V4L2_BUF_FLAG_DONE;
1801 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1804 /* which fields are these? */
1805 if (fh->jpg_settings.TmpDcm != 1)
1808 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1812 odd_even ? V4L2_FIELD_SEQ_TB :
1821 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1822 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1826 buf->memory = V4L2_MEMORY_MMAP;
1828 buf->m.offset = buf->length * num;
1835 zoran_set_norm (struct zoran *zr,
1836 int norm) /* VIDEO_MODE_* */
1838 int norm_encoder, on;
1840 if (zr->v4l_buffers.active != ZORAN_FREE ||
1841 zr->jpg_buffers.active != ZORAN_FREE) {
1844 "%s: set_norm() called while in playback/capture mode\n",
1849 if (lock_norm && norm != zr->norm) {
1850 if (lock_norm > 1) {
1853 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1859 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1865 if (norm != VIDEO_MODE_AUTO &&
1866 (norm < 0 || norm >= zr->card.norms ||
1867 !zr->card.tvn[norm])) {
1869 KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1870 ZR_DEVNAME(zr), norm);
1874 if (norm == VIDEO_MODE_AUTO) {
1877 /* if we have autodetect, ... */
1878 struct video_decoder_capability caps;
1879 decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1880 if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1881 dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1886 decoder_command(zr, DECODER_SET_NORM, &norm);
1888 /* let changes come into effect */
1891 decoder_command(zr, DECODER_GET_STATUS, &status);
1892 if (!(status & DECODER_STATUS_GOOD)) {
1895 "%s: set_norm() - no norm detected\n",
1898 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1902 if (status & DECODER_STATUS_NTSC)
1903 norm = VIDEO_MODE_NTSC;
1904 else if (status & DECODER_STATUS_SECAM)
1905 norm = VIDEO_MODE_SECAM;
1907 norm = VIDEO_MODE_PAL;
1909 zr->timing = zr->card.tvn[norm];
1910 norm_encoder = norm;
1912 /* We switch overlay off and on since a change in the
1913 * norm needs different VFE settings */
1914 on = zr->overlay_active && !zr->v4l_memgrab_active;
1916 zr36057_overlay(zr, 0);
1918 decoder_command(zr, DECODER_SET_NORM, &norm);
1919 encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1922 zr36057_overlay(zr, 1);
1924 /* Make sure the changes come into effect */
1931 zoran_set_input (struct zoran *zr,
1936 if (input == zr->input) {
1940 if (zr->v4l_buffers.active != ZORAN_FREE ||
1941 zr->jpg_buffers.active != ZORAN_FREE) {
1944 "%s: set_input() called while in playback/capture mode\n",
1949 if (input < 0 || input >= zr->card.inputs) {
1952 "%s: set_input() - unnsupported input %d\n",
1953 ZR_DEVNAME(zr), input);
1957 realinput = zr->card.input[input].muxsel;
1960 decoder_command(zr, DECODER_SET_INPUT, &realinput);
1970 zoran_do_ioctl (struct inode *inode,
1975 struct zoran_fh *fh = file->private_data;
1976 struct zoran *zr = fh->zr;
1977 /* CAREFUL: used in multiple places here */
1978 struct zoran_jpg_settings settings;
1980 /* we might have older buffers lying around... We don't want
1981 * to wait, but we do want to try cleaning them up ASAP. So
1982 * we try to obtain the lock and free them. If that fails, we
1983 * don't do anything and wait for the next turn. In the end,
1984 * zoran_close() or a new allocation will still free them...
1985 * This is just a 'the sooner the better' extra 'feature'
1987 * We don't free the buffers right on munmap() because that
1988 * causes oopses (kfree() inside munmap() oopses for no
1989 * apparent reason - it's also not reproduceable in any way,
1990 * but moving the free code outside the munmap() handler fixes
1991 * all this... If someone knows why, please explain me (Ronald)
1993 if (mutex_trylock(&zr->resource_lock)) {
1994 /* we obtained it! Let's try to free some things */
1995 if (fh->jpg_buffers.ready_to_be_freed)
1996 jpg_fbuffer_free(file);
1997 if (fh->v4l_buffers.ready_to_be_freed)
1998 v4l_fbuffer_free(file);
2000 mutex_unlock(&zr->resource_lock);
2007 struct video_capability *vcap = arg;
2009 dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
2011 memset(vcap, 0, sizeof(struct video_capability));
2012 strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name)-1);
2013 vcap->type = ZORAN_VID_TYPE;
2015 vcap->channels = zr->card.inputs;
2017 mutex_lock(&zr->resource_lock);
2018 vcap->maxwidth = BUZ_MAX_WIDTH;
2019 vcap->maxheight = BUZ_MAX_HEIGHT;
2020 vcap->minwidth = BUZ_MIN_WIDTH;
2021 vcap->minheight = BUZ_MIN_HEIGHT;
2022 mutex_unlock(&zr->resource_lock);
2030 struct video_channel *vchan = arg;
2031 int channel = vchan->channel;
2033 dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
2034 ZR_DEVNAME(zr), vchan->channel);
2036 memset(vchan, 0, sizeof(struct video_channel));
2037 if (channel > zr->card.inputs || channel < 0) {
2040 "%s: VIDIOCGCHAN on not existing channel %d\n",
2041 ZR_DEVNAME(zr), channel);
2045 strcpy(vchan->name, zr->card.input[channel].name);
2049 vchan->type = VIDEO_TYPE_CAMERA;
2050 mutex_lock(&zr->resource_lock);
2051 vchan->norm = zr->norm;
2052 mutex_unlock(&zr->resource_lock);
2053 vchan->channel = channel;
2059 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2061 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2063 * * The famos BTTV driver has it implemented with a struct video_channel argument
2064 * * and we follow it for compatibility reasons
2066 * * BTW: this is the only way the user can set the norm!
2071 struct video_channel *vchan = arg;
2076 "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
2077 ZR_DEVNAME(zr), vchan->channel, vchan->norm);
2079 mutex_lock(&zr->resource_lock);
2080 if ((res = zoran_set_input(zr, vchan->channel)))
2081 goto schan_unlock_and_return;
2082 if ((res = zoran_set_norm(zr, vchan->norm)))
2083 goto schan_unlock_and_return;
2085 /* Make sure the changes come into effect */
2086 res = wait_grab_pending(zr);
2087 schan_unlock_and_return:
2088 mutex_unlock(&zr->resource_lock);
2095 struct video_picture *vpict = arg;
2097 dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
2099 memset(vpict, 0, sizeof(struct video_picture));
2100 mutex_lock(&zr->resource_lock);
2101 vpict->hue = zr->hue;
2102 vpict->brightness = zr->brightness;
2103 vpict->contrast = zr->contrast;
2104 vpict->colour = zr->saturation;
2105 if (fh->overlay_settings.format) {
2106 vpict->depth = fh->overlay_settings.format->depth;
2107 vpict->palette = fh->overlay_settings.format->palette;
2111 mutex_unlock(&zr->resource_lock);
2119 struct video_picture *vpict = arg;
2124 "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
2125 ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
2126 vpict->colour, vpict->contrast, vpict->depth,
2129 for (i = 0; i < NUM_FORMATS; i++) {
2130 const struct zoran_format *fmt = &zoran_formats[i];
2132 if (fmt->palette != -1 &&
2133 fmt->flags & ZORAN_FORMAT_OVERLAY &&
2134 fmt->palette == vpict->palette &&
2135 fmt->depth == vpict->depth)
2138 if (i == NUM_FORMATS) {
2141 "%s: VIDIOCSPICT - Invalid palette %d\n",
2142 ZR_DEVNAME(zr), vpict->palette);
2146 mutex_lock(&zr->resource_lock);
2148 decoder_command(zr, DECODER_SET_PICTURE, vpict);
2150 zr->hue = vpict->hue;
2151 zr->contrast = vpict->contrast;
2152 zr->saturation = vpict->colour;
2153 zr->brightness = vpict->brightness;
2155 fh->overlay_settings.format = &zoran_formats[i];
2157 mutex_unlock(&zr->resource_lock);
2167 dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
2168 ZR_DEVNAME(zr), *on);
2170 mutex_lock(&zr->resource_lock);
2171 res = setup_overlay(file, *on);
2172 mutex_unlock(&zr->resource_lock);
2180 struct video_window *vwin = arg;
2182 dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
2184 memset(vwin, 0, sizeof(struct video_window));
2185 mutex_lock(&zr->resource_lock);
2186 vwin->x = fh->overlay_settings.x;
2187 vwin->y = fh->overlay_settings.y;
2188 vwin->width = fh->overlay_settings.width;
2189 vwin->height = fh->overlay_settings.height;
2190 mutex_unlock(&zr->resource_lock);
2191 vwin->clipcount = 0;
2198 struct video_window *vwin = arg;
2203 "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
2204 ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
2205 vwin->height, vwin->clipcount);
2207 mutex_lock(&zr->resource_lock);
2209 setup_window(file, vwin->x, vwin->y, vwin->width,
2210 vwin->height, vwin->clips,
2211 vwin->clipcount, NULL);
2212 mutex_unlock(&zr->resource_lock);
2220 struct video_buffer *vbuf = arg;
2222 dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
2224 mutex_lock(&zr->resource_lock);
2226 mutex_unlock(&zr->resource_lock);
2233 struct video_buffer *vbuf = arg;
2238 "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
2239 ZR_DEVNAME(zr), vbuf->base, vbuf->width,
2240 vbuf->height, vbuf->depth, vbuf->bytesperline);
2242 for (i = 0; i < NUM_FORMATS; i++)
2243 if (zoran_formats[i].depth == vbuf->depth)
2245 if (i == NUM_FORMATS) {
2248 "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
2249 ZR_DEVNAME(zr), vbuf->depth);
2253 mutex_lock(&zr->resource_lock);
2255 setup_fbuffer(file, vbuf->base, &zoran_formats[i],
2256 vbuf->width, vbuf->height,
2257 vbuf->bytesperline);
2258 mutex_unlock(&zr->resource_lock);
2266 int *frame = arg, res;
2268 dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
2269 ZR_DEVNAME(zr), *frame);
2271 mutex_lock(&zr->resource_lock);
2272 res = v4l_sync(file, *frame);
2273 mutex_unlock(&zr->resource_lock);
2275 zr->v4l_sync_tail++;
2280 case VIDIOCMCAPTURE:
2282 struct video_mmap *vmap = arg;
2287 "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
2288 ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
2291 mutex_lock(&zr->resource_lock);
2292 res = v4l_grab(file, vmap);
2293 mutex_unlock(&zr->resource_lock);
2300 struct video_mbuf *vmbuf = arg;
2303 dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
2306 fh->v4l_buffers.num_buffers *
2307 fh->v4l_buffers.buffer_size;
2308 vmbuf->frames = fh->v4l_buffers.num_buffers;
2309 for (i = 0; i < vmbuf->frames; i++) {
2311 i * fh->v4l_buffers.buffer_size;
2314 mutex_lock(&zr->resource_lock);
2316 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2319 "%s: VIDIOCGMBUF - buffers already allocated\n",
2322 goto v4l1reqbuf_unlock_and_return;
2325 if (v4l_fbuffer_alloc(file)) {
2327 goto v4l1reqbuf_unlock_and_return;
2330 /* The next mmap will map the V4L buffers */
2331 fh->map_mode = ZORAN_MAP_MODE_RAW;
2332 v4l1reqbuf_unlock_and_return:
2333 mutex_unlock(&zr->resource_lock);
2341 struct video_unit *vunit = arg;
2343 dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
2345 vunit->video = zr->video_dev->minor;
2346 vunit->vbi = VIDEO_NO_UNIT;
2347 vunit->radio = VIDEO_NO_UNIT;
2348 vunit->audio = VIDEO_NO_UNIT;
2349 vunit->teletext = VIDEO_NO_UNIT;
2356 * RJ: In principal we could support subcaptures for V4L grabbing.
2357 * Not even the famous BTTV driver has them, however.
2358 * If there should be a strong demand, one could consider
2359 * to implement them.
2361 case VIDIOCGCAPTURE:
2363 dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
2369 case VIDIOCSCAPTURE:
2371 dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
2377 case BUZIOC_G_PARAMS:
2379 struct zoran_params *bparams = arg;
2381 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
2383 memset(bparams, 0, sizeof(struct zoran_params));
2384 bparams->major_version = MAJOR_VERSION;
2385 bparams->minor_version = MINOR_VERSION;
2387 mutex_lock(&zr->resource_lock);
2389 bparams->norm = zr->norm;
2390 bparams->input = zr->input;
2392 bparams->decimation = fh->jpg_settings.decimation;
2393 bparams->HorDcm = fh->jpg_settings.HorDcm;
2394 bparams->VerDcm = fh->jpg_settings.VerDcm;
2395 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
2396 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
2397 bparams->img_x = fh->jpg_settings.img_x;
2398 bparams->img_y = fh->jpg_settings.img_y;
2399 bparams->img_width = fh->jpg_settings.img_width;
2400 bparams->img_height = fh->jpg_settings.img_height;
2401 bparams->odd_even = fh->jpg_settings.odd_even;
2403 bparams->quality = fh->jpg_settings.jpg_comp.quality;
2404 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
2405 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2406 memcpy(bparams->APP_data,
2407 fh->jpg_settings.jpg_comp.APP_data,
2408 sizeof(bparams->APP_data));
2409 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
2410 memcpy(bparams->COM_data,
2411 fh->jpg_settings.jpg_comp.COM_data,
2412 sizeof(bparams->COM_data));
2413 bparams->jpeg_markers =
2414 fh->jpg_settings.jpg_comp.jpeg_markers;
2416 mutex_unlock(&zr->resource_lock);
2418 bparams->VFIFO_FB = 0;
2424 case BUZIOC_S_PARAMS:
2426 struct zoran_params *bparams = arg;
2429 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
2431 settings.decimation = bparams->decimation;
2432 settings.HorDcm = bparams->HorDcm;
2433 settings.VerDcm = bparams->VerDcm;
2434 settings.TmpDcm = bparams->TmpDcm;
2435 settings.field_per_buff = bparams->field_per_buff;
2436 settings.img_x = bparams->img_x;
2437 settings.img_y = bparams->img_y;
2438 settings.img_width = bparams->img_width;
2439 settings.img_height = bparams->img_height;
2440 settings.odd_even = bparams->odd_even;
2442 settings.jpg_comp.quality = bparams->quality;
2443 settings.jpg_comp.APPn = bparams->APPn;
2444 settings.jpg_comp.APP_len = bparams->APP_len;
2445 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
2446 sizeof(bparams->APP_data));
2447 settings.jpg_comp.COM_len = bparams->COM_len;
2448 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
2449 sizeof(bparams->COM_data));
2450 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
2452 mutex_lock(&zr->resource_lock);
2454 if (zr->codec_mode != BUZ_MODE_IDLE) {
2457 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
2460 goto sparams_unlock_and_return;
2463 /* Check the params first before overwriting our
2465 if (zoran_check_jpg_settings(zr, &settings)) {
2467 goto sparams_unlock_and_return;
2470 fh->jpg_settings = settings;
2471 sparams_unlock_and_return:
2472 mutex_unlock(&zr->resource_lock);
2478 case BUZIOC_REQBUFS:
2480 struct zoran_requestbuffers *breq = arg;
2485 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
2486 ZR_DEVNAME(zr), breq->count, breq->size);
2488 /* Enforce reasonable lower and upper limits */
2489 if (breq->count < 4)
2490 breq->count = 4; /* Could be choosen smaller */
2491 if (breq->count > jpg_nbufs)
2492 breq->count = jpg_nbufs;
2493 breq->size = PAGE_ALIGN(breq->size);
2494 if (breq->size < 8192)
2495 breq->size = 8192; /* Arbitrary */
2496 /* breq->size is limited by 1 page for the stat_com
2497 * tables to a Maximum of 2 MB */
2498 if (breq->size > jpg_bufsize)
2499 breq->size = jpg_bufsize;
2500 if (fh->jpg_buffers.need_contiguous &&
2501 breq->size > MAX_KMALLOC_MEM)
2502 breq->size = MAX_KMALLOC_MEM;
2504 mutex_lock(&zr->resource_lock);
2506 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2509 "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
2512 goto jpgreqbuf_unlock_and_return;
2515 fh->jpg_buffers.num_buffers = breq->count;
2516 fh->jpg_buffers.buffer_size = breq->size;
2518 if (jpg_fbuffer_alloc(file)) {
2520 goto jpgreqbuf_unlock_and_return;
2523 /* The next mmap will map the MJPEG buffers - could
2524 * also be *_PLAY, but it doesn't matter here */
2525 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2526 jpgreqbuf_unlock_and_return:
2527 mutex_unlock(&zr->resource_lock);
2533 case BUZIOC_QBUF_CAPT:
2535 int *frame = arg, res;
2537 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
2538 ZR_DEVNAME(zr), *frame);
2540 mutex_lock(&zr->resource_lock);
2541 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
2542 mutex_unlock(&zr->resource_lock);
2548 case BUZIOC_QBUF_PLAY:
2550 int *frame = arg, res;
2552 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
2553 ZR_DEVNAME(zr), *frame);
2555 mutex_lock(&zr->resource_lock);
2556 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
2557 mutex_unlock(&zr->resource_lock);
2565 struct zoran_sync *bsync = arg;
2568 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
2570 mutex_lock(&zr->resource_lock);
2571 res = jpg_sync(file, bsync);
2572 mutex_unlock(&zr->resource_lock);
2578 case BUZIOC_G_STATUS:
2580 struct zoran_status *bstat = arg;
2581 int norm, input, status, res = 0;
2583 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
2585 if (zr->codec_mode != BUZ_MODE_IDLE) {
2588 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
2593 input = zr->card.input[bstat->input].muxsel;
2594 norm = VIDEO_MODE_AUTO;
2596 mutex_lock(&zr->resource_lock);
2598 if (zr->codec_mode != BUZ_MODE_IDLE) {
2601 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
2604 goto gstat_unlock_and_return;
2607 decoder_command(zr, DECODER_SET_INPUT, &input);
2608 decoder_command(zr, DECODER_SET_NORM, &norm);
2610 /* sleep 1 second */
2613 /* Get status of video decoder */
2614 decoder_command(zr, DECODER_GET_STATUS, &status);
2616 /* restore previous input and norm */
2617 input = zr->card.input[zr->input].muxsel;
2618 decoder_command(zr, DECODER_SET_INPUT, &input);
2619 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
2620 gstat_unlock_and_return:
2621 mutex_unlock(&zr->resource_lock);
2625 (status & DECODER_STATUS_GOOD) ? 1 : 0;
2626 if (status & DECODER_STATUS_NTSC)
2627 bstat->norm = VIDEO_MODE_NTSC;
2628 else if (status & DECODER_STATUS_SECAM)
2629 bstat->norm = VIDEO_MODE_SECAM;
2631 bstat->norm = VIDEO_MODE_PAL;
2634 (status & DECODER_STATUS_COLOR) ? 1 : 0;
2641 #ifdef CONFIG_VIDEO_V4L2
2643 /* The new video4linux2 capture interface - much nicer than video4linux1, since
2644 * it allows for integrating the JPEG capturing calls inside standard v4l2
2647 case VIDIOC_QUERYCAP:
2649 struct v4l2_capability *cap = arg;
2651 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
2653 memset(cap, 0, sizeof(*cap));
2654 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
2655 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
2656 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2657 pci_name(zr->pci_dev));
2659 KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2661 cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2667 case VIDIOC_ENUM_FMT:
2669 struct v4l2_fmtdesc *fmt = arg;
2670 int index = fmt->index, num = -1, i, flag = 0, type =
2673 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
2674 ZR_DEVNAME(zr), fmt->index);
2676 switch (fmt->type) {
2677 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2678 flag = ZORAN_FORMAT_CAPTURE;
2680 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2681 flag = ZORAN_FORMAT_PLAYBACK;
2683 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2684 flag = ZORAN_FORMAT_OVERLAY;
2689 "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
2690 ZR_DEVNAME(zr), fmt->type);
2694 for (i = 0; i < NUM_FORMATS; i++) {
2695 if (zoran_formats[i].flags & flag)
2697 if (num == fmt->index)
2700 if (fmt->index < 0 /* late, but not too late */ ||
2704 memset(fmt, 0, sizeof(*fmt));
2707 strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
2708 fmt->pixelformat = zoran_formats[i].fourcc;
2709 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2710 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2718 struct v4l2_format *fmt = arg;
2719 int type = fmt->type;
2721 dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
2723 memset(fmt, 0, sizeof(*fmt));
2726 switch (fmt->type) {
2727 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2729 mutex_lock(&zr->resource_lock);
2731 fmt->fmt.win.w.left = fh->overlay_settings.x;
2732 fmt->fmt.win.w.top = fh->overlay_settings.y;
2733 fmt->fmt.win.w.width = fh->overlay_settings.width;
2734 fmt->fmt.win.w.height =
2735 fh->overlay_settings.height;
2736 if (fh->overlay_settings.width * 2 >
2738 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2740 fmt->fmt.win.field = V4L2_FIELD_TOP;
2742 mutex_unlock(&zr->resource_lock);
2746 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2747 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2749 mutex_lock(&zr->resource_lock);
2751 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2752 fh->map_mode == ZORAN_MAP_MODE_RAW) {
2754 fmt->fmt.pix.width =
2755 fh->v4l_settings.width;
2756 fmt->fmt.pix.height =
2757 fh->v4l_settings.height;
2758 fmt->fmt.pix.sizeimage =
2759 fh->v4l_settings.bytesperline *
2760 fh->v4l_settings.height;
2761 fmt->fmt.pix.pixelformat =
2762 fh->v4l_settings.format->fourcc;
2763 fmt->fmt.pix.colorspace =
2764 fh->v4l_settings.format->colorspace;
2765 fmt->fmt.pix.bytesperline = 0;
2766 if (BUZ_MAX_HEIGHT <
2767 (fh->v4l_settings.height * 2))
2768 fmt->fmt.pix.field =
2769 V4L2_FIELD_INTERLACED;
2771 fmt->fmt.pix.field =
2776 fmt->fmt.pix.width =
2777 fh->jpg_settings.img_width /
2778 fh->jpg_settings.HorDcm;
2779 fmt->fmt.pix.height =
2780 fh->jpg_settings.img_height /
2781 (fh->jpg_settings.VerDcm *
2782 fh->jpg_settings.TmpDcm);
2783 fmt->fmt.pix.sizeimage =
2784 zoran_v4l2_calc_bufsize(&fh->
2786 fmt->fmt.pix.pixelformat =
2788 if (fh->jpg_settings.TmpDcm == 1)
2789 fmt->fmt.pix.field =
2791 odd_even ? V4L2_FIELD_SEQ_BT :
2794 fmt->fmt.pix.field =
2796 odd_even ? V4L2_FIELD_TOP :
2799 fmt->fmt.pix.bytesperline = 0;
2800 fmt->fmt.pix.colorspace =
2801 V4L2_COLORSPACE_SMPTE170M;
2804 mutex_unlock(&zr->resource_lock);
2811 "%s: VIDIOC_G_FMT - unsupported type %d\n",
2812 ZR_DEVNAME(zr), fmt->type);
2821 struct v4l2_format *fmt = arg;
2825 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
2826 ZR_DEVNAME(zr), fmt->type);
2828 switch (fmt->type) {
2829 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2831 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2832 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2833 fmt->fmt.win.w.width,
2834 fmt->fmt.win.w.height,
2835 fmt->fmt.win.clipcount,
2836 fmt->fmt.win.bitmap);
2837 mutex_lock(&zr->resource_lock);
2839 setup_window(file, fmt->fmt.win.w.left,
2841 fmt->fmt.win.w.width,
2842 fmt->fmt.win.w.height,
2843 (struct video_clip __user *)
2845 fmt->fmt.win.clipcount,
2846 fmt->fmt.win.bitmap);
2847 mutex_unlock(&zr->resource_lock);
2851 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2852 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2855 __cpu_to_le32(fmt->fmt.pix.pixelformat);
2856 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2857 fmt->fmt.pix.width, fmt->fmt.pix.height,
2858 fmt->fmt.pix.pixelformat,
2859 (char *) &printformat);
2861 if (fmt->fmt.pix.bytesperline > 0) {
2863 KERN_ERR "%s: bpl not supported\n",
2868 /* we can be requested to do JPEG/raw playback/capture */
2870 (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2871 (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2872 fmt->fmt.pix.pixelformat ==
2873 V4L2_PIX_FMT_MJPEG))) {
2876 "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
2877 ZR_DEVNAME(zr), fmt->type,
2878 fmt->fmt.pix.pixelformat,
2879 (char *) &printformat);
2883 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
2884 mutex_lock(&zr->resource_lock);
2886 settings = fh->jpg_settings;
2888 if (fh->v4l_buffers.allocated ||
2889 fh->jpg_buffers.allocated) {
2892 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2895 goto sfmtjpg_unlock_and_return;
2898 /* we actually need to set 'real' parameters now */
2899 if ((fmt->fmt.pix.height * 2) >
2901 settings.TmpDcm = 1;
2903 settings.TmpDcm = 2;
2904 settings.decimation = 0;
2905 if (fmt->fmt.pix.height <=
2906 fh->jpg_settings.img_height / 2)
2907 settings.VerDcm = 2;
2909 settings.VerDcm = 1;
2910 if (fmt->fmt.pix.width <=
2911 fh->jpg_settings.img_width / 4)
2912 settings.HorDcm = 4;
2913 else if (fmt->fmt.pix.width <=
2914 fh->jpg_settings.img_width / 2)
2915 settings.HorDcm = 2;
2917 settings.HorDcm = 1;
2918 if (settings.TmpDcm == 1)
2919 settings.field_per_buff = 2;
2921 settings.field_per_buff = 1;
2925 zoran_check_jpg_settings(zr,
2927 goto sfmtjpg_unlock_and_return;
2929 /* it's ok, so set them */
2930 fh->jpg_settings = settings;
2932 /* tell the user what we actually did */
2933 fmt->fmt.pix.width =
2934 settings.img_width / settings.HorDcm;
2935 fmt->fmt.pix.height =
2936 settings.img_height * 2 /
2937 (settings.TmpDcm * settings.VerDcm);
2938 if (settings.TmpDcm == 1)
2939 fmt->fmt.pix.field =
2941 odd_even ? V4L2_FIELD_SEQ_TB :
2944 fmt->fmt.pix.field =
2946 odd_even ? V4L2_FIELD_TOP :
2948 fh->jpg_buffers.buffer_size =
2949 zoran_v4l2_calc_bufsize(&fh->
2951 fmt->fmt.pix.sizeimage =
2952 fh->jpg_buffers.buffer_size;
2954 /* we hereby abuse this variable to show that
2955 * we're gonna do mjpeg capture */
2958 V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2959 ZORAN_MAP_MODE_JPG_REC :
2960 ZORAN_MAP_MODE_JPG_PLAY;
2961 sfmtjpg_unlock_and_return:
2962 mutex_unlock(&zr->resource_lock);
2964 for (i = 0; i < NUM_FORMATS; i++)
2965 if (fmt->fmt.pix.pixelformat ==
2966 zoran_formats[i].fourcc)
2968 if (i == NUM_FORMATS) {
2971 "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
2973 fmt->fmt.pix.pixelformat,
2974 (char *) &printformat);
2977 mutex_lock(&zr->resource_lock);
2978 if (fh->jpg_buffers.allocated ||
2979 (fh->v4l_buffers.allocated &&
2980 fh->v4l_buffers.active !=
2984 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2987 goto sfmtv4l_unlock_and_return;
2989 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2990 fmt->fmt.pix.height =
2992 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2993 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2996 zoran_v4l_set_format(file,
3003 goto sfmtv4l_unlock_and_return;
3005 /* tell the user the
3006 * results/missing stuff */
3007 fmt->fmt.pix.sizeimage =
3008 fh->v4l_settings.height *
3009 fh->v4l_settings.bytesperline;
3010 if (BUZ_MAX_HEIGHT <
3011 (fh->v4l_settings.height * 2))
3012 fmt->fmt.pix.field =
3013 V4L2_FIELD_INTERLACED;
3015 fmt->fmt.pix.field =
3018 fh->map_mode = ZORAN_MAP_MODE_RAW;
3019 sfmtv4l_unlock_and_return:
3020 mutex_unlock(&zr->resource_lock);
3026 dprintk(3, "unsupported\n");
3029 "%s: VIDIOC_S_FMT - unsupported type %d\n",
3030 ZR_DEVNAME(zr), fmt->type);
3040 struct v4l2_framebuffer *fb = arg;
3042 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
3044 memset(fb, 0, sizeof(*fb));
3045 mutex_lock(&zr->resource_lock);
3046 fb->base = zr->buffer.base;
3047 fb->fmt.width = zr->buffer.width;
3048 fb->fmt.height = zr->buffer.height;
3049 if (zr->overlay_settings.format) {
3050 fb->fmt.pixelformat =
3051 fh->overlay_settings.format->fourcc;
3053 fb->fmt.bytesperline = zr->buffer.bytesperline;
3054 mutex_unlock(&zr->resource_lock);
3055 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
3056 fb->fmt.field = V4L2_FIELD_INTERLACED;
3057 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
3058 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3067 struct v4l2_framebuffer *fb = arg;
3068 __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
3072 "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
3073 ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
3074 fb->fmt.bytesperline, fb->fmt.pixelformat,
3075 (char *) &printformat);
3077 for (i = 0; i < NUM_FORMATS; i++)
3078 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
3080 if (i == NUM_FORMATS) {
3083 "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
3084 ZR_DEVNAME(zr), fb->fmt.pixelformat,
3085 (char *) &printformat);
3089 mutex_lock(&zr->resource_lock);
3091 setup_fbuffer(file, fb->base, &zoran_formats[i],
3092 fb->fmt.width, fb->fmt.height,
3093 fb->fmt.bytesperline);
3094 mutex_unlock(&zr->resource_lock);
3100 case VIDIOC_OVERLAY:
3104 dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
3105 ZR_DEVNAME(zr), *on);
3107 mutex_lock(&zr->resource_lock);
3108 res = setup_overlay(file, *on);
3109 mutex_unlock(&zr->resource_lock);
3115 case VIDIOC_REQBUFS:
3117 struct v4l2_requestbuffers *req = arg;
3120 dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
3121 ZR_DEVNAME(zr), req->type);
3123 if (req->memory != V4L2_MEMORY_MMAP) {
3126 "%s: only MEMORY_MMAP capture is supported, not %d\n",
3127 ZR_DEVNAME(zr), req->memory);
3131 mutex_lock(&zr->resource_lock);
3133 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
3136 "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
3139 goto v4l2reqbuf_unlock_and_return;
3142 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
3143 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3145 /* control user input */
3148 if (req->count > v4l_nbufs)
3149 req->count = v4l_nbufs;
3150 fh->v4l_buffers.num_buffers = req->count;
3152 if (v4l_fbuffer_alloc(file)) {
3154 goto v4l2reqbuf_unlock_and_return;
3157 /* The next mmap will map the V4L buffers */
3158 fh->map_mode = ZORAN_MAP_MODE_RAW;
3160 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
3161 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3163 /* we need to calculate size ourselves now */
3166 if (req->count > jpg_nbufs)
3167 req->count = jpg_nbufs;
3168 fh->jpg_buffers.num_buffers = req->count;
3169 fh->jpg_buffers.buffer_size =
3170 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3172 if (jpg_fbuffer_alloc(file)) {
3174 goto v4l2reqbuf_unlock_and_return;
3177 /* The next mmap will map the MJPEG buffers */
3178 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
3179 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
3181 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
3186 "%s: VIDIOC_REQBUFS - unknown type %d\n",
3187 ZR_DEVNAME(zr), req->type);
3189 goto v4l2reqbuf_unlock_and_return;
3191 v4l2reqbuf_unlock_and_return:
3192 mutex_unlock(&zr->resource_lock);
3198 case VIDIOC_QUERYBUF:
3200 struct v4l2_buffer *buf = arg;
3201 __u32 type = buf->type;
3202 int index = buf->index, res;
3206 "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
3207 ZR_DEVNAME(zr), buf->index, buf->type);
3209 memset(buf, 0, sizeof(buf));
3213 mutex_lock(&zr->resource_lock);
3214 res = zoran_v4l2_buffer_status(file, buf, buf->index);
3215 mutex_unlock(&zr->resource_lock);
3223 struct v4l2_buffer *buf = arg;
3224 int res = 0, codec_mode, buf_type;
3227 KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
3228 ZR_DEVNAME(zr), buf->type, buf->index);
3230 mutex_lock(&zr->resource_lock);
3232 switch (fh->map_mode) {
3233 case ZORAN_MAP_MODE_RAW:
3234 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3237 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3238 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3240 goto qbuf_unlock_and_return;
3243 res = zoran_v4l_queue_frame(file, buf->index);
3245 goto qbuf_unlock_and_return;
3246 if (!zr->v4l_memgrab_active &&
3247 fh->v4l_buffers.active == ZORAN_LOCKED)
3248 zr36057_set_memgrab(zr, 1);
3251 case ZORAN_MAP_MODE_JPG_REC:
3252 case ZORAN_MAP_MODE_JPG_PLAY:
3253 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3254 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3255 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
3257 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3258 codec_mode = BUZ_MODE_MOTION_COMPRESS;
3261 if (buf->type != buf_type) {
3264 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3265 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3267 goto qbuf_unlock_and_return;
3271 zoran_jpg_queue_frame(file, buf->index,
3274 goto qbuf_unlock_and_return;
3275 if (zr->codec_mode == BUZ_MODE_IDLE &&
3276 fh->jpg_buffers.active == ZORAN_LOCKED) {
3277 zr36057_enable_jpg(zr, codec_mode);
3284 "%s: VIDIOC_QBUF - unsupported type %d\n",
3285 ZR_DEVNAME(zr), buf->type);
3287 goto qbuf_unlock_and_return;
3289 qbuf_unlock_and_return:
3290 mutex_unlock(&zr->resource_lock);
3298 struct v4l2_buffer *buf = arg;
3299 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
3301 dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
3302 ZR_DEVNAME(zr), buf->type);
3304 mutex_lock(&zr->resource_lock);
3306 switch (fh->map_mode) {
3307 case ZORAN_MAP_MODE_RAW:
3308 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3311 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3312 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3314 goto dqbuf_unlock_and_return;
3317 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3318 if (file->f_flags & O_NONBLOCK &&
3319 zr->v4l_buffers.buffer[num].state !=
3322 goto dqbuf_unlock_and_return;
3324 res = v4l_sync(file, num);
3326 goto dqbuf_unlock_and_return;
3328 zr->v4l_sync_tail++;
3329 res = zoran_v4l2_buffer_status(file, buf, num);
3332 case ZORAN_MAP_MODE_JPG_REC:
3333 case ZORAN_MAP_MODE_JPG_PLAY:
3335 struct zoran_sync bs;
3337 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
3338 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3340 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3342 if (buf->type != buf_type) {
3345 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3346 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3348 goto dqbuf_unlock_and_return;
3353 jpg_que_tail & BUZ_MASK_FRAME];
3355 if (file->f_flags & O_NONBLOCK &&
3356 zr->jpg_buffers.buffer[num].state !=
3359 goto dqbuf_unlock_and_return;
3361 res = jpg_sync(file, &bs);
3363 goto dqbuf_unlock_and_return;
3365 zoran_v4l2_buffer_status(file, buf, bs.frame);
3372 "%s: VIDIOC_DQBUF - unsupported type %d\n",
3373 ZR_DEVNAME(zr), buf->type);
3375 goto dqbuf_unlock_and_return;
3377 dqbuf_unlock_and_return:
3378 mutex_unlock(&zr->resource_lock);
3384 case VIDIOC_STREAMON:
3388 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
3390 mutex_lock(&zr->resource_lock);
3392 switch (fh->map_mode) {
3393 case ZORAN_MAP_MODE_RAW: /* raw capture */
3394 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
3395 fh->v4l_buffers.active != ZORAN_ACTIVE) {
3397 goto strmon_unlock_and_return;
3400 zr->v4l_buffers.active = fh->v4l_buffers.active =
3402 zr->v4l_settings = fh->v4l_settings;
3404 zr->v4l_sync_tail = zr->v4l_pend_tail;
3405 if (!zr->v4l_memgrab_active &&
3406 zr->v4l_pend_head != zr->v4l_pend_tail) {
3407 zr36057_set_memgrab(zr, 1);
3411 case ZORAN_MAP_MODE_JPG_REC:
3412 case ZORAN_MAP_MODE_JPG_PLAY:
3413 /* what is the codec mode right now? */
3414 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
3415 fh->jpg_buffers.active != ZORAN_ACTIVE) {
3417 goto strmon_unlock_and_return;
3420 zr->jpg_buffers.active = fh->jpg_buffers.active =
3423 if (zr->jpg_que_head != zr->jpg_que_tail) {
3424 /* Start the jpeg codec when the first frame is queued */
3432 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
3433 ZR_DEVNAME(zr), fh->map_mode);
3435 goto strmon_unlock_and_return;
3437 strmon_unlock_and_return:
3438 mutex_unlock(&zr->resource_lock);
3444 case VIDIOC_STREAMOFF:
3448 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
3450 mutex_lock(&zr->resource_lock);
3452 switch (fh->map_mode) {
3453 case ZORAN_MAP_MODE_RAW: /* raw capture */
3454 if (fh->v4l_buffers.active == ZORAN_FREE &&
3455 zr->v4l_buffers.active != ZORAN_FREE) {
3456 res = -EPERM; /* stay off other's settings! */
3457 goto strmoff_unlock_and_return;
3459 if (zr->v4l_buffers.active == ZORAN_FREE)
3460 goto strmoff_unlock_and_return;
3462 /* unload capture */
3463 if (zr->v4l_memgrab_active) {
3466 spin_lock_irqsave(&zr->spinlock, flags);
3467 zr36057_set_memgrab(zr, 0);
3468 spin_unlock_irqrestore(&zr->spinlock, flags);
3471 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3472 zr->v4l_buffers.buffer[i].state =
3474 fh->v4l_buffers = zr->v4l_buffers;
3476 zr->v4l_buffers.active = fh->v4l_buffers.active =
3479 zr->v4l_grab_seq = 0;
3480 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
3481 zr->v4l_sync_tail = 0;
3485 case ZORAN_MAP_MODE_JPG_REC:
3486 case ZORAN_MAP_MODE_JPG_PLAY:
3487 if (fh->jpg_buffers.active == ZORAN_FREE &&
3488 zr->jpg_buffers.active != ZORAN_FREE) {
3489 res = -EPERM; /* stay off other's settings! */
3490 goto strmoff_unlock_and_return;
3492 if (zr->jpg_buffers.active == ZORAN_FREE)
3493 goto strmoff_unlock_and_return;
3498 ZORAN_MAP_MODE_JPG_REC) ?
3499 BUZ_MODE_MOTION_COMPRESS :
3500 BUZ_MODE_MOTION_DECOMPRESS);
3502 goto strmoff_unlock_and_return;
3507 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
3508 ZR_DEVNAME(zr), fh->map_mode);
3510 goto strmoff_unlock_and_return;
3512 strmoff_unlock_and_return:
3513 mutex_unlock(&zr->resource_lock);
3519 case VIDIOC_QUERYCTRL:
3521 struct v4l2_queryctrl *ctrl = arg;
3523 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
3524 ZR_DEVNAME(zr), ctrl->id);
3526 /* we only support hue/saturation/contrast/brightness */
3527 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3528 ctrl->id > V4L2_CID_HUE)
3532 memset(ctrl, 0, sizeof(*ctrl));
3537 case V4L2_CID_BRIGHTNESS:
3538 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
3540 case V4L2_CID_CONTRAST:
3541 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
3543 case V4L2_CID_SATURATION:
3544 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
3547 strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
3552 ctrl->maximum = 65535;
3554 ctrl->default_value = 32768;
3555 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
3563 struct v4l2_control *ctrl = arg;
3565 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
3566 ZR_DEVNAME(zr), ctrl->id);
3568 /* we only support hue/saturation/contrast/brightness */
3569 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3570 ctrl->id > V4L2_CID_HUE)
3573 mutex_lock(&zr->resource_lock);
3575 case V4L2_CID_BRIGHTNESS:
3576 ctrl->value = zr->brightness;
3578 case V4L2_CID_CONTRAST:
3579 ctrl->value = zr->contrast;
3581 case V4L2_CID_SATURATION:
3582 ctrl->value = zr->saturation;
3585 ctrl->value = zr->hue;
3588 mutex_unlock(&zr->resource_lock);
3596 struct v4l2_control *ctrl = arg;
3597 struct video_picture pict;
3599 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
3600 ZR_DEVNAME(zr), ctrl->id);
3602 /* we only support hue/saturation/contrast/brightness */
3603 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3604 ctrl->id > V4L2_CID_HUE)
3607 if (ctrl->value < 0 || ctrl->value > 65535) {
3610 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
3611 ZR_DEVNAME(zr), ctrl->value, ctrl->id);
3615 mutex_lock(&zr->resource_lock);
3617 case V4L2_CID_BRIGHTNESS:
3618 zr->brightness = ctrl->value;
3620 case V4L2_CID_CONTRAST:
3621 zr->contrast = ctrl->value;
3623 case V4L2_CID_SATURATION:
3624 zr->saturation = ctrl->value;
3627 zr->hue = ctrl->value;
3630 pict.brightness = zr->brightness;
3631 pict.contrast = zr->contrast;
3632 pict.colour = zr->saturation;
3635 decoder_command(zr, DECODER_SET_PICTURE, &pict);
3637 mutex_unlock(&zr->resource_lock);
3643 case VIDIOC_ENUMSTD:
3645 struct v4l2_standard *std = arg;
3647 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
3648 ZR_DEVNAME(zr), std->index);
3650 if (std->index < 0 || std->index >= (zr->card.norms + 1))
3653 int id = std->index;
3654 memset(std, 0, sizeof(*std));
3658 if (std->index == zr->card.norms) {
3659 /* if we have autodetect, ... */
3660 struct video_decoder_capability caps;
3661 decoder_command(zr, DECODER_GET_CAPABILITIES,
3663 if (caps.flags & VIDEO_DECODER_AUTO) {
3664 std->id = V4L2_STD_ALL;
3665 strncpy(std->name, "Autodetect", sizeof(std->name)-1);
3670 switch (std->index) {
3672 std->id = V4L2_STD_PAL;
3673 strncpy(std->name, "PAL", sizeof(std->name)-1);
3674 std->frameperiod.numerator = 1;
3675 std->frameperiod.denominator = 25;
3676 std->framelines = zr->card.tvn[0]->Ht;
3679 std->id = V4L2_STD_NTSC;
3680 strncpy(std->name, "NTSC", sizeof(std->name)-1);
3681 std->frameperiod.numerator = 1001;
3682 std->frameperiod.denominator = 30000;
3683 std->framelines = zr->card.tvn[1]->Ht;
3686 std->id = V4L2_STD_SECAM;
3687 strncpy(std->name, "SECAM", sizeof(std->name)-1);
3688 std->frameperiod.numerator = 1;
3689 std->frameperiod.denominator = 25;
3690 std->framelines = zr->card.tvn[2]->Ht;
3700 v4l2_std_id *std = arg;
3703 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
3705 mutex_lock(&zr->resource_lock);
3707 mutex_unlock(&zr->resource_lock);
3710 case VIDEO_MODE_PAL:
3711 *std = V4L2_STD_PAL;
3713 case VIDEO_MODE_NTSC:
3714 *std = V4L2_STD_NTSC;
3716 case VIDEO_MODE_SECAM:
3717 *std = V4L2_STD_SECAM;
3727 int norm = -1, res = 0;
3728 v4l2_std_id *std = arg;
3730 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
3731 ZR_DEVNAME(zr), (unsigned long long)*std);
3733 if ((*std & V4L2_STD_PAL) && !(*std & ~V4L2_STD_PAL))
3734 norm = VIDEO_MODE_PAL;
3735 else if ((*std & V4L2_STD_NTSC) && !(*std & ~V4L2_STD_NTSC))
3736 norm = VIDEO_MODE_NTSC;
3737 else if ((*std & V4L2_STD_SECAM) && !(*std & ~V4L2_STD_SECAM))
3738 norm = VIDEO_MODE_SECAM;
3739 else if (*std == V4L2_STD_ALL)
3740 norm = VIDEO_MODE_AUTO;
3744 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3745 ZR_DEVNAME(zr), (unsigned long long)*std);
3749 mutex_lock(&zr->resource_lock);
3750 if ((res = zoran_set_norm(zr, norm)))
3751 goto sstd_unlock_and_return;
3753 res = wait_grab_pending(zr);
3754 sstd_unlock_and_return:
3755 mutex_unlock(&zr->resource_lock);
3760 case VIDIOC_ENUMINPUT:
3762 struct v4l2_input *inp = arg;
3765 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
3766 ZR_DEVNAME(zr), inp->index);
3768 if (inp->index < 0 || inp->index >= zr->card.inputs)
3771 int id = inp->index;
3772 memset(inp, 0, sizeof(*inp));
3776 strncpy(inp->name, zr->card.input[inp->index].name,
3777 sizeof(inp->name) - 1);
3778 inp->type = V4L2_INPUT_TYPE_CAMERA;
3779 inp->std = V4L2_STD_ALL;
3781 /* Get status of video decoder */
3782 mutex_lock(&zr->resource_lock);
3783 decoder_command(zr, DECODER_GET_STATUS, &status);
3784 mutex_unlock(&zr->resource_lock);
3786 if (!(status & DECODER_STATUS_GOOD)) {
3787 inp->status |= V4L2_IN_ST_NO_POWER;
3788 inp->status |= V4L2_IN_ST_NO_SIGNAL;
3790 if (!(status & DECODER_STATUS_COLOR))
3791 inp->status |= V4L2_IN_ST_NO_COLOR;
3797 case VIDIOC_G_INPUT:
3801 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
3803 mutex_lock(&zr->resource_lock);
3805 mutex_unlock(&zr->resource_lock);
3811 case VIDIOC_S_INPUT:
3813 int *input = arg, res = 0;
3815 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
3816 ZR_DEVNAME(zr), *input);
3818 mutex_lock(&zr->resource_lock);
3819 if ((res = zoran_set_input(zr, *input)))
3820 goto sinput_unlock_and_return;
3822 /* Make sure the changes come into effect */
3823 res = wait_grab_pending(zr);
3824 sinput_unlock_and_return:
3825 mutex_unlock(&zr->resource_lock);
3830 case VIDIOC_ENUMOUTPUT:
3832 struct v4l2_output *outp = arg;
3834 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
3835 ZR_DEVNAME(zr), outp->index);
3837 if (outp->index != 0)
3840 memset(outp, 0, sizeof(*outp));
3842 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3843 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
3849 case VIDIOC_G_OUTPUT:
3853 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
3861 case VIDIOC_S_OUTPUT:
3865 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
3866 ZR_DEVNAME(zr), *output);
3875 /* cropping (sub-frame capture) */
3876 case VIDIOC_CROPCAP:
3878 struct v4l2_cropcap *cropcap = arg;
3879 int type = cropcap->type, res = 0;
3881 dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
3882 ZR_DEVNAME(zr), cropcap->type);
3884 memset(cropcap, 0, sizeof(*cropcap));
3885 cropcap->type = type;
3887 mutex_lock(&zr->resource_lock);
3889 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3890 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3891 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3894 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3897 goto cropcap_unlock_and_return;
3900 cropcap->bounds.top = cropcap->bounds.left = 0;
3901 cropcap->bounds.width = BUZ_MAX_WIDTH;
3902 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3903 cropcap->defrect.top = cropcap->defrect.left = 0;
3904 cropcap->defrect.width = BUZ_MIN_WIDTH;
3905 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3906 cropcap_unlock_and_return:
3907 mutex_unlock(&zr->resource_lock);
3914 struct v4l2_crop *crop = arg;
3915 int type = crop->type, res = 0;
3917 dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
3918 ZR_DEVNAME(zr), crop->type);
3920 memset(crop, 0, sizeof(*crop));
3923 mutex_lock(&zr->resource_lock);
3925 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3926 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3927 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3930 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3933 goto gcrop_unlock_and_return;
3936 crop->c.top = fh->jpg_settings.img_y;
3937 crop->c.left = fh->jpg_settings.img_x;
3938 crop->c.width = fh->jpg_settings.img_width;
3939 crop->c.height = fh->jpg_settings.img_height;
3941 gcrop_unlock_and_return:
3942 mutex_unlock(&zr->resource_lock);
3950 struct v4l2_crop *crop = arg;
3953 settings = fh->jpg_settings;
3957 "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
3958 ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
3959 crop->c.width, crop->c.height);
3961 mutex_lock(&zr->resource_lock);
3963 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3966 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3969 goto scrop_unlock_and_return;
3972 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3973 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3974 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3977 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3980 goto scrop_unlock_and_return;
3983 /* move into a form that we understand */
3984 settings.img_x = crop->c.left;
3985 settings.img_y = crop->c.top;
3986 settings.img_width = crop->c.width;
3987 settings.img_height = crop->c.height;
3989 /* check validity */
3990 if ((res = zoran_check_jpg_settings(zr, &settings)))
3991 goto scrop_unlock_and_return;
3994 fh->jpg_settings = settings;
3996 scrop_unlock_and_return:
3997 mutex_unlock(&zr->resource_lock);
4002 case VIDIOC_G_JPEGCOMP:
4004 struct v4l2_jpegcompression *params = arg;
4006 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
4009 memset(params, 0, sizeof(*params));
4011 mutex_lock(&zr->resource_lock);
4013 params->quality = fh->jpg_settings.jpg_comp.quality;
4014 params->APPn = fh->jpg_settings.jpg_comp.APPn;
4015 memcpy(params->APP_data,
4016 fh->jpg_settings.jpg_comp.APP_data,
4017 fh->jpg_settings.jpg_comp.APP_len);
4018 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
4019 memcpy(params->COM_data,
4020 fh->jpg_settings.jpg_comp.COM_data,
4021 fh->jpg_settings.jpg_comp.COM_len);
4022 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
4023 params->jpeg_markers =
4024 fh->jpg_settings.jpg_comp.jpeg_markers;
4026 mutex_unlock(&zr->resource_lock);
4032 case VIDIOC_S_JPEGCOMP:
4034 struct v4l2_jpegcompression *params = arg;
4037 settings = fh->jpg_settings;
4041 "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
4042 ZR_DEVNAME(zr), params->quality, params->APPn,
4043 params->APP_len, params->COM_len);
4045 settings.jpg_comp = *params;
4047 mutex_lock(&zr->resource_lock);
4049 if (fh->v4l_buffers.active != ZORAN_FREE ||
4050 fh->jpg_buffers.active != ZORAN_FREE) {
4053 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
4056 goto sjpegc_unlock_and_return;
4059 if ((res = zoran_check_jpg_settings(zr, &settings)))
4060 goto sjpegc_unlock_and_return;
4061 if (!fh->jpg_buffers.allocated)
4062 fh->jpg_buffers.buffer_size =
4063 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
4064 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
4065 sjpegc_unlock_and_return:
4066 mutex_unlock(&zr->resource_lock);
4072 case VIDIOC_QUERYSTD: /* why is this useful? */
4074 v4l2_std_id *std = arg;
4077 KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
4078 ZR_DEVNAME(zr), (unsigned long long)*std);
4080 if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
4081 *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
4082 zr->card.norms == 3)) {
4090 case VIDIOC_TRY_FMT:
4092 struct v4l2_format *fmt = arg;
4095 dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
4096 ZR_DEVNAME(zr), fmt->type);
4098 switch (fmt->type) {
4099 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4100 mutex_lock(&zr->resource_lock);
4102 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
4103 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
4104 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
4105 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
4106 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
4107 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
4108 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
4109 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
4111 mutex_unlock(&zr->resource_lock);
4114 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4115 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4116 if (fmt->fmt.pix.bytesperline > 0)
4119 mutex_lock(&zr->resource_lock);
4121 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
4122 settings = fh->jpg_settings;
4124 /* we actually need to set 'real' parameters now */
4125 if ((fmt->fmt.pix.height * 2) >
4127 settings.TmpDcm = 1;
4129 settings.TmpDcm = 2;
4130 settings.decimation = 0;
4131 if (fmt->fmt.pix.height <=
4132 fh->jpg_settings.img_height / 2)
4133 settings.VerDcm = 2;
4135 settings.VerDcm = 1;
4136 if (fmt->fmt.pix.width <=
4137 fh->jpg_settings.img_width / 4)
4138 settings.HorDcm = 4;
4139 else if (fmt->fmt.pix.width <=
4140 fh->jpg_settings.img_width / 2)
4141 settings.HorDcm = 2;
4143 settings.HorDcm = 1;
4144 if (settings.TmpDcm == 1)
4145 settings.field_per_buff = 2;
4147 settings.field_per_buff = 1;
4151 zoran_check_jpg_settings(zr,
4153 goto tryfmt_unlock_and_return;
4155 /* tell the user what we actually did */
4156 fmt->fmt.pix.width =
4157 settings.img_width / settings.HorDcm;
4158 fmt->fmt.pix.height =
4159 settings.img_height * 2 /
4160 (settings.TmpDcm * settings.VerDcm);
4161 if (settings.TmpDcm == 1)
4162 fmt->fmt.pix.field =
4164 odd_even ? V4L2_FIELD_SEQ_TB :
4167 fmt->fmt.pix.field =
4169 odd_even ? V4L2_FIELD_TOP :
4172 fmt->fmt.pix.sizeimage =
4173 zoran_v4l2_calc_bufsize(&settings);
4174 } else if (fmt->type ==
4175 V4L2_BUF_TYPE_VIDEO_CAPTURE) {
4178 for (i = 0; i < NUM_FORMATS; i++)
4179 if (zoran_formats[i].fourcc ==
4180 fmt->fmt.pix.pixelformat)
4182 if (i == NUM_FORMATS) {
4184 goto tryfmt_unlock_and_return;
4187 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
4188 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
4189 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
4190 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
4191 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
4192 fmt->fmt.pix.height =
4194 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
4195 fmt->fmt.pix.height =
4199 goto tryfmt_unlock_and_return;
4201 tryfmt_unlock_and_return:
4202 mutex_unlock(&zr->resource_lock);
4217 dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
4218 ZR_DEVNAME(zr), cmd);
4219 return -ENOIOCTLCMD;
4228 zoran_ioctl (struct inode *inode,
4233 return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
4237 zoran_poll (struct file *file,
4240 struct zoran_fh *fh = file->private_data;
4241 struct zoran *zr = fh->zr;
4243 unsigned long flags;
4245 /* we should check whether buffers are ready to be synced on
4246 * (w/o waits - O_NONBLOCK) here
4247 * if ready for read (sync), return POLLIN|POLLRDNORM,
4248 * if ready for write (sync), return POLLOUT|POLLWRNORM,
4249 * if error, return POLLERR,
4250 * if no buffers queued or so, return POLLNVAL
4253 mutex_lock(&zr->resource_lock);
4255 switch (fh->map_mode) {
4256 case ZORAN_MAP_MODE_RAW:
4257 poll_wait(file, &zr->v4l_capq, wait);
4258 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
4260 spin_lock_irqsave(&zr->spinlock, flags);
4263 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
4264 ZR_DEVNAME(zr), __FUNCTION__,
4265 "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
4266 "UPMD"[zr->v4l_buffers.buffer[frame].state],
4267 zr->v4l_pend_tail, zr->v4l_pend_head);
4268 /* Process is the one capturing? */
4269 if (fh->v4l_buffers.active != ZORAN_FREE &&
4270 /* Buffer ready to DQBUF? */
4271 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
4272 res = POLLIN | POLLRDNORM;
4273 spin_unlock_irqrestore(&zr->spinlock, flags);
4277 case ZORAN_MAP_MODE_JPG_REC:
4278 case ZORAN_MAP_MODE_JPG_PLAY:
4279 poll_wait(file, &zr->jpg_capq, wait);
4280 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
4282 spin_lock_irqsave(&zr->spinlock, flags);
4285 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
4286 ZR_DEVNAME(zr), __FUNCTION__,
4287 "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
4288 "UPMD"[zr->jpg_buffers.buffer[frame].state],
4289 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
4290 if (fh->jpg_buffers.active != ZORAN_FREE &&
4291 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
4292 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
4293 res = POLLIN | POLLRDNORM;
4295 res = POLLOUT | POLLWRNORM;
4297 spin_unlock_irqrestore(&zr->spinlock, flags);
4304 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
4305 ZR_DEVNAME(zr), fh->map_mode);
4309 mutex_unlock(&zr->resource_lock);
4316 * This maps the buffers to user space.
4318 * Depending on the state of fh->map_mode
4319 * the V4L or the MJPEG buffers are mapped
4320 * per buffer or all together
4322 * Note that we need to connect to some
4323 * unmap signal event to unmap the de-allocate
4324 * the buffer accordingly (zoran_vm_close())
4328 zoran_vm_open (struct vm_area_struct *vma)
4330 struct zoran_mapping *map = vma->vm_private_data;
4336 zoran_vm_close (struct vm_area_struct *vma)
4338 struct zoran_mapping *map = vma->vm_private_data;
4339 struct file *file = map->file;
4340 struct zoran_fh *fh = file->private_data;
4341 struct zoran *zr = fh->zr;
4345 if (map->count == 0) {
4346 switch (fh->map_mode) {
4347 case ZORAN_MAP_MODE_JPG_REC:
4348 case ZORAN_MAP_MODE_JPG_PLAY:
4350 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
4353 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
4354 if (fh->jpg_buffers.buffer[i].map == map) {
4355 fh->jpg_buffers.buffer[i].map =
4361 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
4362 if (fh->jpg_buffers.buffer[i].map)
4364 if (i == fh->jpg_buffers.num_buffers) {
4365 mutex_lock(&zr->resource_lock);
4367 if (fh->jpg_buffers.active != ZORAN_FREE) {
4368 jpg_qbuf(file, -1, zr->codec_mode);
4369 zr->jpg_buffers.allocated = 0;
4370 zr->jpg_buffers.active =
4371 fh->jpg_buffers.active =
4374 //jpg_fbuffer_free(file);
4375 fh->jpg_buffers.allocated = 0;
4376 fh->jpg_buffers.ready_to_be_freed = 1;
4378 mutex_unlock(&zr->resource_lock);
4383 case ZORAN_MAP_MODE_RAW:
4385 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
4388 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
4389 if (fh->v4l_buffers.buffer[i].map == map) {
4391 fh->v4l_buffers.buffer[i].map =
4397 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
4398 if (fh->v4l_buffers.buffer[i].map)
4400 if (i == fh->v4l_buffers.num_buffers) {
4401 mutex_lock(&zr->resource_lock);
4403 if (fh->v4l_buffers.active != ZORAN_FREE) {
4406 spin_lock_irqsave(&zr->spinlock, flags);
4407 zr36057_set_memgrab(zr, 0);
4408 zr->v4l_buffers.allocated = 0;
4409 zr->v4l_buffers.active =
4410 fh->v4l_buffers.active =
4412 spin_unlock_irqrestore(&zr->spinlock, flags);
4414 //v4l_fbuffer_free(file);
4415 fh->v4l_buffers.allocated = 0;
4416 fh->v4l_buffers.ready_to_be_freed = 1;
4418 mutex_unlock(&zr->resource_lock);
4425 "%s: munmap() - internal error - unknown map mode %d\n",
4426 ZR_DEVNAME(zr), fh->map_mode);
4433 static struct vm_operations_struct zoran_vm_ops = {
4434 .open = zoran_vm_open,
4435 .close = zoran_vm_close,
4439 zoran_mmap (struct file *file,
4440 struct vm_area_struct *vma)
4442 struct zoran_fh *fh = file->private_data;
4443 struct zoran *zr = fh->zr;
4444 unsigned long size = (vma->vm_end - vma->vm_start);
4445 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4447 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
4449 struct zoran_mapping *map;
4453 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
4455 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
4456 vma->vm_start, vma->vm_end, size);
4458 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
4459 !(vma->vm_flags & VM_WRITE)) {
4462 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
4467 switch (fh->map_mode) {
4469 case ZORAN_MAP_MODE_JPG_REC:
4470 case ZORAN_MAP_MODE_JPG_PLAY:
4473 mutex_lock(&zr->resource_lock);
4475 /* Map the MJPEG buffers */
4476 if (!fh->jpg_buffers.allocated) {
4479 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
4482 goto jpg_mmap_unlock_and_return;
4485 first = offset / fh->jpg_buffers.buffer_size;
4486 last = first - 1 + size / fh->jpg_buffers.buffer_size;
4487 if (offset % fh->jpg_buffers.buffer_size != 0 ||
4488 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
4489 last < 0 || first >= fh->jpg_buffers.num_buffers ||
4490 last >= fh->jpg_buffers.num_buffers) {
4493 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4494 ZR_DEVNAME(zr), offset, size,
4495 fh->jpg_buffers.buffer_size,
4496 fh->jpg_buffers.num_buffers);
4498 goto jpg_mmap_unlock_and_return;
4500 for (i = first; i <= last; i++) {
4501 if (fh->jpg_buffers.buffer[i].map) {
4504 "%s: mmap(MJPEG) - buffer %d already mapped\n",
4507 goto jpg_mmap_unlock_and_return;
4511 /* map these buffers (v4l_buffers[i]) */
4512 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4515 goto jpg_mmap_unlock_and_return;
4520 vma->vm_ops = &zoran_vm_ops;
4521 vma->vm_flags |= VM_DONTEXPAND;
4522 vma->vm_private_data = map;
4524 for (i = first; i <= last; i++) {
4526 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
4529 (le32_to_cpu(fh->jpg_buffers.buffer[i].
4530 frag_tab[2 * j + 1]) & ~1) << 1;
4535 le32_to_cpu((unsigned long) fh->jpg_buffers.
4536 buffer[i].frag_tab[2 * j]);
4537 /* should just be pos on i386 */
4538 page = virt_to_phys(bus_to_virt(pos))
4540 if (remap_pfn_range(vma, start, page,
4541 todo, PAGE_SHARED)) {
4544 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
4547 goto jpg_mmap_unlock_and_return;
4553 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
4554 frag_tab[2 * j + 1]) & 1)
4555 break; /* was last fragment */
4557 fh->jpg_buffers.buffer[i].map = map;
4562 jpg_mmap_unlock_and_return:
4563 mutex_unlock(&zr->resource_lock);
4567 case ZORAN_MAP_MODE_RAW:
4569 mutex_lock(&zr->resource_lock);
4571 /* Map the V4L buffers */
4572 if (!fh->v4l_buffers.allocated) {
4575 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
4578 goto v4l_mmap_unlock_and_return;
4581 first = offset / fh->v4l_buffers.buffer_size;
4582 last = first - 1 + size / fh->v4l_buffers.buffer_size;
4583 if (offset % fh->v4l_buffers.buffer_size != 0 ||
4584 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
4585 last < 0 || first >= fh->v4l_buffers.num_buffers ||
4586 last >= fh->v4l_buffers.buffer_size) {
4589 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4590 ZR_DEVNAME(zr), offset, size,
4591 fh->v4l_buffers.buffer_size,
4592 fh->v4l_buffers.num_buffers);
4594 goto v4l_mmap_unlock_and_return;
4596 for (i = first; i <= last; i++) {
4597 if (fh->v4l_buffers.buffer[i].map) {
4600 "%s: mmap(V4L) - buffer %d already mapped\n",
4603 goto v4l_mmap_unlock_and_return;
4607 /* map these buffers (v4l_buffers[i]) */
4608 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4611 goto v4l_mmap_unlock_and_return;
4616 vma->vm_ops = &zoran_vm_ops;
4617 vma->vm_flags |= VM_DONTEXPAND;
4618 vma->vm_private_data = map;
4620 for (i = first; i <= last; i++) {
4622 if (todo > fh->v4l_buffers.buffer_size)
4623 todo = fh->v4l_buffers.buffer_size;
4624 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
4625 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
4626 todo, PAGE_SHARED)) {
4629 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
4632 goto v4l_mmap_unlock_and_return;
4636 fh->v4l_buffers.buffer[i].map = map;
4640 v4l_mmap_unlock_and_return:
4641 mutex_unlock(&zr->resource_lock);
4648 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
4649 ZR_DEVNAME(zr), fh->map_mode);
4656 static const struct file_operations zoran_fops = {
4657 .owner = THIS_MODULE,
4659 .release = zoran_close,
4660 .ioctl = zoran_ioctl,
4661 .compat_ioctl = v4l_compat_ioctl32,
4662 .llseek = no_llseek,
4664 .write = zoran_write,
4669 struct video_device zoran_template __devinitdata = {
4671 .type = ZORAN_VID_TYPE,
4672 #ifdef CONFIG_VIDEO_V4L2
4673 .type2 = ZORAN_V4L2_VID_FLAGS,
4675 .hardware = ZORAN_HARDWARE,
4676 .fops = &zoran_fops,
4677 .release = &zoran_vdev_release,