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) {
1191 zr36057_set_memgrab(zr, 0);
1192 zr->v4l_buffers.allocated = 0;
1193 zr->v4l_buffers.active = fh->v4l_buffers.active =
1198 if (fh->v4l_buffers.allocated ||
1199 fh->v4l_buffers.ready_to_be_freed) {
1200 v4l_fbuffer_free(file);
1204 if (fh->jpg_buffers.active != ZORAN_FREE) {
1205 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1206 zr->jpg_buffers.allocated = 0;
1207 zr->jpg_buffers.active = fh->jpg_buffers.active =
1212 if (fh->jpg_buffers.allocated ||
1213 fh->jpg_buffers.ready_to_be_freed) {
1214 jpg_fbuffer_free(file);
1219 * Open a zoran card. Right now the flags stuff is just playing
1223 zoran_open (struct inode *inode,
1226 unsigned int minor = iminor(inode);
1227 struct zoran *zr = NULL;
1228 struct zoran_fh *fh;
1229 int i, res, first_open = 0, have_module_locks = 0;
1231 /* find the device */
1232 for (i = 0; i < zoran_num; i++) {
1233 if (zoran[i].video_dev->minor == minor) {
1240 dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
1242 goto open_unlock_and_return;
1245 /* see fs/device.c - the kernel already locks during open(),
1246 * so locking ourselves only causes deadlocks */
1247 /*mutex_lock(&zr->resource_lock);*/
1251 KERN_ERR "%s: no TV decoder loaded for device!\n",
1254 goto open_unlock_and_return;
1257 /* try to grab a module lock */
1258 if (!try_module_get(THIS_MODULE)) {
1261 "%s: failed to acquire my own lock! PANIC!\n",
1264 goto open_unlock_and_return;
1266 if (!try_module_get(zr->decoder->driver->driver.owner)) {
1269 "%s: failed to grab ownership of i2c decoder\n",
1272 module_put(THIS_MODULE);
1273 goto open_unlock_and_return;
1276 !try_module_get(zr->encoder->driver->driver.owner)) {
1279 "%s: failed to grab ownership of i2c encoder\n",
1282 module_put(zr->decoder->driver->driver.owner);
1283 module_put(THIS_MODULE);
1284 goto open_unlock_and_return;
1287 have_module_locks = 1;
1289 if (zr->user >= 2048) {
1290 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1291 ZR_DEVNAME(zr), zr->user);
1293 goto open_unlock_and_return;
1296 dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1297 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1299 /* now, create the open()-specific file_ops struct */
1300 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1304 "%s: zoran_open() - allocation of zoran_fh failed\n",
1307 goto open_unlock_and_return;
1309 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1310 * on norm-change! */
1312 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1313 if (!fh->overlay_mask) {
1316 "%s: zoran_open() - allocation of overlay_mask failed\n",
1320 goto open_unlock_and_return;
1323 if (zr->user++ == 0)
1326 /*mutex_unlock(&zr->resource_lock);*/
1328 /* default setup - TODO: look at flags */
1329 if (first_open) { /* First device open */
1330 zr36057_restart(zr);
1331 zoran_open_init_params(zr);
1332 zoran_init_hardware(zr);
1334 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1337 /* set file_ops stuff */
1338 file->private_data = fh;
1340 zoran_open_init_session(file);
1344 open_unlock_and_return:
1345 /* if we grabbed locks, release them accordingly */
1346 if (have_module_locks) {
1347 module_put(zr->decoder->driver->driver.owner);
1349 module_put(zr->encoder->driver->driver.owner);
1351 module_put(THIS_MODULE);
1354 /* if there's no device found, we didn't obtain the lock either */
1356 /*mutex_unlock(&zr->resource_lock);*/
1363 zoran_close (struct inode *inode,
1366 struct zoran_fh *fh = file->private_data;
1367 struct zoran *zr = fh->zr;
1369 dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1370 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1372 /* kernel locks (fs/device.c), so don't do that ourselves
1373 * (prevents deadlocks) */
1374 /*mutex_lock(&zr->resource_lock);*/
1376 zoran_close_end_session(file);
1378 if (zr->user-- == 1) { /* Last process */
1379 /* Clean up JPEG process */
1380 wake_up_interruptible(&zr->jpg_capq);
1381 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1382 zr->jpg_buffers.allocated = 0;
1383 zr->jpg_buffers.active = ZORAN_FREE;
1385 /* disable interrupts */
1386 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1389 print_interrupts(zr);
1392 zr->v4l_overlay_active = 0;
1393 zr36057_overlay(zr, 0);
1394 zr->overlay_mask = NULL;
1397 wake_up_interruptible(&zr->v4l_capq);
1398 zr36057_set_memgrab(zr, 0);
1399 zr->v4l_buffers.allocated = 0;
1400 zr->v4l_buffers.active = ZORAN_FREE;
1401 zoran_set_pci_master(zr, 0);
1403 if (!pass_through) { /* Switch to color bar */
1404 int zero = 0, two = 2;
1405 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1406 encoder_command(zr, ENCODER_SET_INPUT, &two);
1410 file->private_data = NULL;
1411 kfree(fh->overlay_mask);
1414 /* release locks on the i2c modules */
1415 module_put(zr->decoder->driver->driver.owner);
1417 module_put(zr->encoder->driver->driver.owner);
1419 module_put(THIS_MODULE);
1421 /*mutex_unlock(&zr->resource_lock);*/
1423 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1430 zoran_read (struct file *file,
1435 /* we simply don't support read() (yet)... */
1441 zoran_write (struct file *file,
1442 const char __user *data,
1446 /* ...and the same goes for write() */
1452 setup_fbuffer (struct file *file,
1454 const struct zoran_format *fmt,
1459 struct zoran_fh *fh = file->private_data;
1460 struct zoran *zr = fh->zr;
1462 /* (Ronald) v4l/v4l2 guidelines */
1463 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1466 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1467 ALi Magik (that needs very low latency while the card needs a
1468 higher value always) */
1470 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1473 /* we need a bytesperline value, even if not given */
1475 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1478 if (zr->overlay_active) {
1479 /* dzjee... stupid users... don't even bother to turn off
1480 * overlay before changing the memory location...
1481 * normally, we would return errors here. However, one of
1482 * the tools that does this is... xawtv! and since xawtv
1483 * is used by +/- 99% of the users, we'd rather be user-
1484 * friendly and silently do as if nothing went wrong */
1487 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1489 zr36057_overlay(zr, 0);
1493 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1496 "%s: setup_fbuffer() - no valid overlay format given\n",
1500 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1503 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1504 ZR_DEVNAME(zr), width, height, bytesperline);
1507 if (bytesperline & 3) {
1510 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1511 ZR_DEVNAME(zr), bytesperline);
1515 zr->buffer.base = (void *) ((unsigned long) base & ~3);
1516 zr->buffer.height = height;
1517 zr->buffer.width = width;
1518 zr->buffer.depth = fmt->depth;
1519 zr->overlay_settings.format = fmt;
1520 zr->buffer.bytesperline = bytesperline;
1522 /* The user should set new window parameters */
1523 zr->overlay_settings.is_set = 0;
1530 setup_window (struct file *file,
1535 struct video_clip __user *clips,
1537 void __user *bitmap)
1539 struct zoran_fh *fh = file->private_data;
1540 struct zoran *zr = fh->zr;
1541 struct video_clip *vcp = NULL;
1545 if (!zr->buffer.base) {
1548 "%s: setup_window() - frame buffer has to be set first\n",
1553 if (!fh->overlay_settings.format) {
1556 "%s: setup_window() - no overlay format set\n",
1562 * The video front end needs 4-byte alinged line sizes, we correct that
1563 * silently here if necessary
1565 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1566 end = (x + width) & ~1; /* round down */
1567 x = (x + 1) & ~1; /* round up */
1571 if (zr->buffer.depth == 24) {
1572 end = (x + width) & ~3; /* round down */
1573 x = (x + 3) & ~3; /* round up */
1577 if (width > BUZ_MAX_WIDTH)
1578 width = BUZ_MAX_WIDTH;
1579 if (height > BUZ_MAX_HEIGHT)
1580 height = BUZ_MAX_HEIGHT;
1582 /* Check for vaild parameters */
1583 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1584 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1587 "%s: setup_window() - width = %d or height = %d invalid\n",
1588 ZR_DEVNAME(zr), width, height);
1592 fh->overlay_settings.x = x;
1593 fh->overlay_settings.y = y;
1594 fh->overlay_settings.width = width;
1595 fh->overlay_settings.height = height;
1596 fh->overlay_settings.clipcount = clipcount;
1599 * If an overlay is running, we have to switch it off
1600 * and switch it on again in order to get the new settings in effect.
1602 * We also want to avoid that the overlay mask is written
1603 * when an overlay is running.
1606 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1607 zr->overlay_active != ZORAN_FREE &&
1608 fh->overlay_active != ZORAN_FREE;
1610 zr36057_overlay(zr, 0);
1613 * Write the overlay mask if clips are wanted.
1614 * We prefer a bitmap.
1617 /* fake value - it just means we want clips */
1618 fh->overlay_settings.clipcount = 1;
1620 if (copy_from_user(fh->overlay_mask, bitmap,
1621 (width * height + 7) / 8)) {
1624 } else if (clipcount > 0) {
1625 /* write our own bitmap from the clips */
1626 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1630 "%s: setup_window() - Alloc of clip mask failed\n",
1635 (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1639 write_overlay_mask(file, vcp, clipcount);
1643 fh->overlay_settings.is_set = 1;
1644 if (fh->overlay_active != ZORAN_FREE &&
1645 zr->overlay_active != ZORAN_FREE)
1646 zr->overlay_settings = fh->overlay_settings;
1649 zr36057_overlay(zr, 1);
1651 /* Make sure the changes come into effect */
1652 return wait_grab_pending(zr);
1656 setup_overlay (struct file *file,
1659 struct zoran_fh *fh = file->private_data;
1660 struct zoran *zr = fh->zr;
1662 /* If there is nothing to do, return immediatly */
1663 if ((on && fh->overlay_active != ZORAN_FREE) ||
1664 (!on && fh->overlay_active == ZORAN_FREE))
1667 /* check whether we're touching someone else's overlay */
1668 if (on && zr->overlay_active != ZORAN_FREE &&
1669 fh->overlay_active == ZORAN_FREE) {
1672 "%s: setup_overlay() - overlay is already active for another session\n",
1676 if (!on && zr->overlay_active != ZORAN_FREE &&
1677 fh->overlay_active == ZORAN_FREE) {
1680 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1686 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1687 zr->v4l_overlay_active = 0;
1688 /* When a grab is running, the video simply
1689 * won't be switched on any more */
1690 if (!zr->v4l_memgrab_active)
1691 zr36057_overlay(zr, 0);
1692 zr->overlay_mask = NULL;
1694 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1697 "%s: setup_overlay() - buffer or window not set\n",
1701 if (!fh->overlay_settings.format) {
1704 "%s: setup_overlay() - no overlay format set\n",
1708 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1709 zr->v4l_overlay_active = 1;
1710 zr->overlay_mask = fh->overlay_mask;
1711 zr->overlay_settings = fh->overlay_settings;
1712 if (!zr->v4l_memgrab_active)
1713 zr36057_overlay(zr, 1);
1714 /* When a grab is running, the video will be
1715 * switched on when grab is finished */
1718 /* Make sure the changes come into effect */
1719 return wait_grab_pending(zr);
1722 #ifdef CONFIG_VIDEO_V4L2
1723 /* get the status of a buffer in the clients buffer queue */
1725 zoran_v4l2_buffer_status (struct file *file,
1726 struct v4l2_buffer *buf,
1729 struct zoran_fh *fh = file->private_data;
1730 struct zoran *zr = fh->zr;
1732 buf->flags = V4L2_BUF_FLAG_MAPPED;
1734 switch (fh->map_mode) {
1735 case ZORAN_MAP_MODE_RAW:
1738 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1739 !fh->v4l_buffers.allocated) {
1742 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1747 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1748 buf->length = fh->v4l_buffers.buffer_size;
1751 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1752 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1753 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1754 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1755 buf->flags |= V4L2_BUF_FLAG_DONE;
1757 fh->v4l_buffers.buffer[num].bs.timestamp;
1759 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1762 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1763 buf->field = V4L2_FIELD_TOP;
1765 buf->field = V4L2_FIELD_INTERLACED;
1769 case ZORAN_MAP_MODE_JPG_REC:
1770 case ZORAN_MAP_MODE_JPG_PLAY:
1773 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1774 !fh->jpg_buffers.allocated) {
1777 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1782 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1783 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1784 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1785 buf->length = fh->jpg_buffers.buffer_size;
1787 /* these variables are only written after frame has been captured */
1788 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1789 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1790 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1792 fh->jpg_buffers.buffer[num].bs.timestamp;
1794 fh->jpg_buffers.buffer[num].bs.length;
1795 buf->flags |= V4L2_BUF_FLAG_DONE;
1797 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1800 /* which fields are these? */
1801 if (fh->jpg_settings.TmpDcm != 1)
1804 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1808 odd_even ? V4L2_FIELD_SEQ_TB :
1817 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1818 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1822 buf->memory = V4L2_MEMORY_MMAP;
1824 buf->m.offset = buf->length * num;
1831 zoran_set_norm (struct zoran *zr,
1832 int norm) /* VIDEO_MODE_* */
1834 int norm_encoder, on;
1836 if (zr->v4l_buffers.active != ZORAN_FREE ||
1837 zr->jpg_buffers.active != ZORAN_FREE) {
1840 "%s: set_norm() called while in playback/capture mode\n",
1845 if (lock_norm && norm != zr->norm) {
1846 if (lock_norm > 1) {
1849 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1855 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1861 if (norm != VIDEO_MODE_AUTO &&
1862 (norm < 0 || norm >= zr->card.norms ||
1863 !zr->card.tvn[norm])) {
1865 KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1866 ZR_DEVNAME(zr), norm);
1870 if (norm == VIDEO_MODE_AUTO) {
1873 /* if we have autodetect, ... */
1874 struct video_decoder_capability caps;
1875 decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1876 if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1877 dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1882 decoder_command(zr, DECODER_SET_NORM, &norm);
1884 /* let changes come into effect */
1887 decoder_command(zr, DECODER_GET_STATUS, &status);
1888 if (!(status & DECODER_STATUS_GOOD)) {
1891 "%s: set_norm() - no norm detected\n",
1894 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1898 if (status & DECODER_STATUS_NTSC)
1899 norm = VIDEO_MODE_NTSC;
1900 else if (status & DECODER_STATUS_SECAM)
1901 norm = VIDEO_MODE_SECAM;
1903 norm = VIDEO_MODE_PAL;
1905 zr->timing = zr->card.tvn[norm];
1906 norm_encoder = norm;
1908 /* We switch overlay off and on since a change in the
1909 * norm needs different VFE settings */
1910 on = zr->overlay_active && !zr->v4l_memgrab_active;
1912 zr36057_overlay(zr, 0);
1914 decoder_command(zr, DECODER_SET_NORM, &norm);
1915 encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1918 zr36057_overlay(zr, 1);
1920 /* Make sure the changes come into effect */
1927 zoran_set_input (struct zoran *zr,
1932 if (input == zr->input) {
1936 if (zr->v4l_buffers.active != ZORAN_FREE ||
1937 zr->jpg_buffers.active != ZORAN_FREE) {
1940 "%s: set_input() called while in playback/capture mode\n",
1945 if (input < 0 || input >= zr->card.inputs) {
1948 "%s: set_input() - unnsupported input %d\n",
1949 ZR_DEVNAME(zr), input);
1953 realinput = zr->card.input[input].muxsel;
1956 decoder_command(zr, DECODER_SET_INPUT, &realinput);
1966 zoran_do_ioctl (struct inode *inode,
1971 struct zoran_fh *fh = file->private_data;
1972 struct zoran *zr = fh->zr;
1973 /* CAREFUL: used in multiple places here */
1974 struct zoran_jpg_settings settings;
1976 /* we might have older buffers lying around... We don't want
1977 * to wait, but we do want to try cleaning them up ASAP. So
1978 * we try to obtain the lock and free them. If that fails, we
1979 * don't do anything and wait for the next turn. In the end,
1980 * zoran_close() or a new allocation will still free them...
1981 * This is just a 'the sooner the better' extra 'feature'
1983 * We don't free the buffers right on munmap() because that
1984 * causes oopses (kfree() inside munmap() oopses for no
1985 * apparent reason - it's also not reproduceable in any way,
1986 * but moving the free code outside the munmap() handler fixes
1987 * all this... If someone knows why, please explain me (Ronald)
1989 if (mutex_trylock(&zr->resource_lock)) {
1990 /* we obtained it! Let's try to free some things */
1991 if (fh->jpg_buffers.ready_to_be_freed)
1992 jpg_fbuffer_free(file);
1993 if (fh->v4l_buffers.ready_to_be_freed)
1994 v4l_fbuffer_free(file);
1996 mutex_unlock(&zr->resource_lock);
2003 struct video_capability *vcap = arg;
2005 dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
2007 memset(vcap, 0, sizeof(struct video_capability));
2008 strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name)-1);
2009 vcap->type = ZORAN_VID_TYPE;
2011 vcap->channels = zr->card.inputs;
2013 mutex_lock(&zr->resource_lock);
2014 vcap->maxwidth = BUZ_MAX_WIDTH;
2015 vcap->maxheight = BUZ_MAX_HEIGHT;
2016 vcap->minwidth = BUZ_MIN_WIDTH;
2017 vcap->minheight = BUZ_MIN_HEIGHT;
2018 mutex_unlock(&zr->resource_lock);
2026 struct video_channel *vchan = arg;
2027 int channel = vchan->channel;
2029 dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
2030 ZR_DEVNAME(zr), vchan->channel);
2032 memset(vchan, 0, sizeof(struct video_channel));
2033 if (channel > zr->card.inputs || channel < 0) {
2036 "%s: VIDIOCGCHAN on not existing channel %d\n",
2037 ZR_DEVNAME(zr), channel);
2041 strcpy(vchan->name, zr->card.input[channel].name);
2045 vchan->type = VIDEO_TYPE_CAMERA;
2046 mutex_lock(&zr->resource_lock);
2047 vchan->norm = zr->norm;
2048 mutex_unlock(&zr->resource_lock);
2049 vchan->channel = channel;
2055 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2057 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2059 * * The famos BTTV driver has it implemented with a struct video_channel argument
2060 * * and we follow it for compatibility reasons
2062 * * BTW: this is the only way the user can set the norm!
2067 struct video_channel *vchan = arg;
2072 "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
2073 ZR_DEVNAME(zr), vchan->channel, vchan->norm);
2075 mutex_lock(&zr->resource_lock);
2076 if ((res = zoran_set_input(zr, vchan->channel)))
2077 goto schan_unlock_and_return;
2078 if ((res = zoran_set_norm(zr, vchan->norm)))
2079 goto schan_unlock_and_return;
2081 /* Make sure the changes come into effect */
2082 res = wait_grab_pending(zr);
2083 schan_unlock_and_return:
2084 mutex_unlock(&zr->resource_lock);
2091 struct video_picture *vpict = arg;
2093 dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
2095 memset(vpict, 0, sizeof(struct video_picture));
2096 mutex_lock(&zr->resource_lock);
2097 vpict->hue = zr->hue;
2098 vpict->brightness = zr->brightness;
2099 vpict->contrast = zr->contrast;
2100 vpict->colour = zr->saturation;
2101 if (fh->overlay_settings.format) {
2102 vpict->depth = fh->overlay_settings.format->depth;
2103 vpict->palette = fh->overlay_settings.format->palette;
2107 mutex_unlock(&zr->resource_lock);
2115 struct video_picture *vpict = arg;
2120 "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
2121 ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
2122 vpict->colour, vpict->contrast, vpict->depth,
2125 for (i = 0; i < NUM_FORMATS; i++) {
2126 const struct zoran_format *fmt = &zoran_formats[i];
2128 if (fmt->palette != -1 &&
2129 fmt->flags & ZORAN_FORMAT_OVERLAY &&
2130 fmt->palette == vpict->palette &&
2131 fmt->depth == vpict->depth)
2134 if (i == NUM_FORMATS) {
2137 "%s: VIDIOCSPICT - Invalid palette %d\n",
2138 ZR_DEVNAME(zr), vpict->palette);
2142 mutex_lock(&zr->resource_lock);
2144 decoder_command(zr, DECODER_SET_PICTURE, vpict);
2146 zr->hue = vpict->hue;
2147 zr->contrast = vpict->contrast;
2148 zr->saturation = vpict->colour;
2149 zr->brightness = vpict->brightness;
2151 fh->overlay_settings.format = &zoran_formats[i];
2153 mutex_unlock(&zr->resource_lock);
2163 dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
2164 ZR_DEVNAME(zr), *on);
2166 mutex_lock(&zr->resource_lock);
2167 res = setup_overlay(file, *on);
2168 mutex_unlock(&zr->resource_lock);
2176 struct video_window *vwin = arg;
2178 dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
2180 memset(vwin, 0, sizeof(struct video_window));
2181 mutex_lock(&zr->resource_lock);
2182 vwin->x = fh->overlay_settings.x;
2183 vwin->y = fh->overlay_settings.y;
2184 vwin->width = fh->overlay_settings.width;
2185 vwin->height = fh->overlay_settings.height;
2186 mutex_unlock(&zr->resource_lock);
2187 vwin->clipcount = 0;
2194 struct video_window *vwin = arg;
2199 "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
2200 ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
2201 vwin->height, vwin->clipcount);
2203 mutex_lock(&zr->resource_lock);
2205 setup_window(file, vwin->x, vwin->y, vwin->width,
2206 vwin->height, vwin->clips,
2207 vwin->clipcount, NULL);
2208 mutex_unlock(&zr->resource_lock);
2216 struct video_buffer *vbuf = arg;
2218 dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
2220 mutex_lock(&zr->resource_lock);
2222 mutex_unlock(&zr->resource_lock);
2229 struct video_buffer *vbuf = arg;
2234 "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
2235 ZR_DEVNAME(zr), vbuf->base, vbuf->width,
2236 vbuf->height, vbuf->depth, vbuf->bytesperline);
2238 for (i = 0; i < NUM_FORMATS; i++)
2239 if (zoran_formats[i].depth == vbuf->depth)
2241 if (i == NUM_FORMATS) {
2244 "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
2245 ZR_DEVNAME(zr), vbuf->depth);
2249 mutex_lock(&zr->resource_lock);
2251 setup_fbuffer(file, vbuf->base, &zoran_formats[i],
2252 vbuf->width, vbuf->height,
2253 vbuf->bytesperline);
2254 mutex_unlock(&zr->resource_lock);
2262 int *frame = arg, res;
2264 dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
2265 ZR_DEVNAME(zr), *frame);
2267 mutex_lock(&zr->resource_lock);
2268 res = v4l_sync(file, *frame);
2269 mutex_unlock(&zr->resource_lock);
2271 zr->v4l_sync_tail++;
2276 case VIDIOCMCAPTURE:
2278 struct video_mmap *vmap = arg;
2283 "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
2284 ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
2287 mutex_lock(&zr->resource_lock);
2288 res = v4l_grab(file, vmap);
2289 mutex_unlock(&zr->resource_lock);
2296 struct video_mbuf *vmbuf = arg;
2299 dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
2302 fh->v4l_buffers.num_buffers *
2303 fh->v4l_buffers.buffer_size;
2304 vmbuf->frames = fh->v4l_buffers.num_buffers;
2305 for (i = 0; i < vmbuf->frames; i++) {
2307 i * fh->v4l_buffers.buffer_size;
2310 mutex_lock(&zr->resource_lock);
2312 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2315 "%s: VIDIOCGMBUF - buffers already allocated\n",
2318 goto v4l1reqbuf_unlock_and_return;
2321 if (v4l_fbuffer_alloc(file)) {
2323 goto v4l1reqbuf_unlock_and_return;
2326 /* The next mmap will map the V4L buffers */
2327 fh->map_mode = ZORAN_MAP_MODE_RAW;
2328 v4l1reqbuf_unlock_and_return:
2329 mutex_unlock(&zr->resource_lock);
2337 struct video_unit *vunit = arg;
2339 dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
2341 vunit->video = zr->video_dev->minor;
2342 vunit->vbi = VIDEO_NO_UNIT;
2343 vunit->radio = VIDEO_NO_UNIT;
2344 vunit->audio = VIDEO_NO_UNIT;
2345 vunit->teletext = VIDEO_NO_UNIT;
2352 * RJ: In principal we could support subcaptures for V4L grabbing.
2353 * Not even the famous BTTV driver has them, however.
2354 * If there should be a strong demand, one could consider
2355 * to implement them.
2357 case VIDIOCGCAPTURE:
2359 dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
2365 case VIDIOCSCAPTURE:
2367 dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
2373 case BUZIOC_G_PARAMS:
2375 struct zoran_params *bparams = arg;
2377 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
2379 memset(bparams, 0, sizeof(struct zoran_params));
2380 bparams->major_version = MAJOR_VERSION;
2381 bparams->minor_version = MINOR_VERSION;
2383 mutex_lock(&zr->resource_lock);
2385 bparams->norm = zr->norm;
2386 bparams->input = zr->input;
2388 bparams->decimation = fh->jpg_settings.decimation;
2389 bparams->HorDcm = fh->jpg_settings.HorDcm;
2390 bparams->VerDcm = fh->jpg_settings.VerDcm;
2391 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
2392 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
2393 bparams->img_x = fh->jpg_settings.img_x;
2394 bparams->img_y = fh->jpg_settings.img_y;
2395 bparams->img_width = fh->jpg_settings.img_width;
2396 bparams->img_height = fh->jpg_settings.img_height;
2397 bparams->odd_even = fh->jpg_settings.odd_even;
2399 bparams->quality = fh->jpg_settings.jpg_comp.quality;
2400 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
2401 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2402 memcpy(bparams->APP_data,
2403 fh->jpg_settings.jpg_comp.APP_data,
2404 sizeof(bparams->APP_data));
2405 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
2406 memcpy(bparams->COM_data,
2407 fh->jpg_settings.jpg_comp.COM_data,
2408 sizeof(bparams->COM_data));
2409 bparams->jpeg_markers =
2410 fh->jpg_settings.jpg_comp.jpeg_markers;
2412 mutex_unlock(&zr->resource_lock);
2414 bparams->VFIFO_FB = 0;
2420 case BUZIOC_S_PARAMS:
2422 struct zoran_params *bparams = arg;
2425 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
2427 settings.decimation = bparams->decimation;
2428 settings.HorDcm = bparams->HorDcm;
2429 settings.VerDcm = bparams->VerDcm;
2430 settings.TmpDcm = bparams->TmpDcm;
2431 settings.field_per_buff = bparams->field_per_buff;
2432 settings.img_x = bparams->img_x;
2433 settings.img_y = bparams->img_y;
2434 settings.img_width = bparams->img_width;
2435 settings.img_height = bparams->img_height;
2436 settings.odd_even = bparams->odd_even;
2438 settings.jpg_comp.quality = bparams->quality;
2439 settings.jpg_comp.APPn = bparams->APPn;
2440 settings.jpg_comp.APP_len = bparams->APP_len;
2441 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
2442 sizeof(bparams->APP_data));
2443 settings.jpg_comp.COM_len = bparams->COM_len;
2444 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
2445 sizeof(bparams->COM_data));
2446 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
2448 mutex_lock(&zr->resource_lock);
2450 if (zr->codec_mode != BUZ_MODE_IDLE) {
2453 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
2456 goto sparams_unlock_and_return;
2459 /* Check the params first before overwriting our
2461 if (zoran_check_jpg_settings(zr, &settings)) {
2463 goto sparams_unlock_and_return;
2466 fh->jpg_settings = settings;
2467 sparams_unlock_and_return:
2468 mutex_unlock(&zr->resource_lock);
2474 case BUZIOC_REQBUFS:
2476 struct zoran_requestbuffers *breq = arg;
2481 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
2482 ZR_DEVNAME(zr), breq->count, breq->size);
2484 /* Enforce reasonable lower and upper limits */
2485 if (breq->count < 4)
2486 breq->count = 4; /* Could be choosen smaller */
2487 if (breq->count > jpg_nbufs)
2488 breq->count = jpg_nbufs;
2489 breq->size = PAGE_ALIGN(breq->size);
2490 if (breq->size < 8192)
2491 breq->size = 8192; /* Arbitrary */
2492 /* breq->size is limited by 1 page for the stat_com
2493 * tables to a Maximum of 2 MB */
2494 if (breq->size > jpg_bufsize)
2495 breq->size = jpg_bufsize;
2496 if (fh->jpg_buffers.need_contiguous &&
2497 breq->size > MAX_KMALLOC_MEM)
2498 breq->size = MAX_KMALLOC_MEM;
2500 mutex_lock(&zr->resource_lock);
2502 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2505 "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
2508 goto jpgreqbuf_unlock_and_return;
2511 fh->jpg_buffers.num_buffers = breq->count;
2512 fh->jpg_buffers.buffer_size = breq->size;
2514 if (jpg_fbuffer_alloc(file)) {
2516 goto jpgreqbuf_unlock_and_return;
2519 /* The next mmap will map the MJPEG buffers - could
2520 * also be *_PLAY, but it doesn't matter here */
2521 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2522 jpgreqbuf_unlock_and_return:
2523 mutex_unlock(&zr->resource_lock);
2529 case BUZIOC_QBUF_CAPT:
2531 int *frame = arg, res;
2533 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
2534 ZR_DEVNAME(zr), *frame);
2536 mutex_lock(&zr->resource_lock);
2537 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
2538 mutex_unlock(&zr->resource_lock);
2544 case BUZIOC_QBUF_PLAY:
2546 int *frame = arg, res;
2548 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
2549 ZR_DEVNAME(zr), *frame);
2551 mutex_lock(&zr->resource_lock);
2552 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
2553 mutex_unlock(&zr->resource_lock);
2561 struct zoran_sync *bsync = arg;
2564 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
2566 mutex_lock(&zr->resource_lock);
2567 res = jpg_sync(file, bsync);
2568 mutex_unlock(&zr->resource_lock);
2574 case BUZIOC_G_STATUS:
2576 struct zoran_status *bstat = arg;
2577 int norm, input, status, res = 0;
2579 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
2581 if (zr->codec_mode != BUZ_MODE_IDLE) {
2584 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
2589 input = zr->card.input[bstat->input].muxsel;
2590 norm = VIDEO_MODE_AUTO;
2592 mutex_lock(&zr->resource_lock);
2594 if (zr->codec_mode != BUZ_MODE_IDLE) {
2597 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
2600 goto gstat_unlock_and_return;
2603 decoder_command(zr, DECODER_SET_INPUT, &input);
2604 decoder_command(zr, DECODER_SET_NORM, &norm);
2606 /* sleep 1 second */
2609 /* Get status of video decoder */
2610 decoder_command(zr, DECODER_GET_STATUS, &status);
2612 /* restore previous input and norm */
2613 input = zr->card.input[zr->input].muxsel;
2614 decoder_command(zr, DECODER_SET_INPUT, &input);
2615 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
2616 gstat_unlock_and_return:
2617 mutex_unlock(&zr->resource_lock);
2621 (status & DECODER_STATUS_GOOD) ? 1 : 0;
2622 if (status & DECODER_STATUS_NTSC)
2623 bstat->norm = VIDEO_MODE_NTSC;
2624 else if (status & DECODER_STATUS_SECAM)
2625 bstat->norm = VIDEO_MODE_SECAM;
2627 bstat->norm = VIDEO_MODE_PAL;
2630 (status & DECODER_STATUS_COLOR) ? 1 : 0;
2637 #ifdef CONFIG_VIDEO_V4L2
2639 /* The new video4linux2 capture interface - much nicer than video4linux1, since
2640 * it allows for integrating the JPEG capturing calls inside standard v4l2
2643 case VIDIOC_QUERYCAP:
2645 struct v4l2_capability *cap = arg;
2647 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
2649 memset(cap, 0, sizeof(*cap));
2650 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
2651 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
2652 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2653 pci_name(zr->pci_dev));
2655 KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2657 cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2663 case VIDIOC_ENUM_FMT:
2665 struct v4l2_fmtdesc *fmt = arg;
2666 int index = fmt->index, num = -1, i, flag = 0, type =
2669 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
2670 ZR_DEVNAME(zr), fmt->index);
2672 switch (fmt->type) {
2673 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2674 flag = ZORAN_FORMAT_CAPTURE;
2676 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2677 flag = ZORAN_FORMAT_PLAYBACK;
2679 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2680 flag = ZORAN_FORMAT_OVERLAY;
2685 "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
2686 ZR_DEVNAME(zr), fmt->type);
2690 for (i = 0; i < NUM_FORMATS; i++) {
2691 if (zoran_formats[i].flags & flag)
2693 if (num == fmt->index)
2696 if (fmt->index < 0 /* late, but not too late */ ||
2700 memset(fmt, 0, sizeof(*fmt));
2703 strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
2704 fmt->pixelformat = zoran_formats[i].fourcc;
2705 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2706 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2714 struct v4l2_format *fmt = arg;
2715 int type = fmt->type;
2717 dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
2719 memset(fmt, 0, sizeof(*fmt));
2722 switch (fmt->type) {
2723 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2725 mutex_lock(&zr->resource_lock);
2727 fmt->fmt.win.w.left = fh->overlay_settings.x;
2728 fmt->fmt.win.w.top = fh->overlay_settings.y;
2729 fmt->fmt.win.w.width = fh->overlay_settings.width;
2730 fmt->fmt.win.w.height =
2731 fh->overlay_settings.height;
2732 if (fh->overlay_settings.width * 2 >
2734 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2736 fmt->fmt.win.field = V4L2_FIELD_TOP;
2738 mutex_unlock(&zr->resource_lock);
2742 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2743 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2745 mutex_lock(&zr->resource_lock);
2747 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2748 fh->map_mode == ZORAN_MAP_MODE_RAW) {
2750 fmt->fmt.pix.width =
2751 fh->v4l_settings.width;
2752 fmt->fmt.pix.height =
2753 fh->v4l_settings.height;
2754 fmt->fmt.pix.sizeimage =
2755 fh->v4l_settings.bytesperline *
2756 fh->v4l_settings.height;
2757 fmt->fmt.pix.pixelformat =
2758 fh->v4l_settings.format->fourcc;
2759 fmt->fmt.pix.colorspace =
2760 fh->v4l_settings.format->colorspace;
2761 fmt->fmt.pix.bytesperline = 0;
2762 if (BUZ_MAX_HEIGHT <
2763 (fh->v4l_settings.height * 2))
2764 fmt->fmt.pix.field =
2765 V4L2_FIELD_INTERLACED;
2767 fmt->fmt.pix.field =
2772 fmt->fmt.pix.width =
2773 fh->jpg_settings.img_width /
2774 fh->jpg_settings.HorDcm;
2775 fmt->fmt.pix.height =
2776 fh->jpg_settings.img_height /
2777 (fh->jpg_settings.VerDcm *
2778 fh->jpg_settings.TmpDcm);
2779 fmt->fmt.pix.sizeimage =
2780 zoran_v4l2_calc_bufsize(&fh->
2782 fmt->fmt.pix.pixelformat =
2784 if (fh->jpg_settings.TmpDcm == 1)
2785 fmt->fmt.pix.field =
2787 odd_even ? V4L2_FIELD_SEQ_BT :
2790 fmt->fmt.pix.field =
2792 odd_even ? V4L2_FIELD_TOP :
2795 fmt->fmt.pix.bytesperline = 0;
2796 fmt->fmt.pix.colorspace =
2797 V4L2_COLORSPACE_SMPTE170M;
2800 mutex_unlock(&zr->resource_lock);
2807 "%s: VIDIOC_G_FMT - unsupported type %d\n",
2808 ZR_DEVNAME(zr), fmt->type);
2817 struct v4l2_format *fmt = arg;
2821 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
2822 ZR_DEVNAME(zr), fmt->type);
2824 switch (fmt->type) {
2825 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2827 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2828 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2829 fmt->fmt.win.w.width,
2830 fmt->fmt.win.w.height,
2831 fmt->fmt.win.clipcount,
2832 fmt->fmt.win.bitmap);
2833 mutex_lock(&zr->resource_lock);
2835 setup_window(file, fmt->fmt.win.w.left,
2837 fmt->fmt.win.w.width,
2838 fmt->fmt.win.w.height,
2839 (struct video_clip __user *)
2841 fmt->fmt.win.clipcount,
2842 fmt->fmt.win.bitmap);
2843 mutex_unlock(&zr->resource_lock);
2847 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2848 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2851 __cpu_to_le32(fmt->fmt.pix.pixelformat);
2852 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2853 fmt->fmt.pix.width, fmt->fmt.pix.height,
2854 fmt->fmt.pix.pixelformat,
2855 (char *) &printformat);
2857 if (fmt->fmt.pix.bytesperline > 0) {
2859 KERN_ERR "%s: bpl not supported\n",
2864 /* we can be requested to do JPEG/raw playback/capture */
2866 (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2867 (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2868 fmt->fmt.pix.pixelformat ==
2869 V4L2_PIX_FMT_MJPEG))) {
2872 "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
2873 ZR_DEVNAME(zr), fmt->type,
2874 fmt->fmt.pix.pixelformat,
2875 (char *) &printformat);
2879 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
2880 mutex_lock(&zr->resource_lock);
2882 settings = fh->jpg_settings;
2884 if (fh->v4l_buffers.allocated ||
2885 fh->jpg_buffers.allocated) {
2888 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2891 goto sfmtjpg_unlock_and_return;
2894 /* we actually need to set 'real' parameters now */
2895 if ((fmt->fmt.pix.height * 2) >
2897 settings.TmpDcm = 1;
2899 settings.TmpDcm = 2;
2900 settings.decimation = 0;
2901 if (fmt->fmt.pix.height <=
2902 fh->jpg_settings.img_height / 2)
2903 settings.VerDcm = 2;
2905 settings.VerDcm = 1;
2906 if (fmt->fmt.pix.width <=
2907 fh->jpg_settings.img_width / 4)
2908 settings.HorDcm = 4;
2909 else if (fmt->fmt.pix.width <=
2910 fh->jpg_settings.img_width / 2)
2911 settings.HorDcm = 2;
2913 settings.HorDcm = 1;
2914 if (settings.TmpDcm == 1)
2915 settings.field_per_buff = 2;
2917 settings.field_per_buff = 1;
2921 zoran_check_jpg_settings(zr,
2923 goto sfmtjpg_unlock_and_return;
2925 /* it's ok, so set them */
2926 fh->jpg_settings = settings;
2928 /* tell the user what we actually did */
2929 fmt->fmt.pix.width =
2930 settings.img_width / settings.HorDcm;
2931 fmt->fmt.pix.height =
2932 settings.img_height * 2 /
2933 (settings.TmpDcm * settings.VerDcm);
2934 if (settings.TmpDcm == 1)
2935 fmt->fmt.pix.field =
2937 odd_even ? V4L2_FIELD_SEQ_TB :
2940 fmt->fmt.pix.field =
2942 odd_even ? V4L2_FIELD_TOP :
2944 fh->jpg_buffers.buffer_size =
2945 zoran_v4l2_calc_bufsize(&fh->
2947 fmt->fmt.pix.sizeimage =
2948 fh->jpg_buffers.buffer_size;
2950 /* we hereby abuse this variable to show that
2951 * we're gonna do mjpeg capture */
2954 V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2955 ZORAN_MAP_MODE_JPG_REC :
2956 ZORAN_MAP_MODE_JPG_PLAY;
2957 sfmtjpg_unlock_and_return:
2958 mutex_unlock(&zr->resource_lock);
2960 for (i = 0; i < NUM_FORMATS; i++)
2961 if (fmt->fmt.pix.pixelformat ==
2962 zoran_formats[i].fourcc)
2964 if (i == NUM_FORMATS) {
2967 "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
2969 fmt->fmt.pix.pixelformat,
2970 (char *) &printformat);
2973 mutex_lock(&zr->resource_lock);
2974 if (fh->jpg_buffers.allocated ||
2975 (fh->v4l_buffers.allocated &&
2976 fh->v4l_buffers.active !=
2980 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2983 goto sfmtv4l_unlock_and_return;
2985 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2986 fmt->fmt.pix.height =
2988 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2989 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2992 zoran_v4l_set_format(file,
2999 goto sfmtv4l_unlock_and_return;
3001 /* tell the user the
3002 * results/missing stuff */
3003 fmt->fmt.pix.sizeimage =
3004 fh->v4l_settings.height *
3005 fh->v4l_settings.bytesperline;
3006 if (BUZ_MAX_HEIGHT <
3007 (fh->v4l_settings.height * 2))
3008 fmt->fmt.pix.field =
3009 V4L2_FIELD_INTERLACED;
3011 fmt->fmt.pix.field =
3014 fh->map_mode = ZORAN_MAP_MODE_RAW;
3015 sfmtv4l_unlock_and_return:
3016 mutex_unlock(&zr->resource_lock);
3022 dprintk(3, "unsupported\n");
3025 "%s: VIDIOC_S_FMT - unsupported type %d\n",
3026 ZR_DEVNAME(zr), fmt->type);
3036 struct v4l2_framebuffer *fb = arg;
3038 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
3040 memset(fb, 0, sizeof(*fb));
3041 mutex_lock(&zr->resource_lock);
3042 fb->base = zr->buffer.base;
3043 fb->fmt.width = zr->buffer.width;
3044 fb->fmt.height = zr->buffer.height;
3045 if (zr->overlay_settings.format) {
3046 fb->fmt.pixelformat =
3047 fh->overlay_settings.format->fourcc;
3049 fb->fmt.bytesperline = zr->buffer.bytesperline;
3050 mutex_unlock(&zr->resource_lock);
3051 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
3052 fb->fmt.field = V4L2_FIELD_INTERLACED;
3053 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
3054 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3063 struct v4l2_framebuffer *fb = arg;
3064 __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
3068 "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
3069 ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
3070 fb->fmt.bytesperline, fb->fmt.pixelformat,
3071 (char *) &printformat);
3073 for (i = 0; i < NUM_FORMATS; i++)
3074 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
3076 if (i == NUM_FORMATS) {
3079 "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
3080 ZR_DEVNAME(zr), fb->fmt.pixelformat,
3081 (char *) &printformat);
3085 mutex_lock(&zr->resource_lock);
3087 setup_fbuffer(file, fb->base, &zoran_formats[i],
3088 fb->fmt.width, fb->fmt.height,
3089 fb->fmt.bytesperline);
3090 mutex_unlock(&zr->resource_lock);
3096 case VIDIOC_OVERLAY:
3100 dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
3101 ZR_DEVNAME(zr), *on);
3103 mutex_lock(&zr->resource_lock);
3104 res = setup_overlay(file, *on);
3105 mutex_unlock(&zr->resource_lock);
3111 case VIDIOC_REQBUFS:
3113 struct v4l2_requestbuffers *req = arg;
3116 dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
3117 ZR_DEVNAME(zr), req->type);
3119 if (req->memory != V4L2_MEMORY_MMAP) {
3122 "%s: only MEMORY_MMAP capture is supported, not %d\n",
3123 ZR_DEVNAME(zr), req->memory);
3127 mutex_lock(&zr->resource_lock);
3129 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
3132 "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
3135 goto v4l2reqbuf_unlock_and_return;
3138 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
3139 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3141 /* control user input */
3144 if (req->count > v4l_nbufs)
3145 req->count = v4l_nbufs;
3146 fh->v4l_buffers.num_buffers = req->count;
3148 if (v4l_fbuffer_alloc(file)) {
3150 goto v4l2reqbuf_unlock_and_return;
3153 /* The next mmap will map the V4L buffers */
3154 fh->map_mode = ZORAN_MAP_MODE_RAW;
3156 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
3157 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3159 /* we need to calculate size ourselves now */
3162 if (req->count > jpg_nbufs)
3163 req->count = jpg_nbufs;
3164 fh->jpg_buffers.num_buffers = req->count;
3165 fh->jpg_buffers.buffer_size =
3166 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3168 if (jpg_fbuffer_alloc(file)) {
3170 goto v4l2reqbuf_unlock_and_return;
3173 /* The next mmap will map the MJPEG buffers */
3174 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
3175 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
3177 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
3182 "%s: VIDIOC_REQBUFS - unknown type %d\n",
3183 ZR_DEVNAME(zr), req->type);
3185 goto v4l2reqbuf_unlock_and_return;
3187 v4l2reqbuf_unlock_and_return:
3188 mutex_unlock(&zr->resource_lock);
3194 case VIDIOC_QUERYBUF:
3196 struct v4l2_buffer *buf = arg;
3197 __u32 type = buf->type;
3198 int index = buf->index, res;
3202 "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
3203 ZR_DEVNAME(zr), buf->index, buf->type);
3205 memset(buf, 0, sizeof(buf));
3209 mutex_lock(&zr->resource_lock);
3210 res = zoran_v4l2_buffer_status(file, buf, buf->index);
3211 mutex_unlock(&zr->resource_lock);
3219 struct v4l2_buffer *buf = arg;
3220 int res = 0, codec_mode, buf_type;
3223 KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
3224 ZR_DEVNAME(zr), buf->type, buf->index);
3226 mutex_lock(&zr->resource_lock);
3228 switch (fh->map_mode) {
3229 case ZORAN_MAP_MODE_RAW:
3230 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3233 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3234 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3236 goto qbuf_unlock_and_return;
3239 res = zoran_v4l_queue_frame(file, buf->index);
3241 goto qbuf_unlock_and_return;
3242 if (!zr->v4l_memgrab_active &&
3243 fh->v4l_buffers.active == ZORAN_LOCKED)
3244 zr36057_set_memgrab(zr, 1);
3247 case ZORAN_MAP_MODE_JPG_REC:
3248 case ZORAN_MAP_MODE_JPG_PLAY:
3249 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3250 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3251 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
3253 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3254 codec_mode = BUZ_MODE_MOTION_COMPRESS;
3257 if (buf->type != buf_type) {
3260 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3261 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3263 goto qbuf_unlock_and_return;
3267 zoran_jpg_queue_frame(file, buf->index,
3270 goto qbuf_unlock_and_return;
3271 if (zr->codec_mode == BUZ_MODE_IDLE &&
3272 fh->jpg_buffers.active == ZORAN_LOCKED) {
3273 zr36057_enable_jpg(zr, codec_mode);
3280 "%s: VIDIOC_QBUF - unsupported type %d\n",
3281 ZR_DEVNAME(zr), buf->type);
3283 goto qbuf_unlock_and_return;
3285 qbuf_unlock_and_return:
3286 mutex_unlock(&zr->resource_lock);
3294 struct v4l2_buffer *buf = arg;
3295 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
3297 dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
3298 ZR_DEVNAME(zr), buf->type);
3300 mutex_lock(&zr->resource_lock);
3302 switch (fh->map_mode) {
3303 case ZORAN_MAP_MODE_RAW:
3304 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3307 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3308 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3310 goto dqbuf_unlock_and_return;
3313 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3314 if (file->f_flags & O_NONBLOCK &&
3315 zr->v4l_buffers.buffer[num].state !=
3318 goto dqbuf_unlock_and_return;
3320 res = v4l_sync(file, num);
3322 goto dqbuf_unlock_and_return;
3324 zr->v4l_sync_tail++;
3325 res = zoran_v4l2_buffer_status(file, buf, num);
3328 case ZORAN_MAP_MODE_JPG_REC:
3329 case ZORAN_MAP_MODE_JPG_PLAY:
3331 struct zoran_sync bs;
3333 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
3334 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3336 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3338 if (buf->type != buf_type) {
3341 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3342 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3344 goto dqbuf_unlock_and_return;
3349 jpg_que_tail & BUZ_MASK_FRAME];
3351 if (file->f_flags & O_NONBLOCK &&
3352 zr->jpg_buffers.buffer[num].state !=
3355 goto dqbuf_unlock_and_return;
3357 res = jpg_sync(file, &bs);
3359 goto dqbuf_unlock_and_return;
3361 zoran_v4l2_buffer_status(file, buf, bs.frame);
3368 "%s: VIDIOC_DQBUF - unsupported type %d\n",
3369 ZR_DEVNAME(zr), buf->type);
3371 goto dqbuf_unlock_and_return;
3373 dqbuf_unlock_and_return:
3374 mutex_unlock(&zr->resource_lock);
3380 case VIDIOC_STREAMON:
3384 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
3386 mutex_lock(&zr->resource_lock);
3388 switch (fh->map_mode) {
3389 case ZORAN_MAP_MODE_RAW: /* raw capture */
3390 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
3391 fh->v4l_buffers.active != ZORAN_ACTIVE) {
3393 goto strmon_unlock_and_return;
3396 zr->v4l_buffers.active = fh->v4l_buffers.active =
3398 zr->v4l_settings = fh->v4l_settings;
3400 zr->v4l_sync_tail = zr->v4l_pend_tail;
3401 if (!zr->v4l_memgrab_active &&
3402 zr->v4l_pend_head != zr->v4l_pend_tail) {
3403 zr36057_set_memgrab(zr, 1);
3407 case ZORAN_MAP_MODE_JPG_REC:
3408 case ZORAN_MAP_MODE_JPG_PLAY:
3409 /* what is the codec mode right now? */
3410 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
3411 fh->jpg_buffers.active != ZORAN_ACTIVE) {
3413 goto strmon_unlock_and_return;
3416 zr->jpg_buffers.active = fh->jpg_buffers.active =
3419 if (zr->jpg_que_head != zr->jpg_que_tail) {
3420 /* Start the jpeg codec when the first frame is queued */
3428 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
3429 ZR_DEVNAME(zr), fh->map_mode);
3431 goto strmon_unlock_and_return;
3433 strmon_unlock_and_return:
3434 mutex_unlock(&zr->resource_lock);
3440 case VIDIOC_STREAMOFF:
3444 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
3446 mutex_lock(&zr->resource_lock);
3448 switch (fh->map_mode) {
3449 case ZORAN_MAP_MODE_RAW: /* raw capture */
3450 if (fh->v4l_buffers.active == ZORAN_FREE &&
3451 zr->v4l_buffers.active != ZORAN_FREE) {
3452 res = -EPERM; /* stay off other's settings! */
3453 goto strmoff_unlock_and_return;
3455 if (zr->v4l_buffers.active == ZORAN_FREE)
3456 goto strmoff_unlock_and_return;
3458 /* unload capture */
3459 if (zr->v4l_memgrab_active)
3460 zr36057_set_memgrab(zr, 0);
3462 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3463 zr->v4l_buffers.buffer[i].state =
3465 fh->v4l_buffers = zr->v4l_buffers;
3467 zr->v4l_buffers.active = fh->v4l_buffers.active =
3470 zr->v4l_grab_seq = 0;
3471 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
3472 zr->v4l_sync_tail = 0;
3476 case ZORAN_MAP_MODE_JPG_REC:
3477 case ZORAN_MAP_MODE_JPG_PLAY:
3478 if (fh->jpg_buffers.active == ZORAN_FREE &&
3479 zr->jpg_buffers.active != ZORAN_FREE) {
3480 res = -EPERM; /* stay off other's settings! */
3481 goto strmoff_unlock_and_return;
3483 if (zr->jpg_buffers.active == ZORAN_FREE)
3484 goto strmoff_unlock_and_return;
3489 ZORAN_MAP_MODE_JPG_REC) ?
3490 BUZ_MODE_MOTION_COMPRESS :
3491 BUZ_MODE_MOTION_DECOMPRESS);
3493 goto strmoff_unlock_and_return;
3498 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
3499 ZR_DEVNAME(zr), fh->map_mode);
3501 goto strmoff_unlock_and_return;
3503 strmoff_unlock_and_return:
3504 mutex_unlock(&zr->resource_lock);
3510 case VIDIOC_QUERYCTRL:
3512 struct v4l2_queryctrl *ctrl = arg;
3514 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
3515 ZR_DEVNAME(zr), ctrl->id);
3517 /* we only support hue/saturation/contrast/brightness */
3518 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3519 ctrl->id > V4L2_CID_HUE)
3523 memset(ctrl, 0, sizeof(*ctrl));
3528 case V4L2_CID_BRIGHTNESS:
3529 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
3531 case V4L2_CID_CONTRAST:
3532 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
3534 case V4L2_CID_SATURATION:
3535 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
3538 strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
3543 ctrl->maximum = 65535;
3545 ctrl->default_value = 32768;
3546 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
3554 struct v4l2_control *ctrl = arg;
3556 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
3557 ZR_DEVNAME(zr), ctrl->id);
3559 /* we only support hue/saturation/contrast/brightness */
3560 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3561 ctrl->id > V4L2_CID_HUE)
3564 mutex_lock(&zr->resource_lock);
3566 case V4L2_CID_BRIGHTNESS:
3567 ctrl->value = zr->brightness;
3569 case V4L2_CID_CONTRAST:
3570 ctrl->value = zr->contrast;
3572 case V4L2_CID_SATURATION:
3573 ctrl->value = zr->saturation;
3576 ctrl->value = zr->hue;
3579 mutex_unlock(&zr->resource_lock);
3587 struct v4l2_control *ctrl = arg;
3588 struct video_picture pict;
3590 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
3591 ZR_DEVNAME(zr), ctrl->id);
3593 /* we only support hue/saturation/contrast/brightness */
3594 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3595 ctrl->id > V4L2_CID_HUE)
3598 if (ctrl->value < 0 || ctrl->value > 65535) {
3601 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
3602 ZR_DEVNAME(zr), ctrl->value, ctrl->id);
3606 mutex_lock(&zr->resource_lock);
3608 case V4L2_CID_BRIGHTNESS:
3609 zr->brightness = ctrl->value;
3611 case V4L2_CID_CONTRAST:
3612 zr->contrast = ctrl->value;
3614 case V4L2_CID_SATURATION:
3615 zr->saturation = ctrl->value;
3618 zr->hue = ctrl->value;
3621 pict.brightness = zr->brightness;
3622 pict.contrast = zr->contrast;
3623 pict.colour = zr->saturation;
3626 decoder_command(zr, DECODER_SET_PICTURE, &pict);
3628 mutex_unlock(&zr->resource_lock);
3634 case VIDIOC_ENUMSTD:
3636 struct v4l2_standard *std = arg;
3638 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
3639 ZR_DEVNAME(zr), std->index);
3641 if (std->index < 0 || std->index >= (zr->card.norms + 1))
3644 int id = std->index;
3645 memset(std, 0, sizeof(*std));
3649 if (std->index == zr->card.norms) {
3650 /* if we have autodetect, ... */
3651 struct video_decoder_capability caps;
3652 decoder_command(zr, DECODER_GET_CAPABILITIES,
3654 if (caps.flags & VIDEO_DECODER_AUTO) {
3655 std->id = V4L2_STD_ALL;
3656 strncpy(std->name, "Autodetect", sizeof(std->name)-1);
3661 switch (std->index) {
3663 std->id = V4L2_STD_PAL;
3664 strncpy(std->name, "PAL", sizeof(std->name)-1);
3665 std->frameperiod.numerator = 1;
3666 std->frameperiod.denominator = 25;
3667 std->framelines = zr->card.tvn[0]->Ht;
3670 std->id = V4L2_STD_NTSC;
3671 strncpy(std->name, "NTSC", sizeof(std->name)-1);
3672 std->frameperiod.numerator = 1001;
3673 std->frameperiod.denominator = 30000;
3674 std->framelines = zr->card.tvn[1]->Ht;
3677 std->id = V4L2_STD_SECAM;
3678 strncpy(std->name, "SECAM", sizeof(std->name)-1);
3679 std->frameperiod.numerator = 1;
3680 std->frameperiod.denominator = 25;
3681 std->framelines = zr->card.tvn[2]->Ht;
3691 v4l2_std_id *std = arg;
3694 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
3696 mutex_lock(&zr->resource_lock);
3698 mutex_unlock(&zr->resource_lock);
3701 case VIDEO_MODE_PAL:
3702 *std = V4L2_STD_PAL;
3704 case VIDEO_MODE_NTSC:
3705 *std = V4L2_STD_NTSC;
3707 case VIDEO_MODE_SECAM:
3708 *std = V4L2_STD_SECAM;
3718 int norm = -1, res = 0;
3719 v4l2_std_id *std = arg;
3721 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
3722 ZR_DEVNAME(zr), (unsigned long long)*std);
3724 if ((*std & V4L2_STD_PAL) && !(*std & ~V4L2_STD_PAL))
3725 norm = VIDEO_MODE_PAL;
3726 else if ((*std & V4L2_STD_NTSC) && !(*std & ~V4L2_STD_NTSC))
3727 norm = VIDEO_MODE_NTSC;
3728 else if ((*std & V4L2_STD_SECAM) && !(*std & ~V4L2_STD_SECAM))
3729 norm = VIDEO_MODE_SECAM;
3730 else if (*std == V4L2_STD_ALL)
3731 norm = VIDEO_MODE_AUTO;
3735 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3736 ZR_DEVNAME(zr), (unsigned long long)*std);
3740 mutex_lock(&zr->resource_lock);
3741 if ((res = zoran_set_norm(zr, norm)))
3742 goto sstd_unlock_and_return;
3744 res = wait_grab_pending(zr);
3745 sstd_unlock_and_return:
3746 mutex_unlock(&zr->resource_lock);
3751 case VIDIOC_ENUMINPUT:
3753 struct v4l2_input *inp = arg;
3756 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
3757 ZR_DEVNAME(zr), inp->index);
3759 if (inp->index < 0 || inp->index >= zr->card.inputs)
3762 int id = inp->index;
3763 memset(inp, 0, sizeof(*inp));
3767 strncpy(inp->name, zr->card.input[inp->index].name,
3768 sizeof(inp->name) - 1);
3769 inp->type = V4L2_INPUT_TYPE_CAMERA;
3770 inp->std = V4L2_STD_ALL;
3772 /* Get status of video decoder */
3773 mutex_lock(&zr->resource_lock);
3774 decoder_command(zr, DECODER_GET_STATUS, &status);
3775 mutex_unlock(&zr->resource_lock);
3777 if (!(status & DECODER_STATUS_GOOD)) {
3778 inp->status |= V4L2_IN_ST_NO_POWER;
3779 inp->status |= V4L2_IN_ST_NO_SIGNAL;
3781 if (!(status & DECODER_STATUS_COLOR))
3782 inp->status |= V4L2_IN_ST_NO_COLOR;
3788 case VIDIOC_G_INPUT:
3792 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
3794 mutex_lock(&zr->resource_lock);
3796 mutex_unlock(&zr->resource_lock);
3802 case VIDIOC_S_INPUT:
3804 int *input = arg, res = 0;
3806 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
3807 ZR_DEVNAME(zr), *input);
3809 mutex_lock(&zr->resource_lock);
3810 if ((res = zoran_set_input(zr, *input)))
3811 goto sinput_unlock_and_return;
3813 /* Make sure the changes come into effect */
3814 res = wait_grab_pending(zr);
3815 sinput_unlock_and_return:
3816 mutex_unlock(&zr->resource_lock);
3821 case VIDIOC_ENUMOUTPUT:
3823 struct v4l2_output *outp = arg;
3825 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
3826 ZR_DEVNAME(zr), outp->index);
3828 if (outp->index != 0)
3831 memset(outp, 0, sizeof(*outp));
3833 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3834 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
3840 case VIDIOC_G_OUTPUT:
3844 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
3852 case VIDIOC_S_OUTPUT:
3856 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
3857 ZR_DEVNAME(zr), *output);
3866 /* cropping (sub-frame capture) */
3867 case VIDIOC_CROPCAP:
3869 struct v4l2_cropcap *cropcap = arg;
3870 int type = cropcap->type, res = 0;
3872 dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
3873 ZR_DEVNAME(zr), cropcap->type);
3875 memset(cropcap, 0, sizeof(*cropcap));
3876 cropcap->type = type;
3878 mutex_lock(&zr->resource_lock);
3880 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3881 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3882 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3885 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3888 goto cropcap_unlock_and_return;
3891 cropcap->bounds.top = cropcap->bounds.left = 0;
3892 cropcap->bounds.width = BUZ_MAX_WIDTH;
3893 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3894 cropcap->defrect.top = cropcap->defrect.left = 0;
3895 cropcap->defrect.width = BUZ_MIN_WIDTH;
3896 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3897 cropcap_unlock_and_return:
3898 mutex_unlock(&zr->resource_lock);
3905 struct v4l2_crop *crop = arg;
3906 int type = crop->type, res = 0;
3908 dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
3909 ZR_DEVNAME(zr), crop->type);
3911 memset(crop, 0, sizeof(*crop));
3914 mutex_lock(&zr->resource_lock);
3916 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3917 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3918 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3921 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3924 goto gcrop_unlock_and_return;
3927 crop->c.top = fh->jpg_settings.img_y;
3928 crop->c.left = fh->jpg_settings.img_x;
3929 crop->c.width = fh->jpg_settings.img_width;
3930 crop->c.height = fh->jpg_settings.img_height;
3932 gcrop_unlock_and_return:
3933 mutex_unlock(&zr->resource_lock);
3941 struct v4l2_crop *crop = arg;
3944 settings = fh->jpg_settings;
3948 "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
3949 ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
3950 crop->c.width, crop->c.height);
3952 mutex_lock(&zr->resource_lock);
3954 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3957 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3960 goto scrop_unlock_and_return;
3963 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3964 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3965 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3968 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3971 goto scrop_unlock_and_return;
3974 /* move into a form that we understand */
3975 settings.img_x = crop->c.left;
3976 settings.img_y = crop->c.top;
3977 settings.img_width = crop->c.width;
3978 settings.img_height = crop->c.height;
3980 /* check validity */
3981 if ((res = zoran_check_jpg_settings(zr, &settings)))
3982 goto scrop_unlock_and_return;
3985 fh->jpg_settings = settings;
3987 scrop_unlock_and_return:
3988 mutex_unlock(&zr->resource_lock);
3993 case VIDIOC_G_JPEGCOMP:
3995 struct v4l2_jpegcompression *params = arg;
3997 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
4000 memset(params, 0, sizeof(*params));
4002 mutex_lock(&zr->resource_lock);
4004 params->quality = fh->jpg_settings.jpg_comp.quality;
4005 params->APPn = fh->jpg_settings.jpg_comp.APPn;
4006 memcpy(params->APP_data,
4007 fh->jpg_settings.jpg_comp.APP_data,
4008 fh->jpg_settings.jpg_comp.APP_len);
4009 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
4010 memcpy(params->COM_data,
4011 fh->jpg_settings.jpg_comp.COM_data,
4012 fh->jpg_settings.jpg_comp.COM_len);
4013 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
4014 params->jpeg_markers =
4015 fh->jpg_settings.jpg_comp.jpeg_markers;
4017 mutex_unlock(&zr->resource_lock);
4023 case VIDIOC_S_JPEGCOMP:
4025 struct v4l2_jpegcompression *params = arg;
4028 settings = fh->jpg_settings;
4032 "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
4033 ZR_DEVNAME(zr), params->quality, params->APPn,
4034 params->APP_len, params->COM_len);
4036 settings.jpg_comp = *params;
4038 mutex_lock(&zr->resource_lock);
4040 if (fh->v4l_buffers.active != ZORAN_FREE ||
4041 fh->jpg_buffers.active != ZORAN_FREE) {
4044 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
4047 goto sjpegc_unlock_and_return;
4050 if ((res = zoran_check_jpg_settings(zr, &settings)))
4051 goto sjpegc_unlock_and_return;
4052 if (!fh->jpg_buffers.allocated)
4053 fh->jpg_buffers.buffer_size =
4054 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
4055 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
4056 sjpegc_unlock_and_return:
4057 mutex_unlock(&zr->resource_lock);
4063 case VIDIOC_QUERYSTD: /* why is this useful? */
4065 v4l2_std_id *std = arg;
4068 KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
4069 ZR_DEVNAME(zr), (unsigned long long)*std);
4071 if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
4072 *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
4073 zr->card.norms == 3)) {
4081 case VIDIOC_TRY_FMT:
4083 struct v4l2_format *fmt = arg;
4086 dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
4087 ZR_DEVNAME(zr), fmt->type);
4089 switch (fmt->type) {
4090 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4091 mutex_lock(&zr->resource_lock);
4093 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
4094 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
4095 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
4096 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
4097 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
4098 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
4099 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
4100 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
4102 mutex_unlock(&zr->resource_lock);
4105 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4106 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4107 if (fmt->fmt.pix.bytesperline > 0)
4110 mutex_lock(&zr->resource_lock);
4112 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
4113 settings = fh->jpg_settings;
4115 /* we actually need to set 'real' parameters now */
4116 if ((fmt->fmt.pix.height * 2) >
4118 settings.TmpDcm = 1;
4120 settings.TmpDcm = 2;
4121 settings.decimation = 0;
4122 if (fmt->fmt.pix.height <=
4123 fh->jpg_settings.img_height / 2)
4124 settings.VerDcm = 2;
4126 settings.VerDcm = 1;
4127 if (fmt->fmt.pix.width <=
4128 fh->jpg_settings.img_width / 4)
4129 settings.HorDcm = 4;
4130 else if (fmt->fmt.pix.width <=
4131 fh->jpg_settings.img_width / 2)
4132 settings.HorDcm = 2;
4134 settings.HorDcm = 1;
4135 if (settings.TmpDcm == 1)
4136 settings.field_per_buff = 2;
4138 settings.field_per_buff = 1;
4142 zoran_check_jpg_settings(zr,
4144 goto tryfmt_unlock_and_return;
4146 /* tell the user what we actually did */
4147 fmt->fmt.pix.width =
4148 settings.img_width / settings.HorDcm;
4149 fmt->fmt.pix.height =
4150 settings.img_height * 2 /
4151 (settings.TmpDcm * settings.VerDcm);
4152 if (settings.TmpDcm == 1)
4153 fmt->fmt.pix.field =
4155 odd_even ? V4L2_FIELD_SEQ_TB :
4158 fmt->fmt.pix.field =
4160 odd_even ? V4L2_FIELD_TOP :
4163 fmt->fmt.pix.sizeimage =
4164 zoran_v4l2_calc_bufsize(&settings);
4165 } else if (fmt->type ==
4166 V4L2_BUF_TYPE_VIDEO_CAPTURE) {
4169 for (i = 0; i < NUM_FORMATS; i++)
4170 if (zoran_formats[i].fourcc ==
4171 fmt->fmt.pix.pixelformat)
4173 if (i == NUM_FORMATS) {
4175 goto tryfmt_unlock_and_return;
4178 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
4179 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
4180 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
4181 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
4182 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
4183 fmt->fmt.pix.height =
4185 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
4186 fmt->fmt.pix.height =
4190 goto tryfmt_unlock_and_return;
4192 tryfmt_unlock_and_return:
4193 mutex_unlock(&zr->resource_lock);
4208 dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
4209 ZR_DEVNAME(zr), cmd);
4210 return -ENOIOCTLCMD;
4219 zoran_ioctl (struct inode *inode,
4224 return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
4228 zoran_poll (struct file *file,
4231 struct zoran_fh *fh = file->private_data;
4232 struct zoran *zr = fh->zr;
4234 unsigned long flags;
4236 /* we should check whether buffers are ready to be synced on
4237 * (w/o waits - O_NONBLOCK) here
4238 * if ready for read (sync), return POLLIN|POLLRDNORM,
4239 * if ready for write (sync), return POLLOUT|POLLWRNORM,
4240 * if error, return POLLERR,
4241 * if no buffers queued or so, return POLLNVAL
4244 mutex_lock(&zr->resource_lock);
4246 switch (fh->map_mode) {
4247 case ZORAN_MAP_MODE_RAW:
4248 poll_wait(file, &zr->v4l_capq, wait);
4249 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
4251 spin_lock_irqsave(&zr->spinlock, flags);
4254 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
4255 ZR_DEVNAME(zr), __FUNCTION__,
4256 "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
4257 "UPMD"[zr->v4l_buffers.buffer[frame].state],
4258 zr->v4l_pend_tail, zr->v4l_pend_head);
4259 /* Process is the one capturing? */
4260 if (fh->v4l_buffers.active != ZORAN_FREE &&
4261 /* Buffer ready to DQBUF? */
4262 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
4263 res = POLLIN | POLLRDNORM;
4264 spin_unlock_irqrestore(&zr->spinlock, flags);
4268 case ZORAN_MAP_MODE_JPG_REC:
4269 case ZORAN_MAP_MODE_JPG_PLAY:
4270 poll_wait(file, &zr->jpg_capq, wait);
4271 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
4273 spin_lock_irqsave(&zr->spinlock, flags);
4276 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
4277 ZR_DEVNAME(zr), __FUNCTION__,
4278 "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
4279 "UPMD"[zr->jpg_buffers.buffer[frame].state],
4280 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
4281 if (fh->jpg_buffers.active != ZORAN_FREE &&
4282 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
4283 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
4284 res = POLLIN | POLLRDNORM;
4286 res = POLLOUT | POLLWRNORM;
4288 spin_unlock_irqrestore(&zr->spinlock, flags);
4295 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
4296 ZR_DEVNAME(zr), fh->map_mode);
4300 mutex_unlock(&zr->resource_lock);
4307 * This maps the buffers to user space.
4309 * Depending on the state of fh->map_mode
4310 * the V4L or the MJPEG buffers are mapped
4311 * per buffer or all together
4313 * Note that we need to connect to some
4314 * unmap signal event to unmap the de-allocate
4315 * the buffer accordingly (zoran_vm_close())
4319 zoran_vm_open (struct vm_area_struct *vma)
4321 struct zoran_mapping *map = vma->vm_private_data;
4327 zoran_vm_close (struct vm_area_struct *vma)
4329 struct zoran_mapping *map = vma->vm_private_data;
4330 struct file *file = map->file;
4331 struct zoran_fh *fh = file->private_data;
4332 struct zoran *zr = fh->zr;
4336 if (map->count == 0) {
4337 switch (fh->map_mode) {
4338 case ZORAN_MAP_MODE_JPG_REC:
4339 case ZORAN_MAP_MODE_JPG_PLAY:
4341 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
4344 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
4345 if (fh->jpg_buffers.buffer[i].map == map) {
4346 fh->jpg_buffers.buffer[i].map =
4352 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
4353 if (fh->jpg_buffers.buffer[i].map)
4355 if (i == fh->jpg_buffers.num_buffers) {
4356 mutex_lock(&zr->resource_lock);
4358 if (fh->jpg_buffers.active != ZORAN_FREE) {
4359 jpg_qbuf(file, -1, zr->codec_mode);
4360 zr->jpg_buffers.allocated = 0;
4361 zr->jpg_buffers.active =
4362 fh->jpg_buffers.active =
4365 //jpg_fbuffer_free(file);
4366 fh->jpg_buffers.allocated = 0;
4367 fh->jpg_buffers.ready_to_be_freed = 1;
4369 mutex_unlock(&zr->resource_lock);
4374 case ZORAN_MAP_MODE_RAW:
4376 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
4379 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
4380 if (fh->v4l_buffers.buffer[i].map == map) {
4382 fh->v4l_buffers.buffer[i].map =
4388 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
4389 if (fh->v4l_buffers.buffer[i].map)
4391 if (i == fh->v4l_buffers.num_buffers) {
4392 mutex_lock(&zr->resource_lock);
4394 if (fh->v4l_buffers.active != ZORAN_FREE) {
4395 zr36057_set_memgrab(zr, 0);
4396 zr->v4l_buffers.allocated = 0;
4397 zr->v4l_buffers.active =
4398 fh->v4l_buffers.active =
4401 //v4l_fbuffer_free(file);
4402 fh->v4l_buffers.allocated = 0;
4403 fh->v4l_buffers.ready_to_be_freed = 1;
4405 mutex_unlock(&zr->resource_lock);
4412 "%s: munmap() - internal error - unknown map mode %d\n",
4413 ZR_DEVNAME(zr), fh->map_mode);
4420 static struct vm_operations_struct zoran_vm_ops = {
4421 .open = zoran_vm_open,
4422 .close = zoran_vm_close,
4426 zoran_mmap (struct file *file,
4427 struct vm_area_struct *vma)
4429 struct zoran_fh *fh = file->private_data;
4430 struct zoran *zr = fh->zr;
4431 unsigned long size = (vma->vm_end - vma->vm_start);
4432 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4434 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
4436 struct zoran_mapping *map;
4440 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
4442 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
4443 vma->vm_start, vma->vm_end, size);
4445 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
4446 !(vma->vm_flags & VM_WRITE)) {
4449 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
4454 switch (fh->map_mode) {
4456 case ZORAN_MAP_MODE_JPG_REC:
4457 case ZORAN_MAP_MODE_JPG_PLAY:
4460 mutex_lock(&zr->resource_lock);
4462 /* Map the MJPEG buffers */
4463 if (!fh->jpg_buffers.allocated) {
4466 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
4469 goto jpg_mmap_unlock_and_return;
4472 first = offset / fh->jpg_buffers.buffer_size;
4473 last = first - 1 + size / fh->jpg_buffers.buffer_size;
4474 if (offset % fh->jpg_buffers.buffer_size != 0 ||
4475 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
4476 last < 0 || first >= fh->jpg_buffers.num_buffers ||
4477 last >= fh->jpg_buffers.num_buffers) {
4480 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4481 ZR_DEVNAME(zr), offset, size,
4482 fh->jpg_buffers.buffer_size,
4483 fh->jpg_buffers.num_buffers);
4485 goto jpg_mmap_unlock_and_return;
4487 for (i = first; i <= last; i++) {
4488 if (fh->jpg_buffers.buffer[i].map) {
4491 "%s: mmap(MJPEG) - buffer %d already mapped\n",
4494 goto jpg_mmap_unlock_and_return;
4498 /* map these buffers (v4l_buffers[i]) */
4499 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4502 goto jpg_mmap_unlock_and_return;
4507 vma->vm_ops = &zoran_vm_ops;
4508 vma->vm_flags |= VM_DONTEXPAND;
4509 vma->vm_private_data = map;
4511 for (i = first; i <= last; i++) {
4513 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
4516 (le32_to_cpu(fh->jpg_buffers.buffer[i].
4517 frag_tab[2 * j + 1]) & ~1) << 1;
4522 le32_to_cpu((unsigned long) fh->jpg_buffers.
4523 buffer[i].frag_tab[2 * j]);
4524 /* should just be pos on i386 */
4525 page = virt_to_phys(bus_to_virt(pos))
4527 if (remap_pfn_range(vma, start, page,
4528 todo, PAGE_SHARED)) {
4531 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
4534 goto jpg_mmap_unlock_and_return;
4540 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
4541 frag_tab[2 * j + 1]) & 1)
4542 break; /* was last fragment */
4544 fh->jpg_buffers.buffer[i].map = map;
4549 jpg_mmap_unlock_and_return:
4550 mutex_unlock(&zr->resource_lock);
4554 case ZORAN_MAP_MODE_RAW:
4556 mutex_lock(&zr->resource_lock);
4558 /* Map the V4L buffers */
4559 if (!fh->v4l_buffers.allocated) {
4562 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
4565 goto v4l_mmap_unlock_and_return;
4568 first = offset / fh->v4l_buffers.buffer_size;
4569 last = first - 1 + size / fh->v4l_buffers.buffer_size;
4570 if (offset % fh->v4l_buffers.buffer_size != 0 ||
4571 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
4572 last < 0 || first >= fh->v4l_buffers.num_buffers ||
4573 last >= fh->v4l_buffers.buffer_size) {
4576 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4577 ZR_DEVNAME(zr), offset, size,
4578 fh->v4l_buffers.buffer_size,
4579 fh->v4l_buffers.num_buffers);
4581 goto v4l_mmap_unlock_and_return;
4583 for (i = first; i <= last; i++) {
4584 if (fh->v4l_buffers.buffer[i].map) {
4587 "%s: mmap(V4L) - buffer %d already mapped\n",
4590 goto v4l_mmap_unlock_and_return;
4594 /* map these buffers (v4l_buffers[i]) */
4595 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4598 goto v4l_mmap_unlock_and_return;
4603 vma->vm_ops = &zoran_vm_ops;
4604 vma->vm_flags |= VM_DONTEXPAND;
4605 vma->vm_private_data = map;
4607 for (i = first; i <= last; i++) {
4609 if (todo > fh->v4l_buffers.buffer_size)
4610 todo = fh->v4l_buffers.buffer_size;
4611 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
4612 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
4613 todo, PAGE_SHARED)) {
4616 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
4619 goto v4l_mmap_unlock_and_return;
4623 fh->v4l_buffers.buffer[i].map = map;
4627 v4l_mmap_unlock_and_return:
4628 mutex_unlock(&zr->resource_lock);
4635 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
4636 ZR_DEVNAME(zr), fh->map_mode);
4643 static const struct file_operations zoran_fops = {
4644 .owner = THIS_MODULE,
4646 .release = zoran_close,
4647 .ioctl = zoran_ioctl,
4648 .compat_ioctl = v4l_compat_ioctl32,
4649 .llseek = no_llseek,
4651 .write = zoran_write,
4656 struct video_device zoran_template __devinitdata = {
4658 .type = ZORAN_VID_TYPE,
4659 #ifdef CONFIG_VIDEO_V4L2
4660 .type2 = ZORAN_V4L2_VID_FLAGS,
4662 .hardware = ZORAN_HARDWARE,
4663 .fops = &zoran_fops,
4664 .release = &zoran_vdev_release,