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>
56 #include <linux/interrupt.h>
57 #include <linux/i2c.h>
58 #include <linux/i2c-algo-bit.h>
60 #include <linux/spinlock.h>
61 #define MAP_NR(x) virt_to_page(x)
62 #define ZORAN_VID_TYPE ( \
68 VID_TYPE_MJPEG_DECODER | \
69 VID_TYPE_MJPEG_ENCODER \
72 #include <linux/videodev.h>
73 #include <media/v4l2-common.h>
74 #include "videocodec.h"
76 #include <asm/byteorder.h>
78 #include <asm/uaccess.h>
79 #include <linux/proc_fs.h>
81 #include <linux/video_decoder.h>
82 #include <linux/video_encoder.h>
83 #include <linux/mutex.h>
85 #include "zoran_device.h"
86 #include "zoran_card.h"
88 /* we declare some card type definitions here, they mean
89 * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
90 #define ZORAN_V4L2_VID_FLAGS ( \
92 V4L2_CAP_VIDEO_CAPTURE |\
93 V4L2_CAP_VIDEO_OUTPUT |\
94 V4L2_CAP_VIDEO_OVERLAY \
98 #if defined(CONFIG_VIDEO_V4L1_COMPAT)
99 #define ZFMT(pal, fcc, cs) \
100 .palette = (pal), .fourcc = (fcc), .colorspace = (cs)
102 #define ZFMT(pal, fcc, cs) \
103 .fourcc = (fcc), .colorspace = (cs)
106 const struct zoran_format zoran_formats[] = {
108 .name = "15-bit RGB LE",
109 ZFMT(VIDEO_PALETTE_RGB555,
110 V4L2_PIX_FMT_RGB555, V4L2_COLORSPACE_SRGB),
112 .flags = ZORAN_FORMAT_CAPTURE |
113 ZORAN_FORMAT_OVERLAY,
114 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
115 ZR36057_VFESPFR_LittleEndian,
117 .name = "15-bit RGB BE",
119 V4L2_PIX_FMT_RGB555X, V4L2_COLORSPACE_SRGB),
121 .flags = ZORAN_FORMAT_CAPTURE |
122 ZORAN_FORMAT_OVERLAY,
123 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
125 .name = "16-bit RGB LE",
126 ZFMT(VIDEO_PALETTE_RGB565,
127 V4L2_PIX_FMT_RGB565, V4L2_COLORSPACE_SRGB),
129 .flags = ZORAN_FORMAT_CAPTURE |
130 ZORAN_FORMAT_OVERLAY,
131 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
132 ZR36057_VFESPFR_LittleEndian,
134 .name = "16-bit RGB BE",
136 V4L2_PIX_FMT_RGB565, V4L2_COLORSPACE_SRGB),
138 .flags = ZORAN_FORMAT_CAPTURE |
139 ZORAN_FORMAT_OVERLAY,
140 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
142 .name = "24-bit RGB",
143 ZFMT(VIDEO_PALETTE_RGB24,
144 V4L2_PIX_FMT_BGR24, V4L2_COLORSPACE_SRGB),
146 .flags = ZORAN_FORMAT_CAPTURE |
147 ZORAN_FORMAT_OVERLAY,
148 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
150 .name = "32-bit RGB LE",
151 ZFMT(VIDEO_PALETTE_RGB32,
152 V4L2_PIX_FMT_BGR32, V4L2_COLORSPACE_SRGB),
154 .flags = ZORAN_FORMAT_CAPTURE |
155 ZORAN_FORMAT_OVERLAY,
156 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
158 .name = "32-bit RGB BE",
160 V4L2_PIX_FMT_RGB32, V4L2_COLORSPACE_SRGB),
162 .flags = ZORAN_FORMAT_CAPTURE |
163 ZORAN_FORMAT_OVERLAY,
164 .vfespfr = ZR36057_VFESPFR_RGB888,
166 .name = "4:2:2, packed, YUYV",
167 ZFMT(VIDEO_PALETTE_YUV422,
168 V4L2_PIX_FMT_YUYV, V4L2_COLORSPACE_SMPTE170M),
170 .flags = ZORAN_FORMAT_CAPTURE |
171 ZORAN_FORMAT_OVERLAY,
172 .vfespfr = ZR36057_VFESPFR_YUV422,
174 .name = "4:2:2, packed, UYVY",
175 ZFMT(VIDEO_PALETTE_UYVY,
176 V4L2_PIX_FMT_UYVY, V4L2_COLORSPACE_SMPTE170M),
178 .flags = ZORAN_FORMAT_CAPTURE |
179 ZORAN_FORMAT_OVERLAY,
180 .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
182 .name = "Hardware-encoded Motion-JPEG",
184 V4L2_PIX_FMT_MJPEG, V4L2_COLORSPACE_SMPTE170M),
186 .flags = ZORAN_FORMAT_CAPTURE |
187 ZORAN_FORMAT_PLAYBACK |
188 ZORAN_FORMAT_COMPRESSED,
191 #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
193 // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
196 extern int v4l_nbufs;
197 extern int v4l_bufsize;
198 extern int jpg_nbufs;
199 extern int jpg_bufsize;
200 extern int pass_through;
202 static int lock_norm; /* 0 = default 1 = Don't change TV standard (norm) */
203 module_param(lock_norm, int, 0644);
204 MODULE_PARM_DESC(lock_norm, "Prevent norm changes (1 = ignore, >1 = fail)");
206 /* small helper function for calculating buffersizes for v4l2
207 * we calculate the nearest higher power-of-two, which
208 * will be the recommended buffersize */
210 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
212 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
213 __u32 num = (1024 * 512) / (div);
222 if (result > jpg_bufsize)
229 /* forward references */
230 static void v4l_fbuffer_free(struct file *file);
231 static void jpg_fbuffer_free(struct file *file);
234 * Allocate the V4L grab buffers
236 * These have to be pysically contiguous.
237 * If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
238 * else we try to allocate them with bigphysarea_alloc_pages
239 * if the bigphysarea patch is present in the kernel,
240 * else we try to use high memory (if the user has bootet
241 * Linux with the necessary memory left over).
245 get_high_mem (unsigned long size)
248 * Check if there is usable memory at the end of Linux memory
249 * of at least size. Return the physical address of this memory,
250 * return 0 on failure.
252 * The idea is from Alexandro Rubini's book "Linux device drivers".
253 * The driver from him which is downloadable from O'Reilly's
254 * web site misses the "virt_to_phys(high_memory)" part
255 * (and therefore doesn't work at all - at least with 2.2.x kernels).
257 * It should be unnecessary to mention that THIS IS DANGEROUS,
258 * if more than one driver at a time has the idea to use this memory!!!!
261 volatile unsigned char __iomem *mem;
263 unsigned long hi_mem_ph;
266 /* Map the high memory to user space */
268 hi_mem_ph = virt_to_phys(high_memory);
270 mem = ioremap(hi_mem_ph, size);
273 KERN_ERR "%s: get_high_mem() - ioremap failed\n",
278 for (i = 0; i < size; i++) {
279 /* Check if it is memory */
282 if (readb(mem + i) != c)
286 if (readb(mem + i) != c)
288 writeb(0, mem + i); /* zero out memory */
290 /* give the kernel air to breath */
291 if ((i & 0x3ffff) == 0x3ffff)
300 "%s: get_high_mem() - requested %lu, avail %lu\n",
301 ZORAN_NAME, size, i);
309 v4l_fbuffer_alloc (struct file *file)
311 struct zoran_fh *fh = file->private_data;
312 struct zoran *zr = fh->zr;
315 unsigned long pmem = 0;
317 /* we might have old buffers lying around... */
318 if (fh->v4l_buffers.ready_to_be_freed) {
319 v4l_fbuffer_free(file);
322 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
323 if (fh->v4l_buffers.buffer[i].fbuffer)
326 "%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
330 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
333 mem = kmalloc(fh->v4l_buffers.buffer_size, GFP_KERNEL);
337 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
339 v4l_fbuffer_free(file);
342 fh->v4l_buffers.buffer[i].fbuffer = mem;
343 fh->v4l_buffers.buffer[i].fbuffer_phys =
345 fh->v4l_buffers.buffer[i].fbuffer_bus =
347 for (off = 0; off < fh->v4l_buffers.buffer_size;
349 SetPageReserved(MAP_NR(mem + off));
352 "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
353 ZR_DEVNAME(zr), i, (unsigned long) mem,
357 /* Use high memory which has been left at boot time */
359 /* Ok., Ok. this is an evil hack - we make
360 * the assumption that physical addresses are
361 * the same as bus addresses (true at least
362 * for Intel processors). The whole method of
363 * obtaining and using this memory is not very
364 * nice - but I hope it saves some poor users
365 * from kernel hacking, which might have even
366 * more evil results */
370 fh->v4l_buffers.num_buffers *
371 fh->v4l_buffers.buffer_size;
373 pmem = get_high_mem(size);
377 "%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
378 ZR_DEVNAME(zr), size >> 10);
381 fh->v4l_buffers.buffer[0].fbuffer = NULL;
382 fh->v4l_buffers.buffer[0].fbuffer_phys = pmem;
383 fh->v4l_buffers.buffer[0].fbuffer_bus = pmem;
386 "%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
387 ZR_DEVNAME(zr), size >> 10);
389 fh->v4l_buffers.buffer[i].fbuffer = NULL;
390 fh->v4l_buffers.buffer[i].fbuffer_phys =
391 pmem + i * fh->v4l_buffers.buffer_size;
392 fh->v4l_buffers.buffer[i].fbuffer_bus =
393 pmem + i * fh->v4l_buffers.buffer_size;
398 fh->v4l_buffers.allocated = 1;
403 /* free the V4L grab buffers */
405 v4l_fbuffer_free (struct file *file)
407 struct zoran_fh *fh = file->private_data;
408 struct zoran *zr = fh->zr;
412 dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
414 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
415 if (!fh->v4l_buffers.buffer[i].fbuffer)
418 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
419 mem = fh->v4l_buffers.buffer[i].fbuffer;
420 for (off = 0; off < fh->v4l_buffers.buffer_size;
422 ClearPageReserved(MAP_NR(mem + off));
423 kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
425 fh->v4l_buffers.buffer[i].fbuffer = NULL;
428 fh->v4l_buffers.allocated = 0;
429 fh->v4l_buffers.ready_to_be_freed = 0;
433 * Allocate the MJPEG grab buffers.
435 * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
436 * kmalloc is used to request a physically contiguous area,
437 * else we allocate the memory in framgents with get_zeroed_page.
439 * If a Natoma chipset is present and this is a revision 1 zr36057,
440 * each MJPEG buffer needs to be physically contiguous.
441 * (RJ: This statement is from Dave Perks' original driver,
442 * I could never check it because I have a zr36067)
443 * The driver cares about this because it reduces the buffer
444 * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
446 * RJ: The contents grab buffers needs never be accessed in the driver.
447 * Therefore there is no need to allocate them with vmalloc in order
448 * to get a contiguous virtual memory space.
449 * I don't understand why many other drivers first allocate them with
450 * vmalloc (which uses internally also get_zeroed_page, but delivers you
451 * virtual addresses) and then again have to make a lot of efforts
452 * to get the physical address.
455 * On big-endian architectures (such as ppc) some extra steps
456 * are needed. When reading and writing to the stat_com array
457 * and fragment buffers, the device expects to see little-
458 * endian values. The use of cpu_to_le32() and le32_to_cpu()
459 * in this function (and one or two others in zoran_device.c)
460 * ensure that these values are always stored in little-endian
461 * form, regardless of architecture. The zr36057 does Very Bad
462 * Things on big endian architectures if the stat_com array
463 * and fragment buffers are not little-endian.
467 jpg_fbuffer_alloc (struct file *file)
469 struct zoran_fh *fh = file->private_data;
470 struct zoran *zr = fh->zr;
474 /* we might have old buffers lying around */
475 if (fh->jpg_buffers.ready_to_be_freed) {
476 jpg_fbuffer_free(file);
479 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
480 if (fh->jpg_buffers.buffer[i].frag_tab)
483 "%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
486 /* Allocate fragment table for this buffer */
488 mem = get_zeroed_page(GFP_KERNEL);
492 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
494 jpg_fbuffer_free(file);
497 fh->jpg_buffers.buffer[i].frag_tab = (__le32 *) mem;
498 fh->jpg_buffers.buffer[i].frag_tab_bus =
499 virt_to_bus((void *) mem);
501 //if (alloc_contig) {
502 if (fh->jpg_buffers.need_contiguous) {
504 (unsigned long) kmalloc(fh->jpg_buffers.
510 "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
512 jpg_fbuffer_free(file);
515 fh->jpg_buffers.buffer[i].frag_tab[0] =
516 cpu_to_le32(virt_to_bus((void *) mem));
517 fh->jpg_buffers.buffer[i].frag_tab[1] =
518 cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
519 for (off = 0; off < fh->jpg_buffers.buffer_size;
521 SetPageReserved(MAP_NR(mem + off));
523 /* jpg_bufsize is allreay page aligned */
525 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
527 mem = get_zeroed_page(GFP_KERNEL);
531 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
533 jpg_fbuffer_free(file);
537 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
538 cpu_to_le32(virt_to_bus((void *) mem));
539 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
541 cpu_to_le32((PAGE_SIZE / 4) << 1);
542 SetPageReserved(MAP_NR(mem));
545 fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
550 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
552 (fh->jpg_buffers.num_buffers *
553 fh->jpg_buffers.buffer_size) >> 10);
555 fh->jpg_buffers.allocated = 1;
560 /* free the MJPEG grab buffers */
562 jpg_fbuffer_free (struct file *file)
564 struct zoran_fh *fh = file->private_data;
565 struct zoran *zr = fh->zr;
569 dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
571 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
572 if (!fh->jpg_buffers.buffer[i].frag_tab)
575 //if (alloc_contig) {
576 if (fh->jpg_buffers.need_contiguous) {
577 if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
578 mem = (unsigned char *) bus_to_virt(le32_to_cpu(
579 fh->jpg_buffers.buffer[i].frag_tab[0]));
581 off < fh->jpg_buffers.buffer_size;
583 ClearPageReserved(MAP_NR
586 fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
587 fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
591 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
593 if (!fh->jpg_buffers.buffer[i].
596 ClearPageReserved(MAP_NR
600 buffer[i].frag_tab[2 *
602 free_page((unsigned long)
608 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
610 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
615 free_page((unsigned long) fh->jpg_buffers.buffer[i].
617 fh->jpg_buffers.buffer[i].frag_tab = NULL;
620 fh->jpg_buffers.allocated = 0;
621 fh->jpg_buffers.ready_to_be_freed = 0;
625 * V4L Buffer grabbing
629 zoran_v4l_set_format (struct file *file,
632 const struct zoran_format *format)
634 struct zoran_fh *fh = file->private_data;
635 struct zoran *zr = fh->zr;
638 /* Check size and format of the grab wanted */
640 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
641 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
644 "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
645 ZR_DEVNAME(zr), width, height);
649 bpp = (format->depth + 7) / 8;
651 /* Check against available buffer size */
652 if (height * width * bpp > fh->v4l_buffers.buffer_size) {
655 "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
656 ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
660 /* The video front end needs 4-byte alinged line sizes */
662 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
665 "%s: v4l_set_format() - wrong frame alingment\n",
670 fh->v4l_settings.width = width;
671 fh->v4l_settings.height = height;
672 fh->v4l_settings.format = format;
673 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
679 zoran_v4l_queue_frame (struct file *file,
682 struct zoran_fh *fh = file->private_data;
683 struct zoran *zr = fh->zr;
687 if (!fh->v4l_buffers.allocated) {
690 "%s: v4l_queue_frame() - buffers not yet allocated\n",
695 /* No grabbing outside the buffer range! */
696 if (num >= fh->v4l_buffers.num_buffers || num < 0) {
699 "%s: v4l_queue_frame() - buffer %d is out of range\n",
700 ZR_DEVNAME(zr), num);
704 spin_lock_irqsave(&zr->spinlock, flags);
706 if (fh->v4l_buffers.active == ZORAN_FREE) {
707 if (zr->v4l_buffers.active == ZORAN_FREE) {
708 zr->v4l_buffers = fh->v4l_buffers;
709 fh->v4l_buffers.active = ZORAN_ACTIVE;
713 "%s: v4l_queue_frame() - another session is already capturing\n",
719 /* make sure a grab isn't going on currently with this buffer */
721 switch (zr->v4l_buffers.buffer[num].state) {
724 if (zr->v4l_buffers.active == ZORAN_FREE) {
725 fh->v4l_buffers.active = ZORAN_FREE;
726 zr->v4l_buffers.allocated = 0;
728 res = -EBUSY; /* what are you doing? */
733 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
734 ZR_DEVNAME(zr), num);
736 /* since there is at least one unused buffer there's room for at least
737 * one more pend[] entry */
738 zr->v4l_pend[zr->v4l_pend_head++ &
739 V4L_MASK_FRAME] = num;
740 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
741 zr->v4l_buffers.buffer[num].bs.length =
742 fh->v4l_settings.bytesperline *
743 zr->v4l_settings.height;
744 fh->v4l_buffers.buffer[num] =
745 zr->v4l_buffers.buffer[num];
750 spin_unlock_irqrestore(&zr->spinlock, flags);
752 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
753 zr->v4l_buffers.active = fh->v4l_buffers.active;
759 v4l_grab (struct file *file,
760 struct video_mmap *mp)
762 struct zoran_fh *fh = file->private_data;
763 struct zoran *zr = fh->zr;
766 for (i = 0; i < NUM_FORMATS; i++) {
767 if (zoran_formats[i].palette == mp->format &&
768 zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
769 !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
772 if (i == NUM_FORMATS || zoran_formats[i].depth == 0) {
775 "%s: v4l_grab() - wrong bytes-per-pixel format\n",
781 * To minimize the time spent in the IRQ routine, we avoid setting up
782 * the video front end there.
783 * If this grab has different parameters from a running streaming capture
784 * we stop the streaming capture and start it over again.
786 if (zr->v4l_memgrab_active &&
787 (zr->v4l_settings.width != mp->width ||
788 zr->v4l_settings.height != mp->height ||
789 zr->v4l_settings.format->palette != mp->format)) {
790 res = wait_grab_pending(zr);
794 if ((res = zoran_v4l_set_format(file,
799 zr->v4l_settings = fh->v4l_settings;
801 /* queue the frame in the pending queue */
802 if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
803 fh->v4l_buffers.active = ZORAN_FREE;
807 /* put the 36057 into frame grabbing mode */
808 if (!res && !zr->v4l_memgrab_active)
809 zr36057_set_memgrab(zr, 1);
811 //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
817 * Sync on a V4L buffer
821 v4l_sync (struct file *file,
824 struct zoran_fh *fh = file->private_data;
825 struct zoran *zr = fh->zr;
828 if (fh->v4l_buffers.active == ZORAN_FREE) {
831 "%s: v4l_sync() - no grab active for this session\n",
836 /* check passed-in frame number */
837 if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
839 KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
840 ZR_DEVNAME(zr), frame);
844 /* Check if is buffer was queued at all */
845 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
848 "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
853 /* wait on this buffer to get ready */
854 if (!wait_event_interruptible_timeout(zr->v4l_capq,
855 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
858 if (signal_pending(current))
861 /* buffer should now be in BUZ_STATE_DONE */
862 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
864 KERN_ERR "%s: v4l_sync() - internal state error\n",
867 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
868 fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
870 spin_lock_irqsave(&zr->spinlock, flags);
872 /* Check if streaming capture has finished */
873 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
874 zr36057_set_memgrab(zr, 0);
875 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
876 fh->v4l_buffers.active = zr->v4l_buffers.active =
878 zr->v4l_buffers.allocated = 0;
882 spin_unlock_irqrestore(&zr->spinlock, flags);
888 * Queue a MJPEG buffer for capture/playback
892 zoran_jpg_queue_frame (struct file *file,
894 enum zoran_codec_mode mode)
896 struct zoran_fh *fh = file->private_data;
897 struct zoran *zr = fh->zr;
901 /* Check if buffers are allocated */
902 if (!fh->jpg_buffers.allocated) {
905 "%s: jpg_queue_frame() - buffers not yet allocated\n",
910 /* No grabbing outside the buffer range! */
911 if (num >= fh->jpg_buffers.num_buffers || num < 0) {
914 "%s: jpg_queue_frame() - buffer %d out of range\n",
915 ZR_DEVNAME(zr), num);
919 /* what is the codec mode right now? */
920 if (zr->codec_mode == BUZ_MODE_IDLE) {
921 zr->jpg_settings = fh->jpg_settings;
922 } else if (zr->codec_mode != mode) {
923 /* wrong codec mode active - invalid */
926 "%s: jpg_queue_frame() - codec in wrong mode\n",
931 if (fh->jpg_buffers.active == ZORAN_FREE) {
932 if (zr->jpg_buffers.active == ZORAN_FREE) {
933 zr->jpg_buffers = fh->jpg_buffers;
934 fh->jpg_buffers.active = ZORAN_ACTIVE;
938 "%s: jpg_queue_frame() - another session is already capturing\n",
944 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
945 /* Ok load up the jpeg codec */
946 zr36057_enable_jpg(zr, mode);
949 spin_lock_irqsave(&zr->spinlock, flags);
952 switch (zr->jpg_buffers.buffer[num].state) {
956 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
959 /* since there is at least one unused buffer there's room for at
960 *least one more pend[] entry */
961 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
963 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
964 fh->jpg_buffers.buffer[num] =
965 zr->jpg_buffers.buffer[num];
966 zoran_feed_stat_com(zr);
971 if (zr->jpg_buffers.active == ZORAN_FREE) {
972 fh->jpg_buffers.active = ZORAN_FREE;
973 zr->jpg_buffers.allocated = 0;
975 res = -EBUSY; /* what are you doing? */
980 spin_unlock_irqrestore(&zr->spinlock, flags);
982 if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
983 zr->jpg_buffers.active = fh->jpg_buffers.active;
990 jpg_qbuf (struct file *file,
992 enum zoran_codec_mode mode)
994 struct zoran_fh *fh = file->private_data;
995 struct zoran *zr = fh->zr;
998 /* Does the user want to stop streaming? */
1000 if (zr->codec_mode == mode) {
1001 if (fh->jpg_buffers.active == ZORAN_FREE) {
1004 "%s: jpg_qbuf(-1) - session not active\n",
1008 fh->jpg_buffers.active = zr->jpg_buffers.active =
1010 zr->jpg_buffers.allocated = 0;
1011 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1016 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
1022 if ((res = zoran_jpg_queue_frame(file, frame, mode)))
1025 /* Start the jpeg codec when the first frame is queued */
1026 if (!res && zr->jpg_que_head == 1)
1033 * Sync on a MJPEG buffer
1037 jpg_sync (struct file *file,
1038 struct zoran_sync *bs)
1040 struct zoran_fh *fh = file->private_data;
1041 struct zoran *zr = fh->zr;
1042 unsigned long flags;
1045 if (fh->jpg_buffers.active == ZORAN_FREE) {
1048 "%s: jpg_sync() - capture is not currently active\n",
1052 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
1053 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
1056 "%s: jpg_sync() - codec not in streaming mode\n",
1060 if (!wait_event_interruptible_timeout(zr->jpg_capq,
1061 (zr->jpg_que_tail != zr->jpg_dma_tail ||
1062 zr->jpg_dma_tail == zr->jpg_dma_head),
1066 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1068 zr->codec->control(zr->codec, CODEC_G_STATUS,
1072 "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
1073 ZR_DEVNAME(zr), isr);
1078 if (signal_pending(current))
1079 return -ERESTARTSYS;
1081 spin_lock_irqsave(&zr->spinlock, flags);
1083 if (zr->jpg_dma_tail != zr->jpg_dma_head)
1084 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
1086 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
1088 /* buffer should now be in BUZ_STATE_DONE */
1089 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
1091 KERN_ERR "%s: jpg_sync() - internal state error\n",
1094 *bs = zr->jpg_buffers.buffer[frame].bs;
1096 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
1097 fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
1099 spin_unlock_irqrestore(&zr->spinlock, flags);
1105 zoran_open_init_session (struct file *file)
1108 struct zoran_fh *fh = file->private_data;
1109 struct zoran *zr = fh->zr;
1111 /* Per default, map the V4L Buffers */
1112 fh->map_mode = ZORAN_MAP_MODE_RAW;
1114 /* take over the card's current settings */
1115 fh->overlay_settings = zr->overlay_settings;
1116 fh->overlay_settings.is_set = 0;
1117 fh->overlay_settings.format = zr->overlay_settings.format;
1118 fh->overlay_active = ZORAN_FREE;
1121 fh->v4l_settings = zr->v4l_settings;
1124 memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
1125 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1126 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1127 fh->v4l_buffers.buffer[i].bs.frame = i;
1129 fh->v4l_buffers.allocated = 0;
1130 fh->v4l_buffers.ready_to_be_freed = 0;
1131 fh->v4l_buffers.active = ZORAN_FREE;
1132 fh->v4l_buffers.buffer_size = v4l_bufsize;
1133 fh->v4l_buffers.num_buffers = v4l_nbufs;
1136 fh->jpg_settings = zr->jpg_settings;
1139 memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
1140 for (i = 0; i < BUZ_MAX_FRAME; i++) {
1141 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1142 fh->jpg_buffers.buffer[i].bs.frame = i;
1144 fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
1145 fh->jpg_buffers.allocated = 0;
1146 fh->jpg_buffers.ready_to_be_freed = 0;
1147 fh->jpg_buffers.active = ZORAN_FREE;
1148 fh->jpg_buffers.buffer_size = jpg_bufsize;
1149 fh->jpg_buffers.num_buffers = jpg_nbufs;
1153 zoran_close_end_session (struct file *file)
1155 struct zoran_fh *fh = file->private_data;
1156 struct zoran *zr = fh->zr;
1159 if (fh->overlay_active != ZORAN_FREE) {
1160 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
1161 zr->v4l_overlay_active = 0;
1162 if (!zr->v4l_memgrab_active)
1163 zr36057_overlay(zr, 0);
1164 zr->overlay_mask = NULL;
1168 if (fh->v4l_buffers.active != ZORAN_FREE) {
1169 unsigned long flags;
1171 spin_lock_irqsave(&zr->spinlock, flags);
1172 zr36057_set_memgrab(zr, 0);
1173 zr->v4l_buffers.allocated = 0;
1174 zr->v4l_buffers.active = fh->v4l_buffers.active =
1176 spin_unlock_irqrestore(&zr->spinlock, flags);
1180 if (fh->v4l_buffers.allocated ||
1181 fh->v4l_buffers.ready_to_be_freed) {
1182 v4l_fbuffer_free(file);
1186 if (fh->jpg_buffers.active != ZORAN_FREE) {
1187 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1188 zr->jpg_buffers.allocated = 0;
1189 zr->jpg_buffers.active = fh->jpg_buffers.active =
1194 if (fh->jpg_buffers.allocated ||
1195 fh->jpg_buffers.ready_to_be_freed) {
1196 jpg_fbuffer_free(file);
1201 * Open a zoran card. Right now the flags stuff is just playing
1205 zoran_open (struct inode *inode,
1208 unsigned int minor = iminor(inode);
1209 struct zoran *zr = NULL;
1210 struct zoran_fh *fh;
1211 int i, res, first_open = 0, have_module_locks = 0;
1213 /* find the device */
1214 for (i = 0; i < zoran_num; i++) {
1215 if (zoran[i].video_dev->minor == minor) {
1222 dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
1224 goto open_unlock_and_return;
1227 /* see fs/device.c - the kernel already locks during open(),
1228 * so locking ourselves only causes deadlocks */
1229 /*mutex_lock(&zr->resource_lock);*/
1233 KERN_ERR "%s: no TV decoder loaded for device!\n",
1236 goto open_unlock_and_return;
1239 /* try to grab a module lock */
1240 if (!try_module_get(THIS_MODULE)) {
1243 "%s: failed to acquire my own lock! PANIC!\n",
1246 goto open_unlock_and_return;
1248 if (!try_module_get(zr->decoder->driver->driver.owner)) {
1251 "%s: failed to grab ownership of i2c decoder\n",
1254 module_put(THIS_MODULE);
1255 goto open_unlock_and_return;
1258 !try_module_get(zr->encoder->driver->driver.owner)) {
1261 "%s: failed to grab ownership of i2c encoder\n",
1264 module_put(zr->decoder->driver->driver.owner);
1265 module_put(THIS_MODULE);
1266 goto open_unlock_and_return;
1269 have_module_locks = 1;
1271 if (zr->user >= 2048) {
1272 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1273 ZR_DEVNAME(zr), zr->user);
1275 goto open_unlock_and_return;
1278 dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1279 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user);
1281 /* now, create the open()-specific file_ops struct */
1282 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1286 "%s: zoran_open() - allocation of zoran_fh failed\n",
1289 goto open_unlock_and_return;
1291 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1292 * on norm-change! */
1294 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1295 if (!fh->overlay_mask) {
1298 "%s: zoran_open() - allocation of overlay_mask failed\n",
1302 goto open_unlock_and_return;
1305 if (zr->user++ == 0)
1308 /*mutex_unlock(&zr->resource_lock);*/
1310 /* default setup - TODO: look at flags */
1311 if (first_open) { /* First device open */
1312 zr36057_restart(zr);
1313 zoran_open_init_params(zr);
1314 zoran_init_hardware(zr);
1316 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1319 /* set file_ops stuff */
1320 file->private_data = fh;
1322 zoran_open_init_session(file);
1326 open_unlock_and_return:
1327 /* if we grabbed locks, release them accordingly */
1328 if (have_module_locks) {
1329 module_put(zr->decoder->driver->driver.owner);
1331 module_put(zr->encoder->driver->driver.owner);
1333 module_put(THIS_MODULE);
1336 /* if there's no device found, we didn't obtain the lock either */
1338 /*mutex_unlock(&zr->resource_lock);*/
1345 zoran_close (struct inode *inode,
1348 struct zoran_fh *fh = file->private_data;
1349 struct zoran *zr = fh->zr;
1351 dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1352 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user);
1354 /* kernel locks (fs/device.c), so don't do that ourselves
1355 * (prevents deadlocks) */
1356 /*mutex_lock(&zr->resource_lock);*/
1358 zoran_close_end_session(file);
1360 if (zr->user-- == 1) { /* Last process */
1361 /* Clean up JPEG process */
1362 wake_up_interruptible(&zr->jpg_capq);
1363 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1364 zr->jpg_buffers.allocated = 0;
1365 zr->jpg_buffers.active = ZORAN_FREE;
1367 /* disable interrupts */
1368 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1370 if (zr36067_debug > 1)
1371 print_interrupts(zr);
1374 zr->v4l_overlay_active = 0;
1375 zr36057_overlay(zr, 0);
1376 zr->overlay_mask = NULL;
1379 wake_up_interruptible(&zr->v4l_capq);
1380 zr36057_set_memgrab(zr, 0);
1381 zr->v4l_buffers.allocated = 0;
1382 zr->v4l_buffers.active = ZORAN_FREE;
1383 zoran_set_pci_master(zr, 0);
1385 if (!pass_through) { /* Switch to color bar */
1386 int zero = 0, two = 2;
1387 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1388 encoder_command(zr, ENCODER_SET_INPUT, &two);
1392 file->private_data = NULL;
1393 kfree(fh->overlay_mask);
1396 /* release locks on the i2c modules */
1397 module_put(zr->decoder->driver->driver.owner);
1399 module_put(zr->encoder->driver->driver.owner);
1401 module_put(THIS_MODULE);
1403 /*mutex_unlock(&zr->resource_lock);*/
1405 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1412 zoran_read (struct file *file,
1417 /* we simply don't support read() (yet)... */
1423 zoran_write (struct file *file,
1424 const char __user *data,
1428 /* ...and the same goes for write() */
1434 setup_fbuffer (struct file *file,
1436 const struct zoran_format *fmt,
1441 struct zoran_fh *fh = file->private_data;
1442 struct zoran *zr = fh->zr;
1444 /* (Ronald) v4l/v4l2 guidelines */
1445 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1448 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1449 ALi Magik (that needs very low latency while the card needs a
1450 higher value always) */
1452 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1455 /* we need a bytesperline value, even if not given */
1457 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1460 if (zr->overlay_active) {
1461 /* dzjee... stupid users... don't even bother to turn off
1462 * overlay before changing the memory location...
1463 * normally, we would return errors here. However, one of
1464 * the tools that does this is... xawtv! and since xawtv
1465 * is used by +/- 99% of the users, we'd rather be user-
1466 * friendly and silently do as if nothing went wrong */
1469 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1471 zr36057_overlay(zr, 0);
1475 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1478 "%s: setup_fbuffer() - no valid overlay format given\n",
1482 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1485 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1486 ZR_DEVNAME(zr), width, height, bytesperline);
1489 if (bytesperline & 3) {
1492 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1493 ZR_DEVNAME(zr), bytesperline);
1497 zr->buffer.base = (void *) ((unsigned long) base & ~3);
1498 zr->buffer.height = height;
1499 zr->buffer.width = width;
1500 zr->buffer.depth = fmt->depth;
1501 zr->overlay_settings.format = fmt;
1502 zr->buffer.bytesperline = bytesperline;
1504 /* The user should set new window parameters */
1505 zr->overlay_settings.is_set = 0;
1512 setup_window (struct file *file,
1517 struct video_clip __user *clips,
1519 void __user *bitmap)
1521 struct zoran_fh *fh = file->private_data;
1522 struct zoran *zr = fh->zr;
1523 struct video_clip *vcp = NULL;
1527 if (!zr->buffer.base) {
1530 "%s: setup_window() - frame buffer has to be set first\n",
1535 if (!fh->overlay_settings.format) {
1538 "%s: setup_window() - no overlay format set\n",
1544 * The video front end needs 4-byte alinged line sizes, we correct that
1545 * silently here if necessary
1547 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1548 end = (x + width) & ~1; /* round down */
1549 x = (x + 1) & ~1; /* round up */
1553 if (zr->buffer.depth == 24) {
1554 end = (x + width) & ~3; /* round down */
1555 x = (x + 3) & ~3; /* round up */
1559 if (width > BUZ_MAX_WIDTH)
1560 width = BUZ_MAX_WIDTH;
1561 if (height > BUZ_MAX_HEIGHT)
1562 height = BUZ_MAX_HEIGHT;
1564 /* Check for vaild parameters */
1565 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1566 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1569 "%s: setup_window() - width = %d or height = %d invalid\n",
1570 ZR_DEVNAME(zr), width, height);
1574 fh->overlay_settings.x = x;
1575 fh->overlay_settings.y = y;
1576 fh->overlay_settings.width = width;
1577 fh->overlay_settings.height = height;
1578 fh->overlay_settings.clipcount = clipcount;
1581 * If an overlay is running, we have to switch it off
1582 * and switch it on again in order to get the new settings in effect.
1584 * We also want to avoid that the overlay mask is written
1585 * when an overlay is running.
1588 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1589 zr->overlay_active != ZORAN_FREE &&
1590 fh->overlay_active != ZORAN_FREE;
1592 zr36057_overlay(zr, 0);
1595 * Write the overlay mask if clips are wanted.
1596 * We prefer a bitmap.
1599 /* fake value - it just means we want clips */
1600 fh->overlay_settings.clipcount = 1;
1602 if (copy_from_user(fh->overlay_mask, bitmap,
1603 (width * height + 7) / 8)) {
1606 } else if (clipcount > 0) {
1607 /* write our own bitmap from the clips */
1608 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1612 "%s: setup_window() - Alloc of clip mask failed\n",
1617 (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1621 write_overlay_mask(file, vcp, clipcount);
1625 fh->overlay_settings.is_set = 1;
1626 if (fh->overlay_active != ZORAN_FREE &&
1627 zr->overlay_active != ZORAN_FREE)
1628 zr->overlay_settings = fh->overlay_settings;
1631 zr36057_overlay(zr, 1);
1633 /* Make sure the changes come into effect */
1634 return wait_grab_pending(zr);
1638 setup_overlay (struct file *file,
1641 struct zoran_fh *fh = file->private_data;
1642 struct zoran *zr = fh->zr;
1644 /* If there is nothing to do, return immediatly */
1645 if ((on && fh->overlay_active != ZORAN_FREE) ||
1646 (!on && fh->overlay_active == ZORAN_FREE))
1649 /* check whether we're touching someone else's overlay */
1650 if (on && zr->overlay_active != ZORAN_FREE &&
1651 fh->overlay_active == ZORAN_FREE) {
1654 "%s: setup_overlay() - overlay is already active for another session\n",
1658 if (!on && zr->overlay_active != ZORAN_FREE &&
1659 fh->overlay_active == ZORAN_FREE) {
1662 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1668 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1669 zr->v4l_overlay_active = 0;
1670 /* When a grab is running, the video simply
1671 * won't be switched on any more */
1672 if (!zr->v4l_memgrab_active)
1673 zr36057_overlay(zr, 0);
1674 zr->overlay_mask = NULL;
1676 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1679 "%s: setup_overlay() - buffer or window not set\n",
1683 if (!fh->overlay_settings.format) {
1686 "%s: setup_overlay() - no overlay format set\n",
1690 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1691 zr->v4l_overlay_active = 1;
1692 zr->overlay_mask = fh->overlay_mask;
1693 zr->overlay_settings = fh->overlay_settings;
1694 if (!zr->v4l_memgrab_active)
1695 zr36057_overlay(zr, 1);
1696 /* When a grab is running, the video will be
1697 * switched on when grab is finished */
1700 /* Make sure the changes come into effect */
1701 return wait_grab_pending(zr);
1704 /* get the status of a buffer in the clients buffer queue */
1706 zoran_v4l2_buffer_status (struct file *file,
1707 struct v4l2_buffer *buf,
1710 struct zoran_fh *fh = file->private_data;
1711 struct zoran *zr = fh->zr;
1713 buf->flags = V4L2_BUF_FLAG_MAPPED;
1715 switch (fh->map_mode) {
1716 case ZORAN_MAP_MODE_RAW:
1719 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1720 !fh->v4l_buffers.allocated) {
1723 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1728 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1729 buf->length = fh->v4l_buffers.buffer_size;
1732 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1733 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1734 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1735 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1736 buf->flags |= V4L2_BUF_FLAG_DONE;
1738 fh->v4l_buffers.buffer[num].bs.timestamp;
1740 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1743 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1744 buf->field = V4L2_FIELD_TOP;
1746 buf->field = V4L2_FIELD_INTERLACED;
1750 case ZORAN_MAP_MODE_JPG_REC:
1751 case ZORAN_MAP_MODE_JPG_PLAY:
1754 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1755 !fh->jpg_buffers.allocated) {
1758 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1763 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1764 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1765 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1766 buf->length = fh->jpg_buffers.buffer_size;
1768 /* these variables are only written after frame has been captured */
1769 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1770 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1771 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1773 fh->jpg_buffers.buffer[num].bs.timestamp;
1775 fh->jpg_buffers.buffer[num].bs.length;
1776 buf->flags |= V4L2_BUF_FLAG_DONE;
1778 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1781 /* which fields are these? */
1782 if (fh->jpg_settings.TmpDcm != 1)
1785 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1789 odd_even ? V4L2_FIELD_SEQ_TB :
1798 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1799 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1803 buf->memory = V4L2_MEMORY_MMAP;
1805 buf->m.offset = buf->length * num;
1811 zoran_set_norm (struct zoran *zr,
1812 int norm) /* VIDEO_MODE_* */
1814 int norm_encoder, on;
1816 if (zr->v4l_buffers.active != ZORAN_FREE ||
1817 zr->jpg_buffers.active != ZORAN_FREE) {
1820 "%s: set_norm() called while in playback/capture mode\n",
1825 if (lock_norm && norm != zr->norm) {
1826 if (lock_norm > 1) {
1829 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1835 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1841 if (norm != VIDEO_MODE_AUTO &&
1842 (norm < 0 || norm >= zr->card.norms ||
1843 !zr->card.tvn[norm])) {
1845 KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1846 ZR_DEVNAME(zr), norm);
1850 if (norm == VIDEO_MODE_AUTO) {
1853 /* if we have autodetect, ... */
1854 struct video_decoder_capability caps;
1855 decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1856 if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1857 dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1862 decoder_command(zr, DECODER_SET_NORM, &norm);
1864 /* let changes come into effect */
1867 decoder_command(zr, DECODER_GET_STATUS, &status);
1868 if (!(status & DECODER_STATUS_GOOD)) {
1871 "%s: set_norm() - no norm detected\n",
1874 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1878 if (status & DECODER_STATUS_NTSC)
1879 norm = VIDEO_MODE_NTSC;
1880 else if (status & DECODER_STATUS_SECAM)
1881 norm = VIDEO_MODE_SECAM;
1883 norm = VIDEO_MODE_PAL;
1885 zr->timing = zr->card.tvn[norm];
1886 norm_encoder = norm;
1888 /* We switch overlay off and on since a change in the
1889 * norm needs different VFE settings */
1890 on = zr->overlay_active && !zr->v4l_memgrab_active;
1892 zr36057_overlay(zr, 0);
1894 decoder_command(zr, DECODER_SET_NORM, &norm);
1895 encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1898 zr36057_overlay(zr, 1);
1900 /* Make sure the changes come into effect */
1907 zoran_set_input (struct zoran *zr,
1912 if (input == zr->input) {
1916 if (zr->v4l_buffers.active != ZORAN_FREE ||
1917 zr->jpg_buffers.active != ZORAN_FREE) {
1920 "%s: set_input() called while in playback/capture mode\n",
1925 if (input < 0 || input >= zr->card.inputs) {
1928 "%s: set_input() - unnsupported input %d\n",
1929 ZR_DEVNAME(zr), input);
1933 realinput = zr->card.input[input].muxsel;
1936 decoder_command(zr, DECODER_SET_INPUT, &realinput);
1946 zoran_do_ioctl (struct inode *inode,
1951 struct zoran_fh *fh = file->private_data;
1952 struct zoran *zr = fh->zr;
1953 /* CAREFUL: used in multiple places here */
1954 struct zoran_jpg_settings settings;
1956 /* we might have older buffers lying around... We don't want
1957 * to wait, but we do want to try cleaning them up ASAP. So
1958 * we try to obtain the lock and free them. If that fails, we
1959 * don't do anything and wait for the next turn. In the end,
1960 * zoran_close() or a new allocation will still free them...
1961 * This is just a 'the sooner the better' extra 'feature'
1963 * We don't free the buffers right on munmap() because that
1964 * causes oopses (kfree() inside munmap() oopses for no
1965 * apparent reason - it's also not reproduceable in any way,
1966 * but moving the free code outside the munmap() handler fixes
1967 * all this... If someone knows why, please explain me (Ronald)
1969 if (mutex_trylock(&zr->resource_lock)) {
1970 /* we obtained it! Let's try to free some things */
1971 if (fh->jpg_buffers.ready_to_be_freed)
1972 jpg_fbuffer_free(file);
1973 if (fh->v4l_buffers.ready_to_be_freed)
1974 v4l_fbuffer_free(file);
1976 mutex_unlock(&zr->resource_lock);
1983 struct video_capability *vcap = arg;
1985 dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
1987 memset(vcap, 0, sizeof(struct video_capability));
1988 strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name)-1);
1989 vcap->type = ZORAN_VID_TYPE;
1991 vcap->channels = zr->card.inputs;
1993 mutex_lock(&zr->resource_lock);
1994 vcap->maxwidth = BUZ_MAX_WIDTH;
1995 vcap->maxheight = BUZ_MAX_HEIGHT;
1996 vcap->minwidth = BUZ_MIN_WIDTH;
1997 vcap->minheight = BUZ_MIN_HEIGHT;
1998 mutex_unlock(&zr->resource_lock);
2006 struct video_channel *vchan = arg;
2007 int channel = vchan->channel;
2009 dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
2010 ZR_DEVNAME(zr), vchan->channel);
2012 memset(vchan, 0, sizeof(struct video_channel));
2013 if (channel > zr->card.inputs || channel < 0) {
2016 "%s: VIDIOCGCHAN on not existing channel %d\n",
2017 ZR_DEVNAME(zr), channel);
2021 strcpy(vchan->name, zr->card.input[channel].name);
2025 vchan->type = VIDEO_TYPE_CAMERA;
2026 mutex_lock(&zr->resource_lock);
2027 vchan->norm = zr->norm;
2028 mutex_unlock(&zr->resource_lock);
2029 vchan->channel = channel;
2035 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2037 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2039 * * The famos BTTV driver has it implemented with a struct video_channel argument
2040 * * and we follow it for compatibility reasons
2042 * * BTW: this is the only way the user can set the norm!
2047 struct video_channel *vchan = arg;
2052 "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
2053 ZR_DEVNAME(zr), vchan->channel, vchan->norm);
2055 mutex_lock(&zr->resource_lock);
2056 if ((res = zoran_set_input(zr, vchan->channel)))
2057 goto schan_unlock_and_return;
2058 if ((res = zoran_set_norm(zr, vchan->norm)))
2059 goto schan_unlock_and_return;
2061 /* Make sure the changes come into effect */
2062 res = wait_grab_pending(zr);
2063 schan_unlock_and_return:
2064 mutex_unlock(&zr->resource_lock);
2071 struct video_picture *vpict = arg;
2073 dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
2075 memset(vpict, 0, sizeof(struct video_picture));
2076 mutex_lock(&zr->resource_lock);
2077 vpict->hue = zr->hue;
2078 vpict->brightness = zr->brightness;
2079 vpict->contrast = zr->contrast;
2080 vpict->colour = zr->saturation;
2081 if (fh->overlay_settings.format) {
2082 vpict->depth = fh->overlay_settings.format->depth;
2083 vpict->palette = fh->overlay_settings.format->palette;
2087 mutex_unlock(&zr->resource_lock);
2095 struct video_picture *vpict = arg;
2100 "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
2101 ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
2102 vpict->colour, vpict->contrast, vpict->depth,
2105 for (i = 0; i < NUM_FORMATS; i++) {
2106 const struct zoran_format *fmt = &zoran_formats[i];
2108 if (fmt->palette != -1 &&
2109 fmt->flags & ZORAN_FORMAT_OVERLAY &&
2110 fmt->palette == vpict->palette &&
2111 fmt->depth == vpict->depth)
2114 if (i == NUM_FORMATS) {
2117 "%s: VIDIOCSPICT - Invalid palette %d\n",
2118 ZR_DEVNAME(zr), vpict->palette);
2122 mutex_lock(&zr->resource_lock);
2124 decoder_command(zr, DECODER_SET_PICTURE, vpict);
2126 zr->hue = vpict->hue;
2127 zr->contrast = vpict->contrast;
2128 zr->saturation = vpict->colour;
2129 zr->brightness = vpict->brightness;
2131 fh->overlay_settings.format = &zoran_formats[i];
2133 mutex_unlock(&zr->resource_lock);
2143 dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
2144 ZR_DEVNAME(zr), *on);
2146 mutex_lock(&zr->resource_lock);
2147 res = setup_overlay(file, *on);
2148 mutex_unlock(&zr->resource_lock);
2156 struct video_window *vwin = arg;
2158 dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
2160 memset(vwin, 0, sizeof(struct video_window));
2161 mutex_lock(&zr->resource_lock);
2162 vwin->x = fh->overlay_settings.x;
2163 vwin->y = fh->overlay_settings.y;
2164 vwin->width = fh->overlay_settings.width;
2165 vwin->height = fh->overlay_settings.height;
2166 mutex_unlock(&zr->resource_lock);
2167 vwin->clipcount = 0;
2174 struct video_window *vwin = arg;
2179 "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
2180 ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
2181 vwin->height, vwin->clipcount);
2183 mutex_lock(&zr->resource_lock);
2185 setup_window(file, vwin->x, vwin->y, vwin->width,
2186 vwin->height, vwin->clips,
2187 vwin->clipcount, NULL);
2188 mutex_unlock(&zr->resource_lock);
2196 struct video_buffer *vbuf = arg;
2198 dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
2200 mutex_lock(&zr->resource_lock);
2202 mutex_unlock(&zr->resource_lock);
2209 struct video_buffer *vbuf = arg;
2214 "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
2215 ZR_DEVNAME(zr), vbuf->base, vbuf->width,
2216 vbuf->height, vbuf->depth, vbuf->bytesperline);
2218 for (i = 0; i < NUM_FORMATS; i++)
2219 if (zoran_formats[i].depth == vbuf->depth)
2221 if (i == NUM_FORMATS) {
2224 "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
2225 ZR_DEVNAME(zr), vbuf->depth);
2229 mutex_lock(&zr->resource_lock);
2231 setup_fbuffer(file, vbuf->base, &zoran_formats[i],
2232 vbuf->width, vbuf->height,
2233 vbuf->bytesperline);
2234 mutex_unlock(&zr->resource_lock);
2242 int *frame = arg, res;
2244 dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
2245 ZR_DEVNAME(zr), *frame);
2247 mutex_lock(&zr->resource_lock);
2248 res = v4l_sync(file, *frame);
2249 mutex_unlock(&zr->resource_lock);
2251 zr->v4l_sync_tail++;
2256 case VIDIOCMCAPTURE:
2258 struct video_mmap *vmap = arg;
2263 "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
2264 ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
2267 mutex_lock(&zr->resource_lock);
2268 res = v4l_grab(file, vmap);
2269 mutex_unlock(&zr->resource_lock);
2276 struct video_mbuf *vmbuf = arg;
2279 dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
2282 fh->v4l_buffers.num_buffers *
2283 fh->v4l_buffers.buffer_size;
2284 vmbuf->frames = fh->v4l_buffers.num_buffers;
2285 for (i = 0; i < vmbuf->frames; i++) {
2287 i * fh->v4l_buffers.buffer_size;
2290 mutex_lock(&zr->resource_lock);
2292 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2295 "%s: VIDIOCGMBUF - buffers already allocated\n",
2298 goto v4l1reqbuf_unlock_and_return;
2301 if (v4l_fbuffer_alloc(file)) {
2303 goto v4l1reqbuf_unlock_and_return;
2306 /* The next mmap will map the V4L buffers */
2307 fh->map_mode = ZORAN_MAP_MODE_RAW;
2308 v4l1reqbuf_unlock_and_return:
2309 mutex_unlock(&zr->resource_lock);
2317 struct video_unit *vunit = arg;
2319 dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
2321 vunit->video = zr->video_dev->minor;
2322 vunit->vbi = VIDEO_NO_UNIT;
2323 vunit->radio = VIDEO_NO_UNIT;
2324 vunit->audio = VIDEO_NO_UNIT;
2325 vunit->teletext = VIDEO_NO_UNIT;
2332 * RJ: In principal we could support subcaptures for V4L grabbing.
2333 * Not even the famous BTTV driver has them, however.
2334 * If there should be a strong demand, one could consider
2335 * to implement them.
2337 case VIDIOCGCAPTURE:
2339 dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
2345 case VIDIOCSCAPTURE:
2347 dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
2353 case BUZIOC_G_PARAMS:
2355 struct zoran_params *bparams = arg;
2357 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
2359 memset(bparams, 0, sizeof(struct zoran_params));
2360 bparams->major_version = MAJOR_VERSION;
2361 bparams->minor_version = MINOR_VERSION;
2363 mutex_lock(&zr->resource_lock);
2365 bparams->norm = zr->norm;
2366 bparams->input = zr->input;
2368 bparams->decimation = fh->jpg_settings.decimation;
2369 bparams->HorDcm = fh->jpg_settings.HorDcm;
2370 bparams->VerDcm = fh->jpg_settings.VerDcm;
2371 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
2372 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
2373 bparams->img_x = fh->jpg_settings.img_x;
2374 bparams->img_y = fh->jpg_settings.img_y;
2375 bparams->img_width = fh->jpg_settings.img_width;
2376 bparams->img_height = fh->jpg_settings.img_height;
2377 bparams->odd_even = fh->jpg_settings.odd_even;
2379 bparams->quality = fh->jpg_settings.jpg_comp.quality;
2380 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
2381 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2382 memcpy(bparams->APP_data,
2383 fh->jpg_settings.jpg_comp.APP_data,
2384 sizeof(bparams->APP_data));
2385 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
2386 memcpy(bparams->COM_data,
2387 fh->jpg_settings.jpg_comp.COM_data,
2388 sizeof(bparams->COM_data));
2389 bparams->jpeg_markers =
2390 fh->jpg_settings.jpg_comp.jpeg_markers;
2392 mutex_unlock(&zr->resource_lock);
2394 bparams->VFIFO_FB = 0;
2400 case BUZIOC_S_PARAMS:
2402 struct zoran_params *bparams = arg;
2405 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
2407 settings.decimation = bparams->decimation;
2408 settings.HorDcm = bparams->HorDcm;
2409 settings.VerDcm = bparams->VerDcm;
2410 settings.TmpDcm = bparams->TmpDcm;
2411 settings.field_per_buff = bparams->field_per_buff;
2412 settings.img_x = bparams->img_x;
2413 settings.img_y = bparams->img_y;
2414 settings.img_width = bparams->img_width;
2415 settings.img_height = bparams->img_height;
2416 settings.odd_even = bparams->odd_even;
2418 settings.jpg_comp.quality = bparams->quality;
2419 settings.jpg_comp.APPn = bparams->APPn;
2420 settings.jpg_comp.APP_len = bparams->APP_len;
2421 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
2422 sizeof(bparams->APP_data));
2423 settings.jpg_comp.COM_len = bparams->COM_len;
2424 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
2425 sizeof(bparams->COM_data));
2426 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
2428 mutex_lock(&zr->resource_lock);
2430 if (zr->codec_mode != BUZ_MODE_IDLE) {
2433 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
2436 goto sparams_unlock_and_return;
2439 /* Check the params first before overwriting our
2441 if (zoran_check_jpg_settings(zr, &settings)) {
2443 goto sparams_unlock_and_return;
2446 fh->jpg_settings = settings;
2447 sparams_unlock_and_return:
2448 mutex_unlock(&zr->resource_lock);
2454 case BUZIOC_REQBUFS:
2456 struct zoran_requestbuffers *breq = arg;
2461 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
2462 ZR_DEVNAME(zr), breq->count, breq->size);
2464 /* Enforce reasonable lower and upper limits */
2465 if (breq->count < 4)
2466 breq->count = 4; /* Could be choosen smaller */
2467 if (breq->count > jpg_nbufs)
2468 breq->count = jpg_nbufs;
2469 breq->size = PAGE_ALIGN(breq->size);
2470 if (breq->size < 8192)
2471 breq->size = 8192; /* Arbitrary */
2472 /* breq->size is limited by 1 page for the stat_com
2473 * tables to a Maximum of 2 MB */
2474 if (breq->size > jpg_bufsize)
2475 breq->size = jpg_bufsize;
2476 if (fh->jpg_buffers.need_contiguous &&
2477 breq->size > MAX_KMALLOC_MEM)
2478 breq->size = MAX_KMALLOC_MEM;
2480 mutex_lock(&zr->resource_lock);
2482 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2485 "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
2488 goto jpgreqbuf_unlock_and_return;
2491 fh->jpg_buffers.num_buffers = breq->count;
2492 fh->jpg_buffers.buffer_size = breq->size;
2494 if (jpg_fbuffer_alloc(file)) {
2496 goto jpgreqbuf_unlock_and_return;
2499 /* The next mmap will map the MJPEG buffers - could
2500 * also be *_PLAY, but it doesn't matter here */
2501 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2502 jpgreqbuf_unlock_and_return:
2503 mutex_unlock(&zr->resource_lock);
2509 case BUZIOC_QBUF_CAPT:
2511 int *frame = arg, res;
2513 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
2514 ZR_DEVNAME(zr), *frame);
2516 mutex_lock(&zr->resource_lock);
2517 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
2518 mutex_unlock(&zr->resource_lock);
2524 case BUZIOC_QBUF_PLAY:
2526 int *frame = arg, res;
2528 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
2529 ZR_DEVNAME(zr), *frame);
2531 mutex_lock(&zr->resource_lock);
2532 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
2533 mutex_unlock(&zr->resource_lock);
2541 struct zoran_sync *bsync = arg;
2544 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
2546 mutex_lock(&zr->resource_lock);
2547 res = jpg_sync(file, bsync);
2548 mutex_unlock(&zr->resource_lock);
2554 case BUZIOC_G_STATUS:
2556 struct zoran_status *bstat = arg;
2557 int norm, input, status, res = 0;
2559 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
2561 if (zr->codec_mode != BUZ_MODE_IDLE) {
2564 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
2569 input = zr->card.input[bstat->input].muxsel;
2570 norm = VIDEO_MODE_AUTO;
2572 mutex_lock(&zr->resource_lock);
2574 if (zr->codec_mode != BUZ_MODE_IDLE) {
2577 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
2580 goto gstat_unlock_and_return;
2583 decoder_command(zr, DECODER_SET_INPUT, &input);
2584 decoder_command(zr, DECODER_SET_NORM, &norm);
2586 /* sleep 1 second */
2589 /* Get status of video decoder */
2590 decoder_command(zr, DECODER_GET_STATUS, &status);
2592 /* restore previous input and norm */
2593 input = zr->card.input[zr->input].muxsel;
2594 decoder_command(zr, DECODER_SET_INPUT, &input);
2595 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
2596 gstat_unlock_and_return:
2597 mutex_unlock(&zr->resource_lock);
2601 (status & DECODER_STATUS_GOOD) ? 1 : 0;
2602 if (status & DECODER_STATUS_NTSC)
2603 bstat->norm = VIDEO_MODE_NTSC;
2604 else if (status & DECODER_STATUS_SECAM)
2605 bstat->norm = VIDEO_MODE_SECAM;
2607 bstat->norm = VIDEO_MODE_PAL;
2610 (status & DECODER_STATUS_COLOR) ? 1 : 0;
2617 /* The new video4linux2 capture interface - much nicer than video4linux1, since
2618 * it allows for integrating the JPEG capturing calls inside standard v4l2
2621 case VIDIOC_QUERYCAP:
2623 struct v4l2_capability *cap = arg;
2625 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
2627 memset(cap, 0, sizeof(*cap));
2628 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
2629 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
2630 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2631 pci_name(zr->pci_dev));
2633 KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2635 cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2641 case VIDIOC_ENUM_FMT:
2643 struct v4l2_fmtdesc *fmt = arg;
2644 int index = fmt->index, num = -1, i, flag = 0, type =
2647 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
2648 ZR_DEVNAME(zr), fmt->index);
2650 switch (fmt->type) {
2651 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2652 flag = ZORAN_FORMAT_CAPTURE;
2654 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2655 flag = ZORAN_FORMAT_PLAYBACK;
2657 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2658 flag = ZORAN_FORMAT_OVERLAY;
2663 "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
2664 ZR_DEVNAME(zr), fmt->type);
2668 for (i = 0; i < NUM_FORMATS; i++) {
2669 if (zoran_formats[i].flags & flag)
2671 if (num == fmt->index)
2674 if (fmt->index < 0 /* late, but not too late */ ||
2678 memset(fmt, 0, sizeof(*fmt));
2681 strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
2682 fmt->pixelformat = zoran_formats[i].fourcc;
2683 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2684 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2692 struct v4l2_format *fmt = arg;
2693 int type = fmt->type;
2695 dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
2697 memset(fmt, 0, sizeof(*fmt));
2700 switch (fmt->type) {
2701 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2703 mutex_lock(&zr->resource_lock);
2705 fmt->fmt.win.w.left = fh->overlay_settings.x;
2706 fmt->fmt.win.w.top = fh->overlay_settings.y;
2707 fmt->fmt.win.w.width = fh->overlay_settings.width;
2708 fmt->fmt.win.w.height =
2709 fh->overlay_settings.height;
2710 if (fh->overlay_settings.width * 2 >
2712 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2714 fmt->fmt.win.field = V4L2_FIELD_TOP;
2716 mutex_unlock(&zr->resource_lock);
2720 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2721 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2723 mutex_lock(&zr->resource_lock);
2725 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2726 fh->map_mode == ZORAN_MAP_MODE_RAW) {
2728 fmt->fmt.pix.width =
2729 fh->v4l_settings.width;
2730 fmt->fmt.pix.height =
2731 fh->v4l_settings.height;
2732 fmt->fmt.pix.sizeimage =
2733 fh->v4l_settings.bytesperline *
2734 fh->v4l_settings.height;
2735 fmt->fmt.pix.pixelformat =
2736 fh->v4l_settings.format->fourcc;
2737 fmt->fmt.pix.colorspace =
2738 fh->v4l_settings.format->colorspace;
2739 fmt->fmt.pix.bytesperline = 0;
2740 if (BUZ_MAX_HEIGHT <
2741 (fh->v4l_settings.height * 2))
2742 fmt->fmt.pix.field =
2743 V4L2_FIELD_INTERLACED;
2745 fmt->fmt.pix.field =
2750 fmt->fmt.pix.width =
2751 fh->jpg_settings.img_width /
2752 fh->jpg_settings.HorDcm;
2753 fmt->fmt.pix.height =
2754 fh->jpg_settings.img_height /
2755 (fh->jpg_settings.VerDcm *
2756 fh->jpg_settings.TmpDcm);
2757 fmt->fmt.pix.sizeimage =
2758 zoran_v4l2_calc_bufsize(&fh->
2760 fmt->fmt.pix.pixelformat =
2762 if (fh->jpg_settings.TmpDcm == 1)
2763 fmt->fmt.pix.field =
2765 odd_even ? V4L2_FIELD_SEQ_BT :
2768 fmt->fmt.pix.field =
2770 odd_even ? V4L2_FIELD_TOP :
2773 fmt->fmt.pix.bytesperline = 0;
2774 fmt->fmt.pix.colorspace =
2775 V4L2_COLORSPACE_SMPTE170M;
2778 mutex_unlock(&zr->resource_lock);
2785 "%s: VIDIOC_G_FMT - unsupported type %d\n",
2786 ZR_DEVNAME(zr), fmt->type);
2795 struct v4l2_format *fmt = arg;
2799 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
2800 ZR_DEVNAME(zr), fmt->type);
2802 switch (fmt->type) {
2803 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2805 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2806 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2807 fmt->fmt.win.w.width,
2808 fmt->fmt.win.w.height,
2809 fmt->fmt.win.clipcount,
2810 fmt->fmt.win.bitmap);
2811 mutex_lock(&zr->resource_lock);
2813 setup_window(file, fmt->fmt.win.w.left,
2815 fmt->fmt.win.w.width,
2816 fmt->fmt.win.w.height,
2817 (struct video_clip __user *)
2819 fmt->fmt.win.clipcount,
2820 fmt->fmt.win.bitmap);
2821 mutex_unlock(&zr->resource_lock);
2825 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2826 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2829 __cpu_to_le32(fmt->fmt.pix.pixelformat);
2830 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2831 fmt->fmt.pix.width, fmt->fmt.pix.height,
2832 fmt->fmt.pix.pixelformat,
2833 (char *) &printformat);
2835 if (fmt->fmt.pix.bytesperline > 0) {
2837 KERN_ERR "%s: bpl not supported\n",
2842 /* we can be requested to do JPEG/raw playback/capture */
2844 (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2845 (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2846 fmt->fmt.pix.pixelformat ==
2847 V4L2_PIX_FMT_MJPEG))) {
2850 "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
2851 ZR_DEVNAME(zr), fmt->type,
2852 fmt->fmt.pix.pixelformat,
2853 (char *) &printformat);
2857 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
2858 mutex_lock(&zr->resource_lock);
2860 settings = fh->jpg_settings;
2862 if (fh->v4l_buffers.allocated ||
2863 fh->jpg_buffers.allocated) {
2866 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2869 goto sfmtjpg_unlock_and_return;
2872 /* we actually need to set 'real' parameters now */
2873 if ((fmt->fmt.pix.height * 2) >
2875 settings.TmpDcm = 1;
2877 settings.TmpDcm = 2;
2878 settings.decimation = 0;
2879 if (fmt->fmt.pix.height <=
2880 fh->jpg_settings.img_height / 2)
2881 settings.VerDcm = 2;
2883 settings.VerDcm = 1;
2884 if (fmt->fmt.pix.width <=
2885 fh->jpg_settings.img_width / 4)
2886 settings.HorDcm = 4;
2887 else if (fmt->fmt.pix.width <=
2888 fh->jpg_settings.img_width / 2)
2889 settings.HorDcm = 2;
2891 settings.HorDcm = 1;
2892 if (settings.TmpDcm == 1)
2893 settings.field_per_buff = 2;
2895 settings.field_per_buff = 1;
2899 zoran_check_jpg_settings(zr,
2901 goto sfmtjpg_unlock_and_return;
2903 /* it's ok, so set them */
2904 fh->jpg_settings = settings;
2906 /* tell the user what we actually did */
2907 fmt->fmt.pix.width =
2908 settings.img_width / settings.HorDcm;
2909 fmt->fmt.pix.height =
2910 settings.img_height * 2 /
2911 (settings.TmpDcm * settings.VerDcm);
2912 if (settings.TmpDcm == 1)
2913 fmt->fmt.pix.field =
2915 odd_even ? V4L2_FIELD_SEQ_TB :
2918 fmt->fmt.pix.field =
2920 odd_even ? V4L2_FIELD_TOP :
2922 fh->jpg_buffers.buffer_size =
2923 zoran_v4l2_calc_bufsize(&fh->
2925 fmt->fmt.pix.sizeimage =
2926 fh->jpg_buffers.buffer_size;
2928 /* we hereby abuse this variable to show that
2929 * we're gonna do mjpeg capture */
2932 V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2933 ZORAN_MAP_MODE_JPG_REC :
2934 ZORAN_MAP_MODE_JPG_PLAY;
2935 sfmtjpg_unlock_and_return:
2936 mutex_unlock(&zr->resource_lock);
2938 for (i = 0; i < NUM_FORMATS; i++)
2939 if (fmt->fmt.pix.pixelformat ==
2940 zoran_formats[i].fourcc)
2942 if (i == NUM_FORMATS) {
2945 "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
2947 fmt->fmt.pix.pixelformat,
2948 (char *) &printformat);
2951 mutex_lock(&zr->resource_lock);
2952 if (fh->jpg_buffers.allocated ||
2953 (fh->v4l_buffers.allocated &&
2954 fh->v4l_buffers.active !=
2958 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2961 goto sfmtv4l_unlock_and_return;
2963 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2964 fmt->fmt.pix.height =
2966 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2967 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2970 zoran_v4l_set_format(file,
2977 goto sfmtv4l_unlock_and_return;
2979 /* tell the user the
2980 * results/missing stuff */
2981 fmt->fmt.pix.sizeimage =
2982 fh->v4l_settings.height *
2983 fh->v4l_settings.bytesperline;
2984 if (BUZ_MAX_HEIGHT <
2985 (fh->v4l_settings.height * 2))
2986 fmt->fmt.pix.field =
2987 V4L2_FIELD_INTERLACED;
2989 fmt->fmt.pix.field =
2992 fh->map_mode = ZORAN_MAP_MODE_RAW;
2993 sfmtv4l_unlock_and_return:
2994 mutex_unlock(&zr->resource_lock);
3000 dprintk(3, "unsupported\n");
3003 "%s: VIDIOC_S_FMT - unsupported type %d\n",
3004 ZR_DEVNAME(zr), fmt->type);
3014 struct v4l2_framebuffer *fb = arg;
3016 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
3018 memset(fb, 0, sizeof(*fb));
3019 mutex_lock(&zr->resource_lock);
3020 fb->base = zr->buffer.base;
3021 fb->fmt.width = zr->buffer.width;
3022 fb->fmt.height = zr->buffer.height;
3023 if (zr->overlay_settings.format) {
3024 fb->fmt.pixelformat =
3025 fh->overlay_settings.format->fourcc;
3027 fb->fmt.bytesperline = zr->buffer.bytesperline;
3028 mutex_unlock(&zr->resource_lock);
3029 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
3030 fb->fmt.field = V4L2_FIELD_INTERLACED;
3031 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
3032 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3041 struct v4l2_framebuffer *fb = arg;
3042 __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
3046 "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
3047 ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
3048 fb->fmt.bytesperline, fb->fmt.pixelformat,
3049 (char *) &printformat);
3051 for (i = 0; i < NUM_FORMATS; i++)
3052 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
3054 if (i == NUM_FORMATS) {
3057 "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
3058 ZR_DEVNAME(zr), fb->fmt.pixelformat,
3059 (char *) &printformat);
3063 mutex_lock(&zr->resource_lock);
3065 setup_fbuffer(file, fb->base, &zoran_formats[i],
3066 fb->fmt.width, fb->fmt.height,
3067 fb->fmt.bytesperline);
3068 mutex_unlock(&zr->resource_lock);
3074 case VIDIOC_OVERLAY:
3078 dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
3079 ZR_DEVNAME(zr), *on);
3081 mutex_lock(&zr->resource_lock);
3082 res = setup_overlay(file, *on);
3083 mutex_unlock(&zr->resource_lock);
3089 case VIDIOC_REQBUFS:
3091 struct v4l2_requestbuffers *req = arg;
3094 dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
3095 ZR_DEVNAME(zr), req->type);
3097 if (req->memory != V4L2_MEMORY_MMAP) {
3100 "%s: only MEMORY_MMAP capture is supported, not %d\n",
3101 ZR_DEVNAME(zr), req->memory);
3105 mutex_lock(&zr->resource_lock);
3107 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
3110 "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
3113 goto v4l2reqbuf_unlock_and_return;
3116 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
3117 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3119 /* control user input */
3122 if (req->count > v4l_nbufs)
3123 req->count = v4l_nbufs;
3124 fh->v4l_buffers.num_buffers = req->count;
3126 if (v4l_fbuffer_alloc(file)) {
3128 goto v4l2reqbuf_unlock_and_return;
3131 /* The next mmap will map the V4L buffers */
3132 fh->map_mode = ZORAN_MAP_MODE_RAW;
3134 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
3135 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3137 /* we need to calculate size ourselves now */
3140 if (req->count > jpg_nbufs)
3141 req->count = jpg_nbufs;
3142 fh->jpg_buffers.num_buffers = req->count;
3143 fh->jpg_buffers.buffer_size =
3144 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3146 if (jpg_fbuffer_alloc(file)) {
3148 goto v4l2reqbuf_unlock_and_return;
3151 /* The next mmap will map the MJPEG buffers */
3152 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
3153 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
3155 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
3160 "%s: VIDIOC_REQBUFS - unknown type %d\n",
3161 ZR_DEVNAME(zr), req->type);
3163 goto v4l2reqbuf_unlock_and_return;
3165 v4l2reqbuf_unlock_and_return:
3166 mutex_unlock(&zr->resource_lock);
3172 case VIDIOC_QUERYBUF:
3174 struct v4l2_buffer *buf = arg;
3175 __u32 type = buf->type;
3176 int index = buf->index, res;
3180 "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
3181 ZR_DEVNAME(zr), buf->index, buf->type);
3183 memset(buf, 0, sizeof(*buf));
3187 mutex_lock(&zr->resource_lock);
3188 res = zoran_v4l2_buffer_status(file, buf, buf->index);
3189 mutex_unlock(&zr->resource_lock);
3197 struct v4l2_buffer *buf = arg;
3198 int res = 0, codec_mode, buf_type;
3201 KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
3202 ZR_DEVNAME(zr), buf->type, buf->index);
3204 mutex_lock(&zr->resource_lock);
3206 switch (fh->map_mode) {
3207 case ZORAN_MAP_MODE_RAW:
3208 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3211 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3212 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3214 goto qbuf_unlock_and_return;
3217 res = zoran_v4l_queue_frame(file, buf->index);
3219 goto qbuf_unlock_and_return;
3220 if (!zr->v4l_memgrab_active &&
3221 fh->v4l_buffers.active == ZORAN_LOCKED)
3222 zr36057_set_memgrab(zr, 1);
3225 case ZORAN_MAP_MODE_JPG_REC:
3226 case ZORAN_MAP_MODE_JPG_PLAY:
3227 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3228 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3229 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
3231 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3232 codec_mode = BUZ_MODE_MOTION_COMPRESS;
3235 if (buf->type != buf_type) {
3238 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3239 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3241 goto qbuf_unlock_and_return;
3245 zoran_jpg_queue_frame(file, buf->index,
3248 goto qbuf_unlock_and_return;
3249 if (zr->codec_mode == BUZ_MODE_IDLE &&
3250 fh->jpg_buffers.active == ZORAN_LOCKED) {
3251 zr36057_enable_jpg(zr, codec_mode);
3258 "%s: VIDIOC_QBUF - unsupported type %d\n",
3259 ZR_DEVNAME(zr), buf->type);
3261 goto qbuf_unlock_and_return;
3263 qbuf_unlock_and_return:
3264 mutex_unlock(&zr->resource_lock);
3272 struct v4l2_buffer *buf = arg;
3273 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
3275 dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
3276 ZR_DEVNAME(zr), buf->type);
3278 mutex_lock(&zr->resource_lock);
3280 switch (fh->map_mode) {
3281 case ZORAN_MAP_MODE_RAW:
3282 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3285 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3286 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3288 goto dqbuf_unlock_and_return;
3291 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3292 if (file->f_flags & O_NONBLOCK &&
3293 zr->v4l_buffers.buffer[num].state !=
3296 goto dqbuf_unlock_and_return;
3298 res = v4l_sync(file, num);
3300 goto dqbuf_unlock_and_return;
3302 zr->v4l_sync_tail++;
3303 res = zoran_v4l2_buffer_status(file, buf, num);
3306 case ZORAN_MAP_MODE_JPG_REC:
3307 case ZORAN_MAP_MODE_JPG_PLAY:
3309 struct zoran_sync bs;
3311 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
3312 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3314 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3316 if (buf->type != buf_type) {
3319 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3320 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3322 goto dqbuf_unlock_and_return;
3327 jpg_que_tail & BUZ_MASK_FRAME];
3329 if (file->f_flags & O_NONBLOCK &&
3330 zr->jpg_buffers.buffer[num].state !=
3333 goto dqbuf_unlock_and_return;
3335 res = jpg_sync(file, &bs);
3337 goto dqbuf_unlock_and_return;
3339 zoran_v4l2_buffer_status(file, buf, bs.frame);
3346 "%s: VIDIOC_DQBUF - unsupported type %d\n",
3347 ZR_DEVNAME(zr), buf->type);
3349 goto dqbuf_unlock_and_return;
3351 dqbuf_unlock_and_return:
3352 mutex_unlock(&zr->resource_lock);
3358 case VIDIOC_STREAMON:
3362 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
3364 mutex_lock(&zr->resource_lock);
3366 switch (fh->map_mode) {
3367 case ZORAN_MAP_MODE_RAW: /* raw capture */
3368 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
3369 fh->v4l_buffers.active != ZORAN_ACTIVE) {
3371 goto strmon_unlock_and_return;
3374 zr->v4l_buffers.active = fh->v4l_buffers.active =
3376 zr->v4l_settings = fh->v4l_settings;
3378 zr->v4l_sync_tail = zr->v4l_pend_tail;
3379 if (!zr->v4l_memgrab_active &&
3380 zr->v4l_pend_head != zr->v4l_pend_tail) {
3381 zr36057_set_memgrab(zr, 1);
3385 case ZORAN_MAP_MODE_JPG_REC:
3386 case ZORAN_MAP_MODE_JPG_PLAY:
3387 /* what is the codec mode right now? */
3388 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
3389 fh->jpg_buffers.active != ZORAN_ACTIVE) {
3391 goto strmon_unlock_and_return;
3394 zr->jpg_buffers.active = fh->jpg_buffers.active =
3397 if (zr->jpg_que_head != zr->jpg_que_tail) {
3398 /* Start the jpeg codec when the first frame is queued */
3406 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
3407 ZR_DEVNAME(zr), fh->map_mode);
3409 goto strmon_unlock_and_return;
3411 strmon_unlock_and_return:
3412 mutex_unlock(&zr->resource_lock);
3418 case VIDIOC_STREAMOFF:
3422 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
3424 mutex_lock(&zr->resource_lock);
3426 switch (fh->map_mode) {
3427 case ZORAN_MAP_MODE_RAW: /* raw capture */
3428 if (fh->v4l_buffers.active == ZORAN_FREE &&
3429 zr->v4l_buffers.active != ZORAN_FREE) {
3430 res = -EPERM; /* stay off other's settings! */
3431 goto strmoff_unlock_and_return;
3433 if (zr->v4l_buffers.active == ZORAN_FREE)
3434 goto strmoff_unlock_and_return;
3436 /* unload capture */
3437 if (zr->v4l_memgrab_active) {
3438 unsigned long flags;
3440 spin_lock_irqsave(&zr->spinlock, flags);
3441 zr36057_set_memgrab(zr, 0);
3442 spin_unlock_irqrestore(&zr->spinlock, flags);
3445 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3446 zr->v4l_buffers.buffer[i].state =
3448 fh->v4l_buffers = zr->v4l_buffers;
3450 zr->v4l_buffers.active = fh->v4l_buffers.active =
3453 zr->v4l_grab_seq = 0;
3454 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
3455 zr->v4l_sync_tail = 0;
3459 case ZORAN_MAP_MODE_JPG_REC:
3460 case ZORAN_MAP_MODE_JPG_PLAY:
3461 if (fh->jpg_buffers.active == ZORAN_FREE &&
3462 zr->jpg_buffers.active != ZORAN_FREE) {
3463 res = -EPERM; /* stay off other's settings! */
3464 goto strmoff_unlock_and_return;
3466 if (zr->jpg_buffers.active == ZORAN_FREE)
3467 goto strmoff_unlock_and_return;
3472 ZORAN_MAP_MODE_JPG_REC) ?
3473 BUZ_MODE_MOTION_COMPRESS :
3474 BUZ_MODE_MOTION_DECOMPRESS);
3476 goto strmoff_unlock_and_return;
3481 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
3482 ZR_DEVNAME(zr), fh->map_mode);
3484 goto strmoff_unlock_and_return;
3486 strmoff_unlock_and_return:
3487 mutex_unlock(&zr->resource_lock);
3493 case VIDIOC_QUERYCTRL:
3495 struct v4l2_queryctrl *ctrl = arg;
3497 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
3498 ZR_DEVNAME(zr), ctrl->id);
3500 /* we only support hue/saturation/contrast/brightness */
3501 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3502 ctrl->id > V4L2_CID_HUE)
3506 memset(ctrl, 0, sizeof(*ctrl));
3511 case V4L2_CID_BRIGHTNESS:
3512 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
3514 case V4L2_CID_CONTRAST:
3515 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
3517 case V4L2_CID_SATURATION:
3518 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
3521 strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
3526 ctrl->maximum = 65535;
3528 ctrl->default_value = 32768;
3529 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
3537 struct v4l2_control *ctrl = arg;
3539 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
3540 ZR_DEVNAME(zr), ctrl->id);
3542 /* we only support hue/saturation/contrast/brightness */
3543 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3544 ctrl->id > V4L2_CID_HUE)
3547 mutex_lock(&zr->resource_lock);
3549 case V4L2_CID_BRIGHTNESS:
3550 ctrl->value = zr->brightness;
3552 case V4L2_CID_CONTRAST:
3553 ctrl->value = zr->contrast;
3555 case V4L2_CID_SATURATION:
3556 ctrl->value = zr->saturation;
3559 ctrl->value = zr->hue;
3562 mutex_unlock(&zr->resource_lock);
3570 struct v4l2_control *ctrl = arg;
3571 struct video_picture pict;
3573 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
3574 ZR_DEVNAME(zr), ctrl->id);
3576 /* we only support hue/saturation/contrast/brightness */
3577 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3578 ctrl->id > V4L2_CID_HUE)
3581 if (ctrl->value < 0 || ctrl->value > 65535) {
3584 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
3585 ZR_DEVNAME(zr), ctrl->value, ctrl->id);
3589 mutex_lock(&zr->resource_lock);
3591 case V4L2_CID_BRIGHTNESS:
3592 zr->brightness = ctrl->value;
3594 case V4L2_CID_CONTRAST:
3595 zr->contrast = ctrl->value;
3597 case V4L2_CID_SATURATION:
3598 zr->saturation = ctrl->value;
3601 zr->hue = ctrl->value;
3604 pict.brightness = zr->brightness;
3605 pict.contrast = zr->contrast;
3606 pict.colour = zr->saturation;
3609 decoder_command(zr, DECODER_SET_PICTURE, &pict);
3611 mutex_unlock(&zr->resource_lock);
3617 case VIDIOC_ENUMSTD:
3619 struct v4l2_standard *std = arg;
3621 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
3622 ZR_DEVNAME(zr), std->index);
3624 if (std->index < 0 || std->index >= (zr->card.norms + 1))
3627 int id = std->index;
3628 memset(std, 0, sizeof(*std));
3632 if (std->index == zr->card.norms) {
3633 /* if we have autodetect, ... */
3634 struct video_decoder_capability caps;
3635 decoder_command(zr, DECODER_GET_CAPABILITIES,
3637 if (caps.flags & VIDEO_DECODER_AUTO) {
3638 std->id = V4L2_STD_ALL;
3639 strncpy(std->name, "Autodetect", sizeof(std->name)-1);
3644 switch (std->index) {
3646 std->id = V4L2_STD_PAL;
3647 strncpy(std->name, "PAL", sizeof(std->name)-1);
3648 std->frameperiod.numerator = 1;
3649 std->frameperiod.denominator = 25;
3650 std->framelines = zr->card.tvn[0]->Ht;
3653 std->id = V4L2_STD_NTSC;
3654 strncpy(std->name, "NTSC", sizeof(std->name)-1);
3655 std->frameperiod.numerator = 1001;
3656 std->frameperiod.denominator = 30000;
3657 std->framelines = zr->card.tvn[1]->Ht;
3660 std->id = V4L2_STD_SECAM;
3661 strncpy(std->name, "SECAM", sizeof(std->name)-1);
3662 std->frameperiod.numerator = 1;
3663 std->frameperiod.denominator = 25;
3664 std->framelines = zr->card.tvn[2]->Ht;
3674 v4l2_std_id *std = arg;
3677 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
3679 mutex_lock(&zr->resource_lock);
3681 mutex_unlock(&zr->resource_lock);
3684 case VIDEO_MODE_PAL:
3685 *std = V4L2_STD_PAL;
3687 case VIDEO_MODE_NTSC:
3688 *std = V4L2_STD_NTSC;
3690 case VIDEO_MODE_SECAM:
3691 *std = V4L2_STD_SECAM;
3701 int norm = -1, res = 0;
3702 v4l2_std_id *std = arg;
3704 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
3705 ZR_DEVNAME(zr), (unsigned long long)*std);
3707 if ((*std & V4L2_STD_PAL) && !(*std & ~V4L2_STD_PAL))
3708 norm = VIDEO_MODE_PAL;
3709 else if ((*std & V4L2_STD_NTSC) && !(*std & ~V4L2_STD_NTSC))
3710 norm = VIDEO_MODE_NTSC;
3711 else if ((*std & V4L2_STD_SECAM) && !(*std & ~V4L2_STD_SECAM))
3712 norm = VIDEO_MODE_SECAM;
3713 else if (*std == V4L2_STD_ALL)
3714 norm = VIDEO_MODE_AUTO;
3718 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3719 ZR_DEVNAME(zr), (unsigned long long)*std);
3723 mutex_lock(&zr->resource_lock);
3724 if ((res = zoran_set_norm(zr, norm)))
3725 goto sstd_unlock_and_return;
3727 res = wait_grab_pending(zr);
3728 sstd_unlock_and_return:
3729 mutex_unlock(&zr->resource_lock);
3734 case VIDIOC_ENUMINPUT:
3736 struct v4l2_input *inp = arg;
3739 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
3740 ZR_DEVNAME(zr), inp->index);
3742 if (inp->index < 0 || inp->index >= zr->card.inputs)
3745 int id = inp->index;
3746 memset(inp, 0, sizeof(*inp));
3750 strncpy(inp->name, zr->card.input[inp->index].name,
3751 sizeof(inp->name) - 1);
3752 inp->type = V4L2_INPUT_TYPE_CAMERA;
3753 inp->std = V4L2_STD_ALL;
3755 /* Get status of video decoder */
3756 mutex_lock(&zr->resource_lock);
3757 decoder_command(zr, DECODER_GET_STATUS, &status);
3758 mutex_unlock(&zr->resource_lock);
3760 if (!(status & DECODER_STATUS_GOOD)) {
3761 inp->status |= V4L2_IN_ST_NO_POWER;
3762 inp->status |= V4L2_IN_ST_NO_SIGNAL;
3764 if (!(status & DECODER_STATUS_COLOR))
3765 inp->status |= V4L2_IN_ST_NO_COLOR;
3771 case VIDIOC_G_INPUT:
3775 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
3777 mutex_lock(&zr->resource_lock);
3779 mutex_unlock(&zr->resource_lock);
3785 case VIDIOC_S_INPUT:
3787 int *input = arg, res = 0;
3789 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
3790 ZR_DEVNAME(zr), *input);
3792 mutex_lock(&zr->resource_lock);
3793 if ((res = zoran_set_input(zr, *input)))
3794 goto sinput_unlock_and_return;
3796 /* Make sure the changes come into effect */
3797 res = wait_grab_pending(zr);
3798 sinput_unlock_and_return:
3799 mutex_unlock(&zr->resource_lock);
3804 case VIDIOC_ENUMOUTPUT:
3806 struct v4l2_output *outp = arg;
3808 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
3809 ZR_DEVNAME(zr), outp->index);
3811 if (outp->index != 0)
3814 memset(outp, 0, sizeof(*outp));
3816 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3817 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
3823 case VIDIOC_G_OUTPUT:
3827 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
3835 case VIDIOC_S_OUTPUT:
3839 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
3840 ZR_DEVNAME(zr), *output);
3849 /* cropping (sub-frame capture) */
3850 case VIDIOC_CROPCAP:
3852 struct v4l2_cropcap *cropcap = arg;
3853 int type = cropcap->type, res = 0;
3855 dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
3856 ZR_DEVNAME(zr), cropcap->type);
3858 memset(cropcap, 0, sizeof(*cropcap));
3859 cropcap->type = type;
3861 mutex_lock(&zr->resource_lock);
3863 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3864 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3865 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3868 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3871 goto cropcap_unlock_and_return;
3874 cropcap->bounds.top = cropcap->bounds.left = 0;
3875 cropcap->bounds.width = BUZ_MAX_WIDTH;
3876 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3877 cropcap->defrect.top = cropcap->defrect.left = 0;
3878 cropcap->defrect.width = BUZ_MIN_WIDTH;
3879 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3880 cropcap_unlock_and_return:
3881 mutex_unlock(&zr->resource_lock);
3888 struct v4l2_crop *crop = arg;
3889 int type = crop->type, res = 0;
3891 dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
3892 ZR_DEVNAME(zr), crop->type);
3894 memset(crop, 0, sizeof(*crop));
3897 mutex_lock(&zr->resource_lock);
3899 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3900 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3901 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3904 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3907 goto gcrop_unlock_and_return;
3910 crop->c.top = fh->jpg_settings.img_y;
3911 crop->c.left = fh->jpg_settings.img_x;
3912 crop->c.width = fh->jpg_settings.img_width;
3913 crop->c.height = fh->jpg_settings.img_height;
3915 gcrop_unlock_and_return:
3916 mutex_unlock(&zr->resource_lock);
3924 struct v4l2_crop *crop = arg;
3927 settings = fh->jpg_settings;
3931 "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
3932 ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
3933 crop->c.width, crop->c.height);
3935 mutex_lock(&zr->resource_lock);
3937 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3940 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3943 goto scrop_unlock_and_return;
3946 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3947 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3948 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3951 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3954 goto scrop_unlock_and_return;
3957 /* move into a form that we understand */
3958 settings.img_x = crop->c.left;
3959 settings.img_y = crop->c.top;
3960 settings.img_width = crop->c.width;
3961 settings.img_height = crop->c.height;
3963 /* check validity */
3964 if ((res = zoran_check_jpg_settings(zr, &settings)))
3965 goto scrop_unlock_and_return;
3968 fh->jpg_settings = settings;
3970 scrop_unlock_and_return:
3971 mutex_unlock(&zr->resource_lock);
3976 case VIDIOC_G_JPEGCOMP:
3978 struct v4l2_jpegcompression *params = arg;
3980 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
3983 memset(params, 0, sizeof(*params));
3985 mutex_lock(&zr->resource_lock);
3987 params->quality = fh->jpg_settings.jpg_comp.quality;
3988 params->APPn = fh->jpg_settings.jpg_comp.APPn;
3989 memcpy(params->APP_data,
3990 fh->jpg_settings.jpg_comp.APP_data,
3991 fh->jpg_settings.jpg_comp.APP_len);
3992 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
3993 memcpy(params->COM_data,
3994 fh->jpg_settings.jpg_comp.COM_data,
3995 fh->jpg_settings.jpg_comp.COM_len);
3996 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
3997 params->jpeg_markers =
3998 fh->jpg_settings.jpg_comp.jpeg_markers;
4000 mutex_unlock(&zr->resource_lock);
4006 case VIDIOC_S_JPEGCOMP:
4008 struct v4l2_jpegcompression *params = arg;
4011 settings = fh->jpg_settings;
4015 "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
4016 ZR_DEVNAME(zr), params->quality, params->APPn,
4017 params->APP_len, params->COM_len);
4019 settings.jpg_comp = *params;
4021 mutex_lock(&zr->resource_lock);
4023 if (fh->v4l_buffers.active != ZORAN_FREE ||
4024 fh->jpg_buffers.active != ZORAN_FREE) {
4027 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
4030 goto sjpegc_unlock_and_return;
4033 if ((res = zoran_check_jpg_settings(zr, &settings)))
4034 goto sjpegc_unlock_and_return;
4035 if (!fh->jpg_buffers.allocated)
4036 fh->jpg_buffers.buffer_size =
4037 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
4038 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
4039 sjpegc_unlock_and_return:
4040 mutex_unlock(&zr->resource_lock);
4046 case VIDIOC_QUERYSTD: /* why is this useful? */
4048 v4l2_std_id *std = arg;
4051 KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
4052 ZR_DEVNAME(zr), (unsigned long long)*std);
4054 if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
4055 *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
4056 zr->card.norms == 3)) {
4064 case VIDIOC_TRY_FMT:
4066 struct v4l2_format *fmt = arg;
4069 dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
4070 ZR_DEVNAME(zr), fmt->type);
4072 switch (fmt->type) {
4073 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4074 mutex_lock(&zr->resource_lock);
4076 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
4077 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
4078 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
4079 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
4080 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
4081 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
4082 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
4083 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
4085 mutex_unlock(&zr->resource_lock);
4088 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4089 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4090 if (fmt->fmt.pix.bytesperline > 0)
4093 mutex_lock(&zr->resource_lock);
4095 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
4096 settings = fh->jpg_settings;
4098 /* we actually need to set 'real' parameters now */
4099 if ((fmt->fmt.pix.height * 2) >
4101 settings.TmpDcm = 1;
4103 settings.TmpDcm = 2;
4104 settings.decimation = 0;
4105 if (fmt->fmt.pix.height <=
4106 fh->jpg_settings.img_height / 2)
4107 settings.VerDcm = 2;
4109 settings.VerDcm = 1;
4110 if (fmt->fmt.pix.width <=
4111 fh->jpg_settings.img_width / 4)
4112 settings.HorDcm = 4;
4113 else if (fmt->fmt.pix.width <=
4114 fh->jpg_settings.img_width / 2)
4115 settings.HorDcm = 2;
4117 settings.HorDcm = 1;
4118 if (settings.TmpDcm == 1)
4119 settings.field_per_buff = 2;
4121 settings.field_per_buff = 1;
4125 zoran_check_jpg_settings(zr,
4127 goto tryfmt_unlock_and_return;
4129 /* tell the user what we actually did */
4130 fmt->fmt.pix.width =
4131 settings.img_width / settings.HorDcm;
4132 fmt->fmt.pix.height =
4133 settings.img_height * 2 /
4134 (settings.TmpDcm * settings.VerDcm);
4135 if (settings.TmpDcm == 1)
4136 fmt->fmt.pix.field =
4138 odd_even ? V4L2_FIELD_SEQ_TB :
4141 fmt->fmt.pix.field =
4143 odd_even ? V4L2_FIELD_TOP :
4146 fmt->fmt.pix.sizeimage =
4147 zoran_v4l2_calc_bufsize(&settings);
4148 } else if (fmt->type ==
4149 V4L2_BUF_TYPE_VIDEO_CAPTURE) {
4152 for (i = 0; i < NUM_FORMATS; i++)
4153 if (zoran_formats[i].fourcc ==
4154 fmt->fmt.pix.pixelformat)
4156 if (i == NUM_FORMATS) {
4158 goto tryfmt_unlock_and_return;
4161 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
4162 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
4163 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
4164 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
4165 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
4166 fmt->fmt.pix.height =
4168 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
4169 fmt->fmt.pix.height =
4173 goto tryfmt_unlock_and_return;
4175 tryfmt_unlock_and_return:
4176 mutex_unlock(&zr->resource_lock);
4190 dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
4191 ZR_DEVNAME(zr), cmd);
4192 return -ENOIOCTLCMD;
4201 zoran_ioctl (struct inode *inode,
4206 return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
4210 zoran_poll (struct file *file,
4213 struct zoran_fh *fh = file->private_data;
4214 struct zoran *zr = fh->zr;
4216 unsigned long flags;
4218 /* we should check whether buffers are ready to be synced on
4219 * (w/o waits - O_NONBLOCK) here
4220 * if ready for read (sync), return POLLIN|POLLRDNORM,
4221 * if ready for write (sync), return POLLOUT|POLLWRNORM,
4222 * if error, return POLLERR,
4223 * if no buffers queued or so, return POLLNVAL
4226 mutex_lock(&zr->resource_lock);
4228 switch (fh->map_mode) {
4229 case ZORAN_MAP_MODE_RAW:
4230 poll_wait(file, &zr->v4l_capq, wait);
4231 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
4233 spin_lock_irqsave(&zr->spinlock, flags);
4236 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
4237 ZR_DEVNAME(zr), __func__,
4238 "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
4239 "UPMD"[zr->v4l_buffers.buffer[frame].state],
4240 zr->v4l_pend_tail, zr->v4l_pend_head);
4241 /* Process is the one capturing? */
4242 if (fh->v4l_buffers.active != ZORAN_FREE &&
4243 /* Buffer ready to DQBUF? */
4244 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
4245 res = POLLIN | POLLRDNORM;
4246 spin_unlock_irqrestore(&zr->spinlock, flags);
4250 case ZORAN_MAP_MODE_JPG_REC:
4251 case ZORAN_MAP_MODE_JPG_PLAY:
4252 poll_wait(file, &zr->jpg_capq, wait);
4253 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
4255 spin_lock_irqsave(&zr->spinlock, flags);
4258 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
4259 ZR_DEVNAME(zr), __func__,
4260 "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
4261 "UPMD"[zr->jpg_buffers.buffer[frame].state],
4262 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
4263 if (fh->jpg_buffers.active != ZORAN_FREE &&
4264 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
4265 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
4266 res = POLLIN | POLLRDNORM;
4268 res = POLLOUT | POLLWRNORM;
4270 spin_unlock_irqrestore(&zr->spinlock, flags);
4277 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
4278 ZR_DEVNAME(zr), fh->map_mode);
4282 mutex_unlock(&zr->resource_lock);
4289 * This maps the buffers to user space.
4291 * Depending on the state of fh->map_mode
4292 * the V4L or the MJPEG buffers are mapped
4293 * per buffer or all together
4295 * Note that we need to connect to some
4296 * unmap signal event to unmap the de-allocate
4297 * the buffer accordingly (zoran_vm_close())
4301 zoran_vm_open (struct vm_area_struct *vma)
4303 struct zoran_mapping *map = vma->vm_private_data;
4309 zoran_vm_close (struct vm_area_struct *vma)
4311 struct zoran_mapping *map = vma->vm_private_data;
4312 struct file *file = map->file;
4313 struct zoran_fh *fh = file->private_data;
4314 struct zoran *zr = fh->zr;
4318 if (map->count == 0) {
4319 switch (fh->map_mode) {
4320 case ZORAN_MAP_MODE_JPG_REC:
4321 case ZORAN_MAP_MODE_JPG_PLAY:
4323 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
4326 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
4327 if (fh->jpg_buffers.buffer[i].map == map) {
4328 fh->jpg_buffers.buffer[i].map =
4334 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
4335 if (fh->jpg_buffers.buffer[i].map)
4337 if (i == fh->jpg_buffers.num_buffers) {
4338 mutex_lock(&zr->resource_lock);
4340 if (fh->jpg_buffers.active != ZORAN_FREE) {
4341 jpg_qbuf(file, -1, zr->codec_mode);
4342 zr->jpg_buffers.allocated = 0;
4343 zr->jpg_buffers.active =
4344 fh->jpg_buffers.active =
4347 //jpg_fbuffer_free(file);
4348 fh->jpg_buffers.allocated = 0;
4349 fh->jpg_buffers.ready_to_be_freed = 1;
4351 mutex_unlock(&zr->resource_lock);
4356 case ZORAN_MAP_MODE_RAW:
4358 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
4361 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
4362 if (fh->v4l_buffers.buffer[i].map == map) {
4364 fh->v4l_buffers.buffer[i].map =
4370 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
4371 if (fh->v4l_buffers.buffer[i].map)
4373 if (i == fh->v4l_buffers.num_buffers) {
4374 mutex_lock(&zr->resource_lock);
4376 if (fh->v4l_buffers.active != ZORAN_FREE) {
4377 unsigned long flags;
4379 spin_lock_irqsave(&zr->spinlock, flags);
4380 zr36057_set_memgrab(zr, 0);
4381 zr->v4l_buffers.allocated = 0;
4382 zr->v4l_buffers.active =
4383 fh->v4l_buffers.active =
4385 spin_unlock_irqrestore(&zr->spinlock, flags);
4387 //v4l_fbuffer_free(file);
4388 fh->v4l_buffers.allocated = 0;
4389 fh->v4l_buffers.ready_to_be_freed = 1;
4391 mutex_unlock(&zr->resource_lock);
4398 "%s: munmap() - internal error - unknown map mode %d\n",
4399 ZR_DEVNAME(zr), fh->map_mode);
4406 static struct vm_operations_struct zoran_vm_ops = {
4407 .open = zoran_vm_open,
4408 .close = zoran_vm_close,
4412 zoran_mmap (struct file *file,
4413 struct vm_area_struct *vma)
4415 struct zoran_fh *fh = file->private_data;
4416 struct zoran *zr = fh->zr;
4417 unsigned long size = (vma->vm_end - vma->vm_start);
4418 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4420 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
4422 struct zoran_mapping *map;
4426 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
4428 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
4429 vma->vm_start, vma->vm_end, size);
4431 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
4432 !(vma->vm_flags & VM_WRITE)) {
4435 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
4440 switch (fh->map_mode) {
4442 case ZORAN_MAP_MODE_JPG_REC:
4443 case ZORAN_MAP_MODE_JPG_PLAY:
4446 mutex_lock(&zr->resource_lock);
4448 /* Map the MJPEG buffers */
4449 if (!fh->jpg_buffers.allocated) {
4452 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
4455 goto jpg_mmap_unlock_and_return;
4458 first = offset / fh->jpg_buffers.buffer_size;
4459 last = first - 1 + size / fh->jpg_buffers.buffer_size;
4460 if (offset % fh->jpg_buffers.buffer_size != 0 ||
4461 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
4462 last < 0 || first >= fh->jpg_buffers.num_buffers ||
4463 last >= fh->jpg_buffers.num_buffers) {
4466 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4467 ZR_DEVNAME(zr), offset, size,
4468 fh->jpg_buffers.buffer_size,
4469 fh->jpg_buffers.num_buffers);
4471 goto jpg_mmap_unlock_and_return;
4473 for (i = first; i <= last; i++) {
4474 if (fh->jpg_buffers.buffer[i].map) {
4477 "%s: mmap(MJPEG) - buffer %d already mapped\n",
4480 goto jpg_mmap_unlock_and_return;
4484 /* map these buffers (v4l_buffers[i]) */
4485 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4488 goto jpg_mmap_unlock_and_return;
4493 vma->vm_ops = &zoran_vm_ops;
4494 vma->vm_flags |= VM_DONTEXPAND;
4495 vma->vm_private_data = map;
4497 for (i = first; i <= last; i++) {
4499 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
4502 (le32_to_cpu(fh->jpg_buffers.buffer[i].
4503 frag_tab[2 * j + 1]) & ~1) << 1;
4508 le32_to_cpu(fh->jpg_buffers.
4509 buffer[i].frag_tab[2 * j]);
4510 /* should just be pos on i386 */
4511 page = virt_to_phys(bus_to_virt(pos))
4513 if (remap_pfn_range(vma, start, page,
4514 todo, PAGE_SHARED)) {
4517 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
4520 goto jpg_mmap_unlock_and_return;
4526 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
4527 frag_tab[2 * j + 1]) & 1)
4528 break; /* was last fragment */
4530 fh->jpg_buffers.buffer[i].map = map;
4535 jpg_mmap_unlock_and_return:
4536 mutex_unlock(&zr->resource_lock);
4540 case ZORAN_MAP_MODE_RAW:
4542 mutex_lock(&zr->resource_lock);
4544 /* Map the V4L buffers */
4545 if (!fh->v4l_buffers.allocated) {
4548 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
4551 goto v4l_mmap_unlock_and_return;
4554 first = offset / fh->v4l_buffers.buffer_size;
4555 last = first - 1 + size / fh->v4l_buffers.buffer_size;
4556 if (offset % fh->v4l_buffers.buffer_size != 0 ||
4557 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
4558 last < 0 || first >= fh->v4l_buffers.num_buffers ||
4559 last >= fh->v4l_buffers.buffer_size) {
4562 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4563 ZR_DEVNAME(zr), offset, size,
4564 fh->v4l_buffers.buffer_size,
4565 fh->v4l_buffers.num_buffers);
4567 goto v4l_mmap_unlock_and_return;
4569 for (i = first; i <= last; i++) {
4570 if (fh->v4l_buffers.buffer[i].map) {
4573 "%s: mmap(V4L) - buffer %d already mapped\n",
4576 goto v4l_mmap_unlock_and_return;
4580 /* map these buffers (v4l_buffers[i]) */
4581 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4584 goto v4l_mmap_unlock_and_return;
4589 vma->vm_ops = &zoran_vm_ops;
4590 vma->vm_flags |= VM_DONTEXPAND;
4591 vma->vm_private_data = map;
4593 for (i = first; i <= last; i++) {
4595 if (todo > fh->v4l_buffers.buffer_size)
4596 todo = fh->v4l_buffers.buffer_size;
4597 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
4598 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
4599 todo, PAGE_SHARED)) {
4602 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
4605 goto v4l_mmap_unlock_and_return;
4609 fh->v4l_buffers.buffer[i].map = map;
4613 v4l_mmap_unlock_and_return:
4614 mutex_unlock(&zr->resource_lock);
4621 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
4622 ZR_DEVNAME(zr), fh->map_mode);
4629 static const struct file_operations zoran_fops = {
4630 .owner = THIS_MODULE,
4632 .release = zoran_close,
4633 .ioctl = zoran_ioctl,
4634 #ifdef CONFIG_COMPAT
4635 .compat_ioctl = v4l_compat_ioctl32,
4637 .llseek = no_llseek,
4639 .write = zoran_write,
4644 struct video_device zoran_template __devinitdata = {
4646 .type = ZORAN_VID_TYPE,
4647 .type2 = ZORAN_V4L2_VID_FLAGS,
4648 .fops = &zoran_fops,
4649 .release = &zoran_vdev_release,