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 <media/v4l2-ioctl.h>
75 #include "videocodec.h"
77 #include <asm/byteorder.h>
79 #include <asm/uaccess.h>
80 #include <linux/proc_fs.h>
82 #include <linux/video_decoder.h>
83 #include <linux/video_encoder.h>
84 #include <linux/mutex.h>
86 #include "zoran_device.h"
87 #include "zoran_card.h"
89 /* we declare some card type definitions here, they mean
90 * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
91 #define ZORAN_V4L2_VID_FLAGS ( \
93 V4L2_CAP_VIDEO_CAPTURE |\
94 V4L2_CAP_VIDEO_OUTPUT |\
95 V4L2_CAP_VIDEO_OVERLAY \
99 #if defined(CONFIG_VIDEO_V4L1_COMPAT)
100 #define ZFMT(pal, fcc, cs) \
101 .palette = (pal), .fourcc = (fcc), .colorspace = (cs)
103 #define ZFMT(pal, fcc, cs) \
104 .fourcc = (fcc), .colorspace = (cs)
107 const struct zoran_format zoran_formats[] = {
109 .name = "15-bit RGB LE",
110 ZFMT(VIDEO_PALETTE_RGB555,
111 V4L2_PIX_FMT_RGB555, V4L2_COLORSPACE_SRGB),
113 .flags = ZORAN_FORMAT_CAPTURE |
114 ZORAN_FORMAT_OVERLAY,
115 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
116 ZR36057_VFESPFR_LittleEndian,
118 .name = "15-bit RGB BE",
120 V4L2_PIX_FMT_RGB555X, V4L2_COLORSPACE_SRGB),
122 .flags = ZORAN_FORMAT_CAPTURE |
123 ZORAN_FORMAT_OVERLAY,
124 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
126 .name = "16-bit RGB LE",
127 ZFMT(VIDEO_PALETTE_RGB565,
128 V4L2_PIX_FMT_RGB565, V4L2_COLORSPACE_SRGB),
130 .flags = ZORAN_FORMAT_CAPTURE |
131 ZORAN_FORMAT_OVERLAY,
132 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
133 ZR36057_VFESPFR_LittleEndian,
135 .name = "16-bit RGB BE",
137 V4L2_PIX_FMT_RGB565X, V4L2_COLORSPACE_SRGB),
139 .flags = ZORAN_FORMAT_CAPTURE |
140 ZORAN_FORMAT_OVERLAY,
141 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
143 .name = "24-bit RGB",
144 ZFMT(VIDEO_PALETTE_RGB24,
145 V4L2_PIX_FMT_BGR24, V4L2_COLORSPACE_SRGB),
147 .flags = ZORAN_FORMAT_CAPTURE |
148 ZORAN_FORMAT_OVERLAY,
149 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
151 .name = "32-bit RGB LE",
152 ZFMT(VIDEO_PALETTE_RGB32,
153 V4L2_PIX_FMT_BGR32, V4L2_COLORSPACE_SRGB),
155 .flags = ZORAN_FORMAT_CAPTURE |
156 ZORAN_FORMAT_OVERLAY,
157 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
159 .name = "32-bit RGB BE",
161 V4L2_PIX_FMT_RGB32, V4L2_COLORSPACE_SRGB),
163 .flags = ZORAN_FORMAT_CAPTURE |
164 ZORAN_FORMAT_OVERLAY,
165 .vfespfr = ZR36057_VFESPFR_RGB888,
167 .name = "4:2:2, packed, YUYV",
168 ZFMT(VIDEO_PALETTE_YUV422,
169 V4L2_PIX_FMT_YUYV, V4L2_COLORSPACE_SMPTE170M),
171 .flags = ZORAN_FORMAT_CAPTURE |
172 ZORAN_FORMAT_OVERLAY,
173 .vfespfr = ZR36057_VFESPFR_YUV422,
175 .name = "4:2:2, packed, UYVY",
176 ZFMT(VIDEO_PALETTE_UYVY,
177 V4L2_PIX_FMT_UYVY, V4L2_COLORSPACE_SMPTE170M),
179 .flags = ZORAN_FORMAT_CAPTURE |
180 ZORAN_FORMAT_OVERLAY,
181 .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
183 .name = "Hardware-encoded Motion-JPEG",
185 V4L2_PIX_FMT_MJPEG, V4L2_COLORSPACE_SMPTE170M),
187 .flags = ZORAN_FORMAT_CAPTURE |
188 ZORAN_FORMAT_PLAYBACK |
189 ZORAN_FORMAT_COMPRESSED,
192 #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
194 // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
197 static int lock_norm; /* 0 = default 1 = Don't change TV standard (norm) */
198 module_param(lock_norm, int, 0644);
199 MODULE_PARM_DESC(lock_norm, "Prevent norm changes (1 = ignore, >1 = fail)");
201 /* small helper function for calculating buffersizes for v4l2
202 * we calculate the nearest higher power-of-two, which
203 * will be the recommended buffersize */
205 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
207 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
208 __u32 num = (1024 * 512) / (div);
217 if (result > jpg_bufsize)
224 /* forward references */
225 static void v4l_fbuffer_free(struct file *file);
226 static void jpg_fbuffer_free(struct file *file);
229 * Allocate the V4L grab buffers
231 * These have to be pysically contiguous.
232 * If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
233 * else we try to allocate them with bigphysarea_alloc_pages
234 * if the bigphysarea patch is present in the kernel,
235 * else we try to use high memory (if the user has bootet
236 * Linux with the necessary memory left over).
240 get_high_mem (unsigned long size)
243 * Check if there is usable memory at the end of Linux memory
244 * of at least size. Return the physical address of this memory,
245 * return 0 on failure.
247 * The idea is from Alexandro Rubini's book "Linux device drivers".
248 * The driver from him which is downloadable from O'Reilly's
249 * web site misses the "virt_to_phys(high_memory)" part
250 * (and therefore doesn't work at all - at least with 2.2.x kernels).
252 * It should be unnecessary to mention that THIS IS DANGEROUS,
253 * if more than one driver at a time has the idea to use this memory!!!!
256 volatile unsigned char __iomem *mem;
258 unsigned long hi_mem_ph;
261 /* Map the high memory to user space */
263 hi_mem_ph = virt_to_phys(high_memory);
265 mem = ioremap(hi_mem_ph, size);
268 KERN_ERR "%s: get_high_mem() - ioremap failed\n",
273 for (i = 0; i < size; i++) {
274 /* Check if it is memory */
277 if (readb(mem + i) != c)
281 if (readb(mem + i) != c)
283 writeb(0, mem + i); /* zero out memory */
285 /* give the kernel air to breath */
286 if ((i & 0x3ffff) == 0x3ffff)
295 "%s: get_high_mem() - requested %lu, avail %lu\n",
296 ZORAN_NAME, size, i);
304 v4l_fbuffer_alloc (struct file *file)
306 struct zoran_fh *fh = file->private_data;
307 struct zoran *zr = fh->zr;
310 unsigned long pmem = 0;
312 /* we might have old buffers lying around... */
313 if (fh->v4l_buffers.ready_to_be_freed) {
314 v4l_fbuffer_free(file);
317 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
318 if (fh->v4l_buffers.buffer[i].fbuffer)
321 "%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
325 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
328 mem = kmalloc(fh->v4l_buffers.buffer_size, GFP_KERNEL);
332 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
334 v4l_fbuffer_free(file);
337 fh->v4l_buffers.buffer[i].fbuffer = mem;
338 fh->v4l_buffers.buffer[i].fbuffer_phys =
340 fh->v4l_buffers.buffer[i].fbuffer_bus =
342 for (off = 0; off < fh->v4l_buffers.buffer_size;
344 SetPageReserved(MAP_NR(mem + off));
347 "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
348 ZR_DEVNAME(zr), i, (unsigned long) mem,
352 /* Use high memory which has been left at boot time */
354 /* Ok., Ok. this is an evil hack - we make
355 * the assumption that physical addresses are
356 * the same as bus addresses (true at least
357 * for Intel processors). The whole method of
358 * obtaining and using this memory is not very
359 * nice - but I hope it saves some poor users
360 * from kernel hacking, which might have even
361 * more evil results */
365 fh->v4l_buffers.num_buffers *
366 fh->v4l_buffers.buffer_size;
368 pmem = get_high_mem(size);
372 "%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
373 ZR_DEVNAME(zr), size >> 10);
376 fh->v4l_buffers.buffer[0].fbuffer = NULL;
377 fh->v4l_buffers.buffer[0].fbuffer_phys = pmem;
378 fh->v4l_buffers.buffer[0].fbuffer_bus = pmem;
381 "%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
382 ZR_DEVNAME(zr), size >> 10);
384 fh->v4l_buffers.buffer[i].fbuffer = NULL;
385 fh->v4l_buffers.buffer[i].fbuffer_phys =
386 pmem + i * fh->v4l_buffers.buffer_size;
387 fh->v4l_buffers.buffer[i].fbuffer_bus =
388 pmem + i * fh->v4l_buffers.buffer_size;
393 fh->v4l_buffers.allocated = 1;
398 /* free the V4L grab buffers */
400 v4l_fbuffer_free (struct file *file)
402 struct zoran_fh *fh = file->private_data;
403 struct zoran *zr = fh->zr;
407 dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
409 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
410 if (!fh->v4l_buffers.buffer[i].fbuffer)
413 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
414 mem = fh->v4l_buffers.buffer[i].fbuffer;
415 for (off = 0; off < fh->v4l_buffers.buffer_size;
417 ClearPageReserved(MAP_NR(mem + off));
418 kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
420 fh->v4l_buffers.buffer[i].fbuffer = NULL;
423 fh->v4l_buffers.allocated = 0;
424 fh->v4l_buffers.ready_to_be_freed = 0;
428 * Allocate the MJPEG grab buffers.
430 * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
431 * kmalloc is used to request a physically contiguous area,
432 * else we allocate the memory in framgents with get_zeroed_page.
434 * If a Natoma chipset is present and this is a revision 1 zr36057,
435 * each MJPEG buffer needs to be physically contiguous.
436 * (RJ: This statement is from Dave Perks' original driver,
437 * I could never check it because I have a zr36067)
438 * The driver cares about this because it reduces the buffer
439 * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
441 * RJ: The contents grab buffers needs never be accessed in the driver.
442 * Therefore there is no need to allocate them with vmalloc in order
443 * to get a contiguous virtual memory space.
444 * I don't understand why many other drivers first allocate them with
445 * vmalloc (which uses internally also get_zeroed_page, but delivers you
446 * virtual addresses) and then again have to make a lot of efforts
447 * to get the physical address.
450 * On big-endian architectures (such as ppc) some extra steps
451 * are needed. When reading and writing to the stat_com array
452 * and fragment buffers, the device expects to see little-
453 * endian values. The use of cpu_to_le32() and le32_to_cpu()
454 * in this function (and one or two others in zoran_device.c)
455 * ensure that these values are always stored in little-endian
456 * form, regardless of architecture. The zr36057 does Very Bad
457 * Things on big endian architectures if the stat_com array
458 * and fragment buffers are not little-endian.
462 jpg_fbuffer_alloc (struct file *file)
464 struct zoran_fh *fh = file->private_data;
465 struct zoran *zr = fh->zr;
469 /* we might have old buffers lying around */
470 if (fh->jpg_buffers.ready_to_be_freed) {
471 jpg_fbuffer_free(file);
474 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
475 if (fh->jpg_buffers.buffer[i].frag_tab)
478 "%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
481 /* Allocate fragment table for this buffer */
483 mem = get_zeroed_page(GFP_KERNEL);
487 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
489 jpg_fbuffer_free(file);
492 fh->jpg_buffers.buffer[i].frag_tab = (__le32 *) mem;
493 fh->jpg_buffers.buffer[i].frag_tab_bus =
494 virt_to_bus((void *) mem);
496 //if (alloc_contig) {
497 if (fh->jpg_buffers.need_contiguous) {
499 (unsigned long) kmalloc(fh->jpg_buffers.
505 "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
507 jpg_fbuffer_free(file);
510 fh->jpg_buffers.buffer[i].frag_tab[0] =
511 cpu_to_le32(virt_to_bus((void *) mem));
512 fh->jpg_buffers.buffer[i].frag_tab[1] =
513 cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
514 for (off = 0; off < fh->jpg_buffers.buffer_size;
516 SetPageReserved(MAP_NR(mem + off));
518 /* jpg_bufsize is allreay page aligned */
520 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
522 mem = get_zeroed_page(GFP_KERNEL);
526 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
528 jpg_fbuffer_free(file);
532 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
533 cpu_to_le32(virt_to_bus((void *) mem));
534 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
536 cpu_to_le32((PAGE_SIZE / 4) << 1);
537 SetPageReserved(MAP_NR(mem));
540 fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
545 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
547 (fh->jpg_buffers.num_buffers *
548 fh->jpg_buffers.buffer_size) >> 10);
550 fh->jpg_buffers.allocated = 1;
555 /* free the MJPEG grab buffers */
557 jpg_fbuffer_free (struct file *file)
559 struct zoran_fh *fh = file->private_data;
560 struct zoran *zr = fh->zr;
564 dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
566 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
567 if (!fh->jpg_buffers.buffer[i].frag_tab)
570 //if (alloc_contig) {
571 if (fh->jpg_buffers.need_contiguous) {
572 if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
573 mem = (unsigned char *) bus_to_virt(le32_to_cpu(
574 fh->jpg_buffers.buffer[i].frag_tab[0]));
576 off < fh->jpg_buffers.buffer_size;
578 ClearPageReserved(MAP_NR
581 fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
582 fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
586 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
588 if (!fh->jpg_buffers.buffer[i].
591 ClearPageReserved(MAP_NR
595 buffer[i].frag_tab[2 *
597 free_page((unsigned long)
603 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
605 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
610 free_page((unsigned long) fh->jpg_buffers.buffer[i].
612 fh->jpg_buffers.buffer[i].frag_tab = NULL;
615 fh->jpg_buffers.allocated = 0;
616 fh->jpg_buffers.ready_to_be_freed = 0;
620 * V4L Buffer grabbing
624 zoran_v4l_set_format (struct file *file,
627 const struct zoran_format *format)
629 struct zoran_fh *fh = file->private_data;
630 struct zoran *zr = fh->zr;
633 /* Check size and format of the grab wanted */
635 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
636 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
639 "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
640 ZR_DEVNAME(zr), width, height);
644 bpp = (format->depth + 7) / 8;
646 /* Check against available buffer size */
647 if (height * width * bpp > fh->v4l_buffers.buffer_size) {
650 "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
651 ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
655 /* The video front end needs 4-byte alinged line sizes */
657 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
660 "%s: v4l_set_format() - wrong frame alingment\n",
665 fh->v4l_settings.width = width;
666 fh->v4l_settings.height = height;
667 fh->v4l_settings.format = format;
668 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
674 zoran_v4l_queue_frame (struct file *file,
677 struct zoran_fh *fh = file->private_data;
678 struct zoran *zr = fh->zr;
682 if (!fh->v4l_buffers.allocated) {
685 "%s: v4l_queue_frame() - buffers not yet allocated\n",
690 /* No grabbing outside the buffer range! */
691 if (num >= fh->v4l_buffers.num_buffers || num < 0) {
694 "%s: v4l_queue_frame() - buffer %d is out of range\n",
695 ZR_DEVNAME(zr), num);
699 spin_lock_irqsave(&zr->spinlock, flags);
701 if (fh->v4l_buffers.active == ZORAN_FREE) {
702 if (zr->v4l_buffers.active == ZORAN_FREE) {
703 zr->v4l_buffers = fh->v4l_buffers;
704 fh->v4l_buffers.active = ZORAN_ACTIVE;
708 "%s: v4l_queue_frame() - another session is already capturing\n",
714 /* make sure a grab isn't going on currently with this buffer */
716 switch (zr->v4l_buffers.buffer[num].state) {
719 if (zr->v4l_buffers.active == ZORAN_FREE) {
720 fh->v4l_buffers.active = ZORAN_FREE;
721 zr->v4l_buffers.allocated = 0;
723 res = -EBUSY; /* what are you doing? */
728 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
729 ZR_DEVNAME(zr), num);
731 /* since there is at least one unused buffer there's room for at least
732 * one more pend[] entry */
733 zr->v4l_pend[zr->v4l_pend_head++ &
734 V4L_MASK_FRAME] = num;
735 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
736 zr->v4l_buffers.buffer[num].bs.length =
737 fh->v4l_settings.bytesperline *
738 zr->v4l_settings.height;
739 fh->v4l_buffers.buffer[num] =
740 zr->v4l_buffers.buffer[num];
745 spin_unlock_irqrestore(&zr->spinlock, flags);
747 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
748 zr->v4l_buffers.active = fh->v4l_buffers.active;
754 v4l_grab (struct file *file,
755 struct video_mmap *mp)
757 struct zoran_fh *fh = file->private_data;
758 struct zoran *zr = fh->zr;
761 for (i = 0; i < NUM_FORMATS; i++) {
762 if (zoran_formats[i].palette == mp->format &&
763 zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
764 !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
767 if (i == NUM_FORMATS || zoran_formats[i].depth == 0) {
770 "%s: v4l_grab() - wrong bytes-per-pixel format\n",
776 * To minimize the time spent in the IRQ routine, we avoid setting up
777 * the video front end there.
778 * If this grab has different parameters from a running streaming capture
779 * we stop the streaming capture and start it over again.
781 if (zr->v4l_memgrab_active &&
782 (zr->v4l_settings.width != mp->width ||
783 zr->v4l_settings.height != mp->height ||
784 zr->v4l_settings.format->palette != mp->format)) {
785 res = wait_grab_pending(zr);
789 if ((res = zoran_v4l_set_format(file,
794 zr->v4l_settings = fh->v4l_settings;
796 /* queue the frame in the pending queue */
797 if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
798 fh->v4l_buffers.active = ZORAN_FREE;
802 /* put the 36057 into frame grabbing mode */
803 if (!res && !zr->v4l_memgrab_active)
804 zr36057_set_memgrab(zr, 1);
806 //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
812 * Sync on a V4L buffer
816 v4l_sync (struct file *file,
819 struct zoran_fh *fh = file->private_data;
820 struct zoran *zr = fh->zr;
823 if (fh->v4l_buffers.active == ZORAN_FREE) {
826 "%s: v4l_sync() - no grab active for this session\n",
831 /* check passed-in frame number */
832 if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
834 KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
835 ZR_DEVNAME(zr), frame);
839 /* Check if is buffer was queued at all */
840 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
843 "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
848 /* wait on this buffer to get ready */
849 if (!wait_event_interruptible_timeout(zr->v4l_capq,
850 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
853 if (signal_pending(current))
856 /* buffer should now be in BUZ_STATE_DONE */
857 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
859 KERN_ERR "%s: v4l_sync() - internal state error\n",
862 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
863 fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
865 spin_lock_irqsave(&zr->spinlock, flags);
867 /* Check if streaming capture has finished */
868 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
869 zr36057_set_memgrab(zr, 0);
870 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
871 fh->v4l_buffers.active = zr->v4l_buffers.active =
873 zr->v4l_buffers.allocated = 0;
877 spin_unlock_irqrestore(&zr->spinlock, flags);
883 * Queue a MJPEG buffer for capture/playback
887 zoran_jpg_queue_frame (struct file *file,
889 enum zoran_codec_mode mode)
891 struct zoran_fh *fh = file->private_data;
892 struct zoran *zr = fh->zr;
896 /* Check if buffers are allocated */
897 if (!fh->jpg_buffers.allocated) {
900 "%s: jpg_queue_frame() - buffers not yet allocated\n",
905 /* No grabbing outside the buffer range! */
906 if (num >= fh->jpg_buffers.num_buffers || num < 0) {
909 "%s: jpg_queue_frame() - buffer %d out of range\n",
910 ZR_DEVNAME(zr), num);
914 /* what is the codec mode right now? */
915 if (zr->codec_mode == BUZ_MODE_IDLE) {
916 zr->jpg_settings = fh->jpg_settings;
917 } else if (zr->codec_mode != mode) {
918 /* wrong codec mode active - invalid */
921 "%s: jpg_queue_frame() - codec in wrong mode\n",
926 if (fh->jpg_buffers.active == ZORAN_FREE) {
927 if (zr->jpg_buffers.active == ZORAN_FREE) {
928 zr->jpg_buffers = fh->jpg_buffers;
929 fh->jpg_buffers.active = ZORAN_ACTIVE;
933 "%s: jpg_queue_frame() - another session is already capturing\n",
939 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
940 /* Ok load up the jpeg codec */
941 zr36057_enable_jpg(zr, mode);
944 spin_lock_irqsave(&zr->spinlock, flags);
947 switch (zr->jpg_buffers.buffer[num].state) {
951 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
954 /* since there is at least one unused buffer there's room for at
955 *least one more pend[] entry */
956 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
958 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
959 fh->jpg_buffers.buffer[num] =
960 zr->jpg_buffers.buffer[num];
961 zoran_feed_stat_com(zr);
966 if (zr->jpg_buffers.active == ZORAN_FREE) {
967 fh->jpg_buffers.active = ZORAN_FREE;
968 zr->jpg_buffers.allocated = 0;
970 res = -EBUSY; /* what are you doing? */
975 spin_unlock_irqrestore(&zr->spinlock, flags);
977 if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
978 zr->jpg_buffers.active = fh->jpg_buffers.active;
985 jpg_qbuf (struct file *file,
987 enum zoran_codec_mode mode)
989 struct zoran_fh *fh = file->private_data;
990 struct zoran *zr = fh->zr;
993 /* Does the user want to stop streaming? */
995 if (zr->codec_mode == mode) {
996 if (fh->jpg_buffers.active == ZORAN_FREE) {
999 "%s: jpg_qbuf(-1) - session not active\n",
1003 fh->jpg_buffers.active = zr->jpg_buffers.active =
1005 zr->jpg_buffers.allocated = 0;
1006 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1011 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
1017 if ((res = zoran_jpg_queue_frame(file, frame, mode)))
1020 /* Start the jpeg codec when the first frame is queued */
1021 if (!res && zr->jpg_que_head == 1)
1028 * Sync on a MJPEG buffer
1032 jpg_sync (struct file *file,
1033 struct zoran_sync *bs)
1035 struct zoran_fh *fh = file->private_data;
1036 struct zoran *zr = fh->zr;
1037 unsigned long flags;
1040 if (fh->jpg_buffers.active == ZORAN_FREE) {
1043 "%s: jpg_sync() - capture is not currently active\n",
1047 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
1048 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
1051 "%s: jpg_sync() - codec not in streaming mode\n",
1055 if (!wait_event_interruptible_timeout(zr->jpg_capq,
1056 (zr->jpg_que_tail != zr->jpg_dma_tail ||
1057 zr->jpg_dma_tail == zr->jpg_dma_head),
1061 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1063 zr->codec->control(zr->codec, CODEC_G_STATUS,
1067 "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
1068 ZR_DEVNAME(zr), isr);
1073 if (signal_pending(current))
1074 return -ERESTARTSYS;
1076 spin_lock_irqsave(&zr->spinlock, flags);
1078 if (zr->jpg_dma_tail != zr->jpg_dma_head)
1079 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
1081 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
1083 /* buffer should now be in BUZ_STATE_DONE */
1084 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
1086 KERN_ERR "%s: jpg_sync() - internal state error\n",
1089 *bs = zr->jpg_buffers.buffer[frame].bs;
1091 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
1092 fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
1094 spin_unlock_irqrestore(&zr->spinlock, flags);
1100 zoran_open_init_session (struct file *file)
1103 struct zoran_fh *fh = file->private_data;
1104 struct zoran *zr = fh->zr;
1106 /* Per default, map the V4L Buffers */
1107 fh->map_mode = ZORAN_MAP_MODE_RAW;
1109 /* take over the card's current settings */
1110 fh->overlay_settings = zr->overlay_settings;
1111 fh->overlay_settings.is_set = 0;
1112 fh->overlay_settings.format = zr->overlay_settings.format;
1113 fh->overlay_active = ZORAN_FREE;
1116 fh->v4l_settings = zr->v4l_settings;
1119 memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
1120 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1121 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1122 fh->v4l_buffers.buffer[i].bs.frame = i;
1124 fh->v4l_buffers.allocated = 0;
1125 fh->v4l_buffers.ready_to_be_freed = 0;
1126 fh->v4l_buffers.active = ZORAN_FREE;
1127 fh->v4l_buffers.buffer_size = v4l_bufsize;
1128 fh->v4l_buffers.num_buffers = v4l_nbufs;
1131 fh->jpg_settings = zr->jpg_settings;
1134 memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
1135 for (i = 0; i < BUZ_MAX_FRAME; i++) {
1136 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1137 fh->jpg_buffers.buffer[i].bs.frame = i;
1139 fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
1140 fh->jpg_buffers.allocated = 0;
1141 fh->jpg_buffers.ready_to_be_freed = 0;
1142 fh->jpg_buffers.active = ZORAN_FREE;
1143 fh->jpg_buffers.buffer_size = jpg_bufsize;
1144 fh->jpg_buffers.num_buffers = jpg_nbufs;
1148 zoran_close_end_session (struct file *file)
1150 struct zoran_fh *fh = file->private_data;
1151 struct zoran *zr = fh->zr;
1154 if (fh->overlay_active != ZORAN_FREE) {
1155 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
1156 zr->v4l_overlay_active = 0;
1157 if (!zr->v4l_memgrab_active)
1158 zr36057_overlay(zr, 0);
1159 zr->overlay_mask = NULL;
1163 if (fh->v4l_buffers.active != ZORAN_FREE) {
1164 unsigned long flags;
1166 spin_lock_irqsave(&zr->spinlock, flags);
1167 zr36057_set_memgrab(zr, 0);
1168 zr->v4l_buffers.allocated = 0;
1169 zr->v4l_buffers.active = fh->v4l_buffers.active =
1171 spin_unlock_irqrestore(&zr->spinlock, flags);
1175 if (fh->v4l_buffers.allocated ||
1176 fh->v4l_buffers.ready_to_be_freed) {
1177 v4l_fbuffer_free(file);
1181 if (fh->jpg_buffers.active != ZORAN_FREE) {
1182 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1183 zr->jpg_buffers.allocated = 0;
1184 zr->jpg_buffers.active = fh->jpg_buffers.active =
1189 if (fh->jpg_buffers.allocated ||
1190 fh->jpg_buffers.ready_to_be_freed) {
1191 jpg_fbuffer_free(file);
1196 * Open a zoran card. Right now the flags stuff is just playing
1200 zoran_open (struct inode *inode,
1203 unsigned int minor = iminor(inode);
1204 struct zoran *zr = NULL;
1205 struct zoran_fh *fh;
1206 int i, res, first_open = 0, have_module_locks = 0;
1209 /* find the device */
1210 for (i = 0; i < zoran_num; i++) {
1211 if (zoran[i]->video_dev->minor == minor) {
1218 dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
1220 goto open_unlock_and_return;
1223 /* see fs/device.c - the kernel already locks during open(),
1224 * so locking ourselves only causes deadlocks */
1225 /*mutex_lock(&zr->resource_lock);*/
1229 KERN_ERR "%s: no TV decoder loaded for device!\n",
1232 goto open_unlock_and_return;
1235 /* try to grab a module lock */
1236 if (!try_module_get(THIS_MODULE)) {
1239 "%s: failed to acquire my own lock! PANIC!\n",
1242 goto open_unlock_and_return;
1244 if (!try_module_get(zr->decoder->driver->driver.owner)) {
1247 "%s: failed to grab ownership of i2c decoder\n",
1250 module_put(THIS_MODULE);
1251 goto open_unlock_and_return;
1254 !try_module_get(zr->encoder->driver->driver.owner)) {
1257 "%s: failed to grab ownership of i2c encoder\n",
1260 module_put(zr->decoder->driver->driver.owner);
1261 module_put(THIS_MODULE);
1262 goto open_unlock_and_return;
1265 have_module_locks = 1;
1267 if (zr->user >= 2048) {
1268 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1269 ZR_DEVNAME(zr), zr->user);
1271 goto open_unlock_and_return;
1274 dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1275 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user);
1277 /* now, create the open()-specific file_ops struct */
1278 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1282 "%s: zoran_open() - allocation of zoran_fh failed\n",
1285 goto open_unlock_and_return;
1287 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1288 * on norm-change! */
1290 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1291 if (!fh->overlay_mask) {
1294 "%s: zoran_open() - allocation of overlay_mask failed\n",
1298 goto open_unlock_and_return;
1301 if (zr->user++ == 0)
1304 /*mutex_unlock(&zr->resource_lock);*/
1306 /* default setup - TODO: look at flags */
1307 if (first_open) { /* First device open */
1308 zr36057_restart(zr);
1309 zoran_open_init_params(zr);
1310 zoran_init_hardware(zr);
1312 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1315 /* set file_ops stuff */
1316 file->private_data = fh;
1318 zoran_open_init_session(file);
1323 open_unlock_and_return:
1324 /* if we grabbed locks, release them accordingly */
1325 if (have_module_locks) {
1326 module_put(zr->decoder->driver->driver.owner);
1328 module_put(zr->encoder->driver->driver.owner);
1330 module_put(THIS_MODULE);
1333 /* if there's no device found, we didn't obtain the lock either */
1335 /*mutex_unlock(&zr->resource_lock);*/
1343 zoran_close (struct inode *inode,
1346 struct zoran_fh *fh = file->private_data;
1347 struct zoran *zr = fh->zr;
1349 dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1350 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user);
1352 /* kernel locks (fs/device.c), so don't do that ourselves
1353 * (prevents deadlocks) */
1354 /*mutex_lock(&zr->resource_lock);*/
1356 zoran_close_end_session(file);
1358 if (zr->user-- == 1) { /* Last process */
1359 /* Clean up JPEG process */
1360 wake_up_interruptible(&zr->jpg_capq);
1361 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1362 zr->jpg_buffers.allocated = 0;
1363 zr->jpg_buffers.active = ZORAN_FREE;
1365 /* disable interrupts */
1366 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1368 if (zr36067_debug > 1)
1369 print_interrupts(zr);
1372 zr->v4l_overlay_active = 0;
1373 zr36057_overlay(zr, 0);
1374 zr->overlay_mask = NULL;
1377 wake_up_interruptible(&zr->v4l_capq);
1378 zr36057_set_memgrab(zr, 0);
1379 zr->v4l_buffers.allocated = 0;
1380 zr->v4l_buffers.active = ZORAN_FREE;
1381 zoran_set_pci_master(zr, 0);
1383 if (!pass_through) { /* Switch to color bar */
1384 int zero = 0, two = 2;
1385 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1386 encoder_command(zr, ENCODER_SET_INPUT, &two);
1390 file->private_data = NULL;
1391 kfree(fh->overlay_mask);
1394 /* release locks on the i2c modules */
1395 module_put(zr->decoder->driver->driver.owner);
1397 module_put(zr->encoder->driver->driver.owner);
1399 module_put(THIS_MODULE);
1401 /*mutex_unlock(&zr->resource_lock);*/
1403 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1410 zoran_read (struct file *file,
1415 /* we simply don't support read() (yet)... */
1421 zoran_write (struct file *file,
1422 const char __user *data,
1426 /* ...and the same goes for write() */
1432 setup_fbuffer (struct file *file,
1434 const struct zoran_format *fmt,
1439 struct zoran_fh *fh = file->private_data;
1440 struct zoran *zr = fh->zr;
1442 /* (Ronald) v4l/v4l2 guidelines */
1443 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1446 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1447 ALi Magik (that needs very low latency while the card needs a
1448 higher value always) */
1450 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1453 /* we need a bytesperline value, even if not given */
1455 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1458 if (zr->overlay_active) {
1459 /* dzjee... stupid users... don't even bother to turn off
1460 * overlay before changing the memory location...
1461 * normally, we would return errors here. However, one of
1462 * the tools that does this is... xawtv! and since xawtv
1463 * is used by +/- 99% of the users, we'd rather be user-
1464 * friendly and silently do as if nothing went wrong */
1467 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1469 zr36057_overlay(zr, 0);
1473 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1476 "%s: setup_fbuffer() - no valid overlay format given\n",
1480 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1483 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1484 ZR_DEVNAME(zr), width, height, bytesperline);
1487 if (bytesperline & 3) {
1490 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1491 ZR_DEVNAME(zr), bytesperline);
1495 zr->buffer.base = (void *) ((unsigned long) base & ~3);
1496 zr->buffer.height = height;
1497 zr->buffer.width = width;
1498 zr->buffer.depth = fmt->depth;
1499 zr->overlay_settings.format = fmt;
1500 zr->buffer.bytesperline = bytesperline;
1502 /* The user should set new window parameters */
1503 zr->overlay_settings.is_set = 0;
1510 setup_window (struct file *file,
1515 struct video_clip __user *clips,
1517 void __user *bitmap)
1519 struct zoran_fh *fh = file->private_data;
1520 struct zoran *zr = fh->zr;
1521 struct video_clip *vcp = NULL;
1525 if (!zr->buffer.base) {
1528 "%s: setup_window() - frame buffer has to be set first\n",
1533 if (!fh->overlay_settings.format) {
1536 "%s: setup_window() - no overlay format set\n",
1542 * The video front end needs 4-byte alinged line sizes, we correct that
1543 * silently here if necessary
1545 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1546 end = (x + width) & ~1; /* round down */
1547 x = (x + 1) & ~1; /* round up */
1551 if (zr->buffer.depth == 24) {
1552 end = (x + width) & ~3; /* round down */
1553 x = (x + 3) & ~3; /* round up */
1557 if (width > BUZ_MAX_WIDTH)
1558 width = BUZ_MAX_WIDTH;
1559 if (height > BUZ_MAX_HEIGHT)
1560 height = BUZ_MAX_HEIGHT;
1562 /* Check for vaild parameters */
1563 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1564 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1567 "%s: setup_window() - width = %d or height = %d invalid\n",
1568 ZR_DEVNAME(zr), width, height);
1572 fh->overlay_settings.x = x;
1573 fh->overlay_settings.y = y;
1574 fh->overlay_settings.width = width;
1575 fh->overlay_settings.height = height;
1576 fh->overlay_settings.clipcount = clipcount;
1579 * If an overlay is running, we have to switch it off
1580 * and switch it on again in order to get the new settings in effect.
1582 * We also want to avoid that the overlay mask is written
1583 * when an overlay is running.
1586 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1587 zr->overlay_active != ZORAN_FREE &&
1588 fh->overlay_active != ZORAN_FREE;
1590 zr36057_overlay(zr, 0);
1593 * Write the overlay mask if clips are wanted.
1594 * We prefer a bitmap.
1597 /* fake value - it just means we want clips */
1598 fh->overlay_settings.clipcount = 1;
1600 if (copy_from_user(fh->overlay_mask, bitmap,
1601 (width * height + 7) / 8)) {
1604 } else if (clipcount > 0) {
1605 /* write our own bitmap from the clips */
1606 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1610 "%s: setup_window() - Alloc of clip mask failed\n",
1615 (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1619 write_overlay_mask(file, vcp, clipcount);
1623 fh->overlay_settings.is_set = 1;
1624 if (fh->overlay_active != ZORAN_FREE &&
1625 zr->overlay_active != ZORAN_FREE)
1626 zr->overlay_settings = fh->overlay_settings;
1629 zr36057_overlay(zr, 1);
1631 /* Make sure the changes come into effect */
1632 return wait_grab_pending(zr);
1636 setup_overlay (struct file *file,
1639 struct zoran_fh *fh = file->private_data;
1640 struct zoran *zr = fh->zr;
1642 /* If there is nothing to do, return immediatly */
1643 if ((on && fh->overlay_active != ZORAN_FREE) ||
1644 (!on && fh->overlay_active == ZORAN_FREE))
1647 /* check whether we're touching someone else's overlay */
1648 if (on && zr->overlay_active != ZORAN_FREE &&
1649 fh->overlay_active == ZORAN_FREE) {
1652 "%s: setup_overlay() - overlay is already active for another session\n",
1656 if (!on && zr->overlay_active != ZORAN_FREE &&
1657 fh->overlay_active == ZORAN_FREE) {
1660 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1666 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1667 zr->v4l_overlay_active = 0;
1668 /* When a grab is running, the video simply
1669 * won't be switched on any more */
1670 if (!zr->v4l_memgrab_active)
1671 zr36057_overlay(zr, 0);
1672 zr->overlay_mask = NULL;
1674 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1677 "%s: setup_overlay() - buffer or window not set\n",
1681 if (!fh->overlay_settings.format) {
1684 "%s: setup_overlay() - no overlay format set\n",
1688 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1689 zr->v4l_overlay_active = 1;
1690 zr->overlay_mask = fh->overlay_mask;
1691 zr->overlay_settings = fh->overlay_settings;
1692 if (!zr->v4l_memgrab_active)
1693 zr36057_overlay(zr, 1);
1694 /* When a grab is running, the video will be
1695 * switched on when grab is finished */
1698 /* Make sure the changes come into effect */
1699 return wait_grab_pending(zr);
1702 /* get the status of a buffer in the clients buffer queue */
1704 zoran_v4l2_buffer_status (struct file *file,
1705 struct v4l2_buffer *buf,
1708 struct zoran_fh *fh = file->private_data;
1709 struct zoran *zr = fh->zr;
1711 buf->flags = V4L2_BUF_FLAG_MAPPED;
1713 switch (fh->map_mode) {
1714 case ZORAN_MAP_MODE_RAW:
1717 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1718 !fh->v4l_buffers.allocated) {
1721 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1726 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1727 buf->length = fh->v4l_buffers.buffer_size;
1730 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1731 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1732 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1733 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1734 buf->flags |= V4L2_BUF_FLAG_DONE;
1736 fh->v4l_buffers.buffer[num].bs.timestamp;
1738 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1741 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1742 buf->field = V4L2_FIELD_TOP;
1744 buf->field = V4L2_FIELD_INTERLACED;
1748 case ZORAN_MAP_MODE_JPG_REC:
1749 case ZORAN_MAP_MODE_JPG_PLAY:
1752 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1753 !fh->jpg_buffers.allocated) {
1756 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1761 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1762 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1763 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1764 buf->length = fh->jpg_buffers.buffer_size;
1766 /* these variables are only written after frame has been captured */
1767 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1768 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1769 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1771 fh->jpg_buffers.buffer[num].bs.timestamp;
1773 fh->jpg_buffers.buffer[num].bs.length;
1774 buf->flags |= V4L2_BUF_FLAG_DONE;
1776 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1779 /* which fields are these? */
1780 if (fh->jpg_settings.TmpDcm != 1)
1783 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1787 odd_even ? V4L2_FIELD_SEQ_TB :
1796 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1797 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1801 buf->memory = V4L2_MEMORY_MMAP;
1803 buf->m.offset = buf->length * num;
1809 zoran_set_norm (struct zoran *zr,
1810 int norm) /* VIDEO_MODE_* */
1812 int norm_encoder, on;
1814 if (zr->v4l_buffers.active != ZORAN_FREE ||
1815 zr->jpg_buffers.active != ZORAN_FREE) {
1818 "%s: set_norm() called while in playback/capture mode\n",
1823 if (lock_norm && norm != zr->norm) {
1824 if (lock_norm > 1) {
1827 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1833 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1839 if (norm != VIDEO_MODE_AUTO &&
1840 (norm < 0 || norm >= zr->card.norms ||
1841 !zr->card.tvn[norm])) {
1843 KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1844 ZR_DEVNAME(zr), norm);
1848 if (norm == VIDEO_MODE_AUTO) {
1851 /* if we have autodetect, ... */
1852 struct video_decoder_capability caps;
1853 decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1854 if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1855 dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1860 decoder_command(zr, DECODER_SET_NORM, &norm);
1862 /* let changes come into effect */
1865 decoder_command(zr, DECODER_GET_STATUS, &status);
1866 if (!(status & DECODER_STATUS_GOOD)) {
1869 "%s: set_norm() - no norm detected\n",
1872 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1876 if (status & DECODER_STATUS_NTSC)
1877 norm = VIDEO_MODE_NTSC;
1878 else if (status & DECODER_STATUS_SECAM)
1879 norm = VIDEO_MODE_SECAM;
1881 norm = VIDEO_MODE_PAL;
1883 zr->timing = zr->card.tvn[norm];
1884 norm_encoder = norm;
1886 /* We switch overlay off and on since a change in the
1887 * norm needs different VFE settings */
1888 on = zr->overlay_active && !zr->v4l_memgrab_active;
1890 zr36057_overlay(zr, 0);
1892 decoder_command(zr, DECODER_SET_NORM, &norm);
1893 encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1896 zr36057_overlay(zr, 1);
1898 /* Make sure the changes come into effect */
1905 zoran_set_input (struct zoran *zr,
1910 if (input == zr->input) {
1914 if (zr->v4l_buffers.active != ZORAN_FREE ||
1915 zr->jpg_buffers.active != ZORAN_FREE) {
1918 "%s: set_input() called while in playback/capture mode\n",
1923 if (input < 0 || input >= zr->card.inputs) {
1926 "%s: set_input() - unnsupported input %d\n",
1927 ZR_DEVNAME(zr), input);
1931 realinput = zr->card.input[input].muxsel;
1934 decoder_command(zr, DECODER_SET_INPUT, &realinput);
1944 zoran_do_ioctl (struct inode *inode,
1949 struct zoran_fh *fh = file->private_data;
1950 struct zoran *zr = fh->zr;
1951 /* CAREFUL: used in multiple places here */
1952 struct zoran_jpg_settings settings;
1954 /* we might have older buffers lying around... We don't want
1955 * to wait, but we do want to try cleaning them up ASAP. So
1956 * we try to obtain the lock and free them. If that fails, we
1957 * don't do anything and wait for the next turn. In the end,
1958 * zoran_close() or a new allocation will still free them...
1959 * This is just a 'the sooner the better' extra 'feature'
1961 * We don't free the buffers right on munmap() because that
1962 * causes oopses (kfree() inside munmap() oopses for no
1963 * apparent reason - it's also not reproduceable in any way,
1964 * but moving the free code outside the munmap() handler fixes
1965 * all this... If someone knows why, please explain me (Ronald)
1967 if (mutex_trylock(&zr->resource_lock)) {
1968 /* we obtained it! Let's try to free some things */
1969 if (fh->jpg_buffers.ready_to_be_freed)
1970 jpg_fbuffer_free(file);
1971 if (fh->v4l_buffers.ready_to_be_freed)
1972 v4l_fbuffer_free(file);
1974 mutex_unlock(&zr->resource_lock);
1981 struct video_capability *vcap = arg;
1983 dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
1985 memset(vcap, 0, sizeof(struct video_capability));
1986 strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name)-1);
1987 vcap->type = ZORAN_VID_TYPE;
1989 vcap->channels = zr->card.inputs;
1991 mutex_lock(&zr->resource_lock);
1992 vcap->maxwidth = BUZ_MAX_WIDTH;
1993 vcap->maxheight = BUZ_MAX_HEIGHT;
1994 vcap->minwidth = BUZ_MIN_WIDTH;
1995 vcap->minheight = BUZ_MIN_HEIGHT;
1996 mutex_unlock(&zr->resource_lock);
2004 struct video_channel *vchan = arg;
2005 int channel = vchan->channel;
2007 dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
2008 ZR_DEVNAME(zr), vchan->channel);
2010 memset(vchan, 0, sizeof(struct video_channel));
2011 if (channel > zr->card.inputs || channel < 0) {
2014 "%s: VIDIOCGCHAN on not existing channel %d\n",
2015 ZR_DEVNAME(zr), channel);
2019 strcpy(vchan->name, zr->card.input[channel].name);
2023 vchan->type = VIDEO_TYPE_CAMERA;
2024 mutex_lock(&zr->resource_lock);
2025 vchan->norm = zr->norm;
2026 mutex_unlock(&zr->resource_lock);
2027 vchan->channel = channel;
2033 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2035 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2037 * * The famos BTTV driver has it implemented with a struct video_channel argument
2038 * * and we follow it for compatibility reasons
2040 * * BTW: this is the only way the user can set the norm!
2045 struct video_channel *vchan = arg;
2050 "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
2051 ZR_DEVNAME(zr), vchan->channel, vchan->norm);
2053 mutex_lock(&zr->resource_lock);
2054 if ((res = zoran_set_input(zr, vchan->channel)))
2055 goto schan_unlock_and_return;
2056 if ((res = zoran_set_norm(zr, vchan->norm)))
2057 goto schan_unlock_and_return;
2059 /* Make sure the changes come into effect */
2060 res = wait_grab_pending(zr);
2061 schan_unlock_and_return:
2062 mutex_unlock(&zr->resource_lock);
2069 struct video_picture *vpict = arg;
2071 dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
2073 memset(vpict, 0, sizeof(struct video_picture));
2074 mutex_lock(&zr->resource_lock);
2075 vpict->hue = zr->hue;
2076 vpict->brightness = zr->brightness;
2077 vpict->contrast = zr->contrast;
2078 vpict->colour = zr->saturation;
2079 if (fh->overlay_settings.format) {
2080 vpict->depth = fh->overlay_settings.format->depth;
2081 vpict->palette = fh->overlay_settings.format->palette;
2085 mutex_unlock(&zr->resource_lock);
2093 struct video_picture *vpict = arg;
2098 "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
2099 ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
2100 vpict->colour, vpict->contrast, vpict->depth,
2103 for (i = 0; i < NUM_FORMATS; i++) {
2104 const struct zoran_format *fmt = &zoran_formats[i];
2106 if (fmt->palette != -1 &&
2107 fmt->flags & ZORAN_FORMAT_OVERLAY &&
2108 fmt->palette == vpict->palette &&
2109 fmt->depth == vpict->depth)
2112 if (i == NUM_FORMATS) {
2115 "%s: VIDIOCSPICT - Invalid palette %d\n",
2116 ZR_DEVNAME(zr), vpict->palette);
2120 mutex_lock(&zr->resource_lock);
2122 decoder_command(zr, DECODER_SET_PICTURE, vpict);
2124 zr->hue = vpict->hue;
2125 zr->contrast = vpict->contrast;
2126 zr->saturation = vpict->colour;
2127 zr->brightness = vpict->brightness;
2129 fh->overlay_settings.format = &zoran_formats[i];
2131 mutex_unlock(&zr->resource_lock);
2141 dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
2142 ZR_DEVNAME(zr), *on);
2144 mutex_lock(&zr->resource_lock);
2145 res = setup_overlay(file, *on);
2146 mutex_unlock(&zr->resource_lock);
2154 struct video_window *vwin = arg;
2156 dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
2158 memset(vwin, 0, sizeof(struct video_window));
2159 mutex_lock(&zr->resource_lock);
2160 vwin->x = fh->overlay_settings.x;
2161 vwin->y = fh->overlay_settings.y;
2162 vwin->width = fh->overlay_settings.width;
2163 vwin->height = fh->overlay_settings.height;
2164 mutex_unlock(&zr->resource_lock);
2165 vwin->clipcount = 0;
2172 struct video_window *vwin = arg;
2177 "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
2178 ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
2179 vwin->height, vwin->clipcount);
2181 mutex_lock(&zr->resource_lock);
2183 setup_window(file, vwin->x, vwin->y, vwin->width,
2184 vwin->height, vwin->clips,
2185 vwin->clipcount, NULL);
2186 mutex_unlock(&zr->resource_lock);
2194 struct video_buffer *vbuf = arg;
2196 dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
2198 mutex_lock(&zr->resource_lock);
2200 mutex_unlock(&zr->resource_lock);
2207 struct video_buffer *vbuf = arg;
2212 "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
2213 ZR_DEVNAME(zr), vbuf->base, vbuf->width,
2214 vbuf->height, vbuf->depth, vbuf->bytesperline);
2216 for (i = 0; i < NUM_FORMATS; i++)
2217 if (zoran_formats[i].depth == vbuf->depth)
2219 if (i == NUM_FORMATS) {
2222 "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
2223 ZR_DEVNAME(zr), vbuf->depth);
2227 mutex_lock(&zr->resource_lock);
2229 setup_fbuffer(file, vbuf->base, &zoran_formats[i],
2230 vbuf->width, vbuf->height,
2231 vbuf->bytesperline);
2232 mutex_unlock(&zr->resource_lock);
2240 int *frame = arg, res;
2242 dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
2243 ZR_DEVNAME(zr), *frame);
2245 mutex_lock(&zr->resource_lock);
2246 res = v4l_sync(file, *frame);
2247 mutex_unlock(&zr->resource_lock);
2249 zr->v4l_sync_tail++;
2254 case VIDIOCMCAPTURE:
2256 struct video_mmap *vmap = arg;
2261 "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
2262 ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
2265 mutex_lock(&zr->resource_lock);
2266 res = v4l_grab(file, vmap);
2267 mutex_unlock(&zr->resource_lock);
2274 struct video_mbuf *vmbuf = arg;
2277 dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
2280 fh->v4l_buffers.num_buffers *
2281 fh->v4l_buffers.buffer_size;
2282 vmbuf->frames = fh->v4l_buffers.num_buffers;
2283 for (i = 0; i < vmbuf->frames; i++) {
2285 i * fh->v4l_buffers.buffer_size;
2288 mutex_lock(&zr->resource_lock);
2290 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2293 "%s: VIDIOCGMBUF - buffers already allocated\n",
2296 goto v4l1reqbuf_unlock_and_return;
2299 if (v4l_fbuffer_alloc(file)) {
2301 goto v4l1reqbuf_unlock_and_return;
2304 /* The next mmap will map the V4L buffers */
2305 fh->map_mode = ZORAN_MAP_MODE_RAW;
2306 v4l1reqbuf_unlock_and_return:
2307 mutex_unlock(&zr->resource_lock);
2315 struct video_unit *vunit = arg;
2317 dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
2319 vunit->video = zr->video_dev->minor;
2320 vunit->vbi = VIDEO_NO_UNIT;
2321 vunit->radio = VIDEO_NO_UNIT;
2322 vunit->audio = VIDEO_NO_UNIT;
2323 vunit->teletext = VIDEO_NO_UNIT;
2330 * RJ: In principal we could support subcaptures for V4L grabbing.
2331 * Not even the famous BTTV driver has them, however.
2332 * If there should be a strong demand, one could consider
2333 * to implement them.
2335 case VIDIOCGCAPTURE:
2337 dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
2343 case VIDIOCSCAPTURE:
2345 dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
2351 case BUZIOC_G_PARAMS:
2353 struct zoran_params *bparams = arg;
2355 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
2357 memset(bparams, 0, sizeof(struct zoran_params));
2358 bparams->major_version = MAJOR_VERSION;
2359 bparams->minor_version = MINOR_VERSION;
2361 mutex_lock(&zr->resource_lock);
2363 bparams->norm = zr->norm;
2364 bparams->input = zr->input;
2366 bparams->decimation = fh->jpg_settings.decimation;
2367 bparams->HorDcm = fh->jpg_settings.HorDcm;
2368 bparams->VerDcm = fh->jpg_settings.VerDcm;
2369 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
2370 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
2371 bparams->img_x = fh->jpg_settings.img_x;
2372 bparams->img_y = fh->jpg_settings.img_y;
2373 bparams->img_width = fh->jpg_settings.img_width;
2374 bparams->img_height = fh->jpg_settings.img_height;
2375 bparams->odd_even = fh->jpg_settings.odd_even;
2377 bparams->quality = fh->jpg_settings.jpg_comp.quality;
2378 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
2379 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2380 memcpy(bparams->APP_data,
2381 fh->jpg_settings.jpg_comp.APP_data,
2382 sizeof(bparams->APP_data));
2383 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
2384 memcpy(bparams->COM_data,
2385 fh->jpg_settings.jpg_comp.COM_data,
2386 sizeof(bparams->COM_data));
2387 bparams->jpeg_markers =
2388 fh->jpg_settings.jpg_comp.jpeg_markers;
2390 mutex_unlock(&zr->resource_lock);
2392 bparams->VFIFO_FB = 0;
2398 case BUZIOC_S_PARAMS:
2400 struct zoran_params *bparams = arg;
2403 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
2405 settings.decimation = bparams->decimation;
2406 settings.HorDcm = bparams->HorDcm;
2407 settings.VerDcm = bparams->VerDcm;
2408 settings.TmpDcm = bparams->TmpDcm;
2409 settings.field_per_buff = bparams->field_per_buff;
2410 settings.img_x = bparams->img_x;
2411 settings.img_y = bparams->img_y;
2412 settings.img_width = bparams->img_width;
2413 settings.img_height = bparams->img_height;
2414 settings.odd_even = bparams->odd_even;
2416 settings.jpg_comp.quality = bparams->quality;
2417 settings.jpg_comp.APPn = bparams->APPn;
2418 settings.jpg_comp.APP_len = bparams->APP_len;
2419 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
2420 sizeof(bparams->APP_data));
2421 settings.jpg_comp.COM_len = bparams->COM_len;
2422 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
2423 sizeof(bparams->COM_data));
2424 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
2426 mutex_lock(&zr->resource_lock);
2428 if (zr->codec_mode != BUZ_MODE_IDLE) {
2431 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
2434 goto sparams_unlock_and_return;
2437 /* Check the params first before overwriting our
2439 if (zoran_check_jpg_settings(zr, &settings)) {
2441 goto sparams_unlock_and_return;
2444 fh->jpg_settings = settings;
2445 sparams_unlock_and_return:
2446 mutex_unlock(&zr->resource_lock);
2452 case BUZIOC_REQBUFS:
2454 struct zoran_requestbuffers *breq = arg;
2459 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
2460 ZR_DEVNAME(zr), breq->count, breq->size);
2462 /* Enforce reasonable lower and upper limits */
2463 if (breq->count < 4)
2464 breq->count = 4; /* Could be choosen smaller */
2465 if (breq->count > jpg_nbufs)
2466 breq->count = jpg_nbufs;
2467 breq->size = PAGE_ALIGN(breq->size);
2468 if (breq->size < 8192)
2469 breq->size = 8192; /* Arbitrary */
2470 /* breq->size is limited by 1 page for the stat_com
2471 * tables to a Maximum of 2 MB */
2472 if (breq->size > jpg_bufsize)
2473 breq->size = jpg_bufsize;
2474 if (fh->jpg_buffers.need_contiguous &&
2475 breq->size > MAX_KMALLOC_MEM)
2476 breq->size = MAX_KMALLOC_MEM;
2478 mutex_lock(&zr->resource_lock);
2480 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2483 "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
2486 goto jpgreqbuf_unlock_and_return;
2489 fh->jpg_buffers.num_buffers = breq->count;
2490 fh->jpg_buffers.buffer_size = breq->size;
2492 if (jpg_fbuffer_alloc(file)) {
2494 goto jpgreqbuf_unlock_and_return;
2497 /* The next mmap will map the MJPEG buffers - could
2498 * also be *_PLAY, but it doesn't matter here */
2499 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2500 jpgreqbuf_unlock_and_return:
2501 mutex_unlock(&zr->resource_lock);
2507 case BUZIOC_QBUF_CAPT:
2509 int *frame = arg, res;
2511 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
2512 ZR_DEVNAME(zr), *frame);
2514 mutex_lock(&zr->resource_lock);
2515 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
2516 mutex_unlock(&zr->resource_lock);
2522 case BUZIOC_QBUF_PLAY:
2524 int *frame = arg, res;
2526 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
2527 ZR_DEVNAME(zr), *frame);
2529 mutex_lock(&zr->resource_lock);
2530 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
2531 mutex_unlock(&zr->resource_lock);
2539 struct zoran_sync *bsync = arg;
2542 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
2544 mutex_lock(&zr->resource_lock);
2545 res = jpg_sync(file, bsync);
2546 mutex_unlock(&zr->resource_lock);
2552 case BUZIOC_G_STATUS:
2554 struct zoran_status *bstat = arg;
2555 int norm, input, status, res = 0;
2557 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
2559 if (zr->codec_mode != BUZ_MODE_IDLE) {
2562 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
2567 input = zr->card.input[bstat->input].muxsel;
2568 norm = VIDEO_MODE_AUTO;
2570 mutex_lock(&zr->resource_lock);
2572 if (zr->codec_mode != BUZ_MODE_IDLE) {
2575 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
2578 goto gstat_unlock_and_return;
2581 decoder_command(zr, DECODER_SET_INPUT, &input);
2582 decoder_command(zr, DECODER_SET_NORM, &norm);
2584 /* sleep 1 second */
2587 /* Get status of video decoder */
2588 decoder_command(zr, DECODER_GET_STATUS, &status);
2590 /* restore previous input and norm */
2591 input = zr->card.input[zr->input].muxsel;
2592 decoder_command(zr, DECODER_SET_INPUT, &input);
2593 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
2594 gstat_unlock_and_return:
2595 mutex_unlock(&zr->resource_lock);
2599 (status & DECODER_STATUS_GOOD) ? 1 : 0;
2600 if (status & DECODER_STATUS_NTSC)
2601 bstat->norm = VIDEO_MODE_NTSC;
2602 else if (status & DECODER_STATUS_SECAM)
2603 bstat->norm = VIDEO_MODE_SECAM;
2605 bstat->norm = VIDEO_MODE_PAL;
2608 (status & DECODER_STATUS_COLOR) ? 1 : 0;
2615 /* The new video4linux2 capture interface - much nicer than video4linux1, since
2616 * it allows for integrating the JPEG capturing calls inside standard v4l2
2619 case VIDIOC_QUERYCAP:
2621 struct v4l2_capability *cap = arg;
2623 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
2625 memset(cap, 0, sizeof(*cap));
2626 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
2627 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
2628 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2629 pci_name(zr->pci_dev));
2631 KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2633 cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2639 case VIDIOC_ENUM_FMT:
2641 struct v4l2_fmtdesc *fmt = arg;
2642 int index = fmt->index, num = -1, i, flag = 0, type =
2645 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
2646 ZR_DEVNAME(zr), fmt->index);
2648 switch (fmt->type) {
2649 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2650 flag = ZORAN_FORMAT_CAPTURE;
2652 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2653 flag = ZORAN_FORMAT_PLAYBACK;
2655 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2656 flag = ZORAN_FORMAT_OVERLAY;
2661 "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
2662 ZR_DEVNAME(zr), fmt->type);
2666 for (i = 0; i < NUM_FORMATS; i++) {
2667 if (zoran_formats[i].flags & flag)
2669 if (num == fmt->index)
2672 if (fmt->index < 0 /* late, but not too late */ ||
2676 memset(fmt, 0, sizeof(*fmt));
2679 strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
2680 fmt->pixelformat = zoran_formats[i].fourcc;
2681 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2682 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2690 struct v4l2_format *fmt = arg;
2691 int type = fmt->type;
2693 dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
2695 memset(fmt, 0, sizeof(*fmt));
2698 switch (fmt->type) {
2699 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2701 mutex_lock(&zr->resource_lock);
2703 fmt->fmt.win.w.left = fh->overlay_settings.x;
2704 fmt->fmt.win.w.top = fh->overlay_settings.y;
2705 fmt->fmt.win.w.width = fh->overlay_settings.width;
2706 fmt->fmt.win.w.height =
2707 fh->overlay_settings.height;
2708 if (fh->overlay_settings.width * 2 >
2710 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2712 fmt->fmt.win.field = V4L2_FIELD_TOP;
2714 mutex_unlock(&zr->resource_lock);
2718 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2719 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2721 mutex_lock(&zr->resource_lock);
2723 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2724 fh->map_mode == ZORAN_MAP_MODE_RAW) {
2726 fmt->fmt.pix.width =
2727 fh->v4l_settings.width;
2728 fmt->fmt.pix.height =
2729 fh->v4l_settings.height;
2730 fmt->fmt.pix.sizeimage =
2731 fh->v4l_settings.bytesperline *
2732 fh->v4l_settings.height;
2733 fmt->fmt.pix.pixelformat =
2734 fh->v4l_settings.format->fourcc;
2735 fmt->fmt.pix.colorspace =
2736 fh->v4l_settings.format->colorspace;
2737 fmt->fmt.pix.bytesperline =
2738 fh->v4l_settings.bytesperline;
2739 if (BUZ_MAX_HEIGHT <
2740 (fh->v4l_settings.height * 2))
2741 fmt->fmt.pix.field =
2742 V4L2_FIELD_INTERLACED;
2744 fmt->fmt.pix.field =
2749 fmt->fmt.pix.width =
2750 fh->jpg_settings.img_width /
2751 fh->jpg_settings.HorDcm;
2752 fmt->fmt.pix.height =
2753 fh->jpg_settings.img_height /
2754 (fh->jpg_settings.VerDcm *
2755 fh->jpg_settings.TmpDcm);
2756 fmt->fmt.pix.sizeimage =
2757 zoran_v4l2_calc_bufsize(&fh->
2759 fmt->fmt.pix.pixelformat =
2761 if (fh->jpg_settings.TmpDcm == 1)
2762 fmt->fmt.pix.field =
2764 odd_even ? V4L2_FIELD_SEQ_BT :
2767 fmt->fmt.pix.field =
2769 odd_even ? V4L2_FIELD_TOP :
2772 fmt->fmt.pix.bytesperline = 0;
2773 fmt->fmt.pix.colorspace =
2774 V4L2_COLORSPACE_SMPTE170M;
2777 mutex_unlock(&zr->resource_lock);
2784 "%s: VIDIOC_G_FMT - unsupported type %d\n",
2785 ZR_DEVNAME(zr), fmt->type);
2794 struct v4l2_format *fmt = arg;
2798 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
2799 ZR_DEVNAME(zr), fmt->type);
2801 switch (fmt->type) {
2802 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2804 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2805 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2806 fmt->fmt.win.w.width,
2807 fmt->fmt.win.w.height,
2808 fmt->fmt.win.clipcount,
2809 fmt->fmt.win.bitmap);
2810 mutex_lock(&zr->resource_lock);
2812 setup_window(file, fmt->fmt.win.w.left,
2814 fmt->fmt.win.w.width,
2815 fmt->fmt.win.w.height,
2816 (struct video_clip __user *)
2818 fmt->fmt.win.clipcount,
2819 fmt->fmt.win.bitmap);
2820 mutex_unlock(&zr->resource_lock);
2824 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2825 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2828 __cpu_to_le32(fmt->fmt.pix.pixelformat);
2829 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2830 fmt->fmt.pix.width, fmt->fmt.pix.height,
2831 fmt->fmt.pix.pixelformat,
2832 (char *) &printformat);
2834 /* we can be requested to do JPEG/raw playback/capture */
2836 (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2837 (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2838 fmt->fmt.pix.pixelformat ==
2839 V4L2_PIX_FMT_MJPEG))) {
2842 "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
2843 ZR_DEVNAME(zr), fmt->type,
2844 fmt->fmt.pix.pixelformat,
2845 (char *) &printformat);
2849 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
2850 mutex_lock(&zr->resource_lock);
2852 settings = fh->jpg_settings;
2854 if (fh->v4l_buffers.allocated ||
2855 fh->jpg_buffers.allocated) {
2858 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2861 goto sfmtjpg_unlock_and_return;
2864 /* we actually need to set 'real' parameters now */
2865 if ((fmt->fmt.pix.height * 2) >
2867 settings.TmpDcm = 1;
2869 settings.TmpDcm = 2;
2870 settings.decimation = 0;
2871 if (fmt->fmt.pix.height <=
2872 fh->jpg_settings.img_height / 2)
2873 settings.VerDcm = 2;
2875 settings.VerDcm = 1;
2876 if (fmt->fmt.pix.width <=
2877 fh->jpg_settings.img_width / 4)
2878 settings.HorDcm = 4;
2879 else if (fmt->fmt.pix.width <=
2880 fh->jpg_settings.img_width / 2)
2881 settings.HorDcm = 2;
2883 settings.HorDcm = 1;
2884 if (settings.TmpDcm == 1)
2885 settings.field_per_buff = 2;
2887 settings.field_per_buff = 1;
2891 zoran_check_jpg_settings(zr,
2893 goto sfmtjpg_unlock_and_return;
2895 /* it's ok, so set them */
2896 fh->jpg_settings = settings;
2898 /* tell the user what we actually did */
2899 fmt->fmt.pix.width =
2900 settings.img_width / settings.HorDcm;
2901 fmt->fmt.pix.height =
2902 settings.img_height * 2 /
2903 (settings.TmpDcm * settings.VerDcm);
2904 if (settings.TmpDcm == 1)
2905 fmt->fmt.pix.field =
2907 odd_even ? V4L2_FIELD_SEQ_TB :
2910 fmt->fmt.pix.field =
2912 odd_even ? V4L2_FIELD_TOP :
2914 fh->jpg_buffers.buffer_size =
2915 zoran_v4l2_calc_bufsize(&fh->
2917 fmt->fmt.pix.bytesperline = 0;
2918 fmt->fmt.pix.sizeimage =
2919 fh->jpg_buffers.buffer_size;
2920 fmt->fmt.pix.colorspace =
2921 V4L2_COLORSPACE_SMPTE170M;
2923 /* we hereby abuse this variable to show that
2924 * we're gonna do mjpeg capture */
2927 V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2928 ZORAN_MAP_MODE_JPG_REC :
2929 ZORAN_MAP_MODE_JPG_PLAY;
2930 sfmtjpg_unlock_and_return:
2931 mutex_unlock(&zr->resource_lock);
2933 for (i = 0; i < NUM_FORMATS; i++)
2934 if (fmt->fmt.pix.pixelformat ==
2935 zoran_formats[i].fourcc)
2937 if (i == NUM_FORMATS) {
2940 "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
2942 fmt->fmt.pix.pixelformat,
2943 (char *) &printformat);
2946 mutex_lock(&zr->resource_lock);
2947 if (fh->jpg_buffers.allocated ||
2948 (fh->v4l_buffers.allocated &&
2949 fh->v4l_buffers.active !=
2953 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2956 goto sfmtv4l_unlock_and_return;
2958 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2959 fmt->fmt.pix.height =
2961 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2962 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2965 zoran_v4l_set_format(file,
2972 goto sfmtv4l_unlock_and_return;
2974 /* tell the user the
2975 * results/missing stuff */
2976 fmt->fmt.pix.bytesperline =
2977 fh->v4l_settings.bytesperline;
2978 fmt->fmt.pix.sizeimage =
2979 fh->v4l_settings.height *
2980 fh->v4l_settings.bytesperline;
2981 fmt->fmt.pix.colorspace =
2982 fh->v4l_settings.format->colorspace;
2983 if (BUZ_MAX_HEIGHT <
2984 (fh->v4l_settings.height * 2))
2985 fmt->fmt.pix.field =
2986 V4L2_FIELD_INTERLACED;
2988 fmt->fmt.pix.field =
2991 fh->map_mode = ZORAN_MAP_MODE_RAW;
2992 sfmtv4l_unlock_and_return:
2993 mutex_unlock(&zr->resource_lock);
2999 dprintk(3, "unsupported\n");
3002 "%s: VIDIOC_S_FMT - unsupported type %d\n",
3003 ZR_DEVNAME(zr), fmt->type);
3013 struct v4l2_framebuffer *fb = arg;
3015 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
3017 memset(fb, 0, sizeof(*fb));
3018 mutex_lock(&zr->resource_lock);
3019 fb->base = zr->buffer.base;
3020 fb->fmt.width = zr->buffer.width;
3021 fb->fmt.height = zr->buffer.height;
3022 if (zr->overlay_settings.format) {
3023 fb->fmt.pixelformat =
3024 fh->overlay_settings.format->fourcc;
3026 fb->fmt.bytesperline = zr->buffer.bytesperline;
3027 mutex_unlock(&zr->resource_lock);
3028 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
3029 fb->fmt.field = V4L2_FIELD_INTERLACED;
3030 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
3031 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3040 struct v4l2_framebuffer *fb = arg;
3041 __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
3045 "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
3046 ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
3047 fb->fmt.bytesperline, fb->fmt.pixelformat,
3048 (char *) &printformat);
3050 for (i = 0; i < NUM_FORMATS; i++)
3051 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
3053 if (i == NUM_FORMATS) {
3056 "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
3057 ZR_DEVNAME(zr), fb->fmt.pixelformat,
3058 (char *) &printformat);
3062 mutex_lock(&zr->resource_lock);
3064 setup_fbuffer(file, fb->base, &zoran_formats[i],
3065 fb->fmt.width, fb->fmt.height,
3066 fb->fmt.bytesperline);
3067 mutex_unlock(&zr->resource_lock);
3073 case VIDIOC_OVERLAY:
3077 dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
3078 ZR_DEVNAME(zr), *on);
3080 mutex_lock(&zr->resource_lock);
3081 res = setup_overlay(file, *on);
3082 mutex_unlock(&zr->resource_lock);
3088 case VIDIOC_REQBUFS:
3090 struct v4l2_requestbuffers *req = arg;
3093 dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
3094 ZR_DEVNAME(zr), req->type);
3096 if (req->memory != V4L2_MEMORY_MMAP) {
3099 "%s: only MEMORY_MMAP capture is supported, not %d\n",
3100 ZR_DEVNAME(zr), req->memory);
3104 mutex_lock(&zr->resource_lock);
3106 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
3109 "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
3112 goto v4l2reqbuf_unlock_and_return;
3115 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
3116 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3118 /* control user input */
3121 if (req->count > v4l_nbufs)
3122 req->count = v4l_nbufs;
3123 fh->v4l_buffers.num_buffers = req->count;
3125 if (v4l_fbuffer_alloc(file)) {
3127 goto v4l2reqbuf_unlock_and_return;
3130 /* The next mmap will map the V4L buffers */
3131 fh->map_mode = ZORAN_MAP_MODE_RAW;
3133 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
3134 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3136 /* we need to calculate size ourselves now */
3139 if (req->count > jpg_nbufs)
3140 req->count = jpg_nbufs;
3141 fh->jpg_buffers.num_buffers = req->count;
3142 fh->jpg_buffers.buffer_size =
3143 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3145 if (jpg_fbuffer_alloc(file)) {
3147 goto v4l2reqbuf_unlock_and_return;
3150 /* The next mmap will map the MJPEG buffers */
3151 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
3152 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
3154 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
3159 "%s: VIDIOC_REQBUFS - unknown type %d\n",
3160 ZR_DEVNAME(zr), req->type);
3162 goto v4l2reqbuf_unlock_and_return;
3164 v4l2reqbuf_unlock_and_return:
3165 mutex_unlock(&zr->resource_lock);
3171 case VIDIOC_QUERYBUF:
3173 struct v4l2_buffer *buf = arg;
3174 __u32 type = buf->type;
3175 int index = buf->index, res;
3179 "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
3180 ZR_DEVNAME(zr), buf->index, buf->type);
3182 memset(buf, 0, sizeof(*buf));
3186 mutex_lock(&zr->resource_lock);
3187 res = zoran_v4l2_buffer_status(file, buf, buf->index);
3188 mutex_unlock(&zr->resource_lock);
3196 struct v4l2_buffer *buf = arg;
3197 int res = 0, codec_mode, buf_type;
3200 KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
3201 ZR_DEVNAME(zr), buf->type, buf->index);
3203 mutex_lock(&zr->resource_lock);
3205 switch (fh->map_mode) {
3206 case ZORAN_MAP_MODE_RAW:
3207 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3210 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3211 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3213 goto qbuf_unlock_and_return;
3216 res = zoran_v4l_queue_frame(file, buf->index);
3218 goto qbuf_unlock_and_return;
3219 if (!zr->v4l_memgrab_active &&
3220 fh->v4l_buffers.active == ZORAN_LOCKED)
3221 zr36057_set_memgrab(zr, 1);
3224 case ZORAN_MAP_MODE_JPG_REC:
3225 case ZORAN_MAP_MODE_JPG_PLAY:
3226 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3227 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3228 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
3230 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3231 codec_mode = BUZ_MODE_MOTION_COMPRESS;
3234 if (buf->type != buf_type) {
3237 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3238 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3240 goto qbuf_unlock_and_return;
3244 zoran_jpg_queue_frame(file, buf->index,
3247 goto qbuf_unlock_and_return;
3248 if (zr->codec_mode == BUZ_MODE_IDLE &&
3249 fh->jpg_buffers.active == ZORAN_LOCKED) {
3250 zr36057_enable_jpg(zr, codec_mode);
3257 "%s: VIDIOC_QBUF - unsupported type %d\n",
3258 ZR_DEVNAME(zr), buf->type);
3260 goto qbuf_unlock_and_return;
3262 qbuf_unlock_and_return:
3263 mutex_unlock(&zr->resource_lock);
3271 struct v4l2_buffer *buf = arg;
3272 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
3274 dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
3275 ZR_DEVNAME(zr), buf->type);
3277 mutex_lock(&zr->resource_lock);
3279 switch (fh->map_mode) {
3280 case ZORAN_MAP_MODE_RAW:
3281 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3284 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3285 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3287 goto dqbuf_unlock_and_return;
3290 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3291 if (file->f_flags & O_NONBLOCK &&
3292 zr->v4l_buffers.buffer[num].state !=
3295 goto dqbuf_unlock_and_return;
3297 res = v4l_sync(file, num);
3299 goto dqbuf_unlock_and_return;
3301 zr->v4l_sync_tail++;
3302 res = zoran_v4l2_buffer_status(file, buf, num);
3305 case ZORAN_MAP_MODE_JPG_REC:
3306 case ZORAN_MAP_MODE_JPG_PLAY:
3308 struct zoran_sync bs;
3310 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
3311 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3313 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3315 if (buf->type != buf_type) {
3318 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3319 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3321 goto dqbuf_unlock_and_return;
3326 jpg_que_tail & BUZ_MASK_FRAME];
3328 if (file->f_flags & O_NONBLOCK &&
3329 zr->jpg_buffers.buffer[num].state !=
3332 goto dqbuf_unlock_and_return;
3334 res = jpg_sync(file, &bs);
3336 goto dqbuf_unlock_and_return;
3338 zoran_v4l2_buffer_status(file, buf, bs.frame);
3345 "%s: VIDIOC_DQBUF - unsupported type %d\n",
3346 ZR_DEVNAME(zr), buf->type);
3348 goto dqbuf_unlock_and_return;
3350 dqbuf_unlock_and_return:
3351 mutex_unlock(&zr->resource_lock);
3357 case VIDIOC_STREAMON:
3361 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
3363 mutex_lock(&zr->resource_lock);
3365 switch (fh->map_mode) {
3366 case ZORAN_MAP_MODE_RAW: /* raw capture */
3367 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
3368 fh->v4l_buffers.active != ZORAN_ACTIVE) {
3370 goto strmon_unlock_and_return;
3373 zr->v4l_buffers.active = fh->v4l_buffers.active =
3375 zr->v4l_settings = fh->v4l_settings;
3377 zr->v4l_sync_tail = zr->v4l_pend_tail;
3378 if (!zr->v4l_memgrab_active &&
3379 zr->v4l_pend_head != zr->v4l_pend_tail) {
3380 zr36057_set_memgrab(zr, 1);
3384 case ZORAN_MAP_MODE_JPG_REC:
3385 case ZORAN_MAP_MODE_JPG_PLAY:
3386 /* what is the codec mode right now? */
3387 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
3388 fh->jpg_buffers.active != ZORAN_ACTIVE) {
3390 goto strmon_unlock_and_return;
3393 zr->jpg_buffers.active = fh->jpg_buffers.active =
3396 if (zr->jpg_que_head != zr->jpg_que_tail) {
3397 /* Start the jpeg codec when the first frame is queued */
3405 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
3406 ZR_DEVNAME(zr), fh->map_mode);
3408 goto strmon_unlock_and_return;
3410 strmon_unlock_and_return:
3411 mutex_unlock(&zr->resource_lock);
3417 case VIDIOC_STREAMOFF:
3421 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
3423 mutex_lock(&zr->resource_lock);
3425 switch (fh->map_mode) {
3426 case ZORAN_MAP_MODE_RAW: /* raw capture */
3427 if (fh->v4l_buffers.active == ZORAN_FREE &&
3428 zr->v4l_buffers.active != ZORAN_FREE) {
3429 res = -EPERM; /* stay off other's settings! */
3430 goto strmoff_unlock_and_return;
3432 if (zr->v4l_buffers.active == ZORAN_FREE)
3433 goto strmoff_unlock_and_return;
3435 /* unload capture */
3436 if (zr->v4l_memgrab_active) {
3437 unsigned long flags;
3439 spin_lock_irqsave(&zr->spinlock, flags);
3440 zr36057_set_memgrab(zr, 0);
3441 spin_unlock_irqrestore(&zr->spinlock, flags);
3444 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3445 zr->v4l_buffers.buffer[i].state =
3447 fh->v4l_buffers = zr->v4l_buffers;
3449 zr->v4l_buffers.active = fh->v4l_buffers.active =
3452 zr->v4l_grab_seq = 0;
3453 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
3454 zr->v4l_sync_tail = 0;
3458 case ZORAN_MAP_MODE_JPG_REC:
3459 case ZORAN_MAP_MODE_JPG_PLAY:
3460 if (fh->jpg_buffers.active == ZORAN_FREE &&
3461 zr->jpg_buffers.active != ZORAN_FREE) {
3462 res = -EPERM; /* stay off other's settings! */
3463 goto strmoff_unlock_and_return;
3465 if (zr->jpg_buffers.active == ZORAN_FREE)
3466 goto strmoff_unlock_and_return;
3471 ZORAN_MAP_MODE_JPG_REC) ?
3472 BUZ_MODE_MOTION_COMPRESS :
3473 BUZ_MODE_MOTION_DECOMPRESS);
3475 goto strmoff_unlock_and_return;
3480 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
3481 ZR_DEVNAME(zr), fh->map_mode);
3483 goto strmoff_unlock_and_return;
3485 strmoff_unlock_and_return:
3486 mutex_unlock(&zr->resource_lock);
3492 case VIDIOC_QUERYCTRL:
3494 struct v4l2_queryctrl *ctrl = arg;
3496 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
3497 ZR_DEVNAME(zr), ctrl->id);
3499 /* we only support hue/saturation/contrast/brightness */
3500 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3501 ctrl->id > V4L2_CID_HUE)
3505 memset(ctrl, 0, sizeof(*ctrl));
3510 case V4L2_CID_BRIGHTNESS:
3511 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
3513 case V4L2_CID_CONTRAST:
3514 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
3516 case V4L2_CID_SATURATION:
3517 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
3520 strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
3525 ctrl->maximum = 65535;
3527 ctrl->default_value = 32768;
3528 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
3536 struct v4l2_control *ctrl = arg;
3538 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
3539 ZR_DEVNAME(zr), ctrl->id);
3541 /* we only support hue/saturation/contrast/brightness */
3542 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3543 ctrl->id > V4L2_CID_HUE)
3546 mutex_lock(&zr->resource_lock);
3548 case V4L2_CID_BRIGHTNESS:
3549 ctrl->value = zr->brightness;
3551 case V4L2_CID_CONTRAST:
3552 ctrl->value = zr->contrast;
3554 case V4L2_CID_SATURATION:
3555 ctrl->value = zr->saturation;
3558 ctrl->value = zr->hue;
3561 mutex_unlock(&zr->resource_lock);
3569 struct v4l2_control *ctrl = arg;
3570 struct video_picture pict;
3572 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
3573 ZR_DEVNAME(zr), ctrl->id);
3575 /* we only support hue/saturation/contrast/brightness */
3576 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3577 ctrl->id > V4L2_CID_HUE)
3580 if (ctrl->value < 0 || ctrl->value > 65535) {
3583 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
3584 ZR_DEVNAME(zr), ctrl->value, ctrl->id);
3588 mutex_lock(&zr->resource_lock);
3590 case V4L2_CID_BRIGHTNESS:
3591 zr->brightness = ctrl->value;
3593 case V4L2_CID_CONTRAST:
3594 zr->contrast = ctrl->value;
3596 case V4L2_CID_SATURATION:
3597 zr->saturation = ctrl->value;
3600 zr->hue = ctrl->value;
3603 pict.brightness = zr->brightness;
3604 pict.contrast = zr->contrast;
3605 pict.colour = zr->saturation;
3608 decoder_command(zr, DECODER_SET_PICTURE, &pict);
3610 mutex_unlock(&zr->resource_lock);
3616 case VIDIOC_ENUMSTD:
3618 struct v4l2_standard *std = arg;
3620 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
3621 ZR_DEVNAME(zr), std->index);
3623 if (std->index < 0 || std->index >= (zr->card.norms + 1))
3626 int id = std->index;
3627 memset(std, 0, sizeof(*std));
3631 if (std->index == zr->card.norms) {
3632 /* if we have autodetect, ... */
3633 struct video_decoder_capability caps;
3634 decoder_command(zr, DECODER_GET_CAPABILITIES,
3636 if (caps.flags & VIDEO_DECODER_AUTO) {
3637 std->id = V4L2_STD_ALL;
3638 strncpy(std->name, "Autodetect", sizeof(std->name)-1);
3643 switch (std->index) {
3645 std->id = V4L2_STD_PAL;
3646 strncpy(std->name, "PAL", sizeof(std->name)-1);
3647 std->frameperiod.numerator = 1;
3648 std->frameperiod.denominator = 25;
3649 std->framelines = zr->card.tvn[0]->Ht;
3652 std->id = V4L2_STD_NTSC;
3653 strncpy(std->name, "NTSC", sizeof(std->name)-1);
3654 std->frameperiod.numerator = 1001;
3655 std->frameperiod.denominator = 30000;
3656 std->framelines = zr->card.tvn[1]->Ht;
3659 std->id = V4L2_STD_SECAM;
3660 strncpy(std->name, "SECAM", sizeof(std->name)-1);
3661 std->frameperiod.numerator = 1;
3662 std->frameperiod.denominator = 25;
3663 std->framelines = zr->card.tvn[2]->Ht;
3673 v4l2_std_id *std = arg;
3676 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
3678 mutex_lock(&zr->resource_lock);
3680 mutex_unlock(&zr->resource_lock);
3683 case VIDEO_MODE_PAL:
3684 *std = V4L2_STD_PAL;
3686 case VIDEO_MODE_NTSC:
3687 *std = V4L2_STD_NTSC;
3689 case VIDEO_MODE_SECAM:
3690 *std = V4L2_STD_SECAM;
3700 int norm = -1, res = 0;
3701 v4l2_std_id *std = arg;
3703 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
3704 ZR_DEVNAME(zr), (unsigned long long)*std);
3706 if ((*std & V4L2_STD_PAL) && !(*std & ~V4L2_STD_PAL))
3707 norm = VIDEO_MODE_PAL;
3708 else if ((*std & V4L2_STD_NTSC) && !(*std & ~V4L2_STD_NTSC))
3709 norm = VIDEO_MODE_NTSC;
3710 else if ((*std & V4L2_STD_SECAM) && !(*std & ~V4L2_STD_SECAM))
3711 norm = VIDEO_MODE_SECAM;
3712 else if (*std == V4L2_STD_ALL)
3713 norm = VIDEO_MODE_AUTO;
3717 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3718 ZR_DEVNAME(zr), (unsigned long long)*std);
3722 mutex_lock(&zr->resource_lock);
3723 if ((res = zoran_set_norm(zr, norm)))
3724 goto sstd_unlock_and_return;
3726 res = wait_grab_pending(zr);
3727 sstd_unlock_and_return:
3728 mutex_unlock(&zr->resource_lock);
3733 case VIDIOC_ENUMINPUT:
3735 struct v4l2_input *inp = arg;
3738 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
3739 ZR_DEVNAME(zr), inp->index);
3741 if (inp->index < 0 || inp->index >= zr->card.inputs)
3744 int id = inp->index;
3745 memset(inp, 0, sizeof(*inp));
3749 strncpy(inp->name, zr->card.input[inp->index].name,
3750 sizeof(inp->name) - 1);
3751 inp->type = V4L2_INPUT_TYPE_CAMERA;
3752 inp->std = V4L2_STD_ALL;
3754 /* Get status of video decoder */
3755 mutex_lock(&zr->resource_lock);
3756 decoder_command(zr, DECODER_GET_STATUS, &status);
3757 mutex_unlock(&zr->resource_lock);
3759 if (!(status & DECODER_STATUS_GOOD)) {
3760 inp->status |= V4L2_IN_ST_NO_POWER;
3761 inp->status |= V4L2_IN_ST_NO_SIGNAL;
3763 if (!(status & DECODER_STATUS_COLOR))
3764 inp->status |= V4L2_IN_ST_NO_COLOR;
3770 case VIDIOC_G_INPUT:
3774 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
3776 mutex_lock(&zr->resource_lock);
3778 mutex_unlock(&zr->resource_lock);
3784 case VIDIOC_S_INPUT:
3786 int *input = arg, res = 0;
3788 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
3789 ZR_DEVNAME(zr), *input);
3791 mutex_lock(&zr->resource_lock);
3792 if ((res = zoran_set_input(zr, *input)))
3793 goto sinput_unlock_and_return;
3795 /* Make sure the changes come into effect */
3796 res = wait_grab_pending(zr);
3797 sinput_unlock_and_return:
3798 mutex_unlock(&zr->resource_lock);
3803 case VIDIOC_ENUMOUTPUT:
3805 struct v4l2_output *outp = arg;
3807 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
3808 ZR_DEVNAME(zr), outp->index);
3810 if (outp->index != 0)
3813 memset(outp, 0, sizeof(*outp));
3815 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3816 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
3822 case VIDIOC_G_OUTPUT:
3826 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
3834 case VIDIOC_S_OUTPUT:
3838 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
3839 ZR_DEVNAME(zr), *output);
3848 /* cropping (sub-frame capture) */
3849 case VIDIOC_CROPCAP:
3851 struct v4l2_cropcap *cropcap = arg;
3852 int type = cropcap->type, res = 0;
3854 dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
3855 ZR_DEVNAME(zr), cropcap->type);
3857 memset(cropcap, 0, sizeof(*cropcap));
3858 cropcap->type = type;
3860 mutex_lock(&zr->resource_lock);
3862 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3863 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3864 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3867 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3870 goto cropcap_unlock_and_return;
3873 cropcap->bounds.top = cropcap->bounds.left = 0;
3874 cropcap->bounds.width = BUZ_MAX_WIDTH;
3875 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3876 cropcap->defrect.top = cropcap->defrect.left = 0;
3877 cropcap->defrect.width = BUZ_MIN_WIDTH;
3878 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3879 cropcap_unlock_and_return:
3880 mutex_unlock(&zr->resource_lock);
3887 struct v4l2_crop *crop = arg;
3888 int type = crop->type, res = 0;
3890 dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
3891 ZR_DEVNAME(zr), crop->type);
3893 memset(crop, 0, sizeof(*crop));
3896 mutex_lock(&zr->resource_lock);
3898 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3899 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3900 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3903 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3906 goto gcrop_unlock_and_return;
3909 crop->c.top = fh->jpg_settings.img_y;
3910 crop->c.left = fh->jpg_settings.img_x;
3911 crop->c.width = fh->jpg_settings.img_width;
3912 crop->c.height = fh->jpg_settings.img_height;
3914 gcrop_unlock_and_return:
3915 mutex_unlock(&zr->resource_lock);
3923 struct v4l2_crop *crop = arg;
3926 settings = fh->jpg_settings;
3930 "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
3931 ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
3932 crop->c.width, crop->c.height);
3934 mutex_lock(&zr->resource_lock);
3936 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3939 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3942 goto scrop_unlock_and_return;
3945 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3946 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3947 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3950 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3953 goto scrop_unlock_and_return;
3956 /* move into a form that we understand */
3957 settings.img_x = crop->c.left;
3958 settings.img_y = crop->c.top;
3959 settings.img_width = crop->c.width;
3960 settings.img_height = crop->c.height;
3962 /* check validity */
3963 if ((res = zoran_check_jpg_settings(zr, &settings)))
3964 goto scrop_unlock_and_return;
3967 fh->jpg_settings = settings;
3969 scrop_unlock_and_return:
3970 mutex_unlock(&zr->resource_lock);
3975 case VIDIOC_G_JPEGCOMP:
3977 struct v4l2_jpegcompression *params = arg;
3979 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
3982 memset(params, 0, sizeof(*params));
3984 mutex_lock(&zr->resource_lock);
3986 params->quality = fh->jpg_settings.jpg_comp.quality;
3987 params->APPn = fh->jpg_settings.jpg_comp.APPn;
3988 memcpy(params->APP_data,
3989 fh->jpg_settings.jpg_comp.APP_data,
3990 fh->jpg_settings.jpg_comp.APP_len);
3991 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
3992 memcpy(params->COM_data,
3993 fh->jpg_settings.jpg_comp.COM_data,
3994 fh->jpg_settings.jpg_comp.COM_len);
3995 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
3996 params->jpeg_markers =
3997 fh->jpg_settings.jpg_comp.jpeg_markers;
3999 mutex_unlock(&zr->resource_lock);
4005 case VIDIOC_S_JPEGCOMP:
4007 struct v4l2_jpegcompression *params = arg;
4010 settings = fh->jpg_settings;
4014 "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
4015 ZR_DEVNAME(zr), params->quality, params->APPn,
4016 params->APP_len, params->COM_len);
4018 settings.jpg_comp = *params;
4020 mutex_lock(&zr->resource_lock);
4022 if (fh->v4l_buffers.active != ZORAN_FREE ||
4023 fh->jpg_buffers.active != ZORAN_FREE) {
4026 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
4029 goto sjpegc_unlock_and_return;
4032 if ((res = zoran_check_jpg_settings(zr, &settings)))
4033 goto sjpegc_unlock_and_return;
4034 if (!fh->jpg_buffers.allocated)
4035 fh->jpg_buffers.buffer_size =
4036 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
4037 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
4038 sjpegc_unlock_and_return:
4039 mutex_unlock(&zr->resource_lock);
4045 case VIDIOC_QUERYSTD: /* why is this useful? */
4047 v4l2_std_id *std = arg;
4050 KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
4051 ZR_DEVNAME(zr), (unsigned long long)*std);
4053 if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
4054 *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
4055 zr->card.norms == 3)) {
4063 case VIDIOC_TRY_FMT:
4065 struct v4l2_format *fmt = arg;
4068 dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
4069 ZR_DEVNAME(zr), fmt->type);
4071 switch (fmt->type) {
4072 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4073 mutex_lock(&zr->resource_lock);
4075 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
4076 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
4077 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
4078 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
4079 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
4080 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
4081 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
4082 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
4084 mutex_unlock(&zr->resource_lock);
4087 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4088 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4089 if (fmt->fmt.pix.bytesperline > 0)
4092 mutex_lock(&zr->resource_lock);
4094 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
4095 settings = fh->jpg_settings;
4097 /* we actually need to set 'real' parameters now */
4098 if ((fmt->fmt.pix.height * 2) >
4100 settings.TmpDcm = 1;
4102 settings.TmpDcm = 2;
4103 settings.decimation = 0;
4104 if (fmt->fmt.pix.height <=
4105 fh->jpg_settings.img_height / 2)
4106 settings.VerDcm = 2;
4108 settings.VerDcm = 1;
4109 if (fmt->fmt.pix.width <=
4110 fh->jpg_settings.img_width / 4)
4111 settings.HorDcm = 4;
4112 else if (fmt->fmt.pix.width <=
4113 fh->jpg_settings.img_width / 2)
4114 settings.HorDcm = 2;
4116 settings.HorDcm = 1;
4117 if (settings.TmpDcm == 1)
4118 settings.field_per_buff = 2;
4120 settings.field_per_buff = 1;
4124 zoran_check_jpg_settings(zr,
4126 goto tryfmt_unlock_and_return;
4128 /* tell the user what we actually did */
4129 fmt->fmt.pix.width =
4130 settings.img_width / settings.HorDcm;
4131 fmt->fmt.pix.height =
4132 settings.img_height * 2 /
4133 (settings.TmpDcm * settings.VerDcm);
4134 if (settings.TmpDcm == 1)
4135 fmt->fmt.pix.field =
4137 odd_even ? V4L2_FIELD_SEQ_TB :
4140 fmt->fmt.pix.field =
4142 odd_even ? V4L2_FIELD_TOP :
4145 fmt->fmt.pix.sizeimage =
4146 zoran_v4l2_calc_bufsize(&settings);
4147 } else if (fmt->type ==
4148 V4L2_BUF_TYPE_VIDEO_CAPTURE) {
4151 for (i = 0; i < NUM_FORMATS; i++)
4152 if (zoran_formats[i].fourcc ==
4153 fmt->fmt.pix.pixelformat)
4155 if (i == NUM_FORMATS) {
4157 goto tryfmt_unlock_and_return;
4160 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
4161 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
4162 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
4163 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
4164 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
4165 fmt->fmt.pix.height =
4167 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
4168 fmt->fmt.pix.height =
4172 goto tryfmt_unlock_and_return;
4174 tryfmt_unlock_and_return:
4175 mutex_unlock(&zr->resource_lock);
4189 dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
4190 ZR_DEVNAME(zr), cmd);
4191 return -ENOIOCTLCMD;
4200 zoran_ioctl (struct inode *inode,
4205 return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
4209 zoran_poll (struct file *file,
4212 struct zoran_fh *fh = file->private_data;
4213 struct zoran *zr = fh->zr;
4215 unsigned long flags;
4217 /* we should check whether buffers are ready to be synced on
4218 * (w/o waits - O_NONBLOCK) here
4219 * if ready for read (sync), return POLLIN|POLLRDNORM,
4220 * if ready for write (sync), return POLLOUT|POLLWRNORM,
4221 * if error, return POLLERR,
4222 * if no buffers queued or so, return POLLNVAL
4225 mutex_lock(&zr->resource_lock);
4227 switch (fh->map_mode) {
4228 case ZORAN_MAP_MODE_RAW:
4229 poll_wait(file, &zr->v4l_capq, wait);
4230 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
4232 spin_lock_irqsave(&zr->spinlock, flags);
4235 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
4236 ZR_DEVNAME(zr), __func__,
4237 "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
4238 "UPMD"[zr->v4l_buffers.buffer[frame].state],
4239 zr->v4l_pend_tail, zr->v4l_pend_head);
4240 /* Process is the one capturing? */
4241 if (fh->v4l_buffers.active != ZORAN_FREE &&
4242 /* Buffer ready to DQBUF? */
4243 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
4244 res = POLLIN | POLLRDNORM;
4245 spin_unlock_irqrestore(&zr->spinlock, flags);
4249 case ZORAN_MAP_MODE_JPG_REC:
4250 case ZORAN_MAP_MODE_JPG_PLAY:
4251 poll_wait(file, &zr->jpg_capq, wait);
4252 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
4254 spin_lock_irqsave(&zr->spinlock, flags);
4257 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
4258 ZR_DEVNAME(zr), __func__,
4259 "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
4260 "UPMD"[zr->jpg_buffers.buffer[frame].state],
4261 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
4262 if (fh->jpg_buffers.active != ZORAN_FREE &&
4263 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
4264 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
4265 res = POLLIN | POLLRDNORM;
4267 res = POLLOUT | POLLWRNORM;
4269 spin_unlock_irqrestore(&zr->spinlock, flags);
4276 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
4277 ZR_DEVNAME(zr), fh->map_mode);
4281 mutex_unlock(&zr->resource_lock);
4288 * This maps the buffers to user space.
4290 * Depending on the state of fh->map_mode
4291 * the V4L or the MJPEG buffers are mapped
4292 * per buffer or all together
4294 * Note that we need to connect to some
4295 * unmap signal event to unmap the de-allocate
4296 * the buffer accordingly (zoran_vm_close())
4300 zoran_vm_open (struct vm_area_struct *vma)
4302 struct zoran_mapping *map = vma->vm_private_data;
4308 zoran_vm_close (struct vm_area_struct *vma)
4310 struct zoran_mapping *map = vma->vm_private_data;
4311 struct file *file = map->file;
4312 struct zoran_fh *fh = file->private_data;
4313 struct zoran *zr = fh->zr;
4317 if (map->count == 0) {
4318 switch (fh->map_mode) {
4319 case ZORAN_MAP_MODE_JPG_REC:
4320 case ZORAN_MAP_MODE_JPG_PLAY:
4322 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
4325 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
4326 if (fh->jpg_buffers.buffer[i].map == map) {
4327 fh->jpg_buffers.buffer[i].map =
4333 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
4334 if (fh->jpg_buffers.buffer[i].map)
4336 if (i == fh->jpg_buffers.num_buffers) {
4337 mutex_lock(&zr->resource_lock);
4339 if (fh->jpg_buffers.active != ZORAN_FREE) {
4340 jpg_qbuf(file, -1, zr->codec_mode);
4341 zr->jpg_buffers.allocated = 0;
4342 zr->jpg_buffers.active =
4343 fh->jpg_buffers.active =
4346 //jpg_fbuffer_free(file);
4347 fh->jpg_buffers.allocated = 0;
4348 fh->jpg_buffers.ready_to_be_freed = 1;
4350 mutex_unlock(&zr->resource_lock);
4355 case ZORAN_MAP_MODE_RAW:
4357 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
4360 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
4361 if (fh->v4l_buffers.buffer[i].map == map) {
4363 fh->v4l_buffers.buffer[i].map =
4369 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
4370 if (fh->v4l_buffers.buffer[i].map)
4372 if (i == fh->v4l_buffers.num_buffers) {
4373 mutex_lock(&zr->resource_lock);
4375 if (fh->v4l_buffers.active != ZORAN_FREE) {
4376 unsigned long flags;
4378 spin_lock_irqsave(&zr->spinlock, flags);
4379 zr36057_set_memgrab(zr, 0);
4380 zr->v4l_buffers.allocated = 0;
4381 zr->v4l_buffers.active =
4382 fh->v4l_buffers.active =
4384 spin_unlock_irqrestore(&zr->spinlock, flags);
4386 //v4l_fbuffer_free(file);
4387 fh->v4l_buffers.allocated = 0;
4388 fh->v4l_buffers.ready_to_be_freed = 1;
4390 mutex_unlock(&zr->resource_lock);
4397 "%s: munmap() - internal error - unknown map mode %d\n",
4398 ZR_DEVNAME(zr), fh->map_mode);
4405 static struct vm_operations_struct zoran_vm_ops = {
4406 .open = zoran_vm_open,
4407 .close = zoran_vm_close,
4411 zoran_mmap (struct file *file,
4412 struct vm_area_struct *vma)
4414 struct zoran_fh *fh = file->private_data;
4415 struct zoran *zr = fh->zr;
4416 unsigned long size = (vma->vm_end - vma->vm_start);
4417 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4419 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
4421 struct zoran_mapping *map;
4425 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
4427 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
4428 vma->vm_start, vma->vm_end, size);
4430 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
4431 !(vma->vm_flags & VM_WRITE)) {
4434 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
4439 switch (fh->map_mode) {
4441 case ZORAN_MAP_MODE_JPG_REC:
4442 case ZORAN_MAP_MODE_JPG_PLAY:
4445 mutex_lock(&zr->resource_lock);
4447 /* Map the MJPEG buffers */
4448 if (!fh->jpg_buffers.allocated) {
4451 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
4454 goto jpg_mmap_unlock_and_return;
4457 first = offset / fh->jpg_buffers.buffer_size;
4458 last = first - 1 + size / fh->jpg_buffers.buffer_size;
4459 if (offset % fh->jpg_buffers.buffer_size != 0 ||
4460 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
4461 last < 0 || first >= fh->jpg_buffers.num_buffers ||
4462 last >= fh->jpg_buffers.num_buffers) {
4465 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4466 ZR_DEVNAME(zr), offset, size,
4467 fh->jpg_buffers.buffer_size,
4468 fh->jpg_buffers.num_buffers);
4470 goto jpg_mmap_unlock_and_return;
4472 for (i = first; i <= last; i++) {
4473 if (fh->jpg_buffers.buffer[i].map) {
4476 "%s: mmap(MJPEG) - buffer %d already mapped\n",
4479 goto jpg_mmap_unlock_and_return;
4483 /* map these buffers (v4l_buffers[i]) */
4484 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4487 goto jpg_mmap_unlock_and_return;
4492 vma->vm_ops = &zoran_vm_ops;
4493 vma->vm_flags |= VM_DONTEXPAND;
4494 vma->vm_private_data = map;
4496 for (i = first; i <= last; i++) {
4498 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
4501 (le32_to_cpu(fh->jpg_buffers.buffer[i].
4502 frag_tab[2 * j + 1]) & ~1) << 1;
4507 le32_to_cpu(fh->jpg_buffers.
4508 buffer[i].frag_tab[2 * j]);
4509 /* should just be pos on i386 */
4510 page = virt_to_phys(bus_to_virt(pos))
4512 if (remap_pfn_range(vma, start, page,
4513 todo, PAGE_SHARED)) {
4516 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
4519 goto jpg_mmap_unlock_and_return;
4525 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
4526 frag_tab[2 * j + 1]) & 1)
4527 break; /* was last fragment */
4529 fh->jpg_buffers.buffer[i].map = map;
4534 jpg_mmap_unlock_and_return:
4535 mutex_unlock(&zr->resource_lock);
4539 case ZORAN_MAP_MODE_RAW:
4541 mutex_lock(&zr->resource_lock);
4543 /* Map the V4L buffers */
4544 if (!fh->v4l_buffers.allocated) {
4547 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
4550 goto v4l_mmap_unlock_and_return;
4553 first = offset / fh->v4l_buffers.buffer_size;
4554 last = first - 1 + size / fh->v4l_buffers.buffer_size;
4555 if (offset % fh->v4l_buffers.buffer_size != 0 ||
4556 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
4557 last < 0 || first >= fh->v4l_buffers.num_buffers ||
4558 last >= fh->v4l_buffers.buffer_size) {
4561 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4562 ZR_DEVNAME(zr), offset, size,
4563 fh->v4l_buffers.buffer_size,
4564 fh->v4l_buffers.num_buffers);
4566 goto v4l_mmap_unlock_and_return;
4568 for (i = first; i <= last; i++) {
4569 if (fh->v4l_buffers.buffer[i].map) {
4572 "%s: mmap(V4L) - buffer %d already mapped\n",
4575 goto v4l_mmap_unlock_and_return;
4579 /* map these buffers (v4l_buffers[i]) */
4580 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4583 goto v4l_mmap_unlock_and_return;
4588 vma->vm_ops = &zoran_vm_ops;
4589 vma->vm_flags |= VM_DONTEXPAND;
4590 vma->vm_private_data = map;
4592 for (i = first; i <= last; i++) {
4594 if (todo > fh->v4l_buffers.buffer_size)
4595 todo = fh->v4l_buffers.buffer_size;
4596 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
4597 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
4598 todo, PAGE_SHARED)) {
4601 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
4604 goto v4l_mmap_unlock_and_return;
4608 fh->v4l_buffers.buffer[i].map = map;
4612 v4l_mmap_unlock_and_return:
4613 mutex_unlock(&zr->resource_lock);
4620 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
4621 ZR_DEVNAME(zr), fh->map_mode);
4628 static const struct file_operations zoran_fops = {
4629 .owner = THIS_MODULE,
4631 .release = zoran_close,
4632 .ioctl = zoran_ioctl,
4633 #ifdef CONFIG_COMPAT
4634 .compat_ioctl = v4l_compat_ioctl32,
4636 .llseek = no_llseek,
4638 .write = zoran_write,
4643 struct video_device zoran_template __devinitdata = {
4645 .fops = &zoran_fops,
4646 .release = &zoran_vdev_release,